diff --git a/docs/user/interface/Window.dox b/docs/user/interface/Window.dox index 4d7653bfbc..17bdf36e60 100644 --- a/docs/user/interface/Window.dox +++ b/docs/user/interface/Window.dox @@ -1954,7 +1954,11 @@ /*! \fn void BWindow::CenterOnScreen() - \brief Centers the window on the screen the window is currently on. + \brief Centers the window on the current screen. + + The window is positioned on the screen a bit above center. If you want to + center the window exactly in the middle you'll need to use + BWindow::CenterIn() instead. \since Haiku R1 */ @@ -1964,6 +1968,12 @@ \fn void BWindow::CenterOnScreen(screen_id id) \brief Centers the window on the screen with the passed in \a id. + \param id The screen_id of the screen to center the window in. + + The window is positioned on the screen a bit above center. If you want to + center the window exactly in the middle you'll need to use + BWindow::CenterIn() instead. + \since Haiku R1 */ diff --git a/headers/os/interface/Window.h b/headers/os/interface/Window.h index c59a5494b2..35d0adba27 100644 --- a/headers/os/interface/Window.h +++ b/headers/os/interface/Window.h @@ -366,6 +366,8 @@ private: float* _tabHeight) const; void _SendShowOrHideMessage(); + void _CenterAboveCenter(BRect rect); + private: char* fTitle; int32 _unused0; diff --git a/src/kits/interface/Window.cpp b/src/kits/interface/Window.cpp index 4be291ad68..4413df4269 100644 --- a/src/kits/interface/Window.cpp +++ b/src/kits/interface/Window.cpp @@ -2568,6 +2568,7 @@ BWindow::ResizeToPreferred() } +// Centers the window in rect. void BWindow::CenterIn(const BRect& rect) { @@ -2582,18 +2583,21 @@ BWindow::CenterIn(const BRect& rect) } +// Centers the window offset a bit above center on the current screen. void BWindow::CenterOnScreen() { - CenterIn(BScreen(this).Frame()); + BRect screenFrame(BScreen(this).Frame()); + _CenterAboveCenter(screenFrame); } -// Centers the window on the screen with the passed in id. +// Centers the window offset a bit above center on the screen specified by id. void BWindow::CenterOnScreen(screen_id id) { - CenterIn(BScreen(id).Frame()); + BRect screenFrame(BScreen(id).Frame()); + _CenterAboveCenter(screenFrame); } @@ -4180,6 +4184,45 @@ BWindow::_SendShowOrHideMessage() } +// Centers the window in the rect offset a bit above center. +void +BWindow::_CenterAboveCenter(BRect rect) +{ + BAutolock locker(this); + + // Set size limits now if needed + UpdateSizeLimits(); + + BPoint centered = BLayoutUtils::AlignInFrame(rect, Size(), + BAlignment(B_ALIGN_HORIZONTAL_CENTER, B_ALIGN_VERTICAL_CENTER)) + .LeftTop(); + centered.y -= floorf(rect.Height() / 16); + // Offset y coordinate so that the window is positioned like this: + + // ---------------------------------------------------------- + // | | + // | | + // | _________ | + // | [_]_______|_____________ | + // | | | | + // | | | | + // | | | | + // | | | | + // | | | | + // | | | | + // | |_______________________| | + // | | + // | | + // | | + // | | + // | | + // | | + // ---------------------------------------------------------- + + MoveTo(centered); +} + + // #pragma mark - C++ binary compatibility kludge