]>
Commit | Line | Data |
---|---|---|
2646f485 | 1 | ///////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/mac/classic/dialog.cpp |
2646f485 SC |
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 | |
670f9935 | 9 | // Licence: wxWindows licence |
2646f485 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
670f9935 WS |
12 | #include "wx/wxprec.h" |
13 | ||
2646f485 | 14 | #include "wx/dialog.h" |
670f9935 WS |
15 | |
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/app.h" | |
de6185e2 | 18 | #include "wx/utils.h" |
76b49cf4 | 19 | #include "wx/frame.h" |
9eddec69 | 20 | #include "wx/settings.h" |
670f9935 WS |
21 | #endif // WX_PRECOMP |
22 | ||
2646f485 SC |
23 | #include "wx/mac/uma.h" |
24 | ||
25 | // Lists to keep track of windows, so we can disable/enable them | |
26 | // for modal dialogs | |
27 | wxList wxModalDialogs; | |
28 | //wxList wxModelessWindows; // Frames and modeless dialogs | |
29 | extern wxList wxPendingDelete; | |
30 | ||
2646f485 SC |
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 | ||
6bc3b8e9 | 45 | void wxDialog::Init() |
2646f485 | 46 | { |
70cbdcca | 47 | m_isModalStyle = false; |
2646f485 SC |
48 | SetBackgroundColour(wxSystemSettings::GetColour(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 | { | |
58 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
59 | ||
60 | ||
61 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) | |
670f9935 | 62 | return false; |
2646f485 SC |
63 | |
64 | MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) & ~(wxYES|wxOK|wxNO|wxCANCEL) , name ) ; | |
65 | ||
66 | m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ; | |
67 | SetThemeWindowBackground( (WindowRef) m_macWindow , m_macWindowBackgroundTheme , false ) ; | |
68 | ||
670f9935 | 69 | return true; |
2646f485 SC |
70 | } |
71 | ||
72 | void wxDialog::SetModal(bool flag) | |
73 | { | |
74 | if ( flag ) | |
75 | { | |
70cbdcca | 76 | m_isModalStyle = true; |
2646f485 SC |
77 | |
78 | wxModelessWindows.DeleteObject(this); | |
79 | #if TARGET_CARBON | |
80 | SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ; | |
81 | #endif | |
82 | } | |
83 | else | |
84 | { | |
70cbdcca | 85 | m_isModalStyle = false; |
2646f485 SC |
86 | |
87 | wxModelessWindows.Append(this); | |
88 | } | |
89 | } | |
90 | ||
91 | wxDialog::~wxDialog() | |
92 | { | |
670f9935 WS |
93 | m_isBeingDeleted = true; |
94 | Show(false); | |
2646f485 SC |
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 | { | |
70cbdcca | 118 | return m_isModalStyle; |
2646f485 SC |
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 | |
670f9935 | 132 | return false; |
2646f485 SC |
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 | { | |
670f9935 | 149 | // this will cause IsModalShowing() return false and our local |
2646f485 SC |
150 | // message loop will terminate |
151 | wxModalDialogs.DeleteObject(this); | |
152 | } | |
153 | } | |
154 | ||
670f9935 | 155 | return true; |
2646f485 SC |
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 | #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 | ||
670f9935 | 190 | // Replacement for Show(true) for modal dialogs - returns return code |
2646f485 SC |
191 | int wxDialog::ShowModal() |
192 | { | |
193 | if ( !IsModal() ) | |
194 | { | |
670f9935 | 195 | SetModal(true); |
2646f485 SC |
196 | } |
197 | ||
670f9935 | 198 | Show(true); |
2646f485 SC |
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 | { | |
670f9935 WS |
206 | SetReturnCode(retCode); |
207 | Show(false); | |
2646f485 SC |
208 | } |
209 | ||
210 | // Standard buttons | |
211 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) | |
212 | { | |
213 | if ( Validate() && TransferDataFromWindow() ) | |
214 | { | |
215 | EndModal(wxID_OK); | |
216 | } | |
217 | } | |
218 | ||
219 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) | |
220 | { | |
221 | if (Validate()) | |
222 | TransferDataFromWindow(); | |
223 | // TODO probably need to disable the Apply button until things change again | |
224 | } | |
225 | ||
226 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
227 | { | |
228 | EndModal(wxID_CANCEL); | |
229 | } | |
230 | ||
231 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
232 | { | |
233 | // We'll send a Cancel message by default, | |
234 | // which may close the dialog. | |
235 | // Check for looping if the Cancel event handler calls Close(). | |
236 | ||
237 | // Note that if a cancel button and handler aren't present in the dialog, | |
238 | // nothing will happen when you close the dialog via the window manager, or | |
239 | // via Close(). | |
240 | // We wouldn't want to destroy the dialog by default, since the dialog may have been | |
241 | // created on the stack. | |
242 | // However, this does mean that calling dialog->Close() won't delete the dialog | |
243 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be | |
244 | // sure to destroy the dialog. | |
245 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. | |
246 | ||
247 | static wxList closing; | |
248 | ||
249 | if ( closing.Member(this) ) | |
250 | return; | |
251 | ||
252 | closing.Append(this); | |
253 | ||
254 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
255 | cancelEvent.SetEventObject( this ); | |
256 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
257 | ||
258 | closing.DeleteObject(this); | |
259 | } | |
260 | ||
261 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) | |
262 | { | |
263 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
264 | Refresh(); | |
265 | } |