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);
52 cairo_pattern_t* ArdourButton::mid_mask = 0;
55 ArdourButton::create_mid_mask ()
57 cairo_surface_t *surface;
59 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, 32, 32);
61 cairo_t* cr2 = cairo_create (surface);
63 cairo_set_source_rgba (cr2, 1.0, 0.0, 0.0, 1.0);
64 cairo_set_line_join (cr2, CAIRO_LINE_JOIN_MITER);
66 /* some of these lines extend outside the image surface - we just rely
67 * on cairo clipping them since it makes the math easier to visualize
71 cairo_move_to (cr2, 0.0, 4.0);
72 cairo_line_to (cr2, 4.0, 0.0);
74 cairo_move_to (cr2, 0.0, 12.0);
75 cairo_line_to (cr2, 12.0, 0.0);
77 cairo_move_to (cr2, 0.0, 20.0);
78 cairo_line_to (cr2, 20.0, 0.0);
80 cairo_move_to (cr2, 0.0, 28.0);
81 cairo_line_to (cr2, 28.0, 0.0);
83 cairo_move_to (cr2, 0.0, 28.0);
84 cairo_line_to (cr2, 28.0, 0.0);
86 cairo_move_to (cr2, 0.0, 36.0);
87 cairo_line_to (cr2, 36.0, 0.0);
89 cairo_move_to (cr2, 0.0, 44.0);
90 cairo_line_to (cr2, 44.0, 0.0);
92 cairo_move_to (cr2, 0.0, 52.0);
93 cairo_line_to (cr2, 52.0, 0.0);
95 cairo_set_line_width (cr2, 3.0);
98 mid_mask = cairo_pattern_create_for_surface (surface);
99 cairo_pattern_set_extend (mid_mask, CAIRO_EXTEND_REPEAT);
100 cairo_pattern_set_filter (mid_mask, CAIRO_FILTER_NEAREST);
102 cairo_surface_destroy (surface);
106 ArdourButton::ArdourButton (Element e)
108 , _tweaks (Tweaks (0))
112 , _corner_radius (9.0)
116 , inactive_pattern (0)
117 , led_inset_pattern (0)
118 , reflection_pattern (0)
120 , _act_on_release (true)
122 , _fixed_diameter (true)
123 , _distinct_led_click (false)
126 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
132 ArdourButton::ArdourButton (const std::string& str, Element e)
137 , _corner_radius (9.0)
140 , inactive_pattern (0)
141 , led_inset_pattern (0)
142 , reflection_pattern (0)
144 , _act_on_release (true)
146 , _fixed_diameter (true)
147 , _distinct_led_click (false)
157 ArdourButton::~ArdourButton()
162 cairo_pattern_destroy (edge_pattern);
165 if (active_pattern) {
166 cairo_pattern_destroy (active_pattern);
169 if (inactive_pattern) {
170 cairo_pattern_destroy (inactive_pattern);
175 ArdourButton::set_text (const std::string& str)
179 if (!_layout && !_text.empty()) {
180 _layout = Pango::Layout::create (get_pango_context());
184 _layout->set_text (str);
191 ArdourButton::set_markup (const std::string& str)
196 _layout = Pango::Layout::create (get_pango_context());
199 _layout->set_text (str);
204 ArdourButton::render (cairo_t* cr)
206 void (*rounded_function)(cairo_t*, double, double, double, double, double);
208 switch (_corner_mask) {
209 case 0x1: /* upper left only */
210 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
212 case 0x2: /* upper right only */
213 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
215 case 0x3: /* upper only */
216 rounded_function = Gtkmm2ext::rounded_top_rectangle;
218 /* should really have functions for lower right, lower left,
219 lower only, but for now, we don't
222 rounded_function = Gtkmm2ext::rounded_rectangle;
225 if (!_fixed_diameter) {
226 _diameter = std::min (get_width(), get_height());
229 if (_elements & Edge) {
230 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
231 cairo_set_source (cr, edge_pattern);
235 if (_elements & Body) {
236 if (_elements & Edge) {
237 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.0);
239 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius - 1.0);
242 if (active_state() == Gtkmm2ext::ImplicitActive) {
243 cairo_set_source (cr, inactive_pattern);
244 cairo_fill_preserve (cr);
245 cairo_set_line_width (cr, 2.0);
246 cairo_set_source (cr, active_pattern);
248 } else if (active_state() == Gtkmm2ext::ExplicitActive) {
249 cairo_set_source (cr, active_pattern);
252 cairo_set_source (cr, inactive_pattern);
260 x = (get_width() - _pixbuf->get_width())/2.0;
261 y = (get_height() - _pixbuf->get_height())/2.0;
263 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
264 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
272 if (get_width() < 75) {
278 if ((_elements & Text) && !_text.empty()) {
282 if (_elements & Indicator) {
284 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
286 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
290 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
293 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
294 pango_cairo_show_layout (cr, _layout->gobj());
297 if (_elements & Indicator) {
299 /* move to the center of the indicator/led */
303 if (_elements & Text) {
305 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
307 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
310 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
314 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
315 cairo_set_source (cr, led_inset_pattern);
319 cairo_set_source_rgb (cr, 0, 0, 0);
320 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
324 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
325 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
329 cairo_scale(cr, 0.7, 0.7);
330 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
331 cairo_set_source (cr, reflection_pattern);
339 /* a partially transparent gray layer to indicate insensitivity */
341 if ((visual_state() & Gtkmm2ext::Insensitive)) {
342 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
343 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
347 /* if requested, show hovering */
349 if (ARDOUR::Config->get_widget_prelight()) {
351 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
352 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
359 ArdourButton::set_diameter (float d)
361 _diameter = (d*2) + 5.0;
363 if (_diameter != 0.0) {
364 _fixed_diameter = true;
371 ArdourButton::set_corner_radius (float r)
378 ArdourButton::on_size_request (Gtk::Requisition* req)
383 CairoWidget::on_size_request (req);
385 if ((_elements & Text) && !_text.empty()) {
386 _layout->get_pixel_size (_text_width, _text_height);
387 if (_text_width + _diameter < 75) {
401 if ((_elements & Indicator) && _fixed_diameter) {
403 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
404 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
406 req->width = _text_width + lrint (_diameter) + xpad;
407 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
411 req->width = _pixbuf->get_width() + xpad;
412 req->height = _pixbuf->get_height() + ypad;
414 req->width = _text_width + xpad;
415 req->height = _text_height + ypad;
421 ArdourButton::set_colors ()
423 uint32_t start_color;
429 /* we use the edge of the button to show Selected state, so the
430 * color/pattern used there will vary depending on that
434 cairo_pattern_destroy (edge_pattern);
438 if (_elements & Edge) {
440 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
441 if (visual_state() & Gtkmm2ext::Selected) {
442 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
443 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
445 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
446 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
448 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
449 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
450 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
451 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
454 if (active_pattern) {
455 cairo_pattern_destroy (active_pattern);
459 if (inactive_pattern) {
460 cairo_pattern_destroy (inactive_pattern);
461 inactive_pattern = 0;
464 if (_elements & Body) {
466 active_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
467 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
468 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
469 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
476 cairo_pattern_add_color_stop_rgba (active_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
477 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
478 cairo_pattern_add_color_stop_rgba (active_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
480 inactive_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
481 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
482 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
483 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
484 cairo_pattern_add_color_stop_rgba (inactive_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
485 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
486 cairo_pattern_add_color_stop_rgba (inactive_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
489 if (led_inset_pattern) {
490 cairo_pattern_destroy (led_inset_pattern);
493 if (reflection_pattern) {
494 cairo_pattern_destroy (reflection_pattern);
497 if (_elements & Indicator) {
498 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
499 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
500 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
502 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
503 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
504 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
507 /* text and LED colors */
509 if (active_state() == Gtkmm2ext::ExplicitActive) {
510 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
511 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
513 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
514 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
517 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
522 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
532 ArdourButton::set_led_left (bool yn)
538 ArdourButton::on_button_press_event (GdkEventButton *ev)
540 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
541 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
542 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
547 if (_tweaks & ShowClick) {
548 set_active_state (Gtkmm2ext::ExplicitActive);
551 if (binding_proxy.button_press_handler (ev)) {
555 if (!_act_on_release) {
557 _action->activate ();
566 ArdourButton::on_button_release_event (GdkEventButton *ev)
568 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
569 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
570 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
571 signal_led_clicked(); /* EMIT SIGNAL */
576 if (_tweaks & ShowClick) {
577 unset_active_state ();
582 if (_act_on_release) {
584 _action->activate ();
594 ArdourButton::set_distinct_led_click (bool yn)
596 _distinct_led_click = yn;
601 ArdourButton::color_handler ()
608 ArdourButton::on_size_allocate (Allocation& alloc)
610 CairoWidget::on_size_allocate (alloc);
616 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
618 watch_connection.disconnect ();
619 binding_proxy.set_controllable (c);
623 ArdourButton::watch ()
625 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
628 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
632 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
636 ArdourButton::controllable_changed ()
638 float val = binding_proxy.get_controllable()->get_value();
640 if (fabs (val) >= 0.5f) {
641 set_active_state (Gtkmm2ext::ExplicitActive);
643 unset_active_state ();
648 ArdourButton::set_related_action (RefPtr<Action> act)
650 Gtkmm2ext::Activatable::set_related_action (act);
654 action_tooltip_changed ();
656 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
659 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
662 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
663 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
664 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
669 ArdourButton::action_toggled ()
671 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
674 if (tact->get_active()) {
675 set_active_state (Gtkmm2ext::ExplicitActive);
677 unset_active_state ();
683 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
689 ArdourButton::setup_led_rect ()
693 if (get_width() < 75) {
699 if (_elements & Indicator) {
700 _led_rect = new cairo_rectangle_t;
702 if (_elements & Text) {
704 _led_rect->x = text_margin;
706 _led_rect->x = get_width() - text_margin - _diameter/2.0;
710 _led_rect->x = get_width()/2.0 - _diameter/2.0;
713 _led_rect->y = get_height()/2.0 - _diameter/2.0;
714 _led_rect->width = _diameter;
715 _led_rect->height = _diameter;
724 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
731 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
733 bool changed = (_active_state != s);
734 CairoWidget::set_active_state (s);
741 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
743 bool changed = (_visual_state != s);
744 CairoWidget::set_visual_state (s);
751 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
755 if (ARDOUR::Config->get_widget_prelight()) {
759 return CairoWidget::on_enter_notify_event (ev);
763 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
767 if (ARDOUR::Config->get_widget_prelight()) {
771 return CairoWidget::on_leave_notify_event (ev);
775 ArdourButton::set_tweaks (Tweaks t)
784 ArdourButton::action_sensitivity_changed ()
786 if (_action->property_sensitive ()) {
787 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
789 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
796 ArdourButton::action_visibility_changed ()
798 if (_action->property_visible ()) {
806 ArdourButton::action_tooltip_changed ()
808 string str = _action->property_tooltip().get_value();
809 ARDOUR_UI::instance()->set_tip (*this, str);
813 ArdourButton::set_rounded_corner_mask (int mask)
820 ArdourButton::set_elements (Element e)