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