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 "widgets/ardour_button.h"
39 #include "widgets/tooltips.h"
40 #include "widgets/ui_config.h"
44 #define BASELINESTRETCH (1.25)
45 #define TRACKHEADERBTNW (3.10)
50 using namespace ArdourWidgets;
55 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
56 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
57 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
59 ArdourButton::ArdourButton (Element e)
63 , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
65 , _icon_render_cb_data (0)
66 , _tweaks (Tweaks (0))
67 , _char_pixel_width (0)
68 , _char_pixel_height (0)
69 , _char_avg_pixel_width (0)
73 , _corner_radius (3.5)
78 , fill_inactive_color (0)
79 , fill_active_color (0)
80 , text_active_color(0)
81 , text_inactive_color(0)
83 , led_inactive_color(0)
84 , led_custom_color (0)
85 , use_custom_led_color (false)
88 , led_inset_pattern (0)
90 , _act_on_release (true)
92 , _distinct_led_click (false)
95 , _fixed_colors_set (false)
96 , _fallthrough_to_parent (false)
97 , _layout_ellipsize_width (-1)
98 , _ellipsis (Pango::ELLIPSIZE_NONE)
99 , _update_colors (true)
100 , _pattern_height (0)
102 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
103 /* This is not provided by gtkmm */
104 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
107 ArdourButton::ArdourButton (const std::string& str, Element e)
111 , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
112 , _tweaks (Tweaks (0))
113 , _char_pixel_width (0)
114 , _char_pixel_height (0)
115 , _char_avg_pixel_width (0)
119 , _corner_radius (3.5)
124 , fill_inactive_color (0)
125 , fill_active_color (0)
126 , text_active_color(0)
127 , text_inactive_color(0)
128 , led_active_color(0)
129 , led_inactive_color(0)
130 , led_custom_color (0)
131 , use_custom_led_color (false)
133 , concave_pattern (0)
134 , led_inset_pattern (0)
136 , _act_on_release (true)
138 , _distinct_led_click (false)
141 , _fixed_colors_set (false)
142 , _fallthrough_to_parent (false)
143 , _layout_ellipsize_width (-1)
144 , _ellipsis (Pango::ELLIPSIZE_NONE)
145 , _update_colors (true)
146 , _pattern_height (0)
149 UIConfigurationBase::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
150 UIConfigurationBase::instance().DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
151 /* This is not provided by gtkmm */
152 signal_grab_broken_event().connect (sigc::mem_fun (*this, &ArdourButton::on_grab_broken_event));
155 ArdourButton::~ArdourButton()
159 if (convex_pattern) {
160 cairo_pattern_destroy (convex_pattern);
163 if (concave_pattern) {
164 cairo_pattern_destroy (concave_pattern);
167 if (led_inset_pattern) {
168 cairo_pattern_destroy (led_inset_pattern);
173 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
177 _layout->set_font_description (fd);
179 _char_pixel_width = 0;
180 _char_pixel_height = 0;
185 ArdourButton::set_text_internal () {
188 _layout->set_markup (_text);
190 _layout->set_text (_text);
195 ArdourButton::set_text (const std::string& str, bool markup)
197 if (!(_elements & Text)) {
200 if (_text == str && _markup == markup) {
206 if (!is_realized()) {
210 if (_layout && _layout->get_text() != _text) {
211 set_text_internal ();
212 /* on_size_request() will fill in _text_width/height
213 * so queue it even if _sizing_text != "" */
219 ArdourButton::set_sizing_text (const std::string& str)
221 if (_sizing_text == str) {
225 if (!is_realized()) {
235 ArdourButton::set_angle (const double angle)
241 ArdourButton::set_alignment (const float xa, const float ya)
248 /* TODO make this a dedicated function elsewhere.
251 * virtual ArdourButton::render_vector_icon()
252 * ArdourIconButton::render_vector_icon
255 * ARDOUR_UI_UTILS::render_vector_icon()
258 ArdourButton::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
260 cairo_t* cr = ctx->cobj();
265 const float corner_radius = std::max(2.f, _corner_radius * UIConfigurationBase::instance().get_ui_scale());
267 if (_update_colors) {
270 if (get_height() != _pattern_height) {
274 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
275 text_color = text_active_color;
276 led_color = led_active_color;
278 text_color = text_inactive_color;
279 led_color = led_inactive_color;
282 if (use_custom_led_color) {
283 led_color = led_custom_color;
286 void (*rounded_function)(cairo_t*, double, double, double, double, double);
288 switch (_corner_mask) {
289 case 0x1: /* upper left only */
290 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
292 case 0x2: /* upper right only */
293 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
295 case 0x3: /* upper only */
296 rounded_function = Gtkmm2ext::rounded_top_rectangle;
298 /* should really have functions for lower right, lower left,
299 lower only, but for now, we don't
302 rounded_function = Gtkmm2ext::rounded_rectangle;
305 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
306 if ((_elements & (Body|Edge)) == (Body|Edge)) {
307 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
308 cairo_set_source_rgba (cr, 0, 0, 0, 1);
313 if ((_elements & Body)==Body) {
314 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
315 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
316 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
318 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
320 ArdourCanvas::set_source_rgba (cr, fill_active_color);
322 } else { //inactive, or it has an indicator
324 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
329 // IMPLICIT ACTIVE: draw a border of the active color
330 if ((_elements & Body)==Body) {
331 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
332 cairo_set_line_width (cr, 2.0);
333 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, corner_radius-0.5);
334 ArdourCanvas::set_source_rgba (cr, fill_active_color);
339 //show the "convex" or "concave" gradient
340 if (!_flat_buttons && (_elements & Body)==Body) {
341 if ( active_state() == Gtkmm2ext::ExplicitActive && ( !((_elements & Indicator)==Indicator) || use_custom_led_color) ) {
343 cairo_set_source (cr, concave_pattern);
344 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
347 cairo_set_source (cr, convex_pattern);
348 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
355 double x = rint((get_width() - _pixbuf->get_width()) * .5);
356 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
357 #if 0 // DEBUG style (print on hover)
358 if (_hovering || (_elements & Inactive)) {
359 printf("%s: p:%dx%d (%dx%d)\n",
361 _pixbuf->get_width(), _pixbuf->get_height(),
362 get_width(), get_height());
365 if (_elements & Menu) {
366 //if this is a DropDown with an icon, then we need to
367 //move the icon left slightly to accomomodate the arrow
370 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
371 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
374 else /* VectorIcon, IconRenderCallback are exclusive to Pixbuf Icons */
375 if (_elements & (VectorIcon | IconRenderCallback)) {
376 int vw = get_width();
377 int vh = get_height();
378 if (_elements & Menu) {
381 if (_elements & VectorIcon) {
382 Gtkmm2ext::ArdourIcon::render (cr, _icon, vw, vh, active_state(), text_color);
385 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius + 1.5);
387 _icon_render_cb (cr, vw, vh, text_color, _icon_render_cb_data);
392 const int text_margin = char_pixel_width();
394 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
396 #if 0 // DEBUG style (print on hover)
397 if (_hovering || (_elements & Inactive)) {
398 bool layout_font = true;
399 Pango::FontDescription fd = _layout->get_font_description();
400 if (fd.gobj() == NULL) {
402 fd = get_pango_context()->get_font_description();
404 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
406 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
407 ceil(char_pixel_height() * BASELINESTRETCH),
408 _text_width, _text_height,
409 get_width(), get_height(),
410 layout_font ? "L:" : "W:",
411 fd.to_string().c_str());
416 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
420 ArdourCanvas::set_source_rgba (cr, text_color);
421 const double text_ypos = (get_height() - _text_height) * .5;
423 if (_elements & Menu) {
424 // always left align (dropdown)
425 cairo_move_to (cr, text_margin, text_ypos);
426 pango_cairo_show_layout (cr, _layout->gobj());
427 } else if ( (_elements & Indicator) == Indicator) {
428 // left/right align depending on LED position
430 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
432 cairo_move_to (cr, text_margin, text_ypos);
434 pango_cairo_show_layout (cr, _layout->gobj());
436 /* centered text otherwise */
443 cairo_get_matrix (cr, &m1);
444 cairo_matrix_t m2 = m1;
447 cairo_set_matrix (cr, &m2);
450 cairo_rotate(cr, _angle * M_PI / 180.0);
453 cairo_device_to_user(cr, &ww, &wh);
454 xa = text_margin + (ww - _text_width - 2 * text_margin) * _xalign;
455 ya = (wh - _text_height) * _yalign;
457 /* quick hack for left/bottom alignment at -90deg
458 * TODO this should be generalized incl rotation.
459 * currently only 'user' of this API is meter_strip.cc
461 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
463 cairo_move_to (cr, xa + m1.x0, ya + m1.y0);
464 pango_cairo_update_layout(cr, _layout->gobj());
465 pango_cairo_show_layout (cr, _layout->gobj());
471 if (_elements & Menu) {
472 const float trih = ceil(_diameter * .5);
473 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
475 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
476 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
477 cairo_rel_line_to(cr, -triw2, -trih);
478 cairo_rel_line_to(cr, 2. * triw2, 0);
479 cairo_close_path(cr);
481 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
484 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
485 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
486 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
487 cairo_close_path(cr);
488 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
489 cairo_set_line_width(cr, 1);
494 if (_elements & Indicator) {
497 /* move to the center of the indicator/led */
498 if (_elements & Text) {
499 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
501 cairo_translate (cr, led_xoff, get_height() * .5);
503 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
506 cairo_translate (cr, get_width() * .5, get_height() * .5);
510 if (!_flat_buttons) {
511 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
512 cairo_set_source (cr, led_inset_pattern);
517 cairo_set_source_rgb (cr, 0, 0, 0);
518 cairo_arc (cr, 0, 0, _diameter * .5 - 1 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
522 ArdourCanvas::set_source_rgba (cr, led_color);
523 cairo_arc (cr, 0, 0, _diameter * .5 - 3 * UIConfigurationBase::instance().get_ui_scale(), 0, 2 * M_PI);
529 // a transparent overlay to indicate insensitivity
530 if ((visual_state() & Gtkmm2ext::Insensitive)) {
531 rounded_function (cr, 0, 0, get_width(), get_height(), corner_radius);
532 uint32_t ins_color = UIConfigurationBase::instance().color ("gtk_background");
533 ArdourCanvas::set_source_rgb_a (cr, ins_color, 0.6);
537 // if requested, show hovering
538 if (UIConfigurationBase::instance().get_widget_prelight()
539 && !((visual_state() & Gtkmm2ext::Insensitive))) {
541 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
542 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
547 //user is currently pressing the button. dark outline helps to indicate this
548 if (_grabbed && !(_elements & (Inactive|Menu))) {
549 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, corner_radius);
550 cairo_set_line_width(cr, 2);
551 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
555 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
556 if (visual_state() & Gtkmm2ext::Selected) {
557 cairo_set_line_width(cr, 1);
558 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
559 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, corner_radius);
563 //I guess this means we have keyboard focus. I don't think this works currently
565 //A: yes, it's keyboard focus and it does work when there's no editor window
566 // (the editor is always the first receiver for KeyDown).
567 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
569 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, corner_radius);
570 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
572 cairo_set_dash (cr, &dashes, 1, 0);
573 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
574 cairo_set_line_width (cr, 1.0);
576 cairo_set_dash (cr, 0, 0, 0);
581 ArdourButton::set_corner_radius (float r)
584 CairoWidget::set_dirty ();
588 ArdourButton::on_realize()
590 CairoWidget::on_realize ();
593 if (_layout->get_text() != _text) {
594 set_text_internal ();
601 ArdourButton::on_size_request (Gtk::Requisition* req)
603 req->width = req->height = 0;
604 CairoWidget::on_size_request (req);
606 if (_diameter == 0) {
607 const float newdia = rintf (11.f * UIConfigurationBase::instance().get_ui_scale());
608 if (_diameter != newdia) {
614 if (_elements & Text) {
617 set_text_internal ();
619 /* render() needs the size of the displayed text */
620 _layout->get_pixel_size (_text_width, _text_height);
622 if (_tweaks & OccasionalText) {
624 /* size should not change based on presence or absence
628 } else /*if (!_text.empty() || !_sizing_text.empty()) */ {
630 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
631 req->width += rint(1.75 * char_pixel_width()); // padding
633 if (!_sizing_text.empty()) {
634 _layout->set_text (_sizing_text); /* use sizing text */
637 int sizing_text_width = 0, sizing_text_height = 0;
638 _layout->get_pixel_size (sizing_text_width, sizing_text_height);
640 req->width += sizing_text_width;
642 if (!_sizing_text.empty()) {
643 set_text_internal (); /* restore display text */
647 /* XXX hack (surprise). Deal with two common rotation angles */
649 if (_angle == 90 || _angle == 270) {
650 /* do not swap text width or height because we rely on
651 these being the un-rotated values in ::render()
653 swap (req->width, req->height);
662 req->width += _pixbuf->get_width() + char_pixel_width();
663 req->height = std::max(req->height, _pixbuf->get_height() + 4);
666 if (_elements & Indicator) {
667 req->width += lrint (_diameter) + char_pixel_width();
668 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
671 if ((_elements & Menu)) {
672 req->width += _diameter + 4;
675 if (_elements & (VectorIcon | IconRenderCallback)) {
676 assert(!(_elements & Text));
677 const int wh = std::max (6., std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.)));
679 req->height = std::max(req->height, wh);
682 /* Tweaks to mess the nice stuff above up again. */
683 if (_tweaks & TrackHeader) {
684 // forget everything above and just use a fixed square [em] size
685 // "TrackHeader Buttons" are single letter (usually uppercase)
686 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
687 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
691 else if (_tweaks & Square) {
692 // currerntly unused (again)
693 if (req->width < req->height)
694 req->width = req->height;
695 if (req->height < req->width)
696 req->height = req->width;
697 } else if (_sizing_text.empty() && _text_width > 0 && !(_elements & Menu)) {
698 // properly centered text for those elements that are centered
699 // (no sub-pixel offset)
700 if ((req->width - _text_width) & 1) { ++req->width; }
701 if ((req->height - _text_height) & 1) { ++req->height; }
704 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
709 * This sets the colors used for rendering based on the name of the button, and
710 * thus uses information from the GUI config data.
713 ArdourButton::set_colors ()
715 _update_colors = false;
717 if (_fixed_colors_set == 0x3) {
721 std::string name = get_name();
724 if (!(_fixed_colors_set & 0x1)) {
725 fill_active_color = UIConfigurationBase::instance().color (string_compose ("%1: fill active", name), &failed);
727 fill_active_color = UIConfigurationBase::instance().color ("generic button: fill active");
731 if (!(_fixed_colors_set & 0x2)) {
732 fill_inactive_color = UIConfigurationBase::instance().color (string_compose ("%1: fill", name), &failed);
734 fill_inactive_color = UIConfigurationBase::instance().color ("generic button: fill");
738 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
739 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
741 led_active_color = UIConfigurationBase::instance().color (string_compose ("%1: led active", name), &failed);
743 led_active_color = UIConfigurationBase::instance().color ("generic button: led active");
746 /* The inactive color for the LED is just a fairly dark version of the
750 ArdourCanvas::HSV inactive (led_active_color);
753 led_inactive_color = inactive.color ();
757 * This sets the colors used for rendering based on two fixed values, rather
758 * than basing them on the button name, and thus information in the GUI config
761 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
763 set_active_color (color_active);
764 set_inactive_color (color_inactive);
767 void ArdourButton::set_active_color (const uint32_t color)
769 _fixed_colors_set |= 0x1;
771 fill_active_color = color;
773 unsigned char r, g, b, a;
774 UINT_TO_RGBA(color, &r, &g, &b, &a);
776 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
777 (max (double(g), 255.) - min (double(g), 255.)) +
778 (max (double(b), 255.) - min (double(b), 255.));
780 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
781 (max (double(g), 0.) - min (double(g), 0.)) +
782 (max (double(b), 0.) - min (double(b), 0.));
784 text_active_color = (white_contrast > black_contrast) ?
785 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
786 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
788 /* XXX what about led colors ? */
789 CairoWidget::set_dirty ();
792 void ArdourButton::set_inactive_color (const uint32_t color)
794 _fixed_colors_set |= 0x2;
796 fill_inactive_color = color;
798 unsigned char r, g, b, a;
799 UINT_TO_RGBA(color, &r, &g, &b, &a);
801 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
802 (max (double(g), 255.) - min (double(g), 255.)) +
803 (max (double(b), 255.) - min (double(b), 255.));
805 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
806 (max (double(g), 0.) - min (double(g), 0.)) +
807 (max (double(b), 0.) - min (double(b), 0.));
809 text_inactive_color = (white_contrast > black_contrast) ?
810 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
811 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
813 /* XXX what about led colors ? */
814 CairoWidget::set_dirty ();
818 ArdourButton::build_patterns ()
820 if (convex_pattern) {
821 cairo_pattern_destroy (convex_pattern);
825 if (concave_pattern) {
826 cairo_pattern_destroy (concave_pattern);
830 if (led_inset_pattern) {
831 cairo_pattern_destroy (led_inset_pattern);
832 led_inset_pattern = 0;
836 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
837 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
838 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
841 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
842 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
843 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
845 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
846 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
847 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
849 _pattern_height = get_height() ;
853 ArdourButton::set_led_left (bool yn)
859 ArdourButton::on_button_press_event (GdkEventButton *ev)
861 focus_handler (this);
863 if (ev->button == 1 && (_elements & Indicator) && _led_rect && _distinct_led_click) {
864 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
865 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
870 if (binding_proxy.button_press_handler (ev)) {
875 CairoWidget::set_dirty ();
877 if (ev->button == 1 && !_act_on_release) {
879 _action->activate ();
884 if (_fallthrough_to_parent)
891 ArdourButton::on_button_release_event (GdkEventButton *ev)
893 if (ev->button == 1 && _hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
894 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
895 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
896 signal_led_clicked(ev); /* EMIT SIGNAL */
902 CairoWidget::set_dirty ();
904 if (ev->button == 1 && _hovering) {
906 if (_act_on_release) {
908 _action->activate ();
914 if (_fallthrough_to_parent)
921 ArdourButton::set_distinct_led_click (bool yn)
923 _distinct_led_click = yn;
928 ArdourButton::color_handler ()
930 _update_colors = true;
931 CairoWidget::set_dirty ();
935 ArdourButton::on_size_allocate (Allocation& alloc)
937 CairoWidget::on_size_allocate (alloc);
942 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
944 watch_connection.disconnect ();
945 binding_proxy.set_controllable (c);
949 ArdourButton::watch ()
951 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
954 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
957 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
961 ArdourButton::controllable_changed ()
963 float val = binding_proxy.get_controllable()->get_value();
965 if (fabs (val) >= 0.5f) {
966 set_active_state (Gtkmm2ext::ExplicitActive);
968 unset_active_state ();
970 CairoWidget::set_dirty ();
974 ArdourButton::set_related_action (RefPtr<Action> act)
976 Gtkmm2ext::Activatable::set_related_action (act);
980 action_tooltip_changed ();
981 action_sensitivity_changed ();
983 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
986 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
989 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
990 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
991 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
996 ArdourButton::action_toggled ()
998 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
1001 if (tact->get_active()) {
1002 set_active_state (Gtkmm2ext::ExplicitActive);
1004 unset_active_state ();
1010 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
1012 _update_colors = true;
1013 CairoWidget::set_dirty ();
1014 _char_pixel_width = 0;
1015 _char_pixel_height = 0;
1016 if (is_realized()) {
1022 ArdourButton::on_name_changed ()
1024 _char_pixel_width = 0;
1025 _char_pixel_height = 0;
1027 _update_colors = true;
1028 if (is_realized()) {
1034 ArdourButton::setup_led_rect ()
1036 if (!(_elements & Indicator)) {
1043 _led_rect = new cairo_rectangle_t;
1046 if (_elements & Text) {
1048 _led_rect->x = char_pixel_width();
1050 _led_rect->x = get_width() - char_pixel_width() + _diameter;
1054 _led_rect->x = .5 * get_width() - _diameter;
1057 _led_rect->y = .5 * (get_height() - _diameter);
1058 _led_rect->width = _diameter;
1059 _led_rect->height = _diameter;
1063 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
1065 _elements = (ArdourButton::Element) (_elements & ~ArdourButton::Text);
1067 if (is_realized()) {
1073 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
1075 bool changed = (_active_state != s);
1076 CairoWidget::set_active_state (s);
1078 _update_colors = true;
1079 CairoWidget::set_dirty ();
1084 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1086 bool changed = (_visual_state != s);
1087 CairoWidget::set_visual_state (s);
1089 _update_colors = true;
1090 CairoWidget::set_dirty ();
1095 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1098 CairoWidget::set_dirty ();
1099 return CairoWidget::on_focus_in_event (ev);
1103 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1106 CairoWidget::set_dirty ();
1107 return CairoWidget::on_focus_out_event (ev);
1111 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1112 if (_act_on_release && _focused &&
1113 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1117 _action->activate ();
1121 return CairoWidget::on_key_release_event (ev);
1125 ArdourButton::on_key_press_event (GdkEventKey *ev) {
1126 if (!_act_on_release && _focused &&
1127 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1131 _action->activate ();
1135 return CairoWidget::on_key_release_event (ev);
1139 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1141 _hovering = (_elements & Inactive) ? false : true;
1143 if (UIConfigurationBase::instance().get_widget_prelight()) {
1144 CairoWidget::set_dirty ();
1147 boost::shared_ptr<PBD::Controllable> c (binding_proxy.get_controllable ());
1149 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
1152 return CairoWidget::on_enter_notify_event (ev);
1156 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1160 if (UIConfigurationBase::instance().get_widget_prelight()) {
1161 CairoWidget::set_dirty ();
1164 if (binding_proxy.get_controllable()) {
1165 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
1168 return CairoWidget::on_leave_notify_event (ev);
1172 ArdourButton::on_grab_broken_event(GdkEventGrabBroken* grab_broken_event) {
1173 /* Our implicit grab due to a button_press was broken by another grab:
1174 * the button will not get any button_release event if the mouse leaves
1175 * while the grab is taken, so unpress ourselves */
1177 CairoWidget::set_dirty ();
1182 ArdourButton::set_tweaks (Tweaks t)
1186 if (is_realized()) {
1193 ArdourButton::action_sensitivity_changed ()
1195 if (_action->property_sensitive ()) {
1196 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1198 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1203 ArdourButton::set_layout_ellipsize_width (int w)
1205 if (_layout_ellipsize_width == w) {
1208 _layout_ellipsize_width = w;
1212 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1213 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1215 if (is_realized ()) {
1221 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1223 if (_ellipsis == e) {
1230 _layout->set_ellipsize(_ellipsis);
1231 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1232 _layout->set_width (_layout_ellipsize_width - 3 * PANGO_SCALE);
1234 if (is_realized ()) {
1240 ArdourButton::ensure_layout ()
1244 _layout = Pango::Layout::create (get_pango_context());
1245 _layout->set_ellipsize(_ellipsis);
1246 if (_layout_ellipsize_width > 3 * PANGO_SCALE) {
1247 _layout->set_width (_layout_ellipsize_width - 3* PANGO_SCALE);
1253 ArdourButton::recalc_char_pixel_geometry ()
1255 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1259 // NB. this is not static, since the geometry is different
1260 // depending on the font used.
1262 std::string x = _("@ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1263 _layout->set_text (x);
1264 _layout->get_pixel_size (w, h);
1265 _char_pixel_height = std::max(4, h);
1266 // number of actual chars in the string (not bytes)
1267 // Glib to the rescue.
1268 Glib::ustring gx(x);
1269 _char_avg_pixel_width = w / (float)gx.size();
1270 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1271 set_text_internal (); /* restore display text */
1275 ArdourButton::action_visibility_changed ()
1277 if (_action->property_visible ()) {
1285 ArdourButton::action_tooltip_changed ()
1287 string str = _action->property_tooltip().get_value();
1288 set_tooltip (*this, str);
1292 ArdourButton::set_elements (Element e)
1295 CairoWidget::set_dirty ();
1299 ArdourButton::add_elements (Element e)
1301 _elements = (ArdourButton::Element) (_elements | e);
1302 CairoWidget::set_dirty ();
1306 ArdourButton::set_icon (Gtkmm2ext::ArdourIcon::Icon i)
1309 _icon_render_cb = 0;
1310 _icon_render_cb_data = 0;
1311 _elements = (ArdourButton::Element) ((_elements | VectorIcon) & ~(ArdourButton::Text | IconRenderCallback));
1312 CairoWidget::set_dirty ();
1316 ArdourButton::set_icon (rendercallback_t cb, void* d)
1319 _elements = (ArdourButton::Element) ((_elements | ArdourButton::Text) & ~(IconRenderCallback | VectorIcon));
1320 _icon_render_cb = 0;
1321 _icon_render_cb_data = 0;
1323 _elements = (ArdourButton::Element) ((_elements | IconRenderCallback) & ~(ArdourButton::Text | VectorIcon));
1324 _icon_render_cb = cb;
1325 _icon_render_cb_data = d;
1327 CairoWidget::set_dirty ();
1331 ArdourButton::set_custom_led_color (uint32_t c, bool useit)
1333 if (led_custom_color == c && use_custom_led_color == useit) {
1337 led_custom_color = c;
1338 use_custom_led_color = useit;
1339 CairoWidget::set_dirty ();