1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/msgdlg.mm
3 // Purpose: wxMessageDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/msgdlg.h"
21 #include "wx/thread.h"
22 #include "wx/osx/private.h"
25 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
30 NSAlertStyle GetAlertStyleFromWXStyle( long style )
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;
45 wxMessageDialog::wxMessageDialog(wxWindow *parent,
46 const wxString& message,
47 const wxString& caption,
49 const wxPoint& WXUNUSED(pos))
50 : wxMessageDialogBase(parent, message, caption, style)
52 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
53 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
56 wxMessageDialog::~wxMessageDialog()
58 [m_sheetDelegate release];
61 int wxMessageDialog::ShowModal()
63 int resultbutton = wxID_CANCEL;
65 const long style = GetMessageDialogStyle();
67 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
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
75 wxString msgtitle,msgtext;
76 if(m_extendedMessage.IsEmpty())
84 msgtext = m_extendedMessage;
88 if ( !wxIsMainThread() )
90 CFStringRef defaultButtonTitle = NULL;
91 CFStringRef alternateButtonTitle = NULL;
92 CFStringRef otherButtonTitle = NULL;
94 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
95 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
97 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
98 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
99 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding()) ;
100 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
102 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
104 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
106 if (style & wxYES_NO)
108 if ( style & wxNO_DEFAULT )
110 defaultButtonTitle = cfNoString;
111 alternateButtonTitle = cfYesString;
112 m_buttonId[0] = wxID_NO;
113 m_buttonId[1] = wxID_YES;
117 defaultButtonTitle = cfYesString;
118 alternateButtonTitle = cfNoString;
119 m_buttonId[0] = wxID_YES;
120 m_buttonId[1] = wxID_NO;
122 if (style & wxCANCEL)
124 otherButtonTitle = cfCancelString;
125 m_buttonId[2] = wxID_CANCEL;
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)
136 alternateButtonTitle = cfCancelString;
137 m_buttonId[1] = wxID_CANCEL;
141 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
143 CFOptionFlags exitButton;
144 OSStatus err = CFUserNotificationDisplayAlert(
145 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
146 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
148 resultbutton = m_buttonId[exitButton];
152 NSAlert* alert = (NSAlert*)ConstructNSAlert();
155 button = [alert runModal];
157 ModalFinishedCallback(alert, button);
160 return GetReturnCode();
163 void wxMessageDialog::ShowWindowModal()
165 NSAlert* alert = (NSAlert*)ConstructNSAlert();
167 wxNonOwnedWindow* parentWindow = NULL;
169 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
172 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
174 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
178 NSWindow* nativeParent = parentWindow->GetWXWindow();
179 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
180 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
185 void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
187 int resultbutton = wxID_CANCEL;
188 if ( resultCode < NSAlertFirstButtonReturn )
189 resultbutton = wxID_CANCEL;
192 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
193 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
195 resultbutton = wxID_CANCEL;
197 SetReturnCode(resultbutton);
199 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
200 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
203 void* wxMessageDialog::ConstructNSAlert()
205 const long style = GetMessageDialogStyle();
207 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
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
215 wxString msgtitle,msgtext;
216 if(m_extendedMessage.IsEmpty())
218 msgtitle = m_caption;
223 msgtitle = m_message;
224 msgtext = m_extendedMessage;
227 NSAlert* alert = [[NSAlert alloc] init];
228 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
230 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
231 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
232 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
233 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
235 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
236 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
238 [alert setMessageText:cfTitle.AsNSString()];
239 [alert setInformativeText:cfText.AsNSString()];
240 [alert setAlertStyle:alertType];
244 if (style & wxYES_NO)
246 if ( style & wxNO_DEFAULT )
248 [alert addButtonWithTitle:cfNoString.AsNSString()];
249 m_buttonId[ m_buttonCount++ ] = wxID_NO;
250 [alert addButtonWithTitle:cfYesString.AsNSString()];
251 m_buttonId[ m_buttonCount++ ] = wxID_YES;
255 [alert addButtonWithTitle:cfYesString.AsNSString()];
256 m_buttonId[ m_buttonCount++ ] = wxID_YES;
257 [alert addButtonWithTitle:cfNoString.AsNSString()];
258 m_buttonId[ m_buttonCount++ ] = wxID_NO;
261 if (style & wxCANCEL)
263 [alert addButtonWithTitle:cfCancelString.AsNSString()];
264 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
267 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
270 if ( style & wxCANCEL_DEFAULT )
272 [alert addButtonWithTitle:cfCancelString.AsNSString()];
273 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
275 [alert addButtonWithTitle:cfOKString.AsNSString()];
276 m_buttonId[ m_buttonCount++ ] = wxID_OK;
280 [alert addButtonWithTitle:cfOKString.AsNSString()];
281 m_buttonId[ m_buttonCount++ ] = wxID_OK;
282 if (style & wxCANCEL)
284 [alert addButtonWithTitle:cfCancelString.AsNSString()];
285 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
291 if ( style & wxHELP )
293 wxCFStringRef cfHelpString( GetHelpLabel(), GetFont().GetEncoding() );
294 [alert addButtonWithTitle:cfHelpString.AsNSString()];
295 m_buttonId[ m_buttonCount++ ] = wxID_HELP;
298 wxASSERT_MSG( m_buttonCount <= WXSIZEOF(m_buttonId), "Too many buttons" );