push2: tweak layout APIs etc.
[ardour.git] / libs / surfaces / push2 / gui.cc
1 /*
2     Copyright (C) 2015 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
23
24 #include "pbd/unwind.h"
25 #include "pbd/strsplit.h"
26 #include "pbd/file_utils.h"
27
28 #include "gtkmm2ext/bindings.h"
29 #include "gtkmm2ext/gui_thread.h"
30 #include "gtkmm2ext/utils.h"
31
32 #include "ardour/audioengine.h"
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/parameter_descriptor.h"
35
36 #include "push2.h"
37 #include "gui.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace PBD;
42 using namespace ARDOUR;
43 using namespace ArdourSurface;
44 using namespace std;
45 using namespace Gtk;
46 using namespace Gtkmm2ext;
47
48 void*
49 Push2::get_gui () const
50 {
51         if (!gui) {
52                 const_cast<Push2*>(this)->build_gui ();
53         }
54         static_cast<Gtk::VBox*>(gui)->show_all();
55         return gui;
56 }
57
58 void
59 Push2::tear_down_gui ()
60 {
61         if (gui) {
62                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
63                 if (w) {
64                         w->hide();
65                         delete w;
66                 }
67         }
68         delete gui;
69         gui = 0;
70 }
71
72 void
73 Push2::build_gui ()
74 {
75         gui = new P2GUI (*this);
76 }
77
78 /*--------------------*/
79
80 P2GUI::P2GUI (Push2& p)
81         : p2 (p)
82         , table (2, 5)
83         , action_table (5, 4)
84         , ignore_active_change (false)
85         , pressure_mode_label (_("Pressure Mode"))
86 {
87         set_border_width (12);
88
89         table.set_row_spacings (4);
90         table.set_col_spacings (6);
91         table.set_border_width (12);
92         table.set_homogeneous (false);
93
94         std::string data_file_path;
95         string name = "push2-small.png";
96         Searchpath spath(ARDOUR::ardour_data_search_path());
97         spath.add_subdirectory_to_paths ("icons");
98         find_file (spath, name, data_file_path);
99         if (!data_file_path.empty()) {
100                 image.set (data_file_path);
101                 hpacker.pack_start (image, false, false);
102         }
103
104         Gtk::Label* l;
105         int row = 0;
106
107         input_combo.pack_start (midi_port_columns.short_name);
108         output_combo.pack_start (midi_port_columns.short_name);
109
110         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &P2GUI::active_port_changed), &input_combo, true));
111         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &P2GUI::active_port_changed), &output_combo, false));
112
113         l = manage (new Gtk::Label);
114         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
115         l->set_alignment (1.0, 0.5);
116         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
117         table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
118         row++;
119
120         l = manage (new Gtk::Label);
121         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
122         l->set_alignment (1.0, 0.5);
123         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
124         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
125         row++;
126
127         table.attach (pressure_mode_label, 0, 1, row, row+1, AttachOptions (0), AttachOptions (0));
128         table.attach (pressure_mode_selector, 1, 2, row, row+1, AttachOptions (FILL|EXPAND), AttachOptions (0));
129         row++;
130
131         hpacker.pack_start (table, true, true);
132
133         pressure_mode_selector.set_model (build_pressure_mode_columns());
134         pressure_mode_selector.pack_start (pressure_mode_columns.name);
135         pressure_mode_selector.set_active ((int) p2.pressure_mode());
136         pressure_mode_selector.signal_changed().connect (sigc::mem_fun (*this, &P2GUI::reprogram_pressure_mode));
137
138         set_spacing (12);
139
140         pack_start (hpacker, false, false);
141
142         /* update the port connection combos */
143
144         update_port_combos ();
145
146         /* catch future changes to connection state */
147
148         // p2.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&P2GUI::connection_handler, this), gui_context());
149 }
150
151 P2GUI::~P2GUI ()
152 {
153 }
154
155 void
156 P2GUI::connection_handler ()
157 {
158         /* ignore all changes to combobox active strings here, because we're
159            updating them to match a new ("external") reality - we were called
160            because port connections have changed.
161         */
162
163         PBD::Unwinder<bool> ici (ignore_active_change, true);
164
165         update_port_combos ();
166 }
167
168 void
169 P2GUI::update_port_combos ()
170 {
171         vector<string> midi_inputs;
172         vector<string> midi_outputs;
173
174         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
175         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
176
177         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
178         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
179         bool input_found = false;
180         bool output_found = false;
181         int n;
182
183         input_combo.set_model (input);
184         output_combo.set_model (output);
185
186         Gtk::TreeModel::Children children = input->children();
187         Gtk::TreeModel::Children::iterator i;
188         i = children.begin();
189         ++i; /* skip "Disconnected" */
190
191
192         for (n = 1;  i != children.end(); ++i, ++n) {
193                 string port_name = (*i)[midi_port_columns.full_name];
194                 if (p2.input_port()->connected_to (port_name)) {
195                         input_combo.set_active (n);
196                         input_found = true;
197                         break;
198                 }
199         }
200
201         if (!input_found) {
202                 input_combo.set_active (0); /* disconnected */
203         }
204
205         children = output->children();
206         i = children.begin();
207         ++i; /* skip "Disconnected" */
208
209         for (n = 1;  i != children.end(); ++i, ++n) {
210                 string port_name = (*i)[midi_port_columns.full_name];
211                 if (p2.output_port()->connected_to (port_name)) {
212                         output_combo.set_active (n);
213                         output_found = true;
214                         break;
215                 }
216         }
217
218         if (!output_found) {
219                 output_combo.set_active (0); /* disconnected */
220         }
221 }
222
223 void
224 P2GUI::build_available_action_menu ()
225 {
226         /* build a model of all available actions (needs to be tree structured
227          * more)
228          */
229
230         available_action_model = TreeStore::create (action_columns);
231
232         vector<string> paths;
233         vector<string> labels;
234         vector<string> tooltips;
235         vector<string> keys;
236         vector<Glib::RefPtr<Gtk::Action> > actions;
237
238         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
239
240         typedef std::map<string,TreeIter> NodeMap;
241         NodeMap nodes;
242         NodeMap::iterator r;
243
244
245         vector<string>::iterator k;
246         vector<string>::iterator p;
247         vector<string>::iterator t;
248         vector<string>::iterator l;
249
250         available_action_model->clear ();
251
252         TreeIter rowp;
253         TreeModel::Row parent;
254
255         /* Disabled item (row 0) */
256
257         rowp = available_action_model->append();
258         parent = *(rowp);
259         parent[action_columns.name] = _("Disabled");
260
261         /* Key aliasing */
262
263         rowp = available_action_model->append();
264         parent = *(rowp);
265         parent[action_columns.name] = _("Shift");
266         rowp = available_action_model->append();
267         parent = *(rowp);
268         parent[action_columns.name] = _("Control");
269         rowp = available_action_model->append();
270         parent = *(rowp);
271         parent[action_columns.name] = _("Option");
272         rowp = available_action_model->append();
273         parent = *(rowp);
274         parent[action_columns.name] = _("CmdAlt");
275
276
277         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
278
279                 TreeModel::Row row;
280                 vector<string> parts;
281
282                 parts.clear ();
283
284                 split (*p, parts, '/');
285
286                 if (parts.empty()) {
287                         continue;
288                 }
289
290                 //kinda kludgy way to avoid displaying menu items as mappable
291                 if ( parts[1] == _("Main_menu") )
292                         continue;
293                 if ( parts[1] == _("JACK") )
294                         continue;
295                 if ( parts[1] == _("redirectmenu") )
296                         continue;
297                 if ( parts[1] == _("Editor_menus") )
298                         continue;
299                 if ( parts[1] == _("RegionList") )
300                         continue;
301                 if ( parts[1] == _("ProcessorMenu") )
302                         continue;
303
304                 if ((r = nodes.find (parts[1])) == nodes.end()) {
305
306                         /* top level is missing */
307
308                         TreeIter rowp;
309                         TreeModel::Row parent;
310                         rowp = available_action_model->append();
311                         nodes[parts[1]] = rowp;
312                         parent = *(rowp);
313                         parent[action_columns.name] = parts[1];
314
315                         row = *(available_action_model->append (parent.children()));
316
317                 } else {
318
319                         row = *(available_action_model->append ((*r->second)->children()));
320
321                 }
322
323                 /* add this action */
324
325                 if (l->empty ()) {
326                         row[action_columns.name] = *t;
327                         action_map[*t] = *p;
328                 } else {
329                         row[action_columns.name] = *l;
330                         action_map[*l] = *p;
331                 }
332
333                 string path = (*p);
334                 /* ControlProtocol::access_action() is not interested in the
335                    legacy "<Actions>/" prefix part of a path.
336                 */
337                 path = path.substr (strlen ("<Actions>/"));
338
339                 row[action_columns.path] = path;
340         }
341 }
342
343
344 bool
345 P2GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const & action_path, TreeModel::iterator* found)
346 {
347         TreeModel::Row row = *iter;
348         string path = row[action_columns.path];
349
350         if (path == action_path) {
351                 *found = iter;
352                 return true;
353         }
354
355         return false;
356 }
357
358 Glib::RefPtr<Gtk::ListStore>
359 P2GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
360 {
361         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
362         TreeModel::Row row;
363
364         row = *store->append ();
365         row[midi_port_columns.full_name] = string();
366         row[midi_port_columns.short_name] = _("Disconnected");
367
368         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
369                 row = *store->append ();
370                 row[midi_port_columns.full_name] = *p;
371                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
372                 if (pn.empty ()) {
373                         pn = (*p).substr ((*p).find (':') + 1);
374                 }
375                 row[midi_port_columns.short_name] = pn;
376         }
377
378         return store;
379 }
380
381 void
382 P2GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
383 {
384         if (ignore_active_change) {
385                 return;
386         }
387
388         TreeModel::iterator active = combo->get_active ();
389         string new_port = (*active)[midi_port_columns.full_name];
390
391         if (new_port.empty()) {
392                 if (for_input) {
393                         p2.input_port()->disconnect_all ();
394                 } else {
395                         p2.output_port()->disconnect_all ();
396                 }
397
398                 return;
399         }
400
401         if (for_input) {
402                 if (!p2.input_port()->connected_to (new_port)) {
403                         p2.input_port()->disconnect_all ();
404                         p2.input_port()->connect (new_port);
405                 }
406         } else {
407                 if (!p2.output_port()->connected_to (new_port)) {
408                         p2.output_port()->disconnect_all ();
409                         p2.output_port()->connect (new_port);
410                 }
411         }
412 }
413
414 Glib::RefPtr<Gtk::ListStore>
415 P2GUI::build_pressure_mode_columns ()
416 {
417         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (pressure_mode_columns);
418         TreeModel::Row row;
419
420         row = *store->append();
421         row[pressure_mode_columns.name] = _("AfterTouch (Channel Pressure)");
422         row[pressure_mode_columns.mode] = Push2::AfterTouch;
423
424         row = *store->append();
425         row[pressure_mode_columns.name] = _("Polyphonic Pressure (Note Pressure)");
426         row[pressure_mode_columns.mode] = Push2::PolyPressure;
427
428         return store;
429 }
430
431 void
432 P2GUI::reprogram_pressure_mode ()
433 {
434         Gtk::TreeModel::iterator iter = pressure_mode_selector.get_active();
435         Push2::PressureMode pm;
436
437         if (iter) {
438                 Gtk::TreeModel::Row row = *iter;
439                 if (row) {
440                         pm = row[pressure_mode_columns.mode];
441                 } else {
442                         pm = Push2::AfterTouch;
443                 }
444         } else {
445                 pm = Push2::AfterTouch;
446         }
447
448         cerr << "Reprogram pm to " << pm << endl;
449         p2.set_pressure_mode (pm);
450 }