2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/editing/Caret.h"
29 #include "core/dom/Document.h"
30 #include "core/editing/htmlediting.h"
31 #include "core/frame/Frame.h"
32 #include "core/frame/Settings.h"
33 #include "core/rendering/RenderBlock.h"
34 #include "core/rendering/RenderView.h"
38 CaretBase::CaretBase(CaretVisibility visibility)
39 : m_caretRectNeedsUpdate(true)
40 , m_caretVisibility(visibility)
44 DragCaretController::DragCaretController()
49 PassOwnPtr<DragCaretController> DragCaretController::create()
51 return adoptPtr(new DragCaretController);
54 bool DragCaretController::isContentRichlyEditable() const
56 return isRichlyEditablePosition(m_position.deepEquivalent());
59 void DragCaretController::setCaretPosition(const VisiblePosition& position)
61 if (Node* node = m_position.deepEquivalent().deprecatedNode())
62 invalidateCaretRect(node);
63 m_position = position;
64 setCaretRectNeedsUpdate();
65 Document* document = 0;
66 if (Node* node = m_position.deepEquivalent().deprecatedNode()) {
67 invalidateCaretRect(node);
68 document = &node->document();
70 if (m_position.isNull() || m_position.isOrphan())
73 updateCaretRect(document, m_position);
76 static bool removingNodeRemovesPosition(Node& node, const Position& position)
78 if (!position.anchorNode())
81 if (position.anchorNode() == node)
84 if (!node.isElementNode())
87 Element& element = toElement(node);
88 return element.containsIncludingShadowDOM(position.anchorNode());
91 void DragCaretController::nodeWillBeRemoved(Node& node)
93 if (!hasCaret() || !node.inActiveDocument())
96 if (!removingNodeRemovesPosition(node, m_position.deepEquivalent()))
99 m_position.deepEquivalent().document()->renderView()->clearSelection();
103 void CaretBase::clearCaretRect()
105 m_caretLocalRect = LayoutRect();
108 static inline bool caretRendersInsideNode(Node* node)
110 return node && !isRenderedTable(node) && !editingIgnoresContent(node);
113 RenderObject* CaretBase::caretRenderer(Node* node)
118 RenderObject* renderer = node->renderer();
122 // if caretNode is a block and caret is inside it then caret should be painted by that block
123 bool paintedByBlock = renderer->isRenderBlock() && caretRendersInsideNode(node);
124 return paintedByBlock ? renderer : renderer->containingBlock();
127 bool CaretBase::updateCaretRect(Document* document, const VisiblePosition& caretPosition)
129 document->updateStyleIfNeeded();
130 m_caretLocalRect = LayoutRect();
132 m_caretRectNeedsUpdate = false;
134 if (caretPosition.isNull())
137 ASSERT(caretPosition.deepEquivalent().deprecatedNode()->renderer());
139 // First compute a rect local to the renderer at the selection start.
140 RenderObject* renderer;
141 LayoutRect localRect = caretPosition.localCaretRect(renderer);
143 // Get the renderer that will be responsible for painting the caret
144 // (which is either the renderer we just found, or one of its containers).
145 RenderObject* caretPainter = caretRenderer(caretPosition.deepEquivalent().deprecatedNode());
147 // Compute an offset between the renderer and the caretPainter.
148 bool unrooted = false;
149 while (renderer != caretPainter) {
150 RenderObject* containerObject = renderer->container();
151 if (!containerObject) {
155 localRect.move(renderer->offsetFromContainer(containerObject, localRect.location()));
156 renderer = containerObject;
160 m_caretLocalRect = localRect;
165 RenderObject* DragCaretController::caretRenderer() const
167 return CaretBase::caretRenderer(m_position.deepEquivalent().deprecatedNode());
170 IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, const LayoutRect& rect) const
172 RenderObject* caretPainter = caretRenderer(node);
176 LayoutRect localRect(rect);
177 if (caretPainter->isBox())
178 toRenderBox(caretPainter)->flipForWritingMode(localRect);
179 return caretPainter->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox();
182 void CaretBase::repaintCaretForLocalRect(Node* node, const LayoutRect& rect)
184 RenderObject* caretPainter = caretRenderer(node);
188 // FIXME: Need to over-paint 1 pixel to workaround some rounding problems.
189 // https://bugs.webkit.org/show_bug.cgi?id=108283
190 LayoutRect inflatedRect = rect;
191 inflatedRect.inflate(1);
193 caretPainter->repaintRectangle(inflatedRect);
196 bool CaretBase::shouldRepaintCaret(const RenderView* view, bool isContentEditable) const
199 bool caretBrowsing = false;
200 if (FrameView* frameView = view->frameView()) {
201 Frame& frame = frameView->frame(); // The frame where the selection started
202 caretBrowsing = frame.settings() && frame.settings()->caretBrowsingEnabled();
204 return (caretBrowsing || isContentEditable);
207 void CaretBase::invalidateCaretRect(Node* node, bool caretRectChanged)
209 // EDIT FIXME: This is an unfortunate hack.
210 // Basically, we can't trust this layout position since we
211 // can't guarantee that the check to see if we are in unrendered
212 // content will work at this point. We may have to wait for
213 // a layout and re-render of the document to happen. So, resetting this
214 // flag will cause another caret layout to happen the first time
215 // that we try to paint the caret after this call. That one will work since
216 // it happens after the document has accounted for any editing
217 // changes which may have been done.
218 // And, we need to leave this layout here so the caret moves right
219 // away after clicking.
220 m_caretRectNeedsUpdate = true;
222 if (caretRectChanged)
225 if (RenderView* view = node->document().renderView()) {
226 if (shouldRepaintCaret(view, node->isContentEditable(Node::UserSelectAllIsAlwaysNonEditable)))
227 repaintCaretForLocalRect(node, localCaretRectWithoutUpdate());
231 void CaretBase::paintCaret(Node* node, GraphicsContext* context, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const
233 if (m_caretVisibility == Hidden)
236 LayoutRect drawingRect = localCaretRectWithoutUpdate();
237 RenderObject* renderer = caretRenderer(node);
238 if (renderer && renderer->isBox())
239 toRenderBox(renderer)->flipForWritingMode(drawingRect);
240 drawingRect.moveBy(roundedIntPoint(paintOffset));
241 LayoutRect caret = intersection(drawingRect, clipRect);
245 Color caretColor = Color::black;
248 if (node->isElementNode())
249 element = toElement(node);
251 element = node->parentElement();
253 if (element && element->renderer())
254 caretColor = element->renderer()->resolveColor(CSSPropertyColor);
256 context->fillRect(caret, caretColor);
259 void DragCaretController::paintDragCaret(Frame* frame, GraphicsContext* p, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const
261 if (m_position.deepEquivalent().deprecatedNode()->document().frame() == frame)
262 paintCaret(m_position.deepEquivalent().deprecatedNode(), p, paintOffset, clipRect);