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)
62 , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
63 , _tweaks (Tweaks (0))
64 , _char_pixel_width (0)
65 , _char_pixel_height (0)
66 , _char_avg_pixel_width (0)
70 , _corner_radius (2.5)
75 , fill_inactive_color (0)
76 , fill_active_color (0)
77 , text_active_color(0)
78 , text_inactive_color(0)
80 , led_inactive_color(0)
81 , led_custom_color (0)
82 , use_custom_led_color (false)
85 , led_inset_pattern (0)
87 , _act_on_release (true)
89 , _distinct_led_click (false)
92 , _fixed_colors_set (false)
93 , _fallthrough_to_parent (false)
94 , _layout_ellipsize_width (-1)
95 , _ellipsis (Pango::ELLIPSIZE_NONE)
96 , _update_colors (true)
99 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
100 /* This is not provided by gtkmm */
101 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
104 ArdourButton::ArdourButton (const std::string& str, Element e)
106 , _tweaks (Tweaks (0))
107 , _char_pixel_width (0)
108 , _char_pixel_height (0)
109 , _char_avg_pixel_width (0)
113 , _corner_radius (2.5)
118 , fill_inactive_color (0)
119 , fill_active_color (0)
120 , text_active_color(0)
121 , text_inactive_color(0)
122 , led_active_color(0)
123 , led_inactive_color(0)
124 , led_custom_color (0)
125 , use_custom_led_color (false)
127 , concave_pattern (0)
128 , led_inset_pattern (0)
130 , _act_on_release (true)
132 , _distinct_led_click (false)
135 , _fixed_colors_set (false)
136 , _fallthrough_to_parent (false)
137 , _layout_ellipsize_width (-1)
138 , _ellipsis (Pango::ELLIPSIZE_NONE)
139 , _update_colors (true)
140 , _pattern_height (0)
143 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
144 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
145 /* This is not provided by gtkmm */
146 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
149 ArdourButton::~ArdourButton()
153 if (convex_pattern) {
154 cairo_pattern_destroy (convex_pattern);
157 if (concave_pattern) {
158 cairo_pattern_destroy (concave_pattern);
161 if (led_inset_pattern) {
162 cairo_pattern_destroy (led_inset_pattern);
167 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
171 _layout->set_font_description (fd);
177 ArdourButton::set_text (const std::string& str)
183 if (!is_realized()) {
187 if (_layout && _layout->get_text() != _text) {
188 _layout->set_text (_text);
194 ArdourButton::set_angle (const double angle)
200 ArdourButton::set_alignment (const float xa, const float ya)
207 /* TODO make this a dedicated function elsewhere.
210 * virtual ArdourButton::render_vector_icon()
211 * ArdourIconButton::render_vector_icon
214 * ARDOUR_UI_UTILS::render_vector_icon()
217 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
222 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
224 if (_update_colors) {
227 if (get_height() != _pattern_height) {
231 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
232 text_color = text_active_color;
233 led_color = led_active_color;
235 text_color = text_inactive_color;
236 led_color = led_inactive_color;
239 if (use_custom_led_color) {
240 led_color = led_custom_color;
243 void (*rounded_function)(cairo_t*, double, double, double, double, double);
245 switch (_corner_mask) {
246 case 0x1: /* upper left only */
247 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
249 case 0x2: /* upper right only */
250 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
252 case 0x3: /* upper only */
253 rounded_function = Gtkmm2ext::rounded_top_rectangle;
255 /* should really have functions for lower right, lower left,
256 lower only, but for now, we don't
259 rounded_function = Gtkmm2ext::rounded_rectangle;
262 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
263 if ((_elements & (Body|Edge)) == (Body|Edge)) {
264 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
265 cairo_set_source_rgba (cr, 0, 0, 0, 1);
270 if ((_elements & Body)==Body) {
271 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
272 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
273 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
275 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
277 ArdourCanvas::set_source_rgba (cr, fill_active_color);
279 } else { //inactive, or it has an indicator
281 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
286 // IMPLICIT ACTIVE: draw a border of the active color
287 if ((_elements & Body)==Body) {
288 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
289 cairo_set_line_width (cr, 2.0);
290 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
291 ArdourCanvas::set_source_rgba (cr, fill_active_color);
296 //show the "convex" or "concave" gradient
297 if (!_flat_buttons && (_elements & Body)==Body) {
298 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
300 cairo_set_source (cr, concave_pattern);
301 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
304 cairo_set_source (cr, convex_pattern);
305 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
312 double x = rint((get_width() - _pixbuf->get_width()) * .5);
313 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
314 #if 0 // DEBUG style (print on hover)
315 if (_hovering || (_elements & Inactive)) {
316 printf("%s: p:%dx%d (%dx%d)\n",
318 _pixbuf->get_width(), _pixbuf->get_height(),
319 get_width(), get_height());
322 if (_elements & Menu) {
323 //if this is a DropDown with an icon, then we need to
324 //move the icon left slightly to accomomodate the arrow
327 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
328 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
331 else /* VectorIcons are exclusive to Pixbuf Icons */
332 if (_elements & VectorIcon) {
333 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
336 const int text_margin = char_pixel_width();
338 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
340 #if 0 // DEBUG style (print on hover)
341 if (_hovering || (_elements & Inactive)) {
342 bool layout_font = true;
343 Pango::FontDescription fd = _layout->get_font_description();
344 if (fd.gobj() == NULL) {
346 fd = get_pango_context()->get_font_description();
348 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
350 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
351 ceil(char_pixel_height() * BASELINESTRETCH),
352 _text_width, _text_height,
353 get_width(), get_height(),
354 layout_font ? "L:" : "W:",
355 fd.to_string().c_str());
360 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
364 ArdourCanvas::set_source_rgba (cr, text_color);
365 const double text_ypos = (get_height() - _text_height) * .5;
367 if (_elements & Menu) {
368 // always left align (dropdown)
369 cairo_move_to (cr, text_margin, text_ypos);
370 pango_cairo_show_layout (cr, _layout->gobj());
371 } else if ( (_elements & Indicator) == Indicator) {
372 // left/right align depending on LED position
374 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
376 cairo_move_to (cr, text_margin, text_ypos);
378 pango_cairo_show_layout (cr, _layout->gobj());
380 /* centered text otherwise */
387 cairo_rotate(cr, _angle * M_PI / 180.0);
388 cairo_device_to_user(cr, &ww, &wh);
389 xa = (ww - _text_width) * _xalign;
390 ya = (wh - _text_height) * _yalign;
392 /* quick hack for left/bottom alignment at -90deg
393 * TODO this should be generalized incl rotation.
394 * currently only 'user' of this API is meter_strip.cc
396 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
398 cairo_move_to (cr, xa, ya);
399 pango_cairo_update_layout(cr, _layout->gobj());
400 pango_cairo_show_layout (cr, _layout->gobj());
407 if (_elements & Menu) {
408 const float trih = ceil(_diameter * .5);
409 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
411 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
412 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
413 cairo_rel_line_to(cr, -triw2, -trih);
414 cairo_rel_line_to(cr, 2. * triw2, 0);
415 cairo_close_path(cr);
417 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
420 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
421 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
422 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
423 cairo_close_path(cr);
424 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
425 cairo_set_line_width(cr, 1);
430 if (_elements & Indicator) {
433 /* move to the center of the indicator/led */
434 if (_elements & Text) {
435 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
437 cairo_translate (cr, led_xoff, get_height() * .5);
439 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
442 cairo_translate (cr, get_width() * .5, get_height() * .5);
446 if (!_flat_buttons) {
447 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
448 cairo_set_source (cr, led_inset_pattern);
453 cairo_set_source_rgb (cr, 0, 0, 0);
454 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
458 ArdourCanvas::set_source_rgba (cr, led_color);
459 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
465 // a transparent overlay to indicate insensitivity
466 if ((visual_state() & Gtkmm2ext::Insensitive)) {
467 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
468 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
469 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
473 // if requested, show hovering
474 if (UIConfiguration::instance().get_widget_prelight()
475 && !((visual_state() & Gtkmm2ext::Insensitive))) {
477 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
478 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
483 //user is currently pressing the button. dark outline helps to indicate this
484 if (_grabbed && !(_elements & (Inactive|Menu))) {
485 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
486 cairo_set_line_width(cr, 2);
487 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
491 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
492 if (visual_state() & Gtkmm2ext::Selected) {
493 cairo_set_line_width(cr, 1);
494 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
495 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
499 //I guess this means we have keyboard focus. I don't think this works currently
501 //A: yes, it's keyboard focus and it does work when there's no editor window
502 // (the editor is always the first receiver for KeyDown).
503 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
505 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
506 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
508 cairo_set_dash (cr, &dashes, 1, 0);
509 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
510 cairo_set_line_width (cr, 1.0);
512 cairo_set_dash (cr, 0, 0, 0);
517 ArdourButton::set_corner_radius (float r)
520 CairoWidget::set_dirty ();
524 ArdourButton::on_realize()
526 CairoWidget::on_realize ();
528 if (_layout && _layout->get_text() != _text) {
529 _layout->set_text (_text);
535 ArdourButton::on_size_request (Gtk::Requisition* req)
537 req->width = req->height = 0;
538 CairoWidget::on_size_request (req);
540 if (_diameter == 0) {
541 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
542 if (_diameter != newdia) {
548 if (_elements & Text) {
550 if (_tweaks & OccasionalText) {
552 /* size should not change based on presence or absence
556 if (!_text.empty()) {
558 _layout->set_text (_text);
559 _layout->get_pixel_size (_text_width, _text_height);
562 } else if (!_text.empty()) {
564 //if _layout does not exist, char_pixel_height() creates it,
566 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
568 _layout->get_pixel_size (_text_width, _text_height);
569 req->width += rint(1.75 * char_pixel_width()); // padding
570 req->width += _text_width;
573 /* XXX hack (surprise). Deal with two common rotation angles */
575 if (_angle == 90 || _angle == 270) {
576 /* do not swap text width or height because we rely on
577 these being the un-rotated values in ::render()
579 swap (req->width, req->height);
588 req->width += _pixbuf->get_width() + char_pixel_width();
589 req->height = std::max(req->height, _pixbuf->get_height() + 4);
592 if (_elements & Indicator) {
593 req->width += lrint (_diameter) + char_pixel_width();
594 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
597 if ((_elements & Menu)) {
598 req->width += _diameter + 4;
601 if (_elements & VectorIcon) {
602 assert(!(_elements & Text));
603 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
605 req->height = std::max(req->height, wh);
608 /* Tweaks to mess the nice stuff above up again. */
609 if (_tweaks & TrackHeader) {
610 // forget everything above and just use a fixed square [em] size
611 // "TrackHeader Buttons" are single letter (usually uppercase)
612 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
613 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
617 else if (_tweaks & Square) {
618 // currerntly unused (again)
619 if (req->width < req->height)
620 req->width = req->height;
621 if (req->height < req->width)
622 req->height = req->width;
623 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
624 // properly centered text for those elements that are centered
625 // (no sub-pixel offset)
626 if ((req->width - _text_width) & 1) { ++req->width; }
627 if ((req->height - _text_height) & 1) { ++req->height; }
630 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
635 * This sets the colors used for rendering based on the name of the button, and
636 * thus uses information from the GUI config data.
639 ArdourButton::set_colors ()
641 _update_colors = false;
643 if (_fixed_colors_set == 0x3) {
647 std::string name = get_name();
650 if (!(_fixed_colors_set & 0x1)) {
651 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
653 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
657 if (!(_fixed_colors_set & 0x2)) {
658 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
660 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
664 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
665 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
667 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
669 led_active_color = UIConfiguration::instance().color ("generic button: led active");
672 /* The inactive color for the LED is just a fairly dark version of the
676 ArdourCanvas::HSV inactive (led_active_color);
679 led_inactive_color = inactive.color ();
683 * This sets the colors used for rendering based on two fixed values, rather
684 * than basing them on the button name, and thus information in the GUI config
687 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
689 set_active_color (color_active);
690 set_inactive_color (color_inactive);
693 void ArdourButton::set_active_color (const uint32_t color)
695 _fixed_colors_set |= 0x1;
697 fill_active_color = color;
699 unsigned char r, g, b, a;
700 UINT_TO_RGBA(color, &r, &g, &b, &a);
702 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
703 (max (double(g), 255.) - min (double(g), 255.)) +
704 (max (double(b), 255.) - min (double(b), 255.));
706 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
707 (max (double(g), 0.) - min (double(g), 0.)) +
708 (max (double(b), 0.) - min (double(b), 0.));
710 text_active_color = (white_contrast > black_contrast) ?
711 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
712 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
714 /* XXX what about led colors ? */
715 CairoWidget::set_dirty ();
718 void ArdourButton::set_inactive_color (const uint32_t color)
720 _fixed_colors_set |= 0x2;
722 fill_inactive_color = color;
724 unsigned char r, g, b, a;
725 UINT_TO_RGBA(color, &r, &g, &b, &a);
727 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
728 (max (double(g), 255.) - min (double(g), 255.)) +
729 (max (double(b), 255.) - min (double(b), 255.));
731 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
732 (max (double(g), 0.) - min (double(g), 0.)) +
733 (max (double(b), 0.) - min (double(b), 0.));
735 text_inactive_color = (white_contrast > black_contrast) ?
736 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
737 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
739 /* XXX what about led colors ? */
740 CairoWidget::set_dirty ();
744 ArdourButton::build_patterns ()
746 if (convex_pattern) {
747 cairo_pattern_destroy (convex_pattern);
751 if (concave_pattern) {
752 cairo_pattern_destroy (concave_pattern);
756 if (led_inset_pattern) {
757 cairo_pattern_destroy (led_inset_pattern);
758 led_inset_pattern = 0;
762 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
763 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
764 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
767 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
768 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
769 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
771 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
772 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
773 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
775 _pattern_height = get_height() ;
779 ArdourButton::set_led_left (bool yn)
785 ArdourButton::on_button_press_event (GdkEventButton *ev)
787 focus_handler (this);
789 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
790 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
791 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
796 if (binding_proxy.button_press_handler (ev)) {
801 CairoWidget::set_dirty ();
803 if (ev->button == 1 && !_act_on_release) {
805 _action->activate ();
810 if (_fallthrough_to_parent)
817 ArdourButton::on_button_release_event (GdkEventButton *ev)
819 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
820 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
821 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
822 signal_led_clicked(ev); /* EMIT SIGNAL */
828 CairoWidget::set_dirty ();
830 if (ev->button == 1 && _hovering) {
832 if (_act_on_release) {
834 _action->activate ();
840 if (_fallthrough_to_parent)
847 ArdourButton::set_distinct_led_click (bool yn)
849 _distinct_led_click = yn;
854 ArdourButton::color_handler ()
856 _update_colors = true;
857 CairoWidget::set_dirty ();
861 ArdourButton::on_size_allocate (Allocation& alloc)
863 CairoWidget::on_size_allocate (alloc);
868 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
870 watch_connection.disconnect ();
871 binding_proxy.set_controllable (c);
875 ArdourButton::watch ()
877 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
880 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
883 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
887 ArdourButton::controllable_changed ()
889 float val = binding_proxy.get_controllable()->get_value();
891 if (fabs (val) >= 0.5f) {
892 set_active_state (Gtkmm2ext::ExplicitActive);
894 unset_active_state ();
896 CairoWidget::set_dirty ();
900 ArdourButton::set_related_action (RefPtr<Action> act)
902 Gtkmm2ext::Activatable::set_related_action (act);
906 action_tooltip_changed ();
908 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
911 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
914 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
915 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
916 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
921 ArdourButton::action_toggled ()
923 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
926 if (tact->get_active()) {
927 set_active_state (Gtkmm2ext::ExplicitActive);
929 unset_active_state ();
935 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
937 _update_colors = true;
938 CairoWidget::set_dirty ();
942 ArdourButton::on_name_changed ()
944 _char_pixel_width = 0;
945 _char_pixel_height = 0;
947 _update_colors = true;
954 ArdourButton::setup_led_rect ()
956 if (!(_elements & Indicator)) {
963 _led_rect = new cairo_rectangle_t;
966 if (_elements & Text) {
968 _led_rect->x = char_pixel_width();
970 _led_rect->x = get_width() - char_pixel_width() + _diameter;
974 _led_rect->x = .5 * get_width() - _diameter;
977 _led_rect->y = .5 * (get_height() - _diameter);
978 _led_rect->width = _diameter;
979 _led_rect->height = _diameter;
983 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
992 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
994 bool changed = (_active_state != s);
995 CairoWidget::set_active_state (s);
997 _update_colors = true;
998 CairoWidget::set_dirty ();
1003 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1005 bool changed = (_visual_state != s);
1006 CairoWidget::set_visual_state (s);
1008 _update_colors = true;
1009 CairoWidget::set_dirty ();
1014 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1017 CairoWidget::set_dirty ();
1018 return CairoWidget::on_focus_in_event (ev);
1022 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1025 CairoWidget::set_dirty ();
1026 return CairoWidget::on_focus_out_event (ev);
1030 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1032 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1036 _action->activate ();
1040 return CairoWidget::on_key_release_event (ev);
1044 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1046 _hovering = (_elements & Inactive) ? false : true;
1048 if (UIConfiguration::instance().get_widget_prelight()) {
1049 CairoWidget::set_dirty ();
1052 return CairoWidget::on_enter_notify_event (ev);
1056 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1060 if (UIConfiguration::instance().get_widget_prelight()) {
1061 CairoWidget::set_dirty ();
1064 return CairoWidget::on_leave_notify_event (ev);
1068 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1069 /* Our implicit grab due to a button_press was broken by another grab:
1070 * the button will not get any button_release event if the mouse leaves
1071 * while the grab is taken, so unpress ourselves */
1073 CairoWidget::set_dirty ();
1078 ArdourButton::set_tweaks (Tweaks t)
1082 if (is_realized()) {
1089 ArdourButton::action_sensitivity_changed ()
1091 if (_action->property_sensitive ()) {
1092 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1094 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1099 ArdourButton::set_layout_ellipsize_width (int w)
1101 if (_layout_ellipsize_width == w) {
1104 _layout_ellipsize_width = w;
1108 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1109 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1111 if (is_realized ()) {
1117 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1119 if (_ellipsis == e) {
1126 _layout->set_ellipsize(_ellipsis);
1127 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1128 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1130 if (is_realized ()) {
1136 ArdourButton::ensure_layout ()
1140 _layout = Pango::Layout::create (get_pango_context());
1141 _layout->set_ellipsize(_ellipsis);
1142 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1143 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1149 ArdourButton::recalc_char_pixel_geometry ()
1151 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1155 // NB. this is not static, since the geometry is different
1156 // depending on the font used.
1158 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1159 _layout->set_text (x);
1160 _layout->get_pixel_size (w, h);
1161 _char_pixel_height = std::max(4, h);
1162 // number of actual chars in the string (not bytes)
1163 // Glib to the rescue.
1164 Glib::ustring gx(x);
1165 _char_avg_pixel_width = w / (float)gx.size();
1166 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1167 _layout->set_text (_text);
1171 ArdourButton::action_visibility_changed ()
1173 if (_action->property_visible ()) {
1181 ArdourButton::action_tooltip_changed ()
1183 string str = _action->property_tooltip().get_value();
1184 set_tooltip (*this, str);
1188 ArdourButton::set_elements (Element e)
1191 CairoWidget::set_dirty ();
1195 ArdourButton::add_elements (Element e)
1197 _elements = (ArdourButton::Element) (_elements | e);
1198 CairoWidget::set_dirty ();
1202 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1205 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1206 CairoWidget::set_dirty ();
1210 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1212 if (led_custom_color == c && use_custom_led_color == useit) {
1216 led_custom_color = c;
1217 use_custom_led_color = useit;
1218 CairoWidget::set_dirty ();