2014-06-05 00:57:00 +04: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/. */
|
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "SelectionCarets.h"
|
|
|
|
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "nsBidiPresUtils.h"
|
|
|
|
#include "nsCanvasFrame.h"
|
|
|
|
#include "nsCaret.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
2014-11-13 16:32:00 +03:00
|
|
|
#include "nsDocShell.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "nsDOMTokenList.h"
|
2014-10-17 01:17:00 +04:00
|
|
|
#include "nsFocusManager.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "nsFrame.h"
|
2015-04-20 17:41:20 +03:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#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 14:37:00 +04:00
|
|
|
#include "mozilla/dom/ScrollViewChangeEvent.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
|
|
|
#include "mozilla/dom/TreeWalker.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/TouchEvents.h"
|
2015-06-04 23:51:10 +03:00
|
|
|
#include "Layers.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
#include "TouchCaret.h"
|
2014-08-06 09:19:27 +04:00
|
|
|
#include "nsFrameSelection.h"
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-09-23 14:37:00 +04:00
|
|
|
using namespace mozilla::dom;
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-29 03:16:00 +03:00
|
|
|
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, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gSelectionCaretsLog, LogLevel::Debug, \
|
2014-10-29 03:16:00 +03:00
|
|
|
("SelectionCarets (%p): %s:%d : " message "\n", this, __FUNCTION__, \
|
|
|
|
__LINE__, ##__VA_ARGS__));
|
|
|
|
|
|
|
|
#define SELECTIONCARETS_LOG_STATIC(message, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gSelectionCaretsLog, LogLevel::Debug, \
|
2014-10-29 03:16:00 +03:00
|
|
|
("SelectionCarets: %s:%d : " message "\n", __FUNCTION__, __LINE__, \
|
|
|
|
##__VA_ARGS__));
|
|
|
|
|
2014-06-05 00:57:00 +04: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;
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(SelectionCarets,
|
2014-11-05 23:18:00 +03:00
|
|
|
nsIReflowObserver,
|
2014-06-05 00:57:00 +04:00
|
|
|
nsISelectionListener,
|
|
|
|
nsIScrollObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
/*static*/ int32_t SelectionCarets::sSelectionCaretsInflateSize = 0;
|
2015-04-14 05:33:02 +03:00
|
|
|
/*static*/ bool SelectionCarets::sSelectionCaretDetectsLongTap = true;
|
2015-05-15 05:06:13 +03:00
|
|
|
/*static*/ bool SelectionCarets::sCaretManagesAndroidActionbar = false;
|
|
|
|
/*static*/ bool SelectionCarets::sSelectionCaretObservesCompositions = false;
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-29 03:16:00 +03:00
|
|
|
SelectionCarets::SelectionCarets(nsIPresShell* aPresShell)
|
|
|
|
: mPresShell(aPresShell)
|
|
|
|
, mActiveTouchId(-1)
|
2014-06-05 00:57:00 +04:00
|
|
|
, mCaretCenterToDownPointOffsetY(0)
|
|
|
|
, mDragMode(NONE)
|
2015-03-11 22:29:06 +03:00
|
|
|
, mUseAsyncPanZoom(false)
|
2015-01-15 08:42:00 +03:00
|
|
|
, mInAsyncPanZoomGesture(false)
|
2014-06-05 00:57:00 +04:00
|
|
|
, mEndCaretVisible(false)
|
2014-09-23 14:37:00 +04:00
|
|
|
, mStartCaretVisible(false)
|
2014-12-10 03:50:00 +03:00
|
|
|
, mSelectionVisibleInScrollFrames(true)
|
2014-09-23 14:37:00 +04:00
|
|
|
, mVisible(false)
|
2015-05-15 05:06:13 +03:00
|
|
|
, mActionBarViewID(0)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-10-29 03:16:00 +03:00
|
|
|
if (!gSelectionCaretsLog) {
|
|
|
|
gSelectionCaretsLog = PR_NewLogModule(kSelectionCaretsLogModuleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Constructor, PresShell=%p", mPresShell);
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
static bool addedPref = false;
|
|
|
|
if (!addedPref) {
|
|
|
|
Preferences::AddIntVarCache(&sSelectionCaretsInflateSize,
|
|
|
|
"selectioncaret.inflatesize.threshold");
|
2015-04-14 05:33:02 +03:00
|
|
|
Preferences::AddBoolVarCache(&sSelectionCaretDetectsLongTap,
|
|
|
|
"selectioncaret.detects.longtap", true);
|
2015-05-15 05:06:13 +03:00
|
|
|
Preferences::AddBoolVarCache(&sCaretManagesAndroidActionbar,
|
|
|
|
"caret.manages-android-actionbar");
|
|
|
|
Preferences::AddBoolVarCache(&sSelectionCaretObservesCompositions,
|
|
|
|
"selectioncaret.observes.compositions");
|
2014-06-05 00:57:00 +04:00
|
|
|
addedPref = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 23:18:00 +03:00
|
|
|
void
|
|
|
|
SelectionCarets::Init()
|
|
|
|
{
|
|
|
|
nsPresContext* presContext = mPresShell->GetPresContext();
|
|
|
|
MOZ_ASSERT(presContext, "PresContext should be given in PresShell::Init()");
|
|
|
|
|
|
|
|
nsIDocShell* docShell = presContext->GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-11 22:29:06 +03:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2015-06-04 23:51:10 +03:00
|
|
|
mUseAsyncPanZoom = mPresShell->AsyncPanZoomEnabled();
|
2015-03-11 22:29:06 +03:00
|
|
|
#endif
|
2014-11-10 02:29:00 +03:00
|
|
|
|
2014-11-05 23:18:00 +03:00
|
|
|
docShell->AddWeakReflowObserver(this);
|
2014-11-06 00:00:00 +03:00
|
|
|
docShell->AddWeakScrollObserver(this);
|
2014-11-13 16:32:00 +03:00
|
|
|
|
|
|
|
mDocShell = static_cast<nsDocShell*>(docShell);
|
2014-11-05 23:18:00 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
SelectionCarets::~SelectionCarets()
|
|
|
|
{
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Destructor");
|
2014-06-05 00:57:00 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mPresShell = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-05 23:18:00 +03:00
|
|
|
void
|
|
|
|
SelectionCarets::Terminate()
|
|
|
|
{
|
2014-11-13 16:32:00 +03:00
|
|
|
nsRefPtr<nsDocShell> docShell(mDocShell.get());
|
2014-11-05 23:18:00 +03:00
|
|
|
if (docShell) {
|
|
|
|
docShell->RemoveWeakReflowObserver(this);
|
2014-11-06 00:00:00 +03:00
|
|
|
docShell->RemoveWeakScrollObserver(this);
|
2014-11-05 23:18:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:31:00 +03:00
|
|
|
if (mLongTapDetectorTimer) {
|
|
|
|
mLongTapDetectorTimer->Cancel();
|
|
|
|
mLongTapDetectorTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mScrollEndDetectorTimer) {
|
|
|
|
mScrollEndDetectorTimer->Cancel();
|
|
|
|
mScrollEndDetectorTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-05 23:18:00 +03:00
|
|
|
mPresShell = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
nsEventStatus
|
|
|
|
SelectionCarets::HandleEvent(WidgetEvent* aEvent)
|
|
|
|
{
|
|
|
|
WidgetMouseEvent *mouseEvent = aEvent->AsMouseEvent();
|
|
|
|
if (mouseEvent && mouseEvent->reason == WidgetMouseEvent::eSynthesized) {
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetTouchEvent *touchEvent = aEvent->AsTouchEvent();
|
2015-02-02 01:27:31 +03:00
|
|
|
LayoutDeviceIntPoint movePoint;
|
2014-06-05 00:57:00 +04:00
|
|
|
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) {
|
2015-02-02 01:27:31 +03:00
|
|
|
movePoint = mouseEvent->AsGUIEvent()->refPoint;
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
2015-02-26 02:48:00 +03:00
|
|
|
// XUL has no SelectionCarets elements.
|
|
|
|
if (!mPresShell->GetSelectionCaretsStartElement() ||
|
|
|
|
!mPresShell->GetSelectionCaretsEndElement()) {
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:25:00 +03:00
|
|
|
// Get event coordinate relative to root frame
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
if (!rootFrame) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
2014-10-29 01:25:00 +03:00
|
|
|
nsPoint ptInRoot =
|
|
|
|
nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, movePoint, rootFrame);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2015-08-22 04:34:51 +03:00
|
|
|
if (aEvent->mMessage == NS_TOUCH_START ||
|
|
|
|
(aEvent->mMessage == NS_MOUSE_BUTTON_DOWN &&
|
2014-06-05 00:57:00 +04:00
|
|
|
mouseEvent->button == WidgetMouseEvent::eLeftButton)) {
|
|
|
|
// If having a active touch, ignore other touch down event
|
2015-08-22 04:34:51 +03:00
|
|
|
if (aEvent->mMessage == NS_TOUCH_START && mActiveTouchId >= 0) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
mActiveTouchId = nowTouchId;
|
2014-10-29 01:25:00 +03:00
|
|
|
mDownPoint = ptInRoot;
|
2014-11-13 07:03:00 +03:00
|
|
|
if (IsOnStartFrameInner(ptInRoot)) {
|
2014-06-05 00:57:00 +04:00
|
|
|
mDragMode = START_FRAME;
|
2014-10-29 01:25:00 +03:00
|
|
|
mCaretCenterToDownPointOffsetY = GetCaretYCenterPosition() - ptInRoot.y;
|
2015-03-18 10:26:00 +03:00
|
|
|
SetSelectionDirection(eDirPrevious);
|
2014-08-04 08:26:00 +04:00
|
|
|
SetSelectionDragState(true);
|
2014-06-05 00:57:00 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2014-11-13 07:03:00 +03:00
|
|
|
} else if (IsOnEndFrameInner(ptInRoot)) {
|
2014-06-05 00:57:00 +04:00
|
|
|
mDragMode = END_FRAME;
|
2014-10-29 01:25:00 +03:00
|
|
|
mCaretCenterToDownPointOffsetY = GetCaretYCenterPosition() - ptInRoot.y;
|
2015-03-18 10:26:00 +03:00
|
|
|
SetSelectionDirection(eDirNext);
|
2014-08-04 08:26:00 +04:00
|
|
|
SetSelectionDragState(true);
|
2014-06-05 00:57:00 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
} else {
|
|
|
|
mDragMode = NONE;
|
|
|
|
mActiveTouchId = -1;
|
|
|
|
LaunchLongTapDetector();
|
|
|
|
}
|
2015-08-22 04:34:51 +03:00
|
|
|
} else if (aEvent->mMessage == NS_TOUCH_END ||
|
|
|
|
aEvent->mMessage == NS_TOUCH_CANCEL ||
|
|
|
|
aEvent->mMessage == NS_MOUSE_BUTTON_UP) {
|
2014-06-05 00:57:00 +04:00
|
|
|
CancelLongTapDetector();
|
|
|
|
if (mDragMode != NONE) {
|
|
|
|
// Only care about same id
|
|
|
|
if (mActiveTouchId == nowTouchId) {
|
2014-08-04 08:26:00 +04:00
|
|
|
SetSelectionDragState(false);
|
2014-06-05 00:57:00 +04:00
|
|
|
mDragMode = NONE;
|
|
|
|
mActiveTouchId = -1;
|
|
|
|
}
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2015-08-22 04:34:51 +03:00
|
|
|
} else if (aEvent->mMessage == NS_TOUCH_MOVE ||
|
|
|
|
aEvent->mMessage == NS_MOUSE_MOVE) {
|
2014-06-05 00:57:00 +04:00
|
|
|
if (mDragMode == START_FRAME || mDragMode == END_FRAME) {
|
|
|
|
if (mActiveTouchId == nowTouchId) {
|
2014-10-29 01:25:00 +03:00
|
|
|
ptInRoot.y += mCaretCenterToDownPointOffsetY;
|
2014-12-19 08:25:17 +03:00
|
|
|
|
|
|
|
if (mDragMode == START_FRAME) {
|
|
|
|
if (ptInRoot.y > mDragDownYBoundary) {
|
|
|
|
ptInRoot.y = mDragDownYBoundary;
|
|
|
|
}
|
|
|
|
} else if (mDragMode == END_FRAME) {
|
|
|
|
if (ptInRoot.y < mDragUpYBoundary) {
|
|
|
|
ptInRoot.y = mDragUpYBoundary;
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 01:25:00 +03:00
|
|
|
return DragSelection(ptInRoot);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:25:00 +03:00
|
|
|
nsPoint delta = mDownPoint - ptInRoot;
|
2014-06-05 00:57:00 +04:00
|
|
|
if (NS_hypot(delta.x, delta.y) >
|
|
|
|
nsPresContext::AppUnitsPerCSSPixel() * kMoveStartTolerancePx) {
|
|
|
|
CancelLongTapDetector();
|
|
|
|
}
|
2015-04-14 05:33:02 +03:00
|
|
|
|
2015-08-22 04:34:51 +03:00
|
|
|
} else if (aEvent->mMessage == NS_MOUSE_MOZLONGTAP) {
|
2015-04-14 05:33:02 +03:00
|
|
|
if (!mVisible || !sSelectionCaretDetectsLongTap) {
|
|
|
|
SELECTIONCARETS_LOG("SelectWord from NS_MOUSE_MOZLONGTAP");
|
|
|
|
|
|
|
|
mDownPoint = ptInRoot;
|
2014-12-24 21:14:00 +03:00
|
|
|
nsresult wordSelected = SelectWord();
|
|
|
|
|
|
|
|
if (NS_FAILED(wordSelected)) {
|
2015-04-14 05:33:02 +03:00
|
|
|
SELECTIONCARETS_LOG("SelectWord from NS_MOUSE_MOZLONGTAP failed!");
|
2014-12-24 21:14:00 +03:00
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetElementVisibility(dom::Element* aElement, bool aVisible)
|
|
|
|
{
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!aElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04: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-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Set visibility %s, same as the old one",
|
|
|
|
(aVisible ? "shown" : "hidden"));
|
2014-06-05 00:57:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-10-29 03:16:00 +03:00
|
|
|
|
2014-12-24 03:07:00 +03:00
|
|
|
if (!aVisible) {
|
|
|
|
mSelectionVisibleInScrollFrames = false;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
mVisible = aVisible;
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Set visibility %s", (mVisible ? "shown" : "hidden"));
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
dom::Element* startElement = mPresShell->GetSelectionCaretsStartElement();
|
|
|
|
SetElementVisibility(startElement, mVisible && mStartCaretVisible);
|
|
|
|
|
|
|
|
dom::Element* endElement = mPresShell->GetSelectionCaretsEndElement();
|
|
|
|
SetElementVisibility(endElement, mVisible && mEndCaretVisible);
|
2015-05-15 05:06:13 +03:00
|
|
|
|
|
|
|
// Update the Android Actionbar visibility if in use.
|
|
|
|
if (sCaretManagesAndroidActionbar) {
|
|
|
|
TouchCaret::UpdateAndroidActionBarVisibility(mVisible, mActionBarViewID);
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetStartFrameVisibility(bool aVisible)
|
|
|
|
{
|
|
|
|
mStartCaretVisible = aVisible;
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Set start frame visibility %s",
|
|
|
|
(mStartCaretVisible ? "shown" : "hidden"));
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsStartElement();
|
|
|
|
SetElementVisibility(element, mVisible && mStartCaretVisible);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::SetEndFrameVisibility(bool aVisible)
|
|
|
|
{
|
|
|
|
mEndCaretVisible = aVisible;
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Set end frame visibility %s",
|
2014-11-17 02:05:00 +03:00
|
|
|
(mEndCaretVisible ? "shown" : "hidden"));
|
2014-10-29 03:16:00 +03:00
|
|
|
|
2014-06-05 00:57:00 +04: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-25 04:50:00 +04:00
|
|
|
|
|
|
|
if (!startElement || !endElement) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Set tilted selection carets %s",
|
|
|
|
(aIsTilt ? "enabled" : "disabled"));
|
|
|
|
|
2014-06-05 00:57:00 +04: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 nsIFrame*
|
|
|
|
FindFirstNodeWithFrame(nsIDocument* aDocument,
|
|
|
|
nsRange* aRange,
|
|
|
|
nsFrameSelection* aFrameSelection,
|
|
|
|
bool aBackward,
|
|
|
|
int& aOutOffset)
|
|
|
|
{
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!aDocument || !aRange || !aFrameSelection) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-05 00:57:00 +04: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-06 09:19:27 +04:00
|
|
|
CaretAssociationHint hintStart =
|
2014-09-29 01:48:00 +04:00
|
|
|
aBackward ? CARET_ASSOCIATE_BEFORE : CARET_ASSOCIATE_AFTER;
|
2014-06-05 00:57:00 +04: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-25 04:50:00 +04:00
|
|
|
if (!walker) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
startFrame = startContent ? startContent->GetPrimaryFrame() : nullptr;
|
|
|
|
while (!startFrame && startNode != endNode) {
|
|
|
|
if (aBackward) {
|
|
|
|
startNode = walker->PreviousNode(err);
|
|
|
|
} else {
|
|
|
|
startNode = walker->NextNode(err);
|
|
|
|
}
|
2014-10-17 01:17:00 +04:00
|
|
|
|
|
|
|
if (!startNode) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
startContent = do_QueryInterface(startNode);
|
|
|
|
startFrame = startContent ? startContent->GetPrimaryFrame() : nullptr;
|
|
|
|
}
|
|
|
|
return startFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::UpdateSelectionCarets()
|
|
|
|
{
|
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
|
|
|
if (!selection) {
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Cannot get selection!");
|
2014-06-05 00:57:00 +04:00
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:49:00 +04:00
|
|
|
if (selection->IsCollapsed()) {
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Selection is collapsed!");
|
2014-06-05 00:57:00 +04:00
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:08:00 +03:00
|
|
|
int32_t rangeCount = selection->RangeCount();
|
2014-10-23 21:49:00 +04:00
|
|
|
nsRefPtr<nsRange> firstRange = selection->GetRangeAt(0);
|
|
|
|
nsRefPtr<nsRange> lastRange = selection->GetRangeAt(rangeCount - 1);
|
|
|
|
|
2015-03-24 10:34:00 +03:00
|
|
|
mPresShell->FlushPendingNotifications(Flush_Layout);
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
|
2015-02-05 03:36:00 +03:00
|
|
|
if (!rootFrame) {
|
2014-06-05 00:57:00 +04:00
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check start and end frame is rtl or ltr text
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (!fs) {
|
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
int32_t startOffset;
|
|
|
|
nsIFrame* startFrame = FindFirstNodeWithFrame(mPresShell->GetDocument(),
|
2014-10-23 21:49:00 +04:00
|
|
|
firstRange, fs, false, startOffset);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
int32_t endOffset;
|
|
|
|
nsIFrame* endFrame = FindFirstNodeWithFrame(mPresShell->GetDocument(),
|
2014-10-23 21:49:00 +04:00
|
|
|
lastRange, fs, true, endOffset);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
if (!startFrame || !endFrame) {
|
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if startFrame is after endFrame.
|
|
|
|
if (nsLayoutUtils::CompareTreePosition(startFrame, endFrame) > 0) {
|
|
|
|
SetVisibility(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-10 03:50:00 +03:00
|
|
|
// If the selection is not visible, we should dispatch a event.
|
|
|
|
nsIFrame* commonAncestorFrame =
|
|
|
|
nsLayoutUtils::FindNearestCommonAncestorFrame(startFrame, endFrame);
|
|
|
|
|
2015-02-05 05:58:00 +03:00
|
|
|
nsRect selectionRectInRootFrame = nsContentUtils::GetSelectionBoundingRect(selection);
|
2014-12-10 03:50:00 +03:00
|
|
|
nsRect selectionRectInCommonAncestorFrame = selectionRectInRootFrame;
|
|
|
|
nsLayoutUtils::TransformRect(rootFrame, commonAncestorFrame,
|
|
|
|
selectionRectInCommonAncestorFrame);
|
|
|
|
|
|
|
|
mSelectionVisibleInScrollFrames =
|
|
|
|
nsLayoutUtils::IsRectVisibleInScrollFrames(commonAncestorFrame,
|
|
|
|
selectionRectInCommonAncestorFrame);
|
|
|
|
SELECTIONCARETS_LOG("Selection visibility %s",
|
|
|
|
(mSelectionVisibleInScrollFrames ? "shown" : "hidden"));
|
|
|
|
|
|
|
|
|
2014-12-08 03:54:00 +03:00
|
|
|
nsRect firstRectInStartFrame =
|
2014-11-12 00:54:00 +03:00
|
|
|
nsCaret::GetGeometryForFrame(startFrame, startOffset, nullptr);
|
2014-12-08 03:54:00 +03:00
|
|
|
nsRect lastRectInEndFrame =
|
2014-11-12 00:54:00 +03:00
|
|
|
nsCaret::GetGeometryForFrame(endFrame, endOffset, nullptr);
|
|
|
|
|
2014-12-08 03:54:00 +03:00
|
|
|
bool startFrameVisible =
|
|
|
|
nsLayoutUtils::IsRectVisibleInScrollFrames(startFrame, firstRectInStartFrame);
|
|
|
|
bool endFrameVisible =
|
|
|
|
nsLayoutUtils::IsRectVisibleInScrollFrames(endFrame, lastRectInEndFrame);
|
|
|
|
|
2015-02-05 03:36:00 +03:00
|
|
|
nsRect firstRectInRootFrame = firstRectInStartFrame;
|
|
|
|
nsRect lastRectInRootFrame = lastRectInEndFrame;
|
|
|
|
nsLayoutUtils::TransformRect(startFrame, rootFrame, firstRectInRootFrame);
|
|
|
|
nsLayoutUtils::TransformRect(endFrame, rootFrame, lastRectInRootFrame);
|
2014-11-12 00:54:00 +03:00
|
|
|
|
2014-12-08 03:54:00 +03:00
|
|
|
SetStartFrameVisibility(startFrameVisible);
|
|
|
|
SetEndFrameVisibility(endFrameVisible);
|
2014-10-17 01:17:00 +04:00
|
|
|
|
2015-04-16 02:15:00 +03:00
|
|
|
SetStartFramePos(firstRectInRootFrame);
|
|
|
|
SetEndFramePos(lastRectInRootFrame);
|
2014-06-05 00:57:00 +04:00
|
|
|
SetVisibility(true);
|
|
|
|
|
2014-12-19 08:25:17 +03:00
|
|
|
// Use half of the first(last) rect as the dragup(dragdown) boundary
|
|
|
|
mDragUpYBoundary =
|
|
|
|
(firstRectInRootFrame.BottomLeft().y + firstRectInRootFrame.TopLeft().y) / 2;
|
|
|
|
mDragDownYBoundary =
|
|
|
|
(lastRectInRootFrame.BottomRight().y + lastRectInRootFrame.TopRight().y) / 2;
|
|
|
|
|
2014-11-13 07:03:00 +03:00
|
|
|
nsRect rectStart = GetStartFrameRect();
|
|
|
|
nsRect rectEnd = GetEndFrameRect();
|
|
|
|
bool isTilt = rectStart.Intersects(rectEnd);
|
|
|
|
if (isTilt) {
|
|
|
|
SetCaretDirection(mPresShell->GetSelectionCaretsStartElement(), rectStart.x > rectEnd.x);
|
|
|
|
SetCaretDirection(mPresShell->GetSelectionCaretsEndElement(), rectStart.x <= rectEnd.x);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
SetTilted(isTilt);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SelectionCarets::SelectWord()
|
|
|
|
{
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!mPresShell) {
|
2014-12-24 21:14:00 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-08-25 04:50:00 +04:00
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-15 06:43:29 +04:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2014-10-29 01:25:00 +03:00
|
|
|
if (!rootFrame) {
|
2014-12-24 21:14:00 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-08-25 04:50:00 +04:00
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
// Find content offsets for mouse down point
|
2014-10-15 06:43:29 +04:00
|
|
|
nsIFrame *ptFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, mDownPoint,
|
2014-06-05 00:57:00 +04:00
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION | nsLayoutUtils::IGNORE_CROSS_DOC);
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!ptFrame) {
|
2014-12-24 21:14:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-25 04:50:00 +04:00
|
|
|
}
|
|
|
|
|
2014-11-28 01:57:00 +03:00
|
|
|
bool selectable;
|
|
|
|
ptFrame->IsSelectable(&selectable, nullptr);
|
|
|
|
if (!selectable) {
|
2014-12-19 09:04:45 +03:00
|
|
|
SELECTIONCARETS_LOG(" frame %p is not selectable", ptFrame);
|
2014-12-24 21:14:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-11-28 01:57:00 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
nsPoint ptInFrame = mDownPoint;
|
2014-10-29 01:25:00 +03:00
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, ptFrame, ptInFrame);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2015-01-16 00:46:00 +03:00
|
|
|
nsIFrame* currFrame = ptFrame;
|
|
|
|
nsIContent* newFocusContent = nullptr;
|
|
|
|
while (currFrame) {
|
|
|
|
int32_t tabIndexUnused = 0;
|
|
|
|
if (currFrame->IsFocusable(&tabIndexUnused, true)) {
|
|
|
|
newFocusContent = currFrame->GetContent();
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement(do_QueryInterface(newFocusContent));
|
|
|
|
if (domElement)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
currFrame = currFrame->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If target frame is focusable, we should move focus to it. If target frame
|
|
|
|
// isn't focusable, and our previous focused content is editable, we should
|
2014-10-17 01:17:00 +04:00
|
|
|
// clear focus.
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
nsIContent* editingHost = ptFrame->GetContent()->GetEditingHost();
|
2015-01-16 00:46:00 +03:00
|
|
|
if (newFocusContent && currFrame) {
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement(do_QueryInterface(newFocusContent));
|
|
|
|
fm->SetFocus(domElement,0);
|
2014-12-19 09:04:45 +03:00
|
|
|
|
2015-01-16 00:46:00 +03:00
|
|
|
if (editingHost && !nsContentUtils::HasNonEmptyTextContent(
|
2014-12-19 09:04:45 +03:00
|
|
|
editingHost, nsContentUtils::eRecurseIntoChildren)) {
|
|
|
|
SELECTIONCARETS_LOG("Select a editable content %p with empty text",
|
|
|
|
editingHost);
|
|
|
|
// Long tap on the content with empty text, no action for
|
2015-01-22 07:42:00 +03:00
|
|
|
// selectioncarets but need to dispatch the taponcaret event
|
2014-12-19 09:04:45 +03:00
|
|
|
// to support the short cut mode
|
2015-01-22 07:42:00 +03:00
|
|
|
DispatchSelectionStateChangedEvent(GetSelection(),
|
|
|
|
SelectionState::Taponcaret);
|
2014-12-19 09:04:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-10-17 01:17:00 +04:00
|
|
|
} else {
|
|
|
|
nsIContent* focusedContent = GetFocusedContent();
|
2015-04-20 17:41:20 +03:00
|
|
|
if (focusedContent) {
|
|
|
|
// Clear focus if content was editable element, or contentEditable.
|
|
|
|
nsGenericHTMLElement* focusedGeneric =
|
|
|
|
nsGenericHTMLElement::FromContent(focusedContent);
|
|
|
|
if (focusedContent->GetTextEditorRootContent() ||
|
|
|
|
(focusedGeneric && focusedGeneric->IsContentEditable())) {
|
|
|
|
nsIDOMWindow* win = mPresShell->GetDocument()->GetWindow();
|
|
|
|
if (win) {
|
|
|
|
fm->ClearFocus(win);
|
|
|
|
}
|
2014-10-17 01:17:00 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-25 04:50:00 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 07:08:00 +04:00
|
|
|
SetSelectionDragState(true);
|
2014-06-05 00:57:00 +04:00
|
|
|
nsFrame* frame = static_cast<nsFrame*>(ptFrame);
|
|
|
|
nsresult rs = frame->SelectByTypeAtPoint(mPresShell->GetPresContext(), ptInFrame,
|
|
|
|
eSelectWord, eSelectWord, 0);
|
2014-10-29 03:16:00 +03: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-07 07:08:00 +04:00
|
|
|
SetSelectionDragState(false);
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
// Clear maintain selection otherwise we cannot select less than a word
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (fs) {
|
|
|
|
fs->MaintainSelection();
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
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-06 09:19:27 +04:00
|
|
|
CaretAssociationHint hint;
|
2014-06-05 00:57:00 +04:00
|
|
|
nsDirection dir;
|
|
|
|
|
|
|
|
if (aDragMode == SelectionCarets::START_FRAME) {
|
|
|
|
// Check previous character of end node offset
|
|
|
|
node = aRange->GetEndParent();
|
|
|
|
nodeOffset = aRange->EndOffset();
|
2014-08-06 09:19:27 +04:00
|
|
|
hint = CARET_ASSOCIATE_BEFORE;
|
2014-06-05 00:57:00 +04:00
|
|
|
dir = eDirPrevious;
|
|
|
|
} else {
|
|
|
|
// Check next character of start node offset
|
|
|
|
node = aRange->GetStartParent();
|
|
|
|
nodeOffset = aRange->StartOffset();
|
2014-08-06 09:19:27 +04:00
|
|
|
hint = CARET_ASSOCIATE_AFTER;
|
2014-06-05 00:57:00 +04:00
|
|
|
dir = eDirNext;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
|
|
|
nsIFrame* theFrame =
|
|
|
|
aSelection->GetFrameForNodeOffset(content, nodeOffset, hint, &offset);
|
|
|
|
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!theFrame) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
// Move one character forward/backward from point and get offset
|
|
|
|
nsPeekOffsetStruct pos(eSelectCluster,
|
|
|
|
dir,
|
|
|
|
offset,
|
2014-11-22 17:39:03 +03:00
|
|
|
nsPoint(0, 0),
|
2014-06-05 00:57:00 +04:00
|
|
|
true,
|
|
|
|
true, //limit on scrolled views
|
|
|
|
false,
|
2015-01-15 19:24:49 +03:00
|
|
|
false,
|
2014-06-05 00:57:00 +04:00
|
|
|
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-15 06:43:29 +04:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2014-10-29 01:25:00 +03:00
|
|
|
if (!rootFrame) {
|
2014-08-25 04:50:00 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
// Find out which content we point to
|
2014-10-15 06:43:29 +04:00
|
|
|
nsIFrame *ptFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, movePoint,
|
2014-06-05 00:57:00 +04:00
|
|
|
nsLayoutUtils::IGNORE_PAINT_SUPPRESSION | nsLayoutUtils::IGNORE_CROSS_DOC);
|
2014-08-06 09:55:00 +04:00
|
|
|
if (!ptFrame) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (!fs) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2014-08-06 09:55:00 +04:00
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
nsIFrame *newFrame = nullptr;
|
|
|
|
nsPoint newPoint;
|
2014-06-05 00:57:00 +04:00
|
|
|
nsPoint ptInFrame = movePoint;
|
2014-10-29 01:25:00 +03:00
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, ptFrame, ptInFrame);
|
2014-08-06 09:55:00 +04:00
|
|
|
result = fs->ConstrainFrameAndPointToAnchorSubtree(ptFrame, ptInFrame, &newFrame, newPoint);
|
|
|
|
if (NS_FAILED(result) || !newFrame) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
bool selectable;
|
|
|
|
newFrame->IsSelectable(&selectable, nullptr);
|
|
|
|
if (!selectable) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
nsFrame::ContentOffsets offsets =
|
2014-08-06 09:55:00 +04:00
|
|
|
newFrame->GetContentOffsetsFromPoint(newPoint);
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!offsets.content) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (!selection) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:08:00 +03:00
|
|
|
int32_t rangeCount = selection->RangeCount();
|
2014-10-23 21:49:00 +04:00
|
|
|
if (rangeCount <= 0) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2015-01-23 08:09:00 +03:00
|
|
|
// Limit the drag behavior not to cross the end of last selection range
|
|
|
|
// when drag the start frame and vice versa
|
2014-10-23 21:49:00 +04:00
|
|
|
nsRefPtr<nsRange> range = mDragMode == START_FRAME ?
|
2015-01-23 08:09:00 +03:00
|
|
|
selection->GetRangeAt(rangeCount - 1) : selection->GetRangeAt(0);
|
2014-06-05 00:57:00 +04:00
|
|
|
if (!CompareRangeWithContentOffset(range, fs, offsets, mDragMode)) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
nsIFrame* anchorFrame;
|
|
|
|
selection->GetPrimaryFrameForAnchorNode(&anchorFrame);
|
|
|
|
if (!anchorFrame) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:57:00 +03:00
|
|
|
// Clear maintain selection so that we can drag caret freely.
|
|
|
|
fs->MaintainSelection(eSelectNoAmount);
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
// Move caret postion.
|
|
|
|
nsIFrame *scrollable =
|
2014-10-17 01:17:00 +04:00
|
|
|
nsLayoutUtils::GetClosestFrameOfType(anchorFrame, nsGkAtoms::scrollFrame);
|
2014-06-05 00:57:00 +04:00
|
|
|
nsWeakFrame weakScrollable = scrollable;
|
|
|
|
fs->HandleClick(offsets.content, offsets.StartOffset(),
|
|
|
|
offsets.EndOffset(),
|
|
|
|
true,
|
|
|
|
false,
|
2014-08-06 09:19:27 +04:00
|
|
|
offsets.associate);
|
2014-06-05 00:57:00 +04:00
|
|
|
if (!weakScrollable.IsAlive()) {
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll scrolled frame.
|
|
|
|
nsIScrollableFrame *saf = do_QueryFrame(scrollable);
|
|
|
|
nsIFrame *capturingFrame = saf->GetScrolledFrame();
|
|
|
|
nsPoint ptInScrolled = movePoint;
|
2014-10-29 01:25:00 +03:00
|
|
|
nsLayoutUtils::TransformPoint(rootFrame, capturingFrame, ptInScrolled);
|
2014-06-05 00:57:00 +04:00
|
|
|
fs->StartAutoScrollTimer(capturingFrame, ptInScrolled, TouchCaret::sAutoScrollTimerDelay);
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
nscoord
|
|
|
|
SelectionCarets::GetCaretYCenterPosition()
|
|
|
|
{
|
2014-10-29 01:25:00 +03:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-29 01:25:00 +03:00
|
|
|
if (!rootFrame) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-17 01:17:00 +04:00
|
|
|
|
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (!selection) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:08:00 +03:00
|
|
|
int32_t rangeCount = selection->RangeCount();
|
2014-10-23 21:49:00 +04:00
|
|
|
if (rangeCount <= 0) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-17 01:17:00 +04:00
|
|
|
|
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (!fs) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mDragMode != NONE);
|
|
|
|
nsCOMPtr<nsIContent> node;
|
|
|
|
uint32_t nodeOffset;
|
|
|
|
if (mDragMode == START_FRAME) {
|
2014-10-23 21:49:00 +04:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
2014-06-05 00:57:00 +04:00
|
|
|
node = do_QueryInterface(range->GetStartParent());
|
|
|
|
nodeOffset = range->StartOffset();
|
|
|
|
} else {
|
2014-10-23 21:49:00 +04:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeCount - 1);
|
2014-06-05 00:57:00 +04:00
|
|
|
node = do_QueryInterface(range->GetEndParent());
|
|
|
|
nodeOffset = range->EndOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t offset;
|
2014-08-06 09:19:27 +04:00
|
|
|
CaretAssociationHint hint =
|
2014-09-29 01:48:00 +04:00
|
|
|
mDragMode == START_FRAME ? CARET_ASSOCIATE_AFTER : CARET_ASSOCIATE_BEFORE;
|
2014-06-05 00:57:00 +04:00
|
|
|
nsIFrame* theFrame =
|
|
|
|
fs->GetFrameForNodeOffset(node, nodeOffset, hint, &offset);
|
|
|
|
|
|
|
|
if (!theFrame) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nsRect frameRect = theFrame->GetRectRelativeToSelf();
|
2014-10-29 01:25:00 +03:00
|
|
|
nsLayoutUtils::TransformRect(theFrame, rootFrame, frameRect);
|
2014-06-05 00:57:00 +04:00
|
|
|
return frameRect.Center().y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-04 08:26:00 +04:00
|
|
|
SelectionCarets::SetSelectionDragState(bool aState)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (fs) {
|
|
|
|
fs->SetDragState(aState);
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-18 10:26:00 +03:00
|
|
|
SelectionCarets::SetSelectionDirection(nsDirection aDir)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
2014-11-10 10:04:00 +03:00
|
|
|
if (selection) {
|
2015-03-18 10:26:00 +03:00
|
|
|
selection->AdjustAnchorFocusForMultiRange(aDir);
|
2014-11-10 10:04:00 +03:00
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-16 02:15:00 +03:00
|
|
|
SetFramePos(dom::Element* aElement, const nsRect& aCaretRect)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-08-25 04:50:00 +04:00
|
|
|
if (!aElement) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
|
|
|
nsAutoString styleStr;
|
2014-10-29 03:16:00 +03:00
|
|
|
styleStr.AppendLiteral("left: ");
|
2015-04-16 02:15:00 +03:00
|
|
|
styleStr.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(aCaretRect.Center().x));
|
2014-10-29 03:16:00 +03:00
|
|
|
styleStr.AppendLiteral("px; top: ");
|
2015-04-16 02:15:00 +03:00
|
|
|
styleStr.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(aCaretRect.y));
|
|
|
|
styleStr.AppendLiteral("px; padding-top: ");
|
|
|
|
styleStr.AppendFloat(nsPresContext::AppUnitsToFloatCSSPixels(aCaretRect.height));
|
2014-06-05 00:57:00 +04:00
|
|
|
styleStr.AppendLiteral("px;");
|
|
|
|
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG_STATIC("Set style: %s",
|
|
|
|
NS_ConvertUTF16toUTF8(styleStr).get());
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
aElement->SetAttr(kNameSpaceID_None, nsGkAtoms::style, styleStr, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 02:15:00 +03:00
|
|
|
SelectionCarets::SetStartFramePos(const nsRect& aCaretRect)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2015-04-16 02:15:00 +03:00
|
|
|
SELECTIONCARETS_LOG("x=%d, y=%d, w=%d, h=%d",
|
|
|
|
aCaretRect.x, aCaretRect.y, aCaretRect.width, aCaretRect.height);
|
|
|
|
SetFramePos(mPresShell->GetSelectionCaretsStartElement(), aCaretRect);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 02:15:00 +03:00
|
|
|
SelectionCarets::SetEndFramePos(const nsRect& aCaretRect)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2015-04-16 02:15:00 +03:00
|
|
|
SELECTIONCARETS_LOG("x=%d, y=%d, w=%d, h=%d",
|
|
|
|
aCaretRect.x, aCaretRect.y, aCaretRect.width, aCaretRect.height);
|
|
|
|
SetFramePos(mPresShell->GetSelectionCaretsEndElement(), aCaretRect);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 04:57:00 +04:00
|
|
|
bool
|
2014-11-13 07:03:00 +03:00
|
|
|
SelectionCarets::IsOnStartFrameInner(const nsPoint& aPosition)
|
2014-09-15 04:57:00 +04:00
|
|
|
{
|
|
|
|
return mVisible &&
|
2014-11-13 07:03:00 +03:00
|
|
|
nsLayoutUtils::ContainsPoint(GetStartFrameRectInner(), aPosition,
|
2014-09-15 04:57:00 +04:00
|
|
|
SelectionCaretsInflateSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-11-13 07:03:00 +03:00
|
|
|
SelectionCarets::IsOnEndFrameInner(const nsPoint& aPosition)
|
2014-09-15 04:57:00 +04:00
|
|
|
{
|
|
|
|
return mVisible &&
|
2014-11-13 07:03:00 +03:00
|
|
|
nsLayoutUtils::ContainsPoint(GetEndFrameRectInner(), aPosition,
|
2014-09-15 04:57:00 +04:00
|
|
|
SelectionCaretsInflateSize());
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
nsRect
|
|
|
|
SelectionCarets::GetStartFrameRect()
|
|
|
|
{
|
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsStartElement();
|
2014-10-29 01:25:00 +03:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
return nsLayoutUtils::GetRectRelativeToFrame(element, rootFrame);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRect
|
|
|
|
SelectionCarets::GetEndFrameRect()
|
|
|
|
{
|
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsEndElement();
|
2014-10-29 01:25:00 +03:00
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
return nsLayoutUtils::GetRectRelativeToFrame(element, rootFrame);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:03:00 +03:00
|
|
|
nsRect
|
|
|
|
SelectionCarets::GetStartFrameRectInner()
|
|
|
|
{
|
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsStartElement();
|
|
|
|
dom::Element* childElement = element->GetFirstElementChild();
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
return nsLayoutUtils::GetRectRelativeToFrame(childElement, rootFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRect
|
|
|
|
SelectionCarets::GetEndFrameRectInner()
|
|
|
|
{
|
|
|
|
dom::Element* element = mPresShell->GetSelectionCaretsEndElement();
|
|
|
|
dom::Element* childElement = element->GetFirstElementChild();
|
|
|
|
nsIFrame* rootFrame = mPresShell->GetRootFrame();
|
|
|
|
return nsLayoutUtils::GetRectRelativeToFrame(childElement, rootFrame);
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
nsIContent*
|
|
|
|
SelectionCarets::GetFocusedContent()
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-10-17 01:17:00 +04:00
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm) {
|
|
|
|
return fm->GetFocusedContent();
|
2014-08-25 04:50:00 +04:00
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
return nullptr;
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
Selection*
|
|
|
|
SelectionCarets::GetSelection()
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-10-17 01:17:00 +04:00
|
|
|
nsRefPtr<nsFrameSelection> fs = GetFrameSelection();
|
|
|
|
if (fs) {
|
|
|
|
return fs->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
2014-08-25 04:50:00 +04:00
|
|
|
}
|
2014-10-17 01:17:00 +04:00
|
|
|
return nullptr;
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 01:17:00 +04:00
|
|
|
already_AddRefed<nsFrameSelection>
|
|
|
|
SelectionCarets::GetFrameSelection()
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-10-17 01:17:00 +04:00
|
|
|
nsIContent* focusNode = GetFocusedContent();
|
|
|
|
if (focusNode) {
|
|
|
|
nsIFrame* focusFrame = focusNode->GetPrimaryFrame();
|
|
|
|
if (!focusFrame) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-12-08 03:54:00 +03:00
|
|
|
|
|
|
|
// Prevent us from touching the nsFrameSelection associated to other
|
|
|
|
// PresShell.
|
|
|
|
nsRefPtr<nsFrameSelection> fs = focusFrame->GetFrameSelection();
|
|
|
|
if (!fs || fs->GetShell() != mPresShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.forget();
|
2014-10-17 01:17:00 +04:00
|
|
|
} else {
|
|
|
|
return mPresShell->FrameSelection();
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 20:55:00 +03:00
|
|
|
static dom::Sequence<SelectionState>
|
|
|
|
GetSelectionStates(int16_t aReason)
|
2014-11-28 07:35:00 +03:00
|
|
|
{
|
2014-11-27 20:55:00 +03:00
|
|
|
dom::Sequence<SelectionState> states;
|
2014-11-28 07:35:00 +03:00
|
|
|
if (aReason & nsISelectionListener::DRAG_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Drag, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
if (aReason & nsISelectionListener::MOUSEDOWN_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Mousedown, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
if (aReason & nsISelectionListener::MOUSEUP_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Mouseup, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
if (aReason & nsISelectionListener::KEYPRESS_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Keypress, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
if (aReason & nsISelectionListener::SELECTALL_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Selectall, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
if (aReason & nsISelectionListener::COLLAPSETOSTART_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Collapsetostart, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
if (aReason & nsISelectionListener::COLLAPSETOEND_REASON) {
|
2015-05-28 21:07:44 +03:00
|
|
|
states.AppendElement(SelectionState::Collapsetoend, fallible);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
2014-11-27 20:55:00 +03:00
|
|
|
return states;
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
|
|
|
|
2014-12-19 09:04:45 +03:00
|
|
|
void
|
|
|
|
SelectionCarets::DispatchCustomEvent(const nsAString& aEvent)
|
|
|
|
{
|
|
|
|
SELECTIONCARETS_LOG("dispatch %s event", NS_ConvertUTF16toUTF8(aEvent).get());
|
|
|
|
bool defaultActionEnabled = true;
|
|
|
|
nsIDocument* doc = mPresShell->GetDocument();
|
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
nsContentUtils::DispatchTrustedEvent(doc,
|
|
|
|
ToSupports(doc),
|
|
|
|
aEvent,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
&defaultActionEnabled);
|
|
|
|
}
|
|
|
|
|
2014-12-10 03:50:00 +03:00
|
|
|
void
|
|
|
|
SelectionCarets::DispatchSelectionStateChangedEvent(Selection* aSelection,
|
|
|
|
SelectionState aState)
|
|
|
|
{
|
|
|
|
dom::Sequence<SelectionState> state;
|
2015-05-28 21:07:44 +03:00
|
|
|
state.AppendElement(aState, fallible);
|
2014-12-10 03:50:00 +03:00
|
|
|
DispatchSelectionStateChangedEvent(aSelection, state);
|
|
|
|
}
|
|
|
|
|
2014-12-08 03:55:00 +03:00
|
|
|
void
|
|
|
|
SelectionCarets::DispatchSelectionStateChangedEvent(Selection* aSelection,
|
|
|
|
const Sequence<SelectionState>& aStates)
|
2014-11-28 07:35:00 +03:00
|
|
|
{
|
2014-12-08 03:55:00 +03:00
|
|
|
nsIDocument* doc = mPresShell->GetDocument();
|
2014-11-28 07:35:00 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(doc);
|
|
|
|
|
2014-11-27 20:55:00 +03:00
|
|
|
SelectionStateChangedEventInit init;
|
2014-11-28 07:35:00 +03:00
|
|
|
init.mBubbles = true;
|
|
|
|
|
2014-12-08 03:55:00 +03:00
|
|
|
if (aSelection) {
|
|
|
|
// XXX: Do we need to flush layout?
|
|
|
|
mPresShell->FlushPendingNotifications(Flush_Layout);
|
2015-02-05 05:58:00 +03:00
|
|
|
nsRect rect = nsContentUtils::GetSelectionBoundingRect(aSelection);
|
2014-11-28 07:35:00 +03:00
|
|
|
nsRefPtr<DOMRect>domRect = new DOMRect(ToSupports(doc));
|
|
|
|
|
|
|
|
domRect->SetLayoutRect(rect);
|
|
|
|
init.mBoundingClientRect = domRect;
|
2014-12-10 03:50:00 +03:00
|
|
|
init.mVisible = mSelectionVisibleInScrollFrames;
|
2014-11-28 07:35:00 +03:00
|
|
|
|
2014-12-08 03:55:00 +03:00
|
|
|
aSelection->Stringify(init.mSelectedText);
|
2014-11-28 07:35:00 +03:00
|
|
|
}
|
2014-11-27 20:55:00 +03:00
|
|
|
init.mStates = aStates;
|
2014-11-28 07:35:00 +03:00
|
|
|
|
2014-11-27 20:55:00 +03:00
|
|
|
nsRefPtr<SelectionStateChangedEvent> event =
|
|
|
|
SelectionStateChangedEvent::Constructor(doc, NS_LITERAL_STRING("mozselectionstatechanged"), init);
|
2014-11-28 07:35:00 +03:00
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
|
|
|
|
bool ret;
|
|
|
|
doc->DispatchEvent(event, &ret);
|
|
|
|
}
|
|
|
|
|
2014-11-27 20:56:00 +03:00
|
|
|
void
|
2015-01-05 03:15:00 +03:00
|
|
|
SelectionCarets::NotifyBlur(bool aIsLeavingDocument)
|
2014-11-27 20:56:00 +03:00
|
|
|
{
|
2015-01-07 09:01:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Send out the blur event");
|
2014-11-27 20:56:00 +03:00
|
|
|
SetVisibility(false);
|
2015-01-05 03:15:00 +03:00
|
|
|
if (aIsLeavingDocument) {
|
|
|
|
CancelLongTapDetector();
|
|
|
|
}
|
2015-02-05 23:13:00 +03:00
|
|
|
CancelScrollEndDetector();
|
|
|
|
mInAsyncPanZoomGesture = false;
|
2014-12-10 03:50:00 +03:00
|
|
|
DispatchSelectionStateChangedEvent(nullptr, SelectionState::Blur);
|
2014-11-27 20:56:00 +03:00
|
|
|
}
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
nsresult
|
|
|
|
SelectionCarets::NotifySelectionChanged(nsIDOMDocument* aDoc,
|
2014-10-29 09:03:00 +03:00
|
|
|
nsISelection* aSel,
|
|
|
|
int16_t aReason)
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("aSel (%p), Reason=%d", aSel, aReason);
|
2014-12-29 22:02:00 +03:00
|
|
|
|
|
|
|
if (aSel != GetSelection()) {
|
|
|
|
SELECTIONCARETS_LOG("Return for selection mismatch!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-15 05:06:13 +03:00
|
|
|
// Update SelectionCaret visibility.
|
|
|
|
if (sSelectionCaretObservesCompositions) {
|
|
|
|
// When observing selection change notifications generated for example
|
|
|
|
// by Android soft-keyboard compositions, we can only obtain visibility
|
|
|
|
// after mouse-up by long-tap, or final caret-drag.
|
|
|
|
if (!mVisible) {
|
|
|
|
if (aReason & nsISelectionListener::MOUSEUP_REASON) {
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If already visible, we hide immediately for some known
|
|
|
|
// event-reasons: drag, keypress, or mouse down.
|
|
|
|
if (aReason & (nsISelectionListener::DRAG_REASON |
|
|
|
|
nsISelectionListener::KEYPRESS_REASON |
|
|
|
|
nsISelectionListener::MOUSEDOWN_REASON)) {
|
|
|
|
SetVisibility(false);
|
|
|
|
} else {
|
|
|
|
// Else we look further at the selection status, as currently
|
|
|
|
// style-composition changes don't provide reason codes.
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
}
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
} else {
|
2015-05-15 05:06:13 +03:00
|
|
|
// Default logic, mainly employed by b2g, isn't aware of soft-keyboard
|
|
|
|
// selection change compositions.
|
|
|
|
if (!aReason || (aReason & (nsISelectionListener::DRAG_REASON |
|
|
|
|
nsISelectionListener::KEYPRESS_REASON |
|
|
|
|
nsISelectionListener::MOUSEDOWN_REASON))) {
|
|
|
|
SetVisibility(false);
|
|
|
|
} else {
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe trigger Android ActionBar updates.
|
|
|
|
if (mVisible && sCaretManagesAndroidActionbar) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->NotifyObservers(nullptr, "ActionBar:UpdateState", nullptr);
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
2014-11-28 07:35:00 +03:00
|
|
|
|
2014-12-08 03:55:00 +03:00
|
|
|
DispatchSelectionStateChangedEvent(static_cast<Selection*>(aSel),
|
|
|
|
GetSelectionStates(aReason));
|
2014-06-05 00:57:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-23 14:37:00 +04:00
|
|
|
static void
|
2015-03-25 14:52:37 +03:00
|
|
|
DispatchScrollViewChangeEvent(nsIPresShell *aPresShell, const dom::ScrollState aState)
|
2014-09-23 14:37:00 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = aPresShell->GetDocument();
|
|
|
|
if (doc) {
|
|
|
|
bool ret;
|
|
|
|
ScrollViewChangeEventInit detail;
|
|
|
|
detail.mBubbles = true;
|
|
|
|
detail.mCancelable = false;
|
|
|
|
detail.mState = aState;
|
|
|
|
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-05 00:57:00 +04:00
|
|
|
void
|
2015-03-25 14:52:37 +03:00
|
|
|
SelectionCarets::AsyncPanZoomStarted()
|
2014-06-05 00:57:00 +04:00
|
|
|
{
|
2015-02-05 23:13:00 +03:00
|
|
|
if (mVisible) {
|
|
|
|
mInAsyncPanZoomGesture = true;
|
2015-05-15 05:06:13 +03:00
|
|
|
// Hide selection carets if not using ActionBar.
|
|
|
|
if (!sCaretManagesAndroidActionbar) {
|
|
|
|
SetVisibility(false);
|
|
|
|
}
|
2014-10-29 03:16:00 +03:00
|
|
|
|
2015-03-25 14:52:37 +03:00
|
|
|
SELECTIONCARETS_LOG("Dispatch scroll started");
|
|
|
|
DispatchScrollViewChangeEvent(mPresShell, dom::ScrollState::Started);
|
2015-02-05 23:13:00 +03:00
|
|
|
} else {
|
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
|
|
|
if (selection && selection->RangeCount() && selection->IsCollapsed()) {
|
|
|
|
mInAsyncPanZoomGesture = true;
|
2015-03-25 14:52:37 +03:00
|
|
|
DispatchScrollViewChangeEvent(mPresShell, dom::ScrollState::Started);
|
2015-02-05 23:13:00 +03:00
|
|
|
}
|
|
|
|
}
|
2014-09-23 14:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-25 14:52:37 +03:00
|
|
|
SelectionCarets::AsyncPanZoomStopped()
|
2014-09-23 14:37:00 +04:00
|
|
|
{
|
2015-02-05 23:13:00 +03:00
|
|
|
if (mInAsyncPanZoomGesture) {
|
|
|
|
mInAsyncPanZoomGesture = false;
|
|
|
|
SELECTIONCARETS_LOG("Update selection carets after APZ is stopped!");
|
|
|
|
UpdateSelectionCarets();
|
2014-10-29 03:16:00 +03:00
|
|
|
|
2015-02-05 23:13:00 +03:00
|
|
|
// SelectionStateChangedEvent should be dispatched before ScrollViewChangeEvent.
|
|
|
|
DispatchSelectionStateChangedEvent(GetSelection(),
|
|
|
|
SelectionState::Updateposition);
|
2014-12-10 03:50:00 +03:00
|
|
|
|
2015-03-25 14:52:37 +03:00
|
|
|
SELECTIONCARETS_LOG("Dispatch scroll stopped");
|
2014-12-10 03:50:00 +03:00
|
|
|
|
2015-03-25 14:52:37 +03:00
|
|
|
DispatchScrollViewChangeEvent(mPresShell, dom::ScrollState::Stopped);
|
2015-02-05 23:13:00 +03:00
|
|
|
}
|
2014-09-23 14:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::ScrollPositionChanged()
|
|
|
|
{
|
2015-01-15 08:42:00 +03:00
|
|
|
if (mVisible) {
|
2015-03-11 22:29:06 +03:00
|
|
|
if (!mUseAsyncPanZoom) {
|
2015-05-15 05:06:13 +03:00
|
|
|
// Hide selection carets if not using ActionBar.
|
|
|
|
if (!sCaretManagesAndroidActionbar) {
|
|
|
|
SetVisibility(false);
|
|
|
|
}
|
|
|
|
|
2015-01-15 08:42:00 +03:00
|
|
|
//TODO: handling scrolling for selection bubble when APZ is off
|
2015-03-17 05:09:00 +03:00
|
|
|
// Dispatch event to notify gaia to hide selection bubble.
|
|
|
|
// Positions will be updated when scroll is end, so no need to calculate
|
|
|
|
// and keep scroll positions here. An arbitrary (0, 0) is sent instead.
|
2015-03-25 14:52:37 +03:00
|
|
|
DispatchScrollViewChangeEvent(mPresShell, dom::ScrollState::Started);
|
2014-10-29 03:16:00 +03:00
|
|
|
|
2015-01-15 08:42:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Launch scroll end detector");
|
|
|
|
LaunchScrollEndDetector();
|
|
|
|
} else {
|
|
|
|
if (!mInAsyncPanZoomGesture) {
|
|
|
|
UpdateSelectionCarets();
|
|
|
|
DispatchSelectionStateChangedEvent(GetSelection(),
|
|
|
|
SelectionState::Updateposition);
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 06:10:00 +03:00
|
|
|
} else {
|
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
|
|
|
if (selection && selection->RangeCount() && selection->IsCollapsed()) {
|
|
|
|
DispatchSelectionStateChangedEvent(selection,
|
|
|
|
SelectionState::Updateposition);
|
|
|
|
}
|
2014-09-23 14:37:00 +04:00
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::LaunchLongTapDetector()
|
|
|
|
{
|
2015-04-14 05:33:02 +03:00
|
|
|
if (!sSelectionCaretDetectsLongTap || mUseAsyncPanZoom) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLongTapDetectorTimer) {
|
|
|
|
mLongTapDetectorTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mLongTapDetectorTimer);
|
|
|
|
CancelLongTapDetector();
|
|
|
|
int32_t longTapDelay = gfxPrefs::UiClickHoldContextMenusDelay();
|
2014-10-29 03:16:00 +03:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Will fire long tap after %d ms", longTapDelay);
|
2014-06-05 00:57:00 +04:00
|
|
|
mLongTapDetectorTimer->InitWithFuncCallback(FireLongTap,
|
|
|
|
this,
|
|
|
|
longTapDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::CancelLongTapDetector()
|
|
|
|
{
|
2015-03-11 22:29:06 +03:00
|
|
|
if (mUseAsyncPanZoom) {
|
2014-06-05 00:57:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mLongTapDetectorTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG("Cancel long tap detector!");
|
2014-06-05 00:57:00 +04: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-29 03:16:00 +03:00
|
|
|
SELECTIONCARETS_LOG_STATIC("SelectWord from non-APZ");
|
2014-12-24 21:14:00 +03:00
|
|
|
nsresult wordSelected = self->SelectWord();
|
|
|
|
|
|
|
|
if (NS_FAILED(wordSelected)) {
|
|
|
|
SELECTIONCARETS_LOG_STATIC("SelectWord from non-APZ failed!");
|
|
|
|
}
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionCarets::LaunchScrollEndDetector()
|
|
|
|
{
|
|
|
|
if (!mScrollEndDetectorTimer) {
|
|
|
|
mScrollEndDetectorTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mScrollEndDetectorTimer);
|
2014-10-29 03:16:00 +03:00
|
|
|
|
2015-05-15 05:06:13 +03:00
|
|
|
SELECTIONCARETS_LOG("Will fire scroll end after %d ms",
|
|
|
|
TouchCaret::sScrollEndTimerDelay);
|
2014-06-05 00:57:00 +04:00
|
|
|
mScrollEndDetectorTimer->InitWithFuncCallback(FireScrollEnd,
|
|
|
|
this,
|
2015-05-15 05:06:13 +03:00
|
|
|
TouchCaret::sScrollEndTimerDelay,
|
2014-06-05 00:57:00 +04:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2015-02-05 23:13:00 +03:00
|
|
|
void
|
|
|
|
SelectionCarets::CancelScrollEndDetector()
|
|
|
|
{
|
|
|
|
if (!mScrollEndDetectorTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SELECTIONCARETS_LOG("Cancel scroll end detector!");
|
|
|
|
mScrollEndDetectorTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-05 00:57:00 +04:00
|
|
|
/* static */void
|
|
|
|
SelectionCarets::FireScrollEnd(nsITimer* aTimer, void* aSelectionCarets)
|
|
|
|
{
|
|
|
|
nsRefPtr<SelectionCarets> self = static_cast<SelectionCarets*>(aSelectionCarets);
|
|
|
|
NS_PRECONDITION(aTimer == self->mScrollEndDetectorTimer,
|
|
|
|
"Unexpected timer");
|
2014-10-29 03:16:00 +03:00
|
|
|
|
|
|
|
SELECTIONCARETS_LOG_STATIC("Update selection carets!");
|
2014-06-05 00:57:00 +04:00
|
|
|
self->UpdateSelectionCarets();
|
2014-12-10 03:50:00 +03:00
|
|
|
self->DispatchSelectionStateChangedEvent(self->GetSelection(),
|
|
|
|
SelectionState::Updateposition);
|
2014-06-05 00:57:00 +04:00
|
|
|
}
|
2014-11-05 23:18:00 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SelectionCarets::Reflow(DOMHighResTimeStamp aStart, DOMHighResTimeStamp aEnd)
|
|
|
|
{
|
|
|
|
if (mVisible) {
|
|
|
|
SELECTIONCARETS_LOG("Update selection carets after reflow!");
|
|
|
|
UpdateSelectionCarets();
|
2014-12-10 03:50:00 +03:00
|
|
|
|
2014-12-24 02:02:00 +03:00
|
|
|
// We don't care selection state when we're at drag mode. We always hide
|
|
|
|
// bubble in drag mode. So, don't dispatch event here.
|
|
|
|
if (mDragMode == NONE) {
|
|
|
|
DispatchSelectionStateChangedEvent(GetSelection(),
|
|
|
|
SelectionState::Updateposition);
|
|
|
|
}
|
2015-02-03 06:10:00 +03:00
|
|
|
} else {
|
|
|
|
nsRefPtr<dom::Selection> selection = GetSelection();
|
|
|
|
if (selection && selection->RangeCount() && selection->IsCollapsed()) {
|
|
|
|
DispatchSelectionStateChangedEvent(selection,
|
|
|
|
SelectionState::Updateposition);
|
|
|
|
}
|
2014-11-05 23:18:00 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SelectionCarets::ReflowInterruptible(DOMHighResTimeStamp aStart,
|
|
|
|
DOMHighResTimeStamp aEnd)
|
|
|
|
{
|
|
|
|
return Reflow(aStart, aEnd);
|
|
|
|
}
|