Applied slightly-modified patch 1826540 from Gareth Simpson.
[wxWidgets.git] / src / cocoa / msgdlg.mm
1  /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/dirdlg.mm
3 // Purpose:     wxMessageDialog for wxCocoa
4 // Author:      Gareth Simpson
5 // Created:     2007-10-09
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #if wxUSE_MSGDLG
21
22
23 #include "wx/msgdlg.h"
24
25
26 #ifndef WX_PRECOMP
27     #include "wx/app.h"
28 #endif
29
30
31 #include "wx/cocoa/autorelease.h"
32 #include "wx/cocoa/string.h"
33
34 #import <AppKit/NSAlert.h>
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39 IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
40
41 // ----------------------------------------------------------------------------
42 // wxDirDialog
43 // ----------------------------------------------------------------------------
44
45 wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
46                         const wxString& message,
47                         const wxString& caption,
48                         long style,
49                                                 const wxPoint& pos) : wxMessageDialogBase(parent,message,caption,style)
50 {
51
52         //m_caption = caption;
53         //m_message = message;
54
55     //wxTopLevelWindows.Append((wxWindowBase*)this);
56         wxTopLevelWindows.Append(this);
57
58     wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
59
60     if ( parent )
61         parent->AddChild(this);
62
63
64     m_cocoaNSWindow = nil;
65     m_cocoaNSView = nil;
66         
67     m_yes = _("Yes");
68     m_no  = _("No");
69     m_ok  = _("OK");
70     m_cancel = _("Cancel");
71
72 }
73
74 wxCocoaMessageDialog::~wxCocoaMessageDialog()
75 {
76 }
77
78 int wxCocoaMessageDialog::ShowModal()
79 {
80         wxAutoNSAutoreleasePool thePool;
81
82         NSAlert *alert = [[[NSAlert alloc] init] autorelease];
83         
84     const long style = GetMessageDialogStyle();
85
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;
95                 
96         [alert setAlertStyle:nsStyle];
97         
98         
99
100         
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())
107         {
108                 [alert setMessageText:wxNSStringWithWxString(m_caption)];
109                 [alert setInformativeText:wxNSStringWithWxString(m_message)];
110         }
111         else
112         {
113                 [alert setMessageText:wxNSStringWithWxString(m_message)];
114                 [alert setInformativeText:wxNSStringWithWxString(m_extendedMessage)];   
115         }
116         
117         //      The wxReturn value corresponding to each button
118         int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
119         if (style & wxYES_NO)
120         {
121                 if ( style & wxNO_DEFAULT )
122                 {
123                         [alert addButtonWithTitle:wxNSStringWithWxString(m_no)];
124                         [alert addButtonWithTitle:wxNSStringWithWxString(m_yes)];
125                         buttonId[0] = wxID_NO;
126                         buttonId[1] = wxID_YES;
127                 }
128                 else
129                 {
130                         [alert addButtonWithTitle:wxNSStringWithWxString(m_yes)];
131                         [alert addButtonWithTitle:wxNSStringWithWxString(m_no)];
132                         buttonId[0] = wxID_YES;
133                         buttonId[1] = wxID_NO;
134                 }
135                 if (style & wxCANCEL)
136                 {
137                         [alert addButtonWithTitle:wxNSStringWithWxString(m_cancel)];
138                         buttonId[2] = wxID_CANCEL;
139                 }
140         }
141         else
142         {
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)
148                 {
149                         [alert addButtonWithTitle:wxNSStringWithWxString(m_cancel)];
150                         buttonId[1] = wxID_CANCEL;
151                 }
152         }
153         
154         int ret = [alert runModal];
155
156         
157         return buttonId[ret-NSAlertFirstButtonReturn];
158 }
159
160 bool wxCocoaMessageDialog::SetYesNoLabels(const wxString& yes,const wxString& no)
161 {
162         m_yes = yes;
163         m_yes.Replace(_("&"),_(""));
164         m_no = no;
165         m_no.Replace(_("&"),_(""));
166         return true;
167 }
168 bool wxCocoaMessageDialog::SetYesNoCancelLabels(const wxString& yes, const wxString& no, const wxString& cancel)
169 {
170         m_yes = yes;
171         m_yes.Replace(_("&"),_(""));
172         m_no = no;
173         m_no.Replace(_("&"),_(""));
174         m_cancel = cancel;
175         m_cancel.Replace(_("&"),_(""));
176         return true;
177 }
178 bool wxCocoaMessageDialog::SetOKLabel(const wxString& ok)
179 {
180         m_ok = ok;
181         m_ok.Replace(_("&"),_(""));
182         return true;
183 }
184 bool wxCocoaMessageDialog::SetOKCancelLabels(const wxString& ok, const wxString& cancel)
185 {
186         m_ok = ok;
187         m_ok.Replace(_("&"),_(""));
188         m_cancel = cancel;
189         m_cancel.Replace(_("&"),_(""));
190         return true;
191 }
192
193 #endif // wxUSE_DIRDLG
194