OLD | NEW |
| (Empty) |
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/editing/EditingUtilities.h" | |
30 #include "core/editing/VisibleUnits.h" | |
31 #include "core/frame/Settings.h" | |
32 #include "core/layout/LayoutBlock.h" | |
33 #include "core/layout/LayoutView.h" | |
34 #include "platform/graphics/GraphicsContext.h" | |
35 | |
36 namespace blink { | |
37 | |
38 CaretBase::CaretBase(CaretVisibility visibility) | |
39 : m_caretVisibility(visibility) | |
40 { | |
41 } | |
42 | |
43 void CaretBase::clearCaretRect() | |
44 { | |
45 m_caretLocalRect = LayoutRect(); | |
46 } | |
47 | |
48 static inline bool caretRendersInsideNode(Node* node) | |
49 { | |
50 return node && !isRenderedTableElement(node) && !editingIgnoresContent(node)
; | |
51 } | |
52 | |
53 LayoutBlock* CaretBase::caretLayoutObject(Node* node) | |
54 { | |
55 if (!node) | |
56 return 0; | |
57 | |
58 LayoutObject* layoutObject = node->layoutObject(); | |
59 if (!layoutObject) | |
60 return 0; | |
61 | |
62 // if caretNode is a block and caret is inside it then caret should be paint
ed by that block | |
63 bool paintedByBlock = layoutObject->isLayoutBlock() && caretRendersInsideNod
e(node); | |
64 return paintedByBlock ? toLayoutBlock(layoutObject) : layoutObject->containi
ngBlock(); | |
65 } | |
66 | |
67 static void mapCaretRectToCaretPainter(LayoutObject* caretLayoutObject, LayoutBl
ock* caretPainter, LayoutRect& caretRect) | |
68 { | |
69 // FIXME: This shouldn't be called on un-rooted subtrees. | |
70 // FIXME: This should probably just use mapLocalToContainer. | |
71 // Compute an offset between the caretLayoutObject and the caretPainter. | |
72 | |
73 ASSERT(caretLayoutObject->isDescendantOf(caretPainter)); | |
74 | |
75 bool unrooted = false; | |
76 while (caretLayoutObject != caretPainter) { | |
77 LayoutObject* containerObject = caretLayoutObject->container(); | |
78 if (!containerObject) { | |
79 unrooted = true; | |
80 break; | |
81 } | |
82 caretRect.move(caretLayoutObject->offsetFromContainer(containerObject, c
aretRect.location())); | |
83 caretLayoutObject = containerObject; | |
84 } | |
85 | |
86 if (unrooted) | |
87 caretRect = LayoutRect(); | |
88 } | |
89 | |
90 bool CaretBase::updateCaretRect(const PositionWithAffinity& caretPosition) | |
91 { | |
92 m_caretLocalRect = LayoutRect(); | |
93 | |
94 if (caretPosition.position().isNull()) | |
95 return false; | |
96 | |
97 ASSERT(caretPosition.position().anchorNode()->layoutObject()); | |
98 | |
99 // First compute a rect local to the layoutObject at the selection start. | |
100 LayoutObject* layoutObject; | |
101 m_caretLocalRect = localCaretRectOfPosition(caretPosition, layoutObject); | |
102 | |
103 // Get the layoutObject that will be responsible for painting the caret | |
104 // (which is either the layoutObject we just found, or one of its containers
). | |
105 LayoutBlock* caretPainter = caretLayoutObject(caretPosition.position().ancho
rNode()); | |
106 | |
107 mapCaretRectToCaretPainter(layoutObject, caretPainter, m_caretLocalRect); | |
108 | |
109 return true; | |
110 } | |
111 | |
112 bool CaretBase::updateCaretRect(const VisiblePosition& caretPosition) | |
113 { | |
114 return updateCaretRect(caretPosition.toPositionWithAffinity()); | |
115 } | |
116 | |
117 IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, const LayoutRect& rect
) const | |
118 { | |
119 LayoutBlock* caretPainter = caretLayoutObject(node); | |
120 if (!caretPainter) | |
121 return IntRect(); | |
122 | |
123 LayoutRect localRect(rect); | |
124 caretPainter->flipForWritingMode(localRect); | |
125 return caretPainter->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoun
dingBox(); | |
126 } | |
127 | |
128 void CaretBase::invalidateLocalCaretRect(Node* node, const LayoutRect& rect) | |
129 { | |
130 LayoutBlock* caretPainter = caretLayoutObject(node); | |
131 if (!caretPainter) | |
132 return; | |
133 | |
134 // FIXME: Need to over-paint 1 pixel to workaround some rounding problems. | |
135 // https://bugs.webkit.org/show_bug.cgi?id=108283 | |
136 LayoutRect inflatedRect = rect; | |
137 inflatedRect.inflate(1); | |
138 | |
139 // FIXME: We should use mapLocalToContainer() since we know we're not un-roo
ted. | |
140 mapCaretRectToCaretPainter(node->layoutObject(), caretPainter, inflatedRect)
; | |
141 | |
142 // FIXME: We should not allow paint invalidation out of paint invalidation s
tate. crbug.com/457415 | |
143 DisablePaintInvalidationStateAsserts disabler; | |
144 caretPainter->invalidatePaintRectangle(inflatedRect); | |
145 } | |
146 | |
147 bool CaretBase::shouldRepaintCaret(Node& node) const | |
148 { | |
149 // If PositionAnchorType::BeforeAnchor or PositionAnchorType::AfterAnchor, | |
150 // carets need to be repainted not only when the node is contentEditable but | |
151 // also when its parentNode() is contentEditable. | |
152 return node.isContentEditable() || (node.parentNode() && node.parentNode()->
isContentEditable()); | |
153 } | |
154 | |
155 bool CaretBase::shouldRepaintCaret(const LayoutView* view) const | |
156 { | |
157 ASSERT(view); | |
158 if (FrameView* frameView = view->frameView()) { | |
159 LocalFrame& frame = frameView->frame(); // The frame where the selection
started | |
160 return frame.settings() && frame.settings()->caretBrowsingEnabled(); | |
161 } | |
162 return false; | |
163 } | |
164 | |
165 void CaretBase::invalidateCaretRect(Node* node, bool caretRectChanged) | |
166 { | |
167 if (caretRectChanged) | |
168 return; | |
169 | |
170 if (LayoutView* view = node->document().layoutView()) { | |
171 if (node->isContentEditable(Node::UserSelectAllIsAlwaysNonEditable) || s
houldRepaintCaret(view)) | |
172 invalidateLocalCaretRect(node, localCaretRectWithoutUpdate()); | |
173 } | |
174 } | |
175 | |
176 void CaretBase::paintCaret(Node* node, GraphicsContext* context, const LayoutPoi
nt& paintOffset, const LayoutRect& clipRect) const | |
177 { | |
178 if (m_caretVisibility == Hidden) | |
179 return; | |
180 | |
181 LayoutRect drawingRect = localCaretRectWithoutUpdate(); | |
182 if (LayoutBlock* layoutObject = caretLayoutObject(node)) | |
183 layoutObject->flipForWritingMode(drawingRect); | |
184 drawingRect.moveBy(roundedIntPoint(paintOffset)); | |
185 LayoutRect caret = intersection(drawingRect, clipRect); | |
186 if (caret.isEmpty()) | |
187 return; | |
188 | |
189 Color caretColor = Color::black; | |
190 | |
191 Element* element; | |
192 if (node->isElementNode()) | |
193 element = toElement(node); | |
194 else | |
195 element = node->parentElement(); | |
196 | |
197 if (element && element->layoutObject()) | |
198 caretColor = element->layoutObject()->resolveColor(CSSPropertyColor); | |
199 | |
200 context->fillRect(FloatRect(caret), caretColor); | |
201 } | |
202 | |
203 } // namespace blink | |
OLD | NEW |