forked from mozilla/mozilla-central
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection.h
224 lines (192 loc) · 9.05 KB
/
Selection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_Selection_h__
#define mozilla_Selection_h__
#include "nsIWeakReference.h"
#include "nsISelection.h"
#include "nsISelectionController.h"
#include "nsISelectionPrivate.h"
#include "nsRange.h"
struct CachedOffsetForFrame;
class nsAutoScrollTimer;
class nsIContentIterator;
class nsIFrame;
struct SelectionDetails;
struct RangeData
{
RangeData(nsRange* aRange)
: mRange(aRange)
{}
nsRefPtr<nsRange> mRange;
nsTextRangeStyle mTextRangeStyle;
};
// Note, the ownership of mozilla::Selection depends on which way the object is
// created. When nsFrameSelection has created Selection, addreffing/releasing
// the Selection object is aggregated to nsFrameSelection. Otherwise normal
// addref/release is used. This ensures that nsFrameSelection is never deleted
// before its Selections.
namespace mozilla {
class Selection : public nsISelectionPrivate,
public nsSupportsWeakReference
{
public:
Selection();
Selection(nsFrameSelection *aList);
virtual ~Selection();
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(Selection, nsISelectionPrivate)
NS_DECL_NSISELECTION
NS_DECL_NSISELECTIONPRIVATE
// utility methods for scrolling the selection into view
nsPresContext* GetPresContext() const;
nsIPresShell* GetPresShell() const;
nsFrameSelection* GetFrameSelection() const { return mFrameSelection; }
// Returns a rect containing the selection region, and frame that that
// position is relative to. For SELECTION_ANCHOR_REGION or
// SELECTION_FOCUS_REGION the rect is a zero-width rectangle. For
// SELECTION_WHOLE_SELECTION the rect contains both the anchor and focus
// region rects.
nsIFrame* GetSelectionAnchorGeometry(SelectionRegion aRegion, nsRect *aRect);
// Returns the position of the region (SELECTION_ANCHOR_REGION or
// SELECTION_FOCUS_REGION only), and frame that that position is relative to.
// The 'position' is a zero-width rectangle.
nsIFrame* GetSelectionEndPointGeometry(SelectionRegion aRegion, nsRect *aRect);
nsresult PostScrollSelectionIntoViewEvent(
SelectionRegion aRegion,
int32_t aFlags,
nsIPresShell::ScrollAxis aVertical,
nsIPresShell::ScrollAxis aHorizontal);
enum {
SCROLL_SYNCHRONOUS = 1<<1,
SCROLL_FIRST_ANCESTOR_ONLY = 1<<2,
SCROLL_DO_FLUSH = 1<<3,
SCROLL_OVERFLOW_HIDDEN = 1<<5
};
// aDoFlush only matters if aIsSynchronous is true. If not, we'll just flush
// when the scroll event fires so we make sure to scroll to the right place.
nsresult ScrollIntoView(SelectionRegion aRegion,
nsIPresShell::ScrollAxis aVertical =
nsIPresShell::ScrollAxis(),
nsIPresShell::ScrollAxis aHorizontal =
nsIPresShell::ScrollAxis(),
int32_t aFlags = 0);
nsresult SubtractRange(RangeData* aRange, nsRange* aSubtract,
nsTArray<RangeData>* aOutput);
nsresult AddItem(nsRange *aRange, int32_t* aOutIndex);
nsresult RemoveItem(nsRange *aRange);
nsresult RemoveCollapsedRanges();
nsresult Clear(nsPresContext* aPresContext);
nsresult Collapse(nsINode* aParentNode, int32_t aOffset);
nsresult Extend(nsINode* aParentNode, int32_t aOffset);
nsRange* GetRangeAt(int32_t aIndex);
int32_t GetRangeCount() { return mRanges.Length(); }
// methods for convenience. Note, these don't addref
nsINode* GetAnchorNode();
int32_t GetAnchorOffset();
nsINode* GetFocusNode();
int32_t GetFocusOffset();
bool IsCollapsed();
// Get the anchor-to-focus range if we don't care which end is
// anchor and which end is focus.
const nsRange* GetAnchorFocusRange() const {
return mAnchorFocusRange;
}
nsDirection GetDirection(){return mDirection;}
void SetDirection(nsDirection aDir){mDirection = aDir;}
nsresult SetAnchorFocusToRange(nsRange *aRange);
void ReplaceAnchorFocusRange(nsRange *aRange);
// NS_IMETHOD GetPrimaryFrameForRangeEndpoint(nsIDOMNode *aNode, int32_t aOffset, bool aIsEndNode, nsIFrame **aResultFrame);
NS_IMETHOD GetPrimaryFrameForAnchorNode(nsIFrame **aResultFrame);
NS_IMETHOD GetPrimaryFrameForFocusNode(nsIFrame **aResultFrame, int32_t *aOffset, bool aVisual);
NS_IMETHOD LookUpSelection(nsIContent *aContent, int32_t aContentOffset, int32_t aContentLength,
SelectionDetails **aReturnDetails, SelectionType aType, bool aSlowCheck);
NS_IMETHOD Repaint(nsPresContext* aPresContext);
// Note: StartAutoScrollTimer might destroy arbitrary frames etc.
nsresult StartAutoScrollTimer(nsIFrame *aFrame,
nsPoint& aPoint,
uint32_t aDelay);
nsresult StopAutoScrollTimer();
private:
friend class ::nsAutoScrollTimer;
// Note: DoAutoScroll might destroy arbitrary frames etc.
nsresult DoAutoScroll(nsIFrame *aFrame, nsPoint& aPoint);
public:
SelectionType GetType(){return mType;}
void SetType(SelectionType aType){mType = aType;}
nsresult NotifySelectionListeners();
private:
class ScrollSelectionIntoViewEvent;
friend class ScrollSelectionIntoViewEvent;
class ScrollSelectionIntoViewEvent : public nsRunnable {
public:
NS_DECL_NSIRUNNABLE
ScrollSelectionIntoViewEvent(Selection* aSelection,
SelectionRegion aRegion,
nsIPresShell::ScrollAxis aVertical,
nsIPresShell::ScrollAxis aHorizontal,
int32_t aFlags)
: mSelection(aSelection),
mRegion(aRegion),
mVerticalScroll(aVertical),
mHorizontalScroll(aHorizontal),
mFlags(aFlags) {
NS_ASSERTION(aSelection, "null parameter");
}
void Revoke() { mSelection = nullptr; }
private:
Selection *mSelection;
SelectionRegion mRegion;
nsIPresShell::ScrollAxis mVerticalScroll;
nsIPresShell::ScrollAxis mHorizontalScroll;
int32_t mFlags;
};
void setAnchorFocusRange(int32_t aIndex); // pass in index into mRanges;
// negative value clears
// mAnchorFocusRange
nsresult SelectAllFramesForContent(nsIContentIterator *aInnerIter,
nsIContent *aContent,
bool aSelected);
nsresult selectFrames(nsPresContext* aPresContext, nsRange *aRange, bool aSelect);
nsresult getTableCellLocationFromRange(nsRange *aRange, int32_t *aSelectionType, int32_t *aRow, int32_t *aCol);
nsresult addTableCellRange(nsRange *aRange, bool *aDidAddRange, int32_t *aOutIndex);
nsresult FindInsertionPoint(
nsTArray<RangeData>* aElementArray,
nsINode* aPointNode, int32_t aPointOffset,
nsresult (*aComparator)(nsINode*,int32_t,nsRange*,int32_t*),
int32_t* aPoint);
bool EqualsRangeAtPoint(nsINode* aBeginNode, int32_t aBeginOffset,
nsINode* aEndNode, int32_t aEndOffset,
int32_t aRangeIndex);
nsresult GetIndicesForInterval(nsINode* aBeginNode, int32_t aBeginOffset,
nsINode* aEndNode, int32_t aEndOffset,
bool aAllowAdjacent,
int32_t* aStartIndex, int32_t* aEndIndex);
RangeData* FindRangeData(nsIDOMRange* aRange);
// These are the ranges inside this selection. They are kept sorted in order
// of DOM start position.
//
// This data structure is sorted by the range beginnings. As the ranges are
// disjoint, it is also implicitly sorted by the range endings. This allows
// us to perform binary searches when searching for existence of a range,
// giving us O(log n) search time.
//
// Inserting a new range requires finding the overlapping interval, requiring
// two binary searches plus up to an additional 6 DOM comparisons. If this
// proves to be a performance concern, then an interval tree may be a
// possible solution, allowing the calculation of the overlap interval in
// O(log n) time, though this would require rebalancing and other overhead.
nsTArray<RangeData> mRanges;
nsRefPtr<nsRange> mAnchorFocusRange;
nsRefPtr<nsFrameSelection> mFrameSelection;
nsRefPtr<nsAutoScrollTimer> mAutoScrollTimer;
nsCOMArray<nsISelectionListener> mSelectionListeners;
nsRevocableEventPtr<ScrollSelectionIntoViewEvent> mScrollEvent;
CachedOffsetForFrame *mCachedOffsetForFrame;
nsDirection mDirection;
SelectionType mType;
};
} // namespace mozilla
#endif // mozilla_Selection_h__