app_procs.c 20.4 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
/* 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18

19
#include "config.h"
20

Elliot Lee's avatar
Elliot Lee committed
21 22 23 24
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
25
#ifdef HAVE_SYS_PARAM_H
Elliot Lee's avatar
Elliot Lee committed
26
#include <sys/param.h>
27
#endif
28
#include <sys/types.h>
29
#ifdef HAVE_DIRENT_H
30
#include <dirent.h>
31 32
#endif
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
33
#include <unistd.h>
34
#endif
Elliot Lee's avatar
Elliot Lee committed
35

36 37 38
#include <gtk/gtk.h>
#include <gdk/gdktypes.h>

39
#ifdef G_OS_WIN32
40 41 42
#include <process.h>		/* For _getpid() */
#endif
 
43 44
#include "libgimpwidgets/gimpwidgets.h"

Sven Neumann's avatar
Sven Neumann committed
45 46
#include "apptypes.h"

Elliot Lee's avatar
Elliot Lee committed
47 48 49
#include "appenv.h"
#include "app_procs.h"
#include "batch.h"
50
#include "brush_select.h"
Elliot Lee's avatar
Elliot Lee committed
51 52
#include "color_transfer.h"
#include "colormaps.h"
53
#include "context_manager.h"
54
#include "devices.h"
55
#include "errorconsole.h"
Elliot Lee's avatar
Elliot Lee committed
56
#include "fileops.h"
57 58
#include "gdisplay.h"
#include "gdisplay_ops.h"
59
#include "gimpcontext.h"
60
#include "gimpdatafactory.h"
Michael Natterer's avatar
Michael Natterer committed
61
#include "gimpimage.h"
Elliot Lee's avatar
Elliot Lee committed
62
#include "gimprc.h"
63
#include "gimpparasite.h"
Elliot Lee's avatar
Elliot Lee committed
64
#include "global_edit.h"
Michael Natterer's avatar
Michael Natterer committed
65
#include "gradient_select.h"
Elliot Lee's avatar
Elliot Lee committed
66 67
#include "gximage.h"
#include "image_render.h"
68
#include "lc_dialog.h"
Elliot Lee's avatar
Elliot Lee committed
69 70 71
#include "menus.h"
#include "paint_funcs.h"
#include "palette.h"
72
#include "pattern_select.h"
Elliot Lee's avatar
Elliot Lee committed
73
#include "plug_in.h"
74
#include "module_db.h"
75
#include "session.h"
Elliot Lee's avatar
Elliot Lee committed
76 77 78
#include "temp_buf.h"
#include "tile_swap.h"
#include "tips_dialog.h"
79
#include "toolbox.h"
Elliot Lee's avatar
Elliot Lee committed
80
#include "undo.h"
81
#include "unitrc.h"
Elliot Lee's avatar
Elliot Lee committed
82
#include "xcf.h"
83
#include "errors.h"
84
#include "docindex.h"
85
#include "colormap_dialog.h"
Elliot Lee's avatar
Elliot Lee committed
86

87 88 89 90
#ifdef DISPLAY_FILTERS
#include "gdisplay_color.h"
#endif /* DISPLAY_FILTERS */

91 92
#include "color_notebook.h"
#include "color_select.h"
93
#include "gimpparasite.h"
94

95 96 97
#include "tools/curves.h"
#include "tools/hue_saturation.h"
#include "tools/levels.h"
98
#include "tools/tool_options_dialog.h"
99

100 101
#include "pdb/internal_procs.h"

102
#include "libgimp/gimplimits.h"
103 104 105
#include "libgimp/gimpfeatures.h"
#include "libgimp/gimpenv.h"

106 107
#include "libgimp/gimpintl.h"

108

109
#define LOGO_WIDTH_MIN  300
110
#define LOGO_HEIGHT_MIN 110
Sven Neumann's avatar
Sven Neumann committed
111
#define AUTHORS "Spencer Kimball & Peter Mattis"
112

113 114
#define SHOW_NEVER 0
#define SHOW_LATER 1
115
#define SHOW_NOW   2
116

Elliot Lee's avatar
Elliot Lee committed
117
/*  Function prototype for affirmation dialog when exiting application  */
118 119 120 121 122 123 124 125 126
static void      really_quit_dialog                   (void);
static void      make_initialization_status_window    (void);
static void      destroy_initialization_status_window (void);
static gboolean  splash_logo_load                     (GtkWidget *window);
static gboolean  splash_logo_load_size                (GtkWidget *window);
static void      splash_logo_draw                     (GtkWidget *widget);
static void      splash_text_draw                     (GtkWidget *widget);
static void      splash_logo_expose                   (GtkWidget *widget);
static void      toast_old_temp_files                 (void);
Elliot Lee's avatar
Elliot Lee committed
127 128


129 130
static gboolean is_app_exit_finish_done = FALSE;
gboolean        we_are_exiting          = FALSE;
131 132 133 134 135 136 137 138 139

static GtkWidget *logo_area   = NULL;
static GdkPixmap *logo_pixmap = NULL;
static gint logo_width        = 0;
static gint logo_height       = 0;
static gint logo_area_width   = 0;
static gint logo_area_height  = 0;
static gint show_logo         = SHOW_NEVER;
static gint max_label_length  = MAXPATHLEN;
Tim Janik's avatar
Tim Janik committed
140

Elliot Lee's avatar
Elliot Lee committed
141 142

void
143 144
gimp_init (gint    gimp_argc,
	   gchar **gimp_argv)
Elliot Lee's avatar
Elliot Lee committed
145 146 147 148 149 150 151 152 153
{
  /* Initialize the application */
  app_init ();

  /* Parse the rest of the command line arguments as images to load */
  if (gimp_argc > 0)
    while (gimp_argc--)
      {
	if (*gimp_argv)
154
	  file_open (*gimp_argv, *gimp_argv);
Elliot Lee's avatar
Elliot Lee committed
155 156 157 158
	gimp_argv++;
      }

  batch_init ();
159 160 161 162

  /* Handle showing dialogs with gdk_quit_adds here  */
  if (!no_interface && show_tips)
    tips_dialog_create ();
Elliot Lee's avatar
Elliot Lee committed
163 164
}

165
static gboolean
166 167
splash_logo_load_size (GtkWidget *window)
{
168
  gchar buf[1024];
169 170 171 172 173
  FILE *fp;

  if (logo_pixmap)
    return TRUE;

Michael Natterer's avatar
Michael Natterer committed
174
  g_snprintf (buf, sizeof(buf), "%s" G_DIR_SEPARATOR_S "gimp_splash.ppm",
Tor Lillqvist's avatar
Tor Lillqvist committed
175
	      gimp_data_directory ());
176

177
  fp = fopen (buf, "rb");
178
  if (!fp)
179
    return FALSE;
180

181
  fgets (buf, sizeof (buf), fp);
182 183 184
  if (strcmp (buf, "P6\n") != 0)
    {
      fclose (fp);
185
      return FALSE;
186 187
    }

188 189
  fgets (buf, sizeof (buf), fp);
  fgets (buf, sizeof (buf), fp);
190 191 192
  sscanf (buf, "%d %d", &logo_width, &logo_height);

  fclose (fp);
193

194 195 196
  return TRUE;
}

197
static gboolean
198 199 200
splash_logo_load (GtkWidget *window)
{
  GtkWidget *preview;
201
  GdkGC *gc;
202 203
  gchar   buf[1024];
  guchar *pixelrow;
204
  FILE *fp;
205 206
  gint count;
  gint i;
207 208 209 210

  if (logo_pixmap)
    return TRUE;

Michael Natterer's avatar
Michael Natterer committed
211
  g_snprintf (buf, sizeof(buf), "%s" G_DIR_SEPARATOR_S "gimp_splash.ppm",
Tor Lillqvist's avatar
Tor Lillqvist committed
212
	      gimp_data_directory ());
213

214
  fp = fopen (buf, "rb");
215
  if (!fp)
216
    return FALSE;
217

218
  fgets (buf, sizeof (buf), fp);
219 220 221
  if (strcmp (buf, "P6\n") != 0)
    {
      fclose (fp);
222
      return FALSE;
223 224
    }

225 226
  fgets (buf, sizeof (buf), fp);
  fgets (buf, sizeof (buf), fp);
227 228
  sscanf (buf, "%d %d", &logo_width, &logo_height);

229
  fgets (buf, sizeof (buf), fp);
230 231 232
  if (strcmp (buf, "255\n") != 0)
    {
      fclose (fp);
233
      return FALSE;
234 235 236 237 238 239 240 241 242 243 244 245 246 247
    }

  preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_preview_size (GTK_PREVIEW (preview), logo_width, logo_height);
  pixelrow = g_new (guchar, logo_width * 3);

  for (i = 0; i < logo_height; i++)
    {
      count = fread (pixelrow, sizeof (unsigned char), logo_width * 3, fp);
      if (count != (logo_width * 3))
	{
	  gtk_widget_destroy (preview);
	  g_free (pixelrow);
	  fclose (fp);
248
	  return FALSE;
249 250 251 252 253
	}
      gtk_preview_draw_row (GTK_PREVIEW (preview), pixelrow, 0, i, logo_width);
    }

  gtk_widget_realize (window);
254 255 256
  logo_pixmap = gdk_pixmap_new (window->window, logo_width, logo_height,
				gtk_preview_get_visual ()->depth);
  gc = gdk_gc_new (logo_pixmap);
257
  gtk_preview_put (GTK_PREVIEW (preview),
258
		   logo_pixmap, gc,
259
		   0, 0, 0, 0, logo_width, logo_height);
260
  gdk_gc_destroy (gc);
261

262
  gtk_widget_unref (preview);
263 264 265
  g_free (pixelrow);

  fclose (fp);
266

267 268 269 270 271 272
  return TRUE;
}

static void
splash_text_draw (GtkWidget *widget)
{
273
  GdkFont *font;
274

275
  /* this is a font, provide only one single font definition */
Sven Neumann's avatar
Sven Neumann committed
276
  font = gdk_font_load (_("-*-helvetica-bold-r-normal--*-140-*-*-*-*-*-*"));
277
  if (!font)
278
    {
279 280 281 282 283 284 285 286 287 288 289 290 291
      GtkStyle *style = gtk_widget_get_style (widget);
      font = style->font;
      gdk_font_ref (font);
    }
  
  gdk_draw_string (widget->window,
		   font,
		   widget->style->fg_gc[GTK_STATE_NORMAL],
		   ((logo_area_width - gdk_string_width (font,  _("The GIMP"))) / 2),
		   (0.25 * logo_area_height),
		   _("The GIMP"));

  gdk_font_unref (font);
292
  
293 294 295 296 297 298 299
  /* this is a fontset, e.g. multiple comma-separated font definitions */
  font = gdk_fontset_load (_("-*-helvetica-bold-r-normal--*-120-*-*-*-*-*-*,*"));
  if (!font)
    {
      GtkStyle *style = gtk_widget_get_style (widget);
      font = style->font;
      gdk_font_ref (font);
300
    }
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

  gdk_draw_string (widget->window,
		   font,
		   widget->style->fg_gc[GTK_STATE_NORMAL],
		   ((logo_area_width - gdk_string_width (font, GIMP_VERSION)) / 2),
		   (0.45 * logo_area_height),
		   GIMP_VERSION);
  gdk_draw_string (widget->window,
		   font,
		   widget->style->fg_gc[GTK_STATE_NORMAL],
		   ((logo_area_width - gdk_string_width (font, _("brought to you by"))) / 2),
		   (0.65 * logo_area_height),
		   _("brought to you by"));
  gdk_draw_string (widget->window,
		   font,
		   widget->style->fg_gc[GTK_STATE_NORMAL],
		   ((logo_area_width - gdk_string_width (font, AUTHORS)) / 2),
		   (0.80 * logo_area_height),
		   AUTHORS);

  gdk_font_unref (font);
322 323 324 325 326 327 328
}

static void
splash_logo_draw (GtkWidget *widget)
{
  gdk_draw_pixmap (widget->window,
		   widget->style->black_gc,
329
		   logo_pixmap,
330
		   0, 0,
331 332
		   ((logo_area_width - logo_width) / 2),
		   ((logo_area_height - logo_height) / 2),
333 334 335 336 337 338
		   logo_width, logo_height);
}

static void
splash_logo_expose (GtkWidget *widget)
{
339 340 341 342 343 344 345 346 347 348
  switch (show_logo)
    {
    case SHOW_NEVER:
    case SHOW_LATER:
      splash_text_draw (widget);
      break;
    case SHOW_NOW:
      splash_logo_draw (widget);
      break;
    }
349 350
}

351 352 353
static GtkWidget *win_initstatus = NULL;
static GtkWidget *label1 = NULL;
static GtkWidget *label2 = NULL;
354
static GtkWidget *pbar   = NULL;
355 356

static void
357
destroy_initialization_status_window (void)
358
{
Tor Lillqvist's avatar
Tor Lillqvist committed
359
  if (win_initstatus)
360
    {
361
      gtk_widget_destroy (win_initstatus);
362
      if (logo_pixmap != NULL)
363 364
	gdk_pixmap_unref (logo_pixmap);

365 366
      win_initstatus = label1 = label2 = pbar = logo_area = NULL;
      logo_pixmap = NULL;
367 368 369 370
    }
}

static void
371
make_initialization_status_window (void)
372
{
373 374 375
  GtkWidget *vbox;
  GtkWidget *logo_hbox;
  GtkStyle  *style;
376

377 378
  if (no_interface || no_splash)
    return;
379

380
  win_initstatus = gtk_window_new (GTK_WINDOW_DIALOG);
381

382 383 384 385
  gtk_window_set_title (GTK_WINDOW (win_initstatus), _("GIMP Startup"));
  gtk_window_set_wmclass (GTK_WINDOW (win_initstatus), "gimp_startup", "Gimp");
  gtk_window_set_position (GTK_WINDOW (win_initstatus), GTK_WIN_POS_CENTER);
  gtk_window_set_policy (GTK_WINDOW (win_initstatus), FALSE, FALSE, FALSE);
Michael Natterer's avatar
Michael Natterer committed
386

387
  gimp_dialog_set_icon (GTK_WINDOW (win_initstatus));
388

389 390 391 392 393
  if (no_splash_image == FALSE &&
      splash_logo_load_size (win_initstatus))
    {
      show_logo = SHOW_LATER;
    }
394

395 396
  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_add (GTK_CONTAINER (win_initstatus), vbox);
397

398 399
  logo_hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), logo_hbox, FALSE, TRUE, 0);
400

401
  logo_area = gtk_drawing_area_new ();
402

403 404 405
  gtk_signal_connect (GTK_OBJECT (logo_area), "expose_event",
		      GTK_SIGNAL_FUNC (splash_logo_expose),
		      NULL);
406

407 408
  logo_area_width  = MAX (logo_width, LOGO_WIDTH_MIN);
  logo_area_height = MAX (logo_height, LOGO_HEIGHT_MIN);
409

410 411 412
  gtk_drawing_area_size (GTK_DRAWING_AREA (logo_area),
			 logo_area_width, logo_area_height);
  gtk_box_pack_start (GTK_BOX(logo_hbox), logo_area, TRUE, FALSE, 0);
413

414 415 416 417
  label1 = gtk_label_new ("");
  gtk_box_pack_start_defaults (GTK_BOX (vbox), label1);
  label2 = gtk_label_new ("");
  gtk_box_pack_start_defaults (GTK_BOX (vbox), label2);
418

419 420
  pbar = gtk_progress_bar_new ();
  gtk_box_pack_start_defaults (GTK_BOX (vbox), pbar);
421

422 423 424 425 426 427
  gtk_widget_show (vbox);
  gtk_widget_show (logo_hbox);
  gtk_widget_show (logo_area);
  gtk_widget_show (label1);
  gtk_widget_show (label2);
  gtk_widget_show (pbar);
428

429 430 431 432 433 434 435 436 437 438
  gtk_widget_show (win_initstatus);

  /*  This is a hack: we try to compute a good guess for the maximum 
   *  number of charcters that will fit into the splash-screen using 
   *  the default_font
   */
  style = gtk_widget_get_style (win_initstatus);
  max_label_length = (0.8 * (gfloat) strlen (AUTHORS) *
		      ((gfloat) logo_area_width /
		       (gfloat) gdk_string_width (style->font, AUTHORS)));
439 440 441
}

void
442 443
app_init_update_status (gchar *label1val,
		        gchar *label2val,
444
		        float  pct_progress)
445
{
446
  gchar *temp;
447

448
  if (!no_interface && !no_splash && win_initstatus)
449
    {
450
      if (label1val && strcmp (label1val, GTK_LABEL (label1)->label))
451
	gtk_label_set_text (GTK_LABEL (label1), label1val);
452 453

      if (label2val && strcmp (label2val, GTK_LABEL (label2)->label))
454
	{
455
	  while (strlen (label2val) > max_label_length)
456
	    {
457
	      temp = strchr (label2val, G_DIR_SEPARATOR);
458 459 460 461 462
	      if (temp == NULL)  /* for sanity */
		break;
	      temp++;
	      label2val = temp;
	    }
463

464
	  gtk_label_set_text (GTK_LABEL (label2), label2val);
465
	}
466

467 468
      if (pct_progress >= 0.0 && pct_progress <= 1.0 && 
	  gtk_progress_get_current_percentage (&(GTK_PROGRESS_BAR (pbar)->progress)) != pct_progress)
469 470 471
	/*
	 GTK_PROGRESS_BAR(pbar)->percentage != pct_progress)
	*/
472
	{
473
	  gtk_progress_bar_update (GTK_PROGRESS_BAR (pbar), pct_progress);
474
	}
475 476 477 478

      while (gtk_events_pending ())
	gtk_main_iteration ();

479 480 481
      /* We sync here to make sure things get drawn before continuing,
       * is the improved look worth the time? I'm not sure...
       */
482
      gdk_flush ();
483 484 485 486 487 488
    }
}

/* #define RESET_BAR() app_init_update_status("", "", 0) */
#define RESET_BAR()

Elliot Lee's avatar
Elliot Lee committed
489
void
490
app_init (void)
Elliot Lee's avatar
Elliot Lee committed
491
{
Tor Lillqvist's avatar
Tor Lillqvist committed
492 493
  gchar *filename;
  gchar *path;
Elliot Lee's avatar
Elliot Lee committed
494

495
  /*  parse the systemwide gtkrc  */
Tor Lillqvist's avatar
Tor Lillqvist committed
496
  filename = gimp_gtkrc ();
Manish Singh's avatar
Manish Singh committed
497

498
  if (be_verbose)
Tor Lillqvist's avatar
Tor Lillqvist committed
499
    g_print (_("parsing \"%s\"\n"), filename);
Manish Singh's avatar
Manish Singh committed
500

Tor Lillqvist's avatar
Tor Lillqvist committed
501
  gtk_rc_parse (filename);
Manish Singh's avatar
Manish Singh committed
502

503 504 505 506 507 508 509 510
  /*  parse the user gtkrc  */
  filename = gimp_personal_rc_file ("gtkrc");

  if (be_verbose)
    g_print (_("parsing \"%s\"\n"), filename);

  gtk_rc_parse (filename);

511 512
  g_free (filename);

513 514 515 516 517 518
  if (parse_buffers_init ())
    {
      parse_unitrc ();   /*  this needs to be done before gimprc loading  */
      parse_gimprc ();   /*  parse the local GIMP configuration file      */
    }

519
  if (!no_interface)
520
    get_standard_colormaps ();
521

522
  make_initialization_status_window ();
523

524
  if (!no_interface && !no_splash && win_initstatus)
525
    splash_text_draw (logo_area);
526

527 528 529
  /*  Initialize the context system before loading any data  */
  context_manager_init ();

530
  /*  Initialize the procedural database
Elliot Lee's avatar
Elliot Lee committed
531 532 533 534
   *    We need to do this first because any of the init
   *    procedures might install or query it as needed.
   */
  procedural_db_init ();
535
  RESET_BAR();
Elliot Lee's avatar
Elliot Lee committed
536 537
  internal_procs_init ();

538
#ifdef DISPLAY_FILTERS
539
  color_display_init ();
540
#endif /* DISPLAY_FILTERS */
541

542
  RESET_BAR();
543 544
  if (always_restore_session)
    restore_session = TRUE;
545

546
  /* make sure the monitor resolution is valid */
547 548
  if (monitor_xres < GIMP_MIN_RESOLUTION ||
      monitor_yres < GIMP_MIN_RESOLUTION)
549
    {
550
      gdisplay_xserver_resolution (&monitor_xres, &monitor_yres);
551 552
      using_xserver_resolution = TRUE;
    }
553

554 555 556
  /* Now we are ready to draw the splash-screen-image to the start-up window */
  if (no_interface == FALSE)
    {
557 558 559 560 561 562
      if (no_splash_image == FALSE && show_logo &&
	  splash_logo_load (win_initstatus))
	{
	  show_logo = SHOW_NOW;
	  splash_logo_draw (logo_area);
        }
563 564
    }

565
  RESET_BAR();
Elliot Lee's avatar
Elliot Lee committed
566
  file_ops_pre_init ();    /*  pre-initialize the file types  */
567
  RESET_BAR();
Elliot Lee's avatar
Elliot Lee committed
568
  xcf_init ();             /*  initialize the xcf file format routines */
569

570
  /*  initialize  the global parasite table  */
571
  app_init_update_status (_("Looking for data files"), _("Parasites"), 0.00);
572 573 574
  gimp_init_parasites ();

  /*  initialize the list of gimp brushes    */
575
  app_init_update_status (NULL, _("Brushes"), 0.20);
576 577 578
  gimp_data_factory_data_init (global_brush_factory, no_data); 

  /*  initialize the list of gimp patterns   */
579
  app_init_update_status (NULL, _("Patterns"), 0.40);
580 581 582
  gimp_data_factory_data_init (global_pattern_factory, no_data); 

  /*  initialize the list of gimp palettes   */
583
  app_init_update_status (NULL, _("Palettes"), 0.60);
584 585 586
  gimp_data_factory_data_init (global_palette_factory, no_data); 

  /*  initialize the list of gimp gradients  */
587
  app_init_update_status (NULL, _("Gradients"), 0.80);
588 589
  gimp_data_factory_data_init (global_gradient_factory, no_data); 

590
  app_init_update_status (NULL, NULL, 1.00);
591

592 593
  plug_in_init ();           /*  initialize the plug in structures  */
  module_db_init ();         /*  load any modules we need           */
594
  RESET_BAR();
595 596 597
  file_ops_post_init ();     /*  post-initialize the file types     */

  menus_reorder_plugins ();  /*  beautify some menus                */
Elliot Lee's avatar
Elliot Lee committed
598 599 600

  /* Add the swap file  */
  if (swap_path == NULL)
Tor Lillqvist's avatar
Tor Lillqvist committed
601 602
    swap_path = g_get_tmp_dir ();

603
  toast_old_temp_files ();
Tor Lillqvist's avatar
Tor Lillqvist committed
604 605
  path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "gimpswap.%lu",
			  swap_path, (unsigned long) getpid ());
Elliot Lee's avatar
Elliot Lee committed
606 607 608
  tile_swap_add (path, NULL, NULL);
  g_free (path);

609
  destroy_initialization_status_window ();
610

Elliot Lee's avatar
Elliot Lee committed
611 612 613
  /*  Things to do only if there is an interface  */
  if (no_interface == FALSE)
    {
614 615 616
      gximage_init ();
      render_setup (transparency_type, transparency_size);

617
      devices_init ();
618
      session_init ();
619 620

      toolbox_create ();
621 622 623 624 625

      /*  Fill the "last opened" menu items with the first last_opened_size
       *  elements of the docindex
       */
      {
626
	FILE   *fp;
627
	gchar **filenames = g_new (gchar *, last_opened_size);
628
	gint    i;
629

630
	if ((fp = document_index_parse_init ()))
631 632 633
	  {
	    /*  read the filenames...  */
	    for (i = 0; i < last_opened_size; i++)
634
	      if ((filenames[i] = document_index_parse_line (fp)) == NULL)
635
		break;
636

637 638 639 640 641 642 643 644 645
	    /*  ...and add them in reverse order  */
	    for (--i; i >= 0; i--)
	      {
		menus_last_opened_add (filenames[i]);
		g_free (filenames[i]);
	      }

	    fclose (fp);
	  }
Tor Lillqvist's avatar
Tor Lillqvist committed
646
	g_free (filenames);
647 648
      }

649
      tool_options_dialog_new ();
650 651 652 653

      /*  EEK: force signal emission  */
      if (gimp_context_get_tool (gimp_context_get_user ()) == RECT_SELECT)
	{
654
	  gimp_context_tool_changed (gimp_context_get_user ());
655 656 657 658 659 660 661
	}
      else
	{
	  gimp_context_set_tool (gimp_context_get_user (), RECT_SELECT);
	}

      /*  FIXME: This needs to go in preferences  */
662
      message_handler = MESSAGE_BOX;
Elliot Lee's avatar
Elliot Lee committed
663 664 665 666
    }

  color_transfer_init ();
  paint_funcs_setup ();
667

668 669 670
  /* register internal color selectors */
  color_select_init ();

671
  if (no_interface == FALSE)
Sven Neumann's avatar
Sven Neumann committed
672
    {
673
      devices_restore ();
674
      session_restore ();
Sven Neumann's avatar
Sven Neumann committed
675
    }
Elliot Lee's avatar
Elliot Lee committed
676 677
}

Elliot Lee's avatar
Elliot Lee committed
678
int
Tim Janik's avatar
Tim Janik committed
679 680 681 682 683
app_exit_finish_done (void)
{
  return is_app_exit_finish_done;
}

684
void
685
app_exit_finish (void)
Elliot Lee's avatar
Elliot Lee committed
686
{
Tim Janik's avatar
Tim Janik committed
687 688 689
  if (app_exit_finish_done ())
    return;
  is_app_exit_finish_done = TRUE;
690

691
  message_handler = CONSOLE;
692
  we_are_exiting = TRUE;
Manish Singh's avatar
Manish Singh committed
693

694 695 696 697
  /*  do this here before brushes and patterns are freed  */
  if (!no_interface)
    device_status_free ();

Marc Lehmann's avatar
Marc Lehmann committed
698
  module_db_free ();
Elliot Lee's avatar
Elliot Lee committed
699 700 701 702 703
  lc_dialog_free ();
  gdisplays_delete ();
  global_edit_free ();
  named_buffers_free ();
  swapping_free ();
704 705 706
  brush_dialog_free ();
  pattern_dialog_free ();
  palette_dialog_free ();
Michael Natterer's avatar
Michael Natterer committed
707
  gradient_dialog_free ();
708
  context_manager_free ();
Elliot Lee's avatar
Elliot Lee committed
709 710 711 712 713
  hue_saturation_free ();
  curves_free ();
  levels_free ();
  paint_funcs_free ();
  plug_in_kill ();
714
  procedural_db_free ();
715
  error_console_free ();
Elliot Lee's avatar
Elliot Lee committed
716 717
  menus_quit ();
  tile_swap_exit ();
718
  save_unitrc ();
719
  gimp_parasiterc_save ();
Elliot Lee's avatar
Elliot Lee committed
720 721

  /*  Things to do only if there is an interface  */
722
  if (!no_interface)
Elliot Lee's avatar
Elliot Lee committed
723
    {
724
      toolbox_free ();
725
      document_index_free ();
Elliot Lee's avatar
Elliot Lee committed
726 727
      gximage_free ();
      render_free ();
728
      tool_options_dialog_free ();
729
      save_sessionrc ();
Elliot Lee's avatar
Elliot Lee committed
730
    }
731

732 733 734 735
  /*  There used to be gtk_main_quit() here, but there's a chance 
   *  that gtk_main() was never called before we reach this point. --Sven  
   */
  gtk_exit (0);   
Elliot Lee's avatar
Elliot Lee committed
736 737 738
}

void
739
app_exit (gboolean kill_it)
Elliot Lee's avatar
Elliot Lee committed
740 741
{
  /*  If it's the user's perogative, and there are dirty images  */
742
  if (!kill_it && gdisplays_dirty () && !no_interface)
Elliot Lee's avatar
Elliot Lee committed
743
    really_quit_dialog ();
Tim Janik's avatar
Tim Janik committed
744 745
  else
    app_exit_finish ();
Elliot Lee's avatar
Elliot Lee committed
746 747
}

748 749 750
/*************************************************
 *   Routines to query exiting the application   *
 *************************************************/
Elliot Lee's avatar
Elliot Lee committed
751 752

static void
Michael Natterer's avatar
Michael Natterer committed
753
really_quit_callback (GtkWidget *button,
754 755
		      gboolean   quit,
		      gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
756
{
757 758 759 760 761 762 763 764 765
  if (quit)
    {
      app_exit_finish ();
    }
  else
    {
      menus_set_sensitive ("<Toolbox>/File/Quit", TRUE);
      menus_set_sensitive ("<Image>/File/Quit", TRUE);
    }
Elliot Lee's avatar
Elliot Lee committed
766 767 768
}

static void
769
really_quit_dialog (void)
Elliot Lee's avatar
Elliot Lee committed
770 771 772
{
  GtkWidget *dialog;

773 774
  menus_set_sensitive ("<Toolbox>/File/Quit", FALSE);
  menus_set_sensitive ("<Image>/File/Quit", FALSE);
Elliot Lee's avatar
Elliot Lee committed
775

776 777 778 779 780 781 782 783 784
  dialog = gimp_query_boolean_box (_("Really Quit?"),
				   gimp_standard_help_func,
				   "dialogs/really_quit.html",
				   TRUE,
				   _("Some files unsaved.\n\nQuit the GIMP?"),
				   _("Quit"), _("Cancel"),
				   NULL, NULL,
				   really_quit_callback,
				   NULL);
Elliot Lee's avatar
Elliot Lee committed
785 786 787 788

  gtk_widget_show (dialog);
}

789 790 791 792 793 794
static void
toast_old_temp_files (void)
{
  DIR *dir;
  struct dirent *entry;
  GString *filename = g_string_new ("");
795

796
  dir = opendir (swap_path);
797

798 799
  if (!dir)
    return;
800

801 802 803 804 805 806 807 808 809 810 811
  while ((entry = readdir (dir)) != NULL)
    if (!strncmp (entry->d_name, "gimpswap.", 9))
      {
        /* don't try to kill swap files of running processes
         * yes, I know they might not all be gimp processes, and when you
         * unlink, it's refcounted, but lets not confuse the user by
         * "where did my disk space go?" cause the filename is gone
         * if the kill succeeds, and there running process isn't gimp
         * we'll probably get it the next time around
         */

812
	gint pid = atoi (entry->d_name + 9);
813
#ifndef G_OS_WIN32
814
	if (kill (pid, 0))
815
#endif
816
	  {
817 818 819
	    /*  On Windows, you can't remove open files anyhow,
	     *  so no harm trying.
	     */
820 821
	    g_string_sprintf (filename, "%s" G_DIR_SEPARATOR_S "%s",
			      swap_path, entry->d_name);
822 823 824
	    unlink (filename->str);
	  }
      }
825

826 827 828 829
  closedir (dir);
  
  g_string_free (filename, TRUE);
}