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