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)
64 , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
65 , _tweaks (Tweaks (0))
66 , _char_pixel_width (0)
67 , _char_pixel_height (0)
68 , _char_avg_pixel_width (0)
72 , _corner_radius (3.5)
77 , fill_inactive_color (0)
78 , fill_active_color (0)
79 , text_active_color(0)
80 , text_inactive_color(0)
82 , led_inactive_color(0)
83 , led_custom_color (0)
84 , use_custom_led_color (false)
87 , led_inset_pattern (0)
89 , _act_on_release (true)
91 , _distinct_led_click (false)
94 , _fixed_colors_set (false)
95 , _fallthrough_to_parent (false)
96 , _layout_ellipsize_width (-1)
97 , _ellipsis (Pango::ELLIPSIZE_NONE)
98 , _update_colors (true)
101 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
102 /* This is not provided by gtkmm */
103 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
106 ArdourButton::ArdourButton (const std::string& str, Element e)
110 , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
111 , _tweaks (Tweaks (0))
112 , _char_pixel_width (0)
113 , _char_pixel_height (0)
114 , _char_avg_pixel_width (0)
118 , _corner_radius (3.5)
123 , fill_inactive_color (0)
124 , fill_active_color (0)
125 , text_active_color(0)
126 , text_inactive_color(0)
127 , led_active_color(0)
128 , led_inactive_color(0)
129 , led_custom_color (0)
130 , use_custom_led_color (false)
132 , concave_pattern (0)
133 , led_inset_pattern (0)
135 , _act_on_release (true)
137 , _distinct_led_click (false)
140 , _fixed_colors_set (false)
141 , _fallthrough_to_parent (false)
142 , _layout_ellipsize_width (-1)
143 , _ellipsis (Pango::ELLIPSIZE_NONE)
144 , _update_colors (true)
145 , _pattern_height (0)
148 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
149 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
150 /* This is not provided by gtkmm */
151 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
154 ArdourButton::~ArdourButton()
158 if (convex_pattern) {
159 cairo_pattern_destroy (convex_pattern);
162 if (concave_pattern) {
163 cairo_pattern_destroy (concave_pattern);
166 if (led_inset_pattern) {
167 cairo_pattern_destroy (led_inset_pattern);
172 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
176 _layout->set_font_description (fd);
178 _char_pixel_width = 0;
179 _char_pixel_height = 0;
184 ArdourButton::set_text_internal () {
187 _layout->set_markup (_text);
189 _layout->set_text (_text);
194 ArdourButton::set_text (const std::string& str, bool markup)
196 if (!(_elements & Text)) {
199 if (_text == str && _markup == markup) {
205 if (!is_realized()) {
209 if (_layout && _layout->get_text() != _text) {
210 set_text_internal ();
211 /* on_size_request() will fill in _text_width/height
212 * so queue it even if _sizing_text != "" */
218 ArdourButton::set_sizing_text (const std::string& str)
220 if (_sizing_text == str) {
224 if (!is_realized()) {
234 ArdourButton::set_angle (const double angle)
240 ArdourButton::set_alignment (const float xa, const float ya)
247 /* TODO make this a dedicated function elsewhere.
250 * virtual ArdourButton::render_vector_icon()
251 * ArdourIconButton::render_vector_icon
254 * ARDOUR_UI_UTILS::render_vector_icon()
257 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
262 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
264 if (_update_colors) {
267 if (get_height() != _pattern_height) {
271 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
272 text_color = text_active_color;
273 led_color = led_active_color;
275 text_color = text_inactive_color;
276 led_color = led_inactive_color;
279 if (use_custom_led_color) {
280 led_color = led_custom_color;
283 void (*rounded_function)(cairo_t*, double, double, double, double, double);
285 switch (_corner_mask) {
286 case 0x1: /* upper left only */
287 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
289 case 0x2: /* upper right only */
290 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
292 case 0x3: /* upper only */
293 rounded_function = Gtkmm2ext::rounded_top_rectangle;
295 /* should really have functions for lower right, lower left,
296 lower only, but for now, we don't
299 rounded_function = Gtkmm2ext::rounded_rectangle;
302 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
303 if ((_elements & (Body|Edge)) == (Body|Edge)) {
304 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
305 cairo_set_source_rgba (cr, 0, 0, 0, 1);
310 if ((_elements & Body)==Body) {
311 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
312 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
313 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
315 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
317 ArdourCanvas::set_source_rgba (cr, fill_active_color);
319 } else { //inactive, or it has an indicator
321 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
326 // IMPLICIT ACTIVE: draw a border of the active color
327 if ((_elements & Body)==Body) {
328 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
329 cairo_set_line_width (cr, 2.0);
330 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
331 ArdourCanvas::set_source_rgba (cr, fill_active_color);
336 //show the "convex" or "concave" gradient
337 if (!_flat_buttons && (_elements & Body)==Body) {
338 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
340 cairo_set_source (cr, concave_pattern);
341 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
344 cairo_set_source (cr, convex_pattern);
345 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
352 double x = rint((get_width() - _pixbuf->get_width()) * .5);
353 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
354 #if 0 // DEBUG style (print on hover)
355 if (_hovering || (_elements & Inactive)) {
356 printf("%s: p:%dx%d (%dx%d)\n",
358 _pixbuf->get_width(), _pixbuf->get_height(),
359 get_width(), get_height());
362 if (_elements & Menu) {
363 //if this is a DropDown with an icon, then we need to
364 //move the icon left slightly to accomomodate the arrow
367 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
368 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
371 else /* VectorIcons are exclusive to Pixbuf Icons */
372 if (_elements & VectorIcon) {
373 int vw = get_width();
374 int vh = get_height();
375 if (_elements & Menu) {
378 Gtkmm2ext::ArdourIcon::render (cr, _icon, vw, vh, active_state(), text_color);
381 const int text_margin = char_pixel_width();
383 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
385 #if 0 // DEBUG style (print on hover)
386 if (_hovering || (_elements & Inactive)) {
387 bool layout_font = true;
388 Pango::FontDescription fd = _layout->get_font_description();
389 if (fd.gobj() == NULL) {
391 fd = get_pango_context()->get_font_description();
393 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
395 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
396 ceil(char_pixel_height() * BASELINESTRETCH),
397 _text_width, _text_height,
398 get_width(), get_height(),
399 layout_font ? "L:" : "W:",
400 fd.to_string().c_str());
405 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
409 ArdourCanvas::set_source_rgba (cr, text_color);
410 const double text_ypos = (get_height() - _text_height) * .5;
412 if (_elements & Menu) {
413 // always left align (dropdown)
414 cairo_move_to (cr, text_margin, text_ypos);
415 pango_cairo_show_layout (cr, _layout->gobj());
416 } else if ( (_elements & Indicator) == Indicator) {
417 // left/right align depending on LED position
419 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
421 cairo_move_to (cr, text_margin, text_ypos);
423 pango_cairo_show_layout (cr, _layout->gobj());
425 /* centered text otherwise */
432 cairo_rotate(cr, _angle * M_PI / 180.0);
433 cairo_device_to_user(cr, &ww, &wh);
434 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
435 ya = (wh - _text_height) * _yalign;
437 /* quick hack for left/bottom alignment at -90deg
438 * TODO this should be generalized incl rotation.
439 * currently only 'user' of this API is meter_strip.cc
441 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
443 cairo_move_to (cr, xa, ya);
444 pango_cairo_update_layout(cr, _layout->gobj());
445 pango_cairo_show_layout (cr, _layout->gobj());
452 if (_elements & Menu) {
453 const float trih = ceil(_diameter * .5);
454 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
456 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
457 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
458 cairo_rel_line_to(cr, -triw2, -trih);
459 cairo_rel_line_to(cr, 2. * triw2, 0);
460 cairo_close_path(cr);
462 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
465 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
466 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
467 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
468 cairo_close_path(cr);
469 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
470 cairo_set_line_width(cr, 1);
475 if (_elements & Indicator) {
478 /* move to the center of the indicator/led */
479 if (_elements & Text) {
480 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
482 cairo_translate (cr, led_xoff, get_height() * .5);
484 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
487 cairo_translate (cr, get_width() * .5, get_height() * .5);
491 if (!_flat_buttons) {
492 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
493 cairo_set_source (cr, led_inset_pattern);
498 cairo_set_source_rgb (cr, 0, 0, 0);
499 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
503 ArdourCanvas::set_source_rgba (cr, led_color);
504 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
510 // a transparent overlay to indicate insensitivity
511 if ((visual_state() & Gtkmm2ext::Insensitive)) {
512 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
513 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
514 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
518 // if requested, show hovering
519 if (UIConfiguration::instance().get_widget_prelight()
520 && !((visual_state() & Gtkmm2ext::Insensitive))) {
522 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
523 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
528 //user is currently pressing the button. dark outline helps to indicate this
529 if (_grabbed && !(_elements & (Inactive|Menu))) {
530 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
531 cairo_set_line_width(cr, 2);
532 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
536 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
537 if (visual_state() & Gtkmm2ext::Selected) {
538 cairo_set_line_width(cr, 1);
539 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
540 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
544 //I guess this means we have keyboard focus. I don't think this works currently
546 //A: yes, it's keyboard focus and it does work when there's no editor window
547 // (the editor is always the first receiver for KeyDown).
548 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
550 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
551 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
553 cairo_set_dash (cr, &dashes, 1, 0);
554 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
555 cairo_set_line_width (cr, 1.0);
557 cairo_set_dash (cr, 0, 0, 0);
562 ArdourButton::set_corner_radius (float r)
565 CairoWidget::set_dirty ();
569 ArdourButton::on_realize()
571 CairoWidget::on_realize ();
574 if (_layout->get_text() != _text) {
575 set_text_internal ();
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 = rintf (11.f * UIConfiguration::instance().get_ui_scale());
589 if (_diameter != newdia) {
595 if (_elements & Text) {
598 set_text_internal ();
600 /* render() needs the size of the displayed text */
601 _layout->get_pixel_size (_text_width, _text_height);
603 if (_tweaks & OccasionalText) {
605 /* size should not change based on presence or absence
609 } else { //if (!_text.empty() || !_sizing_text.empty()) {
611 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
612 req->width += rint(1.75 * char_pixel_width()); // padding
614 if (!_sizing_text.empty()) {
615 _layout->set_text (_sizing_text); /* use sizing text */
618 int sizing_text_width = 0, sizing_text_height = 0;
619 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
621 req->width += sizing_text_width;
623 if (!_sizing_text.empty()) {
624 set_text_internal (); /* restore display text */
628 /* XXX hack (surprise). Deal with two common rotation angles */
630 if (_angle == 90 || _angle == 270) {
631 /* do not swap text width or height because we rely on
632 these being the un-rotated values in ::render()
634 swap (req->width, req->height);
643 req->width += _pixbuf->get_width() + char_pixel_width();
644 req->height = std::max(req->height, _pixbuf->get_height() + 4);
647 if (_elements & Indicator) {
648 req->width += lrint (_diameter) + char_pixel_width();
649 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
652 if ((_elements & Menu)) {
653 req->width += _diameter + 4;
656 if (_elements & VectorIcon) {
657 assert(!(_elements & Text));
658 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
660 req->height = std::max(req->height, wh);
663 /* Tweaks to mess the nice stuff above up again. */
664 if (_tweaks & TrackHeader) {
665 // forget everything above and just use a fixed square [em] size
666 // "TrackHeader Buttons" are single letter (usually uppercase)
667 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
668 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
672 else if (_tweaks & Square) {
673 // currerntly unused (again)
674 if (req->width < req->height)
675 req->width = req->height;
676 if (req->height < req->width)
677 req->height = req->width;
678 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
679 // properly centered text for those elements that are centered
680 // (no sub-pixel offset)
681 if ((req->width - _text_width) & 1) { ++req->width; }
682 if ((req->height - _text_height) & 1) { ++req->height; }
685 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
690 * This sets the colors used for rendering based on the name of the button, and
691 * thus uses information from the GUI config data.
694 ArdourButton::set_colors ()
696 _update_colors = false;
698 if (_fixed_colors_set == 0x3) {
702 std::string name = get_name();
705 if (!(_fixed_colors_set & 0x1)) {
706 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
708 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
712 if (!(_fixed_colors_set & 0x2)) {
713 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
715 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
719 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
720 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
722 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
724 led_active_color = UIConfiguration::instance().color ("generic button: led active");
727 /* The inactive color for the LED is just a fairly dark version of the
731 ArdourCanvas::HSV inactive (led_active_color);
734 led_inactive_color = inactive.color ();
738 * This sets the colors used for rendering based on two fixed values, rather
739 * than basing them on the button name, and thus information in the GUI config
742 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
744 set_active_color (color_active);
745 set_inactive_color (color_inactive);
748 void ArdourButton::set_active_color (const uint32_t color)
750 _fixed_colors_set |= 0x1;
752 fill_active_color = color;
754 unsigned char r, g, b, a;
755 UINT_TO_RGBA(color, &r, &g, &b, &a);
757 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
758 (max (double(g), 255.) - min (double(g), 255.)) +
759 (max (double(b), 255.) - min (double(b), 255.));
761 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
762 (max (double(g), 0.) - min (double(g), 0.)) +
763 (max (double(b), 0.) - min (double(b), 0.));
765 text_active_color = (white_contrast > black_contrast) ?
766 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
767 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
769 /* XXX what about led colors ? */
770 CairoWidget::set_dirty ();
773 void ArdourButton::set_inactive_color (const uint32_t color)
775 _fixed_colors_set |= 0x2;
777 fill_inactive_color = color;
779 unsigned char r, g, b, a;
780 UINT_TO_RGBA(color, &r, &g, &b, &a);
782 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
783 (max (double(g), 255.) - min (double(g), 255.)) +
784 (max (double(b), 255.) - min (double(b), 255.));
786 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
787 (max (double(g), 0.) - min (double(g), 0.)) +
788 (max (double(b), 0.) - min (double(b), 0.));
790 text_inactive_color = (white_contrast > black_contrast) ?
791 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
792 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
794 /* XXX what about led colors ? */
795 CairoWidget::set_dirty ();
799 ArdourButton::build_patterns ()
801 if (convex_pattern) {
802 cairo_pattern_destroy (convex_pattern);
806 if (concave_pattern) {
807 cairo_pattern_destroy (concave_pattern);
811 if (led_inset_pattern) {
812 cairo_pattern_destroy (led_inset_pattern);
813 led_inset_pattern = 0;
817 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
818 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
819 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
822 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
823 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
824 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
826 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
827 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
828 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
830 _pattern_height = get_height() ;
834 ArdourButton::set_led_left (bool yn)
840 ArdourButton::on_button_press_event (GdkEventButton *ev)
842 focus_handler (this);
844 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
845 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
846 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
851 if (binding_proxy.button_press_handler (ev)) {
856 CairoWidget::set_dirty ();
858 if (ev->button == 1 && !_act_on_release) {
860 _action->activate ();
865 if (_fallthrough_to_parent)
872 ArdourButton::on_button_release_event (GdkEventButton *ev)
874 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
875 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
876 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
877 signal_led_clicked(ev); /* EMIT SIGNAL */
883 CairoWidget::set_dirty ();
885 if (ev->button == 1 && _hovering) {
887 if (_act_on_release) {
889 _action->activate ();
895 if (_fallthrough_to_parent)
902 ArdourButton::set_distinct_led_click (bool yn)
904 _distinct_led_click = yn;
909 ArdourButton::color_handler ()
911 _update_colors = true;
912 CairoWidget::set_dirty ();
916 ArdourButton::on_size_allocate (Allocation& alloc)
918 CairoWidget::on_size_allocate (alloc);
923 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
925 watch_connection.disconnect ();
926 binding_proxy.set_controllable (c);
930 ArdourButton::watch ()
932 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
935 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
938 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
942 ArdourButton::controllable_changed ()
944 float val = binding_proxy.get_controllable()->get_value();
946 if (fabs (val) >= 0.5f) {
947 set_active_state (Gtkmm2ext::ExplicitActive);
949 unset_active_state ();
951 CairoWidget::set_dirty ();
955 ArdourButton::set_related_action (RefPtr<Action> act)
957 Gtkmm2ext::Activatable::set_related_action (act);
961 action_tooltip_changed ();
963 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
966 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
969 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
970 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
971 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
976 ArdourButton::action_toggled ()
978 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
981 if (tact->get_active()) {
982 set_active_state (Gtkmm2ext::ExplicitActive);
984 unset_active_state ();
990 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
992 _update_colors = true;
993 CairoWidget::set_dirty ();
994 _char_pixel_width = 0;
995 _char_pixel_height = 0;
1002 ArdourButton::on_name_changed ()
1004 _char_pixel_width = 0;
1005 _char_pixel_height = 0;
1007 _update_colors = true;
1008 if (is_realized()) {
1014 ArdourButton::setup_led_rect ()
1016 if (!(_elements & Indicator)) {
1023 _led_rect = new cairo_rectangle_t;
1026 if (_elements & Text) {
1028 _led_rect->x = char_pixel_width();
1030 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1034 _led_rect->x = .5 * get_width() - _diameter;
1037 _led_rect->y = .5 * (get_height() - _diameter);
1038 _led_rect->width = _diameter;
1039 _led_rect->height = _diameter;
1043 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1045 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1047 if (is_realized()) {
1053 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1055 bool changed = (_active_state != s);
1056 CairoWidget::set_active_state (s);
1058 _update_colors = true;
1059 CairoWidget::set_dirty ();
1064 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1066 bool changed = (_visual_state != s);
1067 CairoWidget::set_visual_state (s);
1069 _update_colors = true;
1070 CairoWidget::set_dirty ();
1075 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1078 CairoWidget::set_dirty ();
1079 return CairoWidget::on_focus_in_event (ev);
1083 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1086 CairoWidget::set_dirty ();
1087 return CairoWidget::on_focus_out_event (ev);
1091 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1093 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1097 _action->activate ();
1101 return CairoWidget::on_key_release_event (ev);
1105 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1107 _hovering = (_elements & Inactive) ? false : true;
1109 if (UIConfiguration::instance().get_widget_prelight()) {
1110 CairoWidget::set_dirty ();
1113 return CairoWidget::on_enter_notify_event (ev);
1117 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1121 if (UIConfiguration::instance().get_widget_prelight()) {
1122 CairoWidget::set_dirty ();
1125 return CairoWidget::on_leave_notify_event (ev);
1129 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1130 /* Our implicit grab due to a button_press was broken by another grab:
1131 * the button will not get any button_release event if the mouse leaves
1132 * while the grab is taken, so unpress ourselves */
1134 CairoWidget::set_dirty ();
1139 ArdourButton::set_tweaks (Tweaks t)
1143 if (is_realized()) {
1150 ArdourButton::action_sensitivity_changed ()
1152 if (_action->property_sensitive ()) {
1153 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1155 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1160 ArdourButton::set_layout_ellipsize_width (int w)
1162 if (_layout_ellipsize_width == w) {
1165 _layout_ellipsize_width = w;
1169 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1170 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1172 if (is_realized ()) {
1178 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1180 if (_ellipsis == e) {
1187 _layout->set_ellipsize(_ellipsis);
1188 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1189 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1191 if (is_realized ()) {
1197 ArdourButton::ensure_layout ()
1201 _layout = Pango::Layout::create (get_pango_context());
1202 _layout->set_ellipsize(_ellipsis);
1203 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1204 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1210 ArdourButton::recalc_char_pixel_geometry ()
1212 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1216 // NB. this is not static, since the geometry is different
1217 // depending on the font used.
1219 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1220 _layout->set_text (x);
1221 _layout->get_pixel_size (w, h);
1222 _char_pixel_height = std::max(4, h);
1223 // number of actual chars in the string (not bytes)
1224 // Glib to the rescue.
1225 Glib::ustring gx(x);
1226 _char_avg_pixel_width = w / (float)gx.size();
1227 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1228 set_text_internal (); /* restore display text */
1232 ArdourButton::action_visibility_changed ()
1234 if (_action->property_visible ()) {
1242 ArdourButton::action_tooltip_changed ()
1244 string str = _action->property_tooltip().get_value();
1245 set_tooltip (*this, str);
1249 ArdourButton::set_elements (Element e)
1252 CairoWidget::set_dirty ();
1256 ArdourButton::add_elements (Element e)
1258 _elements = (ArdourButton::Element) (_elements | e);
1259 CairoWidget::set_dirty ();
1263 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1266 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1267 CairoWidget::set_dirty ();
1271 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1273 if (led_custom_color == c && use_custom_led_color == useit) {
1277 led_custom_color = c;
1278 use_custom_led_color = useit;
1279 CairoWidget::set_dirty ();