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