micro tweaks to ArdourButton corners
[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         , _char_pixel_width (0)
62         , _char_pixel_height (0)
63         , _text_width (0)
64         , _text_height (0)
65         , _diameter (11.0)
66         , _corner_radius (4.0)
67         , _corner_mask (0xf)
68         , _angle(0)
69         , _xalign(.5)
70         , _yalign(.5)
71         , fill_inactive_color (0)
72         , fill_active_color (0)
73         , text_active_color(0)
74         , text_inactive_color(0)
75         , led_active_color(0)
76         , led_inactive_color(0)
77         , convex_pattern (0)
78         , concave_pattern (0)
79         , led_inset_pattern (0)
80         , _led_rect (0)
81         , _act_on_release (true)
82         , _led_left (false)
83         , _fixed_diameter (true)
84         , _distinct_led_click (false)
85         , _hovering (false)
86         , _focused (false)
87         , _fixed_colors_set (false)
88         , _fallthrough_to_parent (false)
89 {
90         ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
91 }
92
93 ArdourButton::ArdourButton (const std::string& str, Element e)
94         : _elements (e)
95         , _tweaks (Tweaks (0))
96         , _text_width (0)
97         , _text_height (0)
98         , _diameter (11.0)
99         , _corner_radius (4.0)
100         , _corner_mask (0xf)
101         , _angle(0)
102         , _xalign(.5)
103         , _yalign(.5)
104         , fill_inactive_color (0)
105         , fill_active_color (0)
106         , text_active_color(0)
107         , text_inactive_color(0)
108         , led_active_color(0)
109         , led_inactive_color(0)
110         , convex_pattern (0)
111         , concave_pattern (0)
112         , led_inset_pattern (0)
113         , _led_rect (0)
114         , _act_on_release (true)
115         , _led_left (false)
116         , _fixed_diameter (true)
117         , _distinct_led_click (false)
118         , _hovering (false)
119         , _focused (false)
120         , _fixed_colors_set (false)
121 {
122         set_text (str);
123 }
124
125 ArdourButton::~ArdourButton()
126 {
127         delete _led_rect;
128
129         if (convex_pattern) {
130                 cairo_pattern_destroy (convex_pattern);
131         }
132         
133         if (concave_pattern) {
134                 cairo_pattern_destroy (concave_pattern);
135         }
136         
137         if (led_inset_pattern) {
138                 cairo_pattern_destroy (led_inset_pattern);
139         }
140
141 }
142
143 void
144 ArdourButton::set_text (const std::string& str)
145 {
146         _text = str;
147
148         if (!_layout && !_text.empty()) {
149                 _layout = Pango::Layout::create (get_pango_context());
150         } 
151
152         if (_layout) {
153                 _layout->set_text (str);
154         }
155
156         queue_resize ();
157 }
158
159 void
160 ArdourButton::set_markup (const std::string& str)
161 {
162         _text = str;
163
164         if (!_layout) {
165                 _layout = Pango::Layout::create (get_pango_context());
166         } 
167
168         _layout->set_markup (str);
169         queue_resize ();
170 }
171
172 void
173 ArdourButton::set_angle (const double angle)
174 {
175         _angle = angle;
176 }
177
178 void
179 ArdourButton::set_alignment (const float xa, const float ya)
180 {
181         _xalign = xa;
182         _yalign = ya;
183 }
184
185 void
186 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
187 {
188         uint32_t text_color;
189         uint32_t led_color;
190         if ( active_state() == Gtkmm2ext::ExplicitActive ) {
191                 text_color = text_active_color;
192                 led_color = led_active_color;
193         } else {
194                 text_color = text_inactive_color;
195                 led_color = led_inactive_color;
196         }
197         
198         void (*rounded_function)(cairo_t*, double, double, double, double, double);
199
200         switch (_corner_mask) {
201         case 0x1: /* upper left only */
202                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
203                 break;
204         case 0x2: /* upper right only */
205                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
206                 break;
207         case 0x3: /* upper only */
208                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
209                 break;
210                 /* should really have functions for lower right, lower left,
211                    lower only, but for now, we don't
212                 */
213         default:
214                 rounded_function = Gtkmm2ext::rounded_rectangle;
215         }
216
217         if (!_fixed_diameter) {
218                 _diameter = std::min (get_width(), get_height());
219         }
220
221         // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
222         if ((_elements & (Body|Edge)) == (Body|Edge)) {
223                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
224                 cairo_set_source_rgba (cr, 0, 0, 0, 1);
225                 cairo_fill(cr);
226         }
227
228         // background fill
229         if ((_elements & Body)==Body) {
230                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius-1.5);
231                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
232                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
233                         cairo_fill (cr);
234                 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
235                         //background color
236                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
237                         cairo_fill (cr);
238                 } else {  //inactive, or it has an indicator
239                         //background color
240                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
241                 }
242                 cairo_fill (cr);
243         }
244
245         // IMPLICIT ACTIVE: draw a border of the active color
246         if ((_elements & Body)==Body) {
247                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
248                         cairo_set_line_width (cr, 2.0);
249                         rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-2);
250                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
251                         cairo_stroke (cr);
252                 }
253         }
254
255         //show the "convex" or "concave" gradient
256         if (!_flat_buttons) {
257                 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
258                         //concave
259                         cairo_set_source (cr, concave_pattern);
260                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius-1.5);
261                         cairo_fill (cr);
262                 } else {
263                         cairo_set_source (cr, convex_pattern);
264                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius-1.5);
265                         cairo_fill (cr);
266                 }
267         }
268
269         //Pixbuf, if any
270         if (_pixbuf) {
271
272                 double x,y;
273                 x = (get_width() - _pixbuf->get_width())/2.0;
274                 y = (get_height() - _pixbuf->get_height())/2.0;
275
276                 //if this is a DropDown with an icon, then we need to 
277                   //move the icon left slightly to accomomodate the arrow
278                 if (((_elements & Menu)==Menu)) {
279                         cairo_save (cr);
280                         cairo_translate (cr, -8,0 );
281                 }
282                 
283                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
284                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
285                 cairo_fill (cr);
286
287                 //..and then return to our previous drawing position
288                 if (((_elements & Menu)==Menu)) {
289                         cairo_restore (cr);
290                 }
291         }
292         else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
293         if ((_elements & RecButton)) {
294                 const double x = get_width() * .5;
295                 const double y = get_height() * .5;
296                 cairo_arc (cr, x, y, get_height () *.2 , 0, 2 * M_PI);
297                 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
298                 cairo_fill(cr);
299                 cairo_set_source_rgba (cr, .0, .0, .0, 1.);
300                 cairo_set_line_width(cr, 1);
301                 cairo_stroke_preserve (cr);
302         }
303
304         int text_margin;
305         if (get_width() < 75 || (_elements & Menu) ) {
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, rint(text_margin), rint((get_height() - _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, rint(text_margin + _diameter + 4), rint((get_height() - _text_height)/2.0));
327                         } else {
328                                 cairo_move_to (cr, rint(text_margin), rint((get_height() - _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, rint(.5 + xa), rint(ya));
353                         pango_cairo_update_layout(cr, _layout->gobj());
354                         pango_cairo_show_layout (cr, _layout->gobj());
355                         cairo_restore (cr);
356                 }
357                 cairo_restore (cr);
358         }
359
360         //Menu "triangle"
361         if (((_elements & Menu)==Menu)) {
362                 const float trih = ceil(_diameter/2.0);
363                 const float triw2 = ceil(.577 * _diameter/2.0); // 1/sqrt(3) Equilateral triangle
364                 //menu arrow
365                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
366                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) / 2.0));
367                 cairo_rel_line_to(cr, -triw2, -trih);
368                 cairo_rel_line_to(cr, 2. * triw2, 0);
369                 cairo_close_path(cr);
370
371                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
372                 cairo_fill(cr);
373
374                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) / 2.0));
375                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
376                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
377                 cairo_close_path(cr);
378                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
379                 cairo_set_line_width(cr, 1);
380                 cairo_stroke(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-1.5);
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, 1, 1, get_width() - 2, get_height() - 2, _corner_radius-1.5);
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 & (Inactive|Menu))) {
440                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
441                 cairo_set_line_width(cr, 2);
442                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5); // XXX no longer 'black'
443                 cairo_stroke (cr);
444         }
445         
446         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
447         if (visual_state() & Gtkmm2ext::Selected) {
448 #if 0 // outline, edge + 1px outside
449                 cairo_set_line_width(cr, 2);
450                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
451 #else // replace edge (if any)
452                 cairo_set_line_width(cr, 1);
453                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
454 #endif
455                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
456                 cairo_stroke (cr);
457         }
458         
459         //I guess this means we have keyboard focus.  I don't think this works currently
460         //
461         //A: yes, it's keyboard focus and it does work when there's no editor window
462         //   (the editor is always the first receiver for KeyDown).
463         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
464         if (_focused) {
465                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
466                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
467                 double dashes = 1;
468                 cairo_set_dash (cr, &dashes, 1, 0);
469                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
470                 cairo_set_line_width (cr, 1.0);
471                 cairo_stroke (cr);
472                 cairo_set_dash (cr, 0, 0, 0);
473         }
474 }
475
476 void
477 ArdourButton::set_diameter (float d)
478 {
479         _diameter = (d*2) + 5.0;
480
481         if (_diameter != 0.0) {
482                 _fixed_diameter = true;
483         }
484
485         build_patterns ();
486         queue_resize ();
487 }
488
489 void
490 ArdourButton::set_corner_radius (float r)
491 {
492         _corner_radius = r;
493         set_dirty ();
494 }
495
496 void
497 ArdourButton::on_size_request (Gtk::Requisition* req)
498 {
499         int xpad = 0;
500         int ypad = 6;
501
502         CairoWidget::on_size_request (req);
503
504         if ((_elements & Text) && !_text.empty()) {
505
506                 //calc our real width for our string (but ignore the height, because that results in inconsistent button heights)
507                 int ignored;
508                 _layout->get_pixel_size (_text_width, ignored);
509                 _text_height = char_pixel_height ();
510
511                 if (_text_width + _diameter < 75) {
512                         xpad = 7;
513                 } else {
514                         xpad = 12;
515                 }
516         } else {
517                 _text_width = 0;
518                 _text_height = 0;
519         }
520
521         if (_pixbuf) {
522                 xpad = 6;
523         }
524
525         if ((_elements & Indicator) && _fixed_diameter) {
526                 if (_pixbuf) {
527                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
528                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
529                 } else {
530                         req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
531                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
532                 }
533         } else {
534                 if (_pixbuf) {
535                         req->width = _pixbuf->get_width() + xpad;
536                         req->height = _pixbuf->get_height() + ypad;
537                 }  else {
538                         req->width = _text_width + xpad;
539                         req->height = _text_height + ypad;
540                 }
541         }
542
543         if ((_elements & Menu)) {
544                 req->width += _diameter + 4;
545         }
546
547         if ((_elements & RecButton) && !_pixbuf) {
548                 assert(!(_elements & Text));
549                 req->width += char_pixel_height();
550                 req->height += char_pixel_height();
551         }
552
553         req->width += _corner_radius;
554 }
555
556 /**
557  * This sets the colors used for rendering based on the name of the button, and
558  * thus uses information from the GUI config data. 
559  */
560 void
561 ArdourButton::set_colors ()
562 {
563         if (_fixed_colors_set) {
564                 return;
565         }
566         std::string name = get_name();
567
568         fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
569         fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
570
571         text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
572         text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
573
574         led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
575         led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
576 }
577 /**
578  * This sets the colors used for rendering based on two fixed values, rather
579  * than basing them on the button name, and thus information in the GUI config
580  * data.
581  */
582 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
583 {
584         _fixed_colors_set = true;
585
586         fill_active_color = color_active;
587         fill_inactive_color = color_inactive;
588
589         unsigned char r, g, b, a;
590         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
591         
592         double 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         double 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_active_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
605         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
606
607         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
608                 (max (double(g), 255.) - min (double(g), 255.)) +
609                 (max (double(b), 255.) - min (double(b), 255.));
610         
611         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
612                 (max (double(g), 0.) - min (double(g), 0.)) +
613                 (max (double(b), 0.) - min (double(b), 0.));
614         
615         text_inactive_color = (white_contrast > black_contrast) ?
616                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
617                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
618         
619         /* XXX what about led colors ? */
620
621         build_patterns ();
622         set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
623 }
624
625 void
626 ArdourButton::build_patterns ()
627 {
628         if (convex_pattern) {
629                 cairo_pattern_destroy (convex_pattern);
630                 convex_pattern = 0;
631         }
632
633         if (concave_pattern) {
634                 cairo_pattern_destroy (concave_pattern);
635                 concave_pattern = 0;
636         }
637
638         if (led_inset_pattern) {
639                 cairo_pattern_destroy (led_inset_pattern);
640         }
641         
642         //convex gradient
643         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
644         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
645         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
646
647         //concave gradient
648         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
649         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
650         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
651
652         if (_elements & Indicator) {
653                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
654                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
655                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
656         }
657
658         set_dirty ();
659 }
660
661 void
662 ArdourButton::set_led_left (bool yn)
663 {
664         _led_left = yn;
665 }
666
667 bool
668 ArdourButton::on_button_press_event (GdkEventButton *ev)
669 {
670         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
671                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
672                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
673                         return true;
674                 }
675         }
676
677         if (binding_proxy.button_press_handler (ev)) {
678                 return true;
679         }
680         
681         _grabbed = true;
682         queue_draw ();
683
684         if (!_act_on_release) {
685                 if (_action) {
686                         _action->activate ();
687                         return true;
688                 }
689         }
690
691         if (_fallthrough_to_parent)
692                 return false;
693
694         return true;
695 }
696
697 bool
698 ArdourButton::on_button_release_event (GdkEventButton *ev)
699 {
700         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
701                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
702                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
703                         signal_led_clicked(); /* EMIT SIGNAL */
704                         return true;
705                 }
706         }
707
708         _grabbed = false;
709         queue_draw ();
710
711         if (_hovering) {
712                 signal_clicked ();      
713                 if (_act_on_release) {
714                         if (_action) {
715                                 _action->activate ();
716                                 return true;
717                         }
718                 }
719         }
720         
721         if (_fallthrough_to_parent)
722                 return false;
723
724         return true;
725 }
726
727 void
728 ArdourButton::set_distinct_led_click (bool yn)
729 {
730         _distinct_led_click = yn;
731         setup_led_rect ();
732 }
733
734 void
735 ArdourButton::color_handler ()
736 {
737         set_colors ();
738         build_patterns ();
739         set_dirty ();
740 }
741
742 void
743 ArdourButton::on_size_allocate (Allocation& alloc)
744 {
745         CairoWidget::on_size_allocate (alloc);
746         setup_led_rect ();
747         build_patterns ();
748 }
749
750 void
751 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
752 {
753         watch_connection.disconnect ();
754         binding_proxy.set_controllable (c);
755 }
756
757 void
758 ArdourButton::watch ()
759 {
760         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
761
762         if (!c) {
763                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
764                 return;
765         }
766
767         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
768 }
769
770 void
771 ArdourButton::controllable_changed ()
772 {
773         float val = binding_proxy.get_controllable()->get_value();
774
775         if (fabs (val) >= 0.5f) {
776                 set_active_state (Gtkmm2ext::ExplicitActive);
777         } else {
778                 unset_active_state ();
779         }
780 }
781
782 void
783 ArdourButton::set_related_action (RefPtr<Action> act)
784 {
785         Gtkmm2ext::Activatable::set_related_action (act);
786
787         if (_action) {
788
789                 action_tooltip_changed ();
790
791                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
792                 if (tact) {
793                         action_toggled ();
794                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
795                 } 
796
797                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
798                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
799                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
800         }
801 }
802
803 void
804 ArdourButton::action_toggled ()
805 {
806         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
807
808         if (tact) {
809                 if (tact->get_active()) {
810                         set_active_state (Gtkmm2ext::ExplicitActive);
811                 } else {
812                         unset_active_state ();
813                 }
814         }
815 }       
816
817 void
818 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
819 {
820         on_name_changed();
821 }
822
823 void
824 ArdourButton::on_name_changed ()
825 {
826         _char_pixel_width = 0;
827         _char_pixel_height = 0;
828         set_colors ();
829         build_patterns ();
830 }
831
832 void
833 ArdourButton::setup_led_rect ()
834 {
835         int text_margin;
836
837         if (get_width() < 75) {
838                 text_margin = 3;
839         } else {
840                 text_margin = 10;
841         }
842
843         if (_elements & Indicator) {
844                 _led_rect = new cairo_rectangle_t;
845                 
846                 if (_elements & Text) {
847                         if (_led_left) {
848                                 _led_rect->x = text_margin;
849                         } else {
850                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
851                         }
852                 } else {
853                         /* centered */
854                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
855                 }
856
857                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
858                 _led_rect->width = _diameter;
859                 _led_rect->height = _diameter;
860
861         } else {
862                 delete _led_rect;
863                 _led_rect = 0;
864         }
865 }
866
867 void
868 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
869 {
870         _pixbuf = img;
871         queue_draw ();
872 }
873
874 void
875 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
876 {
877         bool changed = (_active_state != s);
878         CairoWidget::set_active_state (s);
879         if (changed) {
880                 set_colors ();
881                 build_patterns ();
882         }
883 }
884         
885 void
886 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
887 {
888         bool changed = (_visual_state != s);
889         CairoWidget::set_visual_state (s);
890         if (changed) {
891                 set_colors ();
892                 build_patterns ();
893         }
894 }
895         
896
897 bool
898 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
899 {
900         _focused = true;
901         queue_draw ();
902         return CairoWidget::on_focus_in_event (ev);
903 }
904
905 bool
906 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
907 {
908         _focused = false;
909         queue_draw ();
910         return CairoWidget::on_focus_out_event (ev);
911 }
912
913 bool
914 ArdourButton::on_key_release_event (GdkEventKey *ev) {
915         if (_focused &&
916                         (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
917         {
918                 signal_clicked();
919                 if (_action) {
920                         _action->activate ();
921                 }
922                 return true;
923         }
924         return CairoWidget::on_key_release_event (ev);
925 }
926
927 bool
928 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
929 {
930         _hovering = (_elements & Inactive) ? false : true;
931
932         if (ARDOUR::Config->get_widget_prelight()) {
933                 queue_draw ();
934         }
935
936         return CairoWidget::on_enter_notify_event (ev);
937 }
938
939 bool
940 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
941 {
942         _hovering = false;
943
944         if (ARDOUR::Config->get_widget_prelight()) {
945                 queue_draw ();
946         }
947
948         return CairoWidget::on_leave_notify_event (ev);
949 }
950
951 void
952 ArdourButton::set_tweaks (Tweaks t)
953 {
954         if (_tweaks != t) {
955                 _tweaks = t;
956                 queue_draw ();
957         }
958 }
959
960 void
961 ArdourButton::action_sensitivity_changed ()
962 {
963         if (_action->property_sensitive ()) {
964                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
965         } else {
966                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
967         }
968         
969 }
970
971 void
972 ArdourButton::recalc_char_pixel_geometry ()
973 {
974         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
975                 return;
976         }
977         if (!_layout) {
978                 _layout = Pango::Layout::create (get_pango_context());
979         }
980         // NB. this is not static, since the geometry is different
981         // depending on the font used.
982         int w, h;
983         std::string t = _layout->get_text();
984         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
985         _layout->set_text (x);
986         _layout->get_pixel_size (w, h);
987         _layout->set_text (t);
988         _char_pixel_height = std::max(4, h);
989         // number of actual chars in the string (not bytes)
990         // Glib to the rescue.
991         Glib::ustring gx(x);
992         _char_pixel_width = std::max(4, w / (int)gx.size());
993 }
994
995 void
996 ArdourButton::action_visibility_changed ()
997 {
998         if (_action->property_visible ()) {
999                 show ();
1000         } else {
1001                 hide ();
1002         }
1003 }
1004
1005 void
1006 ArdourButton::action_tooltip_changed ()
1007 {
1008         string str = _action->property_tooltip().get_value();
1009         ARDOUR_UI::instance()->set_tip (*this, str);
1010 }
1011
1012 void
1013 ArdourButton::set_elements (Element e)
1014 {
1015         _elements = e;
1016 }
1017
1018 void
1019 ArdourButton::add_elements (Element e)
1020 {
1021         _elements = (ArdourButton::Element) (_elements | e);
1022 }