~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/core/gimpimage-arrange.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <glib-object.h>
 
22
 
 
23
#include "core-types.h"
 
24
 
 
25
#include "gimpimage.h"
 
26
#include "gimpimage-arrange.h"
 
27
#include "gimpimage-guides.h"
 
28
#include "gimpimage-undo.h"
 
29
#include "gimpitem.h"
 
30
#include "gimpchannel.h"
 
31
#include "gimpdrawable.h"
 
32
#include "gimpguide.h"
 
33
 
 
34
#include "gimp-intl.h"
 
35
 
 
36
static GList * sort_by_offset  (GList             *list);
 
37
static void    compute_offsets (GList             *list,
 
38
                                GimpAlignmentType  alignment);
 
39
static void    compute_offset  (GObject           *object,
 
40
                                GimpAlignmentType  alignment);
 
41
static gint    offset_compare  (gconstpointer      a,
 
42
                                gconstpointer      b);
 
43
 
 
44
 
 
45
/**
 
46
 * gimp_image_arrange_objects:
 
47
 * @image:                The #GimpImage to which the objects belong.
 
48
 * @list:                 A #GList of objects to be aligned.
 
49
 * @alignment:            The point on each target object to bring into alignment.
 
50
 * @reference:            The #GObject to align the targets with, or #NULL.
 
51
 * @reference_alignment:  The point on the reference object to align the target item with..
 
52
 * @offset:               How much to shift the target from perfect alignment..
 
53
 *
 
54
 * This function shifts the positions of a set of target objects, which can be
 
55
 * "items" or guides, to bring them into a specified type of alignment with a
 
56
 * reference object, which can be an item, guide, or image.  If the requested
 
57
 * alignment does not make sense (i.e., trying to align a vertical guide vertically),
 
58
 * nothing happens and no error message is generated.
 
59
 *
 
60
 * The objects in the list are sorted into increasing order before
 
61
 * being arranged, where the order is defined by the type of alignment
 
62
 * being requested.  If the @reference argument is #NULL, then the first
 
63
 * object in the sorted list is used as reference.
 
64
 *
 
65
 * When there are multiple target objects, they are arranged so that the spacing
 
66
 * between consecutive ones is given by the argument @offset.
 
67
 */
 
68
void
 
69
gimp_image_arrange_objects (GimpImage         *image,
 
70
                            GList             *list,
 
71
                            GimpAlignmentType  alignment,
 
72
                            GObject           *reference,
 
73
                            GimpAlignmentType  reference_alignment,
 
74
                            gint               offset)
 
75
{
 
76
  gboolean do_x               = FALSE;
 
77
  gboolean do_y               = FALSE;
 
78
  gint     z0                 = 0;
 
79
  GList   *object_list;
 
80
 
 
81
  g_return_if_fail (GIMP_IS_IMAGE (image));
 
82
  g_return_if_fail (G_IS_OBJECT (reference) || reference == NULL);
 
83
 
 
84
  /* get offsets used for sorting */
 
85
  switch (alignment)
 
86
    {
 
87
      /* order vertically for horizontal alignment */
 
88
    case GIMP_ALIGN_LEFT:
 
89
    case GIMP_ALIGN_HCENTER:
 
90
    case GIMP_ALIGN_RIGHT:
 
91
      do_x = TRUE;
 
92
      compute_offsets (list, GIMP_ALIGN_TOP);
 
93
      break;
 
94
      /* order horizontally for horizontal arrangement */
 
95
    case GIMP_ARRANGE_LEFT:
 
96
    case GIMP_ARRANGE_HCENTER:
 
97
    case GIMP_ARRANGE_RIGHT:
 
98
      do_x = TRUE;
 
99
      compute_offsets (list, alignment);
 
100
      break;
 
101
      /* order horizontally for vertical alignment */
 
102
    case GIMP_ALIGN_TOP:
 
103
    case GIMP_ALIGN_VCENTER:
 
104
    case GIMP_ALIGN_BOTTOM:
 
105
      do_y = TRUE;
 
106
      compute_offsets (list, GIMP_ALIGN_LEFT);
 
107
      break;
 
108
      /* order vertically for vertical arrangement */
 
109
    case GIMP_ARRANGE_TOP:
 
110
    case GIMP_ARRANGE_VCENTER:
 
111
    case GIMP_ARRANGE_BOTTOM:
 
112
      do_y = TRUE;
 
113
      compute_offsets (list, alignment);
 
114
      break;
 
115
    }
 
116
 
 
117
  object_list = sort_by_offset (list);
 
118
 
 
119
  /* now get offsets used for aligning */
 
120
  compute_offsets (list, alignment);
 
121
 
 
122
  if (reference == NULL)
 
123
    {
 
124
      reference = G_OBJECT (object_list->data);
 
125
      object_list = g_list_next (object_list);
 
126
    }
 
127
  else
 
128
    compute_offset (reference, reference_alignment);
 
129
 
 
130
  z0 = GPOINTER_TO_INT (g_object_get_data (reference, "align-offset"));
 
131
 
 
132
  if (object_list)
 
133
    {
 
134
      GList *l;
 
135
      gint   n;
 
136
 
 
137
      /* FIXME: undo group type is wrong */
 
138
      gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_DISPLACE,
 
139
                                   _("Arrange Objects"));
 
140
 
 
141
      for (l = object_list, n = 1; l; l = g_list_next (l), n++)
 
142
        {
 
143
          GObject *target          = G_OBJECT (l->data);
 
144
          gint     xtranslate      = 0;
 
145
          gint     ytranslate      = 0;
 
146
          gint     z1;
 
147
 
 
148
          z1 = GPOINTER_TO_INT (g_object_get_data (target,
 
149
                                                    "align-offset"));
 
150
 
 
151
          if (do_x)
 
152
            xtranslate = z0 - z1 + n * offset;
 
153
 
 
154
          if (do_y)
 
155
            ytranslate = z0 - z1 + n * offset;
 
156
 
 
157
          /* now actually align the target object */
 
158
          if (GIMP_IS_ITEM (target))
 
159
            {
 
160
              gimp_item_translate (GIMP_ITEM (target),
 
161
                                   xtranslate, ytranslate, TRUE);
 
162
            }
 
163
          else if (GIMP_IS_GUIDE (target))
 
164
            {
 
165
              GimpGuide *guide = GIMP_GUIDE (target);
 
166
 
 
167
              switch (gimp_guide_get_orientation (guide))
 
168
                {
 
169
                case GIMP_ORIENTATION_VERTICAL:
 
170
                  gimp_image_move_guide (image, guide, z1 + xtranslate, TRUE);
 
171
                  gimp_image_update_guide (image, guide);
 
172
                  break;
 
173
 
 
174
                case GIMP_ORIENTATION_HORIZONTAL:
 
175
                  gimp_image_move_guide (image, guide, z1 + ytranslate, TRUE);
 
176
                  gimp_image_update_guide (image, guide);
 
177
                  break;
 
178
 
 
179
                default:
 
180
                  break;
 
181
                }
 
182
            }
 
183
        }
 
184
 
 
185
      gimp_image_undo_group_end (image);
 
186
    }
 
187
 
 
188
  g_list_free (object_list);
 
189
}
 
190
 
 
191
 
 
192
static GList *
 
193
sort_by_offset (GList *list)
 
194
{
 
195
  return g_list_sort (g_list_copy (list),
 
196
                      offset_compare);
 
197
 
 
198
}
 
199
 
 
200
static gint
 
201
offset_compare (gconstpointer a,
 
202
                gconstpointer b)
 
203
{
 
204
  gint offset1 = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (a),
 
205
                                                     "align-offset"));
 
206
  gint offset2 = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (b),
 
207
                                                     "align-offset"));
 
208
 
 
209
  return offset1 - offset2;
 
210
}
 
211
 
 
212
/*
 
213
 * this function computes the position of the alignment point
 
214
 * for each object in the list, and attaches it to the
 
215
 * object as object data.
 
216
 */
 
217
static void
 
218
compute_offsets (GList             *list,
 
219
                 GimpAlignmentType  alignment)
 
220
{
 
221
  GList *l;
 
222
 
 
223
  for (l = list; l; l = g_list_next (l))
 
224
    compute_offset (G_OBJECT (l->data), alignment);
 
225
}
 
226
 
 
227
static void
 
228
compute_offset (GObject *object,
 
229
                GimpAlignmentType  alignment)
 
230
{
 
231
  gint object_offset_x = 0;
 
232
  gint object_offset_y = 0;
 
233
  gint object_height   = 0;
 
234
  gint object_width    = 0;
 
235
  gint offset          = 0;
 
236
 
 
237
  if (GIMP_IS_IMAGE (object))
 
238
    {
 
239
      GimpImage *image = GIMP_IMAGE (object);
 
240
 
 
241
      object_offset_x = 0;
 
242
      object_offset_y = 0;
 
243
      object_height   = gimp_image_get_height (image);
 
244
      object_width    = gimp_image_get_width (image);
 
245
    }
 
246
  else if (GIMP_IS_CHANNEL (object))
 
247
    {
 
248
      /* for channels, we use the bounds of the visible area, not
 
249
         the layer bounds.  This includes the selection channel */
 
250
 
 
251
      GimpChannel *channel = GIMP_CHANNEL (object);
 
252
 
 
253
      if (gimp_channel_is_empty (channel))
 
254
        {
 
255
          /* fall back on using the offsets instead */
 
256
          GimpItem *item = GIMP_ITEM (object);
 
257
 
 
258
          gimp_item_offsets (item, &object_offset_x, &object_offset_y);
 
259
          object_height   = gimp_item_height (item);
 
260
          object_width    = gimp_item_width (item);
 
261
        }
 
262
      else
 
263
        {
 
264
          gint x1, x2, y1, y2;
 
265
 
 
266
          gimp_channel_bounds (channel, &x1, &y1, &x2, &y2);
 
267
          object_offset_x = x1;
 
268
          object_offset_y = y1;
 
269
          object_height   = y2 - y1;
 
270
          object_width    = x2 - x1;
 
271
        }
 
272
    }
 
273
  else if (GIMP_IS_ITEM (object))
 
274
    {
 
275
      GimpItem *item = GIMP_ITEM (object);
 
276
 
 
277
      gimp_item_offsets (item, &object_offset_x, &object_offset_y);
 
278
      object_height   = gimp_item_height (item);
 
279
      object_width    = gimp_item_width (item);
 
280
    }
 
281
  else if (GIMP_IS_GUIDE (object))
 
282
    {
 
283
      GimpGuide *guide = GIMP_GUIDE (object);
 
284
 
 
285
      switch (gimp_guide_get_orientation (guide))
 
286
        {
 
287
        case GIMP_ORIENTATION_VERTICAL:
 
288
          object_offset_x = gimp_guide_get_position (guide);
 
289
          object_width = 0;
 
290
          break;
 
291
 
 
292
        case GIMP_ORIENTATION_HORIZONTAL:
 
293
          object_offset_y = gimp_guide_get_position (guide);
 
294
          object_height = 0;
 
295
          break;
 
296
 
 
297
        default:
 
298
          break;
 
299
        }
 
300
    }
 
301
  else
 
302
    {
 
303
      g_printerr ("Alignment object is not an image, item or guide.\n");
 
304
    }
 
305
 
 
306
  switch (alignment)
 
307
    {
 
308
    case GIMP_ALIGN_LEFT:
 
309
    case GIMP_ARRANGE_LEFT:
 
310
      offset = object_offset_x;
 
311
      break;
 
312
    case GIMP_ALIGN_HCENTER:
 
313
    case GIMP_ARRANGE_HCENTER:
 
314
      offset = object_offset_x + object_width/2;
 
315
      break;
 
316
    case GIMP_ALIGN_RIGHT:
 
317
    case GIMP_ARRANGE_RIGHT:
 
318
      offset = object_offset_x + object_width;
 
319
      break;
 
320
    case GIMP_ALIGN_TOP:
 
321
    case GIMP_ARRANGE_TOP:
 
322
      offset = object_offset_y;
 
323
      break;
 
324
    case GIMP_ALIGN_VCENTER:
 
325
    case GIMP_ARRANGE_VCENTER:
 
326
      offset = object_offset_y + object_height/2;
 
327
      break;
 
328
    case GIMP_ALIGN_BOTTOM:
 
329
    case GIMP_ARRANGE_BOTTOM:
 
330
      offset = object_offset_y + object_height;
 
331
      break;
 
332
    default:
 
333
      g_assert_not_reached ();
 
334
    }
 
335
 
 
336
  g_object_set_data (object, "align-offset",
 
337
                     GINT_TO_POINTER (offset));
 
338
}