gimpplugin-cleanup.c 9.56 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpplugin-cleanup.c
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9 10 11 12 13 14 15 16
 * (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
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 */

#include "config.h"

22
#include <gegl.h>
23 24 25 26 27

#include "plug-in-types.h"

#include "core/gimp.h"
#include "core/gimpcontainer.h"
28 29
#include "core/gimpdrawable.h"
#include "core/gimpdrawable-shadow.h"
30 31 32 33 34 35 36 37 38
#include "core/gimpimage.h"
#include "core/gimpimage-undo.h"
#include "core/gimpundostack.h"

#include "gimpplugin.h"
#include "gimpplugin-cleanup.h"
#include "gimppluginmanager.h"
#include "gimppluginprocedure.h"

39 40
#include "gimp-log.h"

41 42 43 44 45 46

typedef struct _GimpPlugInCleanupImage GimpPlugInCleanupImage;

struct _GimpPlugInCleanupImage
{
  GimpImage *image;
47 48
  gint       image_ID;

49 50 51 52
  gint       undo_group_count;
};


53 54 55 56 57 58 59 60 61 62 63
typedef struct _GimpPlugInCleanupItem GimpPlugInCleanupItem;

struct _GimpPlugInCleanupItem
{
  GimpItem *item;
  gint      item_ID;

  gboolean  shadow_tiles;
};


64 65 66
/*  local function prototypes  */

static GimpPlugInCleanupImage *
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
              gimp_plug_in_cleanup_image_new  (GimpImage              *image);
static void   gimp_plug_in_cleanup_image_free (GimpPlugInCleanupImage *cleanup);
static GimpPlugInCleanupImage *
              gimp_plug_in_cleanup_image_get  (GimpPlugInProcFrame    *proc_frame,
                                               GimpImage              *image);
static void   gimp_plug_in_cleanup_image      (GimpPlugInProcFrame    *proc_frame,
                                               GimpPlugInCleanupImage *cleanup);

static GimpPlugInCleanupItem *
              gimp_plug_in_cleanup_item_new   (GimpItem               *item);
static void   gimp_plug_in_cleanup_item_free  (GimpPlugInCleanupItem  *cleanup);
static GimpPlugInCleanupItem *
              gimp_plug_in_cleanup_item_get   (GimpPlugInProcFrame    *proc_frame,
                                               GimpItem               *item);
static void   gimp_plug_in_cleanup_item       (GimpPlugInProcFrame    *proc_frame,
                                               GimpPlugInCleanupItem  *cleanup);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


/*  public functions  */

gboolean
gimp_plug_in_cleanup_undo_group_start (GimpPlugIn *plug_in,
                                       GimpImage  *image)
{
  GimpPlugInProcFrame    *proc_frame;
  GimpPlugInCleanupImage *cleanup;

  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);

  proc_frame = gimp_plug_in_get_proc_frame (plug_in);
98
  cleanup    = gimp_plug_in_cleanup_image_get (proc_frame, image);
99 100 101

  if (! cleanup)
    {
102
      cleanup = gimp_plug_in_cleanup_image_new (image);
103

104
      cleanup->undo_group_count = gimp_image_get_undo_group_count (image);
105

106 107
      proc_frame->image_cleanups = g_list_prepend (proc_frame->image_cleanups,
                                                   cleanup);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    }

  return TRUE;
}

gboolean
gimp_plug_in_cleanup_undo_group_end (GimpPlugIn *plug_in,
                                     GimpImage  *image)
{
  GimpPlugInProcFrame    *proc_frame;
  GimpPlugInCleanupImage *cleanup;

  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);

  proc_frame = gimp_plug_in_get_proc_frame (plug_in);
124
  cleanup    = gimp_plug_in_cleanup_image_get (proc_frame, image);
125 126

  if (! cleanup)
127
    return FALSE;
128

129
  if (cleanup->undo_group_count == gimp_image_get_undo_group_count (image) - 1)
130
    {
131 132 133
      proc_frame->image_cleanups = g_list_remove (proc_frame->image_cleanups,
                                                  cleanup);
      gimp_plug_in_cleanup_image_free (cleanup);
134 135 136 137 138
    }

  return TRUE;
}

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
gboolean
gimp_plug_in_cleanup_add_shadow (GimpPlugIn   *plug_in,
                                 GimpDrawable *drawable)
{
  GimpPlugInProcFrame   *proc_frame;
  GimpPlugInCleanupItem *cleanup;

  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);

  proc_frame = gimp_plug_in_get_proc_frame (plug_in);
  cleanup    = gimp_plug_in_cleanup_item_get (proc_frame, GIMP_ITEM (drawable));

  if (! cleanup)
    {
      cleanup = gimp_plug_in_cleanup_item_new (GIMP_ITEM (drawable));

      proc_frame->item_cleanups = g_list_prepend (proc_frame->item_cleanups,
                                                  cleanup);
    }

  cleanup->shadow_tiles = TRUE;

  return TRUE;
}

gboolean
gimp_plug_in_cleanup_remove_shadow (GimpPlugIn   *plug_in,
                                    GimpDrawable *drawable)
{
  GimpPlugInProcFrame   *proc_frame;
  GimpPlugInCleanupItem *cleanup;

  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);

  proc_frame = gimp_plug_in_get_proc_frame (plug_in);
  cleanup    = gimp_plug_in_cleanup_item_get (proc_frame, GIMP_ITEM (drawable));

  if (! cleanup)
    return FALSE;

  if (! cleanup->shadow_tiles)
    return FALSE;

  proc_frame->item_cleanups = g_list_remove (proc_frame->item_cleanups,
                                             cleanup);
  gimp_plug_in_cleanup_item_free (cleanup);

  return TRUE;
}

191 192 193 194 195 196 197 198 199
void
gimp_plug_in_cleanup (GimpPlugIn          *plug_in,
                      GimpPlugInProcFrame *proc_frame)
{
  GList *list;

  g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
  g_return_if_fail (proc_frame != NULL);

200
  for (list = proc_frame->image_cleanups; list; list = g_list_next (list))
201 202 203
    {
      GimpPlugInCleanupImage *cleanup = list->data;

204 205 206 207 208 209 210 211
      if (gimp_image_get_by_ID (plug_in->manager->gimp,
                                cleanup->image_ID) == cleanup->image)
        {
          gimp_plug_in_cleanup_image (proc_frame, cleanup);
        }

      gimp_plug_in_cleanup_image_free (cleanup);
    }
212

213 214
  g_list_free (proc_frame->image_cleanups);
  proc_frame->image_cleanups = NULL;
215

216 217 218 219 220 221
  for (list = proc_frame->item_cleanups; list; list = g_list_next (list))
    {
      GimpPlugInCleanupItem *cleanup = list->data;

      if (gimp_item_get_by_ID (plug_in->manager->gimp,
                               cleanup->item_ID) == cleanup->item)
222
        {
223
          gimp_plug_in_cleanup_item (proc_frame, cleanup);
224 225
        }

226
      gimp_plug_in_cleanup_item_free (cleanup);
227 228
    }

229 230
  g_list_free (proc_frame->item_cleanups);
  proc_frame->item_cleanups = NULL;
231 232 233 234 235 236
}


/*  private functions  */

static GimpPlugInCleanupImage *
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
gimp_plug_in_cleanup_image_new (GimpImage *image)
{
  GimpPlugInCleanupImage *cleanup = g_slice_new0 (GimpPlugInCleanupImage);

  cleanup->image    = image;
  cleanup->image_ID = gimp_image_get_ID (image);

  return cleanup;
}

static void
gimp_plug_in_cleanup_image_free (GimpPlugInCleanupImage *cleanup)
{
  g_slice_free (GimpPlugInCleanupImage, cleanup);
}

static GimpPlugInCleanupImage *
gimp_plug_in_cleanup_image_get (GimpPlugInProcFrame *proc_frame,
255 256 257 258
                                GimpImage           *image)
{
  GList *list;

259
  for (list = proc_frame->image_cleanups; list; list = g_list_next (list))
260 261 262 263 264 265 266 267 268
    {
      GimpPlugInCleanupImage *cleanup = list->data;

      if (cleanup->image == image)
        return cleanup;
    }

  return NULL;
}
269 270 271 272 273 274 275

static void
gimp_plug_in_cleanup_image (GimpPlugInProcFrame    *proc_frame,
                            GimpPlugInCleanupImage *cleanup)
{
  GimpImage *image = cleanup->image;

276
  if (gimp_image_get_undo_group_count (image) == 0)
277 278
    return;

279
  if (cleanup->undo_group_count != gimp_image_get_undo_group_count (image))
280 281 282 283 284 285 286
    {
      GimpProcedure *proc = proc_frame->procedure;

      g_message ("Plug-In '%s' left image undo in inconsistent state, "
                 "closing open undo groups.",
                 gimp_plug_in_procedure_get_label (GIMP_PLUG_IN_PROCEDURE (proc)));

287
      while (cleanup->undo_group_count < gimp_image_get_undo_group_count (image))
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
        {
          if (! gimp_image_undo_group_end (image))
            break;
        }
    }
}

static GimpPlugInCleanupItem *
gimp_plug_in_cleanup_item_new (GimpItem *item)
{
  GimpPlugInCleanupItem *cleanup = g_slice_new0 (GimpPlugInCleanupItem);

  cleanup->item    = item;
  cleanup->item_ID = gimp_item_get_ID (item);

  return cleanup;
}

static void
gimp_plug_in_cleanup_item_free (GimpPlugInCleanupItem *cleanup)
{
  g_slice_free (GimpPlugInCleanupItem, cleanup);
}

static GimpPlugInCleanupItem *
gimp_plug_in_cleanup_item_get (GimpPlugInProcFrame *proc_frame,
                               GimpItem            *item)
{
  GList *list;

  for (list = proc_frame->item_cleanups; list; list = g_list_next (list))
    {
      GimpPlugInCleanupItem *cleanup = list->data;

      if (cleanup->item == item)
        return cleanup;
    }

  return NULL;
}

static void
gimp_plug_in_cleanup_item (GimpPlugInProcFrame   *proc_frame,
                           GimpPlugInCleanupItem *cleanup)
{
  GimpItem *item = cleanup->item;

  if (cleanup->shadow_tiles)
    {
      GimpProcedure *proc = proc_frame->procedure;

      GIMP_LOG (SHADOW_TILES,
                "Freeing shadow tiles of drawable '%s' on behalf of '%s'.",
341
                gimp_object_get_name (item),
342 343 344 345 346
                gimp_plug_in_procedure_get_label (GIMP_PLUG_IN_PROCEDURE (proc)));

      gimp_drawable_free_shadow_tiles (GIMP_DRAWABLE (item));
    }
}