1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
21 #define XtParent XTPARENT
22 #define XtWindow XTWINDOW
27 #include "wx/filedlg.h"
30 #include "wx/settings.h"
31 #include "wx/tokenzr.h"
34 #pragma message disable nosimpint
37 #include <Xm/MwmUtil.h>
39 #include <Xm/BulletinB.h>
42 #include <Xm/DialogS.h>
43 #include <Xm/FileSB.h>
44 #include <Xm/RowColumn.h>
45 #include <Xm/LabelG.h>
47 #pragma message enable nosimpint
50 #include "wx/motif/private.h"
52 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
54 #define DEFAULT_FILE_SELECTOR_SIZE 0
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)
58 #define wxFSB_WIDTH 600
59 #define wxFSB_HEIGHT 500
62 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
63 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
65 static void wxFileSelClose(Widget
WXUNUSED(w
),
66 void* WXUNUSED(client_data
),
67 XmAnyCallbackStruct
*WXUNUSED(call_data
))
69 wxFileDialog::m_fileSelectorAnswer
= "";
70 wxFileDialog::m_fileSelectorReturned
= TRUE
;
73 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
74 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
76 wxFileDialog::m_fileSelectorAnswer
= "";
77 wxFileDialog::m_fileSelectorReturned
= TRUE
;
80 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
82 char *filename
= NULL
;
83 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
84 wxFileDialog::m_fileSelectorAnswer
= "";
85 wxFileDialog::m_fileSelectorReturned
= TRUE
;
88 wxFileDialog::m_fileSelectorAnswer
= filename
;
91 wxFileDialog::m_fileSelectorReturned
= TRUE
;
95 static wxString
ParseWildCard( const wxString
& wild
)
98 static const wxChar
* msg
=
99 _T("Motif file dialog does not understand this ")
100 _T("wildcard syntax");
103 wxStringTokenizer
tok( wild
, _T("|") );
105 wxCHECK_MSG( tok
.CountTokens() <= 2, _T("*.*"), msg
);
107 if( tok
.CountTokens() == 1 ) return wild
;
111 wxStringTokenizer
tok2( tok
.GetNextToken(), _T(";") );
113 wxCHECK_MSG( tok2
.CountTokens() == 1, tok2
.GetNextToken(), msg
);
114 return tok2
.GetNextToken();
117 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
118 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
119 long style
, const wxPoint
& pos
)
120 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
125 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
127 wxDoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
129 // Change colour of the scrolled areas of the listboxes
130 Widget listParent
= XtParent (widget
);
132 wxDoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
135 Widget hsb
= (Widget
) 0;
136 Widget vsb
= (Widget
) 0;
137 XtVaGetValues (listParent
,
138 XmNhorizontalScrollBar
, &hsb
,
139 XmNverticalScrollBar
, &vsb
,
142 /* TODO: should scrollbars be affected? Should probably have separate
143 * function to change them (by default, taken from wxSystemSettings)
145 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
146 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
147 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
151 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
155 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
159 int wxFileDialog::ShowModal()
163 // static char fileBuf[512];
164 Widget parentWidget
= (Widget
) 0;
166 parentWidget
= (Widget
) m_parent
->GetTopWidget();
168 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
169 // prepare the arg list
173 wxComputeColours (XtDisplay(parentWidget
), & m_backgroundColour
,
176 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
177 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
178 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
179 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
182 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", args
, ac
);
183 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
185 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
186 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
187 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
188 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
190 // code using these vars disabled
192 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
193 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
194 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
198 Widget shell
= XtParent(fileSel
);
200 if (!m_message
.IsNull())
202 XmNtitle
, wxConstCast(m_message
.c_str(), char),
205 wxString
entirePath("");
207 if ((m_dir
!= "") && (m_fileName
!= ""))
209 entirePath
= m_dir
+ wxString("/") + m_fileName
;
211 else if ((m_dir
!= "") && (m_fileName
== ""))
213 entirePath
= m_dir
+ wxString("/");
215 else if ((m_dir
== "") && (m_fileName
!= ""))
217 entirePath
= m_fileName
;
220 if (m_wildCard
!= "")
222 // return something understandable by Motif
223 wxString wildCard
= ParseWildCard( m_wildCard
);
226 filter
= m_dir
+ wxString("/") + wildCard
;
230 XmTextSetString(filterWidget
, wxConstCast(filter
.c_str(), char));
231 XmFileSelectionDoSearch(fileSel
, NULL
);
234 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
235 // file selector on Solaris 1.5.1.
238 wxXmString
thePath( m_dir
);
240 XtVaSetValues (fileSel
,
241 XmNdirectory
, thePath(),
245 if (entirePath
!= "")
247 XmTextSetString(selectionWidget
,
248 wxConstCast(entirePath
.c_str(), char));
251 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
252 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
253 XtAddCallback(fileSel
, XmNunmapCallback
,
254 (XtCallbackProc
)wxFileSelClose
, (XtPointer
)this);
256 //#if XmVersion > 1000
257 // I'm not sure about what you mean with XmVersion.
258 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
259 // (Motif1.1.4 ==> XmVersion 1100 )
260 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
262 #if !DEFAULT_FILE_SELECTOR_SIZE
263 int width
= wxFSB_WIDTH
;
264 int height
= wxFSB_HEIGHT
;
265 XtVaSetValues(fileSel
,
268 XmNresizePolicy
, XmRESIZE_NONE
,
271 // wxDoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
272 wxDoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
273 wxDoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
275 wxChangeListBoxColours(this, dirListWidget
);
276 wxChangeListBoxColours(this, fileListWidget
);
278 XtManageChild(fileSel
);
280 m_fileSelectorAnswer
= "";
281 m_fileSelectorReturned
= FALSE
;
285 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
286 XtAppContext context
= (XtAppContext
) wxTheApp
->GetAppContext();
288 while (!m_fileSelectorReturned
)
290 XtAppNextEvent(context
, &event
);
291 XtDispatchEvent(&event
);
293 XtRemoveGrab(XtParent(fileSel
));
295 // XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
297 Display
* display
= XtDisplay(fileSel
);
299 XtUnmapWidget(XtParent(fileSel
));
300 XtDestroyWidget(XtParent(fileSel
));
302 // Now process all events, because otherwise
303 // this might remain on the screen
304 wxFlushEvents(display
);
306 m_path
= m_fileSelectorAnswer
;
307 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
308 m_dir
= wxPathOnly(m_path
);
310 if (m_fileName
== "")