.
1 /*
2 Copyright (C) 2014 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 using Cairo;
16 using Math;
17
18 namespace BirdFont {
19
20 public class CheckBox : Widget {
21
22 public bool checked = false;
23 public signal void updated (bool checked);
24 public double padding = 3.333 * MainWindow.units;
25
26 public double w = 12 * MainWindow.units;
27 public double h = 12 * MainWindow.units;
28
29 bool has_focus = false;
30
31 public Text label;
32
33 public CheckBox (string text = "", double font_size = -1) {
34 if (font_size < 0) {
35 font_size = h;
36 }
37
38 label = new Text (text, font_size);
39 Theme.text_color (label, "Text Foreground");
40 }
41
42 public void set_checked (bool c) {
43 checked = c;
44 updated (c);
45 }
46
47 public override double get_height () {
48 return label.font_size;
49 }
50
51 public override double get_width () {
52 return w + 2 * padding + label.get_sidebearing_extent ();
53 }
54
55 public override void draw (Context cr) {
56 double d = w * 0.25;
57 double center_y = (get_height () - (h + 2 * padding)) / 2.0 + padding;
58
59 cr.save ();
60 Theme.color (cr, "Checkbox Background");
61 draw_rounded_rectangle (cr, widget_x, widget_y + center_y, w, h - padding, padding);
62 cr.fill ();
63 cr.restore ();
64
65 cr.save ();
66 cr.set_line_width (1);
67
68 if (has_focus) {
69 Theme.color (cr, "Highlighted 1");
70 } else {
71 Theme.color (cr, "Text Foreground");
72 }
73
74 draw_rounded_rectangle (cr, widget_x, widget_y + center_y, w, h - padding, padding);
75 cr.stroke ();
76 cr.restore ();
77
78 if (checked) {
79 cr.save ();
80
81 Theme.color (cr, "Text Foreground");
82 cr.set_line_width (1);
83
84 cr.move_to (widget_x + d, widget_y + d + center_y);
85 cr.line_to (widget_x + w - d, widget_y + h - d + center_y);
86 cr.stroke ();
87
88 cr.move_to (widget_x + w - d, widget_y + d + center_y);
89 cr.line_to (widget_x + d, widget_y + h - d + center_y);
90 cr.stroke ();
91
92 cr.restore ();
93 }
94
95 label.widget_x = widget_x + 1.5 * w;
96 label.widget_y = widget_y;
97 label.draw (cr);
98 }
99
100 public override void key_press (uint keyval) {
101 unichar c = (unichar) keyval;
102
103 if (c == ' ') {
104 checked = !checked;
105 }
106 }
107
108 public override void focus (bool focus) {
109 has_focus = focus;
110 }
111
112 }
113
114 }
115