adaf2df94507f646aff0c6a02e0920696ddb8a9a
[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 (_cp.ipmidi_base(), 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         ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
145
146         vector<string> profiles;
147         
148         profiles.push_back ("default");
149
150         for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
151                 profiles.push_back (i->first);
152         }
153         Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
154         _profile_combo.set_active_text (p.device_profile().name());
155         _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::profile_combo_changed));
156
157         append_page (*table, _("Device Setup"));
158         table->show_all();
159
160         /* function key editor */
161
162         VBox* fkey_packer = manage (new VBox);
163         HBox* profile_packer = manage (new HBox);
164
165         l = manage (new Gtk::Label (_("Profile/Settings:")));
166         profile_packer->pack_start (*l, false, false);
167         profile_packer->pack_start (_profile_combo, true, true);
168         profile_packer->set_spacing (12);
169         profile_packer->set_border_width (12);
170
171         fkey_packer->pack_start (*profile_packer, false, false);
172         fkey_packer->pack_start (function_key_scroller, true, true);
173         fkey_packer->set_spacing (12);
174         function_key_scroller.set_size_request (700,700);
175         function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
176         function_key_scroller.add (function_key_editor);
177         append_page (*fkey_packer, _("Function Keys"));
178         
179         build_available_action_menu ();
180         build_function_key_editor ();
181         refresh_function_key_editor ();
182         fkey_packer->show_all();
183 }
184
185 CellRendererCombo*
186 MackieControlProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
187 {
188         CellRendererCombo* renderer = manage (new CellRendererCombo);
189         renderer->property_model() = model;
190         renderer->property_editable() = true;
191         renderer->property_text_column() = 0;
192         renderer->property_has_entry() = false;
193         renderer->signal_edited().connect (sigc::bind (sigc::mem_fun(*this, &MackieControlProtocolGUI::action_changed), column));
194
195         return renderer;
196 }
197
198 void
199 MackieControlProtocolGUI::build_available_action_menu ()
200 {
201         /* build a model of all available actions (needs to be tree structured
202          * more) 
203          */
204
205         available_action_model = TreeStore::create (available_action_columns);
206
207         vector<string> paths;
208         vector<string> labels;
209         vector<string> tooltips;
210         vector<string> keys;
211         vector<AccelKey> bindings;
212         typedef std::map<string,TreeIter> NodeMap;
213         NodeMap nodes;
214         NodeMap::iterator r;
215
216         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
217
218         vector<string>::iterator k;
219         vector<string>::iterator p;
220         vector<string>::iterator t;
221         vector<string>::iterator l;
222
223         available_action_model->clear ();
224
225         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
226
227                 TreeModel::Row row;
228                 vector<string> parts;
229
230                 parts.clear ();
231
232                 split (*p, parts, '/');
233
234                 if (parts.empty()) {
235                         continue;
236                 }
237
238                 //kinda kludgy way to avoid displaying menu items as mappable
239                 if ( parts[1] == _("Main_menu") )
240                         continue;
241                 if ( parts[1] == _("JACK") )
242                         continue;
243                 if ( parts[1] == _("redirectmenu") )
244                         continue;
245                 if ( parts[1] == _("Editor_menus") )
246                         continue;
247                 if ( parts[1] == _("RegionList") )
248                         continue;
249                 if ( parts[1] == _("ProcessorMenu") )
250                         continue;
251
252                 if ((r = nodes.find (parts[1])) == nodes.end()) {
253
254                         /* top level is missing */
255
256                         TreeIter rowp;
257                         TreeModel::Row parent;
258                         rowp = available_action_model->append();
259                         nodes[parts[1]] = rowp;
260                         parent = *(rowp);
261                         parent[available_action_columns.name] = parts[1];
262
263                         row = *(available_action_model->append (parent.children()));
264
265                 } else {
266
267                         row = *(available_action_model->append ((*r->second)->children()));
268
269                 }
270
271                 /* add this action */
272
273                 if (l->empty ()) {
274                         row[available_action_columns.name] = *t;
275                         action_map[*t] = *p;
276                 } else {
277                         row[available_action_columns.name] = *l;
278                         action_map[*l] = *p;
279                 }
280
281                 row[available_action_columns.path] = (*p);
282         }
283 }
284
285 void
286 MackieControlProtocolGUI::build_function_key_editor ()
287 {
288         function_key_editor.append_column (_("Key"), function_key_columns.name);
289
290         TreeViewColumn* col;
291         CellRendererCombo* renderer;
292
293         renderer = make_action_renderer (available_action_model, function_key_columns.plain);
294         col = manage (new TreeViewColumn (_("Plain"), *renderer));
295         col->add_attribute (renderer->property_text(), function_key_columns.plain);
296         function_key_editor.append_column (*col);
297         
298         renderer = make_action_renderer (available_action_model, function_key_columns.shift);
299         col = manage (new TreeViewColumn (_("Shift"), *renderer));
300         col->add_attribute (renderer->property_text(), function_key_columns.shift);
301         function_key_editor.append_column (*col);
302
303         renderer = make_action_renderer (available_action_model, function_key_columns.control);
304         col = manage (new TreeViewColumn (_("Control"), *renderer));
305         col->add_attribute (renderer->property_text(), function_key_columns.control);
306         function_key_editor.append_column (*col);
307
308         renderer = make_action_renderer (available_action_model, function_key_columns.option);
309         col = manage (new TreeViewColumn (_("Option"), *renderer));
310         col->add_attribute (renderer->property_text(), function_key_columns.option);
311         function_key_editor.append_column (*col);
312
313         renderer = make_action_renderer (available_action_model, function_key_columns.cmdalt);
314         col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
315         col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
316         function_key_editor.append_column (*col);
317
318         renderer = make_action_renderer (available_action_model, function_key_columns.shiftcontrol);
319         col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
320         col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
321         function_key_editor.append_column (*col);
322
323         function_key_model = ListStore::create (function_key_columns);
324         function_key_editor.set_model (function_key_model);
325 }
326
327 void
328 MackieControlProtocolGUI::refresh_function_key_editor ()
329 {
330         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
331         function_key_model->clear ();
332
333         /* now fill with data */
334
335         TreeModel::Row row;
336         DeviceProfile dp (_cp.device_profile());
337
338         for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
339
340                 Mackie::Button::ID bid = (Mackie::Button::ID) n;
341
342                 row = *(function_key_model->append());
343                 row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
344                 row[function_key_columns.id] = bid;
345
346                 Glib::RefPtr<Gtk::Action> act;
347                 string action;
348                 const string defstring = "\u2022";
349
350                 action = dp.get_button_action (bid, 0);
351                 if (action.empty()) {
352                         row[function_key_columns.plain] = defstring;
353                 } else {
354                         act = ActionManager::get_action (action.c_str());
355                         if (act) {
356                                 row[function_key_columns.plain] = act->get_label();
357                         } else {
358                                 row[function_key_columns.plain] = defstring;
359                         }
360                 }
361
362                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
363                 if (action.empty()) {
364                         row[function_key_columns.control] = defstring;
365                 } else {
366                         act = ActionManager::get_action (action.c_str());
367                         if (act) {
368                                 row[function_key_columns.control] = act->get_label();
369                         } else {
370                                 row[function_key_columns.control] = defstring;
371                         }
372                 }
373
374                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
375                 if (action.empty()) {
376                         row[function_key_columns.shift] = defstring;
377                 } else {
378                         act = ActionManager::get_action (action.c_str());
379                         if (act) {
380                                 row[function_key_columns.shift] = act->get_label();
381                         } else {
382                                 row[function_key_columns.shift] = defstring;
383                         }
384                 }
385
386                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_OPTION);
387                 if (action.empty()) {
388                         row[function_key_columns.option] = defstring;
389                 } else {
390                         act = ActionManager::get_action (action.c_str());
391                         if (act) {
392                                 row[function_key_columns.option] = act->get_label();
393                         } else {
394                                 row[function_key_columns.option] = defstring;
395                         }
396                 }
397
398                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CMDALT);
399                 if (action.empty()) {
400                         row[function_key_columns.cmdalt] = defstring;
401                 } else {
402                         act = ActionManager::get_action (action.c_str());
403                         if (act) {
404                                 row[function_key_columns.cmdalt] = act->get_label();
405                         } else {
406                                 row[function_key_columns.cmdalt] = defstring;
407                         }
408                 }
409
410                 action = dp.get_button_action (bid, (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL));
411                 if (action.empty()) {
412                         row[function_key_columns.shiftcontrol] = defstring;
413                 } else {
414                         act = ActionManager::get_action (action.c_str());
415                         if (act) {
416                                 row[function_key_columns.shiftcontrol] = act->get_label();
417                         } else {
418                                 row[function_key_columns.shiftcontrol] = defstring;
419                         }
420                 }
421         }
422
423         function_key_editor.set_model (function_key_model);
424 }
425
426 void 
427 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
428 {
429         Gtk::TreePath path(sPath);
430         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
431
432         if (row) {
433
434                 std::map<std::string,std::string>::iterator i = action_map.find (text);
435                 
436                 if (i == action_map.end()) {
437                         return;
438                 }
439
440                 cerr << "Changed to " << i->first << " aka " << i->second << endl;
441
442                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
443
444                 if (act) {
445                         /* update visible text, using string supplied by
446                            available action model so that it matches and is found
447                            within the model.
448                         */
449                         (*row).set_value (col.index(), text);
450
451                         /* update the current DeviceProfile, using the full
452                          * path
453                          */
454
455                         int modifier;
456
457                         switch (col.index()) {
458                         case 3:
459                                 modifier = MackieControlProtocol::MODIFIER_SHIFT;
460                                 break;
461                         case 4:
462                                 modifier = MackieControlProtocol::MODIFIER_CONTROL;
463                                 break;
464                         case 5:
465                                 modifier = MackieControlProtocol::MODIFIER_OPTION;
466                                 break;
467                         case 6:
468                                 modifier = MackieControlProtocol::MODIFIER_CMDALT;
469                                 break;
470                         case 7:
471                                 modifier = (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL);
472                                 break;
473                         default:
474                                 modifier = 0;
475                         }
476
477                         _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
478                 } else {
479                         std::cerr << "no such action\n";
480                 }
481         }
482 }
483
484 void
485 MackieControlProtocolGUI::surface_combo_changed ()
486 {
487         _cp.set_device (_surface_combo.get_active_text());
488
489         /* update ipMIDI field */
490
491         cerr << "New device called " << _cp.device_info().name() << " with ipMIDI ? " << _cp.device_info().uses_ipmidi() << endl;
492
493         ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
494 }
495
496 void
497 MackieControlProtocolGUI::profile_combo_changed ()
498 {
499         string profile = _profile_combo.get_active_text();
500
501         _cp.set_profile (profile);
502
503         refresh_function_key_editor ();
504 }
505
506 void
507 MackieControlProtocolGUI::ipmidi_spinner_changed ()
508 {
509         cerr << "Set IP MIDI base to " << ipmidi_base_port_spinner.get_value() << endl;
510         _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_spinner.get_value()));
511 }