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"
15 #include "wx/filedlg.h"
21 #include "wx/settings.h"
24 #include "wx/tokenzr.h"
25 #include "wx/stockitem.h"
28 #pragma message disable nosimpint
31 #include <Xm/MwmUtil.h>
33 #include <Xm/BulletinB.h>
36 #include <Xm/DialogS.h>
37 #include <Xm/FileSB.h>
38 #include <Xm/RowColumn.h>
39 #include <Xm/LabelG.h>
41 #pragma message enable nosimpint
44 #include "wx/motif/private.h"
46 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
48 #define DEFAULT_FILE_SELECTOR_SIZE 0
49 // Let Motif defines the size of File
50 // Selector Box (if 1), or fix it to
51 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
52 #define wxFSB_WIDTH 600
53 #define wxFSB_HEIGHT 500
56 wxString
wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
57 bool wxFileDialog::m_fileSelectorReturned
= false;
59 static void wxFileSelClose(Widget
WXUNUSED(w
),
60 void* WXUNUSED(client_data
),
61 XmAnyCallbackStruct
*WXUNUSED(call_data
))
63 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
64 wxFileDialog::m_fileSelectorReturned
= true;
67 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
68 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
70 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
71 wxFileDialog::m_fileSelectorReturned
= true;
74 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
76 char *filename
= NULL
;
77 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
78 wxFileDialog::m_fileSelectorAnswer
= wxEmptyString
;
79 wxFileDialog::m_fileSelectorReturned
= true;
82 wxFileDialog::m_fileSelectorAnswer
= filename
;
85 wxFileDialog::m_fileSelectorReturned
= true;
89 static wxString
ParseWildCard( const wxString
& wild
)
92 static const char *msg
=
93 "Motif file dialog does not understand this wildcard syntax";
96 wxArrayString wildDescriptions
, wildFilters
;
97 const size_t count
= wxParseCommonDialogsFilter(wild
,
100 wxCHECK_MSG( count
, _T("*.*"), wxT("wxFileDialog: bad wildcard string") );
101 wxCHECK_MSG( count
== 1, _T("*.*"), msg
);
103 // check for *.txt;*.rtf
104 wxStringTokenizer
tok2( wildFilters
[0], _T(";") );
105 wxString wildcard
= tok2
.GetNextToken();
107 wxCHECK_MSG( tok2
.CountTokens() <= 1, wildcard
, msg
);
111 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
112 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
113 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
114 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
119 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
121 wxDoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
123 // Change colour of the scrolled areas of the listboxes
124 Widget listParent
= XtParent (widget
);
126 wxDoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, true);
129 Widget hsb
= (Widget
) 0;
130 Widget vsb
= (Widget
) 0;
131 XtVaGetValues (listParent
,
132 XmNhorizontalScrollBar
, &hsb
,
133 XmNverticalScrollBar
, &vsb
,
136 /* TODO: should scrollbars be affected? Should probably have separate
137 * function to change them (by default, taken from wxSystemSettings)
139 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
140 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
141 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
145 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
149 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
153 int wxFileDialog::ShowModal()
157 // static char fileBuf[512];
158 Widget parentWidget
= (Widget
) 0;
160 parentWidget
= (Widget
) m_parent
->GetTopWidget();
162 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
163 // prepare the arg list
164 Display
* dpy
= XtDisplay(parentWidget
);
168 if (m_backgroundColour
.Ok())
170 wxComputeColours (dpy
, & m_backgroundColour
, NULL
);
172 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
173 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
174 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
175 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
178 wxFont font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
180 #if __WXMOTIF20__ && !__WXLESSTIF__
181 XtSetArg(args
[ac
], XmNbuttonRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
182 XtSetArg(args
[ac
], XmNlabelRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
183 XtSetArg(args
[ac
], XmNtextRenderTable
, font
.GetFontTypeC(dpy
)); ac
++;
185 XtSetArg(args
[ac
], XmNbuttonFontList
, font
.GetFontTypeC(dpy
)); ac
++;
186 XtSetArg(args
[ac
], XmNlabelFontList
, font
.GetFontTypeC(dpy
)); ac
++;
187 XtSetArg(args
[ac
], XmNtextFontList
, font
.GetFontTypeC(dpy
)); ac
++;
190 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
,
191 wxMOTIF_STR("file_selector"),
193 #define wxFSChild( name ) \
194 XmFileSelectionBoxGetChild(fileSel, name)
196 XtUnmanageChild(wxFSChild(XmDIALOG_HELP_BUTTON
));
198 Widget filterWidget
= wxFSChild(XmDIALOG_FILTER_TEXT
);
199 Widget selectionWidget
= wxFSChild(XmDIALOG_TEXT
);
200 Widget dirListWidget
= wxFSChild(XmDIALOG_DIR_LIST
);
201 Widget fileListWidget
= wxFSChild(XmDIALOG_LIST
);
203 // for changing labels
204 Widget okWidget
= wxFSChild(XmDIALOG_OK_BUTTON
);
205 Widget applyWidget
= wxFSChild(XmDIALOG_APPLY_BUTTON
);
206 Widget cancelWidget
= wxFSChild(XmDIALOG_CANCEL_BUTTON
);
207 Widget dirlistLabel
= wxFSChild(XmDIALOG_DIR_LIST_LABEL
);
208 Widget filterLabel
= wxFSChild(XmDIALOG_FILTER_LABEL
);
209 Widget listLabel
= wxFSChild(XmDIALOG_LIST_LABEL
);
210 Widget selectionLabel
= wxFSChild(XmDIALOG_SELECTION_LABEL
);
215 wxXmString
btnOK( wxGetStockLabel( wxID_OK
, false ) ),
216 btnCancel( wxGetStockLabel( wxID_CANCEL
, false ) ),
217 btnFilter( _("Filter") ), lblFilter( _("Filter") ),
218 lblDirectories( _("Directories") ),
219 lblFiles( _("Files" ) ), lblSelection( _("Selection") );
221 XtVaSetValues( okWidget
, XmNlabelString
, btnOK(), NULL
);
222 XtVaSetValues( applyWidget
, XmNlabelString
, btnFilter(), NULL
);
223 XtVaSetValues( cancelWidget
, XmNlabelString
, btnCancel(), NULL
);
224 XtVaSetValues( dirlistLabel
, XmNlabelString
, lblDirectories(), NULL
);
225 XtVaSetValues( filterLabel
, XmNlabelString
, lblFilter(), NULL
);
226 XtVaSetValues( listLabel
, XmNlabelString
, lblFiles(), NULL
);
227 XtVaSetValues( selectionLabel
, XmNlabelString
, lblSelection(), NULL
);
229 Widget shell
= XtParent(fileSel
);
231 if (!m_message
.IsNull())
233 XmNtitle
, (const char*)m_message
.mb_str(),
236 if (!m_wildCard
.empty())
238 // return something understandable by Motif
239 wxString wildCard
= ParseWildCard( m_wildCard
);
242 filter
= m_dir
+ wxString("/") + wildCard
;
246 XmTextSetString(filterWidget
, filter
.char_str());
247 XmFileSelectionDoSearch(fileSel
, NULL
);
250 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
251 // file selector on Solaris 1.5.1.
252 if ( !m_dir
.empty() )
254 wxXmString
thePath( m_dir
);
256 XtVaSetValues (fileSel
,
257 XmNdirectory
, thePath(),
265 entirePath
= m_dir
+ wxString("/") + m_fileName
;
269 entirePath
= m_fileName
;
272 if (!entirePath
.empty())
274 XmTextSetString(selectionWidget
, entirePath
.char_str());
277 XtAddCallback(fileSel
, XmNcancelCallback
,
278 (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
279 XtAddCallback(fileSel
, XmNokCallback
,
280 (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
281 XtAddCallback(fileSel
, XmNunmapCallback
,
282 (XtCallbackProc
)wxFileSelClose
, (XtPointer
)this);
284 //#if XmVersion > 1000
285 // I'm not sure about what you mean with XmVersion.
286 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
287 // (Motif1.1.4 ==> XmVersion 1100 )
288 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile.
290 #if !DEFAULT_FILE_SELECTOR_SIZE
291 int width
= wxFSB_WIDTH
;
292 int height
= wxFSB_HEIGHT
;
293 XtVaSetValues(fileSel
,
296 XmNresizePolicy
, XmRESIZE_NONE
,
299 wxDoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
300 wxDoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
302 wxChangeListBoxColours(this, dirListWidget
);
303 wxChangeListBoxColours(this, fileListWidget
);
305 XtManageChild(fileSel
);
307 m_fileSelectorAnswer
= wxEmptyString
;
308 m_fileSelectorReturned
= false;
312 XtAddGrab(XtParent(fileSel
), True
, False
);
313 XtAppContext context
= (XtAppContext
) wxTheApp
->GetAppContext();
315 while (!m_fileSelectorReturned
)
317 XtAppNextEvent(context
, &event
);
318 XtDispatchEvent(&event
);
320 XtRemoveGrab(XtParent(fileSel
));
322 XtUnmapWidget(XtParent(fileSel
));
323 XtDestroyWidget(XtParent(fileSel
));
325 // Now process all events, because otherwise
326 // this might remain on the screen
327 wxFlushEvents(XtDisplay(fileSel
));
329 m_path
= m_fileSelectorAnswer
;
330 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
331 m_dir
= wxPathOnly(m_path
);
333 if (m_fileName
.empty())