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