osx new layout
[wxWidgets.git] / src / osx / carbon / msgdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/msgdlg.cpp
3 // Purpose: wxMessageDialog
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/msgdlg.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/app.h"
19 #endif
20
21 #include "wx/thread.h"
22 #include "wx/osx/uma.h"
23
24
25 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
26
27
28 wxMessageDialog::wxMessageDialog(wxWindow *parent,
29 const wxString& message,
30 const wxString& caption,
31 long style,
32 const wxPoint& WXUNUSED(pos))
33 : wxMessageDialogBase(parent, message, caption, style)
34 {
35 m_yes = _("Yes");
36 m_no = _("No");
37 m_ok = _("OK");
38 m_cancel = _("Cancel");
39 }
40
41 bool wxMessageDialog::SetYesNoLabels(const wxString& yes,const wxString& no)
42 {
43 m_yes = yes;
44 m_no = no;
45 return true;
46 }
47
48 bool wxMessageDialog::SetYesNoCancelLabels(const wxString& yes, const wxString& no, const wxString& cancel)
49 {
50 m_yes = yes;
51 m_no = no;
52 m_cancel = cancel;
53 return true;
54 }
55
56 bool wxMessageDialog::SetOKLabel(const wxString& ok)
57 {
58 m_ok = ok;
59 return true;
60 }
61
62 bool wxMessageDialog::SetOKCancelLabels(const wxString& ok, const wxString& cancel)
63 {
64 m_ok = ok;
65 m_cancel = cancel;
66 return true;
67 }
68
69 int wxMessageDialog::ShowModal()
70 {
71 int resultbutton = wxID_CANCEL;
72
73 const long style = GetMessageDialogStyle();
74
75 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
76
77 AlertType alertType = kAlertPlainAlert;
78 if (style & wxICON_EXCLAMATION)
79 alertType = kAlertCautionAlert;
80 else if (style & wxICON_HAND)
81 alertType = kAlertStopAlert;
82 else if (style & wxICON_INFORMATION)
83 alertType = kAlertNoteAlert;
84 else if (style & wxICON_QUESTION)
85 alertType = kAlertNoteAlert;
86
87
88 // work out what to display
89 // if the extended text is empty then we use the caption as the title
90 // and the message as the text (for backwards compatibility)
91 // but if the extended message is not empty then we use the message as the title
92 // and the extended message as the text because that makes more sense
93
94 wxString msgtitle,msgtext;
95 if(m_extendedMessage.IsEmpty())
96 {
97 msgtitle = m_caption;
98 msgtext = m_message;
99 }
100 else
101 {
102 msgtitle = m_message;
103 msgtext = m_extendedMessage;
104 }
105
106
107 if ( !wxIsMainThread() )
108 {
109 CFStringRef defaultButtonTitle = NULL;
110 CFStringRef alternateButtonTitle = NULL;
111 CFStringRef otherButtonTitle = NULL;
112
113 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
114 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
115
116 wxCFStringRef cfNoString( m_no.c_str(), GetFont().GetEncoding() );
117 wxCFStringRef cfYesString( m_yes.c_str(), GetFont().GetEncoding() );
118 wxCFStringRef cfOKString( m_ok.c_str() , GetFont().GetEncoding()) ;
119 wxCFStringRef cfCancelString( m_cancel.c_str(), GetFont().GetEncoding() );
120
121 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
122
123 if (style & wxYES_NO)
124 {
125 if ( style & wxNO_DEFAULT )
126 {
127 defaultButtonTitle = cfNoString;
128 alternateButtonTitle = cfYesString;
129 buttonId[0] = wxID_NO;
130 buttonId[1] = wxID_YES;
131 }
132 else
133 {
134 defaultButtonTitle = cfYesString;
135 alternateButtonTitle = cfNoString;
136 buttonId[0] = wxID_YES;
137 buttonId[1] = wxID_NO;
138 }
139 if (style & wxCANCEL)
140 {
141 otherButtonTitle = cfCancelString;
142 buttonId[2] = wxID_CANCEL;
143 }
144 }
145 else
146 {
147 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
148 buttonId[0] = wxID_OK;
149 // using null as default title does not work on earlier systems
150 defaultButtonTitle = cfOKString;
151 if (style & wxCANCEL)
152 {
153 alternateButtonTitle = cfCancelString;
154 buttonId[1] = wxID_CANCEL;
155 }
156 }
157
158 CFOptionFlags exitButton;
159 OSStatus err = CFUserNotificationDisplayAlert(
160 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
161 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
162 if (err == noErr)
163 resultbutton = buttonId[exitButton];
164 }
165 else
166 {
167 short result;
168
169 AlertStdCFStringAlertParamRec param;
170 wxCFStringRef cfNoString( m_no.c_str(), GetFont().GetEncoding() );
171 wxCFStringRef cfYesString( m_yes.c_str(), GetFont().GetEncoding() );
172 wxCFStringRef cfOKString( m_ok.c_str(), GetFont().GetEncoding() );
173 wxCFStringRef cfCancelString( m_cancel.c_str(), GetFont().GetEncoding() );
174
175 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
176 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
177
178 param.movable = true;
179 param.flags = 0;
180 param.version = kStdCFStringAlertVersionOne;
181
182 bool skipDialog = false;
183
184 if (style & wxYES_NO)
185 {
186 if (style & wxCANCEL)
187 {
188 param.defaultText = cfYesString;
189 param.cancelText = cfCancelString;
190 param.otherText = cfNoString;
191 param.helpButton = false;
192 param.defaultButton = style & wxNO_DEFAULT ? kAlertStdAlertOtherButton : kAlertStdAlertOKButton;
193 param.cancelButton = kAlertStdAlertCancelButton;
194 }
195 else
196 {
197 param.defaultText = cfYesString;
198 param.cancelText = NULL;
199 param.otherText = cfNoString;
200 param.helpButton = false;
201 param.defaultButton = style & wxNO_DEFAULT ? kAlertStdAlertOtherButton : kAlertStdAlertOKButton;
202 param.cancelButton = 0;
203 }
204 }
205 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
206 else
207 {
208 if (style & wxCANCEL)
209 {
210 // that's a cancel missing
211 param.defaultText = cfOKString;
212 param.cancelText = cfCancelString;
213 param.otherText = NULL;
214 param.helpButton = false;
215 param.defaultButton = kAlertStdAlertOKButton;
216 param.cancelButton = 0;
217 }
218 else
219 {
220 param.defaultText = cfOKString;
221 param.cancelText = NULL;
222 param.otherText = NULL;
223 param.helpButton = false;
224 param.defaultButton = kAlertStdAlertOKButton;
225 param.cancelButton = 0;
226 }
227 }
228
229 param.position = kWindowDefaultPosition;
230 if ( !skipDialog )
231 {
232 DialogRef alertRef;
233 CreateStandardAlert( alertType, cfTitle, cfText, &param, &alertRef );
234 RunStandardAlert( alertRef, NULL, &result );
235 }
236 else
237 {
238 return wxID_CANCEL;
239 }
240
241 if (style & wxOK)
242 {
243 switch ( result )
244 {
245 case 1:
246 resultbutton = wxID_OK;
247 break;
248
249 case 2:
250 // TODO: add Cancel button
251 // if (style & wxCANCEL)
252 // resultbutton = wxID_CANCEL;
253 break;
254
255 case 3:
256 default:
257 break;
258 }
259 }
260 else if (style & wxYES_NO)
261 {
262 switch ( result )
263 {
264 case 1:
265 resultbutton = wxID_YES;
266 break;
267
268 case 2:
269 if (!(style & wxCANCEL))
270 resultbutton = wxID_CANCEL;
271 break;
272
273 case 3:
274 resultbutton = wxID_NO;
275 break;
276
277 default:
278 break;
279 }
280 }
281 }
282
283 return resultbutton;
284 }