1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dirdlg.mm
3 // Purpose: wxMessageDialog for wxCocoa
4 // Author: Gareth Simpson
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
23 #include "wx/msgdlg.h"
31 #include "wx/cocoa/autorelease.h"
32 #include "wx/cocoa/string.h"
34 #import <AppKit/NSAlert.h>
35 // ============================================================================
37 // ============================================================================
39 IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
46 const wxString& message,
47 const wxString& caption,
49 const wxPoint& pos) : wxMessageDialogBase(parent,message,caption,style)
52 //m_caption = caption;
53 //m_message = message;
55 //wxTopLevelWindows.Append((wxWindowBase*)this);
56 wxTopLevelWindows.Append(this);
58 wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
61 parent->AddChild(this);
64 m_cocoaNSWindow = nil;
70 m_cancel = _("Cancel");
74 wxCocoaMessageDialog::~wxCocoaMessageDialog()
78 int wxCocoaMessageDialog::ShowModal()
80 wxAutoNSAutoreleasePool thePool;
82 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
84 const long style = GetMessageDialogStyle();
86 NSAlertStyle nsStyle = NSInformationalAlertStyle;
87 if (style & wxICON_EXCLAMATION)
88 nsStyle = NSWarningAlertStyle;
89 else if (style & wxICON_HAND)
90 nsStyle = NSCriticalAlertStyle;
91 else if (style & wxICON_INFORMATION)
92 nsStyle = NSInformationalAlertStyle;
93 else if (style & wxICON_QUESTION)
94 nsStyle = NSInformationalAlertStyle;
96 [alert setAlertStyle:nsStyle];
101 // work out what to display
102 // if the extended text is empty then we use the caption as the title
103 // and the message as the text (for backwards compatibility)
104 // but if the extended message is not empty then we use the message as the title
105 // and the extended message as the text because that makes more sense
106 if (m_extendedMessage.empty())
108 [alert setMessageText:wxNSStringWithWxString(m_caption)];
109 [alert setInformativeText:wxNSStringWithWxString(m_message)];
113 [alert setMessageText:wxNSStringWithWxString(m_message)];
114 [alert setInformativeText:wxNSStringWithWxString(m_extendedMessage)];
117 // The wxReturn value corresponding to each button
118 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
119 if (style & wxYES_NO)
121 if ( style & wxNO_DEFAULT )
123 [alert addButtonWithTitle:wxNSStringWithWxString(m_no)];
124 [alert addButtonWithTitle:wxNSStringWithWxString(m_yes)];
125 buttonId[0] = wxID_NO;
126 buttonId[1] = wxID_YES;
130 [alert addButtonWithTitle:wxNSStringWithWxString(m_yes)];
131 [alert addButtonWithTitle:wxNSStringWithWxString(m_no)];
132 buttonId[0] = wxID_YES;
133 buttonId[1] = wxID_NO;
135 if (style & wxCANCEL)
137 [alert addButtonWithTitle:wxNSStringWithWxString(m_cancel)];
138 buttonId[2] = wxID_CANCEL;
143 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
144 buttonId[0] = wxID_OK;
145 // using null as default title does not work on earlier systems
146 [alert addButtonWithTitle:wxNSStringWithWxString(m_ok)];
147 if (style & wxCANCEL)
149 [alert addButtonWithTitle:wxNSStringWithWxString(m_cancel)];
150 buttonId[1] = wxID_CANCEL;
154 int ret = [alert runModal];
157 return buttonId[ret-NSAlertFirstButtonReturn];
160 bool wxCocoaMessageDialog::SetYesNoLabels(const wxString& yes,const wxString& no)
163 m_yes.Replace(_("&"),_(""));
165 m_no.Replace(_("&"),_(""));
168 bool wxCocoaMessageDialog::SetYesNoCancelLabels(const wxString& yes, const wxString& no, const wxString& cancel)
171 m_yes.Replace(_("&"),_(""));
173 m_no.Replace(_("&"),_(""));
175 m_cancel.Replace(_("&"),_(""));
178 bool wxCocoaMessageDialog::SetOKLabel(const wxString& ok)
181 m_ok.Replace(_("&"),_(""));
184 bool wxCocoaMessageDialog::SetOKCancelLabels(const wxString& ok, const wxString& cancel)
187 m_ok.Replace(_("&"),_(""));
189 m_cancel.Replace(_("&"),_(""));
193 #endif // wxUSE_DIRDLG