Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / Caret.cpp
1 /*
2  * Copyright (C) 2004, 2008, 2009, 2010 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 "core/editing/Caret.h"
28
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"
38
39 namespace blink {
40
41 CaretBase::CaretBase(CaretVisibility visibility)
42     : m_caretRectNeedsUpdate(true)
43     , m_caretVisibility(visibility)
44 {
45 }
46
47 DragCaretController::DragCaretController()
48     : CaretBase(Visible)
49 {
50 }
51
52 PassOwnPtrWillBeRawPtr<DragCaretController> DragCaretController::create()
53 {
54     return adoptPtrWillBeNoop(new DragCaretController);
55 }
56
57 bool DragCaretController::isContentRichlyEditable() const
58 {
59     return isRichlyEditablePosition(m_position.deepEquivalent());
60 }
61
62 void DragCaretController::setCaretPosition(const VisiblePosition& position)
63 {
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();
72     }
73     if (m_position.isNull() || m_position.isOrphan()) {
74         clearCaretRect();
75     } else {
76         document->updateRenderTreeIfNeeded();
77         updateCaretRect(document, m_position);
78     }
79 }
80
81 static bool removingNodeRemovesPosition(Node& node, const Position& position)
82 {
83     if (!position.anchorNode())
84         return false;
85
86     if (position.anchorNode() == node)
87         return true;
88
89     if (!node.isElementNode())
90         return false;
91
92     Element& element = toElement(node);
93     return element.containsIncludingShadowDOM(position.anchorNode());
94 }
95
96 void DragCaretController::nodeWillBeRemoved(Node& node)
97 {
98     if (!hasCaret() || !node.inActiveDocument())
99         return;
100
101     if (!removingNodeRemovesPosition(node, m_position.deepEquivalent()))
102         return;
103
104     m_position.deepEquivalent().document()->renderView()->clearSelection();
105     clear();
106 }
107
108 void DragCaretController::trace(Visitor* visitor)
109 {
110     visitor->trace(m_position);
111 }
112
113 void CaretBase::clearCaretRect()
114 {
115     m_caretLocalRect = LayoutRect();
116 }
117
118 static inline bool caretRendersInsideNode(Node* node)
119 {
120     return node && !isRenderedTableElement(node) && !editingIgnoresContent(node);
121 }
122
123 RenderBlock* CaretBase::caretRenderer(Node* node)
124 {
125     if (!node)
126         return 0;
127
128     RenderObject* renderer = node->renderer();
129     if (!renderer)
130         return 0;
131
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();
135 }
136
137 static void mapCaretRectToCaretPainter(RenderObject* caretRenderer, RenderBlock* caretPainter, LayoutRect& caretRect)
138 {
139     // FIXME: This shouldn't be called on un-rooted subtrees.
140     // FIXME: This should probably just use mapLocalToContainer.
141     // Compute an offset between the caretRenderer and the caretPainter.
142
143     ASSERT(caretRenderer->isDescendantOf(caretPainter));
144
145     bool unrooted = false;
146     while (caretRenderer != caretPainter) {
147         RenderObject* containerObject = caretRenderer->container();
148         if (!containerObject) {
149             unrooted = true;
150             break;
151         }
152         caretRect.move(caretRenderer->offsetFromContainer(containerObject, caretRect.location()));
153         caretRenderer = containerObject;
154     }
155
156     if (unrooted)
157         caretRect = LayoutRect();
158 }
159
160 bool CaretBase::updateCaretRect(Document* document, const PositionWithAffinity& caretPosition)
161 {
162     m_caretLocalRect = LayoutRect();
163
164     m_caretRectNeedsUpdate = false;
165
166     if (caretPosition.position().isNull())
167         return false;
168
169     ASSERT(caretPosition.position().deprecatedNode()->renderer());
170
171     // First compute a rect local to the renderer at the selection start.
172     RenderObject* renderer;
173     m_caretLocalRect = localCaretRectOfPosition(caretPosition, renderer);
174
175     // Get the renderer that will be responsible for painting the caret
176     // (which is either the renderer we just found, or one of its containers).
177     RenderBlock* caretPainter = caretRenderer(caretPosition.position().deprecatedNode());
178
179     mapCaretRectToCaretPainter(renderer, caretPainter, m_caretLocalRect);
180
181     return true;
182 }
183
184 bool CaretBase::updateCaretRect(Document* document, const VisiblePosition& caretPosition)
185 {
186     return updateCaretRect(document, PositionWithAffinity(caretPosition.deepEquivalent(), caretPosition.affinity()));
187 }
188
189 RenderBlock* DragCaretController::caretRenderer() const
190 {
191     return CaretBase::caretRenderer(m_position.deepEquivalent().deprecatedNode());
192 }
193
194 IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, const LayoutRect& rect) const
195 {
196     RenderBlock* caretPainter = caretRenderer(node);
197     if (!caretPainter)
198         return IntRect();
199
200     LayoutRect localRect(rect);
201     caretPainter->flipForWritingMode(localRect);
202     return caretPainter->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox();
203 }
204
205 void CaretBase::invalidateLocalCaretRect(Node* node, const LayoutRect& rect)
206 {
207     RenderBlock* caretPainter = caretRenderer(node);
208     if (!caretPainter)
209         return;
210
211     // FIXME: Need to over-paint 1 pixel to workaround some rounding problems.
212     // https://bugs.webkit.org/show_bug.cgi?id=108283
213     LayoutRect inflatedRect = rect;
214     inflatedRect.inflate(1);
215
216     // FIXME: We should use mapLocalToContainer() since we know we're not un-rooted.
217     mapCaretRectToCaretPainter(node->renderer(), caretPainter, inflatedRect);
218
219     caretPainter->invalidatePaintRectangle(inflatedRect);
220 }
221
222 bool CaretBase::shouldRepaintCaret(const RenderView* view, bool isContentEditable) const
223 {
224     ASSERT(view);
225     bool caretBrowsing = false;
226     if (FrameView* frameView = view->frameView()) {
227         LocalFrame& frame = frameView->frame(); // The frame where the selection started
228         caretBrowsing = frame.settings() && frame.settings()->caretBrowsingEnabled();
229     }
230     return (caretBrowsing || isContentEditable);
231 }
232
233 void CaretBase::invalidateCaretRect(Node* node, bool caretRectChanged)
234 {
235     // EDIT FIXME: This is an unfortunate hack.
236     // Basically, we can't trust this layout position since we
237     // can't guarantee that the check to see if we are in unrendered
238     // content will work at this point. We may have to wait for
239     // a layout and re-render of the document to happen. So, resetting this
240     // flag will cause another caret layout to happen the first time
241     // that we try to paint the caret after this call. That one will work since
242     // it happens after the document has accounted for any editing
243     // changes which may have been done.
244     // And, we need to leave this layout here so the caret moves right
245     // away after clicking.
246     m_caretRectNeedsUpdate = true;
247
248     if (caretRectChanged)
249         return;
250
251     if (RenderView* view = node->document().renderView()) {
252         if (shouldRepaintCaret(view, node->isContentEditable(Node::UserSelectAllIsAlwaysNonEditable)))
253             invalidateLocalCaretRect(node, localCaretRectWithoutUpdate());
254     }
255 }
256
257 void CaretBase::paintCaret(Node* node, GraphicsContext* context, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const
258 {
259     if (m_caretVisibility == Hidden)
260         return;
261
262     LayoutRect drawingRect = localCaretRectWithoutUpdate();
263     if (RenderBlock* renderer = caretRenderer(node))
264         renderer->flipForWritingMode(drawingRect);
265     drawingRect.moveBy(roundedIntPoint(paintOffset));
266     LayoutRect caret = intersection(drawingRect, clipRect);
267     if (caret.isEmpty())
268         return;
269
270     Color caretColor = Color::black;
271
272     Element* element;
273     if (node->isElementNode())
274         element = toElement(node);
275     else
276         element = node->parentElement();
277
278     if (element && element->renderer())
279         caretColor = element->renderer()->resolveColor(CSSPropertyColor);
280
281     context->fillRect(caret, caretColor);
282 }
283
284 void DragCaretController::paintDragCaret(LocalFrame* frame, GraphicsContext* p, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const
285 {
286     if (m_position.deepEquivalent().deprecatedNode()->document().frame() == frame)
287         paintCaret(m_position.deepEquivalent().deprecatedNode(), p, paintOffset, clipRect);
288 }
289
290 }