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 // no client data (yet)
120 m_clientDataType
= wxClientData_None
;
122 // the default event handler is just this window
123 m_eventHandler
= this;
127 m_windowValidator
= (wxValidator
*) NULL
;
128 #endif // wxUSE_VALIDATORS
130 // use the system default colours
131 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
);
132 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
134 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
135 // so the font is *not* really set - but calls to SetFont() later won't do
136 // anything because m_font appears to be already set!
138 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
145 // an optimization for the event processing: checking this flag is much
146 // faster than using IsKindOf(CLASSINFO(wxWindow))
149 #if wxUSE_CONSTRAINTS
150 // no constraints whatsoever
151 m_constraints
= (wxLayoutConstraints
*) NULL
;
152 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
153 m_windowSizer
= (wxSizer
*) NULL
;
154 m_autoLayout
= FALSE
;
155 #endif // wxUSE_CONSTRAINTS
157 #if wxUSE_DRAG_AND_DROP
158 m_dropTarget
= (wxDropTarget
*)NULL
;
159 #endif // wxUSE_DRAG_AND_DROP
162 m_tooltip
= (wxToolTip
*)NULL
;
163 #endif // wxUSE_TOOLTIPS
166 m_caret
= (wxCaret
*)NULL
;
167 #endif // wxUSE_CARET
169 // Whether we're using the current theme for this window (wxGTK only for now)
170 m_themeEnabled
= FALSE
;
173 // common part of window creation process
174 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
176 const wxPoint
& WXUNUSED(pos
),
177 const wxSize
& WXUNUSED(size
),
179 const wxValidator
& validator
,
180 const wxString
& name
)
182 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
183 // member variables - check that it has been called (will catch the case
184 // when a new ctor is added which doesn't call InitWindow)
185 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
187 // generate a new id if the user doesn't care about it
188 m_windowId
= id
== -1 ? NewControlId() : id
;
191 SetWindowStyleFlag(style
);
195 SetValidator(validator
);
196 #endif // wxUSE_VALIDATORS
198 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
199 // have it too - like this it's possible to set it only in the top level
200 // dialog/frame and all children will inherit it by defult
201 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
203 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 wxWindowBase::~wxWindowBase()
216 // FIXME if these 2 cases result from programming errors in the user code
217 // we should probably assert here instead of silently fixing them
219 // Just in case the window has been Closed, but we're then deleting
220 // immediately: don't leave dangling pointers.
221 wxPendingDelete
.DeleteObject(this);
223 // Just in case we've loaded a top-level window via LoadNativeDialog but
224 // we weren't a dialog class
225 wxTopLevelWindows
.DeleteObject(this);
227 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
232 #endif // wxUSE_CARET
235 if ( m_windowValidator
)
236 delete m_windowValidator
;
237 #endif // wxUSE_VALIDATORS
239 // we only delete object data, not untyped
240 if ( m_clientDataType
== wxClientData_Object
)
241 delete m_clientObject
;
243 #if wxUSE_CONSTRAINTS
244 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
245 // at deleted windows as they delete themselves.
246 DeleteRelatedConstraints();
250 // This removes any dangling pointers to this window in other windows'
251 // constraintsInvolvedIn lists.
252 UnsetConstraints(m_constraints
);
253 delete m_constraints
;
254 m_constraints
= NULL
;
258 delete m_windowSizer
;
260 #endif // wxUSE_CONSTRAINTS
262 #if wxUSE_DRAG_AND_DROP
265 #endif // wxUSE_DRAG_AND_DROP
270 #endif // wxUSE_TOOLTIPS
273 bool wxWindowBase::Destroy()
280 bool wxWindowBase::Close(bool force
)
282 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
283 event
.SetEventObject(this);
284 #if WXWIN_COMPATIBILITY
285 event
.SetForce(force
);
286 #endif // WXWIN_COMPATIBILITY
287 event
.SetCanVeto(!force
);
289 // return FALSE if window wasn't closed because the application vetoed the
291 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
294 bool wxWindowBase::DestroyChildren()
296 wxWindowList::Node
*node
;
299 // we iterate until the list becomes empty
300 node
= GetChildren().GetFirst();
304 wxWindow
*child
= node
->GetData();
306 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
310 wxASSERT_MSG( !GetChildren().Find(child
),
311 wxT("child didn't remove itself using RemoveChild()") );
317 // ----------------------------------------------------------------------------
318 // size/position related methods
319 // ----------------------------------------------------------------------------
321 // centre the window with respect to its parent in either (or both) directions
322 void wxWindowBase::Centre(int direction
)
324 // the position/size of the parent window or of the entire screen
326 int widthParent
, heightParent
;
328 wxWindow
*parent
= NULL
;
330 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
332 // find the parent to centre this window on: it should be the
333 // immediate parent for the controls but the top level parent for the
334 // top level windows (like dialogs)
335 parent
= GetParent();
338 while ( parent
&& !parent
->IsTopLevel() )
340 parent
= parent
->GetParent();
344 // did we find the parent?
348 direction
|= wxCENTRE_ON_SCREEN
;
352 if ( direction
& wxCENTRE_ON_SCREEN
)
354 // centre with respect to the whole screen
355 wxDisplaySize(&widthParent
, &heightParent
);
361 // centre on the parent
362 parent
->GetSize(&widthParent
, &heightParent
);
364 // adjust to the parents position
365 posParent
= parent
->GetPosition();
369 // centre inside the parents client rectangle
370 parent
->GetClientSize(&widthParent
, &heightParent
);
375 GetSize(&width
, &height
);
380 if ( direction
& wxHORIZONTAL
)
381 xNew
= (widthParent
- width
)/2;
383 if ( direction
& wxVERTICAL
)
384 yNew
= (heightParent
- height
)/2;
389 // Base size of the visible dimensions of the display
390 // to take into account the taskbar
391 wxRect rect
= wxGetClientDisplayRect();
392 wxSize
size (rect
.width
,rect
.height
);
394 if (posParent
.x
>= 0) // if parent is on the main display
398 else if (xNew
+width
> size
.x
)
399 xNew
= size
.x
-width
-1;
401 if (posParent
.y
>= 0) // if parent is on the main display
403 if (yNew
+height
> size
.y
)
404 yNew
= size
.y
-height
-1;
406 // Make certain that the title bar is initially visible
407 // always, even if this would push the bottom of the
408 // dialog of the visible area of the display
413 // move the window to this position (keeping the old size but using
414 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
415 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
418 // fits the window around the children
419 void wxWindowBase::Fit()
421 if ( GetChildren().GetCount() > 0 )
423 wxSize size
= DoGetBestSize();
425 // for compatibility with the old versions and because it really looks
426 // slightly more pretty like this, add a pad
432 //else: do nothing if we have no children
435 // return the size best suited for the current window
436 wxSize
wxWindowBase::DoGetBestSize() const
438 if ( GetChildren().GetCount() > 0 )
440 // our minimal acceptable size is such that all our windows fit inside
444 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
446 node
= node
->GetNext() )
448 wxWindow
*win
= node
->GetData();
449 if ( win
->IsTopLevel()
451 || wxDynamicCast(win
, wxStatusBar
)
452 #endif // wxUSE_STATUSBAR
455 // dialogs and frames lie in different top level windows -
456 // don't deal with them here; as for the status bars, they
457 // don't lie in the client area at all
462 win
->GetPosition(&wx
, &wy
);
464 // if the window hadn't been positioned yet, assume that it is in
471 win
->GetSize(&ww
, &wh
);
472 if ( wx
+ ww
> maxX
)
474 if ( wy
+ wh
> maxY
)
478 return wxSize(maxX
, maxY
);
482 // for a generic window there is no natural best size - just use the
488 // by default the origin is not shifted
489 wxPoint
wxWindowBase::GetClientAreaOrigin() const
491 return wxPoint(0, 0);
494 // set the min/max size of the window
495 void wxWindowBase::SetSizeHints(int minW
, int minH
,
497 int WXUNUSED(incW
), int WXUNUSED(incH
))
505 // ----------------------------------------------------------------------------
506 // show/hide/enable/disable the window
507 // ----------------------------------------------------------------------------
509 bool wxWindowBase::Show(bool show
)
511 if ( show
!= m_isShown
)
523 bool wxWindowBase::Enable(bool enable
)
525 if ( enable
!= m_isEnabled
)
527 m_isEnabled
= enable
;
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
540 bool wxWindowBase::IsTopLevel() const
545 // ----------------------------------------------------------------------------
546 // reparenting the window
547 // ----------------------------------------------------------------------------
549 void wxWindowBase::AddChild(wxWindowBase
*child
)
551 wxCHECK_RET( child
, wxT("can't add a NULL child") );
553 // this should never happen and it will lead to a crash later if it does
554 // because RemoveChild() will remove only one node from the children list
555 // and the other(s) one(s) will be left with dangling pointers in them
556 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
558 GetChildren().Append(child
);
559 child
->SetParent(this);
562 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
564 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
566 GetChildren().DeleteObject(child
);
567 child
->SetParent((wxWindow
*)NULL
);
570 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
572 wxWindow
*oldParent
= GetParent();
573 if ( newParent
== oldParent
)
579 // unlink this window from the existing parent.
582 oldParent
->RemoveChild(this);
586 wxTopLevelWindows
.DeleteObject(this);
589 // add it to the new one
592 newParent
->AddChild(this);
596 wxTopLevelWindows
.Append(this);
602 // ----------------------------------------------------------------------------
603 // event handler stuff
604 // ----------------------------------------------------------------------------
606 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
608 handler
->SetNextHandler(GetEventHandler());
609 SetEventHandler(handler
);
612 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
614 wxEvtHandler
*handlerA
= GetEventHandler();
617 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
618 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
619 SetEventHandler(handlerB
);
623 handlerA
= (wxEvtHandler
*)NULL
;
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
636 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
639 m_backgroundColour
= colour
;
644 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
646 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
649 m_foregroundColour
= colour
;
654 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
656 // setting an invalid cursor is ok, it means that we don't have any special
658 if ( m_cursor
== cursor
)
669 bool wxWindowBase::SetFont(const wxFont
& font
)
671 // don't try to set invalid font, always fall back to the default
672 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
674 if ( fontOk
== m_font
)
686 void wxWindowBase::SetCaret(wxCaret
*caret
)
697 wxASSERT_MSG( m_caret
->GetWindow() == this,
698 wxT("caret should be created associated to this window") );
701 #endif // wxUSE_CARET
704 // ----------------------------------------------------------------------------
706 // ----------------------------------------------------------------------------
708 void wxWindowBase::SetValidator(const wxValidator
& validator
)
710 if ( m_windowValidator
)
711 delete m_windowValidator
;
713 m_windowValidator
= (wxValidator
*)validator
.Clone();
715 if ( m_windowValidator
)
716 m_windowValidator
->SetWindow(this) ;
718 #endif // wxUSE_VALIDATORS
720 // ----------------------------------------------------------------------------
721 // update region stuff
722 // ----------------------------------------------------------------------------
724 wxRect
wxWindowBase::GetUpdateClientRect() const
726 wxRegion rgnUpdate
= GetUpdateRegion();
727 rgnUpdate
.Intersect(GetClientRect());
728 wxRect rectUpdate
= rgnUpdate
.GetBox();
729 wxPoint ptOrigin
= GetClientAreaOrigin();
730 rectUpdate
.x
-= ptOrigin
.x
;
731 rectUpdate
.y
-= ptOrigin
.y
;
736 bool wxWindowBase::IsExposed(int x
, int y
) const
738 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
741 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
743 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
746 // ----------------------------------------------------------------------------
747 // find window by id or name
748 // ----------------------------------------------------------------------------
750 wxWindow
*wxWindowBase::FindWindow( long id
)
752 if ( id
== m_windowId
)
753 return (wxWindow
*)this;
755 wxWindowBase
*res
= (wxWindow
*)NULL
;
756 wxWindowList::Node
*node
;
757 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
759 wxWindowBase
*child
= node
->GetData();
760 res
= child
->FindWindow( id
);
763 return (wxWindow
*)res
;
766 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
768 if ( name
== m_windowName
)
769 return (wxWindow
*)this;
771 wxWindowBase
*res
= (wxWindow
*)NULL
;
772 wxWindowList::Node
*node
;
773 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
775 wxWindow
*child
= node
->GetData();
776 res
= child
->FindWindow(name
);
779 return (wxWindow
*)res
;
782 // ----------------------------------------------------------------------------
783 // dialog oriented functions
784 // ----------------------------------------------------------------------------
786 void wxWindowBase::MakeModal(bool modal
)
788 // Disable all other windows
791 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
794 wxWindow
*win
= node
->GetData();
798 node
= node
->GetNext();
803 bool wxWindowBase::Validate()
806 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
808 wxWindowList::Node
*node
;
809 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
811 wxWindowBase
*child
= node
->GetData();
812 wxValidator
*validator
= child
->GetValidator();
813 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
818 if ( recurse
&& !child
->Validate() )
823 #endif // wxUSE_VALIDATORS
828 bool wxWindowBase::TransferDataToWindow()
831 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
833 wxWindowList::Node
*node
;
834 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
836 wxWindowBase
*child
= node
->GetData();
837 wxValidator
*validator
= child
->GetValidator();
838 if ( validator
&& !validator
->TransferToWindow() )
840 wxLogWarning(_("Could not transfer data to window"));
841 wxLog::FlushActive();
848 if ( !child
->TransferDataToWindow() )
850 // warning already given
855 #endif // wxUSE_VALIDATORS
860 bool wxWindowBase::TransferDataFromWindow()
863 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
865 wxWindowList::Node
*node
;
866 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
868 wxWindow
*child
= node
->GetData();
869 wxValidator
*validator
= child
->GetValidator();
870 if ( validator
&& !validator
->TransferFromWindow() )
872 // nop warning here because the application is supposed to give
873 // one itself - we don't know here what might have gone wrongly
880 if ( !child
->TransferDataFromWindow() )
882 // warning already given
887 #endif // wxUSE_VALIDATORS
892 void wxWindowBase::InitDialog()
894 wxInitDialogEvent
event(GetId());
895 event
.SetEventObject( this );
896 GetEventHandler()->ProcessEvent(event
);
899 // ----------------------------------------------------------------------------
900 // context-sensitive help support
901 // ----------------------------------------------------------------------------
905 // associate this help text with this window
906 void wxWindowBase::SetHelpText(const wxString
& text
)
908 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
911 helpProvider
->AddHelp(this, text
);
915 // associate this help text with all windows with the same id as this
917 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
919 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
922 helpProvider
->AddHelp(GetId(), text
);
926 // get the help string associated with this window (may be empty)
927 wxString
wxWindowBase::GetHelpText() const
930 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
933 text
= helpProvider
->GetHelp(this);
939 // show help for this window
940 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
942 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
945 if ( helpProvider
->ShowHelp(this) )
947 // skip the event.Skip() below
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
963 void wxWindowBase::SetToolTip( const wxString
&tip
)
965 // don't create the new tooltip if we already have one
968 m_tooltip
->SetTip( tip
);
972 SetToolTip( new wxToolTip( tip
) );
975 // setting empty tooltip text does not remove the tooltip any more - use
976 // SetToolTip((wxToolTip *)NULL) for this
979 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
987 #endif // wxUSE_TOOLTIPS
989 // ----------------------------------------------------------------------------
990 // constraints and sizers
991 // ----------------------------------------------------------------------------
993 #if wxUSE_CONSTRAINTS
995 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
999 UnsetConstraints(m_constraints
);
1000 delete m_constraints
;
1002 m_constraints
= constraints
;
1003 if ( m_constraints
)
1005 // Make sure other windows know they're part of a 'meaningful relationship'
1006 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1007 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1008 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1009 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1010 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1011 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1012 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1013 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1014 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1015 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1016 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1017 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1018 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1019 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1020 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1021 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1025 // This removes any dangling pointers to this window in other windows'
1026 // constraintsInvolvedIn lists.
1027 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1031 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1032 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1033 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1034 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1035 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1036 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1037 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1038 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1039 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1040 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1041 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1042 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1043 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1044 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1045 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1046 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1050 // Back-pointer to other windows we're involved with, so if we delete this
1051 // window, we must delete any constraints we're involved with.
1052 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1054 if ( !m_constraintsInvolvedIn
)
1055 m_constraintsInvolvedIn
= new wxWindowList
;
1056 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1057 m_constraintsInvolvedIn
->Append(otherWin
);
1060 // REMOVE back-pointer to other windows we're involved with.
1061 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1063 if ( m_constraintsInvolvedIn
)
1064 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1067 // Reset any constraints that mention this window
1068 void wxWindowBase::DeleteRelatedConstraints()
1070 if ( m_constraintsInvolvedIn
)
1072 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1075 wxWindow
*win
= node
->GetData();
1076 wxLayoutConstraints
*constr
= win
->GetConstraints();
1078 // Reset any constraints involving this window
1081 constr
->left
.ResetIfWin(this);
1082 constr
->top
.ResetIfWin(this);
1083 constr
->right
.ResetIfWin(this);
1084 constr
->bottom
.ResetIfWin(this);
1085 constr
->width
.ResetIfWin(this);
1086 constr
->height
.ResetIfWin(this);
1087 constr
->centreX
.ResetIfWin(this);
1088 constr
->centreY
.ResetIfWin(this);
1091 wxWindowList::Node
*next
= node
->GetNext();
1096 delete m_constraintsInvolvedIn
;
1097 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1101 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1103 if (m_windowSizer
) delete m_windowSizer
;
1105 m_windowSizer
= sizer
;
1108 bool wxWindowBase::Layout()
1110 // If there is a sizer, use it instead of the constraints
1114 GetClientSize(&w
, &h
);
1116 GetSizer()->SetDimension( 0, 0, w
, h
);
1120 wxLayoutConstraints
*constr
= GetConstraints();
1121 bool wasOk
= constr
&& constr
->AreSatisfied();
1123 ResetConstraints(); // Mark all constraints as unevaluated
1125 // if we're a top level panel (i.e. our parent is frame/dialog), our
1126 // own constraints will never be satisfied any more unless we do it
1131 while ( noChanges
> 0 )
1133 constr
->SatisfyConstraints(this, &noChanges
);
1137 DoPhase(1); // Layout children
1138 DoPhase(2); // Layout grand children
1139 SetConstraintSizes(); // Recursively set the real window sizes
1146 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1147 // do a similar thing, but also impose their own 'constraints' and order the
1148 // evaluation differently.
1149 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1151 wxLayoutConstraints
*constr
= GetConstraints();
1154 return constr
->SatisfyConstraints(this, noChanges
);
1160 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1170 // Do a phase of evaluating child constraints
1171 bool wxWindowBase::DoPhase(int phase
)
1173 int noIterations
= 0;
1174 int maxIterations
= 500;
1177 wxWindowList succeeded
;
1178 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1182 wxWindowList::Node
*node
= GetChildren().GetFirst();
1185 wxWindow
*child
= node
->GetData();
1186 if ( !child
->IsTopLevel() )
1188 wxLayoutConstraints
*constr
= child
->GetConstraints();
1191 if ( !succeeded
.Find(child
) )
1193 int tempNoChanges
= 0;
1194 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1195 noChanges
+= tempNoChanges
;
1198 succeeded
.Append(child
);
1203 node
= node
->GetNext();
1212 void wxWindowBase::ResetConstraints()
1214 wxLayoutConstraints
*constr
= GetConstraints();
1217 constr
->left
.SetDone(FALSE
);
1218 constr
->top
.SetDone(FALSE
);
1219 constr
->right
.SetDone(FALSE
);
1220 constr
->bottom
.SetDone(FALSE
);
1221 constr
->width
.SetDone(FALSE
);
1222 constr
->height
.SetDone(FALSE
);
1223 constr
->centreX
.SetDone(FALSE
);
1224 constr
->centreY
.SetDone(FALSE
);
1227 wxWindowList::Node
*node
= GetChildren().GetFirst();
1230 wxWindow
*win
= node
->GetData();
1231 if ( !win
->IsTopLevel() )
1232 win
->ResetConstraints();
1233 node
= node
->GetNext();
1237 // Need to distinguish between setting the 'fake' size for windows and sizers,
1238 // and setting the real values.
1239 void wxWindowBase::SetConstraintSizes(bool recurse
)
1241 wxLayoutConstraints
*constr
= GetConstraints();
1242 if ( constr
&& constr
->AreSatisfied() )
1244 int x
= constr
->left
.GetValue();
1245 int y
= constr
->top
.GetValue();
1246 int w
= constr
->width
.GetValue();
1247 int h
= constr
->height
.GetValue();
1249 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1250 (constr
->height
.GetRelationship() != wxAsIs
) )
1252 SetSize(x
, y
, w
, h
);
1256 // If we don't want to resize this window, just move it...
1262 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1263 GetClassInfo()->GetClassName(),
1269 wxWindowList::Node
*node
= GetChildren().GetFirst();
1272 wxWindow
*win
= node
->GetData();
1273 if ( !win
->IsTopLevel() )
1274 win
->SetConstraintSizes();
1275 node
= node
->GetNext();
1280 // Only set the size/position of the constraint (if any)
1281 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1283 wxLayoutConstraints
*constr
= GetConstraints();
1288 constr
->left
.SetValue(x
);
1289 constr
->left
.SetDone(TRUE
);
1293 constr
->top
.SetValue(y
);
1294 constr
->top
.SetDone(TRUE
);
1298 constr
->width
.SetValue(w
);
1299 constr
->width
.SetDone(TRUE
);
1303 constr
->height
.SetValue(h
);
1304 constr
->height
.SetDone(TRUE
);
1309 void wxWindowBase::MoveConstraint(int x
, int y
)
1311 wxLayoutConstraints
*constr
= GetConstraints();
1316 constr
->left
.SetValue(x
);
1317 constr
->left
.SetDone(TRUE
);
1321 constr
->top
.SetValue(y
);
1322 constr
->top
.SetDone(TRUE
);
1327 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1329 wxLayoutConstraints
*constr
= GetConstraints();
1332 *w
= constr
->width
.GetValue();
1333 *h
= constr
->height
.GetValue();
1339 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1341 wxLayoutConstraints
*constr
= GetConstraints();
1344 *w
= constr
->width
.GetValue();
1345 *h
= constr
->height
.GetValue();
1348 GetClientSize(w
, h
);
1351 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1353 wxLayoutConstraints
*constr
= GetConstraints();
1356 *x
= constr
->left
.GetValue();
1357 *y
= constr
->top
.GetValue();
1363 #endif // wxUSE_CONSTRAINTS
1365 // ----------------------------------------------------------------------------
1366 // do Update UI processing for child controls
1367 // ----------------------------------------------------------------------------
1369 // TODO: should this be implemented for the child window rather
1370 // than the parent? Then you can override it e.g. for wxCheckBox
1371 // to do the Right Thing rather than having to assume a fixed number
1372 // of control classes.
1373 void wxWindowBase::UpdateWindowUI()
1376 wxUpdateUIEvent
event(GetId());
1377 event
.m_eventObject
= this;
1379 if ( GetEventHandler()->ProcessEvent(event
) )
1381 if ( event
.GetSetEnabled() )
1382 Enable(event
.GetEnabled());
1384 if ( event
.GetSetText() )
1386 wxControl
*control
= wxDynamicThisCast(this, wxControl
);
1390 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1392 text
->SetValue(event
.GetText());
1394 #endif // wxUSE_TEXTCTRL
1395 control
->SetLabel(event
.GetText());
1400 wxCheckBox
*checkbox
= wxDynamicThisCast(this, wxCheckBox
);
1403 if ( event
.GetSetChecked() )
1404 checkbox
->SetValue(event
.GetChecked());
1406 #endif // wxUSE_CHECKBOX
1409 wxRadioButton
*radiobtn
= wxDynamicThisCast(this, wxRadioButton
);
1412 if ( event
.GetSetChecked() )
1413 radiobtn
->SetValue(event
.GetChecked());
1415 #endif // wxUSE_RADIOBTN
1417 #endif // wxUSE_CONTROLS
1420 // ----------------------------------------------------------------------------
1421 // dialog units translations
1422 // ----------------------------------------------------------------------------
1424 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1426 int charWidth
= GetCharWidth();
1427 int charHeight
= GetCharHeight();
1428 wxPoint
pt2(-1, -1);
1430 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1432 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1437 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1439 int charWidth
= GetCharWidth();
1440 int charHeight
= GetCharHeight();
1441 wxPoint
pt2(-1, -1);
1443 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1445 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1450 // ----------------------------------------------------------------------------
1452 // ----------------------------------------------------------------------------
1454 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1456 wxASSERT_MSG( m_clientDataType
!= wxClientData_Void
,
1457 wxT("can't have both object and void client data") );
1459 if ( m_clientObject
)
1460 delete m_clientObject
;
1462 m_clientObject
= data
;
1463 m_clientDataType
= wxClientData_Object
;
1466 wxClientData
*wxWindowBase::DoGetClientObject() const
1468 // it's not an error to call GetClientObject() on a window which doesn't
1469 // have client data at all - NULL will be returned
1470 wxASSERT_MSG( m_clientDataType
!= wxClientData_Void
,
1471 wxT("this window doesn't have object client data") );
1473 return m_clientObject
;
1476 void wxWindowBase::DoSetClientData( void *data
)
1478 wxASSERT_MSG( m_clientDataType
!= wxClientData_Object
,
1479 wxT("can't have both object and void client data") );
1481 m_clientData
= data
;
1482 m_clientDataType
= wxClientData_Void
;
1485 void *wxWindowBase::DoGetClientData() const
1487 // it's not an error to call GetClientData() on a window which doesn't have
1488 // client data at all - NULL will be returned
1489 wxASSERT_MSG( m_clientDataType
!= wxClientData_Object
,
1490 wxT("this window doesn't have void client data") );
1492 return m_clientData
;
1495 // ----------------------------------------------------------------------------
1497 // ----------------------------------------------------------------------------
1499 // propagate the colour change event to the subwindows
1500 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1502 wxWindowList::Node
*node
= GetChildren().GetFirst();
1505 // Only propagate to non-top-level windows
1506 wxWindow
*win
= node
->GetData();
1507 if ( !win
->IsTopLevel() )
1509 wxSysColourChangedEvent event2
;
1510 event
.m_eventObject
= win
;
1511 win
->GetEventHandler()->ProcessEvent(event2
);
1514 node
= node
->GetNext();
1518 // the default action is to populate dialog with data when it's created
1519 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1521 TransferDataToWindow();
1524 // process Ctrl-Alt-mclick
1525 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1528 if ( event
.ControlDown() && event
.AltDown() )
1530 // don't translate these strings
1532 switch ( wxGetOsVersion() )
1534 case wxMOTIF_X
: port
= _T("Motif"); break;
1535 case wxMACINTOSH
: port
= _T("Mac"); break;
1536 case wxBEOS
: port
= _T("BeOS"); break;
1540 case wxGTK_BEOS
: port
= _T("GTK"); break;
1546 case wxWIN386
: port
= _T("MS Windows"); break;
1550 case wxMGL_OS2
: port
= _T("MGL"); break;
1552 case wxOS2_PM
: port
= _T("OS/2"); break;
1553 default: port
= _T("unknown"); break;
1556 wxMessageBox(wxString::Format(
1558 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1567 _T("wxWindows information"),
1568 wxICON_INFORMATION
| wxOK
,
1572 #endif // wxUSE_MSGDLG
1578 // ----------------------------------------------------------------------------
1579 // list classes implementation
1580 // ----------------------------------------------------------------------------
1582 void wxWindowListNode::DeleteData()
1584 delete (wxWindow
*)GetData();
1587 // ----------------------------------------------------------------------------
1589 // ----------------------------------------------------------------------------
1591 wxBorder
wxWindowBase::GetBorder() const
1593 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1594 if ( border
== wxBORDER_DEFAULT
)
1596 border
= GetDefaultBorder();
1602 wxBorder
wxWindowBase::GetDefaultBorder() const
1604 return wxBORDER_NONE
;
1607 // ----------------------------------------------------------------------------
1609 // ----------------------------------------------------------------------------
1611 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1613 // here we just check if the point is inside the window or not
1615 // check the top and left border first
1616 bool outside
= x
< 0 || y
< 0;
1619 // check the right and bottom borders too
1620 wxSize size
= GetSize();
1621 outside
= x
>= size
.x
|| y
>= size
.y
;
1624 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;