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)
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)
107 , _tweaks (Tweaks (0))
108 , _char_pixel_width (0)
109 , _char_pixel_height (0)
110 , _char_avg_pixel_width (0)
114 , _corner_radius (2.5)
119 , fill_inactive_color (0)
120 , fill_active_color (0)
121 , text_active_color(0)
122 , text_inactive_color(0)
123 , led_active_color(0)
124 , led_inactive_color(0)
125 , led_custom_color (0)
126 , use_custom_led_color (false)
128 , concave_pattern (0)
129 , led_inset_pattern (0)
131 , _act_on_release (true)
133 , _distinct_led_click (false)
136 , _fixed_colors_set (false)
137 , _fallthrough_to_parent (false)
138 , _layout_ellipsize_width (-1)
139 , _ellipsis (Pango::ELLIPSIZE_NONE)
140 , _update_colors (true)
141 , _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);
179 ArdourButton::set_text (const std::string& str)
185 if (!is_realized()) {
189 if (_layout && _layout->get_text() != _text) {
190 _layout->set_text (_text);
191 /* on_size_request() will fill in _text_width/height
192 * so queue it even if _sizing_text != "" */
198 ArdourButton::set_sizing_text (const std::string& str)
200 if (_sizing_text == str) {
204 if (!is_realized()) {
214 ArdourButton::set_angle (const double angle)
220 ArdourButton::set_alignment (const float xa, const float ya)
227 /* TODO make this a dedicated function elsewhere.
230 * virtual ArdourButton::render_vector_icon()
231 * ArdourIconButton::render_vector_icon
234 * ARDOUR_UI_UTILS::render_vector_icon()
237 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
242 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
244 if (_update_colors) {
247 if (get_height() != _pattern_height) {
251 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
252 text_color = text_active_color;
253 led_color = led_active_color;
255 text_color = text_inactive_color;
256 led_color = led_inactive_color;
259 if (use_custom_led_color) {
260 led_color = led_custom_color;
263 void (*rounded_function)(cairo_t*, double, double, double, double, double);
265 switch (_corner_mask) {
266 case 0x1: /* upper left only */
267 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
269 case 0x2: /* upper right only */
270 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
272 case 0x3: /* upper only */
273 rounded_function = Gtkmm2ext::rounded_top_rectangle;
275 /* should really have functions for lower right, lower left,
276 lower only, but for now, we don't
279 rounded_function = Gtkmm2ext::rounded_rectangle;
282 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
283 if ((_elements & (Body|Edge)) == (Body|Edge)) {
284 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
285 cairo_set_source_rgba (cr, 0, 0, 0, 1);
290 if ((_elements & Body)==Body) {
291 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
292 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
293 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
295 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
297 ArdourCanvas::set_source_rgba (cr, fill_active_color);
299 } else { //inactive, or it has an indicator
301 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
306 // IMPLICIT ACTIVE: draw a border of the active color
307 if ((_elements & Body)==Body) {
308 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
309 cairo_set_line_width (cr, 2.0);
310 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
311 ArdourCanvas::set_source_rgba (cr, fill_active_color);
316 //show the "convex" or "concave" gradient
317 if (!_flat_buttons && (_elements & Body)==Body) {
318 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
320 cairo_set_source (cr, concave_pattern);
321 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
324 cairo_set_source (cr, convex_pattern);
325 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
332 double x = rint((get_width() - _pixbuf->get_width()) * .5);
333 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
334 #if 0 // DEBUG style (print on hover)
335 if (_hovering || (_elements & Inactive)) {
336 printf("%s: p:%dx%d (%dx%d)\n",
338 _pixbuf->get_width(), _pixbuf->get_height(),
339 get_width(), get_height());
342 if (_elements & Menu) {
343 //if this is a DropDown with an icon, then we need to
344 //move the icon left slightly to accomomodate the arrow
347 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
348 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
351 else /* VectorIcons are exclusive to Pixbuf Icons */
352 if (_elements & VectorIcon) {
353 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
356 const int text_margin = char_pixel_width();
358 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
360 #if 0 // DEBUG style (print on hover)
361 if (_hovering || (_elements & Inactive)) {
362 bool layout_font = true;
363 Pango::FontDescription fd = _layout->get_font_description();
364 if (fd.gobj() == NULL) {
366 fd = get_pango_context()->get_font_description();
368 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
370 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
371 ceil(char_pixel_height() * BASELINESTRETCH),
372 _text_width, _text_height,
373 get_width(), get_height(),
374 layout_font ? "L:" : "W:",
375 fd.to_string().c_str());
380 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
384 ArdourCanvas::set_source_rgba (cr, text_color);
385 const double text_ypos = (get_height() - _text_height) * .5;
387 if (_elements & Menu) {
388 // always left align (dropdown)
389 cairo_move_to (cr, text_margin, text_ypos);
390 pango_cairo_show_layout (cr, _layout->gobj());
391 } else if ( (_elements & Indicator) == Indicator) {
392 // left/right align depending on LED position
394 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
396 cairo_move_to (cr, text_margin, text_ypos);
398 pango_cairo_show_layout (cr, _layout->gobj());
400 /* centered text otherwise */
407 cairo_rotate(cr, _angle * M_PI / 180.0);
408 cairo_device_to_user(cr, &ww, &wh);
409 xa = (ww - _text_width) * _xalign;
410 ya = (wh - _text_height) * _yalign;
412 /* quick hack for left/bottom alignment at -90deg
413 * TODO this should be generalized incl rotation.
414 * currently only 'user' of this API is meter_strip.cc
416 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
418 cairo_move_to (cr, xa, ya);
419 pango_cairo_update_layout(cr, _layout->gobj());
420 pango_cairo_show_layout (cr, _layout->gobj());
427 if (_elements & Menu) {
428 const float trih = ceil(_diameter * .5);
429 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
431 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
432 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
433 cairo_rel_line_to(cr, -triw2, -trih);
434 cairo_rel_line_to(cr, 2. * triw2, 0);
435 cairo_close_path(cr);
437 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
440 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
441 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
442 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
443 cairo_close_path(cr);
444 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
445 cairo_set_line_width(cr, 1);
450 if (_elements & Indicator) {
453 /* move to the center of the indicator/led */
454 if (_elements & Text) {
455 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
457 cairo_translate (cr, led_xoff, get_height() * .5);
459 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
462 cairo_translate (cr, get_width() * .5, get_height() * .5);
466 if (!_flat_buttons) {
467 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
468 cairo_set_source (cr, led_inset_pattern);
473 cairo_set_source_rgb (cr, 0, 0, 0);
474 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
478 ArdourCanvas::set_source_rgba (cr, led_color);
479 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
485 // a transparent overlay to indicate insensitivity
486 if ((visual_state() & Gtkmm2ext::Insensitive)) {
487 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
488 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
489 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
493 // if requested, show hovering
494 if (UIConfiguration::instance().get_widget_prelight()
495 && !((visual_state() & Gtkmm2ext::Insensitive))) {
497 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
498 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
503 //user is currently pressing the button. dark outline helps to indicate this
504 if (_grabbed && !(_elements & (Inactive|Menu))) {
505 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
506 cairo_set_line_width(cr, 2);
507 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
511 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
512 if (visual_state() & Gtkmm2ext::Selected) {
513 cairo_set_line_width(cr, 1);
514 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
515 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
519 //I guess this means we have keyboard focus. I don't think this works currently
521 //A: yes, it's keyboard focus and it does work when there's no editor window
522 // (the editor is always the first receiver for KeyDown).
523 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
525 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
526 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
528 cairo_set_dash (cr, &dashes, 1, 0);
529 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
530 cairo_set_line_width (cr, 1.0);
532 cairo_set_dash (cr, 0, 0, 0);
537 ArdourButton::set_corner_radius (float r)
540 CairoWidget::set_dirty ();
544 ArdourButton::on_realize()
546 CairoWidget::on_realize ();
549 if (_layout->get_text() != _text) {
550 _layout->set_text (_text);
557 ArdourButton::on_size_request (Gtk::Requisition* req)
559 req->width = req->height = 0;
560 CairoWidget::on_size_request (req);
562 if (_diameter == 0) {
563 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
564 if (_diameter != newdia) {
570 if (_elements & Text) {
573 _layout->set_text (_text);
574 /* render() needs the size of the displayed text */
575 _layout->get_pixel_size (_text_width, _text_height);
577 if (_tweaks & OccasionalText) {
579 /* size should not change based on presence or absence
583 } else { //if (!_text.empty() || !_sizing_text.empty()) {
585 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
586 req->width += rint(1.75 * char_pixel_width()); // padding
588 if (!_sizing_text.empty()) {
589 _layout->set_text (_sizing_text); /* use sizing text */
592 int sizing_text_width = 0, sizing_text_height = 0;
593 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
595 req->width += sizing_text_width;
597 if (!_sizing_text.empty()) {
598 _layout->set_text (_text); /* restore display text */
602 /* XXX hack (surprise). Deal with two common rotation angles */
604 if (_angle == 90 || _angle == 270) {
605 /* do not swap text width or height because we rely on
606 these being the un-rotated values in ::render()
608 swap (req->width, req->height);
617 req->width += _pixbuf->get_width() + char_pixel_width();
618 req->height = std::max(req->height, _pixbuf->get_height() + 4);
621 if (_elements & Indicator) {
622 req->width += lrint (_diameter) + char_pixel_width();
623 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
626 if ((_elements & Menu)) {
627 req->width += _diameter + 4;
630 if (_elements & VectorIcon) {
631 assert(!(_elements & Text));
632 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
634 req->height = std::max(req->height, wh);
637 /* Tweaks to mess the nice stuff above up again. */
638 if (_tweaks & TrackHeader) {
639 // forget everything above and just use a fixed square [em] size
640 // "TrackHeader Buttons" are single letter (usually uppercase)
641 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
642 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
646 else if (_tweaks & Square) {
647 // currerntly unused (again)
648 if (req->width < req->height)
649 req->width = req->height;
650 if (req->height < req->width)
651 req->height = req->width;
652 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
653 // properly centered text for those elements that are centered
654 // (no sub-pixel offset)
655 if ((req->width - _text_width) & 1) { ++req->width; }
656 if ((req->height - _text_height) & 1) { ++req->height; }
659 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
664 * This sets the colors used for rendering based on the name of the button, and
665 * thus uses information from the GUI config data.
668 ArdourButton::set_colors ()
670 _update_colors = false;
672 if (_fixed_colors_set == 0x3) {
676 std::string name = get_name();
679 if (!(_fixed_colors_set & 0x1)) {
680 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
682 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
686 if (!(_fixed_colors_set & 0x2)) {
687 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
689 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
693 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
694 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
696 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
698 led_active_color = UIConfiguration::instance().color ("generic button: led active");
701 /* The inactive color for the LED is just a fairly dark version of the
705 ArdourCanvas::HSV inactive (led_active_color);
708 led_inactive_color = inactive.color ();
712 * This sets the colors used for rendering based on two fixed values, rather
713 * than basing them on the button name, and thus information in the GUI config
716 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
718 set_active_color (color_active);
719 set_inactive_color (color_inactive);
722 void ArdourButton::set_active_color (const uint32_t color)
724 _fixed_colors_set |= 0x1;
726 fill_active_color = color;
728 unsigned char r, g, b, a;
729 UINT_TO_RGBA(color, &r, &g, &b, &a);
731 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
732 (max (double(g), 255.) - min (double(g), 255.)) +
733 (max (double(b), 255.) - min (double(b), 255.));
735 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
736 (max (double(g), 0.) - min (double(g), 0.)) +
737 (max (double(b), 0.) - min (double(b), 0.));
739 text_active_color = (white_contrast > black_contrast) ?
740 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
741 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
743 /* XXX what about led colors ? */
744 CairoWidget::set_dirty ();
747 void ArdourButton::set_inactive_color (const uint32_t color)
749 _fixed_colors_set |= 0x2;
751 fill_inactive_color = color;
753 unsigned char r, g, b, a;
754 UINT_TO_RGBA(color, &r, &g, &b, &a);
756 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
757 (max (double(g), 255.) - min (double(g), 255.)) +
758 (max (double(b), 255.) - min (double(b), 255.));
760 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
761 (max (double(g), 0.) - min (double(g), 0.)) +
762 (max (double(b), 0.) - min (double(b), 0.));
764 text_inactive_color = (white_contrast > black_contrast) ?
765 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
766 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
768 /* XXX what about led colors ? */
769 CairoWidget::set_dirty ();
773 ArdourButton::build_patterns ()
775 if (convex_pattern) {
776 cairo_pattern_destroy (convex_pattern);
780 if (concave_pattern) {
781 cairo_pattern_destroy (concave_pattern);
785 if (led_inset_pattern) {
786 cairo_pattern_destroy (led_inset_pattern);
787 led_inset_pattern = 0;
791 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
792 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
793 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
796 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
797 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
798 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
800 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
801 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
802 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
804 _pattern_height = get_height() ;
808 ArdourButton::set_led_left (bool yn)
814 ArdourButton::on_button_press_event (GdkEventButton *ev)
816 focus_handler (this);
818 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
819 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
820 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
825 if (binding_proxy.button_press_handler (ev)) {
830 CairoWidget::set_dirty ();
832 if (ev->button == 1 && !_act_on_release) {
834 _action->activate ();
839 if (_fallthrough_to_parent)
846 ArdourButton::on_button_release_event (GdkEventButton *ev)
848 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
849 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
850 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
851 signal_led_clicked(ev); /* EMIT SIGNAL */
857 CairoWidget::set_dirty ();
859 if (ev->button == 1 && _hovering) {
861 if (_act_on_release) {
863 _action->activate ();
869 if (_fallthrough_to_parent)
876 ArdourButton::set_distinct_led_click (bool yn)
878 _distinct_led_click = yn;
883 ArdourButton::color_handler ()
885 _update_colors = true;
886 CairoWidget::set_dirty ();
890 ArdourButton::on_size_allocate (Allocation& alloc)
892 CairoWidget::on_size_allocate (alloc);
897 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
899 watch_connection.disconnect ();
900 binding_proxy.set_controllable (c);
904 ArdourButton::watch ()
906 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
909 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
912 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
916 ArdourButton::controllable_changed ()
918 float val = binding_proxy.get_controllable()->get_value();
920 if (fabs (val) >= 0.5f) {
921 set_active_state (Gtkmm2ext::ExplicitActive);
923 unset_active_state ();
925 CairoWidget::set_dirty ();
929 ArdourButton::set_related_action (RefPtr<Action> act)
931 Gtkmm2ext::Activatable::set_related_action (act);
935 action_tooltip_changed ();
937 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
940 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
943 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
944 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
945 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
950 ArdourButton::action_toggled ()
952 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
955 if (tact->get_active()) {
956 set_active_state (Gtkmm2ext::ExplicitActive);
958 unset_active_state ();
964 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
966 _update_colors = true;
967 CairoWidget::set_dirty ();
968 _char_pixel_width = 0;
969 _char_pixel_height = 0;
976 ArdourButton::on_name_changed ()
978 _char_pixel_width = 0;
979 _char_pixel_height = 0;
981 _update_colors = true;
988 ArdourButton::setup_led_rect ()
990 if (!(_elements & Indicator)) {
997 _led_rect = new cairo_rectangle_t;
1000 if (_elements & Text) {
1002 _led_rect->x = char_pixel_width();
1004 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1008 _led_rect->x = .5 * get_width() - _diameter;
1011 _led_rect->y = .5 * (get_height() - _diameter);
1012 _led_rect->width = _diameter;
1013 _led_rect->height = _diameter;
1017 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1020 if (is_realized()) {
1026 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1028 bool changed = (_active_state != s);
1029 CairoWidget::set_active_state (s);
1031 _update_colors = true;
1032 CairoWidget::set_dirty ();
1037 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1039 bool changed = (_visual_state != s);
1040 CairoWidget::set_visual_state (s);
1042 _update_colors = true;
1043 CairoWidget::set_dirty ();
1048 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1051 CairoWidget::set_dirty ();
1052 return CairoWidget::on_focus_in_event (ev);
1056 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1059 CairoWidget::set_dirty ();
1060 return CairoWidget::on_focus_out_event (ev);
1064 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1066 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1070 _action->activate ();
1074 return CairoWidget::on_key_release_event (ev);
1078 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1080 _hovering = (_elements & Inactive) ? false : true;
1082 if (UIConfiguration::instance().get_widget_prelight()) {
1083 CairoWidget::set_dirty ();
1086 return CairoWidget::on_enter_notify_event (ev);
1090 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1094 if (UIConfiguration::instance().get_widget_prelight()) {
1095 CairoWidget::set_dirty ();
1098 return CairoWidget::on_leave_notify_event (ev);
1102 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1103 /* Our implicit grab due to a button_press was broken by another grab:
1104 * the button will not get any button_release event if the mouse leaves
1105 * while the grab is taken, so unpress ourselves */
1107 CairoWidget::set_dirty ();
1112 ArdourButton::set_tweaks (Tweaks t)
1116 if (is_realized()) {
1123 ArdourButton::action_sensitivity_changed ()
1125 if (_action->property_sensitive ()) {
1126 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1128 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1133 ArdourButton::set_layout_ellipsize_width (int w)
1135 if (_layout_ellipsize_width == w) {
1138 _layout_ellipsize_width = w;
1142 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1143 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1145 if (is_realized ()) {
1151 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1153 if (_ellipsis == e) {
1160 _layout->set_ellipsize(_ellipsis);
1161 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1162 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1164 if (is_realized ()) {
1170 ArdourButton::ensure_layout ()
1174 _layout = Pango::Layout::create (get_pango_context());
1175 _layout->set_ellipsize(_ellipsis);
1176 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1177 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1183 ArdourButton::recalc_char_pixel_geometry ()
1185 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1189 // NB. this is not static, since the geometry is different
1190 // depending on the font used.
1192 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1193 _layout->set_text (x);
1194 _layout->get_pixel_size (w, h);
1195 _char_pixel_height = std::max(4, h);
1196 // number of actual chars in the string (not bytes)
1197 // Glib to the rescue.
1198 Glib::ustring gx(x);
1199 _char_avg_pixel_width = w / (float)gx.size();
1200 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1201 _layout->set_text (_text);
1205 ArdourButton::action_visibility_changed ()
1207 if (_action->property_visible ()) {
1215 ArdourButton::action_tooltip_changed ()
1217 string str = _action->property_tooltip().get_value();
1218 set_tooltip (*this, str);
1222 ArdourButton::set_elements (Element e)
1225 CairoWidget::set_dirty ();
1229 ArdourButton::add_elements (Element e)
1231 _elements = (ArdourButton::Element) (_elements | e);
1232 CairoWidget::set_dirty ();
1236 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1239 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1240 CairoWidget::set_dirty ();
1244 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1246 if (led_custom_color == c && use_custom_led_color == useit) {
1250 led_custom_color = c;
1251 use_custom_led_color = useit;
1252 CairoWidget::set_dirty ();