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
;
179 SetWindowStyleFlag(style
);
183 SetValidator(validator
);
184 #endif // wxUSE_VALIDATORS
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
194 wxWindowBase::~wxWindowBase()
196 // FIXME if these 2 cases result from programming errors in the user code
197 // we should probably assert here instead of silently fixing them
199 // Just in case the window has been Closed, but we're then deleting
200 // immediately: don't leave dangling pointers.
201 wxPendingDelete
.DeleteObject(this);
203 // Just in case we've loaded a top-level window via LoadNativeDialog but
204 // we weren't a dialog class
205 wxTopLevelWindows
.DeleteObject(this);
207 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
209 // make sure that there are no dangling pointers left pointing to us
210 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
213 if ( panel
->GetLastFocus() == this )
215 panel
->SetLastFocus((wxWindow
*)NULL
);
222 #endif // wxUSE_CARET
225 if ( m_windowValidator
)
226 delete m_windowValidator
;
227 #endif // wxUSE_VALIDATORS
229 // we only delete object data, not untyped
230 if ( m_clientDataType
== ClientData_Object
)
231 delete m_clientObject
;
233 #if wxUSE_CONSTRAINTS
234 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
235 // at deleted windows as they delete themselves.
236 DeleteRelatedConstraints();
240 // This removes any dangling pointers to this window in other windows'
241 // constraintsInvolvedIn lists.
242 UnsetConstraints(m_constraints
);
243 delete m_constraints
;
244 m_constraints
= NULL
;
248 delete m_windowSizer
;
250 #endif // wxUSE_CONSTRAINTS
252 #if wxUSE_DRAG_AND_DROP
255 #endif // wxUSE_DRAG_AND_DROP
260 #endif // wxUSE_TOOLTIPS
263 bool wxWindowBase::Destroy()
270 bool wxWindowBase::Close(bool force
)
272 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
273 event
.SetEventObject(this);
274 #if WXWIN_COMPATIBILITY
275 event
.SetForce(force
);
276 #endif // WXWIN_COMPATIBILITY
277 event
.SetCanVeto(!force
);
279 // return FALSE if window wasn't closed because the application vetoed the
281 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
284 bool wxWindowBase::DestroyChildren()
286 wxWindowList::Node
*node
;
289 // we iterate until the list becomes empty
290 node
= GetChildren().GetFirst();
294 wxWindow
*child
= node
->GetData();
296 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
300 wxASSERT_MSG( !GetChildren().Find(child
),
301 wxT("child didn't remove itself using RemoveChild()") );
307 // ----------------------------------------------------------------------------
308 // size/position related methods
309 // ----------------------------------------------------------------------------
311 // centre the window with respect to its parent in either (or both) directions
312 void wxWindowBase::Centre(int direction
)
314 int widthParent
, heightParent
;
316 wxWindow
*parent
= GetParent();
320 direction
|= wxCENTRE_ON_SCREEN
;
323 if ( direction
& wxCENTRE_ON_SCREEN
)
325 // centre with respect to the whole screen
326 wxDisplaySize(&widthParent
, &heightParent
);
330 // centre inside the parents rectangle
331 parent
->GetClientSize(&widthParent
, &heightParent
);
335 GetSize(&width
, &height
);
340 if ( direction
& wxHORIZONTAL
)
341 xNew
= (widthParent
- width
)/2;
343 if ( direction
& wxVERTICAL
)
344 yNew
= (heightParent
- height
)/2;
346 // controls are always centered on their parent because it doesn't make
347 // sense to centre them on the screen
348 if ( !(direction
& wxCENTRE_ON_SCREEN
) || wxDynamicCast(this, wxControl
) )
350 // theo nly chance to get this is to have a wxControl without parent
351 wxCHECK_RET( parent
, wxT("a control must have a parent") );
353 // adjust to the parents client area origin
354 wxPoint posParent
= parent
->ClientToScreen(wxPoint(0, 0));
360 // move the centre of this window to this position
364 // fits the window around the children
365 void wxWindowBase::Fit()
367 if ( GetChildren().GetCount() > 0 )
369 SetClientSize(DoGetBestSize());
371 //else: do nothing if we have no children
374 // return the size best suited for the current window
375 wxSize
wxWindowBase::DoGetBestSize() const
377 if ( GetChildren().GetCount() > 0 )
379 // our minimal acceptable size is such that all our windows fit inside
383 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
385 node
= node
->GetNext() )
387 wxWindow
*win
= node
->GetData();
388 if ( win
->IsTopLevel() )
390 // dialogs and frames lie in different top level windows -
391 // don't deal with them here
396 win
->GetPosition(&wx
, &wy
);
397 win
->GetSize(&ww
, &wh
);
398 if ( wx
+ ww
> maxX
)
400 if ( wy
+ wh
> maxY
)
405 return wxSize(maxX
+ 7, maxY
+ 14);
409 // for a generic window there is no natural best size - just use the
415 // set the min/max size of the window
416 void wxWindowBase::SetSizeHints(int minW
, int minH
,
418 int WXUNUSED(incW
), int WXUNUSED(incH
))
426 // ----------------------------------------------------------------------------
427 // show/hide/enable/disable the window
428 // ----------------------------------------------------------------------------
430 bool wxWindowBase::Show(bool show
)
432 if ( show
!= m_isShown
)
444 bool wxWindowBase::Enable(bool enable
)
446 if ( enable
!= m_isEnabled
)
448 m_isEnabled
= enable
;
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
461 bool wxWindowBase::IsTopLevel() const
466 // ----------------------------------------------------------------------------
467 // reparenting the window
468 // ----------------------------------------------------------------------------
470 void wxWindowBase::AddChild(wxWindowBase
*child
)
472 wxCHECK_RET( child
, wxT("can't add a NULL child") );
474 GetChildren().Append(child
);
475 child
->SetParent(this);
478 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
480 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
482 GetChildren().DeleteObject(child
);
483 child
->SetParent((wxWindow
*)NULL
);
486 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
488 wxWindow
*oldParent
= GetParent();
489 if ( newParent
== oldParent
)
495 // unlink this window from the existing parent.
498 oldParent
->RemoveChild(this);
502 wxTopLevelWindows
.DeleteObject(this);
505 // add it to the new one
508 newParent
->AddChild(this);
512 wxTopLevelWindows
.Append(this);
518 // ----------------------------------------------------------------------------
519 // event handler stuff
520 // ----------------------------------------------------------------------------
522 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
524 handler
->SetNextHandler(GetEventHandler());
525 SetEventHandler(handler
);
528 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
530 wxEvtHandler
*handlerA
= GetEventHandler();
533 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
534 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
535 SetEventHandler(handlerB
);
539 handlerA
= (wxEvtHandler
*)NULL
;
546 // ----------------------------------------------------------------------------
548 // ----------------------------------------------------------------------------
550 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
552 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
555 m_backgroundColour
= colour
;
560 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
562 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
565 m_foregroundColour
= colour
;
570 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
572 // don't try to set invalid cursor, always fall back to the default
573 const wxCursor
& cursorOk
= cursor
.Ok() ? cursor
: *wxSTANDARD_CURSOR
;
575 if ( (wxCursor
&)cursorOk
== m_cursor
)
586 bool wxWindowBase::SetFont(const wxFont
& font
)
588 // don't try to set invalid font, always fall back to the default
589 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
591 if ( (wxFont
&)fontOk
== m_font
)
603 void wxWindowBase::SetCaret(wxCaret
*caret
)
614 wxASSERT_MSG( m_caret
->GetWindow() == this,
615 wxT("caret should be created associated to this window") );
618 #endif // wxUSE_CARET
621 // ----------------------------------------------------------------------------
623 // ----------------------------------------------------------------------------
625 void wxWindowBase::SetValidator(const wxValidator
& validator
)
627 if ( m_windowValidator
)
628 delete m_windowValidator
;
630 m_windowValidator
= (wxValidator
*)validator
.Clone();
632 if ( m_windowValidator
)
633 m_windowValidator
->SetWindow(this) ;
635 #endif // wxUSE_VALIDATORS
637 // ----------------------------------------------------------------------------
638 // update region testing
639 // ----------------------------------------------------------------------------
641 bool wxWindowBase::IsExposed(int x
, int y
) const
643 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
646 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
648 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
651 // ----------------------------------------------------------------------------
652 // find window by id or name
653 // ----------------------------------------------------------------------------
655 wxWindow
*wxWindowBase::FindWindow( long id
)
657 if ( id
== m_windowId
)
658 return (wxWindow
*)this;
660 wxWindowBase
*res
= (wxWindow
*)NULL
;
661 wxWindowList::Node
*node
;
662 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
664 wxWindowBase
*child
= node
->GetData();
665 res
= child
->FindWindow( id
);
668 return (wxWindow
*)res
;
671 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
673 if ( name
== m_windowName
)
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 wxWindow
*child
= node
->GetData();
681 res
= child
->FindWindow(name
);
684 return (wxWindow
*)res
;
687 // ----------------------------------------------------------------------------
688 // dialog oriented functions
689 // ----------------------------------------------------------------------------
691 void wxWindowBase::MakeModal(bool modal
)
693 // Disable all other windows
696 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
699 wxWindow
*win
= node
->GetData();
703 node
= node
->GetNext();
708 bool wxWindowBase::Validate()
711 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
713 wxWindowList::Node
*node
;
714 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
716 wxWindowBase
*child
= node
->GetData();
717 wxValidator
*validator
= child
->GetValidator();
718 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
723 if ( recurse
&& !child
->Validate() )
728 #endif // wxUSE_VALIDATORS
733 bool wxWindowBase::TransferDataToWindow()
736 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
738 wxWindowList::Node
*node
;
739 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
741 wxWindowBase
*child
= node
->GetData();
742 wxValidator
*validator
= child
->GetValidator();
743 if ( validator
&& !validator
->TransferToWindow() )
745 wxLogWarning(_("Could not transfer data to window"));
746 wxLog::FlushActive();
753 if ( !child
->TransferDataToWindow() )
755 // warning already given
760 #endif // wxUSE_VALIDATORS
765 bool wxWindowBase::TransferDataFromWindow()
768 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
770 wxWindowList::Node
*node
;
771 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
773 wxWindow
*child
= node
->GetData();
774 wxValidator
*validator
= child
->GetValidator();
775 if ( validator
&& !validator
->TransferFromWindow() )
777 // nop warning here because the application is supposed to give
778 // one itself - we don't know here what might have gone wrongly
785 if ( !child
->TransferDataFromWindow() )
787 // warning already given
792 #endif // wxUSE_VALIDATORS
797 void wxWindowBase::InitDialog()
799 wxInitDialogEvent
event(GetId());
800 event
.SetEventObject( this );
801 GetEventHandler()->ProcessEvent(event
);
804 // ----------------------------------------------------------------------------
806 // ----------------------------------------------------------------------------
810 void wxWindowBase::SetToolTip( const wxString
&tip
)
812 // don't create the new tooltip if we already have one
815 m_tooltip
->SetTip( tip
);
819 SetToolTip( new wxToolTip( tip
) );
822 // setting empty tooltip text does not remove the tooltip any more - use
823 // SetToolTip((wxToolTip *)NULL) for this
826 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
834 #endif // wxUSE_TOOLTIPS
836 // ----------------------------------------------------------------------------
837 // constraints and sizers
838 // ----------------------------------------------------------------------------
840 #if wxUSE_CONSTRAINTS
842 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
846 UnsetConstraints(m_constraints
);
847 delete m_constraints
;
849 m_constraints
= constraints
;
852 // Make sure other windows know they're part of a 'meaningful relationship'
853 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
854 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
855 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
856 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
857 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
858 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
859 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
860 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
861 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
862 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
863 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
864 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
865 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
866 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
867 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
868 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
872 // This removes any dangling pointers to this window in other windows'
873 // constraintsInvolvedIn lists.
874 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
878 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
879 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
880 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
881 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
882 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
883 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
884 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
885 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
886 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
887 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
888 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
889 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
890 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
891 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
892 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
893 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
897 // Back-pointer to other windows we're involved with, so if we delete this
898 // window, we must delete any constraints we're involved with.
899 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
901 if ( !m_constraintsInvolvedIn
)
902 m_constraintsInvolvedIn
= new wxWindowList
;
903 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
904 m_constraintsInvolvedIn
->Append(otherWin
);
907 // REMOVE back-pointer to other windows we're involved with.
908 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
910 if ( m_constraintsInvolvedIn
)
911 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
914 // Reset any constraints that mention this window
915 void wxWindowBase::DeleteRelatedConstraints()
917 if ( m_constraintsInvolvedIn
)
919 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
922 wxWindow
*win
= node
->GetData();
923 wxLayoutConstraints
*constr
= win
->GetConstraints();
925 // Reset any constraints involving this window
928 constr
->left
.ResetIfWin(this);
929 constr
->top
.ResetIfWin(this);
930 constr
->right
.ResetIfWin(this);
931 constr
->bottom
.ResetIfWin(this);
932 constr
->width
.ResetIfWin(this);
933 constr
->height
.ResetIfWin(this);
934 constr
->centreX
.ResetIfWin(this);
935 constr
->centreY
.ResetIfWin(this);
938 wxWindowList::Node
*next
= node
->GetNext();
943 delete m_constraintsInvolvedIn
;
944 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
948 void wxWindowBase::SetSizer(wxSizer
*sizer
)
950 if (m_windowSizer
) delete m_windowSizer
;
952 m_windowSizer
= sizer
;
955 bool wxWindowBase::Layout()
957 // If there is a sizer, use it instead of the constraints
961 GetClientSize(&w
, &h
);
963 GetSizer()->SetDimension( 0, 0, w
, h
);
967 wxLayoutConstraints
*constr
= GetConstraints();
968 bool wasOk
= constr
&& constr
->AreSatisfied();
970 ResetConstraints(); // Mark all constraints as unevaluated
972 // if we're a top level panel (i.e. our parent is frame/dialog), our
973 // own constraints will never be satisfied any more unless we do it
978 while ( noChanges
> 0 )
980 constr
->SatisfyConstraints(this, &noChanges
);
984 DoPhase(1); // Layout children
985 DoPhase(2); // Layout grand children
986 SetConstraintSizes(); // Recursively set the real window sizes
993 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
994 // do a similar thing, but also impose their own 'constraints' and order the
995 // evaluation differently.
996 bool wxWindowBase::LayoutPhase1(int *noChanges
)
998 wxLayoutConstraints
*constr
= GetConstraints();
1001 return constr
->SatisfyConstraints(this, noChanges
);
1007 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1017 // Do a phase of evaluating child constraints
1018 bool wxWindowBase::DoPhase(int phase
)
1020 int noIterations
= 0;
1021 int maxIterations
= 500;
1024 wxWindowList succeeded
;
1025 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1029 wxWindowList::Node
*node
= GetChildren().GetFirst();
1032 wxWindow
*child
= node
->GetData();
1033 if ( !child
->IsTopLevel() )
1035 wxLayoutConstraints
*constr
= child
->GetConstraints();
1038 if ( !succeeded
.Find(child
) )
1040 int tempNoChanges
= 0;
1041 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1042 noChanges
+= tempNoChanges
;
1045 succeeded
.Append(child
);
1050 node
= node
->GetNext();
1059 void wxWindowBase::ResetConstraints()
1061 wxLayoutConstraints
*constr
= GetConstraints();
1064 constr
->left
.SetDone(FALSE
);
1065 constr
->top
.SetDone(FALSE
);
1066 constr
->right
.SetDone(FALSE
);
1067 constr
->bottom
.SetDone(FALSE
);
1068 constr
->width
.SetDone(FALSE
);
1069 constr
->height
.SetDone(FALSE
);
1070 constr
->centreX
.SetDone(FALSE
);
1071 constr
->centreY
.SetDone(FALSE
);
1074 wxWindowList::Node
*node
= GetChildren().GetFirst();
1077 wxWindow
*win
= node
->GetData();
1078 if ( !win
->IsTopLevel() )
1079 win
->ResetConstraints();
1080 node
= node
->GetNext();
1084 // Need to distinguish between setting the 'fake' size for windows and sizers,
1085 // and setting the real values.
1086 void wxWindowBase::SetConstraintSizes(bool recurse
)
1088 wxLayoutConstraints
*constr
= GetConstraints();
1089 if ( constr
&& constr
->AreSatisfied() )
1091 int x
= constr
->left
.GetValue();
1092 int y
= constr
->top
.GetValue();
1093 int w
= constr
->width
.GetValue();
1094 int h
= constr
->height
.GetValue();
1096 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1097 (constr
->height
.GetRelationship() != wxAsIs
) )
1099 SetSize(x
, y
, w
, h
);
1103 // If we don't want to resize this window, just move it...
1109 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1110 GetClassInfo()->GetClassName(),
1116 wxWindowList::Node
*node
= GetChildren().GetFirst();
1119 wxWindow
*win
= node
->GetData();
1120 if ( !win
->IsTopLevel() )
1121 win
->SetConstraintSizes();
1122 node
= node
->GetNext();
1127 // Only set the size/position of the constraint (if any)
1128 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1130 wxLayoutConstraints
*constr
= GetConstraints();
1135 constr
->left
.SetValue(x
);
1136 constr
->left
.SetDone(TRUE
);
1140 constr
->top
.SetValue(y
);
1141 constr
->top
.SetDone(TRUE
);
1145 constr
->width
.SetValue(w
);
1146 constr
->width
.SetDone(TRUE
);
1150 constr
->height
.SetValue(h
);
1151 constr
->height
.SetDone(TRUE
);
1156 void wxWindowBase::MoveConstraint(int x
, int y
)
1158 wxLayoutConstraints
*constr
= GetConstraints();
1163 constr
->left
.SetValue(x
);
1164 constr
->left
.SetDone(TRUE
);
1168 constr
->top
.SetValue(y
);
1169 constr
->top
.SetDone(TRUE
);
1174 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1176 wxLayoutConstraints
*constr
= GetConstraints();
1179 *w
= constr
->width
.GetValue();
1180 *h
= constr
->height
.GetValue();
1186 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1188 wxLayoutConstraints
*constr
= GetConstraints();
1191 *w
= constr
->width
.GetValue();
1192 *h
= constr
->height
.GetValue();
1195 GetClientSize(w
, h
);
1198 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1200 wxLayoutConstraints
*constr
= GetConstraints();
1203 *x
= constr
->left
.GetValue();
1204 *y
= constr
->top
.GetValue();
1210 #endif // wxUSE_CONSTRAINTS
1212 // ----------------------------------------------------------------------------
1213 // do Update UI processing for child controls
1214 // ----------------------------------------------------------------------------
1216 // TODO: should this be implemented for the child window rather
1217 // than the parent? Then you can override it e.g. for wxCheckBox
1218 // to do the Right Thing rather than having to assume a fixed number
1219 // of control classes.
1220 void wxWindowBase::UpdateWindowUI()
1222 wxUpdateUIEvent
event(GetId());
1223 event
.m_eventObject
= this;
1225 if ( GetEventHandler()->ProcessEvent(event
) )
1227 if ( event
.GetSetEnabled() )
1228 Enable(event
.GetEnabled());
1230 if ( event
.GetSetText() )
1232 wxControl
*control
= wxDynamicCast(this, wxControl
);
1235 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1237 text
->SetValue(event
.GetText());
1239 control
->SetLabel(event
.GetText());
1244 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1247 if ( event
.GetSetChecked() )
1248 checkbox
->SetValue(event
.GetChecked());
1250 #endif // wxUSE_CHECKBOX
1253 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1256 if ( event
.GetSetChecked() )
1257 radiobtn
->SetValue(event
.GetChecked());
1259 #endif // wxUSE_RADIOBTN
1263 // ----------------------------------------------------------------------------
1264 // dialog units translations
1265 // ----------------------------------------------------------------------------
1267 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1269 int charWidth
= GetCharWidth();
1270 int charHeight
= GetCharHeight();
1271 wxPoint
pt2(-1, -1);
1273 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1275 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1280 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1282 int charWidth
= GetCharWidth();
1283 int charHeight
= GetCharHeight();
1284 wxPoint
pt2(-1, -1);
1286 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1288 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1293 // ----------------------------------------------------------------------------
1295 // ----------------------------------------------------------------------------
1297 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1299 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1300 wxT("can't have both object and void client data") );
1302 if ( m_clientObject
)
1303 delete m_clientObject
;
1305 m_clientObject
= data
;
1306 m_clientDataType
= ClientData_Object
;
1309 wxClientData
*wxWindowBase::DoGetClientObject() const
1311 // it's not an error to call GetClientObject() on a window which doesn't
1312 // have client data at all - NULL will be returned
1313 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1314 wxT("this window doesn't have object client data") );
1316 return m_clientObject
;
1319 void wxWindowBase::DoSetClientData( void *data
)
1321 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1322 wxT("can't have both object and void client data") );
1324 m_clientData
= data
;
1325 m_clientDataType
= ClientData_Void
;
1328 void *wxWindowBase::DoGetClientData() const
1330 // it's not an error to call GetClientData() on a window which doesn't have
1331 // client data at all - NULL will be returned
1332 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1333 wxT("this window doesn't have void client data") );
1335 return m_clientData
;
1338 // ----------------------------------------------------------------------------
1340 // ----------------------------------------------------------------------------
1342 // propagate the colour change event to the subwindows
1343 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1345 wxWindowList::Node
*node
= GetChildren().GetFirst();
1348 // Only propagate to non-top-level windows
1349 wxWindow
*win
= node
->GetData();
1350 if ( !win
->IsTopLevel() )
1352 wxSysColourChangedEvent event2
;
1353 event
.m_eventObject
= win
;
1354 win
->GetEventHandler()->ProcessEvent(event2
);
1357 node
= node
->GetNext();
1361 // the default action is to populate dialog with data when it's created
1362 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1364 TransferDataToWindow();
1367 // process Ctrl-Alt-mclick
1368 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1370 if ( event
.ControlDown() && event
.AltDown() )
1372 // don't translate these strings
1374 switch ( wxGetOsVersion() )
1376 case wxMOTIF_X
: port
= _T("Motif"); break;
1377 case wxMACINTOSH
: port
= _T("Mac"); break;
1378 case wxBEOS
: port
= _T("BeOS"); break;
1382 case wxGTK_BEOS
: port
= _T("GTK"); break;
1388 case wxWIN386
: port
= _T("MS Windows"); break;
1392 case wxMGL_OS2
: port
= _T("MGL"); break;
1394 case wxOS2_PM
: port
= _T("OS/2"); break;
1395 default: port
= _T("unknown"); break;
1398 wxMessageBox(wxString::Format(
1400 " wxWindows Library (%s port)\n"
1401 "Version %u.%u.%u, compiled at %s %s\n"
1402 " Copyright (c) 1995-2000 wxWindows team"
1411 _T("wxWindows information"),
1412 wxICON_INFORMATION
| wxOK
,
1421 // ----------------------------------------------------------------------------
1422 // list classes implementation
1423 // ----------------------------------------------------------------------------
1425 void wxWindowListNode::DeleteData()
1427 delete (wxWindow
*)GetData();