2 Copyright 2009 by Hans Baier, Krzysztof Foltman
10 public class VuMeter
: DrawingArea
{
18 private Cairo
.Surface? cache_surface
= null;
19 private Mode _mode
= Mode
.STANDARD
; private double _value
= 0.5;
23 if (Math
.fabs(_value
- value
) >= 0.0001)
47 // Set favored widget size
48 set_size_request (100, 14);
51 /* Widget is asked to draw itself */
52 public override bool expose_event (Gdk
.EventExpose event
) {
54 int sx
= allocation
.width
- 2, sy
= allocation
.height
- 2;
56 // Create a Cairo context
57 var cr
= Gdk
.cairo_create (this
.window
);
59 if (cache_surface
== null)
62 // looks like its either first call or the widget has been resized.
63 // create the cache_surface.
64 cache_surface
= new Cairo
.Surface
.similar (cr
.get_target(),
69 // And render the meterstuff again.
71 Cairo
.Context cache_cr
= new Cairo
.Context ( cache_surface
);
72 cache_cr
.set_source_rgba (0, 0, 0, 0);
73 cache_cr
.rectangle (ox
, oy
, sx
, sy
);
75 cache_cr
.set_line_width (1);
77 for (x
= ox
; x
<= ox
+ sx
; x
+= 3)
79 double ts
= (x
- ox
) * 1.0 / sx
;
80 double r
= 0, g
= 0, b
= 0;
92 g
= 1 - (ts
- 0.75) / 0.25;
96 case Mode
.MONOCHROME_REVERSE
:
101 case Mode
.MONOCHROME
:
107 cache_cr
.set_source_rgb(r
, g
, b
);
108 cache_cr
.move_to (x
, oy
);
109 cache_cr
.line_to (x
, oy
+ sy
+ 1);
110 cache_cr
.move_to (x
, oy
+ sy
);
111 cache_cr
.line_to (x
, oy
);
116 // Set clipping area in order to avoid unnecessary drawing
117 cr
.rectangle (event
.area
.x
, event
.area
.y
,
118 event
.area
.width
, event
.area
.height
);
121 cr
.set_source_surface (cache_surface
, 0, 0);
123 cr
.set_source_rgba (0, 0, 0, 0.5);
125 if( mode
== Mode
.MONOCHROME_REVERSE
)
126 cr
.rectangle (ox
, oy
, _value
* (sx
- ox
) + 1, sy
- oy
+ 1);
128 cr
.rectangle (ox
+ _value
* (sx
- ox
), oy
, sx
- ox
+ 1, sy
- oy
+ 1);
136 } // namespace Prolooks