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