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"
41 #define REFLECTION_HEIGHT 2
51 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
52 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
53 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
54 bool ArdourButton::_flat_buttons = false;
56 ArdourButton::ArdourButton (Element e)
58 , _tweaks (Tweaks (0))
62 , _corner_radius (4.0)
68 , fill_color_active (0)
69 , fill_color_inactive (0)
71 , fill_pattern_active (0)
73 , led_inset_pattern (0)
74 , reflection_pattern (0)
76 , _act_on_release (true)
78 , _fixed_diameter (true)
79 , _distinct_led_click (false)
82 ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
85 ArdourButton::ArdourButton (const std::string& str, Element e)
87 , _tweaks (Tweaks (0))
91 , _corner_radius (4.0)
97 , fill_color_active (0)
98 , fill_color_inactive (0)
100 , fill_pattern_active (0)
102 , led_inset_pattern (0)
103 , reflection_pattern (0)
105 , _act_on_release (true)
107 , _fixed_diameter (true)
108 , _distinct_led_click (false)
114 ArdourButton::~ArdourButton()
119 cairo_pattern_destroy (shine_pattern);
123 cairo_pattern_destroy (fill_pattern);
126 if (fill_pattern_active) {
127 cairo_pattern_destroy (fill_pattern_active);
130 if (led_inset_pattern) {
131 cairo_pattern_destroy (led_inset_pattern);
134 if (reflection_pattern) {
135 cairo_pattern_destroy (reflection_pattern);
141 ArdourButton::set_text (const std::string& str)
145 if (!_layout && !_text.empty()) {
146 _layout = Pango::Layout::create (get_pango_context());
150 _layout->set_text (str);
157 ArdourButton::set_markup (const std::string& str)
162 _layout = Pango::Layout::create (get_pango_context());
165 _layout->set_markup (str);
170 ArdourButton::set_angle (const double angle)
176 ArdourButton::set_alignment (const float xa, const float ya)
183 ArdourButton::render (cairo_t* cr)
185 void (*rounded_function)(cairo_t*, double, double, double, double, double);
187 switch (_corner_mask) {
188 case 0x1: /* upper left only */
189 rounded_function = Gtkmm2ext::rounded_top_left_rectangle;
191 case 0x2: /* upper right only */
192 rounded_function = Gtkmm2ext::rounded_top_right_rectangle;
194 case 0x3: /* upper only */
195 rounded_function = Gtkmm2ext::rounded_top_rectangle;
197 /* should really have functions for lower right, lower left,
198 lower only, but for now, we don't
201 rounded_function = Gtkmm2ext::rounded_rectangle;
204 if (!_fixed_diameter) {
205 _diameter = std::min (get_width(), get_height());
210 if ((_elements & Body)==Body) {
211 if (_elements & Edge) {
213 cairo_set_source_rgba (cr, 0, 0, 0, 1);
214 rounded_function(cr, 0, 0, get_width(), get_height(), _corner_radius);
217 rounded_function (cr, 1, 1, get_width()-2, get_height()-2, _corner_radius - 1.5);
219 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
222 if (active_state() == Gtkmm2ext::ImplicitActive) {
224 if (!(_tweaks & ImplicitUsesSolidColor)) {
225 cairo_set_source (cr, fill_pattern);
227 cairo_set_source (cr, fill_pattern_active);
231 if (!(_tweaks & ImplicitUsesSolidColor)) {
233 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
234 cairo_set_line_width (cr, 1.0);
235 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 1.5);
236 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
240 } else if (active_state() == Gtkmm2ext::ExplicitActive || ((_elements & Indicator)==Indicator) ) {
243 cairo_set_source (cr, fill_pattern_active);
249 cairo_set_source (cr, fill_pattern);
255 if ( ((_elements & FlatFace)==FlatFace) && (active_state() != Gtkmm2ext::ExplicitActive) ) {
257 if ( !_flat_buttons ) {
258 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
259 Gtkmm2ext::rounded_rectangle (cr, 2, 3, get_width()-4, rheight, _corner_radius-1);
260 cairo_set_source (cr, shine_pattern);
264 if (active_state() == Gtkmm2ext::ExplicitActive) {
266 UINT_TO_RGBA (fill_color_active, &r, &g, &b, &a);
267 cairo_set_line_width (cr, 2.0);
268 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
269 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
274 UINT_TO_RGBA (fill_color_inactive, &r, &g, &b, &a);
275 cairo_set_line_width (cr, 2.0);
276 rounded_function (cr, 2, 2, get_width()-4, get_height()-4, _corner_radius - 2.0);
277 cairo_set_source_rgba (cr, r/255.0, g/255.0, b/255.0, a/255.0);
286 x = (get_width() - _pixbuf->get_width())/2.0;
287 y = (get_height() - _pixbuf->get_height())/2.0;
289 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
290 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
298 if (get_width() < 75) {
304 if ( ((_elements & Text)==Text) && !_text.empty()) {
307 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
309 if (_elements & Indicator) {
311 cairo_move_to (cr, text_margin + _diameter + 4, get_height()/2.0 - _text_height/2.0);
313 cairo_move_to (cr, text_margin, get_height()/2.0 - _text_height/2.0);
315 pango_cairo_show_layout (cr, _layout->gobj());
323 cairo_save (cr); // TODO retain rotataion.. adj. LED,...
324 cairo_rotate(cr, _angle * M_PI / 180.0);
325 cairo_device_to_user(cr, &ww, &wh);
326 xa = (ww - _text_width) * _xalign;
327 ya = (wh - _text_height) * _yalign;
329 /* quick hack for left/bottom alignment at -90deg
330 * TODO this should be generalized incl rotation.
331 * currently only 'user' of this API is meter_strip.cc
333 if (_xalign < 0) xa = (ww * fabs(_xalign) + text_margin);
335 // TODO honor left/right text_margin with min/max()
337 cairo_move_to (cr, xa, ya);
338 pango_cairo_update_layout(cr, _layout->gobj());
339 pango_cairo_show_layout (cr, _layout->gobj());
342 /* use old center'ed layout for follow up items - until rotation/aligment code is completed */
343 cairo_move_to (cr, (get_width() - _text_width)/2.0, get_height()/2.0 - _text_height/2.0);
348 if (((_elements & Indicator)==Indicator)) {
350 /* move to the center of the indicator/led */
354 if (_elements & Text) {
356 cairo_translate (cr, text_margin + (_diameter/2.0), get_height()/2.0);
358 cairo_translate (cr, get_width() - ((_diameter/2.0) + 4.0), get_height()/2.0);
361 cairo_translate (cr, get_width()/2.0, get_height()/2.0);
365 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
366 cairo_set_source (cr, led_inset_pattern);
370 cairo_set_source_rgb (cr, 0, 0, 0);
371 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
375 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
376 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
383 /* a partially transparent gray layer to indicate insensitivity */
385 if ((visual_state() & Gtkmm2ext::Insensitive)) {
386 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
387 cairo_set_source_rgba (cr, 0.505, 0.517, 0.525, 0.6);
392 bool show_reflection = (active_state() == Gtkmm2ext::ExplicitActive);
393 show_reflection &= !_flat_buttons;
394 show_reflection &= !((_elements & Indicator)==Indicator);
395 if ( show_reflection ) {
396 float rheight = get_height()*0.5-REFLECTION_HEIGHT;
397 Gtkmm2ext::rounded_rectangle (cr, 2, get_height()*0.5-1, get_width()-4, rheight, _corner_radius-1);
398 cairo_set_source (cr, shine_pattern);
402 /* if requested, show hovering */
404 if (ARDOUR::Config->get_widget_prelight()
405 && !((visual_state() & Gtkmm2ext::Insensitive))) {
407 rounded_function (cr, 0, 0, get_width(), get_height(), _corner_radius);
408 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
415 ArdourButton::set_diameter (float d)
417 _diameter = (d*2) + 5.0;
419 if (_diameter != 0.0) {
420 _fixed_diameter = true;
428 ArdourButton::set_corner_radius (float r)
435 ArdourButton::on_size_request (Gtk::Requisition* req)
440 CairoWidget::on_size_request (req);
442 if ((_elements & Text) && !_text.empty()) {
443 _layout->get_pixel_size (_text_width, _text_height);
444 if (_text_width + _diameter < 75) {
458 if ((_elements & Indicator) && _fixed_diameter) {
460 req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
461 req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
463 req->width = _text_width + lrint (_diameter) + xpad * 2; // margin left+right * 2
464 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
468 req->width = _pixbuf->get_width() + xpad;
469 req->height = _pixbuf->get_height() + ypad;
471 req->width = _text_width + xpad;
472 req->height = _text_height + ypad;
478 ArdourButton::set_colors ()
480 uint32_t start_color;
486 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
487 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
489 fill_color_active = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
491 fill_color_inactive = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
492 border_color = ARDOUR_UI::config()->color_by_name ( "button border" );
495 cairo_pattern_destroy (shine_pattern);
500 cairo_pattern_destroy (fill_pattern);
504 if (fill_pattern_active) {
505 cairo_pattern_destroy (fill_pattern_active);
506 fill_pattern_active = 0;
509 if (_elements & Body) {
511 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
514 end_color = start_color;
516 end_color = fill_color_active;
518 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
525 shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height());
526 cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
527 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.5, 1,1,1,0.1);
528 cairo_pattern_add_color_stop_rgba (shine_pattern, 0.7, 1,1,1,0.2);
529 cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
531 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
533 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
535 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
536 end_color = fill_color_inactive;
538 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
539 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
540 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
541 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
543 fill_pattern_active = cairo_pattern_create_linear (0.0, 0.0, 0.0, get_height()-3);
545 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
546 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
548 end_color = start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
551 if (active_state() == Gtkmm2ext::ImplicitActive && (_tweaks & ImplicitUsesSolidColor)) {
552 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
553 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
555 start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
556 end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
559 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
560 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 0, r/255.0,g/255.0,b/255.0, a/255.0);
561 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
562 cairo_pattern_add_color_stop_rgba (fill_pattern_active, 1, r/255.0,g/255.0,b/255.0, a/255.0);
565 if (led_inset_pattern) {
566 cairo_pattern_destroy (led_inset_pattern);
569 if (reflection_pattern) {
570 cairo_pattern_destroy (reflection_pattern);
573 if (_elements & Indicator) {
574 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
575 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
576 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
578 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
579 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
580 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
583 /* text and LED colors */
585 if (active_state() == Gtkmm2ext::ExplicitActive || ((_tweaks & ImplicitUsesSolidColor) && active_state() == Gtkmm2ext::ImplicitActive)) {
586 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
587 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
589 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
590 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
593 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
598 UINT_TO_RGBA (led_color, &r, &g, &b, &a);
608 ArdourButton::set_led_left (bool yn)
614 ArdourButton::on_button_press_event (GdkEventButton *ev)
616 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
617 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
618 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
623 if (_tweaks & ShowClick) {
624 set_active_state (Gtkmm2ext::ExplicitActive);
627 if (binding_proxy.button_press_handler (ev)) {
631 if (!_act_on_release) {
633 _action->activate ();
642 ArdourButton::on_button_release_event (GdkEventButton *ev)
644 if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
645 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width &&
646 ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
647 signal_led_clicked(); /* EMIT SIGNAL */
652 if (_tweaks & ShowClick) {
653 unset_active_state ();
658 if (_act_on_release) {
660 _action->activate ();
670 ArdourButton::set_distinct_led_click (bool yn)
672 _distinct_led_click = yn;
677 ArdourButton::color_handler ()
684 ArdourButton::on_size_allocate (Allocation& alloc)
686 CairoWidget::on_size_allocate (alloc);
692 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
694 watch_connection.disconnect ();
695 binding_proxy.set_controllable (c);
699 ArdourButton::watch ()
701 boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
704 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
708 c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
712 ArdourButton::controllable_changed ()
714 float val = binding_proxy.get_controllable()->get_value();
716 if (fabs (val) >= 0.5f) {
717 set_active_state (Gtkmm2ext::ExplicitActive);
719 unset_active_state ();
724 ArdourButton::set_related_action (RefPtr<Action> act)
726 Gtkmm2ext::Activatable::set_related_action (act);
730 action_tooltip_changed ();
732 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
735 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
738 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
739 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
740 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
745 ArdourButton::action_toggled ()
747 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
750 if (tact->get_active()) {
751 set_active_state (Gtkmm2ext::ExplicitActive);
753 unset_active_state ();
759 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
765 ArdourButton::setup_led_rect ()
769 if (get_width() < 75) {
775 if (_elements & Indicator) {
776 _led_rect = new cairo_rectangle_t;
778 if (_elements & Text) {
780 _led_rect->x = text_margin;
782 _led_rect->x = get_width() - text_margin - _diameter/2.0;
786 _led_rect->x = get_width()/2.0 - _diameter/2.0;
789 _led_rect->y = get_height()/2.0 - _diameter/2.0;
790 _led_rect->width = _diameter;
791 _led_rect->height = _diameter;
800 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
807 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
809 bool changed = (_active_state != s);
810 CairoWidget::set_active_state (s);
817 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
819 bool changed = (_visual_state != s);
820 CairoWidget::set_visual_state (s);
827 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
831 if (ARDOUR::Config->get_widget_prelight()) {
835 return CairoWidget::on_enter_notify_event (ev);
839 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
843 if (ARDOUR::Config->get_widget_prelight()) {
847 return CairoWidget::on_leave_notify_event (ev);
851 ArdourButton::set_tweaks (Tweaks t)
860 ArdourButton::action_sensitivity_changed ()
862 if (_action->property_sensitive ()) {
863 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
865 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
872 ArdourButton::action_visibility_changed ()
874 if (_action->property_visible ()) {
882 ArdourButton::action_tooltip_changed ()
884 string str = _action->property_tooltip().get_value();
885 ARDOUR_UI::instance()->set_tip (*this, str);
889 ArdourButton::set_rounded_corner_mask (int mask)
896 ArdourButton::set_elements (Element e)
903 ArdourButton::add_elements (Element e)
905 _elements = (ArdourButton::Element) (_elements | e);
910 ArdourButton::set_flat_buttons (bool yn)