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