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