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