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