]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
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 | ||
519cb848 SC |
22 | #include <wx/mac/uma.h> |
23 | ||
e9576ca5 SC |
24 | // Lists to keep track of windows, so we can disable/enable them |
25 | // for modal dialogs | |
26 | wxList wxModalDialogs; | |
fe08e597 | 27 | //wxList wxModelessWindows; // Frames and modeless dialogs |
e9576ca5 SC |
28 | extern wxList wxPendingDelete; |
29 | ||
2f1ae414 | 30 | #if !USE_SHARED_LIBRARY |
e9576ca5 SC |
31 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel) |
32 | ||
33 | BEGIN_EVENT_TABLE(wxDialog, wxPanel) | |
519cb848 | 34 | EVT_SIZE(wxDialog::OnSize) |
e9576ca5 SC |
35 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) |
36 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) | |
37 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) | |
38 | EVT_CHAR_HOOK(wxDialog::OnCharHook) | |
39 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) | |
40 | EVT_CLOSE(wxDialog::OnCloseWindow) | |
41 | END_EVENT_TABLE() | |
42 | ||
2f1ae414 | 43 | #endif |
e9576ca5 SC |
44 | |
45 | wxDialog::wxDialog() | |
46 | { | |
2f1ae414 | 47 | m_isShown = FALSE; |
e9576ca5 SC |
48 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
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 | { | |
e9576ca5 SC |
58 | |
59 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); | |
e9576ca5 SC |
60 | |
61 | if (!parent) | |
62 | wxTopLevelWindows.Append(this); | |
63 | ||
64 | if (parent) parent->AddChild(this); | |
65 | ||
66 | if ( id == -1 ) | |
67 | m_windowId = (int)NewControlId(); | |
68 | else | |
69 | m_windowId = id; | |
70 | ||
2f1ae414 | 71 | MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) , name ) ; |
e9576ca5 | 72 | |
519cb848 | 73 | m_macWindowData->m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ; |
519cb848 | 74 | return TRUE; |
e9576ca5 SC |
75 | } |
76 | ||
77 | void wxDialog::SetModal(bool flag) | |
78 | { | |
2f1ae414 SC |
79 | if ( flag ) |
80 | { | |
81 | m_windowStyle |= wxDIALOG_MODAL; | |
82 | ||
83 | wxModelessWindows.DeleteObject(this); | |
84 | } | |
85 | else | |
86 | { | |
87 | m_windowStyle &= ~wxDIALOG_MODAL; | |
88 | ||
89 | wxModelessWindows.Append(this); | |
90 | } | |
e9576ca5 SC |
91 | } |
92 | ||
93 | wxDialog::~wxDialog() | |
94 | { | |
e7549107 | 95 | m_isBeingDeleted = TRUE ; |
e9576ca5 SC |
96 | wxTopLevelWindows.DeleteObject(this); |
97 | ||
2f1ae414 | 98 | Show(FALSE); |
e7549107 | 99 | |
2f1ae414 SC |
100 | if ( !IsModal() ) |
101 | wxModelessWindows.DeleteObject(this); | |
e9576ca5 SC |
102 | |
103 | // If this is the last top-level window, exit. | |
104 | if (wxTheApp && (wxTopLevelWindows.Number() == 0)) | |
105 | { | |
106 | wxTheApp->SetTopWindow(NULL); | |
107 | ||
108 | if (wxTheApp->GetExitOnFrameDelete()) | |
109 | { | |
519cb848 | 110 | wxTheApp->ExitMainLoop() ; |
e9576ca5 SC |
111 | } |
112 | } | |
113 | } | |
114 | ||
115 | // By default, pressing escape cancels the dialog | |
116 | void wxDialog::OnCharHook(wxKeyEvent& event) | |
117 | { | |
118 | if (event.m_keyCode == WXK_ESCAPE) | |
119 | { | |
120 | // Behaviour changed in 2.0: we'll send a Cancel message | |
121 | // to the dialog instead of Close. | |
122 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
123 | cancelEvent.SetEventObject( this ); | |
124 | GetEventHandler()->ProcessEvent(cancelEvent); | |
125 | ||
126 | return; | |
127 | } | |
128 | // We didn't process this event. | |
129 | event.Skip(); | |
130 | } | |
131 | ||
e9576ca5 | 132 | |
51abe921 SC |
133 | void wxDialog::DoSetClientSize(int width, int height) |
134 | { | |
135 | wxWindow::DoSetClientSize( width , height ) ; | |
136 | } | |
137 | ||
2f1ae414 | 138 | void wxDialog::DoGetPosition(int *x, int *y) const |
51abe921 | 139 | { |
2f1ae414 | 140 | wxWindow::DoGetPosition( x , y ) ; |
51abe921 SC |
141 | } |
142 | ||
2f1ae414 | 143 | bool wxDialog::IsModal() const |
51abe921 | 144 | { |
2f1ae414 | 145 | return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0; |
51abe921 SC |
146 | } |
147 | ||
2f1ae414 SC |
148 | |
149 | bool wxDialog::IsModalShowing() const | |
51abe921 | 150 | { |
2f1ae414 | 151 | return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast |
51abe921 SC |
152 | } |
153 | ||
154 | ||
519cb848 | 155 | extern bool s_macIsInModalLoop ; |
e9576ca5 SC |
156 | |
157 | bool wxDialog::Show(bool show) | |
158 | { | |
2f1ae414 SC |
159 | if ( !wxDialogBase::Show(show) ) |
160 | { | |
161 | // nothing to do | |
162 | return FALSE; | |
163 | } | |
e9576ca5 | 164 | |
2f1ae414 SC |
165 | if ( show ) |
166 | { | |
167 | // usually will result in TransferDataToWindow() being called | |
168 | InitDialog(); | |
169 | } | |
e9576ca5 | 170 | |
2f1ae414 | 171 | if ( IsModal() ) |
e7549107 | 172 | { |
2f1ae414 SC |
173 | if ( show ) |
174 | { | |
175 | DoShowModal(); | |
176 | } | |
177 | else // end of modal dialog | |
178 | { | |
179 | // this will cause IsModalShowing() return FALSE and our local | |
180 | // message loop will terminate | |
181 | wxModalDialogs.DeleteObject(this); | |
182 | } | |
183 | } | |
e7549107 | 184 | |
2f1ae414 | 185 | return TRUE; |
e9576ca5 SC |
186 | } |
187 | ||
2f1ae414 | 188 | void wxDialog::DoShowModal() |
51abe921 | 189 | { |
2f1ae414 | 190 | wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") ); |
51abe921 | 191 | |
2f1ae414 | 192 | wxModalDialogs.Append(this); |
51abe921 | 193 | |
2f1ae414 SC |
194 | wxWindow *parent = GetParent(); |
195 | ||
196 | // remember where the focus was | |
197 | wxWindow *winFocus = FindFocus(); | |
198 | if ( !winFocus ) | |
199 | { | |
200 | winFocus = parent; | |
201 | } | |
202 | if ( !winFocus ) | |
203 | { | |
204 | winFocus = wxTheApp->GetTopWindow(); | |
205 | } | |
206 | // TODO : test whether parent gets disabled | |
207 | ||
a49afa93 | 208 | bool formerModal = s_macIsInModalLoop ; |
2f1ae414 | 209 | s_macIsInModalLoop = true ; |
51abe921 | 210 | |
2f1ae414 SC |
211 | while ( IsModalShowing() ) |
212 | { | |
213 | while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() ) | |
214 | { | |
215 | } | |
216 | wxTheApp->MacDoOneEvent() ; | |
217 | } | |
218 | ||
a49afa93 | 219 | s_macIsInModalLoop = formerModal ; |
51abe921 | 220 | |
2f1ae414 | 221 | // TODO probably reenable the parent window if any |
51abe921 | 222 | |
2f1ae414 SC |
223 | // and restore focus |
224 | if ( winFocus ) | |
225 | { | |
226 | winFocus->SetFocus(); | |
227 | } | |
51abe921 | 228 | } |
519cb848 | 229 | |
2f1ae414 | 230 | |
e9576ca5 SC |
231 | // Replacement for Show(TRUE) for modal dialogs - returns return code |
232 | int wxDialog::ShowModal() | |
233 | { | |
519cb848 | 234 | m_windowStyle |= wxDIALOG_MODAL; |
51abe921 SC |
235 | Show(TRUE); |
236 | return GetReturnCode(); | |
e9576ca5 SC |
237 | } |
238 | ||
2f1ae414 SC |
239 | // NB: this function (surprizingly) may be called for both modal and modeless |
240 | // dialogs and should work for both of them | |
e9576ca5 SC |
241 | void wxDialog::EndModal(int retCode) |
242 | { | |
51abe921 SC |
243 | SetReturnCode(retCode); |
244 | Show(FALSE); | |
e9576ca5 SC |
245 | } |
246 | ||
247 | // Standard buttons | |
248 | void wxDialog::OnOK(wxCommandEvent& event) | |
249 | { | |
51abe921 SC |
250 | if ( Validate() && TransferDataFromWindow() ) |
251 | { | |
2f1ae414 | 252 | EndModal(wxID_OK); |
51abe921 | 253 | } |
e9576ca5 SC |
254 | } |
255 | ||
256 | void wxDialog::OnApply(wxCommandEvent& event) | |
257 | { | |
51abe921 SC |
258 | if (Validate()) |
259 | TransferDataFromWindow(); | |
260 | // TODO probably need to disable the Apply button until things change again | |
e9576ca5 SC |
261 | } |
262 | ||
263 | void wxDialog::OnCancel(wxCommandEvent& event) | |
264 | { | |
e9576ca5 | 265 | EndModal(wxID_CANCEL); |
7c74e7fe SC |
266 | } |
267 | ||
e3065973 | 268 | void wxDialog::OnCloseWindow(wxCloseEvent& event) |
e9576ca5 | 269 | { |
e3065973 | 270 | // We'll send a Cancel message by default, |
e9576ca5 | 271 | // which may close the dialog. |
e3065973 JS |
272 | // Check for looping if the Cancel event handler calls Close(). |
273 | ||
274 | // Note that if a cancel button and handler aren't present in the dialog, | |
275 | // nothing will happen when you close the dialog via the window manager, or | |
276 | // via Close(). | |
277 | // We wouldn't want to destroy the dialog by default, since the dialog may have been | |
278 | // created on the stack. | |
279 | // However, this does mean that calling dialog->Close() won't delete the dialog | |
280 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be | |
281 | // sure to destroy the dialog. | |
282 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. | |
e9576ca5 SC |
283 | |
284 | static wxList closing; | |
51abe921 | 285 | |
e9576ca5 | 286 | if ( closing.Member(this) ) |
e3065973 | 287 | return; |
51abe921 | 288 | |
e9576ca5 | 289 | closing.Append(this); |
51abe921 | 290 | |
e3065973 JS |
291 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
292 | cancelEvent.SetEventObject( this ); | |
293 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
e9576ca5 SC |
294 | |
295 | closing.DeleteObject(this); | |
e9576ca5 SC |
296 | } |
297 | ||
298 | // Destroy the window (delayed, if a managed window) | |
299 | bool wxDialog::Destroy() | |
300 | { | |
2f1ae414 SC |
301 | wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE, |
302 | _T("wxDialog destroyed twice") ); | |
303 | ||
e9576ca5 SC |
304 | wxPendingDelete.Append(this); |
305 | return TRUE; | |
306 | } | |
307 | ||
308 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event) | |
309 | { | |
310 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); | |
311 | Refresh(); | |
312 | } | |
313 |