Upstream version 5.34.92.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 Frame;
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 : 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(Frame* = 0);
81
82     Element* rootEditableElement() const { return m_selection.rootEditableElement(); }
83     Element* rootEditableElementOrDocumentElement() const;
84     Node* rootEditableElementOrTreeScopeRootNode() 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 VisiblePosition&, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
91     void moveTo(const VisiblePosition&, const VisiblePosition&, EUserTriggered = NotUserTriggered);
92     void moveTo(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
93
94     const VisibleSelection& selection() const { return m_selection; }
95     void setSelection(const VisibleSelection&, SetSelectionOptions = CloseTyping | ClearTypingStyle, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity);
96     void setSelection(const VisibleSelection& selection, TextGranularity granularity) { setSelection(selection, CloseTyping | ClearTypingStyle, AlignCursorOnScrollIfNeeded, granularity); }
97     bool setSelectedRange(Range*, EAffinity, bool closeTyping);
98     void selectAll();
99     void clear();
100     void prepareForDestruction();
101
102     // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
103     void selectFrameElementInParentIfFullySelected();
104
105     bool contains(const LayoutPoint&);
106
107     SelectionType selectionType() const { return m_selection.selectionType(); }
108
109     EAffinity affinity() const { return m_selection.affinity(); }
110
111     bool modify(EAlteration, SelectionDirection, TextGranularity, EUserTriggered = NotUserTriggered);
112     enum VerticalDirection { DirectionUp, DirectionDown };
113     bool modify(EAlteration, unsigned verticalDistance, VerticalDirection, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
114
115     TextGranularity granularity() const { return m_granularity; }
116
117     void setStart(const VisiblePosition &, EUserTriggered = NotUserTriggered);
118     void setEnd(const VisiblePosition &, EUserTriggered = NotUserTriggered);
119
120     void setBase(const VisiblePosition&, EUserTriggered = NotUserTriggered);
121     void setExtent(const VisiblePosition&, EUserTriggered = NotUserTriggered);
122
123     Position base() const { return m_selection.base(); }
124     Position extent() const { return m_selection.extent(); }
125     Position start() const { return m_selection.start(); }
126     Position end() const { return m_selection.end(); }
127
128     // Return the renderer that is responsible for painting the caret (in the selection start node)
129     RenderObject* caretRenderer() const;
130
131     // Caret rect local to the caret's renderer
132     LayoutRect localCaretRect();
133
134     // Bounds of (possibly transformed) caret in absolute coords
135     IntRect absoluteCaretBounds();
136     void setCaretRectNeedsUpdate() { CaretBase::setCaretRectNeedsUpdate(); }
137
138     void didChangeFocus();
139     void willBeModified(EAlteration, SelectionDirection);
140
141     bool isNone() const { return m_selection.isNone(); }
142     bool isCaret() const { return m_selection.isCaret(); }
143     bool isRange() const { return m_selection.isRange(); }
144     bool isCaretOrRange() const { return m_selection.isCaretOrRange(); }
145     bool isInPasswordField() const;
146
147     PassRefPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); }
148
149     void nodeWillBeRemoved(Node&);
150     void didUpdateCharacterData(CharacterData*, unsigned offset, unsigned oldLength, unsigned newLength);
151     void didMergeTextNodes(const Text& oldNode, unsigned offset);
152     void didSplitTextNode(const Text& oldNode);
153
154     void setCaretVisible(bool caretIsVisible) { setCaretVisibility(caretIsVisible ? Visible : Hidden); }
155     bool recomputeCaretRect();
156     void invalidateCaretRect();
157     void paintCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect);
158
159     // Used to suspend caret blinking while the mouse is down.
160     void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; }
161     bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; }
162
163     // Focus
164     void setFocused(bool);
165     bool isFocused() const { return m_focused; }
166     bool isFocusedAndActive() const;
167     void pageActivationChanged();
168
169     // Painting.
170     void updateAppearance();
171
172     void updateSecureKeyboardEntryIfActive();
173
174 #ifndef NDEBUG
175     void formatForDebugger(char* buffer, unsigned length) const;
176     void showTreeForThis() const;
177 #endif
178
179     enum EndPointsAdjustmentMode { AdjustEndpointsAtBidiBoundary, DoNotAdjsutEndpoints };
180     void setNonDirectionalSelectionIfNeeded(const VisibleSelection&, TextGranularity, EndPointsAdjustmentMode = DoNotAdjsutEndpoints);
181     void setFocusedNodeIfNeeded();
182     void notifyRendererOfSelectionChange(EUserTriggered);
183
184     EditingStyle* typingStyle() const;
185     void setTypingStyle(PassRefPtr<EditingStyle>);
186     void clearTypingStyle();
187
188     String selectedText() const;
189     String selectedTextForClipboard() const;
190
191     FloatRect bounds(bool clipToVisibleContent = true) const;
192
193     HTMLFormElement* currentForm() const;
194
195     void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent);
196     void setSelectionFromNone();
197
198     void setShouldShowBlockCursor(bool);
199
200 private:
201     enum EPositionType { START, END, BASE, EXTENT };
202
203     void respondToNodeModification(Node&, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved);
204     TextDirection directionOfEnclosingBlock();
205     TextDirection directionOfSelection();
206
207     VisiblePosition positionForPlatform(bool isGetStart) const;
208     VisiblePosition startForPlatform() const;
209     VisiblePosition endForPlatform() const;
210     VisiblePosition nextWordPositionForPlatform(const VisiblePosition&);
211
212     VisiblePosition modifyExtendingRight(TextGranularity);
213     VisiblePosition modifyExtendingForward(TextGranularity);
214     VisiblePosition modifyMovingRight(TextGranularity);
215     VisiblePosition modifyMovingForward(TextGranularity);
216     VisiblePosition modifyExtendingLeft(TextGranularity);
217     VisiblePosition modifyExtendingBackward(TextGranularity);
218     VisiblePosition modifyMovingLeft(TextGranularity);
219     VisiblePosition modifyMovingBackward(TextGranularity);
220
221     LayoutUnit lineDirectionPointForBlockDirectionNavigation(EPositionType);
222
223     void notifyAccessibilityForSelectionChange();
224
225     void focusedOrActiveStateChanged();
226
227     void caretBlinkTimerFired(Timer<FrameSelection>*);
228
229     void setUseSecureKeyboardEntry(bool);
230
231     void setCaretVisibility(CaretVisibility);
232     bool shouldBlinkCaret() const;
233
234     bool dispatchSelectStart();
235
236     void updateSelectionIfNeeded(const Position& base, const Position& extent, const Position& start, const Position& end);
237
238     Frame* m_frame;
239
240     LayoutUnit m_xPosForVerticalArrowNavigation;
241
242     VisibleSelection m_selection;
243     VisiblePosition m_originalBase; // Used to store base before the adjustment at bidi boundary
244     TextGranularity m_granularity;
245
246     RefPtr<Node> m_previousCaretNode; // The last node which painted the caret. Retained for clearing the old caret when it moves.
247
248     RefPtr<EditingStyle> m_typingStyle;
249
250     Timer<FrameSelection> m_caretBlinkTimer;
251     // The painted bounds of the caret in absolute coordinates
252     IntRect m_absCaretBounds;
253     bool m_absCaretBoundsDirty : 1;
254     bool m_caretPaint : 1;
255     bool m_isCaretBlinkingSuspended : 1;
256     bool m_focused : 1;
257     bool m_shouldShowBlockCursor : 1;
258 };
259
260 inline EditingStyle* FrameSelection::typingStyle() const
261 {
262     return m_typingStyle.get();
263 }
264
265 inline void FrameSelection::clearTypingStyle()
266 {
267     m_typingStyle.clear();
268 }
269
270 inline void FrameSelection::setTypingStyle(PassRefPtr<EditingStyle> style)
271 {
272     m_typingStyle = style;
273 }
274 } // namespace WebCore
275
276 #ifndef NDEBUG
277 // Outside the WebCore namespace for ease of invocation from gdb.
278 void showTree(const WebCore::FrameSelection&);
279 void showTree(const WebCore::FrameSelection*);
280 #endif
281
282 #endif // FrameSelection_h