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/VisibleUnits.h"
31 #include "core/editing/htmlediting.h"
32 #include "core/frame/LocalFrame.h"
33 #include "core/frame/Settings.h"
34 #include "core/html/HTMLTextFormControlElement.h"
35 #include "core/rendering/RenderBlock.h"
36 #include "core/rendering/RenderView.h"
37 #include "platform/graphics/GraphicsContext.h"
41 CaretBase::CaretBase(CaretVisibility visibility)
42 : m_caretRectNeedsUpdate(true)
43 , m_caretVisibility(visibility)
47 DragCaretController::DragCaretController()
52 PassOwnPtrWillBeRawPtr<DragCaretController> DragCaretController::create()
54 return adoptPtrWillBeNoop(new DragCaretController);
57 bool DragCaretController::isContentRichlyEditable() const
59 return isRichlyEditablePosition(m_position.deepEquivalent());
62 void DragCaretController::setCaretPosition(const VisiblePosition& position)
64 if (Node* node = m_position.deepEquivalent().deprecatedNode())
65 invalidateCaretRect(node);
66 m_position = position;
67 setCaretRectNeedsUpdate();
68 Document* document = 0;
69 if (Node* node = m_position.deepEquivalent().deprecatedNode()) {
70 invalidateCaretRect(node);
71 document = &node->document();
73 if (m_position.isNull() || m_position.isOrphan()) {
76 document->updateRenderTreeIfNeeded();
77 updateCaretRect(document, m_position);
81 static bool removingNodeRemovesPosition(Node& node, const Position& position)
83 if (!position.anchorNode())
86 if (position.anchorNode() == node)
89 if (!node.isElementNode())
92 Element& element = toElement(node);
93 return element.containsIncludingShadowDOM(position.anchorNode());
96 void DragCaretController::nodeWillBeRemoved(Node& node)
98 if (!hasCaret() || !node.inActiveDocument())
101 if (!removingNodeRemovesPosition(node, m_position.deepEquivalent()))
104 m_position.deepEquivalent().document()->renderView()->clearSelection();
108 void DragCaretController::trace(Visitor* visitor)
110 visitor->trace(m_position);
113 void CaretBase::clearCaretRect()
115 m_caretLocalRect = LayoutRect();
118 static inline bool caretRendersInsideNode(Node* node)
120 return node && !isRenderedTableElement(node) && !editingIgnoresContent(node);
123 RenderBlock* CaretBase::caretRenderer(Node* node)
128 RenderObject* renderer = node->renderer();
132 // if caretNode is a block and caret is inside it then caret should be painted by that block
133 bool paintedByBlock = renderer->isRenderBlock() && caretRendersInsideNode(node);
134 return paintedByBlock ? toRenderBlock(renderer) : renderer->containingBlock();
137 bool CaretBase::updateCaretRect(Document* document, const PositionWithAffinity& caretPosition)
139 m_caretLocalRect = LayoutRect();
141 m_caretRectNeedsUpdate = false;
143 if (caretPosition.position().isNull())
146 ASSERT(caretPosition.position().deprecatedNode()->renderer());
148 // First compute a rect local to the renderer at the selection start.
149 RenderObject* renderer;
150 LayoutRect localRect = localCaretRectOfPosition(caretPosition, renderer);
152 // Get the renderer that will be responsible for painting the caret
153 // (which is either the renderer we just found, or one of its containers).
154 RenderBlock* caretPainter = caretRenderer(caretPosition.position().deprecatedNode());
156 // Compute an offset between the renderer and the caretPainter.
157 bool unrooted = false;
158 while (renderer != caretPainter) {
159 RenderObject* containerObject = renderer->container();
160 if (!containerObject) {
164 localRect.move(renderer->offsetFromContainer(containerObject, localRect.location()));
165 renderer = containerObject;
169 m_caretLocalRect = localRect;
174 bool CaretBase::updateCaretRect(Document* document, const VisiblePosition& caretPosition)
176 return updateCaretRect(document, PositionWithAffinity(caretPosition.deepEquivalent(), caretPosition.affinity()));
179 RenderBlock* DragCaretController::caretRenderer() const
181 return CaretBase::caretRenderer(m_position.deepEquivalent().deprecatedNode());
184 IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, const LayoutRect& rect) const
186 RenderBlock* caretPainter = caretRenderer(node);
190 LayoutRect localRect(rect);
191 caretPainter->flipForWritingMode(localRect);
192 return caretPainter->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox();
195 void CaretBase::repaintCaretForLocalRect(Node* node, const LayoutRect& rect)
197 RenderBlock* caretPainter = caretRenderer(node);
201 // FIXME: Need to over-paint 1 pixel to workaround some rounding problems.
202 // https://bugs.webkit.org/show_bug.cgi?id=108283
203 LayoutRect inflatedRect = rect;
204 inflatedRect.inflate(1);
206 caretPainter->invalidatePaintRectangle(inflatedRect);
209 bool CaretBase::shouldRepaintCaret(const RenderView* view, bool isContentEditable) const
212 bool caretBrowsing = false;
213 if (FrameView* frameView = view->frameView()) {
214 LocalFrame& frame = frameView->frame(); // The frame where the selection started
215 caretBrowsing = frame.settings() && frame.settings()->caretBrowsingEnabled();
217 return (caretBrowsing || isContentEditable);
220 void CaretBase::invalidateCaretRect(Node* node, bool caretRectChanged)
222 // EDIT FIXME: This is an unfortunate hack.
223 // Basically, we can't trust this layout position since we
224 // can't guarantee that the check to see if we are in unrendered
225 // content will work at this point. We may have to wait for
226 // a layout and re-render of the document to happen. So, resetting this
227 // flag will cause another caret layout to happen the first time
228 // that we try to paint the caret after this call. That one will work since
229 // it happens after the document has accounted for any editing
230 // changes which may have been done.
231 // And, we need to leave this layout here so the caret moves right
232 // away after clicking.
233 m_caretRectNeedsUpdate = true;
235 if (caretRectChanged)
238 if (RenderView* view = node->document().renderView()) {
239 if (shouldRepaintCaret(view, node->isContentEditable(Node::UserSelectAllIsAlwaysNonEditable)))
240 repaintCaretForLocalRect(node, localCaretRectWithoutUpdate());
244 void CaretBase::paintCaret(Node* node, GraphicsContext* context, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const
246 if (m_caretVisibility == Hidden)
249 LayoutRect drawingRect = localCaretRectWithoutUpdate();
250 if (RenderBlock* renderer = caretRenderer(node))
251 renderer->flipForWritingMode(drawingRect);
252 drawingRect.moveBy(roundedIntPoint(paintOffset));
253 LayoutRect caret = intersection(drawingRect, clipRect);
257 Color caretColor = Color::black;
260 if (node->isElementNode())
261 element = toElement(node);
263 element = node->parentElement();
265 if (element && element->renderer())
266 caretColor = element->renderer()->resolveColor(CSSPropertyColor);
268 context->fillRect(caret, caretColor);
271 void DragCaretController::paintDragCaret(LocalFrame* frame, GraphicsContext* p, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const
273 if (m_position.deepEquivalent().deprecatedNode()->document().frame() == frame)
274 paintCaret(m_position.deepEquivalent().deprecatedNode(), p, paintOffset, clipRect);