]> git.saurik.com Git - wxWidgets.git/blame - src/motif/filedlg.cpp
corrections for buffers
[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
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "filedlg.h"
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtDisplay XTDISPLAY
18#define XtParent XTPARENT
19#define XtWindow XTWINDOW
20#endif
21
4bb6408c
JS
22#include "wx/defs.h"
23#include "wx/utils.h"
4bb6408c
JS
24#include "wx/filedlg.h"
25#include "wx/intl.h"
dfad0599 26#include "wx/app.h"
a91b47e8 27#include "wx/settings.h"
a4e64fb5 28#include "wx/tokenzr.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
dfad0599
JS
59wxString wxFileDialog::m_fileSelectorAnswer = "";
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{
66 wxFileDialog::m_fileSelectorAnswer = "";
67 wxFileDialog::m_fileSelectorReturned = TRUE;
68}
69
dfe1eee3 70void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
2d120f83 71 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
f97c9854 72{
2d120f83
JS
73 wxFileDialog::m_fileSelectorAnswer = "";
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)) {
81 wxFileDialog::m_fileSelectorAnswer = "";
82 wxFileDialog::m_fileSelectorReturned = TRUE;
83 } else {
84 if (filename) {
85 wxFileDialog::m_fileSelectorAnswer = filename;
86 XtFree(filename);
87 }
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
MB
99
100 wxStringTokenizer tok( wild, _T("|") );
101
102 wxCHECK_MSG( tok.CountTokens() <= 2, _T("*.*"), msg );
103
104 if( tok.CountTokens() == 1 ) return wild;
105
106 // CountTokens == 2
107 tok.GetNextToken();
108 wxStringTokenizer tok2( tok.GetNextToken(), _T(";") );
109
110 wxCHECK_MSG( tok2.CountTokens() == 1, tok2.GetNextToken(), msg );
111 return tok2.GetNextToken();
112}
113
dfad0599 114wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
2d120f83
JS
115 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
116 long style, const wxPoint& pos)
f74172ab 117 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
dfad0599 118{
dfad0599 119 m_filterIndex = 1;
dfad0599 120}
f97c9854 121
af111fc3 122static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
a91b47e8 123{
a8680e3e 124 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
a91b47e8
JS
125
126 // Change colour of the scrolled areas of the listboxes
127 Widget listParent = XtParent (widget);
d4d46b56 128#if 0
a8680e3e 129 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
d4d46b56 130#endif
a91b47e8
JS
131
132 Widget hsb = (Widget) 0;
133 Widget vsb = (Widget) 0;
134 XtVaGetValues (listParent,
135 XmNhorizontalScrollBar, &hsb,
136 XmNverticalScrollBar, &vsb,
137 NULL);
dfe1eee3 138
a91b47e8
JS
139 /* TODO: should scrollbars be affected? Should probably have separate
140 * function to change them (by default, taken from wxSystemSettings)
141 */
a756f210 142 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
a8680e3e
MB
143 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
144 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
a91b47e8
JS
145
146 if (hsb)
147 XtVaSetValues (hsb,
148 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
149 NULL);
150 if (vsb)
151 XtVaSetValues (vsb,
152 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
153 NULL);
154}
155
dfad0599 156int wxFileDialog::ShowModal()
f97c9854 157{
2d120f83 158 wxBeginBusyCursor();
dfe1eee3 159
2d120f83
JS
160 // static char fileBuf[512];
161 Widget parentWidget = (Widget) 0;
162 if (m_parent)
2d120f83 163 parentWidget = (Widget) m_parent->GetTopWidget();
f97c9854 164 else
2d120f83 165 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
ee1aaf99
JS
166 // prepare the arg list
167 Arg args[10];
168 int ac = 0;
169
170 wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour,
171 (wxColour*) NULL);
172
173 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
174 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
175 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
176 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
177
dfe1eee3 178
ee1aaf99 179 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
2d120f83 180 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
a91b47e8
JS
181
182 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
183 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
184 Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST);
185 Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST);
dfe1eee3
VZ
186
187 // code using these vars disabled
188#if 0
a91b47e8
JS
189 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
190 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
191 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
dfe1eee3
VZ
192#endif
193
a91b47e8 194
2d120f83 195 Widget shell = XtParent(fileSel);
dfe1eee3 196
2d120f83 197 if (!m_message.IsNull())
d3a80c92
MB
198 XtVaSetValues(shell,
199 XmNtitle, wxConstCast(m_message.c_str(), char),
200 NULL);
dfe1eee3 201
2d120f83 202 wxString entirePath("");
dfe1eee3 203
2d120f83
JS
204 if ((m_dir != "") && (m_fileName != ""))
205 {
206 entirePath = m_dir + wxString("/") + m_fileName;
207 }
208 else if ((m_dir != "") && (m_fileName == ""))
209 {
210 entirePath = m_dir + wxString("/");
211 }
212 else if ((m_dir == "") && (m_fileName != ""))
213 {
214 entirePath = m_fileName;
215 }
dfe1eee3 216
2d120f83
JS
217 if (m_wildCard != "")
218 {
a4e64fb5
MB
219 // return something understandable by Motif
220 wxString wildCard = ParseWildCard( m_wildCard );
221 wxString filter;
2d120f83 222 if (m_dir != "")
a4e64fb5 223 filter = m_dir + wxString("/") + wildCard;
2d120f83 224 else
a4e64fb5 225 filter = wildCard;
dfe1eee3 226
d3a80c92 227 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
2d120f83
JS
228 XmFileSelectionDoSearch(fileSel, NULL);
229 }
dfe1eee3 230
2d120f83
JS
231 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
232 // file selector on Solaris 1.5.1.
233 if ( m_dir != "" )
234 {
d4d46b56 235 wxXmString thePath( m_dir );
dfe1eee3 236
2d120f83 237 XtVaSetValues (fileSel,
d4d46b56 238 XmNdirectory, thePath(),
2d120f83 239 NULL);
d4d46b56 240 }
dfe1eee3 241
d4d46b56
MB
242 if (entirePath != "")
243 {
d3a80c92
MB
244 XmTextSetString(selectionWidget,
245 wxConstCast(entirePath.c_str(), char));
2d120f83 246 }
dfe1eee3 247
2d120f83
JS
248 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
249 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
a4e64fb5
MB
250 XtAddCallback(fileSel, XmNunmapCallback,
251 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
dfe1eee3 252
2d120f83
JS
253 //#if XmVersion > 1000
254 // I'm not sure about what you mean with XmVersion.
255 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
256 // (Motif1.1.4 ==> XmVersion 1100 )
257 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
258 //
f97c9854 259#if !DEFAULT_FILE_SELECTOR_SIZE
2d120f83
JS
260 int width = wxFSB_WIDTH;
261 int height = wxFSB_HEIGHT;
262 XtVaSetValues(fileSel,
263 XmNwidth, width,
264 XmNheight, height,
265 XmNresizePolicy, XmRESIZE_NONE,
266 NULL);
f97c9854 267#endif
a8680e3e
MB
268 // wxDoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
269 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
270 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
a91b47e8 271
a91b47e8
JS
272 wxChangeListBoxColours(this, dirListWidget);
273 wxChangeListBoxColours(this, fileListWidget);
dfe1eee3 274
2d120f83 275 XtManageChild(fileSel);
a91b47e8 276
2d120f83
JS
277 m_fileSelectorAnswer = "";
278 m_fileSelectorReturned = FALSE;
dfe1eee3 279
2d120f83 280 wxEndBusyCursor();
dfe1eee3 281
2d120f83 282 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
eb6fa4b4 283 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
2d120f83
JS
284 XEvent event;
285 while (!m_fileSelectorReturned)
286 {
eb6fa4b4
MB
287 XtAppNextEvent(context, &event);
288 XtDispatchEvent(&event);
2d120f83
JS
289 }
290 XtRemoveGrab(XtParent(fileSel));
dfe1eee3 291
eb6fa4b4
MB
292 // XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
293
294 Display* display = XtDisplay(fileSel);
dfe1eee3 295
2d120f83
JS
296 XtUnmapWidget(XtParent(fileSel));
297 XtDestroyWidget(XtParent(fileSel));
dfe1eee3 298
2d120f83
JS
299 // Now process all events, because otherwise
300 // this might remain on the screen
eb6fa4b4 301 wxFlushEvents(display);
dfe1eee3 302
2d120f83
JS
303 m_path = m_fileSelectorAnswer;
304 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
305 m_dir = wxPathOnly(m_path);
dfe1eee3 306
2d120f83
JS
307 if (m_fileName == "")
308 return wxID_CANCEL;
309 else
310 return wxID_OK;
4bb6408c
JS
311}
312