Signal patch
[dia.git] / lib / parent.c
blob340520a701b623b9023060fced4114f48007e23e
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 2003 Vadim Berezniker
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 #include <config.h>
21 #include "parent.h"
22 #include "geometry.h"
23 #include "object.h"
24 #include <glib.h>
28 Takes a list of objects and appends all possible children (at any depth) to the list
30 Returns TRUE if the list didn't change
32 gboolean parent_list_expand(GList *obj_list)
34 gboolean nothing_affected = FALSE;
35 GList *list = obj_list;
36 while (list)
38 DiaObject *obj = (DiaObject *) list->data;
40 if (obj->can_parent && obj->children)
42 obj_list = g_list_concat(obj_list, g_list_copy(obj->children));
43 nothing_affected = FALSE;
46 list = g_list_next(list);
49 return nothing_affected;
52 /**
53 * Returns the original list minus any items that appear as children
54 * (at any depth) of the objects in the original list. This is very
55 * different from the parent_list_affected function, which returns a
56 * list of ALL objects affected.
58 * The caller must call g_list_free() on the returned list
59 * when the list is no longer needed.
62 GList *parent_list_affected_hierarchy(GList *obj_list)
64 GHashTable *object_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
65 GList *all_list = g_list_copy(obj_list);
66 GList *new_list = NULL, *list = all_list;
67 int orig_length = g_list_length(obj_list);
69 if (parent_list_expand(all_list)) /* fast way out */
70 return g_list_copy(obj_list);
72 /* enter all of the objects (except initial) in a hash */
73 list = g_list_nth(all_list, orig_length);
74 while (list)
76 g_hash_table_insert(object_hash, list->data, (void*) 1);
77 list = g_list_next(list);
80 list = obj_list;
81 while (list)
83 if (!g_hash_table_lookup(object_hash, list->data))
84 new_list = g_list_append(new_list, list->data);
86 list = g_list_next(list);
89 g_list_free(all_list);
90 g_hash_table_destroy(object_hash);
92 return new_list;
96 Finds all children of affected objects that are not in the list that should be
97 affected by an operation
99 The caller must call g_list_free() on the returned list
100 when the list is no longer needed
102 Any found affected children will be appended to the end of the list,
103 thus the front of the list is exactly the same as the passed in list.
106 GList *parent_list_affected(GList *obj_list)
108 GHashTable *object_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
109 GList *all_list = g_list_copy(obj_list);
110 GList *new_list = NULL, *list = all_list;
112 if (parent_list_expand(all_list)) /* fast way out */
113 return g_list_copy(obj_list);
115 /* eliminate duplicates */
116 list = all_list;
117 while (list)
119 DiaObject *obj = (DiaObject *) list->data;
120 if (!g_hash_table_lookup(object_hash, obj))
122 new_list = g_list_append(new_list, obj);
123 g_hash_table_insert(object_hash, obj, (void *) 1);
126 list = g_list_next(list);
129 g_list_free(all_list);
131 return new_list;
134 /* if the object is resizing up and hits its parents' border, prevent the resizing from
135 going any further
137 returns TRUE if resizing was obstructed*/
138 gboolean
139 parent_handle_move_out_check(DiaObject *object, Point *to)
141 Rectangle p_ext, c_ext;
142 Point new_delta;
144 if (!object->parent)
145 return FALSE;
147 parent_handle_extents(object->parent, &p_ext);
148 parent_point_extents(to, &c_ext);
150 new_delta = parent_move_child_delta(&p_ext, &c_ext, NULL);
151 point_add(to, &new_delta);
153 if (new_delta.x || new_delta.y)
154 return TRUE;
156 return FALSE;
159 /* if the object resizing down intersects a child, the resizing is prevented from going
160 any further
162 returns TRUE if resizing was obstructed
164 gboolean parent_handle_move_in_check(DiaObject *object, Point *to, Point *start_at)
166 GList *list = object->children;
167 gboolean once = TRUE;
168 Rectangle common_ext;
169 Rectangle p_ext;
170 Point new_delta;
172 if (!object->can_parent || !object->children)
173 return FALSE;
175 parent_point_extents(to, &p_ext);
176 while (list)
178 if (once) {
179 parent_handle_extents(list->data, &common_ext);
180 once = FALSE;
181 } else {
182 parent_handle_extents (list->data, &p_ext);
183 rectangle_union(&common_ext, &p_ext);
185 list = g_list_next(list);
187 new_delta = parent_move_child_delta_out(&p_ext, &common_ext, start_at);
188 point_add(to, &new_delta);
190 if (new_delta.x || new_delta.y)
191 return TRUE;
193 return FALSE;
196 /* this function is the opposite of parent_move_child_delta()
197 this function makes sure that the parent is OUTSIDE the child's "extent"
198 and if it's inside, it returns a delta that moves it back out
199 Also the last parameter is used as a direction guide FIX ME FIX ME FIX ME FIX ME FIX ME FIX ME
200 (the move is performed in the opposite direction of the passed parameter)
202 Point parent_move_child_delta_out(Rectangle *p_ext, Rectangle *c_ext, const Point *start)
204 Point direction;
205 Point new_delta = {0, 0};
207 direction.x = p_ext->left - start->x;
208 direction.y = p_ext->top - start->y;
210 /* if the start point is to the left of child and we're moving to the right */
211 if (start->x <= c_ext->left && direction.x > 0 && p_ext->left > c_ext->left)
212 new_delta.x = c_ext->left - p_ext->left;
213 /* if the start point is to the right of the child and we're moving left */
214 else if (start->x >= c_ext->right && direction.x < 0 && p_ext->left < c_ext->right)
215 new_delta.x = c_ext->right - p_ext->left;
217 /* if the start point is above the child and we're moving down */
218 if (start->y <= c_ext->top && direction.y > 0 && p_ext->top > c_ext->top)
219 new_delta.y = c_ext->top - p_ext->top;
220 /* if the start poit is below the child and we're moving up */
221 else if (start->y >= c_ext->bottom && direction.y < 0 && p_ext->bottom < c_ext->bottom)
222 new_delta.y = c_ext->bottom - p_ext->bottom;
224 return new_delta;
227 /* p_ext are the "extents" of the parent and c_ext are the "extents" of the child
228 The extent is a rectangle that specifies how far an object goes on the grid (based on its handles)
229 If delta is not present, these are the extents *before* any moves
230 If delta is present, delta is considered into the extents's position
232 Point parent_move_child_delta(Rectangle *p_ext, Rectangle *c_ext, Point *delta)
234 Point new_delta = {0, 0};
235 gboolean free_delta = FALSE;
236 if (delta == NULL)
238 delta = g_new0(Point, 1);
239 free_delta = TRUE;
241 /* we check if the child extent would be inside the parent extent after the move
242 if not, we calculate how far we have to move the extent back to place it back
243 inside the parent extent */
244 if (c_ext->left + delta->x < p_ext->left )
245 new_delta.x = p_ext->left - (c_ext->left + delta->x);
246 else if (c_ext->left + delta->x + (c_ext->right - c_ext->left) > p_ext->right)
247 new_delta.x = p_ext->right - (c_ext->left + delta->x + (c_ext->right - c_ext->left));
249 if (c_ext->top + delta->y < p_ext->top)
250 new_delta.y = p_ext->top - (c_ext->top + delta->y);
251 else if (c_ext->top + delta->y + (c_ext->bottom - c_ext->top) > p_ext->bottom)
252 new_delta.y = p_ext->bottom - (c_ext->top + delta->y + (c_ext->bottom - c_ext->top));
254 if (free_delta)
255 g_free(delta);
257 return new_delta;
260 /* the caller must free the returned rectangle */
261 void
262 parent_point_extents(Point *point, Rectangle *extents)
264 extents->left = point->x;
265 extents->right = point->x;
266 extents->top = point->y;
267 extents->bottom = point->y;
271 * the caller must provide the 'returned' rectangle,
272 * which is initialized to the biggest rectangle containing
273 * all the objects handles
275 gboolean
276 parent_handle_extents(DiaObject *obj, Rectangle *extents)
278 int idx;
279 coord *left_most = NULL, *top_most = NULL, *bottom_most = NULL, *right_most = NULL;
281 if (obj->num_handles == 0)
282 return FALSE;
284 for (idx = 0; idx < obj->num_handles; idx++)
286 Handle *handle = obj->handles[idx];
288 if (!left_most || *left_most > handle->pos.x)
289 left_most = &handle->pos.x;
290 if (!right_most || *right_most < handle->pos.x)
291 right_most = &handle->pos.x;
292 if (!top_most || *top_most > handle->pos.y)
293 top_most = &handle->pos.y;
294 if (!bottom_most || *bottom_most < handle->pos.y)
295 bottom_most = &handle->pos.y;
298 extents->left = *left_most;
299 extents->right = *right_most;
300 extents->top = *top_most;
301 extents->bottom = *bottom_most;
303 return TRUE;