1 /////////////////////////////////////////////////////////////////////////////
2// Name:        src/cocoa/dirdlg.mm
3// Purpose:     wxMessageDialog for wxCocoa
4// Author:      Gareth Simpson
5// Created:     2007-10-09
6// RCS-ID:      $Id: msgdlg.mm 51798 2008-02-14 21:31:18Z DE $
7// Licence:     wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#if wxUSE_MSGDLG
22
23
24#include "wx/msgdlg.h"
25
26
27#ifndef WX_PRECOMP
28    #include "wx/app.h"
29#endif
30
31
32#include "wx/cocoa/autorelease.h"
33#include "wx/cocoa/string.h"
34
35#import <AppKit/NSAlert.h>
36// ============================================================================
37// implementation
38// ============================================================================
39
40IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
41
42// ----------------------------------------------------------------------------
43// wxDirDialog
44// ----------------------------------------------------------------------------
45
46wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
47                        const wxString& message,
48                        const wxString& caption,
49                        long style,
50                        const wxPoint& pos) : wxDialog(parent,wxID_ANY,caption, pos, wxDefaultSize, style)
51{
52
53    m_caption = caption;
54    m_message = message;
55
56    //wxTopLevelWindows.Append((wxWindowBase*)this);
57    wxTopLevelWindows.Append(this);
58
59    wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
60
61    if ( parent )
62        parent->AddChild(this);
63
64
65    m_cocoaNSWindow = nil;
66    m_cocoaNSView = nil;
67
68    m_yes = _("Yes");
69    m_no  = _("No");
70    m_ok  = _("OK");
71    m_cancel = _("Cancel");
72
73    SetMessageDialogStyle(style);
74}
75
76wxCocoaMessageDialog::~wxCocoaMessageDialog()
77{
78}
79
80int wxCocoaMessageDialog::ShowModal()
81{
82    wxAutoNSAutoreleasePool thePool;
83
84    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
85
86    const long style = GetMessageDialogStyle();
87
88    NSAlertStyle nsStyle = NSInformationalAlertStyle;
89    if (style & wxICON_EXCLAMATION)
90        nsStyle = NSWarningAlertStyle;
91    else if (style & wxICON_HAND)
92        nsStyle = NSCriticalAlertStyle;
93    else if (style & wxICON_INFORMATION)
94        nsStyle = NSInformationalAlertStyle;
95    else if (style & wxICON_QUESTION)
96        nsStyle = NSInformationalAlertStyle;
97
98    [alert setAlertStyle:nsStyle];
99
100
101
102
103    // work out what to display
104    // if the extended text is empty then we use the caption as the title
105    // and the message as the text (for backwards compatibility)
106    // but if the extended message is not empty then we use the message as the title
107    // and the extended message as the text because that makes more sense
108    if (m_extendedMessage.empty())
109    {
110        [alert setMessageText:wxNSStringWithWxString(m_caption)];
111        [alert setInformativeText:wxNSStringWithWxString(m_message)];
112    }
113    else
114    {
115        [alert setMessageText:wxNSStringWithWxString(m_message)];
116        [alert setInformativeText:wxNSStringWithWxString(m_extendedMessage)];
117    }
118
119    //    The wxReturn value corresponding to each button
120    int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
121    if (style & wxYES_NO)
122    {
123        if ( style & wxNO_DEFAULT )
124        {
125            [alert addButtonWithTitle:wxNSStringWithWxString(m_no)];
126            [alert addButtonWithTitle:wxNSStringWithWxString(m_yes)];
127            buttonId[0] = wxID_NO;
128            buttonId[1] = wxID_YES;
129        }
130        else
131        {
132            [alert addButtonWithTitle:wxNSStringWithWxString(m_yes)];
133            [alert addButtonWithTitle:wxNSStringWithWxString(m_no)];
134            buttonId[0] = wxID_YES;
135            buttonId[1] = wxID_NO;
136        }
137        if (style & wxCANCEL)
138        {
139            [alert addButtonWithTitle:wxNSStringWithWxString(m_cancel)];
140            buttonId[2] = wxID_CANCEL;
141        }
142    }
143    else
144    {
145        // the MSW implementation even shows an OK button if it is not specified, we'll do the same
146        buttonId[0] = wxID_OK;
147        // using null as default title does not work on earlier systems
148        [alert addButtonWithTitle:wxNSStringWithWxString(m_ok)];
149        if (style & wxCANCEL)
150        {
151            [alert addButtonWithTitle:wxNSStringWithWxString(m_cancel)];
152            buttonId[1] = wxID_CANCEL;
153        }
154    }
155
156    int ret = [alert runModal];
157
158
159    return buttonId[ret-NSAlertFirstButtonReturn];
160}
161
162bool wxCocoaMessageDialog::SetYesNoLabels(const wxString& yes,const wxString& no)
163{
164    m_yes = yes;
165    m_yes.Replace(_("&"),_(""));
166    m_no = no;
167    m_no.Replace(_("&"),_(""));
168    return true;
169}
170bool wxCocoaMessageDialog::SetYesNoCancelLabels(const wxString& yes, const wxString& no, const wxString& cancel)
171{
172    m_yes = yes;
173    m_yes.Replace(_("&"),_(""));
174    m_no = no;
175    m_no.Replace(_("&"),_(""));
176    m_cancel = cancel;
177    m_cancel.Replace(_("&"),_(""));
178    return true;
179}
180bool wxCocoaMessageDialog::SetOKLabel(const wxString& ok)
181{
182    m_ok = ok;
183    m_ok.Replace(_("&"),_(""));
184    return true;
185}
186bool wxCocoaMessageDialog::SetOKCancelLabels(const wxString& ok, const wxString& cancel)
187{
188    m_ok = ok;
189    m_ok.Replace(_("&"),_(""));
190    m_cancel = cancel;
191    m_cancel.Replace(_("&"),_(""));
192    return true;
193}
194
195#endif // wxUSE_DIRDLG
196
197