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