123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /* -*- 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/. */
- //
- // Eric Vaughan
- // Netscape Communications
- //
- // See documentation in associated header file
- //
- #include "nsScrollbarButtonFrame.h"
- #include "nsPresContext.h"
- #include "nsIContent.h"
- #include "nsCOMPtr.h"
- #include "nsNameSpaceManager.h"
- #include "nsGkAtoms.h"
- #include "nsSliderFrame.h"
- #include "nsScrollbarFrame.h"
- #include "nsIScrollbarMediator.h"
- #include "nsRepeatService.h"
- #include "mozilla/LookAndFeel.h"
- #include "mozilla/MouseEvents.h"
- #include "mozilla/Telemetry.h"
- #include "mozilla/layers/ScrollInputMethods.h"
- using namespace mozilla;
- using mozilla::layers::ScrollInputMethod;
- //
- // NS_NewToolbarFrame
- //
- // Creates a new Toolbar frame and returns it
- //
- nsIFrame*
- NS_NewScrollbarButtonFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
- {
- return new (aPresShell) nsScrollbarButtonFrame(aContext);
- }
- NS_IMPL_FRAMEARENA_HELPERS(nsScrollbarButtonFrame)
- nsresult
- nsScrollbarButtonFrame::HandleEvent(nsPresContext* aPresContext,
- WidgetGUIEvent* aEvent,
- nsEventStatus* aEventStatus)
- {
- NS_ENSURE_ARG_POINTER(aEventStatus);
- // If a web page calls event.preventDefault() we still want to
- // scroll when scroll arrow is clicked. See bug 511075.
- if (!mContent->IsInNativeAnonymousSubtree() &&
- nsEventStatus_eConsumeNoDefault == *aEventStatus) {
- return NS_OK;
- }
- switch (aEvent->mMessage) {
- case eMouseDown:
- mCursorOnThis = true;
- // if we didn't handle the press ourselves, pass it on to the superclass
- if (HandleButtonPress(aPresContext, aEvent, aEventStatus)) {
- return NS_OK;
- }
- break;
- case eMouseUp:
- HandleRelease(aPresContext, aEvent, aEventStatus);
- break;
- case eMouseOut:
- mCursorOnThis = false;
- break;
- case eMouseMove: {
- nsPoint cursor =
- nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
- nsRect frameRect(nsPoint(0, 0), GetSize());
- mCursorOnThis = frameRect.Contains(cursor);
- break;
- }
- default:
- break;
- }
- return nsButtonBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
- }
- bool
- nsScrollbarButtonFrame::HandleButtonPress(nsPresContext* aPresContext,
- WidgetGUIEvent* aEvent,
- nsEventStatus* aEventStatus)
- {
- // Get the desired action for the scrollbar button.
- LookAndFeel::IntID tmpAction;
- uint16_t button = aEvent->AsMouseEvent()->button;
- if (button == WidgetMouseEvent::eLeftButton) {
- tmpAction = LookAndFeel::eIntID_ScrollButtonLeftMouseButtonAction;
- } else if (button == WidgetMouseEvent::eMiddleButton) {
- tmpAction = LookAndFeel::eIntID_ScrollButtonMiddleMouseButtonAction;
- } else if (button == WidgetMouseEvent::eRightButton) {
- tmpAction = LookAndFeel::eIntID_ScrollButtonRightMouseButtonAction;
- } else {
- return false;
- }
- // Get the button action metric from the pres. shell.
- int32_t pressedButtonAction;
- if (NS_FAILED(LookAndFeel::GetInt(tmpAction, &pressedButtonAction))) {
- return false;
- }
- // get the scrollbar control
- nsIFrame* scrollbar;
- GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
- if (scrollbar == nullptr)
- return false;
-
- static nsIContent::AttrValuesArray strings[] = { &nsGkAtoms::increment,
- &nsGkAtoms::decrement,
- nullptr };
- int32_t index = mContent->FindAttrValueIn(kNameSpaceID_None,
- nsGkAtoms::type,
- strings, eCaseMatters);
- int32_t direction;
- if (index == 0)
- direction = 1;
- else if (index == 1)
- direction = -1;
- else
- return false;
- bool repeat = pressedButtonAction != 2;
- // set this attribute so we can style it later
- nsWeakFrame weakFrame(this);
- mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
- nsIPresShell::SetCapturingContent(mContent, CAPTURE_IGNOREALLOWED);
- if (!weakFrame.IsAlive()) {
- return false;
- }
- nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
- if (sb) {
- nsIScrollbarMediator* m = sb->GetScrollbarMediator();
- switch (pressedButtonAction) {
- case 0:
- sb->SetIncrementToLine(direction);
- if (m) {
- m->ScrollByLine(sb, direction, nsIScrollbarMediator::ENABLE_SNAP);
- }
- break;
- case 1:
- sb->SetIncrementToPage(direction);
- if (m) {
- m->ScrollByPage(sb, direction, nsIScrollbarMediator::ENABLE_SNAP);
- }
- break;
- case 2:
- sb->SetIncrementToWhole(direction);
- if (m) {
- m->ScrollByWhole(sb, direction, nsIScrollbarMediator::ENABLE_SNAP);
- }
- break;
- case 3:
- default:
- // We were told to ignore this click, or someone assigned a non-standard
- // value to the button's action.
- return false;
- }
- if (!weakFrame.IsAlive()) {
- return false;
- }
- if (!m) {
- sb->MoveToNewPosition();
- if (!weakFrame.IsAlive()) {
- return false;
- }
- }
- }
- if (repeat) {
- StartRepeat();
- }
- return true;
- }
- NS_IMETHODIMP
- nsScrollbarButtonFrame::HandleRelease(nsPresContext* aPresContext,
- WidgetGUIEvent* aEvent,
- nsEventStatus* aEventStatus)
- {
- nsIPresShell::SetCapturingContent(nullptr, 0);
- // we're not active anymore
- mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::active, true);
- StopRepeat();
- nsIFrame* scrollbar;
- GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
- nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
- if (sb) {
- nsIScrollbarMediator* m = sb->GetScrollbarMediator();
- if (m) {
- m->ScrollbarReleased(sb);
- }
- }
- return NS_OK;
- }
- void nsScrollbarButtonFrame::Notify()
- {
- if (mCursorOnThis ||
- LookAndFeel::GetInt(
- LookAndFeel::eIntID_ScrollbarButtonAutoRepeatBehavior, 0)) {
- // get the scrollbar control
- nsIFrame* scrollbar;
- GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
- nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
- if (sb) {
- nsIScrollbarMediator* m = sb->GetScrollbarMediator();
- if (m) {
- m->RepeatButtonScroll(sb);
- } else {
- sb->MoveToNewPosition();
- }
- }
- }
- }
- void
- nsScrollbarButtonFrame::MouseClicked(WidgetGUIEvent* aEvent)
- {
- nsButtonBoxFrame::MouseClicked(aEvent);
- //MouseClicked();
- }
- nsresult
- nsScrollbarButtonFrame::GetChildWithTag(nsIAtom* atom, nsIFrame* start,
- nsIFrame*& result)
- {
- // recursively search our children
- for (nsIFrame* childFrame : start->PrincipalChildList())
- {
- // get the content node
- nsIContent* child = childFrame->GetContent();
- if (child) {
- // see if it is the child
- if (child->IsXULElement(atom))
- {
- result = childFrame;
- return NS_OK;
- }
- }
- // recursive search the child
- GetChildWithTag(atom, childFrame, result);
- if (result != nullptr)
- return NS_OK;
- }
- result = nullptr;
- return NS_OK;
- }
- nsresult
- nsScrollbarButtonFrame::GetParentWithTag(nsIAtom* toFind, nsIFrame* start,
- nsIFrame*& result)
- {
- while (start)
- {
- start = start->GetParent();
- if (start) {
- // get the content node
- nsIContent* child = start->GetContent();
- if (child && child->IsXULElement(toFind)) {
- result = start;
- return NS_OK;
- }
- }
- }
- result = nullptr;
- return NS_OK;
- }
- void
- nsScrollbarButtonFrame::DestroyFrom(nsIFrame* aDestructRoot)
- {
- // Ensure our repeat service isn't going... it's possible that a scrollbar can disappear out
- // from under you while you're in the process of scrolling.
- StopRepeat();
- nsButtonBoxFrame::DestroyFrom(aDestructRoot);
- }
|