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