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))
105 , _char_pixel_width (0)
106 , _char_pixel_height (0)
107 , _char_avg_pixel_width (0)
111 , _corner_radius (2.5)
116 , fill_inactive_color (0)
117 , fill_active_color (0)
118 , text_active_color(0)
119 , text_inactive_color(0)
120 , led_active_color(0)
121 , led_inactive_color(0)
122 , led_custom_color (0)
123 , use_custom_led_color (false)
125 , concave_pattern (0)
126 , led_inset_pattern (0)
128 , _act_on_release (true)
130 , _distinct_led_click (false)
133 , _fixed_colors_set (false)
134 , _fallthrough_to_parent (false)
135 , _layout_ellipsize_width (-1)
136 , _ellipsis (Pango::ELLIPSIZE_NONE)
137 , _update_colors (true)
138 , _pattern_height (0)
141 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
142 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
145 ArdourButton::~ArdourButton()
149 if (convex_pattern) {
150 cairo_pattern_destroy (convex_pattern);
153 if (concave_pattern) {
154 cairo_pattern_destroy (concave_pattern);
157 if (led_inset_pattern) {
158 cairo_pattern_destroy (led_inset_pattern);
163 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
167 _layout->set_font_description (fd);
173 ArdourButton::set_text (const std::string& str)
176 if (!is_realized()) {
180 if (_layout && _layout->get_text() != _text) {
181 _layout->set_text (_text);
187 ArdourButton::set_angle (const double angle)
193 ArdourButton::set_alignment (const float xa, const float ya)
200 /* TODO make this a dedicated function elsewhere.
203 * virtual ArdourButton::render_vector_icon()
204 * ArdourIconButton::render_vector_icon
207 * ARDOUR_UI_UTILS::render_vector_icon()
210 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
215 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
217 if (_update_colors) {
220 if (get_height() != _pattern_height) {
224 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
225 text_color = text_active_color;
226 led_color = led_active_color;
228 text_color = text_inactive_color;
229 led_color = led_inactive_color;
232 if (use_custom_led_color) {
233 led_color = led_custom_color;
236 void (*rounded_function)(cairo_t*, double, double, double, double, double);
238 switch (_corner_mask) {
239 case 0x1: /* upper left only */
240 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
242 case 0x2: /* upper right only */
243 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
245 case 0x3: /* upper only */
246 rounded_function = Gtkmm2ext::rounded_top_rectangle;
248 /* should really have functions for lower right, lower left,
249 lower only, but for now, we don't
252 rounded_function = Gtkmm2ext::rounded_rectangle;
255 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
256 if ((_elements & (Body|Edge)) == (Body|Edge)) {
257 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
258 cairo_set_source_rgba (cr, 0, 0, 0, 1);
263 if ((_elements & Body)==Body) {
264 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
265 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
266 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
268 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
270 ArdourCanvas::set_source_rgba (cr, fill_active_color);
272 } else { //inactive, or it has an indicator
274 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
279 // IMPLICIT ACTIVE: draw a border of the active color
280 if ((_elements & Body)==Body) {
281 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
282 cairo_set_line_width (cr, 2.0);
283 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
284 ArdourCanvas::set_source_rgba (cr, fill_active_color);
289 //show the "convex" or "concave" gradient
290 if (!_flat_buttons) {
291 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
293 cairo_set_source (cr, concave_pattern);
294 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
297 cairo_set_source (cr, convex_pattern);
298 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
305 double x = rint((get_width() - _pixbuf->get_width()) * .5);
306 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
307 #if 0 // DEBUG style (print on hover)
308 if (_hovering || (_elements & Inactive)) {
309 printf("%s: p:%dx%d (%dx%d)\n",
311 _pixbuf->get_width(), _pixbuf->get_height(),
312 get_width(), get_height());
315 if (_elements & Menu) {
316 //if this is a DropDown with an icon, then we need to
317 //move the icon left slightly to accomomodate the arrow
320 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
321 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
324 else /* VectorIcons are exclusive to Pixbuf Icons */
325 if (_elements & VectorIcon) {
326 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
329 const int text_margin = char_pixel_width();
331 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
333 #if 0 // DEBUG style (print on hover)
334 if (_hovering || (_elements & Inactive)) {
335 bool layout_font = true;
336 Pango::FontDescription fd = _layout->get_font_description();
337 if (fd.gobj() == NULL) {
339 fd = get_pango_context()->get_font_description();
341 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
343 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
344 ceil(char_pixel_height() * BASELINESTRETCH),
345 _text_width, _text_height,
346 get_width(), get_height(),
347 layout_font ? "L:" : "W:",
348 fd.to_string().c_str());
353 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
357 ArdourCanvas::set_source_rgba (cr, text_color);
358 const double text_ypos = (get_height() - _text_height) * .5;
360 if (_elements & Menu) {
361 // always left align (dropdown)
362 cairo_move_to (cr, text_margin, text_ypos);
363 pango_cairo_show_layout (cr, _layout->gobj());
364 } else if ( (_elements & Indicator) == Indicator) {
365 // left/right align depending on LED position
367 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
369 cairo_move_to (cr, text_margin, text_ypos);
371 pango_cairo_show_layout (cr, _layout->gobj());
373 /* centered text otherwise */
380 cairo_rotate(cr, _angle * M_PI / 180.0);
381 cairo_device_to_user(cr, &ww, &wh);
382 xa = (ww - _text_width) * _xalign;
383 ya = (wh - _text_height) * _yalign;
385 /* quick hack for left/bottom alignment at -90deg
386 * TODO this should be generalized incl rotation.
387 * currently only 'user' of this API is meter_strip.cc
389 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
391 cairo_move_to (cr, xa, ya);
392 pango_cairo_update_layout(cr, _layout->gobj());
393 pango_cairo_show_layout (cr, _layout->gobj());
400 if (_elements & Menu) {
401 const float trih = ceil(_diameter * .5);
402 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
404 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
405 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
406 cairo_rel_line_to(cr, -triw2, -trih);
407 cairo_rel_line_to(cr, 2. * triw2, 0);
408 cairo_close_path(cr);
410 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
413 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
414 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
415 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
416 cairo_close_path(cr);
417 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
418 cairo_set_line_width(cr, 1);
423 if (_elements & Indicator) {
426 /* move to the center of the indicator/led */
427 if (_elements & Text) {
428 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
430 cairo_translate (cr, led_xoff, get_height() * .5);
432 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
435 cairo_translate (cr, get_width() * .5, get_height() * .5);
439 if (!_flat_buttons) {
440 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
441 cairo_set_source (cr, led_inset_pattern);
446 cairo_set_source_rgb (cr, 0, 0, 0);
447 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
451 ArdourCanvas::set_source_rgba (cr, led_color);
452 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
458 // a transparent overlay to indicate insensitivity
459 if ((visual_state() & Gtkmm2ext::Insensitive)) {
460 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
461 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
462 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
466 // if requested, show hovering
467 if (UIConfiguration::instance().get_widget_prelight()
468 && !((visual_state() & Gtkmm2ext::Insensitive))) {
470 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
471 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
476 //user is currently pressing the button. dark outline helps to indicate this
477 if (_grabbed && !(_elements & (Inactive|Menu))) {
478 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
479 cairo_set_line_width(cr, 2);
480 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
484 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
485 if (visual_state() & Gtkmm2ext::Selected) {
486 cairo_set_line_width(cr, 1);
487 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
488 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
492 //I guess this means we have keyboard focus. I don't think this works currently
494 //A: yes, it's keyboard focus and it does work when there's no editor window
495 // (the editor is always the first receiver for KeyDown).
496 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
498 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
499 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
501 cairo_set_dash (cr, &dashes, 1, 0);
502 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
503 cairo_set_line_width (cr, 1.0);
505 cairo_set_dash (cr, 0, 0, 0);
510 ArdourButton::set_corner_radius (float r)
513 CairoWidget::set_dirty ();
517 ArdourButton::on_realize()
519 CairoWidget::on_realize ();
521 if (_layout && _layout->get_text() != _text) {
522 _layout->set_text (_text);
528 ArdourButton::on_size_request (Gtk::Requisition* req)
530 req->width = req->height = 0;
531 CairoWidget::on_size_request (req);
533 if (_diameter == 0) {
534 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
535 if (_diameter != newdia) {
541 if ((_elements & Text) && !_text.empty()) {
542 // if _layout does not exist, char_pixel_height() creates it,
543 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
545 _layout->get_pixel_size (_text_width, _text_height);
546 req->width += rint(1.75 * char_pixel_width()); // padding
547 req->width += _text_width;
554 req->width += _pixbuf->get_width() + char_pixel_width();
555 req->height = std::max(req->height, _pixbuf->get_height() + 4);
558 if (_elements & Indicator) {
559 req->width += lrint (_diameter) + char_pixel_width();
560 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
563 if ((_elements & Menu)) {
564 req->width += _diameter + 4;
567 if (_elements & VectorIcon) {
568 assert(!(_elements & Text));
569 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
571 req->height = std::max(req->height, wh);
574 /* Tweaks to mess the nice stuff above up again. */
575 if (_tweaks & TrackHeader) {
576 // forget everything above and just use a fixed square [em] size
577 // "TrackHeader Buttons" are single letter (usually uppercase)
578 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
579 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
583 else if (_tweaks & Square) {
584 // currerntly unused (again)
585 if (req->width < req->height)
586 req->width = req->height;
587 if (req->height < req->width)
588 req->height = req->width;
589 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
590 // properly centered text for those elements that are centered
591 // (no sub-pixel offset)
592 if ((req->width - _text_width) & 1) { ++req->width; }
593 if ((req->height - _text_height) & 1) { ++req->height; }
596 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
601 * This sets the colors used for rendering based on the name of the button, and
602 * thus uses information from the GUI config data.
605 ArdourButton::set_colors ()
607 _update_colors = false;
608 if (_fixed_colors_set) {
611 std::string name = get_name();
614 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
616 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
618 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
620 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
623 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
624 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
626 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
628 led_active_color = UIConfiguration::instance().color ("generic button: led active");
631 /* The inactive color for the LED is just a fairly dark version of the
635 ArdourCanvas::HSV inactive (led_active_color);
638 led_inactive_color = inactive.color ();
642 * This sets the colors used for rendering based on two fixed values, rather
643 * than basing them on the button name, and thus information in the GUI config
646 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
648 _fixed_colors_set = true;
650 fill_active_color = color_active;
651 fill_inactive_color = color_inactive;
653 unsigned char r, g, b, a;
654 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
656 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
657 (max (double(g), 255.) - min (double(g), 255.)) +
658 (max (double(b), 255.) - min (double(b), 255.));
660 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
661 (max (double(g), 0.) - min (double(g), 0.)) +
662 (max (double(b), 0.) - min (double(b), 0.));
664 text_active_color = (white_contrast > black_contrast) ?
665 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
666 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
669 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
671 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
672 (max (double(g), 255.) - min (double(g), 255.)) +
673 (max (double(b), 255.) - min (double(b), 255.));
675 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
676 (max (double(g), 0.) - min (double(g), 0.)) +
677 (max (double(b), 0.) - min (double(b), 0.));
679 text_inactive_color = (white_contrast > black_contrast) ?
680 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
681 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
683 /* XXX what about led colors ? */
684 CairoWidget::set_dirty ();
688 ArdourButton::build_patterns ()
690 if (convex_pattern) {
691 cairo_pattern_destroy (convex_pattern);
695 if (concave_pattern) {
696 cairo_pattern_destroy (concave_pattern);
700 if (led_inset_pattern) {
701 cairo_pattern_destroy (led_inset_pattern);
702 led_inset_pattern = 0;
706 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
707 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
708 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
711 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
712 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
713 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
715 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
716 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
717 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
719 _pattern_height = get_height() ;
723 ArdourButton::set_led_left (bool yn)
729 ArdourButton::on_button_press_event (GdkEventButton *ev)
731 focus_handler (this);
733 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
734 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
735 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
740 if (binding_proxy.button_press_handler (ev)) {
745 CairoWidget::set_dirty ();
747 if (ev->button == 1 && !_act_on_release) {
749 _action->activate ();
754 if (_fallthrough_to_parent)
761 ArdourButton::on_button_release_event (GdkEventButton *ev)
763 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
764 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
765 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
766 signal_led_clicked(ev); /* EMIT SIGNAL */
772 CairoWidget::set_dirty ();
774 if (ev->button == 1 && _hovering) {
776 if (_act_on_release) {
778 _action->activate ();
784 if (_fallthrough_to_parent)
791 ArdourButton::set_distinct_led_click (bool yn)
793 _distinct_led_click = yn;
798 ArdourButton::color_handler ()
800 _update_colors = true;
801 CairoWidget::set_dirty ();
805 ArdourButton::on_size_allocate (Allocation& alloc)
807 CairoWidget::on_size_allocate (alloc);
812 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
814 watch_connection.disconnect ();
815 binding_proxy.set_controllable (c);
819 ArdourButton::watch ()
821 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
824 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
827 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
831 ArdourButton::controllable_changed ()
833 float val = binding_proxy.get_controllable()->get_value();
835 if (fabs (val) >= 0.5f) {
836 set_active_state (Gtkmm2ext::ExplicitActive);
838 unset_active_state ();
840 CairoWidget::set_dirty ();
844 ArdourButton::set_related_action (RefPtr<Action> act)
846 Gtkmm2ext::Activatable::set_related_action (act);
850 action_tooltip_changed ();
852 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
855 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
858 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
859 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
860 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
865 ArdourButton::action_toggled ()
867 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
870 if (tact->get_active()) {
871 set_active_state (Gtkmm2ext::ExplicitActive);
873 unset_active_state ();
879 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
881 _update_colors = true;
882 CairoWidget::set_dirty ();
886 ArdourButton::on_name_changed ()
888 _char_pixel_width = 0;
889 _char_pixel_height = 0;
891 _update_colors = true;
898 ArdourButton::setup_led_rect ()
900 if (!(_elements & Indicator)) {
907 _led_rect = new cairo_rectangle_t;
910 if (_elements & Text) {
912 _led_rect->x = char_pixel_width();
914 _led_rect->x = get_width() - char_pixel_width() + _diameter;
918 _led_rect->x = .5 * get_width() - _diameter;
921 _led_rect->y = .5 * (get_height() - _diameter);
922 _led_rect->width = _diameter;
923 _led_rect->height = _diameter;
927 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
936 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
938 bool changed = (_active_state != s);
939 CairoWidget::set_active_state (s);
941 _update_colors = true;
942 CairoWidget::set_dirty ();
947 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
949 bool changed = (_visual_state != s);
950 CairoWidget::set_visual_state (s);
952 _update_colors = true;
953 CairoWidget::set_dirty ();
958 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
961 CairoWidget::set_dirty ();
962 return CairoWidget::on_focus_in_event (ev);
966 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
969 CairoWidget::set_dirty ();
970 return CairoWidget::on_focus_out_event (ev);
974 ArdourButton::on_key_release_event (GdkEventKey *ev) {
976 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
980 _action->activate ();
984 return CairoWidget::on_key_release_event (ev);
988 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
990 _hovering = (_elements & Inactive) ? false : true;
992 if (UIConfiguration::instance().get_widget_prelight()) {
993 CairoWidget::set_dirty ();
996 return CairoWidget::on_enter_notify_event (ev);
1000 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1004 if (UIConfiguration::instance().get_widget_prelight()) {
1005 CairoWidget::set_dirty ();
1008 return CairoWidget::on_leave_notify_event (ev);
1012 ArdourButton::set_tweaks (Tweaks t)
1016 if (is_realized()) {
1023 ArdourButton::action_sensitivity_changed ()
1025 if (_action->property_sensitive ()) {
1026 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1028 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1033 ArdourButton::set_layout_ellipsize_width (int w)
1035 if (_layout_ellipsize_width == w) {
1038 _layout_ellipsize_width = w;
1042 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1043 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1045 if (is_realized ()) {
1051 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1053 if (_ellipsis == e) {
1060 _layout->set_ellipsize(_ellipsis);
1061 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1062 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1064 if (is_realized ()) {
1070 ArdourButton::ensure_layout ()
1074 _layout = Pango::Layout::create (get_pango_context());
1075 _layout->set_ellipsize(_ellipsis);
1076 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1077 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1083 ArdourButton::recalc_char_pixel_geometry ()
1085 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1089 // NB. this is not static, since the geometry is different
1090 // depending on the font used.
1092 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1093 _layout->set_text (x);
1094 _layout->get_pixel_size (w, h);
1095 _char_pixel_height = std::max(4, h);
1096 // number of actual chars in the string (not bytes)
1097 // Glib to the rescue.
1098 Glib::ustring gx(x);
1099 _char_avg_pixel_width = w / (float)gx.size();
1100 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1101 _layout->set_text (_text);
1105 ArdourButton::action_visibility_changed ()
1107 if (_action->property_visible ()) {
1115 ArdourButton::action_tooltip_changed ()
1117 string str = _action->property_tooltip().get_value();
1118 set_tooltip (*this, str);
1122 ArdourButton::set_elements (Element e)
1125 CairoWidget::set_dirty ();
1129 ArdourButton::add_elements (Element e)
1131 _elements = (ArdourButton::Element) (_elements | e);
1132 CairoWidget::set_dirty ();
1136 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1139 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1140 CairoWidget::set_dirty ();
1144 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1146 if (led_custom_color == c && use_custom_led_color == useit) {
1150 led_custom_color = c;
1151 use_custom_led_color = useit;
1152 CairoWidget::set_dirty ();