.
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 }
40
41 public void set_checked (bool c) {
42 checked = c;
43 updated (c);
44 }
45
46 public override double get_height () {
47 return label.font_size;
48 }
49
50 public override double get_width () {
51 return w + 2 * padding + label.get_sidebearing_extent ();
52 }
53
54 public override void draw (Context cr) {
55 double d = w * 0.25;
56 double center_y = (get_height () - (h + 2 * padding)) / 2.0 + padding;
57
58 cr.save ();
59 Theme.color (cr, "Background 2");
60 draw_rounded_rectangle (cr, widget_x, widget_y + center_y, w, h - padding, padding);
61 cr.fill ();
62 cr.restore ();
63
64 cr.save ();
65 cr.set_line_width (1);
66
67 if (has_focus) {
68 Theme.color (cr, "Highlighted 1");
69 } else {
70 Theme.color (cr, "Foreground 1");
71 }
72
73 draw_rounded_rectangle (cr, widget_x, widget_y + center_y, w, h - padding, padding);
74 cr.stroke ();
75 cr.restore ();
76
77 if (checked) {
78 cr.save ();
79
80 Theme.color (cr, "Foreground 1");
81 cr.set_line_width (1);
82
83 cr.move_to (widget_x + d, widget_y + d + center_y);
84 cr.line_to (widget_x + w - d, widget_y + h - d + center_y);
85 cr.stroke ();
86
87 cr.move_to (widget_x + w - d, widget_y + d + center_y);
88 cr.line_to (widget_x + d, widget_y + h - d + center_y);
89 cr.stroke ();
90
91 cr.restore ();
92 }
93
94 label.widget_x = widget_x + 1.5 * w;
95 label.widget_y = widget_y;
96 label.draw (cr);
97 }
98
99 public override void key_press (uint keyval) {
100 unichar c = (unichar) keyval;
101
102 if (c == ' ') {
103 checked = !checked;
104 }
105 }
106
107 public override void focus (bool focus) {
108 has_focus = focus;
109 }
110
111 }
112
113 }
114