2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dean Tessman <dean_tessman@hotmail.com>
|
2008-02-14 13:45:47 -08:00
|
|
|
* Stan Shebs <stanshebs@earthlink.net>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
//
|
|
|
|
// Eric Vaughan
|
|
|
|
// Netscape Communications
|
|
|
|
//
|
|
|
|
// See documentation in associated header file
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "nsSliderFrame.h"
|
|
|
|
#include "nsStyleContext.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsHTMLParts.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsCSSRendering.h"
|
2007-05-14 02:11:38 -07:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMMouseEvent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsScrollbarButtonFrame.h"
|
2009-01-14 10:21:58 -08:00
|
|
|
#include "nsISliderListener.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScrollbarMediator.h"
|
|
|
|
#include "nsIScrollbarFrame.h"
|
2007-04-11 17:36:40 -07:00
|
|
|
#include "nsILookAndFeel.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsRepeatService.h"
|
|
|
|
#include "nsBoxLayoutState.h"
|
|
|
|
#include "nsSprocketLayout.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
#include "nsDisplayList.h"
|
|
|
|
|
|
|
|
PRBool nsSliderFrame::gMiddlePref = PR_FALSE;
|
|
|
|
PRInt32 nsSliderFrame::gSnapMultiplier;
|
|
|
|
|
|
|
|
// Turn this on if you want to debug slider frames.
|
|
|
|
#undef DEBUG_SLIDER
|
|
|
|
|
|
|
|
static already_AddRefed<nsIContent>
|
|
|
|
GetContentOfBox(nsIBox *aBox)
|
|
|
|
{
|
|
|
|
nsIContent* content = aBox->GetContent();
|
|
|
|
NS_IF_ADDREF(content);
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame*
|
|
|
|
NS_NewSliderFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
|
|
|
|
{
|
|
|
|
return new (aPresShell) nsSliderFrame(aPresShell, aContext);
|
2009-09-12 09:49:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSliderFrame)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsSliderFrame::nsSliderFrame(nsIPresShell* aPresShell, nsStyleContext* aContext):
|
|
|
|
nsBoxFrame(aPresShell, aContext),
|
|
|
|
mCurPos(0),
|
2009-01-14 10:21:58 -08:00
|
|
|
mChange(0),
|
|
|
|
mUserChanged(PR_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop timer
|
|
|
|
nsSliderFrame::~nsSliderFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::Init(nsIContent* aContent,
|
|
|
|
nsIFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow)
|
|
|
|
{
|
|
|
|
nsresult rv = nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
|
|
|
|
|
|
|
|
static PRBool gotPrefs = PR_FALSE;
|
|
|
|
if (!gotPrefs) {
|
|
|
|
gotPrefs = PR_TRUE;
|
|
|
|
|
|
|
|
gMiddlePref = nsContentUtils::GetBoolPref("middlemouse.scrollbarPosition");
|
|
|
|
gSnapMultiplier = nsContentUtils::GetIntPref("slider.snapMultiplier");
|
|
|
|
}
|
|
|
|
|
2009-05-04 07:55:41 -07:00
|
|
|
mCurPos = GetCurrentPosition(aContent);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::RemoveFrame(nsIAtom* aListName,
|
|
|
|
nsIFrame* aOldFrame)
|
|
|
|
{
|
|
|
|
nsresult rv = nsBoxFrame::RemoveFrame(aListName, aOldFrame);
|
2007-05-22 20:48:43 -07:00
|
|
|
if (mFrames.IsEmpty())
|
2007-03-22 10:30:00 -07:00
|
|
|
RemoveListener();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::InsertFrames(nsIAtom* aListName,
|
|
|
|
nsIFrame* aPrevFrame,
|
2009-07-30 10:23:32 -07:00
|
|
|
nsFrameList& aFrameList)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-05-22 20:48:43 -07:00
|
|
|
PRBool wasEmpty = mFrames.IsEmpty();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = nsBoxFrame::InsertFrames(aListName, aPrevFrame, aFrameList);
|
2007-05-22 20:48:43 -07:00
|
|
|
if (wasEmpty)
|
2007-03-22 10:30:00 -07:00
|
|
|
AddListener();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::AppendFrames(nsIAtom* aListName,
|
2009-07-30 10:23:32 -07:00
|
|
|
nsFrameList& aFrameList)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// if we have no children and on was added then make sure we add the
|
|
|
|
// listener
|
2007-05-22 20:48:43 -07:00
|
|
|
PRBool wasEmpty = mFrames.IsEmpty();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = nsBoxFrame::AppendFrames(aListName, aFrameList);
|
2007-05-22 20:48:43 -07:00
|
|
|
if (wasEmpty)
|
2007-03-22 10:30:00 -07:00
|
|
|
AddListener();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSliderFrame::GetCurrentPosition(nsIContent* content)
|
|
|
|
{
|
|
|
|
return GetIntegerAttribute(content, nsGkAtoms::curpos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSliderFrame::GetMinPosition(nsIContent* content)
|
|
|
|
{
|
|
|
|
return GetIntegerAttribute(content, nsGkAtoms::minpos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSliderFrame::GetMaxPosition(nsIContent* content)
|
|
|
|
{
|
|
|
|
return GetIntegerAttribute(content, nsGkAtoms::maxpos, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSliderFrame::GetIncrement(nsIContent* content)
|
|
|
|
{
|
|
|
|
return GetIntegerAttribute(content, nsGkAtoms::increment, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSliderFrame::GetPageIncrement(nsIContent* content)
|
|
|
|
{
|
|
|
|
return GetIntegerAttribute(content, nsGkAtoms::pageincrement, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsSliderFrame::GetIntegerAttribute(nsIContent* content, nsIAtom* atom, PRInt32 defaultValue)
|
|
|
|
{
|
|
|
|
nsAutoString value;
|
|
|
|
content->GetAttr(kNameSpaceID_None, atom, value);
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
PRInt32 error;
|
|
|
|
|
|
|
|
// convert it to an integer
|
|
|
|
defaultValue = value.ToInteger(&error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2009-01-14 10:21:58 -08:00
|
|
|
class nsValueChangedRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsValueChangedRunnable(nsISliderListener* aListener,
|
|
|
|
nsIAtom* aWhich,
|
|
|
|
PRInt32 aValue,
|
|
|
|
PRBool aUserChanged)
|
|
|
|
: mListener(aListener), mWhich(aWhich),
|
|
|
|
mValue(aValue), mUserChanged(aUserChanged)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
2010-03-08 07:45:00 -08:00
|
|
|
return mListener->ValueChanged(nsDependentAtomString(mWhich),
|
|
|
|
mValue, mUserChanged);
|
2009-01-14 10:21:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISliderListener> mListener;
|
|
|
|
nsCOMPtr<nsIAtom> mWhich;
|
|
|
|
PRInt32 mValue;
|
|
|
|
PRBool mUserChanged;
|
|
|
|
};
|
|
|
|
|
2009-05-20 22:27:01 -07:00
|
|
|
class nsDragStateChangedRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDragStateChangedRunnable(nsISliderListener* aListener,
|
|
|
|
PRBool aDragBeginning)
|
|
|
|
: mListener(aListener),
|
|
|
|
mDragBeginning(aDragBeginning)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
return mListener->DragStateChanged(mDragBeginning);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISliderListener> mListener;
|
|
|
|
PRBool mDragBeginning;
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::AttributeChanged(PRInt32 aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
PRInt32 aModType)
|
|
|
|
{
|
|
|
|
nsresult rv = nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute,
|
|
|
|
aModType);
|
|
|
|
// if the current position changes
|
|
|
|
if (aAttribute == nsGkAtoms::curpos) {
|
2007-05-25 03:09:29 -07:00
|
|
|
rv = CurrentPositionChanged(PresContext(), PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to change position");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
} else if (aAttribute == nsGkAtoms::minpos ||
|
|
|
|
aAttribute == nsGkAtoms::maxpos) {
|
|
|
|
// bounds check it.
|
|
|
|
|
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
nsCOMPtr<nsIContent> scrollbar;
|
|
|
|
scrollbar = GetContentOfBox(scrollbarBox);
|
|
|
|
PRInt32 current = GetCurrentPosition(scrollbar);
|
|
|
|
PRInt32 min = GetMinPosition(scrollbar);
|
|
|
|
PRInt32 max = GetMaxPosition(scrollbar);
|
2009-01-14 10:21:58 -08:00
|
|
|
|
|
|
|
// inform the parent <scale> that the minimum or maximum changed
|
|
|
|
nsIFrame* parent = GetParent();
|
|
|
|
if (parent) {
|
|
|
|
nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
|
|
|
|
if (sliderListener) {
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new nsValueChangedRunnable(sliderListener, aAttribute,
|
|
|
|
aAttribute == nsGkAtoms::minpos ? min : max, PR_FALSE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (current < min || current > max)
|
|
|
|
{
|
|
|
|
if (current < min || max < min)
|
|
|
|
current = min;
|
|
|
|
else if (current > max)
|
|
|
|
current = max;
|
|
|
|
|
|
|
|
// set the new position and notify observers
|
2009-01-12 11:20:59 -08:00
|
|
|
nsIScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (scrollbarFrame) {
|
|
|
|
nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
|
|
|
|
if (mediator) {
|
|
|
|
mediator->PositionChanged(scrollbarFrame, GetCurrentPosition(scrollbar), current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString currentStr;
|
|
|
|
currentStr.AppendInt(current);
|
2008-04-14 16:59:21 -07:00
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new nsSetAttrRunnable(scrollbar, nsGkAtoms::curpos, currentStr));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aAttribute == nsGkAtoms::minpos ||
|
|
|
|
aAttribute == nsGkAtoms::maxpos ||
|
|
|
|
aAttribute == nsGkAtoms::pageincrement ||
|
|
|
|
aAttribute == nsGkAtoms::increment) {
|
|
|
|
|
2007-03-30 14:11:41 -07:00
|
|
|
PresContext()->PresShell()->
|
2007-05-06 12:16:51 -07:00
|
|
|
FrameNeedsReflow(this, nsIPresShell::eStyleChange, NS_FRAME_IS_DIRTY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
|
|
|
{
|
|
|
|
if (aBuilder->IsForEventDelivery() && isDraggingThumb()) {
|
|
|
|
// This is EVIL, we shouldn't be messing with event delivery just to get
|
|
|
|
// thumb mouse drag events to arrive at the slider!
|
|
|
|
return aLists.Outlines()->AppendNewToTop(new (aBuilder)
|
|
|
|
nsDisplayEventReceiver(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsBoxFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
|
|
|
{
|
|
|
|
// if we are too small to have a thumb don't paint it.
|
|
|
|
nsIBox* thumb = GetChildBox();
|
|
|
|
|
|
|
|
if (thumb) {
|
|
|
|
nsRect thumbRect(thumb->GetRect());
|
|
|
|
nsMargin m;
|
|
|
|
thumb->GetMargin(m);
|
|
|
|
thumbRect.Inflate(m);
|
|
|
|
|
|
|
|
nsRect crect;
|
|
|
|
GetClientRect(crect);
|
|
|
|
|
|
|
|
if (crect.width < thumbRect.width || crect.height < thumbRect.height)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsBoxFrame::BuildDisplayListForChildren(aBuilder, aDirtyRect, aLists);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::DoLayout(nsBoxLayoutState& aState)
|
|
|
|
{
|
|
|
|
// get the thumb should be our only child
|
|
|
|
nsIBox* thumbBox = GetChildBox();
|
|
|
|
|
|
|
|
if (!thumbBox) {
|
|
|
|
SyncLayout(aState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureOrient();
|
|
|
|
|
|
|
|
#ifdef DEBUG_LAYOUT
|
|
|
|
if (mState & NS_STATE_DEBUG_WAS_SET) {
|
|
|
|
if (mState & NS_STATE_SET_TO_DEBUG)
|
|
|
|
SetDebug(aState, PR_TRUE);
|
|
|
|
else
|
|
|
|
SetDebug(aState, PR_FALSE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// get the content area inside our borders
|
2009-09-03 14:33:55 -07:00
|
|
|
nsRect clientRect;
|
2007-03-22 10:30:00 -07:00
|
|
|
GetClientRect(clientRect);
|
|
|
|
|
|
|
|
// get the scrollbar
|
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
nsCOMPtr<nsIContent> scrollbar;
|
|
|
|
scrollbar = GetContentOfBox(scrollbarBox);
|
|
|
|
|
|
|
|
// get the thumb's pref size
|
|
|
|
nsSize thumbSize = thumbBox->GetPrefSize(aState);
|
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
if (IsHorizontal())
|
2007-03-22 10:30:00 -07:00
|
|
|
thumbSize.height = clientRect.height;
|
|
|
|
else
|
|
|
|
thumbSize.width = clientRect.width;
|
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
PRInt32 curPos = GetCurrentPosition(scrollbar);
|
|
|
|
PRInt32 minPos = GetMinPosition(scrollbar);
|
|
|
|
PRInt32 maxPos = GetMaxPosition(scrollbar);
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 pageIncrement = GetPageIncrement(scrollbar);
|
|
|
|
|
2009-09-16 08:01:36 -07:00
|
|
|
maxPos = NS_MAX(minPos, maxPos);
|
|
|
|
curPos = NS_MAX(minPos, NS_MIN(curPos, maxPos));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
nscoord& availableLength = IsHorizontal() ? clientRect.width : clientRect.height;
|
|
|
|
nscoord& thumbLength = IsHorizontal() ? thumbSize.width : thumbSize.height;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
if ((pageIncrement + maxPos - minPos) > 0 && thumbBox->GetFlex(aState) > 0) {
|
|
|
|
float ratio = float(pageIncrement) / float(maxPos - minPos + pageIncrement);
|
2009-09-16 08:01:36 -07:00
|
|
|
thumbLength = NS_MAX(thumbLength, NSToCoordRound(availableLength * ratio));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-03 14:34:31 -07:00
|
|
|
// Round the thumb's length to device pixels.
|
|
|
|
nsPresContext* presContext = PresContext();
|
|
|
|
thumbLength = presContext->DevPixelsToAppUnits(
|
|
|
|
presContext->AppUnitsToDevPixels(thumbLength));
|
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
// mRatio translates the thumb position in app units to the value.
|
|
|
|
mRatio = (minPos != maxPos) ? float(availableLength - thumbLength) / float(maxPos - minPos) : 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// in reverse mode, curpos is reversed such that lower values are to the
|
|
|
|
// right or bottom and increase leftwards or upwards. In this case, use the
|
|
|
|
// offset from the end instead of the beginning.
|
|
|
|
PRBool reverse = mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
|
|
|
|
nsGkAtoms::reverse, eCaseMatters);
|
2009-09-03 14:33:55 -07:00
|
|
|
nscoord pos = reverse ? (maxPos - curPos) : (curPos - minPos);
|
2007-04-09 15:39:57 -07:00
|
|
|
|
|
|
|
// set the thumb's coord to be the current pos * the ratio.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRect thumbRect(clientRect.x, clientRect.y, thumbSize.width, thumbSize.height);
|
2009-09-03 14:33:55 -07:00
|
|
|
PRInt32& thumbPos = (IsHorizontal() ? thumbRect.x : thumbRect.y);
|
|
|
|
thumbPos += NSToCoordRound(pos * mRatio);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRect oldThumbRect(thumbBox->GetRect());
|
|
|
|
LayoutChildAt(aState, thumbBox, thumbRect);
|
|
|
|
|
|
|
|
SyncLayout(aState);
|
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
// Redraw only if thumb changed size.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (oldThumbRect != thumbRect)
|
|
|
|
Redraw(aState);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::HandleEvent(nsPresContext* aPresContext,
|
|
|
|
nsGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
2009-02-27 02:48:25 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aEventStatus);
|
|
|
|
if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
nsCOMPtr<nsIContent> scrollbar;
|
|
|
|
scrollbar = GetContentOfBox(scrollbarBox);
|
|
|
|
PRBool isHorizontal = IsHorizontal();
|
|
|
|
|
|
|
|
if (isDraggingThumb())
|
|
|
|
{
|
|
|
|
switch (aEvent->message) {
|
|
|
|
case NS_MOUSE_MOVE: {
|
|
|
|
nsPoint eventPoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent,
|
|
|
|
this);
|
|
|
|
if (mChange) {
|
|
|
|
// We're in the process of moving the thumb to the mouse,
|
|
|
|
// but the mouse just moved. Make sure to update our
|
|
|
|
// destination point.
|
|
|
|
mDestinationPoint = eventPoint;
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
|
|
|
StartRepeat();
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-06 21:28:15 -08:00
|
|
|
nscoord pos = isHorizontal ? eventPoint.x : eventPoint.y;
|
|
|
|
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// take our current position and subtract the start location
|
|
|
|
pos -= mDragStart;
|
|
|
|
PRBool isMouseOutsideThumb = PR_FALSE;
|
|
|
|
if (gSnapMultiplier) {
|
|
|
|
nsSize thumbSize = thumbFrame->GetSize();
|
|
|
|
if (isHorizontal) {
|
|
|
|
// horizontal scrollbar - check if mouse is above or below thumb
|
|
|
|
// XXXbz what about looking at the .y of the thumb's rect? Is that
|
|
|
|
// always zero here?
|
|
|
|
if (eventPoint.y < -gSnapMultiplier * thumbSize.height ||
|
|
|
|
eventPoint.y > thumbSize.height +
|
|
|
|
gSnapMultiplier * thumbSize.height)
|
|
|
|
isMouseOutsideThumb = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// vertical scrollbar - check if mouse is left or right of thumb
|
|
|
|
if (eventPoint.x < -gSnapMultiplier * thumbSize.width ||
|
|
|
|
eventPoint.x > thumbSize.width +
|
|
|
|
gSnapMultiplier * thumbSize.width)
|
|
|
|
isMouseOutsideThumb = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isMouseOutsideThumb)
|
|
|
|
{
|
|
|
|
SetCurrentThumbPosition(scrollbar, mThumbStart, PR_FALSE, PR_TRUE, PR_FALSE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set it
|
|
|
|
SetCurrentThumbPosition(scrollbar, pos, PR_FALSE, PR_TRUE, PR_TRUE); // with snapping
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_MOUSE_BUTTON_UP:
|
2007-07-08 00:08:04 -07:00
|
|
|
if (static_cast<nsMouseEvent*>(aEvent)->button == nsMouseEvent::eLeftButton ||
|
|
|
|
(static_cast<nsMouseEvent*>(aEvent)->button == nsMouseEvent::eMiddleButton &&
|
2007-03-22 10:30:00 -07:00
|
|
|
gMiddlePref)) {
|
|
|
|
// stop capturing
|
|
|
|
AddListener();
|
|
|
|
DragThumb(PR_FALSE);
|
|
|
|
if (mChange) {
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2007-03-22 10:30:00 -07:00
|
|
|
mChange = 0;
|
|
|
|
}
|
2007-05-25 03:09:29 -07:00
|
|
|
//we MUST call nsFrame HandleEvent for mouse ups to maintain the selection state and capture state.
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
|
|
|
return NS_OK;
|
|
|
|
} else if ((aEvent->message == NS_MOUSE_BUTTON_DOWN &&
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsMouseEvent*>(aEvent)->button ==
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMouseEvent::eLeftButton &&
|
2008-02-14 13:45:47 -08:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// On Mac the option key inverts the scroll-to-here preference.
|
|
|
|
(static_cast<nsMouseEvent*>(aEvent)->isAlt != GetScrollToClick())) ||
|
|
|
|
#else
|
2009-03-31 11:11:35 -07:00
|
|
|
(static_cast<nsMouseEvent*>(aEvent)->isShift != GetScrollToClick())) ||
|
2008-02-14 13:45:47 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
(gMiddlePref && aEvent->message == NS_MOUSE_BUTTON_DOWN &&
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsMouseEvent*>(aEvent)->button ==
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMouseEvent::eMiddleButton)) {
|
2008-02-06 21:28:15 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPoint eventPoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent,
|
|
|
|
this);
|
|
|
|
nscoord pos = isHorizontal ? eventPoint.x : eventPoint.y;
|
|
|
|
|
2008-02-06 21:28:15 -08:00
|
|
|
// adjust so that the middle of the thumb is placed under the click
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsSize thumbSize = thumbFrame->GetSize();
|
|
|
|
nscoord thumbLength = isHorizontal ? thumbSize.width : thumbSize.height;
|
|
|
|
|
|
|
|
// set it
|
2007-05-25 03:09:29 -07:00
|
|
|
nsWeakFrame weakFrame(this);
|
2008-02-06 21:28:15 -08:00
|
|
|
// should aMaySnap be PR_TRUE here?
|
|
|
|
SetCurrentThumbPosition(scrollbar, pos - thumbLength/2, PR_FALSE, PR_FALSE, PR_FALSE);
|
2007-05-25 03:09:29 -07:00
|
|
|
NS_ENSURE_TRUE(weakFrame.IsAlive(), NS_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DragThumb(PR_TRUE);
|
|
|
|
|
|
|
|
if (isHorizontal)
|
|
|
|
mThumbStart = thumbFrame->GetPosition().x;
|
|
|
|
else
|
|
|
|
mThumbStart = thumbFrame->GetPosition().y;
|
|
|
|
|
|
|
|
mDragStart = pos - mThumbStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX hack until handle release is actually called in nsframe.
|
|
|
|
// if (aEvent->message == NS_MOUSE_EXIT_SYNTH || aEvent->message == NS_MOUSE_RIGHT_BUTTON_UP || aEvent->message == NS_MOUSE_LEFT_BUTTON_UP)
|
|
|
|
// HandleRelease(aPresContext, aEvent, aEventStatus);
|
|
|
|
|
|
|
|
if (aEvent->message == NS_MOUSE_EXIT_SYNTH && mChange)
|
|
|
|
HandleRelease(aPresContext, aEvent, aEventStatus);
|
|
|
|
|
|
|
|
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
|
|
|
}
|
|
|
|
|
2009-03-31 11:11:35 -07:00
|
|
|
// Helper function to collect the "scroll to click" metric. Beware of
|
|
|
|
// caching this, users expect to be able to change the system preference
|
|
|
|
// and see the browser change its behavior immediately.
|
|
|
|
PRBool
|
|
|
|
nsSliderFrame::GetScrollToClick()
|
|
|
|
{
|
|
|
|
// if there is no parent scrollbar, check the movetoclick attribute. If set
|
|
|
|
// to true, always scroll to the click point. If false, never do this.
|
|
|
|
// Otherwise, the default is true on Mac and false on other platforms.
|
|
|
|
if (GetScrollbar() == this)
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
return !mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::movetoclick,
|
|
|
|
nsGkAtoms::_false, eCaseMatters);
|
|
|
|
|
|
|
|
// if there was no scrollbar, always scroll on click
|
|
|
|
PRBool scrollToClick = PR_FALSE;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILookAndFeel> lookNFeel =
|
|
|
|
do_GetService("@mozilla.org/widget/lookandfeel;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 scrollToClickMetric;
|
|
|
|
rv = lookNFeel->GetMetric(nsILookAndFeel::eMetric_ScrollToClick,
|
|
|
|
scrollToClickMetric);
|
|
|
|
if (NS_SUCCEEDED(rv) && scrollToClickMetric == 1)
|
|
|
|
scrollToClick = PR_TRUE;
|
|
|
|
}
|
|
|
|
return scrollToClick;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-31 11:11:35 -07:00
|
|
|
#else
|
|
|
|
return mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::movetoclick,
|
|
|
|
nsGkAtoms::_true, eCaseMatters);
|
|
|
|
return PR_FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIBox*
|
|
|
|
nsSliderFrame::GetScrollbar()
|
|
|
|
{
|
|
|
|
// if we are in a scrollbar then return the scrollbar's content node
|
|
|
|
// if we are not then return ours.
|
|
|
|
nsIFrame* scrollbar;
|
|
|
|
nsScrollbarButtonFrame::GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
|
|
|
|
|
|
|
|
if (scrollbar == nsnull)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
return scrollbar->IsBoxFrame() ? scrollbar : this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-04-09 15:39:57 -07:00
|
|
|
nsSliderFrame::PageUpDown(nscoord change)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// on a page up or down get our page increment. We get this by getting the scrollbar we are in and
|
|
|
|
// asking it for the current position and the page increment. If we are not in a scrollbar we will
|
|
|
|
// get the values from our own node.
|
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
nsCOMPtr<nsIContent> scrollbar;
|
|
|
|
scrollbar = GetContentOfBox(scrollbarBox);
|
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
|
|
|
|
nsGkAtoms::reverse, eCaseMatters))
|
|
|
|
change = -change;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nscoord pageIncrement = GetPageIncrement(scrollbar);
|
|
|
|
PRInt32 curpos = GetCurrentPosition(scrollbar);
|
|
|
|
PRInt32 minpos = GetMinPosition(scrollbar);
|
2007-04-09 15:39:57 -07:00
|
|
|
PRInt32 maxpos = GetMaxPosition(scrollbar);
|
|
|
|
|
|
|
|
// get the new position and make sure it is in bounds
|
2007-08-10 05:45:20 -07:00
|
|
|
PRInt32 newpos = curpos + change * pageIncrement;
|
2007-04-09 15:39:57 -07:00
|
|
|
if (newpos < minpos || maxpos < minpos)
|
|
|
|
newpos = minpos;
|
|
|
|
else if (newpos > maxpos)
|
|
|
|
newpos = maxpos;
|
|
|
|
|
2007-05-25 03:09:29 -07:00
|
|
|
SetCurrentPositionInternal(scrollbar, newpos, PR_TRUE, PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// called when the current position changed and we need to update the thumb's location
|
|
|
|
nsresult
|
2007-05-25 03:09:29 -07:00
|
|
|
nsSliderFrame::CurrentPositionChanged(nsPresContext* aPresContext,
|
|
|
|
PRBool aImmediateRedraw)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
nsCOMPtr<nsIContent> scrollbar;
|
|
|
|
scrollbar = GetContentOfBox(scrollbarBox);
|
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// get the current position
|
2009-09-03 14:33:55 -07:00
|
|
|
PRInt32 curPos = GetCurrentPosition(scrollbar);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// do nothing if the position did not change
|
2009-09-03 14:33:55 -07:00
|
|
|
if (mCurPos == curPos)
|
2007-04-09 15:39:57 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// get our current min and max position from our content node
|
2009-09-03 14:33:55 -07:00
|
|
|
PRInt32 minPos = GetMinPosition(scrollbar);
|
|
|
|
PRInt32 maxPos = GetMaxPosition(scrollbar);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-16 08:01:36 -07:00
|
|
|
maxPos = NS_MAX(minPos, maxPos);
|
|
|
|
curPos = NS_MAX(minPos, NS_MIN(curPos, maxPos));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// get the thumb's rect
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame)
|
|
|
|
return NS_OK; // The thumb may stream in asynchronously via XBL.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
nsRect thumbRect = thumbFrame->GetRect();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
nsRect clientRect;
|
|
|
|
GetClientRect(clientRect);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// figure out the new rect
|
|
|
|
nsRect newThumbRect(thumbRect);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
PRBool reverse = mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
|
|
|
|
nsGkAtoms::reverse, eCaseMatters);
|
2009-09-03 14:33:55 -07:00
|
|
|
nscoord pos = reverse ? (maxPos - curPos) : (curPos - minPos);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
if (IsHorizontal())
|
|
|
|
newThumbRect.x = clientRect.x + NSToCoordRound(pos * mRatio);
|
2007-04-09 15:39:57 -07:00
|
|
|
else
|
2009-09-03 14:33:55 -07:00
|
|
|
newThumbRect.y = clientRect.y + NSToCoordRound(pos * mRatio);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// set the rect
|
|
|
|
thumbFrame->SetRect(newThumbRect);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-07 00:56:38 -08:00
|
|
|
// Redraw the scrollbar
|
2008-09-18 02:47:21 -07:00
|
|
|
InvalidateWithFlags(clientRect, aImmediateRedraw ? INVALIDATE_IMMEDIATE : 0);
|
2008-02-07 00:56:38 -08:00
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
mCurPos = curPos;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-14 10:21:58 -08:00
|
|
|
// inform the parent <scale> if it exists that the value changed
|
|
|
|
nsIFrame* parent = GetParent();
|
|
|
|
if (parent) {
|
|
|
|
nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
|
|
|
|
if (sliderListener) {
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new nsValueChangedRunnable(sliderListener, nsGkAtoms::curpos, mCurPos, mUserChanged));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateAttribute(nsIContent* aScrollbar, nscoord aNewPos, PRBool aNotify, PRBool aIsSmooth) {
|
|
|
|
nsAutoString str;
|
|
|
|
str.AppendInt(aNewPos);
|
|
|
|
|
|
|
|
if (aIsSmooth) {
|
|
|
|
aScrollbar->SetAttr(kNameSpaceID_None, nsGkAtoms::smooth, NS_LITERAL_STRING("true"), PR_FALSE);
|
|
|
|
}
|
|
|
|
aScrollbar->SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, str, aNotify);
|
|
|
|
if (aIsSmooth) {
|
|
|
|
aScrollbar->UnsetAttr(kNameSpaceID_None, nsGkAtoms::smooth, PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 21:28:15 -08:00
|
|
|
// Use this function when you want to set the scroll position via the position
|
|
|
|
// of the scrollbar thumb, e.g. when dragging the slider. This function scrolls
|
2009-09-03 14:33:55 -07:00
|
|
|
// the content in such a way that thumbRect.x/.y becomes aNewThumbPos.
|
2008-02-06 21:28:15 -08:00
|
|
|
void
|
2009-09-03 14:33:55 -07:00
|
|
|
nsSliderFrame::SetCurrentThumbPosition(nsIContent* aScrollbar, nscoord aNewThumbPos,
|
2008-02-06 21:28:15 -08:00
|
|
|
PRBool aIsSmooth, PRBool aImmediateRedraw, PRBool aMaySnap)
|
|
|
|
{
|
|
|
|
nsRect crect;
|
|
|
|
GetClientRect(crect);
|
|
|
|
nscoord offset = IsHorizontal() ? crect.x : crect.y;
|
2009-09-03 14:33:55 -07:00
|
|
|
PRInt32 newPos = NSToIntRound((aNewThumbPos - offset) / mRatio);
|
2008-02-06 21:28:15 -08:00
|
|
|
|
|
|
|
if (aMaySnap && mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::snap,
|
|
|
|
nsGkAtoms::_true, eCaseMatters)) {
|
|
|
|
// If snap="true", then the slider may only be set to min + (increment * x).
|
|
|
|
// Otherwise, the slider may be set to any positive integer.
|
|
|
|
PRInt32 increment = GetIncrement(aScrollbar);
|
2009-09-03 14:33:55 -07:00
|
|
|
newPos = NSToIntRound(newPos / float(increment)) * increment;
|
2008-02-06 21:28:15 -08:00
|
|
|
}
|
|
|
|
|
2009-09-03 14:33:55 -07:00
|
|
|
SetCurrentPosition(aScrollbar, newPos, aIsSmooth, aImmediateRedraw);
|
2008-02-06 21:28:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use this function when you know the target scroll position of the scrolled content.
|
|
|
|
// aNewPos should be passed to this function as a position as if the minpos is 0.
|
2007-04-09 15:39:57 -07:00
|
|
|
// That is, the minpos will be added to the position by this function. In a reverse
|
|
|
|
// direction slider, the newpos should be the distance from the end.
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2008-02-06 21:28:15 -08:00
|
|
|
nsSliderFrame::SetCurrentPosition(nsIContent* aScrollbar, PRInt32 aNewPos,
|
2007-05-25 03:09:29 -07:00
|
|
|
PRBool aIsSmooth, PRBool aImmediateRedraw)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-04-09 15:39:57 -07:00
|
|
|
// get min and max position from our content node
|
2008-02-06 21:28:15 -08:00
|
|
|
PRInt32 minpos = GetMinPosition(aScrollbar);
|
|
|
|
PRInt32 maxpos = GetMaxPosition(aScrollbar);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
// in reverse direction sliders, flip the value so that it goes from
|
|
|
|
// right to left, or bottom to top.
|
|
|
|
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
|
|
|
|
nsGkAtoms::reverse, eCaseMatters))
|
2008-02-06 21:28:15 -08:00
|
|
|
aNewPos = maxpos - aNewPos;
|
2007-04-09 15:39:57 -07:00
|
|
|
else
|
2008-02-06 21:28:15 -08:00
|
|
|
aNewPos += minpos;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the new position and make sure it is in bounds
|
2008-02-06 21:28:15 -08:00
|
|
|
if (aNewPos < minpos || maxpos < minpos)
|
|
|
|
aNewPos = minpos;
|
|
|
|
else if (aNewPos > maxpos)
|
|
|
|
aNewPos = maxpos;
|
2007-04-09 15:39:57 -07:00
|
|
|
|
2008-02-06 21:28:15 -08:00
|
|
|
SetCurrentPositionInternal(aScrollbar, aNewPos, aIsSmooth, aImmediateRedraw);
|
2007-04-09 15:39:57 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-09 15:39:57 -07:00
|
|
|
void
|
2008-02-06 21:28:15 -08:00
|
|
|
nsSliderFrame::SetCurrentPositionInternal(nsIContent* aScrollbar, PRInt32 aNewPos,
|
2007-05-25 03:09:29 -07:00
|
|
|
PRBool aIsSmooth,
|
|
|
|
PRBool aImmediateRedraw)
|
2007-04-09 15:39:57 -07:00
|
|
|
{
|
2007-05-25 03:09:29 -07:00
|
|
|
nsCOMPtr<nsIContent> scrollbar = aScrollbar;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
2009-01-14 10:21:58 -08:00
|
|
|
|
|
|
|
mUserChanged = PR_TRUE;
|
|
|
|
|
2009-01-12 11:20:59 -08:00
|
|
|
nsIScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (scrollbarFrame) {
|
|
|
|
// See if we have a mediator.
|
|
|
|
nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
|
|
|
|
if (mediator) {
|
2007-05-25 03:09:29 -07:00
|
|
|
nsRefPtr<nsPresContext> context = PresContext();
|
|
|
|
nsCOMPtr<nsIContent> content = GetContent();
|
2008-02-06 21:28:15 -08:00
|
|
|
mediator->PositionChanged(scrollbarFrame, GetCurrentPosition(scrollbar), aNewPos);
|
2007-03-22 10:30:00 -07:00
|
|
|
// 'mediator' might be dangling now...
|
2008-02-06 21:28:15 -08:00
|
|
|
UpdateAttribute(scrollbar, aNewPos, PR_FALSE, aIsSmooth);
|
2009-12-24 13:20:06 -08:00
|
|
|
nsIFrame* frame = content->GetPrimaryFrame();
|
|
|
|
if (frame && frame->GetType() == nsGkAtoms::sliderFrame) {
|
|
|
|
static_cast<nsSliderFrame*>(frame)->
|
|
|
|
CurrentPositionChanged(frame->PresContext(), aImmediateRedraw);
|
2007-05-25 03:09:29 -07:00
|
|
|
}
|
2009-01-14 10:21:58 -08:00
|
|
|
mUserChanged = PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 21:28:15 -08:00
|
|
|
UpdateAttribute(scrollbar, aNewPos, PR_TRUE, aIsSmooth);
|
2009-01-14 10:21:58 -08:00
|
|
|
mUserChanged = PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG_SLIDER
|
2008-02-06 21:28:15 -08:00
|
|
|
printf("Current Pos=%d\n",aNewPos);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-25 03:09:29 -07:00
|
|
|
nsIAtom*
|
|
|
|
nsSliderFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::sliderFrame;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::SetInitialChildList(nsIAtom* aListName,
|
2009-07-28 05:53:20 -07:00
|
|
|
nsFrameList& aChildList)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult r = nsBoxFrame::SetInitialChildList(aListName, aChildList);
|
|
|
|
|
|
|
|
AddListener();
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSliderMediator::MouseDown(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
// Only process the event if the thumb is not being dragged.
|
|
|
|
if (mSlider && !mSlider->isDraggingThumb())
|
|
|
|
return mSlider->MouseDown(aMouseEvent);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSliderMediator::MouseUp(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
// Only process the event if the thumb is not being dragged.
|
|
|
|
if (mSlider && !mSlider->isDraggingThumb())
|
|
|
|
return mSlider->MouseUp(aMouseEvent);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSliderFrame::MouseDown(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
2008-02-06 21:28:15 -08:00
|
|
|
#ifdef DEBUG_SLIDER
|
|
|
|
printf("Begin dragging\n");
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMouseEvent> mouseEvent(do_QueryInterface(aMouseEvent));
|
2008-04-24 17:25:15 -07:00
|
|
|
PRUint16 button = 0;
|
|
|
|
mouseEvent->GetButton(&button);
|
|
|
|
if (!(button == 0 || (button == 1 && gMiddlePref)))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
PRBool isHorizontal = IsHorizontal();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRBool scrollToClick = PR_FALSE;
|
2008-04-24 17:25:15 -07:00
|
|
|
#ifndef XP_MACOSX
|
|
|
|
// On Mac there's no scroll-to-here when clicking the thumb
|
2007-03-22 10:30:00 -07:00
|
|
|
mouseEvent->GetShiftKey(&scrollToClick);
|
|
|
|
if (button != 0) {
|
|
|
|
scrollToClick = PR_TRUE;
|
|
|
|
}
|
2008-02-14 13:45:47 -08:00
|
|
|
#endif
|
2007-04-11 17:36:40 -07:00
|
|
|
|
2007-08-02 18:17:24 -07:00
|
|
|
nsPoint pt = nsLayoutUtils::GetDOMEventCoordinatesRelativeTo(mouseEvent,
|
|
|
|
this);
|
|
|
|
nscoord pos = isHorizontal ? pt.x : pt.y;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If shift click or middle button, first
|
|
|
|
// place the middle of the slider thumb under the click
|
2007-05-25 03:09:29 -07:00
|
|
|
nsCOMPtr<nsIContent> scrollbar;
|
2008-02-06 21:28:15 -08:00
|
|
|
nscoord newpos = pos;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (scrollToClick) {
|
|
|
|
// adjust so that the middle of the thumb is placed under the click
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsSize thumbSize = thumbFrame->GetSize();
|
|
|
|
nscoord thumbLength = isHorizontal ? thumbSize.width : thumbSize.height;
|
|
|
|
|
2008-02-06 21:28:15 -08:00
|
|
|
newpos -= (thumbLength/2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
scrollbar = GetContentOfBox(scrollbarBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
DragThumb(PR_TRUE);
|
|
|
|
|
2008-04-24 17:25:15 -07:00
|
|
|
if (scrollToClick) {
|
|
|
|
// should aMaySnap be PR_TRUE here?
|
|
|
|
SetCurrentThumbPosition(scrollbar, newpos, PR_FALSE, PR_FALSE, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isHorizontal)
|
2008-04-24 17:25:15 -07:00
|
|
|
mThumbStart = thumbFrame->GetPosition().x;
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2008-04-24 17:25:15 -07:00
|
|
|
mThumbStart = thumbFrame->GetPosition().y;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mDragStart = pos - mThumbStart;
|
2008-02-06 21:28:15 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG_SLIDER
|
|
|
|
printf("Pressed mDragStart=%d\n",mDragStart);
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSliderFrame::MouseUp(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
2008-02-06 21:28:15 -08:00
|
|
|
#ifdef DEBUG_SLIDER
|
|
|
|
printf("Finish dragging\n");
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSliderFrame::DragThumb(PRBool aGrabMouseEvents)
|
|
|
|
{
|
2009-05-20 22:27:01 -07:00
|
|
|
// inform the parent <scale> that a drag is beginning or ending
|
|
|
|
nsIFrame* parent = GetParent();
|
|
|
|
if (parent) {
|
|
|
|
nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
|
|
|
|
if (sliderListener) {
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
new nsDragStateChangedRunnable(sliderListener, aGrabMouseEvents));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-13 06:13:16 -07:00
|
|
|
nsIPresShell::SetCapturingContent(aGrabMouseEvents ? GetContent() : nsnull,
|
|
|
|
aGrabMouseEvents ? CAPTURE_IGNOREALLOWED : 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsSliderFrame::isDraggingThumb()
|
|
|
|
{
|
2009-09-13 06:13:16 -07:00
|
|
|
return (nsIPresShell::GetCapturingContent() == GetContent());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSliderFrame::AddListener()
|
|
|
|
{
|
|
|
|
if (!mMediator) {
|
|
|
|
mMediator = new nsSliderMediator(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (thumbFrame) {
|
2007-05-14 02:11:38 -07:00
|
|
|
thumbFrame->GetContent()->
|
|
|
|
AddEventListenerByIID(mMediator, NS_GET_IID(nsIDOMMouseListener));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSliderFrame::RemoveListener()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mMediator, "No listener was ever added!!");
|
|
|
|
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame)
|
|
|
|
return;
|
|
|
|
|
2007-05-14 02:11:38 -07:00
|
|
|
thumbFrame->GetContent()->
|
|
|
|
RemoveEventListenerByIID(mMediator, NS_GET_IID(nsIDOMMouseListener));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::HandlePress(nsPresContext* aPresContext,
|
|
|
|
nsGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
2008-02-14 13:45:47 -08:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// On Mac the option key inverts the scroll-to-here preference.
|
|
|
|
if (((nsMouseEvent *)aEvent)->isAlt != GetScrollToClick())
|
|
|
|
#else
|
2009-03-31 11:11:35 -07:00
|
|
|
if (((nsMouseEvent *)aEvent)->isShift != GetScrollToClick())
|
2008-02-14 13:45:47 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame) // display:none?
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsRect thumbRect = thumbFrame->GetRect();
|
|
|
|
|
|
|
|
nscoord change = 1;
|
|
|
|
nsPoint eventPoint = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent,
|
|
|
|
this);
|
|
|
|
if (IsHorizontal() ? eventPoint.x < thumbRect.x
|
|
|
|
: eventPoint.y < thumbRect.y)
|
|
|
|
change = -1;
|
|
|
|
|
|
|
|
mChange = change;
|
|
|
|
DragThumb(PR_TRUE);
|
|
|
|
mDestinationPoint = eventPoint;
|
2008-02-14 18:04:34 -08:00
|
|
|
StartRepeat();
|
2007-05-25 03:09:29 -07:00
|
|
|
PageUpDown(change);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSliderFrame::HandleRelease(nsPresContext* aPresContext,
|
|
|
|
nsGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-23 21:21:15 -08:00
|
|
|
nsSliderFrame::DestroyFrom(nsIFrame* aDestructRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// tell our mediator if we have one we are gone.
|
|
|
|
if (mMediator) {
|
|
|
|
mMediator->SetSlider(nsnull);
|
|
|
|
mMediator = nsnull;
|
|
|
|
}
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// call base class Destroy()
|
2009-12-23 21:21:15 -08:00
|
|
|
nsBoxFrame::DestroyFrom(aDestructRoot);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSize
|
|
|
|
nsSliderFrame::GetPrefSize(nsBoxLayoutState& aState)
|
|
|
|
{
|
|
|
|
EnsureOrient();
|
|
|
|
return nsBoxFrame::GetPrefSize(aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSize
|
|
|
|
nsSliderFrame::GetMinSize(nsBoxLayoutState& aState)
|
|
|
|
{
|
|
|
|
EnsureOrient();
|
|
|
|
|
|
|
|
// our min size is just our borders and padding
|
|
|
|
return nsBox::GetMinSize(aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSize
|
|
|
|
nsSliderFrame::GetMaxSize(nsBoxLayoutState& aState)
|
|
|
|
{
|
|
|
|
EnsureOrient();
|
|
|
|
return nsBoxFrame::GetMaxSize(aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSliderFrame::EnsureOrient()
|
|
|
|
{
|
|
|
|
nsIBox* scrollbarBox = GetScrollbar();
|
|
|
|
|
|
|
|
PRBool isHorizontal = (scrollbarBox->GetStateBits() & NS_STATE_IS_HORIZONTAL) != 0;
|
|
|
|
if (isHorizontal)
|
|
|
|
mState |= NS_STATE_IS_HORIZONTAL;
|
|
|
|
else
|
|
|
|
mState &= ~NS_STATE_IS_HORIZONTAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-14 18:04:34 -08:00
|
|
|
void nsSliderFrame::Notify(void)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PRBool stop = PR_FALSE;
|
|
|
|
|
|
|
|
nsIFrame* thumbFrame = mFrames.FirstChild();
|
|
|
|
if (!thumbFrame) {
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsRect thumbRect = thumbFrame->GetRect();
|
|
|
|
|
|
|
|
PRBool isHorizontal = IsHorizontal();
|
|
|
|
|
|
|
|
// See if the thumb has moved past our destination point.
|
|
|
|
// if it has we want to stop.
|
|
|
|
if (isHorizontal) {
|
|
|
|
if (mChange < 0) {
|
|
|
|
if (thumbRect.x < mDestinationPoint.x)
|
|
|
|
stop = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
if (thumbRect.x + thumbRect.width > mDestinationPoint.x)
|
|
|
|
stop = PR_TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mChange < 0) {
|
|
|
|
if (thumbRect.y < mDestinationPoint.y)
|
|
|
|
stop = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
if (thumbRect.y + thumbRect.height > mDestinationPoint.y)
|
|
|
|
stop = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (stop) {
|
2008-02-14 18:04:34 -08:00
|
|
|
StopRepeat();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-04-09 15:39:57 -07:00
|
|
|
PageUpDown(mChange);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-06 05:28:34 -07:00
|
|
|
NS_IMPL_ISUPPORTS2(nsSliderMediator,
|
|
|
|
nsIDOMMouseListener,
|
|
|
|
nsIDOMEventListener)
|