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