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/barcontroller.h>
35 using namespace Gtkmm2ext;
37 BarController::BarController (Gtk::Adjustment& adj,
38 MIDI::Controllable *mc,
39 sigc::slot<void,char*,unsigned int> lc)
42 prompter (Gtk::WIN_POS_MOUSE, 30000, false),
47 bind_statemask (Gdk::CONTROL_MASK)
53 switch_on_release = false;
57 layout = darea.create_pango_layout("");
59 set_shadow_type (SHADOW_NONE);
61 initial_value = adjustment.get_value ();
63 adjustment.signal_value_changed().connect (mem_fun (*this, &Gtk::Widget::queue_draw));
64 adjustment.signal_changed().connect (mem_fun (*this, &Gtk::Widget::queue_draw));
66 darea.add_events (Gdk::BUTTON_RELEASE_MASK|
67 Gdk::BUTTON_PRESS_MASK|
68 Gdk::POINTER_MOTION_MASK|
69 Gdk::ENTER_NOTIFY_MASK|
70 Gdk::LEAVE_NOTIFY_MASK);
72 darea.signal_expose_event().connect (mem_fun (*this, &BarController::expose));
73 darea.signal_motion_notify_event().connect (mem_fun (*this, &BarController::motion));
74 darea.signal_button_press_event().connect (mem_fun (*this, &BarController::button_press));
75 darea.signal_button_release_event().connect (mem_fun (*this, &BarController::button_release));
77 prompter.signal_unmap_event().connect (mem_fun (*this, &BarController::prompter_hiding));
83 mc->learning_started.connect (mem_fun (*this, &BarController::midicontrol_prompt));
84 mc->learning_stopped.connect (mem_fun (*this, &BarController::midicontrol_unprompt));
87 spinner.signal_activate().connect (mem_fun (*this, &BarController::entry_activated));
88 spinner.signal_focus_out_event().connect (mem_fun (*this, &BarController::entry_focus_out));
89 spinner.set_digits (3);
96 BarController::set_bind_button_state (guint button, guint statemask)
99 bind_statemask = statemask;
103 BarController::get_bind_button_state (guint &button, guint &statemask)
105 button = bind_button;
106 statemask = bind_statemask;
111 BarController::button_press (GdkEventButton* ev)
113 switch (ev->button) {
115 if (ev->type == GDK_2BUTTON_PRESS) {
116 switch_on_release = true;
118 switch_on_release = false;
119 darea.add_modal_grab();
122 grab_window = ev->window;
141 BarController::button_release (GdkEventButton* ev)
143 switch (ev->button) {
145 if (switch_on_release) {
146 Glib::signal_idle().connect (mem_fun (*this, &BarController::switch_to_spinner));
150 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
151 adjustment.set_value (initial_value);
155 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
157 } else if (ev->state & GDK_CONTROL_MASK) {
163 mouse_control (ev->x, ev->window, scale);
166 darea.remove_modal_grab();
171 if ((ev->state & bind_statemask) && bind_button == 2) {
175 fract = ev->x / (darea.get_width() - 2.0);
176 adjustment.set_value (adjustment.get_lower() +
177 fract * (adjustment.get_upper() - adjustment.get_lower()));
182 if ((ev->state & bind_statemask) && bind_button == 3) {
189 adjustment.set_value (adjustment.get_value() +
190 adjustment.get_step_increment());
193 adjustment.set_value (adjustment.get_value() -
194 adjustment.get_step_increment());
202 BarController::motion (GdkEventMotion* ev)
210 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
214 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
216 } else if (ev->state & GDK_CONTROL_MASK) {
222 return mouse_control (ev->x, ev->window, scale);
226 BarController::mouse_control (double x, GdkWindow* window, double scaling)
231 if (window != grab_window) {
233 grab_window = window;
243 fract = scaling * (delta / (darea.get_width() - 2));
244 fract = min (1.0, fract);
245 fract = max (-1.0, fract);
246 adjustment.set_value (adjustment.get_value() + fract * (adjustment.get_upper() - adjustment.get_lower()));
258 BarController::expose (GdkEventExpose* event)
260 Glib::RefPtr<Gdk::Window> win (darea.get_window());
266 w = darea.get_width() - 2;
267 h = darea.get_height() - 2;
269 fract = ((adjustment.get_value() - adjustment.get_lower()) /
270 (adjustment.get_upper() - adjustment.get_lower()));
274 x1 = (gint) floor (w * fract);
280 parent = get_parent();
283 win->draw_rectangle (parent->get_style()->get_fg_gc (parent->get_state()),
285 0, 0, darea.get_width(), darea.get_height());
288 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
290 0, 0, darea.get_width(), darea.get_height());
294 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
295 true, x1, 1, 2, darea.get_height() - 2);
297 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
298 true, x1 - 1, 1, 3, darea.get_height() - 2);
307 x2 = (gint) floor (w * fract);
311 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
313 0, 0, darea.get_width() - 1, darea.get_height() - 1);
315 /* draw active box */
317 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
324 /* draw inactive box */
326 win->draw_rectangle (get_style()->get_fg_gc (STATE_INSENSITIVE),
349 label_callback (buf, 64);
351 if (buf[0] != '\0') {
356 layout->set_text (buf);
357 layout->get_pixel_size(width, height);
361 xpos = max (3, 1 + (x2 - (width/2)));
362 xpos = min (darea.get_width() - width - 3, xpos);
364 win->draw_layout (get_style()->get_text_gc (get_state()),
366 (darea.get_height()/2) - (height/2),
375 BarController::set_with_text (bool yn)
377 if (with_text != yn) {
384 BarController::midicontrol_set_tip ()
387 // Gtkmm2ext::UI::instance()->set_tip (&darea, midi_control->control_description());
392 BarController::midi_learn()
396 midi_control->learn_about_external_control ();
402 BarController::midicontrol_prompt ()
405 string prompt = _("operate MIDI controller now");
406 prompter.set_text (prompt);
407 Gtkmm2ext::UI::instance()->touch_display (&prompter);
415 BarController::midicontrol_unprompt ()
418 Gtkmm2ext::UI::instance()->touch_display (&prompter);
425 BarController::prompter_hiding (GdkEventAny *ev)
429 midi_control->stop_learning();
439 BarController::set_style (Style s)
446 BarController::switch_to_bar ()
454 if (get_child() == &darea) {
467 BarController::switch_to_spinner ()
475 if (get_child() == &spinner) {
482 spinner.grab_focus ();
489 BarController::entry_activated ()
491 string text = spinner.get_text ();
494 if (sscanf (text.c_str(), "%f", &val) == 1) {
495 adjustment.set_value (val);
502 BarController::entry_focus_out (GdkEventFocus* ev)
509 BarController::set_use_parent (bool yn)