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))
56 , _act_on_release (true)
60 , _corner_radius (9.0)
63 , led_inset_pattern (0)
64 , reflection_pattern (0)
66 , _fixed_diameter (true)
67 , _distinct_led_click (false)
71 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
74 ArdourButton::ArdourButton (const std::string& str, Element e)
76 , _act_on_release (true)
80 , _corner_radius (9.0)
83 , led_inset_pattern (0)
84 , reflection_pattern (0)
86 , _fixed_diameter (true)
87 , _distinct_led_click (false)
94 ArdourButton::~ArdourButton()
100 ArdourButton::set_text (const std::string& str)
104 if (!_layout && !_text.empty()) {
105 _layout = Pango::Layout::create (get_pango_context());
109 _layout->set_text (str);
116 ArdourButton::set_markup (const std::string& str)
121 _layout = Pango::Layout::create (get_pango_context());
124 _layout->set_text (str);
129 ArdourButton::render (cairo_t* cr)
131 if (!_fixed_diameter) {
132 _diameter = std::min (get_width(), get_height());
135 if (_elements & Edge) {
136 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius);
137 cairo_set_source (cr, edge_pattern);
141 if (_elements & Body) {
142 if (_elements & Edge) {
143 Gtkmm2ext::rounded_rectangle (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.0);
145 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius - 1.0);
147 cairo_set_source (cr, fill_pattern);
154 x = (get_width() - _pixbuf->get_width())/2.0;
155 y = (get_height() - _pixbuf->get_height())/2.0;
157 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
158 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
166 if (get_width() < 75) {
172 if ((_elements & Text) && !_text.empty()) {
174 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
176 if (_elements & Indicator) {
178 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
180 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
184 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
187 pango_cairo_show_layout (cr, _layout->gobj());
190 if (_elements & Indicator) {
192 /* move to the center of the indicator/led */
196 if (_elements & Text) {
198 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
200 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
203 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
207 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
208 cairo_set_source (cr, led_inset_pattern);
212 cairo_set_source_rgb (cr, 0, 0, 0);
213 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
217 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
218 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
222 cairo_scale(cr, 0.7, 0.7);
223 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
224 cairo_set_source (cr, reflection_pattern);
232 /* a partially transparent gray layer to indicate insensitivity */
234 if ((visual_state() & Gtkmm2ext::Insensitive)) {
235 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius);
236 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
240 /* if requested, show hovering */
242 if (ARDOUR::Config->get_widget_prelight()) {
244 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), _corner_radius);
245 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
252 ArdourButton::set_diameter (float d)
254 _diameter = (d*2) + 5.0;
256 if (_diameter != 0.0) {
257 _fixed_diameter = true;
264 ArdourButton::set_corner_radius (float r)
271 ArdourButton::on_size_request (Gtk::Requisition* req)
276 CairoWidget::on_size_request (req);
278 if ((_elements & Text) && !_text.empty()) {
279 _layout->get_pixel_size (_text_width, _text_height);
280 if (_text_width + _diameter < 75) {
294 if ((_elements & Indicator) && _fixed_diameter) {
296 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
297 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
299 req->width = _text_width + lrint (_diameter) + xpad;
300 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
304 req->width = _pixbuf->get_width() + xpad;
305 req->height = _pixbuf->get_height() + ypad;
307 req->width = _text_width + xpad;
308 req->height = _text_height + ypad;
314 ArdourButton::set_colors ()
316 uint32_t start_color;
322 /* we use the edge of the button to show Selected state, so the
323 * color/pattern used there will vary depending on that
327 cairo_pattern_destroy (edge_pattern);
330 if (_elements & Edge) {
332 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
333 if (visual_state() & Gtkmm2ext::Selected) {
334 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
335 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
337 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
338 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
340 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
341 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
342 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
343 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
347 /* the fill pattern is used to indicate Normal/Active/Mid state
351 cairo_pattern_destroy (fill_pattern);
354 if (_elements & Body) {
355 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
357 if (active_state() == Gtkmm2ext::Mid) {
358 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start mid", get_name()));
359 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end mid", get_name()));
360 } else if (active_state() == Gtkmm2ext::Active) {
361 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
362 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
364 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
365 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
367 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
368 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
369 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
370 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
373 if (led_inset_pattern) {
374 cairo_pattern_destroy (led_inset_pattern);
377 if (reflection_pattern) {
378 cairo_pattern_destroy (reflection_pattern);
381 if (_elements & Indicator) {
382 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
383 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
384 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
386 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
387 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
388 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
391 /* text and LED colors depend on Active/Normal/Mid */
393 if (active_state() == Gtkmm2ext::Active) {
394 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
395 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
396 } else if (active_state() == Gtkmm2ext::Mid) {
397 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text mid", get_name()));
398 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led mid", get_name()));
400 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
401 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
404 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
409 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
419 ArdourButton::set_led_left (bool yn)
425 ArdourButton::on_button_press_event (GdkEventButton *ev)
427 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
428 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
429 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
434 if (_tweaks & ShowClick) {
435 set_active_state (Gtkmm2ext::Active);
438 if (binding_proxy.button_press_handler (ev)) {
442 if (!_act_on_release) {
444 _action->activate ();
453 ArdourButton::on_button_release_event (GdkEventButton *ev)
455 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
456 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
457 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
458 signal_led_clicked(); /* EMIT SIGNAL */
463 if (_tweaks & ShowClick) {
464 unset_active_state ();
469 if (_act_on_release) {
471 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
472 _action->activate ();
482 ArdourButton::set_distinct_led_click (bool yn)
484 _distinct_led_click = yn;
489 ArdourButton::color_handler ()
496 ArdourButton::on_size_allocate (Allocation& alloc)
498 CairoWidget::on_size_allocate (alloc);
504 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
506 watch_connection.disconnect ();
507 binding_proxy.set_controllable (c);
511 ArdourButton::watch ()
513 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
516 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
520 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
524 ArdourButton::controllable_changed ()
526 float val = binding_proxy.get_controllable()->get_value();
528 if (fabs (val) >= 0.5f) {
529 set_active_state (Gtkmm2ext::Active);
531 unset_active_state ();
536 ArdourButton::set_related_action (RefPtr<Action> act)
542 string str = _action->property_tooltip().get_value();
544 ARDOUR_UI::instance()->set_tip (*this, str);
547 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
549 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
552 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
553 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
554 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
559 ArdourButton::action_toggled ()
561 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
564 if (tact->get_active()) {
565 set_active_state (Gtkmm2ext::Active);
567 unset_active_state ();
573 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
579 ArdourButton::setup_led_rect ()
583 if (get_width() < 75) {
589 if (_elements & Indicator) {
590 _led_rect = new cairo_rectangle_t;
592 if (_elements & Text) {
594 _led_rect->x = text_margin;
596 _led_rect->x = get_width() - text_margin - _diameter/2.0;
600 _led_rect->x = get_width()/2.0 - _diameter/2.0;
603 _led_rect->y = get_height()/2.0 - _diameter/2.0;
604 _led_rect->width = _diameter;
605 _led_rect->height = _diameter;
614 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
621 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
623 bool changed = (_active_state != s);
624 CairoWidget::set_active_state (s);
631 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
633 bool changed = (_visual_state != s);
634 CairoWidget::set_visual_state (s);
641 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
645 if (ARDOUR::Config->get_widget_prelight()) {
649 return CairoWidget::on_enter_notify_event (ev);
653 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
657 if (ARDOUR::Config->get_widget_prelight()) {
661 return CairoWidget::on_leave_notify_event (ev);
665 ArdourButton::set_tweaks (Tweaks t)
674 ArdourButton::action_sensitivity_changed ()
676 if (_action->property_sensitive ()) {
677 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
679 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
686 ArdourButton::action_visibility_changed ()
688 if (_action->property_visible ()) {
696 ArdourButton::action_tooltip_changed ()
698 string str = _action->property_tooltip().get_value();
699 ARDOUR_UI::instance()->set_tip (*this, str);