]> git.saurik.com Git - wxWidgets.git/blob - src/motif/msgdlg.cpp
Someone need to make the clientdata be void* instead of this char*
[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 msgboxCallBackClose(Widget w,
75 int client_data,
76 XmAnyCallbackStruct *call_data)
77 {
78 msgboxCallBack(w, client_data, wxID_CANCEL);
79 }
80
81 // ----------------------------------------------------------------------------
82 // wxMessageDialog
83 // ----------------------------------------------------------------------------
84
85 wxMessageDialog::wxMessageDialog(wxWindow *parent,
86 const wxString& message,
87 const wxString& caption,
88 long style,
89 const wxPoint& pos)
90 {
91 m_caption = caption;
92 m_message = message;
93 m_dialogStyle = style;
94 m_parent = parent;
95 }
96
97 int wxMessageDialog::ShowModal()
98 {
99 Widget (*dialogCreateFunction)(Widget, String, ArgList, Cardinal) = NULL;
100 if ( m_dialogStyle & wxYES_NO )
101 {
102 // if we have [Yes], it must be a question
103 dialogCreateFunction = XmCreateQuestionDialog;
104
105 // TODO we could support this by using the help button...
106 wxASSERT_MSG( !(m_dialogStyle & wxCANCEL), "not supported" );
107 }
108 else if ( m_dialogStyle & wxICON_STOP )
109 {
110 // error dialog is the one with error icon...
111 dialogCreateFunction = XmCreateErrorDialog;
112 }
113 else if ( m_dialogStyle & wxICON_EXCLAMATION )
114 {
115 // ...and the warning dialog too
116 dialogCreateFunction = XmCreateWarningDialog;
117 }
118 else
119 {
120 // finally, use the info dialog by default
121 dialogCreateFunction = XmCreateInformationDialog;
122 }
123
124 // prepare the arg list
125 Arg args[2];
126 int ac = 0;
127
128 wxXmString text(m_message);
129 wxXmString title(m_caption);
130 XtSetArg(args[ac], XmNmessageString, text()); ac++;
131 XtSetArg(args[ac], XmNdialogTitle, title()); ac++;
132
133 // do create message box
134 Widget wParent = m_parent ? GetWidget(m_parent) : NULL;
135 if ( !wParent )
136 {
137 wxWindow *window = wxTheApp->GetTopWindow();
138 if ( !window )
139 {
140 wxFAIL_MSG("can't show message box without parent window");
141
142 return wxID_CANCEL;
143 }
144
145 wParent = GetWidget(window);
146 }
147
148 Widget wMsgBox = (*dialogCreateFunction)(wParent, "", args, ac);
149
150 wxCHECK_MSG( wMsgBox, wxID_CANCEL, "msg box creation failed" );
151
152 // remove the [Help] button which wouldn't do anything anyhow
153 XtUnmanageChild(XmMessageBoxGetChild(wMsgBox, XmDIALOG_HELP_BUTTON));
154
155 // and the [Cancel] button too if we were not asked for it
156 if ( !(m_dialogStyle & wxCANCEL) )
157 {
158 Widget wBtnCancel = XmMessageBoxGetChild(wMsgBox,
159 XmDIALOG_CANCEL_BUTTON);
160
161 // ... unless it's a wxYES_NO dialog in which case we just rename
162 // [Cancel] to [No] instead
163 if ( m_dialogStyle & wxYES_NO )
164 {
165 Widget wBtnOk = XmMessageBoxGetChild(wMsgBox,
166 XmDIALOG_OK_BUTTON);
167
168 wxXmString yes(_("Yes")), no(_("No"));
169 XtVaSetValues(wBtnOk, XmNlabelString, yes(), NULL);
170 XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL);
171 }
172 else
173 {
174 XtUnmanageChild(wBtnCancel);
175 }
176 }
177
178 // set the callbacks for the message box buttons
179 XtAddCallback(wMsgBox, XmNokCallback,
180 (XtCallbackProc)msgboxCallBackOk, (XtPointer)this);
181 XtAddCallback(wMsgBox, XmNcancelCallback,
182 (XtCallbackProc)msgboxCallBackCancel, (XtPointer)this);
183
184 XtAddCallback(wMsgBox, XmNunmapCallback,
185 (XtCallbackProc)msgboxCallBackClose, (XtPointer)this);
186
187 // show it as a modal dialog
188 XtManageChild(wMsgBox);
189 XtAddGrab(wMsgBox, True, False);
190
191 // the m_result will be changed when message box goes away
192 m_result = -1;
193
194 // local message loop
195 XtAppContext context = XtWidgetToApplicationContext(wParent);
196 XEvent event;
197 while ( m_result == -1 )
198 {
199 XtAppNextEvent(context, &event);
200 XtDispatchEvent(&event);
201 }
202
203 // translate the result if necessary
204 if ( m_dialogStyle & wxYES_NO )
205 {
206 if ( m_result == wxID_OK )
207 m_result = wxID_YES;
208 else if ( m_result == wxID_CANCEL )
209 m_result = wxID_NO;
210 }
211
212 return m_result;
213 }
214