]> git.saurik.com Git - wxWidgets.git/blame_incremental - 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
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/msgdlg.mm
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/control.h"
22#include "wx/thread.h"
23#include "wx/osx/private.h"
24
25
26IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
27
28
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
46wxMessageDialog::wxMessageDialog(wxWindow *parent,
47 const wxString& message,
48 const wxString& caption,
49 long style,
50 const wxPoint& WXUNUSED(pos))
51 : wxMessageDialogBase(parent, message, caption, style)
52{
53 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
54 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
55}
56
57wxMessageDialog::~wxMessageDialog()
58{
59 [m_sheetDelegate release];
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
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
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() );
102
103 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
104
105 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
106
107 if (style & wxYES_NO)
108 {
109 if ( style & wxNO_DEFAULT )
110 {
111 defaultButtonTitle = cfNoString;
112 alternateButtonTitle = cfYesString;
113 m_buttonId[0] = wxID_NO;
114 m_buttonId[1] = wxID_YES;
115 }
116 else
117 {
118 defaultButtonTitle = cfYesString;
119 alternateButtonTitle = cfNoString;
120 m_buttonId[0] = wxID_YES;
121 m_buttonId[1] = wxID_NO;
122 }
123 if (style & wxCANCEL)
124 {
125 otherButtonTitle = cfCancelString;
126 m_buttonId[2] = wxID_CANCEL;
127 }
128 }
129 else
130 {
131 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
132 m_buttonId[0] = wxID_OK;
133 // using null as default title does not work on earlier systems
134 defaultButtonTitle = cfOKString;
135 if (style & wxCANCEL)
136 {
137 alternateButtonTitle = cfCancelString;
138 m_buttonId[1] = wxID_CANCEL;
139 }
140 }
141
142 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
143
144 CFOptionFlags exitButton;
145 OSStatus err = CFUserNotificationDisplayAlert(
146 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
147 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
148 if (err == noErr)
149 resultbutton = m_buttonId[exitButton];
150 }
151 else
152 {
153 NSAlert* alert = (NSAlert*)ConstructNSAlert();
154
155 int button = -1;
156 button = [alert runModal];
157 [alert release];
158 ModalFinishedCallback(alert, button);
159 }
160
161 return GetReturnCode();
162}
163
164void wxMessageDialog::ShowWindowModal()
165{
166 NSAlert* alert = (NSAlert*)ConstructNSAlert();
167
168 wxNonOwnedWindow* parentWindow = NULL;
169
170 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
171
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();
180 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
181 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
182 contextInfo: nil];
183 }
184}
185
186void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
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 ];
195 else
196 resultbutton = wxID_CANCEL;
197 }
198 SetReturnCode(resultbutton);
199
200 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
201 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
202}
203
204void* wxMessageDialog::ConstructNSAlert()
205{
206 const long style = GetMessageDialogStyle();
207
208 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
209
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
215
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];
229 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
230
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() );
235
236 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
237 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
238
239 [alert setMessageText:cfTitle.AsNSString()];
240 [alert setInformativeText:cfText.AsNSString()];
241 [alert setAlertStyle:alertType];
242
243 m_buttonCount = 0;
244
245 if (style & wxYES_NO)
246 {
247 if ( style & wxNO_DEFAULT )
248 {
249 [alert addButtonWithTitle:cfNoString.AsNSString()];
250 m_buttonId[ m_buttonCount++ ] = wxID_NO;
251 [alert addButtonWithTitle:cfYesString.AsNSString()];
252 m_buttonId[ m_buttonCount++ ] = wxID_YES;
253 }
254 else
255 {
256 [alert addButtonWithTitle:cfYesString.AsNSString()];
257 m_buttonId[ m_buttonCount++ ] = wxID_YES;
258 [alert addButtonWithTitle:cfNoString.AsNSString()];
259 m_buttonId[ m_buttonCount++ ] = wxID_NO;
260 }
261
262 if (style & wxCANCEL)
263 {
264 [alert addButtonWithTitle:cfCancelString.AsNSString()];
265 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
266 }
267 }
268 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
269 else
270 {
271 if ( style & wxCANCEL_DEFAULT )
272 {
273 [alert addButtonWithTitle:cfCancelString.AsNSString()];
274 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
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 }
288 }
289
290 }
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
301 return alert;
302}