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