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 // size/position related methods
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
361 // fits the window around the children
362 void wxWindowBase::Fit()
364 if ( GetChildren().GetCount() > 0 )
366 SetClientSize(DoGetBestSize());
368 //else: do nothing if we have no children
371 // return the size best suited for the current window
372 wxSize
wxWindowBase::DoGetBestSize() const
374 if ( GetChildren().GetCount() > 0 )
376 // our minimal acceptable size is such that all our windows fit inside
380 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
382 node
= node
->GetNext() )
384 wxWindow
*win
= node
->GetData();
385 if ( win
->IsTopLevel() )
387 // dialogs and frames lie in different top level windows -
388 // don't deal with them here
393 win
->GetPosition(&wx
, &wy
);
394 win
->GetSize(&ww
, &wh
);
395 if ( wx
+ ww
> maxX
)
397 if ( wy
+ wh
> maxY
)
402 return wxSize(maxX
+ 7, maxY
+ 14);
406 // for a generic window there is no natural best size - just use the
412 // set the min/max size of the window
413 void wxWindowBase::SetSizeHints(int minW
, int minH
,
415 int WXUNUSED(incW
), int WXUNUSED(incH
))
423 // ----------------------------------------------------------------------------
424 // show/hide/enable/disable the window
425 // ----------------------------------------------------------------------------
427 bool wxWindowBase::Show(bool show
)
429 if ( show
!= m_isShown
)
441 bool wxWindowBase::Enable(bool enable
)
443 if ( enable
!= m_isEnabled
)
445 m_isEnabled
= enable
;
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 bool wxWindowBase::IsTopLevel() const
463 // ----------------------------------------------------------------------------
464 // reparenting the window
465 // ----------------------------------------------------------------------------
467 void wxWindowBase::AddChild(wxWindowBase
*child
)
469 wxCHECK_RET( child
, wxT("can't add a NULL child") );
471 GetChildren().Append(child
);
472 child
->SetParent(this);
475 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
477 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
479 GetChildren().DeleteObject(child
);
480 child
->SetParent((wxWindow
*)NULL
);
483 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
485 wxWindow
*oldParent
= GetParent();
486 if ( newParent
== oldParent
)
492 // unlink this window from the existing parent.
495 oldParent
->RemoveChild(this);
499 wxTopLevelWindows
.DeleteObject(this);
502 // add it to the new one
505 newParent
->AddChild(this);
509 wxTopLevelWindows
.Append(this);
515 // ----------------------------------------------------------------------------
516 // event handler stuff
517 // ----------------------------------------------------------------------------
519 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
521 handler
->SetNextHandler(GetEventHandler());
522 SetEventHandler(handler
);
525 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
527 wxEvtHandler
*handlerA
= GetEventHandler();
530 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
531 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
532 SetEventHandler(handlerB
);
536 handlerA
= (wxEvtHandler
*)NULL
;
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
549 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
552 m_backgroundColour
= colour
;
557 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
559 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
562 m_foregroundColour
= colour
;
567 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
569 // don't try to set invalid cursor, always fall back to the default
570 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
572 if ( (wxCursor
&)cursorOk
== m_cursor
)
583 bool wxWindowBase::SetFont(const wxFont
& font
)
585 // don't try to set invalid font, always fall back to the default
586 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
588 if ( (wxFont
&)fontOk
== m_font
)
600 void wxWindowBase::SetCaret(wxCaret
*caret
)
611 wxASSERT_MSG( m_caret
->GetWindow() == this,
612 wxT("caret should be created associated to this window") );
615 #endif // wxUSE_CARET
618 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
622 # if defined(__VISAGECPP__)
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) ;
634 void wxWindowBase::SetValidator(const wxValidator
& validator
)
636 if ( m_windowValidator
)
637 delete m_windowValidator
;
639 m_windowValidator
= (wxValidator
*)validator
.Clone();
641 if ( m_windowValidator
)
642 m_windowValidator
->SetWindow(this) ;
644 # endif // __VISAGECPP__
645 #endif // wxUSE_VALIDATORS
647 // ----------------------------------------------------------------------------
648 // update region testing
649 // ----------------------------------------------------------------------------
651 bool wxWindowBase::IsExposed(int x
, int y
) const
653 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
656 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
658 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
661 // ----------------------------------------------------------------------------
662 // find window by id or name
663 // ----------------------------------------------------------------------------
665 wxWindow
*wxWindowBase::FindWindow( long id
)
667 if ( id
== m_windowId
)
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 wxWindowBase
*child
= node
->GetData();
675 res
= child
->FindWindow( id
);
678 return (wxWindow
*)res
;
681 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
683 if ( name
== m_windowName
)
684 return (wxWindow
*)this;
686 wxWindowBase
*res
= (wxWindow
*)NULL
;
687 wxWindowList::Node
*node
;
688 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
690 wxWindow
*child
= node
->GetData();
691 res
= child
->FindWindow(name
);
694 return (wxWindow
*)res
;
697 // ----------------------------------------------------------------------------
698 // dialog oriented functions
699 // ----------------------------------------------------------------------------
701 void wxWindowBase::MakeModal(bool modal
)
703 // Disable all other windows
706 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
709 wxWindow
*win
= node
->GetData();
713 node
= node
->GetNext();
718 bool wxWindowBase::Validate()
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
->Validate((wxWindow
*)this) )
731 #endif // wxUSE_VALIDATORS
736 bool wxWindowBase::TransferDataToWindow()
739 wxWindowList::Node
*node
;
740 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
742 wxWindowBase
*child
= node
->GetData();
743 wxValidator
*validator
= child
->GetValidator();
744 if ( validator
&& !validator
->TransferToWindow() )
746 wxLog
*log
= wxLog::GetActiveTarget();
749 wxLogWarning(_("Could not transfer data to window"));
756 #endif // wxUSE_VALIDATORS
761 bool wxWindowBase::TransferDataFromWindow()
764 wxWindowList::Node
*node
;
765 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
767 wxWindow
*child
= node
->GetData();
768 if ( child
->GetValidator() &&
769 !child
->GetValidator()->TransferFromWindow() )
774 #endif // wxUSE_VALIDATORS
779 void wxWindowBase::InitDialog()
781 wxInitDialogEvent
event(GetId());
782 event
.SetEventObject( this );
783 GetEventHandler()->ProcessEvent(event
);
786 // ----------------------------------------------------------------------------
788 // ----------------------------------------------------------------------------
792 void wxWindowBase::SetToolTip( const wxString
&tip
)
794 // don't create the new tooltip if we already have one
797 m_tooltip
->SetTip( tip
);
801 SetToolTip( new wxToolTip( tip
) );
804 // setting empty tooltip text does not remove the tooltip any more - use
805 // SetToolTip((wxToolTip *)NULL) for this
808 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
816 #endif // wxUSE_TOOLTIPS
818 // ----------------------------------------------------------------------------
819 // constraints and sizers
820 // ----------------------------------------------------------------------------
822 #if wxUSE_CONSTRAINTS
824 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
828 UnsetConstraints(m_constraints
);
829 delete m_constraints
;
831 m_constraints
= constraints
;
834 // Make sure other windows know they're part of a 'meaningful relationship'
835 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
836 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
837 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
838 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
839 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
840 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
841 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
842 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
843 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
844 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
845 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
846 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
847 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
848 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
849 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
850 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
854 // This removes any dangling pointers to this window in other windows'
855 // constraintsInvolvedIn lists.
856 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
860 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
861 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
862 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
863 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
864 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
865 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
866 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
867 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
868 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
869 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
870 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
871 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
872 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
873 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
874 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
875 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
879 // Back-pointer to other windows we're involved with, so if we delete this
880 // window, we must delete any constraints we're involved with.
881 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
883 if ( !m_constraintsInvolvedIn
)
884 m_constraintsInvolvedIn
= new wxWindowList
;
885 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
886 m_constraintsInvolvedIn
->Append(otherWin
);
889 // REMOVE back-pointer to other windows we're involved with.
890 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
892 if ( m_constraintsInvolvedIn
)
893 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
896 // Reset any constraints that mention this window
897 void wxWindowBase::DeleteRelatedConstraints()
899 if ( m_constraintsInvolvedIn
)
901 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
904 wxWindow
*win
= node
->GetData();
905 wxLayoutConstraints
*constr
= win
->GetConstraints();
907 // Reset any constraints involving this window
910 constr
->left
.ResetIfWin(this);
911 constr
->top
.ResetIfWin(this);
912 constr
->right
.ResetIfWin(this);
913 constr
->bottom
.ResetIfWin(this);
914 constr
->width
.ResetIfWin(this);
915 constr
->height
.ResetIfWin(this);
916 constr
->centreX
.ResetIfWin(this);
917 constr
->centreY
.ResetIfWin(this);
920 wxWindowList::Node
*next
= node
->GetNext();
925 delete m_constraintsInvolvedIn
;
926 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
930 void wxWindowBase::SetSizer(wxSizer
*sizer
)
932 if (m_windowSizer
) delete m_windowSizer
;
934 m_windowSizer
= sizer
;
937 bool wxWindowBase::Layout()
940 GetClientSize(&w
, &h
);
942 // If there is a sizer, use it instead of the constraints
945 GetSizer()->SetDimension( 0, 0, w
, h
);
949 if ( GetConstraints() )
951 GetConstraints()->width
.SetValue(w
);
952 GetConstraints()->height
.SetValue(h
);
955 // Evaluate child constraints
956 ResetConstraints(); // Mark all constraints as unevaluated
957 DoPhase(1); // Just one phase need if no sizers involved
959 SetConstraintSizes(); // Recursively set the real window sizes
965 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
966 // do a similar thing, but also impose their own 'constraints' and order the
967 // evaluation differently.
968 bool wxWindowBase::LayoutPhase1(int *noChanges
)
970 wxLayoutConstraints
*constr
= GetConstraints();
973 return constr
->SatisfyConstraints(this, noChanges
);
979 bool wxWindowBase::LayoutPhase2(int *noChanges
)
989 // Do a phase of evaluating child constraints
990 bool wxWindowBase::DoPhase(int phase
)
992 int noIterations
= 0;
993 int maxIterations
= 500;
996 wxWindowList succeeded
;
997 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1001 wxWindowList::Node
*node
= GetChildren().GetFirst();
1004 wxWindow
*child
= node
->GetData();
1005 if ( !child
->IsTopLevel() )
1007 wxLayoutConstraints
*constr
= child
->GetConstraints();
1010 if ( !succeeded
.Find(child
) )
1012 int tempNoChanges
= 0;
1013 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1014 noChanges
+= tempNoChanges
;
1017 succeeded
.Append(child
);
1022 node
= node
->GetNext();
1031 void wxWindowBase::ResetConstraints()
1033 wxLayoutConstraints
*constr
= GetConstraints();
1036 constr
->left
.SetDone(FALSE
);
1037 constr
->top
.SetDone(FALSE
);
1038 constr
->right
.SetDone(FALSE
);
1039 constr
->bottom
.SetDone(FALSE
);
1040 constr
->width
.SetDone(FALSE
);
1041 constr
->height
.SetDone(FALSE
);
1042 constr
->centreX
.SetDone(FALSE
);
1043 constr
->centreY
.SetDone(FALSE
);
1045 wxWindowList::Node
*node
= GetChildren().GetFirst();
1048 wxWindow
*win
= node
->GetData();
1049 if ( !win
->IsTopLevel() )
1050 win
->ResetConstraints();
1051 node
= node
->GetNext();
1055 // Need to distinguish between setting the 'fake' size for windows and sizers,
1056 // and setting the real values.
1057 void wxWindowBase::SetConstraintSizes(bool recurse
)
1059 wxLayoutConstraints
*constr
= GetConstraints();
1060 if ( constr
&& constr
->left
.GetDone() && constr
->right
.GetDone( ) &&
1061 constr
->width
.GetDone() && constr
->height
.GetDone())
1063 int x
= constr
->left
.GetValue();
1064 int y
= constr
->top
.GetValue();
1065 int w
= constr
->width
.GetValue();
1066 int h
= constr
->height
.GetValue();
1068 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1069 (constr
->height
.GetRelationship() != wxAsIs
) )
1071 SetSize(x
, y
, w
, h
);
1075 // If we don't want to resize this window, just move it...
1081 wxChar
*windowClass
= GetClassInfo()->GetClassName();
1084 if ( GetName() == wxT("") )
1085 winName
= wxT("unnamed");
1087 winName
= GetName();
1088 wxLogDebug( wxT("Constraint(s) not satisfied for window of type %s, name %s:\n"),
1089 (const wxChar
*)windowClass
,
1090 (const wxChar
*)winName
);
1091 if ( !constr
->left
.GetDone()) wxLogDebug( wxT(" unsatisfied 'left' constraint.\n") );
1092 if ( !constr
->right
.GetDone()) wxLogDebug( wxT(" unsatisfied 'right' constraint.\n") );
1093 if ( !constr
->width
.GetDone()) wxLogDebug( wxT(" unsatisfied 'width' constraint.\n") );
1094 if ( !constr
->height
.GetDone()) wxLogDebug( wxT(" unsatisfied 'height' constraint.\n") );
1095 wxLogDebug( wxT("Please check constraints: try adding AsIs() constraints.\n") );
1100 wxWindowList::Node
*node
= GetChildren().GetFirst();
1103 wxWindow
*win
= node
->GetData();
1104 if ( !win
->IsTopLevel() )
1105 win
->SetConstraintSizes();
1106 node
= node
->GetNext();
1111 // Only set the size/position of the constraint (if any)
1112 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1114 wxLayoutConstraints
*constr
= GetConstraints();
1119 constr
->left
.SetValue(x
);
1120 constr
->left
.SetDone(TRUE
);
1124 constr
->top
.SetValue(y
);
1125 constr
->top
.SetDone(TRUE
);
1129 constr
->width
.SetValue(w
);
1130 constr
->width
.SetDone(TRUE
);
1134 constr
->height
.SetValue(h
);
1135 constr
->height
.SetDone(TRUE
);
1140 void wxWindowBase::MoveConstraint(int x
, int y
)
1142 wxLayoutConstraints
*constr
= GetConstraints();
1147 constr
->left
.SetValue(x
);
1148 constr
->left
.SetDone(TRUE
);
1152 constr
->top
.SetValue(y
);
1153 constr
->top
.SetDone(TRUE
);
1158 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1160 wxLayoutConstraints
*constr
= GetConstraints();
1163 *w
= constr
->width
.GetValue();
1164 *h
= constr
->height
.GetValue();
1170 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1172 wxLayoutConstraints
*constr
= GetConstraints();
1175 *w
= constr
->width
.GetValue();
1176 *h
= constr
->height
.GetValue();
1179 GetClientSize(w
, h
);
1182 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1184 wxLayoutConstraints
*constr
= GetConstraints();
1187 *x
= constr
->left
.GetValue();
1188 *y
= constr
->top
.GetValue();
1194 #endif // wxUSE_CONSTRAINTS
1196 // ----------------------------------------------------------------------------
1197 // do Update UI processing for child controls
1198 // ----------------------------------------------------------------------------
1200 // TODO: should this be implemented for the child window rather
1201 // than the parent? Then you can override it e.g. for wxCheckBox
1202 // to do the Right Thing rather than having to assume a fixed number
1203 // of control classes.
1204 void wxWindowBase::UpdateWindowUI()
1206 wxUpdateUIEvent
event(GetId());
1207 event
.m_eventObject
= this;
1209 if ( GetEventHandler()->ProcessEvent(event
) )
1211 if ( event
.GetSetEnabled() )
1212 Enable(event
.GetEnabled());
1214 if ( event
.GetSetText() )
1216 wxControl
*control
= wxDynamicCast(this, wxControl
);
1219 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1221 text
->SetValue(event
.GetText());
1223 control
->SetLabel(event
.GetText());
1228 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1231 if ( event
.GetSetChecked() )
1232 checkbox
->SetValue(event
.GetChecked());
1234 #endif // wxUSE_CHECKBOX
1236 #if wxUSE_RADIOBUTTON
1237 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1240 if ( event
.GetSetChecked() )
1241 radiobtn
->SetValue(event
.GetChecked());
1243 #endif // wxUSE_RADIOBUTTON
1247 // ----------------------------------------------------------------------------
1248 // dialog units translations
1249 // ----------------------------------------------------------------------------
1251 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1253 int charWidth
= GetCharWidth();
1254 int charHeight
= GetCharHeight();
1255 wxPoint
pt2(-1, -1);
1257 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1259 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1264 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1266 int charWidth
= GetCharWidth();
1267 int charHeight
= GetCharHeight();
1268 wxPoint
pt2(-1, -1);
1270 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1272 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1277 // ----------------------------------------------------------------------------
1279 // ----------------------------------------------------------------------------
1281 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1283 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1284 wxT("can't have both object and void client data") );
1286 if ( m_clientObject
)
1287 delete m_clientObject
;
1289 m_clientObject
= data
;
1290 m_clientDataType
= ClientData_Object
;
1293 wxClientData
*wxWindowBase::DoGetClientObject() const
1295 wxASSERT_MSG( m_clientDataType
== ClientData_Object
,
1296 wxT("this window doesn't have object client data") );
1298 return m_clientObject
;
1301 void wxWindowBase::DoSetClientData( void *data
)
1303 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1304 wxT("can't have both object and void client data") );
1306 m_clientData
= data
;
1307 m_clientDataType
= ClientData_Void
;
1310 void *wxWindowBase::DoGetClientData() const
1312 wxASSERT_MSG( m_clientDataType
== ClientData_Void
,
1313 wxT("this window doesn't have void client data") );
1315 return m_clientData
;
1318 // ----------------------------------------------------------------------------
1320 // ----------------------------------------------------------------------------
1322 // propagate the colour change event to the subwindows
1323 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1325 wxWindowList::Node
*node
= GetChildren().GetFirst();
1328 // Only propagate to non-top-level windows
1329 wxWindow
*win
= node
->GetData();
1330 if ( !win
->IsTopLevel() )
1332 wxSysColourChangedEvent event2
;
1333 event
.m_eventObject
= win
;
1334 win
->GetEventHandler()->ProcessEvent(event2
);
1337 node
= node
->GetNext();
1341 // the default action is to populate dialog with data when it's created
1342 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1344 TransferDataToWindow();
1347 // ----------------------------------------------------------------------------
1348 // list classes implementation
1349 // ----------------------------------------------------------------------------
1351 void wxWindowListNode::DeleteData()
1353 delete (wxWindow
*)GetData();