initial import
[vuplus_webkit] / Source / WebCore / 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 "EditingStyle.h"
30 #include "IntRect.h"
31 #include "LayoutTypes.h"
32 #include "Range.h"
33 #include "ScrollBehavior.h"
34 #include "Timer.h"
35 #include "VisibleSelection.h"
36 #include <wtf/Noncopyable.h>
37
38 namespace WebCore {
39
40 class CharacterData;
41 class CSSMutableStyleDeclaration;
42 class Frame;
43 class GraphicsContext;
44 class HTMLFormElement;
45 class RenderObject;
46 class RenderView;
47 class Settings;
48 class VisiblePosition;
49
50 enum EUserTriggered { NotUserTriggered = 0, UserTriggered = 1 };
51
52 class CaretBase {
53     WTF_MAKE_NONCOPYABLE(CaretBase);
54     WTF_MAKE_FAST_ALLOCATED;
55 protected:
56     enum CaretVisibility { Visible, Hidden };
57     explicit CaretBase(CaretVisibility = Hidden);
58
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;
67
68     const LayoutRect& localCaretRectWithoutUpdate() const { return m_caretLocalRect; }
69
70     bool shouldUpdateCaretRect() const { return m_caretRectNeedsUpdate; }
71     void setCaretRectNeedsUpdate() { m_caretRectNeedsUpdate = true; }
72
73     void setCaretVisibility(CaretVisibility visibility) { m_caretVisibility = visibility; }
74     bool caretIsVisible() const { return m_caretVisibility == Visible; }
75     CaretVisibility caretVisibility() const { return m_caretVisibility; }
76
77 private:
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;
81 };
82
83 class DragCaretController : private CaretBase {
84     WTF_MAKE_NONCOPYABLE(DragCaretController);
85     WTF_MAKE_FAST_ALLOCATED;
86 public:
87     DragCaretController();
88
89     RenderObject* caretRenderer() const;
90     void paintDragCaret(Frame*, GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
91
92     bool isContentEditable() const { return m_position.rootEditableElement(); }
93     bool isContentRichlyEditable() const;
94
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()); }
99
100     void nodeWillBeRemoved(Node*);
101
102 private:
103     VisiblePosition m_position;
104 };
105
106 class FrameSelection : private CaretBase {
107     WTF_MAKE_NONCOPYABLE(FrameSelection);
108     WTF_MAKE_FAST_ALLOCATED;
109 public:
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     };
119     typedef unsigned SetSelectionOptions; // Union of values in SetSelectionOption and EUserTriggered
120     static inline EUserTriggered selectionOptionsToUserTriggered(SetSelectionOptions options)
121     {
122         return static_cast<EUserTriggered>(options & UserTriggered);
123     }
124
125     FrameSelection(Frame* = 0);
126
127     Element* rootEditableElement() const { return m_selection.rootEditableElement(); }
128     bool isContentEditable() const { return m_selection.isContentEditable(); }
129     bool isContentRichlyEditable() const { return m_selection.isContentRichlyEditable(); }
130      
131     void moveTo(const Range*, EAffinity, EUserTriggered = NotUserTriggered);
132     void moveTo(const VisiblePosition&, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
133     void moveTo(const VisiblePosition&, const VisiblePosition&, EUserTriggered = NotUserTriggered);
134     void moveTo(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
135     void moveTo(const Position&, const Position&, EAffinity, EUserTriggered = NotUserTriggered);
136
137     const VisibleSelection& selection() const { return m_selection; }
138     void setSelection(const VisibleSelection&, SetSelectionOptions = CloseTyping | ClearTypingStyle, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity);
139     void setSelection(const VisibleSelection& selection, TextGranularity granularity) { setSelection(selection, CloseTyping | ClearTypingStyle, AlignCursorOnScrollIfNeeded, granularity); }
140     bool setSelectedRange(Range*, EAffinity, bool closeTyping);
141     void selectAll();
142     void clear();
143     
144     // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
145     void selectFrameElementInParentIfFullySelected();
146
147     bool contains(const LayoutPoint&);
148
149     VisibleSelection::SelectionType selectionType() const { return m_selection.selectionType(); }
150
151     EAffinity affinity() const { return m_selection.affinity(); }
152
153     bool modify(EAlteration, SelectionDirection, TextGranularity, EUserTriggered = NotUserTriggered);
154     enum VerticalDirection { DirectionUp, DirectionDown };
155     bool modify(EAlteration, unsigned verticalDistance, VerticalDirection, EUserTriggered = NotUserTriggered, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
156
157     TextGranularity granularity() const { return m_granularity; }
158
159     void setStart(const VisiblePosition &, EUserTriggered = NotUserTriggered);
160     void setEnd(const VisiblePosition &, EUserTriggered = NotUserTriggered);
161     
162     void setBase(const VisiblePosition&, EUserTriggered = NotUserTriggered);
163     void setBase(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
164     void setExtent(const VisiblePosition&, EUserTriggered = NotUserTriggered);
165     void setExtent(const Position&, EAffinity, EUserTriggered = NotUserTriggered);
166
167     Position base() const { return m_selection.base(); }
168     Position extent() const { return m_selection.extent(); }
169     Position start() const { return m_selection.start(); }
170     Position end() const { return m_selection.end(); }
171
172     // Return the renderer that is responsible for painting the caret (in the selection start node)
173     RenderObject* caretRenderer() const;
174
175     // Caret rect local to the caret's renderer
176     LayoutRect localCaretRect();
177
178     // Bounds of (possibly transformed) caret in absolute coords
179     LayoutRect absoluteCaretBounds();
180     void setCaretRectNeedsUpdate() { CaretBase::setCaretRectNeedsUpdate(); }
181
182     void willBeModified(EAlteration, SelectionDirection);
183
184     bool isNone() const { return m_selection.isNone(); }
185     bool isCaret() const { return m_selection.isCaret(); }
186     bool isRange() const { return m_selection.isRange(); }
187     bool isCaretOrRange() const { return m_selection.isCaretOrRange(); }
188     bool isInPasswordField() const;
189     bool isAll(EditingBoundaryCrossingRule rule = CannotCrossEditingBoundary) const { return m_selection.isAll(rule); }
190     
191     PassRefPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); }
192
193     void debugRenderer(RenderObject*, bool selected) const;
194
195     void nodeWillBeRemoved(Node*);
196     void textWillBeReplaced(CharacterData*, unsigned offset, unsigned oldLength, unsigned newLength);
197
198     void setCaretVisible(bool caretIsVisible) { setCaretVisibility(caretIsVisible ? Visible : Hidden); }
199     void clearCaretRectIfNeeded();
200     bool recomputeCaretRect();
201     void invalidateCaretRect();
202     void paintCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect);
203
204     // Used to suspend caret blinking while the mouse is down.
205     void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; }
206     bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; }
207
208     // Focus
209     void setFocused(bool);
210     bool isFocused() const { return m_focused; }
211     bool isFocusedAndActive() const;
212     void pageActivationChanged();
213
214     // Painting.
215     void updateAppearance();
216
217     void updateSecureKeyboardEntryIfActive();
218
219 #ifndef NDEBUG
220     void formatForDebugger(char* buffer, unsigned length) const;
221     void showTreeForThis() const;
222 #endif
223
224     bool shouldChangeSelection(const VisibleSelection&) const;
225     bool shouldDeleteSelection(const VisibleSelection&) const;
226     void setNonDirectionalSelectionIfNeeded(const VisibleSelection&, TextGranularity);
227     void setFocusedNodeIfNeeded();
228     void notifyRendererOfSelectionChange(EUserTriggered);
229
230     void paintDragCaret(GraphicsContext*, const LayoutPoint&, const LayoutRect& clipRect) const;
231
232     EditingStyle* typingStyle() const;
233     PassRefPtr<CSSMutableStyleDeclaration> copyTypingStyle() const;
234     void setTypingStyle(PassRefPtr<EditingStyle>);
235     void clearTypingStyle();
236
237     FloatRect bounds(bool clipToVisibleContent = true) const;
238
239     void getClippedVisibleTextRectangles(Vector<FloatRect>&) const;
240
241     HTMLFormElement* currentForm() const;
242
243     void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, bool revealExtent = false);
244     void setSelectionFromNone();
245
246 private:
247     enum EPositionType { START, END, BASE, EXTENT };
248
249     void respondToNodeModification(Node*, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved);
250     TextDirection directionOfEnclosingBlock();
251     TextDirection directionOfSelection();
252
253     VisiblePosition positionForPlatform(bool isGetStart) const;
254     VisiblePosition startForPlatform() const;
255     VisiblePosition endForPlatform() const;
256
257     VisiblePosition modifyExtendingRight(TextGranularity);
258     VisiblePosition modifyExtendingForward(TextGranularity);
259     VisiblePosition modifyMovingRight(TextGranularity);
260     VisiblePosition modifyMovingForward(TextGranularity);
261     VisiblePosition modifyExtendingLeft(TextGranularity);
262     VisiblePosition modifyExtendingBackward(TextGranularity);
263     VisiblePosition modifyMovingLeft(TextGranularity);
264     VisiblePosition modifyMovingBackward(TextGranularity);
265
266     LayoutUnit lineDirectionPointForBlockDirectionNavigation(EPositionType);
267     
268     void notifyAccessibilityForSelectionChange();
269
270     void focusedOrActiveStateChanged();
271
272     void caretBlinkTimerFired(Timer<FrameSelection>*);
273
274     void setUseSecureKeyboardEntry(bool);
275
276     void setCaretVisibility(CaretVisibility);
277
278     bool dispatchSelectStart();
279
280     Frame* m_frame;
281
282     LayoutUnit m_xPosForVerticalArrowNavigation;
283
284     VisibleSelection m_selection;
285     TextGranularity m_granularity;
286
287     RefPtr<EditingStyle> m_typingStyle;
288
289     Timer<FrameSelection> m_caretBlinkTimer;
290     LayoutRect m_absCaretBounds; // absolute bounding rect for the caret
291     LayoutRect m_absoluteCaretRepaintBounds;
292     bool m_absCaretBoundsDirty : 1;
293     bool m_caretPaint : 1;
294     bool m_isCaretBlinkingSuspended : 1;
295     bool m_focused : 1;
296 };
297
298 inline EditingStyle* FrameSelection::typingStyle() const
299 {
300     return m_typingStyle.get();
301 }
302
303 inline void FrameSelection::clearTypingStyle()
304 {
305     m_typingStyle.clear();
306 }
307
308 inline void FrameSelection::setTypingStyle(PassRefPtr<EditingStyle> style)
309 {
310     m_typingStyle = style;
311 }
312
313 #if !(PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(CHROMIUM))
314 inline void FrameSelection::notifyAccessibilityForSelectionChange()
315 {
316 }
317 #endif
318
319 } // namespace WebCore
320
321 #ifndef NDEBUG
322 // Outside the WebCore namespace for ease of invocation from gdb.
323 void showTree(const WebCore::FrameSelection&);
324 void showTree(const WebCore::FrameSelection*);
325 #endif
326
327 #endif // FrameSelection_h
328