1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "snippet_popover.h"
#include "mforms/imagebox.h"
#include "mforms/textentry.h"
#include "mforms/label.h"
#include "mforms/panel.h"
#include "mforms/app.h"
#include "mforms/code_editor.h"
#include "mforms/button.h"
#include "mforms/box.h"
#include "base/log.h"
using namespace std;
using namespace wb;
using namespace mforms;
//----------------- Separator ----------------------------------------------------------------------
void Separator::get_layout_size( int* w, int* h )
{
*w = 100; // Will be adjusted by the layout process.
*h = 4; // 2 pixels extra vertical space to match the design.
}
//--------------------------------------------------------------------------------------------------
void wb::Separator::repaint( cairo_t *cr, int x, int y, int w, int h )
{
float width = get_width() + 0.5f;
cairo_set_line_width(cr, 1);
cairo_set_source_rgb(cr, 212 / 255.0f, 212 / 255.0f, 212 / 255.0f);
cairo_move_to(cr, 0.5f, 0.5f);
cairo_line_to(cr, width, 0.5f);
cairo_stroke(cr);
cairo_set_source_rgb(cr, 246 / 255.0f, 246 / 255.0f, 246 / 255.0f);
cairo_move_to(cr, 0.5f, 1.5f);
cairo_line_to(cr, width, 1.5f);
cairo_stroke(cr);
}
//----------------- SnippetPopover -----------------------------------------------------------------
SnippetPopover::SnippetPopover()
: Popover(mforms::PopoverStyleNormal)
{
_content = manage(new Box(false));
_header = manage(new Box(true));
_header->set_spacing(10);
ImageBox* image = manage(new ImageBox());
image->set_image(mforms::App::get()->get_resource_path("snippet_sql.png"));
_heading_label = manage(new Label("Heading"), false);
_heading_label->set_style(mforms::BoldStyle);
_heading_entry = manage(new TextEntry(), false);
_header->add(image, false, true);
_header->add(_heading_label, true, true);
Separator* separator = manage(new Separator());
Panel* border_panel = manage(new Panel(mforms::FilledPanel));
border_panel->set_back_color("#cdcdcd");
border_panel->set_padding(1);
_editor = manage(new CodeEditor());
_editor->set_language(mforms::LanguageMySQL);
_editor->set_text("USE SQL CODE;");
_editor->set_features(mforms::FeatureGutter, false);
_editor->signal_changed()->connect(boost::bind(&SnippetPopover::text_changed, this, _1, _2));
border_panel->add(_editor);
Box* button_box = manage(new Box(true));
button_box->set_spacing(8);
_revert_button = manage(new Button(mforms::ToolButton));
_revert_button->set_tooltip("Discard all changes and revert to the current version");
_revert_button->set_icon(App::get()->get_resource_path(
#ifdef __APPLE__
"tiny_undo_mac.png"
#else
"tiny_undo.png"
#endif
));
_revert_button->signal_clicked()->connect(boost::bind(&SnippetPopover::revert_clicked, this));
_edit_button = manage(new Button());
_edit_button->set_text("Edit");
_edit_button->set_size(65, -1);
_edit_button->signal_clicked()->connect(boost::bind(&SnippetPopover::edit_clicked, this));
_close_button = manage(new Button());
_close_button->set_text("Done");
_close_button->set_size(65, -1);
_close_button->signal_clicked()->connect(boost::bind(&SnippetPopover::close_clicked, this));
button_box->add(_revert_button, false, true);
button_box->add_end(_close_button, false, true);
button_box->add_end(_edit_button, false, true);
_content->add(_header, false, true);
_content->add(separator, false, true);
_content->add(border_panel, true, true);
_content->add_end(button_box, false, true);
_content->set_spacing(4);
set_content(_content);
}
//--------------------------------------------------------------------------------------------------
SnippetPopover::~SnippetPopover()
{
_heading_label->release();
_heading_entry->release();
_content->release();
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::revert_clicked()
{
_heading_label->set_text(_original_heading);
_heading_entry->set_value(_original_heading);
_editor->set_value(_original_text);
_revert_button->set_enabled(false);
set_read_only(true);
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::edit_clicked()
{
set_read_only(false);
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::close_clicked()
{
close();
_closed();
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::text_changed(int start_line, int lines_changed)
{
_revert_button->set_enabled(true);
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::set_heading(const std::string& text)
{
_original_heading = text;
_heading_entry->set_value(text);
_heading_label->set_text(text);
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::set_text(const std::string& text)
{
_original_text = text;
_editor->set_value(text);
_revert_button->set_enabled(false);
}
//--------------------------------------------------------------------------------------------------
void SnippetPopover::set_read_only(bool flag)
{
// We have to exchange a label and a text entry, depending on the read-only state
// because we cannot give the text entry a display format and cannot edit a label.
if (flag)
{
_heading_label->set_text(_heading_entry->get_string_value());
if (_header->contains_subview(_heading_entry))
_header->remove(_heading_entry);
if (!_header->contains_subview(_heading_label))
_header->add(_heading_label, true, true);
}
else
{
if (_header->contains_subview(_heading_label))
_header->remove(_heading_label);
if (!_header->contains_subview(_heading_entry))
_header->add(_heading_entry, true, true);
_heading_entry->focus();
}
_editor->set_features(mforms::FeatureReadOnly, flag);
_edit_button->set_enabled(flag);
}
//--------------------------------------------------------------------------------------------------
std::string SnippetPopover::get_text()
{
return _editor->get_text(false);
}
//--------------------------------------------------------------------------------------------------
std::string SnippetPopover::get_heading()
{
return _heading_entry->get_string_value();
}
//--------------------------------------------------------------------------------------------------
bool SnippetPopover::has_changed()
{
// We don't have a change event from the text entry so we compare the content instead.
return _revert_button->is_enabled() || (_heading_entry->get_string_value() != _original_heading);
}
//--------------------------------------------------------------------------------------------------
|