Skip script-setup if there are no parameters (and name is unique)
[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 "pbd/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, FILL|EXPAND, SHRINK);
46         t->attach (_type, 1, 2, ty, ty+1, FILL|EXPAND, SHRINK);
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, FILL|EXPAND, SHRINK);
52         t->attach (_author, 1, 2, ty, ty+1, FILL|EXPAND, SHRINK);
53         ++ty;
54
55         Frame* f = manage(new Frame (_("Description")));
56         f->add (_description);
57         t->attach (*f, 0, 2, ty, ty+1);
58         ++ty;
59
60         _description.set_padding (5, 5);
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         LuaScripting::instance ().refresh ();
116         _script.reset ();
117         _scripts = LuaScripting::instance ().scripts (_script_type);
118         setup_list ();
119 }
120
121 ///////////////////////////////////////////////////////////////////////////////
122
123 SessionScriptManager::SessionScriptManager (std::string title, const std::vector<std::string> &names)
124         : ArdourDialog (title)
125 {
126         assert (names.size() > 0);
127         Gtkmm2ext::set_popdown_strings (_names_combo, names);
128         _names_combo.set_active(0);
129
130         Gtk::Label* l;
131         l = manage (new Label (_("Select Script to unload")));
132
133         get_vbox()->set_spacing (6);
134         get_vbox()->pack_start (*l, false, false);
135         get_vbox()->pack_start (_names_combo, false, false);
136
137         add_button (Stock::CANCEL, RESPONSE_CANCEL);
138         add_button (Stock::REMOVE, RESPONSE_ACCEPT);
139         set_default_response (RESPONSE_CANCEL);
140
141         show_all ();
142 }
143
144 ///////////////////////////////////////////////////////////////////////////////
145
146
147 ScriptParameterDialog::ScriptParameterDialog (std::string title,
148                 const LuaScriptInfoPtr& spi,
149                 const std::vector<std::string> &names,
150                 LuaScriptParamList& lsp)
151         : ArdourDialog (title)
152         , _existing_names (names)
153         , _lsp (lsp)
154 {
155         Gtk::Label* l;
156
157         Table* t = manage (new Table (4, 3));
158         t->set_spacings (6);
159
160         _name_entry.set_text (spi->name);
161         _name_entry.signal_changed().connect (sigc::mem_fun (*this, &ScriptParameterDialog::update_sensitivity));
162
163         int ty = 0;
164
165         l = manage (new Label (_("<b>Name:</b>"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
166         l->set_use_markup ();
167         t->attach (*l, 0, 1, ty, ty+1);
168         t->attach (_name_entry, 1, 2, ty, ty+1);
169         ++ty;
170
171         if (_lsp.size () > 0) {
172                 l = manage (new Label (_("<b>Instance Parameters</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
173                 l->set_use_markup ();
174                 t->attach (*l, 0, 2, ty, ty+1);
175                 ++ty;
176         }
177
178         for (size_t i = 0; i < _lsp.size (); ++i) {
179                 Entry* e = manage (new Entry());
180                 if (_lsp[i]->optional) {
181                         CheckButton* c = manage (new CheckButton (_lsp[i]->title));
182                         c->set_active (!_lsp[i]->dflt.empty());
183                         c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::active_changed), i, c, e));
184                         t->attach (*c, 0, 1, ty, ty+1);
185                 } else {
186                         Label* l = manage (new Label (_lsp[i]->title, Gtk::ALIGN_LEFT));
187                         t->attach (*l, 0, 1, ty, ty+1);
188                 }
189
190                 e->set_text (_lsp[i]->dflt);
191                 e->set_sensitive (!_lsp[i]->dflt.empty());
192                 e->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::value_changed), i, e));
193
194                 t->attach (*e, 1, 2, ty, ty+1);
195                 ++ty;
196         }
197
198         add_button (Stock::CANCEL, RESPONSE_CANCEL);
199         _add = add_button (Stock::ADD, RESPONSE_ACCEPT);
200         set_default_response (RESPONSE_ACCEPT);
201
202         get_vbox()->pack_start (*t, true, true);
203         show_all ();
204         update_sensitivity ();
205 }
206
207 bool
208 ScriptParameterDialog::need_interation () const
209 {
210         if (_lsp.size () > 0) {
211                 return false;
212         }
213         if (!parameters_ok ()) {
214                 return false;
215         }
216         return true;
217 }
218
219 bool
220 ScriptParameterDialog::parameters_ok () const
221 {
222         std::string n = _name_entry.get_text ();
223         if (n.empty() || std::find (_existing_names.begin(), _existing_names.end(), n) != _existing_names.end()) {
224                 return false;
225         }
226
227         for (size_t i = 0; i < _lsp.size(); ++i) {
228                 if (!_lsp[i]->optional && _lsp[i]->value.empty()) {
229                         return false;
230                 }
231         }
232         return true;
233 }
234
235 void
236 ScriptParameterDialog::update_sensitivity ()
237 {
238         _add->set_sensitive (parameters_ok ());
239 }
240
241 void
242 ScriptParameterDialog::active_changed (int i, Gtk::CheckButton* c, Gtk::Entry* e)
243 {
244         bool en = c->get_active ();
245         _lsp[i]->is_set = en;
246         e->set_sensitive (en);
247 }
248
249 void
250 ScriptParameterDialog::value_changed (int i, Gtk::Entry* e)
251 {
252         _lsp[i]->value = e->get_text ();
253 }