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