Updated Esperanto translation
[gtkhtml.git] / components / editor / gtkhtml-color-swatch.c
blobf484af6482ef06e4a687c98c5f74620da358888c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gtkhtml-color-swatch.c
4 * Copyright (C) 2008 Novell, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "gtkhtml-color-swatch.h"
23 #include <glib/gi18n-lib.h>
25 enum {
26 PROP_0,
27 PROP_COLOR,
28 PROP_SHADOW_TYPE
31 struct _GtkhtmlColorSwatchPrivate {
32 GtkWidget *drawing_area;
33 GtkWidget *frame;
36 static gpointer parent_class;
38 static gboolean
39 color_swatch_draw_cb (GtkWidget *drawing_area,
40 cairo_t *cr)
42 GtkStyleContext *style_context;
43 GdkRGBA rgba;
44 GdkRectangle rect;
46 style_context = gtk_widget_get_style_context (drawing_area);
47 if (!style_context)
48 return FALSE;
50 gtk_style_context_get_background_color (style_context, GTK_STATE_FLAG_NORMAL, &rgba);
52 gdk_cairo_get_clip_rectangle (cr, &rect);
53 gdk_cairo_set_source_rgba (cr, &rgba);
54 gdk_cairo_rectangle (cr, &rect);
55 cairo_fill (cr);
57 return FALSE;
60 static void
61 color_swatch_set_property (GObject *object,
62 guint property_id,
63 const GValue *value,
64 GParamSpec *pspec)
66 switch (property_id) {
67 case PROP_COLOR:
68 gtkhtml_color_swatch_set_color (
69 GTKHTML_COLOR_SWATCH (object),
70 g_value_get_boxed (value));
71 return;
73 case PROP_SHADOW_TYPE:
74 gtkhtml_color_swatch_set_shadow_type (
75 GTKHTML_COLOR_SWATCH (object),
76 g_value_get_enum (value));
77 return;
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83 static void
84 color_swatch_get_property (GObject *object,
85 guint property_id,
86 GValue *value,
87 GParamSpec *pspec)
89 GdkColor color;
91 switch (property_id) {
92 case PROP_COLOR:
93 gtkhtml_color_swatch_get_color (
94 GTKHTML_COLOR_SWATCH (object), &color);
95 g_value_set_boxed (value, &color);
96 return;
98 case PROP_SHADOW_TYPE:
99 g_value_set_enum (
100 value, gtkhtml_color_swatch_get_shadow_type (
101 GTKHTML_COLOR_SWATCH (object)));
102 return;
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
108 static void
109 color_swatch_dispose (GObject *object)
111 GtkhtmlColorSwatch *swatch = GTKHTML_COLOR_SWATCH (object);
113 if (swatch->priv->drawing_area != NULL) {
114 g_object_unref (swatch->priv->drawing_area);
115 swatch->priv->drawing_area = NULL;
118 if (swatch->priv->frame != NULL) {
119 g_object_unref (swatch->priv->frame);
120 swatch->priv->frame = NULL;
123 /* Chain up to parent's dispose() method. */
124 G_OBJECT_CLASS (parent_class)->dispose (object);
127 static void
128 color_swatch_get_preferred_width (GtkWidget *widget,
129 gint *minimum_width,
130 gint *natural_width)
132 GtkhtmlColorSwatchPrivate *priv;
134 priv = GTKHTML_COLOR_SWATCH (widget)->priv;
136 gtk_widget_get_preferred_width (
137 priv->frame, minimum_width, natural_width);
140 static void
141 color_swatch_get_preferred_height (GtkWidget *widget,
142 gint *minimum_height,
143 gint *natural_height)
145 GtkhtmlColorSwatchPrivate *priv;
147 priv = GTKHTML_COLOR_SWATCH (widget)->priv;
149 gtk_widget_get_preferred_height (
150 priv->frame, minimum_height, natural_height);
153 static void
154 color_swatch_size_allocate (GtkWidget *widget,
155 GtkAllocation *allocation)
157 GtkhtmlColorSwatchPrivate *priv;
159 priv = GTKHTML_COLOR_SWATCH (widget)->priv;
161 gtk_widget_set_allocation (widget, allocation);
162 gtk_widget_size_allocate (priv->frame, allocation);
165 static void
166 color_swatch_class_init (GtkhtmlColorSwatchClass *class)
168 GObjectClass *object_class;
169 GtkWidgetClass *widget_class;
171 parent_class = g_type_class_peek_parent (class);
172 g_type_class_add_private (class, sizeof (GtkhtmlColorSwatchPrivate));
174 object_class = G_OBJECT_CLASS (class);
175 object_class->set_property = color_swatch_set_property;
176 object_class->get_property = color_swatch_get_property;
177 object_class->dispose = color_swatch_dispose;
179 widget_class = GTK_WIDGET_CLASS (class);
180 widget_class->get_preferred_width = color_swatch_get_preferred_width;
181 widget_class->get_preferred_height = color_swatch_get_preferred_height;
182 widget_class->size_allocate = color_swatch_size_allocate;
184 g_object_class_install_property (
185 object_class,
186 PROP_COLOR,
187 g_param_spec_boxed (
188 "color",
189 "Color",
190 "The current color",
191 GDK_TYPE_COLOR,
192 G_PARAM_READWRITE));
194 g_object_class_install_property (
195 object_class,
196 PROP_SHADOW_TYPE,
197 g_param_spec_enum (
198 "shadow-type",
199 "Frame Shadow",
200 "Appearance of the frame border",
201 GTK_TYPE_SHADOW_TYPE,
202 GTK_SHADOW_ETCHED_IN,
203 G_PARAM_READWRITE));
206 static void
207 color_swatch_init (GtkhtmlColorSwatch *swatch)
209 GtkWidget *container;
210 GtkWidget *widget;
212 swatch->priv = G_TYPE_INSTANCE_GET_PRIVATE (
213 swatch, GTKHTML_TYPE_COLOR_SWATCH, GtkhtmlColorSwatchPrivate);
215 widget = gtk_frame_new (NULL);
216 gtk_container_add (GTK_CONTAINER (swatch), widget);
217 swatch->priv->frame = g_object_ref (widget);
218 gtk_widget_show (widget);
220 container = widget;
222 widget = gtk_drawing_area_new ();
223 gtk_widget_set_size_request (widget, 16, 16);
224 gtk_container_add (GTK_CONTAINER (container), widget);
225 swatch->priv->drawing_area = g_object_ref (widget);
226 gtk_widget_show (widget);
228 g_signal_connect (
229 widget, "draw",
230 G_CALLBACK (color_swatch_draw_cb), swatch);
233 GType
234 gtkhtml_color_swatch_get_type (void)
236 static GType type = 0;
238 if (G_UNLIKELY (type == 0)) {
239 static const GTypeInfo type_info = {
240 sizeof (GtkhtmlColorSwatchClass),
241 (GBaseInitFunc) NULL,
242 (GBaseFinalizeFunc) NULL,
243 (GClassInitFunc) color_swatch_class_init,
244 (GClassFinalizeFunc) NULL,
245 NULL, /* class_data */
246 sizeof (GtkhtmlColorSwatch),
247 0, /* n_preallocs */
248 (GInstanceInitFunc) color_swatch_init,
249 NULL /* value_table */
252 type = g_type_register_static (
253 GTK_TYPE_BIN, "GtkhtmlColorSwatch", &type_info, 0);
256 return type;
259 GtkWidget *
260 gtkhtml_color_swatch_new (void)
262 return g_object_new (GTKHTML_TYPE_COLOR_SWATCH, NULL);
265 void
266 gtkhtml_color_swatch_get_color (GtkhtmlColorSwatch *swatch,
267 GdkColor *color)
269 GtkStyleContext *style_context;
270 GtkWidget *drawing_area;
271 GdkRGBA rgba;
273 g_return_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch));
274 g_return_if_fail (color != NULL);
276 drawing_area = swatch->priv->drawing_area;
277 style_context = gtk_widget_get_style_context (drawing_area);
278 gtk_style_context_get_background_color (style_context, GTK_STATE_FLAG_NORMAL, &rgba);
280 color->red = rgba.red * 65535;
281 color->green = rgba.green * 65535;
282 color->blue = rgba.blue * 65535;
285 void
286 gtkhtml_color_swatch_set_color (GtkhtmlColorSwatch *swatch,
287 const GdkColor *color)
289 GtkWidget *drawing_area;
291 g_return_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch));
293 drawing_area = swatch->priv->drawing_area;
294 gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, color);
296 g_object_notify (G_OBJECT (swatch), "color");
299 GtkShadowType
300 gtkhtml_color_swatch_get_shadow_type (GtkhtmlColorSwatch *swatch)
302 GtkFrame *frame;
304 g_return_val_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch), 0);
306 frame = GTK_FRAME (swatch->priv->frame);
308 return gtk_frame_get_shadow_type (frame);
311 void
312 gtkhtml_color_swatch_set_shadow_type (GtkhtmlColorSwatch *swatch,
313 GtkShadowType shadow_type)
315 GtkFrame *frame;
317 g_return_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch));
319 frame = GTK_FRAME (swatch->priv->frame);
320 gtk_frame_set_shadow_type (frame, shadow_type);
322 g_object_notify (G_OBJECT (swatch), "shadow-type");