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