ValueDisplay: new widget for displaying values, something like a fancy label
[libprolooks.git] / prolooks / Led.vala
blob4a5632c5832017ad18cdc63ad58e352ac45b4511
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 double r = 1;
15 private double g = 0;
16 private double 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 string color {
35 set {
36 if (value != null & value != "") {
37 gdk_color_to_cairo_color (color_from_string (value), ref r, ref g, ref b);
38 queue_draw ();
39 } else {
40 r = 1; g = 0; b = 0;
45 public void set_rgb(double _r, double _g, double _b)
47 r = _r;
48 g = _g;
49 b = _b;
50 queue_draw();
53 public void get_rgb(ref double _r, ref double _g, ref double _b)
55 _r = r;
56 _g = g;
57 _b = b;
60 /* Widget is asked to draw itself */
61 public override bool expose_event (Gdk.EventExpose event) {
63 // Create a Cairo context
64 var cr = Gdk.cairo_create (this.window);
66 // Set clipping area in order to avoid unnecessary drawing
67 cr.rectangle (event.area.x, event.area.y,
68 event.area.width, event.area.height);
69 cr.clip ();
71 set_source_color(cr, style.bg[Gtk.StateType.NORMAL]);
72 cr.rectangle(0, 0, allocation.width, allocation.height);
73 cr.fill();
75 int xc = allocation.width / 2;
76 int yc = allocation.height / 2;
78 int diameter = (allocation.width < allocation.height ? allocation.width : allocation.height) - 1;
80 Cairo.Pattern pt = new Cairo.Pattern.radial(xc, yc + diameter / 4, 0, xc, yc, diameter / 2);
81 double m1 = _led_state ? 1.0 : 0.25;
82 double m2 = _led_state ? 0.75 : 0.2;
83 double m3 = _led_state ? 0.25 : 0.1;
84 double m4 = 0.3;
85 pt.add_color_stop_rgb(0.0, r * m1, g * m1, b * m1);
86 pt.add_color_stop_rgb(0.5, r * m2, g * m2, b * m2);
87 pt.add_color_stop_rgb(1.0, r * m3, g * m3, b * m3);
89 cr.arc(xc, yc, diameter / 2, 0, 2 * Math.PI);
90 cr.set_line_join(Cairo.LineJoin.BEVEL);
91 cr.set_source (pt);
92 cr.fill();
94 cr.arc(xc, yc, diameter / 2, 0, 2 * Math.PI);
95 cr.set_line_width(0.5);
96 cr.set_source_rgba (r * m4, g * m4 , b * m4, 0.5);
97 cr.stroke();
99 return false;
103 } // namespace Prolooks