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