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_containingSizer
= (wxSizer
*) NULL
;
156 m_autoLayout
= FALSE
;
157 #endif // wxUSE_CONSTRAINTS
159 #if wxUSE_DRAG_AND_DROP
160 m_dropTarget
= (wxDropTarget
*)NULL
;
161 #endif // wxUSE_DRAG_AND_DROP
164 m_tooltip
= (wxToolTip
*)NULL
;
165 #endif // wxUSE_TOOLTIPS
168 m_caret
= (wxCaret
*)NULL
;
169 #endif // wxUSE_CARET
172 m_hasCustomPalette
= FALSE
;
173 #endif // wxUSE_PALETTE
175 // Whether we're using the current theme for this window (wxGTK only for now)
176 m_themeEnabled
= FALSE
;
179 // common part of window creation process
180 bool wxWindowBase::CreateBase(wxWindowBase
*parent
,
182 const wxPoint
& WXUNUSED(pos
),
183 const wxSize
& WXUNUSED(size
),
185 const wxValidator
& validator
,
186 const wxString
& name
)
188 // m_isWindow is set to TRUE in wxWindowBase::Init() as well as many other
189 // member variables - check that it has been called (will catch the case
190 // when a new ctor is added which doesn't call InitWindow)
191 wxASSERT_MSG( m_isWindow
, wxT("Init() must have been called before!") );
193 // generate a new id if the user doesn't care about it
194 m_windowId
= id
== -1 ? NewControlId() : id
;
197 SetWindowStyleFlag(style
);
201 SetValidator(validator
);
202 #endif // wxUSE_VALIDATORS
204 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
205 // have it too - like this it's possible to set it only in the top level
206 // dialog/frame and all children will inherit it by defult
207 if ( parent
&& (parent
->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) )
209 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
220 wxWindowBase::~wxWindowBase()
222 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
224 // FIXME if these 2 cases result from programming errors in the user code
225 // we should probably assert here instead of silently fixing them
227 // Just in case the window has been Closed, but we're then deleting
228 // immediately: don't leave dangling pointers.
229 wxPendingDelete
.DeleteObject(this);
231 // Just in case we've loaded a top-level window via LoadNativeDialog but
232 // we weren't a dialog class
233 wxTopLevelWindows
.DeleteObject(this);
235 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
240 #endif // wxUSE_CARET
243 if ( m_windowValidator
)
244 delete m_windowValidator
;
245 #endif // wxUSE_VALIDATORS
247 #if wxUSE_CONSTRAINTS
248 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
249 // at deleted windows as they delete themselves.
250 DeleteRelatedConstraints();
254 // This removes any dangling pointers to this window in other windows'
255 // constraintsInvolvedIn lists.
256 UnsetConstraints(m_constraints
);
257 delete m_constraints
;
258 m_constraints
= NULL
;
261 if ( m_containingSizer
)
262 m_containingSizer
->Remove((wxWindow
*)this);
265 delete m_windowSizer
;
267 #endif // wxUSE_CONSTRAINTS
269 #if wxUSE_DRAG_AND_DROP
272 #endif // wxUSE_DRAG_AND_DROP
277 #endif // wxUSE_TOOLTIPS
279 // reset the dangling pointer our parent window may keep to us
280 if ( m_parent
&& m_parent
->GetDefaultItem() == this )
282 m_parent
->SetDefaultItem(NULL
);
286 bool wxWindowBase::Destroy()
293 bool wxWindowBase::Close(bool force
)
295 wxCloseEvent
event(wxEVT_CLOSE_WINDOW
, m_windowId
);
296 event
.SetEventObject(this);
297 #if WXWIN_COMPATIBILITY
298 event
.SetForce(force
);
299 #endif // WXWIN_COMPATIBILITY
300 event
.SetCanVeto(!force
);
302 // return FALSE if window wasn't closed because the application vetoed the
304 return GetEventHandler()->ProcessEvent(event
) && !event
.GetVeto();
307 bool wxWindowBase::DestroyChildren()
309 wxWindowList::Node
*node
;
312 // we iterate until the list becomes empty
313 node
= GetChildren().GetFirst();
317 wxWindow
*child
= node
->GetData();
319 wxASSERT_MSG( child
, wxT("children list contains empty nodes") );
324 wxASSERT_MSG( !GetChildren().Find(child
),
325 wxT("child didn't remove itself using RemoveChild()") );
331 // ----------------------------------------------------------------------------
332 // size/position related methods
333 // ----------------------------------------------------------------------------
335 // centre the window with respect to its parent in either (or both) directions
336 void wxWindowBase::Centre(int direction
)
338 // the position/size of the parent window or of the entire screen
340 int widthParent
, heightParent
;
342 wxWindow
*parent
= NULL
;
344 if ( !(direction
& wxCENTRE_ON_SCREEN
) )
346 // find the parent to centre this window on: it should be the
347 // immediate parent for the controls but the top level parent for the
348 // top level windows (like dialogs)
349 parent
= GetParent();
352 while ( parent
&& !parent
->IsTopLevel() )
354 parent
= parent
->GetParent();
358 // did we find the parent?
362 direction
|= wxCENTRE_ON_SCREEN
;
366 if ( direction
& wxCENTRE_ON_SCREEN
)
368 // centre with respect to the whole screen
369 wxDisplaySize(&widthParent
, &heightParent
);
375 // centre on the parent
376 parent
->GetSize(&widthParent
, &heightParent
);
378 // adjust to the parents position
379 posParent
= parent
->GetPosition();
383 // centre inside the parents client rectangle
384 parent
->GetClientSize(&widthParent
, &heightParent
);
389 GetSize(&width
, &height
);
394 if ( direction
& wxHORIZONTAL
)
395 xNew
= (widthParent
- width
)/2;
397 if ( direction
& wxVERTICAL
)
398 yNew
= (heightParent
- height
)/2;
403 // Base size of the visible dimensions of the display
404 // to take into account the taskbar
405 wxRect rect
= wxGetClientDisplayRect();
406 wxSize
size (rect
.width
,rect
.height
);
408 // NB: in wxMSW, negative position may not neccessary mean "out of screen",
409 // but it may mean that the window is placed on other than the main
410 // display. Therefore we only make sure centered window is on the main display
411 // if the parent is at least partially present here.
412 if (posParent
.x
+ widthParent
>= 0) // if parent is (partially) on the main display
416 else if (xNew
+width
> size
.x
)
417 xNew
= size
.x
-width
-1;
419 if (posParent
.y
+ heightParent
>= 0) // if parent is (partially) on the main display
421 if (yNew
+height
> size
.y
)
422 yNew
= size
.y
-height
-1;
424 // Make certain that the title bar is initially visible
425 // always, even if this would push the bottom of the
426 // dialog of the visible area of the display
431 // move the window to this position (keeping the old size but using
432 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
433 SetSize(xNew
, yNew
, width
, height
, wxSIZE_ALLOW_MINUS_ONE
);
436 // fits the window around the children
437 void wxWindowBase::Fit()
439 if ( GetChildren().GetCount() > 0 )
441 wxSize size
= DoGetBestSize();
443 // for compatibility with the old versions and because it really looks
444 // slightly more pretty like this, add a pad
450 //else: do nothing if we have no children
453 // return the size best suited for the current window
454 wxSize
wxWindowBase::DoGetBestSize() const
456 if ( GetChildren().GetCount() > 0 )
458 // our minimal acceptable size is such that all our windows fit inside
462 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
464 node
= node
->GetNext() )
466 wxWindow
*win
= node
->GetData();
467 if ( win
->IsTopLevel()
469 || wxDynamicCast(win
, wxStatusBar
)
470 #endif // wxUSE_STATUSBAR
473 // dialogs and frames lie in different top level windows -
474 // don't deal with them here; as for the status bars, they
475 // don't lie in the client area at all
480 win
->GetPosition(&wx
, &wy
);
482 // if the window hadn't been positioned yet, assume that it is in
489 win
->GetSize(&ww
, &wh
);
490 if ( wx
+ ww
> maxX
)
492 if ( wy
+ wh
> maxY
)
496 return wxSize(maxX
, maxY
);
500 // for a generic window there is no natural best size - just use the
506 // by default the origin is not shifted
507 wxPoint
wxWindowBase::GetClientAreaOrigin() const
509 return wxPoint(0, 0);
512 // set the min/max size of the window
513 void wxWindowBase::SetSizeHints(int minW
, int minH
,
515 int WXUNUSED(incW
), int WXUNUSED(incH
))
523 // ----------------------------------------------------------------------------
524 // show/hide/enable/disable the window
525 // ----------------------------------------------------------------------------
527 bool wxWindowBase::Show(bool show
)
529 if ( show
!= m_isShown
)
541 bool wxWindowBase::Enable(bool enable
)
543 if ( enable
!= m_isEnabled
)
545 m_isEnabled
= enable
;
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 bool wxWindowBase::IsTopLevel() const
563 // ----------------------------------------------------------------------------
564 // reparenting the window
565 // ----------------------------------------------------------------------------
567 void wxWindowBase::AddChild(wxWindowBase
*child
)
569 wxCHECK_RET( child
, wxT("can't add a NULL child") );
571 // this should never happen and it will lead to a crash later if it does
572 // because RemoveChild() will remove only one node from the children list
573 // and the other(s) one(s) will be left with dangling pointers in them
574 wxASSERT_MSG( !GetChildren().Find(child
), _T("AddChild() called twice") );
576 GetChildren().Append(child
);
577 child
->SetParent(this);
580 void wxWindowBase::RemoveChild(wxWindowBase
*child
)
582 wxCHECK_RET( child
, wxT("can't remove a NULL child") );
584 GetChildren().DeleteObject(child
);
585 child
->SetParent((wxWindow
*)NULL
);
588 bool wxWindowBase::Reparent(wxWindowBase
*newParent
)
590 wxWindow
*oldParent
= GetParent();
591 if ( newParent
== oldParent
)
597 // unlink this window from the existing parent.
600 oldParent
->RemoveChild(this);
604 wxTopLevelWindows
.DeleteObject(this);
607 // add it to the new one
610 newParent
->AddChild(this);
614 wxTopLevelWindows
.Append(this);
620 // ----------------------------------------------------------------------------
621 // event handler stuff
622 // ----------------------------------------------------------------------------
624 void wxWindowBase::PushEventHandler(wxEvtHandler
*handler
)
626 handler
->SetNextHandler(GetEventHandler());
627 SetEventHandler(handler
);
630 wxEvtHandler
*wxWindowBase::PopEventHandler(bool deleteHandler
)
632 wxEvtHandler
*handlerA
= GetEventHandler();
635 wxEvtHandler
*handlerB
= handlerA
->GetNextHandler();
636 handlerA
->SetNextHandler((wxEvtHandler
*)NULL
);
637 SetEventHandler(handlerB
);
641 handlerA
= (wxEvtHandler
*)NULL
;
648 bool wxWindowBase::RemoveEventHandler(wxEvtHandler
*handler
)
650 wxCHECK_MSG( handler
, FALSE
, _T("RemoveEventHandler(NULL) called") );
652 wxEvtHandler
*handlerPrev
= NULL
,
653 *handlerCur
= GetEventHandler();
656 wxEvtHandler
*handlerNext
= handlerCur
->GetNextHandler();
658 if ( handlerCur
== handler
)
662 handlerPrev
->SetNextHandler(handlerNext
);
666 SetEventHandler(handlerNext
);
669 handler
->SetNextHandler(NULL
);
674 handlerPrev
= handlerCur
;
675 handlerCur
= handlerNext
;
678 wxFAIL_MSG( _T("where has the event handler gone?") );
683 // ----------------------------------------------------------------------------
685 // ----------------------------------------------------------------------------
687 bool wxWindowBase::SetBackgroundColour( const wxColour
&colour
)
689 if ( !colour
.Ok() || (colour
== m_backgroundColour
) )
692 m_backgroundColour
= colour
;
699 bool wxWindowBase::SetForegroundColour( const wxColour
&colour
)
701 if ( !colour
.Ok() || (colour
== m_foregroundColour
) )
704 m_foregroundColour
= colour
;
711 bool wxWindowBase::SetCursor(const wxCursor
& cursor
)
713 // setting an invalid cursor is ok, it means that we don't have any special
715 if ( m_cursor
== cursor
)
726 bool wxWindowBase::SetFont(const wxFont
& font
)
728 // don't try to set invalid font, always fall back to the default
729 const wxFont
& fontOk
= font
.Ok() ? font
: *wxSWISS_FONT
;
731 if ( fontOk
== m_font
)
746 void wxWindowBase::SetPalette(const wxPalette
& pal
)
748 m_hasCustomPalette
= TRUE
;
751 // VZ: can anyone explain me what do we do here?
752 wxWindowDC
d((wxWindow
*) this);
756 wxWindow
*wxWindowBase::GetAncestorWithCustomPalette() const
758 wxWindow
*win
= (wxWindow
*)this;
759 while ( win
&& !win
->HasCustomPalette() )
761 win
= win
->GetParent();
767 #endif // wxUSE_PALETTE
770 void wxWindowBase::SetCaret(wxCaret
*caret
)
781 wxASSERT_MSG( m_caret
->GetWindow() == this,
782 wxT("caret should be created associated to this window") );
785 #endif // wxUSE_CARET
788 // ----------------------------------------------------------------------------
790 // ----------------------------------------------------------------------------
792 void wxWindowBase::SetValidator(const wxValidator
& validator
)
794 if ( m_windowValidator
)
795 delete m_windowValidator
;
797 m_windowValidator
= (wxValidator
*)validator
.Clone();
799 if ( m_windowValidator
)
800 m_windowValidator
->SetWindow(this) ;
802 #endif // wxUSE_VALIDATORS
804 // ----------------------------------------------------------------------------
805 // update region stuff
806 // ----------------------------------------------------------------------------
808 wxRect
wxWindowBase::GetUpdateClientRect() const
810 wxRegion rgnUpdate
= GetUpdateRegion();
811 rgnUpdate
.Intersect(GetClientRect());
812 wxRect rectUpdate
= rgnUpdate
.GetBox();
813 wxPoint ptOrigin
= GetClientAreaOrigin();
814 rectUpdate
.x
-= ptOrigin
.x
;
815 rectUpdate
.y
-= ptOrigin
.y
;
820 bool wxWindowBase::IsExposed(int x
, int y
) const
822 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
825 bool wxWindowBase::IsExposed(int x
, int y
, int w
, int h
) const
827 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
830 // ----------------------------------------------------------------------------
831 // find window by id or name
832 // ----------------------------------------------------------------------------
834 wxWindow
*wxWindowBase::FindWindow( long id
)
836 if ( id
== m_windowId
)
837 return (wxWindow
*)this;
839 wxWindowBase
*res
= (wxWindow
*)NULL
;
840 wxWindowList::Node
*node
;
841 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
843 wxWindowBase
*child
= node
->GetData();
844 res
= child
->FindWindow( id
);
847 return (wxWindow
*)res
;
850 wxWindow
*wxWindowBase::FindWindow( const wxString
& name
)
852 if ( name
== m_windowName
)
853 return (wxWindow
*)this;
855 wxWindowBase
*res
= (wxWindow
*)NULL
;
856 wxWindowList::Node
*node
;
857 for ( node
= m_children
.GetFirst(); node
&& !res
; node
= node
->GetNext() )
859 wxWindow
*child
= node
->GetData();
860 res
= child
->FindWindow(name
);
863 return (wxWindow
*)res
;
866 // ----------------------------------------------------------------------------
867 // dialog oriented functions
868 // ----------------------------------------------------------------------------
870 void wxWindowBase::MakeModal(bool modal
)
872 // Disable all other windows
875 wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
878 wxWindow
*win
= node
->GetData();
882 node
= node
->GetNext();
887 bool wxWindowBase::Validate()
890 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
892 wxWindowList::Node
*node
;
893 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
895 wxWindowBase
*child
= node
->GetData();
896 wxValidator
*validator
= child
->GetValidator();
897 if ( validator
&& !validator
->Validate((wxWindow
*)this) )
902 if ( recurse
&& !child
->Validate() )
907 #endif // wxUSE_VALIDATORS
912 bool wxWindowBase::TransferDataToWindow()
915 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
917 wxWindowList::Node
*node
;
918 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
920 wxWindowBase
*child
= node
->GetData();
921 wxValidator
*validator
= child
->GetValidator();
922 if ( validator
&& !validator
->TransferToWindow() )
924 wxLogWarning(_("Could not transfer data to window"));
925 wxLog::FlushActive();
932 if ( !child
->TransferDataToWindow() )
934 // warning already given
939 #endif // wxUSE_VALIDATORS
944 bool wxWindowBase::TransferDataFromWindow()
947 bool recurse
= (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY
) != 0;
949 wxWindowList::Node
*node
;
950 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
952 wxWindow
*child
= node
->GetData();
953 wxValidator
*validator
= child
->GetValidator();
954 if ( validator
&& !validator
->TransferFromWindow() )
956 // nop warning here because the application is supposed to give
957 // one itself - we don't know here what might have gone wrongly
964 if ( !child
->TransferDataFromWindow() )
966 // warning already given
971 #endif // wxUSE_VALIDATORS
976 void wxWindowBase::InitDialog()
978 wxInitDialogEvent
event(GetId());
979 event
.SetEventObject( this );
980 GetEventHandler()->ProcessEvent(event
);
983 // ----------------------------------------------------------------------------
984 // context-sensitive help support
985 // ----------------------------------------------------------------------------
989 // associate this help text with this window
990 void wxWindowBase::SetHelpText(const wxString
& text
)
992 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
995 helpProvider
->AddHelp(this, text
);
999 // associate this help text with all windows with the same id as this
1001 void wxWindowBase::SetHelpTextForId(const wxString
& text
)
1003 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1006 helpProvider
->AddHelp(GetId(), text
);
1010 // get the help string associated with this window (may be empty)
1011 wxString
wxWindowBase::GetHelpText() const
1014 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1017 text
= helpProvider
->GetHelp(this);
1023 // show help for this window
1024 void wxWindowBase::OnHelp(wxHelpEvent
& event
)
1026 wxHelpProvider
*helpProvider
= wxHelpProvider::Get();
1029 if ( helpProvider
->ShowHelp(this) )
1031 // skip the event.Skip() below
1039 #endif // wxUSE_HELP
1041 // ----------------------------------------------------------------------------
1042 // tooltipsroot.Replace("\\", "/");
1043 // ----------------------------------------------------------------------------
1047 void wxWindowBase::SetToolTip( const wxString
&tip
)
1049 // don't create the new tooltip if we already have one
1052 m_tooltip
->SetTip( tip
);
1056 SetToolTip( new wxToolTip( tip
) );
1059 // setting empty tooltip text does not remove the tooltip any more - use
1060 // SetToolTip((wxToolTip *)NULL) for this
1063 void wxWindowBase::DoSetToolTip(wxToolTip
*tooltip
)
1068 m_tooltip
= tooltip
;
1071 #endif // wxUSE_TOOLTIPS
1073 // ----------------------------------------------------------------------------
1074 // constraints and sizers
1075 // ----------------------------------------------------------------------------
1077 #if wxUSE_CONSTRAINTS
1079 void wxWindowBase::SetConstraints( wxLayoutConstraints
*constraints
)
1081 if ( m_constraints
)
1083 UnsetConstraints(m_constraints
);
1084 delete m_constraints
;
1086 m_constraints
= constraints
;
1087 if ( m_constraints
)
1089 // Make sure other windows know they're part of a 'meaningful relationship'
1090 if ( m_constraints
->left
.GetOtherWindow() && (m_constraints
->left
.GetOtherWindow() != this) )
1091 m_constraints
->left
.GetOtherWindow()->AddConstraintReference(this);
1092 if ( m_constraints
->top
.GetOtherWindow() && (m_constraints
->top
.GetOtherWindow() != this) )
1093 m_constraints
->top
.GetOtherWindow()->AddConstraintReference(this);
1094 if ( m_constraints
->right
.GetOtherWindow() && (m_constraints
->right
.GetOtherWindow() != this) )
1095 m_constraints
->right
.GetOtherWindow()->AddConstraintReference(this);
1096 if ( m_constraints
->bottom
.GetOtherWindow() && (m_constraints
->bottom
.GetOtherWindow() != this) )
1097 m_constraints
->bottom
.GetOtherWindow()->AddConstraintReference(this);
1098 if ( m_constraints
->width
.GetOtherWindow() && (m_constraints
->width
.GetOtherWindow() != this) )
1099 m_constraints
->width
.GetOtherWindow()->AddConstraintReference(this);
1100 if ( m_constraints
->height
.GetOtherWindow() && (m_constraints
->height
.GetOtherWindow() != this) )
1101 m_constraints
->height
.GetOtherWindow()->AddConstraintReference(this);
1102 if ( m_constraints
->centreX
.GetOtherWindow() && (m_constraints
->centreX
.GetOtherWindow() != this) )
1103 m_constraints
->centreX
.GetOtherWindow()->AddConstraintReference(this);
1104 if ( m_constraints
->centreY
.GetOtherWindow() && (m_constraints
->centreY
.GetOtherWindow() != this) )
1105 m_constraints
->centreY
.GetOtherWindow()->AddConstraintReference(this);
1109 // This removes any dangling pointers to this window in other windows'
1110 // constraintsInvolvedIn lists.
1111 void wxWindowBase::UnsetConstraints(wxLayoutConstraints
*c
)
1115 if ( c
->left
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1116 c
->left
.GetOtherWindow()->RemoveConstraintReference(this);
1117 if ( c
->top
.GetOtherWindow() && (c
->top
.GetOtherWindow() != this) )
1118 c
->top
.GetOtherWindow()->RemoveConstraintReference(this);
1119 if ( c
->right
.GetOtherWindow() && (c
->right
.GetOtherWindow() != this) )
1120 c
->right
.GetOtherWindow()->RemoveConstraintReference(this);
1121 if ( c
->bottom
.GetOtherWindow() && (c
->bottom
.GetOtherWindow() != this) )
1122 c
->bottom
.GetOtherWindow()->RemoveConstraintReference(this);
1123 if ( c
->width
.GetOtherWindow() && (c
->width
.GetOtherWindow() != this) )
1124 c
->width
.GetOtherWindow()->RemoveConstraintReference(this);
1125 if ( c
->height
.GetOtherWindow() && (c
->height
.GetOtherWindow() != this) )
1126 c
->height
.GetOtherWindow()->RemoveConstraintReference(this);
1127 if ( c
->centreX
.GetOtherWindow() && (c
->centreX
.GetOtherWindow() != this) )
1128 c
->centreX
.GetOtherWindow()->RemoveConstraintReference(this);
1129 if ( c
->centreY
.GetOtherWindow() && (c
->centreY
.GetOtherWindow() != this) )
1130 c
->centreY
.GetOtherWindow()->RemoveConstraintReference(this);
1134 // Back-pointer to other windows we're involved with, so if we delete this
1135 // window, we must delete any constraints we're involved with.
1136 void wxWindowBase::AddConstraintReference(wxWindowBase
*otherWin
)
1138 if ( !m_constraintsInvolvedIn
)
1139 m_constraintsInvolvedIn
= new wxWindowList
;
1140 if ( !m_constraintsInvolvedIn
->Find(otherWin
) )
1141 m_constraintsInvolvedIn
->Append(otherWin
);
1144 // REMOVE back-pointer to other windows we're involved with.
1145 void wxWindowBase::RemoveConstraintReference(wxWindowBase
*otherWin
)
1147 if ( m_constraintsInvolvedIn
)
1148 m_constraintsInvolvedIn
->DeleteObject(otherWin
);
1151 // Reset any constraints that mention this window
1152 void wxWindowBase::DeleteRelatedConstraints()
1154 if ( m_constraintsInvolvedIn
)
1156 wxWindowList::Node
*node
= m_constraintsInvolvedIn
->GetFirst();
1159 wxWindow
*win
= node
->GetData();
1160 wxLayoutConstraints
*constr
= win
->GetConstraints();
1162 // Reset any constraints involving this window
1165 constr
->left
.ResetIfWin(this);
1166 constr
->top
.ResetIfWin(this);
1167 constr
->right
.ResetIfWin(this);
1168 constr
->bottom
.ResetIfWin(this);
1169 constr
->width
.ResetIfWin(this);
1170 constr
->height
.ResetIfWin(this);
1171 constr
->centreX
.ResetIfWin(this);
1172 constr
->centreY
.ResetIfWin(this);
1175 wxWindowList::Node
*next
= node
->GetNext();
1180 delete m_constraintsInvolvedIn
;
1181 m_constraintsInvolvedIn
= (wxWindowList
*) NULL
;
1185 void wxWindowBase::SetSizer(wxSizer
*sizer
)
1187 if (m_windowSizer
) delete m_windowSizer
;
1189 m_windowSizer
= sizer
;
1192 bool wxWindowBase::Layout()
1194 // If there is a sizer, use it instead of the constraints
1198 GetClientSize(&w
, &h
);
1200 GetSizer()->SetDimension( 0, 0, w
, h
);
1204 wxLayoutConstraints
*constr
= GetConstraints();
1205 bool wasOk
= constr
&& constr
->AreSatisfied();
1207 ResetConstraints(); // Mark all constraints as unevaluated
1209 // if we're a top level panel (i.e. our parent is frame/dialog), our
1210 // own constraints will never be satisfied any more unless we do it
1215 while ( noChanges
> 0 )
1217 constr
->SatisfyConstraints(this, &noChanges
);
1221 DoPhase(1); // Layout children
1222 DoPhase(2); // Layout grand children
1223 SetConstraintSizes(); // Recursively set the real window sizes
1230 // Do a phase of evaluating constraints: the default behaviour. wxSizers may
1231 // do a similar thing, but also impose their own 'constraints' and order the
1232 // evaluation differently.
1233 bool wxWindowBase::LayoutPhase1(int *noChanges
)
1235 wxLayoutConstraints
*constr
= GetConstraints();
1238 return constr
->SatisfyConstraints(this, noChanges
);
1244 bool wxWindowBase::LayoutPhase2(int *noChanges
)
1254 // Do a phase of evaluating child constraints
1255 bool wxWindowBase::DoPhase(int phase
)
1257 int noIterations
= 0;
1258 int maxIterations
= 500;
1261 wxWindowList succeeded
;
1262 while ((noChanges
> 0) && (noIterations
< maxIterations
))
1266 wxWindowList::Node
*node
= GetChildren().GetFirst();
1269 wxWindow
*child
= node
->GetData();
1270 if ( !child
->IsTopLevel() )
1272 wxLayoutConstraints
*constr
= child
->GetConstraints();
1275 if ( !succeeded
.Find(child
) )
1277 int tempNoChanges
= 0;
1278 bool success
= ( (phase
== 1) ? child
->LayoutPhase1(&tempNoChanges
) : child
->LayoutPhase2(&tempNoChanges
) ) ;
1279 noChanges
+= tempNoChanges
;
1282 succeeded
.Append(child
);
1287 node
= node
->GetNext();
1296 void wxWindowBase::ResetConstraints()
1298 wxLayoutConstraints
*constr
= GetConstraints();
1301 constr
->left
.SetDone(FALSE
);
1302 constr
->top
.SetDone(FALSE
);
1303 constr
->right
.SetDone(FALSE
);
1304 constr
->bottom
.SetDone(FALSE
);
1305 constr
->width
.SetDone(FALSE
);
1306 constr
->height
.SetDone(FALSE
);
1307 constr
->centreX
.SetDone(FALSE
);
1308 constr
->centreY
.SetDone(FALSE
);
1311 wxWindowList::Node
*node
= GetChildren().GetFirst();
1314 wxWindow
*win
= node
->GetData();
1315 if ( !win
->IsTopLevel() )
1316 win
->ResetConstraints();
1317 node
= node
->GetNext();
1321 // Need to distinguish between setting the 'fake' size for windows and sizers,
1322 // and setting the real values.
1323 void wxWindowBase::SetConstraintSizes(bool recurse
)
1325 wxLayoutConstraints
*constr
= GetConstraints();
1326 if ( constr
&& constr
->AreSatisfied() )
1328 int x
= constr
->left
.GetValue();
1329 int y
= constr
->top
.GetValue();
1330 int w
= constr
->width
.GetValue();
1331 int h
= constr
->height
.GetValue();
1333 if ( (constr
->width
.GetRelationship() != wxAsIs
) ||
1334 (constr
->height
.GetRelationship() != wxAsIs
) )
1336 SetSize(x
, y
, w
, h
);
1340 // If we don't want to resize this window, just move it...
1346 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1347 GetClassInfo()->GetClassName(),
1353 wxWindowList::Node
*node
= GetChildren().GetFirst();
1356 wxWindow
*win
= node
->GetData();
1357 if ( !win
->IsTopLevel() && win
->GetConstraints() )
1358 win
->SetConstraintSizes();
1359 node
= node
->GetNext();
1364 // Only set the size/position of the constraint (if any)
1365 void wxWindowBase::SetSizeConstraint(int x
, int y
, int w
, int h
)
1367 wxLayoutConstraints
*constr
= GetConstraints();
1372 constr
->left
.SetValue(x
);
1373 constr
->left
.SetDone(TRUE
);
1377 constr
->top
.SetValue(y
);
1378 constr
->top
.SetDone(TRUE
);
1382 constr
->width
.SetValue(w
);
1383 constr
->width
.SetDone(TRUE
);
1387 constr
->height
.SetValue(h
);
1388 constr
->height
.SetDone(TRUE
);
1393 void wxWindowBase::MoveConstraint(int x
, int y
)
1395 wxLayoutConstraints
*constr
= GetConstraints();
1400 constr
->left
.SetValue(x
);
1401 constr
->left
.SetDone(TRUE
);
1405 constr
->top
.SetValue(y
);
1406 constr
->top
.SetDone(TRUE
);
1411 void wxWindowBase::GetSizeConstraint(int *w
, int *h
) const
1413 wxLayoutConstraints
*constr
= GetConstraints();
1416 *w
= constr
->width
.GetValue();
1417 *h
= constr
->height
.GetValue();
1423 void wxWindowBase::GetClientSizeConstraint(int *w
, int *h
) const
1425 wxLayoutConstraints
*constr
= GetConstraints();
1428 *w
= constr
->width
.GetValue();
1429 *h
= constr
->height
.GetValue();
1432 GetClientSize(w
, h
);
1435 void wxWindowBase::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
1437 // don't do it for the dialogs/frames - they float independently of their
1439 if ( !IsTopLevel() )
1441 wxWindow
*parent
= GetParent();
1442 if ( !(sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) && parent
)
1444 wxPoint
pt(parent
->GetClientAreaOrigin());
1452 void wxWindowBase::GetPositionConstraint(int *x
, int *y
) const
1454 wxLayoutConstraints
*constr
= GetConstraints();
1457 *x
= constr
->left
.GetValue();
1458 *y
= constr
->top
.GetValue();
1464 #endif // wxUSE_CONSTRAINTS
1466 // ----------------------------------------------------------------------------
1467 // do Update UI processing for child controls
1468 // ----------------------------------------------------------------------------
1470 // TODO: should this be implemented for the child window rather
1471 // than the parent? Then you can override it e.g. for wxCheckBox
1472 // to do the Right Thing rather than having to assume a fixed number
1473 // of control classes.
1474 void wxWindowBase::UpdateWindowUI()
1477 wxUpdateUIEvent
event(GetId());
1478 event
.m_eventObject
= this;
1480 if ( GetEventHandler()->ProcessEvent(event
) )
1482 if ( event
.GetSetEnabled() )
1483 Enable(event
.GetEnabled());
1485 if ( event
.GetSetText() )
1487 wxControl
*control
= wxDynamicCastThis(wxControl
);
1491 wxTextCtrl
*text
= wxDynamicCast(control
, wxTextCtrl
);
1493 text
->SetValue(event
.GetText());
1495 #endif // wxUSE_TEXTCTRL
1496 control
->SetLabel(event
.GetText());
1501 wxCheckBox
*checkbox
= wxDynamicCastThis(wxCheckBox
);
1504 if ( event
.GetSetChecked() )
1505 checkbox
->SetValue(event
.GetChecked());
1507 #endif // wxUSE_CHECKBOX
1510 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
1513 if ( event
.GetSetChecked() )
1514 radiobtn
->SetValue(event
.GetChecked());
1516 #endif // wxUSE_RADIOBTN
1518 #endif // wxUSE_CONTROLS
1521 // ----------------------------------------------------------------------------
1522 // dialog units translations
1523 // ----------------------------------------------------------------------------
1525 wxPoint
wxWindowBase::ConvertPixelsToDialog(const wxPoint
& pt
)
1527 int charWidth
= GetCharWidth();
1528 int charHeight
= GetCharHeight();
1529 wxPoint
pt2(-1, -1);
1531 pt2
.x
= (int) ((pt
.x
* 4) / charWidth
) ;
1533 pt2
.y
= (int) ((pt
.y
* 8) / charHeight
) ;
1538 wxPoint
wxWindowBase::ConvertDialogToPixels(const wxPoint
& pt
)
1540 int charWidth
= GetCharWidth();
1541 int charHeight
= GetCharHeight();
1542 wxPoint
pt2(-1, -1);
1544 pt2
.x
= (int) ((pt
.x
* charWidth
) / 4) ;
1546 pt2
.y
= (int) ((pt
.y
* charHeight
) / 8) ;
1551 // ----------------------------------------------------------------------------
1553 // ----------------------------------------------------------------------------
1555 // propagate the colour change event to the subwindows
1556 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1558 wxWindowList::Node
*node
= GetChildren().GetFirst();
1561 // Only propagate to non-top-level windows
1562 wxWindow
*win
= node
->GetData();
1563 if ( !win
->IsTopLevel() )
1565 wxSysColourChangedEvent event2
;
1566 event
.m_eventObject
= win
;
1567 win
->GetEventHandler()->ProcessEvent(event2
);
1570 node
= node
->GetNext();
1574 // the default action is to populate dialog with data when it's created
1575 void wxWindowBase::OnInitDialog( wxInitDialogEvent
&WXUNUSED(event
) )
1577 TransferDataToWindow();
1580 // process Ctrl-Alt-mclick
1581 void wxWindowBase::OnMiddleClick( wxMouseEvent
& event
)
1584 if ( event
.ControlDown() && event
.AltDown() )
1586 // don't translate these strings
1589 #ifdef __WXUNIVERSAL__
1591 #endif // __WXUNIVERSAL__
1593 switch ( wxGetOsVersion() )
1595 case wxMOTIF_X
: port
= _T("Motif"); break;
1597 case wxMAC_DARWIN
: port
= _T("Mac"); break;
1598 case wxBEOS
: port
= _T("BeOS"); break;
1602 case wxGTK_BEOS
: port
= _T("GTK"); break;
1608 case wxWIN386
: port
= _T("MS Windows"); break;
1612 case wxMGL_OS2
: port
= _T("MGL"); break;
1614 case wxOS2_PM
: port
= _T("OS/2"); break;
1615 default: port
= _T("unknown"); break;
1618 wxMessageBox(wxString::Format(
1620 " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team"
1634 _T("wxWindows information"),
1635 wxICON_INFORMATION
| wxOK
,
1639 #endif // wxUSE_MSGDLG
1645 // ----------------------------------------------------------------------------
1646 // list classes implementation
1647 // ----------------------------------------------------------------------------
1649 void wxWindowListNode::DeleteData()
1651 delete (wxWindow
*)GetData();
1654 // ----------------------------------------------------------------------------
1656 // ----------------------------------------------------------------------------
1658 wxBorder
wxWindowBase::GetBorder() const
1660 wxBorder border
= (wxBorder
)(m_windowStyle
& wxBORDER_MASK
);
1661 if ( border
== wxBORDER_DEFAULT
)
1663 border
= GetDefaultBorder();
1669 wxBorder
wxWindowBase::GetDefaultBorder() const
1671 return wxBORDER_NONE
;
1674 // ----------------------------------------------------------------------------
1676 // ----------------------------------------------------------------------------
1678 wxHitTest
wxWindowBase::DoHitTest(wxCoord x
, wxCoord y
) const
1680 // here we just check if the point is inside the window or not
1682 // check the top and left border first
1683 bool outside
= x
< 0 || y
< 0;
1686 // check the right and bottom borders too
1687 wxSize size
= GetSize();
1688 outside
= x
>= size
.x
|| y
>= size
.y
;
1691 return outside
? wxHT_WINDOW_OUTSIDE
: wxHT_WINDOW_INSIDE
;
1694 // ----------------------------------------------------------------------------
1696 // ----------------------------------------------------------------------------
1698 struct WXDLLEXPORT wxWindowNext
1702 } *wxWindowBase::ms_winCaptureNext
= NULL
;
1704 void wxWindowBase::CaptureMouse()
1706 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(0x%08x)"), this);
1708 wxWindow
*winOld
= GetCapture();
1711 ((wxWindowBase
*) winOld
)->DoReleaseMouse();
1714 wxWindowNext
*item
= new wxWindowNext
;
1716 item
->next
= ms_winCaptureNext
;
1717 ms_winCaptureNext
= item
;
1719 //else: no mouse capture to save
1724 void wxWindowBase::ReleaseMouse()
1726 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(0x%08x)"), this);
1728 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") )
1732 if ( ms_winCaptureNext
)
1734 ((wxWindowBase
*)ms_winCaptureNext
->win
)->DoCaptureMouse();
1736 wxWindowNext
*item
= ms_winCaptureNext
;
1737 ms_winCaptureNext
= item
->next
;
1740 //else: stack is empty, no previous capture
1742 wxLogTrace(_T("mousecapture"),
1743 _T("After ReleaseMouse() mouse is captured by 0x%08x"),