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