2006-12-03 Dimitris Glezos <dimitris@glezos.com>
[dia.git] / lib / create.c
blobae8022a5e28945fc79a0e95537eba50bf8960387
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 2006 Lars Clausen
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 /** This file contains functions for importers in particular to create
20 * standard objects.
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <string.h>
28 #include <math.h>
29 #include <glib.h>
30 #include <stdlib.h>
32 #include "intl.h"
33 #include "message.h"
34 #include "geometry.h"
35 #include "filter.h"
36 #include "object.h"
37 #include "properties.h"
38 #include "propinternals.h"
39 #include "group.h"
40 #include "create.h"
42 DiaObject *
43 create_standard_text(real xpos, real ypos) {
44 DiaObjectType *otype = object_get_type("Standard - Text");
45 DiaObject *new_obj;
46 Handle *h1, *h2;
47 Point point;
49 if (otype == NULL){
50 message_error(_("Can't find standard object"));
51 return NULL;
54 point.x = xpos;
55 point.y = ypos;
57 new_obj = otype->ops->create(&point, otype->default_user_data,
58 &h1, &h2);
60 return new_obj;
63 static PropDescription create_element_prop_descs[] = {
64 { "elem_corner", PROP_TYPE_POINT },
65 { "elem_width", PROP_TYPE_REAL },
66 { "elem_height", PROP_TYPE_REAL },
67 PROP_DESC_END};
69 static GPtrArray *make_element_props(real xpos, real ypos,
70 real width, real height)
72 GPtrArray *props;
73 PointProperty *pprop;
74 RealProperty *rprop;
76 props = prop_list_from_descs(create_element_prop_descs,pdtpp_true);
77 g_assert(props->len == 3);
79 pprop = g_ptr_array_index(props,0);
80 pprop->point_data.x = xpos;
81 pprop->point_data.y = ypos;
82 rprop = g_ptr_array_index(props,1);
83 rprop->real_data = width;
84 rprop = g_ptr_array_index(props,2);
85 rprop->real_data = height;
87 return props;
90 DiaObject *
91 create_standard_ellipse(real xpos, real ypos, real width, real height) {
92 DiaObjectType *otype = object_get_type("Standard - Ellipse");
93 DiaObject *new_obj;
94 Handle *h1, *h2;
96 GPtrArray *props;
97 Point point;
99 if (otype == NULL){
100 message_error(_("Can't find standard object"));
101 return NULL;
104 point.x = xpos;
105 point.y = ypos;
107 new_obj = otype->ops->create(&point, otype->default_user_data,
108 &h1, &h2);
110 props = make_element_props(xpos,ypos,width,height);
111 new_obj->ops->set_props(new_obj, props);
112 prop_list_free(props);
114 return new_obj;
118 DiaObject *
119 create_standard_box(real xpos, real ypos, real width, real height) {
120 DiaObjectType *otype = object_get_type("Standard - Box");
121 DiaObject *new_obj;
122 Handle *h1, *h2;
123 Point point;
124 GPtrArray *props;
126 if (otype == NULL){
127 message_error(_("Can't find standard object"));
128 return NULL;
131 point.x = xpos;
132 point.y = ypos;
134 new_obj = otype->ops->create(&point, otype->default_user_data,
135 &h1, &h2);
137 props = make_element_props(xpos,ypos,width,height);
138 new_obj->ops->set_props(new_obj, props);
139 prop_list_free(props);
141 return new_obj;
144 static PropDescription create_line_prop_descs[] = {
145 PROP_STD_START_ARROW,
146 PROP_STD_END_ARROW,
147 PROP_DESC_END};
149 DiaObject *
150 create_standard_polyline(int num_points,
151 Point *points,
152 Arrow *end_arrow,
153 Arrow *start_arrow) {
154 DiaObjectType *otype = object_get_type("Standard - PolyLine");
155 DiaObject *new_obj;
156 Handle *h1, *h2;
157 MultipointCreateData *pcd;
158 GPtrArray *props;
160 if (otype == NULL){
161 message_error(_("Can't find standard object"));
162 return NULL;
165 pcd = g_new(MultipointCreateData, 1);
166 pcd->num_points = num_points;
167 pcd->points = points;
169 new_obj = otype->ops->create(NULL, pcd,
170 &h1, &h2);
172 g_free(pcd);
174 props = prop_list_from_descs(create_line_prop_descs,pdtpp_true);
175 g_assert(props->len == 2);
177 if (start_arrow != NULL)
178 ((ArrowProperty *)g_ptr_array_index(props, 0))->arrow_data = *start_arrow;
179 if (end_arrow != NULL)
180 ((ArrowProperty *)g_ptr_array_index(props, 1))->arrow_data = *end_arrow;
182 new_obj->ops->set_props(new_obj, props);
183 prop_list_free(props);
185 return new_obj;
188 DiaObject *
189 create_standard_polygon(int num_points,
190 Point *points) {
191 DiaObjectType *otype = object_get_type("Standard - Polygon");
192 DiaObject *new_obj;
193 Handle *h1, *h2;
194 MultipointCreateData *pcd;
196 if (otype == NULL){
197 message_error(_("Can't find standard object"));
198 return NULL;
201 pcd = g_new(MultipointCreateData, 1);
202 pcd->num_points = num_points;
203 pcd->points = points;
205 new_obj = otype->ops->create(NULL, pcd, &h1, &h2);
207 g_free(pcd);
209 return new_obj;
212 DiaObject *
213 create_standard_bezierline(int num_points,
214 BezPoint *points,
215 Arrow *end_arrow,
216 Arrow *start_arrow) {
217 DiaObjectType *otype = object_get_type("Standard - BezierLine");
218 DiaObject *new_obj;
219 Handle *h1, *h2;
220 BezierCreateData *bcd;
221 GPtrArray *props;
223 if (otype == NULL){
224 message_error(_("Can't find standard object"));
225 return NULL;
228 bcd = g_new(BezierCreateData, 1);
229 bcd->num_points = num_points;
230 bcd->points = points;
232 new_obj = otype->ops->create(NULL, bcd,
233 &h1, &h2);
235 g_free(bcd);
237 props = prop_list_from_descs(create_line_prop_descs,pdtpp_true);
238 g_assert(props->len == 2);
240 if (start_arrow != NULL)
241 ((ArrowProperty *)g_ptr_array_index(props, 0))->arrow_data = *start_arrow;
242 if (end_arrow != NULL)
243 ((ArrowProperty *)g_ptr_array_index(props, 1))->arrow_data = *end_arrow;
245 new_obj->ops->set_props(new_obj, props);
246 prop_list_free(props);
248 return new_obj;
251 DiaObject *
252 create_standard_beziergon(int num_points,
253 BezPoint *points) {
254 DiaObjectType *otype = object_get_type("Standard - Beziergon");
255 DiaObject *new_obj;
256 Handle *h1, *h2;
257 BezierCreateData *bcd;
259 if (otype == NULL){
260 message_error(_("Can't find standard object"));
261 return NULL;
264 bcd = g_new(BezierCreateData, 1);
265 bcd->num_points = num_points;
266 bcd->points = points;
268 new_obj = otype->ops->create(NULL, bcd,
269 &h1, &h2);
271 g_free(bcd);
273 return new_obj;
277 static PropDescription create_arc_prop_descs[] = {
278 { "curve_distance", PROP_TYPE_REAL },
279 PROP_STD_START_ARROW,
280 PROP_STD_END_ARROW,
281 PROP_DESC_END};
283 DiaObject *
284 create_standard_arc(real x1, real y1, real x2, real y2,
285 real radius,
286 Arrow *end_arrow,
287 Arrow *start_arrow) {
288 DiaObjectType *otype = object_get_type("Standard - Arc");
289 DiaObject *new_obj;
290 Handle *h1, *h2;
291 Point point;
292 GPtrArray *props;
294 if (otype == NULL){
295 message_error(_("Can't find standard object"));
296 return NULL;
299 point.x = x1;
300 point.y = y1;
302 new_obj = otype->ops->create(&point, otype->default_user_data,
303 &h1, &h2);
305 props = prop_list_from_descs(create_arc_prop_descs,pdtpp_true);
306 g_assert(props->len == 3);
308 ((RealProperty *)g_ptr_array_index(props,0))->real_data = radius;
309 if (start_arrow != NULL)
310 ((ArrowProperty *)g_ptr_array_index(props, 1))->arrow_data = *start_arrow;
311 if (end_arrow != NULL)
312 ((ArrowProperty *)g_ptr_array_index(props, 2))->arrow_data = *end_arrow;
314 new_obj->ops->set_props(new_obj, props);
315 prop_list_free(props);
317 return new_obj;
320 static PropDescription create_file_prop_descs[] = {
321 { "image_file", PROP_TYPE_FILE },
322 PROP_DESC_END};
324 DiaObject *
325 create_standard_image(real xpos, real ypos, real width, real height,
326 char *file) {
327 DiaObjectType *otype = object_get_type("Standard - Image");
328 DiaObject *new_obj;
329 Handle *h1, *h2;
330 Point point;
331 GPtrArray *props;
332 StringProperty *sprop;
334 if (otype == NULL){
335 message_error(_("Can't find standard object"));
336 return NULL;
339 point.x = xpos;
340 point.y = ypos;
342 new_obj = otype->ops->create(&point, otype->default_user_data,
343 &h1, &h2);
345 props = make_element_props(xpos,ypos,width,height);
346 new_obj->ops->set_props(new_obj, props);
347 prop_list_free(props);
350 props = prop_list_from_descs(create_file_prop_descs,pdtpp_true);
351 g_assert(props->len == 1);
352 sprop = g_ptr_array_index(props,0);
353 g_free(sprop->string_data);
354 sprop->string_data = g_strdup(file);
355 new_obj->ops->set_props(new_obj, props);
356 prop_list_free(props);
358 return new_obj;
361 DiaObject *
362 create_standard_group(GList *items) {
363 DiaObject *new_obj;
365 new_obj = group_create((GList*)items);
367 return new_obj;