1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/window.cpp
3 // Purpose: common (to all ports) wxWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "windowbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/textctrl.h"
42 #include "wx/settings.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
45 #include "wx/statusbr.h"
49 #include "wx/layout.h"
51 #endif // wxUSE_CONSTRAINTS
53 #if wxUSE_DRAG_AND_DROP
55 #endif // wxUSE_DRAG_AND_DROP
58 #include "wx/cshelp.h"
62 #include "wx/tooltip.h"
63 #endif // wxUSE_TOOLTIPS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 int wxWindowBase::ms_lastControlId
= -200;
75 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase
, wxEvtHandler
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(wxWindowBase
, wxEvtHandler
)
82 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged
)
83 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog
)
84 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick
)
87 EVT_HELP(-1, wxWindowBase::OnHelp
)
92 // ============================================================================
93 // implementation of the common functionality of the wxWindow class
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // the default initialization
101 void wxWindowBase::InitBase()
103 // no window yet, no parent nor children
104 m_parent
= (wxWindow
*)NULL
;
106 m_children
.DeleteContents( FALSE
); // don't auto delete node data
108 // no constraints on the minimal window size
114 // window is created enabled but it's not visible yet
118 // the default event handler is just this window
119 m_eventHandler
= this;
123 m_windowValidator
= (wxValidator
*) NULL
;
124 #endif // wxUSE_VALIDATORS
126 // use the system default colours
127 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
128 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
130 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
131 // so the font is *not* really set - but calls to SetFont() later won't do
132 // anything because m_font appears to be already set!
134 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
137 // the colours/fonts are default for now
146 // an optimization for the event processing: checking this flag is much
147 // faster than using IsKindOf(CLASSINFO(wxWindow))
150 #if wxUSE_CONSTRAINTS
151 // no constraints whatsoever
152 m_constraints
= (wxLayoutConstraints
*) NULL
;
153 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
154 m_windowSizer
= (wxSizer
*) NULL
;
155 m_autoLayout
= FALSE
;
156 #endif // wxUSE_CONSTRAINTS
158 #if wxUSE_DRAG_AND_DROP
159 m_dropTarget
= (wxDropTarget
*)NULL
;
160 #endif // wxUSE_DRAG_AND_DROP
163 m_tooltip
= (wxToolTip
*)NULL
;
164 #endif // wxUSE_TOOLTIPS
167 m_caret
= (wxCaret
*)NULL
;
168 #endif // wxUSE_CARET
170 // Whether we're using the current theme for this window (wxGTK only for now)
171 m_themeEnabled
= FALSE
;
174 // common part of window creation process
175 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
177 const wxPoint
& WXUNUSED(pos
),
178 const wxSize
& WXUNUSED(size
),
180 const wxValidator
& validator
,
181 const wxString
& name
)
183 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
184 // member variables - check that it has been called (will catch the case
185 // when a new ctor is added which doesn't call InitWindow)
186 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
188 // generate a new id if the user doesn't care about it
189 m_windowId
= id
== -1 ? NewControlId() : id
;
192 SetWindowStyleFlag(style
);
196 SetValidator(validator
);
197 #endif // wxUSE_VALIDATORS
199 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
200 // have it too - like this it's possible to set it only in the top level
201 // dialog/frame and all children will inherit it by defult
202 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
204 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
215 wxWindowBase::~wxWindowBase()
217 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
219 // FIXME if these 2 cases result from programming errors in the user code
220 // we should probably assert here instead of silently fixing them
222 // Just in case the window has been Closed, but we're then deleting
223 // immediately: don't leave dangling pointers.
224 wxPendingDelete
.DeleteObject(this);
226 // Just in case we've loaded a top-level window via LoadNativeDialog but
227 // we weren't a dialog class
228 wxTopLevelWindows
.DeleteObject(this);
230 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
235 #endif // wxUSE_CARET
238 if ( m_windowValidator
)
239 delete m_windowValidator
;
240 #endif // wxUSE_VALIDATORS
242 #if wxUSE_CONSTRAINTS
243 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
244 // at deleted windows as they delete themselves.
245 DeleteRelatedConstraints();
249 // This removes any dangling pointers to this window in other windows'
250 // constraintsInvolvedIn lists.
251 UnsetConstraints(m_constraints
);
252 delete m_constraints
;
253 m_constraints
= NULL
;
257 delete m_windowSizer
;
259 #endif // wxUSE_CONSTRAINTS
261 #if wxUSE_DRAG_AND_DROP
264 #endif // wxUSE_DRAG_AND_DROP
269 #endif // wxUSE_TOOLTIPS
271 // reset the dangling pointer our parent window may keep to us
272 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
274 m_parent
->SetDefaultItem(NULL
);
278 bool wxWindowBase::Destroy()
285 bool wxWindowBase::Close(bool force
)
287 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
288 event
.SetEventObject(this);
289 #if WXWIN_COMPATIBILITY
290 event
.SetForce(force
);
291 #endif // WXWIN_COMPATIBILITY
292 event
.SetCanVeto(!force
);
294 // return FALSE if window wasn't closed because the application vetoed the
296 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
299 bool wxWindowBase::DestroyChildren()
301 wxWindowList::Node
*node
;
304 // we iterate until the list becomes empty
305 node
= GetChildren().GetFirst();
309 wxWindow
*child
= node
->GetData();
311 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
316 wxASSERT_MSG( !GetChildren().Find(child
),
317 wxT("child didn't remove itself using RemoveChild()") );
323 // ----------------------------------------------------------------------------
324 // size/position related methods
325 // ----------------------------------------------------------------------------
327 // centre the window with respect to its parent in either (or both) directions
328 void wxWindowBase::Centre(int direction
)
330 // the position/size of the parent window or of the entire screen
332 int widthParent
, heightParent
;
334 wxWindow
*parent
= NULL
;
336 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
338 // find the parent to centre this window on: it should be the
339 // immediate parent for the controls but the top level parent for the
340 // top level windows (like dialogs)
341 parent
= GetParent();
344 while ( parent
&& !parent
->IsTopLevel() )
346 parent
= parent
->GetParent();
350 // did we find the parent?
354 direction
|= wxCENTRE_ON_SCREEN
;
358 if ( direction
& wxCENTRE_ON_SCREEN
)
360 // centre with respect to the whole screen
361 wxDisplaySize(&widthParent
, &heightParent
);
367 // centre on the parent
368 parent
->GetSize(&widthParent
, &heightParent
);
370 // adjust to the parents position
371 posParent
= parent
->GetPosition();
375 // centre inside the parents client rectangle
376 parent
->GetClientSize(&widthParent
, &heightParent
);
381 GetSize(&width
, &height
);
386 if ( direction
& wxHORIZONTAL
)
387 xNew
= (widthParent
- width
)/2;
389 if ( direction
& wxVERTICAL
)
390 yNew
= (heightParent
- height
)/2;
395 // Base size of the visible dimensions of the display
396 // to take into account the taskbar
397 wxRect rect
= wxGetClientDisplayRect();
398 wxSize
size (rect
.width
,rect
.height
);
400 if (posParent
.x
>= 0) // if parent is on the main display
404 else if (xNew
+width
> size
.x
)
405 xNew
= size
.x
-width
-1;
407 if (posParent
.y
>= 0) // if parent is on the main display
409 if (yNew
+height
> size
.y
)
410 yNew
= size
.y
-height
-1;
412 // Make certain that the title bar is initially visible
413 // always, even if this would push the bottom of the
414 // dialog of the visible area of the display
419 // move the window to this position (keeping the old size but using
420 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
421 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
424 // fits the window around the children
425 void wxWindowBase::Fit()
427 if ( GetChildren().GetCount() > 0 )
429 wxSize size
= DoGetBestSize();
431 // for compatibility with the old versions and because it really looks
432 // slightly more pretty like this, add a pad
438 //else: do nothing if we have no children
441 // return the size best suited for the current window
442 wxSize
wxWindowBase::DoGetBestSize() const
444 if ( GetChildren().GetCount() > 0 )
446 // our minimal acceptable size is such that all our windows fit inside
450 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
452 node
= node
->GetNext() )
454 wxWindow
*win
= node
->GetData();
455 if ( win
->IsTopLevel()
457 || wxDynamicCast(win
, wxStatusBar
)
458 #endif // wxUSE_STATUSBAR
461 // dialogs and frames lie in different top level windows -
462 // don't deal with them here; as for the status bars, they
463 // don't lie in the client area at all
468 win
->GetPosition(&wx
, &wy
);
470 // if the window hadn't been positioned yet, assume that it is in
477 win
->GetSize(&ww
, &wh
);
478 if ( wx
+ ww
> maxX
)
480 if ( wy
+ wh
> maxY
)
484 return wxSize(maxX
, maxY
);
488 // for a generic window there is no natural best size - just use the
494 // by default the origin is not shifted
495 wxPoint
wxWindowBase::GetClientAreaOrigin() const
497 return wxPoint(0, 0);
500 // set the min/max size of the window
501 void wxWindowBase::SetSizeHints(int minW
, int minH
,
503 int WXUNUSED(incW
), int WXUNUSED(incH
))
511 // ----------------------------------------------------------------------------
512 // show/hide/enable/disable the window
513 // ----------------------------------------------------------------------------
515 bool wxWindowBase::Show(bool show
)
517 if ( show
!= m_isShown
)
529 bool wxWindowBase::Enable(bool enable
)
531 if ( enable
!= m_isEnabled
)
533 m_isEnabled
= enable
;
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 bool wxWindowBase::IsTopLevel() const
551 // ----------------------------------------------------------------------------
552 // reparenting the window
553 // ----------------------------------------------------------------------------
555 void wxWindowBase::AddChild(wxWindowBase
*child
)
557 wxCHECK_RET( child
, wxT("can't add a NULL child") );
559 // this should never happen and it will lead to a crash later if it does
560 // because RemoveChild() will remove only one node from the children list
561 // and the other(s) one(s) will be left with dangling pointers in them
562 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
564 GetChildren().Append(child
);
565 child
->SetParent(this);
568 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
570 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
572 GetChildren().DeleteObject(child
);
573 child
->SetParent((wxWindow
*)NULL
);
576 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
578 wxWindow
*oldParent
= GetParent();
579 if ( newParent
== oldParent
)
585 // unlink this window from the existing parent.
588 oldParent
->RemoveChild(this);
592 wxTopLevelWindows
.DeleteObject(this);
595 // add it to the new one
598 newParent
->AddChild(this);
602 wxTopLevelWindows
.Append(this);
608 // ----------------------------------------------------------------------------
609 // event handler stuff
610 // ----------------------------------------------------------------------------
612 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
614 handler
->SetNextHandler(GetEventHandler());
615 SetEventHandler(handler
);
618 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
620 wxEvtHandler
*handlerA
= GetEventHandler();
623 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
624 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
625 SetEventHandler(handlerB
);
629 handlerA
= (wxEvtHandler
*)NULL
;
636 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
638 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
640 wxEvtHandler
*handlerPrev
= NULL
,
641 *handlerCur
= GetEventHandler();
644 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
646 if ( handlerCur
== handler
)
650 handlerPrev
->SetNextHandler(handlerNext
);
654 SetEventHandler(handlerNext
);
657 handler
->SetNextHandler(NULL
);
662 handlerPrev
= handlerCur
;
663 handlerCur
= handlerNext
;
666 wxFAIL_MSG( _T("where has the event handler gone?") );
671 // ----------------------------------------------------------------------------
673 // ----------------------------------------------------------------------------
675 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
677 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
680 m_backgroundColour
= colour
;
687 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
689 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
692 m_foregroundColour
= colour
;
699 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
701 // setting an invalid cursor is ok, it means that we don't have any special
703 if ( m_cursor
== cursor
)
714 bool wxWindowBase::SetFont(const wxFont
& font
)
716 // don't try to set invalid font, always fall back to the default
717 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
719 if ( fontOk
== m_font
)
733 void wxWindowBase::SetCaret(wxCaret
*caret
)
744 wxASSERT_MSG( m_caret
->GetWindow() == this,
745 wxT("caret should be created associated to this window") );
748 #endif // wxUSE_CARET
751 // ----------------------------------------------------------------------------
753 // ----------------------------------------------------------------------------
755 void wxWindowBase::SetValidator(const wxValidator
& validator
)
757 if ( m_windowValidator
)
758 delete m_windowValidator
;
760 m_windowValidator
= (wxValidator
*)validator
.Clone();
762 if ( m_windowValidator
)
763 m_windowValidator
->SetWindow(this) ;
765 #endif // wxUSE_VALIDATORS
767 // ----------------------------------------------------------------------------
768 // update region stuff
769 // ----------------------------------------------------------------------------
771 wxRect
wxWindowBase::GetUpdateClientRect() const
773 wxRegion rgnUpdate
= GetUpdateRegion();
774 rgnUpdate
.Intersect(GetClientRect());
775 wxRect rectUpdate
= rgnUpdate
.GetBox();
776 wxPoint ptOrigin
= GetClientAreaOrigin();
777 rectUpdate
.x
-= ptOrigin
.x
;
778 rectUpdate
.y
-= ptOrigin
.y
;
783 bool wxWindowBase::IsExposed(int x
, int y
) const
785 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
788 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
790 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
793 // ----------------------------------------------------------------------------
794 // find window by id or name
795 // ----------------------------------------------------------------------------
797 wxWindow
*wxWindowBase::FindWindow( long id
)
799 if ( id
== m_windowId
)
800 return (wxWindow
*)this;
802 wxWindowBase
*res
= (wxWindow
*)NULL
;
803 wxWindowList::Node
*node
;
804 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
806 wxWindowBase
*child
= node
->GetData();
807 res
= child
->FindWindow( id
);
810 return (wxWindow
*)res
;
813 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
815 if ( name
== m_windowName
)
816 return (wxWindow
*)this;
818 wxWindowBase
*res
= (wxWindow
*)NULL
;
819 wxWindowList::Node
*node
;
820 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
822 wxWindow
*child
= node
->GetData();
823 res
= child
->FindWindow(name
);
826 return (wxWindow
*)res
;
829 // ----------------------------------------------------------------------------
830 // dialog oriented functions
831 // ----------------------------------------------------------------------------
833 void wxWindowBase::MakeModal(bool modal
)
835 // Disable all other windows
838 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
841 wxWindow
*win
= node
->GetData();
845 node
= node
->GetNext();
850 bool wxWindowBase::Validate()
853 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
855 wxWindowList::Node
*node
;
856 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
858 wxWindowBase
*child
= node
->GetData();
859 wxValidator
*validator
= child
->GetValidator();
860 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
865 if ( recurse
&& !child
->Validate() )
870 #endif // wxUSE_VALIDATORS
875 bool wxWindowBase::TransferDataToWindow()
878 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
880 wxWindowList::Node
*node
;
881 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
883 wxWindowBase
*child
= node
->GetData();
884 wxValidator
*validator
= child
->GetValidator();
885 if ( validator
&& !validator
->TransferToWindow() )
887 wxLogWarning(_("Could not transfer data to window"));
888 wxLog::FlushActive();
895 if ( !child
->TransferDataToWindow() )
897 // warning already given
902 #endif // wxUSE_VALIDATORS
907 bool wxWindowBase::TransferDataFromWindow()
910 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
912 wxWindowList::Node
*node
;
913 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
915 wxWindow
*child
= node
->GetData();
916 wxValidator
*validator
= child
->GetValidator();
917 if ( validator
&& !validator
->TransferFromWindow() )
919 // nop warning here because the application is supposed to give
920 // one itself - we don't know here what might have gone wrongly
927 if ( !child
->TransferDataFromWindow() )
929 // warning already given
934 #endif // wxUSE_VALIDATORS
939 void wxWindowBase::InitDialog()
941 wxInitDialogEvent
event(GetId());
942 event
.SetEventObject( this );
943 GetEventHandler()->ProcessEvent(event
);
946 // ----------------------------------------------------------------------------
947 // context-sensitive help support
948 // ----------------------------------------------------------------------------
952 // associate this help text with this window
953 void wxWindowBase::SetHelpText(const wxString
& text
)
955 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
958 helpProvider
->AddHelp(this, text
);
962 // associate this help text with all windows with the same id as this
964 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
966 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
969 helpProvider
->AddHelp(GetId(), text
);
973 // get the help string associated with this window (may be empty)
974 wxString
wxWindowBase::GetHelpText() const
977 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
980 text
= helpProvider
->GetHelp(this);
986 // show help for this window
987 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
989 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
992 if ( helpProvider
->ShowHelp(this) )
994 // skip the event.Skip() below
1002 #endif // wxUSE_HELP
1004 // ----------------------------------------------------------------------------
1006 // ----------------------------------------------------------------------------
1010 void wxWindowBase::SetToolTip( const wxString
&tip
)
1012 // don't create the new tooltip if we already have one
1015 m_tooltip
->SetTip( tip
);
1019 SetToolTip( new wxToolTip( tip
) );
1022 // setting empty tooltip text does not remove the tooltip any more - use
1023 // SetToolTip((wxToolTip *)NULL) for this
1026 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1031 m_tooltip
= tooltip
;
1034 #endif // wxUSE_TOOLTIPS
1036 // ----------------------------------------------------------------------------
1037 // constraints and sizers
1038 // ----------------------------------------------------------------------------
1040 #if wxUSE_CONSTRAINTS
1042 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1044 if ( m_constraints
)
1046 UnsetConstraints(m_constraints
);
1047 delete m_constraints
;
1049 m_constraints
= constraints
;
1050 if ( m_constraints
)
1052 // Make sure other windows know they're part of a 'meaningful relationship'
1053 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1054 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1055 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1056 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1057 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1058 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1059 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1060 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1061 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1062 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1063 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1064 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1065 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1066 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1067 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1068 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1072 // This removes any dangling pointers to this window in other windows'
1073 // constraintsInvolvedIn lists.
1074 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1078 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1079 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1080 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1081 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1082 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1083 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1084 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1085 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1086 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1087 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1088 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1089 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1090 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1091 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1092 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1093 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1097 // Back-pointer to other windows we're involved with, so if we delete this
1098 // window, we must delete any constraints we're involved with.
1099 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1101 if ( !m_constraintsInvolvedIn
)
1102 m_constraintsInvolvedIn
= new wxWindowList
;
1103 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1104 m_constraintsInvolvedIn
->Append(otherWin
);
1107 // REMOVE back-pointer to other windows we're involved with.
1108 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1110 if ( m_constraintsInvolvedIn
)
1111 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1114 // Reset any constraints that mention this window
1115 void wxWindowBase::DeleteRelatedConstraints()
1117 if ( m_constraintsInvolvedIn
)
1119 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1122 wxWindow
*win
= node
->GetData();
1123 wxLayoutConstraints
*constr
= win
->GetConstraints();
1125 // Reset any constraints involving this window
1128 constr
->left
.ResetIfWin(this);
1129 constr
->top
.ResetIfWin(this);
1130 constr
->right
.ResetIfWin(this);
1131 constr
->bottom
.ResetIfWin(this);
1132 constr
->width
.ResetIfWin(this);
1133 constr
->height
.ResetIfWin(this);
1134 constr
->centreX
.ResetIfWin(this);
1135 constr
->centreY
.ResetIfWin(this);
1138 wxWindowList::Node
*next
= node
->GetNext();
1143 delete m_constraintsInvolvedIn
;
1144 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1148 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1150 if (m_windowSizer
) delete m_windowSizer
;
1152 m_windowSizer
= sizer
;
1155 bool wxWindowBase::Layout()
1157 // If there is a sizer, use it instead of the constraints
1161 GetClientSize(&w
, &h
);
1163 GetSizer()->SetDimension( 0, 0, w
, h
);
1164 #if defined __WXPM__
1170 wxLayoutConstraints
*constr
= GetConstraints();
1171 bool wasOk
= constr
&& constr
->AreSatisfied();
1173 ResetConstraints(); // Mark all constraints as unevaluated
1175 // if we're a top level panel (i.e. our parent is frame/dialog), our
1176 // own constraints will never be satisfied any more unless we do it
1181 while ( noChanges
> 0 )
1183 constr
->SatisfyConstraints(this, &noChanges
);
1187 DoPhase(1); // Layout children
1188 DoPhase(2); // Layout grand children
1189 SetConstraintSizes(); // Recursively set the real window sizes
1196 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1197 // do a similar thing, but also impose their own 'constraints' and order the
1198 // evaluation differently.
1199 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1201 wxLayoutConstraints
*constr
= GetConstraints();
1204 return constr
->SatisfyConstraints(this, noChanges
);
1210 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1220 // Do a phase of evaluating child constraints
1221 bool wxWindowBase::DoPhase(int phase
)
1223 int noIterations
= 0;
1224 int maxIterations
= 500;
1227 wxWindowList succeeded
;
1228 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1232 wxWindowList::Node
*node
= GetChildren().GetFirst();
1235 wxWindow
*child
= node
->GetData();
1236 if ( !child
->IsTopLevel() )
1238 wxLayoutConstraints
*constr
= child
->GetConstraints();
1241 if ( !succeeded
.Find(child
) )
1243 int tempNoChanges
= 0;
1244 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1245 noChanges
+= tempNoChanges
;
1248 succeeded
.Append(child
);
1253 node
= node
->GetNext();
1262 void wxWindowBase::ResetConstraints()
1264 wxLayoutConstraints
*constr
= GetConstraints();
1267 constr
->left
.SetDone(FALSE
);
1268 constr
->top
.SetDone(FALSE
);
1269 constr
->right
.SetDone(FALSE
);
1270 constr
->bottom
.SetDone(FALSE
);
1271 constr
->width
.SetDone(FALSE
);
1272 constr
->height
.SetDone(FALSE
);
1273 constr
->centreX
.SetDone(FALSE
);
1274 constr
->centreY
.SetDone(FALSE
);
1277 wxWindowList::Node
*node
= GetChildren().GetFirst();
1280 wxWindow
*win
= node
->GetData();
1281 if ( !win
->IsTopLevel() )
1282 win
->ResetConstraints();
1283 node
= node
->GetNext();
1287 // Need to distinguish between setting the 'fake' size for windows and sizers,
1288 // and setting the real values.
1289 void wxWindowBase::SetConstraintSizes(bool recurse
)
1291 wxLayoutConstraints
*constr
= GetConstraints();
1292 if ( constr
&& constr
->AreSatisfied() )
1294 int x
= constr
->left
.GetValue();
1295 int y
= constr
->top
.GetValue();
1296 int w
= constr
->width
.GetValue();
1297 int h
= constr
->height
.GetValue();
1299 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1300 (constr
->height
.GetRelationship() != wxAsIs
) )
1302 SetSize(x
, y
, w
, h
);
1306 // If we don't want to resize this window, just move it...
1312 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1313 GetClassInfo()->GetClassName(),
1319 wxWindowList::Node
*node
= GetChildren().GetFirst();
1322 wxWindow
*win
= node
->GetData();
1323 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1324 win
->SetConstraintSizes();
1325 node
= node
->GetNext();
1330 // Only set the size/position of the constraint (if any)
1331 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1333 wxLayoutConstraints
*constr
= GetConstraints();
1338 constr
->left
.SetValue(x
);
1339 constr
->left
.SetDone(TRUE
);
1343 constr
->top
.SetValue(y
);
1344 constr
->top
.SetDone(TRUE
);
1348 constr
->width
.SetValue(w
);
1349 constr
->width
.SetDone(TRUE
);
1353 constr
->height
.SetValue(h
);
1354 constr
->height
.SetDone(TRUE
);
1359 void wxWindowBase::MoveConstraint(int x
, int y
)
1361 wxLayoutConstraints
*constr
= GetConstraints();
1366 constr
->left
.SetValue(x
);
1367 constr
->left
.SetDone(TRUE
);
1371 constr
->top
.SetValue(y
);
1372 constr
->top
.SetDone(TRUE
);
1377 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1379 wxLayoutConstraints
*constr
= GetConstraints();
1382 *w
= constr
->width
.GetValue();
1383 *h
= constr
->height
.GetValue();
1389 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1391 wxLayoutConstraints
*constr
= GetConstraints();
1394 *w
= constr
->width
.GetValue();
1395 *h
= constr
->height
.GetValue();
1398 GetClientSize(w
, h
);
1401 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1403 // don't do it for the dialogs/frames - they float independently of their
1405 if ( !IsTopLevel() )
1407 wxWindow
*parent
= GetParent();
1408 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1410 wxPoint
pt(parent
->GetClientAreaOrigin());
1418 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1420 wxLayoutConstraints
*constr
= GetConstraints();
1423 *x
= constr
->left
.GetValue();
1424 *y
= constr
->top
.GetValue();
1430 #endif // wxUSE_CONSTRAINTS
1432 // ----------------------------------------------------------------------------
1433 // do Update UI processing for child controls
1434 // ----------------------------------------------------------------------------
1436 // TODO: should this be implemented for the child window rather
1437 // than the parent? Then you can override it e.g. for wxCheckBox
1438 // to do the Right Thing rather than having to assume a fixed number
1439 // of control classes.
1440 void wxWindowBase::UpdateWindowUI()
1443 wxUpdateUIEvent
event(GetId());
1444 event
.m_eventObject
= this;
1446 if ( GetEventHandler()->ProcessEvent(event
) )
1448 if ( event
.GetSetEnabled() )
1449 Enable(event
.GetEnabled());
1451 if ( event
.GetSetText() )
1453 wxControl
*control
= wxDynamicCastThis(wxControl
);
1457 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1459 text
->SetValue(event
.GetText());
1461 #endif // wxUSE_TEXTCTRL
1462 control
->SetLabel(event
.GetText());
1467 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1470 if ( event
.GetSetChecked() )
1471 checkbox
->SetValue(event
.GetChecked());
1473 #endif // wxUSE_CHECKBOX
1476 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1479 if ( event
.GetSetChecked() )
1480 radiobtn
->SetValue(event
.GetChecked());
1482 #endif // wxUSE_RADIOBTN
1484 #endif // wxUSE_CONTROLS
1487 // ----------------------------------------------------------------------------
1488 // dialog units translations
1489 // ----------------------------------------------------------------------------
1491 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1493 int charWidth
= GetCharWidth();
1494 int charHeight
= GetCharHeight();
1495 wxPoint
pt2(-1, -1);
1497 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1499 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1504 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1506 int charWidth
= GetCharWidth();
1507 int charHeight
= GetCharHeight();
1508 wxPoint
pt2(-1, -1);
1510 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1512 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1517 // ----------------------------------------------------------------------------
1519 // ----------------------------------------------------------------------------
1521 // propagate the colour change event to the subwindows
1522 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1524 wxWindowList::Node
*node
= GetChildren().GetFirst();
1527 // Only propagate to non-top-level windows
1528 wxWindow
*win
= node
->GetData();
1529 if ( !win
->IsTopLevel() )
1531 wxSysColourChangedEvent event2
;
1532 event
.m_eventObject
= win
;
1533 win
->GetEventHandler()->ProcessEvent(event2
);
1536 node
= node
->GetNext();
1540 // the default action is to populate dialog with data when it's created
1541 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1543 TransferDataToWindow();
1546 // process Ctrl-Alt-mclick
1547 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1550 if ( event
.ControlDown() && event
.AltDown() )
1552 // don't translate these strings
1555 #ifdef __WXUNIVERSAL__
1557 #endif // __WXUNIVERSAL__
1559 switch ( wxGetOsVersion() )
1561 case wxMOTIF_X
: port
= _T("Motif"); break;
1563 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1564 case wxBEOS
: port
= _T("BeOS"); break;
1568 case wxGTK_BEOS
: port
= _T("GTK"); break;
1574 case wxWIN386
: port
= _T("MS Windows"); break;
1578 case wxMGL_OS2
: port
= _T("MGL"); break;
1580 case wxOS2_PM
: port
= _T("OS/2"); break;
1581 default: port
= _T("unknown"); break;
1584 wxMessageBox(wxString::Format(
1586 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1600 _T("wxWindows information"),
1601 wxICON_INFORMATION
| wxOK
,
1605 #endif // wxUSE_MSGDLG
1611 // ----------------------------------------------------------------------------
1612 // list classes implementation
1613 // ----------------------------------------------------------------------------
1615 void wxWindowListNode::DeleteData()
1617 delete (wxWindow
*)GetData();
1620 // ----------------------------------------------------------------------------
1622 // ----------------------------------------------------------------------------
1624 wxBorder
wxWindowBase::GetBorder() const
1626 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1627 if ( border
== wxBORDER_DEFAULT
)
1629 border
= GetDefaultBorder();
1635 wxBorder
wxWindowBase::GetDefaultBorder() const
1637 return wxBORDER_NONE
;
1640 // ----------------------------------------------------------------------------
1642 // ----------------------------------------------------------------------------
1644 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1646 // here we just check if the point is inside the window or not
1648 // check the top and left border first
1649 bool outside
= x
< 0 || y
< 0;
1652 // check the right and bottom borders too
1653 wxSize size
= GetSize();
1654 outside
= x
>= size
.x
|| y
>= size
.y
;
1657 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1660 // ----------------------------------------------------------------------------
1662 // ----------------------------------------------------------------------------
1664 struct WXDLLEXPORT wxWindowNext
1668 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1670 void wxWindowBase::CaptureMouse()
1672 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1674 wxWindow
*winOld
= GetCapture();
1678 wxWindowNext
*item
= new wxWindowNext
;
1680 item
->next
= ms_winCaptureNext
;
1681 ms_winCaptureNext
= item
;
1683 //else: no mouse capture to save
1688 void wxWindowBase::ReleaseMouse()
1690 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1692 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1696 if ( ms_winCaptureNext
)
1698 ms_winCaptureNext
->win
->CaptureMouse();
1700 wxWindowNext
*item
= ms_winCaptureNext
;
1701 ms_winCaptureNext
= item
->next
;
1704 //else: stack is empty, no previous capture
1706 wxLogTrace(_T("mousecapture"),
1707 _T("After ReleaseMouse() mouse is captured by 0x%08x"),