renice 5-pin DIN/MIDI button
[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         else if (_elements & StripWidth) {
393                 const double x0 = get_width()  * .2;
394                 const double x1 = get_width()  * .8;
395
396                 const double y0 = get_height() * .25;
397                 const double y1= get_height()  * .75;
398
399                 const double ym= get_height()  * .5;
400
401                 // arrow
402                 const double xa0= get_height()  * .39;
403                 const double xa1= get_height()  * .61;
404                 const double ya0= get_height()  * .35;
405                 const double ya1= get_height()  * .65;
406
407                 ArdourCanvas::set_source_rgba (cr, text_color);
408                 cairo_set_line_width(cr, 1);
409
410                 // left + right
411                 cairo_move_to(cr, x0, y0);
412                 cairo_line_to(cr, x0, y1);
413                 cairo_move_to(cr, x1, y0);
414                 cairo_line_to(cr, x1, y1);
415
416                 // horiz center line
417                 cairo_move_to(cr, x0, ym);
418                 cairo_line_to(cr, x1, ym);
419
420                 // arrow left
421                 cairo_move_to(cr,  x0, ym);
422                 cairo_line_to(cr, xa0, ya0);
423                 cairo_move_to(cr,  x0, ym);
424                 cairo_line_to(cr, xa0, ya1);
425
426                 // arrow right
427                 cairo_move_to(cr,  x1,  ym);
428                 cairo_line_to(cr, xa1, ya0);
429                 cairo_move_to(cr,  x1,  ym);
430                 cairo_line_to(cr, xa1, ya1);
431                 cairo_stroke(cr);
432         }
433         else if (_elements & DinMidi) {
434                 const double x = get_width() * .5;
435                 const double y = get_height() * .5;
436                 const double r = std::min(x, y) * .75;
437                 ArdourCanvas::set_source_rgba (cr, text_color);
438                 cairo_set_line_width(cr, 1);
439                 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
440                 cairo_stroke(cr);
441
442                 // pins equally spaced 45deg
443                 cairo_arc (cr, x, y * 0.5, r * .15, 0, 2 * M_PI);
444                 cairo_fill(cr);
445                 cairo_arc (cr, x * 0.5, y, r * .15, 0, 2 * M_PI);
446                 cairo_fill(cr);
447                 cairo_arc (cr, x * 1.5, y, r * .15, 0, 2 * M_PI);
448                 cairo_fill(cr);
449                 //  .5 + .5 * .5 * sin(45deg),  1.5 - .5 * .5 * cos(45deg)
450                 cairo_arc (cr, x * 0.677, y * .677, r * .15, 0, 2 * M_PI);
451                 cairo_fill(cr);
452                 cairo_arc (cr, x * 1.323, y * .677, r * .15, 0, 2 * M_PI);
453                 cairo_fill(cr);
454
455                 // bottom notch
456                 cairo_arc (cr, x, y+r, r * .28, 1.05 * M_PI, 1.95 * M_PI);
457                 cairo_stroke(cr);
458         }
459
460         const int text_margin = char_pixel_width();
461         // Text, if any
462         if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
463                 assert(_layout);
464 #if 0 // DEBUG style (print on hover)
465                 if (_hovering || (_elements & Inactive)) {
466                         bool layout_font = true;
467                         Pango::FontDescription fd = _layout->get_font_description();
468                         if (fd.gobj() == NULL) {
469                                 layout_font = false;
470                                 fd = get_pango_context()->get_font_description();
471                         }
472                         printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
473                                         get_name().c_str(),
474                                         char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
475                                         ceil(char_pixel_height() * BASELINESTRETCH),
476                                         _text_width, _text_height,
477                                         get_width(), get_height(),
478                                         layout_font ? "L:" : "W:",
479                                         fd.to_string().c_str());
480                 }
481 #endif
482
483                 cairo_save (cr);
484                 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
485                 cairo_clip(cr);
486
487                 cairo_new_path (cr);
488                 ArdourCanvas::set_source_rgba (cr, text_color);
489                 const double text_ypos = (get_height() - _text_height) * .5;
490
491                 if (_elements & Menu) {
492                         // always left align (dropdown)
493                         cairo_move_to (cr, text_margin, text_ypos);
494                         pango_cairo_show_layout (cr, _layout->gobj());
495                 } else if ( (_elements & Indicator)  == Indicator) {
496                         // left/right align depending on LED position
497                         if (_led_left) {
498                                 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
499                         } else {
500                                 cairo_move_to (cr, text_margin, text_ypos);
501                         }
502                         pango_cairo_show_layout (cr, _layout->gobj());
503                 } else {
504                         /* centered text otherwise */
505                         double ww, wh;
506                         double xa, ya;
507                         ww = get_width();
508                         wh = get_height();
509
510                         cairo_save (cr);
511                         cairo_rotate(cr, _angle * M_PI / 180.0);
512                         cairo_device_to_user(cr, &ww, &wh);
513                         xa = (ww - _text_width) * _xalign;
514                         ya = (wh - _text_height) * _yalign;
515
516                         /* quick hack for left/bottom alignment at -90deg
517                          * TODO this should be generalized incl rotation.
518                          * currently only 'user' of this API is meter_strip.cc
519                          */
520                         if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
521
522                         cairo_move_to (cr, xa, ya);
523                         pango_cairo_update_layout(cr, _layout->gobj());
524                         pango_cairo_show_layout (cr, _layout->gobj());
525                         cairo_restore (cr);
526                 }
527                 cairo_restore (cr);
528         }
529
530         //Menu "triangle"
531         if (_elements & Menu) {
532                 const float trih = ceil(_diameter * .5);
533                 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
534                 //menu arrow
535                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
536                 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
537                 cairo_rel_line_to(cr, -triw2, -trih);
538                 cairo_rel_line_to(cr, 2. * triw2, 0);
539                 cairo_close_path(cr);
540
541                 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
542                 cairo_fill(cr);
543
544                 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
545                 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
546                 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
547                 cairo_close_path(cr);
548                 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
549                 cairo_set_line_width(cr, 1);
550                 cairo_stroke(cr);
551         }
552
553         //Indicator LED
554         if (_elements & Indicator) {
555                 cairo_save (cr);
556
557                 /* move to the center of the indicator/led */
558                 if (_elements & Text) {
559                         int led_xoff = ceil(char_pixel_width() + _diameter * .5);
560                         if (_led_left) {
561                                 cairo_translate (cr, led_xoff, get_height() * .5);
562                         } else {
563                                 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
564                         }
565                 } else {
566                         cairo_translate (cr, get_width() * .5, get_height() * .5);
567                 }
568
569                 //inset
570                 if (!_flat_buttons) {
571                         cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
572                         cairo_set_source (cr, led_inset_pattern);
573                         cairo_fill (cr);
574                 }
575
576                 //black ring
577                 cairo_set_source_rgb (cr, 0, 0, 0);
578                 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
579                 cairo_fill(cr);
580
581                 //led color
582                 ArdourCanvas::set_source_rgba (cr, led_color);
583                 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
584                 cairo_fill(cr);
585
586                 cairo_restore (cr);
587         }
588
589         // a transparent overlay to indicate insensitivity
590         if ((visual_state() & Gtkmm2ext::Insensitive)) {
591                 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
592                 uint32_t ins_color = ARDOUR_UI::config()->color ("gtk_background");
593                 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
594                 cairo_fill (cr);
595         }
596
597         // if requested, show hovering
598         if (ARDOUR_UI::config()->get_widget_prelight()
599                         && !((visual_state() & Gtkmm2ext::Insensitive))) {
600                 if (_hovering) {
601                         rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
602                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
603                         cairo_fill (cr);
604                 }
605         }
606
607         //user is currently pressing the button. dark outline helps to indicate this
608         if (_grabbed && !(_elements & (Inactive|Menu))) {
609                 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
610                 cairo_set_line_width(cr, 2);
611                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
612                 cairo_stroke (cr);
613         }
614
615         //some buttons (like processor boxes) can be selected  (so they can be deleted).  Draw a selection indicator
616         if (visual_state() & Gtkmm2ext::Selected) {
617                 cairo_set_line_width(cr, 1);
618                 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
619                 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
620                 cairo_stroke (cr);
621         }
622
623         //I guess this means we have keyboard focus.  I don't think this works currently
624         //
625         //A: yes, it's keyboard focus and it does work when there's no editor window
626         //   (the editor is always the first receiver for KeyDown).
627         //   It's needed for eg. the engine-dialog at startup or after closing a sesion.
628         if (_focused) {
629                 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
630                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
631                 double dashes = 1;
632                 cairo_set_dash (cr, &dashes, 1, 0);
633                 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
634                 cairo_set_line_width (cr, 1.0);
635                 cairo_stroke (cr);
636                 cairo_set_dash (cr, 0, 0, 0);
637         }
638 }
639
640 void
641 ArdourButton::set_corner_radius (float r)
642 {
643         _corner_radius = r;
644         CairoWidget::set_dirty ();
645 }
646
647 void
648 ArdourButton::on_realize()
649 {
650         CairoWidget::on_realize ();
651         ensure_layout ();
652         if (_layout && _layout->get_text() != _text) {
653                 _layout->set_text (_text);
654                 queue_resize ();
655         }
656 }
657
658 void
659 ArdourButton::on_size_request (Gtk::Requisition* req)
660 {
661         req->width = req->height = 0;
662         CairoWidget::on_size_request (req);
663
664         if (_diameter == 0) {
665                 const float newdia = rint (ARDOUR_UI::config()->get_font_scale () / 9600.0); // 11px with 100% font-scaling
666                 if (_diameter != newdia) {
667                         _pattern_height = 0;
668                         _diameter = newdia;
669                 }
670         }
671
672         if ((_elements & Text) && !_text.empty()) {
673                 // if _layout does not exist, char_pixel_height() creates it,
674                 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
675                 _layout->get_pixel_size (_text_width, _text_height);
676                 req->width += rint(1.75 * char_pixel_width()); // padding
677                 req->width += _text_width;
678         } else {
679                 _text_width = 0;
680                 _text_height = 0;
681         }
682
683         if (_pixbuf) {
684                 req->width += _pixbuf->get_width() + char_pixel_width();
685                 req->height = std::max(req->height, _pixbuf->get_height() + 4);
686         }
687
688         if (_elements & Indicator) {
689                 req->width += lrint (_diameter) + char_pixel_width();
690                 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
691         }
692
693         if ((_elements & Menu)) {
694                 req->width += _diameter + 4;
695         }
696
697         if (_elements & (RecButton | CloseCross | StripWidth | DinMidi)) {
698                 assert(!(_elements & Text));
699                 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
700                 req->width += wh;
701                 req->height = std::max(req->height, wh);
702         }
703
704         /* Tweaks to mess the nice stuff above up again. */
705         if (_tweaks & TrackHeader) {
706                 // forget everything above and just use a fixed square [em] size
707                 // "TrackHeader Buttons" are single letter (usually uppercase)
708                 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
709                 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
710                 req->width  = wh;
711                 req->height = wh;
712         }
713         else if (_tweaks & Square) {
714                 // currerntly unused (again)
715                 if (req->width < req->height)
716                         req->width = req->height;
717                 if (req->height < req->width)
718                         req->height = req->width;
719         } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
720                 // properly centered text for those elements that are centered
721                 // (no sub-pixel offset)
722                 if ((req->width - _text_width) & 1) { ++req->width; }
723                 if ((req->height - _text_height) & 1) { ++req->height; }
724         }
725 #if 0
726                 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
727 #endif
728 }
729
730 /**
731  * This sets the colors used for rendering based on the name of the button, and
732  * thus uses information from the GUI config data.
733  */
734 void
735 ArdourButton::set_colors ()
736 {
737         _update_colors = false;
738         if (_fixed_colors_set) {
739                 return;
740         }
741         std::string name = get_name();
742         bool failed = false;
743
744         fill_active_color = ARDOUR_UI::config()->color (string_compose ("%1: fill active", name), &failed);
745         if (failed) {
746                 fill_active_color = ARDOUR_UI::config()->color ("generic button: fill active");
747         }
748         fill_inactive_color = ARDOUR_UI::config()->color (string_compose ("%1: fill", name), &failed);
749         if (failed) {
750                 fill_inactive_color = ARDOUR_UI::config()->color ("generic button: fill");
751         }
752
753         text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
754         text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
755
756         led_active_color = ARDOUR_UI::config()->color (string_compose ("%1: led active", name), &failed);
757         if (failed) {
758                 led_active_color = ARDOUR_UI::config()->color ("generic button: led active");
759         }
760
761         /* The inactive color for the LED is just a fairly dark version of the
762          * active color.
763          */
764         
765         ArdourCanvas::HSV inactive (led_active_color);
766         inactive.v = 0.35;
767
768         led_inactive_color = inactive.color ();
769 }
770
771 /**
772  * This sets the colors used for rendering based on two fixed values, rather
773  * than basing them on the button name, and thus information in the GUI config
774  * data.
775  */
776 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
777 {
778         _fixed_colors_set = true;
779
780         fill_active_color = color_active;
781         fill_inactive_color = color_inactive;
782
783         unsigned char r, g, b, a;
784         UINT_TO_RGBA(color_active, &r, &g, &b, &a);
785
786         double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
787                 (max (double(g), 255.) - min (double(g), 255.)) +
788                 (max (double(b), 255.) - min (double(b), 255.));
789
790         double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
791                 (max (double(g), 0.) - min (double(g), 0.)) +
792                 (max (double(b), 0.) - min (double(b), 0.));
793
794         text_active_color = (white_contrast > black_contrast) ?
795                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
796                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
797
798
799         UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
800
801         white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
802                 (max (double(g), 255.) - min (double(g), 255.)) +
803                 (max (double(b), 255.) - min (double(b), 255.));
804
805         black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
806                 (max (double(g), 0.) - min (double(g), 0.)) +
807                 (max (double(b), 0.) - min (double(b), 0.));
808
809         text_inactive_color = (white_contrast > black_contrast) ?
810                 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
811                 RGBA_TO_UINT(  0,   0,   0,   255);  /* use black */
812
813         /* XXX what about led colors ? */
814         CairoWidget::set_dirty ();
815 }
816
817 void
818 ArdourButton::build_patterns ()
819 {
820         if (convex_pattern) {
821                 cairo_pattern_destroy (convex_pattern);
822                 convex_pattern = 0;
823         }
824
825         if (concave_pattern) {
826                 cairo_pattern_destroy (concave_pattern);
827                 concave_pattern = 0;
828         }
829
830         if (led_inset_pattern) {
831                 cairo_pattern_destroy (led_inset_pattern);
832                 led_inset_pattern = 0;
833         }
834
835         //convex gradient
836         convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
837         cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
838         cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
839
840         //concave gradient
841         concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0,  get_height());
842         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
843         cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
844
845         led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
846         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
847         cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
848
849         _pattern_height = get_height() ;
850 }
851
852 void
853 ArdourButton::set_led_left (bool yn)
854 {
855         _led_left = yn;
856 }
857
858 bool
859 ArdourButton::on_button_press_event (GdkEventButton *ev)
860 {
861         focus_handler ();
862
863         if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
864                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
865                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
866                         return true;
867                 }
868         }
869
870         if (binding_proxy.button_press_handler (ev)) {
871                 return true;
872         }
873
874         _grabbed = true;
875         CairoWidget::set_dirty ();
876
877         if (ev->button == 1 && !_act_on_release) {
878                 if (_action) {
879                         _action->activate ();
880                         return true;
881                 }
882         }
883
884         if (_fallthrough_to_parent)
885                 return false;
886
887         return true;
888 }
889
890 bool
891 ArdourButton::on_button_release_event (GdkEventButton *ev)
892 {
893         if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
894                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
895                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
896                         signal_led_clicked(); /* EMIT SIGNAL */
897                         return true;
898                 }
899         }
900
901         _grabbed = false;
902         CairoWidget::set_dirty ();
903
904         if (ev->button == 1 && _hovering) {
905                 signal_clicked ();
906                 if (_act_on_release) {
907                         if (_action) {
908                                 _action->activate ();
909                                 return true;
910                         }
911                 }
912         }
913
914         if (_fallthrough_to_parent)
915                 return false;
916
917         return true;
918 }
919
920 void
921 ArdourButton::set_distinct_led_click (bool yn)
922 {
923         _distinct_led_click = yn;
924         setup_led_rect ();
925 }
926
927 void
928 ArdourButton::color_handler ()
929 {
930         _update_colors = true;
931         CairoWidget::set_dirty ();
932 }
933
934 void
935 ArdourButton::on_size_allocate (Allocation& alloc)
936 {
937         CairoWidget::on_size_allocate (alloc);
938         setup_led_rect ();
939 }
940
941 void
942 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
943 {
944         watch_connection.disconnect ();
945         binding_proxy.set_controllable (c);
946 }
947
948 void
949 ArdourButton::watch ()
950 {
951         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
952
953         if (!c) {
954                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
955                 return;
956         }
957         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
958 }
959
960 void
961 ArdourButton::controllable_changed ()
962 {
963         float val = binding_proxy.get_controllable()->get_value();
964
965         if (fabs (val) >= 0.5f) {
966                 set_active_state (Gtkmm2ext::ExplicitActive);
967         } else {
968                 unset_active_state ();
969         }
970         CairoWidget::set_dirty ();
971 }
972
973 void
974 ArdourButton::set_related_action (RefPtr<Action> act)
975 {
976         Gtkmm2ext::Activatable::set_related_action (act);
977
978         if (_action) {
979
980                 action_tooltip_changed ();
981
982                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
983                 if (tact) {
984                         action_toggled ();
985                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
986                 }
987
988                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
989                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
990                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
991         }
992 }
993
994 void
995 ArdourButton::action_toggled ()
996 {
997         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
998
999         if (tact) {
1000                 if (tact->get_active()) {
1001                         set_active_state (Gtkmm2ext::ExplicitActive);
1002                 } else {
1003                         unset_active_state ();
1004                 }
1005         }
1006 }
1007
1008 void
1009 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1010 {
1011         _update_colors = true;
1012         CairoWidget::set_dirty ();
1013 }
1014
1015 void
1016 ArdourButton::on_name_changed ()
1017 {
1018         _char_pixel_width = 0;
1019         _char_pixel_height = 0;
1020         _diameter = 0;
1021         _update_colors = true;
1022         if (is_realized()) {
1023                 queue_resize ();
1024         }
1025 }
1026
1027 void
1028 ArdourButton::setup_led_rect ()
1029 {
1030         if (!(_elements & Indicator)) {
1031                 delete _led_rect;
1032                 _led_rect = 0;
1033                 return;
1034         }
1035
1036         if (!_led_rect) {
1037                 _led_rect = new cairo_rectangle_t;
1038         }
1039
1040         if (_elements & Text) {
1041                 if (_led_left) {
1042                         _led_rect->x = char_pixel_width();
1043                 } else {
1044                         _led_rect->x = get_width() - char_pixel_width() + _diameter;
1045                 }
1046         } else {
1047                 /* centered */
1048                 _led_rect->x = .5 * get_width() - _diameter;
1049         }
1050
1051         _led_rect->y = .5 * (get_height() - _diameter);
1052         _led_rect->width = _diameter;
1053         _led_rect->height = _diameter;
1054 }
1055
1056 void
1057 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1058 {
1059         _pixbuf = img;
1060         if (is_realized()) {
1061                 queue_resize ();
1062         }
1063 }
1064
1065 void
1066 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1067 {
1068         bool changed = (_active_state != s);
1069         CairoWidget::set_active_state (s);
1070         if (changed) {
1071                 _update_colors = true;
1072                 CairoWidget::set_dirty ();
1073         }
1074 }
1075
1076 void
1077 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1078 {
1079         bool changed = (_visual_state != s);
1080         CairoWidget::set_visual_state (s);
1081         if (changed) {
1082                 _update_colors = true;
1083                 CairoWidget::set_dirty ();
1084         }
1085 }
1086
1087 bool
1088 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1089 {
1090         _focused = true;
1091         CairoWidget::set_dirty ();
1092         return CairoWidget::on_focus_in_event (ev);
1093 }
1094
1095 bool
1096 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1097 {
1098         _focused = false;
1099         CairoWidget::set_dirty ();
1100         return CairoWidget::on_focus_out_event (ev);
1101 }
1102
1103 bool
1104 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1105         if (_focused &&
1106                         (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1107         {
1108                 signal_clicked();
1109                 if (_action) {
1110                         _action->activate ();
1111                 }
1112                 return true;
1113         }
1114         return CairoWidget::on_key_release_event (ev);
1115 }
1116
1117 bool
1118 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1119 {
1120         _hovering = (_elements & Inactive) ? false : true;
1121
1122         if (ARDOUR_UI::config()->get_widget_prelight()) {
1123                 CairoWidget::set_dirty ();
1124         }
1125
1126         return CairoWidget::on_enter_notify_event (ev);
1127 }
1128
1129 bool
1130 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1131 {
1132         _hovering = false;
1133
1134         if (ARDOUR_UI::config()->get_widget_prelight()) {
1135                 CairoWidget::set_dirty ();
1136         }
1137
1138         return CairoWidget::on_leave_notify_event (ev);
1139 }
1140
1141 void
1142 ArdourButton::set_tweaks (Tweaks t)
1143 {
1144         if (_tweaks != t) {
1145                 _tweaks = t;
1146                 if (is_realized()) {
1147                         queue_resize ();
1148                 }
1149         }
1150 }
1151
1152 void
1153 ArdourButton::action_sensitivity_changed ()
1154 {
1155         if (_action->property_sensitive ()) {
1156                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1157         } else {
1158                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1159         }
1160 }
1161
1162 void
1163 ArdourButton::set_layout_ellipsize_width (int w)
1164 {
1165         if (_layout_ellipsize_width == w) {
1166                 return;
1167         }
1168         _layout_ellipsize_width = w;
1169         if (!_layout) {
1170                 return;
1171         }
1172         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1173                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1174         }
1175         if (is_realized ()) {
1176                 queue_resize ();
1177         }
1178 }
1179
1180 void
1181 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1182 {
1183         if (_ellipsis == e) {
1184                 return;
1185         }
1186         _ellipsis = e;
1187         if (!_layout) {
1188                 return;
1189         }
1190         _layout->set_ellipsize(_ellipsis);
1191         if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1192                 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1193         }
1194         if (is_realized ()) {
1195                 queue_resize ();
1196         }
1197 }
1198
1199 void
1200 ArdourButton::ensure_layout ()
1201 {
1202         if (!_layout) {
1203                 ensure_style ();
1204                 _layout = Pango::Layout::create (get_pango_context());
1205                 _layout->set_ellipsize(_ellipsis);
1206                 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1207                         _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1208                 }
1209         }
1210 }
1211
1212 void
1213 ArdourButton::recalc_char_pixel_geometry ()
1214 {
1215         if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1216                 return;
1217         }
1218         ensure_layout();
1219         // NB. this is not static, since the geometry is different
1220         // depending on the font used.
1221         int w, h;
1222         std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1223         _layout->set_text (x);
1224         _layout->get_pixel_size (w, h);
1225         _char_pixel_height = std::max(4, h);
1226         // number of actual chars in the string (not bytes)
1227         // Glib to the rescue.
1228         Glib::ustring gx(x);
1229         _char_avg_pixel_width = w / (float)gx.size();
1230         _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1231         _layout->set_text (_text);
1232 }
1233
1234 void
1235 ArdourButton::action_visibility_changed ()
1236 {
1237         if (_action->property_visible ()) {
1238                 show ();
1239         } else {
1240                 hide ();
1241         }
1242 }
1243
1244 void
1245 ArdourButton::action_tooltip_changed ()
1246 {
1247         string str = _action->property_tooltip().get_value();
1248         ARDOUR_UI::instance()->set_tip (*this, str);
1249 }
1250
1251 void
1252 ArdourButton::set_elements (Element e)
1253 {
1254         _elements = e;
1255         CairoWidget::set_dirty ();
1256 }
1257
1258 void
1259 ArdourButton::add_elements (Element e)
1260 {
1261         _elements = (ArdourButton::Element) (_elements | e);
1262         CairoWidget::set_dirty ();
1263 }
1264
1265 void
1266 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1267 {
1268         if (led_custom_color == c && use_custom_led_color == useit) {
1269                 return;
1270         }
1271
1272         led_custom_color = c;
1273         use_custom_led_color = useit;
1274         CairoWidget::set_dirty ();
1275 }