]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/msgdlg.mm
Minor corrections to XRC format description.
[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 #include "wx/modalhook.h"
34
35 #import <AppKit/NSAlert.h>
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 IMPLEMENT_CLASS(wxCocoaMessageDialog, wxDialog)
41
42 // ----------------------------------------------------------------------------
43 // wxDirDialog
44 // ----------------------------------------------------------------------------
45
46 wxCocoaMessageDialog::wxCocoaMessageDialog(wxWindow *parent,
47 const wxString& message,
48 const wxString& caption,
49 long style,
50 const wxPoint& pos)
51 : wxMessageDialogWithCustomLabels(parent, message, caption, style)
52 {
53
54 wxTopLevelWindows.Append(this);
55
56 wxASSERT(CreateBase(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize,style,wxDefaultValidator,wxDialogNameStr));
57
58 if ( parent )
59 parent->AddChild(this);
60
61
62 m_cocoaNSWindow = nil;
63 m_cocoaNSView = nil;
64 }
65
66 void wxCocoaMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& value)
67 {
68 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, value);
69
70 var.Replace("&", "");
71 }
72
73 int wxCocoaMessageDialog::ShowModal()
74 {
75 WX_HOOK_MODAL_DIALOG();
76
77 wxAutoNSAutoreleasePool thePool;
78
79 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
80
81 const long style = GetMessageDialogStyle();
82
83 NSAlertStyle nsStyle = NSInformationalAlertStyle;
84
85 switch ( GetEffectiveIcon() )
86 {
87 case wxICON_ERROR:
88 nsStyle = NSCriticalAlertStyle;
89 break;
90
91 case wxICON_WARNING:
92 nsStyle = NSWarningAlertStyle;
93 break;
94 }
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(GetNoLabel())];
124 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
125 buttonId[0] = wxID_NO;
126 buttonId[1] = wxID_YES;
127 }
128 else
129 {
130 [alert addButtonWithTitle:wxNSStringWithWxString(GetYesLabel())];
131 [alert addButtonWithTitle:wxNSStringWithWxString(GetNoLabel())];
132 buttonId[0] = wxID_YES;
133 buttonId[1] = wxID_NO;
134 }
135 if (style & wxCANCEL)
136 {
137 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
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(GetOKLabel())];
147 if (style & wxCANCEL)
148 {
149 [alert addButtonWithTitle:wxNSStringWithWxString(GetCancelLabel())];
150 buttonId[1] = wxID_CANCEL;
151 }
152 }
153
154 int ret = [alert runModal];
155
156
157 return buttonId[ret-NSAlertFirstButtonReturn];
158 }
159
160 #endif // wxUSE_DIRDLG
161