replace ::cast_dynamic() with relevant ActionManager::get_*_action() calls
[ardour.git] / gtk2_ardour / led.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 "led.h"
25
26 using namespace Gdk;
27 using namespace Gtk;
28 using namespace Glib;
29 using namespace Gtkmm2ext;
30
31 LED::LED()
32         : _diameter (0.0)
33         , _red (0.0)
34         , _green (1.0)
35         , _blue (0.0)
36         , _fixed_diameter (false)
37 {
38 }
39
40 LED::~LED()
41 {
42 }
43
44 void
45 LED::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
46 {
47         cairo_t* cr = ctx->cobj();
48         if (!_fixed_diameter) {
49                 _diameter = std::min (get_width(), get_height());
50         }
51
52         //background
53
54         Widget* parent;
55         RefPtr<Style> style;
56         Color c;
57
58         parent = get_parent ();
59
60         while (parent && !parent->get_has_window()) {
61                 parent = parent->get_parent();
62         }
63
64         if (parent && parent->get_has_window()) {
65                 style = parent->get_style ();
66                 c = style->get_bg (parent->get_state());
67         } else {
68                 style = get_style ();
69                 c = style->get_bg (get_state());
70         }
71
72 #if 0
73         cairo_rectangle(cr, 0, 0, _width, _height);
74         cairo_stroke_preserve(cr);
75         cairo_set_source_rgb(cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
76         cairo_fill(cr);
77 #endif
78
79         cairo_translate(cr, get_width()/2, get_height()/2);
80
81         //inset
82         cairo_pattern_t *pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
83         cairo_pattern_add_color_stop_rgba (pat, 0, 0,0,0, 0.4);
84         cairo_pattern_add_color_stop_rgba (pat, 1, 1,1,1, 0.7);
85         cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
86         cairo_set_source (cr, pat);
87         cairo_fill (cr);
88         cairo_pattern_destroy (pat);
89
90         //black ring
91         cairo_set_source_rgb (cr, 0, 0, 0);
92         cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
93         cairo_fill(cr);
94
95         //knob color
96         cairo_set_source_rgba (cr, _red, _green, _blue, (active_state() == Gtkmm2ext::ExplicitActive) ? 0.8 : 0.2);
97         cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
98         cairo_fill(cr);
99
100         //reflection
101         cairo_scale(cr, 0.7, 0.7);
102         cairo_pattern_t *pat2 = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
103         cairo_pattern_add_color_stop_rgba (pat2, 0, 1,1,1, active_state() ? 0.4 : 0.2);
104         cairo_pattern_add_color_stop_rgba (pat2, 1, 1,1,1, 0.0);
105         cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
106         cairo_set_source (cr, pat2);
107         cairo_fill (cr);
108         cairo_pattern_destroy (pat2);
109
110         cairo_stroke (cr);
111 }
112
113 void
114 LED::set_diameter (float d)
115 {
116         _diameter = (d*2) + 5.0;
117
118         if (_diameter != 0.0) {
119                 _fixed_diameter = true;
120         }
121
122         set_dirty ();
123 }
124
125 void
126 LED::on_realize ()
127 {
128         set_colors_from_style ();
129         CairoWidget::on_realize ();
130 }
131
132 void
133 LED::on_size_request (Gtk::Requisition* req)
134 {
135         if (_fixed_diameter) {
136                 req->width = _diameter;
137                 req->height = _diameter;
138         } else {
139                 CairoWidget::on_size_request (req);
140         }
141 }
142
143 void
144 LED::set_colors_from_style ()
145 {
146         RefPtr<Style> style = get_style();
147         Color c;
148
149         switch (_visual_state) {
150         case VisualState(0):
151                 c = style->get_fg (STATE_NORMAL);
152                 break;
153         default:
154                 c = style->get_fg (STATE_ACTIVE);
155                 break;
156         }
157
158         _red = c.get_red_p ();
159         _green = c.get_green_p ();
160         _blue = c.get_blue_p ();
161
162         set_dirty ();
163 }