diff --git a/headers/os/interface/Window.h b/headers/os/interface/Window.h index 59f6c5e560..5b1d6893fa 100644 --- a/headers/os/interface/Window.h +++ b/headers/os/interface/Window.h @@ -166,8 +166,8 @@ public: void ResizeBy(float dx, float dy); void ResizeTo(float width, float height); - void CenterIn(const BRect& rect); - void CenterOnScreen(); + BPoint CenterIn(const BRect& rect); + BPoint CenterOnScreen(); virtual void Show(); virtual void Hide(); diff --git a/src/kits/interface/Window.cpp b/src/kits/interface/Window.cpp index 10cd771a53..a7ef4a8ffe 100644 --- a/src/kits/interface/Window.cpp +++ b/src/kits/interface/Window.cpp @@ -2584,23 +2584,62 @@ BWindow::ResizeTo(float width, float height) } -void +/*! + \brief Center the window in the passed in rectangular area. + \param rect The rectangle to center the window in. + \return The new window position. +*/ +BPoint BWindow::CenterIn(const BRect& rect) { // Set size limits now if needed UpdateSizeLimits(); - MoveTo(BLayoutUtils::AlignInFrame(rect, Size(), + BPoint point = BLayoutUtils::AlignInFrame(rect, Size(), BAlignment(B_ALIGN_HORIZONTAL_CENTER, - B_ALIGN_VERTICAL_CENTER)).LeftTop()); + B_ALIGN_VERTICAL_CENTER)).LeftTop(); + + BRect screenFrame = (BScreen(this)).Frame(); + if (screenFrame == rect) { + // if rect is the screen frame we can skip the ajustments below + MoveTo(point); + return point; + } + + float borderWidth; + float tabHeight; + _GetDecoratorSize(&borderWidth, &tabHeight); + + // clip the x position within the horizontal edges of the screen + if (point.x < screenFrame.left + borderWidth) + point.x = screenFrame.left + borderWidth; + else if (point.x + Frame().Width() > screenFrame.right - borderWidth) + point.x = screenFrame.right - borderWidth - Frame().Width(); + + // If the window is covering the window tab lower it down + if (point.y < rect.LeftTop().y + borderWidth) + point.y = rect.LeftTop().y + borderWidth; + + // clip the y position within the vertical edges of the screen + if (point.y < screenFrame.top + borderWidth) + point.y = screenFrame.top + borderWidth; + else if (point.y + Frame().Height() > screenFrame.bottom - borderWidth) + point.y = screenFrame.bottom - borderWidth - Frame().Height(); + + MoveTo(point); + return point; } -void +/*! + \brief Centers the window on the screen the window is currently on. + \return The new window position. +*/ +BPoint BWindow::CenterOnScreen() { BScreen screen(this); - CenterIn(screen.Frame()); + return CenterIn(screen.Frame()); }