1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/msgdlg.mm
3 // Purpose: wxMessageDialog
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: msgdlg.cpp 54129 2008-06-11 19:30:52Z SC $
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 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
54 int wxMessageDialog::ShowModal()
56 int resultbutton = wxID_CANCEL;
58 const long style = GetMessageDialogStyle();
60 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
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
68 wxString msgtitle,msgtext;
69 if(m_extendedMessage.IsEmpty())
77 msgtext = m_extendedMessage;
81 if ( !wxIsMainThread() )
83 CFStringRef defaultButtonTitle = NULL;
84 CFStringRef alternateButtonTitle = NULL;
85 CFStringRef otherButtonTitle = NULL;
87 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
88 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
90 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
91 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
92 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding()) ;
93 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
95 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
97 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
101 if ( style & wxNO_DEFAULT )
103 defaultButtonTitle = cfNoString;
104 alternateButtonTitle = cfYesString;
105 m_buttonId[0] = wxID_NO;
106 m_buttonId[1] = wxID_YES;
110 defaultButtonTitle = cfYesString;
111 alternateButtonTitle = cfNoString;
112 m_buttonId[0] = wxID_YES;
113 m_buttonId[1] = wxID_NO;
115 if (style & wxCANCEL)
117 otherButtonTitle = cfCancelString;
118 m_buttonId[2] = wxID_CANCEL;
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)
129 alternateButtonTitle = cfCancelString;
130 m_buttonId[1] = wxID_CANCEL;
134 CFOptionFlags exitButton;
135 OSStatus err = CFUserNotificationDisplayAlert(
136 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
137 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
139 resultbutton = m_buttonId[exitButton];
143 NSAlert* alert = (NSAlert*)ConstructNSAlert();
146 button = [alert runModal];
148 ModalFinishedCallback(alert, button);
151 return GetReturnCode();
154 void wxMessageDialog::ShowWindowModal()
156 NSAlert* alert = (NSAlert*)ConstructNSAlert();
158 wxNonOwnedWindow* parentWindow = NULL;
160 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
163 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
165 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
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:)
178 void wxMessageDialog::ModalFinishedCallback(void* panel, int resultCode)
180 int resultbutton = wxID_CANCEL;
181 if ( resultCode < NSAlertFirstButtonReturn )
182 resultbutton = wxID_CANCEL;
185 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
186 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
188 resultbutton = wxID_CANCEL;
190 SetReturnCode(resultbutton);
192 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
193 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
196 void* wxMessageDialog::ConstructNSAlert()
198 const long style = GetMessageDialogStyle();
200 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
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
208 wxString msgtitle,msgtext;
209 if(m_extendedMessage.IsEmpty())
211 msgtitle = m_caption;
216 msgtitle = m_message;
217 msgtext = m_extendedMessage;
220 NSAlert* alert = [[NSAlert alloc] init];
221 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
223 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
224 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
225 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
226 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
228 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
229 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
231 [alert setMessageText:cfTitle.AsNSString()];
232 [alert setInformativeText:cfText.AsNSString()];
233 [alert setAlertStyle:alertType];
237 if (style & wxYES_NO)
239 if ( style & wxNO_DEFAULT )
241 [alert addButtonWithTitle:cfNoString.AsNSString()];
242 m_buttonId[ m_buttonCount++ ] = wxID_NO;
243 [alert addButtonWithTitle:cfYesString.AsNSString()];
244 m_buttonId[ m_buttonCount++ ] = wxID_YES;
248 [alert addButtonWithTitle:cfYesString.AsNSString()];
249 m_buttonId[ m_buttonCount++ ] = wxID_YES;
250 [alert addButtonWithTitle:cfNoString.AsNSString()];
251 m_buttonId[ m_buttonCount++ ] = wxID_NO;
254 if (style & wxCANCEL)
256 [alert addButtonWithTitle:cfCancelString.AsNSString()];
257 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
260 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
263 [alert addButtonWithTitle:cfOKString.AsNSString()];
264 m_buttonId[ m_buttonCount++ ] = wxID_OK;
265 if (style & wxCANCEL)
267 [alert addButtonWithTitle:cfCancelString.AsNSString()];
268 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;