.
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 public Text label;
30
31 public CheckBox (string text = "", double font_size = -1) {
32 if (font_size < 0) {
33 font_size = h;
34 }
35
36 label = new Text (text, font_size);
37 }
38
39 public void set_checked (bool c) {
40 checked = c;
41 updated (c);
42 }
43
44 public override double get_height () {
45 return label.font_size;
46 }
47
48 public override double get_width () {
49 return w + 2 * padding + label.get_sidebearing_extent ();
50 }
51
52 public override void draw (Context cr) {
53 double d = w * 0.25;
54 double center_y = (get_height () - (h + 2 * padding)) / 2.0 + padding;
55
56 cr.save ();
57 Theme.color (cr, "Background 2");
58 draw_rounded_rectangle (cr, widget_x, widget_y + center_y, w, h - padding, padding);
59 cr.fill ();
60 cr.restore ();
61
62 cr.save ();
63 cr.set_line_width (1);
64 Theme.color (cr, "Foreground 1");
65 draw_rounded_rectangle (cr, widget_x, widget_y + center_y, w, h - padding, padding);
66 cr.stroke ();
67 cr.restore ();
68
69 if (checked) {
70 cr.save ();
71
72 Theme.color (cr, "Foreground 1");
73 cr.set_line_width (1);
74
75 cr.move_to (widget_x + d, widget_y + d + center_y);
76 cr.line_to (widget_x + w - d, widget_y + h - d + center_y);
77 cr.stroke ();
78
79 cr.move_to (widget_x + w - d, widget_y + d + center_y);
80 cr.line_to (widget_x + d, widget_y + h - d + center_y);
81 cr.stroke ();
82
83 cr.restore ();
84 }
85
86 label.widget_x = widget_x + 1.5 * w;
87 label.widget_y = widget_y;
88 label.draw (cr);
89 }
90
91 }
92
93 }
94