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