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