extend ArdourButton API to allow independently setting fixed active/inactive colors
[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 "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) {
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) && !_text.empty()) {
545                 // if _layout does not exist, char_pixel_height() creates it,
546                 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
547                 assert (_layout);
548                 _layout->get_pixel_size (_text_width, _text_height);
549
550                 req->width += rint(1.75 * char_pixel_width()); // padding
551                 req->width += _text_width;
552
553                 /* XXX hack (surprise). Deal with two common rotation angles */
554
555                 if (_angle == 90 || _angle == 270) {
556                         /* do not swap text width or height because we rely on
557                            these being the un-rotated values in ::render()
558                         */
559                         swap (req->width, req->height);
560                 }
561
562         } else {
563                 _text_width = 0;
564                 _text_height = 0;
565         }
566
567         if (_pixbuf) {
568                 req->width += _pixbuf->get_width() + char_pixel_width();
569                 req->height = std::max(req->height, _pixbuf->get_height() + 4);
570         }
571
572         if (_elements & Indicator) {
573                 req->width += lrint (_diameter) + char_pixel_width();
574                 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
575         }
576
577         if ((_elements & Menu)) {
578                 req->width += _diameter + 4;
579         }
580
581         if (_elements & VectorIcon) {
582                 assert(!(_elements & Text));
583                 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
584                 req->width += wh;
585                 req->height = std::max(req->height, wh);
586         }
587
588         /* Tweaks to mess the nice stuff above up again. */
589         if (_tweaks & TrackHeader) {
590                 // forget everything above and just use a fixed square [em] size
591                 // "TrackHeader Buttons" are single letter (usually uppercase)
592                 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
593                 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
594                 req->width  = wh;
595                 req->height = wh;
596         }
597         else if (_tweaks & Square) {
598                 // currerntly unused (again)
599                 if (req->width < req->height)
600                         req->width = req->height;
601                 if (req->height < req->width)
602                         req->height = req->width;
603         } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
604                 // properly centered text for those elements that are centered
605                 // (no sub-pixel offset)
606                 if ((req->width - _text_width) & 1) { ++req->width; }
607                 if ((req->height - _text_height) & 1) { ++req->height; }
608         }
609 #if 0
610                 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
611 #endif
612 }
613
614 /**
615  * This sets the colors used for rendering based on the name of the button, and
616  * thus uses information from the GUI config data.
617  */
618 void
619 ArdourButton::set_colors ()
620 {
621         _update_colors = false;
622
623         if (_fixed_colors_set == 0x3) {
624                 return;
625         }
626
627         std::string name = get_name();
628         bool failed = false;
629
630         if (!(_fixed_colors_set & 0x1)) {
631                 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
632                 if (failed) {
633                         fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
634                 }
635         }
636
637         if (!(_fixed_colors_set & 0x2)) {
638                 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
639                 if (failed) {
640                         fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
641                 }
642         }
643
644         text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
645         text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
646
647         led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
648         if (failed) {
649                 led_active_color = UIConfiguration::instance().color ("generic button: led active");
650         }
651
652         /* The inactive color for the LED is just a fairly dark version of the
653          * active color.
654          */
655
656         ArdourCanvas::HSV inactive (led_active_color);
657         inactive.v = 0.35;
658
659         led_inactive_color = inactive.color ();
660 }
661
662 /**
663  * This sets the colors used for rendering based on two fixed values, rather
664  * than basing them on the button name, and thus information in the GUI config
665  * data.
666  */
667 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
668 {
669         set_active_color (color_active);
670         set_inactive_color (color_inactive);
671 }
672
673 void ArdourButton::set_active_color (const uint32_t color)
674 {
675         _fixed_colors_set |= 0x1;
676
677         fill_active_color = color;
678
679         unsigned char r, g, b, a;
680         UINT_TO_RGBA(color, &r, &g, &b, &a);
681
682         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
683                 (max (double(g), 255.) - min (double(g), 255.)) +
684                 (max (double(b), 255.) - min (double(b), 255.));
685
686         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
687                 (max (double(g), 0.) - min (double(g), 0.)) +
688                 (max (double(b), 0.) - min (double(b), 0.));
689
690         text_active_color = (white_contrast > black_contrast) ?
691                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
692                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
693
694         /* XXX what about led colors ? */
695         CairoWidget::set_dirty ();
696 }
697
698 void ArdourButton::set_inactive_color (const uint32_t color)
699 {
700         _fixed_colors_set |= 0x2;
701
702         fill_inactive_color = color;
703
704         unsigned char r, g, b, a;
705         UINT_TO_RGBA(color, &r, &g, &b, &a);
706
707         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
708                 (max (double(g), 255.) - min (double(g), 255.)) +
709                 (max (double(b), 255.) - min (double(b), 255.));
710
711         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
712                 (max (double(g), 0.) - min (double(g), 0.)) +
713                 (max (double(b), 0.) - min (double(b), 0.));
714
715         text_inactive_color = (white_contrast > black_contrast) ?
716                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
717                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
718
719         /* XXX what about led colors ? */
720         CairoWidget::set_dirty ();
721 }
722
723 void
724 ArdourButton::build_patterns ()
725 {
726         if (convex_pattern) {
727                 cairo_pattern_destroy (convex_pattern);
728                 convex_pattern = 0;
729         }
730
731         if (concave_pattern) {
732                 cairo_pattern_destroy (concave_pattern);
733                 concave_pattern = 0;
734         }
735
736         if (led_inset_pattern) {
737                 cairo_pattern_destroy (led_inset_pattern);
738                 led_inset_pattern = 0;
739         }
740
741         //convex gradient
742         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
743         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
744         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
745
746         //concave gradient
747         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
748         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
749         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
750
751         led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
752         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
753         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
754
755         _pattern_height = get_height() ;
756 }
757
758 void
759 ArdourButton::set_led_left (bool yn)
760 {
761         _led_left = yn;
762 }
763
764 bool
765 ArdourButton::on_button_press_event (GdkEventButton *ev)
766 {
767         focus_handler (this);
768
769         if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
770                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
771                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
772                         return true;
773                 }
774         }
775
776         if (binding_proxy.button_press_handler (ev)) {
777                 return true;
778         }
779
780         _grabbed = true;
781         CairoWidget::set_dirty ();
782
783         if (ev->button == 1 && !_act_on_release) {
784                 if (_action) {
785                         _action->activate ();
786                         return true;
787                 }
788         }
789
790         if (_fallthrough_to_parent)
791                 return false;
792
793         return true;
794 }
795
796 bool
797 ArdourButton::on_button_release_event (GdkEventButton *ev)
798 {
799         if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
800                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
801                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
802                         signal_led_clicked(ev); /* EMIT SIGNAL */
803                         return true;
804                 }
805         }
806
807         _grabbed = false;
808         CairoWidget::set_dirty ();
809
810         if (ev->button == 1 && _hovering) {
811                 signal_clicked ();
812                 if (_act_on_release) {
813                         if (_action) {
814                                 _action->activate ();
815                                 return true;
816                         }
817                 }
818         }
819
820         if (_fallthrough_to_parent)
821                 return false;
822
823         return true;
824 }
825
826 void
827 ArdourButton::set_distinct_led_click (bool yn)
828 {
829         _distinct_led_click = yn;
830         setup_led_rect ();
831 }
832
833 void
834 ArdourButton::color_handler ()
835 {
836         _update_colors = true;
837         CairoWidget::set_dirty ();
838 }
839
840 void
841 ArdourButton::on_size_allocate (Allocation& alloc)
842 {
843         CairoWidget::on_size_allocate (alloc);
844         setup_led_rect ();
845 }
846
847 void
848 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
849 {
850         watch_connection.disconnect ();
851         binding_proxy.set_controllable (c);
852 }
853
854 void
855 ArdourButton::watch ()
856 {
857         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
858
859         if (!c) {
860                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
861                 return;
862         }
863         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
864 }
865
866 void
867 ArdourButton::controllable_changed ()
868 {
869         float val = binding_proxy.get_controllable()->get_value();
870
871         if (fabs (val) >= 0.5f) {
872                 set_active_state (Gtkmm2ext::ExplicitActive);
873         } else {
874                 unset_active_state ();
875         }
876         CairoWidget::set_dirty ();
877 }
878
879 void
880 ArdourButton::set_related_action (RefPtr<Action> act)
881 {
882         Gtkmm2ext::Activatable::set_related_action (act);
883
884         if (_action) {
885
886                 action_tooltip_changed ();
887
888                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
889                 if (tact) {
890                         action_toggled ();
891                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
892                 }
893
894                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
895                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
896                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
897         }
898 }
899
900 void
901 ArdourButton::action_toggled ()
902 {
903         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
904
905         if (tact) {
906                 if (tact->get_active()) {
907                         set_active_state (Gtkmm2ext::ExplicitActive);
908                 } else {
909                         unset_active_state ();
910                 }
911         }
912 }
913
914 void
915 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
916 {
917         _update_colors = true;
918         CairoWidget::set_dirty ();
919 }
920
921 void
922 ArdourButton::on_name_changed ()
923 {
924         _char_pixel_width = 0;
925         _char_pixel_height = 0;
926         _diameter = 0;
927         _update_colors = true;
928         if (is_realized()) {
929                 queue_resize ();
930         }
931 }
932
933 void
934 ArdourButton::setup_led_rect ()
935 {
936         if (!(_elements & Indicator)) {
937                 delete _led_rect;
938                 _led_rect = 0;
939                 return;
940         }
941
942         if (!_led_rect) {
943                 _led_rect = new cairo_rectangle_t;
944         }
945
946         if (_elements & Text) {
947                 if (_led_left) {
948                         _led_rect->x = char_pixel_width();
949                 } else {
950                         _led_rect->x = get_width() - char_pixel_width() + _diameter;
951                 }
952         } else {
953                 /* centered */
954                 _led_rect->x = .5 * get_width() - _diameter;
955         }
956
957         _led_rect->y = .5 * (get_height() - _diameter);
958         _led_rect->width = _diameter;
959         _led_rect->height = _diameter;
960 }
961
962 void
963 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
964 {
965         _pixbuf = img;
966         if (is_realized()) {
967                 queue_resize ();
968         }
969 }
970
971 void
972 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
973 {
974         bool changed = (_active_state != s);
975         CairoWidget::set_active_state (s);
976         if (changed) {
977                 _update_colors = true;
978                 CairoWidget::set_dirty ();
979         }
980 }
981
982 void
983 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
984 {
985         bool changed = (_visual_state != s);
986         CairoWidget::set_visual_state (s);
987         if (changed) {
988                 _update_colors = true;
989                 CairoWidget::set_dirty ();
990         }
991 }
992
993 bool
994 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
995 {
996         _focused = true;
997         CairoWidget::set_dirty ();
998         return CairoWidget::on_focus_in_event (ev);
999 }
1000
1001 bool
1002 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1003 {
1004         _focused = false;
1005         CairoWidget::set_dirty ();
1006         return CairoWidget::on_focus_out_event (ev);
1007 }
1008
1009 bool
1010 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1011         if (_focused &&
1012                         (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1013         {
1014                 signal_clicked();
1015                 if (_action) {
1016                         _action->activate ();
1017                 }
1018                 return true;
1019         }
1020         return CairoWidget::on_key_release_event (ev);
1021 }
1022
1023 bool
1024 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1025 {
1026         _hovering = (_elements & Inactive) ? false : true;
1027
1028         if (UIConfiguration::instance().get_widget_prelight()) {
1029                 CairoWidget::set_dirty ();
1030         }
1031
1032         return CairoWidget::on_enter_notify_event (ev);
1033 }
1034
1035 bool
1036 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1037 {
1038         _hovering = false;
1039
1040         if (UIConfiguration::instance().get_widget_prelight()) {
1041                 CairoWidget::set_dirty ();
1042         }
1043
1044         return CairoWidget::on_leave_notify_event (ev);
1045 }
1046
1047 void
1048 ArdourButton::set_tweaks (Tweaks t)
1049 {
1050         if (_tweaks != t) {
1051                 _tweaks = t;
1052                 if (is_realized()) {
1053                         queue_resize ();
1054                 }
1055         }
1056 }
1057
1058 void
1059 ArdourButton::action_sensitivity_changed ()
1060 {
1061         if (_action->property_sensitive ()) {
1062                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1063         } else {
1064                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1065         }
1066 }
1067
1068 void
1069 ArdourButton::set_layout_ellipsize_width (int w)
1070 {
1071         if (_layout_ellipsize_width == w) {
1072                 return;
1073         }
1074         _layout_ellipsize_width = w;
1075         if (!_layout) {
1076                 return;
1077         }
1078         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1079                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1080         }
1081         if (is_realized ()) {
1082                 queue_resize ();
1083         }
1084 }
1085
1086 void
1087 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1088 {
1089         if (_ellipsis == e) {
1090                 return;
1091         }
1092         _ellipsis = e;
1093         if (!_layout) {
1094                 return;
1095         }
1096         _layout->set_ellipsize(_ellipsis);
1097         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1098                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1099         }
1100         if (is_realized ()) {
1101                 queue_resize ();
1102         }
1103 }
1104
1105 void
1106 ArdourButton::ensure_layout ()
1107 {
1108         if (!_layout) {
1109                 ensure_style ();
1110                 _layout = Pango::Layout::create (get_pango_context());
1111                 _layout->set_ellipsize(_ellipsis);
1112                 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1113                         _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1114                 }
1115         }
1116 }
1117
1118 void
1119 ArdourButton::recalc_char_pixel_geometry ()
1120 {
1121         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1122                 return;
1123         }
1124         ensure_layout();
1125         // NB. this is not static, since the geometry is different
1126         // depending on the font used.
1127         int w, h;
1128         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1129         _layout->set_text (x);
1130         _layout->get_pixel_size (w, h);
1131         _char_pixel_height = std::max(4, h);
1132         // number of actual chars in the string (not bytes)
1133         // Glib to the rescue.
1134         Glib::ustring gx(x);
1135         _char_avg_pixel_width = w / (float)gx.size();
1136         _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1137         _layout->set_text (_text);
1138 }
1139
1140 void
1141 ArdourButton::action_visibility_changed ()
1142 {
1143         if (_action->property_visible ()) {
1144                 show ();
1145         } else {
1146                 hide ();
1147         }
1148 }
1149
1150 void
1151 ArdourButton::action_tooltip_changed ()
1152 {
1153         string str = _action->property_tooltip().get_value();
1154         set_tooltip (*this, str);
1155 }
1156
1157 void
1158 ArdourButton::set_elements (Element e)
1159 {
1160         _elements = e;
1161         CairoWidget::set_dirty ();
1162 }
1163
1164 void
1165 ArdourButton::add_elements (Element e)
1166 {
1167         _elements = (ArdourButton::Element) (_elements | e);
1168         CairoWidget::set_dirty ();
1169 }
1170
1171 void
1172 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1173 {
1174         _icon = i;
1175         _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1176         CairoWidget::set_dirty ();
1177 }
1178
1179 void
1180 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1181 {
1182         if (led_custom_color == c && use_custom_led_color == useit) {
1183                 return;
1184         }
1185
1186         led_custom_color = c;
1187         use_custom_led_color = useit;
1188         CairoWidget::set_dirty ();
1189 }