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