Bumping manifests a=b2g-bump
[gecko.git] / layout / base / SelectionCarets.h
blob42163623868135c715dc2d370657d5001cfd0c04
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"
14 #include "Units.h"
15 #include "mozilla/EventForwards.h"
17 class nsCanvasFrame;
18 class nsIDocument;
19 class nsIFrame;
20 class nsIPresShell;
21 class nsITimer;
22 class nsIWidget;
23 class nsPresContext;
25 namespace mozilla {
27 /**
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
43 * caret becomes tilt.
45 class SelectionCarets MOZ_FINAL : public nsISelectionListener,
46 public nsIScrollObserver,
47 public nsSupportsWeakReference
49 public:
50 /**
51 * Indicate which part of caret we are dragging at.
53 enum DragMode {
54 NONE,
55 START_FRAME,
56 END_FRAME
59 explicit SelectionCarets(nsIPresShell *aPresShell);
61 NS_DECL_ISUPPORTS
62 NS_DECL_NSISELECTIONLISTENER
64 // nsIScrollObserver
65 virtual void ScrollPositionChanged() MOZ_OVERRIDE;
67 void Terminate()
69 mPresShell = nullptr;
72 nsEventStatus HandleEvent(WidgetEvent* aEvent);
74 /**
75 * Set visibility for selection caret.
77 void SetVisibility(bool aVisible);
79 bool GetVisibility() const
81 return mVisible;
84 /**
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;
93 private:
94 virtual ~SelectionCarets();
96 SelectionCarets() MOZ_DELETE;
98 /**
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
116 * frame.
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.
130 * In app units.
132 void SetStartFramePos(const nsPoint& aPosition);
135 * Move end frame of selection caret to given position.
136 * In app units.
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);
170 // Utility function
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
199 // drag distance
200 nsPoint mDownPoint;
202 // For filter multitouch event
203 int32_t mActiveTouchId;
205 nscoord mCaretCenterToDownPointOffsetY;
206 DragMode mDragMode;
207 bool mVisible;
208 bool mStartCaretVisible;
209 bool mEndCaretVisible;
211 // Preference
212 static int32_t sSelectionCaretsInflateSize;
214 } // namespace mozilla
216 #endif //SelectionCarets_h__