]>
Commit | Line | Data |
---|---|---|
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" | |
21 | #include "wx/app.h" | |
22 | #include "wx/settings.h" | |
23 | ||
24 | #include <Xm/Xm.h> | |
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> | |
34 | ||
35 | #if !USE_SHARED_LIBRARY | |
36 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) | |
37 | #endif | |
38 | ||
39 | #define DEFAULT_FILE_SELECTOR_SIZE 0 | |
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) | |
43 | #define wxFSB_WIDTH 600 | |
44 | #define wxFSB_HEIGHT 500 | |
45 | ||
46 | ||
47 | wxString wxFileSelector(const char *title, | |
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. | |
54 | ||
55 | wxString filter2(""); | |
56 | if ( defaultExtension && !filter ) | |
57 | filter2 = wxString("*.") + wxString(defaultExtension) ; | |
58 | else if ( filter ) | |
59 | filter2 = filter; | |
60 | ||
61 | wxString defaultDirString; | |
62 | if (defaultDir) | |
63 | defaultDirString = defaultDir; | |
64 | else | |
65 | defaultDirString = ""; | |
66 | ||
67 | wxString defaultFilenameString; | |
68 | if (defaultFileName) | |
69 | defaultFilenameString = defaultFileName; | |
70 | else | |
71 | defaultFilenameString = ""; | |
72 | ||
73 | wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y)); | |
74 | ||
75 | if ( fileDialog.ShowModal() == wxID_OK ) | |
76 | { | |
77 | return fileDialog.GetPath(); | |
78 | } | |
79 | else | |
80 | return wxEmptyString; | |
81 | } | |
82 | ||
83 | wxString wxFileSelectorEx(const char *title, | |
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) | |
92 | ||
93 | { | |
94 | wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "", | |
95 | defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y)); | |
96 | ||
97 | if ( fileDialog.ShowModal() == wxID_OK ) | |
98 | { | |
99 | *defaultFilterIndex = fileDialog.GetFilterIndex(); | |
100 | return fileDialog.GetPath(); | |
101 | } | |
102 | else | |
103 | return wxEmptyString; | |
104 | } | |
105 | ||
106 | wxString wxFileDialog::m_fileSelectorAnswer = ""; | |
107 | bool wxFileDialog::m_fileSelectorReturned = FALSE; | |
108 | ||
109 | void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), | |
110 | XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) ) | |
111 | { | |
112 | wxFileDialog::m_fileSelectorAnswer = ""; | |
113 | wxFileDialog::m_fileSelectorReturned = TRUE; | |
114 | } | |
115 | ||
116 | void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs) | |
117 | { | |
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; | |
128 | } | |
129 | } | |
130 | ||
131 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, | |
132 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
133 | long style, const wxPoint& pos) | |
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 | } | |
145 | ||
146 | static void wxChangeListBoxColours(wxWindow* win, Widget widget) | |
147 | { | |
148 | win->DoChangeBackgroundColour((WXWidget) widget, *wxWHITE); | |
149 | ||
150 | // Change colour of the scrolled areas of the listboxes | |
151 | Widget listParent = XtParent (widget); | |
152 | win->DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE); | |
153 | ||
154 | Widget hsb = (Widget) 0; | |
155 | Widget vsb = (Widget) 0; | |
156 | XtVaGetValues (listParent, | |
157 | XmNhorizontalScrollBar, &hsb, | |
158 | XmNverticalScrollBar, &vsb, | |
159 | NULL); | |
160 | ||
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); | |
165 | win->DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); | |
166 | win->DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
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 | ||
178 | int wxFileDialog::ShowModal() | |
179 | { | |
180 | wxBeginBusyCursor(); | |
181 | ||
182 | // static char fileBuf[512]; | |
183 | Widget parentWidget = (Widget) 0; | |
184 | if (m_parent) | |
185 | { | |
186 | parentWidget = (Widget) m_parent->GetTopWidget(); | |
187 | } | |
188 | else | |
189 | parentWidget = (Widget) wxTheApp->GetTopLevelWidget(); | |
190 | ||
191 | Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", NULL, 0); | |
192 | XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON)); | |
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); | |
198 | Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON); | |
199 | Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON); | |
200 | Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON); | |
201 | ||
202 | ||
203 | Widget shell = XtParent(fileSel); | |
204 | ||
205 | if (!m_message.IsNull()) | |
206 | XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL); | |
207 | ||
208 | wxString entirePath(""); | |
209 | ||
210 | if ((m_dir != "") && (m_fileName != "")) | |
211 | { | |
212 | entirePath = m_dir + wxString("/") + m_fileName; | |
213 | } | |
214 | else if ((m_dir != "") && (m_fileName == "")) | |
215 | { | |
216 | entirePath = m_dir + wxString("/"); | |
217 | } | |
218 | else if ((m_dir == "") && (m_fileName != "")) | |
219 | { | |
220 | entirePath = m_fileName; | |
221 | } | |
222 | ||
223 | if (entirePath != "") | |
224 | { | |
225 | XmTextSetString(selectionWidget, (char*) (const char*) entirePath); | |
226 | } | |
227 | ||
228 | if (m_wildCard != "") | |
229 | { | |
230 | wxString filter(""); | |
231 | if (m_dir != "") | |
232 | filter = m_dir + wxString("/") + m_wildCard; | |
233 | else | |
234 | filter = m_wildCard; | |
235 | ||
236 | XmTextSetString(filterWidget, (char*) (const char*) filter); | |
237 | XmFileSelectionDoSearch(fileSel, NULL); | |
238 | } | |
239 | ||
240 | // Suggested by Terry Gitnick, 16/9/97, because of change in Motif | |
241 | // file selector on Solaris 1.5.1. | |
242 | if ( m_dir != "" ) | |
243 | { | |
244 | XmString thePath = XmStringCreateLtoR ((char*) (const char*) m_dir, | |
245 | XmSTRING_DEFAULT_CHARSET); | |
246 | ||
247 | XtVaSetValues (fileSel, | |
248 | XmNdirectory, thePath, | |
249 | NULL); | |
250 | ||
251 | XmStringFree(thePath); | |
252 | } | |
253 | ||
254 | XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL); | |
255 | XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL); | |
256 | ||
257 | //#if XmVersion > 1000 | |
258 | // I'm not sure about what you mean with XmVersion. | |
259 | // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200 | |
260 | // (Motif1.1.4 ==> XmVersion 1100 ) | |
261 | // Nevertheless, I put here a #define, so anyone can choose in (I)makefile... | |
262 | // | |
263 | #if !DEFAULT_FILE_SELECTOR_SIZE | |
264 | int width = wxFSB_WIDTH; | |
265 | int height = wxFSB_HEIGHT; | |
266 | XtVaSetValues(fileSel, | |
267 | XmNwidth, width, | |
268 | XmNheight, height, | |
269 | XmNresizePolicy, XmRESIZE_NONE, | |
270 | NULL); | |
271 | #endif | |
272 | DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour); | |
273 | DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE); | |
274 | DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE); | |
275 | ||
276 | /* For some reason this crashes | |
277 | DoChangeBackgroundColour((WXWidget) okWidget, m_backgroundColour, TRUE); | |
278 | DoChangeBackgroundColour((WXWidget) cancelWidget, m_backgroundColour, TRUE); | |
279 | DoChangeBackgroundColour((WXWidget) applyWidget, m_backgroundColour, TRUE); | |
280 | */ | |
281 | ||
282 | wxChangeListBoxColours(this, dirListWidget); | |
283 | wxChangeListBoxColours(this, fileListWidget); | |
284 | ||
285 | XtManageChild(fileSel); | |
286 | ||
287 | m_fileSelectorAnswer = ""; | |
288 | m_fileSelectorReturned = FALSE; | |
289 | ||
290 | wxEndBusyCursor(); | |
291 | ||
292 | XtAddGrab(XtParent(fileSel), TRUE, FALSE); | |
293 | XEvent event; | |
294 | while (!m_fileSelectorReturned) | |
295 | { | |
296 | XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); | |
297 | } | |
298 | XtRemoveGrab(XtParent(fileSel)); | |
299 | ||
300 | XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental | |
301 | ||
302 | // XtDestroyWidget(fileSel); | |
303 | XtUnmapWidget(XtParent(fileSel)); | |
304 | XtDestroyWidget(XtParent(fileSel)); | |
305 | ||
306 | // Now process all events, because otherwise | |
307 | // this might remain on the screen | |
308 | XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE); | |
309 | while (XtAppPending((XtAppContext) wxTheApp->GetAppContext())) | |
310 | { | |
311 | XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())); | |
312 | XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event); | |
313 | XtDispatchEvent(&event); | |
314 | } | |
315 | ||
316 | m_path = m_fileSelectorAnswer; | |
317 | m_fileName = wxFileNameFromPath(m_fileSelectorAnswer); | |
318 | m_dir = wxPathOnly(m_path); | |
319 | ||
320 | if (m_fileName == "") | |
321 | return wxID_CANCEL; | |
322 | else | |
323 | return wxID_OK; | |
324 | } | |
325 | ||
326 | // Generic file load/save dialog | |
327 | static wxString | |
328 | wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent) | |
329 | { | |
330 | char *ext = (char *)extension; | |
331 | ||
332 | char prompt[50]; | |
333 | wxString str; | |
334 | if (load) | |
335 | str = "Load %s file"; | |
336 | else | |
337 | str = "Save %s file"; | |
338 | sprintf(prompt, wxGetTranslation(str), what); | |
339 | ||
340 | if (*ext == '.') ext++; | |
341 | char wild[60]; | |
342 | sprintf(wild, "*.%s", ext); | |
343 | ||
344 | return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent); | |
345 | } | |
346 | ||
347 | // Generic file load dialog | |
348 | wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent) | |
349 | { | |
350 | return wxDefaultFileSelector(TRUE, what, extension, default_name, parent); | |
351 | } | |
352 | ||
353 | ||
354 | // Generic file save dialog | |
355 | wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent) | |
356 | { | |
357 | return wxDefaultFileSelector(FALSE, what, extension, default_name, parent); | |
358 | } | |
359 | ||
360 |