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