]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
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 | ||
bcd055ae JJ |
16 | #ifdef __VMS |
17 | #define XtDisplay XTDISPLAY | |
18 | #define XtParent XTPARENT | |
19 | #define XtWindow XTWINDOW | |
20 | #endif | |
21 | ||
4bb6408c JS |
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" | |
dfad0599 | 27 | #include "wx/app.h" |
a91b47e8 | 28 | #include "wx/settings.h" |
a4e64fb5 | 29 | #include "wx/tokenzr.h" |
4bb6408c | 30 | |
338dd992 JJ |
31 | #ifdef __VMS__ |
32 | #pragma message disable nosimpint | |
33 | #endif | |
f97c9854 | 34 | #include <Xm/Xm.h> |
dfad0599 JS |
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> | |
338dd992 JJ |
44 | #ifdef __VMS__ |
45 | #pragma message enable nosimpint | |
46 | #endif | |
f97c9854 | 47 | |
ee1aaf99 JS |
48 | #include "wx/motif/private.h" |
49 | ||
4bb6408c | 50 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) |
4bb6408c | 51 | |
dfe1eee3 | 52 | #define DEFAULT_FILE_SELECTOR_SIZE 0 |
2d120f83 JS |
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) | |
dfe1eee3 | 56 | #define wxFSB_WIDTH 600 |
dfad0599 JS |
57 | #define wxFSB_HEIGHT 500 |
58 | ||
59 | ||
7482b220 | 60 | wxString wxFileSelector(const char *title, |
4bb6408c JS |
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. | |
dfe1eee3 | 67 | |
4bb6408c JS |
68 | wxString filter2(""); |
69 | if ( defaultExtension && !filter ) | |
70 | filter2 = wxString("*.") + wxString(defaultExtension) ; | |
71 | else if ( filter ) | |
72 | filter2 = filter; | |
dfe1eee3 | 73 | |
4bb6408c JS |
74 | wxString defaultDirString; |
75 | if (defaultDir) | |
76 | defaultDirString = defaultDir; | |
77 | else | |
78 | defaultDirString = ""; | |
dfe1eee3 | 79 | |
4bb6408c JS |
80 | wxString defaultFilenameString; |
81 | if (defaultFileName) | |
82 | defaultFilenameString = defaultFileName; | |
83 | else | |
84 | defaultFilenameString = ""; | |
dfe1eee3 | 85 | |
4bb6408c | 86 | wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y)); |
dfe1eee3 | 87 | |
4bb6408c JS |
88 | if ( fileDialog.ShowModal() == wxID_OK ) |
89 | { | |
7482b220 | 90 | return fileDialog.GetPath(); |
4bb6408c JS |
91 | } |
92 | else | |
7482b220 | 93 | return wxEmptyString; |
4bb6408c JS |
94 | } |
95 | ||
7482b220 | 96 | wxString wxFileSelectorEx(const char *title, |
4bb6408c JS |
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) | |
dfe1eee3 | 105 | |
4bb6408c JS |
106 | { |
107 | wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "", | |
108 | defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y)); | |
dfe1eee3 | 109 | |
4bb6408c JS |
110 | if ( fileDialog.ShowModal() == wxID_OK ) |
111 | { | |
112 | *defaultFilterIndex = fileDialog.GetFilterIndex(); | |
c330a2cf | 113 | return fileDialog.GetPath(); |
4bb6408c JS |
114 | } |
115 | else | |
7482b220 | 116 | return wxEmptyString; |
4bb6408c JS |
117 | } |
118 | ||
dfad0599 JS |
119 | wxString wxFileDialog::m_fileSelectorAnswer = ""; |
120 | bool wxFileDialog::m_fileSelectorReturned = FALSE; | |
f97c9854 | 121 | |
a4e64fb5 MB |
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 | ||
dfe1eee3 | 130 | void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), |
2d120f83 | 131 | XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) ) |
f97c9854 | 132 | { |
2d120f83 JS |
133 | wxFileDialog::m_fileSelectorAnswer = ""; |
134 | wxFileDialog::m_fileSelectorReturned = TRUE; | |
f97c9854 JS |
135 | } |
136 | ||
f9e02ac7 | 137 | void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs) |
f97c9854 | 138 | { |
2d120f83 JS |
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; | |
f97c9854 | 149 | } |
f97c9854 JS |
150 | } |
151 | ||
a4e64fb5 MB |
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 | ||
dfad0599 | 172 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
2d120f83 JS |
173 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, |
174 | long style, const wxPoint& pos) | |
dfad0599 JS |
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 | } | |
f97c9854 | 186 | |
af111fc3 | 187 | static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget) |
a91b47e8 | 188 | { |
dfe1eee3 | 189 | wxWindow::DoChangeBackgroundColour((WXWidget) widget, *wxWHITE); |
a91b47e8 JS |
190 | |
191 | // Change colour of the scrolled areas of the listboxes | |
192 | Widget listParent = XtParent (widget); | |
d4d46b56 | 193 | #if 0 |
dfe1eee3 | 194 | wxWindow::DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE); |
d4d46b56 | 195 | #endif |
a91b47e8 JS |
196 | |
197 | Widget hsb = (Widget) 0; | |
198 | Widget vsb = (Widget) 0; | |
199 | XtVaGetValues (listParent, | |
200 | XmNhorizontalScrollBar, &hsb, | |
201 | XmNverticalScrollBar, &vsb, | |
202 | NULL); | |
dfe1eee3 | 203 | |
a91b47e8 JS |
204 | /* TODO: should scrollbars be affected? Should probably have separate |
205 | * function to change them (by default, taken from wxSystemSettings) | |
206 | */ | |
a756f210 | 207 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
dfe1eee3 VZ |
208 | wxWindow::DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); |
209 | wxWindow::DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
a91b47e8 JS |
210 | |
211 | if (hsb) | |
212 | XtVaSetValues (hsb, | |
213 | XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)), | |
214 | NULL); | |
215 | if (vsb) | |
216 | XtVaSetValues (vsb, | |
217 | XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)), | |
218 | NULL); | |
219 | } | |
220 | ||
dfad0599 | 221 | int wxFileDialog::ShowModal() |
f97c9854 | 222 | { |
2d120f83 | 223 | wxBeginBusyCursor(); |
dfe1eee3 | 224 | |
2d120f83 JS |
225 | // static char fileBuf[512]; |
226 | Widget parentWidget = (Widget) 0; | |
227 | if (m_parent) | |
228 | { | |
229 | parentWidget = (Widget) m_parent->GetTopWidget(); | |
230 | } | |
f97c9854 | 231 | else |
2d120f83 | 232 | parentWidget = (Widget) wxTheApp->GetTopLevelWidget(); |
ee1aaf99 JS |
233 | // prepare the arg list |
234 | Arg args[10]; | |
235 | int ac = 0; | |
236 | ||
237 | wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour, | |
238 | (wxColour*) NULL); | |
239 | ||
240 | XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++; | |
241 | XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++; | |
242 | XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++; | |
243 | XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++; | |
244 | ||
dfe1eee3 | 245 | |
ee1aaf99 | 246 | Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac); |
2d120f83 | 247 | XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON)); |
a91b47e8 JS |
248 | |
249 | Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT); | |
250 | Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT); | |
251 | Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST); | |
252 | Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST); | |
dfe1eee3 VZ |
253 | |
254 | // code using these vars disabled | |
255 | #if 0 | |
a91b47e8 JS |
256 | Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON); |
257 | Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON); | |
258 | Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON); | |
dfe1eee3 VZ |
259 | #endif |
260 | ||
a91b47e8 | 261 | |
2d120f83 | 262 | Widget shell = XtParent(fileSel); |
dfe1eee3 | 263 | |
2d120f83 JS |
264 | if (!m_message.IsNull()) |
265 | XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL); | |
dfe1eee3 | 266 | |
2d120f83 | 267 | wxString entirePath(""); |
dfe1eee3 | 268 | |
2d120f83 JS |
269 | if ((m_dir != "") && (m_fileName != "")) |
270 | { | |
271 | entirePath = m_dir + wxString("/") + m_fileName; | |
272 | } | |
273 | else if ((m_dir != "") && (m_fileName == "")) | |
274 | { | |
275 | entirePath = m_dir + wxString("/"); | |
276 | } | |
277 | else if ((m_dir == "") && (m_fileName != "")) | |
278 | { | |
279 | entirePath = m_fileName; | |
280 | } | |
dfe1eee3 | 281 | |
2d120f83 JS |
282 | if (m_wildCard != "") |
283 | { | |
a4e64fb5 MB |
284 | // return something understandable by Motif |
285 | wxString wildCard = ParseWildCard( m_wildCard ); | |
286 | wxString filter; | |
2d120f83 | 287 | if (m_dir != "") |
a4e64fb5 | 288 | filter = m_dir + wxString("/") + wildCard; |
2d120f83 | 289 | else |
a4e64fb5 | 290 | filter = wildCard; |
dfe1eee3 | 291 | |
a4e64fb5 | 292 | XmTextSetString(filterWidget, (char*)filter.c_str()); |
2d120f83 JS |
293 | XmFileSelectionDoSearch(fileSel, NULL); |
294 | } | |
dfe1eee3 | 295 | |
2d120f83 JS |
296 | // Suggested by Terry Gitnick, 16/9/97, because of change in Motif |
297 | // file selector on Solaris 1.5.1. | |
298 | if ( m_dir != "" ) | |
299 | { | |
d4d46b56 | 300 | wxXmString thePath( m_dir ); |
dfe1eee3 | 301 | |
2d120f83 | 302 | XtVaSetValues (fileSel, |
d4d46b56 | 303 | XmNdirectory, thePath(), |
2d120f83 | 304 | NULL); |
d4d46b56 | 305 | } |
dfe1eee3 | 306 | |
d4d46b56 MB |
307 | if (entirePath != "") |
308 | { | |
309 | XmTextSetString(selectionWidget, (char*)entirePath.c_str()); | |
2d120f83 | 310 | } |
dfe1eee3 | 311 | |
2d120f83 JS |
312 | XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL); |
313 | XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL); | |
a4e64fb5 MB |
314 | XtAddCallback(fileSel, XmNunmapCallback, |
315 | (XtCallbackProc)wxFileSelClose, (XtPointer)this); | |
dfe1eee3 | 316 | |
2d120f83 JS |
317 | //#if XmVersion > 1000 |
318 | // I'm not sure about what you mean with XmVersion. | |
319 | // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200 | |
320 | // (Motif1.1.4 ==> XmVersion 1100 ) | |
321 | // Nevertheless, I put here a #define, so anyone can choose in (I)makefile... | |
322 | // | |
f97c9854 | 323 | #if !DEFAULT_FILE_SELECTOR_SIZE |
2d120f83 JS |
324 | int width = wxFSB_WIDTH; |
325 | int height = wxFSB_HEIGHT; | |
326 | XtVaSetValues(fileSel, | |
327 | XmNwidth, width, | |
328 | XmNheight, height, | |
329 | XmNresizePolicy, XmRESIZE_NONE, | |
330 | NULL); | |
f97c9854 | 331 | #endif |
ee1aaf99 | 332 | // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour); |
a91b47e8 JS |
333 | DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE); |
334 | DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE); | |
335 | ||
a91b47e8 JS |
336 | wxChangeListBoxColours(this, dirListWidget); |
337 | wxChangeListBoxColours(this, fileListWidget); | |
dfe1eee3 | 338 | |
2d120f83 | 339 | XtManageChild(fileSel); |
a91b47e8 | 340 | |
2d120f83 JS |
341 | m_fileSelectorAnswer = ""; |
342 | m_fileSelectorReturned = FALSE; | |
dfe1eee3 | 343 | |
2d120f83 | 344 | wxEndBusyCursor(); |
dfe1eee3 | 345 | |
2d120f83 JS |
346 | XtAddGrab(XtParent(fileSel), TRUE, FALSE); |
347 | XEvent event; | |
348 | while (!m_fileSelectorReturned) | |
349 | { | |
350 | XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); | |
351 | } | |
352 | XtRemoveGrab(XtParent(fileSel)); | |
dfe1eee3 | 353 | |
2d120f83 | 354 | XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental |
dfe1eee3 | 355 | |
2d120f83 JS |
356 | // XtDestroyWidget(fileSel); |
357 | XtUnmapWidget(XtParent(fileSel)); | |
358 | XtDestroyWidget(XtParent(fileSel)); | |
dfe1eee3 | 359 | |
2d120f83 JS |
360 | // Now process all events, because otherwise |
361 | // this might remain on the screen | |
362 | XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE); | |
363 | while (XtAppPending((XtAppContext) wxTheApp->GetAppContext())) | |
364 | { | |
365 | XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())); | |
366 | XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event); | |
367 | XtDispatchEvent(&event); | |
368 | } | |
dfe1eee3 | 369 | |
2d120f83 JS |
370 | m_path = m_fileSelectorAnswer; |
371 | m_fileName = wxFileNameFromPath(m_fileSelectorAnswer); | |
372 | m_dir = wxPathOnly(m_path); | |
dfe1eee3 | 373 | |
2d120f83 JS |
374 | if (m_fileName == "") |
375 | return wxID_CANCEL; | |
376 | else | |
377 | return wxID_OK; | |
4bb6408c JS |
378 | } |
379 | ||
380 | // Generic file load/save dialog | |
7482b220 | 381 | static wxString |
4bb6408c JS |
382 | wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent) |
383 | { | |
2d120f83 | 384 | char *ext = (char *)extension; |
dfe1eee3 VZ |
385 | |
386 | wxString prompt; | |
2d120f83 JS |
387 | wxString str; |
388 | if (load) | |
dfe1eee3 | 389 | str = _("Load %s file"); |
2d120f83 | 390 | else |
dfe1eee3 VZ |
391 | str = _("Save %s file"); |
392 | prompt.Printf(str, what); | |
393 | ||
394 | if (*ext == '.') | |
395 | ext++; | |
396 | wxString wild; | |
397 | wild.Printf("*.%s", ext); | |
398 | ||
2d120f83 | 399 | return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent); |
4bb6408c JS |
400 | } |
401 | ||
402 | // Generic file load dialog | |
7482b220 | 403 | wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent) |
4bb6408c | 404 | { |
2d120f83 | 405 | return wxDefaultFileSelector(TRUE, what, extension, default_name, parent); |
4bb6408c JS |
406 | } |
407 | ||
408 | ||
409 | // Generic file save dialog | |
7482b220 | 410 | wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent) |
4bb6408c | 411 | { |
2d120f83 | 412 | return wxDefaultFileSelector(FALSE, what, extension, default_name, parent); |
4bb6408c JS |
413 | } |
414 | ||
415 |