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 "pbd/gstdio_compat.h"
30 #include <gdk/gdkkeysyms.h>
33 #include "pbd/controllable.h"
34 #include "pbd/compose.h"
35 #include "pbd/error.h"
37 #include "gtkmm2ext/motionfeedback.h"
38 #include "gtkmm2ext/keyboard.h"
39 #include "gtkmm2ext/prolooks-helpers.h"
40 #include "gtkmm2ext/gui_thread.h"
45 using namespace Gtkmm2ext;
49 using PBD::Controllable;
51 Gdk::Color* MotionFeedback::base_color;
53 MotionFeedback::MotionFeedback (Glib::RefPtr<Gdk::Pixbuf> pix,
55 boost::shared_ptr<PBD::Controllable> c,
57 double step_increment,
58 double page_increment,
59 const char *widget_name,
60 bool with_numeric_display,
65 , default_value (default_val)
66 , step_inc (step_increment)
67 , page_inc (page_increment)
75 base_color = new Gdk::Color ("#1a5274");
78 char value_name[1024];
80 print_func = default_printer;
84 HBox* hpacker = manage (new HBox);
85 hpacker->pack_start (pixwin, true, true);
87 pack_start (*hpacker, false, false);
90 if (with_numeric_display) {
92 value_packer = new EventBox;
93 value_packer->set_name ("MotionControllerValue");
94 value_packer->show ();
95 value_packer->set_border_width (6);
98 value->set_justify (Gtk::JUSTIFY_RIGHT);
101 value_packer->add (*value);
103 hpacker = manage (new HBox);
104 hpacker->pack_start (*value_packer, true, false);
106 hpacker->set_border_width (6);
108 pack_start (*hpacker, false, false);
111 snprintf (value_name, sizeof(value_name), "%sValue", widget_name);
112 value->set_name (value_name);
117 print_func (buf, _controllable, print_arg);
118 value->set_text (buf);
122 pixwin.set_events (Gdk::BUTTON_PRESS_MASK|
123 Gdk::BUTTON_RELEASE_MASK|
124 Gdk::POINTER_MOTION_MASK|
125 Gdk::ENTER_NOTIFY_MASK|
126 Gdk::LEAVE_NOTIFY_MASK|
129 Gdk::KEY_RELEASE_MASK);
131 pixwin.set_flags (CAN_FOCUS);
133 /* Proxy all important events on the pixwin to ourselves */
135 pixwin.signal_button_press_event().connect(mem_fun (*this,&MotionFeedback::pixwin_button_press_event));
136 pixwin.signal_button_release_event().connect(mem_fun (*this,&MotionFeedback::pixwin_button_release_event));
137 pixwin.signal_motion_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_motion_notify_event));
138 pixwin.signal_enter_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_enter_notify_event));
139 pixwin.signal_leave_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_leave_notify_event));
140 pixwin.signal_key_press_event().connect(mem_fun (*this,&MotionFeedback::pixwin_key_press_event));
141 pixwin.signal_scroll_event().connect(mem_fun (*this,&MotionFeedback::pixwin_scroll_event));
142 pixwin.signal_expose_event().connect(mem_fun (*this,&MotionFeedback::pixwin_expose_event), true);
143 pixwin.signal_size_request().connect(mem_fun (*this,&MotionFeedback::pixwin_size_request));
146 MotionFeedback::~MotionFeedback()
153 MotionFeedback::pixwin_button_press_event (GdkEventButton *ev)
155 if (binding_proxy.button_press_handler (ev)) {
159 switch (ev->button) {
161 grab_is_fine = false;
170 gtk_grab_add(GTK_WIDGET(pixwin.gobj()));
172 grabbed_y = ev->y_root;
173 grabbed_x = ev->x_root;
179 MotionFeedback::pixwin_button_release_event (GdkEventButton *ev)
181 if (!_controllable) {
185 switch (ev->button) {
187 if (pixwin.has_grab()) {
190 (GTK_WIDGET(pixwin.gobj()));
193 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
194 /* shift click back to the default */
195 _controllable->set_value (default_value, Controllable::NoGroup);
197 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
198 /* ctrl click back to the minimum value */
199 _controllable->set_value (_controllable->lower (), Controllable::NoGroup);
204 if (pixwin.has_grab()) {
207 (GTK_WIDGET(pixwin.gobj()));
213 return VBox::on_button_release_event (ev);
217 MotionFeedback::pixwin_motion_notify_event (GdkEventMotion *ev)
219 if (!_controllable) {
227 if (!pixwin.has_grab()) {
228 return VBox::on_motion_notify_event (ev);
231 multiplier = ((ev->state & Keyboard::TertiaryModifier) ? 100 : 1) *
232 ((ev->state & Keyboard::PrimaryModifier) ? 10 : 1) *
233 ((ev->state & Keyboard::SecondaryModifier) ? 0.1 : 1);
235 if (ev->state & Gdk::BUTTON1_MASK) {
237 /* vertical control */
239 y_delta = grabbed_y - ev->y_root;
240 grabbed_y = ev->y_root;
242 x_delta = ev->x_root - grabbed_x;
244 if (y_delta == 0) return TRUE;
246 y_delta *= 1 + (x_delta/100);
247 y_delta *= multiplier;
250 _controllable->set_value (adjust ((grab_is_fine ? step_inc : page_inc) * y_delta), Controllable::NoGroup);
252 } else if (ev->state & Gdk::BUTTON2_MASK) {
256 double x = ev->x - subwidth/2;
257 double y = - ev->y + subwidth/2;
258 double angle = std::atan2 (y, x) / M_PI;
264 angle = -(2.0/3.0) * (angle - 1.25);
267 _controllable->set_value (to_control_value (angle), Controllable::NoGroup);
275 MotionFeedback::pixwin_enter_notify_event (GdkEventCrossing*)
282 MotionFeedback::pixwin_leave_notify_event (GdkEventCrossing*)
284 pixwin.unset_flags (HAS_FOCUS);
289 MotionFeedback::pixwin_key_press_event (GdkEventKey *ev)
291 if (!_controllable) {
298 multiplier = ((ev->state & Keyboard::TertiaryModifier) ? 100.0 : 1.0) *
299 ((ev->state & Keyboard::SecondaryModifier) ? 10.0 : 1.0) *
300 ((ev->state & Keyboard::PrimaryModifier) ? 2.0 : 1.0);
302 switch (ev->keyval) {
305 _controllable->set_value (adjust (multiplier * page_inc), Controllable::NoGroup);
310 _controllable->set_value (adjust (-multiplier * page_inc), Controllable::NoGroup);
315 _controllable->set_value (adjust (multiplier * step_inc), Controllable::NoGroup);
320 _controllable->set_value (adjust (-multiplier * step_inc), Controllable::NoGroup);
325 _controllable->set_value (_controllable->lower(), Controllable::NoGroup);
330 _controllable->set_value (_controllable->upper(), Controllable::NoGroup);
338 MotionFeedback::pixwin_expose_event (GdkEventExpose*)
340 if (!_controllable) {
344 GdkWindow *window = pixwin.get_window()->gobj();
345 double display_val = to_display_value (_controllable->get_value());
346 int32_t phase = lrint (display_val * 64.0);
348 // skip middle phase except for true middle value
350 if (type == Rotary && phase == 32) {
351 double pt = (display_val * 2.0) - 1.0;
358 // endless knob: skip 90deg highlights unless the value is really a multiple of 90deg
360 if (type == Endless && !(phase % 16)) {
365 double nom = phase / 64.0;
366 double diff = display_val - nom;
369 phase = (phase + 1) % 64;
371 phase = (phase + 63) % 64;
374 phase = std::min (phase, (int32_t) 63);
376 GtkWidget* widget = GTK_WIDGET(pixwin.gobj());
377 gdk_draw_pixbuf (GDK_DRAWABLE(window), widget->style->fg_gc[0],
379 phase * subwidth, type * subheight,
380 /* center image in allocated area */
381 (get_width() - subwidth)/2,
383 subwidth, subheight, GDK_RGB_DITHER_NORMAL, 0, 0);
389 MotionFeedback::pixwin_scroll_event (GdkEventScroll* ev)
393 if (!_controllable) {
397 if (ev->state & Keyboard::GainFineScaleModifier) {
398 if (ev->state & Keyboard::GainExtraFineScaleModifier) {
407 switch (ev->direction) {
409 case GDK_SCROLL_RIGHT:
410 _controllable->set_value (adjust (scale * page_inc), Controllable::NoGroup);
413 case GDK_SCROLL_DOWN:
414 case GDK_SCROLL_LEFT:
415 _controllable->set_value (adjust (-scale * page_inc), Controllable::NoGroup);
423 MotionFeedback::pixwin_size_request (GtkRequisition* req)
425 req->width = subwidth;
426 req->height = subheight;
431 MotionFeedback::controllable_value_changed ()
435 print_func (buf, _controllable, print_arg);
436 value->set_text (buf);
439 pixwin.queue_draw ();
443 MotionFeedback::set_controllable (boost::shared_ptr<PBD::Controllable> c)
446 binding_proxy.set_controllable (c);
447 controller_connection.disconnect ();
450 c->Changed.connect (controller_connection, MISSING_INVALIDATOR, boost::bind (&MotionFeedback::controllable_value_changed, this), gui_context());
453 print_func (buf, _controllable, print_arg);
454 value->set_text (buf);
457 pixwin.queue_draw ();
460 boost::shared_ptr<PBD::Controllable>
461 MotionFeedback::controllable () const
463 return _controllable;
467 MotionFeedback::default_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void *)
470 sprintf (buf, "%.2f", c->get_value());
476 Glib::RefPtr<Gdk::Pixbuf>
477 MotionFeedback::render_pixbuf (int size)
479 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
482 GError *gerror = NULL;
484 fd = g_file_open_tmp ("mfimgXXXXXX", &path, &gerror);
487 error << string_compose (_("motionfeedback: failed to open a temporary file for writing: %1"), gerror->message) << endmsg;
488 g_error_free (gerror);
495 GdkColor col2 = {0,0,0,0};
496 GdkColor col3 = {0,0,0,0};
501 hsv = prolooks_hsv_new_for_gdk_color (base_color->gobj());
502 bright = (prolooks_hsv_to_gdk_color (hsv, &col2), col2);
503 prolooks_hsv_set_saturation (hsv, 0.66);
504 prolooks_hsv_set_value (hsv, 0.67);
505 dark = (prolooks_hsv_to_gdk_color (hsv, &col3), col3);
507 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size * 64, size);
508 cairo_t* cr = cairo_create (surface);
510 for (int i = 0; i < 64; ++i) {
512 core_draw (cr, i, size, 20, size*i, 0, &bright, &dark);
516 if (cairo_surface_write_to_png (surface, path) != CAIRO_STATUS_SUCCESS) {
517 error << string_compose (_("motionfeedback: could not save image set to %1"), path) << endmsg;
522 cairo_surface_destroy (surface);
525 pixbuf = Gdk::Pixbuf::create_from_file (path);
526 } catch (const Gdk::PixbufError &e) {
527 error << string_compose (_("motionfeedback: caught PixbufError: %1"), e.what()) << endmsg;
529 error << _("motionfeedback: unknown exception") << endmsg;
539 MotionFeedback::core_draw (cairo_t* cr, int phase, double size, double progress_width, double xorigin, double yorigin,
540 const GdkColor* bright, const GdkColor* dark)
550 double start_angle_x;
551 double start_angle_y;
554 double progress_radius;
555 double progress_radius_inner;
556 double progress_radius_outer;
558 g_return_if_fail (cr != NULL);
560 progress_radius = 40.0;
561 progress_radius_inner = progress_radius - (progress_width / 2.0);
562 progress_radius_outer = progress_radius + (progress_width / 2.0);
564 const double pad = 2.0; /* line width for boundary of progress ring */
565 const double actual_width = ((2.0 * pad) + (2.0 * progress_radius_outer));
566 const double scale_factor = size / actual_width;
568 /* knob center is at middle of the area bounded by (xorigin,yorigin) and (xorigin+size, yorigin+size)
569 but the coordinates will be scaled by the scale factor when cairo uses them so first
570 adjust them by the reciprocal of the scale factor.
573 xc = (xorigin + (size / 2.0)) * (1.0/scale_factor);
574 yc = (yorigin + (size / 2.0)) * (1.0/scale_factor);
576 value = (phase * 1.0) / (65 - 1);
578 start_angle = ((180 - 65) * G_PI) / 180;
579 end_angle = ((360 + 65) * G_PI) / 180;
581 value_angle = start_angle + (value * (end_angle - start_angle));
582 value_x = cos (value_angle);
583 value_y = sin (value_angle);
584 start_angle_x = cos (start_angle);
585 start_angle_y = sin (start_angle);
586 end_angle_x = cos (end_angle);
587 end_angle_y = sin (end_angle);
589 cairo_scale (cr, scale_factor, scale_factor);
591 //dark arc background
592 cairo_set_source_rgb (cr, 0.3, 0.3, 0.3 );
593 cairo_set_line_width (cr, progress_width);
594 cairo_arc (cr, xc, yc, progress_radius, start_angle, end_angle);
598 float r = (value) * (((float)bright->red)/G_MAXUINT16) + (1.0-value)*(((float)dark->red)/G_MAXUINT16);
599 float g = (value) * (((float)bright->green)/G_MAXUINT16) + (1.0-value)*(((float)dark->green)/G_MAXUINT16);
600 float b = (value) * (((float)bright->blue)/G_MAXUINT16) + (1.0-value)*(((float)dark->blue)/G_MAXUINT16);
603 cairo_set_source_rgb (cr, r,g,b);
604 cairo_set_line_width (cr, progress_width);
605 cairo_arc (cr, xc, yc, progress_radius, start_angle, value_angle);
609 cairo_pattern_t* shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, progress_radius_outer);
610 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, 1,1,1, 0.3);
611 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, 1,1,1, 0.0);
612 cairo_set_source (cr, shade_pattern);
613 cairo_arc (cr, xc, yc, progress_radius_outer-1, 0, 2.0*G_PI);
615 cairo_pattern_destroy (shade_pattern);
618 cairo_set_source_rgb (cr, 0, 0, 0 );
619 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
620 cairo_set_line_width (cr, 1.0/scale_factor);
621 cairo_move_to (cr, xc + (progress_radius_outer * start_angle_x), yc + (progress_radius_outer * start_angle_y));
622 cairo_line_to (cr, xc + (progress_radius_inner * start_angle_x), yc + (progress_radius_inner * start_angle_y));
624 cairo_move_to (cr, xc + (progress_radius_outer * end_angle_x), yc + (progress_radius_outer * end_angle_y));
625 cairo_line_to (cr, xc + (progress_radius_inner * end_angle_x), yc + (progress_radius_inner * end_angle_y));
627 cairo_arc (cr, xc, yc, progress_radius_outer, start_angle, end_angle);
629 cairo_arc (cr, xc, yc, progress_radius_inner, start_angle, end_angle);
634 cairo_translate(cr, 6, 6 );
635 cairo_set_source_rgba (cr, 0,0,0,0.1 );
636 cairo_arc (cr, xc, yc, progress_radius_inner-1, 0, 2.0*G_PI);
641 cairo_set_source_rgba (cr, 0.3, 0.3, 0.3, 1 );
642 cairo_arc (cr, xc, yc, progress_radius_inner-1, 0, 2.0*G_PI);
646 shade_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, progress_radius_outer);
647 cairo_pattern_add_color_stop_rgba (shade_pattern, 0, 1,1,1, 0.5);
648 cairo_pattern_add_color_stop_rgba (shade_pattern, 1, 0,0,0, 0.3);
649 cairo_set_source (cr, shade_pattern);
650 cairo_arc (cr, xc, yc, progress_radius_inner-1, 0, 2.0*G_PI);
652 cairo_pattern_destroy (shade_pattern);
655 cairo_set_source_rgba (cr, 0.3, 0.3, 0.3, 0.5 );
656 cairo_arc (cr, xc, yc, progress_radius_inner-5, 0, 2.0*G_PI);
661 cairo_translate(cr, 2, 2 );
662 cairo_set_source_rgba (cr, 0,0,0,0.5 );
663 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
664 cairo_set_line_width (cr, 4);
665 cairo_move_to (cr, xc + (progress_radius_inner * value_x), yc + (progress_radius_inner * value_y));
666 cairo_line_to (cr, xc + ((progress_radius_inner*0.4) * value_x), yc + ((progress_radius_inner*0.4) * value_y));
669 cairo_set_source_rgba (cr, 1,1,1,0.7 );
670 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
671 cairo_set_line_width (cr, 4.0);
672 cairo_move_to (cr, xc + (progress_radius_inner * value_x), yc + (progress_radius_inner * value_y));
673 cairo_line_to (cr, xc + ((progress_radius_inner*0.4) * value_x), yc + ((progress_radius_inner*0.4) * value_y));
677 //highlight if focused (damn, this is a cached image which doesn't (yet) have a "focused" state
678 // if (pixwin.has_focus()) {
679 // cairo_set_source_rgba (cr, 1,1,1, 0.5 );
680 // cairo_arc (cr, xc, yc, progress_radius_inner-1, 0, 2.0*G_PI);
686 MotionFeedback::set_lamp_color (const std::string& str)
689 *base_color = Gdk::Color (str);
691 base_color = new Gdk::Color (str);