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