globally remove all trailing whitespace from ardour code base.
[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/gtk_ui.h"
32 #include "gtkmm2ext/utils.h"
33
34 #include "generic_midi_control_protocol.h"
35
36 #include "i18n.h"
37
38 class GMCPGUI : public Gtk::VBox
39 {
40 public:
41         GMCPGUI (GenericMidiControlProtocol&);
42         ~GMCPGUI ();
43         
44 private:
45         GenericMidiControlProtocol& cp;
46         Gtk::ComboBoxText map_combo;
47         Gtk::Adjustment bank_adjustment;
48         Gtk::SpinButton bank_spinner;
49         Gtk::CheckButton motorised_button;
50         Gtk::Adjustment threshold_adjustment;
51         Gtk::SpinButton threshold_spinner;
52
53         void binding_changed ();
54         void bank_changed ();
55         void motorised_changed ();
56         void threshold_changed ();
57 };
58
59 using namespace PBD;
60 using namespace ARDOUR;
61 using namespace std;
62 using namespace Gtk;
63 using namespace Gtkmm2ext;
64
65 void*
66 GenericMidiControlProtocol::get_gui () const
67 {
68         if (!gui) {
69                 const_cast<GenericMidiControlProtocol*>(this)->build_gui ();
70         }
71         static_cast<Gtk::VBox*>(gui)->show_all();
72         return gui;
73 }
74
75 void
76 GenericMidiControlProtocol::tear_down_gui ()
77 {
78         if (gui) {
79                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
80                 if (w) {
81                         w->hide();
82                         delete w;
83                 }
84         }
85         delete (GMCPGUI*) gui;
86         gui = 0;
87 }
88
89 void
90 GenericMidiControlProtocol::build_gui ()
91 {
92         gui = (void*) new GMCPGUI (*this);
93 }
94
95 /*--------------------*/
96
97 GMCPGUI::GMCPGUI (GenericMidiControlProtocol& p)
98         : cp (p)
99         , bank_adjustment (1, 1, 100, 1, 10)
100         , bank_spinner (bank_adjustment)
101         , motorised_button ("Motorised")
102         , threshold_adjustment (p.threshold(), 1, 127, 1, 10)
103         , threshold_spinner (threshold_adjustment)
104 {
105         vector<string> popdowns;
106         popdowns.push_back (_("Reset All"));
107
108         for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
109                 popdowns.push_back (x->name);
110         }
111
112         set_popdown_strings (map_combo, popdowns);
113         
114         if (cp.current_binding().empty()) {
115                 map_combo.set_active_text (popdowns[0]);
116         } else {
117                 map_combo.set_active_text (cp.current_binding());
118         }
119
120         map_combo.signal_changed().connect (sigc::mem_fun (*this, &GMCPGUI::binding_changed));
121
122         set_spacing (6);
123         set_border_width (6);
124
125         Table* table = manage (new Table);
126         table->set_row_spacings (6);
127         table->set_col_spacings (6);
128         table->show ();
129         
130         int n = 0;
131
132         Label* label = manage (new Label (_("MIDI Bindings:")));
133         label->set_alignment (0, 0.5);
134         table->attach (*label, 0, 1, n, n + 1);
135         table->attach (map_combo, 1, 2, n, n + 1);
136         ++n;
137         
138         map_combo.show ();
139         label->show ();
140         
141         bank_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::bank_changed));
142
143         label = manage (new Label (_("Current Bank:")));
144         label->set_alignment (0, 0.5);
145         table->attach (*label, 0, 1, n, n + 1);
146         table->attach (bank_spinner, 1, 2, n, n + 1);
147         ++n;
148         
149         bank_spinner.show ();
150         label->show ();
151
152         motorised_button.signal_toggled().connect (sigc::mem_fun (*this, &GMCPGUI::motorised_changed));
153         table->attach (motorised_button, 0, 2, n, n + 1);
154         ++n;
155
156         motorised_button.show ();
157         motorised_button.set_active (p.motorised ());
158
159         threshold_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::threshold_changed));
160
161         Gtkmm2ext::UI::instance()->set_tip (threshold_spinner,
162                                             string_compose (_("Controls how %1 behaves if the MIDI controller sends discontinuous values"), PROGRAM_NAME));
163
164         label = manage (new Label (_("Smoothing:")));
165         label->set_alignment (0, 0.5);
166         table->attach (*label, 0, 1, n, n + 1);
167         table->attach (threshold_spinner, 1, 2, n, n + 1);
168         ++n;
169
170         threshold_spinner.show ();
171         label->show ();
172
173         pack_start (*table, false, false);
174
175         binding_changed ();
176 }
177
178 GMCPGUI::~GMCPGUI ()
179 {
180 }
181
182 void
183 GMCPGUI::bank_changed ()
184 {
185         int new_bank = bank_adjustment.get_value() - 1;
186         cp.set_current_bank (new_bank);
187 }
188
189 void
190 GMCPGUI::binding_changed ()
191 {
192         string str = map_combo.get_active_text ();
193
194         if (str == _("Reset All")) {
195                 cp.drop_bindings ();
196         } else {
197                 for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
198                         if (str == x->name) {
199                                 cp.load_bindings (x->path);
200                                 motorised_button.set_active (cp.motorised ());
201                                 threshold_adjustment.set_value (cp.threshold ());
202                                 break;
203                         }
204                 }
205         }
206 }
207
208 void
209 GMCPGUI::motorised_changed ()
210 {
211         cp.set_motorised (motorised_button.get_active ());
212 }
213
214 void
215 GMCPGUI::threshold_changed ()
216 {
217         cp.set_threshold (threshold_adjustment.get_value());
218 }