1 |
/* |
/* |
2 |
Copyright (c) 2014-2016 Christian Schoenebeck |
Copyright (c) 2014-2017 Christian Schoenebeck |
3 |
|
|
4 |
This file is part of "gigedit" and released under the terms of the |
This file is part of "gigedit" and released under the terms of the |
5 |
GNU General Public License version 2. |
GNU General Public License version 2. |
12 |
|
|
13 |
static const std::string _keywords[] = { |
static const std::string _keywords[] = { |
14 |
"on", "end", "declare", "while", "if", "or", "and", "not", "else", "case", |
"on", "end", "declare", "while", "if", "or", "and", "not", "else", "case", |
15 |
"select", "to", "const", "polyphonic", "mod" |
"select", "to", "const", "polyphonic", "mod", "synchronized" |
16 |
}; |
}; |
17 |
static int _keywordsSz = sizeof(_keywords) / sizeof(std::string); |
static int _keywordsSz = sizeof(_keywords) / sizeof(std::string); |
18 |
|
|
41 |
int w = 0; |
int w = 0; |
42 |
int h = 0; // ignored |
int h = 0; // ignored |
43 |
Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h); |
Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h); |
44 |
|
if (!theme->has_icon(name)) |
45 |
|
return Glib::RefPtr<Gdk::Pixbuf>(); |
46 |
Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK); |
Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK); |
47 |
if (pixbuf->get_height() != targetH) { |
if (pixbuf->get_height() != targetH) { |
48 |
pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR); |
pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR); |
50 |
return pixbuf; |
return pixbuf; |
51 |
} |
} |
52 |
|
|
53 |
|
static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::vector<std::string> alternativeNames, const Glib::RefPtr<Gdk::Screen>& screen) { |
54 |
|
for (int i = 0; i < alternativeNames.size(); ++i) { |
55 |
|
Glib::RefPtr<Gdk::Pixbuf> buf = createIcon(alternativeNames[i], screen); |
56 |
|
if (buf) return buf; |
57 |
|
} |
58 |
|
return Glib::RefPtr<Gdk::Pixbuf>(); |
59 |
|
} |
60 |
|
|
61 |
ScriptEditor::ScriptEditor() : |
ScriptEditor::ScriptEditor() : |
62 |
m_statusLabel("", Gtk::ALIGN_START), |
m_statusLabel("", Gtk::ALIGN_START), |
63 |
m_applyButton(_("_Apply"), true), |
m_applyButton(Gtk::Stock::APPLY), |
64 |
m_cancelButton(_("_Cancel"), true) |
m_cancelButton(Gtk::Stock::CANCEL) |
65 |
{ |
{ |
66 |
m_script = NULL; |
m_script = NULL; |
67 |
#if USE_LS_SCRIPTVM |
#if USE_LS_SCRIPTVM |
68 |
m_vm = NULL; |
m_vm = NULL; |
69 |
#endif |
#endif |
70 |
|
|
71 |
m_errorIcon = createIcon("dialog-error", get_screen()); |
if (!Settings::singleton()->autoRestoreWindowDimension) { |
72 |
m_warningIcon = createIcon("dialog-warning-symbolic", get_screen()); |
set_default_size(800, 700); |
73 |
m_successIcon = createIcon("emblem-default", get_screen()); |
set_position(Gtk::WIN_POS_MOUSE); |
74 |
|
} |
75 |
|
|
76 |
|
// depending on GTK version and installed themes, there may be different |
77 |
|
// icons, and different names for them, so for each type of icon we use, |
78 |
|
// we provide a list of possible icon names, the first one found to be |
79 |
|
// installed on the local system from the list will be used and loaded for |
80 |
|
// the respective purpose (so order matters in those lists) |
81 |
|
// |
82 |
|
// (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for |
83 |
|
// available icon names) |
84 |
|
std::vector<std::string> errorIconNames; |
85 |
|
errorIconNames.push_back("dialog-error"); |
86 |
|
errorIconNames.push_back("media-record"); |
87 |
|
errorIconNames.push_back("process-stop"); |
88 |
|
|
89 |
|
std::vector<std::string> warningIconNames; |
90 |
|
warningIconNames.push_back("dialog-warning-symbolic"); |
91 |
|
warningIconNames.push_back("dialog-warning"); |
92 |
|
|
93 |
|
std::vector<std::string> successIconNames; |
94 |
|
successIconNames.push_back("emblem-default"); |
95 |
|
successIconNames.push_back("tools-check-spelling"); |
96 |
|
|
97 |
|
m_errorIcon = createIcon(errorIconNames, get_screen()); |
98 |
|
m_warningIcon = createIcon(warningIconNames, get_screen()); |
99 |
|
m_successIcon = createIcon(successIconNames, get_screen()); |
100 |
|
|
101 |
add(m_vbox); |
add(m_vbox); |
102 |
|
|
132 |
m_commentTag->property_foreground() = "#9c9c9c"; // gray |
m_commentTag->property_foreground() = "#9c9c9c"; // gray |
133 |
m_tagTable->add(m_commentTag); |
m_tagTable->add(m_commentTag); |
134 |
|
|
135 |
|
#define PREPROC_TOKEN_COLOR "#2f8a33" // green |
136 |
|
|
137 |
m_preprocTag = Gtk::TextBuffer::Tag::create(); |
m_preprocTag = Gtk::TextBuffer::Tag::create(); |
138 |
m_preprocTag->property_foreground() = "#2f8a33"; // green |
m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR; |
139 |
m_tagTable->add(m_preprocTag); |
m_tagTable->add(m_preprocTag); |
140 |
|
|
141 |
|
m_preprocCommentTag = Gtk::TextBuffer::Tag::create(); |
142 |
|
m_preprocCommentTag->property_strikethrough() = true; |
143 |
|
m_preprocCommentTag->property_background() = "#e5e5e5"; |
144 |
|
m_tagTable->add(m_preprocCommentTag); |
145 |
|
|
146 |
m_errorTag = Gtk::TextBuffer::Tag::create(); |
m_errorTag = Gtk::TextBuffer::Tag::create(); |
147 |
m_errorTag->property_background() = "#ff9393"; // red |
m_errorTag->property_background() = "#ff9393"; // red |
148 |
m_tagTable->add(m_errorTag); |
m_tagTable->add(m_errorTag); |
160 |
m_actionGroup->add(Gtk::Action::create("Close", _("_Close")), |
m_actionGroup->add(Gtk::Action::create("Close", _("_Close")), |
161 |
Gtk::AccelKey("<control>q"), |
Gtk::AccelKey("<control>q"), |
162 |
sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)); |
sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)); |
163 |
|
m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor"))); |
164 |
|
m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")), |
165 |
|
sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize)); |
166 |
m_uiManager = Gtk::UIManager::create(); |
m_uiManager = Gtk::UIManager::create(); |
167 |
m_uiManager->insert_action_group(m_actionGroup); |
m_uiManager->insert_action_group(m_actionGroup); |
168 |
add_accel_group(m_uiManager->get_accel_group()); |
add_accel_group(m_uiManager->get_accel_group()); |
174 |
" <separator/>" |
" <separator/>" |
175 |
" <menuitem action='Close'/>" |
" <menuitem action='Close'/>" |
176 |
" </menu>" |
" </menu>" |
177 |
|
" <menu action='MenuEditor'>" |
178 |
|
" <menuitem action='ChangeFont'/>" |
179 |
|
" </menu>" |
180 |
" </menubar>" |
" </menubar>" |
181 |
"</ui>" |
"</ui>" |
182 |
); |
); |
183 |
|
|
184 |
m_textBuffer = Gtk::TextBuffer::create(m_tagTable); |
m_textBuffer = Gtk::TextBuffer::create(m_tagTable); |
185 |
m_textView.set_buffer(m_textBuffer); |
m_textView.set_buffer(m_textBuffer); |
186 |
{ |
setFontSize(currentFontSize(), false); |
|
Pango::FontDescription fdesc; |
|
|
fdesc.set_family("monospace"); |
|
|
#if defined(__APPLE__) |
|
|
fdesc.set_size(12 * PANGO_SCALE); |
|
|
#else |
|
|
fdesc.set_size(10 * PANGO_SCALE); |
|
|
#endif |
|
|
#if GTKMM_MAJOR_VERSION < 3 |
|
|
m_textView.modify_font(fdesc); |
|
|
#else |
|
|
m_textView.override_font(fdesc); |
|
|
#endif |
|
|
} |
|
187 |
m_scrolledWindow.add(m_textView); |
m_scrolledWindow.add(m_textView); |
188 |
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
189 |
|
|
252 |
#endif |
#endif |
253 |
} |
} |
254 |
|
|
255 |
|
int ScriptEditor::currentFontSize() const { |
256 |
|
#if defined(__APPLE__) |
257 |
|
const int defaultFontSize = 13; |
258 |
|
#else |
259 |
|
const int defaultFontSize = 10; |
260 |
|
#endif |
261 |
|
const int settingFontSize = Settings::singleton()->scriptEditorFontSize; |
262 |
|
const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize; |
263 |
|
return fontSize; |
264 |
|
} |
265 |
|
|
266 |
|
void ScriptEditor::setFontSize(int size, bool save) { |
267 |
|
//printf("setFontSize(%d,%d)\n", size, save); |
268 |
|
Pango::FontDescription fdesc; |
269 |
|
fdesc.set_family("monospace"); |
270 |
|
fdesc.set_size(size * PANGO_SCALE); |
271 |
|
#if GTKMM_MAJOR_VERSION < 3 |
272 |
|
m_textView.modify_font(fdesc); |
273 |
|
#else |
274 |
|
m_textView.override_font(fdesc); |
275 |
|
#endif |
276 |
|
if (save) Settings::singleton()->scriptEditorFontSize = size; |
277 |
|
} |
278 |
|
|
279 |
void ScriptEditor::setScript(gig::Script* script) { |
void ScriptEditor::setScript(gig::Script* script) { |
280 |
m_script = script; |
m_script = script; |
281 |
if (!script) { |
if (!script) { |
350 |
return m_vm; |
return m_vm; |
351 |
} |
} |
352 |
|
|
353 |
static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) { |
template<class T> |
354 |
start = txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1); |
static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) { |
355 |
|
Gtk::TextBuffer::iterator itLine = |
356 |
|
txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0); |
357 |
|
const int charsInLine = itLine.get_bytes_in_line(); |
358 |
|
start = txtbuf->get_iter_at_line_index( |
359 |
|
issue.firstLine - 1, |
360 |
|
// check we are not getting past the end of the line here, otherwise Gtk crashes |
361 |
|
issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1 |
362 |
|
); |
363 |
end = start; |
end = start; |
364 |
end.forward_lines(issue.lastLine - issue.firstLine); |
end.forward_lines(issue.lastLine - issue.firstLine); |
365 |
end.forward_chars( |
end.forward_chars( |
370 |
} |
} |
371 |
|
|
372 |
static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) { |
static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) { |
373 |
Gtk::TextBuffer::iterator itStart = |
Gtk::TextBuffer::iterator itLine = |
374 |
txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn()); |
txtbuf->get_iter_at_line_index(token.firstLine(), 0); |
375 |
|
const int charsInLine = itLine.get_bytes_in_line(); |
376 |
|
Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index( |
377 |
|
token.firstLine(), |
378 |
|
// check we are not getting past the end of the line here, otherwise Gtk crashes |
379 |
|
token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1 |
380 |
|
); |
381 |
Gtk::TextBuffer::iterator itEnd = itStart; |
Gtk::TextBuffer::iterator itEnd = itStart; |
382 |
const int length = token.text().length(); |
const int length = token.text().length(); |
383 |
itEnd.forward_chars(length); |
itEnd.forward_chars(length); |
390 |
txtbuf->apply_tag(tag, itStart, itEnd); |
txtbuf->apply_tag(tag, itStart, itEnd); |
391 |
} |
} |
392 |
|
|
393 |
|
static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) { |
394 |
|
Gtk::TextBuffer::iterator itStart, itEnd; |
395 |
|
getIteratorsForIssue(txtbuf, block, itStart, itEnd); |
396 |
|
txtbuf->apply_tag(tag, itStart, itEnd); |
397 |
|
} |
398 |
|
|
399 |
void ScriptEditor::updateSyntaxHighlightingByVM() { |
void ScriptEditor::updateSyntaxHighlightingByVM() { |
400 |
GetScriptVM(); |
GetScriptVM(); |
401 |
const std::string s = m_textBuffer->get_text(); |
const std::string s = m_textBuffer->get_text(); |
402 |
|
if (s.empty()) return; |
403 |
std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s); |
std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s); |
404 |
|
|
405 |
for (int i = 0; i < tokens.size(); ++i) { |
for (int i = 0; i < tokens.size(); ++i) { |
435 |
m_issues = parserContext->issues(); |
m_issues = parserContext->issues(); |
436 |
m_errors = parserContext->errors(); |
m_errors = parserContext->errors(); |
437 |
m_warnings = parserContext->warnings(); |
m_warnings = parserContext->warnings(); |
438 |
|
m_preprocComments = parserContext->preprocessorComments(); |
439 |
|
|
440 |
for (int i = 0; i < m_issues.size(); ++i) { |
if (!s.empty()) { |
441 |
const LinuxSampler::ParserIssue& issue = m_issues[i]; |
for (int i = 0; i < m_issues.size(); ++i) { |
442 |
|
const LinuxSampler::ParserIssue& issue = m_issues[i]; |
443 |
if (issue.isErr()) { |
|
444 |
applyCodeTag(m_textBuffer, issue, m_errorTag); |
if (issue.isErr()) { |
445 |
} else if (issue.isWrn()) { |
applyCodeTag(m_textBuffer, issue, m_errorTag); |
446 |
applyCodeTag(m_textBuffer, issue, m_warningTag); |
} else if (issue.isWrn()) { |
447 |
|
applyCodeTag(m_textBuffer, issue, m_warningTag); |
448 |
|
} |
449 |
} |
} |
450 |
} |
} |
451 |
|
|
452 |
|
for (int i = 0; i < m_preprocComments.size(); ++i) { |
453 |
|
applyPreprocessorComment(m_textBuffer, m_preprocComments[i], |
454 |
|
m_preprocCommentTag); |
455 |
|
} |
456 |
|
|
457 |
delete parserContext; |
delete parserContext; |
458 |
} |
} |
459 |
|
|
487 |
} |
} |
488 |
} |
} |
489 |
|
|
490 |
|
for (int i = 0; i < m_preprocComments.size(); ++i) { |
491 |
|
const LinuxSampler::CodeBlock& block = m_preprocComments[i]; |
492 |
|
const int firstLine = block.firstLine - 1; |
493 |
|
const int firstColumn = block.firstColumn - 1; |
494 |
|
const int lastLine = block.lastLine - 1; |
495 |
|
const int lastColumn = block.lastColumn - 1; |
496 |
|
if (firstLine <= line && line <= lastLine && |
497 |
|
(firstLine != line || firstColumn <= column) && |
498 |
|
(lastLine != line || lastColumn >= column)) |
499 |
|
{ |
500 |
|
m_textView.set_tooltip_markup( |
501 |
|
"Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement." |
502 |
|
); |
503 |
|
return; |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
m_textView.set_tooltip_markup(""); |
m_textView.set_tooltip_markup(""); |
508 |
} |
} |
509 |
|
|
576 |
return ManagedWindow::on_motion_notify_event(e); |
return ManagedWindow::on_motion_notify_event(e); |
577 |
} |
} |
578 |
|
|
579 |
|
void ScriptEditor::onMenuChangeFontSize() { |
580 |
|
//TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead |
581 |
|
Gtk::Dialog dialog(_("Font Size"), true /*modal*/); |
582 |
|
Gtk::HBox hbox; |
583 |
|
hbox.set_spacing(6); |
584 |
|
|
585 |
|
Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START); |
586 |
|
hbox.pack_start(label, Gtk::PACK_SHRINK); |
587 |
|
|
588 |
|
Gtk::SpinButton spinButton; |
589 |
|
spinButton.set_range(4, 80); |
590 |
|
spinButton.set_increments(1, 10); |
591 |
|
spinButton.set_value(currentFontSize()); |
592 |
|
hbox.pack_start(spinButton); |
593 |
|
|
594 |
|
dialog.get_vbox()->pack_start(hbox); |
595 |
|
dialog.add_button(_("_OK"), 0); |
596 |
|
dialog.add_button(_("_Cancel"), 1); |
597 |
|
|
598 |
|
dialog.show_all_children(); |
599 |
|
|
600 |
|
if (!dialog.run()) { // OK selected ... |
601 |
|
const int newFontSize = spinButton.get_value_as_int(); |
602 |
|
if (newFontSize >= 4) |
603 |
|
setFontSize(newFontSize, true); |
604 |
|
} |
605 |
|
} |
606 |
|
|
607 |
bool ScriptEditor::onWindowDelete(GdkEventAny* e) { |
bool ScriptEditor::onWindowDelete(GdkEventAny* e) { |
608 |
//printf("onWindowDelete\n"); |
//printf("onWindowDelete\n"); |
609 |
|
|