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