Fix saving Lua Callbacks when un/register succeeds
[ardour.git] / gtk2_ardour / script_selector.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <gtkmm/frame.h>
20 #include <gtkmm/stock.h>
21 #include <gtkmm/table.h>
22
23 #include "gtkmm2ext/utils.h"
24
25 #include "script_selector.h"
26 #include "pbd/i18n.h"
27
28 using namespace std;
29 using namespace Gtk;
30 using namespace ARDOUR;
31
32 ScriptSelector::ScriptSelector (std::string title, LuaScriptInfo::ScriptType type)
33         : ArdourDialog (title)
34         , _type_label ("<b>Type:</b>", Gtk::ALIGN_END, Gtk::ALIGN_CENTER)
35         , _type ("", Gtk::ALIGN_START, Gtk::ALIGN_CENTER)
36         , _author_label ("<b>Author:</b>", Gtk::ALIGN_END, Gtk::ALIGN_CENTER)
37         , _author ("", Gtk::ALIGN_START, Gtk::ALIGN_CENTER)
38         , _description ("", Gtk::ALIGN_START, Gtk::ALIGN_START)
39         , _scripts (LuaScripting::instance ().scripts (type))
40         , _script_type (type)
41 {
42         Table* t = manage (new Table (3, 2));
43         t->set_spacings (6);
44
45         int ty = 0;
46
47         _type_label.set_use_markup ();
48         t->attach (_type_label, 0, 1, ty, ty+1, FILL|EXPAND, SHRINK);
49         t->attach (_type, 1, 2, ty, ty+1, FILL|EXPAND, SHRINK);
50         ++ty;
51
52         _author_label.set_use_markup ();
53         t->attach (_author_label, 0, 1, ty, ty+1, FILL|EXPAND, SHRINK);
54         t->attach (_author, 1, 2, ty, ty+1, FILL|EXPAND, SHRINK);
55         ++ty;
56
57         Frame* f = manage(new Frame (_("Description")));
58         f->add (_description);
59         t->attach (*f, 0, 2, ty, ty+1);
60         ++ty;
61
62         _description.set_padding (5, 5);
63         _description.set_line_wrap();
64
65         get_vbox()->set_spacing (6);
66         get_vbox()->pack_start (_script_combo, false, false);
67         get_vbox()->pack_start (*t, true, true);
68
69         Button *r = Gtk::manage (new Gtk::Button (Stock::REFRESH));
70         r->signal_clicked().connect (sigc::mem_fun (*this, &ScriptSelector::refresh));
71         get_action_area()->pack_start(*r);
72
73         add_button (Stock::CANCEL, RESPONSE_CANCEL);
74         _add = add_button (Stock::ADD, RESPONSE_ACCEPT);
75         set_default_response (RESPONSE_ACCEPT);
76
77         _add->set_sensitive (false);
78         _combocon = _script_combo.signal_changed().connect (sigc::mem_fun (*this, &ScriptSelector::script_combo_changed));
79
80         setup_list ();
81         show_all ();
82
83         script_combo_changed();
84 }
85
86 bool
87 ScriptSelector::script_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
88 {
89         _script_combo.set_active (i);
90
91         return _script_combo.get_active_text () == "--separator--";
92 }
93
94 void
95 ScriptSelector::setup_list ()
96 {
97         _combocon.block();
98
99         vector<string> script_names;
100         for (LuaScriptList::const_iterator s = _scripts.begin(); s != _scripts.end(); ++s) {
101                 if ((*s)->name != "Shortcut" || _script_type != LuaScriptInfo::EditorAction) {
102                         script_names.push_back ((*s)->name);
103                 }
104         }
105
106         _script_combo.clear();
107         _script_combo.set_row_separator_func (sigc::mem_fun (*this, &ScriptSelector::script_separator));
108
109         if (_script_type == LuaScriptInfo::EditorAction) {
110                 _script_combo.append_text ("Shortcut");
111                 _script_combo.append_text ("--separator--");
112         }
113
114         vector<string>::const_iterator i;
115         for (i = script_names.begin(); i != script_names.end(); ++i) {
116                 _script_combo.append_text (*i);
117         }
118
119         _script_combo.set_active(0);
120         script_combo_changed ();
121
122         _combocon.unblock();
123 }
124
125 void
126 ScriptSelector::script_combo_changed ()
127 {
128         std::string nm = _script_combo.get_active_text();
129
130         for (LuaScriptList::const_iterator s = _scripts.begin(); s != _scripts.end(); ++s) {
131                 if ((*s)->name == nm) {
132                         _script = (*s);
133                 }
134         }
135
136         if (_script) {
137
138                 if (_script->name == "Shortcut") {
139                         _type.hide();
140                         _type_label.hide();
141                         _author.hide();
142                         _author_label.hide();
143                         _description.set_text (_script->description);
144                 } else {
145                         _type.show();
146                         _type_label.show();
147                         _author.show();
148                         _author_label.show();
149                         _type.set_text(LuaScriptInfo::type2str (_script->type));
150                         _author.set_text (_script->author);
151                         _description.set_text (_script->description);
152                 }
153
154                 _add->set_sensitive (Glib::file_test(_script->path, Glib::FILE_TEST_EXISTS));
155         }
156 }
157
158 void
159 ScriptSelector::refresh ()
160 {
161         LuaScripting::instance ().refresh ();
162         _script.reset ();
163         _scripts = LuaScripting::instance ().scripts (_script_type);
164         setup_list ();
165 }
166
167 ///////////////////////////////////////////////////////////////////////////////
168
169 SessionScriptManager::SessionScriptManager (std::string title, const std::vector<std::string> &names)
170         : ArdourDialog (title)
171 {
172         assert (names.size() > 0);
173         Gtkmm2ext::set_popdown_strings (_names_combo, names);
174         _names_combo.set_active(0);
175
176         Gtk::Label* l;
177         l = manage (new Label (_("Select Script to unload")));
178
179         get_vbox()->set_spacing (6);
180         get_vbox()->pack_start (*l, false, false);
181         get_vbox()->pack_start (_names_combo, false, false);
182
183         add_button (Stock::CANCEL, RESPONSE_CANCEL);
184         add_button (Stock::REMOVE, RESPONSE_ACCEPT);
185         set_default_response (RESPONSE_CANCEL);
186
187         show_all ();
188 }
189
190 ///////////////////////////////////////////////////////////////////////////////
191
192
193 ScriptParameterDialog::ScriptParameterDialog (std::string title,
194                 const LuaScriptInfoPtr& spi,
195                 const std::vector<std::string> &names,
196                 LuaScriptParamList& lsp)
197         : ArdourDialog (title)
198         , _existing_names (names)
199         , _lsp (lsp)
200 {
201         Gtk::Label* l;
202
203         Table* t = manage (new Table (4, 3));
204         t->set_spacings (6);
205
206         _name_entry.set_text (spi->name);
207
208         for (size_t i = 0; i < _lsp.size(); ++i) {
209                 if (_lsp[i]->preseeded && _lsp[i]->name == "x-script-name" && !_lsp[i]->value.empty ()) {
210                         _name_entry.set_text (_lsp[i]->value);
211                 }
212         }
213
214         _name_entry.signal_changed().connect (sigc::mem_fun (*this, &ScriptParameterDialog::update_sensitivity));
215
216         int ty = 0;
217
218         l = manage (new Label (_("<b>Name:</b>"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
219         l->set_use_markup ();
220         t->attach (*l, 0, 1, ty, ty+1);
221         t->attach (_name_entry, 1, 2, ty, ty+1);
222         ++ty;
223
224         if (_lsp.size () > 0) {
225                 l = manage (new Label (_("<b>Instance Parameters</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
226                 l->set_use_markup ();
227                 t->attach (*l, 0, 2, ty, ty+1);
228                 ++ty;
229         }
230
231         for (size_t i = 0; i < _lsp.size (); ++i) {
232                 Entry* e = manage (new Entry());
233                 if (_lsp[i]->optional) {
234                         CheckButton* c = manage (new CheckButton (_lsp[i]->title));
235                         c->set_active (!_lsp[i]->dflt.empty());
236                         c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::active_changed), i, c, e));
237                         t->attach (*c, 0, 1, ty, ty+1);
238                 } else {
239                         Label* l = manage (new Label (_lsp[i]->title, Gtk::ALIGN_LEFT));
240                         t->attach (*l, 0, 1, ty, ty+1);
241                 }
242
243                 e->set_text (_lsp[i]->dflt);
244                 e->set_sensitive (!_lsp[i]->dflt.empty());
245                 e->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::value_changed), i, e));
246
247                 t->attach (*e, 1, 2, ty, ty+1);
248                 ++ty;
249         }
250
251         add_button (Stock::CANCEL, RESPONSE_CANCEL);
252         _add = add_button (Stock::ADD, RESPONSE_ACCEPT);
253         set_default_response (RESPONSE_ACCEPT);
254
255         get_vbox()->pack_start (*t, true, true);
256         show_all ();
257         update_sensitivity ();
258 }
259
260 bool
261 ScriptParameterDialog::need_interation () const
262 {
263         if (!parameters_ok ()) {
264                 return true;
265         }
266         for (size_t i = 0; i < _lsp.size(); ++i) {
267                 if (!_lsp[i]->optional && !_lsp[i]->preseeded) {
268                         return true;
269                 }
270         }
271         return false;
272 }
273
274 bool
275 ScriptParameterDialog::parameters_ok () const
276 {
277         std::string n = _name_entry.get_text ();
278         if (n.empty() || std::find (_existing_names.begin(), _existing_names.end(), n) != _existing_names.end()) {
279                 return false;
280         }
281
282         for (size_t i = 0; i < _lsp.size(); ++i) {
283                 if (!_lsp[i]->optional && _lsp[i]->value.empty()) {
284                         return false;
285                 }
286         }
287         return true;
288 }
289
290 void
291 ScriptParameterDialog::update_sensitivity ()
292 {
293         _add->set_sensitive (parameters_ok ());
294 }
295
296 void
297 ScriptParameterDialog::active_changed (int i, Gtk::CheckButton* c, Gtk::Entry* e)
298 {
299         bool en = c->get_active ();
300         _lsp[i]->is_set = en;
301         e->set_sensitive (en);
302 }
303
304 void
305 ScriptParameterDialog::value_changed (int i, Gtk::Entry* e)
306 {
307         _lsp[i]->value = e->get_text ();
308 }