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