experiment with track-selection box shadow
[ardour.git] / gtk2_ardour / ardour_button.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21 #include <cmath>
22 #include <algorithm>
23
24 #include <pangomm/layout.h>
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
29
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
33
34 #include "ardour/rc_configuration.h" // for widget prelight preference
35
36 #include "canvas/utils.h"
37
38 #include "ardour_button.h"
39 #include "ardour_ui.h"
40 #include "global_signals.h"
41
42 #include "i18n.h"
43
44 #define REFLECTION_HEIGHT 2
45
46 using namespace Gdk;
47 using namespace Gtk;
48 using namespace Glib;
49 using namespace PBD;
50 using std::max;
51 using std::min;
52 using namespace std;
53
54 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
55 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
56 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
57
58 ArdourButton::ArdourButton (Element e)
59         : _elements (e)
60         , _tweaks (Tweaks (0))
61         , _text_width (0)
62         , _text_height (0)
63         , _diameter (11.0)
64         , _corner_radius (4.0)
65         , _corner_mask (0xf)
66         , _angle(0)
67         , _xalign(.5)
68         , _yalign(.5)
69         , fill_inactive_color (0)
70         , fill_active_color (0)
71         , text_active_color(0)
72         , text_inactive_color(0)
73         , led_active_color(0)
74         , led_inactive_color(0)
75         , convex_pattern (0)
76         , concave_pattern (0)
77         , led_inset_pattern (0)
78         , _led_rect (0)
79         , _act_on_release (true)
80         , _led_left (false)
81         , _fixed_diameter (true)
82         , _distinct_led_click (false)
83         , _hovering (false)
84         , _focused (false)
85         , _fixed_colors_set (false)
86         , _fallthrough_to_parent (false)
87 {
88         ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
89 }
90
91 ArdourButton::ArdourButton (const std::string& str, Element e)
92         : _elements (e)
93         , _tweaks (Tweaks (0))
94         , _text_width (0)
95         , _text_height (0)
96         , _diameter (11.0)
97         , _corner_radius (4.0)
98         , _corner_mask (0xf)
99         , _angle(0)
100         , _xalign(.5)
101         , _yalign(.5)
102         , fill_inactive_color (0)
103         , fill_active_color (0)
104         , text_active_color(0)
105         , text_inactive_color(0)
106         , led_active_color(0)
107         , led_inactive_color(0)
108         , convex_pattern (0)
109         , concave_pattern (0)
110         , led_inset_pattern (0)
111         , _led_rect (0)
112         , _act_on_release (true)
113         , _led_left (false)
114         , _fixed_diameter (true)
115         , _distinct_led_click (false)
116         , _hovering (false)
117         , _focused (false)
118         , _fixed_colors_set (false)
119 {
120         set_text (str);
121 }
122
123 ArdourButton::~ArdourButton()
124 {
125         delete _led_rect;
126
127         if (convex_pattern) {
128                 cairo_pattern_destroy (convex_pattern);
129         }
130         
131         if (concave_pattern) {
132                 cairo_pattern_destroy (concave_pattern);
133         }
134         
135         if (led_inset_pattern) {
136                 cairo_pattern_destroy (led_inset_pattern);
137         }
138
139 }
140
141 void
142 ArdourButton::set_text (const std::string& str)
143 {
144         _text = str;
145
146         if (!_layout && !_text.empty()) {
147                 _layout = Pango::Layout::create (get_pango_context());
148         } 
149
150         if (_layout) {
151                 _layout->set_text (str);
152         }
153
154         queue_resize ();
155 }
156
157 void
158 ArdourButton::set_markup (const std::string& str)
159 {
160         _text = str;
161
162         if (!_layout) {
163                 _layout = Pango::Layout::create (get_pango_context());
164         } 
165
166         _layout->set_markup (str);
167         queue_resize ();
168 }
169
170 void
171 ArdourButton::set_angle (const double angle)
172 {
173         _angle = angle;
174 }
175
176 void
177 ArdourButton::set_alignment (const float xa, const float ya)
178 {
179         _xalign = xa;
180         _yalign = ya;
181 }
182
183 void
184 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
185 {
186         uint32_t text_color;
187         uint32_t led_color;
188         if ( active_state() == Gtkmm2ext::ExplicitActive ) {
189                 text_color = text_active_color;
190                 led_color = led_active_color;
191         } else {
192                 text_color = text_inactive_color;
193                 led_color = led_inactive_color;
194         }
195         
196         void (*rounded_function)(cairo_t*, double, double, double, double, double);
197
198         switch (_corner_mask) {
199         case 0x1: /* upper left only */
200                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
201                 break;
202         case 0x2: /* upper right only */
203                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
204                 break;
205         case 0x3: /* upper only */
206                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
207                 break;
208                 /* should really have functions for lower right, lower left,
209                    lower only, but for now, we don't
210                 */
211         default:
212                 rounded_function = Gtkmm2ext::rounded_rectangle;
213         }
214
215         if (!_fixed_diameter) {
216                 _diameter = std::min (get_width(), get_height());
217         }
218
219         // background fill
220         if ((_elements & Body)==Body) {
221                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
222                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
223                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
224                         cairo_fill (cr);
225                 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
226                         //background color
227                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
228                         cairo_fill (cr);
229                 } else {  //inactive, or it has an indicator
230                         //background color
231                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
232                 }
233                 cairo_fill (cr);
234         }
235
236         //show the "convex" or "concave" gradient
237         if (!_flat_buttons) {
238                 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
239                         //concave
240                         cairo_set_source (cr, concave_pattern);
241                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
242                         cairo_fill (cr);
243                 } else {
244                         cairo_set_source (cr, convex_pattern);
245                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
246                         cairo_fill (cr);
247                 }
248         }
249
250         // indicator border on top of gradient
251         if ((_elements & Body)==Body) {
252                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
253                         cairo_set_line_width (cr, 3.0);
254                         rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
255                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
256                         cairo_stroke (cr);
257                 }
258         }
259
260         // draw edge
261         if ((_elements & (Body|Edge)) == (Body|Edge)) {
262                 rounded_function (cr, .5, .5, get_width() - 1, get_height() - 1, _corner_radius);
263                 cairo_set_source_rgba (cr, 0, 0, 0, 1);
264                 cairo_set_line_width (cr, 1.0);
265                 cairo_stroke(cr);
266         }
267
268         //Pixbuf, if any
269         if (_pixbuf) {
270
271                 double x,y;
272                 x = (get_width() - _pixbuf->get_width())/2.0;
273                 y = (get_height() - _pixbuf->get_height())/2.0;
274
275                 //if this is a DropDown with an icon, then we need to 
276                   //move the icon left slightly to accomomodate the arrow
277                 if (((_elements & Menu)==Menu)) {
278                         cairo_save (cr);
279                         cairo_translate (cr, -8,0 );
280                 }
281                 
282                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
283                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
284                 cairo_fill (cr);
285
286                 //..and then return to our previous drawing position
287                 if (((_elements & Menu)==Menu)) {
288                         cairo_restore (cr);
289                 }
290         }
291
292         int text_margin;
293         if (get_width() < 75 || (_elements & Menu) ) {
294                 text_margin = 5;
295         } else {
296                 text_margin = 10;
297         }
298
299         // Text, if any
300         if ( !_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
301
302                 cairo_save (cr);
303                 cairo_rectangle (cr, 2, 1, get_width()-4, get_height()-2);
304                 cairo_clip(cr);
305
306                 cairo_new_path (cr);    
307                 ArdourCanvas::set_source_rgba (cr, text_color);
308
309                 if ( (_elements & Menu) == Menu) {
310                         cairo_move_to (cr, rint(text_margin), rint((get_height() - _text_height)/2.0));
311                         pango_cairo_show_layout (cr, _layout->gobj());
312                 } else if ( (_elements & Indicator)  == Indicator) {
313                         if (_led_left) {
314                                 cairo_move_to (cr, rint(text_margin + _diameter + 4), rint((get_height() - _text_height)/2.0));
315                         } else {
316                                 cairo_move_to (cr, rint(text_margin), rint((get_height() - _text_height)/2.0));
317                         }
318                         pango_cairo_show_layout (cr, _layout->gobj());
319                 } else {
320                         /* align text */
321
322                         double ww, wh;
323                         double xa, ya;
324                         ww = get_width();
325                         wh = get_height();
326                         cairo_save (cr); // TODO retain rotataion.. adj. LED,...
327                         cairo_rotate(cr, _angle * M_PI / 180.0);
328                         cairo_device_to_user(cr, &ww, &wh);
329                         xa = (ww - _text_width) * _xalign;
330                         ya = (wh - _text_height) * _yalign;
331
332                         /* quick hack for left/bottom alignment at -90deg
333                          * TODO this should be generalized incl rotation.
334                          * currently only 'user' of this API is meter_strip.cc
335                          */
336                         if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
337
338                         // TODO honor left/right text_margin with min/max()
339
340                         cairo_move_to (cr, rint(.5 + xa), rint(ya));
341                         pango_cairo_update_layout(cr, _layout->gobj());
342                         pango_cairo_show_layout (cr, _layout->gobj());
343                         cairo_restore (cr);
344                 }
345                 cairo_restore (cr);
346         }
347
348         //Menu "triangle"
349         if (((_elements & Menu)==Menu)) {
350                 const float trih = ceil(_diameter/2.0);
351                 const float triw2 = ceil(.577 * _diameter/2.0); // 1/sqrt(3) Equilateral triangle
352                 //menu arrow
353                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
354                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) / 2.0));
355                 cairo_rel_line_to(cr, -triw2, -trih);
356                 cairo_rel_line_to(cr, 2. * triw2, 0);
357                 cairo_close_path(cr);
358
359                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
360                 cairo_fill(cr);
361
362                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) / 2.0));
363                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
364                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
365                 cairo_close_path(cr);
366                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
367                 cairo_set_line_width(cr, 1);
368                 cairo_stroke(cr);
369         }
370         
371         //Indicator LED
372         if (((_elements & Indicator)==Indicator)) {
373
374                 /* move to the center of the indicator/led */
375
376                 cairo_save (cr);
377
378                 if (_elements & Text) {
379                         if (_led_left) {
380                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
381                         } else {
382                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
383                         }
384                 } else {
385                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
386                 }
387                 
388                 //inset
389                 if (!_flat_buttons) {
390                         cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
391                         cairo_set_source (cr, led_inset_pattern);
392                         cairo_fill (cr);
393                 }
394
395                 //black ring
396                 cairo_set_source_rgb (cr, 0, 0, 0);
397                 cairo_arc (cr, 0, 0, _diameter/2-1, 0, 2 * M_PI);
398                 cairo_fill(cr);
399
400                 //led color
401                 ArdourCanvas::set_source_rgba (cr, led_color);
402                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
403                 cairo_fill(cr);
404                 
405                 cairo_restore (cr);
406         }
407
408
409         // a transparent gray layer to indicate insensitivity
410         if ((visual_state() & Gtkmm2ext::Insensitive)) {
411                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
412                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
413                 cairo_fill (cr);
414         }
415
416         // if requested, show hovering
417         if (ARDOUR::Config->get_widget_prelight()
418                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
419                 if (_hovering) {
420                         rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
421                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
422                         cairo_fill (cr);
423                 }
424         }
425
426         //user is currently pressing the button.  black outline helps to indicate this
427         if ( _grabbed && !(_elements & (Inactive|Menu))) {
428                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
429                 cairo_set_line_width(cr, 2);
430                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5); // XXX no longer 'black'
431                 cairo_stroke (cr);
432         }
433         
434         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
435         if (visual_state() & Gtkmm2ext::Selected) {
436 #if 0 // outline, edge + 1px outside
437                 cairo_set_line_width(cr, 2);
438                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
439 #else // replace edge (if any)
440                 cairo_set_line_width(cr, 1);
441                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
442 #endif
443                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
444                 cairo_stroke (cr);
445         }
446         
447         //I guess this means we have keyboard focus.  I don't think this works currently
448         //
449         //A: yes, it's keyboard focus and it does work when there's no editor window
450         //   (the editor is always the first receiver for KeyDown).
451         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
452         if (_focused) {
453                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
454                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
455                 double dashes = 1;
456                 cairo_set_dash (cr, &dashes, 1, 0);
457                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
458                 cairo_set_line_width (cr, 1.0);
459                 cairo_stroke (cr);
460                 cairo_set_dash (cr, 0, 0, 0);
461         }
462 }
463
464 void
465 ArdourButton::set_diameter (float d)
466 {
467         _diameter = (d*2) + 5.0;
468
469         if (_diameter != 0.0) {
470                 _fixed_diameter = true;
471         }
472
473         build_patterns ();
474         queue_resize ();
475 }
476
477 void
478 ArdourButton::set_corner_radius (float r)
479 {
480         _corner_radius = r;
481         set_dirty ();
482 }
483
484 void
485 ArdourButton::on_size_request (Gtk::Requisition* req)
486 {
487         int xpad = 0;
488         int ypad = 6;
489
490         CairoWidget::on_size_request (req);
491
492         if ((_elements & Text) && !_text.empty()) {
493
494                 //calc our real width for our string (but ignore the height, because that results in inconsistent button heights)
495                 int ignored;
496                 _layout->get_pixel_size (_text_width, ignored);
497
498                 //calc the height using some text with both ascenders and descenders
499                 std::string t = _layout->get_text();
500                 _layout->set_text ("WjgO");  //what we put here probably doesn't matter, as long as its the same for everyone
501                 _layout->get_pixel_size (ignored, _text_height);
502                 _layout->set_text (t);
503
504                 if (_text_width + _diameter < 75) {
505                         xpad = 7;
506                 } else {
507                         xpad = 12;
508                 }
509         } else {
510                 _text_width = 0;
511                 _text_height = 0;
512         }
513
514         if (_pixbuf) {
515                 xpad = 6;
516         }
517
518         if ((_elements & Indicator) && _fixed_diameter) {
519                 if (_pixbuf) {
520                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
521                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
522                 } else {
523                         req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
524                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
525                 }
526         } else {
527                 if (_pixbuf) {
528                         req->width = _pixbuf->get_width() + xpad;
529                         req->height = _pixbuf->get_height() + ypad;
530                 }  else {
531                         req->width = _text_width + xpad;
532                         req->height = _text_height + ypad;
533                 }
534         }
535
536         if ((_elements & Menu)) {
537                 req->width += _diameter + 4;
538         }
539         req->width += _corner_radius;
540 }
541
542 /**
543  * This sets the colors used for rendering based on the name of the button, and
544  * thus uses information from the GUI config data. 
545  */
546 void
547 ArdourButton::set_colors ()
548 {
549         if (_fixed_colors_set) {
550                 return;
551         }
552         std::string name = get_name();
553
554         fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
555         fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
556
557         text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
558         text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
559
560         led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
561         led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
562 }
563 /**
564  * This sets the colors used for rendering based on two fixed values, rather
565  * than basing them on the button name, and thus information in the GUI config
566  * data.
567  */
568 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
569 {
570         _fixed_colors_set = true;
571
572         fill_active_color = color_active;
573         fill_inactive_color = color_inactive;
574
575         unsigned char r, g, b, a;
576         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
577         
578         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
579                 (max (double(g), 255.) - min (double(g), 255.)) +
580                 (max (double(b), 255.) - min (double(b), 255.));
581         
582         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
583                 (max (double(g), 0.) - min (double(g), 0.)) +
584                 (max (double(b), 0.) - min (double(b), 0.));
585         
586         text_active_color = (white_contrast > black_contrast) ?
587                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
588                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
589         
590
591         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
592
593         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
594                 (max (double(g), 255.) - min (double(g), 255.)) +
595                 (max (double(b), 255.) - min (double(b), 255.));
596         
597         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
598                 (max (double(g), 0.) - min (double(g), 0.)) +
599                 (max (double(b), 0.) - min (double(b), 0.));
600         
601         text_inactive_color = (white_contrast > black_contrast) ?
602                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
603                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
604         
605         /* XXX what about led colors ? */
606
607         build_patterns ();
608         set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
609 }
610
611 void
612 ArdourButton::build_patterns ()
613 {
614         if (convex_pattern) {
615                 cairo_pattern_destroy (convex_pattern);
616                 convex_pattern = 0;
617         }
618
619         if (concave_pattern) {
620                 cairo_pattern_destroy (concave_pattern);
621                 concave_pattern = 0;
622         }
623
624         if (led_inset_pattern) {
625                 cairo_pattern_destroy (led_inset_pattern);
626         }
627         
628         //convex gradient
629         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
630         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
631         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
632
633         //concave gradient
634         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
635         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
636         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
637
638         if (_elements & Indicator) {
639                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
640                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
641                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
642         }
643
644         set_dirty ();
645 }
646
647 void
648 ArdourButton::set_led_left (bool yn)
649 {
650         _led_left = yn;
651 }
652
653 bool
654 ArdourButton::on_button_press_event (GdkEventButton *ev)
655 {
656         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
657                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
658                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
659                         return true;
660                 }
661         }
662
663         if (binding_proxy.button_press_handler (ev)) {
664                 return true;
665         }
666         
667         _grabbed = true;
668         queue_draw ();
669
670         if (!_act_on_release) {
671                 if (_action) {
672                         _action->activate ();
673                         return true;
674                 }
675         }
676
677         if (_fallthrough_to_parent)
678                 return false;
679
680         return true;
681 }
682
683 bool
684 ArdourButton::on_button_release_event (GdkEventButton *ev)
685 {
686         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
687                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
688                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
689                         signal_led_clicked(); /* EMIT SIGNAL */
690                         return true;
691                 }
692         }
693
694         _grabbed = false;
695         queue_draw ();
696
697         if (_hovering) {
698                 signal_clicked ();      
699                 if (_act_on_release) {
700                         if (_action) {
701                                 _action->activate ();
702                                 return true;
703                         }
704                 }
705         }
706         
707         if (_fallthrough_to_parent)
708                 return false;
709
710         return true;
711 }
712
713 void
714 ArdourButton::set_distinct_led_click (bool yn)
715 {
716         _distinct_led_click = yn;
717         setup_led_rect ();
718 }
719
720 void
721 ArdourButton::color_handler ()
722 {
723         set_colors ();
724         build_patterns ();
725         set_dirty ();
726 }
727
728 void
729 ArdourButton::on_size_allocate (Allocation& alloc)
730 {
731         CairoWidget::on_size_allocate (alloc);
732         setup_led_rect ();
733         build_patterns ();
734 }
735
736 void
737 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
738 {
739         watch_connection.disconnect ();
740         binding_proxy.set_controllable (c);
741 }
742
743 void
744 ArdourButton::watch ()
745 {
746         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
747
748         if (!c) {
749                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
750                 return;
751         }
752
753         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
754 }
755
756 void
757 ArdourButton::controllable_changed ()
758 {
759         float val = binding_proxy.get_controllable()->get_value();
760
761         if (fabs (val) >= 0.5f) {
762                 set_active_state (Gtkmm2ext::ExplicitActive);
763         } else {
764                 unset_active_state ();
765         }
766 }
767
768 void
769 ArdourButton::set_related_action (RefPtr<Action> act)
770 {
771         Gtkmm2ext::Activatable::set_related_action (act);
772
773         if (_action) {
774
775                 action_tooltip_changed ();
776
777                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
778                 if (tact) {
779                         action_toggled ();
780                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
781                 } 
782
783                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
784                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
785                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
786         }
787 }
788
789 void
790 ArdourButton::action_toggled ()
791 {
792         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
793
794         if (tact) {
795                 if (tact->get_active()) {
796                         set_active_state (Gtkmm2ext::ExplicitActive);
797                 } else {
798                         unset_active_state ();
799                 }
800         }
801 }       
802
803 void
804 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
805 {
806         set_colors ();
807         build_patterns ();
808 }
809
810 void
811 ArdourButton::on_name_changed ()
812 {
813         set_colors ();
814         build_patterns ();
815 }
816
817 void
818 ArdourButton::setup_led_rect ()
819 {
820         int text_margin;
821
822         if (get_width() < 75) {
823                 text_margin = 3;
824         } else {
825                 text_margin = 10;
826         }
827
828         if (_elements & Indicator) {
829                 _led_rect = new cairo_rectangle_t;
830                 
831                 if (_elements & Text) {
832                         if (_led_left) {
833                                 _led_rect->x = text_margin;
834                         } else {
835                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
836                         }
837                 } else {
838                         /* centered */
839                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
840                 }
841
842                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
843                 _led_rect->width = _diameter;
844                 _led_rect->height = _diameter;
845
846         } else {
847                 delete _led_rect;
848                 _led_rect = 0;
849         }
850 }
851
852 void
853 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
854 {
855         _pixbuf = img;
856         queue_draw ();
857 }
858
859 void
860 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
861 {
862         bool changed = (_active_state != s);
863         CairoWidget::set_active_state (s);
864         if (changed) {
865                 set_colors ();
866                 build_patterns ();
867         }
868 }
869         
870 void
871 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
872 {
873         bool changed = (_visual_state != s);
874         CairoWidget::set_visual_state (s);
875         if (changed) {
876                 set_colors ();
877                 build_patterns ();
878         }
879 }
880         
881
882 bool
883 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
884 {
885         _focused = true;
886         queue_draw ();
887         return CairoWidget::on_focus_in_event (ev);
888 }
889
890 bool
891 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
892 {
893         _focused = false;
894         queue_draw ();
895         return CairoWidget::on_focus_out_event (ev);
896 }
897
898 bool
899 ArdourButton::on_key_release_event (GdkEventKey *ev) {
900         if (_focused &&
901                         (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
902         {
903                 signal_clicked();
904                 if (_action) {
905                         _action->activate ();
906                 }
907                 return true;
908         }
909         return CairoWidget::on_key_release_event (ev);
910 }
911
912 bool
913 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
914 {
915         _hovering = (_elements & Inactive) ? false : true;
916
917         if (ARDOUR::Config->get_widget_prelight()) {
918                 queue_draw ();
919         }
920
921         return CairoWidget::on_enter_notify_event (ev);
922 }
923
924 bool
925 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
926 {
927         _hovering = false;
928
929         if (ARDOUR::Config->get_widget_prelight()) {
930                 queue_draw ();
931         }
932
933         return CairoWidget::on_leave_notify_event (ev);
934 }
935
936 void
937 ArdourButton::set_tweaks (Tweaks t)
938 {
939         if (_tweaks != t) {
940                 _tweaks = t;
941                 queue_draw ();
942         }
943 }
944
945 void
946 ArdourButton::action_sensitivity_changed ()
947 {
948         if (_action->property_sensitive ()) {
949                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
950         } else {
951                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
952         }
953         
954 }
955
956
957 void
958 ArdourButton::action_visibility_changed ()
959 {
960         if (_action->property_visible ()) {
961                 show ();
962         } else {
963                 hide ();
964         }
965 }
966
967 void
968 ArdourButton::action_tooltip_changed ()
969 {
970         string str = _action->property_tooltip().get_value();
971         ARDOUR_UI::instance()->set_tip (*this, str);
972 }
973
974 void
975 ArdourButton::set_elements (Element e)
976 {
977         _elements = e;
978 }
979
980 void
981 ArdourButton::add_elements (Element e)
982 {
983         _elements = (ArdourButton::Element) (_elements | e);
984 }