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 // GRG, changed Mar/2000
127 #if 0 // !defined(__WXMAC__) && !defined(__WXGTK__)
128 m_font
= *wxSWISS_FONT
; // and this?
130 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
;
179 SetWindowStyleFlag(style
);
183 SetValidator(validator
);
184 #endif // wxUSE_VALIDATORS
186 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
187 // have it too - like this it's possible to set it only in the top level
188 // dialog/frame and all children will inherit it by defult
189 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
191 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
202 wxWindowBase::~wxWindowBase()
204 // FIXME if these 2 cases result from programming errors in the user code
205 // we should probably assert here instead of silently fixing them
207 // Just in case the window has been Closed, but we're then deleting
208 // immediately: don't leave dangling pointers.
209 wxPendingDelete
.DeleteObject(this);
211 // Just in case we've loaded a top-level window via LoadNativeDialog but
212 // we weren't a dialog class
213 wxTopLevelWindows
.DeleteObject(this);
215 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
217 // make sure that there are no dangling pointers left pointing to us
218 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
221 if ( panel
->GetLastFocus() == this )
223 panel
->SetLastFocus((wxWindow
*)NULL
);
230 #endif // wxUSE_CARET
233 if ( m_windowValidator
)
234 delete m_windowValidator
;
235 #endif // wxUSE_VALIDATORS
237 // we only delete object data, not untyped
238 if ( m_clientDataType
== ClientData_Object
)
239 delete m_clientObject
;
241 #if wxUSE_CONSTRAINTS
242 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
243 // at deleted windows as they delete themselves.
244 DeleteRelatedConstraints();
248 // This removes any dangling pointers to this window in other windows'
249 // constraintsInvolvedIn lists.
250 UnsetConstraints(m_constraints
);
251 delete m_constraints
;
252 m_constraints
= NULL
;
256 delete m_windowSizer
;
258 #endif // wxUSE_CONSTRAINTS
260 #if wxUSE_DRAG_AND_DROP
263 #endif // wxUSE_DRAG_AND_DROP
268 #endif // wxUSE_TOOLTIPS
271 bool wxWindowBase::Destroy()
278 bool wxWindowBase::Close(bool force
)
280 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
281 event
.SetEventObject(this);
282 #if WXWIN_COMPATIBILITY
283 event
.SetForce(force
);
284 #endif // WXWIN_COMPATIBILITY
285 event
.SetCanVeto(!force
);
287 // return FALSE if window wasn't closed because the application vetoed the
289 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
292 bool wxWindowBase::DestroyChildren()
294 wxWindowList::Node
*node
;
297 // we iterate until the list becomes empty
298 node
= GetChildren().GetFirst();
302 wxWindow
*child
= node
->GetData();
304 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
308 wxASSERT_MSG( !GetChildren().Find(child
),
309 wxT("child didn't remove itself using RemoveChild()") );
315 // ----------------------------------------------------------------------------
316 // size/position related methods
317 // ----------------------------------------------------------------------------
319 // centre the window with respect to its parent in either (or both) directions
320 void wxWindowBase::Centre(int direction
)
322 int widthParent
, heightParent
;
324 wxWindow
*parent
= GetParent();
328 direction
|= wxCENTRE_ON_SCREEN
;
331 if ( direction
& wxCENTRE_ON_SCREEN
)
333 // centre with respect to the whole screen
334 wxDisplaySize(&widthParent
, &heightParent
);
338 // centre inside the parents rectangle
339 parent
->GetClientSize(&widthParent
, &heightParent
);
343 GetSize(&width
, &height
);
348 if ( direction
& wxHORIZONTAL
)
349 xNew
= (widthParent
- width
)/2;
351 if ( direction
& wxVERTICAL
)
352 yNew
= (heightParent
- height
)/2;
354 // controls are always centered on their parent because it doesn't make
355 // sense to centre them on the screen
356 if ( !(direction
& wxCENTRE_ON_SCREEN
) || !IsTopLevel() )
358 // the only chance to get this is to have a not top level window
359 // without parent which shouldn't happen
360 wxCHECK_RET( parent
, wxT("this window must have a parent") );
362 // adjust to the parents client area origin
363 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
369 // move the centre of this window to this position
373 // fits the window around the children
374 void wxWindowBase::Fit()
376 if ( GetChildren().GetCount() > 0 )
378 SetClientSize(DoGetBestSize());
380 //else: do nothing if we have no children
383 // return the size best suited for the current window
384 wxSize
wxWindowBase::DoGetBestSize() const
386 if ( GetChildren().GetCount() > 0 )
388 // our minimal acceptable size is such that all our windows fit inside
392 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
394 node
= node
->GetNext() )
396 wxWindow
*win
= node
->GetData();
397 if ( win
->IsTopLevel() )
399 // dialogs and frames lie in different top level windows -
400 // don't deal with them here
405 win
->GetPosition(&wx
, &wy
);
407 // if the window hadn't been positioned yet, assume that it is in
414 win
->GetSize(&ww
, &wh
);
415 if ( wx
+ ww
> maxX
)
417 if ( wy
+ wh
> maxY
)
422 return wxSize(maxX
+ 7, maxY
+ 14);
426 // for a generic window there is no natural best size - just use the
432 // set the min/max size of the window
433 void wxWindowBase::SetSizeHints(int minW
, int minH
,
435 int WXUNUSED(incW
), int WXUNUSED(incH
))
443 // ----------------------------------------------------------------------------
444 // show/hide/enable/disable the window
445 // ----------------------------------------------------------------------------
447 bool wxWindowBase::Show(bool show
)
449 if ( show
!= m_isShown
)
461 bool wxWindowBase::Enable(bool enable
)
463 if ( enable
!= m_isEnabled
)
465 m_isEnabled
= enable
;
474 // ----------------------------------------------------------------------------
476 // ----------------------------------------------------------------------------
478 bool wxWindowBase::IsTopLevel() const
483 // ----------------------------------------------------------------------------
484 // reparenting the window
485 // ----------------------------------------------------------------------------
487 void wxWindowBase::AddChild(wxWindowBase
*child
)
489 wxCHECK_RET( child
, wxT("can't add a NULL child") );
491 GetChildren().Append(child
);
492 child
->SetParent(this);
495 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
497 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
499 GetChildren().DeleteObject(child
);
500 child
->SetParent((wxWindow
*)NULL
);
503 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
505 wxWindow
*oldParent
= GetParent();
506 if ( newParent
== oldParent
)
512 // unlink this window from the existing parent.
515 oldParent
->RemoveChild(this);
519 wxTopLevelWindows
.DeleteObject(this);
522 // add it to the new one
525 newParent
->AddChild(this);
529 wxTopLevelWindows
.Append(this);
535 // ----------------------------------------------------------------------------
536 // event handler stuff
537 // ----------------------------------------------------------------------------
539 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
541 handler
->SetNextHandler(GetEventHandler());
542 SetEventHandler(handler
);
545 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
547 wxEvtHandler
*handlerA
= GetEventHandler();
550 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
551 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
552 SetEventHandler(handlerB
);
556 handlerA
= (wxEvtHandler
*)NULL
;
563 // ----------------------------------------------------------------------------
565 // ----------------------------------------------------------------------------
567 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
569 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
572 m_backgroundColour
= colour
;
577 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
579 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
582 m_foregroundColour
= colour
;
587 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
589 // setting an invalid cursor is ok, it means that we don't have any special
591 if ( m_cursor
== cursor
)
602 bool wxWindowBase::SetFont(const wxFont
& font
)
604 // don't try to set invalid font, always fall back to the default
605 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
607 if ( (wxFont
&)fontOk
== m_font
)
619 void wxWindowBase::SetCaret(wxCaret
*caret
)
630 wxASSERT_MSG( m_caret
->GetWindow() == this,
631 wxT("caret should be created associated to this window") );
634 #endif // wxUSE_CARET
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
641 void wxWindowBase::SetValidator(const wxValidator
& validator
)
643 if ( m_windowValidator
)
644 delete m_windowValidator
;
646 m_windowValidator
= (wxValidator
*)validator
.Clone();
648 if ( m_windowValidator
)
649 m_windowValidator
->SetWindow(this) ;
651 #endif // wxUSE_VALIDATORS
653 // ----------------------------------------------------------------------------
654 // update region testing
655 // ----------------------------------------------------------------------------
657 bool wxWindowBase::IsExposed(int x
, int y
) const
659 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
662 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
664 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
667 // ----------------------------------------------------------------------------
668 // find window by id or name
669 // ----------------------------------------------------------------------------
671 wxWindow
*wxWindowBase::FindWindow( long id
)
673 if ( id
== m_windowId
)
674 return (wxWindow
*)this;
676 wxWindowBase
*res
= (wxWindow
*)NULL
;
677 wxWindowList::Node
*node
;
678 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
680 wxWindowBase
*child
= node
->GetData();
681 res
= child
->FindWindow( id
);
684 return (wxWindow
*)res
;
687 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
689 if ( name
== m_windowName
)
690 return (wxWindow
*)this;
692 wxWindowBase
*res
= (wxWindow
*)NULL
;
693 wxWindowList::Node
*node
;
694 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
696 wxWindow
*child
= node
->GetData();
697 res
= child
->FindWindow(name
);
700 return (wxWindow
*)res
;
703 // ----------------------------------------------------------------------------
704 // dialog oriented functions
705 // ----------------------------------------------------------------------------
707 void wxWindowBase::MakeModal(bool modal
)
709 // Disable all other windows
712 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
715 wxWindow
*win
= node
->GetData();
719 node
= node
->GetNext();
724 bool wxWindowBase::Validate()
727 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
729 wxWindowList::Node
*node
;
730 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
732 wxWindowBase
*child
= node
->GetData();
733 wxValidator
*validator
= child
->GetValidator();
734 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
739 if ( recurse
&& !child
->Validate() )
744 #endif // wxUSE_VALIDATORS
749 bool wxWindowBase::TransferDataToWindow()
752 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
754 wxWindowList::Node
*node
;
755 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
757 wxWindowBase
*child
= node
->GetData();
758 wxValidator
*validator
= child
->GetValidator();
759 if ( validator
&& !validator
->TransferToWindow() )
761 wxLogWarning(_("Could not transfer data to window"));
762 wxLog::FlushActive();
769 if ( !child
->TransferDataToWindow() )
771 // warning already given
776 #endif // wxUSE_VALIDATORS
781 bool wxWindowBase::TransferDataFromWindow()
784 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
786 wxWindowList::Node
*node
;
787 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
789 wxWindow
*child
= node
->GetData();
790 wxValidator
*validator
= child
->GetValidator();
791 if ( validator
&& !validator
->TransferFromWindow() )
793 // nop warning here because the application is supposed to give
794 // one itself - we don't know here what might have gone wrongly
801 if ( !child
->TransferDataFromWindow() )
803 // warning already given
808 #endif // wxUSE_VALIDATORS
813 void wxWindowBase::InitDialog()
815 wxInitDialogEvent
event(GetId());
816 event
.SetEventObject( this );
817 GetEventHandler()->ProcessEvent(event
);
820 // ----------------------------------------------------------------------------
822 // ----------------------------------------------------------------------------
826 void wxWindowBase::SetToolTip( const wxString
&tip
)
828 // don't create the new tooltip if we already have one
831 m_tooltip
->SetTip( tip
);
835 SetToolTip( new wxToolTip( tip
) );
838 // setting empty tooltip text does not remove the tooltip any more - use
839 // SetToolTip((wxToolTip *)NULL) for this
842 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
850 #endif // wxUSE_TOOLTIPS
852 // ----------------------------------------------------------------------------
853 // constraints and sizers
854 // ----------------------------------------------------------------------------
856 #if wxUSE_CONSTRAINTS
858 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
862 UnsetConstraints(m_constraints
);
863 delete m_constraints
;
865 m_constraints
= constraints
;
868 // Make sure other windows know they're part of a 'meaningful relationship'
869 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
870 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
871 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
872 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
873 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
874 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
875 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
876 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
877 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
878 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
879 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
880 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
881 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
882 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
883 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
884 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
888 // This removes any dangling pointers to this window in other windows'
889 // constraintsInvolvedIn lists.
890 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
894 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
895 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
896 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
897 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
898 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
899 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
900 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
901 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
902 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
903 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
904 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
905 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
906 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
907 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
908 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
909 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
913 // Back-pointer to other windows we're involved with, so if we delete this
914 // window, we must delete any constraints we're involved with.
915 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
917 if ( !m_constraintsInvolvedIn
)
918 m_constraintsInvolvedIn
= new wxWindowList
;
919 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
920 m_constraintsInvolvedIn
->Append(otherWin
);
923 // REMOVE back-pointer to other windows we're involved with.
924 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
926 if ( m_constraintsInvolvedIn
)
927 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
930 // Reset any constraints that mention this window
931 void wxWindowBase::DeleteRelatedConstraints()
933 if ( m_constraintsInvolvedIn
)
935 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
938 wxWindow
*win
= node
->GetData();
939 wxLayoutConstraints
*constr
= win
->GetConstraints();
941 // Reset any constraints involving this window
944 constr
->left
.ResetIfWin(this);
945 constr
->top
.ResetIfWin(this);
946 constr
->right
.ResetIfWin(this);
947 constr
->bottom
.ResetIfWin(this);
948 constr
->width
.ResetIfWin(this);
949 constr
->height
.ResetIfWin(this);
950 constr
->centreX
.ResetIfWin(this);
951 constr
->centreY
.ResetIfWin(this);
954 wxWindowList::Node
*next
= node
->GetNext();
959 delete m_constraintsInvolvedIn
;
960 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
964 void wxWindowBase::SetSizer(wxSizer
*sizer
)
966 if (m_windowSizer
) delete m_windowSizer
;
968 m_windowSizer
= sizer
;
971 bool wxWindowBase::Layout()
973 // If there is a sizer, use it instead of the constraints
977 GetClientSize(&w
, &h
);
979 GetSizer()->SetDimension( 0, 0, w
, h
);
983 wxLayoutConstraints
*constr
= GetConstraints();
984 bool wasOk
= constr
&& constr
->AreSatisfied();
986 ResetConstraints(); // Mark all constraints as unevaluated
988 // if we're a top level panel (i.e. our parent is frame/dialog), our
989 // own constraints will never be satisfied any more unless we do it
994 while ( noChanges
> 0 )
996 constr
->SatisfyConstraints(this, &noChanges
);
1000 DoPhase(1); // Layout children
1001 DoPhase(2); // Layout grand children
1002 SetConstraintSizes(); // Recursively set the real window sizes
1009 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1010 // do a similar thing, but also impose their own 'constraints' and order the
1011 // evaluation differently.
1012 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1014 wxLayoutConstraints
*constr
= GetConstraints();
1017 return constr
->SatisfyConstraints(this, noChanges
);
1023 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1033 // Do a phase of evaluating child constraints
1034 bool wxWindowBase::DoPhase(int phase
)
1036 int noIterations
= 0;
1037 int maxIterations
= 500;
1040 wxWindowList succeeded
;
1041 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1045 wxWindowList::Node
*node
= GetChildren().GetFirst();
1048 wxWindow
*child
= node
->GetData();
1049 if ( !child
->IsTopLevel() )
1051 wxLayoutConstraints
*constr
= child
->GetConstraints();
1054 if ( !succeeded
.Find(child
) )
1056 int tempNoChanges
= 0;
1057 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1058 noChanges
+= tempNoChanges
;
1061 succeeded
.Append(child
);
1066 node
= node
->GetNext();
1075 void wxWindowBase::ResetConstraints()
1077 wxLayoutConstraints
*constr
= GetConstraints();
1080 constr
->left
.SetDone(FALSE
);
1081 constr
->top
.SetDone(FALSE
);
1082 constr
->right
.SetDone(FALSE
);
1083 constr
->bottom
.SetDone(FALSE
);
1084 constr
->width
.SetDone(FALSE
);
1085 constr
->height
.SetDone(FALSE
);
1086 constr
->centreX
.SetDone(FALSE
);
1087 constr
->centreY
.SetDone(FALSE
);
1090 wxWindowList::Node
*node
= GetChildren().GetFirst();
1093 wxWindow
*win
= node
->GetData();
1094 if ( !win
->IsTopLevel() )
1095 win
->ResetConstraints();
1096 node
= node
->GetNext();
1100 // Need to distinguish between setting the 'fake' size for windows and sizers,
1101 // and setting the real values.
1102 void wxWindowBase::SetConstraintSizes(bool recurse
)
1104 wxLayoutConstraints
*constr
= GetConstraints();
1105 if ( constr
&& constr
->AreSatisfied() )
1107 int x
= constr
->left
.GetValue();
1108 int y
= constr
->top
.GetValue();
1109 int w
= constr
->width
.GetValue();
1110 int h
= constr
->height
.GetValue();
1112 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1113 (constr
->height
.GetRelationship() != wxAsIs
) )
1115 SetSize(x
, y
, w
, h
);
1119 // If we don't want to resize this window, just move it...
1125 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1126 GetClassInfo()->GetClassName(),
1132 wxWindowList::Node
*node
= GetChildren().GetFirst();
1135 wxWindow
*win
= node
->GetData();
1136 if ( !win
->IsTopLevel() )
1137 win
->SetConstraintSizes();
1138 node
= node
->GetNext();
1143 // Only set the size/position of the constraint (if any)
1144 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1146 wxLayoutConstraints
*constr
= GetConstraints();
1151 constr
->left
.SetValue(x
);
1152 constr
->left
.SetDone(TRUE
);
1156 constr
->top
.SetValue(y
);
1157 constr
->top
.SetDone(TRUE
);
1161 constr
->width
.SetValue(w
);
1162 constr
->width
.SetDone(TRUE
);
1166 constr
->height
.SetValue(h
);
1167 constr
->height
.SetDone(TRUE
);
1172 void wxWindowBase::MoveConstraint(int x
, int y
)
1174 wxLayoutConstraints
*constr
= GetConstraints();
1179 constr
->left
.SetValue(x
);
1180 constr
->left
.SetDone(TRUE
);
1184 constr
->top
.SetValue(y
);
1185 constr
->top
.SetDone(TRUE
);
1190 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1192 wxLayoutConstraints
*constr
= GetConstraints();
1195 *w
= constr
->width
.GetValue();
1196 *h
= constr
->height
.GetValue();
1202 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1204 wxLayoutConstraints
*constr
= GetConstraints();
1207 *w
= constr
->width
.GetValue();
1208 *h
= constr
->height
.GetValue();
1211 GetClientSize(w
, h
);
1214 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1216 wxLayoutConstraints
*constr
= GetConstraints();
1219 *x
= constr
->left
.GetValue();
1220 *y
= constr
->top
.GetValue();
1226 #endif // wxUSE_CONSTRAINTS
1228 // ----------------------------------------------------------------------------
1229 // do Update UI processing for child controls
1230 // ----------------------------------------------------------------------------
1232 // TODO: should this be implemented for the child window rather
1233 // than the parent? Then you can override it e.g. for wxCheckBox
1234 // to do the Right Thing rather than having to assume a fixed number
1235 // of control classes.
1236 void wxWindowBase::UpdateWindowUI()
1238 wxUpdateUIEvent
event(GetId());
1239 event
.m_eventObject
= this;
1241 if ( GetEventHandler()->ProcessEvent(event
) )
1243 if ( event
.GetSetEnabled() )
1244 Enable(event
.GetEnabled());
1246 if ( event
.GetSetText() )
1248 wxControl
*control
= wxDynamicCast(this, wxControl
);
1251 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1253 text
->SetValue(event
.GetText());
1255 control
->SetLabel(event
.GetText());
1260 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1263 if ( event
.GetSetChecked() )
1264 checkbox
->SetValue(event
.GetChecked());
1266 #endif // wxUSE_CHECKBOX
1269 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1272 if ( event
.GetSetChecked() )
1273 radiobtn
->SetValue(event
.GetChecked());
1275 #endif // wxUSE_RADIOBTN
1279 // ----------------------------------------------------------------------------
1280 // dialog units translations
1281 // ----------------------------------------------------------------------------
1283 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1285 int charWidth
= GetCharWidth();
1286 int charHeight
= GetCharHeight();
1287 wxPoint
pt2(-1, -1);
1289 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1291 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1296 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1298 int charWidth
= GetCharWidth();
1299 int charHeight
= GetCharHeight();
1300 wxPoint
pt2(-1, -1);
1302 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1304 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1309 // ----------------------------------------------------------------------------
1311 // ----------------------------------------------------------------------------
1313 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1315 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1316 wxT("can't have both object and void client data") );
1318 if ( m_clientObject
)
1319 delete m_clientObject
;
1321 m_clientObject
= data
;
1322 m_clientDataType
= ClientData_Object
;
1325 wxClientData
*wxWindowBase::DoGetClientObject() const
1327 // it's not an error to call GetClientObject() on a window which doesn't
1328 // have client data at all - NULL will be returned
1329 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1330 wxT("this window doesn't have object client data") );
1332 return m_clientObject
;
1335 void wxWindowBase::DoSetClientData( void *data
)
1337 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1338 wxT("can't have both object and void client data") );
1340 m_clientData
= data
;
1341 m_clientDataType
= ClientData_Void
;
1344 void *wxWindowBase::DoGetClientData() const
1346 // it's not an error to call GetClientData() on a window which doesn't have
1347 // client data at all - NULL will be returned
1348 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1349 wxT("this window doesn't have void client data") );
1351 return m_clientData
;
1354 // ----------------------------------------------------------------------------
1356 // ----------------------------------------------------------------------------
1358 // propagate the colour change event to the subwindows
1359 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1361 wxWindowList::Node
*node
= GetChildren().GetFirst();
1364 // Only propagate to non-top-level windows
1365 wxWindow
*win
= node
->GetData();
1366 if ( !win
->IsTopLevel() )
1368 wxSysColourChangedEvent event2
;
1369 event
.m_eventObject
= win
;
1370 win
->GetEventHandler()->ProcessEvent(event2
);
1373 node
= node
->GetNext();
1377 // the default action is to populate dialog with data when it's created
1378 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1380 TransferDataToWindow();
1383 // process Ctrl-Alt-mclick
1384 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1386 if ( event
.ControlDown() && event
.AltDown() )
1388 // don't translate these strings
1390 switch ( wxGetOsVersion() )
1392 case wxMOTIF_X
: port
= _T("Motif"); break;
1393 case wxMACINTOSH
: port
= _T("Mac"); break;
1394 case wxBEOS
: port
= _T("BeOS"); break;
1398 case wxGTK_BEOS
: port
= _T("GTK"); break;
1404 case wxWIN386
: port
= _T("MS Windows"); break;
1408 case wxMGL_OS2
: port
= _T("MGL"); break;
1410 case wxOS2_PM
: port
= _T("OS/2"); break;
1411 default: port
= _T("unknown"); break;
1414 wxMessageBox(wxString::Format(
1416 " wxWindows Library (%s port)\n"
1417 "Version %u.%u.%u, compiled at %s %s\n"
1418 " Copyright (c) 1995-2000 wxWindows team"
1427 _T("wxWindows information"),
1428 wxICON_INFORMATION
| wxOK
,
1437 // ----------------------------------------------------------------------------
1438 // list classes implementation
1439 // ----------------------------------------------------------------------------
1441 void wxWindowListNode::DeleteData()
1443 delete (wxWindow
*)GetData();