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 #include <Xm/MwmUtil.h>
27 #include <Xm/BulletinB.h>
30 #include <Xm/DialogS.h>
31 #include <Xm/FileSB.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/LabelG.h>
35 #include "wx/motif/private.h"
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
41 #define DEFAULT_FILE_SELECTOR_SIZE 0
42 // Let Motif defines the size of File
43 // Selector Box (if 1), or fix it to
44 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
45 #define wxFSB_WIDTH 600
46 #define wxFSB_HEIGHT 500
49 wxString
wxFileSelector(const char *title
,
50 const char *defaultDir
, const char *defaultFileName
,
51 const char *defaultExtension
, const char *filter
, int flags
,
52 wxWindow
*parent
, int x
, int y
)
54 // If there's a default extension specified but no filter, we create a suitable
58 if ( defaultExtension
&& !filter
)
59 filter2
= wxString("*.") + wxString(defaultExtension
) ;
63 wxString defaultDirString
;
65 defaultDirString
= defaultDir
;
67 defaultDirString
= "";
69 wxString defaultFilenameString
;
71 defaultFilenameString
= defaultFileName
;
73 defaultFilenameString
= "";
75 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
77 if ( fileDialog
.ShowModal() == wxID_OK
)
79 return fileDialog
.GetPath();
85 wxString
wxFileSelectorEx(const char *title
,
86 const char *defaultDir
,
87 const char *defaultFileName
,
88 int* defaultFilterIndex
,
96 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
97 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
99 if ( fileDialog
.ShowModal() == wxID_OK
)
101 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
102 return fileDialog
.GetPath();
105 return wxEmptyString
;
108 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
109 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
111 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
112 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
114 wxFileDialog::m_fileSelectorAnswer
= "";
115 wxFileDialog::m_fileSelectorReturned
= TRUE
;
118 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
120 char *filename
= NULL
;
121 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
122 wxFileDialog::m_fileSelectorAnswer
= "";
123 wxFileDialog::m_fileSelectorReturned
= TRUE
;
126 wxFileDialog::m_fileSelectorAnswer
= filename
;
129 wxFileDialog::m_fileSelectorReturned
= TRUE
;
133 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
134 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
135 long style
, const wxPoint
& pos
)
138 m_dialogStyle
= style
;
141 m_fileName
= defaultFileName
;
143 m_wildCard
= wildCard
;
148 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
150 wxWindow::DoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
152 // Change colour of the scrolled areas of the listboxes
153 Widget listParent
= XtParent (widget
);
154 wxWindow::DoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
156 Widget hsb
= (Widget
) 0;
157 Widget vsb
= (Widget
) 0;
158 XtVaGetValues (listParent
,
159 XmNhorizontalScrollBar
, &hsb
,
160 XmNverticalScrollBar
, &vsb
,
163 /* TODO: should scrollbars be affected? Should probably have separate
164 * function to change them (by default, taken from wxSystemSettings)
166 wxColour backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
167 wxWindow::DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
168 wxWindow::DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
172 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
176 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
180 int wxFileDialog::ShowModal()
184 // static char fileBuf[512];
185 Widget parentWidget
= (Widget
) 0;
188 parentWidget
= (Widget
) m_parent
->GetTopWidget();
191 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
192 // prepare the arg list
196 wxComputeColours (XtDisplay(parentWidget
), & m_backgroundColour
,
199 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
200 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
201 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
202 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
205 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", args
, ac
);
206 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
208 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
209 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
210 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
211 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
213 // code using these vars disabled
215 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
216 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
217 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
221 Widget shell
= XtParent(fileSel
);
223 if (!m_message
.IsNull())
224 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
226 wxString
entirePath("");
228 if ((m_dir
!= "") && (m_fileName
!= ""))
230 entirePath
= m_dir
+ wxString("/") + m_fileName
;
232 else if ((m_dir
!= "") && (m_fileName
== ""))
234 entirePath
= m_dir
+ wxString("/");
236 else if ((m_dir
== "") && (m_fileName
!= ""))
238 entirePath
= m_fileName
;
241 if (entirePath
!= "")
243 XmTextSetString(selectionWidget
, (char*) (const char*) entirePath
);
246 if (m_wildCard
!= "")
250 filter
= m_dir
+ wxString("/") + m_wildCard
;
254 XmTextSetString(filterWidget
, (char*) (const char*) filter
);
255 XmFileSelectionDoSearch(fileSel
, NULL
);
258 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
259 // file selector on Solaris 1.5.1.
262 XmString thePath
= XmStringCreateLtoR ((char*) (const char*) m_dir
,
263 XmSTRING_DEFAULT_CHARSET
);
265 XtVaSetValues (fileSel
,
266 XmNdirectory
, thePath
,
269 XmStringFree(thePath
);
272 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
273 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
275 //#if XmVersion > 1000
276 // I'm not sure about what you mean with XmVersion.
277 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
278 // (Motif1.1.4 ==> XmVersion 1100 )
279 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
281 #if !DEFAULT_FILE_SELECTOR_SIZE
282 int width
= wxFSB_WIDTH
;
283 int height
= wxFSB_HEIGHT
;
284 XtVaSetValues(fileSel
,
287 XmNresizePolicy
, XmRESIZE_NONE
,
290 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
291 DoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
292 DoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
294 wxChangeListBoxColours(this, dirListWidget
);
295 wxChangeListBoxColours(this, fileListWidget
);
297 XtManageChild(fileSel
);
299 m_fileSelectorAnswer
= "";
300 m_fileSelectorReturned
= FALSE
;
304 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
306 while (!m_fileSelectorReturned
)
308 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
310 XtRemoveGrab(XtParent(fileSel
));
312 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
314 // XtDestroyWidget(fileSel);
315 XtUnmapWidget(XtParent(fileSel
));
316 XtDestroyWidget(XtParent(fileSel
));
318 // Now process all events, because otherwise
319 // this might remain on the screen
320 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
321 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
323 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
324 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
325 XtDispatchEvent(&event
);
328 m_path
= m_fileSelectorAnswer
;
329 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
330 m_dir
= wxPathOnly(m_path
);
332 if (m_fileName
== "")
338 // Generic file load/save dialog
340 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
342 char *ext
= (char *)extension
;
347 str
= _("Load %s file");
349 str
= _("Save %s file");
350 prompt
.Printf(str
, what
);
355 wild
.Printf("*.%s", ext
);
357 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
360 // Generic file load dialog
361 wxString
wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
363 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
367 // Generic file save dialog
368 wxString
wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
370 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);