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/DragCaretController.h"
29 #include "core/editing/EditingUtilities.h"
30 #include "core/layout/LayoutView.h"
31 #include "core/paint/DeprecatedPaintLayer.h"
35 DragCaretController::DragCaretController()
40 PassOwnPtrWillBeRawPtr
<DragCaretController
> DragCaretController::create()
42 return adoptPtrWillBeNoop(new DragCaretController
);
45 bool DragCaretController::isContentRichlyEditable() const
47 return isRichlyEditablePosition(m_position
.deepEquivalent());
50 void DragCaretController::setCaretPosition(const PositionWithAffinity
& position
)
52 // for querying Layer::compositingState()
53 // This code is probably correct, since it doesn't occur in a stack that
54 // involves updating compositing state.
55 DisableCompositingQueryAsserts disabler
;
57 if (Node
* node
= m_position
.deepEquivalent().anchorNode())
58 invalidateCaretRect(node
);
59 m_position
= createVisiblePosition(position
);
60 Document
* document
= nullptr;
61 if (Node
* node
= m_position
.deepEquivalent().anchorNode()) {
62 invalidateCaretRect(node
);
63 document
= &node
->document();
65 if (m_position
.isNull() || m_position
.isOrphan()) {
68 document
->updateLayoutTreeIfNeeded();
69 updateCaretRect(m_position
);
73 static bool removingNodeRemovesPosition(Node
& node
, const Position
& position
)
75 if (!position
.anchorNode())
78 if (position
.anchorNode() == node
)
81 if (!node
.isElementNode())
84 Element
& element
= toElement(node
);
85 return element
.containsIncludingShadowDOM(position
.anchorNode());
88 void DragCaretController::nodeWillBeRemoved(Node
& node
)
90 if (!hasCaret() || !node
.inActiveDocument())
93 if (!removingNodeRemovesPosition(node
, m_position
.deepEquivalent()))
96 m_position
.deepEquivalent().document()->layoutView()->clearSelection();
100 DEFINE_TRACE(DragCaretController
)
102 visitor
->trace(m_position
);
105 LayoutBlock
* DragCaretController::caretLayoutObject() const
107 return CaretBase::caretLayoutObject(m_position
.deepEquivalent().anchorNode());
110 bool DragCaretController::isContentEditable() const
112 return rootEditableElementOf(m_position
);
115 void DragCaretController::paintDragCaret(LocalFrame
* frame
, GraphicsContext
* p
, const LayoutPoint
& paintOffset
, const LayoutRect
& clipRect
) const
117 if (m_position
.deepEquivalent().anchorNode()->document().frame() == frame
)
118 paintCaret(m_position
.deepEquivalent().anchorNode(), p
, paintOffset
, clipRect
);