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)
28 wxMessageDialog::wxMessageDialog(wxWindow *parent,
29 const wxString& message,
30 const wxString& caption,
32 const wxPoint& WXUNUSED(pos))
33 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
37 int wxMessageDialog::ShowModal()
39 int resultbutton = wxID_CANCEL;
41 const long style = GetMessageDialogStyle();
43 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
45 NSAlertStyle alertType = NSWarningAlertStyle;
46 if (style & wxICON_EXCLAMATION)
47 alertType = NSCriticalAlertStyle;
48 else if (style & wxICON_HAND)
49 alertType = NSWarningAlertStyle;
50 else if (style & wxICON_INFORMATION)
51 alertType = NSInformationalAlertStyle;
52 else if (style & wxICON_QUESTION)
53 alertType = NSInformationalAlertStyle;
56 // work out what to display
57 // if the extended text is empty then we use the caption as the title
58 // and the message as the text (for backwards compatibility)
59 // but if the extended message is not empty then we use the message as the title
60 // and the extended message as the text because that makes more sense
62 wxString msgtitle,msgtext;
63 if(m_extendedMessage.IsEmpty())
71 msgtext = m_extendedMessage;
75 if ( !wxIsMainThread() )
77 CFStringRef defaultButtonTitle = NULL;
78 CFStringRef alternateButtonTitle = NULL;
79 CFStringRef otherButtonTitle = NULL;
81 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
82 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
84 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
85 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
86 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding()) ;
87 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
89 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
93 if ( style & wxNO_DEFAULT )
95 defaultButtonTitle = cfNoString;
96 alternateButtonTitle = cfYesString;
97 m_buttonId[0] = wxID_NO;
98 m_buttonId[1] = wxID_YES;
102 defaultButtonTitle = cfYesString;
103 alternateButtonTitle = cfNoString;
104 m_buttonId[0] = wxID_YES;
105 m_buttonId[1] = wxID_NO;
107 if (style & wxCANCEL)
109 otherButtonTitle = cfCancelString;
110 m_buttonId[2] = wxID_CANCEL;
115 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
116 m_buttonId[0] = wxID_OK;
117 // using null as default title does not work on earlier systems
118 defaultButtonTitle = cfOKString;
119 if (style & wxCANCEL)
121 alternateButtonTitle = cfCancelString;
122 m_buttonId[1] = wxID_CANCEL;
126 CFOptionFlags exitButton;
127 OSStatus err = CFUserNotificationDisplayAlert(
128 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
129 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
131 resultbutton = m_buttonId[exitButton];
135 NSAlert* alert = (NSAlert*)ConstructNSAlert();
138 button = [alert runModal];
140 ModalFinishedCallback(alert, button);
143 return GetReturnCode();
146 void wxMessageDialog::ShowWindowModal()
148 NSAlert* alert = (NSAlert*)ConstructNSAlert();
150 wxNonOwnedWindow* parentWindow = NULL;
152 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
155 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
157 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
161 NSWindow* nativeParent = parentWindow->GetWXWindow();
162 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
163 [sheetDelegate setImplementation: this];
164 [alert beginSheetModalForWindow: nativeParent modalDelegate: sheetDelegate
165 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
170 void wxMessageDialog::ModalFinishedCallback(void* panel, int resultCode)
172 int resultbutton = wxID_CANCEL;
173 if ( resultCode < NSAlertFirstButtonReturn )
174 resultbutton = wxID_CANCEL;
177 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
178 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
180 resultbutton = wxID_CANCEL;
182 SetReturnCode(resultbutton);
184 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
185 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
188 void* wxMessageDialog::ConstructNSAlert()
190 const long style = GetMessageDialogStyle();
192 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
194 // work out what to display
195 // if the extended text is empty then we use the caption as the title
196 // and the message as the text (for backwards compatibility)
197 // but if the extended message is not empty then we use the message as the title
198 // and the extended message as the text because that makes more sense
200 wxString msgtitle,msgtext;
201 if(m_extendedMessage.IsEmpty())
203 msgtitle = m_caption;
208 msgtitle = m_message;
209 msgtext = m_extendedMessage;
212 NSAlert* alert = [[NSAlert alloc] init];
214 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
215 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
216 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
217 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
219 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
220 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
222 [alert setMessageText:cfTitle.AsNSString()];
223 [alert setInformativeText:cfText.AsNSString()];
227 if (style & wxYES_NO)
229 if ( style & wxNO_DEFAULT )
231 [alert addButtonWithTitle:cfNoString.AsNSString()];
232 m_buttonId[ m_buttonCount++ ] = wxID_NO;
233 [alert addButtonWithTitle:cfYesString.AsNSString()];
234 m_buttonId[ m_buttonCount++ ] = wxID_YES;
238 [alert addButtonWithTitle:cfYesString.AsNSString()];
239 m_buttonId[ m_buttonCount++ ] = wxID_YES;
240 [alert addButtonWithTitle:cfNoString.AsNSString()];
241 m_buttonId[ m_buttonCount++ ] = wxID_NO;
244 if (style & wxCANCEL)
246 [alert addButtonWithTitle:cfCancelString.AsNSString()];
247 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
250 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
253 [alert addButtonWithTitle:cfOKString.AsNSString()];
254 m_buttonId[ m_buttonCount++ ] = wxID_OK;
255 if (style & wxCANCEL)
257 [alert addButtonWithTitle:cfCancelString.AsNSString()];
258 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;