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