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
;
643 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
645 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
648 m_foregroundColour
= colour
;
653 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
655 // setting an invalid cursor is ok, it means that we don't have any special
657 if ( m_cursor
== cursor
)
668 bool wxWindowBase::SetFont(const wxFont
& font
)
670 // don't try to set invalid font, always fall back to the default
671 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
673 if ( fontOk
== m_font
)
685 void wxWindowBase::SetCaret(wxCaret
*caret
)
696 wxASSERT_MSG( m_caret
->GetWindow() == this,
697 wxT("caret should be created associated to this window") );
700 #endif // wxUSE_CARET
703 // ----------------------------------------------------------------------------
705 // ----------------------------------------------------------------------------
707 void wxWindowBase::SetValidator(const wxValidator
& validator
)
709 if ( m_windowValidator
)
710 delete m_windowValidator
;
712 m_windowValidator
= (wxValidator
*)validator
.Clone();
714 if ( m_windowValidator
)
715 m_windowValidator
->SetWindow(this) ;
717 #endif // wxUSE_VALIDATORS
719 // ----------------------------------------------------------------------------
720 // update region stuff
721 // ----------------------------------------------------------------------------
723 wxRect
wxWindowBase::GetUpdateClientRect() const
725 wxRegion rgnUpdate
= GetUpdateRegion();
726 rgnUpdate
.Intersect(GetClientRect());
727 wxRect rectUpdate
= rgnUpdate
.GetBox();
728 wxPoint ptOrigin
= GetClientAreaOrigin();
729 rectUpdate
.x
-= ptOrigin
.x
;
730 rectUpdate
.y
-= ptOrigin
.y
;
735 bool wxWindowBase::IsExposed(int x
, int y
) const
737 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
740 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
742 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
745 // ----------------------------------------------------------------------------
746 // find window by id or name
747 // ----------------------------------------------------------------------------
749 wxWindow
*wxWindowBase::FindWindow( long id
)
751 if ( id
== m_windowId
)
752 return (wxWindow
*)this;
754 wxWindowBase
*res
= (wxWindow
*)NULL
;
755 wxWindowList::Node
*node
;
756 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
758 wxWindowBase
*child
= node
->GetData();
759 res
= child
->FindWindow( id
);
762 return (wxWindow
*)res
;
765 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
767 if ( name
== m_windowName
)
768 return (wxWindow
*)this;
770 wxWindowBase
*res
= (wxWindow
*)NULL
;
771 wxWindowList::Node
*node
;
772 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
774 wxWindow
*child
= node
->GetData();
775 res
= child
->FindWindow(name
);
778 return (wxWindow
*)res
;
781 // ----------------------------------------------------------------------------
782 // dialog oriented functions
783 // ----------------------------------------------------------------------------
785 void wxWindowBase::MakeModal(bool modal
)
787 // Disable all other windows
790 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
793 wxWindow
*win
= node
->GetData();
797 node
= node
->GetNext();
802 bool wxWindowBase::Validate()
805 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
807 wxWindowList::Node
*node
;
808 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
810 wxWindowBase
*child
= node
->GetData();
811 wxValidator
*validator
= child
->GetValidator();
812 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
817 if ( recurse
&& !child
->Validate() )
822 #endif // wxUSE_VALIDATORS
827 bool wxWindowBase::TransferDataToWindow()
830 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
832 wxWindowList::Node
*node
;
833 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
835 wxWindowBase
*child
= node
->GetData();
836 wxValidator
*validator
= child
->GetValidator();
837 if ( validator
&& !validator
->TransferToWindow() )
839 wxLogWarning(_("Could not transfer data to window"));
840 wxLog::FlushActive();
847 if ( !child
->TransferDataToWindow() )
849 // warning already given
854 #endif // wxUSE_VALIDATORS
859 bool wxWindowBase::TransferDataFromWindow()
862 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
864 wxWindowList::Node
*node
;
865 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
867 wxWindow
*child
= node
->GetData();
868 wxValidator
*validator
= child
->GetValidator();
869 if ( validator
&& !validator
->TransferFromWindow() )
871 // nop warning here because the application is supposed to give
872 // one itself - we don't know here what might have gone wrongly
879 if ( !child
->TransferDataFromWindow() )
881 // warning already given
886 #endif // wxUSE_VALIDATORS
891 void wxWindowBase::InitDialog()
893 wxInitDialogEvent
event(GetId());
894 event
.SetEventObject( this );
895 GetEventHandler()->ProcessEvent(event
);
898 // ----------------------------------------------------------------------------
899 // context-sensitive help support
900 // ----------------------------------------------------------------------------
904 // associate this help text with this window
905 void wxWindowBase::SetHelpText(const wxString
& text
)
907 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
910 helpProvider
->AddHelp(this, text
);
914 // associate this help text with all windows with the same id as this
916 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
918 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
921 helpProvider
->AddHelp(GetId(), text
);
925 // get the help string associated with this window (may be empty)
926 wxString
wxWindowBase::GetHelpText() const
929 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
932 text
= helpProvider
->GetHelp(this);
938 // show help for this window
939 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
941 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
944 if ( helpProvider
->ShowHelp(this) )
946 // skip the event.Skip() below
956 // ----------------------------------------------------------------------------
958 // ----------------------------------------------------------------------------
962 void wxWindowBase::SetToolTip( const wxString
&tip
)
964 // don't create the new tooltip if we already have one
967 m_tooltip
->SetTip( tip
);
971 SetToolTip( new wxToolTip( tip
) );
974 // setting empty tooltip text does not remove the tooltip any more - use
975 // SetToolTip((wxToolTip *)NULL) for this
978 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
986 #endif // wxUSE_TOOLTIPS
988 // ----------------------------------------------------------------------------
989 // constraints and sizers
990 // ----------------------------------------------------------------------------
992 #if wxUSE_CONSTRAINTS
994 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
998 UnsetConstraints(m_constraints
);
999 delete m_constraints
;
1001 m_constraints
= constraints
;
1002 if ( m_constraints
)
1004 // Make sure other windows know they're part of a 'meaningful relationship'
1005 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1006 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1007 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1008 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1009 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1010 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1011 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1012 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1013 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1014 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1015 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1016 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1017 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1018 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1019 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1020 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1024 // This removes any dangling pointers to this window in other windows'
1025 // constraintsInvolvedIn lists.
1026 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1030 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1031 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1032 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1033 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1034 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1035 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1036 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1037 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1038 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1039 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1040 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1041 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1042 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1043 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1044 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1045 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1049 // Back-pointer to other windows we're involved with, so if we delete this
1050 // window, we must delete any constraints we're involved with.
1051 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1053 if ( !m_constraintsInvolvedIn
)
1054 m_constraintsInvolvedIn
= new wxWindowList
;
1055 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1056 m_constraintsInvolvedIn
->Append(otherWin
);
1059 // REMOVE back-pointer to other windows we're involved with.
1060 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1062 if ( m_constraintsInvolvedIn
)
1063 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1066 // Reset any constraints that mention this window
1067 void wxWindowBase::DeleteRelatedConstraints()
1069 if ( m_constraintsInvolvedIn
)
1071 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1074 wxWindow
*win
= node
->GetData();
1075 wxLayoutConstraints
*constr
= win
->GetConstraints();
1077 // Reset any constraints involving this window
1080 constr
->left
.ResetIfWin(this);
1081 constr
->top
.ResetIfWin(this);
1082 constr
->right
.ResetIfWin(this);
1083 constr
->bottom
.ResetIfWin(this);
1084 constr
->width
.ResetIfWin(this);
1085 constr
->height
.ResetIfWin(this);
1086 constr
->centreX
.ResetIfWin(this);
1087 constr
->centreY
.ResetIfWin(this);
1090 wxWindowList::Node
*next
= node
->GetNext();
1095 delete m_constraintsInvolvedIn
;
1096 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1100 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1102 if (m_windowSizer
) delete m_windowSizer
;
1104 m_windowSizer
= sizer
;
1107 bool wxWindowBase::Layout()
1109 // If there is a sizer, use it instead of the constraints
1113 GetClientSize(&w
, &h
);
1115 GetSizer()->SetDimension( 0, 0, w
, h
);
1119 wxLayoutConstraints
*constr
= GetConstraints();
1120 bool wasOk
= constr
&& constr
->AreSatisfied();
1122 ResetConstraints(); // Mark all constraints as unevaluated
1124 // if we're a top level panel (i.e. our parent is frame/dialog), our
1125 // own constraints will never be satisfied any more unless we do it
1130 while ( noChanges
> 0 )
1132 constr
->SatisfyConstraints(this, &noChanges
);
1136 DoPhase(1); // Layout children
1137 DoPhase(2); // Layout grand children
1138 SetConstraintSizes(); // Recursively set the real window sizes
1145 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1146 // do a similar thing, but also impose their own 'constraints' and order the
1147 // evaluation differently.
1148 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1150 wxLayoutConstraints
*constr
= GetConstraints();
1153 return constr
->SatisfyConstraints(this, noChanges
);
1159 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1169 // Do a phase of evaluating child constraints
1170 bool wxWindowBase::DoPhase(int phase
)
1172 int noIterations
= 0;
1173 int maxIterations
= 500;
1176 wxWindowList succeeded
;
1177 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1181 wxWindowList::Node
*node
= GetChildren().GetFirst();
1184 wxWindow
*child
= node
->GetData();
1185 if ( !child
->IsTopLevel() )
1187 wxLayoutConstraints
*constr
= child
->GetConstraints();
1190 if ( !succeeded
.Find(child
) )
1192 int tempNoChanges
= 0;
1193 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1194 noChanges
+= tempNoChanges
;
1197 succeeded
.Append(child
);
1202 node
= node
->GetNext();
1211 void wxWindowBase::ResetConstraints()
1213 wxLayoutConstraints
*constr
= GetConstraints();
1216 constr
->left
.SetDone(FALSE
);
1217 constr
->top
.SetDone(FALSE
);
1218 constr
->right
.SetDone(FALSE
);
1219 constr
->bottom
.SetDone(FALSE
);
1220 constr
->width
.SetDone(FALSE
);
1221 constr
->height
.SetDone(FALSE
);
1222 constr
->centreX
.SetDone(FALSE
);
1223 constr
->centreY
.SetDone(FALSE
);
1226 wxWindowList::Node
*node
= GetChildren().GetFirst();
1229 wxWindow
*win
= node
->GetData();
1230 if ( !win
->IsTopLevel() )
1231 win
->ResetConstraints();
1232 node
= node
->GetNext();
1236 // Need to distinguish between setting the 'fake' size for windows and sizers,
1237 // and setting the real values.
1238 void wxWindowBase::SetConstraintSizes(bool recurse
)
1240 wxLayoutConstraints
*constr
= GetConstraints();
1241 if ( constr
&& constr
->AreSatisfied() )
1243 int x
= constr
->left
.GetValue();
1244 int y
= constr
->top
.GetValue();
1245 int w
= constr
->width
.GetValue();
1246 int h
= constr
->height
.GetValue();
1248 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1249 (constr
->height
.GetRelationship() != wxAsIs
) )
1251 SetSize(x
, y
, w
, h
);
1255 // If we don't want to resize this window, just move it...
1261 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1262 GetClassInfo()->GetClassName(),
1268 wxWindowList::Node
*node
= GetChildren().GetFirst();
1271 wxWindow
*win
= node
->GetData();
1272 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1273 win
->SetConstraintSizes();
1274 node
= node
->GetNext();
1279 // Only set the size/position of the constraint (if any)
1280 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1282 wxLayoutConstraints
*constr
= GetConstraints();
1287 constr
->left
.SetValue(x
);
1288 constr
->left
.SetDone(TRUE
);
1292 constr
->top
.SetValue(y
);
1293 constr
->top
.SetDone(TRUE
);
1297 constr
->width
.SetValue(w
);
1298 constr
->width
.SetDone(TRUE
);
1302 constr
->height
.SetValue(h
);
1303 constr
->height
.SetDone(TRUE
);
1308 void wxWindowBase::MoveConstraint(int x
, int y
)
1310 wxLayoutConstraints
*constr
= GetConstraints();
1315 constr
->left
.SetValue(x
);
1316 constr
->left
.SetDone(TRUE
);
1320 constr
->top
.SetValue(y
);
1321 constr
->top
.SetDone(TRUE
);
1326 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1328 wxLayoutConstraints
*constr
= GetConstraints();
1331 *w
= constr
->width
.GetValue();
1332 *h
= constr
->height
.GetValue();
1338 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1340 wxLayoutConstraints
*constr
= GetConstraints();
1343 *w
= constr
->width
.GetValue();
1344 *h
= constr
->height
.GetValue();
1347 GetClientSize(w
, h
);
1350 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1352 // don't do it for the dialogs/frames - they float independently of their
1354 if ( !IsTopLevel() )
1356 wxWindow
*parent
= GetParent();
1357 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1359 wxPoint
pt(parent
->GetClientAreaOrigin());
1367 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1369 wxLayoutConstraints
*constr
= GetConstraints();
1372 *x
= constr
->left
.GetValue();
1373 *y
= constr
->top
.GetValue();
1379 #endif // wxUSE_CONSTRAINTS
1381 // ----------------------------------------------------------------------------
1382 // do Update UI processing for child controls
1383 // ----------------------------------------------------------------------------
1385 // TODO: should this be implemented for the child window rather
1386 // than the parent? Then you can override it e.g. for wxCheckBox
1387 // to do the Right Thing rather than having to assume a fixed number
1388 // of control classes.
1389 void wxWindowBase::UpdateWindowUI()
1392 wxUpdateUIEvent
event(GetId());
1393 event
.m_eventObject
= this;
1395 if ( GetEventHandler()->ProcessEvent(event
) )
1397 if ( event
.GetSetEnabled() )
1398 Enable(event
.GetEnabled());
1400 if ( event
.GetSetText() )
1402 wxControl
*control
= wxDynamicCastThis(wxControl
);
1406 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1408 text
->SetValue(event
.GetText());
1410 #endif // wxUSE_TEXTCTRL
1411 control
->SetLabel(event
.GetText());
1416 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1419 if ( event
.GetSetChecked() )
1420 checkbox
->SetValue(event
.GetChecked());
1422 #endif // wxUSE_CHECKBOX
1425 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1428 if ( event
.GetSetChecked() )
1429 radiobtn
->SetValue(event
.GetChecked());
1431 #endif // wxUSE_RADIOBTN
1433 #endif // wxUSE_CONTROLS
1436 // ----------------------------------------------------------------------------
1437 // dialog units translations
1438 // ----------------------------------------------------------------------------
1440 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1442 int charWidth
= GetCharWidth();
1443 int charHeight
= GetCharHeight();
1444 wxPoint
pt2(-1, -1);
1446 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1448 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1453 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1455 int charWidth
= GetCharWidth();
1456 int charHeight
= GetCharHeight();
1457 wxPoint
pt2(-1, -1);
1459 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1461 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1466 // ----------------------------------------------------------------------------
1468 // ----------------------------------------------------------------------------
1470 // propagate the colour change event to the subwindows
1471 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1473 wxWindowList::Node
*node
= GetChildren().GetFirst();
1476 // Only propagate to non-top-level windows
1477 wxWindow
*win
= node
->GetData();
1478 if ( !win
->IsTopLevel() )
1480 wxSysColourChangedEvent event2
;
1481 event
.m_eventObject
= win
;
1482 win
->GetEventHandler()->ProcessEvent(event2
);
1485 node
= node
->GetNext();
1489 // the default action is to populate dialog with data when it's created
1490 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1492 TransferDataToWindow();
1495 // process Ctrl-Alt-mclick
1496 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1499 if ( event
.ControlDown() && event
.AltDown() )
1501 // don't translate these strings
1503 switch ( wxGetOsVersion() )
1505 case wxMOTIF_X
: port
= _T("Motif"); break;
1506 case wxMACINTOSH
: port
= _T("Mac"); break;
1507 case wxBEOS
: port
= _T("BeOS"); break;
1511 case wxGTK_BEOS
: port
= _T("GTK"); break;
1517 case wxWIN386
: port
= _T("MS Windows"); break;
1521 case wxMGL_OS2
: port
= _T("MGL"); break;
1523 case wxOS2_PM
: port
= _T("OS/2"); break;
1524 default: port
= _T("unknown"); break;
1527 wxMessageBox(wxString::Format(
1529 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2001 wxWindows team"
1538 _T("wxWindows information"),
1539 wxICON_INFORMATION
| wxOK
,
1543 #endif // wxUSE_MSGDLG
1549 // ----------------------------------------------------------------------------
1550 // list classes implementation
1551 // ----------------------------------------------------------------------------
1553 void wxWindowListNode::DeleteData()
1555 delete (wxWindow
*)GetData();
1558 // ----------------------------------------------------------------------------
1560 // ----------------------------------------------------------------------------
1562 wxBorder
wxWindowBase::GetBorder() const
1564 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1565 if ( border
== wxBORDER_DEFAULT
)
1567 border
= GetDefaultBorder();
1573 wxBorder
wxWindowBase::GetDefaultBorder() const
1575 return wxBORDER_NONE
;
1578 // ----------------------------------------------------------------------------
1580 // ----------------------------------------------------------------------------
1582 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1584 // here we just check if the point is inside the window or not
1586 // check the top and left border first
1587 bool outside
= x
< 0 || y
< 0;
1590 // check the right and bottom borders too
1591 wxSize size
= GetSize();
1592 outside
= x
>= size
.x
|| y
>= size
.y
;
1595 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;