Debian packaging: 0.0.5-1
[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 = int.min (width, 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.translate (width / 2.0, height / 2.0);
52                 ctx.arc (0, 0, radius * 1.25, 0, 2 * Math.PI);
53                 ctx.clip ();
54
55                 ctx.set_operator (Cairo.Operator.ADD);
56
57                 if (LedColor.R in color) {
58                         ctx.save ();
59                         ctx.set_source_rgb (1.0, 0, 0);
60                         ctx.new_path ();
61                         ctx.translate (radius / 3.2 * Math.cos (270 * Math.PI / 180.0),
62                                        radius / 3.2 * Math.sin (270 * Math.PI / 180.0));
63                         ctx.arc (0, 0, radius, 0, 2 * Math.PI);
64                         ctx.fill ();
65                         ctx.restore ();
66                 }
67
68                 if (LedColor.G in color) {
69                         ctx.save ();
70                         ctx.set_source_rgb (0, 1.0, 0);
71                         ctx.new_path ();
72                         ctx.translate (radius / 3.2 * Math.cos (150 * Math.PI / 180.0),
73                                        radius / 3.2 * Math.sin (150 * Math.PI / 180.0));
74                         ctx.arc (0, 0, radius, 0, 2 * Math.PI);
75                         ctx.fill ();
76                         ctx.restore ();
77                 }
78
79                 if (LedColor.B in color) {
80                         ctx.save ();
81                         ctx.set_source_rgb (0, 0, 1.0);
82                         ctx.new_path ();
83                         ctx.translate (radius / 3.2 * Math.cos (30 * Math.PI / 180.0),
84                                        radius / 3.2 * Math.sin (30 * Math.PI / 180.0));
85                         ctx.arc (0, 0, radius, 0, 2 * Math.PI);
86                         ctx.fill ();
87                         ctx.restore ();
88                 }
89
90                 return true;
91         }
92
93         public void update () {
94                 unowned Gdk.Region region = window.get_clip_region ();
95
96                 // redraw the cairo canvas completely by exposing it
97                 window.invalidate_region (region, true);
98                 window.process_updates (true);
99         }
100 }
101
102 public class LedColorButton : Gtk.Button {
103         LedColorWidget ledcolor;
104
105         public LedColorButton.with_map (string led_map) {
106                 if (led_map == "r")
107                         set_color (LedColor.R);
108                 if (led_map == "g")
109                         set_color (LedColor.G);
110                 if (led_map == "b")
111                         set_color (LedColor.B);
112                 if (led_map == "rg")
113                         set_color (LedColor.RG);
114                 if (led_map == "rb")
115                         set_color (LedColor.RB);
116                 if (led_map == "gb")
117                         set_color (LedColor.GB);
118                 if (led_map == "rgb")
119                         set_color (LedColor.RGB);
120         }
121
122         public LedColorButton.with_color (LedColor color) {
123                 ledcolor.color = color;
124         }
125
126         public void set_color (LedColor color) {
127                 ledcolor.color = color;
128                 ledcolor.update ();
129         }
130
131         public LedColor get_color () {
132                 return ledcolor.color;
133         }
134
135         construct {
136                 Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
137                 ledcolor = new LedColorWidget ();
138                 ledcolor.set_size_request (55, 55);
139                 add (ledcolor);
140         }
141 }
142
143 class LedColorDialog : Gtk.Dialog {
144         public LedColorDialog () {
145                 set_title (_("Pick a LED color"));
146
147                 var content = (Gtk.VBox) get_content_area ();
148                 var hbox = new Gtk.HBox (true, 0);
149
150                 LedColor[] colors = {
151                         LedColor.R, LedColor.G, LedColor.B,
152                         LedColor.RG, LedColor.RB, LedColor.GB,
153                         LedColor.RGB
154                 };
155
156                 for (int i = 0; i < colors.length; i++) {
157                         var button = new LedColorButton.with_color (colors[i]);
158                         Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
159                         button.clicked.connect (on_clicked);
160                         hbox.pack_start (button, true, true, 0);
161                 }
162
163                 content.pack_start (hbox, true, true, 0);
164                 content.show_all ();
165         }
166
167         void on_clicked (Gtk.Button _button) {
168                 var button = (LedColorButton) _button;
169
170                 response ((int) button.get_color ());
171         }
172 }
173