2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// Eric Vaughan
|
|
|
|
// Netscape Communications
|
|
|
|
//
|
|
|
|
// See documentation in associated header file
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "nsScrollbarButtonFrame.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2014-02-27 15:04:46 -08:00
|
|
|
#include "nsNameSpaceManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsSliderFrame.h"
|
2011-07-11 07:05:09 -07:00
|
|
|
#include "nsScrollbarFrame.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScrollbarMediator.h"
|
|
|
|
#include "nsRepeatService.h"
|
2011-09-08 19:27:13 -07:00
|
|
|
#include "mozilla/LookAndFeel.h"
|
2013-09-25 04:21:18 -07:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2011-09-08 19:27:13 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// NS_NewToolbarFrame
|
|
|
|
//
|
|
|
|
// Creates a new Toolbar frame and returns it
|
|
|
|
//
|
|
|
|
nsIFrame*
|
|
|
|
NS_NewScrollbarButtonFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
|
|
|
|
{
|
|
|
|
return new (aPresShell) nsScrollbarButtonFrame(aPresShell, aContext);
|
2009-09-12 09:49:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsScrollbarButtonFrame)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-17 23:47:48 -08:00
|
|
|
nsresult
|
2013-10-01 20:46:03 -07:00
|
|
|
nsScrollbarButtonFrame::HandleEvent(nsPresContext* aPresContext,
|
|
|
|
WidgetGUIEvent* aEvent,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
2009-02-27 02:48:25 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aEventStatus);
|
2010-04-12 06:59:16 -07:00
|
|
|
|
|
|
|
// 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) {
|
2009-02-27 02:48:25 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-17 15:41:35 -08:00
|
|
|
switch (aEvent->message) {
|
|
|
|
case NS_MOUSE_BUTTON_DOWN:
|
|
|
|
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 NS_MOUSE_BUTTON_UP:
|
|
|
|
HandleRelease(aPresContext, aEvent, aEventStatus);
|
|
|
|
break;
|
|
|
|
case NS_MOUSE_EXIT_SYNTH:
|
|
|
|
mCursorOnThis = false;
|
|
|
|
break;
|
|
|
|
case NS_MOUSE_MOVE: {
|
|
|
|
nsPoint cursor =
|
|
|
|
nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
|
|
|
|
nsRect frameRect(nsPoint(0, 0), GetSize());
|
|
|
|
mCursorOnThis = frameRect.Contains(cursor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsButtonBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-10-01 20:46:03 -07:00
|
|
|
nsScrollbarButtonFrame::HandleButtonPress(nsPresContext* aPresContext,
|
|
|
|
WidgetGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Get the desired action for the scrollbar button.
|
2011-09-08 19:27:13 -07:00
|
|
|
LookAndFeel::IntID tmpAction;
|
2013-10-22 01:55:20 -07:00
|
|
|
uint16_t button = aEvent->AsMouseEvent()->button;
|
2013-10-01 23:38:27 -07:00
|
|
|
if (button == WidgetMouseEvent::eLeftButton) {
|
2011-11-17 15:41:35 -08:00
|
|
|
tmpAction = LookAndFeel::eIntID_ScrollButtonLeftMouseButtonAction;
|
2013-10-01 23:38:27 -07:00
|
|
|
} else if (button == WidgetMouseEvent::eMiddleButton) {
|
2011-11-17 15:41:35 -08:00
|
|
|
tmpAction = LookAndFeel::eIntID_ScrollButtonMiddleMouseButtonAction;
|
2013-10-01 23:38:27 -07:00
|
|
|
} else if (button == WidgetMouseEvent::eRightButton) {
|
2011-11-17 15:41:35 -08:00
|
|
|
tmpAction = LookAndFeel::eIntID_ScrollButtonRightMouseButtonAction;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the button action metric from the pres. shell.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t pressedButtonAction;
|
2011-09-08 19:27:13 -07:00
|
|
|
if (NS_FAILED(LookAndFeel::GetInt(tmpAction, &pressedButtonAction))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-09-08 19:27:13 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the scrollbar control
|
|
|
|
nsIFrame* scrollbar;
|
|
|
|
GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (scrollbar == nullptr)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the scrollbars content node
|
|
|
|
nsIContent* content = scrollbar->GetContent();
|
|
|
|
|
|
|
|
static nsIContent::AttrValuesArray strings[] = { &nsGkAtoms::increment,
|
|
|
|
&nsGkAtoms::decrement,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr };
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = mContent->FindAttrValueIn(kNameSpaceID_None,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsGkAtoms::type,
|
|
|
|
strings, eCaseMatters);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t direction;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (index == 0)
|
|
|
|
direction = 1;
|
|
|
|
else if (index == 1)
|
|
|
|
direction = -1;
|
|
|
|
else
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Whether or not to repeat the click action.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool repeat = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Use smooth scrolling by default.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool smoothScroll = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (pressedButtonAction) {
|
|
|
|
case 0:
|
|
|
|
mIncrement = direction * nsSliderFrame::GetIncrement(content);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mIncrement = direction * nsSliderFrame::GetPageIncrement(content);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (direction == -1)
|
|
|
|
mIncrement = -nsSliderFrame::GetCurrentPosition(content);
|
|
|
|
else
|
|
|
|
mIncrement = nsSliderFrame::GetMaxPosition(content) -
|
|
|
|
nsSliderFrame::GetCurrentPosition(content);
|
|
|
|
// Don't repeat or use smooth scrolling if scrolling to beginning or end
|
|
|
|
// of a page.
|
2011-10-17 07:59:28 -07:00
|
|
|
repeat = smoothScroll = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
default:
|
|
|
|
// We were told to ignore this click, or someone assigned a non-standard
|
|
|
|
// value to the button's action.
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// set this attribute so we can style it later
|
|
|
|
nsWeakFrame weakFrame(this);
|
2011-10-17 07:59:28 -07:00
|
|
|
mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-17 15:41:35 -08:00
|
|
|
nsIPresShell::SetCapturingContent(mContent, CAPTURE_IGNOREALLOWED);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (weakFrame.IsAlive()) {
|
|
|
|
DoButtonAction(smoothScroll);
|
|
|
|
}
|
|
|
|
if (repeat)
|
2008-02-14 18:04:34 -08:00
|
|
|
StartRepeat();
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-10-01 20:46:03 -07:00
|
|
|
nsScrollbarButtonFrame::HandleRelease(nsPresContext* aPresContext,
|
|
|
|
WidgetGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIPresShell::SetCapturingContent(nullptr, 0);
|
2011-08-02 20:39:02 -07:00
|
|
|
// we're not active anymore
|
2011-10-17 07:59:28 -07:00
|
|
|
mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::active, true);
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2011-08-02 20:39:02 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-14 18:04:34 -08:00
|
|
|
void nsScrollbarButtonFrame::Notify()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Since this is only going to get called if we're scrolling a page length
|
|
|
|
// or a line increment, we will always use smooth scrolling.
|
2011-11-17 15:41:35 -08:00
|
|
|
if (mCursorOnThis ||
|
|
|
|
LookAndFeel::GetInt(
|
|
|
|
LookAndFeel::eIntID_ScrollbarButtonAutoRepeatBehavior, 0)) {
|
2011-11-17 15:41:35 -08:00
|
|
|
DoButtonAction(true);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-01 20:46:03 -07:00
|
|
|
nsScrollbarButtonFrame::MouseClicked(nsPresContext* aPresContext,
|
|
|
|
WidgetGUIEvent* aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsButtonBoxFrame::MouseClicked(aPresContext, aEvent);
|
|
|
|
//MouseClicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsScrollbarButtonFrame::DoButtonAction(bool aSmoothScroll)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// get the scrollbar control
|
|
|
|
nsIFrame* scrollbar;
|
|
|
|
GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (scrollbar == nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// get the scrollbars content node
|
|
|
|
nsCOMPtr<nsIContent> content = scrollbar->GetContent();
|
|
|
|
|
|
|
|
// get the current pos
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t curpos = nsSliderFrame::GetCurrentPosition(content);
|
|
|
|
int32_t oldpos = curpos;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the max pos
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t maxpos = nsSliderFrame::GetMaxPosition(content);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// increment the given amount
|
|
|
|
if (mIncrement)
|
|
|
|
curpos += mIncrement;
|
|
|
|
|
|
|
|
// make sure the current position is between the current and max positions
|
|
|
|
if (curpos < 0)
|
|
|
|
curpos = 0;
|
|
|
|
else if (curpos > maxpos)
|
|
|
|
curpos = maxpos;
|
|
|
|
|
2011-07-11 07:05:09 -07:00
|
|
|
nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sb) {
|
|
|
|
nsIScrollbarMediator* m = sb->GetScrollbarMediator();
|
|
|
|
if (m) {
|
|
|
|
m->ScrollbarButtonPressed(sb, oldpos, curpos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the current position of the slider.
|
|
|
|
nsAutoString curposStr;
|
|
|
|
curposStr.AppendInt(curpos);
|
|
|
|
|
|
|
|
if (aSmoothScroll)
|
2011-10-17 07:59:28 -07:00
|
|
|
content->SetAttr(kNameSpaceID_None, nsGkAtoms::smooth, NS_LITERAL_STRING("true"), false);
|
|
|
|
content->SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, curposStr, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aSmoothScroll)
|
2011-10-17 07:59:28 -07:00
|
|
|
content->UnsetAttr(kNameSpaceID_None, nsGkAtoms::smooth, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScrollbarButtonFrame::GetChildWithTag(nsPresContext* aPresContext,
|
|
|
|
nsIAtom* atom, nsIFrame* start,
|
|
|
|
nsIFrame*& result)
|
|
|
|
{
|
|
|
|
// recursively search our children
|
2011-08-24 13:54:30 -07:00
|
|
|
nsIFrame* childFrame = start->GetFirstPrincipalChild();
|
2012-07-30 07:20:58 -07:00
|
|
|
while (nullptr != childFrame)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// get the content node
|
|
|
|
nsIContent* child = childFrame->GetContent();
|
|
|
|
|
|
|
|
if (child) {
|
|
|
|
// see if it is the child
|
|
|
|
if (child->Tag() == atom)
|
|
|
|
{
|
|
|
|
result = childFrame;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursive search the child
|
|
|
|
GetChildWithTag(aPresContext, atom, childFrame, result);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (result != nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
childFrame = childFrame->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
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->Tag() == toFind) {
|
|
|
|
result = start;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-23 21:21:15 -08:00
|
|
|
nsScrollbarButtonFrame::DestroyFrom(nsIFrame* aDestructRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// 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.
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2009-12-23 21:21:15 -08:00
|
|
|
nsButtonBoxFrame::DestroyFrom(aDestructRoot);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|