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