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