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