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/controllable.h"
28 #include "pbd/error.h"
29 #include "pbd/stacktrace.h"
31 #include "gtkmm2ext/utils.h"
32 #include "gtkmm2ext/rgb_macros.h"
33 #include "gtkmm2ext/gui_thread.h"
35 #include "canvas/utils.h"
36 #include "canvas/colors.h"
38 #include "ardour_button.h"
40 #include "ui_config.h"
44 #define BASELINESTRETCH (1.25)
45 #define TRACKHEADERBTNW (3.10)
51 using namespace ARDOUR_UI_UTILS;
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);
60 ArdourButton::ArdourButton (Element e)
63 , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
64 , _tweaks (Tweaks (0))
65 , _char_pixel_width (0)
66 , _char_pixel_height (0)
67 , _char_avg_pixel_width (0)
71 , _corner_radius (2.5)
76 , fill_inactive_color (0)
77 , fill_active_color (0)
78 , text_active_color(0)
79 , text_inactive_color(0)
81 , led_inactive_color(0)
82 , led_custom_color (0)
83 , use_custom_led_color (false)
86 , led_inset_pattern (0)
88 , _act_on_release (true)
90 , _distinct_led_click (false)
93 , _fixed_colors_set (false)
94 , _fallthrough_to_parent (false)
95 , _layout_ellipsize_width (-1)
96 , _ellipsis (Pango::ELLIPSIZE_NONE)
97 , _update_colors (true)
100 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
101 /* This is not provided by gtkmm */
102 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
105 ArdourButton::ArdourButton (const std::string& str, Element e)
108 , _tweaks (Tweaks (0))
109 , _char_pixel_width (0)
110 , _char_pixel_height (0)
111 , _char_avg_pixel_width (0)
115 , _corner_radius (2.5)
120 , fill_inactive_color (0)
121 , fill_active_color (0)
122 , text_active_color(0)
123 , text_inactive_color(0)
124 , led_active_color(0)
125 , led_inactive_color(0)
126 , led_custom_color (0)
127 , use_custom_led_color (false)
129 , concave_pattern (0)
130 , led_inset_pattern (0)
132 , _act_on_release (true)
134 , _distinct_led_click (false)
137 , _fixed_colors_set (false)
138 , _fallthrough_to_parent (false)
139 , _layout_ellipsize_width (-1)
140 , _ellipsis (Pango::ELLIPSIZE_NONE)
141 , _update_colors (true)
142 , _pattern_height (0)
145 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
146 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
147 /* This is not provided by gtkmm */
148 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
151 ArdourButton::~ArdourButton()
155 if (convex_pattern) {
156 cairo_pattern_destroy (convex_pattern);
159 if (concave_pattern) {
160 cairo_pattern_destroy (concave_pattern);
163 if (led_inset_pattern) {
164 cairo_pattern_destroy (led_inset_pattern);
169 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
173 _layout->set_font_description (fd);
175 _char_pixel_width = 0;
176 _char_pixel_height = 0;
181 ArdourButton::set_text (const std::string& str)
187 if (!is_realized()) {
191 if (_layout && _layout->get_text() != _text) {
192 _layout->set_text (_text);
193 /* on_size_request() will fill in _text_width/height
194 * so queue it even if _sizing_text != "" */
200 ArdourButton::set_sizing_text (const std::string& str)
202 if (_sizing_text == str) {
206 if (!is_realized()) {
216 ArdourButton::set_angle (const double angle)
222 ArdourButton::set_alignment (const float xa, const float ya)
229 /* TODO make this a dedicated function elsewhere.
232 * virtual ArdourButton::render_vector_icon()
233 * ArdourIconButton::render_vector_icon
236 * ARDOUR_UI_UTILS::render_vector_icon()
239 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
244 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
246 if (_update_colors) {
249 if (get_height() != _pattern_height) {
253 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
254 text_color = text_active_color;
255 led_color = led_active_color;
257 text_color = text_inactive_color;
258 led_color = led_inactive_color;
261 if (use_custom_led_color) {
262 led_color = led_custom_color;
265 void (*rounded_function)(cairo_t*, double, double, double, double, double);
267 switch (_corner_mask) {
268 case 0x1: /* upper left only */
269 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
271 case 0x2: /* upper right only */
272 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
274 case 0x3: /* upper only */
275 rounded_function = Gtkmm2ext::rounded_top_rectangle;
277 /* should really have functions for lower right, lower left,
278 lower only, but for now, we don't
281 rounded_function = Gtkmm2ext::rounded_rectangle;
284 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
285 if ((_elements & (Body|Edge)) == (Body|Edge)) {
286 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
287 cairo_set_source_rgba (cr, 0, 0, 0, 1);
292 if ((_elements & Body)==Body) {
293 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
294 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
295 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
297 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
299 ArdourCanvas::set_source_rgba (cr, fill_active_color);
301 } else { //inactive, or it has an indicator
303 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
308 // IMPLICIT ACTIVE: draw a border of the active color
309 if ((_elements & Body)==Body) {
310 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
311 cairo_set_line_width (cr, 2.0);
312 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
313 ArdourCanvas::set_source_rgba (cr, fill_active_color);
318 //show the "convex" or "concave" gradient
319 if (!_flat_buttons && (_elements & Body)==Body) {
320 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
322 cairo_set_source (cr, concave_pattern);
323 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
326 cairo_set_source (cr, convex_pattern);
327 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
334 double x = rint((get_width() - _pixbuf->get_width()) * .5);
335 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
336 #if 0 // DEBUG style (print on hover)
337 if (_hovering || (_elements & Inactive)) {
338 printf("%s: p:%dx%d (%dx%d)\n",
340 _pixbuf->get_width(), _pixbuf->get_height(),
341 get_width(), get_height());
344 if (_elements & Menu) {
345 //if this is a DropDown with an icon, then we need to
346 //move the icon left slightly to accomomodate the arrow
349 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
350 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
353 else /* VectorIcons are exclusive to Pixbuf Icons */
354 if (_elements & VectorIcon) {
355 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
358 const int text_margin = char_pixel_width();
360 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
362 #if 0 // DEBUG style (print on hover)
363 if (_hovering || (_elements & Inactive)) {
364 bool layout_font = true;
365 Pango::FontDescription fd = _layout->get_font_description();
366 if (fd.gobj() == NULL) {
368 fd = get_pango_context()->get_font_description();
370 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
372 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
373 ceil(char_pixel_height() * BASELINESTRETCH),
374 _text_width, _text_height,
375 get_width(), get_height(),
376 layout_font ? "L:" : "W:",
377 fd.to_string().c_str());
382 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
386 ArdourCanvas::set_source_rgba (cr, text_color);
387 const double text_ypos = (get_height() - _text_height) * .5;
389 if (_elements & Menu) {
390 // always left align (dropdown)
391 cairo_move_to (cr, text_margin, text_ypos);
392 pango_cairo_show_layout (cr, _layout->gobj());
393 } else if ( (_elements & Indicator) == Indicator) {
394 // left/right align depending on LED position
396 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
398 cairo_move_to (cr, text_margin, text_ypos);
400 pango_cairo_show_layout (cr, _layout->gobj());
402 /* centered text otherwise */
409 cairo_rotate(cr, _angle * M_PI / 180.0);
410 cairo_device_to_user(cr, &ww, &wh);
411 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
412 ya = (wh - _text_height) * _yalign;
414 /* quick hack for left/bottom alignment at -90deg
415 * TODO this should be generalized incl rotation.
416 * currently only 'user' of this API is meter_strip.cc
418 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
420 cairo_move_to (cr, xa, ya);
421 pango_cairo_update_layout(cr, _layout->gobj());
422 pango_cairo_show_layout (cr, _layout->gobj());
429 if (_elements & Menu) {
430 const float trih = ceil(_diameter * .5);
431 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
433 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
434 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
435 cairo_rel_line_to(cr, -triw2, -trih);
436 cairo_rel_line_to(cr, 2. * triw2, 0);
437 cairo_close_path(cr);
439 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
442 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
443 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
444 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
445 cairo_close_path(cr);
446 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
447 cairo_set_line_width(cr, 1);
452 if (_elements & Indicator) {
455 /* move to the center of the indicator/led */
456 if (_elements & Text) {
457 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
459 cairo_translate (cr, led_xoff, get_height() * .5);
461 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
464 cairo_translate (cr, get_width() * .5, get_height() * .5);
468 if (!_flat_buttons) {
469 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
470 cairo_set_source (cr, led_inset_pattern);
475 cairo_set_source_rgb (cr, 0, 0, 0);
476 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
480 ArdourCanvas::set_source_rgba (cr, led_color);
481 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
487 // a transparent overlay to indicate insensitivity
488 if ((visual_state() & Gtkmm2ext::Insensitive)) {
489 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
490 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
491 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
495 // if requested, show hovering
496 if (UIConfiguration::instance().get_widget_prelight()
497 && !((visual_state() & Gtkmm2ext::Insensitive))) {
499 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
500 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
505 //user is currently pressing the button. dark outline helps to indicate this
506 if (_grabbed && !(_elements & (Inactive|Menu))) {
507 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
508 cairo_set_line_width(cr, 2);
509 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
513 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
514 if (visual_state() & Gtkmm2ext::Selected) {
515 cairo_set_line_width(cr, 1);
516 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
517 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
521 //I guess this means we have keyboard focus. I don't think this works currently
523 //A: yes, it's keyboard focus and it does work when there's no editor window
524 // (the editor is always the first receiver for KeyDown).
525 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
527 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
528 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
530 cairo_set_dash (cr, &dashes, 1, 0);
531 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
532 cairo_set_line_width (cr, 1.0);
534 cairo_set_dash (cr, 0, 0, 0);
539 ArdourButton::set_corner_radius (float r)
542 CairoWidget::set_dirty ();
546 ArdourButton::on_realize()
548 CairoWidget::on_realize ();
551 if (_layout->get_text() != _text) {
552 _layout->set_text (_text);
559 ArdourButton::on_size_request (Gtk::Requisition* req)
561 req->width = req->height = 0;
562 CairoWidget::on_size_request (req);
564 if (_diameter == 0) {
565 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
566 if (_diameter != newdia) {
572 if (_elements & Text) {
575 _layout->set_text (_text);
576 /* render() needs the size of the displayed text */
577 _layout->get_pixel_size (_text_width, _text_height);
579 if (_tweaks & OccasionalText) {
581 /* size should not change based on presence or absence
585 } else { //if (!_text.empty() || !_sizing_text.empty()) {
587 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
588 req->width += rint(1.75 * char_pixel_width()); // padding
590 if (!_sizing_text.empty()) {
591 _layout->set_text (_sizing_text); /* use sizing text */
594 int sizing_text_width = 0, sizing_text_height = 0;
595 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
597 req->width += sizing_text_width;
599 if (!_sizing_text.empty()) {
600 _layout->set_text (_text); /* restore display text */
604 /* XXX hack (surprise). Deal with two common rotation angles */
606 if (_angle == 90 || _angle == 270) {
607 /* do not swap text width or height because we rely on
608 these being the un-rotated values in ::render()
610 swap (req->width, req->height);
619 req->width += _pixbuf->get_width() + char_pixel_width();
620 req->height = std::max(req->height, _pixbuf->get_height() + 4);
623 if (_elements & Indicator) {
624 req->width += lrint (_diameter) + char_pixel_width();
625 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
628 if ((_elements & Menu)) {
629 req->width += _diameter + 4;
632 if (_elements & VectorIcon) {
633 assert(!(_elements & Text));
634 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
636 req->height = std::max(req->height, wh);
639 /* Tweaks to mess the nice stuff above up again. */
640 if (_tweaks & TrackHeader) {
641 // forget everything above and just use a fixed square [em] size
642 // "TrackHeader Buttons" are single letter (usually uppercase)
643 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
644 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
648 else if (_tweaks & Square) {
649 // currerntly unused (again)
650 if (req->width < req->height)
651 req->width = req->height;
652 if (req->height < req->width)
653 req->height = req->width;
654 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
655 // properly centered text for those elements that are centered
656 // (no sub-pixel offset)
657 if ((req->width - _text_width) & 1) { ++req->width; }
658 if ((req->height - _text_height) & 1) { ++req->height; }
661 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
666 * This sets the colors used for rendering based on the name of the button, and
667 * thus uses information from the GUI config data.
670 ArdourButton::set_colors ()
672 _update_colors = false;
674 if (_fixed_colors_set == 0x3) {
678 std::string name = get_name();
681 if (!(_fixed_colors_set & 0x1)) {
682 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
684 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
688 if (!(_fixed_colors_set & 0x2)) {
689 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
691 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
695 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
696 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
698 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
700 led_active_color = UIConfiguration::instance().color ("generic button: led active");
703 /* The inactive color for the LED is just a fairly dark version of the
707 ArdourCanvas::HSV inactive (led_active_color);
710 led_inactive_color = inactive.color ();
714 * This sets the colors used for rendering based on two fixed values, rather
715 * than basing them on the button name, and thus information in the GUI config
718 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
720 set_active_color (color_active);
721 set_inactive_color (color_inactive);
724 void ArdourButton::set_active_color (const uint32_t color)
726 _fixed_colors_set |= 0x1;
728 fill_active_color = color;
730 unsigned char r, g, b, a;
731 UINT_TO_RGBA(color, &r, &g, &b, &a);
733 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
734 (max (double(g), 255.) - min (double(g), 255.)) +
735 (max (double(b), 255.) - min (double(b), 255.));
737 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
738 (max (double(g), 0.) - min (double(g), 0.)) +
739 (max (double(b), 0.) - min (double(b), 0.));
741 text_active_color = (white_contrast > black_contrast) ?
742 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
743 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
745 /* XXX what about led colors ? */
746 CairoWidget::set_dirty ();
749 void ArdourButton::set_inactive_color (const uint32_t color)
751 _fixed_colors_set |= 0x2;
753 fill_inactive_color = color;
755 unsigned char r, g, b, a;
756 UINT_TO_RGBA(color, &r, &g, &b, &a);
758 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
759 (max (double(g), 255.) - min (double(g), 255.)) +
760 (max (double(b), 255.) - min (double(b), 255.));
762 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
763 (max (double(g), 0.) - min (double(g), 0.)) +
764 (max (double(b), 0.) - min (double(b), 0.));
766 text_inactive_color = (white_contrast > black_contrast) ?
767 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
768 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
770 /* XXX what about led colors ? */
771 CairoWidget::set_dirty ();
775 ArdourButton::build_patterns ()
777 if (convex_pattern) {
778 cairo_pattern_destroy (convex_pattern);
782 if (concave_pattern) {
783 cairo_pattern_destroy (concave_pattern);
787 if (led_inset_pattern) {
788 cairo_pattern_destroy (led_inset_pattern);
789 led_inset_pattern = 0;
793 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
794 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
795 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
798 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
799 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
800 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
802 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
803 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
804 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
806 _pattern_height = get_height() ;
810 ArdourButton::set_led_left (bool yn)
816 ArdourButton::on_button_press_event (GdkEventButton *ev)
818 focus_handler (this);
820 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
821 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
822 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
827 if (binding_proxy.button_press_handler (ev)) {
832 CairoWidget::set_dirty ();
834 if (ev->button == 1 && !_act_on_release) {
836 _action->activate ();
841 if (_fallthrough_to_parent)
848 ArdourButton::on_button_release_event (GdkEventButton *ev)
850 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
851 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
852 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
853 signal_led_clicked(ev); /* EMIT SIGNAL */
859 CairoWidget::set_dirty ();
861 if (ev->button == 1 && _hovering) {
863 if (_act_on_release) {
865 _action->activate ();
871 if (_fallthrough_to_parent)
878 ArdourButton::set_distinct_led_click (bool yn)
880 _distinct_led_click = yn;
885 ArdourButton::color_handler ()
887 _update_colors = true;
888 CairoWidget::set_dirty ();
892 ArdourButton::on_size_allocate (Allocation& alloc)
894 CairoWidget::on_size_allocate (alloc);
899 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
901 watch_connection.disconnect ();
902 binding_proxy.set_controllable (c);
906 ArdourButton::watch ()
908 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
911 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
914 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
918 ArdourButton::controllable_changed ()
920 float val = binding_proxy.get_controllable()->get_value();
922 if (fabs (val) >= 0.5f) {
923 set_active_state (Gtkmm2ext::ExplicitActive);
925 unset_active_state ();
927 CairoWidget::set_dirty ();
931 ArdourButton::set_related_action (RefPtr<Action> act)
933 Gtkmm2ext::Activatable::set_related_action (act);
937 action_tooltip_changed ();
939 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
942 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
945 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
946 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
947 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
952 ArdourButton::action_toggled ()
954 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
957 if (tact->get_active()) {
958 set_active_state (Gtkmm2ext::ExplicitActive);
960 unset_active_state ();
966 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
968 _update_colors = true;
969 CairoWidget::set_dirty ();
970 _char_pixel_width = 0;
971 _char_pixel_height = 0;
978 ArdourButton::on_name_changed ()
980 _char_pixel_width = 0;
981 _char_pixel_height = 0;
983 _update_colors = true;
990 ArdourButton::setup_led_rect ()
992 if (!(_elements & Indicator)) {
999 _led_rect = new cairo_rectangle_t;
1002 if (_elements & Text) {
1004 _led_rect->x = char_pixel_width();
1006 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1010 _led_rect->x = .5 * get_width() - _diameter;
1013 _led_rect->y = .5 * (get_height() - _diameter);
1014 _led_rect->width = _diameter;
1015 _led_rect->height = _diameter;
1019 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1022 if (is_realized()) {
1028 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1030 bool changed = (_active_state != s);
1031 CairoWidget::set_active_state (s);
1033 _update_colors = true;
1034 CairoWidget::set_dirty ();
1039 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1041 bool changed = (_visual_state != s);
1042 CairoWidget::set_visual_state (s);
1044 _update_colors = true;
1045 CairoWidget::set_dirty ();
1050 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1053 CairoWidget::set_dirty ();
1054 return CairoWidget::on_focus_in_event (ev);
1058 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1061 CairoWidget::set_dirty ();
1062 return CairoWidget::on_focus_out_event (ev);
1066 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1068 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1072 _action->activate ();
1076 return CairoWidget::on_key_release_event (ev);
1080 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1082 _hovering = (_elements & Inactive) ? false : true;
1084 if (UIConfiguration::instance().get_widget_prelight()) {
1085 CairoWidget::set_dirty ();
1088 return CairoWidget::on_enter_notify_event (ev);
1092 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1096 if (UIConfiguration::instance().get_widget_prelight()) {
1097 CairoWidget::set_dirty ();
1100 return CairoWidget::on_leave_notify_event (ev);
1104 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1105 /* Our implicit grab due to a button_press was broken by another grab:
1106 * the button will not get any button_release event if the mouse leaves
1107 * while the grab is taken, so unpress ourselves */
1109 CairoWidget::set_dirty ();
1114 ArdourButton::set_tweaks (Tweaks t)
1118 if (is_realized()) {
1125 ArdourButton::action_sensitivity_changed ()
1127 if (_action->property_sensitive ()) {
1128 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1130 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1135 ArdourButton::set_layout_ellipsize_width (int w)
1137 if (_layout_ellipsize_width == w) {
1140 _layout_ellipsize_width = w;
1144 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1145 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1147 if (is_realized ()) {
1153 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1155 if (_ellipsis == e) {
1162 _layout->set_ellipsize(_ellipsis);
1163 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1164 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1166 if (is_realized ()) {
1172 ArdourButton::ensure_layout ()
1176 _layout = Pango::Layout::create (get_pango_context());
1177 _layout->set_ellipsize(_ellipsis);
1178 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1179 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1185 ArdourButton::recalc_char_pixel_geometry ()
1187 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1191 // NB. this is not static, since the geometry is different
1192 // depending on the font used.
1194 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1195 _layout->set_text (x);
1196 _layout->get_pixel_size (w, h);
1197 _char_pixel_height = std::max(4, h);
1198 // number of actual chars in the string (not bytes)
1199 // Glib to the rescue.
1200 Glib::ustring gx(x);
1201 _char_avg_pixel_width = w / (float)gx.size();
1202 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1203 _layout->set_text (_text);
1207 ArdourButton::action_visibility_changed ()
1209 if (_action->property_visible ()) {
1217 ArdourButton::action_tooltip_changed ()
1219 string str = _action->property_tooltip().get_value();
1220 set_tooltip (*this, str);
1224 ArdourButton::set_elements (Element e)
1227 CairoWidget::set_dirty ();
1231 ArdourButton::add_elements (Element e)
1233 _elements = (ArdourButton::Element) (_elements | e);
1234 CairoWidget::set_dirty ();
1238 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1241 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1242 CairoWidget::set_dirty ();
1246 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1248 if (led_custom_color == c && use_custom_led_color == useit) {
1252 led_custom_color = c;
1253 use_custom_led_color = useit;
1254 CairoWidget::set_dirty ();