1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/checkbox.h"
39 #include "wx/radiobut.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
42 #include "wx/dialog.h"
46 #include "wx/layout.h"
48 #endif // wxUSE_CONSTRAINTS
50 #if wxUSE_DRAG_AND_DROP
52 #endif // wxUSE_DRAG_AND_DROP
55 #include "wx/tooltip.h"
56 #endif // wxUSE_TOOLTIPS
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 int wxWindowBase::ms_lastControlId
= -200;
68 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
75 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
76 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
79 // ============================================================================
80 // implementation of the common functionality of the wxWindow class
81 // ============================================================================
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // the default initialization
88 void wxWindowBase::InitBase()
90 // no window yet, no parent nor children
91 m_parent
= (wxWindow
*)NULL
;
93 m_children
.DeleteContents( FALSE
); // don't auto delete node data
95 // no constraints on the minimal window size
101 // window is created enabled but it's not visible yet
105 // no client data (yet)
107 m_clientDataType
= ClientData_None
;
109 // the default event handler is just this window
110 m_eventHandler
= this;
114 m_windowValidator
= (wxValidator
*) NULL
;
115 #endif // wxUSE_VALIDATORS
117 // use the system default colours
118 wxSystemSettings settings
;
120 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
121 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
122 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
124 #if !defined(__WXMAC__) && !defined(__WXGTK__)
125 m_font
= *wxSWISS_FONT
; // and this?
127 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
134 // an optimization for the event processing: checking this flag is much
135 // faster than using IsKindOf(CLASSINFO(wxWindow))
138 #if wxUSE_CONSTRAINTS
139 // no constraints whatsoever
140 m_constraints
= (wxLayoutConstraints
*) NULL
;
141 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
142 m_windowSizer
= (wxSizer
*) NULL
;
143 m_autoLayout
= FALSE
;
144 #endif // wxUSE_CONSTRAINTS
146 #if wxUSE_DRAG_AND_DROP
147 m_dropTarget
= (wxDropTarget
*)NULL
;
148 #endif // wxUSE_DRAG_AND_DROP
151 m_tooltip
= (wxToolTip
*)NULL
;
152 #endif // wxUSE_TOOLTIPS
155 m_caret
= (wxCaret
*)NULL
;
156 #endif // wxUSE_CARET
159 // common part of window creation process
160 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
162 const wxPoint
& WXUNUSED(pos
),
163 const wxSize
& WXUNUSED(size
),
165 const wxValidator
& validator
,
166 const wxString
& name
)
168 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
169 // member variables - check that it has been called (will catch the case
170 // when a new ctor is added which doesn't call InitWindow)
171 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
173 // generate a new id if the user doesn't care about it
174 m_windowId
= id
== -1 ? NewControlId() : id
;
177 SetWindowStyleFlag(style
);
181 SetValidator(validator
);
182 #endif // wxUSE_VALIDATORS
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
192 wxWindowBase::~wxWindowBase()
194 // FIXME if these 2 cases result from programming errors in the user code
195 // we should probably assert here instead of silently fixing them
197 // Just in case the window has been Closed, but we're then deleting
198 // immediately: don't leave dangling pointers.
199 wxPendingDelete
.DeleteObject(this);
201 // Just in case we've loaded a top-level window via LoadNativeDialog but
202 // we weren't a dialog class
203 wxTopLevelWindows
.DeleteObject(this);
205 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
207 // make sure that there are no dangling pointers left pointing to us
208 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
211 if ( panel
->GetLastFocus() == this )
213 panel
->SetLastFocus((wxWindow
*)NULL
);
220 #endif // wxUSE_CARET
223 if ( m_windowValidator
)
224 delete m_windowValidator
;
225 #endif // wxUSE_VALIDATORS
227 // we only delete object data, not untyped
228 if ( m_clientDataType
== ClientData_Object
)
229 delete m_clientObject
;
231 #if wxUSE_CONSTRAINTS
232 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
233 // at deleted windows as they delete themselves.
234 DeleteRelatedConstraints();
238 // This removes any dangling pointers to this window in other windows'
239 // constraintsInvolvedIn lists.
240 UnsetConstraints(m_constraints
);
241 delete m_constraints
;
242 m_constraints
= NULL
;
246 delete m_windowSizer
;
248 #endif // wxUSE_CONSTRAINTS
250 #if wxUSE_DRAG_AND_DROP
253 #endif // wxUSE_DRAG_AND_DROP
258 #endif // wxUSE_TOOLTIPS
261 bool wxWindowBase::Destroy()
268 bool wxWindowBase::Close(bool force
)
270 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
271 event
.SetEventObject(this);
272 #if WXWIN_COMPATIBILITY
273 event
.SetForce(force
);
274 #endif // WXWIN_COMPATIBILITY
275 event
.SetCanVeto(!force
);
277 // return FALSE if window wasn't closed because the application vetoed the
279 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
282 bool wxWindowBase::DestroyChildren()
284 wxWindowList::Node
*node
;
287 // we iterate until the list becomes empty
288 node
= GetChildren().GetFirst();
292 wxWindow
*child
= node
->GetData();
294 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
298 wxASSERT_MSG( !GetChildren().Find(child
),
299 wxT("child didn't remove itself using RemoveChild()") );
305 // ----------------------------------------------------------------------------
306 // size/position related methods
307 // ----------------------------------------------------------------------------
309 // centre the window with respect to its parent in either (or both) directions
310 void wxWindowBase::Centre(int direction
)
312 int widthParent
, heightParent
;
314 wxWindow
*parent
= GetParent();
318 direction
|= wxCENTRE_ON_SCREEN
;
321 if ( direction
& wxCENTRE_ON_SCREEN
)
323 // centre with respect to the whole screen
324 wxDisplaySize(&widthParent
, &heightParent
);
328 // centre inside the parents rectangle
329 parent
->GetClientSize(&widthParent
, &heightParent
);
333 GetSize(&width
, &height
);
338 if ( direction
& wxHORIZONTAL
)
339 xNew
= (widthParent
- width
)/2;
341 if ( direction
& wxVERTICAL
)
342 yNew
= (heightParent
- height
)/2;
344 // controls are always centered on their parent because it doesn't make
345 // sense to centre them on the screen
346 if ( !(direction
& wxCENTRE_ON_SCREEN
) || wxDynamicCast(this, wxControl
) )
348 // theo nly chance to get this is to have a wxControl without parent
349 wxCHECK_RET( parent
, wxT("a control must have a parent") );
351 // adjust to the parents client area origin
352 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
358 // move the centre of this window to this position
362 // fits the window around the children
363 void wxWindowBase::Fit()
365 if ( GetChildren().GetCount() > 0 )
367 SetClientSize(DoGetBestSize());
369 //else: do nothing if we have no children
372 // return the size best suited for the current window
373 wxSize
wxWindowBase::DoGetBestSize() const
375 if ( GetChildren().GetCount() > 0 )
377 // our minimal acceptable size is such that all our windows fit inside
381 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
383 node
= node
->GetNext() )
385 wxWindow
*win
= node
->GetData();
386 if ( win
->IsTopLevel() )
388 // dialogs and frames lie in different top level windows -
389 // don't deal with them here
394 win
->GetPosition(&wx
, &wy
);
395 win
->GetSize(&ww
, &wh
);
396 if ( wx
+ ww
> maxX
)
398 if ( wy
+ wh
> maxY
)
403 return wxSize(maxX
+ 7, maxY
+ 14);
407 // for a generic window there is no natural best size - just use the
413 // set the min/max size of the window
414 void wxWindowBase::SetSizeHints(int minW
, int minH
,
416 int WXUNUSED(incW
), int WXUNUSED(incH
))
424 // ----------------------------------------------------------------------------
425 // show/hide/enable/disable the window
426 // ----------------------------------------------------------------------------
428 bool wxWindowBase::Show(bool show
)
430 if ( show
!= m_isShown
)
442 bool wxWindowBase::Enable(bool enable
)
444 if ( enable
!= m_isEnabled
)
446 m_isEnabled
= enable
;
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 bool wxWindowBase::IsTopLevel() const
464 // ----------------------------------------------------------------------------
465 // reparenting the window
466 // ----------------------------------------------------------------------------
468 void wxWindowBase::AddChild(wxWindowBase
*child
)
470 wxCHECK_RET( child
, wxT("can't add a NULL child") );
472 GetChildren().Append(child
);
473 child
->SetParent(this);
476 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
478 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
480 GetChildren().DeleteObject(child
);
481 child
->SetParent((wxWindow
*)NULL
);
484 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
486 wxWindow
*oldParent
= GetParent();
487 if ( newParent
== oldParent
)
493 // unlink this window from the existing parent.
496 oldParent
->RemoveChild(this);
500 wxTopLevelWindows
.DeleteObject(this);
503 // add it to the new one
506 newParent
->AddChild(this);
510 wxTopLevelWindows
.Append(this);
516 // ----------------------------------------------------------------------------
517 // event handler stuff
518 // ----------------------------------------------------------------------------
520 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
522 handler
->SetNextHandler(GetEventHandler());
523 SetEventHandler(handler
);
526 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
528 wxEvtHandler
*handlerA
= GetEventHandler();
531 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
532 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
533 SetEventHandler(handlerB
);
537 handlerA
= (wxEvtHandler
*)NULL
;
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
550 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
553 m_backgroundColour
= colour
;
558 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
560 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
563 m_foregroundColour
= colour
;
568 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
570 // don't try to set invalid cursor, always fall back to the default
571 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
573 if ( (wxCursor
&)cursorOk
== m_cursor
)
584 bool wxWindowBase::SetFont(const wxFont
& font
)
586 // don't try to set invalid font, always fall back to the default
587 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
589 if ( (wxFont
&)fontOk
== m_font
)
601 void wxWindowBase::SetCaret(wxCaret
*caret
)
612 wxASSERT_MSG( m_caret
->GetWindow() == this,
613 wxT("caret should be created associated to this window") );
616 #endif // wxUSE_CARET
619 // ----------------------------------------------------------------------------
621 // ----------------------------------------------------------------------------
623 void wxWindowBase::SetValidator(const wxValidator
& validator
)
625 if ( m_windowValidator
)
626 delete m_windowValidator
;
628 m_windowValidator
= (wxValidator
*)validator
.Clone();
630 if ( m_windowValidator
)
631 m_windowValidator
->SetWindow(this) ;
633 #endif // wxUSE_VALIDATORS
635 // ----------------------------------------------------------------------------
636 // update region testing
637 // ----------------------------------------------------------------------------
639 bool wxWindowBase::IsExposed(int x
, int y
) const
641 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
644 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
646 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
649 // ----------------------------------------------------------------------------
650 // find window by id or name
651 // ----------------------------------------------------------------------------
653 wxWindow
*wxWindowBase::FindWindow( long id
)
655 if ( id
== m_windowId
)
656 return (wxWindow
*)this;
658 wxWindowBase
*res
= (wxWindow
*)NULL
;
659 wxWindowList::Node
*node
;
660 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
662 wxWindowBase
*child
= node
->GetData();
663 res
= child
->FindWindow( id
);
666 return (wxWindow
*)res
;
669 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
671 if ( name
== m_windowName
)
672 return (wxWindow
*)this;
674 wxWindowBase
*res
= (wxWindow
*)NULL
;
675 wxWindowList::Node
*node
;
676 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
678 wxWindow
*child
= node
->GetData();
679 res
= child
->FindWindow(name
);
682 return (wxWindow
*)res
;
685 // ----------------------------------------------------------------------------
686 // dialog oriented functions
687 // ----------------------------------------------------------------------------
689 void wxWindowBase::MakeModal(bool modal
)
691 // Disable all other windows
694 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
697 wxWindow
*win
= node
->GetData();
701 node
= node
->GetNext();
706 bool wxWindowBase::Validate()
709 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
711 wxWindowList::Node
*node
;
712 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
714 wxWindowBase
*child
= node
->GetData();
715 wxValidator
*validator
= child
->GetValidator();
716 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
721 if ( recurse
&& !child
->Validate() )
726 #endif // wxUSE_VALIDATORS
731 bool wxWindowBase::TransferDataToWindow()
734 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
736 wxWindowList::Node
*node
;
737 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
739 wxWindowBase
*child
= node
->GetData();
740 wxValidator
*validator
= child
->GetValidator();
741 if ( validator
&& !validator
->TransferToWindow() )
743 wxLogWarning(_("Could not transfer data to window"));
744 wxLog::FlushActive();
751 if ( !child
->TransferDataToWindow() )
753 // warning already given
758 #endif // wxUSE_VALIDATORS
763 bool wxWindowBase::TransferDataFromWindow()
766 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
768 wxWindowList::Node
*node
;
769 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
771 wxWindow
*child
= node
->GetData();
772 wxValidator
*validator
= child
->GetValidator();
773 if ( validator
&& !validator
->TransferFromWindow() )
775 // nop warning here because the application is supposed to give
776 // one itself - we don't know here what might have gone wrongly
783 if ( !child
->TransferDataFromWindow() )
785 // warning already given
790 #endif // wxUSE_VALIDATORS
795 void wxWindowBase::InitDialog()
797 wxInitDialogEvent
event(GetId());
798 event
.SetEventObject( this );
799 GetEventHandler()->ProcessEvent(event
);
802 // ----------------------------------------------------------------------------
804 // ----------------------------------------------------------------------------
808 void wxWindowBase::SetToolTip( const wxString
&tip
)
810 // don't create the new tooltip if we already have one
813 m_tooltip
->SetTip( tip
);
817 SetToolTip( new wxToolTip( tip
) );
820 // setting empty tooltip text does not remove the tooltip any more - use
821 // SetToolTip((wxToolTip *)NULL) for this
824 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
832 #endif // wxUSE_TOOLTIPS
834 // ----------------------------------------------------------------------------
835 // constraints and sizers
836 // ----------------------------------------------------------------------------
838 #if wxUSE_CONSTRAINTS
840 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
844 UnsetConstraints(m_constraints
);
845 delete m_constraints
;
847 m_constraints
= constraints
;
850 // Make sure other windows know they're part of a 'meaningful relationship'
851 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
852 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
853 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
854 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
855 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
856 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
857 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
858 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
859 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
860 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
861 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
862 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
863 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
864 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
865 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
866 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
870 // This removes any dangling pointers to this window in other windows'
871 // constraintsInvolvedIn lists.
872 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
876 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
877 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
878 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
879 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
880 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
881 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
882 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
883 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
884 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
885 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
886 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
887 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
888 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
889 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
890 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
891 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
895 // Back-pointer to other windows we're involved with, so if we delete this
896 // window, we must delete any constraints we're involved with.
897 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
899 if ( !m_constraintsInvolvedIn
)
900 m_constraintsInvolvedIn
= new wxWindowList
;
901 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
902 m_constraintsInvolvedIn
->Append(otherWin
);
905 // REMOVE back-pointer to other windows we're involved with.
906 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
908 if ( m_constraintsInvolvedIn
)
909 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
912 // Reset any constraints that mention this window
913 void wxWindowBase::DeleteRelatedConstraints()
915 if ( m_constraintsInvolvedIn
)
917 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
920 wxWindow
*win
= node
->GetData();
921 wxLayoutConstraints
*constr
= win
->GetConstraints();
923 // Reset any constraints involving this window
926 constr
->left
.ResetIfWin(this);
927 constr
->top
.ResetIfWin(this);
928 constr
->right
.ResetIfWin(this);
929 constr
->bottom
.ResetIfWin(this);
930 constr
->width
.ResetIfWin(this);
931 constr
->height
.ResetIfWin(this);
932 constr
->centreX
.ResetIfWin(this);
933 constr
->centreY
.ResetIfWin(this);
936 wxWindowList::Node
*next
= node
->GetNext();
941 delete m_constraintsInvolvedIn
;
942 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
946 void wxWindowBase::SetSizer(wxSizer
*sizer
)
948 if (m_windowSizer
) delete m_windowSizer
;
950 m_windowSizer
= sizer
;
953 bool wxWindowBase::Layout()
955 // If there is a sizer, use it instead of the constraints
959 GetClientSize(&w
, &h
);
961 GetSizer()->SetDimension( 0, 0, w
, h
);
965 wxLayoutConstraints
*constr
= GetConstraints();
966 bool wasOk
= constr
&& constr
->AreSatisfied();
968 ResetConstraints(); // Mark all constraints as unevaluated
970 // if we're a top level panel (i.e. our parent is frame/dialog), our
971 // own constraints will never be satisfied any more unless we do it
976 while ( noChanges
> 0 )
978 constr
->SatisfyConstraints(this, &noChanges
);
982 DoPhase(1); // Layout children
983 DoPhase(2); // Layout grand children
984 SetConstraintSizes(); // Recursively set the real window sizes
991 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
992 // do a similar thing, but also impose their own 'constraints' and order the
993 // evaluation differently.
994 bool wxWindowBase::LayoutPhase1(int *noChanges
)
996 wxLayoutConstraints
*constr
= GetConstraints();
999 return constr
->SatisfyConstraints(this, noChanges
);
1005 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1015 // Do a phase of evaluating child constraints
1016 bool wxWindowBase::DoPhase(int phase
)
1018 int noIterations
= 0;
1019 int maxIterations
= 500;
1022 wxWindowList succeeded
;
1023 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1027 wxWindowList::Node
*node
= GetChildren().GetFirst();
1030 wxWindow
*child
= node
->GetData();
1031 if ( !child
->IsTopLevel() )
1033 wxLayoutConstraints
*constr
= child
->GetConstraints();
1036 if ( !succeeded
.Find(child
) )
1038 int tempNoChanges
= 0;
1039 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1040 noChanges
+= tempNoChanges
;
1043 succeeded
.Append(child
);
1048 node
= node
->GetNext();
1057 void wxWindowBase::ResetConstraints()
1059 wxLayoutConstraints
*constr
= GetConstraints();
1062 constr
->left
.SetDone(FALSE
);
1063 constr
->top
.SetDone(FALSE
);
1064 constr
->right
.SetDone(FALSE
);
1065 constr
->bottom
.SetDone(FALSE
);
1066 constr
->width
.SetDone(FALSE
);
1067 constr
->height
.SetDone(FALSE
);
1068 constr
->centreX
.SetDone(FALSE
);
1069 constr
->centreY
.SetDone(FALSE
);
1072 wxWindowList::Node
*node
= GetChildren().GetFirst();
1075 wxWindow
*win
= node
->GetData();
1076 if ( !win
->IsTopLevel() )
1077 win
->ResetConstraints();
1078 node
= node
->GetNext();
1082 // Need to distinguish between setting the 'fake' size for windows and sizers,
1083 // and setting the real values.
1084 void wxWindowBase::SetConstraintSizes(bool recurse
)
1086 wxLayoutConstraints
*constr
= GetConstraints();
1087 if ( constr
&& constr
->AreSatisfied() )
1089 int x
= constr
->left
.GetValue();
1090 int y
= constr
->top
.GetValue();
1091 int w
= constr
->width
.GetValue();
1092 int h
= constr
->height
.GetValue();
1094 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1095 (constr
->height
.GetRelationship() != wxAsIs
) )
1097 SetSize(x
, y
, w
, h
);
1101 // If we don't want to resize this window, just move it...
1107 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1108 GetClassInfo()->GetClassName(),
1114 wxWindowList::Node
*node
= GetChildren().GetFirst();
1117 wxWindow
*win
= node
->GetData();
1118 if ( !win
->IsTopLevel() )
1119 win
->SetConstraintSizes();
1120 node
= node
->GetNext();
1125 // Only set the size/position of the constraint (if any)
1126 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1128 wxLayoutConstraints
*constr
= GetConstraints();
1133 constr
->left
.SetValue(x
);
1134 constr
->left
.SetDone(TRUE
);
1138 constr
->top
.SetValue(y
);
1139 constr
->top
.SetDone(TRUE
);
1143 constr
->width
.SetValue(w
);
1144 constr
->width
.SetDone(TRUE
);
1148 constr
->height
.SetValue(h
);
1149 constr
->height
.SetDone(TRUE
);
1154 void wxWindowBase::MoveConstraint(int x
, int y
)
1156 wxLayoutConstraints
*constr
= GetConstraints();
1161 constr
->left
.SetValue(x
);
1162 constr
->left
.SetDone(TRUE
);
1166 constr
->top
.SetValue(y
);
1167 constr
->top
.SetDone(TRUE
);
1172 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1174 wxLayoutConstraints
*constr
= GetConstraints();
1177 *w
= constr
->width
.GetValue();
1178 *h
= constr
->height
.GetValue();
1184 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1186 wxLayoutConstraints
*constr
= GetConstraints();
1189 *w
= constr
->width
.GetValue();
1190 *h
= constr
->height
.GetValue();
1193 GetClientSize(w
, h
);
1196 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1198 wxLayoutConstraints
*constr
= GetConstraints();
1201 *x
= constr
->left
.GetValue();
1202 *y
= constr
->top
.GetValue();
1208 #endif // wxUSE_CONSTRAINTS
1210 // ----------------------------------------------------------------------------
1211 // do Update UI processing for child controls
1212 // ----------------------------------------------------------------------------
1214 // TODO: should this be implemented for the child window rather
1215 // than the parent? Then you can override it e.g. for wxCheckBox
1216 // to do the Right Thing rather than having to assume a fixed number
1217 // of control classes.
1218 void wxWindowBase::UpdateWindowUI()
1220 wxUpdateUIEvent
event(GetId());
1221 event
.m_eventObject
= this;
1223 if ( GetEventHandler()->ProcessEvent(event
) )
1225 if ( event
.GetSetEnabled() )
1226 Enable(event
.GetEnabled());
1228 if ( event
.GetSetText() )
1230 wxControl
*control
= wxDynamicCast(this, wxControl
);
1233 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1235 text
->SetValue(event
.GetText());
1237 control
->SetLabel(event
.GetText());
1242 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1245 if ( event
.GetSetChecked() )
1246 checkbox
->SetValue(event
.GetChecked());
1248 #endif // wxUSE_CHECKBOX
1251 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1254 if ( event
.GetSetChecked() )
1255 radiobtn
->SetValue(event
.GetChecked());
1257 #endif // wxUSE_RADIOBTN
1261 // ----------------------------------------------------------------------------
1262 // dialog units translations
1263 // ----------------------------------------------------------------------------
1265 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1267 int charWidth
= GetCharWidth();
1268 int charHeight
= GetCharHeight();
1269 wxPoint
pt2(-1, -1);
1271 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1273 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1278 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1280 int charWidth
= GetCharWidth();
1281 int charHeight
= GetCharHeight();
1282 wxPoint
pt2(-1, -1);
1284 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1286 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1291 // ----------------------------------------------------------------------------
1293 // ----------------------------------------------------------------------------
1295 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1297 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1298 wxT("can't have both object and void client data") );
1300 if ( m_clientObject
)
1301 delete m_clientObject
;
1303 m_clientObject
= data
;
1304 m_clientDataType
= ClientData_Object
;
1307 wxClientData
*wxWindowBase::DoGetClientObject() const
1309 // it's not an error to call GetClientObject() on a window which doesn't
1310 // have client data at all - NULL will be returned
1311 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1312 wxT("this window doesn't have object client data") );
1314 return m_clientObject
;
1317 void wxWindowBase::DoSetClientData( void *data
)
1319 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1320 wxT("can't have both object and void client data") );
1322 m_clientData
= data
;
1323 m_clientDataType
= ClientData_Void
;
1326 void *wxWindowBase::DoGetClientData() const
1328 // it's not an error to call GetClientData() on a window which doesn't have
1329 // client data at all - NULL will be returned
1330 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1331 wxT("this window doesn't have void client data") );
1333 return m_clientData
;
1336 // ----------------------------------------------------------------------------
1338 // ----------------------------------------------------------------------------
1340 // propagate the colour change event to the subwindows
1341 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1343 wxWindowList::Node
*node
= GetChildren().GetFirst();
1346 // Only propagate to non-top-level windows
1347 wxWindow
*win
= node
->GetData();
1348 if ( !win
->IsTopLevel() )
1350 wxSysColourChangedEvent event2
;
1351 event
.m_eventObject
= win
;
1352 win
->GetEventHandler()->ProcessEvent(event2
);
1355 node
= node
->GetNext();
1359 // the default action is to populate dialog with data when it's created
1360 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1362 TransferDataToWindow();
1365 // ----------------------------------------------------------------------------
1366 // list classes implementation
1367 // ----------------------------------------------------------------------------
1369 void wxWindowListNode::DeleteData()
1371 delete (wxWindow
*)GetData();