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/control.h"
22 #include "wx/thread.h"
23 #include "wx/osx/private.h"
26 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
31 NSAlertStyle GetAlertStyleFromWXStyle( long style )
33 NSAlertStyle alertType = NSWarningAlertStyle;
34 if (style & wxICON_EXCLAMATION)
35 alertType = NSCriticalAlertStyle;
36 else if (style & wxICON_HAND)
37 alertType = NSWarningAlertStyle;
38 else if (style & wxICON_INFORMATION)
39 alertType = NSInformationalAlertStyle;
40 else if (style & wxICON_QUESTION)
41 alertType = NSInformationalAlertStyle;
46 wxMessageDialog::wxMessageDialog(wxWindow *parent,
47 const wxString& message,
48 const wxString& caption,
50 const wxPoint& WXUNUSED(pos))
51 : wxMessageDialogBase(parent, message, caption, style)
53 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
54 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
57 wxMessageDialog::~wxMessageDialog()
59 [m_sheetDelegate release];
62 int wxMessageDialog::ShowModal()
64 int resultbutton = wxID_CANCEL;
66 const long style = GetMessageDialogStyle();
68 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
70 // work out what to display
71 // if the extended text is empty then we use the caption as the title
72 // and the message as the text (for backwards compatibility)
73 // but if the extended message is not empty then we use the message as the title
74 // and the extended message as the text because that makes more sense
76 wxString msgtitle,msgtext;
77 if(m_extendedMessage.IsEmpty())
85 msgtext = m_extendedMessage;
89 if ( !wxIsMainThread() )
91 CFStringRef defaultButtonTitle = NULL;
92 CFStringRef alternateButtonTitle = NULL;
93 CFStringRef otherButtonTitle = NULL;
95 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
96 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
98 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
99 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
100 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding()) ;
101 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
103 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
105 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
107 if (style & wxYES_NO)
109 if ( style & wxNO_DEFAULT )
111 defaultButtonTitle = cfNoString;
112 alternateButtonTitle = cfYesString;
113 m_buttonId[0] = wxID_NO;
114 m_buttonId[1] = wxID_YES;
118 defaultButtonTitle = cfYesString;
119 alternateButtonTitle = cfNoString;
120 m_buttonId[0] = wxID_YES;
121 m_buttonId[1] = wxID_NO;
123 if (style & wxCANCEL)
125 otherButtonTitle = cfCancelString;
126 m_buttonId[2] = wxID_CANCEL;
131 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
132 m_buttonId[0] = wxID_OK;
133 // using null as default title does not work on earlier systems
134 defaultButtonTitle = cfOKString;
135 if (style & wxCANCEL)
137 alternateButtonTitle = cfCancelString;
138 m_buttonId[1] = wxID_CANCEL;
142 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
144 CFOptionFlags exitButton;
145 OSStatus err = CFUserNotificationDisplayAlert(
146 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
147 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
149 resultbutton = m_buttonId[exitButton];
153 NSAlert* alert = (NSAlert*)ConstructNSAlert();
156 button = [alert runModal];
158 ModalFinishedCallback(alert, button);
161 return GetReturnCode();
164 void wxMessageDialog::ShowWindowModal()
166 NSAlert* alert = (NSAlert*)ConstructNSAlert();
168 wxNonOwnedWindow* parentWindow = NULL;
170 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
173 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
175 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
179 NSWindow* nativeParent = parentWindow->GetWXWindow();
180 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
181 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
186 void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
188 int resultbutton = wxID_CANCEL;
189 if ( resultCode < NSAlertFirstButtonReturn )
190 resultbutton = wxID_CANCEL;
193 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
194 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
196 resultbutton = wxID_CANCEL;
198 SetReturnCode(resultbutton);
200 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
201 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
204 void* wxMessageDialog::ConstructNSAlert()
206 const long style = GetMessageDialogStyle();
208 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
210 // work out what to display
211 // if the extended text is empty then we use the caption as the title
212 // and the message as the text (for backwards compatibility)
213 // but if the extended message is not empty then we use the message as the title
214 // and the extended message as the text because that makes more sense
216 wxString msgtitle,msgtext;
217 if(m_extendedMessage.IsEmpty())
219 msgtitle = m_caption;
224 msgtitle = m_message;
225 msgtext = m_extendedMessage;
228 NSAlert* alert = [[NSAlert alloc] init];
229 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
231 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
232 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
233 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding() );
234 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
236 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
237 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
239 [alert setMessageText:cfTitle.AsNSString()];
240 [alert setInformativeText:cfText.AsNSString()];
241 [alert setAlertStyle:alertType];
245 if (style & wxYES_NO)
247 if ( style & wxNO_DEFAULT )
249 [alert addButtonWithTitle:cfNoString.AsNSString()];
250 m_buttonId[ m_buttonCount++ ] = wxID_NO;
251 [alert addButtonWithTitle:cfYesString.AsNSString()];
252 m_buttonId[ m_buttonCount++ ] = wxID_YES;
256 [alert addButtonWithTitle:cfYesString.AsNSString()];
257 m_buttonId[ m_buttonCount++ ] = wxID_YES;
258 [alert addButtonWithTitle:cfNoString.AsNSString()];
259 m_buttonId[ m_buttonCount++ ] = wxID_NO;
262 if (style & wxCANCEL)
264 [alert addButtonWithTitle:cfCancelString.AsNSString()];
265 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
268 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
271 if ( style & wxCANCEL_DEFAULT )
273 [alert addButtonWithTitle:cfCancelString.AsNSString()];
274 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
276 [alert addButtonWithTitle:cfOKString.AsNSString()];
277 m_buttonId[ m_buttonCount++ ] = wxID_OK;
281 [alert addButtonWithTitle:cfOKString.AsNSString()];
282 m_buttonId[ m_buttonCount++ ] = wxID_OK;
283 if (style & wxCANCEL)
285 [alert addButtonWithTitle:cfCancelString.AsNSString()];
286 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
292 if ( style & wxHELP )
294 wxCFStringRef cfHelpString( GetHelpLabel(), GetFont().GetEncoding() );
295 [alert addButtonWithTitle:cfHelpString.AsNSString()];
296 m_buttonId[ m_buttonCount++ ] = wxID_HELP;
299 wxASSERT_MSG( m_buttonCount <= WXSIZEOF(m_buttonId), "Too many buttons" );