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"
43 #include "wx/msgdlg.h"
44 #include "wx/statusbr.h"
48 #include "wx/layout.h"
50 #endif // wxUSE_CONSTRAINTS
52 #if wxUSE_DRAG_AND_DROP
54 #endif // wxUSE_DRAG_AND_DROP
57 #include "wx/tooltip.h"
58 #endif // wxUSE_TOOLTIPS
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 int wxWindowBase::ms_lastControlId
= -200;
70 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
77 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
78 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
79 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
82 // ============================================================================
83 // implementation of the common functionality of the wxWindow class
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // the default initialization
91 void wxWindowBase::InitBase()
93 // no window yet, no parent nor children
94 m_parent
= (wxWindow
*)NULL
;
96 m_children
.DeleteContents( FALSE
); // don't auto delete node data
98 // no constraints on the minimal window size
104 // window is created enabled but it's not visible yet
108 // no client data (yet)
110 m_clientDataType
= ClientData_None
;
112 // the default event handler is just this window
113 m_eventHandler
= this;
117 m_windowValidator
= (wxValidator
*) NULL
;
118 #endif // wxUSE_VALIDATORS
120 // use the system default colours
121 wxSystemSettings settings
;
123 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
124 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
125 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
127 // GRG, changed Mar/2000
128 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
133 // an optimization for the event processing: checking this flag is much
134 // faster than using IsKindOf(CLASSINFO(wxWindow))
137 #if wxUSE_CONSTRAINTS
138 // no constraints whatsoever
139 m_constraints
= (wxLayoutConstraints
*) NULL
;
140 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
141 m_windowSizer
= (wxSizer
*) NULL
;
142 m_autoLayout
= FALSE
;
143 #endif // wxUSE_CONSTRAINTS
145 #if wxUSE_DRAG_AND_DROP
146 m_dropTarget
= (wxDropTarget
*)NULL
;
147 #endif // wxUSE_DRAG_AND_DROP
150 m_tooltip
= (wxToolTip
*)NULL
;
151 #endif // wxUSE_TOOLTIPS
154 m_caret
= (wxCaret
*)NULL
;
155 #endif // wxUSE_CARET
158 // common part of window creation process
159 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
161 const wxPoint
& WXUNUSED(pos
),
162 const wxSize
& WXUNUSED(size
),
164 const wxValidator
& validator
,
165 const wxString
& name
)
167 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
168 // member variables - check that it has been called (will catch the case
169 // when a new ctor is added which doesn't call InitWindow)
170 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
172 // generate a new id if the user doesn't care about it
173 m_windowId
= id
== -1 ? NewControlId() : id
;
176 SetWindowStyleFlag(style
);
180 SetValidator(validator
);
181 #endif // wxUSE_VALIDATORS
183 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
184 // have it too - like this it's possible to set it only in the top level
185 // dialog/frame and all children will inherit it by defult
186 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
188 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
199 wxWindowBase::~wxWindowBase()
201 // FIXME if these 2 cases result from programming errors in the user code
202 // we should probably assert here instead of silently fixing them
204 // Just in case the window has been Closed, but we're then deleting
205 // immediately: don't leave dangling pointers.
206 wxPendingDelete
.DeleteObject(this);
208 // Just in case we've loaded a top-level window via LoadNativeDialog but
209 // we weren't a dialog class
210 wxTopLevelWindows
.DeleteObject(this);
212 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
214 // make sure that there are no dangling pointers left pointing to us
215 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
218 if ( panel
->GetLastFocus() == this )
220 panel
->SetLastFocus((wxWindow
*)NULL
);
227 #endif // wxUSE_CARET
230 if ( m_windowValidator
)
231 delete m_windowValidator
;
232 #endif // wxUSE_VALIDATORS
234 // we only delete object data, not untyped
235 if ( m_clientDataType
== ClientData_Object
)
236 delete m_clientObject
;
238 #if wxUSE_CONSTRAINTS
239 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
240 // at deleted windows as they delete themselves.
241 DeleteRelatedConstraints();
245 // This removes any dangling pointers to this window in other windows'
246 // constraintsInvolvedIn lists.
247 UnsetConstraints(m_constraints
);
248 delete m_constraints
;
249 m_constraints
= NULL
;
253 delete m_windowSizer
;
255 #endif // wxUSE_CONSTRAINTS
257 #if wxUSE_DRAG_AND_DROP
260 #endif // wxUSE_DRAG_AND_DROP
265 #endif // wxUSE_TOOLTIPS
268 bool wxWindowBase::Destroy()
275 bool wxWindowBase::Close(bool force
)
277 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
278 event
.SetEventObject(this);
279 #if WXWIN_COMPATIBILITY
280 event
.SetForce(force
);
281 #endif // WXWIN_COMPATIBILITY
282 event
.SetCanVeto(!force
);
284 // return FALSE if window wasn't closed because the application vetoed the
286 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
289 bool wxWindowBase::DestroyChildren()
291 wxWindowList::Node
*node
;
294 // we iterate until the list becomes empty
295 node
= GetChildren().GetFirst();
299 wxWindow
*child
= node
->GetData();
301 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
305 wxASSERT_MSG( !GetChildren().Find(child
),
306 wxT("child didn't remove itself using RemoveChild()") );
312 // ----------------------------------------------------------------------------
313 // size/position related methods
314 // ----------------------------------------------------------------------------
316 // centre the window with respect to its parent in either (or both) directions
317 void wxWindowBase::Centre(int direction
)
319 // the position/size of the parent window or of the entire screen
321 int widthParent
, heightParent
;
323 wxWindow
*parent
= NULL
;
325 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
327 // find the parent to centre this window on: it should be the
328 // immediate parent for the controls but the top level parent for the
329 // top level windows (like dialogs)
330 parent
= GetParent();
333 while ( parent
&& !parent
->IsTopLevel() )
335 parent
= parent
->GetParent();
339 // did we find the parent?
343 direction
|= wxCENTRE_ON_SCREEN
;
347 if ( direction
& wxCENTRE_ON_SCREEN
)
349 // centre with respect to the whole screen
350 wxDisplaySize(&widthParent
, &heightParent
);
356 // centre on the parent
357 parent
->GetSize(&widthParent
, &heightParent
);
359 // adjust to the parents position
360 posParent
= parent
->GetPosition();
364 // centre inside the parents client rectangle
365 parent
->GetClientSize(&widthParent
, &heightParent
);
370 GetSize(&width
, &height
);
375 if ( direction
& wxHORIZONTAL
)
376 xNew
= (widthParent
- width
)/2;
378 if ( direction
& wxVERTICAL
)
379 yNew
= (heightParent
- height
)/2;
384 // move the centre of this window to this position
388 // fits the window around the children
389 void wxWindowBase::Fit()
391 if ( GetChildren().GetCount() > 0 )
393 SetClientSize(DoGetBestSize());
395 //else: do nothing if we have no children
398 // return the size best suited for the current window
399 wxSize
wxWindowBase::DoGetBestSize() const
401 if ( GetChildren().GetCount() > 0 )
403 // our minimal acceptable size is such that all our windows fit inside
407 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
409 node
= node
->GetNext() )
411 wxWindow
*win
= node
->GetData();
412 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) )
414 // dialogs and frames lie in different top level windows -
415 // don't deal with them here; as for the status bars, they
416 // don't lie in the client area at all
421 win
->GetPosition(&wx
, &wy
);
423 // if the window hadn't been positioned yet, assume that it is in
430 win
->GetSize(&ww
, &wh
);
431 if ( wx
+ ww
> maxX
)
433 if ( wy
+ wh
> maxY
)
438 return wxSize(maxX
+ 7, maxY
+ 14);
442 // for a generic window there is no natural best size - just use the
448 // set the min/max size of the window
449 void wxWindowBase::SetSizeHints(int minW
, int minH
,
451 int WXUNUSED(incW
), int WXUNUSED(incH
))
459 // ----------------------------------------------------------------------------
460 // show/hide/enable/disable the window
461 // ----------------------------------------------------------------------------
463 bool wxWindowBase::Show(bool show
)
465 if ( show
!= m_isShown
)
477 bool wxWindowBase::Enable(bool enable
)
479 if ( enable
!= m_isEnabled
)
481 m_isEnabled
= enable
;
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 bool wxWindowBase::IsTopLevel() const
499 // ----------------------------------------------------------------------------
500 // reparenting the window
501 // ----------------------------------------------------------------------------
503 void wxWindowBase::AddChild(wxWindowBase
*child
)
505 wxCHECK_RET( child
, wxT("can't add a NULL child") );
507 // this should never happen and it will lead to a crash later if it does
508 // because RemoveChild() will remove only one node from the children list
509 // and the other(s) one(s) will be left with dangling pointers in them
510 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
512 GetChildren().Append(child
);
513 child
->SetParent(this);
516 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
518 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
520 GetChildren().DeleteObject(child
);
521 child
->SetParent((wxWindow
*)NULL
);
524 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
526 wxWindow
*oldParent
= GetParent();
527 if ( newParent
== oldParent
)
533 // unlink this window from the existing parent.
536 oldParent
->RemoveChild(this);
540 wxTopLevelWindows
.DeleteObject(this);
543 // add it to the new one
546 newParent
->AddChild(this);
550 wxTopLevelWindows
.Append(this);
556 // ----------------------------------------------------------------------------
557 // event handler stuff
558 // ----------------------------------------------------------------------------
560 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
562 handler
->SetNextHandler(GetEventHandler());
563 SetEventHandler(handler
);
566 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
568 wxEvtHandler
*handlerA
= GetEventHandler();
571 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
572 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
573 SetEventHandler(handlerB
);
577 handlerA
= (wxEvtHandler
*)NULL
;
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
590 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
593 m_backgroundColour
= colour
;
598 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
600 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
603 m_foregroundColour
= colour
;
608 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
610 // setting an invalid cursor is ok, it means that we don't have any special
612 if ( m_cursor
== cursor
)
623 bool wxWindowBase::SetFont(const wxFont
& font
)
625 // don't try to set invalid font, always fall back to the default
626 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
628 if ( (wxFont
&)fontOk
== m_font
)
640 void wxWindowBase::SetCaret(wxCaret
*caret
)
651 wxASSERT_MSG( m_caret
->GetWindow() == this,
652 wxT("caret should be created associated to this window") );
655 #endif // wxUSE_CARET
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
662 void wxWindowBase::SetValidator(const wxValidator
& validator
)
664 if ( m_windowValidator
)
665 delete m_windowValidator
;
667 m_windowValidator
= (wxValidator
*)validator
.Clone();
669 if ( m_windowValidator
)
670 m_windowValidator
->SetWindow(this) ;
672 #endif // wxUSE_VALIDATORS
674 // ----------------------------------------------------------------------------
675 // update region testing
676 // ----------------------------------------------------------------------------
678 bool wxWindowBase::IsExposed(int x
, int y
) const
680 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
683 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
685 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
688 // ----------------------------------------------------------------------------
689 // find window by id or name
690 // ----------------------------------------------------------------------------
692 wxWindow
*wxWindowBase::FindWindow( long id
)
694 if ( id
== m_windowId
)
695 return (wxWindow
*)this;
697 wxWindowBase
*res
= (wxWindow
*)NULL
;
698 wxWindowList::Node
*node
;
699 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
701 wxWindowBase
*child
= node
->GetData();
702 res
= child
->FindWindow( id
);
705 return (wxWindow
*)res
;
708 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
710 if ( name
== m_windowName
)
711 return (wxWindow
*)this;
713 wxWindowBase
*res
= (wxWindow
*)NULL
;
714 wxWindowList::Node
*node
;
715 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
717 wxWindow
*child
= node
->GetData();
718 res
= child
->FindWindow(name
);
721 return (wxWindow
*)res
;
724 // ----------------------------------------------------------------------------
725 // dialog oriented functions
726 // ----------------------------------------------------------------------------
728 void wxWindowBase::MakeModal(bool modal
)
730 // Disable all other windows
733 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
736 wxWindow
*win
= node
->GetData();
740 node
= node
->GetNext();
745 bool wxWindowBase::Validate()
748 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
750 wxWindowList::Node
*node
;
751 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
753 wxWindowBase
*child
= node
->GetData();
754 wxValidator
*validator
= child
->GetValidator();
755 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
760 if ( recurse
&& !child
->Validate() )
765 #endif // wxUSE_VALIDATORS
770 bool wxWindowBase::TransferDataToWindow()
773 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
775 wxWindowList::Node
*node
;
776 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
778 wxWindowBase
*child
= node
->GetData();
779 wxValidator
*validator
= child
->GetValidator();
780 if ( validator
&& !validator
->TransferToWindow() )
782 wxLogWarning(_("Could not transfer data to window"));
783 wxLog::FlushActive();
790 if ( !child
->TransferDataToWindow() )
792 // warning already given
797 #endif // wxUSE_VALIDATORS
802 bool wxWindowBase::TransferDataFromWindow()
805 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
807 wxWindowList::Node
*node
;
808 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
810 wxWindow
*child
= node
->GetData();
811 wxValidator
*validator
= child
->GetValidator();
812 if ( validator
&& !validator
->TransferFromWindow() )
814 // nop warning here because the application is supposed to give
815 // one itself - we don't know here what might have gone wrongly
822 if ( !child
->TransferDataFromWindow() )
824 // warning already given
829 #endif // wxUSE_VALIDATORS
834 void wxWindowBase::InitDialog()
836 wxInitDialogEvent
event(GetId());
837 event
.SetEventObject( this );
838 GetEventHandler()->ProcessEvent(event
);
841 // ----------------------------------------------------------------------------
843 // ----------------------------------------------------------------------------
847 void wxWindowBase::SetToolTip( const wxString
&tip
)
849 // don't create the new tooltip if we already have one
852 m_tooltip
->SetTip( tip
);
856 SetToolTip( new wxToolTip( tip
) );
859 // setting empty tooltip text does not remove the tooltip any more - use
860 // SetToolTip((wxToolTip *)NULL) for this
863 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
871 #endif // wxUSE_TOOLTIPS
873 // ----------------------------------------------------------------------------
874 // constraints and sizers
875 // ----------------------------------------------------------------------------
877 #if wxUSE_CONSTRAINTS
879 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
883 UnsetConstraints(m_constraints
);
884 delete m_constraints
;
886 m_constraints
= constraints
;
889 // Make sure other windows know they're part of a 'meaningful relationship'
890 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
891 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
892 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
893 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
894 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
895 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
896 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
897 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
898 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
899 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
900 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
901 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
902 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
903 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
904 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
905 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
909 // This removes any dangling pointers to this window in other windows'
910 // constraintsInvolvedIn lists.
911 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
915 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
916 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
917 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
918 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
919 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
920 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
921 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
922 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
923 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
924 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
925 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
926 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
927 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
928 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
929 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
930 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
934 // Back-pointer to other windows we're involved with, so if we delete this
935 // window, we must delete any constraints we're involved with.
936 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
938 if ( !m_constraintsInvolvedIn
)
939 m_constraintsInvolvedIn
= new wxWindowList
;
940 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
941 m_constraintsInvolvedIn
->Append(otherWin
);
944 // REMOVE back-pointer to other windows we're involved with.
945 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
947 if ( m_constraintsInvolvedIn
)
948 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
951 // Reset any constraints that mention this window
952 void wxWindowBase::DeleteRelatedConstraints()
954 if ( m_constraintsInvolvedIn
)
956 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
959 wxWindow
*win
= node
->GetData();
960 wxLayoutConstraints
*constr
= win
->GetConstraints();
962 // Reset any constraints involving this window
965 constr
->left
.ResetIfWin(this);
966 constr
->top
.ResetIfWin(this);
967 constr
->right
.ResetIfWin(this);
968 constr
->bottom
.ResetIfWin(this);
969 constr
->width
.ResetIfWin(this);
970 constr
->height
.ResetIfWin(this);
971 constr
->centreX
.ResetIfWin(this);
972 constr
->centreY
.ResetIfWin(this);
975 wxWindowList::Node
*next
= node
->GetNext();
980 delete m_constraintsInvolvedIn
;
981 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
985 void wxWindowBase::SetSizer(wxSizer
*sizer
)
987 if (m_windowSizer
) delete m_windowSizer
;
989 m_windowSizer
= sizer
;
992 bool wxWindowBase::Layout()
994 // If there is a sizer, use it instead of the constraints
998 GetClientSize(&w
, &h
);
1000 GetSizer()->SetDimension( 0, 0, w
, h
);
1004 wxLayoutConstraints
*constr
= GetConstraints();
1005 bool wasOk
= constr
&& constr
->AreSatisfied();
1007 ResetConstraints(); // Mark all constraints as unevaluated
1009 // if we're a top level panel (i.e. our parent is frame/dialog), our
1010 // own constraints will never be satisfied any more unless we do it
1015 while ( noChanges
> 0 )
1017 constr
->SatisfyConstraints(this, &noChanges
);
1021 DoPhase(1); // Layout children
1022 DoPhase(2); // Layout grand children
1023 SetConstraintSizes(); // Recursively set the real window sizes
1030 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1031 // do a similar thing, but also impose their own 'constraints' and order the
1032 // evaluation differently.
1033 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1035 wxLayoutConstraints
*constr
= GetConstraints();
1038 return constr
->SatisfyConstraints(this, noChanges
);
1044 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1054 // Do a phase of evaluating child constraints
1055 bool wxWindowBase::DoPhase(int phase
)
1057 int noIterations
= 0;
1058 int maxIterations
= 500;
1061 wxWindowList succeeded
;
1062 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1066 wxWindowList::Node
*node
= GetChildren().GetFirst();
1069 wxWindow
*child
= node
->GetData();
1070 if ( !child
->IsTopLevel() )
1072 wxLayoutConstraints
*constr
= child
->GetConstraints();
1075 if ( !succeeded
.Find(child
) )
1077 int tempNoChanges
= 0;
1078 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1079 noChanges
+= tempNoChanges
;
1082 succeeded
.Append(child
);
1087 node
= node
->GetNext();
1096 void wxWindowBase::ResetConstraints()
1098 wxLayoutConstraints
*constr
= GetConstraints();
1101 constr
->left
.SetDone(FALSE
);
1102 constr
->top
.SetDone(FALSE
);
1103 constr
->right
.SetDone(FALSE
);
1104 constr
->bottom
.SetDone(FALSE
);
1105 constr
->width
.SetDone(FALSE
);
1106 constr
->height
.SetDone(FALSE
);
1107 constr
->centreX
.SetDone(FALSE
);
1108 constr
->centreY
.SetDone(FALSE
);
1111 wxWindowList::Node
*node
= GetChildren().GetFirst();
1114 wxWindow
*win
= node
->GetData();
1115 if ( !win
->IsTopLevel() )
1116 win
->ResetConstraints();
1117 node
= node
->GetNext();
1121 // Need to distinguish between setting the 'fake' size for windows and sizers,
1122 // and setting the real values.
1123 void wxWindowBase::SetConstraintSizes(bool recurse
)
1125 wxLayoutConstraints
*constr
= GetConstraints();
1126 if ( constr
&& constr
->AreSatisfied() )
1128 int x
= constr
->left
.GetValue();
1129 int y
= constr
->top
.GetValue();
1130 int w
= constr
->width
.GetValue();
1131 int h
= constr
->height
.GetValue();
1133 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1134 (constr
->height
.GetRelationship() != wxAsIs
) )
1136 SetSize(x
, y
, w
, h
);
1140 // If we don't want to resize this window, just move it...
1146 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1147 GetClassInfo()->GetClassName(),
1153 wxWindowList::Node
*node
= GetChildren().GetFirst();
1156 wxWindow
*win
= node
->GetData();
1157 if ( !win
->IsTopLevel() )
1158 win
->SetConstraintSizes();
1159 node
= node
->GetNext();
1164 // Only set the size/position of the constraint (if any)
1165 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1167 wxLayoutConstraints
*constr
= GetConstraints();
1172 constr
->left
.SetValue(x
);
1173 constr
->left
.SetDone(TRUE
);
1177 constr
->top
.SetValue(y
);
1178 constr
->top
.SetDone(TRUE
);
1182 constr
->width
.SetValue(w
);
1183 constr
->width
.SetDone(TRUE
);
1187 constr
->height
.SetValue(h
);
1188 constr
->height
.SetDone(TRUE
);
1193 void wxWindowBase::MoveConstraint(int x
, int y
)
1195 wxLayoutConstraints
*constr
= GetConstraints();
1200 constr
->left
.SetValue(x
);
1201 constr
->left
.SetDone(TRUE
);
1205 constr
->top
.SetValue(y
);
1206 constr
->top
.SetDone(TRUE
);
1211 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1213 wxLayoutConstraints
*constr
= GetConstraints();
1216 *w
= constr
->width
.GetValue();
1217 *h
= constr
->height
.GetValue();
1223 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1225 wxLayoutConstraints
*constr
= GetConstraints();
1228 *w
= constr
->width
.GetValue();
1229 *h
= constr
->height
.GetValue();
1232 GetClientSize(w
, h
);
1235 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1237 wxLayoutConstraints
*constr
= GetConstraints();
1240 *x
= constr
->left
.GetValue();
1241 *y
= constr
->top
.GetValue();
1247 #endif // wxUSE_CONSTRAINTS
1249 // ----------------------------------------------------------------------------
1250 // do Update UI processing for child controls
1251 // ----------------------------------------------------------------------------
1253 // TODO: should this be implemented for the child window rather
1254 // than the parent? Then you can override it e.g. for wxCheckBox
1255 // to do the Right Thing rather than having to assume a fixed number
1256 // of control classes.
1257 void wxWindowBase::UpdateWindowUI()
1259 wxUpdateUIEvent
event(GetId());
1260 event
.m_eventObject
= this;
1262 if ( GetEventHandler()->ProcessEvent(event
) )
1264 if ( event
.GetSetEnabled() )
1265 Enable(event
.GetEnabled());
1267 if ( event
.GetSetText() )
1269 wxControl
*control
= wxDynamicCast(this, wxControl
);
1272 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1274 text
->SetValue(event
.GetText());
1276 control
->SetLabel(event
.GetText());
1281 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1284 if ( event
.GetSetChecked() )
1285 checkbox
->SetValue(event
.GetChecked());
1287 #endif // wxUSE_CHECKBOX
1290 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1293 if ( event
.GetSetChecked() )
1294 radiobtn
->SetValue(event
.GetChecked());
1296 #endif // wxUSE_RADIOBTN
1300 // ----------------------------------------------------------------------------
1301 // dialog units translations
1302 // ----------------------------------------------------------------------------
1304 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1306 int charWidth
= GetCharWidth();
1307 int charHeight
= GetCharHeight();
1308 wxPoint
pt2(-1, -1);
1310 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1312 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1317 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1319 int charWidth
= GetCharWidth();
1320 int charHeight
= GetCharHeight();
1321 wxPoint
pt2(-1, -1);
1323 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1325 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1330 // ----------------------------------------------------------------------------
1332 // ----------------------------------------------------------------------------
1334 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1336 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1337 wxT("can't have both object and void client data") );
1339 if ( m_clientObject
)
1340 delete m_clientObject
;
1342 m_clientObject
= data
;
1343 m_clientDataType
= ClientData_Object
;
1346 wxClientData
*wxWindowBase::DoGetClientObject() const
1348 // it's not an error to call GetClientObject() on a window which doesn't
1349 // have client data at all - NULL will be returned
1350 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1351 wxT("this window doesn't have object client data") );
1353 return m_clientObject
;
1356 void wxWindowBase::DoSetClientData( void *data
)
1358 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1359 wxT("can't have both object and void client data") );
1361 m_clientData
= data
;
1362 m_clientDataType
= ClientData_Void
;
1365 void *wxWindowBase::DoGetClientData() const
1367 // it's not an error to call GetClientData() on a window which doesn't have
1368 // client data at all - NULL will be returned
1369 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1370 wxT("this window doesn't have void client data") );
1372 return m_clientData
;
1375 // ----------------------------------------------------------------------------
1377 // ----------------------------------------------------------------------------
1379 // propagate the colour change event to the subwindows
1380 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1382 wxWindowList::Node
*node
= GetChildren().GetFirst();
1385 // Only propagate to non-top-level windows
1386 wxWindow
*win
= node
->GetData();
1387 if ( !win
->IsTopLevel() )
1389 wxSysColourChangedEvent event2
;
1390 event
.m_eventObject
= win
;
1391 win
->GetEventHandler()->ProcessEvent(event2
);
1394 node
= node
->GetNext();
1398 // the default action is to populate dialog with data when it's created
1399 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1401 TransferDataToWindow();
1404 // process Ctrl-Alt-mclick
1405 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1407 if ( event
.ControlDown() && event
.AltDown() )
1409 // don't translate these strings
1411 switch ( wxGetOsVersion() )
1413 case wxMOTIF_X
: port
= _T("Motif"); break;
1414 case wxMACINTOSH
: port
= _T("Mac"); break;
1415 case wxBEOS
: port
= _T("BeOS"); break;
1419 case wxGTK_BEOS
: port
= _T("GTK"); break;
1425 case wxWIN386
: port
= _T("MS Windows"); break;
1429 case wxMGL_OS2
: port
= _T("MGL"); break;
1431 case wxOS2_PM
: port
= _T("OS/2"); break;
1432 default: port
= _T("unknown"); break;
1435 wxMessageBox(wxString::Format(
1437 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1446 _T("wxWindows information"),
1447 wxICON_INFORMATION
| wxOK
,
1456 // ----------------------------------------------------------------------------
1457 // list classes implementation
1458 // ----------------------------------------------------------------------------
1460 void wxWindowListNode::DeleteData()
1462 delete (wxWindow
*)GetData();