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