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 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
134 // so the font is *not* really set - but calls to SetFont() later won't do
135 // anything because m_font appears to be already set!
137 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
144 // an optimization for the event processing: checking this flag is much
145 // faster than using IsKindOf(CLASSINFO(wxWindow))
148 #if wxUSE_CONSTRAINTS
149 // no constraints whatsoever
150 m_constraints
= (wxLayoutConstraints
*) NULL
;
151 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
152 m_windowSizer
= (wxSizer
*) NULL
;
153 m_autoLayout
= FALSE
;
154 #endif // wxUSE_CONSTRAINTS
156 #if wxUSE_DRAG_AND_DROP
157 m_dropTarget
= (wxDropTarget
*)NULL
;
158 #endif // wxUSE_DRAG_AND_DROP
161 m_tooltip
= (wxToolTip
*)NULL
;
162 #endif // wxUSE_TOOLTIPS
165 m_caret
= (wxCaret
*)NULL
;
166 #endif // wxUSE_CARET
168 // Whether we're using the current theme for this window (wxGTK only for now)
169 m_themeEnabled
= FALSE
;
172 // common part of window creation process
173 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
175 const wxPoint
& WXUNUSED(pos
),
176 const wxSize
& WXUNUSED(size
),
178 const wxValidator
& validator
,
179 const wxString
& name
)
181 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
182 // member variables - check that it has been called (will catch the case
183 // when a new ctor is added which doesn't call InitWindow)
184 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
186 // generate a new id if the user doesn't care about it
187 m_windowId
= id
== -1 ? NewControlId() : id
;
190 SetWindowStyleFlag(style
);
194 SetValidator(validator
);
195 #endif // wxUSE_VALIDATORS
197 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
198 // have it too - like this it's possible to set it only in the top level
199 // dialog/frame and all children will inherit it by defult
200 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
202 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
213 wxWindowBase::~wxWindowBase()
215 // FIXME if these 2 cases result from programming errors in the user code
216 // we should probably assert here instead of silently fixing them
218 // Just in case the window has been Closed, but we're then deleting
219 // immediately: don't leave dangling pointers.
220 wxPendingDelete
.DeleteObject(this);
222 // Just in case we've loaded a top-level window via LoadNativeDialog but
223 // we weren't a dialog class
224 wxTopLevelWindows
.DeleteObject(this);
226 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
231 #endif // wxUSE_CARET
234 if ( m_windowValidator
)
235 delete m_windowValidator
;
236 #endif // wxUSE_VALIDATORS
238 // we only delete object data, not untyped
239 if ( m_clientDataType
== ClientData_Object
)
240 delete m_clientObject
;
242 #if wxUSE_CONSTRAINTS
243 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
244 // at deleted windows as they delete themselves.
245 DeleteRelatedConstraints();
249 // This removes any dangling pointers to this window in other windows'
250 // constraintsInvolvedIn lists.
251 UnsetConstraints(m_constraints
);
252 delete m_constraints
;
253 m_constraints
= NULL
;
257 delete m_windowSizer
;
259 #endif // wxUSE_CONSTRAINTS
261 #if wxUSE_DRAG_AND_DROP
264 #endif // wxUSE_DRAG_AND_DROP
269 #endif // wxUSE_TOOLTIPS
272 bool wxWindowBase::Destroy()
279 bool wxWindowBase::Close(bool force
)
281 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
282 event
.SetEventObject(this);
283 #if WXWIN_COMPATIBILITY
284 event
.SetForce(force
);
285 #endif // WXWIN_COMPATIBILITY
286 event
.SetCanVeto(!force
);
288 // return FALSE if window wasn't closed because the application vetoed the
290 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
293 bool wxWindowBase::DestroyChildren()
295 wxWindowList::Node
*node
;
298 // we iterate until the list becomes empty
299 node
= GetChildren().GetFirst();
303 wxWindow
*child
= node
->GetData();
305 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
309 wxASSERT_MSG( !GetChildren().Find(child
),
310 wxT("child didn't remove itself using RemoveChild()") );
316 // ----------------------------------------------------------------------------
317 // size/position related methods
318 // ----------------------------------------------------------------------------
320 // centre the window with respect to its parent in either (or both) directions
321 void wxWindowBase::Centre(int direction
)
323 // the position/size of the parent window or of the entire screen
325 int widthParent
, heightParent
;
327 wxWindow
*parent
= NULL
;
329 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
331 // find the parent to centre this window on: it should be the
332 // immediate parent for the controls but the top level parent for the
333 // top level windows (like dialogs)
334 parent
= GetParent();
337 while ( parent
&& !parent
->IsTopLevel() )
339 parent
= parent
->GetParent();
343 // did we find the parent?
347 direction
|= wxCENTRE_ON_SCREEN
;
351 if ( direction
& wxCENTRE_ON_SCREEN
)
353 // centre with respect to the whole screen
354 wxDisplaySize(&widthParent
, &heightParent
);
360 // centre on the parent
361 parent
->GetSize(&widthParent
, &heightParent
);
363 // adjust to the parents position
364 posParent
= parent
->GetPosition();
368 // centre inside the parents client rectangle
369 parent
->GetClientSize(&widthParent
, &heightParent
);
374 GetSize(&width
, &height
);
379 if ( direction
& wxHORIZONTAL
)
380 xNew
= (widthParent
- width
)/2;
382 if ( direction
& wxVERTICAL
)
383 yNew
= (heightParent
- height
)/2;
388 // Base size of the visible dimensions of the display
389 // to take into account the taskbar
390 wxRect rect
= wxGetClientDisplayRect();
391 wxSize
size (rect
.width
,rect
.height
);
393 if (posParent
.x
>= 0) // if parent is on the main display
397 else if (xNew
+width
> size
.x
)
398 xNew
= size
.x
-width
-1;
400 if (posParent
.y
>= 0) // if parent is on the main display
402 if (yNew
+height
> size
.y
)
403 yNew
= size
.y
-height
-1;
405 // Make certain that the title bar is initially visible
406 // always, even if this would push the bottom of the
407 // dialog of the visible area of the display
412 // move the window to this position (keeping the old size but using
413 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
414 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
417 // fits the window around the children
418 void wxWindowBase::Fit()
420 if ( GetChildren().GetCount() > 0 )
422 wxSize size
= DoGetBestSize();
424 // for compatibility with the old versions and because it really looks
425 // slightly more pretty like this, add a pad
431 //else: do nothing if we have no children
434 // return the size best suited for the current window
435 wxSize
wxWindowBase::DoGetBestSize() const
437 if ( GetChildren().GetCount() > 0 )
439 // our minimal acceptable size is such that all our windows fit inside
443 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
445 node
= node
->GetNext() )
447 wxWindow
*win
= node
->GetData();
448 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) || !win
->IsShown())
450 // dialogs and frames lie in different top level windows -
451 // don't deal with them here; as for the status bars, they
452 // don't lie in the client area at all
457 win
->GetPosition(&wx
, &wy
);
459 // if the window hadn't been positioned yet, assume that it is in
466 win
->GetSize(&ww
, &wh
);
467 if ( wx
+ ww
> maxX
)
469 if ( wy
+ wh
> maxY
)
473 return wxSize(maxX
, maxY
);
477 // for a generic window there is no natural best size - just use the
483 // set the min/max size of the window
484 void wxWindowBase::SetSizeHints(int minW
, int minH
,
486 int WXUNUSED(incW
), int WXUNUSED(incH
))
494 // ----------------------------------------------------------------------------
495 // show/hide/enable/disable the window
496 // ----------------------------------------------------------------------------
498 bool wxWindowBase::Show(bool show
)
500 if ( show
!= m_isShown
)
512 bool wxWindowBase::Enable(bool enable
)
514 if ( enable
!= m_isEnabled
)
516 m_isEnabled
= enable
;
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 bool wxWindowBase::IsTopLevel() const
534 // ----------------------------------------------------------------------------
535 // reparenting the window
536 // ----------------------------------------------------------------------------
538 void wxWindowBase::AddChild(wxWindowBase
*child
)
540 wxCHECK_RET( child
, wxT("can't add a NULL child") );
542 // this should never happen and it will lead to a crash later if it does
543 // because RemoveChild() will remove only one node from the children list
544 // and the other(s) one(s) will be left with dangling pointers in them
545 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
547 GetChildren().Append(child
);
548 child
->SetParent(this);
551 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
553 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
555 GetChildren().DeleteObject(child
);
556 child
->SetParent((wxWindow
*)NULL
);
559 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
561 wxWindow
*oldParent
= GetParent();
562 if ( newParent
== oldParent
)
568 // unlink this window from the existing parent.
571 oldParent
->RemoveChild(this);
575 wxTopLevelWindows
.DeleteObject(this);
578 // add it to the new one
581 newParent
->AddChild(this);
585 wxTopLevelWindows
.Append(this);
591 // ----------------------------------------------------------------------------
592 // event handler stuff
593 // ----------------------------------------------------------------------------
595 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
597 handler
->SetNextHandler(GetEventHandler());
598 SetEventHandler(handler
);
601 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
603 wxEvtHandler
*handlerA
= GetEventHandler();
606 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
607 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
608 SetEventHandler(handlerB
);
612 handlerA
= (wxEvtHandler
*)NULL
;
619 // ----------------------------------------------------------------------------
621 // ----------------------------------------------------------------------------
623 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
625 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
628 m_backgroundColour
= colour
;
633 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
635 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
638 m_foregroundColour
= colour
;
643 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
645 // setting an invalid cursor is ok, it means that we don't have any special
647 if ( m_cursor
== cursor
)
658 bool wxWindowBase::SetFont(const wxFont
& font
)
660 // don't try to set invalid font, always fall back to the default
661 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
663 if ( fontOk
== m_font
)
675 void wxWindowBase::SetCaret(wxCaret
*caret
)
686 wxASSERT_MSG( m_caret
->GetWindow() == this,
687 wxT("caret should be created associated to this window") );
690 #endif // wxUSE_CARET
693 // ----------------------------------------------------------------------------
695 // ----------------------------------------------------------------------------
697 void wxWindowBase::SetValidator(const wxValidator
& validator
)
699 if ( m_windowValidator
)
700 delete m_windowValidator
;
702 m_windowValidator
= (wxValidator
*)validator
.Clone();
704 if ( m_windowValidator
)
705 m_windowValidator
->SetWindow(this) ;
707 #endif // wxUSE_VALIDATORS
709 // ----------------------------------------------------------------------------
710 // update region testing
711 // ----------------------------------------------------------------------------
713 bool wxWindowBase::IsExposed(int x
, int y
) const
715 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
718 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
720 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
723 // ----------------------------------------------------------------------------
724 // find window by id or name
725 // ----------------------------------------------------------------------------
727 wxWindow
*wxWindowBase::FindWindow( long id
)
729 if ( id
== m_windowId
)
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 wxWindowBase
*child
= node
->GetData();
737 res
= child
->FindWindow( id
);
740 return (wxWindow
*)res
;
743 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
745 if ( name
== m_windowName
)
746 return (wxWindow
*)this;
748 wxWindowBase
*res
= (wxWindow
*)NULL
;
749 wxWindowList::Node
*node
;
750 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
752 wxWindow
*child
= node
->GetData();
753 res
= child
->FindWindow(name
);
756 return (wxWindow
*)res
;
759 // ----------------------------------------------------------------------------
760 // dialog oriented functions
761 // ----------------------------------------------------------------------------
763 void wxWindowBase::MakeModal(bool modal
)
765 // Disable all other windows
768 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
771 wxWindow
*win
= node
->GetData();
775 node
= node
->GetNext();
780 bool wxWindowBase::Validate()
783 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
785 wxWindowList::Node
*node
;
786 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
788 wxWindowBase
*child
= node
->GetData();
789 wxValidator
*validator
= child
->GetValidator();
790 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
795 if ( recurse
&& !child
->Validate() )
800 #endif // wxUSE_VALIDATORS
805 bool wxWindowBase::TransferDataToWindow()
808 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
810 wxWindowList::Node
*node
;
811 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
813 wxWindowBase
*child
= node
->GetData();
814 wxValidator
*validator
= child
->GetValidator();
815 if ( validator
&& !validator
->TransferToWindow() )
817 wxLogWarning(_("Could not transfer data to window"));
818 wxLog::FlushActive();
825 if ( !child
->TransferDataToWindow() )
827 // warning already given
832 #endif // wxUSE_VALIDATORS
837 bool wxWindowBase::TransferDataFromWindow()
840 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
842 wxWindowList::Node
*node
;
843 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
845 wxWindow
*child
= node
->GetData();
846 wxValidator
*validator
= child
->GetValidator();
847 if ( validator
&& !validator
->TransferFromWindow() )
849 // nop warning here because the application is supposed to give
850 // one itself - we don't know here what might have gone wrongly
857 if ( !child
->TransferDataFromWindow() )
859 // warning already given
864 #endif // wxUSE_VALIDATORS
869 void wxWindowBase::InitDialog()
871 wxInitDialogEvent
event(GetId());
872 event
.SetEventObject( this );
873 GetEventHandler()->ProcessEvent(event
);
876 // ----------------------------------------------------------------------------
877 // context-sensitive help support
878 // ----------------------------------------------------------------------------
882 // associate this help text with this window
883 void wxWindowBase::SetHelpText(const wxString
& text
)
885 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
888 helpProvider
->AddHelp(this, text
);
892 // associate this help text with all windows with the same id as this
894 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
896 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
899 helpProvider
->AddHelp(GetId(), text
);
903 // get the help string associated with this window (may be empty)
904 wxString
wxWindowBase::GetHelpText() const
907 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
910 text
= helpProvider
->GetHelp(this);
916 // show help for this window
917 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
919 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
922 if ( helpProvider
->ShowHelp(this) )
924 // skip the event.Skip() below
934 // ----------------------------------------------------------------------------
936 // ----------------------------------------------------------------------------
940 void wxWindowBase::SetToolTip( const wxString
&tip
)
942 // don't create the new tooltip if we already have one
945 m_tooltip
->SetTip( tip
);
949 SetToolTip( new wxToolTip( tip
) );
952 // setting empty tooltip text does not remove the tooltip any more - use
953 // SetToolTip((wxToolTip *)NULL) for this
956 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
964 #endif // wxUSE_TOOLTIPS
966 // ----------------------------------------------------------------------------
967 // constraints and sizers
968 // ----------------------------------------------------------------------------
970 #if wxUSE_CONSTRAINTS
972 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
976 UnsetConstraints(m_constraints
);
977 delete m_constraints
;
979 m_constraints
= constraints
;
982 // Make sure other windows know they're part of a 'meaningful relationship'
983 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
984 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
985 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
986 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
987 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
988 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
989 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
990 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
991 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
992 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
993 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
994 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
995 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
996 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
997 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
998 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1002 // This removes any dangling pointers to this window in other windows'
1003 // constraintsInvolvedIn lists.
1004 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1008 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1009 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1010 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1011 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1012 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1013 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1014 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1015 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1016 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1017 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1018 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1019 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1020 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1021 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1022 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1023 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1027 // Back-pointer to other windows we're involved with, so if we delete this
1028 // window, we must delete any constraints we're involved with.
1029 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1031 if ( !m_constraintsInvolvedIn
)
1032 m_constraintsInvolvedIn
= new wxWindowList
;
1033 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1034 m_constraintsInvolvedIn
->Append(otherWin
);
1037 // REMOVE back-pointer to other windows we're involved with.
1038 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1040 if ( m_constraintsInvolvedIn
)
1041 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1044 // Reset any constraints that mention this window
1045 void wxWindowBase::DeleteRelatedConstraints()
1047 if ( m_constraintsInvolvedIn
)
1049 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1052 wxWindow
*win
= node
->GetData();
1053 wxLayoutConstraints
*constr
= win
->GetConstraints();
1055 // Reset any constraints involving this window
1058 constr
->left
.ResetIfWin(this);
1059 constr
->top
.ResetIfWin(this);
1060 constr
->right
.ResetIfWin(this);
1061 constr
->bottom
.ResetIfWin(this);
1062 constr
->width
.ResetIfWin(this);
1063 constr
->height
.ResetIfWin(this);
1064 constr
->centreX
.ResetIfWin(this);
1065 constr
->centreY
.ResetIfWin(this);
1068 wxWindowList::Node
*next
= node
->GetNext();
1073 delete m_constraintsInvolvedIn
;
1074 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1078 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1080 if (m_windowSizer
) delete m_windowSizer
;
1082 m_windowSizer
= sizer
;
1085 bool wxWindowBase::Layout()
1087 // If there is a sizer, use it instead of the constraints
1091 GetClientSize(&w
, &h
);
1093 GetSizer()->SetDimension( 0, 0, w
, h
);
1097 wxLayoutConstraints
*constr
= GetConstraints();
1098 bool wasOk
= constr
&& constr
->AreSatisfied();
1100 ResetConstraints(); // Mark all constraints as unevaluated
1102 // if we're a top level panel (i.e. our parent is frame/dialog), our
1103 // own constraints will never be satisfied any more unless we do it
1108 while ( noChanges
> 0 )
1110 constr
->SatisfyConstraints(this, &noChanges
);
1114 DoPhase(1); // Layout children
1115 DoPhase(2); // Layout grand children
1116 SetConstraintSizes(); // Recursively set the real window sizes
1123 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1124 // do a similar thing, but also impose their own 'constraints' and order the
1125 // evaluation differently.
1126 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1128 wxLayoutConstraints
*constr
= GetConstraints();
1131 return constr
->SatisfyConstraints(this, noChanges
);
1137 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1147 // Do a phase of evaluating child constraints
1148 bool wxWindowBase::DoPhase(int phase
)
1150 int noIterations
= 0;
1151 int maxIterations
= 500;
1154 wxWindowList succeeded
;
1155 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1159 wxWindowList::Node
*node
= GetChildren().GetFirst();
1162 wxWindow
*child
= node
->GetData();
1163 if ( !child
->IsTopLevel() )
1165 wxLayoutConstraints
*constr
= child
->GetConstraints();
1168 if ( !succeeded
.Find(child
) )
1170 int tempNoChanges
= 0;
1171 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1172 noChanges
+= tempNoChanges
;
1175 succeeded
.Append(child
);
1180 node
= node
->GetNext();
1189 void wxWindowBase::ResetConstraints()
1191 wxLayoutConstraints
*constr
= GetConstraints();
1194 constr
->left
.SetDone(FALSE
);
1195 constr
->top
.SetDone(FALSE
);
1196 constr
->right
.SetDone(FALSE
);
1197 constr
->bottom
.SetDone(FALSE
);
1198 constr
->width
.SetDone(FALSE
);
1199 constr
->height
.SetDone(FALSE
);
1200 constr
->centreX
.SetDone(FALSE
);
1201 constr
->centreY
.SetDone(FALSE
);
1204 wxWindowList::Node
*node
= GetChildren().GetFirst();
1207 wxWindow
*win
= node
->GetData();
1208 if ( !win
->IsTopLevel() )
1209 win
->ResetConstraints();
1210 node
= node
->GetNext();
1214 // Need to distinguish between setting the 'fake' size for windows and sizers,
1215 // and setting the real values.
1216 void wxWindowBase::SetConstraintSizes(bool recurse
)
1218 wxLayoutConstraints
*constr
= GetConstraints();
1219 if ( constr
&& constr
->AreSatisfied() )
1221 int x
= constr
->left
.GetValue();
1222 int y
= constr
->top
.GetValue();
1223 int w
= constr
->width
.GetValue();
1224 int h
= constr
->height
.GetValue();
1226 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1227 (constr
->height
.GetRelationship() != wxAsIs
) )
1229 SetSize(x
, y
, w
, h
);
1233 // If we don't want to resize this window, just move it...
1239 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1240 GetClassInfo()->GetClassName(),
1246 wxWindowList::Node
*node
= GetChildren().GetFirst();
1249 wxWindow
*win
= node
->GetData();
1250 if ( !win
->IsTopLevel() )
1251 win
->SetConstraintSizes();
1252 node
= node
->GetNext();
1257 // Only set the size/position of the constraint (if any)
1258 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1260 wxLayoutConstraints
*constr
= GetConstraints();
1265 constr
->left
.SetValue(x
);
1266 constr
->left
.SetDone(TRUE
);
1270 constr
->top
.SetValue(y
);
1271 constr
->top
.SetDone(TRUE
);
1275 constr
->width
.SetValue(w
);
1276 constr
->width
.SetDone(TRUE
);
1280 constr
->height
.SetValue(h
);
1281 constr
->height
.SetDone(TRUE
);
1286 void wxWindowBase::MoveConstraint(int x
, int y
)
1288 wxLayoutConstraints
*constr
= GetConstraints();
1293 constr
->left
.SetValue(x
);
1294 constr
->left
.SetDone(TRUE
);
1298 constr
->top
.SetValue(y
);
1299 constr
->top
.SetDone(TRUE
);
1304 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1306 wxLayoutConstraints
*constr
= GetConstraints();
1309 *w
= constr
->width
.GetValue();
1310 *h
= constr
->height
.GetValue();
1316 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1318 wxLayoutConstraints
*constr
= GetConstraints();
1321 *w
= constr
->width
.GetValue();
1322 *h
= constr
->height
.GetValue();
1325 GetClientSize(w
, h
);
1328 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1330 wxLayoutConstraints
*constr
= GetConstraints();
1333 *x
= constr
->left
.GetValue();
1334 *y
= constr
->top
.GetValue();
1340 #endif // wxUSE_CONSTRAINTS
1342 // ----------------------------------------------------------------------------
1343 // do Update UI processing for child controls
1344 // ----------------------------------------------------------------------------
1346 // TODO: should this be implemented for the child window rather
1347 // than the parent? Then you can override it e.g. for wxCheckBox
1348 // to do the Right Thing rather than having to assume a fixed number
1349 // of control classes.
1350 void wxWindowBase::UpdateWindowUI()
1352 wxUpdateUIEvent
event(GetId());
1353 event
.m_eventObject
= this;
1355 if ( GetEventHandler()->ProcessEvent(event
) )
1357 if ( event
.GetSetEnabled() )
1358 Enable(event
.GetEnabled());
1360 if ( event
.GetSetText() )
1362 wxControl
*control
= wxDynamicThisCast(this, wxControl
);
1365 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1367 text
->SetValue(event
.GetText());
1369 control
->SetLabel(event
.GetText());
1374 wxCheckBox
*checkbox
= wxDynamicThisCast(this, wxCheckBox
);
1377 if ( event
.GetSetChecked() )
1378 checkbox
->SetValue(event
.GetChecked());
1380 #endif // wxUSE_CHECKBOX
1383 wxRadioButton
*radiobtn
= wxDynamicThisCast(this, wxRadioButton
);
1386 if ( event
.GetSetChecked() )
1387 radiobtn
->SetValue(event
.GetChecked());
1389 #endif // wxUSE_RADIOBTN
1393 // ----------------------------------------------------------------------------
1394 // dialog units translations
1395 // ----------------------------------------------------------------------------
1397 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1399 int charWidth
= GetCharWidth();
1400 int charHeight
= GetCharHeight();
1401 wxPoint
pt2(-1, -1);
1403 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1405 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1410 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1412 int charWidth
= GetCharWidth();
1413 int charHeight
= GetCharHeight();
1414 wxPoint
pt2(-1, -1);
1416 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1418 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1423 // ----------------------------------------------------------------------------
1425 // ----------------------------------------------------------------------------
1427 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1429 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1430 wxT("can't have both object and void client data") );
1432 if ( m_clientObject
)
1433 delete m_clientObject
;
1435 m_clientObject
= data
;
1436 m_clientDataType
= ClientData_Object
;
1439 wxClientData
*wxWindowBase::DoGetClientObject() const
1441 // it's not an error to call GetClientObject() on a window which doesn't
1442 // have client data at all - NULL will be returned
1443 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1444 wxT("this window doesn't have object client data") );
1446 return m_clientObject
;
1449 void wxWindowBase::DoSetClientData( void *data
)
1451 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1452 wxT("can't have both object and void client data") );
1454 m_clientData
= data
;
1455 m_clientDataType
= ClientData_Void
;
1458 void *wxWindowBase::DoGetClientData() const
1460 // it's not an error to call GetClientData() on a window which doesn't have
1461 // client data at all - NULL will be returned
1462 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1463 wxT("this window doesn't have void client data") );
1465 return m_clientData
;
1468 // ----------------------------------------------------------------------------
1470 // ----------------------------------------------------------------------------
1472 // propagate the colour change event to the subwindows
1473 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1475 wxWindowList::Node
*node
= GetChildren().GetFirst();
1478 // Only propagate to non-top-level windows
1479 wxWindow
*win
= node
->GetData();
1480 if ( !win
->IsTopLevel() )
1482 wxSysColourChangedEvent event2
;
1483 event
.m_eventObject
= win
;
1484 win
->GetEventHandler()->ProcessEvent(event2
);
1487 node
= node
->GetNext();
1491 // the default action is to populate dialog with data when it's created
1492 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1494 TransferDataToWindow();
1497 // process Ctrl-Alt-mclick
1498 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1500 if ( event
.ControlDown() && event
.AltDown() )
1502 // don't translate these strings
1504 switch ( wxGetOsVersion() )
1506 case wxMOTIF_X
: port
= _T("Motif"); break;
1507 case wxMACINTOSH
: port
= _T("Mac"); break;
1508 case wxBEOS
: port
= _T("BeOS"); break;
1512 case wxGTK_BEOS
: port
= _T("GTK"); break;
1518 case wxWIN386
: port
= _T("MS Windows"); break;
1522 case wxMGL_OS2
: port
= _T("MGL"); break;
1524 case wxOS2_PM
: port
= _T("OS/2"); break;
1525 default: port
= _T("unknown"); break;
1528 wxMessageBox(wxString::Format(
1530 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1539 _T("wxWindows information"),
1540 wxICON_INFORMATION
| wxOK
,
1549 // ----------------------------------------------------------------------------
1550 // list classes implementation
1551 // ----------------------------------------------------------------------------
1553 void wxWindowListNode::DeleteData()
1555 delete (wxWindow
*)GetData();