1 |
schoenebeck |
2604 |
/* |
2 |
schoenebeck |
3225 |
Copyright (c) 2014-2017 Christian Schoenebeck |
3 |
schoenebeck |
2604 |
|
4 |
|
|
This file is part of "gigedit" and released under the terms of the |
5 |
|
|
GNU General Public License version 2. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "scripteditor.h" |
9 |
|
|
#include "global.h" |
10 |
schoenebeck |
3310 |
#include <gtk/gtkwidget.h> // for gtk_widget_modify_*() |
11 |
schoenebeck |
2604 |
|
12 |
schoenebeck |
2886 |
#if !USE_LS_SCRIPTVM |
13 |
|
|
|
14 |
schoenebeck |
2604 |
static const std::string _keywords[] = { |
15 |
|
|
"on", "end", "declare", "while", "if", "or", "and", "not", "else", "case", |
16 |
schoenebeck |
3261 |
"select", "to", "const", "polyphonic", "mod", "synchronized" |
17 |
schoenebeck |
2604 |
}; |
18 |
|
|
static int _keywordsSz = sizeof(_keywords) / sizeof(std::string); |
19 |
|
|
|
20 |
|
|
static const std::string _eventNames[] = { |
21 |
|
|
"init", "note", "release", "controller" |
22 |
|
|
}; |
23 |
|
|
static int _eventNamesSz = sizeof(_eventNames) / sizeof(std::string); |
24 |
|
|
|
25 |
|
|
static bool isKeyword(const Glib::ustring& s) { |
26 |
|
|
for (int i = 0; i < _keywordsSz; ++i) |
27 |
|
|
if (_keywords[i] == s) return true; |
28 |
|
|
return false; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
static bool isEvent(const Glib::ustring& s) { |
32 |
|
|
for (int i = 0; i < _eventNamesSz; ++i) |
33 |
|
|
if (_eventNames[i] == s) return true; |
34 |
|
|
return false; |
35 |
|
|
} |
36 |
|
|
|
37 |
schoenebeck |
2886 |
#endif // !USE_LS_SCRIPTVM |
38 |
|
|
|
39 |
schoenebeck |
2899 |
static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::string name, const Glib::RefPtr<Gdk::Screen>& screen) { |
40 |
|
|
const int targetH = 16; |
41 |
|
|
Glib::RefPtr<Gtk::IconTheme> theme = Gtk::IconTheme::get_for_screen(screen); |
42 |
|
|
int w = 0; |
43 |
|
|
int h = 0; // ignored |
44 |
|
|
Gtk::IconSize::lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR, w, h); |
45 |
schoenebeck |
2928 |
if (!theme->has_icon(name)) |
46 |
|
|
return Glib::RefPtr<Gdk::Pixbuf>(); |
47 |
schoenebeck |
2899 |
Glib::RefPtr<Gdk::Pixbuf> pixbuf = theme->load_icon(name, w, Gtk::ICON_LOOKUP_GENERIC_FALLBACK); |
48 |
|
|
if (pixbuf->get_height() != targetH) { |
49 |
|
|
pixbuf = pixbuf->scale_simple(targetH, targetH, Gdk::INTERP_BILINEAR); |
50 |
|
|
} |
51 |
|
|
return pixbuf; |
52 |
|
|
} |
53 |
|
|
|
54 |
schoenebeck |
2928 |
static Glib::RefPtr<Gdk::Pixbuf> createIcon(std::vector<std::string> alternativeNames, const Glib::RefPtr<Gdk::Screen>& screen) { |
55 |
|
|
for (int i = 0; i < alternativeNames.size(); ++i) { |
56 |
|
|
Glib::RefPtr<Gdk::Pixbuf> buf = createIcon(alternativeNames[i], screen); |
57 |
|
|
if (buf) return buf; |
58 |
|
|
} |
59 |
|
|
return Glib::RefPtr<Gdk::Pixbuf>(); |
60 |
|
|
} |
61 |
|
|
|
62 |
schoenebeck |
2604 |
ScriptEditor::ScriptEditor() : |
63 |
schoenebeck |
2899 |
m_statusLabel("", Gtk::ALIGN_START), |
64 |
schoenebeck |
3158 |
m_applyButton(Gtk::Stock::APPLY), |
65 |
|
|
m_cancelButton(Gtk::Stock::CANCEL) |
66 |
schoenebeck |
2604 |
{ |
67 |
|
|
m_script = NULL; |
68 |
schoenebeck |
2896 |
#if USE_LS_SCRIPTVM |
69 |
schoenebeck |
2886 |
m_vm = NULL; |
70 |
schoenebeck |
2896 |
#endif |
71 |
schoenebeck |
2604 |
|
72 |
schoenebeck |
3225 |
if (!Settings::singleton()->autoRestoreWindowDimension) { |
73 |
|
|
set_default_size(800, 700); |
74 |
|
|
set_position(Gtk::WIN_POS_MOUSE); |
75 |
|
|
} |
76 |
|
|
|
77 |
schoenebeck |
2928 |
// depending on GTK version and installed themes, there may be different |
78 |
|
|
// icons, and different names for them, so for each type of icon we use, |
79 |
|
|
// we provide a list of possible icon names, the first one found to be |
80 |
|
|
// installed on the local system from the list will be used and loaded for |
81 |
|
|
// the respective purpose (so order matters in those lists) |
82 |
|
|
// |
83 |
|
|
// (see https://developer.gnome.org/gtkmm/stable/namespaceGtk_1_1Stock.html for |
84 |
|
|
// available icon names) |
85 |
|
|
std::vector<std::string> errorIconNames; |
86 |
|
|
errorIconNames.push_back("dialog-error"); |
87 |
|
|
errorIconNames.push_back("media-record"); |
88 |
|
|
errorIconNames.push_back("process-stop"); |
89 |
schoenebeck |
2899 |
|
90 |
schoenebeck |
2928 |
std::vector<std::string> warningIconNames; |
91 |
|
|
warningIconNames.push_back("dialog-warning-symbolic"); |
92 |
|
|
warningIconNames.push_back("dialog-warning"); |
93 |
|
|
|
94 |
|
|
std::vector<std::string> successIconNames; |
95 |
|
|
successIconNames.push_back("emblem-default"); |
96 |
|
|
successIconNames.push_back("tools-check-spelling"); |
97 |
|
|
|
98 |
|
|
m_errorIcon = createIcon(errorIconNames, get_screen()); |
99 |
|
|
m_warningIcon = createIcon(warningIconNames, get_screen()); |
100 |
|
|
m_successIcon = createIcon(successIconNames, get_screen()); |
101 |
|
|
|
102 |
schoenebeck |
2604 |
add(m_vbox); |
103 |
|
|
|
104 |
|
|
m_tagTable = Gtk::TextBuffer::TagTable::create(); |
105 |
schoenebeck |
2886 |
|
106 |
schoenebeck |
2604 |
m_keywordTag = Gtk::TextBuffer::Tag::create(); |
107 |
schoenebeck |
2887 |
m_keywordTag->property_foreground() = "#000000"; // black |
108 |
schoenebeck |
2604 |
m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD; |
109 |
|
|
m_tagTable->add(m_keywordTag); |
110 |
schoenebeck |
2886 |
|
111 |
schoenebeck |
2604 |
m_eventTag = Gtk::TextBuffer::Tag::create(); |
112 |
schoenebeck |
2887 |
m_eventTag->property_foreground() = "#07c0cf"; // cyan 1 |
113 |
schoenebeck |
2610 |
m_eventTag->property_weight() = PANGO_WEIGHT_BOLD; |
114 |
schoenebeck |
2604 |
m_tagTable->add(m_eventTag); |
115 |
schoenebeck |
2886 |
|
116 |
|
|
m_variableTag = Gtk::TextBuffer::Tag::create(); |
117 |
schoenebeck |
2887 |
m_variableTag->property_foreground() = "#790cc4"; // magenta |
118 |
schoenebeck |
2886 |
m_tagTable->add(m_variableTag); |
119 |
|
|
|
120 |
|
|
m_functionTag = Gtk::TextBuffer::Tag::create(); |
121 |
schoenebeck |
2887 |
m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2 |
122 |
schoenebeck |
2886 |
m_tagTable->add(m_functionTag); |
123 |
|
|
|
124 |
|
|
m_numberTag = Gtk::TextBuffer::Tag::create(); |
125 |
schoenebeck |
2887 |
m_numberTag->property_foreground() = "#c4950c"; // yellow |
126 |
schoenebeck |
2886 |
m_tagTable->add(m_numberTag); |
127 |
|
|
|
128 |
|
|
m_stringTag = Gtk::TextBuffer::Tag::create(); |
129 |
schoenebeck |
2887 |
m_stringTag->property_foreground() = "#c40c0c"; // red |
130 |
schoenebeck |
2886 |
m_tagTable->add(m_stringTag); |
131 |
|
|
|
132 |
|
|
m_commentTag = Gtk::TextBuffer::Tag::create(); |
133 |
schoenebeck |
2887 |
m_commentTag->property_foreground() = "#9c9c9c"; // gray |
134 |
schoenebeck |
2886 |
m_tagTable->add(m_commentTag); |
135 |
|
|
|
136 |
schoenebeck |
3286 |
#define PREPROC_TOKEN_COLOR "#2f8a33" // green |
137 |
|
|
|
138 |
schoenebeck |
2886 |
m_preprocTag = Gtk::TextBuffer::Tag::create(); |
139 |
schoenebeck |
3286 |
m_preprocTag->property_foreground() = PREPROC_TOKEN_COLOR; |
140 |
schoenebeck |
2886 |
m_tagTable->add(m_preprocTag); |
141 |
|
|
|
142 |
schoenebeck |
3286 |
m_preprocCommentTag = Gtk::TextBuffer::Tag::create(); |
143 |
|
|
m_preprocCommentTag->property_strikethrough() = true; |
144 |
|
|
m_preprocCommentTag->property_background() = "#e5e5e5"; |
145 |
|
|
m_tagTable->add(m_preprocCommentTag); |
146 |
|
|
|
147 |
schoenebeck |
2890 |
m_errorTag = Gtk::TextBuffer::Tag::create(); |
148 |
|
|
m_errorTag->property_background() = "#ff9393"; // red |
149 |
|
|
m_tagTable->add(m_errorTag); |
150 |
|
|
|
151 |
|
|
m_warningTag = Gtk::TextBuffer::Tag::create(); |
152 |
|
|
m_warningTag->property_background() = "#fffd7c"; // yellow |
153 |
|
|
m_tagTable->add(m_warningTag); |
154 |
|
|
|
155 |
schoenebeck |
3310 |
m_lineNrTag = Gtk::TextBuffer::Tag::create(); |
156 |
|
|
m_lineNrTag->property_foreground() = "#CCCCCC"; |
157 |
|
|
m_tagTable->add(m_lineNrTag); |
158 |
|
|
|
159 |
schoenebeck |
2901 |
// create menu |
160 |
|
|
m_actionGroup = Gtk::ActionGroup::create(); |
161 |
|
|
m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script"))); |
162 |
|
|
m_actionGroup->add(Gtk::Action::create("Apply", _("_Apply")), |
163 |
|
|
Gtk::AccelKey("<control>s"), |
164 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onButtonApply)); |
165 |
|
|
m_actionGroup->add(Gtk::Action::create("Close", _("_Close")), |
166 |
schoenebeck |
2903 |
Gtk::AccelKey("<control>q"), |
167 |
schoenebeck |
2901 |
sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)); |
168 |
schoenebeck |
2956 |
m_actionGroup->add(Gtk::Action::create("MenuEditor", _("_Editor"))); |
169 |
|
|
m_actionGroup->add(Gtk::Action::create("ChangeFont", _("_Font Size ...")), |
170 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onMenuChangeFontSize)); |
171 |
schoenebeck |
2901 |
m_uiManager = Gtk::UIManager::create(); |
172 |
|
|
m_uiManager->insert_action_group(m_actionGroup); |
173 |
|
|
add_accel_group(m_uiManager->get_accel_group()); |
174 |
|
|
m_uiManager->add_ui_from_string( |
175 |
|
|
"<ui>" |
176 |
|
|
" <menubar name='MenuBar'>" |
177 |
|
|
" <menu action='MenuScript'>" |
178 |
|
|
" <menuitem action='Apply'/>" |
179 |
|
|
" <separator/>" |
180 |
|
|
" <menuitem action='Close'/>" |
181 |
|
|
" </menu>" |
182 |
schoenebeck |
2956 |
" <menu action='MenuEditor'>" |
183 |
|
|
" <menuitem action='ChangeFont'/>" |
184 |
|
|
" </menu>" |
185 |
schoenebeck |
2901 |
" </menubar>" |
186 |
|
|
"</ui>" |
187 |
|
|
); |
188 |
|
|
|
189 |
schoenebeck |
3310 |
m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable); |
190 |
|
|
m_lineNrView.set_buffer(m_lineNrBuffer); |
191 |
|
|
m_lineNrView.set_left_margin(3); |
192 |
|
|
m_lineNrView.set_right_margin(3); |
193 |
|
|
m_lineNrTextViewSpacer.set_size_request(5); |
194 |
|
|
{ |
195 |
|
|
Gdk::Color color; |
196 |
|
|
color.set("#F5F5F5"); |
197 |
|
|
GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj(); |
198 |
|
|
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); |
199 |
|
|
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); |
200 |
|
|
} |
201 |
|
|
{ |
202 |
|
|
Gdk::Color color; |
203 |
|
|
color.set("#EEEEEE"); |
204 |
|
|
GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj(); |
205 |
|
|
gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj()); |
206 |
|
|
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj()); |
207 |
|
|
} |
208 |
schoenebeck |
2604 |
m_textBuffer = Gtk::TextBuffer::create(m_tagTable); |
209 |
|
|
m_textView.set_buffer(m_textBuffer); |
210 |
schoenebeck |
3310 |
m_textView.set_left_margin(5); |
211 |
schoenebeck |
2956 |
setFontSize(currentFontSize(), false); |
212 |
schoenebeck |
3310 |
m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK); |
213 |
|
|
m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK); |
214 |
|
|
m_textViewHBox.add(m_textView); |
215 |
|
|
m_scrolledWindow.add(m_textViewHBox); |
216 |
schoenebeck |
2604 |
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
217 |
schoenebeck |
2901 |
|
218 |
|
|
Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar"); |
219 |
|
|
m_vbox.pack_start(*menuBar, Gtk::PACK_SHRINK); |
220 |
schoenebeck |
2604 |
m_vbox.pack_start(m_scrolledWindow); |
221 |
|
|
|
222 |
|
|
m_buttonBox.set_layout(Gtk::BUTTONBOX_END); |
223 |
|
|
m_buttonBox.pack_start(m_applyButton); |
224 |
|
|
m_buttonBox.pack_start(m_cancelButton); |
225 |
|
|
m_applyButton.set_can_default(); |
226 |
|
|
m_applyButton.set_sensitive(false); |
227 |
|
|
m_applyButton.grab_focus(); |
228 |
schoenebeck |
2901 |
|
229 |
schoenebeck |
2900 |
#if GTKMM_MAJOR_VERSION >= 3 |
230 |
schoenebeck |
2899 |
m_statusImage.set_margin_left(6); |
231 |
|
|
m_statusImage.set_margin_right(6); |
232 |
schoenebeck |
2900 |
#else |
233 |
|
|
m_statusHBox.set_spacing(6); |
234 |
|
|
#endif |
235 |
schoenebeck |
2604 |
|
236 |
schoenebeck |
2899 |
m_statusHBox.pack_start(m_statusImage, Gtk::PACK_SHRINK); |
237 |
|
|
m_statusHBox.pack_start(m_statusLabel); |
238 |
|
|
m_statusHBox.show_all_children(); |
239 |
|
|
|
240 |
|
|
m_footerHBox.pack_start(m_statusHBox); |
241 |
|
|
m_footerHBox.pack_start(m_buttonBox, Gtk::PACK_SHRINK); |
242 |
|
|
|
243 |
|
|
m_vbox.pack_start(m_footerHBox, Gtk::PACK_SHRINK); |
244 |
|
|
|
245 |
schoenebeck |
2604 |
m_applyButton.signal_clicked().connect( |
246 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onButtonApply) |
247 |
|
|
); |
248 |
|
|
|
249 |
|
|
m_cancelButton.signal_clicked().connect( |
250 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onButtonCancel) |
251 |
|
|
); |
252 |
|
|
|
253 |
|
|
m_textBuffer->signal_insert().connect( |
254 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onTextInserted) |
255 |
|
|
); |
256 |
|
|
|
257 |
|
|
m_textBuffer->signal_erase().connect( |
258 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onTextErased) |
259 |
|
|
); |
260 |
|
|
|
261 |
|
|
m_textBuffer->signal_modified_changed().connect( |
262 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged) |
263 |
|
|
); |
264 |
|
|
|
265 |
|
|
signal_hide().connect( |
266 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onWindowHide) |
267 |
|
|
); |
268 |
|
|
|
269 |
schoenebeck |
2898 |
signal_delete_event().connect( |
270 |
|
|
sigc::mem_fun(*this, &ScriptEditor::onWindowDelete) |
271 |
|
|
); |
272 |
|
|
|
273 |
schoenebeck |
2604 |
show_all_children(); |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
ScriptEditor::~ScriptEditor() { |
277 |
|
|
printf("ScriptEditor destruct\n"); |
278 |
schoenebeck |
2896 |
#if USE_LS_SCRIPTVM |
279 |
schoenebeck |
2886 |
if (m_vm) delete m_vm; |
280 |
schoenebeck |
2896 |
#endif |
281 |
schoenebeck |
2604 |
} |
282 |
|
|
|
283 |
schoenebeck |
2956 |
int ScriptEditor::currentFontSize() const { |
284 |
|
|
#if defined(__APPLE__) |
285 |
schoenebeck |
2964 |
const int defaultFontSize = 13; |
286 |
schoenebeck |
2956 |
#else |
287 |
|
|
const int defaultFontSize = 10; |
288 |
|
|
#endif |
289 |
|
|
const int settingFontSize = Settings::singleton()->scriptEditorFontSize; |
290 |
|
|
const int fontSize = (settingFontSize > 0) ? settingFontSize : defaultFontSize; |
291 |
|
|
return fontSize; |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
void ScriptEditor::setFontSize(int size, bool save) { |
295 |
|
|
//printf("setFontSize(%d,%d)\n", size, save); |
296 |
|
|
Pango::FontDescription fdesc; |
297 |
|
|
fdesc.set_family("monospace"); |
298 |
|
|
fdesc.set_size(size * PANGO_SCALE); |
299 |
|
|
#if GTKMM_MAJOR_VERSION < 3 |
300 |
schoenebeck |
3310 |
m_lineNrView.modify_font(fdesc); |
301 |
schoenebeck |
2956 |
m_textView.modify_font(fdesc); |
302 |
|
|
#else |
303 |
schoenebeck |
3310 |
m_lineNrView.override_font(fdesc); |
304 |
schoenebeck |
2956 |
m_textView.override_font(fdesc); |
305 |
|
|
#endif |
306 |
|
|
if (save) Settings::singleton()->scriptEditorFontSize = size; |
307 |
|
|
} |
308 |
|
|
|
309 |
schoenebeck |
2604 |
void ScriptEditor::setScript(gig::Script* script) { |
310 |
|
|
m_script = script; |
311 |
|
|
if (!script) { |
312 |
|
|
set_title(_("No Script")); |
313 |
|
|
return; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\""); |
317 |
|
|
|
318 |
|
|
std::string txt = script->GetScriptAsText(); |
319 |
|
|
//printf("text : '%s'\n", txt.c_str()); |
320 |
|
|
m_textBuffer->set_text(txt); |
321 |
|
|
m_textBuffer->set_modified(false); |
322 |
|
|
} |
323 |
|
|
|
324 |
schoenebeck |
3310 |
void ScriptEditor::updateLineNumbers() { |
325 |
|
|
const int n = m_textBuffer->get_line_count(); |
326 |
|
|
const int old = m_lineNrBuffer->get_line_count(); |
327 |
|
|
if (n == old) return; |
328 |
|
|
Glib::ustring s; |
329 |
|
|
for (int i = 0; i < n; ++i) { |
330 |
|
|
if (i) s += "\n"; |
331 |
|
|
s += ToString(i+1); |
332 |
|
|
} |
333 |
|
|
m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end()); |
334 |
|
|
m_lineNrBuffer->set_text(s); |
335 |
|
|
m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end()); |
336 |
|
|
} |
337 |
|
|
|
338 |
schoenebeck |
2604 |
void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) { |
339 |
schoenebeck |
2897 |
//printf("onTextInserted()\n"); |
340 |
schoenebeck |
2886 |
#if USE_LS_SCRIPTVM |
341 |
schoenebeck |
2890 |
m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end()); |
342 |
schoenebeck |
2886 |
updateSyntaxHighlightingByVM(); |
343 |
schoenebeck |
2890 |
updateParserIssuesByVM(); |
344 |
schoenebeck |
2899 |
updateStatusBar(); |
345 |
schoenebeck |
2886 |
#else |
346 |
schoenebeck |
2610 |
//printf("inserted %d\n", length); |
347 |
schoenebeck |
2604 |
Gtk::TextBuffer::iterator itStart = itEnd; |
348 |
|
|
itStart.backward_chars(length); |
349 |
|
|
|
350 |
|
|
Gtk::TextBuffer::iterator it = itStart; |
351 |
|
|
it.backward_word_start(); |
352 |
|
|
|
353 |
|
|
bool eofReached = false; |
354 |
|
|
while (it <= itEnd) { |
355 |
|
|
Gtk::TextBuffer::iterator itWordStart = it; |
356 |
|
|
if (!it.forward_word_end()) { |
357 |
|
|
eofReached = true; |
358 |
|
|
it = itEnd; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false); |
362 |
schoenebeck |
2610 |
//printf("{%s}\n", s.c_str()); |
363 |
schoenebeck |
2604 |
if (isKeyword(s)) |
364 |
|
|
m_textBuffer->apply_tag(m_keywordTag, itWordStart, it); |
365 |
|
|
else if (isEvent(s)) { |
366 |
|
|
// check if previous word is "on" |
367 |
|
|
Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart; |
368 |
|
|
if (itPreviousWordStart.backward_word_start()) { |
369 |
|
|
Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart; |
370 |
|
|
itPreviousWordEnd.forward_word_end(); |
371 |
|
|
if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") { |
372 |
|
|
m_textBuffer->apply_tag(m_eventTag, itWordStart, it); |
373 |
|
|
} |
374 |
|
|
} |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
if (eofReached) break; |
378 |
|
|
|
379 |
|
|
while (!it.inside_word()) |
380 |
|
|
if (!it.forward_char()) |
381 |
|
|
goto EOF_REACHED; |
382 |
|
|
} |
383 |
|
|
|
384 |
|
|
EOF_REACHED: |
385 |
|
|
; |
386 |
schoenebeck |
2886 |
|
387 |
|
|
#endif // USE_LS_SCRIPTVM |
388 |
schoenebeck |
3310 |
updateLineNumbers(); |
389 |
schoenebeck |
2604 |
} |
390 |
|
|
|
391 |
schoenebeck |
2886 |
#if USE_LS_SCRIPTVM |
392 |
|
|
|
393 |
schoenebeck |
2890 |
LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() { |
394 |
|
|
if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig"); |
395 |
|
|
return m_vm; |
396 |
|
|
} |
397 |
|
|
|
398 |
schoenebeck |
3286 |
template<class T> |
399 |
|
|
static void getIteratorsForIssue(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const T& issue, Gtk::TextBuffer::iterator& start, Gtk::TextBuffer::iterator& end) { |
400 |
schoenebeck |
3206 |
Gtk::TextBuffer::iterator itLine = |
401 |
|
|
txtbuf->get_iter_at_line_index(issue.firstLine - 1, 0); |
402 |
|
|
const int charsInLine = itLine.get_bytes_in_line(); |
403 |
|
|
start = txtbuf->get_iter_at_line_index( |
404 |
|
|
issue.firstLine - 1, |
405 |
|
|
// check we are not getting past the end of the line here, otherwise Gtk crashes |
406 |
|
|
issue.firstColumn - 1 < charsInLine ? issue.firstColumn - 1 : charsInLine - 1 |
407 |
|
|
); |
408 |
schoenebeck |
2897 |
end = start; |
409 |
|
|
end.forward_lines(issue.lastLine - issue.firstLine); |
410 |
|
|
end.forward_chars( |
411 |
|
|
(issue.lastLine != issue.firstLine) |
412 |
|
|
? issue.lastColumn - 1 |
413 |
|
|
: issue.lastColumn - issue.firstColumn + 1 |
414 |
|
|
); |
415 |
|
|
} |
416 |
|
|
|
417 |
schoenebeck |
2886 |
static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) { |
418 |
schoenebeck |
3206 |
Gtk::TextBuffer::iterator itLine = |
419 |
|
|
txtbuf->get_iter_at_line_index(token.firstLine(), 0); |
420 |
|
|
const int charsInLine = itLine.get_bytes_in_line(); |
421 |
|
|
Gtk::TextBuffer::iterator itStart = txtbuf->get_iter_at_line_index( |
422 |
|
|
token.firstLine(), |
423 |
|
|
// check we are not getting past the end of the line here, otherwise Gtk crashes |
424 |
|
|
token.firstColumn() < charsInLine ? token.firstColumn() : charsInLine - 1 |
425 |
|
|
); |
426 |
schoenebeck |
2886 |
Gtk::TextBuffer::iterator itEnd = itStart; |
427 |
|
|
const int length = token.text().length(); |
428 |
|
|
itEnd.forward_chars(length); |
429 |
|
|
txtbuf->apply_tag(tag, itStart, itEnd); |
430 |
|
|
} |
431 |
|
|
|
432 |
schoenebeck |
2890 |
static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) { |
433 |
schoenebeck |
2897 |
Gtk::TextBuffer::iterator itStart, itEnd; |
434 |
|
|
getIteratorsForIssue(txtbuf, issue, itStart, itEnd); |
435 |
schoenebeck |
2890 |
txtbuf->apply_tag(tag, itStart, itEnd); |
436 |
|
|
} |
437 |
|
|
|
438 |
schoenebeck |
3286 |
static void applyPreprocessorComment(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::CodeBlock& block, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) { |
439 |
|
|
Gtk::TextBuffer::iterator itStart, itEnd; |
440 |
|
|
getIteratorsForIssue(txtbuf, block, itStart, itEnd); |
441 |
|
|
txtbuf->apply_tag(tag, itStart, itEnd); |
442 |
|
|
} |
443 |
|
|
|
444 |
schoenebeck |
2886 |
void ScriptEditor::updateSyntaxHighlightingByVM() { |
445 |
schoenebeck |
2890 |
GetScriptVM(); |
446 |
schoenebeck |
2886 |
const std::string s = m_textBuffer->get_text(); |
447 |
schoenebeck |
2975 |
if (s.empty()) return; |
448 |
schoenebeck |
2886 |
std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s); |
449 |
|
|
|
450 |
|
|
for (int i = 0; i < tokens.size(); ++i) { |
451 |
|
|
const LinuxSampler::VMSourceToken& token = tokens[i]; |
452 |
|
|
|
453 |
|
|
if (token.isKeyword()) { |
454 |
|
|
applyCodeTag(m_textBuffer, token, m_keywordTag); |
455 |
|
|
} else if (token.isVariableName()) { |
456 |
|
|
applyCodeTag(m_textBuffer, token, m_variableTag); |
457 |
|
|
} else if (token.isIdentifier()) { |
458 |
|
|
if (token.isEventHandlerName()) { |
459 |
|
|
applyCodeTag(m_textBuffer, token, m_eventTag); |
460 |
|
|
} else { // a function ... |
461 |
|
|
applyCodeTag(m_textBuffer, token, m_functionTag); |
462 |
|
|
} |
463 |
|
|
} else if (token.isNumberLiteral()) { |
464 |
|
|
applyCodeTag(m_textBuffer, token, m_numberTag); |
465 |
|
|
} else if (token.isStringLiteral()) { |
466 |
|
|
applyCodeTag(m_textBuffer, token, m_stringTag); |
467 |
|
|
} else if (token.isComment()) { |
468 |
|
|
applyCodeTag(m_textBuffer, token, m_commentTag); |
469 |
|
|
} else if (token.isPreprocessor()) { |
470 |
|
|
applyCodeTag(m_textBuffer, token, m_preprocTag); |
471 |
|
|
} else if (token.isNewLine()) { |
472 |
|
|
} |
473 |
|
|
} |
474 |
|
|
} |
475 |
|
|
|
476 |
schoenebeck |
2890 |
void ScriptEditor::updateParserIssuesByVM() { |
477 |
|
|
GetScriptVM(); |
478 |
|
|
const std::string s = m_textBuffer->get_text(); |
479 |
|
|
LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s); |
480 |
schoenebeck |
2896 |
m_issues = parserContext->issues(); |
481 |
schoenebeck |
2899 |
m_errors = parserContext->errors(); |
482 |
|
|
m_warnings = parserContext->warnings(); |
483 |
schoenebeck |
3286 |
m_preprocComments = parserContext->preprocessorComments(); |
484 |
schoenebeck |
2890 |
|
485 |
schoenebeck |
2975 |
if (!s.empty()) { |
486 |
|
|
for (int i = 0; i < m_issues.size(); ++i) { |
487 |
|
|
const LinuxSampler::ParserIssue& issue = m_issues[i]; |
488 |
schoenebeck |
2890 |
|
489 |
schoenebeck |
2975 |
if (issue.isErr()) { |
490 |
|
|
applyCodeTag(m_textBuffer, issue, m_errorTag); |
491 |
|
|
} else if (issue.isWrn()) { |
492 |
|
|
applyCodeTag(m_textBuffer, issue, m_warningTag); |
493 |
|
|
} |
494 |
schoenebeck |
2890 |
} |
495 |
|
|
} |
496 |
|
|
|
497 |
schoenebeck |
3286 |
for (int i = 0; i < m_preprocComments.size(); ++i) { |
498 |
|
|
applyPreprocessorComment(m_textBuffer, m_preprocComments[i], |
499 |
|
|
m_preprocCommentTag); |
500 |
|
|
} |
501 |
|
|
|
502 |
schoenebeck |
2897 |
delete parserContext; |
503 |
|
|
} |
504 |
schoenebeck |
2896 |
|
505 |
schoenebeck |
2897 |
void ScriptEditor::updateIssueTooltip(GdkEventMotion* e) { |
506 |
|
|
int x, y; |
507 |
|
|
m_textView.window_to_buffer_coords(Gtk::TEXT_WINDOW_TEXT, int(e->x), int(e->y), x, y); |
508 |
schoenebeck |
2896 |
|
509 |
schoenebeck |
2897 |
Gtk::TextBuffer::iterator it; |
510 |
|
|
m_textView.get_iter_at_location(it, x, y); |
511 |
|
|
|
512 |
|
|
const int line = it.get_line(); |
513 |
|
|
const int column = it.get_line_offset(); |
514 |
|
|
|
515 |
|
|
//printf("mouse at l%d c%d\n", line, column); |
516 |
|
|
|
517 |
|
|
for (int i = 0; i < m_issues.size(); ++i) { |
518 |
|
|
const LinuxSampler::ParserIssue& issue = m_issues[i]; |
519 |
|
|
const int firstLine = issue.firstLine - 1; |
520 |
|
|
const int firstColumn = issue.firstColumn - 1; |
521 |
|
|
const int lastLine = issue.lastLine - 1; |
522 |
|
|
const int lastColumn = issue.lastColumn - 1; |
523 |
|
|
if (firstLine <= line && line <= lastLine && |
524 |
|
|
(firstLine != line || firstColumn <= column) && |
525 |
|
|
(lastLine != line || lastColumn >= column)) |
526 |
|
|
{ |
527 |
|
|
m_textView.set_tooltip_markup( |
528 |
|
|
(issue.isErr() ? "<span foreground=\"#ff9393\">ERROR:</span> " : "<span foreground=\"#c4950c\">Warning:</span> ") + |
529 |
|
|
issue.txt |
530 |
|
|
); |
531 |
|
|
return; |
532 |
schoenebeck |
2896 |
} |
533 |
|
|
} |
534 |
|
|
|
535 |
schoenebeck |
3286 |
for (int i = 0; i < m_preprocComments.size(); ++i) { |
536 |
|
|
const LinuxSampler::CodeBlock& block = m_preprocComments[i]; |
537 |
|
|
const int firstLine = block.firstLine - 1; |
538 |
|
|
const int firstColumn = block.firstColumn - 1; |
539 |
|
|
const int lastLine = block.lastLine - 1; |
540 |
|
|
const int lastColumn = block.lastColumn - 1; |
541 |
|
|
if (firstLine <= line && line <= lastLine && |
542 |
|
|
(firstLine != line || firstColumn <= column) && |
543 |
|
|
(lastLine != line || lastColumn >= column)) |
544 |
|
|
{ |
545 |
|
|
m_textView.set_tooltip_markup( |
546 |
|
|
"Code block filtered out by preceding <span foreground=\"" PREPROC_TOKEN_COLOR "\">preprocessor</span> statement." |
547 |
|
|
); |
548 |
|
|
return; |
549 |
|
|
} |
550 |
|
|
} |
551 |
|
|
|
552 |
schoenebeck |
2897 |
m_textView.set_tooltip_markup(""); |
553 |
schoenebeck |
2890 |
} |
554 |
|
|
|
555 |
schoenebeck |
2899 |
static std::string warningsCountTxt(const std::vector<LinuxSampler::ParserIssue> warnings) { |
556 |
|
|
std::string txt = "<span foreground=\"#c4950c\">" + ToString(warnings.size()); |
557 |
|
|
txt += (warnings.size() == 1) ? " Warning" : " Warnings"; |
558 |
|
|
txt += "</span>"; |
559 |
|
|
return txt; |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
static std::string errorsCountTxt(const std::vector<LinuxSampler::ParserIssue> errors) { |
563 |
|
|
std::string txt = "<span foreground=\"#c40c0c\">" + ToString(errors.size()); |
564 |
|
|
txt += (errors.size() == 1) ? " Error" : " Errors"; |
565 |
|
|
txt += "</span>"; |
566 |
|
|
return txt; |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
void ScriptEditor::updateStatusBar() { |
570 |
|
|
// update status text |
571 |
|
|
std::string txt; |
572 |
|
|
if (m_issues.empty()) { |
573 |
|
|
txt = "No issues with this script."; |
574 |
|
|
} else { |
575 |
|
|
const char* txtWontLoad = ". Sampler won't load instruments using this script!"; |
576 |
|
|
txt = "There "; |
577 |
|
|
txt += (m_errors.size() <= 1 && m_warnings.size() <= 1) ? "is " : "are "; |
578 |
|
|
if (m_errors.empty()) { |
579 |
|
|
txt += warningsCountTxt(m_warnings) + ". Script will load, but might not behave as expected!"; |
580 |
|
|
} else if (m_warnings.empty()) { |
581 |
|
|
txt += errorsCountTxt(m_errors) + txtWontLoad; |
582 |
|
|
} else { |
583 |
|
|
txt += errorsCountTxt(m_errors) + " and " + |
584 |
|
|
warningsCountTxt(m_warnings) + txtWontLoad; |
585 |
|
|
} |
586 |
|
|
} |
587 |
|
|
m_statusLabel.set_markup(txt); |
588 |
|
|
|
589 |
|
|
// update status icon |
590 |
|
|
m_statusImage.set( |
591 |
|
|
m_issues.empty() ? m_successIcon : !m_errors.empty() ? m_errorIcon : m_warningIcon |
592 |
|
|
); |
593 |
|
|
} |
594 |
|
|
|
595 |
schoenebeck |
2886 |
#endif // USE_LS_SCRIPTVM |
596 |
|
|
|
597 |
schoenebeck |
2604 |
void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) { |
598 |
schoenebeck |
2610 |
//printf("erased\n"); |
599 |
schoenebeck |
2886 |
#if USE_LS_SCRIPTVM |
600 |
schoenebeck |
2890 |
m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end()); |
601 |
schoenebeck |
2886 |
updateSyntaxHighlightingByVM(); |
602 |
schoenebeck |
2890 |
updateParserIssuesByVM(); |
603 |
schoenebeck |
2899 |
updateStatusBar(); |
604 |
schoenebeck |
2886 |
#else |
605 |
schoenebeck |
2604 |
Gtk::TextBuffer::iterator itStart2 = itStart; |
606 |
|
|
if (itStart2.inside_word() || itStart2.ends_word()) |
607 |
|
|
itStart2.backward_word_start(); |
608 |
|
|
|
609 |
|
|
Gtk::TextBuffer::iterator itEnd2 = itEnd; |
610 |
|
|
if (itEnd2.inside_word()) itEnd2.forward_word_end(); |
611 |
|
|
|
612 |
|
|
m_textBuffer->remove_all_tags(itStart2, itEnd2); |
613 |
schoenebeck |
2886 |
#endif // USE_LS_SCRIPTVM |
614 |
schoenebeck |
3310 |
updateLineNumbers(); |
615 |
schoenebeck |
2604 |
} |
616 |
|
|
|
617 |
schoenebeck |
2897 |
bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) { |
618 |
|
|
#if USE_LS_SCRIPTVM |
619 |
|
|
//TODO: event throttling would be a good idea here |
620 |
|
|
updateIssueTooltip(e); |
621 |
|
|
#endif |
622 |
|
|
return ManagedWindow::on_motion_notify_event(e); |
623 |
|
|
} |
624 |
|
|
|
625 |
schoenebeck |
2956 |
void ScriptEditor::onMenuChangeFontSize() { |
626 |
|
|
//TODO: for GTKMM >= 3.2 class Gtk::FontChooser could be used instead |
627 |
|
|
Gtk::Dialog dialog(_("Font Size"), true /*modal*/); |
628 |
|
|
Gtk::HBox hbox; |
629 |
|
|
hbox.set_spacing(6); |
630 |
|
|
|
631 |
|
|
Gtk::Label label(_("Editor's Font Size:"), Gtk::ALIGN_START); |
632 |
|
|
hbox.pack_start(label, Gtk::PACK_SHRINK); |
633 |
|
|
|
634 |
|
|
Gtk::SpinButton spinButton; |
635 |
|
|
spinButton.set_range(4, 80); |
636 |
|
|
spinButton.set_increments(1, 10); |
637 |
|
|
spinButton.set_value(currentFontSize()); |
638 |
|
|
hbox.pack_start(spinButton); |
639 |
|
|
|
640 |
|
|
dialog.get_vbox()->pack_start(hbox); |
641 |
|
|
dialog.add_button(_("_OK"), 0); |
642 |
|
|
dialog.add_button(_("_Cancel"), 1); |
643 |
|
|
|
644 |
|
|
dialog.show_all_children(); |
645 |
|
|
|
646 |
|
|
if (!dialog.run()) { // OK selected ... |
647 |
|
|
const int newFontSize = spinButton.get_value_as_int(); |
648 |
|
|
if (newFontSize >= 4) |
649 |
|
|
setFontSize(newFontSize, true); |
650 |
|
|
} |
651 |
|
|
} |
652 |
|
|
|
653 |
schoenebeck |
2898 |
bool ScriptEditor::onWindowDelete(GdkEventAny* e) { |
654 |
|
|
//printf("onWindowDelete\n"); |
655 |
|
|
|
656 |
|
|
if (!isModified()) return false; // propagate event further (which will close this window) |
657 |
|
|
|
658 |
|
|
gchar* msg = g_strdup_printf(_("Apply changes to instrument script \"%s\" before closing?"), |
659 |
|
|
m_script->Name.c_str()); |
660 |
|
|
Gtk::MessageDialog dialog(*this, msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE); |
661 |
|
|
g_free(msg); |
662 |
|
|
dialog.set_secondary_text(_("If you close without applying, your changes will be lost.")); |
663 |
|
|
dialog.add_button(_("Close _Without Applying"), Gtk::RESPONSE_NO); |
664 |
|
|
dialog.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL); |
665 |
|
|
dialog.add_button(_("_Apply"), Gtk::RESPONSE_YES); |
666 |
|
|
dialog.set_default_response(Gtk::RESPONSE_YES); |
667 |
|
|
int response = dialog.run(); |
668 |
|
|
dialog.hide(); |
669 |
|
|
|
670 |
|
|
// user decided to close script editor without saving |
671 |
|
|
if (response == Gtk::RESPONSE_NO) |
672 |
|
|
return false; // propagate event further (which will close this window) |
673 |
|
|
|
674 |
|
|
// user cancelled dialog, thus don't close script editor |
675 |
|
|
if (response == Gtk::RESPONSE_CANCEL) { |
676 |
|
|
show(); |
677 |
|
|
return true; // drop event (prevents closing this window) |
678 |
|
|
} |
679 |
|
|
|
680 |
|
|
// user wants to apply the changes, afterwards close window |
681 |
|
|
if (response == Gtk::RESPONSE_YES) { |
682 |
|
|
onButtonApply(); |
683 |
|
|
return false; // propagate event further (which will close this window) |
684 |
|
|
} |
685 |
|
|
|
686 |
|
|
// should never ever make it to this point actually |
687 |
|
|
return false; |
688 |
|
|
} |
689 |
|
|
|
690 |
|
|
bool ScriptEditor::isModified() const { |
691 |
|
|
return m_textBuffer->get_modified(); |
692 |
|
|
} |
693 |
|
|
|
694 |
schoenebeck |
2604 |
void ScriptEditor::onModifiedChanged() { |
695 |
schoenebeck |
2898 |
m_applyButton.set_sensitive(isModified()); |
696 |
schoenebeck |
2899 |
#if USE_LS_SCRIPTVM |
697 |
|
|
updateStatusBar(); |
698 |
|
|
#endif |
699 |
schoenebeck |
2604 |
} |
700 |
|
|
|
701 |
|
|
void ScriptEditor::onButtonCancel() { |
702 |
schoenebeck |
2898 |
bool dropEvent = onWindowDelete(NULL); |
703 |
|
|
if (dropEvent) return; |
704 |
schoenebeck |
2604 |
hide(); |
705 |
|
|
} |
706 |
|
|
|
707 |
|
|
void ScriptEditor::onButtonApply() { |
708 |
schoenebeck |
2903 |
signal_script_to_be_changed.emit(m_script); |
709 |
schoenebeck |
2604 |
m_script->SetScriptAsText(m_textBuffer->get_text()); |
710 |
schoenebeck |
2903 |
signal_script_changed.emit(m_script); |
711 |
schoenebeck |
2604 |
m_textBuffer->set_modified(false); |
712 |
|
|
} |
713 |
|
|
|
714 |
|
|
void ScriptEditor::onWindowHide() { |
715 |
|
|
delete this; // this is the end, my friend |
716 |
|
|
} |