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"
38 #include "ardour_button.h"
39 #include "ardour_ui.h"
40 #include "global_signals.h"
44 #define BASELINESTRETCH (1.25)
45 #define TRACKHEADERBTNW (3.10)
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)
61 , _tweaks (Tweaks (0))
62 , _char_pixel_width (0)
63 , _char_pixel_height (0)
64 , _char_avg_pixel_width (0)
68 , _corner_radius (2.5)
73 , fill_inactive_color (0)
74 , fill_active_color (0)
75 , text_active_color(0)
76 , text_inactive_color(0)
78 , led_inactive_color(0)
81 , led_inset_pattern (0)
83 , _act_on_release (true)
85 , _distinct_led_click (false)
88 , _fixed_colors_set (false)
89 , _fallthrough_to_parent (false)
90 , _layout_ellipsize_width (-1)
91 , _ellipsis (Pango::ELLIPSIZE_NONE)
92 , _update_colors (true)
95 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
98 ArdourButton::ArdourButton (const std::string& str, Element e)
100 , _tweaks (Tweaks (0))
104 , _corner_radius (2.5)
109 , fill_inactive_color (0)
110 , fill_active_color (0)
111 , text_active_color(0)
112 , text_inactive_color(0)
113 , led_active_color(0)
114 , led_inactive_color(0)
116 , concave_pattern (0)
117 , led_inset_pattern (0)
119 , _act_on_release (true)
121 , _distinct_led_click (false)
124 , _fixed_colors_set (false)
125 , _fallthrough_to_parent (false)
126 , _layout_ellipsize_width (-1)
127 , _ellipsis (Pango::ELLIPSIZE_NONE)
128 , _update_colors (true)
129 , _pattern_height (0)
132 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
133 ARDOUR_UI_UTILS::DPIReset.connect (sigc::mem_fun (*this, &ArdourButton::on_name_changed));
136 ArdourButton::~ArdourButton()
140 if (convex_pattern) {
141 cairo_pattern_destroy (convex_pattern);
144 if (concave_pattern) {
145 cairo_pattern_destroy (concave_pattern);
148 if (led_inset_pattern) {
149 cairo_pattern_destroy (led_inset_pattern);
154 ArdourButton::set_text (const std::string& str)
157 if (!is_realized()) {
161 if (_layout && _layout->get_text() != _text) {
162 _layout->set_text (_text);
168 ArdourButton::set_angle (const double angle)
174 ArdourButton::set_alignment (const float xa, const float ya)
181 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
186 if (_update_colors) {
189 if (get_height() != _pattern_height) {
193 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
194 text_color = text_active_color;
195 led_color = led_active_color;
197 text_color = text_inactive_color;
198 led_color = led_inactive_color;
201 void (*rounded_function)(cairo_t*, double, double, double, double, double);
203 switch (_corner_mask) {
204 case 0x1: /* upper left only */
205 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
207 case 0x2: /* upper right only */
208 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
210 case 0x3: /* upper only */
211 rounded_function = Gtkmm2ext::rounded_top_rectangle;
213 /* should really have functions for lower right, lower left,
214 lower only, but for now, we don't
217 rounded_function = Gtkmm2ext::rounded_rectangle;
220 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
221 if ((_elements & (Body|Edge)) == (Body|Edge)) {
222 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
223 cairo_set_source_rgba (cr, 0, 0, 0, 1);
228 if ((_elements & Body)==Body) {
229 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
230 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
231 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
233 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
235 ArdourCanvas::set_source_rgba (cr, fill_active_color);
237 } else { //inactive, or it has an indicator
239 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
244 // IMPLICIT ACTIVE: draw a border of the active color
245 if ((_elements & Body)==Body) {
246 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
247 cairo_set_line_width (cr, 2.0);
248 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
249 ArdourCanvas::set_source_rgba (cr, fill_active_color);
254 //show the "convex" or "concave" gradient
255 if (!_flat_buttons) {
256 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
258 cairo_set_source (cr, concave_pattern);
259 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
262 cairo_set_source (cr, convex_pattern);
263 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
270 double x = rint((get_width() - _pixbuf->get_width()) * .5);
271 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
273 if (_elements & Menu) {
274 //if this is a DropDown with an icon, then we need to
275 //move the icon left slightly to accomomodate the arrow
278 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
279 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
282 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
283 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
284 const double x = get_width() * .5;
285 const double y = get_height() * .5;
286 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
287 const double slit = .11 * M_PI;
289 cairo_translate(cr, x, y);
291 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
292 if (active_state() == Gtkmm2ext::ExplicitActive)
293 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
295 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
296 cairo_fill_preserve(cr);
297 cairo_set_source_rgba (cr, .0, .0, .0, .5);
298 cairo_set_line_width(cr, 1);
302 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
304 cairo_rotate (cr, -.5 * M_PI);
305 cairo_move_to(cr, 0, 0);
306 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
307 cairo_line_to(cr, 0, 0);
308 cairo_close_path(cr);
311 cairo_rotate (cr, 2. * M_PI / 3.);
313 cairo_move_to(cr, 0, 0);
314 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
315 cairo_line_to(cr, 0, 0);
316 cairo_close_path(cr);
319 cairo_rotate (cr, 2. * M_PI / 3.);
320 cairo_move_to(cr, 0, 0);
321 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
322 cairo_line_to(cr, 0, 0);
323 cairo_close_path(cr);
328 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
329 if (active_state() == Gtkmm2ext::ExplicitActive)
330 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
332 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
334 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
335 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
340 else if (_elements & RecButton) {
341 const double x = get_width() * .5;
342 const double y = get_height() * .5;
343 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
344 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
345 if (active_state() == Gtkmm2ext::ExplicitActive)
346 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
348 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
349 cairo_fill_preserve(cr);
350 cairo_set_source_rgba (cr, .0, .0, .0, .8);
351 cairo_set_line_width(cr, 1);
354 else if (_elements & CloseCross) {
355 const double x = get_width() * .5;
356 const double y = get_height() * .5;
357 const double o = .5 + std::min(x, y) * .4;
358 ArdourCanvas::set_source_rgba (cr, text_color);
359 cairo_set_line_width(cr, 1);
360 cairo_move_to(cr, x-o, y-o);
361 cairo_line_to(cr, x+o, y+o);
362 cairo_move_to(cr, x+o, y-o);
363 cairo_line_to(cr, x-o, y+o);
367 const int text_margin = char_pixel_width();
369 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
371 #if 0 // DEBUG style (print on hover)
373 bool layout_font = true;
374 Pango::FontDescription fd = _layout->get_font_description();
375 if (fd.gobj() == NULL) {
377 fd = get_pango_context()->get_font_description();
379 printf("%s: f:%dx%d aw:%.3f bh:%.0f t:%dx%d (%dx%d) %s\"%s\"\n",
381 char_pixel_width(), char_pixel_height(), char_avg_pixel_width(),
382 ceil(char_pixel_height() * BASELINESTRETCH),
383 _text_width, _text_height,
384 get_width(), get_height(),
385 layout_font ? "L:" : "W:",
386 fd.to_string().c_str());
391 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
395 ArdourCanvas::set_source_rgba (cr, text_color);
396 const double text_ypos = (get_height() - _text_height) * .5;
398 if (_elements & Menu) {
399 // always left align (dropdown)
400 cairo_move_to (cr, text_margin, text_ypos);
401 pango_cairo_show_layout (cr, _layout->gobj());
402 } else if ( (_elements & Indicator) == Indicator) {
403 // left/right align depending on LED position
405 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
407 cairo_move_to (cr, text_margin, text_ypos);
409 pango_cairo_show_layout (cr, _layout->gobj());
411 /* centered text otherwise */
418 cairo_rotate(cr, _angle * M_PI / 180.0);
419 cairo_device_to_user(cr, &ww, &wh);
420 xa = (ww - _text_width) * _xalign;
421 ya = (wh - _text_height) * _yalign;
423 /* quick hack for left/bottom alignment at -90deg
424 * TODO this should be generalized incl rotation.
425 * currently only 'user' of this API is meter_strip.cc
427 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
429 cairo_move_to (cr, xa, ya);
430 pango_cairo_update_layout(cr, _layout->gobj());
431 pango_cairo_show_layout (cr, _layout->gobj());
438 if (_elements & Menu) {
439 const float trih = ceil(_diameter * .5);
440 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
442 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
443 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
444 cairo_rel_line_to(cr, -triw2, -trih);
445 cairo_rel_line_to(cr, 2. * triw2, 0);
446 cairo_close_path(cr);
448 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
451 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
452 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
453 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
454 cairo_close_path(cr);
455 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
456 cairo_set_line_width(cr, 1);
461 if (_elements & Indicator) {
464 /* move to the center of the indicator/led */
465 if (_elements & Text) {
466 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
468 cairo_translate (cr, led_xoff, get_height() * .5);
470 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
473 cairo_translate (cr, get_width() * .5, get_height() * .5);
477 if (!_flat_buttons) {
478 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
479 cairo_set_source (cr, led_inset_pattern);
484 cairo_set_source_rgb (cr, 0, 0, 0);
485 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
489 ArdourCanvas::set_source_rgba (cr, led_color);
490 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
496 // a transparent gray layer to indicate insensitivity
497 if ((visual_state() & Gtkmm2ext::Insensitive)) {
498 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
499 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
503 // if requested, show hovering
504 if (ARDOUR::Config->get_widget_prelight()
505 && !((visual_state() & Gtkmm2ext::Insensitive))) {
507 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
508 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
513 //user is currently pressing the button. dark outline helps to indicate this
514 if (_grabbed && !(_elements & (Inactive|Menu))) {
515 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
516 cairo_set_line_width(cr, 2);
517 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
521 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
522 if (visual_state() & Gtkmm2ext::Selected) {
523 cairo_set_line_width(cr, 1);
524 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
525 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
529 //I guess this means we have keyboard focus. I don't think this works currently
531 //A: yes, it's keyboard focus and it does work when there's no editor window
532 // (the editor is always the first receiver for KeyDown).
533 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
535 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
536 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
538 cairo_set_dash (cr, &dashes, 1, 0);
539 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
540 cairo_set_line_width (cr, 1.0);
542 cairo_set_dash (cr, 0, 0, 0);
547 ArdourButton::set_corner_radius (float r)
550 CairoWidget::set_dirty ();
554 ArdourButton::on_realize()
556 CairoWidget::on_realize ();
558 if (_layout && _layout->get_text() != _text) {
559 _layout->set_text (_text);
565 ArdourButton::on_size_request (Gtk::Requisition* req)
567 req->width = req->height = 0;
568 CairoWidget::on_size_request (req);
570 if (_diameter == 0) {
571 const float newdia = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
572 if (_diameter != newdia) {
578 if ((_elements & Text) && !_text.empty()) {
580 _text_height = char_pixel_height ();
581 // if _layout does not exist, char_pixel_height() creates it,
582 _layout->get_pixel_size (_text_width, ignored);
583 req->width += rint(1.75 * char_pixel_width()); // padding
584 req->width += _text_width;
585 req->height = std::max(req->height, (int) ceil(_text_height * BASELINESTRETCH + 1.0));
592 req->width += _pixbuf->get_width() + char_pixel_width();
593 req->height = std::max(req->height, _pixbuf->get_height() + 4);
596 if (_elements & Indicator) {
597 req->width += lrint (_diameter) + char_pixel_width();
598 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
601 if ((_elements & Menu)) {
602 req->width += _diameter + 4;
605 if (_elements & (RecButton | CloseCross)) {
606 assert(!(_elements & Text));
607 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
609 req->height = std::max(req->height, wh);
612 /* Tweaks to mess the nice stuff above up again. */
613 if (_tweaks & TrackHeader) {
614 // forget everything above and just use a fixed square [em] size
615 // "TrackHeader Buttons" are single letter (usually uppercase)
616 // a SizeGroup is much less efficient (lots of gtk work under the hood for each track)
617 const int wh = std::max (rint (TRACKHEADERBTNW * char_avg_pixel_width()), ceil (char_pixel_height() * BASELINESTRETCH + 1.));
621 else if (_tweaks & Square) {
622 // currerntly unused (again)
623 if (req->width < req->height)
624 req->width = req->height;
625 if (req->height < req->width)
626 req->height = req->width;
627 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
628 // properly centered text for those elements that are centered
629 // (no sub-pixel offset)
630 if ((req->width - _text_width) & 1) { ++req->width; }
631 if ((req->height - _text_height) & 1) { ++req->height; }
634 printf("REQ: %s: %dx%d\n", get_name().c_str(), req->width, req->height);
639 * This sets the colors used for rendering based on the name of the button, and
640 * thus uses information from the GUI config data.
643 ArdourButton::set_colors ()
645 _update_colors = false;
646 if (_fixed_colors_set) {
649 std::string name = get_name();
651 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
652 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
654 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
655 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
657 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
658 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
662 * This sets the colors used for rendering based on two fixed values, rather
663 * than basing them on the button name, and thus information in the GUI config
666 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
668 _fixed_colors_set = true;
670 fill_active_color = color_active;
671 fill_inactive_color = color_inactive;
673 unsigned char r, g, b, a;
674 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
676 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
677 (max (double(g), 255.) - min (double(g), 255.)) +
678 (max (double(b), 255.) - min (double(b), 255.));
680 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
681 (max (double(g), 0.) - min (double(g), 0.)) +
682 (max (double(b), 0.) - min (double(b), 0.));
684 text_active_color = (white_contrast > black_contrast) ?
685 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
686 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
689 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
691 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
692 (max (double(g), 255.) - min (double(g), 255.)) +
693 (max (double(b), 255.) - min (double(b), 255.));
695 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
696 (max (double(g), 0.) - min (double(g), 0.)) +
697 (max (double(b), 0.) - min (double(b), 0.));
699 text_inactive_color = (white_contrast > black_contrast) ?
700 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
701 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
703 /* XXX what about led colors ? */
704 CairoWidget::set_dirty ();
708 ArdourButton::build_patterns ()
710 if (convex_pattern) {
711 cairo_pattern_destroy (convex_pattern);
715 if (concave_pattern) {
716 cairo_pattern_destroy (concave_pattern);
720 if (led_inset_pattern) {
721 cairo_pattern_destroy (led_inset_pattern);
722 led_inset_pattern = 0;
726 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
727 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
728 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
731 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
732 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
733 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
735 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
736 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
737 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
739 _pattern_height = get_height() ;
743 ArdourButton::set_led_left (bool yn)
749 ArdourButton::on_button_press_event (GdkEventButton *ev)
751 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
752 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
753 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
758 if (binding_proxy.button_press_handler (ev)) {
763 CairoWidget::set_dirty ();
765 if (!_act_on_release) {
767 _action->activate ();
772 if (_fallthrough_to_parent)
779 ArdourButton::on_button_release_event (GdkEventButton *ev)
781 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
782 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
783 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
784 signal_led_clicked(); /* EMIT SIGNAL */
790 CairoWidget::set_dirty ();
794 if (_act_on_release) {
796 _action->activate ();
802 if (_fallthrough_to_parent)
809 ArdourButton::set_distinct_led_click (bool yn)
811 _distinct_led_click = yn;
816 ArdourButton::color_handler ()
818 _update_colors = true;
819 CairoWidget::set_dirty ();
823 ArdourButton::on_size_allocate (Allocation& alloc)
825 CairoWidget::on_size_allocate (alloc);
830 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
832 watch_connection.disconnect ();
833 binding_proxy.set_controllable (c);
837 ArdourButton::watch ()
839 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
842 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
845 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
849 ArdourButton::controllable_changed ()
851 float val = binding_proxy.get_controllable()->get_value();
853 if (fabs (val) >= 0.5f) {
854 set_active_state (Gtkmm2ext::ExplicitActive);
856 unset_active_state ();
858 CairoWidget::set_dirty ();
862 ArdourButton::set_related_action (RefPtr<Action> act)
864 Gtkmm2ext::Activatable::set_related_action (act);
868 action_tooltip_changed ();
870 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
873 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
876 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
877 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
878 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
883 ArdourButton::action_toggled ()
885 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
888 if (tact->get_active()) {
889 set_active_state (Gtkmm2ext::ExplicitActive);
891 unset_active_state ();
897 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
899 _update_colors = true;
900 CairoWidget::set_dirty ();
904 ArdourButton::on_name_changed ()
906 _char_pixel_width = 0;
907 _char_pixel_height = 0;
909 _update_colors = true;
916 ArdourButton::setup_led_rect ()
918 if (!(_elements & Indicator)) {
924 _led_rect = new cairo_rectangle_t;
926 if (_elements & Text) {
928 _led_rect->x = char_pixel_width();
930 _led_rect->x = get_width() - char_pixel_width() + _diameter;
934 _led_rect->x = .5 * get_width() - _diameter;
937 _led_rect->y = .5 * (get_height() - _diameter);
938 _led_rect->width = _diameter;
939 _led_rect->height = _diameter;
943 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
952 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
954 bool changed = (_active_state != s);
955 CairoWidget::set_active_state (s);
957 _update_colors = true;
958 CairoWidget::set_dirty ();
963 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
965 bool changed = (_visual_state != s);
966 CairoWidget::set_visual_state (s);
968 _update_colors = true;
969 CairoWidget::set_dirty ();
974 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
977 CairoWidget::set_dirty ();
978 return CairoWidget::on_focus_in_event (ev);
982 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
985 CairoWidget::set_dirty ();
986 return CairoWidget::on_focus_out_event (ev);
990 ArdourButton::on_key_release_event (GdkEventKey *ev) {
992 (ev->keyval == GDK_space || ev->keyval == GDK_Return))
996 _action->activate ();
1000 return CairoWidget::on_key_release_event (ev);
1004 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
1006 _hovering = (_elements & Inactive) ? false : true;
1008 if (ARDOUR::Config->get_widget_prelight()) {
1009 CairoWidget::set_dirty ();
1012 return CairoWidget::on_enter_notify_event (ev);
1016 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1020 if (ARDOUR::Config->get_widget_prelight()) {
1021 CairoWidget::set_dirty ();
1024 return CairoWidget::on_leave_notify_event (ev);
1028 ArdourButton::set_tweaks (Tweaks t)
1032 if (is_realized()) {
1039 ArdourButton::action_sensitivity_changed ()
1041 if (_action->property_sensitive ()) {
1042 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1044 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1049 ArdourButton::set_layout_ellisize_width (int w)
1051 if (_layout_ellipsize_width == w) {
1054 _layout_ellipsize_width = w;
1058 if (_layout_ellipsize_width > 0) {
1059 _layout->set_width (_layout_ellipsize_width);
1064 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1066 if (_ellipsis == e) {
1073 _layout->set_ellipsize(_ellipsis);
1074 if (_layout_ellipsize_width > 0) {
1075 _layout->set_width (_layout_ellipsize_width);
1077 if (is_realized ()) {
1083 ArdourButton::ensure_layout ()
1087 _layout = Pango::Layout::create (get_pango_context());
1088 _layout->set_ellipsize(_ellipsis);
1089 if (_layout_ellipsize_width > 0) {
1090 _layout->set_width (_layout_ellipsize_width);
1096 ArdourButton::recalc_char_pixel_geometry ()
1098 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1102 // NB. this is not static, since the geometry is different
1103 // depending on the font used.
1105 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1106 _layout->set_text (x);
1107 _layout->get_pixel_size (w, h);
1108 _char_pixel_height = std::max(4, h);
1109 // number of actual chars in the string (not bytes)
1110 // Glib to the rescue.
1111 Glib::ustring gx(x);
1112 _char_avg_pixel_width = w / (float)gx.size();
1113 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
1114 _layout->set_text (_text);
1118 ArdourButton::action_visibility_changed ()
1120 if (_action->property_visible ()) {
1128 ArdourButton::action_tooltip_changed ()
1130 string str = _action->property_tooltip().get_value();
1131 ARDOUR_UI::instance()->set_tip (*this, str);
1135 ArdourButton::set_elements (Element e)
1138 CairoWidget::set_dirty ();
1142 ArdourButton::add_elements (Element e)
1144 _elements = (ArdourButton::Element) (_elements | e);
1145 CairoWidget::set_dirty ();