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