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 REFLECTION_HEIGHT 2
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))
64 , _corner_radius (4.0)
69 , fill_inactive_color (0)
70 , fill_active_color (0)
71 , text_active_color(0)
72 , text_inactive_color(0)
74 , led_inactive_color(0)
77 , led_inset_pattern (0)
79 , _act_on_release (true)
81 , _fixed_diameter (true)
82 , _distinct_led_click (false)
85 , _fixed_colors_set (false)
86 , _fallthrough_to_parent (false)
88 ARDOUR_UI_UTILS::ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
91 ArdourButton::ArdourButton (const std::string& str, Element e)
93 , _tweaks (Tweaks (0))
97 , _corner_radius (4.0)
102 , fill_inactive_color (0)
103 , fill_active_color (0)
104 , text_active_color(0)
105 , text_inactive_color(0)
106 , led_active_color(0)
107 , led_inactive_color(0)
109 , concave_pattern (0)
110 , led_inset_pattern (0)
112 , _act_on_release (true)
114 , _fixed_diameter (true)
115 , _distinct_led_click (false)
118 , _fixed_colors_set (false)
123 ArdourButton::~ArdourButton()
127 if (convex_pattern) {
128 cairo_pattern_destroy (convex_pattern);
131 if (concave_pattern) {
132 cairo_pattern_destroy (concave_pattern);
135 if (led_inset_pattern) {
136 cairo_pattern_destroy (led_inset_pattern);
142 ArdourButton::set_text (const std::string& str)
146 if (!_layout && !_text.empty()) {
147 _layout = Pango::Layout::create (get_pango_context());
151 _layout->set_text (str);
158 ArdourButton::set_markup (const std::string& str)
163 _layout = Pango::Layout::create (get_pango_context());
166 _layout->set_markup (str);
171 ArdourButton::set_angle (const double angle)
177 ArdourButton::set_alignment (const float xa, const float ya)
184 ArdourButton::render (cairo_t* cr, cairo_rectangle_t *)
188 if ( active_state() == Gtkmm2ext::ExplicitActive ) {
189 text_color = text_active_color;
190 led_color = led_active_color;
192 text_color = text_inactive_color;
193 led_color = led_inactive_color;
196 void (*rounded_function)(cairo_t*, double, double, double, double, double);
198 switch (_corner_mask) {
199 case 0x1: /* upper left only */
200 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
202 case 0x2: /* upper right only */
203 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
205 case 0x3: /* upper only */
206 rounded_function = Gtkmm2ext::rounded_top_rectangle;
208 /* should really have functions for lower right, lower left,
209 lower only, but for now, we don't
212 rounded_function = Gtkmm2ext::rounded_rectangle;
215 if (!_fixed_diameter) {
216 _diameter = std::min (get_width(), get_height());
220 if ((_elements & Body)==Body) {
221 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
222 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
223 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
225 } else if ( (active_state() == Gtkmm2ext::ExplicitActive) && !((_elements & Indicator)==Indicator) ) {
227 ArdourCanvas::set_source_rgba (cr, fill_active_color);
229 } else { //inactive, or it has an indicator
231 ArdourCanvas::set_source_rgba (cr, fill_inactive_color);
236 //show the "convex" or "concave" gradient
237 if (!_flat_buttons) {
238 if ( active_state() == Gtkmm2ext::ExplicitActive && !((_elements & Indicator)==Indicator) ) {
240 cairo_set_source (cr, concave_pattern);
241 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
244 cairo_set_source (cr, convex_pattern);
245 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
250 // indicator border on top of gradient
251 if ((_elements & Body)==Body) {
252 if (active_state() == Gtkmm2ext::ImplicitActive && !((_elements & Indicator)==Indicator)) {
253 cairo_set_line_width (cr, 3.0);
254 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
255 ArdourCanvas::set_source_rgba (cr, fill_active_color);
261 if ((_elements & (Body|Edge)) == (Body|Edge)) {
262 rounded_function (cr, .5, .5, get_width() - 1, get_height() - 1, _corner_radius);
263 cairo_set_source_rgba (cr, 0, 0, 0, 1);
264 cairo_set_line_width (cr, 1.0);
272 x = (get_width() - _pixbuf->get_width())/2.0;
273 y = (get_height() - _pixbuf->get_height())/2.0;
275 //if this is a DropDown with an icon, then we need to
276 //move the icon left slightly to accomomodate the arrow
277 if (((_elements & Menu)==Menu)) {
279 cairo_translate (cr, -8,0 );
282 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
283 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
286 //..and then return to our previous drawing position
287 if (((_elements & Menu)==Menu)) {
293 if (get_width() < 75) {
300 if ( !_pixbuf && ((_elements & Text)==Text) && !_text.empty()) {
303 cairo_rectangle (cr, 2, 1, get_width()-4, get_height()-2);
307 ArdourCanvas::set_source_rgba (cr, text_color);
309 if ( (_elements & Menu) == Menu) {
310 cairo_move_to (cr, rint(text_margin), rint((get_height() - _text_height)/2.0));
311 pango_cairo_show_layout (cr, _layout->gobj());
312 } else if ( (_elements & Indicator) == Indicator) {
314 cairo_move_to (cr, rint(text_margin + _diameter + 4),rint((get_height() - _text_height)/2.0));
316 cairo_move_to (cr, rint(text_margin), rint((get_height() - _text_height)/2.0));
318 pango_cairo_show_layout (cr, _layout->gobj());
326 cairo_save (cr); // TODO retain rotataion.. adj. LED,...
327 cairo_rotate(cr, _angle * M_PI / 180.0);
328 cairo_device_to_user(cr, &ww, &wh);
329 xa = (ww - _text_width) * _xalign;
330 ya = (wh - _text_height) * _yalign;
332 /* quick hack for left/bottom alignment at -90deg
333 * TODO this should be generalized incl rotation.
334 * currently only 'user' of this API is meter_strip.cc
336 if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
338 // TODO honor left/right text_margin with min/max()
340 cairo_move_to (cr, rint(xa), rint(ya));
341 pango_cairo_update_layout(cr, _layout->gobj());
342 pango_cairo_show_layout (cr, _layout->gobj());
349 if (((_elements & Menu)==Menu)) {
354 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
355 cairo_move_to(cr, get_width() - ((_diameter/2.0) + 6.0), get_height()/2.0 +_diameter/4);
356 cairo_rel_line_to(cr, -_diameter/2, -_diameter/2);
357 cairo_rel_line_to(cr, _diameter, 0);
358 cairo_close_path(cr);
359 cairo_set_source_rgba (cr, 1, 1, 1, 0.4);
360 cairo_fill_preserve(cr);
361 cairo_set_source_rgba (cr, 0, 0, 0, 0.8);
362 cairo_set_line_width(cr, 0.5);
369 if (((_elements & Indicator)==Indicator)) {
371 /* move to the center of the indicator/led */
375 if (_elements & Text) {
377 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
379 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
382 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
386 if (!_flat_buttons) {
387 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
388 cairo_set_source (cr, led_inset_pattern);
393 cairo_set_source_rgb (cr, 0, 0, 0);
394 cairo_arc (cr, 0, 0, _diameter/2-1, 0, 2 * M_PI);
398 ArdourCanvas::set_source_rgba (cr, led_color);
399 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
406 // a transparent gray layer to indicate insensitivity
407 if ((visual_state() & Gtkmm2ext::Insensitive)) {
408 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
409 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
413 // if requested, show hovering
414 if (ARDOUR::Config->get_widget_prelight()
415 && !((visual_state() & Gtkmm2ext::Insensitive))) {
417 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
418 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
423 //user is currently pressing the button. black outline helps to indicate this
424 if ( _grabbed && !(_elements & (Inactive|Menu))) {
425 rounded_function (cr, 1, 1, get_width() - 2, get_height() - 2, _corner_radius);
426 cairo_set_line_width(cr, 2);
427 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, .5); // XXX no longer 'black'
431 //some buttons (like processor boxes) can be selected (so they can be deleted). Draw a selection indicator
432 if (visual_state() & Gtkmm2ext::Selected) {
433 #if 0 // outline, edge + 1px outside
434 cairo_set_line_width(cr, 2);
435 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
436 #else // replace edge (if any)
437 cairo_set_line_width(cr, 1);
438 cairo_set_source_rgba (cr, 1, 0, 0, 0.8);
440 rounded_function (cr, 0.5, 0.5, get_width() - 1, get_height() - 1, _corner_radius);
444 //I guess this means we have keyboard focus. I don't think this works currently
446 //A: yes, it's keyboard focus and it does work when there's no editor window
447 // (the editor is always the first receiver for KeyDown).
448 // It's needed for eg. the engine-dialog at startup or after closing a sesion.
450 rounded_function (cr, 1.5, 1.5, get_width() - 3, get_height() - 3, _corner_radius);
451 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.8);
453 cairo_set_dash (cr, &dashes, 1, 0);
454 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
455 cairo_set_line_width (cr, 1.0);
457 cairo_set_dash (cr, 0, 0, 0);
462 ArdourButton::set_diameter (float d)
464 _diameter = (d*2) + 5.0;
466 if (_diameter != 0.0) {
467 _fixed_diameter = true;
475 ArdourButton::set_corner_radius (float r)
482 ArdourButton::on_size_request (Gtk::Requisition* req)
487 CairoWidget::on_size_request (req);
489 if ((_elements & Text) && !_text.empty()) {
491 //calc our real width for our string (but ignore the height, because that results in inconsistent button heights)
493 _layout->get_pixel_size (_text_width, ignored);
495 //calc the height using some text with both ascenders and descenders
496 std::string t = _layout->get_text();
497 _layout->set_text ("WjgO"); //what we put here probably doesn't matter, as long as its the same for everyone
498 _layout->get_pixel_size (ignored, _text_height);
499 _layout->set_text (t);
501 if (_text_width + _diameter < 75) {
515 if ((_elements & Indicator) && _fixed_diameter) {
517 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
518 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
520 req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
521 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
525 req->width = _pixbuf->get_width() + xpad;
526 req->height = _pixbuf->get_height() + ypad;
528 req->width = _text_width + xpad;
529 req->height = _text_height + ypad;
532 req->width += _corner_radius;
536 * This sets the colors used for rendering based on the name of the button, and
537 * thus uses information from the GUI config data.
540 ArdourButton::set_colors ()
542 if (_fixed_colors_set) {
545 std::string name = get_name();
547 fill_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill active", name));
548 fill_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill", name));
550 text_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", name));
551 text_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", name));
553 led_active_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", name));
554 led_inactive_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", name));
557 * This sets the colors used for rendering based on two fixed values, rather
558 * than basing them on the button name, and thus information in the GUI config
561 void ArdourButton::set_fixed_colors (const uint32_t color_active, const uint32_t color_inactive)
563 _fixed_colors_set = true;
565 fill_active_color = color_active;
566 fill_inactive_color = color_inactive;
568 unsigned char r, g, b, a;
569 UINT_TO_RGBA(color_active, &r, &g, &b, &a);
571 double white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
572 (max (double(g), 255.) - min (double(g), 255.)) +
573 (max (double(b), 255.) - min (double(b), 255.));
575 double black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
576 (max (double(g), 0.) - min (double(g), 0.)) +
577 (max (double(b), 0.) - min (double(b), 0.));
579 text_active_color = (white_contrast > black_contrast) ?
580 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
581 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
584 UINT_TO_RGBA(color_inactive, &r, &g, &b, &a);
586 white_contrast = (max (double(r), 255.) - min (double(r), 255.)) +
587 (max (double(g), 255.) - min (double(g), 255.)) +
588 (max (double(b), 255.) - min (double(b), 255.));
590 black_contrast = (max (double(r), 0.) - min (double(r), 0.)) +
591 (max (double(g), 0.) - min (double(g), 0.)) +
592 (max (double(b), 0.) - min (double(b), 0.));
594 text_inactive_color = (white_contrast > black_contrast) ?
595 RGBA_TO_UINT(255, 255, 255, 255) : /* use white */
596 RGBA_TO_UINT( 0, 0, 0, 255); /* use black */
598 /* XXX what about led colors ? */
601 set_name (""); /* this will trigger a "style-changed" message and then set_colors() */
605 ArdourButton::build_patterns ()
607 if (convex_pattern) {
608 cairo_pattern_destroy (convex_pattern);
612 if (concave_pattern) {
613 cairo_pattern_destroy (concave_pattern);
617 if (led_inset_pattern) {
618 cairo_pattern_destroy (led_inset_pattern);
621 float width = get_width(); float height = get_height();
624 convex_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, height);
625 cairo_pattern_add_color_stop_rgba (convex_pattern, 0.0, 0,0,0, 0.0);
626 cairo_pattern_add_color_stop_rgba (convex_pattern, 1.0, 0,0,0, 0.35);
629 concave_pattern = cairo_pattern_create_linear (0.0, 0, 0.0, height);
630 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.0, 0,0,0, 0.5);
631 cairo_pattern_add_color_stop_rgba (concave_pattern, 0.7, 0,0,0, 0.0);
633 if (_elements & Indicator) {
634 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
635 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
636 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
643 ArdourButton::set_led_left (bool yn)
649 ArdourButton::on_button_press_event (GdkEventButton *ev)
651 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
652 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
653 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
658 if (binding_proxy.button_press_handler (ev)) {
665 if (!_act_on_release) {
667 _action->activate ();
672 if (_fallthrough_to_parent)
679 ArdourButton::on_button_release_event (GdkEventButton *ev)
681 if (_hovering && (_elements & Indicator) && _led_rect && _distinct_led_click) {
682 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
683 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
684 signal_led_clicked(); /* EMIT SIGNAL */
694 if (_act_on_release) {
696 _action->activate ();
702 if (_fallthrough_to_parent)
709 ArdourButton::set_distinct_led_click (bool yn)
711 _distinct_led_click = yn;
716 ArdourButton::color_handler ()
724 ArdourButton::on_size_allocate (Allocation& alloc)
726 CairoWidget::on_size_allocate (alloc);
732 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
734 watch_connection.disconnect ();
735 binding_proxy.set_controllable (c);
739 ArdourButton::watch ()
741 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
744 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
748 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
752 ArdourButton::controllable_changed ()
754 float val = binding_proxy.get_controllable()->get_value();
756 if (fabs (val) >= 0.5f) {
757 set_active_state (Gtkmm2ext::ExplicitActive);
759 unset_active_state ();
764 ArdourButton::set_related_action (RefPtr<Action> act)
766 Gtkmm2ext::Activatable::set_related_action (act);
770 action_tooltip_changed ();
772 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
775 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
778 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
779 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
780 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
785 ArdourButton::action_toggled ()
787 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
790 if (tact->get_active()) {
791 set_active_state (Gtkmm2ext::ExplicitActive);
793 unset_active_state ();
799 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
806 ArdourButton::on_name_changed ()
813 ArdourButton::setup_led_rect ()
817 if (get_width() < 75) {
823 if (_elements & Indicator) {
824 _led_rect = new cairo_rectangle_t;
826 if (_elements & Text) {
828 _led_rect->x = text_margin;
830 _led_rect->x = get_width() - text_margin - _diameter/2.0;
834 _led_rect->x = get_width()/2.0 - _diameter/2.0;
837 _led_rect->y = get_height()/2.0 - _diameter/2.0;
838 _led_rect->width = _diameter;
839 _led_rect->height = _diameter;
848 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
855 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
857 bool changed = (_active_state != s);
858 CairoWidget::set_active_state (s);
866 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
868 bool changed = (_visual_state != s);
869 CairoWidget::set_visual_state (s);
878 ArdourButton::on_focus_in_event (GdkEventFocus* ev)
882 return CairoWidget::on_focus_in_event (ev);
886 ArdourButton::on_focus_out_event (GdkEventFocus* ev)
890 return CairoWidget::on_focus_out_event (ev);
894 ArdourButton::on_key_release_event (GdkEventKey *ev) {
896 (ev->keyval == GDK_KEY_space || ev->keyval == GDK_Return))
900 _action->activate ();
904 return CairoWidget::on_key_release_event (ev);
908 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
910 _hovering = (_elements & Inactive) ? false : true;
912 if (ARDOUR::Config->get_widget_prelight()) {
916 return CairoWidget::on_enter_notify_event (ev);
920 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
924 if (ARDOUR::Config->get_widget_prelight()) {
928 return CairoWidget::on_leave_notify_event (ev);
932 ArdourButton::set_tweaks (Tweaks t)
941 ArdourButton::action_sensitivity_changed ()
943 if (_action->property_sensitive ()) {
944 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
946 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
953 ArdourButton::action_visibility_changed ()
955 if (_action->property_visible ()) {
963 ArdourButton::action_tooltip_changed ()
965 string str = _action->property_tooltip().get_value();
966 ARDOUR_UI::instance()->set_tip (*this, str);
970 ArdourButton::set_elements (Element e)
976 ArdourButton::add_elements (Element e)
978 _elements = (ArdourButton::Element) (_elements | e);