2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* the caret is the text cursor used, e.g., when editing */
|
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
#include "nsCaret.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsITimer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsFrameSelection.h"
|
|
|
|
#include "nsIFrame.h"
|
2007-08-13 13:51:16 -07:00
|
|
|
#include "nsIScrollableFrame.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIPresShell.h"
|
2011-04-07 18:04:40 -07:00
|
|
|
#include "nsRenderingContext.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsBlockFrame.h"
|
|
|
|
#include "nsISelectionController.h"
|
2007-11-07 20:07:00 -08:00
|
|
|
#include "nsTextFrame.h"
|
2008-04-01 01:35:11 -07:00
|
|
|
#include "nsXULPopupManager.h"
|
|
|
|
#include "nsMenuPopupFrame.h"
|
2008-04-22 20:13:55 -07:00
|
|
|
#include "nsTextFragment.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-08 19:27:13 -07:00
|
|
|
#include "mozilla/LookAndFeel.h"
|
2014-04-10 09:09:40 -07:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIBidiKeyboard.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2011-05-24 23:31:59 -07:00
|
|
|
using namespace mozilla;
|
2014-08-05 22:19:27 -07:00
|
|
|
using namespace mozilla::dom;
|
2011-05-24 23:31:59 -07:00
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
// The bidi indicator hangs off the caret to one side, to show which
|
|
|
|
// direction the typing is in. It needs to be at least 2x2 to avoid looking like
|
|
|
|
// an insignificant dot
|
|
|
|
static const int32_t kMinBidiIndicatorPixels = 2;
|
|
|
|
|
2010-03-14 15:52:45 -07:00
|
|
|
/**
|
|
|
|
* Find the first frame in an in-order traversal of the frame subtree rooted
|
|
|
|
* at aFrame which is either a text frame logically at the end of a line,
|
|
|
|
* or which is aStopAtFrame. Return null if no such frame is found. We don't
|
|
|
|
* descend into the children of non-eLineParticipant frames.
|
|
|
|
*/
|
|
|
|
static nsIFrame*
|
|
|
|
CheckForTrailingTextFrameRecursive(nsIFrame* aFrame, nsIFrame* aStopAtFrame)
|
|
|
|
{
|
|
|
|
if (aFrame == aStopAtFrame ||
|
|
|
|
((aFrame->GetType() == nsGkAtoms::textFrame &&
|
|
|
|
(static_cast<nsTextFrame*>(aFrame))->IsAtEndOfLine())))
|
|
|
|
return aFrame;
|
|
|
|
if (!aFrame->IsFrameOfType(nsIFrame::eLineParticipant))
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-03-14 15:52:45 -07:00
|
|
|
|
2011-08-24 13:54:30 -07:00
|
|
|
for (nsIFrame* f = aFrame->GetFirstPrincipalChild(); f; f = f->GetNextSibling())
|
2010-03-14 15:52:45 -07:00
|
|
|
{
|
|
|
|
nsIFrame* r = CheckForTrailingTextFrameRecursive(f, aStopAtFrame);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-03-14 15:52:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsLineBox*
|
|
|
|
FindContainingLine(nsIFrame* aFrame)
|
|
|
|
{
|
|
|
|
while (aFrame && aFrame->IsFrameOfType(nsIFrame::eLineParticipant))
|
|
|
|
{
|
|
|
|
nsIFrame* parent = aFrame->GetParent();
|
|
|
|
nsBlockFrame* blockParent = nsLayoutUtils::GetAsBlock(parent);
|
|
|
|
if (blockParent)
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isValid;
|
2010-03-14 15:52:45 -07:00
|
|
|
nsBlockInFlowLineIterator iter(blockParent, aFrame, &isValid);
|
2012-07-30 07:20:58 -07:00
|
|
|
return isValid ? iter.GetLine().get() : nullptr;
|
2010-03-14 15:52:45 -07:00
|
|
|
}
|
|
|
|
aFrame = parent;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-03-14 15:52:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-22 08:56:38 -07:00
|
|
|
AdjustCaretFrameForLineEnd(nsIFrame** aFrame, int32_t* aOffset)
|
2010-03-14 15:52:45 -07:00
|
|
|
{
|
|
|
|
nsLineBox* line = FindContainingLine(*aFrame);
|
|
|
|
if (!line)
|
|
|
|
return;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = line->GetChildCount();
|
2010-03-14 15:52:45 -07:00
|
|
|
for (nsIFrame* f = line->mFirstChild; count > 0; --count, f = f->GetNextSibling())
|
|
|
|
{
|
|
|
|
nsIFrame* r = CheckForTrailingTextFrameRecursive(f, *aFrame);
|
|
|
|
if (r == *aFrame)
|
|
|
|
return;
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
*aFrame = r;
|
|
|
|
NS_ASSERTION(r->GetType() == nsGkAtoms::textFrame, "Expected text frame");
|
|
|
|
*aOffset = (static_cast<nsTextFrame*>(r))->GetContentEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
static bool
|
|
|
|
IsBidiUI()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("bidi.browser.ui");
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCaret::nsCaret()
|
2014-08-05 22:19:30 -07:00
|
|
|
: mOverrideOffset(0)
|
2014-08-05 22:19:29 -07:00
|
|
|
, mIsBlinkOn(false)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mVisible(false)
|
|
|
|
, mReadOnly(false)
|
|
|
|
, mShowDuringSelection(false)
|
|
|
|
, mIgnoreUserModify(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCaret::~nsCaret()
|
|
|
|
{
|
2014-08-05 22:19:30 -07:00
|
|
|
StopBlinking();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-16 03:52:01 -07:00
|
|
|
nsresult nsCaret::Init(nsIPresShell *inPresShell)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(inPresShell);
|
2008-07-16 03:52:01 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mPresShell = do_GetWeakReference(inPresShell); // the presshell owns us, so no addref
|
|
|
|
NS_ASSERTION(mPresShell, "Hey, pres shell should support weak refs");
|
|
|
|
|
2011-09-08 19:27:13 -07:00
|
|
|
mShowDuringSelection =
|
|
|
|
LookAndFeel::GetInt(LookAndFeel::eIntID_ShowCaretDuringSelection,
|
|
|
|
mShowDuringSelection ? 1 : 0) != 0;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// get the selection from the pres shell, and set ourselves up as a selection
|
|
|
|
// listener
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionController> selCon = do_QueryReferent(mPresShell);
|
|
|
|
if (!selCon)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
|
|
|
nsresult rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!domSelection)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection = do_QueryInterface(domSelection);
|
|
|
|
if (privateSelection)
|
|
|
|
privateSelection->AddSelectionListener(this);
|
|
|
|
mDomSelectionWeak = do_GetWeakReference(domSelection);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2012-08-22 08:56:38 -07:00
|
|
|
DrawCJKCaret(nsIFrame* aFrame, int32_t aOffset)
|
2008-04-22 20:13:55 -07:00
|
|
|
{
|
|
|
|
nsIContent* content = aFrame->GetContent();
|
|
|
|
const nsTextFragment* frag = content->GetText();
|
|
|
|
if (!frag)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aOffset < 0 || uint32_t(aOffset) >= frag->GetLength())
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t ch = frag->CharAt(aOffset);
|
2008-04-22 20:13:55 -07:00
|
|
|
return 0x2e80 <= ch && ch <= 0xd7ff;
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
nsCaret::Metrics
|
|
|
|
nsCaret::ComputeMetrics(nsIFrame* aFrame, int32_t aOffset, nscoord aCaretHeight)
|
2008-04-14 14:09:23 -07:00
|
|
|
{
|
|
|
|
// Compute nominal sizes in appunits
|
2014-08-05 22:19:25 -07:00
|
|
|
nscoord caretWidth =
|
|
|
|
(aCaretHeight * LookAndFeel::GetFloat(LookAndFeel::eFloatID_CaretAspectRatio, 0.0f)) +
|
2014-08-05 22:19:25 -07:00
|
|
|
nsPresContext::CSSPixelsToAppUnits(
|
|
|
|
LookAndFeel::GetInt(LookAndFeel::eIntID_CaretWidth, 1));
|
2008-06-23 01:50:52 -07:00
|
|
|
|
2008-04-22 20:13:55 -07:00
|
|
|
if (DrawCJKCaret(aFrame, aOffset)) {
|
|
|
|
caretWidth += nsPresContext::CSSPixelsToAppUnits(1);
|
|
|
|
}
|
2008-04-14 14:09:23 -07:00
|
|
|
nscoord bidiIndicatorSize = nsPresContext::CSSPixelsToAppUnits(kMinBidiIndicatorPixels);
|
2013-01-15 04:22:03 -08:00
|
|
|
bidiIndicatorSize = std::max(caretWidth, bidiIndicatorSize);
|
2008-04-14 14:09:23 -07:00
|
|
|
|
|
|
|
// Round them to device pixels. Always round down, except that anything
|
|
|
|
// between 0 and 1 goes up to 1 so we don't let the caret disappear.
|
2013-02-19 13:46:27 -08:00
|
|
|
int32_t tpp = aFrame->PresContext()->AppUnitsPerDevPixel();
|
2008-04-14 14:09:23 -07:00
|
|
|
Metrics result;
|
|
|
|
result.mCaretWidth = NS_ROUND_BORDER_TO_PIXELS(caretWidth, tpp);
|
|
|
|
result.mBidiIndicatorSize = NS_ROUND_BORDER_TO_PIXELS(bidiIndicatorSize, tpp);
|
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-16 03:52:01 -07:00
|
|
|
void nsCaret::Terminate()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// this doesn't erase the caret if it's drawn. Should it? We might not have
|
|
|
|
// a good drawing environment during teardown.
|
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
StopBlinking();
|
2012-07-30 07:20:58 -07:00
|
|
|
mBlinkTimer = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// unregiser ourselves as a selection listener
|
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(domSelection));
|
|
|
|
if (privateSelection)
|
|
|
|
privateSelection->RemoveSelectionListener(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDomSelectionWeak = nullptr;
|
|
|
|
mPresShell = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
mOverrideContent = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsCaret, nsISelectionListener)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:27 -07:00
|
|
|
nsISelection* nsCaret::GetSelection()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> sel(do_QueryReferent(mDomSelectionWeak));
|
2014-08-05 22:19:27 -07:00
|
|
|
return sel;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:27 -07:00
|
|
|
void nsCaret::SetSelection(nsISelection *aDOMSel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:27 -07:00
|
|
|
MOZ_ASSERT(aDOMSel);
|
2007-03-22 10:30:00 -07:00
|
|
|
mDomSelectionWeak = do_GetWeakReference(aDOMSel); // weak reference to pres shell
|
2014-08-05 22:19:30 -07:00
|
|
|
ResetBlinking();
|
2014-08-05 22:19:29 -07:00
|
|
|
SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
void nsCaret::SetVisible(bool inMakeVisible)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mVisible = inMakeVisible;
|
2014-08-05 22:19:30 -07:00
|
|
|
mIgnoreUserModify = mVisible;
|
|
|
|
ResetBlinking();
|
2014-08-05 22:19:29 -07:00
|
|
|
SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
bool nsCaret::IsVisible()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:29 -07:00
|
|
|
if (!mVisible) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mShowDuringSelection) {
|
|
|
|
Selection* selection = GetSelectionInternal();
|
|
|
|
if (!selection) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool isCollapsed;
|
|
|
|
if (NS_FAILED(selection->GetIsCollapsed(&isCollapsed)) || !isCollapsed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsMenuPopupHidingCaret()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void nsCaret::SetCaretReadOnly(bool inMakeReadonly)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mReadOnly = inMakeReadonly;
|
2014-08-05 22:19:30 -07:00
|
|
|
ResetBlinking();
|
2014-08-05 22:19:29 -07:00
|
|
|
SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:32 -07:00
|
|
|
/* static */ nsRect
|
2010-03-14 15:56:26 -07:00
|
|
|
nsCaret::GetGeometryForFrame(nsIFrame* aFrame,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aFrameOffset,
|
2010-03-14 15:56:26 -07:00
|
|
|
nscoord* aBidiIndicatorSize)
|
2010-01-11 13:45:16 -08:00
|
|
|
{
|
|
|
|
nsPoint framePos(0, 0);
|
2014-08-05 22:19:32 -07:00
|
|
|
nsRect rect;
|
2010-11-24 17:35:01 -08:00
|
|
|
nsresult rv = aFrame->GetPointFromOffset(aFrameOffset, &framePos);
|
2014-08-05 22:19:32 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (aBidiIndicatorSize) {
|
|
|
|
*aBidiIndicatorSize = 0;
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
}
|
2010-11-24 17:35:01 -08:00
|
|
|
|
2014-05-24 15:20:39 -07:00
|
|
|
nsIFrame* frame = aFrame->GetContentInsertionFrame();
|
|
|
|
if (!frame) {
|
|
|
|
frame = aFrame;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!(frame->GetStateBits() & NS_FRAME_IN_REFLOW),
|
|
|
|
"We should not be in the middle of reflow");
|
2010-11-22 00:13:37 -08:00
|
|
|
nscoord baseline = frame->GetCaretBaseline();
|
2010-08-31 11:54:44 -07:00
|
|
|
nscoord ascent = 0, descent = 0;
|
2011-04-07 18:05:49 -07:00
|
|
|
nsRefPtr<nsFontMetrics> fm;
|
2011-11-23 18:48:23 -08:00
|
|
|
nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm),
|
2012-05-20 22:18:28 -07:00
|
|
|
nsLayoutUtils::FontSizeInflationFor(aFrame));
|
2010-08-31 11:54:44 -07:00
|
|
|
NS_ASSERTION(fm, "We should be able to get the font metrics");
|
|
|
|
if (fm) {
|
2011-04-07 21:18:43 -07:00
|
|
|
ascent = fm->MaxAscent();
|
|
|
|
descent = fm->MaxDescent();
|
2010-03-14 15:52:45 -07:00
|
|
|
}
|
2010-08-31 11:54:44 -07:00
|
|
|
nscoord height = ascent + descent;
|
|
|
|
framePos.y = baseline - ascent;
|
2010-03-14 15:56:26 -07:00
|
|
|
Metrics caretMetrics = ComputeMetrics(aFrame, aFrameOffset, height);
|
2014-08-05 22:19:32 -07:00
|
|
|
rect = nsRect(framePos, nsSize(caretMetrics.mCaretWidth, height));
|
2010-03-14 15:52:45 -07:00
|
|
|
|
|
|
|
// Clamp the x-position to be within our scroll frame. If we don't, then it
|
|
|
|
// clips us, and we don't appear at all. See bug 335560.
|
|
|
|
nsIFrame *scrollFrame =
|
2010-03-14 15:56:26 -07:00
|
|
|
nsLayoutUtils::GetClosestFrameOfType(aFrame, nsGkAtoms::scrollFrame);
|
2010-03-14 15:52:45 -07:00
|
|
|
if (scrollFrame) {
|
2010-03-14 15:56:26 -07:00
|
|
|
// First, use the scrollFrame to get at the scrollable view that we're in.
|
2010-03-14 15:52:45 -07:00
|
|
|
nsIScrollableFrame *sf = do_QueryFrame(scrollFrame);
|
|
|
|
nsIFrame *scrolled = sf->GetScrolledFrame();
|
2014-08-05 22:19:32 -07:00
|
|
|
nsRect caretInScroll = rect + aFrame->GetOffsetTo(scrolled);
|
2010-03-14 15:52:45 -07:00
|
|
|
|
2010-03-14 15:56:26 -07:00
|
|
|
// Now see if thet caret extends beyond the view's bounds. If it does,
|
2010-03-14 15:52:45 -07:00
|
|
|
// then snap it back, put it as close to the edge as it can.
|
|
|
|
nscoord overflow = caretInScroll.XMost() -
|
2010-10-06 21:25:47 -07:00
|
|
|
scrolled->GetVisualOverflowRectRelativeToSelf().width;
|
2014-08-05 22:19:32 -07:00
|
|
|
if (overflow > 0) {
|
|
|
|
rect.x -= overflow;
|
|
|
|
}
|
2010-03-14 15:52:45 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:32 -07:00
|
|
|
if (aBidiIndicatorSize) {
|
2010-03-14 15:52:45 -07:00
|
|
|
*aBidiIndicatorSize = caretMetrics.mBidiIndicatorSize;
|
2014-08-05 22:19:32 -07:00
|
|
|
}
|
|
|
|
return rect;
|
2010-03-14 15:56:26 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
static nsIFrame*
|
|
|
|
GetFrameAndOffset(Selection* aSelection,
|
|
|
|
nsINode* aOverrideNode, int32_t aOverrideOffset,
|
|
|
|
int32_t* aFrameOffset)
|
2010-03-14 15:56:26 -07:00
|
|
|
{
|
2014-08-05 22:19:29 -07:00
|
|
|
nsINode* focusNode;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t focusOffset;
|
2014-08-05 22:19:29 -07:00
|
|
|
|
|
|
|
if (aOverrideNode) {
|
|
|
|
focusNode = aOverrideNode;
|
|
|
|
focusOffset = aOverrideOffset;
|
2014-08-05 22:19:31 -07:00
|
|
|
} else if (aSelection) {
|
2014-08-05 22:19:29 -07:00
|
|
|
focusNode = aSelection->GetFocusNode();
|
|
|
|
aSelection->GetFocusOffset(&focusOffset);
|
2014-08-05 22:19:31 -07:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
2014-08-05 22:19:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!focusNode || !focusNode->IsContent()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2014-08-05 22:19:29 -07:00
|
|
|
}
|
2010-03-14 15:56:26 -07:00
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
nsIContent* contentNode = focusNode->AsContent();
|
|
|
|
nsFrameSelection* frameSelection = aSelection->GetFrameSelection();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t bidiLevel = frameSelection->GetCaretBidiLevel();
|
2010-03-14 15:56:26 -07:00
|
|
|
nsIFrame* frame;
|
2014-08-05 22:19:29 -07:00
|
|
|
nsresult rv = nsCaret::GetCaretFrameForNodeOffset(
|
|
|
|
frameSelection, contentNode, focusOffset,
|
|
|
|
frameSelection->GetHint(), bidiLevel, &frame, aFrameOffset);
|
|
|
|
if (NS_FAILED(rv) || !frame) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2014-08-05 22:19:29 -07:00
|
|
|
}
|
2010-03-14 15:56:26 -07:00
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ nsIFrame*
|
|
|
|
nsCaret::GetGeometry(nsISelection* aSelection, nsRect* aRect)
|
|
|
|
{
|
|
|
|
int32_t frameOffset;
|
|
|
|
nsIFrame* frame = GetFrameAndOffset(
|
|
|
|
static_cast<Selection*>(aSelection), nullptr, 0, &frameOffset);
|
|
|
|
if (frame) {
|
2014-08-05 22:19:32 -07:00
|
|
|
*aRect = GetGeometryForFrame(frame, frameOffset, nullptr);
|
2014-08-05 22:19:29 -07:00
|
|
|
}
|
2010-03-14 15:56:26 -07:00
|
|
|
return frame;
|
2010-01-11 13:45:16 -08:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:27 -07:00
|
|
|
Selection*
|
|
|
|
nsCaret::GetSelectionInternal()
|
|
|
|
{
|
|
|
|
return static_cast<Selection*>(GetSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsCaret::SchedulePaint()
|
|
|
|
{
|
2014-08-05 22:19:31 -07:00
|
|
|
Selection* selection = GetSelectionInternal();
|
|
|
|
nsINode* focusNode;
|
|
|
|
if (mOverrideContent) {
|
|
|
|
focusNode = mOverrideContent;
|
|
|
|
} else if (selection) {
|
|
|
|
focusNode = selection->GetFocusNode();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-05 22:19:27 -07:00
|
|
|
if (!focusNode || !focusNode->IsContent()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsIFrame* f = focusNode->AsContent()->GetPrimaryFrame();
|
|
|
|
if (!f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This may not be the correct continuation frame, but that's OK since we're
|
|
|
|
// just scheduling a paint of the window (or popup).
|
|
|
|
f->SchedulePaint();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void nsCaret::SetVisibilityDuringSelection(bool aVisibility)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mShowDuringSelection = aVisibility;
|
2014-08-05 22:19:29 -07:00
|
|
|
SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:31 -07:00
|
|
|
void
|
|
|
|
nsCaret::SetCaretPosition(nsIDOMNode* aNode, int32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:30 -07:00
|
|
|
mOverrideContent = do_QueryInterface(aNode);
|
|
|
|
mOverrideOffset = aOffset;
|
2014-08-05 22:19:29 -07:00
|
|
|
|
2014-08-05 22:19:31 -07:00
|
|
|
ResetBlinking();
|
2014-08-05 22:19:29 -07:00
|
|
|
SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
void
|
|
|
|
nsCaret::CheckSelectionLanguageChange()
|
|
|
|
{
|
2014-08-12 06:27:02 -07:00
|
|
|
if (!IsBidiUI()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
bool isKeyboardRTL = false;
|
|
|
|
nsIBidiKeyboard* bidiKeyboard = nsContentUtils::GetBidiKeyboard();
|
2014-08-12 06:27:02 -07:00
|
|
|
if (bidiKeyboard) {
|
|
|
|
bidiKeyboard->IsLangRTL(&isKeyboardRTL);
|
|
|
|
}
|
|
|
|
// Call SelectionLanguageChange on every paint. Mostly it will be a noop
|
|
|
|
// but it should be fast anyway. This guarantees we never paint the caret
|
|
|
|
// at the wrong place.
|
|
|
|
Selection* selection = GetSelectionInternal();
|
|
|
|
if (selection) {
|
|
|
|
selection->SelectionLanguageChange(isKeyboardRTL);
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:28 -07:00
|
|
|
nsIFrame*
|
|
|
|
nsCaret::GetPaintGeometry(nsRect* aRect)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:29 -07:00
|
|
|
// Return null if we should not be visible.
|
2014-08-05 22:19:31 -07:00
|
|
|
if (!IsVisible() || !mIsBlinkOn) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2014-08-05 22:19:29 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
// Update selection language direction now so the new direction will be
|
|
|
|
// taken into account when computing the caret position below.
|
|
|
|
CheckSelectionLanguageChange();
|
|
|
|
|
2014-08-05 22:19:29 -07:00
|
|
|
int32_t frameOffset;
|
|
|
|
nsIFrame *frame = GetFrameAndOffset(GetSelectionInternal(),
|
2014-08-05 22:19:30 -07:00
|
|
|
mOverrideContent, mOverrideOffset, &frameOffset);
|
2014-08-05 22:19:29 -07:00
|
|
|
if (!frame) {
|
2014-08-05 22:19:29 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we have a frame, check whether it's appropriate to show the caret here
|
|
|
|
const nsStyleUserInterface* userinterface = frame->StyleUserInterface();
|
|
|
|
if ((!mIgnoreUserModify &&
|
|
|
|
userinterface->mUserModify == NS_STYLE_USER_MODIFY_READ_ONLY) ||
|
|
|
|
userinterface->mUserInput == NS_STYLE_USER_INPUT_NONE ||
|
|
|
|
userinterface->mUserInput == NS_STYLE_USER_INPUT_DISABLED) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2014-08-05 22:19:29 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:28 -07:00
|
|
|
// If the offset falls outside of the frame, then don't paint the caret.
|
|
|
|
int32_t startOffset, endOffset;
|
|
|
|
if (frame->GetType() == nsGkAtoms::textFrame &&
|
|
|
|
(NS_FAILED(frame->GetOffsets(startOffset, endOffset)) ||
|
2014-08-05 22:19:29 -07:00
|
|
|
startOffset > frameOffset ||
|
|
|
|
endOffset < frameOffset)) {
|
2014-08-05 22:19:28 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRect caretRect;
|
|
|
|
nsRect hookRect;
|
2014-08-05 22:19:29 -07:00
|
|
|
ComputeCaretRects(frame, frameOffset, &caretRect, &hookRect);
|
2014-08-05 22:19:28 -07:00
|
|
|
|
|
|
|
aRect->UnionRect(caretRect, hookRect);
|
2014-08-05 22:19:28 -07:00
|
|
|
return frame;
|
2014-08-05 22:19:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void nsCaret::PaintCaret(nsDisplayListBuilder *aBuilder,
|
2011-04-07 18:04:40 -07:00
|
|
|
nsRenderingContext *aCtx,
|
2008-06-23 01:50:52 -07:00
|
|
|
nsIFrame* aForFrame,
|
|
|
|
const nsPoint &aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:28 -07:00
|
|
|
int32_t contentOffset;
|
2014-08-05 22:19:29 -07:00
|
|
|
nsIFrame* frame = GetFrameAndOffset(GetSelectionInternal(),
|
2014-08-05 22:19:30 -07:00
|
|
|
mOverrideContent, mOverrideOffset, &contentOffset);
|
2014-08-05 22:19:29 -07:00
|
|
|
if (!frame) {
|
2014-08-05 22:19:28 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-03-31 19:35:48 -07:00
|
|
|
NS_ASSERTION(frame == aForFrame, "We're referring different frame");
|
2014-08-05 22:19:28 -07:00
|
|
|
|
2010-03-31 19:35:48 -07:00
|
|
|
nscolor foregroundColor = aForFrame->GetCaretColorAt(contentOffset);
|
|
|
|
aCtx->SetColor(foregroundColor);
|
2014-08-05 22:19:28 -07:00
|
|
|
|
|
|
|
nsRect caretRect;
|
|
|
|
nsRect hookRect;
|
|
|
|
ComputeCaretRects(frame, contentOffset, &caretRect, &hookRect);
|
|
|
|
aCtx->FillRect(caretRect + aOffset);
|
|
|
|
if (!hookRect.IsEmpty()) {
|
|
|
|
aCtx->FillRect(hookRect + aOffset);
|
2014-08-05 22:19:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCaret::NotifySelectionChanged(nsIDOMDocument *, nsISelection *aDomSel,
|
|
|
|
int16_t aReason)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aReason & nsISelectionListener::MOUSEUP_REASON)//this wont do
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSel(do_QueryReferent(mDomSelectionWeak));
|
|
|
|
|
|
|
|
// The same caret is shared amongst the document and any text widgets it
|
|
|
|
// may contain. This means that the caret could get notifications from
|
|
|
|
// multiple selections.
|
|
|
|
//
|
|
|
|
// If this notification is for a selection that is not the one the
|
|
|
|
// the caret is currently interested in (mDomSelectionWeak), then there
|
|
|
|
// is nothing to do!
|
|
|
|
|
|
|
|
if (domSel != aDomSel)
|
|
|
|
return NS_OK;
|
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
ResetBlinking();
|
|
|
|
SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
void nsCaret::ResetBlinking()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:31 -07:00
|
|
|
mIsBlinkOn = true;
|
|
|
|
|
2014-09-06 03:25:00 -07:00
|
|
|
if (mReadOnly || !mVisible) {
|
2014-08-05 22:19:30 -07:00
|
|
|
StopBlinking();
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:31 -07:00
|
|
|
if (mBlinkTimer) {
|
|
|
|
mBlinkTimer->Cancel();
|
|
|
|
} else {
|
2014-08-05 22:19:30 -07:00
|
|
|
nsresult err;
|
|
|
|
mBlinkTimer = do_CreateInstance("@mozilla.org/timer;1", &err);
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:30 -07:00
|
|
|
uint32_t blinkRate = static_cast<uint32_t>(
|
|
|
|
LookAndFeel::GetInt(LookAndFeel::eIntID_CaretBlinkTime, 500));
|
2014-08-05 22:19:31 -07:00
|
|
|
if (blinkRate > 0) {
|
|
|
|
mBlinkTimer->InitWithFuncCallback(CaretBlinkCallback, this, blinkRate,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-16 03:52:01 -07:00
|
|
|
void nsCaret::StopBlinking()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:30 -07:00
|
|
|
if (mBlinkTimer)
|
|
|
|
{
|
|
|
|
mBlinkTimer->Cancel();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-16 03:52:01 -07:00
|
|
|
nsresult
|
2014-08-05 22:19:27 -07:00
|
|
|
nsCaret::GetCaretFrameForNodeOffset(nsFrameSelection* aFrameSelection,
|
|
|
|
nsIContent* aContentNode,
|
|
|
|
int32_t aOffset,
|
|
|
|
CaretAssociationHint aFrameHint,
|
|
|
|
uint8_t aBidiLevel,
|
|
|
|
nsIFrame** aReturnFrame,
|
|
|
|
int32_t* aReturnOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:24 -07:00
|
|
|
if (!aFrameSelection)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsIPresShell* presShell = aFrameSelection->GetShell();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!presShell)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2014-08-05 16:23:02 -07:00
|
|
|
if (!aContentNode || !aContentNode->IsInComposedDoc() ||
|
|
|
|
presShell->GetDocument() != aContentNode->GetComposedDoc())
|
2010-11-24 17:35:01 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIFrame* theFrame = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t theFrameOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:24 -07:00
|
|
|
theFrame = aFrameSelection->GetFrameForNodeOffset(
|
|
|
|
aContentNode, aOffset, aFrameHint, &theFrameOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!theFrame)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-11-07 20:07:00 -08:00
|
|
|
// if theFrame is after a text frame that's logically at the end of the line
|
|
|
|
// (e.g. if theFrame is a <br> frame), then put the caret at the end of
|
|
|
|
// that text frame instead. This way, the caret will be positioned as if
|
|
|
|
// trailing whitespace was not trimmed.
|
|
|
|
AdjustCaretFrameForLineEnd(&theFrame, &theFrameOffset);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Mamdouh : modification of the caret to work at rtl and ltr with Bidi
|
|
|
|
//
|
2013-02-16 13:51:03 -08:00
|
|
|
// Direction Style from visibility->mDirection
|
2007-03-22 10:30:00 -07:00
|
|
|
// ------------------
|
|
|
|
// NS_STYLE_DIRECTION_LTR : LTR or Default
|
|
|
|
// NS_STYLE_DIRECTION_RTL
|
|
|
|
// NS_STYLE_DIRECTION_INHERIT
|
2014-08-05 22:19:24 -07:00
|
|
|
if (IsBidiUI())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// If there has been a reflow, take the caret Bidi level to be the level of the current frame
|
|
|
|
if (aBidiLevel & BIDI_LEVEL_UNDEFINED)
|
|
|
|
aBidiLevel = NS_GET_EMBEDDING_LEVEL(theFrame);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t start;
|
|
|
|
int32_t end;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIFrame* frameBefore;
|
|
|
|
nsIFrame* frameAfter;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t levelBefore; // Bidi level of the character before the caret
|
|
|
|
uint8_t levelAfter; // Bidi level of the character after the caret
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
if (start == 0 || end == 0 || start == theFrameOffset || end == theFrameOffset)
|
|
|
|
{
|
2014-08-05 22:19:24 -07:00
|
|
|
nsPrevNextBidiLevels levels = aFrameSelection->
|
2011-10-17 07:59:28 -07:00
|
|
|
GetPrevNextBidiLevels(aContentNode, aOffset, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Boundary condition, we need to know the Bidi levels of the characters before and after the caret */
|
|
|
|
if (levels.mFrameBefore || levels.mFrameAfter)
|
|
|
|
{
|
|
|
|
frameBefore = levels.mFrameBefore;
|
|
|
|
frameAfter = levels.mFrameAfter;
|
|
|
|
levelBefore = levels.mLevelBefore;
|
|
|
|
levelAfter = levels.mLevelAfter;
|
|
|
|
|
|
|
|
if ((levelBefore != levelAfter) || (aBidiLevel != levelBefore))
|
|
|
|
{
|
2013-01-15 04:22:03 -08:00
|
|
|
aBidiLevel = std::max(aBidiLevel, std::min(levelBefore, levelAfter)); // rule c3
|
|
|
|
aBidiLevel = std::min(aBidiLevel, std::max(levelBefore, levelAfter)); // rule c4
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aBidiLevel == levelBefore // rule c1
|
2008-09-15 08:40:25 -07:00
|
|
|
|| (aBidiLevel > levelBefore && aBidiLevel < levelAfter && !((aBidiLevel ^ levelBefore) & 1)) // rule c5
|
|
|
|
|| (aBidiLevel < levelBefore && aBidiLevel > levelAfter && !((aBidiLevel ^ levelBefore) & 1))) // rule c9
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (theFrame != frameBefore)
|
|
|
|
{
|
|
|
|
if (frameBefore) // if there is a frameBefore, move into it
|
|
|
|
{
|
|
|
|
theFrame = frameBefore;
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrameOffset = end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if there is no frameBefore, we must be at the beginning of the line
|
|
|
|
// so we stay with the current frame.
|
|
|
|
// Exception: when the first frame on the line has a different Bidi level from the paragraph level, there is no
|
|
|
|
// real frame for the caret to be in. We have to find the visually first frame on the line.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t baseLevel = NS_GET_BASE_LEVEL(frameAfter);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (baseLevel != levelAfter)
|
|
|
|
{
|
2012-03-26 09:09:38 -07:00
|
|
|
nsPeekOffsetStruct pos(eSelectBeginLine, eDirPrevious, 0, 0, false, true, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(frameAfter->PeekOffset(&pos))) {
|
|
|
|
theFrame = pos.mResultFrame;
|
|
|
|
theFrameOffset = pos.mContentOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (aBidiLevel == levelAfter // rule c2
|
2008-09-15 08:40:25 -07:00
|
|
|
|| (aBidiLevel > levelBefore && aBidiLevel < levelAfter && !((aBidiLevel ^ levelAfter) & 1)) // rule c6
|
|
|
|
|| (aBidiLevel < levelBefore && aBidiLevel > levelAfter && !((aBidiLevel ^ levelAfter) & 1))) // rule c10
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (theFrame != frameAfter)
|
|
|
|
{
|
|
|
|
if (frameAfter)
|
|
|
|
{
|
|
|
|
// if there is a frameAfter, move into it
|
|
|
|
theFrame = frameAfter;
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrameOffset = start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if there is no frameAfter, we must be at the end of the line
|
|
|
|
// so we stay with the current frame.
|
|
|
|
// Exception: when the last frame on the line has a different Bidi level from the paragraph level, there is no
|
|
|
|
// real frame for the caret to be in. We have to find the visually last frame on the line.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t baseLevel = NS_GET_BASE_LEVEL(frameBefore);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (baseLevel != levelBefore)
|
|
|
|
{
|
2012-03-26 09:09:38 -07:00
|
|
|
nsPeekOffsetStruct pos(eSelectEndLine, eDirNext, 0, 0, false, true, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(frameBefore->PeekOffset(&pos))) {
|
|
|
|
theFrame = pos.mResultFrame;
|
|
|
|
theFrameOffset = pos.mContentOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (aBidiLevel > levelBefore && aBidiLevel < levelAfter // rule c7/8
|
|
|
|
&& !((levelBefore ^ levelAfter) & 1) // before and after have the same parity
|
|
|
|
&& ((aBidiLevel ^ levelAfter) & 1)) // caret has different parity
|
|
|
|
{
|
2014-08-05 22:19:24 -07:00
|
|
|
if (NS_SUCCEEDED(aFrameSelection->GetFrameFromLevel(frameAfter, eDirNext, aBidiLevel, &theFrame)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
levelAfter = NS_GET_EMBEDDING_LEVEL(theFrame);
|
|
|
|
if (aBidiLevel & 1) // c8: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? start : end;
|
|
|
|
else // c7: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? end : start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (aBidiLevel < levelBefore && aBidiLevel > levelAfter // rule c11/12
|
|
|
|
&& !((levelBefore ^ levelAfter) & 1) // before and after have the same parity
|
|
|
|
&& ((aBidiLevel ^ levelAfter) & 1)) // caret has different parity
|
|
|
|
{
|
2014-08-05 22:19:24 -07:00
|
|
|
if (NS_SUCCEEDED(aFrameSelection->GetFrameFromLevel(frameBefore, eDirPrevious, aBidiLevel, &theFrame)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
levelBefore = NS_GET_EMBEDDING_LEVEL(theFrame);
|
|
|
|
if (aBidiLevel & 1) // c12: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? end : start;
|
|
|
|
else // c11: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? start : end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-24 17:35:01 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*aReturnFrame = theFrame;
|
|
|
|
*aReturnOffset = theFrameOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-22 15:02:59 -07:00
|
|
|
size_t nsCaret::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t total = aMallocSizeOf(this);
|
|
|
|
if (mPresShell) {
|
|
|
|
// We only want the size of the nsWeakReference object, not the PresShell
|
|
|
|
// (since we don't own the PresShell).
|
|
|
|
total += mPresShell->SizeOfOnlyThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
if (mDomSelectionWeak) {
|
|
|
|
// We only want size of the nsWeakReference object, not the selection
|
|
|
|
// (again, we don't own the selection).
|
|
|
|
total += mDomSelectionWeak->SizeOfOnlyThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
if (mBlinkTimer) {
|
|
|
|
total += mBlinkTimer->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsCaret::IsMenuPopupHidingCaret()
|
2008-04-01 01:35:11 -07:00
|
|
|
{
|
2008-09-08 05:23:38 -07:00
|
|
|
#ifdef MOZ_XUL
|
2008-04-01 01:35:11 -07:00
|
|
|
// Check if there are open popups.
|
|
|
|
nsXULPopupManager *popMgr = nsXULPopupManager::GetInstance();
|
2012-11-29 08:14:13 -08:00
|
|
|
nsTArray<nsIFrame*> popups;
|
|
|
|
popMgr->GetVisiblePopups(popups);
|
2008-04-01 01:35:11 -07:00
|
|
|
|
|
|
|
if (popups.Length() == 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false; // No popups, so caret can't be hidden by them.
|
2008-04-01 01:35:11 -07:00
|
|
|
|
|
|
|
// Get the selection focus content, that's where the caret would
|
|
|
|
// go if it was drawn.
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
|
|
|
if (!domSelection)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true; // No selection/caret to draw.
|
2008-04-01 01:35:11 -07:00
|
|
|
domSelection->GetFocusNode(getter_AddRefs(node));
|
|
|
|
if (!node)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true; // No selection/caret to draw.
|
2008-04-01 01:35:11 -07:00
|
|
|
nsCOMPtr<nsIContent> caretContent = do_QueryInterface(node);
|
2008-04-08 23:16:09 -07:00
|
|
|
if (!caretContent)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true; // No selection/caret to draw.
|
2008-04-01 01:35:11 -07:00
|
|
|
|
|
|
|
// If there's a menu popup open before the popup with
|
|
|
|
// the caret, don't show the caret.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i=0; i<popups.Length(); i++) {
|
2008-04-01 01:35:11 -07:00
|
|
|
nsMenuPopupFrame* popupFrame = static_cast<nsMenuPopupFrame*>(popups[i]);
|
|
|
|
nsIContent* popupContent = popupFrame->GetContent();
|
|
|
|
|
|
|
|
if (nsContentUtils::ContentIsDescendantOf(caretContent, popupContent)) {
|
|
|
|
// The caret is in this popup. There were no menu popups before this
|
|
|
|
// popup, so don't hide the caret.
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-04-01 01:35:11 -07:00
|
|
|
}
|
|
|
|
|
2008-04-30 09:54:16 -07:00
|
|
|
if (popupFrame->PopupType() == ePopupTypeMenu && !popupFrame->IsContextMenu()) {
|
2008-04-01 01:35:11 -07:00
|
|
|
// This is an open menu popup. It does not contain the caret (else we'd
|
|
|
|
// have returned above). Even if the caret is in a subsequent popup,
|
|
|
|
// or another document/frame, it should be hidden.
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-04-01 01:35:11 -07:00
|
|
|
}
|
|
|
|
}
|
2008-09-08 05:23:38 -07:00
|
|
|
#endif
|
2008-04-01 01:35:11 -07:00
|
|
|
|
|
|
|
// There are no open menu popups, no need to hide the caret.
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-04-01 01:35:11 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-05 22:19:28 -07:00
|
|
|
void
|
|
|
|
nsCaret::ComputeCaretRects(nsIFrame* aFrame, int32_t aFrameOffset,
|
|
|
|
nsRect* aCaretRect, nsRect* aHookRect)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aFrame, "Should have a frame here");
|
|
|
|
|
2010-03-14 15:52:45 -07:00
|
|
|
nscoord bidiIndicatorSize;
|
2014-08-05 22:19:32 -07:00
|
|
|
*aCaretRect = GetGeometryForFrame(aFrame, aFrameOffset, &bidiIndicatorSize);
|
2007-08-13 13:51:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// on RTL frames the right edge of mCaretRect must be equal to framePos
|
2013-02-16 13:51:02 -08:00
|
|
|
const nsStyleVisibility* vis = aFrame->StyleVisibility();
|
2014-08-05 22:19:28 -07:00
|
|
|
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {
|
|
|
|
aCaretRect->x -= aCaretRect->width;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-12 06:27:02 -07:00
|
|
|
// Simon -- make a hook to draw to the left or right of the caret to show keyboard language direction
|
2014-08-05 22:19:28 -07:00
|
|
|
aHookRect->SetEmpty();
|
2014-08-12 06:27:02 -07:00
|
|
|
if (!IsBidiUI()) {
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
bool isCaretRTL;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIBidiKeyboard* bidiKeyboard = nsContentUtils::GetBidiKeyboard();
|
2014-08-12 06:27:02 -07:00
|
|
|
// if bidiKeyboard->IsLangRTL() fails, there is no way to tell the
|
|
|
|
// keyboard direction, or the user has no right-to-left keyboard
|
|
|
|
// installed, so we never draw the hook.
|
|
|
|
if (bidiKeyboard && NS_SUCCEEDED(bidiKeyboard->IsLangRTL(&isCaretRTL))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// If keyboard language is RTL, draw the hook on the left; if LTR, to the right
|
|
|
|
// The height of the hook rectangle is the same as the width of the caret
|
|
|
|
// rectangle.
|
Bug 1048752. Part 22: Factor out SelectionLanguageChange call to run early in nsCaret::GetPaintGeometry. r=tn
This code is somewhat tricky. nsCaret::ComputeCaretRects was checking to see
if we have a bidi keyboard, and if so, what direction it's set to.
If the direction changed from the last direction seen for *this caret*,
we fired a SelectionLanguageChange notification on the caret's current
Selection. This looked bogus because the caret can be switched between
selections so it would seem some selections won't get a notification when
they should, but that's how it was. Also, when the SelectionLanguageChange
notification fired we then didn't draw the caret in that iteration, which
seems even more bogus.
This patch fixes all that by moving the logic to fire SelectionLanguageChange
out to GetPaintGeometry and firing the notification every single time without
trying to detect whether the state has changed or not. I carefully examined
the implementation of SelectionLanguageChange and I'm pretty sure it's
idempotent so this should be correct. That doesn't look like an
expensive function, and runs at most once per window paint, so I'm not
worried about perf. Because we now fire SelectionLanguageChange before
reading selection or frame state, it should be fine to carry on after
calling SelectionLanguageChange and drawing the caret based on whatever
changes SelectionLanguageChange has performed.
This also lets us remove mKeyboardRTL, which as noted above seems inherently
bogus.
--HG--
extra : rebase_source : 3ddfd10f6f30033e090e72b4bb43f2695218752e
2014-08-05 22:19:28 -07:00
|
|
|
aHookRect->SetRect(aCaretRect->x + (isCaretRTL ? bidiIndicatorSize * -1 : aCaretRect->width),
|
2014-08-05 22:19:28 -07:00
|
|
|
aCaretRect->y + bidiIndicatorSize,
|
|
|
|
bidiIndicatorSize,
|
|
|
|
aCaretRect->width);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2014-08-05 22:19:29 -07:00
|
|
|
void nsCaret::CaretBlinkCallback(nsITimer* aTimer, void* aClosure)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-08-05 22:19:29 -07:00
|
|
|
nsCaret* theCaret = reinterpret_cast<nsCaret*>(aClosure);
|
|
|
|
if (!theCaret) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-05 22:19:29 -07:00
|
|
|
theCaret->mIsBlinkOn = !theCaret->mIsBlinkOn;
|
2014-08-05 22:19:29 -07:00
|
|
|
theCaret->SchedulePaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-10 06:19:13 -07:00
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsCaret::SetIgnoreUserModify(bool aIgnoreUserModify)
|
2007-08-10 06:19:13 -07:00
|
|
|
{
|
|
|
|
mIgnoreUserModify = aIgnoreUserModify;
|
2014-08-05 22:19:29 -07:00
|
|
|
SchedulePaint();
|
2007-08-10 06:19:13 -07:00
|
|
|
}
|