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