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