summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimpundoeditor.c
blob: 62bee1681692ebaa3aba1e0716f2e5121c2c279a (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* GIMP - The GNU 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "config/gimpcoreconfig.h"

#include "core/gimp.h"
#include "core/gimplist.h"
#include "core/gimpimage.h"
#include "core/gimpimage-undo.h"
#include "core/gimpundostack.h"

#include "gimpcontainertreeview.h"
#include "gimpcontainerview.h"
#include "gimpdocked.h"
#include "gimphelp-ids.h"
#include "gimpmenufactory.h"
#include "gimpundoeditor.h"

#include "gimp-intl.h"


enum
{
  PROP_0,
  PROP_VIEW_SIZE
};


static void   gimp_undo_editor_docked_iface_init (GimpDockedInterface *iface);

static void   gimp_undo_editor_constructed    (GObject           *object);
static void   gimp_undo_editor_set_property   (GObject           *object,
                                               guint              property_id,
                                               const GValue      *value,
                                               GParamSpec        *pspec);

static void   gimp_undo_editor_set_image      (GimpImageEditor   *editor,
                                               GimpImage         *image);

static void   gimp_undo_editor_set_context    (GimpDocked        *docked,
                                               GimpContext       *context);

static void   gimp_undo_editor_fill           (GimpUndoEditor    *editor);
static void   gimp_undo_editor_clear          (GimpUndoEditor    *editor);

static void   gimp_undo_editor_undo_event     (GimpImage         *image,
                                               GimpUndoEvent      event,
                                               GimpUndo          *undo,
                                               GimpUndoEditor    *editor);

static void   gimp_undo_editor_select_item    (GimpContainerView *view,
                                               GimpUndo          *undo,
                                               gpointer           insert_data,
                                               GimpUndoEditor    *editor);


G_DEFINE_TYPE_WITH_CODE (GimpUndoEditor, gimp_undo_editor,
                         GIMP_TYPE_IMAGE_EDITOR,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,
                                                gimp_undo_editor_docked_iface_init))

#define parent_class gimp_undo_editor_parent_class

static GimpDockedInterface *parent_docked_iface = NULL;


static void
gimp_undo_editor_class_init (GimpUndoEditorClass *klass)
{
  GObjectClass         *object_class       = G_OBJECT_CLASS (klass);
  GimpImageEditorClass *image_editor_class = GIMP_IMAGE_EDITOR_CLASS (klass);

  object_class->constructed     = gimp_undo_editor_constructed;
  object_class->set_property    = gimp_undo_editor_set_property;

  image_editor_class->set_image = gimp_undo_editor_set_image;

  g_object_class_install_property (object_class, PROP_VIEW_SIZE,
                                   g_param_spec_enum ("view-size",
                                                      NULL, NULL,
                                                      GIMP_TYPE_VIEW_SIZE,
                                                      GIMP_VIEW_SIZE_LARGE,
                                                      GIMP_PARAM_WRITABLE |
                                                      G_PARAM_CONSTRUCT_ONLY));
}

static void
gimp_undo_editor_docked_iface_init (GimpDockedInterface *iface)
{
  parent_docked_iface = g_type_interface_peek_parent (iface);

  if (! parent_docked_iface)
    parent_docked_iface = g_type_default_interface_peek (GIMP_TYPE_DOCKED);

  iface->set_context = gimp_undo_editor_set_context;
}

static void
gimp_undo_editor_init (GimpUndoEditor *undo_editor)
{
}

static void
gimp_undo_editor_constructed (GObject *object)
{
  GimpUndoEditor *undo_editor = GIMP_UNDO_EDITOR (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  undo_editor->view = gimp_container_tree_view_new (NULL, NULL,
                                                    undo_editor->view_size,
                                                    1);

  gtk_box_pack_start (GTK_BOX (undo_editor), undo_editor->view, TRUE, TRUE, 0);
  gtk_widget_show (undo_editor->view);

  g_signal_connect (undo_editor->view, "select-item",
                    G_CALLBACK (gimp_undo_editor_select_item),
                    undo_editor);

  undo_editor->undo_button =
    gimp_editor_add_action_button (GIMP_EDITOR (undo_editor), "edit",
                                   "edit-undo", NULL);

  undo_editor->redo_button =
    gimp_editor_add_action_button (GIMP_EDITOR (undo_editor), "edit",
                                   "edit-redo", NULL);

  undo_editor->clear_button =
    gimp_editor_add_action_button (GIMP_EDITOR (undo_editor), "edit",
                                   "edit-undo-clear", NULL);
}

static void
gimp_undo_editor_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpUndoEditor *undo_editor = GIMP_UNDO_EDITOR (object);

  switch (property_id)
    {
    case PROP_VIEW_SIZE:
      undo_editor->view_size = g_value_get_enum (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_undo_editor_set_image (GimpImageEditor *image_editor,
                            GimpImage       *image)
{
  GimpUndoEditor *editor = GIMP_UNDO_EDITOR (image_editor);

  if (image_editor->image)
    {
      gimp_undo_editor_clear (editor);

      g_signal_handlers_disconnect_by_func (image_editor->image,
                                            gimp_undo_editor_undo_event,
                                            editor);
    }

  GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image);

  if (image_editor->image)
    {
      if (gimp_image_undo_is_enabled (image_editor->image))
        gimp_undo_editor_fill (editor);

      g_signal_connect (image_editor->image, "undo-event",
                        G_CALLBACK (gimp_undo_editor_undo_event),
                        editor);
    }
}

static void
gimp_undo_editor_set_context (GimpDocked  *docked,
                              GimpContext *context)
{
  GimpUndoEditor *editor = GIMP_UNDO_EDITOR (docked);

  if (editor->context)
    g_object_unref (editor->context);

  editor->context = context;

  if (editor->context)
    g_object_ref (editor->context);

  /* This calls gimp_undo_editor_set_image(), so make sure that it
   * isn't called before editor->context has been initialized.
   */
  parent_docked_iface->set_context (docked, context);

  gimp_container_view_set_context (GIMP_CONTAINER_VIEW (editor->view),
                                   context);
}


/*  public functions  */

GtkWidget *
gimp_undo_editor_new (GimpCoreConfig  *config,
                      GimpMenuFactory *menu_factory)
{
  g_return_val_if_fail (GIMP_IS_CORE_CONFIG (config), NULL);
  g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);

  return g_object_new (GIMP_TYPE_UNDO_EDITOR,
                       "menu-factory",    menu_factory,
                       "menu-identifier", "<Undo>",
                       "ui-path",         "/undo-popup",
                       "view-size",       config->undo_preview_size,
                       NULL);
}


/*  private functions  */

static void
gimp_undo_editor_fill (GimpUndoEditor *editor)
{
  GimpImage     *image      = GIMP_IMAGE_EDITOR (editor)->image;
  GimpUndoStack *undo_stack = gimp_image_get_undo_stack (image);
  GimpUndoStack *redo_stack = gimp_image_get_redo_stack (image);
  GimpUndo      *top_undo_item;
  GList         *list;

  /*  create a container as model for the undo history list  */
  editor->container = gimp_list_new (GIMP_TYPE_UNDO, FALSE);
  editor->base_item = g_object_new (GIMP_TYPE_UNDO,
                                    "image", image,
                                    "name",  _("[ Base Image ]"),
                                    NULL);

  /*  the list prepends its items, so first add the redo items in
   *  reverse (ascending) order...
   */
  for (list = GIMP_LIST (redo_stack->undos)->queue->tail;
       list;
       list = g_list_previous (list))
    {
      gimp_container_add (editor->container, GIMP_OBJECT (list->data));
    }

  /*  ...then add the undo items in descending order...  */
  for (list = GIMP_LIST (undo_stack->undos)->queue->head;
       list;
       list = g_list_next (list))
    {
      /*  Don't add the topmost item if it is an open undo group,
       *  it will be added upon closing of the group.
       */
      if (list->prev || ! GIMP_IS_UNDO_STACK (list->data) ||
          gimp_image_get_undo_group_count (image) == 0)
        {
          gimp_container_add (editor->container, GIMP_OBJECT (list->data));
        }
    }

  /*  ...finally, the first item is the special "base_item" which stands
   *  for the image with no more undos available to pop
   */
  gimp_container_add (editor->container, GIMP_OBJECT (editor->base_item));

  /*  display the container  */
  gimp_container_view_set_container (GIMP_CONTAINER_VIEW (editor->view),
                                     editor->container);

  top_undo_item = gimp_undo_stack_peek (undo_stack);

  g_signal_handlers_block_by_func (editor->view,
                                   gimp_undo_editor_select_item,
                                   editor);

  /*  select the current state of the image  */
  if (top_undo_item)
    {
      gimp_container_view_select_item (GIMP_CONTAINER_VIEW (editor->view),
                                       GIMP_VIEWABLE (top_undo_item));
      gimp_undo_create_preview (top_undo_item, editor->context, FALSE);
    }
  else
    {
      gimp_container_view_select_item (GIMP_CONTAINER_VIEW (editor->view),
                                       GIMP_VIEWABLE (editor->base_item));
      gimp_undo_create_preview (editor->base_item, editor->context, TRUE);
    }

  g_signal_handlers_unblock_by_func (editor->view,
                                     gimp_undo_editor_select_item,
                                     editor);
}

static void
gimp_undo_editor_clear (GimpUndoEditor *editor)
{
  if (editor->container)
    {
      gimp_container_view_set_container (GIMP_CONTAINER_VIEW (editor->view),
                                         NULL);
      g_clear_object (&editor->container);
    }

  g_clear_object (&editor->base_item);
}

static void
gimp_undo_editor_undo_event (GimpImage      *image,
                             GimpUndoEvent   event,
                             GimpUndo       *undo,
                             GimpUndoEditor *editor)
{
  GimpUndoStack *undo_stack    = gimp_image_get_undo_stack (image);
  GimpUndo      *top_undo_item = gimp_undo_stack_peek (undo_stack);

  switch (event)
    {
    case GIMP_UNDO_EVENT_UNDO_PUSHED:
      g_signal_handlers_block_by_func (editor->view,
                                       gimp_undo_editor_select_item,
                                       editor);

      gimp_container_insert (editor->container, GIMP_OBJECT (undo), -1);
      gimp_container_view_select_item (GIMP_CONTAINER_VIEW (editor->view),
                                       GIMP_VIEWABLE (undo));
      gimp_undo_create_preview (undo, editor->context, FALSE);

      g_signal_handlers_unblock_by_func (editor->view,
                                         gimp_undo_editor_select_item,
                                         editor);
      break;

    case GIMP_UNDO_EVENT_UNDO_EXPIRED:
    case GIMP_UNDO_EVENT_REDO_EXPIRED:
      gimp_container_remove (editor->container, GIMP_OBJECT (undo));
      break;

    case GIMP_UNDO_EVENT_UNDO:
    case GIMP_UNDO_EVENT_REDO:
      g_signal_handlers_block_by_func (editor->view,
                                       gimp_undo_editor_select_item,
                                       editor);

      if (top_undo_item)
        {
          gimp_container_view_select_item (GIMP_CONTAINER_VIEW (editor->view),
                                           GIMP_VIEWABLE (top_undo_item));
          gimp_undo_create_preview (top_undo_item, editor->context, FALSE);
        }
      else
        {
          gimp_container_view_select_item (GIMP_CONTAINER_VIEW (editor->view),
                                           GIMP_VIEWABLE (editor->base_item));
          gimp_undo_create_preview (editor->base_item, editor->context, TRUE);
        }

      g_signal_handlers_unblock_by_func (editor->view,
                                         gimp_undo_editor_select_item,
                                         editor);
      break;

    case GIMP_UNDO_EVENT_UNDO_FREE:
      if (gimp_image_undo_is_enabled (image))
        gimp_undo_editor_clear (editor);
      break;

    case GIMP_UNDO_EVENT_UNDO_FREEZE:
      gimp_undo_editor_clear (editor);
      break;

    case GIMP_UNDO_EVENT_UNDO_THAW:
      gimp_undo_editor_fill (editor);
      break;
    }
}

static void
gimp_undo_editor_select_item (GimpContainerView *view,
                              GimpUndo          *undo,
                              gpointer           insert_data,
                              GimpUndoEditor    *editor)
{
  GimpImage     *image      = GIMP_IMAGE_EDITOR (editor)->image;
  GimpUndoStack *undo_stack = gimp_image_get_undo_stack (image);
  GimpUndoStack *redo_stack = gimp_image_get_redo_stack (image);
  GimpUndo      *top_undo_item;

  if (! undo)
    return;

  top_undo_item = gimp_undo_stack_peek (undo_stack);

  if (undo == editor->base_item)
    {
      /*  the base_item was selected, pop all available undo items
       */
      while (top_undo_item != NULL)
        {
          if (! gimp_image_undo (image))
            break;

          top_undo_item = gimp_undo_stack_peek (undo_stack);
        }
    }
  else if (gimp_container_have (undo_stack->undos, GIMP_OBJECT (undo)))
    {
      /*  the selected item is on the undo stack, pop undos until it
       *  is on top of the undo stack
       */
      while (top_undo_item != undo)
        {
          if(! gimp_image_undo (image))
            break;

          top_undo_item = gimp_undo_stack_peek (undo_stack);
        }
    }
  else if (gimp_container_have (redo_stack->undos, GIMP_OBJECT (undo)))
    {
      /*  the selected item is on the redo stack, pop redos until it
       *  is on top of the undo stack
       */
      while (top_undo_item != undo)
        {
          if (! gimp_image_redo (image))
            break;

          top_undo_item = gimp_undo_stack_peek (undo_stack);
        }
    }

  gimp_image_flush (image);
}