/[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 2604 - (hide annotations) (download)
Sat Jun 7 22:34:31 2014 UTC (9 years, 10 months ago) by schoenebeck
File size: 5718 byte(s)
* GIG SOUND FORMAT EXTENSION: Added support for managing and
  editing real-time instrument scripts. Instrument scripts are
  a LinuxSampler extension of the original Giga format and will
  not load with the GigaStudio software.
* Added a new tab "Scripts" where script groups and scripts can
  be created and deleted.
* Added an initial script editor.

1 schoenebeck 2604 /*
2     Copyright (c) 2014 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    
11     static const std::string _keywords[] = {
12     "on", "end", "declare", "while", "if", "or", "and", "not", "else", "case",
13     "select", "to", "const", "polyphonic", "mod"
14     };
15     static int _keywordsSz = sizeof(_keywords) / sizeof(std::string);
16    
17     static const std::string _eventNames[] = {
18     "init", "note", "release", "controller"
19     };
20     static int _eventNamesSz = sizeof(_eventNames) / sizeof(std::string);
21    
22     static bool isKeyword(const Glib::ustring& s) {
23     for (int i = 0; i < _keywordsSz; ++i)
24     if (_keywords[i] == s) return true;
25     return false;
26     }
27    
28     static bool isEvent(const Glib::ustring& s) {
29     for (int i = 0; i < _eventNamesSz; ++i)
30     if (_eventNames[i] == s) return true;
31     return false;
32     }
33    
34     ScriptEditor::ScriptEditor() :
35     m_applyButton(Gtk::Stock::APPLY), m_cancelButton(Gtk::Stock::CANCEL)
36     {
37     m_script = NULL;
38    
39     add(m_vbox);
40    
41     m_tagTable = Gtk::TextBuffer::TagTable::create();
42     m_keywordTag = Gtk::TextBuffer::Tag::create();
43     m_keywordTag->property_weight() = PANGO_WEIGHT_BOLD;
44     m_tagTable->add(m_keywordTag);
45     m_eventTag = Gtk::TextBuffer::Tag::create();
46     m_eventTag->property_foreground() = "blue";
47     m_tagTable->add(m_eventTag);
48     m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
49     m_textView.set_buffer(m_textBuffer);
50     {
51     Pango::FontDescription fdesc;
52     fdesc.set_family("monospace");
53     fdesc.set_size(10 * PANGO_SCALE);
54     #if GTKMM_MAJOR_VERSION < 3
55     m_textView.modify_font(fdesc);
56     #else
57     m_textView.override_font(fdesc);
58     #endif
59     }
60     m_scrolledWindow.add(m_textView);
61     m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
62     m_vbox.pack_start(m_scrolledWindow);
63    
64     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
65     m_buttonBox.pack_start(m_applyButton);
66     m_buttonBox.pack_start(m_cancelButton);
67     m_applyButton.set_can_default();
68     m_applyButton.set_sensitive(false);
69     m_applyButton.grab_focus();
70     m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
71    
72     m_applyButton.signal_clicked().connect(
73     sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
74     );
75    
76     m_cancelButton.signal_clicked().connect(
77     sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
78     );
79    
80     m_textBuffer->signal_insert().connect(
81     sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
82     );
83    
84     m_textBuffer->signal_erase().connect(
85     sigc::mem_fun(*this, &ScriptEditor::onTextErased)
86     );
87    
88     m_textBuffer->signal_modified_changed().connect(
89     sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
90     );
91    
92     signal_hide().connect(
93     sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
94     );
95    
96     show_all_children();
97    
98     resize(460,300);
99     }
100    
101     ScriptEditor::~ScriptEditor() {
102     printf("ScriptEditor destruct\n");
103     }
104    
105     void ScriptEditor::setScript(gig::Script* script) {
106     m_script = script;
107     if (!script) {
108     set_title(_("No Script"));
109     return;
110     }
111    
112     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
113    
114     std::string txt = script->GetScriptAsText();
115     //printf("text : '%s'\n", txt.c_str());
116     m_textBuffer->set_text(txt);
117     m_textBuffer->set_modified(false);
118     }
119    
120     void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
121     printf("inserted %d\n", length);
122     Gtk::TextBuffer::iterator itStart = itEnd;
123     itStart.backward_chars(length);
124    
125     Gtk::TextBuffer::iterator it = itStart;
126     it.backward_word_start();
127    
128     bool eofReached = false;
129     while (it <= itEnd) {
130     Gtk::TextBuffer::iterator itWordStart = it;
131     if (!it.forward_word_end()) {
132     eofReached = true;
133     it = itEnd;
134     }
135    
136     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
137     printf("{%s}\n", s.c_str());
138     if (isKeyword(s))
139     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
140     else if (isEvent(s)) {
141     // check if previous word is "on"
142     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
143     if (itPreviousWordStart.backward_word_start()) {
144     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
145     itPreviousWordEnd.forward_word_end();
146     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
147     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
148     }
149     }
150     }
151    
152     if (eofReached) break;
153    
154     while (!it.inside_word())
155     if (!it.forward_char())
156     goto EOF_REACHED;
157     }
158    
159     EOF_REACHED:
160     ;
161     }
162    
163     void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
164     printf("erased\n");
165     Gtk::TextBuffer::iterator itStart2 = itStart;
166     if (itStart2.inside_word() || itStart2.ends_word())
167     itStart2.backward_word_start();
168    
169     Gtk::TextBuffer::iterator itEnd2 = itEnd;
170     if (itEnd2.inside_word()) itEnd2.forward_word_end();
171    
172     m_textBuffer->remove_all_tags(itStart2, itEnd2);
173     }
174    
175     void ScriptEditor::onModifiedChanged() {
176     m_applyButton.set_sensitive( m_textBuffer->get_modified() );
177     }
178    
179     void ScriptEditor::onButtonCancel() {
180     hide();
181     }
182    
183     void ScriptEditor::onButtonApply() {
184     m_script->SetScriptAsText(m_textBuffer->get_text());
185     m_textBuffer->set_modified(false);
186     }
187    
188     void ScriptEditor::onWindowHide() {
189     delete this; // this is the end, my friend
190     }

  ViewVC Help
Powered by ViewVC