Add OFF state to LedColor
[led-pattern-ed] / src / led-color-widgets.vala
1 /* This file is part of LED Pattern Editor.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * LED Pattern Editor is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * LED Pattern Editor is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with LED Pattern Editor. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 public enum LedColor {
20         OFF = 0,
21         R = 1,
22         G = 2,
23         B = 4,
24         RG = 3,
25         RB = 5,
26         GB = 6,
27         RGB = 7
28 }
29
30 class LedColorWidget : Gtk.DrawingArea {
31         public LedColor color;
32
33         public LedColorWidget () {
34                 color = LedColor.OFF;
35         }
36
37         public LedColorWidget.with_color (LedColor _color) {
38                 color = _color;
39         }
40
41         public override bool expose_event (Gdk.EventExpose event) {
42                 var ctx = Gdk.cairo_create (window);
43                 int height = allocation.height;
44                 int width = allocation.width;
45                 double radius = Math.fmin (width * 0.4, height * 0.4);
46
47                 ctx.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
48                 ctx.clip ();
49
50                 ctx.new_path ();
51                 ctx.arc (width / 2.0, height / 2.0, Math.fmin (width, height) / 2.0, 0, 2 * Math.PI);
52
53                 ctx.clip ();
54
55                 ctx.set_operator (Cairo.Operator.ADD);
56
57                 if (LedColor.R in color) {
58                         ctx.set_source_rgb (1.0, 0, 0);
59                         ctx.new_path ();
60                         ctx.arc (width / 2.0 + radius / 3.2 * Math.cos (270 * Math.PI / 180.0),
61                                  height / 2.0 + radius / 3.2 * Math.sin (270 * Math.PI / 180.0),
62                                  radius, 0, 2 * Math.PI);
63                         ctx.fill ();
64                 }
65
66                 if (LedColor.G in color) {
67                         ctx.set_source_rgb (0, 1.0, 0);
68                         ctx.new_path ();
69                         ctx.arc (width / 2.0 + radius / 3.2 * Math.cos (150 * Math.PI / 180.0),
70                                  height / 2.0 + radius / 3.2 * Math.sin (150 * Math.PI / 180.0),
71                                  radius, 0, 2 * Math.PI);
72                         ctx.fill ();
73                 }
74
75                 if (LedColor.B in color) {
76                         ctx.set_source_rgb (0, 0, 1.0);
77                         ctx.new_path ();
78                         ctx.arc (width / 2.0 + radius / 3.2 * Math.cos (30 * Math.PI / 180.0),
79                                  height / 2.0 + radius / 3.2 * Math.sin (30 * Math.PI / 180.0),
80                                  radius, 0, 2 * Math.PI);
81                         ctx.fill ();
82                 }
83
84                 return true;
85         }
86
87         public void update () {
88                 unowned Gdk.Region region = window.get_clip_region ();
89
90                 // redraw the cairo canvas completely by exposing it
91                 window.invalidate_region (region, true);
92                 window.process_updates (true);
93         }
94 }
95
96 public class LedColorButton : Gtk.Button {
97         LedColorWidget ledcolor;
98
99         public LedColorButton.with_map (string led_map) {
100                 if (led_map == "r")
101                         set_color (LedColor.R);
102                 if (led_map == "g")
103                         set_color (LedColor.G);
104                 if (led_map == "b")
105                         set_color (LedColor.B);
106                 if (led_map == "rg")
107                         set_color (LedColor.RG);
108                 if (led_map == "rb")
109                         set_color (LedColor.RB);
110                 if (led_map == "gb")
111                         set_color (LedColor.GB);
112                 if (led_map == "rgb")
113                         set_color (LedColor.RGB);
114         }
115
116         public LedColorButton.with_color (LedColor color) {
117                 ledcolor.color = color;
118         }
119
120         public void set_color (LedColor color) {
121                 ledcolor.color = color;
122                 ledcolor.update ();
123         }
124
125         public LedColor get_color () {
126                 return ledcolor.color;
127         }
128
129         construct {
130                 Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
131                 ledcolor = new LedColorWidget ();
132                 ledcolor.set_size_request (55, 55);
133                 add (ledcolor);
134         }
135 }
136
137 class LedColorDialog : Gtk.Dialog {
138         public LedColorDialog () {
139                 set_title ("Pick a LED color");
140
141                 var content = (Gtk.VBox) get_content_area ();
142                 var hbox = new Gtk.HBox (true, 0);
143
144                 LedColor[] colors = {
145                         LedColor.R, LedColor.G, LedColor.B,
146                         LedColor.RG, LedColor.RB, LedColor.GB,
147                         LedColor.RGB
148                 };
149
150                 for (int i = 0; i < colors.length; i++) {
151                         var button = new LedColorButton.with_color (colors[i]);
152                         Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
153                         button.clicked.connect (on_clicked);
154                         hbox.pack_start (button, true, true, 0);
155                 }
156
157                 content.pack_start (hbox, true, true, 0);
158                 content.show_all ();
159         }
160
161         void on_clicked (Gtk.Button _button) {
162                 var button = (LedColorButton) _button;
163
164                 response ((int) button.get_color ());
165         }
166 }
167