char* -> wxString
[wxWidgets.git] / src / motif / filedlg.cpp
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
23 #include <Xm/Xm.h>
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>
33
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
36 #endif
37
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
46 wxString 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 return fileDialog.GetPath();
77 }
78 else
79 return wxEmptyString;
80 }
81
82 wxString wxFileSelectorEx(const char *title,
83 const char *defaultDir,
84 const char *defaultFileName,
85 int* defaultFilterIndex,
86 const char *filter,
87 int flags,
88 wxWindow* parent,
89 int x,
90 int y)
91
92 {
93 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
94 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
95
96 if ( fileDialog.ShowModal() == wxID_OK )
97 {
98 *defaultFilterIndex = fileDialog.GetFilterIndex();
99 return fileDialog.GetPath());
100 }
101 else
102 return wxEmptyString;
103 }
104
105 wxString wxFileDialog::m_fileSelectorAnswer = "";
106 bool wxFileDialog::m_fileSelectorReturned = FALSE;
107
108 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
109 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
110 {
111 wxFileDialog::m_fileSelectorAnswer = "";
112 wxFileDialog::m_fileSelectorReturned = TRUE;
113 }
114
115 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
116 {
117 char *filename = NULL;
118 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
119 wxFileDialog::m_fileSelectorAnswer = "";
120 wxFileDialog::m_fileSelectorReturned = TRUE;
121 } else {
122 if (filename) {
123 wxFileDialog::m_fileSelectorAnswer = filename;
124 XtFree(filename);
125 }
126 wxFileDialog::m_fileSelectorReturned = TRUE;
127 }
128 }
129
130 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
131 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
132 long style, const wxPoint& pos)
133 {
134 m_message = message;
135 m_dialogStyle = style;
136 m_parent = parent;
137 m_path = "";
138 m_fileName = defaultFileName;
139 m_dir = defaultDir;
140 m_wildCard = wildCard;
141 m_filterIndex = 1;
142 m_pos = pos;
143 }
144
145 int wxFileDialog::ShowModal()
146 {
147 wxBeginBusyCursor();
148
149 // static char fileBuf[512];
150 Widget parentWidget = (Widget) 0;
151 if (m_parent)
152 {
153 parentWidget = (Widget) m_parent->GetTopWidget();
154 }
155 else
156 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
157
158 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", NULL, 0);
159 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
160
161 Widget shell = XtParent(fileSel);
162
163 if (!m_message.IsNull())
164 XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL);
165
166 wxString entirePath("");
167
168 if ((m_dir != "") && (m_fileName != ""))
169 {
170 entirePath = m_dir + wxString("/") + m_fileName;
171 }
172 else if ((m_dir != "") && (m_fileName == ""))
173 {
174 entirePath = m_dir + wxString("/");
175 }
176 else if ((m_dir == "") && (m_fileName != ""))
177 {
178 entirePath = m_fileName;
179 }
180
181 if (entirePath != "")
182 {
183 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
184 XmTextSetString(selectionWidget, (char*) (const char*) entirePath);
185 }
186
187 if (m_wildCard != "")
188 {
189 wxString filter("");
190 if (m_dir != "")
191 filter = m_dir + wxString("/") + m_wildCard;
192 else
193 filter = m_wildCard;
194
195 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
196 XmTextSetString(filterWidget, (char*) (const char*) filter);
197 XmFileSelectionDoSearch(fileSel, NULL);
198 }
199
200 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
201 // file selector on Solaris 1.5.1.
202 if ( m_dir != "" )
203 {
204 XmString thePath = XmStringCreateLtoR ((char*) (const char*) m_dir,
205 XmSTRING_DEFAULT_CHARSET);
206
207 XtVaSetValues (fileSel,
208 XmNdirectory, thePath,
209 NULL);
210
211 XmStringFree(thePath);
212 }
213
214 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
215 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
216
217 //#if XmVersion > 1000
218 // I'm not sure about what you mean with XmVersion.
219 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
220 // (Motif1.1.4 ==> XmVersion 1100 )
221 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
222 //
223 #if !DEFAULT_FILE_SELECTOR_SIZE
224 int width = wxFSB_WIDTH;
225 int height = wxFSB_HEIGHT;
226 XtVaSetValues(fileSel,
227 XmNwidth, width,
228 XmNheight, height,
229 XmNresizePolicy, XmRESIZE_NONE,
230 NULL);
231 #endif
232
233 XtManageChild(fileSel);
234
235 m_fileSelectorAnswer = "";
236 m_fileSelectorReturned = FALSE;
237
238 wxEndBusyCursor();
239
240 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
241 XEvent event;
242 while (!m_fileSelectorReturned)
243 {
244 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
245 }
246 XtRemoveGrab(XtParent(fileSel));
247
248 XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
249
250 // XtDestroyWidget(fileSel);
251 XtUnmapWidget(XtParent(fileSel));
252 XtDestroyWidget(XtParent(fileSel));
253
254 // Now process all events, because otherwise
255 // this might remain on the screen
256 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
257 while (XtAppPending((XtAppContext) wxTheApp->GetAppContext()))
258 {
259 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
260 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
261 XtDispatchEvent(&event);
262 }
263
264 m_path = m_fileSelectorAnswer;
265 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
266 m_dir = wxPathOnly(m_path);
267
268 if (m_fileName == "")
269 return wxID_CANCEL;
270 else
271 return wxID_OK;
272 }
273
274 // Generic file load/save dialog
275 static wxString
276 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
277 {
278 char *ext = (char *)extension;
279
280 char prompt[50];
281 wxString str;
282 if (load)
283 str = "Load %s file";
284 else
285 str = "Save %s file";
286 sprintf(prompt, wxGetTranslation(str), what);
287
288 if (*ext == '.') ext++;
289 char wild[60];
290 sprintf(wild, "*.%s", ext);
291
292 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
293 }
294
295 // Generic file load dialog
296 wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
297 {
298 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
299 }
300
301
302 // Generic file save dialog
303 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
304 {
305 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
306 }
307
308