From 7162312f781940ec9390b7f97ce539cc241ea9f6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 11 May 2000 11:05:04 +0000 Subject: [PATCH] a fix for Centre() to always use the top level parent git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7381 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/wincmn.cpp | 52 ++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 2cd6afe038..6252fbd6d3 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -321,13 +321,27 @@ bool wxWindowBase::DestroyChildren() // centre the window with respect to its parent in either (or both) directions void wxWindowBase::Centre(int direction) { + // the position/size of the parent window or of the entire screen + wxPoint posParent; int widthParent, heightParent; - wxWindow *parent = GetParent(); - if ( !parent ) + wxWindow *parent = NULL; + + if ( !(direction & wxCENTRE_ON_SCREEN) ) { - // no other choice - direction |= wxCENTRE_ON_SCREEN; + // find the top level parent to centre this window on + parent = GetParent(); + while ( parent && !parent->IsTopLevel() ) + { + parent = parent->GetParent(); + } + + // did we find the parent? + if ( !parent ) + { + // no other choice + direction |= wxCENTRE_ON_SCREEN; + } } if ( direction & wxCENTRE_ON_SCREEN ) @@ -337,10 +351,13 @@ void wxWindowBase::Centre(int direction) } else { - if (IsTopLevel()) + if ( IsTopLevel() ) { // centre on the parent parent->GetSize(&widthParent, &heightParent); + + // adjust to the parents position + posParent = parent->GetPosition(); } else { @@ -361,29 +378,8 @@ void wxWindowBase::Centre(int direction) if ( direction & wxVERTICAL ) yNew = (heightParent - height)/2; - // controls are always centered on their parent because it doesn't make - // sense to centre them on the screen - if ( !(direction & wxCENTRE_ON_SCREEN) || !IsTopLevel() ) - { - // the only chance to get this is to have a not top level window - // without parent which shouldn't happen - wxCHECK_RET( parent, wxT("this window must have a parent") ); - - wxPoint posParent; - if (IsTopLevel()) - { - // adjust to the parents position - posParent = parent->GetPosition(); - } - else - { - // adjust to the parents client area origin - posParent = parent->ClientToScreen(wxPoint(0, 0)); - } - - xNew += posParent.x; - yNew += posParent.y; - } + xNew += posParent.x; + yNew += posParent.y; // move the centre of this window to this position Move(xNew, yNew);