]> git.sur5r.net Git - glabels/blob - src/label-line.c
Imported Upstream version 2.2.8
[glabels] / src / label-line.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  label_line.c:  GLabels label line object
7  *
8  *  Copyright (C) 2001-2007  Jim Evins <evins@snaught.com>.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "label-line.h"
26
27 #include <glib/gmem.h>
28 #include <glib/gstrfuncs.h>
29 #include <glib/gmessages.h>
30
31 #include "debug.h"
32
33 /*========================================================*/
34 /* Private types.                                         */
35 /*========================================================*/
36
37 struct _glLabelLinePrivate {
38         gdouble          line_width;
39         glColorNode     *line_color_node;
40 };
41
42 /*========================================================*/
43 /* Private globals.                                       */
44 /*========================================================*/
45
46 /*========================================================*/
47 /* Private function prototypes.                           */
48 /*========================================================*/
49
50 static void    gl_label_line_finalize      (GObject          *object);
51
52 static void    copy                        (glLabelObject    *dst_object,
53                                             glLabelObject    *src_object);
54
55 static void    set_line_color              (glLabelObject    *object,
56                                             glColorNode      *line_color_node);
57
58 static void    set_line_width              (glLabelObject    *object,
59                                             gdouble           line_width);
60
61 static glColorNode   *get_line_color       (glLabelObject    *object);
62
63 static gdouble get_line_width              (glLabelObject    *object);
64
65 static void    draw_object                (glLabelObject     *object,
66                                            cairo_t           *cr,
67                                            gboolean           screen_flag,
68                                            glMergeRecord     *record);
69
70 static void    draw_shadow                (glLabelObject     *object,
71                                            cairo_t           *cr,
72                                            gboolean           screen_flag,
73                                            glMergeRecord     *record);
74
75
76 \f
77 /*****************************************************************************/
78 /* Boilerplate object stuff.                                                 */
79 /*****************************************************************************/
80 G_DEFINE_TYPE (glLabelLine, gl_label_line, GL_TYPE_LABEL_OBJECT);
81
82 static void
83 gl_label_line_class_init (glLabelLineClass *class)
84 {
85         GObjectClass       *object_class       = G_OBJECT_CLASS (class);
86         glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
87
88         gl_label_line_parent_class = g_type_class_peek_parent (class);
89
90         label_object_class->copy           = copy;
91         label_object_class->set_line_color = set_line_color;
92         label_object_class->set_line_width = set_line_width;
93         label_object_class->get_line_color = get_line_color;
94         label_object_class->get_line_width = get_line_width;
95         label_object_class->draw_object    = draw_object;
96         label_object_class->draw_shadow    = draw_shadow;
97
98         object_class->finalize = gl_label_line_finalize;
99 }
100
101 static void
102 gl_label_line_init (glLabelLine *lline)
103 {
104         lline->priv = g_new0 (glLabelLinePrivate, 1);
105         lline->priv->line_color_node = gl_color_node_new_default ();
106 }
107
108 static void
109 gl_label_line_finalize (GObject *object)
110 {
111         glLabelLine *lline = GL_LABEL_LINE (object);
112
113         g_return_if_fail (object && GL_IS_LABEL_LINE (object));
114
115         gl_color_node_free (&(lline->priv->line_color_node));
116         g_free (lline->priv);
117
118         G_OBJECT_CLASS (gl_label_line_parent_class)->finalize (object);
119 }
120
121 /*****************************************************************************/
122 /* NEW label "line" object.                                               */
123 /*****************************************************************************/
124 GObject *
125 gl_label_line_new (glLabel *label)
126 {
127         glLabelLine *lline;
128
129         lline = g_object_new (gl_label_line_get_type(), NULL);
130
131         gl_label_object_set_parent (GL_LABEL_OBJECT(lline), label);
132
133         return G_OBJECT (lline);
134 }
135
136 /*****************************************************************************/
137 /* Copy object contents.                                                     */
138 /*****************************************************************************/
139 static void
140 copy (glLabelObject *dst_object,
141       glLabelObject *src_object)
142 {
143         glLabelLine *lline     = (glLabelLine *)src_object;
144         glLabelLine *new_lline = (glLabelLine *)dst_object;
145         gdouble      line_width;
146         glColorNode *line_color_node;
147
148         gl_debug (DEBUG_LABEL, "START");
149
150         g_return_if_fail (lline && GL_IS_LABEL_LINE (lline));
151         g_return_if_fail (new_lline && GL_IS_LABEL_LINE (new_lline));
152
153         line_width = get_line_width (src_object);
154         line_color_node = get_line_color (src_object);
155
156         set_line_width (dst_object, line_width);
157         set_line_color (dst_object, line_color_node);
158
159         gl_color_node_free (&line_color_node);
160         
161         gl_debug (DEBUG_LABEL, "END");
162 }
163
164
165 /*---------------------------------------------------------------------------*/
166 /* PRIVATE.  Set line color method.                                          */
167 /*---------------------------------------------------------------------------*/
168 static void
169 set_line_color (glLabelObject *object,
170                 glColorNode   *line_color_node)
171 {
172         glLabelLine *lline = (glLabelLine *)object;
173
174         g_return_if_fail (lline && GL_IS_LABEL_LINE (lline));
175
176         if ( !gl_color_node_equal (lline->priv->line_color_node, line_color_node)) {
177                 
178                 gl_color_node_free (&(lline->priv->line_color_node ));
179                 lline->priv->line_color_node = gl_color_node_dup (line_color_node);
180                 
181                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lline));
182         }
183 }
184
185 /*---------------------------------------------------------------------------*/
186 /* PRIVATE.  Set line width method.                                          */
187 /*---------------------------------------------------------------------------*/
188 static void
189 set_line_width (glLabelObject *object,
190                 gdouble        line_width)
191 {
192         glLabelLine *lline = (glLabelLine *)object;
193
194         g_return_if_fail (lline && GL_IS_LABEL_LINE (lline));
195
196         if ( lline->priv->line_width != line_width ) {
197                 lline->priv->line_width = line_width;
198                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lline));
199         }
200 }
201
202
203 /*---------------------------------------------------------------------------*/
204 /* PRIVATE.  Get line color method.                                          */
205 /*---------------------------------------------------------------------------*/
206 static gdouble
207 get_line_width (glLabelObject *object)
208 {
209         glLabelLine *lline = (glLabelLine *)object;
210
211         g_return_val_if_fail (lline && GL_IS_LABEL_LINE (lline), 0.0);
212
213         return lline->priv->line_width;
214 }
215
216 /*---------------------------------------------------------------------------*/
217 /* PRIVATE.  Get line width method.                                          */
218 /*---------------------------------------------------------------------------*/
219 static glColorNode*
220 get_line_color (glLabelObject *object)
221 {
222         glLabelLine *lline = (glLabelLine *)object;
223
224         g_return_val_if_fail (lline && GL_IS_LABEL_LINE (lline), 0);
225
226         return gl_color_node_dup (lline->priv->line_color_node);
227 }
228
229 /*****************************************************************************/
230 /* Draw object method.                                                       */
231 /*****************************************************************************/
232 static void
233 draw_object (glLabelObject *object,
234              cairo_t       *cr,
235              gboolean       screen_flag,
236              glMergeRecord *record)
237 {
238         gdouble        w, h;
239         gdouble        line_width;
240         glColorNode   *line_color_node;
241         guint          line_color;
242
243         gl_debug (DEBUG_LABEL, "START");
244
245         gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
246
247         line_width = gl_label_object_get_line_width (object);
248         
249         line_color_node = gl_label_object_get_line_color (object);
250         line_color = gl_color_node_expand (line_color_node, record);
251         if (line_color_node->field_flag && screen_flag)
252         {
253                 line_color = GL_COLOR_MERGE_DEFAULT;
254         }
255
256
257         cairo_move_to (cr, 0.0, 0.0);
258         cairo_line_to (cr, w, h);
259
260
261         /* Draw line */
262         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (line_color));
263         cairo_set_line_width (cr, line_width);
264         cairo_stroke (cr);
265
266         gl_color_node_free (&line_color_node);
267
268         gl_debug (DEBUG_LABEL, "END");
269 }
270
271 /*****************************************************************************/
272 /* Draw shadow method.                                                       */
273 /*****************************************************************************/
274 static void
275 draw_shadow (glLabelObject *object,
276              cairo_t       *cr,
277              gboolean       screen_flag,
278              glMergeRecord *record)
279 {
280         gdouble        w, h;
281         gdouble        line_width;
282         glColorNode   *line_color_node;
283         glColorNode   *shadow_color_node;
284         gdouble        shadow_opacity;
285         guint          shadow_line_color;
286
287         gl_debug (DEBUG_LABEL, "START");
288
289         gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
290
291         line_width = gl_label_object_get_line_width (object);
292         
293         line_color_node = gl_label_object_get_line_color (object);
294         if (line_color_node->field_flag)
295         {
296                 line_color_node->color = GL_COLOR_MERGE_DEFAULT;
297         }
298
299         shadow_color_node = gl_label_object_get_shadow_color (object);
300         if (shadow_color_node->field_flag)
301         {
302                 shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
303         }
304         shadow_opacity = gl_label_object_get_shadow_opacity (object);
305         shadow_line_color = gl_color_shadow (shadow_color_node->color, shadow_opacity, line_color_node->color);
306
307
308         cairo_move_to (cr, 0.0, 0.0);
309         cairo_line_to (cr, w, h);
310
311
312         /* Draw outline shadow */
313         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_line_color));
314         cairo_set_line_width (cr, line_width);
315         cairo_stroke (cr);
316
317
318         gl_color_node_free (&line_color_node);
319         gl_color_node_free (&shadow_color_node);
320
321         gl_debug (DEBUG_LABEL, "END");
322 }
323