2 Copyright (C) 2010-2011 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 $Id: motionfeedback.cc,v 1.5 2004/03/01 03:44:19 pauld Exp $
26 #include <stdio.h> /* for snprintf, grrr */
28 #include <glib/gstdio.h>
30 #include <gdk/gdkkeysyms.h>
33 #include "pbd/controllable.h"
35 #include "gtkmm2ext/motionfeedback.h"
36 #include "gtkmm2ext/keyboard.h"
37 #include "gtkmm2ext/prolooks-helpers.h"
38 #include "gtkmm2ext/gui_thread.h"
41 using namespace Gtkmm2ext;
44 Gdk::Color* MotionFeedback::base_color;
46 MotionFeedback::MotionFeedback (Glib::RefPtr<Gdk::Pixbuf> pix,
48 boost::shared_ptr<PBD::Controllable> c,
50 double step_increment,
51 double page_increment,
52 const char *widget_name,
53 bool with_numeric_display,
58 , default_value (default_val)
59 , step_inc (step_increment)
60 , page_inc (page_increment)
68 base_color = new Gdk::Color ("#1a5274");
71 char value_name[1024];
73 print_func = default_printer;
77 HBox* hpacker = manage (new HBox);
78 hpacker->pack_start (pixwin, true, true);
80 pack_start (*hpacker, false, false);
83 if (with_numeric_display) {
85 value_packer = new EventBox;
86 value_packer->set_name ("MotionControllerValue");
87 value_packer->show ();
88 value_packer->set_border_width (6);
91 value->set_justify (Gtk::JUSTIFY_RIGHT);
94 value_packer->add (*value);
96 hpacker = manage (new HBox);
97 hpacker->pack_start (*value_packer, true, false);
99 hpacker->set_border_width (6);
101 pack_start (*hpacker, false, false);
104 snprintf (value_name, sizeof(value_name), "%sValue", widget_name);
105 value->set_name (value_name);
110 print_func (buf, _controllable, print_arg);
111 value->set_text (buf);
115 pixwin.set_events (Gdk::BUTTON_PRESS_MASK|
116 Gdk::BUTTON_RELEASE_MASK|
117 Gdk::POINTER_MOTION_MASK|
118 Gdk::ENTER_NOTIFY_MASK|
119 Gdk::LEAVE_NOTIFY_MASK|
122 Gdk::KEY_RELEASE_MASK);
124 pixwin.set_flags (CAN_FOCUS);
126 /* Proxy all important events on the pixwin to ourselves */
128 pixwin.signal_button_press_event().connect(mem_fun (*this,&MotionFeedback::pixwin_button_press_event));
129 pixwin.signal_button_release_event().connect(mem_fun (*this,&MotionFeedback::pixwin_button_release_event));
130 pixwin.signal_motion_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_motion_notify_event));
131 pixwin.signal_enter_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_enter_notify_event));
132 pixwin.signal_leave_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_leave_notify_event));
133 pixwin.signal_key_press_event().connect(mem_fun (*this,&MotionFeedback::pixwin_key_press_event));
134 pixwin.signal_scroll_event().connect(mem_fun (*this,&MotionFeedback::pixwin_scroll_event));
135 pixwin.signal_expose_event().connect(mem_fun (*this,&MotionFeedback::pixwin_expose_event), true);
136 pixwin.signal_size_request().connect(mem_fun (*this,&MotionFeedback::pixwin_size_request));
139 MotionFeedback::~MotionFeedback()
146 MotionFeedback::pixwin_button_press_event (GdkEventButton *ev)
148 if (binding_proxy.button_press_handler (ev)) {
152 switch (ev->button) {
154 grab_is_fine = false;
163 gtk_grab_add(GTK_WIDGET(pixwin.gobj()));
165 grabbed_y = ev->y_root;
166 grabbed_x = ev->x_root;
172 MotionFeedback::pixwin_button_release_event (GdkEventButton *ev)
174 if (!_controllable) {
178 switch (ev->button) {
180 if (pixwin.has_grab()) {
183 (GTK_WIDGET(pixwin.gobj()));
186 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
187 /* shift click back to the default */
188 _controllable->set_value (default_value);
190 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
191 /* ctrl click back to the minimum value */
192 _controllable->set_value (_controllable->lower ());
197 if (pixwin.has_grab()) {
200 (GTK_WIDGET(pixwin.gobj()));
206 return VBox::on_button_release_event (ev);
210 MotionFeedback::pixwin_motion_notify_event (GdkEventMotion *ev)
212 if (!_controllable) {
220 if (!pixwin.has_grab()) {
221 return VBox::on_motion_notify_event (ev);
224 multiplier = ((ev->state & Keyboard::TertiaryModifier) ? 100 : 1) *
225 ((ev->state & Keyboard::PrimaryModifier) ? 10 : 1) *
226 ((ev->state & Keyboard::SecondaryModifier) ? 0.1 : 1);
228 if (ev->state & Gdk::BUTTON1_MASK) {
230 /* vertical control */
232 y_delta = grabbed_y - ev->y_root;
233 grabbed_y = ev->y_root;
235 x_delta = ev->x_root - grabbed_x;
237 if (y_delta == 0) return TRUE;
239 y_delta *= 1 + (x_delta/100);
240 y_delta *= multiplier;
243 _controllable->set_value (adjust ((grab_is_fine ? step_inc : page_inc) * y_delta));
245 } else if (ev->state & Gdk::BUTTON2_MASK) {
249 double x = ev->x - subwidth/2;
250 double y = - ev->y + subwidth/2;
251 double angle = std::atan2 (y, x) / M_PI;
257 angle = -(2.0/3.0) * (angle - 1.25);
260 _controllable->set_value (to_control_value (angle));
268 MotionFeedback::pixwin_enter_notify_event (GdkEventCrossing*)
275 MotionFeedback::pixwin_leave_notify_event (GdkEventCrossing*)
277 pixwin.unset_flags (HAS_FOCUS);
282 MotionFeedback::pixwin_key_press_event (GdkEventKey *ev)
284 if (!_controllable) {
291 multiplier = ((ev->state & Keyboard::TertiaryModifier) ? 100.0 : 1.0) *
292 ((ev->state & Keyboard::SecondaryModifier) ? 10.0 : 1.0) *
293 ((ev->state & Keyboard::PrimaryModifier) ? 2.0 : 1.0);
295 switch (ev->keyval) {
298 _controllable->set_value (adjust (multiplier * page_inc));
303 _controllable->set_value (adjust (-multiplier * page_inc));
308 _controllable->set_value (adjust (multiplier * step_inc));
313 _controllable->set_value (adjust (-multiplier * step_inc));
318 _controllable->set_value (_controllable->lower());
323 _controllable->set_value (_controllable->upper());
331 MotionFeedback::pixwin_expose_event (GdkEventExpose*)
333 if (!_controllable) {
337 GdkWindow *window = pixwin.get_window()->gobj();
338 double display_val = to_display_value (_controllable->get_value());
339 int32_t phase = lrint (display_val * 64.0);
341 // skip middle phase except for true middle value
343 if (type == Rotary && phase == 32) {
344 double pt = (display_val * 2.0) - 1.0;
351 // endless knob: skip 90deg highlights unless the value is really a multiple of 90deg
353 if (type == Endless && !(phase % 16)) {
358 double nom = phase / 64.0;
359 double diff = display_val - nom;
362 phase = (phase + 1) % 64;
364 phase = (phase + 63) % 64;
367 phase = std::min (phase, (int32_t) 63);
369 GtkWidget* widget = GTK_WIDGET(pixwin.gobj());
370 gdk_draw_pixbuf (GDK_DRAWABLE(window), widget->style->fg_gc[0],
372 phase * subwidth, type * subheight,
373 /* center image in allocated area */
374 (get_width() - subwidth)/2,
376 subwidth, subheight, GDK_RGB_DITHER_NORMAL, 0, 0);
382 MotionFeedback::pixwin_scroll_event (GdkEventScroll* ev)
386 if (!_controllable) {
390 if (ev->state & Keyboard::GainFineScaleModifier) {
391 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
400 switch (ev->direction) {
402 case GDK_SCROLL_RIGHT:
403 _controllable->set_value (adjust (scale * page_inc));
406 case GDK_SCROLL_DOWN:
407 case GDK_SCROLL_LEFT:
408 _controllable->set_value (adjust (-scale * page_inc));
416 MotionFeedback::pixwin_size_request (GtkRequisition* req)
418 req->width = subwidth;
419 req->height = subheight;
424 MotionFeedback::controllable_value_changed ()
428 print_func (buf, _controllable, print_arg);
429 value->set_text (buf);
432 pixwin.queue_draw ();
436 MotionFeedback::set_controllable (boost::shared_ptr<PBD::Controllable> c)
439 binding_proxy.set_controllable (c);
440 controller_connection.disconnect ();
443 c->Changed.connect (controller_connection, MISSING_INVALIDATOR, boost::bind (&MotionFeedback::controllable_value_changed, this), gui_context());
446 print_func (buf, _controllable, print_arg);
447 value->set_text (buf);
450 pixwin.queue_draw ();
453 boost::shared_ptr<PBD::Controllable>
454 MotionFeedback::controllable () const
456 return _controllable;
460 MotionFeedback::default_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void *)
463 sprintf (buf, "%.2f", c->get_value());
469 Glib::RefPtr<Gdk::Pixbuf>
470 MotionFeedback::render_pixbuf (int size)
472 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
475 GError *error = NULL;
477 fd = g_file_open_tmp ("mfimgXXXXXX", &path, &error);
481 g_critical("failed to open a temporary file for writing: %s.", error->message);
482 g_error_free (error);
486 GdkColor col2 = {0,0,0,0};
487 GdkColor col3 = {0,0,0,0};
492 hsv = prolooks_hsv_new_for_gdk_color (base_color->gobj());
493 bright = (prolooks_hsv_to_gdk_color (hsv, &col2), col2);
494 prolooks_hsv_set_saturation (hsv, 0.66);
495 prolooks_hsv_set_value (hsv, 0.67);
496 dark = (prolooks_hsv_to_gdk_color (hsv, &col3), col3);
498 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size * 64, size);
499 cairo_t* cr = cairo_create (surface);
501 for (int i = 0; i < 64; ++i) {
503 core_draw (cr, i, size, 20, size*i, 0, &bright, &dark);
507 if (cairo_surface_write_to_png (surface, path) != CAIRO_STATUS_SUCCESS) {
508 std::cerr << "could not save image set to " << path << std::endl;
515 cairo_surface_destroy (surface);
518 pixbuf = Gdk::Pixbuf::create_from_file (path);
519 } catch (const Gdk::PixbufError &e) {
520 std::cerr << "Caught PixbufError: " << e.what() << std::endl;
525 g_message("Caught ... ");
537 MotionFeedback::core_draw (cairo_t* cr, int phase, double size, double progress_width, double xorigin, double yorigin,
538 const GdkColor* bright, const GdkColor* dark)
548 double start_angle_x;
549 double start_angle_y;
552 double progress_radius;
553 double progress_radius_inner;
554 double progress_radius_outer;
555 double knob_disc_radius;
556 cairo_pattern_t* pattern;
557 double progress_rim_width;
558 cairo_pattern_t* progress_shine;
560 cairo_pattern_t* knob_ripples;
564 g_return_if_fail (cr != NULL);
566 progress_radius = 40.0;
567 progress_radius_inner = progress_radius - (progress_width / 2.0);
568 progress_radius_outer = progress_radius + (progress_width / 2.0);
569 knob_disc_radius = progress_radius_inner - 5.0;
571 const double pad = 2.0; /* line width for boundary of progress ring */
572 const double actual_width = ((2.0 * pad) + (2.0 * progress_radius_outer));
573 const double scale_factor = size / actual_width;
575 /* knob center is at middle of the area bounded by (xorigin,yorigin) and (xorigin+size, yorigin+size)
576 but the coordinates will be scaled by the scale factor when cairo uses them so first
577 adjust them by the reciprocal of the scale factor.
580 xc = (xorigin + (size / 2.0)) * (1.0/scale_factor);
581 yc = (yorigin + (size / 2.0)) * (1.0/scale_factor);
583 pxs = xorigin * (1.0/scale_factor);
584 pys = yorigin * (1.0/scale_factor);
589 value = (phase * 1.0) / (65 - 1);
591 start_angle = ((180 - 65) * G_PI) / 180;
592 end_angle = ((360 + 65) * G_PI) / 180;
594 value_angle = start_angle + (value * (end_angle - start_angle));
595 value_x = cos (value_angle);
596 value_y = sin (value_angle);
597 start_angle_x = cos (start_angle);
598 start_angle_y = sin (start_angle);
599 end_angle_x = cos (end_angle);
600 end_angle_y = sin (end_angle);
602 cairo_scale (cr, scale_factor, scale_factor);
604 pattern = prolooks_create_gradient_str (pxs + 32.0, pys + 16.0, pxs + 75.0, pys + 16.0, "#d4c8b9", "#ae977b", 1.0, 1.0);
605 cairo_set_source (cr, pattern);
606 cairo_pattern_destroy (pattern);
607 cairo_set_line_width (cr, 2.0);
608 cairo_arc (cr, xc, yc, 31.5, 0.0, 2 * G_PI);
611 pattern = prolooks_create_gradient_str (pxs + 20.0, pys + 20.0, pxs + 89.0, pys + 87.0, "#2f2f4c", "#090a0d", 1.0, 1.0);
612 cairo_set_source (cr, pattern);
613 cairo_pattern_destroy (pattern);
614 cairo_set_line_width (cr, progress_width);
615 cairo_arc (cr, xc, yc, progress_radius, start_angle, end_angle);
618 pattern = prolooks_create_gradient (pxs + 20.0, pys + 20.0, pxs + 89.0, pys + 87.0, bright, dark, 1.0, 1.0);
619 cairo_set_source (cr, pattern);
620 cairo_pattern_destroy (pattern);
621 cairo_set_line_width (cr, progress_width);
622 cairo_arc (cr, xc, yc, progress_radius, start_angle, value_angle);
625 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
626 progress_rim_width = 2.0;
627 cairo_set_line_width (cr, progress_rim_width);
628 pattern = prolooks_create_gradient_str (pxs + 18.0, pys + 79.0, pxs + 35.0, pys + 79.0, "#dfd5c9", "#dfd5c9", 1.0, 0.0);
629 cairo_set_source (cr, pattern);
630 cairo_pattern_destroy (pattern);
631 cairo_move_to (cr, xc + (progress_radius_outer * start_angle_x), yc + (progress_radius_outer * start_angle_y));
632 cairo_line_to (cr, xc + (progress_radius_inner * start_angle_x), yc + (progress_radius_inner * start_angle_y));
635 prolooks_set_source_color_string (cr, "#000000", 1.0);
636 cairo_move_to (cr, xc + (progress_radius_outer * end_angle_x), yc + (progress_radius_outer * end_angle_y));
637 cairo_line_to (cr, xc + (progress_radius_inner * end_angle_x), yc + (progress_radius_inner * end_angle_y));
640 // pattern = prolooks_create_gradient_str (95.0, 6.0, 5.0, 44.0, "#dfd5c9", "#b0a090", 1.0, 1.0);
641 pattern = prolooks_create_gradient_str (pxs + 95.0, pys + 6.0, pxs + 5.0, pys + 44.0, "#000000", "#000000", 1.0, 1.0);
642 cairo_set_source (cr, pattern);
643 cairo_pattern_destroy (pattern);
644 cairo_arc (cr, xc, yc, progress_radius_outer, start_angle, end_angle);
647 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
648 pattern = prolooks_create_gradient (pxs + 20.0, pys + 20.0, pxs + 89.0, pys + 87.0, bright, dark, 0.25, 0.25);
649 cairo_set_source (cr, pattern);
650 cairo_pattern_destroy (pattern);
651 cairo_set_line_width (cr, progress_width);
652 cairo_arc (cr, xc, yc, progress_radius, start_angle, value_angle + (G_PI / 180.0));
655 progress_shine = prolooks_create_gradient_str (pxs + 89.0, pys + 73.0, pxs + 34.0, pys + 16.0, "#ffffff", "#ffffff", 0.3, 0.04);
656 cairo_pattern_add_color_stop_rgba (progress_shine, 0.5, 1.0, 1.0, 1.0, 0.0);
658 cairo_pattern_add_color_stop_rgba (progress_shine, 0.75, 1.0, 1.0, 1.0, 0.3);
660 cairo_pattern_add_color_stop_rgba (progress_shine, 0.75, 1.0, 1.0, 1.0, 0.2);
662 cairo_set_source (cr, progress_shine);
663 cairo_set_line_width (cr, progress_width);
664 cairo_arc (cr, xc, yc, progress_radius, start_angle, end_angle);
666 cairo_pattern_destroy (progress_shine);
668 cairo_set_line_width (cr, 1.0);
669 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
670 cairo_arc (cr, xc, yc, progress_radius_inner, 0.0, 2 * G_PI);
671 pattern = prolooks_create_gradient_str (pxs + 35.0, pys + 31.0, pxs + 75.0, pys + 72.0, "#68625c", "#44494b", 1.0, 1.0);
672 cairo_set_source (cr, pattern);
673 cairo_pattern_destroy (pattern);
675 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
676 cairo_arc (cr, xc, yc, progress_radius_inner, 0.0, 2 * G_PI);
679 pattern = prolooks_create_gradient_str (pxs + 42.0, pys + 34.0, pxs + 68.0, pys + 70.0, "#e7ecef", "#9cafb8", 1.0, 1.0);
680 cairo_set_source (cr, pattern);
681 cairo_pattern_destroy (pattern);
682 cairo_arc (cr, xc, yc, knob_disc_radius, 0.0, 2 * G_PI);
685 cairo_set_line_width (cr, 2.0);
686 degrees = G_PI / 180.0;
687 pattern = prolooks_create_gradient_str (pxs + 38.0, pys + 34.0, pxs + 70.0, pys + 68.0, "#ffffff", "#caddf2", 0.2, 0.2);
688 cairo_set_source (cr, pattern);
689 cairo_pattern_destroy (pattern);
690 cairo_move_to (cr, xc, yc);
691 cairo_arc (cr, xc, yc, knob_disc_radius - 1, (-154) * degrees, (-120) * degrees);
692 cairo_move_to (cr, xc, yc);
693 cairo_arc (cr, xc, yc, knob_disc_radius - 1, (G_PI / 2.0) - (60 * degrees), (G_PI / 2.0) - (29 * degrees));
696 pattern = prolooks_create_gradient_str (pxs + 50.0, pys + 40.0, pxs + 62.0, pys + 60.0, "#a1adb6", "#47535c", 0.07, 0.15);
697 cairo_set_source (cr, pattern);
698 cairo_pattern_destroy (pattern);
699 cairo_move_to (cr, xc, yc);
700 cairo_arc (cr, xc, yc, knob_disc_radius - 1, (-67) * degrees, (-27) * degrees);
701 cairo_move_to (cr, xc, yc);
702 cairo_arc (cr, xc, yc, knob_disc_radius - 1, G_PI - (67 * degrees), G_PI - (27 * degrees));
705 knob_ripples = cairo_pattern_create_radial (xc, yc, 0.0, xc, yc, 4.0);
706 prolooks_add_color_stop_str (knob_ripples, 0.0, "#e7ecef", 0.05);
707 prolooks_add_color_stop_str (knob_ripples, 0.5, "#58717d", 0.05);
708 prolooks_add_color_stop_str (knob_ripples, 0.75, "#d1d9de", 0.05);
709 prolooks_add_color_stop_str (knob_ripples, 1.0, "#5d7682", 0.05);
710 cairo_pattern_set_extend (knob_ripples, CAIRO_EXTEND_REPEAT);
711 cairo_set_line_width (cr, 0.0);
712 cairo_set_source (cr, knob_ripples);
713 cairo_arc (cr, xc, yc, knob_disc_radius, 0.0, 2 * G_PI);
717 cairo_translate (cr, xc + (knob_disc_radius * value_x), yc + (knob_disc_radius * value_y));
718 cairo_rotate (cr, value_angle - G_PI);
719 pattern = prolooks_create_gradient_str (pxs + 16.0, pys + -2.0, pxs + 9.0, pys + 13.0, "#e7ecef", "#9cafb8", 0.8, 0.8);
720 cairo_set_source (cr, pattern);
721 cairo_pattern_destroy (pattern);
722 cairo_move_to (cr, 0.0, 4.0);
723 cairo_line_to (cr, 17.0, 4.0);
724 cairo_curve_to (cr, 19.0, 4.0, 21.0, 2.0, 21.0, 0.0);
725 cairo_curve_to (cr, 21.0, -2.0, 19.0, -4.0, 17.0, -4.0);
726 cairo_line_to (cr, 0.0, -4.0);
727 cairo_close_path (cr);
730 pattern = prolooks_create_gradient_str (pxs + 9.0, pys + -2.0, pxs + 9.0, pys + 2.0, "#68625c", "#44494b", 1.0, 1.0);
731 cairo_set_source (cr, pattern);
732 cairo_pattern_destroy (pattern);
733 cairo_move_to (cr, 0.0, 2.0);
734 cairo_line_to (cr, 16.0, 2.0);
735 cairo_curve_to (cr, 17.0, 2.0, 18.0, 1.0, 18.0, 0.0);
736 cairo_curve_to (cr, 18.0, -1.0, 17.0, -2.0, 16.0, -2.0);
737 cairo_line_to (cr, 0.0, -2.0);
738 cairo_close_path (cr);
742 cairo_set_line_width (cr, 2.0);
743 pattern = prolooks_create_gradient_str (pxs + 38.0, pys + 32.0, pxs + 70.0, pys + 67.0, "#3d3d3d", "#000000", 1.0, 1.0);
744 cairo_set_source (cr, pattern);
745 cairo_pattern_destroy (pattern);
746 cairo_arc (cr, xc, yc, knob_disc_radius, 0.0, 2 * G_PI);
749 cairo_pattern_destroy (knob_ripples);
753 MotionFeedback::set_lamp_color (const std::string& str)
756 *base_color = Gdk::Color (str);
758 base_color = new Gdk::Color (str);