Use default GUI font for wxMessageDialog and wxFileDialog.
[wxWidgets.git] / src / motif / msgdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/msgdlg.cpp
3 // Purpose: wxMessageDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "msgdlg.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #include "wx/defs.h"
28
29 #ifdef __VMS
30 #define XtDisplay XTDISPLAY
31 #pragma message disable nosimpint
32 #include <wx/vms_x_fix.h>
33 #endif
34 #include <X11/Xlib.h>
35
36 #include <Xm/Xm.h>
37 #include <Xm/MessageB.h>
38 #ifdef __VMS
39 #pragma message enable nosimpint
40 #endif
41
42 #include "wx/app.h"
43 #include "wx/intl.h"
44 #include "wx/msgdlg.h"
45 #include "wx/motif/private.h"
46 #include "wx/settings.h"
47
48 // ----------------------------------------------------------------------------
49 // macros
50 // ----------------------------------------------------------------------------
51
52 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // the callbacks for message box buttons
60 // ----------------------------------------------------------------------------
61
62 // the common part
63 static void msgboxCallBack(Widget w, void* client_data, int id)
64 {
65 // close the dialog
66 XtUnmanageChild(w);
67
68 wxMessageDialog *dlg = (wxMessageDialog *)client_data;
69 dlg->SetResult(id);
70 }
71
72 static void msgboxCallBackOk(Widget w,
73 void* client_data,
74 XmAnyCallbackStruct *WXUNUSED(call_data))
75 {
76 msgboxCallBack(w, client_data, wxID_OK);
77 }
78
79 static void msgboxCallBackCancel(Widget w,
80 void* client_data,
81 XmAnyCallbackStruct *WXUNUSED(call_data))
82 {
83 msgboxCallBack(w, client_data, wxID_CANCEL);
84 }
85
86 static void msgboxCallBackHelp(Widget w,
87 void* client_data,
88 XmAnyCallbackStruct *WXUNUSED(call_data))
89 {
90 msgboxCallBack(w, client_data, wxID_HELP);
91 }
92
93 static void msgboxCallBackClose(Widget w,
94 void* client_data,
95 XmAnyCallbackStruct *WXUNUSED(call_data))
96 {
97 msgboxCallBack(w, client_data, wxID_CANCEL);
98 }
99
100 // ----------------------------------------------------------------------------
101 // wxMessageDialog
102 // ----------------------------------------------------------------------------
103
104 wxMessageDialog::wxMessageDialog(wxWindow *parent,
105 const wxString& message,
106 const wxString& caption,
107 long style,
108 const wxPoint& WXUNUSED(pos))
109 {
110 m_caption = caption;
111 m_message = message;
112 m_parent = parent;
113 SetMessageDialogStyle(style);
114 }
115
116 int wxMessageDialog::ShowModal()
117 {
118 Widget (*dialogCreateFunction)(Widget, String, ArgList, Cardinal) = NULL;
119 const long style = GetMessageDialogStyle();
120
121 if ( style & wxYES_NO )
122 {
123 // if we have [Yes], it must be a question
124 dialogCreateFunction = XmCreateQuestionDialog;
125 }
126 else if ( style & wxICON_STOP )
127 {
128 // error dialog is the one with error icon...
129 dialogCreateFunction = XmCreateErrorDialog;
130 }
131 else if ( style & wxICON_EXCLAMATION )
132 {
133 // ...and the warning dialog too
134 dialogCreateFunction = XmCreateWarningDialog;
135 }
136 else
137 {
138 // finally, use the info dialog by default
139 dialogCreateFunction = XmCreateInformationDialog;
140 }
141
142 Widget wParent = m_parent ? GetWidget(m_parent) : (Widget) 0;
143 if ( !wParent )
144 {
145 wxWindow *window = wxTheApp->GetTopWindow();
146 if ( !window )
147 {
148 wxFAIL_MSG("can't show message box without parent window");
149
150 return wxID_CANCEL;
151 }
152
153 wParent = GetWidget(window);
154 }
155
156 // prepare the arg list
157 Arg args[10];
158 int ac = 0;
159
160 wxXmString text(m_message);
161 wxXmString title(m_caption);
162 XtSetArg(args[ac], XmNmessageString, text()); ac++;
163 XtSetArg(args[ac], XmNdialogTitle, title()); ac++;
164
165 Display* dpy = XtDisplay(wParent);
166
167 wxComputeColours (dpy, & m_backgroundColour, (wxColour*) NULL);
168
169 XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++;
170 XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++;
171 XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++;
172 XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++;
173
174 wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
175
176 if ( wxFont::GetFontTag() == (WXString) XmNfontList )
177 {
178 XtSetArg(args[ac], XmNbuttonFontList, font.GetFontTypeC(dpy)); ac++;
179 XtSetArg(args[ac], XmNlabelFontList, font.GetFontTypeC(dpy)); ac++;
180 XtSetArg(args[ac], XmNtextFontList, font.GetFontTypeC(dpy)); ac++;
181 }
182 else
183 {
184 XtSetArg(args[ac], XmNbuttonRenderTable, font.GetFontTypeC(dpy)); ac++;
185 XtSetArg(args[ac], XmNlabelRenderTable, font.GetFontTypeC(dpy)); ac++;
186 XtSetArg(args[ac], XmNtextRenderTable, font.GetFontTypeC(dpy)); ac++;
187 }
188
189 // do create message box
190
191 Widget wMsgBox = (*dialogCreateFunction)(wParent, "", args, ac);
192
193 wxCHECK_MSG( wMsgBox, wxID_CANCEL, "msg box creation failed" );
194
195 // get the buttons which we might either remove or rename
196 // depending on the requested style
197 //
198 Widget wBtnOk = XmMessageBoxGetChild(wMsgBox, XmDIALOG_OK_BUTTON);
199 Widget wBtnHelp = XmMessageBoxGetChild(wMsgBox, XmDIALOG_HELP_BUTTON);
200 Widget wBtnCancel = XmMessageBoxGetChild(wMsgBox, XmDIALOG_CANCEL_BUTTON);
201
202 if ( style & wxYES_NO )
203 {
204 wxXmString yes(_("Yes")), no(_("No")), cancel(_("Cancel"));
205
206 if ( style & wxCANCEL )
207 {
208 // use the cancel button for No and the help button for
209 // Cancel Yuk :-) MB
210 //
211 XtVaSetValues(wBtnOk, XmNlabelString, yes(), NULL);
212 XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL);
213 XtVaSetValues(wBtnHelp, XmNlabelString, cancel(), NULL);
214 }
215 else
216 {
217 // no cancel button requested...
218 // remove the help button and use cancel for no
219 //
220 XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL);
221 XtUnmanageChild(wBtnHelp);
222 }
223 }
224 else
225 {
226 // remove the help button and the cancel button (unless it was
227 // requested)
228 //
229 XtUnmanageChild(wBtnHelp);
230 if ( !(style & wxCANCEL ) ) XtUnmanageChild(wBtnCancel);
231 }
232
233 // set the callbacks for the message box buttons
234 XtAddCallback(wMsgBox, XmNokCallback,
235 (XtCallbackProc)msgboxCallBackOk, (XtPointer)this);
236 XtAddCallback(wMsgBox, XmNcancelCallback,
237 (XtCallbackProc)msgboxCallBackCancel, (XtPointer)this);
238 XtAddCallback(wMsgBox, XmNhelpCallback,
239 (XtCallbackProc)msgboxCallBackHelp, (XtPointer)this);
240 XtAddCallback(wMsgBox, XmNunmapCallback,
241 (XtCallbackProc)msgboxCallBackClose, (XtPointer)this);
242
243 // show it as a modal dialog
244 XtManageChild(wMsgBox);
245 XtAddGrab(wMsgBox, True, False);
246
247 // the m_result will be changed when message box goes away
248 m_result = -1;
249
250 // local message loop
251 XtAppContext context = XtWidgetToApplicationContext(wParent);
252 XEvent event;
253 while ( m_result == -1 )
254 {
255 XtAppNextEvent(context, &event);
256 XtDispatchEvent(&event);
257 }
258
259 // translate the result if necessary
260 if ( style & wxYES_NO )
261 {
262 if ( m_result == wxID_OK )
263 m_result = wxID_YES;
264 else if ( m_result == wxID_CANCEL )
265 m_result = wxID_NO;
266 else if ( m_result == wxID_HELP )
267 m_result = wxID_CANCEL;
268 }
269
270 return m_result;
271 }
272