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)
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)
60 , _tweaks (Tweaks (0))
61 , _char_pixel_width (0)
62 , _char_pixel_height (0)
66 , _corner_radius (2.5)
71 , fill_inactive_color (0)
72 , fill_active_color (0)
73 , text_active_color(0)
74 , text_inactive_color(0)
76 , led_inactive_color(0)
79 , led_inset_pattern (0)
81 , _act_on_release (true)
83 , _distinct_led_click (false)
86 , _fixed_colors_set (false)
87 , _fallthrough_to_parent (false)
88 , _layout_ellipsize_width (-1)
89 , _ellipsis (Pango::ELLIPSIZE_NONE)
90 , _update_colors (true)
93 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
96 ArdourButton::ArdourButton (const std::string& str, Element e)
98 , _tweaks (Tweaks (0))
102 , _corner_radius (2.5)
107 , fill_inactive_color (0)
108 , fill_active_color (0)
109 , text_active_color(0)
110 , text_inactive_color(0)
111 , led_active_color(0)
112 , led_inactive_color(0)
114 , concave_pattern (0)
115 , led_inset_pattern (0)
117 , _act_on_release (true)
119 , _distinct_led_click (false)
122 , _fixed_colors_set (false)
123 , _fallthrough_to_parent (false)
124 , _layout_ellipsize_width (-1)
125 , _ellipsis (Pango::ELLIPSIZE_NONE)
126 , _update_colors (true)
127 , _pattern_height (0)
130 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
133 ArdourButton::~ArdourButton()
137 if (convex_pattern) {
138 cairo_pattern_destroy (convex_pattern);
141 if (concave_pattern) {
142 cairo_pattern_destroy (concave_pattern);
145 if (led_inset_pattern) {
146 cairo_pattern_destroy (led_inset_pattern);
151 ArdourButton::set_text (const std::string& str)
154 if (!is_realized()) {
158 if (_layout && _layout->get_text() != _text) {
159 _layout->set_text (_text);
165 ArdourButton::set_angle (const double angle)
171 ArdourButton::set_alignment (const float xa, const float ya)
178 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
183 if (_update_colors) {
186 if (get_height() != _pattern_height) {
190 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
191 text_color = text_active_color;
192 led_color = led_active_color;
194 text_color = text_inactive_color;
195 led_color = led_inactive_color;
198 void (*rounded_function)(cairo_t*, double, double, double, double, double);
200 switch (_corner_mask) {
201 case 0x1: /* upper left only */
202 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
204 case 0x2: /* upper right only */
205 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
207 case 0x3: /* upper only */
208 rounded_function = Gtkmm2ext::rounded_top_rectangle;
210 /* should really have functions for lower right, lower left,
211 lower only, but for now, we don't
214 rounded_function = Gtkmm2ext::rounded_rectangle;
217 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
218 if ((_elements & (Body|Edge)) == (Body|Edge)) {
219 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
220 cairo_set_source_rgba (cr, 0, 0, 0, 1);
225 if ((_elements & Body)==Body) {
226 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
227 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
228 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
230 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
232 ArdourCanvas::set_source_rgba (cr, fill_active_color);
234 } else { //inactive, or it has an indicator
236 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
241 // IMPLICIT ACTIVE: draw a border of the active color
242 if ((_elements & Body)==Body) {
243 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
244 cairo_set_line_width (cr, 2.0);
245 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
246 ArdourCanvas::set_source_rgba (cr, fill_active_color);
251 //show the "convex" or "concave" gradient
252 if (!_flat_buttons) {
253 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
255 cairo_set_source (cr, concave_pattern);
256 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
259 cairo_set_source (cr, convex_pattern);
260 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
267 double x = rint((get_width() - _pixbuf->get_width()) * .5);
268 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
270 if (_elements & Menu) {
271 //if this is a DropDown with an icon, then we need to
272 //move the icon left slightly to accomomodate the arrow
275 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
276 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
279 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
280 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
281 const double x = get_width() * .5;
282 const double y = get_height() * .5;
283 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
284 const double slit = .11 * M_PI;
286 cairo_translate(cr, x, y);
288 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
289 if (active_state() == Gtkmm2ext::ExplicitActive)
290 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
292 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
293 cairo_fill_preserve(cr);
294 cairo_set_source_rgba (cr, .0, .0, .0, .5);
295 cairo_set_line_width(cr, 1);
299 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
301 cairo_rotate (cr, -.5 * M_PI);
302 cairo_move_to(cr, 0, 0);
303 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
304 cairo_line_to(cr, 0, 0);
305 cairo_close_path(cr);
308 cairo_rotate (cr, 2. * M_PI / 3.);
310 cairo_move_to(cr, 0, 0);
311 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
312 cairo_line_to(cr, 0, 0);
313 cairo_close_path(cr);
316 cairo_rotate (cr, 2. * M_PI / 3.);
317 cairo_move_to(cr, 0, 0);
318 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
319 cairo_line_to(cr, 0, 0);
320 cairo_close_path(cr);
325 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
326 if (active_state() == Gtkmm2ext::ExplicitActive)
327 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
329 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
331 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
332 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
337 else if (_elements & RecButton) {
338 const double x = get_width() * .5;
339 const double y = get_height() * .5;
340 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
341 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
342 if (active_state() == Gtkmm2ext::ExplicitActive)
343 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
345 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
346 cairo_fill_preserve(cr);
347 cairo_set_source_rgba (cr, .0, .0, .0, .8);
348 cairo_set_line_width(cr, 1);
351 else if (_elements & CloseCross) {
352 const double x = get_width() * .5;
353 const double y = get_height() * .5;
354 const double o = .5 + std::min(x, y) * .4;
355 ArdourCanvas::set_source_rgba (cr, text_color);
356 cairo_set_line_width(cr, 1);
357 cairo_move_to(cr, x-o, y-o);
358 cairo_line_to(cr, x+o, y+o);
359 cairo_move_to(cr, x+o, y-o);
360 cairo_line_to(cr, x-o, y+o);
364 const int text_margin = char_pixel_width();
366 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
368 #if 0 // DEBUG style (print on hover)
370 bool layout_font = true;
371 Pango::FontDescription fd = _layout->get_font_description();
372 if (fd.gobj() == NULL) {
374 fd = get_pango_context()->get_font_description();
376 printf("%s: f:%dx%d bh:%.0f tw:%d (%dx%d) %s\"%s\"\n",
378 char_pixel_width(), char_pixel_height(),
379 ceil(char_pixel_height() * BASELINESTRETCH),
381 get_width(), get_height(),
382 layout_font ? "L:" : "W:",
383 fd.to_string().c_str());
388 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
392 ArdourCanvas::set_source_rgba (cr, text_color);
393 const double text_ypos = (get_height() - _text_height) * .5;
395 if (_elements & Menu) {
396 // always left align (dropdown)
397 cairo_move_to (cr, text_margin, text_ypos);
398 pango_cairo_show_layout (cr, _layout->gobj());
399 } else if ( (_elements & Indicator) == Indicator) {
400 // left/right align depending on LED position
402 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
404 cairo_move_to (cr, text_margin, text_ypos);
406 pango_cairo_show_layout (cr, _layout->gobj());
408 /* centered text otherwise */
415 cairo_rotate(cr, _angle * M_PI / 180.0);
416 cairo_device_to_user(cr, &ww, &wh);
417 xa = (ww - _text_width) * _xalign;
418 ya = (wh - _text_height) * _yalign;
420 /* quick hack for left/bottom alignment at -90deg
421 * TODO this should be generalized incl rotation.
422 * currently only 'user' of this API is meter_strip.cc
424 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
426 cairo_move_to (cr, xa, ya);
427 pango_cairo_update_layout(cr, _layout->gobj());
428 pango_cairo_show_layout (cr, _layout->gobj());
435 if (_elements & Menu) {
436 const float trih = ceil(_diameter * .5);
437 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
439 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
440 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
441 cairo_rel_line_to(cr, -triw2, -trih);
442 cairo_rel_line_to(cr, 2. * triw2, 0);
443 cairo_close_path(cr);
445 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
448 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
449 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
450 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
451 cairo_close_path(cr);
452 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
453 cairo_set_line_width(cr, 1);
458 if (_elements & Indicator) {
461 /* move to the center of the indicator/led */
462 if (_elements & Text) {
463 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
465 cairo_translate (cr, led_xoff, get_height() * .5);
467 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
470 cairo_translate (cr, get_width() * .5, get_height() * .5);
474 if (!_flat_buttons) {
475 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
476 cairo_set_source (cr, led_inset_pattern);
481 cairo_set_source_rgb (cr, 0, 0, 0);
482 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
486 ArdourCanvas::set_source_rgba (cr, led_color);
487 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
493 // a transparent gray layer to indicate insensitivity
494 if ((visual_state() & Gtkmm2ext::Insensitive)) {
495 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
496 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
500 // if requested, show hovering
501 if (ARDOUR::Config->get_widget_prelight()
502 && !((visual_state() & Gtkmm2ext::Insensitive))) {
504 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
505 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
510 //user is currently pressing the button. dark outline helps to indicate this
511 if (_grabbed && !(_elements & (Inactive|Menu))) {
512 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
513 cairo_set_line_width(cr, 2);
514 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
518 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
519 if (visual_state() & Gtkmm2ext::Selected) {
520 cairo_set_line_width(cr, 1);
521 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
522 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
526 //I guess this means we have keyboard focus. I don't think this works currently
528 //A: yes, it's keyboard focus and it does work when there's no editor window
529 // (the editor is always the first receiver for KeyDown).
530 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
532 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
533 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
535 cairo_set_dash (cr, &dashes, 1, 0);
536 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
537 cairo_set_line_width (cr, 1.0);
539 cairo_set_dash (cr, 0, 0, 0);
544 ArdourButton::set_corner_radius (float r)
547 CairoWidget::set_dirty ();
551 ArdourButton::on_realize()
553 CairoWidget::on_realize ();
555 if (_layout && _layout->get_text() != _text) {
556 _layout->set_text (_text);
562 ArdourButton::on_size_request (Gtk::Requisition* req)
564 req->width = req->height = 0;
565 CairoWidget::on_size_request (req);
567 if (_diameter == 0) {
568 const float newdia = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
569 if (_diameter != newdia) {
575 if ((_elements & Text) && !_text.empty()) {
577 _text_height = char_pixel_height ();
578 // if _layout does not exist, char_pixel_height() creates it,
579 _layout->get_pixel_size (_text_width, ignored);
580 req->width += rint(1.75 * char_pixel_width()); // padding
581 req->width += _text_width;
582 req->height = std::max(req->height, (int) ceil(_text_height * BASELINESTRETCH + 1.0));
589 req->width += _pixbuf->get_width() + char_pixel_width();
590 req->height = std::max(req->height, _pixbuf->get_height() + 4);
593 if (_elements & Indicator) {
594 req->width += lrint (_diameter) + char_pixel_width();
595 req->height = std::max (req->height, (int) lrint (_diameter) + 4);
598 if ((_elements & Menu)) {
599 req->width += _diameter + 4;
602 if (_elements & (RecButton | CloseCross)) {
603 assert(!(_elements & Text));
604 const int wh = std::max(3.5 * char_pixel_width(), ceil(char_pixel_height() * BASELINESTRETCH + 1));
606 req->height = std::max(req->height, wh);
609 if (_tweaks & Square) {
610 // squared buttons are also grouped, we cannot align all texts
611 // -> skip proper center adjustment
612 if (req->width < req->height)
613 req->width = req->height;
614 if (req->height < req->width)
615 req->height = req->width;
616 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
617 // properly centered text for those elements that are centered
618 if ((req->width - _text_width) & 1) { ++req->width; }
619 if ((req->height - _text_height) & 1) { ++req->height; }
624 * This sets the colors used for rendering based on the name of the button, and
625 * thus uses information from the GUI config data.
628 ArdourButton::set_colors ()
630 _update_colors = false;
631 if (_fixed_colors_set) {
634 std::string name = get_name();
636 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
637 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
639 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
640 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
642 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
643 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
647 * This sets the colors used for rendering based on two fixed values, rather
648 * than basing them on the button name, and thus information in the GUI config
651 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
653 _fixed_colors_set = true;
655 fill_active_color = color_active;
656 fill_inactive_color = color_inactive;
658 unsigned char r, g, b, a;
659 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
661 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
662 (max (double(g), 255.) - min (double(g), 255.)) +
663 (max (double(b), 255.) - min (double(b), 255.));
665 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
666 (max (double(g), 0.) - min (double(g), 0.)) +
667 (max (double(b), 0.) - min (double(b), 0.));
669 text_active_color = (white_contrast > black_contrast) ?
670 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
671 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
674 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
676 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 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_inactive_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 */
688 /* XXX what about led colors ? */
689 CairoWidget::set_dirty ();
693 ArdourButton::build_patterns ()
695 if (convex_pattern) {
696 cairo_pattern_destroy (convex_pattern);
700 if (concave_pattern) {
701 cairo_pattern_destroy (concave_pattern);
705 if (led_inset_pattern) {
706 cairo_pattern_destroy (led_inset_pattern);
707 led_inset_pattern = 0;
711 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
712 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
713 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
716 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
717 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
718 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
720 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
721 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
722 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
724 _pattern_height = get_height() ;
728 ArdourButton::set_led_left (bool yn)
734 ArdourButton::on_button_press_event (GdkEventButton *ev)
736 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
737 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
738 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
743 if (binding_proxy.button_press_handler (ev)) {
748 CairoWidget::set_dirty ();
750 if (!_act_on_release) {
752 _action->activate ();
757 if (_fallthrough_to_parent)
764 ArdourButton::on_button_release_event (GdkEventButton *ev)
766 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
767 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
768 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
769 signal_led_clicked(); /* EMIT SIGNAL */
775 CairoWidget::set_dirty ();
779 if (_act_on_release) {
781 _action->activate ();
787 if (_fallthrough_to_parent)
794 ArdourButton::set_distinct_led_click (bool yn)
796 _distinct_led_click = yn;
801 ArdourButton::color_handler ()
803 _update_colors = true;
804 CairoWidget::set_dirty ();
808 ArdourButton::on_size_allocate (Allocation& alloc)
810 CairoWidget::on_size_allocate (alloc);
815 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
817 watch_connection.disconnect ();
818 binding_proxy.set_controllable (c);
822 ArdourButton::watch ()
824 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
827 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
830 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
834 ArdourButton::controllable_changed ()
836 float val = binding_proxy.get_controllable()->get_value();
838 if (fabs (val) >= 0.5f) {
839 set_active_state (Gtkmm2ext::ExplicitActive);
841 unset_active_state ();
843 CairoWidget::set_dirty ();
847 ArdourButton::set_related_action (RefPtr<Action> act)
849 Gtkmm2ext::Activatable::set_related_action (act);
853 action_tooltip_changed ();
855 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
858 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
861 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
862 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
863 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
868 ArdourButton::action_toggled ()
870 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
873 if (tact->get_active()) {
874 set_active_state (Gtkmm2ext::ExplicitActive);
876 unset_active_state ();
882 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
884 _update_colors = true;
885 CairoWidget::set_dirty ();
889 ArdourButton::on_name_changed ()
891 _char_pixel_width = 0;
892 _char_pixel_height = 0;
894 _update_colors = true;
901 ArdourButton::setup_led_rect ()
903 if (!(_elements & Indicator)) {
909 _led_rect = new cairo_rectangle_t;
911 if (_elements & Text) {
913 _led_rect->x = char_pixel_width();
915 _led_rect->x = get_width() - char_pixel_width() + _diameter;
919 _led_rect->x = .5 * get_width() - _diameter;
922 _led_rect->y = .5 * (get_height() - _diameter);
923 _led_rect->width = _diameter;
924 _led_rect->height = _diameter;
928 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
937 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
939 bool changed = (_active_state != s);
940 CairoWidget::set_active_state (s);
942 _update_colors = true;
943 CairoWidget::set_dirty ();
948 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
950 bool changed = (_visual_state != s);
951 CairoWidget::set_visual_state (s);
953 _update_colors = true;
954 CairoWidget::set_dirty ();
959 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
962 CairoWidget::set_dirty ();
963 return CairoWidget::on_focus_in_event (ev);
967 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
970 CairoWidget::set_dirty ();
971 return CairoWidget::on_focus_out_event (ev);
975 ArdourButton::on_key_release_event (GdkEventKey *ev) {
977 (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
981 _action->activate ();
985 return CairoWidget::on_key_release_event (ev);
989 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
991 _hovering = (_elements & Inactive) ? false : true;
993 if (ARDOUR::Config->get_widget_prelight()) {
994 CairoWidget::set_dirty ();
997 return CairoWidget::on_enter_notify_event (ev);
1001 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1005 if (ARDOUR::Config->get_widget_prelight()) {
1006 CairoWidget::set_dirty ();
1009 return CairoWidget::on_leave_notify_event (ev);
1013 ArdourButton::set_tweaks (Tweaks t)
1017 if (is_realized()) {
1024 ArdourButton::action_sensitivity_changed ()
1026 if (_action->property_sensitive ()) {
1027 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1029 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1034 ArdourButton::set_layout_ellisize_width (int w)
1036 if (_layout_ellipsize_width == w) {
1039 _layout_ellipsize_width = w;
1043 if (_layout_ellipsize_width > 0) {
1044 _layout->set_width (_layout_ellipsize_width);
1049 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1051 if (_ellipsis == e) {
1058 _layout->set_ellipsize(_ellipsis);
1059 if (_layout_ellipsize_width > 0) {
1060 _layout->set_width (_layout_ellipsize_width);
1062 if (is_realized ()) {
1068 ArdourButton::ensure_layout ()
1072 _layout = Pango::Layout::create (get_pango_context());
1073 _layout->set_ellipsize(_ellipsis);
1074 if (_layout_ellipsize_width > 0) {
1075 _layout->set_width (_layout_ellipsize_width);
1081 ArdourButton::recalc_char_pixel_geometry ()
1083 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1087 // NB. this is not static, since the geometry is different
1088 // depending on the font used.
1090 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1091 _layout->set_text (x);
1092 _layout->get_pixel_size (w, h);
1093 _char_pixel_height = std::max(4, h);
1094 // number of actual chars in the string (not bytes)
1095 // Glib to the rescue.
1096 Glib::ustring gx(x);
1097 _char_pixel_width = std::max(4, w / (int)gx.size());
1098 _layout->set_text (_text);
1102 ArdourButton::action_visibility_changed ()
1104 if (_action->property_visible ()) {
1112 ArdourButton::action_tooltip_changed ()
1114 string str = _action->property_tooltip().get_value();
1115 ARDOUR_UI::instance()->set_tip (*this, str);
1119 ArdourButton::set_elements (Element e)
1122 CairoWidget::set_dirty ();
1126 ArdourButton::add_elements (Element e)
1128 _elements = (ArdourButton::Element) (_elements | e);
1129 CairoWidget::set_dirty ();