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 #if 0 // !defined(__WXMAC__) && !defined(__WXGTK__)
129 m_font
= *wxSWISS_FONT
; // and this?
131 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
137 // an optimization for the event processing: checking this flag is much
138 // faster than using IsKindOf(CLASSINFO(wxWindow))
141 #if wxUSE_CONSTRAINTS
142 // no constraints whatsoever
143 m_constraints
= (wxLayoutConstraints
*) NULL
;
144 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
145 m_windowSizer
= (wxSizer
*) NULL
;
146 m_autoLayout
= FALSE
;
147 #endif // wxUSE_CONSTRAINTS
149 #if wxUSE_DRAG_AND_DROP
150 m_dropTarget
= (wxDropTarget
*)NULL
;
151 #endif // wxUSE_DRAG_AND_DROP
154 m_tooltip
= (wxToolTip
*)NULL
;
155 #endif // wxUSE_TOOLTIPS
158 m_caret
= (wxCaret
*)NULL
;
159 #endif // wxUSE_CARET
162 // common part of window creation process
163 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
165 const wxPoint
& WXUNUSED(pos
),
166 const wxSize
& WXUNUSED(size
),
168 const wxValidator
& validator
,
169 const wxString
& name
)
171 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
172 // member variables - check that it has been called (will catch the case
173 // when a new ctor is added which doesn't call InitWindow)
174 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
176 // generate a new id if the user doesn't care about it
177 m_windowId
= id
== -1 ? NewControlId() : id
;
180 SetWindowStyleFlag(style
);
184 SetValidator(validator
);
185 #endif // wxUSE_VALIDATORS
187 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
188 // have it too - like this it's possible to set it only in the top level
189 // dialog/frame and all children will inherit it by defult
190 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
192 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
203 wxWindowBase::~wxWindowBase()
205 // FIXME if these 2 cases result from programming errors in the user code
206 // we should probably assert here instead of silently fixing them
208 // Just in case the window has been Closed, but we're then deleting
209 // immediately: don't leave dangling pointers.
210 wxPendingDelete
.DeleteObject(this);
212 // Just in case we've loaded a top-level window via LoadNativeDialog but
213 // we weren't a dialog class
214 wxTopLevelWindows
.DeleteObject(this);
216 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
218 // make sure that there are no dangling pointers left pointing to us
219 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
222 if ( panel
->GetLastFocus() == this )
224 panel
->SetLastFocus((wxWindow
*)NULL
);
231 #endif // wxUSE_CARET
234 if ( m_windowValidator
)
235 delete m_windowValidator
;
236 #endif // wxUSE_VALIDATORS
238 // we only delete object data, not untyped
239 if ( m_clientDataType
== ClientData_Object
)
240 delete m_clientObject
;
242 #if wxUSE_CONSTRAINTS
243 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
244 // at deleted windows as they delete themselves.
245 DeleteRelatedConstraints();
249 // This removes any dangling pointers to this window in other windows'
250 // constraintsInvolvedIn lists.
251 UnsetConstraints(m_constraints
);
252 delete m_constraints
;
253 m_constraints
= NULL
;
257 delete m_windowSizer
;
259 #endif // wxUSE_CONSTRAINTS
261 #if wxUSE_DRAG_AND_DROP
264 #endif // wxUSE_DRAG_AND_DROP
269 #endif // wxUSE_TOOLTIPS
272 bool wxWindowBase::Destroy()
279 bool wxWindowBase::Close(bool force
)
281 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
282 event
.SetEventObject(this);
283 #if WXWIN_COMPATIBILITY
284 event
.SetForce(force
);
285 #endif // WXWIN_COMPATIBILITY
286 event
.SetCanVeto(!force
);
288 // return FALSE if window wasn't closed because the application vetoed the
290 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
293 bool wxWindowBase::DestroyChildren()
295 wxWindowList::Node
*node
;
298 // we iterate until the list becomes empty
299 node
= GetChildren().GetFirst();
303 wxWindow
*child
= node
->GetData();
305 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
309 wxASSERT_MSG( !GetChildren().Find(child
),
310 wxT("child didn't remove itself using RemoveChild()") );
316 // ----------------------------------------------------------------------------
317 // size/position related methods
318 // ----------------------------------------------------------------------------
320 // centre the window with respect to its parent in either (or both) directions
321 void wxWindowBase::Centre(int direction
)
323 int widthParent
, heightParent
;
325 wxWindow
*parent
= GetParent();
329 direction
|= wxCENTRE_ON_SCREEN
;
332 if ( direction
& wxCENTRE_ON_SCREEN
)
334 // centre with respect to the whole screen
335 wxDisplaySize(&widthParent
, &heightParent
);
339 // centre inside the parents rectangle
340 parent
->GetClientSize(&widthParent
, &heightParent
);
344 GetSize(&width
, &height
);
349 if ( direction
& wxHORIZONTAL
)
350 xNew
= (widthParent
- width
)/2;
352 if ( direction
& wxVERTICAL
)
353 yNew
= (heightParent
- height
)/2;
355 // controls are always centered on their parent because it doesn't make
356 // sense to centre them on the screen
357 if ( !(direction
& wxCENTRE_ON_SCREEN
) || !IsTopLevel() )
359 // the only chance to get this is to have a not top level window
360 // without parent which shouldn't happen
361 wxCHECK_RET( parent
, wxT("this window must have a parent") );
363 // adjust to the parents client area origin
364 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
370 // move the centre of this window to this position
374 // fits the window around the children
375 void wxWindowBase::Fit()
377 if ( GetChildren().GetCount() > 0 )
379 SetClientSize(DoGetBestSize());
381 //else: do nothing if we have no children
384 // return the size best suited for the current window
385 wxSize
wxWindowBase::DoGetBestSize() const
387 if ( GetChildren().GetCount() > 0 )
389 // our minimal acceptable size is such that all our windows fit inside
393 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
395 node
= node
->GetNext() )
397 wxWindow
*win
= node
->GetData();
398 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) )
400 // dialogs and frames lie in different top level windows -
401 // don't deal with them here; as for the status bars, they
402 // don't lie in the client area at all
407 win
->GetPosition(&wx
, &wy
);
409 // if the window hadn't been positioned yet, assume that it is in
416 win
->GetSize(&ww
, &wh
);
417 if ( wx
+ ww
> maxX
)
419 if ( wy
+ wh
> maxY
)
424 return wxSize(maxX
+ 7, maxY
+ 14);
428 // for a generic window there is no natural best size - just use the
434 // set the min/max size of the window
435 void wxWindowBase::SetSizeHints(int minW
, int minH
,
437 int WXUNUSED(incW
), int WXUNUSED(incH
))
445 // ----------------------------------------------------------------------------
446 // show/hide/enable/disable the window
447 // ----------------------------------------------------------------------------
449 bool wxWindowBase::Show(bool show
)
451 if ( show
!= m_isShown
)
463 bool wxWindowBase::Enable(bool enable
)
465 if ( enable
!= m_isEnabled
)
467 m_isEnabled
= enable
;
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 bool wxWindowBase::IsTopLevel() const
485 // ----------------------------------------------------------------------------
486 // reparenting the window
487 // ----------------------------------------------------------------------------
489 void wxWindowBase::AddChild(wxWindowBase
*child
)
491 wxCHECK_RET( child
, wxT("can't add a NULL child") );
493 GetChildren().Append(child
);
494 child
->SetParent(this);
497 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
499 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
501 GetChildren().DeleteObject(child
);
502 child
->SetParent((wxWindow
*)NULL
);
505 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
507 wxWindow
*oldParent
= GetParent();
508 if ( newParent
== oldParent
)
514 // unlink this window from the existing parent.
517 oldParent
->RemoveChild(this);
521 wxTopLevelWindows
.DeleteObject(this);
524 // add it to the new one
527 newParent
->AddChild(this);
531 wxTopLevelWindows
.Append(this);
537 // ----------------------------------------------------------------------------
538 // event handler stuff
539 // ----------------------------------------------------------------------------
541 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
543 handler
->SetNextHandler(GetEventHandler());
544 SetEventHandler(handler
);
547 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
549 wxEvtHandler
*handlerA
= GetEventHandler();
552 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
553 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
554 SetEventHandler(handlerB
);
558 handlerA
= (wxEvtHandler
*)NULL
;
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
571 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
574 m_backgroundColour
= colour
;
579 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
581 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
584 m_foregroundColour
= colour
;
589 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
591 // setting an invalid cursor is ok, it means that we don't have any special
593 if ( m_cursor
== cursor
)
604 bool wxWindowBase::SetFont(const wxFont
& font
)
606 // don't try to set invalid font, always fall back to the default
607 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
609 if ( (wxFont
&)fontOk
== m_font
)
621 void wxWindowBase::SetCaret(wxCaret
*caret
)
632 wxASSERT_MSG( m_caret
->GetWindow() == this,
633 wxT("caret should be created associated to this window") );
636 #endif // wxUSE_CARET
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 void wxWindowBase::SetValidator(const wxValidator
& validator
)
645 if ( m_windowValidator
)
646 delete m_windowValidator
;
648 m_windowValidator
= (wxValidator
*)validator
.Clone();
650 if ( m_windowValidator
)
651 m_windowValidator
->SetWindow(this) ;
653 #endif // wxUSE_VALIDATORS
655 // ----------------------------------------------------------------------------
656 // update region testing
657 // ----------------------------------------------------------------------------
659 bool wxWindowBase::IsExposed(int x
, int y
) const
661 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
664 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
666 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
669 // ----------------------------------------------------------------------------
670 // find window by id or name
671 // ----------------------------------------------------------------------------
673 wxWindow
*wxWindowBase::FindWindow( long id
)
675 if ( id
== m_windowId
)
676 return (wxWindow
*)this;
678 wxWindowBase
*res
= (wxWindow
*)NULL
;
679 wxWindowList::Node
*node
;
680 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
682 wxWindowBase
*child
= node
->GetData();
683 res
= child
->FindWindow( id
);
686 return (wxWindow
*)res
;
689 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
691 if ( name
== m_windowName
)
692 return (wxWindow
*)this;
694 wxWindowBase
*res
= (wxWindow
*)NULL
;
695 wxWindowList::Node
*node
;
696 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
698 wxWindow
*child
= node
->GetData();
699 res
= child
->FindWindow(name
);
702 return (wxWindow
*)res
;
705 // ----------------------------------------------------------------------------
706 // dialog oriented functions
707 // ----------------------------------------------------------------------------
709 void wxWindowBase::MakeModal(bool modal
)
711 // Disable all other windows
714 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
717 wxWindow
*win
= node
->GetData();
721 node
= node
->GetNext();
726 bool wxWindowBase::Validate()
729 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
731 wxWindowList::Node
*node
;
732 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
734 wxWindowBase
*child
= node
->GetData();
735 wxValidator
*validator
= child
->GetValidator();
736 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
741 if ( recurse
&& !child
->Validate() )
746 #endif // wxUSE_VALIDATORS
751 bool wxWindowBase::TransferDataToWindow()
754 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
756 wxWindowList::Node
*node
;
757 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
759 wxWindowBase
*child
= node
->GetData();
760 wxValidator
*validator
= child
->GetValidator();
761 if ( validator
&& !validator
->TransferToWindow() )
763 wxLogWarning(_("Could not transfer data to window"));
764 wxLog::FlushActive();
771 if ( !child
->TransferDataToWindow() )
773 // warning already given
778 #endif // wxUSE_VALIDATORS
783 bool wxWindowBase::TransferDataFromWindow()
786 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
788 wxWindowList::Node
*node
;
789 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
791 wxWindow
*child
= node
->GetData();
792 wxValidator
*validator
= child
->GetValidator();
793 if ( validator
&& !validator
->TransferFromWindow() )
795 // nop warning here because the application is supposed to give
796 // one itself - we don't know here what might have gone wrongly
803 if ( !child
->TransferDataFromWindow() )
805 // warning already given
810 #endif // wxUSE_VALIDATORS
815 void wxWindowBase::InitDialog()
817 wxInitDialogEvent
event(GetId());
818 event
.SetEventObject( this );
819 GetEventHandler()->ProcessEvent(event
);
822 // ----------------------------------------------------------------------------
824 // ----------------------------------------------------------------------------
828 void wxWindowBase::SetToolTip( const wxString
&tip
)
830 // don't create the new tooltip if we already have one
833 m_tooltip
->SetTip( tip
);
837 SetToolTip( new wxToolTip( tip
) );
840 // setting empty tooltip text does not remove the tooltip any more - use
841 // SetToolTip((wxToolTip *)NULL) for this
844 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
852 #endif // wxUSE_TOOLTIPS
854 // ----------------------------------------------------------------------------
855 // constraints and sizers
856 // ----------------------------------------------------------------------------
858 #if wxUSE_CONSTRAINTS
860 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
864 UnsetConstraints(m_constraints
);
865 delete m_constraints
;
867 m_constraints
= constraints
;
870 // Make sure other windows know they're part of a 'meaningful relationship'
871 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
872 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
873 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
874 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
875 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
876 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
877 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
878 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
879 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
880 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
881 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
882 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
883 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
884 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
885 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
886 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
890 // This removes any dangling pointers to this window in other windows'
891 // constraintsInvolvedIn lists.
892 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
896 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
897 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
898 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
899 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
900 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
901 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
902 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
903 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
904 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
905 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
906 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
907 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
908 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
909 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
910 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
911 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
915 // Back-pointer to other windows we're involved with, so if we delete this
916 // window, we must delete any constraints we're involved with.
917 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
919 if ( !m_constraintsInvolvedIn
)
920 m_constraintsInvolvedIn
= new wxWindowList
;
921 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
922 m_constraintsInvolvedIn
->Append(otherWin
);
925 // REMOVE back-pointer to other windows we're involved with.
926 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
928 if ( m_constraintsInvolvedIn
)
929 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
932 // Reset any constraints that mention this window
933 void wxWindowBase::DeleteRelatedConstraints()
935 if ( m_constraintsInvolvedIn
)
937 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
940 wxWindow
*win
= node
->GetData();
941 wxLayoutConstraints
*constr
= win
->GetConstraints();
943 // Reset any constraints involving this window
946 constr
->left
.ResetIfWin(this);
947 constr
->top
.ResetIfWin(this);
948 constr
->right
.ResetIfWin(this);
949 constr
->bottom
.ResetIfWin(this);
950 constr
->width
.ResetIfWin(this);
951 constr
->height
.ResetIfWin(this);
952 constr
->centreX
.ResetIfWin(this);
953 constr
->centreY
.ResetIfWin(this);
956 wxWindowList::Node
*next
= node
->GetNext();
961 delete m_constraintsInvolvedIn
;
962 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
966 void wxWindowBase::SetSizer(wxSizer
*sizer
)
968 if (m_windowSizer
) delete m_windowSizer
;
970 m_windowSizer
= sizer
;
973 bool wxWindowBase::Layout()
975 // If there is a sizer, use it instead of the constraints
979 GetClientSize(&w
, &h
);
981 GetSizer()->SetDimension( 0, 0, w
, h
);
985 wxLayoutConstraints
*constr
= GetConstraints();
986 bool wasOk
= constr
&& constr
->AreSatisfied();
988 ResetConstraints(); // Mark all constraints as unevaluated
990 // if we're a top level panel (i.e. our parent is frame/dialog), our
991 // own constraints will never be satisfied any more unless we do it
996 while ( noChanges
> 0 )
998 constr
->SatisfyConstraints(this, &noChanges
);
1002 DoPhase(1); // Layout children
1003 DoPhase(2); // Layout grand children
1004 SetConstraintSizes(); // Recursively set the real window sizes
1011 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1012 // do a similar thing, but also impose their own 'constraints' and order the
1013 // evaluation differently.
1014 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1016 wxLayoutConstraints
*constr
= GetConstraints();
1019 return constr
->SatisfyConstraints(this, noChanges
);
1025 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1035 // Do a phase of evaluating child constraints
1036 bool wxWindowBase::DoPhase(int phase
)
1038 int noIterations
= 0;
1039 int maxIterations
= 500;
1042 wxWindowList succeeded
;
1043 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1047 wxWindowList::Node
*node
= GetChildren().GetFirst();
1050 wxWindow
*child
= node
->GetData();
1051 if ( !child
->IsTopLevel() )
1053 wxLayoutConstraints
*constr
= child
->GetConstraints();
1056 if ( !succeeded
.Find(child
) )
1058 int tempNoChanges
= 0;
1059 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1060 noChanges
+= tempNoChanges
;
1063 succeeded
.Append(child
);
1068 node
= node
->GetNext();
1077 void wxWindowBase::ResetConstraints()
1079 wxLayoutConstraints
*constr
= GetConstraints();
1082 constr
->left
.SetDone(FALSE
);
1083 constr
->top
.SetDone(FALSE
);
1084 constr
->right
.SetDone(FALSE
);
1085 constr
->bottom
.SetDone(FALSE
);
1086 constr
->width
.SetDone(FALSE
);
1087 constr
->height
.SetDone(FALSE
);
1088 constr
->centreX
.SetDone(FALSE
);
1089 constr
->centreY
.SetDone(FALSE
);
1092 wxWindowList::Node
*node
= GetChildren().GetFirst();
1095 wxWindow
*win
= node
->GetData();
1096 if ( !win
->IsTopLevel() )
1097 win
->ResetConstraints();
1098 node
= node
->GetNext();
1102 // Need to distinguish between setting the 'fake' size for windows and sizers,
1103 // and setting the real values.
1104 void wxWindowBase::SetConstraintSizes(bool recurse
)
1106 wxLayoutConstraints
*constr
= GetConstraints();
1107 if ( constr
&& constr
->AreSatisfied() )
1109 int x
= constr
->left
.GetValue();
1110 int y
= constr
->top
.GetValue();
1111 int w
= constr
->width
.GetValue();
1112 int h
= constr
->height
.GetValue();
1114 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1115 (constr
->height
.GetRelationship() != wxAsIs
) )
1117 SetSize(x
, y
, w
, h
);
1121 // If we don't want to resize this window, just move it...
1127 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1128 GetClassInfo()->GetClassName(),
1134 wxWindowList::Node
*node
= GetChildren().GetFirst();
1137 wxWindow
*win
= node
->GetData();
1138 if ( !win
->IsTopLevel() )
1139 win
->SetConstraintSizes();
1140 node
= node
->GetNext();
1145 // Only set the size/position of the constraint (if any)
1146 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1148 wxLayoutConstraints
*constr
= GetConstraints();
1153 constr
->left
.SetValue(x
);
1154 constr
->left
.SetDone(TRUE
);
1158 constr
->top
.SetValue(y
);
1159 constr
->top
.SetDone(TRUE
);
1163 constr
->width
.SetValue(w
);
1164 constr
->width
.SetDone(TRUE
);
1168 constr
->height
.SetValue(h
);
1169 constr
->height
.SetDone(TRUE
);
1174 void wxWindowBase::MoveConstraint(int x
, int y
)
1176 wxLayoutConstraints
*constr
= GetConstraints();
1181 constr
->left
.SetValue(x
);
1182 constr
->left
.SetDone(TRUE
);
1186 constr
->top
.SetValue(y
);
1187 constr
->top
.SetDone(TRUE
);
1192 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1194 wxLayoutConstraints
*constr
= GetConstraints();
1197 *w
= constr
->width
.GetValue();
1198 *h
= constr
->height
.GetValue();
1204 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1206 wxLayoutConstraints
*constr
= GetConstraints();
1209 *w
= constr
->width
.GetValue();
1210 *h
= constr
->height
.GetValue();
1213 GetClientSize(w
, h
);
1216 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1218 wxLayoutConstraints
*constr
= GetConstraints();
1221 *x
= constr
->left
.GetValue();
1222 *y
= constr
->top
.GetValue();
1228 #endif // wxUSE_CONSTRAINTS
1230 // ----------------------------------------------------------------------------
1231 // do Update UI processing for child controls
1232 // ----------------------------------------------------------------------------
1234 // TODO: should this be implemented for the child window rather
1235 // than the parent? Then you can override it e.g. for wxCheckBox
1236 // to do the Right Thing rather than having to assume a fixed number
1237 // of control classes.
1238 void wxWindowBase::UpdateWindowUI()
1240 wxUpdateUIEvent
event(GetId());
1241 event
.m_eventObject
= this;
1243 if ( GetEventHandler()->ProcessEvent(event
) )
1245 if ( event
.GetSetEnabled() )
1246 Enable(event
.GetEnabled());
1248 if ( event
.GetSetText() )
1250 wxControl
*control
= wxDynamicCast(this, wxControl
);
1253 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1255 text
->SetValue(event
.GetText());
1257 control
->SetLabel(event
.GetText());
1262 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1265 if ( event
.GetSetChecked() )
1266 checkbox
->SetValue(event
.GetChecked());
1268 #endif // wxUSE_CHECKBOX
1271 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1274 if ( event
.GetSetChecked() )
1275 radiobtn
->SetValue(event
.GetChecked());
1277 #endif // wxUSE_RADIOBTN
1281 // ----------------------------------------------------------------------------
1282 // dialog units translations
1283 // ----------------------------------------------------------------------------
1285 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1287 int charWidth
= GetCharWidth();
1288 int charHeight
= GetCharHeight();
1289 wxPoint
pt2(-1, -1);
1291 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1293 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1298 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1300 int charWidth
= GetCharWidth();
1301 int charHeight
= GetCharHeight();
1302 wxPoint
pt2(-1, -1);
1304 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1306 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1317 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1318 wxT("can't have both object and void client data") );
1320 if ( m_clientObject
)
1321 delete m_clientObject
;
1323 m_clientObject
= data
;
1324 m_clientDataType
= ClientData_Object
;
1327 wxClientData
*wxWindowBase::DoGetClientObject() const
1329 // it's not an error to call GetClientObject() on a window which doesn't
1330 // have client data at all - NULL will be returned
1331 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1332 wxT("this window doesn't have object client data") );
1334 return m_clientObject
;
1337 void wxWindowBase::DoSetClientData( void *data
)
1339 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1340 wxT("can't have both object and void client data") );
1342 m_clientData
= data
;
1343 m_clientDataType
= ClientData_Void
;
1346 void *wxWindowBase::DoGetClientData() const
1348 // it's not an error to call GetClientData() on a window which doesn't have
1349 // client data at all - NULL will be returned
1350 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1351 wxT("this window doesn't have void client data") );
1353 return m_clientData
;
1356 // ----------------------------------------------------------------------------
1358 // ----------------------------------------------------------------------------
1360 // propagate the colour change event to the subwindows
1361 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1363 wxWindowList::Node
*node
= GetChildren().GetFirst();
1366 // Only propagate to non-top-level windows
1367 wxWindow
*win
= node
->GetData();
1368 if ( !win
->IsTopLevel() )
1370 wxSysColourChangedEvent event2
;
1371 event
.m_eventObject
= win
;
1372 win
->GetEventHandler()->ProcessEvent(event2
);
1375 node
= node
->GetNext();
1379 // the default action is to populate dialog with data when it's created
1380 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1382 TransferDataToWindow();
1385 // process Ctrl-Alt-mclick
1386 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1388 if ( event
.ControlDown() && event
.AltDown() )
1390 // don't translate these strings
1392 switch ( wxGetOsVersion() )
1394 case wxMOTIF_X
: port
= _T("Motif"); break;
1395 case wxMACINTOSH
: port
= _T("Mac"); break;
1396 case wxBEOS
: port
= _T("BeOS"); break;
1400 case wxGTK_BEOS
: port
= _T("GTK"); break;
1406 case wxWIN386
: port
= _T("MS Windows"); break;
1410 case wxMGL_OS2
: port
= _T("MGL"); break;
1412 case wxOS2_PM
: port
= _T("OS/2"); break;
1413 default: port
= _T("unknown"); break;
1416 wxMessageBox(wxString::Format(
1418 " wxWindows Library (%s port)\n"
1419 "Version %u.%u.%u, compiled at %s %s\n"
1420 " Copyright (c) 1995-2000 wxWindows team"
1429 _T("wxWindows information"),
1430 wxICON_INFORMATION
| wxOK
,
1439 // ----------------------------------------------------------------------------
1440 // list classes implementation
1441 // ----------------------------------------------------------------------------
1443 void wxWindowListNode::DeleteData()
1445 delete (wxWindow
*)GetData();