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 /* $TOG: Application.C /main/9 1998/07/24 16:14:41 mgreess $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
31 #include "Application.h"
33 #include <Xm/Protocols.h>
38 #include <Dt/EnvControlP.h>
39 #include <Dt/DbUtil.h>
40 #include <Dt/Action.h>
41 #include <Dt/Session.h>
45 #include <stdlib.h> // for putenv function
47 const char *SESSION_FLAG = "-session";
48 const char *SESSION_NAME = "session";
49 const char *SESSION_CLASSNAME = "Session";
50 const char *DEFAULT_FONT = "fixed";
55 XmFontList system_font;
57 Dimension shadow_thickness;
58 } ApplicationArgs, *ApplicationArgsPtr;
60 static XtResource resources[] =
62 {"userFont", "XmCFontList", XmRFontList, sizeof(XmFontList),
63 XtOffsetOf (ApplicationArgs, user_font), XmRString,
65 (XtPointer) DEFAULT_FONT
70 {"systemFont", "XmCFontList", XmRFontList, sizeof(XmFontList),
71 XtOffsetOf (ApplicationArgs, system_font), XmRString,
73 (XtPointer) DEFAULT_FONT
75 {(char *)SESSION_NAME, (char *)SESSION_CLASSNAME, XtRString, sizeof(char *),
79 {"session", "Session", XtRString, sizeof(char *),
81 XtOffsetOf (ApplicationArgs, session_file), XmRString, (XtPointer) NULL,
83 {XmNshadowThickness, XmCShadowThickness, XmRHorizontalDimension,
84 sizeof (Dimension), XtOffsetOf (ApplicationArgs, shadow_thickness),
85 XmRImmediate, (XtPointer) 1
89 static XrmOptionDescRec options[] =
92 {(char *)SESSION_FLAG, (char *)SESSION_NAME, XrmoptionSepArg, NULL}
94 {"-session", "session", XrmoptionSepArg, NULL}
99 extern void XmeRenderTableGetDefaultFont(XmFontList, XFontStruct **);
102 Application::Application(char *name,
106 : MotifUI(NULL, name, appClassName, appClassName)
109 _DtEnvControl(DT_ENV_SET);
112 XtSetLanguageProc(NULL, NULL, NULL);
113 _w = XtVaAppInitialize(&appContext, appClassName, options, XtNumber(options),
114 _argc, _argv, NULL, XmNallowShellResize, true,
115 XmNtitle, name, XmNiconName, name, NULL);
118 display = XtDisplay(_w);
119 root = RootWindowOfScreen(XtScreen(_w));
120 white = WhitePixelOfScreen(XtScreen(_w));
121 black = BlackPixelOfScreen(XtScreen(_w));
122 depth = DefaultDepthOfScreen(XtScreen(_w));
123 int numMouseButtons = XGetPointerMapping(display, (unsigned char *)NULL, 0);
124 bMenuButton = (numMouseButtons < 3) ? Button2 : Button3;
125 ApplicationArgs application_args;
126 XtGetApplicationResources(_w, &application_args, resources,
127 XtNumber(resources), NULL, 0);
128 userFont = application_args.user_font;
129 userFont = application_args.system_font;
136 if (session_file = application_args.session_file)
138 if (*session_file == '/')
139 session_path = strdup(session_file);
141 DtSessionRestorePath(topLevel, &session_path, session_file);
144 shadowThickness = application_args.shadow_thickness;
146 XmFontContext context;
147 if (XmFontListInitFontContext(&context, userFont))
149 XmFontListEntry entry = XmFontListNextEntry(context);
152 XmFontType _type_return;
153 fs = (XFontStruct *)XmFontListEntryGetFont(entry, &_type_return);
154 if (_type_return != XmFONT_IS_FONT)
155 XmeRenderTableGetDefaultFont(userFont, &fs);
158 XmFontListFreeFontContext(context);
167 // If the user specified -display on the command line, we need
168 // to set the environment variable DISPLAY to this value so that
169 // any aixterms or X applications launched from this program
170 // go to the same display
171 new_display = new char [strlen("DISPLAY=") +
172 strlen(DisplayString(display)) + 1];
173 sprintf(new_display, "DISPLAY=%s", DisplayString(display));
176 // Make the environment variable ENV set to nothing. This is so that
177 // we don't run the user's ENV every time we open a child process.
181 if (!DtAppInitialize(appContext, display, topLevel, argv[0], appClassName))
183 // Fatal Error: could not connect to the messaging system.
184 // DtAppInitialize() has already logged an appropriate error msg
187 // Load the filetype/action dbs; DtInvokeAction() requires this
191 Atom xa_WM_SAVE_YOURSELF = XInternAtom(display, "WM_SAVE_YOURSELF", False);
192 XmAddWMProtocols(topLevel, &xa_WM_SAVE_YOURSELF, 1);
193 XmAddWMProtocolCallback(topLevel, xa_WM_SAVE_YOURSELF, SaveSessionCB,
195 XmAddWMProtocolCallback(topLevel,
196 XmInternAtom(display, "WM_DELETE_WINDOW", False),
197 CloseCB, (XtPointer)this);
200 Application::~Application()
203 XmFontListFree(userFont);
210 boolean Application::SetName(char *name)
215 XtVaSetValues(_w, XmNtitle, name, XmNiconName, name, NULL);
220 boolean Application::SetVisiblity(boolean flag)
228 XtVaSetValues(_w, XmNallowShellResize, False, NULL);
234 void Application::Run()
236 XtAppMainLoop(appContext);
239 void Application::SaveSessionCB(Widget, XtPointer client_data, XtPointer)
241 Application *obj = (Application *)client_data;
245 void Application::CloseCB(Widget, XtPointer client_data, XtPointer)
247 Application *obj = (Application *)client_data;
251 void Application::SaveMe(boolean save_as_session)
253 char *path = NULL, *name = NULL;
256 DtSessionSavePath(topLevel, &path, &name);
259 name = SessionFile();
260 char *path1 = SessionPath();
261 path = new char[strlen(path1) + strlen(name) + 2];
262 sprintf(path, "%s/%s", path1, name);
265 if (path && (fp = fopen(path, "w")))
276 char **new_argv = NULL;
280 XtVaGetValues(topLevel, XmNargc, &_argc, XmNargv, &_argv, NULL);
285 if (_argc > 2 && !STRCMP(SESSION_FLAG, _argv[1]))
287 new_argv = new char *[_argc + 1];
288 for (i = 0; i < _argc; i++)
289 new_argv[i] = _argv[i];
297 new_argv = new char *[_argc + 3];
298 new_argv[0] = _argv[0];
299 new_argv[1] = (char *)SESSION_FLAG;
301 for (i = 1, j = 3; i < _argc; i++, j++)
302 new_argv[j] = _argv[i];
309 new_argv = new char *[_argc + 1];
310 for (i = 0; i < _argc; i++)
311 new_argv[i] = _argv[i];
314 XSetCommand(display, XtWindow(topLevel), new_argv, _argc);
323 void Application::Save(char *attribute, char *value)
326 fprintf(fp, "%s\n%s\n", attribute, value);
329 char *Application::Restore(char *attribute)
336 char *name = SessionFile();
337 char *path = SessionPath();
338 session_path = new char[strlen(path) + strlen(name) + 2];
339 sprintf(session_path, "%s/%s", path, name);
340 session_file = strdup(session_path);
342 struct stat statbuff;
343 if (stat(session_path, &statbuff) != -1)
346 if (fp1 = fopen(session_path, "r"))
348 session_info = new char[statbuff.st_size + 1];
349 fread(session_info, (unsigned int)statbuff.st_size, 1, fp1);
351 session_info[statbuff.st_size] = '\0';
352 char *s, *s1 = session_info;
354 while (s && (s = strchr(s1, '\n')))
361 attributes = new char*[n_attrs];
362 values = new char*[n_attrs];
363 s = strtok(session_info, "\n");
368 values[i] = strtok(NULL, "\n");
369 s = strtok(NULL, "\n");
375 session_info = strdup("");
378 for (i = 0; i < n_attrs; i++)
379 if (!STRCMP(attributes[i], attribute))