Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dticon / fileIODialog.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $TOG: fileIODialog.c /main/6 1999/06/01 10:55:30 mgreess $ */
24 /*********************************************************************
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 Unix System Labs, Inc., a subsidiary of
29 *      Novell, Inc.
30 **********************************************************************/
31 /*******************************************************************************
32         fileIODialog.c
33
34 *******************************************************************************/
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/param.h>
41 #include <Xm/Xm.h>
42 #include <Xm/FileSB.h>
43 #include <Xm/DialogS.h>
44 #include <Xm/MwmUtil.h>
45 #include "main.h"
46 #include "externals.h"
47 #include <Dt/DtPStrings.h>
48
49 #include <Xm/FileSB.h>
50
51 /*******************************************************************************
52         Includes, Defines, and Global variables from the Declarations Editor:
53 *******************************************************************************/
54
55 extern void Do_FileIO();
56
57 Widget  fileIODialog;
58
59 /*******************************************************************************
60         Forward declarations of functions that are defined later in this file.
61 *******************************************************************************/
62
63 Widget  create_fileIODialog();
64
65 /*******************************************************************************
66         The following are callback functions.
67 *******************************************************************************/
68
69 static void
70 cancelCB_fileIODialog(
71         Widget w,
72         XtPointer clientData,
73         XtPointer callbackArg )
74 {
75     XtUnmanageChild(fileIODialog);
76 }
77
78 /*******************************************************************************
79         The following is the 'Interface function' which is the
80         external entry point for creating this interface.
81         This function should be called from your application or from
82         a callback function.
83 *******************************************************************************/
84 Widget
85 create_fileIODialog( void )
86 {
87     Widget    fileIODialog_shell, wid;
88     XmString tmpXmStr, tmpXmStr1, tmpXmStr2, tmpXmStr3, tmpXmStr4,
89              tmpXmStr6, tmpXmStr7, tmpXmStr8;
90     Arg  args[10];
91     int  n;
92     char *userIconPath, *s;
93     struct stat sbuf;
94
95     userIconPath = getenv("XMICONSEARCHPATH");
96     if (NULL == userIconPath)
97       userIconPath = getenv("XMICONBMSEARCHPATH");
98     if (NULL == userIconPath)
99       userIconPath = getenv("HOME");
100
101     if (NULL != userIconPath)
102     {
103         char *s;
104
105         userIconPath = XtNewString(userIconPath);
106         s = strchr(userIconPath, ':');
107         if (NULL != s) *s = '\0';
108         s = strrchr(userIconPath, '/');
109         if (NULL != s) *s = '\0';
110     }
111
112     fileIODialog_shell = XtVaCreatePopupShell( "fileIODialog_shell",
113                         xmDialogShellWidgetClass, mainWindow,
114                         XmNtitle, GETSTR(2,2, "Icon Editor - File I/O"),
115                         NULL );
116
117     tmpXmStr  = GETXMSTR(2,6,  "Icon Editor - Save As");
118     tmpXmStr1 = GETXMSTR(2,10, "Filter");
119     tmpXmStr2 = GETXMSTR(2,12, "Folders");
120     tmpXmStr3 = GETXMSTR(2,14, "Files");
121     tmpXmStr4 = GETXMSTR(2,16, "Enter file name:");
122     tmpXmStr6 = GETXMSTR(4,8,  "Cancel");
123     tmpXmStr7 = GETXMSTR(4,10, "Help");
124
125     fileIODialog = XtVaCreateWidget( "fileIODialog",
126                         xmFileSelectionBoxWidgetClass, fileIODialog_shell,
127                         XmNdialogTitle, tmpXmStr,
128                         XmNfilterLabelString, tmpXmStr1,
129                         XmNdirListLabelString, tmpXmStr2,
130                         XmNfileListLabelString, tmpXmStr3,
131                         XmNselectionLabelString, tmpXmStr4,
132                         XmNapplyLabelString, tmpXmStr1,
133                         XmNcancelLabelString, tmpXmStr6,
134                         XmNhelpLabelString, tmpXmStr7,
135                         NULL );
136
137     userIconPath = getenv("XMICONSEARCHPATH");
138     if (NULL != userIconPath)
139     {
140         userIconPath = XtNewString(userIconPath);
141         s = strchr(userIconPath, ':');
142         if (NULL != s) *s = '\0';
143         s = strrchr(userIconPath, '/');
144         if (NULL != s && userIconPath != s) *s = '\0';
145
146         if (0 != stat(userIconPath, &sbuf) ||
147             0 == (S_IFDIR & sbuf.st_mode) ||
148             0 == (S_IWUSR & sbuf.st_mode))
149         {
150             XtFree(userIconPath);
151             userIconPath = NULL;
152         }
153     }
154     if (NULL == userIconPath)
155     {
156         userIconPath = getenv("XMICONBMSEARCHPATH");
157         if (NULL != userIconPath)
158         {
159             userIconPath = XtNewString(userIconPath);
160             s = strchr(userIconPath, ':');
161             if (NULL != s) *s = '\0';
162             s = strrchr(userIconPath, '/');
163             if (NULL != s && userIconPath != s) *s = '\0';
164
165             if (0 != stat(userIconPath, &sbuf) ||
166                 0 == (S_IFDIR & sbuf.st_mode) ||
167                 0 == (S_IWUSR & sbuf.st_mode))
168             {
169                 XtFree(userIconPath);
170                 userIconPath = NULL;
171             }
172         }
173     }
174     if (NULL == userIconPath)
175     {
176         char    buf[MAXPATHLEN+1];
177         char    *home;
178
179         home = getenv("HOME");
180         sprintf(buf, "%s/%s/icons", home, DtPERSONAL_CONFIG_DIRECTORY);
181
182         if (0 == stat(buf, &sbuf) &&
183             S_IFDIR & sbuf.st_mode &&
184             S_IWUSR & sbuf.st_mode)
185         {
186             userIconPath = XtNewString(buf);
187         }
188     }
189     if (NULL != userIconPath)
190     {
191         tmpXmStr8 = XmStringCreateLocalized(userIconPath);
192         XtVaSetValues(fileIODialog, XmNdirectory, tmpXmStr8, NULL);
193         XmStringFree(tmpXmStr8);
194     }
195
196     XmStringFree(tmpXmStr);
197     XmStringFree(tmpXmStr1);
198     XmStringFree(tmpXmStr2);
199     XmStringFree(tmpXmStr3);
200     XmStringFree(tmpXmStr4);
201     XmStringFree(tmpXmStr6);
202     XmStringFree(tmpXmStr7);
203
204     XtAddCallback(fileIODialog, XmNcancelCallback, cancelCB_fileIODialog, NULL);
205     XtAddCallback(fileIODialog, XmNokCallback, Do_FileIO, NULL);
206
207     n = 0;
208     XtSetArg (args[n], XmNuseAsyncGeometry, True);                          n++;
209     XtSetArg (args[n], XmNmwmInputMode,MWM_INPUT_PRIMARY_APPLICATION_MODAL);n++;
210     XtSetValues (fileIODialog_shell, args, n);
211
212
213     /* check resources to get rid of unwanted children :-) */
214     if (!xrdb.useFileFilter)
215     {
216         /* remove file filter label and text, and filter button */
217         wid = XmFileSelectionBoxGetChild(fileIODialog, XmDIALOG_FILTER_LABEL);
218         XtUnmanageChild (wid);
219         wid = XmFileSelectionBoxGetChild(fileIODialog, XmDIALOG_FILTER_TEXT);
220         XtUnmanageChild (wid);
221
222         /* if no File Lists, unmanage it... otherwise rename it */
223         if (!xrdb.useFileLists)
224         {
225             XtUnmanageChild (XmFileSelectionBoxGetChild(fileIODialog,
226                                                         XmDIALOG_APPLY_BUTTON));
227         }
228         else
229         {
230             n = 0;
231             tmpXmStr = GETXMSTR(2,18, "Update");
232             XtSetArg (args[n], XmNapplyLabelString, tmpXmStr);         n++;
233             XtSetValues (fileIODialog, args, n);
234             XmStringFree(tmpXmStr);
235         }
236     }
237     if (!xrdb.useFileLists)
238     {
239         /* remove label and scrolled lists for directories and files */
240         wid = XmFileSelectionBoxGetChild(fileIODialog, XmDIALOG_DIR_LIST_LABEL);
241         XtUnmanageChild (wid);
242         wid = XmFileSelectionBoxGetChild(fileIODialog, XmDIALOG_LIST_LABEL);
243         XtUnmanageChild (wid);
244         wid = XmFileSelectionBoxGetChild(fileIODialog, XmDIALOG_DIR_LIST);
245         XtUnmanageChild (XtParent(wid));
246         wid = XmFileSelectionBoxGetChild(fileIODialog, XmDIALOG_LIST);
247         XtUnmanageChild (XtParent(wid));
248     }
249
250     return ( fileIODialog );
251 }
252
253 /*******************************************************************************
254         END OF FILE
255 *******************************************************************************/
256