2 * Copyright (C) 2004, 2005, 2006, 2007, 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.
26 #ifndef FrameSelection_h
27 #define FrameSelection_h
29 #include "EditingStyle.h"
31 #include "LayoutTypes.h"
33 #include "ScrollBehavior.h"
35 #include "VisibleSelection.h"
36 #include <wtf/Noncopyable.h>
41 class CSSMutableStyleDeclaration;
43 class GraphicsContext;
44 class HTMLFormElement;
48 class VisiblePosition;
50 enum EUserTriggered { NotUserTriggered = 0, UserTriggered = 1 };
53 WTF_MAKE_NONCOPYABLE(CaretBase);
54 WTF_MAKE_FAST_ALLOCATED;
56 enum CaretVisibility { Visible, Hidden };
57 explicit CaretBase(CaretVisibility = Hidden);
59 void invalidateCaretRect(Node*, bool caretRectChanged = false);
60 void clearCaretRect();
61 bool updateCaretRect(Document*, const VisiblePosition& caretPosition);
62 LayoutRect absoluteBoundsForLocalRect(Node*, const LayoutRect&) const;
63 LayoutRect caretRepaintRect(Node*) const;
64 bool shouldRepaintCaret(const RenderView*, bool isContentEditable) const;
65 void paintCaret(Node*, GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
66 RenderObject* caretRenderer(Node*) const;
68 const LayoutRect& localCaretRectWithoutUpdate() const { return m_caretLocalRect; }
70 bool shouldUpdateCaretRect() const { return m_caretRectNeedsUpdate; }
71 void setCaretRectNeedsUpdate() { m_caretRectNeedsUpdate = true; }
73 void setCaretVisibility(CaretVisibility visibility) { m_caretVisibility = visibility; }
74 bool caretIsVisible() const { return m_caretVisibility == Visible; }
75 CaretVisibility caretVisibility() const { return m_caretVisibility; }
78 LayoutRect m_caretLocalRect; // caret rect in coords local to the renderer responsible for painting the caret
79 bool m_caretRectNeedsUpdate; // true if m_caretRect (and m_absCaretBounds in FrameSelection) need to be calculated
80 CaretVisibility m_caretVisibility;
83 class DragCaretController : private CaretBase {
84 WTF_MAKE_NONCOPYABLE(DragCaretController);
85 WTF_MAKE_FAST_ALLOCATED;
87 DragCaretController();
89 RenderObject* caretRenderer() const;
90 void paintDragCaret(Frame*, GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
92 bool isContentEditable() const { return m_position.rootEditableElement(); }
93 bool isContentRichlyEditable() const;
95 bool hasCaret() const { return m_position.isNotNull(); }
96 const VisiblePosition& caretPosition() { return m_position; }
97 void setCaretPosition(const VisiblePosition&);
98 void clear() { setCaretPosition(VisiblePosition()); }
100 void nodeWillBeRemoved(Node*);
103 VisiblePosition m_position;
106 class FrameSelection : private CaretBase {
107 WTF_MAKE_NONCOPYABLE(FrameSelection);
108 WTF_MAKE_FAST_ALLOCATED;
110 enum EAlteration { AlterationMove, AlterationExtend };
111 enum CursorAlignOnScroll { AlignCursorOnScrollIfNeeded,
112 AlignCursorOnScrollAlways };
113 enum SetSelectionOption {
114 // 1 << 0 is reserved for EUserTriggered
115 CloseTyping = 1 << 1,
116 ClearTypingStyle = 1 << 2,
117 SpellCorrectionTriggered = 1 << 3,
119 typedef unsigned SetSelectionOptions; // Union of values in SetSelectionOption and EUserTriggered
120 static inline EUserTriggered selectionOptionsToUserTriggered(SetSelectionOptions options)
122 return static_cast<EUserTriggered>(options & UserTriggered);
125 FrameSelection(Frame* = 0);
127 Element* rootEditableElement() const { return m_selection.rootEditableElement(); }
128 bool isContentEditable() const { return m_selection.isContentEditable(); }
129 bool isContentRichlyEditable() const { return m_selection.isContentRichlyEditable(); }
131 void moveTo(const Range*, EAffinity, EUserTriggered = NotUserTriggered);
132 void moveTo(const VisiblePosition&, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
133 void moveTo(const VisiblePosition&, const VisiblePosition&, EUserTriggered = NotUserTriggered);
134 void moveTo(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
135 void moveTo(const Position&, const Position&, EAffinity, EUserTriggered = NotUserTriggered);
137 const VisibleSelection& selection() const { return m_selection; }
138 void setSelection(const VisibleSelection&, SetSelectionOptions = CloseTyping | ClearTypingStyle, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity);
139 void setSelection(const VisibleSelection& selection, TextGranularity granularity) { setSelection(selection, CloseTyping | ClearTypingStyle, AlignCursorOnScrollIfNeeded, granularity); }
140 bool setSelectedRange(Range*, EAffinity, bool closeTyping);
144 // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
145 void selectFrameElementInParentIfFullySelected();
147 bool contains(const LayoutPoint&);
149 VisibleSelection::SelectionType selectionType() const { return m_selection.selectionType(); }
151 EAffinity affinity() const { return m_selection.affinity(); }
153 bool modify(EAlteration, SelectionDirection, TextGranularity, EUserTriggered = NotUserTriggered);
154 enum VerticalDirection { DirectionUp, DirectionDown };
155 bool modify(EAlteration, unsigned verticalDistance, VerticalDirection, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
157 TextGranularity granularity() const { return m_granularity; }
159 void setStart(const VisiblePosition &, EUserTriggered = NotUserTriggered);
160 void setEnd(const VisiblePosition &, EUserTriggered = NotUserTriggered);
162 void setBase(const VisiblePosition&, EUserTriggered = NotUserTriggered);
163 void setBase(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
164 void setExtent(const VisiblePosition&, EUserTriggered = NotUserTriggered);
165 void setExtent(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
167 Position base() const { return m_selection.base(); }
168 Position extent() const { return m_selection.extent(); }
169 Position start() const { return m_selection.start(); }
170 Position end() const { return m_selection.end(); }
172 // Return the renderer that is responsible for painting the caret (in the selection start node)
173 RenderObject* caretRenderer() const;
175 // Caret rect local to the caret's renderer
176 LayoutRect localCaretRect();
178 // Bounds of (possibly transformed) caret in absolute coords
179 LayoutRect absoluteCaretBounds();
180 void setCaretRectNeedsUpdate() { CaretBase::setCaretRectNeedsUpdate(); }
182 void willBeModified(EAlteration, SelectionDirection);
184 bool isNone() const { return m_selection.isNone(); }
185 bool isCaret() const { return m_selection.isCaret(); }
186 bool isRange() const { return m_selection.isRange(); }
187 bool isCaretOrRange() const { return m_selection.isCaretOrRange(); }
188 bool isInPasswordField() const;
189 bool isAll(EditingBoundaryCrossingRule rule = CannotCrossEditingBoundary) const { return m_selection.isAll(rule); }
191 PassRefPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); }
193 void debugRenderer(RenderObject*, bool selected) const;
195 void nodeWillBeRemoved(Node*);
196 void textWillBeReplaced(CharacterData*, unsigned offset, unsigned oldLength, unsigned newLength);
198 void setCaretVisible(bool caretIsVisible) { setCaretVisibility(caretIsVisible ? Visible : Hidden); }
199 void clearCaretRectIfNeeded();
200 bool recomputeCaretRect();
201 void invalidateCaretRect();
202 void paintCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect);
204 // Used to suspend caret blinking while the mouse is down.
205 void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; }
206 bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; }
209 void setFocused(bool);
210 bool isFocused() const { return m_focused; }
211 bool isFocusedAndActive() const;
212 void pageActivationChanged();
215 void updateAppearance();
217 void updateSecureKeyboardEntryIfActive();
220 void formatForDebugger(char* buffer, unsigned length) const;
221 void showTreeForThis() const;
224 bool shouldChangeSelection(const VisibleSelection&) const;
225 bool shouldDeleteSelection(const VisibleSelection&) const;
226 void setNonDirectionalSelectionIfNeeded(const VisibleSelection&, TextGranularity);
227 void setFocusedNodeIfNeeded();
228 void notifyRendererOfSelectionChange(EUserTriggered);
230 void paintDragCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
232 EditingStyle* typingStyle() const;
233 PassRefPtr<CSSMutableStyleDeclaration> copyTypingStyle() const;
234 void setTypingStyle(PassRefPtr<EditingStyle>);
235 void clearTypingStyle();
237 FloatRect bounds(bool clipToVisibleContent = true) const;
239 void getClippedVisibleTextRectangles(Vector<FloatRect>&) const;
241 HTMLFormElement* currentForm() const;
243 void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, bool revealExtent = false);
244 void setSelectionFromNone();
247 enum EPositionType { START, END, BASE, EXTENT };
249 void respondToNodeModification(Node*, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved);
250 TextDirection directionOfEnclosingBlock();
251 TextDirection directionOfSelection();
253 VisiblePosition positionForPlatform(bool isGetStart) const;
254 VisiblePosition startForPlatform() const;
255 VisiblePosition endForPlatform() const;
257 VisiblePosition modifyExtendingRight(TextGranularity);
258 VisiblePosition modifyExtendingForward(TextGranularity);
259 VisiblePosition modifyMovingRight(TextGranularity);
260 VisiblePosition modifyMovingForward(TextGranularity);
261 VisiblePosition modifyExtendingLeft(TextGranularity);
262 VisiblePosition modifyExtendingBackward(TextGranularity);
263 VisiblePosition modifyMovingLeft(TextGranularity);
264 VisiblePosition modifyMovingBackward(TextGranularity);
266 LayoutUnit lineDirectionPointForBlockDirectionNavigation(EPositionType);
268 void notifyAccessibilityForSelectionChange();
270 void focusedOrActiveStateChanged();
272 void caretBlinkTimerFired(Timer<FrameSelection>*);
274 void setUseSecureKeyboardEntry(bool);
276 void setCaretVisibility(CaretVisibility);
278 bool dispatchSelectStart();
282 LayoutUnit m_xPosForVerticalArrowNavigation;
284 VisibleSelection m_selection;
285 TextGranularity m_granularity;
287 RefPtr<EditingStyle> m_typingStyle;
289 Timer<FrameSelection> m_caretBlinkTimer;
290 LayoutRect m_absCaretBounds; // absolute bounding rect for the caret
291 LayoutRect m_absoluteCaretRepaintBounds;
292 bool m_absCaretBoundsDirty : 1;
293 bool m_caretPaint : 1;
294 bool m_isCaretBlinkingSuspended : 1;
298 inline EditingStyle* FrameSelection::typingStyle() const
300 return m_typingStyle.get();
303 inline void FrameSelection::clearTypingStyle()
305 m_typingStyle.clear();
308 inline void FrameSelection::setTypingStyle(PassRefPtr<EditingStyle> style)
310 m_typingStyle = style;
313 #if !(PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(CHROMIUM))
314 inline void FrameSelection::notifyAccessibilityForSelectionChange()
319 } // namespace WebCore
322 // Outside the WebCore namespace for ease of invocation from gdb.
323 void showTree(const WebCore::FrameSelection&);
324 void showTree(const WebCore::FrameSelection*);
327 #endif // FrameSelection_h