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 buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
93 if ( style & wxNO_DEFAULT )
95 defaultButtonTitle = cfNoString;
96 alternateButtonTitle = cfYesString;
97 buttonId[0] = wxID_NO;
98 buttonId[1] = wxID_YES;
102 defaultButtonTitle = cfYesString;
103 alternateButtonTitle = cfNoString;
104 buttonId[0] = wxID_YES;
105 buttonId[1] = wxID_NO;
107 if (style & wxCANCEL)
109 otherButtonTitle = cfCancelString;
110 buttonId[2] = wxID_CANCEL;
115 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
116 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 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 = buttonId[exitButton];
135 NSAlert* alert = [[NSAlert alloc] init];
137 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
138 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
139 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
140 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
142 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
143 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
145 [alert setMessageText:cfTitle.AsNSString()];
146 [alert setInformativeText:cfText.AsNSString()];
148 int buttonId[3] = { 0, 0, 0 };
151 if (style & wxYES_NO)
153 if ( style & wxNO_DEFAULT )
155 [alert addButtonWithTitle:cfNoString.AsNSString()];
156 buttonId[ buttonCount++ ] = wxID_NO;
157 [alert addButtonWithTitle:cfYesString.AsNSString()];
158 buttonId[ buttonCount++ ] = wxID_YES;
162 [alert addButtonWithTitle:cfYesString.AsNSString()];
163 buttonId[ buttonCount++ ] = wxID_YES;
164 [alert addButtonWithTitle:cfNoString.AsNSString()];
165 buttonId[ buttonCount++ ] = wxID_NO;
168 if (style & wxCANCEL)
170 [alert addButtonWithTitle:cfCancelString.AsNSString()];
171 buttonId[ buttonCount++ ] = wxID_CANCEL;
174 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
177 [alert addButtonWithTitle:cfOKString.AsNSString()];
178 buttonId[ buttonCount++ ] = wxID_OK;
179 if (style & wxCANCEL)
181 [alert addButtonWithTitle:cfCancelString.AsNSString()];
182 buttonId[ buttonCount++ ] = wxID_CANCEL;
187 wxNonOwnedWindow* parentWindow = NULL;
192 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
197 NSWindow* nativeParent = parentWindow->GetWXWindow();
198 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
199 [alert beginSheetModalForWindow: nativeParent modalDelegate: sheetDelegate
200 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
202 [sheetDelegate waitForSheetToFinish];
203 button = [sheetDelegate code];
204 [sheetDelegate release];
208 button = [alert runModal];
212 if ( button < NSAlertFirstButtonReturn )
213 resultbutton = wxID_CANCEL;
216 if ( button - NSAlertFirstButtonReturn < buttonCount )
217 resultbutton = buttonId[ button - NSAlertFirstButtonReturn ];
219 resultbutton = wxID_CANCEL;