]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
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 | |
ee31c392 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
ee31c392 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
4bb6408c | 16 | #ifdef __GNUG__ |
ee31c392 | 17 | #pragma implementation "msgdlg.h" |
4bb6408c JS |
18 | #endif |
19 | ||
ee31c392 VZ |
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" | |
5dcf05ae | 31 | #include "wx/motif/msgdlg.h" |
ee31c392 VZ |
32 | #include "wx/motif/private.h" |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
35 | // macros | |
36 | // ---------------------------------------------------------------------------- | |
4bb6408c JS |
37 | |
38 | #if !USE_SHARED_LIBRARY | |
ee31c392 | 39 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) |
4bb6408c JS |
40 | #endif |
41 | ||
ee31c392 VZ |
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, | |
af111fc3 | 62 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
ee31c392 VZ |
63 | { |
64 | msgboxCallBack(w, client_data, wxID_OK); | |
65 | } | |
66 | ||
67 | static void msgboxCallBackCancel(Widget w, | |
68 | int client_data, | |
af111fc3 | 69 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
ee31c392 VZ |
70 | { |
71 | msgboxCallBack(w, client_data, wxID_CANCEL); | |
72 | } | |
73 | ||
11d737b4 MB |
74 | static void msgboxCallBackHelp(Widget w, |
75 | int client_data, | |
af111fc3 | 76 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
11d737b4 MB |
77 | { |
78 | msgboxCallBack(w, client_data, wxID_HELP); | |
79 | } | |
80 | ||
ee31c392 VZ |
81 | static void msgboxCallBackClose(Widget w, |
82 | int client_data, | |
af111fc3 | 83 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
ee31c392 VZ |
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, | |
af111fc3 | 96 | const wxPoint& WXUNUSED(pos)) |
4bb6408c JS |
97 | { |
98 | m_caption = caption; | |
99 | m_message = message; | |
100 | m_dialogStyle = style; | |
101 | m_parent = parent; | |
102 | } | |
103 | ||
104 | int wxMessageDialog::ShowModal() | |
105 | { | |
ee31c392 VZ |
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; | |
ee31c392 VZ |
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 | ||
af111fc3 | 128 | Widget wParent = m_parent ? GetWidget(m_parent) : (Widget) 0; |
ee31c392 VZ |
129 | if ( !wParent ) |
130 | { | |
131 | wxWindow *window = wxTheApp->GetTopWindow(); | |
132 | if ( !window ) | |
133 | { | |
134 | wxFAIL_MSG("can't show message box without parent window"); | |
135 | ||
136 | return wxID_CANCEL; | |
137 | } | |
138 | ||
139 | wParent = GetWidget(window); | |
140 | } | |
141 | ||
ee1aaf99 JS |
142 | // prepare the arg list |
143 | Arg args[10]; | |
144 | int ac = 0; | |
145 | ||
146 | wxXmString text(m_message); | |
147 | wxXmString title(m_caption); | |
148 | XtSetArg(args[ac], XmNmessageString, text()); ac++; | |
149 | XtSetArg(args[ac], XmNdialogTitle, title()); ac++; | |
150 | ||
151 | wxComputeColours (XtDisplay(wParent), & m_backgroundColour, | |
152 | (wxColour*) NULL); | |
153 | ||
154 | XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++; | |
155 | XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++; | |
156 | XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++; | |
157 | XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++; | |
158 | ||
159 | // do create message box | |
160 | ||
ee31c392 VZ |
161 | Widget wMsgBox = (*dialogCreateFunction)(wParent, "", args, ac); |
162 | ||
163 | wxCHECK_MSG( wMsgBox, wxID_CANCEL, "msg box creation failed" ); | |
164 | ||
11d737b4 MB |
165 | // get the buttons which we might either remove or rename |
166 | // depending on the requested style | |
167 | // | |
168 | Widget wBtnOk = XmMessageBoxGetChild(wMsgBox, XmDIALOG_OK_BUTTON); | |
169 | Widget wBtnHelp = XmMessageBoxGetChild(wMsgBox, XmDIALOG_HELP_BUTTON); | |
170 | Widget wBtnCancel = XmMessageBoxGetChild(wMsgBox, XmDIALOG_CANCEL_BUTTON); | |
ee31c392 | 171 | |
11d737b4 | 172 | if ( m_dialogStyle & wxYES_NO ) |
ee31c392 | 173 | { |
11d737b4 | 174 | wxXmString yes(_("Yes")), no(_("No")), cancel(_("Cancel")); |
ee31c392 | 175 | |
11d737b4 | 176 | if ( m_dialogStyle & wxCANCEL ) |
ee31c392 | 177 | { |
11d737b4 MB |
178 | // use the cancel button for No and the help button for |
179 | // Cancel Yuk :-) MB | |
180 | // | |
ee31c392 VZ |
181 | XtVaSetValues(wBtnOk, XmNlabelString, yes(), NULL); |
182 | XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL); | |
11d737b4 | 183 | XtVaSetValues(wBtnHelp, XmNlabelString, cancel(), NULL); |
ee31c392 VZ |
184 | } |
185 | else | |
186 | { | |
11d737b4 MB |
187 | // no cancel button requested... |
188 | // remove the help button and use cancel for no | |
189 | // | |
190 | XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL); | |
191 | XtUnmanageChild(wBtnHelp); | |
ee31c392 VZ |
192 | } |
193 | } | |
11d737b4 MB |
194 | else |
195 | { | |
196 | // remove the help button and the cancel button (unless it was | |
197 | // requested) | |
198 | // | |
199 | XtUnmanageChild(wBtnHelp); | |
200 | if ( !(m_dialogStyle & wxCANCEL ) ) XtUnmanageChild(wBtnCancel); | |
201 | } | |
ee31c392 VZ |
202 | |
203 | // set the callbacks for the message box buttons | |
204 | XtAddCallback(wMsgBox, XmNokCallback, | |
205 | (XtCallbackProc)msgboxCallBackOk, (XtPointer)this); | |
206 | XtAddCallback(wMsgBox, XmNcancelCallback, | |
207 | (XtCallbackProc)msgboxCallBackCancel, (XtPointer)this); | |
11d737b4 MB |
208 | XtAddCallback(wMsgBox, XmNhelpCallback, |
209 | (XtCallbackProc)msgboxCallBackHelp, (XtPointer)this); | |
ee31c392 VZ |
210 | XtAddCallback(wMsgBox, XmNunmapCallback, |
211 | (XtCallbackProc)msgboxCallBackClose, (XtPointer)this); | |
212 | ||
213 | // show it as a modal dialog | |
214 | XtManageChild(wMsgBox); | |
215 | XtAddGrab(wMsgBox, True, False); | |
216 | ||
217 | // the m_result will be changed when message box goes away | |
218 | m_result = -1; | |
219 | ||
220 | // local message loop | |
221 | XtAppContext context = XtWidgetToApplicationContext(wParent); | |
222 | XEvent event; | |
223 | while ( m_result == -1 ) | |
224 | { | |
225 | XtAppNextEvent(context, &event); | |
226 | XtDispatchEvent(&event); | |
227 | } | |
228 | ||
229 | // translate the result if necessary | |
230 | if ( m_dialogStyle & wxYES_NO ) | |
231 | { | |
232 | if ( m_result == wxID_OK ) | |
233 | m_result = wxID_YES; | |
234 | else if ( m_result == wxID_CANCEL ) | |
235 | m_result = wxID_NO; | |
11d737b4 MB |
236 | else if ( m_result == wxID_HELP ) |
237 | m_result = wxID_CANCEL; | |
ee31c392 VZ |
238 | } |
239 | ||
240 | return m_result; | |
4bb6408c JS |
241 | } |
242 |