1 /* Dia -- a diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * Property system for dia objects/shapes.
5 * Copyright (C) 2000 James Henstridge
6 * Copyright (C) 2001 Cyrille Chepelov
7 * Major restructuration done in August 2001 by C. Chepelov
9 * Propobject.c: routines which deal with Dia Objects and affect their
12 * Most of these routines used to exist in code before the restructuration.
13 * They've lost most of their meat, in favour for more modularity.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include "properties.h"
38 #include "propinternals.h"
40 const PropDescription
*
41 object_get_prop_descriptions(const DiaObject
*obj
) {
42 const PropDescription
*pdesc
;
43 if (!obj
->ops
->describe_props
) return NULL
;
45 pdesc
= obj
->ops
->describe_props((DiaObject
*)obj
); /* Yes... */
46 if (pdesc
[0].quark
!= 0) return pdesc
;
48 prop_desc_list_calculate_quarks((PropDescription
*)pdesc
); /* Yes again... */
53 /* ------------------------------------------------------ */
54 /* Change management */
56 /* an ObjectChange structure for setting of properties */
57 typedef struct _ObjectPropChange ObjectPropChange
;
58 struct _ObjectPropChange
{
59 ObjectChange obj_change
;
62 GPtrArray
*saved_props
;
66 object_prop_change_apply_revert(ObjectPropChange
*change
, DiaObject
*obj
)
70 old_props
= prop_list_copy_empty(change
->saved_props
);
72 if (change
->obj
->ops
->get_props
)
73 change
->obj
->ops
->get_props(change
->obj
, old_props
);
75 /* set saved property values */
76 if (change
->obj
->ops
->set_props
)
77 change
->obj
->ops
->set_props(change
->obj
, change
->saved_props
);
79 /* move old props to saved properties */
80 prop_list_free(change
->saved_props
);
81 change
->saved_props
= old_props
;
85 object_prop_change_free(ObjectPropChange
*change
)
87 prop_list_free(change
->saved_props
);
91 object_apply_props(DiaObject
*obj
, GPtrArray
*props
)
93 ObjectPropChange
*change
;
96 change
= g_new0(ObjectPropChange
, 1);
98 change
->obj_change
.apply
=
99 (ObjectChangeApplyFunc
) object_prop_change_apply_revert
;
100 change
->obj_change
.revert
=
101 (ObjectChangeRevertFunc
) object_prop_change_apply_revert
;
102 change
->obj_change
.free
=
103 (ObjectChangeFreeFunc
) object_prop_change_free
;
107 /* create new properties structure with current values */
108 old_props
= prop_list_copy_empty(props
);
110 if (obj
->ops
->get_props
)
111 obj
->ops
->get_props(obj
, old_props
);
113 /* set saved property values */
114 if (obj
->ops
->set_props
)
115 obj
->ops
->set_props(obj
, props
);
117 change
->saved_props
= old_props
;
119 return (ObjectChange
*)change
;
123 /* Get/Set routines */
126 object_get_props_from_offsets(DiaObject
*obj
, PropOffset
*offsets
,
129 prop_offset_list_calculate_quarks(offsets
);
130 do_get_props_from_offsets(obj
,props
,offsets
);
136 object_set_props_from_offsets(DiaObject
*obj
, PropOffset
*offsets
,
139 prop_offset_list_calculate_quarks(offsets
);
140 do_set_props_from_offsets(obj
,props
,offsets
);
147 object_create_props_dialog(DiaObject
*obj
, gboolean is_default
)
149 return prop_dialog_new(obj
, is_default
)->widget
;
154 object_apply_props_from_dialog(DiaObject
*obj
, WIDGET
*dialog_widget
)
156 PropDialog
*dialog
= prop_dialog_from_widget(dialog_widget
);
158 prop_get_data_from_widgets(dialog
);
159 return object_apply_props(obj
, dialog
->props
);
164 object_complies_with_stdprop(const DiaObject
*obj
)
166 if (obj
->ops
->set_props
== NULL
) {
167 g_warning("No set_props !");
170 if (obj
->ops
->get_props
== NULL
) {
171 g_warning("No get_props !");
174 if (obj
->ops
->describe_props
== NULL
) {
175 g_warning("No describe_props !");
178 if (object_get_prop_descriptions(obj
) == NULL
) {
179 g_warning("No properties !");
186 object_copy_props(DiaObject
*dest
, const DiaObject
*src
, gboolean is_default
)
190 g_return_if_fail(src
!= NULL
);
191 g_return_if_fail(dest
!= NULL
);
192 g_return_if_fail(strcmp(src
->type
->name
,dest
->type
->name
)==0);
193 g_return_if_fail(src
->ops
== dest
->ops
);
194 g_return_if_fail(object_complies_with_stdprop(src
));
195 g_return_if_fail(object_complies_with_stdprop(dest
));
197 props
= prop_list_from_descs(object_get_prop_descriptions(src
),
198 (is_default
?pdtpp_do_save_no_standard
:
201 src
->ops
->get_props((DiaObject
*)src
, props
); /* FIXME: really should make
202 get_props' first argument
203 a (const DiaObject *) */
204 dest
->ops
->set_props(dest
, props
);
206 prop_list_free(props
);
210 object_load_props(DiaObject
*obj
, ObjectNode obj_node
)
215 g_return_if_fail(obj
!= NULL
);
216 g_return_if_fail(obj_node
!= NULL
);
217 g_return_if_fail(object_complies_with_stdprop(obj
));
219 props
= prop_list_from_descs(object_get_prop_descriptions(obj
),
222 if (!prop_list_load(props
,obj_node
, &err
)) {
223 g_warning ("%s: %s", obj
->type
->name
, err
->message
);
227 obj
->ops
->set_props(obj
, props
);
228 prop_list_free(props
);
232 object_save_props(DiaObject
*obj
, ObjectNode obj_node
)
236 g_return_if_fail(obj
!= NULL
);
237 g_return_if_fail(obj_node
!= NULL
);
238 g_return_if_fail(object_complies_with_stdprop(obj
));
240 props
= prop_list_from_descs(object_get_prop_descriptions(obj
),
243 obj
->ops
->get_props(obj
, props
);
244 prop_list_save(props
,obj_node
);
245 prop_list_free(props
);
249 object_prop_by_name_type(DiaObject
*obj
, const char *name
, const char *type
)
251 const PropDescription
*pdesc
;
252 GQuark name_quark
= g_quark_from_string(name
);
254 if (!object_complies_with_stdprop(obj
)) return NULL
;
256 for (pdesc
= object_get_prop_descriptions(obj
);
259 if ((pdesc
->quark
== name_quark
)) {
261 static GPtrArray
*plist
= NULL
;
263 if (type
&& (0 != strcmp(pdesc
->type
,type
))) continue;
266 plist
= g_ptr_array_new();
267 g_ptr_array_set_size(plist
,1);
269 prop
= pdesc
->ops
->new_prop(pdesc
,pdtpp_from_object
);
270 g_ptr_array_index(plist
,0) = prop
;
271 obj
->ops
->get_props(obj
,plist
);
279 object_prop_by_name(DiaObject
*obj
, const char *name
)
281 return object_prop_by_name_type(obj
,name
,NULL
);