1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
49 #include "wx/layout.h"
51 #endif // wxUSE_CONSTRAINTS
53 #if wxUSE_DRAG_AND_DROP
55 #endif // wxUSE_DRAG_AND_DROP
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 int wxWindowBase::ms_lastControlId
= -200;
75 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
82 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
83 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
84 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
87 EVT_HELP(-1, wxWindowBase::OnHelp
)
92 // ============================================================================
93 // implementation of the common functionality of the wxWindow class
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // the default initialization
101 void wxWindowBase::InitBase()
103 // no window yet, no parent nor children
104 m_parent
= (wxWindow
*)NULL
;
106 m_children
.DeleteContents( FALSE
); // don't auto delete node data
108 // no constraints on the minimal window size
114 // window is created enabled but it's not visible yet
118 // no client data (yet)
120 m_clientDataType
= wxClientData_None
;
122 // the default event handler is just this window
123 m_eventHandler
= this;
127 m_windowValidator
= (wxValidator
*) NULL
;
128 #endif // wxUSE_VALIDATORS
130 // use the system default colours
131 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
);
132 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
134 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
135 // so the font is *not* really set - but calls to SetFont() later won't do
136 // anything because m_font appears to be already set!
138 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
145 // an optimization for the event processing: checking this flag is much
146 // faster than using IsKindOf(CLASSINFO(wxWindow))
149 #if wxUSE_CONSTRAINTS
150 // no constraints whatsoever
151 m_constraints
= (wxLayoutConstraints
*) NULL
;
152 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
153 m_windowSizer
= (wxSizer
*) NULL
;
154 m_autoLayout
= FALSE
;
155 #endif // wxUSE_CONSTRAINTS
157 #if wxUSE_DRAG_AND_DROP
158 m_dropTarget
= (wxDropTarget
*)NULL
;
159 #endif // wxUSE_DRAG_AND_DROP
162 m_tooltip
= (wxToolTip
*)NULL
;
163 #endif // wxUSE_TOOLTIPS
166 m_caret
= (wxCaret
*)NULL
;
167 #endif // wxUSE_CARET
169 // Whether we're using the current theme for this window (wxGTK only for now)
170 m_themeEnabled
= FALSE
;
173 // common part of window creation process
174 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
176 const wxPoint
& WXUNUSED(pos
),
177 const wxSize
& WXUNUSED(size
),
179 const wxValidator
& validator
,
180 const wxString
& name
)
182 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
183 // member variables - check that it has been called (will catch the case
184 // when a new ctor is added which doesn't call InitWindow)
185 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
187 // generate a new id if the user doesn't care about it
188 m_windowId
= id
== -1 ? NewControlId() : id
;
191 SetWindowStyleFlag(style
);
195 SetValidator(validator
);
196 #endif // wxUSE_VALIDATORS
198 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
199 // have it too - like this it's possible to set it only in the top level
200 // dialog/frame and all children will inherit it by defult
201 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
203 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 wxWindowBase::~wxWindowBase()
216 // FIXME if these 2 cases result from programming errors in the user code
217 // we should probably assert here instead of silently fixing them
219 // Just in case the window has been Closed, but we're then deleting
220 // immediately: don't leave dangling pointers.
221 wxPendingDelete
.DeleteObject(this);
223 // Just in case we've loaded a top-level window via LoadNativeDialog but
224 // we weren't a dialog class
225 wxTopLevelWindows
.DeleteObject(this);
227 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
232 #endif // wxUSE_CARET
235 if ( m_windowValidator
)
236 delete m_windowValidator
;
237 #endif // wxUSE_VALIDATORS
239 // we only delete object data, not untyped
240 if ( m_clientDataType
== wxClientData_Object
)
241 delete m_clientObject
;
243 #if wxUSE_CONSTRAINTS
244 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
245 // at deleted windows as they delete themselves.
246 DeleteRelatedConstraints();
250 // This removes any dangling pointers to this window in other windows'
251 // constraintsInvolvedIn lists.
252 UnsetConstraints(m_constraints
);
253 delete m_constraints
;
254 m_constraints
= NULL
;
258 delete m_windowSizer
;
260 #endif // wxUSE_CONSTRAINTS
262 #if wxUSE_DRAG_AND_DROP
265 #endif // wxUSE_DRAG_AND_DROP
270 #endif // wxUSE_TOOLTIPS
273 bool wxWindowBase::Destroy()
280 bool wxWindowBase::Close(bool force
)
282 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
283 event
.SetEventObject(this);
284 #if WXWIN_COMPATIBILITY
285 event
.SetForce(force
);
286 #endif // WXWIN_COMPATIBILITY
287 event
.SetCanVeto(!force
);
289 // return FALSE if window wasn't closed because the application vetoed the
291 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
294 bool wxWindowBase::DestroyChildren()
296 wxWindowList::Node
*node
;
299 // we iterate until the list becomes empty
300 node
= GetChildren().GetFirst();
304 wxWindow
*child
= node
->GetData();
306 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
311 wxASSERT_MSG( !GetChildren().Find(child
),
312 wxT("child didn't remove itself using RemoveChild()") );
318 // ----------------------------------------------------------------------------
319 // size/position related methods
320 // ----------------------------------------------------------------------------
322 // centre the window with respect to its parent in either (or both) directions
323 void wxWindowBase::Centre(int direction
)
325 // the position/size of the parent window or of the entire screen
327 int widthParent
, heightParent
;
329 wxWindow
*parent
= NULL
;
331 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
333 // find the parent to centre this window on: it should be the
334 // immediate parent for the controls but the top level parent for the
335 // top level windows (like dialogs)
336 parent
= GetParent();
339 while ( parent
&& !parent
->IsTopLevel() )
341 parent
= parent
->GetParent();
345 // did we find the parent?
349 direction
|= wxCENTRE_ON_SCREEN
;
353 if ( direction
& wxCENTRE_ON_SCREEN
)
355 // centre with respect to the whole screen
356 wxDisplaySize(&widthParent
, &heightParent
);
362 // centre on the parent
363 parent
->GetSize(&widthParent
, &heightParent
);
365 // adjust to the parents position
366 posParent
= parent
->GetPosition();
370 // centre inside the parents client rectangle
371 parent
->GetClientSize(&widthParent
, &heightParent
);
376 GetSize(&width
, &height
);
381 if ( direction
& wxHORIZONTAL
)
382 xNew
= (widthParent
- width
)/2;
384 if ( direction
& wxVERTICAL
)
385 yNew
= (heightParent
- height
)/2;
390 // Base size of the visible dimensions of the display
391 // to take into account the taskbar
392 wxRect rect
= wxGetClientDisplayRect();
393 wxSize
size (rect
.width
,rect
.height
);
395 if (posParent
.x
>= 0) // if parent is on the main display
399 else if (xNew
+width
> size
.x
)
400 xNew
= size
.x
-width
-1;
402 if (posParent
.y
>= 0) // if parent is on the main display
404 if (yNew
+height
> size
.y
)
405 yNew
= size
.y
-height
-1;
407 // Make certain that the title bar is initially visible
408 // always, even if this would push the bottom of the
409 // dialog of the visible area of the display
414 // move the window to this position (keeping the old size but using
415 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
416 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
419 // fits the window around the children
420 void wxWindowBase::Fit()
422 if ( GetChildren().GetCount() > 0 )
424 wxSize size
= DoGetBestSize();
426 // for compatibility with the old versions and because it really looks
427 // slightly more pretty like this, add a pad
433 //else: do nothing if we have no children
436 // return the size best suited for the current window
437 wxSize
wxWindowBase::DoGetBestSize() const
439 if ( GetChildren().GetCount() > 0 )
441 // our minimal acceptable size is such that all our windows fit inside
445 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
447 node
= node
->GetNext() )
449 wxWindow
*win
= node
->GetData();
450 if ( win
->IsTopLevel()
452 || wxDynamicCast(win
, wxStatusBar
)
453 #endif // wxUSE_STATUSBAR
456 // dialogs and frames lie in different top level windows -
457 // don't deal with them here; as for the status bars, they
458 // don't lie in the client area at all
463 win
->GetPosition(&wx
, &wy
);
465 // if the window hadn't been positioned yet, assume that it is in
472 win
->GetSize(&ww
, &wh
);
473 if ( wx
+ ww
> maxX
)
475 if ( wy
+ wh
> maxY
)
479 return wxSize(maxX
, maxY
);
483 // for a generic window there is no natural best size - just use the
489 // by default the origin is not shifted
490 wxPoint
wxWindowBase::GetClientAreaOrigin() const
492 return wxPoint(0, 0);
495 // set the min/max size of the window
496 void wxWindowBase::SetSizeHints(int minW
, int minH
,
498 int WXUNUSED(incW
), int WXUNUSED(incH
))
506 // ----------------------------------------------------------------------------
507 // show/hide/enable/disable the window
508 // ----------------------------------------------------------------------------
510 bool wxWindowBase::Show(bool show
)
512 if ( show
!= m_isShown
)
524 bool wxWindowBase::Enable(bool enable
)
526 if ( enable
!= m_isEnabled
)
528 m_isEnabled
= enable
;
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 bool wxWindowBase::IsTopLevel() const
546 // ----------------------------------------------------------------------------
547 // reparenting the window
548 // ----------------------------------------------------------------------------
550 void wxWindowBase::AddChild(wxWindowBase
*child
)
552 wxCHECK_RET( child
, wxT("can't add a NULL child") );
554 // this should never happen and it will lead to a crash later if it does
555 // because RemoveChild() will remove only one node from the children list
556 // and the other(s) one(s) will be left with dangling pointers in them
557 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
559 GetChildren().Append(child
);
560 child
->SetParent(this);
563 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
565 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
567 GetChildren().DeleteObject(child
);
568 child
->SetParent((wxWindow
*)NULL
);
571 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
573 wxWindow
*oldParent
= GetParent();
574 if ( newParent
== oldParent
)
580 // unlink this window from the existing parent.
583 oldParent
->RemoveChild(this);
587 wxTopLevelWindows
.DeleteObject(this);
590 // add it to the new one
593 newParent
->AddChild(this);
597 wxTopLevelWindows
.Append(this);
603 // ----------------------------------------------------------------------------
604 // event handler stuff
605 // ----------------------------------------------------------------------------
607 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
609 handler
->SetNextHandler(GetEventHandler());
610 SetEventHandler(handler
);
613 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
615 wxEvtHandler
*handlerA
= GetEventHandler();
618 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
619 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
620 SetEventHandler(handlerB
);
624 handlerA
= (wxEvtHandler
*)NULL
;
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
637 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
640 m_backgroundColour
= colour
;
645 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
647 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
650 m_foregroundColour
= colour
;
655 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
657 // setting an invalid cursor is ok, it means that we don't have any special
659 if ( m_cursor
== cursor
)
670 bool wxWindowBase::SetFont(const wxFont
& font
)
672 // don't try to set invalid font, always fall back to the default
673 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
675 if ( fontOk
== m_font
)
687 void wxWindowBase::SetCaret(wxCaret
*caret
)
698 wxASSERT_MSG( m_caret
->GetWindow() == this,
699 wxT("caret should be created associated to this window") );
702 #endif // wxUSE_CARET
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 void wxWindowBase::SetValidator(const wxValidator
& validator
)
711 if ( m_windowValidator
)
712 delete m_windowValidator
;
714 m_windowValidator
= (wxValidator
*)validator
.Clone();
716 if ( m_windowValidator
)
717 m_windowValidator
->SetWindow(this) ;
719 #endif // wxUSE_VALIDATORS
721 // ----------------------------------------------------------------------------
722 // update region stuff
723 // ----------------------------------------------------------------------------
725 wxRect
wxWindowBase::GetUpdateClientRect() const
727 wxRegion rgnUpdate
= GetUpdateRegion();
728 rgnUpdate
.Intersect(GetClientRect());
729 wxRect rectUpdate
= rgnUpdate
.GetBox();
730 wxPoint ptOrigin
= GetClientAreaOrigin();
731 rectUpdate
.x
-= ptOrigin
.x
;
732 rectUpdate
.y
-= ptOrigin
.y
;
737 bool wxWindowBase::IsExposed(int x
, int y
) const
739 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
742 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
744 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
747 // ----------------------------------------------------------------------------
748 // find window by id or name
749 // ----------------------------------------------------------------------------
751 wxWindow
*wxWindowBase::FindWindow( long id
)
753 if ( id
== m_windowId
)
754 return (wxWindow
*)this;
756 wxWindowBase
*res
= (wxWindow
*)NULL
;
757 wxWindowList::Node
*node
;
758 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
760 wxWindowBase
*child
= node
->GetData();
761 res
= child
->FindWindow( id
);
764 return (wxWindow
*)res
;
767 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
769 if ( name
== m_windowName
)
770 return (wxWindow
*)this;
772 wxWindowBase
*res
= (wxWindow
*)NULL
;
773 wxWindowList::Node
*node
;
774 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
776 wxWindow
*child
= node
->GetData();
777 res
= child
->FindWindow(name
);
780 return (wxWindow
*)res
;
783 // ----------------------------------------------------------------------------
784 // dialog oriented functions
785 // ----------------------------------------------------------------------------
787 void wxWindowBase::MakeModal(bool modal
)
789 // Disable all other windows
792 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
795 wxWindow
*win
= node
->GetData();
799 node
= node
->GetNext();
804 bool wxWindowBase::Validate()
807 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
809 wxWindowList::Node
*node
;
810 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
812 wxWindowBase
*child
= node
->GetData();
813 wxValidator
*validator
= child
->GetValidator();
814 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
819 if ( recurse
&& !child
->Validate() )
824 #endif // wxUSE_VALIDATORS
829 bool wxWindowBase::TransferDataToWindow()
832 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
834 wxWindowList::Node
*node
;
835 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
837 wxWindowBase
*child
= node
->GetData();
838 wxValidator
*validator
= child
->GetValidator();
839 if ( validator
&& !validator
->TransferToWindow() )
841 wxLogWarning(_("Could not transfer data to window"));
842 wxLog::FlushActive();
849 if ( !child
->TransferDataToWindow() )
851 // warning already given
856 #endif // wxUSE_VALIDATORS
861 bool wxWindowBase::TransferDataFromWindow()
864 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
866 wxWindowList::Node
*node
;
867 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
869 wxWindow
*child
= node
->GetData();
870 wxValidator
*validator
= child
->GetValidator();
871 if ( validator
&& !validator
->TransferFromWindow() )
873 // nop warning here because the application is supposed to give
874 // one itself - we don't know here what might have gone wrongly
881 if ( !child
->TransferDataFromWindow() )
883 // warning already given
888 #endif // wxUSE_VALIDATORS
893 void wxWindowBase::InitDialog()
895 wxInitDialogEvent
event(GetId());
896 event
.SetEventObject( this );
897 GetEventHandler()->ProcessEvent(event
);
900 // ----------------------------------------------------------------------------
901 // context-sensitive help support
902 // ----------------------------------------------------------------------------
906 // associate this help text with this window
907 void wxWindowBase::SetHelpText(const wxString
& text
)
909 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
912 helpProvider
->AddHelp(this, text
);
916 // associate this help text with all windows with the same id as this
918 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
920 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
923 helpProvider
->AddHelp(GetId(), text
);
927 // get the help string associated with this window (may be empty)
928 wxString
wxWindowBase::GetHelpText() const
931 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
934 text
= helpProvider
->GetHelp(this);
940 // show help for this window
941 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
943 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
946 if ( helpProvider
->ShowHelp(this) )
948 // skip the event.Skip() below
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
964 void wxWindowBase::SetToolTip( const wxString
&tip
)
966 // don't create the new tooltip if we already have one
969 m_tooltip
->SetTip( tip
);
973 SetToolTip( new wxToolTip( tip
) );
976 // setting empty tooltip text does not remove the tooltip any more - use
977 // SetToolTip((wxToolTip *)NULL) for this
980 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
988 #endif // wxUSE_TOOLTIPS
990 // ----------------------------------------------------------------------------
991 // constraints and sizers
992 // ----------------------------------------------------------------------------
994 #if wxUSE_CONSTRAINTS
996 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1000 UnsetConstraints(m_constraints
);
1001 delete m_constraints
;
1003 m_constraints
= constraints
;
1004 if ( m_constraints
)
1006 // Make sure other windows know they're part of a 'meaningful relationship'
1007 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1008 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1009 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1010 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1011 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1012 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1013 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1014 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1015 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1016 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1017 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1018 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1019 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1020 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1021 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1022 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1026 // This removes any dangling pointers to this window in other windows'
1027 // constraintsInvolvedIn lists.
1028 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1032 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1033 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1034 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1035 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1036 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1037 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1038 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1039 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1040 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1041 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1042 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1043 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1044 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1045 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1046 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1047 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1051 // Back-pointer to other windows we're involved with, so if we delete this
1052 // window, we must delete any constraints we're involved with.
1053 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1055 if ( !m_constraintsInvolvedIn
)
1056 m_constraintsInvolvedIn
= new wxWindowList
;
1057 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1058 m_constraintsInvolvedIn
->Append(otherWin
);
1061 // REMOVE back-pointer to other windows we're involved with.
1062 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1064 if ( m_constraintsInvolvedIn
)
1065 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1068 // Reset any constraints that mention this window
1069 void wxWindowBase::DeleteRelatedConstraints()
1071 if ( m_constraintsInvolvedIn
)
1073 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1076 wxWindow
*win
= node
->GetData();
1077 wxLayoutConstraints
*constr
= win
->GetConstraints();
1079 // Reset any constraints involving this window
1082 constr
->left
.ResetIfWin(this);
1083 constr
->top
.ResetIfWin(this);
1084 constr
->right
.ResetIfWin(this);
1085 constr
->bottom
.ResetIfWin(this);
1086 constr
->width
.ResetIfWin(this);
1087 constr
->height
.ResetIfWin(this);
1088 constr
->centreX
.ResetIfWin(this);
1089 constr
->centreY
.ResetIfWin(this);
1092 wxWindowList::Node
*next
= node
->GetNext();
1097 delete m_constraintsInvolvedIn
;
1098 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1102 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1104 if (m_windowSizer
) delete m_windowSizer
;
1106 m_windowSizer
= sizer
;
1109 bool wxWindowBase::Layout()
1111 // If there is a sizer, use it instead of the constraints
1115 GetClientSize(&w
, &h
);
1117 GetSizer()->SetDimension( 0, 0, w
, h
);
1121 wxLayoutConstraints
*constr
= GetConstraints();
1122 bool wasOk
= constr
&& constr
->AreSatisfied();
1124 ResetConstraints(); // Mark all constraints as unevaluated
1126 // if we're a top level panel (i.e. our parent is frame/dialog), our
1127 // own constraints will never be satisfied any more unless we do it
1132 while ( noChanges
> 0 )
1134 constr
->SatisfyConstraints(this, &noChanges
);
1138 DoPhase(1); // Layout children
1139 DoPhase(2); // Layout grand children
1140 SetConstraintSizes(); // Recursively set the real window sizes
1147 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1148 // do a similar thing, but also impose their own 'constraints' and order the
1149 // evaluation differently.
1150 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1152 wxLayoutConstraints
*constr
= GetConstraints();
1155 return constr
->SatisfyConstraints(this, noChanges
);
1161 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1171 // Do a phase of evaluating child constraints
1172 bool wxWindowBase::DoPhase(int phase
)
1174 int noIterations
= 0;
1175 int maxIterations
= 500;
1178 wxWindowList succeeded
;
1179 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1183 wxWindowList::Node
*node
= GetChildren().GetFirst();
1186 wxWindow
*child
= node
->GetData();
1187 if ( !child
->IsTopLevel() )
1189 wxLayoutConstraints
*constr
= child
->GetConstraints();
1192 if ( !succeeded
.Find(child
) )
1194 int tempNoChanges
= 0;
1195 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1196 noChanges
+= tempNoChanges
;
1199 succeeded
.Append(child
);
1204 node
= node
->GetNext();
1213 void wxWindowBase::ResetConstraints()
1215 wxLayoutConstraints
*constr
= GetConstraints();
1218 constr
->left
.SetDone(FALSE
);
1219 constr
->top
.SetDone(FALSE
);
1220 constr
->right
.SetDone(FALSE
);
1221 constr
->bottom
.SetDone(FALSE
);
1222 constr
->width
.SetDone(FALSE
);
1223 constr
->height
.SetDone(FALSE
);
1224 constr
->centreX
.SetDone(FALSE
);
1225 constr
->centreY
.SetDone(FALSE
);
1228 wxWindowList::Node
*node
= GetChildren().GetFirst();
1231 wxWindow
*win
= node
->GetData();
1232 if ( !win
->IsTopLevel() )
1233 win
->ResetConstraints();
1234 node
= node
->GetNext();
1238 // Need to distinguish between setting the 'fake' size for windows and sizers,
1239 // and setting the real values.
1240 void wxWindowBase::SetConstraintSizes(bool recurse
)
1242 wxLayoutConstraints
*constr
= GetConstraints();
1243 if ( constr
&& constr
->AreSatisfied() )
1245 int x
= constr
->left
.GetValue();
1246 int y
= constr
->top
.GetValue();
1247 int w
= constr
->width
.GetValue();
1248 int h
= constr
->height
.GetValue();
1250 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1251 (constr
->height
.GetRelationship() != wxAsIs
) )
1253 SetSize(x
, y
, w
, h
);
1257 // If we don't want to resize this window, just move it...
1263 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1264 GetClassInfo()->GetClassName(),
1270 wxWindowList::Node
*node
= GetChildren().GetFirst();
1273 wxWindow
*win
= node
->GetData();
1274 if ( !win
->IsTopLevel() )
1275 win
->SetConstraintSizes();
1276 node
= node
->GetNext();
1281 // Only set the size/position of the constraint (if any)
1282 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1284 wxLayoutConstraints
*constr
= GetConstraints();
1289 constr
->left
.SetValue(x
);
1290 constr
->left
.SetDone(TRUE
);
1294 constr
->top
.SetValue(y
);
1295 constr
->top
.SetDone(TRUE
);
1299 constr
->width
.SetValue(w
);
1300 constr
->width
.SetDone(TRUE
);
1304 constr
->height
.SetValue(h
);
1305 constr
->height
.SetDone(TRUE
);
1310 void wxWindowBase::MoveConstraint(int x
, int y
)
1312 wxLayoutConstraints
*constr
= GetConstraints();
1317 constr
->left
.SetValue(x
);
1318 constr
->left
.SetDone(TRUE
);
1322 constr
->top
.SetValue(y
);
1323 constr
->top
.SetDone(TRUE
);
1328 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1330 wxLayoutConstraints
*constr
= GetConstraints();
1333 *w
= constr
->width
.GetValue();
1334 *h
= constr
->height
.GetValue();
1340 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1342 wxLayoutConstraints
*constr
= GetConstraints();
1345 *w
= constr
->width
.GetValue();
1346 *h
= constr
->height
.GetValue();
1349 GetClientSize(w
, h
);
1352 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1354 wxLayoutConstraints
*constr
= GetConstraints();
1357 *x
= constr
->left
.GetValue();
1358 *y
= constr
->top
.GetValue();
1364 #endif // wxUSE_CONSTRAINTS
1366 // ----------------------------------------------------------------------------
1367 // do Update UI processing for child controls
1368 // ----------------------------------------------------------------------------
1370 // TODO: should this be implemented for the child window rather
1371 // than the parent? Then you can override it e.g. for wxCheckBox
1372 // to do the Right Thing rather than having to assume a fixed number
1373 // of control classes.
1374 void wxWindowBase::UpdateWindowUI()
1377 wxUpdateUIEvent
event(GetId());
1378 event
.m_eventObject
= this;
1380 if ( GetEventHandler()->ProcessEvent(event
) )
1382 if ( event
.GetSetEnabled() )
1383 Enable(event
.GetEnabled());
1385 if ( event
.GetSetText() )
1387 wxControl
*control
= wxDynamicCastThis(wxControl
);
1391 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1393 text
->SetValue(event
.GetText());
1395 #endif // wxUSE_TEXTCTRL
1396 control
->SetLabel(event
.GetText());
1401 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1404 if ( event
.GetSetChecked() )
1405 checkbox
->SetValue(event
.GetChecked());
1407 #endif // wxUSE_CHECKBOX
1410 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1413 if ( event
.GetSetChecked() )
1414 radiobtn
->SetValue(event
.GetChecked());
1416 #endif // wxUSE_RADIOBTN
1418 #endif // wxUSE_CONTROLS
1421 // ----------------------------------------------------------------------------
1422 // dialog units translations
1423 // ----------------------------------------------------------------------------
1425 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1427 int charWidth
= GetCharWidth();
1428 int charHeight
= GetCharHeight();
1429 wxPoint
pt2(-1, -1);
1431 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1433 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1438 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1440 int charWidth
= GetCharWidth();
1441 int charHeight
= GetCharHeight();
1442 wxPoint
pt2(-1, -1);
1444 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1446 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1451 // ----------------------------------------------------------------------------
1453 // ----------------------------------------------------------------------------
1455 void wxWindowBase::DoSetClientObject( wxClientData
*data
)
1457 wxASSERT_MSG( m_clientDataType
!= wxClientData_Void
,
1458 wxT("can't have both object and void client data") );
1460 if ( m_clientObject
)
1461 delete m_clientObject
;
1463 m_clientObject
= data
;
1464 m_clientDataType
= wxClientData_Object
;
1467 wxClientData
*wxWindowBase::DoGetClientObject() const
1469 // it's not an error to call GetClientObject() on a window which doesn't
1470 // have client data at all - NULL will be returned
1471 wxASSERT_MSG( m_clientDataType
!= wxClientData_Void
,
1472 wxT("this window doesn't have object client data") );
1474 return m_clientObject
;
1477 void wxWindowBase::DoSetClientData( void *data
)
1479 wxASSERT_MSG( m_clientDataType
!= wxClientData_Object
,
1480 wxT("can't have both object and void client data") );
1482 m_clientData
= data
;
1483 m_clientDataType
= wxClientData_Void
;
1486 void *wxWindowBase::DoGetClientData() const
1488 // it's not an error to call GetClientData() on a window which doesn't have
1489 // client data at all - NULL will be returned
1490 wxASSERT_MSG( m_clientDataType
!= wxClientData_Object
,
1491 wxT("this window doesn't have void client data") );
1493 return m_clientData
;
1496 // ----------------------------------------------------------------------------
1498 // ----------------------------------------------------------------------------
1500 // propagate the colour change event to the subwindows
1501 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1503 wxWindowList::Node
*node
= GetChildren().GetFirst();
1506 // Only propagate to non-top-level windows
1507 wxWindow
*win
= node
->GetData();
1508 if ( !win
->IsTopLevel() )
1510 wxSysColourChangedEvent event2
;
1511 event
.m_eventObject
= win
;
1512 win
->GetEventHandler()->ProcessEvent(event2
);
1515 node
= node
->GetNext();
1519 // the default action is to populate dialog with data when it's created
1520 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1522 TransferDataToWindow();
1525 // process Ctrl-Alt-mclick
1526 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1529 if ( event
.ControlDown() && event
.AltDown() )
1531 // don't translate these strings
1533 switch ( wxGetOsVersion() )
1535 case wxMOTIF_X
: port
= _T("Motif"); break;
1536 case wxMACINTOSH
: port
= _T("Mac"); break;
1537 case wxBEOS
: port
= _T("BeOS"); break;
1541 case wxGTK_BEOS
: port
= _T("GTK"); break;
1547 case wxWIN386
: port
= _T("MS Windows"); break;
1551 case wxMGL_OS2
: port
= _T("MGL"); break;
1553 case wxOS2_PM
: port
= _T("OS/2"); break;
1554 default: port
= _T("unknown"); break;
1557 wxMessageBox(wxString::Format(
1559 " wxWindows Library (%s port)\nVersion %u.%u.%u, compiled at %s %s\n Copyright (c) 1995-2000 wxWindows team"
1568 _T("wxWindows information"),
1569 wxICON_INFORMATION
| wxOK
,
1573 #endif // wxUSE_MSGDLG
1579 // ----------------------------------------------------------------------------
1580 // list classes implementation
1581 // ----------------------------------------------------------------------------
1583 void wxWindowListNode::DeleteData()
1585 delete (wxWindow
*)GetData();
1588 // ----------------------------------------------------------------------------
1590 // ----------------------------------------------------------------------------
1592 wxBorder
wxWindowBase::GetBorder() const
1594 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1595 if ( border
== wxBORDER_DEFAULT
)
1597 border
= GetDefaultBorder();
1603 wxBorder
wxWindowBase::GetDefaultBorder() const
1605 return wxBORDER_NONE
;
1608 // ----------------------------------------------------------------------------
1610 // ----------------------------------------------------------------------------
1612 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1614 // here we just check if the point is inside the window or not
1616 // check the top and left border first
1617 bool outside
= x
< 0 || y
< 0;
1620 // check the right and bottom borders too
1621 wxSize size
= GetSize();
1622 outside
= x
>= size
.x
|| y
>= size
.y
;
1625 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;