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 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);
70 darea.signal_expose_event().connect (mem_fun (*this, &BarController::expose));
71 darea.signal_motion_notify_event().connect (mem_fun (*this, &BarController::motion));
72 darea.signal_button_press_event().connect (mem_fun (*this, &BarController::button_press));
73 darea.signal_button_release_event().connect (mem_fun (*this, &BarController::button_release));
75 prompter.signal_unmap_event().connect (mem_fun (*this, &BarController::prompter_hiding));
81 mc->learning_started.connect (mem_fun (*this, &BarController::midicontrol_prompt));
82 mc->learning_stopped.connect (mem_fun (*this, &BarController::midicontrol_unprompt));
85 spinner.signal_activate().connect (mem_fun (*this, &BarController::entry_activated));
86 spinner.signal_focus_out_event().connect (mem_fun (*this, &BarController::entry_focus_out));
87 spinner.set_digits (3);
94 BarController::set_bind_button_state (guint button, guint statemask)
97 bind_statemask = statemask;
101 BarController::get_bind_button_state (guint &button, guint &statemask)
103 button = bind_button;
104 statemask = bind_statemask;
109 BarController::button_press (GdkEventButton* ev)
111 switch (ev->button) {
113 if (ev->type == GDK_2BUTTON_PRESS) {
114 switch_on_release = true;
116 switch_on_release = false;
117 darea.add_modal_grab();
120 grab_window = ev->window;
139 BarController::button_release (GdkEventButton* ev)
141 switch (ev->button) {
143 if (switch_on_release) {
144 Glib::signal_idle().connect (mem_fun (*this, &BarController::switch_to_spinner));
148 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
149 adjustment.set_value (initial_value);
153 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
155 } else if (ev->state & GDK_CONTROL_MASK) {
161 mouse_control (ev->x, ev->window, scale);
164 darea.remove_modal_grab();
169 if ((ev->state & bind_statemask) && bind_button == 2) {
173 fract = ev->x / (darea.get_width() - 2.0);
174 adjustment.set_value (adjustment.get_lower() +
175 fract * (adjustment.get_upper() - adjustment.get_lower()));
180 if ((ev->state & bind_statemask) && bind_button == 3) {
187 adjustment.set_value (adjustment.get_value() +
188 adjustment.get_step_increment());
191 adjustment.set_value (adjustment.get_value() -
192 adjustment.get_step_increment());
200 BarController::motion (GdkEventMotion* ev)
208 if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) {
212 if (ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
214 } else if (ev->state & GDK_CONTROL_MASK) {
220 return mouse_control (ev->x, ev->window, scale);
224 BarController::mouse_control (double x, GdkWindow* window, double scaling)
229 if (window != grab_window) {
231 grab_window = window;
241 fract = scaling * (delta / (darea.get_width() - 2));
242 fract = min (1.0, fract);
243 fract = max (-1.0, fract);
244 adjustment.set_value (adjustment.get_value() + fract * (adjustment.get_upper() - adjustment.get_lower()));
256 BarController::expose (GdkEventExpose* event)
258 Glib::RefPtr<Gdk::Window> win (darea.get_window());
264 w = darea.get_width() - 2;
265 h = darea.get_height() - 2;
267 fract = ((adjustment.get_value() - adjustment.get_lower()) /
268 (adjustment.get_upper() - adjustment.get_lower()));
272 x1 = (gint) floor (w * fract);
278 parent = get_parent();
281 win->draw_rectangle (parent->get_style()->get_fg_gc (parent->get_state()),
283 0, 0, darea.get_width(), darea.get_height());
286 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
288 0, 0, darea.get_width(), darea.get_height());
292 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
293 true, x1, 1, 2, darea.get_height() - 2);
295 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
296 true, x1 - 1, 1, 3, darea.get_height() - 2);
305 x2 = (gint) floor (w * fract);
309 win->draw_rectangle (get_style()->get_bg_gc (get_state()),
311 0, 0, darea.get_width(), darea.get_height());
313 /* draw active box */
315 win->draw_rectangle (get_style()->get_fg_gc (get_state()),
322 /* draw inactive box */
324 win->draw_rectangle (get_style()->get_fg_gc (STATE_INSENSITIVE),
347 label_callback (buf, 64);
349 if (buf[0] != '\0') {
350 int width = 0, height;
351 darea.create_pango_layout(buf)->get_pixel_size(width, height);
352 darea.set_size_request(width + 2, -1);
359 BarController::set_with_text (bool yn)
361 if (with_text != yn) {
368 BarController::midicontrol_set_tip ()
371 // Gtkmm2ext::UI::instance()->set_tip (&darea, midi_control->control_description());
376 BarController::midi_learn()
380 midi_control->learn_about_external_control ();
386 BarController::midicontrol_prompt ()
389 string prompt = _("operate MIDI controller now");
390 prompter.set_text (prompt);
391 Gtkmm2ext::UI::instance()->touch_display (&prompter);
399 BarController::midicontrol_unprompt ()
402 Gtkmm2ext::UI::instance()->touch_display (&prompter);
409 BarController::prompter_hiding (GdkEventAny *ev)
413 midi_control->stop_learning();
423 BarController::set_style (Style s)
430 BarController::switch_to_bar ()
438 if (get_child() == &darea) {
451 BarController::switch_to_spinner ()
459 if (get_child() == &spinner) {
466 spinner.grab_focus ();
473 BarController::entry_activated ()
475 string text = spinner.get_text ();
478 if (sscanf (text.c_str(), "%f", &val) == 1) {
479 adjustment.set_value (val);
486 BarController::entry_focus_out (GdkEventFocus* ev)
493 BarController::set_use_parent (bool yn)