]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/msgdlg.mm
Fix harmless unused parameter warning in wxOSX.
[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/evtloop.h"
24#include "wx/modalhook.h"
25#include "wx/osx/private.h"
26
27
28IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
29
30
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
48wxMessageDialog::wxMessageDialog(wxWindow *parent,
49 const wxString& message,
50 const wxString& caption,
51 long style,
52 const wxPoint& WXUNUSED(pos))
53 : wxMessageDialogBase(parent, message, caption, style)
54{
55 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
56 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
57}
58
59wxMessageDialog::~wxMessageDialog()
60{
61 [m_sheetDelegate release];
62}
63
64int wxMessageDialog::ShowModal()
65{
66 WX_HOOK_MODAL_DIALOG();
67
68 wxCFEventLoopPauseIdleEvents pause;
69
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
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
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() );
108
109 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
110
111 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
112
113 if (style & wxYES_NO)
114 {
115 if ( style & wxNO_DEFAULT )
116 {
117 defaultButtonTitle = cfNoString;
118 alternateButtonTitle = cfYesString;
119 m_buttonId[0] = wxID_NO;
120 m_buttonId[1] = wxID_YES;
121 }
122 else
123 {
124 defaultButtonTitle = cfYesString;
125 alternateButtonTitle = cfNoString;
126 m_buttonId[0] = wxID_YES;
127 m_buttonId[1] = wxID_NO;
128 }
129 if (style & wxCANCEL)
130 {
131 otherButtonTitle = cfCancelString;
132 m_buttonId[2] = wxID_CANCEL;
133 }
134 }
135 else
136 {
137 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
138 m_buttonId[0] = wxID_OK;
139 // using null as default title does not work on earlier systems
140 defaultButtonTitle = cfOKString;
141 if (style & wxCANCEL)
142 {
143 alternateButtonTitle = cfCancelString;
144 m_buttonId[1] = wxID_CANCEL;
145 }
146 }
147
148 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
149
150 CFOptionFlags exitButton;
151 OSStatus err = CFUserNotificationDisplayAlert(
152 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
153 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
154 if (err == noErr)
155 resultbutton = m_buttonId[exitButton];
156 }
157 else
158 {
159 NSAlert* alert = (NSAlert*)ConstructNSAlert();
160
161 int button = -1;
162 button = [alert runModal];
163 [alert release];
164 ModalFinishedCallback(alert, button);
165 }
166
167 return GetReturnCode();
168}
169
170void wxMessageDialog::ShowWindowModal()
171{
172 NSAlert* alert = (NSAlert*)ConstructNSAlert();
173
174 wxNonOwnedWindow* parentWindow = NULL;
175
176 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
177
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();
186 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
187 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
188 contextInfo: nil];
189 }
190}
191
192void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
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 ];
201 else
202 resultbutton = wxID_CANCEL;
203 }
204 SetReturnCode(resultbutton);
205
206 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
207 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
208}
209
210void* wxMessageDialog::ConstructNSAlert()
211{
212 const long style = GetMessageDialogStyle();
213
214 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
215
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
221
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];
235 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
236
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() );
241
242 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
243 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
244
245 [alert setMessageText:cfTitle.AsNSString()];
246 [alert setInformativeText:cfText.AsNSString()];
247 [alert setAlertStyle:alertType];
248
249 m_buttonCount = 0;
250
251 if (style & wxYES_NO)
252 {
253 if ( style & wxNO_DEFAULT )
254 {
255 [alert addButtonWithTitle:cfNoString.AsNSString()];
256 m_buttonId[ m_buttonCount++ ] = wxID_NO;
257 [alert addButtonWithTitle:cfYesString.AsNSString()];
258 m_buttonId[ m_buttonCount++ ] = wxID_YES;
259 }
260 else
261 {
262 [alert addButtonWithTitle:cfYesString.AsNSString()];
263 m_buttonId[ m_buttonCount++ ] = wxID_YES;
264 [alert addButtonWithTitle:cfNoString.AsNSString()];
265 m_buttonId[ m_buttonCount++ ] = wxID_NO;
266 }
267
268 if (style & wxCANCEL)
269 {
270 [alert addButtonWithTitle:cfCancelString.AsNSString()];
271 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
272 }
273 }
274 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
275 else
276 {
277 if ( style & wxCANCEL_DEFAULT )
278 {
279 [alert addButtonWithTitle:cfCancelString.AsNSString()];
280 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
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 }
294 }
295
296 }
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
307 return alert;
308}