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