2 * Copyright (C) 2004 Apple Computer, 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.
26 #ifndef SelectionController_h
27 #define SelectionController_h
30 #include "Selection.h"
32 #include <wtf/Noncopyable.h>
37 class GraphicsContext
;
39 class VisiblePosition
;
41 class SelectionController
: Noncopyable
{
43 enum EAlteration
{ MOVE
, EXTEND
};
44 enum EDirection
{ FORWARD
, BACKWARD
, RIGHT
, LEFT
};
46 SelectionController(Frame
* = 0, bool isDragCaretController
= false);
48 Element
* rootEditableElement() const { return m_sel
.rootEditableElement(); }
49 bool isContentEditable() const { return m_sel
.isContentEditable(); }
50 bool isContentRichlyEditable() const { return m_sel
.isContentRichlyEditable(); }
51 Node
* shadowTreeRootNode() const { return m_sel
.shadowTreeRootNode(); }
53 void moveTo(const Range
*, EAffinity
, bool userTriggered
= false);
54 void moveTo(const VisiblePosition
&, bool userTriggered
= false);
55 void moveTo(const VisiblePosition
&, const VisiblePosition
&, bool userTriggered
= false);
56 void moveTo(const Position
&, EAffinity
, bool userTriggered
= false);
57 void moveTo(const Position
&, const Position
&, EAffinity
, bool userTriggered
= false);
59 const Selection
& selection() const { return m_sel
; }
60 void setSelection(const Selection
&, bool closeTyping
= true, bool clearTypingStyle
= true, bool userTriggered
= false);
61 bool setSelectedRange(Range
*, EAffinity
, bool closeTyping
);
65 // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
66 void selectFrameElementInParentIfFullySelected();
68 bool contains(const IntPoint
&);
70 Selection::EState
state() const { return m_sel
.state(); }
72 EAffinity
affinity() const { return m_sel
.affinity(); }
74 bool modify(EAlteration
, EDirection
, TextGranularity
, bool userTriggered
= false);
75 bool modify(EAlteration
, int verticalDistance
, bool userTriggered
= false);
76 bool expandUsingGranularity(TextGranularity
);
78 void setBase(const VisiblePosition
&, bool userTriggered
= false);
79 void setBase(const Position
&, EAffinity
, bool userTriggered
= false);
80 void setExtent(const VisiblePosition
&, bool userTriggered
= false);
81 void setExtent(const Position
&, EAffinity
, bool userTriggered
= false);
83 Position
base() const { return m_sel
.base(); }
84 Position
extent() const { return m_sel
.extent(); }
85 Position
start() const { return m_sel
.start(); }
86 Position
end() const { return m_sel
.end(); }
88 // Return the renderer that is responsible for painting the caret (in the selection start node)
89 RenderObject
* caretRenderer() const;
91 // Caret rect local to the caret's renderer
92 IntRect
localCaretRect() const;
93 // Bounds of (possibly transformed) caret in absolute coords
94 IntRect
absoluteCaretBounds();
95 void setNeedsLayout(bool flag
= true);
97 void setLastChangeWasHorizontalExtension(bool b
) { m_lastChangeWasHorizontalExtension
= b
; }
98 void willBeModified(EAlteration
, EDirection
);
100 bool isNone() const { return m_sel
.isNone(); }
101 bool isCaret() const { return m_sel
.isCaret(); }
102 bool isRange() const { return m_sel
.isRange(); }
103 bool isCaretOrRange() const { return m_sel
.isCaretOrRange(); }
104 bool isInPasswordField() const;
106 PassRefPtr
<Range
> toRange() const { return m_sel
.toRange(); }
108 void debugRenderer(RenderObject
*, bool selected
) const;
110 void nodeWillBeRemoved(Node
*);
112 bool recomputeCaretRect(); // returns true if caret rect moved
113 void invalidateCaretRect();
114 void paintCaret(GraphicsContext
*, int tx
, int ty
, const IntRect
& clipRect
);
116 // Used to suspend caret blinking while the mouse is down.
117 void setCaretBlinkingSuspended(bool suspended
) { m_isCaretBlinkingSuspended
= suspended
; }
118 bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended
; }
121 void setFocused(bool);
122 bool isFocusedAndActive() const;
123 void pageActivationChanged();
126 void formatForDebugger(char* buffer
, unsigned length
) const;
127 void showTreeForThis() const;
131 void expandSelectionToElementContainingCaretSelection();
132 PassRefPtr
<Range
> elementRangeContainingCaretSelection() const;
133 void expandSelectionToWordContainingCaretSelection();
134 PassRefPtr
<Range
> wordRangeContainingCaretSelection();
135 void expandSelectionToStartOfWordContainingCaretSelection();
136 UChar
characterInRelationToCaretSelection(int amount
) const;
137 UChar
characterBeforeCaretSelection() const;
138 UChar
characterAfterCaretSelection() const;
139 int wordOffsetInRange(const Range
*range
) const;
140 bool spaceFollowsWordInRange(const Range
* range
) const;
141 bool selectionAtDocumentStart() const;
142 bool selectionAtSentenceStart() const;
143 bool selectionAtWordStart() const;
144 bool rangeAtSentenceStart(const Range
*range
) const;
145 PassRefPtr
<Range
> rangeByMovingCurrentSelection(int amount
) const;
146 PassRefPtr
<Range
> rangeByExtendingCurrentSelection(int amount
) const;
147 void selectRangeOnElement(unsigned int location
, unsigned int length
, Node
* node
);
148 bool selectionIsCaretInDisplayBlockElementAtOffset(int offset
) const;
149 void suppressCloseTyping() { ++m_closeTypingSuppressions
; }
150 void restoreCloseTyping() { --m_closeTypingSuppressions
; }
152 static Selection
wordSelectionContainingCaretSelection(const Selection
&);
153 bool _selectionAtSentenceStart(const Selection
& sel
) const;
154 PassRefPtr
<Range
> _rangeByAlteringCurrentSelection(EAlteration alteration
, int amount
) const;
157 enum EPositionType
{ START
, END
, BASE
, EXTENT
};
159 VisiblePosition
modifyExtendingRightForward(TextGranularity
);
160 VisiblePosition
modifyMovingRight(TextGranularity
);
161 VisiblePosition
modifyMovingForward(TextGranularity
);
162 VisiblePosition
modifyExtendingLeftBackward(TextGranularity
);
163 VisiblePosition
modifyMovingLeft(TextGranularity
);
164 VisiblePosition
modifyMovingBackward(TextGranularity
);
167 IntRect
caretRepaintRect() const;
169 int xPosForVerticalArrowNavigation(EPositionType
);
172 void notifyAccessibilityForSelectionChange();
174 void notifyAccessibilityForSelectionChange() {};
177 void focusedOrActiveStateChanged();
178 bool caretRendersInsideNode(Node
*) const;
180 IntRect
absoluteBoundsForLocalRect(const IntRect
&) const;
183 int m_xPosForVerticalArrowNavigation
;
187 IntRect m_caretRect
; // caret rect in coords local to the renderer responsible for painting the caret
188 IntRect m_absCaretBounds
; // absolute bounding rect for the caret
189 IntRect m_absoluteCaretRepaintBounds
;
191 bool m_needsLayout
: 1; // true if the caret and expectedVisible rectangles need to be calculated
192 bool m_absCaretBoundsDirty
: 1;
193 bool m_lastChangeWasHorizontalExtension
: 1;
194 bool m_isDragCaretController
: 1;
195 bool m_isCaretBlinkingSuspended
: 1;
198 int m_closeTypingSuppressions
;
201 inline bool operator==(const SelectionController
& a
, const SelectionController
& b
)
203 return a
.start() == b
.start() && a
.end() == b
.end() && a
.affinity() == b
.affinity();
206 inline bool operator!=(const SelectionController
& a
, const SelectionController
& b
)
211 } // namespace WebCore
214 // Outside the WebCore namespace for ease of invocation from gdb.
215 void showTree(const WebCore::SelectionController
&);
216 void showTree(const WebCore::SelectionController
*);
219 #endif // SelectionController_h