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