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 {
29 class LedColorWidget : Gtk.DrawingArea {
30 public LedColor color;
32 public LedColorWidget () {
36 public LedColorWidget.with_color (LedColor _color) {
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);
46 ctx.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
50 ctx.arc (width / 2.0, height / 2.0, Math.fmin (width, height) / 2.0, 0, 2 * Math.PI);
54 ctx.set_operator (Cairo.Operator.ADD);
56 if (LedColor.R in color) {
57 ctx.set_source_rgb (1.0, 0, 0);
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);
65 if (LedColor.G in color) {
66 ctx.set_source_rgb (0, 1.0, 0);
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);
74 if (LedColor.B in color) {
75 ctx.set_source_rgb (0, 0, 1.0);
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);
86 public void update () {
87 unowned Gdk.Region region = window.get_clip_region ();
89 // redraw the cairo canvas completely by exposing it
90 window.invalidate_region (region, true);
91 window.process_updates (true);
95 public class LedColorButton : Gtk.Button {
96 LedColorWidget ledcolor;
98 public LedColorButton.with_map (string led_map) {
100 set_color (LedColor.R);
102 set_color (LedColor.G);
104 set_color (LedColor.B);
106 set_color (LedColor.RG);
108 set_color (LedColor.RB);
110 set_color (LedColor.GB);
111 if (led_map == "rgb")
112 set_color (LedColor.RGB);
115 public LedColorButton.with_color (LedColor color) {
116 ledcolor.color = color;
119 public void set_color (LedColor color) {
120 ledcolor.color = color;
124 public LedColor get_color () {
125 return ledcolor.color;
129 Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
130 ledcolor = new LedColorWidget ();
131 ledcolor.set_size_request (55, 55);
136 class LedColorDialog : Gtk.Dialog {
137 public LedColorDialog () {
138 set_title ("Pick a LED color");
140 var content = (Gtk.VBox) get_content_area ();
141 var hbox = new Gtk.HBox (true, 0);
143 LedColor[] colors = {
144 LedColor.R, LedColor.G, LedColor.B,
145 LedColor.RG, LedColor.RB, LedColor.GB,
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);
156 content.pack_start (hbox, true, true, 0);
160 void on_clicked (Gtk.Button _button) {
161 var button = (LedColorButton) _button;
163 response ((int) button.get_color ());