1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/checkbox.h"
39 #include "wx/radiobut.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/statusbr.h"
48 #include "wx/layout.h"
50 #endif // wxUSE_CONSTRAINTS
52 #if wxUSE_DRAG_AND_DROP
54 #endif // wxUSE_DRAG_AND_DROP
57 #include "wx/cshelp.h"
61 #include "wx/tooltip.h"
62 #endif // wxUSE_TOOLTIPS
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 int wxWindowBase::ms_lastControlId
= -200;
74 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
81 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
82 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
83 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
86 EVT_HELP(-1, wxWindowBase::OnHelp
)
91 // ============================================================================
92 // implementation of the common functionality of the wxWindow class
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // the default initialization
100 void wxWindowBase::InitBase()
102 // no window yet, no parent nor children
103 m_parent
= (wxWindow
*)NULL
;
105 m_children
.DeleteContents( FALSE
); // don't auto delete node data
107 // no constraints on the minimal window size
113 // window is created enabled but it's not visible yet
117 // no client data (yet)
119 m_clientDataType
= ClientData_None
;
121 // the default event handler is just this window
122 m_eventHandler
= this;
126 m_windowValidator
= (wxValidator
*) NULL
;
127 #endif // wxUSE_VALIDATORS
129 // use the system default colours
130 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
);
131 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
133 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
134 // so the font is *not* really set - but calls to SetFont() later won't do
135 // anything because m_font appears to be already set!
137 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
144 // an optimization for the event processing: checking this flag is much
145 // faster than using IsKindOf(CLASSINFO(wxWindow))
148 #if wxUSE_CONSTRAINTS
149 // no constraints whatsoever
150 m_constraints
= (wxLayoutConstraints
*) NULL
;
151 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
152 m_windowSizer
= (wxSizer
*) NULL
;
153 m_autoLayout
= FALSE
;
154 #endif // wxUSE_CONSTRAINTS
156 #if wxUSE_DRAG_AND_DROP
157 m_dropTarget
= (wxDropTarget
*)NULL
;
158 #endif // wxUSE_DRAG_AND_DROP
161 m_tooltip
= (wxToolTip
*)NULL
;
162 #endif // wxUSE_TOOLTIPS
165 m_caret
= (wxCaret
*)NULL
;
166 #endif // wxUSE_CARET
168 // Whether we're using the current theme for this window (wxGTK only for now)
169 m_themeEnabled
= FALSE
;
172 // common part of window creation process
173 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
175 const wxPoint
& WXUNUSED(pos
),
176 const wxSize
& WXUNUSED(size
),
178 const wxValidator
& validator
,
179 const wxString
& name
)
181 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
182 // member variables - check that it has been called (will catch the case
183 // when a new ctor is added which doesn't call InitWindow)
184 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
186 // generate a new id if the user doesn't care about it
187 m_windowId
= id
== -1 ? NewControlId() : id
;
190 SetWindowStyleFlag(style
);
194 SetValidator(validator
);
195 #endif // wxUSE_VALIDATORS
197 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
198 // have it too - like this it's possible to set it only in the top level
199 // dialog/frame and all children will inherit it by defult
200 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
202 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
213 wxWindowBase::~wxWindowBase()
215 // FIXME if these 2 cases result from programming errors in the user code
216 // we should probably assert here instead of silently fixing them
218 // Just in case the window has been Closed, but we're then deleting
219 // immediately: don't leave dangling pointers.
220 wxPendingDelete
.DeleteObject(this);
222 // Just in case we've loaded a top-level window via LoadNativeDialog but
223 // we weren't a dialog class
224 wxTopLevelWindows
.DeleteObject(this);
226 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
228 // make sure that there are no dangling pointers left pointing to us
229 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
232 if ( panel
->GetLastFocus() == this )
234 panel
->SetLastFocus((wxWindow
*)NULL
);
241 #endif // wxUSE_CARET
244 if ( m_windowValidator
)
245 delete m_windowValidator
;
246 #endif // wxUSE_VALIDATORS
248 // we only delete object data, not untyped
249 if ( m_clientDataType
== ClientData_Object
)
250 delete m_clientObject
;
252 #if wxUSE_CONSTRAINTS
253 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
254 // at deleted windows as they delete themselves.
255 DeleteRelatedConstraints();
259 // This removes any dangling pointers to this window in other windows'
260 // constraintsInvolvedIn lists.
261 UnsetConstraints(m_constraints
);
262 delete m_constraints
;
263 m_constraints
= NULL
;
267 delete m_windowSizer
;
269 #endif // wxUSE_CONSTRAINTS
271 #if wxUSE_DRAG_AND_DROP
274 #endif // wxUSE_DRAG_AND_DROP
279 #endif // wxUSE_TOOLTIPS
282 bool wxWindowBase::Destroy()
289 bool wxWindowBase::Close(bool force
)
291 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
292 event
.SetEventObject(this);
293 #if WXWIN_COMPATIBILITY
294 event
.SetForce(force
);
295 #endif // WXWIN_COMPATIBILITY
296 event
.SetCanVeto(!force
);
298 // return FALSE if window wasn't closed because the application vetoed the
300 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
303 bool wxWindowBase::DestroyChildren()
305 wxWindowList::Node
*node
;
308 // we iterate until the list becomes empty
309 node
= GetChildren().GetFirst();
313 wxWindow
*child
= node
->GetData();
315 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
319 wxASSERT_MSG( !GetChildren().Find(child
),
320 wxT("child didn't remove itself using RemoveChild()") );
326 // ----------------------------------------------------------------------------
327 // size/position related methods
328 // ----------------------------------------------------------------------------
330 // centre the window with respect to its parent in either (or both) directions
331 void wxWindowBase::Centre(int direction
)
333 // the position/size of the parent window or of the entire screen
335 int widthParent
, heightParent
;
337 wxWindow
*parent
= NULL
;
339 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
341 // find the parent to centre this window on: it should be the
342 // immediate parent for the controls but the top level parent for the
343 // top level windows (like dialogs)
344 parent
= GetParent();
347 while ( parent
&& !parent
->IsTopLevel() )
349 parent
= parent
->GetParent();
353 // did we find the parent?
357 direction
|= wxCENTRE_ON_SCREEN
;
361 if ( direction
& wxCENTRE_ON_SCREEN
)
363 // centre with respect to the whole screen
364 wxDisplaySize(&widthParent
, &heightParent
);
370 // centre on the parent
371 parent
->GetSize(&widthParent
, &heightParent
);
373 // adjust to the parents position
374 posParent
= parent
->GetPosition();
378 // centre inside the parents client rectangle
379 parent
->GetClientSize(&widthParent
, &heightParent
);
384 GetSize(&width
, &height
);
389 if ( direction
& wxHORIZONTAL
)
390 xNew
= (widthParent
- width
)/2;
392 if ( direction
& wxVERTICAL
)
393 yNew
= (heightParent
- height
)/2;
398 // Base size of the visible dimensions of the display
399 // to take into account the taskbar
400 wxRect rect
= wxGetClientDisplayRect();
401 wxSize
size (rect
.width
,rect
.height
);
403 if (posParent
.x
>= 0) // if parent is on the main display
407 else if (xNew
+width
> size
.x
)
408 xNew
= size
.x
-width
-1;
410 if (posParent
.y
>= 0) // if parent is on the main display
412 if (yNew
+height
> size
.y
)
413 yNew
= size
.y
-height
-1;
415 // Make certain that the title bar is initially visible
416 // always, even if this would push the bottom of the
417 // dialog of the visible area of the display
422 // move the window to this position (keeping the old size but using
423 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
424 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
427 // fits the window around the children
428 void wxWindowBase::Fit()
430 if ( GetChildren().GetCount() > 0 )
432 wxSize size
= DoGetBestSize();
434 // for compatibility with the old versions and because it really looks
435 // slightly more pretty like this, add a pad
441 //else: do nothing if we have no children
444 // return the size best suited for the current window
445 wxSize
wxWindowBase::DoGetBestSize() const
447 if ( GetChildren().GetCount() > 0 )
449 // our minimal acceptable size is such that all our windows fit inside
453 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
455 node
= node
->GetNext() )
457 wxWindow
*win
= node
->GetData();
458 if ( win
->IsTopLevel() || wxDynamicCast(win
, wxStatusBar
) || !win
->IsShown())
460 // dialogs and frames lie in different top level windows -
461 // don't deal with them here; as for the status bars, they
462 // don't lie in the client area at all
467 win
->GetPosition(&wx
, &wy
);
469 // if the window hadn't been positioned yet, assume that it is in
476 win
->GetSize(&ww
, &wh
);
477 if ( wx
+ ww
> maxX
)
479 if ( wy
+ wh
> maxY
)
483 return wxSize(maxX
, maxY
);
487 // for a generic window there is no natural best size - just use the
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 testing
721 // ----------------------------------------------------------------------------
723 bool wxWindowBase::IsExposed(int x
, int y
) const
725 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
728 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
730 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
733 // ----------------------------------------------------------------------------
734 // find window by id or name
735 // ----------------------------------------------------------------------------
737 wxWindow
*wxWindowBase::FindWindow( long id
)
739 if ( id
== m_windowId
)
740 return (wxWindow
*)this;
742 wxWindowBase
*res
= (wxWindow
*)NULL
;
743 wxWindowList::Node
*node
;
744 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
746 wxWindowBase
*child
= node
->GetData();
747 res
= child
->FindWindow( id
);
750 return (wxWindow
*)res
;
753 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
755 if ( name
== m_windowName
)
756 return (wxWindow
*)this;
758 wxWindowBase
*res
= (wxWindow
*)NULL
;
759 wxWindowList::Node
*node
;
760 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
762 wxWindow
*child
= node
->GetData();
763 res
= child
->FindWindow(name
);
766 return (wxWindow
*)res
;
769 // ----------------------------------------------------------------------------
770 // dialog oriented functions
771 // ----------------------------------------------------------------------------
773 void wxWindowBase::MakeModal(bool modal
)
775 // Disable all other windows
778 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
781 wxWindow
*win
= node
->GetData();
785 node
= node
->GetNext();
790 bool wxWindowBase::Validate()
793 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
795 wxWindowList::Node
*node
;
796 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
798 wxWindowBase
*child
= node
->GetData();
799 wxValidator
*validator
= child
->GetValidator();
800 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
805 if ( recurse
&& !child
->Validate() )
810 #endif // wxUSE_VALIDATORS
815 bool wxWindowBase::TransferDataToWindow()
818 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
820 wxWindowList::Node
*node
;
821 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
823 wxWindowBase
*child
= node
->GetData();
824 wxValidator
*validator
= child
->GetValidator();
825 if ( validator
&& !validator
->TransferToWindow() )
827 wxLogWarning(_("Could not transfer data to window"));
828 wxLog::FlushActive();
835 if ( !child
->TransferDataToWindow() )
837 // warning already given
842 #endif // wxUSE_VALIDATORS
847 bool wxWindowBase::TransferDataFromWindow()
850 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
852 wxWindowList::Node
*node
;
853 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
855 wxWindow
*child
= node
->GetData();
856 wxValidator
*validator
= child
->GetValidator();
857 if ( validator
&& !validator
->TransferFromWindow() )
859 // nop warning here because the application is supposed to give
860 // one itself - we don't know here what might have gone wrongly
867 if ( !child
->TransferDataFromWindow() )
869 // warning already given
874 #endif // wxUSE_VALIDATORS
879 void wxWindowBase::InitDialog()
881 wxInitDialogEvent
event(GetId());
882 event
.SetEventObject( this );
883 GetEventHandler()->ProcessEvent(event
);
886 // ----------------------------------------------------------------------------
887 // context-sensitive help support
888 // ----------------------------------------------------------------------------
892 // associate this help text with this window
893 void wxWindowBase::SetHelpText(const wxString
& text
)
895 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
898 helpProvider
->AddHelp(this, text
);
902 // associate this help text with all windows with the same id as this
904 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
906 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
909 helpProvider
->AddHelp(GetId(), text
);
913 // get the help string associated with this window (may be empty)
914 wxString
wxWindowBase::GetHelpText() const
917 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
920 text
= helpProvider
->GetHelp(this);
926 // show help for this window
927 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
929 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
932 if ( helpProvider
->ShowHelp(this) )
934 // skip the event.Skip() below
944 // ----------------------------------------------------------------------------
946 // ----------------------------------------------------------------------------
950 void wxWindowBase::SetToolTip( const wxString
&tip
)
952 // don't create the new tooltip if we already have one
955 m_tooltip
->SetTip( tip
);
959 SetToolTip( new wxToolTip( tip
) );
962 // setting empty tooltip text does not remove the tooltip any more - use
963 // SetToolTip((wxToolTip *)NULL) for this
966 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
974 #endif // wxUSE_TOOLTIPS
976 // ----------------------------------------------------------------------------
977 // constraints and sizers
978 // ----------------------------------------------------------------------------
980 #if wxUSE_CONSTRAINTS
982 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
986 UnsetConstraints(m_constraints
);
987 delete m_constraints
;
989 m_constraints
= constraints
;
992 // Make sure other windows know they're part of a 'meaningful relationship'
993 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
994 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
995 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
996 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
997 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
998 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
999 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1000 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1001 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1002 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1003 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1004 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1005 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1006 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1007 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1008 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1012 // This removes any dangling pointers to this window in other windows'
1013 // constraintsInvolvedIn lists.
1014 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1018 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1019 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1020 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1021 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1022 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1023 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1024 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1025 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1026 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1027 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1028 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1029 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1030 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1031 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1032 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1033 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1037 // Back-pointer to other windows we're involved with, so if we delete this
1038 // window, we must delete any constraints we're involved with.
1039 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1041 if ( !m_constraintsInvolvedIn
)
1042 m_constraintsInvolvedIn
= new wxWindowList
;
1043 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1044 m_constraintsInvolvedIn
->Append(otherWin
);
1047 // REMOVE back-pointer to other windows we're involved with.
1048 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1050 if ( m_constraintsInvolvedIn
)
1051 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1054 // Reset any constraints that mention this window
1055 void wxWindowBase::DeleteRelatedConstraints()
1057 if ( m_constraintsInvolvedIn
)
1059 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1062 wxWindow
*win
= node
->GetData();
1063 wxLayoutConstraints
*constr
= win
->GetConstraints();
1065 // Reset any constraints involving this window
1068 constr
->left
.ResetIfWin(this);
1069 constr
->top
.ResetIfWin(this);
1070 constr
->right
.ResetIfWin(this);
1071 constr
->bottom
.ResetIfWin(this);
1072 constr
->width
.ResetIfWin(this);
1073 constr
->height
.ResetIfWin(this);
1074 constr
->centreX
.ResetIfWin(this);
1075 constr
->centreY
.ResetIfWin(this);
1078 wxWindowList::Node
*next
= node
->GetNext();
1083 delete m_constraintsInvolvedIn
;
1084 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1088 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1090 if (m_windowSizer
) delete m_windowSizer
;
1092 m_windowSizer
= sizer
;
1095 bool wxWindowBase::Layout()
1097 // If there is a sizer, use it instead of the constraints
1101 GetClientSize(&w
, &h
);
1103 GetSizer()->SetDimension( 0, 0, w
, h
);
1107 wxLayoutConstraints
*constr
= GetConstraints();
1108 bool wasOk
= constr
&& constr
->AreSatisfied();
1110 ResetConstraints(); // Mark all constraints as unevaluated
1112 // if we're a top level panel (i.e. our parent is frame/dialog), our
1113 // own constraints will never be satisfied any more unless we do it
1118 while ( noChanges
> 0 )
1120 constr
->SatisfyConstraints(this, &noChanges
);
1124 DoPhase(1); // Layout children
1125 DoPhase(2); // Layout grand children
1126 SetConstraintSizes(); // Recursively set the real window sizes
1133 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1134 // do a similar thing, but also impose their own 'constraints' and order the
1135 // evaluation differently.
1136 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1138 wxLayoutConstraints
*constr
= GetConstraints();
1141 return constr
->SatisfyConstraints(this, noChanges
);
1147 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1157 // Do a phase of evaluating child constraints
1158 bool wxWindowBase::DoPhase(int phase
)
1160 int noIterations
= 0;
1161 int maxIterations
= 500;
1164 wxWindowList succeeded
;
1165 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1169 wxWindowList::Node
*node
= GetChildren().GetFirst();
1172 wxWindow
*child
= node
->GetData();
1173 if ( !child
->IsTopLevel() )
1175 wxLayoutConstraints
*constr
= child
->GetConstraints();
1178 if ( !succeeded
.Find(child
) )
1180 int tempNoChanges
= 0;
1181 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1182 noChanges
+= tempNoChanges
;
1185 succeeded
.Append(child
);
1190 node
= node
->GetNext();
1199 void wxWindowBase::ResetConstraints()
1201 wxLayoutConstraints
*constr
= GetConstraints();
1204 constr
->left
.SetDone(FALSE
);
1205 constr
->top
.SetDone(FALSE
);
1206 constr
->right
.SetDone(FALSE
);
1207 constr
->bottom
.SetDone(FALSE
);
1208 constr
->width
.SetDone(FALSE
);
1209 constr
->height
.SetDone(FALSE
);
1210 constr
->centreX
.SetDone(FALSE
);
1211 constr
->centreY
.SetDone(FALSE
);
1214 wxWindowList::Node
*node
= GetChildren().GetFirst();
1217 wxWindow
*win
= node
->GetData();
1218 if ( !win
->IsTopLevel() )
1219 win
->ResetConstraints();
1220 node
= node
->GetNext();
1224 // Need to distinguish between setting the 'fake' size for windows and sizers,
1225 // and setting the real values.
1226 void wxWindowBase::SetConstraintSizes(bool recurse
)
1228 wxLayoutConstraints
*constr
= GetConstraints();
1229 if ( constr
&& constr
->AreSatisfied() )
1231 int x
= constr
->left
.GetValue();
1232 int y
= constr
->top
.GetValue();
1233 int w
= constr
->width
.GetValue();
1234 int h
= constr
->height
.GetValue();
1236 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1237 (constr
->height
.GetRelationship() != wxAsIs
) )
1239 SetSize(x
, y
, w
, h
);
1243 // If we don't want to resize this window, just move it...
1249 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1250 GetClassInfo()->GetClassName(),
1256 wxWindowList::Node
*node
= GetChildren().GetFirst();
1259 wxWindow
*win
= node
->GetData();
1260 if ( !win
->IsTopLevel() )
1261 win
->SetConstraintSizes();
1262 node
= node
->GetNext();
1267 // Only set the size/position of the constraint (if any)
1268 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1270 wxLayoutConstraints
*constr
= GetConstraints();
1275 constr
->left
.SetValue(x
);
1276 constr
->left
.SetDone(TRUE
);
1280 constr
->top
.SetValue(y
);
1281 constr
->top
.SetDone(TRUE
);
1285 constr
->width
.SetValue(w
);
1286 constr
->width
.SetDone(TRUE
);
1290 constr
->height
.SetValue(h
);
1291 constr
->height
.SetDone(TRUE
);
1296 void wxWindowBase::MoveConstraint(int x
, int y
)
1298 wxLayoutConstraints
*constr
= GetConstraints();
1303 constr
->left
.SetValue(x
);
1304 constr
->left
.SetDone(TRUE
);
1308 constr
->top
.SetValue(y
);
1309 constr
->top
.SetDone(TRUE
);
1314 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1316 wxLayoutConstraints
*constr
= GetConstraints();
1319 *w
= constr
->width
.GetValue();
1320 *h
= constr
->height
.GetValue();
1326 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1328 wxLayoutConstraints
*constr
= GetConstraints();
1331 *w
= constr
->width
.GetValue();
1332 *h
= constr
->height
.GetValue();
1335 GetClientSize(w
, h
);
1338 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1340 wxLayoutConstraints
*constr
= GetConstraints();
1343 *x
= constr
->left
.GetValue();
1344 *y
= constr
->top
.GetValue();
1350 #endif // wxUSE_CONSTRAINTS
1352 // ----------------------------------------------------------------------------
1353 // do Update UI processing for child controls
1354 // ----------------------------------------------------------------------------
1356 // TODO: should this be implemented for the child window rather
1357 // than the parent? Then you can override it e.g. for wxCheckBox
1358 // to do the Right Thing rather than having to assume a fixed number
1359 // of control classes.
1360 void wxWindowBase::UpdateWindowUI()
1362 wxUpdateUIEvent
event(GetId());
1363 event
.m_eventObject
= this;
1365 if ( GetEventHandler()->ProcessEvent(event
) )
1367 if ( event
.GetSetEnabled() )
1368 Enable(event
.GetEnabled());
1370 if ( event
.GetSetText() )
1372 wxControl
*control
= wxDynamicThisCast(this, wxControl
);
1375 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1377 text
->SetValue(event
.GetText());
1379 control
->SetLabel(event
.GetText());
1384 wxCheckBox
*checkbox
= wxDynamicThisCast(this, wxCheckBox
);
1387 if ( event
.GetSetChecked() )
1388 checkbox
->SetValue(event
.GetChecked());
1390 #endif // wxUSE_CHECKBOX
1393 wxRadioButton
*radiobtn
= wxDynamicThisCast(this, wxRadioButton
);
1396 if ( event
.GetSetChecked() )
1397 radiobtn
->SetValue(event
.GetChecked());
1399 #endif // wxUSE_RADIOBTN
1403 // ----------------------------------------------------------------------------
1404 // dialog units translations
1405 // ----------------------------------------------------------------------------
1407 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1409 int charWidth
= GetCharWidth();
1410 int charHeight
= GetCharHeight();
1411 wxPoint
pt2(-1, -1);
1413 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1415 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1420 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1422 int charWidth
= GetCharWidth();
1423 int charHeight
= GetCharHeight();
1424 wxPoint
pt2(-1, -1);
1426 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1428 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1433 // ----------------------------------------------------------------------------
1435 // ----------------------------------------------------------------------------
1437 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1439 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1440 wxT("can't have both object and void client data") );
1442 if ( m_clientObject
)
1443 delete m_clientObject
;
1445 m_clientObject
= data
;
1446 m_clientDataType
= ClientData_Object
;
1449 wxClientData
*wxWindowBase::DoGetClientObject() const
1451 // it's not an error to call GetClientObject() on a window which doesn't
1452 // have client data at all - NULL will be returned
1453 wxASSERT_MSG( m_clientDataType
!= ClientData_Void
,
1454 wxT("this window doesn't have object client data") );
1456 return m_clientObject
;
1459 void wxWindowBase::DoSetClientData( void *data
)
1461 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1462 wxT("can't have both object and void client data") );
1464 m_clientData
= data
;
1465 m_clientDataType
= ClientData_Void
;
1468 void *wxWindowBase::DoGetClientData() const
1470 // it's not an error to call GetClientData() on a window which doesn't have
1471 // client data at all - NULL will be returned
1472 wxASSERT_MSG( m_clientDataType
!= ClientData_Object
,
1473 wxT("this window doesn't have void client data") );
1475 return m_clientData
;
1478 // ----------------------------------------------------------------------------
1480 // ----------------------------------------------------------------------------
1482 // propagate the colour change event to the subwindows
1483 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1485 wxWindowList::Node
*node
= GetChildren().GetFirst();
1488 // Only propagate to non-top-level windows
1489 wxWindow
*win
= node
->GetData();
1490 if ( !win
->IsTopLevel() )
1492 wxSysColourChangedEvent event2
;
1493 event
.m_eventObject
= win
;
1494 win
->GetEventHandler()->ProcessEvent(event2
);
1497 node
= node
->GetNext();
1501 // the default action is to populate dialog with data when it's created
1502 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1504 TransferDataToWindow();
1507 // process Ctrl-Alt-mclick
1508 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;
1517 case wxMACINTOSH
: port
= _T("Mac"); break;
1518 case wxBEOS
: port
= _T("BeOS"); break;
1522 case wxGTK_BEOS
: port
= _T("GTK"); break;
1528 case wxWIN386
: port
= _T("MS Windows"); break;
1532 case wxMGL_OS2
: port
= _T("MGL"); break;
1534 case wxOS2_PM
: port
= _T("OS/2"); break;
1535 default: port
= _T("unknown"); break;
1538 wxMessageBox(wxString::Format(
1540 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1549 _T("wxWindows information"),
1550 wxICON_INFORMATION
| wxOK
,
1559 // ----------------------------------------------------------------------------
1560 // list classes implementation
1561 // ----------------------------------------------------------------------------
1563 void wxWindowListNode::DeleteData()
1565 delete (wxWindow
*)GetData();