]> git.sur5r.net Git - glabels/blob - glabels2/src/label-line.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / label-line.c
1 /*
2  *  label-line.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "label-line.h"
22
23 #include <glib.h>
24
25 #include "debug.h"
26
27
28 /*========================================================*/
29 /* Private types.                                         */
30 /*========================================================*/
31
32 struct _glLabelLinePrivate {
33         gdouble          line_width;
34         glColorNode     *line_color_node;
35 };
36
37
38 /*========================================================*/
39 /* Private globals.                                       */
40 /*========================================================*/
41
42
43 /*========================================================*/
44 /* Private function prototypes.                           */
45 /*========================================================*/
46
47 static void    gl_label_line_finalize     (GObject           *object);
48
49 static void    copy                       (glLabelObject     *dst_object,
50                                            glLabelObject     *src_object);
51
52 static void    set_line_color             (glLabelObject     *object,
53                                            glColorNode       *line_color_node);
54
55 static void    set_line_width             (glLabelObject     *object,
56                                            gdouble            line_width);
57
58 static glColorNode   *get_line_color      (glLabelObject     *object);
59
60 static gdouble get_line_width             (glLabelObject     *object);
61
62 static void    draw_object                (glLabelObject     *object,
63                                            cairo_t           *cr,
64                                            gboolean           screen_flag,
65                                            glMergeRecord     *record);
66
67 static void    draw_shadow                (glLabelObject     *object,
68                                            cairo_t           *cr,
69                                            gboolean           screen_flag,
70                                            glMergeRecord     *record);
71
72
73 /*****************************************************************************/
74 /* Boilerplate object stuff.                                                 */
75 /*****************************************************************************/
76 G_DEFINE_TYPE (glLabelLine, gl_label_line, GL_TYPE_LABEL_OBJECT);
77
78
79 static void
80 gl_label_line_class_init (glLabelLineClass *class)
81 {
82         GObjectClass       *object_class       = G_OBJECT_CLASS (class);
83         glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
84
85         gl_label_line_parent_class = g_type_class_peek_parent (class);
86
87         label_object_class->copy           = copy;
88         label_object_class->set_line_color = set_line_color;
89         label_object_class->set_line_width = set_line_width;
90         label_object_class->get_line_color = get_line_color;
91         label_object_class->get_line_width = get_line_width;
92         label_object_class->draw_object    = draw_object;
93         label_object_class->draw_shadow    = draw_shadow;
94
95         object_class->finalize = gl_label_line_finalize;
96 }
97
98
99 static void
100 gl_label_line_init (glLabelLine *lline)
101 {
102         lline->priv = g_new0 (glLabelLinePrivate, 1);
103         lline->priv->line_color_node = gl_color_node_new_default ();
104 }
105
106
107 static void
108 gl_label_line_finalize (GObject *object)
109 {
110         glLabelLine *lline = GL_LABEL_LINE (object);
111
112         g_return_if_fail (object && GL_IS_LABEL_LINE (object));
113
114         gl_color_node_free (&(lline->priv->line_color_node));
115         g_free (lline->priv);
116
117         G_OBJECT_CLASS (gl_label_line_parent_class)->finalize (object);
118 }
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 /*****************************************************************************/
138 /* Copy object contents.                                                     */
139 /*****************************************************************************/
140 static void
141 copy (glLabelObject *dst_object,
142       glLabelObject *src_object)
143 {
144         glLabelLine *lline     = (glLabelLine *)src_object;
145         glLabelLine *new_lline = (glLabelLine *)dst_object;
146         gdouble      line_width;
147         glColorNode *line_color_node;
148
149         gl_debug (DEBUG_LABEL, "START");
150
151         g_return_if_fail (lline && GL_IS_LABEL_LINE (lline));
152         g_return_if_fail (new_lline && GL_IS_LABEL_LINE (new_lline));
153
154         line_width = get_line_width (src_object);
155         line_color_node = get_line_color (src_object);
156
157         set_line_width (dst_object, line_width);
158         set_line_color (dst_object, line_color_node);
159
160         gl_color_node_free (&line_color_node);
161         
162         gl_debug (DEBUG_LABEL, "END");
163 }
164
165
166 /*---------------------------------------------------------------------------*/
167 /* PRIVATE.  Set line color method.                                          */
168 /*---------------------------------------------------------------------------*/
169 static void
170 set_line_color (glLabelObject *object,
171                 glColorNode   *line_color_node)
172 {
173         glLabelLine *lline = (glLabelLine *)object;
174
175         g_return_if_fail (lline && GL_IS_LABEL_LINE (lline));
176
177         if ( !gl_color_node_equal (lline->priv->line_color_node, line_color_node)) {
178                 
179                 gl_color_node_free (&(lline->priv->line_color_node ));
180                 lline->priv->line_color_node = gl_color_node_dup (line_color_node);
181                 
182                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lline));
183         }
184 }
185
186
187 /*---------------------------------------------------------------------------*/
188 /* PRIVATE.  Set line width method.                                          */
189 /*---------------------------------------------------------------------------*/
190 static void
191 set_line_width (glLabelObject *object,
192                 gdouble        line_width)
193 {
194         glLabelLine *lline = (glLabelLine *)object;
195
196         g_return_if_fail (lline && GL_IS_LABEL_LINE (lline));
197
198         if ( lline->priv->line_width != line_width ) {
199                 lline->priv->line_width = line_width;
200                 gl_label_object_emit_changed (GL_LABEL_OBJECT(lline));
201         }
202 }
203
204
205 /*---------------------------------------------------------------------------*/
206 /* PRIVATE.  Get line color method.                                          */
207 /*---------------------------------------------------------------------------*/
208 static gdouble
209 get_line_width (glLabelObject *object)
210 {
211         glLabelLine *lline = (glLabelLine *)object;
212
213         g_return_val_if_fail (lline && GL_IS_LABEL_LINE (lline), 0.0);
214
215         return lline->priv->line_width;
216 }
217
218
219 /*---------------------------------------------------------------------------*/
220 /* PRIVATE.  Get line width method.                                          */
221 /*---------------------------------------------------------------------------*/
222 static glColorNode*
223 get_line_color (glLabelObject *object)
224 {
225         glLabelLine *lline = (glLabelLine *)object;
226
227         g_return_val_if_fail (lline && GL_IS_LABEL_LINE (lline), 0);
228
229         return gl_color_node_dup (lline->priv->line_color_node);
230 }
231
232
233 /*****************************************************************************/
234 /* Draw object method.                                                       */
235 /*****************************************************************************/
236 static void
237 draw_object (glLabelObject *object,
238              cairo_t       *cr,
239              gboolean       screen_flag,
240              glMergeRecord *record)
241 {
242         gdouble        w, h;
243         gdouble        line_width;
244         glColorNode   *line_color_node;
245         guint          line_color;
246
247         gl_debug (DEBUG_LABEL, "START");
248
249         gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
250
251         line_width = gl_label_object_get_line_width (object);
252         
253         line_color_node = gl_label_object_get_line_color (object);
254         line_color = gl_color_node_expand (line_color_node, record);
255         if (line_color_node->field_flag && screen_flag)
256         {
257                 line_color = GL_COLOR_MERGE_DEFAULT;
258         }
259
260
261         cairo_move_to (cr, 0.0, 0.0);
262         cairo_line_to (cr, w, h);
263
264
265         /* Draw line */
266         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (line_color));
267         cairo_set_line_width (cr, line_width);
268         cairo_stroke (cr);
269
270         gl_color_node_free (&line_color_node);
271
272         gl_debug (DEBUG_LABEL, "END");
273 }
274
275
276 /*****************************************************************************/
277 /* Draw shadow method.                                                       */
278 /*****************************************************************************/
279 static void
280 draw_shadow (glLabelObject *object,
281              cairo_t       *cr,
282              gboolean       screen_flag,
283              glMergeRecord *record)
284 {
285         gdouble        w, h;
286         gdouble        line_width;
287         glColorNode   *line_color_node;
288         glColorNode   *shadow_color_node;
289         gdouble        shadow_opacity;
290         guint          shadow_line_color;
291
292         gl_debug (DEBUG_LABEL, "START");
293
294         gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
295
296         line_width = gl_label_object_get_line_width (object);
297         
298         line_color_node = gl_label_object_get_line_color (object);
299         if (line_color_node->field_flag)
300         {
301                 line_color_node->color = GL_COLOR_MERGE_DEFAULT;
302         }
303
304         shadow_color_node = gl_label_object_get_shadow_color (object);
305         if (shadow_color_node->field_flag)
306         {
307                 shadow_color_node->color = GL_COLOR_SHADOW_MERGE_DEFAULT;
308         }
309         shadow_opacity = gl_label_object_get_shadow_opacity (object);
310         shadow_line_color = gl_color_shadow (shadow_color_node->color, shadow_opacity, line_color_node->color);
311
312
313         cairo_move_to (cr, 0.0, 0.0);
314         cairo_line_to (cr, w, h);
315
316
317         /* Draw outline shadow */
318         cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_line_color));
319         cairo_set_line_width (cr, line_width);
320         cairo_stroke (cr);
321
322
323         gl_color_node_free (&line_color_node);
324         gl_color_node_free (&shadow_color_node);
325
326         gl_debug (DEBUG_LABEL, "END");
327 }
328
329
330
331
332 /*
333  * Local Variables:       -- emacs
334  * mode: C                -- emacs
335  * c-basic-offset: 8      -- emacs
336  * tab-width: 8           -- emacs
337  * indent-tabs-mode: nil  -- emacs
338  * End:                   -- emacs
339  */