Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / FrameSelection.h
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/rendering/ScrollAlignment.h"
34 #include "platform/Timer.h"
35 #include "platform/geometry/IntRect.h"
36 #include "platform/geometry/LayoutRect.h"
37 #include "wtf/Noncopyable.h"
38
39 namespace WebCore {
40
41 class CharacterData;
42 class LocalFrame;
43 class GraphicsContext;
44 class HTMLFormElement;
45 class MutableStylePropertySet;
46 class RenderObject;
47 class RenderView;
48 class Settings;
49 class Text;
50 class VisiblePosition;
51
52 enum EUserTriggered { NotUserTriggered = 0, UserTriggered = 1 };
53
54 enum RevealExtentOption {
55     RevealExtent,
56     DoNotRevealExtent
57 };
58
59 class FrameSelection FINAL : public VisibleSelection::ChangeObserver, private CaretBase {
60     WTF_MAKE_NONCOPYABLE(FrameSelection);
61     WTF_MAKE_FAST_ALLOCATED;
62 public:
63     enum EAlteration { AlterationMove, AlterationExtend };
64     enum CursorAlignOnScroll { AlignCursorOnScrollIfNeeded,
65                                AlignCursorOnScrollAlways };
66     enum SetSelectionOption {
67         // 1 << 0 is reserved for EUserTriggered
68         CloseTyping = 1 << 1,
69         ClearTypingStyle = 1 << 2,
70         SpellCorrectionTriggered = 1 << 3,
71         DoNotSetFocus = 1 << 4,
72         DoNotUpdateAppearance = 1 << 5,
73     };
74     typedef unsigned SetSelectionOptions; // Union of values in SetSelectionOption and EUserTriggered
75     static inline EUserTriggered selectionOptionsToUserTriggered(SetSelectionOptions options)
76     {
77         return static_cast<EUserTriggered>(options & UserTriggered);
78     }
79
80     explicit FrameSelection(LocalFrame* = 0);
81     virtual ~FrameSelection();
82
83     Element* rootEditableElement() const { return m_selection.rootEditableElement(); }
84     Element* rootEditableElementOrDocumentElement() const;
85     Node* rootEditableElementOrTreeScopeRootNode() const;
86
87     bool rendererIsEditable() const { return m_selection.rendererIsEditable(); }
88     bool isContentEditable() const { return m_selection.isContentEditable(); }
89     bool isContentRichlyEditable() const { return m_selection.isContentRichlyEditable(); }
90
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
95     const VisibleSelection& selection() const { return m_selection; }
96     void setSelection(const VisibleSelection&, SetSelectionOptions = CloseTyping | ClearTypingStyle, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity);
97     void setSelection(const VisibleSelection& selection, TextGranularity granularity) { setSelection(selection, CloseTyping | ClearTypingStyle, AlignCursorOnScrollIfNeeded, granularity); }
98     bool setSelectedRange(Range*, EAffinity, SetSelectionOptions = CloseTyping | ClearTypingStyle);
99     void selectAll();
100     void clear();
101     void prepareForDestruction();
102
103     // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
104     void selectFrameElementInParentIfFullySelected();
105
106     bool contains(const LayoutPoint&);
107
108     SelectionType selectionType() const { return m_selection.selectionType(); }
109
110     EAffinity affinity() const { return m_selection.affinity(); }
111
112     bool modify(EAlteration, SelectionDirection, TextGranularity, EUserTriggered = NotUserTriggered);
113     enum VerticalDirection { DirectionUp, DirectionDown };
114     bool modify(EAlteration, unsigned verticalDistance, VerticalDirection, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
115
116     TextGranularity granularity() const { return m_granularity; }
117
118     void setStart(const VisiblePosition &, EUserTriggered = NotUserTriggered);
119     void setEnd(const VisiblePosition &, EUserTriggered = NotUserTriggered);
120
121     void setBase(const VisiblePosition&, EUserTriggered = NotUserTriggered);
122     void setExtent(const VisiblePosition&, EUserTriggered = NotUserTriggered);
123
124     Position base() const { return m_selection.base(); }
125     Position extent() const { return m_selection.extent(); }
126     Position start() const { return m_selection.start(); }
127     Position end() const { return m_selection.end(); }
128
129     // Return the renderer that is responsible for painting the caret (in the selection start node)
130     RenderObject* caretRenderer() const;
131
132     // Caret rect local to the caret's renderer
133     LayoutRect localCaretRect();
134
135     // Bounds of (possibly transformed) caret in absolute coords
136     IntRect absoluteCaretBounds();
137     void setCaretRectNeedsUpdate() { CaretBase::setCaretRectNeedsUpdate(); }
138
139     void didChangeFocus();
140     void willBeModified(EAlteration, SelectionDirection);
141
142     bool isNone() const { return m_selection.isNone(); }
143     bool isCaret() const { return m_selection.isCaret(); }
144     bool isRange() const { return m_selection.isRange(); }
145     bool isCaretOrRange() const { return m_selection.isCaretOrRange(); }
146     bool isInPasswordField() const;
147
148     // If this FrameSelection has a logical range which is still valid, this function return its clone. Otherwise,
149     // the return value from underlying VisibleSelection's firstRange() is returned.
150     PassRefPtrWillBeRawPtr<Range> firstRange() const;
151
152     PassRefPtrWillBeRawPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); }
153
154     void nodeWillBeRemoved(Node&);
155     void didUpdateCharacterData(CharacterData*, unsigned offset, unsigned oldLength, unsigned newLength);
156     void didMergeTextNodes(const Text& oldNode, unsigned offset);
157     void didSplitTextNode(const Text& oldNode);
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     enum EndPointsAdjustmentMode { AdjustEndpointsAtBidiBoundary, DoNotAdjsutEndpoints };
185     void setNonDirectionalSelectionIfNeeded(const VisibleSelection&, TextGranularity, EndPointsAdjustmentMode = DoNotAdjsutEndpoints);
186     void setFocusedNodeIfNeeded();
187     void notifyRendererOfSelectionChange(EUserTriggered);
188
189     EditingStyle* typingStyle() const;
190     void setTypingStyle(PassRefPtr<EditingStyle>);
191     void clearTypingStyle();
192
193     String selectedText() const;
194     String selectedTextForClipboard() const;
195
196     FloatRect bounds(bool clipToVisibleContent = true) const;
197
198     HTMLFormElement* currentForm() const;
199
200     void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent);
201     void setSelectionFromNone();
202
203     void setShouldShowBlockCursor(bool);
204
205     // VisibleSelection::ChangeObserver interface.
206     virtual void didChangeVisibleSelection() OVERRIDE;
207
208 private:
209     enum EPositionType { START, END, BASE, EXTENT };
210
211     void respondToNodeModification(Node&, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved);
212     TextDirection directionOfEnclosingBlock();
213     TextDirection directionOfSelection();
214
215     VisiblePosition positionForPlatform(bool isGetStart) const;
216     VisiblePosition startForPlatform() const;
217     VisiblePosition endForPlatform() const;
218     VisiblePosition nextWordPositionForPlatform(const VisiblePosition&);
219
220     VisiblePosition modifyExtendingRight(TextGranularity);
221     VisiblePosition modifyExtendingForward(TextGranularity);
222     VisiblePosition modifyMovingRight(TextGranularity);
223     VisiblePosition modifyMovingForward(TextGranularity);
224     VisiblePosition modifyExtendingLeft(TextGranularity);
225     VisiblePosition modifyExtendingBackward(TextGranularity);
226     VisiblePosition modifyMovingLeft(TextGranularity);
227     VisiblePosition modifyMovingBackward(TextGranularity);
228
229     LayoutUnit lineDirectionPointForBlockDirectionNavigation(EPositionType);
230
231     void notifyAccessibilityForSelectionChange();
232
233     void focusedOrActiveStateChanged();
234
235     void caretBlinkTimerFired(Timer<FrameSelection>*);
236
237     void setUseSecureKeyboardEntry(bool);
238
239     void setCaretVisibility(CaretVisibility);
240     bool shouldBlinkCaret() const;
241
242     bool dispatchSelectStart();
243
244     void updateSelectionIfNeeded(const Position& base, const Position& extent, const Position& start, const Position& end);
245
246     void startObservingVisibleSelectionChange();
247     void stopObservingVisibleSelectionChangeIfNecessary();
248
249     LocalFrame* m_frame;
250
251     LayoutUnit m_xPosForVerticalArrowNavigation;
252
253     VisibleSelection m_selection;
254     bool m_observingVisibleSelection;
255     VisiblePosition m_originalBase; // Used to store base before the adjustment at bidi boundary
256     TextGranularity m_granularity;
257
258     // The range specified by the user, which may not be visually canonicalized (hence "logical").
259     // This will be invalidated if the underlying VisibleSelection changes. If that happens, this variable will
260     // become null, in which case logical positions == visible positions.
261     RefPtrWillBePersistent<Range> m_logicalRange;
262
263     RefPtr<Node> m_previousCaretNode; // The last node which painted the caret. Retained for clearing the old caret when it moves.
264
265     RefPtr<EditingStyle> m_typingStyle;
266
267     Timer<FrameSelection> m_caretBlinkTimer;
268     // The painted bounds of the caret in absolute coordinates
269     IntRect m_absCaretBounds;
270     bool m_absCaretBoundsDirty : 1;
271     bool m_caretPaint : 1;
272     bool m_isCaretBlinkingSuspended : 1;
273     bool m_focused : 1;
274     bool m_shouldShowBlockCursor : 1;
275 };
276
277 inline EditingStyle* FrameSelection::typingStyle() const
278 {
279     return m_typingStyle.get();
280 }
281
282 inline void FrameSelection::clearTypingStyle()
283 {
284     m_typingStyle.clear();
285 }
286
287 inline void FrameSelection::setTypingStyle(PassRefPtr<EditingStyle> style)
288 {
289     m_typingStyle = style;
290 }
291 } // namespace WebCore
292
293 #ifndef NDEBUG
294 // Outside the WebCore namespace for ease of invocation from gdb.
295 void showTree(const WebCore::FrameSelection&);
296 void showTree(const WebCore::FrameSelection*);
297 #endif
298
299 #endif // FrameSelection_h