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?
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
),
162 # if defined(__VISAGECPP__)
163 const wxValidator
* validator
,
165 const wxValidator
& validator
,
168 const wxString
& name
)
170 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
171 // member variables - check that it has been called (will catch the case
172 // when a new ctor is added which doesn't call InitWindow)
173 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
175 // generate a new id if the user doesn't care about it
176 m_windowId
= id
== -1 ? NewControlId() : id
;
179 SetWindowStyleFlag(style
);
181 SetValidator(validator
);
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 wxWindowBase::~wxWindowBase()
193 // FIXME if these 2 cases result from programming errors in the user code
194 // we should probably assert here instead of silently fixing them
196 // Just in case the window has been Closed, but we're then deleting
197 // immediately: don't leave dangling pointers.
198 wxPendingDelete
.DeleteObject(this);
200 // Just in case we've loaded a top-level window via LoadNativeDialog but
201 // we weren't a dialog class
202 wxTopLevelWindows
.DeleteObject(this);
204 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
206 // make sure that there are no dangling pointers left pointing to us
207 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
210 if ( panel
->GetLastFocus() == this )
212 panel
->SetLastFocus((wxWindow
*)NULL
);
219 #endif // wxUSE_CARET
222 if ( m_windowValidator
)
223 delete m_windowValidator
;
224 #endif // wxUSE_VALIDATORS
226 // we only delete object data, not untyped
227 if ( m_clientDataType
== ClientData_Object
)
228 delete m_clientObject
;
230 #if wxUSE_CONSTRAINTS
231 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
232 // at deleted windows as they delete themselves.
233 DeleteRelatedConstraints();
237 // This removes any dangling pointers to this window in other windows'
238 // constraintsInvolvedIn lists.
239 UnsetConstraints(m_constraints
);
240 delete m_constraints
;
241 m_constraints
= NULL
;
245 delete m_windowSizer
;
247 #endif // wxUSE_CONSTRAINTS
249 #if wxUSE_DRAG_AND_DROP
252 #endif // wxUSE_DRAG_AND_DROP
257 #endif // wxUSE_TOOLTIPS
260 bool wxWindowBase::Destroy()
267 bool wxWindowBase::Close(bool force
)
269 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
270 event
.SetEventObject(this);
271 #if WXWIN_COMPATIBILITY
272 event
.SetForce(force
);
273 #endif // WXWIN_COMPATIBILITY
274 event
.SetCanVeto(!force
);
276 // return FALSE if window wasn't closed because the application vetoed the
278 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
281 bool wxWindowBase::DestroyChildren()
283 wxWindowList::Node
*node
;
286 // we iterate until the list becomes empty
287 node
= GetChildren().GetFirst();
291 wxWindow
*child
= node
->GetData();
293 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
297 wxASSERT_MSG( !GetChildren().Find(child
),
298 wxT("child didn't remove itself using RemoveChild()") );
304 // ----------------------------------------------------------------------------
305 // centre/fit the window
306 // ----------------------------------------------------------------------------
308 // centre the window with respect to its parent in either (or both) directions
309 void wxWindowBase::Centre(int direction
)
311 int widthParent
, heightParent
;
313 wxWindow
*parent
= GetParent();
317 direction
|= wxCENTRE_ON_SCREEN
;
320 if ( direction
& wxCENTRE_ON_SCREEN
)
322 // centre with respect to the whole screen
323 wxDisplaySize(&widthParent
, &heightParent
);
327 // centre inside the parents rectangle
328 parent
->GetClientSize(&widthParent
, &heightParent
);
332 GetSize(&width
, &height
);
337 if ( direction
& wxHORIZONTAL
)
338 xNew
= (widthParent
- width
)/2;
340 if ( direction
& wxVERTICAL
)
341 yNew
= (heightParent
- height
)/2;
343 // controls are always centered on their parent because it doesn't make
344 // sense to centre them on the screen
345 if ( !(direction
& wxCENTRE_ON_SCREEN
) || wxDynamicCast(this, wxControl
) )
347 // theo nly chance to get this is to have a wxControl without parent
348 wxCHECK_RET( parent
, wxT("a control must have a parent") );
350 // adjust to the parents client area origin
351 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
357 // move the centre of this window to this position (not the upper left
358 // corner as it was done before)
359 Move(xNew
- width
/ 2, yNew
- height
/ 2);
362 // fits the window around the children
363 void wxWindowBase::Fit()
368 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
370 node
= node
->GetNext() )
372 wxWindow
*win
= node
->GetData();
373 if ( win
->IsTopLevel() )
375 // dialogs and frames lie in different top level windows - don't
376 // deal with them here
381 win
->GetPosition(&wx
, &wy
);
382 win
->GetSize(&ww
, &wh
);
383 if ( wx
+ ww
> maxX
)
385 if ( wy
+ wh
> maxY
)
390 SetClientSize(maxX
+ 7, maxY
+ 14);
393 // set the min/max size of the window
395 void wxWindowBase::SetSizeHints(int minW
, int minH
,
397 int WXUNUSED(incW
), int WXUNUSED(incH
))
405 // ----------------------------------------------------------------------------
406 // show/hide/enable/disable the window
407 // ----------------------------------------------------------------------------
409 bool wxWindowBase::Show(bool show
)
411 if ( show
!= m_isShown
)
423 bool wxWindowBase::Enable(bool enable
)
425 if ( enable
!= m_isEnabled
)
427 m_isEnabled
= enable
;
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 bool wxWindowBase::IsTopLevel() const
445 // ----------------------------------------------------------------------------
446 // reparenting the window
447 // ----------------------------------------------------------------------------
449 void wxWindowBase::AddChild(wxWindowBase
*child
)
451 wxCHECK_RET( child
, wxT("can't add a NULL child") );
453 GetChildren().Append(child
);
454 child
->SetParent(this);
457 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
459 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
461 GetChildren().DeleteObject(child
);
462 child
->SetParent((wxWindow
*)NULL
);
465 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
467 wxWindow
*oldParent
= GetParent();
468 if ( newParent
== oldParent
)
474 // unlink this window from the existing parent.
477 oldParent
->RemoveChild(this);
481 wxTopLevelWindows
.DeleteObject(this);
484 // add it to the new one
487 newParent
->AddChild(this);
491 wxTopLevelWindows
.Append(this);
497 // ----------------------------------------------------------------------------
498 // event handler stuff
499 // ----------------------------------------------------------------------------
501 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
503 handler
->SetNextHandler(GetEventHandler());
504 SetEventHandler(handler
);
507 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
509 wxEvtHandler
*handlerA
= GetEventHandler();
512 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
513 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
514 SetEventHandler(handlerB
);
518 handlerA
= (wxEvtHandler
*)NULL
;
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
531 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
534 m_backgroundColour
= colour
;
539 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
541 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
544 m_foregroundColour
= colour
;
549 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
551 // don't try to set invalid cursor, always fall back to the default
552 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
554 if ( (wxCursor
&)cursorOk
== m_cursor
)
565 bool wxWindowBase::SetFont(const wxFont
& font
)
567 // don't try to set invalid font, always fall back to the default
568 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
570 if ( (wxFont
&)fontOk
== m_font
)
582 void wxWindowBase::SetCaret(wxCaret
*caret
)
593 wxASSERT_MSG( m_caret
->GetWindow() == this,
594 wxT("caret should be created associated to this window") );
597 #endif // wxUSE_CARET
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 # if defined(__VISAGECPP__)
605 void wxWindowBase::SetValidator(const wxValidator
* validator
)
607 if ( m_windowValidator
)
608 delete m_windowValidator
;
610 m_windowValidator
= (wxValidator
*)validator
->Clone();
612 if ( m_windowValidator
)
613 m_windowValidator
->SetWindow(this) ;
616 void wxWindowBase::SetValidator(const wxValidator
& validator
)
618 if ( m_windowValidator
)
619 delete m_windowValidator
;
621 m_windowValidator
= (wxValidator
*)validator
.Clone();
623 if ( m_windowValidator
)
624 m_windowValidator
->SetWindow(this) ;
626 # endif // __VISAGECPP__
627 #endif // wxUSE_VALIDATORS
629 // ----------------------------------------------------------------------------
630 // update region testing
631 // ----------------------------------------------------------------------------
633 bool wxWindowBase::IsExposed(int x
, int y
) const
635 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
638 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
640 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
643 // ----------------------------------------------------------------------------
644 // find window by id or name
645 // ----------------------------------------------------------------------------
647 wxWindow
*wxWindowBase::FindWindow( long id
)
649 if ( id
== m_windowId
)
650 return (wxWindow
*)this;
652 wxWindowBase
*res
= (wxWindow
*)NULL
;
653 wxWindowList::Node
*node
;
654 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
656 wxWindowBase
*child
= node
->GetData();
657 res
= child
->FindWindow( id
);
660 return (wxWindow
*)res
;
663 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
665 if ( name
== m_windowName
)
666 return (wxWindow
*)this;
668 wxWindowBase
*res
= (wxWindow
*)NULL
;
669 wxWindowList::Node
*node
;
670 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
672 wxWindow
*child
= node
->GetData();
673 res
= child
->FindWindow(name
);
676 return (wxWindow
*)res
;
679 // ----------------------------------------------------------------------------
680 // dialog oriented functions
681 // ----------------------------------------------------------------------------
683 void wxWindowBase::MakeModal(bool modal
)
685 // Disable all other windows
688 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
691 wxWindow
*win
= node
->GetData();
695 node
= node
->GetNext();
700 bool wxWindowBase::Validate()
703 wxWindowList::Node
*node
;
704 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
706 wxWindowBase
*child
= node
->GetData();
707 wxValidator
*validator
= child
->GetValidator();
708 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
713 #endif // wxUSE_VALIDATORS
718 bool wxWindowBase::TransferDataToWindow()
721 wxWindowList::Node
*node
;
722 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
724 wxWindowBase
*child
= node
->GetData();
725 wxValidator
*validator
= child
->GetValidator();
726 if ( validator
&& !validator
->TransferToWindow() )
728 wxLog
*log
= wxLog::GetActiveTarget();
731 wxLogWarning(_("Could not transfer data to window"));
738 #endif // wxUSE_VALIDATORS
743 bool wxWindowBase::TransferDataFromWindow()
746 wxWindowList::Node
*node
;
747 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
749 wxWindow
*child
= node
->GetData();
750 if ( child
->GetValidator() &&
751 !child
->GetValidator()->TransferFromWindow() )
756 #endif // wxUSE_VALIDATORS
761 void wxWindowBase::InitDialog()
763 wxInitDialogEvent
event(GetId());
764 event
.SetEventObject( this );
765 GetEventHandler()->ProcessEvent(event
);
768 // ----------------------------------------------------------------------------
770 // ----------------------------------------------------------------------------
774 void wxWindowBase::SetToolTip( const wxString
&tip
)
776 // don't create the new tooltip if we already have one
779 m_tooltip
->SetTip( tip
);
783 SetToolTip( new wxToolTip( tip
) );
786 // setting empty tooltip text does not remove the tooltip any more - use
787 // SetToolTip((wxToolTip *)NULL) for this
790 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
798 #endif // wxUSE_TOOLTIPS
800 // ----------------------------------------------------------------------------
801 // constraints and sizers
802 // ----------------------------------------------------------------------------
804 #if wxUSE_CONSTRAINTS
806 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
810 UnsetConstraints(m_constraints
);
811 delete m_constraints
;
813 m_constraints
= constraints
;
816 // Make sure other windows know they're part of a 'meaningful relationship'
817 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
818 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
819 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
820 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
821 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
822 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
823 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
824 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
825 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
826 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
827 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
828 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
829 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
830 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
831 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
832 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
836 // This removes any dangling pointers to this window in other windows'
837 // constraintsInvolvedIn lists.
838 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
842 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
843 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
844 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
845 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
846 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
847 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
848 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
849 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
850 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
851 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
852 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
853 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
854 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
855 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
856 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
857 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
861 // Back-pointer to other windows we're involved with, so if we delete this
862 // window, we must delete any constraints we're involved with.
863 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
865 if ( !m_constraintsInvolvedIn
)
866 m_constraintsInvolvedIn
= new wxWindowList
;
867 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
868 m_constraintsInvolvedIn
->Append(otherWin
);
871 // REMOVE back-pointer to other windows we're involved with.
872 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
874 if ( m_constraintsInvolvedIn
)
875 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
878 // Reset any constraints that mention this window
879 void wxWindowBase::DeleteRelatedConstraints()
881 if ( m_constraintsInvolvedIn
)
883 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
886 wxWindow
*win
= node
->GetData();
887 wxLayoutConstraints
*constr
= win
->GetConstraints();
889 // Reset any constraints involving this window
892 constr
->left
.ResetIfWin(this);
893 constr
->top
.ResetIfWin(this);
894 constr
->right
.ResetIfWin(this);
895 constr
->bottom
.ResetIfWin(this);
896 constr
->width
.ResetIfWin(this);
897 constr
->height
.ResetIfWin(this);
898 constr
->centreX
.ResetIfWin(this);
899 constr
->centreY
.ResetIfWin(this);
902 wxWindowList::Node
*next
= node
->GetNext();
907 delete m_constraintsInvolvedIn
;
908 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
912 void wxWindowBase::SetSizer(wxSizer
*sizer
)
914 if (m_windowSizer
) delete m_windowSizer
;
916 m_windowSizer
= sizer
;
919 bool wxWindowBase::Layout()
922 GetClientSize(&w
, &h
);
924 // If there is a sizer, use it instead of the constraints
927 GetSizer()->SetDimension( 0, 0, w
, h
);
931 if ( GetConstraints() )
933 GetConstraints()->width
.SetValue(w
);
934 GetConstraints()->height
.SetValue(h
);
937 // Evaluate child constraints
938 ResetConstraints(); // Mark all constraints as unevaluated
939 DoPhase(1); // Just one phase need if no sizers involved
941 SetConstraintSizes(); // Recursively set the real window sizes
947 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
948 // do a similar thing, but also impose their own 'constraints' and order the
949 // evaluation differently.
950 bool wxWindowBase::LayoutPhase1(int *noChanges
)
952 wxLayoutConstraints
*constr
= GetConstraints();
955 return constr
->SatisfyConstraints(this, noChanges
);
961 bool wxWindowBase::LayoutPhase2(int *noChanges
)
971 // Do a phase of evaluating child constraints
972 bool wxWindowBase::DoPhase(int phase
)
974 int noIterations
= 0;
975 int maxIterations
= 500;
978 wxWindowList succeeded
;
979 while ((noChanges
> 0) && (noIterations
< maxIterations
))
983 wxWindowList::Node
*node
= GetChildren().GetFirst();
986 wxWindow
*child
= node
->GetData();
987 if ( !child
->IsTopLevel() )
989 wxLayoutConstraints
*constr
= child
->GetConstraints();
992 if ( !succeeded
.Find(child
) )
994 int tempNoChanges
= 0;
995 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
996 noChanges
+= tempNoChanges
;
999 succeeded
.Append(child
);
1004 node
= node
->GetNext();
1013 void wxWindowBase::ResetConstraints()
1015 wxLayoutConstraints
*constr
= GetConstraints();
1018 constr
->left
.SetDone(FALSE
);
1019 constr
->top
.SetDone(FALSE
);
1020 constr
->right
.SetDone(FALSE
);
1021 constr
->bottom
.SetDone(FALSE
);
1022 constr
->width
.SetDone(FALSE
);
1023 constr
->height
.SetDone(FALSE
);
1024 constr
->centreX
.SetDone(FALSE
);
1025 constr
->centreY
.SetDone(FALSE
);
1027 wxWindowList::Node
*node
= GetChildren().GetFirst();
1030 wxWindow
*win
= node
->GetData();
1031 if ( !win
->IsTopLevel() )
1032 win
->ResetConstraints();
1033 node
= node
->GetNext();
1037 // Need to distinguish between setting the 'fake' size for windows and sizers,
1038 // and setting the real values.
1039 void wxWindowBase::SetConstraintSizes(bool recurse
)
1041 wxLayoutConstraints
*constr
= GetConstraints();
1042 if ( constr
&& constr
->left
.GetDone() && constr
->right
.GetDone( ) &&
1043 constr
->width
.GetDone() && constr
->height
.GetDone())
1045 int x
= constr
->left
.GetValue();
1046 int y
= constr
->top
.GetValue();
1047 int w
= constr
->width
.GetValue();
1048 int h
= constr
->height
.GetValue();
1050 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1051 (constr
->height
.GetRelationship() != wxAsIs
) )
1053 SetSize(x
, y
, w
, h
);
1057 // If we don't want to resize this window, just move it...
1063 wxChar
*windowClass
= GetClassInfo()->GetClassName();
1066 if ( GetName() == wxT("") )
1067 winName
= wxT("unnamed");
1069 winName
= GetName();
1070 wxLogDebug( wxT("Constraint(s) not satisfied for window of type %s, name %s:\n"),
1071 (const wxChar
*)windowClass
,
1072 (const wxChar
*)winName
);
1073 if ( !constr
->left
.GetDone()) wxLogDebug( wxT(" unsatisfied 'left' constraint.\n") );
1074 if ( !constr
->right
.GetDone()) wxLogDebug( wxT(" unsatisfied 'right' constraint.\n") );
1075 if ( !constr
->width
.GetDone()) wxLogDebug( wxT(" unsatisfied 'width' constraint.\n") );
1076 if ( !constr
->height
.GetDone()) wxLogDebug( wxT(" unsatisfied 'height' constraint.\n") );
1077 wxLogDebug( wxT("Please check constraints: try adding AsIs() constraints.\n") );
1082 wxWindowList::Node
*node
= GetChildren().GetFirst();
1085 wxWindow
*win
= node
->GetData();
1086 if ( !win
->IsTopLevel() )
1087 win
->SetConstraintSizes();
1088 node
= node
->GetNext();
1093 // Only set the size/position of the constraint (if any)
1094 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1096 wxLayoutConstraints
*constr
= GetConstraints();
1101 constr
->left
.SetValue(x
);
1102 constr
->left
.SetDone(TRUE
);
1106 constr
->top
.SetValue(y
);
1107 constr
->top
.SetDone(TRUE
);
1111 constr
->width
.SetValue(w
);
1112 constr
->width
.SetDone(TRUE
);
1116 constr
->height
.SetValue(h
);
1117 constr
->height
.SetDone(TRUE
);
1122 void wxWindowBase::MoveConstraint(int x
, int y
)
1124 wxLayoutConstraints
*constr
= GetConstraints();
1129 constr
->left
.SetValue(x
);
1130 constr
->left
.SetDone(TRUE
);
1134 constr
->top
.SetValue(y
);
1135 constr
->top
.SetDone(TRUE
);
1140 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1142 wxLayoutConstraints
*constr
= GetConstraints();
1145 *w
= constr
->width
.GetValue();
1146 *h
= constr
->height
.GetValue();
1152 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1154 wxLayoutConstraints
*constr
= GetConstraints();
1157 *w
= constr
->width
.GetValue();
1158 *h
= constr
->height
.GetValue();
1161 GetClientSize(w
, h
);
1164 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1166 wxLayoutConstraints
*constr
= GetConstraints();
1169 *x
= constr
->left
.GetValue();
1170 *y
= constr
->top
.GetValue();
1176 #endif // wxUSE_CONSTRAINTS
1178 // ----------------------------------------------------------------------------
1179 // do Update UI processing for child controls
1180 // ----------------------------------------------------------------------------
1182 // TODO: should this be implemented for the child window rather
1183 // than the parent? Then you can override it e.g. for wxCheckBox
1184 // to do the Right Thing rather than having to assume a fixed number
1185 // of control classes.
1186 void wxWindowBase::UpdateWindowUI()
1188 wxUpdateUIEvent
event(GetId());
1189 event
.m_eventObject
= this;
1191 if ( GetEventHandler()->ProcessEvent(event
) )
1193 if ( event
.GetSetEnabled() )
1194 Enable(event
.GetEnabled());
1196 if ( event
.GetSetText() )
1198 wxControl
*control
= wxDynamicCast(this, wxControl
);
1201 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1203 text
->SetValue(event
.GetText());
1205 control
->SetLabel(event
.GetText());
1210 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1213 if ( event
.GetSetChecked() )
1214 checkbox
->SetValue(event
.GetChecked());
1216 #endif // wxUSE_CHECKBOX
1218 #if wxUSE_RADIOBUTTON
1219 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1222 if ( event
.GetSetChecked() )
1223 radiobtn
->SetValue(event
.GetChecked());
1225 #endif // wxUSE_RADIOBUTTON
1229 // ----------------------------------------------------------------------------
1230 // dialog units translations
1231 // ----------------------------------------------------------------------------
1233 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1235 int charWidth
= GetCharWidth();
1236 int charHeight
= GetCharHeight();
1237 wxPoint
pt2(-1, -1);
1239 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1241 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1246 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1248 int charWidth
= GetCharWidth();
1249 int charHeight
= GetCharHeight();
1250 wxPoint
pt2(-1, -1);
1252 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1254 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1259 // ----------------------------------------------------------------------------
1261 // ----------------------------------------------------------------------------
1263 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1265 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1266 wxT("can't have both object and void client data") );
1268 if ( m_clientObject
)
1269 delete m_clientObject
;
1271 m_clientObject
= data
;
1272 m_clientDataType
= ClientData_Object
;
1275 wxClientData
*wxWindowBase::DoGetClientObject() const
1277 wxASSERT_MSG( m_clientDataType
== ClientData_Object
,
1278 wxT("this window doesn't have object client data") );
1280 return m_clientObject
;
1283 void wxWindowBase::DoSetClientData( void *data
)
1285 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1286 wxT("can't have both object and void client data") );
1288 m_clientData
= data
;
1289 m_clientDataType
= ClientData_Void
;
1292 void *wxWindowBase::DoGetClientData() const
1294 wxASSERT_MSG( m_clientDataType
== ClientData_Void
,
1295 wxT("this window doesn't have void client data") );
1297 return m_clientData
;
1300 // ----------------------------------------------------------------------------
1302 // ----------------------------------------------------------------------------
1304 // propagate the colour change event to the subwindows
1305 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1307 wxWindowList::Node
*node
= GetChildren().GetFirst();
1310 // Only propagate to non-top-level windows
1311 wxWindow
*win
= node
->GetData();
1312 if ( !win
->IsTopLevel() )
1314 wxSysColourChangedEvent event2
;
1315 event
.m_eventObject
= win
;
1316 win
->GetEventHandler()->ProcessEvent(event2
);
1319 node
= node
->GetNext();
1323 // the default action is to populate dialog with data when it's created
1324 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1326 TransferDataToWindow();
1329 // ----------------------------------------------------------------------------
1330 // list classes implementation
1331 // ----------------------------------------------------------------------------
1333 void wxWindowListNode::DeleteData()
1335 delete (wxWindow
*)GetData();