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.
26 #include <midi++/controllable.h>
28 #include <gtkmm2ext/gtk_ui.h>
29 #include <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/barcontroller.h>
36 using namespace Gtkmm2ext;
38 BarController::BarController (Gtk::Adjustment& adj,
39 MIDI::Controllable *mc,
40 sigc::slot<void,char*,unsigned int> lc)
43 prompter (Gtk::WIN_POS_MOUSE, 30000, false),
48 bind_statemask (Gdk::CONTROL_MASK)
54 switch_on_release = false;
58 layout = darea.create_pango_layout("");
60 set_shadow_type (SHADOW_NONE);
62 initial_value = adjustment.get_value ();
64 adjustment.signal_value_changed().connect (mem_fun (*this, &Gtk::Widget::queue_draw));
65 adjustment.signal_changed().connect (mem_fun (*this, &Gtk::Widget::queue_draw));
67 darea.add_events (Gdk::BUTTON_RELEASE_MASK|
68 Gdk::BUTTON_PRESS_MASK|
69 Gdk::POINTER_MOTION_MASK|
70 Gdk::ENTER_NOTIFY_MASK|
71 Gdk::LEAVE_NOTIFY_MASK|
74 darea.signal_expose_event().connect (mem_fun (*this, &BarController::expose));
75 darea.signal_motion_notify_event().connect (mem_fun (*this, &BarController::motion));
76 darea.signal_button_press_event().connect (mem_fun (*this, &BarController::button_press));
77 darea.signal_button_release_event().connect (mem_fun (*this, &BarController::button_release));
78 darea.signal_scroll_event().connect (mem_fun (*this, &BarController::scroll));
80 prompter.signal_unmap_event().connect (mem_fun (*this, &BarController::prompter_hiding));
86 mc->learning_started.connect (mem_fun (*this, &BarController::midicontrol_prompt));
87 mc->learning_stopped.connect (mem_fun (*this, &BarController::midicontrol_unprompt));
90 spinner.signal_activate().connect (mem_fun (*this, &BarController::entry_activated));
91 spinner.signal_focus_out_event().connect (mem_fun (*this, &BarController::entry_focus_out));
92 spinner.set_digits (3);
99 BarController::set_bind_button_state (guint button, guint statemask)
101 bind_button = button;
102 bind_statemask = statemask;
106 BarController::get_bind_button_state (guint &button, guint &statemask)
108 button = bind_button;
109 statemask = bind_statemask;
114 BarController::button_press (GdkEventButton* ev)
116 switch (ev->button) {
118 if (ev->type == GDK_2BUTTON_PRESS) {
119 switch_on_release = true;
121 darea.remove_modal_grab();
123 switch_on_release = false;
124 darea.add_modal_grab();
127 grab_window = ev->window;
146 BarController::button_release (GdkEventButton* ev)
148 switch (ev->button) {
150 if (switch_on_release) {
151 Glib::signal_idle().connect (mem_fun (*this, &BarController::switch_to_spinner));
155 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
156 adjustment.set_value (initial_value);
160 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
162 } else if (ev->state & GDK_CONTROL_MASK) {
168 mouse_control (ev->x, ev->window, scale);
170 darea.remove_modal_grab();
177 if ((ev->state & bind_statemask) && bind_button == 2) {
181 fract = ev->x / (darea.get_width() - 2.0);
182 adjustment.set_value (adjustment.get_lower() +
183 fract * (adjustment.get_upper() - adjustment.get_lower()));
188 if ((ev->state & bind_statemask) && bind_button == 3) {
202 BarController::scroll (GdkEventScroll* ev)
206 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
208 } else if (ev->state & GDK_CONTROL_MASK) {
214 switch (ev->direction) {
216 case GDK_SCROLL_RIGHT:
217 adjustment.set_value (adjustment.get_value() + (scale * adjustment.get_step_increment()));
220 case GDK_SCROLL_DOWN:
221 case GDK_SCROLL_LEFT:
222 adjustment.set_value (adjustment.get_value() - (scale * adjustment.get_step_increment()));
230 BarController::motion (GdkEventMotion* ev)
238 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
242 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
244 } else if (ev->state & GDK_CONTROL_MASK) {
250 return mouse_control (ev->x, ev->window, scale);
254 BarController::mouse_control (double x, GdkWindow* window, double scaling)
259 if (window != grab_window) {
261 grab_window = window;
271 fract = scaling * (delta / (darea.get_width() - 2));
272 fract = min (1.0, fract);
273 fract = max (-1.0, fract);
274 adjustment.set_value (adjustment.get_value() + fract * (adjustment.get_upper() - adjustment.get_lower()));
286 BarController::expose (GdkEventExpose* event)
288 Glib::RefPtr<Gdk::Window> win (darea.get_window());
294 w = darea.get_width() - 2;
295 h = darea.get_height() - 2;
297 fract = ((adjustment.get_value() - adjustment.get_lower()) /
298 (adjustment.get_upper() - adjustment.get_lower()));
302 x1 = (gint) floor (w * fract);
308 parent = get_parent();
311 win->draw_rectangle (parent->get_style()->get_fg_gc (parent->get_state()),
313 0, 0, darea.get_width(), darea.get_height());
316 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
318 0, 0, darea.get_width(), darea.get_height());
322 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
323 true, x1, 1, 2, darea.get_height() - 2);
325 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
326 true, x1 - 1, 1, 3, darea.get_height() - 2);
335 x2 = (gint) floor (w * fract);
339 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
341 0, 0, darea.get_width() - 1, darea.get_height() - 1);
343 /* draw active box */
345 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
352 /* draw inactive box */
354 win->draw_rectangle (get_style()->get_fg_gc (STATE_INSENSITIVE),
377 label_callback (buf, 64);
379 if (buf[0] != '\0') {
381 layout->set_text (buf);
384 layout->get_pixel_size (width, height);
388 xpos = max (3, 1 + (x2 - (width/2)));
389 xpos = min (darea.get_width() - width - 3, xpos);
391 win->draw_layout (get_style()->get_text_gc (get_state()),
393 (darea.get_height()/2) - (height/2),
402 BarController::set_with_text (bool yn)
404 if (with_text != yn) {
411 BarController::midicontrol_set_tip ()
414 // Gtkmm2ext::UI::instance()->set_tip (&darea, midi_control->control_description());
419 BarController::midi_learn()
423 midi_control->learn_about_external_control ();
429 BarController::midicontrol_prompt ()
432 string prompt = _("operate MIDI controller now");
433 prompter.set_text (prompt);
434 Gtkmm2ext::UI::instance()->touch_display (&prompter);
442 BarController::midicontrol_unprompt ()
445 Gtkmm2ext::UI::instance()->touch_display (&prompter);
452 BarController::prompter_hiding (GdkEventAny *ev)
456 midi_control->stop_learning();
466 BarController::set_style (Style s)
473 BarController::switch_to_bar ()
481 if (get_child() == &darea) {
494 BarController::switch_to_spinner ()
502 if (get_child() == &spinner) {
509 spinner.grab_focus ();
516 BarController::entry_activated ()
518 string text = spinner.get_text ();
521 if (sscanf (text.c_str(), "%f", &val) == 1) {
522 adjustment.set_value (val);
529 BarController::entry_focus_out (GdkEventFocus* ev)
536 BarController::set_use_parent (bool yn)