More fixes to ArdourButton actions.
[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         //border and background fill
220         if ((_elements & Body)==Body) {
221                 if (_elements & Edge) {
222
223                         cairo_set_source_rgba (cr, 0, 0, 0, 1);
224                         rounded_function(cr, 0, 0, get_width(), get_height(), _corner_radius);
225                         cairo_fill (cr);
226
227                         rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.5);
228                 } else {
229                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
230                 }
231
232                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
233                         
234                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
235                         cairo_fill (cr);
236
237                         //border
238                         cairo_set_line_width (cr, 2.0);
239                         rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
240                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
241                         cairo_stroke (cr);
242                                 
243                 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
244
245                         //background color
246                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
247                         cairo_fill (cr);
248
249                 } else {  //inactive, or it has an indicator
250
251                         //background color
252                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
253                         cairo_fill (cr);
254
255                 }
256         }
257
258         //show the "convex" or "concave" gradient
259         if (!_flat_buttons) {
260                 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
261                         //concave
262                         float width = get_width();  float height = get_height();
263                         cairo_set_source (cr, concave_pattern);
264                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, width-2, height-2, _corner_radius - 1.5);
265                         cairo_fill (cr);
266                 } else {
267                         float width = get_width();  float height = get_height();
268                         cairo_set_source (cr, convex_pattern);
269                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, width-2, height-2, _corner_radius - 1.5);
270                         cairo_fill (cr);
271                 }
272         }
273
274         //Pixbuf, if any
275         if (_pixbuf) {
276
277                 double x,y;
278                 x = (get_width() - _pixbuf->get_width())/2.0;
279                 y = (get_height() - _pixbuf->get_height())/2.0;
280
281                 //if this is a DropDown with an icon, then we need to 
282                   //move the icon left slightly to accomomodate the arrow
283                 if (((_elements & Menu)==Menu)) {
284                         cairo_save (cr);
285                         cairo_translate (cr, -8,0 );
286                 }
287                 
288                 if (_grabbed) {
289                         cairo_rectangle (cr, x+1, y+1, _pixbuf->get_width(), _pixbuf->get_height());
290                         gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x+1, y+1);
291                         cairo_fill (cr);
292                 } else {
293                         cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
294                         gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
295                         cairo_fill (cr);
296                 }
297
298                 //..and then return to our previous drawing position
299                 if (((_elements & Menu)==Menu)) {
300                         cairo_restore (cr);
301                 }
302         }
303
304         int text_margin;
305         if (get_width() < 75) {
306                 text_margin = 5;
307         } else {
308                 text_margin = 10;
309         }
310
311         // Text, if any
312         if ( !_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
313
314                 cairo_save (cr);
315                 cairo_rectangle (cr, 2, 1, get_width()-4, get_height()-2);
316                 cairo_clip(cr);
317
318                 cairo_new_path (cr);    
319                 ArdourCanvas::set_source_rgba (cr, text_color);
320
321                 if ( (_elements & Menu) == Menu) {
322                         cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
323                         pango_cairo_show_layout (cr, _layout->gobj());
324                 } else if ( (_elements & Indicator)  == Indicator) {
325                         if (_led_left) {
326                                 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
327                         } else {
328                                 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
329                         }
330                         pango_cairo_show_layout (cr, _layout->gobj());
331                 } else {
332                         /* align text */
333
334                         double ww, wh;
335                         double xa, ya;
336                         ww = get_width();
337                         wh = get_height();
338                         cairo_save (cr); // TODO retain rotataion.. adj. LED,...
339                         cairo_rotate(cr, _angle * M_PI / 180.0);
340                         cairo_device_to_user(cr, &ww, &wh);
341                         xa = (ww - _text_width) * _xalign;
342                         ya = (wh - _text_height) * _yalign;
343
344                         /* quick hack for left/bottom alignment at -90deg
345                          * TODO this should be generalized incl rotation.
346                          * currently only 'user' of this API is meter_strip.cc
347                          */
348                         if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
349
350                         // TODO honor left/right text_margin with min/max()
351
352                         cairo_move_to (cr, xa, ya);
353                         pango_cairo_update_layout(cr, _layout->gobj());
354                         pango_cairo_show_layout (cr, _layout->gobj());
355                         cairo_restore (cr);
356
357                         /* use old center'ed layout for follow up items - until rotation/aligment code is completed */
358                         cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
359                 }
360                 cairo_restore (cr);
361         } 
362
363         //Menu "triangle"
364         if (((_elements & Menu)==Menu)) {
365         
366                 cairo_save (cr);
367
368                 //menu arrow
369                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
370                 cairo_move_to(cr, get_width() - ((_diameter/2.0) + 6.0), get_height()/2.0 +_diameter/4);
371                 cairo_rel_line_to(cr, -_diameter/2, -_diameter/2);
372                 cairo_rel_line_to(cr, _diameter, 0);
373                 cairo_close_path(cr);
374                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
375                 cairo_fill_preserve(cr);
376                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
377                 cairo_set_line_width(cr, 0.5);
378                 cairo_stroke(cr);
379         
380                 cairo_restore (cr);
381         }
382         
383         //Indicator LED
384         if (((_elements & Indicator)==Indicator)) {
385
386                 /* move to the center of the indicator/led */
387
388                 cairo_save (cr);
389
390                 if (_elements & Text) {
391                         if (_led_left) {
392                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
393                         } else {
394                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
395                         }
396                 } else {
397                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
398                 }
399                 
400                 //inset
401                 if (!_flat_buttons) {
402                         cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
403                         cairo_set_source (cr, led_inset_pattern);
404                         cairo_fill (cr);
405                 }
406
407                 //black ring
408                 cairo_set_source_rgb (cr, 0, 0, 0);
409                 cairo_arc (cr, 0, 0, _diameter/2-1, 0, 2 * M_PI);
410                 cairo_fill(cr);
411
412                 //led color
413                 ArdourCanvas::set_source_rgba (cr, led_color);
414                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
415                 cairo_fill(cr);
416                 
417                 cairo_restore (cr);
418         }
419
420
421         // a transparent gray layer to indicate insensitivity
422         if ((visual_state() & Gtkmm2ext::Insensitive)) {
423                 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius);
424                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
425                 cairo_fill (cr);
426         }
427
428         // if requested, show hovering
429         if (ARDOUR::Config->get_widget_prelight()
430                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
431                 if (_hovering) {
432                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
433                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
434                         cairo_fill (cr);
435                 }
436         }
437
438         //user is currently pressing the button.  black outline helps to indicate this
439         if ( _grabbed && !((_elements & Menu)==Menu) ) {
440                 cairo_set_line_width(cr,1);
441                 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1);
442                 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
443                 cairo_stroke (cr);
444         }
445         
446         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a white box around them
447         if (visual_state() & Gtkmm2ext::Selected) {
448                 cairo_set_line_width(cr,2);
449                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
450                 cairo_set_source_rgba (cr, 1, 1, 1, 0.75);
451                 cairo_stroke (cr);
452         }
453         
454         //I guess this means we have keyboard focus.  I don't think this works currently
455         if (_focused) {
456                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
457                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
458                 double dashes = 1;
459                 cairo_set_dash (cr, &dashes, 1, 0);
460                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
461                 cairo_set_line_width (cr, 1.0);
462                 cairo_stroke (cr);
463                 cairo_set_dash (cr, 0, 0, 0);
464         }
465 }
466
467 void
468 ArdourButton::set_diameter (float d)
469 {
470         _diameter = (d*2) + 5.0;
471
472         if (_diameter != 0.0) {
473                 _fixed_diameter = true;
474         }
475
476         build_patterns ();
477         queue_resize ();
478 }
479
480 void
481 ArdourButton::set_corner_radius (float r)
482 {
483         _corner_radius = r;
484         set_dirty ();
485 }
486
487 void
488 ArdourButton::on_size_request (Gtk::Requisition* req)
489 {
490         int xpad = 0;
491         int ypad = 6;
492
493         CairoWidget::on_size_request (req);
494
495         if ((_elements & Text) && !_text.empty()) {
496
497                 //calc our real width for our string (but ignore the height, because that results in inconsistent button heights)
498                 int ignored;
499                 _layout->get_pixel_size (_text_width, ignored);
500
501                 //calc the height using some text with both ascenders and descenders
502                 std::string t = _layout->get_text();
503                 _layout->set_text ("WjgO");  //what we put here probably doesn't matter, as long as its the same for everyone
504                 _layout->get_pixel_size (ignored, _text_height);
505                 _layout->set_text (t);
506
507                 if (_text_width + _diameter < 75) {
508                         xpad = 7;
509                 } else {
510                         xpad = 12;
511                 }
512         } else {
513                 _text_width = 0;
514                 _text_height = 0;
515         }
516
517         if (_pixbuf) {
518                 xpad = 6;
519         }
520
521         if ((_elements & Indicator) && _fixed_diameter) {
522                 if (_pixbuf) {
523                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
524                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
525                 } else {
526                         req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
527                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
528                 }
529         } else {
530                 if (_pixbuf) {
531                         req->width = _pixbuf->get_width() + xpad;
532                         req->height = _pixbuf->get_height() + ypad;
533                 }  else {
534                         req->width = _text_width + xpad;
535                         req->height = _text_height + ypad;
536                 }
537         }
538         req->width += _corner_radius;
539 }
540
541 /**
542  * This sets the colors used for rendering based on the name of the button, and
543  * thus uses information from the GUI config data. 
544  */
545 void
546 ArdourButton::set_colors ()
547 {
548         if (_fixed_colors_set) {
549                 return;
550         }
551         std::string name = get_name();
552
553         fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
554         fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
555
556         text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
557         text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
558
559         led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
560         led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
561 }
562 /**
563  * This sets the colors used for rendering based on two fixed values, rather
564  * than basing them on the button name, and thus information in the GUI config
565  * data.
566  */
567 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
568 {
569         _fixed_colors_set = true;
570
571         fill_active_color = color_active;
572         fill_inactive_color = color_inactive;
573
574         unsigned char r, g, b, a;
575         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
576         
577         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
578                 (max (double(g), 255.) - min (double(g), 255.)) +
579                 (max (double(b), 255.) - min (double(b), 255.));
580         
581         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
582                 (max (double(g), 0.) - min (double(g), 0.)) +
583                 (max (double(b), 0.) - min (double(b), 0.));
584         
585         text_active_color = (white_contrast > black_contrast) ?
586                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
587                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
588         
589
590         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
591
592         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
593                 (max (double(g), 255.) - min (double(g), 255.)) +
594                 (max (double(b), 255.) - min (double(b), 255.));
595         
596         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
597                 (max (double(g), 0.) - min (double(g), 0.)) +
598                 (max (double(b), 0.) - min (double(b), 0.));
599         
600         text_inactive_color = (white_contrast > black_contrast) ?
601                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
602                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
603         
604         /* XXX what about led colors ? */
605
606         build_patterns ();
607         set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
608 }
609
610 void
611 ArdourButton::build_patterns ()
612 {
613         if (convex_pattern) {
614                 cairo_pattern_destroy (convex_pattern);
615                 convex_pattern = 0;
616         }
617
618         if (concave_pattern) {
619                 cairo_pattern_destroy (concave_pattern);
620                 concave_pattern = 0;
621         }
622
623         if (led_inset_pattern) {
624                 cairo_pattern_destroy (led_inset_pattern);
625         }
626         
627         float width = get_width();  float height = get_height();
628
629         //convex gradient
630         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  height);
631         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
632         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
633
634         //concave gradient
635         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  height);
636         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
637         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
638
639         if (_elements & Indicator) {
640                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
641                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
642                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
643         }
644
645         set_dirty ();
646 }
647
648 void
649 ArdourButton::set_led_left (bool yn)
650 {
651         _led_left = yn;
652 }
653
654 bool
655 ArdourButton::on_button_press_event (GdkEventButton *ev)
656 {
657         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
658                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
659                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
660                         return true;
661                 }
662         }
663
664         if (binding_proxy.button_press_handler (ev)) {
665                 return true;
666         }
667         
668         _grabbed = true;
669         queue_draw ();
670
671         if (!_act_on_release) {
672                 if (_action) {
673                         _action->activate ();
674                         return true;
675                 }
676         }
677
678         if (_fallthrough_to_parent)
679                 return false;
680
681         return false;
682 }
683
684 bool
685 ArdourButton::on_button_release_event (GdkEventButton *ev)
686 {
687         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
688                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
689                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
690                         signal_led_clicked(); /* EMIT SIGNAL */
691                         return true;
692                 }
693         }
694
695         _grabbed = false;
696         queue_draw ();
697
698         if (_hovering) {
699                 signal_clicked ();      
700                 if (_act_on_release) {
701                         if (_action) {
702                                 _action->activate ();
703                                 return true;
704                         }
705                 }
706         }
707         
708         if (_fallthrough_to_parent)
709                 return false;
710
711         return true;
712 }
713
714 void
715 ArdourButton::set_distinct_led_click (bool yn)
716 {
717         _distinct_led_click = yn;
718         setup_led_rect ();
719 }
720
721 void
722 ArdourButton::color_handler ()
723 {
724         set_colors ();
725         build_patterns ();
726         set_dirty ();
727 }
728
729 void
730 ArdourButton::on_size_allocate (Allocation& alloc)
731 {
732         CairoWidget::on_size_allocate (alloc);
733         setup_led_rect ();
734         build_patterns ();
735 }
736
737 void
738 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
739 {
740         watch_connection.disconnect ();
741         binding_proxy.set_controllable (c);
742 }
743
744 void
745 ArdourButton::watch ()
746 {
747         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
748
749         if (!c) {
750                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
751                 return;
752         }
753
754         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
755 }
756
757 void
758 ArdourButton::controllable_changed ()
759 {
760         float val = binding_proxy.get_controllable()->get_value();
761
762         if (fabs (val) >= 0.5f) {
763                 set_active_state (Gtkmm2ext::ExplicitActive);
764         } else {
765                 unset_active_state ();
766         }
767 }
768
769 void
770 ArdourButton::set_related_action (RefPtr<Action> act)
771 {
772         Gtkmm2ext::Activatable::set_related_action (act);
773
774         if (_action) {
775
776                 action_tooltip_changed ();
777
778                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
779                 if (tact) {
780                         action_toggled ();
781                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
782                 } 
783
784                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
785                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
786                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
787         }
788 }
789
790 void
791 ArdourButton::action_toggled ()
792 {
793         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
794
795         if (tact) {
796                 if (tact->get_active()) {
797                         set_active_state (Gtkmm2ext::ExplicitActive);
798                 } else {
799                         unset_active_state ();
800                 }
801         }
802 }       
803
804 void
805 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
806 {
807         set_colors ();
808         build_patterns ();
809 }
810
811 void
812 ArdourButton::on_name_changed ()
813 {
814         set_colors ();
815         build_patterns ();
816 }
817
818 void
819 ArdourButton::setup_led_rect ()
820 {
821         int text_margin;
822
823         if (get_width() < 75) {
824                 text_margin = 3;
825         } else {
826                 text_margin = 10;
827         }
828
829         if (_elements & Indicator) {
830                 _led_rect = new cairo_rectangle_t;
831                 
832                 if (_elements & Text) {
833                         if (_led_left) {
834                                 _led_rect->x = text_margin;
835                         } else {
836                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
837                         }
838                 } else {
839                         /* centered */
840                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
841                 }
842
843                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
844                 _led_rect->width = _diameter;
845                 _led_rect->height = _diameter;
846
847         } else {
848                 delete _led_rect;
849                 _led_rect = 0;
850         }
851 }
852
853 void
854 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
855 {
856         _pixbuf = img;
857         queue_draw ();
858 }
859
860 void
861 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
862 {
863         bool changed = (_active_state != s);
864         CairoWidget::set_active_state (s);
865         if (changed) {
866                 set_colors ();
867                 build_patterns ();
868         }
869 }
870         
871 void
872 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
873 {
874         bool changed = (_visual_state != s);
875         CairoWidget::set_visual_state (s);
876         if (changed) {
877                 set_colors ();
878                 build_patterns ();
879         }
880 }
881         
882
883 bool
884 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
885 {
886         _focused = true;
887         queue_draw ();
888         return CairoWidget::on_focus_in_event (ev);
889 }
890
891 bool
892 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
893 {
894         _focused = false;
895         queue_draw ();
896         return CairoWidget::on_focus_out_event (ev);
897 }
898
899 bool
900 ArdourButton::on_key_release_event (GdkEventKey *ev) {
901         if (_focused &&
902                         (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
903         {
904                 signal_clicked();
905                 if (_action) {
906                         _action->activate ();
907                 }
908                 return true;
909         }
910         return CairoWidget::on_key_release_event (ev);
911 }
912
913 bool
914 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
915 {
916         _hovering = true;
917
918         if (ARDOUR::Config->get_widget_prelight()) {
919                 queue_draw ();
920         }
921
922         return CairoWidget::on_enter_notify_event (ev);
923 }
924
925 bool
926 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
927 {
928         _hovering = false;
929
930         if (ARDOUR::Config->get_widget_prelight()) {
931                 queue_draw ();
932         }
933
934         return CairoWidget::on_leave_notify_event (ev);
935 }
936
937 void
938 ArdourButton::set_tweaks (Tweaks t)
939 {
940         if (_tweaks != t) {
941                 _tweaks = t;
942                 queue_draw ();
943         }
944 }
945
946 void
947 ArdourButton::action_sensitivity_changed ()
948 {
949         if (_action->property_sensitive ()) {
950                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
951         } else {
952                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
953         }
954         
955 }
956
957
958 void
959 ArdourButton::action_visibility_changed ()
960 {
961         if (_action->property_visible ()) {
962                 show ();
963         } else {
964                 hide ();
965         }
966 }
967
968 void
969 ArdourButton::action_tooltip_changed ()
970 {
971         string str = _action->property_tooltip().get_value();
972         ARDOUR_UI::instance()->set_tip (*this, str);
973 }
974
975 void
976 ArdourButton::set_elements (Element e)
977 {
978         _elements = e;
979 }
980
981 void
982 ArdourButton::add_elements (Element e)
983 {
984         _elements = (ArdourButton::Element) (_elements | e);
985 }