wxFileDialog cleanup, extracted common code to fldlgcmn.cpp (patch 754187)
[wxWidgets.git] / src / motif / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "filedlg.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
19 #define XtWindow XTWINDOW
20 #endif
21
22 #include "wx/defs.h"
23 #include "wx/utils.h"
24 #include "wx/dialog.h"
25 #include "wx/filedlg.h"
26 #include "wx/intl.h"
27 #include "wx/app.h"
28 #include "wx/settings.h"
29 #include "wx/tokenzr.h"
30
31 #ifdef __VMS__
32 #pragma message disable nosimpint
33 #endif
34 #include <Xm/Xm.h>
35 #include <Xm/MwmUtil.h>
36 #include <Xm/Label.h>
37 #include <Xm/BulletinB.h>
38 #include <Xm/Frame.h>
39 #include <Xm/Text.h>
40 #include <Xm/DialogS.h>
41 #include <Xm/FileSB.h>
42 #include <Xm/RowColumn.h>
43 #include <Xm/LabelG.h>
44 #ifdef __VMS__
45 #pragma message enable nosimpint
46 #endif
47
48 #include "wx/motif/private.h"
49
50 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
51
52 #define DEFAULT_FILE_SELECTOR_SIZE 0
53 // Let Motif defines the size of File
54 // Selector Box (if 1), or fix it to
55 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
56 #define wxFSB_WIDTH 600
57 #define wxFSB_HEIGHT 500
58
59
60 wxString wxFileDialog::m_fileSelectorAnswer = "";
61 bool wxFileDialog::m_fileSelectorReturned = FALSE;
62
63 static void wxFileSelClose(Widget WXUNUSED(w),
64 void* WXUNUSED(client_data),
65 XmAnyCallbackStruct *WXUNUSED(call_data))
66 {
67 wxFileDialog::m_fileSelectorAnswer = "";
68 wxFileDialog::m_fileSelectorReturned = TRUE;
69 }
70
71 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
72 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
73 {
74 wxFileDialog::m_fileSelectorAnswer = "";
75 wxFileDialog::m_fileSelectorReturned = TRUE;
76 }
77
78 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
79 {
80 char *filename = NULL;
81 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
82 wxFileDialog::m_fileSelectorAnswer = "";
83 wxFileDialog::m_fileSelectorReturned = TRUE;
84 } else {
85 if (filename) {
86 wxFileDialog::m_fileSelectorAnswer = filename;
87 XtFree(filename);
88 }
89 wxFileDialog::m_fileSelectorReturned = TRUE;
90 }
91 }
92
93 static wxString ParseWildCard( const wxString& wild )
94 {
95 #ifdef __WXDEBUG__
96 static const wxChar* msg =
97 _T("Motif file dialog does not understand this ")
98 _T("wildcard syntax");
99 #endif
100
101 wxStringTokenizer tok( wild, _T("|") );
102
103 wxCHECK_MSG( tok.CountTokens() <= 2, _T("*.*"), msg );
104
105 if( tok.CountTokens() == 1 ) return wild;
106
107 // CountTokens == 2
108 tok.GetNextToken();
109 wxStringTokenizer tok2( tok.GetNextToken(), _T(";") );
110
111 wxCHECK_MSG( tok2.CountTokens() == 1, tok2.GetNextToken(), msg );
112 return tok2.GetNextToken();
113 }
114
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 {
119 m_message = message;
120 m_dialogStyle = style;
121 m_parent = parent;
122 m_path = "";
123 m_fileName = defaultFileName;
124 m_dir = defaultDir;
125 m_wildCard = wildCard;
126 m_filterIndex = 1;
127 m_pos = pos;
128 }
129
130 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
131 {
132 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
133
134 // Change colour of the scrolled areas of the listboxes
135 Widget listParent = XtParent (widget);
136 #if 0
137 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
138 #endif
139
140 Widget hsb = (Widget) 0;
141 Widget vsb = (Widget) 0;
142 XtVaGetValues (listParent,
143 XmNhorizontalScrollBar, &hsb,
144 XmNverticalScrollBar, &vsb,
145 NULL);
146
147 /* TODO: should scrollbars be affected? Should probably have separate
148 * function to change them (by default, taken from wxSystemSettings)
149 */
150 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
151 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
152 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
153
154 if (hsb)
155 XtVaSetValues (hsb,
156 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
157 NULL);
158 if (vsb)
159 XtVaSetValues (vsb,
160 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
161 NULL);
162 }
163
164 int wxFileDialog::ShowModal()
165 {
166 wxBeginBusyCursor();
167
168 // static char fileBuf[512];
169 Widget parentWidget = (Widget) 0;
170 if (m_parent)
171 parentWidget = (Widget) m_parent->GetTopWidget();
172 else
173 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
174 // prepare the arg list
175 Arg args[10];
176 int ac = 0;
177
178 wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour,
179 (wxColour*) NULL);
180
181 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
182 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
183 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
184 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
185
186
187 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
188 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
189
190 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
191 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
192 Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST);
193 Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST);
194
195 // code using these vars disabled
196 #if 0
197 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
198 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
199 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
200 #endif
201
202
203 Widget shell = XtParent(fileSel);
204
205 if (!m_message.IsNull())
206 XtVaSetValues(shell,
207 XmNtitle, wxConstCast(m_message.c_str(), char),
208 NULL);
209
210 wxString entirePath("");
211
212 if ((m_dir != "") && (m_fileName != ""))
213 {
214 entirePath = m_dir + wxString("/") + m_fileName;
215 }
216 else if ((m_dir != "") && (m_fileName == ""))
217 {
218 entirePath = m_dir + wxString("/");
219 }
220 else if ((m_dir == "") && (m_fileName != ""))
221 {
222 entirePath = m_fileName;
223 }
224
225 if (m_wildCard != "")
226 {
227 // return something understandable by Motif
228 wxString wildCard = ParseWildCard( m_wildCard );
229 wxString filter;
230 if (m_dir != "")
231 filter = m_dir + wxString("/") + wildCard;
232 else
233 filter = wildCard;
234
235 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
236 XmFileSelectionDoSearch(fileSel, NULL);
237 }
238
239 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
240 // file selector on Solaris 1.5.1.
241 if ( m_dir != "" )
242 {
243 wxXmString thePath( m_dir );
244
245 XtVaSetValues (fileSel,
246 XmNdirectory, thePath(),
247 NULL);
248 }
249
250 if (entirePath != "")
251 {
252 XmTextSetString(selectionWidget,
253 wxConstCast(entirePath.c_str(), char));
254 }
255
256 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
257 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
258 XtAddCallback(fileSel, XmNunmapCallback,
259 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
260
261 //#if XmVersion > 1000
262 // I'm not sure about what you mean with XmVersion.
263 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
264 // (Motif1.1.4 ==> XmVersion 1100 )
265 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
266 //
267 #if !DEFAULT_FILE_SELECTOR_SIZE
268 int width = wxFSB_WIDTH;
269 int height = wxFSB_HEIGHT;
270 XtVaSetValues(fileSel,
271 XmNwidth, width,
272 XmNheight, height,
273 XmNresizePolicy, XmRESIZE_NONE,
274 NULL);
275 #endif
276 // wxDoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
277 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
278 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
279
280 wxChangeListBoxColours(this, dirListWidget);
281 wxChangeListBoxColours(this, fileListWidget);
282
283 XtManageChild(fileSel);
284
285 m_fileSelectorAnswer = "";
286 m_fileSelectorReturned = FALSE;
287
288 wxEndBusyCursor();
289
290 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
291 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
292 XEvent event;
293 while (!m_fileSelectorReturned)
294 {
295 XtAppNextEvent(context, &event);
296 XtDispatchEvent(&event);
297 }
298 XtRemoveGrab(XtParent(fileSel));
299
300 // XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
301
302 Display* display = XtDisplay(fileSel);
303
304 XtUnmapWidget(XtParent(fileSel));
305 XtDestroyWidget(XtParent(fileSel));
306
307 // Now process all events, because otherwise
308 // this might remain on the screen
309 wxFlushEvents(display);
310
311 m_path = m_fileSelectorAnswer;
312 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
313 m_dir = wxPathOnly(m_path);
314
315 if (m_fileName == "")
316 return wxID_CANCEL;
317 else
318 return wxID_OK;
319 }
320