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