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