]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dialog.cpp
full keyboard access support
[wxWidgets.git] / src / mac / carbon / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dialog.h"
15 #include "wx/utils.h"
16 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/settings.h"
19
20 #include "wx/mac/uma.h"
21
22
23 // Lists to keep track of windows, so we can disable/enable them
24 // for modal dialogs
25 wxList wxModalDialogs;
26
27 extern wxList wxPendingDelete;
28
29 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
30
31 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
32 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
33 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
34 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
35
36 EVT_CHAR_HOOK(wxDialog::OnCharHook)
37
38 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
39
40 EVT_CLOSE(wxDialog::OnCloseWindow)
41 END_EVENT_TABLE()
42
43
44 void wxDialog::Init()
45 {
46 m_isModalStyle = false;
47 }
48
49 bool wxDialog::Create( wxWindow *parent,
50 wxWindowID id,
51 const wxString& title,
52 const wxPoint& pos,
53 const wxSize& size,
54 long style,
55 const wxString& name )
56 {
57 SetExtraStyle( GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
58
59 // All dialogs should really have this style...
60 style |= wxTAB_TRAVERSAL;
61
62 // ...but not these styles
63 style &= ~(wxYES | wxOK | wxNO); // | wxCANCEL
64
65 if ( !wxTopLevelWindow::Create( parent, id, title, pos, size, style, name ) )
66 return false;
67
68 HIViewRef growBoxRef = 0 ;
69 OSStatus err = HIViewFindByID( HIViewGetRoot( (WindowRef)m_macWindow ), kHIViewWindowGrowBoxID, &growBoxRef );
70 if ( err == noErr && growBoxRef != 0 )
71 HIGrowBoxViewSetTransparent( growBoxRef, true ) ;
72
73 return true;
74 }
75
76 void wxDialog::SetModal( bool flag )
77 {
78 if ( flag )
79 {
80 m_isModalStyle = true;
81
82 wxModelessWindows.DeleteObject( this );
83
84 #if TARGET_CARBON
85 SetWindowModality( (WindowRef)MacGetWindowRef(), kWindowModalityAppModal, NULL ) ;
86 #endif
87 }
88 else
89 {
90 m_isModalStyle = false;
91
92 wxModelessWindows.Append( this );
93 }
94 }
95
96 wxDialog::~wxDialog()
97 {
98 m_isBeingDeleted = true;
99 Show(false);
100 }
101
102 // By default, pressing escape cancels the dialog; on mac command-stop does the same thing
103 void wxDialog::OnCharHook(wxKeyEvent& event)
104 {
105 if (( event.m_keyCode == WXK_ESCAPE ||
106 ( event.m_keyCode == '.' && event.MetaDown() ) )
107 && FindWindow(wxID_CANCEL) )
108 {
109 // Behaviour changed in 2.0: we'll send a Cancel message
110 // to the dialog instead of Close.
111 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
112 cancelEvent.SetEventObject( this );
113 GetEventHandler()->ProcessEvent(cancelEvent);
114
115 return;
116 }
117
118 // We didn't process this event.
119 event.Skip();
120 }
121
122 bool wxDialog::IsModal() const
123 {
124 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
125 // return m_isModalStyle;
126 }
127
128
129 bool wxDialog::IsModalShowing() const
130 {
131 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
132 }
133
134 bool wxDialog::Show(bool show)
135 {
136 if ( !wxDialogBase::Show(show) )
137 // nothing to do
138 return false;
139
140 if ( show )
141 // usually will result in TransferDataToWindow() being called
142 InitDialog();
143
144 if ( m_isModalStyle )
145 {
146 if ( show )
147 {
148 DoShowModal();
149 }
150 else // end of modal dialog
151 {
152 // this will cause IsModalShowing() return false and our local
153 // message loop will terminate
154 wxModalDialogs.DeleteObject(this);
155 }
156 }
157
158 return true;
159 }
160
161 #if !TARGET_CARBON
162 extern bool s_macIsInModalLoop ;
163 #endif
164
165 void wxDialog::DoShowModal()
166 {
167 wxCHECK_RET( !IsModalShowing(), wxT("DoShowModal() called twice") );
168
169 wxModalDialogs.Append(this);
170
171 SetFocus() ;
172
173 #if TARGET_CARBON
174 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
175 #else
176 // TODO : test whether parent gets disabled
177 bool formerModal = s_macIsInModalLoop ;
178 s_macIsInModalLoop = true ;
179 #endif
180
181 while ( IsModalShowing() )
182 {
183 wxTheApp->MacDoOneEvent() ;
184 // calls process idle itself
185 }
186
187 #if TARGET_CARBON
188 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
189 #else
190 // TODO probably reenable the parent window if any
191 s_macIsInModalLoop = formerModal ;
192 #endif
193 }
194
195
196 // Replacement for Show(true) for modal dialogs - returns return code
197 int wxDialog::ShowModal()
198 {
199 if ( !m_isModalStyle )
200 SetModal(true);
201
202 Show(true);
203
204 return GetReturnCode();
205 }
206
207 // NB: this function (surprizingly) may be called for both modal and modeless
208 // dialogs and should work for both of them
209 void wxDialog::EndModal(int retCode)
210 {
211 SetReturnCode(retCode);
212 Show(false);
213 SetModal(false);
214 }
215
216 // Standard buttons
217 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
218 {
219 if ( Validate() && TransferDataFromWindow() )
220 EndModal(wxID_OK);
221 }
222
223 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
224 {
225 if (Validate())
226 TransferDataFromWindow();
227
228 // TODO probably need to disable the Apply button until things change again
229 }
230
231 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
232 {
233 EndModal(wxID_CANCEL);
234 }
235
236 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
237 {
238 // We'll send a Cancel message by default,
239 // which may close the dialog.
240 // Check for looping if the Cancel event handler calls Close().
241
242 // Note that if a cancel button and handler aren't present in the dialog,
243 // nothing will happen when you close the dialog via the window manager, or
244 // via Close().
245 // We wouldn't want to destroy the dialog by default, since the dialog may have been
246 // created on the stack.
247 // However, this does mean that calling dialog->Close() won't delete the dialog
248 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
249 // sure to destroy the dialog.
250 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
251
252 static wxList closing;
253
254 if ( closing.Member(this) )
255 return;
256
257 closing.Append(this);
258
259 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
260 cancelEvent.SetEventObject( this );
261 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
262
263 closing.DeleteObject(this);
264 }
265
266 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
267 {
268 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
269 Refresh();
270 }
271