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 (5.0)
63 , inactive_pattern (0)
64 , led_inset_pattern (0)
65 , reflection_pattern (0)
67 , _act_on_release (true)
69 , _fixed_diameter (true)
70 , _distinct_led_click (false)
73 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
76 ArdourButton::ArdourButton (const std::string& str, Element e)
81 , _corner_radius (9.0)
84 , inactive_pattern (0)
85 , led_inset_pattern (0)
86 , reflection_pattern (0)
88 , _act_on_release (true)
90 , _fixed_diameter (true)
91 , _distinct_led_click (false)
97 ArdourButton::~ArdourButton()
102 cairo_pattern_destroy (edge_pattern);
105 if (active_pattern) {
106 cairo_pattern_destroy (active_pattern);
109 if (inactive_pattern) {
110 cairo_pattern_destroy (inactive_pattern);
115 ArdourButton::set_text (const std::string& str)
119 if (!_layout && !_text.empty()) {
120 _layout = Pango::Layout::create (get_pango_context());
124 _layout->set_text (str);
131 ArdourButton::set_markup (const std::string& str)
136 _layout = Pango::Layout::create (get_pango_context());
139 _layout->set_text (str);
144 ArdourButton::render (cairo_t* cr)
146 void (*rounded_function)(cairo_t*, double, double, double, double, double);
148 switch (_corner_mask) {
149 case 0x1: /* upper left only */
150 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
152 case 0x2: /* upper right only */
153 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
155 case 0x3: /* upper only */
156 rounded_function = Gtkmm2ext::rounded_top_rectangle;
158 /* should really have functions for lower right, lower left,
159 lower only, but for now, we don't
162 rounded_function = Gtkmm2ext::rounded_rectangle;
165 if (!_fixed_diameter) {
166 _diameter = std::min (get_width(), get_height());
169 if (_elements & Edge) {
170 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
171 cairo_set_source (cr, edge_pattern);
175 if (_elements & Body) {
176 if (_elements & Edge) {
177 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.0);
179 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius - 1.0);
182 if (active_state() == Gtkmm2ext::ImplicitActive) {
183 cairo_set_source (cr, inactive_pattern);
184 cairo_fill_preserve (cr);
185 cairo_set_line_width (cr, 2.0);
186 cairo_set_source (cr, active_pattern);
188 } else if (active_state() == Gtkmm2ext::ExplicitActive) {
189 cairo_set_source (cr, active_pattern);
192 cairo_set_source (cr, inactive_pattern);
200 x = (get_width() - _pixbuf->get_width())/2.0;
201 y = (get_height() - _pixbuf->get_height())/2.0;
203 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
204 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
212 if (get_width() < 75) {
218 if ((_elements & Text) && !_text.empty()) {
222 if (_elements & Indicator) {
224 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
226 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
230 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
233 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
234 pango_cairo_show_layout (cr, _layout->gobj());
237 if (_elements & Indicator) {
239 /* move to the center of the indicator/led */
243 if (_elements & Text) {
245 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
247 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
250 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
254 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
255 cairo_set_source (cr, led_inset_pattern);
259 cairo_set_source_rgb (cr, 0, 0, 0);
260 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
264 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
265 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
269 cairo_scale(cr, 0.7, 0.7);
270 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
271 cairo_set_source (cr, reflection_pattern);
279 /* a partially transparent gray layer to indicate insensitivity */
281 if ((visual_state() & Gtkmm2ext::Insensitive)) {
282 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
283 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
287 /* if requested, show hovering */
289 if (ARDOUR::Config->get_widget_prelight()) {
291 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
292 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
299 ArdourButton::set_diameter (float d)
301 _diameter = (d*2) + 5.0;
303 if (_diameter != 0.0) {
304 _fixed_diameter = true;
311 ArdourButton::set_corner_radius (float r)
318 ArdourButton::on_size_request (Gtk::Requisition* req)
323 CairoWidget::on_size_request (req);
325 if ((_elements & Text) && !_text.empty()) {
326 _layout->get_pixel_size (_text_width, _text_height);
327 if (_text_width + _diameter < 75) {
341 if ((_elements & Indicator) && _fixed_diameter) {
343 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
344 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
346 req->width = _text_width + lrint (_diameter) + xpad;
347 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
351 req->width = _pixbuf->get_width() + xpad;
352 req->height = _pixbuf->get_height() + ypad;
354 req->width = _text_width + xpad;
355 req->height = _text_height + ypad;
361 ArdourButton::set_colors ()
363 uint32_t start_color;
369 /* we use the edge of the button to show Selected state, so the
370 * color/pattern used there will vary depending on that
374 cairo_pattern_destroy (edge_pattern);
378 if (_elements & Edge) {
380 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
381 if (visual_state() & Gtkmm2ext::Selected) {
382 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
383 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
385 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
386 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
388 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
389 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
390 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
391 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
394 if (active_pattern) {
395 cairo_pattern_destroy (active_pattern);
399 if (inactive_pattern) {
400 cairo_pattern_destroy (inactive_pattern);
401 inactive_pattern = 0;
404 if (_elements & Body) {
406 active_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
407 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
408 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
409 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
416 cairo_pattern_add_color_stop_rgba (active_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
417 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
418 cairo_pattern_add_color_stop_rgba (active_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
420 inactive_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
421 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
422 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
423 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
424 cairo_pattern_add_color_stop_rgba (inactive_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
425 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
426 cairo_pattern_add_color_stop_rgba (inactive_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
429 if (led_inset_pattern) {
430 cairo_pattern_destroy (led_inset_pattern);
433 if (reflection_pattern) {
434 cairo_pattern_destroy (reflection_pattern);
437 if (_elements & Indicator) {
438 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
439 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
440 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
442 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
443 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
444 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
447 /* text and LED colors */
449 if (active_state() == Gtkmm2ext::ExplicitActive) {
450 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
451 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
453 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
454 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
457 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
462 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
472 ArdourButton::set_led_left (bool yn)
478 ArdourButton::on_button_press_event (GdkEventButton *ev)
480 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
481 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
482 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
487 if (_tweaks & ShowClick) {
488 set_active_state (Gtkmm2ext::ExplicitActive);
491 if (binding_proxy.button_press_handler (ev)) {
495 if (!_act_on_release) {
497 _action->activate ();
506 ArdourButton::on_button_release_event (GdkEventButton *ev)
508 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
509 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
510 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
511 signal_led_clicked(); /* EMIT SIGNAL */
516 if (_tweaks & ShowClick) {
517 unset_active_state ();
522 if (_act_on_release) {
524 _action->activate ();
534 ArdourButton::set_distinct_led_click (bool yn)
536 _distinct_led_click = yn;
541 ArdourButton::color_handler ()
548 ArdourButton::on_size_allocate (Allocation& alloc)
550 CairoWidget::on_size_allocate (alloc);
556 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
558 watch_connection.disconnect ();
559 binding_proxy.set_controllable (c);
563 ArdourButton::watch ()
565 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
568 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
572 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
576 ArdourButton::controllable_changed ()
578 float val = binding_proxy.get_controllable()->get_value();
580 if (fabs (val) >= 0.5f) {
581 set_active_state (Gtkmm2ext::ExplicitActive);
583 unset_active_state ();
588 ArdourButton::set_related_action (RefPtr<Action> act)
590 Gtkmm2ext::Activatable::set_related_action (act);
594 action_tooltip_changed ();
596 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
599 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
602 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
603 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
604 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
609 ArdourButton::action_toggled ()
611 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
614 if (tact->get_active()) {
615 set_active_state (Gtkmm2ext::ExplicitActive);
617 unset_active_state ();
623 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
629 ArdourButton::setup_led_rect ()
633 if (get_width() < 75) {
639 if (_elements & Indicator) {
640 _led_rect = new cairo_rectangle_t;
642 if (_elements & Text) {
644 _led_rect->x = text_margin;
646 _led_rect->x = get_width() - text_margin - _diameter/2.0;
650 _led_rect->x = get_width()/2.0 - _diameter/2.0;
653 _led_rect->y = get_height()/2.0 - _diameter/2.0;
654 _led_rect->width = _diameter;
655 _led_rect->height = _diameter;
664 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
671 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
673 bool changed = (_active_state != s);
674 CairoWidget::set_active_state (s);
681 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
683 bool changed = (_visual_state != s);
684 CairoWidget::set_visual_state (s);
691 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
695 if (ARDOUR::Config->get_widget_prelight()) {
699 return CairoWidget::on_enter_notify_event (ev);
703 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
707 if (ARDOUR::Config->get_widget_prelight()) {
711 return CairoWidget::on_leave_notify_event (ev);
715 ArdourButton::set_tweaks (Tweaks t)
724 ArdourButton::action_sensitivity_changed ()
726 if (_action->property_sensitive ()) {
727 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
729 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
736 ArdourButton::action_visibility_changed ()
738 if (_action->property_visible ()) {
746 ArdourButton::action_tooltip_changed ()
748 string str = _action->property_tooltip().get_value();
749 ARDOUR_UI::instance()->set_tip (*this, str);
753 ArdourButton::set_rounded_corner_mask (int mask)
760 ArdourButton::set_elements (Element e)