]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/dlgcmn.cpp | |
3 | // Purpose: common (to all ports) wxDialog functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 28.06.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #include "wx/dialog.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #include "wx/button.h" | |
32 | #include "wx/dcclient.h" | |
33 | #include "wx/intl.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/stattext.h" | |
36 | #include "wx/sizer.h" | |
37 | #include "wx/containr.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/statline.h" | |
41 | #include "wx/sysopt.h" | |
42 | #include "wx/private/stattext.h" | |
43 | ||
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // wxDialogBase | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow) | |
50 | EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton) | |
51 | ||
52 | EVT_CLOSE(wxDialogBase::OnCloseWindow) | |
53 | ||
54 | EVT_CHAR_HOOK(wxDialogBase::OnCharHook) | |
55 | ||
56 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase) | |
57 | END_EVENT_TABLE() | |
58 | ||
59 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase, wxTopLevelWindow) | |
60 | ||
61 | void wxDialogBase::Init() | |
62 | { | |
63 | m_returnCode = 0; | |
64 | m_affirmativeId = wxID_OK; | |
65 | m_escapeId = wxID_ANY; | |
66 | ||
67 | // the dialogs have this flag on by default to prevent the events from the | |
68 | // dialog controls from reaching the parent frame which is usually | |
69 | // undesirable and can lead to unexpected and hard to find bugs | |
70 | SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS); | |
71 | ||
72 | WX_INIT_CONTROL_CONTAINER(); | |
73 | } | |
74 | ||
75 | // helper of GetParentForModalDialog() | |
76 | static bool CanBeUsedAsParent(wxWindow *parent) | |
77 | { | |
78 | extern WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete; | |
79 | ||
80 | return !parent->HasExtraStyle(wxWS_EX_TRANSIENT) && | |
81 | parent->IsShownOnScreen() && | |
82 | !wxPendingDelete.Member(parent) && | |
83 | !parent->IsBeingDeleted(); | |
84 | } | |
85 | ||
86 | wxWindow *wxDialogBase::GetParentForModalDialog(wxWindow *parent) const | |
87 | { | |
88 | // creating a parent-less modal dialog will result (under e.g. wxGTK2) | |
89 | // in an unfocused dialog, so try to find a valid parent for it: | |
90 | if ( parent ) | |
91 | parent = wxGetTopLevelParent(parent); | |
92 | ||
93 | if ( !parent || !CanBeUsedAsParent(parent) ) | |
94 | parent = wxTheApp->GetTopWindow(); | |
95 | ||
96 | if ( parent && !CanBeUsedAsParent(parent) ) | |
97 | { | |
98 | // can't use this one, it's going to disappear | |
99 | parent = NULL; | |
100 | } | |
101 | ||
102 | return parent; | |
103 | } | |
104 | ||
105 | #if wxUSE_STATTEXT | |
106 | ||
107 | class wxTextSizerWrapper : public wxTextWrapper | |
108 | { | |
109 | public: | |
110 | wxTextSizerWrapper(wxWindow *win) | |
111 | { | |
112 | m_win = win; | |
113 | m_hLine = 0; | |
114 | } | |
115 | ||
116 | wxSizer *CreateSizer(const wxString& text, int widthMax) | |
117 | { | |
118 | m_sizer = new wxBoxSizer(wxVERTICAL); | |
119 | Wrap(m_win, text, widthMax); | |
120 | return m_sizer; | |
121 | } | |
122 | ||
123 | protected: | |
124 | virtual void OnOutputLine(const wxString& line) | |
125 | { | |
126 | if ( !line.empty() ) | |
127 | { | |
128 | m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line)); | |
129 | } | |
130 | else // empty line, no need to create a control for it | |
131 | { | |
132 | if ( !m_hLine ) | |
133 | m_hLine = m_win->GetCharHeight(); | |
134 | ||
135 | m_sizer->Add(5, m_hLine); | |
136 | } | |
137 | } | |
138 | ||
139 | private: | |
140 | wxWindow *m_win; | |
141 | wxSizer *m_sizer; | |
142 | int m_hLine; | |
143 | }; | |
144 | ||
145 | wxSizer *wxDialogBase::CreateTextSizer(const wxString& message) | |
146 | { | |
147 | // I admit that this is complete bogus, but it makes | |
148 | // message boxes work for pda screens temporarily.. | |
149 | int widthMax = -1; | |
150 | const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA; | |
151 | if (is_pda) | |
152 | { | |
153 | widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25; | |
154 | } | |
155 | ||
156 | // '&' is used as accel mnemonic prefix in the wxWidgets controls but in | |
157 | // the static messages created by CreateTextSizer() (used by wxMessageBox, | |
158 | // for example), we don't want this special meaning, so we need to quote it | |
159 | wxString text(message); | |
160 | text.Replace(_T("&"), _T("&&")); | |
161 | ||
162 | wxTextSizerWrapper wrapper(this); | |
163 | ||
164 | return wrapper.CreateSizer(text, widthMax); | |
165 | } | |
166 | ||
167 | #endif // wxUSE_STATTEXT | |
168 | ||
169 | wxSizer *wxDialogBase::CreateButtonSizer(long flags) | |
170 | { | |
171 | wxSizer *sizer = NULL; | |
172 | ||
173 | #ifdef __SMARTPHONE__ | |
174 | wxDialog* dialog = (wxDialog*) this; | |
175 | if ( flags & wxOK ) | |
176 | dialog->SetLeftMenu(wxID_OK); | |
177 | ||
178 | if ( flags & wxCANCEL ) | |
179 | dialog->SetRightMenu(wxID_CANCEL); | |
180 | ||
181 | if ( flags & wxYES ) | |
182 | dialog->SetLeftMenu(wxID_YES); | |
183 | ||
184 | if ( flags & wxNO ) | |
185 | dialog->SetRightMenu(wxID_NO); | |
186 | #else // !__SMARTPHONE__ | |
187 | ||
188 | #if wxUSE_BUTTON | |
189 | ||
190 | #ifdef __POCKETPC__ | |
191 | // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button | |
192 | // located inside caption bar and implement Cancel functionality through | |
193 | // Undo outside dialog. As native behaviour this will be default here but | |
194 | // can be replaced with real wxButtons by setting the option below to 1 | |
195 | if ( (flags & ~(wxCANCEL|wxNO_DEFAULT)) != wxOK || | |
196 | wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) ) | |
197 | #endif // __POCKETPC__ | |
198 | { | |
199 | sizer = CreateStdDialogButtonSizer(flags); | |
200 | } | |
201 | #endif // wxUSE_BUTTON | |
202 | ||
203 | #endif // __SMARTPHONE__/!__SMARTPHONE__ | |
204 | ||
205 | return sizer; | |
206 | } | |
207 | ||
208 | wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags) | |
209 | { | |
210 | wxSizer *sizer = CreateButtonSizer(flags); | |
211 | if ( !sizer ) | |
212 | return NULL; | |
213 | ||
214 | // Mac Human Interface Guidelines recommend not to use static lines as | |
215 | // grouping elements | |
216 | #if wxUSE_STATLINE && !defined(__WXMAC__) | |
217 | wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL); | |
218 | topsizer->Add(new wxStaticLine(this), | |
219 | wxSizerFlags().Expand().DoubleBorder(wxBOTTOM)); | |
220 | topsizer->Add(sizer, wxSizerFlags().Expand()); | |
221 | sizer = topsizer; | |
222 | #endif // wxUSE_STATLINE | |
223 | ||
224 | return sizer; | |
225 | } | |
226 | ||
227 | #if wxUSE_BUTTON | |
228 | ||
229 | wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags ) | |
230 | { | |
231 | wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer(); | |
232 | ||
233 | wxButton *ok = NULL; | |
234 | wxButton *yes = NULL; | |
235 | wxButton *no = NULL; | |
236 | ||
237 | if (flags & wxOK) | |
238 | { | |
239 | ok = new wxButton(this, wxID_OK); | |
240 | sizer->AddButton(ok); | |
241 | } | |
242 | ||
243 | if (flags & wxCANCEL) | |
244 | { | |
245 | wxButton *cancel = new wxButton(this, wxID_CANCEL); | |
246 | sizer->AddButton(cancel); | |
247 | } | |
248 | ||
249 | if (flags & wxYES) | |
250 | { | |
251 | yes = new wxButton(this, wxID_YES); | |
252 | sizer->AddButton(yes); | |
253 | } | |
254 | ||
255 | if (flags & wxNO) | |
256 | { | |
257 | no = new wxButton(this, wxID_NO); | |
258 | sizer->AddButton(no); | |
259 | } | |
260 | ||
261 | if (flags & wxAPPLY) | |
262 | { | |
263 | wxButton *apply = new wxButton(this, wxID_APPLY); | |
264 | sizer->AddButton(apply); | |
265 | } | |
266 | ||
267 | if (flags & wxCLOSE) | |
268 | { | |
269 | wxButton *close = new wxButton(this, wxID_CLOSE); | |
270 | sizer->AddButton(close); | |
271 | } | |
272 | ||
273 | if (flags & wxHELP) | |
274 | { | |
275 | wxButton *help = new wxButton(this, wxID_HELP); | |
276 | sizer->AddButton(help); | |
277 | } | |
278 | ||
279 | if (flags & wxNO_DEFAULT) | |
280 | { | |
281 | if (no) | |
282 | { | |
283 | no->SetDefault(); | |
284 | no->SetFocus(); | |
285 | } | |
286 | } | |
287 | else | |
288 | { | |
289 | if (ok) | |
290 | { | |
291 | ok->SetDefault(); | |
292 | ok->SetFocus(); | |
293 | } | |
294 | else if (yes) | |
295 | { | |
296 | yes->SetDefault(); | |
297 | yes->SetFocus(); | |
298 | } | |
299 | } | |
300 | ||
301 | if (flags & wxOK) | |
302 | SetAffirmativeId(wxID_OK); | |
303 | else if (flags & wxYES) | |
304 | SetAffirmativeId(wxID_YES); | |
305 | ||
306 | sizer->Realize(); | |
307 | ||
308 | return sizer; | |
309 | } | |
310 | ||
311 | #endif // wxUSE_BUTTON | |
312 | ||
313 | // ---------------------------------------------------------------------------- | |
314 | // standard buttons handling | |
315 | // ---------------------------------------------------------------------------- | |
316 | ||
317 | void wxDialogBase::EndDialog(int rc) | |
318 | { | |
319 | if ( IsModal() ) | |
320 | EndModal(rc); | |
321 | else | |
322 | Hide(); | |
323 | } | |
324 | ||
325 | void wxDialogBase::AcceptAndClose() | |
326 | { | |
327 | if ( Validate() && TransferDataFromWindow() ) | |
328 | { | |
329 | EndDialog(m_affirmativeId); | |
330 | } | |
331 | } | |
332 | ||
333 | void wxDialogBase::SetAffirmativeId(int affirmativeId) | |
334 | { | |
335 | m_affirmativeId = affirmativeId; | |
336 | } | |
337 | ||
338 | void wxDialogBase::SetEscapeId(int escapeId) | |
339 | { | |
340 | m_escapeId = escapeId; | |
341 | } | |
342 | ||
343 | bool wxDialogBase::EmulateButtonClickIfPresent(int id) | |
344 | { | |
345 | #if wxUSE_BUTTON | |
346 | wxButton *btn = wxDynamicCast(FindWindow(id), wxButton); | |
347 | ||
348 | if ( !btn || !btn->IsEnabled() || !btn->IsShown() ) | |
349 | return false; | |
350 | ||
351 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id); | |
352 | event.SetEventObject(btn); | |
353 | btn->GetEventHandler()->ProcessEvent(event); | |
354 | ||
355 | return true; | |
356 | #else // !wxUSE_BUTTON | |
357 | wxUnusedVar(id); | |
358 | return false; | |
359 | #endif // wxUSE_BUTTON/!wxUSE_BUTTON | |
360 | } | |
361 | ||
362 | bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event) | |
363 | { | |
364 | // for most platforms, Esc key is used to close the dialogs | |
365 | return event.GetKeyCode() == WXK_ESCAPE && | |
366 | event.GetModifiers() == wxMOD_NONE; | |
367 | } | |
368 | ||
369 | void wxDialogBase::OnCharHook(wxKeyEvent& event) | |
370 | { | |
371 | if ( event.GetKeyCode() == WXK_ESCAPE ) | |
372 | { | |
373 | int idCancel = GetEscapeId(); | |
374 | switch ( idCancel ) | |
375 | { | |
376 | case wxID_NONE: | |
377 | // don't handle Esc specially at all | |
378 | break; | |
379 | ||
380 | case wxID_ANY: | |
381 | // this value is special: it means translate Esc to wxID_CANCEL | |
382 | // but if there is no such button, then fall back to wxID_OK | |
383 | if ( EmulateButtonClickIfPresent(wxID_CANCEL) ) | |
384 | return; | |
385 | idCancel = GetAffirmativeId(); | |
386 | // fall through | |
387 | ||
388 | default: | |
389 | // translate Esc to button press for the button with given id | |
390 | if ( EmulateButtonClickIfPresent(idCancel) ) | |
391 | return; | |
392 | } | |
393 | } | |
394 | ||
395 | event.Skip(); | |
396 | } | |
397 | ||
398 | void wxDialogBase::OnButton(wxCommandEvent& event) | |
399 | { | |
400 | const int id = event.GetId(); | |
401 | if ( id == GetAffirmativeId() ) | |
402 | { | |
403 | AcceptAndClose(); | |
404 | } | |
405 | else if ( id == wxID_APPLY ) | |
406 | { | |
407 | if ( Validate() ) | |
408 | TransferDataFromWindow(); | |
409 | ||
410 | // TODO: disable the Apply button until things change again | |
411 | } | |
412 | else if ( id == GetEscapeId() || | |
413 | (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) ) | |
414 | { | |
415 | EndDialog(wxID_CANCEL); | |
416 | } | |
417 | else // not a standard button | |
418 | { | |
419 | event.Skip(); | |
420 | } | |
421 | } | |
422 | ||
423 | // ---------------------------------------------------------------------------- | |
424 | // other event handlers | |
425 | // ---------------------------------------------------------------------------- | |
426 | ||
427 | void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
428 | { | |
429 | // We'll send a Cancel message by default, which may close the dialog. | |
430 | // Check for looping if the Cancel event handler calls Close(). | |
431 | ||
432 | // Note that if a cancel button and handler aren't present in the dialog, | |
433 | // nothing will happen when you close the dialog via the window manager, or | |
434 | // via Close(). We wouldn't want to destroy the dialog by default, since | |
435 | // the dialog may have been created on the stack. However, this does mean | |
436 | // that calling dialog->Close() won't delete the dialog unless the handler | |
437 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to | |
438 | // destroy the dialog. The default OnCancel (above) simply ends a modal | |
439 | // dialog, and hides a modeless dialog. | |
440 | ||
441 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global | |
442 | // lists here? don't dare to change it now, but should be done later! | |
443 | static wxList closing; | |
444 | ||
445 | if ( closing.Member(this) ) | |
446 | return; | |
447 | ||
448 | closing.Append(this); | |
449 | ||
450 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
451 | cancelEvent.SetEventObject( this ); | |
452 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
453 | ||
454 | closing.DeleteObject(this); | |
455 | } | |
456 | ||
457 | void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) | |
458 | { | |
459 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
460 | Refresh(); | |
461 | } |