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