Fix warnings.
[ardour.git] / gtk2_ardour / ardour_button.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21 #include <cmath>
22 #include <algorithm>
23
24 #include <pangomm/layout.h>
25
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 #include "gtkmm2ext/gui_thread.h"
32
33 #include "ardour/rc_configuration.h" // for widget prelight preference
34
35 #include "ardour_button.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
38
39 #include "i18n.h"
40
41 using namespace Gdk;
42 using namespace Gtk;
43 using namespace Glib;
44 using namespace PBD;
45 using std::max;
46 using std::min;
47 using namespace std;
48
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
53 ArdourButton::ArdourButton (Element e)
54         : _elements (e)
55         , _tweaks (Tweaks (0))
56         , _act_on_release (true)
57         , _text_width (0)
58         , _text_height (0)
59         , _diameter (11.0)
60         , _corner_radius (9.0)
61         , edge_pattern (0)
62         , fill_pattern (0)
63         , led_inset_pattern (0)
64         , reflection_pattern (0)
65         , _led_left (false)
66         , _fixed_diameter (true)
67         , _distinct_led_click (false)
68         , _led_rect (0)
69         , _hovering (false)
70 {
71         ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
72 }
73
74 ArdourButton::ArdourButton (const std::string& str, Element e)
75         : _elements (e)
76         , _act_on_release (true)
77         , _text_width (0)
78         , _text_height (0)
79         , _diameter (11.0)
80         , _corner_radius (9.0)
81         , edge_pattern (0)
82         , fill_pattern (0)
83         , led_inset_pattern (0)
84         , reflection_pattern (0)
85         , _led_left (false)
86         , _fixed_diameter (true)
87         , _distinct_led_click (false)
88         , _led_rect (0)
89         , _hovering (false)
90 {
91         set_text (str);
92 }
93
94 ArdourButton::~ArdourButton()
95 {
96         delete _led_rect;
97 }
98
99 void
100 ArdourButton::set_text (const std::string& str)
101 {
102         _text = str;
103
104         if (!_layout && !_text.empty()) {
105                 _layout = Pango::Layout::create (get_pango_context());
106         } 
107
108         if (_layout) {
109                 _layout->set_text (str);
110         }
111
112         queue_resize ();
113 }
114
115 void
116 ArdourButton::set_markup (const std::string& str)
117 {
118         _text = str;
119
120         if (!_layout) {
121                 _layout = Pango::Layout::create (get_pango_context());
122         } 
123
124         _layout->set_text (str);
125         queue_resize ();
126 }
127
128 void
129 ArdourButton::render (cairo_t* cr)
130 {
131         if (!_fixed_diameter) {
132                 _diameter = std::min (_width, _height);
133         }
134
135         /* background fill. use parent window style, so that we fit in nicely.
136          */
137         
138         Color c = get_parent_bg ();
139
140         cairo_rectangle (cr, 0, 0, _width, _height);
141         cairo_stroke_preserve (cr);
142         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
143         cairo_fill (cr);
144
145         if (_elements & Edge) {
146                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
147                 cairo_set_source (cr, edge_pattern);
148                 cairo_fill (cr);
149         }
150
151         if (_elements & Body) {
152                 if (_elements & Edge) {
153                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, _width-2, _height-2, _corner_radius - 1.0);
154                 } else {
155                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius - 1.0);
156                 }
157                 cairo_set_source (cr, fill_pattern);
158                 cairo_fill (cr);
159         }
160
161         if (_pixbuf) {
162
163                 double x,y;
164                 x = (_width - _pixbuf->get_width())/2.0;
165                 y = (_height - _pixbuf->get_height())/2.0;
166
167                 cairo_rectangle (cr, x, y, _pixbuf->get_width(), _pixbuf->get_height());
168                 gdk_cairo_set_source_pixbuf (cr, _pixbuf->gobj(), x, y);
169                 cairo_fill (cr);
170         }
171
172         /* text, if any */
173
174         int text_margin;
175
176         if (_width < 75) {
177                 text_margin = 3;
178         } else {
179                 text_margin = 10;
180         }
181
182         if ((_elements & Text) && !_text.empty()) {
183
184                 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
185
186                 if (_elements & Indicator) {
187                         if (_led_left) {
188                                 cairo_move_to (cr, text_margin + _diameter + 4, _height/2.0 - _text_height/2.0);
189                         } else {
190                                 cairo_move_to (cr, text_margin, _height/2.0 - _text_height/2.0);
191                         }
192                 } else {
193                         /* center text */
194                         cairo_move_to (cr, (_width - _text_width)/2.0, _height/2.0 - _text_height/2.0);
195                 }
196
197                 pango_cairo_show_layout (cr, _layout->gobj());
198         } 
199
200         if (_elements & Indicator) {
201
202                 /* move to the center of the indicator/led */
203
204                 cairo_save (cr);
205
206                 if (_elements & Text) {
207                         if (_led_left) {
208                                 cairo_translate (cr, text_margin + (_diameter/2.0), _height/2.0);
209                         } else {
210                                 cairo_translate (cr, _width - ((_diameter/2.0) + 4.0), _height/2.0);
211                         }
212                 } else {
213                         cairo_translate (cr, _width/2.0, _height/2.0);
214                 }
215                 
216                 //inset
217                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
218                 cairo_set_source (cr, led_inset_pattern);
219                 cairo_fill (cr);
220                 
221                 //black ring
222                 cairo_set_source_rgb (cr, 0, 0, 0);
223                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
224                 cairo_fill(cr);
225                 
226                 //led color
227                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
228                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
229                 cairo_fill(cr);
230                 
231                 //reflection
232                 cairo_scale(cr, 0.7, 0.7);
233                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
234                 cairo_set_source (cr, reflection_pattern);
235                 cairo_fill (cr);
236
237                 cairo_restore (cr);
238
239         }
240
241
242         /* a partially transparent gray layer to indicate insensitivity */
243
244         if ((visual_state() & Gtkmm2ext::Insensitive)) {
245                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
246                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
247                 cairo_fill (cr);
248         }
249
250         /* if requested, show hovering */
251         
252         if (ARDOUR::Config->get_widget_prelight()) {
253                 if (_hovering) {
254                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
255                         cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.2);
256                         cairo_fill (cr);
257                 }
258         }
259 }
260
261 void
262 ArdourButton::set_diameter (float d)
263 {
264         _diameter = (d*2) + 5.0;
265
266         if (_diameter != 0.0) {
267                 _fixed_diameter = true;
268         }
269
270         set_colors ();
271 }
272
273 void
274 ArdourButton::set_corner_radius (float r)
275 {
276         _corner_radius = r;
277         set_dirty ();
278 }
279
280 void
281 ArdourButton::on_size_request (Gtk::Requisition* req)
282 {
283         int xpad = 0;
284         int ypad = 6;
285
286         CairoWidget::on_size_request (req);
287
288         if ((_elements & Text) && !_text.empty()) {
289                 _layout->get_pixel_size (_text_width, _text_height);
290                 if (_text_width + _diameter < 75) {
291                         xpad = 7;
292                 } else {
293                         xpad = 20;
294                 }
295         } else {
296                 _text_width = 0;
297                 _text_height = 0;
298         }
299
300         if (_pixbuf) {
301                 xpad = 6;
302         }
303
304         if ((_elements & Indicator) && _fixed_diameter) {
305                 if (_pixbuf) {
306                         req->width = _pixbuf->get_width() + lrint (_diameter) + xpad;
307                         req->height = max (_pixbuf->get_height(), (int) lrint (_diameter)) + ypad;
308                 } else {
309                         req->width = _text_width + lrint (_diameter) + xpad;
310                         req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
311                 }
312         } else {
313                 if (_pixbuf) {
314                         req->width = _pixbuf->get_width() + xpad;
315                         req->height = _pixbuf->get_height() + ypad;
316                 }  else {
317                         req->width = _text_width + xpad;
318                         req->height = _text_height + ypad;
319                 }
320         }
321 }
322
323 void
324 ArdourButton::set_colors ()
325 {
326         uint32_t start_color;
327         uint32_t end_color;
328         uint32_t r, g, b, a;
329         uint32_t text_color;
330         uint32_t led_color;
331
332         /* we use the edge of the button to show Selected state, so the
333          * color/pattern used there will vary depending on that
334          */
335         
336         if (edge_pattern) {
337                 cairo_pattern_destroy (edge_pattern);
338         }
339
340         if (_elements & Edge) {
341
342                 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
343                 if (visual_state() & Gtkmm2ext::Selected) {
344                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start selected", get_name()));
345                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end selected", get_name()));
346                 } else {
347                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border start", get_name()));
348                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: border end", get_name()));
349                 }
350                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
351                 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
352                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
353                 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
354         }
355
356
357         /* the fill pattern is used to indicate Normal/Active/Mid state
358          */
359
360         if (fill_pattern) {
361                 cairo_pattern_destroy (fill_pattern);
362         }
363
364         if (_elements & Body) {
365                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
366                 
367                 if (active_state() == Gtkmm2ext::Mid) {
368                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start mid", get_name()));
369                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end mid", get_name()));
370                 } else if (active_state() == Gtkmm2ext::Active) {
371                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start active", get_name()));
372                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end active", get_name()));
373                 } else {
374                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill start", get_name()));
375                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: fill end", get_name()));
376                 }
377                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
378                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
379                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
380                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
381         }
382
383         if (led_inset_pattern) {
384                 cairo_pattern_destroy (led_inset_pattern);
385         }
386         
387         if (reflection_pattern) {
388                 cairo_pattern_destroy (reflection_pattern);
389         }
390
391         if (_elements & Indicator) {
392                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
393                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
394                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
395
396                 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
397                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
398                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
399         }
400         
401         /* text and LED colors depend on Active/Normal/Mid */
402
403         if (active_state() == Gtkmm2ext::Active) {
404                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text active", get_name()));
405                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led active", get_name()));
406         } else if (active_state() == Gtkmm2ext::Mid) {
407                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text mid", get_name()));
408                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led mid", get_name()));
409         } else {
410                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
411                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: led", get_name()));
412         }
413
414         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
415         text_r = r/255.0;
416         text_g = g/255.0;
417         text_b = b/255.0;
418         text_a = a/255.0;
419         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
420         led_r = r/255.0;
421         led_g = g/255.0;
422         led_b = b/255.0;
423         led_a = a/255.0;
424
425         set_dirty ();
426 }
427
428 void
429 ArdourButton::set_led_left (bool yn)
430 {
431         _led_left = yn;
432 }
433
434 bool
435 ArdourButton::on_button_press_event (GdkEventButton *ev)
436 {
437         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
438                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
439                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
440                         return true;
441                 }
442         }
443
444         if (_tweaks & ShowClick) {
445                 set_active_state (Gtkmm2ext::Active);
446         }
447
448         if (binding_proxy.button_press_handler (ev)) {
449                 return true;
450         }
451
452         if (!_act_on_release) {
453                 if (_action) {
454                         _action->activate ();
455                         return true;
456                 }
457         }
458
459         return false;
460 }
461
462 bool
463 ArdourButton::on_button_release_event (GdkEventButton *ev)
464 {
465         if ((_elements & Indicator) && _led_rect && _distinct_led_click) {
466                 if (ev->x >= _led_rect->x && ev->x < _led_rect->x + _led_rect->width && 
467                     ev->y >= _led_rect->y && ev->y < _led_rect->y + _led_rect->height) {
468                         signal_led_clicked(); /* EMIT SIGNAL */
469                         return true;
470                 }
471         }
472
473         if (_tweaks & ShowClick) {
474                 unset_active_state ();
475         }
476
477         signal_clicked ();
478
479         if (_act_on_release) {
480                 if (_action) {
481                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
482                         _action->activate ();
483                         return true;
484                 }
485         }
486
487
488         return false;
489 }
490
491 void
492 ArdourButton::set_distinct_led_click (bool yn)
493 {
494         _distinct_led_click = yn;
495         setup_led_rect ();
496 }
497
498 void
499 ArdourButton::color_handler ()
500 {
501         set_colors ();
502         set_dirty ();
503 }
504
505 void
506 ArdourButton::on_size_allocate (Allocation& alloc)
507 {
508         CairoWidget::on_size_allocate (alloc);
509         setup_led_rect ();
510         set_colors ();
511 }
512
513 void
514 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
515 {
516         watch_connection.disconnect ();
517         binding_proxy.set_controllable (c);
518 }
519
520 void
521 ArdourButton::watch ()
522 {
523         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
524
525         if (!c) {
526                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
527                 return;
528         }
529
530         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
531 }
532
533 void
534 ArdourButton::controllable_changed ()
535 {
536         float val = binding_proxy.get_controllable()->get_value();
537
538         if (fabs (val) >= 0.5f) {
539                 set_active_state (Gtkmm2ext::Active);
540         } else {
541                 unset_active_state ();
542         }
543 }
544
545 void
546 ArdourButton::set_related_action (RefPtr<Action> act)
547 {
548         _action = act;
549
550         if (_action) {
551
552                 string str = _action->property_tooltip().get_value();
553                 if (!str.empty()) {
554                         ARDOUR_UI::instance()->set_tip (*this, str);
555                 }
556
557                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
558                 if (tact) {
559                         tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
560                 } 
561
562                 _action->connect_property_changed ("sensitive", sigc::mem_fun (*this, &ArdourButton::action_sensitivity_changed));
563                 _action->connect_property_changed ("visible", sigc::mem_fun (*this, &ArdourButton::action_visibility_changed));
564                 _action->connect_property_changed ("tooltip", sigc::mem_fun (*this, &ArdourButton::action_tooltip_changed));
565         }
566 }
567
568 void
569 ArdourButton::action_toggled ()
570 {
571         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
572
573         if (tact) {
574                 if (tact->get_active()) {
575                         set_active_state (Gtkmm2ext::Active);
576                 } else {
577                         unset_active_state ();
578                 }
579         }
580 }       
581
582 void
583 ArdourButton::on_style_changed (const RefPtr<Gtk::Style>&)
584 {
585         set_colors ();
586 }
587
588 void
589 ArdourButton::setup_led_rect ()
590 {
591         int text_margin;
592
593         if (_width < 75) {
594                 text_margin = 3;
595         } else {
596                 text_margin = 10;
597         }
598
599         if (_elements & Indicator) {
600                 _led_rect = new cairo_rectangle_t;
601                 
602                 if (_elements & Text) {
603                         if (_led_left) {
604                                 _led_rect->x = text_margin;
605                         } else {
606                                 _led_rect->x = _width - text_margin - _diameter/2.0;
607                         }
608                 } else {
609                         /* centered */
610                         _led_rect->x = _width/2.0 - _diameter/2.0;
611                 }
612
613                 _led_rect->y = _height/2.0 - _diameter/2.0;
614                 _led_rect->width = _diameter;
615                 _led_rect->height = _diameter;
616
617         } else {
618                 delete _led_rect;
619                 _led_rect = 0;
620         }
621 }
622
623 void
624 ArdourButton::set_image (const RefPtr<Gdk::Pixbuf>& img)
625 {
626         _pixbuf = img;
627         queue_draw ();
628 }
629
630 void
631 ArdourButton::set_active (bool yn)
632 {
633         /* this is an API simplification for buttons
634            that only use the Active and Normal states.
635         */
636
637         if (yn) {
638                 set_active_state (Gtkmm2ext::Active);
639         } else {
640                 unset_active_state ();
641         }
642 }
643
644 void
645 ArdourButton::set_active_state (Gtkmm2ext::ActiveState s)
646 {
647         bool changed = (_active_state != s);
648         CairoWidget::set_active_state (s);
649         if (changed) {
650                 set_colors ();
651         }
652 }
653         
654 void
655 ArdourButton::set_visual_state (Gtkmm2ext::VisualState s)
656 {
657         bool changed = (_visual_state != s);
658         CairoWidget::set_visual_state (s);
659         if (changed) {
660                 set_colors ();
661         }
662 }
663         
664 bool
665 ArdourButton::on_enter_notify_event (GdkEventCrossing* ev)
666 {
667         _hovering = true;
668
669         if (ARDOUR::Config->get_widget_prelight()) {
670                 queue_draw ();
671         }
672
673         return CairoWidget::on_enter_notify_event (ev);
674 }
675
676 bool
677 ArdourButton::on_leave_notify_event (GdkEventCrossing* ev)
678 {
679         _hovering = false;
680
681         if (ARDOUR::Config->get_widget_prelight()) {
682                 queue_draw ();
683         }
684
685         return CairoWidget::on_leave_notify_event (ev);
686 }
687
688 void
689 ArdourButton::set_tweaks (Tweaks t)
690 {
691         if (_tweaks != t) {
692                 _tweaks = t;
693                 queue_draw ();
694         }
695 }
696
697 void
698 ArdourButton::action_sensitivity_changed ()
699 {
700         if (_action->property_sensitive ()) {
701                 set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
702         } else {
703                 set_visual_state (Gtkmm2ext::VisualState (visual_state() | Gtkmm2ext::Insensitive));
704         }
705         
706 }
707
708
709 void
710 ArdourButton::action_visibility_changed ()
711 {
712         if (_action->property_visible ()) {
713                 show ();
714         } else {
715                 hide ();
716         }
717 }
718
719 void
720 ArdourButton::action_tooltip_changed ()
721 {
722         string str = _action->property_tooltip().get_value();
723         ARDOUR_UI::instance()->set_tip (*this, str);
724 }
725