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