1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dirdlg.mm
3 // Purpose: wxMessageDialog for wxCocoa
4 // Author: Gareth Simpson
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
24 #include "wx/msgdlg.h"
32 #include "wx/cocoa/autorelease.h"
33 #include "wx/cocoa/string.h"
34 #include "wx/testing.h"
36 #import <AppKit/NSAlert.h>
37 // ============================================================================
39 // ============================================================================
41 IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
48 const wxString& message,
49 const wxString& caption,
52 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
55 wxTopLevelWindows.Append(this);
57 wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
60 parent->AddChild(this);
63 m_cocoaNSWindow = nil;
67 void wxCocoaMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& value)
69 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, value);
74 int wxCocoaMessageDialog::ShowModal()
76 WX_TESTING_SHOW_MODAL_HOOK();
78 wxAutoNSAutoreleasePool thePool;
80 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
82 const long style = GetMessageDialogStyle();
84 NSAlertStyle nsStyle = NSInformationalAlertStyle;
86 switch ( GetEffectiveIcon() )
89 nsStyle = NSCriticalAlertStyle;
93 nsStyle = NSWarningAlertStyle;
97 [alert setAlertStyle:nsStyle];
102 // work out what to display
103 // if the extended text is empty then we use the caption as the title
104 // and the message as the text (for backwards compatibility)
105 // but if the extended message is not empty then we use the message as the title
106 // and the extended message as the text because that makes more sense
107 if (m_extendedMessage.empty())
109 [alert setMessageText:wxNSStringWithWxString(m_caption)];
110 [alert setInformativeText:wxNSStringWithWxString(m_message)];
114 [alert setMessageText:wxNSStringWithWxString(m_message)];
115 [alert setInformativeText:wxNSStringWithWxString(m_extendedMessage)];
118 // The wxReturn value corresponding to each button
119 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
120 if (style & wxYES_NO)
122 if ( style & wxNO_DEFAULT )
124 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
125 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
126 buttonId[0] = wxID_NO;
127 buttonId[1] = wxID_YES;
131 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
132 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
133 buttonId[0] = wxID_YES;
134 buttonId[1] = wxID_NO;
136 if (style & wxCANCEL)
138 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
139 buttonId[2] = wxID_CANCEL;
144 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
145 buttonId[0] = wxID_OK;
146 // using null as default title does not work on earlier systems
147 [alert addButtonWithTitle:wxNSStringWithWxString(GetOKLabel())];
148 if (style & wxCANCEL)
150 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
151 buttonId[1] = wxID_CANCEL;
155 int ret = [alert runModal];
158 return buttonId[ret-NSAlertFirstButtonReturn];
161 #endif // wxUSE_DIRDLG