summaryrefslogtreecommitdiff
path: root/gtk2_ardour/led.cc
blob: dca5caefc153a6f8c380f32846b559e8591a5d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
    Copyright (C) 2010 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <iostream>
#include <cmath>
#include <algorithm>

#include "led.h"

using namespace Gdk;
using namespace Gtk;
using namespace Glib;
using namespace Gtkmm2ext;

LED::LED()
	: _diameter (0.0)
	, _red (0.0)
	, _green (1.0)
	, _blue (0.0)
	, _fixed_diameter (false)
{
}

LED::~LED()
{
}

void
LED::render (cairo_t* cr, cairo_rectangle_t*)
{
	if (!_fixed_diameter) {
		_diameter = std::min (get_width(), get_height());
	}

	//background

	Widget* parent;
	RefPtr<Style> style;
	Color c;

	parent = get_parent ();

	while (parent && !parent->get_has_window()) {
		parent = parent->get_parent();
	}

	if (parent && parent->get_has_window()) {
		style = parent->get_style ();
		c = style->get_bg (parent->get_state());
	} else {
		style = get_style ();
		c = style->get_bg (get_state());
	}

#if 0
	cairo_rectangle(cr, 0, 0, _width, _height);
	cairo_stroke_preserve(cr);
	cairo_set_source_rgb(cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
	cairo_fill(cr);
#endif

	cairo_translate(cr, get_width()/2, get_height()/2);

	//inset
	cairo_pattern_t *pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter);
	cairo_pattern_add_color_stop_rgba (pat, 0, 0,0,0, 0.4);
	cairo_pattern_add_color_stop_rgba (pat, 1, 1,1,1, 0.7);
	cairo_arc (cr, 0, 0, _diameter/2, 0, 2 * M_PI);
	cairo_set_source (cr, pat);
	cairo_fill (cr);
	cairo_pattern_destroy (pat);

	//black ring
	cairo_set_source_rgb (cr, 0, 0, 0);
	cairo_arc (cr, 0, 0, _diameter/2-2, 0, 2 * M_PI);
	cairo_fill(cr);

	//knob color
	cairo_set_source_rgba (cr, _red, _green, _blue, (active_state() == Gtkmm2ext::ExplicitActive) ? 0.8 : 0.2);
	cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
	cairo_fill(cr);

	//reflection
	cairo_scale(cr, 0.7, 0.7);
	cairo_pattern_t *pat2 = cairo_pattern_create_linear (0.0, 0.0, 0.0, _diameter/2-3);
	cairo_pattern_add_color_stop_rgba (pat2, 0, 1,1,1, active_state() ? 0.4 : 0.2);
	cairo_pattern_add_color_stop_rgba (pat2, 1, 1,1,1, 0.0);
	cairo_arc (cr, 0, 0, _diameter/2-3, 0, 2 * M_PI);
	cairo_set_source (cr, pat2);
	cairo_fill (cr);
	cairo_pattern_destroy (pat2);

	cairo_stroke (cr);
}

void
LED::set_diameter (float d)
{
	_diameter = (d*2) + 5.0;

	if (_diameter != 0.0) {
		_fixed_diameter = true;
	}

	set_dirty ();
}

void
LED::on_realize ()
{
	set_colors_from_style ();
	CairoWidget::on_realize ();
}

void
LED::on_size_request (Gtk::Requisition* req)
{
	if (_fixed_diameter) {
		req->width = _diameter;
		req->height = _diameter;
	} else {
		CairoWidget::on_size_request (req);
	}
}

void
LED::set_colors_from_style ()
{
	RefPtr<Style> style = get_style();
	Color c;

	switch (_visual_state) {
	case VisualState(0):
		c = style->get_fg (STATE_NORMAL);
		break;
	default:
		c = style->get_fg (STATE_ACTIVE);
		break;
	}

	_red = c.get_red_p ();
	_green = c.get_green_p ();
	_blue = c.get_blue_p ();

	set_dirty ();
}