revert VST debugging hacks
[ardour2.git] / libs / gtkmm2ext / barcontroller.cc
blob4318b1df2896d97410f2e1fe6b64328258a29c4e
1 /*
2 Copyright (C) 2004 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.
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.
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.
17 $Id$
20 #include <string>
21 #include <climits>
22 #include <cstdio>
23 #include <cmath>
24 #include <algorithm>
26 #include <pbd/controllable.h>
28 #include <gtkmm2ext/gtk_ui.h>
29 #include <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/barcontroller.h>
32 #include "i18n.h"
34 using namespace std;
35 using namespace Gtk;
36 using namespace Gtkmm2ext;
38 BarController::BarController (Gtk::Adjustment& adj,
39 PBD::Controllable& mc,
40 sigc::slot<void,char*,unsigned int> lc)
42 : adjustment (adj),
43 binding_proxy (&mc),
44 label_callback (lc),
45 spinner (adjustment)
48 _style = LeftToRight;
49 grabbed = false;
50 switching = false;
51 switch_on_release = false;
52 with_text = true;
53 use_parent = false;
55 layout = darea.create_pango_layout("");
57 set_shadow_type (SHADOW_NONE);
59 initial_value = adjustment.get_value ();
61 adjustment.signal_value_changed().connect (mem_fun (*this, &Gtk::Widget::queue_draw));
62 adjustment.signal_changed().connect (mem_fun (*this, &Gtk::Widget::queue_draw));
64 darea.add_events (Gdk::BUTTON_RELEASE_MASK|
65 Gdk::BUTTON_PRESS_MASK|
66 Gdk::POINTER_MOTION_MASK|
67 Gdk::ENTER_NOTIFY_MASK|
68 Gdk::LEAVE_NOTIFY_MASK|
69 Gdk::SCROLL_MASK);
71 darea.signal_expose_event().connect (mem_fun (*this, &BarController::expose));
72 darea.signal_motion_notify_event().connect (mem_fun (*this, &BarController::motion));
73 darea.signal_button_press_event().connect (mem_fun (*this, &BarController::button_press), false);
74 darea.signal_button_release_event().connect (mem_fun (*this, &BarController::button_release), false);
75 darea.signal_scroll_event().connect (mem_fun (*this, &BarController::scroll));
77 spinner.signal_activate().connect (mem_fun (*this, &BarController::entry_activated));
78 spinner.signal_focus_out_event().connect (mem_fun (*this, &BarController::entry_focus_out));
79 spinner.set_digits (3);
81 add (darea);
82 show_all ();
85 void
86 BarController::drop_grab ()
88 if (grabbed) {
89 grabbed = false;
90 darea.remove_modal_grab();
91 StopGesture ();
95 bool
96 BarController::button_press (GdkEventButton* ev)
98 double fract;
100 if (binding_proxy.button_press_handler (ev)) {
101 return true;
104 switch (ev->button) {
105 case 1:
106 if (ev->type == GDK_2BUTTON_PRESS) {
107 switch_on_release = true;
108 drop_grab ();
109 } else {
110 switch_on_release = false;
111 darea.add_modal_grab();
112 grabbed = true;
113 grab_x = ev->x;
114 grab_window = ev->window;
115 StartGesture ();
117 return true;
118 break;
120 case 2:
121 fract = ev->x / (darea.get_width() - 2.0);
122 adjustment.set_value (adjustment.get_lower() + fract * (adjustment.get_upper() - adjustment.get_lower()));
124 case 3:
125 break;
127 case 4:
128 case 5:
129 break;
132 return false;
135 bool
136 BarController::button_release (GdkEventButton* ev)
138 drop_grab ();
140 switch (ev->button) {
141 case 1:
142 if (switch_on_release) {
143 Glib::signal_idle().connect (mem_fun (*this, &BarController::switch_to_spinner));
144 return true;
147 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
148 adjustment.set_value (initial_value);
149 } else {
150 double scale;
152 if ((ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
153 scale = 0.01;
154 } else if (ev->state & GDK_CONTROL_MASK) {
155 scale = 0.1;
156 } else {
157 scale = 1.0;
160 mouse_control (ev->x, ev->window, scale);
162 break;
164 case 2:
165 break;
167 case 3:
168 return false;
170 default:
171 break;
174 return true;
177 bool
178 BarController::scroll (GdkEventScroll* ev)
180 double scale;
182 if ((ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
183 scale = 0.01;
184 } else if (ev->state & GDK_CONTROL_MASK) {
185 scale = 0.1;
186 } else {
187 scale = 1.0;
190 switch (ev->direction) {
191 case GDK_SCROLL_UP:
192 case GDK_SCROLL_RIGHT:
193 adjustment.set_value (adjustment.get_value() + (scale * adjustment.get_step_increment()));
194 break;
196 case GDK_SCROLL_DOWN:
197 case GDK_SCROLL_LEFT:
198 adjustment.set_value (adjustment.get_value() - (scale * adjustment.get_step_increment()));
199 break;
202 return true;
205 bool
206 BarController::motion (GdkEventMotion* ev)
208 double scale;
210 if (!grabbed) {
211 return true;
214 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
215 return TRUE;
218 if ((ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
219 scale = 0.01;
220 } else if (ev->state & GDK_CONTROL_MASK) {
221 scale = 0.1;
222 } else {
223 scale = 1.0;
226 return mouse_control (ev->x, ev->window, scale);
229 gint
230 BarController::mouse_control (double x, GdkWindow* window, double scaling)
232 double fract = 0.0;
233 double delta;
235 if (window != grab_window) {
236 grab_x = x;
237 grab_window = window;
238 return TRUE;
241 delta = x - grab_x;
242 grab_x = x;
244 switch (_style) {
245 case Line:
246 case LeftToRight:
247 fract = scaling * (delta / (darea.get_width() - 2));
248 fract = min (1.0, fract);
249 fract = max (-1.0, fract);
250 adjustment.set_value (adjustment.get_value() + fract * (adjustment.get_upper() - adjustment.get_lower()));
251 break;
253 default:
254 fract = 0.0;
258 return TRUE;
261 bool
262 BarController::expose (GdkEventExpose* event)
264 Glib::RefPtr<Gdk::Window> win (darea.get_window());
265 Widget* parent;
266 gint x1=0, x2=0, y1=0, y2=0;
267 gint w, h;
268 double fract;
270 fract = ((adjustment.get_value() - adjustment.get_lower()) /
271 (adjustment.get_upper() - adjustment.get_lower()));
273 switch (_style) {
274 case Line:
275 w = darea.get_width() - 1;
276 h = darea.get_height();
277 x1 = (gint) floor (w * fract);
278 x2 = x1;
279 y1 = 0;
280 y2 = h - 1;
282 if (use_parent) {
283 parent = get_parent();
285 if (parent) {
286 win->draw_rectangle (parent->get_style()->get_fg_gc (parent->get_state()),
287 true,
288 0, 0, darea.get_width(), darea.get_height());
291 } else {
293 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
294 true,
295 0, 0, darea.get_width() - ((darea.get_width()+1) % 2), darea.get_height());
298 win->draw_line (get_style()->get_fg_gc (get_state()), x1, 0, x1, h);
299 break;
301 case CenterOut:
302 break;
304 case LeftToRight:
306 w = darea.get_width() - 2;
307 h = darea.get_height() - 2;
309 x1 = 0;
310 x2 = (gint) floor (w * fract);
311 y1 = 0;
312 y2 = h - 1;
314 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
315 false,
316 0, 0, darea.get_width() - 1, darea.get_height() - 1);
318 /* draw active box */
320 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
321 true,
322 1 + x1,
323 1 + y1,
325 1 + y2);
327 /* draw inactive box */
329 win->draw_rectangle (get_style()->get_fg_gc (STATE_INSENSITIVE),
330 true,
331 1 + x2,
332 1 + y1,
333 w - x2,
334 1 + y2);
336 break;
338 case RightToLeft:
339 break;
340 case TopToBottom:
341 break;
342 case BottomToTop:
343 break;
346 if (with_text) {
347 /* draw label */
349 char buf[64];
350 buf[0] = '\0';
352 label_callback (buf, 64);
354 if (buf[0] != '\0') {
356 layout->set_text (buf);
358 int width, height;
359 layout->get_pixel_size (width, height);
361 int xpos;
363 xpos = max (3, 1 + (x2 - (width/2)));
364 xpos = min (darea.get_width() - width - 3, xpos);
366 win->draw_layout (get_style()->get_text_gc (get_state()),
367 xpos,
368 (darea.get_height()/2) - (height/2),
369 layout);
373 return true;
376 void
377 BarController::set_with_text (bool yn)
379 if (with_text != yn) {
380 with_text = yn;
381 queue_draw ();
385 void
386 BarController::set_style (Style s)
388 _style = s;
389 darea.queue_draw ();
392 gint
393 BarController::switch_to_bar ()
395 if (switching) {
396 return FALSE;
399 switching = true;
401 if (get_child() == &darea) {
402 return FALSE;
405 remove ();
406 add (darea);
407 darea.show ();
409 switching = false;
410 return FALSE;
413 gint
414 BarController::switch_to_spinner ()
416 if (switching) {
417 return FALSE;
420 switching = true;
422 if (get_child() == &spinner) {
423 return FALSE;
426 remove ();
427 add (spinner);
428 spinner.show ();
429 spinner.select_region (0, spinner.get_text_length());
430 spinner.grab_focus ();
432 switching = false;
433 return FALSE;
436 void
437 BarController::entry_activated ()
439 string text = spinner.get_text ();
440 float val;
442 if (sscanf (text.c_str(), "%f", &val) == 1) {
443 adjustment.set_value (val);
446 switch_to_bar ();
449 bool
450 BarController::entry_focus_out (GdkEventFocus* ev)
452 entry_activated ();
453 return true;
456 void
457 BarController::set_use_parent (bool yn)
459 use_parent = yn;
460 queue_draw ();
463 void
464 BarController::set_sensitive (bool yn)
466 Frame::set_sensitive (yn);
467 darea.set_sensitive (yn);