OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / editing / SelectionController.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 SelectionController_h
27 #define SelectionController_h
28
29 #include "EditingStyle.h"
30 #include "IntRect.h"
31 #include "Range.h"
32 #include "ScrollBehavior.h"
33 #include "Timer.h"
34 #include "VisibleSelection.h"
35 #include <wtf/Noncopyable.h>
36
37 namespace WebCore {
38
39 class Frame;
40 class CSSMutableStyleDeclaration;
41 class GraphicsContext;
42 class HTMLFormElement;
43 class RenderObject;
44 class RenderView;
45 class Settings;
46 class VisiblePosition;
47
48 enum DirectionalityPolicy { MakeNonDirectionalSelection, MakeDirectionalSelection };
49
50 class SelectionController : public Noncopyable {
51 public:
52     enum EAlteration { AlterationMove, AlterationExtend };
53     enum EDirection { DirectionForward, DirectionBackward, DirectionRight, DirectionLeft };
54     enum CursorAlignOnScroll { AlignCursorOnScrollIfNeeded,
55                                AlignCursorOnScrollAlways };
56
57     SelectionController(Frame* = 0, bool isDragCaretController = false);
58
59     Element* rootEditableElement() const { return m_selection.rootEditableElement(); }
60     bool isContentEditable() const { return m_selection.isContentEditable(); }
61     bool isContentRichlyEditable() const { return m_selection.isContentRichlyEditable(); }
62     Node* shadowTreeRootNode() const { return m_selection.shadowTreeRootNode(); }
63      
64     void moveTo(const Range*, EAffinity, bool userTriggered = false);
65     void moveTo(const VisiblePosition&, bool userTriggered = false, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
66     void moveTo(const VisiblePosition&, const VisiblePosition&, bool userTriggered = false);
67     void moveTo(const Position&, EAffinity, bool userTriggered = false);
68     void moveTo(const Position&, const Position&, EAffinity, bool userTriggered = false);
69
70     const VisibleSelection& selection() const { return m_selection; }
71     void setSelection(const VisibleSelection&, bool closeTyping = true, bool clearTypingStyle = true, bool userTriggered = false, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded, TextGranularity = CharacterGranularity, DirectionalityPolicy = MakeDirectionalSelection);
72     void setSelection(const VisibleSelection& selection, TextGranularity granularity, DirectionalityPolicy directionality = MakeDirectionalSelection) { setSelection(selection, true, true, false, AlignCursorOnScrollIfNeeded, granularity, directionality); }
73     bool setSelectedRange(Range*, EAffinity, bool closeTyping);
74     void selectAll();
75     void clear();
76     
77     // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
78     void selectFrameElementInParentIfFullySelected();
79
80     bool contains(const IntPoint&);
81
82     VisibleSelection::SelectionType selectionType() const { return m_selection.selectionType(); }
83
84     EAffinity affinity() const { return m_selection.affinity(); }
85
86     bool modify(EAlteration, EDirection, TextGranularity, bool userTriggered = false);
87     bool modify(EAlteration, int verticalDistance, bool userTriggered = false, CursorAlignOnScroll = AlignCursorOnScrollIfNeeded);
88     TextGranularity granularity() const { return m_granularity; }
89
90     void setStart(const VisiblePosition &, bool userTriggered = false);
91     void setEnd(const VisiblePosition &, bool userTriggered = false);
92     
93     void setBase(const VisiblePosition&, bool userTriggered = false);
94     void setBase(const Position&, EAffinity, bool userTriggered = false);
95     void setExtent(const VisiblePosition&, bool userTriggered = false);
96     void setExtent(const Position&, EAffinity, bool userTriggered = false);
97
98     Position base() const { return m_selection.base(); }
99     Position extent() const { return m_selection.extent(); }
100     Position start() const { return m_selection.start(); }
101     Position end() const { return m_selection.end(); }
102
103     // Return the renderer that is responsible for painting the caret (in the selection start node)
104     RenderObject* caretRenderer() const;
105
106     // Caret rect local to the caret's renderer
107     IntRect localCaretRect();
108     IntRect localCaretRectForPainting() const { return m_caretRect; }
109
110     // Bounds of (possibly transformed) caret in absolute coords
111     IntRect absoluteCaretBounds();
112     void setCaretRectNeedsUpdate(bool flag = true);
113
114     void setIsDirectional(bool);
115     void willBeModified(EAlteration, EDirection);
116     
117     bool isNone() const { return m_selection.isNone(); }
118     bool isCaret() const { return m_selection.isCaret(); }
119     bool isRange() const { return m_selection.isRange(); }
120     bool isCaretOrRange() const { return m_selection.isCaretOrRange(); }
121     bool isInPasswordField() const;
122     bool isAll(StayInEditableContent stayInEditableContent = MustStayInEditableContent) const { return m_selection.isAll(stayInEditableContent); }
123     
124     PassRefPtr<Range> toNormalizedRange() const { return m_selection.toNormalizedRange(); }
125
126     void debugRenderer(RenderObject*, bool selected) const;
127     
128     void nodeWillBeRemoved(Node*);
129
130     void setCaretVisible(bool = true);
131     void clearCaretRectIfNeeded();
132     bool recomputeCaretRect(); // returns true if caret rect moved
133     void invalidateCaretRect();
134     void paintCaret(GraphicsContext*, int tx, int ty, const IntRect& clipRect);
135
136     // Used to suspend caret blinking while the mouse is down.
137     void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; }
138     bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; }
139
140     // Focus
141     void setFocused(bool);
142     bool isFocused() const { return m_focused; }
143     bool isFocusedAndActive() const;
144     void pageActivationChanged();
145
146     // Painting.
147     void updateAppearance();
148
149     void updateSecureKeyboardEntryIfActive();
150
151 #ifndef NDEBUG
152     void formatForDebugger(char* buffer, unsigned length) const;
153     void showTreeForThis() const;
154 #endif
155
156     bool shouldChangeSelection(const VisibleSelection&) const;
157     bool shouldDeleteSelection(const VisibleSelection&) const;
158     void setFocusedNodeIfNeeded();
159     void notifyRendererOfSelectionChange(bool userTriggered);
160
161     void paintDragCaret(GraphicsContext*, int tx, int ty, const IntRect& clipRect) const;
162
163     EditingStyle* typingStyle() const;
164     PassRefPtr<CSSMutableStyleDeclaration> copyTypingStyle() const;
165     void setTypingStyle(PassRefPtr<EditingStyle>);
166     void clearTypingStyle();
167
168     FloatRect bounds(bool clipToVisibleContent = true) const;
169
170     void getClippedVisibleTextRectangles(Vector<FloatRect>&) const;
171
172     HTMLFormElement* currentForm() const;
173
174     void revealSelection(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, bool revealExtent = false);
175     void setSelectionFromNone();
176
177 private:
178     enum EPositionType { START, END, BASE, EXTENT };
179
180     TextDirection directionOfEnclosingBlock();
181
182     VisiblePosition positionForPlatform(bool isGetStart) const;
183     VisiblePosition startForPlatform() const;
184     VisiblePosition endForPlatform() const;
185
186     VisiblePosition modifyExtendingRight(TextGranularity);
187     VisiblePosition modifyExtendingForward(TextGranularity);
188     VisiblePosition modifyMovingRight(TextGranularity);
189     VisiblePosition modifyMovingForward(TextGranularity);
190     VisiblePosition modifyExtendingLeft(TextGranularity);
191     VisiblePosition modifyExtendingBackward(TextGranularity);
192     VisiblePosition modifyMovingLeft(TextGranularity);
193     VisiblePosition modifyMovingBackward(TextGranularity);
194
195     void updateCaretRect();
196     IntRect caretRepaintRect() const;
197     bool shouldRepaintCaret(const RenderView* view) const;
198
199     int xPosForVerticalArrowNavigation(EPositionType);
200     
201     void notifyAccessibilityForSelectionChange();
202
203     void focusedOrActiveStateChanged();
204     bool caretRendersInsideNode(Node*) const;
205     
206     IntRect absoluteBoundsForLocalRect(const IntRect&) const;
207
208     void caretBlinkTimerFired(Timer<SelectionController>*);
209
210     void setUseSecureKeyboardEntry(bool);
211
212     Frame* m_frame;
213
214     int m_xPosForVerticalArrowNavigation;
215
216     VisibleSelection m_selection;
217     TextGranularity m_granularity;
218
219     RefPtr<EditingStyle> m_typingStyle;
220
221     Timer<SelectionController> m_caretBlinkTimer;
222
223     IntRect m_caretRect; // caret rect in coords local to the renderer responsible for painting the caret
224     IntRect m_absCaretBounds; // absolute bounding rect for the caret
225     IntRect m_absoluteCaretRepaintBounds;
226     
227     bool m_caretRectNeedsUpdate; // true if m_caretRect and m_absCaretBounds need to be calculated
228     bool m_absCaretBoundsDirty;
229     bool m_isDirectional;
230     bool m_isDragCaretController;
231     bool m_isCaretBlinkingSuspended;
232     bool m_focused;
233     bool m_caretVisible;
234     bool m_caretPaint;
235 };
236
237 inline EditingStyle* SelectionController::typingStyle() const
238 {
239     return m_typingStyle.get();
240 }
241
242 inline PassRefPtr<CSSMutableStyleDeclaration> SelectionController::copyTypingStyle() const
243 {
244     if (!m_typingStyle || !m_typingStyle->style())
245         return 0;
246     return m_typingStyle->style()->copy();
247 }
248
249 inline void SelectionController::clearTypingStyle()
250 {
251     m_typingStyle.clear();
252 }
253
254 inline void SelectionController::setTypingStyle(PassRefPtr<EditingStyle> style)
255 {
256     m_typingStyle = style;
257 }
258
259 #if !(PLATFORM(MAC) || PLATFORM(GTK) || PLATFORM(CHROMIUM))
260 inline void SelectionController::notifyAccessibilityForSelectionChange()
261 {
262 }
263 #endif
264
265 } // namespace WebCore
266
267 #ifndef NDEBUG
268 // Outside the WebCore namespace for ease of invocation from gdb.
269 void showTree(const WebCore::SelectionController&);
270 void showTree(const WebCore::SelectionController*);
271 #endif
272
273 #endif // SelectionController_h
274