no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / layout / base / nsCaret.cpp
blob8707dcba5f15ee6e044803c609f774ae74abff9d
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 */
9 #include "nsCaret.h"
11 #include <algorithm>
13 #include "gfxUtils.h"
14 #include "mozilla/CaretAssociationHint.h"
15 #include "mozilla/gfx/2D.h"
16 #include "mozilla/intl/BidiEmbeddingLevel.h"
17 #include "mozilla/StaticPrefs_bidi.h"
18 #include "nsCOMPtr.h"
19 #include "nsFontMetrics.h"
20 #include "nsITimer.h"
21 #include "nsFrameSelection.h"
22 #include "nsIFrame.h"
23 #include "nsIScrollableFrame.h"
24 #include "nsIContent.h"
25 #include "nsIFrameInlines.h"
26 #include "nsLayoutUtils.h"
27 #include "nsPresContext.h"
28 #include "nsBlockFrame.h"
29 #include "nsISelectionController.h"
30 #include "nsTextFrame.h"
31 #include "nsXULPopupManager.h"
32 #include "nsMenuPopupFrame.h"
33 #include "nsTextFragment.h"
34 #include "mozilla/Preferences.h"
35 #include "mozilla/PresShell.h"
36 #include "mozilla/LookAndFeel.h"
37 #include "mozilla/dom/Selection.h"
38 #include "nsIBidiKeyboard.h"
39 #include "nsContentUtils.h"
40 #include "SelectionMovementUtils.h"
42 using namespace mozilla;
43 using namespace mozilla::dom;
44 using namespace mozilla::gfx;
46 using BidiEmbeddingLevel = mozilla::intl::BidiEmbeddingLevel;
48 // The bidi indicator hangs off the caret to one side, to show which
49 // direction the typing is in. It needs to be at least 2x2 to avoid looking
50 // like an insignificant dot
51 static const int32_t kMinBidiIndicatorPixels = 2;
53 nsCaret::nsCaret() = default;
55 nsCaret::~nsCaret() { StopBlinking(); }
57 nsresult nsCaret::Init(PresShell* aPresShell) {
58 NS_ENSURE_ARG(aPresShell);
60 mPresShell =
61 do_GetWeakReference(aPresShell); // the presshell owns us, so no addref
62 NS_ASSERTION(mPresShell, "Hey, pres shell should support weak refs");
64 RefPtr<Selection> selection =
65 aPresShell->GetSelection(nsISelectionController::SELECTION_NORMAL);
66 if (!selection) {
67 return NS_ERROR_FAILURE;
70 selection->AddSelectionListener(this);
71 mDomSelectionWeak = selection;
72 UpdateCaretPositionFromSelectionIfNeeded();
74 return NS_OK;
77 static bool DrawCJKCaret(nsIFrame* aFrame, int32_t aOffset) {
78 nsIContent* content = aFrame->GetContent();
79 const nsTextFragment* frag = content->GetText();
80 if (!frag) {
81 return false;
83 if (aOffset < 0 || static_cast<uint32_t>(aOffset) >= frag->GetLength()) {
84 return false;
86 const char16_t ch = frag->CharAt(AssertedCast<uint32_t>(aOffset));
87 return 0x2e80 <= ch && ch <= 0xd7ff;
90 nsCaret::Metrics nsCaret::ComputeMetrics(nsIFrame* aFrame, int32_t aOffset,
91 nscoord aCaretHeight) {
92 // Compute nominal sizes in appunits
93 nscoord caretWidth =
94 (aCaretHeight *
95 LookAndFeel::GetFloat(LookAndFeel::FloatID::CaretAspectRatio, 0.0f)) +
96 nsPresContext::CSSPixelsToAppUnits(
97 LookAndFeel::GetInt(LookAndFeel::IntID::CaretWidth, 1));
99 if (DrawCJKCaret(aFrame, aOffset)) {
100 caretWidth += nsPresContext::CSSPixelsToAppUnits(1);
102 nscoord bidiIndicatorSize =
103 nsPresContext::CSSPixelsToAppUnits(kMinBidiIndicatorPixels);
104 bidiIndicatorSize = std::max(caretWidth, bidiIndicatorSize);
106 // Round them to device pixels. Always round down, except that anything
107 // between 0 and 1 goes up to 1 so we don't let the caret disappear.
108 int32_t tpp = aFrame->PresContext()->AppUnitsPerDevPixel();
109 Metrics result;
110 result.mCaretWidth = NS_ROUND_BORDER_TO_PIXELS(caretWidth, tpp);
111 result.mBidiIndicatorSize = NS_ROUND_BORDER_TO_PIXELS(bidiIndicatorSize, tpp);
112 return result;
115 void nsCaret::Terminate() {
116 // this doesn't erase the caret if it's drawn. Should it? We might not have
117 // a good drawing environment during teardown.
119 StopBlinking();
120 mBlinkTimer = nullptr;
122 // unregiser ourselves as a selection listener
123 if (mDomSelectionWeak) {
124 mDomSelectionWeak->RemoveSelectionListener(this);
126 mDomSelectionWeak = nullptr;
127 mPresShell = nullptr;
128 mCaretPosition = {};
131 NS_IMPL_ISUPPORTS(nsCaret, nsISelectionListener)
133 Selection* nsCaret::GetSelection() { return mDomSelectionWeak; }
135 void nsCaret::SetSelection(Selection* aDOMSel) {
136 MOZ_ASSERT(aDOMSel);
137 mDomSelectionWeak = aDOMSel;
138 UpdateCaretPositionFromSelectionIfNeeded();
139 ResetBlinking();
140 SchedulePaint();
143 void nsCaret::SetVisible(bool aVisible) {
144 const bool wasVisible = mVisible;
145 mVisible = aVisible;
146 mIgnoreUserModify = aVisible;
147 if (mVisible != wasVisible) {
148 CaretVisibilityMaybeChanged();
152 bool nsCaret::IsVisible() const { return mVisible && !mHideCount; }
154 void nsCaret::CaretVisibilityMaybeChanged() {
155 ResetBlinking();
156 SchedulePaint();
157 if (IsVisible()) {
158 // We ignore caret position updates when the caret is not visible, so we
159 // update the caret position here if needed.
160 UpdateCaretPositionFromSelectionIfNeeded();
164 void nsCaret::AddForceHide() {
165 MOZ_ASSERT(mHideCount < UINT32_MAX);
166 if (++mHideCount > 1) {
167 return;
169 CaretVisibilityMaybeChanged();
172 void nsCaret::RemoveForceHide() {
173 if (!mHideCount || --mHideCount) {
174 return;
176 CaretVisibilityMaybeChanged();
179 void nsCaret::SetCaretReadOnly(bool aReadOnly) {
180 mReadOnly = aReadOnly;
181 ResetBlinking();
182 SchedulePaint();
185 // Clamp the inline-position to be within our closest scroll frame and any
186 // ancestor clips if any. If we don't, then it clips us, and we don't appear at
187 // all. See bug 335560 and bug 1539720.
188 static nsPoint AdjustRectForClipping(const nsRect& aRect, nsIFrame* aFrame,
189 bool aVertical) {
190 nsRect rectRelativeToClip = aRect;
191 nsIScrollableFrame* sf = nullptr;
192 nsIFrame* scrollFrame = nullptr;
193 for (nsIFrame* current = aFrame; current; current = current->GetParent()) {
194 if ((sf = do_QueryFrame(current))) {
195 scrollFrame = current;
196 break;
198 if (current->IsTransformed()) {
199 // We don't account for transforms in rectRelativeToCurrent, so stop
200 // adjusting here.
201 break;
203 rectRelativeToClip += current->GetPosition();
206 if (!sf) {
207 return {};
210 nsRect clipRect = sf->GetScrollPortRect();
212 const auto& disp = *scrollFrame->StyleDisplay();
213 if (disp.mOverflowClipBoxBlock == StyleOverflowClipBox::ContentBox ||
214 disp.mOverflowClipBoxInline == StyleOverflowClipBox::ContentBox) {
215 const WritingMode wm = scrollFrame->GetWritingMode();
216 const bool cbH = (wm.IsVertical() ? disp.mOverflowClipBoxBlock
217 : disp.mOverflowClipBoxInline) ==
218 StyleOverflowClipBox::ContentBox;
219 const bool cbV = (wm.IsVertical() ? disp.mOverflowClipBoxInline
220 : disp.mOverflowClipBoxBlock) ==
221 StyleOverflowClipBox::ContentBox;
222 nsMargin padding = scrollFrame->GetUsedPadding();
223 if (!cbH) {
224 padding.left = padding.right = 0;
226 if (!cbV) {
227 padding.top = padding.bottom = 0;
229 clipRect.Deflate(padding);
232 nsPoint offset;
233 // Now see if the caret extends beyond the view's bounds. If it does, then
234 // snap it back, put it as close to the edge as it can.
235 if (aVertical) {
236 nscoord overflow = rectRelativeToClip.YMost() - clipRect.YMost();
237 if (overflow > 0) {
238 offset.y -= overflow;
239 } else {
240 overflow = rectRelativeToClip.y - clipRect.y;
241 if (overflow < 0) {
242 offset.y -= overflow;
245 } else {
246 nscoord overflow = rectRelativeToClip.XMost() - clipRect.XMost();
247 if (overflow > 0) {
248 offset.x -= overflow;
249 } else {
250 overflow = rectRelativeToClip.x - clipRect.x;
251 if (overflow < 0) {
252 offset.x -= overflow;
256 return offset;
259 /* static */
260 nsRect nsCaret::GetGeometryForFrame(nsIFrame* aFrame, int32_t aFrameOffset,
261 nscoord* aBidiIndicatorSize) {
262 nsPoint framePos(0, 0);
263 nsRect rect;
264 nsresult rv = aFrame->GetPointFromOffset(aFrameOffset, &framePos);
265 if (NS_FAILED(rv)) {
266 if (aBidiIndicatorSize) {
267 *aBidiIndicatorSize = 0;
269 return rect;
272 nsIFrame* frame = aFrame->GetContentInsertionFrame();
273 if (!frame) {
274 frame = aFrame;
276 NS_ASSERTION(!frame->HasAnyStateBits(NS_FRAME_IN_REFLOW),
277 "We should not be in the middle of reflow");
278 WritingMode wm = aFrame->GetWritingMode();
279 RefPtr<nsFontMetrics> fm =
280 nsLayoutUtils::GetInflatedFontMetricsForFrame(aFrame);
281 const auto caretBlockAxisMetrics = frame->GetCaretBlockAxisMetrics(wm, *fm);
282 const bool vertical = wm.IsVertical();
283 Metrics caretMetrics =
284 ComputeMetrics(aFrame, aFrameOffset, caretBlockAxisMetrics.mExtent);
286 nscoord inlineOffset = 0;
287 if (nsTextFrame* textFrame = do_QueryFrame(aFrame)) {
288 if (gfxTextRun* textRun = textFrame->GetTextRun(nsTextFrame::eInflated)) {
289 // For "upstream" text where the textrun direction is reversed from the
290 // frame's inline-dir we want the caret to be painted before rather than
291 // after its nominal inline position, so we offset by its width.
292 const bool textRunDirIsReverseOfFrame =
293 wm.IsInlineReversed() != textRun->IsInlineReversed();
294 // However, in sideways-lr mode we invert this behavior because this is
295 // the one writing mode where bidi-LTR corresponds to inline-reversed
296 // already, which reverses the desired caret placement behavior.
297 // Note that the following condition is equivalent to:
298 // if ( (!textRun->IsSidewaysLeft() && textRunDirIsReverseOfFrame) ||
299 // (textRun->IsSidewaysLeft() && !textRunDirIsReverseOfFrame) )
300 if (textRunDirIsReverseOfFrame != textRun->IsSidewaysLeft()) {
301 inlineOffset = wm.IsBidiLTR() ? -caretMetrics.mCaretWidth
302 : caretMetrics.mCaretWidth;
307 // on RTL frames the right edge of mCaretRect must be equal to framePos
308 if (aFrame->StyleVisibility()->mDirection == StyleDirection::Rtl) {
309 if (vertical) {
310 inlineOffset -= caretMetrics.mCaretWidth;
311 } else {
312 inlineOffset -= caretMetrics.mCaretWidth;
316 if (vertical) {
317 framePos.x = caretBlockAxisMetrics.mOffset;
318 framePos.y += inlineOffset;
319 } else {
320 framePos.x += inlineOffset;
321 framePos.y = caretBlockAxisMetrics.mOffset;
324 rect = nsRect(framePos, vertical ? nsSize(caretBlockAxisMetrics.mExtent,
325 caretMetrics.mCaretWidth)
326 : nsSize(caretMetrics.mCaretWidth,
327 caretBlockAxisMetrics.mExtent));
329 rect.MoveBy(AdjustRectForClipping(rect, aFrame, vertical));
330 if (aBidiIndicatorSize) {
331 *aBidiIndicatorSize = caretMetrics.mBidiIndicatorSize;
333 return rect;
336 auto nsCaret::CaretPositionFor(const Selection* aSelection) -> CaretPosition {
337 if (!aSelection) {
338 return {};
340 const nsFrameSelection* frameSelection = aSelection->GetFrameSelection();
341 if (!frameSelection) {
342 return {};
344 nsINode* node = aSelection->GetFocusNode();
345 if (!node) {
346 return {};
348 return {
349 node,
350 int32_t(aSelection->FocusOffset()),
351 frameSelection->GetHint(),
352 frameSelection->GetCaretBidiLevel(),
356 CaretFrameData nsCaret::GetFrameAndOffset(const CaretPosition& aPosition) {
357 nsINode* focusNode = aPosition.mContent;
358 int32_t focusOffset = aPosition.mOffset;
360 if (!focusNode || !focusNode->IsContent()) {
361 return {};
364 nsIContent* contentNode = focusNode->AsContent();
365 return SelectionMovementUtils::GetCaretFrameForNodeOffset(
366 nullptr, contentNode, focusOffset, aPosition.mHint, aPosition.mBidiLevel,
367 ForceEditableRegion::No);
370 /* static */
371 nsIFrame* nsCaret::GetGeometry(const Selection* aSelection, nsRect* aRect) {
372 auto data = GetFrameAndOffset(CaretPositionFor(aSelection));
373 if (data.mFrame) {
374 *aRect =
375 GetGeometryForFrame(data.mFrame, data.mOffsetInFrameContent, nullptr);
377 return data.mFrame;
380 [[nodiscard]] static nsIFrame* GetContainingBlockIfNeeded(nsIFrame* aFrame) {
381 if (aFrame->IsBlockOutside() || aFrame->IsBlockFrameOrSubclass()) {
382 return nullptr;
384 return aFrame->GetContainingBlock();
387 void nsCaret::SchedulePaint() {
388 if (mLastPaintedFrame) {
389 mLastPaintedFrame->SchedulePaint();
390 mLastPaintedFrame = nullptr;
392 auto data = GetFrameAndOffset(mCaretPosition);
393 if (!data.mFrame) {
394 return;
396 nsIFrame* frame = data.mFrame;
397 if (nsIFrame* cb = GetContainingBlockIfNeeded(frame)) {
398 frame = cb;
400 frame->SchedulePaint();
403 void nsCaret::SetVisibilityDuringSelection(bool aVisibility) {
404 if (mShowDuringSelection == aVisibility) {
405 return;
407 mShowDuringSelection = aVisibility;
408 if (mHiddenDuringSelection && aVisibility) {
409 RemoveForceHide();
410 mHiddenDuringSelection = false;
412 SchedulePaint();
415 void nsCaret::UpdateCaretPositionFromSelectionIfNeeded() {
416 if (mFixedCaretPosition) {
417 return;
419 CaretPosition newPos = CaretPositionFor(GetSelection());
420 if (newPos == mCaretPosition) {
421 return;
423 mCaretPosition = newPos;
424 SchedulePaint();
427 void nsCaret::SetCaretPosition(nsINode* aNode, int32_t aOffset) {
428 // Schedule a paint with the old position to invalidate.
429 mFixedCaretPosition = !!aNode;
430 if (mFixedCaretPosition) {
431 mCaretPosition = {aNode, aOffset};
432 SchedulePaint();
433 } else {
434 UpdateCaretPositionFromSelectionIfNeeded();
436 ResetBlinking();
439 void nsCaret::CheckSelectionLanguageChange() {
440 if (!StaticPrefs::bidi_browser_ui()) {
441 return;
444 bool isKeyboardRTL = false;
445 nsIBidiKeyboard* bidiKeyboard = nsContentUtils::GetBidiKeyboard();
446 if (bidiKeyboard) {
447 bidiKeyboard->IsLangRTL(&isKeyboardRTL);
449 // Call SelectionLanguageChange on every paint. Mostly it will be a noop
450 // but it should be fast anyway. This guarantees we never paint the caret
451 // at the wrong place.
452 Selection* selection = GetSelection();
453 if (selection) {
454 selection->SelectionLanguageChange(isKeyboardRTL);
458 // This ensures that the caret is not affected by clips on inlines and so forth.
459 [[nodiscard]] static nsIFrame* MapToContainingBlock(nsIFrame* aFrame,
460 nsRect* aCaretRect,
461 nsRect* aHookRect) {
462 nsIFrame* containingBlock = GetContainingBlockIfNeeded(aFrame);
463 if (!containingBlock) {
464 return aFrame;
467 *aCaretRect = nsLayoutUtils::TransformFrameRectToAncestor(aFrame, *aCaretRect,
468 containingBlock);
469 *aHookRect = nsLayoutUtils::TransformFrameRectToAncestor(aFrame, *aHookRect,
470 containingBlock);
471 return containingBlock;
474 nsIFrame* nsCaret::GetPaintGeometry(nsRect* aCaretRect, nsRect* aHookRect,
475 nscolor* aCaretColor) {
476 // Return null if we should not be visible.
477 if (!IsVisible() || !mIsBlinkOn) {
478 return nullptr;
481 // Update selection language direction now so the new direction will be
482 // taken into account when computing the caret position below.
483 CheckSelectionLanguageChange();
485 auto data = GetFrameAndOffset(mCaretPosition);
486 MOZ_ASSERT(!!data.mFrame == !!data.mUnadjustedFrame);
487 if (!data.mFrame) {
488 return nullptr;
491 nsIFrame* frame = data.mFrame;
492 nsIFrame* unadjustedFrame = data.mUnadjustedFrame;
493 int32_t frameOffset(data.mOffsetInFrameContent);
494 // Now we have a frame, check whether it's appropriate to show the caret here.
495 // Note we need to check the unadjusted frame, otherwise consider the
496 // following case:
498 // <div contenteditable><span contenteditable=false>Text </span><br>
500 // Where the selection is targeting the <br>. We want to display the caret,
501 // since the <br> we're focused at is editable, but we do want to paint it at
502 // the adjusted frame offset, so that we can see the collapsed whitespace.
503 const nsStyleUI* ui = unadjustedFrame->StyleUI();
504 if ((!mIgnoreUserModify && ui->UserModify() == StyleUserModify::ReadOnly) ||
505 unadjustedFrame->IsContentDisabled()) {
506 return nullptr;
509 // If the offset falls outside of the frame, then don't paint the caret.
510 if (frame->IsTextFrame()) {
511 auto [startOffset, endOffset] = frame->GetOffsets();
512 if (startOffset > frameOffset || endOffset < frameOffset) {
513 return nullptr;
517 if (aCaretColor) {
518 *aCaretColor = frame->GetCaretColorAt(frameOffset);
521 ComputeCaretRects(frame, frameOffset, aCaretRect, aHookRect);
522 return MapToContainingBlock(frame, aCaretRect, aHookRect);
525 nsIFrame* nsCaret::GetPaintGeometry(nsRect* aRect) {
526 nsRect caretRect;
527 nsRect hookRect;
528 nsIFrame* frame = GetPaintGeometry(&caretRect, &hookRect);
529 aRect->UnionRect(caretRect, hookRect);
530 return frame;
533 void nsCaret::PaintCaret(DrawTarget& aDrawTarget, nsIFrame* aForFrame,
534 const nsPoint& aOffset) {
535 nsRect caretRect;
536 nsRect hookRect;
537 nscolor color;
538 nsIFrame* frame = GetPaintGeometry(&caretRect, &hookRect, &color);
539 MOZ_ASSERT(frame == aForFrame, "We're referring different frame");
541 if (!frame) {
542 return;
545 int32_t appUnitsPerDevPixel = frame->PresContext()->AppUnitsPerDevPixel();
546 Rect devPxCaretRect = NSRectToSnappedRect(caretRect + aOffset,
547 appUnitsPerDevPixel, aDrawTarget);
548 Rect devPxHookRect =
549 NSRectToSnappedRect(hookRect + aOffset, appUnitsPerDevPixel, aDrawTarget);
551 ColorPattern pattern(ToDeviceColor(color));
552 aDrawTarget.FillRect(devPxCaretRect, pattern);
553 if (!hookRect.IsEmpty()) {
554 aDrawTarget.FillRect(devPxHookRect, pattern);
558 NS_IMETHODIMP
559 nsCaret::NotifySelectionChanged(Document*, Selection* aDomSel, int16_t aReason,
560 int32_t aAmount) {
561 // The same caret is shared amongst the document and any text widgets it
562 // may contain. This means that the caret could get notifications from
563 // multiple selections.
565 // If this notification is for a selection that is not the one the
566 // the caret is currently interested in (mDomSelectionWeak), or the caret
567 // position is fixed, then there is nothing to do!
568 if (mDomSelectionWeak != aDomSel) {
569 return NS_OK;
572 // Check if we need to hide / un-hide the caret due to the selection being
573 // collapsed.
574 if (!mShowDuringSelection &&
575 !aDomSel->IsCollapsed() != mHiddenDuringSelection) {
576 if (mHiddenDuringSelection) {
577 RemoveForceHide();
578 } else {
579 AddForceHide();
581 mHiddenDuringSelection = !mHiddenDuringSelection;
584 // We don't bother computing the caret position when invisible. We'll do it if
585 // we become visible in CaretVisibilityMaybeChanged().
586 if (IsVisible()) {
587 UpdateCaretPositionFromSelectionIfNeeded();
588 ResetBlinking();
591 return NS_OK;
594 void nsCaret::ResetBlinking() {
595 using IntID = LookAndFeel::IntID;
597 // The default caret blinking rate (in ms of blinking interval)
598 constexpr uint32_t kDefaultBlinkRate = 500;
599 // The default caret blinking count (-1 for "never stop blinking")
600 constexpr int32_t kDefaultBlinkCount = -1;
602 mIsBlinkOn = true;
604 if (mReadOnly || !IsVisible()) {
605 StopBlinking();
606 return;
609 auto blinkRate =
610 LookAndFeel::GetInt(IntID::CaretBlinkTime, kDefaultBlinkRate);
612 if (blinkRate > 0) {
613 // Make sure to reset the remaining blink count even if the blink rate
614 // hasn't changed.
615 mBlinkCount =
616 LookAndFeel::GetInt(IntID::CaretBlinkCount, kDefaultBlinkCount);
619 if (mBlinkRate == blinkRate) {
620 // If the rate hasn't changed, then there is nothing else to do.
621 return;
624 mBlinkRate = blinkRate;
626 if (mBlinkTimer) {
627 mBlinkTimer->Cancel();
628 } else {
629 mBlinkTimer = NS_NewTimer(GetMainThreadSerialEventTarget());
630 if (!mBlinkTimer) {
631 return;
635 if (blinkRate > 0) {
636 mBlinkTimer->InitWithNamedFuncCallback(CaretBlinkCallback, this, blinkRate,
637 nsITimer::TYPE_REPEATING_SLACK,
638 "nsCaret::CaretBlinkCallback_timer");
642 void nsCaret::StopBlinking() {
643 if (mBlinkTimer) {
644 mBlinkTimer->Cancel();
645 mBlinkRate = 0;
649 size_t nsCaret::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
650 size_t total = aMallocSizeOf(this);
651 if (mPresShell) {
652 // We only want the size of the nsWeakReference object, not the PresShell
653 // (since we don't own the PresShell).
654 total += mPresShell->SizeOfOnlyThis(aMallocSizeOf);
656 if (mBlinkTimer) {
657 total += mBlinkTimer->SizeOfIncludingThis(aMallocSizeOf);
659 return total;
662 void nsCaret::ComputeCaretRects(nsIFrame* aFrame, int32_t aFrameOffset,
663 nsRect* aCaretRect, nsRect* aHookRect) {
664 NS_ASSERTION(aFrame, "Should have a frame here");
666 WritingMode wm = aFrame->GetWritingMode();
667 bool isVertical = wm.IsVertical();
669 nscoord bidiIndicatorSize;
670 *aCaretRect = GetGeometryForFrame(aFrame, aFrameOffset, &bidiIndicatorSize);
672 // Simon -- make a hook to draw to the left or right of the caret to show
673 // keyboard language direction
674 aHookRect->SetEmpty();
675 if (!StaticPrefs::bidi_browser_ui()) {
676 return;
679 bool isCaretRTL;
680 nsIBidiKeyboard* bidiKeyboard = nsContentUtils::GetBidiKeyboard();
681 // if bidiKeyboard->IsLangRTL() fails, there is no way to tell the
682 // keyboard direction, or the user has no right-to-left keyboard
683 // installed, so we never draw the hook.
684 if (bidiKeyboard && NS_SUCCEEDED(bidiKeyboard->IsLangRTL(&isCaretRTL))) {
685 // If keyboard language is RTL, draw the hook on the left; if LTR, to the
686 // right The height of the hook rectangle is the same as the width of the
687 // caret rectangle.
688 if (isVertical) {
689 if (wm.IsSidewaysLR()) {
690 aHookRect->SetRect(aCaretRect->x + bidiIndicatorSize,
691 aCaretRect->y + (!isCaretRTL ? bidiIndicatorSize * -1
692 : aCaretRect->height),
693 aCaretRect->height, bidiIndicatorSize);
694 } else {
695 aHookRect->SetRect(aCaretRect->XMost() - bidiIndicatorSize,
696 aCaretRect->y + (isCaretRTL ? bidiIndicatorSize * -1
697 : aCaretRect->height),
698 aCaretRect->height, bidiIndicatorSize);
700 } else {
701 aHookRect->SetRect(aCaretRect->x + (isCaretRTL ? bidiIndicatorSize * -1
702 : aCaretRect->width),
703 aCaretRect->y + bidiIndicatorSize, bidiIndicatorSize,
704 aCaretRect->width);
709 /* static */
710 void nsCaret::CaretBlinkCallback(nsITimer* aTimer, void* aClosure) {
711 nsCaret* theCaret = reinterpret_cast<nsCaret*>(aClosure);
712 if (!theCaret) {
713 return;
715 theCaret->mIsBlinkOn = !theCaret->mIsBlinkOn;
716 theCaret->SchedulePaint();
718 // mBlinkCount of -1 means blink count is not enabled.
719 if (theCaret->mBlinkCount == -1) {
720 return;
723 // Track the blink count, but only at end of a blink cycle.
724 if (theCaret->mIsBlinkOn) {
725 // If we exceeded the blink count, stop the timer.
726 if (--theCaret->mBlinkCount <= 0) {
727 theCaret->StopBlinking();
732 void nsCaret::SetIgnoreUserModify(bool aIgnoreUserModify) {
733 mIgnoreUserModify = aIgnoreUserModify;
734 SchedulePaint();