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 #if !defined(__WXMAC__) && !defined(__WXGTK__)
123 m_font
= *wxSWISS_FONT
; // and this?
125 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
130 // an optimization for the event processing: checking this flag is much
131 // faster than using IsKindOf(CLASSINFO(wxWindow))
134 #if wxUSE_CONSTRAINTS
135 // no constraints whatsoever
136 m_constraints
= (wxLayoutConstraints
*) NULL
;
137 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
138 m_windowSizer
= (wxSizer
*) NULL
;
139 m_autoLayout
= FALSE
;
140 #endif // wxUSE_CONSTRAINTS
142 #if wxUSE_DRAG_AND_DROP
143 m_dropTarget
= (wxDropTarget
*)NULL
;
144 #endif // wxUSE_DRAG_AND_DROP
147 m_tooltip
= (wxToolTip
*)NULL
;
148 #endif // wxUSE_TOOLTIPS
151 m_caret
= (wxCaret
*)NULL
;
152 #endif // wxUSE_CARET
155 // common part of window creation process
156 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
158 const wxPoint
& WXUNUSED(pos
),
159 const wxSize
& WXUNUSED(size
),
161 const wxValidator
& validator
,
162 const wxString
& name
)
164 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
165 // member variables - check that it has been called (will catch the case
166 // when a new ctor is added which doesn't call InitWindow)
167 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
169 // generate a new id if the user doesn't care about it
170 m_windowId
= id
== -1 ? NewControlId() : id
;
173 SetWindowStyleFlag(style
);
177 SetValidator(validator
);
178 #endif // wxUSE_VALIDATORS
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 wxWindowBase::~wxWindowBase()
190 // FIXME if these 2 cases result from programming errors in the user code
191 // we should probably assert here instead of silently fixing them
193 // Just in case the window has been Closed, but we're then deleting
194 // immediately: don't leave dangling pointers.
195 wxPendingDelete
.DeleteObject(this);
197 // Just in case we've loaded a top-level window via LoadNativeDialog but
198 // we weren't a dialog class
199 wxTopLevelWindows
.DeleteObject(this);
201 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
203 // make sure that there are no dangling pointers left pointing to us
204 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
207 if ( panel
->GetLastFocus() == this )
209 panel
->SetLastFocus((wxWindow
*)NULL
);
216 #endif // wxUSE_CARET
219 if ( m_windowValidator
)
220 delete m_windowValidator
;
221 #endif // wxUSE_VALIDATORS
223 // we only delete object data, not untyped
224 if ( m_clientDataType
== ClientData_Object
)
225 delete m_clientObject
;
227 #if wxUSE_CONSTRAINTS
228 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
229 // at deleted windows as they delete themselves.
230 DeleteRelatedConstraints();
234 // This removes any dangling pointers to this window in other windows'
235 // constraintsInvolvedIn lists.
236 UnsetConstraints(m_constraints
);
237 delete m_constraints
;
238 m_constraints
= NULL
;
242 delete m_windowSizer
;
244 #endif // wxUSE_CONSTRAINTS
246 #if wxUSE_DRAG_AND_DROP
249 #endif // wxUSE_DRAG_AND_DROP
254 #endif // wxUSE_TOOLTIPS
257 bool wxWindowBase::Destroy()
264 bool wxWindowBase::Close(bool force
)
266 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
267 event
.SetEventObject(this);
268 #if WXWIN_COMPATIBILITY
269 event
.SetForce(force
);
270 #endif // WXWIN_COMPATIBILITY
271 event
.SetCanVeto(!force
);
273 // return FALSE if window wasn't closed because the application vetoed the
275 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
278 bool wxWindowBase::DestroyChildren()
280 wxWindowList::Node
*node
;
283 // we iterate until the list becomes empty
284 node
= GetChildren().GetFirst();
288 wxWindow
*child
= node
->GetData();
290 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
294 wxASSERT_MSG( !GetChildren().Find(child
),
295 wxT("child didn't remove itself using RemoveChild()") );
301 // ----------------------------------------------------------------------------
302 // size/position related methods
303 // ----------------------------------------------------------------------------
305 // centre the window with respect to its parent in either (or both) directions
306 void wxWindowBase::Centre(int direction
)
308 int widthParent
, heightParent
;
310 wxWindow
*parent
= GetParent();
314 direction
|= wxCENTRE_ON_SCREEN
;
317 if ( direction
& wxCENTRE_ON_SCREEN
)
319 // centre with respect to the whole screen
320 wxDisplaySize(&widthParent
, &heightParent
);
324 // centre inside the parents rectangle
325 parent
->GetClientSize(&widthParent
, &heightParent
);
329 GetSize(&width
, &height
);
334 if ( direction
& wxHORIZONTAL
)
335 xNew
= (widthParent
- width
)/2;
337 if ( direction
& wxVERTICAL
)
338 yNew
= (heightParent
- height
)/2;
340 // controls are always centered on their parent because it doesn't make
341 // sense to centre them on the screen
342 if ( !(direction
& wxCENTRE_ON_SCREEN
) || wxDynamicCast(this, wxControl
) )
344 // theo nly chance to get this is to have a wxControl without parent
345 wxCHECK_RET( parent
, wxT("a control must have a parent") );
347 // adjust to the parents client area origin
348 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
354 // move the centre of this window to this position
358 // fits the window around the children
359 void wxWindowBase::Fit()
361 if ( GetChildren().GetCount() > 0 )
363 SetClientSize(DoGetBestSize());
365 //else: do nothing if we have no children
368 // return the size best suited for the current window
369 wxSize
wxWindowBase::DoGetBestSize() const
371 if ( GetChildren().GetCount() > 0 )
373 // our minimal acceptable size is such that all our windows fit inside
377 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
379 node
= node
->GetNext() )
381 wxWindow
*win
= node
->GetData();
382 if ( win
->IsTopLevel() )
384 // dialogs and frames lie in different top level windows -
385 // don't deal with them here
390 win
->GetPosition(&wx
, &wy
);
391 win
->GetSize(&ww
, &wh
);
392 if ( wx
+ ww
> maxX
)
394 if ( wy
+ wh
> maxY
)
399 return wxSize(maxX
+ 7, maxY
+ 14);
403 // for a generic window there is no natural best size - just use the
409 // set the min/max size of the window
410 void wxWindowBase::SetSizeHints(int minW
, int minH
,
412 int WXUNUSED(incW
), int WXUNUSED(incH
))
420 // ----------------------------------------------------------------------------
421 // show/hide/enable/disable the window
422 // ----------------------------------------------------------------------------
424 bool wxWindowBase::Show(bool show
)
426 if ( show
!= m_isShown
)
438 bool wxWindowBase::Enable(bool enable
)
440 if ( enable
!= m_isEnabled
)
442 m_isEnabled
= enable
;
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 bool wxWindowBase::IsTopLevel() const
460 // ----------------------------------------------------------------------------
461 // reparenting the window
462 // ----------------------------------------------------------------------------
464 void wxWindowBase::AddChild(wxWindowBase
*child
)
466 wxCHECK_RET( child
, wxT("can't add a NULL child") );
468 GetChildren().Append(child
);
469 child
->SetParent(this);
472 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
474 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
476 GetChildren().DeleteObject(child
);
477 child
->SetParent((wxWindow
*)NULL
);
480 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
482 wxWindow
*oldParent
= GetParent();
483 if ( newParent
== oldParent
)
489 // unlink this window from the existing parent.
492 oldParent
->RemoveChild(this);
496 wxTopLevelWindows
.DeleteObject(this);
499 // add it to the new one
502 newParent
->AddChild(this);
506 wxTopLevelWindows
.Append(this);
512 // ----------------------------------------------------------------------------
513 // event handler stuff
514 // ----------------------------------------------------------------------------
516 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
518 handler
->SetNextHandler(GetEventHandler());
519 SetEventHandler(handler
);
522 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
524 wxEvtHandler
*handlerA
= GetEventHandler();
527 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
528 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
529 SetEventHandler(handlerB
);
533 handlerA
= (wxEvtHandler
*)NULL
;
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
546 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
549 m_backgroundColour
= colour
;
554 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
556 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
559 m_foregroundColour
= colour
;
564 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
566 // don't try to set invalid cursor, always fall back to the default
567 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
569 if ( (wxCursor
&)cursorOk
== m_cursor
)
580 bool wxWindowBase::SetFont(const wxFont
& font
)
582 // don't try to set invalid font, always fall back to the default
583 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
585 if ( (wxFont
&)fontOk
== m_font
)
597 void wxWindowBase::SetCaret(wxCaret
*caret
)
608 wxASSERT_MSG( m_caret
->GetWindow() == this,
609 wxT("caret should be created associated to this window") );
612 #endif // wxUSE_CARET
615 // ----------------------------------------------------------------------------
617 // ----------------------------------------------------------------------------
619 void wxWindowBase::SetValidator(const wxValidator
& validator
)
621 if ( m_windowValidator
)
622 delete m_windowValidator
;
624 m_windowValidator
= (wxValidator
*)validator
.Clone();
626 if ( m_windowValidator
)
627 m_windowValidator
->SetWindow(this) ;
629 #endif // wxUSE_VALIDATORS
631 // ----------------------------------------------------------------------------
632 // update region testing
633 // ----------------------------------------------------------------------------
635 bool wxWindowBase::IsExposed(int x
, int y
) const
637 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
640 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
642 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
645 // ----------------------------------------------------------------------------
646 // find window by id or name
647 // ----------------------------------------------------------------------------
649 wxWindow
*wxWindowBase::FindWindow( long id
)
651 if ( id
== m_windowId
)
652 return (wxWindow
*)this;
654 wxWindowBase
*res
= (wxWindow
*)NULL
;
655 wxWindowList::Node
*node
;
656 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
658 wxWindowBase
*child
= node
->GetData();
659 res
= child
->FindWindow( id
);
662 return (wxWindow
*)res
;
665 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
667 if ( name
== m_windowName
)
668 return (wxWindow
*)this;
670 wxWindowBase
*res
= (wxWindow
*)NULL
;
671 wxWindowList::Node
*node
;
672 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
674 wxWindow
*child
= node
->GetData();
675 res
= child
->FindWindow(name
);
678 return (wxWindow
*)res
;
681 // ----------------------------------------------------------------------------
682 // dialog oriented functions
683 // ----------------------------------------------------------------------------
685 void wxWindowBase::MakeModal(bool modal
)
687 // Disable all other windows
690 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
693 wxWindow
*win
= node
->GetData();
697 node
= node
->GetNext();
702 bool wxWindowBase::Validate()
705 wxWindowList::Node
*node
;
706 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
708 wxWindowBase
*child
= node
->GetData();
709 wxValidator
*validator
= child
->GetValidator();
710 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
715 #endif // wxUSE_VALIDATORS
720 bool wxWindowBase::TransferDataToWindow()
723 wxWindowList::Node
*node
;
724 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
726 wxWindowBase
*child
= node
->GetData();
727 wxValidator
*validator
= child
->GetValidator();
728 if ( validator
&& !validator
->TransferToWindow() )
730 wxLog
*log
= wxLog::GetActiveTarget();
733 wxLogWarning(_("Could not transfer data to window"));
740 #endif // wxUSE_VALIDATORS
745 bool wxWindowBase::TransferDataFromWindow()
748 wxWindowList::Node
*node
;
749 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
751 wxWindow
*child
= node
->GetData();
752 if ( child
->GetValidator() &&
753 !child
->GetValidator()->TransferFromWindow() )
758 #endif // wxUSE_VALIDATORS
763 void wxWindowBase::InitDialog()
765 wxInitDialogEvent
event(GetId());
766 event
.SetEventObject( this );
767 GetEventHandler()->ProcessEvent(event
);
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
776 void wxWindowBase::SetToolTip( const wxString
&tip
)
778 // don't create the new tooltip if we already have one
781 m_tooltip
->SetTip( tip
);
785 SetToolTip( new wxToolTip( tip
) );
788 // setting empty tooltip text does not remove the tooltip any more - use
789 // SetToolTip((wxToolTip *)NULL) for this
792 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
800 #endif // wxUSE_TOOLTIPS
802 // ----------------------------------------------------------------------------
803 // constraints and sizers
804 // ----------------------------------------------------------------------------
806 #if wxUSE_CONSTRAINTS
808 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
812 UnsetConstraints(m_constraints
);
813 delete m_constraints
;
815 m_constraints
= constraints
;
818 // Make sure other windows know they're part of a 'meaningful relationship'
819 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
820 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
821 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
822 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
823 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
824 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
825 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
826 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
827 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
828 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
829 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
830 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
831 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
832 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
833 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
834 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
838 // This removes any dangling pointers to this window in other windows'
839 // constraintsInvolvedIn lists.
840 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
844 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
845 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
846 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
847 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
848 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
849 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
850 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
851 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
852 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
853 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
854 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
855 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
856 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
857 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
858 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
859 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
863 // Back-pointer to other windows we're involved with, so if we delete this
864 // window, we must delete any constraints we're involved with.
865 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
867 if ( !m_constraintsInvolvedIn
)
868 m_constraintsInvolvedIn
= new wxWindowList
;
869 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
870 m_constraintsInvolvedIn
->Append(otherWin
);
873 // REMOVE back-pointer to other windows we're involved with.
874 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
876 if ( m_constraintsInvolvedIn
)
877 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
880 // Reset any constraints that mention this window
881 void wxWindowBase::DeleteRelatedConstraints()
883 if ( m_constraintsInvolvedIn
)
885 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
888 wxWindow
*win
= node
->GetData();
889 wxLayoutConstraints
*constr
= win
->GetConstraints();
891 // Reset any constraints involving this window
894 constr
->left
.ResetIfWin(this);
895 constr
->top
.ResetIfWin(this);
896 constr
->right
.ResetIfWin(this);
897 constr
->bottom
.ResetIfWin(this);
898 constr
->width
.ResetIfWin(this);
899 constr
->height
.ResetIfWin(this);
900 constr
->centreX
.ResetIfWin(this);
901 constr
->centreY
.ResetIfWin(this);
904 wxWindowList::Node
*next
= node
->GetNext();
909 delete m_constraintsInvolvedIn
;
910 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
914 void wxWindowBase::SetSizer(wxSizer
*sizer
)
916 if (m_windowSizer
) delete m_windowSizer
;
918 m_windowSizer
= sizer
;
921 bool wxWindowBase::Layout()
924 GetClientSize(&w
, &h
);
926 // If there is a sizer, use it instead of the constraints
929 GetSizer()->SetDimension( 0, 0, w
, h
);
933 if ( GetConstraints() )
935 GetConstraints()->width
.SetValue(w
);
936 GetConstraints()->height
.SetValue(h
);
939 // Evaluate child constraints
940 ResetConstraints(); // Mark all constraints as unevaluated
941 DoPhase(1); // Just one phase need if no sizers involved
943 SetConstraintSizes(); // Recursively set the real window sizes
949 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
950 // do a similar thing, but also impose their own 'constraints' and order the
951 // evaluation differently.
952 bool wxWindowBase::LayoutPhase1(int *noChanges
)
954 wxLayoutConstraints
*constr
= GetConstraints();
957 return constr
->SatisfyConstraints(this, noChanges
);
963 bool wxWindowBase::LayoutPhase2(int *noChanges
)
973 // Do a phase of evaluating child constraints
974 bool wxWindowBase::DoPhase(int phase
)
976 int noIterations
= 0;
977 int maxIterations
= 500;
980 wxWindowList succeeded
;
981 while ((noChanges
> 0) && (noIterations
< maxIterations
))
985 wxWindowList::Node
*node
= GetChildren().GetFirst();
988 wxWindow
*child
= node
->GetData();
989 if ( !child
->IsTopLevel() )
991 wxLayoutConstraints
*constr
= child
->GetConstraints();
994 if ( !succeeded
.Find(child
) )
996 int tempNoChanges
= 0;
997 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
998 noChanges
+= tempNoChanges
;
1001 succeeded
.Append(child
);
1006 node
= node
->GetNext();
1015 void wxWindowBase::ResetConstraints()
1017 wxLayoutConstraints
*constr
= GetConstraints();
1020 constr
->left
.SetDone(FALSE
);
1021 constr
->top
.SetDone(FALSE
);
1022 constr
->right
.SetDone(FALSE
);
1023 constr
->bottom
.SetDone(FALSE
);
1024 constr
->width
.SetDone(FALSE
);
1025 constr
->height
.SetDone(FALSE
);
1026 constr
->centreX
.SetDone(FALSE
);
1027 constr
->centreY
.SetDone(FALSE
);
1029 wxWindowList::Node
*node
= GetChildren().GetFirst();
1032 wxWindow
*win
= node
->GetData();
1033 if ( !win
->IsTopLevel() )
1034 win
->ResetConstraints();
1035 node
= node
->GetNext();
1039 // Need to distinguish between setting the 'fake' size for windows and sizers,
1040 // and setting the real values.
1041 void wxWindowBase::SetConstraintSizes(bool recurse
)
1043 wxLayoutConstraints
*constr
= GetConstraints();
1044 if ( constr
&& constr
->left
.GetDone() && constr
->right
.GetDone( ) &&
1045 constr
->width
.GetDone() && constr
->height
.GetDone())
1047 int x
= constr
->left
.GetValue();
1048 int y
= constr
->top
.GetValue();
1049 int w
= constr
->width
.GetValue();
1050 int h
= constr
->height
.GetValue();
1052 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1053 (constr
->height
.GetRelationship() != wxAsIs
) )
1055 SetSize(x
, y
, w
, h
);
1059 // If we don't want to resize this window, just move it...
1065 wxChar
*windowClass
= GetClassInfo()->GetClassName();
1068 if ( GetName() == wxT("") )
1069 winName
= wxT("unnamed");
1071 winName
= GetName();
1072 wxLogDebug( wxT("Constraint(s) not satisfied for window of type %s, name %s:\n"),
1073 (const wxChar
*)windowClass
,
1074 (const wxChar
*)winName
);
1075 if ( !constr
->left
.GetDone()) wxLogDebug( wxT(" unsatisfied 'left' constraint.\n") );
1076 if ( !constr
->right
.GetDone()) wxLogDebug( wxT(" unsatisfied 'right' constraint.\n") );
1077 if ( !constr
->width
.GetDone()) wxLogDebug( wxT(" unsatisfied 'width' constraint.\n") );
1078 if ( !constr
->height
.GetDone()) wxLogDebug( wxT(" unsatisfied 'height' constraint.\n") );
1079 wxLogDebug( wxT("Please check constraints: try adding AsIs() constraints.\n") );
1084 wxWindowList::Node
*node
= GetChildren().GetFirst();
1087 wxWindow
*win
= node
->GetData();
1088 if ( !win
->IsTopLevel() )
1089 win
->SetConstraintSizes();
1090 node
= node
->GetNext();
1095 // Only set the size/position of the constraint (if any)
1096 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1098 wxLayoutConstraints
*constr
= GetConstraints();
1103 constr
->left
.SetValue(x
);
1104 constr
->left
.SetDone(TRUE
);
1108 constr
->top
.SetValue(y
);
1109 constr
->top
.SetDone(TRUE
);
1113 constr
->width
.SetValue(w
);
1114 constr
->width
.SetDone(TRUE
);
1118 constr
->height
.SetValue(h
);
1119 constr
->height
.SetDone(TRUE
);
1124 void wxWindowBase::MoveConstraint(int x
, int y
)
1126 wxLayoutConstraints
*constr
= GetConstraints();
1131 constr
->left
.SetValue(x
);
1132 constr
->left
.SetDone(TRUE
);
1136 constr
->top
.SetValue(y
);
1137 constr
->top
.SetDone(TRUE
);
1142 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1144 wxLayoutConstraints
*constr
= GetConstraints();
1147 *w
= constr
->width
.GetValue();
1148 *h
= constr
->height
.GetValue();
1154 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1156 wxLayoutConstraints
*constr
= GetConstraints();
1159 *w
= constr
->width
.GetValue();
1160 *h
= constr
->height
.GetValue();
1163 GetClientSize(w
, h
);
1166 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1168 wxLayoutConstraints
*constr
= GetConstraints();
1171 *x
= constr
->left
.GetValue();
1172 *y
= constr
->top
.GetValue();
1178 #endif // wxUSE_CONSTRAINTS
1180 // ----------------------------------------------------------------------------
1181 // do Update UI processing for child controls
1182 // ----------------------------------------------------------------------------
1184 // TODO: should this be implemented for the child window rather
1185 // than the parent? Then you can override it e.g. for wxCheckBox
1186 // to do the Right Thing rather than having to assume a fixed number
1187 // of control classes.
1188 void wxWindowBase::UpdateWindowUI()
1190 wxUpdateUIEvent
event(GetId());
1191 event
.m_eventObject
= this;
1193 if ( GetEventHandler()->ProcessEvent(event
) )
1195 if ( event
.GetSetEnabled() )
1196 Enable(event
.GetEnabled());
1198 if ( event
.GetSetText() )
1200 wxControl
*control
= wxDynamicCast(this, wxControl
);
1203 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1205 text
->SetValue(event
.GetText());
1207 control
->SetLabel(event
.GetText());
1212 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1215 if ( event
.GetSetChecked() )
1216 checkbox
->SetValue(event
.GetChecked());
1218 #endif // wxUSE_CHECKBOX
1220 #if wxUSE_RADIOBUTTON
1221 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1224 if ( event
.GetSetChecked() )
1225 radiobtn
->SetValue(event
.GetChecked());
1227 #endif // wxUSE_RADIOBUTTON
1231 // ----------------------------------------------------------------------------
1232 // dialog units translations
1233 // ----------------------------------------------------------------------------
1235 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1237 int charWidth
= GetCharWidth();
1238 int charHeight
= GetCharHeight();
1239 wxPoint
pt2(-1, -1);
1241 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1243 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1248 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1250 int charWidth
= GetCharWidth();
1251 int charHeight
= GetCharHeight();
1252 wxPoint
pt2(-1, -1);
1254 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1256 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1261 // ----------------------------------------------------------------------------
1263 // ----------------------------------------------------------------------------
1265 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1267 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1268 wxT("can't have both object and void client data") );
1270 if ( m_clientObject
)
1271 delete m_clientObject
;
1273 m_clientObject
= data
;
1274 m_clientDataType
= ClientData_Object
;
1277 wxClientData
*wxWindowBase::DoGetClientObject() const
1279 wxASSERT_MSG( m_clientDataType
== ClientData_Object
,
1280 wxT("this window doesn't have object client data") );
1282 return m_clientObject
;
1285 void wxWindowBase::DoSetClientData( void *data
)
1287 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1288 wxT("can't have both object and void client data") );
1290 m_clientData
= data
;
1291 m_clientDataType
= ClientData_Void
;
1294 void *wxWindowBase::DoGetClientData() const
1296 wxASSERT_MSG( m_clientDataType
== ClientData_Void
,
1297 wxT("this window doesn't have void client data") );
1299 return m_clientData
;
1302 // ----------------------------------------------------------------------------
1304 // ----------------------------------------------------------------------------
1306 // propagate the colour change event to the subwindows
1307 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1309 wxWindowList::Node
*node
= GetChildren().GetFirst();
1312 // Only propagate to non-top-level windows
1313 wxWindow
*win
= node
->GetData();
1314 if ( !win
->IsTopLevel() )
1316 wxSysColourChangedEvent event2
;
1317 event
.m_eventObject
= win
;
1318 win
->GetEventHandler()->ProcessEvent(event2
);
1321 node
= node
->GetNext();
1325 // the default action is to populate dialog with data when it's created
1326 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1328 TransferDataToWindow();
1331 // ----------------------------------------------------------------------------
1332 // list classes implementation
1333 // ----------------------------------------------------------------------------
1335 void wxWindowListNode::DeleteData()
1337 delete (wxWindow
*)GetData();