2 Copyright (C) 2009-2012 Paul Davis
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.
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.
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.
24 #include <gtkmm/comboboxtext.h>
25 #include <gtkmm/label.h>
26 #include <gtkmm/box.h>
27 #include <gtkmm/adjustment.h>
28 #include <gtkmm/spinbutton.h>
29 #include <gtkmm/table.h>
30 #include <gtkmm/liststore.h>
32 #include "pbd/unwind.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/port.h"
36 #include "ardour/midi_port.h"
38 #include "gtkmm2ext/gtk_ui.h"
39 #include "gtkmm2ext/gui_thread.h"
40 #include "gtkmm2ext/utils.h"
42 #include "generic_midi_control_protocol.h"
46 class GMCPGUI : public Gtk::VBox
49 GMCPGUI (GenericMidiControlProtocol&);
53 GenericMidiControlProtocol& cp;
54 Gtk::ComboBoxText map_combo;
55 Gtk::Adjustment bank_adjustment;
56 Gtk::SpinButton bank_spinner;
57 Gtk::CheckButton motorised_button;
58 Gtk::Adjustment threshold_adjustment;
59 Gtk::SpinButton threshold_spinner;
61 Gtk::ComboBox input_combo;
62 Gtk::ComboBox output_combo;
64 void binding_changed ();
66 void motorised_changed ();
67 void threshold_changed ();
69 void update_port_combos ();
70 PBD::ScopedConnection connection_change_connection;
71 void connection_handler ();
73 struct MidiPortColumns : public Gtk::TreeModel::ColumnRecord {
78 Gtk::TreeModelColumn<std::string> short_name;
79 Gtk::TreeModelColumn<std::string> full_name;
82 MidiPortColumns midi_port_columns;
83 bool ignore_active_change;
85 Glib::RefPtr<Gtk::ListStore> build_midi_port_list (std::vector<std::string> const & ports, bool for_input);
86 void active_port_changed (Gtk::ComboBox*,bool for_input);
90 using namespace ARDOUR;
93 using namespace Gtkmm2ext;
96 GenericMidiControlProtocol::get_gui () const
99 const_cast<GenericMidiControlProtocol*>(this)->build_gui ();
101 static_cast<Gtk::VBox*>(gui)->show_all();
106 GenericMidiControlProtocol::tear_down_gui ()
109 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
115 delete (GMCPGUI*) gui;
120 GenericMidiControlProtocol::build_gui ()
122 gui = (void*) new GMCPGUI (*this);
125 /*--------------------*/
127 GMCPGUI::GMCPGUI (GenericMidiControlProtocol& p)
129 , bank_adjustment (1, 1, 100, 1, 10)
130 , bank_spinner (bank_adjustment)
131 , motorised_button ("Motorised")
132 , threshold_adjustment (p.threshold(), 1, 127, 1, 10)
133 , threshold_spinner (threshold_adjustment)
135 vector<string> popdowns;
136 popdowns.push_back (_("Reset All"));
138 for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
139 popdowns.push_back (x->name);
142 set_popdown_strings (map_combo, popdowns);
144 if (cp.current_binding().empty()) {
145 map_combo.set_active_text (popdowns[0]);
147 map_combo.set_active_text (cp.current_binding());
150 map_combo.signal_changed().connect (sigc::mem_fun (*this, &GMCPGUI::binding_changed));
153 set_border_width (6);
155 Table* table = manage (new Table);
156 table->set_row_spacings (6);
157 table->set_col_spacings (6);
162 // MIDI input and output selectors
163 input_combo.pack_start (midi_port_columns.short_name);
164 output_combo.pack_start (midi_port_columns.short_name);
166 input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &GMCPGUI::active_port_changed), &input_combo, true));
167 output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &GMCPGUI::active_port_changed), &output_combo, false));
169 Label* label = manage (new Gtk::Label);
170 label->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
171 label->set_alignment (1.0, 0.5);
172 table->attach (*label, 0, 1, n, n+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
173 table->attach (input_combo, 1, 2, n, n+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
176 label = manage (new Gtk::Label);
177 label->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
178 label->set_alignment (1.0, 0.5);
179 table->attach (*label, 0, 1, n, n+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
180 table->attach (output_combo, 1, 2, n, n+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
183 //MIDI binding file selector...
184 label = manage (new Label (_("MIDI Bindings:")));
185 label->set_alignment (0, 0.5);
186 table->attach (*label, 0, 1, n, n + 1);
187 table->attach (map_combo, 1, 2, n, n + 1);
193 bank_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::bank_changed));
195 label = manage (new Label (_("Current Bank:")));
196 label->set_alignment (0, 0.5);
197 table->attach (*label, 0, 1, n, n + 1);
198 table->attach (bank_spinner, 1, 2, n, n + 1);
201 bank_spinner.show ();
204 motorised_button.signal_toggled().connect (sigc::mem_fun (*this, &GMCPGUI::motorised_changed));
205 table->attach (motorised_button, 0, 2, n, n + 1);
208 motorised_button.show ();
209 motorised_button.set_active (p.motorised ());
211 threshold_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::threshold_changed));
213 Gtkmm2ext::UI::instance()->set_tip (threshold_spinner,
214 string_compose (_("Controls how %1 behaves if the MIDI controller sends discontinuous values"), PROGRAM_NAME));
216 label = manage (new Label (_("Smoothing:")));
217 label->set_alignment (0, 0.5);
218 table->attach (*label, 0, 1, n, n + 1);
219 table->attach (threshold_spinner, 1, 2, n, n + 1);
222 threshold_spinner.show ();
225 pack_start (*table, false, false);
229 /* update the port connection combos */
231 update_port_combos ();
233 /* catch future changes to connection state */
235 cp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&GMCPGUI::connection_handler, this), gui_context());
243 GMCPGUI::bank_changed ()
245 int new_bank = bank_adjustment.get_value() - 1;
246 cp.set_current_bank (new_bank);
250 GMCPGUI::binding_changed ()
252 string str = map_combo.get_active_text ();
254 if (str == _("Reset All")) {
257 for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
258 if (str == x->name) {
259 cp.load_bindings (x->path);
260 motorised_button.set_active (cp.motorised ());
261 threshold_adjustment.set_value (cp.threshold ());
269 GMCPGUI::motorised_changed ()
271 cp.set_motorised (motorised_button.get_active ());
275 GMCPGUI::threshold_changed ()
277 cp.set_threshold (threshold_adjustment.get_value());
281 GMCPGUI::connection_handler ()
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.
288 PBD::Unwinder<bool> ici (ignore_active_change, true);
290 update_port_combos ();
294 GMCPGUI::update_port_combos ()
296 vector<string> midi_inputs;
297 vector<string> midi_outputs;
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);
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;
308 input_combo.set_model (input);
309 output_combo.set_model (output);
311 Gtk::TreeModel::Children children = input->children();
312 Gtk::TreeModel::Children::iterator i;
313 i = children.begin();
314 ++i; /* skip "Disconnected" */
317 for (n = 1; i != children.end(); ++i, ++n) {
318 string port_name = (*i)[midi_port_columns.full_name];
319 if (cp.input_port()->connected_to (port_name)) {
320 input_combo.set_active (n);
327 input_combo.set_active (0); /* disconnected */
330 children = output->children();
331 i = children.begin();
332 ++i; /* skip "Disconnected" */
334 for (n = 1; i != children.end(); ++i, ++n) {
335 string port_name = (*i)[midi_port_columns.full_name];
336 if (cp.output_port()->connected_to (port_name)) {
337 output_combo.set_active (n);
344 output_combo.set_active (0); /* disconnected */
348 Glib::RefPtr<Gtk::ListStore>
349 GMCPGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
351 Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
354 row = *store->append ();
355 row[midi_port_columns.full_name] = string();
356 row[midi_port_columns.short_name] = _("Disconnected");
358 for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
359 row = *store->append ();
360 row[midi_port_columns.full_name] = *p;
361 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
363 pn = (*p).substr ((*p).find (':') + 1);
365 row[midi_port_columns.short_name] = pn;
372 GMCPGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
374 if (ignore_active_change) {
378 TreeModel::iterator active = combo->get_active ();
379 string new_port = (*active)[midi_port_columns.full_name];
381 if (new_port.empty()) {
383 cp.input_port()->disconnect_all ();
385 cp.output_port()->disconnect_all ();
392 if (!cp.input_port()->connected_to (new_port)) {
393 cp.input_port()->disconnect_all ();
394 cp.input_port()->connect (new_port);
397 if (!cp.output_port()->connected_to (new_port)) {
398 cp.output_port()->disconnect_all ();
399 cp.output_port()->connect (new_port);