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 : wxMessageDialogBase(parent, message, caption, style)
38 m_cancel = _("Cancel");
41 bool wxMessageDialog::SetYesNoLabels(const wxString& yes,const wxString& no)
48 bool wxMessageDialog::SetYesNoCancelLabels(const wxString& yes, const wxString& no, const wxString& cancel)
56 bool wxMessageDialog::SetOKLabel(const wxString& ok)
62 bool wxMessageDialog::SetOKCancelLabels(const wxString& ok, const wxString& cancel)
69 int wxMessageDialog::ShowModal()
71 int resultbutton = wxID_CANCEL;
73 const long style = GetMessageDialogStyle();
75 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
77 NSAlertStyle alertType = NSWarningAlertStyle;
78 if (style & wxICON_EXCLAMATION)
79 alertType = NSCriticalAlertStyle;
80 else if (style & wxICON_HAND)
81 alertType = NSWarningAlertStyle;
82 else if (style & wxICON_INFORMATION)
83 alertType = NSInformationalAlertStyle;
84 else if (style & wxICON_QUESTION)
85 alertType = NSInformationalAlertStyle;
88 // work out what to display
89 // if the extended text is empty then we use the caption as the title
90 // and the message as the text (for backwards compatibility)
91 // but if the extended message is not empty then we use the message as the title
92 // and the extended message as the text because that makes more sense
94 wxString msgtitle,msgtext;
95 if(m_extendedMessage.IsEmpty())
102 msgtitle = m_message;
103 msgtext = m_extendedMessage;
107 if ( !wxIsMainThread() )
109 CFStringRef defaultButtonTitle = NULL;
110 CFStringRef alternateButtonTitle = NULL;
111 CFStringRef otherButtonTitle = NULL;
113 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
114 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
116 wxCFStringRef cfNoString( m_no.c_str(), GetFont().GetEncoding() );
117 wxCFStringRef cfYesString( m_yes.c_str(), GetFont().GetEncoding() );
118 wxCFStringRef cfOKString( m_ok.c_str() , GetFont().GetEncoding()) ;
119 wxCFStringRef cfCancelString( m_cancel.c_str(), GetFont().GetEncoding() );
121 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
123 if (style & wxYES_NO)
125 if ( style & wxNO_DEFAULT )
127 defaultButtonTitle = cfNoString;
128 alternateButtonTitle = cfYesString;
129 buttonId[0] = wxID_NO;
130 buttonId[1] = wxID_YES;
134 defaultButtonTitle = cfYesString;
135 alternateButtonTitle = cfNoString;
136 buttonId[0] = wxID_YES;
137 buttonId[1] = wxID_NO;
139 if (style & wxCANCEL)
141 otherButtonTitle = cfCancelString;
142 buttonId[2] = wxID_CANCEL;
147 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
148 buttonId[0] = wxID_OK;
149 // using null as default title does not work on earlier systems
150 defaultButtonTitle = cfOKString;
151 if (style & wxCANCEL)
153 alternateButtonTitle = cfCancelString;
154 buttonId[1] = wxID_CANCEL;
158 CFOptionFlags exitButton;
159 OSStatus err = CFUserNotificationDisplayAlert(
160 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
161 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
163 resultbutton = buttonId[exitButton];
167 NSAlert* alert = [[NSAlert alloc] init];
169 wxCFStringRef cfNoString( m_no.c_str(), GetFont().GetEncoding() );
170 wxCFStringRef cfYesString( m_yes.c_str(), GetFont().GetEncoding() );
171 wxCFStringRef cfOKString( m_ok.c_str(), GetFont().GetEncoding() );
172 wxCFStringRef cfCancelString( m_cancel.c_str(), GetFont().GetEncoding() );
174 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
175 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
177 [alert setMessageText:cfTitle.AsNSString()];
178 [alert setInformativeText:cfText.AsNSString()];
180 int buttonId[3] = { 0, 0, 0 };
183 if (style & wxYES_NO)
185 if ( style & wxNO_DEFAULT )
187 [alert addButtonWithTitle:cfNoString.AsNSString()];
188 buttonId[ buttonCount++ ] = wxID_NO;
189 [alert addButtonWithTitle:cfYesString.AsNSString()];
190 buttonId[ buttonCount++ ] = wxID_YES;
194 [alert addButtonWithTitle:cfYesString.AsNSString()];
195 buttonId[ buttonCount++ ] = wxID_YES;
196 [alert addButtonWithTitle:cfNoString.AsNSString()];
197 buttonId[ buttonCount++ ] = wxID_NO;
200 if (style & wxCANCEL)
202 [alert addButtonWithTitle:cfCancelString.AsNSString()];
203 buttonId[ buttonCount++ ] = wxID_CANCEL;
206 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
209 [alert addButtonWithTitle:cfOKString.AsNSString()];
210 buttonId[ buttonCount++ ] = wxID_OK;
211 if (style & wxCANCEL)
213 [alert addButtonWithTitle:cfCancelString.AsNSString()];
214 buttonId[ buttonCount++ ] = wxID_CANCEL;
218 int button = [alert runModal];
222 if ( button < NSAlertFirstButtonReturn )
223 resultbutton = wxID_CANCEL;
226 if ( button - NSAlertFirstButtonReturn < buttonCount )
227 resultbutton = buttonId[ button - NSAlertFirstButtonReturn ];
229 resultbutton = wxID_CANCEL;