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