1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/msgdlg.mm
3 // Purpose: wxMessageDialog
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/msgdlg.h"
20 #include "wx/control.h"
21 #include "wx/thread.h"
22 #include "wx/evtloop.h"
23 #include "wx/modalhook.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 WX_HOOK_MODAL_DIALOG();
67 wxCFEventLoopPauseIdleEvents pause;
69 int resultbutton = wxID_CANCEL;
71 const long style = GetMessageDialogStyle();
73 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
75 // work out what to display
76 // if the extended text is empty then we use the caption as the title
77 // and the message as the text (for backwards compatibility)
78 // but if the extended message is not empty then we use the message as the title
79 // and the extended message as the text because that makes more sense
81 wxString msgtitle,msgtext;
82 if(m_extendedMessage.IsEmpty())
90 msgtext = m_extendedMessage;
94 if ( !wxIsMainThread() )
96 CFStringRef defaultButtonTitle = NULL;
97 CFStringRef alternateButtonTitle = NULL;
98 CFStringRef otherButtonTitle = NULL;
100 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
101 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
103 wxCFStringRef cfNoString( wxControl::GetLabelText(GetNoLabel()), GetFont().GetEncoding() );
104 wxCFStringRef cfYesString( wxControl::GetLabelText(GetYesLabel()), GetFont().GetEncoding() );
105 wxCFStringRef cfOKString( wxControl::GetLabelText(GetOKLabel()), GetFont().GetEncoding()) ;
106 wxCFStringRef cfCancelString( wxControl::GetLabelText(GetCancelLabel()), GetFont().GetEncoding() );
108 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
110 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
112 if (style & wxYES_NO)
114 if ( style & wxNO_DEFAULT )
116 defaultButtonTitle = cfNoString;
117 alternateButtonTitle = cfYesString;
118 m_buttonId[0] = wxID_NO;
119 m_buttonId[1] = wxID_YES;
123 defaultButtonTitle = cfYesString;
124 alternateButtonTitle = cfNoString;
125 m_buttonId[0] = wxID_YES;
126 m_buttonId[1] = wxID_NO;
128 if (style & wxCANCEL)
130 otherButtonTitle = cfCancelString;
131 m_buttonId[2] = wxID_CANCEL;
136 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
137 m_buttonId[0] = wxID_OK;
138 // using null as default title does not work on earlier systems
139 defaultButtonTitle = cfOKString;
140 if (style & wxCANCEL)
142 alternateButtonTitle = cfCancelString;
143 m_buttonId[1] = wxID_CANCEL;
147 wxASSERT_MSG( !(style & wxHELP), "wxHELP not supported in non-GUI thread" );
149 CFOptionFlags exitButton;
150 OSStatus err = CFUserNotificationDisplayAlert(
151 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
152 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
154 resultbutton = m_buttonId[exitButton];
155 SetReturnCode(resultbutton);
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" );