]> git.saurik.com Git - wxWidgets.git/blame - src/motif/filedlg.cpp
fix another memory leak in SetCommand() (coverity checker CID 52)
[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
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
bcd055ae
JJ
15#ifdef __VMS
16#define XtDisplay XTDISPLAY
17#define XtParent XTPARENT
18#define XtWindow XTWINDOW
19#endif
20
4bb6408c
JS
21#include "wx/defs.h"
22#include "wx/utils.h"
4bb6408c
JS
23#include "wx/filedlg.h"
24#include "wx/intl.h"
dfad0599 25#include "wx/app.h"
a91b47e8 26#include "wx/settings.h"
a4e64fb5 27#include "wx/tokenzr.h"
aeb9a156 28#include "wx/stockitem.h"
4bb6408c 29
338dd992
JJ
30#ifdef __VMS__
31#pragma message disable nosimpint
32#endif
f97c9854 33#include <Xm/Xm.h>
dfad0599
JS
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>
338dd992
JJ
43#ifdef __VMS__
44#pragma message enable nosimpint
45#endif
f97c9854 46
ee1aaf99
JS
47#include "wx/motif/private.h"
48
f74172ab 49IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
4bb6408c 50
dfe1eee3 51#define DEFAULT_FILE_SELECTOR_SIZE 0
2d120f83
JS
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)
dfe1eee3 55#define wxFSB_WIDTH 600
dfad0599
JS
56#define wxFSB_HEIGHT 500
57
58
83498ef2 59wxString wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
96be256b 60bool wxFileDialog::m_fileSelectorReturned = false;
f97c9854 61
a4e64fb5
MB
62static void wxFileSelClose(Widget WXUNUSED(w),
63 void* WXUNUSED(client_data),
64 XmAnyCallbackStruct *WXUNUSED(call_data))
65{
83498ef2 66 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
96be256b 67 wxFileDialog::m_fileSelectorReturned = true;
a4e64fb5
MB
68}
69
dfe1eee3 70void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
2d120f83 71 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
f97c9854 72{
83498ef2 73 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
96be256b 74 wxFileDialog::m_fileSelectorReturned = true;
f97c9854
JS
75}
76
f9e02ac7 77void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
f97c9854 78{
2d120f83
JS
79 char *filename = NULL;
80 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
83498ef2 81 wxFileDialog::m_fileSelectorAnswer = wxEmptyString;
96be256b 82 wxFileDialog::m_fileSelectorReturned = true;
2d120f83
JS
83 } else {
84 if (filename) {
85 wxFileDialog::m_fileSelectorAnswer = filename;
86 XtFree(filename);
87 }
96be256b 88 wxFileDialog::m_fileSelectorReturned = true;
f97c9854 89 }
f97c9854
JS
90}
91
a4e64fb5
MB
92static wxString ParseWildCard( const wxString& wild )
93{
02a48e3b 94#ifdef __WXDEBUG__
a4e64fb5
MB
95 static const wxChar* msg =
96 _T("Motif file dialog does not understand this ")
97 _T("wildcard syntax");
02a48e3b 98#endif
a4e64fb5 99
0fcb9bef
MB
100 wxArrayString wildDescriptions, wildFilters;
101 const size_t count = wxParseCommonDialogsFilter(wild,
102 wildDescriptions,
103 wildFilters);
104 wxCHECK_MSG( count, _T("*.*"), wxT("wxFileDialog: bad wildcard string") );
105 wxCHECK_MSG( count == 1, _T("*.*"), msg );
106
107 // check for *.txt;*.rtf
108 wxStringTokenizer tok2( wildFilters[0], _T(";") );
109 wxString wildcard = tok2.GetNextToken();
110
111 wxCHECK_MSG( tok2.CountTokens() <= 1, wildcard, msg );
112 return wildcard;
a4e64fb5
MB
113}
114
dfad0599 115wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
2d120f83
JS
116 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
117 long style, const wxPoint& pos)
f74172ab 118 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
dfad0599 119{
dfad0599 120 m_filterIndex = 1;
dfad0599 121}
f97c9854 122
af111fc3 123static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
a91b47e8 124{
a8680e3e 125 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
a91b47e8
JS
126
127 // Change colour of the scrolled areas of the listboxes
128 Widget listParent = XtParent (widget);
d4d46b56 129#if 0
96be256b 130 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, true);
d4d46b56 131#endif
a91b47e8
JS
132
133 Widget hsb = (Widget) 0;
134 Widget vsb = (Widget) 0;
135 XtVaGetValues (listParent,
136 XmNhorizontalScrollBar, &hsb,
137 XmNverticalScrollBar, &vsb,
138 NULL);
dfe1eee3 139
a91b47e8
JS
140 /* TODO: should scrollbars be affected? Should probably have separate
141 * function to change them (by default, taken from wxSystemSettings)
142 */
a756f210 143 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
96be256b
MB
144 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true);
145 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true);
a91b47e8
JS
146
147 if (hsb)
148 XtVaSetValues (hsb,
149 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
150 NULL);
151 if (vsb)
152 XtVaSetValues (vsb,
153 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
154 NULL);
155}
156
dfad0599 157int wxFileDialog::ShowModal()
f97c9854 158{
2d120f83 159 wxBeginBusyCursor();
dfe1eee3 160
2d120f83
JS
161 // static char fileBuf[512];
162 Widget parentWidget = (Widget) 0;
163 if (m_parent)
2d120f83 164 parentWidget = (Widget) m_parent->GetTopWidget();
f97c9854 165 else
2d120f83 166 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
ee1aaf99 167 // prepare the arg list
6ace5176 168 Display* dpy = XtDisplay(parentWidget);
ee1aaf99
JS
169 Arg args[10];
170 int ac = 0;
171
6ace5176 172 wxComputeColours (dpy, & m_backgroundColour, (wxColour*) NULL);
ee1aaf99
JS
173
174 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
175 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
176 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
177 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
178
6ace5176
MB
179 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
180
7398fb81
MB
181#if __WXMOTIF20__ && !__WXLESSTIF__
182 XtSetArg(args[ac], XmNbuttonRenderTable, font.GetFontTypeC(dpy)); ac++;
183 XtSetArg(args[ac], XmNlabelRenderTable, font.GetFontTypeC(dpy)); ac++;
184 XtSetArg(args[ac], XmNtextRenderTable, font.GetFontTypeC(dpy)); ac++;
185#else
186 XtSetArg(args[ac], XmNbuttonFontList, font.GetFontTypeC(dpy)); ac++;
187 XtSetArg(args[ac], XmNlabelFontList, font.GetFontTypeC(dpy)); ac++;
188 XtSetArg(args[ac], XmNtextFontList, font.GetFontTypeC(dpy)); ac++;
189#endif
dfe1eee3 190
aeb9a156 191 Widget fileSel = XmCreateFileSelectionDialog(parentWidget,
f1db433a
VZ
192 wxMOTIF_STR("file_selector"),
193 args, ac);
aeb9a156
MB
194#define wxFSChild( name ) \
195 XmFileSelectionBoxGetChild(fileSel, name)
196
197 XtUnmanageChild(wxFSChild(XmDIALOG_HELP_BUTTON));
198
199 Widget filterWidget = wxFSChild(XmDIALOG_FILTER_TEXT);
200 Widget selectionWidget = wxFSChild(XmDIALOG_TEXT);
201 Widget dirListWidget = wxFSChild(XmDIALOG_DIR_LIST);
202 Widget fileListWidget = wxFSChild(XmDIALOG_LIST);
203
204 // for changing labels
205 Widget okWidget = wxFSChild(XmDIALOG_OK_BUTTON);
206 Widget applyWidget = wxFSChild(XmDIALOG_APPLY_BUTTON);
207 Widget cancelWidget = wxFSChild(XmDIALOG_CANCEL_BUTTON);
208 Widget dirlistLabel = wxFSChild(XmDIALOG_DIR_LIST_LABEL);
209 Widget filterLabel = wxFSChild(XmDIALOG_FILTER_LABEL);
210 Widget listLabel = wxFSChild(XmDIALOG_LIST_LABEL);
211 Widget selectionLabel = wxFSChild(XmDIALOG_SELECTION_LABEL);
212
213#undef wxFSChild
214
215 // change labels
216 wxXmString btnOK( wxGetStockLabel( wxID_OK, false ) ),
217 btnCancel( wxGetStockLabel( wxID_CANCEL, false ) ),
218 btnFilter( _("Filter") ), lblFilter( _("Filter") ),
219 lblDirectories( _("Directories") ),
220 lblFiles( _("Files" ) ), lblSelection( _("Selection") );
221
222 XtVaSetValues( okWidget, XmNlabelString, btnOK(), NULL );
223 XtVaSetValues( applyWidget, XmNlabelString, btnFilter(), NULL );
224 XtVaSetValues( cancelWidget, XmNlabelString, btnCancel(), NULL );
225 XtVaSetValues( dirlistLabel, XmNlabelString, lblDirectories(), NULL );
226 XtVaSetValues( filterLabel, XmNlabelString, lblFilter(), NULL );
227 XtVaSetValues( listLabel, XmNlabelString, lblFiles(), NULL );
228 XtVaSetValues( selectionLabel, XmNlabelString, lblSelection(), NULL );
a91b47e8 229
2d120f83 230 Widget shell = XtParent(fileSel);
dfe1eee3 231
2d120f83 232 if (!m_message.IsNull())
d3a80c92
MB
233 XtVaSetValues(shell,
234 XmNtitle, wxConstCast(m_message.c_str(), char),
235 NULL);
dfe1eee3 236
83498ef2 237 if (!m_wildCard.empty())
2d120f83 238 {
a4e64fb5
MB
239 // return something understandable by Motif
240 wxString wildCard = ParseWildCard( m_wildCard );
241 wxString filter;
83498ef2 242 if (!m_dir.empty())
a4e64fb5 243 filter = m_dir + wxString("/") + wildCard;
2d120f83 244 else
a4e64fb5 245 filter = wildCard;
dfe1eee3 246
d3a80c92 247 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
2d120f83
JS
248 XmFileSelectionDoSearch(fileSel, NULL);
249 }
dfe1eee3 250
2d120f83
JS
251 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
252 // file selector on Solaris 1.5.1.
83498ef2 253 if ( !m_dir.empty() )
2d120f83 254 {
d4d46b56 255 wxXmString thePath( m_dir );
dfe1eee3 256
2d120f83 257 XtVaSetValues (fileSel,
d4d46b56 258 XmNdirectory, thePath(),
2d120f83 259 NULL);
d4d46b56 260 }
dfe1eee3 261
83498ef2 262 wxString entirePath;
aeb9a156 263
83498ef2 264 if (!m_dir.empty())
aeb9a156
MB
265 {
266 entirePath = m_dir + wxString("/") + m_fileName;
267 }
268 else
269 {
270 entirePath = m_fileName;
271 }
272
83498ef2 273 if (!entirePath.empty())
d4d46b56 274 {
d3a80c92
MB
275 XmTextSetString(selectionWidget,
276 wxConstCast(entirePath.c_str(), char));
2d120f83 277 }
dfe1eee3 278
aeb9a156
MB
279 XtAddCallback(fileSel, XmNcancelCallback,
280 (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
281 XtAddCallback(fileSel, XmNokCallback,
282 (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
a4e64fb5
MB
283 XtAddCallback(fileSel, XmNunmapCallback,
284 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
dfe1eee3 285
2d120f83
JS
286 //#if XmVersion > 1000
287 // I'm not sure about what you mean with XmVersion.
288 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
289 // (Motif1.1.4 ==> XmVersion 1100 )
aeb9a156 290 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile.
2d120f83 291 //
f97c9854 292#if !DEFAULT_FILE_SELECTOR_SIZE
2d120f83
JS
293 int width = wxFSB_WIDTH;
294 int height = wxFSB_HEIGHT;
295 XtVaSetValues(fileSel,
296 XmNwidth, width,
297 XmNheight, height,
298 XmNresizePolicy, XmRESIZE_NONE,
299 NULL);
f97c9854 300#endif
a8680e3e
MB
301 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
302 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
a91b47e8 303
a91b47e8
JS
304 wxChangeListBoxColours(this, dirListWidget);
305 wxChangeListBoxColours(this, fileListWidget);
dfe1eee3 306
2d120f83 307 XtManageChild(fileSel);
a91b47e8 308
83498ef2 309 m_fileSelectorAnswer = wxEmptyString;
96be256b 310 m_fileSelectorReturned = false;
dfe1eee3 311
2d120f83 312 wxEndBusyCursor();
dfe1eee3 313
96be256b 314 XtAddGrab(XtParent(fileSel), True, False);
eb6fa4b4 315 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
2d120f83
JS
316 XEvent event;
317 while (!m_fileSelectorReturned)
318 {
eb6fa4b4
MB
319 XtAppNextEvent(context, &event);
320 XtDispatchEvent(&event);
2d120f83
JS
321 }
322 XtRemoveGrab(XtParent(fileSel));
dfe1eee3 323
2d120f83
JS
324 XtUnmapWidget(XtParent(fileSel));
325 XtDestroyWidget(XtParent(fileSel));
dfe1eee3 326
2d120f83
JS
327 // Now process all events, because otherwise
328 // this might remain on the screen
aeb9a156 329 wxFlushEvents(XtDisplay(fileSel));
dfe1eee3 330
2d120f83
JS
331 m_path = m_fileSelectorAnswer;
332 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
333 m_dir = wxPathOnly(m_path);
dfe1eee3 334
83498ef2 335 if (m_fileName.empty())
2d120f83
JS
336 return wxID_CANCEL;
337 else
338 return wxID_OK;
4bb6408c 339}