From 2ff62714d017b620b7d452a686f3667662b143f2 Mon Sep 17 00:00:00 2001 From: Ryan Leavengood Date: Sat, 22 Aug 2009 14:17:29 +0000 Subject: [PATCH] Finally implemented BWindow::CenterOnScreen, with associated CenterIn(BRect) methods as well as Size(). To avoid the problem of centering the window before it has been resized by the layout system, I force the resizing early. If there is a better way to do this or some way to avoid doing it repeatedly, let me know. But I figure the Center* methods should not be called that often. Updated Screenshot and DiskProbe to use this new method as a test. It certainly cleaned up DiskProbe. I will update other code over the next few days (if anyone wants to help, please do :) git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32612 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/interface/Window.h | 6 ++++ src/apps/diskprobe/OpenWindow.cpp | 16 +--------- src/apps/diskprobe/OpenWindow.h | 3 -- src/apps/screenshot/ScreenshotWindow.cpp | 7 +---- src/kits/interface/Window.cpp | 39 ++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 24 deletions(-) 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() {