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