Initial revision
[ardour.git] / libs / gtkmm2ext / slider_controller.cc
1 /*
2     Copyright (C) 1998-99 Paul Davis
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17     $Id$
18 */
19
20 #include <string>
21 #include <climits>
22
23 #include <midi++/controllable.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26 #include <gtkmm2ext/slider_controller.h>
27 #include <gtkmm2ext/pix.h>
28
29 #include "i18n.h"
30
31 using namespace Gtkmm2ext;
32
33 SliderController::SliderController (Pix *pixset,
34                                     Gtk::Adjustment *adj,
35                                     MIDI::Controllable *mc,
36                                     bool with_numeric)
37
38         : PixScroller (*adj, *pixset),
39           spin (*adj, 0, 2),
40           prompter (Gtk::WIN_POS_MOUSE, 30000, false),
41           midi_control (mc),
42           bind_button (2),
43           bind_statemask (Gdk::CONTROL_MASK)
44
45 {                         
46         pixset->generate ();
47
48         signal_button_press_event().connect (mem_fun (this, &SliderController::button_press));
49         spin.set_name ("SliderControllerValue");
50         spin.set_size_request (70,-1); // should be based on font size somehow
51         spin.set_numeric (true);
52         spin.set_snap_to_ticks (false);
53
54         prompter.signal_unmap_event().connect (mem_fun (*this, &SliderController::prompter_hiding));
55         
56         prompting = false;
57         unprompting = false;
58         
59         if (mc) {
60                 mc->learning_started.connect (mem_fun (*this, &SliderController::midicontrol_prompt));
61                 mc->learning_stopped.connect (mem_fun (*this, &SliderController::midicontrol_unprompt));
62         }
63 }
64
65 void
66 SliderController::set_value (float v)
67 {
68         adj.set_value (v);
69 }
70
71 void
72 SliderController::set_bind_button_state (guint button, guint statemask)
73 {
74         bind_button = button;
75         bind_statemask = statemask;
76 }
77
78 void
79 SliderController::get_bind_button_state (guint &button, guint &statemask)
80 {
81         button = bind_button;
82         statemask = bind_statemask;
83 }
84
85 void
86 SliderController::midi_learn()
87 {
88         if (midi_control) {
89                 prompting = true;
90                 midi_control->learn_about_external_control ();
91         }
92 }
93
94 bool
95 SliderController::button_press (GdkEventButton *ev)
96 {
97         if ((ev->state & bind_statemask) && ev->button == bind_button) { 
98                 midi_learn ();
99                 return true;
100         }
101
102         return false;
103 }
104
105 void
106 SliderController::midicontrol_set_tip ()
107
108 {
109         if (midi_control) {
110                 // Gtkmm2ext::UI::instance()->set_tip (this, midi_control->control_description());
111         }
112 }
113
114 bool
115 SliderController::prompter_hiding (GdkEventAny *ev)
116 {
117         if (unprompting) {
118                 if (midi_control) {
119                         midi_control->stop_learning();
120                 }
121                 unprompting = false;
122         }
123         
124         return false;
125 }
126
127 void
128 SliderController::midicontrol_prompt ()
129
130 {
131         if (prompting) {
132
133                 string prompt = _("operate MIDI controller now");
134                 prompter.set_text (prompt);
135                 Gtkmm2ext::UI::instance()->touch_display (&prompter);
136
137                 unprompting = true;
138                 prompting = false;
139         }
140 }
141
142 void
143 SliderController::midicontrol_unprompt ()
144
145 {
146         if (unprompting) {
147                 Gtkmm2ext::UI::instance()->touch_display (&prompter);
148                 unprompting = false;
149         }
150 }
151
152
153 VSliderController::VSliderController (Pix *pixset,
154                                       Gtk::Adjustment *adj,
155                                       MIDI::Controllable *mcontrol,
156                                       bool with_numeric)
157
158         : SliderController (pixset, adj, mcontrol, with_numeric)
159 {
160         if (with_numeric) {
161                 spin_frame.add (spin);
162                 spin_frame.set_shadow_type (Gtk::SHADOW_IN);
163                 spin_frame.set_name ("BaseFrame");
164                 spin_hbox.pack_start (spin_frame, false, true);
165                 // pack_start (spin_hbox, false, false);
166         }
167 }
168
169 HSliderController::HSliderController (Pix *pixset,
170                                       Gtk::Adjustment *adj,
171                                       MIDI::Controllable *mcontrol,
172                                       bool with_numeric)
173         
174         : SliderController (pixset, adj, mcontrol, with_numeric)
175 {
176         if (with_numeric) {
177                 spin_frame.add (spin);
178                 //spin_frame.set_shadow_type (Gtk::SHADOW_IN);
179                 spin_frame.set_name ("BaseFrame");
180                 spin_hbox.pack_start (spin_frame, false, true);
181                 // pack_start (spin_hbox, false, false);
182         }
183 }