2014-06-04 13:57:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
#include "prlog.h"
|
2014-06-04 13:57:00 -07:00
|
|
|
#include "SelectionCarets.h"
|
|
|
|
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "nsBidiPresUtils.h"
|
|
|
|
#include "nsCanvasFrame.h"
|
|
|
|
#include "nsCaret.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsDOMTokenList.h"
|
2014-10-16 14:17:00 -07:00
|
|
|
#include "nsFocusManager.h"
|
2014-06-04 13:57:00 -07:00
|
|
|
#include "nsFrame.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMNodeFilter.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
#include "nsView.h"
|
|
|
|
#include "mozilla/dom/DOMRect.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
2014-09-23 03:37:00 -07:00
|
|
|
#include "mozilla/dom/ScrollViewChangeEvent.h"
|
2014-06-04 13:57:00 -07:00
|
|
|
#include "mozilla/dom/Selection.h"
|
|
|
|
#include "mozilla/dom/TreeWalker.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/TouchEvents.h"
|
|
|
|
#include "TouchCaret.h"
|
2014-08-05 22:19:27 -07:00
|
|
|
#include "nsFrameSelection.h"
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-09-23 03:37:00 -07:00
|
|
|
using namespace mozilla::dom;
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo* gSelectionCaretsLog;
|
|
|
|
static const char* kSelectionCaretsLogModuleName = "SelectionCarets";
|
|
|
|
|
|
|
|
// To enable all the SELECTIONCARETS_LOG print statements, set the environment
|
|
|
|
// variable NSPR_LOG_MODULES=SelectionCarets:5
|
|
|
|
#define SELECTIONCARETS_LOG(message, ...) \
|
|
|
|
PR_LOG(gSelectionCaretsLog, PR_LOG_DEBUG, \
|
|
|
|
("SelectionCarets (%p): %s:%d : " message "\n", this, __FUNCTION__, \
|
|
|
|
__LINE__, ##__VA_ARGS__));
|
|
|
|
|
|
|
|
#define SELECTIONCARETS_LOG_STATIC(message, ...) \
|
|
|
|
PR_LOG(gSelectionCaretsLog, PR_LOG_DEBUG, \
|
|
|
|
("SelectionCarets: %s:%d : " message "\n", __FUNCTION__, __LINE__, \
|
|
|
|
##__VA_ARGS__));
|
|
|
|
#else
|
|
|
|
#define SELECTIONCARETS_LOG(message, ...)
|
|
|
|
#define SELECTIONCARETS_LOG_STATIC(message, ...)
|
|
|
|
#endif // #ifdef PR_LOGGING
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
// We treat mouse/touch move as "REAL" move event once its move distance
|
|
|
|
// exceed this value, in CSS pixel.
|
|
|
|
static const int32_t kMoveStartTolerancePx = 5;
|
|
|
|
// Time for trigger scroll end event, in miliseconds.
|
|
|
|
static const int32_t kScrollEndTimerDelay = 300;
|
2014-09-28 14:49:00 -07:00
|
|
|
// Read from preference "selectioncaret.noneditable". Indicate whether support
|
|
|
|
// non-editable fields selection or not. We have stable state for editable
|
|
|
|
// fields selection now. And we don't want to break this stable state when
|
|
|
|
// enabling non-editable support. So I add a pref to control to support or
|
|
|
|
// not. Once non-editable fields support is stable. We should remove this
|
|
|
|
// pref.
|
|
|
|
static bool kSupportNonEditableFields = false;
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(SelectionCarets,
|
|
|
|
nsISelectionListener,
|
|
|
|
nsIScrollObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
/*static*/ int32_t SelectionCarets::sSelectionCaretsInflateSize = 0;
|
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
SelectionCarets::SelectionCarets(nsIPresShell* aPresShell)
|
|
|
|
: mPresShell(aPresShell)
|
|
|
|
, mActiveTouchId(-1)
|
2014-06-04 13:57:00 -07:00
|
|
|
, mCaretCenterToDownPointOffsetY(0)
|
|
|
|
, mDragMode(NONE)
|
2014-09-23 03:37:00 -07:00
|
|
|
, mAPZenabled(false)
|
2014-06-04 13:57:00 -07:00
|
|
|
, mEndCaretVisible(false)
|
2014-09-23 03:37:00 -07:00
|
|
|
, mStartCaretVisible(false)
|
|
|
|
, mVisible(false)
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gSelectionCaretsLog) {
|
|
|
|
gSelectionCaretsLog = PR_NewLogModule(kSelectionCaretsLogModuleName);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Constructor, PresShell=%p", mPresShell);
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
static bool addedPref = false;
|
|
|
|
if (!addedPref) {
|
|
|
|
Preferences::AddIntVarCache(&sSelectionCaretsInflateSize,
|
|
|
|
"selectioncaret.inflatesize.threshold");
|
2014-09-28 14:49:00 -07:00
|
|
|
Preferences::AddBoolVarCache(&kSupportNonEditableFields,
|
|
|
|
"selectioncaret.noneditable");
|
2014-06-04 13:57:00 -07:00
|
|
|
addedPref = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectionCarets::~SelectionCarets()
|
|
|
|
{
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Destructor");
|
2014-06-04 13:57:00 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mLongTapDetectorTimer) {
|
|
|
|
mLongTapDetectorTimer->Cancel();
|
|
|
|
mLongTapDetectorTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mScrollEndDetectorTimer) {
|
|
|
|
mScrollEndDetectorTimer->Cancel();
|
|
|
|
mScrollEndDetectorTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPresShell = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus
|
|
|
|
SelectionCarets::HandleEvent(WidgetEvent* aEvent)
|
|
|
|
{
|
|
|
|
WidgetMouseEvent *mouseEvent = aEvent->AsMouseEvent();
|
|
|
|
if (mouseEvent && mouseEvent->reason == WidgetMouseEvent::eSynthesized) {
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetTouchEvent *touchEvent = aEvent->AsTouchEvent();
|
|
|
|
nsIntPoint movePoint;
|
|
|
|
int32_t nowTouchId = -1;
|
|
|
|
if (touchEvent && !touchEvent->touches.IsEmpty()) {
|
|
|
|
// If touch happened, just grab event with same identifier
|
|
|
|
if (mActiveTouchId >= 0) {
|
|
|
|
for (uint32_t i = 0; i < touchEvent->touches.Length(); ++i) {
|
|
|
|
if (touchEvent->touches[i]->Identifier() == mActiveTouchId) {
|
|
|
|
movePoint = touchEvent->touches[i]->mRefPoint;
|
|
|
|
nowTouchId = touchEvent->touches[i]->Identifier();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not found, consume it
|
|
|
|
if (nowTouchId == -1) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
movePoint = touchEvent->touches[0]->mRefPoint;
|
|
|
|
nowTouchId = touchEvent->touches[0]->Identifier();
|
|
|
|
}
|
|
|
|
} else if (mouseEvent) {
|
|
|
|
movePoint = LayoutDeviceIntPoint::ToUntyped(mouseEvent->AsGUIEvent()->refPoint);
|
|
|
|
}
|
|
|
|
|
2014-10-28 15:25:00 -07:00
|
|
|
// Get event coordinate relative to root frame
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
if (!rootFrame) {
|
2014-06-04 13:57:00 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
2014-10-28 15:25:00 -07:00
|
|
|
nsPoint ptInRoot =
|
|
|
|
nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, movePoint, rootFrame);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
if (aEvent->message == NS_TOUCH_START ||
|
|
|
|
(aEvent->message == NS_MOUSE_BUTTON_DOWN &&
|
|
|
|
mouseEvent->button == WidgetMouseEvent::eLeftButton)) {
|
|
|
|
// If having a active touch, ignore other touch down event
|
|
|
|
if (aEvent->message == NS_TOUCH_START && mActiveTouchId >= 0) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
mActiveTouchId = nowTouchId;
|
2014-10-28 15:25:00 -07:00
|
|
|
mDownPoint = ptInRoot;
|
|
|
|
if (IsOnStartFrame(ptInRoot)) {
|
2014-06-04 13:57:00 -07:00
|
|
|
mDragMode = START_FRAME;
|
2014-10-28 15:25:00 -07:00
|
|
|
mCaretCenterToDownPointOffsetY = GetCaretYCenterPosition() - ptInRoot.y;
|
2014-06-04 13:57:00 -07:00
|
|
|
SetSelectionDirection(false);
|
2014-08-03 21:26:00 -07:00
|
|
|
SetSelectionDragState(true);
|
2014-06-04 13:57:00 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2014-10-28 15:25:00 -07:00
|
|
|
} else if (IsOnEndFrame(ptInRoot)) {
|
2014-06-04 13:57:00 -07:00
|
|
|
mDragMode = END_FRAME;
|
2014-10-28 15:25:00 -07:00
|
|
|
mCaretCenterToDownPointOffsetY = GetCaretYCenterPosition() - ptInRoot.y;
|
2014-06-04 13:57:00 -07:00
|
|
|
SetSelectionDirection(true);
|
2014-08-03 21:26:00 -07:00
|
|
|
SetSelectionDragState(true);
|
2014-06-04 13:57:00 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
} else {
|
|
|
|
mDragMode = NONE;
|
|
|
|
mActiveTouchId = -1;
|
|
|
|
SetVisibility(false);
|
|
|
|
LaunchLongTapDetector();
|
|
|
|
}
|
|
|
|
} else if (aEvent->message == NS_TOUCH_END ||
|
|
|
|
aEvent->message == NS_TOUCH_CANCEL ||
|
|
|
|
aEvent->message == NS_MOUSE_BUTTON_UP) {
|
|
|
|
CancelLongTapDetector();
|
|
|
|
if (mDragMode != NONE) {
|
|
|
|
// Only care about same id
|
|
|
|
if (mActiveTouchId == nowTouchId) {
|
2014-08-03 21:26:00 -07:00
|
|
|
SetSelectionDragState(false);
|
2014-06-04 13:57:00 -07:00
|
|
|
mDragMode = NONE;
|
|
|
|
mActiveTouchId = -1;
|
|
|
|
}
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
} else if (aEvent->message == NS_TOUCH_MOVE ||
|
|
|
|
aEvent->message == NS_MOUSE_MOVE) {
|
|
|
|
if (mDragMode == START_FRAME || mDragMode == END_FRAME) {
|
|
|
|
if (mActiveTouchId == nowTouchId) {
|
2014-10-28 15:25:00 -07:00
|
|
|
ptInRoot.y += mCaretCenterToDownPointOffsetY;
|
|
|
|
return DragSelection(ptInRoot);
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-28 15:25:00 -07:00
|
|
|
nsPoint delta = mDownPoint - ptInRoot;
|
2014-06-04 13:57:00 -07:00
|
|
|
if (NS_hypot(delta.x, delta.y) >
|
|
|
|
nsPresContext::AppUnitsPerCSSPixel() * kMoveStartTolerancePx) {
|
|
|
|
CancelLongTapDetector();
|
|
|
|
}
|
|
|
|
} else if (aEvent->message == NS_MOUSE_MOZLONGTAP) {
|
|
|
|
if (!mVisible) {
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("SelectWord from APZ");
|
2014-06-04 13:57:00 -07:00
|
|
|
SelectWord();
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetElementVisibility(dom::Element* aElement, bool aVisible)
|
|
|
|
{
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!aElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
ErrorResult err;
|
|
|
|
aElement->ClassList()->Toggle(NS_LITERAL_STRING("hidden"),
|
|
|
|
dom::Optional<bool>(!aVisible), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetVisibility(bool aVisible)
|
|
|
|
{
|
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVisible == aVisible) {
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Set visibility %s, same as the old one",
|
|
|
|
(aVisible ? "shown" : "hidden"));
|
2014-06-04 13:57:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-10-28 17:16:00 -07:00
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
mVisible = aVisible;
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Set visibility %s", (mVisible ? "shown" : "hidden"));
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
dom::Element* startElement = mPresShell->GetSelectionCaretsStartElement();
|
|
|
|
SetElementVisibility(startElement, mVisible && mStartCaretVisible);
|
|
|
|
|
|
|
|
dom::Element* endElement = mPresShell->GetSelectionCaretsEndElement();
|
|
|
|
SetElementVisibility(endElement, mVisible && mEndCaretVisible);
|
|
|
|
|
|
|
|
// We must call SetHasTouchCaret() in order to get APZC to wait until the
|
|
|
|
// event has been round-tripped and check whether it has been handled,
|
|
|
|
// otherwise B2G will end up panning the document when the user tries to drag
|
|
|
|
// selection caret.
|
|
|
|
mPresShell->SetMayHaveTouchCaret(mVisible);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetStartFrameVisibility(bool aVisible)
|
|
|
|
{
|
|
|
|
mStartCaretVisible = aVisible;
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Set start frame visibility %s",
|
|
|
|
(mStartCaretVisible ? "shown" : "hidden"));
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsStartElement();
|
|
|
|
SetElementVisibility(element, mVisible && mStartCaretVisible);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetEndFrameVisibility(bool aVisible)
|
|
|
|
{
|
|
|
|
mEndCaretVisible = aVisible;
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Set end frame visibility %s",
|
|
|
|
(mStartCaretVisible ? "shown" : "hidden"));
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsEndElement();
|
|
|
|
SetElementVisibility(element, mVisible && mEndCaretVisible);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetTilted(bool aIsTilt)
|
|
|
|
{
|
|
|
|
dom::Element* startElement = mPresShell->GetSelectionCaretsStartElement();
|
|
|
|
dom::Element* endElement = mPresShell->GetSelectionCaretsEndElement();
|
2014-08-24 17:50:00 -07:00
|
|
|
|
|
|
|
if (!startElement || !endElement) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Set tilted selection carets %s",
|
|
|
|
(aIsTilt ? "enabled" : "disabled"));
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
ErrorResult err;
|
|
|
|
startElement->ClassList()->Toggle(NS_LITERAL_STRING("tilt"),
|
|
|
|
dom::Optional<bool>(aIsTilt), err);
|
|
|
|
|
|
|
|
endElement->ClassList()->Toggle(NS_LITERAL_STRING("tilt"),
|
|
|
|
dom::Optional<bool>(aIsTilt), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetCaretDirection(dom::Element* aElement, bool aIsRight)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
|
|
|
|
ErrorResult err;
|
|
|
|
if (aIsRight) {
|
|
|
|
aElement->ClassList()->Add(NS_LITERAL_STRING("moz-selectioncaret-right"), err);
|
|
|
|
aElement->ClassList()->Remove(NS_LITERAL_STRING("moz-selectioncaret-left"), err);
|
|
|
|
} else {
|
|
|
|
aElement->ClassList()->Add(NS_LITERAL_STRING("moz-selectioncaret-left"), err);
|
|
|
|
aElement->ClassList()->Remove(NS_LITERAL_STRING("moz-selectioncaret-right"), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsRightToLeft(nsIFrame* aFrame)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFrame);
|
|
|
|
|
|
|
|
return aFrame->IsFrameOfType(nsIFrame::eLineParticipant) ?
|
|
|
|
(nsBidiPresUtils::GetFrameEmbeddingLevel(aFrame) & 1) :
|
|
|
|
aFrame->StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-09-28 14:50:00 -07:00
|
|
|
* Reduce rect to 1 css pixel width along either left or right edge base on
|
2014-06-04 13:57:00 -07:00
|
|
|
* aToRightEdge parameter.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ReduceRectToVerticalEdge(nsRect& aRect, bool aToRightEdge)
|
|
|
|
{
|
|
|
|
if (aToRightEdge) {
|
2014-09-28 14:50:00 -07:00
|
|
|
aRect.x = aRect.XMost() - AppUnitsPerCSSPixel();
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
2014-09-28 14:50:00 -07:00
|
|
|
aRect.width = AppUnitsPerCSSPixel();
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsIFrame*
|
|
|
|
FindFirstNodeWithFrame(nsIDocument* aDocument,
|
|
|
|
nsRange* aRange,
|
|
|
|
nsFrameSelection* aFrameSelection,
|
|
|
|
bool aBackward,
|
|
|
|
int& aOutOffset)
|
|
|
|
{
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!aDocument || !aRange || !aFrameSelection) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> startNode =
|
|
|
|
do_QueryInterface(aBackward ? aRange->GetEndParent() : aRange->GetStartParent());
|
|
|
|
nsCOMPtr<nsINode> endNode =
|
|
|
|
do_QueryInterface(aBackward ? aRange->GetStartParent() : aRange->GetEndParent());
|
|
|
|
int32_t offset = aBackward ? aRange->EndOffset() : aRange->StartOffset();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> startContent = do_QueryInterface(startNode);
|
2014-08-05 22:19:27 -07:00
|
|
|
CaretAssociationHint hintStart =
|
2014-09-28 14:48:00 -07:00
|
|
|
aBackward ? CARET_ASSOCIATE_BEFORE : CARET_ASSOCIATE_AFTER;
|
2014-06-04 13:57:00 -07:00
|
|
|
nsIFrame* startFrame = aFrameSelection->GetFrameForNodeOffset(startContent,
|
|
|
|
offset,
|
|
|
|
hintStart,
|
|
|
|
&aOutOffset);
|
|
|
|
|
|
|
|
if (startFrame) {
|
|
|
|
return startFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult err;
|
|
|
|
nsRefPtr<dom::TreeWalker> walker =
|
|
|
|
aDocument->CreateTreeWalker(*startNode,
|
|
|
|
nsIDOMNodeFilter::SHOW_ALL,
|
|
|
|
nullptr,
|
|
|
|
err);
|
|
|
|
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!walker) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
startFrame = startContent ? startContent->GetPrimaryFrame() : nullptr;
|
|
|
|
while (!startFrame && startNode != endNode) {
|
|
|
|
if (aBackward) {
|
|
|
|
startNode = walker->PreviousNode(err);
|
|
|
|
} else {
|
|
|
|
startNode = walker->NextNode(err);
|
|
|
|
}
|
2014-10-16 14:17:00 -07:00
|
|
|
|
|
|
|
if (!startNode) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
startContent = do_QueryInterface(startNode);
|
|
|
|
startFrame = startContent ? startContent->GetPrimaryFrame() : nullptr;
|
|
|
|
}
|
|
|
|
return startFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::UpdateSelectionCarets()
|
|
|
|
{
|
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
|
|
|
if (!selection) {
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Cannot get selection!");
|
2014-06-04 13:57:00 -07:00
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:49:00 -07:00
|
|
|
if (selection->IsCollapsed()) {
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Selection is collapsed!");
|
2014-06-04 13:57:00 -07:00
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:49:00 -07:00
|
|
|
int32_t rangeCount = selection->GetRangeCount();
|
|
|
|
nsRefPtr<nsRange> firstRange = selection->GetRangeAt(0);
|
|
|
|
nsRefPtr<nsRange> lastRange = selection->GetRangeAt(rangeCount - 1);
|
|
|
|
|
|
|
|
nsLayoutUtils::FirstAndLastRectCollector collectorStart;
|
|
|
|
nsRange::CollectClientRects(&collectorStart, firstRange,
|
|
|
|
firstRange->GetStartParent(), firstRange->StartOffset(),
|
|
|
|
firstRange->GetEndParent(), firstRange->EndOffset(), true, true);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-23 10:49:00 -07:00
|
|
|
nsLayoutUtils::FirstAndLastRectCollector collectorEnd;
|
|
|
|
nsRange::CollectClientRects(&collectorEnd, lastRange,
|
|
|
|
lastRange->GetStartParent(), lastRange->StartOffset(),
|
|
|
|
lastRange->GetEndParent(), lastRange->EndOffset(), true, true);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
nsIFrame* canvasFrame = mPresShell->GetCanvasFrame();
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
|
|
|
|
if (!canvasFrame || !rootFrame) {
|
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check start and end frame is rtl or ltr text
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-06-04 13:57:00 -07:00
|
|
|
int32_t startOffset;
|
|
|
|
nsIFrame* startFrame = FindFirstNodeWithFrame(mPresShell->GetDocument(),
|
2014-10-23 10:49:00 -07:00
|
|
|
firstRange, fs, false, startOffset);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
int32_t endOffset;
|
|
|
|
nsIFrame* endFrame = FindFirstNodeWithFrame(mPresShell->GetDocument(),
|
2014-10-23 10:49:00 -07:00
|
|
|
lastRange, fs, true, endOffset);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
if (!startFrame || !endFrame) {
|
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
// If frame isn't editable and we don't support non-editable fields, bail
|
|
|
|
// out.
|
|
|
|
if (!kSupportNonEditableFields &&
|
|
|
|
(!startFrame->GetContent()->IsEditable() ||
|
|
|
|
!endFrame->GetContent()->IsEditable())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
// Check if startFrame is after endFrame.
|
|
|
|
if (nsLayoutUtils::CompareTreePosition(startFrame, endFrame) > 0) {
|
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startFrameIsRTL = IsRightToLeft(startFrame);
|
|
|
|
bool endFrameIsRTL = IsRightToLeft(endFrame);
|
|
|
|
|
|
|
|
// If start frame is LTR, then place start caret in first rect's leftmost
|
|
|
|
// otherwise put it to first rect's rightmost.
|
2014-10-23 10:49:00 -07:00
|
|
|
ReduceRectToVerticalEdge(collectorStart.mFirstRect, startFrameIsRTL);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
// Contrary to start frame, if end frame is LTR, put end caret to last
|
|
|
|
// rect's rightmost position, otherwise, put it to last rect's leftmost.
|
2014-10-23 10:49:00 -07:00
|
|
|
ReduceRectToVerticalEdge(collectorEnd.mLastRect, !endFrameIsRTL);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
nsAutoTArray<nsIFrame*, 16> hitFramesInFirstRect;
|
2014-10-14 19:43:29 -07:00
|
|
|
nsLayoutUtils::GetFramesForArea(rootFrame,
|
2014-10-23 10:49:00 -07:00
|
|
|
collectorStart.mFirstRect,
|
2014-10-16 14:17:00 -07:00
|
|
|
hitFramesInFirstRect,
|
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION |
|
|
|
|
nsLayoutUtils::IGNORE_CROSS_DOC |
|
|
|
|
nsLayoutUtils::IGNORE_ROOT_SCROLL_FRAME);
|
|
|
|
|
|
|
|
nsAutoTArray<nsIFrame*, 16> hitFramesInLastRect;
|
2014-10-14 19:43:29 -07:00
|
|
|
nsLayoutUtils::GetFramesForArea(rootFrame,
|
2014-10-23 10:49:00 -07:00
|
|
|
collectorEnd.mLastRect,
|
2014-10-16 14:17:00 -07:00
|
|
|
hitFramesInLastRect,
|
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION |
|
|
|
|
nsLayoutUtils::IGNORE_CROSS_DOC |
|
|
|
|
nsLayoutUtils::IGNORE_ROOT_SCROLL_FRAME);
|
|
|
|
|
|
|
|
SetStartFrameVisibility(hitFramesInFirstRect.Contains(startFrame));
|
|
|
|
SetEndFrameVisibility(hitFramesInLastRect.Contains(endFrame));
|
|
|
|
|
2014-10-23 10:49:00 -07:00
|
|
|
nsLayoutUtils::TransformRect(rootFrame, canvasFrame, collectorStart.mFirstRect);
|
|
|
|
nsLayoutUtils::TransformRect(rootFrame, canvasFrame, collectorEnd.mLastRect);
|
2014-10-14 19:43:29 -07:00
|
|
|
|
2014-10-23 10:49:00 -07:00
|
|
|
SetStartFramePos(collectorStart.mFirstRect.BottomLeft());
|
|
|
|
SetEndFramePos(collectorEnd.mLastRect.BottomRight());
|
2014-06-04 13:57:00 -07:00
|
|
|
SetVisibility(true);
|
|
|
|
|
|
|
|
// If range select only one character, append tilt class name to it.
|
|
|
|
bool isTilt = false;
|
2014-07-15 13:44:00 -07:00
|
|
|
if (startFrame && endFrame) {
|
|
|
|
// In this case <textarea>abc</textarea> and we select 'c' character,
|
|
|
|
// EndContent would be HTMLDivElement and mResultContent which get by
|
|
|
|
// calling startFrame->PeekOffset() with selecting next cluster would be
|
|
|
|
// TextNode. Although the position is same, nsContentUtils::ComparePoints
|
|
|
|
// still shows HTMLDivElement is after TextNode. So that we cannot use
|
|
|
|
// EndContent or StartContent to compare with result of PeekOffset().
|
|
|
|
// So we compare between next charater of startFrame and previous character
|
|
|
|
// of endFrame.
|
|
|
|
nsPeekOffsetStruct posNext(eSelectCluster,
|
|
|
|
eDirNext,
|
|
|
|
startOffset,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
true, //limit on scrolled views
|
|
|
|
false,
|
|
|
|
false);
|
|
|
|
|
|
|
|
nsPeekOffsetStruct posPrev(eSelectCluster,
|
|
|
|
eDirPrevious,
|
|
|
|
endOffset,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
true, //limit on scrolled views
|
|
|
|
false,
|
|
|
|
false);
|
|
|
|
startFrame->PeekOffset(&posNext);
|
|
|
|
endFrame->PeekOffset(&posPrev);
|
|
|
|
|
|
|
|
if (posNext.mResultContent && posPrev.mResultContent &&
|
|
|
|
nsContentUtils::ComparePoints(posNext.mResultContent, posNext.mContentOffset,
|
|
|
|
posPrev.mResultContent, posPrev.mContentOffset) > 0) {
|
2014-06-04 13:57:00 -07:00
|
|
|
isTilt = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCaretDirection(mPresShell->GetSelectionCaretsStartElement(), startFrameIsRTL);
|
|
|
|
SetCaretDirection(mPresShell->GetSelectionCaretsEndElement(), !endFrameIsRTL);
|
|
|
|
SetTilted(isTilt);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SelectionCarets::SelectWord()
|
|
|
|
{
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!mPresShell) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-14 19:43:29 -07:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2014-10-28 15:25:00 -07:00
|
|
|
if (!rootFrame) {
|
2014-08-24 17:50:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
// Find content offsets for mouse down point
|
2014-10-14 19:43:29 -07:00
|
|
|
nsIFrame *ptFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, mDownPoint,
|
2014-06-04 13:57:00 -07:00
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION | nsLayoutUtils::IGNORE_CROSS_DOC);
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!ptFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
// If frame isn't editable and we don't support non-editable fields, bail
|
|
|
|
// out.
|
|
|
|
if (!kSupportNonEditableFields && !ptFrame->GetContent()->IsEditable()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
nsPoint ptInFrame = mDownPoint;
|
2014-10-28 15:25:00 -07:00
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, ptFrame, ptInFrame);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
// If target frame is editable, we should move focus to targe frame. If
|
|
|
|
// target frame isn't editable and our focus content is editable, we should
|
|
|
|
// clear focus.
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
nsIContent* editingHost = ptFrame->GetContent()->GetEditingHost();
|
|
|
|
if (editingHost) {
|
|
|
|
nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(editingHost->GetParent());
|
|
|
|
if (elt) {
|
|
|
|
fm->SetFocus(elt, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsIContent* focusedContent = GetFocusedContent();
|
|
|
|
if (focusedContent && focusedContent->GetTextEditorRootContent()) {
|
|
|
|
nsIDOMWindow* win = mPresShell->GetDocument()->GetWindow();
|
|
|
|
if (win) {
|
|
|
|
fm->ClearFocus(win);
|
|
|
|
}
|
|
|
|
}
|
2014-08-24 17:50:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 20:08:00 -07:00
|
|
|
SetSelectionDragState(true);
|
2014-06-04 13:57:00 -07:00
|
|
|
nsFrame* frame = static_cast<nsFrame*>(ptFrame);
|
|
|
|
nsresult rs = frame->SelectByTypeAtPoint(mPresShell->GetPresContext(), ptInFrame,
|
|
|
|
eSelectWord, eSelectWord, 0);
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG_FRAME_DUMP
|
|
|
|
nsCString frameTag;
|
|
|
|
frame->ListTag(frameTag);
|
|
|
|
SELECTIONCARETS_LOG("Frame=%s, ptInFrame=(%d, %d)", frameTag.get(),
|
|
|
|
ptInFrame.x, ptInFrame.y);
|
|
|
|
#endif
|
|
|
|
|
2014-08-06 20:08:00 -07:00
|
|
|
SetSelectionDragState(false);
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
// Clear maintain selection otherwise we cannot select less than a word
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-06-04 13:57:00 -07:00
|
|
|
fs->MaintainSelection();
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're dragging start caret, we do not want to drag over previous
|
|
|
|
* character of end caret. Same as end caret. So we check if content offset
|
|
|
|
* exceed previous/next character of end/start caret base on aDragMode.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
CompareRangeWithContentOffset(nsRange* aRange,
|
|
|
|
nsFrameSelection* aSelection,
|
|
|
|
nsIFrame::ContentOffsets& aOffsets,
|
|
|
|
SelectionCarets::DragMode aDragMode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDragMode != SelectionCarets::NONE);
|
|
|
|
nsINode* node = nullptr;
|
|
|
|
int32_t nodeOffset = 0;
|
2014-08-05 22:19:27 -07:00
|
|
|
CaretAssociationHint hint;
|
2014-06-04 13:57:00 -07:00
|
|
|
nsDirection dir;
|
|
|
|
|
|
|
|
if (aDragMode == SelectionCarets::START_FRAME) {
|
|
|
|
// Check previous character of end node offset
|
|
|
|
node = aRange->GetEndParent();
|
|
|
|
nodeOffset = aRange->EndOffset();
|
2014-08-05 22:19:27 -07:00
|
|
|
hint = CARET_ASSOCIATE_BEFORE;
|
2014-06-04 13:57:00 -07:00
|
|
|
dir = eDirPrevious;
|
|
|
|
} else {
|
|
|
|
// Check next character of start node offset
|
|
|
|
node = aRange->GetStartParent();
|
|
|
|
nodeOffset = aRange->StartOffset();
|
2014-08-05 22:19:27 -07:00
|
|
|
hint = CARET_ASSOCIATE_AFTER;
|
2014-06-04 13:57:00 -07:00
|
|
|
dir = eDirNext;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
|
|
|
nsIFrame* theFrame =
|
|
|
|
aSelection->GetFrameForNodeOffset(content, nodeOffset, hint, &offset);
|
|
|
|
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!theFrame) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
// Move one character forward/backward from point and get offset
|
|
|
|
nsPeekOffsetStruct pos(eSelectCluster,
|
|
|
|
dir,
|
|
|
|
offset,
|
|
|
|
0,
|
|
|
|
true,
|
|
|
|
true, //limit on scrolled views
|
|
|
|
false,
|
|
|
|
false);
|
|
|
|
nsresult rv = theFrame->PeekOffset(&pos);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
pos.mResultContent = content;
|
|
|
|
pos.mContentOffset = nodeOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare with current point
|
|
|
|
int32_t result = nsContentUtils::ComparePoints(aOffsets.content,
|
|
|
|
aOffsets.StartOffset(),
|
|
|
|
pos.mResultContent,
|
|
|
|
pos.mContentOffset);
|
|
|
|
if ((aDragMode == SelectionCarets::START_FRAME && result == 1) ||
|
|
|
|
(aDragMode == SelectionCarets::END_FRAME && result == -1)) {
|
|
|
|
aOffsets.content = pos.mResultContent;
|
|
|
|
aOffsets.offset = pos.mContentOffset;
|
|
|
|
aOffsets.secondaryOffset = pos.mContentOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventStatus
|
|
|
|
SelectionCarets::DragSelection(const nsPoint &movePoint)
|
|
|
|
{
|
2014-10-14 19:43:29 -07:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2014-10-28 15:25:00 -07:00
|
|
|
if (!rootFrame) {
|
2014-08-24 17:50:00 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
// Find out which content we point to
|
2014-10-14 19:43:29 -07:00
|
|
|
nsIFrame *ptFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, movePoint,
|
2014-06-04 13:57:00 -07:00
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION | nsLayoutUtils::IGNORE_CROSS_DOC);
|
2014-08-05 22:55:00 -07:00
|
|
|
if (!ptFrame) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-08-05 22:55:00 -07:00
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
nsIFrame *newFrame = nullptr;
|
|
|
|
nsPoint newPoint;
|
2014-06-04 13:57:00 -07:00
|
|
|
nsPoint ptInFrame = movePoint;
|
2014-10-28 15:25:00 -07:00
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, ptFrame, ptInFrame);
|
2014-08-05 22:55:00 -07:00
|
|
|
result = fs->ConstrainFrameAndPointToAnchorSubtree(ptFrame, ptInFrame, &newFrame, newPoint);
|
|
|
|
if (NS_FAILED(result) || !newFrame) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
bool selectable;
|
|
|
|
newFrame->IsSelectable(&selectable, nullptr);
|
|
|
|
if (!selectable) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
nsFrame::ContentOffsets offsets =
|
2014-08-05 22:55:00 -07:00
|
|
|
newFrame->GetContentOffsetsFromPoint(newPoint);
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!offsets.content) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
2014-10-23 10:49:00 -07:00
|
|
|
int32_t rangeCount = selection->GetRangeCount();
|
|
|
|
if (rangeCount <= 0) {
|
2014-06-04 13:57:00 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:49:00 -07:00
|
|
|
nsRefPtr<nsRange> range = mDragMode == START_FRAME ?
|
|
|
|
selection->GetRangeAt(0) : selection->GetRangeAt(rangeCount - 1);
|
2014-06-04 13:57:00 -07:00
|
|
|
if (!CompareRangeWithContentOffset(range, fs, offsets, mDragMode)) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
nsIFrame* anchorFrame;
|
|
|
|
selection->GetPrimaryFrameForAnchorNode(&anchorFrame);
|
|
|
|
if (!anchorFrame) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
// Move caret postion.
|
|
|
|
nsIFrame *scrollable =
|
2014-10-16 14:17:00 -07:00
|
|
|
nsLayoutUtils::GetClosestFrameOfType(anchorFrame, nsGkAtoms::scrollFrame);
|
2014-06-04 13:57:00 -07:00
|
|
|
nsWeakFrame weakScrollable = scrollable;
|
|
|
|
fs->HandleClick(offsets.content, offsets.StartOffset(),
|
|
|
|
offsets.EndOffset(),
|
|
|
|
true,
|
|
|
|
false,
|
2014-08-05 22:19:27 -07:00
|
|
|
offsets.associate);
|
2014-06-04 13:57:00 -07:00
|
|
|
if (!weakScrollable.IsAlive()) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll scrolled frame.
|
|
|
|
nsIScrollableFrame *saf = do_QueryFrame(scrollable);
|
|
|
|
nsIFrame *capturingFrame = saf->GetScrolledFrame();
|
|
|
|
nsPoint ptInScrolled = movePoint;
|
2014-10-28 15:25:00 -07:00
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, capturingFrame, ptInScrolled);
|
2014-06-04 13:57:00 -07:00
|
|
|
fs->StartAutoScrollTimer(capturingFrame, ptInScrolled, TouchCaret::sAutoScrollTimerDelay);
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nscoord
|
|
|
|
SelectionCarets::GetCaretYCenterPosition()
|
|
|
|
{
|
2014-10-28 15:25:00 -07:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-28 15:25:00 -07:00
|
|
|
if (!rootFrame) {
|
2014-06-04 13:57:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-16 14:17:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
2014-10-23 10:49:00 -07:00
|
|
|
int32_t rangeCount = selection->GetRangeCount();
|
|
|
|
if (rangeCount <= 0) {
|
2014-06-04 13:57:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-16 14:17:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
MOZ_ASSERT(mDragMode != NONE);
|
|
|
|
nsCOMPtr<nsIContent> node;
|
|
|
|
uint32_t nodeOffset;
|
|
|
|
if (mDragMode == START_FRAME) {
|
2014-10-23 10:49:00 -07:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
2014-06-04 13:57:00 -07:00
|
|
|
node = do_QueryInterface(range->GetStartParent());
|
|
|
|
nodeOffset = range->StartOffset();
|
|
|
|
} else {
|
2014-10-23 10:49:00 -07:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeCount - 1);
|
2014-06-04 13:57:00 -07:00
|
|
|
node = do_QueryInterface(range->GetEndParent());
|
|
|
|
nodeOffset = range->EndOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t offset;
|
2014-08-05 22:19:27 -07:00
|
|
|
CaretAssociationHint hint =
|
2014-09-28 14:48:00 -07:00
|
|
|
mDragMode == START_FRAME ? CARET_ASSOCIATE_AFTER : CARET_ASSOCIATE_BEFORE;
|
2014-06-04 13:57:00 -07:00
|
|
|
nsIFrame* theFrame =
|
|
|
|
fs->GetFrameForNodeOffset(node, nodeOffset, hint, &offset);
|
|
|
|
|
|
|
|
if (!theFrame) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nsRect frameRect = theFrame->GetRectRelativeToSelf();
|
2014-10-28 15:25:00 -07:00
|
|
|
nsLayoutUtils::TransformRect(theFrame, rootFrame, frameRect);
|
2014-06-04 13:57:00 -07:00
|
|
|
return frameRect.Center().y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-03 21:26:00 -07:00
|
|
|
SelectionCarets::SetSelectionDragState(bool aState)
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-08-03 21:26:00 -07:00
|
|
|
fs->SetDragState(aState);
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetSelectionDirection(bool aForward)
|
|
|
|
{
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
2014-06-04 13:57:00 -07:00
|
|
|
selection->SetDirection(aForward ? eDirNext : eDirPrevious);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetFramePos(dom::Element* aElement, const nsPoint& aPosition)
|
|
|
|
{
|
2014-08-24 17:50:00 -07:00
|
|
|
if (!aElement) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
|
|
|
nsAutoString styleStr;
|
2014-10-28 17:16:00 -07:00
|
|
|
styleStr.AppendLiteral("left: ");
|
2014-06-04 13:57:00 -07:00
|
|
|
styleStr.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(aPosition.x));
|
2014-10-28 17:16:00 -07:00
|
|
|
styleStr.AppendLiteral("px; top: ");
|
2014-06-04 13:57:00 -07:00
|
|
|
styleStr.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(aPosition.y));
|
|
|
|
styleStr.AppendLiteral("px;");
|
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG_STATIC("Set style: %s",
|
|
|
|
NS_ConvertUTF16toUTF8(styleStr).get());
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
aElement->SetAttr(kNameSpaceID_None, nsGkAtoms::style, styleStr, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetStartFramePos(const nsPoint& aPosition)
|
|
|
|
{
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("x=%d, y=%d", aPosition.x, aPosition.y);
|
2014-06-04 13:57:00 -07:00
|
|
|
SetFramePos(mPresShell->GetSelectionCaretsStartElement(), aPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetEndFramePos(const nsPoint& aPosition)
|
|
|
|
{
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("x=%d, y=%d", aPosition.y, aPosition.y);
|
2014-06-04 13:57:00 -07:00
|
|
|
SetFramePos(mPresShell->GetSelectionCaretsEndElement(), aPosition);
|
|
|
|
}
|
|
|
|
|
2014-09-14 17:57:00 -07:00
|
|
|
bool
|
|
|
|
SelectionCarets::IsOnStartFrame(const nsPoint& aPosition)
|
|
|
|
{
|
|
|
|
return mVisible &&
|
|
|
|
nsLayoutUtils::ContainsPoint(GetStartFrameRect(), aPosition,
|
|
|
|
SelectionCaretsInflateSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SelectionCarets::IsOnEndFrame(const nsPoint& aPosition)
|
|
|
|
{
|
|
|
|
return mVisible &&
|
|
|
|
nsLayoutUtils::ContainsPoint(GetEndFrameRect(), aPosition,
|
|
|
|
SelectionCaretsInflateSize());
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
nsRect
|
|
|
|
SelectionCarets::GetStartFrameRect()
|
|
|
|
{
|
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsStartElement();
|
2014-10-28 15:25:00 -07:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
return nsLayoutUtils::GetRectRelativeToFrame(element, rootFrame);
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRect
|
|
|
|
SelectionCarets::GetEndFrameRect()
|
|
|
|
{
|
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsEndElement();
|
2014-10-28 15:25:00 -07:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
return nsLayoutUtils::GetRectRelativeToFrame(element, rootFrame);
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
nsIContent*
|
|
|
|
SelectionCarets::GetFocusedContent()
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
2014-10-16 14:17:00 -07:00
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm) {
|
|
|
|
return fm->GetFocusedContent();
|
2014-08-24 17:50:00 -07:00
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
return nullptr;
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
Selection*
|
|
|
|
SelectionCarets::GetSelection()
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
2014-10-16 14:17:00 -07:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
|
|
|
if (fs) {
|
|
|
|
return fs->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
2014-08-24 17:50:00 -07:00
|
|
|
}
|
2014-10-16 14:17:00 -07:00
|
|
|
return nullptr;
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
2014-10-16 14:17:00 -07:00
|
|
|
already_AddRefed<nsFrameSelection>
|
|
|
|
SelectionCarets::GetFrameSelection()
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
2014-10-16 14:17:00 -07:00
|
|
|
nsIContent* focusNode = GetFocusedContent();
|
|
|
|
if (focusNode) {
|
|
|
|
nsIFrame* focusFrame = focusNode->GetPrimaryFrame();
|
|
|
|
if (!focusFrame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return focusFrame->GetFrameSelection();
|
|
|
|
} else {
|
|
|
|
return mPresShell->FrameSelection();
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SelectionCarets::NotifySelectionChanged(nsIDOMDocument* aDoc,
|
2014-10-28 23:03:00 -07:00
|
|
|
nsISelection* aSel,
|
|
|
|
int16_t aReason)
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("aSel (%p), Reason=%d", aSel, aReason);
|
2014-09-26 00:14:35 -07:00
|
|
|
if (!aReason || (aReason & (nsISelectionListener::DRAG_REASON |
|
|
|
|
nsISelectionListener::KEYPRESS_REASON |
|
|
|
|
nsISelectionListener::MOUSEDOWN_REASON))) {
|
2014-06-04 13:57:00 -07:00
|
|
|
SetVisibility(false);
|
|
|
|
} else {
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:37:00 -07:00
|
|
|
static void
|
|
|
|
DispatchScrollViewChangeEvent(nsIPresShell *aPresShell, const dom::ScrollState aState, const mozilla::CSSIntPoint aScrollPos)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = aPresShell->GetDocument();
|
|
|
|
if (doc) {
|
|
|
|
bool ret;
|
|
|
|
ScrollViewChangeEventInit detail;
|
|
|
|
detail.mBubbles = true;
|
|
|
|
detail.mCancelable = false;
|
|
|
|
detail.mState = aState;
|
|
|
|
detail.mScrollX = aScrollPos.x;
|
|
|
|
detail.mScrollY = aScrollPos.y;
|
|
|
|
nsRefPtr<ScrollViewChangeEvent> event =
|
|
|
|
ScrollViewChangeEvent::Constructor(doc, NS_LITERAL_STRING("scrollviewchange"), detail);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
|
|
|
|
doc->DispatchEvent(event, &ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:57:00 -07:00
|
|
|
void
|
2014-09-23 03:37:00 -07:00
|
|
|
SelectionCarets::AsyncPanZoomStarted(const mozilla::CSSIntPoint aScrollPos)
|
2014-06-04 13:57:00 -07:00
|
|
|
{
|
2014-09-23 03:37:00 -07:00
|
|
|
// Receives the notifications from AsyncPanZoom, sets mAPZenabled as true here
|
|
|
|
// to bypass the notifications from ScrollPositionChanged callbacks
|
|
|
|
mAPZenabled = true;
|
2014-06-04 13:57:00 -07:00
|
|
|
SetVisibility(false);
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Dispatch scroll started with position x=%d, y=%d",
|
|
|
|
aScrollPos.x, aScrollPos.y);
|
2014-09-23 03:37:00 -07:00
|
|
|
DispatchScrollViewChangeEvent(mPresShell, dom::ScrollState::Started, aScrollPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::AsyncPanZoomStopped(const mozilla::CSSIntPoint aScrollPos)
|
|
|
|
{
|
|
|
|
UpdateSelectionCarets();
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Dispatch scroll stopped with position x=%d, y=%d",
|
|
|
|
aScrollPos.x, aScrollPos.y);
|
2014-09-23 03:37:00 -07:00
|
|
|
DispatchScrollViewChangeEvent(mPresShell, dom::ScrollState::Stopped, aScrollPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::ScrollPositionChanged()
|
|
|
|
{
|
2014-09-26 00:14:35 -07:00
|
|
|
if (!mAPZenabled && mVisible) {
|
2014-09-23 03:37:00 -07:00
|
|
|
SetVisibility(false);
|
|
|
|
//TODO: handling scrolling for selection bubble when APZ is off
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Launch scroll end detector");
|
2014-09-23 03:37:00 -07:00
|
|
|
LaunchScrollEndDetector();
|
|
|
|
}
|
2014-06-04 13:57:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::LaunchLongTapDetector()
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLongTapDetectorTimer) {
|
|
|
|
mLongTapDetectorTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mLongTapDetectorTimer);
|
|
|
|
CancelLongTapDetector();
|
|
|
|
int32_t longTapDelay = gfxPrefs::UiClickHoldContextMenusDelay();
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Will fire long tap after %d ms", longTapDelay);
|
2014-06-04 13:57:00 -07:00
|
|
|
mLongTapDetectorTimer->InitWithFuncCallback(FireLongTap,
|
|
|
|
this,
|
|
|
|
longTapDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::CancelLongTapDetector()
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLongTapDetectorTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG("Cancel long tap detector!");
|
2014-06-04 13:57:00 -07:00
|
|
|
mLongTapDetectorTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */void
|
|
|
|
SelectionCarets::FireLongTap(nsITimer* aTimer, void* aSelectionCarets)
|
|
|
|
{
|
|
|
|
nsRefPtr<SelectionCarets> self = static_cast<SelectionCarets*>(aSelectionCarets);
|
|
|
|
NS_PRECONDITION(aTimer == self->mLongTapDetectorTimer,
|
|
|
|
"Unexpected timer");
|
|
|
|
|
2014-10-28 17:16:00 -07:00
|
|
|
SELECTIONCARETS_LOG_STATIC("SelectWord from non-APZ");
|
2014-06-04 13:57:00 -07:00
|
|
|
self->SelectWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::LaunchScrollEndDetector()
|
|
|
|
{
|
|
|
|
if (!mScrollEndDetectorTimer) {
|
|
|
|
mScrollEndDetectorTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mScrollEndDetectorTimer);
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Will fire scroll end after %d ms", kScrollEndTimerDelay);
|
2014-06-04 13:57:00 -07:00
|
|
|
mScrollEndDetectorTimer->InitWithFuncCallback(FireScrollEnd,
|
|
|
|
this,
|
|
|
|
kScrollEndTimerDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */void
|
|
|
|
SelectionCarets::FireScrollEnd(nsITimer* aTimer, void* aSelectionCarets)
|
|
|
|
{
|
|
|
|
nsRefPtr<SelectionCarets> self = static_cast<SelectionCarets*>(aSelectionCarets);
|
|
|
|
NS_PRECONDITION(aTimer == self->mScrollEndDetectorTimer,
|
|
|
|
"Unexpected timer");
|
2014-10-28 17:16:00 -07:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG_STATIC("Update selection carets!");
|
2014-06-04 13:57:00 -07:00
|
|
|
self->SetVisibility(true);
|
|
|
|
self->UpdateSelectionCarets();
|
|
|
|
}
|