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