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