FocusController.cpp [plain text]
#include "config.h"
#include "FocusController.h"
#include "AXObjectCache.h"
#include "Chrome.h"
#include "Document.h"
#include "Editor.h"
#include "EditorClient.h"
#include "Element.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLNames.h"
#include "KeyboardEvent.h"
#include "Page.h"
#include "Range.h"
#include "RenderObject.h"
#include "RenderWidget.h"
#include "SelectionController.h"
#include "Settings.h"
#include "SpatialNavigation.h"
#include "Widget.h"
namespace WebCore {
using namespace HTMLNames;
using namespace std;
static inline void dispatchEventsOnWindowAndFocusedNode(Document* document, bool focused)
{
if (Page* page = document->page()) {
if (page->defersLoading())
return;
}
if (!focused && document->focusedNode())
document->focusedNode()->dispatchBlurEvent();
document->dispatchWindowEvent(Event::create(focused ? eventNames().focusEvent : eventNames().blurEvent, false, false));
if (focused && document->focusedNode())
document->focusedNode()->dispatchFocusEvent();
}
FocusController::FocusController(Page* page)
: m_page(page)
, m_isActive(false)
, m_isFocused(false)
, m_isChangingFocusedFrame(false)
{
}
void FocusController::setFocusedFrame(PassRefPtr<Frame> frame)
{
if (m_focusedFrame == frame || m_isChangingFocusedFrame)
return;
m_isChangingFocusedFrame = true;
RefPtr<Frame> oldFrame = m_focusedFrame;
RefPtr<Frame> newFrame = frame;
m_focusedFrame = newFrame;
if (oldFrame && oldFrame->view()) {
oldFrame->selection()->setFocused(false);
oldFrame->document()->dispatchWindowEvent(Event::create(eventNames().blurEvent, false, false));
}
if (newFrame && newFrame->view() && isFocused()) {
newFrame->selection()->setFocused(true);
newFrame->document()->dispatchWindowEvent(Event::create(eventNames().focusEvent, false, false));
}
m_isChangingFocusedFrame = false;
}
Frame* FocusController::focusedOrMainFrame()
{
if (Frame* frame = focusedFrame())
return frame;
return m_page->mainFrame();
}
void FocusController::setFocused(bool focused)
{
if (isFocused() == focused)
return;
m_isFocused = focused;
if (m_focusedFrame && m_focusedFrame->view()) {
m_focusedFrame->selection()->setFocused(focused);
dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), focused);
}
}
static Node* deepFocusableNode(FocusDirection direction, Node* node, KeyboardEvent* event)
{
while (node && node->isFrameOwnerElement()) {
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
if (!owner->contentFrame())
break;
Document* document = owner->contentFrame()->document();
node = (direction == FocusDirectionForward)
? document->nextFocusableNode(0, event)
: document->previousFocusableNode(0, event);
if (!node) {
node = owner;
break;
}
}
return node;
}
bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event)
{
return advanceFocus(direction, event, true);
}
bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
{
switch (direction) {
case FocusDirectionForward:
case FocusDirectionBackward:
return advanceFocusInDocumentOrder(direction, event, initialFocus);
case FocusDirectionLeft:
case FocusDirectionRight:
case FocusDirectionUp:
case FocusDirectionDown:
return advanceFocusDirectionally(direction, event);
default:
ASSERT_NOT_REACHED();
}
return false;
}
bool FocusController::advanceFocusInDocumentOrder(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
{
Frame* frame = focusedOrMainFrame();
ASSERT(frame);
Document* document = frame->document();
Node* currentNode = document->focusedNode();
bool caretBrowsing = focusedOrMainFrame()->settings()->caretBrowsingEnabled();
if (caretBrowsing && !currentNode)
currentNode = frame->selection()->start().node();
document->updateLayoutIgnorePendingStylesheets();
Node* node = (direction == FocusDirectionForward)
? document->nextFocusableNode(currentNode, event)
: document->previousFocusableNode(currentNode, event);
while (!node && frame) {
Frame* parentFrame = frame->tree()->parent();
if (!parentFrame)
break;
Document* parentDocument = parentFrame->document();
HTMLFrameOwnerElement* owner = frame->ownerElement();
if (!owner)
break;
node = (direction == FocusDirectionForward)
? parentDocument->nextFocusableNode(owner, event)
: parentDocument->previousFocusableNode(owner, event);
frame = parentFrame;
}
node = deepFocusableNode(direction, node, event);
if (!node) {
if (!initialFocus && m_page->chrome()->canTakeFocus(direction)) {
document->setFocusedNode(0);
setFocusedFrame(0);
m_page->chrome()->takeFocus(direction);
return true;
}
Document* d = m_page->mainFrame()->document();
node = (direction == FocusDirectionForward)
? d->nextFocusableNode(0, event)
: d->previousFocusableNode(0, event);
node = deepFocusableNode(direction, node, event);
if (!node)
return false;
}
ASSERT(node);
if (node == document->focusedNode())
return true;
if (!node->isElementNode())
return false;
if (node->isFrameOwnerElement()) {
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
if (!owner->contentFrame())
return false;
document->setFocusedNode(0);
setFocusedFrame(owner->contentFrame());
return true;
}
Document* newDocument = node->document();
if (newDocument != document)
document->setFocusedNode(0);
if (newDocument)
setFocusedFrame(newDocument->frame());
if (caretBrowsing) {
VisibleSelection newSelection(Position(node, 0), Position(node, 0), DOWNSTREAM);
if (frame->shouldChangeSelection(newSelection))
frame->selection()->setSelection(newSelection);
}
static_cast<Element*>(node)->focus(false);
return true;
}
bool FocusController::advanceFocusDirectionally(FocusDirection direction, KeyboardEvent* event)
{
Frame* frame = focusedOrMainFrame();
ASSERT(frame);
Document* focusedDocument = frame->document();
if (!focusedDocument)
return false;
Node* focusedNode = focusedDocument->focusedNode();
if (!focusedNode) {
FocusDirection tabDirection = (direction == FocusDirectionUp || direction == FocusDirectionLeft) ?
FocusDirectionForward : FocusDirectionBackward;
return advanceFocusInDocumentOrder(tabDirection, event, true);
}
frame = frame->tree()->top();
FocusCandidate focusCandidate;
findFocusableNodeInDirection(frame->document()->firstChild(), focusedNode, direction, event, focusCandidate);
Node* node = focusCandidate.node;
if (!node || !node->isElementNode()) {
Frame* frame = focusedOrMainFrame();
scrollInDirection(frame, direction);
return false;
}
if (hasOffscreenRect(node)) {
Frame* frame = node->document()->view()->frame();
scrollInDirection(frame, direction);
return true;
}
Document* newDocument = node->document();
if (newDocument != focusedDocument) {
focusedDocument->setFocusedNode(0);
}
if (newDocument)
setFocusedFrame(newDocument->frame());
Element* element = static_cast<Element*>(node);
ASSERT(element);
scrollIntoView(element);
element->focus(false);
return true;
}
static void updateFocusCandidateIfCloser(Node* focusedNode, const FocusCandidate& candidate, FocusCandidate& closest)
{
bool sameDocument = candidate.document() == closest.document();
if (sameDocument) {
if (closest.alignment > candidate.alignment
|| (closest.parentAlignment && candidate.alignment > closest.parentAlignment))
return;
} else if (closest.alignment > candidate.alignment
&& (closest.parentAlignment && candidate.alignment > closest.parentAlignment))
return;
if (candidate.alignment != None
|| (closest.parentAlignment >= candidate.alignment
&& closest.document() == candidate.document())) {
if (closest.alignment < candidate.alignment
&& closest.parentAlignment < candidate.alignment)
closest.distance = maxDistance();
}
if (candidate.distance >= closest.distance)
return;
if (closest.isNull()) {
closest = candidate;
return;
}
if (focusedNode->document() == candidate.document()
&& candidate.distance < closest.parentDistance)
closest = candidate;
else if (focusedNode->document() != candidate.document()) {
if (!((isInRootDocument(candidate.node) && !isInRootDocument(focusedNode))
&& focusedNode->document() == closest.document()))
closest = candidate;
}
}
void FocusController::findFocusableNodeInDirection(Node* outer, Node* focusedNode,
FocusDirection direction, KeyboardEvent* event,
FocusCandidate& closestFocusCandidate,
const FocusCandidate& candidateParent)
{
ASSERT(outer);
ASSERT(candidateParent.isNull()
|| candidateParent.node->hasTagName(frameTag)
|| candidateParent.node->hasTagName(iframeTag));
Node* candidate = outer;
while (candidate) {
if (candidate->isFrameOwnerElement())
deepFindFocusableNodeInDirection(candidate, focusedNode, direction, event, closestFocusCandidate);
else if (candidate != focusedNode && candidate->isKeyboardFocusable(event)) {
FocusCandidate currentFocusCandidate(candidate);
distanceDataForNode(direction, focusedNode, currentFocusCandidate);
if (currentFocusCandidate.distance == maxDistance()) {
candidate = candidate->traverseNextNode(outer->parent());
continue;
}
if (!candidateParent.isNull()) {
currentFocusCandidate.parentAlignment = candidateParent.alignment;
currentFocusCandidate.parentDistance = candidateParent.distance;
}
updateFocusCandidateIfCloser(focusedNode, currentFocusCandidate, closestFocusCandidate);
}
candidate = candidate->traverseNextNode(outer->parent());
}
}
void FocusController::deepFindFocusableNodeInDirection(Node* container, Node* focusedNode,
FocusDirection direction, KeyboardEvent* event,
FocusCandidate& closestFocusCandidate)
{
ASSERT(container->hasTagName(frameTag) || container->hasTagName(iframeTag));
bool descendantOfContainer = false;
Node* firstChild = 0;
if (container->hasTagName(frameTag) || container->hasTagName(iframeTag)) {
HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(container);
if (!owner->contentFrame())
return;
Document* innerDocument = owner->contentFrame()->document();
if (!innerDocument)
return;
descendantOfContainer = innerDocument == focusedNode->document();
firstChild = innerDocument->firstChild();
}
if (descendantOfContainer) {
findFocusableNodeInDirection(firstChild, focusedNode, direction, event, closestFocusCandidate);
return;
}
FocusCandidate candidateParent = FocusCandidate(container);
distanceDataForNode(direction, focusedNode, candidateParent);
if (candidateParent.distance == maxDistance())
return;
if (candidateParent.distance < closestFocusCandidate.distance)
findFocusableNodeInDirection(firstChild, focusedNode, direction, event, closestFocusCandidate, candidateParent);
}
static bool relinquishesEditingFocus(Node *node)
{
ASSERT(node);
ASSERT(node->isContentEditable());
Node* root = node->rootEditableElement();
Frame* frame = node->document()->frame();
if (!frame || !root)
return false;
return frame->editor()->shouldEndEditing(rangeOfContents(root).get());
}
static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode)
{
if (!oldFocusedFrame || !newFocusedFrame)
return;
if (oldFocusedFrame->document() != newFocusedFrame->document())
return;
SelectionController* s = oldFocusedFrame->selection();
if (s->isNone())
return;
bool caretBrowsing = oldFocusedFrame->settings()->caretBrowsingEnabled();
if (caretBrowsing)
return;
Node* selectionStartNode = s->selection().start().node();
if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->shadowAncestorNode() == newFocusedNode)
return;
if (Node* mousePressNode = newFocusedFrame->eventHandler()->mousePressNode())
if (mousePressNode->renderer() && !mousePressNode->canStartSelection())
if (Node* root = s->rootEditableElement())
if (Node* shadowAncestorNode = root->shadowAncestorNode())
if (!shadowAncestorNode->hasTagName(inputTag) && !shadowAncestorNode->hasTagName(textareaTag))
return;
s->clear();
}
bool FocusController::setFocusedNode(Node* node, PassRefPtr<Frame> newFocusedFrame)
{
RefPtr<Frame> oldFocusedFrame = focusedFrame();
RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : 0;
Node* oldFocusedNode = oldDocument ? oldDocument->focusedNode() : 0;
if (oldFocusedNode == node)
return true;
if (oldFocusedNode && oldFocusedNode->rootEditableElement() == oldFocusedNode && !relinquishesEditingFocus(oldFocusedNode))
return false;
clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), node);
if (!node) {
if (oldDocument)
oldDocument->setFocusedNode(0);
m_page->editorClient()->setInputMethodState(false);
return true;
}
RefPtr<Document> newDocument = node->document();
if (newDocument && newDocument->focusedNode() == node) {
m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
return true;
}
if (oldDocument && oldDocument != newDocument)
oldDocument->setFocusedNode(0);
setFocusedFrame(newFocusedFrame);
RefPtr<Node> protect = node;
if (newDocument) {
bool successfullyFocused = newDocument->setFocusedNode(node);
if (!successfullyFocused)
return false;
}
if (newDocument->focusedNode() == node)
m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
return true;
}
void FocusController::setActive(bool active)
{
if (m_isActive == active)
return;
m_isActive = active;
if (FrameView* view = m_page->mainFrame()->view()) {
if (!view->platformWidget()) {
view->layoutIfNeededRecursive();
view->updateControlTints();
}
}
focusedOrMainFrame()->selection()->pageActivationChanged();
if (m_focusedFrame && isFocused())
dispatchEventsOnWindowAndFocusedNode(m_focusedFrame->document(), active);
}
}