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