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