Fix some GCC 3.2 -O2 warnings.
[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 #ifdef __WXDEBUG__
155 static const wxChar* msg =
156 _T("Motif file dialog does not understand this ")
157 _T("wildcard syntax");
158 #endif
159
160 wxStringTokenizer tok( wild, _T("|") );
161
162 wxCHECK_MSG( tok.CountTokens() <= 2, _T("*.*"), msg );
163
164 if( tok.CountTokens() == 1 ) return wild;
165
166 // CountTokens == 2
167 tok.GetNextToken();
168 wxStringTokenizer tok2( tok.GetNextToken(), _T(";") );
169
170 wxCHECK_MSG( tok2.CountTokens() == 1, tok2.GetNextToken(), msg );
171 return tok2.GetNextToken();
172 }
173
174 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
175 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
176 long style, const wxPoint& pos)
177 {
178 m_message = message;
179 m_dialogStyle = style;
180 m_parent = parent;
181 m_path = "";
182 m_fileName = defaultFileName;
183 m_dir = defaultDir;
184 m_wildCard = wildCard;
185 m_filterIndex = 1;
186 m_pos = pos;
187 }
188
189 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
190 {
191 wxWindow::DoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
192
193 // Change colour of the scrolled areas of the listboxes
194 Widget listParent = XtParent (widget);
195 #if 0
196 wxWindow::DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
197 #endif
198
199 Widget hsb = (Widget) 0;
200 Widget vsb = (Widget) 0;
201 XtVaGetValues (listParent,
202 XmNhorizontalScrollBar, &hsb,
203 XmNverticalScrollBar, &vsb,
204 NULL);
205
206 /* TODO: should scrollbars be affected? Should probably have separate
207 * function to change them (by default, taken from wxSystemSettings)
208 */
209 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
210 wxWindow::DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
211 wxWindow::DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
212
213 if (hsb)
214 XtVaSetValues (hsb,
215 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
216 NULL);
217 if (vsb)
218 XtVaSetValues (vsb,
219 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
220 NULL);
221 }
222
223 int wxFileDialog::ShowModal()
224 {
225 wxBeginBusyCursor();
226
227 // static char fileBuf[512];
228 Widget parentWidget = (Widget) 0;
229 if (m_parent)
230 {
231 parentWidget = (Widget) m_parent->GetTopWidget();
232 }
233 else
234 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
235 // prepare the arg list
236 Arg args[10];
237 int ac = 0;
238
239 wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour,
240 (wxColour*) NULL);
241
242 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
243 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
244 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
245 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
246
247
248 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
249 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
250
251 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
252 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
253 Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST);
254 Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST);
255
256 // code using these vars disabled
257 #if 0
258 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
259 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
260 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
261 #endif
262
263
264 Widget shell = XtParent(fileSel);
265
266 if (!m_message.IsNull())
267 XtVaSetValues(shell,
268 XmNtitle, wxConstCast(m_message.c_str(), char),
269 NULL);
270
271 wxString entirePath("");
272
273 if ((m_dir != "") && (m_fileName != ""))
274 {
275 entirePath = m_dir + wxString("/") + m_fileName;
276 }
277 else if ((m_dir != "") && (m_fileName == ""))
278 {
279 entirePath = m_dir + wxString("/");
280 }
281 else if ((m_dir == "") && (m_fileName != ""))
282 {
283 entirePath = m_fileName;
284 }
285
286 if (m_wildCard != "")
287 {
288 // return something understandable by Motif
289 wxString wildCard = ParseWildCard( m_wildCard );
290 wxString filter;
291 if (m_dir != "")
292 filter = m_dir + wxString("/") + wildCard;
293 else
294 filter = wildCard;
295
296 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
297 XmFileSelectionDoSearch(fileSel, NULL);
298 }
299
300 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
301 // file selector on Solaris 1.5.1.
302 if ( m_dir != "" )
303 {
304 wxXmString thePath( m_dir );
305
306 XtVaSetValues (fileSel,
307 XmNdirectory, thePath(),
308 NULL);
309 }
310
311 if (entirePath != "")
312 {
313 XmTextSetString(selectionWidget,
314 wxConstCast(entirePath.c_str(), char));
315 }
316
317 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
318 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
319 XtAddCallback(fileSel, XmNunmapCallback,
320 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
321
322 //#if XmVersion > 1000
323 // I'm not sure about what you mean with XmVersion.
324 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
325 // (Motif1.1.4 ==> XmVersion 1100 )
326 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
327 //
328 #if !DEFAULT_FILE_SELECTOR_SIZE
329 int width = wxFSB_WIDTH;
330 int height = wxFSB_HEIGHT;
331 XtVaSetValues(fileSel,
332 XmNwidth, width,
333 XmNheight, height,
334 XmNresizePolicy, XmRESIZE_NONE,
335 NULL);
336 #endif
337 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
338 DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
339 DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
340
341 wxChangeListBoxColours(this, dirListWidget);
342 wxChangeListBoxColours(this, fileListWidget);
343
344 XtManageChild(fileSel);
345
346 m_fileSelectorAnswer = "";
347 m_fileSelectorReturned = FALSE;
348
349 wxEndBusyCursor();
350
351 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
352 XEvent event;
353 while (!m_fileSelectorReturned)
354 {
355 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
356 }
357 XtRemoveGrab(XtParent(fileSel));
358
359 XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
360
361 // XtDestroyWidget(fileSel);
362 XtUnmapWidget(XtParent(fileSel));
363 XtDestroyWidget(XtParent(fileSel));
364
365 // Now process all events, because otherwise
366 // this might remain on the screen
367 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
368 while (XtAppPending((XtAppContext) wxTheApp->GetAppContext()))
369 {
370 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
371 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
372 XtDispatchEvent(&event);
373 }
374
375 m_path = m_fileSelectorAnswer;
376 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
377 m_dir = wxPathOnly(m_path);
378
379 if (m_fileName == "")
380 return wxID_CANCEL;
381 else
382 return wxID_OK;
383 }
384
385 // Generic file load/save dialog
386 static wxString
387 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
388 {
389 char *ext = wxConstCast(extension, char);
390
391 wxString prompt;
392 wxString str;
393 if (load)
394 str = _("Load %s file");
395 else
396 str = _("Save %s file");
397 prompt.Printf(str, what);
398
399 if (*ext == '.')
400 ext++;
401 wxString wild;
402 wild.Printf("*.%s", ext);
403
404 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
405 }
406
407 // Generic file load dialog
408 wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
409 {
410 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
411 }
412
413
414 // Generic file save dialog
415 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
416 {
417 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
418 }
419
420