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