1c2862926fde5d40e0dd1960277d9ca9039ccc08
[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 "canvas/utils.h"
37 #include "canvas/colors.h"
38
39 #include "ardour_button.h"
40 #include "ardour_ui.h"
41 #include "global_signals.h"
42
43 #include "i18n.h"
44
45 #define BASELINESTRETCH (1.25)
46 #define TRACKHEADERBTNW (3.10)
47
48 using namespace Gdk;
49 using namespace Gtk;
50 using namespace Glib;
51 using namespace PBD;
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 (ArdourButton::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         ARDOUR_UI_UTILS::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         ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
139         ARDOUR_UI_UTILS::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 void
197 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
198 {
199         uint32_t text_color;
200         uint32_t led_color;
201
202         const double dpiscale = ARDOUR_UI::config()->get_font_scale () / 102400.;
203         const double corner_radius = std::max(2.0, _corner_radius * dpiscale);
204
205         if (_update_colors) {
206                 set_colors ();
207         }
208         if (get_height() != _pattern_height) {
209                 build_patterns ();
210         }
211
212         if ( active_state() == Gtkmm2ext::ExplicitActive ) {
213                 text_color = text_active_color;
214                 led_color = led_active_color;
215         } else {
216                 text_color = text_inactive_color;
217                 led_color = led_inactive_color;
218         }
219
220         if (use_custom_led_color) {
221                 led_color = led_custom_color;
222         }
223
224         void (*rounded_function)(cairo_t*, double, double, double, double, double);
225
226         switch (_corner_mask) {
227         case 0x1: /* upper left only */
228                 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
229                 break;
230         case 0x2: /* upper right only */
231                 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
232                 break;
233         case 0x3: /* upper only */
234                 rounded_function = Gtkmm2ext::rounded_top_rectangle;
235                 break;
236                 /* should really have functions for lower right, lower left,
237                    lower only, but for now, we don't
238                 */
239         default:
240                 rounded_function = Gtkmm2ext::rounded_rectangle;
241         }
242
243         // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
244         if ((_elements & (Body|Edge)) == (Body|Edge)) {
245                 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
246                 cairo_set_source_rgba (cr, 0, 0, 0, 1);
247                 cairo_fill(cr);
248         }
249
250         // background fill
251         if ((_elements & Body)==Body) {
252                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
253                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
254                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
255                         cairo_fill (cr);
256                 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
257                         //background color
258                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
259                         cairo_fill (cr);
260                 } else {  //inactive, or it has an indicator
261                         //background color
262                         ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
263                 }
264                 cairo_fill (cr);
265         }
266
267         // IMPLICIT ACTIVE: draw a border of the active color
268         if ((_elements & Body)==Body) {
269                 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
270                         cairo_set_line_width (cr, 2.0);
271                         rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
272                         ArdourCanvas::set_source_rgba (cr, fill_active_color);
273                         cairo_stroke (cr);
274                 }
275         }
276
277         //show the "convex" or "concave" gradient
278         if (!_flat_buttons) {
279                 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
280                         //concave
281                         cairo_set_source (cr, concave_pattern);
282                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
283                         cairo_fill (cr);
284                 } else {
285                         cairo_set_source (cr, convex_pattern);
286                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
287                         cairo_fill (cr);
288                 }
289         }
290
291 #define VECTORICONSTROKEFILL(fillalpha) \
292         cairo_set_line_width(cr, 1.5); \
293         cairo_set_source_rgba (cr, 0, 0, 0, 1.0); \
294         cairo_stroke_preserve(cr); \
295         cairo_set_source_rgba (cr, 1, 1, 1, (fillalpha)); \
296         cairo_fill(cr);
297
298         //Pixbuf, if any
299         if (_pixbuf) {
300                 double x = rint((get_width() - _pixbuf->get_width()) * .5);
301                 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
302 #if 0 // DEBUG style (print on hover)
303                 if (_hovering || (_elements & Inactive)) {
304                         printf("%s: p:%dx%d (%dx%d)\n",
305                                         get_name().c_str(),
306                                         _pixbuf->get_width(), _pixbuf->get_height(),
307                                         get_width(), get_height());
308                 }
309 #endif
310                 if (_elements & Menu) {
311                         //if this is a DropDown with an icon, then we need to
312                         //move the icon left slightly to accomomodate the arrow
313                         x -= _diameter - 2;
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         else /* VectorIcons are exclusive to Pixbuf Icons */
320         /* TODO separate these into dedicated class
321          * it may also be efficient to render them only once for every size (image-surface) */
322         if ((_elements & VectorIcon) && _icon == RecTapeMode) {
323                 const double x = get_width() * .5;
324                 const double y = get_height() * .5;
325                 const double r = std::min(x, y) * .6;
326                 const double slit = .11 * M_PI;
327                 cairo_save(cr);
328                 cairo_translate(cr, x, y);
329
330                 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
331                 if (active_state() == Gtkmm2ext::ExplicitActive) {
332                         cairo_set_source_rgba (cr, .95, .1, .1, 1.);
333                 } else {
334                         cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
335                 }
336                 cairo_fill_preserve(cr);
337                 cairo_set_source_rgba (cr, .0, .0, .0, .5);
338                 cairo_set_line_width(cr, 1);
339                 cairo_stroke(cr);
340
341                 cairo_save(cr);
342                 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
343
344                 cairo_rotate (cr, -.5 * M_PI);
345                 cairo_move_to(cr, 0, 0);
346                 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
347                 cairo_line_to(cr, 0, 0);
348                 cairo_close_path(cr);
349
350                 cairo_fill(cr);
351                 cairo_rotate (cr, 2. * M_PI / 3.);
352
353                 cairo_move_to(cr, 0, 0);
354                 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
355                 cairo_line_to(cr, 0, 0);
356                 cairo_close_path(cr);
357                 cairo_fill(cr);
358
359                 cairo_rotate (cr, 2. * M_PI / 3.);
360                 cairo_move_to(cr, 0, 0);
361                 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
362                 cairo_line_to(cr, 0, 0);
363                 cairo_close_path(cr);
364                 cairo_fill(cr);
365
366                 cairo_restore(cr);
367
368                 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
369                 if (active_state() == Gtkmm2ext::ExplicitActive)
370                         cairo_set_source_rgba (cr, .95, .1, .1, 1.);
371                 else
372                         cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
373                 cairo_fill(cr);
374                 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
375                 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
376                 cairo_fill(cr);
377
378                 cairo_restore(cr);
379         }
380         else if ((_elements & VectorIcon) && _icon == RecButton) {
381                 const double x = get_width() * .5;
382                 const double y = get_height() * .5;
383                 const double r = std::min(x, y) * .55;
384                 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
385                 if (active_state() == Gtkmm2ext::ExplicitActive)
386                         cairo_set_source_rgba (cr, .95, .1, .1, 1.);
387                 else
388                         cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
389                 cairo_fill_preserve(cr);
390                 cairo_set_source_rgba (cr, .0, .0, .0, .8);
391                 cairo_set_line_width(cr, 1);
392                 cairo_stroke(cr);
393         }
394         else if ((_elements & VectorIcon) && _icon == CloseCross) {
395                 const double x = get_width() * .5;
396                 const double y = get_height() * .5;
397                 const double o = .5 + std::min(x, y) * .4;
398                 ArdourCanvas::set_source_rgba (cr, text_color);
399                 cairo_set_line_width(cr, 1);
400                 cairo_move_to(cr, x-o, y-o);
401                 cairo_line_to(cr, x+o, y+o);
402                 cairo_move_to(cr, x+o, y-o);
403                 cairo_line_to(cr, x-o, y+o);
404                 cairo_stroke(cr);
405         }
406         else if ((_elements & VectorIcon) && _icon == StripWidth) {
407                 const double x0 = get_width()  * .2;
408                 const double x1 = get_width()  * .8;
409
410                 const double y0 = get_height() * .25;
411                 const double y1= get_height()  * .75;
412
413                 const double ym= get_height()  * .5;
414
415                 // arrow
416                 const double xa0= get_height()  * .39;
417                 const double xa1= get_height()  * .61;
418                 const double ya0= get_height()  * .35;
419                 const double ya1= get_height()  * .65;
420
421                 ArdourCanvas::set_source_rgba (cr, text_color);
422                 cairo_set_line_width(cr, 1);
423
424                 // left + right
425                 cairo_move_to(cr, x0, y0);
426                 cairo_line_to(cr, x0, y1);
427                 cairo_move_to(cr, x1, y0);
428                 cairo_line_to(cr, x1, y1);
429
430                 // horiz center line
431                 cairo_move_to(cr, x0, ym);
432                 cairo_line_to(cr, x1, ym);
433
434                 // arrow left
435                 cairo_move_to(cr,  x0, ym);
436                 cairo_line_to(cr, xa0, ya0);
437                 cairo_move_to(cr,  x0, ym);
438                 cairo_line_to(cr, xa0, ya1);
439
440                 // arrow right
441                 cairo_move_to(cr,  x1,  ym);
442                 cairo_line_to(cr, xa1, ya0);
443                 cairo_move_to(cr,  x1,  ym);
444                 cairo_line_to(cr, xa1, ya1);
445                 cairo_stroke(cr);
446         }
447         else if ((_elements & VectorIcon) && _icon == DinMidi) {
448                 const double x = get_width() * .5;
449                 const double y = get_height() * .5;
450                 const double r = std::min(x, y) * .75;
451                 ArdourCanvas::set_source_rgba (cr, text_color);
452                 cairo_set_line_width(cr, 1);
453                 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
454                 cairo_stroke(cr);
455
456                 // pins equally spaced 45deg
457                 cairo_arc (cr, x, y * 0.5, r * .15, 0, 2 * M_PI);
458                 cairo_fill(cr);
459                 cairo_arc (cr, x * 0.5, y, r * .15, 0, 2 * M_PI);
460                 cairo_fill(cr);
461                 cairo_arc (cr, x * 1.5, y, r * .15, 0, 2 * M_PI);
462                 cairo_fill(cr);
463                 //  .5 + .5 * .5 * sin(45deg),  1.5 - .5 * .5 * cos(45deg)
464                 cairo_arc (cr, x * 0.677, y * .677, r * .15, 0, 2 * M_PI);
465                 cairo_fill(cr);
466                 cairo_arc (cr, x * 1.323, y * .677, r * .15, 0, 2 * M_PI);
467                 cairo_fill(cr);
468
469                 // bottom notch
470                 cairo_arc (cr, x, y+r, r * .28, 1.05 * M_PI, 1.95 * M_PI);
471                 cairo_stroke(cr);
472         }
473         else if ((_elements & VectorIcon) && _icon == TransportStop) {
474                 const int wh = std::min (get_width(), get_height());
475                 cairo_rectangle (cr,
476                                 (get_width() - wh) * .5 + wh * .25,
477                                 (get_height() - wh) * .5 + wh * .25,
478                                 wh * .5, wh * .5);
479
480                 VECTORICONSTROKEFILL(0.8);
481         }
482         else if ((_elements & VectorIcon) && _icon == TransportPlay) {
483                 const int wh = std::min (get_width(), get_height()) * .5;
484                 const double y = get_height() * .5;
485                 const double x = get_width() - wh;
486
487                 const float tri = ceil(.577 * wh); // 1/sqrt(3)
488
489                 cairo_move_to (cr,  x + wh * .5, y);
490                 cairo_line_to (cr,  x - wh * .5, y - tri);
491                 cairo_line_to (cr,  x - wh * .5, y + tri);
492                 cairo_close_path (cr);
493
494                 VECTORICONSTROKEFILL(0.8);
495         }
496         else if ((_elements & VectorIcon) && _icon == TransportPanic) {
497                 const int wh = std::min (get_width(), get_height()) * .1;
498                 const double xc = get_width() * .5;
499                 const double yh = get_height();
500                 cairo_rectangle (cr,
501                                 xc - wh, yh *.2,
502                                 wh * 2,  yh *.4);
503                 VECTORICONSTROKEFILL(0.8);
504
505                 cairo_arc (cr, xc, yh *.75, wh, 0, 2 * M_PI);
506                 VECTORICONSTROKEFILL(0.8);
507         }
508         else if ((_elements & VectorIcon) && (_icon == TransportStart || _icon == TransportEnd || _icon == TransportRange)) {
509                 // small play triangle
510                 int wh = std::min (get_width(), get_height());
511                 const double y = get_height() * .5;
512                 const double x = get_width() - wh * .5;
513                 wh *= .18;
514                 const float tri = ceil(.577 * wh * 2); // 1/sqrt(3)
515
516                 const float ln = std::min (get_width(), get_height()) * .07;
517
518                 if (_icon == TransportStart || _icon == TransportRange) {
519                         cairo_rectangle (cr,
520                                          x - wh - ln, y  - tri * 1.7,
521                                          ln * 2,  tri * 3.4);
522
523                         VECTORICONSTROKEFILL(1.0);
524                 }
525
526                 if (_icon == TransportEnd || _icon == TransportRange) {
527                         cairo_rectangle (cr,
528                                          x + wh - ln, y  - tri * 1.7,
529                                          ln * 2,  tri * 3.4);
530
531                         VECTORICONSTROKEFILL(1.0);
532                 }
533
534                 if (_icon == TransportStart) {
535                         cairo_move_to (cr,  x - wh, y);
536                         cairo_line_to (cr,  x + wh, y - tri);
537                         cairo_line_to (cr,  x + wh, y + tri);
538                 } else {
539                         cairo_move_to (cr,  x + wh, y);
540                         cairo_line_to (cr,  x - wh, y - tri);
541                         cairo_line_to (cr,  x - wh, y + tri);
542                 }
543
544                 cairo_close_path (cr);
545                 VECTORICONSTROKEFILL(1.0);
546         }
547         else if ((_elements & VectorIcon) && _icon == TransportLoop) {
548                 const double x = get_width() * .5;
549                 const double y = get_height() * .5;
550                 const double r = std::min(x, y);
551
552                 cairo_arc (cr, x, y, r * .6, 0, 2 * M_PI);
553                 cairo_arc_negative (cr, x, y, r * .3, 2 * M_PI, 0);
554
555                 VECTORICONSTROKEFILL(1.0);
556 #define ARCARROW(rad, ang) \
557                 x + (rad) * sin((ang) * 2.0 * M_PI), y + (rad) * cos((ang) * 2.0 * M_PI)
558
559                 cairo_move_to (cr, ARCARROW(r * .30, .72));
560                 cairo_line_to (cr, ARCARROW(r * .08, .72));
561                 cairo_line_to (cr, ARCARROW(r * .54, .60));
562                 cairo_line_to (cr, ARCARROW(r * .73, .72));
563                 cairo_line_to (cr, ARCARROW(r * .60, .72));
564
565                 cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
566                 cairo_stroke_preserve(cr);
567                 cairo_close_path (cr);
568                 cairo_set_source_rgba (cr, 1, 1, 1, 1.0);
569                 cairo_fill(cr);
570 #undef ARCARROW
571         }
572         else if ((_elements & VectorIcon) && _icon == TransportMetronom) {
573                 const double x  = get_width() * .5;
574                 const double y  = get_height() * .5;
575                 const double wh = std::min(x, y);
576                 const double h  = wh * .8;
577                 const double w  = wh * .5;
578                 const double lw = w * .25;
579
580                 cairo_rectangle (cr,
581                                  x - w * .7, y + h * .25,
582                                  w * 1.4, lw);
583
584                 VECTORICONSTROKEFILL(1.0);
585
586                 cairo_move_to (cr,  x - w,       y + h);
587                 cairo_line_to (cr,  x + w,       y + h);
588                 cairo_line_to (cr,  x + w * .35, y - h);
589                 cairo_line_to (cr,  x - w * .35, y - h);
590                 cairo_line_to (cr,  x - w,       y + h);
591
592                 cairo_move_to (cr,  x - w + lw,       y + h -lw);
593                 cairo_line_to (cr,  x - w * .35 + lw, y - h + lw);
594                 cairo_line_to (cr,  x + w * .35 - lw, y - h + lw);
595                 cairo_line_to (cr,  x + w - lw,       y + h -lw);
596                 cairo_line_to (cr,  x - w + lw,       y + h -lw);
597
598                 VECTORICONSTROKEFILL(1.0);
599
600                 // ddx = .70 w      = .75 * .5 wh              = .375 wh
601                 // ddy = .75 h - lw = .75 * .8 wh - wh .5 * .2 = .5 wh
602                 // ang = (ddx/ddy):
603                 // -> angle = atan (ang) = atan (375 / .5) ~= 36deg
604                 const double dx = lw * .2;  // 1 - cos(tan^-1(ang))
605                 const double dy = lw * .4;  // 1 - sin(tan^-1(ang))
606                 cairo_move_to (cr,  x - w * .3     , y + h * .25 + lw * .5);
607                 cairo_line_to (cr,  x - w + dx     , y - h + lw + dy);
608                 cairo_line_to (cr,  x - w + lw     , y - h + lw);
609                 cairo_line_to (cr,  x - w * .3 + lw, y + h * .25 + lw * .5);
610                 cairo_close_path (cr);
611
612                 VECTORICONSTROKEFILL(1.0);
613
614                 cairo_rectangle (cr,
615                                  x - w * .7, y + h * .25,
616                                  w * 1.4, lw);
617                 cairo_fill(cr);
618         }
619         else if (_elements & VectorIcon) {
620                 // missing icon
621                 assert(0);
622         }
623 #undef VECTORICONSTROKEFILL
624
625         const int text_margin = char_pixel_width();
626         // Text, if any
627         if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
628                 assert(_layout);
629 #if 0 // DEBUG style (print on hover)
630                 if (_hovering || (_elements & Inactive)) {
631                         bool layout_font = true;
632                         Pango::FontDescription fd = _layout->get_font_description();
633                         if (fd.gobj() == NULL) {
634                                 layout_font = false;
635                                 fd = get_pango_context()->get_font_description();
636                         }
637                         printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
638                                         get_name().c_str(),
639                                         char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
640                                         ceil(char_pixel_height() * BASELINESTRETCH),
641                                         _text_width, _text_height,
642                                         get_width(), get_height(),
643                                         layout_font ? "L:" : "W:",
644                                         fd.to_string().c_str());
645                 }
646 #endif
647
648                 cairo_save (cr);
649                 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
650                 cairo_clip(cr);
651
652                 cairo_new_path (cr);
653                 ArdourCanvas::set_source_rgba (cr, text_color);
654                 const double text_ypos = (get_height() - _text_height) * .5;
655
656                 if (_elements & Menu) {
657                         // always left align (dropdown)
658                         cairo_move_to (cr, text_margin, text_ypos);
659                         pango_cairo_show_layout (cr, _layout->gobj());
660                 } else if ( (_elements & Indicator)  == Indicator) {
661                         // left/right align depending on LED position
662                         if (_led_left) {
663                                 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
664                         } else {
665                                 cairo_move_to (cr, text_margin, text_ypos);
666                         }
667                         pango_cairo_show_layout (cr, _layout->gobj());
668                 } else {
669                         /* centered text otherwise */
670                         double ww, wh;
671                         double xa, ya;
672                         ww = get_width();
673                         wh = get_height();
674
675                         cairo_save (cr);
676                         cairo_rotate(cr, _angle * M_PI / 180.0);
677                         cairo_device_to_user(cr, &ww, &wh);
678                         xa = (ww - _text_width) * _xalign;
679                         ya = (wh - _text_height) * _yalign;
680
681                         /* quick hack for left/bottom alignment at -90deg
682                          * TODO this should be generalized incl rotation.
683                          * currently only 'user' of this API is meter_strip.cc
684                          */
685                         if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
686
687                         cairo_move_to (cr, xa, ya);
688                         pango_cairo_update_layout(cr, _layout->gobj());
689                         pango_cairo_show_layout (cr, _layout->gobj());
690                         cairo_restore (cr);
691                 }
692                 cairo_restore (cr);
693         }
694
695         //Menu "triangle"
696         if (_elements & Menu) {
697                 const float trih = ceil(_diameter * .5);
698                 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
699                 //menu arrow
700                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
701                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
702                 cairo_rel_line_to(cr, -triw2, -trih);
703                 cairo_rel_line_to(cr, 2. * triw2, 0);
704                 cairo_close_path(cr);
705
706                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
707                 cairo_fill(cr);
708
709                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
710                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
711                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
712                 cairo_close_path(cr);
713                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
714                 cairo_set_line_width(cr, 1);
715                 cairo_stroke(cr);
716         }
717
718         //Indicator LED
719         if (_elements & Indicator) {
720                 cairo_save (cr);
721
722                 /* move to the center of the indicator/led */
723                 if (_elements & Text) {
724                         int led_xoff = ceil(char_pixel_width() + _diameter * .5);
725                         if (_led_left) {
726                                 cairo_translate (cr, led_xoff, get_height() * .5);
727                         } else {
728                                 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
729                         }
730                 } else {
731                         cairo_translate (cr, get_width() * .5, get_height() * .5);
732                 }
733
734                 //inset
735                 if (!_flat_buttons) {
736                         cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
737                         cairo_set_source (cr, led_inset_pattern);
738                         cairo_fill (cr);
739                 }
740
741                 //black ring
742                 cairo_set_source_rgb (cr, 0, 0, 0);
743                 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * dpiscale, 0, 2 * M_PI);
744                 cairo_fill(cr);
745
746                 //led color
747                 ArdourCanvas::set_source_rgba (cr, led_color);
748                 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * dpiscale, 0, 2 * M_PI);
749                 cairo_fill(cr);
750
751                 cairo_restore (cr);
752         }
753
754         // a transparent overlay to indicate insensitivity
755         if ((visual_state() & Gtkmm2ext::Insensitive)) {
756                 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
757                 uint32_t ins_color = ARDOUR_UI::config()->color ("gtk_background");
758                 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
759                 cairo_fill (cr);
760         }
761
762         // if requested, show hovering
763         if (ARDOUR_UI::config()->get_widget_prelight()
764                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
765                 if (_hovering) {
766                         rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
767                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
768                         cairo_fill (cr);
769                 }
770         }
771
772         //user is currently pressing the button. dark outline helps to indicate this
773         if (_grabbed && !(_elements & (Inactive|Menu))) {
774                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
775                 cairo_set_line_width(cr, 2);
776                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
777                 cairo_stroke (cr);
778         }
779
780         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
781         if (visual_state() & Gtkmm2ext::Selected) {
782                 cairo_set_line_width(cr, 1);
783                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
784                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
785                 cairo_stroke (cr);
786         }
787
788         //I guess this means we have keyboard focus.  I don't think this works currently
789         //
790         //A: yes, it's keyboard focus and it does work when there's no editor window
791         //   (the editor is always the first receiver for KeyDown).
792         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
793         if (_focused) {
794                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
795                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
796                 double dashes = 1;
797                 cairo_set_dash (cr, &dashes, 1, 0);
798                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
799                 cairo_set_line_width (cr, 1.0);
800                 cairo_stroke (cr);
801                 cairo_set_dash (cr, 0, 0, 0);
802         }
803 }
804
805 void
806 ArdourButton::set_corner_radius (float r)
807 {
808         _corner_radius = r;
809         CairoWidget::set_dirty ();
810 }
811
812 void
813 ArdourButton::on_realize()
814 {
815         CairoWidget::on_realize ();
816         ensure_layout ();
817         if (_layout && _layout->get_text() != _text) {
818                 _layout->set_text (_text);
819                 queue_resize ();
820         }
821 }
822
823 void
824 ArdourButton::on_size_request (Gtk::Requisition* req)
825 {
826         req->width = req->height = 0;
827         CairoWidget::on_size_request (req);
828
829         if (_diameter == 0) {
830                 const float newdia = rint (11. * ARDOUR_UI::config()->get_font_scale () / 102400.);
831                 if (_diameter != newdia) {
832                         _pattern_height = 0;
833                         _diameter = newdia;
834                 }
835         }
836
837         if ((_elements & Text) && !_text.empty()) {
838                 // if _layout does not exist, char_pixel_height() creates it,
839                 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
840                 _layout->get_pixel_size (_text_width, _text_height);
841                 req->width += rint(1.75 * char_pixel_width()); // padding
842                 req->width += _text_width;
843         } else {
844                 _text_width = 0;
845                 _text_height = 0;
846         }
847
848         if (_pixbuf) {
849                 req->width += _pixbuf->get_width() + char_pixel_width();
850                 req->height = std::max(req->height, _pixbuf->get_height() + 4);
851         }
852
853         if (_elements & Indicator) {
854                 req->width += lrint (_diameter) + char_pixel_width();
855                 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
856         }
857
858         if ((_elements & Menu)) {
859                 req->width += _diameter + 4;
860         }
861
862         if (_elements & VectorIcon) {
863                 assert(!(_elements & Text));
864                 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
865                 req->width += wh;
866                 req->height = std::max(req->height, wh);
867         }
868
869         /* Tweaks to mess the nice stuff above up again. */
870         if (_tweaks & TrackHeader) {
871                 // forget everything above and just use a fixed square [em] size
872                 // "TrackHeader Buttons" are single letter (usually uppercase)
873                 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
874                 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
875                 req->width  = wh;
876                 req->height = wh;
877         }
878         else if (_tweaks & Square) {
879                 // currerntly unused (again)
880                 if (req->width < req->height)
881                         req->width = req->height;
882                 if (req->height < req->width)
883                         req->height = req->width;
884         } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
885                 // properly centered text for those elements that are centered
886                 // (no sub-pixel offset)
887                 if ((req->width - _text_width) & 1) { ++req->width; }
888                 if ((req->height - _text_height) & 1) { ++req->height; }
889         }
890 #if 0
891                 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
892 #endif
893 }
894
895 /**
896  * This sets the colors used for rendering based on the name of the button, and
897  * thus uses information from the GUI config data.
898  */
899 void
900 ArdourButton::set_colors ()
901 {
902         _update_colors = false;
903         if (_fixed_colors_set) {
904                 return;
905         }
906         std::string name = get_name();
907         bool failed = false;
908
909         fill_active_color = ARDOUR_UI::config()->color (string_compose ("%1: fill active", name), &failed);
910         if (failed) {
911                 fill_active_color = ARDOUR_UI::config()->color ("generic button: fill active");
912         }
913         fill_inactive_color = ARDOUR_UI::config()->color (string_compose ("%1: fill", name), &failed);
914         if (failed) {
915                 fill_inactive_color = ARDOUR_UI::config()->color ("generic button: fill");
916         }
917
918         text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
919         text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
920
921         led_active_color = ARDOUR_UI::config()->color (string_compose ("%1: led active", name), &failed);
922         if (failed) {
923                 led_active_color = ARDOUR_UI::config()->color ("generic button: led active");
924         }
925
926         /* The inactive color for the LED is just a fairly dark version of the
927          * active color.
928          */
929         
930         ArdourCanvas::HSV inactive (led_active_color);
931         inactive.v = 0.35;
932
933         led_inactive_color = inactive.color ();
934 }
935
936 /**
937  * This sets the colors used for rendering based on two fixed values, rather
938  * than basing them on the button name, and thus information in the GUI config
939  * data.
940  */
941 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
942 {
943         _fixed_colors_set = true;
944
945         fill_active_color = color_active;
946         fill_inactive_color = color_inactive;
947
948         unsigned char r, g, b, a;
949         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
950
951         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
952                 (max (double(g), 255.) - min (double(g), 255.)) +
953                 (max (double(b), 255.) - min (double(b), 255.));
954
955         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
956                 (max (double(g), 0.) - min (double(g), 0.)) +
957                 (max (double(b), 0.) - min (double(b), 0.));
958
959         text_active_color = (white_contrast > black_contrast) ?
960                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
961                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
962
963
964         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
965
966         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
967                 (max (double(g), 255.) - min (double(g), 255.)) +
968                 (max (double(b), 255.) - min (double(b), 255.));
969
970         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
971                 (max (double(g), 0.) - min (double(g), 0.)) +
972                 (max (double(b), 0.) - min (double(b), 0.));
973
974         text_inactive_color = (white_contrast > black_contrast) ?
975                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
976                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
977
978         /* XXX what about led colors ? */
979         CairoWidget::set_dirty ();
980 }
981
982 void
983 ArdourButton::build_patterns ()
984 {
985         if (convex_pattern) {
986                 cairo_pattern_destroy (convex_pattern);
987                 convex_pattern = 0;
988         }
989
990         if (concave_pattern) {
991                 cairo_pattern_destroy (concave_pattern);
992                 concave_pattern = 0;
993         }
994
995         if (led_inset_pattern) {
996                 cairo_pattern_destroy (led_inset_pattern);
997                 led_inset_pattern = 0;
998         }
999
1000         //convex gradient
1001         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
1002         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
1003         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
1004
1005         //concave gradient
1006         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
1007         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
1008         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
1009
1010         led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
1011         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
1012         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
1013
1014         _pattern_height = get_height() ;
1015 }
1016
1017 void
1018 ArdourButton::set_led_left (bool yn)
1019 {
1020         _led_left = yn;
1021 }
1022
1023 bool
1024 ArdourButton::on_button_press_event (GdkEventButton *ev)
1025 {
1026         focus_handler ();
1027
1028         if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
1029                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
1030                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
1031                         return true;
1032                 }
1033         }
1034
1035         if (binding_proxy.button_press_handler (ev)) {
1036                 return true;
1037         }
1038
1039         _grabbed = true;
1040         CairoWidget::set_dirty ();
1041
1042         if (ev->button == 1 && !_act_on_release) {
1043                 if (_action) {
1044                         _action->activate ();
1045                         return true;
1046                 }
1047         }
1048
1049         if (_fallthrough_to_parent)
1050                 return false;
1051
1052         return true;
1053 }
1054
1055 bool
1056 ArdourButton::on_button_release_event (GdkEventButton *ev)
1057 {
1058         if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
1059                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
1060                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
1061                         signal_led_clicked(); /* EMIT SIGNAL */
1062                         return true;
1063                 }
1064         }
1065
1066         _grabbed = false;
1067         CairoWidget::set_dirty ();
1068
1069         if (ev->button == 1 && _hovering) {
1070                 signal_clicked ();
1071                 if (_act_on_release) {
1072                         if (_action) {
1073                                 _action->activate ();
1074                                 return true;
1075                         }
1076                 }
1077         }
1078
1079         if (_fallthrough_to_parent)
1080                 return false;
1081
1082         return true;
1083 }
1084
1085 void
1086 ArdourButton::set_distinct_led_click (bool yn)
1087 {
1088         _distinct_led_click = yn;
1089         setup_led_rect ();
1090 }
1091
1092 void
1093 ArdourButton::color_handler ()
1094 {
1095         _update_colors = true;
1096         CairoWidget::set_dirty ();
1097 }
1098
1099 void
1100 ArdourButton::on_size_allocate (Allocation& alloc)
1101 {
1102         CairoWidget::on_size_allocate (alloc);
1103         setup_led_rect ();
1104 }
1105
1106 void
1107 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
1108 {
1109         watch_connection.disconnect ();
1110         binding_proxy.set_controllable (c);
1111 }
1112
1113 void
1114 ArdourButton::watch ()
1115 {
1116         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
1117
1118         if (!c) {
1119                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
1120                 return;
1121         }
1122         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
1123 }
1124
1125 void
1126 ArdourButton::controllable_changed ()
1127 {
1128         float val = binding_proxy.get_controllable()->get_value();
1129
1130         if (fabs (val) >= 0.5f) {
1131                 set_active_state (Gtkmm2ext::ExplicitActive);
1132         } else {
1133                 unset_active_state ();
1134         }
1135         CairoWidget::set_dirty ();
1136 }
1137
1138 void
1139 ArdourButton::set_related_action (RefPtr<Action> act)
1140 {
1141         Gtkmm2ext::Activatable::set_related_action (act);
1142
1143         if (_action) {
1144
1145                 action_tooltip_changed ();
1146
1147                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1148                 if (tact) {
1149                         action_toggled ();
1150                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
1151                 }
1152
1153                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
1154                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
1155                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
1156         }
1157 }
1158
1159 void
1160 ArdourButton::action_toggled ()
1161 {
1162         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1163
1164         if (tact) {
1165                 if (tact->get_active()) {
1166                         set_active_state (Gtkmm2ext::ExplicitActive);
1167                 } else {
1168                         unset_active_state ();
1169                 }
1170         }
1171 }
1172
1173 void
1174 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1175 {
1176         _update_colors = true;
1177         CairoWidget::set_dirty ();
1178 }
1179
1180 void
1181 ArdourButton::on_name_changed ()
1182 {
1183         _char_pixel_width = 0;
1184         _char_pixel_height = 0;
1185         _diameter = 0;
1186         _update_colors = true;
1187         if (is_realized()) {
1188                 queue_resize ();
1189         }
1190 }
1191
1192 void
1193 ArdourButton::setup_led_rect ()
1194 {
1195         if (!(_elements & Indicator)) {
1196                 delete _led_rect;
1197                 _led_rect = 0;
1198                 return;
1199         }
1200
1201         if (!_led_rect) {
1202                 _led_rect = new cairo_rectangle_t;
1203         }
1204
1205         if (_elements & Text) {
1206                 if (_led_left) {
1207                         _led_rect->x = char_pixel_width();
1208                 } else {
1209                         _led_rect->x = get_width() - char_pixel_width() + _diameter;
1210                 }
1211         } else {
1212                 /* centered */
1213                 _led_rect->x = .5 * get_width() - _diameter;
1214         }
1215
1216         _led_rect->y = .5 * (get_height() - _diameter);
1217         _led_rect->width = _diameter;
1218         _led_rect->height = _diameter;
1219 }
1220
1221 void
1222 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1223 {
1224         _pixbuf = img;
1225         if (is_realized()) {
1226                 queue_resize ();
1227         }
1228 }
1229
1230 void
1231 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1232 {
1233         bool changed = (_active_state != s);
1234         CairoWidget::set_active_state (s);
1235         if (changed) {
1236                 _update_colors = true;
1237                 CairoWidget::set_dirty ();
1238         }
1239 }
1240
1241 void
1242 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1243 {
1244         bool changed = (_visual_state != s);
1245         CairoWidget::set_visual_state (s);
1246         if (changed) {
1247                 _update_colors = true;
1248                 CairoWidget::set_dirty ();
1249         }
1250 }
1251
1252 bool
1253 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1254 {
1255         _focused = true;
1256         CairoWidget::set_dirty ();
1257         return CairoWidget::on_focus_in_event (ev);
1258 }
1259
1260 bool
1261 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1262 {
1263         _focused = false;
1264         CairoWidget::set_dirty ();
1265         return CairoWidget::on_focus_out_event (ev);
1266 }
1267
1268 bool
1269 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1270         if (_focused &&
1271                         (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1272         {
1273                 signal_clicked();
1274                 if (_action) {
1275                         _action->activate ();
1276                 }
1277                 return true;
1278         }
1279         return CairoWidget::on_key_release_event (ev);
1280 }
1281
1282 bool
1283 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1284 {
1285         _hovering = (_elements & Inactive) ? false : true;
1286
1287         if (ARDOUR_UI::config()->get_widget_prelight()) {
1288                 CairoWidget::set_dirty ();
1289         }
1290
1291         return CairoWidget::on_enter_notify_event (ev);
1292 }
1293
1294 bool
1295 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1296 {
1297         _hovering = false;
1298
1299         if (ARDOUR_UI::config()->get_widget_prelight()) {
1300                 CairoWidget::set_dirty ();
1301         }
1302
1303         return CairoWidget::on_leave_notify_event (ev);
1304 }
1305
1306 void
1307 ArdourButton::set_tweaks (Tweaks t)
1308 {
1309         if (_tweaks != t) {
1310                 _tweaks = t;
1311                 if (is_realized()) {
1312                         queue_resize ();
1313                 }
1314         }
1315 }
1316
1317 void
1318 ArdourButton::action_sensitivity_changed ()
1319 {
1320         if (_action->property_sensitive ()) {
1321                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1322         } else {
1323                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1324         }
1325 }
1326
1327 void
1328 ArdourButton::set_layout_ellipsize_width (int w)
1329 {
1330         if (_layout_ellipsize_width == w) {
1331                 return;
1332         }
1333         _layout_ellipsize_width = w;
1334         if (!_layout) {
1335                 return;
1336         }
1337         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1338                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1339         }
1340         if (is_realized ()) {
1341                 queue_resize ();
1342         }
1343 }
1344
1345 void
1346 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1347 {
1348         if (_ellipsis == e) {
1349                 return;
1350         }
1351         _ellipsis = e;
1352         if (!_layout) {
1353                 return;
1354         }
1355         _layout->set_ellipsize(_ellipsis);
1356         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1357                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1358         }
1359         if (is_realized ()) {
1360                 queue_resize ();
1361         }
1362 }
1363
1364 void
1365 ArdourButton::ensure_layout ()
1366 {
1367         if (!_layout) {
1368                 ensure_style ();
1369                 _layout = Pango::Layout::create (get_pango_context());
1370                 _layout->set_ellipsize(_ellipsis);
1371                 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1372                         _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1373                 }
1374         }
1375 }
1376
1377 void
1378 ArdourButton::recalc_char_pixel_geometry ()
1379 {
1380         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1381                 return;
1382         }
1383         ensure_layout();
1384         // NB. this is not static, since the geometry is different
1385         // depending on the font used.
1386         int w, h;
1387         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1388         _layout->set_text (x);
1389         _layout->get_pixel_size (w, h);
1390         _char_pixel_height = std::max(4, h);
1391         // number of actual chars in the string (not bytes)
1392         // Glib to the rescue.
1393         Glib::ustring gx(x);
1394         _char_avg_pixel_width = w / (float)gx.size();
1395         _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1396         _layout->set_text (_text);
1397 }
1398
1399 void
1400 ArdourButton::action_visibility_changed ()
1401 {
1402         if (_action->property_visible ()) {
1403                 show ();
1404         } else {
1405                 hide ();
1406         }
1407 }
1408
1409 void
1410 ArdourButton::action_tooltip_changed ()
1411 {
1412         string str = _action->property_tooltip().get_value();
1413         ARDOUR_UI::instance()->set_tip (*this, str);
1414 }
1415
1416 void
1417 ArdourButton::set_elements (Element e)
1418 {
1419         _elements = e;
1420         CairoWidget::set_dirty ();
1421 }
1422
1423 void
1424 ArdourButton::add_elements (Element e)
1425 {
1426         _elements = (ArdourButton::Element) (_elements | e);
1427         CairoWidget::set_dirty ();
1428 }
1429
1430 void
1431 ArdourButton::set_icon (Icon i)
1432 {
1433         _icon = i;
1434         CairoWidget::set_dirty ();
1435 }
1436
1437 void
1438 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1439 {
1440         if (led_custom_color == c && use_custom_led_color == useit) {
1441                 return;
1442         }
1443
1444         led_custom_color = c;
1445         use_custom_led_color = useit;
1446         CairoWidget::set_dirty ();
1447 }