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/error.h"
28 #include "pbd/stacktrace.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32 #include "gtkmm2ext/gui_thread.h"
34 #include "ardour/rc_configuration.h" // for widget prelight preference
36 #include "canvas/utils.h"
37 #include "canvas/colors.h"
39 #include "ardour_button.h"
40 #include "ardour_ui.h"
41 #include "global_signals.h"
45 #define BASELINESTRETCH (1.25)
46 #define TRACKHEADERBTNW (3.10)
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 , _tweaks (Tweaks (0))
63 , _char_pixel_width (0)
64 , _char_pixel_height (0)
65 , _char_avg_pixel_width (0)
69 , _corner_radius (2.5)
74 , fill_inactive_color (0)
75 , fill_active_color (0)
76 , text_active_color(0)
77 , text_inactive_color(0)
79 , led_inactive_color(0)
82 , led_inset_pattern (0)
84 , _act_on_release (true)
86 , _distinct_led_click (false)
89 , _fixed_colors_set (false)
90 , _fallthrough_to_parent (false)
91 , _layout_ellipsize_width (-1)
92 , _ellipsis (Pango::ELLIPSIZE_NONE)
93 , _update_colors (true)
96 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
99 ArdourButton::ArdourButton (const std::string& str, Element e)
101 , _tweaks (Tweaks (0))
105 , _corner_radius (2.5)
110 , fill_inactive_color (0)
111 , fill_active_color (0)
112 , text_active_color(0)
113 , text_inactive_color(0)
114 , led_active_color(0)
115 , led_inactive_color(0)
117 , concave_pattern (0)
118 , led_inset_pattern (0)
120 , _act_on_release (true)
122 , _distinct_led_click (false)
125 , _fixed_colors_set (false)
126 , _fallthrough_to_parent (false)
127 , _layout_ellipsize_width (-1)
128 , _ellipsis (Pango::ELLIPSIZE_NONE)
129 , _update_colors (true)
130 , _pattern_height (0)
133 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
134 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
137 ArdourButton::~ArdourButton()
141 if (convex_pattern) {
142 cairo_pattern_destroy (convex_pattern);
145 if (concave_pattern) {
146 cairo_pattern_destroy (concave_pattern);
149 if (led_inset_pattern) {
150 cairo_pattern_destroy (led_inset_pattern);
155 ArdourButton::set_layout_font (const Pango::FontDescription& fd)
159 _layout->set_font_description (fd);
165 ArdourButton::set_text (const std::string& str)
168 if (!is_realized()) {
172 if (_layout && _layout->get_text() != _text) {
173 _layout->set_text (_text);
179 ArdourButton::set_angle (const double angle)
185 ArdourButton::set_alignment (const float xa, const float ya)
192 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
197 if (_update_colors) {
200 if (get_height() != _pattern_height) {
204 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
205 text_color = text_active_color;
206 led_color = led_active_color;
208 text_color = text_inactive_color;
209 led_color = led_inactive_color;
212 void (*rounded_function)(cairo_t*, double, double, double, double, double);
214 switch (_corner_mask) {
215 case 0x1: /* upper left only */
216 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
218 case 0x2: /* upper right only */
219 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
221 case 0x3: /* upper only */
222 rounded_function = Gtkmm2ext::rounded_top_rectangle;
224 /* should really have functions for lower right, lower left,
225 lower only, but for now, we don't
228 rounded_function = Gtkmm2ext::rounded_rectangle;
231 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
232 if ((_elements & (Body|Edge)) == (Body|Edge)) {
233 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
234 cairo_set_source_rgba (cr, 0, 0, 0, 1);
239 if ((_elements & Body)==Body) {
240 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
241 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
242 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
244 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
246 ArdourCanvas::set_source_rgba (cr, fill_active_color);
248 } else { //inactive, or it has an indicator
250 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
255 // IMPLICIT ACTIVE: draw a border of the active color
256 if ((_elements & Body)==Body) {
257 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
258 cairo_set_line_width (cr, 2.0);
259 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
260 ArdourCanvas::set_source_rgba (cr, fill_active_color);
265 //show the "convex" or "concave" gradient
266 if (!_flat_buttons) {
267 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
269 cairo_set_source (cr, concave_pattern);
270 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
273 cairo_set_source (cr, convex_pattern);
274 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
281 double x = rint((get_width() - _pixbuf->get_width()) * .5);
282 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
283 #if 0 // DEBUG style (print on hover)
284 if (_hovering || (_elements & Inactive)) {
285 printf("%s: p:%dx%d (%dx%d)\n",
287 _pixbuf->get_width(), _pixbuf->get_height(),
288 get_width(), get_height());
291 if (_elements & Menu) {
292 //if this is a DropDown with an icon, then we need to
293 //move the icon left slightly to accomomodate the arrow
296 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
297 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
300 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
301 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
302 const double x = get_width() * .5;
303 const double y = get_height() * .5;
304 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
305 const double slit = .11 * M_PI;
307 cairo_translate(cr, x, y);
309 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
310 if (active_state() == Gtkmm2ext::ExplicitActive)
311 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
313 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
314 cairo_fill_preserve(cr);
315 cairo_set_source_rgba (cr, .0, .0, .0, .5);
316 cairo_set_line_width(cr, 1);
320 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
322 cairo_rotate (cr, -.5 * M_PI);
323 cairo_move_to(cr, 0, 0);
324 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
325 cairo_line_to(cr, 0, 0);
326 cairo_close_path(cr);
329 cairo_rotate (cr, 2. * M_PI / 3.);
331 cairo_move_to(cr, 0, 0);
332 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
333 cairo_line_to(cr, 0, 0);
334 cairo_close_path(cr);
337 cairo_rotate (cr, 2. * M_PI / 3.);
338 cairo_move_to(cr, 0, 0);
339 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
340 cairo_line_to(cr, 0, 0);
341 cairo_close_path(cr);
346 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
347 if (active_state() == Gtkmm2ext::ExplicitActive)
348 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
350 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
352 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
353 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
358 else if (_elements & RecButton) {
359 const double x = get_width() * .5;
360 const double y = get_height() * .5;
361 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
362 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
363 if (active_state() == Gtkmm2ext::ExplicitActive)
364 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
366 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
367 cairo_fill_preserve(cr);
368 cairo_set_source_rgba (cr, .0, .0, .0, .8);
369 cairo_set_line_width(cr, 1);
372 else if (_elements & CloseCross) {
373 const double x = get_width() * .5;
374 const double y = get_height() * .5;
375 const double o = .5 + std::min(x, y) * .4;
376 ArdourCanvas::set_source_rgba (cr, text_color);
377 cairo_set_line_width(cr, 1);
378 cairo_move_to(cr, x-o, y-o);
379 cairo_line_to(cr, x+o, y+o);
380 cairo_move_to(cr, x+o, y-o);
381 cairo_line_to(cr, x-o, y+o);
385 const int text_margin = char_pixel_width();
387 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
389 #if 0 // DEBUG style (print on hover)
390 if (_hovering || (_elements & Inactive)) {
391 bool layout_font = true;
392 Pango::FontDescription fd = _layout->get_font_description();
393 if (fd.gobj() == NULL) {
395 fd = get_pango_context()->get_font_description();
397 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
399 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
400 ceil(char_pixel_height() * BASELINESTRETCH),
401 _text_width, _text_height,
402 get_width(), get_height(),
403 layout_font ? "L:" : "W:",
404 fd.to_string().c_str());
409 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
413 ArdourCanvas::set_source_rgba (cr, text_color);
414 const double text_ypos = (get_height() - _text_height) * .5;
416 if (_elements & Menu) {
417 // always left align (dropdown)
418 cairo_move_to (cr, text_margin, text_ypos);
419 pango_cairo_show_layout (cr, _layout->gobj());
420 } else if ( (_elements & Indicator) == Indicator) {
421 // left/right align depending on LED position
423 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
425 cairo_move_to (cr, text_margin, text_ypos);
427 pango_cairo_show_layout (cr, _layout->gobj());
429 /* centered text otherwise */
436 cairo_rotate(cr, _angle * M_PI / 180.0);
437 cairo_device_to_user(cr, &ww, &wh);
438 xa = (ww - _text_width) * _xalign;
439 ya = (wh - _text_height) * _yalign;
441 /* quick hack for left/bottom alignment at -90deg
442 * TODO this should be generalized incl rotation.
443 * currently only 'user' of this API is meter_strip.cc
445 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
447 cairo_move_to (cr, xa, ya);
448 pango_cairo_update_layout(cr, _layout->gobj());
449 pango_cairo_show_layout (cr, _layout->gobj());
456 if (_elements & Menu) {
457 const float trih = ceil(_diameter * .5);
458 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
460 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
461 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
462 cairo_rel_line_to(cr, -triw2, -trih);
463 cairo_rel_line_to(cr, 2. * triw2, 0);
464 cairo_close_path(cr);
466 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
469 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
470 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
471 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
472 cairo_close_path(cr);
473 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
474 cairo_set_line_width(cr, 1);
479 if (_elements & Indicator) {
482 /* move to the center of the indicator/led */
483 if (_elements & Text) {
484 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
486 cairo_translate (cr, led_xoff, get_height() * .5);
488 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
491 cairo_translate (cr, get_width() * .5, get_height() * .5);
495 if (!_flat_buttons) {
496 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
497 cairo_set_source (cr, led_inset_pattern);
502 cairo_set_source_rgb (cr, 0, 0, 0);
503 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
507 ArdourCanvas::set_source_rgba (cr, led_color);
508 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
514 // a transparent gray layer to indicate insensitivity
515 if ((visual_state() & Gtkmm2ext::Insensitive)) {
516 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
517 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
521 // if requested, show hovering
522 if (ARDOUR::Config->get_widget_prelight()
523 && !((visual_state() & Gtkmm2ext::Insensitive))) {
525 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
526 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
531 //user is currently pressing the button. dark outline helps to indicate this
532 if (_grabbed && !(_elements & (Inactive|Menu))) {
533 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
534 cairo_set_line_width(cr, 2);
535 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
539 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
540 if (visual_state() & Gtkmm2ext::Selected) {
541 cairo_set_line_width(cr, 1);
542 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
543 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
547 //I guess this means we have keyboard focus. I don't think this works currently
549 //A: yes, it's keyboard focus and it does work when there's no editor window
550 // (the editor is always the first receiver for KeyDown).
551 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
553 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
554 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
556 cairo_set_dash (cr, &dashes, 1, 0);
557 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
558 cairo_set_line_width (cr, 1.0);
560 cairo_set_dash (cr, 0, 0, 0);
565 ArdourButton::set_corner_radius (float r)
568 CairoWidget::set_dirty ();
572 ArdourButton::on_realize()
574 CairoWidget::on_realize ();
576 if (_layout && _layout->get_text() != _text) {
577 _layout->set_text (_text);
583 ArdourButton::on_size_request (Gtk::Requisition* req)
585 req->width = req->height = 0;
586 CairoWidget::on_size_request (req);
588 if (_diameter == 0) {
589 const float newdia = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
590 if (_diameter != newdia) {
596 if ((_elements & Text) && !_text.empty()) {
597 // if _layout does not exist, char_pixel_height() creates it,
598 req->height = std::max(req->height, (int) ceil(char_pixel_height() * BASELINESTRETCH + 1.0));
599 _layout->get_pixel_size (_text_width, _text_height);
600 req->width += rint(1.75 * char_pixel_width()); // padding
601 req->width += _text_width;
608 req->width += _pixbuf->get_width() + char_pixel_width();
609 req->height = std::max(req->height, _pixbuf->get_height() + 4);
612 if (_elements & Indicator) {
613 req->width += lrint (_diameter) + char_pixel_width();
614 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
617 if ((_elements & Menu)) {
618 req->width += _diameter + 4;
621 if (_elements & (RecButton | CloseCross)) {
622 assert(!(_elements & Text));
623 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
625 req->height = std::max(req->height, wh);
628 /* Tweaks to mess the nice stuff above up again. */
629 if (_tweaks & TrackHeader) {
630 // forget everything above and just use a fixed square [em] size
631 // "TrackHeader Buttons" are single letter (usually uppercase)
632 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
633 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
637 else if (_tweaks & Square) {
638 // currerntly unused (again)
639 if (req->width < req->height)
640 req->width = req->height;
641 if (req->height < req->width)
642 req->height = req->width;
643 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
644 // properly centered text for those elements that are centered
645 // (no sub-pixel offset)
646 if ((req->width - _text_width) & 1) { ++req->width; }
647 if ((req->height - _text_height) & 1) { ++req->height; }
650 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
655 * This sets the colors used for rendering based on the name of the button, and
656 * thus uses information from the GUI config data.
659 ArdourButton::set_colors ()
661 _update_colors = false;
662 if (_fixed_colors_set) {
665 std::string name = get_name();
668 fill_active_color = ARDOUR_UI::config()->color (string_compose ("%1: fill active", name), &failed);
670 fill_active_color = ARDOUR_UI::config()->color ("generic button: fill active");
672 fill_inactive_color = ARDOUR_UI::config()->color (string_compose ("%1: fill", name), &failed);
674 fill_inactive_color = ARDOUR_UI::config()->color ("generic button: fill");
677 text_active_color = ArdourCanvas::contrasting_text_color (fill_active_color);
678 text_inactive_color = ArdourCanvas::contrasting_text_color (fill_inactive_color);
680 led_active_color = ARDOUR_UI::config()->color (string_compose ("%1: led active", name), &failed);
682 led_active_color = ARDOUR_UI::config()->color ("generic button: led active");
685 /* The inactive color for the LED is just a fairly dark version of the
689 ArdourCanvas::HSV inactive (led_active_color);
692 led_inactive_color = inactive.color ();
696 * This sets the colors used for rendering based on two fixed values, rather
697 * than basing them on the button name, and thus information in the GUI config
700 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
702 _fixed_colors_set = true;
704 fill_active_color = color_active;
705 fill_inactive_color = color_inactive;
707 unsigned char r, g, b, a;
708 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
710 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
711 (max (double(g), 255.) - min (double(g), 255.)) +
712 (max (double(b), 255.) - min (double(b), 255.));
714 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
715 (max (double(g), 0.) - min (double(g), 0.)) +
716 (max (double(b), 0.) - min (double(b), 0.));
718 text_active_color = (white_contrast > black_contrast) ?
719 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
720 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
723 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
725 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
726 (max (double(g), 255.) - min (double(g), 255.)) +
727 (max (double(b), 255.) - min (double(b), 255.));
729 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
730 (max (double(g), 0.) - min (double(g), 0.)) +
731 (max (double(b), 0.) - min (double(b), 0.));
733 text_inactive_color = (white_contrast > black_contrast) ?
734 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
735 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
737 /* XXX what about led colors ? */
738 CairoWidget::set_dirty ();
742 ArdourButton::build_patterns ()
744 if (convex_pattern) {
745 cairo_pattern_destroy (convex_pattern);
749 if (concave_pattern) {
750 cairo_pattern_destroy (concave_pattern);
754 if (led_inset_pattern) {
755 cairo_pattern_destroy (led_inset_pattern);
756 led_inset_pattern = 0;
760 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
761 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
762 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
765 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
766 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
767 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
769 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
770 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
771 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
773 _pattern_height = get_height() ;
777 ArdourButton::set_led_left (bool yn)
783 ArdourButton::on_button_press_event (GdkEventButton *ev)
787 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
788 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
789 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
794 if (binding_proxy.button_press_handler (ev)) {
799 CairoWidget::set_dirty ();
801 if (!_act_on_release) {
803 _action->activate ();
808 if (_fallthrough_to_parent)
815 ArdourButton::on_button_release_event (GdkEventButton *ev)
817 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
818 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
819 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
820 signal_led_clicked(); /* EMIT SIGNAL */
826 CairoWidget::set_dirty ();
830 if (_act_on_release) {
832 _action->activate ();
838 if (_fallthrough_to_parent)
845 ArdourButton::set_distinct_led_click (bool yn)
847 _distinct_led_click = yn;
852 ArdourButton::color_handler ()
854 _update_colors = true;
855 CairoWidget::set_dirty ();
859 ArdourButton::on_size_allocate (Allocation& alloc)
861 CairoWidget::on_size_allocate (alloc);
866 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
868 watch_connection.disconnect ();
869 binding_proxy.set_controllable (c);
873 ArdourButton::watch ()
875 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
878 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
881 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
885 ArdourButton::controllable_changed ()
887 float val = binding_proxy.get_controllable()->get_value();
889 if (fabs (val) >= 0.5f) {
890 set_active_state (Gtkmm2ext::ExplicitActive);
892 unset_active_state ();
894 CairoWidget::set_dirty ();
898 ArdourButton::set_related_action (RefPtr<Action> act)
900 Gtkmm2ext::Activatable::set_related_action (act);
904 action_tooltip_changed ();
906 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
909 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
912 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
913 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
914 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
919 ArdourButton::action_toggled ()
921 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
924 if (tact->get_active()) {
925 set_active_state (Gtkmm2ext::ExplicitActive);
927 unset_active_state ();
933 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
935 _update_colors = true;
936 CairoWidget::set_dirty ();
940 ArdourButton::on_name_changed ()
942 _char_pixel_width = 0;
943 _char_pixel_height = 0;
945 _update_colors = true;
952 ArdourButton::setup_led_rect ()
954 if (!(_elements & Indicator)) {
961 _led_rect = new cairo_rectangle_t;
964 if (_elements & Text) {
966 _led_rect->x = char_pixel_width();
968 _led_rect->x = get_width() - char_pixel_width() + _diameter;
972 _led_rect->x = .5 * get_width() - _diameter;
975 _led_rect->y = .5 * (get_height() - _diameter);
976 _led_rect->width = _diameter;
977 _led_rect->height = _diameter;
981 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
990 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
992 bool changed = (_active_state != s);
993 CairoWidget::set_active_state (s);
995 _update_colors = true;
996 CairoWidget::set_dirty ();
1001 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
1003 bool changed = (_visual_state != s);
1004 CairoWidget::set_visual_state (s);
1006 _update_colors = true;
1007 CairoWidget::set_dirty ();
1012 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
1015 CairoWidget::set_dirty ();
1016 return CairoWidget::on_focus_in_event (ev);
1020 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
1023 CairoWidget::set_dirty ();
1024 return CairoWidget::on_focus_out_event (ev);
1028 ArdourButton::on_key_release_event (GdkEventKey *ev) {
1030 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
1034 _action->activate ();
1038 return CairoWidget::on_key_release_event (ev);
1042 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1044 _hovering = (_elements & Inactive) ? false : true;
1046 if (ARDOUR::Config->get_widget_prelight()) {
1047 CairoWidget::set_dirty ();
1050 return CairoWidget::on_enter_notify_event (ev);
1054 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1058 if (ARDOUR::Config->get_widget_prelight()) {
1059 CairoWidget::set_dirty ();
1062 return CairoWidget::on_leave_notify_event (ev);
1066 ArdourButton::set_tweaks (Tweaks t)
1070 if (is_realized()) {
1077 ArdourButton::action_sensitivity_changed ()
1079 if (_action->property_sensitive ()) {
1080 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1082 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1087 ArdourButton::set_layout_ellisize_width (int w)
1089 if (_layout_ellipsize_width == w) {
1092 _layout_ellipsize_width = w;
1096 if (_layout_ellipsize_width > 0) {
1097 _layout->set_width (_layout_ellipsize_width);
1102 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1104 if (_ellipsis == e) {
1111 _layout->set_ellipsize(_ellipsis);
1112 if (_layout_ellipsize_width > 0) {
1113 _layout->set_width (_layout_ellipsize_width);
1115 if (is_realized ()) {
1121 ArdourButton::ensure_layout ()
1125 _layout = Pango::Layout::create (get_pango_context());
1126 _layout->set_ellipsize(_ellipsis);
1127 if (_layout_ellipsize_width > 0) {
1128 _layout->set_width (_layout_ellipsize_width);
1134 ArdourButton::recalc_char_pixel_geometry ()
1136 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1140 // NB. this is not static, since the geometry is different
1141 // depending on the font used.
1143 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1144 _layout->set_text (x);
1145 _layout->get_pixel_size (w, h);
1146 _char_pixel_height = std::max(4, h);
1147 // number of actual chars in the string (not bytes)
1148 // Glib to the rescue.
1149 Glib::ustring gx(x);
1150 _char_avg_pixel_width = w / (float)gx.size();
1151 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1152 _layout->set_text (_text);
1156 ArdourButton::action_visibility_changed ()
1158 if (_action->property_visible ()) {
1166 ArdourButton::action_tooltip_changed ()
1168 string str = _action->property_tooltip().get_value();
1169 ARDOUR_UI::instance()->set_tip (*this, str);
1173 ArdourButton::set_elements (Element e)
1176 CairoWidget::set_dirty ();
1180 ArdourButton::add_elements (Element e)
1182 _elements = (ArdourButton::Element) (_elements | e);
1183 CairoWidget::set_dirty ();