1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=78: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef SelectionCarets_h__
8 #define SelectionCarets_h__
10 #include "nsIScrollObserver.h"
11 #include "nsISelectionListener.h"
12 #include "nsWeakPtr.h"
13 #include "nsWeakReference.h"
15 #include "mozilla/EventForwards.h"
28 * The SelectionCarets draw a pair of carets when the selection is not
29 * collapsed, one at each end of the selection.
30 * SelectionCarets also handle visibility, dragging caret and selecting word
31 * when long tap event fired.
33 * The DOM structure is 2 div elements for showing start and end caret.
35 * Here is an explanation of the html class names:
36 * .moz-selectioncaret-left: Indicates start DIV.
37 * .moz-selectioncaret-right: Indicates end DIV.
38 * .hidden: This class name is set by SetVisibility,
39 * SetStartFrameVisibility and SetEndFrameVisibility. Element
40 * with this class name become hidden.
41 * .tilt: This class name is set by SetTilted. According to the
42 * UX spec, when selection contains only one characters, the image of
45 class SelectionCarets MOZ_FINAL
: public nsISelectionListener
,
46 public nsIScrollObserver
,
47 public nsSupportsWeakReference
51 * Indicate which part of caret we are dragging at.
59 explicit SelectionCarets(nsIPresShell
*aPresShell
);
62 NS_DECL_NSISELECTIONLISTENER
65 virtual void ScrollPositionChanged() MOZ_OVERRIDE
;
72 nsEventStatus
HandleEvent(WidgetEvent
* aEvent
);
75 * Set visibility for selection caret.
77 void SetVisibility(bool aVisible
);
79 bool GetVisibility() const
85 * Get from pref "selectioncaret.inflatesize.threshold". This will inflate size of
86 * caret frame when we checking if user click on caret or not. In app units.
88 static int32_t SelectionCaretsInflateSize()
90 return sSelectionCaretsInflateSize
;
94 virtual ~SelectionCarets();
96 SelectionCarets() MOZ_DELETE
;
99 * Update selection caret position base on current selection range.
101 void UpdateSelectionCarets();
104 * Select word base on current position, only active when element
105 * is focused. Triggered by long tap event.
107 nsresult
SelectWord();
110 * Move selection base on current touch/mouse point
112 nsEventStatus
DragSelection(const nsPoint
&movePoint
);
115 * Get the vertical center position of selection caret relative to canvas
118 nscoord
GetCaretYCenterPosition();
121 * Simulate drag state when we change the selection range.
122 * Hence, the selection change event will fire normally.
124 void SetSelectionDragState(bool aState
);
126 void SetSelectionDirection(bool aForward
);
129 * Move start frame of selection caret to given position.
132 void SetStartFramePos(const nsPoint
& aPosition
);
135 * Move end frame of selection caret to given position.
138 void SetEndFramePos(const nsPoint
& aPosition
);
141 * Get rect of selection caret's start frame relative
142 * to document's canvas frame, in app units.
144 nsRect
GetStartFrameRect();
147 * Get rect of selection caret's end frame relative
148 * to document's canvas frame, in app units.
150 nsRect
GetEndFrameRect();
153 * Set visibility for start part of selection caret, this function
154 * only affects css property of start frame. So it doesn't change
155 * mVisible member. When caret overflows element's box we'll hide
156 * it by calling this function.
158 void SetStartFrameVisibility(bool aVisible
);
161 * Same as above function but for end frame of selection caret.
163 void SetEndFrameVisibility(bool aVisible
);
166 * Set tilt class name to start and end frame of selection caret.
168 void SetTilted(bool aIsTilt
);
171 nsIFrame
* GetCaretFocusFrame();
172 bool GetCaretVisible();
173 nsISelection
* GetSelection();
176 * Detecting long tap using timer
178 void LaunchLongTapDetector();
179 void CancelLongTapDetector();
180 static void FireLongTap(nsITimer
* aTimer
, void* aSelectionCarets
);
182 void LaunchScrollEndDetector();
183 static void FireScrollEnd(nsITimer
* aTimer
, void* aSelectionCarets
);
185 nsIPresShell
* mPresShell
;
187 // This timer is used for detecting long tap fire. If content process
188 // has APZC, we'll use APZC for long tap detecting. Otherwise, we use this
189 // timer to detect long tap.
190 nsCOMPtr
<nsITimer
> mLongTapDetectorTimer
;
192 // This timer is used for detecting scroll end. We don't have
193 // scroll end event now, so we will fire this event with a
194 // const time when we scroll. So when timer triggers, we treat it
195 // as scroll end event.
196 nsCOMPtr
<nsITimer
> mScrollEndDetectorTimer
;
198 // When touch or mouse down, we save the position for detecting
202 // For filter multitouch event
203 int32_t mActiveTouchId
;
205 nscoord mCaretCenterToDownPointOffsetY
;
208 bool mStartCaretVisible
;
209 bool mEndCaretVisible
;
212 static int32_t sSelectionCaretsInflateSize
;
214 } // namespace mozilla
216 #endif //SelectionCarets_h__