f925ecf6bc86e5a9ce999fef226b71ad2d7e54d9
[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_button.h"
34 #include "ardour_ui.h"
35 #include "global_signals.h"
36
37 #include "i18n.h"
38
39 using namespace Gdk;
40 using namespace Gtk;
41 using namespace Glib;
42 using namespace PBD;
43 using std::max;
44 using std::min;
45 using namespace std;
46
47 ArdourButton::Element ArdourButton::default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text);
48 ArdourButton::Element ArdourButton::led_default_elements = ArdourButton::Element (ArdourButton::default_elements|ArdourButton::Indicator);
49 ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::Element (ArdourButton::Edge|ArdourButton::Body|ArdourButton::Indicator);
50
51 ArdourButton::ArdourButton (Element e)
52         : _elements (e)
53         , _act_on_release (true)
54         , _text_width (0)
55         , _text_height (0)
56         , _diameter (11.0)
57         , _corner_radius (9.0)
58         , edge_pattern (0)
59         , fill_pattern (0)
60         , led_inset_pattern (0)
61         , reflection_pattern (0)
62         , _led_left (false)
63         , _fixed_diameter (true)
64         , _distinct_led_click (false)
65 {
66         ColorsChanged.connect (sigc::mem_fun (*this, &ArdourButton::color_handler));
67         StateChanged.connect (sigc::mem_fun (*this, &ArdourButton::state_handler));
68 }
69
70 ArdourButton::~ArdourButton()
71 {
72 }
73
74 void
75 ArdourButton::set_text (const std::string& str)
76 {
77         _text = str;
78
79         if (!_layout && !_text.empty()) {
80                 _layout = Pango::Layout::create (get_pango_context());
81         } 
82         
83         _layout->set_text (str);
84
85         queue_resize ();
86 }
87
88 void
89 ArdourButton::set_markup (const std::string& str)
90 {
91         _text = str;
92
93         if (!_layout) {
94                 _layout = Pango::Layout::create (get_pango_context());
95         } 
96
97         _layout->set_text (str);
98         queue_resize ();
99 }
100
101 void
102 ArdourButton::render (cairo_t* cr)
103 {
104         if (!_fixed_diameter) {
105                 _diameter = std::min (_width, _height);
106         }
107
108         /* background fill. use parent window style, so that we fit in nicely.
109          */
110         
111         Color c = get_parent_bg ();
112
113         cairo_rectangle (cr, 0, 0, _width, _height);
114         cairo_stroke_preserve (cr);
115         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
116         cairo_fill (cr);
117
118         if (_elements & Edge) {
119                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius);
120                 cairo_set_source (cr, edge_pattern);
121                 cairo_fill (cr);
122         }
123
124         if (_elements & Body) {
125                 if (_elements & Edge) {
126                         Gtkmm2ext::rounded_rectangle (cr, 1, 1, _width-2, _height-2, _corner_radius - 1.0);
127                 } else {
128                         Gtkmm2ext::rounded_rectangle (cr, 0, 0, _width, _height, _corner_radius - 1.0);
129                 }
130                 cairo_set_source (cr, fill_pattern);
131                 cairo_fill (cr);
132         }
133
134         /* text, if any */
135
136         if ((_elements & Text) && !_text.empty()) {
137                 cairo_set_source_rgba (cr, text_r, text_g, text_b, text_a);
138
139                 if (_elements & Indicator) {
140                         if (_led_left) {
141                                 cairo_move_to (cr, _diameter + 3 + 4, _height/2.0 - _text_height/2.0);
142                         } else {
143                                 cairo_move_to (cr, 3, _height/2.0 - _text_height/2.0);
144                         }
145                 } else {
146                         /* center text */
147                         cairo_move_to (cr, (_width - _text_width)/2.0, _height/2.0 - _text_height/2.0);
148                 }
149
150                 pango_cairo_show_layout (cr, _layout->gobj());
151         }
152
153         if (_elements & Indicator) {
154
155                 /* move to the center of the indicator/led */
156
157                 cairo_save (cr);
158
159                 if (_elements & Text) {
160                         if (_led_left) {
161                                 cairo_translate (cr, 3 + (_diameter/2.0), _height/2.0);
162                         } else {
163                                 cairo_translate (cr, _width - ((_diameter/2.0) + 4.0), _height/2.0);
164                         }
165                 } else {
166                         cairo_translate (cr, _width/2.0, _height/2.0);
167                 }
168                 
169                 //inset
170                 cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
171                 cairo_set_source (cr, led_inset_pattern);
172                 cairo_fill (cr);
173                 
174                 //black ring
175                 cairo_set_source_rgb (cr, 0, 0, 0);
176                 cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
177                 cairo_fill(cr);
178                 
179                 //led color
180                 cairo_set_source_rgba (cr, led_r, led_g, led_b, led_a);
181                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
182                 cairo_fill(cr);
183                 
184                 //reflection
185                 cairo_scale(cr, 0.7, 0.7);
186                 cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
187                 cairo_set_source (cr, reflection_pattern);
188                 cairo_fill (cr);
189
190                 cairo_restore (cr);
191
192         }
193
194         /* a partially transparent gray layer to indicate insensitivity */
195
196         if ((visual_state() & Insensitive)) {
197                 cairo_rectangle (cr, 0, 0, _width, _height);
198                 cairo_set_source_rgba (cr, 0.905, 0.917, 0.925, 0.5);
199                 cairo_fill (cr);
200         }
201 }
202
203 void
204 ArdourButton::state_handler ()
205 {
206         set_colors ();
207 }
208
209 void
210 ArdourButton::set_diameter (float d)
211 {
212         _diameter = (d*2) + 5.0;
213
214         if (_diameter != 0.0) {
215                 _fixed_diameter = true;
216         }
217
218         set_colors ();
219 }
220
221 void
222 ArdourButton::set_corner_radius (float r)
223 {
224         _corner_radius = r;
225         set_dirty ();
226 }
227
228 void
229 ArdourButton::on_size_request (Gtk::Requisition* req)
230 {
231         int xpad = 0;
232         int ypad = 6;
233
234         CairoWidget::on_size_request (req);
235
236         if ((_elements & Text) && !_text.empty()) {
237                 _layout->get_pixel_size (_text_width, _text_height);
238                 xpad += 6;
239         } else {
240                 _text_width = 0;
241                 _text_height = 0;
242         }
243
244         if ((_elements & Indicator) && _fixed_diameter) {
245                 req->width = _text_width + lrint (_diameter) + xpad;
246                 req->height = max (_text_height, (int) lrint (_diameter)) + ypad;
247         } else {
248                 req->width = _text_width + xpad;
249                 req->height = _text_height + ypad;
250         }
251 }
252
253 void
254 ArdourButton::set_colors ()
255 {
256         uint32_t start_color;
257         uint32_t end_color;
258         uint32_t r, g, b, a;
259         uint32_t text_color;
260         uint32_t led_color;
261
262         /* we use the edge of the button to show Selected state, so the
263          * color/pattern used there will vary depending on that
264          */
265         
266         if (edge_pattern) {
267                 cairo_pattern_destroy (edge_pattern);
268         }
269
270         if (_elements & Edge) {
271
272                 edge_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
273                 if (visual_state() & CairoWidget::Selected) {
274                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border start selected", get_name()));
275                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border end selected", get_name()));
276                 } else {
277                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border start", get_name()));
278                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 border end", get_name()));
279                 }
280                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
281                 cairo_pattern_add_color_stop_rgba (edge_pattern, 0, r/255.0,g/255.0,b/255.0, 0.7);
282                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
283                 cairo_pattern_add_color_stop_rgba (edge_pattern, 1, r/255.0,g/255.0,b/255.0, 0.7);
284         }
285
286
287         /* the fill pattern is used to indicate Normal/Active/Mid state
288          */
289
290         if (fill_pattern) {
291                 cairo_pattern_destroy (fill_pattern);
292         }
293
294         if (_elements & Body) {
295                 fill_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _height);
296                 
297                 if (active_state() == Mid) {
298                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start mid", get_name()));
299                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end mid", get_name()));
300                 } else if (active_state() == Active) {
301                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start active", get_name()));
302                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end active", get_name()));
303                 } else {
304                         start_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill start", get_name()));
305                         end_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 fill end", get_name()));
306                 }
307                 UINT_TO_RGBA (start_color, &r, &g, &b, &a);
308                 cairo_pattern_add_color_stop_rgba (fill_pattern, 0, r/255.0,g/255.0,b/255.0, a/255.0);
309                 UINT_TO_RGBA (end_color, &r, &g, &b, &a);
310                 cairo_pattern_add_color_stop_rgba (fill_pattern, 1, r/255.0,g/255.0,b/255.0, a/255.0);
311         }
312
313         if (led_inset_pattern) {
314                 cairo_pattern_destroy (led_inset_pattern);
315         }
316         
317         if (reflection_pattern) {
318                 cairo_pattern_destroy (reflection_pattern);
319         }
320
321         if (_elements & Indicator) {
322                 led_inset_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
323                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 0, 0,0,0, 0.4);
324                 cairo_pattern_add_color_stop_rgba (led_inset_pattern, 1, 1,1,1, 0.7);
325
326                 reflection_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
327                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 0, 1,1,1, active_state() ? 0.4 : 0.2);
328                 cairo_pattern_add_color_stop_rgba (reflection_pattern, 1, 1,1,1, 0.0);
329         }
330         
331         /* text and LED colors depend on Active/Normal/Mid */
332
333         if (active_state() == Active) {
334                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text active", get_name()));
335                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led active", get_name()));
336         } else if (active_state() == Mid) {
337                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text mid", get_name()));
338                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led active", get_name()));
339         } else {
340                 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 text", get_name()));
341                 led_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 led", get_name()));
342         }
343
344         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
345         text_r = r/255.0;
346         text_g = g/255.0;
347         text_b = b/255.0;
348         text_a = a/255.0;
349         UINT_TO_RGBA (led_color, &r, &g, &b, &a);
350         led_r = r/255.0;
351         led_g = g/255.0;
352         led_b = b/255.0;
353         led_a = a/255.0;
354
355         set_dirty ();
356 }
357
358 void
359 ArdourButton::set_led_left (bool yn)
360 {
361         _led_left = yn;
362 }
363
364 bool
365 ArdourButton::on_button_press_event (GdkEventButton *ev)
366 {
367         if ((_elements & Indicator) && _distinct_led_click) {
368                 /* if within LED, swallow event */
369                 
370                 int top = lrint (_height/2.0 - _diameter/2.0);
371                 int bottom = lrint (_height/2.0 + _diameter/2.0);
372                 int left;
373                 int right;
374                 
375                 if (_elements & Text) {
376                         if (_led_left) {
377                                 left = 4;
378                                 right = left + _diameter;
379                         } else {
380                                 left = lrint (_width - 4 - _diameter/2.0);
381                                 right = left + _diameter;
382                         }
383                 } else {
384                         left = _width/2.0 - (_diameter/2.0);
385                         right = _width/2.0 + (_diameter/2.0);
386                 }
387
388                 if (ev->x >= left && ev->x <= right && ev->y <= bottom && ev->y >= top) {
389                         return true;
390                 }
391         }
392
393         if (binding_proxy.button_press_handler (ev)) {
394                 return true;
395         }
396
397         if (!_act_on_release) {
398                 if (_action) {
399                         _action->activate ();
400                         return true;
401                 }
402         }
403
404         return false;
405 }
406
407 bool
408 ArdourButton::on_button_release_event (GdkEventButton *ev)
409 {
410         if ((_elements & Indicator) && _distinct_led_click) {
411
412                 /* if within LED, emit signal */
413
414                 int top = lrint (_height/2.0 - _diameter/2.0);
415                 int bottom = lrint (_height/2.0 + _diameter/2.0);
416                 int left;
417                 int right;
418                 if (_led_left) {
419                         left = 4;
420                         right = left + _diameter;
421                 } else {
422                         left = lrint (_width - 4 - _diameter/2.0);
423                         right = left + _diameter;
424                 }
425                 
426                 if (ev->x >= left && ev->x <= right && ev->y <= bottom && ev->y >= top) {
427                         signal_led_clicked(); /* EMIT SIGNAL */
428                         return true;
429                 }
430         }
431
432         if (_act_on_release) {
433                 if (_action) {
434                         _action->activate ();
435                         return true;
436                 }
437         }
438
439         return false;
440 }
441
442 void
443 ArdourButton::set_distinct_led_click (bool yn)
444 {
445         _distinct_led_click = yn;
446 }
447
448 void
449 ArdourButton::color_handler ()
450 {
451         set_colors ();
452         set_dirty ();
453 }
454
455 void
456 ArdourButton::on_size_allocate (Allocation& alloc)
457 {
458         CairoWidget::on_size_allocate (alloc);
459         set_colors ();
460 }
461
462 void
463 ArdourButton::set_controllable (boost::shared_ptr<Controllable> c)
464 {
465         watch_connection.disconnect ();
466         binding_proxy.set_controllable (c);
467 }
468
469 void
470 ArdourButton::watch ()
471 {
472         boost::shared_ptr<Controllable> c (binding_proxy.get_controllable ());
473
474         if (!c) {
475                 warning << _("button cannot watch state of non-existing Controllable\n") << endmsg;
476                 return;
477         }
478
479         c->Changed.connect (watch_connection, invalidator(*this), boost::bind (&ArdourButton::controllable_changed, this), gui_context());
480 }
481
482 void
483 ArdourButton::controllable_changed ()
484 {
485         float val = binding_proxy.get_controllable()->get_value();
486
487         if (fabs (val) >= 0.5f) {
488                 set_active_state (CairoWidget::Active);
489         } else {
490                 unset_active_state ();
491         }
492 }
493
494 void
495 ArdourButton::set_related_action (RefPtr<Action> act)
496 {
497         _action = act;
498
499         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
500         if (tact) {
501                 tact->signal_toggled().connect (sigc::mem_fun (*this, &ArdourButton::action_toggled));
502         }
503 }
504
505 void
506 ArdourButton::action_toggled ()
507 {
508         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (_action);
509
510         if (tact) {
511                 if (tact->get_active()) {
512                         set_active_state (CairoWidget::Active);
513                 } else {
514                         unset_active_state ();
515                 }
516         }
517 }