use ActionManager namespace, rather than ActionMap objects
[ardour.git] / libs / surfaces / faderport / gui.cc
1 /*
2     Copyright (C) 2015 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
20 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
23
24 #include "pbd/unwind.h"
25 #include "pbd/strsplit.h"
26 #include "pbd/file_utils.h"
27
28 #include "gtkmm2ext/actions.h"
29 #include "gtkmm2ext/bindings.h"
30 #include "gtkmm2ext/gtk_ui.h"
31 #include "gtkmm2ext/gui_thread.h"
32 #include "gtkmm2ext/utils.h"
33
34 #include "ardour/audioengine.h"
35 #include "ardour/filesystem_paths.h"
36
37 #include "faderport.h"
38 #include "gui.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace PBD;
43 using namespace ARDOUR;
44 using namespace ArdourSurface;
45 using namespace std;
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48
49 void*
50 FaderPort::get_gui () const
51 {
52         if (!gui) {
53                 const_cast<FaderPort*>(this)->build_gui ();
54         }
55         static_cast<Gtk::VBox*>(gui)->show_all();
56         return gui;
57 }
58
59 void
60 FaderPort::tear_down_gui ()
61 {
62         if (gui) {
63                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
64                 if (w) {
65                         w->hide();
66                         delete w;
67                 }
68         }
69         delete static_cast<FPGUI*> (gui);
70         gui = 0;
71 }
72
73 void
74 FaderPort::build_gui ()
75 {
76         gui = (void*) new FPGUI (*this);
77 }
78
79 /*--------------------*/
80
81 FPGUI::FPGUI (FaderPort& p)
82         : fp (p)
83         , table (2, 5)
84         , action_table (5, 4)
85         , ignore_active_change (false)
86 {
87         set_border_width (12);
88
89         table.set_row_spacings (4);
90         table.set_col_spacings (6);
91         table.set_border_width (12);
92         table.set_homogeneous (false);
93
94         std::string data_file_path;
95         string name = "faderport-small.png";
96         Searchpath spath(ARDOUR::ardour_data_search_path());
97         spath.add_subdirectory_to_paths ("icons");
98         find_file (spath, name, data_file_path);
99         if (!data_file_path.empty()) {
100                 image.set (data_file_path);
101                 hpacker.pack_start (image, false, false);
102         }
103
104         Gtk::Label* l;
105         Gtk::Alignment* align;
106         int row = 0;
107
108         input_combo.pack_start (midi_port_columns.short_name);
109         output_combo.pack_start (midi_port_columns.short_name);
110
111         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &input_combo, true));
112         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &output_combo, false));
113
114         l = manage (new Gtk::Label);
115         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
116         l->set_alignment (1.0, 0.5);
117         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
118         table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
119         row++;
120
121         l = manage (new Gtk::Label);
122         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
123         l->set_alignment (1.0, 0.5);
124         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
125         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
126         row++;
127
128         build_mix_action_combo (mix_combo[0], FaderPort::ButtonState(0));
129         build_mix_action_combo (mix_combo[1], FaderPort::ShiftDown);
130         build_mix_action_combo (mix_combo[2], FaderPort::LongPress);
131
132         build_proj_action_combo (proj_combo[0], FaderPort::ButtonState(0));
133         build_proj_action_combo (proj_combo[1], FaderPort::ShiftDown);
134         build_proj_action_combo (proj_combo[2], FaderPort::LongPress);
135
136         build_trns_action_combo (trns_combo[0], FaderPort::ButtonState(0));
137         build_trns_action_combo (trns_combo[1], FaderPort::ShiftDown);
138         build_trns_action_combo (trns_combo[2], FaderPort::LongPress);
139
140         build_available_action_menu ();
141
142         build_foot_action_combo (foot_combo[0], FaderPort::ButtonState(0));
143         build_foot_action_combo (foot_combo[1], FaderPort::ShiftDown);
144         build_foot_action_combo (foot_combo[2], FaderPort::LongPress);
145
146         /* No shift-press combo for User because that is labelled as "next"
147          * (marker)
148          */
149
150         build_user_action_combo (user_combo[0], FaderPort::ButtonState(0));
151         build_user_action_combo (user_combo[1], FaderPort::LongPress);
152
153         action_table.set_row_spacings (4);
154         action_table.set_col_spacings (6);
155         action_table.set_border_width (12);
156         action_table.set_homogeneous (false);
157
158         int action_row = 0;
159
160         l = manage (new Gtk::Label);
161         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Press Action")));
162         l->set_alignment (0.5, 0.5);
163         action_table.attach (*l, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
164         l = manage (new Gtk::Label);
165         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Shift-Press Action")));
166         l->set_alignment (0.5, 0.5);
167         action_table.attach (*l, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
168         l = manage (new Gtk::Label);
169         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Long Press Action")));
170         l->set_alignment (0.5, 0.5);
171         action_table.attach (*l, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
172         action_row++;
173
174         l = manage (new Gtk::Label);
175         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Mix")));
176         l->set_alignment (1.0, 0.5);
177         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
178         align = manage (new Alignment);
179         align->set (0.0, 0.5);
180         align->add (mix_combo[0]);
181         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
182         align = manage (new Alignment);
183         align->set (0.0, 0.5);
184         align->add (mix_combo[1]);
185         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
186         align = manage (new Alignment);
187         align->set (0.0, 0.5);
188         align->add (mix_combo[2]);
189         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
190         action_row++;
191
192         l = manage (new Gtk::Label);
193         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Proj")));
194         l->set_alignment (1.0, 0.5);
195         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
196         align = manage (new Alignment);
197         align->set (0.0, 0.5);
198         align->add (proj_combo[0]);
199         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
200         align = manage (new Alignment);
201         align->set (0.0, 0.5);
202         align->add (proj_combo[1]);
203         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
204         align = manage (new Alignment);
205         align->set (0.0, 0.5);
206         align->add (proj_combo[2]);
207         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
208         action_row++;
209
210         l = manage (new Gtk::Label);
211         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Trns")));
212         l->set_alignment (1.0, 0.5);
213         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
214         align = manage (new Alignment);
215         align->set (0.0, 0.5);
216         align->add (trns_combo[0]);
217         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
218         align = manage (new Alignment);
219         align->set (0.0, 0.5);
220         align->add (trns_combo[1]);
221         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
222         align = manage (new Alignment);
223         align->set (0.0, 0.5);
224         align->add (trns_combo[2]);
225         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
226         action_row++;
227
228         l = manage (new Gtk::Label);
229         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("User")));
230         l->set_alignment (1.0, 0.5);
231         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
232         align = manage (new Alignment);
233         align->set (0.0, 0.5);
234         align->add (user_combo[0]);
235         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
236         /* skip shift press combo */
237         align = manage (new Alignment);
238         align->set (0.0, 0.5);
239         align->add (user_combo[1]);
240         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
241         action_row++;
242
243         l = manage (new Gtk::Label);
244         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Footswitch")));
245         l->set_alignment (1.0, 0.5);
246         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
247         align = manage (new Alignment);
248         align->set (0.0, 0.5);
249         align->add (foot_combo[0]);
250         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
251         align = manage (new Alignment);
252         align->set (0.0, 0.5);
253         align->add (foot_combo[1]);
254         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
255         align = manage (new Alignment);
256         align->set (0.0, 0.5);
257         align->add (foot_combo[2]);
258         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
259         action_row++;
260
261         table.attach (action_table, 0, 5, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
262         row++;
263
264         hpacker.pack_start (table, true, true);
265         pack_start (hpacker, false, false);
266
267         /* update the port connection combos */
268
269         update_port_combos ();
270
271         /* catch future changes to connection state */
272
273         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FPGUI::connection_handler, this), gui_context());
274 }
275
276 FPGUI::~FPGUI ()
277 {
278 }
279
280 void
281 FPGUI::connection_handler ()
282 {
283         /* ignore all changes to combobox active strings here, because we're
284            updating them to match a new ("external") reality - we were called
285            because port connections have changed.
286         */
287
288         PBD::Unwinder<bool> ici (ignore_active_change, true);
289
290         update_port_combos ();
291 }
292
293 void
294 FPGUI::update_port_combos ()
295 {
296         vector<string> midi_inputs;
297         vector<string> midi_outputs;
298
299         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
300         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
301
302         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
303         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
304         bool input_found = false;
305         bool output_found = false;
306         int n;
307
308         input_combo.set_model (input);
309         output_combo.set_model (output);
310
311         Gtk::TreeModel::Children children = input->children();
312         Gtk::TreeModel::Children::iterator i;
313         i = children.begin();
314         ++i; /* skip "Disconnected" */
315
316
317         for (n = 1;  i != children.end(); ++i, ++n) {
318                 string port_name = (*i)[midi_port_columns.full_name];
319                 if (fp.input_port()->connected_to (port_name)) {
320                         input_combo.set_active (n);
321                         input_found = true;
322                         break;
323                 }
324         }
325
326         if (!input_found) {
327                 input_combo.set_active (0); /* disconnected */
328         }
329
330         children = output->children();
331         i = children.begin();
332         ++i; /* skip "Disconnected" */
333
334         for (n = 1;  i != children.end(); ++i, ++n) {
335                 string port_name = (*i)[midi_port_columns.full_name];
336                 if (fp.output_port()->connected_to (port_name)) {
337                         output_combo.set_active (n);
338                         output_found = true;
339                         break;
340                 }
341         }
342
343         if (!output_found) {
344                 output_combo.set_active (0); /* disconnected */
345         }
346 }
347
348 void
349 FPGUI::build_available_action_menu ()
350 {
351         /* build a model of all available actions (needs to be tree structured
352          * more)
353          */
354
355         available_action_model = TreeStore::create (action_columns);
356
357         vector<string> paths;
358         vector<string> labels;
359         vector<string> tooltips;
360         vector<string> keys;
361         vector<Glib::RefPtr<Gtk::Action> > actions;
362
363         ActionManager::get_all_actions (paths, labels, tooltips, keys, actions);
364
365         typedef std::map<string,TreeIter> NodeMap;
366         NodeMap nodes;
367         NodeMap::iterator r;
368
369
370         vector<string>::iterator k;
371         vector<string>::iterator p;
372         vector<string>::iterator t;
373         vector<string>::iterator l;
374
375         available_action_model->clear ();
376
377         TreeIter rowp;
378         TreeModel::Row parent;
379
380         /* Disabled item (row 0) */
381
382         rowp = available_action_model->append();
383         parent = *(rowp);
384         parent[action_columns.name] = _("Disabled");
385
386         /* Key aliasing */
387
388         rowp = available_action_model->append();
389         parent = *(rowp);
390         parent[action_columns.name] = _("Shift");
391         rowp = available_action_model->append();
392         parent = *(rowp);
393         parent[action_columns.name] = _("Control");
394         rowp = available_action_model->append();
395         parent = *(rowp);
396         parent[action_columns.name] = _("Option");
397         rowp = available_action_model->append();
398         parent = *(rowp);
399         parent[action_columns.name] = _("CmdAlt");
400
401
402         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
403
404                 TreeModel::Row row;
405                 vector<string> parts;
406
407                 parts.clear ();
408
409                 split (*p, parts, '/');
410
411                 if (parts.empty()) {
412                         continue;
413                 }
414
415                 //kinda kludgy way to avoid displaying menu items as mappable
416                 if ( parts[1] == _("Main_menu") )
417                         continue;
418                 if ( parts[1] == _("JACK") )
419                         continue;
420                 if ( parts[1] == _("redirectmenu") )
421                         continue;
422                 if ( parts[1] == _("Editor_menus") )
423                         continue;
424                 if ( parts[1] == _("RegionList") )
425                         continue;
426                 if ( parts[1] == _("ProcessorMenu") )
427                         continue;
428
429                 if ((r = nodes.find (parts[1])) == nodes.end()) {
430
431                         /* top level is missing */
432
433                         TreeIter rowp;
434                         TreeModel::Row parent;
435                         rowp = available_action_model->append();
436                         nodes[parts[1]] = rowp;
437                         parent = *(rowp);
438                         parent[action_columns.name] = parts[1];
439
440                         row = *(available_action_model->append (parent.children()));
441
442                 } else {
443
444                         row = *(available_action_model->append ((*r->second)->children()));
445
446                 }
447
448                 /* add this action */
449
450                 if (l->empty ()) {
451                         row[action_columns.name] = *t;
452                         action_map[*t] = *p;
453                 } else {
454                         row[action_columns.name] = *l;
455                         action_map[*l] = *p;
456                 }
457
458                 string path = (*p);
459                 /* ControlProtocol::access_action() is not interested in the
460                    legacy "<Actions>/" prefix part of a path.
461                 */
462                 path = path.substr (strlen ("<Actions>/"));
463
464                 row[action_columns.path] = path;
465         }
466 }
467
468 void
469 FPGUI::action_changed (Gtk::ComboBox* cb, FaderPort::ButtonID id, FaderPort::ButtonState bs)
470 {
471         TreeModel::const_iterator row = cb->get_active ();
472         string action_path = (*row)[action_columns.path];
473
474         /* release binding */
475         fp.set_action (id, action_path, false, bs);
476 }
477
478 void
479 FPGUI::build_action_combo (Gtk::ComboBox& cb, vector<pair<string,string> > const & actions, FaderPort::ButtonID id, FaderPort::ButtonState bs)
480 {
481         Glib::RefPtr<Gtk::ListStore> model (Gtk::ListStore::create (action_columns));
482         TreeIter rowp;
483         TreeModel::Row row;
484         string current_action = fp.get_action (id, false, bs); /* lookup release action */
485         int active_row = -1;
486         int n;
487         vector<pair<string,string> >::const_iterator i;
488
489         rowp = model->append();
490         row = *(rowp);
491         row[action_columns.name] = _("Disabled");
492         row[action_columns.path] = string();
493
494         if (current_action.empty()) {
495                 active_row = 0;
496         }
497
498         for (i = actions.begin(), n = 0; i != actions.end(); ++i, ++n) {
499                 rowp = model->append();
500                 row = *(rowp);
501                 row[action_columns.name] = i->first;
502                 row[action_columns.path] = i->second;
503                 if (current_action == i->second) {
504                         active_row = n+1;
505                 }
506         }
507
508         cb.set_model (model);
509         cb.pack_start (action_columns.name);
510
511         if (active_row >= 0) {
512                 cb.set_active (active_row);
513         }
514
515         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::action_changed), &cb, id, bs));
516 }
517
518 void
519 FPGUI::build_mix_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
520 {
521         vector<pair<string,string> > actions;
522
523         actions.push_back (make_pair (string (_("Show Mixer Window")), string (X_("Window/show-mixer"))));
524         actions.push_back (make_pair (string (_("Show/Hide Mixer list")), string (X_("Common/ToggleMixerList"))));
525         actions.push_back (make_pair (string("Toggle Meterbridge"), string(X_("Common/toggle-meterbridge"))));
526         actions.push_back (make_pair (string (_("Show/Hide Editor mixer strip")), string (X_("Editor/show-editor-mixer"))));
527
528         build_action_combo (cb, actions, FaderPort::Mix, bs);
529 }
530
531 void
532 FPGUI::build_proj_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
533 {
534         vector<pair<string,string> > actions;
535
536         actions.push_back (make_pair (string (_("Show Editor Window")), string (X_("Mixer/show-editor"))));
537         actions.push_back (make_pair (string("Toggle Editor Lists"), string(X_("Editor/show-editor-list"))));
538         actions.push_back (make_pair (string("Toggle Summary"), string(X_("Editor/ToggleSummary"))));
539         actions.push_back (make_pair (string("Toggle Meterbridge"), string(X_("Common/toggle-meterbridge"))));
540         actions.push_back (make_pair (string (_("Zoom to Session")), string (X_("Editor/zoom-to-session"))));
541
542 //      actions.push_back (make_pair (string (_("Zoom In")), string (X_("Editor/temporal-zoom-in"))));
543 //      actions.push_back (make_pair (string (_("Zoom Out")), string (X_("Editor/temporal-zoom-out"))));
544
545         build_action_combo (cb, actions, FaderPort::Proj, bs);
546 }
547
548 void
549 FPGUI::build_trns_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
550 {
551         vector<pair<string,string> > actions;
552
553         actions.push_back (make_pair (string("Toggle Big Clock"), string(X_("Window/toggle-big-clock"))));  //note:  this would really make sense if the Big Clock had transport buttons on it
554         actions.push_back (make_pair (string("Toggle Locations window"), string(X_("Window/toggle-locations"))));
555         actions.push_back (make_pair (string("Toggle Metronome"), string(X_("Transport/ToggleClick"))));
556         actions.push_back (make_pair (string("Toggle External Sync"), string(X_("Transport/ToggleExternalSync"))));
557         actions.push_back (make_pair (string("Toggle Follow Playhead"), string(X_("Editor/toggle-follow-playhead"))));
558
559 //      actions.push_back (make_pair (string("Set Playhead @pointer"), string(X_("Editor/set-playhead"))));
560
561
562         build_action_combo (cb, actions, FaderPort::Trns, bs);
563 }
564
565 void
566 FPGUI::build_foot_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
567 {
568         vector<pair<string,string> > actions;
569
570         actions.push_back (make_pair (string("Toggle Roll"), string(X_("Transport/ToggleRoll"))));
571         actions.push_back (make_pair (string("Toggle Rec-Enable"), string(X_("Transport/Record"))));
572         actions.push_back (make_pair (string("Toggle Roll+Rec"), string(X_("Transport/record-roll"))));
573         actions.push_back (make_pair (string("Toggle Loop"), string(X_("Transport/Loop"))));
574         actions.push_back (make_pair (string("Toggle Click"), string(X_("Transport/ToggleClick"))));
575         actions.push_back (make_pair (string("Record with Pre-Roll"), string(X_("Transport/RecordPreroll"))));
576         actions.push_back (make_pair (string("Record with Count-In"), string(X_("Transport/RecordCountIn"))));
577
578         build_action_combo (cb, actions, FaderPort::Footswitch, bs);
579 }
580
581 bool
582 FPGUI::find_action_in_model (const TreeModel::iterator& iter, std::string const & action_path, TreeModel::iterator* found)
583 {
584         TreeModel::Row row = *iter;
585         string path = row[action_columns.path];
586
587         if (path == action_path) {
588                 *found = iter;
589                 return true;
590         }
591
592         return false;
593 }
594
595 void
596 FPGUI::build_user_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
597 {
598         cb.set_model (available_action_model);
599         cb.pack_start (action_columns.name);
600         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::action_changed), &cb, FaderPort::User, bs));
601
602         /* set the active "row" to the right value for the current button binding */
603
604         string current_action = fp.get_action (FaderPort::User, false, bs); /* lookup release action */
605
606         if (current_action.empty()) {
607                 cb.set_active (0); /* "disabled" */
608                 return;
609         }
610
611         TreeModel::iterator iter = available_action_model->children().end();
612
613         available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &FPGUI::find_action_in_model), current_action, &iter));
614
615         if (iter != available_action_model->children().end()) {
616                 cb.set_active (iter);
617         } else {
618                 cb.set_active (0);
619         }
620
621 }
622
623 Glib::RefPtr<Gtk::ListStore>
624 FPGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
625 {
626         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
627         TreeModel::Row row;
628
629         row = *store->append ();
630         row[midi_port_columns.full_name] = string();
631         row[midi_port_columns.short_name] = _("Disconnected");
632
633         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
634                 row = *store->append ();
635                 row[midi_port_columns.full_name] = *p;
636                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
637                 if (pn.empty ()) {
638                         pn = (*p).substr ((*p).find (':') + 1);
639                 }
640                 row[midi_port_columns.short_name] = pn;
641         }
642
643         return store;
644 }
645
646 void
647 FPGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
648 {
649         if (ignore_active_change) {
650                 return;
651         }
652
653         TreeModel::iterator active = combo->get_active ();
654         string new_port = (*active)[midi_port_columns.full_name];
655
656         if (new_port.empty()) {
657                 if (for_input) {
658                         fp.input_port()->disconnect_all ();
659                 } else {
660                         fp.output_port()->disconnect_all ();
661                 }
662
663                 return;
664         }
665
666         if (for_input) {
667                 if (!fp.input_port()->connected_to (new_port)) {
668                         fp.input_port()->disconnect_all ();
669                         fp.input_port()->connect (new_port);
670                 }
671         } else {
672                 if (!fp.output_port()->connected_to (new_port)) {
673                         fp.output_port()->disconnect_all ();
674                         fp.output_port()->connect (new_port);
675                 }
676         }
677 }