1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
19 #define XtWindow XTWINDOW
24 #include "wx/dialog.h"
25 #include "wx/filedlg.h"
28 #include "wx/settings.h"
29 #include "wx/tokenzr.h"
32 #pragma message disable nosimpint
35 #include <Xm/MwmUtil.h>
37 #include <Xm/BulletinB.h>
40 #include <Xm/DialogS.h>
41 #include <Xm/FileSB.h>
42 #include <Xm/RowColumn.h>
43 #include <Xm/LabelG.h>
45 #pragma message enable nosimpint
48 #include "wx/motif/private.h"
50 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
52 #define DEFAULT_FILE_SELECTOR_SIZE 0
53 // Let Motif defines the size of File
54 // Selector Box (if 1), or fix it to
55 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
56 #define wxFSB_WIDTH 600
57 #define wxFSB_HEIGHT 500
60 wxString
wxFileSelector(const char *title
,
61 const char *defaultDir
, const char *defaultFileName
,
62 const char *defaultExtension
, const char *filter
, int flags
,
63 wxWindow
*parent
, int x
, int y
)
65 // If there's a default extension specified but no filter, we create a suitable
69 if ( defaultExtension
&& !filter
)
70 filter2
= wxString("*.") + wxString(defaultExtension
) ;
74 wxString defaultDirString
;
76 defaultDirString
= defaultDir
;
78 defaultDirString
= "";
80 wxString defaultFilenameString
;
82 defaultFilenameString
= defaultFileName
;
84 defaultFilenameString
= "";
86 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
88 if ( fileDialog
.ShowModal() == wxID_OK
)
90 return fileDialog
.GetPath();
96 wxString
wxFileSelectorEx(const char *title
,
97 const char *defaultDir
,
98 const char *defaultFileName
,
99 int* defaultFilterIndex
,
107 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
108 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
110 if ( fileDialog
.ShowModal() == wxID_OK
)
112 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
113 return fileDialog
.GetPath();
116 return wxEmptyString
;
119 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
120 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
122 static void wxFileSelClose(Widget
WXUNUSED(w
),
123 void* WXUNUSED(client_data
),
124 XmAnyCallbackStruct
*WXUNUSED(call_data
))
126 wxFileDialog::m_fileSelectorAnswer
= "";
127 wxFileDialog::m_fileSelectorReturned
= TRUE
;
130 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
131 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
133 wxFileDialog::m_fileSelectorAnswer
= "";
134 wxFileDialog::m_fileSelectorReturned
= TRUE
;
137 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
139 char *filename
= NULL
;
140 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
141 wxFileDialog::m_fileSelectorAnswer
= "";
142 wxFileDialog::m_fileSelectorReturned
= TRUE
;
145 wxFileDialog::m_fileSelectorAnswer
= filename
;
148 wxFileDialog::m_fileSelectorReturned
= TRUE
;
152 static wxString
ParseWildCard( const wxString
& wild
)
154 static const wxChar
* msg
=
155 _T("Motif file dialog does not understand this ")
156 _T("wildcard syntax");
158 wxStringTokenizer
tok( wild
, _T("|") );
160 wxCHECK_MSG( tok
.CountTokens() <= 2, _T("*.*"), msg
);
162 if( tok
.CountTokens() == 1 ) return wild
;
166 wxStringTokenizer
tok2( tok
.GetNextToken(), _T(";") );
168 wxCHECK_MSG( tok2
.CountTokens() == 1, tok2
.GetNextToken(), msg
);
169 return tok2
.GetNextToken();
172 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
173 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
174 long style
, const wxPoint
& pos
)
177 m_dialogStyle
= style
;
180 m_fileName
= defaultFileName
;
182 m_wildCard
= wildCard
;
187 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
189 wxWindow::DoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
191 // Change colour of the scrolled areas of the listboxes
192 Widget listParent
= XtParent (widget
);
194 wxWindow::DoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
197 Widget hsb
= (Widget
) 0;
198 Widget vsb
= (Widget
) 0;
199 XtVaGetValues (listParent
,
200 XmNhorizontalScrollBar
, &hsb
,
201 XmNverticalScrollBar
, &vsb
,
204 /* TODO: should scrollbars be affected? Should probably have separate
205 * function to change them (by default, taken from wxSystemSettings)
207 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
208 wxWindow::DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
209 wxWindow::DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
213 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
217 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
221 int wxFileDialog::ShowModal()
225 // static char fileBuf[512];
226 Widget parentWidget
= (Widget
) 0;
229 parentWidget
= (Widget
) m_parent
->GetTopWidget();
232 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
233 // prepare the arg list
237 wxComputeColours (XtDisplay(parentWidget
), & m_backgroundColour
,
240 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
241 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
242 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
243 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
246 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", args
, ac
);
247 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
249 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
250 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
251 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
252 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
254 // code using these vars disabled
256 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
257 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
258 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
262 Widget shell
= XtParent(fileSel
);
264 if (!m_message
.IsNull())
265 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
267 wxString
entirePath("");
269 if ((m_dir
!= "") && (m_fileName
!= ""))
271 entirePath
= m_dir
+ wxString("/") + m_fileName
;
273 else if ((m_dir
!= "") && (m_fileName
== ""))
275 entirePath
= m_dir
+ wxString("/");
277 else if ((m_dir
== "") && (m_fileName
!= ""))
279 entirePath
= m_fileName
;
282 if (m_wildCard
!= "")
284 // return something understandable by Motif
285 wxString wildCard
= ParseWildCard( m_wildCard
);
288 filter
= m_dir
+ wxString("/") + wildCard
;
292 XmTextSetString(filterWidget
, (char*)filter
.c_str());
293 XmFileSelectionDoSearch(fileSel
, NULL
);
296 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
297 // file selector on Solaris 1.5.1.
300 wxXmString
thePath( m_dir
);
302 XtVaSetValues (fileSel
,
303 XmNdirectory
, thePath(),
307 if (entirePath
!= "")
309 XmTextSetString(selectionWidget
, (char*)entirePath
.c_str());
312 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
313 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
314 XtAddCallback(fileSel
, XmNunmapCallback
,
315 (XtCallbackProc
)wxFileSelClose
, (XtPointer
)this);
317 //#if XmVersion > 1000
318 // I'm not sure about what you mean with XmVersion.
319 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
320 // (Motif1.1.4 ==> XmVersion 1100 )
321 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
323 #if !DEFAULT_FILE_SELECTOR_SIZE
324 int width
= wxFSB_WIDTH
;
325 int height
= wxFSB_HEIGHT
;
326 XtVaSetValues(fileSel
,
329 XmNresizePolicy
, XmRESIZE_NONE
,
332 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
333 DoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
334 DoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
336 wxChangeListBoxColours(this, dirListWidget
);
337 wxChangeListBoxColours(this, fileListWidget
);
339 XtManageChild(fileSel
);
341 m_fileSelectorAnswer
= "";
342 m_fileSelectorReturned
= FALSE
;
346 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
348 while (!m_fileSelectorReturned
)
350 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
352 XtRemoveGrab(XtParent(fileSel
));
354 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
356 // XtDestroyWidget(fileSel);
357 XtUnmapWidget(XtParent(fileSel
));
358 XtDestroyWidget(XtParent(fileSel
));
360 // Now process all events, because otherwise
361 // this might remain on the screen
362 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
363 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
365 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
366 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
367 XtDispatchEvent(&event
);
370 m_path
= m_fileSelectorAnswer
;
371 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
372 m_dir
= wxPathOnly(m_path
);
374 if (m_fileName
== "")
380 // Generic file load/save dialog
382 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
384 char *ext
= (char *)extension
;
389 str
= _("Load %s file");
391 str
= _("Save %s file");
392 prompt
.Printf(str
, what
);
397 wild
.Printf("*.%s", ext
);
399 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
402 // Generic file load dialog
403 wxString
wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
405 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
409 // Generic file save dialog
410 wxString
wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
412 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);