hide speaker-config in menu (it's not implemented yet)
[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
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 #include "gtkmm2ext/gui_thread.h"
32
33 #include "ardour/rc_configuration.h" // for widget prelight preference
34
35 #include "ardour_button.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
38
39 #include "i18n.h"
40
41 #define REFLECTION_HEIGHT 2
42
43 using namespace Gdk;
44 using namespace Gtk;
45 using namespace Glib;
46 using namespace PBD;
47 using std::max;
48 using std::min;
49 using namespace std;
50
51 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
52 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
53 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
54 bool ArdourButton::_flat_buttons = false;
55
56 ArdourButton::ArdourButton (Element e)
57         : _elements (e)
58         , _tweaks (Tweaks (0))
59         , _text_width (0)
60         , _text_height (0)
61         , _diameter (11.0)
62         , _corner_radius (4.0)
63         , _corner_mask (0xf)
64         , _angle(0)
65         , _xalign(.5)
66         , _yalign(.5)
67         , border_color (0)
68         , fill_color_active (0)
69         , fill_color_inactive (0)
70         , fill_pattern (0)
71         , fill_pattern_active (0)
72         , shine_pattern (0)
73         , led_inset_pattern (0)
74         , reflection_pattern (0)
75         , _led_rect (0)
76         , _act_on_release (true)
77         , _led_left (false)
78         , _fixed_diameter (true)
79         , _distinct_led_click (false)
80         , _hovering (false)
81 {
82         ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
83 }
84
85 ArdourButton::ArdourButton (const std::string& str, Element e)
86         : _elements (e)
87         , _tweaks (Tweaks (0))
88         , _text_width (0)
89         , _text_height (0)
90         , _diameter (11.0)
91         , _corner_radius (4.0)
92         , _corner_mask (0xf)
93         , _angle(0)
94         , _xalign(.5)
95         , _yalign(.5)
96         , border_color (0)
97         , fill_color_active (0)
98         , fill_color_inactive (0)
99         , fill_pattern (0)
100         , fill_pattern_active (0)
101         , shine_pattern (0)
102         , led_inset_pattern (0)
103         , reflection_pattern (0)
104         , _led_rect (0)
105         , _act_on_release (true)
106         , _led_left (false)
107         , _fixed_diameter (true)
108         , _distinct_led_click (false)
109         , _hovering (false)
110 {
111         set_text (str);
112 }
113
114 ArdourButton::~ArdourButton()
115 {
116         delete _led_rect;
117
118         if (shine_pattern) {
119                 cairo_pattern_destroy (shine_pattern);
120         }
121
122         if (fill_pattern) {
123                 cairo_pattern_destroy (fill_pattern);
124         }
125         
126         if (fill_pattern_active) {
127                 cairo_pattern_destroy (fill_pattern_active);
128         }
129         
130         if (led_inset_pattern) {
131                 cairo_pattern_destroy (led_inset_pattern);
132         }
133         
134         if (reflection_pattern) {
135                 cairo_pattern_destroy (reflection_pattern);
136         }
137
138 }
139
140 void
141 ArdourButton::set_text (const std::string& str)
142 {
143         _text = str;
144
145         if (!_layout && !_text.empty()) {
146                 _layout = Pango::Layout::create (get_pango_context());
147         } 
148
149         if (_layout) {
150                 _layout->set_text (str);
151         }
152
153         queue_resize ();
154 }
155
156 void
157 ArdourButton::set_markup (const std::string& str)
158 {
159         _text = str;
160
161         if (!_layout) {
162                 _layout = Pango::Layout::create (get_pango_context());
163         } 
164
165         _layout->set_markup (str);
166         queue_resize ();
167 }
168
169 void
170 ArdourButton::set_angle (const double angle)
171 {
172         _angle = angle;
173 }
174
175 void
176 ArdourButton::set_alignment (const float xa, const float ya)
177 {
178         _xalign = xa;
179         _yalign = ya;
180 }
181
182 void
183 ArdourButton::render (cairo_t* cr)
184 {
185         void (*rounded_function)(cairo_t*, double, double, double, double, double);
186
187         switch (_corner_mask) {
188         case 0x1: /* upper left only */
189                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
190                 break;
191         case 0x2: /* upper right only */
192                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
193                 break;
194         case 0x3: /* upper only */
195                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
196                 break;
197                 /* should really have functions for lower right, lower left,
198                    lower only, but for now, we don't
199                 */
200         default:
201                 rounded_function = Gtkmm2ext::rounded_rectangle;
202         }
203
204         if (!_fixed_diameter) {
205                 _diameter = std::min (get_width(), get_height());
206         }
207
208         float r,g,b,a;
209
210         if ((_elements & Body)==Body) {
211                 if (_elements & Edge) {
212
213                         cairo_set_source_rgba (cr, 0, 0, 0, 1);
214                         rounded_function(cr, 0, 0, get_width(), get_height(), _corner_radius);
215                         cairo_fill (cr);
216
217                         rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.5);
218                 } else {
219                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
220                 }
221
222                 if (active_state() == Gtkmm2ext::ImplicitActive) {
223                         
224                         if (!(_tweaks & ImplicitUsesSolidColor)) {
225                                 cairo_set_source (cr, fill_pattern);
226                         } else {
227                                 cairo_set_source (cr, fill_pattern_active);
228                         }
229                         cairo_fill (cr);
230                         
231                         if (!(_tweaks & ImplicitUsesSolidColor)) {
232                                 //border
233                                 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
234                                 cairo_set_line_width (cr, 1.0);
235                                 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
236                                 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
237                                 cairo_stroke (cr);
238                         }
239                                 
240                 } else if (active_state() == Gtkmm2ext::ExplicitActive || ((_elements & Indicator)==Indicator) ) {
241
242                         //background color
243                         cairo_set_source (cr, fill_pattern_active);
244                         cairo_fill (cr);
245
246                 } else {
247
248                         //background color
249                         cairo_set_source (cr, fill_pattern);
250                         cairo_fill (cr);
251
252                 }
253         }
254
255         if ( ((_elements & FlatFace)==FlatFace) && (active_state() != Gtkmm2ext::ExplicitActive) ) {
256
257                 if ( !_flat_buttons ) {
258                         float rheight = get_height()*0.5-REFLECTION_HEIGHT;
259                         Gtkmm2ext::rounded_rectangle (cr, 2, 3, get_width()-4, rheight, _corner_radius-1);
260                         cairo_set_source (cr, shine_pattern);
261                         cairo_fill (cr);
262                 }
263
264                 if (active_state() == Gtkmm2ext::ExplicitActive) {
265
266                         UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
267                         cairo_set_line_width (cr, 2.0);
268                         rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
269                         cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
270                         cairo_fill (cr);
271
272                 } else {
273
274                         UINT_TO_RGBA (fill_color_inactive, &r, &g, &b, &a);
275                         cairo_set_line_width (cr, 2.0);
276                         rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
277                         cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
278                         cairo_fill (cr);
279
280                 }
281         }
282
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                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
290                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
291                 cairo_fill (cr);
292         }
293
294         /* text, if any */
295
296         int text_margin;
297
298         if (get_width() < 75) {
299                 text_margin = 5;
300         } else {
301                 text_margin = 10;
302         }
303
304         if ( ((_elements & Text)==Text) && !_text.empty()) {
305                 cairo_save (cr);
306                 cairo_rectangle (cr, 2, 1, get_width()-4, get_height()-2);
307                 cairo_clip(cr);
308
309                 cairo_new_path (cr);    
310                 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
311
312                 if (_elements & Indicator) {
313                         if (_led_left) {
314                                 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
315                         } else {
316                                 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
317                         }
318                         pango_cairo_show_layout (cr, _layout->gobj());
319                 } else {
320                         /* align text */
321
322                         double ww, wh;
323                         double xa, ya;
324                         ww = get_width();
325                         wh = get_height();
326                         cairo_save (cr); // TODO retain rotataion.. adj. LED,...
327                         cairo_rotate(cr, _angle * M_PI / 180.0);
328                         cairo_device_to_user(cr, &ww, &wh);
329                         xa = (ww - _text_width) * _xalign;
330                         ya = (wh - _text_height) * _yalign;
331
332                         /* quick hack for left/bottom alignment at -90deg
333                          * TODO this should be generalized incl rotation.
334                          * currently only 'user' of this API is meter_strip.cc
335                          */
336                         if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
337
338                         // TODO honor left/right text_margin with min/max()
339
340                         cairo_move_to (cr, xa, ya);
341                         pango_cairo_update_layout(cr, _layout->gobj());
342                         pango_cairo_show_layout (cr, _layout->gobj());
343                         cairo_restore (cr);
344
345                         /* use old center'ed layout for follow up items - until rotation/aligment code is completed */
346                         cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
347                 }
348                 cairo_restore (cr);
349         } 
350
351         if (((_elements & Indicator)==Indicator)) {
352
353                 /* move to the center of the indicator/led */
354
355                 cairo_save (cr);
356
357                 if (_elements & Text) {
358                         if (_led_left) {
359                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
360                         } else {
361                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
362                         }
363                 } else {
364                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
365                 }
366                 
367                 //inset
368                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
369                 cairo_set_source (cr, led_inset_pattern);
370                 cairo_fill (cr);
371                 
372                 //black ring
373                 cairo_set_source_rgb (cr, 0, 0, 0);
374                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
375                 cairo_fill(cr);
376                 
377                 //led color
378                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
379                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
380                 cairo_fill(cr);
381                 
382                 cairo_restore (cr);
383         }
384
385
386         /* a partially transparent gray layer to indicate insensitivity */
387
388         if ((visual_state() & Gtkmm2ext::Insensitive)) {
389                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
390                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
391                 cairo_fill (cr);
392         }
393
394         //reflection
395         bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
396         show_reflection &= !_flat_buttons;
397         show_reflection &= !((_elements & Indicator)==Indicator);
398         if ( show_reflection ) {
399                 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
400                 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
401                 cairo_set_source (cr, shine_pattern);
402                 cairo_fill (cr);
403         }
404
405         /* if requested, show hovering */
406         
407         if (ARDOUR::Config->get_widget_prelight()
408                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
409                 if (_hovering) {
410                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
411                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
412                         cairo_fill (cr);
413                 }
414         }
415 }
416
417 void
418 ArdourButton::set_diameter (float d)
419 {
420         _diameter = (d*2) + 5.0;
421
422         if (_diameter != 0.0) {
423                 _fixed_diameter = true;
424         }
425
426         set_colors ();
427         queue_resize ();
428 }
429
430 void
431 ArdourButton::set_corner_radius (float r)
432 {
433         _corner_radius = r;
434         set_dirty ();
435 }
436
437 void
438 ArdourButton::on_size_request (Gtk::Requisition* req)
439 {
440         int xpad = 0;
441         int ypad = 6;
442
443         CairoWidget::on_size_request (req);
444
445         if ((_elements & Text) && !_text.empty()) {
446                 _layout->get_pixel_size (_text_width, _text_height);
447                 if (_text_width + _diameter < 75) {
448                         xpad = 7;
449                 } else {
450                         xpad = 12;
451                 }
452         } else {
453                 _text_width = 0;
454                 _text_height = 0;
455         }
456
457         if (_pixbuf) {
458                 xpad = 6;
459         }
460
461         if ((_elements & Indicator) && _fixed_diameter) {
462                 if (_pixbuf) {
463                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
464                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
465                 } else {
466                         req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
467                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
468                 }
469         } else {
470                 if (_pixbuf) {
471                         req->width = _pixbuf->get_width() + xpad;
472                         req->height = _pixbuf->get_height() + ypad;
473                 }  else {
474                         req->width = _text_width + xpad;
475                         req->height = _text_height + ypad;
476                 }
477         }
478         req->width += _corner_radius;
479 }
480
481 void
482 ArdourButton::set_colors ()
483 {
484         uint32_t start_color;
485         uint32_t end_color;
486         uint32_t r, g, b, a;
487         uint32_t text_color;
488         uint32_t led_color;
489
490         if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
491                 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
492         } else {
493                 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
494         }
495         fill_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
496         border_color = ARDOUR_UI::config()->color_by_name ( "button border" );
497
498         if (shine_pattern) {
499                 cairo_pattern_destroy (shine_pattern);
500                 shine_pattern = 0;
501         }
502
503         if (fill_pattern) {
504                 cairo_pattern_destroy (fill_pattern);
505                 fill_pattern = 0;
506         }
507
508         if (fill_pattern_active) {
509                 cairo_pattern_destroy (fill_pattern_active);
510                 fill_pattern_active = 0;
511         }
512
513         if (_elements & Body) {
514
515                 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
516                 
517                 if (_flat_buttons) {
518                         end_color = start_color;
519                 } else {
520                         end_color = fill_color_active;
521                 }
522                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
523
524                 active_r = r/255.0;
525                 active_g = g/255.0;
526                 active_b = b/255.0;
527                 active_a = a/255.0;
528
529                 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
530                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
531                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
532                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
533                 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
534
535                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
536                 if (_flat_buttons) {
537                         end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
538                 } else {
539                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
540                         end_color = fill_color_inactive;
541                 }
542                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
543                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
544                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
545                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
546
547                 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
548                 if (_flat_buttons) {
549                         if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
550                                 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
551                         } else {
552                                 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
553                         }
554                 } else {
555                         if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
556                                 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
557                                 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
558                         } else {
559                                 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
560                                 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
561                         }
562                 }
563                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
564                 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
565                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
566                 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
567         }
568
569         if (led_inset_pattern) {
570                 cairo_pattern_destroy (led_inset_pattern);
571         }
572         
573         if (reflection_pattern) {
574                 cairo_pattern_destroy (reflection_pattern);
575         }
576
577         if (_elements & Indicator) {
578                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
579                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
580                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
581
582                 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
583                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
584                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
585         }
586         
587         /* text and LED colors */
588
589         if (active_state() == Gtkmm2ext::ExplicitActive || ((_tweaks & ImplicitUsesSolidColor) && active_state() == Gtkmm2ext::ImplicitActive)) {
590                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
591                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
592         } else {
593                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
594                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
595         }
596
597         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
598         text_r = r/255.0;
599         text_g = g/255.0;
600         text_b = b/255.0;
601         text_a = a/255.0;
602         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
603         led_r = r/255.0;
604         led_g = g/255.0;
605         led_b = b/255.0;
606         led_a = a/255.0;
607
608         set_dirty ();
609 }
610
611 void
612 ArdourButton::set_led_left (bool yn)
613 {
614         _led_left = yn;
615 }
616
617 bool
618 ArdourButton::on_button_press_event (GdkEventButton *ev)
619 {
620         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
621                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
622                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
623                         return true;
624                 }
625         }
626
627         if (_tweaks & ShowClick) {
628                 set_active_state (Gtkmm2ext::ExplicitActive);
629         }
630
631         if (binding_proxy.button_press_handler (ev)) {
632                 return true;
633         }
634
635         if (!_act_on_release) {
636                 if (_action) {
637                         _action->activate ();
638                         return true;
639                 }
640         }
641
642         return false;
643 }
644
645 bool
646 ArdourButton::on_button_release_event (GdkEventButton *ev)
647 {
648         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
649                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
650                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
651                         signal_led_clicked(); /* EMIT SIGNAL */
652                         return true;
653                 }
654         }
655
656         if (_tweaks & ShowClick) {
657                 unset_active_state ();
658         }
659
660         signal_clicked ();
661
662         if (_act_on_release) {
663                 if (_action) {
664                         _action->activate ();
665                         return true;
666                 }
667         }
668
669
670         return false;
671 }
672
673 void
674 ArdourButton::set_distinct_led_click (bool yn)
675 {
676         _distinct_led_click = yn;
677         setup_led_rect ();
678 }
679
680 void
681 ArdourButton::color_handler ()
682 {
683         set_colors ();
684         set_dirty ();
685 }
686
687 void
688 ArdourButton::on_size_allocate (Allocation& alloc)
689 {
690         CairoWidget::on_size_allocate (alloc);
691         setup_led_rect ();
692         set_colors ();
693 }
694
695 void
696 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
697 {
698         watch_connection.disconnect ();
699         binding_proxy.set_controllable (c);
700 }
701
702 void
703 ArdourButton::watch ()
704 {
705         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
706
707         if (!c) {
708                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
709                 return;
710         }
711
712         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
713 }
714
715 void
716 ArdourButton::controllable_changed ()
717 {
718         float val = binding_proxy.get_controllable()->get_value();
719
720         if (fabs (val) >= 0.5f) {
721                 set_active_state (Gtkmm2ext::ExplicitActive);
722         } else {
723                 unset_active_state ();
724         }
725 }
726
727 void
728 ArdourButton::set_related_action (RefPtr<Action> act)
729 {
730         Gtkmm2ext::Activatable::set_related_action (act);
731
732         if (_action) {
733
734                 action_tooltip_changed ();
735
736                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
737                 if (tact) {
738                         action_toggled ();
739                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
740                 } 
741
742                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
743                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
744                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
745         }
746 }
747
748 void
749 ArdourButton::action_toggled ()
750 {
751         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
752
753         if (tact) {
754                 if (tact->get_active()) {
755                         set_active_state (Gtkmm2ext::ExplicitActive);
756                 } else {
757                         unset_active_state ();
758                 }
759         }
760 }       
761
762 void
763 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
764 {
765         set_colors ();
766 }
767
768 void
769 ArdourButton::setup_led_rect ()
770 {
771         int text_margin;
772
773         if (get_width() < 75) {
774                 text_margin = 3;
775         } else {
776                 text_margin = 10;
777         }
778
779         if (_elements & Indicator) {
780                 _led_rect = new cairo_rectangle_t;
781                 
782                 if (_elements & Text) {
783                         if (_led_left) {
784                                 _led_rect->x = text_margin;
785                         } else {
786                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
787                         }
788                 } else {
789                         /* centered */
790                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
791                 }
792
793                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
794                 _led_rect->width = _diameter;
795                 _led_rect->height = _diameter;
796
797         } else {
798                 delete _led_rect;
799                 _led_rect = 0;
800         }
801 }
802
803 void
804 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
805 {
806         _pixbuf = img;
807         queue_draw ();
808 }
809
810 void
811 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
812 {
813         bool changed = (_active_state != s);
814         CairoWidget::set_active_state (s);
815         if (changed) {
816                 set_colors ();
817         }
818 }
819         
820 void
821 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
822 {
823         bool changed = (_visual_state != s);
824         CairoWidget::set_visual_state (s);
825         if (changed) {
826                 set_colors ();
827         }
828 }
829         
830 bool
831 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
832 {
833         _hovering = true;
834
835         if (ARDOUR::Config->get_widget_prelight()) {
836                 queue_draw ();
837         }
838
839         return CairoWidget::on_enter_notify_event (ev);
840 }
841
842 bool
843 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
844 {
845         _hovering = false;
846
847         if (ARDOUR::Config->get_widget_prelight()) {
848                 queue_draw ();
849         }
850
851         return CairoWidget::on_leave_notify_event (ev);
852 }
853
854 void
855 ArdourButton::set_tweaks (Tweaks t)
856 {
857         if (_tweaks != t) {
858                 _tweaks = t;
859                 queue_draw ();
860         }
861 }
862
863 void
864 ArdourButton::action_sensitivity_changed ()
865 {
866         if (_action->property_sensitive ()) {
867                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
868         } else {
869                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
870         }
871         
872 }
873
874
875 void
876 ArdourButton::action_visibility_changed ()
877 {
878         if (_action->property_visible ()) {
879                 show ();
880         } else {
881                 hide ();
882         }
883 }
884
885 void
886 ArdourButton::action_tooltip_changed ()
887 {
888         string str = _action->property_tooltip().get_value();
889         ARDOUR_UI::instance()->set_tip (*this, str);
890 }
891
892 void
893 ArdourButton::set_rounded_corner_mask (int mask)
894 {
895         _corner_mask = mask;
896         queue_draw ();
897 }
898
899 void
900 ArdourButton::set_elements (Element e)
901 {
902         _elements = e;
903         set_colors ();
904 }
905
906 void
907 ArdourButton::add_elements (Element e)
908 {
909         _elements = (ArdourButton::Element) (_elements | e);
910         set_colors ();
911 }
912
913 void
914 ArdourButton::set_flat_buttons (bool yn)
915 {
916         _flat_buttons = yn;
917 }