1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
18 #include "wx/dialog.h"
19 #include "wx/filedlg.h"
24 #include <Xm/MwmUtil.h>
26 #include <Xm/BulletinB.h>
29 #include <Xm/DialogS.h>
30 #include <Xm/FileSB.h>
31 #include <Xm/RowColumn.h>
32 #include <Xm/LabelG.h>
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
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
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
)
51 // If there's a default extension specified but no filter, we create a suitable
55 if ( defaultExtension
&& !filter
)
56 filter2
= wxString("*.") + wxString(defaultExtension
) ;
60 wxString defaultDirString
;
62 defaultDirString
= defaultDir
;
64 defaultDirString
= "";
66 wxString defaultFilenameString
;
68 defaultFilenameString
= defaultFileName
;
70 defaultFilenameString
= "";
72 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
74 if ( fileDialog
.ShowModal() == wxID_OK
)
76 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
83 char *wxFileSelectorEx(const char *title
,
84 const char *defaultDir
,
85 const char *defaultFileName
,
86 int* defaultFilterIndex
,
94 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
95 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
97 if ( fileDialog
.ShowModal() == wxID_OK
)
99 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
100 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
107 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
108 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
110 void wxFileSelCancel(Widget fs
, XtPointer client_data
, XmFileSelectionBoxCallbackStruct
*cbs
)
112 wxFileDialog::m_fileSelectorAnswer
= "";
113 wxFileDialog::m_fileSelectorReturned
= TRUE
;
116 void wxFileSelOk(Widget fs
, XtPointer client_data
, XmFileSelectionBoxCallbackStruct
*cbs
)
118 char *filename
= NULL
;
119 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
120 wxFileDialog::m_fileSelectorAnswer
= "";
121 wxFileDialog::m_fileSelectorReturned
= TRUE
;
124 wxFileDialog::m_fileSelectorAnswer
= filename
;
127 wxFileDialog::m_fileSelectorReturned
= TRUE
;
131 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
132 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
133 long style
, const wxPoint
& pos
)
136 m_dialogStyle
= style
;
139 m_fileName
= defaultFileName
;
141 m_wildCard
= wildCard
;
146 int wxFileDialog::ShowModal()
150 static char fileBuf
[512];
151 Widget parentWidget
= (Widget
) 0;
154 parentWidget
= (Widget
) m_parent
->GetTopWidget();
157 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
159 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", NULL
, 0);
160 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
162 Widget shell
= XtParent(fileSel
);
164 if (!m_message
.IsNull())
165 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
167 wxString
entirePath("");
169 if ((m_dir
!= "") && (m_fileName
!= ""))
171 entirePath
= m_dir
+ wxString("/") + m_fileName
;
173 else if ((m_dir
!= "") && (m_fileName
== ""))
175 entirePath
= m_dir
+ wxString("/");
177 else if ((m_dir
== "") && (m_fileName
!= ""))
179 entirePath
= m_fileName
;
182 if (entirePath
!= "")
184 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
185 XmTextSetString(selectionWidget
, (char*) (const char*) entirePath
);
188 if (m_wildCard
!= "")
192 filter
= m_dir
+ wxString("/") + m_wildCard
;
196 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
197 XmTextSetString(filterWidget
, (char*) (const char*) filter
);
198 XmFileSelectionDoSearch(fileSel
, NULL
);
201 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
202 // file selector on Solaris 1.5.1.
205 XmString thePath
= XmStringCreateLtoR ((char*) (const char*) m_dir
,
206 XmSTRING_DEFAULT_CHARSET
);
208 XtVaSetValues (fileSel
,
209 XmNdirectory
, thePath
,
212 XmStringFree(thePath
);
215 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
216 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
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...
224 #if !DEFAULT_FILE_SELECTOR_SIZE
225 int width
= wxFSB_WIDTH
;
226 int height
= wxFSB_HEIGHT
;
227 XtVaSetValues(fileSel
,
230 XmNresizePolicy
, XmRESIZE_NONE
,
234 XtManageChild(fileSel
);
236 m_fileSelectorAnswer
= "";
237 m_fileSelectorReturned
= FALSE
;
241 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
243 while (!m_fileSelectorReturned
)
245 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
247 XtRemoveGrab(XtParent(fileSel
));
249 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
251 // XtDestroyWidget(fileSel);
252 XtUnmapWidget(XtParent(fileSel
));
253 XtDestroyWidget(XtParent(fileSel
));
255 // Now process all events, because otherwise
256 // this might remain on the screen
257 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
258 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
260 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
261 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
262 XtDispatchEvent(&event
);
265 m_path
= m_fileSelectorAnswer
;
266 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
267 m_dir
= wxPathOnly(m_path
);
269 if (m_fileName
== "")
275 // Generic file load/save dialog
277 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
279 char *ext
= (char *)extension
;
284 str
= "Load %s file";
286 str
= "Save %s file";
287 sprintf(prompt
, wxGetTranslation(str
), what
);
289 if (*ext
== '.') ext
++;
291 sprintf(wild
, "*.%s", ext
);
293 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
296 // Generic file load dialog
298 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
300 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
304 // Generic file save dialog
306 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
308 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);