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 /* the caret is the text cursor used, e.g., when editing */
12 #include "mozilla/MemoryReporting.h"
14 #include "nsISelectionListener.h"
15 #include "nsIWeakReferenceUtils.h"
16 #include "CaretAssociationHint.h"
20 class nsDisplayListBuilder
;
21 class nsFrameSelection
;
38 //-----------------------------------------------------------------------------
39 class nsCaret MOZ_FINAL
: public nsISelectionListener
41 typedef mozilla::gfx::DrawTarget DrawTarget
;
52 typedef mozilla::CaretAssociationHint CaretAssociationHint
;
54 nsresult
Init(nsIPresShell
*inPresShell
);
57 void SetSelection(nsISelection
*aDOMSel
);
58 nsISelection
* GetSelection();
61 * Sets whether the caret should only be visible in nodes that are not
62 * user-modify: read-only, or whether it should be visible in all nodes.
64 * @param aIgnoreUserModify true to have the cursor visible in all nodes,
65 * false to have it visible in all nodes except
66 * those with user-modify: read-only
68 void SetIgnoreUserModify(bool aIgnoreUserModify
);
69 /** SetVisible will set the visibility of the caret
70 * @param inMakeVisible true to show the caret, false to hide it
72 void SetVisible(bool intMakeVisible
);
73 /** IsVisible will get the visibility of the caret.
74 * This returns false if the caret is hidden because it was set
75 * to not be visible, or because the selection is not collapsed, or
76 * because an open popup is hiding the caret.
77 * It does not take account of blinking or the caret being hidden
78 * because we're in non-editable/disabled content.
81 /** SetCaretReadOnly set the appearance of the caret
82 * @param inMakeReadonly true to show the caret in a 'read only' state,
83 * false to show the caret in normal, editing state
85 void SetCaretReadOnly(bool inMakeReadonly
);
87 * @param aVisibility true if the caret should be visible even when the
88 * selection is not collapsed.
90 void SetVisibilityDuringSelection(bool aVisibility
);
93 * Set the caret's position explicitly to the specified node and offset
94 * instead of tracking its selection.
95 * Passing null for aNode would set the caret to track its selection again.
97 void SetCaretPosition(nsIDOMNode
* aNode
, int32_t aOffset
);
100 * Schedule a repaint for the frame where the caret would appear.
101 * Does not check visibility etc.
103 void SchedulePaint();
106 * Returns a frame to paint in, and the bounds of the painted caret
107 * relative to that frame.
108 * The rectangle includes bidi decorations.
109 * Returns null if the caret should not be drawn (including if it's blinked
112 nsIFrame
* GetPaintGeometry(nsRect
* aRect
);
114 * A simple wrapper around GetGeometry. Does not take any caret state into
115 * account other than the current selection.
117 nsIFrame
* GetGeometry(nsRect
* aRect
)
119 return GetGeometry(GetSelection(), aRect
);
123 * Actually paint the caret onto the given rendering context.
125 void PaintCaret(nsDisplayListBuilder
*aBuilder
,
126 DrawTarget
& aDrawTarget
,
128 const nsPoint
&aOffset
);
130 //nsISelectionListener interface
131 NS_DECL_NSISELECTIONLISTENER
134 * Gets the position and size of the caret that would be drawn for
135 * the focus node/offset of aSelection (assuming it would be drawn,
136 * i.e., disregarding blink status). The geometry is stored in aRect,
137 * and we return the frame aRect is relative to.
138 * Only looks at the focus node of aSelection, so you can call it even if
139 * aSelection is not collapsed.
140 * This rect does not include any extra decorations for bidi.
141 * @param aRect must be non-null
143 static nsIFrame
* GetGeometry(nsISelection
* aSelection
,
145 static nsresult
GetCaretFrameForNodeOffset(nsFrameSelection
* aFrameSelection
,
146 nsIContent
* aContentNode
,
148 CaretAssociationHint aFrameHint
,
150 nsIFrame
** aReturnFrame
,
151 int32_t* aReturnOffset
);
152 static nsRect
GetGeometryForFrame(nsIFrame
* aFrame
,
153 int32_t aFrameOffset
,
154 nscoord
* aBidiIndicatorSize
);
156 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
159 static void CaretBlinkCallback(nsITimer
*aTimer
, void *aClosure
);
161 void CheckSelectionLanguageChange();
163 void ResetBlinking();
166 mozilla::dom::Selection
* GetSelectionInternal();
169 nscoord mBidiIndicatorSize
; // width and height of bidi indicator
170 nscoord mCaretWidth
; // full caret width including bidi indicator
172 static Metrics
ComputeMetrics(nsIFrame
* aFrame
, int32_t aOffset
,
173 nscoord aCaretHeight
);
175 void ComputeCaretRects(nsIFrame
* aFrame
, int32_t aFrameOffset
,
176 nsRect
* aCaretRect
, nsRect
* aHookRect
);
178 // Returns true if we should not draw the caret because of XUL menu popups.
179 // The caret should be hidden if:
180 // 1. An open popup contains the caret, but a menu popup exists before the
181 // caret-owning popup in the popup list (i.e. a menu is in front of the
182 // popup with the caret). If the menu itself contains the caret we don't
184 // 2. A menu popup is open, but there is no caret present in any popup.
185 // 3. The caret selection is empty.
186 bool IsMenuPopupHidingCaret();
188 nsWeakPtr mPresShell
;
189 nsWeakPtr mDomSelectionWeak
;
191 nsCOMPtr
<nsITimer
> mBlinkTimer
;
194 * The content to draw the caret at. If null, we use mDomSelectionWeak's
195 * focus node instead.
197 nsCOMPtr
<nsINode
> mOverrideContent
;
199 * The character offset to draw the caret at.
200 * Ignored if mOverrideContent is null.
202 int32_t mOverrideOffset
;
205 * mIsBlinkOn is true when we're in a blink cycle where the caret is on.
209 * mIsVisible is true when SetVisible was last called with 'true'.
213 * mReadOnly is true when the caret is set to "read only" mode (i.e.,
218 * mShowDuringSelection is true when the caret should be shown even when
219 * the selection is not collapsed.
221 bool mShowDuringSelection
;
223 * mIgnoreUserModify is true when the caret should be shown even when
224 * it's in non-user-modifiable content.
226 bool mIgnoreUserModify
;