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