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