feedback for toggled MIDI controls now sends 0 or 127, not various in-between values...
[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         Gtk::Adjustment threshold_adjustment;
50         Gtk::SpinButton threshold_spinner;
51
52         void binding_changed ();
53         void bank_changed ();
54         void motorised_changed ();
55         void threshold_changed ();
56 };
57
58 using namespace PBD;
59 using namespace ARDOUR;
60 using namespace std;
61 using namespace Gtk;
62 using namespace Gtkmm2ext;
63
64 void*
65 GenericMidiControlProtocol::get_gui () const
66 {
67         if (!gui) {
68                 const_cast<GenericMidiControlProtocol*>(this)->build_gui ();
69         }
70         return gui;
71 }
72
73 void
74 GenericMidiControlProtocol::tear_down_gui ()
75 {
76         delete (GMCPGUI*) gui;
77 }
78
79 void
80 GenericMidiControlProtocol::build_gui ()
81 {
82         gui = (void*) new GMCPGUI (*this);
83 }
84
85 /*--------------------*/
86
87 GMCPGUI::GMCPGUI (GenericMidiControlProtocol& p)
88         : cp (p)
89         , bank_adjustment (1, 1, 100, 1, 10)
90         , bank_spinner (bank_adjustment)
91         , motorised_button ("Motorised")
92         , threshold_adjustment (1, 1, 127, 1, 10)
93         , threshold_spinner (threshold_adjustment)
94 {
95         vector<string> popdowns;
96         popdowns.push_back (_("Reset All"));
97
98         for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
99                 popdowns.push_back (x->name);
100         }
101
102         set_popdown_strings (map_combo, popdowns);
103         
104         if (cp.current_binding().empty()) {
105                 map_combo.set_active_text (popdowns[0]);
106         } else {
107                 map_combo.set_active_text (cp.current_binding());
108         }
109
110         map_combo.signal_changed().connect (sigc::mem_fun (*this, &GMCPGUI::binding_changed));
111
112         set_spacing (6);
113         set_border_width (6);
114
115         Table* table = manage (new Table);
116         table->set_row_spacings (6);
117         table->set_col_spacings (6);
118         table->show ();
119         
120         int n = 0;
121
122         Label* label = manage (new Label (_("MIDI Bindings:")));
123         label->set_alignment (0, 0.5);
124         table->attach (*label, 0, 1, n, n + 1);
125         table->attach (map_combo, 1, 2, n, n + 1);
126         ++n;
127         
128         map_combo.show ();
129         label->show ();
130         
131         bank_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::bank_changed));
132
133         label = manage (new Label (_("Current Bank:")));
134         label->set_alignment (0, 0.5);
135         table->attach (*label, 0, 1, n, n + 1);
136         table->attach (bank_spinner, 1, 2, n, n + 1);
137         ++n;
138         
139         bank_spinner.show ();
140         label->show ();
141
142         motorised_button.signal_toggled().connect (sigc::mem_fun (*this, &GMCPGUI::motorised_changed));
143         table->attach (motorised_button, 0, 2, n, n + 1);
144         ++n;
145
146         motorised_button.show ();
147
148         threshold_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::threshold_changed));
149
150         label = manage (new Label (_("Threshold:")));
151         label->set_alignment (0, 0.5);
152         table->attach (*label, 0, 1, n, n + 1);
153         table->attach (threshold_spinner, 1, 2, n, n + 1);
154         ++n;
155
156         threshold_spinner.show ();
157         label->show ();
158
159         pack_start (*table, false, false);
160
161         binding_changed ();
162 }
163
164 GMCPGUI::~GMCPGUI ()
165 {
166 }
167
168 void
169 GMCPGUI::bank_changed ()
170 {
171         int new_bank = bank_adjustment.get_value() - 1;
172         cp.set_current_bank (new_bank);
173 }
174
175 void
176 GMCPGUI::binding_changed ()
177 {
178         string str = map_combo.get_active_text ();
179
180         if (str == _("Reset All")) {
181                 cp.drop_bindings ();
182         } else {
183                 for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
184                         if (str == x->name) {
185                                 cp.load_bindings (x->path);
186                                 motorised_button.set_active (cp.motorised ());
187                                 threshold_adjustment.set_value (cp.threshold ());
188                                 break;
189                         }
190                 }
191         }
192 }
193
194 void
195 GMCPGUI::motorised_changed ()
196 {
197         cp.set_motorised (motorised_button.get_active ());
198 }
199
200 void
201 GMCPGUI::threshold_changed ()
202 {
203         cp.set_threshold (threshold_adjustment.get_value());
204 }