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