]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dialog.cpp
Patch by David Brinegar to fix menubar updating after modal dialog shown
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dialog.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/dialog.h"
19 #include "wx/utils.h"
20 #include "wx/frame.h"
21 #include "wx/app.h"
22 #include "wx/settings.h"
23
24 #include "wx/mac/uma.h"
25
26 // Lists to keep track of windows, so we can disable/enable them
27 // for modal dialogs
28 wxList wxModalDialogs;
29 //wxList wxModelessWindows; // Frames and modeless dialogs
30 extern wxList wxPendingDelete;
31
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
34
35 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
36 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
37 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
38 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
39
40 EVT_CHAR_HOOK(wxDialog::OnCharHook)
41
42 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
43
44 EVT_CLOSE(wxDialog::OnCloseWindow)
45 END_EVENT_TABLE()
46
47 #endif
48
49 void wxDialog::Init()
50 {
51 m_isModalStyle = false;
52 }
53
54 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
55 const wxString& title,
56 const wxPoint& pos,
57 const wxSize& size,
58 long style,
59 const wxString& name)
60 {
61 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
62
63 // All dialogs should really have this style
64 style |= wxTAB_TRAVERSAL;
65
66 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style & ~(wxYES|wxOK|wxNO|wxCANCEL) , name) )
67 return FALSE;
68
69 return TRUE;
70 }
71
72 void wxDialog::SetModal(bool flag)
73 {
74 if ( flag )
75 {
76 m_isModalStyle = true;
77
78 wxModelessWindows.DeleteObject(this);
79 #if TARGET_CARBON
80 SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ;
81 #endif
82 }
83 else
84 {
85 m_isModalStyle = false;
86
87 wxModelessWindows.Append(this);
88 }
89 }
90
91 wxDialog::~wxDialog()
92 {
93 m_isBeingDeleted = TRUE;
94 Show(FALSE);
95 }
96
97 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
98 void wxDialog::OnCharHook(wxKeyEvent& event)
99 {
100 if (( 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 m_isModalStyle;
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 SetFocus() ;
169
170 #if TARGET_CARBON
171 BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ;
172 #else
173 // TODO : test whether parent gets disabled
174 bool formerModal = s_macIsInModalLoop ;
175 s_macIsInModalLoop = true ;
176 #endif
177 while ( IsModalShowing() )
178 {
179 wxTheApp->MacDoOneEvent() ;
180 // calls process idle itself
181 }
182
183 #if TARGET_CARBON
184 EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ;
185 #else
186 // TODO probably reenable the parent window if any
187 s_macIsInModalLoop = formerModal ;
188 #endif
189 }
190
191
192 // Replacement for Show(TRUE) for modal dialogs - returns return code
193 int wxDialog::ShowModal()
194 {
195 if ( !IsModal() )
196 {
197 SetModal(TRUE);
198 }
199
200 Show(TRUE);
201 return GetReturnCode();
202 }
203
204 // NB: this function (surprizingly) may be called for both modal and modeless
205 // dialogs and should work for both of them
206 void wxDialog::EndModal(int retCode)
207 {
208 SetReturnCode(retCode);
209 Show(FALSE);
210 }
211
212 // Standard buttons
213 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
214 {
215 if ( Validate() && TransferDataFromWindow() )
216 {
217 EndModal(wxID_OK);
218 }
219 }
220
221 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
222 {
223 if (Validate())
224 TransferDataFromWindow();
225 // TODO probably need to disable the Apply button until things change again
226 }
227
228 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
229 {
230 EndModal(wxID_CANCEL);
231 }
232
233 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
234 {
235 // We'll send a Cancel message by default,
236 // which may close the dialog.
237 // Check for looping if the Cancel event handler calls Close().
238
239 // Note that if a cancel button and handler aren't present in the dialog,
240 // nothing will happen when you close the dialog via the window manager, or
241 // via Close().
242 // We wouldn't want to destroy the dialog by default, since the dialog may have been
243 // created on the stack.
244 // However, this does mean that calling dialog->Close() won't delete the dialog
245 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
246 // sure to destroy the dialog.
247 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
248
249 static wxList closing;
250
251 if ( closing.Member(this) )
252 return;
253
254 closing.Append(this);
255
256 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
257 cancelEvent.SetEventObject( this );
258 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
259
260 closing.DeleteObject(this);
261 }
262
263 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
264 {
265 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
266 Refresh();
267 }
268