1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtDisplay XTDISPLAY
17 #define XtParent XTPARENT
18 #define XtWindow XTWINDOW
23 #include "wx/filedlg.h"
26 #include "wx/settings.h"
27 #include "wx/tokenzr.h"
28 #include "wx/stockitem.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
, wxFileDialogBase
)
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
wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
60 bool wxFileDialog::m_fileSelectorReturned
= false;
62 static void wxFileSelClose(Widget
WXUNUSED(w
),
63 void* WXUNUSED(client_data
),
64 XmAnyCallbackStruct
*WXUNUSED(call_data
))
66 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
67 wxFileDialog::m_fileSelectorReturned
= true;
70 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
71 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
73 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
74 wxFileDialog::m_fileSelectorReturned
= true;
77 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
79 char *filename
= NULL
;
80 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
81 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
82 wxFileDialog::m_fileSelectorReturned
= true;
85 wxFileDialog::m_fileSelectorAnswer
= filename
;
88 wxFileDialog::m_fileSelectorReturned
= true;
92 static wxString
ParseWildCard( const wxString
& wild
)
95 static const wxChar
* msg
=
96 _T("Motif file dialog does not understand this ")
97 _T("wildcard syntax");
100 wxArrayString wildDescriptions
, wildFilters
;
101 const size_t count
= wxParseCommonDialogsFilter(wild
,
104 wxCHECK_MSG( count
, _T("*.*"), wxT("wxFileDialog: bad wildcard string") );
105 wxCHECK_MSG( count
== 1, _T("*.*"), msg
);
107 // check for *.txt;*.rtf
108 wxStringTokenizer
tok2( wildFilters
[0], _T(";") );
109 wxString wildcard
= tok2
.GetNextToken();
111 wxCHECK_MSG( tok2
.CountTokens() <= 1, wildcard
, msg
);
115 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
116 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
117 long style
, const wxPoint
& pos
)
118 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
123 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
125 wxDoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
127 // Change colour of the scrolled areas of the listboxes
128 Widget listParent
= XtParent (widget
);
130 wxDoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, true);
133 Widget hsb
= (Widget
) 0;
134 Widget vsb
= (Widget
) 0;
135 XtVaGetValues (listParent
,
136 XmNhorizontalScrollBar
, &hsb
,
137 XmNverticalScrollBar
, &vsb
,
140 /* TODO: should scrollbars be affected? Should probably have separate
141 * function to change them (by default, taken from wxSystemSettings)
143 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
144 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
145 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
149 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
153 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
157 int wxFileDialog::ShowModal()
161 // static char fileBuf[512];
162 Widget parentWidget
= (Widget
) 0;
164 parentWidget
= (Widget
) m_parent
->GetTopWidget();
166 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
167 // prepare the arg list
168 Display
* dpy
= XtDisplay(parentWidget
);
172 wxComputeColours (dpy
, & m_backgroundColour
, (wxColour
*) NULL
);
174 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
175 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
176 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
177 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
179 wxFont font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
181 #if __WXMOTIF20__ && !__WXLESSTIF__
182 XtSetArg(args
[ac
], XmNbuttonRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
183 XtSetArg(args
[ac
], XmNlabelRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
184 XtSetArg(args
[ac
], XmNtextRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
186 XtSetArg(args
[ac
], XmNbuttonFontList
, font
.GetFontTypeC(dpy
)); ac
++;
187 XtSetArg(args
[ac
], XmNlabelFontList
, font
.GetFontTypeC(dpy
)); ac
++;
188 XtSetArg(args
[ac
], XmNtextFontList
, font
.GetFontTypeC(dpy
)); ac
++;
191 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
,
192 wxMOTIF_STR("file_selector"),
194 #define wxFSChild( name ) \
195 XmFileSelectionBoxGetChild(fileSel, name)
197 XtUnmanageChild(wxFSChild(XmDIALOG_HELP_BUTTON
));
199 Widget filterWidget
= wxFSChild(XmDIALOG_FILTER_TEXT
);
200 Widget selectionWidget
= wxFSChild(XmDIALOG_TEXT
);
201 Widget dirListWidget
= wxFSChild(XmDIALOG_DIR_LIST
);
202 Widget fileListWidget
= wxFSChild(XmDIALOG_LIST
);
204 // for changing labels
205 Widget okWidget
= wxFSChild(XmDIALOG_OK_BUTTON
);
206 Widget applyWidget
= wxFSChild(XmDIALOG_APPLY_BUTTON
);
207 Widget cancelWidget
= wxFSChild(XmDIALOG_CANCEL_BUTTON
);
208 Widget dirlistLabel
= wxFSChild(XmDIALOG_DIR_LIST_LABEL
);
209 Widget filterLabel
= wxFSChild(XmDIALOG_FILTER_LABEL
);
210 Widget listLabel
= wxFSChild(XmDIALOG_LIST_LABEL
);
211 Widget selectionLabel
= wxFSChild(XmDIALOG_SELECTION_LABEL
);
216 wxXmString
btnOK( wxGetStockLabel( wxID_OK
, false ) ),
217 btnCancel( wxGetStockLabel( wxID_CANCEL
, false ) ),
218 btnFilter( _("Filter") ), lblFilter( _("Filter") ),
219 lblDirectories( _("Directories") ),
220 lblFiles( _("Files" ) ), lblSelection( _("Selection") );
222 XtVaSetValues( okWidget
, XmNlabelString
, btnOK(), NULL
);
223 XtVaSetValues( applyWidget
, XmNlabelString
, btnFilter(), NULL
);
224 XtVaSetValues( cancelWidget
, XmNlabelString
, btnCancel(), NULL
);
225 XtVaSetValues( dirlistLabel
, XmNlabelString
, lblDirectories(), NULL
);
226 XtVaSetValues( filterLabel
, XmNlabelString
, lblFilter(), NULL
);
227 XtVaSetValues( listLabel
, XmNlabelString
, lblFiles(), NULL
);
228 XtVaSetValues( selectionLabel
, XmNlabelString
, lblSelection(), NULL
);
230 Widget shell
= XtParent(fileSel
);
232 if (!m_message
.IsNull())
234 XmNtitle
, wxConstCast(m_message
.c_str(), char),
237 if (!m_wildCard
.empty())
239 // return something understandable by Motif
240 wxString wildCard
= ParseWildCard( m_wildCard
);
243 filter
= m_dir
+ wxString("/") + wildCard
;
247 XmTextSetString(filterWidget
, wxConstCast(filter
.c_str(), char));
248 XmFileSelectionDoSearch(fileSel
, NULL
);
251 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
252 // file selector on Solaris 1.5.1.
253 if ( !m_dir
.empty() )
255 wxXmString
thePath( m_dir
);
257 XtVaSetValues (fileSel
,
258 XmNdirectory
, thePath(),
266 entirePath
= m_dir
+ wxString("/") + m_fileName
;
270 entirePath
= m_fileName
;
273 if (!entirePath
.empty())
275 XmTextSetString(selectionWidget
,
276 wxConstCast(entirePath
.c_str(), char));
279 XtAddCallback(fileSel
, XmNcancelCallback
,
280 (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
281 XtAddCallback(fileSel
, XmNokCallback
,
282 (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
283 XtAddCallback(fileSel
, XmNunmapCallback
,
284 (XtCallbackProc
)wxFileSelClose
, (XtPointer
)this);
286 //#if XmVersion > 1000
287 // I'm not sure about what you mean with XmVersion.
288 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
289 // (Motif1.1.4 ==> XmVersion 1100 )
290 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile.
292 #if !DEFAULT_FILE_SELECTOR_SIZE
293 int width
= wxFSB_WIDTH
;
294 int height
= wxFSB_HEIGHT
;
295 XtVaSetValues(fileSel
,
298 XmNresizePolicy
, XmRESIZE_NONE
,
301 wxDoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
302 wxDoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
304 wxChangeListBoxColours(this, dirListWidget
);
305 wxChangeListBoxColours(this, fileListWidget
);
307 XtManageChild(fileSel
);
309 m_fileSelectorAnswer
= wxEmptyString
;
310 m_fileSelectorReturned
= false;
314 XtAddGrab(XtParent(fileSel
), True
, False
);
315 XtAppContext context
= (XtAppContext
) wxTheApp
->GetAppContext();
317 while (!m_fileSelectorReturned
)
319 XtAppNextEvent(context
, &event
);
320 XtDispatchEvent(&event
);
322 XtRemoveGrab(XtParent(fileSel
));
324 XtUnmapWidget(XtParent(fileSel
));
325 XtDestroyWidget(XtParent(fileSel
));
327 // Now process all events, because otherwise
328 // this might remain on the screen
329 wxFlushEvents(XtDisplay(fileSel
));
331 m_path
= m_fileSelectorAnswer
;
332 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
333 m_dir
= wxPathOnly(m_path
);
335 if (m_fileName
.empty())