]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/msgdlg.mm
Add borders if none specified
[wxWidgets.git] / src / osx / cocoa / msgdlg.mm
CommitLineData
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
a9a4f229 7// RCS-ID: $Id$
524c47aa
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
25IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
26
27
0aeac464
SC
28namespace
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
524c47aa
SC
45wxMessageDialog::wxMessageDialog(wxWindow *parent,
46 const wxString& message,
47 const wxString& caption,
48 long style,
49 const wxPoint& WXUNUSED(pos))
ede7b017 50 : wxMessageDialogBase(parent, message, caption, style)
524c47aa 51{
4d3e2dc9
SC
52 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
53 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
54}
55
56wxMessageDialog::~wxMessageDialog()
57{
58 [m_sheetDelegate release];
524c47aa
SC
59}
60
61int 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
524c47aa
SC
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
8e06b283
SC
97 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
98 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
99 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding()) ;
100 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
524c47aa 101
0aeac464
SC
102 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
103
bfa92264 104 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
524c47aa
SC
105
106 if (style & wxYES_NO)
107 {
108 if ( style & wxNO_DEFAULT )
109 {
110 defaultButtonTitle = cfNoString;
111 alternateButtonTitle = cfYesString;
bfa92264
KO
112 m_buttonId[0] = wxID_NO;
113 m_buttonId[1] = wxID_YES;
524c47aa
SC
114 }
115 else
116 {
117 defaultButtonTitle = cfYesString;
118 alternateButtonTitle = cfNoString;
bfa92264
KO
119 m_buttonId[0] = wxID_YES;
120 m_buttonId[1] = wxID_NO;
524c47aa
SC
121 }
122 if (style & wxCANCEL)
123 {
124 otherButtonTitle = cfCancelString;
bfa92264 125 m_buttonId[2] = wxID_CANCEL;
524c47aa
SC
126 }
127 }
128 else
129 {
130 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
bfa92264 131 m_buttonId[0] = wxID_OK;
524c47aa
SC
132 // using null as default title does not work on earlier systems
133 defaultButtonTitle = cfOKString;
134 if (style & wxCANCEL)
135 {
136 alternateButtonTitle = cfCancelString;
bfa92264 137 m_buttonId[1] = wxID_CANCEL;
524c47aa
SC
138 }
139 }
140
7112cdd1
VZ
141 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
142
524c47aa
SC
143 CFOptionFlags exitButton;
144 OSStatus err = CFUserNotificationDisplayAlert(
145 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
146 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
147 if (err == noErr)
bfa92264 148 resultbutton = m_buttonId[exitButton];
524c47aa
SC
149 }
150 else
151 {
bfa92264 152 NSAlert* alert = (NSAlert*)ConstructNSAlert();
03647350 153
bfa92264
KO
154 int button = -1;
155 button = [alert runModal];
156 [alert release];
157 ModalFinishedCallback(alert, button);
158 }
524c47aa 159
bfa92264
KO
160 return GetReturnCode();
161}
524c47aa 162
bfa92264
KO
163void wxMessageDialog::ShowWindowModal()
164{
165 NSAlert* alert = (NSAlert*)ConstructNSAlert();
03647350 166
bfa92264 167 wxNonOwnedWindow* parentWindow = NULL;
524c47aa 168
bfa92264 169 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
1e181c7a 170
bfa92264
KO
171 if (GetParent())
172 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
173
174 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
175
176 if (parentWindow)
177 {
178 NSWindow* nativeParent = parentWindow->GetWXWindow();
4d3e2dc9 179 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
bfa92264
KO
180 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
181 contextInfo: nil];
182 }
183}
184
e7794cf2 185void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
bfa92264
KO
186{
187 int resultbutton = wxID_CANCEL;
188 if ( resultCode < NSAlertFirstButtonReturn )
189 resultbutton = wxID_CANCEL;
190 else
191 {
192 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
193 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
524c47aa 194 else
bfa92264
KO
195 resultbutton = wxID_CANCEL;
196 }
197 SetReturnCode(resultbutton);
198
199 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
200 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
201}
524c47aa 202
bfa92264
KO
203void* wxMessageDialog::ConstructNSAlert()
204{
205 const long style = GetMessageDialogStyle();
ba41a8c6 206
bfa92264 207 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
03647350 208
bfa92264
KO
209 // work out what to display
210 // if the extended text is empty then we use the caption as the title
211 // and the message as the text (for backwards compatibility)
212 // but if the extended message is not empty then we use the message as the title
213 // and the extended message as the text because that makes more sense
03647350 214
bfa92264
KO
215 wxString msgtitle,msgtext;
216 if(m_extendedMessage.IsEmpty())
217 {
218 msgtitle = m_caption;
219 msgtext = m_message;
220 }
221 else
222 {
223 msgtitle = m_message;
224 msgtext = m_extendedMessage;
225 }
226
227 NSAlert* alert = [[NSAlert alloc] init];
0aeac464 228 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
bfa92264
KO
229
230 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
231 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
232 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
233 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
234
235 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
236 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
237
238 [alert setMessageText:cfTitle.AsNSString()];
239 [alert setInformativeText:cfText.AsNSString()];
0aeac464 240 [alert setAlertStyle:alertType];
bfa92264
KO
241
242 m_buttonCount = 0;
243
244 if (style & wxYES_NO)
245 {
246 if ( style & wxNO_DEFAULT )
ba41a8c6 247 {
bfa92264
KO
248 [alert addButtonWithTitle:cfNoString.AsNSString()];
249 m_buttonId[ m_buttonCount++ ] = wxID_NO;
250 [alert addButtonWithTitle:cfYesString.AsNSString()];
251 m_buttonId[ m_buttonCount++ ] = wxID_YES;
ba41a8c6
KO
252 }
253 else
254 {
bfa92264
KO
255 [alert addButtonWithTitle:cfYesString.AsNSString()];
256 m_buttonId[ m_buttonCount++ ] = wxID_YES;
257 [alert addButtonWithTitle:cfNoString.AsNSString()];
258 m_buttonId[ m_buttonCount++ ] = wxID_NO;
ba41a8c6 259 }
03647350 260
bfa92264 261 if (style & wxCANCEL)
524c47aa 262 {
bfa92264
KO
263 [alert addButtonWithTitle:cfCancelString.AsNSString()];
264 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
524c47aa
SC
265 }
266 }
bfa92264
KO
267 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
268 else
269 {
ec46fd5f 270 if ( style & wxCANCEL_DEFAULT )
bfa92264
KO
271 {
272 [alert addButtonWithTitle:cfCancelString.AsNSString()];
273 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
ec46fd5f
SC
274
275 [alert addButtonWithTitle:cfOKString.AsNSString()];
276 m_buttonId[ m_buttonCount++ ] = wxID_OK;
277 }
278 else
279 {
280 [alert addButtonWithTitle:cfOKString.AsNSString()];
281 m_buttonId[ m_buttonCount++ ] = wxID_OK;
282 if (style & wxCANCEL)
283 {
284 [alert addButtonWithTitle:cfCancelString.AsNSString()];
285 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
286 }
bfa92264 287 }
ec46fd5f 288
bfa92264 289 }
7112cdd1
VZ
290
291 if ( style & wxHELP )
292 {
293 wxCFStringRef cfHelpString( GetHelpLabel(), GetFont().GetEncoding() );
294 [alert addButtonWithTitle:cfHelpString.AsNSString()];
295 m_buttonId[ m_buttonCount++ ] = wxID_HELP;
296 }
297
298 wxASSERT_MSG( m_buttonCount <= WXSIZEOF(m_buttonId), "Too many buttons" );
299
bfa92264 300 return alert;
524c47aa 301}