/[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 2664 - (hide annotations) (download)
Sat Jul 5 19:43:14 2014 UTC (9 years, 8 months ago) by schoenebeck
File size: 5857 byte(s)
* If launched by the sampler ("live-mode"): automatically
  select and show the instrument which the sampler requested
  gigedit to edit.
* OS X: Increased instrument script editor's font size by 20%.

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 schoenebeck 2610 m_eventTag->property_weight() = PANGO_WEIGHT_BOLD;
48 schoenebeck 2604 m_tagTable->add(m_eventTag);
49     m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
50     m_textView.set_buffer(m_textBuffer);
51     {
52     Pango::FontDescription fdesc;
53     fdesc.set_family("monospace");
54 schoenebeck 2664 #if defined(__APPLE__)
55     fdesc.set_size(12 * PANGO_SCALE);
56     #else
57 schoenebeck 2604 fdesc.set_size(10 * PANGO_SCALE);
58 schoenebeck 2664 #endif
59 schoenebeck 2604 #if GTKMM_MAJOR_VERSION < 3
60     m_textView.modify_font(fdesc);
61     #else
62     m_textView.override_font(fdesc);
63     #endif
64     }
65     m_scrolledWindow.add(m_textView);
66     m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
67     m_vbox.pack_start(m_scrolledWindow);
68    
69     m_buttonBox.set_layout(Gtk::BUTTONBOX_END);
70     m_buttonBox.pack_start(m_applyButton);
71     m_buttonBox.pack_start(m_cancelButton);
72     m_applyButton.set_can_default();
73     m_applyButton.set_sensitive(false);
74     m_applyButton.grab_focus();
75     m_vbox.pack_start(m_buttonBox, Gtk::PACK_SHRINK);
76    
77     m_applyButton.signal_clicked().connect(
78     sigc::mem_fun(*this, &ScriptEditor::onButtonApply)
79     );
80    
81     m_cancelButton.signal_clicked().connect(
82     sigc::mem_fun(*this, &ScriptEditor::onButtonCancel)
83     );
84    
85     m_textBuffer->signal_insert().connect(
86     sigc::mem_fun(*this, &ScriptEditor::onTextInserted)
87     );
88    
89     m_textBuffer->signal_erase().connect(
90     sigc::mem_fun(*this, &ScriptEditor::onTextErased)
91     );
92    
93     m_textBuffer->signal_modified_changed().connect(
94     sigc::mem_fun(*this, &ScriptEditor::onModifiedChanged)
95     );
96    
97     signal_hide().connect(
98     sigc::mem_fun(*this, &ScriptEditor::onWindowHide)
99     );
100    
101     show_all_children();
102    
103     resize(460,300);
104     }
105    
106     ScriptEditor::~ScriptEditor() {
107     printf("ScriptEditor destruct\n");
108     }
109    
110     void ScriptEditor::setScript(gig::Script* script) {
111     m_script = script;
112     if (!script) {
113     set_title(_("No Script"));
114     return;
115     }
116    
117     set_title(std::string(_("Instrument Script")) + " - \"" + script->Name + "\"");
118    
119     std::string txt = script->GetScriptAsText();
120     //printf("text : '%s'\n", txt.c_str());
121     m_textBuffer->set_text(txt);
122     m_textBuffer->set_modified(false);
123     }
124    
125     void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
126 schoenebeck 2610 //printf("inserted %d\n", length);
127 schoenebeck 2604 Gtk::TextBuffer::iterator itStart = itEnd;
128     itStart.backward_chars(length);
129    
130     Gtk::TextBuffer::iterator it = itStart;
131     it.backward_word_start();
132    
133     bool eofReached = false;
134     while (it <= itEnd) {
135     Gtk::TextBuffer::iterator itWordStart = it;
136     if (!it.forward_word_end()) {
137     eofReached = true;
138     it = itEnd;
139     }
140    
141     Glib::ustring s = m_textBuffer->get_text(itWordStart, it, false);
142 schoenebeck 2610 //printf("{%s}\n", s.c_str());
143 schoenebeck 2604 if (isKeyword(s))
144     m_textBuffer->apply_tag(m_keywordTag, itWordStart, it);
145     else if (isEvent(s)) {
146     // check if previous word is "on"
147     Gtk::TextBuffer::iterator itPreviousWordStart = itWordStart;
148     if (itPreviousWordStart.backward_word_start()) {
149     Gtk::TextBuffer::iterator itPreviousWordEnd = itPreviousWordStart;
150     itPreviousWordEnd.forward_word_end();
151     if (m_textBuffer->get_text(itPreviousWordStart, itPreviousWordEnd, false) == "on") {
152     m_textBuffer->apply_tag(m_eventTag, itWordStart, it);
153     }
154     }
155     }
156    
157     if (eofReached) break;
158    
159     while (!it.inside_word())
160     if (!it.forward_char())
161     goto EOF_REACHED;
162     }
163    
164     EOF_REACHED:
165     ;
166     }
167    
168     void ScriptEditor::onTextErased(const Gtk::TextBuffer::iterator& itStart, const Gtk::TextBuffer::iterator& itEnd) {
169 schoenebeck 2610 //printf("erased\n");
170 schoenebeck 2604 Gtk::TextBuffer::iterator itStart2 = itStart;
171     if (itStart2.inside_word() || itStart2.ends_word())
172     itStart2.backward_word_start();
173    
174     Gtk::TextBuffer::iterator itEnd2 = itEnd;
175     if (itEnd2.inside_word()) itEnd2.forward_word_end();
176    
177     m_textBuffer->remove_all_tags(itStart2, itEnd2);
178     }
179    
180     void ScriptEditor::onModifiedChanged() {
181     m_applyButton.set_sensitive( m_textBuffer->get_modified() );
182     }
183    
184     void ScriptEditor::onButtonCancel() {
185     hide();
186     }
187    
188     void ScriptEditor::onButtonApply() {
189     m_script->SetScriptAsText(m_textBuffer->get_text());
190     m_textBuffer->set_modified(false);
191     }
192    
193     void ScriptEditor::onWindowHide() {
194     delete this; // this is the end, my friend
195     }

  ViewVC Help
Powered by ViewVC