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