]>
Commit | Line | Data |
---|---|---|
a90939db JF |
1 | /* |
2 | * Copyright (C) 2004 Apple Computer, 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 | #ifndef SelectionController_h | |
27 | #define SelectionController_h | |
28 | ||
29 | #include "IntRect.h" | |
30 | #include "Selection.h" | |
31 | #include "Range.h" | |
32 | #include <wtf/Noncopyable.h> | |
33 | ||
34 | namespace WebCore { | |
35 | ||
36 | class Frame; | |
37 | class GraphicsContext; | |
38 | class RenderObject; | |
39 | class VisiblePosition; | |
40 | ||
41 | class SelectionController : Noncopyable { | |
42 | public: | |
43 | enum EAlteration { MOVE, EXTEND }; | |
44 | enum EDirection { FORWARD, BACKWARD, RIGHT, LEFT }; | |
45 | ||
46 | SelectionController(Frame* = 0, bool isDragCaretController = false); | |
47 | ||
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(); } | |
52 | ||
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); | |
58 | ||
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); | |
62 | void selectAll(); | |
63 | void clear(); | |
64 | ||
65 | // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected. | |
66 | void selectFrameElementInParentIfFullySelected(); | |
67 | ||
68 | bool contains(const IntPoint&); | |
69 | ||
70 | Selection::EState state() const { return m_sel.state(); } | |
71 | ||
72 | EAffinity affinity() const { return m_sel.affinity(); } | |
73 | ||
74 | bool modify(EAlteration, EDirection, TextGranularity, bool userTriggered = false); | |
75 | bool modify(EAlteration, int verticalDistance, bool userTriggered = false); | |
76 | bool expandUsingGranularity(TextGranularity); | |
77 | ||
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); | |
82 | ||
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(); } | |
87 | ||
88 | // Return the renderer that is responsible for painting the caret (in the selection start node) | |
89 | RenderObject* caretRenderer() const; | |
90 | ||
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); | |
96 | ||
97 | void setLastChangeWasHorizontalExtension(bool b) { m_lastChangeWasHorizontalExtension = b; } | |
98 | void willBeModified(EAlteration, EDirection); | |
99 | ||
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; | |
105 | ||
106 | PassRefPtr<Range> toRange() const { return m_sel.toRange(); } | |
107 | ||
108 | void debugRenderer(RenderObject*, bool selected) const; | |
109 | ||
110 | void nodeWillBeRemoved(Node*); | |
111 | ||
112 | bool recomputeCaretRect(); // returns true if caret rect moved | |
113 | void invalidateCaretRect(); | |
114 | void paintCaret(GraphicsContext*, int tx, int ty, const IntRect& clipRect); | |
115 | ||
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; } | |
119 | ||
120 | // Focus | |
121 | void setFocused(bool); | |
122 | bool isFocusedAndActive() const; | |
123 | void pageActivationChanged(); | |
124 | ||
125 | #ifndef NDEBUG | |
126 | void formatForDebugger(char* buffer, unsigned length) const; | |
127 | void showTreeForThis() const; | |
128 | #endif | |
129 | ||
130 | public: | |
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; } | |
151 | private: | |
152 | static Selection wordSelectionContainingCaretSelection(const Selection&); | |
153 | bool _selectionAtSentenceStart(const Selection& sel) const; | |
154 | PassRefPtr<Range> _rangeByAlteringCurrentSelection(EAlteration alteration, int amount) const; | |
155 | ||
156 | private: | |
157 | enum EPositionType { START, END, BASE, EXTENT }; | |
158 | ||
159 | VisiblePosition modifyExtendingRightForward(TextGranularity); | |
160 | VisiblePosition modifyMovingRight(TextGranularity); | |
161 | VisiblePosition modifyMovingForward(TextGranularity); | |
162 | VisiblePosition modifyExtendingLeftBackward(TextGranularity); | |
163 | VisiblePosition modifyMovingLeft(TextGranularity); | |
164 | VisiblePosition modifyMovingBackward(TextGranularity); | |
165 | ||
166 | void layout(); | |
167 | IntRect caretRepaintRect() const; | |
168 | ||
169 | int xPosForVerticalArrowNavigation(EPositionType); | |
170 | ||
171 | #if PLATFORM(MAC) | |
172 | void notifyAccessibilityForSelectionChange(); | |
173 | #else | |
174 | void notifyAccessibilityForSelectionChange() {}; | |
175 | #endif | |
176 | ||
177 | void focusedOrActiveStateChanged(); | |
178 | bool caretRendersInsideNode(Node*) const; | |
179 | ||
180 | IntRect absoluteBoundsForLocalRect(const IntRect&) const; | |
181 | ||
182 | Frame* m_frame; | |
183 | int m_xPosForVerticalArrowNavigation; | |
184 | ||
185 | Selection m_sel; | |
186 | ||
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; | |
190 | ||
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; | |
196 | bool m_focused : 1; | |
197 | ||
198 | int m_closeTypingSuppressions; | |
199 | }; | |
200 | ||
201 | inline bool operator==(const SelectionController& a, const SelectionController& b) | |
202 | { | |
203 | return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity(); | |
204 | } | |
205 | ||
206 | inline bool operator!=(const SelectionController& a, const SelectionController& b) | |
207 | { | |
208 | return !(a == b); | |
209 | } | |
210 | ||
211 | } // namespace WebCore | |
212 | ||
213 | #ifndef NDEBUG | |
214 | // Outside the WebCore namespace for ease of invocation from gdb. | |
215 | void showTree(const WebCore::SelectionController&); | |
216 | void showTree(const WebCore::SelectionController*); | |
217 | #endif | |
218 | ||
219 | #endif // SelectionController_h |