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