home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / session.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  8.4 KB  |  335 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  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.    Since everything saved in sessionrc changes often (with each session?) 
  25.    the whole file is rewritten each time the gimp exits. I don't see any
  26.    use in implementing a more flexible scheme like it is used for gimprc.
  27.  
  28.    Right now session-managment is limited to window geometry. Restoring 
  29.    openend images is planned, but I'm still not sure how to deal with dirty
  30.    images.
  31.  
  32.    Dialogs are now reopened if the gimp is called with the command-line-option
  33.    --restore-session or if the related entry is set in gimprc.
  34.    Probably there should alternatively be a list of dialogs in the preferences 
  35.    that should always be opened on start-up. 
  36.  
  37.    Please point me into the right direction to make this work with Gnome-SM.
  38.  */
  39.  
  40. #include "config.h"
  41.  
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47.  
  48. #include <gtk/gtk.h>
  49.  
  50. #include "apptypes.h"
  51.  
  52. #include "app_procs.h"
  53. #include "appenv.h"
  54. #include "commands.h"
  55. #include "gimprc.h"
  56. #include "session.h"
  57.  
  58. #include "libgimp/gimpenv.h"
  59.  
  60. static void sessionrc_write_info     (SessionInfo *info,
  61.                       FILE        *fp);
  62. static void session_open_dialog      (SessionInfo *info);
  63. static void session_reset_open_state (SessionInfo *info);
  64.  
  65. GList *session_info_updates = NULL;
  66.  
  67. #define LEFT_OFFSET 60
  68. #define TOP_OFFSET  60
  69.  
  70. /* global session variables */
  71. SessionInfo toolbox_session_info =
  72. {
  73.   "toolbox",
  74.   NULL,
  75.   LEFT_OFFSET, TOP_OFFSET,
  76.   0, 0,
  77.   0, FALSE
  78. };
  79.  
  80. SessionInfo lc_dialog_session_info =
  81. {
  82.   "lc-dialog",
  83.   (GtkItemFactoryCallback) dialogs_lc_cmd_callback,
  84.   LEFT_OFFSET, TOP_OFFSET + 300,
  85.   0, 0,
  86.   0, FALSE
  87. };
  88.  
  89. SessionInfo tool_options_session_info =
  90. {
  91.   "tool-options",
  92.   (GtkItemFactoryCallback) dialogs_tool_options_cmd_callback,
  93.   LEFT_OFFSET + 150, TOP_OFFSET,
  94.   0, 0,
  95.   0, FALSE
  96. };
  97.  
  98. SessionInfo device_status_session_info =
  99. {
  100.   "device-status",
  101.   (GtkItemFactoryCallback) dialogs_device_status_cmd_callback,
  102.   LEFT_OFFSET + 150, TOP_OFFSET + 250,
  103.   0, 0,
  104.   0, FALSE
  105. };
  106.  
  107. SessionInfo brush_select_session_info =
  108. {
  109.   "brush-select",
  110.   (GtkItemFactoryCallback) dialogs_brushes_cmd_callback,
  111.   LEFT_OFFSET + 350, TOP_OFFSET,
  112.   0, 0,
  113.   0, FALSE
  114. };
  115.  
  116. SessionInfo pattern_select_session_info =
  117. {
  118.   "pattern-select",
  119.   (GtkItemFactoryCallback) dialogs_patterns_cmd_callback,
  120.   LEFT_OFFSET + 400, TOP_OFFSET + 50,
  121.   0, 0,
  122.   0, FALSE
  123. };
  124.  
  125. SessionInfo gradient_select_session_info =
  126. {
  127.   "gradient-select",
  128.   (GtkItemFactoryCallback) dialogs_gradients_cmd_callback,
  129.   LEFT_OFFSET + 450, TOP_OFFSET + 100,
  130.   0, 0,
  131.   0, FALSE
  132. };
  133.  
  134. SessionInfo palette_session_info =
  135. {
  136.   "palette",
  137.   (GtkItemFactoryCallback) dialogs_palette_cmd_callback,
  138.   LEFT_OFFSET + 500, TOP_OFFSET + 150,
  139.   0, 0,
  140.   0, FALSE
  141. };
  142.  
  143. SessionInfo info_dialog_session_info =
  144. {
  145.   "info-dialog",
  146.   NULL,
  147.   LEFT_OFFSET + 350, TOP_OFFSET + 250,
  148.   0, 0,
  149.   0, FALSE
  150. };
  151.  
  152. SessionInfo error_console_session_info =
  153. {
  154.   "error-console",
  155.   (GtkItemFactoryCallback) dialogs_error_console_cmd_callback,
  156.   LEFT_OFFSET, TOP_OFFSET + 250,
  157.   400, 150,
  158.   0, FALSE
  159. };
  160.  
  161. SessionInfo document_index_session_info =
  162. {
  163.   "document-index",
  164.   (GtkItemFactoryCallback) dialogs_document_index_cmd_callback,
  165.   LEFT_OFFSET, TOP_OFFSET + 300,
  166.   400, 150,
  167.   0, FALSE
  168. };
  169.  
  170.  
  171. /* public functions */
  172. void 
  173. session_get_window_info (GtkWidget   *window, 
  174.              SessionInfo *info)
  175. {
  176.   if (info == NULL || window == NULL || window->window == NULL)
  177.     return;
  178.  
  179.   gdk_window_get_root_origin (window->window, &info->x, &info->y);
  180.   gdk_window_get_size (window->window, &info->width, &info->height);
  181.  
  182.   if (we_are_exiting)
  183.     info->open = GTK_WIDGET_VISIBLE (window);
  184.  
  185.   /*  this place is free for a new window, reset the counter  */
  186.   info->count = 0;
  187.  
  188.   if (save_session_info && 
  189.       g_list_find (session_info_updates, info) == NULL)
  190.     {
  191.       session_info_updates = g_list_append (session_info_updates, info);
  192.     }
  193. }
  194.  
  195. void 
  196. session_set_window_geometry (GtkWidget   *window, 
  197.                  SessionInfo *info,
  198.                  gboolean     set_size)
  199. {
  200.   static gint screen_width = 0;
  201.   static gint screen_height = 0;
  202.   gint x;
  203.   gint y;
  204.   
  205.   g_return_if_fail (window != NULL && info != NULL);
  206.   
  207.   if (screen_width == 0 || screen_height == 0)
  208.     {
  209.       screen_width  = gdk_screen_width ();
  210.       screen_height = gdk_screen_height ();
  211.     }
  212.  
  213.   /*  cascade multiple windows of same type (e.g. info_dialogs)  */ 
  214.   x = info->x + info->count * 32;
  215.   y = info->y + info->count * 32;
  216.   info->count++;
  217.  
  218.   if (set_size)
  219.     {
  220.       if (x >= 0 && x + info->width < screen_width &&
  221.       y >= 0 && y + info->height < screen_height)
  222.     {
  223.       gtk_widget_set_uposition (window, x, y);
  224.       gtk_window_set_default_size (GTK_WINDOW(window), info->width, info->height);
  225.     }
  226.     }
  227.   else
  228.     {
  229.       if (x >= 0 && x + 32 < screen_width && 
  230.       y >= 0 && y + 32 < screen_height)
  231.     gtk_widget_set_uposition (window, x, y);
  232.     }
  233. }
  234.  
  235. void
  236. save_sessionrc (void)
  237. {
  238.   gchar *filename;
  239.   FILE  *fp;
  240.  
  241.   filename = gimp_personal_rc_file ("sessionrc");
  242.  
  243.   fp = fopen (filename, "wt");
  244.   g_free (filename);
  245.   if (!fp)
  246.     return;
  247.  
  248.   fprintf (fp, ("# GIMP sessionrc\n"
  249.         "# This file takes session-specific info (that is info,\n"
  250.         "# you want to keep between two gimp-sessions). You are\n"
  251.         "# not supposed to edit it manually, but of course you\n"
  252.         "# can do. This file will be entirely rewritten every time\n" 
  253.         "# you quit the gimp. If this file isn't found, defaults\n"
  254.         "# are used.\n\n"));
  255.  
  256.   /* save window geometries */
  257.   g_list_foreach (session_info_updates, (GFunc) sessionrc_write_info, fp);
  258.  
  259.   /* save last tip shown */
  260.   fprintf(fp, "(last-tip-shown %d)\n\n", last_tip + 1);
  261.  
  262.   fclose (fp);
  263. }
  264.  
  265. void
  266. session_init (void)
  267. {
  268.   gchar *filename;
  269.  
  270.   filename = gimp_personal_rc_file ("sessionrc");
  271.   app_init_update_status (NULL, filename, -1);
  272.  
  273.   /*  always show L&C&P, Tool Options and Brushes on first invocation  */
  274.   if (! parse_gimprc_file (filename))
  275.     {
  276.       lc_dialog_session_info.open = TRUE;
  277.       session_info_updates =
  278.     g_list_append (session_info_updates, &lc_dialog_session_info);
  279.  
  280.       tool_options_session_info.open = TRUE;
  281.       session_info_updates =
  282.     g_list_append (session_info_updates, &tool_options_session_info);
  283.  
  284.       brush_select_session_info.open = TRUE;
  285.       session_info_updates =
  286.     g_list_append (session_info_updates, &brush_select_session_info);
  287.     }
  288.  
  289.   g_free (filename);
  290. }
  291.  
  292. void
  293. session_restore (void)
  294. {      
  295.   /* open dialogs */
  296.   if (restore_session)
  297.     g_list_foreach (session_info_updates, (GFunc)session_open_dialog, NULL);
  298.  
  299.   /* reset the open state in the session_infos */
  300.   g_list_foreach (session_info_updates, (GFunc)session_reset_open_state, NULL);
  301. }
  302.  
  303. /* internal function */
  304. static void
  305. sessionrc_write_info (SessionInfo *info, FILE *fp)
  306. {
  307.   if (fp == NULL || info == NULL) 
  308.     return;
  309.   
  310.   fprintf (fp,"(session-info \"%s\"\n", info->name);
  311.   fprintf (fp,"   (position %d %d)\n", info->x, info->y);
  312.   fprintf (fp,"   (size %d %d)", info->width, info->height);
  313.   if ( info->open ) 
  314.     fprintf (fp,"\n   (open-on-exit)");
  315.   fprintf (fp,")\n\n"); 
  316. }
  317.  
  318. static void
  319. session_open_dialog (SessionInfo *info)
  320. {
  321.   if (info == NULL || info->open == FALSE || info->open_callback == NULL) 
  322.     return;
  323.  
  324.   (info->open_callback) ();
  325. }
  326.  
  327. static void
  328. session_reset_open_state (SessionInfo *info)
  329. {
  330.   if (info == NULL)
  331.     return;
  332.  
  333.   info->open = FALSE;
  334. }
  335.