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)
179 if (!is_realized()) {
183 if (_layout && _layout->get_text() != _text) {
184 _layout->set_text (_text);
190 ArdourButton::set_angle (const double angle)
196 ArdourButton::set_alignment (const float xa, const float ya)
203 /* TODO make this a dedicated function elsewhere.
206 * virtual ArdourButton::render_vector_icon()
207 * ArdourIconButton::render_vector_icon
210 * ARDOUR_UI_UTILS::render_vector_icon()
213 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
218 const float corner_radius = std::max(2.f, _corner_radius * UIConfiguration::instance().get_ui_scale());
220 if (_update_colors) {
223 if (get_height() != _pattern_height) {
227 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
228 text_color = text_active_color;
229 led_color = led_active_color;
231 text_color = text_inactive_color;
232 led_color = led_inactive_color;
235 if (use_custom_led_color) {
236 led_color = led_custom_color;
239 void (*rounded_function)(cairo_t*, double, double, double, double, double);
241 switch (_corner_mask) {
242 case 0x1: /* upper left only */
243 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
245 case 0x2: /* upper right only */
246 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
248 case 0x3: /* upper only */
249 rounded_function = Gtkmm2ext::rounded_top_rectangle;
251 /* should really have functions for lower right, lower left,
252 lower only, but for now, we don't
255 rounded_function = Gtkmm2ext::rounded_rectangle;
258 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
259 if ((_elements & (Body|Edge)) == (Body|Edge)) {
260 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
261 cairo_set_source_rgba (cr, 0, 0, 0, 1);
266 if ((_elements & Body)==Body) {
267 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
268 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
269 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
271 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
273 ArdourCanvas::set_source_rgba (cr, fill_active_color);
275 } else { //inactive, or it has an indicator
277 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
282 // IMPLICIT ACTIVE: draw a border of the active color
283 if ((_elements & Body)==Body) {
284 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
285 cairo_set_line_width (cr, 2.0);
286 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
287 ArdourCanvas::set_source_rgba (cr, fill_active_color);
292 //show the "convex" or "concave" gradient
293 if (!_flat_buttons && (_elements & Body)==Body) {
294 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
296 cairo_set_source (cr, concave_pattern);
297 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
300 cairo_set_source (cr, convex_pattern);
301 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
308 double x = rint((get_width() - _pixbuf->get_width()) * .5);
309 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
310 #if 0 // DEBUG style (print on hover)
311 if (_hovering || (_elements & Inactive)) {
312 printf("%s: p:%dx%d (%dx%d)\n",
314 _pixbuf->get_width(), _pixbuf->get_height(),
315 get_width(), get_height());
318 if (_elements & Menu) {
319 //if this is a DropDown with an icon, then we need to
320 //move the icon left slightly to accomomodate the arrow
323 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
324 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
327 else /* VectorIcons are exclusive to Pixbuf Icons */
328 if (_elements & VectorIcon) {
329 Gtkmm2ext::ArdourIcon::render (cr, _icon, get_width(), get_height(), active_state(), text_color);
332 const int text_margin = char_pixel_width();
334 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
336 #if 0 // DEBUG style (print on hover)
337 if (_hovering || (_elements & Inactive)) {
338 bool layout_font = true;
339 Pango::FontDescription fd = _layout->get_font_description();
340 if (fd.gobj() == NULL) {
342 fd = get_pango_context()->get_font_description();
344 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
346 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
347 ceil(char_pixel_height() * BASELINESTRETCH),
348 _text_width, _text_height,
349 get_width(), get_height(),
350 layout_font ? "L:" : "W:",
351 fd.to_string().c_str());
356 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
360 ArdourCanvas::set_source_rgba (cr, text_color);
361 const double text_ypos = (get_height() - _text_height) * .5;
363 if (_elements & Menu) {
364 // always left align (dropdown)
365 cairo_move_to (cr, text_margin, text_ypos);
366 pango_cairo_show_layout (cr, _layout->gobj());
367 } else if ( (_elements & Indicator) == Indicator) {
368 // left/right align depending on LED position
370 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
372 cairo_move_to (cr, text_margin, text_ypos);
374 pango_cairo_show_layout (cr, _layout->gobj());
376 /* centered text otherwise */
383 cairo_rotate(cr, _angle * M_PI / 180.0);
384 cairo_device_to_user(cr, &ww, &wh);
385 xa = (ww - _text_width) * _xalign;
386 ya = (wh - _text_height) * _yalign;
388 /* quick hack for left/bottom alignment at -90deg
389 * TODO this should be generalized incl rotation.
390 * currently only 'user' of this API is meter_strip.cc
392 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
394 cairo_move_to (cr, xa, ya);
395 pango_cairo_update_layout(cr, _layout->gobj());
396 pango_cairo_show_layout (cr, _layout->gobj());
403 if (_elements & Menu) {
404 const float trih = ceil(_diameter * .5);
405 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
407 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
408 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
409 cairo_rel_line_to(cr, -triw2, -trih);
410 cairo_rel_line_to(cr, 2. * triw2, 0);
411 cairo_close_path(cr);
413 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
416 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
417 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
418 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
419 cairo_close_path(cr);
420 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
421 cairo_set_line_width(cr, 1);
426 if (_elements & Indicator) {
429 /* move to the center of the indicator/led */
430 if (_elements & Text) {
431 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
433 cairo_translate (cr, led_xoff, get_height() * .5);
435 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
438 cairo_translate (cr, get_width() * .5, get_height() * .5);
442 if (!_flat_buttons) {
443 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
444 cairo_set_source (cr, led_inset_pattern);
449 cairo_set_source_rgb (cr, 0, 0, 0);
450 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
454 ArdourCanvas::set_source_rgba (cr, led_color);
455 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfiguration::instance().get_ui_scale(), 0, 2 * M_PI);
461 // a transparent overlay to indicate insensitivity
462 if ((visual_state() & Gtkmm2ext::Insensitive)) {
463 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
464 uint32_t ins_color = UIConfiguration::instance().color ("gtk_background");
465 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
469 // if requested, show hovering
470 if (UIConfiguration::instance().get_widget_prelight()
471 && !((visual_state() & Gtkmm2ext::Insensitive))) {
473 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
474 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
479 //user is currently pressing the button. dark outline helps to indicate this
480 if (_grabbed && !(_elements & (Inactive|Menu))) {
481 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
482 cairo_set_line_width(cr, 2);
483 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
487 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
488 if (visual_state() & Gtkmm2ext::Selected) {
489 cairo_set_line_width(cr, 1);
490 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
491 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
495 //I guess this means we have keyboard focus. I don't think this works currently
497 //A: yes, it's keyboard focus and it does work when there's no editor window
498 // (the editor is always the first receiver for KeyDown).
499 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
501 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
502 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
504 cairo_set_dash (cr, &dashes, 1, 0);
505 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
506 cairo_set_line_width (cr, 1.0);
508 cairo_set_dash (cr, 0, 0, 0);
513 ArdourButton::set_corner_radius (float r)
516 CairoWidget::set_dirty ();
520 ArdourButton::on_realize()
522 CairoWidget::on_realize ();
524 if (_layout && _layout->get_text() != _text) {
525 _layout->set_text (_text);
531 ArdourButton::on_size_request (Gtk::Requisition* req)
533 req->width = req->height = 0;
534 CairoWidget::on_size_request (req);
536 if (_diameter == 0) {
537 const float newdia = rintf (11.f * UIConfiguration::instance().get_ui_scale());
538 if (_diameter != newdia) {
544 if (_elements & Text) {
546 if (_tweaks & OccasionalText) {
548 /* size should not change based on presence or absence
552 if (!_text.empty()) {
554 _layout->set_text (_text);
555 _layout->get_pixel_size (_text_width, _text_height);
558 } else if (!_text.empty()) {
560 //if _layout does not exist, char_pixel_height() creates it,
562 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
564 _layout->get_pixel_size (_text_width, _text_height);
565 req->width += rint(1.75 * char_pixel_width()); // padding
566 req->width += _text_width;
569 /* XXX hack (surprise). Deal with two common rotation angles */
571 if (_angle == 90 || _angle == 270) {
572 /* do not swap text width or height because we rely on
573 these being the un-rotated values in ::render()
575 swap (req->width, req->height);
584 req->width += _pixbuf->get_width() + char_pixel_width();
585 req->height = std::max(req->height, _pixbuf->get_height() + 4);
588 if (_elements & Indicator) {
589 req->width += lrint (_diameter) + char_pixel_width();
590 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
593 if ((_elements & Menu)) {
594 req->width += _diameter + 4;
597 if (_elements & VectorIcon) {
598 assert(!(_elements & Text));
599 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
601 req->height = std::max(req->height, wh);
604 /* Tweaks to mess the nice stuff above up again. */
605 if (_tweaks & TrackHeader) {
606 // forget everything above and just use a fixed square [em] size
607 // "TrackHeader Buttons" are single letter (usually uppercase)
608 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
609 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
613 else if (_tweaks & Square) {
614 // currerntly unused (again)
615 if (req->width < req->height)
616 req->width = req->height;
617 if (req->height < req->width)
618 req->height = req->width;
619 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
620 // properly centered text for those elements that are centered
621 // (no sub-pixel offset)
622 if ((req->width - _text_width) & 1) { ++req->width; }
623 if ((req->height - _text_height) & 1) { ++req->height; }
626 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
631 * This sets the colors used for rendering based on the name of the button, and
632 * thus uses information from the GUI config data.
635 ArdourButton::set_colors ()
637 _update_colors = false;
639 if (_fixed_colors_set == 0x3) {
643 std::string name = get_name();
646 if (!(_fixed_colors_set & 0x1)) {
647 fill_active_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
649 fill_active_color = UIConfiguration::instance().color ("generic button: fill active");
653 if (!(_fixed_colors_set & 0x2)) {
654 fill_inactive_color = UIConfiguration::instance().color (string_compose ("%1: fill", name), &failed);
656 fill_inactive_color = UIConfiguration::instance().color ("generic button: fill");
660 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
661 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
663 led_active_color = UIConfiguration::instance().color (string_compose ("%1: led active", name), &failed);
665 led_active_color = UIConfiguration::instance().color ("generic button: led active");
668 /* The inactive color for the LED is just a fairly dark version of the
672 ArdourCanvas::HSV inactive (led_active_color);
675 led_inactive_color = inactive.color ();
679 * This sets the colors used for rendering based on two fixed values, rather
680 * than basing them on the button name, and thus information in the GUI config
683 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
685 set_active_color (color_active);
686 set_inactive_color (color_inactive);
689 void ArdourButton::set_active_color (const uint32_t color)
691 _fixed_colors_set |= 0x1;
693 fill_active_color = color;
695 unsigned char r, g, b, a;
696 UINT_TO_RGBA(color, &r, &g, &b, &a);
698 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
699 (max (double(g), 255.) - min (double(g), 255.)) +
700 (max (double(b), 255.) - min (double(b), 255.));
702 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
703 (max (double(g), 0.) - min (double(g), 0.)) +
704 (max (double(b), 0.) - min (double(b), 0.));
706 text_active_color = (white_contrast > black_contrast) ?
707 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
708 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
710 /* XXX what about led colors ? */
711 CairoWidget::set_dirty ();
714 void ArdourButton::set_inactive_color (const uint32_t color)
716 _fixed_colors_set |= 0x2;
718 fill_inactive_color = color;
720 unsigned char r, g, b, a;
721 UINT_TO_RGBA(color, &r, &g, &b, &a);
723 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
724 (max (double(g), 255.) - min (double(g), 255.)) +
725 (max (double(b), 255.) - min (double(b), 255.));
727 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
728 (max (double(g), 0.) - min (double(g), 0.)) +
729 (max (double(b), 0.) - min (double(b), 0.));
731 text_inactive_color = (white_contrast > black_contrast) ?
732 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
733 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
735 /* XXX what about led colors ? */
736 CairoWidget::set_dirty ();
740 ArdourButton::build_patterns ()
742 if (convex_pattern) {
743 cairo_pattern_destroy (convex_pattern);
747 if (concave_pattern) {
748 cairo_pattern_destroy (concave_pattern);
752 if (led_inset_pattern) {
753 cairo_pattern_destroy (led_inset_pattern);
754 led_inset_pattern = 0;
758 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
759 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
760 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
763 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
764 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
765 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
767 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
768 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
769 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
771 _pattern_height = get_height() ;
775 ArdourButton::set_led_left (bool yn)
781 ArdourButton::on_button_press_event (GdkEventButton *ev)
783 focus_handler (this);
785 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
786 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
787 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
792 if (binding_proxy.button_press_handler (ev)) {
797 CairoWidget::set_dirty ();
799 if (ev->button == 1 && !_act_on_release) {
801 _action->activate ();
806 if (_fallthrough_to_parent)
813 ArdourButton::on_button_release_event (GdkEventButton *ev)
815 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
816 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
817 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
818 signal_led_clicked(ev); /* EMIT SIGNAL */
824 CairoWidget::set_dirty ();
826 if (ev->button == 1 && _hovering) {
828 if (_act_on_release) {
830 _action->activate ();
836 if (_fallthrough_to_parent)
843 ArdourButton::set_distinct_led_click (bool yn)
845 _distinct_led_click = yn;
850 ArdourButton::color_handler ()
852 _update_colors = true;
853 CairoWidget::set_dirty ();
857 ArdourButton::on_size_allocate (Allocation& alloc)
859 CairoWidget::on_size_allocate (alloc);
864 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
866 watch_connection.disconnect ();
867 binding_proxy.set_controllable (c);
871 ArdourButton::watch ()
873 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
876 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
879 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
883 ArdourButton::controllable_changed ()
885 float val = binding_proxy.get_controllable()->get_value();
887 if (fabs (val) >= 0.5f) {
888 set_active_state (Gtkmm2ext::ExplicitActive);
890 unset_active_state ();
892 CairoWidget::set_dirty ();
896 ArdourButton::set_related_action (RefPtr<Action> act)
898 Gtkmm2ext::Activatable::set_related_action (act);
902 action_tooltip_changed ();
904 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
907 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
910 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
911 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
912 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
917 ArdourButton::action_toggled ()
919 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
922 if (tact->get_active()) {
923 set_active_state (Gtkmm2ext::ExplicitActive);
925 unset_active_state ();
931 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
933 _update_colors = true;
934 CairoWidget::set_dirty ();
938 ArdourButton::on_name_changed ()
940 _char_pixel_width = 0;
941 _char_pixel_height = 0;
943 _update_colors = true;
950 ArdourButton::setup_led_rect ()
952 if (!(_elements & Indicator)) {
959 _led_rect = new cairo_rectangle_t;
962 if (_elements & Text) {
964 _led_rect->x = char_pixel_width();
966 _led_rect->x = get_width() - char_pixel_width() + _diameter;
970 _led_rect->x = .5 * get_width() - _diameter;
973 _led_rect->y = .5 * (get_height() - _diameter);
974 _led_rect->width = _diameter;
975 _led_rect->height = _diameter;
979 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
988 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
990 bool changed = (_active_state != s);
991 CairoWidget::set_active_state (s);
993 _update_colors = true;
994 CairoWidget::set_dirty ();
999 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1001 bool changed = (_visual_state != s);
1002 CairoWidget::set_visual_state (s);
1004 _update_colors = true;
1005 CairoWidget::set_dirty ();
1010 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1013 CairoWidget::set_dirty ();
1014 return CairoWidget::on_focus_in_event (ev);
1018 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1021 CairoWidget::set_dirty ();
1022 return CairoWidget::on_focus_out_event (ev);
1026 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1028 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1032 _action->activate ();
1036 return CairoWidget::on_key_release_event (ev);
1040 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1042 _hovering = (_elements & Inactive) ? false : true;
1044 if (UIConfiguration::instance().get_widget_prelight()) {
1045 CairoWidget::set_dirty ();
1048 return CairoWidget::on_enter_notify_event (ev);
1052 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1056 if (UIConfiguration::instance().get_widget_prelight()) {
1057 CairoWidget::set_dirty ();
1060 return CairoWidget::on_leave_notify_event (ev);
1064 ArdourButton::set_tweaks (Tweaks t)
1068 if (is_realized()) {
1075 ArdourButton::action_sensitivity_changed ()
1077 if (_action->property_sensitive ()) {
1078 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1080 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1085 ArdourButton::set_layout_ellipsize_width (int w)
1087 if (_layout_ellipsize_width == w) {
1090 _layout_ellipsize_width = w;
1094 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1095 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1097 if (is_realized ()) {
1103 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1105 if (_ellipsis == e) {
1112 _layout->set_ellipsize(_ellipsis);
1113 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1114 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1116 if (is_realized ()) {
1122 ArdourButton::ensure_layout ()
1126 _layout = Pango::Layout::create (get_pango_context());
1127 _layout->set_ellipsize(_ellipsis);
1128 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1129 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1135 ArdourButton::recalc_char_pixel_geometry ()
1137 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1141 // NB. this is not static, since the geometry is different
1142 // depending on the font used.
1144 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1145 _layout->set_text (x);
1146 _layout->get_pixel_size (w, h);
1147 _char_pixel_height = std::max(4, h);
1148 // number of actual chars in the string (not bytes)
1149 // Glib to the rescue.
1150 Glib::ustring gx(x);
1151 _char_avg_pixel_width = w / (float)gx.size();
1152 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1153 _layout->set_text (_text);
1157 ArdourButton::action_visibility_changed ()
1159 if (_action->property_visible ()) {
1167 ArdourButton::action_tooltip_changed ()
1169 string str = _action->property_tooltip().get_value();
1170 set_tooltip (*this, str);
1174 ArdourButton::set_elements (Element e)
1177 CairoWidget::set_dirty ();
1181 ArdourButton::add_elements (Element e)
1183 _elements = (ArdourButton::Element) (_elements | e);
1184 CairoWidget::set_dirty ();
1188 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1191 _elements = (ArdourButton::Element) ((_elements | ArdourButton::VectorIcon) & ~ArdourButton::Text);
1192 CairoWidget::set_dirty ();
1196 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1198 if (led_custom_color == c && use_custom_led_color == useit) {
1202 led_custom_color = c;
1203 use_custom_led_color = useit;
1204 CairoWidget::set_dirty ();