← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2698: dwt: improve centerWindow()

 

------------------------------------------------------------
revno: 2698
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Mon 2011-12-05 20:17:57 +0100
message:
  dwt: improve centerWindow()
modified:
  dwt/include/dwt/aspects/Sizable.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dwt/include/dwt/aspects/Sizable.h'
--- dwt/include/dwt/aspects/Sizable.h	2011-11-07 20:53:49 +0000
+++ dwt/include/dwt/aspects/Sizable.h	2011-12-05 19:17:57 +0000
@@ -97,7 +97,28 @@
 	  */
 	void bringToFront();
 
-	void centerWindow(Widget* target = 0);
+	void centerWindow() {
+		// this is greatly inspired by MFC (wincore.cpp - CWnd::CenterWindow). 
+
+		/// @todo this only works for top-level windows (ie dialogs); for child windows, coords will need conversion.
+		auto root = W().getParent()->getRoot();
+		auto size = W().getWindowSize();
+		auto rect = root->getWindowRect();
+		rect.pos.x = (rect.left() + rect.right() - size.x) / 2;
+		rect.pos.y = (rect.top() + rect.bottom() - size.y) / 2;
+		rect.size = size;
+
+		// make sure the window is still in the screen area.
+		MONITORINFO mi = { sizeof(MONITORINFO) };
+		if(::GetMonitorInfo(::MonitorFromWindow(root->handle(), MONITOR_DEFAULTTONEAREST), &mi)) {
+			if(rect.right() > mi.rcWork.right) { rect.pos.x = mi.rcWork.right - rect.width(); }
+			if(rect.left() < mi.rcWork.left) { rect.pos.x = mi.rcWork.left; }
+			if(rect.bottom() > mi.rcWork.bottom) { rect.pos.y = mi.rcWork.bottom - rect.height(); }
+			if(rect.top() < mi.rcWork.top) { rect.pos.y = mi.rcWork.top; }
+		}
+
+		resize(rect);
+	}
 
 	/// Brings the widget to the bottom
 	/** Makes the widget become the bottom most widget meaning it will be obscured by
@@ -147,16 +168,6 @@
 }
 
 template< class WidgetType >
-void Sizable< WidgetType >::centerWindow( Widget* target ) {
-	Point size = W().getWindowSize();
-	if(!target) {
-		target = static_cast<WidgetType*>(this)->getParent();
-	}
-	Rectangle rc(target->getWindowRect());
-	resize(Rectangle(rc.left() + (rc.right() - rc.left())/2 - size.x/2, rc.top() + (rc.bottom() - rc.top())/2 - size.y/2, size.x, size.y)); /// @todo improve with methods of Rectangle like width() and height()?
-}
-
-template< class WidgetType >
 void Sizable< WidgetType >::bringToFront()
 {
 	::SetWindowPos(H(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );