2 Copyright (C) 2010 Paul Davis
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.
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.
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.
24 #include <pangomm/layout.h>
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
34 #include "ardour/rc_configuration.h" // for widget prelight preference
36 #include "canvas/utils.h"
38 #include "ardour_button.h"
39 #include "ardour_ui.h"
40 #include "global_signals.h"
44 #define BASELINESTRETCH (1.25)
45 #define TRACKHEADERBTNW (3.10)
55 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
56 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
57 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
59 ArdourButton::ArdourButton (Element e)
61 , _tweaks (Tweaks (0))
62 , _char_pixel_width (0)
63 , _char_pixel_height (0)
64 , _char_avg_pixel_width (0)
68 , _corner_radius (2.5)
73 , fill_inactive_color (0)
74 , fill_active_color (0)
75 , text_active_color(0)
76 , text_inactive_color(0)
78 , led_inactive_color(0)
81 , led_inset_pattern (0)
83 , _act_on_release (true)
85 , _distinct_led_click (false)
88 , _fixed_colors_set (false)
89 , _fallthrough_to_parent (false)
90 , _layout_ellipsize_width (-1)
91 , _ellipsis (Pango::ELLIPSIZE_NONE)
92 , _update_colors (true)
95 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
98 ArdourButton::ArdourButton (const std::string& str, Element e)
100 , _tweaks (Tweaks (0))
104 , _corner_radius (2.5)
109 , fill_inactive_color (0)
110 , fill_active_color (0)
111 , text_active_color(0)
112 , text_inactive_color(0)
113 , led_active_color(0)
114 , led_inactive_color(0)
116 , concave_pattern (0)
117 , led_inset_pattern (0)
119 , _act_on_release (true)
121 , _distinct_led_click (false)
124 , _fixed_colors_set (false)
125 , _fallthrough_to_parent (false)
126 , _layout_ellipsize_width (-1)
127 , _ellipsis (Pango::ELLIPSIZE_NONE)
128 , _update_colors (true)
129 , _pattern_height (0)
132 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
133 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
136 ArdourButton::~ArdourButton()
140 if (convex_pattern) {
141 cairo_pattern_destroy (convex_pattern);
144 if (concave_pattern) {
145 cairo_pattern_destroy (concave_pattern);
148 if (led_inset_pattern) {
149 cairo_pattern_destroy (led_inset_pattern);
154 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
158 _layout->set_font_description (fd);
164 ArdourButton::set_text (const std::string& str)
167 if (!is_realized()) {
171 if (_layout && _layout->get_text() != _text) {
172 _layout->set_text (_text);
178 ArdourButton::set_angle (const double angle)
184 ArdourButton::set_alignment (const float xa, const float ya)
191 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
196 if (_update_colors) {
199 if (get_height() != _pattern_height) {
203 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
204 text_color = text_active_color;
205 led_color = led_active_color;
207 text_color = text_inactive_color;
208 led_color = led_inactive_color;
211 void (*rounded_function)(cairo_t*, double, double, double, double, double);
213 switch (_corner_mask) {
214 case 0x1: /* upper left only */
215 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
217 case 0x2: /* upper right only */
218 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
220 case 0x3: /* upper only */
221 rounded_function = Gtkmm2ext::rounded_top_rectangle;
223 /* should really have functions for lower right, lower left,
224 lower only, but for now, we don't
227 rounded_function = Gtkmm2ext::rounded_rectangle;
230 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
231 if ((_elements & (Body|Edge)) == (Body|Edge)) {
232 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
233 cairo_set_source_rgba (cr, 0, 0, 0, 1);
238 if ((_elements & Body)==Body) {
239 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
240 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
241 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
243 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
245 ArdourCanvas::set_source_rgba (cr, fill_active_color);
247 } else { //inactive, or it has an indicator
249 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
254 // IMPLICIT ACTIVE: draw a border of the active color
255 if ((_elements & Body)==Body) {
256 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
257 cairo_set_line_width (cr, 2.0);
258 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
259 ArdourCanvas::set_source_rgba (cr, fill_active_color);
264 //show the "convex" or "concave" gradient
265 if (!_flat_buttons) {
266 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
268 cairo_set_source (cr, concave_pattern);
269 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
272 cairo_set_source (cr, convex_pattern);
273 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
280 double x = rint((get_width() - _pixbuf->get_width()) * .5);
281 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
282 #if 0 // DEBUG style (print on hover)
283 if (_hovering || (_elements & Inactive)) {
284 printf("%s: p:%dx%d (%dx%d)\n",
286 _pixbuf->get_width(), _pixbuf->get_height(),
287 get_width(), get_height());
290 if (_elements & Menu) {
291 //if this is a DropDown with an icon, then we need to
292 //move the icon left slightly to accomomodate the arrow
295 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
296 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
299 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
300 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
301 const double x = get_width() * .5;
302 const double y = get_height() * .5;
303 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
304 const double slit = .11 * M_PI;
306 cairo_translate(cr, x, y);
308 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
309 if (active_state() == Gtkmm2ext::ExplicitActive)
310 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
312 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
313 cairo_fill_preserve(cr);
314 cairo_set_source_rgba (cr, .0, .0, .0, .5);
315 cairo_set_line_width(cr, 1);
319 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
321 cairo_rotate (cr, -.5 * M_PI);
322 cairo_move_to(cr, 0, 0);
323 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
324 cairo_line_to(cr, 0, 0);
325 cairo_close_path(cr);
328 cairo_rotate (cr, 2. * M_PI / 3.);
330 cairo_move_to(cr, 0, 0);
331 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
332 cairo_line_to(cr, 0, 0);
333 cairo_close_path(cr);
336 cairo_rotate (cr, 2. * M_PI / 3.);
337 cairo_move_to(cr, 0, 0);
338 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
339 cairo_line_to(cr, 0, 0);
340 cairo_close_path(cr);
345 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
346 if (active_state() == Gtkmm2ext::ExplicitActive)
347 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
349 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
351 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
352 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
357 else if (_elements & RecButton) {
358 const double x = get_width() * .5;
359 const double y = get_height() * .5;
360 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
361 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
362 if (active_state() == Gtkmm2ext::ExplicitActive)
363 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
365 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
366 cairo_fill_preserve(cr);
367 cairo_set_source_rgba (cr, .0, .0, .0, .8);
368 cairo_set_line_width(cr, 1);
371 else if (_elements & CloseCross) {
372 const double x = get_width() * .5;
373 const double y = get_height() * .5;
374 const double o = .5 + std::min(x, y) * .4;
375 ArdourCanvas::set_source_rgba (cr, text_color);
376 cairo_set_line_width(cr, 1);
377 cairo_move_to(cr, x-o, y-o);
378 cairo_line_to(cr, x+o, y+o);
379 cairo_move_to(cr, x+o, y-o);
380 cairo_line_to(cr, x-o, y+o);
384 const int text_margin = char_pixel_width();
386 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
388 #if 0 // DEBUG style (print on hover)
389 if (_hovering || (_elements & Inactive)) {
390 bool layout_font = true;
391 Pango::FontDescription fd = _layout->get_font_description();
392 if (fd.gobj() == NULL) {
394 fd = get_pango_context()->get_font_description();
396 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
398 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
399 ceil(char_pixel_height() * BASELINESTRETCH),
400 _text_width, _text_height,
401 get_width(), get_height(),
402 layout_font ? "L:" : "W:",
403 fd.to_string().c_str());
408 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
412 ArdourCanvas::set_source_rgba (cr, text_color);
413 const double text_ypos = (get_height() - _text_height) * .5;
415 if (_elements & Menu) {
416 // always left align (dropdown)
417 cairo_move_to (cr, text_margin, text_ypos);
418 pango_cairo_show_layout (cr, _layout->gobj());
419 } else if ( (_elements & Indicator) == Indicator) {
420 // left/right align depending on LED position
422 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
424 cairo_move_to (cr, text_margin, text_ypos);
426 pango_cairo_show_layout (cr, _layout->gobj());
428 /* centered text otherwise */
435 cairo_rotate(cr, _angle * M_PI / 180.0);
436 cairo_device_to_user(cr, &ww, &wh);
437 xa = (ww - _text_width) * _xalign;
438 ya = (wh - _text_height) * _yalign;
440 /* quick hack for left/bottom alignment at -90deg
441 * TODO this should be generalized incl rotation.
442 * currently only 'user' of this API is meter_strip.cc
444 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
446 cairo_move_to (cr, xa, ya);
447 pango_cairo_update_layout(cr, _layout->gobj());
448 pango_cairo_show_layout (cr, _layout->gobj());
455 if (_elements & Menu) {
456 const float trih = ceil(_diameter * .5);
457 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
459 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
460 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
461 cairo_rel_line_to(cr, -triw2, -trih);
462 cairo_rel_line_to(cr, 2. * triw2, 0);
463 cairo_close_path(cr);
465 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
468 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
469 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
470 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
471 cairo_close_path(cr);
472 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
473 cairo_set_line_width(cr, 1);
478 if (_elements & Indicator) {
481 /* move to the center of the indicator/led */
482 if (_elements & Text) {
483 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
485 cairo_translate (cr, led_xoff, get_height() * .5);
487 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
490 cairo_translate (cr, get_width() * .5, get_height() * .5);
494 if (!_flat_buttons) {
495 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
496 cairo_set_source (cr, led_inset_pattern);
501 cairo_set_source_rgb (cr, 0, 0, 0);
502 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
506 ArdourCanvas::set_source_rgba (cr, led_color);
507 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
513 // a transparent gray layer to indicate insensitivity
514 if ((visual_state() & Gtkmm2ext::Insensitive)) {
515 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
516 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
520 // if requested, show hovering
521 if (ARDOUR::Config->get_widget_prelight()
522 && !((visual_state() & Gtkmm2ext::Insensitive))) {
524 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
525 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
530 //user is currently pressing the button. dark outline helps to indicate this
531 if (_grabbed && !(_elements & (Inactive|Menu))) {
532 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
533 cairo_set_line_width(cr, 2);
534 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
538 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
539 if (visual_state() & Gtkmm2ext::Selected) {
540 cairo_set_line_width(cr, 1);
541 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
542 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
546 //I guess this means we have keyboard focus. I don't think this works currently
548 //A: yes, it's keyboard focus and it does work when there's no editor window
549 // (the editor is always the first receiver for KeyDown).
550 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
552 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
553 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
555 cairo_set_dash (cr, &dashes, 1, 0);
556 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
557 cairo_set_line_width (cr, 1.0);
559 cairo_set_dash (cr, 0, 0, 0);
564 ArdourButton::set_corner_radius (float r)
567 CairoWidget::set_dirty ();
571 ArdourButton::on_realize()
573 CairoWidget::on_realize ();
575 if (_layout && _layout->get_text() != _text) {
576 _layout->set_text (_text);
582 ArdourButton::on_size_request (Gtk::Requisition* req)
584 req->width = req->height = 0;
585 CairoWidget::on_size_request (req);
587 if (_diameter == 0) {
588 const float newdia = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
589 if (_diameter != newdia) {
595 if ((_elements & Text) && !_text.empty()) {
596 // if _layout does not exist, char_pixel_height() creates it,
597 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
598 _layout->get_pixel_size (_text_width, _text_height);
599 req->width += rint(1.75 * char_pixel_width()); // padding
600 req->width += _text_width;
607 req->width += _pixbuf->get_width() + char_pixel_width();
608 req->height = std::max(req->height, _pixbuf->get_height() + 4);
611 if (_elements & Indicator) {
612 req->width += lrint (_diameter) + char_pixel_width();
613 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
616 if ((_elements & Menu)) {
617 req->width += _diameter + 4;
620 if (_elements & (RecButton | CloseCross)) {
621 assert(!(_elements & Text));
622 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
624 req->height = std::max(req->height, wh);
627 /* Tweaks to mess the nice stuff above up again. */
628 if (_tweaks & TrackHeader) {
629 // forget everything above and just use a fixed square [em] size
630 // "TrackHeader Buttons" are single letter (usually uppercase)
631 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
632 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
636 else if (_tweaks & Square) {
637 // currerntly unused (again)
638 if (req->width < req->height)
639 req->width = req->height;
640 if (req->height < req->width)
641 req->height = req->width;
642 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
643 // properly centered text for those elements that are centered
644 // (no sub-pixel offset)
645 if ((req->width - _text_width) & 1) { ++req->width; }
646 if ((req->height - _text_height) & 1) { ++req->height; }
649 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
654 * This sets the colors used for rendering based on the name of the button, and
655 * thus uses information from the GUI config data.
658 ArdourButton::set_colors ()
660 _update_colors = false;
661 if (_fixed_colors_set) {
664 std::string name = get_name();
666 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
667 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
669 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
670 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
672 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
673 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
677 * This sets the colors used for rendering based on two fixed values, rather
678 * than basing them on the button name, and thus information in the GUI config
681 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
683 _fixed_colors_set = true;
685 fill_active_color = color_active;
686 fill_inactive_color = color_inactive;
688 unsigned char r, g, b, a;
689 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
691 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
692 (max (double(g), 255.) - min (double(g), 255.)) +
693 (max (double(b), 255.) - min (double(b), 255.));
695 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
696 (max (double(g), 0.) - min (double(g), 0.)) +
697 (max (double(b), 0.) - min (double(b), 0.));
699 text_active_color = (white_contrast > black_contrast) ?
700 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
701 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
704 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
706 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
707 (max (double(g), 255.) - min (double(g), 255.)) +
708 (max (double(b), 255.) - min (double(b), 255.));
710 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
711 (max (double(g), 0.) - min (double(g), 0.)) +
712 (max (double(b), 0.) - min (double(b), 0.));
714 text_inactive_color = (white_contrast > black_contrast) ?
715 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
716 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
718 /* XXX what about led colors ? */
719 CairoWidget::set_dirty ();
723 ArdourButton::build_patterns ()
725 if (convex_pattern) {
726 cairo_pattern_destroy (convex_pattern);
730 if (concave_pattern) {
731 cairo_pattern_destroy (concave_pattern);
735 if (led_inset_pattern) {
736 cairo_pattern_destroy (led_inset_pattern);
737 led_inset_pattern = 0;
741 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
742 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
743 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
746 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
747 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
748 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
750 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
751 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
752 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
754 _pattern_height = get_height() ;
758 ArdourButton::set_led_left (bool yn)
764 ArdourButton::on_button_press_event (GdkEventButton *ev)
768 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
769 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
770 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
775 if (binding_proxy.button_press_handler (ev)) {
780 CairoWidget::set_dirty ();
782 if (!_act_on_release) {
784 _action->activate ();
789 if (_fallthrough_to_parent)
796 ArdourButton::on_button_release_event (GdkEventButton *ev)
798 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
799 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
800 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
801 signal_led_clicked(); /* EMIT SIGNAL */
807 CairoWidget::set_dirty ();
811 if (_act_on_release) {
813 _action->activate ();
819 if (_fallthrough_to_parent)
826 ArdourButton::set_distinct_led_click (bool yn)
828 _distinct_led_click = yn;
833 ArdourButton::color_handler ()
835 _update_colors = true;
836 CairoWidget::set_dirty ();
840 ArdourButton::on_size_allocate (Allocation& alloc)
842 CairoWidget::on_size_allocate (alloc);
847 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
849 watch_connection.disconnect ();
850 binding_proxy.set_controllable (c);
854 ArdourButton::watch ()
856 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
859 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
862 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
866 ArdourButton::controllable_changed ()
868 float val = binding_proxy.get_controllable()->get_value();
870 if (fabs (val) >= 0.5f) {
871 set_active_state (Gtkmm2ext::ExplicitActive);
873 unset_active_state ();
875 CairoWidget::set_dirty ();
879 ArdourButton::set_related_action (RefPtr<Action> act)
881 Gtkmm2ext::Activatable::set_related_action (act);
885 action_tooltip_changed ();
887 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
890 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
893 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
894 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
895 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
900 ArdourButton::action_toggled ()
902 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
905 if (tact->get_active()) {
906 set_active_state (Gtkmm2ext::ExplicitActive);
908 unset_active_state ();
914 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
916 _update_colors = true;
917 CairoWidget::set_dirty ();
921 ArdourButton::on_name_changed ()
923 _char_pixel_width = 0;
924 _char_pixel_height = 0;
926 _update_colors = true;
933 ArdourButton::setup_led_rect ()
935 if (!(_elements & Indicator)) {
942 _led_rect = new cairo_rectangle_t;
945 if (_elements & Text) {
947 _led_rect->x = char_pixel_width();
949 _led_rect->x = get_width() - char_pixel_width() + _diameter;
953 _led_rect->x = .5 * get_width() - _diameter;
956 _led_rect->y = .5 * (get_height() - _diameter);
957 _led_rect->width = _diameter;
958 _led_rect->height = _diameter;
962 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
971 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
973 bool changed = (_active_state != s);
974 CairoWidget::set_active_state (s);
976 _update_colors = true;
977 CairoWidget::set_dirty ();
982 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
984 bool changed = (_visual_state != s);
985 CairoWidget::set_visual_state (s);
987 _update_colors = true;
988 CairoWidget::set_dirty ();
993 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
996 CairoWidget::set_dirty ();
997 return CairoWidget::on_focus_in_event (ev);
1001 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1004 CairoWidget::set_dirty ();
1005 return CairoWidget::on_focus_out_event (ev);
1009 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1011 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1015 _action->activate ();
1019 return CairoWidget::on_key_release_event (ev);
1023 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1025 _hovering = (_elements & Inactive) ? false : true;
1027 if (ARDOUR::Config->get_widget_prelight()) {
1028 CairoWidget::set_dirty ();
1031 return CairoWidget::on_enter_notify_event (ev);
1035 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1039 if (ARDOUR::Config->get_widget_prelight()) {
1040 CairoWidget::set_dirty ();
1043 return CairoWidget::on_leave_notify_event (ev);
1047 ArdourButton::set_tweaks (Tweaks t)
1051 if (is_realized()) {
1058 ArdourButton::action_sensitivity_changed ()
1060 if (_action->property_sensitive ()) {
1061 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1063 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1068 ArdourButton::set_layout_ellisize_width (int w)
1070 if (_layout_ellipsize_width == w) {
1073 _layout_ellipsize_width = w;
1077 if (_layout_ellipsize_width > 0) {
1078 _layout->set_width (_layout_ellipsize_width);
1083 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1085 if (_ellipsis == e) {
1092 _layout->set_ellipsize(_ellipsis);
1093 if (_layout_ellipsize_width > 0) {
1094 _layout->set_width (_layout_ellipsize_width);
1096 if (is_realized ()) {
1102 ArdourButton::ensure_layout ()
1106 _layout = Pango::Layout::create (get_pango_context());
1107 _layout->set_ellipsize(_ellipsis);
1108 if (_layout_ellipsize_width > 0) {
1109 _layout->set_width (_layout_ellipsize_width);
1115 ArdourButton::recalc_char_pixel_geometry ()
1117 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1121 // NB. this is not static, since the geometry is different
1122 // depending on the font used.
1124 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1125 _layout->set_text (x);
1126 _layout->get_pixel_size (w, h);
1127 _char_pixel_height = std::max(4, h);
1128 // number of actual chars in the string (not bytes)
1129 // Glib to the rescue.
1130 Glib::ustring gx(x);
1131 _char_avg_pixel_width = w / (float)gx.size();
1132 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1133 _layout->set_text (_text);
1137 ArdourButton::action_visibility_changed ()
1139 if (_action->property_visible ()) {
1147 ArdourButton::action_tooltip_changed ()
1149 string str = _action->property_tooltip().get_value();
1150 ARDOUR_UI::instance()->set_tip (*this, str);
1154 ArdourButton::set_elements (Element e)
1157 CairoWidget::set_dirty ();
1161 ArdourButton::add_elements (Element e)
1163 _elements = (ArdourButton::Element) (_elements | e);
1164 CairoWidget::set_dirty ();