1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/checkbox.h"
39 #include "wx/radiobut.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/statusbr.h"
48 #include "wx/layout.h"
50 #endif // wxUSE_CONSTRAINTS
52 #if wxUSE_DRAG_AND_DROP
54 #endif // wxUSE_DRAG_AND_DROP
57 #include "wx/cshelp.h"
61 #include "wx/tooltip.h"
62 #endif // wxUSE_TOOLTIPS
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 int wxWindowBase::ms_lastControlId
= -200;
74 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
81 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
82 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
83 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
86 EVT_HELP(-1, wxWindowBase::OnHelp
)
91 // ============================================================================
92 // implementation of the common functionality of the wxWindow class
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // the default initialization
100 void wxWindowBase::InitBase()
102 // no window yet, no parent nor children
103 m_parent
= (wxWindow
*)NULL
;
105 m_children
.DeleteContents( FALSE
); // don't auto delete node data
107 // no constraints on the minimal window size
113 // window is created enabled but it's not visible yet
117 // no client data (yet)
119 m_clientDataType
= ClientData_None
;
121 // the default event handler is just this window
122 m_eventHandler
= this;
126 m_windowValidator
= (wxValidator
*) NULL
;
127 #endif // wxUSE_VALIDATORS
129 // use the system default colours
130 wxSystemSettings settings
;
132 m_backgroundColour
= settings
.GetSystemColour(wxSYS_COLOUR_BTNFACE
);
133 // m_foregroundColour = *wxBLACK; // TODO take this from sys settings too?
134 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
136 // GRG, changed Mar/2000
137 m_font
= settings
.GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
142 // an optimization for the event processing: checking this flag is much
143 // faster than using IsKindOf(CLASSINFO(wxWindow))
146 #if wxUSE_CONSTRAINTS
147 // no constraints whatsoever
148 m_constraints
= (wxLayoutConstraints
*) NULL
;
149 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
150 m_windowSizer
= (wxSizer
*) NULL
;
151 m_autoLayout
= FALSE
;
152 #endif // wxUSE_CONSTRAINTS
154 #if wxUSE_DRAG_AND_DROP
155 m_dropTarget
= (wxDropTarget
*)NULL
;
156 #endif // wxUSE_DRAG_AND_DROP
159 m_tooltip
= (wxToolTip
*)NULL
;
160 #endif // wxUSE_TOOLTIPS
163 m_caret
= (wxCaret
*)NULL
;
164 #endif // wxUSE_CARET
166 // Whether we're using the current theme for this window (wxGTK only for now)
167 m_themeEnabled
= FALSE
;
170 // common part of window creation process
171 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
173 const wxPoint
& WXUNUSED(pos
),
174 const wxSize
& WXUNUSED(size
),
176 const wxValidator
& validator
,
177 const wxString
& name
)
179 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
180 // member variables - check that it has been called (will catch the case
181 // when a new ctor is added which doesn't call InitWindow)
182 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
184 // generate a new id if the user doesn't care about it
185 m_windowId
= id
== -1 ? NewControlId() : id
;
188 SetWindowStyleFlag(style
);
192 SetValidator(validator
);
193 #endif // wxUSE_VALIDATORS
195 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
196 // have it too - like this it's possible to set it only in the top level
197 // dialog/frame and all children will inherit it by defult
198 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
200 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
211 wxWindowBase::~wxWindowBase()
213 // FIXME if these 2 cases result from programming errors in the user code
214 // we should probably assert here instead of silently fixing them
216 // Just in case the window has been Closed, but we're then deleting
217 // immediately: don't leave dangling pointers.
218 wxPendingDelete
.DeleteObject(this);
220 // Just in case we've loaded a top-level window via LoadNativeDialog but
221 // we weren't a dialog class
222 wxTopLevelWindows
.DeleteObject(this);
224 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
226 // make sure that there are no dangling pointers left pointing to us
227 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
230 if ( panel
->GetLastFocus() == this )
232 panel
->SetLastFocus((wxWindow
*)NULL
);
239 #endif // wxUSE_CARET
242 if ( m_windowValidator
)
243 delete m_windowValidator
;
244 #endif // wxUSE_VALIDATORS
246 // we only delete object data, not untyped
247 if ( m_clientDataType
== ClientData_Object
)
248 delete m_clientObject
;
250 #if wxUSE_CONSTRAINTS
251 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
252 // at deleted windows as they delete themselves.
253 DeleteRelatedConstraints();
257 // This removes any dangling pointers to this window in other windows'
258 // constraintsInvolvedIn lists.
259 UnsetConstraints(m_constraints
);
260 delete m_constraints
;
261 m_constraints
= NULL
;
265 delete m_windowSizer
;
267 #endif // wxUSE_CONSTRAINTS
269 #if wxUSE_DRAG_AND_DROP
272 #endif // wxUSE_DRAG_AND_DROP
277 #endif // wxUSE_TOOLTIPS
280 bool wxWindowBase::Destroy()
287 bool wxWindowBase::Close(bool force
)
289 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
290 event
.SetEventObject(this);
291 #if WXWIN_COMPATIBILITY
292 event
.SetForce(force
);
293 #endif // WXWIN_COMPATIBILITY
294 event
.SetCanVeto(!force
);
296 // return FALSE if window wasn't closed because the application vetoed the
298 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
301 bool wxWindowBase::DestroyChildren()
303 wxWindowList::Node
*node
;
306 // we iterate until the list becomes empty
307 node
= GetChildren().GetFirst();
311 wxWindow
*child
= node
->GetData();
313 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
317 wxASSERT_MSG( !GetChildren().Find(child
),
318 wxT("child didn't remove itself using RemoveChild()") );
324 // ----------------------------------------------------------------------------
325 // size/position related methods
326 // ----------------------------------------------------------------------------
328 // centre the window with respect to its parent in either (or both) directions
329 void wxWindowBase::Centre(int direction
)
331 // the position/size of the parent window or of the entire screen
333 int widthParent
, heightParent
;
335 wxWindow
*parent
= NULL
;
337 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
339 // find the parent to centre this window on: it should be the
340 // immediate parent for the controls but the top level parent for the
341 // top level windows (like dialogs)
342 parent
= GetParent();
345 while ( parent
&& !parent
->IsTopLevel() )
347 parent
= parent
->GetParent();
351 // did we find the parent?
355 direction
|= wxCENTRE_ON_SCREEN
;
359 if ( direction
& wxCENTRE_ON_SCREEN
)
361 // centre with respect to the whole screen
362 wxDisplaySize(&widthParent
, &heightParent
);
368 // centre on the parent
369 parent
->GetSize(&widthParent
, &heightParent
);
371 // adjust to the parents position
372 posParent
= parent
->GetPosition();
376 // centre inside the parents client rectangle
377 parent
->GetClientSize(&widthParent
, &heightParent
);
382 GetSize(&width
, &height
);
387 if ( direction
& wxHORIZONTAL
)
388 xNew
= (widthParent
- width
)/2;
390 if ( direction
& wxVERTICAL
)
391 yNew
= (heightParent
- height
)/2;
396 // move the window to this position (keeping the old size but using
397 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
398 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
401 // fits the window around the children
402 void wxWindowBase::Fit()
404 if ( GetChildren().GetCount() > 0 )
406 wxSize size
= DoGetBestSize();
408 // for compatibility with the old versions and because it really looks
409 // slightly more pretty like this, add a pad
415 //else: do nothing if we have no children
418 // return the size best suited for the current window
419 wxSize
wxWindowBase::DoGetBestSize() const
421 if ( GetChildren().GetCount() > 0 )
423 // our minimal acceptable size is such that all our windows fit inside
427 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
429 node
= node
->GetNext() )
431 wxWindow
*win
= node
->GetData();
432 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) || !win
->IsShown())
434 // dialogs and frames lie in different top level windows -
435 // don't deal with them here; as for the status bars, they
436 // don't lie in the client area at all
441 win
->GetPosition(&wx
, &wy
);
443 // if the window hadn't been positioned yet, assume that it is in
450 win
->GetSize(&ww
, &wh
);
451 if ( wx
+ ww
> maxX
)
453 if ( wy
+ wh
> maxY
)
457 return wxSize(maxX
, maxY
);
461 // for a generic window there is no natural best size - just use the
467 // set the min/max size of the window
468 void wxWindowBase::SetSizeHints(int minW
, int minH
,
470 int WXUNUSED(incW
), int WXUNUSED(incH
))
478 // ----------------------------------------------------------------------------
479 // show/hide/enable/disable the window
480 // ----------------------------------------------------------------------------
482 bool wxWindowBase::Show(bool show
)
484 if ( show
!= m_isShown
)
496 bool wxWindowBase::Enable(bool enable
)
498 if ( enable
!= m_isEnabled
)
500 m_isEnabled
= enable
;
509 // ----------------------------------------------------------------------------
511 // ----------------------------------------------------------------------------
513 bool wxWindowBase::IsTopLevel() const
518 // ----------------------------------------------------------------------------
519 // reparenting the window
520 // ----------------------------------------------------------------------------
522 void wxWindowBase::AddChild(wxWindowBase
*child
)
524 wxCHECK_RET( child
, wxT("can't add a NULL child") );
526 // this should never happen and it will lead to a crash later if it does
527 // because RemoveChild() will remove only one node from the children list
528 // and the other(s) one(s) will be left with dangling pointers in them
529 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
531 GetChildren().Append(child
);
532 child
->SetParent(this);
535 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
537 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
539 GetChildren().DeleteObject(child
);
540 child
->SetParent((wxWindow
*)NULL
);
543 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
545 wxWindow
*oldParent
= GetParent();
546 if ( newParent
== oldParent
)
552 // unlink this window from the existing parent.
555 oldParent
->RemoveChild(this);
559 wxTopLevelWindows
.DeleteObject(this);
562 // add it to the new one
565 newParent
->AddChild(this);
569 wxTopLevelWindows
.Append(this);
575 // ----------------------------------------------------------------------------
576 // event handler stuff
577 // ----------------------------------------------------------------------------
579 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
581 handler
->SetNextHandler(GetEventHandler());
582 SetEventHandler(handler
);
585 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
587 wxEvtHandler
*handlerA
= GetEventHandler();
590 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
591 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
592 SetEventHandler(handlerB
);
596 handlerA
= (wxEvtHandler
*)NULL
;
603 // ----------------------------------------------------------------------------
605 // ----------------------------------------------------------------------------
607 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
609 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
612 m_backgroundColour
= colour
;
617 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
619 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
622 m_foregroundColour
= colour
;
627 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
629 // setting an invalid cursor is ok, it means that we don't have any special
631 if ( m_cursor
== cursor
)
642 bool wxWindowBase::SetFont(const wxFont
& font
)
644 // don't try to set invalid font, always fall back to the default
645 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
647 if ( (wxFont
&)fontOk
== m_font
)
659 void wxWindowBase::SetCaret(wxCaret
*caret
)
670 wxASSERT_MSG( m_caret
->GetWindow() == this,
671 wxT("caret should be created associated to this window") );
674 #endif // wxUSE_CARET
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
681 void wxWindowBase::SetValidator(const wxValidator
& validator
)
683 if ( m_windowValidator
)
684 delete m_windowValidator
;
686 m_windowValidator
= (wxValidator
*)validator
.Clone();
688 if ( m_windowValidator
)
689 m_windowValidator
->SetWindow(this) ;
691 #endif // wxUSE_VALIDATORS
693 // ----------------------------------------------------------------------------
694 // update region testing
695 // ----------------------------------------------------------------------------
697 bool wxWindowBase::IsExposed(int x
, int y
) const
699 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
702 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
704 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
707 // ----------------------------------------------------------------------------
708 // find window by id or name
709 // ----------------------------------------------------------------------------
711 wxWindow
*wxWindowBase::FindWindow( long id
)
713 if ( id
== m_windowId
)
714 return (wxWindow
*)this;
716 wxWindowBase
*res
= (wxWindow
*)NULL
;
717 wxWindowList::Node
*node
;
718 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
720 wxWindowBase
*child
= node
->GetData();
721 res
= child
->FindWindow( id
);
724 return (wxWindow
*)res
;
727 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
729 if ( name
== m_windowName
)
730 return (wxWindow
*)this;
732 wxWindowBase
*res
= (wxWindow
*)NULL
;
733 wxWindowList::Node
*node
;
734 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
736 wxWindow
*child
= node
->GetData();
737 res
= child
->FindWindow(name
);
740 return (wxWindow
*)res
;
743 // ----------------------------------------------------------------------------
744 // dialog oriented functions
745 // ----------------------------------------------------------------------------
747 void wxWindowBase::MakeModal(bool modal
)
749 // Disable all other windows
752 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
755 wxWindow
*win
= node
->GetData();
759 node
= node
->GetNext();
764 bool wxWindowBase::Validate()
767 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
769 wxWindowList::Node
*node
;
770 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
772 wxWindowBase
*child
= node
->GetData();
773 wxValidator
*validator
= child
->GetValidator();
774 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
779 if ( recurse
&& !child
->Validate() )
784 #endif // wxUSE_VALIDATORS
789 bool wxWindowBase::TransferDataToWindow()
792 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
794 wxWindowList::Node
*node
;
795 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
797 wxWindowBase
*child
= node
->GetData();
798 wxValidator
*validator
= child
->GetValidator();
799 if ( validator
&& !validator
->TransferToWindow() )
801 wxLogWarning(_("Could not transfer data to window"));
802 wxLog::FlushActive();
809 if ( !child
->TransferDataToWindow() )
811 // warning already given
816 #endif // wxUSE_VALIDATORS
821 bool wxWindowBase::TransferDataFromWindow()
824 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
826 wxWindowList::Node
*node
;
827 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
829 wxWindow
*child
= node
->GetData();
830 wxValidator
*validator
= child
->GetValidator();
831 if ( validator
&& !validator
->TransferFromWindow() )
833 // nop warning here because the application is supposed to give
834 // one itself - we don't know here what might have gone wrongly
841 if ( !child
->TransferDataFromWindow() )
843 // warning already given
848 #endif // wxUSE_VALIDATORS
853 void wxWindowBase::InitDialog()
855 wxInitDialogEvent
event(GetId());
856 event
.SetEventObject( this );
857 GetEventHandler()->ProcessEvent(event
);
860 // ----------------------------------------------------------------------------
861 // context-sensitive help support
862 // ----------------------------------------------------------------------------
866 // associate this help text with this window
867 void wxWindowBase::SetHelpText(const wxString
& text
)
869 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
872 helpProvider
->AddHelp(this, text
);
876 // associate this help text with all windows with the same id as this
878 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
880 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
883 helpProvider
->AddHelp(GetId(), text
);
887 // get the help string associated with this window (may be empty)
888 wxString
wxWindowBase::GetHelpText() const
891 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
894 text
= helpProvider
->GetHelp(this);
900 // show help for this window
901 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
903 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
906 if ( helpProvider
->ShowHelp(this) )
908 // skip the event.Skip() below
918 // ----------------------------------------------------------------------------
920 // ----------------------------------------------------------------------------
924 void wxWindowBase::SetToolTip( const wxString
&tip
)
926 // don't create the new tooltip if we already have one
929 m_tooltip
->SetTip( tip
);
933 SetToolTip( new wxToolTip( tip
) );
936 // setting empty tooltip text does not remove the tooltip any more - use
937 // SetToolTip((wxToolTip *)NULL) for this
940 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
948 #endif // wxUSE_TOOLTIPS
950 // ----------------------------------------------------------------------------
951 // constraints and sizers
952 // ----------------------------------------------------------------------------
954 #if wxUSE_CONSTRAINTS
956 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
960 UnsetConstraints(m_constraints
);
961 delete m_constraints
;
963 m_constraints
= constraints
;
966 // Make sure other windows know they're part of a 'meaningful relationship'
967 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
968 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
969 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
970 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
971 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
972 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
973 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
974 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
975 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
976 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
977 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
978 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
979 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
980 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
981 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
982 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
986 // This removes any dangling pointers to this window in other windows'
987 // constraintsInvolvedIn lists.
988 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
992 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
993 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
994 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
995 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
996 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
997 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
998 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
999 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1000 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1001 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1002 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1003 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1004 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1005 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1006 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1007 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1011 // Back-pointer to other windows we're involved with, so if we delete this
1012 // window, we must delete any constraints we're involved with.
1013 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1015 if ( !m_constraintsInvolvedIn
)
1016 m_constraintsInvolvedIn
= new wxWindowList
;
1017 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1018 m_constraintsInvolvedIn
->Append(otherWin
);
1021 // REMOVE back-pointer to other windows we're involved with.
1022 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1024 if ( m_constraintsInvolvedIn
)
1025 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1028 // Reset any constraints that mention this window
1029 void wxWindowBase::DeleteRelatedConstraints()
1031 if ( m_constraintsInvolvedIn
)
1033 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1036 wxWindow
*win
= node
->GetData();
1037 wxLayoutConstraints
*constr
= win
->GetConstraints();
1039 // Reset any constraints involving this window
1042 constr
->left
.ResetIfWin(this);
1043 constr
->top
.ResetIfWin(this);
1044 constr
->right
.ResetIfWin(this);
1045 constr
->bottom
.ResetIfWin(this);
1046 constr
->width
.ResetIfWin(this);
1047 constr
->height
.ResetIfWin(this);
1048 constr
->centreX
.ResetIfWin(this);
1049 constr
->centreY
.ResetIfWin(this);
1052 wxWindowList::Node
*next
= node
->GetNext();
1057 delete m_constraintsInvolvedIn
;
1058 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1062 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1064 if (m_windowSizer
) delete m_windowSizer
;
1066 m_windowSizer
= sizer
;
1069 bool wxWindowBase::Layout()
1071 // If there is a sizer, use it instead of the constraints
1075 GetClientSize(&w
, &h
);
1077 GetSizer()->SetDimension( 0, 0, w
, h
);
1081 wxLayoutConstraints
*constr
= GetConstraints();
1082 bool wasOk
= constr
&& constr
->AreSatisfied();
1084 ResetConstraints(); // Mark all constraints as unevaluated
1086 // if we're a top level panel (i.e. our parent is frame/dialog), our
1087 // own constraints will never be satisfied any more unless we do it
1092 while ( noChanges
> 0 )
1094 constr
->SatisfyConstraints(this, &noChanges
);
1098 DoPhase(1); // Layout children
1099 DoPhase(2); // Layout grand children
1100 SetConstraintSizes(); // Recursively set the real window sizes
1107 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1108 // do a similar thing, but also impose their own 'constraints' and order the
1109 // evaluation differently.
1110 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1112 wxLayoutConstraints
*constr
= GetConstraints();
1115 return constr
->SatisfyConstraints(this, noChanges
);
1121 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1131 // Do a phase of evaluating child constraints
1132 bool wxWindowBase::DoPhase(int phase
)
1134 int noIterations
= 0;
1135 int maxIterations
= 500;
1138 wxWindowList succeeded
;
1139 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1143 wxWindowList::Node
*node
= GetChildren().GetFirst();
1146 wxWindow
*child
= node
->GetData();
1147 if ( !child
->IsTopLevel() )
1149 wxLayoutConstraints
*constr
= child
->GetConstraints();
1152 if ( !succeeded
.Find(child
) )
1154 int tempNoChanges
= 0;
1155 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1156 noChanges
+= tempNoChanges
;
1159 succeeded
.Append(child
);
1164 node
= node
->GetNext();
1173 void wxWindowBase::ResetConstraints()
1175 wxLayoutConstraints
*constr
= GetConstraints();
1178 constr
->left
.SetDone(FALSE
);
1179 constr
->top
.SetDone(FALSE
);
1180 constr
->right
.SetDone(FALSE
);
1181 constr
->bottom
.SetDone(FALSE
);
1182 constr
->width
.SetDone(FALSE
);
1183 constr
->height
.SetDone(FALSE
);
1184 constr
->centreX
.SetDone(FALSE
);
1185 constr
->centreY
.SetDone(FALSE
);
1188 wxWindowList::Node
*node
= GetChildren().GetFirst();
1191 wxWindow
*win
= node
->GetData();
1192 if ( !win
->IsTopLevel() )
1193 win
->ResetConstraints();
1194 node
= node
->GetNext();
1198 // Need to distinguish between setting the 'fake' size for windows and sizers,
1199 // and setting the real values.
1200 void wxWindowBase::SetConstraintSizes(bool recurse
)
1202 wxLayoutConstraints
*constr
= GetConstraints();
1203 if ( constr
&& constr
->AreSatisfied() )
1205 int x
= constr
->left
.GetValue();
1206 int y
= constr
->top
.GetValue();
1207 int w
= constr
->width
.GetValue();
1208 int h
= constr
->height
.GetValue();
1210 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1211 (constr
->height
.GetRelationship() != wxAsIs
) )
1213 SetSize(x
, y
, w
, h
);
1217 // If we don't want to resize this window, just move it...
1223 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1224 GetClassInfo()->GetClassName(),
1230 wxWindowList::Node
*node
= GetChildren().GetFirst();
1233 wxWindow
*win
= node
->GetData();
1234 if ( !win
->IsTopLevel() )
1235 win
->SetConstraintSizes();
1236 node
= node
->GetNext();
1241 // Only set the size/position of the constraint (if any)
1242 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1244 wxLayoutConstraints
*constr
= GetConstraints();
1249 constr
->left
.SetValue(x
);
1250 constr
->left
.SetDone(TRUE
);
1254 constr
->top
.SetValue(y
);
1255 constr
->top
.SetDone(TRUE
);
1259 constr
->width
.SetValue(w
);
1260 constr
->width
.SetDone(TRUE
);
1264 constr
->height
.SetValue(h
);
1265 constr
->height
.SetDone(TRUE
);
1270 void wxWindowBase::MoveConstraint(int x
, int y
)
1272 wxLayoutConstraints
*constr
= GetConstraints();
1277 constr
->left
.SetValue(x
);
1278 constr
->left
.SetDone(TRUE
);
1282 constr
->top
.SetValue(y
);
1283 constr
->top
.SetDone(TRUE
);
1288 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1290 wxLayoutConstraints
*constr
= GetConstraints();
1293 *w
= constr
->width
.GetValue();
1294 *h
= constr
->height
.GetValue();
1300 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1302 wxLayoutConstraints
*constr
= GetConstraints();
1305 *w
= constr
->width
.GetValue();
1306 *h
= constr
->height
.GetValue();
1309 GetClientSize(w
, h
);
1312 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1314 wxLayoutConstraints
*constr
= GetConstraints();
1317 *x
= constr
->left
.GetValue();
1318 *y
= constr
->top
.GetValue();
1324 #endif // wxUSE_CONSTRAINTS
1326 // ----------------------------------------------------------------------------
1327 // do Update UI processing for child controls
1328 // ----------------------------------------------------------------------------
1330 // TODO: should this be implemented for the child window rather
1331 // than the parent? Then you can override it e.g. for wxCheckBox
1332 // to do the Right Thing rather than having to assume a fixed number
1333 // of control classes.
1334 void wxWindowBase::UpdateWindowUI()
1336 wxUpdateUIEvent
event(GetId());
1337 event
.m_eventObject
= this;
1339 if ( GetEventHandler()->ProcessEvent(event
) )
1341 if ( event
.GetSetEnabled() )
1342 Enable(event
.GetEnabled());
1344 if ( event
.GetSetText() )
1346 wxControl
*control
= wxDynamicCast(this, wxControl
);
1349 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1351 text
->SetValue(event
.GetText());
1353 control
->SetLabel(event
.GetText());
1358 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1361 if ( event
.GetSetChecked() )
1362 checkbox
->SetValue(event
.GetChecked());
1364 #endif // wxUSE_CHECKBOX
1367 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1370 if ( event
.GetSetChecked() )
1371 radiobtn
->SetValue(event
.GetChecked());
1373 #endif // wxUSE_RADIOBTN
1377 // ----------------------------------------------------------------------------
1378 // dialog units translations
1379 // ----------------------------------------------------------------------------
1381 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1383 int charWidth
= GetCharWidth();
1384 int charHeight
= GetCharHeight();
1385 wxPoint
pt2(-1, -1);
1387 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1389 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1394 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1396 int charWidth
= GetCharWidth();
1397 int charHeight
= GetCharHeight();
1398 wxPoint
pt2(-1, -1);
1400 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1402 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1407 // ----------------------------------------------------------------------------
1409 // ----------------------------------------------------------------------------
1411 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1413 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1414 wxT("can't have both object and void client data") );
1416 if ( m_clientObject
)
1417 delete m_clientObject
;
1419 m_clientObject
= data
;
1420 m_clientDataType
= ClientData_Object
;
1423 wxClientData
*wxWindowBase::DoGetClientObject() const
1425 // it's not an error to call GetClientObject() on a window which doesn't
1426 // have client data at all - NULL will be returned
1427 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1428 wxT("this window doesn't have object client data") );
1430 return m_clientObject
;
1433 void wxWindowBase::DoSetClientData( void *data
)
1435 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1436 wxT("can't have both object and void client data") );
1438 m_clientData
= data
;
1439 m_clientDataType
= ClientData_Void
;
1442 void *wxWindowBase::DoGetClientData() const
1444 // it's not an error to call GetClientData() on a window which doesn't have
1445 // client data at all - NULL will be returned
1446 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1447 wxT("this window doesn't have void client data") );
1449 return m_clientData
;
1452 // ----------------------------------------------------------------------------
1454 // ----------------------------------------------------------------------------
1456 // propagate the colour change event to the subwindows
1457 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1459 wxWindowList::Node
*node
= GetChildren().GetFirst();
1462 // Only propagate to non-top-level windows
1463 wxWindow
*win
= node
->GetData();
1464 if ( !win
->IsTopLevel() )
1466 wxSysColourChangedEvent event2
;
1467 event
.m_eventObject
= win
;
1468 win
->GetEventHandler()->ProcessEvent(event2
);
1471 node
= node
->GetNext();
1475 // the default action is to populate dialog with data when it's created
1476 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1478 TransferDataToWindow();
1481 // process Ctrl-Alt-mclick
1482 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1484 if ( event
.ControlDown() && event
.AltDown() )
1486 // don't translate these strings
1488 switch ( wxGetOsVersion() )
1490 case wxMOTIF_X
: port
= _T("Motif"); break;
1491 case wxMACINTOSH
: port
= _T("Mac"); break;
1492 case wxBEOS
: port
= _T("BeOS"); break;
1496 case wxGTK_BEOS
: port
= _T("GTK"); break;
1502 case wxWIN386
: port
= _T("MS Windows"); break;
1506 case wxMGL_OS2
: port
= _T("MGL"); break;
1508 case wxOS2_PM
: port
= _T("OS/2"); break;
1509 default: port
= _T("unknown"); break;
1512 wxMessageBox(wxString::Format(
1514 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1523 _T("wxWindows information"),
1524 wxICON_INFORMATION
| wxOK
,
1533 // ----------------------------------------------------------------------------
1534 // list classes implementation
1535 // ----------------------------------------------------------------------------
1537 void wxWindowListNode::DeleteData()
1539 delete (wxWindow
*)GetData();