1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * label_box.c: GLabels label box object
8 * Copyright (C) 2001-2007 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "label-box.h"
27 #include <glib/gmem.h>
28 #include <glib/gstrfuncs.h>
29 #include <glib/gmessages.h>
33 /*========================================================*/
35 /*========================================================*/
37 struct _glLabelBoxPrivate {
39 glColorNode *line_color_node;
40 glColorNode *fill_color_node;
43 /*========================================================*/
44 /* Private globals. */
45 /*========================================================*/
47 /*========================================================*/
48 /* Private function prototypes. */
49 /*========================================================*/
51 static void gl_label_box_finalize (GObject *object);
53 static void copy (glLabelObject *dst_object,
54 glLabelObject *src_object);
56 static void set_fill_color (glLabelObject *object,
57 glColorNode *fill_color_node);
59 static void set_line_color (glLabelObject *object,
60 glColorNode *line_color_node);
62 static void set_line_width (glLabelObject *object,
65 static glColorNode* get_fill_color (glLabelObject *object);
67 static glColorNode* get_line_color (glLabelObject *object);
69 static gdouble get_line_width (glLabelObject *object);
71 static void draw_object (glLabelObject *object,
74 glMergeRecord *record);
76 static void draw_shadow (glLabelObject *object,
79 glMergeRecord *record);
85 /*****************************************************************************/
86 /* Boilerplate object stuff. */
87 /*****************************************************************************/
88 G_DEFINE_TYPE (glLabelBox, gl_label_box, GL_TYPE_LABEL_OBJECT);
91 gl_label_box_class_init (glLabelBoxClass *class)
93 GObjectClass *object_class = G_OBJECT_CLASS (class);
94 glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
96 gl_label_box_parent_class = g_type_class_peek_parent (class);
98 label_object_class->copy = copy;
99 label_object_class->set_fill_color = set_fill_color;
100 label_object_class->set_line_color = set_line_color;
101 label_object_class->set_line_width = set_line_width;
102 label_object_class->get_fill_color = get_fill_color;
103 label_object_class->get_line_color = get_line_color;
104 label_object_class->get_line_width = get_line_width;
105 label_object_class->draw_object = draw_object;
106 label_object_class->draw_shadow = draw_shadow;
108 object_class->finalize = gl_label_box_finalize;
112 gl_label_box_init (glLabelBox *lbox)
114 lbox->priv = g_new0 (glLabelBoxPrivate, 1);
115 lbox->priv->line_color_node = gl_color_node_new_default ();
116 lbox->priv->fill_color_node = gl_color_node_new_default ();
120 gl_label_box_finalize (GObject *object)
122 glLabelBox *lbox = GL_LABEL_BOX (object);
124 g_return_if_fail (object && GL_IS_LABEL_BOX (object));
126 gl_color_node_free (&(lbox->priv->fill_color_node));
127 gl_color_node_free (&(lbox->priv->line_color_node));
130 G_OBJECT_CLASS (gl_label_box_parent_class)->finalize (object);
133 /*****************************************************************************/
134 /* NEW label "box" object. */
135 /*****************************************************************************/
137 gl_label_box_new (glLabel *label)
141 lbox = g_object_new (gl_label_box_get_type(), NULL);
143 gl_label_object_set_parent (GL_LABEL_OBJECT(lbox), label);
145 return G_OBJECT (lbox);
148 /*****************************************************************************/
149 /* Copy object contents. */
150 /*****************************************************************************/
152 copy (glLabelObject *dst_object,
153 glLabelObject *src_object)
155 glLabelBox *lbox = (glLabelBox *)src_object;
156 glLabelBox *new_lbox = (glLabelBox *)dst_object;
158 glColorNode *line_color_node;
159 glColorNode *fill_color_node;
161 gl_debug (DEBUG_LABEL, "START");
163 g_return_if_fail (lbox && GL_IS_LABEL_BOX (lbox));
164 g_return_if_fail (new_lbox && GL_IS_LABEL_BOX (new_lbox));
166 line_width = get_line_width (src_object);
167 line_color_node = get_line_color (src_object);
168 fill_color_node = get_fill_color (src_object);
170 set_line_width (dst_object, line_width);
171 set_line_color (dst_object, line_color_node);
172 set_fill_color (dst_object, fill_color_node);
174 gl_color_node_free (&line_color_node);
175 gl_color_node_free (&fill_color_node);
177 gl_debug (DEBUG_LABEL, "END");
181 /*---------------------------------------------------------------------------*/
182 /* PRIVATE. Set fill color method. */
183 /*---------------------------------------------------------------------------*/
185 set_fill_color (glLabelObject *object,
186 glColorNode *fill_color_node)
188 glLabelBox *lbox = (glLabelBox *)object;
190 gl_debug (DEBUG_LABEL, "START");
192 g_return_if_fail (lbox && GL_IS_LABEL_BOX (lbox));
194 if (!gl_color_node_equal (lbox->priv->fill_color_node, fill_color_node)) {
196 gl_color_node_free (&(lbox->priv->fill_color_node));
197 lbox->priv->fill_color_node = gl_color_node_dup (fill_color_node);
199 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbox));
201 gl_debug (DEBUG_LABEL, "END");
204 /*---------------------------------------------------------------------------*/
205 /* PRIVATE. Set line color method. */
206 /*---------------------------------------------------------------------------*/
208 set_line_color (glLabelObject *object,
209 glColorNode *line_color_node)
211 glLabelBox *lbox = (glLabelBox *)object;
213 g_return_if_fail (lbox && GL_IS_LABEL_BOX (lbox));
215 if ( !gl_color_node_equal (lbox->priv->line_color_node, line_color_node )) {
216 gl_color_node_free (&(lbox->priv->line_color_node));
217 lbox->priv->line_color_node = gl_color_node_dup (line_color_node);
218 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbox));
222 /*---------------------------------------------------------------------------*/
223 /* PRIVATE. Set line width method. */
224 /*---------------------------------------------------------------------------*/
226 set_line_width (glLabelObject *object,
229 glLabelBox *lbox = (glLabelBox *)object;
231 g_return_if_fail (lbox && GL_IS_LABEL_BOX (lbox));
233 if ( lbox->priv->line_width != line_width ) {
234 lbox->priv->line_width = line_width;
235 gl_label_object_emit_changed (GL_LABEL_OBJECT(lbox));
240 /*---------------------------------------------------------------------------*/
241 /* PRIVATE. Get fill color method. */
242 /*---------------------------------------------------------------------------*/
244 get_line_width (glLabelObject *object)
246 glLabelBox *lbox = (glLabelBox *)object;
248 g_return_val_if_fail (lbox && GL_IS_LABEL_BOX (lbox), 0.0);
250 return lbox->priv->line_width;
253 /*---------------------------------------------------------------------------*/
254 /* PRIVATE. Get line color method. */
255 /*---------------------------------------------------------------------------*/
257 get_line_color (glLabelObject *object)
259 glLabelBox *lbox = (glLabelBox *)object;
261 g_return_val_if_fail (lbox && GL_IS_LABEL_BOX (lbox), 0);
263 return gl_color_node_dup (lbox->priv->line_color_node);
266 /*---------------------------------------------------------------------------*/
267 /* PRIVATE. Get line width method. */
268 /*---------------------------------------------------------------------------*/
270 get_fill_color (glLabelObject *object)
272 glLabelBox *lbox = (glLabelBox *)object;
274 g_return_val_if_fail (lbox && GL_IS_LABEL_BOX (lbox), 0);
276 return gl_color_node_dup (lbox->priv->fill_color_node);
279 /*****************************************************************************/
280 /* Draw object method. */
281 /*****************************************************************************/
283 draw_object (glLabelObject *object,
285 gboolean screen_flag,
286 glMergeRecord *record)
290 glColorNode *line_color_node;
291 glColorNode *fill_color_node;
295 gl_debug (DEBUG_LABEL, "START");
297 gl_label_object_get_size (object, &w, &h);
299 line_width = gl_label_object_get_line_width (object);
301 line_color_node = gl_label_object_get_line_color (object);
302 fill_color_node = gl_label_object_get_fill_color (object);
303 line_color = gl_color_node_expand (line_color_node, record);
304 fill_color = gl_color_node_expand (fill_color_node, record);
305 if (line_color_node->field_flag && screen_flag)
307 line_color = GL_COLOR_MERGE_DEFAULT;
309 if (fill_color_node->field_flag && screen_flag)
311 fill_color = GL_COLOR_FILL_MERGE_DEFAULT;
315 cairo_rectangle (cr, 0.0, 0.0, w, h);
317 /* Paint fill color */
318 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
319 cairo_fill_preserve (cr);
322 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (line_color));
323 cairo_set_line_width (cr, line_width);
326 gl_color_node_free (&line_color_node);
327 gl_color_node_free (&fill_color_node);
329 gl_debug (DEBUG_LABEL, "END");
332 /*****************************************************************************/
333 /* Draw shadow method. */
334 /*****************************************************************************/
336 draw_shadow (glLabelObject *object,
338 gboolean screen_flag,
339 glMergeRecord *record)
343 glColorNode *line_color_node;
344 glColorNode *fill_color_node;
347 glColorNode *shadow_color_node;
349 gdouble shadow_opacity;
350 guint shadow_line_color;
351 guint shadow_fill_color;
353 gl_debug (DEBUG_LABEL, "START");
355 gl_label_object_get_size (object, &w, &h);
357 line_width = gl_label_object_get_line_width (object);
359 line_color_node = gl_label_object_get_line_color (object);
360 fill_color_node = gl_label_object_get_fill_color (object);
361 line_color = gl_color_node_expand (line_color_node, record);
362 fill_color = gl_color_node_expand (fill_color_node, record);
363 if (line_color_node->field_flag && screen_flag)
365 line_color = GL_COLOR_MERGE_DEFAULT;
367 if (fill_color_node->field_flag && screen_flag)
369 fill_color = GL_COLOR_FILL_MERGE_DEFAULT;
372 shadow_color_node = gl_label_object_get_shadow_color (object);
373 shadow_color = gl_color_node_expand (shadow_color_node, record);
374 if (shadow_color_node->field_flag && screen_flag)
376 shadow_color = GL_COLOR_SHADOW_MERGE_DEFAULT;
378 shadow_opacity = gl_label_object_get_shadow_opacity (object);
379 shadow_line_color = gl_color_shadow (shadow_color, shadow_opacity, line_color_node->color);
380 shadow_fill_color = gl_color_shadow (shadow_color, shadow_opacity, fill_color_node->color);
383 cairo_rectangle (cr, 0.0, 0.0, w, h);
386 /* Draw fill shadow */
387 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_fill_color));
388 cairo_fill_preserve (cr);
390 /* Draw outline shadow */
391 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_line_color));
392 cairo_set_line_width (cr, line_width);
396 gl_color_node_free (&line_color_node);
397 gl_color_node_free (&fill_color_node);
398 gl_color_node_free (&shadow_color_node);
400 gl_debug (DEBUG_LABEL, "END");