fileops.c 48.9 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* The GIMP -- an image manipulation program
 * 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
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19
20
21

#include "config.h"

22
23
24
25
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

Tor Lillqvist's avatar
Tor Lillqvist committed
26
27
#include <glib.h>

Elliot Lee's avatar
Elliot Lee committed
28
29
#include <ctype.h>
#include <errno.h>
30
#include <stdlib.h>
Elliot Lee's avatar
Elliot Lee committed
31
32
33
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
34
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
35
#include <unistd.h>
36
#endif
Elliot Lee's avatar
Elliot Lee committed
37
38
#include <errno.h>

Tor Lillqvist's avatar
Tor Lillqvist committed
39
#ifdef NATIVE_WIN32
40
41
42
43
#include <direct.h>		/* For _mkdir() */
#define mkdir(path,mode) _mkdir(path)
#endif

Elliot Lee's avatar
Elliot Lee committed
44
45
#include "appenv.h"
#include "actionarea.h"
46
#include "cursorutil.h"
Elliot Lee's avatar
Elliot Lee committed
47
48
49
#include "gdisplay.h"
#include "general.h"
#include "gimage.h"
50
#include "gimpcontext.h"
Elliot Lee's avatar
Elliot Lee committed
51
52
53
54
55
56
#include "fileops.h"
#include "interface.h"
#include "menus.h"
#include "plug_in.h"
#include "procedural_db.h"
#include "gimprc.h"
Chris Lahey's avatar
Chris Lahey committed
57
#include "docindex.h"
58
#include "undo.h"
Elliot Lee's avatar
Elliot Lee committed
59

60
#include "libgimp/gimpintl.h"
61
#include "libgimp/gimpmath.h"
62

Elliot Lee's avatar
Elliot Lee committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
typedef struct _OverwriteBox OverwriteBox;

struct _OverwriteBox
{
  GtkWidget * obox;
  char *      full_filename;
  char *      raw_filename;
};

static void file_overwrite              (char *filename,
					 char* raw_filename);
static void file_overwrite_yes_callback (GtkWidget *w,
					 gpointer   client_data);
static void file_overwrite_no_callback  (GtkWidget *w,
					 gpointer   client_data);

static gint file_overwrite_delete_callback  (GtkWidget *w,
					     GdkEvent  *e,
					     gpointer   client_data);

Manish Singh's avatar
Manish Singh committed
83
static GimpImage* file_open_image   (char *filename,
84
85
				     char *raw_filename,
				     RunModeType runmode);
Manish Singh's avatar
Manish Singh committed
86

87
88
89
static void genbutton_callback (GtkWidget *w,
				gpointer   client_data);

90
91
static void file_open_clistrow_callback (GtkWidget *w,
				         int        client_data);
Elliot Lee's avatar
Elliot Lee committed
92
93
94
95
96
97
static void file_open_ok_callback   (GtkWidget *w,
				     gpointer   client_data);
static void file_save_ok_callback   (GtkWidget *w,
				     gpointer   client_data);

static void file_dialog_show        (GtkWidget *filesel);
98
static int  file_dialog_hide        (GtkWidget *filesel);
Elliot Lee's avatar
Elliot Lee committed
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
static void file_update_name        (PlugInProcDef *proc,
				     GtkWidget     *filesel);
static void file_load_type_callback (GtkWidget *w,
				     gpointer   client_data);
static void file_save_type_callback (GtkWidget *w,
				     gpointer   client_data);

static void file_convert_string (char *instr,
                                 char *outmem,
                                 int maxmem,
                                 int *nmem);

static int  file_check_single_magic (char *offset,
                                     char *type,
                                     char *value,
                                     int headsize,
                                     unsigned char *file_head,
                                     FILE *ifp);

static int  file_check_magic_list (GSList *magics_list,
                                   int headsize,
                                   unsigned char *head,
                                   FILE *ifp);

static void      file_update_menus      (GSList *procs,
					 int     image_type);


127
128
129
130
GtkWidget  *fileload = NULL; /* Shared with dialog_handler.c */
static GtkWidget  *filesave = NULL;
static GtkWidget  *open_options = NULL;
static GtkWidget  *save_options = NULL;
131
132
133
134
/* widgets for the open_options menu */
static GtkPreview *open_options_preview = NULL;
static GtkWidget  *open_options_fixed = NULL;
static GtkWidget  *open_options_label = NULL;
135
static GtkWidget  *open_options_frame = NULL;
136
static GtkWidget  *open_options_genbuttonlabel = NULL;
137
138
139
/* Some state for the thumbnailer */
static gchar      *preview_fullname = NULL;

140
141
GSList *load_procs = NULL;
GSList *save_procs = NULL;
Elliot Lee's avatar
Elliot Lee committed
142
143
144
145

static PlugInProcDef *load_file_proc = NULL;
static PlugInProcDef *save_file_proc = NULL;

146
147
148
static GimpImage *the_gimage = NULL;

extern GSList *display_list; /* from gdisplay.c */
Elliot Lee's avatar
Elliot Lee committed
149

150
151
152
153
154
155
156
#define FILE_ERR_MESSAGE(str)				G_STMT_START{	\
  if (message_handler == MESSAGE_BOX)					\
    message_box ((str), file_message_box_close_callback, (void *) fs);	\
  else									\
    g_message (str);							\
    gtk_widget_set_sensitive (GTK_WIDGET (fs), TRUE);	}G_STMT_END

Matt Wilson's avatar
Matt Wilson committed
157
158
159
160
161
162
163
164
165
166
static void
file_message_box_close_callback (GtkWidget *w,
				 gpointer   client_data)
{
  GtkFileSelection *fs;

  fs = (GtkFileSelection *) client_data;

  gtk_widget_set_sensitive (GTK_WIDGET (fs), TRUE);
}
Elliot Lee's avatar
Elliot Lee committed
167
168

void
169
file_ops_pre_init (void)
Elliot Lee's avatar
Elliot Lee committed
170
171
172
173
{
}

void
174
file_ops_post_init (void)
Elliot Lee's avatar
Elliot Lee committed
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
{
  GtkMenuEntry entry;
  PlugInProcDef *file_proc;
  GSList *tmp;

  load_procs = g_slist_reverse (load_procs);
  save_procs = g_slist_reverse (save_procs);

  tmp = load_procs;
  while (tmp)
    {
      file_proc = tmp->data;
      tmp = tmp->next;

      entry.path = file_proc->menu_path;
      entry.accelerator = NULL;
      entry.callback = file_load_type_callback;
      entry.callback_data = file_proc;

      menus_create (&entry, 1);
    }

  tmp = save_procs;
  while (tmp)
    {
      file_proc = tmp->data;
      tmp = tmp->next;

      entry.path = file_proc->menu_path;
      entry.accelerator = NULL;
      entry.callback = file_save_type_callback;
      entry.callback_data = file_proc;

      menus_create (&entry, 1);
    }
}

void
file_open_callback (GtkWidget *w,
		    gpointer   client_data)
{
  GtkWidget *hbox;
217
  GtkWidget *vbox;
Elliot Lee's avatar
Elliot Lee committed
218
219
220
  GtkWidget *label;
  GtkWidget *option_menu;
  GtkWidget *load_menu;
221
  GtkWidget *open_options_genbutton;
Elliot Lee's avatar
Elliot Lee committed
222
223
224

  if (!fileload)
    {
225
      fileload = gtk_file_selection_new (_("Load Image"));
226
      gtk_window_set_position (GTK_WINDOW (fileload), GTK_WIN_POS_MOUSE);
227
      gtk_window_set_wmclass (GTK_WINDOW (fileload), "load_image", "Gimp");
228
229
      gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (fileload)->cancel_button),
				 "clicked",
230
				 GTK_SIGNAL_FUNC (file_dialog_hide),
231
232
233
				 GTK_OBJECT (fileload));
      gtk_signal_connect (GTK_OBJECT (fileload),
			  "delete_event",
234
			  GTK_SIGNAL_FUNC (file_dialog_hide),
235
			  NULL);
Elliot Lee's avatar
Elliot Lee committed
236
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (fileload)->ok_button), "clicked", (GtkSignalFunc) file_open_ok_callback, fileload);
237
      gtk_quit_add_destroy (1, GTK_OBJECT (fileload));
238
239
240
241

      gtk_clist_set_selection_mode (GTK_CLIST
				    (GTK_FILE_SELECTION (fileload)->file_list),
				    GTK_SELECTION_EXTENDED);
242
243
244

      /* Catch file-clist clicks so we can update the preview thumbnail */
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (fileload)->file_list), "select_row", (GtkSignalFunc) file_open_clistrow_callback, fileload);
Elliot Lee's avatar
Elliot Lee committed
245
246
247
    }
  else
    {
248
      gtk_widget_set_sensitive (GTK_WIDGET (fileload), TRUE);
Elliot Lee's avatar
Elliot Lee committed
249
250
251
      if (GTK_WIDGET_VISIBLE (fileload))
	return;

252
253
      gtk_file_selection_set_filename (GTK_FILE_SELECTION(fileload),
				       "." G_DIR_SEPARATOR_S);
254
      gtk_window_set_title (GTK_WINDOW (fileload), _("Load Image"));
Elliot Lee's avatar
Elliot Lee committed
255
256
257
258
    }

  if (!open_options)
    {
259
      GtkWidget* frame;
Elliot Lee's avatar
Elliot Lee committed
260

261
      open_options = gtk_hbox_new (TRUE, 1);
Elliot Lee's avatar
Elliot Lee committed
262

263
264
265
266
267
268
269
270
      /* format-chooser frame */
      frame = gtk_frame_new (_("Open Options"));
      {
	gtk_frame_set_shadow_type (GTK_FRAME (frame),
				   GTK_SHADOW_ETCHED_IN);
	
	vbox = gtk_vbox_new (FALSE, 0);
	{
271
	  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
272
	  gtk_container_add (GTK_CONTAINER (frame), vbox);
Elliot Lee's avatar
Elliot Lee committed
273

274
	  hbox = gtk_hbox_new (FALSE, 1);
275
	  {
276
	    gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
277
278
279
	    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
	    
	    label = gtk_label_new (_("Determine file type:"));
280
	    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 5);
281
282
283
	    gtk_widget_show (label);
	    
	    option_menu = gtk_option_menu_new ();
284
	    gtk_box_pack_start (GTK_BOX (hbox), option_menu, FALSE, TRUE, 0);
285
286
287
288
	    gtk_widget_show (option_menu);
	    
	    menus_get_load_menu (&load_menu, NULL);
	    gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), load_menu);
289
	    gtk_box_pack_start (GTK_BOX (open_options), frame, TRUE, TRUE, 5);
290
291
292
293
294
295
296
297
	  }
	  gtk_widget_show (hbox);
	}
	gtk_widget_show (vbox);
      }
      gtk_widget_show (frame);

      /* Preview frame */
298
299
      frame = gtk_frame_new ("");
      open_options_frame = frame;
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
      {
	gtk_frame_set_shadow_type (GTK_FRAME (frame),
				   GTK_SHADOW_ETCHED_IN);
	gtk_box_pack_end (GTK_BOX (open_options), frame, FALSE, TRUE, 5);
	
	vbox = gtk_vbox_new (FALSE, 1);
	{
	  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
	  gtk_container_add (GTK_CONTAINER (frame), vbox);

	  hbox = gtk_hbox_new (TRUE, 1);
	  {
	    gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
	    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

315
	    open_options_genbutton = gtk_button_new();
316
317
318
319
320
321
	    {
	      gtk_signal_connect (GTK_OBJECT (open_options_genbutton),
				  "clicked",
				  (GtkSignalFunc) genbutton_callback,
				  open_options_genbutton);
	      gtk_box_pack_end (GTK_BOX (hbox), open_options_genbutton,
322
				TRUE, FALSE, 0);
323
324
325
	    }
	    gtk_widget_show (open_options_genbutton);	
	    
326
327
	    open_options_fixed = gtk_fixed_new ();
	    {
328
329
330
	      GtkWidget* abox;
	      GtkWidget* sbox;
	      
331
	      gtk_widget_set_usize (open_options_fixed, 80, 60);
332
	      gtk_container_add (GTK_CONTAINER (GTK_BIN (open_options_genbutton)), open_options_fixed);
333
	      
334
	      sbox = gtk_vbox_new(TRUE, 0);
335
	      {
336
337
		GtkWidget* align;

338
		gtk_container_add (GTK_CONTAINER (open_options_fixed),
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
				   GTK_WIDGET (sbox));

		align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
		{
		  gtk_widget_set_usize (align, 80, 60);
		  
		  gtk_box_pack_start (GTK_BOX (sbox),
				      GTK_WIDGET (align),
				      FALSE, TRUE, 0);
		  
		  abox = gtk_hbox_new (FALSE, 0);
		  {
		    gtk_container_add (GTK_CONTAINER (align), abox);
		    
		    open_options_preview = GTK_PREVIEW (gtk_preview_new
							(GTK_PREVIEW_COLOR));
		    {
		      gtk_box_pack_start (GTK_BOX (abox),
					  GTK_WIDGET (open_options_preview),
					  FALSE, TRUE, 0);
		    }
		    gtk_widget_show(GTK_WIDGET (open_options_preview));
		    
		    open_options_genbuttonlabel = gtk_label_new("generate\npreview");
		    {
		      gtk_box_pack_start (GTK_BOX (abox),
					  GTK_WIDGET (open_options_genbuttonlabel),
					  FALSE, TRUE, 0);
		    }
		    gtk_widget_show(GTK_WIDGET (open_options_genbuttonlabel));
		  }
		  gtk_widget_show(abox);
		}
		gtk_widget_show(align);
373
	      }
374
	      gtk_widget_show(sbox);
375
376
377
378
379
	    }
	    gtk_widget_show (open_options_fixed);
	  }
	  gtk_widget_show (hbox);

380
	  open_options_label = gtk_label_new ("");
381
382
383
384
	  {
	    gtk_box_pack_end (GTK_BOX (vbox), open_options_label,
			      FALSE, TRUE, 0);
	  }
385
	  gtk_widget_show (open_options_label); 
386
387
388
389
	}
	gtk_widget_show (vbox);
      }
      gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
390

391
      /* pack the containing open_options hbox into the load-dialog */
Elliot Lee's avatar
Elliot Lee committed
392
      gtk_box_pack_end (GTK_BOX (GTK_FILE_SELECTION (fileload)->main_vbox),
393
			open_options, FALSE, FALSE, 1);
Elliot Lee's avatar
Elliot Lee committed
394
395
    }

396
  gtk_frame_set_label (GTK_FRAME(open_options_frame), _("Preview"));
397
  gtk_label_set_text (GTK_LABEL(open_options_label), "No selection.");
398
399

  gtk_widget_show (GTK_WIDGET(open_options_genbuttonlabel));
400
  gtk_widget_hide (GTK_WIDGET(open_options_preview));
401
402
  gtk_widget_set_sensitive (GTK_WIDGET(open_options_frame), FALSE);

Elliot Lee's avatar
Elliot Lee committed
403
404
405
406
407
408
409
410
411
412
413
414
  gtk_widget_show (open_options);

  file_dialog_show (fileload);
}

void
file_save_callback (GtkWidget *w,
		    gpointer   client_data)
{
  GDisplay *gdisplay;

  gdisplay = gdisplay_active ();
415
  if (!gdisplay) return;
Elliot Lee's avatar
Elliot Lee committed
416
417
418
419
420
421
422
423
424

  /*  Only save if the gimage has been modified  */
  if (gdisplay->gimage->dirty != 0)
    {
      if (gdisplay->gimage->has_filename == FALSE)
	{
	  file_save_as_callback (w, client_data);
	}
      else
425
426
427
428
	{
	  file_save (gdisplay->gimage, gimage_filename (gdisplay->gimage),
		     g_basename (gimage_filename(gdisplay->gimage)), 2);
	}
Elliot Lee's avatar
Elliot Lee committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
    }
}

void
file_save_as_callback (GtkWidget *w,
		       gpointer   client_data)
{
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *option_menu;
  GtkWidget *save_menu;
  GDisplay *gdisplay;

  if (!filesave)
    {
444
      filesave = gtk_file_selection_new (_("Save Image"));
445
      gtk_window_set_wmclass (GTK_WINDOW (filesave), "save_image", "Gimp");
446
      gtk_window_set_position (GTK_WINDOW (filesave), GTK_WIN_POS_MOUSE);
447
      gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (filesave)->cancel_button),
448
			  "clicked",
449
			  GTK_SIGNAL_FUNC (file_dialog_hide),
450
451
452
			  GTK_OBJECT (filesave));
      gtk_signal_connect (GTK_OBJECT (filesave),
			  "delete_event",
453
			  GTK_SIGNAL_FUNC (file_dialog_hide),
454
			  NULL);
Elliot Lee's avatar
Elliot Lee committed
455
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (filesave)->ok_button), "clicked", (GtkSignalFunc) file_save_ok_callback, filesave);
456
      gtk_quit_add_destroy (1, GTK_OBJECT (filesave));
Elliot Lee's avatar
Elliot Lee committed
457
458
459
    }
  else
    {
460
      gtk_widget_set_sensitive (GTK_WIDGET (filesave), TRUE);
Elliot Lee's avatar
Elliot Lee committed
461
462
463
      if (GTK_WIDGET_VISIBLE (filesave))
	return;

464
465
      gtk_file_selection_set_filename (GTK_FILE_SELECTION(filesave),
				       "." G_DIR_SEPARATOR_S);
466
      gtk_window_set_title (GTK_WINDOW (filesave), _("Save Image"));
Elliot Lee's avatar
Elliot Lee committed
467
468
469
    }

  gdisplay = gdisplay_active ();
470
  if (!gdisplay) return;
471
  the_gimage = gdisplay->gimage;
Elliot Lee's avatar
Elliot Lee committed
472
473
474

  if (!save_options)
    {
475
      save_options = gtk_frame_new (_("Save Options"));
Elliot Lee's avatar
Elliot Lee committed
476
477
478
      gtk_frame_set_shadow_type (GTK_FRAME (save_options), GTK_SHADOW_ETCHED_IN);

      hbox = gtk_hbox_new (FALSE, 1);
479
      gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
Elliot Lee's avatar
Elliot Lee committed
480
481
482
      gtk_container_add (GTK_CONTAINER (save_options), hbox);
      gtk_widget_show (hbox);

483
      label = gtk_label_new (_("Determine file type:"));
Elliot Lee's avatar
Elliot Lee committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
      gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
      gtk_widget_show (label);

      option_menu = gtk_option_menu_new ();
      gtk_box_pack_start (GTK_BOX (hbox), option_menu, TRUE, TRUE, 0);
      gtk_widget_show (option_menu);

      menus_get_save_menu (&save_menu, NULL);
      gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), save_menu);
      gtk_box_pack_end (GTK_BOX (GTK_FILE_SELECTION (filesave)->main_vbox),
			save_options, FALSE, FALSE, 5);
    }

  switch (gdisplay->gimage->base_type)
    {
    case RGB:
      file_update_menus (save_procs, RGB_IMAGE);
      break;
    case GRAY:
      file_update_menus (save_procs, GRAY_IMAGE);
      break;
    case INDEXED:
      file_update_menus (save_procs, INDEXED_IMAGE);
      break;
    }

  gtk_widget_show (save_options);

  file_dialog_show (filesave);
}

Manish Singh's avatar
Manish Singh committed
515
516
517
518
519
520
521
522
523
void
file_revert_callback (GtkWidget *w,
		      gpointer   client_data)
{
  GDisplay *gdisplay;
  GimpImage *gimage;
  char *filename, *raw_filename;

  gdisplay = gdisplay_active ();
524
  if (!gdisplay) return;
Manish Singh's avatar
Manish Singh committed
525
526
527
528
529

  if (gdisplay->gimage->has_filename == FALSE)
    g_message (_("Can't revert. No filename associated with this image"));
  else
    {
530
531
      raw_filename = gimage_filename (gdisplay->gimage);
      filename = g_basename (filename);
Manish Singh's avatar
Manish Singh committed
532

533
      gimage = file_open_image (raw_filename, raw_filename, RUN_INTERACTIVE);
534
535
536
537
538

      if (gimage != NULL)
	{
	  undo_free (gimage);
	  gdisplay_reconnect (gdisplay, gimage);
539
	  gimp_image_clean_all (gimage);
540
	}
Manish Singh's avatar
Manish Singh committed
541
542
543
544
545
      else
        g_message (_("Revert failed."));
    }
}

Elliot Lee's avatar
Elliot Lee committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
void
file_load_by_extension_callback (GtkWidget *w,
				 gpointer   client_data)
{
  load_file_proc = NULL;
}

void
file_save_by_extension_callback (GtkWidget *w,
				 gpointer   client_data)
{
  save_file_proc = NULL;
}

static void
file_update_name (PlugInProcDef *proc, GtkWidget *filesel)
{
  if (proc->extensions_list)
    {
      char* text = gtk_entry_get_text (GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry));
      char* last_dot = strrchr (text, '.');
      GString *s;

      if (last_dot == text || !text[0])
	return;

      s = g_string_new (text);

      if (last_dot)
	g_string_truncate (s, last_dot-text);

      g_string_append (s, ".");
      g_string_append (s, (char*) proc->extensions_list->data);

      gtk_entry_set_text (GTK_ENTRY(GTK_FILE_SELECTION(filesel)->selection_entry), s->str);

      g_string_free (s, TRUE);
    }
}

static void
file_load_type_callback (GtkWidget *w,
			 gpointer   client_data)
{
  PlugInProcDef* proc = (PlugInProcDef *) client_data;

  file_update_name (proc, fileload);

  load_file_proc = proc;
}

static void
file_save_type_callback (GtkWidget *w,
			 gpointer   client_data)
{
  PlugInProcDef* proc = (PlugInProcDef *) client_data;

  file_update_name (proc, filesave);

  save_file_proc = proc;
}

Manish Singh's avatar
Manish Singh committed
608
static GimpImage*
609
file_open_image (char *filename, char *raw_filename, RunModeType runmode)
Elliot Lee's avatar
Elliot Lee committed
610
611
612
613
614
{
  PlugInProcDef *file_proc;
  ProcRecord *proc;
  Argument *args;
  Argument *return_vals;
615
  int gimage_id;
Manish Singh's avatar
Manish Singh committed
616
  gboolean status;
Elliot Lee's avatar
Elliot Lee committed
617
618
619
620
621
622
623
624
625
  int i;

  file_proc = load_file_proc;
  if (!file_proc)
    file_proc = file_proc_find (load_procs, filename);

  if (!file_proc)
    {
      /* WARNING */
Manish Singh's avatar
Manish Singh committed
626
      return NULL;
Elliot Lee's avatar
Elliot Lee committed
627
628
629
630
631
632
633
634
635
636
    }

  proc = &file_proc->db_info;

  args = g_new (Argument, proc->num_args);
  memset (args, 0, (sizeof (Argument) * proc->num_args));

  for (i = 0; i < proc->num_args; i++)
    args[i].arg_type = proc->args[i].arg_type;

637
  args[0].value.pdb_int = runmode;
Elliot Lee's avatar
Elliot Lee committed
638
639
640
641
  args[1].value.pdb_pointer = filename;
  args[2].value.pdb_pointer = raw_filename;

  return_vals = procedural_db_execute (proc->name, args);
Manish Singh's avatar
Manish Singh committed
642
  status = (return_vals[0].value.pdb_int == PDB_SUCCESS);
643
  gimage_id = return_vals[1].value.pdb_int;
Elliot Lee's avatar
Elliot Lee committed
644
645
646
647

  procedural_db_destroy_args (return_vals, proc->num_values);
  g_free (args);

Manish Singh's avatar
Manish Singh committed
648
649
650
651
652
653
654
655
656
657
  if (status)
    return pdb_id_to_image (gimage_id);
  else
    return NULL;
}

int
file_open (char *filename, char *raw_filename)
{
  GimpImage *gimage;
658
  GDisplay  *gdisplay;
Manish Singh's avatar
Manish Singh committed
659

660
  if ((gimage = file_open_image (filename, raw_filename, RUN_INTERACTIVE)) != NULL)
Elliot Lee's avatar
Elliot Lee committed
661
    {
Manish Singh's avatar
Manish Singh committed
662
      /* enable & clear all undo steps */
Elliot Lee's avatar
Elliot Lee committed
663
664
      gimage_enable_undo (gimage);

Manish Singh's avatar
Manish Singh committed
665
      /* set the image to clean  */
Elliot Lee's avatar
Elliot Lee committed
666
667
      gimage_clean_all (gimage);

Manish Singh's avatar
Manish Singh committed
668
      /* display the image */
669
670
671
672
673
      gdisplay = gdisplay_new (gimage, 0x0101);

      /* always activate the first display */
      if (g_slist_length (display_list) == 1)
	gimp_context_set_display (gimp_context_get_user (), gdisplay);
Elliot Lee's avatar
Elliot Lee committed
674

675
676
      idea_add (filename);
      menus_last_opened_add (filename);
Manish Singh's avatar
Manish Singh committed
677
678

      return TRUE;
679
    }
Chris Lahey's avatar
Chris Lahey committed
680

Manish Singh's avatar
Manish Singh committed
681
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
682
683
}

684

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
static TempBuf*
make_thumb_tempbuf (GimpImage* gimage)
{  
  gint w,h;

  if (gimage->width<=80 && gimage->height<=60)
    {
      w = gimage->width;
      h = gimage->height;
    }
  else
    {
      /* Ratio molesting to fit within .xvpic thumbnail size limits */
      if (60*gimage->width < 80*gimage->height)
	{
	  h = 60;
	  w = (60*gimage->width)/gimage->height;
	  if (w==0)
	    w = 1;
	}
      else
	{
	  w = 80;
	  h = (80*gimage->height)/gimage->width;
	  if (h==0)
	    h = 1;
	}
    }

  /*printf("tn: %d x %d -> ", w, h);fflush(stdout);*/

  return (gimp_image_composite_preview (gimage, GRAY_CHANNEL, w, h));
}


static guchar*
make_RGBbuf_from_tempbuf (TempBuf* tempbuf, gint* width_rtn, gint* height_rtn)
{  
  int i,j,w,h;
  guchar* tbd;
  guchar* ptr;
  guchar* rtn = NULL;
  guchar alpha,r,g,b;

  w = (*width_rtn) = tempbuf->width;
  h = (*height_rtn) = tempbuf->height;
  tbd = temp_buf_data (tempbuf);

  switch (tempbuf->bytes)
    {
    case 4:
      rtn = ptr = g_malloc (3 * w * h);
      for (i=0; i<h; i++)
	{
	  for (j=0; j<w; j++)
	    {
	      r = *(tbd++);
	      g = *(tbd++);
	      b = *(tbd++);
	      alpha = *(tbd++);

	      if (alpha & 128)
		{
		  *(ptr++) = r;
		  *(ptr++) = g;
		  *(ptr++) = b;
		}
	      else
		{
		  r = (( (i^j) & 4 ) << 5) | 64;
		  *(ptr++) = r;
		  *(ptr++) = r;
		  *(ptr++) = r;
		}
	    }
	}
      break;

    case 2:
      rtn = ptr = g_malloc (3 * w * h);
      for (i=0; i<h; i++)
	{
	  for (j=0; j<w; j++)
	    {
	      r = *(tbd++);
	      alpha = *(tbd++);

	      if (!(alpha & 128))
		r = (( (i^j) & 4 ) << 5) | 64;

	      *(ptr++) = r;
	      *(ptr++) = r;
	      *(ptr++) = r;
	    }
	}
      break;

    default:
      g_warning("UNKNOWN TempBuf width in make_RGBbuf_from_tempbuf()");
    }

  return(rtn);
}


790
791
static gboolean
file_save_thumbnail (GimpImage* gimage,
792
793
		     const char *full_source_filename,
		     TempBuf* tempbuf)
794
795
{
  gint i,j;
796
  gint w,h;
797
  unsigned char* tbd;
798
799
800
801
  gchar* pathname;
  gchar* filename;
  gchar* xvpathname;
  gchar* thumbnailname;
802
803
  GimpImageBaseType basetype;
  FILE* fp;
804
805
806
807
808
809
  struct stat statbuf;

  if (stat(full_source_filename, &statbuf) != 0)
    {
      return FALSE;
    }
810

811
  if (gimp_image_preview_valid (gimage, GRAY_CHANNEL))
812
    {
813
814
      /* just for debugging */
      printf("(incidentally, gimage already has a valid preview - %dx%d)\n",
815
816
817
818
	     gimage->comp_preview->width,
	     gimage->comp_preview->height);
    }

819
820
  pathname = g_dirname(full_source_filename);
  filename = g_basename(full_source_filename); /* Don't free! */
821

822
823
824
825
826
827
  xvpathname = g_strconcat(pathname, G_DIR_SEPARATOR_S, ".xvpics",
			   NULL);

  thumbnailname = g_strconcat (xvpathname, G_DIR_SEPARATOR_S,
			       filename,
			       NULL);
828
829
830
831
832

  tbd = temp_buf_data(tempbuf);

  w = tempbuf->width;
  h = tempbuf->height;
833
  /*printf("tn: %d x %d\n", w, h);fflush(stdout);*/
834

835
  mkdir (xvpathname, 0755);
836

837
838
839
840
  fp = fopen(thumbnailname,"wb");
  g_free(pathname);
  g_free(xvpathname);
  g_free(thumbnailname);
841
842
843
844
845
846

  if (fp)
    {
      basetype = gimp_image_base_type(gimage);

      fprintf(fp,
847
	      "P7 332\n#IMGINFO:%dx%d %s (%d %s)\n"
848
849
850
851
852
853
	      "#END_OF_COMMENTS\n%d %d 255\n",
	      gimage->width, gimage->height,
	      (basetype == RGB) ? "RGB" :
	      (basetype == GRAY) ? "Greyscale" :
	      (basetype == INDEXED) ? "Indexed" :
	      "(UNKNOWN COLOUR TYPE)",
854
855
	      (int)statbuf.st_size,
	      (statbuf.st_size == 1) ? "byte" : "bytes",
856
857
858
859
860
861
	      w, h);

      switch (basetype)
	{
	case INDEXED:
	case RGB:
862
	  for (i=0; i<h; i++)
863
	    {
864
	      /* Do a cheap unidirectional error-spread to look better */
865
	      gint rerr=0, gerr=0, berr=0, a;
866
867

	      for (j=0; j<w; j++)
868
		{
869
870
		  gint32 r,g,b;

871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
		  if (128 & *(tbd + 3))
		    {
		      r = *(tbd++) + rerr;
		      g = *(tbd++) + gerr;
		      b = *(tbd++) + berr;
		      tbd++;
		    }
		  else
		    {
		      a = (( (i^j) & 4 ) << 5) | 64; /* cute. */
		      r = a + rerr;
		      g = a + gerr;
		      b = a + berr;
		      tbd += 4;
		    }
886
887
888
889
890

		  r = CLAMP0255 (r);
		  g = CLAMP0255 (g);
		  b = CLAMP0255 (b);

891
		  fputc(((r>>5)<<5) | ((g>>5)<<2) | (b>>6), fp);
892
893
894
895

		  rerr = r - ( (r>>5) * 255 ) / 7;
		  gerr = g - ( (g>>5) * 255 ) / 7;
		  berr = b - ( (b>>6) * 255 ) / 3;
896
897
898
899
		}
	    }
	  break;
	case GRAY:
900
	  for (i=0; i<h; i++)
901
	    {
902
	      /* Do a cheap unidirectional error-spread to look better */
903
	      gint b3err=0, b2err=0, v, a;
904
905

	      for (j=0; j<w; j++)
906
		{
907
		  gint32 b3, b2;
908

909
		  v = *(tbd++);
910
911
912
913
914
		  a = *(tbd++);

		  if (!(128 & a))
		    v = (( (i^j) & 4 ) << 5) | 64;

915
916
917
918
919
920
921
922
923
924
		  b2 = v + b2err;
		  b3 = v + b3err;

		  b2 = CLAMP0255 (b2);
		  b3 = CLAMP0255 (b3);

		  fputc(((b3>>5)<<5) | ((b3>>5)<<2) | (b2>>6), fp);

		  b2err = b2 - ( (b2>>6) * 255 ) / 3;
		  b3err = b3 - ( (b3>>5) * 255 ) / 7;
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
		}
	    }
	  break;
	default:
	  g_warning("UNKNOWN GIMAGE TYPE IN THUMBNAIL SAVE");
	}
      fclose(fp);
    }
  else /* Error writing thumbnail */
    {
      return (FALSE);
    }

  return (TRUE);
}


Elliot Lee's avatar
Elliot Lee committed
942
int
943
file_save (GimpImage* gimage,
Elliot Lee's avatar
Elliot Lee committed
944
	   char *filename,
945
946
	   char *raw_filename,
           gint mode)
Elliot Lee's avatar
Elliot Lee committed
947
948
949
950
951
952
953
954
{
  PlugInProcDef *file_proc;
  ProcRecord *proc;
  Argument *args;
  Argument *return_vals;
  int return_val;
  int i;

955
956
  if (gimage_active_drawable (gimage) == NULL)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
957

958
959
  file_proc = gimage_get_save_proc(gimage);

Elliot Lee's avatar
Elliot Lee committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
  if (!file_proc)
    file_proc = file_proc_find (save_procs, raw_filename);

  if (!file_proc)
    return FALSE;

  proc = &file_proc->db_info;

  args = g_new (Argument, proc->num_args);
  memset (args, 0, (sizeof (Argument) * proc->num_args));

  for (i = 0; i < proc->num_args; i++)
    args[i].arg_type = proc->args[i].arg_type;

974
  args[0].value.pdb_int = mode;
975
  args[1].value.pdb_int = pdb_image_to_id(gimage);
976
  args[2].value.pdb_int = drawable_ID (gimage_active_drawable (gimage));
Elliot Lee's avatar
Elliot Lee committed
977
978
979
980
981
982
983
984
985
986
987
  args[3].value.pdb_pointer = filename;
  args[4].value.pdb_pointer = raw_filename;

  return_vals = procedural_db_execute (proc->name, args);
  return_val = (return_vals[0].value.pdb_int == PDB_SUCCESS);

  if (return_val)
    {
      /*  set this image to clean  */
      gimage_clean_all (gimage);

988
989
      /* these calls must come before the call to gimage_set_filename */
      idea_add (filename);
990
      menus_last_opened_add (filename);
991
992

      /*  use the same plug-in for this image next time  */
993
994
995
996
      /* DISABLED - gets stuck on first saved format... needs
	 attention --Adam */
      /* gimage_set_save_proc(gimage, file_proc); */

997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
      /* Write a thumbnail for the saved image, where appropriate */
      switch (thumbnail_mode)
	{
	case 0:
	  break;
	default:
	  {
	    TempBuf* tempbuf;
	    tempbuf = make_thumb_tempbuf (gimage);
	    file_save_thumbnail (gimage, filename, tempbuf);
	  }
	}
1009
1010
1011
1012
      
      /*  set the image title  */
      gimp_image_set_filename (gimage, filename);
      /* note: 'filename' may have been free'd by above call! */
Elliot Lee's avatar
Elliot Lee committed
1013
1014
1015
1016
1017
    }

  g_free (return_vals);
  g_free (args);

1018

Elliot Lee's avatar
Elliot Lee committed
1019
1020
1021
1022
  return return_val;
}


1023
1024
1025
1026
1027
1028
/* The readXVThumb function source may be re-used under
   the XFree86-style license. <adam@gimp.org> */
static guchar*
readXVThumb(const gchar *fnam,
	    gint* w, gint* h,
	    gchar** imginfo /* caller frees if != NULL */)
1029
1030
1031
1032
1033
1034
1035
1036
1037
{
  FILE *fp;
  const gchar *P7_332 = "P7 332";
  gchar P7_buf[7];
  gchar linebuf[200];
  guchar *buf;
  gint twofivefive;
  void *ptr;

1038
1039
1040
  *w = *h = 0;
  *imginfo = NULL;

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
  fp = fopen (fnam, "rb");
  if (!fp)
    return (NULL);

  fread (P7_buf, 6, 1, fp);

  if (strncmp(P7_buf, P7_332, 6)!=0)
    {
      g_warning("Thumbnail doesn't have the 'P7 332' header.");
      fclose(fp);
      return(NULL);
    }

  /*newline*/
  fread (P7_buf, 1, 1, fp);

  do
    {
      ptr = fgets(linebuf, 199, fp);
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
      if ((strncmp(linebuf, "#IMGINFO:", 9) == 0) &&
	  (linebuf[9] != '\0') &&
	  (linebuf[9] != '\n'))
	{
	  if (linebuf[strlen(linebuf)-1] == '\n')
	    linebuf[strlen(linebuf)-1] = '\0';

	  if (linebuf[9] != '\0')
	    {
	      if (*imginfo)
		g_free(*imginfo);
	      *imginfo = g_strdup (&linebuf[9]);
	    }
	}
1074
1075
1076
    }
  while (ptr && linebuf[0]=='#'); /* keep throwing away comment lines */

1077
1078
1079
1080
1081
1082
1083
  if (!ptr)
    {
      /* g_warning("Thumbnail ended - not an image?"); */
      fclose(fp);
      return(NULL);
    }

1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  sscanf(linebuf, "%d %d %d\n", w, h, &twofivefive);

  if (twofivefive!=255)
    {
      g_warning("Thumbnail is of funky depth.");
      fclose(fp);
      return(NULL);
    }

  if ((*w)<1 || (*h)<1 || (*w)>80 || (*h)>60)
    {
      g_warning ("Thumbnail size bad.  Corrupted?");
      fclose(fp);
      return (NULL);
    }

  buf = g_malloc((*w)*(*h));

  fread(buf, (*w)*(*h), 1, fp);
  
  fclose(fp);
  
  return(buf);
}


1110
/* don't call with preview_fullname as parameter!  will be clobbered! */
1111
static void
1112
set_preview (const gchar* fullfname, guchar* RGB_source, gint RGB_w, gint RGB_h)
1113
1114
1115
{
  guchar *thumb_rgb;
  guchar *raw_thumb;
1116
  gint    tnw,tnh, i;
1117
1118
1119
1120
1121
1122
1123
  gchar  *pname;
  gchar  *fname;
  gchar  *tname;
  gchar  *imginfo;
  struct stat file_stat;
  struct stat thumb_stat;
  gboolean thumb_may_be_outdated = FALSE;
1124
1125
  gboolean show_generate_label = TRUE;

1126

1127
1128
  pname = g_dirname (fullfname);
  fname = g_basename (fullfname); /* Don't free this! */
1129
1130
1131
1132
1133
1134
  tname = g_strconcat (pname, G_DIR_SEPARATOR_S, ".xvpics", G_DIR_SEPARATOR_S,
		       fname,
		       NULL);

  g_free (pname);

1135
1136
  /* If the file is newer than its thumbnail, the thumbnail may
     be out of date. */
1137
1138
  if ((stat(tname,     &thumb_stat)==0) &&
      (stat(fullfname, &file_stat )==0))
1139
1140
1141
1142
1143
1144
    {
      if ((thumb_stat.st_mtime) < (file_stat.st_mtime))
	{
	  thumb_may_be_outdated = TRUE;
	}
    }
1145

1146
  raw_thumb = readXVThumb(tname, &tnw, &tnh, &imginfo);
1147

1148
1149
  g_free (tname);

1150
1151
  gtk_frame_set_label (GTK_FRAME(open_options_frame),
		       fname);
1152

1153
  if (preview_fullname)
1154
1155
1156
    {
      g_free (preview_fullname);
    }
1157
  preview_fullname = g_strdup (fullfname);
1158

1159

1160
1161
1162
1163
1164
  if (RGB_source)
    {
      gtk_preview_size (open_options_preview, RGB_w, RGB_h);
      
      for (i=0; i<RGB_h; i++)
1165
	{
1166
1167
1168
	  gtk_preview_draw_row(open_options_preview, &RGB_source[3*i*RGB_w],
			       0, i,
			       RGB_w);
1169
	}
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
    }
  else
    {
      if (raw_thumb)
	{
	  thumb_rgb = g_malloc (3 * tnw * tnh);
	  
	  for (i=0; i<tnw*tnh; i++)
	    {
	      thumb_rgb[i*3  ] = ((raw_thumb[i]>>5)*255)/7;
	      thumb_rgb[i*3+1] = (((raw_thumb[i]>>2)&7)*255)/7;
	      thumb_rgb[i*3+2] = (((raw_thumb[i])&3)*255)/3;
	    }
	  
	  gtk_preview_size (open_options_preview, tnw, tnh);
	      
	  for (i=0; i<tnh; i++)
	    {
	      gtk_preview_draw_row(open_options_preview, &thumb_rgb[3*i*tnw],
				   0, i,
				   tnw);
	    }
1192
      
1193
1194
1195
	  g_free (thumb_rgb);
	}
    }
1196

1197
1198
1199
  if (raw_thumb || RGB_source)  /* We can show *some* kind of preview. */
    {
      if (raw_thumb) /* Managed to commit thumbnail file to disk */
1200
	{
1201
1202
1203
1204
1205
1206
1207
	  gtk_label_set_text (GTK_LABEL(open_options_label),
			      thumb_may_be_outdated ?
			      "(this thumbnail may be out of date)" :
			      (imginfo ? imginfo : "(no information)"));
	}
      else
	{
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
	  switch (thumbnail_mode)
	    {
	    case 0:
	      gtk_label_set_text (GTK_LABEL(open_options_label),
				  "(thumbnail saving is disabled)");
	      break;
	    case 1:
	      gtk_label_set_text (GTK_LABEL(open_options_label),
				  "(could not write thumbnail file)");
	      break;
	    default:
	      gtk_label_set_text (GTK_LABEL(open_options_label),
				  "(thumbnail file not written)");
	    }
1222
	}
1223
1224
      gtk_widget_show (GTK_WIDGET(open_options_preview));
      gtk_widget_queue_draw (GTK_WIDGET(open_options_preview));
1225
      
1226
      show_generate_label = FALSE;
1227
1228
1229
1230
1231
      
      g_free (raw_thumb);
    }
  else
    {
1232
1233
      if (imginfo)
	g_free(imginfo);
1234
      
1235
      gtk_widget_hide (GTK_WIDGET(open_options_preview));
1236
1237
1238
      gtk_label_set_text (GTK_LABEL(open_options_label),
			  "no preview available");
    }
1239

1240
1241
1242
1243
1244
  if (show_generate_label)
    {
      gtk_widget_hide (GTK_WIDGET(open_options_preview));
      gtk_widget_show (GTK_WIDGET(open_options_genbuttonlabel));
    }
1245
  else
1246
1247
1248
1249
    {
      gtk_widget_hide (GTK_WIDGET(open_options_genbuttonlabel));
      gtk_widget_show (GTK_WIDGET(open_options_preview));
    }
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
}


static void
file_open_clistrow_callback (GtkWidget *w,
			     int client_data)
{
  gchar  *fullfname = NULL;

  fullfname = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileload));

1261
  gtk_widget_set_sensitive (GTK_WIDGET(open_options_frame), TRUE);
1262
  set_preview (fullfname, NULL, 0, 0);
1263
1264
1265
1266
1267
1268
1269
1270
}


static void
genbutton_callback (GtkWidget *w,
		    gpointer   client_data)
{
  GimpImage* gimage_to_be_thumbed;
1271
  gchar* filename;
1272
1273
1274
1275
1276
1277

  if (!preview_fullname)
    {
      g_warning ("Tried to generate thumbnail for NULL filename.");
      return;
    }
1278
1279
  
  filename = g_strdup(preview_fullname);
1280
1281
1282

  gimp_add_busy_cursors();
  gtk_widget_set_sensitive (GTK_WIDGET (fileload), FALSE);
1283
1284
1285
  if ((gimage_to_be_thumbed = file_open_image (filename,
 					       g_basename(filename),
					       RUN_NONINTERACTIVE)))
1286
    {
1287
1288
1289
1290
1291
1292
      guchar* RGBbuf;
      gint RGBbuf_w, RGBbuf_h;
      TempBuf* tempbuf;

      tempbuf = make_thumb_tempbuf (gimage_to_be_thumbed);
      RGBbuf = make_RGBbuf_from_tempbuf (tempbuf, &RGBbuf_w, &RGBbuf_h);
1293
1294
1295
1296
1297
1298
1299
      switch (thumbnail_mode)
	{
	case 0:
	  break;
	default:
	  file_save_thumbnail (gimage_to_be_thumbed, filename, tempbuf);
	}
1300
1301
      set_preview (filename, RGBbuf, RGBbuf_w, RGBbuf_h);

1302
      gimage_delete (gimage_to_be_thumbed);
1303
1304
1305

      if (RGBbuf)
	g_free (RGBbuf);
1306
    }
1307
1308
1309
1310
1311
1312
  else
    {
      gtk_label_set_text (GTK_LABEL(open_options_label),
			  "(could not make preview)");
    }

1313
  gtk_widget_set_sensitive (GTK_WIDGET (fileload), TRUE);
1314
  gimp_remove_busy_cursors (NULL);
1315
1316

  g_free (filename);
1317
1318
1319
}


Elliot Lee's avatar
Elliot Lee committed
1320
1321
1322
1323
1324
static void
file_open_ok_callback (GtkWidget *w,
		       gpointer   client_data)
{
  GtkFileSelection *fs;
1325
1326
  char* filename, *raw_filename, *mfilename;
  gchar* filedirname;
Elliot Lee's avatar
Elliot Lee committed
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
  struct stat buf;
  int err;
  GString *s;

  fs = GTK_FILE_SELECTION (client_data);
  filename = gtk_file_selection_get_filename (fs);
  raw_filename = gtk_entry_get_text (GTK_ENTRY(fs->selection_entry));

  g_assert (filename && raw_filename);

1337
1338
1339
  if (strlen (raw_filename) == 0)
    return;

Elliot Lee's avatar
Elliot Lee committed
1340
1341
1342
1343
1344
  err = stat (filename, &buf);

  if (err == 0 && (buf.st_mode & S_IFDIR))
    {
      GString *s = g_string_new (filename);
1345
      if (s->str[s->len - 1] != G_DIR_SEPARATOR)
Elliot Lee's avatar
Elliot Lee committed
1346
        {
1347
          g_string_append_c (s, G_DIR_SEPARATOR);
Elliot Lee's avatar
Elliot Lee committed
1348
1349
1350
1351
1352
1353
        }
      gtk_file_selection_set_filename (fs, s->str);
      g_string_free (s, TRUE);
      return;
    }

1354
1355
  gtk_widget_set_sensitive (GTK_WIDGET (fs), FALSE);

1356
1357
1358
  if (err)
    filename = raw_filename;

Elliot Lee's avatar
Elliot Lee committed
1359
1360
1361
  if (file_open (filename, raw_filename))
    {
      file_dialog_hide (client_data);
1362
      gtk_widget_set_sensitive (GTK_WIDGET (fs), TRUE);
1363
1364
1365
    }
  else
    {
1366
      s = g_string_new (_("Open failed: "));
1367
      g_string_append (s, raw_filename);
1368
      FILE_ERR_MESSAGE (s->str);
1369
      g_string_free (s, TRUE);
Elliot Lee's avatar
Elliot Lee committed
1370
1371
1372
    }


1373
1374
1375
1376
1377
1378
  /*
   * Now deal with multiple selections from the filesel clist
   */

  /*
   * FIXME: cope with cwd != fileselcwd != dirname(filename)
1379
   */
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395