Update motorised button when a motorised surface's bindings are loaded.
[ardour.git] / libs / surfaces / generic_midi / gmcp_gui.cc
1 /*
2     Copyright (C) 2009-2012 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 <iostream>
21 #include <list>
22 #include <string>
23
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
31 #include "gtkmm2ext/utils.h"
32
33 #include "generic_midi_control_protocol.h"
34
35 #include "i18n.h"
36
37 class GMCPGUI : public Gtk::VBox 
38 {
39 public:
40         GMCPGUI (GenericMidiControlProtocol&);
41         ~GMCPGUI ();
42         
43 private:
44         GenericMidiControlProtocol& cp;
45         Gtk::ComboBoxText map_combo;
46         Gtk::Adjustment bank_adjustment;
47         Gtk::SpinButton bank_spinner;
48         Gtk::CheckButton motorised_button;
49
50         void binding_changed ();
51         void bank_changed ();
52         void motorised_changed ();
53 };
54
55 using namespace PBD;
56 using namespace ARDOUR;
57 using namespace std;
58 using namespace Gtk;
59 using namespace Gtkmm2ext;
60
61 void*
62 GenericMidiControlProtocol::get_gui () const
63 {
64         if (!gui) {
65                 const_cast<GenericMidiControlProtocol*>(this)->build_gui ();
66         }
67         return gui;
68 }
69
70 void
71 GenericMidiControlProtocol::tear_down_gui ()
72 {
73         delete (GMCPGUI*) gui;
74 }
75
76 void
77 GenericMidiControlProtocol::build_gui ()
78 {
79         gui = (void*) new GMCPGUI (*this);
80 }
81
82 /*--------------------*/
83
84 GMCPGUI::GMCPGUI (GenericMidiControlProtocol& p)
85         : cp (p)
86         , bank_adjustment (1, 1, 100, 1, 10)
87         , bank_spinner (bank_adjustment)
88         , motorised_button ("Motorised")
89 {
90         vector<string> popdowns;
91         popdowns.push_back (_("Reset All"));
92
93         for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
94                 popdowns.push_back (x->name);
95         }
96
97         set_popdown_strings (map_combo, popdowns);
98         
99         if (cp.current_binding().empty()) {
100                 map_combo.set_active_text (popdowns[0]);
101         } else {
102                 map_combo.set_active_text (cp.current_binding());
103         }
104
105         map_combo.signal_changed().connect (sigc::mem_fun (*this, &GMCPGUI::binding_changed));
106
107         set_spacing (6);
108         set_border_width (6);
109
110         Table* table = manage (new Table);
111         table->set_row_spacings (6);
112         table->set_col_spacings (6);
113         table->show ();
114         
115         int n = 0;
116
117         Label* label = manage (new Label (_("MIDI Bindings:")));
118         label->set_alignment (0, 0.5);
119         table->attach (*label, 0, 1, n, n + 1);
120         table->attach (map_combo, 1, 2, n, n + 1);
121         ++n;
122         
123         map_combo.show ();
124         label->show ();
125         
126         bank_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::bank_changed));
127
128         label = manage (new Label (_("Current Bank:")));
129         label->set_alignment (0, 0.5);
130         table->attach (*label, 0, 1, n, n + 1);
131         table->attach (bank_spinner, 1, 2, n, n + 1);
132         ++n;
133         
134         bank_spinner.show ();
135         label->show ();
136
137         motorised_button.signal_toggled().connect (sigc::mem_fun (*this, &GMCPGUI::motorised_changed));
138         table->attach (motorised_button, 0, 2, n, n + 1);
139         ++n;
140
141         motorised_button.show ();
142
143         pack_start (*table, false, false);
144 }
145
146 GMCPGUI::~GMCPGUI ()
147 {
148 }
149
150 void
151 GMCPGUI::bank_changed ()
152 {
153         int new_bank = bank_adjustment.get_value() - 1;
154         cp.set_current_bank (new_bank);
155 }
156
157 void
158 GMCPGUI::binding_changed ()
159 {
160         string str = map_combo.get_active_text ();
161
162         if (str == _("Reset All")) {
163                 cp.drop_bindings ();
164         } else {
165                 for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
166                         if (str == x->name) {
167                                 cp.load_bindings (x->path);
168                                 motorised_button.set_active (cp.motorised ());
169                                 break;
170                         }
171                 }
172         }
173 }
174
175 void
176 GMCPGUI::motorised_changed ()
177 {
178         cp.set_motorised (motorised_button.get_active ());
179 }