123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- #include "mozilla/dom/ScrollBoxObject.h"
- #include "mozilla/dom/ScrollBoxObjectBinding.h"
- #include "mozilla/dom/Element.h"
- #include "mozilla/dom/ToJSValue.h"
- #include "nsCOMPtr.h"
- #include "nsIPresShell.h"
- #include "nsIContent.h"
- #include "nsIDOMElement.h"
- #include "nsPresContext.h"
- #include "nsBox.h"
- #include "nsIScrollableFrame.h"
- namespace mozilla {
- namespace dom {
- ScrollBoxObject::ScrollBoxObject()
- {
- }
- ScrollBoxObject::~ScrollBoxObject()
- {
- }
- JSObject* ScrollBoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
- {
- return ScrollBoxObjectBinding::Wrap(aCx, this, aGivenProto);
- }
- nsIScrollableFrame* ScrollBoxObject::GetScrollFrame()
- {
- return do_QueryFrame(GetFrame(false));
- }
- void ScrollBoxObject::ScrollTo(int32_t x, int32_t y, ErrorResult& aRv)
- {
- nsIScrollableFrame* sf = GetScrollFrame();
- if (!sf) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- sf->ScrollToCSSPixels(CSSIntPoint(x, y));
- }
- void ScrollBoxObject::ScrollBy(int32_t dx, int32_t dy, ErrorResult& aRv)
- {
- CSSIntPoint pt;
- GetPosition(pt, aRv);
- if (aRv.Failed()) {
- return;
- }
- ScrollTo(pt.x + dx, pt.y + dy, aRv);
- }
- void ScrollBoxObject::ScrollByLine(int32_t dlines, ErrorResult& aRv)
- {
- nsIScrollableFrame* sf = GetScrollFrame();
- if (!sf) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- sf->ScrollBy(nsIntPoint(0, dlines), nsIScrollableFrame::LINES,
- nsIScrollableFrame::SMOOTH);
- }
- // XUL <scrollbox> elements have a single box child element.
- // Get a pointer to that box.
- // Note that now that the <scrollbox> is just a regular box
- // with 'overflow:hidden', the boxobject's frame is an nsXULScrollFrame,
- // the <scrollbox>'s box frame is the scrollframe's "scrolled frame", and
- // the <scrollbox>'s child box is a child of that.
- static nsIFrame* GetScrolledBox(BoxObject* aScrollBox) {
- nsIFrame* frame = aScrollBox->GetFrame(false);
- if (!frame) {
- return nullptr;
- }
- nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
- if (!scrollFrame) {
- NS_WARNING("ScrollBoxObject attached to something that's not a scroll frame!");
- return nullptr;
- }
- nsIFrame* scrolledFrame = scrollFrame->GetScrolledFrame();
- if (!scrolledFrame)
- return nullptr;
- return nsBox::GetChildXULBox(scrolledFrame);
- }
- void ScrollBoxObject::ScrollByIndex(int32_t dindexes, ErrorResult& aRv)
- {
- nsIScrollableFrame* sf = GetScrollFrame();
- if (!sf) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- nsIFrame* scrolledBox = GetScrolledBox(this);
- if (!scrolledBox) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- nsRect rect;
- // now get the scrolled boxes first child.
- nsIFrame* child = nsBox::GetChildXULBox(scrolledBox);
- bool horiz = scrolledBox->IsXULHorizontal();
- nsPoint cp = sf->GetScrollPosition();
- nscoord diff = 0;
- int32_t curIndex = 0;
- bool isLTR = scrolledBox->IsXULNormalDirection();
- int32_t frameWidth = 0;
- if (!isLTR && horiz) {
- GetWidth(&frameWidth);
- nsCOMPtr<nsIPresShell> shell = GetPresShell(false);
- if (!shell) {
- aRv.Throw(NS_ERROR_UNEXPECTED);
- return;
- }
- frameWidth = nsPresContext::CSSPixelsToAppUnits(frameWidth);
- }
- // first find out what index we are currently at
- while(child) {
- rect = child->GetRect();
- if (horiz) {
- // In the left-to-right case we break from the loop when the center of
- // the current child rect is greater than the scrolled position of
- // the left edge of the scrollbox
- // In the right-to-left case we break when the center of the current
- // child rect is less than the scrolled position of the right edge of
- // the scrollbox.
- diff = rect.x + rect.width/2; // use the center, to avoid rounding errors
- if ((isLTR && diff > cp.x) ||
- (!isLTR && diff < cp.x + frameWidth)) {
- break;
- }
- } else {
- diff = rect.y + rect.height/2;// use the center, to avoid rounding errors
- if (diff > cp.y) {
- break;
- }
- }
- child = nsBox::GetNextXULBox(child);
- curIndex++;
- }
- int32_t count = 0;
- if (dindexes == 0)
- return;
- if (dindexes > 0) {
- while(child) {
- child = nsBox::GetNextXULBox(child);
- if (child) {
- rect = child->GetRect();
- }
- count++;
- if (count >= dindexes) {
- break;
- }
- }
- } else if (dindexes < 0) {
- child = nsBox::GetChildXULBox(scrolledBox);
- while(child) {
- rect = child->GetRect();
- if (count >= curIndex + dindexes) {
- break;
- }
- count++;
- child = nsBox::GetNextXULBox(child);
- }
- }
- nscoord csspixel = nsPresContext::CSSPixelsToAppUnits(1);
- if (horiz) {
- // In the left-to-right case we scroll so that the left edge of the
- // selected child is scrolled to the left edge of the scrollbox.
- // In the right-to-left case we scroll so that the right edge of the
- // selected child is scrolled to the right edge of the scrollbox.
- nsPoint pt(isLTR ? rect.x : rect.x + rect.width - frameWidth,
- cp.y);
- // Use a destination range that ensures the left edge (or right edge,
- // for RTL) will indeed be visible. Also ensure that the top edge
- // is visible.
- nsRect range(pt.x, pt.y, csspixel, 0);
- if (isLTR) {
- range.x -= csspixel;
- }
- sf->ScrollTo(pt, nsIScrollableFrame::INSTANT, &range);
- } else {
- // Use a destination range that ensures the top edge will be visible.
- nsRect range(cp.x, rect.y - csspixel, 0, csspixel);
- sf->ScrollTo(nsPoint(cp.x, rect.y), nsIScrollableFrame::INSTANT, &range);
- }
- }
- void ScrollBoxObject::ScrollToLine(int32_t line, ErrorResult& aRv)
- {
- nsIScrollableFrame* sf = GetScrollFrame();
- if (!sf) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- nscoord y = sf->GetLineScrollAmount().height * line;
- nsRect range(0, y - nsPresContext::CSSPixelsToAppUnits(1),
- 0, nsPresContext::CSSPixelsToAppUnits(1));
- sf->ScrollTo(nsPoint(0, y), nsIScrollableFrame::INSTANT, &range);
- }
- void ScrollBoxObject::ScrollToElement(Element& child, ErrorResult& aRv)
- {
- nsCOMPtr<nsIPresShell> shell = GetPresShell(false);
- if (!shell) {
- aRv.Throw(NS_ERROR_UNEXPECTED);
- return;
- }
- shell->ScrollContentIntoView(&child,
- nsIPresShell::ScrollAxis(
- nsIPresShell::SCROLL_TOP,
- nsIPresShell::SCROLL_ALWAYS),
- nsIPresShell::ScrollAxis(
- nsIPresShell::SCROLL_LEFT,
- nsIPresShell::SCROLL_ALWAYS),
- nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY |
- nsIPresShell::SCROLL_OVERFLOW_HIDDEN);
- }
- void ScrollBoxObject::ScrollToIndex(int32_t index, ErrorResult& aRv)
- {
- aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
- }
- int32_t ScrollBoxObject::GetPositionX(ErrorResult& aRv)
- {
- CSSIntPoint pt;
- GetPosition(pt, aRv);
- return pt.x;
- }
- int32_t ScrollBoxObject::GetPositionY(ErrorResult& aRv)
- {
- CSSIntPoint pt;
- GetPosition(pt, aRv);
- return pt.y;
- }
- int32_t ScrollBoxObject::GetScrolledWidth(ErrorResult& aRv)
- {
- nsRect scrollRect;
- GetScrolledSize(scrollRect, aRv);
- return scrollRect.width;
- }
- int32_t ScrollBoxObject::GetScrolledHeight(ErrorResult& aRv)
- {
- nsRect scrollRect;
- GetScrolledSize(scrollRect, aRv);
- return scrollRect.height;
- }
- /* private helper */
- void ScrollBoxObject::GetPosition(CSSIntPoint& aPos, ErrorResult& aRv)
- {
- nsIScrollableFrame* sf = GetScrollFrame();
- if (!sf) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- aPos = sf->GetScrollPositionCSSPixels();
- }
- /* private helper */
- void ScrollBoxObject::GetScrolledSize(nsRect& aRect, ErrorResult& aRv)
- {
- nsIFrame* scrolledBox = GetScrolledBox(this);
- if (!scrolledBox) {
- aRv.Throw(NS_ERROR_FAILURE);
- return;
- }
- aRect = scrolledBox->GetRect();
- aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
- aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
- }
- void ScrollBoxObject::GetPosition(JSContext* cx,
- JS::Handle<JSObject*> x,
- JS::Handle<JSObject*> y,
- ErrorResult& aRv)
- {
- CSSIntPoint pt;
- GetPosition(pt, aRv);
- JS::Rooted<JS::Value> v(cx);
- if (!ToJSValue(cx, pt.x, &v) ||
- !JS_SetProperty(cx, x, "value", v)) {
- aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
- return;
- }
- if (!ToJSValue(cx, pt.y, &v) ||
- !JS_SetProperty(cx, y, "value", v)) {
- aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
- return;
- }
- }
- void ScrollBoxObject::GetScrolledSize(JSContext* cx,
- JS::Handle<JSObject*> width,
- JS::Handle<JSObject*> height,
- ErrorResult& aRv)
- {
- nsRect rect;
- GetScrolledSize(rect, aRv);
- JS::Rooted<JS::Value> v(cx);
- if (!ToJSValue(cx, rect.width, &v) ||
- !JS_SetProperty(cx, width, "value", v)) {
- aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
- return;
- }
- if (!ToJSValue(cx, rect.height, &v) ||
- !JS_SetProperty(cx, height, "value", v)) {
- aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
- return;
- }
- }
- void ScrollBoxObject::EnsureElementIsVisible(Element& child, ErrorResult& aRv)
- {
- nsCOMPtr<nsIPresShell> shell = GetPresShell(false);
- if (!shell) {
- aRv.Throw(NS_ERROR_UNEXPECTED);
- return;
- }
- shell->ScrollContentIntoView(&child,
- nsIPresShell::ScrollAxis(),
- nsIPresShell::ScrollAxis(),
- nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY |
- nsIPresShell::SCROLL_OVERFLOW_HIDDEN);
- }
- void ScrollBoxObject::EnsureIndexIsVisible(int32_t index, ErrorResult& aRv)
- {
- aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
- }
- void ScrollBoxObject::EnsureLineIsVisible(int32_t line, ErrorResult& aRv)
- {
- aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
- }
- } // namespace dom
- } // namespace mozilla
- // Creation Routine ///////////////////////////////////////////////////////////////////////
- using namespace mozilla::dom;
- nsresult
- NS_NewScrollBoxObject(nsIBoxObject** aResult)
- {
- NS_ADDREF(*aResult = new ScrollBoxObject());
- return NS_OK;
- }
|