~ubuntu-branches/ubuntu/breezy/libgnomecanvas/breezy

« back to all changes in this revision

Viewing changes to libgnomecanvas/gnome-canvas-polygon.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2004-09-13 22:40:39 UTC
  • Revision ID: james.westby@ubuntu.com-20040913224039-giftt9jhvyoorcuv
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
 
3
 * All rights reserved.
 
4
 *
 
5
 * This file is part of the Gnome Library.
 
6
 *
 
7
 * The Gnome Library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public License as
 
9
 * published by the Free Software Foundation; either version 2 of the
 
10
 * License, or (at your option) any later version.
 
11
 *
 
12
 * The Gnome Library 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 GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 
19
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
/*
 
23
  @NOTATION@
 
24
 */
 
25
/* Polygon item type for GnomeCanvas widget
 
26
 *
 
27
 * GnomeCanvas is basically a port of the Tk toolkit's most excellent canvas widget.  Tk is
 
28
 * copyrighted by the Regents of the University of California, Sun Microsystems, and other parties.
 
29
 *
 
30
 * Author: Federico Mena <federico@nuclecu.unam.mx>
 
31
 *         Rusty Conover <rconover@bangtail.net>
 
32
 */
 
33
 
 
34
#include <config.h>
 
35
#include <math.h>
 
36
#include <string.h>
 
37
#include "libart_lgpl/art_vpath.h"
 
38
#include "libart_lgpl/art_svp.h"
 
39
#include "libart_lgpl/art_svp_vpath.h"
 
40
#include "libart_lgpl/art_svp_vpath_stroke.h"
 
41
#include "libgnomecanvas.h"
 
42
 
 
43
#include "gnome-canvas-shape.h"
 
44
 
 
45
 
 
46
#define NUM_STATIC_POINTS 256   /* Number of static points to use to avoid allocating arrays */
 
47
 
 
48
 
 
49
enum {
 
50
        PROP_0,
 
51
        PROP_POINTS
 
52
};
 
53
 
 
54
 
 
55
static void gnome_canvas_polygon_class_init (GnomeCanvasPolygonClass *class);
 
56
static void gnome_canvas_polygon_init       (GnomeCanvasPolygon      *poly);
 
57
static void gnome_canvas_polygon_destroy    (GtkObject               *object);
 
58
static void gnome_canvas_polygon_set_property (GObject              *object,
 
59
                                               guint                 param_id,
 
60
                                               const GValue         *value,
 
61
                                               GParamSpec           *pspec);
 
62
static void gnome_canvas_polygon_get_property (GObject              *object,
 
63
                                               guint                 param_id,
 
64
                                               GValue               *value,
 
65
                                               GParamSpec           *pspec);
 
66
 
 
67
static void gnome_canvas_polygon_update      (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags);
 
68
 
 
69
static GnomeCanvasItemClass *parent_class;
 
70
 
 
71
GType
 
72
gnome_canvas_polygon_get_type (void)
 
73
{
 
74
        static GType polygon_type;
 
75
 
 
76
        if (!polygon_type) {
 
77
                static const GTypeInfo object_info = {
 
78
                        sizeof (GnomeCanvasPolygonClass),
 
79
                        (GBaseInitFunc) NULL,
 
80
                        (GBaseFinalizeFunc) NULL,
 
81
                        (GClassInitFunc) gnome_canvas_polygon_class_init,
 
82
                        (GClassFinalizeFunc) NULL,
 
83
                        NULL,                   /* class_data */
 
84
                        sizeof (GnomeCanvasPolygon),
 
85
                        0,                      /* n_preallocs */
 
86
                        (GInstanceInitFunc) gnome_canvas_polygon_init,
 
87
                        NULL                    /* value_table */
 
88
                };
 
89
 
 
90
                polygon_type = g_type_register_static (GNOME_TYPE_CANVAS_SHAPE, "GnomeCanvasPolygon",
 
91
                                                       &object_info, 0);
 
92
        }
 
93
 
 
94
        return polygon_type;
 
95
}
 
96
 
 
97
static void
 
98
gnome_canvas_polygon_class_init (GnomeCanvasPolygonClass *class)
 
99
{
 
100
        GObjectClass *gobject_class;
 
101
        GtkObjectClass *object_class;
 
102
        GnomeCanvasItemClass *item_class;
 
103
 
 
104
        gobject_class = (GObjectClass *) class;
 
105
        object_class = (GtkObjectClass *) class;
 
106
        item_class = (GnomeCanvasItemClass *) class;
 
107
 
 
108
        parent_class = g_type_class_peek_parent (class);
 
109
 
 
110
        gobject_class->set_property = gnome_canvas_polygon_set_property;
 
111
        gobject_class->get_property = gnome_canvas_polygon_get_property;
 
112
 
 
113
        g_object_class_install_property
 
114
                (gobject_class,
 
115
                 PROP_POINTS,
 
116
                 g_param_spec_boxed ("points", NULL, NULL,
 
117
                                     GNOME_TYPE_CANVAS_POINTS,
 
118
                                     (G_PARAM_READABLE | G_PARAM_WRITABLE)));
 
119
 
 
120
        object_class->destroy = gnome_canvas_polygon_destroy;
 
121
 
 
122
        item_class->update = gnome_canvas_polygon_update;
 
123
}
 
124
 
 
125
static void
 
126
gnome_canvas_polygon_init (GnomeCanvasPolygon *poly)
 
127
{
 
128
        poly->path_def = NULL;
 
129
}
 
130
 
 
131
static void
 
132
gnome_canvas_polygon_destroy (GtkObject *object)
 
133
{
 
134
        GnomeCanvasPolygon *poly;
 
135
 
 
136
        g_return_if_fail (object != NULL);
 
137
        g_return_if_fail (GNOME_IS_CANVAS_POLYGON (object));
 
138
 
 
139
        poly = GNOME_CANVAS_POLYGON (object);
 
140
 
 
141
        /* remember, destroy can be run multiple times! */
 
142
 
 
143
        if(poly->path_def)
 
144
                gnome_canvas_path_def_unref(poly->path_def);
 
145
 
 
146
        poly->path_def = NULL;
 
147
 
 
148
 
 
149
        if (GTK_OBJECT_CLASS (parent_class)->destroy)
 
150
                (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
 
151
}
 
152
 
 
153
static void
 
154
set_points (GnomeCanvasPolygon *poly, GnomeCanvasPoints *points)
 
155
{
 
156
        int i;
 
157
 
 
158
 
 
159
        if (poly->path_def)
 
160
                gnome_canvas_path_def_unref(poly->path_def);
 
161
 
 
162
        if (!points) {
 
163
                poly->path_def = gnome_canvas_path_def_new();
 
164
                gnome_canvas_shape_set_path_def (GNOME_CANVAS_SHAPE (poly), poly->path_def);
 
165
                return;
 
166
        }
 
167
 
 
168
 
 
169
        /* Optomize the path def to the number of points */
 
170
        poly->path_def = gnome_canvas_path_def_new_sized(points->num_points+1);
 
171
 
 
172
#if 0
 
173
        /* No need for explicit duplicate, as closepaths does it for us (Lauris) */
 
174
        /* See if we need to duplicate the first point */
 
175
        duplicate = ((points->coords[0] != points->coords[2 * points->num_points - 2])
 
176
                     || (points->coords[1] != points->coords[2 * points->num_points - 1]));
 
177
#endif
 
178
 
 
179
        
 
180
        gnome_canvas_path_def_moveto (poly->path_def, points->coords[0], points->coords[1]);
 
181
        
 
182
        for (i = 1; i < points->num_points; i++) {
 
183
                gnome_canvas_path_def_lineto(poly->path_def, points->coords[i * 2], points->coords[(i * 2) + 1]);
 
184
        }
 
185
 
 
186
        gnome_canvas_path_def_closepath (poly->path_def);
 
187
 
 
188
        gnome_canvas_shape_set_path_def (GNOME_CANVAS_SHAPE (poly), poly->path_def);
 
189
}
 
190
 
 
191
 
 
192
static void
 
193
gnome_canvas_polygon_set_property (GObject              *object,
 
194
                                   guint                 param_id,
 
195
                                   const GValue         *value,
 
196
                                   GParamSpec           *pspec)
 
197
{
 
198
        GnomeCanvasItem *item;
 
199
        GnomeCanvasPolygon *poly;
 
200
        GnomeCanvasPoints *points;
 
201
 
 
202
        g_return_if_fail (object != NULL);
 
203
        g_return_if_fail (GNOME_IS_CANVAS_POLYGON (object));
 
204
 
 
205
        item = GNOME_CANVAS_ITEM (object);
 
206
        poly = GNOME_CANVAS_POLYGON (object);
 
207
 
 
208
        switch (param_id) {
 
209
        case PROP_POINTS:
 
210
                points = g_value_get_boxed (value);
 
211
 
 
212
                set_points (poly, points);
 
213
 
 
214
                gnome_canvas_item_request_update (item);
 
215
                break;
 
216
        default:
 
217
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
218
                break;
 
219
        }
 
220
}
 
221
 
 
222
 
 
223
static void
 
224
gnome_canvas_polygon_get_property (GObject              *object,
 
225
                                   guint                 param_id,
 
226
                                   GValue               *value,
 
227
                                   GParamSpec           *pspec)
 
228
{
 
229
        GnomeCanvasPolygon *poly;
 
230
 
 
231
        g_return_if_fail (object != NULL);
 
232
        g_return_if_fail (GNOME_IS_CANVAS_POLYGON (object));
 
233
 
 
234
        poly = GNOME_CANVAS_POLYGON (object);
 
235
 
 
236
        switch (param_id) {
 
237
        case PROP_POINTS:
 
238
                break;
 
239
        default:
 
240
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
 
241
                break;
 
242
        }
 
243
}
 
244
 
 
245
 
 
246
static void
 
247
gnome_canvas_polygon_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
 
248
{
 
249
        /* Since the path has already been defined just pass the update up. */
 
250
 
 
251
        if (parent_class->update)
 
252
                (* parent_class->update) (item, affine, clip_path, flags);
 
253
}