Copyrights
[libprolooks.git] / prolooks / LED.vala
blob7253095f46968b09a9be6ff36ae7079f9a340af5
1 /*
2 Copyright 2009 by Krzysztof Foltman
3 License: LGPLv2+
4 */
6 using Gtk;
8 namespace Prolooks {
10 public class LED : DrawingArea {
12 private bool _led_state = false;
14 private float r = 1;
15 private float g = 0;
16 private float b = 0;
18 public bool led_state {
19 set {
20 _led_state = value;
21 queue_draw();
23 get {
24 return _led_state;
28 public LED () {
30 // Set favored widget size
31 set_size_request (12, 12);
34 public void set_rgb(float _r, float _g, float _b)
36 r = _r;
37 g = _g;
38 b = _b;
39 queue_draw();
42 public void get_rgb(ref float _r, ref float _g, ref float _b)
44 _r = r;
45 _g = g;
46 _b = b;
49 /* Widget is asked to draw itself */
50 public override bool expose_event (Gdk.EventExpose event) {
52 // Create a Cairo context
53 var cr = Gdk.cairo_create (this.window);
55 // Set clipping area in order to avoid unnecessary drawing
56 cr.rectangle (event.area.x, event.area.y,
57 event.area.width, event.area.height);
58 cr.clip ();
60 set_source_from_color(cr, style.bg[Gtk.StateType.NORMAL]);
61 cr.rectangle(0, 0, allocation.width, allocation.height);
62 cr.fill();
64 int xc = allocation.width / 2;
65 int yc = allocation.height / 2;
67 int diameter = (allocation.width < allocation.height ? allocation.width : allocation.height) - 1;
69 Cairo.Pattern pt = new Cairo.Pattern.radial(xc, yc + diameter / 4, 0, xc, yc, diameter / 2);
70 double m1 = _led_state ? 1.0 : 0.25;
71 double m2 = _led_state ? 0.75 : 0.2;
72 double m3 = _led_state ? 0.25 : 0.1;
73 double m4 = 0.3;
74 pt.add_color_stop_rgb(0.0, r * m1, g * m1, b * m1);
75 pt.add_color_stop_rgb(0.5, r * m2, g * m2, b * m2);
76 pt.add_color_stop_rgb(1.0, r * m3, g * m3, b * m3);
78 cr.arc(xc, yc, diameter / 2, 0, 2 * Math.PI);
79 cr.set_line_join(Cairo.LineJoin.BEVEL);
80 cr.set_source (pt);
81 cr.fill();
83 cr.arc(xc, yc, diameter / 2, 0, 2 * Math.PI);
84 cr.set_line_width(0.5);
85 cr.set_source_rgba (r * m4, g * m4 , b * m4, 0.5);
86 cr.stroke();
88 return false;
92 } // namespace Prolooks