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>
31 struct _GtkhtmlColorSwatchPrivate
{
32 GtkWidget
*drawing_area
;
36 static gpointer parent_class
;
39 color_swatch_draw_cb (GtkWidget
*drawing_area
,
42 GtkStyleContext
*style_context
;
46 style_context
= gtk_widget_get_style_context (drawing_area
);
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
);
61 color_swatch_set_property (GObject
*object
,
66 switch (property_id
) {
68 gtkhtml_color_swatch_set_color (
69 GTKHTML_COLOR_SWATCH (object
),
70 g_value_get_boxed (value
));
73 case PROP_SHADOW_TYPE
:
74 gtkhtml_color_swatch_set_shadow_type (
75 GTKHTML_COLOR_SWATCH (object
),
76 g_value_get_enum (value
));
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
84 color_swatch_get_property (GObject
*object
,
91 switch (property_id
) {
93 gtkhtml_color_swatch_get_color (
94 GTKHTML_COLOR_SWATCH (object
), &color
);
95 g_value_set_boxed (value
, &color
);
98 case PROP_SHADOW_TYPE
:
100 value
, gtkhtml_color_swatch_get_shadow_type (
101 GTKHTML_COLOR_SWATCH (object
)));
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
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
);
128 color_swatch_get_preferred_width (GtkWidget
*widget
,
132 GtkhtmlColorSwatchPrivate
*priv
;
134 priv
= GTKHTML_COLOR_SWATCH (widget
)->priv
;
136 gtk_widget_get_preferred_width (
137 priv
->frame
, minimum_width
, natural_width
);
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
);
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
);
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 (
194 g_object_class_install_property (
200 "Appearance of the frame border",
201 GTK_TYPE_SHADOW_TYPE
,
202 GTK_SHADOW_ETCHED_IN
,
207 color_swatch_init (GtkhtmlColorSwatch
*swatch
)
209 GtkWidget
*container
;
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
);
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
);
230 G_CALLBACK (color_swatch_draw_cb
), swatch
);
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
),
248 (GInstanceInitFunc
) color_swatch_init
,
249 NULL
/* value_table */
252 type
= g_type_register_static (
253 GTK_TYPE_BIN
, "GtkhtmlColorSwatch", &type_info
, 0);
260 gtkhtml_color_swatch_new (void)
262 return g_object_new (GTKHTML_TYPE_COLOR_SWATCH
, NULL
);
266 gtkhtml_color_swatch_get_color (GtkhtmlColorSwatch
*swatch
,
269 GtkStyleContext
*style_context
;
270 GtkWidget
*drawing_area
;
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;
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");
300 gtkhtml_color_swatch_get_shadow_type (GtkhtmlColorSwatch
*swatch
)
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
);
312 gtkhtml_color_swatch_set_shadow_type (GtkhtmlColorSwatch
*swatch
,
313 GtkShadowType shadow_type
)
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");