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/evtloop.h"
24 #include "wx/modalhook.h"
25 #include "wx/osx/private.h"
28 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
33 NSAlertStyle GetAlertStyleFromWXStyle( long style )
35 NSAlertStyle alertType = NSWarningAlertStyle;
36 if (style & wxICON_EXCLAMATION)
37 alertType = NSCriticalAlertStyle;
38 else if (style & wxICON_HAND)
39 alertType = NSWarningAlertStyle;
40 else if (style & wxICON_INFORMATION)
41 alertType = NSInformationalAlertStyle;
42 else if (style & wxICON_QUESTION)
43 alertType = NSInformationalAlertStyle;
48 wxMessageDialog::wxMessageDialog(wxWindow *parent,
49 const wxString& message,
50 const wxString& caption,
52 const wxPoint& WXUNUSED(pos))
53 : wxMessageDialogBase(parent, message, caption, style)
55 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
56 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
59 wxMessageDialog::~wxMessageDialog()
61 [m_sheetDelegate release];
64 int wxMessageDialog::ShowModal()
66 WX_HOOK_MODAL_DIALOG();
68 wxCFEventLoopPauseIdleEvents pause;
70 int resultbutton = wxID_CANCEL;
72 const long style = GetMessageDialogStyle();
74 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
76 // work out what to display
77 // if the extended text is empty then we use the caption as the title
78 // and the message as the text (for backwards compatibility)
79 // but if the extended message is not empty then we use the message as the title
80 // and the extended message as the text because that makes more sense
82 wxString msgtitle,msgtext;
83 if(m_extendedMessage.IsEmpty())
91 msgtext = m_extendedMessage;
95 if ( !wxIsMainThread() )
97 CFStringRef defaultButtonTitle = NULL;
98 CFStringRef alternateButtonTitle = NULL;
99 CFStringRef otherButtonTitle = NULL;
101 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
102 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
104 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
105 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
106 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding()) ;
107 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
109 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
111 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
113 if (style & wxYES_NO)
115 if ( style & wxNO_DEFAULT )
117 defaultButtonTitle = cfNoString;
118 alternateButtonTitle = cfYesString;
119 m_buttonId[0] = wxID_NO;
120 m_buttonId[1] = wxID_YES;
124 defaultButtonTitle = cfYesString;
125 alternateButtonTitle = cfNoString;
126 m_buttonId[0] = wxID_YES;
127 m_buttonId[1] = wxID_NO;
129 if (style & wxCANCEL)
131 otherButtonTitle = cfCancelString;
132 m_buttonId[2] = wxID_CANCEL;
137 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
138 m_buttonId[0] = wxID_OK;
139 // using null as default title does not work on earlier systems
140 defaultButtonTitle = cfOKString;
141 if (style & wxCANCEL)
143 alternateButtonTitle = cfCancelString;
144 m_buttonId[1] = wxID_CANCEL;
148 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
150 CFOptionFlags exitButton;
151 OSStatus err = CFUserNotificationDisplayAlert(
152 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
153 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
155 resultbutton = m_buttonId[exitButton];
159 NSAlert* alert = (NSAlert*)ConstructNSAlert();
162 button = [alert runModal];
164 ModalFinishedCallback(alert, button);
167 return GetReturnCode();
170 void wxMessageDialog::ShowWindowModal()
172 NSAlert* alert = (NSAlert*)ConstructNSAlert();
174 wxNonOwnedWindow* parentWindow = NULL;
176 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
179 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
181 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
185 NSWindow* nativeParent = parentWindow->GetWXWindow();
186 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
187 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
192 void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
194 int resultbutton = wxID_CANCEL;
195 if ( resultCode < NSAlertFirstButtonReturn )
196 resultbutton = wxID_CANCEL;
199 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
200 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
202 resultbutton = wxID_CANCEL;
204 SetReturnCode(resultbutton);
206 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
207 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
210 void* wxMessageDialog::ConstructNSAlert()
212 const long style = GetMessageDialogStyle();
214 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
216 // work out what to display
217 // if the extended text is empty then we use the caption as the title
218 // and the message as the text (for backwards compatibility)
219 // but if the extended message is not empty then we use the message as the title
220 // and the extended message as the text because that makes more sense
222 wxString msgtitle,msgtext;
223 if(m_extendedMessage.IsEmpty())
225 msgtitle = m_caption;
230 msgtitle = m_message;
231 msgtext = m_extendedMessage;
234 NSAlert* alert = [[NSAlert alloc] init];
235 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
237 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
238 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
239 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding() );
240 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
242 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
243 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
245 [alert setMessageText:cfTitle.AsNSString()];
246 [alert setInformativeText:cfText.AsNSString()];
247 [alert setAlertStyle:alertType];
251 if (style & wxYES_NO)
253 if ( style & wxNO_DEFAULT )
255 [alert addButtonWithTitle:cfNoString.AsNSString()];
256 m_buttonId[ m_buttonCount++ ] = wxID_NO;
257 [alert addButtonWithTitle:cfYesString.AsNSString()];
258 m_buttonId[ m_buttonCount++ ] = wxID_YES;
262 [alert addButtonWithTitle:cfYesString.AsNSString()];
263 m_buttonId[ m_buttonCount++ ] = wxID_YES;
264 [alert addButtonWithTitle:cfNoString.AsNSString()];
265 m_buttonId[ m_buttonCount++ ] = wxID_NO;
268 if (style & wxCANCEL)
270 [alert addButtonWithTitle:cfCancelString.AsNSString()];
271 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
274 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
277 if ( style & wxCANCEL_DEFAULT )
279 [alert addButtonWithTitle:cfCancelString.AsNSString()];
280 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
282 [alert addButtonWithTitle:cfOKString.AsNSString()];
283 m_buttonId[ m_buttonCount++ ] = wxID_OK;
287 [alert addButtonWithTitle:cfOKString.AsNSString()];
288 m_buttonId[ m_buttonCount++ ] = wxID_OK;
289 if (style & wxCANCEL)
291 [alert addButtonWithTitle:cfCancelString.AsNSString()];
292 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
298 if ( style & wxHELP )
300 wxCFStringRef cfHelpString( GetHelpLabel(), GetFont().GetEncoding() );
301 [alert addButtonWithTitle:cfHelpString.AsNSString()];
302 m_buttonId[ m_buttonCount++ ] = wxID_HELP;
305 wxASSERT_MSG( m_buttonCount <= WXSIZEOF(m_buttonId), "Too many buttons" );