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_and_patterns (true)
92 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
95 ArdourButton::ArdourButton (const std::string& str, Element e)
97 , _tweaks (Tweaks (0))
101 , _corner_radius (2.5)
106 , fill_inactive_color (0)
107 , fill_active_color (0)
108 , text_active_color(0)
109 , text_inactive_color(0)
110 , led_active_color(0)
111 , led_inactive_color(0)
113 , concave_pattern (0)
114 , led_inset_pattern (0)
116 , _act_on_release (true)
118 , _distinct_led_click (false)
121 , _fixed_colors_set (false)
126 ArdourButton::~ArdourButton()
130 if (convex_pattern) {
131 cairo_pattern_destroy (convex_pattern);
134 if (concave_pattern) {
135 cairo_pattern_destroy (concave_pattern);
138 if (led_inset_pattern) {
139 cairo_pattern_destroy (led_inset_pattern);
144 ArdourButton::set_text (const std::string& str)
147 if (!is_realized()) {
151 if (_layout && _layout->get_text() != _text) {
152 _layout->set_text (_text);
158 ArdourButton::set_angle (const double angle)
164 ArdourButton::set_alignment (const float xa, const float ya)
171 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
176 if (_update_colors_and_patterns) {
179 _update_colors_and_patterns = false;
182 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
183 text_color = text_active_color;
184 led_color = led_active_color;
186 text_color = text_inactive_color;
187 led_color = led_inactive_color;
190 void (*rounded_function)(cairo_t*, double, double, double, double, double);
192 switch (_corner_mask) {
193 case 0x1: /* upper left only */
194 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
196 case 0x2: /* upper right only */
197 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
199 case 0x3: /* upper only */
200 rounded_function = Gtkmm2ext::rounded_top_rectangle;
202 /* should really have functions for lower right, lower left,
203 lower only, but for now, we don't
206 rounded_function = Gtkmm2ext::rounded_rectangle;
209 // draw edge (filling a rect underneath, rather than stroking a border on top, allows the corners to be lighter-weight.
210 if ((_elements & (Body|Edge)) == (Body|Edge)) {
211 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius + 1.5);
212 cairo_set_source_rgba (cr, 0, 0, 0, 1);
217 if ((_elements & Body)==Body) {
218 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
219 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
220 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
222 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
224 ArdourCanvas::set_source_rgba (cr, fill_active_color);
226 } else { //inactive, or it has an indicator
228 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
233 // IMPLICIT ACTIVE: draw a border of the active color
234 if ((_elements & Body)==Body) {
235 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
236 cairo_set_line_width (cr, 2.0);
237 rounded_function (cr, 2, 2, get_width() - 4, get_height() - 4, _corner_radius-0.5);
238 ArdourCanvas::set_source_rgba (cr, fill_active_color);
243 //show the "convex" or "concave" gradient
244 if (!_flat_buttons) {
245 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
247 cairo_set_source (cr, concave_pattern);
248 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
251 cairo_set_source (cr, convex_pattern);
252 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
259 double x = rint((get_width() - _pixbuf->get_width()) * .5);
260 const double y = rint((get_height() - _pixbuf->get_height()) * .5);
262 if (_elements & Menu) {
263 //if this is a DropDown with an icon, then we need to
264 //move the icon left slightly to accomomodate the arrow
267 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
268 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
271 else // rec-en is exclusive to pixbuf (tape machine mode, rec-en)
272 if ((_elements & (RecButton|RecTapeMode)) == (RecButton|RecTapeMode)) {
273 const double x = get_width() * .5;
274 const double y = get_height() * .5;
275 const double r = std::min(10., std::min(x, y) * .6); // TODO we need a better way to limit max. radius.
276 const double slit = .11 * M_PI;
278 cairo_translate(cr, x, y);
280 cairo_arc (cr, 0, 0, r, 0, 2 * M_PI);
281 if (active_state() == Gtkmm2ext::ExplicitActive)
282 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
284 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
285 cairo_fill_preserve(cr);
286 cairo_set_source_rgba (cr, .0, .0, .0, .5);
287 cairo_set_line_width(cr, 1);
291 cairo_set_source_rgba (cr, .15, .07, .07, 1.0);
293 cairo_rotate (cr, -.5 * M_PI);
294 cairo_move_to(cr, 0, 0);
295 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
296 cairo_line_to(cr, 0, 0);
297 cairo_close_path(cr);
300 cairo_rotate (cr, 2. * M_PI / 3.);
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.);
309 cairo_move_to(cr, 0, 0);
310 cairo_arc (cr, 0, 0, r *.85, -slit, slit);
311 cairo_line_to(cr, 0, 0);
312 cairo_close_path(cr);
317 cairo_arc (cr, 0, 0, r * .3, 0, 2 * M_PI);
318 if (active_state() == Gtkmm2ext::ExplicitActive)
319 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
321 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
323 cairo_set_source_rgba (cr, .0, .0, .0, 1.0);
324 cairo_arc (cr, 0, 0, r *.15, 0, 2 * M_PI); // hole in the middle
329 else if (_elements & RecButton) {
330 const double x = get_width() * .5;
331 const double y = get_height() * .5;
332 const double r = std::min(10., std::min(x, y) * .55); // TODO we need a better way to limit max. radius.
333 cairo_arc (cr, x, y, r, 0, 2 * M_PI);
334 if (active_state() == Gtkmm2ext::ExplicitActive)
335 cairo_set_source_rgba (cr, .95, .1, .1, 1.);
337 cairo_set_source_rgba (cr, .95, .44, .44, 1.); // #f46f6f
338 cairo_fill_preserve(cr);
339 cairo_set_source_rgba (cr, .0, .0, .0, .8);
340 cairo_set_line_width(cr, 1);
343 else if (_elements & CloseCross) {
344 const double x = get_width() * .5;
345 const double y = get_height() * .5;
346 const double o = .5 + std::min(x, y) * .4;
347 ArdourCanvas::set_source_rgba (cr, text_color);
348 cairo_set_line_width(cr, 1);
349 cairo_move_to(cr, x-o, y-o);
350 cairo_line_to(cr, x+o, y+o);
351 cairo_move_to(cr, x+o, y-o);
352 cairo_line_to(cr, x-o, y+o);
356 const int text_margin = char_pixel_width();
359 if (!_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
361 #if 0 // DEBUG style (print on hover)
363 bool layout_font = true;
364 Pango::FontDescription fd = _layout->get_font_description();
365 if (fd.gobj() == NULL) {
367 fd = get_pango_context()->get_font_description();
369 printf("%s: f:%dx%d bh:%.0f tw:%d (%dx%d) %s\"%s\"\n",
371 char_pixel_width(), char_pixel_height(),
372 ceil(_text_height * BASELINESTRETCH),
374 get_width(), get_height(),
375 layout_font ? "L:" : "W:",
376 fd.to_string().c_str());
381 cairo_rectangle (cr, 2, 1, get_width() - 4, get_height() - 2);
385 ArdourCanvas::set_source_rgba (cr, text_color);
386 const double text_ypos = (get_height() - _text_height) * .5;
388 if (_elements & Menu) {
389 // always left align (dropdown)
390 cairo_move_to (cr, text_margin, text_ypos);
391 pango_cairo_show_layout (cr, _layout->gobj());
392 } else if ( (_elements & Indicator) == Indicator) {
393 // left/right align depending on LED position
395 cairo_move_to (cr, text_margin + _diameter + .5 * char_pixel_width(), text_ypos);
397 cairo_move_to (cr, text_margin, text_ypos);
399 pango_cairo_show_layout (cr, _layout->gobj());
401 /* centered text otherwise */
408 cairo_rotate(cr, _angle * M_PI / 180.0);
409 cairo_device_to_user(cr, &ww, &wh);
410 xa = (ww - _text_width) * _xalign;
411 ya = (wh - _text_height) * _yalign;
413 /* quick hack for left/bottom alignment at -90deg
414 * TODO this should be generalized incl rotation.
415 * currently only 'user' of this API is meter_strip.cc
417 if (_xalign < 0) xa = ceil(.5 + (ww * fabs(_xalign) + text_margin));
419 cairo_move_to (cr, xa, ya);
420 pango_cairo_update_layout(cr, _layout->gobj());
421 pango_cairo_show_layout (cr, _layout->gobj());
428 if (_elements & Menu) {
429 const float trih = ceil(_diameter * .5);
430 const float triw2 = ceil(.577 * _diameter * .5); // 1/sqrt(3) Equilateral triangle
432 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
433 cairo_move_to(cr, get_width() - triw2 - 3. , rint((get_height() + trih) * .5));
434 cairo_rel_line_to(cr, -triw2, -trih);
435 cairo_rel_line_to(cr, 2. * triw2, 0);
436 cairo_close_path(cr);
438 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
441 cairo_move_to(cr, get_width() - triw2 - 3 , rint((get_height() + trih) * .5));
442 cairo_rel_line_to(cr, .5 - triw2, .5 - trih);
443 cairo_rel_line_to(cr, 2. * triw2 - 1, 0);
444 cairo_close_path(cr);
445 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
446 cairo_set_line_width(cr, 1);
451 if (_elements & Indicator) {
454 /* move to the center of the indicator/led */
455 if (_elements & Text) {
456 int led_xoff = ceil(char_pixel_width() + _diameter * .5);
458 cairo_translate (cr, led_xoff, get_height() * .5);
460 cairo_translate (cr, get_width() - led_xoff, get_height() * .5);
463 cairo_translate (cr, get_width() * .5, get_height() * .5);
467 if (!_flat_buttons) {
468 cairo_arc (cr, 0, 0, _diameter * .5, 0, 2 * M_PI);
469 cairo_set_source (cr, led_inset_pattern);
474 cairo_set_source_rgb (cr, 0, 0, 0);
475 cairo_arc (cr, 0, 0, _diameter * .5 - 1, 0, 2 * M_PI);
479 ArdourCanvas::set_source_rgba (cr, led_color);
480 cairo_arc (cr, 0, 0, _diameter * .5 - 3, 0, 2 * M_PI);
486 // a transparent gray layer to indicate insensitivity
487 if ((visual_state() & Gtkmm2ext::Insensitive)) {
488 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
489 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
493 // if requested, show hovering
494 if (ARDOUR::Config->get_widget_prelight()
495 && !((visual_state() & Gtkmm2ext::Insensitive))) {
497 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
498 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
503 //user is currently pressing the button. dark outline helps to indicate this
504 if (_grabbed && !(_elements & (Inactive|Menu))) {
505 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
506 cairo_set_line_width(cr, 2);
507 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5);
511 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
512 if (visual_state() & Gtkmm2ext::Selected) {
513 #if 0 // outline, edge + 1px outside
514 cairo_set_line_width(cr, 2);
515 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
516 #else // replace edge (if any)
517 cairo_set_line_width(cr, 1);
518 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
520 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
524 //I guess this means we have keyboard focus. I don't think this works currently
526 //A: yes, it's keyboard focus and it does work when there's no editor window
527 // (the editor is always the first receiver for KeyDown).
528 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
530 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
531 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
533 cairo_set_dash (cr, &dashes, 1, 0);
534 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
535 cairo_set_line_width (cr, 1.0);
537 cairo_set_dash (cr, 0, 0, 0);
542 ArdourButton::set_diameter (float d)
544 _diameter = (d*2) + 5.0;
552 ArdourButton::set_corner_radius (float r)
555 CairoWidget::set_dirty ();
559 ArdourButton::on_realize()
561 CairoWidget::on_realize ();
563 if (_layout && _layout->get_text() != _text) {
564 _layout->set_text (_text);
569 ArdourButton::on_size_request (Gtk::Requisition* req)
571 req->width = req->height = 0;
572 CairoWidget::on_size_request (req);
574 if (_diameter == 0) {
575 _diameter = rint (ARDOUR::Config->get_font_scale () / 1024. / 7.5); // 11px with 80% font-scaling
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.6 * 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(char_pixel_width(), char_pixel_height()) * BASELINESTRETCH;
609 req->height = std::max(req->height, (int) wh);
612 if (_tweaks & Square) {
613 // squared buttons are also grouped, we cannot align all texts
614 // -> skip proper center adjustment
615 if (req->width < req->height)
616 req->width = req->height;
617 if (req->height < req->width)
618 req->height = req->width;
619 } else if (_text_width > 0 && !(_elements & (Menu | Indicator))) {
620 // properly centered text for those elements that are centered
621 if ((req->width - _text_width) & 1) { ++req->width; }
622 if ((req->height - _text_height) & 1) { ++req->height; }
627 * This sets the colors used for rendering based on the name of the button, and
628 * thus uses information from the GUI config data.
631 ArdourButton::set_colors ()
633 if (_fixed_colors_set) {
636 std::string name = get_name();
638 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
639 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
641 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
642 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
644 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
645 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
649 * This sets the colors used for rendering based on two fixed values, rather
650 * than basing them on the button name, and thus information in the GUI config
653 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
655 _fixed_colors_set = true;
657 fill_active_color = color_active;
658 fill_inactive_color = color_inactive;
660 unsigned char r, g, b, a;
661 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
663 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
664 (max (double(g), 255.) - min (double(g), 255.)) +
665 (max (double(b), 255.) - min (double(b), 255.));
667 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
668 (max (double(g), 0.) - min (double(g), 0.)) +
669 (max (double(b), 0.) - min (double(b), 0.));
671 text_active_color = (white_contrast > black_contrast) ?
672 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
673 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
676 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
678 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
679 (max (double(g), 255.) - min (double(g), 255.)) +
680 (max (double(b), 255.) - min (double(b), 255.));
682 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
683 (max (double(g), 0.) - min (double(g), 0.)) +
684 (max (double(b), 0.) - min (double(b), 0.));
686 text_inactive_color = (white_contrast > black_contrast) ?
687 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
688 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
690 /* XXX what about led colors ? */
692 /* trigger a "style-changed" message */
697 ArdourButton::build_patterns ()
699 if (convex_pattern) {
700 cairo_pattern_destroy (convex_pattern);
704 if (concave_pattern) {
705 cairo_pattern_destroy (concave_pattern);
709 if (led_inset_pattern) {
710 cairo_pattern_destroy (led_inset_pattern);
714 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
715 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
716 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
719 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, get_height());
720 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
721 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
723 if (_elements & Indicator) {
724 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
725 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
726 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
729 CairoWidget::set_dirty ();
733 ArdourButton::set_led_left (bool yn)
739 ArdourButton::on_button_press_event (GdkEventButton *ev)
741 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
742 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
743 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
748 if (binding_proxy.button_press_handler (ev)) {
753 CairoWidget::set_dirty ();
755 if (!_act_on_release) {
757 _action->activate ();
762 if (_fallthrough_to_parent)
769 ArdourButton::on_button_release_event (GdkEventButton *ev)
771 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
772 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
773 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
774 signal_led_clicked(); /* EMIT SIGNAL */
780 CairoWidget::set_dirty ();
784 if (_act_on_release) {
786 _action->activate ();
792 if (_fallthrough_to_parent)
799 ArdourButton::set_distinct_led_click (bool yn)
801 _distinct_led_click = yn;
806 ArdourButton::color_handler ()
808 _update_colors_and_patterns = true;
809 CairoWidget::set_dirty ();
813 ArdourButton::on_size_allocate (Allocation& alloc)
815 CairoWidget::on_size_allocate (alloc);
817 _update_colors_and_patterns = true;
821 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
823 watch_connection.disconnect ();
824 binding_proxy.set_controllable (c);
828 ArdourButton::watch ()
830 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
833 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
836 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
840 ArdourButton::controllable_changed ()
842 float val = binding_proxy.get_controllable()->get_value();
844 if (fabs (val) >= 0.5f) {
845 set_active_state (Gtkmm2ext::ExplicitActive);
847 unset_active_state ();
849 CairoWidget::set_dirty ();
853 ArdourButton::set_related_action (RefPtr<Action> act)
855 Gtkmm2ext::Activatable::set_related_action (act);
859 action_tooltip_changed ();
861 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
864 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
867 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
868 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
869 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
874 ArdourButton::action_toggled ()
876 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
879 if (tact->get_active()) {
880 set_active_state (Gtkmm2ext::ExplicitActive);
882 unset_active_state ();
888 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
894 ArdourButton::on_name_changed ()
896 _char_pixel_width = 0;
897 _char_pixel_height = 0;
899 _update_colors_and_patterns = true;
906 ArdourButton::setup_led_rect ()
908 if (!(_elements & Indicator)) {
914 _led_rect = new cairo_rectangle_t;
916 if (_elements & Text) {
918 _led_rect->x = char_pixel_width();
920 _led_rect->x = get_width() - char_pixel_width() + _diameter;
924 _led_rect->x = .5 * get_width() - _diameter;
927 _led_rect->y = .5 * (get_height() - _diameter);
928 _led_rect->width = _diameter;
929 _led_rect->height = _diameter;
933 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
939 CairoWidget::set_dirty ();
943 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
945 bool changed = (_active_state != s);
946 CairoWidget::set_active_state (s);
948 _update_colors_and_patterns = true;
949 CairoWidget::set_dirty ();
954 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
956 bool changed = (_visual_state != s);
957 CairoWidget::set_visual_state (s);
959 _update_colors_and_patterns = true;
960 CairoWidget::set_dirty ();
965 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
968 CairoWidget::set_dirty ();
969 return CairoWidget::on_focus_in_event (ev);
973 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
976 CairoWidget::set_dirty ();
977 return CairoWidget::on_focus_out_event (ev);
981 ArdourButton::on_key_release_event (GdkEventKey *ev) {
983 (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
987 _action->activate ();
991 return CairoWidget::on_key_release_event (ev);
995 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
997 _hovering = (_elements & Inactive) ? false : true;
999 if (ARDOUR::Config->get_widget_prelight()) {
1000 CairoWidget::set_dirty ();
1003 return CairoWidget::on_enter_notify_event (ev);
1007 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
1011 if (ARDOUR::Config->get_widget_prelight()) {
1012 CairoWidget::set_dirty ();
1015 return CairoWidget::on_leave_notify_event (ev);
1019 ArdourButton::set_tweaks (Tweaks t)
1023 CairoWidget::set_dirty ();
1028 ArdourButton::action_sensitivity_changed ()
1030 if (_action->property_sensitive ()) {
1031 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
1033 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
1038 ArdourButton::set_layout_ellisize_width (int w)
1040 if (_layout_ellipsize_width == w) {
1043 _layout_ellipsize_width = w;
1047 if (_layout_ellipsize_width > 0) {
1048 _layout->set_width (_layout_ellipsize_width);
1053 ArdourButton::set_text_ellipsize (Pango::EllipsizeMode e)
1055 if (_ellipsis == e) {
1062 _layout->set_ellipsize(_ellipsis);
1063 if (is_realized () && _layout_ellipsize_width > 0) {
1064 _layout->set_width (_layout_ellipsize_width);
1066 CairoWidget::set_dirty ();
1071 ArdourButton::ensure_layout ()
1075 _layout = Pango::Layout::create (get_pango_context());
1076 _layout->set_ellipsize(_ellipsis);
1077 if (_layout_ellipsize_width > 0) {
1078 _layout->set_width (_layout_ellipsize_width);
1084 ArdourButton::recalc_char_pixel_geometry ()
1086 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
1090 // NB. this is not static, since the geometry is different
1091 // depending on the font used.
1093 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1094 _layout->set_text (x);
1095 _layout->get_pixel_size (w, h);
1096 _char_pixel_height = std::max(4, h);
1097 // number of actual chars in the string (not bytes)
1098 // Glib to the rescue.
1099 Glib::ustring gx(x);
1100 _char_pixel_width = std::max(4, w / (int)gx.size());
1101 _layout->set_text (_text);
1105 ArdourButton::action_visibility_changed ()
1107 if (_action->property_visible ()) {
1115 ArdourButton::action_tooltip_changed ()
1117 string str = _action->property_tooltip().get_value();
1118 ARDOUR_UI::instance()->set_tip (*this, str);
1122 ArdourButton::set_elements (Element e)
1125 CairoWidget::set_dirty ();
1129 ArdourButton::add_elements (Element e)
1131 _elements = (ArdourButton::Element) (_elements | e);
1132 CairoWidget::set_dirty ();