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));
102 ArdourButton::ArdourButton (const std::string& str, Element e)
104 , _tweaks (Tweaks (0))
108 , _corner_radius (2.5)
113 , fill_inactive_color (0)
114 , fill_active_color (0)
115 , text_active_color(0)
116 , text_inactive_color(0)
117 , led_active_color(0)
118 , led_inactive_color(0)
119 , led_custom_color (0)
120 , use_custom_led_color (false)
122 , concave_pattern (0)
123 , led_inset_pattern (0)
125 , _act_on_release (true)
127 , _distinct_led_click (false)
130 , _fixed_colors_set (false)
131 , _fallthrough_to_parent (false)
132 , _layout_ellipsize_width (-1)
133 , _ellipsis (Pango::ELLIPSIZE_NONE)
134 , _update_colors (true)
135 , _pattern_height (0)
138 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
139 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
142 ArdourButton::~ArdourButton()
146 if (convex_pattern) {
147 cairo_pattern_destroy (convex_pattern);
150 if (concave_pattern) {
151 cairo_pattern_destroy (concave_pattern);
154 if (led_inset_pattern) {
155 cairo_pattern_destroy (led_inset_pattern);
160 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
164 _layout->set_font_description (fd);
170 ArdourButton::set_text (const std::string& str)
173 if (!is_realized()) {
177 if (_layout && _layout->get_text() != _text) {
178 _layout->set_text (_text);
184 ArdourButton::set_angle (const double angle)
190 ArdourButton::set_alignment (const float xa, const float ya)
197 /* TODO make this a dedicated function elsewhere.
200 * virtual ArdourButton::render_vector_icon()
201 * ArdourIconButton::render_vector_icon
204 * ARDOUR_UI_UTILS::render_vector_icon()
207 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
212 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
214 if (_update_colors) {
217 if (get_height() != _pattern_height) {
221 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
222 text_color = text_active_color;
223 led_color = led_active_color;
225 text_color = text_inactive_color;
226 led_color = led_inactive_color;
229 if (use_custom_led_color) {
230 led_color = led_custom_color;
233 void (*rounded_function)(cairo_t*, double, double, double, double, double);
235 switch (_corner_mask) {
236 case 0x1: /* upper left only */
237 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
239 case 0x2: /* upper right only */
240 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
242 case 0x3: /* upper only */
243 rounded_function = Gtkmm2ext::rounded_top_rectangle;
245 /* should really have functions for lower right, lower left,
246 lower only, but for now, we don't
249 rounded_function = Gtkmm2ext::rounded_rectangle;
252 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
253 if ((_elements & (Body|Edge)) == (Body|Edge)) {
254 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
255 cairo_set_source_rgba (cr, 0, 0, 0, 1);
260 if ((_elements & Body)==Body) {
261 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
262 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
263 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
265 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
267 ArdourCanvas::set_source_rgba (cr, fill_active_color);
269 } else { //inactive, or it has an indicator
271 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
276 // IMPLICIT ACTIVE: draw a border of the active color
277 if ((_elements & Body)==Body) {
278 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
279 cairo_set_line_width (cr, 2.0);
280 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
281 ArdourCanvas::set_source_rgba (cr, fill_active_color);
286 //show the "convex" or "concave" gradient
287 if (!_flat_buttons) {
288 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
290 cairo_set_source (cr, concave_pattern);
291 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
294 cairo_set_source (cr, convex_pattern);
295 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
302 double x = rint((get_width() - _pixbuf->get_width()) * .5);
303 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
304 #if 0 // DEBUG style (print on hover)
305 if (_hovering || (_elements & Inactive)) {
306 printf("%s: p:%dx%d (%dx%d)\n",
308 _pixbuf->get_width(), _pixbuf->get_height(),
309 get_width(), get_height());
312 if (_elements & Menu) {
313 //if this is a DropDown with an icon, then we need to
314 //move the icon left slightly to accomomodate the arrow
317 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
318 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
321 else /* VectorIcons are exclusive to Pixbuf Icons */
322 if (_elements & VectorIcon) {
323 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
326 const int text_margin = char_pixel_width();
328 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
330 #if 0 // DEBUG style (print on hover)
331 if (_hovering || (_elements & Inactive)) {
332 bool layout_font = true;
333 Pango::FontDescription fd = _layout->get_font_description();
334 if (fd.gobj() == NULL) {
336 fd = get_pango_context()->get_font_description();
338 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
340 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
341 ceil(char_pixel_height() * BASELINESTRETCH),
342 _text_width, _text_height,
343 get_width(), get_height(),
344 layout_font ? "L:" : "W:",
345 fd.to_string().c_str());
350 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
354 ArdourCanvas::set_source_rgba (cr, text_color);
355 const double text_ypos = (get_height() - _text_height) * .5;
357 if (_elements & Menu) {
358 // always left align (dropdown)
359 cairo_move_to (cr, text_margin, text_ypos);
360 pango_cairo_show_layout (cr, _layout->gobj());
361 } else if ( (_elements & Indicator) == Indicator) {
362 // left/right align depending on LED position
364 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
366 cairo_move_to (cr, text_margin, text_ypos);
368 pango_cairo_show_layout (cr, _layout->gobj());
370 /* centered text otherwise */
377 cairo_rotate(cr, _angle * M_PI / 180.0);
378 cairo_device_to_user(cr, &ww, &wh);
379 xa = (ww - _text_width) * _xalign;
380 ya = (wh - _text_height) * _yalign;
382 /* quick hack for left/bottom alignment at -90deg
383 * TODO this should be generalized incl rotation.
384 * currently only 'user' of this API is meter_strip.cc
386 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
388 cairo_move_to (cr, xa, ya);
389 pango_cairo_update_layout(cr, _layout->gobj());
390 pango_cairo_show_layout (cr, _layout->gobj());
397 if (_elements & Menu) {
398 const float trih = ceil(_diameter * .5);
399 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
401 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
402 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
403 cairo_rel_line_to(cr, -triw2, -trih);
404 cairo_rel_line_to(cr, 2. * triw2, 0);
405 cairo_close_path(cr);
407 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
410 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
411 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
412 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
413 cairo_close_path(cr);
414 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
415 cairo_set_line_width(cr, 1);
420 if (_elements & Indicator) {
423 /* move to the center of the indicator/led */
424 if (_elements & Text) {
425 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
427 cairo_translate (cr, led_xoff, get_height() * .5);
429 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
432 cairo_translate (cr, get_width() * .5, get_height() * .5);
436 if (!_flat_buttons) {
437 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
438 cairo_set_source (cr, led_inset_pattern);
443 cairo_set_source_rgb (cr, 0, 0, 0);
444 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
448 ArdourCanvas::set_source_rgba (cr, led_color);
449 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
455 // a transparent overlay to indicate insensitivity
456 if ((visual_state() & Gtkmm2ext::Insensitive)) {
457 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
458 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
459 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
463 // if requested, show hovering
464 if (UIConfiguration::instance().get_widget_prelight()
465 && !((visual_state() & Gtkmm2ext::Insensitive))) {
467 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
468 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
473 //user is currently pressing the button. dark outline helps to indicate this
474 if (_grabbed && !(_elements & (Inactive|Menu))) {
475 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
476 cairo_set_line_width(cr, 2);
477 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
481 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
482 if (visual_state() & Gtkmm2ext::Selected) {
483 cairo_set_line_width(cr, 1);
484 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
485 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
489 //I guess this means we have keyboard focus. I don't think this works currently
491 //A: yes, it's keyboard focus and it does work when there's no editor window
492 // (the editor is always the first receiver for KeyDown).
493 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
495 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
496 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
498 cairo_set_dash (cr, &dashes, 1, 0);
499 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
500 cairo_set_line_width (cr, 1.0);
502 cairo_set_dash (cr, 0, 0, 0);
507 ArdourButton::set_corner_radius (float r)
510 CairoWidget::set_dirty ();
514 ArdourButton::on_realize()
516 CairoWidget::on_realize ();
518 if (_layout && _layout->get_text() != _text) {
519 _layout->set_text (_text);
525 ArdourButton::on_size_request (Gtk::Requisition* req)
527 req->width = req->height = 0;
528 CairoWidget::on_size_request (req);
530 if (_diameter == 0) {
531 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
532 if (_diameter != newdia) {
538 if ((_elements & Text) && !_text.empty()) {
539 // if _layout does not exist, char_pixel_height() creates it,
540 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
541 _layout->get_pixel_size (_text_width, _text_height);
542 req->width += rint(1.75 * char_pixel_width()); // padding
543 req->width += _text_width;
550 req->width += _pixbuf->get_width() + char_pixel_width();
551 req->height = std::max(req->height, _pixbuf->get_height() + 4);
554 if (_elements & Indicator) {
555 req->width += lrint (_diameter) + char_pixel_width();
556 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
559 if ((_elements & Menu)) {
560 req->width += _diameter + 4;
563 if (_elements & VectorIcon) {
564 assert(!(_elements & Text));
565 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
567 req->height = std::max(req->height, wh);
570 /* Tweaks to mess the nice stuff above up again. */
571 if (_tweaks & TrackHeader) {
572 // forget everything above and just use a fixed square [em] size
573 // "TrackHeader Buttons" are single letter (usually uppercase)
574 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
575 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
579 else if (_tweaks & Square) {
580 // currerntly unused (again)
581 if (req->width < req->height)
582 req->width = req->height;
583 if (req->height < req->width)
584 req->height = req->width;
585 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
586 // properly centered text for those elements that are centered
587 // (no sub-pixel offset)
588 if ((req->width - _text_width) & 1) { ++req->width; }
589 if ((req->height - _text_height) & 1) { ++req->height; }
592 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
597 * This sets the colors used for rendering based on the name of the button, and
598 * thus uses information from the GUI config data.
601 ArdourButton::set_colors ()
603 _update_colors = false;
604 if (_fixed_colors_set) {
607 std::string name = get_name();
610 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
612 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
614 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
616 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
619 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
620 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
622 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
624 led_active_color = UIConfiguration::instance().color ("generic button: led active");
627 /* The inactive color for the LED is just a fairly dark version of the
631 ArdourCanvas::HSV inactive (led_active_color);
634 led_inactive_color = inactive.color ();
638 * This sets the colors used for rendering based on two fixed values, rather
639 * than basing them on the button name, and thus information in the GUI config
642 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
644 _fixed_colors_set = true;
646 fill_active_color = color_active;
647 fill_inactive_color = color_inactive;
649 unsigned char r, g, b, a;
650 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
652 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
653 (max (double(g), 255.) - min (double(g), 255.)) +
654 (max (double(b), 255.) - min (double(b), 255.));
656 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
657 (max (double(g), 0.) - min (double(g), 0.)) +
658 (max (double(b), 0.) - min (double(b), 0.));
660 text_active_color = (white_contrast > black_contrast) ?
661 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
662 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
665 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
667 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
668 (max (double(g), 255.) - min (double(g), 255.)) +
669 (max (double(b), 255.) - min (double(b), 255.));
671 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
672 (max (double(g), 0.) - min (double(g), 0.)) +
673 (max (double(b), 0.) - min (double(b), 0.));
675 text_inactive_color = (white_contrast > black_contrast) ?
676 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
677 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
679 /* XXX what about led colors ? */
680 CairoWidget::set_dirty ();
684 ArdourButton::build_patterns ()
686 if (convex_pattern) {
687 cairo_pattern_destroy (convex_pattern);
691 if (concave_pattern) {
692 cairo_pattern_destroy (concave_pattern);
696 if (led_inset_pattern) {
697 cairo_pattern_destroy (led_inset_pattern);
698 led_inset_pattern = 0;
702 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
703 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
704 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
707 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
708 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
709 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
711 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
712 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
713 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
715 _pattern_height = get_height() ;
719 ArdourButton::set_led_left (bool yn)
725 ArdourButton::on_button_press_event (GdkEventButton *ev)
729 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
730 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
731 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
736 if (binding_proxy.button_press_handler (ev)) {
741 CairoWidget::set_dirty ();
743 if (ev->button == 1 && !_act_on_release) {
745 _action->activate ();
750 if (_fallthrough_to_parent)
757 ArdourButton::on_button_release_event (GdkEventButton *ev)
759 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
760 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
761 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
762 signal_led_clicked(ev); /* EMIT SIGNAL */
768 CairoWidget::set_dirty ();
770 if (ev->button == 1 && _hovering) {
772 if (_act_on_release) {
774 _action->activate ();
780 if (_fallthrough_to_parent)
787 ArdourButton::set_distinct_led_click (bool yn)
789 _distinct_led_click = yn;
794 ArdourButton::color_handler ()
796 _update_colors = true;
797 CairoWidget::set_dirty ();
801 ArdourButton::on_size_allocate (Allocation& alloc)
803 CairoWidget::on_size_allocate (alloc);
808 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
810 watch_connection.disconnect ();
811 binding_proxy.set_controllable (c);
815 ArdourButton::watch ()
817 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
820 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
823 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
827 ArdourButton::controllable_changed ()
829 float val = binding_proxy.get_controllable()->get_value();
831 if (fabs (val) >= 0.5f) {
832 set_active_state (Gtkmm2ext::ExplicitActive);
834 unset_active_state ();
836 CairoWidget::set_dirty ();
840 ArdourButton::set_related_action (RefPtr<Action> act)
842 Gtkmm2ext::Activatable::set_related_action (act);
846 action_tooltip_changed ();
848 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
851 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
854 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
855 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
856 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
861 ArdourButton::action_toggled ()
863 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
866 if (tact->get_active()) {
867 set_active_state (Gtkmm2ext::ExplicitActive);
869 unset_active_state ();
875 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
877 _update_colors = true;
878 CairoWidget::set_dirty ();
882 ArdourButton::on_name_changed ()
884 _char_pixel_width = 0;
885 _char_pixel_height = 0;
887 _update_colors = true;
894 ArdourButton::setup_led_rect ()
896 if (!(_elements & Indicator)) {
903 _led_rect = new cairo_rectangle_t;
906 if (_elements & Text) {
908 _led_rect->x = char_pixel_width();
910 _led_rect->x = get_width() - char_pixel_width() + _diameter;
914 _led_rect->x = .5 * get_width() - _diameter;
917 _led_rect->y = .5 * (get_height() - _diameter);
918 _led_rect->width = _diameter;
919 _led_rect->height = _diameter;
923 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
932 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
934 bool changed = (_active_state != s);
935 CairoWidget::set_active_state (s);
937 _update_colors = true;
938 CairoWidget::set_dirty ();
943 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
945 bool changed = (_visual_state != s);
946 CairoWidget::set_visual_state (s);
948 _update_colors = true;
949 CairoWidget::set_dirty ();
954 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
957 CairoWidget::set_dirty ();
958 return CairoWidget::on_focus_in_event (ev);
962 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
965 CairoWidget::set_dirty ();
966 return CairoWidget::on_focus_out_event (ev);
970 ArdourButton::on_key_release_event (GdkEventKey *ev) {
972 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
976 _action->activate ();
980 return CairoWidget::on_key_release_event (ev);
984 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
986 _hovering = (_elements & Inactive) ? false : true;
988 if (UIConfiguration::instance().get_widget_prelight()) {
989 CairoWidget::set_dirty ();
992 return CairoWidget::on_enter_notify_event (ev);
996 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1000 if (UIConfiguration::instance().get_widget_prelight()) {
1001 CairoWidget::set_dirty ();
1004 return CairoWidget::on_leave_notify_event (ev);
1008 ArdourButton::set_tweaks (Tweaks t)
1012 if (is_realized()) {
1019 ArdourButton::action_sensitivity_changed ()
1021 if (_action->property_sensitive ()) {
1022 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1024 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1029 ArdourButton::set_layout_ellipsize_width (int w)
1031 if (_layout_ellipsize_width == w) {
1034 _layout_ellipsize_width = w;
1038 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1039 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1041 if (is_realized ()) {
1047 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1049 if (_ellipsis == e) {
1056 _layout->set_ellipsize(_ellipsis);
1057 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1058 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1060 if (is_realized ()) {
1066 ArdourButton::ensure_layout ()
1070 _layout = Pango::Layout::create (get_pango_context());
1071 _layout->set_ellipsize(_ellipsis);
1072 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1073 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1079 ArdourButton::recalc_char_pixel_geometry ()
1081 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1085 // NB. this is not static, since the geometry is different
1086 // depending on the font used.
1088 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1089 _layout->set_text (x);
1090 _layout->get_pixel_size (w, h);
1091 _char_pixel_height = std::max(4, h);
1092 // number of actual chars in the string (not bytes)
1093 // Glib to the rescue.
1094 Glib::ustring gx(x);
1095 _char_avg_pixel_width = w / (float)gx.size();
1096 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1097 _layout->set_text (_text);
1101 ArdourButton::action_visibility_changed ()
1103 if (_action->property_visible ()) {
1111 ArdourButton::action_tooltip_changed ()
1113 string str = _action->property_tooltip().get_value();
1114 set_tooltip (*this, str);
1118 ArdourButton::set_elements (Element e)
1121 CairoWidget::set_dirty ();
1125 ArdourButton::add_elements (Element e)
1127 _elements = (ArdourButton::Element) (_elements | e);
1128 CairoWidget::set_dirty ();
1132 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1135 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1136 CairoWidget::set_dirty ();
1140 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1142 if (led_custom_color == c && use_custom_led_color == useit) {
1146 led_custom_color = c;
1147 use_custom_led_color = useit;
1148 CairoWidget::set_dirty ();