Merge branch 'cairocanvas'
[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
158         threshold_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &GMCPGUI::threshold_changed));
159
160         Gtkmm2ext::UI::instance()->set_tip (threshold_spinner, 
161                                             string_compose (_("Controls how %1 behaves if the MIDI controller sends discontinuous values"), PROGRAM_NAME));
162
163         label = manage (new Label (_("Smoothing:")));
164         label->set_alignment (0, 0.5);
165         table->attach (*label, 0, 1, n, n + 1);
166         table->attach (threshold_spinner, 1, 2, n, n + 1);
167         ++n;
168
169         threshold_spinner.show ();
170         label->show ();
171
172         pack_start (*table, false, false);
173
174         binding_changed ();
175 }
176
177 GMCPGUI::~GMCPGUI ()
178 {
179 }
180
181 void
182 GMCPGUI::bank_changed ()
183 {
184         int new_bank = bank_adjustment.get_value() - 1;
185         cp.set_current_bank (new_bank);
186 }
187
188 void
189 GMCPGUI::binding_changed ()
190 {
191         string str = map_combo.get_active_text ();
192
193         if (str == _("Reset All")) {
194                 cp.drop_bindings ();
195         } else {
196                 for (list<GenericMidiControlProtocol::MapInfo>::iterator x = cp.map_info.begin(); x != cp.map_info.end(); ++x) {
197                         if (str == x->name) {
198                                 cp.load_bindings (x->path);
199                                 motorised_button.set_active (cp.motorised ());
200                                 threshold_adjustment.set_value (cp.threshold ());
201                                 break;
202                         }
203                 }
204         }
205 }
206
207 void
208 GMCPGUI::motorised_changed ()
209 {
210         cp.set_motorised (motorised_button.get_active ());
211 }
212
213 void
214 GMCPGUI::threshold_changed ()
215 {
216         cp.set_threshold (threshold_adjustment.get_value());
217 }