1 /* This file is part of LED Pattern Editor.
3 * Copyright (C) 2010 Philipp Zabel
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.
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.
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/>.
19 public enum LedColor {
30 class LedColorWidget : Gtk.DrawingArea {
31 public LedColor color;
33 public LedColorWidget () {
37 public LedColorWidget.with_color (LedColor _color) {
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;
47 ctx.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
51 ctx.translate (width / 2.0, height / 2.0);
52 ctx.arc (0, 0, radius * 1.25, 0, 2 * Math.PI);
55 ctx.set_operator (Cairo.Operator.ADD);
57 if (LedColor.R in color) {
59 ctx.set_source_rgb (1.0, 0, 0);
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);
68 if (LedColor.G in color) {
70 ctx.set_source_rgb (0, 1.0, 0);
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);
79 if (LedColor.B in color) {
81 ctx.set_source_rgb (0, 0, 1.0);
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);
93 public void update () {
94 unowned Gdk.Region region = window.get_clip_region ();
96 // redraw the cairo canvas completely by exposing it
97 window.invalidate_region (region, true);
98 window.process_updates (true);
102 public class LedColorButton : Gtk.Button {
103 LedColorWidget ledcolor;
105 public LedColorButton.with_map (string led_map) {
107 set_color (LedColor.R);
109 set_color (LedColor.G);
111 set_color (LedColor.B);
113 set_color (LedColor.RG);
115 set_color (LedColor.RB);
117 set_color (LedColor.GB);
118 if (led_map == "rgb")
119 set_color (LedColor.RGB);
122 public LedColorButton.with_color (LedColor color) {
123 ledcolor.color = color;
126 public void set_color (LedColor color) {
127 ledcolor.color = color;
131 public LedColor get_color () {
132 return ledcolor.color;
136 Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
137 ledcolor = new LedColorWidget ();
138 ledcolor.set_size_request (55, 55);
143 class LedColorDialog : Gtk.Dialog {
144 public LedColorDialog () {
145 set_title (_("Pick a LED color"));
147 var content = (Gtk.VBox) get_content_area ();
148 var hbox = new Gtk.HBox (true, 0);
150 LedColor[] colors = {
151 LedColor.R, LedColor.G, LedColor.B,
152 LedColor.RG, LedColor.RB, LedColor.GB,
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);
163 content.pack_start (hbox, true, true, 0);
167 void on_clicked (Gtk.Button _button) {
168 var button = (LedColorButton) _button;
170 response ((int) button.get_color ());