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