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