Ctrl+shift functionality (toggle all) for plugin leds.
[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/controllable.h"
28 #include "pbd/error.h"
29 #include "pbd/stacktrace.h"
30
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/rgb_macros.h"
33 #include "gtkmm2ext/gui_thread.h"
34
35 #include "canvas/utils.h"
36 #include "canvas/colors.h"
37
38 #include "ardour_button.h"
39 #include "tooltips.h"
40 #include "ui_config.h"
41
42 #include "i18n.h"
43
44 #define BASELINESTRETCH (1.25)
45 #define TRACKHEADERBTNW (3.10)
46
47 using namespace Gdk;
48 using namespace Gtk;
49 using namespace Glib;
50 using namespace PBD;
51 using namespace ARDOUR_UI_UTILS;
52 using std::max;
53 using std::min;
54 using namespace std;
55
56 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
57 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
58 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
59
60 ArdourButton::ArdourButton (Element e)
61         : _elements (e)
62         , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
63         , _tweaks (Tweaks (0))
64         , _char_pixel_width (0)
65         , _char_pixel_height (0)
66         , _char_avg_pixel_width (0)
67         , _text_width (0)
68         , _text_height (0)
69         , _diameter (0)
70         , _corner_radius (2.5)
71         , _corner_mask (0xf)
72         , _angle(0)
73         , _xalign(.5)
74         , _yalign(.5)
75         , fill_inactive_color (0)
76         , fill_active_color (0)
77         , text_active_color(0)
78         , text_inactive_color(0)
79         , led_active_color(0)
80         , led_inactive_color(0)
81         , led_custom_color (0)
82         , use_custom_led_color (false)
83         , convex_pattern (0)
84         , concave_pattern (0)
85         , led_inset_pattern (0)
86         , _led_rect (0)
87         , _act_on_release (true)
88         , _led_left (false)
89         , _distinct_led_click (false)
90         , _hovering (false)
91         , _focused (false)
92         , _fixed_colors_set (false)
93         , _fallthrough_to_parent (false)
94         , _layout_ellipsize_width (-1)
95         , _ellipsis (Pango::ELLIPSIZE_NONE)
96         , _update_colors (true)
97         , _pattern_height (0)
98 {
99         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
100 }
101
102 ArdourButton::ArdourButton (const std::string& str, Element e)
103         : _elements (e)
104         , _tweaks (Tweaks (0))
105         , _text_width (0)
106         , _text_height (0)
107         , _diameter (0)
108         , _corner_radius (2.5)
109         , _corner_mask (0xf)
110         , _angle(0)
111         , _xalign(.5)
112         , _yalign(.5)
113         , fill_inactive_color (0)
114         , fill_active_color (0)
115         , text_active_color(0)
116         , text_inactive_color(0)
117         , led_active_color(0)
118         , led_inactive_color(0)
119         , led_custom_color (0)
120         , use_custom_led_color (false)
121         , convex_pattern (0)
122         , concave_pattern (0)
123         , led_inset_pattern (0)
124         , _led_rect (0)
125         , _act_on_release (true)
126         , _led_left (false)
127         , _distinct_led_click (false)
128         , _hovering (false)
129         , _focused (false)
130         , _fixed_colors_set (false)
131         , _fallthrough_to_parent (false)
132         , _layout_ellipsize_width (-1)
133         , _ellipsis (Pango::ELLIPSIZE_NONE)
134         , _update_colors (true)
135         , _pattern_height (0)
136 {
137         set_text (str);
138         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
139         UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
140 }
141
142 ArdourButton::~ArdourButton()
143 {
144         delete _led_rect;
145
146         if (convex_pattern) {
147                 cairo_pattern_destroy (convex_pattern);
148         }
149
150         if (concave_pattern) {
151                 cairo_pattern_destroy (concave_pattern);
152         }
153
154         if (led_inset_pattern) {
155                 cairo_pattern_destroy (led_inset_pattern);
156         }
157 }
158
159 void
160 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
161 {
162         ensure_layout ();
163         if (_layout) {
164                 _layout->set_font_description (fd);
165                 queue_resize ();
166         }
167 }
168
169 void
170 ArdourButton::set_text (const std::string& str)
171 {
172         _text = str;
173         if (!is_realized()) {
174                 return;
175         }
176         ensure_layout ();
177         if (_layout && _layout->get_text() != _text) {
178                 _layout->set_text (_text);
179                 queue_resize ();
180         }
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
197 /* TODO make this a dedicated function elsewhere.
198  *
199  * Option 1:
200  * virtual ArdourButton::render_vector_icon()
201  * ArdourIconButton::render_vector_icon
202  *
203  * Option 2:
204  * ARDOUR_UI_UTILS::render_vector_icon()
205  */
206 void
207 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
208 {
209         uint32_t text_color;
210         uint32_t led_color;
211
212         const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
213
214         if (_update_colors) {
215                 set_colors ();
216         }
217         if (get_height() != _pattern_height) {
218                 build_patterns ();
219         }
220
221         if ( active_state() == Gtkmm2ext::ExplicitActive ) {
222                 text_color = text_active_color;
223                 led_color = led_active_color;
224         } else {
225                 text_color = text_inactive_color;
226                 led_color = led_inactive_color;
227         }
228
229         if (use_custom_led_color) {
230                 led_color = led_custom_color;
231         }
232
233         void (*rounded_function)(cairo_t*, double, double, double, double, double);
234
235         switch (_corner_mask) {
236         case 0x1: /* upper left only */
237                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
238                 break;
239         case 0x2: /* upper right only */
240                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
241                 break;
242         case 0x3: /* upper only */
243                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
244                 break;
245                 /* should really have functions for lower right, lower left,
246                    lower only, but for now, we don't
247                 */
248         default:
249                 rounded_function = Gtkmm2ext::rounded_rectangle;
250         }
251
252         // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
253         if ((_elements & (Body|Edge)) == (Body|Edge)) {
254                 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
255                 cairo_set_source_rgba (cr, 0, 0, 0, 1);
256                 cairo_fill(cr);
257         }
258
259         // background fill
260         if ((_elements & Body)==Body) {
261                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
262                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
263                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
264                         cairo_fill (cr);
265                 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
266                         //background color
267                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
268                         cairo_fill (cr);
269                 } else {  //inactive, or it has an indicator
270                         //background color
271                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
272                 }
273                 cairo_fill (cr);
274         }
275
276         // IMPLICIT ACTIVE: draw a border of the active color
277         if ((_elements & Body)==Body) {
278                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
279                         cairo_set_line_width (cr, 2.0);
280                         rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
281                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
282                         cairo_stroke (cr);
283                 }
284         }
285
286         //show the "convex" or "concave" gradient
287         if (!_flat_buttons) {
288                 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
289                         //concave
290                         cairo_set_source (cr, concave_pattern);
291                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
292                         cairo_fill (cr);
293                 } else {
294                         cairo_set_source (cr, convex_pattern);
295                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
296                         cairo_fill (cr);
297                 }
298         }
299
300         //Pixbuf, if any
301         if (_pixbuf) {
302                 double x = rint((get_width() - _pixbuf->get_width()) * .5);
303                 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
304 #if 0 // DEBUG style (print on hover)
305                 if (_hovering || (_elements & Inactive)) {
306                         printf("%s: p:%dx%d (%dx%d)\n",
307                                         get_name().c_str(),
308                                         _pixbuf->get_width(), _pixbuf->get_height(),
309                                         get_width(), get_height());
310                 }
311 #endif
312                 if (_elements & Menu) {
313                         //if this is a DropDown with an icon, then we need to
314                         //move the icon left slightly to accomomodate the arrow
315                         x -= _diameter - 2;
316                 }
317                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
318                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
319                 cairo_fill (cr);
320         }
321         else /* VectorIcons are exclusive to Pixbuf Icons */
322         if (_elements & VectorIcon) {
323                 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
324         }
325
326         const int text_margin = char_pixel_width();
327         // Text, if any
328         if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
329                 assert(_layout);
330 #if 0 // DEBUG style (print on hover)
331                 if (_hovering || (_elements & Inactive)) {
332                         bool layout_font = true;
333                         Pango::FontDescription fd = _layout->get_font_description();
334                         if (fd.gobj() == NULL) {
335                                 layout_font = false;
336                                 fd = get_pango_context()->get_font_description();
337                         }
338                         printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
339                                         get_name().c_str(),
340                                         char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
341                                         ceil(char_pixel_height() * BASELINESTRETCH),
342                                         _text_width, _text_height,
343                                         get_width(), get_height(),
344                                         layout_font ? "L:" : "W:",
345                                         fd.to_string().c_str());
346                 }
347 #endif
348
349                 cairo_save (cr);
350                 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
351                 cairo_clip(cr);
352
353                 cairo_new_path (cr);
354                 ArdourCanvas::set_source_rgba (cr, text_color);
355                 const double text_ypos = (get_height() - _text_height) * .5;
356
357                 if (_elements & Menu) {
358                         // always left align (dropdown)
359                         cairo_move_to (cr, text_margin, text_ypos);
360                         pango_cairo_show_layout (cr, _layout->gobj());
361                 } else if ( (_elements & Indicator)  == Indicator) {
362                         // left/right align depending on LED position
363                         if (_led_left) {
364                                 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
365                         } else {
366                                 cairo_move_to (cr, text_margin, text_ypos);
367                         }
368                         pango_cairo_show_layout (cr, _layout->gobj());
369                 } else {
370                         /* centered text otherwise */
371                         double ww, wh;
372                         double xa, ya;
373                         ww = get_width();
374                         wh = get_height();
375
376                         cairo_save (cr);
377                         cairo_rotate(cr, _angle * M_PI / 180.0);
378                         cairo_device_to_user(cr, &ww, &wh);
379                         xa = (ww - _text_width) * _xalign;
380                         ya = (wh - _text_height) * _yalign;
381
382                         /* quick hack for left/bottom alignment at -90deg
383                          * TODO this should be generalized incl rotation.
384                          * currently only 'user' of this API is meter_strip.cc
385                          */
386                         if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
387
388                         cairo_move_to (cr, xa, ya);
389                         pango_cairo_update_layout(cr, _layout->gobj());
390                         pango_cairo_show_layout (cr, _layout->gobj());
391                         cairo_restore (cr);
392                 }
393                 cairo_restore (cr);
394         }
395
396         //Menu "triangle"
397         if (_elements & Menu) {
398                 const float trih = ceil(_diameter * .5);
399                 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
400                 //menu arrow
401                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
402                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
403                 cairo_rel_line_to(cr, -triw2, -trih);
404                 cairo_rel_line_to(cr, 2. * triw2, 0);
405                 cairo_close_path(cr);
406
407                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
408                 cairo_fill(cr);
409
410                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
411                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
412                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
413                 cairo_close_path(cr);
414                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
415                 cairo_set_line_width(cr, 1);
416                 cairo_stroke(cr);
417         }
418
419         //Indicator LED
420         if (_elements & Indicator) {
421                 cairo_save (cr);
422
423                 /* move to the center of the indicator/led */
424                 if (_elements & Text) {
425                         int led_xoff = ceil(char_pixel_width() + _diameter * .5);
426                         if (_led_left) {
427                                 cairo_translate (cr, led_xoff, get_height() * .5);
428                         } else {
429                                 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
430                         }
431                 } else {
432                         cairo_translate (cr, get_width() * .5, get_height() * .5);
433                 }
434
435                 //inset
436                 if (!_flat_buttons) {
437                         cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
438                         cairo_set_source (cr, led_inset_pattern);
439                         cairo_fill (cr);
440                 }
441
442                 //black ring
443                 cairo_set_source_rgb (cr, 0, 0, 0);
444                 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
445                 cairo_fill(cr);
446
447                 //led color
448                 ArdourCanvas::set_source_rgba (cr, led_color);
449                 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
450                 cairo_fill(cr);
451
452                 cairo_restore (cr);
453         }
454
455         // a transparent overlay to indicate insensitivity
456         if ((visual_state() & Gtkmm2ext::Insensitive)) {
457                 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
458                 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
459                 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
460                 cairo_fill (cr);
461         }
462
463         // if requested, show hovering
464         if (UIConfiguration::instance().get_widget_prelight()
465                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
466                 if (_hovering) {
467                         rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
468                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
469                         cairo_fill (cr);
470                 }
471         }
472
473         //user is currently pressing the button. dark outline helps to indicate this
474         if (_grabbed && !(_elements & (Inactive|Menu))) {
475                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
476                 cairo_set_line_width(cr, 2);
477                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
478                 cairo_stroke (cr);
479         }
480
481         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
482         if (visual_state() & Gtkmm2ext::Selected) {
483                 cairo_set_line_width(cr, 1);
484                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
485                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
486                 cairo_stroke (cr);
487         }
488
489         //I guess this means we have keyboard focus.  I don't think this works currently
490         //
491         //A: yes, it's keyboard focus and it does work when there's no editor window
492         //   (the editor is always the first receiver for KeyDown).
493         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
494         if (_focused) {
495                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
496                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
497                 double dashes = 1;
498                 cairo_set_dash (cr, &dashes, 1, 0);
499                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
500                 cairo_set_line_width (cr, 1.0);
501                 cairo_stroke (cr);
502                 cairo_set_dash (cr, 0, 0, 0);
503         }
504 }
505
506 void
507 ArdourButton::set_corner_radius (float r)
508 {
509         _corner_radius = r;
510         CairoWidget::set_dirty ();
511 }
512
513 void
514 ArdourButton::on_realize()
515 {
516         CairoWidget::on_realize ();
517         ensure_layout ();
518         if (_layout && _layout->get_text() != _text) {
519                 _layout->set_text (_text);
520                 queue_resize ();
521         }
522 }
523
524 void
525 ArdourButton::on_size_request (Gtk::Requisition* req)
526 {
527         req->width = req->height = 0;
528         CairoWidget::on_size_request (req);
529
530         if (_diameter == 0) {
531                 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
532                 if (_diameter != newdia) {
533                         _pattern_height = 0;
534                         _diameter = newdia;
535                 }
536         }
537
538         if ((_elements & Text) && !_text.empty()) {
539                 // if _layout does not exist, char_pixel_height() creates it,
540                 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
541                 _layout->get_pixel_size (_text_width, _text_height);
542                 req->width += rint(1.75 * char_pixel_width()); // padding
543                 req->width += _text_width;
544         } else {
545                 _text_width = 0;
546                 _text_height = 0;
547         }
548
549         if (_pixbuf) {
550                 req->width += _pixbuf->get_width() + char_pixel_width();
551                 req->height = std::max(req->height, _pixbuf->get_height() + 4);
552         }
553
554         if (_elements & Indicator) {
555                 req->width += lrint (_diameter) + char_pixel_width();
556                 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
557         }
558
559         if ((_elements & Menu)) {
560                 req->width += _diameter + 4;
561         }
562
563         if (_elements & VectorIcon) {
564                 assert(!(_elements & Text));
565                 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
566                 req->width += wh;
567                 req->height = std::max(req->height, wh);
568         }
569
570         /* Tweaks to mess the nice stuff above up again. */
571         if (_tweaks & TrackHeader) {
572                 // forget everything above and just use a fixed square [em] size
573                 // "TrackHeader Buttons" are single letter (usually uppercase)
574                 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
575                 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
576                 req->width  = wh;
577                 req->height = wh;
578         }
579         else if (_tweaks & Square) {
580                 // currerntly unused (again)
581                 if (req->width < req->height)
582                         req->width = req->height;
583                 if (req->height < req->width)
584                         req->height = req->width;
585         } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
586                 // properly centered text for those elements that are centered
587                 // (no sub-pixel offset)
588                 if ((req->width - _text_width) & 1) { ++req->width; }
589                 if ((req->height - _text_height) & 1) { ++req->height; }
590         }
591 #if 0
592                 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
593 #endif
594 }
595
596 /**
597  * This sets the colors used for rendering based on the name of the button, and
598  * thus uses information from the GUI config data.
599  */
600 void
601 ArdourButton::set_colors ()
602 {
603         _update_colors = false;
604         if (_fixed_colors_set) {
605                 return;
606         }
607         std::string name = get_name();
608         bool failed = false;
609
610         fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
611         if (failed) {
612                 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
613         }
614         fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
615         if (failed) {
616                 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
617         }
618
619         text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
620         text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
621
622         led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
623         if (failed) {
624                 led_active_color = UIConfiguration::instance().color ("generic button: led active");
625         }
626
627         /* The inactive color for the LED is just a fairly dark version of the
628          * active color.
629          */
630
631         ArdourCanvas::HSV inactive (led_active_color);
632         inactive.v = 0.35;
633
634         led_inactive_color = inactive.color ();
635 }
636
637 /**
638  * This sets the colors used for rendering based on two fixed values, rather
639  * than basing them on the button name, and thus information in the GUI config
640  * data.
641  */
642 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
643 {
644         _fixed_colors_set = true;
645
646         fill_active_color = color_active;
647         fill_inactive_color = color_inactive;
648
649         unsigned char r, g, b, a;
650         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
651
652         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
653                 (max (double(g), 255.) - min (double(g), 255.)) +
654                 (max (double(b), 255.) - min (double(b), 255.));
655
656         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
657                 (max (double(g), 0.) - min (double(g), 0.)) +
658                 (max (double(b), 0.) - min (double(b), 0.));
659
660         text_active_color = (white_contrast > black_contrast) ?
661                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
662                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
663
664
665         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
666
667         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
668                 (max (double(g), 255.) - min (double(g), 255.)) +
669                 (max (double(b), 255.) - min (double(b), 255.));
670
671         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
672                 (max (double(g), 0.) - min (double(g), 0.)) +
673                 (max (double(b), 0.) - min (double(b), 0.));
674
675         text_inactive_color = (white_contrast > black_contrast) ?
676                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
677                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
678
679         /* XXX what about led colors ? */
680         CairoWidget::set_dirty ();
681 }
682
683 void
684 ArdourButton::build_patterns ()
685 {
686         if (convex_pattern) {
687                 cairo_pattern_destroy (convex_pattern);
688                 convex_pattern = 0;
689         }
690
691         if (concave_pattern) {
692                 cairo_pattern_destroy (concave_pattern);
693                 concave_pattern = 0;
694         }
695
696         if (led_inset_pattern) {
697                 cairo_pattern_destroy (led_inset_pattern);
698                 led_inset_pattern = 0;
699         }
700
701         //convex gradient
702         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
703         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
704         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
705
706         //concave gradient
707         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
708         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
709         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
710
711         led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
712         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
713         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
714
715         _pattern_height = get_height() ;
716 }
717
718 void
719 ArdourButton::set_led_left (bool yn)
720 {
721         _led_left = yn;
722 }
723
724 bool
725 ArdourButton::on_button_press_event (GdkEventButton *ev)
726 {
727         focus_handler ();
728
729         if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
730                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
731                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
732                         return true;
733                 }
734         }
735
736         if (binding_proxy.button_press_handler (ev)) {
737                 return true;
738         }
739
740         _grabbed = true;
741         CairoWidget::set_dirty ();
742
743         if (ev->button == 1 && !_act_on_release) {
744                 if (_action) {
745                         _action->activate ();
746                         return true;
747                 }
748         }
749
750         if (_fallthrough_to_parent)
751                 return false;
752
753         return true;
754 }
755
756 bool
757 ArdourButton::on_button_release_event (GdkEventButton *ev)
758 {
759         if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
760                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
761                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
762                         signal_led_clicked(ev); /* EMIT SIGNAL */
763                         return true;
764                 }
765         }
766
767         _grabbed = false;
768         CairoWidget::set_dirty ();
769
770         if (ev->button == 1 && _hovering) {
771                 signal_clicked ();
772                 if (_act_on_release) {
773                         if (_action) {
774                                 _action->activate ();
775                                 return true;
776                         }
777                 }
778         }
779
780         if (_fallthrough_to_parent)
781                 return false;
782
783         return true;
784 }
785
786 void
787 ArdourButton::set_distinct_led_click (bool yn)
788 {
789         _distinct_led_click = yn;
790         setup_led_rect ();
791 }
792
793 void
794 ArdourButton::color_handler ()
795 {
796         _update_colors = true;
797         CairoWidget::set_dirty ();
798 }
799
800 void
801 ArdourButton::on_size_allocate (Allocation& alloc)
802 {
803         CairoWidget::on_size_allocate (alloc);
804         setup_led_rect ();
805 }
806
807 void
808 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
809 {
810         watch_connection.disconnect ();
811         binding_proxy.set_controllable (c);
812 }
813
814 void
815 ArdourButton::watch ()
816 {
817         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
818
819         if (!c) {
820                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
821                 return;
822         }
823         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
824 }
825
826 void
827 ArdourButton::controllable_changed ()
828 {
829         float val = binding_proxy.get_controllable()->get_value();
830
831         if (fabs (val) >= 0.5f) {
832                 set_active_state (Gtkmm2ext::ExplicitActive);
833         } else {
834                 unset_active_state ();
835         }
836         CairoWidget::set_dirty ();
837 }
838
839 void
840 ArdourButton::set_related_action (RefPtr<Action> act)
841 {
842         Gtkmm2ext::Activatable::set_related_action (act);
843
844         if (_action) {
845
846                 action_tooltip_changed ();
847
848                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
849                 if (tact) {
850                         action_toggled ();
851                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
852                 }
853
854                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
855                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
856                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
857         }
858 }
859
860 void
861 ArdourButton::action_toggled ()
862 {
863         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
864
865         if (tact) {
866                 if (tact->get_active()) {
867                         set_active_state (Gtkmm2ext::ExplicitActive);
868                 } else {
869                         unset_active_state ();
870                 }
871         }
872 }
873
874 void
875 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
876 {
877         _update_colors = true;
878         CairoWidget::set_dirty ();
879 }
880
881 void
882 ArdourButton::on_name_changed ()
883 {
884         _char_pixel_width = 0;
885         _char_pixel_height = 0;
886         _diameter = 0;
887         _update_colors = true;
888         if (is_realized()) {
889                 queue_resize ();
890         }
891 }
892
893 void
894 ArdourButton::setup_led_rect ()
895 {
896         if (!(_elements & Indicator)) {
897                 delete _led_rect;
898                 _led_rect = 0;
899                 return;
900         }
901
902         if (!_led_rect) {
903                 _led_rect = new cairo_rectangle_t;
904         }
905
906         if (_elements & Text) {
907                 if (_led_left) {
908                         _led_rect->x = char_pixel_width();
909                 } else {
910                         _led_rect->x = get_width() - char_pixel_width() + _diameter;
911                 }
912         } else {
913                 /* centered */
914                 _led_rect->x = .5 * get_width() - _diameter;
915         }
916
917         _led_rect->y = .5 * (get_height() - _diameter);
918         _led_rect->width = _diameter;
919         _led_rect->height = _diameter;
920 }
921
922 void
923 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
924 {
925         _pixbuf = img;
926         if (is_realized()) {
927                 queue_resize ();
928         }
929 }
930
931 void
932 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
933 {
934         bool changed = (_active_state != s);
935         CairoWidget::set_active_state (s);
936         if (changed) {
937                 _update_colors = true;
938                 CairoWidget::set_dirty ();
939         }
940 }
941
942 void
943 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
944 {
945         bool changed = (_visual_state != s);
946         CairoWidget::set_visual_state (s);
947         if (changed) {
948                 _update_colors = true;
949                 CairoWidget::set_dirty ();
950         }
951 }
952
953 bool
954 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
955 {
956         _focused = true;
957         CairoWidget::set_dirty ();
958         return CairoWidget::on_focus_in_event (ev);
959 }
960
961 bool
962 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
963 {
964         _focused = false;
965         CairoWidget::set_dirty ();
966         return CairoWidget::on_focus_out_event (ev);
967 }
968
969 bool
970 ArdourButton::on_key_release_event (GdkEventKey *ev) {
971         if (_focused &&
972                         (ev->keyval == GDK_space || ev->keyval == GDK_Return))
973         {
974                 signal_clicked();
975                 if (_action) {
976                         _action->activate ();
977                 }
978                 return true;
979         }
980         return CairoWidget::on_key_release_event (ev);
981 }
982
983 bool
984 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
985 {
986         _hovering = (_elements & Inactive) ? false : true;
987
988         if (UIConfiguration::instance().get_widget_prelight()) {
989                 CairoWidget::set_dirty ();
990         }
991
992         return CairoWidget::on_enter_notify_event (ev);
993 }
994
995 bool
996 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
997 {
998         _hovering = false;
999
1000         if (UIConfiguration::instance().get_widget_prelight()) {
1001                 CairoWidget::set_dirty ();
1002         }
1003
1004         return CairoWidget::on_leave_notify_event (ev);
1005 }
1006
1007 void
1008 ArdourButton::set_tweaks (Tweaks t)
1009 {
1010         if (_tweaks != t) {
1011                 _tweaks = t;
1012                 if (is_realized()) {
1013                         queue_resize ();
1014                 }
1015         }
1016 }
1017
1018 void
1019 ArdourButton::action_sensitivity_changed ()
1020 {
1021         if (_action->property_sensitive ()) {
1022                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1023         } else {
1024                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1025         }
1026 }
1027
1028 void
1029 ArdourButton::set_layout_ellipsize_width (int w)
1030 {
1031         if (_layout_ellipsize_width == w) {
1032                 return;
1033         }
1034         _layout_ellipsize_width = w;
1035         if (!_layout) {
1036                 return;
1037         }
1038         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1039                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1040         }
1041         if (is_realized ()) {
1042                 queue_resize ();
1043         }
1044 }
1045
1046 void
1047 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1048 {
1049         if (_ellipsis == e) {
1050                 return;
1051         }
1052         _ellipsis = e;
1053         if (!_layout) {
1054                 return;
1055         }
1056         _layout->set_ellipsize(_ellipsis);
1057         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1058                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1059         }
1060         if (is_realized ()) {
1061                 queue_resize ();
1062         }
1063 }
1064
1065 void
1066 ArdourButton::ensure_layout ()
1067 {
1068         if (!_layout) {
1069                 ensure_style ();
1070                 _layout = Pango::Layout::create (get_pango_context());
1071                 _layout->set_ellipsize(_ellipsis);
1072                 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1073                         _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1074                 }
1075         }
1076 }
1077
1078 void
1079 ArdourButton::recalc_char_pixel_geometry ()
1080 {
1081         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1082                 return;
1083         }
1084         ensure_layout();
1085         // NB. this is not static, since the geometry is different
1086         // depending on the font used.
1087         int w, h;
1088         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1089         _layout->set_text (x);
1090         _layout->get_pixel_size (w, h);
1091         _char_pixel_height = std::max(4, h);
1092         // number of actual chars in the string (not bytes)
1093         // Glib to the rescue.
1094         Glib::ustring gx(x);
1095         _char_avg_pixel_width = w / (float)gx.size();
1096         _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1097         _layout->set_text (_text);
1098 }
1099
1100 void
1101 ArdourButton::action_visibility_changed ()
1102 {
1103         if (_action->property_visible ()) {
1104                 show ();
1105         } else {
1106                 hide ();
1107         }
1108 }
1109
1110 void
1111 ArdourButton::action_tooltip_changed ()
1112 {
1113         string str = _action->property_tooltip().get_value();
1114         set_tooltip (*this, str);
1115 }
1116
1117 void
1118 ArdourButton::set_elements (Element e)
1119 {
1120         _elements = e;
1121         CairoWidget::set_dirty ();
1122 }
1123
1124 void
1125 ArdourButton::add_elements (Element e)
1126 {
1127         _elements = (ArdourButton::Element) (_elements | e);
1128         CairoWidget::set_dirty ();
1129 }
1130
1131 void
1132 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1133 {
1134         _icon = i;
1135         _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1136         CairoWidget::set_dirty ();
1137 }
1138
1139 void
1140 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1141 {
1142         if (led_custom_color == c && use_custom_led_color == useit) {
1143                 return;
1144         }
1145
1146         led_custom_color = c;
1147         use_custom_led_color = useit;
1148         CairoWidget::set_dirty ();
1149 }