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/osx/private.h"
27 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
32 NSAlertStyle GetAlertStyleFromWXStyle( long style )
34 NSAlertStyle alertType = NSWarningAlertStyle;
35 if (style & wxICON_EXCLAMATION)
36 alertType = NSCriticalAlertStyle;
37 else if (style & wxICON_HAND)
38 alertType = NSWarningAlertStyle;
39 else if (style & wxICON_INFORMATION)
40 alertType = NSInformationalAlertStyle;
41 else if (style & wxICON_QUESTION)
42 alertType = NSInformationalAlertStyle;
47 wxMessageDialog::wxMessageDialog(wxWindow *parent,
48 const wxString& message,
49 const wxString& caption,
51 const wxPoint& WXUNUSED(pos))
52 : wxMessageDialogBase(parent, message, caption, style)
54 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
55 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
58 wxMessageDialog::~wxMessageDialog()
60 [m_sheetDelegate release];
63 int wxMessageDialog::ShowModal()
65 wxCFEventLoopPauseIdleEvents pause;
67 int resultbutton = wxID_CANCEL;
69 const long style = GetMessageDialogStyle();
71 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
73 // work out what to display
74 // if the extended text is empty then we use the caption as the title
75 // and the message as the text (for backwards compatibility)
76 // but if the extended message is not empty then we use the message as the title
77 // and the extended message as the text because that makes more sense
79 wxString msgtitle,msgtext;
80 if(m_extendedMessage.IsEmpty())
88 msgtext = m_extendedMessage;
92 if ( !wxIsMainThread() )
94 CFStringRef defaultButtonTitle = NULL;
95 CFStringRef alternateButtonTitle = NULL;
96 CFStringRef otherButtonTitle = NULL;
98 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
99 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
101 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
102 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
103 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding()) ;
104 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
106 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
108 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
110 if (style & wxYES_NO)
112 if ( style & wxNO_DEFAULT )
114 defaultButtonTitle = cfNoString;
115 alternateButtonTitle = cfYesString;
116 m_buttonId[0] = wxID_NO;
117 m_buttonId[1] = wxID_YES;
121 defaultButtonTitle = cfYesString;
122 alternateButtonTitle = cfNoString;
123 m_buttonId[0] = wxID_YES;
124 m_buttonId[1] = wxID_NO;
126 if (style & wxCANCEL)
128 otherButtonTitle = cfCancelString;
129 m_buttonId[2] = wxID_CANCEL;
134 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
135 m_buttonId[0] = wxID_OK;
136 // using null as default title does not work on earlier systems
137 defaultButtonTitle = cfOKString;
138 if (style & wxCANCEL)
140 alternateButtonTitle = cfCancelString;
141 m_buttonId[1] = wxID_CANCEL;
145 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
147 CFOptionFlags exitButton;
148 OSStatus err = CFUserNotificationDisplayAlert(
149 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
150 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
152 resultbutton = m_buttonId[exitButton];
156 NSAlert* alert = (NSAlert*)ConstructNSAlert();
159 button = [alert runModal];
161 ModalFinishedCallback(alert, button);
164 return GetReturnCode();
167 void wxMessageDialog::ShowWindowModal()
169 NSAlert* alert = (NSAlert*)ConstructNSAlert();
171 wxNonOwnedWindow* parentWindow = NULL;
173 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
176 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
178 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
182 NSWindow* nativeParent = parentWindow->GetWXWindow();
183 [alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate
184 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
189 void wxMessageDialog::ModalFinishedCallback(void* WXUNUSED(panel), int resultCode)
191 int resultbutton = wxID_CANCEL;
192 if ( resultCode < NSAlertFirstButtonReturn )
193 resultbutton = wxID_CANCEL;
196 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
197 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
199 resultbutton = wxID_CANCEL;
201 SetReturnCode(resultbutton);
203 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
204 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
207 void* wxMessageDialog::ConstructNSAlert()
209 const long style = GetMessageDialogStyle();
211 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
213 // work out what to display
214 // if the extended text is empty then we use the caption as the title
215 // and the message as the text (for backwards compatibility)
216 // but if the extended message is not empty then we use the message as the title
217 // and the extended message as the text because that makes more sense
219 wxString msgtitle,msgtext;
220 if(m_extendedMessage.IsEmpty())
222 msgtitle = m_caption;
227 msgtitle = m_message;
228 msgtext = m_extendedMessage;
231 NSAlert* alert = [[NSAlert alloc] init];
232 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
234 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
235 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
236 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding() );
237 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
239 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
240 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
242 [alert setMessageText:cfTitle.AsNSString()];
243 [alert setInformativeText:cfText.AsNSString()];
244 [alert setAlertStyle:alertType];
248 if (style & wxYES_NO)
250 if ( style & wxNO_DEFAULT )
252 [alert addButtonWithTitle:cfNoString.AsNSString()];
253 m_buttonId[ m_buttonCount++ ] = wxID_NO;
254 [alert addButtonWithTitle:cfYesString.AsNSString()];
255 m_buttonId[ m_buttonCount++ ] = wxID_YES;
259 [alert addButtonWithTitle:cfYesString.AsNSString()];
260 m_buttonId[ m_buttonCount++ ] = wxID_YES;
261 [alert addButtonWithTitle:cfNoString.AsNSString()];
262 m_buttonId[ m_buttonCount++ ] = wxID_NO;
265 if (style & wxCANCEL)
267 [alert addButtonWithTitle:cfCancelString.AsNSString()];
268 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
271 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
274 if ( style & wxCANCEL_DEFAULT )
276 [alert addButtonWithTitle:cfCancelString.AsNSString()];
277 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
279 [alert addButtonWithTitle:cfOKString.AsNSString()];
280 m_buttonId[ m_buttonCount++ ] = wxID_OK;
284 [alert addButtonWithTitle:cfOKString.AsNSString()];
285 m_buttonId[ m_buttonCount++ ] = wxID_OK;
286 if (style & wxCANCEL)
288 [alert addButtonWithTitle:cfCancelString.AsNSString()];
289 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
295 if ( style & wxHELP )
297 wxCFStringRef cfHelpString( GetHelpLabel(), GetFont().GetEncoding() );
298 [alert addButtonWithTitle:cfHelpString.AsNSString()];
299 m_buttonId[ m_buttonCount++ ] = wxID_HELP;
302 wxASSERT_MSG( m_buttonCount <= WXSIZEOF(m_buttonId), "Too many buttons" );