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,
118 DoNotSetFocus = 1 << 4,
120 typedef unsigned SetSelectionOptions; // Union of values in SetSelectionOption and EUserTriggered
121 static inline EUserTriggered selectionOptionsToUserTriggered(SetSelectionOptions options)
123 return static_cast<EUserTriggered>(options & UserTriggered);
126 FrameSelection(Frame* = 0);
128 Element* rootEditableElement() const { return m_selection.rootEditableElement(); }
129 Element* rootEditableElementOrDocumentElement() const;
130 bool isContentEditable() const { return m_selection.isContentEditable(); }
131 bool isContentRichlyEditable() const { return m_selection.isContentRichlyEditable(); }
133 void moveTo(const Range*, EAffinity, EUserTriggered = NotUserTriggered);
134 void moveTo(const VisiblePosition&, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
135 void moveTo(const VisiblePosition&, const VisiblePosition&, EUserTriggered = NotUserTriggered);
136 void moveTo(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
137 void moveTo(const Position&, const Position&, EAffinity, EUserTriggered = NotUserTriggered);
139 const VisibleSelection& selection() const { return m_selection; }
140 void setSelection(const VisibleSelection&, SetSelectionOptions = CloseTyping | ClearTypingStyle, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity);
141 void setSelection(const VisibleSelection& selection, TextGranularity granularity) { setSelection(selection, CloseTyping | ClearTypingStyle, AlignCursorOnScrollIfNeeded, granularity); }
142 bool setSelectedRange(Range*, EAffinity, bool closeTyping);
146 // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
147 void selectFrameElementInParentIfFullySelected();
149 bool contains(const LayoutPoint&);
151 VisibleSelection::SelectionType selectionType() const { return m_selection.selectionType(); }
153 EAffinity affinity() const { return m_selection.affinity(); }
155 bool modify(EAlteration, SelectionDirection, TextGranularity, EUserTriggered = NotUserTriggered);
156 enum VerticalDirection { DirectionUp, DirectionDown };
157 bool modify(EAlteration, unsigned verticalDistance, VerticalDirection, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
159 TextGranularity granularity() const { return m_granularity; }
161 void setStart(const VisiblePosition &, EUserTriggered = NotUserTriggered);
162 void setEnd(const VisiblePosition &, EUserTriggered = NotUserTriggered);
164 void setBase(const VisiblePosition&, EUserTriggered = NotUserTriggered);
165 void setBase(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
166 void setExtent(const VisiblePosition&, EUserTriggered = NotUserTriggered);
167 void setExtent(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
169 Position base() const { return m_selection.base(); }
170 Position extent() const { return m_selection.extent(); }
171 Position start() const { return m_selection.start(); }
172 Position end() const { return m_selection.end(); }
174 // Return the renderer that is responsible for painting the caret (in the selection start node)
175 RenderObject* caretRenderer() const;
177 // Caret rect local to the caret's renderer
178 LayoutRect localCaretRect();
180 // Bounds of (possibly transformed) caret in absolute coords
181 LayoutRect absoluteCaretBounds();
182 void setCaretRectNeedsUpdate() { CaretBase::setCaretRectNeedsUpdate(); }
184 void willBeModified(EAlteration, SelectionDirection);
186 bool isNone() const { return m_selection.isNone(); }
187 bool isCaret() const { return m_selection.isCaret(); }
188 bool isRange() const { return m_selection.isRange(); }
189 bool isCaretOrRange() const { return m_selection.isCaretOrRange(); }
190 bool isInPasswordField() const;
191 bool isAll(EditingBoundaryCrossingRule rule = CannotCrossEditingBoundary) const { return m_selection.isAll(rule); }
193 PassRefPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); }
195 void debugRenderer(RenderObject*, bool selected) const;
197 void nodeWillBeRemoved(Node*);
198 void textWillBeReplaced(CharacterData*, unsigned offset, unsigned oldLength, unsigned newLength);
200 void setCaretVisible(bool caretIsVisible) { setCaretVisibility(caretIsVisible ? Visible : Hidden); }
201 void clearCaretRectIfNeeded();
202 bool recomputeCaretRect();
203 void invalidateCaretRect();
204 void paintCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect);
206 // Used to suspend caret blinking while the mouse is down.
207 void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; }
208 bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; }
211 void setFocused(bool);
212 bool isFocused() const { return m_focused; }
213 bool isFocusedAndActive() const;
214 void pageActivationChanged();
217 void updateAppearance();
219 void updateSecureKeyboardEntryIfActive();
222 void formatForDebugger(char* buffer, unsigned length) const;
223 void showTreeForThis() const;
226 bool shouldChangeSelection(const VisibleSelection&) const;
227 bool shouldDeleteSelection(const VisibleSelection&) const;
228 enum EndPointsAdjustmentMode { AdjustEndpointsAtBidiBoundary, DoNotAdjsutEndpoints };
229 void setNonDirectionalSelectionIfNeeded(const VisibleSelection&, TextGranularity, EndPointsAdjustmentMode = DoNotAdjsutEndpoints);
230 void setFocusedNodeIfNeeded();
231 void notifyRendererOfSelectionChange(EUserTriggered);
233 void paintDragCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
235 EditingStyle* typingStyle() const;
236 PassRefPtr<CSSMutableStyleDeclaration> copyTypingStyle() const;
237 void setTypingStyle(PassRefPtr<EditingStyle>);
238 void clearTypingStyle();
240 FloatRect bounds(bool clipToVisibleContent = true) const;
242 void getClippedVisibleTextRectangles(Vector<FloatRect>&) const;
244 HTMLFormElement* currentForm() const;
246 void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, bool revealExtent = false);
247 void setSelectionFromNone();
249 #if ENABLE(TIZEN_CONTEXT_MENU_SELECT)
250 bool canEditableSelectRange() const;
254 enum EPositionType { START, END, BASE, EXTENT };
256 void respondToNodeModification(Node*, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved);
257 TextDirection directionOfEnclosingBlock();
258 TextDirection directionOfSelection();
260 VisiblePosition positionForPlatform(bool isGetStart) const;
261 VisiblePosition startForPlatform() const;
262 VisiblePosition endForPlatform() const;
264 VisiblePosition modifyExtendingRight(TextGranularity);
265 VisiblePosition modifyExtendingForward(TextGranularity);
266 VisiblePosition modifyMovingRight(TextGranularity);
267 VisiblePosition modifyMovingForward(TextGranularity);
268 VisiblePosition modifyExtendingLeft(TextGranularity);
269 VisiblePosition modifyExtendingBackward(TextGranularity);
270 VisiblePosition modifyMovingLeft(TextGranularity);
271 VisiblePosition modifyMovingBackward(TextGranularity);
273 LayoutUnit lineDirectionPointForBlockDirectionNavigation(EPositionType);
275 void notifyAccessibilityForSelectionChange();
277 void focusedOrActiveStateChanged();
279 void caretBlinkTimerFired(Timer<FrameSelection>*);
281 void setUseSecureKeyboardEntry(bool);
283 void setCaretVisibility(CaretVisibility);
285 bool dispatchSelectStart();
287 bool visualWordMovementEnabled() const;
291 LayoutUnit m_xPosForVerticalArrowNavigation;
293 VisibleSelection m_selection;
294 VisiblePosition m_originalBase; // Used to store base before the adjustment at bidi boundary
295 TextGranularity m_granularity;
297 RefPtr<EditingStyle> m_typingStyle;
299 Timer<FrameSelection> m_caretBlinkTimer;
300 LayoutRect m_absCaretBounds; // absolute bounding rect for the caret
301 LayoutRect m_absoluteCaretRepaintBounds;
302 bool m_absCaretBoundsDirty : 1;
303 bool m_caretPaint : 1;
304 bool m_isCaretBlinkingSuspended : 1;
308 inline EditingStyle* FrameSelection::typingStyle() const
310 return m_typingStyle.get();
313 inline void FrameSelection::clearTypingStyle()
315 m_typingStyle.clear();
318 inline void FrameSelection::setTypingStyle(PassRefPtr<EditingStyle> style)
320 m_typingStyle = style;
323 #if !(PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(CHROMIUM))
324 inline void FrameSelection::notifyAccessibilityForSelectionChange()
329 } // namespace WebCore
332 // Outside the WebCore namespace for ease of invocation from gdb.
333 void showTree(const WebCore::FrameSelection&);
334 void showTree(const WebCore::FrameSelection*);
337 #endif // FrameSelection_h