optimize ArdourButton
[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         // Text, if any
366         if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
367                 assert(_layout);
368 #if 0 // DEBUG style (print on hover)
369                 if (_hovering) {
370                         bool layout_font = true;
371                         Pango::FontDescription fd = _layout->get_font_description();
372                         if (fd.gobj() == NULL) {
373                                 layout_font = false;
374                                 fd = get_pango_context()->get_font_description();
375                         }
376                         printf("%s: f:%dx%d bh:%.0f tw:%d (%dx%d) %s\"%s\"\n",
377                                         get_name().c_str(),
378                                         char_pixel_width(), char_pixel_height(),
379                                         ceil(char_pixel_height() * BASELINESTRETCH),
380                                         _text_width,
381                                         get_width(), get_height(),
382                                         layout_font ? "L:" : "W:",
383                                         fd.to_string().c_str());
384                 }
385 #endif
386
387                 cairo_save (cr);
388                 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
389                 cairo_clip(cr);
390
391                 cairo_new_path (cr);
392                 ArdourCanvas::set_source_rgba (cr, text_color);
393                 const double text_ypos = (get_height() - _text_height) * .5;
394
395                 if (_elements & Menu) {
396                         // always left align (dropdown)
397                         cairo_move_to (cr, text_margin, text_ypos);
398                         pango_cairo_show_layout (cr, _layout->gobj());
399                 } else if ( (_elements & Indicator)  == Indicator) {
400                         // left/right align depending on LED position
401                         if (_led_left) {
402                                 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
403                         } else {
404                                 cairo_move_to (cr, text_margin, text_ypos);
405                         }
406                         pango_cairo_show_layout (cr, _layout->gobj());
407                 } else {
408                         /* centered text otherwise */
409                         double ww, wh;
410                         double xa, ya;
411                         ww = get_width();
412                         wh = get_height();
413
414                         cairo_save (cr);
415                         cairo_rotate(cr, _angle * M_PI / 180.0);
416                         cairo_device_to_user(cr, &ww, &wh);
417                         xa = (ww - _text_width) * _xalign;
418                         ya = (wh - _text_height) * _yalign;
419
420                         /* quick hack for left/bottom alignment at -90deg
421                          * TODO this should be generalized incl rotation.
422                          * currently only 'user' of this API is meter_strip.cc
423                          */
424                         if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
425
426                         cairo_move_to (cr, xa, ya);
427                         pango_cairo_update_layout(cr, _layout->gobj());
428                         pango_cairo_show_layout (cr, _layout->gobj());
429                         cairo_restore (cr);
430                 }
431                 cairo_restore (cr);
432         }
433
434         //Menu "triangle"
435         if (_elements & Menu) {
436                 const float trih = ceil(_diameter * .5);
437                 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
438                 //menu arrow
439                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
440                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
441                 cairo_rel_line_to(cr, -triw2, -trih);
442                 cairo_rel_line_to(cr, 2. * triw2, 0);
443                 cairo_close_path(cr);
444
445                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
446                 cairo_fill(cr);
447
448                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
449                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
450                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
451                 cairo_close_path(cr);
452                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
453                 cairo_set_line_width(cr, 1);
454                 cairo_stroke(cr);
455         }
456
457         //Indicator LED
458         if (_elements & Indicator) {
459                 cairo_save (cr);
460
461                 /* move to the center of the indicator/led */
462                 if (_elements & Text) {
463                         int led_xoff = ceil(char_pixel_width() + _diameter * .5);
464                         if (_led_left) {
465                                 cairo_translate (cr, led_xoff, get_height() * .5);
466                         } else {
467                                 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
468                         }
469                 } else {
470                         cairo_translate (cr, get_width() * .5, get_height() * .5);
471                 }
472
473                 //inset
474                 if (!_flat_buttons) {
475                         cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
476                         cairo_set_source (cr, led_inset_pattern);
477                         cairo_fill (cr);
478                 }
479
480                 //black ring
481                 cairo_set_source_rgb (cr, 0, 0, 0);
482                 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
483                 cairo_fill(cr);
484
485                 //led color
486                 ArdourCanvas::set_source_rgba (cr, led_color);
487                 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
488                 cairo_fill(cr);
489
490                 cairo_restore (cr);
491         }
492
493         // a transparent gray layer to indicate insensitivity
494         if ((visual_state() & Gtkmm2ext::Insensitive)) {
495                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
496                 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
497                 cairo_fill (cr);
498         }
499
500         // if requested, show hovering
501         if (ARDOUR::Config->get_widget_prelight()
502                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
503                 if (_hovering) {
504                         rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
505                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
506                         cairo_fill (cr);
507                 }
508         }
509
510         //user is currently pressing the button. dark outline helps to indicate this
511         if (_grabbed && !(_elements & (Inactive|Menu))) {
512                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
513                 cairo_set_line_width(cr, 2);
514                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
515                 cairo_stroke (cr);
516         }
517
518         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
519         if (visual_state() & Gtkmm2ext::Selected) {
520                 cairo_set_line_width(cr, 1);
521                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
522                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
523                 cairo_stroke (cr);
524         }
525
526         //I guess this means we have keyboard focus.  I don't think this works currently
527         //
528         //A: yes, it's keyboard focus and it does work when there's no editor window
529         //   (the editor is always the first receiver for KeyDown).
530         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
531         if (_focused) {
532                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
533                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
534                 double dashes = 1;
535                 cairo_set_dash (cr, &dashes, 1, 0);
536                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
537                 cairo_set_line_width (cr, 1.0);
538                 cairo_stroke (cr);
539                 cairo_set_dash (cr, 0, 0, 0);
540         }
541 }
542
543 void
544 ArdourButton::set_corner_radius (float r)
545 {
546         _corner_radius = r;
547         CairoWidget::set_dirty ();
548 }
549
550 void
551 ArdourButton::on_realize()
552 {
553         CairoWidget::on_realize ();
554         ensure_layout ();
555         if (_layout && _layout->get_text() != _text) {
556                 _layout->set_text (_text);
557                 queue_resize ();
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.75 * 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(3.5 * char_pixel_width(), ceil(char_pixel_height() * BASELINESTRETCH + 1));
605                 req->width += wh;
606                 req->height = std::max(req->height, 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         CairoWidget::set_dirty ();
690 }
691
692 void
693 ArdourButton::build_patterns ()
694 {
695         if (convex_pattern) {
696                 cairo_pattern_destroy (convex_pattern);
697                 convex_pattern = 0;
698         }
699
700         if (concave_pattern) {
701                 cairo_pattern_destroy (concave_pattern);
702                 concave_pattern = 0;
703         }
704
705         if (led_inset_pattern) {
706                 cairo_pattern_destroy (led_inset_pattern);
707                 led_inset_pattern = 0;
708         }
709
710         //convex gradient
711         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
712         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
713         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
714
715         //concave gradient
716         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
717         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
718         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
719
720         led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
721         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
722         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
723
724         _pattern_height = get_height() ;
725 }
726
727 void
728 ArdourButton::set_led_left (bool yn)
729 {
730         _led_left = yn;
731 }
732
733 bool
734 ArdourButton::on_button_press_event (GdkEventButton *ev)
735 {
736         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
737                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
738                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
739                         return true;
740                 }
741         }
742
743         if (binding_proxy.button_press_handler (ev)) {
744                 return true;
745         }
746
747         _grabbed = true;
748         CairoWidget::set_dirty ();
749
750         if (!_act_on_release) {
751                 if (_action) {
752                         _action->activate ();
753                         return true;
754                 }
755         }
756
757         if (_fallthrough_to_parent)
758                 return false;
759
760         return true;
761 }
762
763 bool
764 ArdourButton::on_button_release_event (GdkEventButton *ev)
765 {
766         if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
767                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
768                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
769                         signal_led_clicked(); /* EMIT SIGNAL */
770                         return true;
771                 }
772         }
773
774         _grabbed = false;
775         CairoWidget::set_dirty ();
776
777         if (_hovering) {
778                 signal_clicked ();
779                 if (_act_on_release) {
780                         if (_action) {
781                                 _action->activate ();
782                                 return true;
783                         }
784                 }
785         }
786
787         if (_fallthrough_to_parent)
788                 return false;
789
790         return true;
791 }
792
793 void
794 ArdourButton::set_distinct_led_click (bool yn)
795 {
796         _distinct_led_click = yn;
797         setup_led_rect ();
798 }
799
800 void
801 ArdourButton::color_handler ()
802 {
803         _update_colors = true;
804         CairoWidget::set_dirty ();
805 }
806
807 void
808 ArdourButton::on_size_allocate (Allocation& alloc)
809 {
810         CairoWidget::on_size_allocate (alloc);
811         setup_led_rect ();
812 }
813
814 void
815 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
816 {
817         watch_connection.disconnect ();
818         binding_proxy.set_controllable (c);
819 }
820
821 void
822 ArdourButton::watch ()
823 {
824         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
825
826         if (!c) {
827                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
828                 return;
829         }
830         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
831 }
832
833 void
834 ArdourButton::controllable_changed ()
835 {
836         float val = binding_proxy.get_controllable()->get_value();
837
838         if (fabs (val) >= 0.5f) {
839                 set_active_state (Gtkmm2ext::ExplicitActive);
840         } else {
841                 unset_active_state ();
842         }
843         CairoWidget::set_dirty ();
844 }
845
846 void
847 ArdourButton::set_related_action (RefPtr<Action> act)
848 {
849         Gtkmm2ext::Activatable::set_related_action (act);
850
851         if (_action) {
852
853                 action_tooltip_changed ();
854
855                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
856                 if (tact) {
857                         action_toggled ();
858                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
859                 }
860
861                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
862                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
863                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
864         }
865 }
866
867 void
868 ArdourButton::action_toggled ()
869 {
870         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
871
872         if (tact) {
873                 if (tact->get_active()) {
874                         set_active_state (Gtkmm2ext::ExplicitActive);
875                 } else {
876                         unset_active_state ();
877                 }
878         }
879 }
880
881 void
882 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
883 {
884         _update_colors = true;
885         CairoWidget::set_dirty ();
886 }
887
888 void
889 ArdourButton::on_name_changed ()
890 {
891         _char_pixel_width = 0;
892         _char_pixel_height = 0;
893         _diameter = 0;
894         _update_colors = true;
895         if (is_realized()) {
896                 queue_resize ();
897         }
898 }
899
900 void
901 ArdourButton::setup_led_rect ()
902 {
903         if (!(_elements & Indicator)) {
904                 delete _led_rect;
905                 _led_rect = 0;
906                 return;
907         }
908
909         _led_rect = new cairo_rectangle_t;
910
911         if (_elements & Text) {
912                 if (_led_left) {
913                         _led_rect->x = char_pixel_width();
914                 } else {
915                         _led_rect->x = get_width() - char_pixel_width() + _diameter;
916                 }
917         } else {
918                 /* centered */
919                 _led_rect->x = .5 * get_width() - _diameter;
920         }
921
922         _led_rect->y = .5 * (get_height() - _diameter);
923         _led_rect->width = _diameter;
924         _led_rect->height = _diameter;
925 }
926
927 void
928 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
929 {
930         _pixbuf = img;
931         if (is_realized()) {
932                 queue_resize ();
933         }
934 }
935
936 void
937 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
938 {
939         bool changed = (_active_state != s);
940         CairoWidget::set_active_state (s);
941         if (changed) {
942                 _update_colors = true;
943                 CairoWidget::set_dirty ();
944         }
945 }
946
947 void
948 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
949 {
950         bool changed = (_visual_state != s);
951         CairoWidget::set_visual_state (s);
952         if (changed) {
953                 _update_colors = true;
954                 CairoWidget::set_dirty ();
955         }
956 }
957
958 bool
959 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
960 {
961         _focused = true;
962         CairoWidget::set_dirty ();
963         return CairoWidget::on_focus_in_event (ev);
964 }
965
966 bool
967 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
968 {
969         _focused = false;
970         CairoWidget::set_dirty ();
971         return CairoWidget::on_focus_out_event (ev);
972 }
973
974 bool
975 ArdourButton::on_key_release_event (GdkEventKey *ev) {
976         if (_focused &&
977                         (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
978         {
979                 signal_clicked();
980                 if (_action) {
981                         _action->activate ();
982                 }
983                 return true;
984         }
985         return CairoWidget::on_key_release_event (ev);
986 }
987
988 bool
989 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
990 {
991         _hovering = (_elements & Inactive) ? false : true;
992
993         if (ARDOUR::Config->get_widget_prelight()) {
994                 CairoWidget::set_dirty ();
995         }
996
997         return CairoWidget::on_enter_notify_event (ev);
998 }
999
1000 bool
1001 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1002 {
1003         _hovering = false;
1004
1005         if (ARDOUR::Config->get_widget_prelight()) {
1006                 CairoWidget::set_dirty ();
1007         }
1008
1009         return CairoWidget::on_leave_notify_event (ev);
1010 }
1011
1012 void
1013 ArdourButton::set_tweaks (Tweaks t)
1014 {
1015         if (_tweaks != t) {
1016                 _tweaks = t;
1017                 if (is_realized()) {
1018                         queue_resize ();
1019                 }
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 (_layout_ellipsize_width > 0) {
1060                 _layout->set_width (_layout_ellipsize_width);
1061         }
1062         if (is_realized ()) {
1063                 queue_resize ();
1064         }
1065 }
1066
1067 void
1068 ArdourButton::ensure_layout ()
1069 {
1070         if (!_layout) {
1071                 ensure_style ();
1072                 _layout = Pango::Layout::create (get_pango_context());
1073                 _layout->set_ellipsize(_ellipsis);
1074                 if (_layout_ellipsize_width > 0) {
1075                         _layout->set_width (_layout_ellipsize_width);
1076                 }
1077         }
1078 }
1079
1080 void
1081 ArdourButton::recalc_char_pixel_geometry ()
1082 {
1083         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1084                 return;
1085         }
1086         ensure_layout();
1087         // NB. this is not static, since the geometry is different
1088         // depending on the font used.
1089         int w, h;
1090         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1091         _layout->set_text (x);
1092         _layout->get_pixel_size (w, h);
1093         _char_pixel_height = std::max(4, h);
1094         // number of actual chars in the string (not bytes)
1095         // Glib to the rescue.
1096         Glib::ustring gx(x);
1097         _char_pixel_width = std::max(4, w / (int)gx.size());
1098         _layout->set_text (_text);
1099 }
1100
1101 void
1102 ArdourButton::action_visibility_changed ()
1103 {
1104         if (_action->property_visible ()) {
1105                 show ();
1106         } else {
1107                 hide ();
1108         }
1109 }
1110
1111 void
1112 ArdourButton::action_tooltip_changed ()
1113 {
1114         string str = _action->property_tooltip().get_value();
1115         ARDOUR_UI::instance()->set_tip (*this, str);
1116 }
1117
1118 void
1119 ArdourButton::set_elements (Element e)
1120 {
1121         _elements = e;
1122         CairoWidget::set_dirty ();
1123 }
1124
1125 void
1126 ArdourButton::add_elements (Element e)
1127 {
1128         _elements = (ArdourButton::Element) (_elements | e);
1129         CairoWidget::set_dirty ();
1130 }