]> git.saurik.com Git - wxWidgets.git/blame - src/motif/filedlg.cpp
Crash fix under VC++
[wxWidgets.git] / src / motif / filedlg.cpp
CommitLineData
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "filedlg.h"
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
bcd055ae
JJ
19#ifdef __VMS
20#define XtDisplay XTDISPLAY
21#define XtParent XTPARENT
22#define XtWindow XTWINDOW
23#endif
24
4bb6408c
JS
25#include "wx/defs.h"
26#include "wx/utils.h"
4bb6408c
JS
27#include "wx/filedlg.h"
28#include "wx/intl.h"
dfad0599 29#include "wx/app.h"
a91b47e8 30#include "wx/settings.h"
a4e64fb5 31#include "wx/tokenzr.h"
4bb6408c 32
338dd992
JJ
33#ifdef __VMS__
34#pragma message disable nosimpint
35#endif
f97c9854 36#include <Xm/Xm.h>
dfad0599
JS
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>
338dd992
JJ
46#ifdef __VMS__
47#pragma message enable nosimpint
48#endif
f97c9854 49
ee1aaf99
JS
50#include "wx/motif/private.h"
51
f74172ab 52IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
4bb6408c 53
dfe1eee3 54#define DEFAULT_FILE_SELECTOR_SIZE 0
2d120f83
JS
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)
dfe1eee3 58#define wxFSB_WIDTH 600
dfad0599
JS
59#define wxFSB_HEIGHT 500
60
61
dfad0599
JS
62wxString wxFileDialog::m_fileSelectorAnswer = "";
63bool wxFileDialog::m_fileSelectorReturned = FALSE;
f97c9854 64
a4e64fb5
MB
65static 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
dfe1eee3 73void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
2d120f83 74 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
f97c9854 75{
2d120f83
JS
76 wxFileDialog::m_fileSelectorAnswer = "";
77 wxFileDialog::m_fileSelectorReturned = TRUE;
f97c9854
JS
78}
79
f9e02ac7 80void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
f97c9854 81{
2d120f83
JS
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;
f97c9854 92 }
f97c9854
JS
93}
94
a4e64fb5
MB
95static wxString ParseWildCard( const wxString& wild )
96{
02a48e3b 97#ifdef __WXDEBUG__
a4e64fb5
MB
98 static const wxChar* msg =
99 _T("Motif file dialog does not understand this ")
100 _T("wildcard syntax");
02a48e3b 101#endif
a4e64fb5
MB
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
dfad0599 117wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
2d120f83
JS
118 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
119 long style, const wxPoint& pos)
f74172ab 120 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
dfad0599 121{
dfad0599 122 m_filterIndex = 1;
dfad0599 123}
f97c9854 124
af111fc3 125static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
a91b47e8 126{
a8680e3e 127 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
a91b47e8
JS
128
129 // Change colour of the scrolled areas of the listboxes
130 Widget listParent = XtParent (widget);
d4d46b56 131#if 0
a8680e3e 132 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
d4d46b56 133#endif
a91b47e8
JS
134
135 Widget hsb = (Widget) 0;
136 Widget vsb = (Widget) 0;
137 XtVaGetValues (listParent,
138 XmNhorizontalScrollBar, &hsb,
139 XmNverticalScrollBar, &vsb,
140 NULL);
dfe1eee3 141
a91b47e8
JS
142 /* TODO: should scrollbars be affected? Should probably have separate
143 * function to change them (by default, taken from wxSystemSettings)
144 */
a756f210 145 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
a8680e3e
MB
146 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
147 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
a91b47e8
JS
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
dfad0599 159int wxFileDialog::ShowModal()
f97c9854 160{
2d120f83 161 wxBeginBusyCursor();
dfe1eee3 162
2d120f83
JS
163 // static char fileBuf[512];
164 Widget parentWidget = (Widget) 0;
165 if (m_parent)
2d120f83 166 parentWidget = (Widget) m_parent->GetTopWidget();
f97c9854 167 else
2d120f83 168 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
ee1aaf99
JS
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
dfe1eee3 181
ee1aaf99 182 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
2d120f83 183 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
a91b47e8
JS
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);
dfe1eee3
VZ
189
190 // code using these vars disabled
191#if 0
a91b47e8
JS
192 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
193 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
194 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
dfe1eee3
VZ
195#endif
196
a91b47e8 197
2d120f83 198 Widget shell = XtParent(fileSel);
dfe1eee3 199
2d120f83 200 if (!m_message.IsNull())
d3a80c92
MB
201 XtVaSetValues(shell,
202 XmNtitle, wxConstCast(m_message.c_str(), char),
203 NULL);
dfe1eee3 204
2d120f83 205 wxString entirePath("");
dfe1eee3 206
2d120f83
JS
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 }
dfe1eee3 219
2d120f83
JS
220 if (m_wildCard != "")
221 {
a4e64fb5
MB
222 // return something understandable by Motif
223 wxString wildCard = ParseWildCard( m_wildCard );
224 wxString filter;
2d120f83 225 if (m_dir != "")
a4e64fb5 226 filter = m_dir + wxString("/") + wildCard;
2d120f83 227 else
a4e64fb5 228 filter = wildCard;
dfe1eee3 229
d3a80c92 230 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
2d120f83
JS
231 XmFileSelectionDoSearch(fileSel, NULL);
232 }
dfe1eee3 233
2d120f83
JS
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 {
d4d46b56 238 wxXmString thePath( m_dir );
dfe1eee3 239
2d120f83 240 XtVaSetValues (fileSel,
d4d46b56 241 XmNdirectory, thePath(),
2d120f83 242 NULL);
d4d46b56 243 }
dfe1eee3 244
d4d46b56
MB
245 if (entirePath != "")
246 {
d3a80c92
MB
247 XmTextSetString(selectionWidget,
248 wxConstCast(entirePath.c_str(), char));
2d120f83 249 }
dfe1eee3 250
2d120f83
JS
251 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
252 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
a4e64fb5
MB
253 XtAddCallback(fileSel, XmNunmapCallback,
254 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
dfe1eee3 255
2d120f83
JS
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 //
f97c9854 262#if !DEFAULT_FILE_SELECTOR_SIZE
2d120f83
JS
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);
f97c9854 270#endif
a8680e3e
MB
271 // wxDoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
272 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
273 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
a91b47e8 274
a91b47e8
JS
275 wxChangeListBoxColours(this, dirListWidget);
276 wxChangeListBoxColours(this, fileListWidget);
dfe1eee3 277
2d120f83 278 XtManageChild(fileSel);
a91b47e8 279
2d120f83
JS
280 m_fileSelectorAnswer = "";
281 m_fileSelectorReturned = FALSE;
dfe1eee3 282
2d120f83 283 wxEndBusyCursor();
dfe1eee3 284
2d120f83 285 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
eb6fa4b4 286 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
2d120f83
JS
287 XEvent event;
288 while (!m_fileSelectorReturned)
289 {
eb6fa4b4
MB
290 XtAppNextEvent(context, &event);
291 XtDispatchEvent(&event);
2d120f83
JS
292 }
293 XtRemoveGrab(XtParent(fileSel));
dfe1eee3 294
eb6fa4b4
MB
295 // XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
296
297 Display* display = XtDisplay(fileSel);
dfe1eee3 298
2d120f83
JS
299 XtUnmapWidget(XtParent(fileSel));
300 XtDestroyWidget(XtParent(fileSel));
dfe1eee3 301
2d120f83
JS
302 // Now process all events, because otherwise
303 // this might remain on the screen
eb6fa4b4 304 wxFlushEvents(display);
dfe1eee3 305
2d120f83
JS
306 m_path = m_fileSelectorAnswer;
307 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
308 m_dir = wxPathOnly(m_path);
dfe1eee3 309
2d120f83
JS
310 if (m_fileName == "")
311 return wxID_CANCEL;
312 else
313 return wxID_OK;
4bb6408c
JS
314}
315