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"
35 #import <AppKit/NSAlert.h>
36 // ============================================================================
38 // ============================================================================
40 IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
47 const wxString& message,
48 const wxString& caption,
51 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
54 wxTopLevelWindows.Append(this);
56 wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
59 parent->AddChild(this);
62 m_cocoaNSWindow = nil;
66 void wxCocoaMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& value)
68 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, value);
73 int wxCocoaMessageDialog::ShowModal()
75 wxAutoNSAutoreleasePool thePool;
77 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
79 const long style = GetMessageDialogStyle();
81 NSAlertStyle nsStyle = NSInformationalAlertStyle;
83 switch ( GetEffectiveIcon() )
86 nsStyle = NSCriticalAlertStyle;
90 nsStyle = NSWarningAlertStyle;
94 [alert setAlertStyle:nsStyle];
99 // work out what to display
100 // if the extended text is empty then we use the caption as the title
101 // and the message as the text (for backwards compatibility)
102 // but if the extended message is not empty then we use the message as the title
103 // and the extended message as the text because that makes more sense
104 if (m_extendedMessage.empty())
106 [alert setMessageText:wxNSStringWithWxString(m_caption)];
107 [alert setInformativeText:wxNSStringWithWxString(m_message)];
111 [alert setMessageText:wxNSStringWithWxString(m_message)];
112 [alert setInformativeText:wxNSStringWithWxString(m_extendedMessage)];
115 // The wxReturn value corresponding to each button
116 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
117 if (style & wxYES_NO)
119 if ( style & wxNO_DEFAULT )
121 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
122 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
123 buttonId[0] = wxID_NO;
124 buttonId[1] = wxID_YES;
128 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
129 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
130 buttonId[0] = wxID_YES;
131 buttonId[1] = wxID_NO;
133 if (style & wxCANCEL)
135 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
136 buttonId[2] = wxID_CANCEL;
141 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
142 buttonId[0] = wxID_OK;
143 // using null as default title does not work on earlier systems
144 [alert addButtonWithTitle:wxNSStringWithWxString(GetOKLabel())];
145 if (style & wxCANCEL)
147 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
148 buttonId[1] = wxID_CANCEL;
152 int ret = [alert runModal];
155 return buttonId[ret-NSAlertFirstButtonReturn];
158 #endif // wxUSE_DIRDLG