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 = Math.fmin (width * 0.4, height * 0.4);
47 ctx.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
51 ctx.arc (width / 2.0, height / 2.0, Math.fmin (width, height) / 2.0, 0, 2 * Math.PI);
55 ctx.set_operator (Cairo.Operator.ADD);
57 if (LedColor.R in color) {
58 ctx.set_source_rgb (1.0, 0, 0);
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);
66 if (LedColor.G in color) {
67 ctx.set_source_rgb (0, 1.0, 0);
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);
75 if (LedColor.B in color) {
76 ctx.set_source_rgb (0, 0, 1.0);
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);
87 public void update () {
88 unowned Gdk.Region region = window.get_clip_region ();
90 // redraw the cairo canvas completely by exposing it
91 window.invalidate_region (region, true);
92 window.process_updates (true);
96 public class LedColorButton : Gtk.Button {
97 LedColorWidget ledcolor;
99 public LedColorButton.with_map (string led_map) {
101 set_color (LedColor.R);
103 set_color (LedColor.G);
105 set_color (LedColor.B);
107 set_color (LedColor.RG);
109 set_color (LedColor.RB);
111 set_color (LedColor.GB);
112 if (led_map == "rgb")
113 set_color (LedColor.RGB);
116 public LedColorButton.with_color (LedColor color) {
117 ledcolor.color = color;
120 public void set_color (LedColor color) {
121 ledcolor.color = color;
125 public LedColor get_color () {
126 return ledcolor.color;
130 Hildon.gtk_widget_set_theme_size (this, Hildon.SizeType.FINGER_HEIGHT);
131 ledcolor = new LedColorWidget ();
132 ledcolor.set_size_request (55, 55);
137 class LedColorDialog : Gtk.Dialog {
138 public LedColorDialog () {
139 set_title ("Pick a LED color");
141 var content = (Gtk.VBox) get_content_area ();
142 var hbox = new Gtk.HBox (true, 0);
144 LedColor[] colors = {
145 LedColor.R, LedColor.G, LedColor.B,
146 LedColor.RG, LedColor.RB, LedColor.GB,
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);
157 content.pack_start (hbox, true, true, 0);
161 void on_clicked (Gtk.Button _button) {
162 var button = (LedColorButton) _button;
164 response ((int) button.get_color ());