1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
18 #include "wx/dialog.h"
19 #include "wx/filedlg.h"
22 #include "wx/settings.h"
25 #pragma message disable nosimpint
28 #include <Xm/MwmUtil.h>
30 #include <Xm/BulletinB.h>
33 #include <Xm/DialogS.h>
34 #include <Xm/FileSB.h>
35 #include <Xm/RowColumn.h>
36 #include <Xm/LabelG.h>
38 #pragma message enable nosimpint
41 #include "wx/motif/private.h"
43 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
45 #define DEFAULT_FILE_SELECTOR_SIZE 0
46 // Let Motif defines the size of File
47 // Selector Box (if 1), or fix it to
48 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
49 #define wxFSB_WIDTH 600
50 #define wxFSB_HEIGHT 500
53 wxString
wxFileSelector(const char *title
,
54 const char *defaultDir
, const char *defaultFileName
,
55 const char *defaultExtension
, const char *filter
, int flags
,
56 wxWindow
*parent
, int x
, int y
)
58 // If there's a default extension specified but no filter, we create a suitable
62 if ( defaultExtension
&& !filter
)
63 filter2
= wxString("*.") + wxString(defaultExtension
) ;
67 wxString defaultDirString
;
69 defaultDirString
= defaultDir
;
71 defaultDirString
= "";
73 wxString defaultFilenameString
;
75 defaultFilenameString
= defaultFileName
;
77 defaultFilenameString
= "";
79 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
81 if ( fileDialog
.ShowModal() == wxID_OK
)
83 return fileDialog
.GetPath();
89 wxString
wxFileSelectorEx(const char *title
,
90 const char *defaultDir
,
91 const char *defaultFileName
,
92 int* defaultFilterIndex
,
100 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
101 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
103 if ( fileDialog
.ShowModal() == wxID_OK
)
105 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
106 return fileDialog
.GetPath();
109 return wxEmptyString
;
112 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
113 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
115 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
116 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
118 wxFileDialog::m_fileSelectorAnswer
= "";
119 wxFileDialog::m_fileSelectorReturned
= TRUE
;
122 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
124 char *filename
= NULL
;
125 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
126 wxFileDialog::m_fileSelectorAnswer
= "";
127 wxFileDialog::m_fileSelectorReturned
= TRUE
;
130 wxFileDialog::m_fileSelectorAnswer
= filename
;
133 wxFileDialog::m_fileSelectorReturned
= TRUE
;
137 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
138 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
139 long style
, const wxPoint
& pos
)
142 m_dialogStyle
= style
;
145 m_fileName
= defaultFileName
;
147 m_wildCard
= wildCard
;
152 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
154 wxWindow::DoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
156 // Change colour of the scrolled areas of the listboxes
157 Widget listParent
= XtParent (widget
);
158 wxWindow::DoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
160 Widget hsb
= (Widget
) 0;
161 Widget vsb
= (Widget
) 0;
162 XtVaGetValues (listParent
,
163 XmNhorizontalScrollBar
, &hsb
,
164 XmNverticalScrollBar
, &vsb
,
167 /* TODO: should scrollbars be affected? Should probably have separate
168 * function to change them (by default, taken from wxSystemSettings)
170 wxColour backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
171 wxWindow::DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
172 wxWindow::DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
176 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
180 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
184 int wxFileDialog::ShowModal()
188 // static char fileBuf[512];
189 Widget parentWidget
= (Widget
) 0;
192 parentWidget
= (Widget
) m_parent
->GetTopWidget();
195 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
196 // prepare the arg list
200 wxComputeColours (XtDisplay(parentWidget
), & m_backgroundColour
,
203 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
204 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
205 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
206 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
209 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", args
, ac
);
210 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
212 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
213 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
214 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
215 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
217 // code using these vars disabled
219 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
220 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
221 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
225 Widget shell
= XtParent(fileSel
);
227 if (!m_message
.IsNull())
228 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
230 wxString
entirePath("");
232 if ((m_dir
!= "") && (m_fileName
!= ""))
234 entirePath
= m_dir
+ wxString("/") + m_fileName
;
236 else if ((m_dir
!= "") && (m_fileName
== ""))
238 entirePath
= m_dir
+ wxString("/");
240 else if ((m_dir
== "") && (m_fileName
!= ""))
242 entirePath
= m_fileName
;
245 if (entirePath
!= "")
247 XmTextSetString(selectionWidget
, (char*) (const char*) entirePath
);
250 if (m_wildCard
!= "")
254 filter
= m_dir
+ wxString("/") + m_wildCard
;
258 XmTextSetString(filterWidget
, (char*) (const char*) filter
);
259 XmFileSelectionDoSearch(fileSel
, NULL
);
262 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
263 // file selector on Solaris 1.5.1.
266 XmString thePath
= XmStringCreateLtoR ((char*) (const char*) m_dir
,
267 XmSTRING_DEFAULT_CHARSET
);
269 XtVaSetValues (fileSel
,
270 XmNdirectory
, thePath
,
273 XmStringFree(thePath
);
276 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
277 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
279 //#if XmVersion > 1000
280 // I'm not sure about what you mean with XmVersion.
281 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
282 // (Motif1.1.4 ==> XmVersion 1100 )
283 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
285 #if !DEFAULT_FILE_SELECTOR_SIZE
286 int width
= wxFSB_WIDTH
;
287 int height
= wxFSB_HEIGHT
;
288 XtVaSetValues(fileSel
,
291 XmNresizePolicy
, XmRESIZE_NONE
,
294 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
295 DoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
296 DoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
298 wxChangeListBoxColours(this, dirListWidget
);
299 wxChangeListBoxColours(this, fileListWidget
);
301 XtManageChild(fileSel
);
303 m_fileSelectorAnswer
= "";
304 m_fileSelectorReturned
= FALSE
;
308 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
310 while (!m_fileSelectorReturned
)
312 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
314 XtRemoveGrab(XtParent(fileSel
));
316 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
318 // XtDestroyWidget(fileSel);
319 XtUnmapWidget(XtParent(fileSel
));
320 XtDestroyWidget(XtParent(fileSel
));
322 // Now process all events, because otherwise
323 // this might remain on the screen
324 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
325 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
327 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
328 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
329 XtDispatchEvent(&event
);
332 m_path
= m_fileSelectorAnswer
;
333 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
334 m_dir
= wxPathOnly(m_path
);
336 if (m_fileName
== "")
342 // Generic file load/save dialog
344 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
346 char *ext
= (char *)extension
;
351 str
= _("Load %s file");
353 str
= _("Save %s file");
354 prompt
.Printf(str
, what
);
359 wild
.Printf("*.%s", ext
);
361 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
364 // Generic file load dialog
365 wxString
wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
367 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
371 // Generic file save dialog
372 wxString
wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
374 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);