]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorchooser.c
Add docs
[~andy/gtk] / gtk / gtkcolorchooser.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2012, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "gtkcolorchooser.h"
24 #include "gtkcolorchooserprivate.h"
25 #include "gtkintl.h"
26 #include "gtktypebuiltins.h"
27 #include "gtkprivate.h"
28
29 /**
30  * SECTION:gtkcolorchooser
31  * @Short_description: Interface implemented by widgets for choosing colors
32  * @Title: GtkColorChooser
33  * @See_also: #GtkColorChooserDialog, #GtkColorChooserWidget, #GtkColorButton
34  *
35  * #GtkColorChooser is an interface that is implemented by widgets
36  * for choosing colors. Depending on the situation, colors may be
37  * allowed to have alpha (translucency).
38  *
39  * In GTK+, the main widgets that implement this interface are
40  * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
41  *
42  * Since: 3.4
43  */
44
45 enum
46 {
47   COLOR_ACTIVATED,
48   LAST_SIGNAL
49 };
50
51 static guint signals[LAST_SIGNAL];
52
53 G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);
54
55 static void
56 gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
57 {
58   /**
59    * GtkColorChooser:rgba:
60    *
61    * The ::rgba property contains the currently selected color,
62    * as a #GdkRGBA struct. The property can be set to change
63    * the current selection programmatically.
64    *
65    * Since: 3.4
66    */
67   g_object_interface_install_property (iface,
68       g_param_spec_boxed ("rgba",
69                           P_("Color"),
70                           P_("Current color, as a GdkRGBA"),
71                           GDK_TYPE_RGBA,
72                           GTK_PARAM_READWRITE));
73
74   /**
75    * GtkColorChooser:use-alpha:
76    *
77    * When ::use-alpha is %TRUE, colors may have alpha (translucency)
78    * information. When it is %FALSE, the #GdkRGBA struct obtained
79    * via the #GtkColorChooser:rgba property will be forced to have
80    * alpha == 1.
81    *
82    * Implementations are expected to show alpha by rendering the color
83    * over a non-uniform background (like a checkerboard pattern).
84    *
85    * Since: 3.4
86    */
87   g_object_interface_install_property (iface,
88       g_param_spec_boolean ("use-alpha",
89                             P_("Use alpha"),
90                             P_("Whether alpha should be shown"),
91                             TRUE,
92                             GTK_PARAM_READWRITE));
93
94   /**
95    * GtkColorChooser::color-activated:
96    * @chooser: the object which received the signal
97    * @color: the color
98    *
99    * Emitted when a color is activated from the color chooser.
100    * This usually happens when the user clicks a color swatch,
101    * or a color is selected and the user presses one of the keys
102    * Space, Shift+Space, Return or Enter.
103    *
104    * Since: 3.4
105    */
106   signals[COLOR_ACTIVATED] =
107     g_signal_new ("color-activated",
108                   GTK_TYPE_COLOR_CHOOSER,
109                   G_SIGNAL_RUN_FIRST,
110                   G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
111                   NULL, NULL,
112                   NULL,
113                   G_TYPE_NONE,
114                   1, G_TYPE_STRING);
115 }
116
117 /**
118  * gtk_color_chooser_get_rgba:
119  * @chooser: a #GtkColorChooser
120  * @color: return location for the color
121  *
122  * Gets the currently-selected color.
123  *
124  * Since: 3.4
125  */
126 void
127 gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
128                             GdkRGBA         *color)
129 {
130   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
131
132   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
133 }
134
135 /**
136  * gtk_color_chooser_set_rgba:
137  * @chooser: a #GtkColorChooser
138  * @color: the new color
139  *
140  * Sets the color.
141  */
142 void
143 gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
144                             const GdkRGBA   *color)
145 {
146   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
147   g_return_if_fail (color != NULL);
148
149   GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
150 }
151
152 void
153 _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
154                                     const GdkRGBA   *color)
155 {
156   g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
157 }
158
159 /**
160  * gtk_color_chooser_get_use_alpha:
161  * @chooser: a #GtkColorChooser
162  *
163  * Returns whether the color chooser shows the alpha channel.
164  *
165  * Returns: %TRUE if the color chooser uses the alpha channel,
166  *     %FALSE if not
167  *
168  * Since: 3.4
169  */
170 gboolean
171 gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser)
172 {
173   gboolean use_alpha;
174
175   g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);
176
177   g_object_get (chooser, "use-alpha", &use_alpha, NULL);
178
179   return use_alpha;
180 }
181
182 /**
183  * gtk_color_chooser_set_use_alpha:
184  * @chooser: a #GtkColorChooser
185  * @use_alpha: %TRUE if color chooser should use alpha channel, %FALSE if not
186  *
187  * Sets whether or not the color chooser should use the alpha channel.
188  *
189  * Since: 3.4
190  */
191 void
192 gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser,
193                                  gboolean         use_alpha)
194 {
195
196   g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
197
198   g_object_set (chooser, "use-alpha", use_alpha, NULL);
199 }