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 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
47 #define DEFAULT_FILE_SELECTOR_SIZE 0
48 // Let Motif defines the size of File
49 // Selector Box (if 1), or fix it to
50 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
51 #define wxFSB_WIDTH 600
52 #define wxFSB_HEIGHT 500
55 wxString
wxFileSelector(const char *title
,
56 const char *defaultDir
, const char *defaultFileName
,
57 const char *defaultExtension
, const char *filter
, int flags
,
58 wxWindow
*parent
, int x
, int y
)
60 // If there's a default extension specified but no filter, we create a suitable
64 if ( defaultExtension
&& !filter
)
65 filter2
= wxString("*.") + wxString(defaultExtension
) ;
69 wxString defaultDirString
;
71 defaultDirString
= defaultDir
;
73 defaultDirString
= "";
75 wxString defaultFilenameString
;
77 defaultFilenameString
= defaultFileName
;
79 defaultFilenameString
= "";
81 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
83 if ( fileDialog
.ShowModal() == wxID_OK
)
85 return fileDialog
.GetPath();
91 wxString
wxFileSelectorEx(const char *title
,
92 const char *defaultDir
,
93 const char *defaultFileName
,
94 int* defaultFilterIndex
,
102 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
103 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
105 if ( fileDialog
.ShowModal() == wxID_OK
)
107 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
108 return fileDialog
.GetPath();
111 return wxEmptyString
;
114 wxString
wxFileDialog::m_fileSelectorAnswer
= "";
115 bool wxFileDialog::m_fileSelectorReturned
= FALSE
;
117 void wxFileSelCancel( Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
),
118 XmFileSelectionBoxCallbackStruct
*WXUNUSED(cbs
) )
120 wxFileDialog::m_fileSelectorAnswer
= "";
121 wxFileDialog::m_fileSelectorReturned
= TRUE
;
124 void wxFileSelOk(Widget
WXUNUSED(fs
), XtPointer
WXUNUSED(client_data
), XmFileSelectionBoxCallbackStruct
*cbs
)
126 char *filename
= NULL
;
127 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
128 wxFileDialog::m_fileSelectorAnswer
= "";
129 wxFileDialog::m_fileSelectorReturned
= TRUE
;
132 wxFileDialog::m_fileSelectorAnswer
= filename
;
135 wxFileDialog::m_fileSelectorReturned
= TRUE
;
139 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
140 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
141 long style
, const wxPoint
& pos
)
144 m_dialogStyle
= style
;
147 m_fileName
= defaultFileName
;
149 m_wildCard
= wildCard
;
154 static void wxChangeListBoxColours(wxWindow
* WXUNUSED(win
), Widget widget
)
156 wxWindow::DoChangeBackgroundColour((WXWidget
) widget
, *wxWHITE
);
158 // Change colour of the scrolled areas of the listboxes
159 Widget listParent
= XtParent (widget
);
160 wxWindow::DoChangeBackgroundColour((WXWidget
) listParent
, *wxWHITE
, TRUE
);
162 Widget hsb
= (Widget
) 0;
163 Widget vsb
= (Widget
) 0;
164 XtVaGetValues (listParent
,
165 XmNhorizontalScrollBar
, &hsb
,
166 XmNverticalScrollBar
, &vsb
,
169 /* TODO: should scrollbars be affected? Should probably have separate
170 * function to change them (by default, taken from wxSystemSettings)
172 wxColour backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
173 wxWindow::DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
174 wxWindow::DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
178 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
182 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
186 int wxFileDialog::ShowModal()
190 // static char fileBuf[512];
191 Widget parentWidget
= (Widget
) 0;
194 parentWidget
= (Widget
) m_parent
->GetTopWidget();
197 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
198 // prepare the arg list
202 wxComputeColours (XtDisplay(parentWidget
), & m_backgroundColour
,
205 XtSetArg(args
[ac
], XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
); ac
++;
206 XtSetArg(args
[ac
], XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
); ac
++;
207 XtSetArg(args
[ac
], XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
); ac
++;
208 XtSetArg(args
[ac
], XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
); ac
++;
211 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", args
, ac
);
212 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
214 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
215 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
216 Widget dirListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_DIR_LIST
);
217 Widget fileListWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_LIST
);
219 // code using these vars disabled
221 Widget okWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_OK_BUTTON
);
222 Widget applyWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_APPLY_BUTTON
);
223 Widget cancelWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_CANCEL_BUTTON
);
227 Widget shell
= XtParent(fileSel
);
229 if (!m_message
.IsNull())
230 XtVaSetValues(shell
, XmNtitle
, (char*) (const char*) m_message
, NULL
);
232 wxString
entirePath("");
234 if ((m_dir
!= "") && (m_fileName
!= ""))
236 entirePath
= m_dir
+ wxString("/") + m_fileName
;
238 else if ((m_dir
!= "") && (m_fileName
== ""))
240 entirePath
= m_dir
+ wxString("/");
242 else if ((m_dir
== "") && (m_fileName
!= ""))
244 entirePath
= m_fileName
;
247 if (entirePath
!= "")
249 XmTextSetString(selectionWidget
, (char*) (const char*) entirePath
);
252 if (m_wildCard
!= "")
256 filter
= m_dir
+ wxString("/") + m_wildCard
;
260 XmTextSetString(filterWidget
, (char*) (const char*) filter
);
261 XmFileSelectionDoSearch(fileSel
, NULL
);
264 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
265 // file selector on Solaris 1.5.1.
268 XmString thePath
= XmStringCreateLtoR ((char*) (const char*) m_dir
,
269 XmSTRING_DEFAULT_CHARSET
);
271 XtVaSetValues (fileSel
,
272 XmNdirectory
, thePath
,
275 XmStringFree(thePath
);
278 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
279 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
281 //#if XmVersion > 1000
282 // I'm not sure about what you mean with XmVersion.
283 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
284 // (Motif1.1.4 ==> XmVersion 1100 )
285 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
287 #if !DEFAULT_FILE_SELECTOR_SIZE
288 int width
= wxFSB_WIDTH
;
289 int height
= wxFSB_HEIGHT
;
290 XtVaSetValues(fileSel
,
293 XmNresizePolicy
, XmRESIZE_NONE
,
296 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
297 DoChangeBackgroundColour((WXWidget
) filterWidget
, *wxWHITE
);
298 DoChangeBackgroundColour((WXWidget
) selectionWidget
, *wxWHITE
);
300 wxChangeListBoxColours(this, dirListWidget
);
301 wxChangeListBoxColours(this, fileListWidget
);
303 XtManageChild(fileSel
);
305 m_fileSelectorAnswer
= "";
306 m_fileSelectorReturned
= FALSE
;
310 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
312 while (!m_fileSelectorReturned
)
314 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
316 XtRemoveGrab(XtParent(fileSel
));
318 XmUpdateDisplay((Widget
) wxTheApp
->GetTopLevelWidget()); // Experimental
320 // XtDestroyWidget(fileSel);
321 XtUnmapWidget(XtParent(fileSel
));
322 XtDestroyWidget(XtParent(fileSel
));
324 // Now process all events, because otherwise
325 // this might remain on the screen
326 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
327 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
329 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
330 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
331 XtDispatchEvent(&event
);
334 m_path
= m_fileSelectorAnswer
;
335 m_fileName
= wxFileNameFromPath(m_fileSelectorAnswer
);
336 m_dir
= wxPathOnly(m_path
);
338 if (m_fileName
== "")
344 // Generic file load/save dialog
346 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
348 char *ext
= (char *)extension
;
353 str
= _("Load %s file");
355 str
= _("Save %s file");
356 prompt
.Printf(str
, what
);
361 wild
.Printf("*.%s", ext
);
363 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
366 // Generic file load dialog
367 wxString
wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
369 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
373 // Generic file save dialog
374 wxString
wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
376 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);