webengine/osswebengine/WebCore/page/FocusController.cpp
changeset 0 dd21522fd290
child 26 cb62a4f66ebe
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    24  */
       
    25 
       
    26 #include "config.h"
       
    27 #include "FocusController.h"
       
    28 
       
    29 #include "AXObjectCache.h"
       
    30 #include "Chrome.h"
       
    31 #include "Document.h"
       
    32 #include "Editor.h"
       
    33 #include "EditorClient.h"
       
    34 #include "Element.h"
       
    35 #include "Event.h"
       
    36 #include "EventHandler.h"
       
    37 #include "EventNames.h"
       
    38 #include "Frame.h"
       
    39 #include "FrameView.h"
       
    40 #include "FrameTree.h"
       
    41 #include "HTMLFrameOwnerElement.h"
       
    42 #include "HTMLNames.h"
       
    43 #include "KeyboardEvent.h"
       
    44 #include "Page.h"
       
    45 #include "Range.h"
       
    46 #include "RenderObject.h"
       
    47 #include "RenderWidget.h"
       
    48 #include "SelectionController.h"
       
    49 #include "Widget.h"
       
    50 #include <wtf/Platform.h>
       
    51 
       
    52 namespace WebCore {
       
    53 
       
    54 using namespace EventNames;
       
    55 using namespace HTMLNames;
       
    56 
       
    57 FocusController::FocusController(Page* page)
       
    58     : m_page(page)
       
    59 {
       
    60 }
       
    61 
       
    62 void FocusController::setFocusedFrame(PassRefPtr<Frame> frame)
       
    63 {
       
    64     if (m_focusedFrame == frame)
       
    65         return;
       
    66 
       
    67     if (m_focusedFrame) {
       
    68         m_focusedFrame->setWindowHasFocus(false);
       
    69         m_focusedFrame->setIsActive(false);
       
    70     }
       
    71 
       
    72     m_focusedFrame = frame;
       
    73 
       
    74     if (m_focusedFrame) {
       
    75         m_focusedFrame->setWindowHasFocus(true);
       
    76         m_focusedFrame->setIsActive(true);
       
    77     }
       
    78 }
       
    79 
       
    80 Frame* FocusController::focusedOrMainFrame()
       
    81 {
       
    82     if (Frame* frame = focusedFrame())
       
    83         return frame;
       
    84     return m_page->mainFrame();
       
    85 }
       
    86 
       
    87 static Node* deepFocusableNode(FocusDirection direction, Node* node, KeyboardEvent* event)
       
    88 {
       
    89     // The node we found might be a HTMLFrameOwnerElement, so descend down the frame tree until we find either:
       
    90     // 1) a focusable node, or
       
    91     // 2) the deepest-nested HTMLFrameOwnerElement
       
    92     while (node && node->isFrameOwnerElement()) {
       
    93         HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
       
    94         if (!owner->contentFrame())
       
    95             break;
       
    96 
       
    97         Document* document = owner->contentFrame()->document();
       
    98         if (!document)
       
    99             break;
       
   100 
       
   101         node = (direction == FocusDirectionForward)
       
   102             ? document->nextFocusableNode(0, event)
       
   103             : document->previousFocusableNode(0, event);
       
   104         if (!node) {
       
   105             node = owner;
       
   106             break;
       
   107         }
       
   108     }
       
   109 
       
   110     return node;
       
   111 }
       
   112 
       
   113 bool FocusController::setInitialFocus(FocusDirection direction, KeyboardEvent* event)
       
   114 {
       
   115     return advanceFocus(direction, event, true);
       
   116 }
       
   117 
       
   118 bool FocusController::advanceFocus(KeyboardEvent* event)
       
   119 {
       
   120     return advanceFocus((event && event->shiftKey()) ? FocusDirectionBackward : FocusDirectionForward, event);
       
   121 }
       
   122 
       
   123 bool FocusController::advanceFocus(FocusDirection direction, KeyboardEvent* event, bool initialFocus)
       
   124 {
       
   125     Frame* frame = focusedOrMainFrame();
       
   126     ASSERT(frame);
       
   127     Document* document = frame->document();
       
   128     if (!document)
       
   129         return false;
       
   130 
       
   131     Node* node = (direction == FocusDirectionForward)
       
   132         ? document->nextFocusableNode(document->focusedNode(), event)
       
   133         : document->previousFocusableNode(document->focusedNode(), event);
       
   134             
       
   135     // If there's no focusable node to advance to, move up the frame tree until we find one.
       
   136     while (!node && frame) {
       
   137         Frame* parentFrame = frame->tree()->parent();
       
   138         if (!parentFrame)
       
   139             break;
       
   140 
       
   141         Document* parentDocument = parentFrame->document();
       
   142         if (!parentDocument)
       
   143             break;
       
   144 
       
   145         HTMLFrameOwnerElement* owner = frame->ownerElement();
       
   146         if (!owner)
       
   147             break;
       
   148 
       
   149         node = (direction == FocusDirectionForward)
       
   150             ? parentDocument->nextFocusableNode(owner, event)
       
   151             : parentDocument->previousFocusableNode(owner, event);
       
   152 
       
   153         frame = parentFrame;
       
   154     }
       
   155 
       
   156     node = deepFocusableNode(direction, node, event);
       
   157 
       
   158     if (!node) {
       
   159         // We didn't find a node to focus, so we should try to pass focus to Chrome.
       
   160         if (!initialFocus && m_page->chrome()->canTakeFocus(direction)) {
       
   161             document->setFocusedNode(0);
       
   162             setFocusedFrame(0);
       
   163             m_page->chrome()->takeFocus(direction);
       
   164             return true;
       
   165         }
       
   166 
       
   167         // Chrome doesn't want focus, so we should wrap focus.
       
   168         if (Document* d = m_page->mainFrame()->document())
       
   169             node = (direction == FocusDirectionForward)
       
   170                 ? d->nextFocusableNode(0, event)
       
   171                 : d->previousFocusableNode(0, event);
       
   172 
       
   173         node = deepFocusableNode(direction, node, event);
       
   174 
       
   175         if (!node)
       
   176             return false;
       
   177     }
       
   178 
       
   179     ASSERT(node);
       
   180 
       
   181     if (node == document->focusedNode())
       
   182         // Focus wrapped around to the same node.
       
   183         return true;
       
   184 
       
   185     if (!node->isElementNode())
       
   186         // FIXME: May need a way to focus a document here.
       
   187         return false;
       
   188 
       
   189     if (node->isFrameOwnerElement()) {
       
   190         // We focus frames rather than frame owners.
       
   191         // FIXME: We should not focus frames that have no scrollbars, as focusing them isn't useful to the user.
       
   192         HTMLFrameOwnerElement* owner = static_cast<HTMLFrameOwnerElement*>(node);
       
   193         if (!owner->contentFrame())
       
   194             return false;
       
   195 
       
   196         document->setFocusedNode(0);
       
   197         setFocusedFrame(owner->contentFrame());
       
   198         return true;
       
   199     }
       
   200     
       
   201     // FIXME: It would be nice to just be able to call setFocusedNode(node) here, but we can't do
       
   202     // that because some elements (e.g. HTMLInputElement and HTMLTextAreaElement) do extra work in
       
   203     // their focus() methods.
       
   204 
       
   205     Document* newDocument = node->document();
       
   206 
       
   207     if (newDocument != document)
       
   208         // Focus is going away from this document, so clear the focused node.
       
   209         document->setFocusedNode(0);
       
   210 
       
   211     if (newDocument)
       
   212         setFocusedFrame(newDocument->frame());
       
   213 
       
   214     static_cast<Element*>(node)->focus(false);
       
   215     return true;
       
   216 }
       
   217 
       
   218 static bool relinquishesEditingFocus(Node *node)
       
   219 {
       
   220     ASSERT(node);
       
   221     ASSERT(node->isContentEditable());
       
   222 
       
   223     Node* root = node->rootEditableElement();
       
   224     Frame* frame = node->document()->frame();
       
   225     if (!frame || !root)
       
   226         return false;
       
   227 
       
   228     return frame->editor()->shouldEndEditing(rangeOfContents(root).get());
       
   229 }
       
   230 
       
   231 static void clearSelectionIfNeeded(Frame* oldFocusedFrame, Frame* newFocusedFrame, Node* newFocusedNode)
       
   232 {
       
   233     if (!oldFocusedFrame || !newFocusedFrame)
       
   234         return;
       
   235         
       
   236     if (oldFocusedFrame->document() != newFocusedFrame->document())
       
   237         return;
       
   238     
       
   239     SelectionController* s = oldFocusedFrame->selectionController();
       
   240     if (s->isNone())
       
   241         return;
       
   242     
       
   243     Node* selectionStartNode = s->selection().start().node();
       
   244     if (selectionStartNode == newFocusedNode || selectionStartNode->isDescendantOf(newFocusedNode) || selectionStartNode->shadowAncestorNode() == newFocusedNode)
       
   245         return;
       
   246         
       
   247     if (Node* mousePressNode = newFocusedFrame->eventHandler()->mousePressNode())
       
   248         if (mousePressNode->renderer() && !mousePressNode->canStartSelection())
       
   249             if (Node* root = s->rootEditableElement())
       
   250                 if (Node* shadowAncestorNode = root->shadowAncestorNode())
       
   251                     // Don't do this for textareas and text fields, when they lose focus their selections should be cleared
       
   252                     // and then restored when they regain focus, to match other browsers.
       
   253                     if (!shadowAncestorNode->hasTagName(inputTag) && !shadowAncestorNode->hasTagName(textareaTag))
       
   254                         return;
       
   255     
       
   256     s->clear();
       
   257 }
       
   258 
       
   259 bool FocusController::setFocusedNode(Node* node, PassRefPtr<Frame> newFocusedFrame)
       
   260 {
       
   261     RefPtr<Frame> oldFocusedFrame = focusedFrame();
       
   262     RefPtr<Document> oldDocument = oldFocusedFrame ? oldFocusedFrame->document() : 0;
       
   263     
       
   264     Node* oldFocusedNode = oldDocument ? oldDocument->focusedNode() : 0;
       
   265     if (oldFocusedNode == node)
       
   266         return true;
       
   267         
       
   268     if (oldFocusedNode && oldFocusedNode->rootEditableElement() == oldFocusedNode && !relinquishesEditingFocus(oldFocusedNode))
       
   269         return false;
       
   270         
       
   271     clearSelectionIfNeeded(oldFocusedFrame.get(), newFocusedFrame.get(), node);
       
   272     
       
   273     if (!node) {
       
   274         if (oldDocument)
       
   275             oldDocument->setFocusedNode(0);
       
   276         m_page->editorClient()->setInputMethodState(false);
       
   277         return true;
       
   278     }
       
   279     
       
   280     RefPtr<Document> newDocument = node ? node->document() : 0;
       
   281     
       
   282     if (newDocument && newDocument->focusedNode() == node) {
       
   283         m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
       
   284         return true;
       
   285     }
       
   286     
       
   287     if (oldDocument && oldDocument != newDocument)
       
   288         oldDocument->setFocusedNode(0);
       
   289     
       
   290     setFocusedFrame(newFocusedFrame);
       
   291     
       
   292     if (newDocument)
       
   293         newDocument->setFocusedNode(node);
       
   294     
       
   295     m_page->editorClient()->setInputMethodState(node->shouldUseInputMethod());
       
   296 
       
   297     return true;
       
   298 }
       
   299 
       
   300 } // namespace WebCore