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;
22 [Description(blurb
="the displayed value, ranging from 0.0 to 1.0")]
25 if (Math
.fabs(_value
- value
) >= 0.0001)
38 [Description(nick
="monochrome color", blurb
="sets the meter color in monochrome color mode, can contain a string as accepted by gdk_color_parse (), eg: #a0b1c2")]
41 if (value
!= null & value
!= "") {
42 gdk_color_to_cairo_color (color_from_string (value
), ref r
, ref g
, ref b
);
51 [Description(nick
="mode", blurb
="whether the vu meter should be red/yellow/green (standard), or one color (monochrome) or one color but flipped left to right (monochrome reversed)")]
66 // Set favored widget size
67 set_size_request (100, 14);
70 public override void size_allocate (Gdk
.Rectangle allocation
) {
71 base.size_allocate (allocation
);
75 /* Widget is asked to draw itself */
76 public override bool expose_event (Gdk
.EventExpose event
) {
78 int sx
= allocation
.width
- 2, sy
= allocation
.height
- 2;
80 // Create a Cairo context
81 var cr
= Gdk
.cairo_create (this
.window
);
83 if (cache_surface
== null)
86 // looks like its either first call or the widget has been resized.
87 // create the cache_surface.
88 cache_surface
= new Cairo
.Surface
.similar (cr
.get_target(),
93 // And render the meterstuff again.
95 Cairo
.Context cache_cr
= new Cairo
.Context ( cache_surface
);
96 cache_cr
.set_source_rgba (0, 0, 0, 0);
97 cache_cr
.rectangle (ox
, oy
, sx
, sy
);
99 cache_cr
.set_line_width (1);
101 for (x
= ox
; x
<= ox
+ sx
; x
+= 3)
103 double ts
= (x
- ox
) * 1.0 / sx
;
115 g
= 1 - (ts
- 0.75) / 0.25;
119 case Mode
.MONOCHROME_REVERSE
:
121 case Mode
.MONOCHROME
:
124 cache_cr
.set_source_rgb(r
, g
, b
);
125 cache_cr
.move_to (x
, oy
);
126 cache_cr
.line_to (x
, oy
+ sy
+ 1);
127 cache_cr
.move_to (x
, oy
+ sy
);
128 cache_cr
.line_to (x
, oy
);
133 // Set clipping area in order to avoid unnecessary drawing
134 cr
.rectangle (event
.area
.x
, event
.area
.y
,
135 event
.area
.width
, event
.area
.height
);
138 cr
.set_source_surface (cache_surface
, 0, 0);
140 cr
.set_source_rgba (0, 0, 0, 0.5);
142 if( mode
== Mode
.MONOCHROME_REVERSE
)
143 cr
.rectangle (ox
, oy
, _value
* (sx
- ox
) + 1, sy
- oy
+ 1);
145 cr
.rectangle (ox
+ _value
* (sx
- ox
), oy
, sx
- ox
+ 1, sy
- oy
+ 1);
153 } // namespace Prolooks