1 /* 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple 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 FrameSelection_h 27 #define FrameSelection_h 28 29 #include "core/dom/Range.h" 30 #include "core/editing/Caret.h" 31 #include "core/editing/EditingStyle.h" 32 #include "core/editing/VisibleSelection.h" 33 #include "core/platform/Timer.h" 34 #include "core/platform/graphics/IntRect.h" 35 #include "core/platform/graphics/LayoutRect.h" 36 #include "core/rendering/ScrollBehavior.h" 37 #include "wtf/Noncopyable.h" 38 39 namespace WebCore { 40 41 class CharacterData; 42 class Frame; 43 class GraphicsContext; 44 class HTMLFormElement; 45 class MutableStylePropertySet; 46 class RenderObject; 47 class RenderView; 48 class Settings; 49 class VisiblePosition; 50 51 enum EUserTriggered { NotUserTriggered = 0, UserTriggered = 1 }; 52 53 enum RevealExtentOption { 54 RevealExtent, 55 DoNotRevealExtent 56 }; 57 58 class FrameSelection : private CaretBase { 59 WTF_MAKE_NONCOPYABLE(FrameSelection); 60 WTF_MAKE_FAST_ALLOCATED; 61 public: 62 enum EAlteration { AlterationMove, AlterationExtend }; 63 enum CursorAlignOnScroll { AlignCursorOnScrollIfNeeded, 64 AlignCursorOnScrollAlways }; 65 enum SetSelectionOption { 66 // 1 << 0 is reserved for EUserTriggered 67 CloseTyping = 1 << 1, 68 ClearTypingStyle = 1 << 2, 69 SpellCorrectionTriggered = 1 << 3, 70 DoNotSetFocus = 1 << 4, 71 DoNotUpdateAppearance = 1 << 5, 72 }; 73 typedef unsigned SetSelectionOptions; // Union of values in SetSelectionOption and EUserTriggered 74 static inline EUserTriggered selectionOptionsToUserTriggered(SetSelectionOptions options) 75 { 76 return static_cast<EUserTriggered>(options & UserTriggered); 77 } 78 79 explicit FrameSelection(Frame* = 0); 80 81 Element* rootEditableElement() const { return m_selection.rootEditableElement(); } 82 Element* rootEditableElementOrDocumentElement() const; 83 Node* rootEditableElementOrTreeScopeRootNode() const; 84 Element* rootEditableElementRespectingShadowTree() const; 85 86 bool rendererIsEditable() const { return m_selection.rendererIsEditable(); } 87 bool isContentEditable() const { return m_selection.isContentEditable(); } 88 bool isContentRichlyEditable() const { return m_selection.isContentRichlyEditable(); } 89 90 void moveTo(const Range*, EAffinity, EUserTriggered = NotUserTriggered); 91 void moveTo(const VisiblePosition&, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded); 92 void moveTo(const VisiblePosition&, const VisiblePosition&, EUserTriggered = NotUserTriggered); 93 void moveTo(const Position&, EAffinity, EUserTriggered = NotUserTriggered); 94 void moveTo(const Position&, const Position&, EAffinity, EUserTriggered = NotUserTriggered); 95 96 const VisibleSelection& selection() const { return m_selection; } 97 void setSelection(const VisibleSelection&, SetSelectionOptions = CloseTyping | ClearTypingStyle, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity); 98 void setSelection(const VisibleSelection& selection, TextGranularity granularity) { setSelection(selection, CloseTyping | ClearTypingStyle, AlignCursorOnScrollIfNeeded, granularity); } 99 bool setSelectedRange(Range*, EAffinity, bool closeTyping); 100 void selectAll(); 101 void clear(); 102 void prepareForDestruction(); 103 104 // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected. 105 void selectFrameElementInParentIfFullySelected(); 106 107 bool contains(const LayoutPoint&); 108 109 VisibleSelection::SelectionType selectionType() const { return m_selection.selectionType(); } 110 111 EAffinity affinity() const { return m_selection.affinity(); } 112 113 bool modify(EAlteration, SelectionDirection, TextGranularity, EUserTriggered = NotUserTriggered); 114 enum VerticalDirection { DirectionUp, DirectionDown }; 115 bool modify(EAlteration, unsigned verticalDistance, VerticalDirection, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded); 116 117 TextGranularity granularity() const { return m_granularity; } 118 119 void setStart(const VisiblePosition &, EUserTriggered = NotUserTriggered); 120 void setEnd(const VisiblePosition &, EUserTriggered = NotUserTriggered); 121 122 void setBase(const VisiblePosition&, EUserTriggered = NotUserTriggered); 123 void setBase(const Position&, EAffinity, EUserTriggered = NotUserTriggered); 124 void setExtent(const VisiblePosition&, EUserTriggered = NotUserTriggered); 125 void setExtent(const Position&, EAffinity, EUserTriggered = NotUserTriggered); 126 127 Position base() const { return m_selection.base(); } 128 Position extent() const { return m_selection.extent(); } 129 Position start() const { return m_selection.start(); } 130 Position end() const { return m_selection.end(); } 131 132 // Return the renderer that is responsible for painting the caret (in the selection start node) 133 RenderObject* caretRenderer() const; 134 135 // Caret rect local to the caret's renderer 136 LayoutRect localCaretRect(); 137 138 // Bounds of (possibly transformed) caret in absolute coords 139 IntRect absoluteCaretBounds(); 140 void setCaretRectNeedsUpdate() { CaretBase::setCaretRectNeedsUpdate(); } 141 142 void didChangeFocus(); 143 void willBeModified(EAlteration, SelectionDirection); 144 145 bool isNone() const { return m_selection.isNone(); } 146 bool isCaret() const { return m_selection.isCaret(); } 147 bool isRange() const { return m_selection.isRange(); } 148 bool isCaretOrRange() const { return m_selection.isCaretOrRange(); } 149 bool isInPasswordField() const; 150 bool isAll(EditingBoundaryCrossingRule rule = CannotCrossEditingBoundary) const { return m_selection.isAll(rule); } 151 152 PassRefPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); } 153 154 void debugRenderer(RenderObject*, bool selected) const; 155 156 void nodeWillBeRemoved(Node*); 157 void textWasReplaced(CharacterData*, unsigned offset, unsigned oldLength, unsigned newLength); 158 159 void setCaretVisible(bool caretIsVisible) { setCaretVisibility(caretIsVisible ? Visible : Hidden); } 160 bool recomputeCaretRect(); 161 void invalidateCaretRect(); 162 void paintCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect); 163 164 // Used to suspend caret blinking while the mouse is down. 165 void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; } 166 bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; } 167 168 // Focus 169 void setFocused(bool); 170 bool isFocused() const { return m_focused; } 171 bool isFocusedAndActive() const; 172 void pageActivationChanged(); 173 174 // Painting. 175 void updateAppearance(); 176 177 void updateSecureKeyboardEntryIfActive(); 178 179 #ifndef NDEBUG 180 void formatForDebugger(char* buffer, unsigned length) const; 181 void showTreeForThis() const; 182 #endif 183 184 bool shouldChangeSelection(const VisibleSelection&) const; 185 bool shouldDeleteSelection(const VisibleSelection&) const; 186 enum EndPointsAdjustmentMode { AdjustEndpointsAtBidiBoundary, DoNotAdjsutEndpoints }; 187 void setNonDirectionalSelectionIfNeeded(const VisibleSelection&, TextGranularity, EndPointsAdjustmentMode = DoNotAdjsutEndpoints); 188 void setFocusedNodeIfNeeded(); 189 void notifyRendererOfSelectionChange(EUserTriggered); 190 191 void paintDragCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const; 192 193 EditingStyle* typingStyle() const; 194 PassRefPtr<MutableStylePropertySet> copyTypingStyle() const; 195 void setTypingStyle(PassRefPtr<EditingStyle>); 196 void clearTypingStyle(); 197 198 String selectedText() const; 199 String selectedTextForClipboard() const; 200 201 FloatRect bounds(bool clipToVisibleContent = true) const; 202 203 void getClippedVisibleTextRectangles(Vector<FloatRect>&) const; 204 205 HTMLFormElement* currentForm() const; 206 207 void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent); 208 void setSelectionFromNone(); 209 210 bool shouldShowBlockCursor() const { return m_shouldShowBlockCursor; } 211 void setShouldShowBlockCursor(bool); 212 213 private: 214 enum EPositionType { START, END, BASE, EXTENT }; 215 216 void respondToNodeModification(Node*, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved); 217 TextDirection directionOfEnclosingBlock(); 218 TextDirection directionOfSelection(); 219 220 VisiblePosition positionForPlatform(bool isGetStart) const; 221 VisiblePosition startForPlatform() const; 222 VisiblePosition endForPlatform() const; 223 VisiblePosition nextWordPositionForPlatform(const VisiblePosition&); 224 225 VisiblePosition modifyExtendingRight(TextGranularity); 226 VisiblePosition modifyExtendingForward(TextGranularity); 227 VisiblePosition modifyMovingRight(TextGranularity); 228 VisiblePosition modifyMovingForward(TextGranularity); 229 VisiblePosition modifyExtendingLeft(TextGranularity); 230 VisiblePosition modifyExtendingBackward(TextGranularity); 231 VisiblePosition modifyMovingLeft(TextGranularity); 232 VisiblePosition modifyMovingBackward(TextGranularity); 233 234 LayoutUnit lineDirectionPointForBlockDirectionNavigation(EPositionType); 235 236 void notifyAccessibilityForSelectionChange(); 237 238 void focusedOrActiveStateChanged(); 239 240 void caretBlinkTimerFired(Timer<FrameSelection>*); 241 242 void setUseSecureKeyboardEntry(bool); 243 244 void setCaretVisibility(CaretVisibility); 245 bool shouldBlinkCaret() const; 246 247 bool dispatchSelectStart(); 248 249 bool visualWordMovementEnabled() const; 250 251 Frame* m_frame; 252 253 LayoutUnit m_xPosForVerticalArrowNavigation; 254 255 VisibleSelection m_selection; 256 VisiblePosition m_originalBase; // Used to store base before the adjustment at bidi boundary 257 TextGranularity m_granularity; 258 259 RefPtr<Node> m_previousCaretNode; // The last node which painted the caret. Retained for clearing the old caret when it moves. 260 261 RefPtr<EditingStyle> m_typingStyle; 262 263 Timer<FrameSelection> m_caretBlinkTimer; 264 // The painted bounds of the caret in absolute coordinates 265 IntRect m_absCaretBounds; 266 bool m_absCaretBoundsDirty : 1; 267 bool m_caretPaint : 1; 268 bool m_isCaretBlinkingSuspended : 1; 269 bool m_focused : 1; 270 bool m_shouldShowBlockCursor : 1; 271 }; 272 273 inline EditingStyle* FrameSelection::typingStyle() const 274 { 275 return m_typingStyle.get(); 276 } 277 278 inline void FrameSelection::clearTypingStyle() 279 { 280 m_typingStyle.clear(); 281 } 282 283 inline void FrameSelection::setTypingStyle(PassRefPtr<EditingStyle> style) 284 { 285 m_typingStyle = style; 286 } 287 } // namespace WebCore 288 289 #ifndef NDEBUG 290 // Outside the WebCore namespace for ease of invocation from gdb. 291 void showTree(const WebCore::FrameSelection&); 292 void showTree(const WebCore::FrameSelection*); 293 #endif 294 295 #endif // FrameSelection_h 296