]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/msgdlg.mm
simplify code so it always returns the same object
[wxWidgets.git] / src / cocoa / msgdlg.mm
CommitLineData
e9871aaf
DE
1 /////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/dirdlg.mm
3// Purpose: wxMessageDialog for wxCocoa
4// Author: Gareth Simpson
5// Created: 2007-10-09
e5633f9a 6// RCS-ID: $Id$
e9871aaf
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"
643e9cf9 34#include "wx/testing.h"
e9871aaf
DE
35
36#import <AppKit/NSAlert.h>
37// ============================================================================
38// implementation
39// ============================================================================
40
41IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
42
43// ----------------------------------------------------------------------------
44// wxDirDialog
45// ----------------------------------------------------------------------------
46
47wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
23e00c55
VZ
48 const wxString& message,
49 const wxString& caption,
50 long style,
51 const wxPoint& pos)
52 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
e9871aaf
DE
53{
54
e5633f9a 55 wxTopLevelWindows.Append(this);
e9871aaf
DE
56
57 wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
58
59 if ( parent )
60 parent->AddChild(this);
61
62
63 m_cocoaNSWindow = nil;
64 m_cocoaNSView = nil;
e9871aaf
DE
65}
66
e08931c0 67void wxCocoaMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& value)
e9871aaf 68{
23e00c55
VZ
69 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, value);
70
71 var.Replace("&", "");
e9871aaf
DE
72}
73
74int wxCocoaMessageDialog::ShowModal()
75{
643e9cf9
VS
76 WX_TESTING_SHOW_MODAL_HOOK();
77
e5633f9a
DE
78 wxAutoNSAutoreleasePool thePool;
79
80 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
e9871aaf 81
e9871aaf
DE
82 const long style = GetMessageDialogStyle();
83
84 NSAlertStyle nsStyle = NSInformationalAlertStyle;
a4578b0c
VZ
85
86 switch ( GetEffectiveIcon() )
87 {
88 case wxICON_ERROR:
89 nsStyle = NSCriticalAlertStyle;
90 break;
91
92 case wxICON_WARNING:
93 nsStyle = NSWarningAlertStyle;
94 break;
95 }
e9871aaf 96
e5633f9a
DE
97 [alert setAlertStyle:nsStyle];
98
99
100
101
e9871aaf
DE
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
e5633f9a
DE
107 if (m_extendedMessage.empty())
108 {
109 [alert setMessageText:wxNSStringWithWxString(m_caption)];
110 [alert setInformativeText:wxNSStringWithWxString(m_message)];
111 }
112 else
113 {
114 [alert setMessageText:wxNSStringWithWxString(m_message)];
115 [alert setInformativeText:wxNSStringWithWxString(m_extendedMessage)];
116 }
117
118 // The wxReturn value corresponding to each button
119 int buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
120 if (style & wxYES_NO)
121 {
122 if ( style & wxNO_DEFAULT )
123 {
23e00c55
VZ
124 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
125 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
e5633f9a
DE
126 buttonId[0] = wxID_NO;
127 buttonId[1] = wxID_YES;
128 }
129 else
130 {
23e00c55
VZ
131 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
132 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
e5633f9a
DE
133 buttonId[0] = wxID_YES;
134 buttonId[1] = wxID_NO;
135 }
136 if (style & wxCANCEL)
137 {
23e00c55 138 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
e5633f9a
DE
139 buttonId[2] = wxID_CANCEL;
140 }
141 }
142 else
143 {
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
23e00c55 147 [alert addButtonWithTitle:wxNSStringWithWxString(GetOKLabel())];
e5633f9a
DE
148 if (style & wxCANCEL)
149 {
23e00c55 150 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
e5633f9a
DE
151 buttonId[1] = wxID_CANCEL;
152 }
153 }
154
155 int ret = [alert runModal];
156
157
158 return buttonId[ret-NSAlertFirstButtonReturn];
e9871aaf
DE
159}
160
e9871aaf 161#endif // wxUSE_DIRDLG
dcb68102 162