1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
19 #define XtWindow XTWINDOW
24 #include "wx/dialog.h"
25 #include "wx/filedlg.h"
28 #include "wx/settings.h"
31 #pragma message disable nosimpint
34 #include <Xm/MwmUtil.h>
36 #include <Xm/BulletinB.h>
39 #include <Xm/DialogS.h>
40 #include <Xm/FileSB.h>
41 #include <Xm/RowColumn.h>
42 #include <Xm/LabelG.h>
44 #pragma message enable nosimpint
47 #include "wx/motif/private.h"
49 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
51 #define DEFAULT_FILE_SELECTOR_SIZE 0
52 // Let Motif defines the size of File
53 // Selector Box (if 1), or fix it to
54 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
55 #define wxFSB_WIDTH 600
56 #define wxFSB_HEIGHT 500
59 wxString
wxFileSelector(const char *title
,
60 const char *defaultDir
, const char *defaultFileName
,
61 const char *defaultExtension
, const char *filter
, int flags
,
62 wxWindow
*parent
, int x
, int y
)
64 // If there's a default extension specified but no filter, we create a suitable
68 if ( defaultExtension
&& !filter
)
69 filter2
= wxString("*.") + wxString(defaultExtension
) ;
73 wxString defaultDirString
;
75 defaultDirString
= defaultDir
;
77 defaultDirString
= "";
79 wxString defaultFilenameString
;
81 defaultFilenameString
= defaultFileName
;
83 defaultFilenameString
= "";
85 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
87 if ( fileDialog
.ShowModal() == wxID_OK
)
89 return fileDialog
.GetPath();
95 wxString
wxFileSelectorEx(const char *title
,
96 const char *defaultDir
,
97 const char *defaultFileName
,
98 int* defaultFilterIndex
,
106 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
107 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
109 if ( fileDialog
.ShowModal() == wxID_OK
)
111 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
112 return fileDialog
.GetPath();
115 return wxEmptyString
;
118 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
119 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
121 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
122 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
124 wxFileDialog::m_fileSelectorAnswer
= "";
125 wxFileDialog::m_fileSelectorReturned
= TRUE
;
128 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
130 char *filename
= NULL
;
131 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
132 wxFileDialog::m_fileSelectorAnswer
= "";
133 wxFileDialog::m_fileSelectorReturned
= TRUE
;
136 wxFileDialog::m_fileSelectorAnswer
= filename
;
139 wxFileDialog::m_fileSelectorReturned
= TRUE
;
143 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
144 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
145 long style
, const wxPoint
& pos
)
148 m_dialogStyle
= style
;
151 m_fileName
= defaultFileName
;
153 m_wildCard
= wildCard
;
158 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
160 wxWindow::DoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
162 // Change colour of the scrolled areas of the listboxes
163 Widget listParent
= XtParent (widget
);
164 wxWindow::DoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
166 Widget hsb
= (Widget
) 0;
167 Widget vsb
= (Widget
) 0;
168 XtVaGetValues (listParent
,
169 XmNhorizontalScrollBar
, &hsb
,
170 XmNverticalScrollBar
, &vsb
,
173 /* TODO: should scrollbars be affected? Should probably have separate
174 * function to change them (by default, taken from wxSystemSettings)
176 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
177 wxWindow::DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
178 wxWindow::DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
182 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
186 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
190 int wxFileDialog::ShowModal()
194 // static char fileBuf[512];
195 Widget parentWidget
= (Widget
) 0;
198 parentWidget
= (Widget
) m_parent
->GetTopWidget();
201 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
202 // prepare the arg list
206 wxComputeColours (XtDisplay(parentWidget
), & m_backgroundColour
,
209 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
210 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
211 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
212 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
215 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", args
, ac
);
216 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
218 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
219 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
220 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
221 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
223 // code using these vars disabled
225 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
226 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
227 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
231 Widget shell
= XtParent(fileSel
);
233 if (!m_message
.IsNull())
234 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
236 wxString
entirePath("");
238 if ((m_dir
!= "") && (m_fileName
!= ""))
240 entirePath
= m_dir
+ wxString("/") + m_fileName
;
242 else if ((m_dir
!= "") && (m_fileName
== ""))
244 entirePath
= m_dir
+ wxString("/");
246 else if ((m_dir
== "") && (m_fileName
!= ""))
248 entirePath
= m_fileName
;
251 if (entirePath
!= "")
253 XmTextSetString(selectionWidget
, (char*) (const char*) entirePath
);
256 if (m_wildCard
!= "")
260 filter
= m_dir
+ wxString("/") + m_wildCard
;
264 XmTextSetString(filterWidget
, (char*) (const char*) filter
);
265 XmFileSelectionDoSearch(fileSel
, NULL
);
268 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
269 // file selector on Solaris 1.5.1.
272 XmString thePath
= XmStringCreateLtoR ((char*) (const char*) m_dir
,
273 XmSTRING_DEFAULT_CHARSET
);
275 XtVaSetValues (fileSel
,
276 XmNdirectory
, thePath
,
279 XmStringFree(thePath
);
282 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
283 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
285 //#if XmVersion > 1000
286 // I'm not sure about what you mean with XmVersion.
287 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
288 // (Motif1.1.4 ==> XmVersion 1100 )
289 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
291 #if !DEFAULT_FILE_SELECTOR_SIZE
292 int width
= wxFSB_WIDTH
;
293 int height
= wxFSB_HEIGHT
;
294 XtVaSetValues(fileSel
,
297 XmNresizePolicy
, XmRESIZE_NONE
,
300 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
301 DoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
302 DoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
304 wxChangeListBoxColours(this, dirListWidget
);
305 wxChangeListBoxColours(this, fileListWidget
);
307 XtManageChild(fileSel
);
309 m_fileSelectorAnswer
= "";
310 m_fileSelectorReturned
= FALSE
;
314 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
316 while (!m_fileSelectorReturned
)
318 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
320 XtRemoveGrab(XtParent(fileSel
));
322 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
324 // XtDestroyWidget(fileSel);
325 XtUnmapWidget(XtParent(fileSel
));
326 XtDestroyWidget(XtParent(fileSel
));
328 // Now process all events, because otherwise
329 // this might remain on the screen
330 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
331 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
333 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
334 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
335 XtDispatchEvent(&event
);
338 m_path
= m_fileSelectorAnswer
;
339 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
340 m_dir
= wxPathOnly(m_path
);
342 if (m_fileName
== "")
348 // Generic file load/save dialog
350 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
352 char *ext
= (char *)extension
;
357 str
= _("Load %s file");
359 str
= _("Save %s file");
360 prompt
.Printf(str
, what
);
365 wild
.Printf("*.%s", ext
);
367 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
370 // Generic file load dialog
371 wxString
wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
373 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
377 // Generic file save dialog
378 wxString
wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
380 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);