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"
33 #include "wx/modalhook.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 WX_HOOK_MODAL_DIALOG();
77 wxAutoNSAutoreleasePool thePool;
79 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
81 const long style = GetMessageDialogStyle();
83 NSAlertStyle nsStyle = NSInformationalAlertStyle;
85 switch ( GetEffectiveIcon() )
88 nsStyle = NSCriticalAlertStyle;
92 nsStyle = NSWarningAlertStyle;
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(GetNoLabel())];
124 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
125 buttonId[0] = wxID_NO;
126 buttonId[1] = wxID_YES;
130 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
131 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
132 buttonId[0] = wxID_YES;
133 buttonId[1] = wxID_NO;
135 if (style & wxCANCEL)
137 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
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(GetOKLabel())];
147 if (style & wxCANCEL)
149 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
150 buttonId[1] = wxID_CANCEL;
154 int ret = [alert runModal];
157 return buttonId[ret-NSAlertFirstButtonReturn];
160 #endif // wxUSE_DIRDLG