Use default GUI font for wxMessageDialog and wxFileDialog.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __VMS
20 #define XtDisplay XTDISPLAY
21 #define XtParent XTPARENT
22 #define XtWindow XTWINDOW
23 #endif
24
25 #include "wx/defs.h"
26 #include "wx/utils.h"
27 #include "wx/filedlg.h"
28 #include "wx/intl.h"
29 #include "wx/app.h"
30 #include "wx/settings.h"
31 #include "wx/tokenzr.h"
32
33 #ifdef __VMS__
34 #pragma message disable nosimpint
35 #endif
36 #include <Xm/Xm.h>
37 #include <Xm/MwmUtil.h>
38 #include <Xm/Label.h>
39 #include <Xm/BulletinB.h>
40 #include <Xm/Frame.h>
41 #include <Xm/Text.h>
42 #include <Xm/DialogS.h>
43 #include <Xm/FileSB.h>
44 #include <Xm/RowColumn.h>
45 #include <Xm/LabelG.h>
46 #ifdef __VMS__
47 #pragma message enable nosimpint
48 #endif
49
50 #include "wx/motif/private.h"
51
52 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
53
54 #define DEFAULT_FILE_SELECTOR_SIZE 0
55 // Let Motif defines the size of File
56 // Selector Box (if 1), or fix it to
57 // wxFSB_WIDTH x wxFSB_HEIGHT (if 0)
58 #define wxFSB_WIDTH 600
59 #define wxFSB_HEIGHT 500
60
61
62 wxString wxFileDialog::m_fileSelectorAnswer = "";
63 bool wxFileDialog::m_fileSelectorReturned = false;
64
65 static void wxFileSelClose(Widget WXUNUSED(w),
66 void* WXUNUSED(client_data),
67 XmAnyCallbackStruct *WXUNUSED(call_data))
68 {
69 wxFileDialog::m_fileSelectorAnswer = "";
70 wxFileDialog::m_fileSelectorReturned = true;
71 }
72
73 void wxFileSelCancel( Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data),
74 XmFileSelectionBoxCallbackStruct *WXUNUSED(cbs) )
75 {
76 wxFileDialog::m_fileSelectorAnswer = "";
77 wxFileDialog::m_fileSelectorReturned = true;
78 }
79
80 void wxFileSelOk(Widget WXUNUSED(fs), XtPointer WXUNUSED(client_data), XmFileSelectionBoxCallbackStruct *cbs)
81 {
82 char *filename = NULL;
83 if (!XmStringGetLtoR(cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
84 wxFileDialog::m_fileSelectorAnswer = "";
85 wxFileDialog::m_fileSelectorReturned = true;
86 } else {
87 if (filename) {
88 wxFileDialog::m_fileSelectorAnswer = filename;
89 XtFree(filename);
90 }
91 wxFileDialog::m_fileSelectorReturned = true;
92 }
93 }
94
95 static wxString ParseWildCard( const wxString& wild )
96 {
97 #ifdef __WXDEBUG__
98 static const wxChar* msg =
99 _T("Motif file dialog does not understand this ")
100 _T("wildcard syntax");
101 #endif
102
103 wxArrayString wildDescriptions, wildFilters;
104 const size_t count = wxParseCommonDialogsFilter(wild,
105 wildDescriptions,
106 wildFilters);
107 wxCHECK_MSG( count, _T("*.*"), wxT("wxFileDialog: bad wildcard string") );
108 wxCHECK_MSG( count == 1, _T("*.*"), msg );
109
110 // check for *.txt;*.rtf
111 wxStringTokenizer tok2( wildFilters[0], _T(";") );
112 wxString wildcard = tok2.GetNextToken();
113
114 wxCHECK_MSG( tok2.CountTokens() <= 1, wildcard, msg );
115 return wildcard;
116 }
117
118 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
119 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
120 long style, const wxPoint& pos)
121 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
122 {
123 m_filterIndex = 1;
124 }
125
126 static void wxChangeListBoxColours(wxWindow* WXUNUSED(win), Widget widget)
127 {
128 wxDoChangeBackgroundColour((WXWidget) widget, *wxWHITE);
129
130 // Change colour of the scrolled areas of the listboxes
131 Widget listParent = XtParent (widget);
132 #if 0
133 wxDoChangeBackgroundColour((WXWidget) listParent, *wxWHITE, true);
134 #endif
135
136 Widget hsb = (Widget) 0;
137 Widget vsb = (Widget) 0;
138 XtVaGetValues (listParent,
139 XmNhorizontalScrollBar, &hsb,
140 XmNverticalScrollBar, &vsb,
141 NULL);
142
143 /* TODO: should scrollbars be affected? Should probably have separate
144 * function to change them (by default, taken from wxSystemSettings)
145 */
146 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
147 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true);
148 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true);
149
150 if (hsb)
151 XtVaSetValues (hsb,
152 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
153 NULL);
154 if (vsb)
155 XtVaSetValues (vsb,
156 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
157 NULL);
158 }
159
160 int wxFileDialog::ShowModal()
161 {
162 wxBeginBusyCursor();
163
164 // static char fileBuf[512];
165 Widget parentWidget = (Widget) 0;
166 if (m_parent)
167 parentWidget = (Widget) m_parent->GetTopWidget();
168 else
169 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
170 // prepare the arg list
171 Display* dpy = XtDisplay(parentWidget);
172 Arg args[10];
173 int ac = 0;
174
175 wxComputeColours (dpy, & m_backgroundColour, (wxColour*) NULL);
176
177 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
178 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
179 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
180 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
181
182 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
183
184 if ( wxFont::GetFontTag() == (WXString) XmNfontList )
185 {
186 XtSetArg(args[ac], XmNbuttonFontList, font.GetFontTypeC(dpy)); ac++;
187 XtSetArg(args[ac], XmNlabelFontList, font.GetFontTypeC(dpy)); ac++;
188 XtSetArg(args[ac], XmNtextFontList, font.GetFontTypeC(dpy)); ac++;
189 }
190 else
191 {
192 XtSetArg(args[ac], XmNbuttonRenderTable, font.GetFontTypeC(dpy)); ac++;
193 XtSetArg(args[ac], XmNlabelRenderTable, font.GetFontTypeC(dpy)); ac++;
194 XtSetArg(args[ac], XmNtextRenderTable, font.GetFontTypeC(dpy)); ac++;
195 }
196
197 Widget fileSel = XmCreateFileSelectionDialog(parentWidget, "file_selector", args, ac);
198 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel, XmDIALOG_HELP_BUTTON));
199
200 Widget filterWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_FILTER_TEXT);
201 Widget selectionWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_TEXT);
202 Widget dirListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_DIR_LIST);
203 Widget fileListWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_LIST);
204
205 // code using these vars disabled
206 #if 0
207 Widget okWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_OK_BUTTON);
208 Widget applyWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_APPLY_BUTTON);
209 Widget cancelWidget = XmFileSelectionBoxGetChild(fileSel, XmDIALOG_CANCEL_BUTTON);
210 #endif
211
212
213 Widget shell = XtParent(fileSel);
214
215 if (!m_message.IsNull())
216 XtVaSetValues(shell,
217 XmNtitle, wxConstCast(m_message.c_str(), char),
218 NULL);
219
220 wxString entirePath("");
221
222 if ((m_dir != "") && (m_fileName != ""))
223 {
224 entirePath = m_dir + wxString("/") + m_fileName;
225 }
226 else if ((m_dir != "") && (m_fileName == ""))
227 {
228 entirePath = m_dir + wxString("/");
229 }
230 else if ((m_dir == "") && (m_fileName != ""))
231 {
232 entirePath = m_fileName;
233 }
234
235 if (m_wildCard != "")
236 {
237 // return something understandable by Motif
238 wxString wildCard = ParseWildCard( m_wildCard );
239 wxString filter;
240 if (m_dir != "")
241 filter = m_dir + wxString("/") + wildCard;
242 else
243 filter = wildCard;
244
245 XmTextSetString(filterWidget, wxConstCast(filter.c_str(), char));
246 XmFileSelectionDoSearch(fileSel, NULL);
247 }
248
249 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
250 // file selector on Solaris 1.5.1.
251 if ( m_dir != "" )
252 {
253 wxXmString thePath( m_dir );
254
255 XtVaSetValues (fileSel,
256 XmNdirectory, thePath(),
257 NULL);
258 }
259
260 if (entirePath != "")
261 {
262 XmTextSetString(selectionWidget,
263 wxConstCast(entirePath.c_str(), char));
264 }
265
266 XtAddCallback(fileSel, XmNcancelCallback, (XtCallbackProc)wxFileSelCancel, (XtPointer)NULL);
267 XtAddCallback(fileSel, XmNokCallback, (XtCallbackProc)wxFileSelOk, (XtPointer)NULL);
268 XtAddCallback(fileSel, XmNunmapCallback,
269 (XtCallbackProc)wxFileSelClose, (XtPointer)this);
270
271 //#if XmVersion > 1000
272 // I'm not sure about what you mean with XmVersion.
273 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
274 // (Motif1.1.4 ==> XmVersion 1100 )
275 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
276 //
277 #if !DEFAULT_FILE_SELECTOR_SIZE
278 int width = wxFSB_WIDTH;
279 int height = wxFSB_HEIGHT;
280 XtVaSetValues(fileSel,
281 XmNwidth, width,
282 XmNheight, height,
283 XmNresizePolicy, XmRESIZE_NONE,
284 NULL);
285 #endif
286 // wxDoChangeBackgroundColour((WXWidget) fileSel, m_backgroundColour);
287 wxDoChangeBackgroundColour((WXWidget) filterWidget, *wxWHITE);
288 wxDoChangeBackgroundColour((WXWidget) selectionWidget, *wxWHITE);
289
290 wxChangeListBoxColours(this, dirListWidget);
291 wxChangeListBoxColours(this, fileListWidget);
292
293 XtManageChild(fileSel);
294
295 m_fileSelectorAnswer = "";
296 m_fileSelectorReturned = false;
297
298 wxEndBusyCursor();
299
300 XtAddGrab(XtParent(fileSel), True, False);
301 XtAppContext context = (XtAppContext) wxTheApp->GetAppContext();
302 XEvent event;
303 while (!m_fileSelectorReturned)
304 {
305 XtAppNextEvent(context, &event);
306 XtDispatchEvent(&event);
307 }
308 XtRemoveGrab(XtParent(fileSel));
309
310 // XmUpdateDisplay((Widget) wxTheApp->GetTopLevelWidget()); // Experimental
311
312 Display* display = XtDisplay(fileSel);
313
314 XtUnmapWidget(XtParent(fileSel));
315 XtDestroyWidget(XtParent(fileSel));
316
317 // Now process all events, because otherwise
318 // this might remain on the screen
319 wxFlushEvents(display);
320
321 m_path = m_fileSelectorAnswer;
322 m_fileName = wxFileNameFromPath(m_fileSelectorAnswer);
323 m_dir = wxPathOnly(m_path);
324
325 if (m_fileName == "")
326 return wxID_CANCEL;
327 else
328 return wxID_OK;
329 }
330