2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: session.c /main/3 1995/11/01 10:39:26 rswiston $ */
24 /**********************************<+>*************************************
25 ***************************************************************************
29 ** Project: DT dtpad, a memo maker type editor based on the motif
32 ** Description: Provides the functionality for saving and restoring the
36 *******************************************************************
37 ** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
39 ** reserved. Copying or other reproduction of this program
40 ** except for archival purposes is prohibited without prior
41 ** written consent of Hewlett-Packard Company.
42 ********************************************************************
44 ********************************************************************
45 ** (c) Copyright 1993, 1994 Hewlett-Packard Company
46 ** (c) Copyright 1993, 1994 International Business Machines Corp.
47 ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
48 ** (c) Copyright 1993, 1994 Novell, Inc.
49 ********************************************************************
52 **************************************************************************
53 **********************************<+>*************************************/
57 * This for the sleazy hack to get the window manager frame width/height
59 #include <Xm/VendorSEP.h>
61 /* Copied from BaseClassI.h */
62 extern XmWidgetExtData _XmGetWidgetExtData(
64 #if NeedWidePrototypes
65 unsigned int extType) ;
67 unsigned char extType) ;
68 #endif /* NeedWidePrototypes */
70 #define MSG1 ((char *)GETMESSAGE(10, 1, "Check the file permissions."))
71 #define MSG2 ((char *)GETMESSAGE(10, 2, "%s is the file that would have been used to save your session."))
73 extern Widget topLevelWithWmCommand; /* declared in main.c */
74 extern Editor *pPadList;
75 extern int numActivePads;
77 /* Structure used on a save session to see if a dt is iconic */
79 static Atom wm_state_atom;
87 /************************************************************************
89 * SaveMain - saves the parameters associated with a Pad's mainWindow
90 * (called from SaveSessionCB() below).
92 ***********************************************************************/
101 Dimension width, height;
103 unsigned long numInfo;
106 unsigned long nitems;
107 unsigned long leftover;
110 if(pPad->mainWindow != (Widget)NULL)
112 XmVendorShellExtObject vendorExt;
113 XmWidgetExtData extData;
115 if(XtIsRealized(pPad->mainWindow))
116 sprintf(bufr,"*mainWindow%d.ismapped: True\n", padNum);
118 /* Get and write out the geometry info for our Window */
119 x = XtX(XtParent(pPad->mainWindow));
120 y = XtY(XtParent(pPad->mainWindow));
123 * Modify x & y to take into account window mgr frames
124 * This is pretty bogus, but I don't know a better way to do it.
126 extData = _XmGetWidgetExtData(pPad->app_shell, XmSHELL_EXTENSION);
127 vendorExt = (XmVendorShellExtObject)extData->widget;
128 x -= vendorExt->vendor.xOffset;
129 y -= vendorExt->vendor.yOffset;
131 width = XtWidth(XtParent(pPad->mainWindow));
132 height = XtHeight(XtParent(pPad->mainWindow));
134 sprintf(bufr, "%s*mainWindow%d.x: %d\n", bufr, padNum, x);
135 sprintf(bufr, "%s*mainWindow%d.y: %d\n", bufr, padNum, y);
136 sprintf(bufr, "%s*mainWindow%d.width: %d\n", bufr, padNum, width);
137 sprintf(bufr, "%s*mainWindow%d.height: %d\n", bufr, padNum, height);
139 wm_state_atom = XmInternAtom (XtDisplay(pPad->app_shell), "WM_STATE",
141 /* Getting the WM_STATE property to see if iconified or not */
142 XGetWindowProperty (XtDisplay(pPad->app_shell),
143 XtWindow (pPad->app_shell),
144 wm_state_atom, 0L, (long) BUFSIZ, False,
145 wm_state_atom, &actual_type, &actual_format,
146 &nitems, &leftover, (unsigned char **) &wm_state);
148 /* Write out if iconified our not */
149 sprintf(bufr, "%s*mainWindow%d.iconify: ", bufr, padNum);
151 if (wm_state->state == IconicState)
152 sprintf (bufr, "%sTrue\n", bufr);
154 sprintf (bufr, "%sFalse\n", bufr);
156 if(DtWsmGetWorkspacesOccupied(XtDisplay(pPad->app_shell),
157 XtWindow(pPad->app_shell), &pWsPresence,
158 &numInfo) == Success)
161 sprintf(bufr, "%s*mainWindow%d.workspaceList: ", bufr, padNum);
162 for(i = 0; i < numInfo; i++)
164 char *name = XGetAtomName(XtDisplay(pPad->app_shell),
166 sprintf(bufr, "%s %s", bufr, name);
169 sprintf(bufr, "%s\n", bufr);
170 XtFree((char *)pWsPresence);
173 write (fd, bufr, strlen(bufr));
175 if(pPad->fileStuff.fileName != (char *)NULL)
177 sprintf(bufr, "*mainWindow%d.fileName: %s\n", padNum,
178 pPad->fileStuff.fileName);
179 write (fd, bufr, strlen(bufr));
184 /************************************************************************
186 * SaveSessionCB - saves the editor state (just filename) - does not save
187 * the file at this time.
189 * This routines is setup as the "WM_SAVE_YOURSELF" WMProtocolCallback
190 * on the top level widget (created via XtInitialize).
192 ************************************************************************/
196 Widget w, /* widget id */
197 caddr_t client_data, /* data from application */
198 caddr_t call_data ) /* data from widget class */
200 char *longpath, *fileName;
201 int fd, numPadsToSave;
202 char *xa_CommandStr[10];
203 char *tmpStr, bufr[1024];
207 /* Xt may not pass a widget as advertised (??? is this needed? - hp) */
211 for(pPad = pPadList, numPadsToSave = 0; pPad != (Editor *)NULL;
212 pPad = pPad->pNextPad)
214 if(pPad->inUse == True)
217 if(numPadsToSave < 1)
219 xa_CommandStr[0] = (char *)NULL;
220 XSetCommand(XtDisplay(w), XtWindow(w), xa_CommandStr, 1);
224 DtSessionSavePath(w, &longpath, &fileName);
226 /* Create the session file */
227 if ((fd = creat (longpath, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1)
229 tmpStr = (char *)malloc(strlen(MSG2) + strlen(longpath)+ 1);
230 sprintf(tmpStr, MSG2, longpath);
231 _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
233 XtFree ((char *)longpath);
237 sprintf(bufr, "*pads.numActivePads: %d\n", numPadsToSave);
238 write (fd, bufr, strlen(bufr));
240 for(pPad = pPadList, i = 0; pPad != (Editor *)NULL;
241 pPad = pPad->pNextPad, i++)
243 if(pPad->inUse == True)
244 SaveMain(pPad, i, fd);
250 xa_CommandStr[i] = pPadList->progname; i++;
251 xa_CommandStr[i] = "-session"; i++;
252 xa_CommandStr[i] = fileName; i++;
254 XSetCommand(XtDisplay(topLevelWithWmCommand),
255 XtWindow(topLevelWithWmCommand), xa_CommandStr, i);
257 XtFree ((char *)fileName);
261 /***********************************************************************
263 * closeCB - set up as the "WM_DELETE_WINDOW" WMProtocolCallback on the
264 * application shell for each Editor instance - and called when
265 * a delete window command is received from the Window Manager.
266 * Waits for CloseWindow to become false before it continues.
268 ***********************************************************************/
277 Editor *pPad = (Editor *)client_data;
279 /* call the callback for Exit within the File Menu pulldown */
280 XtCallCallbacks(pPad->ExitWid, XmNactivateCallback, (XtPointer)pPad);
284 /***********************************************************************
286 * restoreSession - gets the valid x and y location of where to put the
287 * Text Edit on the root window. Sets the global varible
288 * dtpad.saveRestore to True so the rest of the program knows that
289 * a session is being restored.
291 ***********************************************************************/
299 XrmRepresentation rep_type;
301 char *fileName = pPad->xrdb.session;
303 int numPadsToRestore, i;
306 if(DtSessionRestorePath(topLevelWithWmCommand, &path, fileName) == False)
309 /* Open the file as a resource database */
310 if ((db = XrmGetFileDatabase (path)) == NULL)
312 tmpStr = (char *)XtMalloc(strlen(MSG2) + strlen(path)+ 1);
313 sprintf(tmpStr, MSG2, path);
314 _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
319 xrm_name[0] = XrmStringToQuark ("pads");
320 xrm_name[1] = XrmStringToQuark ("numActivePads");
322 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
323 numPadsToRestore = atoi((char *)value.addr);
325 if(numPadsToRestore == 0)
328 * Either it's an old (i.e. 2.01) session file,
329 * or it's bogus. Either way, we'll create one
330 * window, taking whatever mainWindow: resources
333 xrm_name[0] = XrmStringToQuark ("mainWindow");
337 xrm_name[1] = XrmStringToQuark ("x");
338 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
339 pPad->x = atoi((char *)value.addr);
342 xrm_name[1] = XrmStringToQuark ("y");
343 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
344 pPad->y = atoi((char *)value.addr);
346 pPad->saveRestore = True;
351 RestorePad(pPad, 0, db);
353 for(i = 1; i < numPadsToRestore; i++)
355 foundPad = FindOrCreatePad(&pPad);
356 RestorePad(pPad, i, db);
358 if(foundPad == False)
361 ManageOldPad(pPad, False);
366 /************************************************************************
370 ***********************************************************************/
377 char * iconify = NULL;
380 XrmRepresentation rep_type;
383 sprintf(buf, "mainWindow%d", padNum);
384 xrm_name[0] = XrmStringToQuark(buf);
388 xrm_name[1] = XrmStringToQuark ("x");
389 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
390 pPad->x = atoi((char *)value.addr);
393 xrm_name [1] = XrmStringToQuark ("y");
394 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
395 pPad->y = atoi((char *)value.addr);
398 xrm_name [1] = XrmStringToQuark ("width");
399 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
400 pPad->width = atoi((char *)value.addr);
403 xrm_name [1] = XrmStringToQuark ("height");
404 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
405 pPad->height = atoi((char *)value.addr);
407 /* Get and set whether the view is iconic */
408 xrm_name [1] = XrmStringToQuark ("iconify");
409 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
410 /* If there is an iconify resource and its value is True, */
411 /* then mark the window as iconified. */
412 if ((iconify = (char *) value.addr) != NULL &&
413 strcmp (iconify, "True") == 0)
416 pPad->iconic = False;
418 /* get the file name */
419 xrm_name [1] = XrmStringToQuark ("fileName");
420 XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
421 pPad->fileStuff.fileName = strdup((char *) value.addr);
423 /* get workspace list */
424 xrm_name[1] = XrmStringToQuark("workspaceList");
425 XrmQGetResource(db, xrm_name, xrm_name, &rep_type, &value);
426 pPad->xrdb.workspaceList = strdup((char *) value.addr);
431 /************************************************************************
435 ***********************************************************************/
442 RestoreMain(pPad, padNum, db);
443 pPad->saveRestore = True;