Transient Designer: image and text for manual, small change to the UI
[calf.git] / src / ctl_led.cpp
blob55f787bd8ea337d1e4d7bf45b3354b9023bcae21
1 /* Calf DSP Library
2 * Light emitting diode-like control.
4 * Copyright (C) 2008 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 #include <calf/ctl_led.h>
22 #include <math.h>
23 #include <stdint.h>
24 #include <stdlib.h>
26 GtkWidget *
27 calf_led_new()
29 GtkWidget *widget = GTK_WIDGET( g_object_new (CALF_TYPE_LED, NULL ));
30 return widget;
33 static gboolean
34 calf_led_expose (GtkWidget *widget, GdkEventExpose *event)
36 g_assert(CALF_IS_LED(widget));
38 CalfLed *self = CALF_LED(widget);
39 GdkWindow *window = widget->window;
40 cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(window));
41 GtkStyle *style = gtk_widget_get_style(widget);
43 int ox = 4;
44 int oy = 3;
45 int sx = widget->allocation.width - ox * 2;
46 int sy = widget->allocation.height - oy * 2;
47 int xc = widget->allocation.width / 2;
48 int yc = widget->allocation.height / 2;
49 int pad;
51 if( self->cache_surface == NULL ) {
52 // looks like its either first call or the widget has been resized.
53 // create the cache_surface.
54 cairo_surface_t *window_surface = cairo_get_target( c );
55 self->cache_surface = cairo_surface_create_similar( window_surface,
56 CAIRO_CONTENT_COLOR,
57 widget->allocation.width,
58 widget->allocation.height );
59 cairo_t *cache_cr = cairo_create( self->cache_surface );
61 // if(widget->style->bg_pixmap[0] == NULL) {
62 gdk_cairo_set_source_color(cache_cr,&style->bg[GTK_STATE_NORMAL]);
63 // } else {
64 // gdk_cairo_set_source_pixbuf(cache_cr, GDK_PIXBUF(&style->bg_pixmap[0]), widget->allocation.x, widget->allocation.y + 20);
65 // }
66 cairo_paint(cache_cr);
68 // outer (black)
69 pad = 0;
70 cairo_rectangle(cache_cr, pad, pad, sx + ox * 2 - pad * 2, sy + oy * 2 - pad * 2);
71 cairo_set_source_rgb(cache_cr, 0, 0, 0);
72 cairo_fill(cache_cr);
74 // inner (bevel)
75 pad = 1;
76 cairo_rectangle(cache_cr, pad, pad, sx + ox * 2 - pad * 2, sy + oy * 2 - pad * 2);
77 cairo_pattern_t *pat2 = cairo_pattern_create_linear (0, 0, 0, sy + oy * 2 - pad * 2);
78 cairo_pattern_add_color_stop_rgba (pat2, 0, 0.23, 0.23, 0.23, 1);
79 cairo_pattern_add_color_stop_rgba (pat2, 0.5, 0, 0, 0, 1);
80 cairo_set_source (cache_cr, pat2);
81 cairo_fill(cache_cr);
83 cairo_rectangle(cache_cr, ox, oy, sx, sy);
84 cairo_set_source_rgb (cache_cr, 0, 0, 0);
85 cairo_fill(cache_cr);
87 cairo_destroy( cache_cr );
90 cairo_set_source_surface( c, self->cache_surface, 0,0 );
91 cairo_paint( c );
94 cairo_pattern_t *pt = cairo_pattern_create_radial(xc, yc, 0, xc, yc, xc > yc ? xc : yc);
96 float value = self->led_value;
98 if(self->led_mode >= 4 && self->led_mode <= 5 && value > 1.f) {
99 value = 1.f;
101 switch (self->led_mode) {
102 default:
103 case 0:
104 // blue-on/off
105 cairo_pattern_add_color_stop_rgb(pt, 0.0, value > 0.f ? 0.2 : 0.0, value > 0.f ? 1.0 : 0.25, value > 0.f ? 1.0 : 0.35);
106 cairo_pattern_add_color_stop_rgb(pt, 0.5, value > 0.f ? 0.1 : 0.0, value > 0.f ? 0.6 : 0.15, value > 0.f ? 0.75 : 0.2);
107 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.0, value > 0.f ? 0.3 : 0.1, value > 0.f ? 0.5 : 0.1);
108 break;
109 case 1:
110 // red-on/off
111 cairo_pattern_add_color_stop_rgb(pt, 0.0, value > 0.f ? 1.0 : 0.35, value > 0.f ? 0.5 : 0.0, value > 0.f ? 0.2 : 0.0);
112 cairo_pattern_add_color_stop_rgb(pt, 0.5, value > 0.f ? 0.80 : 0.2, value > 0.f ? 0.2 : 0.0, value > 0.f ? 0.1 : 0.0);
113 cairo_pattern_add_color_stop_rgb(pt, 1.0, value > 0.f ? 0.65 : 0.1, value > 0.f ? 0.1 : 0.0, 0.0);
114 break;
115 case 2:
116 case 4:
117 // blue-dynamic (limited)
118 cairo_pattern_add_color_stop_rgb(pt, 0.0, value * 0.2, value * 0.75 + 0.25, value * 0.65 + 0.35);
119 cairo_pattern_add_color_stop_rgb(pt, 0.5, value * 0.1, value * 0.45 + 0.15, value * 0.55 + 0.2);
120 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.0, value * 0.2 + 0.1, value * 0.4 + 0.1);
121 break;
122 case 3:
123 case 5:
124 // red-dynamic (limited)
125 cairo_pattern_add_color_stop_rgb(pt, 0.0, value * 0.65 + 0.35, value * 0.5, value * 0.2);
126 cairo_pattern_add_color_stop_rgb(pt, 0.5, value * 0.6 + 0.2, value * 0.2, value * 0.1);
127 cairo_pattern_add_color_stop_rgb(pt, 1.0, value * 0.66 + 0.1, value * 0.1, 0.0);
128 break;
129 case 6:
130 // blue-dynamic with red peak at >= 1.f
131 if(value < 1.0) {
132 cairo_pattern_add_color_stop_rgb(pt, 0.0, value * 0.2, value * 0.75 + 0.25, value * 0.65 + 0.35);
133 cairo_pattern_add_color_stop_rgb(pt, 0.5, value * 0.1, value * 0.45 + 0.15, value * 0.55 + 0.2);
134 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.0, value * 0.2 + 0.1, value * 0.4 + 0.1);
135 } else {
136 cairo_pattern_add_color_stop_rgb(pt, 0.0, 1.0, 0.5, 0.2);
137 cairo_pattern_add_color_stop_rgb(pt, 0.5, 0.80, 0.2, 0.1);
138 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.66, 0.1, 0.0);
140 break;
141 case 7:
142 // off @ 0.0, blue < 1.0, red @ 1.0
143 if(value < 1.f and value > 0.f) {
144 // blue
145 cairo_pattern_add_color_stop_rgb(pt, 0.0, 0.2, 1.0, 1.0);
146 cairo_pattern_add_color_stop_rgb(pt, 0.5, 0.1, 0.6, 0.75);
147 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.0, 0.3, 0.5);
148 } else if(value == 0.f) {
149 // off
150 cairo_pattern_add_color_stop_rgb(pt, 0.0, 0.0, 0.25, 0.35);
151 cairo_pattern_add_color_stop_rgb(pt, 0.5, 0.0, 0.15, 0.2);
152 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.0, 0.1, 0.1);
153 } else {
154 // red
155 cairo_pattern_add_color_stop_rgb(pt, 0.0, 1.0, 0.5, 0.2);
156 cairo_pattern_add_color_stop_rgb(pt, 0.5, 0.80, 0.2, 0.1);
157 cairo_pattern_add_color_stop_rgb(pt, 1.0, 0.66, 0.1, 0.0);
159 break;
162 cairo_rectangle(c, ox + 1, oy + 1, sx - 2, sy - 2);
163 cairo_set_source (c, pt);
164 cairo_fill_preserve(c);
165 pt = cairo_pattern_create_linear (ox, oy, ox, ox + sy);
166 cairo_pattern_add_color_stop_rgba (pt, 0, 1, 1, 1, 0.4);
167 cairo_pattern_add_color_stop_rgba (pt, 0.4, 1, 1, 1, 0.1);
168 cairo_pattern_add_color_stop_rgba (pt, 0.401, 0, 0, 0, 0.0);
169 cairo_pattern_add_color_stop_rgba (pt, 1, 0, 0, 0, 0.2);
170 cairo_set_source (c, pt);
171 cairo_fill(c);
172 cairo_pattern_destroy(pt);
174 cairo_destroy(c);
176 return TRUE;
179 static void
180 calf_led_realize(GtkWidget *widget)
182 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
184 GdkWindowAttr attributes;
185 attributes.event_mask = GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
186 attributes.x = widget->allocation.x;
187 attributes.y = widget->allocation.y;
188 attributes.width = widget->allocation.width;
189 attributes.height = widget->allocation.height;
190 attributes.wclass = GDK_INPUT_OUTPUT;
191 attributes.window_type = GDK_WINDOW_CHILD;
193 widget->window = gdk_window_new(gtk_widget_get_parent_window (widget), &attributes, GDK_WA_X | GDK_WA_Y);
195 gdk_window_set_user_data(widget->window, widget);
196 widget->style = gtk_style_attach(widget->style, widget->window);
199 static void
200 calf_led_size_request (GtkWidget *widget,
201 GtkRequisition *requisition)
203 g_assert(CALF_IS_LED(widget));
204 CalfLed *self = CALF_LED(widget);
205 requisition->width = self->size ? 24 : 19;
206 requisition->height = self->size ? 18 : 14;
209 static void
210 calf_led_size_allocate (GtkWidget *widget,
211 GtkAllocation *allocation)
213 g_assert(CALF_IS_LED(widget));
214 CalfLed *led = CALF_LED(widget);
216 widget->allocation = *allocation;
218 if( led->cache_surface )
219 cairo_surface_destroy( led->cache_surface );
220 led->cache_surface = NULL;
222 if (GTK_WIDGET_REALIZED(widget))
223 gdk_window_move_resize(widget->window, allocation->x, allocation->y, allocation->width, allocation->height );
226 static gboolean
227 calf_led_button_press (GtkWidget *widget, GdkEventButton *event)
229 return TRUE;
232 static void
233 calf_led_class_init (CalfLedClass *klass)
235 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
236 widget_class->realize = calf_led_realize;
237 widget_class->expose_event = calf_led_expose;
238 widget_class->size_request = calf_led_size_request;
239 widget_class->size_allocate = calf_led_size_allocate;
240 widget_class->button_press_event = calf_led_button_press;
243 static void
244 calf_led_init (CalfLed *self)
246 GtkWidget *widget = GTK_WIDGET(self);
247 // GtkWidget *widget = GTK_WIDGET(self);
248 // GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
249 self->led_value = 0.f;
250 self->cache_surface = NULL;
251 widget->requisition.width = self->size ? 24 : 19;
252 widget->requisition.height = self->size ? 18 : 14;
255 void calf_led_set_value(CalfLed *led, float value)
257 if (value != led->led_value)
259 float old_value = led->led_value;
260 led->led_value = value;
261 if (led->led_mode >= 2 || (old_value > 0) != (value > 0))
263 GtkWidget *widget = GTK_WIDGET (led);
264 if (GTK_WIDGET_REALIZED(widget))
265 gtk_widget_queue_draw (widget);
270 gboolean calf_led_get_value(CalfLed *led)
272 return led->led_value;
275 GType
276 calf_led_get_type (void)
278 static GType type = 0;
279 if (!type) {
280 static const GTypeInfo type_info = {
281 sizeof(CalfLedClass),
282 NULL, /* base_init */
283 NULL, /* base_finalize */
284 (GClassInitFunc)calf_led_class_init,
285 NULL, /* class_finalize */
286 NULL, /* class_data */
287 sizeof(CalfLed),
288 0, /* n_preallocs */
289 (GInstanceInitFunc)calf_led_init
292 for (int i = 0; ; i++) {
293 char *name = g_strdup_printf("CalfLed%u%d",
294 ((unsigned int)(intptr_t)calf_led_class_init) >> 16, i);
295 if (g_type_from_name(name)) {
296 free(name);
297 continue;
299 type = g_type_register_static(GTK_TYPE_WIDGET,
300 name,
301 &type_info,
302 (GTypeFlags)0);
303 free(name);
304 break;
307 return type;