]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/msgdlg.mm
wiring OnInit on osx to a later point in event processing
[wxWidgets.git] / src / osx / cocoa / msgdlg.mm
CommitLineData
524c47aa
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/msgdlg.mm
3// Purpose: wxMessageDialog
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
a9a4f229 7// RCS-ID: $Id$
524c47aa
SC
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
37e48466 21#include "wx/control.h"
524c47aa 22#include "wx/thread.h"
ffbee810 23#include "wx/evtloop.h"
691745ab 24#include "wx/modalhook.h"
524c47aa
SC
25#include "wx/osx/private.h"
26
27
28IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
29
30
0aeac464
SC
31namespace
32{
33 NSAlertStyle GetAlertStyleFromWXStyle( long style )
34 {
35 NSAlertStyle alertType = NSWarningAlertStyle;
36 if (style & wxICON_EXCLAMATION)
37 alertType = NSCriticalAlertStyle;
38 else if (style & wxICON_HAND)
39 alertType = NSWarningAlertStyle;
40 else if (style & wxICON_INFORMATION)
41 alertType = NSInformationalAlertStyle;
42 else if (style & wxICON_QUESTION)
43 alertType = NSInformationalAlertStyle;
44 return alertType;
45 }
46}
47
524c47aa
SC
48wxMessageDialog::wxMessageDialog(wxWindow *parent,
49 const wxString& message,
50 const wxString& caption,
51 long style,
52 const wxPoint& WXUNUSED(pos))
ede7b017 53 : wxMessageDialogBase(parent, message, caption, style)
524c47aa 54{
4d3e2dc9
SC
55 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
56 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
57}
58
59wxMessageDialog::~wxMessageDialog()
60{
61 [m_sheetDelegate release];
524c47aa
SC
62}
63
64int wxMessageDialog::ShowModal()
65{
691745ab 66 WX_HOOK_MODAL_DIALOG();
643e9cf9 67
c657294b 68 wxCFEventLoopPauseIdleEvents pause;
ffbee810 69
524c47aa
SC
70 int resultbutton = wxID_CANCEL;
71
72 const long style = GetMessageDialogStyle();
73
74 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
75
524c47aa
SC
76 // work out what to display
77 // if the extended text is empty then we use the caption as the title
78 // and the message as the text (for backwards compatibility)
79 // but if the extended message is not empty then we use the message as the title
80 // and the extended message as the text because that makes more sense
81
82 wxString msgtitle,msgtext;
83 if(m_extendedMessage.IsEmpty())
84 {
85 msgtitle = m_caption;
86 msgtext = m_message;
87 }
88 else
89 {
90 msgtitle = m_message;
91 msgtext = m_extendedMessage;
92 }
93
94
95 if ( !wxIsMainThread() )
96 {
97 CFStringRef defaultButtonTitle = NULL;
98 CFStringRef alternateButtonTitle = NULL;
99 CFStringRef otherButtonTitle = NULL;
100
101 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
102 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
103
4d3b8623
SC
104 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
105 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
106 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding()) ;
107 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
524c47aa 108
0aeac464
SC
109 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
110
bfa92264 111 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
524c47aa
SC
112
113 if (style & wxYES_NO)
114 {
115 if ( style & wxNO_DEFAULT )
116 {
117 defaultButtonTitle = cfNoString;
118 alternateButtonTitle = cfYesString;
bfa92264
KO
119 m_buttonId[0] = wxID_NO;
120 m_buttonId[1] = wxID_YES;
524c47aa
SC
121 }
122 else
123 {
124 defaultButtonTitle = cfYesString;
125 alternateButtonTitle = cfNoString;
bfa92264
KO
126 m_buttonId[0] = wxID_YES;
127 m_buttonId[1] = wxID_NO;
524c47aa
SC
128 }
129 if (style & wxCANCEL)
130 {
131 otherButtonTitle = cfCancelString;
bfa92264 132 m_buttonId[2] = wxID_CANCEL;
524c47aa
SC
133 }
134 }
135 else
136 {
137 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
bfa92264 138 m_buttonId[0] = wxID_OK;
524c47aa
SC
139 // using null as default title does not work on earlier systems
140 defaultButtonTitle = cfOKString;
141 if (style & wxCANCEL)
142 {
143 alternateButtonTitle = cfCancelString;
bfa92264 144 m_buttonId[1] = wxID_CANCEL;
524c47aa
SC
145 }
146 }
147
7112cdd1
VZ
148 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
149
524c47aa
SC
150 CFOptionFlags exitButton;
151 OSStatus err = CFUserNotificationDisplayAlert(
152 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
153 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
154 if (err == noErr)
bfa92264 155 resultbutton = m_buttonId[exitButton];
524c47aa
SC
156 }
157 else
158 {
bfa92264 159 NSAlert* alert = (NSAlert*)ConstructNSAlert();
03647350 160
bfa92264
KO
161 int button = -1;
162 button = [alert runModal];
163 [alert release];
164 ModalFinishedCallback(alert, button);
165 }
524c47aa 166
bfa92264
KO
167 return GetReturnCode();
168}
524c47aa 169
bfa92264
KO
170void wxMessageDialog::ShowWindowModal()
171{
172 NSAlert* alert = (NSAlert*)ConstructNSAlert();
03647350 173
bfa92264 174 wxNonOwnedWindow* parentWindow = NULL;
524c47aa 175
bfa92264 176 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
1e181c7a 177
bfa92264
KO
178 if (GetParent())
179 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
180
181 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
182
183 if (parentWindow)
184 {
185 NSWindow* nativeParent = parentWindow->GetWXWindow();
4d3e2dc9 186 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
bfa92264
KO
187 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
188 contextInfo: nil];
189 }
190}
191
e7794cf2 192void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
bfa92264
KO
193{
194 int resultbutton = wxID_CANCEL;
195 if ( resultCode < NSAlertFirstButtonReturn )
196 resultbutton = wxID_CANCEL;
197 else
198 {
199 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
200 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
524c47aa 201 else
bfa92264
KO
202 resultbutton = wxID_CANCEL;
203 }
204 SetReturnCode(resultbutton);
205
206 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
207 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
208}
524c47aa 209
bfa92264
KO
210void* wxMessageDialog::ConstructNSAlert()
211{
212 const long style = GetMessageDialogStyle();
ba41a8c6 213
bfa92264 214 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
03647350 215
bfa92264
KO
216 // work out what to display
217 // if the extended text is empty then we use the caption as the title
218 // and the message as the text (for backwards compatibility)
219 // but if the extended message is not empty then we use the message as the title
220 // and the extended message as the text because that makes more sense
03647350 221
bfa92264
KO
222 wxString msgtitle,msgtext;
223 if(m_extendedMessage.IsEmpty())
224 {
225 msgtitle = m_caption;
226 msgtext = m_message;
227 }
228 else
229 {
230 msgtitle = m_message;
231 msgtext = m_extendedMessage;
232 }
233
234 NSAlert* alert = [[NSAlert alloc] init];
0aeac464 235 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
bfa92264 236
4d3b8623
SC
237 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
238 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
239 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding() );
240 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
bfa92264
KO
241
242 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
243 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
244
245 [alert setMessageText:cfTitle.AsNSString()];
246 [alert setInformativeText:cfText.AsNSString()];
0aeac464 247 [alert setAlertStyle:alertType];
bfa92264
KO
248
249 m_buttonCount = 0;
250
251 if (style & wxYES_NO)
252 {
253 if ( style & wxNO_DEFAULT )
ba41a8c6 254 {
bfa92264
KO
255 [alert addButtonWithTitle:cfNoString.AsNSString()];
256 m_buttonId[ m_buttonCount++ ] = wxID_NO;
257 [alert addButtonWithTitle:cfYesString.AsNSString()];
258 m_buttonId[ m_buttonCount++ ] = wxID_YES;
ba41a8c6
KO
259 }
260 else
261 {
bfa92264
KO
262 [alert addButtonWithTitle:cfYesString.AsNSString()];
263 m_buttonId[ m_buttonCount++ ] = wxID_YES;
264 [alert addButtonWithTitle:cfNoString.AsNSString()];
265 m_buttonId[ m_buttonCount++ ] = wxID_NO;
ba41a8c6 266 }
03647350 267
bfa92264 268 if (style & wxCANCEL)
524c47aa 269 {
bfa92264
KO
270 [alert addButtonWithTitle:cfCancelString.AsNSString()];
271 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
524c47aa
SC
272 }
273 }
bfa92264
KO
274 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
275 else
276 {
ec46fd5f 277 if ( style & wxCANCEL_DEFAULT )
bfa92264
KO
278 {
279 [alert addButtonWithTitle:cfCancelString.AsNSString()];
280 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
ec46fd5f
SC
281
282 [alert addButtonWithTitle:cfOKString.AsNSString()];
283 m_buttonId[ m_buttonCount++ ] = wxID_OK;
284 }
285 else
286 {
287 [alert addButtonWithTitle:cfOKString.AsNSString()];
288 m_buttonId[ m_buttonCount++ ] = wxID_OK;
289 if (style & wxCANCEL)
290 {
291 [alert addButtonWithTitle:cfCancelString.AsNSString()];
292 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
293 }
bfa92264 294 }
ec46fd5f 295
bfa92264 296 }
7112cdd1
VZ
297
298 if ( style & wxHELP )
299 {
300 wxCFStringRef cfHelpString( GetHelpLabel(), GetFont().GetEncoding() );
301 [alert addButtonWithTitle:cfHelpString.AsNSString()];
302 m_buttonId[ m_buttonCount++ ] = wxID_HELP;
303 }
304
305 wxASSERT_MSG( m_buttonCount <= WXSIZEOF(m_buttonId), "Too many buttons" );
306
bfa92264 307 return alert;
524c47aa 308}