2006-11-08 Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
[hildon] / hildon-widgets / hildon-color-chooser.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.
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
29  * @short_description: A widget to choose a color.
30  * @see_also: #HildonColorChooserDialog, #HildonColorButton
31  *
32  * HildonColorChooser is a widget to choose a color.
33  */
34 #include <gtk/gtk.h>
35
36
37 #include "hildon-color-chooser.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_signals[LAST_SIGNAL] = { 0 };
52
53
54 static void hildon_color_chooser_init(HildonColorChooser *sel);
55 static void hildon_color_chooser_class_init(HildonColorChooserClass *klass);
56
57
58 GtkType hildon_color_chooser_get_type ()
59 {
60   static GtkType chooser_type = 0;
61
62   if (!chooser_type)
63     {
64       static const GtkTypeInfo chooser_info =
65       {
66         "HildonColorChooser",
67         sizeof (HildonColorChooser),
68         sizeof (HildonColorChooserClass),
69         (GtkClassInitFunc) hildon_color_chooser_class_init,
70         (GtkObjectInitFunc) hildon_color_chooser_init,
71         /* reserved_1 */ NULL,
72         /* reserved_1 */ NULL,
73         (GtkClassInitFunc) NULL
74       };
75
76       chooser_type = gtk_type_unique (GTK_TYPE_WIDGET, &chooser_info);
77     }
78
79   return chooser_type;
80 }
81
82
83 static void hildon_color_chooser_init(HildonColorChooser *sel)
84 {
85   sel->color.red   = 0x0000;
86   sel->color.green = 0x0000;
87   sel->color.blue  = 0x0000;
88   sel->color.pixel = 0x00000000;
89 }
90
91 static void hildon_color_chooser_class_init(HildonColorChooserClass *klass)
92 {
93   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
94   GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass);
95
96   klass->set_color = NULL;
97
98
99   gtk_widget_class_install_style_property(widget_class,
100                                           g_param_spec_boxed("outer_border",
101                                                              "Outer border",
102                                                              "Size of outer border",
103                                                              GTK_TYPE_BORDER,
104                                                              G_PARAM_READABLE));
105
106   color_chooser_signals[COLOR_CHANGED] = g_signal_new("color-changed", G_OBJECT_CLASS_TYPE (object_class),
107                                                       G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (HildonColorChooserClass, color_changed),
108                                                       NULL, NULL, g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1, GDK_TYPE_COLOR);
109 }
110
111 /**
112  * hildon_color_chooser_new:
113  *
114  * Creates a new color chooser widget. The dialog is created through
115  * HildonPluginWidget API and is loaded from plugin. The initially selected
116  * color can be anything, so it's recommended to call
117  * hildon_color_chooser_dialog_set_color () after creating the widget.
118  *
119  * Returns: a new color chooser widget
120  */
121 GtkWidget *hildon_color_chooser_new()
122 {
123   if(!global_plugin) {
124     global_plugin = hildon_plugin_info_initialize(HILDON_TYPE_COLOR_CHOOSER, NULL);
125   }
126
127
128   return hildon_plugin_info_construct_widget(global_plugin);
129 }
130
131 /**
132  * hildon_color_chooser_set_color:
133  * @chooser: a #HildonColorChooser
134  * @color: a color to be set
135  *
136  * Sets the color selected in the widget.
137  */
138 void hildon_color_chooser_set_color(HildonColorChooser *chooser, GdkColor *color)
139 {
140   HildonColorChooserClass *klass = HILDON_COLOR_CHOOSER_CLASS(G_OBJECT_GET_CLASS(chooser));
141
142
143   chooser->color = *color;
144
145   if(klass->set_color) {
146     klass->set_color(chooser, color);
147   }
148 }
149
150 /**
151  * hildon_color_chooser_get_color:
152  * @chooser: a #HildonColorChooser
153  * @color: a pointer to #GdkColor to be filled by the function
154  *
155  * Gets the color selected in the widget.
156  */
157 void hildon_color_chooser_get_color(HildonColorChooser *chooser, GdkColor *color)
158 {
159   *color = chooser->color;
160 }
161
162
163 void hildon_color_chooser_emit_color_changed(HildonColorChooser *chooser)
164 {
165   g_signal_emit(chooser, color_chooser_signals[COLOR_CHANGED], 0, &chooser->color);
166 }