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