Fix compiler warnings for hildon-extras dialogs
[maepad.git] / src / he / he-simple-color-dialog.c
blob8bf4883aac4bd7944ec71085e5e0668677f48859
1 /*
2 * This file is a part of hildon-extras
4 * Copyright (C) 2010 Thomas Perl
5 * Copyright (C) 2005, 2008 Nokia Corporation.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /**
23 * SECTION:he-simple_color-dialog
24 * @short_description: An simple_color dialog for Hildon-based applications
26 * #HeSimpleColorDialog works as a nice default simple_color dialog for Maemo apps
29 #define _GNU_SOURCE /* needed for GNU nl_langinfo_l */
30 #define __USE_GNU /* needed for locale */
32 #include <locale.h>
34 #include <string.h>
35 #include <stdlib.h>
37 #include <libintl.h>
38 #include <langinfo.h>
40 #include <hildon/hildon.h>
42 #include <gdk/gdk.h>
44 #include "he-simple-color-dialog.h"
46 #define PALETTE_SIZE 21
47 #define PALETTE_CUSTOM_COLOR (PALETTE_SIZE-1)
49 #define ICON_SIZE 50
51 static const gchar* DEFAULT_PALETTE[PALETTE_SIZE] = {
52 "#fcaf3e", "#f57900", "#ce5c00", /* orange */
53 "#8ae234", "#73d215", "#4e9a06", /* green */
54 "#729fcf", "#3465a4", "#204a87", /* blue */
55 "#ad7fa8", "#75507b", "#5c3566", /* purple */
56 "#ef2929", "#cc0000", "#a40000", /* red */
57 "#888a85", "#555753", "#2e3436", /* grey */
58 "#ffffff", "#000000", "#ffffff", /* white, black, custom */
61 #define HE_SIMPLE_COLOR_DIALOG_GET_PRIVATE(obj) \
62 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HE_TYPE_SIMPLE_COLOR_DIALOG, HeSimpleColorDialogPrivate))
64 #define _(String) dgettext("hildon-libs", String)
66 struct _HeSimpleColorDialogPrivate
68 /* Widgets */
69 GtkWidget* table_layout;
70 GtkWidget* buttons[PALETTE_SIZE];
72 /* Data */
73 GdkColor palette[PALETTE_SIZE];
74 guint color_index;
77 static GObject * he_simple_color_dialog_constructor (GType type,
78 guint n_construct_properties,
79 GObjectConstructParam *construct_properties);
80 static void he_simple_color_dialog_finalize (GObject * object);
82 void he_simple_color_dialog_response (GtkDialog *dialog, gint response_id, HeSimpleColorDialog *fd);
83 static void he_simple_color_dialog_class_init (HeSimpleColorDialogClass * class);
84 static void he_simple_color_dialog_init (HeSimpleColorDialog * fd);
85 static void he_simple_color_dialog_show (GtkWidget *widget);
86 static void he_simple_color_dialog_destroy (GtkObject *object);
88 static gpointer parent_class = NULL;
90 GType G_GNUC_CONST
91 he_simple_color_dialog_get_type (void)
93 static GType dialog_type = 0;
94 dialog_type = g_type_from_name ("HeSimpleColorDialog");
96 if (!dialog_type) {
97 static const GTypeInfo dialog_info =
99 sizeof (HeSimpleColorDialogClass),
100 NULL,
101 NULL,
102 (GClassInitFunc) he_simple_color_dialog_class_init,
103 NULL,
104 NULL,
105 sizeof (HeSimpleColorDialog),
107 (GInstanceInitFunc) he_simple_color_dialog_init,
108 NULL
111 dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
112 "HeSimpleColorDialog", &dialog_info, 0);
115 return dialog_type;
118 static void
119 he_simple_color_dialog_class_init (HeSimpleColorDialogClass * class)
121 GObjectClass *gobject_class;
122 GtkObjectClass *object_class;
123 GtkWidgetClass *widget_class;
125 gobject_class = (GObjectClass *) class;
126 object_class = (GtkObjectClass *) class;
127 widget_class = (GtkWidgetClass *) class;
129 parent_class = g_type_class_peek_parent (class);
131 g_type_class_add_private (object_class, sizeof (HeSimpleColorDialogPrivate));
134 static void
135 he_simple_color_dialog_show (GtkWidget *widget)
137 /*HeSimpleColorDialogPrivate *priv = HE_SIMPLE_COLOR_DIALOG_GET_PRIVATE (widget);*/
138 GTK_WIDGET_CLASS (parent_class)->show (widget);
141 static void
142 he_simple_color_dialog_destroy (GtkObject *object)
144 /*HeSimpleColorDialogPrivate *priv = HE_SIMPLE_COLOR_DIALOG_GET_PRIVATE (object);*/
145 GTK_OBJECT_CLASS (parent_class)->destroy (object);
148 static void
149 _he_simple_color_dialog_toggled(GtkToggleButton* toggle_button, gpointer user_data)
151 HeSimpleColorDialog* scd = HE_SIMPLE_COLOR_DIALOG(user_data);
152 g_return_if_fail (HE_IS_SIMPLE_COLOR_DIALOG(scd));
154 guint button_index = (guint)g_object_get_data(G_OBJECT(toggle_button), "color-index");
156 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle_button))) {
157 if (scd->priv->color_index != button_index) {
158 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(scd->priv->buttons[scd->priv->color_index]), FALSE);
159 scd->priv->color_index = button_index;
164 static GtkWidget*
165 _he_simple_color_dialog_create_color_widget(GdkColor* color)
167 GdkPixmap* pixmap = gdk_pixmap_new(NULL, ICON_SIZE, ICON_SIZE, 16);
169 cairo_t* cr = gdk_cairo_create(pixmap);
170 gdk_cairo_set_source_color(cr, color);
172 cairo_rectangle(cr, 0, 0, ICON_SIZE, ICON_SIZE);
173 cairo_fill(cr);
174 cairo_destroy(cr);
176 GtkWidget* result = gtk_image_new_from_pixmap(pixmap, NULL);
177 gdk_pixmap_unref(pixmap);
179 return result;
182 static void
183 he_simple_color_dialog_init (HeSimpleColorDialog *ad)
185 ad->priv = HE_SIMPLE_COLOR_DIALOG_GET_PRIVATE (ad);
186 guint i;
188 ad->priv->color_index = 0;
190 /* Parse default colors */
191 for (i=0; i<PALETTE_SIZE; i++) {
192 if (!gdk_color_parse(DEFAULT_PALETTE[i], &(ad->priv->palette[i]))) {
193 g_error("Cannot parse color from default palette: %s", DEFAULT_PALETTE[i]);
197 gtk_window_set_title(GTK_WINDOW(ad), _("Select a color"));
199 /* Create table and insert color chooser buttons */
200 GtkTable* t = GTK_TABLE(gtk_table_new(PALETTE_SIZE/3, 3, FALSE));
202 for (i=0; i<PALETTE_SIZE; i++) {
203 ad->priv->buttons[i] = hildon_gtk_toggle_button_new(HILDON_SIZE_THUMB_HEIGHT);
204 gtk_widget_set_size_request(GTK_WIDGET(ad->priv->buttons[i]), 80, 80);
205 gtk_container_add(GTK_CONTAINER(ad->priv->buttons[i]), _he_simple_color_dialog_create_color_widget(&(ad->priv->palette[i])));
206 g_object_set_data(G_OBJECT(ad->priv->buttons[i]), "color-index", (gpointer)i);
207 g_signal_connect(G_OBJECT(ad->priv->buttons[i]), "toggled", G_CALLBACK(_he_simple_color_dialog_toggled), ad);
208 /* Set color image on button */
209 gtk_table_attach(GTK_TABLE(t), ad->priv->buttons[i],
210 i/3, i/3+1, /* xattach */
211 i%3, i%3+1, /* yattach */
212 GTK_EXPAND | GTK_FILL, /* xoptions */
213 GTK_EXPAND | GTK_FILL, /* yoptions */
214 0, /* xpadding */
215 0 /* ypadding */);
218 ad->priv->table_layout = GTK_WIDGET(t);
219 GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(ad));
220 gtk_container_add(GTK_CONTAINER(content_area), GTK_WIDGET(t));
222 gtk_dialog_add_button(GTK_DIALOG(ad), _("Select"), GTK_RESPONSE_OK);
224 /* By default, toggle the first button */
225 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ad->priv->buttons[0]), TRUE);
227 gtk_widget_show_all(GTK_WIDGET(ad));
229 /* Hide the "custom color" button */
230 gtk_widget_hide(GTK_WIDGET(ad->priv->buttons[PALETTE_CUSTOM_COLOR]));
232 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(ad), GTK_NO_WINDOW);
233 gtk_widget_set_redraw_on_allocate(GTK_WIDGET(ad), FALSE);
236 static void
237 he_simple_color_dialog_finalize (GObject * object)
239 /*HeSimpleColorDialogPrivate *priv = HE_SIMPLE_COLOR_DIALOG_GET_PRIVATE (object);*/
241 if (G_OBJECT_CLASS (parent_class)->finalize)
242 G_OBJECT_CLASS (parent_class)->finalize (object);
245 /* ------------------------------ PRIVATE METHODS ---------------------------- */
247 /* ------------------------------ PUBLIC METHODS ---------------------------- */
249 GtkWidget*
250 he_simple_color_dialog_new()
252 return g_object_new(HE_TYPE_SIMPLE_COLOR_DIALOG, NULL);
256 * Sets the currently-selected color of this color dialog.
257 * The color is copied to an internal structure, so the
258 * caller can free the GdkColor passed to this object after
259 * the function call.
261 void
262 he_simple_color_dialog_set_color(HeSimpleColorDialog* scd, GdkColor* color)
264 g_return_if_fail (HE_IS_SIMPLE_COLOR_DIALOG(scd));
265 guint i;
267 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(scd->priv->buttons[scd->priv->color_index]), FALSE);
269 /* Try to find the color in the existing palette */
270 for (i=0; i<PALETTE_SIZE; i++) {
271 if (gdk_color_equal(color, &(scd->priv->palette[i]))) {
272 scd->priv->color_index = i;
273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(scd->priv->buttons[scd->priv->color_index]), TRUE);
274 return;
278 /* Replace the custom color field with the new color */
279 scd->priv->color_index = PALETTE_CUSTOM_COLOR;
280 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(scd->priv->buttons[scd->priv->color_index]), TRUE);
281 scd->priv->palette[scd->priv->color_index] = *color;
283 /* Update button image */
284 GtkWidget* old_child = gtk_bin_get_child(GTK_BIN(scd->priv->buttons[scd->priv->color_index]));
285 gtk_widget_destroy(old_child);
286 gtk_container_add(GTK_CONTAINER(scd->priv->buttons[scd->priv->color_index]), _he_simple_color_dialog_create_color_widget(color));
288 /* Show the custom button now that we have a color set */
289 gtk_widget_show_all(GTK_WIDGET(scd->priv->buttons[scd->priv->color_index]));
293 * Returns the currently-selected color. The caller has to
294 * free the returned value using gdk_color_free.
296 GdkColor*
297 he_simple_color_dialog_get_color(HeSimpleColorDialog* scd)
299 g_assert(HE_IS_SIMPLE_COLOR_DIALOG(scd));
300 return gdk_color_copy(&(scd->priv->palette[scd->priv->color_index]));