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