2006-09-21 Tommi Komulainen <tommi.komulainen@nokia.com>
[hildon] / hildon-widgets / hildon-color-chooser-dialog.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Author: Kuisma Salonen <kuisma.salonen@nokia.com>
7  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
8  *
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
12  * the License or any later version.
13  *
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.
18  *
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
22  * 02110-1301 USA
23  *
24  */
25
26
27 /**
28  * SECTION:hildon-color-chooser-dialog
29  * @short_description: A dialog to choose a color.
30  * @see_also: #HildonColorButton, #HildonColorChooser
31  *
32  * HildonColorChooserDialog is a widget widget to choose a color.
33  */
34 #include <gtk/gtk.h>
35
36
37 #include "hildon-color-chooser-dialog.h"
38
39 #include "hildon-plugin-widget.h"
40
41
42 enum {
43   COLOR_CHANGED,
44   LAST_SIGNAL
45 };
46
47
48 static HildonPluginWidgetInfo *global_plugin = NULL;
49
50
51 static guint color_chooser_dialog_signals[LAST_SIGNAL] = { 0 };
52
53
54 static void hildon_color_chooser_dialog_init(HildonColorChooserDialog *object);
55 static void hildon_color_chooser_dialog_class_init(HildonColorChooserDialogClass *klass);
56
57
58 GtkType hildon_color_chooser_dialog_get_type ()
59 {
60   static GtkType chooser_type = 0;
61
62   if (!chooser_type)
63     {
64       static const GtkTypeInfo chooser_info =
65       {
66         "HildonColorChooserDialog",
67         sizeof (HildonColorChooserDialog),
68         sizeof (HildonColorChooserDialogClass),
69         (GtkClassInitFunc) hildon_color_chooser_dialog_class_init,
70         (GtkObjectInitFunc) hildon_color_chooser_dialog_init,
71         /* reserved_1 */ NULL,
72         /* reserved_1 */ NULL,
73         (GtkClassInitFunc) NULL
74       };
75
76       chooser_type = gtk_type_unique (GTK_TYPE_DIALOG, &chooser_info);
77     }
78
79   return chooser_type;
80 }
81
82
83 static void hildon_color_chooser_dialog_init(HildonColorChooserDialog *object)
84 {
85   int i;
86
87
88   object->color.red   = 0x0000;
89   object->color.green = 0x0000;
90   object->color.blue  = 0x0000;
91   object->color.pixel = 0x00000000;
92
93
94   for(i = 0; i < 32; i++) {
95     object->reserved[i] = 0;
96   }
97 }
98
99 static void hildon_color_chooser_dialog_class_init(HildonColorChooserDialogClass *klass)
100 {
101   GtkObjectClass *object_klass = GTK_OBJECT_CLASS(klass);
102   int i;
103
104
105   for(i = 0; i < 32; i++) {
106     klass->reserved[i] = 0;
107   }
108
109   klass->set_color = 0;
110
111
112   color_chooser_dialog_signals[COLOR_CHANGED] = g_signal_new("color-changed", G_OBJECT_CLASS_TYPE (object_klass),
113                                                              G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (HildonColorChooserDialogClass, color_changed),
114                                                              NULL, NULL, g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1, GDK_TYPE_COLOR);
115 }
116
117 /**
118  * hildon_color_chooser_dialog_new:
119  *
120  * Creates a new color chooser dialog. The dialog is created through
121  * HildonPluginWidget API and is loaded from plugin. The initially selected
122  * color can be anything, so it's recommended to call
123  * hildon_color_chooser_dialog_set_color () after creating the widget.
124  *
125  * Returns: a new color chooser dialog
126  */
127 GtkWidget *hildon_color_chooser_dialog_new()
128 {
129   if(!global_plugin) {
130     global_plugin = hildon_plugin_info_initialize(HILDON_TYPE_COLOR_CHOOSER_DIALOG, NULL);
131     g_return_val_if_fail (global_plugin != NULL, NULL);
132   }
133
134   return hildon_plugin_info_construct_widget(global_plugin);
135 }
136
137 /**
138  * hildon_color_chooser_dialog_set_color:
139  * @chooser: a #HildonColorChooserDialog
140  * @color: a color to be set
141  *
142  * Sets the color selected in the dialog.
143  */
144 void hildon_color_chooser_dialog_set_color(HildonColorChooserDialog *chooser, GdkColor *color)
145 {
146   HildonColorChooserDialogClass *klass = HILDON_COLOR_CHOOSER_DIALOG_CLASS(G_OBJECT_GET_CLASS(chooser));
147
148
149   chooser->color = *color;
150
151   if(klass->set_color) {
152     klass->set_color(chooser, color);
153   }
154 }
155
156 /**
157  * hildon_color_chooser_dialog_get_color:
158  * @chooser: a #HildonColorChooserDialog
159  * @color: a pointer to #GdkColor to be filled by the function
160  *
161  * Gets the color selected in the dialog.
162  */
163 void hildon_color_chooser_dialog_get_color(HildonColorChooserDialog *chooser, GdkColor *color)
164 {
165   *color = chooser->color;
166 }
167
168
169 void hildon_color_chooser_dialog_emit_color_changed(HildonColorChooserDialog *chooser)
170 {
171   g_signal_emit(chooser, color_chooser_dialog_signals[COLOR_CHANGED], 0, &chooser->color);
172 }