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