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/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
49 #include "wx/layout.h"
51 #endif // wxUSE_CONSTRAINTS
53 #if wxUSE_DRAG_AND_DROP
55 #endif // wxUSE_DRAG_AND_DROP
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 int wxWindowBase::ms_lastControlId
= -200;
75 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
82 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
83 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
84 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
87 EVT_HELP(-1, wxWindowBase::OnHelp
)
92 // ============================================================================
93 // implementation of the common functionality of the wxWindow class
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // the default initialization
101 void wxWindowBase::InitBase()
103 // no window yet, no parent nor children
104 m_parent
= (wxWindow
*)NULL
;
106 m_children
.DeleteContents( FALSE
); // don't auto delete node data
108 // no constraints on the minimal window size
114 // window is created enabled but it's not visible yet
118 // the default event handler is just this window
119 m_eventHandler
= this;
123 m_windowValidator
= (wxValidator
*) NULL
;
124 #endif // wxUSE_VALIDATORS
126 // use the system default colours
127 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
);
128 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
130 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
131 // so the font is *not* really set - but calls to SetFont() later won't do
132 // anything because m_font appears to be already set!
134 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
141 // an optimization for the event processing: checking this flag is much
142 // faster than using IsKindOf(CLASSINFO(wxWindow))
145 #if wxUSE_CONSTRAINTS
146 // no constraints whatsoever
147 m_constraints
= (wxLayoutConstraints
*) NULL
;
148 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
149 m_windowSizer
= (wxSizer
*) NULL
;
150 m_autoLayout
= FALSE
;
151 #endif // wxUSE_CONSTRAINTS
153 #if wxUSE_DRAG_AND_DROP
154 m_dropTarget
= (wxDropTarget
*)NULL
;
155 #endif // wxUSE_DRAG_AND_DROP
158 m_tooltip
= (wxToolTip
*)NULL
;
159 #endif // wxUSE_TOOLTIPS
162 m_caret
= (wxCaret
*)NULL
;
163 #endif // wxUSE_CARET
165 // Whether we're using the current theme for this window (wxGTK only for now)
166 m_themeEnabled
= FALSE
;
169 // common part of window creation process
170 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
172 const wxPoint
& WXUNUSED(pos
),
173 const wxSize
& WXUNUSED(size
),
175 const wxValidator
& validator
,
176 const wxString
& name
)
178 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
179 // member variables - check that it has been called (will catch the case
180 // when a new ctor is added which doesn't call InitWindow)
181 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
183 // generate a new id if the user doesn't care about it
184 m_windowId
= id
== -1 ? NewControlId() : id
;
187 SetWindowStyleFlag(style
);
191 SetValidator(validator
);
192 #endif // wxUSE_VALIDATORS
194 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
195 // have it too - like this it's possible to set it only in the top level
196 // dialog/frame and all children will inherit it by defult
197 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
199 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
210 wxWindowBase::~wxWindowBase()
212 // FIXME if these 2 cases result from programming errors in the user code
213 // we should probably assert here instead of silently fixing them
215 // Just in case the window has been Closed, but we're then deleting
216 // immediately: don't leave dangling pointers.
217 wxPendingDelete
.DeleteObject(this);
219 // Just in case we've loaded a top-level window via LoadNativeDialog but
220 // we weren't a dialog class
221 wxTopLevelWindows
.DeleteObject(this);
223 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
228 #endif // wxUSE_CARET
231 if ( m_windowValidator
)
232 delete m_windowValidator
;
233 #endif // wxUSE_VALIDATORS
235 #if wxUSE_CONSTRAINTS
236 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
237 // at deleted windows as they delete themselves.
238 DeleteRelatedConstraints();
242 // This removes any dangling pointers to this window in other windows'
243 // constraintsInvolvedIn lists.
244 UnsetConstraints(m_constraints
);
245 delete m_constraints
;
246 m_constraints
= NULL
;
250 delete m_windowSizer
;
252 #endif // wxUSE_CONSTRAINTS
254 #if wxUSE_DRAG_AND_DROP
257 #endif // wxUSE_DRAG_AND_DROP
262 #endif // wxUSE_TOOLTIPS
264 // reset the dangling pointer our parent window may keep to us
265 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
267 m_parent
->SetDefaultItem(NULL
);
271 bool wxWindowBase::Destroy()
278 bool wxWindowBase::Close(bool force
)
280 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
281 event
.SetEventObject(this);
282 #if WXWIN_COMPATIBILITY
283 event
.SetForce(force
);
284 #endif // WXWIN_COMPATIBILITY
285 event
.SetCanVeto(!force
);
287 // return FALSE if window wasn't closed because the application vetoed the
289 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
292 bool wxWindowBase::DestroyChildren()
294 wxWindowList::Node
*node
;
297 // we iterate until the list becomes empty
298 node
= GetChildren().GetFirst();
302 wxWindow
*child
= node
->GetData();
304 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
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()
450 || wxDynamicCast(win
, wxStatusBar
)
451 #endif // wxUSE_STATUSBAR
454 // dialogs and frames lie in different top level windows -
455 // don't deal with them here; as for the status bars, they
456 // don't lie in the client area at all
461 win
->GetPosition(&wx
, &wy
);
463 // if the window hadn't been positioned yet, assume that it is in
470 win
->GetSize(&ww
, &wh
);
471 if ( wx
+ ww
> maxX
)
473 if ( wy
+ wh
> maxY
)
477 return wxSize(maxX
, maxY
);
481 // for a generic window there is no natural best size - just use the
487 // by default the origin is not shifted
488 wxPoint
wxWindowBase::GetClientAreaOrigin() const
490 return wxPoint(0, 0);
493 // set the min/max size of the window
494 void wxWindowBase::SetSizeHints(int minW
, int minH
,
496 int WXUNUSED(incW
), int WXUNUSED(incH
))
504 // ----------------------------------------------------------------------------
505 // show/hide/enable/disable the window
506 // ----------------------------------------------------------------------------
508 bool wxWindowBase::Show(bool show
)
510 if ( show
!= m_isShown
)
522 bool wxWindowBase::Enable(bool enable
)
524 if ( enable
!= m_isEnabled
)
526 m_isEnabled
= enable
;
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 bool wxWindowBase::IsTopLevel() const
544 // ----------------------------------------------------------------------------
545 // reparenting the window
546 // ----------------------------------------------------------------------------
548 void wxWindowBase::AddChild(wxWindowBase
*child
)
550 wxCHECK_RET( child
, wxT("can't add a NULL child") );
552 // this should never happen and it will lead to a crash later if it does
553 // because RemoveChild() will remove only one node from the children list
554 // and the other(s) one(s) will be left with dangling pointers in them
555 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
557 GetChildren().Append(child
);
558 child
->SetParent(this);
561 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
563 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
565 GetChildren().DeleteObject(child
);
566 child
->SetParent((wxWindow
*)NULL
);
569 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
571 wxWindow
*oldParent
= GetParent();
572 if ( newParent
== oldParent
)
578 // unlink this window from the existing parent.
581 oldParent
->RemoveChild(this);
585 wxTopLevelWindows
.DeleteObject(this);
588 // add it to the new one
591 newParent
->AddChild(this);
595 wxTopLevelWindows
.Append(this);
601 // ----------------------------------------------------------------------------
602 // event handler stuff
603 // ----------------------------------------------------------------------------
605 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
607 handler
->SetNextHandler(GetEventHandler());
608 SetEventHandler(handler
);
611 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
613 wxEvtHandler
*handlerA
= GetEventHandler();
616 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
617 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
618 SetEventHandler(handlerB
);
622 handlerA
= (wxEvtHandler
*)NULL
;
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
635 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
638 m_backgroundColour
= colour
;
645 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
647 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
650 m_foregroundColour
= colour
;
657 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
659 // setting an invalid cursor is ok, it means that we don't have any special
661 if ( m_cursor
== cursor
)
672 bool wxWindowBase::SetFont(const wxFont
& font
)
674 // don't try to set invalid font, always fall back to the default
675 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
677 if ( fontOk
== m_font
)
691 void wxWindowBase::SetCaret(wxCaret
*caret
)
702 wxASSERT_MSG( m_caret
->GetWindow() == this,
703 wxT("caret should be created associated to this window") );
706 #endif // wxUSE_CARET
709 // ----------------------------------------------------------------------------
711 // ----------------------------------------------------------------------------
713 void wxWindowBase::SetValidator(const wxValidator
& validator
)
715 if ( m_windowValidator
)
716 delete m_windowValidator
;
718 m_windowValidator
= (wxValidator
*)validator
.Clone();
720 if ( m_windowValidator
)
721 m_windowValidator
->SetWindow(this) ;
723 #endif // wxUSE_VALIDATORS
725 // ----------------------------------------------------------------------------
726 // update region stuff
727 // ----------------------------------------------------------------------------
729 wxRect
wxWindowBase::GetUpdateClientRect() const
731 wxRegion rgnUpdate
= GetUpdateRegion();
732 rgnUpdate
.Intersect(GetClientRect());
733 wxRect rectUpdate
= rgnUpdate
.GetBox();
734 wxPoint ptOrigin
= GetClientAreaOrigin();
735 rectUpdate
.x
-= ptOrigin
.x
;
736 rectUpdate
.y
-= ptOrigin
.y
;
741 bool wxWindowBase::IsExposed(int x
, int y
) const
743 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
746 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
748 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
751 // ----------------------------------------------------------------------------
752 // find window by id or name
753 // ----------------------------------------------------------------------------
755 wxWindow
*wxWindowBase::FindWindow( long id
)
757 if ( id
== m_windowId
)
758 return (wxWindow
*)this;
760 wxWindowBase
*res
= (wxWindow
*)NULL
;
761 wxWindowList::Node
*node
;
762 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
764 wxWindowBase
*child
= node
->GetData();
765 res
= child
->FindWindow( id
);
768 return (wxWindow
*)res
;
771 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
773 if ( name
== m_windowName
)
774 return (wxWindow
*)this;
776 wxWindowBase
*res
= (wxWindow
*)NULL
;
777 wxWindowList::Node
*node
;
778 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
780 wxWindow
*child
= node
->GetData();
781 res
= child
->FindWindow(name
);
784 return (wxWindow
*)res
;
787 // ----------------------------------------------------------------------------
788 // dialog oriented functions
789 // ----------------------------------------------------------------------------
791 void wxWindowBase::MakeModal(bool modal
)
793 // Disable all other windows
796 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
799 wxWindow
*win
= node
->GetData();
803 node
= node
->GetNext();
808 bool wxWindowBase::Validate()
811 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
813 wxWindowList::Node
*node
;
814 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
816 wxWindowBase
*child
= node
->GetData();
817 wxValidator
*validator
= child
->GetValidator();
818 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
823 if ( recurse
&& !child
->Validate() )
828 #endif // wxUSE_VALIDATORS
833 bool wxWindowBase::TransferDataToWindow()
836 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
838 wxWindowList::Node
*node
;
839 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
841 wxWindowBase
*child
= node
->GetData();
842 wxValidator
*validator
= child
->GetValidator();
843 if ( validator
&& !validator
->TransferToWindow() )
845 wxLogWarning(_("Could not transfer data to window"));
846 wxLog::FlushActive();
853 if ( !child
->TransferDataToWindow() )
855 // warning already given
860 #endif // wxUSE_VALIDATORS
865 bool wxWindowBase::TransferDataFromWindow()
868 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
870 wxWindowList::Node
*node
;
871 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
873 wxWindow
*child
= node
->GetData();
874 wxValidator
*validator
= child
->GetValidator();
875 if ( validator
&& !validator
->TransferFromWindow() )
877 // nop warning here because the application is supposed to give
878 // one itself - we don't know here what might have gone wrongly
885 if ( !child
->TransferDataFromWindow() )
887 // warning already given
892 #endif // wxUSE_VALIDATORS
897 void wxWindowBase::InitDialog()
899 wxInitDialogEvent
event(GetId());
900 event
.SetEventObject( this );
901 GetEventHandler()->ProcessEvent(event
);
904 // ----------------------------------------------------------------------------
905 // context-sensitive help support
906 // ----------------------------------------------------------------------------
910 // associate this help text with this window
911 void wxWindowBase::SetHelpText(const wxString
& text
)
913 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
916 helpProvider
->AddHelp(this, text
);
920 // associate this help text with all windows with the same id as this
922 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
924 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
927 helpProvider
->AddHelp(GetId(), text
);
931 // get the help string associated with this window (may be empty)
932 wxString
wxWindowBase::GetHelpText() const
935 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
938 text
= helpProvider
->GetHelp(this);
944 // show help for this window
945 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
947 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
950 if ( helpProvider
->ShowHelp(this) )
952 // skip the event.Skip() below
962 // ----------------------------------------------------------------------------
964 // ----------------------------------------------------------------------------
968 void wxWindowBase::SetToolTip( const wxString
&tip
)
970 // don't create the new tooltip if we already have one
973 m_tooltip
->SetTip( tip
);
977 SetToolTip( new wxToolTip( tip
) );
980 // setting empty tooltip text does not remove the tooltip any more - use
981 // SetToolTip((wxToolTip *)NULL) for this
984 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
992 #endif // wxUSE_TOOLTIPS
994 // ----------------------------------------------------------------------------
995 // constraints and sizers
996 // ----------------------------------------------------------------------------
998 #if wxUSE_CONSTRAINTS
1000 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1002 if ( m_constraints
)
1004 UnsetConstraints(m_constraints
);
1005 delete m_constraints
;
1007 m_constraints
= constraints
;
1008 if ( m_constraints
)
1010 // Make sure other windows know they're part of a 'meaningful relationship'
1011 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1012 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1013 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1014 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1015 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1016 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1017 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1018 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1019 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1020 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1021 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1022 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1023 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1024 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1025 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1026 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1030 // This removes any dangling pointers to this window in other windows'
1031 // constraintsInvolvedIn lists.
1032 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1036 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1037 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1038 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1039 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1040 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1041 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1042 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1043 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1044 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1045 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1046 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1047 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1048 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1049 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1050 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1051 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1055 // Back-pointer to other windows we're involved with, so if we delete this
1056 // window, we must delete any constraints we're involved with.
1057 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1059 if ( !m_constraintsInvolvedIn
)
1060 m_constraintsInvolvedIn
= new wxWindowList
;
1061 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1062 m_constraintsInvolvedIn
->Append(otherWin
);
1065 // REMOVE back-pointer to other windows we're involved with.
1066 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1068 if ( m_constraintsInvolvedIn
)
1069 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1072 // Reset any constraints that mention this window
1073 void wxWindowBase::DeleteRelatedConstraints()
1075 if ( m_constraintsInvolvedIn
)
1077 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1080 wxWindow
*win
= node
->GetData();
1081 wxLayoutConstraints
*constr
= win
->GetConstraints();
1083 // Reset any constraints involving this window
1086 constr
->left
.ResetIfWin(this);
1087 constr
->top
.ResetIfWin(this);
1088 constr
->right
.ResetIfWin(this);
1089 constr
->bottom
.ResetIfWin(this);
1090 constr
->width
.ResetIfWin(this);
1091 constr
->height
.ResetIfWin(this);
1092 constr
->centreX
.ResetIfWin(this);
1093 constr
->centreY
.ResetIfWin(this);
1096 wxWindowList::Node
*next
= node
->GetNext();
1101 delete m_constraintsInvolvedIn
;
1102 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1106 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1108 if (m_windowSizer
) delete m_windowSizer
;
1110 m_windowSizer
= sizer
;
1113 bool wxWindowBase::Layout()
1115 // If there is a sizer, use it instead of the constraints
1119 GetClientSize(&w
, &h
);
1121 GetSizer()->SetDimension( 0, 0, w
, h
);
1125 wxLayoutConstraints
*constr
= GetConstraints();
1126 bool wasOk
= constr
&& constr
->AreSatisfied();
1128 ResetConstraints(); // Mark all constraints as unevaluated
1130 // if we're a top level panel (i.e. our parent is frame/dialog), our
1131 // own constraints will never be satisfied any more unless we do it
1136 while ( noChanges
> 0 )
1138 constr
->SatisfyConstraints(this, &noChanges
);
1142 DoPhase(1); // Layout children
1143 DoPhase(2); // Layout grand children
1144 SetConstraintSizes(); // Recursively set the real window sizes
1151 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1152 // do a similar thing, but also impose their own 'constraints' and order the
1153 // evaluation differently.
1154 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1156 wxLayoutConstraints
*constr
= GetConstraints();
1159 return constr
->SatisfyConstraints(this, noChanges
);
1165 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1175 // Do a phase of evaluating child constraints
1176 bool wxWindowBase::DoPhase(int phase
)
1178 int noIterations
= 0;
1179 int maxIterations
= 500;
1182 wxWindowList succeeded
;
1183 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1187 wxWindowList::Node
*node
= GetChildren().GetFirst();
1190 wxWindow
*child
= node
->GetData();
1191 if ( !child
->IsTopLevel() )
1193 wxLayoutConstraints
*constr
= child
->GetConstraints();
1196 if ( !succeeded
.Find(child
) )
1198 int tempNoChanges
= 0;
1199 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1200 noChanges
+= tempNoChanges
;
1203 succeeded
.Append(child
);
1208 node
= node
->GetNext();
1217 void wxWindowBase::ResetConstraints()
1219 wxLayoutConstraints
*constr
= GetConstraints();
1222 constr
->left
.SetDone(FALSE
);
1223 constr
->top
.SetDone(FALSE
);
1224 constr
->right
.SetDone(FALSE
);
1225 constr
->bottom
.SetDone(FALSE
);
1226 constr
->width
.SetDone(FALSE
);
1227 constr
->height
.SetDone(FALSE
);
1228 constr
->centreX
.SetDone(FALSE
);
1229 constr
->centreY
.SetDone(FALSE
);
1232 wxWindowList::Node
*node
= GetChildren().GetFirst();
1235 wxWindow
*win
= node
->GetData();
1236 if ( !win
->IsTopLevel() )
1237 win
->ResetConstraints();
1238 node
= node
->GetNext();
1242 // Need to distinguish between setting the 'fake' size for windows and sizers,
1243 // and setting the real values.
1244 void wxWindowBase::SetConstraintSizes(bool recurse
)
1246 wxLayoutConstraints
*constr
= GetConstraints();
1247 if ( constr
&& constr
->AreSatisfied() )
1249 int x
= constr
->left
.GetValue();
1250 int y
= constr
->top
.GetValue();
1251 int w
= constr
->width
.GetValue();
1252 int h
= constr
->height
.GetValue();
1254 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1255 (constr
->height
.GetRelationship() != wxAsIs
) )
1257 SetSize(x
, y
, w
, h
);
1261 // If we don't want to resize this window, just move it...
1267 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1268 GetClassInfo()->GetClassName(),
1274 wxWindowList::Node
*node
= GetChildren().GetFirst();
1277 wxWindow
*win
= node
->GetData();
1278 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1279 win
->SetConstraintSizes();
1280 node
= node
->GetNext();
1285 // Only set the size/position of the constraint (if any)
1286 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1288 wxLayoutConstraints
*constr
= GetConstraints();
1293 constr
->left
.SetValue(x
);
1294 constr
->left
.SetDone(TRUE
);
1298 constr
->top
.SetValue(y
);
1299 constr
->top
.SetDone(TRUE
);
1303 constr
->width
.SetValue(w
);
1304 constr
->width
.SetDone(TRUE
);
1308 constr
->height
.SetValue(h
);
1309 constr
->height
.SetDone(TRUE
);
1314 void wxWindowBase::MoveConstraint(int x
, int y
)
1316 wxLayoutConstraints
*constr
= GetConstraints();
1321 constr
->left
.SetValue(x
);
1322 constr
->left
.SetDone(TRUE
);
1326 constr
->top
.SetValue(y
);
1327 constr
->top
.SetDone(TRUE
);
1332 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1334 wxLayoutConstraints
*constr
= GetConstraints();
1337 *w
= constr
->width
.GetValue();
1338 *h
= constr
->height
.GetValue();
1344 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1346 wxLayoutConstraints
*constr
= GetConstraints();
1349 *w
= constr
->width
.GetValue();
1350 *h
= constr
->height
.GetValue();
1353 GetClientSize(w
, h
);
1356 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1358 // don't do it for the dialogs/frames - they float independently of their
1360 if ( !IsTopLevel() )
1362 wxWindow
*parent
= GetParent();
1363 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1365 wxPoint
pt(parent
->GetClientAreaOrigin());
1373 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1375 wxLayoutConstraints
*constr
= GetConstraints();
1378 *x
= constr
->left
.GetValue();
1379 *y
= constr
->top
.GetValue();
1385 #endif // wxUSE_CONSTRAINTS
1387 // ----------------------------------------------------------------------------
1388 // do Update UI processing for child controls
1389 // ----------------------------------------------------------------------------
1391 // TODO: should this be implemented for the child window rather
1392 // than the parent? Then you can override it e.g. for wxCheckBox
1393 // to do the Right Thing rather than having to assume a fixed number
1394 // of control classes.
1395 void wxWindowBase::UpdateWindowUI()
1398 wxUpdateUIEvent
event(GetId());
1399 event
.m_eventObject
= this;
1401 if ( GetEventHandler()->ProcessEvent(event
) )
1403 if ( event
.GetSetEnabled() )
1404 Enable(event
.GetEnabled());
1406 if ( event
.GetSetText() )
1408 wxControl
*control
= wxDynamicCastThis(wxControl
);
1412 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1414 text
->SetValue(event
.GetText());
1416 #endif // wxUSE_TEXTCTRL
1417 control
->SetLabel(event
.GetText());
1422 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1425 if ( event
.GetSetChecked() )
1426 checkbox
->SetValue(event
.GetChecked());
1428 #endif // wxUSE_CHECKBOX
1431 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1434 if ( event
.GetSetChecked() )
1435 radiobtn
->SetValue(event
.GetChecked());
1437 #endif // wxUSE_RADIOBTN
1439 #endif // wxUSE_CONTROLS
1442 // ----------------------------------------------------------------------------
1443 // dialog units translations
1444 // ----------------------------------------------------------------------------
1446 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1448 int charWidth
= GetCharWidth();
1449 int charHeight
= GetCharHeight();
1450 wxPoint
pt2(-1, -1);
1452 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1454 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1459 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1461 int charWidth
= GetCharWidth();
1462 int charHeight
= GetCharHeight();
1463 wxPoint
pt2(-1, -1);
1465 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1467 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1472 // ----------------------------------------------------------------------------
1474 // ----------------------------------------------------------------------------
1476 // propagate the colour change event to the subwindows
1477 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1479 wxWindowList::Node
*node
= GetChildren().GetFirst();
1482 // Only propagate to non-top-level windows
1483 wxWindow
*win
= node
->GetData();
1484 if ( !win
->IsTopLevel() )
1486 wxSysColourChangedEvent event2
;
1487 event
.m_eventObject
= win
;
1488 win
->GetEventHandler()->ProcessEvent(event2
);
1491 node
= node
->GetNext();
1495 // the default action is to populate dialog with data when it's created
1496 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1498 TransferDataToWindow();
1501 // process Ctrl-Alt-mclick
1502 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1505 if ( event
.ControlDown() && event
.AltDown() )
1507 // don't translate these strings
1509 switch ( wxGetOsVersion() )
1511 case wxMOTIF_X
: port
= _T("Motif"); break;
1513 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1514 case wxBEOS
: port
= _T("BeOS"); break;
1518 case wxGTK_BEOS
: port
= _T("GTK"); break;
1524 case wxWIN386
: port
= _T("MS Windows"); break;
1528 case wxMGL_OS2
: port
= _T("MGL"); break;
1530 case wxOS2_PM
: port
= _T("OS/2"); break;
1531 default: port
= _T("unknown"); break;
1534 wxMessageBox(wxString::Format(
1536 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2001 wxWindows team"
1545 _T("wxWindows information"),
1546 wxICON_INFORMATION
| wxOK
,
1550 #endif // wxUSE_MSGDLG
1556 // ----------------------------------------------------------------------------
1557 // list classes implementation
1558 // ----------------------------------------------------------------------------
1560 void wxWindowListNode::DeleteData()
1562 delete (wxWindow
*)GetData();
1565 // ----------------------------------------------------------------------------
1567 // ----------------------------------------------------------------------------
1569 wxBorder
wxWindowBase::GetBorder() const
1571 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1572 if ( border
== wxBORDER_DEFAULT
)
1574 border
= GetDefaultBorder();
1580 wxBorder
wxWindowBase::GetDefaultBorder() const
1582 return wxBORDER_NONE
;
1585 // ----------------------------------------------------------------------------
1587 // ----------------------------------------------------------------------------
1589 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1591 // here we just check if the point is inside the window or not
1593 // check the top and left border first
1594 bool outside
= x
< 0 || y
< 0;
1597 // check the right and bottom borders too
1598 wxSize size
= GetSize();
1599 outside
= x
>= size
.x
|| y
>= size
.y
;
1602 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;