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