6753f0a97c965b7dff6d8e25ce8172f81afb7f28
[ardour.git] / libs / surfaces / mackie / gui.cc
1 /*
2         Copyright (C) 2010 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 #include <gtkmm/comboboxtext.h>
20 #include <gtkmm/box.h>
21 #include <gtkmm/spinbutton.h>
22 #include <gtkmm/table.h>
23 #include <gtkmm/treeview.h>
24 #include <gtkmm/liststore.h>
25 #include <gtkmm/treestore.h>
26 #include <gtkmm/notebook.h>
27 #include <gtkmm/cellrenderercombo.h>
28 #include <gtkmm/scale.h>
29 #include <gtkmm/alignment.h>
30
31 #include "pbd/strsplit.h"
32
33 #include "gtkmm2ext/utils.h"
34 #include "gtkmm2ext/actions.h"
35
36 #include "ardour/rc_configuration.h"
37
38 #include "mackie_control_protocol.h"
39 #include "device_info.h"
40 #include "gui.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace Mackie;
46 using namespace Gtk;
47
48 void*
49 MackieControlProtocol::get_gui () const
50 {
51         if (!_gui) {
52                 const_cast<MackieControlProtocol*>(this)->build_gui ();
53         }
54         static_cast<Gtk::Notebook*>(_gui)->show_all();
55         return _gui;
56 }
57
58 void
59 MackieControlProtocol::tear_down_gui ()
60 {
61         if (_gui) {
62                 Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
63                 if (w) {
64                         w->hide();
65                         delete w;
66                 }
67         }
68         delete (MackieControlProtocolGUI*) _gui;
69         _gui = 0;
70 }
71
72 void
73 MackieControlProtocol::build_gui ()
74 {
75         _gui = (void *) new MackieControlProtocolGUI (*this);
76 }
77
78 MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p)
79         : _cp (p)
80         , touch_sensitivity_adjustment (0, 0, 9, 1, 4)
81         , touch_sensitivity_scale (touch_sensitivity_adjustment)
82         , recalibrate_fader_button (_("Recalibrate Faders"))
83         , ipmidi_base_port_adjustment (_cp.ipmidi_base(), 0, 32767, 1, 1000)
84         , ipmidi_base_port_spinner (ipmidi_base_port_adjustment)
85         , discover_button (_("Discover Mackie Devices"))
86 {
87         Gtk::Label* l;
88         Gtk::Alignment* align;
89
90         set_border_width (12);
91
92         Gtk::Table* table = Gtk::manage (new Gtk::Table (2, 9));
93         table->set_row_spacings (4);
94         table->set_col_spacings (6);
95         table->set_border_width (12);
96         l = manage (new Gtk::Label (_("Device Type:")));
97         l->set_alignment (1.0, 0.5);
98         table->attach (*l, 0, 1, 0, 1, AttachOptions(FILL|EXPAND), AttachOptions(0));
99         table->attach (_surface_combo, 1, 2, 0, 1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 20);
100
101         vector<string> surfaces;
102         
103         for (std::map<std::string,DeviceInfo>::iterator i = DeviceInfo::device_info.begin(); i != DeviceInfo::device_info.end(); ++i) {
104                 surfaces.push_back (i->first);
105         }
106         Gtkmm2ext::set_popdown_strings (_surface_combo, surfaces);
107         _surface_combo.set_active_text (p.device_info().name());
108         _surface_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::surface_combo_changed));
109
110         RadioButtonGroup rb_group = absolute_touch_mode_button.get_group();
111         touch_move_mode_button.set_group (rb_group);
112
113         l = manage (new Gtk::Label (_("Button click")));
114         l->set_alignment (1.0, 0.5);
115         table->attach (*l, 0, 1, 1, 2, AttachOptions(FILL|EXPAND), AttachOptions (0));
116         align = manage (new Alignment);
117         align->set (0.0, 0.5);
118         align->add (relay_click_button);
119         table->attach (*align, 1, 2, 1, 2, AttachOptions(FILL|EXPAND), AttachOptions (0));
120         l = manage (new Gtk::Label (_("Backlight")));
121         l->set_alignment (1.0, 0.5);
122         table->attach (*l, 0, 1, 2, 3, AttachOptions(FILL|EXPAND), AttachOptions (0));
123         align = manage (new Alignment);
124         align->set (0.0, 0.5);
125         align->add (backlight_button);
126         table->attach (*align, 1, 2, 2, 3, AttachOptions(FILL|EXPAND), AttachOptions (0));
127         l = manage (new Gtk::Label (_("Send Fader Position Only When Touched")));
128         l->set_alignment (1.0, 0.5);
129         table->attach (*l, 0, 1, 3, 4, AttachOptions(FILL|EXPAND), AttachOptions (0));
130         align = manage (new Alignment);
131         align->set (0.0, 0.5);
132         align->add (absolute_touch_mode_button);
133         table->attach (*align, 1, 2, 3, 4, AttachOptions(FILL|EXPAND), AttachOptions (0));
134         l = manage (new Gtk::Label (_("Send Fader Position When Moved")));
135         l->set_alignment (1.0, 0.5);
136         table->attach (*l, 0, 1, 4, 5, AttachOptions(FILL|EXPAND), AttachOptions (0));
137         align = manage (new Alignment);
138         align->set (0.0, 0.5);
139         align->add (touch_move_mode_button);
140         table->attach (*align, 1, 2, 4, 5, AttachOptions(FILL|EXPAND), AttachOptions (0));
141         l = manage (new Gtk::Label (_("Fader Touch Sense Sensitivity")));
142         l->set_alignment (1.0, 0.5);
143         table->attach (*l, 0, 1, 5, 6, AttachOptions(FILL|EXPAND), AttachOptions (0));
144         touch_sensitivity_scale.property_digits() = 0;
145         touch_sensitivity_scale.property_draw_value() = false;
146         table->attach (touch_sensitivity_scale, 1, 2, 5, 6, AttachOptions(FILL|EXPAND), AttachOptions (0));
147         table->attach (recalibrate_fader_button, 1, 2, 6, 7, AttachOptions(FILL|EXPAND), AttachOptions (0));
148
149         l = manage (new Gtk::Label (_("ipMIDI Port (lowest)")));
150         l->set_alignment (1.0, 0.5);
151         table->attach (*l, 0, 1, 7, 8, AttachOptions(FILL|EXPAND), AttachOptions (0));
152         table->attach (ipmidi_base_port_spinner, 1, 2, 7, 8, AttachOptions(FILL|EXPAND), AttachOptions (0));
153
154         ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
155         ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
156
157         
158         table->attach (discover_button, 1, 2, 8, 9, AttachOptions(FILL|EXPAND), AttachOptions (0));
159         discover_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::discover_clicked));
160
161         vector<string> profiles;
162         
163         profiles.push_back ("default");
164
165         for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
166                 profiles.push_back (i->first);
167         }
168         Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
169         _profile_combo.set_active_text (p.device_profile().name());
170         _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::profile_combo_changed));
171
172         append_page (*table, _("Device Setup"));
173         table->show_all();
174
175         /* function key editor */
176
177         VBox* fkey_packer = manage (new VBox);
178         HBox* profile_packer = manage (new HBox);
179         HBox* observation_packer = manage (new HBox);
180         
181         l = manage (new Gtk::Label (_("Profile/Settings:")));
182         profile_packer->pack_start (*l, false, false);
183         profile_packer->pack_start (_profile_combo, true, true);
184         profile_packer->set_spacing (12);
185         profile_packer->set_border_width (12);
186         
187         l = manage (new Gtk::Label (_("* Button available at the original Mackie MCU PRO or current device if enabled (NOT implemented yet). Device specific name presented.")));
188         observation_packer->pack_start (*l, false, false);
189
190         fkey_packer->pack_start (*profile_packer, false, false);
191         fkey_packer->pack_start (function_key_scroller, true, true);
192         fkey_packer->pack_start (*observation_packer, false, false);
193         fkey_packer->set_spacing (12);
194         function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
195         function_key_scroller.add (function_key_editor);
196         append_page (*fkey_packer, _("Function Keys"));
197
198         build_available_action_menu ();
199         build_function_key_editor ();
200         refresh_function_key_editor ();
201         fkey_packer->show_all();
202 }
203
204 CellRendererCombo*
205 MackieControlProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
206 {
207         CellRendererCombo* renderer = manage (new CellRendererCombo);
208         renderer->property_model() = model;
209         renderer->property_editable() = true;
210         renderer->property_text_column() = 0;
211         renderer->property_has_entry() = false;
212         renderer->signal_edited().connect (sigc::bind (sigc::mem_fun(*this, &MackieControlProtocolGUI::action_changed), column));
213
214         return renderer;
215 }
216
217 void
218 MackieControlProtocolGUI::build_available_action_menu ()
219 {
220         /* build a model of all available actions (needs to be tree structured
221          * more) 
222          */
223
224         available_action_model = TreeStore::create (available_action_columns);
225
226         vector<string> paths;
227         vector<string> labels;
228         vector<string> tooltips;
229         vector<string> keys;
230         vector<AccelKey> bindings;
231         typedef std::map<string,TreeIter> NodeMap;
232         NodeMap nodes;
233         NodeMap::iterator r;
234
235         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
236
237         vector<string>::iterator k;
238         vector<string>::iterator p;
239         vector<string>::iterator t;
240         vector<string>::iterator l;
241
242         available_action_model->clear ();
243
244         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
245
246                 TreeModel::Row row;
247                 vector<string> parts;
248
249                 parts.clear ();
250
251                 split (*p, parts, '/');
252
253                 if (parts.empty()) {
254                         continue;
255                 }
256
257                 //kinda kludgy way to avoid displaying menu items as mappable
258                 if ( parts[1] == _("Main_menu") )
259                         continue;
260                 if ( parts[1] == _("JACK") )
261                         continue;
262                 if ( parts[1] == _("redirectmenu") )
263                         continue;
264                 if ( parts[1] == _("Editor_menus") )
265                         continue;
266                 if ( parts[1] == _("RegionList") )
267                         continue;
268                 if ( parts[1] == _("ProcessorMenu") )
269                         continue;
270
271                 if ((r = nodes.find (parts[1])) == nodes.end()) {
272
273                         /* top level is missing */
274
275                         TreeIter rowp;
276                         TreeModel::Row parent;
277                         rowp = available_action_model->append();
278                         nodes[parts[1]] = rowp;
279                         parent = *(rowp);
280                         parent[available_action_columns.name] = parts[1];
281
282                         row = *(available_action_model->append (parent.children()));
283
284                 } else {
285
286                         row = *(available_action_model->append ((*r->second)->children()));
287
288                 }
289
290                 /* add this action */
291
292                 if (l->empty ()) {
293                         row[available_action_columns.name] = *t;
294                         action_map[*t] = *p;
295                 } else {
296                         row[available_action_columns.name] = *l;
297                         action_map[*l] = *p;
298                 }
299
300                 row[available_action_columns.path] = (*p);
301         }
302 }
303
304 void
305 MackieControlProtocolGUI::build_function_key_editor ()
306 {
307         function_key_editor.append_column (_("Key"), function_key_columns.name);
308
309         TreeViewColumn* col;
310         CellRendererCombo* renderer;
311
312         renderer = make_action_renderer (available_action_model, function_key_columns.plain);
313         col = manage (new TreeViewColumn (_("Plain"), *renderer));
314         col->add_attribute (renderer->property_text(), function_key_columns.plain);
315         function_key_editor.append_column (*col);
316         
317         renderer = make_action_renderer (available_action_model, function_key_columns.shift);
318         col = manage (new TreeViewColumn (_("Shift"), *renderer));
319         col->add_attribute (renderer->property_text(), function_key_columns.shift);
320         function_key_editor.append_column (*col);
321
322         renderer = make_action_renderer (available_action_model, function_key_columns.control);
323         col = manage (new TreeViewColumn (_("Control"), *renderer));
324         col->add_attribute (renderer->property_text(), function_key_columns.control);
325         function_key_editor.append_column (*col);
326
327         renderer = make_action_renderer (available_action_model, function_key_columns.option);
328         col = manage (new TreeViewColumn (_("Option"), *renderer));
329         col->add_attribute (renderer->property_text(), function_key_columns.option);
330         function_key_editor.append_column (*col);
331
332         renderer = make_action_renderer (available_action_model, function_key_columns.cmdalt);
333         col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
334         col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
335         function_key_editor.append_column (*col);
336
337         renderer = make_action_renderer (available_action_model, function_key_columns.shiftcontrol);
338         col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
339         col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
340         function_key_editor.append_column (*col);
341
342         function_key_model = ListStore::create (function_key_columns);
343         function_key_editor.set_model (function_key_model);
344 }
345
346 void
347 MackieControlProtocolGUI::refresh_function_key_editor ()
348 {
349         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
350         function_key_model->clear ();
351
352         /* now fill with data */
353
354         TreeModel::Row row;
355         DeviceProfile dp (_cp.device_profile());
356         DeviceInfo di;
357
358         for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
359
360                 Mackie::Button::ID bid = (Mackie::Button::ID) n;
361
362                 row = *(function_key_model->append());
363                 if (di.global_buttons().find (bid) == di.global_buttons().end()) {
364                         row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
365                 } else {
366                         row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
367                 }
368                 row[function_key_columns.id] = bid;
369
370                 Glib::RefPtr<Gtk::Action> act;
371                 string action;
372                 const string defstring = "\u2022";
373
374                 action = dp.get_button_action (bid, 0);
375                 if (action.empty()) {
376                         row[function_key_columns.plain] = defstring;
377                 } else {
378                         act = ActionManager::get_action (action.c_str());
379                         if (act) {
380                                 row[function_key_columns.plain] = act->get_label();
381                         } else {
382                                 row[function_key_columns.plain] = defstring;
383                         }
384                 }
385
386                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
387                 if (action.empty()) {
388                         row[function_key_columns.control] = defstring;
389                 } else {
390                         act = ActionManager::get_action (action.c_str());
391                         if (act) {
392                                 row[function_key_columns.control] = act->get_label();
393                         } else {
394                                 row[function_key_columns.control] = defstring;
395                         }
396                 }
397
398                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
399                 if (action.empty()) {
400                         row[function_key_columns.shift] = defstring;
401                 } else {
402                         act = ActionManager::get_action (action.c_str());
403                         if (act) {
404                                 row[function_key_columns.shift] = act->get_label();
405                         } else {
406                                 row[function_key_columns.shift] = defstring;
407                         }
408                 }
409
410                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_OPTION);
411                 if (action.empty()) {
412                         row[function_key_columns.option] = defstring;
413                 } else {
414                         act = ActionManager::get_action (action.c_str());
415                         if (act) {
416                                 row[function_key_columns.option] = act->get_label();
417                         } else {
418                                 row[function_key_columns.option] = defstring;
419                         }
420                 }
421
422                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CMDALT);
423                 if (action.empty()) {
424                         row[function_key_columns.cmdalt] = defstring;
425                 } else {
426                         act = ActionManager::get_action (action.c_str());
427                         if (act) {
428                                 row[function_key_columns.cmdalt] = act->get_label();
429                         } else {
430                                 row[function_key_columns.cmdalt] = defstring;
431                         }
432                 }
433
434                 action = dp.get_button_action (bid, (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL));
435                 if (action.empty()) {
436                         row[function_key_columns.shiftcontrol] = defstring;
437                 } else {
438                         act = ActionManager::get_action (action.c_str());
439                         if (act) {
440                                 row[function_key_columns.shiftcontrol] = act->get_label();
441                         } else {
442                                 row[function_key_columns.shiftcontrol] = defstring;
443                         }
444                 }
445         }
446
447         function_key_editor.set_model (function_key_model);
448 }
449
450 void 
451 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
452 {
453         Gtk::TreePath path(sPath);
454         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
455
456         if (row) {
457
458                 std::map<std::string,std::string>::iterator i = action_map.find (text);
459                 
460                 if (i == action_map.end()) {
461                         return;
462                 }
463
464                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
465
466                 if (act) {
467                         /* update visible text, using string supplied by
468                            available action model so that it matches and is found
469                            within the model.
470                         */
471                         (*row).set_value (col.index(), text);
472
473                         /* update the current DeviceProfile, using the full
474                          * path
475                          */
476
477                         int modifier;
478
479                         switch (col.index()) {
480                         case 3:
481                                 modifier = MackieControlProtocol::MODIFIER_SHIFT;
482                                 break;
483                         case 4:
484                                 modifier = MackieControlProtocol::MODIFIER_CONTROL;
485                                 break;
486                         case 5:
487                                 modifier = MackieControlProtocol::MODIFIER_OPTION;
488                                 break;
489                         case 6:
490                                 modifier = MackieControlProtocol::MODIFIER_CMDALT;
491                                 break;
492                         case 7:
493                                 modifier = (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL);
494                                 break;
495                         default:
496                                 modifier = 0;
497                         }
498
499                         _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
500                 } else {
501                         std::cerr << "no such action\n";
502                 }
503         }
504 }
505
506 void
507 MackieControlProtocolGUI::surface_combo_changed ()
508 {
509         _cp.set_device (_surface_combo.get_active_text());
510
511         /* update ipMIDI field */
512
513         ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
514 }
515
516 void
517 MackieControlProtocolGUI::profile_combo_changed ()
518 {
519         string profile = _profile_combo.get_active_text();
520
521         _cp.set_profile (profile);
522
523         refresh_function_key_editor ();
524 }
525
526 void
527 MackieControlProtocolGUI::ipmidi_spinner_changed ()
528 {
529         _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_spinner.get_value()));
530 }
531
532 void
533 MackieControlProtocolGUI::discover_clicked ()
534 {
535         /* this should help to get things started */
536         _cp.midi_connectivity_established ();
537 }