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