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