Add wxEventLoop::ScheduleExit().
[wxWidgets.git] / src / common / dlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dlgcmn.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dialog.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
33 #include "wx/intl.h"
34 #include "wx/settings.h"
35 #include "wx/stattext.h"
36 #include "wx/sizer.h"
37 #include "wx/containr.h"
38 #endif
39
40 #include "wx/statline.h"
41 #include "wx/sysopt.h"
42 #include "wx/module.h"
43 #include "wx/bookctrl.h"
44 #include "wx/scrolwin.h"
45 #include "wx/textwrapper.h"
46 #include "wx/modalhook.h"
47
48 #if wxUSE_DISPLAY
49 #include "wx/display.h"
50 #endif
51
52 extern WXDLLEXPORT_DATA(const char) wxDialogNameStr[] = "dialog";
53
54 // ----------------------------------------------------------------------------
55 // XTI
56 // ----------------------------------------------------------------------------
57
58 wxDEFINE_FLAGS( wxDialogStyle )
59 wxBEGIN_FLAGS( wxDialogStyle )
60 // new style border flags, we put them first to
61 // use them for streaming out
62 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
63 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
64 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
65 wxFLAGS_MEMBER(wxBORDER_RAISED)
66 wxFLAGS_MEMBER(wxBORDER_STATIC)
67 wxFLAGS_MEMBER(wxBORDER_NONE)
68
69 // old style border flags
70 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
71 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
72 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
73 wxFLAGS_MEMBER(wxRAISED_BORDER)
74 wxFLAGS_MEMBER(wxSTATIC_BORDER)
75 wxFLAGS_MEMBER(wxNO_BORDER)
76
77 // standard window styles
78 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
79 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
80
81 // dialog styles
82 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
83 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
84 wxFLAGS_MEMBER(wxCAPTION)
85 #if WXWIN_COMPATIBILITY_2_6
86 wxFLAGS_MEMBER(wxTHICK_FRAME)
87 #endif // WXWIN_COMPATIBILITY_2_6
88 wxFLAGS_MEMBER(wxSYSTEM_MENU)
89 wxFLAGS_MEMBER(wxRESIZE_BORDER)
90 #if WXWIN_COMPATIBILITY_2_6
91 wxFLAGS_MEMBER(wxRESIZE_BOX)
92 #endif // WXWIN_COMPATIBILITY_2_6
93 wxFLAGS_MEMBER(wxCLOSE_BOX)
94 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
95 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
96 wxEND_FLAGS( wxDialogStyle )
97
98 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow, "wx/dialog.h")
99
100 wxBEGIN_PROPERTIES_TABLE(wxDialog)
101 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString(), \
102 0 /*flags*/, wxT("Helpstring"), wxT("group"))
103
104 wxPROPERTY_FLAGS( WindowStyle, wxDialogStyle, long, SetWindowStyleFlag, \
105 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
106 wxT("Helpstring"), wxT("group")) // style
107 wxEND_PROPERTIES_TABLE()
108
109 wxEMPTY_HANDLERS_TABLE(wxDialog)
110
111 wxCONSTRUCTOR_6( wxDialog, wxWindow*, Parent, wxWindowID, Id, \
112 wxString, Title, wxPoint, Position, wxSize, Size, long, WindowStyle)
113
114 // ----------------------------------------------------------------------------
115 // wxDialogBase
116 // ----------------------------------------------------------------------------
117
118 BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow)
119 EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton)
120
121 EVT_CLOSE(wxDialogBase::OnCloseWindow)
122
123 EVT_CHAR_HOOK(wxDialogBase::OnCharHook)
124 END_EVENT_TABLE()
125
126 wxDialogLayoutAdapter* wxDialogBase::sm_layoutAdapter = NULL;
127 bool wxDialogBase::sm_layoutAdaptation = false;
128
129 void wxDialogBase::Init()
130 {
131 m_returnCode = 0;
132 m_affirmativeId = wxID_OK;
133 m_escapeId = wxID_ANY;
134 m_layoutAdaptationLevel = 3;
135 m_layoutAdaptationDone = FALSE;
136 m_layoutAdaptationMode = wxDIALOG_ADAPTATION_MODE_DEFAULT;
137
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);
142 }
143
144 wxWindow *wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow *parent) const
145 {
146 if ( !parent )
147 return NULL;
148
149 extern WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete;
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
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 )
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;
180 }
181
182 wxWindow *
183 wxDialogBase::GetParentForModalDialog(wxWindow *parent, long style) const
184 {
185 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
186 // in an unfocused dialog, so try to find a valid parent for it unless we
187 // were explicitly asked not to
188 if ( style & wxDIALOG_NO_PARENT )
189 return NULL;
190
191 // first try the given parent
192 if ( parent )
193 parent = CheckIfCanBeUsedAsParent(wxGetTopLevelParent(parent));
194
195 // then the currently active window
196 if ( !parent )
197 parent = CheckIfCanBeUsedAsParent(
198 wxGetTopLevelParent(wxGetActiveWindow()));
199
200 // and finally the application main window
201 if ( !parent )
202 parent = CheckIfCanBeUsedAsParent(wxTheApp->GetTopWindow());
203
204 return parent;
205 }
206
207 #if wxUSE_STATTEXT
208
209 wxSizer *wxDialogBase::CreateTextSizer(const wxString& message)
210 {
211 wxTextSizerWrapper wrapper(this);
212
213 return CreateTextSizer(message, wrapper);
214 }
215
216 wxSizer *wxDialogBase::CreateTextSizer(const wxString& message,
217 wxTextSizerWrapper& wrapper)
218 {
219 // I admit that this is complete bogus, but it makes
220 // message boxes work for pda screens temporarily..
221 int widthMax = -1;
222 const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
223 if (is_pda)
224 {
225 widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
226 }
227
228 return wrapper.CreateSizer(message, widthMax);
229 }
230
231 #endif // wxUSE_STATTEXT
232
233 wxSizer *wxDialogBase::CreateButtonSizer(long flags)
234 {
235 #ifdef __SMARTPHONE__
236 wxDialog* dialog = (wxDialog*) this;
237 if ( flags & wxOK )
238 dialog->SetLeftMenu(wxID_OK);
239
240 if ( flags & wxCANCEL )
241 dialog->SetRightMenu(wxID_CANCEL);
242
243 if ( flags & wxYES )
244 dialog->SetLeftMenu(wxID_YES);
245
246 if ( flags & wxNO )
247 dialog->SetRightMenu(wxID_NO);
248
249 return NULL;
250 #else // !__SMARTPHONE__
251
252 #if wxUSE_BUTTON
253
254 #ifdef __POCKETPC__
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__
262 {
263 return CreateStdDialogButtonSizer(flags);
264 }
265 #ifdef __POCKETPC__
266 return NULL;
267 #endif // __POCKETPC__
268
269 #else // !wxUSE_BUTTON
270 wxUnusedVar(flags);
271
272 return NULL;
273 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
274
275 #endif // __SMARTPHONE__/!__SMARTPHONE__
276 }
277
278 wxSizer *wxDialogBase::CreateSeparatedSizer(wxSizer *sizer)
279 {
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
289
290 return sizer;
291 }
292
293 wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags)
294 {
295 wxSizer *sizer = CreateButtonSizer(flags);
296 if ( !sizer )
297 return NULL;
298
299 return CreateSeparatedSizer(sizer);
300 }
301
302 #if wxUSE_BUTTON
303
304 wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags )
305 {
306 wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer();
307
308 wxButton *ok = NULL;
309 wxButton *yes = NULL;
310 wxButton *no = NULL;
311
312 if (flags & wxOK)
313 {
314 ok = new wxButton(this, wxID_OK);
315 sizer->AddButton(ok);
316 }
317
318 if (flags & wxCANCEL)
319 {
320 wxButton *cancel = new wxButton(this, wxID_CANCEL);
321 sizer->AddButton(cancel);
322 }
323
324 if (flags & wxYES)
325 {
326 yes = new wxButton(this, wxID_YES);
327 sizer->AddButton(yes);
328 }
329
330 if (flags & wxNO)
331 {
332 no = new wxButton(this, wxID_NO);
333 sizer->AddButton(no);
334 }
335
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
348 if (flags & wxHELP)
349 {
350 wxButton *help = new wxButton(this, wxID_HELP);
351 sizer->AddButton(help);
352 }
353
354 if (flags & wxNO_DEFAULT)
355 {
356 if (no)
357 {
358 no->SetDefault();
359 no->SetFocus();
360 }
361 }
362 else
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 }
375
376 if (flags & wxOK)
377 SetAffirmativeId(wxID_OK);
378 else if (flags & wxYES)
379 SetAffirmativeId(wxID_YES);
380
381 sizer->Realize();
382
383 return sizer;
384 }
385
386 #endif // wxUSE_BUTTON
387
388 // ----------------------------------------------------------------------------
389 // standard buttons handling
390 // ----------------------------------------------------------------------------
391
392 void wxDialogBase::EndDialog(int rc)
393 {
394 if ( IsModal() )
395 EndModal(rc);
396 else
397 Hide();
398 }
399
400 void wxDialogBase::AcceptAndClose()
401 {
402 if ( Validate() && TransferDataFromWindow() )
403 {
404 EndDialog(m_affirmativeId);
405 }
406 }
407
408 void wxDialogBase::SetAffirmativeId(int affirmativeId)
409 {
410 m_affirmativeId = affirmativeId;
411 }
412
413 void wxDialogBase::SetEscapeId(int escapeId)
414 {
415 m_escapeId = escapeId;
416 }
417
418 bool wxDialogBase::EmulateButtonClickIfPresent(int id)
419 {
420 #if wxUSE_BUTTON
421 wxButton *btn = wxDynamicCast(FindWindow(id), wxButton);
422
423 if ( !btn || !btn->IsEnabled() || !btn->IsShown() )
424 return false;
425
426 wxCommandEvent event(wxEVT_BUTTON, id);
427 event.SetEventObject(btn);
428 btn->GetEventHandler()->ProcessEvent(event);
429
430 return true;
431 #else // !wxUSE_BUTTON
432 wxUnusedVar(id);
433 return false;
434 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
435 }
436
437 bool 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
463 bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event)
464 {
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;
470 }
471
472 void wxDialogBase::OnCharHook(wxKeyEvent& event)
473 {
474 if ( IsEscapeKey(event) )
475 {
476 if ( SendCloseButtonClickEvent() )
477 {
478 // Skip the call to event.Skip() below, we did handle this key.
479 return;
480 }
481 }
482
483 event.Skip();
484 }
485
486 void wxDialogBase::OnButton(wxCommandEvent& event)
487 {
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();
497
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 }
509 }
510
511 // ----------------------------------------------------------------------------
512 // compatibility methods for supporting the modality API
513 // ----------------------------------------------------------------------------
514
515 wxDEFINE_EVENT( wxEVT_WINDOW_MODAL_DIALOG_CLOSED , wxWindowModalDialogEvent );
516
517 IMPLEMENT_DYNAMIC_CLASS(wxWindowModalDialogEvent, wxCommandEvent)
518
519 void wxDialogBase::ShowWindowModal ()
520 {
521 ShowModal();
522 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
523 }
524
525 void wxDialogBase::SendWindowModalDialogEvent ( wxEventType type )
526 {
527 wxWindowModalDialogEvent event ( type, GetId());
528 event.SetEventObject(this);
529
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);
537 }
538 }
539
540
541 wxDialogModality wxDialogBase::GetModality() const
542 {
543 return IsModal() ? wxDIALOG_MODALITY_APP_MODAL : wxDIALOG_MODALITY_NONE;
544 }
545
546 // ----------------------------------------------------------------------------
547 // other event handlers
548 // ----------------------------------------------------------------------------
549
550 void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
551 {
552 // We'll send a Cancel message by default, which may close the dialog.
553
554 // Check for looping if the Cancel event handler calls Close().
555 //
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
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 }
576
577 closing.DeleteObject(this);
578 }
579
580 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& event)
581 {
582 #ifndef __WXGTK__
583 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
584 Refresh();
585 #endif
586
587 event.Skip();
588 }
589
590 /// Do the adaptation
591 bool wxDialogBase::DoLayoutAdaptation()
592 {
593 if (GetLayoutAdapter())
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 }
605 else
606 return false;
607 }
608
609 /// Can we do the adaptation?
610 bool 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
619 wxDialogLayoutAdapter* 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
630 IMPLEMENT_CLASS(wxDialogLayoutAdapter, wxObject)
631
632 IMPLEMENT_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
642 bool 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
653 bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog* dialog)
654 {
655 if (dialog->GetSizer())
656 {
657 #if wxUSE_BOOKCTRL
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
693 #endif // wxUSE_BOOKCTRL
694 {
695 #if wxUSE_BUTTON
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 {
721 wxDELETE(buttonSizer);
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);
742 #endif // wxUSE_BUTTON
743 }
744 }
745
746 dialog->SetLayoutAdaptationDone(true);
747 return true;
748 }
749
750 // Create the scrolled window
751 wxScrolledWindow* 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
757 #if wxUSE_BUTTON
758
759 /// Find and remove the button sizer, if any
760 wxSizer* 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
804 bool 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
822 bool 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
831 bool 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
856 #endif // wxUSE_BUTTON
857
858 /// Reparent the controls to the scrolled window
859 void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer)
860 {
861 DoReparentControls(parent, reparentTo, buttonSizer);
862 }
863
864 void 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
888 int 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
894 int 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
902 displaySize = wxGetClientDisplayRect().GetSize();
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).
917 bool 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).
924 bool 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).
931 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow)
932 {
933 wxWindowList windows;
934 windows.Append(scrolledWindow);
935 return DoFitWithScrolling(dialog, windows);
936 }
937
938 bool 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
1000 class wxDialogLayoutAdapterModule: public wxModule
1001 {
1002 DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule)
1003 public:
1004 wxDialogLayoutAdapterModule() {}
1005 virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL); }
1006 virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter); return true; }
1007 };
1008
1009 IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule, wxModule)