/[svn]/gigedit/trunk/src/gigedit/scripteditor.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/scripteditor.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2890 - (show annotations) (download)
Mon Apr 25 17:35:21 2016 UTC (7 years, 11 months ago) by schoenebeck
File size: 10853 byte(s)
* Script Editor: Added support for liblinuxsampler's NKSP parser
  backend for displaying parser errors and warnings directly within
  the source code (for now simply errors cause a red background color
  at the relevant location, and warnings a yellow background color;
  error text / warning text is not displayed yet).
* Bumped version (1.0.0.svn4).

1 /*
2 Copyright (c) 2014-2016 Christian Schoenebeck
3
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 #if USE_LS_SCRIPTVM
11 # include <linuxsampler/scriptvm/ScriptVM.h>
12 # include <linuxsampler/scriptvm/ScriptVMFactory.h>
13 #endif
14
15 #if !USE_LS_SCRIPTVM
16
17 static const std::string _keywords[] = {
18 "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
19 "select", "to", "const", "polyphonic", "mod"
20 };
21 static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
22
23 static const std::string _eventNames[] = {
24 "init", "note", "release", "controller"
25 };
26 static int _eventNamesSz = sizeof(_eventNames) / sizeof(std::string);
27
28 static bool isKeyword(const Glib::ustring& s) {
29 for (int i = 0; i < _keywordsSz; ++i)
30 if (_keywords[i] == s) return true;
31 return false;
32 }
33
34 static bool isEvent(const Glib::ustring& s) {
35 for (int i = 0; i < _eventNamesSz; ++i)
36 if (_eventNames[i] == s) return true;
37 return false;
38 }
39
40 #endif // !USE_LS_SCRIPTVM
41
42 ScriptEditor::ScriptEditor() :
43 m_applyButton(_("_Apply"), true),
44 m_cancelButton(_("_Cancel"), true)
45 {
46 m_script = NULL;
47 m_vm = NULL;
48
49 add(m_vbox);
50
51 m_tagTable = Gtk::TextBuffer::TagTable::create();
52
53 m_keywordTag = Gtk::TextBuffer::Tag::create();
54 m_keywordTag->property_foreground() = "#000000"; // black
55 m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
56 m_tagTable->add(m_keywordTag);
57
58 m_eventTag = Gtk::TextBuffer::Tag::create();
59 m_eventTag->property_foreground() = "#07c0cf"; // cyan 1
60 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
61 m_tagTable->add(m_eventTag);
62
63 m_variableTag = Gtk::TextBuffer::Tag::create();
64 m_variableTag->property_foreground() = "#790cc4"; // magenta
65 m_tagTable->add(m_variableTag);
66
67 m_functionTag = Gtk::TextBuffer::Tag::create();
68 m_functionTag->property_foreground() = "#1ba1dd"; // cyan 2
69 m_tagTable->add(m_functionTag);
70
71 m_numberTag = Gtk::TextBuffer::Tag::create();
72 m_numberTag->property_foreground() = "#c4950c"; // yellow
73 m_tagTable->add(m_numberTag);
74
75 m_stringTag = Gtk::TextBuffer::Tag::create();
76 m_stringTag->property_foreground() = "#c40c0c"; // red
77 m_tagTable->add(m_stringTag);
78
79 m_commentTag = Gtk::TextBuffer::Tag::create();
80 m_commentTag->property_foreground() = "#9c9c9c"; // gray
81 m_tagTable->add(m_commentTag);
82
83 m_preprocTag = Gtk::TextBuffer::Tag::create();
84 m_preprocTag->property_foreground() = "#2f8a33"; // green
85 m_tagTable->add(m_preprocTag);
86
87 m_errorTag = Gtk::TextBuffer::Tag::create();
88 m_errorTag->property_background() = "#ff9393"; // red
89 m_tagTable->add(m_errorTag);
90
91 m_warningTag = Gtk::TextBuffer::Tag::create();
92 m_warningTag->property_background() = "#fffd7c"; // yellow
93 m_tagTable->add(m_warningTag);
94
95 m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
96 m_textView.set_buffer(m_textBuffer);
97 {
98 Pango::FontDescription fdesc;
99 fdesc.set_family("monospace");
100 #if defined(__APPLE__)
101 fdesc.set_size(12 * PANGO_SCALE);
102 #else
103 fdesc.set_size(10 * PANGO_SCALE);
104 #endif
105 #if GTKMM_MAJOR_VERSION < 3
106 m_textView.modify_font(fdesc);
107 #else
108 m_textView.override_font(fdesc);
109 #endif
110 }
111 m_scrolledWindow.add(m_textView);
112 m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
113 m_vbox.pack_start(m_scrolledWindow);
114
115 m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
116 m_buttonBox.pack_start(m_applyButton);
117 m_buttonBox.pack_start(m_cancelButton);
118 m_applyButton.set_can_default();
119 m_applyButton.set_sensitive(false);
120 m_applyButton.grab_focus();
121 m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
122
123 m_applyButton.signal_clicked().connect(
124 sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
125 );
126
127 m_cancelButton.signal_clicked().connect(
128 sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
129 );
130
131 m_textBuffer->signal_insert().connect(
132 sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
133 );
134
135 m_textBuffer->signal_erase().connect(
136 sigc::mem_fun(*this, &ScriptEditor::onTextErased)
137 );
138
139 m_textBuffer->signal_modified_changed().connect(
140 sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
141 );
142
143 signal_hide().connect(
144 sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
145 );
146
147 show_all_children();
148
149 resize(460,300);
150 }
151
152 ScriptEditor::~ScriptEditor() {
153 printf("ScriptEditor destruct\n");
154 if (m_vm) delete m_vm;
155 }
156
157 void ScriptEditor::setScript(gig::Script* script) {
158 m_script = script;
159 if (!script) {
160 set_title(_("No Script"));
161 return;
162 }
163
164 set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
165
166 std::string txt = script->GetScriptAsText();
167 //printf("text : '%s'\n", txt.c_str());
168 m_textBuffer->set_text(txt);
169 m_textBuffer->set_modified(false);
170 }
171
172 void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
173 #if USE_LS_SCRIPTVM
174 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
175 updateSyntaxHighlightingByVM();
176 updateParserIssuesByVM();
177 #else
178 //printf("inserted %d\n", length);
179 Gtk::TextBuffer::iterator itStart = itEnd;
180 itStart.backward_chars(length);
181
182 Gtk::TextBuffer::iterator it = itStart;
183 it.backward_word_start();
184
185 bool eofReached = false;
186 while (it <= itEnd) {
187 Gtk::TextBuffer::iterator itWordStart = it;
188 if (!it.forward_word_end()) {
189 eofReached = true;
190 it = itEnd;
191 }
192
193 Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
194 //printf("{%s}\n", s.c_str());
195 if (isKeyword(s))
196 m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
197 else if (isEvent(s)) {
198 // check if previous word is "on"
199 Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
200 if (itPreviousWordStart.backward_word_start()) {
201 Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
202 itPreviousWordEnd.forward_word_end();
203 if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
204 m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
205 }
206 }
207 }
208
209 if (eofReached) break;
210
211 while (!it.inside_word())
212 if (!it.forward_char())
213 goto EOF_REACHED;
214 }
215
216 EOF_REACHED:
217 ;
218
219 #endif // USE_LS_SCRIPTVM
220 }
221
222 #if USE_LS_SCRIPTVM
223
224 LinuxSampler::ScriptVM* ScriptEditor::GetScriptVM() {
225 if (!m_vm) m_vm = LinuxSampler::ScriptVMFactory::Create("gig");
226 return m_vm;
227 }
228
229 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::VMSourceToken& token, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
230 Gtk::TextBuffer::iterator itStart =
231 txtbuf->get_iter_at_line_index(token.firstLine(), token.firstColumn());
232 Gtk::TextBuffer::iterator itEnd = itStart;
233 const int length = token.text().length();
234 itEnd.forward_chars(length);
235 txtbuf->apply_tag(tag, itStart, itEnd);
236 }
237
238 static void applyCodeTag(Glib::RefPtr<Gtk::TextBuffer>& txtbuf, const LinuxSampler::ParserIssue& issue, Glib::RefPtr<Gtk::TextBuffer::Tag>& tag) {
239 Gtk::TextBuffer::iterator itStart =
240 txtbuf->get_iter_at_line_index(issue.firstLine - 1, issue.firstColumn - 1);
241 Gtk::TextBuffer::iterator itEnd = itStart;
242 itEnd.forward_lines(issue.lastLine - issue.firstLine);
243 itEnd.forward_chars(
244 (issue.lastLine != issue.firstLine)
245 ? issue.lastColumn - 1
246 : issue.lastColumn - issue.firstColumn + 1
247 );
248 txtbuf->apply_tag(tag, itStart, itEnd);
249 }
250
251
252 void ScriptEditor::updateSyntaxHighlightingByVM() {
253 GetScriptVM();
254 const std::string s = m_textBuffer->get_text();
255 std::vector<LinuxSampler::VMSourceToken> tokens = m_vm->syntaxHighlighting(s);
256
257 for (int i = 0; i < tokens.size(); ++i) {
258 const LinuxSampler::VMSourceToken& token = tokens[i];
259
260 if (token.isKeyword()) {
261 applyCodeTag(m_textBuffer, token, m_keywordTag);
262 } else if (token.isVariableName()) {
263 applyCodeTag(m_textBuffer, token, m_variableTag);
264 } else if (token.isIdentifier()) {
265 if (token.isEventHandlerName()) {
266 applyCodeTag(m_textBuffer, token, m_eventTag);
267 } else { // a function ...
268 applyCodeTag(m_textBuffer, token, m_functionTag);
269 }
270 } else if (token.isNumberLiteral()) {
271 applyCodeTag(m_textBuffer, token, m_numberTag);
272 } else if (token.isStringLiteral()) {
273 applyCodeTag(m_textBuffer, token, m_stringTag);
274 } else if (token.isComment()) {
275 applyCodeTag(m_textBuffer, token, m_commentTag);
276 } else if (token.isPreprocessor()) {
277 applyCodeTag(m_textBuffer, token, m_preprocTag);
278 } else if (token.isNewLine()) {
279 }
280 }
281 }
282
283 void ScriptEditor::updateParserIssuesByVM() {
284 GetScriptVM();
285 const std::string s = m_textBuffer->get_text();
286 LinuxSampler::VMParserContext* parserContext = m_vm->loadScript(s);
287 std::vector<LinuxSampler::ParserIssue> issues = parserContext->issues();
288
289 for (int i = 0; i < issues.size(); ++i) {
290 const LinuxSampler::ParserIssue& issue = issues[i];
291
292 if (issue.isErr()) {
293 applyCodeTag(m_textBuffer, issue, m_errorTag);
294 } else if (issue.isWrn()) {
295 applyCodeTag(m_textBuffer, issue, m_warningTag);
296 }
297 }
298
299 delete parserContext;
300 }
301
302 #endif // USE_LS_SCRIPTVM
303
304 void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
305 //printf("erased\n");
306 #if USE_LS_SCRIPTVM
307 m_textBuffer->remove_all_tags(m_textBuffer->begin(), m_textBuffer->end());
308 updateSyntaxHighlightingByVM();
309 updateParserIssuesByVM();
310 #else
311 Gtk::TextBuffer::iterator itStart2 = itStart;
312 if (itStart2.inside_word() || itStart2.ends_word())
313 itStart2.backward_word_start();
314
315 Gtk::TextBuffer::iterator itEnd2 = itEnd;
316 if (itEnd2.inside_word()) itEnd2.forward_word_end();
317
318 m_textBuffer->remove_all_tags(itStart2, itEnd2);
319 #endif // USE_LS_SCRIPTVM
320 }
321
322 void ScriptEditor::onModifiedChanged() {
323 m_applyButton.set_sensitive( m_textBuffer->get_modified() );
324 }
325
326 void ScriptEditor::onButtonCancel() {
327 hide();
328 }
329
330 void ScriptEditor::onButtonApply() {
331 m_script->SetScriptAsText(m_textBuffer->get_text());
332 m_textBuffer->set_modified(false);
333 }
334
335 void ScriptEditor::onWindowHide() {
336 delete this; // this is the end, my friend
337 }

  ViewVC Help
Powered by ViewVC