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::GetColour(wxSYS_COLOUR_BTNFACE
);
128 m_foregroundColour
= wxSystemSettings::GetColour(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::GetFont(wxSYS_DEFAULT_GUI_FONT
);
137 // the colours/fonts are default for now
146 // an optimization for the event processing: checking this flag is much
147 // faster than using IsKindOf(CLASSINFO(wxWindow))
150 #if wxUSE_CONSTRAINTS
151 // no constraints whatsoever
152 m_constraints
= (wxLayoutConstraints
*) NULL
;
153 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
154 m_windowSizer
= (wxSizer
*) NULL
;
155 m_autoLayout
= FALSE
;
156 #endif // wxUSE_CONSTRAINTS
158 #if wxUSE_DRAG_AND_DROP
159 m_dropTarget
= (wxDropTarget
*)NULL
;
160 #endif // wxUSE_DRAG_AND_DROP
163 m_tooltip
= (wxToolTip
*)NULL
;
164 #endif // wxUSE_TOOLTIPS
167 m_caret
= (wxCaret
*)NULL
;
168 #endif // wxUSE_CARET
170 // Whether we're using the current theme for this window (wxGTK only for now)
171 m_themeEnabled
= FALSE
;
174 // common part of window creation process
175 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
177 const wxPoint
& WXUNUSED(pos
),
178 const wxSize
& WXUNUSED(size
),
180 const wxValidator
& validator
,
181 const wxString
& name
)
183 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
184 // member variables - check that it has been called (will catch the case
185 // when a new ctor is added which doesn't call InitWindow)
186 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
188 // generate a new id if the user doesn't care about it
189 m_windowId
= id
== -1 ? NewControlId() : id
;
192 SetWindowStyleFlag(style
);
196 SetValidator(validator
);
197 #endif // wxUSE_VALIDATORS
199 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
200 // have it too - like this it's possible to set it only in the top level
201 // dialog/frame and all children will inherit it by defult
202 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
204 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
215 wxWindowBase::~wxWindowBase()
217 // FIXME if these 2 cases result from programming errors in the user code
218 // we should probably assert here instead of silently fixing them
220 // Just in case the window has been Closed, but we're then deleting
221 // immediately: don't leave dangling pointers.
222 wxPendingDelete
.DeleteObject(this);
224 // Just in case we've loaded a top-level window via LoadNativeDialog but
225 // we weren't a dialog class
226 wxTopLevelWindows
.DeleteObject(this);
228 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
233 #endif // wxUSE_CARET
236 if ( m_windowValidator
)
237 delete m_windowValidator
;
238 #endif // wxUSE_VALIDATORS
240 #if wxUSE_CONSTRAINTS
241 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
242 // at deleted windows as they delete themselves.
243 DeleteRelatedConstraints();
247 // This removes any dangling pointers to this window in other windows'
248 // constraintsInvolvedIn lists.
249 UnsetConstraints(m_constraints
);
250 delete m_constraints
;
251 m_constraints
= NULL
;
255 delete m_windowSizer
;
257 #endif // wxUSE_CONSTRAINTS
259 #if wxUSE_DRAG_AND_DROP
262 #endif // wxUSE_DRAG_AND_DROP
267 #endif // wxUSE_TOOLTIPS
269 // reset the dangling pointer our parent window may keep to us
270 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
272 m_parent
->SetDefaultItem(NULL
);
276 bool wxWindowBase::Destroy()
283 bool wxWindowBase::Close(bool force
)
285 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
286 event
.SetEventObject(this);
287 #if WXWIN_COMPATIBILITY
288 event
.SetForce(force
);
289 #endif // WXWIN_COMPATIBILITY
290 event
.SetCanVeto(!force
);
292 // return FALSE if window wasn't closed because the application vetoed the
294 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
297 bool wxWindowBase::DestroyChildren()
299 wxWindowList::Node
*node
;
302 // we iterate until the list becomes empty
303 node
= GetChildren().GetFirst();
307 wxWindow
*child
= node
->GetData();
309 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
314 wxASSERT_MSG( !GetChildren().Find(child
),
315 wxT("child didn't remove itself using RemoveChild()") );
321 // ----------------------------------------------------------------------------
322 // size/position related methods
323 // ----------------------------------------------------------------------------
325 // centre the window with respect to its parent in either (or both) directions
326 void wxWindowBase::Centre(int direction
)
328 // the position/size of the parent window or of the entire screen
330 int widthParent
, heightParent
;
332 wxWindow
*parent
= NULL
;
334 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
336 // find the parent to centre this window on: it should be the
337 // immediate parent for the controls but the top level parent for the
338 // top level windows (like dialogs)
339 parent
= GetParent();
342 while ( parent
&& !parent
->IsTopLevel() )
344 parent
= parent
->GetParent();
348 // did we find the parent?
352 direction
|= wxCENTRE_ON_SCREEN
;
356 if ( direction
& wxCENTRE_ON_SCREEN
)
358 // centre with respect to the whole screen
359 wxDisplaySize(&widthParent
, &heightParent
);
365 // centre on the parent
366 parent
->GetSize(&widthParent
, &heightParent
);
368 // adjust to the parents position
369 posParent
= parent
->GetPosition();
373 // centre inside the parents client rectangle
374 parent
->GetClientSize(&widthParent
, &heightParent
);
379 GetSize(&width
, &height
);
384 if ( direction
& wxHORIZONTAL
)
385 xNew
= (widthParent
- width
)/2;
387 if ( direction
& wxVERTICAL
)
388 yNew
= (heightParent
- height
)/2;
393 // Base size of the visible dimensions of the display
394 // to take into account the taskbar
395 wxRect rect
= wxGetClientDisplayRect();
396 wxSize
size (rect
.width
,rect
.height
);
398 if (posParent
.x
>= 0) // if parent is on the main display
402 else if (xNew
+width
> size
.x
)
403 xNew
= size
.x
-width
-1;
405 if (posParent
.y
>= 0) // if parent is on the main display
407 if (yNew
+height
> size
.y
)
408 yNew
= size
.y
-height
-1;
410 // Make certain that the title bar is initially visible
411 // always, even if this would push the bottom of the
412 // dialog of the visible area of the display
417 // move the window to this position (keeping the old size but using
418 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
419 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
422 // fits the window around the children
423 void wxWindowBase::Fit()
425 if ( GetChildren().GetCount() > 0 )
427 wxSize size
= DoGetBestSize();
429 // for compatibility with the old versions and because it really looks
430 // slightly more pretty like this, add a pad
436 //else: do nothing if we have no children
439 // return the size best suited for the current window
440 wxSize
wxWindowBase::DoGetBestSize() const
442 if ( GetChildren().GetCount() > 0 )
444 // our minimal acceptable size is such that all our windows fit inside
448 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
450 node
= node
->GetNext() )
452 wxWindow
*win
= node
->GetData();
453 if ( win
->IsTopLevel()
455 || wxDynamicCast(win
, wxStatusBar
)
456 #endif // wxUSE_STATUSBAR
459 // dialogs and frames lie in different top level windows -
460 // don't deal with them here; as for the status bars, they
461 // don't lie in the client area at all
466 win
->GetPosition(&wx
, &wy
);
468 // if the window hadn't been positioned yet, assume that it is in
475 win
->GetSize(&ww
, &wh
);
476 if ( wx
+ ww
> maxX
)
478 if ( wy
+ wh
> maxY
)
482 return wxSize(maxX
, maxY
);
486 // for a generic window there is no natural best size - just use the
492 // by default the origin is not shifted
493 wxPoint
wxWindowBase::GetClientAreaOrigin() const
495 return wxPoint(0, 0);
498 // set the min/max size of the window
499 void wxWindowBase::SetSizeHints(int minW
, int minH
,
501 int WXUNUSED(incW
), int WXUNUSED(incH
))
509 // ----------------------------------------------------------------------------
510 // show/hide/enable/disable the window
511 // ----------------------------------------------------------------------------
513 bool wxWindowBase::Show(bool show
)
515 if ( show
!= m_isShown
)
527 bool wxWindowBase::Enable(bool enable
)
529 if ( enable
!= m_isEnabled
)
531 m_isEnabled
= enable
;
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 bool wxWindowBase::IsTopLevel() const
549 // ----------------------------------------------------------------------------
550 // reparenting the window
551 // ----------------------------------------------------------------------------
553 void wxWindowBase::AddChild(wxWindowBase
*child
)
555 wxCHECK_RET( child
, wxT("can't add a NULL child") );
557 // this should never happen and it will lead to a crash later if it does
558 // because RemoveChild() will remove only one node from the children list
559 // and the other(s) one(s) will be left with dangling pointers in them
560 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
562 GetChildren().Append(child
);
563 child
->SetParent(this);
566 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
568 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
570 GetChildren().DeleteObject(child
);
571 child
->SetParent((wxWindow
*)NULL
);
574 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
576 wxWindow
*oldParent
= GetParent();
577 if ( newParent
== oldParent
)
583 // unlink this window from the existing parent.
586 oldParent
->RemoveChild(this);
590 wxTopLevelWindows
.DeleteObject(this);
593 // add it to the new one
596 newParent
->AddChild(this);
600 wxTopLevelWindows
.Append(this);
606 // ----------------------------------------------------------------------------
607 // event handler stuff
608 // ----------------------------------------------------------------------------
610 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
612 handler
->SetNextHandler(GetEventHandler());
613 SetEventHandler(handler
);
616 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
618 wxEvtHandler
*handlerA
= GetEventHandler();
621 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
622 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
623 SetEventHandler(handlerB
);
627 handlerA
= (wxEvtHandler
*)NULL
;
634 // ----------------------------------------------------------------------------
636 // ----------------------------------------------------------------------------
638 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
640 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
643 m_backgroundColour
= colour
;
650 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
652 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
655 m_foregroundColour
= colour
;
662 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
664 // setting an invalid cursor is ok, it means that we don't have any special
666 if ( m_cursor
== cursor
)
677 bool wxWindowBase::SetFont(const wxFont
& font
)
679 // don't try to set invalid font, always fall back to the default
680 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
682 if ( fontOk
== m_font
)
696 void wxWindowBase::SetCaret(wxCaret
*caret
)
707 wxASSERT_MSG( m_caret
->GetWindow() == this,
708 wxT("caret should be created associated to this window") );
711 #endif // wxUSE_CARET
714 // ----------------------------------------------------------------------------
716 // ----------------------------------------------------------------------------
718 void wxWindowBase::SetValidator(const wxValidator
& validator
)
720 if ( m_windowValidator
)
721 delete m_windowValidator
;
723 m_windowValidator
= (wxValidator
*)validator
.Clone();
725 if ( m_windowValidator
)
726 m_windowValidator
->SetWindow(this) ;
728 #endif // wxUSE_VALIDATORS
730 // ----------------------------------------------------------------------------
731 // update region stuff
732 // ----------------------------------------------------------------------------
734 wxRect
wxWindowBase::GetUpdateClientRect() const
736 wxRegion rgnUpdate
= GetUpdateRegion();
737 rgnUpdate
.Intersect(GetClientRect());
738 wxRect rectUpdate
= rgnUpdate
.GetBox();
739 wxPoint ptOrigin
= GetClientAreaOrigin();
740 rectUpdate
.x
-= ptOrigin
.x
;
741 rectUpdate
.y
-= ptOrigin
.y
;
746 bool wxWindowBase::IsExposed(int x
, int y
) const
748 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
751 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
753 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
756 // ----------------------------------------------------------------------------
757 // find window by id or name
758 // ----------------------------------------------------------------------------
760 wxWindow
*wxWindowBase::FindWindow( long id
)
762 if ( id
== m_windowId
)
763 return (wxWindow
*)this;
765 wxWindowBase
*res
= (wxWindow
*)NULL
;
766 wxWindowList::Node
*node
;
767 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
769 wxWindowBase
*child
= node
->GetData();
770 res
= child
->FindWindow( id
);
773 return (wxWindow
*)res
;
776 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
778 if ( name
== m_windowName
)
779 return (wxWindow
*)this;
781 wxWindowBase
*res
= (wxWindow
*)NULL
;
782 wxWindowList::Node
*node
;
783 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
785 wxWindow
*child
= node
->GetData();
786 res
= child
->FindWindow(name
);
789 return (wxWindow
*)res
;
792 // ----------------------------------------------------------------------------
793 // dialog oriented functions
794 // ----------------------------------------------------------------------------
796 void wxWindowBase::MakeModal(bool modal
)
798 // Disable all other windows
801 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
804 wxWindow
*win
= node
->GetData();
808 node
= node
->GetNext();
813 bool wxWindowBase::Validate()
816 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
818 wxWindowList::Node
*node
;
819 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
821 wxWindowBase
*child
= node
->GetData();
822 wxValidator
*validator
= child
->GetValidator();
823 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
828 if ( recurse
&& !child
->Validate() )
833 #endif // wxUSE_VALIDATORS
838 bool wxWindowBase::TransferDataToWindow()
841 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
843 wxWindowList::Node
*node
;
844 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
846 wxWindowBase
*child
= node
->GetData();
847 wxValidator
*validator
= child
->GetValidator();
848 if ( validator
&& !validator
->TransferToWindow() )
850 wxLogWarning(_("Could not transfer data to window"));
851 wxLog::FlushActive();
858 if ( !child
->TransferDataToWindow() )
860 // warning already given
865 #endif // wxUSE_VALIDATORS
870 bool wxWindowBase::TransferDataFromWindow()
873 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
875 wxWindowList::Node
*node
;
876 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
878 wxWindow
*child
= node
->GetData();
879 wxValidator
*validator
= child
->GetValidator();
880 if ( validator
&& !validator
->TransferFromWindow() )
882 // nop warning here because the application is supposed to give
883 // one itself - we don't know here what might have gone wrongly
890 if ( !child
->TransferDataFromWindow() )
892 // warning already given
897 #endif // wxUSE_VALIDATORS
902 void wxWindowBase::InitDialog()
904 wxInitDialogEvent
event(GetId());
905 event
.SetEventObject( this );
906 GetEventHandler()->ProcessEvent(event
);
909 // ----------------------------------------------------------------------------
910 // context-sensitive help support
911 // ----------------------------------------------------------------------------
915 // associate this help text with this window
916 void wxWindowBase::SetHelpText(const wxString
& text
)
918 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
921 helpProvider
->AddHelp(this, text
);
925 // associate this help text with all windows with the same id as this
927 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
929 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
932 helpProvider
->AddHelp(GetId(), text
);
936 // get the help string associated with this window (may be empty)
937 wxString
wxWindowBase::GetHelpText() const
940 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
943 text
= helpProvider
->GetHelp(this);
949 // show help for this window
950 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
952 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
955 if ( helpProvider
->ShowHelp(this) )
957 // skip the event.Skip() below
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
973 void wxWindowBase::SetToolTip( const wxString
&tip
)
975 // don't create the new tooltip if we already have one
978 m_tooltip
->SetTip( tip
);
982 SetToolTip( new wxToolTip( tip
) );
985 // setting empty tooltip text does not remove the tooltip any more - use
986 // SetToolTip((wxToolTip *)NULL) for this
989 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
997 #endif // wxUSE_TOOLTIPS
999 // ----------------------------------------------------------------------------
1000 // constraints and sizers
1001 // ----------------------------------------------------------------------------
1003 #if wxUSE_CONSTRAINTS
1005 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1007 if ( m_constraints
)
1009 UnsetConstraints(m_constraints
);
1010 delete m_constraints
;
1012 m_constraints
= constraints
;
1013 if ( m_constraints
)
1015 // Make sure other windows know they're part of a 'meaningful relationship'
1016 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1017 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1018 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1019 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1020 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1021 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1022 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1023 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1024 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1025 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1026 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1027 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1028 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1029 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1030 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1031 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1035 // This removes any dangling pointers to this window in other windows'
1036 // constraintsInvolvedIn lists.
1037 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1041 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1042 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1043 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1044 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1045 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1046 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1047 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1048 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1049 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1050 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1051 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1052 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1053 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1054 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1055 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1056 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1060 // Back-pointer to other windows we're involved with, so if we delete this
1061 // window, we must delete any constraints we're involved with.
1062 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1064 if ( !m_constraintsInvolvedIn
)
1065 m_constraintsInvolvedIn
= new wxWindowList
;
1066 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1067 m_constraintsInvolvedIn
->Append(otherWin
);
1070 // REMOVE back-pointer to other windows we're involved with.
1071 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1073 if ( m_constraintsInvolvedIn
)
1074 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1077 // Reset any constraints that mention this window
1078 void wxWindowBase::DeleteRelatedConstraints()
1080 if ( m_constraintsInvolvedIn
)
1082 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1085 wxWindow
*win
= node
->GetData();
1086 wxLayoutConstraints
*constr
= win
->GetConstraints();
1088 // Reset any constraints involving this window
1091 constr
->left
.ResetIfWin(this);
1092 constr
->top
.ResetIfWin(this);
1093 constr
->right
.ResetIfWin(this);
1094 constr
->bottom
.ResetIfWin(this);
1095 constr
->width
.ResetIfWin(this);
1096 constr
->height
.ResetIfWin(this);
1097 constr
->centreX
.ResetIfWin(this);
1098 constr
->centreY
.ResetIfWin(this);
1101 wxWindowList::Node
*next
= node
->GetNext();
1106 delete m_constraintsInvolvedIn
;
1107 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1111 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1113 if (m_windowSizer
) delete m_windowSizer
;
1115 m_windowSizer
= sizer
;
1118 bool wxWindowBase::Layout()
1120 // If there is a sizer, use it instead of the constraints
1124 GetClientSize(&w
, &h
);
1126 GetSizer()->SetDimension( 0, 0, w
, h
);
1130 wxLayoutConstraints
*constr
= GetConstraints();
1131 bool wasOk
= constr
&& constr
->AreSatisfied();
1133 ResetConstraints(); // Mark all constraints as unevaluated
1135 // if we're a top level panel (i.e. our parent is frame/dialog), our
1136 // own constraints will never be satisfied any more unless we do it
1141 while ( noChanges
> 0 )
1143 constr
->SatisfyConstraints(this, &noChanges
);
1147 DoPhase(1); // Layout children
1148 DoPhase(2); // Layout grand children
1149 SetConstraintSizes(); // Recursively set the real window sizes
1156 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1157 // do a similar thing, but also impose their own 'constraints' and order the
1158 // evaluation differently.
1159 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1161 wxLayoutConstraints
*constr
= GetConstraints();
1164 return constr
->SatisfyConstraints(this, noChanges
);
1170 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1180 // Do a phase of evaluating child constraints
1181 bool wxWindowBase::DoPhase(int phase
)
1183 int noIterations
= 0;
1184 int maxIterations
= 500;
1187 wxWindowList succeeded
;
1188 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1192 wxWindowList::Node
*node
= GetChildren().GetFirst();
1195 wxWindow
*child
= node
->GetData();
1196 if ( !child
->IsTopLevel() )
1198 wxLayoutConstraints
*constr
= child
->GetConstraints();
1201 if ( !succeeded
.Find(child
) )
1203 int tempNoChanges
= 0;
1204 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1205 noChanges
+= tempNoChanges
;
1208 succeeded
.Append(child
);
1213 node
= node
->GetNext();
1222 void wxWindowBase::ResetConstraints()
1224 wxLayoutConstraints
*constr
= GetConstraints();
1227 constr
->left
.SetDone(FALSE
);
1228 constr
->top
.SetDone(FALSE
);
1229 constr
->right
.SetDone(FALSE
);
1230 constr
->bottom
.SetDone(FALSE
);
1231 constr
->width
.SetDone(FALSE
);
1232 constr
->height
.SetDone(FALSE
);
1233 constr
->centreX
.SetDone(FALSE
);
1234 constr
->centreY
.SetDone(FALSE
);
1237 wxWindowList::Node
*node
= GetChildren().GetFirst();
1240 wxWindow
*win
= node
->GetData();
1241 if ( !win
->IsTopLevel() )
1242 win
->ResetConstraints();
1243 node
= node
->GetNext();
1247 // Need to distinguish between setting the 'fake' size for windows and sizers,
1248 // and setting the real values.
1249 void wxWindowBase::SetConstraintSizes(bool recurse
)
1251 wxLayoutConstraints
*constr
= GetConstraints();
1252 if ( constr
&& constr
->AreSatisfied() )
1254 int x
= constr
->left
.GetValue();
1255 int y
= constr
->top
.GetValue();
1256 int w
= constr
->width
.GetValue();
1257 int h
= constr
->height
.GetValue();
1259 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1260 (constr
->height
.GetRelationship() != wxAsIs
) )
1262 SetSize(x
, y
, w
, h
);
1266 // If we don't want to resize this window, just move it...
1272 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1273 GetClassInfo()->GetClassName(),
1279 wxWindowList::Node
*node
= GetChildren().GetFirst();
1282 wxWindow
*win
= node
->GetData();
1283 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1284 win
->SetConstraintSizes();
1285 node
= node
->GetNext();
1290 // Only set the size/position of the constraint (if any)
1291 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1293 wxLayoutConstraints
*constr
= GetConstraints();
1298 constr
->left
.SetValue(x
);
1299 constr
->left
.SetDone(TRUE
);
1303 constr
->top
.SetValue(y
);
1304 constr
->top
.SetDone(TRUE
);
1308 constr
->width
.SetValue(w
);
1309 constr
->width
.SetDone(TRUE
);
1313 constr
->height
.SetValue(h
);
1314 constr
->height
.SetDone(TRUE
);
1319 void wxWindowBase::MoveConstraint(int x
, int y
)
1321 wxLayoutConstraints
*constr
= GetConstraints();
1326 constr
->left
.SetValue(x
);
1327 constr
->left
.SetDone(TRUE
);
1331 constr
->top
.SetValue(y
);
1332 constr
->top
.SetDone(TRUE
);
1337 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1339 wxLayoutConstraints
*constr
= GetConstraints();
1342 *w
= constr
->width
.GetValue();
1343 *h
= constr
->height
.GetValue();
1349 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1351 wxLayoutConstraints
*constr
= GetConstraints();
1354 *w
= constr
->width
.GetValue();
1355 *h
= constr
->height
.GetValue();
1358 GetClientSize(w
, h
);
1361 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1363 // don't do it for the dialogs/frames - they float independently of their
1365 if ( !IsTopLevel() )
1367 wxWindow
*parent
= GetParent();
1368 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1370 wxPoint
pt(parent
->GetClientAreaOrigin());
1378 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1380 wxLayoutConstraints
*constr
= GetConstraints();
1383 *x
= constr
->left
.GetValue();
1384 *y
= constr
->top
.GetValue();
1390 #endif // wxUSE_CONSTRAINTS
1392 // ----------------------------------------------------------------------------
1393 // do Update UI processing for child controls
1394 // ----------------------------------------------------------------------------
1396 // TODO: should this be implemented for the child window rather
1397 // than the parent? Then you can override it e.g. for wxCheckBox
1398 // to do the Right Thing rather than having to assume a fixed number
1399 // of control classes.
1400 void wxWindowBase::UpdateWindowUI()
1403 wxUpdateUIEvent
event(GetId());
1404 event
.m_eventObject
= this;
1406 if ( GetEventHandler()->ProcessEvent(event
) )
1408 if ( event
.GetSetEnabled() )
1409 Enable(event
.GetEnabled());
1411 if ( event
.GetSetText() )
1413 wxControl
*control
= wxDynamicCastThis(wxControl
);
1417 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1419 text
->SetValue(event
.GetText());
1421 #endif // wxUSE_TEXTCTRL
1422 control
->SetLabel(event
.GetText());
1427 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1430 if ( event
.GetSetChecked() )
1431 checkbox
->SetValue(event
.GetChecked());
1433 #endif // wxUSE_CHECKBOX
1436 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1439 if ( event
.GetSetChecked() )
1440 radiobtn
->SetValue(event
.GetChecked());
1442 #endif // wxUSE_RADIOBTN
1444 #endif // wxUSE_CONTROLS
1447 // ----------------------------------------------------------------------------
1448 // dialog units translations
1449 // ----------------------------------------------------------------------------
1451 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1453 int charWidth
= GetCharWidth();
1454 int charHeight
= GetCharHeight();
1455 wxPoint
pt2(-1, -1);
1457 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1459 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1464 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1466 int charWidth
= GetCharWidth();
1467 int charHeight
= GetCharHeight();
1468 wxPoint
pt2(-1, -1);
1470 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1472 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1477 // ----------------------------------------------------------------------------
1479 // ----------------------------------------------------------------------------
1481 // propagate the colour change event to the subwindows
1482 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1484 wxWindowList::Node
*node
= GetChildren().GetFirst();
1487 // Only propagate to non-top-level windows
1488 wxWindow
*win
= node
->GetData();
1489 if ( !win
->IsTopLevel() )
1491 wxSysColourChangedEvent event2
;
1492 event
.m_eventObject
= win
;
1493 win
->GetEventHandler()->ProcessEvent(event2
);
1496 node
= node
->GetNext();
1500 // the default action is to populate dialog with data when it's created
1501 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1503 TransferDataToWindow();
1506 // process Ctrl-Alt-mclick
1507 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1510 if ( event
.ControlDown() && event
.AltDown() )
1512 // don't translate these strings
1514 switch ( wxGetOsVersion() )
1516 case wxMOTIF_X
: port
= _T("Motif"); break;
1518 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1519 case wxBEOS
: port
= _T("BeOS"); break;
1523 case wxGTK_BEOS
: port
= _T("GTK"); break;
1529 case wxWIN386
: port
= _T("MS Windows"); break;
1533 case wxMGL_OS2
: port
= _T("MGL"); break;
1535 case wxOS2_PM
: port
= _T("OS/2"); break;
1536 default: port
= _T("unknown"); break;
1539 wxMessageBox(wxString::Format(
1541 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2001 wxWindows team"
1555 _T("wxWindows information"),
1556 wxICON_INFORMATION
| wxOK
,
1560 #endif // wxUSE_MSGDLG
1566 // ----------------------------------------------------------------------------
1567 // list classes implementation
1568 // ----------------------------------------------------------------------------
1570 void wxWindowListNode::DeleteData()
1572 delete (wxWindow
*)GetData();
1575 // ----------------------------------------------------------------------------
1577 // ----------------------------------------------------------------------------
1579 wxBorder
wxWindowBase::GetBorder() const
1581 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1582 if ( border
== wxBORDER_DEFAULT
)
1584 border
= GetDefaultBorder();
1590 wxBorder
wxWindowBase::GetDefaultBorder() const
1592 return wxBORDER_NONE
;
1595 // ----------------------------------------------------------------------------
1597 // ----------------------------------------------------------------------------
1599 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1601 // here we just check if the point is inside the window or not
1603 // check the top and left border first
1604 bool outside
= x
< 0 || y
< 0;
1607 // check the right and bottom borders too
1608 wxSize size
= GetSize();
1609 outside
= x
>= size
.x
|| y
>= size
.y
;
1612 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;