From 817c3a16bacce382111f62e4fb6afc3c95b46ec0 Mon Sep 17 00:00:00 2001 From: "rods%netscape.com" Date: Tue, 21 Mar 2000 15:18:07 +0000 Subject: [PATCH] Added static method for getting the absolute rect in twips and pixels moved from nsComboboxControlFrame.cpp r=dcone b=18895 --- layout/forms/nsFormControlFrame.cpp | 79 ++++++++++++++++++++ layout/forms/nsFormControlFrame.h | 12 +++ layout/html/forms/src/nsFormControlFrame.cpp | 79 ++++++++++++++++++++ layout/html/forms/src/nsFormControlFrame.h | 12 +++ 4 files changed, 182 insertions(+) diff --git a/layout/forms/nsFormControlFrame.cpp b/layout/forms/nsFormControlFrame.cpp index 780518a743f..cf3900dd3dd 100644 --- a/layout/forms/nsFormControlFrame.cpp +++ b/layout/forms/nsFormControlFrame.cpp @@ -59,6 +59,7 @@ #include "nsIDOMHTMLLegendElement.h" #include "nsIDOMHTMLButtonElement.h" #include "nsIEventStateManager.h" +#include "nsIScrollableView.h" #ifdef DEBUG_evaughan //#define DEBUG_rods @@ -968,3 +969,81 @@ nsFormControlFrame::GetScreenHeight(nsIPresContext* aPresContext, nscoord& aHeig return NS_ERROR_FAILURE; } + +// Calculate a frame's position in screen coordinates +nsresult +nsFormControlFrame::GetAbsoluteFramePosition(nsIPresContext* aPresContext, + nsIFrame *aFrame, + nsRect& aAbsoluteTwipsRect, + nsRect& aAbsolutePixelRect) +{ + //XXX: This code needs to take the view's offset into account when calculating + //the absolute coordinate of the frame. + nsresult rv = NS_OK; + + aFrame->GetRect(aAbsoluteTwipsRect); + // zero these out, + // because the GetOffsetFromView figures them out + aAbsoluteTwipsRect.x = 0; + aAbsoluteTwipsRect.y = 0; + + // Get conversions between twips and pixels + float t2p; + float p2t; + aPresContext->GetTwipsToPixels(&t2p); + aPresContext->GetPixelsToTwips(&p2t); + + // Add in frame's offset from it it's containing view + nsIView *containingView = nsnull; + nsPoint offset; + rv = aFrame->GetOffsetFromView(aPresContext, offset, &containingView); + if (NS_SUCCEEDED(rv) && (nsnull != containingView)) { + aAbsoluteTwipsRect.x += offset.x; + aAbsoluteTwipsRect.y += offset.y; + + nsPoint viewOffset; + containingView->GetPosition(&viewOffset.x, &viewOffset.y); + nsIView * parent; + containingView->GetParent(parent); + while (nsnull != parent) { + nsPoint po; + parent->GetPosition(&po.x, &po.y); + viewOffset.x += po.x; + viewOffset.y += po.y; + nsIScrollableView * scrollView; + if (NS_OK == containingView->QueryInterface(NS_GET_IID(nsIScrollableView), (void **)&scrollView)) { + nscoord x; + nscoord y; + scrollView->GetScrollPosition(x, y); + viewOffset.x -= x; + viewOffset.y -= y; + } + nsIWidget * widget; + parent->GetWidget(widget); + if (nsnull != widget) { + // Add in the absolute offset of the widget. + nsRect absBounds; + nsRect lc; + widget->WidgetToScreen(lc, absBounds); + // Convert widget coordinates to twips + aAbsoluteTwipsRect.x += NSIntPixelsToTwips(absBounds.x, p2t); + aAbsoluteTwipsRect.y += NSIntPixelsToTwips(absBounds.y, p2t); + NS_RELEASE(widget); + break; + } + parent->GetParent(parent); + } + aAbsoluteTwipsRect.x += viewOffset.x; + aAbsoluteTwipsRect.y += viewOffset.y; + } + + // convert to pixel coordinates + if (NS_SUCCEEDED(rv)) { + aAbsolutePixelRect.x = NSTwipsToIntPixels(aAbsoluteTwipsRect.x, t2p); + aAbsolutePixelRect.y = NSTwipsToIntPixels(aAbsoluteTwipsRect.y, t2p); + aAbsolutePixelRect.width = NSTwipsToIntPixels(aAbsoluteTwipsRect.width, t2p); + aAbsolutePixelRect.height = NSTwipsToIntPixels(aAbsoluteTwipsRect.height, t2p); + } + + return rv; +} diff --git a/layout/forms/nsFormControlFrame.h b/layout/forms/nsFormControlFrame.h index 9fe84d84d14..5bdb8453b4f 100644 --- a/layout/forms/nsFormControlFrame.h +++ b/layout/forms/nsFormControlFrame.h @@ -243,8 +243,20 @@ public: const nsRect& aDirtyRect, const nsRect& aRect); + /** + * Helper routine to that returns the height of the screen + * + */ static nsresult GetScreenHeight(nsIPresContext* aPresContext, nscoord& aHeight); + /** + * Helper method to get the absolute position of a frame + * + */ + static nsresult GetAbsoluteFramePosition(nsIPresContext* aPresContext, + nsIFrame *aFrame, + nsRect& aAbsoluteTwipsRect, + nsRect& aAbsolutePixelRect); protected: virtual ~nsFormControlFrame(); diff --git a/layout/html/forms/src/nsFormControlFrame.cpp b/layout/html/forms/src/nsFormControlFrame.cpp index 780518a743f..cf3900dd3dd 100644 --- a/layout/html/forms/src/nsFormControlFrame.cpp +++ b/layout/html/forms/src/nsFormControlFrame.cpp @@ -59,6 +59,7 @@ #include "nsIDOMHTMLLegendElement.h" #include "nsIDOMHTMLButtonElement.h" #include "nsIEventStateManager.h" +#include "nsIScrollableView.h" #ifdef DEBUG_evaughan //#define DEBUG_rods @@ -968,3 +969,81 @@ nsFormControlFrame::GetScreenHeight(nsIPresContext* aPresContext, nscoord& aHeig return NS_ERROR_FAILURE; } + +// Calculate a frame's position in screen coordinates +nsresult +nsFormControlFrame::GetAbsoluteFramePosition(nsIPresContext* aPresContext, + nsIFrame *aFrame, + nsRect& aAbsoluteTwipsRect, + nsRect& aAbsolutePixelRect) +{ + //XXX: This code needs to take the view's offset into account when calculating + //the absolute coordinate of the frame. + nsresult rv = NS_OK; + + aFrame->GetRect(aAbsoluteTwipsRect); + // zero these out, + // because the GetOffsetFromView figures them out + aAbsoluteTwipsRect.x = 0; + aAbsoluteTwipsRect.y = 0; + + // Get conversions between twips and pixels + float t2p; + float p2t; + aPresContext->GetTwipsToPixels(&t2p); + aPresContext->GetPixelsToTwips(&p2t); + + // Add in frame's offset from it it's containing view + nsIView *containingView = nsnull; + nsPoint offset; + rv = aFrame->GetOffsetFromView(aPresContext, offset, &containingView); + if (NS_SUCCEEDED(rv) && (nsnull != containingView)) { + aAbsoluteTwipsRect.x += offset.x; + aAbsoluteTwipsRect.y += offset.y; + + nsPoint viewOffset; + containingView->GetPosition(&viewOffset.x, &viewOffset.y); + nsIView * parent; + containingView->GetParent(parent); + while (nsnull != parent) { + nsPoint po; + parent->GetPosition(&po.x, &po.y); + viewOffset.x += po.x; + viewOffset.y += po.y; + nsIScrollableView * scrollView; + if (NS_OK == containingView->QueryInterface(NS_GET_IID(nsIScrollableView), (void **)&scrollView)) { + nscoord x; + nscoord y; + scrollView->GetScrollPosition(x, y); + viewOffset.x -= x; + viewOffset.y -= y; + } + nsIWidget * widget; + parent->GetWidget(widget); + if (nsnull != widget) { + // Add in the absolute offset of the widget. + nsRect absBounds; + nsRect lc; + widget->WidgetToScreen(lc, absBounds); + // Convert widget coordinates to twips + aAbsoluteTwipsRect.x += NSIntPixelsToTwips(absBounds.x, p2t); + aAbsoluteTwipsRect.y += NSIntPixelsToTwips(absBounds.y, p2t); + NS_RELEASE(widget); + break; + } + parent->GetParent(parent); + } + aAbsoluteTwipsRect.x += viewOffset.x; + aAbsoluteTwipsRect.y += viewOffset.y; + } + + // convert to pixel coordinates + if (NS_SUCCEEDED(rv)) { + aAbsolutePixelRect.x = NSTwipsToIntPixels(aAbsoluteTwipsRect.x, t2p); + aAbsolutePixelRect.y = NSTwipsToIntPixels(aAbsoluteTwipsRect.y, t2p); + aAbsolutePixelRect.width = NSTwipsToIntPixels(aAbsoluteTwipsRect.width, t2p); + aAbsolutePixelRect.height = NSTwipsToIntPixels(aAbsoluteTwipsRect.height, t2p); + } + + return rv; +} diff --git a/layout/html/forms/src/nsFormControlFrame.h b/layout/html/forms/src/nsFormControlFrame.h index 9fe84d84d14..5bdb8453b4f 100644 --- a/layout/html/forms/src/nsFormControlFrame.h +++ b/layout/html/forms/src/nsFormControlFrame.h @@ -243,8 +243,20 @@ public: const nsRect& aDirtyRect, const nsRect& aRect); + /** + * Helper routine to that returns the height of the screen + * + */ static nsresult GetScreenHeight(nsIPresContext* aPresContext, nscoord& aHeight); + /** + * Helper method to get the absolute position of a frame + * + */ + static nsresult GetAbsoluteFramePosition(nsIPresContext* aPresContext, + nsIFrame *aFrame, + nsRect& aAbsoluteTwipsRect, + nsRect& aAbsolutePixelRect); protected: virtual ~nsFormControlFrame();