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"
47 #include "wx/layout.h"
49 #endif // wxUSE_CONSTRAINTS
51 #if wxUSE_DRAG_AND_DROP
53 #endif // wxUSE_DRAG_AND_DROP
56 #include "wx/tooltip.h"
57 #endif // wxUSE_TOOLTIPS
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 int wxWindowBase::ms_lastControlId
= -200;
69 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
76 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
77 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
78 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
81 // ============================================================================
82 // implementation of the common functionality of the wxWindow class
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // the default initialization
90 void wxWindowBase::InitBase()
92 // no window yet, no parent nor children
93 m_parent
= (wxWindow
*)NULL
;
95 m_children
.DeleteContents( FALSE
); // don't auto delete node data
97 // no constraints on the minimal window size
103 // window is created enabled but it's not visible yet
107 // no client data (yet)
109 m_clientDataType
= ClientData_None
;
111 // the default event handler is just this window
112 m_eventHandler
= this;
116 m_windowValidator
= (wxValidator
*) NULL
;
117 #endif // wxUSE_VALIDATORS
119 // use the system default colours
120 wxSystemSettings settings
;
122 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
123 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
124 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
126 #if !defined(__WXMAC__) && !defined(__WXGTK__)
127 m_font
= *wxSWISS_FONT
; // and this?
129 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
136 // an optimization for the event processing: checking this flag is much
137 // faster than using IsKindOf(CLASSINFO(wxWindow))
140 #if wxUSE_CONSTRAINTS
141 // no constraints whatsoever
142 m_constraints
= (wxLayoutConstraints
*) NULL
;
143 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
144 m_windowSizer
= (wxSizer
*) NULL
;
145 m_autoLayout
= FALSE
;
146 #endif // wxUSE_CONSTRAINTS
148 #if wxUSE_DRAG_AND_DROP
149 m_dropTarget
= (wxDropTarget
*)NULL
;
150 #endif // wxUSE_DRAG_AND_DROP
153 m_tooltip
= (wxToolTip
*)NULL
;
154 #endif // wxUSE_TOOLTIPS
157 m_caret
= (wxCaret
*)NULL
;
158 #endif // wxUSE_CARET
161 // common part of window creation process
162 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
164 const wxPoint
& WXUNUSED(pos
),
165 const wxSize
& WXUNUSED(size
),
167 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
;
180 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
181 // have it too - like this it's possible to set it only in the top level
182 // dialog/frame and all children will inherit it by defult
183 if ( parent
&& (parent
->GetWindowStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
185 style
|= wxWS_EX_VALIDATE_RECURSIVELY
;
188 SetWindowStyleFlag(style
);
192 SetValidator(validator
);
193 #endif // wxUSE_VALIDATORS
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() )
400 // dialogs and frames lie in different top level windows -
401 // don't deal with them here
406 win
->GetPosition(&wx
, &wy
);
407 win
->GetSize(&ww
, &wh
);
408 if ( wx
+ ww
> maxX
)
410 if ( wy
+ wh
> maxY
)
415 return wxSize(maxX
+ 7, maxY
+ 14);
419 // for a generic window there is no natural best size - just use the
425 // set the min/max size of the window
426 void wxWindowBase::SetSizeHints(int minW
, int minH
,
428 int WXUNUSED(incW
), int WXUNUSED(incH
))
436 // ----------------------------------------------------------------------------
437 // show/hide/enable/disable the window
438 // ----------------------------------------------------------------------------
440 bool wxWindowBase::Show(bool show
)
442 if ( show
!= m_isShown
)
454 bool wxWindowBase::Enable(bool enable
)
456 if ( enable
!= m_isEnabled
)
458 m_isEnabled
= enable
;
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 bool wxWindowBase::IsTopLevel() const
476 // ----------------------------------------------------------------------------
477 // reparenting the window
478 // ----------------------------------------------------------------------------
480 void wxWindowBase::AddChild(wxWindowBase
*child
)
482 wxCHECK_RET( child
, wxT("can't add a NULL child") );
484 GetChildren().Append(child
);
485 child
->SetParent(this);
488 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
490 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
492 GetChildren().DeleteObject(child
);
493 child
->SetParent((wxWindow
*)NULL
);
496 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
498 wxWindow
*oldParent
= GetParent();
499 if ( newParent
== oldParent
)
505 // unlink this window from the existing parent.
508 oldParent
->RemoveChild(this);
512 wxTopLevelWindows
.DeleteObject(this);
515 // add it to the new one
518 newParent
->AddChild(this);
522 wxTopLevelWindows
.Append(this);
528 // ----------------------------------------------------------------------------
529 // event handler stuff
530 // ----------------------------------------------------------------------------
532 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
534 handler
->SetNextHandler(GetEventHandler());
535 SetEventHandler(handler
);
538 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
540 wxEvtHandler
*handlerA
= GetEventHandler();
543 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
544 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
545 SetEventHandler(handlerB
);
549 handlerA
= (wxEvtHandler
*)NULL
;
556 // ----------------------------------------------------------------------------
558 // ----------------------------------------------------------------------------
560 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
562 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
565 m_backgroundColour
= colour
;
570 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
572 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
575 m_foregroundColour
= colour
;
580 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
582 // don't try to set invalid cursor, always fall back to the default
583 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
585 if ( (wxCursor
&)cursorOk
== m_cursor
)
596 bool wxWindowBase::SetFont(const wxFont
& font
)
598 // don't try to set invalid font, always fall back to the default
599 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
601 if ( (wxFont
&)fontOk
== m_font
)
613 void wxWindowBase::SetCaret(wxCaret
*caret
)
624 wxASSERT_MSG( m_caret
->GetWindow() == this,
625 wxT("caret should be created associated to this window") );
628 #endif // wxUSE_CARET
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 void wxWindowBase::SetValidator(const wxValidator
& validator
)
637 if ( m_windowValidator
)
638 delete m_windowValidator
;
640 m_windowValidator
= (wxValidator
*)validator
.Clone();
642 if ( m_windowValidator
)
643 m_windowValidator
->SetWindow(this) ;
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 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
723 wxWindowList::Node
*node
;
724 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
726 wxWindowBase
*child
= node
->GetData();
727 wxValidator
*validator
= child
->GetValidator();
728 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
733 if ( recurse
&& !child
->Validate() )
738 #endif // wxUSE_VALIDATORS
743 bool wxWindowBase::TransferDataToWindow()
746 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
748 wxWindowList::Node
*node
;
749 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
751 wxWindowBase
*child
= node
->GetData();
752 wxValidator
*validator
= child
->GetValidator();
753 if ( validator
&& !validator
->TransferToWindow() )
755 wxLogWarning(_("Could not transfer data to window"));
756 wxLog::FlushActive();
763 if ( !child
->TransferDataToWindow() )
765 // warning already given
770 #endif // wxUSE_VALIDATORS
775 bool wxWindowBase::TransferDataFromWindow()
778 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
780 wxWindowList::Node
*node
;
781 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
783 wxWindow
*child
= node
->GetData();
784 wxValidator
*validator
= child
->GetValidator();
785 if ( validator
&& !validator
->TransferFromWindow() )
787 // nop warning here because the application is supposed to give
788 // one itself - we don't know here what might have gone wrongly
795 if ( !child
->TransferDataFromWindow() )
797 // warning already given
802 #endif // wxUSE_VALIDATORS
807 void wxWindowBase::InitDialog()
809 wxInitDialogEvent
event(GetId());
810 event
.SetEventObject( this );
811 GetEventHandler()->ProcessEvent(event
);
814 // ----------------------------------------------------------------------------
816 // ----------------------------------------------------------------------------
820 void wxWindowBase::SetToolTip( const wxString
&tip
)
822 // don't create the new tooltip if we already have one
825 m_tooltip
->SetTip( tip
);
829 SetToolTip( new wxToolTip( tip
) );
832 // setting empty tooltip text does not remove the tooltip any more - use
833 // SetToolTip((wxToolTip *)NULL) for this
836 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
844 #endif // wxUSE_TOOLTIPS
846 // ----------------------------------------------------------------------------
847 // constraints and sizers
848 // ----------------------------------------------------------------------------
850 #if wxUSE_CONSTRAINTS
852 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
856 UnsetConstraints(m_constraints
);
857 delete m_constraints
;
859 m_constraints
= constraints
;
862 // Make sure other windows know they're part of a 'meaningful relationship'
863 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
864 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
865 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
866 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
867 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
868 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
869 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
870 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
871 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
872 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
873 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
874 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
875 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
876 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
877 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
878 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
882 // This removes any dangling pointers to this window in other windows'
883 // constraintsInvolvedIn lists.
884 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
888 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
889 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
890 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
891 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
892 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
893 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
894 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
895 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
896 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
897 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
898 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
899 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
900 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
901 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
902 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
903 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
907 // Back-pointer to other windows we're involved with, so if we delete this
908 // window, we must delete any constraints we're involved with.
909 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
911 if ( !m_constraintsInvolvedIn
)
912 m_constraintsInvolvedIn
= new wxWindowList
;
913 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
914 m_constraintsInvolvedIn
->Append(otherWin
);
917 // REMOVE back-pointer to other windows we're involved with.
918 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
920 if ( m_constraintsInvolvedIn
)
921 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
924 // Reset any constraints that mention this window
925 void wxWindowBase::DeleteRelatedConstraints()
927 if ( m_constraintsInvolvedIn
)
929 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
932 wxWindow
*win
= node
->GetData();
933 wxLayoutConstraints
*constr
= win
->GetConstraints();
935 // Reset any constraints involving this window
938 constr
->left
.ResetIfWin(this);
939 constr
->top
.ResetIfWin(this);
940 constr
->right
.ResetIfWin(this);
941 constr
->bottom
.ResetIfWin(this);
942 constr
->width
.ResetIfWin(this);
943 constr
->height
.ResetIfWin(this);
944 constr
->centreX
.ResetIfWin(this);
945 constr
->centreY
.ResetIfWin(this);
948 wxWindowList::Node
*next
= node
->GetNext();
953 delete m_constraintsInvolvedIn
;
954 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
958 void wxWindowBase::SetSizer(wxSizer
*sizer
)
960 if (m_windowSizer
) delete m_windowSizer
;
962 m_windowSizer
= sizer
;
965 bool wxWindowBase::Layout()
967 // If there is a sizer, use it instead of the constraints
971 GetClientSize(&w
, &h
);
973 GetSizer()->SetDimension( 0, 0, w
, h
);
977 wxLayoutConstraints
*constr
= GetConstraints();
978 bool wasOk
= constr
&& constr
->AreSatisfied();
980 ResetConstraints(); // Mark all constraints as unevaluated
982 // if we're a top level panel (i.e. our parent is frame/dialog), our
983 // own constraints will never be satisfied any more unless we do it
988 while ( noChanges
> 0 )
990 constr
->SatisfyConstraints(this, &noChanges
);
994 DoPhase(1); // Layout children
995 DoPhase(2); // Layout grand children
996 SetConstraintSizes(); // Recursively set the real window sizes
1003 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1004 // do a similar thing, but also impose their own 'constraints' and order the
1005 // evaluation differently.
1006 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1008 wxLayoutConstraints
*constr
= GetConstraints();
1011 return constr
->SatisfyConstraints(this, noChanges
);
1017 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1027 // Do a phase of evaluating child constraints
1028 bool wxWindowBase::DoPhase(int phase
)
1030 int noIterations
= 0;
1031 int maxIterations
= 500;
1034 wxWindowList succeeded
;
1035 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1039 wxWindowList::Node
*node
= GetChildren().GetFirst();
1042 wxWindow
*child
= node
->GetData();
1043 if ( !child
->IsTopLevel() )
1045 wxLayoutConstraints
*constr
= child
->GetConstraints();
1048 if ( !succeeded
.Find(child
) )
1050 int tempNoChanges
= 0;
1051 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1052 noChanges
+= tempNoChanges
;
1055 succeeded
.Append(child
);
1060 node
= node
->GetNext();
1069 void wxWindowBase::ResetConstraints()
1071 wxLayoutConstraints
*constr
= GetConstraints();
1074 constr
->left
.SetDone(FALSE
);
1075 constr
->top
.SetDone(FALSE
);
1076 constr
->right
.SetDone(FALSE
);
1077 constr
->bottom
.SetDone(FALSE
);
1078 constr
->width
.SetDone(FALSE
);
1079 constr
->height
.SetDone(FALSE
);
1080 constr
->centreX
.SetDone(FALSE
);
1081 constr
->centreY
.SetDone(FALSE
);
1084 wxWindowList::Node
*node
= GetChildren().GetFirst();
1087 wxWindow
*win
= node
->GetData();
1088 if ( !win
->IsTopLevel() )
1089 win
->ResetConstraints();
1090 node
= node
->GetNext();
1094 // Need to distinguish between setting the 'fake' size for windows and sizers,
1095 // and setting the real values.
1096 void wxWindowBase::SetConstraintSizes(bool recurse
)
1098 wxLayoutConstraints
*constr
= GetConstraints();
1099 if ( constr
&& constr
->AreSatisfied() )
1101 int x
= constr
->left
.GetValue();
1102 int y
= constr
->top
.GetValue();
1103 int w
= constr
->width
.GetValue();
1104 int h
= constr
->height
.GetValue();
1106 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1107 (constr
->height
.GetRelationship() != wxAsIs
) )
1109 SetSize(x
, y
, w
, h
);
1113 // If we don't want to resize this window, just move it...
1119 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1120 GetClassInfo()->GetClassName(),
1126 wxWindowList::Node
*node
= GetChildren().GetFirst();
1129 wxWindow
*win
= node
->GetData();
1130 if ( !win
->IsTopLevel() )
1131 win
->SetConstraintSizes();
1132 node
= node
->GetNext();
1137 // Only set the size/position of the constraint (if any)
1138 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1140 wxLayoutConstraints
*constr
= GetConstraints();
1145 constr
->left
.SetValue(x
);
1146 constr
->left
.SetDone(TRUE
);
1150 constr
->top
.SetValue(y
);
1151 constr
->top
.SetDone(TRUE
);
1155 constr
->width
.SetValue(w
);
1156 constr
->width
.SetDone(TRUE
);
1160 constr
->height
.SetValue(h
);
1161 constr
->height
.SetDone(TRUE
);
1166 void wxWindowBase::MoveConstraint(int x
, int y
)
1168 wxLayoutConstraints
*constr
= GetConstraints();
1173 constr
->left
.SetValue(x
);
1174 constr
->left
.SetDone(TRUE
);
1178 constr
->top
.SetValue(y
);
1179 constr
->top
.SetDone(TRUE
);
1184 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1186 wxLayoutConstraints
*constr
= GetConstraints();
1189 *w
= constr
->width
.GetValue();
1190 *h
= constr
->height
.GetValue();
1196 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1198 wxLayoutConstraints
*constr
= GetConstraints();
1201 *w
= constr
->width
.GetValue();
1202 *h
= constr
->height
.GetValue();
1205 GetClientSize(w
, h
);
1208 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1210 wxLayoutConstraints
*constr
= GetConstraints();
1213 *x
= constr
->left
.GetValue();
1214 *y
= constr
->top
.GetValue();
1220 #endif // wxUSE_CONSTRAINTS
1222 // ----------------------------------------------------------------------------
1223 // do Update UI processing for child controls
1224 // ----------------------------------------------------------------------------
1226 // TODO: should this be implemented for the child window rather
1227 // than the parent? Then you can override it e.g. for wxCheckBox
1228 // to do the Right Thing rather than having to assume a fixed number
1229 // of control classes.
1230 void wxWindowBase::UpdateWindowUI()
1232 wxUpdateUIEvent
event(GetId());
1233 event
.m_eventObject
= this;
1235 if ( GetEventHandler()->ProcessEvent(event
) )
1237 if ( event
.GetSetEnabled() )
1238 Enable(event
.GetEnabled());
1240 if ( event
.GetSetText() )
1242 wxControl
*control
= wxDynamicCast(this, wxControl
);
1245 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1247 text
->SetValue(event
.GetText());
1249 control
->SetLabel(event
.GetText());
1254 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1257 if ( event
.GetSetChecked() )
1258 checkbox
->SetValue(event
.GetChecked());
1260 #endif // wxUSE_CHECKBOX
1263 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1266 if ( event
.GetSetChecked() )
1267 radiobtn
->SetValue(event
.GetChecked());
1269 #endif // wxUSE_RADIOBTN
1273 // ----------------------------------------------------------------------------
1274 // dialog units translations
1275 // ----------------------------------------------------------------------------
1277 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1279 int charWidth
= GetCharWidth();
1280 int charHeight
= GetCharHeight();
1281 wxPoint
pt2(-1, -1);
1283 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1285 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1290 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1292 int charWidth
= GetCharWidth();
1293 int charHeight
= GetCharHeight();
1294 wxPoint
pt2(-1, -1);
1296 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1298 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1303 // ----------------------------------------------------------------------------
1305 // ----------------------------------------------------------------------------
1307 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1309 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1310 wxT("can't have both object and void client data") );
1312 if ( m_clientObject
)
1313 delete m_clientObject
;
1315 m_clientObject
= data
;
1316 m_clientDataType
= ClientData_Object
;
1319 wxClientData
*wxWindowBase::DoGetClientObject() const
1321 // it's not an error to call GetClientObject() on a window which doesn't
1322 // have client data at all - NULL will be returned
1323 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1324 wxT("this window doesn't have object client data") );
1326 return m_clientObject
;
1329 void wxWindowBase::DoSetClientData( void *data
)
1331 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1332 wxT("can't have both object and void client data") );
1334 m_clientData
= data
;
1335 m_clientDataType
= ClientData_Void
;
1338 void *wxWindowBase::DoGetClientData() const
1340 // it's not an error to call GetClientData() on a window which doesn't have
1341 // client data at all - NULL will be returned
1342 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1343 wxT("this window doesn't have void client data") );
1345 return m_clientData
;
1348 // ----------------------------------------------------------------------------
1350 // ----------------------------------------------------------------------------
1352 // propagate the colour change event to the subwindows
1353 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1355 wxWindowList::Node
*node
= GetChildren().GetFirst();
1358 // Only propagate to non-top-level windows
1359 wxWindow
*win
= node
->GetData();
1360 if ( !win
->IsTopLevel() )
1362 wxSysColourChangedEvent event2
;
1363 event
.m_eventObject
= win
;
1364 win
->GetEventHandler()->ProcessEvent(event2
);
1367 node
= node
->GetNext();
1371 // the default action is to populate dialog with data when it's created
1372 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1374 TransferDataToWindow();
1377 // process Ctrl-Alt-mclick
1378 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1380 if ( event
.ControlDown() && event
.AltDown() )
1382 // don't translate these strings
1384 switch ( wxGetOsVersion() )
1386 case wxMOTIF_X
: port
= _T("Motif"); break;
1387 case wxMACINTOSH
: port
= _T("Mac"); break;
1388 case wxBEOS
: port
= _T("BeOS"); break;
1392 case wxGTK_BEOS
: port
= _T("GTK"); break;
1398 case wxWIN386
: port
= _T("MS Windows"); break;
1402 case wxMGL_OS2
: port
= _T("MGL"); break;
1404 case wxOS2_PM
: port
= _T("OS/2"); break;
1405 default: port
= _T("unknown"); break;
1408 wxMessageBox(wxString::Format(
1410 " wxWindows Library (%s port)\n"
1411 "Version %u.%u.%u, compiled at %s %s\n"
1412 " Copyright (c) 1995-2000 wxWindows team"
1421 _T("wxWindows information"),
1422 wxICON_INFORMATION
| wxOK
,
1431 // ----------------------------------------------------------------------------
1432 // list classes implementation
1433 // ----------------------------------------------------------------------------
1435 void wxWindowListNode::DeleteData()
1437 delete (wxWindow
*)GetData();