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