~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/editing/SelectionController.h

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004 Apple Computer, 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 "IntRect.h"
 
30
#include "Selection.h"
 
31
#include "Range.h"
 
32
#include <wtf/Noncopyable.h>
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
class Frame;
 
37
class GraphicsContext;
 
38
class RenderObject;
 
39
class VisiblePosition;
 
40
 
 
41
class SelectionController : Noncopyable {
 
42
public:
 
43
    enum EAlteration { MOVE, EXTEND };
 
44
    enum EDirection { FORWARD, BACKWARD, RIGHT, LEFT };
 
45
 
 
46
    SelectionController(Frame* = 0, bool isDragCaretController = false);
 
47
 
 
48
    Element* rootEditableElement() const { return m_sel.rootEditableElement(); }
 
49
    bool isContentEditable() const { return m_sel.isContentEditable(); }
 
50
    bool isContentRichlyEditable() const { return m_sel.isContentRichlyEditable(); }
 
51
 
 
52
    void moveTo(const Range*, EAffinity, bool userTriggered = false);
 
53
    void moveTo(const VisiblePosition&, bool userTriggered = false);
 
54
    void moveTo(const VisiblePosition&, const VisiblePosition&, bool userTriggered = false);
 
55
    void moveTo(const Position&, EAffinity, bool userTriggered = false);
 
56
    void moveTo(const Position&, const Position&, EAffinity, bool userTriggered = false);
 
57
 
 
58
    const Selection& selection() const { return m_sel; }
 
59
    void setSelection(const Selection&, bool closeTyping = true, bool clearTypingStyle = true, bool userTriggered = false);
 
60
    void setSelectedRange(Range*, EAffinity, bool closeTyping, ExceptionCode&);
 
61
    void selectAll();
 
62
    void clear();
 
63
    
 
64
    // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected.
 
65
    void selectFrameElementInParentIfFullySelected();
 
66
 
 
67
    bool contains(const IntPoint&);
 
68
 
 
69
    Selection::EState state() const { return m_sel.state(); }
 
70
 
 
71
    EAffinity affinity() const { return m_sel.affinity(); }
 
72
 
 
73
    bool modify(EAlteration, EDirection, TextGranularity, bool userTriggered = false);
 
74
    bool modify(EAlteration, int verticalDistance, bool userTriggered = false);
 
75
    bool expandUsingGranularity(TextGranularity);
 
76
 
 
77
    void setBase(const VisiblePosition&, bool userTriggered = false);
 
78
    void setBase(const Position&, EAffinity, bool userTriggered = false);
 
79
    void setExtent(const VisiblePosition&, bool userTriggered = false);
 
80
    void setExtent(const Position&, EAffinity, bool userTriggered = false);
 
81
 
 
82
    Position base() const { return m_sel.base(); }
 
83
    Position extent() const { return m_sel.extent(); }
 
84
    Position start() const { return m_sel.start(); }
 
85
    Position end() const { return m_sel.end(); }
 
86
 
 
87
    IntRect caretRect() const;
 
88
    void setNeedsLayout(bool flag = true);
 
89
 
 
90
    void setLastChangeWasHorizontalExtension(bool b) { m_lastChangeWasHorizontalExtension = b; }
 
91
    void willBeModified(EAlteration, EDirection);
 
92
    
 
93
    bool isNone() const { return m_sel.isNone(); }
 
94
    bool isCaret() const { return m_sel.isCaret(); }
 
95
    bool isRange() const { return m_sel.isRange(); }
 
96
    bool isCaretOrRange() const { return m_sel.isCaretOrRange(); }
 
97
    bool isInPasswordField() const;
 
98
    bool isInsideNode() const;
 
99
    
 
100
    PassRefPtr<Range> toRange() const { return m_sel.toRange(); }
 
101
 
 
102
    void debugRenderer(RenderObject*, bool selected) const;
 
103
    
 
104
    void nodeWillBeRemoved(Node*);
 
105
 
 
106
    // Safari Selection Object API
 
107
    // These methods return the valid equivalents of internal editing positions.
 
108
    Node* baseNode() const;
 
109
    Node* extentNode() const;
 
110
    int baseOffset() const;
 
111
    int extentOffset() const;
 
112
    String type() const;
 
113
    void setBaseAndExtent(Node* baseNode, int baseOffset, Node* extentNode, int extentOffset, ExceptionCode&);
 
114
    void setPosition(Node*, int offset, ExceptionCode&);
 
115
    bool modify(const String& alterString, const String& directionString, const String& granularityString, bool userTriggered = false);
 
116
    
 
117
    // Mozilla Selection Object API
 
118
    // In FireFox, anchor/focus are the equal to the start/end of the selection,
 
119
    // but reflect the direction in which the selection was made by the user.  That does
 
120
    // not mean that they are base/extent, since the base/extent don't reflect
 
121
    // expansion.
 
122
    // These methods return the valid equivalents of internal editing positions.
 
123
    Node* anchorNode() const;
 
124
    int anchorOffset() const;
 
125
    Node* focusNode() const;
 
126
    int focusOffset() const;
 
127
    bool isCollapsed() const { return !isRange(); }
 
128
    String toString() const;
 
129
    void collapse(Node*, int offset, ExceptionCode&);
 
130
    void collapseToEnd();
 
131
    void collapseToStart();
 
132
    void extend(Node*, int offset, ExceptionCode&);
 
133
    PassRefPtr<Range> getRangeAt(int index, ExceptionCode&) const;
 
134
    int rangeCount() const { return !isNone() ? 1 : 0; }
 
135
    void removeAllRanges();
 
136
    void addRange(const Range*);
 
137
    //void deleteFromDocument();
 
138
    //bool containsNode(Node *node, bool entirelyContained);
 
139
    //void selectAllChildren(const Node *);
 
140
    //void removeRange(const Range *);
 
141
    
 
142
    // Microsoft Selection Object API
 
143
    void empty();
 
144
    //void clear();
 
145
    //TextRange *createRange();
 
146
    
 
147
    bool recomputeCaretRect(); // returns true if caret rect moved
 
148
    void invalidateCaretRect();
 
149
    void paintCaret(GraphicsContext*, const IntRect&);
 
150
 
 
151
    // Used to suspend caret blinking while the mouse is down.
 
152
    void setCaretBlinkingSuspended(bool suspended) { m_isCaretBlinkingSuspended = suspended; }
 
153
    bool isCaretBlinkingSuspended() const { return m_isCaretBlinkingSuspended; }
 
154
 
 
155
#ifndef NDEBUG
 
156
    void formatForDebugger(char* buffer, unsigned length) const;
 
157
    void showTreeForThis() const;
 
158
#endif
 
159
 
 
160
private:
 
161
    enum EPositionType { START, END, BASE, EXTENT };
 
162
 
 
163
    VisiblePosition modifyExtendingRightForward(TextGranularity);
 
164
    VisiblePosition modifyMovingRightForward(TextGranularity);
 
165
    VisiblePosition modifyExtendingLeftBackward(TextGranularity);
 
166
    VisiblePosition modifyMovingLeftBackward(TextGranularity);
 
167
 
 
168
    void layout();
 
169
    IntRect caretRepaintRect() const;
 
170
 
 
171
    int xPosForVerticalArrowNavigation(EPositionType);
 
172
    
 
173
#if PLATFORM(MAC)
 
174
    void notifyAccessibilityForSelectionChange();
 
175
#else
 
176
    void notifyAccessibilityForSelectionChange() {};
 
177
#endif
 
178
 
 
179
    Selection m_sel;
 
180
 
 
181
    IntRect m_caretRect;            // caret coordinates, size, and position
 
182
    
 
183
    // m_caretPositionOnLayout stores the scroll offset on the previous call to SelectionController::layout().
 
184
    // When asked for caretRect(), we correct m_caretRect for offset due to scrolling since the last layout().
 
185
    // This is faster than doing another layout().
 
186
    IntPoint m_caretPositionOnLayout;
 
187
    
 
188
    bool m_needsLayout : 1;       // true if the caret and expectedVisible rectangles need to be calculated
 
189
    bool m_lastChangeWasHorizontalExtension : 1;
 
190
    Frame* m_frame;
 
191
    bool m_isDragCaretController;
 
192
 
 
193
    bool m_isCaretBlinkingSuspended;
 
194
    
 
195
    int m_xPosForVerticalArrowNavigation;
 
196
};
 
197
 
 
198
inline bool operator==(const SelectionController& a, const SelectionController& b)
 
199
{
 
200
    return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity();
 
201
}
 
202
 
 
203
inline bool operator!=(const SelectionController& a, const SelectionController& b)
 
204
{
 
205
    return !(a == b);
 
206
}
 
207
 
 
208
} // namespace WebCore
 
209
 
 
210
#ifndef NDEBUG
 
211
// Outside the WebCore namespace for ease of invocation from gdb.
 
212
void showTree(const WebCore::SelectionController&);
 
213
void showTree(const WebCore::SelectionController*);
 
214
#endif
 
215
 
 
216
#endif // SelectionController_h