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