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