]>
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" |
4bb6408c | 22 | |
f97c9854 | 23 | #include <Xm/Xm.h> |
dfad0599 JS |
24 | #include <Xm/MwmUtil.h> |
25 | #include <Xm/Label.h> | |
26 | #include <Xm/BulletinB.h> | |
27 | #include <Xm/Frame.h> | |
28 | #include <Xm/Text.h> | |
29 | #include <Xm/DialogS.h> | |
30 | #include <Xm/FileSB.h> | |
31 | #include <Xm/RowColumn.h> | |
32 | #include <Xm/LabelG.h> | |
f97c9854 | 33 | |
4bb6408c JS |
34 | #if !USE_SHARED_LIBRARY |
35 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) | |
36 | #endif | |
37 | ||
dfad0599 JS |
38 | #define DEFAULT_FILE_SELECTOR_SIZE 0 |
39 | // Let Motif defines the size of File | |
40 | // Selector Box (if 1), or fix it to | |
41 | // wxFSB_WIDTH x wxFSB_HEIGHT (if 0) | |
42 | #define wxFSB_WIDTH 600 | |
43 | #define wxFSB_HEIGHT 500 | |
44 | ||
45 | ||
4bb6408c JS |
46 | char *wxFileSelector(const char *title, |
47 | const char *defaultDir, const char *defaultFileName, | |
48 | const char *defaultExtension, const char *filter, int flags, | |
49 | wxWindow *parent, int x, int y) | |
50 | { | |
51 | // If there's a default extension specified but no filter, we create a suitable | |
52 | // filter. | |
53 | ||
54 | wxString filter2(""); | |
55 | if ( defaultExtension && !filter ) | |
56 | filter2 = wxString("*.") + wxString(defaultExtension) ; | |
57 | else if ( filter ) | |
58 | filter2 = filter; | |
59 | ||
60 | wxString defaultDirString; | |
61 | if (defaultDir) | |
62 | defaultDirString = defaultDir; | |
63 | else | |
64 | defaultDirString = ""; | |
65 | ||
66 | wxString defaultFilenameString; | |
67 | if (defaultFileName) | |
68 | defaultFilenameString = defaultFileName; | |
69 | else | |
70 | defaultFilenameString = ""; | |
71 | ||
72 | wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y)); | |
73 | ||
74 | if ( fileDialog.ShowModal() == wxID_OK ) | |
75 | { | |
76 | strcpy(wxBuffer, (const char *)fileDialog.GetPath()); | |
77 | return wxBuffer; | |
78 | } | |
79 | else | |
80 | return NULL; | |
81 | } | |
82 | ||
83 | char *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 | strcpy(wxBuffer, (const char *)fileDialog.GetPath()); | |
101 | return wxBuffer; | |
102 | } | |
103 | else | |
104 | return NULL; | |
105 | } | |
106 | ||
dfad0599 JS |
107 | wxString wxFileDialog::m_fileSelectorAnswer = ""; |
108 | bool wxFileDialog::m_fileSelectorReturned = FALSE; | |
f97c9854 | 109 | |
f9e02ac7 RR |
110 | void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), |
111 | XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) ) | |
f97c9854 | 112 | { |
dfad0599 JS |
113 | wxFileDialog::m_fileSelectorAnswer = ""; |
114 | wxFileDialog::m_fileSelectorReturned = TRUE; | |
f97c9854 JS |
115 | } |
116 | ||
f9e02ac7 | 117 | void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs) |
f97c9854 JS |
118 | { |
119 | char *filename = NULL; | |
120 | if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) { | |
dfad0599 JS |
121 | wxFileDialog::m_fileSelectorAnswer = ""; |
122 | wxFileDialog::m_fileSelectorReturned = TRUE; | |
f97c9854 JS |
123 | } else { |
124 | if (filename) { | |
dfad0599 | 125 | wxFileDialog::m_fileSelectorAnswer = filename; |
f97c9854 JS |
126 | XtFree(filename); |
127 | } | |
dfad0599 | 128 | wxFileDialog::m_fileSelectorReturned = TRUE; |
f97c9854 JS |
129 | } |
130 | } | |
131 | ||
dfad0599 JS |
132 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
133 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
134 | long style, const wxPoint& pos) | |
135 | { | |
136 | m_message = message; | |
137 | m_dialogStyle = style; | |
138 | m_parent = parent; | |
139 | m_path = ""; | |
140 | m_fileName = defaultFileName; | |
141 | m_dir = defaultDir; | |
142 | m_wildCard = wildCard; | |
143 | m_filterIndex = 1; | |
144 | m_pos = pos; | |
145 | } | |
f97c9854 | 146 | |
dfad0599 | 147 | int wxFileDialog::ShowModal() |
f97c9854 JS |
148 | { |
149 | wxBeginBusyCursor(); | |
dfad0599 | 150 | |
f9e02ac7 | 151 | // static char fileBuf[512]; |
dfad0599 JS |
152 | Widget parentWidget = (Widget) 0; |
153 | if (m_parent) | |
f97c9854 | 154 | { |
dfad0599 | 155 | parentWidget = (Widget) m_parent->GetTopWidget(); |
f97c9854 | 156 | } |
dfad0599 JS |
157 | else |
158 | parentWidget = (Widget) wxTheApp->GetTopLevelWidget(); | |
f97c9854 JS |
159 | |
160 | Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", NULL, 0); | |
161 | XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON)); | |
162 | ||
163 | Widget shell = XtParent(fileSel); | |
164 | ||
dfad0599 JS |
165 | if (!m_message.IsNull()) |
166 | XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL); | |
f97c9854 | 167 | |
dfad0599 | 168 | wxString entirePath(""); |
f97c9854 | 169 | |
dfad0599 | 170 | if ((m_dir != "") && (m_fileName != "")) |
f97c9854 | 171 | { |
dfad0599 | 172 | entirePath = m_dir + wxString("/") + m_fileName; |
f97c9854 | 173 | } |
dfad0599 | 174 | else if ((m_dir != "") && (m_fileName == "")) |
f97c9854 | 175 | { |
dfad0599 | 176 | entirePath = m_dir + wxString("/"); |
f97c9854 | 177 | } |
dfad0599 | 178 | else if ((m_dir == "") && (m_fileName != "")) |
f97c9854 | 179 | { |
dfad0599 | 180 | entirePath = m_fileName; |
f97c9854 JS |
181 | } |
182 | ||
dfad0599 | 183 | if (entirePath != "") |
f97c9854 JS |
184 | { |
185 | Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT); | |
dfad0599 | 186 | XmTextSetString(selectionWidget, (char*) (const char*) entirePath); |
f97c9854 JS |
187 | } |
188 | ||
dfad0599 | 189 | if (m_wildCard != "") |
f97c9854 | 190 | { |
dfad0599 JS |
191 | wxString filter(""); |
192 | if (m_dir != "") | |
193 | filter = m_dir + wxString("/") + m_wildCard; | |
f97c9854 | 194 | else |
dfad0599 | 195 | filter = m_wildCard; |
f97c9854 JS |
196 | |
197 | Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT); | |
dfad0599 | 198 | XmTextSetString(filterWidget, (char*) (const char*) filter); |
f97c9854 JS |
199 | XmFileSelectionDoSearch(fileSel, NULL); |
200 | } | |
201 | ||
202 | // Suggested by Terry Gitnick, 16/9/97, because of change in Motif | |
203 | // file selector on Solaris 1.5.1. | |
dfad0599 | 204 | if ( m_dir != "" ) |
f97c9854 | 205 | { |
dfad0599 | 206 | XmString thePath = XmStringCreateLtoR ((char*) (const char*) m_dir, |
f97c9854 JS |
207 | XmSTRING_DEFAULT_CHARSET); |
208 | ||
209 | XtVaSetValues (fileSel, | |
210 | XmNdirectory, thePath, | |
211 | NULL); | |
212 | ||
213 | XmStringFree(thePath); | |
214 | } | |
215 | ||
216 | XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL); | |
217 | XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL); | |
218 | ||
219 | //#if XmVersion > 1000 | |
220 | // I'm not sure about what you mean with XmVersion. | |
221 | // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200 | |
222 | // (Motif1.1.4 ==> XmVersion 1100 ) | |
223 | // Nevertheless, I put here a #define, so anyone can choose in (I)makefile... | |
224 | // | |
225 | #if !DEFAULT_FILE_SELECTOR_SIZE | |
226 | int width = wxFSB_WIDTH; | |
227 | int height = wxFSB_HEIGHT; | |
228 | XtVaSetValues(fileSel, | |
229 | XmNwidth, width, | |
230 | XmNheight, height, | |
231 | XmNresizePolicy, XmRESIZE_NONE, | |
232 | NULL); | |
233 | #endif | |
234 | ||
235 | XtManageChild(fileSel); | |
236 | ||
dfad0599 JS |
237 | m_fileSelectorAnswer = ""; |
238 | m_fileSelectorReturned = FALSE; | |
f97c9854 JS |
239 | |
240 | wxEndBusyCursor(); | |
241 | ||
242 | XtAddGrab(XtParent(fileSel), TRUE, FALSE); | |
243 | XEvent event; | |
dfad0599 | 244 | while (!m_fileSelectorReturned) |
f97c9854 | 245 | { |
dfad0599 | 246 | XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); |
f97c9854 JS |
247 | } |
248 | XtRemoveGrab(XtParent(fileSel)); | |
249 | ||
dfad0599 | 250 | XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental |
f97c9854 JS |
251 | |
252 | // XtDestroyWidget(fileSel); | |
253 | XtUnmapWidget(XtParent(fileSel)); | |
254 | XtDestroyWidget(XtParent(fileSel)); | |
255 | ||
256 | // Now process all events, because otherwise | |
257 | // this might remain on the screen | |
dfad0599 JS |
258 | XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE); |
259 | while (XtAppPending((XtAppContext) wxTheApp->GetAppContext())) | |
f97c9854 | 260 | { |
dfad0599 JS |
261 | XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())); |
262 | XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event); | |
f97c9854 JS |
263 | XtDispatchEvent(&event); |
264 | } | |
265 | ||
dfad0599 JS |
266 | m_path = m_fileSelectorAnswer; |
267 | m_fileName = wxFileNameFromPath(m_fileSelectorAnswer); | |
268 | m_dir = wxPathOnly(m_path); | |
f97c9854 | 269 | |
dfad0599 | 270 | if (m_fileName == "") |
4bb6408c | 271 | return wxID_CANCEL; |
dfad0599 JS |
272 | else |
273 | return wxID_OK; | |
4bb6408c JS |
274 | } |
275 | ||
276 | // Generic file load/save dialog | |
277 | static char * | |
278 | wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent) | |
279 | { | |
280 | char *ext = (char *)extension; | |
281 | ||
282 | char prompt[50]; | |
283 | wxString str; | |
284 | if (load) | |
285 | str = "Load %s file"; | |
286 | else | |
287 | str = "Save %s file"; | |
288 | sprintf(prompt, wxGetTranslation(str), what); | |
289 | ||
290 | if (*ext == '.') ext++; | |
291 | char wild[60]; | |
292 | sprintf(wild, "*.%s", ext); | |
293 | ||
294 | return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent); | |
295 | } | |
296 | ||
297 | // Generic file load dialog | |
298 | char * | |
299 | wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent) | |
300 | { | |
301 | return wxDefaultFileSelector(TRUE, what, extension, default_name, parent); | |
302 | } | |
303 | ||
304 | ||
305 | // Generic file save dialog | |
306 | char * | |
307 | wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent) | |
308 | { | |
309 | return wxDefaultFileSelector(FALSE, what, extension, default_name, parent); | |
310 | } | |
311 | ||
312 |