extracted common code into a single wxfileDialogBase class (patch 758901)
[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/filedlg.h"
25 #include "wx/intl.h"
26 #include "wx/app.h"
27 #include "wx/settings.h"
28 #include "wx/tokenzr.h"
29
30 #ifdef __VMS__
31 #pragma message disable nosimpint
32 #endif
33 #include <Xm/Xm.h>
34 #include <Xm/MwmUtil.h>
35 #include <Xm/Label.h>
36 #include <Xm/BulletinB.h>
37 #include <Xm/Frame.h>
38 #include <Xm/Text.h>
39 #include <Xm/DialogS.h>
40 #include <Xm/FileSB.h>
41 #include <Xm/RowColumn.h>
42 #include <Xm/LabelG.h>
43 #ifdef __VMS__
44 #pragma message enable nosimpint
45 #endif
46
47 #include "wx/motif/private.h"
48
49 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
50
51 #define DEFAULT_FILE_SELECTOR_SIZE 0
52 // Let Motif defines the size of File
53 // Selector Box (if 1), or fix it to
54 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
55 #define wxFSB_WIDTH 600
56 #define wxFSB_HEIGHT 500
57
58
59 wxString wxFileDialog::m_fileSelectorAnswer = "";
60 bool wxFileDialog::m_fileSelectorReturned = FALSE;
61
62 static void wxFileSelClose(Widget WXUNUSED(w),
63 void* WXUNUSED(client_data),
64 XmAnyCallbackStruct *WXUNUSED(call_data))
65 {
66 wxFileDialog::m_fileSelectorAnswer = "";
67 wxFileDialog::m_fileSelectorReturned = TRUE;
68 }
69
70 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
71 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
72 {
73 wxFileDialog::m_fileSelectorAnswer = "";
74 wxFileDialog::m_fileSelectorReturned = TRUE;
75 }
76
77 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
78 {
79 char *filename = NULL;
80 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
81 wxFileDialog::m_fileSelectorAnswer = "";
82 wxFileDialog::m_fileSelectorReturned = TRUE;
83 } else {
84 if (filename) {
85 wxFileDialog::m_fileSelectorAnswer = filename;
86 XtFree(filename);
87 }
88 wxFileDialog::m_fileSelectorReturned = TRUE;
89 }
90 }
91
92 static wxString ParseWildCard( const wxString& wild )
93 {
94 #ifdef __WXDEBUG__
95 static const wxChar* msg =
96 _T("Motif file dialog does not understand this ")
97 _T("wildcard syntax");
98 #endif
99
100 wxStringTokenizer tok( wild, _T("|") );
101
102 wxCHECK_MSG( tok.CountTokens() <= 2, _T("*.*"), msg );
103
104 if( tok.CountTokens() == 1 ) return wild;
105
106 // CountTokens == 2
107 tok.GetNextToken();
108 wxStringTokenizer tok2( tok.GetNextToken(), _T(";") );
109
110 wxCHECK_MSG( tok2.CountTokens() == 1, tok2.GetNextToken(), msg );
111 return tok2.GetNextToken();
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 Arg args[10];
168 int ac = 0;
169
170 wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour,
171 (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
179 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
180 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
181
182 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
183 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
184 Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST);
185 Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST);
186
187 // code using these vars disabled
188 #if 0
189 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
190 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
191 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
192 #endif
193
194
195 Widget shell = XtParent(fileSel);
196
197 if (!m_message.IsNull())
198 XtVaSetValues(shell,
199 XmNtitle, wxConstCast(m_message.c_str(), char),
200 NULL);
201
202 wxString entirePath("");
203
204 if ((m_dir != "") && (m_fileName != ""))
205 {
206 entirePath = m_dir + wxString("/") + m_fileName;
207 }
208 else if ((m_dir != "") && (m_fileName == ""))
209 {
210 entirePath = m_dir + wxString("/");
211 }
212 else if ((m_dir == "") && (m_fileName != ""))
213 {
214 entirePath = m_fileName;
215 }
216
217 if (m_wildCard != "")
218 {
219 // return something understandable by Motif
220 wxString wildCard = ParseWildCard( m_wildCard );
221 wxString filter;
222 if (m_dir != "")
223 filter = m_dir + wxString("/") + wildCard;
224 else
225 filter = wildCard;
226
227 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
228 XmFileSelectionDoSearch(fileSel, NULL);
229 }
230
231 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
232 // file selector on Solaris 1.5.1.
233 if ( m_dir != "" )
234 {
235 wxXmString thePath( m_dir );
236
237 XtVaSetValues (fileSel,
238 XmNdirectory, thePath(),
239 NULL);
240 }
241
242 if (entirePath != "")
243 {
244 XmTextSetString(selectionWidget,
245 wxConstCast(entirePath.c_str(), char));
246 }
247
248 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
249 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
250 XtAddCallback(fileSel, XmNunmapCallback,
251 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
252
253 //#if XmVersion > 1000
254 // I'm not sure about what you mean with XmVersion.
255 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
256 // (Motif1.1.4 ==> XmVersion 1100 )
257 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
258 //
259 #if !DEFAULT_FILE_SELECTOR_SIZE
260 int width = wxFSB_WIDTH;
261 int height = wxFSB_HEIGHT;
262 XtVaSetValues(fileSel,
263 XmNwidth, width,
264 XmNheight, height,
265 XmNresizePolicy, XmRESIZE_NONE,
266 NULL);
267 #endif
268 // wxDoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
269 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
270 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
271
272 wxChangeListBoxColours(this, dirListWidget);
273 wxChangeListBoxColours(this, fileListWidget);
274
275 XtManageChild(fileSel);
276
277 m_fileSelectorAnswer = "";
278 m_fileSelectorReturned = FALSE;
279
280 wxEndBusyCursor();
281
282 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
283 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
284 XEvent event;
285 while (!m_fileSelectorReturned)
286 {
287 XtAppNextEvent(context, &event);
288 XtDispatchEvent(&event);
289 }
290 XtRemoveGrab(XtParent(fileSel));
291
292 // XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
293
294 Display* display = XtDisplay(fileSel);
295
296 XtUnmapWidget(XtParent(fileSel));
297 XtDestroyWidget(XtParent(fileSel));
298
299 // Now process all events, because otherwise
300 // this might remain on the screen
301 wxFlushEvents(display);
302
303 m_path = m_fileSelectorAnswer;
304 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
305 m_dir = wxPathOnly(m_path);
306
307 if (m_fileName == "")
308 return wxID_CANCEL;
309 else
310 return wxID_OK;
311 }
312