Motif: made file selector and message box properly take on background colour.
[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 #include <Xm/Xm.h>
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>
34
35 #include "wx/motif/private.h"
36
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
39 #endif
40
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
47
48
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)
53 {
54 // If there's a default extension specified but no filter, we create a suitable
55 // filter.
56
57 wxString filter2("");
58 if ( defaultExtension && !filter )
59 filter2 = wxString("*.") + wxString(defaultExtension) ;
60 else if ( filter )
61 filter2 = filter;
62
63 wxString defaultDirString;
64 if (defaultDir)
65 defaultDirString = defaultDir;
66 else
67 defaultDirString = "";
68
69 wxString defaultFilenameString;
70 if (defaultFileName)
71 defaultFilenameString = defaultFileName;
72 else
73 defaultFilenameString = "";
74
75 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y));
76
77 if ( fileDialog.ShowModal() == wxID_OK )
78 {
79 return fileDialog.GetPath();
80 }
81 else
82 return wxEmptyString;
83 }
84
85 wxString wxFileSelectorEx(const char *title,
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)
94
95 {
96 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
97 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
98
99 if ( fileDialog.ShowModal() == wxID_OK )
100 {
101 *defaultFilterIndex = fileDialog.GetFilterIndex();
102 return fileDialog.GetPath();
103 }
104 else
105 return wxEmptyString;
106 }
107
108 wxString wxFileDialog::m_fileSelectorAnswer = "";
109 bool wxFileDialog::m_fileSelectorReturned = FALSE;
110
111 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
112 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
113 {
114 wxFileDialog::m_fileSelectorAnswer = "";
115 wxFileDialog::m_fileSelectorReturned = TRUE;
116 }
117
118 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
119 {
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;
130 }
131 }
132
133 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
134 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
135 long style, const wxPoint& pos)
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 }
147
148 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
149 {
150 wxWindow::DoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
151
152 // Change colour of the scrolled areas of the listboxes
153 Widget listParent = XtParent (widget);
154 wxWindow::DoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, TRUE);
155
156 Widget hsb = (Widget) 0;
157 Widget vsb = (Widget) 0;
158 XtVaGetValues (listParent,
159 XmNhorizontalScrollBar, &hsb,
160 XmNverticalScrollBar, &vsb,
161 NULL);
162
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);
167 wxWindow::DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
168 wxWindow::DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
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
180 int wxFileDialog::ShowModal()
181 {
182 wxBeginBusyCursor();
183
184 // static char fileBuf[512];
185 Widget parentWidget = (Widget) 0;
186 if (m_parent)
187 {
188 parentWidget = (Widget) m_parent->GetTopWidget();
189 }
190 else
191 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
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
204
205 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
206 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
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);
212
213 // code using these vars disabled
214 #if 0
215 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
216 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
217 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
218 #endif
219
220
221 Widget shell = XtParent(fileSel);
222
223 if (!m_message.IsNull())
224 XtVaSetValues(shell, XmNtitle, (char*) (const char*) m_message, NULL);
225
226 wxString entirePath("");
227
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 }
240
241 if (entirePath != "")
242 {
243 XmTextSetString(selectionWidget, (char*) (const char*) entirePath);
244 }
245
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;
253
254 XmTextSetString(filterWidget, (char*) (const char*) filter);
255 XmFileSelectionDoSearch(fileSel, NULL);
256 }
257
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);
264
265 XtVaSetValues (fileSel,
266 XmNdirectory, thePath,
267 NULL);
268
269 XmStringFree(thePath);
270 }
271
272 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
273 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
274
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 //
281 #if !DEFAULT_FILE_SELECTOR_SIZE
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);
289 #endif
290 // DoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
291 DoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
292 DoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
293
294 wxChangeListBoxColours(this, dirListWidget);
295 wxChangeListBoxColours(this, fileListWidget);
296
297 XtManageChild(fileSel);
298
299 m_fileSelectorAnswer = "";
300 m_fileSelectorReturned = FALSE;
301
302 wxEndBusyCursor();
303
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));
311
312 XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
313
314 // XtDestroyWidget(fileSel);
315 XtUnmapWidget(XtParent(fileSel));
316 XtDestroyWidget(XtParent(fileSel));
317
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 }
327
328 m_path = m_fileSelectorAnswer;
329 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
330 m_dir = wxPathOnly(m_path);
331
332 if (m_fileName == "")
333 return wxID_CANCEL;
334 else
335 return wxID_OK;
336 }
337
338 // Generic file load/save dialog
339 static wxString
340 wxDefaultFileSelector(bool load, const char *what, const char *extension, const char *default_name, wxWindow *parent)
341 {
342 char *ext = (char *)extension;
343
344 wxString prompt;
345 wxString str;
346 if (load)
347 str = _("Load %s file");
348 else
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
357 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
358 }
359
360 // Generic file load dialog
361 wxString wxLoadFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
362 {
363 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
364 }
365
366
367 // Generic file save dialog
368 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name, wxWindow *parent)
369 {
370 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
371 }
372
373