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