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