ValueDisplay: Bugfixes
[libprolooks.git] / prolooks / ValueDisplay.vala
bloba7832209f049caad980a31d412451c017edc469b
1 /*
2 Copyright 2009 by Hans Baier
3 License: LGPLv2+
4 */
6 using Gtk;
8 namespace Prolooks {
10 public class ValueDisplay : DrawingArea {
11 private Cairo.Surface? cache_surface = null;
13 private string _text = "";
14 public string text {
15 set {
16 if (_text != value) {
17 _text = value;
18 queue_draw ();
21 get {
22 return _text;
26 private bool _show_matrix = true;
27 public bool show_matrix {
28 get { return _show_matrix; }
29 set {
30 _show_matrix = value;
31 cache_surface = null;
32 queue_draw ();
36 private int width = 36;
37 private int height = 16;
39 construct {
40 set_size_request (width, height);
43 /* Widget is asked to draw itself */
44 public override bool expose_event (Gdk.EventExpose event) {
45 //Gdk.Color test_color = color_from_string ("#ff0000");
47 if (width != allocation.width || height != allocation.height) {
48 width = allocation.width;
49 height = allocation.height;
50 cache_surface = null;
52 var inner_width = width - 6;
53 var inner_height = height - 6;
55 // Create a Cairo context
56 var cr = Gdk.cairo_create (this.window);
58 if (cache_surface == null) {
59 cache_surface = new Cairo.Surface.similar (cr.get_target (),
60 Cairo.Content.COLOR_ALPHA,
61 width,
62 height);
64 Cairo.Context cache_cr = new Cairo.Context ( cache_surface );
66 DisplayBase.outer_rim_for_display (cache_cr, 0, 0, width, height, style.bg[Gtk.StateType.SELECTED]);
68 cache_cr.translate (3, 3);
70 DisplayBase.lcd(cache_cr, inner_width, inner_height, 3.0);
72 if (show_matrix) {
73 //especially the matrix is expensive for large displays
74 DisplayBase.dot_matrix (cache_cr, 0.5, 0.5, inner_width, inner_height, DisplayBase.matrix_dot_color ());
78 // Set clipping area in order to avoid unnecessary drawing
79 cr.rectangle (event.area.x, event.area.y,
80 event.area.width, event.area.height);
81 cr.clip ();
83 cr.set_source_surface (cache_surface, 0, 0);
84 cr.paint ();
86 cr.translate (3, 3);
88 // draw text
89 double font_size = inner_height;
91 cr.select_font_face ("FreeSans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
92 cr.set_font_size (font_size);
93 Cairo.TextExtents ext = Cairo.TextExtents();
94 cr.text_extents (text, ref ext);
95 double x = (inner_width - ext.x_advance) / 2.0;
96 double y = inner_height * 0.85;
98 DisplayBase.text (cr, text, x, y, font_size);
100 DisplayBase.reflection(cr, inner_width, inner_height);
102 return true;
106 } // namespace Prolooks