1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/filedlg.cpp
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
21 #include "wx/filedlg.h"
27 #include "wx/settings.h"
30 #include "wx/tokenzr.h"
31 #include "wx/stockitem.h"
34 #pragma message disable nosimpint
37 #include <Xm/MwmUtil.h>
39 #include <Xm/BulletinB.h>
42 #include <Xm/DialogS.h>
43 #include <Xm/FileSB.h>
44 #include <Xm/RowColumn.h>
45 #include <Xm/LabelG.h>
47 #pragma message enable nosimpint
50 #include "wx/motif/private.h"
52 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
54 #define DEFAULT_FILE_SELECTOR_SIZE 0
55 // Let Motif defines the size of File
56 // Selector Box (if 1), or fix it to
57 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
58 #define wxFSB_WIDTH 600
59 #define wxFSB_HEIGHT 500
62 wxString
wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
63 bool wxFileDialog::m_fileSelectorReturned
= false;
65 static void wxFileSelClose(Widget
WXUNUSED(w
),
66 void* WXUNUSED(client_data
),
67 XmAnyCallbackStruct
*WXUNUSED(call_data
))
69 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
70 wxFileDialog::m_fileSelectorReturned
= true;
73 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
74 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
76 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
77 wxFileDialog::m_fileSelectorReturned
= true;
80 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
82 char *filename
= NULL
;
83 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
84 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
85 wxFileDialog::m_fileSelectorReturned
= true;
88 wxFileDialog::m_fileSelectorAnswer
= filename
;
91 wxFileDialog::m_fileSelectorReturned
= true;
95 static wxString
ParseWildCard( const wxString
& wild
)
98 static const wxChar
* msg
=
99 _T("Motif file dialog does not understand this ")
100 _T("wildcard syntax");
103 wxArrayString wildDescriptions
, wildFilters
;
104 const size_t count
= wxParseCommonDialogsFilter(wild
,
107 wxCHECK_MSG( count
, _T("*.*"), wxT("wxFileDialog: bad wildcard string") );
108 wxCHECK_MSG( count
== 1, _T("*.*"), msg
);
110 // check for *.txt;*.rtf
111 wxStringTokenizer
tok2( wildFilters
[0], _T(";") );
112 wxString wildcard
= tok2
.GetNextToken();
114 wxCHECK_MSG( tok2
.CountTokens() <= 1, wildcard
, msg
);
118 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
119 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
120 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
121 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
126 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
128 wxDoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
130 // Change colour of the scrolled areas of the listboxes
131 Widget listParent
= XtParent (widget
);
133 wxDoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, true);
136 Widget hsb
= (Widget
) 0;
137 Widget vsb
= (Widget
) 0;
138 XtVaGetValues (listParent
,
139 XmNhorizontalScrollBar
, &hsb
,
140 XmNverticalScrollBar
, &vsb
,
143 /* TODO: should scrollbars be affected? Should probably have separate
144 * function to change them (by default, taken from wxSystemSettings)
146 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
147 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
148 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
152 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
156 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
160 int wxFileDialog::ShowModal()
164 // static char fileBuf[512];
165 Widget parentWidget
= (Widget
) 0;
167 parentWidget
= (Widget
) m_parent
->GetTopWidget();
169 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
170 // prepare the arg list
171 Display
* dpy
= XtDisplay(parentWidget
);
175 wxComputeColours (dpy
, & m_backgroundColour
, (wxColour
*) NULL
);
177 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
178 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
179 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
180 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
182 wxFont font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
184 #if __WXMOTIF20__ && !__WXLESSTIF__
185 XtSetArg(args
[ac
], XmNbuttonRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
186 XtSetArg(args
[ac
], XmNlabelRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
187 XtSetArg(args
[ac
], XmNtextRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
189 XtSetArg(args
[ac
], XmNbuttonFontList
, font
.GetFontTypeC(dpy
)); ac
++;
190 XtSetArg(args
[ac
], XmNlabelFontList
, font
.GetFontTypeC(dpy
)); ac
++;
191 XtSetArg(args
[ac
], XmNtextFontList
, font
.GetFontTypeC(dpy
)); ac
++;
194 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
,
195 wxMOTIF_STR("file_selector"),
197 #define wxFSChild( name ) \
198 XmFileSelectionBoxGetChild(fileSel, name)
200 XtUnmanageChild(wxFSChild(XmDIALOG_HELP_BUTTON
));
202 Widget filterWidget
= wxFSChild(XmDIALOG_FILTER_TEXT
);
203 Widget selectionWidget
= wxFSChild(XmDIALOG_TEXT
);
204 Widget dirListWidget
= wxFSChild(XmDIALOG_DIR_LIST
);
205 Widget fileListWidget
= wxFSChild(XmDIALOG_LIST
);
207 // for changing labels
208 Widget okWidget
= wxFSChild(XmDIALOG_OK_BUTTON
);
209 Widget applyWidget
= wxFSChild(XmDIALOG_APPLY_BUTTON
);
210 Widget cancelWidget
= wxFSChild(XmDIALOG_CANCEL_BUTTON
);
211 Widget dirlistLabel
= wxFSChild(XmDIALOG_DIR_LIST_LABEL
);
212 Widget filterLabel
= wxFSChild(XmDIALOG_FILTER_LABEL
);
213 Widget listLabel
= wxFSChild(XmDIALOG_LIST_LABEL
);
214 Widget selectionLabel
= wxFSChild(XmDIALOG_SELECTION_LABEL
);
219 wxXmString
btnOK( wxGetStockLabel( wxID_OK
, false ) ),
220 btnCancel( wxGetStockLabel( wxID_CANCEL
, false ) ),
221 btnFilter( _("Filter") ), lblFilter( _("Filter") ),
222 lblDirectories( _("Directories") ),
223 lblFiles( _("Files" ) ), lblSelection( _("Selection") );
225 XtVaSetValues( okWidget
, XmNlabelString
, btnOK(), NULL
);
226 XtVaSetValues( applyWidget
, XmNlabelString
, btnFilter(), NULL
);
227 XtVaSetValues( cancelWidget
, XmNlabelString
, btnCancel(), NULL
);
228 XtVaSetValues( dirlistLabel
, XmNlabelString
, lblDirectories(), NULL
);
229 XtVaSetValues( filterLabel
, XmNlabelString
, lblFilter(), NULL
);
230 XtVaSetValues( listLabel
, XmNlabelString
, lblFiles(), NULL
);
231 XtVaSetValues( selectionLabel
, XmNlabelString
, lblSelection(), NULL
);
233 Widget shell
= XtParent(fileSel
);
235 if (!m_message
.IsNull())
237 XmNtitle
, wxConstCast(m_message
.c_str(), char),
240 if (!m_wildCard
.empty())
242 // return something understandable by Motif
243 wxString wildCard
= ParseWildCard( m_wildCard
);
246 filter
= m_dir
+ wxString("/") + wildCard
;
250 XmTextSetString(filterWidget
, wxConstCast(filter
.c_str(), char));
251 XmFileSelectionDoSearch(fileSel
, NULL
);
254 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
255 // file selector on Solaris 1.5.1.
256 if ( !m_dir
.empty() )
258 wxXmString
thePath( m_dir
);
260 XtVaSetValues (fileSel
,
261 XmNdirectory
, thePath(),
269 entirePath
= m_dir
+ wxString("/") + m_fileName
;
273 entirePath
= m_fileName
;
276 if (!entirePath
.empty())
278 XmTextSetString(selectionWidget
,
279 wxConstCast(entirePath
.c_str(), char));
282 XtAddCallback(fileSel
, XmNcancelCallback
,
283 (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
284 XtAddCallback(fileSel
, XmNokCallback
,
285 (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
286 XtAddCallback(fileSel
, XmNunmapCallback
,
287 (XtCallbackProc
)wxFileSelClose
, (XtPointer
)this);
289 //#if XmVersion > 1000
290 // I'm not sure about what you mean with XmVersion.
291 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
292 // (Motif1.1.4 ==> XmVersion 1100 )
293 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile.
295 #if !DEFAULT_FILE_SELECTOR_SIZE
296 int width
= wxFSB_WIDTH
;
297 int height
= wxFSB_HEIGHT
;
298 XtVaSetValues(fileSel
,
301 XmNresizePolicy
, XmRESIZE_NONE
,
304 wxDoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
305 wxDoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
307 wxChangeListBoxColours(this, dirListWidget
);
308 wxChangeListBoxColours(this, fileListWidget
);
310 XtManageChild(fileSel
);
312 m_fileSelectorAnswer
= wxEmptyString
;
313 m_fileSelectorReturned
= false;
317 XtAddGrab(XtParent(fileSel
), True
, False
);
318 XtAppContext context
= (XtAppContext
) wxTheApp
->GetAppContext();
320 while (!m_fileSelectorReturned
)
322 XtAppNextEvent(context
, &event
);
323 XtDispatchEvent(&event
);
325 XtRemoveGrab(XtParent(fileSel
));
327 XtUnmapWidget(XtParent(fileSel
));
328 XtDestroyWidget(XtParent(fileSel
));
330 // Now process all events, because otherwise
331 // this might remain on the screen
332 wxFlushEvents(XtDisplay(fileSel
));
334 m_path
= m_fileSelectorAnswer
;
335 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
336 m_dir
= wxPathOnly(m_path
);
338 if (m_fileName
.empty())