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 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
);
131 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
133 // GRG, changed Mar/2000
134 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
140 // an optimization for the event processing: checking this flag is much
141 // faster than using IsKindOf(CLASSINFO(wxWindow))
144 #if wxUSE_CONSTRAINTS
145 // no constraints whatsoever
146 m_constraints
= (wxLayoutConstraints
*) NULL
;
147 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
148 m_windowSizer
= (wxSizer
*) NULL
;
149 m_autoLayout
= FALSE
;
150 #endif // wxUSE_CONSTRAINTS
152 #if wxUSE_DRAG_AND_DROP
153 m_dropTarget
= (wxDropTarget
*)NULL
;
154 #endif // wxUSE_DRAG_AND_DROP
157 m_tooltip
= (wxToolTip
*)NULL
;
158 #endif // wxUSE_TOOLTIPS
161 m_caret
= (wxCaret
*)NULL
;
162 #endif // wxUSE_CARET
164 // Whether we're using the current theme for this window (wxGTK only for now)
165 m_themeEnabled
= FALSE
;
168 // common part of window creation process
169 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
171 const wxPoint
& WXUNUSED(pos
),
172 const wxSize
& WXUNUSED(size
),
174 const wxValidator
& validator
,
175 const wxString
& name
)
177 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
178 // member variables - check that it has been called (will catch the case
179 // when a new ctor is added which doesn't call InitWindow)
180 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
182 // generate a new id if the user doesn't care about it
183 m_windowId
= id
== -1 ? NewControlId() : id
;
186 SetWindowStyleFlag(style
);
190 SetValidator(validator
);
191 #endif // wxUSE_VALIDATORS
193 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
194 // have it too - like this it's possible to set it only in the top level
195 // dialog/frame and all children will inherit it by defult
196 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
198 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
209 wxWindowBase::~wxWindowBase()
211 // FIXME if these 2 cases result from programming errors in the user code
212 // we should probably assert here instead of silently fixing them
214 // Just in case the window has been Closed, but we're then deleting
215 // immediately: don't leave dangling pointers.
216 wxPendingDelete
.DeleteObject(this);
218 // Just in case we've loaded a top-level window via LoadNativeDialog but
219 // we weren't a dialog class
220 wxTopLevelWindows
.DeleteObject(this);
222 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
224 // make sure that there are no dangling pointers left pointing to us
225 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
228 if ( panel
->GetLastFocus() == this )
230 panel
->SetLastFocus((wxWindow
*)NULL
);
237 #endif // wxUSE_CARET
240 if ( m_windowValidator
)
241 delete m_windowValidator
;
242 #endif // wxUSE_VALIDATORS
244 // we only delete object data, not untyped
245 if ( m_clientDataType
== ClientData_Object
)
246 delete m_clientObject
;
248 #if wxUSE_CONSTRAINTS
249 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
250 // at deleted windows as they delete themselves.
251 DeleteRelatedConstraints();
255 // This removes any dangling pointers to this window in other windows'
256 // constraintsInvolvedIn lists.
257 UnsetConstraints(m_constraints
);
258 delete m_constraints
;
259 m_constraints
= NULL
;
263 delete m_windowSizer
;
265 #endif // wxUSE_CONSTRAINTS
267 #if wxUSE_DRAG_AND_DROP
270 #endif // wxUSE_DRAG_AND_DROP
275 #endif // wxUSE_TOOLTIPS
278 bool wxWindowBase::Destroy()
285 bool wxWindowBase::Close(bool force
)
287 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
288 event
.SetEventObject(this);
289 #if WXWIN_COMPATIBILITY
290 event
.SetForce(force
);
291 #endif // WXWIN_COMPATIBILITY
292 event
.SetCanVeto(!force
);
294 // return FALSE if window wasn't closed because the application vetoed the
296 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
299 bool wxWindowBase::DestroyChildren()
301 wxWindowList::Node
*node
;
304 // we iterate until the list becomes empty
305 node
= GetChildren().GetFirst();
309 wxWindow
*child
= node
->GetData();
311 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
315 wxASSERT_MSG( !GetChildren().Find(child
),
316 wxT("child didn't remove itself using RemoveChild()") );
322 // ----------------------------------------------------------------------------
323 // size/position related methods
324 // ----------------------------------------------------------------------------
326 // centre the window with respect to its parent in either (or both) directions
327 void wxWindowBase::Centre(int direction
)
329 // the position/size of the parent window or of the entire screen
331 int widthParent
, heightParent
;
333 wxWindow
*parent
= NULL
;
335 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
337 // find the parent to centre this window on: it should be the
338 // immediate parent for the controls but the top level parent for the
339 // top level windows (like dialogs)
340 parent
= GetParent();
343 while ( parent
&& !parent
->IsTopLevel() )
345 parent
= parent
->GetParent();
349 // did we find the parent?
353 direction
|= wxCENTRE_ON_SCREEN
;
357 if ( direction
& wxCENTRE_ON_SCREEN
)
359 // centre with respect to the whole screen
360 wxDisplaySize(&widthParent
, &heightParent
);
366 // centre on the parent
367 parent
->GetSize(&widthParent
, &heightParent
);
369 // adjust to the parents position
370 posParent
= parent
->GetPosition();
374 // centre inside the parents client rectangle
375 parent
->GetClientSize(&widthParent
, &heightParent
);
380 GetSize(&width
, &height
);
385 if ( direction
& wxHORIZONTAL
)
386 xNew
= (widthParent
- width
)/2;
388 if ( direction
& wxVERTICAL
)
389 yNew
= (heightParent
- height
)/2;
394 // move the window to this position (keeping the old size but using
395 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
396 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
399 // fits the window around the children
400 void wxWindowBase::Fit()
402 if ( GetChildren().GetCount() > 0 )
404 wxSize size
= DoGetBestSize();
406 // for compatibility with the old versions and because it really looks
407 // slightly more pretty like this, add a pad
413 //else: do nothing if we have no children
416 // return the size best suited for the current window
417 wxSize
wxWindowBase::DoGetBestSize() const
419 if ( GetChildren().GetCount() > 0 )
421 // our minimal acceptable size is such that all our windows fit inside
425 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
427 node
= node
->GetNext() )
429 wxWindow
*win
= node
->GetData();
430 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) || !win
->IsShown())
432 // dialogs and frames lie in different top level windows -
433 // don't deal with them here; as for the status bars, they
434 // don't lie in the client area at all
439 win
->GetPosition(&wx
, &wy
);
441 // if the window hadn't been positioned yet, assume that it is in
448 win
->GetSize(&ww
, &wh
);
449 if ( wx
+ ww
> maxX
)
451 if ( wy
+ wh
> maxY
)
455 return wxSize(maxX
, maxY
);
459 // for a generic window there is no natural best size - just use the
465 // set the min/max size of the window
466 void wxWindowBase::SetSizeHints(int minW
, int minH
,
468 int WXUNUSED(incW
), int WXUNUSED(incH
))
476 // ----------------------------------------------------------------------------
477 // show/hide/enable/disable the window
478 // ----------------------------------------------------------------------------
480 bool wxWindowBase::Show(bool show
)
482 if ( show
!= m_isShown
)
494 bool wxWindowBase::Enable(bool enable
)
496 if ( enable
!= m_isEnabled
)
498 m_isEnabled
= enable
;
507 // ----------------------------------------------------------------------------
509 // ----------------------------------------------------------------------------
511 bool wxWindowBase::IsTopLevel() const
516 // ----------------------------------------------------------------------------
517 // reparenting the window
518 // ----------------------------------------------------------------------------
520 void wxWindowBase::AddChild(wxWindowBase
*child
)
522 wxCHECK_RET( child
, wxT("can't add a NULL child") );
524 // this should never happen and it will lead to a crash later if it does
525 // because RemoveChild() will remove only one node from the children list
526 // and the other(s) one(s) will be left with dangling pointers in them
527 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
529 GetChildren().Append(child
);
530 child
->SetParent(this);
533 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
535 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
537 GetChildren().DeleteObject(child
);
538 child
->SetParent((wxWindow
*)NULL
);
541 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
543 wxWindow
*oldParent
= GetParent();
544 if ( newParent
== oldParent
)
550 // unlink this window from the existing parent.
553 oldParent
->RemoveChild(this);
557 wxTopLevelWindows
.DeleteObject(this);
560 // add it to the new one
563 newParent
->AddChild(this);
567 wxTopLevelWindows
.Append(this);
573 // ----------------------------------------------------------------------------
574 // event handler stuff
575 // ----------------------------------------------------------------------------
577 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
579 handler
->SetNextHandler(GetEventHandler());
580 SetEventHandler(handler
);
583 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
585 wxEvtHandler
*handlerA
= GetEventHandler();
588 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
589 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
590 SetEventHandler(handlerB
);
594 handlerA
= (wxEvtHandler
*)NULL
;
601 // ----------------------------------------------------------------------------
603 // ----------------------------------------------------------------------------
605 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
607 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
610 m_backgroundColour
= colour
;
615 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
617 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
620 m_foregroundColour
= colour
;
625 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
627 // setting an invalid cursor is ok, it means that we don't have any special
629 if ( m_cursor
== cursor
)
640 bool wxWindowBase::SetFont(const wxFont
& font
)
642 // don't try to set invalid font, always fall back to the default
643 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
645 if ( (wxFont
&)fontOk
== m_font
)
657 void wxWindowBase::SetCaret(wxCaret
*caret
)
668 wxASSERT_MSG( m_caret
->GetWindow() == this,
669 wxT("caret should be created associated to this window") );
672 #endif // wxUSE_CARET
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
679 void wxWindowBase::SetValidator(const wxValidator
& validator
)
681 if ( m_windowValidator
)
682 delete m_windowValidator
;
684 m_windowValidator
= (wxValidator
*)validator
.Clone();
686 if ( m_windowValidator
)
687 m_windowValidator
->SetWindow(this) ;
689 #endif // wxUSE_VALIDATORS
691 // ----------------------------------------------------------------------------
692 // update region testing
693 // ----------------------------------------------------------------------------
695 bool wxWindowBase::IsExposed(int x
, int y
) const
697 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
700 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
702 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
705 // ----------------------------------------------------------------------------
706 // find window by id or name
707 // ----------------------------------------------------------------------------
709 wxWindow
*wxWindowBase::FindWindow( long id
)
711 if ( id
== m_windowId
)
712 return (wxWindow
*)this;
714 wxWindowBase
*res
= (wxWindow
*)NULL
;
715 wxWindowList::Node
*node
;
716 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
718 wxWindowBase
*child
= node
->GetData();
719 res
= child
->FindWindow( id
);
722 return (wxWindow
*)res
;
725 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
727 if ( name
== m_windowName
)
728 return (wxWindow
*)this;
730 wxWindowBase
*res
= (wxWindow
*)NULL
;
731 wxWindowList::Node
*node
;
732 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
734 wxWindow
*child
= node
->GetData();
735 res
= child
->FindWindow(name
);
738 return (wxWindow
*)res
;
741 // ----------------------------------------------------------------------------
742 // dialog oriented functions
743 // ----------------------------------------------------------------------------
745 void wxWindowBase::MakeModal(bool modal
)
747 // Disable all other windows
750 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
753 wxWindow
*win
= node
->GetData();
757 node
= node
->GetNext();
762 bool wxWindowBase::Validate()
765 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
767 wxWindowList::Node
*node
;
768 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
770 wxWindowBase
*child
= node
->GetData();
771 wxValidator
*validator
= child
->GetValidator();
772 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
777 if ( recurse
&& !child
->Validate() )
782 #endif // wxUSE_VALIDATORS
787 bool wxWindowBase::TransferDataToWindow()
790 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
792 wxWindowList::Node
*node
;
793 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
795 wxWindowBase
*child
= node
->GetData();
796 wxValidator
*validator
= child
->GetValidator();
797 if ( validator
&& !validator
->TransferToWindow() )
799 wxLogWarning(_("Could not transfer data to window"));
800 wxLog::FlushActive();
807 if ( !child
->TransferDataToWindow() )
809 // warning already given
814 #endif // wxUSE_VALIDATORS
819 bool wxWindowBase::TransferDataFromWindow()
822 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
824 wxWindowList::Node
*node
;
825 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
827 wxWindow
*child
= node
->GetData();
828 wxValidator
*validator
= child
->GetValidator();
829 if ( validator
&& !validator
->TransferFromWindow() )
831 // nop warning here because the application is supposed to give
832 // one itself - we don't know here what might have gone wrongly
839 if ( !child
->TransferDataFromWindow() )
841 // warning already given
846 #endif // wxUSE_VALIDATORS
851 void wxWindowBase::InitDialog()
853 wxInitDialogEvent
event(GetId());
854 event
.SetEventObject( this );
855 GetEventHandler()->ProcessEvent(event
);
858 // ----------------------------------------------------------------------------
859 // context-sensitive help support
860 // ----------------------------------------------------------------------------
864 // associate this help text with this window
865 void wxWindowBase::SetHelpText(const wxString
& text
)
867 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
870 helpProvider
->AddHelp(this, text
);
874 // associate this help text with all windows with the same id as this
876 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
878 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
881 helpProvider
->AddHelp(GetId(), text
);
885 // get the help string associated with this window (may be empty)
886 wxString
wxWindowBase::GetHelpText() const
889 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
892 text
= helpProvider
->GetHelp(this);
898 // show help for this window
899 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
901 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
904 if ( helpProvider
->ShowHelp(this) )
906 // skip the event.Skip() below
916 // ----------------------------------------------------------------------------
918 // ----------------------------------------------------------------------------
922 void wxWindowBase::SetToolTip( const wxString
&tip
)
924 // don't create the new tooltip if we already have one
927 m_tooltip
->SetTip( tip
);
931 SetToolTip( new wxToolTip( tip
) );
934 // setting empty tooltip text does not remove the tooltip any more - use
935 // SetToolTip((wxToolTip *)NULL) for this
938 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
946 #endif // wxUSE_TOOLTIPS
948 // ----------------------------------------------------------------------------
949 // constraints and sizers
950 // ----------------------------------------------------------------------------
952 #if wxUSE_CONSTRAINTS
954 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
958 UnsetConstraints(m_constraints
);
959 delete m_constraints
;
961 m_constraints
= constraints
;
964 // Make sure other windows know they're part of a 'meaningful relationship'
965 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
966 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
967 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
968 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
969 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
970 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
971 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
972 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
973 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
974 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
975 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
976 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
977 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
978 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
979 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
980 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
984 // This removes any dangling pointers to this window in other windows'
985 // constraintsInvolvedIn lists.
986 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
990 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
991 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
992 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
993 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
994 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
995 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
996 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
997 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
998 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
999 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1000 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1001 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1002 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1003 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1004 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1005 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1009 // Back-pointer to other windows we're involved with, so if we delete this
1010 // window, we must delete any constraints we're involved with.
1011 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1013 if ( !m_constraintsInvolvedIn
)
1014 m_constraintsInvolvedIn
= new wxWindowList
;
1015 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1016 m_constraintsInvolvedIn
->Append(otherWin
);
1019 // REMOVE back-pointer to other windows we're involved with.
1020 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1022 if ( m_constraintsInvolvedIn
)
1023 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1026 // Reset any constraints that mention this window
1027 void wxWindowBase::DeleteRelatedConstraints()
1029 if ( m_constraintsInvolvedIn
)
1031 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1034 wxWindow
*win
= node
->GetData();
1035 wxLayoutConstraints
*constr
= win
->GetConstraints();
1037 // Reset any constraints involving this window
1040 constr
->left
.ResetIfWin(this);
1041 constr
->top
.ResetIfWin(this);
1042 constr
->right
.ResetIfWin(this);
1043 constr
->bottom
.ResetIfWin(this);
1044 constr
->width
.ResetIfWin(this);
1045 constr
->height
.ResetIfWin(this);
1046 constr
->centreX
.ResetIfWin(this);
1047 constr
->centreY
.ResetIfWin(this);
1050 wxWindowList::Node
*next
= node
->GetNext();
1055 delete m_constraintsInvolvedIn
;
1056 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1060 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1062 if (m_windowSizer
) delete m_windowSizer
;
1064 m_windowSizer
= sizer
;
1067 bool wxWindowBase::Layout()
1069 // If there is a sizer, use it instead of the constraints
1073 GetClientSize(&w
, &h
);
1075 GetSizer()->SetDimension( 0, 0, w
, h
);
1079 wxLayoutConstraints
*constr
= GetConstraints();
1080 bool wasOk
= constr
&& constr
->AreSatisfied();
1082 ResetConstraints(); // Mark all constraints as unevaluated
1084 // if we're a top level panel (i.e. our parent is frame/dialog), our
1085 // own constraints will never be satisfied any more unless we do it
1090 while ( noChanges
> 0 )
1092 constr
->SatisfyConstraints(this, &noChanges
);
1096 DoPhase(1); // Layout children
1097 DoPhase(2); // Layout grand children
1098 SetConstraintSizes(); // Recursively set the real window sizes
1105 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1106 // do a similar thing, but also impose their own 'constraints' and order the
1107 // evaluation differently.
1108 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1110 wxLayoutConstraints
*constr
= GetConstraints();
1113 return constr
->SatisfyConstraints(this, noChanges
);
1119 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1129 // Do a phase of evaluating child constraints
1130 bool wxWindowBase::DoPhase(int phase
)
1132 int noIterations
= 0;
1133 int maxIterations
= 500;
1136 wxWindowList succeeded
;
1137 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1141 wxWindowList::Node
*node
= GetChildren().GetFirst();
1144 wxWindow
*child
= node
->GetData();
1145 if ( !child
->IsTopLevel() )
1147 wxLayoutConstraints
*constr
= child
->GetConstraints();
1150 if ( !succeeded
.Find(child
) )
1152 int tempNoChanges
= 0;
1153 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1154 noChanges
+= tempNoChanges
;
1157 succeeded
.Append(child
);
1162 node
= node
->GetNext();
1171 void wxWindowBase::ResetConstraints()
1173 wxLayoutConstraints
*constr
= GetConstraints();
1176 constr
->left
.SetDone(FALSE
);
1177 constr
->top
.SetDone(FALSE
);
1178 constr
->right
.SetDone(FALSE
);
1179 constr
->bottom
.SetDone(FALSE
);
1180 constr
->width
.SetDone(FALSE
);
1181 constr
->height
.SetDone(FALSE
);
1182 constr
->centreX
.SetDone(FALSE
);
1183 constr
->centreY
.SetDone(FALSE
);
1186 wxWindowList::Node
*node
= GetChildren().GetFirst();
1189 wxWindow
*win
= node
->GetData();
1190 if ( !win
->IsTopLevel() )
1191 win
->ResetConstraints();
1192 node
= node
->GetNext();
1196 // Need to distinguish between setting the 'fake' size for windows and sizers,
1197 // and setting the real values.
1198 void wxWindowBase::SetConstraintSizes(bool recurse
)
1200 wxLayoutConstraints
*constr
= GetConstraints();
1201 if ( constr
&& constr
->AreSatisfied() )
1203 int x
= constr
->left
.GetValue();
1204 int y
= constr
->top
.GetValue();
1205 int w
= constr
->width
.GetValue();
1206 int h
= constr
->height
.GetValue();
1208 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1209 (constr
->height
.GetRelationship() != wxAsIs
) )
1211 SetSize(x
, y
, w
, h
);
1215 // If we don't want to resize this window, just move it...
1221 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1222 GetClassInfo()->GetClassName(),
1228 wxWindowList::Node
*node
= GetChildren().GetFirst();
1231 wxWindow
*win
= node
->GetData();
1232 if ( !win
->IsTopLevel() )
1233 win
->SetConstraintSizes();
1234 node
= node
->GetNext();
1239 // Only set the size/position of the constraint (if any)
1240 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1242 wxLayoutConstraints
*constr
= GetConstraints();
1247 constr
->left
.SetValue(x
);
1248 constr
->left
.SetDone(TRUE
);
1252 constr
->top
.SetValue(y
);
1253 constr
->top
.SetDone(TRUE
);
1257 constr
->width
.SetValue(w
);
1258 constr
->width
.SetDone(TRUE
);
1262 constr
->height
.SetValue(h
);
1263 constr
->height
.SetDone(TRUE
);
1268 void wxWindowBase::MoveConstraint(int x
, int y
)
1270 wxLayoutConstraints
*constr
= GetConstraints();
1275 constr
->left
.SetValue(x
);
1276 constr
->left
.SetDone(TRUE
);
1280 constr
->top
.SetValue(y
);
1281 constr
->top
.SetDone(TRUE
);
1286 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1288 wxLayoutConstraints
*constr
= GetConstraints();
1291 *w
= constr
->width
.GetValue();
1292 *h
= constr
->height
.GetValue();
1298 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1300 wxLayoutConstraints
*constr
= GetConstraints();
1303 *w
= constr
->width
.GetValue();
1304 *h
= constr
->height
.GetValue();
1307 GetClientSize(w
, h
);
1310 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1312 wxLayoutConstraints
*constr
= GetConstraints();
1315 *x
= constr
->left
.GetValue();
1316 *y
= constr
->top
.GetValue();
1322 #endif // wxUSE_CONSTRAINTS
1324 // ----------------------------------------------------------------------------
1325 // do Update UI processing for child controls
1326 // ----------------------------------------------------------------------------
1328 // TODO: should this be implemented for the child window rather
1329 // than the parent? Then you can override it e.g. for wxCheckBox
1330 // to do the Right Thing rather than having to assume a fixed number
1331 // of control classes.
1332 void wxWindowBase::UpdateWindowUI()
1334 wxUpdateUIEvent
event(GetId());
1335 event
.m_eventObject
= this;
1337 if ( GetEventHandler()->ProcessEvent(event
) )
1339 if ( event
.GetSetEnabled() )
1340 Enable(event
.GetEnabled());
1342 if ( event
.GetSetText() )
1344 wxControl
*control
= wxDynamicCast(this, wxControl
);
1347 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1349 text
->SetValue(event
.GetText());
1351 control
->SetLabel(event
.GetText());
1356 wxCheckBox
*checkbox
= wxDynamicCast(this, wxCheckBox
);
1359 if ( event
.GetSetChecked() )
1360 checkbox
->SetValue(event
.GetChecked());
1362 #endif // wxUSE_CHECKBOX
1365 wxRadioButton
*radiobtn
= wxDynamicCast(this, wxRadioButton
);
1368 if ( event
.GetSetChecked() )
1369 radiobtn
->SetValue(event
.GetChecked());
1371 #endif // wxUSE_RADIOBTN
1375 // ----------------------------------------------------------------------------
1376 // dialog units translations
1377 // ----------------------------------------------------------------------------
1379 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1381 int charWidth
= GetCharWidth();
1382 int charHeight
= GetCharHeight();
1383 wxPoint
pt2(-1, -1);
1385 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1387 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1392 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1394 int charWidth
= GetCharWidth();
1395 int charHeight
= GetCharHeight();
1396 wxPoint
pt2(-1, -1);
1398 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1400 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1405 // ----------------------------------------------------------------------------
1407 // ----------------------------------------------------------------------------
1409 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1411 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1412 wxT("can't have both object and void client data") );
1414 if ( m_clientObject
)
1415 delete m_clientObject
;
1417 m_clientObject
= data
;
1418 m_clientDataType
= ClientData_Object
;
1421 wxClientData
*wxWindowBase::DoGetClientObject() const
1423 // it's not an error to call GetClientObject() on a window which doesn't
1424 // have client data at all - NULL will be returned
1425 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1426 wxT("this window doesn't have object client data") );
1428 return m_clientObject
;
1431 void wxWindowBase::DoSetClientData( void *data
)
1433 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1434 wxT("can't have both object and void client data") );
1436 m_clientData
= data
;
1437 m_clientDataType
= ClientData_Void
;
1440 void *wxWindowBase::DoGetClientData() const
1442 // it's not an error to call GetClientData() on a window which doesn't have
1443 // client data at all - NULL will be returned
1444 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1445 wxT("this window doesn't have void client data") );
1447 return m_clientData
;
1450 // ----------------------------------------------------------------------------
1452 // ----------------------------------------------------------------------------
1454 // propagate the colour change event to the subwindows
1455 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1457 wxWindowList::Node
*node
= GetChildren().GetFirst();
1460 // Only propagate to non-top-level windows
1461 wxWindow
*win
= node
->GetData();
1462 if ( !win
->IsTopLevel() )
1464 wxSysColourChangedEvent event2
;
1465 event
.m_eventObject
= win
;
1466 win
->GetEventHandler()->ProcessEvent(event2
);
1469 node
= node
->GetNext();
1473 // the default action is to populate dialog with data when it's created
1474 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1476 TransferDataToWindow();
1479 // process Ctrl-Alt-mclick
1480 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1482 if ( event
.ControlDown() && event
.AltDown() )
1484 // don't translate these strings
1486 switch ( wxGetOsVersion() )
1488 case wxMOTIF_X
: port
= _T("Motif"); break;
1489 case wxMACINTOSH
: port
= _T("Mac"); break;
1490 case wxBEOS
: port
= _T("BeOS"); break;
1494 case wxGTK_BEOS
: port
= _T("GTK"); break;
1500 case wxWIN386
: port
= _T("MS Windows"); break;
1504 case wxMGL_OS2
: port
= _T("MGL"); break;
1506 case wxOS2_PM
: port
= _T("OS/2"); break;
1507 default: port
= _T("unknown"); break;
1510 wxMessageBox(wxString::Format(
1512 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1521 _T("wxWindows information"),
1522 wxICON_INFORMATION
| wxOK
,
1531 // ----------------------------------------------------------------------------
1532 // list classes implementation
1533 // ----------------------------------------------------------------------------
1535 void wxWindowListNode::DeleteData()
1537 delete (wxWindow
*)GetData();