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>
25 #include <gtkmm/toggleaction.h>
27 #include "pbd/compose.h"
28 #include "pbd/controllable.h"
29 #include "pbd/error.h"
30 #include "pbd/stacktrace.h"
32 #include "gtkmm2ext/colors.h"
33 #include "gtkmm2ext/gui_thread.h"
34 #include "gtkmm2ext/rgb_macros.h"
35 #include "gtkmm2ext/utils.h"
37 #include "widgets/ardour_button.h"
38 #include "widgets/tooltips.h"
39 #include "widgets/ui_config.h"
43 #define BASELINESTRETCH (1.25)
44 #define TRACKHEADERBTNW (3.10)
49 using namespace ArdourWidgets;
54 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
55 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
56 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
58 ArdourButton::ArdourButton (Element e)
62 , _icon (ArdourIcon::NoIcon)
64 , _icon_render_cb_data (0)
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 UIConfigurationBase::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 (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 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
149 UIConfigurationBase::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::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
259 cairo_t* cr = ctx->cobj();
264 const float corner_radius = std::max(2.f, _corner_radius * UIConfigurationBase::instance().get_ui_scale());
266 if (_update_colors) {
269 if (get_height() != _pattern_height) {
273 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
274 text_color = text_active_color;
275 led_color = led_active_color;
277 text_color = text_inactive_color;
278 led_color = led_inactive_color;
281 if (use_custom_led_color) {
282 led_color = led_custom_color;
285 void (*rounded_function)(cairo_t*, double, double, double, double, double);
287 switch (_corner_mask) {
288 case 0x1: /* upper left only */
289 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
291 case 0x2: /* upper right only */
292 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
294 case 0x3: /* upper only */
295 rounded_function = Gtkmm2ext::rounded_top_rectangle;
297 /* should really have functions for lower right, lower left,
298 lower only, but for now, we don't
301 rounded_function = Gtkmm2ext::rounded_rectangle;
304 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
305 if ((_elements & (Body|Edge)) == (Body|Edge)) {
306 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
307 cairo_set_source_rgba (cr, 0, 0, 0, 1);
312 if ((_elements & Body)==Body) {
313 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
314 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
315 Gtkmm2ext::set_source_rgba (cr, fill_inactive_color);
317 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
319 Gtkmm2ext::set_source_rgba (cr, fill_active_color);
321 } else { //inactive, or it has an indicator
323 Gtkmm2ext::set_source_rgba (cr, fill_inactive_color);
328 // IMPLICIT ACTIVE: draw a border of the active color
329 if ((_elements & Body)==Body) {
330 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
331 cairo_set_line_width (cr, 2.0);
332 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
333 Gtkmm2ext::set_source_rgba (cr, fill_active_color);
338 //show the "convex" or "concave" gradient
339 if (!_flat_buttons && (_elements & Body)==Body) {
340 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
342 cairo_set_source (cr, concave_pattern);
343 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
346 cairo_set_source (cr, convex_pattern);
347 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
354 double x = rint((get_width() - _pixbuf->get_width()) * .5);
355 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
356 #if 0 // DEBUG style (print on hover)
357 if (_hovering || (_elements & Inactive)) {
358 printf("%s: p:%dx%d (%dx%d)\n",
360 _pixbuf->get_width(), _pixbuf->get_height(),
361 get_width(), get_height());
364 if (_elements & Menu) {
365 //if this is a DropDown with an icon, then we need to
366 //move the icon left slightly to accomomodate the arrow
369 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
370 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
373 else /* VectorIcon, IconRenderCallback are exclusive to Pixbuf Icons */
374 if (_elements & (VectorIcon | IconRenderCallback)) {
375 int vw = get_width();
376 int vh = get_height();
377 if (_elements & Menu) {
380 if (_elements & VectorIcon) {
381 ArdourIcon::render (cr, _icon, vw, vh, active_state(), text_color);
384 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
386 _icon_render_cb (cr, vw, vh, text_color, _icon_render_cb_data);
391 const int text_margin = char_pixel_width();
393 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
395 #if 0 // DEBUG style (print on hover)
396 if (_hovering || (_elements & Inactive)) {
397 bool layout_font = true;
398 Pango::FontDescription fd = _layout->get_font_description();
399 if (fd.gobj() == NULL) {
401 fd = get_pango_context()->get_font_description();
403 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
405 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
406 ceil(char_pixel_height() * BASELINESTRETCH),
407 _text_width, _text_height,
408 get_width(), get_height(),
409 layout_font ? "L:" : "W:",
410 fd.to_string().c_str());
415 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
419 Gtkmm2ext::set_source_rgba (cr, text_color);
420 const double text_ypos = (get_height() - _text_height) * .5;
422 if (_elements & Menu) {
423 // always left align (dropdown)
424 cairo_move_to (cr, text_margin, text_ypos);
425 pango_cairo_show_layout (cr, _layout->gobj());
426 } else if ( (_elements & Indicator) == Indicator) {
427 // left/right align depending on LED position
429 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
431 cairo_move_to (cr, text_margin, text_ypos);
433 pango_cairo_show_layout (cr, _layout->gobj());
435 /* centered text otherwise */
442 cairo_get_matrix (cr, &m1);
443 cairo_matrix_t m2 = m1;
446 cairo_set_matrix (cr, &m2);
449 cairo_rotate(cr, _angle * M_PI / 180.0);
452 cairo_device_to_user(cr, &ww, &wh);
453 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
454 ya = (wh - _text_height) * _yalign;
456 /* quick hack for left/bottom alignment at -90deg
457 * TODO this should be generalized incl rotation.
458 * currently only 'user' of this API is meter_strip.cc
460 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
462 cairo_move_to (cr, xa + m1.x0, ya + m1.y0);
463 pango_cairo_update_layout(cr, _layout->gobj());
464 pango_cairo_show_layout (cr, _layout->gobj());
470 if (_elements & Menu) {
471 const float trih = ceil(_diameter * .5);
472 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
474 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
475 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
476 cairo_rel_line_to(cr, -triw2, -trih);
477 cairo_rel_line_to(cr, 2. * triw2, 0);
478 cairo_close_path(cr);
480 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
483 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
484 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
485 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
486 cairo_close_path(cr);
487 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
488 cairo_set_line_width(cr, 1);
493 if (_elements & Indicator) {
496 /* move to the center of the indicator/led */
497 if (_elements & Text) {
498 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
500 cairo_translate (cr, led_xoff, get_height() * .5);
502 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
505 cairo_translate (cr, get_width() * .5, get_height() * .5);
509 if (!_flat_buttons) {
510 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
511 cairo_set_source (cr, led_inset_pattern);
516 cairo_set_source_rgb (cr, 0, 0, 0);
517 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
521 Gtkmm2ext::set_source_rgba (cr, led_color);
522 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
528 // a transparent overlay to indicate insensitivity
529 if ((visual_state() & Gtkmm2ext::Insensitive)) {
530 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
531 uint32_t ins_color = UIConfigurationBase::instance().color ("gtk_background");
532 Gtkmm2ext::set_source_rgb_a (cr, ins_color, 0.6);
536 // if requested, show hovering
537 if (UIConfigurationBase::instance().get_widget_prelight()
538 && !((visual_state() & Gtkmm2ext::Insensitive))) {
540 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
541 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
546 //user is currently pressing the button. dark outline helps to indicate this
547 if (_grabbed && !(_elements & (Inactive|Menu))) {
548 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
549 cairo_set_line_width(cr, 2);
550 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
554 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
555 if (visual_state() & Gtkmm2ext::Selected) {
556 cairo_set_line_width(cr, 1);
557 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
558 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
562 //I guess this means we have keyboard focus. I don't think this works currently
564 //A: yes, it's keyboard focus and it does work when there's no editor window
565 // (the editor is always the first receiver for KeyDown).
566 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
568 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
569 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
571 cairo_set_dash (cr, &dashes, 1, 0);
572 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
573 cairo_set_line_width (cr, 1.0);
575 cairo_set_dash (cr, 0, 0, 0);
580 ArdourButton::set_corner_radius (float r)
583 CairoWidget::set_dirty ();
587 ArdourButton::on_realize()
589 CairoWidget::on_realize ();
592 if (_layout->get_text() != _text) {
593 set_text_internal ();
600 ArdourButton::on_size_request (Gtk::Requisition* req)
602 req->width = req->height = 0;
603 CairoWidget::on_size_request (req);
605 if (_diameter == 0) {
606 const float newdia = rintf (11.f * UIConfigurationBase::instance().get_ui_scale());
607 if (_diameter != newdia) {
613 if (_elements & Text) {
616 set_text_internal ();
618 /* render() needs the size of the displayed text */
619 _layout->get_pixel_size (_text_width, _text_height);
621 if (_tweaks & OccasionalText) {
623 /* size should not change based on presence or absence
627 } else /*if (!_text.empty() || !_sizing_text.empty()) */ {
629 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
630 req->width += rint(1.75 * char_pixel_width()); // padding
632 if (!_sizing_text.empty()) {
633 _layout->set_text (_sizing_text); /* use sizing text */
636 int sizing_text_width = 0, sizing_text_height = 0;
637 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
639 req->width += sizing_text_width;
641 if (!_sizing_text.empty()) {
642 set_text_internal (); /* restore display text */
646 /* XXX hack (surprise). Deal with two common rotation angles */
648 if (_angle == 90 || _angle == 270) {
649 /* do not swap text width or height because we rely on
650 these being the un-rotated values in ::render()
652 swap (req->width, req->height);
661 req->width += _pixbuf->get_width() + char_pixel_width();
662 req->height = std::max(req->height, _pixbuf->get_height() + 4);
665 if (_elements & Indicator) {
666 req->width += lrint (_diameter) + char_pixel_width();
667 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
670 if ((_elements & Menu)) {
671 req->width += _diameter + 4;
674 if (_elements & (VectorIcon | IconRenderCallback)) {
675 assert(!(_elements & Text));
676 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
678 req->height = std::max(req->height, wh);
681 /* Tweaks to mess the nice stuff above up again. */
682 if (_tweaks & TrackHeader) {
683 // forget everything above and just use a fixed square [em] size
684 // "TrackHeader Buttons" are single letter (usually uppercase)
685 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
686 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
690 else if (_tweaks & Square) {
691 // currerntly unused (again)
692 if (req->width < req->height)
693 req->width = req->height;
694 if (req->height < req->width)
695 req->height = req->width;
696 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
697 // properly centered text for those elements that are centered
698 // (no sub-pixel offset)
699 if ((req->width - _text_width) & 1) { ++req->width; }
700 if ((req->height - _text_height) & 1) { ++req->height; }
703 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
708 * This sets the colors used for rendering based on the name of the button, and
709 * thus uses information from the GUI config data.
712 ArdourButton::set_colors ()
714 _update_colors = false;
716 if (_fixed_colors_set == 0x3) {
720 std::string name = get_name();
723 if (!(_fixed_colors_set & 0x1)) {
724 fill_active_color = UIConfigurationBase::instance().color (string_compose ("%1: fill active", name), &failed);
726 fill_active_color = UIConfigurationBase::instance().color ("generic button: fill active");
730 if (!(_fixed_colors_set & 0x2)) {
731 fill_inactive_color = UIConfigurationBase::instance().color (string_compose ("%1: fill", name), &failed);
733 fill_inactive_color = UIConfigurationBase::instance().color ("generic button: fill");
737 text_active_color = Gtkmm2ext::contrasting_text_color (fill_active_color);
738 text_inactive_color = Gtkmm2ext::contrasting_text_color (fill_inactive_color);
740 led_active_color = UIConfigurationBase::instance().color (string_compose ("%1: led active", name), &failed);
742 led_active_color = UIConfigurationBase::instance().color ("generic button: led active");
745 /* The inactive color for the LED is just a fairly dark version of the
749 Gtkmm2ext::HSV inactive (led_active_color);
752 led_inactive_color = inactive.color ();
756 * This sets the colors used for rendering based on two fixed values, rather
757 * than basing them on the button name, and thus information in the GUI config
760 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
762 set_active_color (color_active);
763 set_inactive_color (color_inactive);
766 void ArdourButton::set_active_color (const uint32_t color)
768 _fixed_colors_set |= 0x1;
770 fill_active_color = color;
772 unsigned char r, g, b, a;
773 UINT_TO_RGBA(color, &r, &g, &b, &a);
775 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
776 (max (double(g), 255.) - min (double(g), 255.)) +
777 (max (double(b), 255.) - min (double(b), 255.));
779 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
780 (max (double(g), 0.) - min (double(g), 0.)) +
781 (max (double(b), 0.) - min (double(b), 0.));
783 text_active_color = (white_contrast > black_contrast) ?
784 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
785 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
787 /* XXX what about led colors ? */
788 CairoWidget::set_dirty ();
791 void ArdourButton::set_inactive_color (const uint32_t color)
793 _fixed_colors_set |= 0x2;
795 fill_inactive_color = color;
797 unsigned char r, g, b, a;
798 UINT_TO_RGBA(color, &r, &g, &b, &a);
800 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
801 (max (double(g), 255.) - min (double(g), 255.)) +
802 (max (double(b), 255.) - min (double(b), 255.));
804 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
805 (max (double(g), 0.) - min (double(g), 0.)) +
806 (max (double(b), 0.) - min (double(b), 0.));
808 text_inactive_color = (white_contrast > black_contrast) ?
809 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
810 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
812 /* XXX what about led colors ? */
813 CairoWidget::set_dirty ();
817 ArdourButton::build_patterns ()
819 if (convex_pattern) {
820 cairo_pattern_destroy (convex_pattern);
824 if (concave_pattern) {
825 cairo_pattern_destroy (concave_pattern);
829 if (led_inset_pattern) {
830 cairo_pattern_destroy (led_inset_pattern);
831 led_inset_pattern = 0;
835 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
836 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
837 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
840 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
841 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
842 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
844 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
845 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
846 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
848 _pattern_height = get_height() ;
852 ArdourButton::set_led_left (bool yn)
858 ArdourButton::on_button_press_event (GdkEventButton *ev)
860 focus_handler (this);
862 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
863 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
864 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
869 if (binding_proxy.button_press_handler (ev)) {
874 CairoWidget::set_dirty ();
876 if (ev->button == 1 && !_act_on_release) {
878 _action->activate ();
883 if (_fallthrough_to_parent)
890 ArdourButton::on_button_release_event (GdkEventButton *ev)
892 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
893 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
894 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
895 signal_led_clicked(ev); /* EMIT SIGNAL */
901 CairoWidget::set_dirty ();
903 if (ev->button == 1 && _hovering) {
905 if (_act_on_release) {
907 _action->activate ();
913 if (_fallthrough_to_parent)
920 ArdourButton::set_distinct_led_click (bool yn)
922 _distinct_led_click = yn;
927 ArdourButton::color_handler ()
929 _update_colors = true;
930 CairoWidget::set_dirty ();
934 ArdourButton::on_size_allocate (Allocation& alloc)
936 CairoWidget::on_size_allocate (alloc);
941 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
943 watch_connection.disconnect ();
944 binding_proxy.set_controllable (c);
948 ArdourButton::watch ()
950 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
953 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
956 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
960 ArdourButton::controllable_changed ()
962 float val = binding_proxy.get_controllable()->get_value();
964 if (fabs (val) >= 0.5f) {
965 set_active_state (Gtkmm2ext::ExplicitActive);
967 unset_active_state ();
969 CairoWidget::set_dirty ();
973 ArdourButton::set_related_action (RefPtr<Action> act)
975 Gtkmm2ext::Activatable::set_related_action (act);
979 action_tooltip_changed ();
980 action_sensitivity_changed ();
982 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
985 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
988 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
989 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
990 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
995 ArdourButton::action_toggled ()
997 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1000 if (tact->get_active()) {
1001 set_active_state (Gtkmm2ext::ExplicitActive);
1003 unset_active_state ();
1009 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1011 _update_colors = true;
1012 CairoWidget::set_dirty ();
1013 _char_pixel_width = 0;
1014 _char_pixel_height = 0;
1015 if (is_realized()) {
1021 ArdourButton::on_name_changed ()
1023 _char_pixel_width = 0;
1024 _char_pixel_height = 0;
1026 _update_colors = true;
1027 if (is_realized()) {
1033 ArdourButton::setup_led_rect ()
1035 if (!(_elements & Indicator)) {
1042 _led_rect = new cairo_rectangle_t;
1045 if (_elements & Text) {
1047 _led_rect->x = char_pixel_width();
1049 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1053 _led_rect->x = .5 * get_width() - _diameter;
1056 _led_rect->y = .5 * (get_height() - _diameter);
1057 _led_rect->width = _diameter;
1058 _led_rect->height = _diameter;
1062 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1064 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1066 if (is_realized()) {
1072 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1074 bool changed = (_active_state != s);
1075 CairoWidget::set_active_state (s);
1077 _update_colors = true;
1078 CairoWidget::set_dirty ();
1083 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1085 bool changed = (_visual_state != s);
1086 CairoWidget::set_visual_state (s);
1088 _update_colors = true;
1089 CairoWidget::set_dirty ();
1094 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1097 CairoWidget::set_dirty ();
1098 return CairoWidget::on_focus_in_event (ev);
1102 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1105 CairoWidget::set_dirty ();
1106 return CairoWidget::on_focus_out_event (ev);
1110 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1111 if (_act_on_release && _focused &&
1112 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1116 _action->activate ();
1120 return CairoWidget::on_key_release_event (ev);
1124 ArdourButton::on_key_press_event (GdkEventKey *ev) {
1125 if (!_act_on_release && _focused &&
1126 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1130 _action->activate ();
1134 return CairoWidget::on_key_release_event (ev);
1138 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1140 _hovering = (_elements & Inactive) ? false : true;
1142 if (UIConfigurationBase::instance().get_widget_prelight()) {
1143 CairoWidget::set_dirty ();
1146 boost::shared_ptr<PBD::Controllable> c (binding_proxy.get_controllable ());
1148 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
1151 return CairoWidget::on_enter_notify_event (ev);
1155 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1159 if (UIConfigurationBase::instance().get_widget_prelight()) {
1160 CairoWidget::set_dirty ();
1163 if (binding_proxy.get_controllable()) {
1164 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
1167 return CairoWidget::on_leave_notify_event (ev);
1171 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1172 /* Our implicit grab due to a button_press was broken by another grab:
1173 * the button will not get any button_release event if the mouse leaves
1174 * while the grab is taken, so unpress ourselves */
1176 CairoWidget::set_dirty ();
1181 ArdourButton::set_tweaks (Tweaks t)
1185 if (is_realized()) {
1192 ArdourButton::action_sensitivity_changed ()
1194 if (_action->property_sensitive ()) {
1195 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1197 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1202 ArdourButton::set_layout_ellipsize_width (int w)
1204 if (_layout_ellipsize_width == w) {
1207 _layout_ellipsize_width = w;
1211 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1212 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1214 if (is_realized ()) {
1220 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1222 if (_ellipsis == e) {
1229 _layout->set_ellipsize(_ellipsis);
1230 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1231 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1233 if (is_realized ()) {
1239 ArdourButton::ensure_layout ()
1243 _layout = Pango::Layout::create (get_pango_context());
1244 _layout->set_ellipsize(_ellipsis);
1245 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1246 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1252 ArdourButton::recalc_char_pixel_geometry ()
1254 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1258 // NB. this is not static, since the geometry is different
1259 // depending on the font used.
1261 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1262 _layout->set_text (x);
1263 _layout->get_pixel_size (w, h);
1264 _char_pixel_height = std::max(4, h);
1265 // number of actual chars in the string (not bytes)
1266 // Glib to the rescue.
1267 Glib::ustring gx(x);
1268 _char_avg_pixel_width = w / (float)gx.size();
1269 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1270 set_text_internal (); /* restore display text */
1274 ArdourButton::action_visibility_changed ()
1276 if (_action->property_visible ()) {
1284 ArdourButton::action_tooltip_changed ()
1286 string str = _action->property_tooltip().get_value();
1287 set_tooltip (*this, str);
1291 ArdourButton::set_elements (Element e)
1294 CairoWidget::set_dirty ();
1298 ArdourButton::add_elements (Element e)
1300 _elements = (ArdourButton::Element) (_elements | e);
1301 CairoWidget::set_dirty ();
1305 ArdourButton::set_icon (ArdourIcon::Icon i)
1308 _icon_render_cb = 0;
1309 _icon_render_cb_data = 0;
1310 _elements = (ArdourButton::Element) ((_elements | VectorIcon) & ~(ArdourButton::Text | IconRenderCallback));
1311 CairoWidget::set_dirty ();
1315 ArdourButton::set_icon (rendercallback_t cb, void* d)
1318 _elements = (ArdourButton::Element) ((_elements | ArdourButton::Text) & ~(IconRenderCallback | VectorIcon));
1319 _icon_render_cb = 0;
1320 _icon_render_cb_data = 0;
1322 _elements = (ArdourButton::Element) ((_elements | IconRenderCallback) & ~(ArdourButton::Text | VectorIcon));
1323 _icon_render_cb = cb;
1324 _icon_render_cb_data = d;
1326 CairoWidget::set_dirty ();
1330 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1332 if (led_custom_color == c && use_custom_led_color == useit) {
1336 led_custom_color = c;
1337 use_custom_led_color = useit;
1338 CairoWidget::set_dirty ();