1 /* $XConsortium: session.c /main/3 1995/11/01 10:39:26 rswiston $ */
2 /**********************************<+>*************************************
3 ***************************************************************************
7 ** Project: DT dtpad, a memo maker type editor based on the motif
10 ** Description: Provides the functionality for saving and restoring the
14 *******************************************************************
15 ** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
17 ** reserved. Copying or other reproduction of this program
18 ** except for archival purposes is prohibited without prior
19 ** written consent of Hewlett-Packard Company.
20 ********************************************************************
22 ********************************************************************
23 ** (c) Copyright 1993, 1994 Hewlett-Packard Company
24 ** (c) Copyright 1993, 1994 International Business Machines Corp.
25 ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 ** (c) Copyright 1993, 1994 Novell, Inc.
27 ********************************************************************
30 **************************************************************************
31 **********************************<+>*************************************/
35 * This for the sleazy hack to get the window manager frame width/height
37 #include <Xm/VendorSEP.h>
39 /* Copied from BaseClassI.h */
40 extern XmWidgetExtData _XmGetWidgetExtData(
42 #if NeedWidePrototypes
43 unsigned int extType) ;
45 unsigned char extType) ;
46 #endif /* NeedWidePrototypes */
48 #define MSG1 ((char *)GETMESSAGE(10, 1, "Check the file permissions."))
49 #define MSG2 ((char *)GETMESSAGE(10, 2, "%s is the file that would have been used to save your session."))
51 extern Widget topLevelWithWmCommand; /* declared in main.c */
52 extern Editor *pPadList;
53 extern int numActivePads;
55 /* Structure used on a save session to see if a dt is iconic */
57 static Atom wm_state_atom;
65 /************************************************************************
67 * SaveMain - saves the parameters associated with a Pad's mainWindow
68 * (called from SaveSessionCB() below).
70 ***********************************************************************/
79 Dimension width, height;
81 unsigned long numInfo;
85 unsigned long leftover;
88 if(pPad->mainWindow != (Widget)NULL)
90 XmVendorShellExtObject vendorExt;
91 XmWidgetExtData extData;
93 if(XtIsRealized(pPad->mainWindow))
94 sprintf(bufr,"*mainWindow%d.ismapped: True\n", padNum);
96 /* Get and write out the geometry info for our Window */
97 x = XtX(XtParent(pPad->mainWindow));
98 y = XtY(XtParent(pPad->mainWindow));
101 * Modify x & y to take into account window mgr frames
102 * This is pretty bogus, but I don't know a better way to do it.
104 extData = _XmGetWidgetExtData(pPad->app_shell, XmSHELL_EXTENSION);
105 vendorExt = (XmVendorShellExtObject)extData->widget;
106 x -= vendorExt->vendor.xOffset;
107 y -= vendorExt->vendor.yOffset;
109 width = XtWidth(XtParent(pPad->mainWindow));
110 height = XtHeight(XtParent(pPad->mainWindow));
112 sprintf(bufr, "%s*mainWindow%d.x: %d\n", bufr, padNum, x);
113 sprintf(bufr, "%s*mainWindow%d.y: %d\n", bufr, padNum, y);
114 sprintf(bufr, "%s*mainWindow%d.width: %d\n", bufr, padNum, width);
115 sprintf(bufr, "%s*mainWindow%d.height: %d\n", bufr, padNum, height);
117 wm_state_atom = XmInternAtom (XtDisplay(pPad->app_shell), "WM_STATE",
119 /* Getting the WM_STATE property to see if iconified or not */
120 XGetWindowProperty (XtDisplay(pPad->app_shell),
121 XtWindow (pPad->app_shell),
122 wm_state_atom, 0L, (long) BUFSIZ, False,
123 wm_state_atom, &actual_type, &actual_format,
124 &nitems, &leftover, (unsigned char **) &wm_state);
126 /* Write out if iconified our not */
127 sprintf(bufr, "%s*mainWindow%d.iconify: ", bufr, padNum);
129 if (wm_state->state == IconicState)
130 sprintf (bufr, "%sTrue\n", bufr);
132 sprintf (bufr, "%sFalse\n", bufr);
134 if(DtWsmGetWorkspacesOccupied(XtDisplay(pPad->app_shell),
135 XtWindow(pPad->app_shell), &pWsPresence,
136 &numInfo) == Success)
139 sprintf(bufr, "%s*mainWindow%d.workspaceList: ", bufr, padNum);
140 for(i = 0; i < numInfo; i++)
142 char *name = XGetAtomName(XtDisplay(pPad->app_shell),
144 sprintf(bufr, "%s %s", bufr, name);
147 sprintf(bufr, "%s\n", bufr);
148 XtFree((char *)pWsPresence);
151 write (fd, bufr, strlen(bufr));
153 if(pPad->fileStuff.fileName != (char *)NULL)
155 sprintf(bufr, "*mainWindow%d.fileName: %s\n", padNum,
156 pPad->fileStuff.fileName);
157 write (fd, bufr, strlen(bufr));
162 /************************************************************************
164 * SaveSessionCB - saves the editor state (just filename) - does not save
165 * the file at this time.
167 * This routines is setup as the "WM_SAVE_YOURSELF" WMProtocolCallback
168 * on the top level widget (created via XtInitialize).
170 ************************************************************************/
174 Widget w, /* widget id */
175 caddr_t client_data, /* data from application */
176 caddr_t call_data ) /* data from widget class */
178 char *longpath, *fileName;
179 int fd, numPadsToSave;
180 char *xa_CommandStr[10];
181 char *tmpStr, bufr[1024];
185 /* Xt may not pass a widget as advertised (??? is this needed? - hp) */
189 for(pPad = pPadList, numPadsToSave = 0; pPad != (Editor *)NULL;
190 pPad = pPad->pNextPad)
192 if(pPad->inUse == True)
195 if(numPadsToSave < 1)
197 xa_CommandStr[0] = (char *)NULL;
198 XSetCommand(XtDisplay(w), XtWindow(w), xa_CommandStr, 1);
202 DtSessionSavePath(w, &longpath, &fileName);
204 /* Create the session file */
205 if ((fd = creat (longpath, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1)
207 tmpStr = (char *)malloc(strlen(MSG2) + strlen(longpath)+ 1);
208 sprintf(tmpStr, MSG2, longpath);
209 _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
211 XtFree ((char *)longpath);
215 sprintf(bufr, "*pads.numActivePads: %d\n", numPadsToSave);
216 write (fd, bufr, strlen(bufr));
218 for(pPad = pPadList, i = 0; pPad != (Editor *)NULL;
219 pPad = pPad->pNextPad, i++)
221 if(pPad->inUse == True)
222 SaveMain(pPad, i, fd);
228 xa_CommandStr[i] = pPadList->progname; i++;
229 xa_CommandStr[i] = "-session"; i++;
230 xa_CommandStr[i] = fileName; i++;
232 XSetCommand(XtDisplay(topLevelWithWmCommand),
233 XtWindow(topLevelWithWmCommand), xa_CommandStr, i);
235 XtFree ((char *)fileName);
239 /***********************************************************************
241 * closeCB - set up as the "WM_DELETE_WINDOW" WMProtocolCallback on the
242 * application shell for each Editor instance - and called when
243 * a delete window command is received from the Window Manager.
244 * Waits for CloseWindow to become false before it continues.
246 ***********************************************************************/
255 Editor *pPad = (Editor *)client_data;
257 /* call the callback for Exit within the File Menu pulldown */
258 XtCallCallbacks(pPad->ExitWid, XmNactivateCallback, (XtPointer)pPad);
262 /***********************************************************************
264 * restoreSession - gets the valid x and y location of where to put the
265 * Text Edit on the root window. Sets the global varible
266 * dtpad.saveRestore to True so the rest of the program knows that
267 * a session is being restored.
269 ***********************************************************************/
277 XrmRepresentation rep_type;
279 char *fileName = pPad->xrdb.session;
281 int numPadsToRestore, i;
284 if(DtSessionRestorePath(topLevelWithWmCommand, &path, fileName) == False)
287 /* Open the file as a resource database */
288 if ((db = XrmGetFileDatabase (path)) == NULL)
290 tmpStr = (char *)XtMalloc(strlen(MSG2) + strlen(path)+ 1);
291 sprintf(tmpStr, MSG2, path);
292 _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
297 xrm_name[0] = XrmStringToQuark ("pads");
298 xrm_name[1] = XrmStringToQuark ("numActivePads");
300 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
301 numPadsToRestore = atoi((char *)value.addr);
303 if(numPadsToRestore == 0)
306 * Either it's an old (i.e. 2.01) session file,
307 * or it's bogus. Either way, we'll create one
308 * window, taking whatever mainWindow: resources
311 xrm_name[0] = XrmStringToQuark ("mainWindow");
315 xrm_name[1] = XrmStringToQuark ("x");
316 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
317 pPad->x = atoi((char *)value.addr);
320 xrm_name[1] = XrmStringToQuark ("y");
321 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
322 pPad->y = atoi((char *)value.addr);
324 pPad->saveRestore = True;
329 RestorePad(pPad, 0, db);
331 for(i = 1; i < numPadsToRestore; i++)
333 foundPad = FindOrCreatePad(&pPad);
334 RestorePad(pPad, i, db);
336 if(foundPad == False)
339 ManageOldPad(pPad, False);
344 /************************************************************************
348 ***********************************************************************/
355 char * iconify = NULL;
358 XrmRepresentation rep_type;
361 sprintf(buf, "mainWindow%d", padNum);
362 xrm_name[0] = XrmStringToQuark(buf);
366 xrm_name[1] = XrmStringToQuark ("x");
367 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
368 pPad->x = atoi((char *)value.addr);
371 xrm_name [1] = XrmStringToQuark ("y");
372 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
373 pPad->y = atoi((char *)value.addr);
376 xrm_name [1] = XrmStringToQuark ("width");
377 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
378 pPad->width = atoi((char *)value.addr);
381 xrm_name [1] = XrmStringToQuark ("height");
382 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
383 pPad->height = atoi((char *)value.addr);
385 /* Get and set whether the view is iconic */
386 xrm_name [1] = XrmStringToQuark ("iconify");
387 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
388 /* If there is an iconify resource and its value is True, */
389 /* then mark the window as iconified. */
390 if ((iconify = (char *) value.addr) != NULL &&
391 strcmp (iconify, "True") == 0)
394 pPad->iconic = False;
396 /* get the file name */
397 xrm_name [1] = XrmStringToQuark ("fileName");
398 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
399 pPad->fileStuff.fileName = strdup((char *) value.addr);
401 /* get workspace list */
402 xrm_name[1] = XrmStringToQuark("workspaceList");
403 XrmQGetResource(db, xrm_name, xrm_name, &rep_type, &value);
404 pPad->xrdb.workspaceList = strdup((char *) value.addr);
409 /************************************************************************
413 ***********************************************************************/
420 RestoreMain(pPad, padNum, db);
421 pPad->saveRestore = True;