867b3ab525a168abc85faa8942c02bae68be21cc
[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         R = 1,
21         G = 2,
22         B = 4,
23         RG = 3,
24         RB = 5,
25         GB = 6,
26         RGB = 7
27 }
28
29 class LedColorWidget : Gtk.DrawingArea {
30         public LedColor color;
31
32         public LedColorWidget () {
33                 color = LedColor.RGB;
34         }
35
36         public LedColorWidget.with_color (LedColor _color) {
37                 color = _color;
38         }
39
40         public override bool expose_event (Gdk.EventExpose event) {
41                 var ctx = Gdk.cairo_create (window);
42                 int height = allocation.height;
43                 int width = allocation.width;
44                 double radius = Math.fmin (width * 0.4, height * 0.4);
45
46                 ctx.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
47                 ctx.clip ();
48
49                 ctx.new_path ();
50                 ctx.arc (width / 2.0, height / 2.0, Math.fmin (width, height) / 2.0, 0, 2 * Math.PI);
51
52                 ctx.clip ();
53
54                 ctx.set_operator (Cairo.Operator.ADD);
55
56                 if (LedColor.R in color) {
57                         ctx.set_source_rgb (1.0, 0, 0);
58                         ctx.new_path ();
59                         ctx.arc (width / 2.0 + radius / 3.2 * Math.cos (270 * Math.PI / 180.0),
60                                  height / 2.0 + radius / 3.2 * Math.sin (270 * Math.PI / 180.0),
61                                  radius, 0, 2 * Math.PI);
62                         ctx.fill ();
63                 }
64
65                 if (LedColor.G in color) {
66                         ctx.set_source_rgb (0, 1.0, 0);
67                         ctx.new_path ();
68                         ctx.arc (width / 2.0 + radius / 3.2 * Math.cos (150 * Math.PI / 180.0),
69                                  height / 2.0 + radius / 3.2 * Math.sin (150 * Math.PI / 180.0),
70                                  radius, 0, 2 * Math.PI);
71                         ctx.fill ();
72                 }
73
74                 if (LedColor.B in color) {
75                         ctx.set_source_rgb (0, 0, 1.0);
76                         ctx.new_path ();
77                         ctx.arc (width / 2.0 + radius / 3.2 * Math.cos (30 * Math.PI / 180.0),
78                                  height / 2.0 + radius / 3.2 * Math.sin (30 * Math.PI / 180.0),
79                                  radius, 0, 2 * Math.PI);
80                         ctx.fill ();
81                 }
82
83                 return true;
84         }
85
86         public void update () {
87                 unowned Gdk.Region region = window.get_clip_region ();
88
89                 // redraw the cairo canvas completely by exposing it
90                 window.invalidate_region (region, true);
91                 window.process_updates (true);
92         }
93 }
94
95 public class LedColorButton : Gtk.Button {
96         LedColorWidget ledcolor;
97
98         public LedColorButton.with_map (string led_map) {
99                 if (led_map == "r")
100                         set_color (LedColor.R);
101                 if (led_map == "g")
102                         set_color (LedColor.G);
103                 if (led_map == "b")
104                         set_color (LedColor.B);
105                 if (led_map == "rg")
106                         set_color (LedColor.RG);
107                 if (led_map == "rb")
108                         set_color (LedColor.RB);
109                 if (led_map == "gb")
110                         set_color (LedColor.GB);
111                 if (led_map == "rgb")
112                         set_color (LedColor.RGB);
113         }
114
115         public LedColorButton.with_color (LedColor color) {
116                 ledcolor.color = color;
117         }
118
119         public void set_color (LedColor color) {
120                 ledcolor.color = color;
121                 ledcolor.update ();
122         }
123
124         public LedColor get_color () {
125                 return ledcolor.color;
126         }
127
128         construct {
129                 Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
130                 ledcolor = new LedColorWidget ();
131                 ledcolor.set_size_request (55, 55);
132                 add (ledcolor);
133         }
134 }
135
136 class LedColorDialog : Gtk.Dialog {
137         public LedColorDialog () {
138                 set_title ("Pick a LED color");
139
140                 var content = (Gtk.VBox) get_content_area ();
141                 var hbox = new Gtk.HBox (true, 0);
142
143                 LedColor[] colors = {
144                         LedColor.R, LedColor.G, LedColor.B,
145                         LedColor.RG, LedColor.RB, LedColor.GB,
146                         LedColor.RGB
147                 };
148
149                 for (int i = 0; i < colors.length; i++) {
150                         var button = new LedColorButton.with_color (colors[i]);
151                         Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
152                         button.clicked.connect (on_clicked);
153                         hbox.pack_start (button, true, true, 0);
154                 }
155
156                 content.pack_start (hbox, true, true, 0);
157                 content.show_all ();
158         }
159
160         void on_clicked (Gtk.Button _button) {
161                 var button = (LedColorButton) _button;
162
163                 response ((int) button.get_color ());
164         }
165 }
166