evolution-3.6.4/widgets/misc/e-canvas-background.c

No issues found

  1 /*
  2  * e-canvas-background.c - background color for canvas.
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Authors:
 19  *		Chris Lahey <clahey@ximian.com>
 20  *
 21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 22  *
 23  */
 24 
 25 #ifdef HAVE_CONFIG_H
 26 #include <config.h>
 27 #endif
 28 
 29 #include <math.h>
 30 #include <stdio.h>
 31 #include <string.h>
 32 
 33 #include <gdk/gdkkeysyms.h>
 34 #include <gtk/gtk.h>
 35 
 36 #include <glib/gi18n.h>
 37 #include "e-util/e-util.h"
 38 #include "misc/e-canvas.h"
 39 #include "misc/e-canvas-utils.h"
 40 
 41 #include "e-canvas-background.h"
 42 
 43 #define E_CANVAS_BACKGROUND_GET_PRIVATE(obj) \
 44 	(G_TYPE_INSTANCE_GET_PRIVATE \
 45 	((obj), E_CANVAS_BACKGROUND_TYPE, ECanvasBackgroundPrivate))
 46 
 47 /* workaround for avoiding API broken */
 48 #define ecb_get_type e_canvas_background_get_type
 49 G_DEFINE_TYPE (
 50 	ECanvasBackground,
 51 	ecb,
 52 	GNOME_TYPE_CANVAS_ITEM)
 53 
 54 #define d(x)
 55 
 56 struct _ECanvasBackgroundPrivate {
 57 	guint rgba;		/* Fill color, RGBA */
 58 };
 59 
 60 enum {
 61 	STYLE_SET,
 62 	LAST_SIGNAL
 63 };
 64 
 65 static guint ecb_signals[LAST_SIGNAL] = { 0, };
 66 
 67 enum {
 68 	PROP_0,
 69 	PROP_FILL_COLOR,
 70 	PROP_FILL_COLOR_GDK,
 71 	PROP_FILL_COLOR_RGBA,
 72 };
 73 
 74 static void
 75 ecb_bounds (GnomeCanvasItem *item,
 76             gdouble *x1,
 77             gdouble *y1,
 78             gdouble *x2,
 79             gdouble *y2)
 80 {
 81 	*x1 = -G_MAXDOUBLE;
 82 	*y1 = -G_MAXDOUBLE;
 83 	*x2 = G_MAXDOUBLE;
 84 	*y2 = G_MAXDOUBLE;
 85 }
 86 
 87 static void
 88 ecb_update (GnomeCanvasItem *item,
 89             const cairo_matrix_t *i2c,
 90             gint flags)
 91 {
 92 	gdouble x1, y1, x2, y2;
 93 
 94 	x1 = item->x1;
 95 	y1 = item->y1;
 96 	x2 = item->x2;
 97 	y2 = item->y2;
 98 
 99 	/* The bounds are all constants so we should only have to
100 	 * redraw once. */
101 	ecb_bounds (item, &item->x1, &item->y1, &item->x2, &item->y2);
102 
103 	if (item->x1 != x1 || item->y1 != y1 ||
104 	    item->x2 != x2 || item->y2 != y2)
105 		gnome_canvas_request_redraw (
106 			item->canvas, item->x1, item->y1, item->x2, item->y2);
107 }
108 
109 static void
110 ecb_set_property (GObject *object,
111                   guint property_id,
112                   const GValue *value,
113                   GParamSpec *pspec)
114 {
115 	ECanvasBackground *ecb;
116 
117 	GdkColor color = { 0, 0, 0, 0, };
118 	GdkColor *pcolor;
119 
120 	ecb = E_CANVAS_BACKGROUND (object);
121 
122 	switch (property_id) {
123 	case PROP_FILL_COLOR:
124 		if (g_value_get_string (value))
125 			gdk_color_parse (g_value_get_string (value), &color);
126 
127 		ecb->priv->rgba = ((color.red & 0xff00) << 16 |
128 				   (color.green & 0xff00) << 8 |
129 				   (color.blue & 0xff00) |
130 				   0xff);
131 		break;
132 
133 	case PROP_FILL_COLOR_GDK:
134 		pcolor = g_value_get_boxed (value);
135 		if (pcolor) {
136 			color = *pcolor;
137 		}
138 
139 		ecb->priv->rgba = ((color.red & 0xff00) << 16 |
140 				   (color.green & 0xff00) << 8 |
141 				   (color.blue & 0xff00) |
142 				   0xff);
143 		break;
144 
145 	case PROP_FILL_COLOR_RGBA:
146 		ecb->priv->rgba = g_value_get_uint (value);
147 		break;
148 
149 	}
150 
151 	gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (ecb));
152 }
153 
154 static void
155 ecb_get_property (GObject *object,
156                   guint property_id,
157                   GValue *value,
158                   GParamSpec *pspec)
159 {
160 	ECanvasBackground *ecb;
161 
162 	ecb = E_CANVAS_BACKGROUND (object);
163 
164 	switch (property_id) {
165 	case PROP_FILL_COLOR_RGBA:
166 		g_value_set_uint (value, ecb->priv->rgba);
167 		break;
168 	default:
169 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
170 		break;
171 	}
172 }
173 
174 static void
175 ecb_init (ECanvasBackground *ecb)
176 {
177 	ecb->priv = E_CANVAS_BACKGROUND_GET_PRIVATE (ecb);
178 }
179 
180 static void
181 ecb_draw (GnomeCanvasItem *item,
182           cairo_t *cr,
183           gint x,
184           gint y,
185           gint width,
186           gint height)
187 {
188 	ECanvasBackground *ecb = E_CANVAS_BACKGROUND (item);
189 
190 	cairo_save (cr);
191 	cairo_set_source_rgba (
192 		cr,
193 		((ecb->priv->rgba >> 24) & 0xff) / 255.0,
194 		((ecb->priv->rgba >> 16) & 0xff) / 255.0,
195 		((ecb->priv->rgba >>  8) & 0xff) / 255.0,
196 		( ecb->priv->rgba        & 0xff) / 255.0);
197 	cairo_paint (cr);
198 	cairo_restore (cr);
199 }
200 
201 static GnomeCanvasItem *
202 ecb_point (GnomeCanvasItem *item,
203            gdouble x,
204            gdouble y,
205            gint cx,
206            gint cy)
207 {
208 	return item;
209 }
210 
211 static void
212 ecb_style_set (ECanvasBackground *ecb,
213                GtkStyle *previous_style)
214 {
215 	GnomeCanvasItem *item = GNOME_CANVAS_ITEM (ecb);
216 
217 	if (gtk_widget_get_realized (GTK_WIDGET (item->canvas)))
218 		gnome_canvas_item_request_update (GNOME_CANVAS_ITEM (ecb));
219 }
220 
221 static void
222 ecb_class_init (ECanvasBackgroundClass *ecb_class)
223 {
224 	GnomeCanvasItemClass *item_class = GNOME_CANVAS_ITEM_CLASS (ecb_class);
225 	GObjectClass *object_class = G_OBJECT_CLASS (ecb_class);
226 
227 	g_type_class_add_private (ecb_class, sizeof (ECanvasBackgroundPrivate));
228 
229 	object_class->set_property  = ecb_set_property;
230 	object_class->get_property  = ecb_get_property;
231 
232 	item_class->update          = ecb_update;
233 	item_class->draw            = ecb_draw;
234 	item_class->point           = ecb_point;
235 	item_class->bounds          = ecb_bounds;
236 
237 	ecb_class->style_set	    = ecb_style_set;
238 
239 	g_object_class_install_property (
240 		object_class,
241 		PROP_FILL_COLOR,
242 		g_param_spec_string (
243 			"fill_color",
244 			"Fill color",
245 			"Fill color",
246 			NULL,
247 			G_PARAM_WRITABLE));
248 
249 	g_object_class_install_property (
250 		object_class,
251 		PROP_FILL_COLOR_GDK,
252 		g_param_spec_boxed (
253 			"fill_color_gdk",
254 			"GDK fill color",
255 			"GDK fill color",
256 			GDK_TYPE_COLOR,
257 			G_PARAM_WRITABLE));
258 
259 	g_object_class_install_property (
260 		object_class,
261 		PROP_FILL_COLOR_RGBA,
262 		g_param_spec_uint (
263 			"fill_color_rgba",
264 			"GDK fill color",
265 			"GDK fill color",
266 			0, G_MAXUINT, 0,
267 			G_PARAM_READWRITE));
268 
269 	ecb_signals[STYLE_SET] = g_signal_new (
270 		"style_set",
271 		G_OBJECT_CLASS_TYPE (object_class),
272 		G_SIGNAL_RUN_LAST,
273 		G_STRUCT_OFFSET (ECanvasBackgroundClass, style_set),
274 		NULL, NULL,
275 		g_cclosure_marshal_VOID__OBJECT,
276 		G_TYPE_NONE, 1,
277 		GTK_TYPE_STYLE);
278 }