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