Don't initialize wxFileDialog::m_filterIndex to 1 in wxMotif.
[wxWidgets.git] / src / motif / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/filedlg.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/app.h"
20 #include "wx/utils.h"
21 #include "wx/settings.h"
22 #endif
23
24 #include "wx/tokenzr.h"
25 #include "wx/stockitem.h"
26
27 #ifdef __VMS__
28 #pragma message disable nosimpint
29 #endif
30 #include <Xm/Xm.h>
31 #include <Xm/MwmUtil.h>
32 #include <Xm/Label.h>
33 #include <Xm/BulletinB.h>
34 #include <Xm/Frame.h>
35 #include <Xm/Text.h>
36 #include <Xm/DialogS.h>
37 #include <Xm/FileSB.h>
38 #include <Xm/RowColumn.h>
39 #include <Xm/LabelG.h>
40 #ifdef __VMS__
41 #pragma message enable nosimpint
42 #endif
43
44 #include "wx/motif/private.h"
45
46 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
47
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
54
55
56 wxString wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
57 bool wxFileDialog::m_fileSelectorReturned = false;
58
59 static void wxFileSelClose(Widget WXUNUSED(w),
60 void* WXUNUSED(client_data),
61 XmAnyCallbackStruct *WXUNUSED(call_data))
62 {
63 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
64 wxFileDialog::m_fileSelectorReturned = true;
65 }
66
67 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
68 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
69 {
70 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
71 wxFileDialog::m_fileSelectorReturned = true;
72 }
73
74 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
75 {
76 char *filename = NULL;
77 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
78 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
79 wxFileDialog::m_fileSelectorReturned = true;
80 } else {
81 if (filename) {
82 wxFileDialog::m_fileSelectorAnswer = filename;
83 XtFree(filename);
84 }
85 wxFileDialog::m_fileSelectorReturned = true;
86 }
87 }
88
89 static wxString ParseWildCard( const wxString& wild )
90 {
91 #if wxDEBUG_LEVEL
92 static const char *msg =
93 "Motif file dialog does not understand this wildcard syntax";
94 #endif
95
96 wxArrayString wildDescriptions, wildFilters;
97 const size_t count = wxParseCommonDialogsFilter(wild,
98 wildDescriptions,
99 wildFilters);
100 wxCHECK_MSG( count, wxT("*.*"), wxT("wxFileDialog: bad wildcard string") );
101 wxCHECK_MSG( count == 1, wxT("*.*"), msg );
102
103 // check for *.txt;*.rtf
104 wxStringTokenizer tok2( wildFilters[0], wxT(";") );
105 wxString wildcard = tok2.GetNextToken();
106
107 wxCHECK_MSG( tok2.CountTokens() <= 1, wildcard, msg );
108 return wildcard;
109 }
110
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)
115 {
116 }
117
118 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
119 {
120 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
121
122 // Change colour of the scrolled areas of the listboxes
123 Widget listParent = XtParent (widget);
124 #if 0
125 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, true);
126 #endif
127
128 Widget hsb = (Widget) 0;
129 Widget vsb = (Widget) 0;
130 XtVaGetValues (listParent,
131 XmNhorizontalScrollBar, &hsb,
132 XmNverticalScrollBar, &vsb,
133 NULL);
134
135 /* TODO: should scrollbars be affected? Should probably have separate
136 * function to change them (by default, taken from wxSystemSettings)
137 */
138 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
139 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true);
140 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true);
141
142 if (hsb)
143 XtVaSetValues (hsb,
144 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
145 NULL);
146 if (vsb)
147 XtVaSetValues (vsb,
148 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
149 NULL);
150 }
151
152 int wxFileDialog::ShowModal()
153 {
154 wxBeginBusyCursor();
155
156 // static char fileBuf[512];
157 Widget parentWidget = (Widget) 0;
158 if (m_parent)
159 parentWidget = (Widget) m_parent->GetTopWidget();
160 else
161 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
162 // prepare the arg list
163 Display* dpy = XtDisplay(parentWidget);
164 Arg args[10];
165 int ac = 0;
166
167 if (m_backgroundColour.IsOk())
168 {
169 wxComputeColours (dpy, & m_backgroundColour, NULL);
170
171 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
172 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
173 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
174 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
175 }
176
177 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
178
179 #if __WXMOTIF20__ && !__WXLESSTIF__
180 XtSetArg(args[ac], XmNbuttonRenderTable, font.GetFontTypeC(dpy)); ac++;
181 XtSetArg(args[ac], XmNlabelRenderTable, font.GetFontTypeC(dpy)); ac++;
182 XtSetArg(args[ac], XmNtextRenderTable, font.GetFontTypeC(dpy)); ac++;
183 #else
184 XtSetArg(args[ac], XmNbuttonFontList, font.GetFontTypeC(dpy)); ac++;
185 XtSetArg(args[ac], XmNlabelFontList, font.GetFontTypeC(dpy)); ac++;
186 XtSetArg(args[ac], XmNtextFontList, font.GetFontTypeC(dpy)); ac++;
187 #endif
188
189 Widget fileSel = XmCreateFileSelectionDialog(parentWidget,
190 wxMOTIF_STR("file_selector"),
191 args, ac);
192 #define wxFSChild( name ) \
193 XmFileSelectionBoxGetChild(fileSel, name)
194
195 XtUnmanageChild(wxFSChild(XmDIALOG_HELP_BUTTON));
196
197 Widget filterWidget = wxFSChild(XmDIALOG_FILTER_TEXT);
198 Widget selectionWidget = wxFSChild(XmDIALOG_TEXT);
199 Widget dirListWidget = wxFSChild(XmDIALOG_DIR_LIST);
200 Widget fileListWidget = wxFSChild(XmDIALOG_LIST);
201
202 // for changing labels
203 Widget okWidget = wxFSChild(XmDIALOG_OK_BUTTON);
204 Widget applyWidget = wxFSChild(XmDIALOG_APPLY_BUTTON);
205 Widget cancelWidget = wxFSChild(XmDIALOG_CANCEL_BUTTON);
206 Widget dirlistLabel = wxFSChild(XmDIALOG_DIR_LIST_LABEL);
207 Widget filterLabel = wxFSChild(XmDIALOG_FILTER_LABEL);
208 Widget listLabel = wxFSChild(XmDIALOG_LIST_LABEL);
209 Widget selectionLabel = wxFSChild(XmDIALOG_SELECTION_LABEL);
210
211 #undef wxFSChild
212
213 // change labels
214 wxXmString btnOK( wxGetStockLabel( wxID_OK, false ) ),
215 btnCancel( wxGetStockLabel( wxID_CANCEL, false ) ),
216 btnFilter( _("Filter") ), lblFilter( _("Filter") ),
217 lblDirectories( _("Directories") ),
218 lblFiles( _("Files" ) ), lblSelection( _("Selection") );
219
220 XtVaSetValues( okWidget, XmNlabelString, btnOK(), NULL );
221 XtVaSetValues( applyWidget, XmNlabelString, btnFilter(), NULL );
222 XtVaSetValues( cancelWidget, XmNlabelString, btnCancel(), NULL );
223 XtVaSetValues( dirlistLabel, XmNlabelString, lblDirectories(), NULL );
224 XtVaSetValues( filterLabel, XmNlabelString, lblFilter(), NULL );
225 XtVaSetValues( listLabel, XmNlabelString, lblFiles(), NULL );
226 XtVaSetValues( selectionLabel, XmNlabelString, lblSelection(), NULL );
227
228 Widget shell = XtParent(fileSel);
229
230 if ( !m_message.empty() )
231 XtVaSetValues(shell,
232 XmNtitle, (const char*)m_message.mb_str(),
233 NULL);
234
235 if (!m_wildCard.empty())
236 {
237 // return something understandable by Motif
238 wxString wildCard = ParseWildCard( m_wildCard );
239 wxString filter;
240 if (!m_dir.empty())
241 filter = m_dir + wxString("/") + wildCard;
242 else
243 filter = wildCard;
244
245 XmTextSetString(filterWidget, filter.char_str());
246 XmFileSelectionDoSearch(fileSel, NULL);
247 }
248
249 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
250 // file selector on Solaris 1.5.1.
251 if ( !m_dir.empty() )
252 {
253 wxXmString thePath( m_dir );
254
255 XtVaSetValues (fileSel,
256 XmNdirectory, thePath(),
257 NULL);
258 }
259
260 wxString entirePath;
261
262 if (!m_dir.empty())
263 {
264 entirePath = m_dir + wxString("/") + m_fileName;
265 }
266 else
267 {
268 entirePath = m_fileName;
269 }
270
271 if (!entirePath.empty())
272 {
273 XmTextSetString(selectionWidget, entirePath.char_str());
274 }
275
276 XtAddCallback(fileSel, XmNcancelCallback,
277 (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
278 XtAddCallback(fileSel, XmNokCallback,
279 (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
280 XtAddCallback(fileSel, XmNunmapCallback,
281 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
282
283 //#if XmVersion > 1000
284 // I'm not sure about what you mean with XmVersion.
285 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
286 // (Motif1.1.4 ==> XmVersion 1100 )
287 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile.
288 //
289 #if !DEFAULT_FILE_SELECTOR_SIZE
290 int width = wxFSB_WIDTH;
291 int height = wxFSB_HEIGHT;
292 XtVaSetValues(fileSel,
293 XmNwidth, width,
294 XmNheight, height,
295 XmNresizePolicy, XmRESIZE_NONE,
296 NULL);
297 #endif
298 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
299 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
300
301 wxChangeListBoxColours(this, dirListWidget);
302 wxChangeListBoxColours(this, fileListWidget);
303
304 XtManageChild(fileSel);
305
306 m_fileSelectorAnswer = wxEmptyString;
307 m_fileSelectorReturned = false;
308
309 wxEndBusyCursor();
310
311 XtAddGrab(XtParent(fileSel), True, False);
312 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
313 XEvent event;
314 while (!m_fileSelectorReturned)
315 {
316 XtAppNextEvent(context, &event);
317 XtDispatchEvent(&event);
318 }
319 XtRemoveGrab(XtParent(fileSel));
320
321 XtUnmapWidget(XtParent(fileSel));
322 XtDestroyWidget(XtParent(fileSel));
323
324 // Now process all events, because otherwise
325 // this might remain on the screen
326 wxFlushEvents(XtDisplay(fileSel));
327
328 m_path = m_fileSelectorAnswer;
329 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
330 m_dir = wxPathOnly(m_path);
331
332 if (m_fileName.empty())
333 return wxID_CANCEL;
334 else
335 return wxID_OK;
336 }