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