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