pre3 updates
[dia.git] / lib / object.h
blob56ee28d4d01ebdc5a95eed91f9fc2e0a63a8553e
1 /* Dia -- an diagram creation/manipulation program -*- c -*-
2 * Copyright (C) 1998 Alexander Larsson
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 /** @file object.h -- definitions for Dia objects, in particular the 'virtual'
20 * functions and the object and type structures.
22 #ifndef OBJECT_H
23 #define OBJECT_H
25 #include "diatypes.h"
26 #include <gtk/gtk.h>
28 #include "geometry.h"
29 #include "connectionpoint.h"
30 #include "handle.h"
31 #include "diamenu.h"
32 #include "objchange.h"
33 #include "dia_xml.h"
34 #include "parent.h"
35 #include "text.h"
37 G_BEGIN_DECLS
39 /** This enumeration gives a bitset of modifier keys currently held down.
41 typedef enum {
42 MODIFIER_NONE,
43 MODIFIER_LEFT_SHIFT,
44 MODIFIER_RIGHT_SHIFT,
45 MODIFIER_SHIFT,
46 MODIFIER_LEFT_ALT = 4,
47 MODIFIER_RIGHT_ALT = 8,
48 MODIFIER_ALT = 12,
49 MODIFIER_LEFT_CONTROL = 16,
50 MODIFIER_RIGHT_CONTROL = 32,
51 MODIFIER_CONTROL = 48
52 } ModifierKeys;
54 /************************************
55 ** Some general function prototypes
57 ** These are the prototypes for the
58 ** functions that must be defined for
59 ** every object we can insert in a
60 ** diagram.
62 ************************************/
64 /*** Object type (class) operations ***/
66 /** Function called to create an object.
67 * This function is responsible for allocation memory for the object.
68 * This is one of the object class functions.
69 * @param startpoint : initial position for the object
70 * @param user_data : Can be used to pass extra params to the creation
71 * of the object. Can be used to have two icons of
72 * the same object that are instansiated a bit different.
73 * @param handle1 : (return) Handle connected to startpoint
74 * @param handle2 : (return) Handle dragged on creation
75 * both handle1 and handle2 can be NULL
76 * @return A newly created object.
78 typedef DiaObject* (*CreateFunc) (Point *startpoint,
79 void *user_data,
80 Handle **handle1,
81 Handle **handle2);
83 /** This function load the object's data from file fd. No header has to be
84 * skipped. The data should be read using the functions in lib/files.h
85 * The memory for the object has to be allocated (see CreateFunc)
86 * The version number is the version number of the DiaObjectType that was saved.
87 * This must be used to maintain backwards compatible if you change some
88 * in the save format. All objects must be capable of reading all earlier
89 * version.
90 * This is one of the object class functions.
91 * @param obj_node A node in an XML object to load from.
92 * @param version The version of the object found in the XML file.
93 * @param filename The name of the file we're loading from, for use in
94 * error messages.
96 typedef DiaObject* (*LoadFunc) (ObjectNode obj_node, int version,
97 const char *filename);
99 /** This function save the object's data to file fd. No header is required.
100 * The data should be written using the functions in lib/files.h
101 * This is one of the object class functions.
102 * @param obj An object to save.
103 * @param obj_node An XML node to save it in.
104 * @param filename The name of the file we're saving to, for use in error
105 * messages.
107 typedef void (*SaveFunc) (DiaObject* obj, ObjectNode obj_node,
108 const char *filename);
110 /** Function called when the user has double clicked on an Tool.
111 * When this function is called and the dialog already is created,
112 * make sure to update the values in the widgets so that it
113 * accurately describes the current state of the tool.
114 * This is one of the object class functions.
115 * @return a dialog that the user can use to edit the defaults for new
116 * objects of this type.
118 typedef GtkWidget *(*GetDefaultsFunc) ();
120 /** Function called when the user clicks Apply on an edit defaults dialog.
121 * This is currently not used by any object.
122 * This is one of the object class functions.
124 typedef void *(*ApplyDefaultsFunc) ();
126 /*** Object operations ***/
128 /** Function called before an object is deleted.
129 * This function must call the parent class's DestroyFunc, and then free
130 * the memory associated with the object, but not the object itself
131 * Must also unconnect itself from all other objects.
132 * (This is by calling object_destroy, or letting the super-class call it)
133 * This is one of the object_ops functions.
134 * @param obj An object to destroy.
136 typedef void (*DestroyFunc) (DiaObject* obj);
139 /** Function responsible for drawing the object.
140 * Every drawing must be done through the use of the Renderer, so that we
141 * can render the picture on screen, in an eps file, ...
142 * This is one of the object_ops functions.
143 * @param The object to draw.
144 * @param The renderer object to draw with.
146 typedef void (*DrawFunc) (DiaObject* obj, DiaRenderer* ddisp);
149 /** This function must return the distance between the DiaObject and the Point.
150 * Several functions are provided in geometry.h to facilitate this calculus.
151 * This is one of the object_ops functions.
152 * @param obj The object.
153 * @param point A point to give the distance to.
154 * @return The distance from the point to the nearest part of the object.
155 * If the point is inside a closed object, return 0.0.
157 typedef real (*DistanceFunc) (DiaObject* obj, Point* point);
160 /** Function called once the object has been selected.
161 * Basically, this function should update the object (position of the
162 * handles,...) This is one of the object_ops functions.
163 * This function should not redraw the object.
164 * @param obj An object that is being selected.
165 * @param clicked_point is the point on the screen where the user has clicked
166 * @param interactive_renderer is a renderer that has some extra functions
167 * most notably the possibility to get EXACT
168 * measures of strings. Used to place cursors
169 * and other interactive stuff.
170 * (Don't draw to the renderer)
172 typedef void (*SelectFunc) (DiaObject* obj,
173 Point* clicked_point,
174 DiaRenderer* interactive_renderer);
176 /** Returns a copy of DiaObject.
177 * This must be an depth-copy (pointers must be duplicated and so on)
178 * as the initial object can be deleted any time.
179 * This is one of the object_ops functions.
180 * @param obj An object to make a copy of.
181 * @return A newly allocated object copied from `obj', but without any
182 * connections to other objects.
184 typedef DiaObject* (*CopyFunc) (DiaObject* obj);
186 /** Function called to move the entire object.
187 * This is one of the object_ops functions.
188 * @param obj The object being moved.
189 * @param pos Where the object is being moved to.
190 * Its exact definition depends on the object. It is the point on the
191 * object that 'snaps' to the grid if that is enabled. (generally it
192 * is the upper left corner)
193 * @return An ObjectChange* with additional undo information, or
194 * (in most cases) NULL. Undo for moving the object itself is handled
195 * elsewhere.
197 typedef ObjectChange* (*MoveFunc) (DiaObject* obj, Point * pos);
199 /** Function called to move one of the handles associated with the
200 * object. This is one of the object_ops functions.
201 * @param obj The object whose handle is being moved.
202 * @param handle The handle being moved.
203 * @param pos The position it has been moved to (corrected for
204 * vertical/horizontal only movement).
205 * @param cp If non-NULL, the connectionpoint found at this position.
206 * If @a cp is NULL, there may or may not be a connectionpoint.
207 * @param The reason the handle was moved.
208 * - HANDLE_MOVE_USER means the user is dragging the point.
209 * - HANDLE_MOVE_USER_FINAL means the user let go of the point.
210 * - HANDLE_MOVE_CONNECTED means it was moved because something
211 * it was connected to moved.
212 * @param modifiers gives a bitset of modifier keys currently held down
213 * - MODIFIER_SHIFT is either shift key
214 * - MODIFIER_ALT is either alt key
215 * - MODIFIER_CONTROL is either control key
216 * Each has MODIFIER_LEFT_* and MODIFIER_RIGHT_* variants
217 * @return An @a ObjectChange* with additional undo information, or
218 * (in most cases) NULL. Undo for moving the handle itself is handled
219 * elsewhere.
221 typedef ObjectChange* (*MoveHandleFunc) (DiaObject* obj,
222 Handle* handle,
223 Point* pos,
224 ConnectionPoint* cp,
225 HandleMoveReason reason,
226 ModifierKeys modifiers);
228 /** Function called when the user has double clicked on an DiaObject.
229 * This is one of the object_ops functions.
230 * @param obj An obj that this dialog is being made for.
231 * @param is_default If true, this dialog is for object defaults, and
232 * the toolbox options should not be shown.
233 * @return A dialog to edit the properties of the object.
234 * When this function is called and the dialog already is created,
235 * make sure to update the values in the widgets so that it
236 * accurately describes the current state of the object.
237 * Remember to destroy this dialog when the object is destroyed!
239 * Note that if you want to use the same dialog multiple times,
240 * you should ref it first. Just run the following on the widget
241 * when you create it:
242 * gtk_object_ref(GTK_OBJECT(widget));
243 * gtk_object_sink(GTK_OBJECT(widget)); / * optional, but recommended * /
244 * If you don't do this, the widget will be destroyed when the
245 * properties dialog is closed.
247 typedef GtkWidget *(*GetPropertiesFunc) (DiaObject* obj, gboolean is_default);
249 /** This function is called when the user clicks on
250 * the "Apply" button. The widget parameter is the one created by
251 * the get_properties function.
252 * This is one of the object_ops functions.
253 * @param obj The object whose dialog has had its Apply button clicked.
254 * @param widget The properties dialog being applied.
255 * @return a Change that can be used for undo/redo.
256 * The returned change is already applied.
258 typedef ObjectChange *(*ApplyPropertiesFunc) (DiaObject* obj, GtkWidget *widget);
260 /** Desribe the properties that this object supports.
261 * This is one of the object_ops functions.
262 * @param obj The object whose properties we want described.
263 * @return a NULL-terminated array of property descriptions.
264 * As the const return implies the returned data is not owned by the caller.
266 typedef const PropDescription *(* DescribePropsFunc) (DiaObject *obj);
268 /** Get the actual values of the properties given.
269 * Note that the props array need not contain all the properties
270 * defined for the object, nor do all the properties in the array need be
271 * defined for the object. All properties in the props array that are
272 * actually set will be set.
273 * This is one of the object_ops functions.
274 * @param obj An object that delivers the values.
275 * @param props A list of Property objects whose values are to be set based
276 * on the objects internal data. The types for the objects are
277 * also being set as a side-effect.
279 typedef void (* GetPropsFunc) (DiaObject *obj, GPtrArray *props);
281 /** Set the object to have the values defined in the properties list.
282 * Note that the props array may contain more or fewer properties than the
283 * object defines, but only and all the ones defined for the object will
284 * be applied to the object.
285 * This is one of the object_ops functions.
286 * @param obj An object to update values on.
287 * @param props An array of Property objects whose values are to be set on
288 * the object.
290 typedef void (* SetPropsFunc) (DiaObject *obj, GPtrArray *props);
292 /** Return an object-specific menu with toggles etc. properly set.
293 * This is one of the object_ops functions.
294 * @param obj The object that is selected when the object menu is asked for.
295 * @param position Where the user clicked. This can be used to place whatever
296 * the menu point may create, such as new segment corners.
297 * @return A menu description with values set appropriately for this object.
298 * The description object must not be freed by the caller.
300 typedef DiaMenu *(*ObjectMenuFunc) (DiaObject* obj, Point *position);
302 /** Function for updates on a text part of an object. This function, if
303 * not null, will be called every time the text is changed or editing
304 * starts or stops.
305 * This is one of the object_ops functions.
306 * @param obj The self object
307 * @param text The text entry being edited
308 * @param state The state of the editing, either TEXT_EDIT_START,
309 * TEXT_EDIT_INSERT, TEXT_EDIT_DELETE, or TEXT_EDIT_END.
310 * @param textchange For TEXT_EDIT_INSERT, the text about to be inserted.
311 * For TEXT_EDIT_DELETE, the text about to be deleted.
312 * @return For TEXT_EDIT_INSERT and TEXT_EDIT_DELETE, TRUE this change
313 * will be allowed, FALSE otherwise. For TEXT_EDIT_START and TEXT_EDIT_END,
314 * the return value is ignored.
316 typedef gboolean (*TextEditFunc) (DiaObject *obj, Text *text, TextEditState state, gchar *textchange);
318 /*************************************
319 ** The functions provided in object.c
320 *************************************/
322 void object_init(DiaObject *obj, int num_handles, int num_connections);
323 void object_destroy(DiaObject *obj); /* Unconnects handles, so don't
324 free handles before calling. */
325 void object_copy(DiaObject *from, DiaObject *to);
327 void object_save(DiaObject *obj, ObjectNode obj_node);
328 void object_load(DiaObject *obj, ObjectNode obj_node);
330 GList *object_copy_list(GList *list);
331 ObjectChange* object_list_move_delta_r(GList *objects, Point *delta, gboolean affected);
332 ObjectChange* object_list_move_delta(GList *objects, Point *delta);
333 /** Rotate an object around a point. If center is NULL, the position of
334 * the object is used. */
335 ObjectChange* object_list_rotate(GList *objects, Point *center, real angle);
336 /** Scale an object around a point. If center is NULL, the position of
337 * the object is used. */
338 ObjectChange* object_list_scale(GList *objects, Point *center, real factor);
339 void destroy_object_list(GList *list);
340 void object_add_handle(DiaObject *obj, Handle *handle);
341 void object_add_handle_at(DiaObject *obj, Handle *handle, int pos);
342 void object_remove_handle(DiaObject *obj, Handle *handle);
343 void object_add_connectionpoint(DiaObject *obj, ConnectionPoint *conpoint);
344 void object_remove_connectionpoint(DiaObject *obj,
345 ConnectionPoint *conpoint);
346 void object_add_connectionpoint_at(DiaObject *obj,
347 ConnectionPoint *conpoint,
348 int pos);
349 void object_connect(DiaObject *obj, Handle *handle,
350 ConnectionPoint *conpoint);
351 void object_unconnect(DiaObject *connected_obj, Handle *handle);
352 void object_remove_connections_to(ConnectionPoint *conpoint);
353 void object_unconnect_all(DiaObject *connected_obj);
354 void object_registry_init(void);
355 void object_register_type(DiaObjectType *type);
356 void object_registry_foreach(GHFunc func, gpointer user_data);
357 DiaObjectType *object_get_type(char *name);
358 gchar *object_get_displayname (DiaObject* obj);
360 int object_return_false(DiaObject *obj); /* Just returns FALSE */
361 void *object_return_null(DiaObject *obj); /* Just returns NULL */
362 void object_return_void(DiaObject *obj); /* Just an empty function */
364 /* These functions can be used as a default implementation for an object which
365 can be completely described, loaded and saved through standard properties.
367 DiaObject *object_load_using_properties(const DiaObjectType *type,
368 ObjectNode obj_node, int version,
369 const char *filename);
370 void object_save_using_properties(DiaObject *obj, ObjectNode obj_node,
371 int version, const char *filename);
372 DiaObject *object_copy_using_properties(DiaObject *obj);
374 /*****************************************
375 ** The structures used to define an object
376 *****************************************/
379 * \private This is currently unused
381 * This structure defines an affine transformation that has been applied
382 * to this object. Affine transformations done on a group are performed
383 * on all objects in the group.
385 typedef struct _Affine {
386 real rotation;
387 real scale;
388 real translation;
389 } Affine;
393 \brief _DiaObject vtable
395 This structure gives access to the functions used to manipulate an object
396 See information above on the use of the functions
398 struct _ObjectOps {
399 DestroyFunc destroy;
400 DrawFunc draw;
401 DistanceFunc distance_from;
402 SelectFunc selectf;
403 CopyFunc copy;
404 MoveFunc move;
405 MoveHandleFunc move_handle;
406 GetPropertiesFunc get_properties;
407 ApplyPropertiesFunc apply_properties;
408 ObjectMenuFunc get_object_menu;
410 DescribePropsFunc describe_props;
411 GetPropsFunc get_props;
412 SetPropsFunc set_props;
414 TextEditFunc edit_text;
417 Unused places (for extension).
418 These should be NULL for now. In the future they might be used.
419 Then an older object will be binary compatible, because all new code
420 checks if new ops are supported (!= NULL)
422 void (*(unused[5]))(DiaObject *obj,...);
426 \class _DiaObject
428 \brief Base class for all of Dia's objects, i.e. diagram building blocks
430 The base class in the DiaObject hierarchy.
431 All information in this structure read-only
432 from the application point of view except
433 when connection objects. (Then handles and
434 connections are changed).
436 position is not necessarly the corner of the object, but rather
437 some 'good' spot on it which will be natural to snap to.
439 struct _DiaObject {
440 DiaObjectType *type; /*!< pointer to the registered type */
441 Point position; /*!< often but not necessarily the upper left corner of the object */
442 /** The area that contains all parts of the 'real' object, i.e. the parts
443 * that would be printed, exported to pixmaps etc. This is also used to
444 * determine the size of autofit scaling, so it should be as large as
445 * the objects without interactive bits and preferably no larger.
446 * The bounding_box will always contain this box.
447 * Do not access this field directly, but use dia_object_get_enclosing_box().
449 Rectangle bounding_box;
450 Affine affine; /*!< unused */
452 int num_handles;
453 Handle **handles;
455 int num_connections;
456 ConnectionPoint **connections;
458 ObjectOps *ops; /*!< pointer to the vtable */
460 Layer *parent_layer; /*< Back-pointer to the owning layer.
461 This may only be set by functions internal to
462 the layer, and accessed via
463 dia_object_get_parent_layer() */
464 DiaObject *parent; /*!< Back-pointer to DiaObject which is parenting this object. Can be NULL */
465 GList *children; /*!< In case this object is a parent of other object the children are listed here */
466 gboolean can_parent; /*!< Only some object support parenting, i.e. hosting other objects */
468 Color *highlight_color; /*!< The color that this object is currently
469 highlighted with, or NULL if it is not
470 highlighted. */
471 /** The area that contains all parts rendered interactively, so includes
472 * handles, bezier controllers etc. Despite historical difference, this
473 * should not be accessed directly, but through dia_object_get_bounding_box().
474 * Note that handles and connection points are not included by this, but
475 * added by that which needs it.
476 * Internal: If this is set to a 0x0 box, returns bounding_box. That is for
477 * those objects that don't actually calculate it, but can just use the BB.
478 * Since handles and CPs are not in the BB, that will be the case for most
479 * objects.
481 Rectangle enclosing_box;
485 * \brief Vtable for _DiaObjectType
487 struct _ObjectTypeOps {
488 CreateFunc create;
489 LoadFunc load;
490 SaveFunc save;
491 GetDefaultsFunc get_defaults;
492 ApplyDefaultsFunc apply_defaults;
494 Unused places (for extension).
495 These should be NULL for now. In the future they might be used.
496 Then an older object will be binary compatible, because all new code
497 checks if new ops are supported (!= NULL)
499 void (*(unused[10]))(DiaObject *obj,...);
503 \brief _DiaObject factory
505 Structure so that the ObjectFactory can create objects
506 of unknown type. (Read in from a shared lib.)
508 struct _DiaObjectType {
510 char *name; /*!< The type name should follow a pattern of '<module> - <class>' like "UML - Class" */
511 int version; /*!< DiaObjects must be backward compatible, i.e. support possibly older versions formats */
513 char **pixmap; /* Also put a pixmap in the sheet_object.
514 This one is used if not in sheet but in toolbar.
515 Stored in xpm format */
517 ObjectTypeOps *ops; /*!< pointer to the vtable */
519 char *pixmap_file; /* fallback if pixmap is NULL */
520 void *default_user_data; /* use this if no user data is specified in
521 the .sheet file */
524 /* base property stuff ... */
525 #define OBJECT_COMMON_PROPERTIES \
526 { "obj_pos", PROP_TYPE_POINT, 0, \
527 "Object position", "Where the object is located"}, \
528 { "obj_bb", PROP_TYPE_RECT, 0, \
529 "Object bounding box", "The bounding box of the object"}
531 #define OBJECT_COMMON_PROPERTIES_OFFSETS \
532 { "obj_pos", PROP_TYPE_POINT, offsetof(DiaObject, position) }, \
533 { "obj_bb", PROP_TYPE_RECT, offsetof(DiaObject, bounding_box) }
536 gboolean dia_object_defaults_load (const gchar *filename,
537 gboolean create_lazy);
538 void dia_object_default_make (const DiaObject *obj_from);
539 DiaObject *dia_object_default_get (const DiaObjectType *type);
540 DiaObject *dia_object_default_create (const DiaObjectType *type,
541 Point *startpoint,
542 void *user_data,
543 Handle **handle1,
544 Handle **handle2);
545 gboolean dia_object_defaults_save (const gchar *filename);
546 Layer *dia_object_get_parent_layer(DiaObject *obj);
547 gboolean dia_object_is_selected (const DiaObject *obj);
548 Rectangle *dia_object_get_bounding_box(const DiaObject *obj);
549 Rectangle *dia_object_get_enclosing_box(const DiaObject *obj);
550 /* The below are for debugging purposes only. */
551 gboolean dia_object_sanity_check(const DiaObject *obj, const gchar *msg);
553 G_END_DECLS
555 #endif /* OBJECT_H */