documents-commands.c 7.69 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 22
#include <string.h>

23 24
#include <gtk/gtk.h>

25
#include "libgimpthumb/gimpthumb.h"
26 27
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
28
#include "actions-types.h"
29

30 31
#include "config/gimpcoreconfig.h"

32 33 34 35 36 37 38 39 40
#include "core/gimp.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpimagefile.h"

#include "file/file-open.h"
#include "file/file-utils.h"

#include "widgets/gimpcontainerview.h"
41 42
#include "widgets/gimpdocumentview.h"

43 44 45
#include "display/gimpdisplay.h"
#include "display/gimpdisplay-foreach.h"

46
#include "documents-commands.h"
47 48 49 50 51
#include "file-commands.h"

#include "gimp-intl.h"


52 53 54 55 56 57 58 59 60
typedef struct _RaiseClosure RaiseClosure;

struct _RaiseClosure
{
  const gchar *name;
  gboolean     found;
};


61 62
/*  local function prototypes  */

63 64 65 66
static void   documents_open_image    (GimpContext   *context,
                                       GimpImagefile *imagefile);
static void   documents_raise_display (gpointer       data,
                                       gpointer       user_data);
67 68


69
/*  public functions */
70 71

void
72 73
documents_open_cmd_callback (GtkAction *action,
                             gpointer   data)
74
{
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext         *context;
  GimpContainer       *container;
  GimpImagefile       *imagefile;

  context   = gimp_container_view_get_context (editor->view);
  container = gimp_container_view_get_container (editor->view);

  imagefile = gimp_context_get_imagefile (context);

  if (imagefile && gimp_container_have (container, GIMP_OBJECT (imagefile)))
    {
      documents_open_image (context, imagefile);
    }
  else
    {
      file_file_open_dialog (context->gimp, NULL, GTK_WIDGET (editor));
    }
}

95
void
96 97
documents_raise_or_open_cmd_callback (GtkAction *action,
                                      gpointer   data)
98
{
99 100 101 102
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext         *context;
  GimpContainer       *container;
  GimpImagefile       *imagefile;
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  context   = gimp_container_view_get_context (editor->view);
  container = gimp_container_view_get_container (editor->view);

  imagefile = gimp_context_get_imagefile (context);

  if (imagefile && gimp_container_have (container, GIMP_OBJECT (imagefile)))
    {
      RaiseClosure closure;

      closure.name  = gimp_object_get_name (GIMP_OBJECT (imagefile));
      closure.found = FALSE;

      gimp_container_foreach (context->gimp->displays,
                              documents_raise_display,
                              &closure);

      if (! closure.found)
        documents_open_image (context, imagefile);
    }
123 124 125
}

void
126
documents_file_open_dialog_cmd_callback (GtkAction *action,
127 128
                                         gpointer   data)
{
129 130 131 132 133 134 135
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext         *context;
  GimpContainer       *container;
  GimpImagefile       *imagefile;

  context   = gimp_container_view_get_context (editor->view);
  container = gimp_container_view_get_container (editor->view);
136

137 138 139 140 141 142 143 144
  imagefile = gimp_context_get_imagefile (context);

  if (imagefile && gimp_container_have (container, GIMP_OBJECT (imagefile)))
    {
      file_file_open_dialog (context->gimp,
                             gimp_object_get_name (GIMP_OBJECT (imagefile)),
                             GTK_WIDGET (editor));
    }
145 146 147
}

void
148 149
documents_remove_cmd_callback (GtkAction *action,
                               gpointer   data)
150
{
151 152 153 154
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext         *context;
  GimpContainer       *container;
  GimpImagefile       *imagefile;
155

156 157 158 159
  context   = gimp_container_view_get_context (editor->view);
  container = gimp_container_view_get_container (editor->view);

  imagefile = gimp_context_get_imagefile (context);
160

161 162 163 164 165
  if (imagefile && gimp_container_have (container, GIMP_OBJECT (imagefile)))
    {
      gimp_container_remove (container, GIMP_OBJECT (imagefile));
    }
}
166 167

void
168
documents_recreate_preview_cmd_callback (GtkAction *action,
169
                                         gpointer   data)
170
{
171 172 173 174 175 176 177 178 179
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContext         *context;
  GimpContainer       *container;
  GimpImagefile       *imagefile;

  context   = gimp_container_view_get_context (editor->view);
  container = gimp_container_view_get_container (editor->view);

  imagefile = gimp_context_get_imagefile (context);
180

181 182
  if (imagefile && gimp_container_have (container, GIMP_OBJECT (imagefile)))
    {
183 184 185 186
      gimp_imagefile_create_thumbnail (imagefile,
                                       context, NULL,
                                       imagefile->gimp->config->thumbnail_size,
                                       FALSE);
187
    }
188 189
}

190
void
191
documents_reload_previews_cmd_callback (GtkAction *action,
192 193
                                        gpointer   data)
{
194 195 196 197
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;

  container = gimp_container_view_get_container (editor->view);
198

199 200 201 202 203 204
  gimp_container_foreach (container,
                          (GFunc) gimp_imagefile_update,
                          editor->view);
}

static void
205
documents_remove_dangling_foreach (GimpImagefile *imagefile,
206 207 208 209 210 211 212
                                   GimpContainer *container)
{
  if (gimp_thumbnail_peek_image (imagefile->thumbnail) ==
      GIMP_THUMB_STATE_NOT_FOUND)
    {
      gimp_container_remove (container, GIMP_OBJECT (imagefile));
    }
213 214 215
}

void
216 217
documents_remove_dangling_cmd_callback (GtkAction *action,
                                        gpointer   data)
218
{
219 220 221 222 223 224
  GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
  GimpContainer       *container;

  container = gimp_container_view_get_container (editor->view);

  gimp_container_foreach (container,
225
                          (GFunc) documents_remove_dangling_foreach,
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                          container);
}


/*  private functions  */

static void
documents_open_image (GimpContext   *context,
                      GimpImagefile *imagefile)
{
  const gchar        *uri;
  GimpImage          *gimage;
  GimpPDBStatusType   status;
  GError             *error = NULL;

  uri = gimp_object_get_name (GIMP_OBJECT (imagefile));

243
  gimage = file_open_with_display (context->gimp, context, NULL,
244 245 246 247 248 249 250 251 252 253
                                   uri, &status, &error);

  if (! gimage && status != GIMP_PDB_CANCEL)
    {
      gchar *filename;

      filename = file_utils_uri_to_utf8_filename (uri);
      g_message (_("Opening '%s' failed:\n\n%s"),
                 filename, error->message);
      g_clear_error (&error);
254

255 256
      g_free (filename);
    }
257
}
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

static void
documents_raise_display (gpointer data,
                         gpointer user_data)
{
  GimpDisplay  *gdisp   = data;
  RaiseClosure *closure = user_data;
  const gchar  *uri;

  uri = gimp_object_get_name (GIMP_OBJECT (gdisp->gimage));

  if (uri && ! strcmp (closure->name, uri))
    {
      closure->found = TRUE;
      gtk_window_present (GTK_WINDOW (gdisp->shell));
    }
}