use ActionManager namespace, rather than ActionMap objects
[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/error.h"
32 #include "pbd/unwind.h"
33 #include "pbd/strsplit.h"
34 #include "pbd/stacktrace.h"
35
36 #include "gtkmm2ext/actions.h"
37 #include "gtkmm2ext/bindings.h"
38 #include "gtkmm2ext/gui_thread.h"
39 #include "gtkmm2ext/utils.h"
40
41 #include "ardour/audioengine.h"
42 #include "ardour/port.h"
43 #include "ardour/rc_configuration.h"
44
45 #include "mackie_control_protocol.h"
46 #include "device_info.h"
47 #include "gui.h"
48 #include "surface.h"
49 #include "surface_port.h"
50
51 #include "pbd/i18n.h"
52
53 using namespace std;
54 using namespace Gtk;
55 using namespace ArdourSurface;
56 using namespace Mackie;
57
58 void*
59 MackieControlProtocol::get_gui () const
60 {
61         if (!_gui) {
62                 const_cast<MackieControlProtocol*>(this)->build_gui ();
63         }
64         static_cast<Gtk::Notebook*>(_gui)->show_all();
65         return _gui;
66 }
67
68 void
69 MackieControlProtocol::tear_down_gui ()
70 {
71         if (_gui) {
72                 Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
73                 if (w) {
74                         w->hide();
75                         delete w;
76                 }
77         }
78         delete (MackieControlProtocolGUI*) _gui;
79         _gui = 0;
80 }
81
82 void
83 MackieControlProtocol::build_gui ()
84 {
85         _gui = (void *) new MackieControlProtocolGUI (*this);
86 }
87
88 MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p)
89         : _cp (p)
90         , table (2, 9)
91         , touch_sensitivity_adjustment (0, 0, 9, 1, 4)
92         , touch_sensitivity_scale (touch_sensitivity_adjustment)
93         , recalibrate_fader_button (_("Recalibrate Faders"))
94         , ipmidi_base_port_adjustment (_cp.ipmidi_base(), 0, 32767, 1, 1000)
95         , discover_button (_("Discover Mackie Devices"))
96         , _device_dependent_widget (0)
97         , _ignore_profile_changed (false)
98         , ignore_active_change (false)
99 {
100         Gtk::Label* l;
101         Gtk::Alignment* align;
102         int row = 0;
103
104         set_border_width (12);
105
106         table.set_row_spacings (4);
107         table.set_col_spacings (6);
108         table.set_border_width (12);
109         table.set_homogeneous (false);
110
111         l = manage (new Gtk::Label (_("Device Type:")));
112         l->set_alignment (1.0, 0.5);
113         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
114         table.attach (_surface_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
115         row++;
116
117         vector<string> surfaces;
118
119         for (std::map<std::string,DeviceInfo>::iterator i = DeviceInfo::device_info.begin(); i != DeviceInfo::device_info.end(); ++i) {
120                 surfaces.push_back (i->first);
121         }
122         Gtkmm2ext::set_popdown_strings (_surface_combo, surfaces);
123         _surface_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::surface_combo_changed));
124
125         _cp.DeviceChanged.connect (device_change_connection, invalidator (*this), boost::bind (&MackieControlProtocolGUI::device_changed, this), gui_context());
126         _cp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&MackieControlProtocolGUI::connection_handler, this), gui_context());
127
128         ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
129
130         /* device-dependent part */
131
132         device_dependent_row = row;
133
134         if (_device_dependent_widget) {
135                 table.remove (*_device_dependent_widget);
136                 _device_dependent_widget = 0;
137         }
138
139         _device_dependent_widget = device_dependent_widget ();
140         table.attach (*_device_dependent_widget, 0, 12, row, row+1, AttachOptions(0), AttachOptions(0), 0, 0);
141         row++;
142
143         /* back to the boilerplate */
144
145         RadioButtonGroup rb_group = absolute_touch_mode_button.get_group();
146         touch_move_mode_button.set_group (rb_group);
147
148         recalibrate_fader_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::recalibrate_faders));
149         backlight_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::toggle_backlight));
150
151         touch_sensitivity_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::touch_sensitive_change));
152         touch_sensitivity_scale.set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
153
154         l = manage (new Gtk::Label (_("Button click")));
155         l->set_alignment (1.0, 0.5);
156         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
157         align = manage (new Alignment);
158         align->set (0.0, 0.5);
159         align->add (relay_click_button);
160         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
161         row++;
162
163         l = manage (new Gtk::Label (_("Backlight")));
164         l->set_alignment (1.0, 0.5);
165         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
166         align = manage (new Alignment);
167         align->set (0.0, 0.5);
168         align->add (backlight_button);
169         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
170         row++;
171
172         l = manage (new Gtk::Label (_("Send Fader Position Only When Touched")));
173         l->set_alignment (1.0, 0.5);
174         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
175         align = manage (new Alignment);
176         align->set (0.0, 0.5);
177         align->add (absolute_touch_mode_button);
178         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
179         row++;
180
181         l = manage (new Gtk::Label (_("Send Fader Position When Moved")));
182         l->set_alignment (1.0, 0.5);
183         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
184         align = manage (new Alignment);
185         align->set (0.0, 0.5);
186         align->add (touch_move_mode_button);
187         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
188         row++;
189
190         l = manage (new Gtk::Label (_("Fader Touch Sense Sensitivity")));
191         l->set_alignment (1.0, 0.5);
192         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
193         touch_sensitivity_scale.property_digits() = 0;
194         touch_sensitivity_scale.property_draw_value() = false;
195         table.attach (touch_sensitivity_scale, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
196         row++;
197         table.attach (recalibrate_fader_button, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
198         row++;
199
200
201         table.attach (discover_button, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
202         discover_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::discover_clicked));
203         row++;
204
205         vector<string> profiles;
206
207         for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
208                 cerr << "add discovered profile " << i->first << endl;
209                 profiles.push_back (i->first);
210         }
211         Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
212         cerr << "set active profile from " << p.device_profile().name() << endl;
213         _profile_combo.set_active_text (p.device_profile().name());
214         _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::profile_combo_changed));
215
216         append_page (table, _("Device Setup"));
217         table.show_all();
218
219         /* function key editor */
220
221         VBox* fkey_packer = manage (new VBox);
222         HBox* profile_packer = manage (new HBox);
223         HBox* observation_packer = manage (new HBox);
224
225         l = manage (new Gtk::Label (_("Profile/Settings:")));
226         profile_packer->pack_start (*l, false, false);
227         profile_packer->pack_start (_profile_combo, true, true);
228         profile_packer->set_spacing (12);
229         profile_packer->set_border_width (12);
230
231         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.")));
232         observation_packer->pack_start (*l, false, false);
233
234         fkey_packer->pack_start (*profile_packer, false, false);
235         fkey_packer->pack_start (function_key_scroller, true, true);
236         fkey_packer->pack_start (*observation_packer, false, false);
237         fkey_packer->set_spacing (12);
238         function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
239         function_key_scroller.add (function_key_editor);
240         append_page (*fkey_packer, _("Function Keys"));
241
242         build_available_action_menu ();
243         build_function_key_editor ();
244         refresh_function_key_editor ();
245         fkey_packer->show_all();
246 }
247
248 void
249 MackieControlProtocolGUI::connection_handler ()
250 {
251         /* ignore all changes to combobox active strings here, because we're
252            updating them to match a new ("external") reality - we were called
253            because port connections have changed.
254         */
255
256         PBD::Unwinder<bool> ici (ignore_active_change, true);
257
258         vector<Gtk::ComboBox*>::iterator ic;
259         vector<Gtk::ComboBox*>::iterator oc;
260
261         vector<string> midi_inputs;
262         vector<string> midi_outputs;
263
264         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
265         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
266
267         for (ic = input_combos.begin(), oc = output_combos.begin(); ic != input_combos.end() && oc != output_combos.end(); ++ic, ++oc) {
268
269                 boost::shared_ptr<Surface> surface = _cp.get_surface_by_raw_pointer ((*ic)->get_data ("surface"));
270
271                 if (surface) {
272                         update_port_combos (midi_inputs, midi_outputs, *ic, *oc, surface);
273                 }
274         }
275 }
276
277 void
278 MackieControlProtocolGUI::update_port_combos (vector<string> const& midi_inputs, vector<string> const& midi_outputs,
279                                               Gtk::ComboBox* input_combo,
280                                               Gtk::ComboBox* output_combo,
281                                               boost::shared_ptr<Surface> surface)
282 {
283         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
284         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
285         bool input_found = false;
286         bool output_found = false;
287         int n;
288
289         input_combo->set_model (input);
290         output_combo->set_model (output);
291
292         Gtk::TreeModel::Children children = input->children();
293         Gtk::TreeModel::Children::iterator i;
294         i = children.begin();
295         ++i; /* skip "Disconnected" */
296
297
298         for (n = 1;  i != children.end(); ++i, ++n) {
299                 string port_name = (*i)[midi_port_columns.full_name];
300                 if (surface->port().input().connected_to (port_name)) {
301                         input_combo->set_active (n);
302                         input_found = true;
303                         break;
304                 }
305         }
306
307         if (!input_found) {
308                 input_combo->set_active (0); /* disconnected */
309         }
310
311         children = output->children();
312         i = children.begin();
313         ++i; /* skip "Disconnected" */
314
315         for (n = 1;  i != children.end(); ++i, ++n) {
316                 string port_name = (*i)[midi_port_columns.full_name];
317                 if (surface->port().output().connected_to (port_name)) {
318                         output_combo->set_active (n);
319                         output_found = true;
320                         break;
321                 }
322         }
323
324         if (!output_found) {
325                 output_combo->set_active (0); /* disconnected */
326         }
327 }
328
329 Gtk::Widget*
330 MackieControlProtocolGUI::device_dependent_widget ()
331 {
332         Gtk::Table* dd_table;
333         Gtk::Label* l;
334         int row = 0;
335
336         uint32_t n_surfaces = 1 + _cp.device_info().extenders();
337         uint32_t main_pos = _cp.device_info().master_position();
338
339         if (!_cp.device_info().uses_ipmidi()) {
340                 dd_table = Gtk::manage (new Gtk::Table (n_surfaces, 2));
341         } else {
342                 dd_table = Gtk::manage (new Gtk::Table (1, 2));
343         }
344
345         dd_table = Gtk::manage (new Gtk::Table (2, n_surfaces));
346         dd_table->set_row_spacings (4);
347         dd_table->set_col_spacings (6);
348         dd_table->set_border_width (12);
349
350         _surface_combo.set_active_text (_cp.device_info().name());
351
352         vector<string> midi_inputs;
353         vector<string> midi_outputs;
354
355         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsPhysical), midi_inputs);
356         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsPhysical), midi_outputs);
357
358         input_combos.clear ();
359         output_combos.clear ();
360
361         if (!_cp.device_info().uses_ipmidi()) {
362
363                 for (uint32_t n = 0; n < n_surfaces; ++n) {
364
365                         boost::shared_ptr<Surface> surface = _cp.nth_surface (n);
366
367                         if (!surface) {
368                                 PBD::fatal << string_compose (_("programming error: %1\n"), string_compose ("n=%1 surface not found!", n)) << endmsg;
369                                 /*NOTREACHED*/
370                         }
371
372                         Gtk::ComboBox* input_combo = manage (new Gtk::ComboBox);
373                         Gtk::ComboBox* output_combo = manage (new Gtk::ComboBox);
374
375                         update_port_combos (midi_inputs, midi_outputs, input_combo, output_combo, surface);
376
377                         input_combo->pack_start (midi_port_columns.short_name);
378                         input_combo->set_data ("surface", surface.get());
379                         input_combos.push_back (input_combo);
380                         output_combo->pack_start (midi_port_columns.short_name);
381                         output_combo->set_data ("surface", surface.get());
382                         output_combos.push_back (output_combo);
383
384                         boost::weak_ptr<Surface> ws (surface);
385                         input_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &MackieControlProtocolGUI::active_port_changed), input_combo, ws, true));
386                         output_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &MackieControlProtocolGUI::active_port_changed), output_combo, ws, false));
387
388                         string send_string;
389                         string receive_string;
390
391                         if (n_surfaces > 1) {
392                                 if (n == main_pos) {
393                                         send_string = string_compose(_("Main surface at position %1 sends via:"), n + 1);
394                                         receive_string = string_compose(_("Main surface at position %1 receives via:"), n + 1);
395                                 } else {
396                                         send_string = string_compose (_("Extender at position %1 sends via:"), n + 1);
397                                         receive_string = string_compose (_("Extender at position %1 receives via:"), n + 1);
398                                 }
399                         } else {
400                                 send_string = _("Surface sends via:");
401                                 receive_string = _("Surface receives via:");
402                         }
403
404                         l = manage (new Gtk::Label (send_string));
405                         l->set_alignment (1.0, 0.5);
406                         dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
407                         dd_table->attach (*input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
408                         row++;
409
410                         l = manage (new Gtk::Label (receive_string));
411                         l->set_alignment (1.0, 0.5);
412                         dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
413                         dd_table->attach (*output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
414                         row++;
415                 }
416
417         } else {
418
419                 l = manage (new Gtk::Label (_("ipMIDI Port (lowest)")));
420                 l->set_alignment (1.0, 0.5);
421
422                 Gtk::SpinButton*  ipmidi_base_port_spinner = manage (new Gtk::SpinButton (ipmidi_base_port_adjustment));
423                 dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
424                 dd_table->attach (*ipmidi_base_port_spinner, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
425                 row++;
426         }
427
428         return dd_table;
429 }
430
431 CellRendererCombo*
432 MackieControlProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
433 {
434         CellRendererCombo* renderer = manage (new CellRendererCombo);
435         renderer->property_model() = model;
436         renderer->property_editable() = true;
437         renderer->property_text_column() = 0;
438         renderer->property_has_entry() = false;
439         renderer->signal_edited().connect (sigc::bind (sigc::mem_fun(*this, &MackieControlProtocolGUI::action_changed), column));
440
441         return renderer;
442 }
443
444 void
445 MackieControlProtocolGUI::build_available_action_menu ()
446 {
447         /* build a model of all available actions (needs to be tree structured
448          * more)
449          */
450
451         available_action_model = TreeStore::create (available_action_columns);
452
453         vector<string> paths;
454         vector<string> labels;
455         vector<string> tooltips;
456         vector<string> keys;
457         vector<Glib::RefPtr<Gtk::Action> > actions;
458
459         typedef std::map<string,TreeIter> NodeMap;
460         NodeMap nodes;
461         NodeMap::iterator r;
462
463         ActionManager::get_all_actions (paths, labels, tooltips, keys, actions);
464
465         vector<string>::iterator k;
466         vector<string>::iterator p;
467         vector<string>::iterator t;
468         vector<string>::iterator l;
469
470         available_action_model->clear ();
471
472         /* Because there are button bindings built in that are not
473            in the key binding map, there needs to be a way to undo
474            a profile edit.
475         */
476         TreeIter rowp;
477         TreeModel::Row parent;
478         rowp = available_action_model->append();
479         parent = *(rowp);
480         parent[available_action_columns.name] = _("Remove Binding");
481
482         /* Key aliasing */
483
484         rowp = available_action_model->append();
485         parent = *(rowp);
486         parent[available_action_columns.name] = _("Shift");
487         rowp = available_action_model->append();
488         parent = *(rowp);
489         parent[available_action_columns.name] = _("Control");
490         rowp = available_action_model->append();
491         parent = *(rowp);
492         parent[available_action_columns.name] = _("Option");
493         rowp = available_action_model->append();
494         parent = *(rowp);
495         parent[available_action_columns.name] = _("CmdAlt");
496
497         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
498
499                 TreeModel::Row row;
500                 vector<string> parts;
501
502                 parts.clear ();
503
504                 split (*p, parts, '/');
505
506                 if (parts.empty()) {
507                         continue;
508                 }
509
510                 //kinda kludgy way to avoid displaying menu items as mappable
511                 if ( parts[1] == _("Main_menu") )
512                         continue;
513                 if ( parts[1] == _("JACK") )
514                         continue;
515                 if ( parts[1] == _("redirectmenu") )
516                         continue;
517                 if ( parts[1] == _("Editor_menus") )
518                         continue;
519                 if ( parts[1] == _("RegionList") )
520                         continue;
521                 if ( parts[1] == _("ProcessorMenu") )
522                         continue;
523
524                 if ((r = nodes.find (parts[1])) == nodes.end()) {
525
526                         /* top level is missing */
527
528                         TreeIter rowp;
529                         TreeModel::Row parent;
530                         rowp = available_action_model->append();
531                         nodes[parts[1]] = rowp;
532                         parent = *(rowp);
533                         parent[available_action_columns.name] = parts[1];
534
535                         row = *(available_action_model->append (parent.children()));
536
537                 } else {
538
539                         row = *(available_action_model->append ((*r->second)->children()));
540
541                 }
542
543                 /* add this action */
544
545                 if (l->empty ()) {
546                         row[available_action_columns.name] = *t;
547                         action_map[*t] = *p;
548                 } else {
549                         row[available_action_columns.name] = *l;
550                         action_map[*l] = *p;
551                 }
552
553                 row[available_action_columns.path] = (*p);
554         }
555 }
556
557 void
558 MackieControlProtocolGUI::build_function_key_editor ()
559 {
560         function_key_editor.append_column (_("Key"), function_key_columns.name);
561
562         TreeViewColumn* col;
563         CellRendererCombo* renderer;
564
565         renderer = make_action_renderer (available_action_model, function_key_columns.plain);
566         col = manage (new TreeViewColumn (_("Plain"), *renderer));
567         col->add_attribute (renderer->property_text(), function_key_columns.plain);
568         function_key_editor.append_column (*col);
569
570         renderer = make_action_renderer (available_action_model, function_key_columns.shift);
571         col = manage (new TreeViewColumn (_("Shift"), *renderer));
572         col->add_attribute (renderer->property_text(), function_key_columns.shift);
573         function_key_editor.append_column (*col);
574
575         renderer = make_action_renderer (available_action_model, function_key_columns.control);
576         col = manage (new TreeViewColumn (_("Control"), *renderer));
577         col->add_attribute (renderer->property_text(), function_key_columns.control);
578         function_key_editor.append_column (*col);
579
580         renderer = make_action_renderer (available_action_model, function_key_columns.option);
581         col = manage (new TreeViewColumn (_("Option"), *renderer));
582         col->add_attribute (renderer->property_text(), function_key_columns.option);
583         function_key_editor.append_column (*col);
584
585         renderer = make_action_renderer (available_action_model, function_key_columns.cmdalt);
586         col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
587         col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
588         function_key_editor.append_column (*col);
589
590         renderer = make_action_renderer (available_action_model, function_key_columns.shiftcontrol);
591         col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
592         col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
593         function_key_editor.append_column (*col);
594
595         function_key_model = ListStore::create (function_key_columns);
596         function_key_editor.set_model (function_key_model);
597 }
598
599 void
600 MackieControlProtocolGUI::refresh_function_key_editor ()
601 {
602         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
603         function_key_model->clear ();
604
605         /* now fill with data */
606
607         TreeModel::Row row;
608         DeviceProfile dp (_cp.device_profile());
609         DeviceInfo di;
610
611         for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
612
613                 Mackie::Button::ID bid = (Mackie::Button::ID) n;
614
615                 row = *(function_key_model->append());
616                 if (di.global_buttons().find (bid) == di.global_buttons().end()) {
617                         row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
618                 } else {
619                         row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
620                 }
621                 row[function_key_columns.id] = bid;
622
623                 Glib::RefPtr<Gtk::Action> act;
624                 string action;
625                 const string defstring = "\u2022";
626
627                 /* We only allow plain bindings for Fn keys. All others are
628                  * reserved for hard-coded actions.
629                  */
630
631                 if (bid >= Mackie::Button::F1 && bid <= Mackie::Button::F8) {
632
633                         action = dp.get_button_action (bid, 0);
634                         if (action.empty()) {
635                                 row[function_key_columns.plain] = defstring;
636                         } else {
637                                 if (action.find ('/') == string::npos) {
638                                         /* Probably a key alias */
639                                         row[function_key_columns.plain] = action;
640                                 } else {
641
642                                         act = ActionManager::get_action (action.c_str());
643                                         if (act) {
644                                                 row[function_key_columns.plain] = act->get_label();
645                                         } else {
646                                                 row[function_key_columns.plain] = defstring;
647                                         }
648                                 }
649                         }
650                 }
651
652                 /* We only allow plain bindings for Fn keys. All others are
653                  * reserved for hard-coded actions.
654                  */
655
656                 if (bid >= Mackie::Button::F1 && bid <= Mackie::Button::F8) {
657
658                         action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
659                         if (action.empty()) {
660                                 row[function_key_columns.shift] = defstring;
661                         } else {
662                                 if (action.find ('/') == string::npos) {
663                                         /* Probably a key alias */
664                                         row[function_key_columns.shift] = action;
665                                 } else {
666                                         act = ActionManager::get_action (action.c_str());
667                                         if (act) {
668                                                 row[function_key_columns.shift] = act->get_label();
669                                         } else {
670                                                 row[function_key_columns.shift] = defstring;
671                                         }
672                                 }
673                         }
674                 }
675
676                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
677                 if (action.empty()) {
678                         row[function_key_columns.control] = defstring;
679                 } else {
680                         if (action.find ('/') == string::npos) {
681                                 /* Probably a key alias */
682                                 row[function_key_columns.control] = action;
683                         } else {
684                                 act = ActionManager::get_action (action.c_str());
685                                 if (act) {
686                                         row[function_key_columns.control] = act->get_label();
687                                 } else {
688                                         row[function_key_columns.control] = defstring;
689                                 }
690                         }
691                 }
692
693                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_OPTION);
694                 if (action.empty()) {
695                         row[function_key_columns.option] = defstring;
696                 } else {
697                         if (action.find ('/') == string::npos) {
698                                 /* Probably a key alias */
699                                 row[function_key_columns.option] = action;
700                         } else {
701                                 act = ActionManager::get_action (action.c_str());
702                                 if (act) {
703                                         row[function_key_columns.option] = act->get_label();
704                                 } else {
705                                         row[function_key_columns.option] = defstring;
706                                 }
707                         }
708                 }
709
710                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CMDALT);
711                 if (action.empty()) {
712                         row[function_key_columns.cmdalt] = defstring;
713                 } else {
714                         if (action.find ('/') == string::npos) {
715                                 /* Probably a key alias */
716                                 row[function_key_columns.cmdalt] = action;
717                         } else {
718                                 act = ActionManager::get_action (action.c_str());
719                                 if (act) {
720                                         row[function_key_columns.cmdalt] = act->get_label();
721                                 } else {
722                                         row[function_key_columns.cmdalt] = defstring;
723                                 }
724                         }
725                 }
726
727                 action = dp.get_button_action (bid, (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL));
728                 if (action.empty()) {
729                         row[function_key_columns.shiftcontrol] = defstring;
730                 } else {
731                         act = ActionManager::get_action (action.c_str());
732                         if (act) {
733                                 row[function_key_columns.shiftcontrol] = act->get_label();
734                         } else {
735                                 row[function_key_columns.shiftcontrol] = defstring;
736                         }
737                 }
738         }
739
740         function_key_editor.set_model (function_key_model);
741 }
742
743 void
744 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
745 {
746         // Remove Binding is not in the action map but still valid
747         bool remove (false);
748         if ( text == "Remove Binding") {
749                 remove = true;
750         }
751         Gtk::TreePath path(sPath);
752         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
753
754         if (row) {
755
756                 std::map<std::string,std::string>::iterator i = action_map.find (text);
757
758                 if (i == action_map.end()) {
759                         if (!remove) {
760                                 return;
761                         }
762                 }
763                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
764
765                 if (act || remove) {
766                         /* update visible text, using string supplied by
767                            available action model so that it matches and is found
768                            within the model.
769                         */
770                         if (remove) {
771                                 Glib::ustring dot = "\u2022";
772                                 (*row).set_value (col.index(), dot);
773                         } else {
774                                 (*row).set_value (col.index(), text);
775                         }
776
777                         /* update the current DeviceProfile, using the full
778                          * path
779                          */
780
781                         int modifier;
782
783                         switch (col.index()) {
784                         case 3:
785                                 modifier = MackieControlProtocol::MODIFIER_SHIFT;
786                                 break;
787                         case 4:
788                                 modifier = MackieControlProtocol::MODIFIER_CONTROL;
789                                 break;
790                         case 5:
791                                 modifier = MackieControlProtocol::MODIFIER_OPTION;
792                                 break;
793                         case 6:
794                                 modifier = MackieControlProtocol::MODIFIER_CMDALT;
795                                 break;
796                         case 7:
797                                 modifier = (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL);
798                                 break;
799                         default:
800                                 modifier = 0;
801                         }
802
803                         if (remove) {
804                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
805                         } else {
806                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
807                         }
808
809                         _ignore_profile_changed = true;
810                         _profile_combo.set_active_text ( _cp.device_profile().name() );
811                         _ignore_profile_changed = false;
812
813                 } else {
814                         std::cerr << "no such action\n";
815                 }
816         }
817 }
818
819 void
820 MackieControlProtocolGUI::surface_combo_changed ()
821 {
822         _cp.set_device (_surface_combo.get_active_text(), false);
823 }
824
825 void
826 MackieControlProtocolGUI::device_changed ()
827 {
828         if (_device_dependent_widget) {
829                 table.remove (*_device_dependent_widget);
830                 _device_dependent_widget = 0;
831         }
832
833         _device_dependent_widget = device_dependent_widget ();
834         _device_dependent_widget->show_all ();
835
836         table.attach (*_device_dependent_widget, 0, 12, device_dependent_row, device_dependent_row+1, AttachOptions(0), AttachOptions(0), 0, 0);
837 }
838
839 void
840 MackieControlProtocolGUI::profile_combo_changed ()
841 {
842         if (!_ignore_profile_changed) {
843                 string profile = _profile_combo.get_active_text();
844
845                 _cp.set_profile (profile);
846
847                 refresh_function_key_editor ();
848         }
849 }
850
851 void
852 MackieControlProtocolGUI::ipmidi_spinner_changed ()
853 {
854         _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_adjustment.get_value()));
855 }
856
857 void
858 MackieControlProtocolGUI::discover_clicked ()
859 {
860         /* this should help to get things started */
861         _cp.ping_devices ();
862 }
863
864 void
865 MackieControlProtocolGUI::recalibrate_faders ()
866 {
867         _cp.recalibrate_faders ();
868 }
869
870 void
871 MackieControlProtocolGUI::toggle_backlight ()
872 {
873         _cp.toggle_backlight ();
874 }
875
876 void
877 MackieControlProtocolGUI::touch_sensitive_change ()
878 {
879         int sensitivity = (int) touch_sensitivity_adjustment.get_value ();
880         _cp.set_touch_sensitivity (sensitivity);
881 }
882
883 Glib::RefPtr<Gtk::ListStore>
884 MackieControlProtocolGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
885 {
886         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
887         TreeModel::Row row;
888
889         row = *store->append ();
890         row[midi_port_columns.full_name] = string();
891         row[midi_port_columns.short_name] = _("Disconnected");
892
893         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
894                 row = *store->append ();
895                 row[midi_port_columns.full_name] = *p;
896                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
897                 if (pn.empty ()) {
898                         pn = (*p).substr ((*p).find (':') + 1);
899                 }
900                 row[midi_port_columns.short_name] = pn;
901         }
902
903         return store;
904 }
905
906 void
907 MackieControlProtocolGUI::active_port_changed (Gtk::ComboBox* combo, boost::weak_ptr<Surface> ws, bool for_input)
908 {
909         if (ignore_active_change) {
910                 return;
911         }
912
913         boost::shared_ptr<Surface> surface = ws.lock();
914
915         if (!surface) {
916                 return;
917         }
918
919         TreeModel::iterator active = combo->get_active ();
920         string new_port = (*active)[midi_port_columns.full_name];
921
922         if (new_port.empty()) {
923                 if (for_input) {
924                         surface->port().input().disconnect_all ();
925                 } else {
926                         surface->port().output().disconnect_all ();
927                 }
928
929                 return;
930         }
931
932         if (for_input) {
933                 if (!surface->port().input().connected_to (new_port)) {
934                         surface->port().input().disconnect_all ();
935                         surface->port().input().connect (new_port);
936                 }
937         } else {
938                 if (!surface->port().output().connected_to (new_port)) {
939                         surface->port().output().disconnect_all ();
940                         surface->port().output().connect (new_port);
941                 }
942         }
943 }