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