1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 /* the caret is the text cursor used, e.g., when editing */
12 #include "mozilla/MemoryReporting.h"
13 #include "mozilla/dom/Selection.h"
15 #include "nsISelectionListener.h"
16 #include "nsIWeakReferenceUtils.h"
20 class nsFrameSelection
;
28 enum class CaretAssociationHint
;
32 } // namespace mozilla
34 //-----------------------------------------------------------------------------
35 class nsCaret final
: public nsISelectionListener
{
36 typedef mozilla::gfx::DrawTarget DrawTarget
;
47 using CaretAssociationHint
= mozilla::CaretAssociationHint
;
49 nsresult
Init(mozilla::PresShell
* aPresShell
);
52 void SetSelection(mozilla::dom::Selection
* aDOMSel
);
53 mozilla::dom::Selection
* GetSelection();
56 * Sets whether the caret should only be visible in nodes that are not
57 * user-modify: read-only, or whether it should be visible in all nodes.
59 * @param aIgnoreUserModify true to have the cursor visible in all nodes,
60 * false to have it visible in all nodes except
61 * those with user-modify: read-only
63 void SetIgnoreUserModify(bool aIgnoreUserModify
);
64 /** SetVisible will set the visibility of the caret
65 * @param inMakeVisible true to show the caret, false to hide it
67 void SetVisible(bool intMakeVisible
);
68 /** IsVisible will get the visibility of the caret.
69 * This returns false if the caret is hidden because it was set
70 * to not be visible, or because the selection is not collapsed, or
71 * because an open popup is hiding the caret.
72 * It does not take account of blinking or the caret being hidden
73 * because we're in non-editable/disabled content.
75 bool IsVisible(mozilla::dom::Selection
* aSelection
= nullptr) {
76 if (!mVisible
|| mHideCount
) {
80 if (!mShowDuringSelection
) {
81 mozilla::dom::Selection
* selection
;
83 selection
= aSelection
;
85 selection
= GetSelection();
87 if (!selection
|| !selection
->IsCollapsed()) {
92 if (IsMenuPopupHidingCaret()) {
99 * AddForceHide() increases mHideCount and hide the caret even if
100 * SetVisible(true) has been or will be called. This is useful when the
101 * caller wants to hide caret temporarily and it needs to cancel later.
102 * Especially, in the latter case, it's too difficult to decide if the
103 * caret should be actually visible or not because caret visible state
104 * is set from a lot of event handlers. So, it's very stateful.
108 * RemoveForceHide() decreases mHideCount if it's over 0.
109 * If the value becomes 0, this may show the caret if SetVisible(true)
112 void RemoveForceHide();
113 /** SetCaretReadOnly set the appearance of the caret
114 * @param inMakeReadonly true to show the caret in a 'read only' state,
115 * false to show the caret in normal, editing state
117 void SetCaretReadOnly(bool inMakeReadonly
);
119 * @param aVisibility true if the caret should be visible even when the
120 * selection is not collapsed.
122 void SetVisibilityDuringSelection(bool aVisibility
);
125 * Set the caret's position explicitly to the specified node and offset
126 * instead of tracking its selection.
127 * Passing null for aNode would set the caret to track its selection again.
129 void SetCaretPosition(nsINode
* aNode
, int32_t aOffset
);
132 * Schedule a repaint for the frame where the caret would appear.
133 * Does not check visibility etc.
135 void SchedulePaint(mozilla::dom::Selection
* aSelection
= nullptr);
138 * Returns a frame to paint in, and the bounds of the painted caret
139 * relative to that frame.
140 * The rectangle includes bidi decorations.
141 * Returns null if the caret should not be drawn (including if it's blinked
144 nsIFrame
* GetPaintGeometry(nsRect
* aRect
);
146 * Same as the overload above, but returns the caret and hook rects
147 * separately, and also computes the color if requested.
149 nsIFrame
* GetPaintGeometry(nsRect
* aCaretRect
, nsRect
* aHookRect
,
150 nscolor
* aCaretColor
= nullptr);
152 * A simple wrapper around GetGeometry. Does not take any caret state into
153 * account other than the current selection.
155 nsIFrame
* GetGeometry(nsRect
* aRect
) {
156 return GetGeometry(GetSelection(), aRect
);
160 * Actually paint the caret onto the given rendering context.
162 void PaintCaret(DrawTarget
& aDrawTarget
, nsIFrame
* aForFrame
,
163 const nsPoint
& aOffset
);
165 // nsISelectionListener interface
166 NS_DECL_NSISELECTIONLISTENER
169 * Gets the position and size of the caret that would be drawn for
170 * the focus node/offset of aSelection (assuming it would be drawn,
171 * i.e., disregarding blink status). The geometry is stored in aRect,
172 * and we return the frame aRect is relative to.
173 * Only looks at the focus node of aSelection, so you can call it even if
174 * aSelection is not collapsed.
175 * This rect does not include any extra decorations for bidi.
176 * @param aRect must be non-null
178 static nsIFrame
* GetGeometry(const mozilla::dom::Selection
* aSelection
,
181 static nsRect
GetGeometryForFrame(nsIFrame
* aFrame
, int32_t aFrameOffset
,
182 nscoord
* aBidiIndicatorSize
);
184 // Get the frame and frame offset based on the focus node and focus offset
185 // of aSelection. If aOverrideNode and aOverride are provided, use them
187 // @param aFrameOffset return the frame offset if non-null.
188 // @param aUnadjustedFrame return the original frame that the selection is
189 // targeting, without any adjustment for painting.
190 // @return the frame of the focus node.
191 static nsIFrame
* GetFrameAndOffset(const mozilla::dom::Selection
* aSelection
,
192 nsINode
* aOverrideNode
,
193 int32_t aOverrideOffset
,
194 int32_t* aFrameOffset
,
195 nsIFrame
** aUnadjustedFrame
= nullptr);
197 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
200 static void CaretBlinkCallback(nsITimer
* aTimer
, void* aClosure
);
202 void CheckSelectionLanguageChange();
204 void ResetBlinking();
208 nscoord mBidiIndicatorSize
; // width and height of bidi indicator
209 nscoord mCaretWidth
; // full caret width including bidi indicator
211 static Metrics
ComputeMetrics(nsIFrame
* aFrame
, int32_t aOffset
,
212 nscoord aCaretHeight
);
213 void ComputeCaretRects(nsIFrame
* aFrame
, int32_t aFrameOffset
,
214 nsRect
* aCaretRect
, nsRect
* aHookRect
);
216 // Returns true if we should not draw the caret because of XUL menu popups.
217 // The caret should be hidden if:
218 // 1. An open popup contains the caret, but a menu popup exists before the
219 // caret-owning popup in the popup list (i.e. a menu is in front of the
220 // popup with the caret). If the menu itself contains the caret we don't
222 // 2. A menu popup is open, but there is no caret present in any popup.
223 // 3. The caret selection is empty.
224 bool IsMenuPopupHidingCaret();
226 nsWeakPtr mPresShell
;
227 mozilla::WeakPtr
<mozilla::dom::Selection
> mDomSelectionWeak
;
229 nsCOMPtr
<nsITimer
> mBlinkTimer
;
232 * The content to draw the caret at. If null, we use mDomSelectionWeak's
233 * focus node instead.
235 nsCOMPtr
<nsINode
> mOverrideContent
;
237 * The character offset to draw the caret at.
238 * Ignored if mOverrideContent is null.
240 int32_t mOverrideOffset
;
242 * mBlinkCount is used to control the number of times to blink the caret
243 * before stopping the blink. This is reset each time we reset the
248 * mBlinkRate is the rate of the caret blinking the last time we read it.
249 * It is used as a way to optimize whether we need to reset the blinking
250 * timer. 0 or a negative value means no blinking.
254 * mHideCount is not 0, it means that somebody doesn't want the caret
255 * to be visible. See AddForceHide() and RemoveForceHide().
260 * mIsBlinkOn is true when we're in a blink cycle where the caret is on.
264 * mIsVisible is true when SetVisible was last called with 'true'.
268 * mReadOnly is true when the caret is set to "read only" mode (i.e.,
273 * mShowDuringSelection is true when the caret should be shown even when
274 * the selection is not collapsed.
276 bool mShowDuringSelection
;
278 * mIgnoreUserModify is true when the caret should be shown even when
279 * it's in non-user-modifiable content.
281 bool mIgnoreUserModify
;
284 #endif // nsCaret_h__