]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/msgdlg.mm
Show the first, not the last, inserted item in wxListBox in wxOSX.
[wxWidgets.git] / src / osx / cocoa / msgdlg.mm
CommitLineData
524c47aa
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/msgdlg.mm
3// Purpose: wxMessageDialog
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id: msgdlg.cpp 54129 2008-06-11 19:30:52Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/msgdlg.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/app.h"
19#endif
20
21#include "wx/thread.h"
22#include "wx/osx/private.h"
23
24
25IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
26
27
0aeac464
SC
28namespace
29{
30 NSAlertStyle GetAlertStyleFromWXStyle( long style )
31 {
32 NSAlertStyle alertType = NSWarningAlertStyle;
33 if (style & wxICON_EXCLAMATION)
34 alertType = NSCriticalAlertStyle;
35 else if (style & wxICON_HAND)
36 alertType = NSWarningAlertStyle;
37 else if (style & wxICON_INFORMATION)
38 alertType = NSInformationalAlertStyle;
39 else if (style & wxICON_QUESTION)
40 alertType = NSInformationalAlertStyle;
41 return alertType;
42 }
43}
44
524c47aa
SC
45wxMessageDialog::wxMessageDialog(wxWindow *parent,
46 const wxString& message,
47 const wxString& caption,
48 long style,
49 const wxPoint& WXUNUSED(pos))
ede7b017 50 : wxMessageDialogBase(parent, message, caption, style)
524c47aa 51{
524c47aa
SC
52}
53
54int wxMessageDialog::ShowModal()
55{
56 int resultbutton = wxID_CANCEL;
57
58 const long style = GetMessageDialogStyle();
59
60 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
61
524c47aa
SC
62 // work out what to display
63 // if the extended text is empty then we use the caption as the title
64 // and the message as the text (for backwards compatibility)
65 // but if the extended message is not empty then we use the message as the title
66 // and the extended message as the text because that makes more sense
67
68 wxString msgtitle,msgtext;
69 if(m_extendedMessage.IsEmpty())
70 {
71 msgtitle = m_caption;
72 msgtext = m_message;
73 }
74 else
75 {
76 msgtitle = m_message;
77 msgtext = m_extendedMessage;
78 }
79
80
81 if ( !wxIsMainThread() )
82 {
83 CFStringRef defaultButtonTitle = NULL;
84 CFStringRef alternateButtonTitle = NULL;
85 CFStringRef otherButtonTitle = NULL;
86
87 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
88 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
89
8e06b283
SC
90 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
91 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
92 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding()) ;
93 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
524c47aa 94
0aeac464
SC
95 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
96
bfa92264 97 int m_buttonId[4] = { 0, 0, 0, wxID_CANCEL /* time-out */ };
524c47aa
SC
98
99 if (style & wxYES_NO)
100 {
101 if ( style & wxNO_DEFAULT )
102 {
103 defaultButtonTitle = cfNoString;
104 alternateButtonTitle = cfYesString;
bfa92264
KO
105 m_buttonId[0] = wxID_NO;
106 m_buttonId[1] = wxID_YES;
524c47aa
SC
107 }
108 else
109 {
110 defaultButtonTitle = cfYesString;
111 alternateButtonTitle = cfNoString;
bfa92264
KO
112 m_buttonId[0] = wxID_YES;
113 m_buttonId[1] = wxID_NO;
524c47aa
SC
114 }
115 if (style & wxCANCEL)
116 {
117 otherButtonTitle = cfCancelString;
bfa92264 118 m_buttonId[2] = wxID_CANCEL;
524c47aa
SC
119 }
120 }
121 else
122 {
123 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
bfa92264 124 m_buttonId[0] = wxID_OK;
524c47aa
SC
125 // using null as default title does not work on earlier systems
126 defaultButtonTitle = cfOKString;
127 if (style & wxCANCEL)
128 {
129 alternateButtonTitle = cfCancelString;
bfa92264 130 m_buttonId[1] = wxID_CANCEL;
524c47aa
SC
131 }
132 }
133
134 CFOptionFlags exitButton;
135 OSStatus err = CFUserNotificationDisplayAlert(
136 0, alertType, NULL, NULL, NULL, cfTitle, cfText,
137 defaultButtonTitle, alternateButtonTitle, otherButtonTitle, &exitButton );
138 if (err == noErr)
bfa92264 139 resultbutton = m_buttonId[exitButton];
524c47aa
SC
140 }
141 else
142 {
bfa92264 143 NSAlert* alert = (NSAlert*)ConstructNSAlert();
03647350 144
bfa92264
KO
145 int button = -1;
146 button = [alert runModal];
147 [alert release];
148 ModalFinishedCallback(alert, button);
149 }
524c47aa 150
bfa92264
KO
151 return GetReturnCode();
152}
524c47aa 153
bfa92264
KO
154void wxMessageDialog::ShowWindowModal()
155{
156 NSAlert* alert = (NSAlert*)ConstructNSAlert();
03647350 157
bfa92264 158 wxNonOwnedWindow* parentWindow = NULL;
524c47aa 159
bfa92264 160 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
1e181c7a 161
bfa92264
KO
162 if (GetParent())
163 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
164
165 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
166
167 if (parentWindow)
168 {
169 NSWindow* nativeParent = parentWindow->GetWXWindow();
170 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
171 [sheetDelegate setImplementation: this];
172 [alert beginSheetModalForWindow: nativeParent modalDelegate: sheetDelegate
173 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
174 contextInfo: nil];
175 }
176}
177
178void wxMessageDialog::ModalFinishedCallback(void* panel, int resultCode)
179{
180 int resultbutton = wxID_CANCEL;
181 if ( resultCode < NSAlertFirstButtonReturn )
182 resultbutton = wxID_CANCEL;
183 else
184 {
185 if ( resultCode - NSAlertFirstButtonReturn < m_buttonCount )
186 resultbutton = m_buttonId[ resultCode - NSAlertFirstButtonReturn ];
524c47aa 187 else
bfa92264
KO
188 resultbutton = wxID_CANCEL;
189 }
190 SetReturnCode(resultbutton);
191
192 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
193 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
194}
524c47aa 195
bfa92264
KO
196void* wxMessageDialog::ConstructNSAlert()
197{
198 const long style = GetMessageDialogStyle();
ba41a8c6 199
bfa92264 200 wxASSERT_MSG( (style & 0x3F) != wxYES, wxT("this style is not supported on Mac") );
03647350 201
bfa92264
KO
202 // work out what to display
203 // if the extended text is empty then we use the caption as the title
204 // and the message as the text (for backwards compatibility)
205 // but if the extended message is not empty then we use the message as the title
206 // and the extended message as the text because that makes more sense
03647350 207
bfa92264
KO
208 wxString msgtitle,msgtext;
209 if(m_extendedMessage.IsEmpty())
210 {
211 msgtitle = m_caption;
212 msgtext = m_message;
213 }
214 else
215 {
216 msgtitle = m_message;
217 msgtext = m_extendedMessage;
218 }
219
220 NSAlert* alert = [[NSAlert alloc] init];
0aeac464 221 NSAlertStyle alertType = GetAlertStyleFromWXStyle(style);
bfa92264
KO
222
223 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
224 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
225 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
226 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
227
228 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
229 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
230
231 [alert setMessageText:cfTitle.AsNSString()];
232 [alert setInformativeText:cfText.AsNSString()];
0aeac464 233 [alert setAlertStyle:alertType];
bfa92264
KO
234
235 m_buttonCount = 0;
236
237 if (style & wxYES_NO)
238 {
239 if ( style & wxNO_DEFAULT )
ba41a8c6 240 {
bfa92264
KO
241 [alert addButtonWithTitle:cfNoString.AsNSString()];
242 m_buttonId[ m_buttonCount++ ] = wxID_NO;
243 [alert addButtonWithTitle:cfYesString.AsNSString()];
244 m_buttonId[ m_buttonCount++ ] = wxID_YES;
ba41a8c6
KO
245 }
246 else
247 {
bfa92264
KO
248 [alert addButtonWithTitle:cfYesString.AsNSString()];
249 m_buttonId[ m_buttonCount++ ] = wxID_YES;
250 [alert addButtonWithTitle:cfNoString.AsNSString()];
251 m_buttonId[ m_buttonCount++ ] = wxID_NO;
ba41a8c6 252 }
03647350 253
bfa92264 254 if (style & wxCANCEL)
524c47aa 255 {
bfa92264
KO
256 [alert addButtonWithTitle:cfCancelString.AsNSString()];
257 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
524c47aa
SC
258 }
259 }
bfa92264
KO
260 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
261 else
262 {
263 [alert addButtonWithTitle:cfOKString.AsNSString()];
264 m_buttonId[ m_buttonCount++ ] = wxID_OK;
265 if (style & wxCANCEL)
266 {
267 [alert addButtonWithTitle:cfCancelString.AsNSString()];
268 m_buttonId[ m_buttonCount++ ] = wxID_CANCEL;
269 }
270 }
271 return alert;
524c47aa 272}