]> git.saurik.com Git - wxWidgets.git/blob - src/motif/filedlg.cpp
Start of actual filling in of wxFrame.
[wxWidgets.git] / src / motif / filedlg.cpp
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"
21 #include "wx/app.h"
22 #include "wx/settings.h"
23
24 #ifdef __VMS__
25 #pragma message disable nosimpint
26 #endif
27 #include <Xm/Xm.h>
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>
37 #ifdef __VMS__
38 #pragma message enable nosimpint
39 #endif
40
41 #include "wx/motif/private.h"
42
43 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
44
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
51
52
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)
57 {
58 // If there's a default extension specified but no filter, we create a suitable
59 // filter.
60
61 wxString filter2("");
62 if ( defaultExtension && !filter )
63 filter2 = wxString("*.") + wxString(defaultExtension) ;
64 else if ( filter )
65 filter2 = filter;
66
67 wxString defaultDirString;
68 if (defaultDir)
69 defaultDirString = defaultDir;
70 else
71 defaultDirString = "";
72
73 wxString defaultFilenameString;
74 if (defaultFileName)
75 defaultFilenameString = defaultFileName;
76 else
77 defaultFilenameString = "";
78
79 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
80
81 if ( fileDialog.ShowModal() == wxID_OK )
82 {
83 return fileDialog.GetPath();
84 }
85 else
86 return wxEmptyString;
87 }
88
89 wxString wxFileSelectorEx(const char *title,
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)
98
99 {
100 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
101 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
102
103 if ( fileDialog.ShowModal() == wxID_OK )
104 {
105 *defaultFilterIndex = fileDialog.GetFilterIndex();
106 return fileDialog.GetPath();
107 }
108 else
109 return wxEmptyString;
110 }
111
112 wxString wxFileDialog::m_fileSelectorAnswer = "";
113 bool wxFileDialog::m_fileSelectorReturned = FALSE;
114
115 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
116 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
117 {
118 wxFileDialog::m_fileSelectorAnswer = "";
119 wxFileDialog::m_fileSelectorReturned = TRUE;
120 }
121
122 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
123 {
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;
134 }
135 }
136
137 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
138 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
139 long style, const wxPoint& pos)
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 }
151
152 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
153 {
154 wxWindow::DoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
155
156 // Change colour of the scrolled areas of the listboxes
157 Widget listParent = XtParent (widget);
158 wxWindow::DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
159
160 Widget hsb = (Widget) 0;
161 Widget vsb = (Widget) 0;
162 XtVaGetValues (listParent,
163 XmNhorizontalScrollBar, &hsb,
164 XmNverticalScrollBar, &vsb,
165 NULL);
166
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);
171 wxWindow::DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
172 wxWindow::DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
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
184 int wxFileDialog::ShowModal()
185 {
186 wxBeginBusyCursor();
187
188 // static char fileBuf[512];
189 Widget parentWidget = (Widget) 0;
190 if (m_parent)
191 {
192 parentWidget = (Widget) m_parent->GetTopWidget();
193 }
194 else
195 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
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
208
209 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
210 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
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);
216
217 // code using these vars disabled
218 #if 0
219 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
220 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
221 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
222 #endif
223
224
225 Widget shell = XtParent(fileSel);
226
227 if (!m_message.IsNull())
228 XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL);
229
230 wxString entirePath("");
231
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 }
244
245 if (entirePath != "")
246 {
247 XmTextSetString(selectionWidget, (char*) (const char*) entirePath);
248 }
249
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;
257
258 XmTextSetString(filterWidget, (char*) (const char*) filter);
259 XmFileSelectionDoSearch(fileSel, NULL);
260 }
261
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);
268
269 XtVaSetValues (fileSel,
270 XmNdirectory, thePath,
271 NULL);
272
273 XmStringFree(thePath);
274 }
275
276 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
277 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
278
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 //
285 #if !DEFAULT_FILE_SELECTOR_SIZE
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);
293 #endif
294 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
295 DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
296 DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
297
298 wxChangeListBoxColours(this, dirListWidget);
299 wxChangeListBoxColours(this, fileListWidget);
300
301 XtManageChild(fileSel);
302
303 m_fileSelectorAnswer = "";
304 m_fileSelectorReturned = FALSE;
305
306 wxEndBusyCursor();
307
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));
315
316 XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
317
318 // XtDestroyWidget(fileSel);
319 XtUnmapWidget(XtParent(fileSel));
320 XtDestroyWidget(XtParent(fileSel));
321
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 }
331
332 m_path = m_fileSelectorAnswer;
333 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
334 m_dir = wxPathOnly(m_path);
335
336 if (m_fileName == "")
337 return wxID_CANCEL;
338 else
339 return wxID_OK;
340 }
341
342 // Generic file load/save dialog
343 static wxString
344 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
345 {
346 char *ext = (char *)extension;
347
348 wxString prompt;
349 wxString str;
350 if (load)
351 str = _("Load %s file");
352 else
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
361 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
362 }
363
364 // Generic file load dialog
365 wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
366 {
367 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
368 }
369
370
371 // Generic file save dialog
372 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
373 {
374 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
375 }
376
377