]> git.saurik.com Git - wxWidgets.git/blame - src/motif/filedlg.cpp
one more note
[wxWidgets.git] / src / motif / filedlg.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: filedlg.cpp
3// Purpose: wxFileDialog
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "filedlg.h"
14#endif
15
16#include "wx/defs.h"
17#include "wx/utils.h"
18#include "wx/dialog.h"
19#include "wx/filedlg.h"
20#include "wx/intl.h"
dfad0599 21#include "wx/app.h"
a91b47e8 22#include "wx/settings.h"
4bb6408c 23
f97c9854 24#include <Xm/Xm.h>
dfad0599
JS
25#include <Xm/MwmUtil.h>
26#include <Xm/Label.h>
27#include <Xm/BulletinB.h>
28#include <Xm/Frame.h>
29#include <Xm/Text.h>
30#include <Xm/DialogS.h>
31#include <Xm/FileSB.h>
32#include <Xm/RowColumn.h>
33#include <Xm/LabelG.h>
f97c9854 34
ee1aaf99
JS
35#include "wx/motif/private.h"
36
4bb6408c
JS
37#if !USE_SHARED_LIBRARY
38IMPLEMENT_CLASS(wxFileDialog, wxDialog)
39#endif
40
dfe1eee3 41#define DEFAULT_FILE_SELECTOR_SIZE 0
2d120f83
JS
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)
dfe1eee3 45#define wxFSB_WIDTH 600
dfad0599
JS
46#define wxFSB_HEIGHT 500
47
48
7482b220 49wxString wxFileSelector(const char *title,
4bb6408c
JS
50 const char *defaultDir, const char *defaultFileName,
51 const char *defaultExtension, const char *filter, int flags,
52 wxWindow *parent, int x, int y)
53{
54 // If there's a default extension specified but no filter, we create a suitable
55 // filter.
dfe1eee3 56
4bb6408c
JS
57 wxString filter2("");
58 if ( defaultExtension && !filter )
59 filter2 = wxString("*.") + wxString(defaultExtension) ;
60 else if ( filter )
61 filter2 = filter;
dfe1eee3 62
4bb6408c
JS
63 wxString defaultDirString;
64 if (defaultDir)
65 defaultDirString = defaultDir;
66 else
67 defaultDirString = "";
dfe1eee3 68
4bb6408c
JS
69 wxString defaultFilenameString;
70 if (defaultFileName)
71 defaultFilenameString = defaultFileName;
72 else
73 defaultFilenameString = "";
dfe1eee3 74
4bb6408c 75 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
dfe1eee3 76
4bb6408c
JS
77 if ( fileDialog.ShowModal() == wxID_OK )
78 {
7482b220 79 return fileDialog.GetPath();
4bb6408c
JS
80 }
81 else
7482b220 82 return wxEmptyString;
4bb6408c
JS
83}
84
7482b220 85wxString wxFileSelectorEx(const char *title,
4bb6408c
JS
86 const char *defaultDir,
87 const char *defaultFileName,
88 int* defaultFilterIndex,
89 const char *filter,
90 int flags,
91 wxWindow* parent,
92 int x,
93 int y)
dfe1eee3 94
4bb6408c
JS
95{
96 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
97 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
dfe1eee3 98
4bb6408c
JS
99 if ( fileDialog.ShowModal() == wxID_OK )
100 {
101 *defaultFilterIndex = fileDialog.GetFilterIndex();
c330a2cf 102 return fileDialog.GetPath();
4bb6408c
JS
103 }
104 else
7482b220 105 return wxEmptyString;
4bb6408c
JS
106}
107
dfad0599
JS
108wxString wxFileDialog::m_fileSelectorAnswer = "";
109bool wxFileDialog::m_fileSelectorReturned = FALSE;
f97c9854 110
dfe1eee3 111void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
2d120f83 112 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
f97c9854 113{
2d120f83
JS
114 wxFileDialog::m_fileSelectorAnswer = "";
115 wxFileDialog::m_fileSelectorReturned = TRUE;
f97c9854
JS
116}
117
f9e02ac7 118void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
f97c9854 119{
2d120f83
JS
120 char *filename = NULL;
121 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
122 wxFileDialog::m_fileSelectorAnswer = "";
123 wxFileDialog::m_fileSelectorReturned = TRUE;
124 } else {
125 if (filename) {
126 wxFileDialog::m_fileSelectorAnswer = filename;
127 XtFree(filename);
128 }
129 wxFileDialog::m_fileSelectorReturned = TRUE;
f97c9854 130 }
f97c9854
JS
131}
132
dfad0599 133wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
2d120f83
JS
134 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
135 long style, const wxPoint& pos)
dfad0599
JS
136{
137 m_message = message;
138 m_dialogStyle = style;
139 m_parent = parent;
140 m_path = "";
141 m_fileName = defaultFileName;
142 m_dir = defaultDir;
143 m_wildCard = wildCard;
144 m_filterIndex = 1;
145 m_pos = pos;
146}
f97c9854 147
af111fc3 148static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
a91b47e8 149{
dfe1eee3 150 wxWindow::DoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
a91b47e8
JS
151
152 // Change colour of the scrolled areas of the listboxes
153 Widget listParent = XtParent (widget);
dfe1eee3 154 wxWindow::DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
a91b47e8
JS
155
156 Widget hsb = (Widget) 0;
157 Widget vsb = (Widget) 0;
158 XtVaGetValues (listParent,
159 XmNhorizontalScrollBar, &hsb,
160 XmNverticalScrollBar, &vsb,
161 NULL);
dfe1eee3 162
a91b47e8
JS
163 /* TODO: should scrollbars be affected? Should probably have separate
164 * function to change them (by default, taken from wxSystemSettings)
165 */
166 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
dfe1eee3
VZ
167 wxWindow::DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
168 wxWindow::DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
a91b47e8
JS
169
170 if (hsb)
171 XtVaSetValues (hsb,
172 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
173 NULL);
174 if (vsb)
175 XtVaSetValues (vsb,
176 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
177 NULL);
178}
179
dfad0599 180int wxFileDialog::ShowModal()
f97c9854 181{
2d120f83 182 wxBeginBusyCursor();
dfe1eee3 183
2d120f83
JS
184 // static char fileBuf[512];
185 Widget parentWidget = (Widget) 0;
186 if (m_parent)
187 {
188 parentWidget = (Widget) m_parent->GetTopWidget();
189 }
f97c9854 190 else
2d120f83 191 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
ee1aaf99
JS
192 // prepare the arg list
193 Arg args[10];
194 int ac = 0;
195
196 wxComputeColours (XtDisplay(parentWidget), & m_backgroundColour,
197 (wxColour*) NULL);
198
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++;
203
dfe1eee3 204
ee1aaf99 205 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
2d120f83 206 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
a91b47e8
JS
207
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);
dfe1eee3
VZ
212
213 // code using these vars disabled
214#if 0
a91b47e8
JS
215 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
216 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
217 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
dfe1eee3
VZ
218#endif
219
a91b47e8 220
2d120f83 221 Widget shell = XtParent(fileSel);
dfe1eee3 222
2d120f83
JS
223 if (!m_message.IsNull())
224 XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL);
dfe1eee3 225
2d120f83 226 wxString entirePath("");
dfe1eee3 227
2d120f83
JS
228 if ((m_dir != "") && (m_fileName != ""))
229 {
230 entirePath = m_dir + wxString("/") + m_fileName;
231 }
232 else if ((m_dir != "") && (m_fileName == ""))
233 {
234 entirePath = m_dir + wxString("/");
235 }
236 else if ((m_dir == "") && (m_fileName != ""))
237 {
238 entirePath = m_fileName;
239 }
dfe1eee3 240
2d120f83
JS
241 if (entirePath != "")
242 {
2d120f83
JS
243 XmTextSetString(selectionWidget, (char*) (const char*) entirePath);
244 }
dfe1eee3 245
2d120f83
JS
246 if (m_wildCard != "")
247 {
248 wxString filter("");
249 if (m_dir != "")
250 filter = m_dir + wxString("/") + m_wildCard;
251 else
252 filter = m_wildCard;
dfe1eee3 253
2d120f83
JS
254 XmTextSetString(filterWidget, (char*) (const char*) filter);
255 XmFileSelectionDoSearch(fileSel, NULL);
256 }
dfe1eee3 257
2d120f83
JS
258 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
259 // file selector on Solaris 1.5.1.
260 if ( m_dir != "" )
261 {
262 XmString thePath = XmStringCreateLtoR ((char*) (const char*) m_dir,
263 XmSTRING_DEFAULT_CHARSET);
dfe1eee3 264
2d120f83
JS
265 XtVaSetValues (fileSel,
266 XmNdirectory, thePath,
267 NULL);
dfe1eee3 268
2d120f83
JS
269 XmStringFree(thePath);
270 }
dfe1eee3 271
2d120f83
JS
272 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
273 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
dfe1eee3 274
2d120f83
JS
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...
280 //
f97c9854 281#if !DEFAULT_FILE_SELECTOR_SIZE
2d120f83
JS
282 int width = wxFSB_WIDTH;
283 int height = wxFSB_HEIGHT;
284 XtVaSetValues(fileSel,
285 XmNwidth, width,
286 XmNheight, height,
287 XmNresizePolicy, XmRESIZE_NONE,
288 NULL);
f97c9854 289#endif
ee1aaf99 290 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
a91b47e8
JS
291 DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
292 DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
293
a91b47e8
JS
294 wxChangeListBoxColours(this, dirListWidget);
295 wxChangeListBoxColours(this, fileListWidget);
dfe1eee3 296
2d120f83 297 XtManageChild(fileSel);
a91b47e8 298
2d120f83
JS
299 m_fileSelectorAnswer = "";
300 m_fileSelectorReturned = FALSE;
dfe1eee3 301
2d120f83 302 wxEndBusyCursor();
dfe1eee3 303
2d120f83
JS
304 XtAddGrab(XtParent(fileSel), TRUE, FALSE);
305 XEvent event;
306 while (!m_fileSelectorReturned)
307 {
308 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
309 }
310 XtRemoveGrab(XtParent(fileSel));
dfe1eee3 311
2d120f83 312 XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
dfe1eee3 313
2d120f83
JS
314 // XtDestroyWidget(fileSel);
315 XtUnmapWidget(XtParent(fileSel));
316 XtDestroyWidget(XtParent(fileSel));
dfe1eee3 317
2d120f83
JS
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()))
322 {
323 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
324 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
325 XtDispatchEvent(&event);
326 }
dfe1eee3 327
2d120f83
JS
328 m_path = m_fileSelectorAnswer;
329 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
330 m_dir = wxPathOnly(m_path);
dfe1eee3 331
2d120f83
JS
332 if (m_fileName == "")
333 return wxID_CANCEL;
334 else
335 return wxID_OK;
4bb6408c
JS
336}
337
338// Generic file load/save dialog
7482b220 339static wxString
4bb6408c
JS
340wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
341{
2d120f83 342 char *ext = (char *)extension;
dfe1eee3
VZ
343
344 wxString prompt;
2d120f83
JS
345 wxString str;
346 if (load)
dfe1eee3 347 str = _("Load %s file");
2d120f83 348 else
dfe1eee3
VZ
349 str = _("Save %s file");
350 prompt.Printf(str, what);
351
352 if (*ext == '.')
353 ext++;
354 wxString wild;
355 wild.Printf("*.%s", ext);
356
2d120f83 357 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
4bb6408c
JS
358}
359
360// Generic file load dialog
7482b220 361wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
4bb6408c 362{
2d120f83 363 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
4bb6408c
JS
364}
365
366
367// Generic file save dialog
7482b220 368wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
4bb6408c 369{
2d120f83 370 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
4bb6408c
JS
371}
372
373