Dialog to select and configure Lua Scripts
[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 "gtkmm2ext/utils.h"
20
21 #include "script_selector.h"
22 #include "i18n.h"
23
24 using namespace std;
25 using namespace Gtk;
26 using namespace ARDOUR;
27
28 ScriptSelector::ScriptSelector (std::string title, LuaScriptInfo::ScriptType type)
29         : ArdourDialog (title)
30         , _type ("", Gtk::ALIGN_START, Gtk::ALIGN_CENTER)
31         , _author ("", Gtk::ALIGN_START, Gtk::ALIGN_CENTER)
32         , _description ("", Gtk::ALIGN_START, Gtk::ALIGN_START)
33         , _scripts (LuaScripting::instance ().scripts (type))
34         , _script_type (type)
35 {
36         Gtk::Label* l;
37
38         Table* t = manage (new Table (3, 2));
39         t->set_spacings (6);
40
41         int ty = 0;
42
43         l = manage (new Label (_("<b>Type:</b>"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
44         l->set_use_markup ();
45         t->attach (*l, 0, 1, ty, ty+1);
46         t->attach (_type, 1, 2, ty, ty+1);
47         ++ty;
48
49         l = manage (new Label (_("<b>Author:</b>"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
50         l->set_use_markup ();
51         t->attach (*l, 0, 1, ty, ty+1);
52         t->attach (_author, 1, 2, ty, ty+1);
53         ++ty;
54
55         l = manage (new Label (_("<b>Description:</b>"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
56         l->set_use_markup ();
57         t->attach (*l, 0, 1, ty, ty+1);
58         t->attach (_description, 1, 2, ty, ty+1);
59         ++ty;
60
61         _description.set_line_wrap();
62
63         get_vbox()->set_spacing (6);
64         get_vbox()->pack_start (_script_combo, false, false);
65         get_vbox()->pack_start (*t, true, true);
66
67         Button *r = Gtk::manage (new Gtk::Button (Stock::REFRESH));
68         r->signal_clicked().connect (sigc::mem_fun (*this, &ScriptSelector::refresh));
69         get_action_area()->pack_start(*r);
70
71         add_button (Stock::CANCEL, RESPONSE_CANCEL);
72         _add = add_button (Stock::ADD, RESPONSE_ACCEPT);
73         set_default_response (RESPONSE_ACCEPT);
74
75         _add->set_sensitive (false);
76         _combocon = _script_combo.signal_changed().connect (sigc::mem_fun (*this, &ScriptSelector::script_combo_changed));
77
78         setup_list ();
79         show_all ();
80 }
81
82 void
83 ScriptSelector::setup_list ()
84 {
85         _combocon.block();
86         vector<string> script_names;
87         for (LuaScriptList::const_iterator s = _scripts.begin(); s != _scripts.end(); ++s) {
88                 script_names.push_back ((*s)->name);
89         }
90
91         Gtkmm2ext::set_popdown_strings (_script_combo, script_names);
92         if (script_names.size() > 0) {
93                 _script_combo.set_active(0);
94                 script_combo_changed ();
95         }
96         _combocon.unblock();
97 }
98
99 void
100 ScriptSelector::script_combo_changed ()
101 {
102         int i = _script_combo.get_active_row_number();
103         _script = _scripts[i];
104
105         _type.set_text(LuaScriptInfo::type2str (_script->type));
106         _author.set_text (_script->author);
107         _description.set_text (_script->description);
108
109         _add->set_sensitive (Glib::file_test(_script->path, Glib::FILE_TEST_EXISTS));
110 }
111
112 void
113 ScriptSelector::refresh ()
114 {
115         _scripts = LuaScripting::instance ().scripts (_script_type);
116         setup_list ();
117 }
118
119 ///////////////////////////////////////////////////////////////////////////////
120
121 SessionScriptManager::SessionScriptManager (std::string title, const std::vector<std::string> &names)
122         : ArdourDialog (title)
123 {
124         assert (names.size() > 0);
125         Gtkmm2ext::set_popdown_strings (_names_combo, names);
126         _names_combo.set_active(0);
127
128         Gtk::Label* l;
129         l = manage (new Label (_("Select Script to unload")));
130
131         get_vbox()->set_spacing (6);
132         get_vbox()->pack_start (*l, false, false);
133         get_vbox()->pack_start (_names_combo, false, false);
134
135         add_button (Stock::CANCEL, RESPONSE_CANCEL);
136         add_button (Stock::REMOVE, RESPONSE_ACCEPT);
137         set_default_response (RESPONSE_CANCEL);
138
139         show_all ();
140 }
141
142 ///////////////////////////////////////////////////////////////////////////////
143
144
145 ScriptParameterDialog::ScriptParameterDialog (std::string title,
146                 const LuaScriptInfoPtr& spi,
147                 const std::vector<std::string> &names,
148                 LuaScriptParamList& lsp)
149         : ArdourDialog (title)
150         , _existing_names (names)
151         , _lsp (lsp)
152 {
153         Gtk::Label* l;
154
155         Table* t = manage (new Table (4, 3));
156         t->set_spacings (6);
157
158         _name_entry.set_text (spi->name);
159         _name_entry.signal_changed().connect (sigc::mem_fun (*this, &ScriptParameterDialog::update_sensitivity));
160
161         int ty = 0;
162
163         l = manage (new Label (_("<b>Name:</b>"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
164         l->set_use_markup ();
165         t->attach (*l, 0, 1, ty, ty+1);
166         t->attach (_name_entry, 1, 2, ty, ty+1);
167         ++ty;
168
169         l = manage (new Label (_("<b>Parameters:</b>"), Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, false));
170         l->set_use_markup ();
171         t->attach (_name_entry, 0, 2, ty, ty+1);
172         ++ty;
173
174         for (size_t i = 0; i < _lsp.size (); ++i) {
175                 CheckButton* c = manage (new CheckButton (_lsp[i]->title));
176                 Entry* e = manage (new Entry());
177                 c->set_active (!_lsp[i]->optional); // also if default ??
178                 c->set_sensitive (_lsp[i]->optional);
179                 e->set_text (_lsp[i]->dflt);
180                 e->set_sensitive (c->get_active ());
181
182                 c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::active_changed), i, c, e));
183                 e->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::value_changed), i, e));
184
185                 t->attach (*c, 0, 1, ty, ty+1);
186                 t->attach (*e, 1, 2, ty, ty+1);
187                 ++ty;
188         }
189
190         add_button (Stock::CANCEL, RESPONSE_CANCEL);
191         _add = add_button (Stock::ADD, RESPONSE_ACCEPT);
192         set_default_response (RESPONSE_ACCEPT);
193
194         get_vbox()->pack_start (*t, true, true);
195         show_all ();
196         update_sensitivity ();
197 }
198
199 void
200 ScriptParameterDialog::update_sensitivity ()
201 {
202         std::string n = _name_entry.get_text ();
203         if (n.empty() || std::find (_existing_names.begin(), _existing_names.end(), n) != _existing_names.end()) {
204                 _add->set_sensitive (false);
205                 return;
206         }
207
208         for (size_t i = 0; i < _lsp.size(); ++i) {
209                 if (!_lsp[i]->optional && _lsp[i]->value.empty()) {
210                         _add->set_sensitive (false);
211                         return;
212                 }
213         }
214
215         _add->set_sensitive (true);
216 }
217
218 void
219 ScriptParameterDialog::active_changed (int i, Gtk::CheckButton* c, Gtk::Entry* e)
220 {
221         bool en = c->get_active ();
222         _lsp[i]->is_set = en;
223         e->set_sensitive (en);
224 }
225
226 void
227 ScriptParameterDialog::value_changed (int i, Gtk::Entry* e)
228 {
229         _lsp[i]->value = e->get_text ();
230 }