session.c 6.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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.
 */

19
/* Session-managment stuff   Copyright (C) 1998 Sven Neumann <sven@gimp.org>
20 21

   I include a short description here on what is done and what problems 
22
   are left:
23

24 25 26 27
   Since everything saved in sessionrc changes often (with each session?) 
   the whole file is rewritten each time the gimp exits. I don't see any
   use in implementing a more flexible scheme like it is used for gimprc.

28 29 30
   Right now session-managment is limited to window geometry. Restoring 
   openend images is planned, but I'm still not sure how to deal with dirty
   images.
31

32 33 34 35 36 37
   Dialogs are now reopened if the gimp is called with the command-line-option
   --restore-session or if the related entry is set in gimprc.
   Probably there should alternatively be a list of dialogs in the preferences 
   that should always be opened on start-up. 

   Please point me into the right direction to make this work with Gnome-SM.
38 39
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
40 41
#include "config.h"

42 43
#include <stdlib.h>
#include <stdio.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
44
#ifdef HAVE_UNISTD_H
45
#include <unistd.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
46
#endif
47

48 49
#include <gtk/gtk.h>

50
#include "app_procs.h"
51
#include "appenv.h"
52
#include "commands.h"
53 54 55
#include "gimprc.h"
#include "session.h"

Tor Lillqvist's avatar
Tor Lillqvist committed
56
#include "libgimp/gimpenv.h"
57

58 59
static void sessionrc_write_info     (SessionInfo *, FILE *);
static void session_open_dialog      (SessionInfo *);
60
static void session_reset_open_state (SessionInfo *);
61

62
GList *session_info_updates = NULL;
63 64

/* global session variables */
65 66 67
SessionInfo toolbox_session_info = 
  { "toolbox", NULL, 0, 0, 0, 0, FALSE };
SessionInfo lc_dialog_session_info = 
68
  { "lc-dialog", (GtkItemFactoryCallback)dialogs_lc_cmd_callback, 0, 400, 0, 0, FALSE };
69 70 71
SessionInfo info_dialog_session_info = 
  { "info-dialog", NULL, 165, 0, 0, 0, FALSE };
SessionInfo tool_options_session_info = 
72
  { "tool-options", (GtkItemFactoryCallback)dialogs_tool_options_cmd_callback, 0, 345, 0, 0, FALSE };
73
SessionInfo palette_session_info = 
74
  { "palette", (GtkItemFactoryCallback)dialogs_palette_cmd_callback, 140, 180, 0, 0, FALSE };
75
SessionInfo brush_select_session_info = 
76
  { "brush-select",  (GtkItemFactoryCallback)dialogs_brushes_cmd_callback, 150, 180, 0, 0, FALSE };
77
SessionInfo pattern_select_session_info = 
78
  { "pattern-select", (GtkItemFactoryCallback)dialogs_patterns_cmd_callback, 160, 180, 0, 0, FALSE };
79
SessionInfo gradient_select_session_info = 
80
  { "gradient-editor", (GtkItemFactoryCallback)dialogs_gradients_cmd_callback, 170, 180, 0, 0, FALSE };
81 82
SessionInfo device_status_session_info = 
  { "device-status", (GtkItemFactoryCallback)dialogs_device_status_cmd_callback, 0, 600, 0, 0, FALSE };
83 84
SessionInfo error_console_session_info = 
  { "error-console", (GtkItemFactoryCallback)dialogs_error_console_cmd_callback, 400, 0, 250, 300, FALSE };
85 86 87

/* public functions */
void 
88 89
session_get_window_info (GtkWidget   *window, 
			 SessionInfo *info)
90
{
91
  if ( !save_session_info || info == NULL || window->window == NULL )
92 93
    return;

94
  gdk_window_get_root_origin (window->window, &info->x, &info->y);
95 96 97 98 99 100 101
  gdk_window_get_size (window->window, &info->width, &info->height);

  if ( we_are_exiting )
    info->open = GTK_WIDGET_VISIBLE (window);

  if ( g_list_find (session_info_updates, info) == NULL )
    session_info_updates = g_list_append (session_info_updates, info);
102 103 104
}

void 
105 106 107
session_set_window_geometry (GtkWidget   *window, 
			     SessionInfo *info,
			     int          set_size)
108
{
109
  if ( window == NULL || info == NULL)
110 111
    return;
  
112
#ifdef GDK_WINDOWING_WIN32
Tor Lillqvist's avatar
Tor Lillqvist committed
113 114 115 116
  /* We should not position windows so that no decoration is visible */
  if (info->y > 0)
    gtk_widget_set_uposition (window, info->x, info->y);
#else
117
  gtk_widget_set_uposition (window, info->x, info->y);
Tor Lillqvist's avatar
Tor Lillqvist committed
118
#endif
119
  
120
  if ( (set_size) && (info->width > 0) && (info->height > 0) )
121
    gtk_window_set_default_size (GTK_WINDOW(window), info->width, info->height);
122 123 124 125 126
}

void
save_sessionrc (void)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
127
  char *filename;
128 129
  FILE *fp;

Tor Lillqvist's avatar
Tor Lillqvist committed
130
  filename = gimp_personal_rc_file ("sessionrc");
131

132
  fp = fopen (filename, "wt");
Tor Lillqvist's avatar
Tor Lillqvist committed
133 134 135
  g_free (filename);
  if (!fp)
    return;
136

Sven Neumann's avatar
Sven Neumann committed
137 138 139 140 141 142 143
  fprintf(fp, "# GIMP sessionrc\n"
              "# This file takes session-specific info (that is info,\n"
              "# you want to keep between two gimp-sessions). You are\n"
              "# not supposed to edit it manually, but of course you\n"
              "# can do. This file will be entirely rewritten every time\n" 
              "# you quit the gimp. If this file isn't found, defaults\n"
              "# are used.\n\n");
Tor Lillqvist's avatar
Tor Lillqvist committed
144 145 146 147 148 149 150 151
  
  /* save window geometries */
  g_list_foreach (session_info_updates, (GFunc)sessionrc_write_info, fp);
  
  /* save last tip shown */
  fprintf(fp, "(last-tip-shown %d)\n\n", last_tip + 1);
  
  fclose (fp);
152 153 154 155 156
}

void
session_init (void)
{
157
  gchar *filename;
158

Tor Lillqvist's avatar
Tor Lillqvist committed
159
  filename = gimp_personal_rc_file ("sessionrc");
160 161 162 163 164 165 166 167 168 169 170 171 172 173
  app_init_update_status (NULL, filename, -1);

  /*  always show L&C and Brushes on first invocation  */
  if (! parse_gimprc_file (filename) && save_session_info)
    {
      lc_dialog_session_info.open = TRUE;
      session_info_updates =
	g_list_append (session_info_updates, &lc_dialog_session_info);

      brush_select_session_info.open = TRUE;
      session_info_updates =
	g_list_append (session_info_updates, &brush_select_session_info);
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
174
  g_free (filename);
175 176
}

177 178 179 180 181 182 183 184 185 186
void
session_restore (void)
{      
  /* open dialogs */
  if (restore_session)
    g_list_foreach (session_info_updates, (GFunc)session_open_dialog, NULL);

  /* reset the open state in the session_infos */
  g_list_foreach (session_info_updates, (GFunc)session_reset_open_state, NULL);
}
187 188 189

/* internal function */
static void
190
sessionrc_write_info (SessionInfo *info, FILE *fp)
191
{
192
  if (fp == NULL || info == NULL) 
193 194
    return;
  
195 196 197 198 199 200 201 202 203
  fprintf (fp,"(session-info \"%s\"\n", info->name);
  fprintf (fp,"   (position %d %d)\n", info->x, info->y);
  fprintf (fp,"   (size %d %d)", info->width, info->height);
  if ( info->open ) 
    fprintf (fp,"\n   (open-on-exit)");
  fprintf (fp,")\n\n"); 
}

static void
204
session_open_dialog (SessionInfo *info)
205
{
206
  if (info == NULL || info->open == FALSE || info->open_callback == NULL) 
207 208
    return;

209
  (info->open_callback) ();
210 211
}

212 213 214 215 216 217 218 219
static void
session_reset_open_state (SessionInfo *info)
{
  if (info == NULL ) 
    return;

  info->open = FALSE;
}