change the meters into CairoWidget, add expose_area to CairoWidget::render()
[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 & FlatFace)==FlatFace) && (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 & Indicator) {
328                         if (_led_left) {
329                                 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
330                         } else {
331                                 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
332                         }
333                         pango_cairo_show_layout (cr, _layout->gobj());
334                 } else {
335                         /* align text */
336
337                         double ww, wh;
338                         double xa, ya;
339                         ww = get_width();
340                         wh = get_height();
341                         cairo_save (cr); // TODO retain rotataion.. adj. LED,...
342                         cairo_rotate(cr, _angle * M_PI / 180.0);
343                         cairo_device_to_user(cr, &ww, &wh);
344                         xa = (ww - _text_width) * _xalign;
345                         ya = (wh - _text_height) * _yalign;
346
347                         /* quick hack for left/bottom alignment at -90deg
348                          * TODO this should be generalized incl rotation.
349                          * currently only 'user' of this API is meter_strip.cc
350                          */
351                         if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
352
353                         // TODO honor left/right text_margin with min/max()
354
355                         cairo_move_to (cr, xa, ya);
356                         pango_cairo_update_layout(cr, _layout->gobj());
357                         pango_cairo_show_layout (cr, _layout->gobj());
358                         cairo_restore (cr);
359
360                         /* use old center'ed layout for follow up items - until rotation/aligment code is completed */
361                         cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
362                 }
363                 cairo_restore (cr);
364         } 
365
366         if (((_elements & Indicator)==Indicator)) {
367
368                 /* move to the center of the indicator/led */
369
370                 cairo_save (cr);
371
372                 if (_elements & Text) {
373                         if (_led_left) {
374                                 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
375                         } else {
376                                 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
377                         }
378                 } else {
379                         cairo_translate (cr, get_width()/2.0, get_height()/2.0);
380                 }
381                 
382                 //inset
383                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
384                 cairo_set_source (cr, led_inset_pattern);
385                 cairo_fill (cr);
386                 
387                 //black ring
388                 cairo_set_source_rgb (cr, 0, 0, 0);
389                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
390                 cairo_fill(cr);
391                 
392                 //led color
393                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
394                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
395                 cairo_fill(cr);
396                 
397                 cairo_restore (cr);
398         }
399
400
401         /* a partially transparent gray layer to indicate insensitivity */
402
403         if ((visual_state() & Gtkmm2ext::Insensitive)) {
404                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
405                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
406                 cairo_fill (cr);
407         }
408
409         //reflection
410         bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
411         show_reflection &= !_flat_buttons;
412         show_reflection &= !((_elements & Indicator)==Indicator);
413         if ( show_reflection ) {
414                 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
415                 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
416                 cairo_set_source (cr, shine_pattern);
417                 cairo_fill (cr);
418         }
419
420         /* if requested, show hovering */
421         
422         if (ARDOUR::Config->get_widget_prelight()
423                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
424                 if (_hovering) {
425                         rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
426                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
427                         cairo_fill (cr);
428                 }
429         }
430 }
431
432 void
433 ArdourButton::set_diameter (float d)
434 {
435         _diameter = (d*2) + 5.0;
436
437         if (_diameter != 0.0) {
438                 _fixed_diameter = true;
439         }
440
441         build_patterns ();
442         queue_resize ();
443 }
444
445 void
446 ArdourButton::set_corner_radius (float r)
447 {
448         _corner_radius = r;
449         set_dirty ();
450 }
451
452 void
453 ArdourButton::on_size_request (Gtk::Requisition* req)
454 {
455         int xpad = 0;
456         int ypad = 6;
457
458         CairoWidget::on_size_request (req);
459
460         if ((_elements & Text) && !_text.empty()) {
461                 _layout->get_pixel_size (_text_width, _text_height);
462                 if (_text_width + _diameter < 75) {
463                         xpad = 7;
464                 } else {
465                         xpad = 12;
466                 }
467         } else {
468                 _text_width = 0;
469                 _text_height = 0;
470         }
471
472         if (_pixbuf) {
473                 xpad = 6;
474         }
475
476         if ((_elements & Indicator) && _fixed_diameter) {
477                 if (_pixbuf) {
478                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
479                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
480                 } else {
481                         req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
482                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
483                 }
484         } else {
485                 if (_pixbuf) {
486                         req->width = _pixbuf->get_width() + xpad;
487                         req->height = _pixbuf->get_height() + ypad;
488                 }  else {
489                         req->width = _text_width + xpad;
490                         req->height = _text_height + ypad;
491                 }
492         }
493         req->width += _corner_radius;
494 }
495
496 /**
497  * This sets the colors used for rendering based on the name of the button, and
498  * thus uses information from the GUI config data. 
499  */
500 void
501 ArdourButton::set_colors ()
502 {
503         std::string name = get_name();
504
505         border_color = ARDOUR_UI::config()->color_by_name ("button border");
506
507         fill_start_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", name));
508         fill_end_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", name));
509         
510         fill_start_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", name));
511         fill_end_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", name));
512
513         text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
514         text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
515
516         led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
517         led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
518 }
519 /**
520  * This sets the colors used for rendering based on two fixed values, rather
521  * than basing them on the button name, and thus information in the GUI config
522  * data.
523  */
524 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
525 {
526         set_name (""); /* this will trigger a "style-changed" message and then
527                           set_colors()
528                        */
529
530         fill_start_active_color = fill_end_active_color = color_active;
531
532         unsigned char r, g, b, a;
533         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
534         
535         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
536                 (max (double(g), 255.) - min (double(g), 255.)) +
537                 (max (double(b), 255.) - min (double(b), 255.));
538         
539         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
540                 (max (double(g), 0.) - min (double(g), 0.)) +
541                 (max (double(b), 0.) - min (double(b), 0.));
542         
543         text_active_color =
544                 text_inactive_color = (white_contrast > black_contrast) ?
545                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
546                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
547         
548         fill_start_inactive_color = fill_end_inactive_color = color_inactive;
549
550         /* XXX what about led colors ? */
551
552         build_patterns ();
553 }
554
555 void
556 ArdourButton::build_patterns ()
557 {
558         uint32_t start_color;
559         uint32_t end_color;
560         uint32_t text_color;
561         uint32_t led_color;
562         uint32_t r, g, b, a;
563
564         if (shine_pattern) {
565                 cairo_pattern_destroy (shine_pattern);
566                 shine_pattern = 0;
567         }
568
569         if (fill_pattern) {
570                 cairo_pattern_destroy (fill_pattern);
571                 fill_pattern = 0;
572         }
573
574         if (fill_pattern_active) {
575                 cairo_pattern_destroy (fill_pattern_active);
576                 fill_pattern_active = 0;
577         }
578
579         if (_elements & Body) {
580
581                 if (_flat_buttons) {
582                         end_color = start_color = fill_start_active_color;
583                 } else {
584                         start_color = fill_start_active_color;
585                         end_color = fill_end_active_color;
586                 }
587                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
588
589                 active_r = r/255.0;
590                 active_g = g/255.0;
591                 active_b = b/255.0;
592                 active_a = a/255.0;
593
594                 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
595                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
596                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
597                 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
598                 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
599
600                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
601                 if (_flat_buttons) {
602                         end_color = start_color = fill_start_inactive_color;
603                 } else {
604                         start_color = fill_start_inactive_color;
605                         end_color = fill_end_inactive_color;
606                 }
607                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
608                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
609                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
610                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
611
612                 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
613                 if (_flat_buttons) {
614                         if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
615                                 end_color = start_color = led_active_color;
616                         } else {
617                                 end_color = start_color = fill_end_active_color;
618                         }
619                 } else {
620                         if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
621                                 end_color = start_color = led_active_color;
622                         } else {
623                                 start_color = fill_start_active_color;
624                                 end_color = fill_end_active_color;
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         if (active_state() == Gtkmm2ext::ExplicitActive || ((_tweaks & ImplicitUsesSolidColor) && active_state() == Gtkmm2ext::ImplicitActive)) {
652                 text_color = text_active_color;
653                 led_color = led_active_color;
654         } else {
655                 text_color = text_inactive_color;
656                 led_color = led_inactive_color;
657         }
658         
659         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
660         text_r = r/255.0;
661         text_g = g/255.0;
662         text_b = b/255.0;
663         text_a = a/255.0;
664         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
665         led_r = r/255.0;
666         led_g = g/255.0;
667         led_b = b/255.0;
668         led_a = a/255.0;
669
670         set_dirty ();
671 }
672
673 void
674 ArdourButton::set_led_left (bool yn)
675 {
676         _led_left = yn;
677 }
678
679 bool
680 ArdourButton::on_button_press_event (GdkEventButton *ev)
681 {
682         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
683                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
684                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
685                         return true;
686                 }
687         }
688
689         if (_tweaks & ShowClick) {
690                 set_active_state (Gtkmm2ext::ExplicitActive);
691         }
692
693         if (binding_proxy.button_press_handler (ev)) {
694                 return true;
695         }
696
697         if (!_act_on_release) {
698                 if (_action) {
699                         _action->activate ();
700                         return true;
701                 }
702         }
703
704         return false;
705 }
706
707 bool
708 ArdourButton::on_button_release_event (GdkEventButton *ev)
709 {
710         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
711                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
712                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
713                         signal_led_clicked(); /* EMIT SIGNAL */
714                         return true;
715                 }
716         }
717
718         if (_tweaks & ShowClick) {
719                 unset_active_state ();
720         }
721
722         if (_hovering) {
723                 signal_clicked ();
724                 
725                 if (_act_on_release) {
726                         if (_action) {
727                                 _action->activate ();
728                                 return true;
729                         }
730                 }
731         }
732
733         return false;
734 }
735
736 void
737 ArdourButton::set_distinct_led_click (bool yn)
738 {
739         _distinct_led_click = yn;
740         setup_led_rect ();
741 }
742
743 void
744 ArdourButton::color_handler ()
745 {
746         set_colors ();
747         build_patterns ();
748         set_dirty ();
749 }
750
751 void
752 ArdourButton::on_size_allocate (Allocation& alloc)
753 {
754         CairoWidget::on_size_allocate (alloc);
755         setup_led_rect ();
756         build_patterns ();
757 }
758
759 void
760 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
761 {
762         watch_connection.disconnect ();
763         binding_proxy.set_controllable (c);
764 }
765
766 void
767 ArdourButton::watch ()
768 {
769         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
770
771         if (!c) {
772                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
773                 return;
774         }
775
776         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
777 }
778
779 void
780 ArdourButton::controllable_changed ()
781 {
782         float val = binding_proxy.get_controllable()->get_value();
783
784         if (fabs (val) >= 0.5f) {
785                 set_active_state (Gtkmm2ext::ExplicitActive);
786         } else {
787                 unset_active_state ();
788         }
789 }
790
791 void
792 ArdourButton::set_related_action (RefPtr<Action> act)
793 {
794         Gtkmm2ext::Activatable::set_related_action (act);
795
796         if (_action) {
797
798                 action_tooltip_changed ();
799
800                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
801                 if (tact) {
802                         action_toggled ();
803                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
804                 } 
805
806                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
807                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
808                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
809         }
810 }
811
812 void
813 ArdourButton::action_toggled ()
814 {
815         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
816
817         if (tact) {
818                 if (tact->get_active()) {
819                         set_active_state (Gtkmm2ext::ExplicitActive);
820                 } else {
821                         unset_active_state ();
822                 }
823         }
824 }       
825
826 void
827 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
828 {
829         set_colors ();
830         build_patterns ();
831 }
832
833 void
834 ArdourButton::on_name_changed ()
835 {
836         set_colors ();
837         build_patterns ();
838 }
839
840 void
841 ArdourButton::setup_led_rect ()
842 {
843         int text_margin;
844
845         if (get_width() < 75) {
846                 text_margin = 3;
847         } else {
848                 text_margin = 10;
849         }
850
851         if (_elements & Indicator) {
852                 _led_rect = new cairo_rectangle_t;
853                 
854                 if (_elements & Text) {
855                         if (_led_left) {
856                                 _led_rect->x = text_margin;
857                         } else {
858                                 _led_rect->x = get_width() - text_margin - _diameter/2.0;
859                         }
860                 } else {
861                         /* centered */
862                         _led_rect->x = get_width()/2.0 - _diameter/2.0;
863                 }
864
865                 _led_rect->y = get_height()/2.0 - _diameter/2.0;
866                 _led_rect->width = _diameter;
867                 _led_rect->height = _diameter;
868
869         } else {
870                 delete _led_rect;
871                 _led_rect = 0;
872         }
873 }
874
875 void
876 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
877 {
878         _pixbuf = img;
879         queue_draw ();
880 }
881
882 void
883 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
884 {
885         bool changed = (_active_state != s);
886         CairoWidget::set_active_state (s);
887         if (changed) {
888                 set_colors ();
889                 build_patterns ();
890         }
891 }
892         
893 void
894 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
895 {
896         bool changed = (_visual_state != s);
897         CairoWidget::set_visual_state (s);
898         if (changed) {
899                 set_colors ();
900                 build_patterns ();
901         }
902 }
903         
904 bool
905 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
906 {
907         _hovering = true;
908
909         if (ARDOUR::Config->get_widget_prelight()) {
910                 queue_draw ();
911         }
912
913         return CairoWidget::on_enter_notify_event (ev);
914 }
915
916 bool
917 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
918 {
919         _hovering = false;
920
921         if (ARDOUR::Config->get_widget_prelight()) {
922                 queue_draw ();
923         }
924
925         return CairoWidget::on_leave_notify_event (ev);
926 }
927
928 void
929 ArdourButton::set_tweaks (Tweaks t)
930 {
931         if (_tweaks != t) {
932                 _tweaks = t;
933                 queue_draw ();
934         }
935 }
936
937 void
938 ArdourButton::action_sensitivity_changed ()
939 {
940         if (_action->property_sensitive ()) {
941                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
942         } else {
943                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
944         }
945         
946 }
947
948
949 void
950 ArdourButton::action_visibility_changed ()
951 {
952         if (_action->property_visible ()) {
953                 show ();
954         } else {
955                 hide ();
956         }
957 }
958
959 void
960 ArdourButton::action_tooltip_changed ()
961 {
962         string str = _action->property_tooltip().get_value();
963         ARDOUR_UI::instance()->set_tip (*this, str);
964 }
965
966 void
967 ArdourButton::set_rounded_corner_mask (int mask)
968 {
969         _corner_mask = mask;
970         queue_draw ();
971 }
972
973 void
974 ArdourButton::set_elements (Element e)
975 {
976         _elements = e;
977 }
978
979 void
980 ArdourButton::add_elements (Element e)
981 {
982         _elements = (ArdourButton::Element) (_elements | e);
983 }
984
985 void
986 ArdourButton::set_flat_buttons (bool yn)
987 {
988         _flat_buttons = yn;
989 }