enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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);
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         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                 CheckButton* c = manage (new CheckButton (_lsp[i]->title));
180                 Entry* e = manage (new Entry());
181                 c->set_active (!_lsp[i]->optional); // also if default ??
182                 c->set_sensitive (_lsp[i]->optional);
183                 e->set_text (_lsp[i]->dflt);
184                 e->set_sensitive (c->get_active ());
185
186                 c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::active_changed), i, c, e));
187                 e->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &ScriptParameterDialog::value_changed), i, e));
188
189                 t->attach (*c, 0, 1, ty, ty+1);
190                 t->attach (*e, 1, 2, ty, ty+1);
191                 ++ty;
192         }
193
194         add_button (Stock::CANCEL, RESPONSE_CANCEL);
195         _add = add_button (Stock::ADD, RESPONSE_ACCEPT);
196         set_default_response (RESPONSE_ACCEPT);
197
198         get_vbox()->pack_start (*t, true, true);
199         show_all ();
200         update_sensitivity ();
201 }
202
203 void
204 ScriptParameterDialog::update_sensitivity ()
205 {
206         std::string n = _name_entry.get_text ();
207         if (n.empty() || std::find (_existing_names.begin(), _existing_names.end(), n) != _existing_names.end()) {
208                 _add->set_sensitive (false);
209                 return;
210         }
211
212         for (size_t i = 0; i < _lsp.size(); ++i) {
213                 if (!_lsp[i]->optional && _lsp[i]->value.empty()) {
214                         _add->set_sensitive (false);
215                         return;
216                 }
217         }
218
219         _add->set_sensitive (true);
220 }
221
222 void
223 ScriptParameterDialog::active_changed (int i, Gtk::CheckButton* c, Gtk::Entry* e)
224 {
225         bool en = c->get_active ();
226         _lsp[i]->is_set = en;
227         e->set_sensitive (en);
228 }
229
230 void
231 ScriptParameterDialog::value_changed (int i, Gtk::Entry* e)
232 {
233         _lsp[i]->value = e->get_text ();
234 }