4305c22738fcacad2727a5697191f99688e7c6ee
[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         signal_button_press_event().connect (mem_fun (this, &SliderController::button_press));
47         spin.set_name ("SliderControllerValue");
48         spin.set_size_request (70,-1); // should be based on font size somehow
49         spin.set_numeric (true);
50         spin.set_snap_to_ticks (false);
51
52         prompter.signal_unmap_event().connect (mem_fun (*this, &SliderController::prompter_hiding));
53         
54         prompting = false;
55         unprompting = false;
56         
57         if (mc) {
58                 mc->learning_started.connect (mem_fun (*this, &SliderController::midicontrol_prompt));
59                 mc->learning_stopped.connect (mem_fun (*this, &SliderController::midicontrol_unprompt));
60         }
61 }
62
63 void
64 SliderController::set_value (float v)
65 {
66         adj.set_value (v);
67 }
68
69 void
70 SliderController::set_bind_button_state (guint button, guint statemask)
71 {
72         bind_button = button;
73         bind_statemask = statemask;
74 }
75
76 void
77 SliderController::get_bind_button_state (guint &button, guint &statemask)
78 {
79         button = bind_button;
80         statemask = bind_statemask;
81 }
82
83 void
84 SliderController::midi_learn()
85 {
86         if (midi_control) {
87                 prompting = true;
88                 midi_control->learn_about_external_control ();
89         }
90 }
91
92 bool
93 SliderController::button_press (GdkEventButton *ev)
94 {
95         if ((ev->state & bind_statemask) && ev->button == bind_button) { 
96                 midi_learn ();
97                 return true;
98         }
99
100         return false;
101 }
102
103 void
104 SliderController::midicontrol_set_tip ()
105
106 {
107         if (midi_control) {
108                 // Gtkmm2ext::UI::instance()->set_tip (this, midi_control->control_description());
109         }
110 }
111
112 bool
113 SliderController::prompter_hiding (GdkEventAny *ev)
114 {
115         if (unprompting) {
116                 if (midi_control) {
117                         midi_control->stop_learning();
118                 }
119                 unprompting = false;
120         }
121         
122         return false;
123 }
124
125 void
126 SliderController::midicontrol_prompt ()
127
128 {
129         if (prompting) {
130
131                 string prompt = _("operate MIDI controller now");
132                 prompter.set_text (prompt);
133                 Gtkmm2ext::UI::instance()->touch_display (&prompter);
134
135                 unprompting = true;
136                 prompting = false;
137         }
138 }
139
140 void
141 SliderController::midicontrol_unprompt ()
142
143 {
144         if (unprompting) {
145                 Gtkmm2ext::UI::instance()->touch_display (&prompter);
146                 unprompting = false;
147         }
148 }
149
150
151 VSliderController::VSliderController (Pix *pixset,
152                                       Gtk::Adjustment *adj,
153                                       MIDI::Controllable *mcontrol,
154                                       bool with_numeric)
155
156         : SliderController (pixset, adj, mcontrol, with_numeric)
157 {
158         if (with_numeric) {
159                 spin_frame.add (spin);
160                 spin_frame.set_shadow_type (Gtk::SHADOW_IN);
161                 spin_frame.set_name ("BaseFrame");
162                 spin_hbox.pack_start (spin_frame, false, true);
163                 // pack_start (spin_hbox, false, false);
164         }
165 }
166
167 HSliderController::HSliderController (Pix *pixset,
168                                       Gtk::Adjustment *adj,
169                                       MIDI::Controllable *mcontrol,
170                                       bool with_numeric)
171         
172         : SliderController (pixset, adj, mcontrol, with_numeric)
173 {
174         if (with_numeric) {
175                 spin_frame.add (spin);
176                 //spin_frame.set_shadow_type (Gtk::SHADOW_IN);
177                 spin_frame.set_name ("BaseFrame");
178                 spin_hbox.pack_start (spin_frame, false, true);
179                 // pack_start (spin_hbox, false, false);
180         }
181 }