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