file-open-dialog.c 7.72 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
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
 * Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
 * Copyright (C) 1997 Josh MacDonald
 *
 * 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"

#include <string.h>

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

28
#include "dialogs-types.h"
29

30
#include "core/gimp.h"
31
#include "core/gimpimage.h"
32 33
#include "core/gimpimage-undo.h"
#include "core/gimplayer.h"
34
#include "core/gimpprogress.h"
Michael Natterer's avatar
Michael Natterer committed
35

Michael Natterer's avatar
Michael Natterer committed
36 37 38
#include "file/file-open.h"
#include "file/file-utils.h"

39
#include "widgets/gimpfiledialog.h"
40
#include "widgets/gimphelp-ids.h"
41

42
#include "file-open-dialog.h"
43

44
#include "gimp-intl.h"
45 46


47
/*  local function prototypes  */
48

49 50 51 52 53 54 55 56 57 58 59 60 61
static void       file_open_dialog_response    (GtkWidget           *open_dialog,
                                                gint                 response_id,
                                                Gimp                *gimp);
static gboolean   file_open_dialog_open_image  (GtkWidget           *open_dialog,
                                                Gimp                *gimp,
                                                const gchar         *uri,
                                                const gchar         *entered_filename,
                                                GimpPlugInProcedure *load_proc);
static gboolean   file_open_dialog_open_layers (GtkWidget           *open_dialog,
                                                GimpImage           *image,
                                                const gchar         *uri,
                                                const gchar         *entered_filename,
                                                GimpPlugInProcedure *load_proc);
62 63 64 65


/*  public functions  */

66 67
GtkWidget *
file_open_dialog_new (Gimp *gimp)
68
{
Michael Natterer's avatar
Michael Natterer committed
69
  GtkWidget *dialog;
70

71 72
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

73
  dialog = gimp_file_dialog_new (gimp,
74
                                 GTK_FILE_CHOOSER_ACTION_OPEN,
Michael Natterer's avatar
Michael Natterer committed
75 76 77 78
                                 _("Open Image"), "gimp-file-open",
                                 GTK_STOCK_OPEN,
                                 GIMP_HELP_FILE_OPEN);

79
  gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
80

Michael Natterer's avatar
Michael Natterer committed
81 82
  g_signal_connect (dialog, "response",
                    G_CALLBACK (file_open_dialog_response),
83
                    gimp);
84

Michael Natterer's avatar
Michael Natterer committed
85
  return dialog;
86 87
}

88 89 90

/*  private functions  */

91
static void
Michael Natterer's avatar
Michael Natterer committed
92 93 94
file_open_dialog_response (GtkWidget *open_dialog,
                           gint       response_id,
                           Gimp      *gimp)
95
{
96 97 98 99
  GimpFileDialog *dialog  = GIMP_FILE_DIALOG (open_dialog);
  GSList         *uris;
  GSList         *list;
  gboolean        success = FALSE;
100

101 102
  if (response_id != GTK_RESPONSE_OK)
    {
103 104 105
      if (! dialog->busy)
        gtk_widget_hide (open_dialog);

106 107
      return;
    }
108

109
  uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (open_dialog));
110

111
  gimp_file_dialog_set_sensitive (dialog, FALSE);
112

113 114 115
  /*  open layers in reverse order so they appear in the same
   *  order as in the file dialog
   */
116
  if (dialog->image)
117 118
    uris = g_slist_reverse (uris);

119
  for (list = uris; list; list = g_slist_next (list))
120
    {
121
      gchar *filename = file_utils_filename_from_uri (list->data);
122

123
      if (filename)
124
        {
125 126 127 128 129 130 131 132
          gboolean regular = g_file_test (filename, G_FILE_TEST_IS_REGULAR);

          g_free (filename);

          if (! regular)
            continue;
        }

133
      if (dialog->image)
134
        {
135 136 137 138 139
          if (file_open_dialog_open_layers (open_dialog,
                                            dialog->image,
                                            list->data,
                                            list->data,
                                            dialog->file_proc))
140
            {
141
              success = TRUE;
142
            }
143 144 145 146 147 148 149 150
        }
      else
        {
          if (file_open_dialog_open_image (open_dialog,
                                           gimp,
                                           list->data,
                                           list->data,
                                           dialog->file_proc))
151
            {
152 153 154
              success = TRUE;

              gdk_window_raise (open_dialog->window);
155
            }
156
        }
157

158 159
      if (dialog->canceled)
        break;
160
    }
161

162
  if (success)
163 164 165
    {
      gtk_widget_hide (open_dialog);

166 167
      if (dialog->image)
        gimp_image_flush (dialog->image);
168
    }
169

170
  gimp_file_dialog_set_sensitive (dialog, TRUE);
171 172 173

  g_slist_foreach (uris, (GFunc) g_free, NULL);
  g_slist_free (uris);
174 175
}

176
static gboolean
177 178 179 180 181
file_open_dialog_open_image (GtkWidget           *open_dialog,
                             Gimp                *gimp,
                             const gchar         *uri,
                             const gchar         *entered_filename,
                             GimpPlugInProcedure *load_proc)
182
{
183
  GimpImage         *image;
184 185
  GimpPDBStatusType  status;
  GError            *error = NULL;
186

187 188 189
  image = file_open_with_proc_and_display (gimp,
                                           gimp_get_user_context (gimp),
                                           GIMP_PROGRESS (open_dialog),
190
                                           uri, entered_filename, FALSE,
191
                                           load_proc,
192
                                           &status, &error);
193

194
  if (image)
195
    {
196
      return TRUE;
197 198 199
    }
  else if (status != GIMP_PDB_CANCEL)
    {
200
      gchar *filename = file_utils_uri_display_name (uri);
201

202
      gimp_message (gimp, G_OBJECT (open_dialog), GIMP_MESSAGE_ERROR,
203
                    _("Opening '%s' failed:\n\n%s"), filename, error->message);
204 205 206 207
      g_clear_error (&error);

      g_free (filename);
    }
208 209

  return FALSE;
210
}
211 212

static gboolean
213 214 215 216 217
file_open_dialog_open_layers (GtkWidget           *open_dialog,
                              GimpImage           *image,
                              const gchar         *uri,
                              const gchar         *entered_filename,
                              GimpPlugInProcedure *load_proc)
218
{
219
  GList             *new_layers;
220 221 222
  GimpPDBStatusType  status;
  GError            *error = NULL;

223 224 225 226 227 228
  new_layers = file_open_layers (image->gimp,
                                 gimp_get_user_context (image->gimp),
                                 GIMP_PROGRESS (open_dialog),
                                 image, FALSE,
                                 uri, GIMP_RUN_INTERACTIVE, load_proc,
                                 &status, &error);
229

230
  if (new_layers)
231
    {
232 233 234 235 236
      gimp_image_add_layers (image, new_layers, -1,
                             0, 0,
                             gimp_image_get_width (image),
                             gimp_image_get_height (image),
                             _("Open layers"));
237

238
      g_list_free (new_layers);
239

240 241
      gimp_image_flush (image);

242 243 244 245
      return TRUE;
    }
  else if (status != GIMP_PDB_CANCEL)
    {
246
      gchar *filename = file_utils_uri_display_name (uri);
247

248
      gimp_message (image->gimp, G_OBJECT (open_dialog), GIMP_MESSAGE_ERROR,
249
                    _("Opening '%s' failed:\n\n%s"), filename, error->message);
250 251 252 253 254 255 256
      g_clear_error (&error);

      g_free (filename);
    }

  return FALSE;
}