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