relayout solo led stuff, as per the boss' requests :)
[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
30 LED::LED()
31         : _visual_state (0)
32         , _active (false)
33         , _diameter (0.0)
34         , _fixed_diameter (false)
35         , _red (0.0)
36         , _green (1.0)
37         , _blue (0.0)
38
39 {
40 }
41
42 LED::~LED()
43 {
44 }
45
46 void
47 LED::render (cairo_t* cr)
48 {
49         if (!_fixed_diameter) {
50                 _diameter = std::min (_width, _height);
51         }
52
53         //background
54
55         RefPtr<Style> style = get_style();
56         Color c;
57         
58         switch (_visual_state) {
59         case 0:
60                 c = style->get_bg (STATE_NORMAL);
61                 break;
62         default:
63                 c = style->get_bg (STATE_ACTIVE);
64                 break;
65         }
66
67         cairo_rectangle(cr, 0, 0, _width, _height);
68         cairo_stroke_preserve(cr);
69         cairo_set_source_rgb(cr, c.get_green_p(), c.get_red_p(), c.get_blue_p());
70         cairo_fill(cr);
71
72         cairo_translate(cr, _width/2, _height/2);
73
74 #if 0
75         //inset
76         cairo_pattern_t *pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
77         cairo_pattern_add_color_stop_rgba (pat, 0, 0,0,0, 0.4);
78         cairo_pattern_add_color_stop_rgba (pat, 1, 1,1,1, 0.7);
79         cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
80         cairo_set_source (cr, pat);
81         cairo_fill (cr);
82         cairo_pattern_destroy (pat);
83
84         //black ring
85         cairo_set_source_rgb (cr, 0, 0, 0);
86         cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
87         cairo_fill(cr);
88
89         //knob color
90         cairo_set_source_rgba (cr, _red, _green, _blue, _active ? 0.8 : 0.2);
91         cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
92         cairo_fill(cr);
93
94         //reflection
95         cairo_scale(cr, 0.7, 0.7);
96         cairo_pattern_t *pat2 = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
97         cairo_pattern_add_color_stop_rgba (pat2, 0, 1,1,1, _active ? 0.4 : 0.2);
98         cairo_pattern_add_color_stop_rgba (pat2, 1, 1,1,1, 0.0);
99         cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
100         cairo_set_source (cr, pat2);
101         cairo_fill (cr);
102         cairo_pattern_destroy (pat2);
103 #endif
104
105         cairo_set_source_rgba (cr, _red, _green, _blue,  1.0);
106         cairo_arc (cr, 0, 0, _diameter/2-5, 0, 2 * M_PI);
107         cairo_fill(cr);
108
109         cairo_stroke (cr);
110 }
111
112 void
113 LED::set_visual_state (int32_t s)
114 {
115         if (s != _visual_state) {
116
117                 _visual_state = s;
118
119                 RefPtr<Style> style = get_style();
120                 Color c;
121                 
122                 switch (_visual_state) {
123                 case 0:
124                         c = style->get_fg (STATE_NORMAL);
125                         break;
126                 default:
127                         c = style->get_fg (STATE_ACTIVE);
128                         break;
129                 }
130
131                 _red = c.get_red_p ();
132                 _green = c.get_green_p ();
133                 _blue = c.get_blue_p ();
134                 
135                 set_dirty ();
136         }
137 }
138
139 void
140 LED::set_diameter (float d)
141 {
142         _diameter = (d*2) + 5.0;
143
144         if (_diameter != 0.0) {
145                 _fixed_diameter = true;
146         }
147
148         set_dirty ();
149 }
150
151 void
152 LED::on_size_request (Gtk::Requisition* req)
153 {
154         if (_fixed_diameter) {
155                 req->width = _diameter;
156                 req->height = _diameter;
157         } else {
158                 CairoWidget::on_size_request (req);
159         }
160 }