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