wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[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 #ifdef __VMS
16 #define XtDisplay XTDISPLAY
17 #define XtParent XTPARENT
18 #define XtWindow XTWINDOW
19 #endif
20
21 #include "wx/utils.h"
22 #include "wx/filedlg.h"
23 #include "wx/intl.h"
24 #include "wx/app.h"
25 #include "wx/settings.h"
26 #include "wx/tokenzr.h"
27 #include "wx/stockitem.h"
28
29 #ifdef __VMS__
30 #pragma message disable nosimpint
31 #endif
32 #include <Xm/Xm.h>
33 #include <Xm/MwmUtil.h>
34 #include <Xm/Label.h>
35 #include <Xm/BulletinB.h>
36 #include <Xm/Frame.h>
37 #include <Xm/Text.h>
38 #include <Xm/DialogS.h>
39 #include <Xm/FileSB.h>
40 #include <Xm/RowColumn.h>
41 #include <Xm/LabelG.h>
42 #ifdef __VMS__
43 #pragma message enable nosimpint
44 #endif
45
46 #include "wx/motif/private.h"
47
48 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
49
50 #define DEFAULT_FILE_SELECTOR_SIZE 0
51 // Let Motif defines the size of File
52 // Selector Box (if 1), or fix it to
53 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
54 #define wxFSB_WIDTH 600
55 #define wxFSB_HEIGHT 500
56
57
58 wxString wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
59 bool wxFileDialog::m_fileSelectorReturned = false;
60
61 static void wxFileSelClose(Widget WXUNUSED(w),
62 void* WXUNUSED(client_data),
63 XmAnyCallbackStruct *WXUNUSED(call_data))
64 {
65 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
66 wxFileDialog::m_fileSelectorReturned = true;
67 }
68
69 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
70 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
71 {
72 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
73 wxFileDialog::m_fileSelectorReturned = true;
74 }
75
76 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
77 {
78 char *filename = NULL;
79 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
80 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
81 wxFileDialog::m_fileSelectorReturned = true;
82 } else {
83 if (filename) {
84 wxFileDialog::m_fileSelectorAnswer = filename;
85 XtFree(filename);
86 }
87 wxFileDialog::m_fileSelectorReturned = true;
88 }
89 }
90
91 static wxString ParseWildCard( const wxString& wild )
92 {
93 #ifdef __WXDEBUG__
94 static const wxChar* msg =
95 _T("Motif file dialog does not understand this ")
96 _T("wildcard syntax");
97 #endif
98
99 wxArrayString wildDescriptions, wildFilters;
100 const size_t count = wxParseCommonDialogsFilter(wild,
101 wildDescriptions,
102 wildFilters);
103 wxCHECK_MSG( count, _T("*.*"), wxT("wxFileDialog: bad wildcard string") );
104 wxCHECK_MSG( count == 1, _T("*.*"), msg );
105
106 // check for *.txt;*.rtf
107 wxStringTokenizer tok2( wildFilters[0], _T(";") );
108 wxString wildcard = tok2.GetNextToken();
109
110 wxCHECK_MSG( tok2.CountTokens() <= 1, wildcard, msg );
111 return wildcard;
112 }
113
114 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
115 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
116 long style, const wxPoint& pos)
117 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
118 {
119 m_filterIndex = 1;
120 }
121
122 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
123 {
124 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
125
126 // Change colour of the scrolled areas of the listboxes
127 Widget listParent = XtParent (widget);
128 #if 0
129 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, true);
130 #endif
131
132 Widget hsb = (Widget) 0;
133 Widget vsb = (Widget) 0;
134 XtVaGetValues (listParent,
135 XmNhorizontalScrollBar, &hsb,
136 XmNverticalScrollBar, &vsb,
137 NULL);
138
139 /* TODO: should scrollbars be affected? Should probably have separate
140 * function to change them (by default, taken from wxSystemSettings)
141 */
142 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
143 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true);
144 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true);
145
146 if (hsb)
147 XtVaSetValues (hsb,
148 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
149 NULL);
150 if (vsb)
151 XtVaSetValues (vsb,
152 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
153 NULL);
154 }
155
156 int wxFileDialog::ShowModal()
157 {
158 wxBeginBusyCursor();
159
160 // static char fileBuf[512];
161 Widget parentWidget = (Widget) 0;
162 if (m_parent)
163 parentWidget = (Widget) m_parent->GetTopWidget();
164 else
165 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
166 // prepare the arg list
167 Display* dpy = XtDisplay(parentWidget);
168 Arg args[10];
169 int ac = 0;
170
171 wxComputeColours (dpy, & m_backgroundColour, (wxColour*) NULL);
172
173 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
174 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
175 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
176 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
177
178 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
179
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++;
184 #else
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++;
188 #endif
189
190 Widget fileSel = XmCreateFileSelectionDialog(parentWidget,
191 wxMOTIF_STR("file_selector"),
192 args, ac);
193 #define wxFSChild( name ) \
194 XmFileSelectionBoxGetChild(fileSel, name)
195
196 XtUnmanageChild(wxFSChild(XmDIALOG_HELP_BUTTON));
197
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);
202
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);
211
212 #undef wxFSChild
213
214 // change labels
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") );
220
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 );
228
229 Widget shell = XtParent(fileSel);
230
231 if (!m_message.IsNull())
232 XtVaSetValues(shell,
233 XmNtitle, wxConstCast(m_message.c_str(), char),
234 NULL);
235
236 if (!m_wildCard.empty())
237 {
238 // return something understandable by Motif
239 wxString wildCard = ParseWildCard( m_wildCard );
240 wxString filter;
241 if (!m_dir.empty())
242 filter = m_dir + wxString("/") + wildCard;
243 else
244 filter = wildCard;
245
246 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
247 XmFileSelectionDoSearch(fileSel, NULL);
248 }
249
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() )
253 {
254 wxXmString thePath( m_dir );
255
256 XtVaSetValues (fileSel,
257 XmNdirectory, thePath(),
258 NULL);
259 }
260
261 wxString entirePath;
262
263 if (!m_dir.empty())
264 {
265 entirePath = m_dir + wxString("/") + m_fileName;
266 }
267 else
268 {
269 entirePath = m_fileName;
270 }
271
272 if (!entirePath.empty())
273 {
274 XmTextSetString(selectionWidget,
275 wxConstCast(entirePath.c_str(), char));
276 }
277
278 XtAddCallback(fileSel, XmNcancelCallback,
279 (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
280 XtAddCallback(fileSel, XmNokCallback,
281 (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
282 XtAddCallback(fileSel, XmNunmapCallback,
283 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
284
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.
290 //
291 #if !DEFAULT_FILE_SELECTOR_SIZE
292 int width = wxFSB_WIDTH;
293 int height = wxFSB_HEIGHT;
294 XtVaSetValues(fileSel,
295 XmNwidth, width,
296 XmNheight, height,
297 XmNresizePolicy, XmRESIZE_NONE,
298 NULL);
299 #endif
300 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
301 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
302
303 wxChangeListBoxColours(this, dirListWidget);
304 wxChangeListBoxColours(this, fileListWidget);
305
306 XtManageChild(fileSel);
307
308 m_fileSelectorAnswer = wxEmptyString;
309 m_fileSelectorReturned = false;
310
311 wxEndBusyCursor();
312
313 XtAddGrab(XtParent(fileSel), True, False);
314 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
315 XEvent event;
316 while (!m_fileSelectorReturned)
317 {
318 XtAppNextEvent(context, &event);
319 XtDispatchEvent(&event);
320 }
321 XtRemoveGrab(XtParent(fileSel));
322
323 XtUnmapWidget(XtParent(fileSel));
324 XtDestroyWidget(XtParent(fileSel));
325
326 // Now process all events, because otherwise
327 // this might remain on the screen
328 wxFlushEvents(XtDisplay(fileSel));
329
330 m_path = m_fileSelectorAnswer;
331 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
332 m_dir = wxPathOnly(m_path);
333
334 if (m_fileName.empty())
335 return wxID_CANCEL;
336 else
337 return wxID_OK;
338 }