Committing in .
[wxWidgets.git] / src / motif / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "filedlg.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
19 #define XtWindow XTWINDOW
20 #endif
21
22 #include "wx/defs.h"
23 #include "wx/utils.h"
24 #include "wx/dialog.h"
25 #include "wx/filedlg.h"
26 #include "wx/intl.h"
27 #include "wx/app.h"
28 #include "wx/settings.h"
29
30 #ifdef __VMS__
31 #pragma message disable nosimpint
32 #endif
33 #include <Xm/Xm.h>
34 #include <Xm/MwmUtil.h>
35 #include <Xm/Label.h>
36 #include <Xm/BulletinB.h>
37 #include <Xm/Frame.h>
38 #include <Xm/Text.h>
39 #include <Xm/DialogS.h>
40 #include <Xm/FileSB.h>
41 #include <Xm/RowColumn.h>
42 #include <Xm/LabelG.h>
43 #ifdef __VMS__
44 #pragma message enable nosimpint
45 #endif
46
47 #include "wx/motif/private.h"
48
49 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
50
51 #define DEFAULT_FILE_SELECTOR_SIZE 0
52 // Let Motif defines the size of File
53 // Selector Box (if 1), or fix it to
54 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
55 #define wxFSB_WIDTH 600
56 #define wxFSB_HEIGHT 500
57
58
59 wxString wxFileSelector(const char *title,
60 const char *defaultDir, const char *defaultFileName,
61 const char *defaultExtension, const char *filter, int flags,
62 wxWindow *parent, int x, int y)
63 {
64 // If there's a default extension specified but no filter, we create a suitable
65 // filter.
66
67 wxString filter2("");
68 if ( defaultExtension && !filter )
69 filter2 = wxString("*.") + wxString(defaultExtension) ;
70 else if ( filter )
71 filter2 = filter;
72
73 wxString defaultDirString;
74 if (defaultDir)
75 defaultDirString = defaultDir;
76 else
77 defaultDirString = "";
78
79 wxString defaultFilenameString;
80 if (defaultFileName)
81 defaultFilenameString = defaultFileName;
82 else
83 defaultFilenameString = "";
84
85 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
86
87 if ( fileDialog.ShowModal() == wxID_OK )
88 {
89 return fileDialog.GetPath();
90 }
91 else
92 return wxEmptyString;
93 }
94
95 wxString wxFileSelectorEx(const char *title,
96 const char *defaultDir,
97 const char *defaultFileName,
98 int* defaultFilterIndex,
99 const char *filter,
100 int flags,
101 wxWindow* parent,
102 int x,
103 int y)
104
105 {
106 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
107 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
108
109 if ( fileDialog.ShowModal() == wxID_OK )
110 {
111 *defaultFilterIndex = fileDialog.GetFilterIndex();
112 return fileDialog.GetPath();
113 }
114 else
115 return wxEmptyString;
116 }
117
118 wxString wxFileDialog::m_fileSelectorAnswer = "";
119 bool wxFileDialog::m_fileSelectorReturned = FALSE;
120
121 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
122 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
123 {
124 wxFileDialog::m_fileSelectorAnswer = "";
125 wxFileDialog::m_fileSelectorReturned = TRUE;
126 }
127
128 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
129 {
130 char *filename = NULL;
131 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
132 wxFileDialog::m_fileSelectorAnswer = "";
133 wxFileDialog::m_fileSelectorReturned = TRUE;
134 } else {
135 if (filename) {
136 wxFileDialog::m_fileSelectorAnswer = filename;
137 XtFree(filename);
138 }
139 wxFileDialog::m_fileSelectorReturned = TRUE;
140 }
141 }
142
143 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
144 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
145 long style, const wxPoint& pos)
146 {
147 m_message = message;
148 m_dialogStyle = style;
149 m_parent = parent;
150 m_path = "";
151 m_fileName = defaultFileName;
152 m_dir = defaultDir;
153 m_wildCard = wildCard;
154 m_filterIndex = 1;
155 m_pos = pos;
156 }
157
158 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
159 {
160 wxWindow::DoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
161
162 // Change colour of the scrolled areas of the listboxes
163 Widget listParent = XtParent (widget);
164 wxWindow::DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
165
166 Widget hsb = (Widget) 0;
167 Widget vsb = (Widget) 0;
168 XtVaGetValues (listParent,
169 XmNhorizontalScrollBar, &hsb,
170 XmNverticalScrollBar, &vsb,
171 NULL);
172
173 /* TODO: should scrollbars be affected? Should probably have separate
174 * function to change them (by default, taken from wxSystemSettings)
175 */
176 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
177 wxWindow::DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
178 wxWindow::DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
179
180 if (hsb)
181 XtVaSetValues (hsb,
182 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
183 NULL);
184 if (vsb)
185 XtVaSetValues (vsb,
186 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
187 NULL);
188 }
189
190 int wxFileDialog::ShowModal()
191 {
192 wxBeginBusyCursor();
193
194 // static char fileBuf[512];
195 Widget parentWidget = (Widget) 0;
196 if (m_parent)
197 {
198 parentWidget = (Widget) m_parent->GetTopWidget();
199 }
200 else
201 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
202 // prepare the arg list
203 Arg args[10];
204 int ac = 0;
205
206 wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour,
207 (wxColour*) NULL);
208
209 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
210 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
211 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
212 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
213
214
215 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
216 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
217
218 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
219 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
220 Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST);
221 Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST);
222
223 // code using these vars disabled
224 #if 0
225 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
226 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
227 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
228 #endif
229
230
231 Widget shell = XtParent(fileSel);
232
233 if (!m_message.IsNull())
234 XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL);
235
236 wxString entirePath("");
237
238 if ((m_dir != "") && (m_fileName != ""))
239 {
240 entirePath = m_dir + wxString("/") + m_fileName;
241 }
242 else if ((m_dir != "") && (m_fileName == ""))
243 {
244 entirePath = m_dir + wxString("/");
245 }
246 else if ((m_dir == "") && (m_fileName != ""))
247 {
248 entirePath = m_fileName;
249 }
250
251 if (entirePath != "")
252 {
253 XmTextSetString(selectionWidget, (char*) (const char*) entirePath);
254 }
255
256 if (m_wildCard != "")
257 {
258 wxString filter("");
259 if (m_dir != "")
260 filter = m_dir + wxString("/") + m_wildCard;
261 else
262 filter = m_wildCard;
263
264 XmTextSetString(filterWidget, (char*) (const char*) filter);
265 XmFileSelectionDoSearch(fileSel, NULL);
266 }
267
268 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
269 // file selector on Solaris 1.5.1.
270 if ( m_dir != "" )
271 {
272 XmString thePath = XmStringCreateLtoR ((char*) (const char*) m_dir,
273 XmSTRING_DEFAULT_CHARSET);
274
275 XtVaSetValues (fileSel,
276 XmNdirectory, thePath,
277 NULL);
278
279 XmStringFree(thePath);
280 }
281
282 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
283 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
284
285 //#if XmVersion > 1000
286 // I'm not sure about what you mean with XmVersion.
287 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
288 // (Motif1.1.4 ==> XmVersion 1100 )
289 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
290 //
291 #if !DEFAULT_FILE_SELECTOR_SIZE
292 int width = wxFSB_WIDTH;
293 int height = wxFSB_HEIGHT;
294 XtVaSetValues(fileSel,
295 XmNwidth, width,
296 XmNheight, height,
297 XmNresizePolicy, XmRESIZE_NONE,
298 NULL);
299 #endif
300 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
301 DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
302 DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
303
304 wxChangeListBoxColours(this, dirListWidget);
305 wxChangeListBoxColours(this, fileListWidget);
306
307 XtManageChild(fileSel);
308
309 m_fileSelectorAnswer = "";
310 m_fileSelectorReturned = FALSE;
311
312 wxEndBusyCursor();
313
314 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
315 XEvent event;
316 while (!m_fileSelectorReturned)
317 {
318 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
319 }
320 XtRemoveGrab(XtParent(fileSel));
321
322 XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
323
324 // XtDestroyWidget(fileSel);
325 XtUnmapWidget(XtParent(fileSel));
326 XtDestroyWidget(XtParent(fileSel));
327
328 // Now process all events, because otherwise
329 // this might remain on the screen
330 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
331 while (XtAppPending((XtAppContext) wxTheApp->GetAppContext()))
332 {
333 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
334 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
335 XtDispatchEvent(&event);
336 }
337
338 m_path = m_fileSelectorAnswer;
339 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
340 m_dir = wxPathOnly(m_path);
341
342 if (m_fileName == "")
343 return wxID_CANCEL;
344 else
345 return wxID_OK;
346 }
347
348 // Generic file load/save dialog
349 static wxString
350 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
351 {
352 char *ext = (char *)extension;
353
354 wxString prompt;
355 wxString str;
356 if (load)
357 str = _("Load %s file");
358 else
359 str = _("Save %s file");
360 prompt.Printf(str, what);
361
362 if (*ext == '.')
363 ext++;
364 wxString wild;
365 wild.Printf("*.%s", ext);
366
367 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
368 }
369
370 // Generic file load dialog
371 wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
372 {
373 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
374 }
375
376
377 // Generic file save dialog
378 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
379 {
380 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
381 }
382
383