File: parent.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (288 lines) | stat: -rw-r--r-- 8,464 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 2003 Vadim Berezniker
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include "geometry.h"
#include "object.h"
#include "parent.h"
#include <glib.h>


/*
  Takes a list of objects and appends all possible children (at any depth) to the list

  Returns TRUE if the list didn't change
*/
gboolean parent_list_expand(GList *obj_list)
{
  gboolean nothing_affected = FALSE;
  GList *list = obj_list;
  while (list)
  {
    DiaObject *obj = (DiaObject *) list->data;

    if (object_flags_set(obj, DIA_OBJECT_CAN_PARENT) && obj->children)
    {
      obj_list = g_list_concat(obj_list, g_list_copy(obj->children));
      nothing_affected = FALSE;
    }

    list = g_list_next(list);
  }

  return nothing_affected;
}

/**
 * Returns the original list minus any items that appear as children
 * (at any depth) of the objects in the original list.  This is very
 * different from the parent_list_affected function, which returns a
 * list of ALL objects affected.

 * The caller must call g_list_free() on the returned list
 * when the list is no longer needed.
 */

GList *parent_list_affected_hierarchy(GList *obj_list)
{
  GHashTable *object_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
  GList *all_list = g_list_copy(obj_list);
  GList *new_list = NULL, *list;
  int orig_length = g_list_length(obj_list);

  if (parent_list_expand(all_list)) /* fast way out */
    return g_list_copy(obj_list);

  /* enter all of the objects (except initial) in a hash */
  list = g_list_nth(all_list, orig_length);
  while (list)
  {
    g_hash_table_insert(object_hash, list->data, (void*) 1);
    list  = g_list_next(list);
  }

  list = obj_list;
  while (list)
  {
    if (!g_hash_table_lookup(object_hash, list->data))
      new_list = g_list_append(new_list, list->data);

    list = g_list_next(list);
  }

  g_list_free(all_list);
  g_hash_table_destroy(object_hash);

  return new_list;
}

/*
   Finds all children of affected objects that are not in the list that should be
   affected by an operation

   The caller must call g_list_free() on the returned list
   when the list is no longer needed

   Any found affected children will be appended to the end of the list,
   thus the front of the list is exactly the same as the passed in list.
 */

GList *parent_list_affected(GList *obj_list)
{
  GHashTable *object_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
  GList *all_list = g_list_copy(obj_list);
  GList *new_list = NULL, *list;

  if (parent_list_expand(all_list)) /* fast way out */
    return g_list_copy(obj_list);

  /* eliminate duplicates */
  list = all_list;
  while (list)
  {
    DiaObject *obj = (DiaObject *) list->data;
    if (!g_hash_table_lookup(object_hash, obj))
    {
      new_list = g_list_append(new_list, obj);
      g_hash_table_insert(object_hash, obj, (void *) 1);
    }

    list  = g_list_next(list);
  }

  g_list_free(all_list);

  return new_list;
}

/* if the object is resizing up and hits its parents' border, prevent the resizing from
  going any further

  returns TRUE if resizing was obstructed*/
gboolean 
parent_handle_move_out_check(DiaObject *object, Point *to)
{
  Rectangle p_ext, c_ext;
  Point new_delta;

  if (!object->parent)
    return FALSE;

  parent_handle_extents(object->parent, &p_ext);
  parent_point_extents(to, &c_ext);

  new_delta = parent_move_child_delta(&p_ext, &c_ext, NULL);
  point_add(to, &new_delta);

  if (new_delta.x || new_delta.y)
    return TRUE;

  return FALSE;
}

/* if the object resizing down intersects a child, the resizing is prevented from going
   any further

   returns TRUE if resizing was obstructed
   */
gboolean parent_handle_move_in_check(DiaObject *object, Point *to, Point *start_at)
{
  GList *list = object->children;
  gboolean once = TRUE;
  Rectangle common_ext;
  gboolean restricted = FALSE;

  if (!object_flags_set(object, DIA_OBJECT_CAN_PARENT) || !list)
    return FALSE;

  while (list)
  {
    if (once) {
      parent_handle_extents(list->data, &common_ext);
      once = FALSE;
    } else {
      Rectangle c_ext;
      parent_handle_extents (list->data, &c_ext);
      rectangle_union(&common_ext, &c_ext);
    }
    list = g_list_next(list);
  }
  /* The start point gives the decision were to clip to:
   * if it is below the child rect we need to clip to it's bottom.
   * The check has nothing to do with 'to' being in the rectangle,
   * but instead having the right barrier
   */
  if (start_at->y >= common_ext.bottom) {
	if (to->y < common_ext.bottom)
	  to->y = common_ext.bottom, restricted = TRUE;
  } else if (start_at->y <= common_ext.top) {
	if (to->y > common_ext.top)
	  to->y = common_ext.top, restricted = TRUE;
  }

  if (start_at->x >= common_ext.right) {
	if (to->x < common_ext.right)
	  to->x = common_ext.right, restricted = TRUE;
  } else if (start_at->x <= common_ext.left) {
	if (to->x > common_ext.left)
	  to->x = common_ext.left, restricted = TRUE;
  }

  return restricted;
}

    /* p_ext are the "extents" of the parent and c_ext are the "extents" of the child
      The extent is a rectangle that specifies how far an object goes on the grid (based on its handles)
      If delta is not present, these are the extents *before* any moves
      If delta is present, delta is considered into the extents's position
      */
Point parent_move_child_delta(Rectangle *p_ext, Rectangle *c_ext, Point *delta)
{
    Point new_delta = {0, 0};
    gboolean free_delta = FALSE;
    if (delta == NULL)
    {
      delta = g_new0(Point, 1);
      free_delta = TRUE;
    }
    /* we check if the child extent would be inside the parent extent after the move
      if not, we calculate how far we have to move the extent back to place it back
      inside the parent extent */
    if (c_ext->left + delta->x < p_ext->left )
      new_delta.x = p_ext->left - (c_ext->left + delta->x);
    else if (c_ext->left + delta->x + (c_ext->right - c_ext->left) > p_ext->right)
      new_delta.x = p_ext->right - (c_ext->left + delta->x + (c_ext->right - c_ext->left));

    if (c_ext->top + delta->y < p_ext->top)
      new_delta.y = p_ext->top - (c_ext->top + delta->y);
    else if (c_ext->top + delta->y + (c_ext->bottom - c_ext->top) > p_ext->bottom)
      new_delta.y = p_ext->bottom  - (c_ext->top + delta->y + (c_ext->bottom - c_ext->top));

    if (free_delta)
      g_free(delta);

    return new_delta;
}

/* the caller must free the returned rectangle */
void
parent_point_extents(Point *point, Rectangle *extents)
{
  extents->left = point->x;
  extents->right = point->x;
  extents->top = point->y;
  extents->bottom = point->y;
}

/**
 * the caller must provide the 'returned' rectangle,
 * which is initialized to the biggest rectangle containing
 * all the objects handles
 */
void
parent_handle_extents(DiaObject *obj, Rectangle *extents)
{
  int idx;

  g_assert (obj->num_handles > 0);
  /* initialize the rectangle with the first handle */
  extents->left = extents->right = obj->handles[0]->pos.x;
  extents->top = extents->bottom = obj->handles[0]->pos.y;
  /* grow it with the other ones */
  for (idx = 1; idx < obj->num_handles; idx++) {
    Handle *handle = obj->handles[idx];
    rectangle_add_point (extents, &handle->pos);
  }
}

/** Apply a function to all children of the given object (recursively, 
 * depth-first).
 * @param obj A parent object.
 * @param function A function that takes a single DiaObject as an argument.
 */
void
parent_apply_to_children(DiaObject *obj, DiaObjectFunc function)
{
  GList *children;
  for (children = obj->children; children != NULL; children = g_list_next(children)) {
    DiaObject *child = children->data;
    (*function)(child);
    parent_apply_to_children(child, function);
  }
}