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