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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2886 - (hide annotations) (download)
Fri Apr 22 15:55:08 2016 UTC (7 years, 11 months ago) by schoenebeck
File size: 8924 byte(s)
* Automake: set environment variable GCC_COLORS=auto to allow GCC to
  auto detect whether it (sh/c)ould output its messages in color.
* Instrument script editor: Added support for liblinuxsampler's
  NKSP syntax highlighting backend.
* Bumped version (1.0.0.svn2).

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

  ViewVC Help
Powered by ViewVC