2 * This file is part of hildon-libs
4 * Copyright (C) 2005, 2006 Nokia Corporation.
6 * Author: Kuisma Salonen <kuisma.salonen@nokia.com>
7 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; version 2.1 of
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
30 #include "hildon-color-chooser-button.h"
32 #include "hildon-color-chooser-dialog.h"
41 static guint color_chooser_button_signals[LAST_SIGNAL] = { 0 };
44 static GtkButtonClass *parent_klass = NULL;
47 static void hildon_color_chooser_button_init(HildonColorChooserButton *object);
48 static void hildon_color_chooser_button_class_init(HildonColorChooserButtonClass *klass);
51 static void hildon_color_chooser_button_size_request(GtkWidget *widget, GtkRequisition *req);
52 static void hildon_color_chooser_button_size_allocate(GtkWidget *widget, GtkAllocation *alloc);
54 static void hildon_color_chooser_button_realize(GtkWidget *widget);
55 static void hildon_color_chooser_button_unrealize(GtkWidget *widget);
57 static void hildon_color_chooser_button_style_set(GtkWidget *widget, GtkStyle *previous_style);
59 static void hildon_color_chooser_button_show(GtkWidget *widget);
60 static void hildon_color_chooser_button_show_all(GtkWidget *widget);
62 static void hildon_color_chooser_button_virtual_set_color(HildonColorChooserButton *button, GdkColor *color);
63 static void hildon_color_chooser_button_virtual_color_changed(HildonColorChooserButton *button, GdkColor *color);
65 static void hildon_color_chooser_button_clicked(GtkButton *button);
68 static gboolean hildon_color_chooser_button_area_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data);
71 static void hildon_color_chooser_button_helper_get_style_info(HildonColorChooserButton *button);
74 GtkType hildon_color_chooser_button_get_type ()
76 static GtkType button_type = 0;
80 static const GtkTypeInfo button_info =
82 "HildonColorChooserButton",
83 sizeof (HildonColorChooserButton),
84 sizeof (HildonColorChooserButtonClass),
85 (GtkClassInitFunc) hildon_color_chooser_button_class_init,
86 (GtkObjectInitFunc) hildon_color_chooser_button_init,
87 /* reserved_1 */ NULL,
88 /* reserved_1 */ NULL,
89 (GtkClassInitFunc) NULL
92 button_type = gtk_type_unique (GTK_TYPE_BUTTON, &button_info);
99 /* initializer functions */
100 static void hildon_color_chooser_button_init(HildonColorChooserButton *object)
102 object->color.red = 0x0000;
103 object->color.green = 0x0000;
104 object->color.blue = 0x0000;
105 object->color.pixel = 0x00000000;
108 object->area = gtk_drawing_area_new();
110 gtk_container_add(GTK_CONTAINER(object), object->area);
113 g_signal_connect(G_OBJECT(object->area), "expose-event",
114 G_CALLBACK(hildon_color_chooser_button_area_expose), object);
117 static void hildon_color_chooser_button_class_init(HildonColorChooserButtonClass *klass)
119 GtkWidgetClass *widget_klass = GTK_WIDGET_CLASS(klass);
120 GtkButtonClass *button_klass = GTK_BUTTON_CLASS(klass);
121 GtkObjectClass *object_klass = GTK_OBJECT_CLASS(klass);
124 parent_klass = g_type_class_peek_parent(klass);
127 klass->set_color = hildon_color_chooser_button_virtual_set_color;
128 klass->color_changed = hildon_color_chooser_button_virtual_color_changed;
131 button_klass->clicked = hildon_color_chooser_button_clicked;
134 widget_klass->size_request = hildon_color_chooser_button_size_request;
135 widget_klass->size_allocate = hildon_color_chooser_button_size_allocate;
137 widget_klass->realize = hildon_color_chooser_button_realize;
138 widget_klass->unrealize = hildon_color_chooser_button_unrealize;
140 widget_klass->style_set = hildon_color_chooser_button_style_set;
142 widget_klass->show = hildon_color_chooser_button_show;
143 widget_klass->show_all = hildon_color_chooser_button_show_all;
146 gtk_widget_class_install_style_property(widget_klass,
147 g_param_spec_boxed("outer_border",
149 "Size of the outer border",
152 gtk_widget_class_install_style_property(widget_klass,
153 g_param_spec_boxed("inner_border",
155 "Size of the inner border",
158 gtk_widget_class_install_style_property(widget_klass,
159 g_param_spec_boxed("minimum_size",
161 "Minimum size of the color area",
166 color_chooser_button_signals[COLOR_CHANGED] = g_signal_new("color-changed", G_OBJECT_CLASS_TYPE (object_klass),
167 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (HildonColorChooserButtonClass, color_changed),
168 NULL, NULL, g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1, GDK_TYPE_COLOR);
172 /* virtual widget functions */
173 static void hildon_color_chooser_button_size_request(GtkWidget *widget, GtkRequisition *req)
175 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(widget);
178 req->width = button->style_info.outer.left + button->style_info.min.left + button->style_info.outer.right;
179 req->height = button->style_info.outer.top + button->style_info.min.right + button->style_info.outer.bottom;
182 static void hildon_color_chooser_button_size_allocate(GtkWidget *widget, GtkAllocation *alloc)
184 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(widget);
185 GtkAllocation child_alloc;
186 GdkRectangle clip_rect;
189 GTK_WIDGET_CLASS(parent_klass)->size_allocate(widget, alloc);
190 child_alloc = *alloc;
193 child_alloc.x += button->style_info.outer.left;
194 child_alloc.y += button->style_info.outer.top;
196 child_alloc.width -= (button->style_info.outer.left + button->style_info.outer.right);
197 child_alloc.height -= (button->style_info.outer.top + button->style_info.outer.bottom);
200 gtk_widget_size_allocate(button->area, &child_alloc);
203 if(GTK_WIDGET_REALIZED(widget)) {
204 clip_rect.x = button->style_info.inner.left;
205 clip_rect.y = button->style_info.inner.top;
206 clip_rect.width = button->area->allocation.width - button->style_info.inner.left - button->style_info.inner.right;
207 clip_rect.height = button->area->allocation.height - button->style_info.inner.top - button->style_info.inner.bottom;
209 gdk_gc_set_clip_rectangle(button->color_gc, &clip_rect);
214 static void hildon_color_chooser_button_realize(GtkWidget *widget)
216 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(widget);
217 GdkRectangle clip_rect;
220 GTK_WIDGET_CLASS(parent_klass)->realize(widget);
223 button->color_gc = gdk_gc_new(widget->window);
224 gdk_gc_set_rgb_fg_color(button->color_gc, &button->color);
227 clip_rect.x = button->style_info.inner.left;
228 clip_rect.y = button->style_info.inner.top;
229 clip_rect.width = button->area->allocation.width - button->style_info.inner.left - button->style_info.inner.right;
230 clip_rect.height = button->area->allocation.height - button->style_info.inner.top - button->style_info.inner.bottom;
232 gdk_gc_set_clip_rectangle(button->color_gc, &clip_rect);
235 static void hildon_color_chooser_button_unrealize(GtkWidget *widget)
237 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(widget);
240 g_object_unref(button->color_gc);
241 button->color_gc = NULL;
244 GTK_WIDGET_CLASS(parent_klass)->unrealize(widget);
248 static void hildon_color_chooser_button_style_set(GtkWidget *widget, GtkStyle *previous_style)
250 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(widget);
251 GdkRectangle clip_rect;
254 hildon_color_chooser_button_helper_get_style_info(button);
257 if(GTK_WIDGET_REALIZED(widget)) {
258 clip_rect.x = button->style_info.inner.left;
259 clip_rect.y = button->style_info.inner.top;
260 clip_rect.width = button->area->allocation.width - button->style_info.inner.left - button->style_info.inner.right;
261 clip_rect.height = button->area->allocation.height - button->style_info.inner.top - button->style_info.inner.bottom;
263 gdk_gc_set_clip_rectangle(button->color_gc, &clip_rect);
268 static void hildon_color_chooser_button_show(GtkWidget *widget)
270 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(widget);
273 gtk_widget_show(button->area);
275 GTK_WIDGET_CLASS(parent_klass)->show(widget);
278 static void hildon_color_chooser_button_show_all(GtkWidget *widget)
280 hildon_color_chooser_button_show(widget);
284 static void hildon_color_chooser_button_virtual_set_color(HildonColorChooserButton *button, GdkColor *color)
286 button->color = *color;
288 if(GTK_WIDGET_REALIZED(button)) {
289 gdk_gc_set_rgb_fg_color(button->color_gc, &button->color);
291 gtk_widget_queue_draw(button->area);
295 g_signal_emit(button, color_chooser_button_signals[COLOR_CHANGED], 0, &button->color);
298 static void hildon_color_chooser_button_virtual_color_changed(HildonColorChooserButton *button, GdkColor *color)
303 static void hildon_color_chooser_button_clicked(GtkButton *button)
305 HildonColorChooserButton *color_button = HILDON_COLOR_CHOOSER_BUTTON(button);
311 dialog = hildon_color_chooser_dialog_new();
312 gtk_widget_realize(dialog);
313 hildon_color_chooser_dialog_set_color(HILDON_COLOR_CHOOSER_DIALOG(dialog), &color_button->color);
314 gtk_widget_show(dialog);
317 result = gtk_dialog_run(GTK_DIALOG(dialog));
320 if(result == GTK_RESPONSE_OK) {
321 hildon_color_chooser_dialog_get_color(HILDON_COLOR_CHOOSER_DIALOG(dialog), &color);
322 hildon_color_chooser_button_virtual_set_color(color_button, &color);
326 /*g_object_unref(G_OBJECT(dialog));*/
327 gtk_widget_destroy(dialog);
331 /* signal handlers */
332 static gboolean hildon_color_chooser_button_area_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data)
334 HildonColorChooserButton *button = HILDON_COLOR_CHOOSER_BUTTON(data);
335 GtkWidget *button_widget = GTK_WIDGET(data);
338 if(button->style_info.inner.left > 0 || button->style_info.inner.right > 0 ||
339 button->style_info.inner.top > 0 || button->style_info.inner.bottom > 0) {
340 gtk_paint_box(gtk_widget_get_style(button_widget), widget->window, GTK_WIDGET_STATE(button_widget), GTK_SHADOW_NONE,
341 &event->area, button_widget, "color-button", 0, 0, widget->allocation.width, widget->allocation.height);
345 gdk_draw_rectangle(widget->window, button->color_gc, TRUE, event->area.x, event->area.y, event->area.width, event->area.height);
352 /* additional use-only-here functions */
353 static void hildon_color_chooser_button_helper_get_style_info(HildonColorChooserButton *button)
355 GtkBorder *in, *out, *min;
357 gtk_widget_style_get(GTK_WIDGET(button), "inner_border", &in,
358 "outer_border", &out,
359 "minimum_size", &min, NULL);
363 button->style_info.inner = *in;
366 button->style_info.inner.left = 0;
367 button->style_info.inner.right = 0;
368 button->style_info.inner.top = 0;
369 button->style_info.inner.bottom = 0;
373 button->style_info.outer = *out;
376 button->style_info.outer.left = 4;
377 button->style_info.outer.right = 4;
378 button->style_info.outer.top = 4;
379 button->style_info.outer.bottom = 4;
383 button->style_info.min = *min;
386 button->style_info.min.left = 8;
387 button->style_info.min.right = 8;
388 button->style_info.min.top = 0;
389 button->style_info.min.bottom = 0;
395 GtkWidget *hildon_color_chooser_button_new()
397 return gtk_type_new(HILDON_TYPE_COLOR_CHOOSER_BUTTON);
401 void hildon_color_chooser_button_set_color(HildonColorChooserButton *button, GdkColor *color)
403 HILDON_COLOR_CHOOSER_BUTTON_CLASS(G_OBJECT_GET_CLASS(button))->set_color(button, color);
406 void hildon_color_chooser_button_get_color(HildonColorChooserButton *button, GdkColor *color)
408 *color = button->color;