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_get_matrix (cr, &m1);
433 cairo_matrix_t m2 = m1;
436 cairo_set_matrix (cr, &m2);
439 cairo_rotate(cr, _angle * M_PI / 180.0);
442 cairo_device_to_user(cr, &ww, &wh);
443 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
444 ya = (wh - _text_height) * _yalign;
446 /* quick hack for left/bottom alignment at -90deg
447 * TODO this should be generalized incl rotation.
448 * currently only 'user' of this API is meter_strip.cc
450 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
452 cairo_move_to (cr, xa + m1.x0, ya + m1.y0);
453 pango_cairo_update_layout(cr, _layout->gobj());
454 pango_cairo_show_layout (cr, _layout->gobj());
460 if (_elements & Menu) {
461 const float trih = ceil(_diameter * .5);
462 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
464 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, -triw2, -trih);
467 cairo_rel_line_to(cr, 2. * triw2, 0);
468 cairo_close_path(cr);
470 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
473 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
474 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
475 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
476 cairo_close_path(cr);
477 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
478 cairo_set_line_width(cr, 1);
483 if (_elements & Indicator) {
486 /* move to the center of the indicator/led */
487 if (_elements & Text) {
488 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
490 cairo_translate (cr, led_xoff, get_height() * .5);
492 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
495 cairo_translate (cr, get_width() * .5, get_height() * .5);
499 if (!_flat_buttons) {
500 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
501 cairo_set_source (cr, led_inset_pattern);
506 cairo_set_source_rgb (cr, 0, 0, 0);
507 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
511 ArdourCanvas::set_source_rgba (cr, led_color);
512 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
518 // a transparent overlay to indicate insensitivity
519 if ((visual_state() & Gtkmm2ext::Insensitive)) {
520 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
521 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
522 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
526 // if requested, show hovering
527 if (UIConfiguration::instance().get_widget_prelight()
528 && !((visual_state() & Gtkmm2ext::Insensitive))) {
530 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
531 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
536 //user is currently pressing the button. dark outline helps to indicate this
537 if (_grabbed && !(_elements & (Inactive|Menu))) {
538 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
539 cairo_set_line_width(cr, 2);
540 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
544 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
545 if (visual_state() & Gtkmm2ext::Selected) {
546 cairo_set_line_width(cr, 1);
547 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
548 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
552 //I guess this means we have keyboard focus. I don't think this works currently
554 //A: yes, it's keyboard focus and it does work when there's no editor window
555 // (the editor is always the first receiver for KeyDown).
556 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
558 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
559 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
561 cairo_set_dash (cr, &dashes, 1, 0);
562 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
563 cairo_set_line_width (cr, 1.0);
565 cairo_set_dash (cr, 0, 0, 0);
570 ArdourButton::set_corner_radius (float r)
573 CairoWidget::set_dirty ();
577 ArdourButton::on_realize()
579 CairoWidget::on_realize ();
582 if (_layout->get_text() != _text) {
583 set_text_internal ();
590 ArdourButton::on_size_request (Gtk::Requisition* req)
592 req->width = req->height = 0;
593 CairoWidget::on_size_request (req);
595 if (_diameter == 0) {
596 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
597 if (_diameter != newdia) {
603 if (_elements & Text) {
606 set_text_internal ();
608 /* render() needs the size of the displayed text */
609 _layout->get_pixel_size (_text_width, _text_height);
611 if (_tweaks & OccasionalText) {
613 /* size should not change based on presence or absence
617 } else { //if (!_text.empty() || !_sizing_text.empty()) {
619 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
620 req->width += rint(1.75 * char_pixel_width()); // padding
622 if (!_sizing_text.empty()) {
623 _layout->set_text (_sizing_text); /* use sizing text */
626 int sizing_text_width = 0, sizing_text_height = 0;
627 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
629 req->width += sizing_text_width;
631 if (!_sizing_text.empty()) {
632 set_text_internal (); /* restore display text */
636 /* XXX hack (surprise). Deal with two common rotation angles */
638 if (_angle == 90 || _angle == 270) {
639 /* do not swap text width or height because we rely on
640 these being the un-rotated values in ::render()
642 swap (req->width, req->height);
651 req->width += _pixbuf->get_width() + char_pixel_width();
652 req->height = std::max(req->height, _pixbuf->get_height() + 4);
655 if (_elements & Indicator) {
656 req->width += lrint (_diameter) + char_pixel_width();
657 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
660 if ((_elements & Menu)) {
661 req->width += _diameter + 4;
664 if (_elements & VectorIcon) {
665 assert(!(_elements & Text));
666 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
668 req->height = std::max(req->height, wh);
671 /* Tweaks to mess the nice stuff above up again. */
672 if (_tweaks & TrackHeader) {
673 // forget everything above and just use a fixed square [em] size
674 // "TrackHeader Buttons" are single letter (usually uppercase)
675 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
676 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
680 else if (_tweaks & Square) {
681 // currerntly unused (again)
682 if (req->width < req->height)
683 req->width = req->height;
684 if (req->height < req->width)
685 req->height = req->width;
686 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
687 // properly centered text for those elements that are centered
688 // (no sub-pixel offset)
689 if ((req->width - _text_width) & 1) { ++req->width; }
690 if ((req->height - _text_height) & 1) { ++req->height; }
693 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
698 * This sets the colors used for rendering based on the name of the button, and
699 * thus uses information from the GUI config data.
702 ArdourButton::set_colors ()
704 _update_colors = false;
706 if (_fixed_colors_set == 0x3) {
710 std::string name = get_name();
713 if (!(_fixed_colors_set & 0x1)) {
714 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
716 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
720 if (!(_fixed_colors_set & 0x2)) {
721 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
723 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
727 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
728 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
730 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
732 led_active_color = UIConfiguration::instance().color ("generic button: led active");
735 /* The inactive color for the LED is just a fairly dark version of the
739 ArdourCanvas::HSV inactive (led_active_color);
742 led_inactive_color = inactive.color ();
746 * This sets the colors used for rendering based on two fixed values, rather
747 * than basing them on the button name, and thus information in the GUI config
750 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
752 set_active_color (color_active);
753 set_inactive_color (color_inactive);
756 void ArdourButton::set_active_color (const uint32_t color)
758 _fixed_colors_set |= 0x1;
760 fill_active_color = color;
762 unsigned char r, g, b, a;
763 UINT_TO_RGBA(color, &r, &g, &b, &a);
765 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
766 (max (double(g), 255.) - min (double(g), 255.)) +
767 (max (double(b), 255.) - min (double(b), 255.));
769 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
770 (max (double(g), 0.) - min (double(g), 0.)) +
771 (max (double(b), 0.) - min (double(b), 0.));
773 text_active_color = (white_contrast > black_contrast) ?
774 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
775 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
777 /* XXX what about led colors ? */
778 CairoWidget::set_dirty ();
781 void ArdourButton::set_inactive_color (const uint32_t color)
783 _fixed_colors_set |= 0x2;
785 fill_inactive_color = color;
787 unsigned char r, g, b, a;
788 UINT_TO_RGBA(color, &r, &g, &b, &a);
790 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
791 (max (double(g), 255.) - min (double(g), 255.)) +
792 (max (double(b), 255.) - min (double(b), 255.));
794 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
795 (max (double(g), 0.) - min (double(g), 0.)) +
796 (max (double(b), 0.) - min (double(b), 0.));
798 text_inactive_color = (white_contrast > black_contrast) ?
799 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
800 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
802 /* XXX what about led colors ? */
803 CairoWidget::set_dirty ();
807 ArdourButton::build_patterns ()
809 if (convex_pattern) {
810 cairo_pattern_destroy (convex_pattern);
814 if (concave_pattern) {
815 cairo_pattern_destroy (concave_pattern);
819 if (led_inset_pattern) {
820 cairo_pattern_destroy (led_inset_pattern);
821 led_inset_pattern = 0;
825 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
826 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
827 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
830 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
831 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
832 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
834 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
835 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
836 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
838 _pattern_height = get_height() ;
842 ArdourButton::set_led_left (bool yn)
848 ArdourButton::on_button_press_event (GdkEventButton *ev)
850 focus_handler (this);
852 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
853 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
854 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
859 if (binding_proxy.button_press_handler (ev)) {
864 CairoWidget::set_dirty ();
866 if (ev->button == 1 && !_act_on_release) {
868 _action->activate ();
873 if (_fallthrough_to_parent)
880 ArdourButton::on_button_release_event (GdkEventButton *ev)
882 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
883 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
884 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
885 signal_led_clicked(ev); /* EMIT SIGNAL */
891 CairoWidget::set_dirty ();
893 if (ev->button == 1 && _hovering) {
895 if (_act_on_release) {
897 _action->activate ();
903 if (_fallthrough_to_parent)
910 ArdourButton::set_distinct_led_click (bool yn)
912 _distinct_led_click = yn;
917 ArdourButton::color_handler ()
919 _update_colors = true;
920 CairoWidget::set_dirty ();
924 ArdourButton::on_size_allocate (Allocation& alloc)
926 CairoWidget::on_size_allocate (alloc);
931 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
933 watch_connection.disconnect ();
934 binding_proxy.set_controllable (c);
938 ArdourButton::watch ()
940 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
943 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
946 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
950 ArdourButton::controllable_changed ()
952 float val = binding_proxy.get_controllable()->get_value();
954 if (fabs (val) >= 0.5f) {
955 set_active_state (Gtkmm2ext::ExplicitActive);
957 unset_active_state ();
959 CairoWidget::set_dirty ();
963 ArdourButton::set_related_action (RefPtr<Action> act)
965 Gtkmm2ext::Activatable::set_related_action (act);
969 action_tooltip_changed ();
971 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
974 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
977 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
978 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
979 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
984 ArdourButton::action_toggled ()
986 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
989 if (tact->get_active()) {
990 set_active_state (Gtkmm2ext::ExplicitActive);
992 unset_active_state ();
998 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1000 _update_colors = true;
1001 CairoWidget::set_dirty ();
1002 _char_pixel_width = 0;
1003 _char_pixel_height = 0;
1004 if (is_realized()) {
1010 ArdourButton::on_name_changed ()
1012 _char_pixel_width = 0;
1013 _char_pixel_height = 0;
1015 _update_colors = true;
1016 if (is_realized()) {
1022 ArdourButton::setup_led_rect ()
1024 if (!(_elements & Indicator)) {
1031 _led_rect = new cairo_rectangle_t;
1034 if (_elements & Text) {
1036 _led_rect->x = char_pixel_width();
1038 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1042 _led_rect->x = .5 * get_width() - _diameter;
1045 _led_rect->y = .5 * (get_height() - _diameter);
1046 _led_rect->width = _diameter;
1047 _led_rect->height = _diameter;
1051 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1053 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1055 if (is_realized()) {
1061 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1063 bool changed = (_active_state != s);
1064 CairoWidget::set_active_state (s);
1066 _update_colors = true;
1067 CairoWidget::set_dirty ();
1072 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1074 bool changed = (_visual_state != s);
1075 CairoWidget::set_visual_state (s);
1077 _update_colors = true;
1078 CairoWidget::set_dirty ();
1083 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1086 CairoWidget::set_dirty ();
1087 return CairoWidget::on_focus_in_event (ev);
1091 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1094 CairoWidget::set_dirty ();
1095 return CairoWidget::on_focus_out_event (ev);
1099 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1101 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1105 _action->activate ();
1109 return CairoWidget::on_key_release_event (ev);
1113 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1115 _hovering = (_elements & Inactive) ? false : true;
1117 if (UIConfiguration::instance().get_widget_prelight()) {
1118 CairoWidget::set_dirty ();
1121 return CairoWidget::on_enter_notify_event (ev);
1125 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1129 if (UIConfiguration::instance().get_widget_prelight()) {
1130 CairoWidget::set_dirty ();
1133 return CairoWidget::on_leave_notify_event (ev);
1137 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1138 /* Our implicit grab due to a button_press was broken by another grab:
1139 * the button will not get any button_release event if the mouse leaves
1140 * while the grab is taken, so unpress ourselves */
1142 CairoWidget::set_dirty ();
1147 ArdourButton::set_tweaks (Tweaks t)
1151 if (is_realized()) {
1158 ArdourButton::action_sensitivity_changed ()
1160 if (_action->property_sensitive ()) {
1161 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1163 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1168 ArdourButton::set_layout_ellipsize_width (int w)
1170 if (_layout_ellipsize_width == w) {
1173 _layout_ellipsize_width = w;
1177 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1178 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1180 if (is_realized ()) {
1186 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1188 if (_ellipsis == e) {
1195 _layout->set_ellipsize(_ellipsis);
1196 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1197 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1199 if (is_realized ()) {
1205 ArdourButton::ensure_layout ()
1209 _layout = Pango::Layout::create (get_pango_context());
1210 _layout->set_ellipsize(_ellipsis);
1211 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1212 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1218 ArdourButton::recalc_char_pixel_geometry ()
1220 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1224 // NB. this is not static, since the geometry is different
1225 // depending on the font used.
1227 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1228 _layout->set_text (x);
1229 _layout->get_pixel_size (w, h);
1230 _char_pixel_height = std::max(4, h);
1231 // number of actual chars in the string (not bytes)
1232 // Glib to the rescue.
1233 Glib::ustring gx(x);
1234 _char_avg_pixel_width = w / (float)gx.size();
1235 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1236 set_text_internal (); /* restore display text */
1240 ArdourButton::action_visibility_changed ()
1242 if (_action->property_visible ()) {
1250 ArdourButton::action_tooltip_changed ()
1252 string str = _action->property_tooltip().get_value();
1253 set_tooltip (*this, str);
1257 ArdourButton::set_elements (Element e)
1260 CairoWidget::set_dirty ();
1264 ArdourButton::add_elements (Element e)
1266 _elements = (ArdourButton::Element) (_elements | e);
1267 CairoWidget::set_dirty ();
1271 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1274 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1275 CairoWidget::set_dirty ();
1279 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1281 if (led_custom_color == c && use_custom_led_color == useit) {
1285 led_custom_color = c;
1286 use_custom_led_color = useit;
1287 CairoWidget::set_dirty ();