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