SelectionHandler.cpp [plain text]
#include "config.h"
#include "SelectionHandler.h"
#include "DOMSupport.h"
#include "Document.h"
#include "Editor.h"
#include "EditorClient.h"
#include "FatFingers.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "FrameView.h"
#include "HTMLAnchorElement.h"
#include "HTMLAreaElement.h"
#include "HitTestResult.h"
#include "InputHandler.h"
#include "IntRect.h"
#include "Page.h"
#include "RenderPart.h"
#include "TextGranularity.h"
#include "TouchEventHandler.h"
#include "WebPage.h"
#include "WebPageClient.h"
#include "WebPage_p.h"
#include "htmlediting.h"
#include "visible_units.h"
#include <BlackBerryPlatformKeyboardEvent.h>
#include <sys/keycodes.h>
#define SHOWDEBUG_SELECTIONHANDLER 0
using namespace BlackBerry::Platform;
using namespace WebCore;
#if SHOWDEBUG_SELECTIONHANDLER
#define DEBUG_SELECTION(severity, format, ...) logAlways(severity, format, ## __VA_ARGS__)
#else
#define DEBUG_SELECTION(severity, format, ...)
#endif // SHOWDEBUG_SELECTIONHANDLER
namespace BlackBerry {
namespace WebKit {
SelectionHandler::SelectionHandler(WebPagePrivate* page)
: m_webPage(page)
, m_selectionActive(false)
, m_caretActive(false)
, m_lastUpdatedEndPointIsValid(false)
{
}
SelectionHandler::~SelectionHandler()
{
}
void SelectionHandler::cancelSelection()
{
m_selectionActive = false;
m_lastSelectionRegion = IntRectRegion();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::cancelSelection");
if (m_webPage->m_inputHandler->isInputMode())
m_webPage->m_inputHandler->cancelSelection();
else
m_webPage->focusedOrMainFrame()->selection()->clear();
}
WebString SelectionHandler::selectedText() const
{
return m_webPage->focusedOrMainFrame()->editor()->selectedText();
}
WebCore::IntRect SelectionHandler::clippingRectForVisibleContent() const
{
Frame* frame = m_webPage->focusedOrMainFrame();
WebCore::IntRect clipRect = WebCore::IntRect(WebCore::IntPoint(0, 0), m_webPage->contentsSize());
if (frame != m_webPage->mainFrame()) {
clipRect = m_webPage->getRecursiveVisibleWindowRect(frame->view(), true );
clipRect = m_webPage->m_mainFrame->view()->windowToContents(clipRect);
}
WebCore::IntRect inputBoundingBox = m_webPage->m_inputHandler->boundingBoxForInputField();
if (!inputBoundingBox.isEmpty()) {
inputBoundingBox = m_webPage->mainFrame()->view()->windowToContents(frame->view()->contentsToWindow(inputBoundingBox));
clipRect.intersect(inputBoundingBox);
}
return clipRect;
}
void SelectionHandler::regionForTextQuads(Vector<FloatQuad> &quadList, IntRectRegion& region, bool shouldClipToVisibleContent) const
{
ASSERT(region.isEmpty());
if (!quadList.isEmpty()) {
FrameView* frameView = m_webPage->focusedOrMainFrame()->view();
WebCore::IntRect frameRect(WebCore::IntPoint(0, 0), frameView->contentsSize());
WebCore::IntPoint framePosition = m_webPage->frameOffset(m_webPage->focusedOrMainFrame());
WebCore::IntRect clippingRect = shouldClipToVisibleContent ? clippingRectForVisibleContent() : WebCore::IntRect(-1, -1, 0, 0);
Platform::IntRect selectionBoundingBox;
std::vector<Platform::IntRect> adjustedIntRects;
for (unsigned i = 0; i < quadList.size(); i++) {
WebCore::IntRect enclosingRect = quadList[i].enclosingBoundingBox();
enclosingRect.intersect(frameRect);
enclosingRect.move(framePosition.x(), framePosition.y());
if (clippingRect.location() != DOMSupport::InvalidPoint)
enclosingRect.intersect(clippingRect);
adjustedIntRects.push_back(enclosingRect);
selectionBoundingBox = unionOfRects(enclosingRect, selectionBoundingBox);
}
region = IntRectRegion(selectionBoundingBox, adjustedIntRects.size(), adjustedIntRects);
}
}
static VisiblePosition visiblePositionForPointIgnoringClipping(const Frame& frame, const WebCore::IntPoint& framePoint)
{
HitTestResult result = frame.eventHandler()->hitTestResultAtPoint(framePoint, true , true );
Node* node = result.innerNode();
if (!node || node->document() != frame.document())
return VisiblePosition();
RenderObject* renderer = node->renderer();
if (!renderer)
return VisiblePosition();
VisiblePosition visiblePos = renderer->positionForPoint(result.localPoint());
if (visiblePos.isNull())
visiblePos = VisiblePosition(Position(createLegacyEditingPosition(node, 0)));
return visiblePos;
}
static unsigned short directionOfPointRelativeToRect(const WebCore::IntPoint& point, const WebCore::IntRect& rect, const bool useTopPadding = true, const bool useBottomPadding = true)
{
ASSERT(!rect.contains(point));
const int verticalPadding = 5;
if (point.y() + (useTopPadding ? verticalPadding : 0) < rect.y())
return KEYCODE_UP;
if (point.y() > rect.maxY() + (useBottomPadding ? verticalPadding : 0))
return KEYCODE_DOWN;
if (point.x() < rect.location().x())
return KEYCODE_LEFT;
if (point.x() > rect.maxX())
return KEYCODE_RIGHT;
return 0;
}
bool SelectionHandler::shouldUpdateSelectionOrCaretForPoint(const WebCore::IntPoint& point, const WebCore::IntRect& caretRect, bool startCaret) const
{
ASSERT(m_webPage->m_inputHandler->isInputMode());
if (point == DOMSupport::InvalidPoint)
return true;
VisibleSelection currentSelection = m_webPage->focusedOrMainFrame()->selection()->selection();
bool aboveCaret = point.y() < caretRect.y();
bool belowCaret = point.y() >= caretRect.maxY();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::shouldUpdateSelectionOrCaretForPoint multiline = %s above = %s below = %s first line = %s last line = %s start = %s \n"
, m_webPage->m_inputHandler->isMultilineInputMode() ? "true" : "false", aboveCaret ? "true" : "false", belowCaret ? "true" : "false"
, inSameLine(currentSelection.visibleStart(), startOfEditableContent(currentSelection.visibleStart())) ? "true" : "false"
, inSameLine(currentSelection.visibleEnd(), endOfEditableContent(currentSelection.visibleEnd())) ? "true" : "false"
, startCaret ? "true" : "false");
if (!m_webPage->m_inputHandler->isMultilineInputMode() && (aboveCaret || belowCaret))
return false;
if (startCaret && inSameLine(currentSelection.visibleStart(), startOfEditableContent(currentSelection.visibleStart())) && aboveCaret)
return false;
if (!startCaret && inSameLine(currentSelection.visibleEnd(), endOfEditableContent(currentSelection.visibleEnd())) && belowCaret)
return false;
return true;
}
void SelectionHandler::setCaretPosition(const WebCore::IntPoint &position)
{
if (!m_webPage->m_inputHandler->isInputMode())
return;
m_caretActive = true;
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::setCaretPosition requested point %d, %d", position.x(), position.y());
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
FrameSelection* controller = focusedFrame->selection();
WebCore::IntPoint relativePoint = DOMSupport::convertPointToFrame(m_webPage->mainFrame(), focusedFrame, position);
WebCore::IntRect currentCaretRect = controller->selection().visibleStart().absoluteCaretBounds();
if (relativePoint == DOMSupport::InvalidPoint || !shouldUpdateSelectionOrCaretForPoint(relativePoint, currentCaretRect)) {
selectionPositionChanged();
return;
}
VisiblePosition visibleCaretPosition(focusedFrame->visiblePositionForPoint(relativePoint));
if (!DOMSupport::isPositionInNode(m_webPage->focusedOrMainFrame()->document()->focusedNode(), visibleCaretPosition.deepEquivalent())) {
if (unsigned short character = directionOfPointRelativeToRect(relativePoint, currentCaretRect))
m_webPage->m_inputHandler->handleKeyboardInput(Platform::KeyboardEvent(character));
selectionPositionChanged();
return;
}
VisibleSelection newSelection(visibleCaretPosition);
if (controller->selection() == newSelection) {
selectionPositionChanged();
return;
}
controller->setSelection(newSelection);
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::setCaretPosition point valid, cursor updated");
}
static bool shouldExtendSelectionInDirection(const VisibleSelection& selection, unsigned short character)
{
FrameSelection tempSelection;
tempSelection.setSelection(selection);
switch (character) {
case KEYCODE_LEFT:
tempSelection.modify(FrameSelection::AlterationExtend, DirectionLeft, CharacterGranularity);
break;
case KEYCODE_RIGHT:
tempSelection.modify(FrameSelection::AlterationExtend, DirectionRight, CharacterGranularity);
break;
case KEYCODE_UP:
tempSelection.modify(FrameSelection::AlterationExtend, DirectionBackward, LineGranularity);
break;
case KEYCODE_DOWN:
tempSelection.modify(FrameSelection::AlterationExtend, DirectionForward, LineGranularity);
break;
default:
break;
}
if ((character == KEYCODE_LEFT || character == KEYCODE_RIGHT)
&& (!inSameLine(selection.visibleStart(), tempSelection.selection().visibleStart())
|| !inSameLine(selection.visibleEnd(), tempSelection.selection().visibleEnd())))
return false;
return tempSelection.selection().selectionType() == VisibleSelection::RangeSelection;
}
static int clamp(const int min, const int value, const int max)
{
return value < min ? min : std::min(value, max);
}
static VisiblePosition directionalVisiblePositionAtExtentOfBox(Frame* frame, const WebCore::IntRect& boundingBox, unsigned short direction, const WebCore::IntPoint& basePoint)
{
ASSERT(frame);
if (!frame)
return VisiblePosition();
switch (direction) {
case KEYCODE_LEFT:
return frame->visiblePositionForPoint(WebCore::IntPoint(boundingBox.x(), clamp(boundingBox.y(), basePoint.y(), boundingBox.maxY())));
case KEYCODE_RIGHT:
return frame->visiblePositionForPoint(WebCore::IntPoint(boundingBox.maxX(), clamp(boundingBox.y(), basePoint.y(), boundingBox.maxY())));
case KEYCODE_UP:
return frame->visiblePositionForPoint(WebCore::IntPoint(clamp(boundingBox.x(), basePoint.x(), boundingBox.maxX()), boundingBox.y()));
case KEYCODE_DOWN:
return frame->visiblePositionForPoint(WebCore::IntPoint(clamp(boundingBox.x(), basePoint.x(), boundingBox.maxX()), boundingBox.maxY()));
default:
break;
}
return frame->visiblePositionForPoint(WebCore::IntPoint(basePoint.x(), basePoint.y()));
}
static bool pointIsOutsideOfBoundingBoxInDirection(unsigned direction, const WebCore::IntPoint& selectionPoint, const WebCore::IntRect& boundingBox)
{
if ((direction == KEYCODE_LEFT && selectionPoint.x() < boundingBox.x())
|| (direction == KEYCODE_UP && selectionPoint.y() < boundingBox.y())
|| (direction == KEYCODE_RIGHT && selectionPoint.x() > boundingBox.maxX())
|| (direction == KEYCODE_DOWN && selectionPoint.y() > boundingBox.maxY()))
return true;
return false;
}
unsigned short SelectionHandler::extendSelectionToFieldBoundary(bool isStartHandle, const WebCore::IntPoint& selectionPoint, VisibleSelection& newSelection)
{
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
if (!focusedFrame->document()->focusedNode() || !focusedFrame->document()->focusedNode()->renderer())
return 0;
FrameSelection* controller = focusedFrame->selection();
WebCore::IntRect caretRect = isStartHandle ? controller->selection().visibleStart().absoluteCaretBounds()
: controller->selection().visibleEnd().absoluteCaretBounds();
WebCore::IntRect nodeBoundingBox = focusedFrame->document()->focusedNode()->renderer()->absoluteBoundingBoxRect();
nodeBoundingBox.inflate(-1);
unsigned short character = directionOfPointRelativeToRect(selectionPoint, caretRect, isStartHandle , !isStartHandle );
if (isStartHandle && (character == KEYCODE_RIGHT || character == KEYCODE_DOWN)
|| !isStartHandle && (character == KEYCODE_LEFT || character == KEYCODE_UP))
character = 0;
VisiblePosition newVisiblePosition = isStartHandle ? controller->selection().extent() : controller->selection().base();
if (pointIsOutsideOfBoundingBoxInDirection(character, selectionPoint, nodeBoundingBox))
newVisiblePosition = directionalVisiblePositionAtExtentOfBox(focusedFrame, nodeBoundingBox, character, selectionPoint);
if (isStartHandle)
newSelection = VisibleSelection(newVisiblePosition, newSelection.extent(), true );
else
newSelection = VisibleSelection(newSelection.base(), newVisiblePosition, true );
if (controller->selection() == newSelection)
return character;
return 0;
}
bool SelectionHandler::updateOrHandleInputSelection(VisibleSelection& newSelection, const WebCore::IntPoint& relativeStart
, const WebCore::IntPoint& relativeEnd)
{
ASSERT(m_webPage->m_inputHandler->isInputMode());
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
Node* focusedNode = focusedFrame->document()->focusedNode();
if (!focusedNode || !focusedNode->renderer())
return false;
FrameSelection* controller = focusedFrame->selection();
WebCore::IntRect currentStartCaretRect = controller->selection().visibleStart().absoluteCaretBounds();
WebCore::IntRect currentEndCaretRect = controller->selection().visibleEnd().absoluteCaretBounds();
if (!shouldUpdateSelectionOrCaretForPoint(relativeStart, currentStartCaretRect, true )
|| !shouldUpdateSelectionOrCaretForPoint(relativeEnd, currentEndCaretRect, false )) {
selectionPositionChanged();
return true;
}
WebCore::IntRect nodeBoundingBox = focusedNode->renderer()->absoluteBoundingBoxRect();
bool startIsOutsideOfField = relativeStart != DOMSupport::InvalidPoint && !nodeBoundingBox.contains(relativeStart);
bool endIsOutsideOfField = relativeEnd != DOMSupport::InvalidPoint && !nodeBoundingBox.contains(relativeEnd);
if (startIsOutsideOfField && endIsOutsideOfField)
return false;
unsigned short character = 0;
if (startIsOutsideOfField) {
character = extendSelectionToFieldBoundary(true , relativeStart, newSelection);
if (character) {
controller->setSelection(VisibleSelection(controller->selection().end(), controller->selection().start(), true ));
}
} else if (endIsOutsideOfField) {
character = extendSelectionToFieldBoundary(false , relativeEnd, newSelection);
if (character) {
controller->setSelection(VisibleSelection(controller->selection().start(), controller->selection().end(), true ));
}
}
if (!character)
return false;
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::setSelection making selection change attempt using key event %d", character);
if (shouldExtendSelectionInDirection(controller->selection(), character))
m_webPage->m_inputHandler->handleKeyboardInput(Platform::KeyboardEvent(character, Platform::KeyboardEvent::KeyDown, KEYMOD_SHIFT));
selectionPositionChanged();
return true;
}
void SelectionHandler::setSelection(const WebCore::IntPoint& start, const WebCore::IntPoint& end)
{
m_selectionActive = true;
ASSERT(m_webPage);
ASSERT(m_webPage->focusedOrMainFrame());
ASSERT(m_webPage->focusedOrMainFrame()->selection());
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
FrameSelection* controller = focusedFrame->selection();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::setSelection adjusted points %d, %d, %d, %d", start.x(), start.y(), end.x(), end.y());
bool startIsValid = start != DOMSupport::InvalidPoint;
m_lastUpdatedEndPointIsValid = end != DOMSupport::InvalidPoint;
ASSERT(startIsValid || m_lastUpdatedEndPointIsValid);
WebCore::IntPoint relativeStart = start;
WebCore::IntPoint relativeEnd = end;
VisibleSelection newSelection(controller->selection());
if (!controller->selection().isBaseFirst())
controller->setSelection(VisibleSelection(controller->selection().start(), controller->selection().end(), true ));
if (startIsValid) {
relativeStart = DOMSupport::convertPointToFrame(m_webPage->mainFrame(), focusedFrame, start);
VisiblePosition base = visiblePositionForPointIgnoringClipping(*focusedFrame, clipPointToVisibleContainer(start));
if (base.isNotNull()) {
newSelection.setBase(base);
newSelection.setWithoutValidation(newSelection.base(), controller->selection().end());
}
}
if (m_lastUpdatedEndPointIsValid) {
relativeEnd = DOMSupport::convertPointToFrame(m_webPage->mainFrame(), focusedFrame, end);
VisiblePosition extent = visiblePositionForPointIgnoringClipping(*focusedFrame, clipPointToVisibleContainer(end));
if (extent.isNotNull()) {
newSelection.setExtent(extent);
newSelection.setWithoutValidation(controller->selection().start(), newSelection.extent());
}
}
newSelection.setIsDirectional(true);
if (m_webPage->m_inputHandler->isInputMode()) {
if (updateOrHandleInputSelection(newSelection, relativeStart, relativeEnd))
return;
}
if (controller->selection() == newSelection) {
selectionPositionChanged();
return;
}
Vector<FloatQuad> quads;
DOMSupport::visibleTextQuads(newSelection, quads);
IntRectRegion unclippedRegion;
regionForTextQuads(quads, unclippedRegion, false );
if (!unclippedRegion.isEmpty()) {
if (m_selectionActive && !newSelection.isBaseFirst())
m_webPage->m_client->notifySelectionHandlesReversed();
controller->setSelection(newSelection);
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::setSelection selection points valid, selection updated\n");
} else {
selectionPositionChanged();
DEBUG_SELECTION(LogLevelWarn, "SelectionHandler::setSelection selection points invalid, selection not updated\n");
}
}
static Node* enclosingLinkEventParentForNode(Node* node)
{
if (!node)
return 0;
Node* linkNode = node->enclosingLinkEventParentOrSelf();
return linkNode && linkNode->isLink() ? linkNode : 0;
}
void SelectionHandler::selectAtPoint(const WebCore::IntPoint& location)
{
if (location == DOMSupport::InvalidPoint) {
selectObject(WordGranularity);
return;
}
Node* targetNode;
WebCore::IntPoint targetPosition;
const FatFingersResult lastFatFingersResult = m_webPage->m_touchEventHandler->lastFatFingersResult();
if (lastFatFingersResult.positionWasAdjusted() && lastFatFingersResult.nodeAsElementIfApplicable()) {
targetNode = lastFatFingersResult.node(FatFingersResult::ShadowContentNotAllowed);
targetPosition = lastFatFingersResult.adjustedPosition();
} else {
FatFingersResult newFatFingersResult = FatFingers(m_webPage, location, FatFingers::Text).findBestPoint();
if (!newFatFingersResult.positionWasAdjusted())
return;
targetPosition = newFatFingersResult.adjustedPosition();
targetNode = newFatFingersResult.node(FatFingersResult::ShadowContentNotAllowed);
}
ASSERT(targetNode);
if (Node* link = enclosingLinkEventParentForNode(targetNode)) {
selectObject(link);
return;
}
selectObject(targetPosition, WordGranularity);
}
static bool expandSelectionToGranularity(Frame* frame, VisibleSelection selection, TextGranularity granularity, bool isInputMode)
{
ASSERT(frame);
ASSERT(frame->selection());
if (!(selection.start().anchorNode() && selection.start().anchorNode()->isTextNode()))
return false;
if (granularity == WordGranularity)
selection = DOMSupport::visibleSelectionForClosestActualWordStart(selection);
selection.expandUsingGranularity(granularity);
RefPtr<Range> newRange = selection.toNormalizedRange();
RefPtr<Range> oldRange = frame->selection()->selection().toNormalizedRange();
EAffinity affinity = frame->selection()->affinity();
if (isInputMode && !frame->editor()->client()->shouldChangeSelectedRange(oldRange.get(), newRange.get(), affinity, false))
return false;
return frame->selection()->setSelectedRange(newRange.get(), affinity, true);
}
void SelectionHandler::selectObject(const WebCore::IntPoint& location, TextGranularity granularity)
{
ASSERT(location.x() >= 0 && location.y() >= 0);
ASSERT(m_webPage && m_webPage->focusedOrMainFrame() && m_webPage->focusedOrMainFrame()->selection());
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::selectObject adjusted points %d, %d", location.x(), location.y());
WebCore::IntPoint relativePoint = DOMSupport::convertPointToFrame(m_webPage->mainFrame(), focusedFrame, location);
VisiblePosition pointLocation(focusedFrame->visiblePositionForPoint(relativePoint));
VisibleSelection selection = VisibleSelection(pointLocation, pointLocation);
m_selectionActive = expandSelectionToGranularity(focusedFrame, selection, granularity, m_webPage->m_inputHandler->isInputMode());
}
void SelectionHandler::selectObject(TextGranularity granularity)
{
ASSERT(m_webPage && m_webPage->m_inputHandler);
if (!m_webPage->m_inputHandler->isInputMode())
return;
ASSERT(m_webPage->focusedOrMainFrame() && m_webPage->focusedOrMainFrame()->selection());
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::selectObject using current selection");
ASSERT(focusedFrame->selection()->selectionType() != VisibleSelection::NoSelection);
m_selectionActive = expandSelectionToGranularity(focusedFrame, focusedFrame->selection()->selection(), granularity, true );
}
void SelectionHandler::selectObject(Node* node)
{
if (!node)
return;
m_selectionActive = true;
ASSERT(m_webPage && m_webPage->focusedOrMainFrame() && m_webPage->focusedOrMainFrame()->selection());
Frame* focusedFrame = m_webPage->focusedOrMainFrame();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::selectNode");
VisibleSelection selection = VisibleSelection::selectionFromContentsOfNode(node);
focusedFrame->selection()->setSelection(selection);
}
static TextDirection directionOfEnclosingBlock(FrameSelection* selection)
{
Node* enclosingBlockNode = enclosingBlock(selection->selection().extent().deprecatedNode());
if (!enclosingBlockNode)
return LTR;
if (RenderObject* renderer = enclosingBlockNode->renderer())
return renderer->style()->direction();
return LTR;
}
static inline int comparePointsToReferencePoint(const WebCore::IntPoint& p1, const WebCore::IntPoint& p2, const WebCore::IntPoint& referencePoint, bool rightGravity)
{
int dy1 = abs(referencePoint.y() - p1.y());
int dy2 = abs(referencePoint.y() - p2.y());
if (dy1 != dy2)
return dy2 - dy1;
if (p1.x() == p2.x())
return 0;
if (p1.x() > p2.x())
return rightGravity ? 1 : -1;
return rightGravity ? -1 : 1;
}
static WebCore::IntPoint minXMinYCorner(const WebCore::IntRect& rect) { return rect.location(); } static WebCore::IntPoint maxXMinYCorner(const WebCore::IntRect& rect) { return WebCore::IntPoint(rect.x() + rect.width() - 1, rect.y()); } static WebCore::IntPoint minXMaxYCorner(const WebCore::IntRect& rect) { return WebCore::IntPoint(rect.x(), rect.y() + rect.height() - 1); } static WebCore::IntPoint maxXMaxYCorner(const WebCore::IntRect& rect) { return WebCore::IntPoint(rect.x() + rect.width() - 1, rect.y() + rect.height() - 1); }
static inline bool caretIsOnLeft(bool isStartCaret, bool isRTL)
{
if (isStartCaret)
return !isRTL;
return isRTL;
}
static inline WebCore::IntPoint caretLocationForRect(const WebCore::IntRect& rect, bool isStartCaret, bool isRTL)
{
return caretIsOnLeft(isStartCaret, isRTL) ? minXMinYCorner(rect) : maxXMinYCorner(rect);
}
static inline WebCore::IntPoint caretComparisonPointForRect(const WebCore::IntRect& rect, bool isStartCaret, bool isRTL)
{
if (isStartCaret)
return caretIsOnLeft(isStartCaret, isRTL) ? minXMinYCorner(rect) : maxXMinYCorner(rect);
return caretIsOnLeft(isStartCaret, isRTL) ? minXMaxYCorner(rect) : maxXMaxYCorner(rect);
}
static void adjustCaretRects(WebCore::IntRect& startCaret, bool isStartCaretClippedOut,
WebCore::IntRect& endCaret, bool isEndCaretClippedOut,
const std::vector<Platform::IntRect> rectList,
const WebCore::IntPoint& startReferencePoint,
const WebCore::IntPoint& endReferencePoint,
bool isRTL)
{
if (isStartCaretClippedOut)
startCaret.setLocation(DOMSupport::InvalidPoint);
else {
startCaret = rectList[0];
startCaret.setLocation(caretLocationForRect(startCaret, true, isRTL));
}
if (isEndCaretClippedOut)
endCaret.setLocation(DOMSupport::InvalidPoint);
else {
endCaret = rectList[0];
endCaret.setLocation(caretLocationForRect(endCaret, false, isRTL));
}
if (isStartCaretClippedOut && isEndCaretClippedOut)
return;
startCaret.setWidth(1);
endCaret.setWidth(1);
for (unsigned i = 1; i < rectList.size(); i++) {
WebCore::IntRect currentRect(rectList[i]);
if (!isStartCaretClippedOut && comparePointsToReferencePoint(
caretComparisonPointForRect(currentRect, true, isRTL),
caretComparisonPointForRect(startCaret, true, isRTL),
startReferencePoint, isRTL) > 0) {
startCaret.setLocation(caretLocationForRect(currentRect, true, isRTL));
startCaret.setHeight(currentRect.height());
}
if (!isEndCaretClippedOut && comparePointsToReferencePoint(
caretComparisonPointForRect(currentRect, false, isRTL),
caretComparisonPointForRect(endCaret, false, isRTL),
endReferencePoint, !isRTL) > 0) {
endCaret.setLocation(caretLocationForRect(currentRect, false, isRTL));
endCaret.setHeight(currentRect.height());
}
}
}
WebCore::IntPoint SelectionHandler::clipPointToVisibleContainer(const WebCore::IntPoint& point) const
{
ASSERT(m_webPage->m_mainFrame && m_webPage->m_mainFrame->view());
Frame* frame = m_webPage->focusedOrMainFrame();
WebCore::IntPoint clippedPoint = DOMSupport::convertPointToFrame(m_webPage->mainFrame(), frame, point, true );
if (m_webPage->m_inputHandler->isInputMode()
&& frame->document()->focusedNode()
&& frame->document()->focusedNode()->renderer()) {
WebCore::IntRect boundingBox(frame->document()->focusedNode()->renderer()->absoluteBoundingBoxRect());
boundingBox.inflate(-1);
clippedPoint = WebCore::IntPoint(clamp(boundingBox.x(), clippedPoint.x(), boundingBox.maxX()), clamp(boundingBox.y(), clippedPoint.y(), boundingBox.maxY()));
}
return clippedPoint;
}
static WebCore::IntPoint referencePoint(const VisiblePosition& position, const WebCore::IntRect& boundingRect, const WebCore::IntPoint& framePosition, bool isStartCaret, bool isRTL)
{
WebCore::IntRect startCaretBounds(position.absoluteCaretBounds());
if (startCaretBounds.isEmpty())
startCaretBounds = boundingRect;
else
startCaretBounds.move(framePosition.x(), framePosition.y());
return caretComparisonPointForRect(startCaretBounds, isStartCaret, isRTL);
}
static bool regionRectListContainsPoint(const IntRectRegion& region, const WebCore::IntPoint& point)
{
if (!region.extents().contains(point))
return false;
std::vector<Platform::IntRect> rectList = region.rects();
for (unsigned int i = 0; i < rectList.size(); i++) {
if (rectList[i].contains(point))
return true;
}
return false;
}
bool SelectionHandler::inputNodeOverridesTouch() const
{
if (!m_webPage->m_inputHandler->isInputMode())
return false;
Node* focusedNode = m_webPage->focusedOrMainFrame()->document()->focusedNode();
if (!focusedNode || !focusedNode->isElementNode())
return false;
DEFINE_STATIC_LOCAL(QualifiedName, selectionTouchOverrideAttr, (nullAtom, "data-blackberry-end-selection-on-touch", nullAtom));
Element* element = static_cast<Element*>(focusedNode);
return DOMSupport::elementAttributeState(element, selectionTouchOverrideAttr) == DOMSupport::On;
}
void SelectionHandler::selectionPositionChanged(bool visualChangeOnly)
{
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::selectionPositionChanged visibleChangeOnly = %s", visualChangeOnly ? "true" : "false");
if (!m_webPage->m_mainFrame)
return;
if (m_webPage->m_inputHandler->isInputMode() && m_webPage->m_inputHandler->processingChange()) {
m_webPage->m_client->cancelSelectionVisuals();
return;
}
if (m_caretActive || (m_webPage->m_inputHandler->isInputMode() && m_webPage->focusedOrMainFrame()->selection()->isCaret())) {
caretPositionChanged();
}
Frame* frame = m_webPage->focusedOrMainFrame();
WebCore::IntPoint framePos = m_webPage->frameOffset(frame);
if (m_selectionActive && (m_caretActive || frame->selection()->isNone()))
m_selectionActive = false;
else if (frame->selection()->isRange())
m_selectionActive = true;
else if (!m_selectionActive)
return;
WebCore::IntRect startCaret;
WebCore::IntRect endCaret;
Vector<FloatQuad> quads;
DOMSupport::visibleTextQuads(frame->selection()->selection(), quads);
IntRectRegion unclippedRegion;
regionForTextQuads(quads, unclippedRegion, false );
if (visualChangeOnly && m_lastSelectionRegion.isEqual(unclippedRegion))
return;
m_lastSelectionRegion = unclippedRegion;
IntRectRegion visibleSelectionRegion;
if (!unclippedRegion.isEmpty()) {
WebCore::IntRect unclippedStartCaret;
WebCore::IntRect unclippedEndCaret;
bool isRTL = directionOfEnclosingBlock(frame->selection()) == RTL;
WebCore::IntPoint startCaretReferencePoint = referencePoint(frame->selection()->selection().visibleStart(), unclippedRegion.extents(), framePos, true , isRTL);
WebCore::IntPoint endCaretReferencePoint = referencePoint(frame->selection()->selection().visibleEnd(), unclippedRegion.extents(), framePos, false , isRTL);
adjustCaretRects(unclippedStartCaret, false , unclippedEndCaret, false , unclippedRegion.rects(), startCaretReferencePoint, endCaretReferencePoint, isRTL);
regionForTextQuads(quads, visibleSelectionRegion);
#if SHOWDEBUG_SELECTIONHANDLER // Don't rely just on DEBUG_SELECTION to avoid loop.
for (unsigned int i = 0; i < unclippedRegion.numRects(); i++)
DEBUG_SELECTION(LogLevelCritical, "Rect list - Unmodified #%d, (%d, %d) (%d x %d)", i, unclippedRegion.rects()[i].x(), unclippedRegion.rects()[i].y(), unclippedRegion.rects()[i].width(), unclippedRegion.rects()[i].height());
for (unsigned int i = 0; i < visibleSelectionRegion.numRects(); i++)
DEBUG_SELECTION(LogLevelCritical, "Rect list - Clipped to Visible #%d, (%d, %d) (%d x %d)", i, visibleSelectionRegion.rects()[i].x(), visibleSelectionRegion.rects()[i].y(), visibleSelectionRegion.rects()[i].width(), visibleSelectionRegion.rects()[i].height());
#endif
bool shouldCareAboutPossibleClippedOutSelection = frame != m_webPage->mainFrame() || m_webPage->m_inputHandler->isInputMode();
if (!visibleSelectionRegion.isEmpty() || shouldCareAboutPossibleClippedOutSelection) {
bool shouldClipStartCaret = !regionRectListContainsPoint(visibleSelectionRegion, unclippedStartCaret.location());
bool shouldClipEndCaret = !regionRectListContainsPoint(visibleSelectionRegion, unclippedEndCaret.location());
adjustCaretRects(startCaret, shouldClipStartCaret, endCaret, shouldClipEndCaret, visibleSelectionRegion.rects(), startCaretReferencePoint, endCaretReferencePoint, isRTL);
if (!shouldClipStartCaret) {
startCaret = m_webPage->mapToTransformed(startCaret);
m_webPage->clipToTransformedContentsRect(startCaret);
}
if (!shouldClipEndCaret) {
endCaret = m_webPage->mapToTransformed(endCaret);
m_webPage->clipToTransformedContentsRect(endCaret);
}
}
}
DEBUG_SELECTION(BlackBerry::Platform::LogLevelInfo, "SelectionHandler::selectionPositionChanged Start Rect=(%d, %d) (%d x %d) End Rect=(%d, %d) (%d x %d)",
startCaret.x(), startCaret.y(), startCaret.width(), startCaret.height(), endCaret.x(), endCaret.y(), endCaret.width(), endCaret.height());
m_webPage->m_client->notifySelectionDetailsChanged(startCaret, endCaret, visibleSelectionRegion, inputNodeOverridesTouch());
}
void SelectionHandler::caretPositionChanged()
{
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::caretPositionChanged");
WebCore::IntRect caretLocation;
if (!m_webPage->m_inputHandler->isInputMode() && m_caretActive) {
m_caretActive = false;
m_webPage->m_client->notifyCaretChanged(caretLocation, m_webPage->m_touchEventHandler->lastFatFingersResult().isTextInput() );
return;
}
ASSERT(m_webPage && m_webPage->focusedOrMainFrame() && m_webPage->focusedOrMainFrame()->selection());
ASSERT(m_webPage->m_inputHandler->isInputMode());
if (m_webPage->focusedOrMainFrame()->selection()->selectionType() == VisibleSelection::CaretSelection) {
WebCore::IntPoint frameOffset = m_webPage->frameOffset(m_webPage->focusedOrMainFrame());
caretLocation = m_webPage->focusedOrMainFrame()->selection()->selection().visibleStart().absoluteCaretBounds();
caretLocation.move(frameOffset.x(), frameOffset.y());
caretLocation.intersect(clippingRectForVisibleContent());
}
m_caretActive = !caretLocation.isEmpty();
DEBUG_SELECTION(LogLevelInfo, "SelectionHandler::caretPositionChanged caret Rect %d, %d, %dx%d",
caretLocation.x(), caretLocation.y(), caretLocation.width(), caretLocation.height());
caretLocation = m_webPage->mapToTransformed(caretLocation);
m_webPage->clipToTransformedContentsRect(caretLocation);
bool singleLineInput = !m_webPage->m_inputHandler->isMultilineInputMode();
WebCore::IntRect nodeBoundingBox = singleLineInput ? m_webPage->m_inputHandler->boundingBoxForInputField() : WebCore::IntRect();
m_webPage->m_client->notifyCaretChanged(caretLocation, m_webPage->m_touchEventHandler->lastFatFingersResult().isTextInput() , singleLineInput, nodeBoundingBox);
}
bool SelectionHandler::selectionContains(const WebCore::IntPoint& point)
{
ASSERT(m_webPage && m_webPage->focusedOrMainFrame() && m_webPage->focusedOrMainFrame()->selection());
return m_webPage->focusedOrMainFrame()->selection()->contains(point);
}
}
}