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"
22 #include "wx/settings.h"
25 #include <Xm/MwmUtil.h>
27 #include <Xm/BulletinB.h>
30 #include <Xm/DialogS.h>
31 #include <Xm/FileSB.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/LabelG.h>
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
39 #define DEFAULT_FILE_SELECTOR_SIZE 0
40 // Let Motif defines the size of File
41 // Selector Box (if 1), or fix it to
42 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
43 #define wxFSB_WIDTH 600
44 #define wxFSB_HEIGHT 500
47 wxString
wxFileSelector(const char *title
,
48 const char *defaultDir
, const char *defaultFileName
,
49 const char *defaultExtension
, const char *filter
, int flags
,
50 wxWindow
*parent
, int x
, int y
)
52 // If there's a default extension specified but no filter, we create a suitable
56 if ( defaultExtension
&& !filter
)
57 filter2
= wxString("*.") + wxString(defaultExtension
) ;
61 wxString defaultDirString
;
63 defaultDirString
= defaultDir
;
65 defaultDirString
= "";
67 wxString defaultFilenameString
;
69 defaultFilenameString
= defaultFileName
;
71 defaultFilenameString
= "";
73 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
75 if ( fileDialog
.ShowModal() == wxID_OK
)
77 return fileDialog
.GetPath();
83 wxString
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 return fileDialog
.GetPath();
103 return wxEmptyString
;
106 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
107 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
109 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
110 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
112 wxFileDialog::m_fileSelectorAnswer
= "";
113 wxFileDialog::m_fileSelectorReturned
= TRUE
;
116 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(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 static void wxChangeListBoxColours(wxWindow
* win
, Widget widget
)
148 wxWindow::DoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
150 // Change colour of the scrolled areas of the listboxes
151 Widget listParent
= XtParent (widget
);
152 wxWindow::DoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
154 Widget hsb
= (Widget
) 0;
155 Widget vsb
= (Widget
) 0;
156 XtVaGetValues (listParent
,
157 XmNhorizontalScrollBar
, &hsb
,
158 XmNverticalScrollBar
, &vsb
,
161 /* TODO: should scrollbars be affected? Should probably have separate
162 * function to change them (by default, taken from wxSystemSettings)
164 wxColour backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
165 wxWindow::DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
166 wxWindow::DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
170 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
174 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
178 int wxFileDialog::ShowModal()
182 // static char fileBuf[512];
183 Widget parentWidget
= (Widget
) 0;
186 parentWidget
= (Widget
) m_parent
->GetTopWidget();
189 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
191 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", NULL
, 0);
192 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
194 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
195 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
196 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
197 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
199 // code using these vars disabled
201 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
202 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
203 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
207 Widget shell
= XtParent(fileSel
);
209 if (!m_message
.IsNull())
210 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
212 wxString
entirePath("");
214 if ((m_dir
!= "") && (m_fileName
!= ""))
216 entirePath
= m_dir
+ wxString("/") + m_fileName
;
218 else if ((m_dir
!= "") && (m_fileName
== ""))
220 entirePath
= m_dir
+ wxString("/");
222 else if ((m_dir
== "") && (m_fileName
!= ""))
224 entirePath
= m_fileName
;
227 if (entirePath
!= "")
229 XmTextSetString(selectionWidget
, (char*) (const char*) entirePath
);
232 if (m_wildCard
!= "")
236 filter
= m_dir
+ wxString("/") + m_wildCard
;
240 XmTextSetString(filterWidget
, (char*) (const char*) filter
);
241 XmFileSelectionDoSearch(fileSel
, NULL
);
244 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
245 // file selector on Solaris 1.5.1.
248 XmString thePath
= XmStringCreateLtoR ((char*) (const char*) m_dir
,
249 XmSTRING_DEFAULT_CHARSET
);
251 XtVaSetValues (fileSel
,
252 XmNdirectory
, thePath
,
255 XmStringFree(thePath
);
258 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
259 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
261 //#if XmVersion > 1000
262 // I'm not sure about what you mean with XmVersion.
263 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
264 // (Motif1.1.4 ==> XmVersion 1100 )
265 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
267 #if !DEFAULT_FILE_SELECTOR_SIZE
268 int width
= wxFSB_WIDTH
;
269 int height
= wxFSB_HEIGHT
;
270 XtVaSetValues(fileSel
,
273 XmNresizePolicy
, XmRESIZE_NONE
,
276 DoChangeBackgroundColour((WXWidget
) fileSel
, m_backgroundColour
);
277 DoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
278 DoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
280 // apparently, this provokes a crash
282 DoChangeBackgroundColour((WXWidget
) okWidget
, m_backgroundColour
, TRUE
);
283 DoChangeBackgroundColour((WXWidget
) cancelWidget
, m_backgroundColour
, TRUE
);
284 DoChangeBackgroundColour((WXWidget
) applyWidget
, m_backgroundColour
, TRUE
);
287 wxChangeListBoxColours(this, dirListWidget
);
288 wxChangeListBoxColours(this, fileListWidget
);
290 XtManageChild(fileSel
);
292 m_fileSelectorAnswer
= "";
293 m_fileSelectorReturned
= FALSE
;
297 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
299 while (!m_fileSelectorReturned
)
301 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
303 XtRemoveGrab(XtParent(fileSel
));
305 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
307 // XtDestroyWidget(fileSel);
308 XtUnmapWidget(XtParent(fileSel
));
309 XtDestroyWidget(XtParent(fileSel
));
311 // Now process all events, because otherwise
312 // this might remain on the screen
313 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
314 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
316 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
317 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
318 XtDispatchEvent(&event
);
321 m_path
= m_fileSelectorAnswer
;
322 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
323 m_dir
= wxPathOnly(m_path
);
325 if (m_fileName
== "")
331 // Generic file load/save dialog
333 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
335 char *ext
= (char *)extension
;
340 str
= _("Load %s file");
342 str
= _("Save %s file");
343 prompt
.Printf(str
, what
);
348 wild
.Printf("*.%s", ext
);
350 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
353 // Generic file load dialog
354 wxString
wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
356 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
360 // Generic file save dialog
361 wxString
wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
363 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);