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