3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
21 #include "label-ellipse.h"
23 #include <glib/gi18n.h>
27 #include "cairo-ellipse-path.h"
31 /*========================================================*/
32 /* Private macros and constants. */
33 /*========================================================*/
35 /*========================================================*/
37 /*========================================================*/
39 struct _glLabelEllipsePrivate {
41 glColorNode *line_color_node;
42 glColorNode *fill_color_node;
45 /*========================================================*/
46 /* Private globals. */
47 /*========================================================*/
49 /*========================================================*/
50 /* Private function prototypes. */
51 /*========================================================*/
53 static void gl_label_ellipse_finalize (GObject *object);
55 static void copy (glLabelObject *dst_object,
56 glLabelObject *src_object);
58 static void set_fill_color (glLabelObject *object,
59 glColorNode *fill_color_node,
62 static void set_line_color (glLabelObject *object,
63 glColorNode *line_color_node,
66 static void set_line_width (glLabelObject *object,
70 static glColorNode* get_fill_color (glLabelObject *object);
72 static glColorNode* get_line_color (glLabelObject *object);
74 static gdouble get_line_width (glLabelObject *object);
76 static void draw_object (glLabelObject *object,
79 glMergeRecord *record);
81 static void draw_shadow (glLabelObject *object,
84 glMergeRecord *record);
86 static gboolean object_at (glLabelObject *object,
92 /*****************************************************************************/
93 /* Boilerplate object stuff. */
94 /*****************************************************************************/
95 G_DEFINE_TYPE (glLabelEllipse, gl_label_ellipse, GL_TYPE_LABEL_OBJECT)
99 gl_label_ellipse_class_init (glLabelEllipseClass *class)
101 GObjectClass *object_class = G_OBJECT_CLASS (class);
102 glLabelObjectClass *label_object_class = GL_LABEL_OBJECT_CLASS (class);
104 gl_label_ellipse_parent_class = g_type_class_peek_parent (class);
106 label_object_class->copy = copy;
107 label_object_class->set_fill_color = set_fill_color;
108 label_object_class->set_line_color = set_line_color;
109 label_object_class->set_line_width = set_line_width;
110 label_object_class->get_fill_color = get_fill_color;
111 label_object_class->get_line_color = get_line_color;
112 label_object_class->get_line_width = get_line_width;
113 label_object_class->draw_object = draw_object;
114 label_object_class->draw_shadow = draw_shadow;
115 label_object_class->object_at = object_at;
117 object_class->finalize = gl_label_ellipse_finalize;
122 gl_label_ellipse_init (glLabelEllipse *lellipse)
124 lellipse->priv = g_new0 (glLabelEllipsePrivate, 1);
129 gl_label_ellipse_finalize (GObject *object)
131 glLabelEllipse *lellipse = GL_LABEL_ELLIPSE (object);
133 g_return_if_fail (object && GL_IS_LABEL_ELLIPSE (object));
135 gl_color_node_free (&(lellipse->priv->line_color_node));
136 gl_color_node_free (&(lellipse->priv->fill_color_node));
137 g_free (lellipse->priv);
139 G_OBJECT_CLASS (gl_label_ellipse_parent_class)->finalize (object);
143 /*****************************************************************************/
144 /* NEW label "ellipse" object. */
145 /*****************************************************************************/
147 gl_label_ellipse_new (glLabel *label,
150 glLabelEllipse *lellipse;
151 glColorNode *fill_color_node;
152 glColorNode *line_color_node;
154 lellipse = g_object_new (gl_label_ellipse_get_type(), NULL);
160 gl_label_checkpoint (label, _("Create ellipse object"));
163 fill_color_node = gl_color_node_new_default ();
164 line_color_node = gl_color_node_new_default ();
166 line_color_node->color = gl_label_get_default_line_color(label);
167 fill_color_node->color = gl_label_get_default_fill_color(label);
169 lellipse->priv->line_width = gl_label_get_default_line_width(label);
170 lellipse->priv->line_color_node = line_color_node;
171 lellipse->priv->fill_color_node = fill_color_node;
173 gl_label_add_object (label, GL_LABEL_OBJECT (lellipse));
174 gl_label_object_set_parent (GL_LABEL_OBJECT (lellipse), label);
177 return G_OBJECT (lellipse);
181 /*****************************************************************************/
182 /* Copy object contents. */
183 /*****************************************************************************/
185 copy (glLabelObject *dst_object,
186 glLabelObject *src_object)
188 glLabelEllipse *lellipse = (glLabelEllipse *)src_object;
189 glLabelEllipse *new_lellipse = (glLabelEllipse *)dst_object;
191 glColorNode *line_color_node;
192 glColorNode *fill_color_node;
194 gl_debug (DEBUG_LABEL, "START");
196 g_return_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse));
197 g_return_if_fail (new_lellipse && GL_IS_LABEL_ELLIPSE (new_lellipse));
199 line_width = get_line_width (src_object);
200 line_color_node = get_line_color (src_object);
201 fill_color_node = get_fill_color (src_object);
203 set_line_width (dst_object, line_width, FALSE);
204 set_line_color (dst_object, line_color_node, FALSE);
205 set_fill_color (dst_object, fill_color_node, FALSE);
207 gl_color_node_free (&line_color_node);
208 gl_color_node_free (&fill_color_node);
210 gl_debug (DEBUG_LABEL, "END");
214 /*---------------------------------------------------------------------------*/
215 /* PRIVATE. Set fill color method. */
216 /*---------------------------------------------------------------------------*/
218 set_fill_color (glLabelObject *object,
219 glColorNode *fill_color_node,
222 glLabelEllipse *lellipse = (glLabelEllipse *)object;
225 g_return_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse));
227 if (!gl_color_node_equal (lellipse->priv->fill_color_node, fill_color_node))
231 label = gl_label_object_get_parent (GL_LABEL_OBJECT (lellipse));
232 gl_label_checkpoint (label, _("Fill color"));
235 gl_color_node_free (&(lellipse->priv->fill_color_node));
236 lellipse->priv->fill_color_node = gl_color_node_dup (fill_color_node);
238 gl_label_object_emit_changed (GL_LABEL_OBJECT(lellipse));
243 /*---------------------------------------------------------------------------*/
244 /* PRIVATE. Set line color method. */
245 /*---------------------------------------------------------------------------*/
247 set_line_color (glLabelObject *object,
248 glColorNode *line_color_node,
251 glLabelEllipse *lellipse = (glLabelEllipse *)object;
254 g_return_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse));
256 if ( !gl_color_node_equal (lellipse->priv->line_color_node, line_color_node) )
260 label = gl_label_object_get_parent (GL_LABEL_OBJECT (lellipse));
261 gl_label_checkpoint (label, _("Line color"));
264 gl_color_node_free (&(lellipse->priv->line_color_node));
265 lellipse->priv->line_color_node = gl_color_node_dup (line_color_node);
267 gl_label_object_emit_changed (GL_LABEL_OBJECT(lellipse));
272 /*---------------------------------------------------------------------------*/
273 /* PRIVATE. Set line width method. */
274 /*---------------------------------------------------------------------------*/
276 set_line_width (glLabelObject *object,
280 glLabelEllipse *lellipse = (glLabelEllipse *)object;
283 g_return_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse));
285 if ( lellipse->priv->line_width != line_width )
289 label = gl_label_object_get_parent (GL_LABEL_OBJECT (lellipse));
290 gl_label_checkpoint (label, _("Line width"));
293 lellipse->priv->line_width = line_width;
294 gl_label_object_emit_changed (GL_LABEL_OBJECT(lellipse));
299 /*---------------------------------------------------------------------------*/
300 /* PRIVATE. Get fill color method. */
301 /*---------------------------------------------------------------------------*/
303 get_line_width (glLabelObject *object)
305 glLabelEllipse *lellipse = (glLabelEllipse *)object;
307 g_return_val_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse), 0.0);
309 return lellipse->priv->line_width;
313 /*---------------------------------------------------------------------------*/
314 /* PRIVATE. Get line color method. */
315 /*---------------------------------------------------------------------------*/
317 get_line_color (glLabelObject *object)
319 glLabelEllipse *lellipse = (glLabelEllipse *)object;
321 g_return_val_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse), 0);
323 return gl_color_node_dup (lellipse->priv->line_color_node);
327 /*---------------------------------------------------------------------------*/
328 /* PRIVATE. Get line width method. */
329 /*---------------------------------------------------------------------------*/
331 get_fill_color (glLabelObject *object)
333 glLabelEllipse *lellipse = (glLabelEllipse *)object;
335 g_return_val_if_fail (lellipse && GL_IS_LABEL_ELLIPSE (lellipse), 0);
337 return gl_color_node_dup (lellipse->priv->fill_color_node);
341 /*****************************************************************************/
342 /* Draw object method. */
343 /*****************************************************************************/
345 draw_object (glLabelObject *object,
347 gboolean screen_flag,
348 glMergeRecord *record)
352 glColorNode *line_color_node;
353 glColorNode *fill_color_node;
357 gl_debug (DEBUG_LABEL, "START");
359 gl_label_object_get_size (object, &w, &h);
361 line_width = gl_label_object_get_line_width (object);
363 line_color_node = gl_label_object_get_line_color (object);
364 fill_color_node = gl_label_object_get_fill_color (object);
365 line_color = gl_color_node_expand (line_color_node, record);
366 fill_color = gl_color_node_expand (fill_color_node, record);
367 if (line_color_node->field_flag && screen_flag)
369 line_color = GL_COLOR_MERGE_DEFAULT;
371 if (fill_color_node->field_flag && screen_flag)
373 fill_color = GL_COLOR_FILL_MERGE_DEFAULT;
377 gl_cairo_ellipse_path (cr, w/2, h/2);
379 /* Paint fill color */
380 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (fill_color));
381 cairo_fill_preserve (cr);
384 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (line_color));
385 cairo_set_line_width (cr, line_width);
388 gl_color_node_free (&line_color_node);
389 gl_color_node_free (&fill_color_node);
391 gl_debug (DEBUG_LABEL, "END");
395 /*****************************************************************************/
396 /* Draw shadow method. */
397 /*****************************************************************************/
399 draw_shadow (glLabelObject *object,
401 gboolean screen_flag,
402 glMergeRecord *record)
406 glColorNode *line_color_node;
407 glColorNode *fill_color_node;
410 glColorNode *shadow_color_node;
412 gdouble shadow_opacity;
414 gl_debug (DEBUG_LABEL, "START");
418 gl_label_object_get_size (object, &w, &h);
420 line_width = gl_label_object_get_line_width (object);
422 line_color_node = gl_label_object_get_line_color (object);
423 fill_color_node = gl_label_object_get_fill_color (object);
424 line_color = gl_color_node_expand (line_color_node, record);
425 fill_color = gl_color_node_expand (fill_color_node, record);
426 if (line_color_node->field_flag && screen_flag)
428 line_color = GL_COLOR_MERGE_DEFAULT;
430 if (fill_color_node->field_flag && screen_flag)
432 fill_color = GL_COLOR_FILL_MERGE_DEFAULT;
435 shadow_color_node = gl_label_object_get_shadow_color (object);
436 shadow_color = gl_color_node_expand (shadow_color_node, record);
437 if (shadow_color_node->field_flag && screen_flag)
439 shadow_color = GL_COLOR_SHADOW_MERGE_DEFAULT;
441 shadow_opacity = gl_label_object_get_shadow_opacity (object);
442 shadow_color = gl_color_set_opacity (shadow_color, shadow_opacity);
445 cairo_set_source_rgba (cr, GL_COLOR_RGBA_ARGS (shadow_color));
447 if ( GL_COLOR_F_ALPHA (fill_color) )
449 if ( GL_COLOR_F_ALPHA (line_color) )
451 /* Has FILL and OUTLINE: adjust size to account for line width. */
452 cairo_translate (cr, -line_width/2, -line_width/2);
453 gl_cairo_ellipse_path (cr, (w+line_width)/2, (h+line_width)/2);
458 /* Has FILL but no OUTLINE. */
459 gl_cairo_ellipse_path (cr, w/2, h/2);
467 if ( GL_COLOR_F_ALPHA (line_color) )
469 /* Has only OUTLINE. */
470 gl_cairo_ellipse_path (cr, w/2, h/2);
472 /* Draw shadow of outline */
473 cairo_set_line_width (cr, line_width);
479 gl_color_node_free (&line_color_node);
480 gl_color_node_free (&fill_color_node);
481 gl_color_node_free (&shadow_color_node);
485 gl_debug (DEBUG_LABEL, "END");
489 /*****************************************************************************/
490 /* Is object at coordinates? */
491 /*****************************************************************************/
493 object_at (glLabelObject *object,
501 gl_label_object_get_size (object, &w, &h);
504 gl_cairo_ellipse_path (cr, w/2, h/2);
506 if (cairo_in_fill (cr, x, y))
511 line_width = gl_label_object_get_line_width (object);
512 cairo_set_line_width (cr, line_width);
513 if (cairo_in_stroke (cr, x, y))
525 * Local Variables: -- emacs
527 * c-basic-offset: 8 -- emacs
528 * tab-width: 8 -- emacs
529 * indent-tabs-mode: nil -- emacs