7 |
|
|
8 |
#include "scripteditor.h" |
#include "scripteditor.h" |
9 |
#include "global.h" |
#include "global.h" |
10 |
|
#include "compat.h" |
11 |
#include <gtk/gtkwidget.h> // for gtk_widget_modify_*() |
#include <gtk/gtkwidget.h> // for gtk_widget_modify_*() |
12 |
#if defined(__APPLE__) |
#if defined(__APPLE__) |
13 |
# include "MacHelper.h" |
# include "MacHelper.h" |
66 |
|
|
67 |
ScriptEditor::ScriptEditor() : |
ScriptEditor::ScriptEditor() : |
68 |
m_statusLabel("", Gtk::ALIGN_START), |
m_statusLabel("", Gtk::ALIGN_START), |
69 |
|
#if HAS_GTKMM_STOCK |
70 |
m_applyButton(Gtk::Stock::APPLY), |
m_applyButton(Gtk::Stock::APPLY), |
71 |
m_cancelButton(Gtk::Stock::CANCEL) |
m_cancelButton(Gtk::Stock::CANCEL) |
72 |
|
#else |
73 |
|
m_applyButton(_("_Apply"), true), |
74 |
|
m_cancelButton(_("_Cancel"), true) |
75 |
|
#endif |
76 |
{ |
{ |
77 |
m_script = NULL; |
m_script = NULL; |
78 |
#if USE_LS_SCRIPTVM |
#if USE_LS_SCRIPTVM |
167 |
m_tagTable->add(m_lineNrTag); |
m_tagTable->add(m_lineNrTag); |
168 |
|
|
169 |
// create menu |
// create menu |
170 |
|
#if USE_GTKMM_BUILDER |
171 |
|
m_actionGroup = Gio::SimpleActionGroup::create(); |
172 |
|
m_actionGroup->add_action( |
173 |
|
"Apply", sigc::mem_fun(*this, &ScriptEditor::onButtonApply) |
174 |
|
); |
175 |
|
m_actionGroup->add_action( |
176 |
|
"Close", sigc::mem_fun(*this, &ScriptEditor::onButtonCancel) |
177 |
|
); |
178 |
|
m_actionGroup->add_action( |
179 |
|
"ChangeFont", sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize) |
180 |
|
); |
181 |
|
insert_action_group("ScriptEditor", m_actionGroup); |
182 |
|
|
183 |
|
m_uiManager = Gtk::Builder::create(); |
184 |
|
Glib::ustring ui_info = |
185 |
|
"<interface>" |
186 |
|
" <menubar id='MenuBar'>" |
187 |
|
" <menu id='MenuScript'>" |
188 |
|
" <section>" |
189 |
|
" <item id='Apply'>" |
190 |
|
" <attribute name='label' translatable='yes'>_Apply</attribute>" |
191 |
|
" <attribute name='action'>ScriptEditor.Apply</attribute>" |
192 |
|
" <attribute name='accel'><Primary>s</attribute>" |
193 |
|
" </item>" |
194 |
|
" </section>" |
195 |
|
" <section>" |
196 |
|
" <item id='Close'>" |
197 |
|
" <attribute name='label' translatable='yes'>_Close</attribute>" |
198 |
|
" <attribute name='action'>ScriptEditor.Close</attribute>" |
199 |
|
" <attribute name='accel'><Primary>q</attribute>" |
200 |
|
" </item>" |
201 |
|
" </section>" |
202 |
|
" </menu>" |
203 |
|
" <menu id='MenuEditor'>" |
204 |
|
" <section>" |
205 |
|
" <item id='ChangeFont'>" |
206 |
|
" <attribute name='label' translatable='yes'>_Font Size ...</attribute>" |
207 |
|
" <attribute name='action'>ScriptEditor.ChangeFont</attribute>" |
208 |
|
" </item>" |
209 |
|
" </section>" |
210 |
|
" </menu>" |
211 |
|
" </menubar>" |
212 |
|
"</interface>"; |
213 |
|
m_uiManager->add_from_string(ui_info); |
214 |
|
/*{ |
215 |
|
auto object = uiManager->get_object("MenuBar"); |
216 |
|
auto gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object); |
217 |
|
set_menubar(gmenu); |
218 |
|
}*/ |
219 |
|
#else |
220 |
m_actionGroup = Gtk::ActionGroup::create(); |
m_actionGroup = Gtk::ActionGroup::create(); |
221 |
m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script"))); |
m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script"))); |
222 |
m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")), |
m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")), |
245 |
" </menubar>" |
" </menubar>" |
246 |
"</ui>" |
"</ui>" |
247 |
); |
); |
248 |
|
#endif |
249 |
|
|
250 |
m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable); |
m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable); |
251 |
|
m_lineNrView.set_size_request(22,14); |
252 |
m_lineNrView.set_buffer(m_lineNrBuffer); |
m_lineNrView.set_buffer(m_lineNrBuffer); |
253 |
m_lineNrView.set_left_margin(3); |
m_lineNrView.set_left_margin(3); |
254 |
m_lineNrView.set_right_margin(3); |
m_lineNrView.set_right_margin(3); |
255 |
m_lineNrTextViewSpacer.set_size_request(5); |
m_lineNrView.property_editable() = false; |
256 |
|
m_lineNrView.property_sensitive() = false; |
257 |
|
m_lineNrTextViewSpacer.set_size_request(5,14); |
258 |
|
m_lineNrTextViewSpacer.property_editable() = false; |
259 |
|
m_lineNrTextViewSpacer.property_sensitive() = false; |
260 |
{ |
{ |
261 |
|
#if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 |
262 |
Gdk::Color color; |
Gdk::Color color; |
263 |
|
#else |
264 |
|
Gdk::RGBA color; |
265 |
|
#endif |
266 |
color.set("#F5F5F5"); |
color.set("#F5F5F5"); |
267 |
GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj(); |
GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj(); |
268 |
|
#if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 22) |
269 |
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); |
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); |
270 |
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); |
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); |
271 |
|
#endif |
272 |
} |
} |
273 |
{ |
{ |
274 |
|
#if 1 //(GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 90) || GTKMM_MAJOR_VERSION < 2 |
275 |
Gdk::Color color; |
Gdk::Color color; |
276 |
|
#else |
277 |
|
Gdk::RGBA color; |
278 |
|
#endif |
279 |
color.set("#EEEEEE"); |
color.set("#EEEEEE"); |
280 |
GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj(); |
GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj(); |
281 |
|
#if GTK_MAJOR_VERSION < 3 || (GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION <= 22) |
282 |
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); |
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); |
283 |
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); |
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); |
284 |
|
#endif |
285 |
} |
} |
286 |
m_textBuffer = Gtk::TextBuffer::create(m_tagTable); |
m_textBuffer = Gtk::TextBuffer::create(m_tagTable); |
287 |
m_textView.set_buffer(m_textBuffer); |
m_textView.set_buffer(m_textBuffer); |
293 |
m_scrolledWindow.add(m_textViewHBox); |
m_scrolledWindow.add(m_textViewHBox); |
294 |
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
295 |
|
|
296 |
|
#if USE_GTKMM_BUILDER |
297 |
|
Gtk::Widget* menuBar = new Gtk::MenuBar( |
298 |
|
Glib::RefPtr<Gio::Menu>::cast_dynamic( |
299 |
|
m_uiManager->get_object("MenuBar") |
300 |
|
) |
301 |
|
); |
302 |
|
#else |
303 |
Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar"); |
Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar"); |
304 |
|
#endif |
305 |
|
|
306 |
m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK); |
m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK); |
307 |
m_vbox.pack_start(m_scrolledWindow); |
m_vbox.pack_start(m_scrolledWindow); |
308 |
|
|
313 |
m_applyButton.set_sensitive(false); |
m_applyButton.set_sensitive(false); |
314 |
m_applyButton.grab_focus(); |
m_applyButton.grab_focus(); |
315 |
|
|
316 |
#if GTKMM_MAJOR_VERSION >= 3 |
#if GTKMM_MAJOR_VERSION < 3 |
317 |
|
m_statusHBox.set_spacing(6); |
318 |
|
#elif GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 12 |
319 |
m_statusImage.set_margin_left(6); |
m_statusImage.set_margin_left(6); |
320 |
m_statusImage.set_margin_right(6); |
m_statusImage.set_margin_right(6); |
321 |
#else |
#else |
322 |
m_statusHBox.set_spacing(6); |
m_statusImage.set_margin_start(6); |
323 |
|
m_statusImage.set_margin_end(6); |
324 |
#endif |
#endif |
325 |
|
|
326 |
m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK); |
m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK); |
327 |
m_statusHBox.pack_start(m_statusLabel); |
m_statusHBox.pack_start(m_statusLabel); |
328 |
|
#if HAS_GTKMM_SHOW_ALL_CHILDREN |
329 |
m_statusHBox.show_all_children(); |
m_statusHBox.show_all_children(); |
330 |
|
#endif |
331 |
|
|
332 |
m_footerHBox.pack_start(m_statusHBox); |
m_footerHBox.pack_start(m_statusHBox); |
333 |
m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK); |
m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK); |
359 |
); |
); |
360 |
|
|
361 |
signal_delete_event().connect( |
signal_delete_event().connect( |
362 |
|
#if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2 |
363 |
sigc::mem_fun(*this, &ScriptEditor::onWindowDelete) |
sigc::mem_fun(*this, &ScriptEditor::onWindowDelete) |
364 |
|
#else |
365 |
|
sigc::mem_fun(*this, &ScriptEditor::onWindowDeleteP) |
366 |
|
#endif |
367 |
); |
); |
368 |
|
|
369 |
|
#if HAS_GTKMM_SHOW_ALL_CHILDREN |
370 |
show_all_children(); |
show_all_children(); |
371 |
|
#endif |
372 |
} |
} |
373 |
|
|
374 |
ScriptEditor::~ScriptEditor() { |
ScriptEditor::~ScriptEditor() { |
389 |
return fontSize; |
return fontSize; |
390 |
} |
} |
391 |
|
|
392 |
void ScriptEditor::setFontSize(int size, bool save) { |
void ScriptEditor::setFontSize(int sizePt, bool save) { |
393 |
//printf("setFontSize(%d,%d)\n", size, save); |
//printf("setFontSize(%d,%d)\n", size, save); |
394 |
|
|
395 |
|
// make sure the real size on the screen for the editor's font is consistent |
396 |
|
// on all screens (which otherwise may vary between models and DPI settings) |
397 |
|
const double referenceDPI = 96; |
398 |
|
double dpi = Gdk::Screen::get_default()->get_resolution(); |
399 |
|
double sizePx = sizePt * dpi / referenceDPI; |
400 |
|
|
401 |
|
#if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION < 20) |
402 |
Pango::FontDescription fdesc; |
Pango::FontDescription fdesc; |
403 |
fdesc.set_family("monospace"); |
fdesc.set_family("monospace"); |
404 |
#if defined(__APPLE__) |
# if defined(__APPLE__) |
405 |
// fixes poor readability of default monospace font on Macs |
// fixes poor readability of default monospace font on Macs |
406 |
if (macIsMinMac10_6()) |
if (macIsMinMac10_6()) |
407 |
fdesc.set_family("Menlo"); |
fdesc.set_family("Menlo"); |
408 |
#endif |
# endif |
409 |
fdesc.set_size(size * PANGO_SCALE); |
fdesc.set_size(sizePx * PANGO_SCALE); |
410 |
/*Glib::RefPtr<Pango::Context> context = m_textView.get_pango_context(); |
# if GTKMM_MAJOR_VERSION < 3 |
|
Cairo::FontOptions options; |
|
|
options.set_antialias(Cairo::ANTIALIAS_NONE); |
|
|
context->set_cairo_font_options(options);*/ |
|
|
#if GTKMM_MAJOR_VERSION < 3 |
|
411 |
m_lineNrView.modify_font(fdesc); |
m_lineNrView.modify_font(fdesc); |
412 |
m_textView.modify_font(fdesc); |
m_textView.modify_font(fdesc); |
413 |
#else |
# else |
414 |
m_lineNrView.override_font(fdesc); |
m_lineNrView.override_font(fdesc); |
415 |
m_textView.override_font(fdesc); |
m_textView.override_font(fdesc); |
416 |
|
# endif |
417 |
|
#else |
418 |
|
Glib::ustring family = "monospace"; |
419 |
|
# if defined(__APPLE__) |
420 |
|
// fixes poor readability of default monospace font on Macs |
421 |
|
if (macIsMinMac10_6()) |
422 |
|
family = "Menlo"; |
423 |
|
# endif |
424 |
|
if (!m_css) { |
425 |
|
m_css = Gtk::CssProvider::create(); |
426 |
|
m_lineNrView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK); |
427 |
|
m_textView.get_style_context()->add_provider(m_css, GTK_STYLE_PROVIDER_PRIORITY_FALLBACK); |
428 |
|
} |
429 |
|
m_css->load_from_data( |
430 |
|
"* {" |
431 |
|
" font: " + ToString(sizePt) + "pt " + family + ";" |
432 |
|
"}" |
433 |
|
); |
434 |
#endif |
#endif |
435 |
if (save) Settings::singleton()->scriptEditorFontSize = size; |
if (save) Settings::singleton()->scriptEditorFontSize = sizePt; |
436 |
} |
} |
437 |
|
|
438 |
void ScriptEditor::setScript(gig::Script* script) { |
void ScriptEditor::setScript(gig::Script* script) { |
448 |
//printf("text : '%s'\n", txt.c_str()); |
//printf("text : '%s'\n", txt.c_str()); |
449 |
m_textBuffer->set_text(txt); |
m_textBuffer->set_text(txt); |
450 |
m_textBuffer->set_modified(false); |
m_textBuffer->set_modified(false); |
451 |
|
|
452 |
|
// on Gtk 3 the respective text change callback would not be called, so force this update here |
453 |
|
if (txt.empty()) |
454 |
|
updateLineNumbers(); |
455 |
} |
} |
456 |
|
|
457 |
void ScriptEditor::updateLineNumbers() { |
void ScriptEditor::updateLineNumbers() { |
458 |
const int n = m_textBuffer->get_line_count(); |
int n = m_textBuffer->get_line_count(); |
459 |
const int old = m_lineNrBuffer->get_line_count(); |
int old = m_lineNrBuffer->get_line_count(); |
460 |
if (n == old) return; |
if (n == old && old > 1) return; |
461 |
|
if (n < 1) n = 1; |
462 |
const int digits = log10(n) + 1; |
const int digits = log10(n) + 1; |
463 |
const int bufSz = digits + 2; |
const int bufSz = digits + 2; |
464 |
char* buf = new char[bufSz]; |
char* buf = new char[bufSz]; |
759 |
//TODO: event throttling would be a good idea here |
//TODO: event throttling would be a good idea here |
760 |
updateIssueTooltip(e); |
updateIssueTooltip(e); |
761 |
#endif |
#endif |
762 |
|
#if GTKMM_MAJOR_VERSION < 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION <= 22) |
763 |
return ManagedWindow::on_motion_notify_event(e); |
return ManagedWindow::on_motion_notify_event(e); |
764 |
|
#else |
765 |
|
Gdk::EventMotion em = Glib::wrap(e, true); |
766 |
|
return ManagedWindow::on_motion_notify_event(em); |
767 |
|
#endif |
768 |
} |
} |
769 |
|
|
770 |
void ScriptEditor::onMenuChangeFontSize() { |
void ScriptEditor::onMenuChangeFontSize() { |
771 |
//TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead |
//TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead |
772 |
Gtk::Dialog dialog(_("Font Size"), true /*modal*/); |
Gtk::Dialog dialog(_("Font Size"), true /*modal*/); |
773 |
Gtk::HBox hbox; |
HBox hbox; |
774 |
hbox.set_spacing(6); |
hbox.set_spacing(6); |
775 |
|
|
776 |
Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START); |
Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START); |
782 |
spinButton.set_value(currentFontSize()); |
spinButton.set_value(currentFontSize()); |
783 |
hbox.pack_start(spinButton); |
hbox.pack_start(spinButton); |
784 |
|
|
785 |
|
#if USE_GTKMM_BOX |
786 |
|
dialog.get_content_area()->pack_start(hbox); |
787 |
|
#else |
788 |
dialog.get_vbox()->pack_start(hbox); |
dialog.get_vbox()->pack_start(hbox); |
789 |
|
#endif |
790 |
dialog.add_button(_("_OK"), 0); |
dialog.add_button(_("_OK"), 0); |
791 |
dialog.add_button(_("_Cancel"), 1); |
dialog.add_button(_("_Cancel"), 1); |
792 |
|
|
793 |
|
#if HAS_GTKMM_SHOW_ALL_CHILDREN |
794 |
dialog.show_all_children(); |
dialog.show_all_children(); |
795 |
|
#endif |
796 |
|
|
797 |
if (!dialog.run()) { // OK selected ... |
if (!dialog.run()) { // OK selected ... |
798 |
const int newFontSize = spinButton.get_value_as_int(); |
const int newFontSize = spinButton.get_value_as_int(); |
801 |
} |
} |
802 |
} |
} |
803 |
|
|
804 |
bool ScriptEditor::onWindowDelete(GdkEventAny* e) { |
#if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && (GTKMM_MINOR_VERSION > 91 || (GTKMM_MINOR_VERSION == 91 && GTKMM_MICRO_VERSION >= 2))) // GTKMM >= 3.91.2 |
805 |
|
bool ScriptEditor::onWindowDelete(Gdk::Event& e) { |
806 |
|
return onWindowDeleteP(NULL); |
807 |
|
} |
808 |
|
#endif |
809 |
|
|
810 |
|
bool ScriptEditor::onWindowDeleteP(GdkEventAny* /*e*/) { |
811 |
//printf("onWindowDelete\n"); |
//printf("onWindowDelete\n"); |
812 |
|
|
813 |
if (!isModified()) return false; // propagate event further (which will close this window) |
if (!isModified()) return false; // propagate event further (which will close this window) |
856 |
} |
} |
857 |
|
|
858 |
void ScriptEditor::onButtonCancel() { |
void ScriptEditor::onButtonCancel() { |
859 |
bool dropEvent = onWindowDelete(NULL); |
bool dropEvent = onWindowDeleteP(NULL); |
860 |
if (dropEvent) return; |
if (dropEvent) return; |
861 |
hide(); |
hide(); |
862 |
} |
} |