Minor coding style fix and comment addition.
[ardour2.git] / libs / gtkmm2ext / motionfeedback.cc
blob43c8531b8e238625d293e02fa52a5adbc44edbe2
1 /*
2 Copyright (C) 1998-99 Paul Barton-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.
17 $Id: motionfeedback.cc,v 1.5 2004/03/01 03:44:19 pauld Exp $
20 #include <iostream>
21 #include <cmath>
22 #include <cstdlib>
23 #include <algorithm>
24 #include <unistd.h>
25 #include <stdio.h> /* for snprintf, grrr */
27 #include <gdk/gdkkeysyms.h>
28 #include <gtkmm.h>
30 #include "gtkmm2ext/motionfeedback.h"
31 #include "gtkmm2ext/keyboard.h"
32 #include "gtkmm2ext/prolooks-helpers.h"
34 using namespace Gtk;
35 using namespace Gtkmm2ext;
36 using namespace sigc;
38 MotionFeedback::MotionFeedback (Glib::RefPtr<Gdk::Pixbuf> pix,
39 Type t,
40 const char *widget_name,
41 Adjustment *adj,
42 bool with_numeric_display,
43 int subw,
44 int subh)
45 : type (t)
46 , value_packer (0)
47 , value (0)
48 , pixbuf (pix)
49 , subwidth (subw)
50 , subheight (subh)
52 char value_name[1024];
54 if (adj == NULL) {
55 i_own_my_adjustment = true;
56 set_adjustment (new Adjustment (0, 0, 10000, 1, 10, 0));
57 } else {
58 i_own_my_adjustment = false;
59 set_adjustment (adj);
62 default_value = adjustment->get_value();
64 HBox* hpacker = manage (new HBox);
65 hpacker->pack_start (pixwin, true, false);
66 hpacker->show ();
67 pack_start (*hpacker, false, false);
68 pixwin.show ();
70 if (with_numeric_display) {
72 value_packer = new HBox;
73 value = new SpinButton (*adjustment);
74 value_packer->pack_start (*value, false, false);
76 if (step_inc < 1) {
77 value->set_digits (abs ((int) ceil (log10 (step_inc))));
80 pack_start (*value_packer, false, false);
82 if (widget_name) {
83 snprintf (value_name, sizeof(value_name), "%sValue", widget_name);
84 value->set_name (value_name);
87 value->show ();
90 adjustment->signal_value_changed().connect (mem_fun (*this, &MotionFeedback::adjustment_changed));
92 pixwin.set_events (Gdk::BUTTON_PRESS_MASK|
93 Gdk::BUTTON_RELEASE_MASK|
94 Gdk::POINTER_MOTION_MASK|
95 Gdk::ENTER_NOTIFY_MASK|
96 Gdk::LEAVE_NOTIFY_MASK|
97 Gdk::SCROLL_MASK|
98 Gdk::KEY_PRESS_MASK|
99 Gdk::KEY_RELEASE_MASK);
101 pixwin.set_flags (CAN_FOCUS);
103 /* Proxy all important events on the pixwin to ourselves */
105 pixwin.signal_button_press_event().connect(mem_fun (*this,&MotionFeedback::pixwin_button_press_event));
106 pixwin.signal_button_release_event().connect(mem_fun (*this,&MotionFeedback::pixwin_button_release_event));
107 pixwin.signal_motion_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_motion_notify_event));
108 pixwin.signal_enter_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_enter_notify_event));
109 pixwin.signal_leave_notify_event().connect(mem_fun (*this,&MotionFeedback::pixwin_leave_notify_event));
110 pixwin.signal_key_press_event().connect(mem_fun (*this,&MotionFeedback::pixwin_key_press_event));
111 pixwin.signal_scroll_event().connect(mem_fun (*this,&MotionFeedback::pixwin_scroll_event));
112 pixwin.signal_expose_event().connect(mem_fun (*this,&MotionFeedback::pixwin_expose_event), true);
113 pixwin.signal_size_request().connect(mem_fun (*this,&MotionFeedback::pixwin_size_request));
114 pixwin.signal_realize().connect(mem_fun (*this,&MotionFeedback::pixwin_realized));
117 MotionFeedback::~MotionFeedback()
120 if (i_own_my_adjustment) {
121 delete adjustment;
124 delete value;
125 delete value_packer;
128 void
129 MotionFeedback::set_adjustment (Adjustment *adj)
131 adjustment = adj;
133 if (value) {
134 value->set_adjustment (*adj);
137 _lower = adj->get_lower();
138 _upper = adj->get_upper();
139 _range = _upper - _lower;
140 step_inc = adj->get_step_increment();
141 page_inc = adj->get_page_increment();
144 bool
145 MotionFeedback::pixwin_button_press_event (GdkEventButton *ev)
147 if (binding_proxy.button_press_handler (ev)) {
148 return true;
151 switch (ev->button) {
152 case 2:
153 return FALSE; /* XXX why ? */
155 case 1:
156 grab_is_fine = false;
157 break;
158 case 3:
159 grab_is_fine = true;
160 break;
163 gtk_grab_add(GTK_WIDGET(pixwin.gobj()));
164 grabbed_y = ev->y_root;
165 grabbed_x = ev->x_root;
167 /* XXX should we return TRUE ? */
169 return FALSE;
172 bool
173 MotionFeedback::pixwin_button_release_event (GdkEventButton *ev)
175 switch (ev->button) {
176 case 1:
177 if (pixwin.has_grab()) {
178 if (!grab_is_fine) {
179 gtk_grab_remove
180 (GTK_WIDGET(pixwin.gobj()));
183 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
184 /* shift click back to the default */
185 adjustment->set_value (default_value);
186 return true;
188 break;
190 case 3:
191 if (pixwin.has_grab()) {
192 if (grab_is_fine) {
193 gtk_grab_remove
194 (GTK_WIDGET(pixwin.gobj()));
197 break;
200 return VBox::on_button_release_event (ev);
203 bool
204 MotionFeedback::pixwin_motion_notify_event (GdkEventMotion *ev)
206 gfloat multiplier;
207 gfloat x_delta;
208 gfloat y_delta;
210 if (!pixwin.has_grab()) {
211 return VBox::on_motion_notify_event (ev);
214 multiplier = ((ev->state & Keyboard::TertiaryModifier) ? 100 : 1) *
215 ((ev->state & Keyboard::SecondaryModifier) ? 10 : 1) *
216 ((ev->state & Keyboard::PrimaryModifier) ? 2 : 1);
219 if (ev->state & Gdk::BUTTON1_MASK) {
221 y_delta = grabbed_y - ev->y_root;
222 grabbed_y = ev->y_root;
224 x_delta = ev->x_root - grabbed_x;
226 if (y_delta == 0) return TRUE;
228 y_delta *= 1 + (x_delta/100);
229 y_delta *= multiplier;
230 y_delta /= 10;
232 adjustment->set_value (adjustment->get_value() +
233 ((grab_is_fine ? step_inc : page_inc) * y_delta));
235 } else if (ev->state & Gdk::BUTTON3_MASK) {
237 double range = adjustment->get_upper() - adjustment->get_lower();
238 double x = ev->x - subwidth/2;
239 double y = - ev->y + subwidth/2;
240 double angle = std::atan2 (y, x) / M_PI;
242 if (angle < -0.5) {
243 angle += 2.0;
246 angle = -(2.0/3.0) * (angle - 1.25);
247 angle *= range;
248 angle *= multiplier;
249 angle += adjustment->get_lower();
251 adjustment->set_value (angle);
255 return true;
258 bool
259 MotionFeedback::pixwin_enter_notify_event (GdkEventCrossing *ev)
261 pixwin.grab_focus();
262 return false;
265 bool
266 MotionFeedback::pixwin_leave_notify_event (GdkEventCrossing *ev)
268 pixwin.unset_flags (HAS_FOCUS);
269 return false;
272 bool
273 MotionFeedback::pixwin_key_press_event (GdkEventKey *ev)
275 bool retval = false;
276 gfloat curval;
277 gfloat multiplier;
279 multiplier = ((ev->state & Keyboard::TertiaryModifier) ? 100 : 1) *
280 ((ev->state & Keyboard::SecondaryModifier) ? 10 : 1) *
281 ((ev->state & Keyboard::PrimaryModifier) ? 2 : 1);
283 switch (ev->keyval) {
284 case GDK_Page_Up:
285 retval = true;
286 curval = adjustment->get_value();
287 adjustment->set_value (curval + (multiplier * page_inc));
288 break;
290 case GDK_Page_Down:
291 retval = true;
292 curval = adjustment->get_value();
293 adjustment->set_value (curval - (multiplier * page_inc));
294 break;
296 case GDK_Up:
297 retval = true;
298 curval = adjustment->get_value();
299 adjustment->set_value (curval + (multiplier * step_inc));
300 break;
302 case GDK_Down:
303 retval = true;
304 curval = adjustment->get_value();
305 adjustment->set_value (curval - (multiplier * step_inc));
306 break;
308 case GDK_Home:
309 retval = true;
310 adjustment->set_value (_lower);
311 break;
313 case GDK_End:
314 retval = true;
315 adjustment->set_value (_upper);
316 break;
319 return retval;
322 void
323 MotionFeedback::adjustment_changed ()
325 pixwin.queue_draw ();
328 void
329 MotionFeedback::core_draw (cairo_t* cr, int phase, double size, double progress_width, double xorigin, double yorigin,
330 const GdkColor* bright, const GdkColor* dark)
332 double xc;
333 double yc;
334 double start_angle;
335 double end_angle;
336 double value_angle;
337 double value;
338 double value_x;
339 double value_y;
340 double start_angle_x;
341 double start_angle_y;
342 double end_angle_x;
343 double end_angle_y;
344 double progress_radius;
345 double progress_radius_inner;
346 double progress_radius_outer;
347 double knob_disc_radius;
348 cairo_pattern_t* pattern;
349 double progress_rim_width;
350 cairo_pattern_t* progress_shine;
351 double degrees;
352 cairo_pattern_t* knob_ripples;
353 double pxs;
354 double pys;
356 g_return_if_fail (cr != NULL);
358 progress_radius = 40.0;
359 progress_radius_inner = progress_radius - (progress_width / 2.0);
360 progress_radius_outer = progress_radius + (progress_width / 2.0);
361 knob_disc_radius = progress_radius_inner - 5.0;
363 const double pad = 2.0; /* line width for boundary of progress ring */
364 const double actual_width = ((2.0 * pad) + (2.0 * progress_radius_outer));
365 const double scale_factor = size / actual_width;
367 /* knob center is at middle of the area bounded by (xorigin,yorigin) and (xorigin+size, yorigin+size)
368 but the coordinates will be scaled by the scale factor when cairo uses them so first
369 adjust them by the reciprocal of the scale factor.
372 xc = (xorigin + (size / 2.0)) * (1.0/scale_factor);
373 yc = (yorigin + (size / 2.0)) * (1.0/scale_factor);
375 pxs = xorigin * (1.0/scale_factor);
376 pys = yorigin * (1.0/scale_factor);
378 start_angle = 0.0;
379 end_angle = 0.0;
380 value_angle = 0.0;
381 value = (phase * 1.0) / (65 - 1);
383 start_angle = ((180 - 65) * G_PI) / 180;
384 end_angle = ((360 + 65) * G_PI) / 180;
386 value_angle = start_angle + (value * (end_angle - start_angle));
387 value_x = cos (value_angle);
388 value_y = sin (value_angle);
389 start_angle_x = cos (start_angle);
390 start_angle_y = sin (start_angle);
391 end_angle_x = cos (end_angle);
392 end_angle_y = sin (end_angle);
394 cairo_scale (cr, scale_factor, scale_factor);
396 pattern = prolooks_create_gradient_str (pxs + 32.0, pys + 16.0, pxs + 75.0, pys + 16.0, "#d4c8b9", "#ae977b", 1.0, 1.0);
397 cairo_set_source (cr, pattern);
398 cairo_pattern_destroy (pattern);
399 cairo_set_line_width (cr, 2.0);
400 cairo_arc (cr, xc, yc, 31.5, 0.0, 2 * G_PI);
401 cairo_stroke (cr);
403 pattern = prolooks_create_gradient_str (pxs + 20.0, pys + 20.0, pxs + 89.0, pys + 87.0, "#2f2f4c", "#090a0d", 1.0, 1.0);
404 cairo_set_source (cr, pattern);
405 cairo_pattern_destroy (pattern);
406 cairo_set_line_width (cr, progress_width);
407 cairo_arc (cr, xc, yc, progress_radius, start_angle, end_angle);
408 cairo_stroke (cr);
410 pattern = prolooks_create_gradient (pxs + 20.0, pys + 20.0, pxs + 89.0, pys + 87.0, bright, dark, 1.0, 1.0);
411 cairo_set_source (cr, pattern);
412 cairo_pattern_destroy (pattern);
413 cairo_set_line_width (cr, progress_width);
414 cairo_arc (cr, xc, yc, progress_radius, start_angle, value_angle);
415 cairo_stroke (cr);
417 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
418 progress_rim_width = 2.0;
419 cairo_set_line_width (cr, progress_rim_width);
420 pattern = prolooks_create_gradient_str (pxs + 18.0, pys + 79.0, pxs + 35.0, pys + 79.0, "#dfd5c9", "#dfd5c9", 1.0, 0.0);
421 cairo_set_source (cr, pattern);
422 cairo_pattern_destroy (pattern);
423 cairo_move_to (cr, xc + (progress_radius_outer * start_angle_x), yc + (progress_radius_outer * start_angle_y));
424 cairo_line_to (cr, xc + (progress_radius_inner * start_angle_x), yc + (progress_radius_inner * start_angle_y));
425 cairo_stroke (cr);
427 prolooks_set_source_color_string (cr, "#000000", 1.0);
428 cairo_move_to (cr, xc + (progress_radius_outer * end_angle_x), yc + (progress_radius_outer * end_angle_y));
429 cairo_line_to (cr, xc + (progress_radius_inner * end_angle_x), yc + (progress_radius_inner * end_angle_y));
430 cairo_stroke (cr);
432 // pattern = prolooks_create_gradient_str (95.0, 6.0, 5.0, 44.0, "#dfd5c9", "#b0a090", 1.0, 1.0);
433 pattern = prolooks_create_gradient_str (pxs + 95.0, pys + 6.0, pxs + 5.0, pys + 44.0, "#000000", "#000000", 1.0, 1.0);
434 cairo_set_source (cr, pattern);
435 cairo_pattern_destroy (pattern);
436 cairo_arc (cr, xc, yc, progress_radius_outer, start_angle, end_angle);
437 cairo_stroke (cr);
439 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
440 pattern = prolooks_create_gradient (pxs + 20.0, pys + 20.0, pxs + 89.0, pys + 87.0, bright, dark, 0.25, 0.25);
441 cairo_set_source (cr, pattern);
442 cairo_pattern_destroy (pattern);
443 cairo_set_line_width (cr, progress_width);
444 cairo_arc (cr, xc, yc, progress_radius, start_angle, value_angle + (G_PI / 180.0));
445 cairo_stroke (cr);
447 progress_shine = prolooks_create_gradient_str (pxs + 89.0, pys + 73.0, pxs + 34.0, pys + 16.0, "#ffffff", "#ffffff", 0.3, 0.04);
448 cairo_pattern_add_color_stop_rgba (progress_shine, 0.5, 1.0, 1.0, 1.0, 0.0);
449 if (size > 50) {
450 cairo_pattern_add_color_stop_rgba (progress_shine, 0.75, 1.0, 1.0, 1.0, 0.3);
451 } else {
452 cairo_pattern_add_color_stop_rgba (progress_shine, 0.75, 1.0, 1.0, 1.0, 0.2);
454 cairo_set_source (cr, progress_shine);
455 cairo_set_line_width (cr, progress_width);
456 cairo_arc (cr, xc, yc, progress_radius, start_angle, end_angle);
457 cairo_stroke (cr);
458 cairo_pattern_destroy (progress_shine);
460 cairo_set_line_width (cr, 1.0);
461 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
462 cairo_arc (cr, xc, yc, progress_radius_inner, 0.0, 2 * G_PI);
463 pattern = prolooks_create_gradient_str (pxs + 35.0, pys + 31.0, pxs + 75.0, pys + 72.0, "#68625c", "#44494b", 1.0, 1.0);
464 cairo_set_source (cr, pattern);
465 cairo_pattern_destroy (pattern);
466 cairo_fill (cr);
467 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
468 cairo_arc (cr, xc, yc, progress_radius_inner, 0.0, 2 * G_PI);
469 cairo_stroke (cr);
471 pattern = prolooks_create_gradient_str (pxs + 42.0, pys + 34.0, pxs + 68.0, pys + 70.0, "#e7ecef", "#9cafb8", 1.0, 1.0);
472 cairo_set_source (cr, pattern);
473 cairo_pattern_destroy (pattern);
474 cairo_arc (cr, xc, yc, knob_disc_radius, 0.0, 2 * G_PI);
475 cairo_fill (cr);
477 cairo_set_line_width (cr, 2.0);
478 degrees = G_PI / 180.0;
479 pattern = prolooks_create_gradient_str (pxs + 38.0, pys + 34.0, pxs + 70.0, pys + 68.0, "#ffffff", "#caddf2", 0.2, 0.2);
480 cairo_set_source (cr, pattern);
481 cairo_pattern_destroy (pattern);
482 cairo_move_to (cr, xc, yc);
483 cairo_arc (cr, xc, yc, knob_disc_radius - 1, (-154) * degrees, (-120) * degrees);
484 cairo_move_to (cr, xc, yc);
485 cairo_arc (cr, xc, yc, knob_disc_radius - 1, (G_PI / 2.0) - (60 * degrees), (G_PI / 2.0) - (29 * degrees));
486 cairo_fill (cr);
488 pattern = prolooks_create_gradient_str (pxs + 50.0, pys + 40.0, pxs + 62.0, pys + 60.0, "#a1adb6", "#47535c", 0.07, 0.15);
489 cairo_set_source (cr, pattern);
490 cairo_pattern_destroy (pattern);
491 cairo_move_to (cr, xc, yc);
492 cairo_arc (cr, xc, yc, knob_disc_radius - 1, (-67) * degrees, (-27) * degrees);
493 cairo_move_to (cr, xc, yc);
494 cairo_arc (cr, xc, yc, knob_disc_radius - 1, G_PI - (67 * degrees), G_PI - (27 * degrees));
495 cairo_fill (cr);
497 knob_ripples = cairo_pattern_create_radial (xc, yc, 0.0, xc, yc, 4.0);
498 prolooks_add_color_stop_str (knob_ripples, 0.0, "#e7ecef", 0.05);
499 prolooks_add_color_stop_str (knob_ripples, 0.5, "#58717d", 0.05);
500 prolooks_add_color_stop_str (knob_ripples, 0.75, "#d1d9de", 0.05);
501 prolooks_add_color_stop_str (knob_ripples, 1.0, "#5d7682", 0.05);
502 cairo_pattern_set_extend (knob_ripples, CAIRO_EXTEND_REPEAT);
503 cairo_set_line_width (cr, 0.0);
504 cairo_set_source (cr, knob_ripples);
505 cairo_arc (cr, xc, yc, knob_disc_radius, 0.0, 2 * G_PI);
506 cairo_fill (cr);
508 cairo_save (cr);
509 cairo_translate (cr, xc + (knob_disc_radius * value_x), yc + (knob_disc_radius * value_y));
510 cairo_rotate (cr, value_angle - G_PI);
511 pattern = prolooks_create_gradient_str (pxs + 16.0, pys + -2.0, pxs + 9.0, pys + 13.0, "#e7ecef", "#9cafb8", 0.8, 0.8);
512 cairo_set_source (cr, pattern);
513 cairo_pattern_destroy (pattern);
514 cairo_move_to (cr, 0.0, 4.0);
515 cairo_line_to (cr, 17.0, 4.0);
516 cairo_curve_to (cr, 19.0, 4.0, 21.0, 2.0, 21.0, 0.0);
517 cairo_curve_to (cr, 21.0, -2.0, 19.0, -4.0, 17.0, -4.0);
518 cairo_line_to (cr, 0.0, -4.0);
519 cairo_close_path (cr);
520 cairo_fill (cr);
522 pattern = prolooks_create_gradient_str (pxs + 9.0, pys + -2.0, pxs + 9.0, pys + 2.0, "#68625c", "#44494b", 1.0, 1.0);
523 cairo_set_source (cr, pattern);
524 cairo_pattern_destroy (pattern);
525 cairo_move_to (cr, 0.0, 2.0);
526 cairo_line_to (cr, 16.0, 2.0);
527 cairo_curve_to (cr, 17.0, 2.0, 18.0, 1.0, 18.0, 0.0);
528 cairo_curve_to (cr, 18.0, -1.0, 17.0, -2.0, 16.0, -2.0);
529 cairo_line_to (cr, 0.0, -2.0);
530 cairo_close_path (cr);
531 cairo_fill (cr);
533 cairo_restore (cr);
534 cairo_set_line_width (cr, 2.0);
535 pattern = prolooks_create_gradient_str (pxs + 38.0, pys + 32.0, pxs + 70.0, pys + 67.0, "#3d3d3d", "#000000", 1.0, 1.0);
536 cairo_set_source (cr, pattern);
537 cairo_pattern_destroy (pattern);
538 cairo_arc (cr, xc, yc, knob_disc_radius, 0.0, 2 * G_PI);
539 cairo_stroke (cr);
541 cairo_pattern_destroy (knob_ripples);
544 bool
545 MotionFeedback::pixwin_expose_event (GdkEventExpose* ev)
547 GdkWindow *window = pixwin.get_window()->gobj();
548 GtkAdjustment* adj = adjustment->gobj();
550 int phase = (int)((adj->value - adj->lower) * 64 /
551 (adj->upper - adj->lower));
553 // skip middle phase except for true middle value
555 if (type == Rotary && phase == 32) {
556 double pt = (adj->value - adj->lower) * 2.0 /
557 (adj->upper - adj->lower) - 1.0;
558 if (pt < 0)
559 phase = 31;
560 if (pt > 0)
561 phase = 33;
564 // endless knob: skip 90deg highlights unless the value is really a multiple of 90deg
566 if (type == Endless && !(phase % 16)) {
567 if (phase == 64) {
568 phase = 0;
571 double nom = adj->lower + phase * (adj->upper - adj->lower) / 64.0;
572 double diff = (adj->value - nom) / (adj->upper - adj->lower);
574 if (diff > 0.0001)
575 phase = (phase + 1) % 64;
576 if (diff < -0.0001)
577 phase = (phase + 63) % 64;
580 phase = std::min (phase, 63);
582 GtkWidget* widget = GTK_WIDGET(pixwin.gobj());
583 gdk_draw_pixbuf (GDK_DRAWABLE(window), widget->style->fg_gc[0],
584 pixbuf->gobj(),
585 phase * subwidth, type * subheight,
586 0, 0, subwidth, subheight, GDK_RGB_DITHER_NORMAL, 0, 0);
588 return true;
591 bool
592 MotionFeedback::pixwin_scroll_event (GdkEventScroll* ev)
594 double scale;
596 if ((ev->state & (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) == (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
597 scale = 0.01;
598 } else if (ev->state & Keyboard::PrimaryModifier) {
599 scale = 0.1;
600 } else {
601 scale = 1.0;
604 switch (ev->direction) {
605 case GDK_SCROLL_UP:
606 case GDK_SCROLL_RIGHT:
607 adjustment->set_value (adjustment->get_value() + (scale * adjustment->get_step_increment()));
608 break;
610 case GDK_SCROLL_DOWN:
611 case GDK_SCROLL_LEFT:
612 adjustment->set_value (adjustment->get_value() - (scale * adjustment->get_step_increment()));
613 break;
616 return true;
619 void
620 MotionFeedback::pixwin_size_request (GtkRequisition* req)
622 req->width = subwidth;
623 req->height = subheight;
626 void
627 MotionFeedback::pixwin_realized ()
629 set_lamp_color (Gdk::Color ("#b9feff"));
632 void
633 MotionFeedback::set_lamp_color (const Gdk::Color& c)
635 GdkColor col2 = {0,0,0,0};
636 GdkColor col3 = {0,0,0,0};
638 _lamp_color = c;
639 lamp_hsv = prolooks_hsv_new_for_gdk_color (_lamp_color.gobj());
640 lamp_bright = (prolooks_hsv_to_gdk_color (lamp_hsv, &col2), col2);
641 prolooks_hsv_set_saturation (lamp_hsv, 0.66);
642 prolooks_hsv_set_value (lamp_hsv, 0.67);
643 lamp_dark = (prolooks_hsv_to_gdk_color (lamp_hsv, &col3), col3);
646 Glib::RefPtr<Gdk::Pixbuf>
647 MotionFeedback::render_pixbuf (int size)
649 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
650 char path[32];
651 int fd;
653 snprintf (path, sizeof (path), "/tmp/mfimg%dXXXXXX", size);
655 if ((fd = mkstemp (path)) < 0) {
656 return pixbuf;
659 GdkColor col2 = {0,0,0,0};
660 GdkColor col3 = {0,0,0,0};
661 Gdk::Color base ("#b9feff");
662 GdkColor dark;
663 GdkColor bright;
664 ProlooksHSV* hsv;
666 hsv = prolooks_hsv_new_for_gdk_color (base.gobj());
667 bright = (prolooks_hsv_to_gdk_color (hsv, &col2), col2);
668 prolooks_hsv_set_saturation (hsv, 0.66);
669 prolooks_hsv_set_value (hsv, 0.67);
670 dark = (prolooks_hsv_to_gdk_color (hsv, &col3), col3);
672 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size * 64, size);
673 cairo_t* cr = cairo_create (surface);
675 for (int i = 0; i < 64; ++i) {
676 cairo_save (cr);
677 core_draw (cr, i, size, 20, size*i, 0, &bright, &dark);
678 cairo_restore (cr);
681 if (cairo_surface_write_to_png (surface, path) != CAIRO_STATUS_SUCCESS) {
682 std::cerr << "could not save image set to " << path << std::endl;
683 return pixbuf;
686 close (fd);
688 cairo_destroy (cr);
689 cairo_surface_destroy (surface);
691 try {
692 pixbuf = Gdk::Pixbuf::create_from_file (path);
693 } catch (const Gdk::PixbufError &e) {
694 std::cerr << "Caught PixbufError: " << e.what() << std::endl;
695 unlink (path);
696 throw;
697 } catch (...) {
698 unlink (path);
699 g_message("Caught ... ");
700 throw;
703 unlink (path);
705 return pixbuf;
708 void
709 MotionFeedback::set_controllable (boost::shared_ptr<PBD::Controllable> c)
711 binding_proxy.set_controllable (c);
714 boost::shared_ptr<PBD::Controllable>
715 MotionFeedback::controllable () const
717 return binding_proxy.get_controllable ();