]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/msgdlg.mm
Sheet support for wxMessageDialog.
[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 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 buttonId[0] = wxID_NO;
98 buttonId[1] = wxID_YES;
99 }
100 else
101 {
102 defaultButtonTitle = cfYesString;
103 alternateButtonTitle = cfNoString;
104 buttonId[0] = wxID_YES;
105 buttonId[1] = wxID_NO;
106 }
107 if (style & wxCANCEL)
108 {
109 otherButtonTitle = cfCancelString;
110 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 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 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 = buttonId[exitButton];
132 }
133 else
134 {
135 NSAlert* alert = [[NSAlert alloc] init];
136
137 wxCFStringRef cfNoString( GetNoLabel(), GetFont().GetEncoding() );
138 wxCFStringRef cfYesString( GetYesLabel(), GetFont().GetEncoding() );
139 wxCFStringRef cfOKString( GetOKLabel(), GetFont().GetEncoding() );
140 wxCFStringRef cfCancelString( GetCancelLabel(), GetFont().GetEncoding() );
141
142 wxCFStringRef cfTitle( msgtitle, GetFont().GetEncoding() );
143 wxCFStringRef cfText( msgtext, GetFont().GetEncoding() );
144
145 [alert setMessageText:cfTitle.AsNSString()];
146 [alert setInformativeText:cfText.AsNSString()];
147
148 int buttonId[3] = { 0, 0, 0 };
149 int buttonCount = 0;
150
151 if (style & wxYES_NO)
152 {
153 if ( style & wxNO_DEFAULT )
154 {
155 [alert addButtonWithTitle:cfNoString.AsNSString()];
156 buttonId[ buttonCount++ ] = wxID_NO;
157 [alert addButtonWithTitle:cfYesString.AsNSString()];
158 buttonId[ buttonCount++ ] = wxID_YES;
159 }
160 else
161 {
162 [alert addButtonWithTitle:cfYesString.AsNSString()];
163 buttonId[ buttonCount++ ] = wxID_YES;
164 [alert addButtonWithTitle:cfNoString.AsNSString()];
165 buttonId[ buttonCount++ ] = wxID_NO;
166 }
167
168 if (style & wxCANCEL)
169 {
170 [alert addButtonWithTitle:cfCancelString.AsNSString()];
171 buttonId[ buttonCount++ ] = wxID_CANCEL;
172 }
173 }
174 // the MSW implementation even shows an OK button if it is not specified, we'll do the same
175 else
176 {
177 [alert addButtonWithTitle:cfOKString.AsNSString()];
178 buttonId[ buttonCount++ ] = wxID_OK;
179 if (style & wxCANCEL)
180 {
181 [alert addButtonWithTitle:cfCancelString.AsNSString()];
182 buttonId[ buttonCount++ ] = wxID_CANCEL;
183 }
184 }
185
186
187 wxNonOwnedWindow* parentWindow = NULL;
188 int button = -1;
189
190 if (GetParent())
191 {
192 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
193 }
194
195 if (parentWindow)
196 {
197 NSWindow* nativeParent = parentWindow->GetWXWindow();
198 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
199 [alert beginSheetModalForWindow: nativeParent modalDelegate: sheetDelegate
200 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
201 contextInfo: nil];
202 [sheetDelegate waitForSheetToFinish];
203 button = [sheetDelegate code];
204 [sheetDelegate release];
205 }
206 else
207 {
208 button = [alert runModal];
209 }
210 [alert release];
211
212 if ( button < NSAlertFirstButtonReturn )
213 resultbutton = wxID_CANCEL;
214 else
215 {
216 if ( button - NSAlertFirstButtonReturn < buttonCount )
217 resultbutton = buttonId[ button - NSAlertFirstButtonReturn ];
218 else
219 resultbutton = wxID_CANCEL;
220 }
221 }
222
223 return resultbutton;
224 }