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