diff --git a/headers/os/interface/Window.h b/headers/os/interface/Window.h index 49869faf46..cc03c338dd 100644 --- a/headers/os/interface/Window.h +++ b/headers/os/interface/Window.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -167,6 +168,10 @@ public: void ResizeBy(float dx, float dy); void ResizeTo(float width, float height); + void CenterIn(BRect* rect); + void CenterIn(BRect rect); + void CenterOnScreen(); + virtual void Show(); virtual void Hide(); bool IsHidden() const; @@ -188,6 +193,7 @@ public: BRect Bounds() const; BRect Frame() const; BRect DecoratorFrame() const; + BSize Size() const; const char* Title() const; void SetTitle(const char* title); bool IsFront() const; diff --git a/src/apps/diskprobe/OpenWindow.cpp b/src/apps/diskprobe/OpenWindow.cpp index 4ededce048..1d3ec212b8 100644 --- a/src/apps/diskprobe/OpenWindow.cpp +++ b/src/apps/diskprobe/OpenWindow.cpp @@ -57,22 +57,8 @@ OpenWindow::OpenWindow() .Add(probeDeviceButton, 2, 1) .SetInsets(8, 8, 8, 8) ); - BScreen screen(this); - // move the window offscreen.. - MoveTo(screen.Frame().right+20, screen.Frame().top); - fCentered = true; -} - -void -OpenWindow::FrameResized(float width, float height) -{ - if (fCentered) { - BScreen screen(this); - MoveTo(screen.Frame().left + (screen.Frame().Width() - width) / 2, - screen.Frame().top + (screen.Frame().Height() - height) / 2); - } - fCentered = false; + CenterOnScreen(); } diff --git a/src/apps/diskprobe/OpenWindow.h b/src/apps/diskprobe/OpenWindow.h index b7269751f6..4df1c635ce 100644 --- a/src/apps/diskprobe/OpenWindow.h +++ b/src/apps/diskprobe/OpenWindow.h @@ -20,14 +20,11 @@ public: virtual void MessageReceived(BMessage* message); virtual bool QuitRequested(); - virtual void FrameResized(float width, float height); - static void CollectDevices(BMenu* menu, BEntry* startEntry = NULL); private: BMenu* fDevicesMenu; - bool fCentered; }; #endif /* OPEN_WINDOW_H */ diff --git a/src/apps/screenshot/ScreenshotWindow.cpp b/src/apps/screenshot/ScreenshotWindow.cpp index 310db4e739..d4f1a200d5 100644 --- a/src/apps/screenshot/ScreenshotWindow.cpp +++ b/src/apps/screenshot/ScreenshotWindow.cpp @@ -492,12 +492,7 @@ ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label, void ScreenshotWindow::_CenterAndShow() { - BSize size = GetLayout()->PreferredSize(); - ResizeTo(size.Width(), size.Height()); - - BRect frame(BScreen(this).Frame()); - MoveTo((frame.Width() - size.Width()) / 2.0, - (frame.Height() - size.Height()) / 2.0); + CenterOnScreen(); Show(); } diff --git a/src/kits/interface/Window.cpp b/src/kits/interface/Window.cpp index 20a1bf24a7..7e7cf7f751 100644 --- a/src/kits/interface/Window.cpp +++ b/src/kits/interface/Window.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include #include @@ -2026,6 +2028,13 @@ BWindow::DecoratorFrame() const } +BSize +BWindow::Size() const +{ + return BSize(fFrame.Width(), fFrame.Height()); +} + + const char* BWindow::Title() const { @@ -2448,6 +2457,36 @@ BWindow::ResizeTo(float width, float height) } +void +BWindow::CenterIn(BRect rect) +{ + // Force layout resizing if needed + if (GetLayout() != NULL) { + BSize size = GetLayout()->PreferredSize(); + ResizeTo(size.Width(), size.Height()); + } + + MoveTo(BLayoutUtils::AlignInFrame(rect, Size(), + BAlignment(B_ALIGN_HORIZONTAL_CENTER, + B_ALIGN_VERTICAL_CENTER)).LeftTop()); +} + + +void +BWindow::CenterIn(BRect* rect) +{ + CenterIn(*rect); +} + + +void +BWindow::CenterOnScreen() +{ + BScreen screen(this); + CenterIn(screen.Frame()); +} + + void BWindow::Show() {