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"
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 #include "gtkmm2ext/gui_thread.h"
33 #include "ardour/rc_configuration.h" // for widget prelight preference
35 #include "ardour_button.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
49 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
50 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
51 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
53 ArdourButton::ArdourButton (Element e)
55 , _tweaks (Tweaks (0))
59 , _corner_radius (9.0)
63 , led_inset_pattern (0)
64 , reflection_pattern (0)
66 , _act_on_release (true)
68 , _fixed_diameter (true)
69 , _distinct_led_click (false)
72 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
75 ArdourButton::ArdourButton (const std::string& str, Element e)
80 , _corner_radius (9.0)
83 , led_inset_pattern (0)
84 , reflection_pattern (0)
86 , _act_on_release (true)
88 , _fixed_diameter (true)
89 , _distinct_led_click (false)
95 ArdourButton::~ArdourButton()
101 ArdourButton::set_text (const std::string& str)
105 if (!_layout && !_text.empty()) {
106 _layout = Pango::Layout::create (get_pango_context());
110 _layout->set_text (str);
117 ArdourButton::set_markup (const std::string& str)
122 _layout = Pango::Layout::create (get_pango_context());
125 _layout->set_text (str);
130 ArdourButton::render (cairo_t* cr)
132 void (*rounded_function)(cairo_t*, double, double, double, double, double);
134 switch (_corner_mask) {
135 case 0x1: /* upper left only */
136 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
138 case 0x2: /* upper right only */
139 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
141 case 0x3: /* upper only */
142 rounded_function = Gtkmm2ext::rounded_top_rectangle;
144 /* should really have functions for lower right, lower left,
145 lower only, but for now, we don't
148 rounded_function = Gtkmm2ext::rounded_rectangle;
151 if (!_fixed_diameter) {
152 _diameter = std::min (get_width(), get_height());
155 if (_elements & Edge) {
156 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
157 cairo_set_source (cr, edge_pattern);
161 if (_elements & Body) {
162 if (_elements & Edge) {
163 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.0);
165 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius - 1.0);
167 cairo_set_source (cr, fill_pattern);
174 x = (get_width() - _pixbuf->get_width())/2.0;
175 y = (get_height() - _pixbuf->get_height())/2.0;
177 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
178 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
186 if (get_width() < 75) {
192 if ((_elements & Text) && !_text.empty()) {
194 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
196 if (_elements & Indicator) {
198 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
200 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
204 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
207 pango_cairo_show_layout (cr, _layout->gobj());
210 if (_elements & Indicator) {
212 /* move to the center of the indicator/led */
216 if (_elements & Text) {
218 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
220 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
223 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
227 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
228 cairo_set_source (cr, led_inset_pattern);
232 cairo_set_source_rgb (cr, 0, 0, 0);
233 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
237 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
238 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
242 cairo_scale(cr, 0.7, 0.7);
243 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
244 cairo_set_source (cr, reflection_pattern);
252 /* a partially transparent gray layer to indicate insensitivity */
254 if ((visual_state() & Gtkmm2ext::Insensitive)) {
255 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
256 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
260 /* if requested, show hovering */
262 if (ARDOUR::Config->get_widget_prelight()) {
264 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
265 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
272 ArdourButton::set_diameter (float d)
274 _diameter = (d*2) + 5.0;
276 if (_diameter != 0.0) {
277 _fixed_diameter = true;
284 ArdourButton::set_corner_radius (float r)
291 ArdourButton::on_size_request (Gtk::Requisition* req)
296 CairoWidget::on_size_request (req);
298 if ((_elements & Text) && !_text.empty()) {
299 _layout->get_pixel_size (_text_width, _text_height);
300 if (_text_width + _diameter < 75) {
314 if ((_elements & Indicator) && _fixed_diameter) {
316 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
317 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
319 req->width = _text_width + lrint (_diameter) + xpad;
320 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
324 req->width = _pixbuf->get_width() + xpad;
325 req->height = _pixbuf->get_height() + ypad;
327 req->width = _text_width + xpad;
328 req->height = _text_height + ypad;
334 ArdourButton::set_colors ()
336 uint32_t start_color;
342 /* we use the edge of the button to show Selected state, so the
343 * color/pattern used there will vary depending on that
347 cairo_pattern_destroy (edge_pattern);
350 if (_elements & Edge) {
352 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
353 if (visual_state() & Gtkmm2ext::Selected) {
354 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
355 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
357 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
358 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
360 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
361 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
362 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
363 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
367 /* the fill pattern is used to indicate Normal/Active/Mid state
371 cairo_pattern_destroy (fill_pattern);
374 if (_elements & Body) {
375 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
377 if (active_state() == Gtkmm2ext::Mid) {
378 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start mid", get_name()));
379 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end mid", get_name()));
380 } else if (active_state() == Gtkmm2ext::Active) {
381 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
382 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
384 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
385 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
387 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
388 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
389 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
390 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
393 if (led_inset_pattern) {
394 cairo_pattern_destroy (led_inset_pattern);
397 if (reflection_pattern) {
398 cairo_pattern_destroy (reflection_pattern);
401 if (_elements & Indicator) {
402 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
403 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
404 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
406 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
407 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
408 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
411 /* text and LED colors depend on Active/Normal/Mid */
413 if (active_state() == Gtkmm2ext::Active) {
414 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
415 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
416 } else if (active_state() == Gtkmm2ext::Mid) {
417 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text mid", get_name()));
418 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led mid", get_name()));
420 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
421 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
424 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
429 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
439 ArdourButton::set_led_left (bool yn)
445 ArdourButton::on_button_press_event (GdkEventButton *ev)
447 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
448 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
449 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
454 if (_tweaks & ShowClick) {
455 set_active_state (Gtkmm2ext::Active);
458 if (binding_proxy.button_press_handler (ev)) {
462 if (!_act_on_release) {
464 _action->activate ();
473 ArdourButton::on_button_release_event (GdkEventButton *ev)
475 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
476 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
477 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
478 signal_led_clicked(); /* EMIT SIGNAL */
483 if (_tweaks & ShowClick) {
484 unset_active_state ();
489 if (_act_on_release) {
491 _action->activate ();
501 ArdourButton::set_distinct_led_click (bool yn)
503 _distinct_led_click = yn;
508 ArdourButton::color_handler ()
515 ArdourButton::on_size_allocate (Allocation& alloc)
517 CairoWidget::on_size_allocate (alloc);
523 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
525 watch_connection.disconnect ();
526 binding_proxy.set_controllable (c);
530 ArdourButton::watch ()
532 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
535 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
539 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
543 ArdourButton::controllable_changed ()
545 float val = binding_proxy.get_controllable()->get_value();
547 if (fabs (val) >= 0.5f) {
548 set_active_state (Gtkmm2ext::Active);
550 unset_active_state ();
555 ArdourButton::set_related_action (RefPtr<Action> act)
557 Gtkmm2ext::Activatable::set_related_action (act);
561 action_tooltip_changed ();
563 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
566 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
569 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
570 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
571 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
576 ArdourButton::action_toggled ()
578 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
581 if (tact->get_active()) {
582 set_active_state (Gtkmm2ext::Active);
584 unset_active_state ();
590 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
596 ArdourButton::setup_led_rect ()
600 if (get_width() < 75) {
606 if (_elements & Indicator) {
607 _led_rect = new cairo_rectangle_t;
609 if (_elements & Text) {
611 _led_rect->x = text_margin;
613 _led_rect->x = get_width() - text_margin - _diameter/2.0;
617 _led_rect->x = get_width()/2.0 - _diameter/2.0;
620 _led_rect->y = get_height()/2.0 - _diameter/2.0;
621 _led_rect->width = _diameter;
622 _led_rect->height = _diameter;
631 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
638 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
640 bool changed = (_active_state != s);
641 CairoWidget::set_active_state (s);
648 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
650 bool changed = (_visual_state != s);
651 CairoWidget::set_visual_state (s);
658 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
662 if (ARDOUR::Config->get_widget_prelight()) {
666 return CairoWidget::on_enter_notify_event (ev);
670 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
674 if (ARDOUR::Config->get_widget_prelight()) {
678 return CairoWidget::on_leave_notify_event (ev);
682 ArdourButton::set_tweaks (Tweaks t)
691 ArdourButton::action_sensitivity_changed ()
693 if (_action->property_sensitive ()) {
694 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
696 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
703 ArdourButton::action_visibility_changed ()
705 if (_action->property_visible ()) {
713 ArdourButton::action_tooltip_changed ()
715 string str = _action->property_tooltip().get_value();
716 ARDOUR_UI::instance()->set_tip (*this, str);
720 ArdourButton::set_rounded_corner_mask (int mask)