2 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2015 Paul Davis
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
24 #include "pbd/unwind.h"
25 #include "pbd/strsplit.h"
26 #include "pbd/file_utils.h"
28 #include "gtkmm2ext/bindings.h"
29 #include "gtkmm2ext/gtk_ui.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/utils.h"
33 #include "ardour/audioengine.h"
34 #include "ardour/filesystem_paths.h"
36 #include "faderport8.h"
42 using namespace ARDOUR;
43 using namespace ArdourSurface;
46 using namespace Gtkmm2ext;
49 FaderPort8::get_gui () const
52 const_cast<FaderPort8*>(this)->build_gui ();
54 static_cast<Gtk::VBox*>(gui)->show_all();
59 FaderPort8::tear_down_gui ()
62 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
68 delete static_cast<FP8GUI*> (gui);
73 FaderPort8::build_gui ()
75 gui = (void*) new FP8GUI (*this);
78 /* ****************************************************************************/
80 FP8GUI::FP8GUI (FaderPort8& p)
83 , ignore_active_change (false)
85 set_border_width (12);
87 table.set_row_spacings (4);
88 table.set_col_spacings (6);
89 table.set_border_width (12);
90 table.set_homogeneous (false);
95 input_combo.pack_start (midi_port_columns.short_name);
96 output_combo.pack_start (midi_port_columns.short_name);
98 input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &input_combo, true));
99 output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &output_combo, false));
101 l = manage (new Gtk::Label);
102 l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
103 l->set_alignment (1.0, 0.5);
104 table.attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
105 table.attach (input_combo, 2, 6, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
108 l = manage (new Gtk::Label);
109 l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
110 l->set_alignment (1.0, 0.5);
111 table.attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
112 table.attach (output_combo, 2, 6, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
118 build_available_action_menu ();
122 Gtk::Alignment* align;
124 for (FP8Controls::UserButtonMap::const_iterator i = fp.control().user_buttons ().begin ();
125 i != fp.control().user_buttons ().end (); ++i) {
126 Gtk::ComboBox* user_combo = manage (new Gtk::ComboBox);
127 build_action_combo (*user_combo, i->first);
128 l = manage (new Gtk::Label);
129 l->set_markup (string_compose ("<span weight=\"bold\">%1:</span>", i->second));
130 l->set_alignment (1.0, 0.5);
131 table.attach (*l, 2 * action_col, 2 * action_col + 1, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
132 align = manage (new Alignment);
133 align->set (0.0, 0.5);
134 align->add (*user_combo);
135 table.attach (*align, 2 * action_col + 1, 2 * action_col + 2, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
137 if (++action_row == 4) {
143 /* update the port connection combos */
144 update_port_combos ();
146 /* catch future changes to connection state */
147 fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FP8GUI::connection_handler, this), gui_context());
155 FP8GUI::connection_handler ()
157 PBD::Unwinder<bool> ici (ignore_active_change, true);
158 update_port_combos ();
162 FP8GUI::update_port_combos ()
164 vector<string> midi_inputs;
165 vector<string> midi_outputs;
167 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
168 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
170 Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
171 Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
172 bool input_found = false;
173 bool output_found = false;
176 input_combo.set_model (input);
177 output_combo.set_model (output);
179 Gtk::TreeModel::Children children = input->children();
180 Gtk::TreeModel::Children::iterator i;
181 i = children.begin();
182 ++i; /* skip "Disconnected" */
184 for (n = 1; i != children.end(); ++i, ++n) {
185 string port_name = (*i)[midi_port_columns.full_name];
186 if (fp.input_port()->connected_to (port_name)) {
187 input_combo.set_active (n);
194 input_combo.set_active (0); /* disconnected */
197 children = output->children();
198 i = children.begin();
199 ++i; /* skip "Disconnected" */
201 for (n = 1; i != children.end(); ++i, ++n) {
202 string port_name = (*i)[midi_port_columns.full_name];
203 if (fp.output_port()->connected_to (port_name)) {
204 output_combo.set_active (n);
211 output_combo.set_active (0); /* disconnected */
216 Glib::RefPtr<Gtk::ListStore>
217 FP8GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
219 Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
222 row = *store->append ();
223 row[midi_port_columns.full_name] = string();
224 row[midi_port_columns.short_name] = _("Disconnected");
226 for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
227 row = *store->append ();
228 row[midi_port_columns.full_name] = *p;
229 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
231 pn = (*p).substr ((*p).find (':') + 1);
233 row[midi_port_columns.short_name] = pn;
240 FP8GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
242 if (ignore_active_change) {
246 TreeModel::iterator active = combo->get_active ();
247 string new_port = (*active)[midi_port_columns.full_name];
249 if (new_port.empty()) {
251 fp.input_port()->disconnect_all ();
253 fp.output_port()->disconnect_all ();
260 if (!fp.input_port()->connected_to (new_port)) {
261 fp.input_port()->disconnect_all ();
262 fp.input_port()->connect (new_port);
265 if (!fp.output_port()->connected_to (new_port)) {
266 fp.output_port()->disconnect_all ();
267 fp.output_port()->connect (new_port);
275 FP8GUI::build_available_action_menu ()
277 /* build a model of all available actions (needs to be tree structured
281 available_action_model = TreeStore::create (action_columns);
283 vector<string> paths;
284 vector<string> labels;
285 vector<string> tooltips;
287 vector<Glib::RefPtr<Gtk::Action> > actions;
289 Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
291 typedef std::map<string,TreeIter> NodeMap;
296 vector<string>::iterator k;
297 vector<string>::iterator p;
298 vector<string>::iterator t;
299 vector<string>::iterator l;
301 available_action_model->clear ();
304 TreeModel::Row parent;
306 /* Disabled item (row 0) */
308 rowp = available_action_model->append();
310 parent[action_columns.name] = _("Disabled");
312 for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
315 vector<string> parts;
319 split (*p, parts, '/');
325 //kinda kludgy way to avoid displaying menu items as mappable
326 if ( parts[1] == _("Main_menu") )
328 if ( parts[1] == _("JACK") )
330 if ( parts[1] == _("redirectmenu") )
332 if ( parts[1] == _("Editor_menus") )
334 if ( parts[1] == _("RegionList") )
336 if ( parts[1] == _("ProcessorMenu") )
339 if ((r = nodes.find (parts[1])) == nodes.end()) {
341 /* top level is missing */
344 TreeModel::Row parent;
345 rowp = available_action_model->append();
346 nodes[parts[1]] = rowp;
348 parent[action_columns.name] = parts[1];
350 row = *(available_action_model->append (parent.children()));
354 row = *(available_action_model->append ((*r->second)->children()));
358 /* add this action */
361 row[action_columns.name] = *t;
364 row[action_columns.name] = *l;
369 /* ControlProtocol::access_action() is not interested in the
370 legacy "<Actions>/" prefix part of a path.
372 path = path.substr (strlen ("<Actions>/"));
374 row[action_columns.path] = path;
379 FP8GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const& action_path, TreeModel::iterator* found)
381 TreeModel::Row row = *iter;
382 string path = row[action_columns.path];
384 if (path == action_path) {
393 FP8GUI::build_action_combo (Gtk::ComboBox& cb, FP8Controls::ButtonId id)
395 cb.set_model (available_action_model);
396 cb.pack_start (action_columns.name);
398 /* set the active "row" to the right value for the current button binding */
399 string current_action = fp.get_button_action (id, false); /* lookup release action */
401 if (current_action.empty()) {
402 cb.set_active (0); /* "disabled" */
404 TreeModel::iterator iter = available_action_model->children().end();
406 available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &FP8GUI::find_action_in_model), current_action, &iter));
408 if (iter != available_action_model->children().end()) {
409 cb.set_active (iter);
414 /* bind signal _after_ setting the current value */
415 cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::action_changed), &cb, id));
419 FP8GUI::action_changed (Gtk::ComboBox* cb, FP8Controls::ButtonId id)
421 TreeModel::const_iterator row = cb->get_active ();
422 string action_path = (*row)[action_columns.path];
423 fp.set_button_action (id, false, action_path);