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>
25 #include <calf/drawingutils.h>
30 GtkWidget
*widget
= GTK_WIDGET( g_object_new (CALF_TYPE_LED
, NULL
));
35 calf_led_expose (GtkWidget
*widget
, GdkEventExpose
*event
)
37 g_assert(CALF_IS_LED(widget
));
39 CalfLed
*self
= CALF_LED(widget
);
40 GdkWindow
*window
= widget
->window
;
41 cairo_t
*c
= gdk_cairo_create(GDK_DRAWABLE(window
));
42 GtkStyle
*style
= gtk_widget_get_style(widget
);
46 int sx
= widget
->allocation
.width
- ox
* 2;
47 int sy
= widget
->allocation
.height
- oy
* 2;
48 int xc
= widget
->allocation
.width
/ 2;
49 int yc
= widget
->allocation
.height
/ 2;
52 if( self
->cache_surface
== NULL
) {
53 // looks like its either first call or the widget has been resized.
54 // create the cache_surface.
55 cairo_surface_t
*window_surface
= cairo_get_target( c
);
56 self
->cache_surface
= cairo_surface_create_similar( window_surface
,
58 widget
->allocation
.width
,
59 widget
->allocation
.height
);
60 cairo_t
*cache_cr
= cairo_create( self
->cache_surface
);
62 // if(widget->style->bg_pixmap[0] == NULL) {
63 gdk_cairo_set_source_color(cache_cr
,&style
->bg
[GTK_STATE_NORMAL
]);
65 // gdk_cairo_set_source_pixbuf(cache_cr, GDK_PIXBUF(&style->bg_pixmap[0]), widget->allocation.x, widget->allocation.y + 20);
67 cairo_paint(cache_cr
);
71 cairo_rectangle(cache_cr
, pad
, pad
, sx
+ ox
* 2 - pad
* 2, sy
+ oy
* 2 - pad
* 2);
72 cairo_set_source_rgb(cache_cr
, 0, 0, 0);
73 cairo_set_operator(cache_cr
,CAIRO_OPERATOR_CLEAR
);
75 cairo_set_operator(cache_cr
,CAIRO_OPERATOR_OVER
);
79 create_rectangle(cache_cr
, pad
, pad
, sx
+ ox
* 2 - pad
* 2, sy
+ oy
* 2 - pad
* 2, 0);
80 cairo_pattern_t
*pat2
= cairo_pattern_create_linear (0, 0, 0, sy
+ oy
* 2 - pad
* 2);
82 get_bg_color(widget
, NULL
, &r
, &g
, &b
);
83 cairo_pattern_add_color_stop_rgba (pat2
, 0, r
*1.111, g
*1.111, b
*1.111, 1);
84 cairo_pattern_add_color_stop_rgba (pat2
, 1, r
*0.82, g
*0.82, b
*0.82, 1);
85 cairo_set_source (cache_cr
, pat2
);
88 cairo_rectangle(cache_cr
, ox
, oy
, sx
, sy
);
89 cairo_set_source_rgb (cache_cr
, 0, 0, 0);
92 cairo_destroy( cache_cr
);
95 cairo_set_source_surface( c
, self
->cache_surface
, 0,0 );
99 cairo_pattern_t
*pt
= cairo_pattern_create_radial(xc
, yc
, 0, xc
, yc
, xc
> yc
? xc
: yc
);
101 float value
= self
->led_value
;
103 if(self
->led_mode
>= 4 && self
->led_mode
<= 5 && value
> 1.f
) {
106 switch (self
->led_mode
) {
110 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);
111 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);
112 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);
116 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);
117 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);
118 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);
122 // blue-dynamic (limited)
123 cairo_pattern_add_color_stop_rgb(pt
, 0.0, value
* 0.2, value
* 0.75 + 0.25, value
* 0.65 + 0.35);
124 cairo_pattern_add_color_stop_rgb(pt
, 0.5, value
* 0.1, value
* 0.45 + 0.15, value
* 0.55 + 0.2);
125 cairo_pattern_add_color_stop_rgb(pt
, 1.0, 0.0, value
* 0.2 + 0.1, value
* 0.4 + 0.1);
129 // red-dynamic (limited)
130 cairo_pattern_add_color_stop_rgb(pt
, 0.0, value
* 0.65 + 0.35, value
* 0.5, value
* 0.2);
131 cairo_pattern_add_color_stop_rgb(pt
, 0.5, value
* 0.6 + 0.2, value
* 0.2, value
* 0.1);
132 cairo_pattern_add_color_stop_rgb(pt
, 1.0, value
* 0.66 + 0.1, value
* 0.1, 0.0);
135 // blue-dynamic with red peak at >= 1.f
137 cairo_pattern_add_color_stop_rgb(pt
, 0.0, value
* 0.2, value
* 0.75 + 0.25, value
* 0.65 + 0.35);
138 cairo_pattern_add_color_stop_rgb(pt
, 0.5, value
* 0.1, value
* 0.45 + 0.15, value
* 0.55 + 0.2);
139 cairo_pattern_add_color_stop_rgb(pt
, 1.0, 0.0, value
* 0.2 + 0.1, value
* 0.4 + 0.1);
141 cairo_pattern_add_color_stop_rgb(pt
, 0.0, 1.0, 0.5, 0.2);
142 cairo_pattern_add_color_stop_rgb(pt
, 0.5, 0.80, 0.2, 0.1);
143 cairo_pattern_add_color_stop_rgb(pt
, 1.0, 0.66, 0.1, 0.0);
147 // off @ 0.0, blue < 1.0, red @ 1.0
148 if(value
< 1.f
and value
> 0.f
) {
150 cairo_pattern_add_color_stop_rgb(pt
, 0.0, 0.2, 1.0, 1.0);
151 cairo_pattern_add_color_stop_rgb(pt
, 0.5, 0.1, 0.6, 0.75);
152 cairo_pattern_add_color_stop_rgb(pt
, 1.0, 0.0, 0.3, 0.5);
153 } else if(value
== 0.f
) {
155 cairo_pattern_add_color_stop_rgb(pt
, 0.0, 0.0, 0.25, 0.35);
156 cairo_pattern_add_color_stop_rgb(pt
, 0.5, 0.0, 0.15, 0.2);
157 cairo_pattern_add_color_stop_rgb(pt
, 1.0, 0.0, 0.1, 0.1);
160 cairo_pattern_add_color_stop_rgb(pt
, 0.0, 1.0, 0.5, 0.2);
161 cairo_pattern_add_color_stop_rgb(pt
, 0.5, 0.80, 0.2, 0.1);
162 cairo_pattern_add_color_stop_rgb(pt
, 1.0, 0.66, 0.1, 0.0);
167 cairo_rectangle(c
, ox
+ 1, oy
+ 1, sx
- 2, sy
- 2);
168 cairo_set_source (c
, pt
);
169 cairo_fill_preserve(c
);
170 pt
= cairo_pattern_create_linear (ox
, oy
, ox
, ox
+ sy
);
171 cairo_pattern_add_color_stop_rgba (pt
, 0, 1, 1, 1, 0.4);
172 cairo_pattern_add_color_stop_rgba (pt
, 0.4, 1, 1, 1, 0.1);
173 cairo_pattern_add_color_stop_rgba (pt
, 0.401, 0, 0, 0, 0.0);
174 cairo_pattern_add_color_stop_rgba (pt
, 1, 0, 0, 0, 0.2);
175 cairo_set_source (c
, pt
);
177 cairo_pattern_destroy(pt
);
185 calf_led_realize(GtkWidget
*widget
)
187 GTK_WIDGET_SET_FLAGS(widget
, GTK_REALIZED
);
189 GdkWindowAttr attributes
;
190 attributes
.event_mask
= GDK_EXPOSURE_MASK
| GDK_BUTTON_PRESS_MASK
;
191 attributes
.x
= widget
->allocation
.x
;
192 attributes
.y
= widget
->allocation
.y
;
193 attributes
.width
= widget
->allocation
.width
;
194 attributes
.height
= widget
->allocation
.height
;
195 attributes
.wclass
= GDK_INPUT_OUTPUT
;
196 attributes
.window_type
= GDK_WINDOW_CHILD
;
198 widget
->window
= gdk_window_new(gtk_widget_get_parent_window (widget
), &attributes
, GDK_WA_X
| GDK_WA_Y
);
200 gdk_window_set_user_data(widget
->window
, widget
);
201 widget
->style
= gtk_style_attach(widget
->style
, widget
->window
);
205 calf_led_size_request (GtkWidget
*widget
,
206 GtkRequisition
*requisition
)
208 g_assert(CALF_IS_LED(widget
));
209 CalfLed
*self
= CALF_LED(widget
);
210 requisition
->width
= self
->size
? 24 : 19;
211 requisition
->height
= self
->size
? 18 : 14;
215 calf_led_size_allocate (GtkWidget
*widget
,
216 GtkAllocation
*allocation
)
218 g_assert(CALF_IS_LED(widget
));
219 CalfLed
*led
= CALF_LED(widget
);
221 widget
->allocation
= *allocation
;
223 if( led
->cache_surface
)
224 cairo_surface_destroy( led
->cache_surface
);
225 led
->cache_surface
= NULL
;
227 if (GTK_WIDGET_REALIZED(widget
))
228 gdk_window_move_resize(widget
->window
, allocation
->x
, allocation
->y
, allocation
->width
, allocation
->height
);
232 calf_led_button_press (GtkWidget
*widget
, GdkEventButton
*event
)
238 calf_led_class_init (CalfLedClass
*klass
)
240 GtkWidgetClass
*widget_class
= GTK_WIDGET_CLASS(klass
);
241 widget_class
->realize
= calf_led_realize
;
242 widget_class
->expose_event
= calf_led_expose
;
243 widget_class
->size_request
= calf_led_size_request
;
244 widget_class
->size_allocate
= calf_led_size_allocate
;
245 widget_class
->button_press_event
= calf_led_button_press
;
249 calf_led_init (CalfLed
*self
)
251 GtkWidget
*widget
= GTK_WIDGET(self
);
252 // GtkWidget *widget = GTK_WIDGET(self);
253 // GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
254 self
->led_value
= 0.f
;
255 self
->cache_surface
= NULL
;
256 widget
->requisition
.width
= self
->size
? 24 : 19;
257 widget
->requisition
.height
= self
->size
? 18 : 14;
260 void calf_led_set_value(CalfLed
*led
, float value
)
262 if (value
!= led
->led_value
)
264 float old_value
= led
->led_value
;
265 led
->led_value
= value
;
266 if (led
->led_mode
>= 2 || (old_value
> 0) != (value
> 0))
268 GtkWidget
*widget
= GTK_WIDGET (led
);
269 if (GTK_WIDGET_REALIZED(widget
))
270 gtk_widget_queue_draw (widget
);
275 gboolean
calf_led_get_value(CalfLed
*led
)
277 return led
->led_value
;
281 calf_led_get_type (void)
283 static GType type
= 0;
285 static const GTypeInfo type_info
= {
286 sizeof(CalfLedClass
),
287 NULL
, /* base_init */
288 NULL
, /* base_finalize */
289 (GClassInitFunc
)calf_led_class_init
,
290 NULL
, /* class_finalize */
291 NULL
, /* class_data */
294 (GInstanceInitFunc
)calf_led_init
297 for (int i
= 0; ; i
++) {
298 char *name
= g_strdup_printf("CalfLed%u%d",
299 ((unsigned int)(intptr_t)calf_led_class_init
) >> 16, i
);
300 if (g_type_from_name(name
)) {
304 type
= g_type_register_static(GTK_TYPE_WIDGET
,