]> git.saurik.com Git - wxWidgets.git/blame - src/common/dlgcmn.cpp
Disable handling of wxEVT_MOUSEWHEEL in wxVarScrollHelperEvtHandler in wxGTK.
[wxWidgets.git] / src / common / dlgcmn.cpp
CommitLineData
e37feda2 1/////////////////////////////////////////////////////////////////////////////
f1e01716 2// Name: src/common/dlgcmn.cpp
e37feda2
VZ
3// Purpose: common (to all ports) wxDialog functions
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 28.06.99
e37feda2 7// Copyright: (c) Vadim Zeitlin
65571936 8// Licence: wxWindows licence
e37feda2
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
e37feda2
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
fdf565fe
WS
26#include "wx/dialog.h"
27
e37feda2 28#ifndef WX_PRECOMP
df57f6be 29 #include "wx/app.h"
f6bcfd97 30 #include "wx/button.h"
e37feda2 31 #include "wx/dcclient.h"
9f3a38fc 32 #include "wx/intl.h"
e37feda2 33 #include "wx/settings.h"
9f3a38fc 34 #include "wx/stattext.h"
92afa2b1 35 #include "wx/sizer.h"
7d9f12f3 36 #include "wx/containr.h"
e37feda2
VZ
37#endif
38
897b24cf
WS
39#include "wx/statline.h"
40#include "wx/sysopt.h"
3aa8e4ea 41#include "wx/module.h"
3aa8e4ea 42#include "wx/bookctrl.h"
52519733 43#include "wx/scrolwin.h"
255c07b4 44#include "wx/textwrapper.h"
691745ab 45#include "wx/modalhook.h"
897b24cf 46
3aa8e4ea
JS
47#if wxUSE_DISPLAY
48#include "wx/display.h"
49#endif
5d1b4919 50
f313deaa 51extern WXDLLEXPORT_DATA(const char) wxDialogNameStr[] = "dialog";
28953245
SC
52
53// ----------------------------------------------------------------------------
54// XTI
55// ----------------------------------------------------------------------------
56
57wxDEFINE_FLAGS( wxDialogStyle )
58wxBEGIN_FLAGS( wxDialogStyle )
59// new style border flags, we put them first to
60// use them for streaming out
61wxFLAGS_MEMBER(wxBORDER_SIMPLE)
62wxFLAGS_MEMBER(wxBORDER_SUNKEN)
63wxFLAGS_MEMBER(wxBORDER_DOUBLE)
64wxFLAGS_MEMBER(wxBORDER_RAISED)
65wxFLAGS_MEMBER(wxBORDER_STATIC)
66wxFLAGS_MEMBER(wxBORDER_NONE)
67
68// old style border flags
69wxFLAGS_MEMBER(wxSIMPLE_BORDER)
70wxFLAGS_MEMBER(wxSUNKEN_BORDER)
71wxFLAGS_MEMBER(wxDOUBLE_BORDER)
72wxFLAGS_MEMBER(wxRAISED_BORDER)
73wxFLAGS_MEMBER(wxSTATIC_BORDER)
74wxFLAGS_MEMBER(wxNO_BORDER)
75
76// standard window styles
77wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
78wxFLAGS_MEMBER(wxCLIP_CHILDREN)
79
80// dialog styles
81wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
82wxFLAGS_MEMBER(wxSTAY_ON_TOP)
83wxFLAGS_MEMBER(wxCAPTION)
84#if WXWIN_COMPATIBILITY_2_6
85wxFLAGS_MEMBER(wxTHICK_FRAME)
86#endif // WXWIN_COMPATIBILITY_2_6
87wxFLAGS_MEMBER(wxSYSTEM_MENU)
88wxFLAGS_MEMBER(wxRESIZE_BORDER)
89#if WXWIN_COMPATIBILITY_2_6
90wxFLAGS_MEMBER(wxRESIZE_BOX)
91#endif // WXWIN_COMPATIBILITY_2_6
92wxFLAGS_MEMBER(wxCLOSE_BOX)
93wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
94wxFLAGS_MEMBER(wxMINIMIZE_BOX)
95wxEND_FLAGS( wxDialogStyle )
96
97wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow, "wx/dialog.h")
98
99wxBEGIN_PROPERTIES_TABLE(wxDialog)
100wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString(), \
101 0 /*flags*/, wxT("Helpstring"), wxT("group"))
102
103wxPROPERTY_FLAGS( WindowStyle, wxDialogStyle, long, SetWindowStyleFlag, \
104 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
105 wxT("Helpstring"), wxT("group")) // style
106wxEND_PROPERTIES_TABLE()
107
108wxEMPTY_HANDLERS_TABLE(wxDialog)
109
110wxCONSTRUCTOR_6( wxDialog, wxWindow*, Parent, wxWindowID, Id, \
111 wxString, Title, wxPoint, Position, wxSize, Size, long, WindowStyle)
112
5d1b4919 113// ----------------------------------------------------------------------------
92afa2b1 114// wxDialogBase
5d1b4919 115// ----------------------------------------------------------------------------
e37feda2 116
7d9f12f3 117BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow)
a9f620da 118 EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton)
2158f4d7
VZ
119
120 EVT_CLOSE(wxDialogBase::OnCloseWindow)
121
0be27418 122 EVT_CHAR_HOOK(wxDialogBase::OnCharHook)
7d9f12f3
VS
123END_EVENT_TABLE()
124
3aa8e4ea
JS
125wxDialogLayoutAdapter* wxDialogBase::sm_layoutAdapter = NULL;
126bool wxDialogBase::sm_layoutAdaptation = false;
127
092162bc 128wxDialogBase::wxDialogBase()
7d9f12f3
VS
129{
130 m_returnCode = 0;
9ceeecb9 131 m_affirmativeId = wxID_OK;
c6ece595 132 m_escapeId = wxID_ANY;
3aa8e4ea
JS
133 m_layoutAdaptationLevel = 3;
134 m_layoutAdaptationDone = FALSE;
135 m_layoutAdaptationMode = wxDIALOG_ADAPTATION_MODE_DEFAULT;
c6ece595 136
e4b713a2
VZ
137 // the dialogs have this flag on by default to prevent the events from the
138 // dialog controls from reaching the parent frame which is usually
139 // undesirable and can lead to unexpected and hard to find bugs
140 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
7d9f12f3
VS
141}
142
8bda0ec6 143wxWindow *wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow *parent) const
893f7840 144{
4f73f25c
VZ
145 if ( !parent )
146 return NULL;
893f7840 147
d1b5dd55 148 extern WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete;
8bda0ec6
VZ
149 if ( wxPendingDelete.Member(parent) || parent->IsBeingDeleted() )
150 {
151 // this window is being deleted and we shouldn't create any children
152 // under it
153 return NULL;
154 }
155
156 if ( parent->HasExtraStyle(wxWS_EX_TRANSIENT) )
157 {
158 // this window is not being deleted yet but it's going to disappear
159 // soon so still don't parent this window under it
160 return NULL;
161 }
162
163 if ( !parent->IsShownOnScreen() )
164 {
165 // using hidden parent won't work correctly neither
166 return NULL;
167 }
168
53e34ee6
VZ
169 // FIXME-VC6: this compiler requires an explicit const cast or it fails
170 // with error C2446
171 if ( const_cast<const wxWindow *>(parent) == this )
8bda0ec6
VZ
172 {
173 // not sure if this can really happen but it doesn't hurt to guard
174 // against this clearly invalid situation
175 return NULL;
176 }
177
178 return parent;
893f7840
VZ
179}
180
cdc48273
VZ
181wxWindow *
182wxDialogBase::GetParentForModalDialog(wxWindow *parent, long style) const
2229243b
VZ
183{
184 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
8bda0ec6
VZ
185 // in an unfocused dialog, so try to find a valid parent for it unless we
186 // were explicitly asked not to
cdc48273 187 if ( style & wxDIALOG_NO_PARENT )
8bda0ec6
VZ
188 return NULL;
189
8bda0ec6 190 // first try the given parent
2229243b 191 if ( parent )
8bda0ec6 192 parent = CheckIfCanBeUsedAsParent(wxGetTopLevelParent(parent));
2229243b 193
8bda0ec6
VZ
194 // then the currently active window
195 if ( !parent )
4f73f25c
VZ
196 parent = CheckIfCanBeUsedAsParent(
197 wxGetTopLevelParent(wxGetActiveWindow()));
2229243b 198
8bda0ec6
VZ
199 // and finally the application main window
200 if ( !parent )
201 parent = CheckIfCanBeUsedAsParent(wxTheApp->GetTopWindow());
2229243b
VZ
202
203 return parent;
204}
205
5d1b4919 206#if wxUSE_STATTEXT
1e6feb95 207
c79510ca 208wxSizer *wxDialogBase::CreateTextSizer(const wxString& message)
cfd1ac21 209{
c79510ca 210 wxTextSizerWrapper wrapper(this);
cfd1ac21 211
c79510ca
VZ
212 return CreateTextSizer(message, wrapper);
213}
cfd1ac21 214
c79510ca
VZ
215wxSizer *wxDialogBase::CreateTextSizer(const wxString& message,
216 wxTextSizerWrapper& wrapper)
5d1b4919 217{
2b5f62a0
VZ
218 // I admit that this is complete bogus, but it makes
219 // message boxes work for pda screens temporarily..
5d1b4919
VZ
220 int widthMax = -1;
221 const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
2b5f62a0
VZ
222 if (is_pda)
223 {
5d1b4919 224 widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
2b5f62a0 225 }
68379eaf 226
63415ba9 227 return wrapper.CreateSizer(message, widthMax);
cfd1ac21 228}
5d1b4919 229
5d1b4919 230#endif // wxUSE_STATTEXT
1e6feb95 231
25eb10d2 232wxSizer *wxDialogBase::CreateButtonSizer(long flags)
e37feda2 233{
25eb10d2 234#ifdef __SMARTPHONE__
102c0454 235 wxDialog* dialog = (wxDialog*) this;
25eb10d2 236 if ( flags & wxOK )
102c0454 237 dialog->SetLeftMenu(wxID_OK);
102c0454 238
25eb10d2 239 if ( flags & wxCANCEL )
102c0454 240 dialog->SetRightMenu(wxID_CANCEL);
102c0454 241
25eb10d2 242 if ( flags & wxYES )
102c0454 243 dialog->SetLeftMenu(wxID_YES);
897b24cf 244
25eb10d2
VZ
245 if ( flags & wxNO )
246 dialog->SetRightMenu(wxID_NO);
e822d1bd
VZ
247
248 return NULL;
897b24cf
WS
249#else // !__SMARTPHONE__
250
25eb10d2
VZ
251#if wxUSE_BUTTON
252
897b24cf 253#ifdef __POCKETPC__
25eb10d2
VZ
254 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
255 // located inside caption bar and implement Cancel functionality through
256 // Undo outside dialog. As native behaviour this will be default here but
257 // can be replaced with real wxButtons by setting the option below to 1
258 if ( (flags & ~(wxCANCEL|wxNO_DEFAULT)) != wxOK ||
259 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
260#endif // __POCKETPC__
897b24cf 261 {
e822d1bd 262 return CreateStdDialogButtonSizer(flags);
897b24cf 263 }
e822d1bd
VZ
264#ifdef __POCKETPC__
265 return NULL;
266#endif // __POCKETPC__
267
8d22935d
VZ
268#else // !wxUSE_BUTTON
269 wxUnusedVar(flags);
e822d1bd
VZ
270
271 return NULL;
8d22935d 272#endif // wxUSE_BUTTON/!wxUSE_BUTTON
897b24cf 273
25eb10d2 274#endif // __SMARTPHONE__/!__SMARTPHONE__
25eb10d2 275}
897b24cf 276
b14cca2a 277wxSizer *wxDialogBase::CreateSeparatedSizer(wxSizer *sizer)
25eb10d2 278{
25eb10d2
VZ
279 // Mac Human Interface Guidelines recommend not to use static lines as
280 // grouping elements
281#if wxUSE_STATLINE && !defined(__WXMAC__)
282 wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
283 topsizer->Add(new wxStaticLine(this),
284 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM));
285 topsizer->Add(sizer, wxSizerFlags().Expand());
286 sizer = topsizer;
287#endif // wxUSE_STATLINE
897b24cf 288
897b24cf 289 return sizer;
acf2ac37 290}
68379eaf 291
b14cca2a
VZ
292wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags)
293{
294 wxSizer *sizer = CreateButtonSizer(flags);
295 if ( !sizer )
296 return NULL;
297
298 return CreateSeparatedSizer(sizer);
299}
300
897b24cf
WS
301#if wxUSE_BUTTON
302
acf2ac37
RR
303wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags )
304{
305 wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer();
897b24cf 306
acf2ac37 307 wxButton *ok = NULL;
acf2ac37
RR
308 wxButton *yes = NULL;
309 wxButton *no = NULL;
52069700 310
25eb10d2
VZ
311 if (flags & wxOK)
312 {
331c1816 313 ok = new wxButton(this, wxID_OK);
52069700 314 sizer->AddButton(ok);
2b5f62a0 315 }
52069700 316
25eb10d2
VZ
317 if (flags & wxCANCEL)
318 {
331c1816 319 wxButton *cancel = new wxButton(this, wxID_CANCEL);
52069700 320 sizer->AddButton(cancel);
b5b49e42 321 }
52069700 322
25eb10d2
VZ
323 if (flags & wxYES)
324 {
331c1816 325 yes = new wxButton(this, wxID_YES);
52069700 326 sizer->AddButton(yes);
e37feda2 327 }
52069700 328
25eb10d2
VZ
329 if (flags & wxNO)
330 {
331c1816 331 no = new wxButton(this, wxID_NO);
52069700 332 sizer->AddButton(no);
e37feda2 333 }
52069700 334
57d7f988
VZ
335 if (flags & wxAPPLY)
336 {
337 wxButton *apply = new wxButton(this, wxID_APPLY);
338 sizer->AddButton(apply);
339 }
340
341 if (flags & wxCLOSE)
342 {
343 wxButton *close = new wxButton(this, wxID_CLOSE);
344 sizer->AddButton(close);
345 }
346
25eb10d2
VZ
347 if (flags & wxHELP)
348 {
331c1816 349 wxButton *help = new wxButton(this, wxID_HELP);
52069700 350 sizer->AddButton(help);
e37feda2 351 }
52069700 352
b730516c
RD
353 if (flags & wxNO_DEFAULT)
354 {
355 if (no)
356 {
357 no->SetDefault();
358 no->SetFocus();
359 }
360 }
361 else
92afa2b1
RR
362 {
363 if (ok)
364 {
365 ok->SetDefault();
366 ok->SetFocus();
367 }
368 else if (yes)
369 {
370 yes->SetDefault();
371 yes->SetFocus();
372 }
373 }
d30814cd 374
9ceeecb9
JS
375 if (flags & wxOK)
376 SetAffirmativeId(wxID_OK);
377 else if (flags & wxYES)
378 SetAffirmativeId(wxID_YES);
b730516c 379
d30814cd
MB
380 sizer->Realize();
381
acf2ac37 382 return sizer;
e37feda2
VZ
383}
384
1e6feb95 385#endif // wxUSE_BUTTON
0be27418 386
551f281b 387// ----------------------------------------------------------------------------
a9f620da 388// standard buttons handling
551f281b
VZ
389// ----------------------------------------------------------------------------
390
a9f620da
VZ
391void wxDialogBase::EndDialog(int rc)
392{
393 if ( IsModal() )
394 EndModal(rc);
395 else
396 Hide();
397}
398
551f281b
VZ
399void wxDialogBase::AcceptAndClose()
400{
401 if ( Validate() && TransferDataFromWindow() )
402 {
a9f620da 403 EndDialog(m_affirmativeId);
551f281b
VZ
404 }
405}
406
407void wxDialogBase::SetAffirmativeId(int affirmativeId)
408{
fabd7a7f 409 m_affirmativeId = affirmativeId;
551f281b
VZ
410}
411
412void wxDialogBase::SetEscapeId(int escapeId)
413{
fabd7a7f 414 m_escapeId = escapeId;
551f281b
VZ
415}
416
0be27418
VZ
417bool wxDialogBase::EmulateButtonClickIfPresent(int id)
418{
dbfa7f33 419#if wxUSE_BUTTON
0be27418
VZ
420 wxButton *btn = wxDynamicCast(FindWindow(id), wxButton);
421
422 if ( !btn || !btn->IsEnabled() || !btn->IsShown() )
423 return false;
424
ce7fe42e 425 wxCommandEvent event(wxEVT_BUTTON, id);
0be27418
VZ
426 event.SetEventObject(btn);
427 btn->GetEventHandler()->ProcessEvent(event);
428
429 return true;
dbfa7f33
VS
430#else // !wxUSE_BUTTON
431 wxUnusedVar(id);
432 return false;
433#endif // wxUSE_BUTTON/!wxUSE_BUTTON
0be27418
VZ
434}
435
83a7613b
VZ
436bool wxDialogBase::SendCloseButtonClickEvent()
437{
438 int idCancel = GetEscapeId();
439 switch ( idCancel )
440 {
441 case wxID_NONE:
442 // The user doesn't want this dialog to close "implicitly".
443 break;
444
445 case wxID_ANY:
446 // this value is special: it means translate Esc to wxID_CANCEL
447 // but if there is no such button, then fall back to wxID_OK
448 if ( EmulateButtonClickIfPresent(wxID_CANCEL) )
449 return true;
450 idCancel = GetAffirmativeId();
451 // fall through
452
453 default:
454 // translate Esc to button press for the button with given id
455 if ( EmulateButtonClickIfPresent(idCancel) )
456 return true;
457 }
458
459 return false;
460}
461
0be27418
VZ
462bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event)
463{
f821bc36
VZ
464 // For most platforms, Esc key is used to close the dialogs.
465 //
466 // Notice that we intentionally don't check for modifiers here, Shift-Esc,
467 // Alt-Esc and so on still close the dialog, typically.
468 return event.GetKeyCode() == WXK_ESCAPE;
0be27418
VZ
469}
470
471void wxDialogBase::OnCharHook(wxKeyEvent& event)
472{
f821bc36 473 if ( IsEscapeKey(event) )
0be27418 474 {
83a7613b 475 if ( SendCloseButtonClickEvent() )
0be27418 476 {
83a7613b
VZ
477 // Skip the call to event.Skip() below, we did handle this key.
478 return;
0be27418
VZ
479 }
480 }
481
482 event.Skip();
483}
484
a9f620da 485void wxDialogBase::OnButton(wxCommandEvent& event)
2158f4d7 486{
a9f620da
VZ
487 const int id = event.GetId();
488 if ( id == GetAffirmativeId() )
489 {
490 AcceptAndClose();
491 }
492 else if ( id == wxID_APPLY )
493 {
494 if ( Validate() )
495 TransferDataFromWindow();
2158f4d7 496
a9f620da
VZ
497 // TODO: disable the Apply button until things change again
498 }
499 else if ( id == GetEscapeId() ||
500 (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) )
501 {
502 EndDialog(wxID_CANCEL);
503 }
504 else // not a standard button
505 {
506 event.Skip();
507 }
2158f4d7
VZ
508}
509
94b4dd54 510// ----------------------------------------------------------------------------
03647350 511// compatibility methods for supporting the modality API
94b4dd54
SC
512// ----------------------------------------------------------------------------
513
514wxDEFINE_EVENT( wxEVT_WINDOW_MODAL_DIALOG_CLOSED , wxWindowModalDialogEvent );
515
ebcd855c
SC
516IMPLEMENT_DYNAMIC_CLASS(wxWindowModalDialogEvent, wxCommandEvent)
517
2d4c4ba8 518void wxDialogBase::ShowWindowModal ()
94b4dd54
SC
519{
520 ShowModal();
521 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
94b4dd54
SC
522}
523
524void wxDialogBase::SendWindowModalDialogEvent ( wxEventType type )
525{
9f88f9fb 526 wxWindowModalDialogEvent event ( type, GetId());
94b4dd54 527 event.SetEventObject(this);
03647350 528
94b4dd54
SC
529 if ( !GetEventHandler()->ProcessEvent(event) )
530 {
531 // the event is not propagated upwards to the parent automatically
532 // because the dialog is a top level window, so do it manually as
533 // in 9 cases of 10 the message must be processed by the dialog
534 // owner and not the dialog itself
535 (void)GetParent()->GetEventHandler()->ProcessEvent(event);
03647350 536 }
94b4dd54
SC
537}
538
539
540wxDialogModality wxDialogBase::GetModality() const
541{
542 return IsModal() ? wxDIALOG_MODALITY_APP_MODAL : wxDIALOG_MODALITY_NONE;
543}
544
a9f620da
VZ
545// ----------------------------------------------------------------------------
546// other event handlers
547// ----------------------------------------------------------------------------
2158f4d7
VZ
548
549void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
550{
551 // We'll send a Cancel message by default, which may close the dialog.
c84d0c86 552
83a7613b
VZ
553 // Check for looping if the Cancel event handler calls Close().
554 //
2158f4d7
VZ
555 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
556 // lists here? don't dare to change it now, but should be done later!
557 static wxList closing;
558
559 if ( closing.Member(this) )
560 return;
561
562 closing.Append(this);
563
ffc71ce5
VZ
564 if ( !SendCloseButtonClickEvent() )
565 {
566 // If the handler didn't close the dialog (e.g. because there is no
567 // button with matching id) we still want to close it when the user
568 // clicks the "x" button in the title bar, otherwise we shouldn't even
569 // have put it there.
570 //
571 // Notice that using wxID_CLOSE might have been a better choice but we
572 // use wxID_CANCEL for compatibility reasons.
573 EndDialog(wxID_CANCEL);
574 }
2158f4d7
VZ
575
576 closing.DeleteObject(this);
577}
578
a45f904d 579void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2158f4d7 580{
a45f904d
JS
581#ifndef __WXGTK__
582 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
583 Refresh();
584#endif
585
586 event.Skip();
2158f4d7 587}
3aa8e4ea
JS
588
589/// Do the adaptation
590bool wxDialogBase::DoLayoutAdaptation()
591{
592 if (GetLayoutAdapter())
3e84eb5f
JS
593 {
594 wxWindow* focusWindow = wxFindFocusDescendant(this); // from event.h
595 if (GetLayoutAdapter()->DoLayoutAdaptation((wxDialog*) this))
596 {
597 if (focusWindow)
598 focusWindow->SetFocus();
599 return true;
600 }
601 else
602 return false;
603 }
3aa8e4ea
JS
604 else
605 return false;
606}
607
608/// Can we do the adaptation?
609bool wxDialogBase::CanDoLayoutAdaptation()
610{
611 // Check if local setting overrides the global setting
612 bool layoutEnabled = (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED));
613
614 return (layoutEnabled && !m_layoutAdaptationDone && GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL && GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog*) this));
615}
616
617/// Set scrolling adapter class, returning old adapter
618wxDialogLayoutAdapter* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter* adapter)
619{
620 wxDialogLayoutAdapter* oldLayoutAdapter = sm_layoutAdapter;
621 sm_layoutAdapter = adapter;
622 return oldLayoutAdapter;
623}
624
625/*!
626 * Standard adapter
627 */
628
629IMPLEMENT_CLASS(wxDialogLayoutAdapter, wxObject)
630
631IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter, wxDialogLayoutAdapter)
632
633// Allow for caption size on wxWidgets < 2.9
634#if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0)
635#define wxEXTRA_DIALOG_HEIGHT 30
636#else
637#define wxEXTRA_DIALOG_HEIGHT 0
638#endif
639
640/// Indicate that adaptation should be done
641bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog* dialog)
642{
643 if (dialog->GetSizer())
644 {
645 wxSize windowSize, displaySize;
646 return MustScroll(dialog, windowSize, displaySize) != 0;
647 }
648 else
649 return false;
650}
651
652bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog* dialog)
653{
654 if (dialog->GetSizer())
655 {
f8b3f036 656#if wxUSE_BOOKCTRL
3aa8e4ea
JS
657 wxBookCtrlBase* bookContentWindow = wxDynamicCast(dialog->GetContentWindow(), wxBookCtrlBase);
658
659 if (bookContentWindow)
660 {
661 // If we have a book control, make all the pages (that use sizers) scrollable
662 wxWindowList windows;
663 for (size_t i = 0; i < bookContentWindow->GetPageCount(); i++)
664 {
665 wxWindow* page = bookContentWindow->GetPage(i);
666
667 wxScrolledWindow* scrolledWindow = wxDynamicCast(page, wxScrolledWindow);
668 if (scrolledWindow)
669 windows.Append(scrolledWindow);
670 else if (!scrolledWindow && page->GetSizer())
671 {
672 // Create a scrolled window and reparent
673 scrolledWindow = CreateScrolledWindow(page);
674 wxSizer* oldSizer = page->GetSizer();
675
676 wxSizer* newSizer = new wxBoxSizer(wxVERTICAL);
677 newSizer->Add(scrolledWindow,1, wxEXPAND, 0);
678
679 page->SetSizer(newSizer, false /* don't delete the old sizer */);
680
681 scrolledWindow->SetSizer(oldSizer);
682
683 ReparentControls(page, scrolledWindow);
684
685 windows.Append(scrolledWindow);
686 }
687 }
688
689 FitWithScrolling(dialog, windows);
690 }
691 else
f8b3f036 692#endif // wxUSE_BOOKCTRL
3aa8e4ea 693 {
6d61520d 694#if wxUSE_BUTTON
3aa8e4ea
JS
695 // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer
696 // for the main buttons.
697 wxScrolledWindow* scrolledWindow = CreateScrolledWindow(dialog);
698
699 int buttonSizerBorder = 0;
700
701 // First try to find a wxStdDialogButtonSizer
702 wxSizer* buttonSizer = FindButtonSizer(true /* find std button sizer */, dialog, dialog->GetSizer(), buttonSizerBorder);
703
704 // Next try to find a wxBoxSizer containing the controls
705 if (!buttonSizer && dialog->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER)
706 buttonSizer = FindButtonSizer(false /* find ordinary sizer */, dialog, dialog->GetSizer(), buttonSizerBorder);
707
708 // If we still don't have a button sizer, collect any 'loose' buttons in the layout
709 if (!buttonSizer && dialog->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER)
710 {
711 int count = 0;
712 wxStdDialogButtonSizer* stdButtonSizer = new wxStdDialogButtonSizer;
713 buttonSizer = stdButtonSizer;
714
715 FindLooseButtons(dialog, stdButtonSizer, dialog->GetSizer(), count);
716 if (count > 0)
717 stdButtonSizer->Realize();
718 else
719 {
5276b0a5 720 wxDELETE(buttonSizer);
3aa8e4ea
JS
721 }
722 }
723
724 if (buttonSizerBorder == 0)
725 buttonSizerBorder = 5;
726
727 ReparentControls(dialog, scrolledWindow, buttonSizer);
728
729 wxBoxSizer* newTopSizer = new wxBoxSizer(wxVERTICAL);
730 wxSizer* oldSizer = dialog->GetSizer();
731
732 dialog->SetSizer(newTopSizer, false /* don't delete old sizer */);
733
734 newTopSizer->Add(scrolledWindow, 1, wxEXPAND|wxALL, 0);
735 if (buttonSizer)
736 newTopSizer->Add(buttonSizer, 0, wxEXPAND|wxALL, buttonSizerBorder);
737
738 scrolledWindow->SetSizer(oldSizer);
739
740 FitWithScrolling(dialog, scrolledWindow);
6d61520d 741#endif // wxUSE_BUTTON
3aa8e4ea
JS
742 }
743 }
744
745 dialog->SetLayoutAdaptationDone(true);
746 return true;
747}
748
749// Create the scrolled window
750wxScrolledWindow* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow* parent)
751{
752 wxScrolledWindow* scrolledWindow = new wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxVSCROLL|wxHSCROLL|wxBORDER_NONE);
753 return scrolledWindow;
754}
755
6d61520d
VZ
756#if wxUSE_BUTTON
757
3aa8e4ea
JS
758/// Find and remove the button sizer, if any
759wxSizer* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer, wxDialog* dialog, wxSizer* sizer, int& retBorder, int accumlatedBorder)
760{
761 for ( wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst();
762 node; node = node->GetNext() )
763 {
764 wxSizerItem *item = node->GetData();
765 wxSizer *childSizer = item->GetSizer();
766
767 if ( childSizer )
768 {
769 int newBorder = accumlatedBorder;
770 if (item->GetFlag() & wxALL)
771 newBorder += item->GetBorder();
772
773 if (stdButtonSizer) // find wxStdDialogButtonSizer
774 {
775 wxStdDialogButtonSizer* buttonSizer = wxDynamicCast(childSizer, wxStdDialogButtonSizer);
776 if (buttonSizer)
777 {
778 sizer->Detach(childSizer);
779 retBorder = newBorder;
780 return buttonSizer;
781 }
782 }
783 else // find a horizontal box sizer containing standard buttons
784 {
785 wxBoxSizer* buttonSizer = wxDynamicCast(childSizer, wxBoxSizer);
786 if (buttonSizer && IsOrdinaryButtonSizer(dialog, buttonSizer))
787 {
788 sizer->Detach(childSizer);
789 retBorder = newBorder;
790 return buttonSizer;
791 }
792 }
793
794 wxSizer* s = FindButtonSizer(stdButtonSizer, dialog, childSizer, retBorder, newBorder);
795 if (s)
796 return s;
797 }
798 }
799 return NULL;
800}
801
802/// Check if this sizer contains standard buttons, and so can be repositioned in the dialog
803bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog* dialog, wxBoxSizer* sizer)
804{
805 if (sizer->GetOrientation() != wxHORIZONTAL)
806 return false;
807
808 for ( wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst();
809 node; node = node->GetNext() )
810 {
811 wxSizerItem *item = node->GetData();
812 wxButton *childButton = wxDynamicCast(item->GetWindow(), wxButton);
813
814 if (childButton && IsStandardButton(dialog, childButton))
815 return true;
816 }
817 return false;
818}
819
820/// Check if this is a standard button
821bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog* dialog, wxButton* button)
822{
823 wxWindowID id = button->GetId();
824
825 return (id == wxID_OK || id == wxID_CANCEL || id == wxID_YES || id == wxID_NO || id == wxID_SAVE ||
826 id == wxID_APPLY || id == wxID_HELP || id == wxID_CONTEXT_HELP || dialog->IsMainButtonId(id));
827}
828
829/// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
830bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog* dialog, wxStdDialogButtonSizer* buttonSizer, wxSizer* sizer, int& count)
831{
832 wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst();
833 while (node)
834 {
835 wxSizerItemList::compatibility_iterator next = node->GetNext();
836 wxSizerItem *item = node->GetData();
837 wxSizer *childSizer = item->GetSizer();
838 wxButton *childButton = wxDynamicCast(item->GetWindow(), wxButton);
839
840 if (childButton && IsStandardButton(dialog, childButton))
841 {
842 sizer->Detach(childButton);
843 buttonSizer->AddButton(childButton);
844 count ++;
845 }
846
847 if (childSizer)
848 FindLooseButtons(dialog, buttonSizer, childSizer, count);
849
850 node = next;
851 }
852 return true;
853}
854
6d61520d
VZ
855#endif // wxUSE_BUTTON
856
3aa8e4ea
JS
857/// Reparent the controls to the scrolled window
858void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer)
859{
860 DoReparentControls(parent, reparentTo, buttonSizer);
861}
862
863void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer)
864{
865 wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
866 while (node)
867 {
868 wxWindowList::compatibility_iterator next = node->GetNext();
869
870 wxWindow *win = node->GetData();
871
872 // Don't reparent the scrolled window or buttons in the button sizer
873 if (win != reparentTo && (!buttonSizer || !buttonSizer->GetItem(win)))
874 {
875 win->Reparent(reparentTo);
876#ifdef __WXMSW__
877 // Restore correct tab order
878 ::SetWindowPos((HWND) win->GetHWND(), HWND_BOTTOM, -1, -1, -1, -1, SWP_NOMOVE|SWP_NOSIZE);
879#endif
880 }
881
882 node = next;
883 }
884}
885
886/// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
887int wxStandardDialogLayoutAdapter::MustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize)
888{
889 return DoMustScroll(dialog, windowSize, displaySize);
890}
891
892/// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
893int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize)
894{
895 wxSize minWindowSize = dialog->GetSizer()->GetMinSize();
896 windowSize = dialog->GetSize();
897 windowSize = wxSize(wxMax(windowSize.x, minWindowSize.x), wxMax(windowSize.y, minWindowSize.y));
898#if wxUSE_DISPLAY
899 displaySize = wxDisplay(wxDisplay::GetFromWindow(dialog)).GetClientArea().GetSize();
900#else
728cf0ad 901 displaySize = wxGetClientDisplayRect().GetSize();
3aa8e4ea
JS
902#endif
903
904 int flags = 0;
905
906 if (windowSize.y >= (displaySize.y - wxEXTRA_DIALOG_HEIGHT))
907 flags |= wxVERTICAL;
908 if (windowSize.x >= displaySize.x)
909 flags |= wxHORIZONTAL;
910
911 return flags;
912}
913
914// A function to fit the dialog around its contents, and then adjust for screen size.
915// If scrolled windows are passed, scrolling is enabled in the required orientation(s).
916bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog* dialog, wxWindowList& windows)
917{
918 return DoFitWithScrolling(dialog, windows);
919}
920
921// A function to fit the dialog around its contents, and then adjust for screen size.
922// If a scrolled window is passed, scrolling is enabled in the required orientation(s).
923bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow)
924{
925 return DoFitWithScrolling(dialog, scrolledWindow);
926}
927
928// A function to fit the dialog around its contents, and then adjust for screen size.
929// If a scrolled window is passed, scrolling is enabled in the required orientation(s).
930bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow)
931{
932 wxWindowList windows;
933 windows.Append(scrolledWindow);
934 return DoFitWithScrolling(dialog, windows);
935}
936
937bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxWindowList& windows)
938{
939 wxSizer* sizer = dialog->GetSizer();
940 if (!sizer)
941 return false;
942
943 sizer->SetSizeHints(dialog);
944
945 wxSize windowSize, displaySize;
946 int scrollFlags = DoMustScroll(dialog, windowSize, displaySize);
947 int scrollBarSize = 20;
948
949 if (scrollFlags)
950 {
951 int scrollBarExtraX = 0, scrollBarExtraY = 0;
952 bool resizeHorizontally = (scrollFlags & wxHORIZONTAL) != 0;
953 bool resizeVertically = (scrollFlags & wxVERTICAL) != 0;
954
955 if (windows.GetCount() != 0)
956 {
957 // Allow extra for a scrollbar, assuming we resizing in one direction only.
958 if ((resizeVertically && !resizeHorizontally) && (windowSize.x < (displaySize.x - scrollBarSize)))
959 scrollBarExtraX = scrollBarSize;
960 if ((resizeHorizontally && !resizeVertically) && (windowSize.y < (displaySize.y - scrollBarSize)))
961 scrollBarExtraY = scrollBarSize;
962 }
963
964 wxWindowList::compatibility_iterator node = windows.GetFirst();
965 while (node)
966 {
967 wxWindow *win = node->GetData();
968 wxScrolledWindow* scrolledWindow = wxDynamicCast(win, wxScrolledWindow);
969 if (scrolledWindow)
970 {
971 scrolledWindow->SetScrollRate(resizeHorizontally ? 10 : 0, resizeVertically ? 10 : 0);
972
973 if (scrolledWindow->GetSizer())
974 scrolledWindow->GetSizer()->Fit(scrolledWindow);
975 }
976
977 node = node->GetNext();
978 }
979
980 wxSize limitTo = windowSize + wxSize(scrollBarExtraX, scrollBarExtraY);
981 if (resizeVertically)
982 limitTo.y = displaySize.y - wxEXTRA_DIALOG_HEIGHT;
983 if (resizeHorizontally)
984 limitTo.x = displaySize.x;
985
986 dialog->SetMinSize(limitTo);
987 dialog->SetSize(limitTo);
988
989 dialog->SetSizeHints( limitTo.x, limitTo.y, dialog->GetMaxWidth(), dialog->GetMaxHeight() );
990 }
991
992 return true;
993}
994
995/*!
996 * Module to initialise standard adapter
997 */
998
999class wxDialogLayoutAdapterModule: public wxModule
1000{
1001 DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule)
1002public:
1003 wxDialogLayoutAdapterModule() {}
1004 virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL); }
1005 virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter); return true; }
1006};
1007
1008IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule, wxModule)