gimpundostack.c 6.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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"

21
#include <glib-object.h>
22

23
#include "core-types.h"
24 25 26 27 28 29 30

#include "gimpimage.h"
#include "gimplist.h"
#include "gimpundo.h"
#include "gimpundostack.h"


31 32
static void    gimp_undo_stack_class_init  (GimpUndoStackClass  *klass);
static void    gimp_undo_stack_init        (GimpUndoStack       *stack);
33

34
static void    gimp_undo_stack_finalize    (GObject             *object);
35

36 37
static gint64  gimp_undo_stack_get_memsize (GimpObject          *object,
                                            gint64              *gui_size);
38

39 40 41 42 43
static void    gimp_undo_stack_pop         (GimpUndo            *undo,
                                            GimpUndoMode         undo_mode,
                                            GimpUndoAccumulator *accum);
static void    gimp_undo_stack_free        (GimpUndo            *undo,
                                            GimpUndoMode         undo_mode);
44

45

46 47 48
static GimpUndoClass *parent_class = NULL;


49
GType
50 51
gimp_undo_stack_get_type (void)
{
52
  static GType undo_stack_type = 0;
53

54 55
  if (! undo_stack_type)
    {
56
      static const GTypeInfo undo_stack_info =
57 58
      {
        sizeof (GimpUndoStackClass),
59 60 61 62 63 64 65 66
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_undo_stack_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpUndoStack),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_undo_stack_init,
67 68
      };

69
      undo_stack_type = g_type_register_static (GIMP_TYPE_UNDO,
70 71
                                                "GimpUndoStack",
                                                &undo_stack_info, 0);
72
    }
73 74 75 76 77 78 79

  return undo_stack_type;
}

static void
gimp_undo_stack_class_init (GimpUndoStackClass *klass)
{
80 81 82
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
  GimpUndoClass   *undo_class        = GIMP_UNDO_CLASS (klass);
83

84
  parent_class = g_type_class_peek_parent (klass);
85

86 87 88
  object_class->finalize         = gimp_undo_stack_finalize;

  gimp_object_class->get_memsize = gimp_undo_stack_get_memsize;
89 90 91

  undo_class->pop                = gimp_undo_stack_pop;
  undo_class->free               = gimp_undo_stack_free;
92 93 94 95 96
}

static void
gimp_undo_stack_init (GimpUndoStack *stack)
{
97
  stack->undos = gimp_list_new (GIMP_TYPE_UNDO, FALSE);
98 99 100
}

static void
101
gimp_undo_stack_finalize (GObject *object)
102
{
103
  GimpUndoStack *stack = GIMP_UNDO_STACK (object);
104

105 106
  if (stack->undos)
    {
107
      g_object_unref (stack->undos);
108 109
      stack->undos = NULL;
    }
110

111
  G_OBJECT_CLASS (parent_class)->finalize (object);
112 113
}

114
static gint64
115
gimp_undo_stack_get_memsize (GimpObject *object,
116
                             gint64     *gui_size)
117
{
118
  GimpUndoStack *stack   = GIMP_UNDO_STACK (object);
119
  gint64         memsize = 0;
120 121

  if (stack->undos)
122
    memsize += gimp_object_get_memsize (GIMP_OBJECT (stack->undos), gui_size);
123

124 125
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
126 127
}

128 129 130 131 132
static void
gimp_undo_stack_pop (GimpUndo            *undo,
                     GimpUndoMode         undo_mode,
                     GimpUndoAccumulator *accum)
{
133
  GimpUndoStack *stack = GIMP_UNDO_STACK (undo);
134 135 136 137 138 139
  GList         *list;

  for (list = GIMP_LIST (stack->undos)->list;
       list;
       list = g_list_next (list))
    {
140
      GimpUndo *child = list->data;
141

142
      gimp_undo_pop (child, undo_mode, accum);
143 144 145 146 147 148 149
    }
}

static void
gimp_undo_stack_free (GimpUndo     *undo,
                      GimpUndoMode  undo_mode)
{
150
  GimpUndoStack *stack = GIMP_UNDO_STACK (undo);
151 152 153 154 155 156
  GList         *list;

  for (list = GIMP_LIST (stack->undos)->list;
       list;
       list = g_list_next (list))
    {
157
      GimpUndo *child = list->data;
158

159
      gimp_undo_free (child, undo_mode);
160 161 162 163 164
      g_object_unref (child);
    }

  while (GIMP_LIST (stack->undos)->list)
    gimp_container_remove (GIMP_CONTAINER (stack->undos),
165
                           GIMP_LIST (stack->undos)->list->data);
166 167
}

168 169 170 171 172
GimpUndoStack *
gimp_undo_stack_new (GimpImage *gimage)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);

173 174 175
  return g_object_new (GIMP_TYPE_UNDO_STACK,
                       "image", gimage,
                       NULL);
176 177
}

178 179
void
gimp_undo_stack_push_undo (GimpUndoStack *stack,
180
                           GimpUndo      *undo)
181 182 183 184 185 186 187
{
  g_return_if_fail (GIMP_IS_UNDO_STACK (stack));
  g_return_if_fail (GIMP_IS_UNDO (undo));

  gimp_container_add (GIMP_CONTAINER (stack->undos), GIMP_OBJECT (undo));
}

188
GimpUndo *
189 190 191
gimp_undo_stack_pop_undo (GimpUndoStack       *stack,
                          GimpUndoMode         undo_mode,
                          GimpUndoAccumulator *accum)
192
{
193
  GimpUndo *undo;
194 195

  g_return_val_if_fail (GIMP_IS_UNDO_STACK (stack), NULL);
196
  g_return_val_if_fail (accum != NULL, NULL);
197

198 199 200 201
  undo = (GimpUndo *)
    gimp_container_get_child_by_index (GIMP_CONTAINER (stack->undos), 0);

  if (undo)
202
    {
203 204
      gimp_container_remove (GIMP_CONTAINER (stack->undos), GIMP_OBJECT (undo));
      gimp_undo_pop (undo, undo_mode, accum);
205

206
      return undo;
207
    }
208

209 210 211
  return NULL;
}

212
GimpUndo *
213 214 215
gimp_undo_stack_free_bottom (GimpUndoStack *stack,
                             GimpUndoMode   undo_mode)
{
216 217
  GimpUndo *undo;
  gint      n_children;
218

219
  g_return_val_if_fail (GIMP_IS_UNDO_STACK (stack), NULL);
220 221 222

  n_children = gimp_container_num_children (GIMP_CONTAINER (stack->undos));

223 224 225
  undo = (GimpUndo *)
    gimp_container_get_child_by_index (GIMP_CONTAINER (stack->undos),
                                       n_children - 1);
226

227
  if (undo)
228
    {
229 230
      gimp_container_remove (GIMP_CONTAINER (stack->undos), GIMP_OBJECT (undo));
      gimp_undo_free (undo, undo_mode);
231

232
      return undo;
233
    }
234 235

  return NULL;
236 237
}

238
GimpUndo *
239 240 241 242 243 244
gimp_undo_stack_peek (GimpUndoStack *stack)
{
  GimpObject *object;

  g_return_val_if_fail (GIMP_IS_UNDO_STACK (stack), NULL);

245
  object = gimp_container_get_child_by_index (GIMP_CONTAINER (stack->undos), 0);
246 247 248

  return (object ? GIMP_UNDO (object) : NULL);
}