implement support for custom button labels in wxMessageBox under MSW; refactor the...
[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 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
34 {
35 }
36
37 int wxMessageDialog::ShowModal()
38 {
39 int resultbutton = wxID_CANCEL;
40
41 const long style = GetMessageDialogStyle();
42
43 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
44
45 AlertType alertType = kAlertPlainAlert;
46 if (style & wxICON_EXCLAMATION)
47 alertType = kAlertCautionAlert;
48 else if (style & wxICON_HAND)
49 alertType = kAlertStopAlert;
50 else if (style & wxICON_INFORMATION)
51 alertType = kAlertNoteAlert;
52 else if (style & wxICON_QUESTION)
53 alertType = kAlertNoteAlert;
54
55
56 // work out what to display
57 // if the extended text is empty then we use the caption as the title
58 // and the message as the text (for backwards compatibility)
59 // but if the extended message is not empty then we use the message as the title
60 // and the extended message as the text because that makes more sense
61
62 wxString msgtitle,msgtext;
63 if(m_extendedMessage.IsEmpty())
64 {
65 msgtitle = m_caption;
66 msgtext = m_message;
67 }
68 else
69 {
70 msgtitle = m_message;
71 msgtext = m_extendedMessage;
72 }
73
74
75 if ( !wxIsMainThread() )
76 {
77 CFStringRef defaultButtonTitle = NULL;
78 CFStringRef alternateButtonTitle = NULL;
79 CFStringRef otherButtonTitle = NULL;
80
81 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
82 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
83
84 wxCFStringRef cfNoString( GetNoLabel().c_str(), GetFont().GetEncoding() );
85 wxCFStringRef cfYesString( GetYesLabel().c_str(), GetFont().GetEncoding() );
86 wxCFStringRef cfOKString( GetOKLabel().c_str() , GetFont().GetEncoding()) ;
87 wxCFStringRef cfCancelString( GetCancelLabel().c_str(), GetFont().GetEncoding() );
88
89 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
90
91 if (style & wxYES_NO)
92 {
93 if ( style & wxNO_DEFAULT )
94 {
95 defaultButtonTitle = cfNoString;
96 alternateButtonTitle = cfYesString;
97 buttonId[0] = wxID_NO;
98 buttonId[1] = wxID_YES;
99 }
100 else
101 {
102 defaultButtonTitle = cfYesString;
103 alternateButtonTitle = cfNoString;
104 buttonId[0] = wxID_YES;
105 buttonId[1] = wxID_NO;
106 }
107 if (style & wxCANCEL)
108 {
109 otherButtonTitle = cfCancelString;
110 buttonId[2] = wxID_CANCEL;
111 }
112 }
113 else
114 {
115 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
116 buttonId[0] = wxID_OK;
117 // using null as default title does not work on earlier systems
118 defaultButtonTitle = cfOKString;
119 if (style & wxCANCEL)
120 {
121 alternateButtonTitle = cfCancelString;
122 buttonId[1] = wxID_CANCEL;
123 }
124 }
125
126 CFOptionFlags exitButton;
127 OSStatus err = CFUserNotificationDisplayAlert(
128 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
129 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
130 if (err == noErr)
131 resultbutton = buttonId[exitButton];
132 }
133 else
134 {
135 short result;
136
137 AlertStdCFStringAlertParamRec param;
138 wxCFStringRef cfNoString( GetNoLabel().c_str(), GetFont().GetEncoding() );
139 wxCFStringRef cfYesString( GetYesLabel().c_str(), GetFont().GetEncoding() );
140 wxCFStringRef cfOKString( GetOKLabel().c_str(), GetFont().GetEncoding() );
141 wxCFStringRef cfCancelString( GetCancelLabel().c_str(), GetFont().GetEncoding() );
142
143 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
144 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
145
146 param.movable = true;
147 param.flags = 0;
148 param.version = kStdCFStringAlertVersionOne;
149
150 bool skipDialog = false;
151
152 if (style & wxYES_NO)
153 {
154 if (style & wxCANCEL)
155 {
156 param.defaultText = cfYesString;
157 param.cancelText = cfCancelString;
158 param.otherText = cfNoString;
159 param.helpButton = false;
160 param.defaultButton = style & wxNO_DEFAULT ? kAlertStdAlertOtherButton : kAlertStdAlertOKButton;
161 param.cancelButton = kAlertStdAlertCancelButton;
162 }
163 else
164 {
165 param.defaultText = cfYesString;
166 param.cancelText = NULL;
167 param.otherText = cfNoString;
168 param.helpButton = false;
169 param.defaultButton = style & wxNO_DEFAULT ? kAlertStdAlertOtherButton : kAlertStdAlertOKButton;
170 param.cancelButton = 0;
171 }
172 }
173 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
174 else
175 {
176 if (style & wxCANCEL)
177 {
178 // that's a cancel missing
179 param.defaultText = cfOKString;
180 param.cancelText = cfCancelString;
181 param.otherText = NULL;
182 param.helpButton = false;
183 param.defaultButton = kAlertStdAlertOKButton;
184 param.cancelButton = 0;
185 }
186 else
187 {
188 param.defaultText = cfOKString;
189 param.cancelText = NULL;
190 param.otherText = NULL;
191 param.helpButton = false;
192 param.defaultButton = kAlertStdAlertOKButton;
193 param.cancelButton = 0;
194 }
195 }
196
197 param.position = kWindowDefaultPosition;
198 if ( !skipDialog )
199 {
200 DialogRef alertRef;
201 CreateStandardAlert( alertType, cfTitle, cfText, &param, &alertRef );
202 RunStandardAlert( alertRef, NULL, &result );
203 }
204 else
205 {
206 return wxID_CANCEL;
207 }
208
209 if (style & wxOK)
210 {
211 switch ( result )
212 {
213 case 1:
214 resultbutton = wxID_OK;
215 break;
216
217 case 2:
218 // TODO: add Cancel button
219 // if (style & wxCANCEL)
220 // resultbutton = wxID_CANCEL;
221 break;
222
223 case 3:
224 default:
225 break;
226 }
227 }
228 else if (style & wxYES_NO)
229 {
230 switch ( result )
231 {
232 case 1:
233 resultbutton = wxID_YES;
234 break;
235
236 case 2:
237 if (!(style & wxCANCEL))
238 resultbutton = wxID_CANCEL;
239 break;
240
241 case 3:
242 resultbutton = wxID_NO;
243 break;
244
245 default:
246 break;
247 }
248 }
249 }
250
251 return resultbutton;
252 }