File: gtkhtml-color-swatch.c

package info (click to toggle)
gtkhtml4.0 4.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,244 kB
  • ctags: 6,515
  • sloc: ansic: 68,248; sh: 11,047; makefile: 492; xml: 25
file content (323 lines) | stat: -rw-r--r-- 8,248 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gtkhtml-color-swatch.c
 *
 * Copyright (C) 2008 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gtkhtml-color-swatch.h"

#include <glib/gi18n-lib.h>

enum {
	PROP_0,
	PROP_COLOR,
	PROP_SHADOW_TYPE
};

struct _GtkhtmlColorSwatchPrivate {
	GtkWidget *drawing_area;
	GtkWidget *frame;
};

static gpointer parent_class;

static gboolean
color_swatch_draw_cb (GtkWidget *drawing_area,
                      cairo_t *cr)
{
	GtkStyleContext *style_context;
	GdkRGBA rgba;
	GdkRectangle rect;

	style_context = gtk_widget_get_style_context (drawing_area);
	if (!style_context)
		return FALSE;

	gtk_style_context_get_background_color (style_context, GTK_STATE_FLAG_NORMAL, &rgba);

	gdk_cairo_get_clip_rectangle (cr, &rect);
	gdk_cairo_set_source_rgba (cr, &rgba);
	gdk_cairo_rectangle (cr, &rect);
	cairo_fill (cr);

	return FALSE;
}

static void
color_swatch_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_COLOR:
			gtkhtml_color_swatch_set_color (
				GTKHTML_COLOR_SWATCH (object),
				g_value_get_boxed (value));
			return;

		case PROP_SHADOW_TYPE:
			gtkhtml_color_swatch_set_shadow_type (
				GTKHTML_COLOR_SWATCH (object),
				g_value_get_enum (value));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
color_swatch_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
	GdkColor color;

	switch (property_id) {
		case PROP_COLOR:
			gtkhtml_color_swatch_get_color (
				GTKHTML_COLOR_SWATCH (object), &color);
			g_value_set_boxed (value, &color);
			return;

		case PROP_SHADOW_TYPE:
			g_value_set_enum (
				value, gtkhtml_color_swatch_get_shadow_type (
				GTKHTML_COLOR_SWATCH (object)));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
color_swatch_dispose (GObject *object)
{
	GtkhtmlColorSwatch *swatch = GTKHTML_COLOR_SWATCH (object);

	if (swatch->priv->drawing_area != NULL) {
		g_object_unref (swatch->priv->drawing_area);
		swatch->priv->drawing_area = NULL;
	}

	if (swatch->priv->frame != NULL) {
		g_object_unref (swatch->priv->frame);
		swatch->priv->frame = NULL;
	}

	/* Chain up to parent's dispose() method. */
	G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
color_swatch_get_preferred_width (GtkWidget *widget,
                                  gint *minimum_width,
                                  gint *natural_width)
{
	GtkhtmlColorSwatchPrivate *priv;

	priv = GTKHTML_COLOR_SWATCH (widget)->priv;

	gtk_widget_get_preferred_width (
		priv->frame, minimum_width, natural_width);
}

static void
color_swatch_get_preferred_height (GtkWidget *widget,
                                   gint *minimum_height,
                                   gint *natural_height)
{
	GtkhtmlColorSwatchPrivate *priv;

	priv = GTKHTML_COLOR_SWATCH (widget)->priv;

	gtk_widget_get_preferred_height (
		priv->frame, minimum_height, natural_height);
}

static void
color_swatch_size_allocate (GtkWidget *widget,
                            GtkAllocation *allocation)
{
	GtkhtmlColorSwatchPrivate *priv;

	priv = GTKHTML_COLOR_SWATCH (widget)->priv;

	gtk_widget_set_allocation (widget, allocation);
	gtk_widget_size_allocate (priv->frame, allocation);
}

static void
color_swatch_class_init (GtkhtmlColorSwatchClass *class)
{
	GObjectClass *object_class;
	GtkWidgetClass *widget_class;

	parent_class = g_type_class_peek_parent (class);
	g_type_class_add_private (class, sizeof (GtkhtmlColorSwatchPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->set_property = color_swatch_set_property;
	object_class->get_property = color_swatch_get_property;
	object_class->dispose = color_swatch_dispose;

	widget_class = GTK_WIDGET_CLASS (class);
	widget_class->get_preferred_width = color_swatch_get_preferred_width;
	widget_class->get_preferred_height = color_swatch_get_preferred_height;
	widget_class->size_allocate = color_swatch_size_allocate;

	g_object_class_install_property (
		object_class,
		PROP_COLOR,
		g_param_spec_boxed (
			"color",
			"Color",
			"The current color",
			GDK_TYPE_COLOR,
			G_PARAM_READWRITE));

	g_object_class_install_property (
		object_class,
		PROP_SHADOW_TYPE,
		g_param_spec_enum (
			"shadow-type",
			"Frame Shadow",
			"Appearance of the frame border",
			GTK_TYPE_SHADOW_TYPE,
			GTK_SHADOW_ETCHED_IN,
			G_PARAM_READWRITE));
}

static void
color_swatch_init (GtkhtmlColorSwatch *swatch)
{
	GtkWidget *container;
	GtkWidget *widget;

	swatch->priv = G_TYPE_INSTANCE_GET_PRIVATE (
		swatch, GTKHTML_TYPE_COLOR_SWATCH, GtkhtmlColorSwatchPrivate);

	widget = gtk_frame_new (NULL);
	gtk_container_add (GTK_CONTAINER (swatch), widget);
	swatch->priv->frame = g_object_ref (widget);
	gtk_widget_show (widget);

	container = widget;

	widget = gtk_drawing_area_new ();
	gtk_widget_set_size_request (widget, 16, 16);
	gtk_container_add (GTK_CONTAINER (container), widget);
	swatch->priv->drawing_area = g_object_ref (widget);
	gtk_widget_show (widget);

	g_signal_connect (
		widget, "draw",
		G_CALLBACK (color_swatch_draw_cb), swatch);
}

GType
gtkhtml_color_swatch_get_type (void)
{
	static GType type = 0;

	if (G_UNLIKELY (type == 0)) {
		static const GTypeInfo type_info = {
			sizeof (GtkhtmlColorSwatchClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) color_swatch_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,  /* class_data */
			sizeof (GtkhtmlColorSwatch),
			0,     /* n_preallocs */
			(GInstanceInitFunc) color_swatch_init,
			NULL   /* value_table */
		};

		type = g_type_register_static (
			GTK_TYPE_BIN, "GtkhtmlColorSwatch", &type_info, 0);
	}

	return type;
}

GtkWidget *
gtkhtml_color_swatch_new (void)
{
	return g_object_new (GTKHTML_TYPE_COLOR_SWATCH, NULL);
}

void
gtkhtml_color_swatch_get_color (GtkhtmlColorSwatch *swatch,
                                GdkColor *color)
{
	GtkStyleContext *style_context;
	GtkWidget *drawing_area;
	GdkRGBA rgba;

	g_return_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch));
	g_return_if_fail (color != NULL);

	drawing_area = swatch->priv->drawing_area;
	style_context = gtk_widget_get_style_context (drawing_area);
	gtk_style_context_get_background_color (style_context, GTK_STATE_FLAG_NORMAL, &rgba);

	color->red   = rgba.red * 65535;
	color->green = rgba.green * 65535;
	color->blue  = rgba.blue * 65535;
}

void
gtkhtml_color_swatch_set_color (GtkhtmlColorSwatch *swatch,
                                const GdkColor *color)
{
	GtkWidget *drawing_area;

	g_return_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch));

	drawing_area = swatch->priv->drawing_area;
	gtk_widget_modify_bg (drawing_area, GTK_STATE_NORMAL, color);

	g_object_notify (G_OBJECT (swatch), "color");
}

GtkShadowType
gtkhtml_color_swatch_get_shadow_type (GtkhtmlColorSwatch *swatch)
{
	GtkFrame *frame;

	g_return_val_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch), 0);

	frame = GTK_FRAME (swatch->priv->frame);

	return gtk_frame_get_shadow_type (frame);
}

void
gtkhtml_color_swatch_set_shadow_type (GtkhtmlColorSwatch *swatch,
                                      GtkShadowType shadow_type)
{
	GtkFrame *frame;

	g_return_if_fail (GTKHTML_IS_COLOR_SWATCH (swatch));

	frame = GTK_FRAME (swatch->priv->frame);
	gtk_frame_set_shadow_type (frame, shadow_type);

	g_object_notify (G_OBJECT (swatch), "shadow-type");
}