2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2006-04-18 03:16:46 +04:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2012-05-21 15:12:37 +04: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/. */
|
1999-02-12 02:49:08 +03:00
|
|
|
|
2006-03-30 09:56:38 +04:00
|
|
|
/* the caret is the text cursor used, e.g., when editing */
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
#include "nsITimer.h"
|
|
|
|
|
2006-04-26 05:57:22 +04:00
|
|
|
#include "nsFrameSelection.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIFrame.h"
|
2007-08-14 00:51:16 +04:00
|
|
|
#include "nsIScrollableFrame.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIDOMNode.h"
|
2000-09-14 15:45:01 +04:00
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIPresShell.h"
|
2011-04-08 05:04:40 +04:00
|
|
|
#include "nsRenderingContext.h"
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
1999-09-15 03:41:19 +04:00
|
|
|
#include "nsBlockFrame.h"
|
2000-04-27 11:37:12 +04:00
|
|
|
#include "nsISelectionController.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsCaret.h"
|
2007-11-08 07:07:00 +03:00
|
|
|
#include "nsTextFrame.h"
|
2008-04-01 12:35:11 +04:00
|
|
|
#include "nsXULPopupManager.h"
|
|
|
|
#include "nsMenuPopupFrame.h"
|
2008-04-23 07:13:55 +04:00
|
|
|
#include "nsTextFragment.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-09 06:27:13 +04:00
|
|
|
#include "mozilla/LookAndFeel.h"
|
2013-02-16 00:09:28 +04:00
|
|
|
#include "mozilla/Selection.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
1999-02-12 02:49:08 +03:00
|
|
|
|
2004-10-13 06:24:38 +04: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
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kMinBidiIndicatorPixels = 2;
|
2004-10-13 06:24:38 +04:00
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
#ifdef IBMBIDI
|
2006-08-08 09:14:52 +04:00
|
|
|
#include "nsIBidiKeyboard.h"
|
|
|
|
#include "nsContentUtils.h"
|
2001-03-21 04:16:22 +03:00
|
|
|
#endif //IBMBIDI
|
|
|
|
|
2011-05-25 10:31:59 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2010-03-15 01:52:45 +03: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 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:52:45 +03:00
|
|
|
|
2011-08-25 00:54:30 +04:00
|
|
|
for (nsIFrame* f = aFrame->GetFirstPrincipalChild(); f; f = f->GetNextSibling())
|
2010-03-15 01:52:45 +03:00
|
|
|
{
|
|
|
|
nsIFrame* r = CheckForTrailingTextFrameRecursive(f, aStopAtFrame);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:52:45 +03: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-29 10:19:26 +04:00
|
|
|
bool isValid;
|
2010-03-15 01:52:45 +03:00
|
|
|
nsBlockInFlowLineIterator iter(blockParent, aFrame, &isValid);
|
2012-07-30 18:20:58 +04:00
|
|
|
return isValid ? iter.GetLine().get() : nullptr;
|
2010-03-15 01:52:45 +03:00
|
|
|
}
|
|
|
|
aFrame = parent;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:52:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-22 19:56:38 +04:00
|
|
|
AdjustCaretFrameForLineEnd(nsIFrame** aFrame, int32_t* aOffset)
|
2010-03-15 01:52:45 +03:00
|
|
|
{
|
|
|
|
nsLineBox* line = FindContainingLine(*aFrame);
|
|
|
|
if (!line)
|
|
|
|
return;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = line->GetChildCount();
|
2010-03-15 01:52:45 +03: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-10-09 00:41:19 +04:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
nsCaret::nsCaret()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mPresShell(nullptr)
|
2000-09-07 09:26:25 +04:00
|
|
|
, mBlinkRate(500)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mVisible(false)
|
|
|
|
, mDrawn(false)
|
|
|
|
, mPendingDraw(false)
|
|
|
|
, mReadOnly(false)
|
|
|
|
, mShowDuringSelection(false)
|
|
|
|
, mIgnoreUserModify(true)
|
2003-02-28 02:09:51 +03:00
|
|
|
#ifdef IBMBIDI
|
2011-10-17 18:59:28 +04:00
|
|
|
, mKeyboardRTL(false)
|
2008-12-28 15:08:22 +03:00
|
|
|
, mLastBidiLevel(0)
|
2003-02-28 02:09:51 +03:00
|
|
|
#endif
|
2008-12-28 15:08:22 +03:00
|
|
|
, mLastContentOffset(0)
|
|
|
|
, mLastHint(nsFrameSelection::HINTLEFT)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsCaret::~nsCaret()
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
KillTimer();
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
nsresult nsCaret::Init(nsIPresShell *inPresShell)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2003-02-28 02:09:51 +03:00
|
|
|
NS_ENSURE_ARG(inPresShell);
|
2008-07-16 14:52:01 +04:00
|
|
|
|
2003-07-25 23:06:59 +04:00
|
|
|
mPresShell = do_GetWeakReference(inPresShell); // the presshell owns us, so no addref
|
2003-02-28 02:09:51 +03:00
|
|
|
NS_ASSERTION(mPresShell, "Hey, pres shell should support weak refs");
|
2000-04-27 11:37:12 +04:00
|
|
|
|
2011-09-09 06:27:13 +04:00
|
|
|
// XXX we should just do this LookAndFeel consultation every time
|
2008-04-15 01:09:23 +04:00
|
|
|
// we need these values.
|
2011-09-09 06:27:13 +04:00
|
|
|
mCaretWidthCSSPx = LookAndFeel::GetInt(LookAndFeel::eIntID_CaretWidth, 1);
|
|
|
|
mCaretAspectRatio =
|
|
|
|
LookAndFeel::GetFloat(LookAndFeel::eFloatID_CaretAspectRatio, 0.0f);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
mBlinkRate = static_cast<uint32_t>(
|
2011-09-09 06:27:13 +04:00
|
|
|
LookAndFeel::GetInt(LookAndFeel::eIntID_CaretBlinkTime, mBlinkRate));
|
|
|
|
mShowDuringSelection =
|
|
|
|
LookAndFeel::GetInt(LookAndFeel::eIntID_ShowCaretDuringSelection,
|
|
|
|
mShowDuringSelection ? 1 : 0) != 0;
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
// get the selection from the pres shell, and set ourselves up as a selection
|
|
|
|
// listener
|
2003-02-28 02:09:51 +03:00
|
|
|
|
2000-04-27 11:37:12 +04:00
|
|
|
nsCOMPtr<nsISelectionController> selCon = do_QueryReferent(mPresShell);
|
2003-02-28 02:09:51 +03:00
|
|
|
if (!selCon)
|
2000-04-27 11:37:12 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2003-02-28 02:09:51 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
2006-04-18 03:16:46 +04:00
|
|
|
nsresult rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection));
|
2003-02-28 02:09:51 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!domSelection)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection = do_QueryInterface(domSelection);
|
|
|
|
if (privateSelection)
|
|
|
|
privateSelection->AddSelectionListener(this);
|
2003-07-25 23:06:59 +04:00
|
|
|
mDomSelectionWeak = do_GetWeakReference(domSelection);
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
// set up the blink timer
|
|
|
|
if (mVisible)
|
|
|
|
{
|
2008-07-16 14:52:01 +04:00
|
|
|
StartBlinking();
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
2008-12-14 12:05:39 +03:00
|
|
|
#ifdef IBMBIDI
|
2011-05-25 10:31:59 +04:00
|
|
|
mBidiUI = Preferences::GetBool("bidi.browser.ui");
|
2008-12-14 12:05:39 +03:00
|
|
|
#endif
|
2003-02-28 02:09:51 +03:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2012-08-22 19:56:38 +04:00
|
|
|
DrawCJKCaret(nsIFrame* aFrame, int32_t aOffset)
|
2008-04-23 07:13:55 +04:00
|
|
|
{
|
|
|
|
nsIContent* content = aFrame->GetContent();
|
|
|
|
const nsTextFragment* frag = content->GetText();
|
|
|
|
if (!frag)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-08-22 19:56:38 +04:00
|
|
|
if (aOffset < 0 || uint32_t(aOffset) >= frag->GetLength())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ch = frag->CharAt(aOffset);
|
2008-04-23 07:13:55 +04:00
|
|
|
return 0x2e80 <= ch && ch <= 0xd7ff;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCaret::Metrics nsCaret::ComputeMetrics(nsIFrame* aFrame, int32_t aOffset, nscoord aCaretHeight)
|
2008-04-15 01:09:23 +04:00
|
|
|
{
|
|
|
|
// Compute nominal sizes in appunits
|
2008-06-23 12:50:52 +04:00
|
|
|
nscoord caretWidth = (aCaretHeight * mCaretAspectRatio) +
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(mCaretWidthCSSPx);
|
|
|
|
|
2008-04-23 07:13:55 +04:00
|
|
|
if (DrawCJKCaret(aFrame, aOffset)) {
|
|
|
|
caretWidth += nsPresContext::CSSPixelsToAppUnits(1);
|
|
|
|
}
|
2008-04-15 01:09:23 +04:00
|
|
|
nscoord bidiIndicatorSize = nsPresContext::CSSPixelsToAppUnits(kMinBidiIndicatorPixels);
|
2013-01-15 16:22:03 +04:00
|
|
|
bidiIndicatorSize = std::max(caretWidth, bidiIndicatorSize);
|
2008-04-15 01:09:23 +04: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-20 01:46:27 +04:00
|
|
|
int32_t tpp = aFrame->PresContext()->AppUnitsPerDevPixel();
|
2008-04-15 01:09:23 +04:00
|
|
|
Metrics result;
|
|
|
|
result.mCaretWidth = NS_ROUND_BORDER_TO_PIXELS(caretWidth, tpp);
|
|
|
|
result.mBidiIndicatorSize = NS_ROUND_BORDER_TO_PIXELS(bidiIndicatorSize, tpp);
|
|
|
|
return result;
|
|
|
|
}
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
void nsCaret::Terminate()
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2006-04-18 03:16:46 +04:00
|
|
|
// this doesn't erase the caret if it's drawn. Should it? We might not have
|
|
|
|
// a good drawing environment during teardown.
|
2003-02-28 02:09:51 +03:00
|
|
|
|
|
|
|
KillTimer();
|
2012-07-30 18:20:58 +04:00
|
|
|
mBlinkTimer = nullptr;
|
2000-09-14 15:45:01 +04:00
|
|
|
|
2003-02-28 02:09:51 +03: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 18:20:58 +04:00
|
|
|
mDomSelectionWeak = nullptr;
|
|
|
|
mPresShell = nullptr;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mLastContent = nullptr;
|
2003-02-28 02:09:51 +03:00
|
|
|
}
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2003-02-28 02:09:51 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsCaret, nsISelectionListener)
|
1999-02-12 02:49:08 +03:00
|
|
|
|
2002-09-26 04:07:53 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
nsISelection* nsCaret::GetCaretDOMSelection()
|
2002-09-26 04:07:53 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> sel(do_QueryReferent(mDomSelectionWeak));
|
2008-07-16 14:52:01 +04:00
|
|
|
return sel;
|
2002-09-26 04:07:53 +04:00
|
|
|
}
|
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
nsresult nsCaret::SetCaretDOMSelection(nsISelection *aDOMSel)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-06-01 06:37:40 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aDOMSel);
|
2003-07-25 23:06:59 +04:00
|
|
|
mDomSelectionWeak = do_GetWeakReference(aDOMSel); // weak reference to pres shell
|
2005-10-12 22:40:38 +04:00
|
|
|
if (mVisible)
|
|
|
|
{
|
|
|
|
// Stop the caret from blinking in its previous location.
|
|
|
|
StopBlinking();
|
|
|
|
// Start the caret blinking in the new location.
|
|
|
|
StartBlinking();
|
|
|
|
}
|
2000-06-01 06:37:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsCaret::SetCaretVisible(bool inMakeVisible)
|
2000-06-01 06:37:40 +04:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
mVisible = inMakeVisible;
|
2008-02-27 04:49:46 +03:00
|
|
|
if (mVisible) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetIgnoreUserModify(true);
|
2011-02-11 00:12:53 +03:00
|
|
|
StartBlinking();
|
2008-02-27 04:49:46 +03:00
|
|
|
} else {
|
2008-07-16 14:52:01 +04:00
|
|
|
StopBlinking();
|
2011-10-17 18:59:28 +04:00
|
|
|
SetIgnoreUserModify(false);
|
2008-02-27 04:49:46 +03:00
|
|
|
}
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2003-02-28 02:09:51 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsCaret::GetCaretVisible(bool *outMakeVisible)
|
2000-06-01 06:37:40 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outMakeVisible);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outMakeVisible = (mVisible && MustDrawCaret(true));
|
2000-06-01 06:37:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsCaret::SetCaretReadOnly(bool inMakeReadonly)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
mReadOnly = inMakeReadonly;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2010-11-25 04:35:01 +03:00
|
|
|
nsresult
|
2010-03-15 01:56:26 +03:00
|
|
|
nsCaret::GetGeometryForFrame(nsIFrame* aFrame,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aFrameOffset,
|
2010-03-15 01:56:26 +03:00
|
|
|
nsRect* aRect,
|
|
|
|
nscoord* aBidiIndicatorSize)
|
2010-01-12 00:45:16 +03:00
|
|
|
{
|
|
|
|
nsPoint framePos(0, 0);
|
2010-11-25 04:35:01 +03:00
|
|
|
nsresult rv = aFrame->GetPointFromOffset(aFrameOffset, &framePos);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2010-11-22 11:13:37 +03:00
|
|
|
nsIFrame *frame = aFrame->GetContentInsertionFrame();
|
|
|
|
NS_ASSERTION(frame, "We should not be in the middle of reflow");
|
|
|
|
nscoord baseline = frame->GetCaretBaseline();
|
2010-08-31 22:54:44 +04:00
|
|
|
nscoord ascent = 0, descent = 0;
|
2011-04-08 05:05:49 +04:00
|
|
|
nsRefPtr<nsFontMetrics> fm;
|
2011-11-24 06:48:23 +04:00
|
|
|
nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm),
|
2012-05-21 09:18:28 +04:00
|
|
|
nsLayoutUtils::FontSizeInflationFor(aFrame));
|
2010-08-31 22:54:44 +04:00
|
|
|
NS_ASSERTION(fm, "We should be able to get the font metrics");
|
|
|
|
if (fm) {
|
2011-04-08 08:18:43 +04:00
|
|
|
ascent = fm->MaxAscent();
|
|
|
|
descent = fm->MaxDescent();
|
2010-03-15 01:52:45 +03:00
|
|
|
}
|
2010-08-31 22:54:44 +04:00
|
|
|
nscoord height = ascent + descent;
|
|
|
|
framePos.y = baseline - ascent;
|
2010-03-15 01:56:26 +03:00
|
|
|
Metrics caretMetrics = ComputeMetrics(aFrame, aFrameOffset, height);
|
2010-03-15 01:52:45 +03:00
|
|
|
*aRect = nsRect(framePos, nsSize(caretMetrics.mCaretWidth, height));
|
|
|
|
|
|
|
|
// 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-15 01:56:26 +03:00
|
|
|
nsLayoutUtils::GetClosestFrameOfType(aFrame, nsGkAtoms::scrollFrame);
|
2010-03-15 01:52:45 +03:00
|
|
|
if (scrollFrame) {
|
2010-03-15 01:56:26 +03:00
|
|
|
// First, use the scrollFrame to get at the scrollable view that we're in.
|
2010-03-15 01:52:45 +03:00
|
|
|
nsIScrollableFrame *sf = do_QueryFrame(scrollFrame);
|
|
|
|
nsIFrame *scrolled = sf->GetScrolledFrame();
|
2010-03-15 01:56:26 +03:00
|
|
|
nsRect caretInScroll = *aRect + aFrame->GetOffsetTo(scrolled);
|
2010-03-15 01:52:45 +03:00
|
|
|
|
2010-03-15 01:56:26 +03:00
|
|
|
// Now see if thet caret extends beyond the view's bounds. If it does,
|
2010-03-15 01:52:45 +03:00
|
|
|
// then snap it back, put it as close to the edge as it can.
|
|
|
|
nscoord overflow = caretInScroll.XMost() -
|
2010-10-07 08:25:47 +04:00
|
|
|
scrolled->GetVisualOverflowRectRelativeToSelf().width;
|
2010-03-15 01:52:45 +03:00
|
|
|
if (overflow > 0)
|
|
|
|
aRect->x -= overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBidiIndicatorSize)
|
|
|
|
*aBidiIndicatorSize = caretMetrics.mBidiIndicatorSize;
|
2010-11-25 04:35:01 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2010-03-15 01:56:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* nsCaret::GetGeometry(nsISelection* aSelection, nsRect* aRect,
|
|
|
|
nscoord* aBidiIndicatorSize)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
nsresult rv = aSelection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(rv) || !focusNode)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:56:26 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t focusOffset;
|
2010-03-15 01:56:26 +03:00
|
|
|
rv = aSelection->GetFocusOffset(&focusOffset);
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:56:26 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> contentNode = do_QueryInterface(focusNode);
|
|
|
|
if (!contentNode)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:56:26 +03:00
|
|
|
|
2011-05-19 07:10:49 +04:00
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
|
2010-03-15 01:56:26 +03:00
|
|
|
if (!frameSelection)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t bidiLevel = frameSelection->GetCaretBidiLevel();
|
2010-03-15 01:56:26 +03:00
|
|
|
nsIFrame* frame;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t frameOffset;
|
2010-03-15 01:56:26 +03:00
|
|
|
rv = GetCaretFrameForNodeOffset(contentNode, focusOffset,
|
|
|
|
frameSelection->GetHint(), bidiLevel,
|
|
|
|
&frame, &frameOffset);
|
|
|
|
if (NS_FAILED(rv) || !frame)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-15 01:56:26 +03:00
|
|
|
|
|
|
|
GetGeometryForFrame(frame, frameOffset, aRect, aBidiIndicatorSize);
|
|
|
|
return frame;
|
2010-01-12 00:45:16 +03:00
|
|
|
}
|
|
|
|
|
2005-10-04 07:14:03 +04:00
|
|
|
void nsCaret::DrawCaretAfterBriefDelay()
|
|
|
|
{
|
|
|
|
// Make sure readonly caret gets drawn again if it needs to be
|
|
|
|
if (!mBlinkTimer) {
|
|
|
|
nsresult err;
|
|
|
|
mBlinkTimer = do_CreateInstance("@mozilla.org/timer;1", &err);
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBlinkTimer->InitWithFuncCallback(CaretBlinkCallback, this, 0,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2008-07-16 14:52:01 +04:00
|
|
|
void nsCaret::EraseCaret()
|
2000-06-09 08:22:20 +04:00
|
|
|
{
|
2005-10-04 07:14:03 +04:00
|
|
|
if (mDrawn) {
|
2011-10-17 18:59:28 +04:00
|
|
|
DrawCaret(true);
|
2007-08-14 01:33:25 +04:00
|
|
|
if (mReadOnly && mBlinkRate) {
|
2005-10-04 07:14:03 +04:00
|
|
|
// If readonly we don't have a blink timer set, so caret won't
|
|
|
|
// be redrawn automatically. We need to force the caret to get
|
|
|
|
// redrawn right after the paint
|
|
|
|
DrawCaretAfterBriefDelay();
|
|
|
|
}
|
|
|
|
}
|
2000-06-09 08:22:20 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsCaret::SetVisibilityDuringSelection(bool aVisibility)
|
2003-02-28 02:09:51 +03:00
|
|
|
{
|
|
|
|
mShowDuringSelection = aVisibility;
|
|
|
|
}
|
|
|
|
|
2012-05-11 22:50:19 +04:00
|
|
|
static
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFrameSelection::HINT GetHintForPosition(nsIDOMNode* aNode, int32_t aOffset)
|
2012-05-11 22:50:19 +04:00
|
|
|
{
|
|
|
|
nsFrameSelection::HINT hint = nsFrameSelection::HINTLEFT;
|
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(aNode);
|
|
|
|
if (!node || aOffset < 1) {
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
const nsTextFragment* text = node->GetText();
|
|
|
|
if (text && text->CharAt(aOffset - 1) == '\n') {
|
|
|
|
// Attach the caret to the next line if needed
|
|
|
|
hint = nsFrameSelection::HINTRIGHT;
|
|
|
|
}
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsCaret::DrawAtPosition(nsIDOMNode* aNode, int32_t aOffset)
|
2003-02-28 02:09:51 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aNode);
|
2005-11-16 12:37:01 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t bidiLevel;
|
2011-05-19 07:10:49 +04:00
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
|
2006-08-08 09:14:52 +04:00
|
|
|
if (!frameSelection)
|
2006-07-21 03:26:05 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2006-08-08 09:14:52 +04:00
|
|
|
bidiLevel = frameSelection->GetCaretBidiLevel();
|
2007-08-14 01:33:25 +04:00
|
|
|
|
|
|
|
// DrawAtPosition is used by consumers who want us to stay drawn where they
|
|
|
|
// tell us. Setting mBlinkRate to 0 tells us to not set a timer to erase
|
|
|
|
// ourselves, our consumer will take care of that.
|
|
|
|
mBlinkRate = 0;
|
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
nsresult rv = DrawAtPositionWithHint(aNode, aOffset,
|
2012-05-11 22:50:19 +04:00
|
|
|
GetHintForPosition(aNode, aOffset),
|
2011-10-17 18:59:28 +04:00
|
|
|
bidiLevel, true)
|
2006-04-28 08:33:41 +04:00
|
|
|
? NS_OK : NS_ERROR_FAILURE;
|
2006-04-18 03:16:46 +04:00
|
|
|
ToggleDrawnStatus();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIFrame * nsCaret::GetCaretFrame(int32_t *aOffset)
|
2006-04-18 03:16:46 +04:00
|
|
|
{
|
|
|
|
// Return null if we're not drawn to prevent anybody from trying to draw us.
|
|
|
|
if (!mDrawn)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-04-18 03:16:46 +04:00
|
|
|
|
2010-09-09 04:15:24 +04:00
|
|
|
// Recompute the frame that we're supposed to draw in to guarantee that
|
|
|
|
// we're not going to try to draw into a stale (dead) frame.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame *frame = nullptr;
|
2006-04-18 03:16:46 +04:00
|
|
|
nsresult rv = GetCaretFrameForNodeOffset(mLastContent, mLastContentOffset,
|
|
|
|
mLastHint, mLastBidiLevel, &frame,
|
2010-04-01 06:35:48 +04:00
|
|
|
&offset);
|
2006-04-18 03:16:46 +04:00
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-04-18 03:16:46 +04:00
|
|
|
|
2010-04-01 06:35:48 +04:00
|
|
|
if (aOffset) {
|
|
|
|
*aOffset = offset;
|
|
|
|
}
|
2006-04-18 03:16:46 +04:00
|
|
|
return frame;
|
2003-02-28 02:09:51 +03:00
|
|
|
}
|
|
|
|
|
2006-04-18 03:16:46 +04:00
|
|
|
void nsCaret::InvalidateOutsideCaret()
|
|
|
|
{
|
|
|
|
nsIFrame *frame = GetCaretFrame();
|
|
|
|
|
|
|
|
// Only invalidate if we are not fully contained by our frame's rect.
|
2012-08-29 09:39:31 +04:00
|
|
|
if (frame && !frame->GetVisualOverflowRect().Contains(GetCaretRect())) {
|
|
|
|
frame->SchedulePaint();
|
|
|
|
}
|
2006-04-18 03:16:46 +04:00
|
|
|
}
|
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
void nsCaret::UpdateCaretPosition()
|
|
|
|
{
|
|
|
|
// We'll recalculate anyway if we're not drawn right now.
|
|
|
|
if (!mDrawn)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// A trick! Make the DrawCaret code recalculate the caret's current
|
|
|
|
// position.
|
2011-10-17 18:59:28 +04:00
|
|
|
mDrawn = false;
|
|
|
|
DrawCaret(false);
|
2006-04-28 08:33:41 +04:00
|
|
|
}
|
|
|
|
|
2006-04-18 03:16:46 +04:00
|
|
|
void nsCaret::PaintCaret(nsDisplayListBuilder *aBuilder,
|
2011-04-08 05:04:40 +04:00
|
|
|
nsRenderingContext *aCtx,
|
2008-06-23 12:50:52 +04:00
|
|
|
nsIFrame* aForFrame,
|
|
|
|
const nsPoint &aOffset)
|
2006-04-18 03:16:46 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDrawn, "The caret shouldn't be drawing");
|
|
|
|
|
2008-06-23 12:50:52 +04:00
|
|
|
const nsRect drawCaretRect = mCaretRect + aOffset;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t contentOffset;
|
2010-07-11 16:47:45 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsIFrame* frame =
|
|
|
|
#endif
|
|
|
|
GetCaretFrame(&contentOffset);
|
2010-04-01 06:35:48 +04:00
|
|
|
NS_ASSERTION(frame == aForFrame, "We're referring different frame");
|
2012-01-27 01:40:25 +04:00
|
|
|
// If the offset falls outside of the frame, then don't paint the caret.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset, endOffset;
|
2012-01-27 01:40:25 +04:00
|
|
|
if (aForFrame->GetType() == nsGkAtoms::textFrame &&
|
|
|
|
(NS_FAILED(aForFrame->GetOffsets(startOffset, endOffset)) ||
|
|
|
|
startOffset > contentOffset ||
|
|
|
|
endOffset < contentOffset)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-04-01 06:35:48 +04:00
|
|
|
nscolor foregroundColor = aForFrame->GetCaretColorAt(contentOffset);
|
2008-06-23 12:50:52 +04:00
|
|
|
|
2010-04-01 06:35:48 +04:00
|
|
|
aCtx->SetColor(foregroundColor);
|
2008-06-23 12:50:52 +04:00
|
|
|
aCtx->FillRect(drawCaretRect);
|
2006-04-18 03:16:46 +04:00
|
|
|
if (!GetHookRect().IsEmpty())
|
2006-04-21 23:13:58 +04:00
|
|
|
aCtx->FillRect(GetHookRect() + aOffset);
|
2006-04-18 03:16:46 +04:00
|
|
|
}
|
1999-04-02 03:57:35 +04:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsCaret::NotifySelectionChanged(nsIDOMDocument *, nsISelection *aDomSel, int16_t aReason)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
if (aReason & nsISelectionListener::MOUSEUP_REASON)//this wont do
|
2000-06-09 08:22:20 +04:00
|
|
|
return NS_OK;
|
2002-04-26 17:11:47 +04:00
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> domSel(do_QueryReferent(mDomSelectionWeak));
|
2002-04-26 17:11:47 +04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mVisible)
|
2002-04-26 17:11:47 +04:00
|
|
|
{
|
|
|
|
// Stop the caret from blinking in its previous location.
|
|
|
|
StopBlinking();
|
|
|
|
|
|
|
|
// Start the caret blinking in the new location.
|
2000-09-07 09:26:25 +04:00
|
|
|
StartBlinking();
|
2002-04-26 17:11:47 +04:00
|
|
|
}
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 07:26:49 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void nsCaret::KillTimer()
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mBlinkTimer)
|
|
|
|
{
|
|
|
|
mBlinkTimer->Cancel();
|
|
|
|
}
|
1999-03-02 07:26:49 +03:00
|
|
|
}
|
1999-02-12 02:49:08 +03:00
|
|
|
|
1999-02-17 05:08:01 +03:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-02-17 05:08:01 +03:00
|
|
|
nsresult nsCaret::PrimeTimer()
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
// set up the blink timer
|
|
|
|
if (!mReadOnly && mBlinkRate > 0)
|
|
|
|
{
|
2003-05-26 11:03:29 +04:00
|
|
|
if (!mBlinkTimer) {
|
|
|
|
nsresult err;
|
|
|
|
mBlinkTimer = do_CreateInstance("@mozilla.org/timer;1", &err);
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
mBlinkTimer->InitWithFuncCallback(CaretBlinkCallback, this, mBlinkRate,
|
2003-05-26 11:03:29 +04:00
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-02-17 05:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
void nsCaret::StartBlinking()
|
1999-02-17 05:08:01 +03:00
|
|
|
{
|
2005-10-04 07:14:03 +04:00
|
|
|
if (mReadOnly) {
|
|
|
|
// Make sure the one draw command we use for a readonly caret isn't
|
|
|
|
// done until the selection is set
|
|
|
|
DrawCaretAfterBriefDelay();
|
2008-07-16 14:52:01 +04:00
|
|
|
return;
|
2005-10-04 07:14:03 +04:00
|
|
|
}
|
2000-09-07 09:26:25 +04:00
|
|
|
PrimeTimer();
|
1999-03-03 04:11:28 +03:00
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
// If we are currently drawn, then the second call to DrawCaret below will
|
|
|
|
// actually erase the caret. That would cause the caret to spend an "off"
|
|
|
|
// cycle before it appears, which is not really what we want. This first
|
|
|
|
// call to DrawCaret makes sure that the first cycle after a call to
|
|
|
|
// StartBlinking is an "on" cycle.
|
|
|
|
if (mDrawn)
|
2011-10-17 18:59:28 +04:00
|
|
|
DrawCaret(true);
|
2006-04-28 08:33:41 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
DrawCaret(true); // draw it right away
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-07-16 14:52:01 +04:00
|
|
|
void nsCaret::StopBlinking()
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mDrawn) // erase the caret if necessary
|
2011-10-17 18:59:28 +04:00
|
|
|
DrawCaret(true);
|
2006-04-28 08:33:41 +04:00
|
|
|
|
2008-02-27 04:49:46 +03:00
|
|
|
NS_ASSERTION(!mDrawn, "Caret still drawn after StopBlinking().");
|
2000-09-07 09:26:25 +04:00
|
|
|
KillTimer();
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-08-11 07:44:16 +04:00
|
|
|
nsCaret::DrawAtPositionWithHint(nsIDOMNode* aNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aOffset,
|
2006-04-28 08:33:41 +04:00
|
|
|
nsFrameSelection::HINT aFrameHint,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aBidiLevel,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aInvalidate)
|
2006-04-18 03:16:46 +04:00
|
|
|
{
|
2003-02-28 02:09:51 +03:00
|
|
|
nsCOMPtr<nsIContent> contentNode = do_QueryInterface(aNode);
|
2005-08-11 07:44:16 +04:00
|
|
|
if (!contentNode)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-04-18 03:16:46 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame* theFrame = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t theFrameOffset = 0;
|
2005-11-16 12:37:01 +03:00
|
|
|
|
|
|
|
nsresult rv = GetCaretFrameForNodeOffset(contentNode, aOffset, aFrameHint, aBidiLevel,
|
|
|
|
&theFrame, &theFrameOffset);
|
|
|
|
if (NS_FAILED(rv) || !theFrame)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-06-28 06:48:16 +04:00
|
|
|
|
2005-11-16 12:37:01 +03:00
|
|
|
// now we have a frame, check whether it's appropriate to show the caret here
|
2013-02-17 01:51:02 +04:00
|
|
|
const nsStyleUserInterface* userinterface = theFrame->StyleUserInterface();
|
2007-08-10 17:19:13 +04:00
|
|
|
if ((!mIgnoreUserModify &&
|
|
|
|
userinterface->mUserModify == NS_STYLE_USER_MODIFY_READ_ONLY) ||
|
2005-11-16 12:37:01 +03:00
|
|
|
(userinterface->mUserInput == NS_STYLE_USER_INPUT_NONE) ||
|
|
|
|
(userinterface->mUserInput == NS_STYLE_USER_INPUT_DISABLED))
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-16 12:37:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDrawn)
|
|
|
|
{
|
2006-04-18 03:16:46 +04:00
|
|
|
// save stuff so we can figure out what frame we're in later.
|
2005-11-16 12:37:01 +03:00
|
|
|
mLastContent = contentNode;
|
|
|
|
mLastContentOffset = aOffset;
|
|
|
|
mLastHint = aFrameHint;
|
|
|
|
mLastBidiLevel = aBidiLevel;
|
|
|
|
|
|
|
|
// If there has been a reflow, set the caret Bidi level to the level of the current frame
|
2006-08-08 09:14:52 +04:00
|
|
|
if (aBidiLevel & BIDI_LEVEL_UNDEFINED) {
|
2011-05-19 07:10:49 +04:00
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
|
2006-08-08 09:14:52 +04:00
|
|
|
if (!frameSelection)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2006-08-08 09:14:52 +04:00
|
|
|
frameSelection->SetCaretBidiLevel(NS_GET_EMBEDDING_LEVEL(theFrame));
|
|
|
|
}
|
2006-05-04 04:05:22 +04:00
|
|
|
|
|
|
|
// Only update the caret's rect when we're not currently drawn.
|
2010-03-15 01:54:11 +03:00
|
|
|
if (!UpdateCaretRects(theFrame, theFrameOffset))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-16 12:37:01 +03:00
|
|
|
}
|
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
if (aInvalidate)
|
2012-08-29 09:39:31 +04:00
|
|
|
theFrame->SchedulePaint();
|
2005-11-16 12:37:01 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-16 12:37:01 +03:00
|
|
|
}
|
|
|
|
|
2008-07-16 14:52:01 +04:00
|
|
|
nsresult
|
2006-04-18 03:16:46 +04:00
|
|
|
nsCaret::GetCaretFrameForNodeOffset(nsIContent* aContentNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aOffset,
|
2006-04-26 05:57:22 +04:00
|
|
|
nsFrameSelection::HINT aFrameHint,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aBidiLevel,
|
2006-04-18 03:16:46 +04:00
|
|
|
nsIFrame** aReturnFrame,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aReturnOffset)
|
2005-11-16 12:37:01 +03:00
|
|
|
{
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
//get frame selection and find out what frame to use...
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
|
|
|
|
if (!presShell)
|
2005-11-16 12:37:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-24 21:44:28 +04:00
|
|
|
|
2010-11-25 04:35:01 +03:00
|
|
|
if (!aContentNode || !aContentNode->IsInDoc() ||
|
|
|
|
presShell->GetDocument() != aContentNode->GetCurrentDoc())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-05-19 07:10:49 +04:00
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
|
2006-08-08 09:14:52 +04:00
|
|
|
if (!frameSelection)
|
2005-11-16 12:37:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-24 21:44:28 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame* theFrame = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t theFrameOffset = 0;
|
2000-09-08 00:21:22 +04:00
|
|
|
|
2006-04-26 05:57:22 +04:00
|
|
|
theFrame = frameSelection->GetFrameForNodeOffset(aContentNode, aOffset,
|
|
|
|
aFrameHint, &theFrameOffset);
|
|
|
|
if (!theFrame)
|
2005-11-16 12:37:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2000-09-08 00:21:22 +04:00
|
|
|
|
2007-11-08 07:07:00 +03: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);
|
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
// Mamdouh : modification of the caret to work at rtl and ltr with Bidi
|
|
|
|
//
|
2013-02-17 01:51:03 +04:00
|
|
|
// Direction Style from visibility->mDirection
|
2001-03-21 04:16:22 +03:00
|
|
|
// ------------------
|
|
|
|
// NS_STYLE_DIRECTION_LTR : LTR or Default
|
|
|
|
// NS_STYLE_DIRECTION_RTL
|
|
|
|
// NS_STYLE_DIRECTION_INHERIT
|
2008-12-14 12:05:39 +03:00
|
|
|
if (mBidiUI)
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
2005-11-16 12:37:01 +03: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);
|
2001-03-21 04:16:22 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t start;
|
|
|
|
int32_t end;
|
2001-03-21 04:16:22 +03:00
|
|
|
nsIFrame* frameBefore;
|
|
|
|
nsIFrame* frameAfter;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t levelBefore; // Bidi level of the character before the caret
|
|
|
|
uint8_t levelAfter; // Bidi level of the character after the caret
|
2001-03-21 04:16:22 +03:00
|
|
|
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
if (start == 0 || end == 0 || start == theFrameOffset || end == theFrameOffset)
|
|
|
|
{
|
2006-04-26 05:57:22 +04:00
|
|
|
nsPrevNextBidiLevels levels = frameSelection->
|
2011-10-17 18:59:28 +04:00
|
|
|
GetPrevNextBidiLevels(aContentNode, aOffset, false);
|
2006-04-26 05:57:22 +04:00
|
|
|
|
2005-11-16 12:37:01 +03:00
|
|
|
/* Boundary condition, we need to know the Bidi levels of the characters before and after the caret */
|
2006-04-26 05:57:22 +04:00
|
|
|
if (levels.mFrameBefore || levels.mFrameAfter)
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
2006-04-26 05:57:22 +04:00
|
|
|
frameBefore = levels.mFrameBefore;
|
|
|
|
frameAfter = levels.mFrameAfter;
|
|
|
|
levelBefore = levels.mLevelBefore;
|
|
|
|
levelAfter = levels.mLevelAfter;
|
|
|
|
|
2005-11-16 12:37:01 +03:00
|
|
|
if ((levelBefore != levelAfter) || (aBidiLevel != levelBefore))
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
2013-01-15 16:22:03 +04:00
|
|
|
aBidiLevel = std::max(aBidiLevel, std::min(levelBefore, levelAfter)); // rule c3
|
|
|
|
aBidiLevel = std::min(aBidiLevel, std::max(levelBefore, levelAfter)); // rule c4
|
2005-11-16 12:37:01 +03:00
|
|
|
if (aBidiLevel == levelBefore // rule c1
|
2008-09-15 19:40:25 +04:00
|
|
|
|| (aBidiLevel > levelBefore && aBidiLevel < levelAfter && !((aBidiLevel ^ levelBefore) & 1)) // rule c5
|
|
|
|
|| (aBidiLevel < levelBefore && aBidiLevel > levelAfter && !((aBidiLevel ^ levelBefore) & 1))) // rule c9
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
|
|
|
if (theFrame != frameBefore)
|
|
|
|
{
|
2005-11-16 12:37:01 +03:00
|
|
|
if (frameBefore) // if there is a frameBefore, move into it
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
|
|
|
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
|
2006-08-03 10:24:11 +04:00
|
|
|
// real frame for the caret to be in. We have to find the visually first frame on the line.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t baseLevel = NS_GET_BASE_LEVEL(frameAfter);
|
2001-03-21 04:16:22 +03:00
|
|
|
if (baseLevel != levelAfter)
|
|
|
|
{
|
2012-03-26 20:09:38 +04:00
|
|
|
nsPeekOffsetStruct pos(eSelectBeginLine, eDirPrevious, 0, 0, false, true, false, true);
|
2006-09-12 00:43:01 +04:00
|
|
|
if (NS_SUCCEEDED(frameAfter->PeekOffset(&pos))) {
|
2006-08-03 10:24:11 +04:00
|
|
|
theFrame = pos.mResultFrame;
|
|
|
|
theFrameOffset = pos.mContentOffset;
|
2001-03-21 04:16:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-16 12:37:01 +03:00
|
|
|
else if (aBidiLevel == levelAfter // rule c2
|
2008-09-15 19:40:25 +04:00
|
|
|
|| (aBidiLevel > levelBefore && aBidiLevel < levelAfter && !((aBidiLevel ^ levelAfter) & 1)) // rule c6
|
|
|
|
|| (aBidiLevel < levelBefore && aBidiLevel > levelAfter && !((aBidiLevel ^ levelAfter) & 1))) // rule c10
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
|
|
|
if (theFrame != frameAfter)
|
|
|
|
{
|
|
|
|
if (frameAfter)
|
|
|
|
{
|
2005-11-16 12:37:01 +03:00
|
|
|
// if there is a frameAfter, move into it
|
2001-03-21 04:16:22 +03:00
|
|
|
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
|
2006-08-03 10:24:11 +04:00
|
|
|
// real frame for the caret to be in. We have to find the visually last frame on the line.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t baseLevel = NS_GET_BASE_LEVEL(frameBefore);
|
2001-03-21 04:16:22 +03:00
|
|
|
if (baseLevel != levelBefore)
|
|
|
|
{
|
2012-03-26 20:09:38 +04:00
|
|
|
nsPeekOffsetStruct pos(eSelectEndLine, eDirNext, 0, 0, false, true, false, true);
|
2006-09-12 00:43:01 +04:00
|
|
|
if (NS_SUCCEEDED(frameBefore->PeekOffset(&pos))) {
|
2006-08-03 10:24:11 +04:00
|
|
|
theFrame = pos.mResultFrame;
|
|
|
|
theFrameOffset = pos.mContentOffset;
|
2001-03-21 04:16:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-16 12:37:01 +03:00
|
|
|
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
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
2006-04-26 05:57:22 +04:00
|
|
|
if (NS_SUCCEEDED(frameSelection->GetFrameFromLevel(frameAfter, eDirNext, aBidiLevel, &theFrame)))
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
2004-06-01 10:20:16 +04:00
|
|
|
levelAfter = NS_GET_EMBEDDING_LEVEL(theFrame);
|
2005-11-16 12:37:01 +03:00
|
|
|
if (aBidiLevel & 1) // c8: caret to the right of the rightmost character
|
2001-03-21 04:16:22 +03:00
|
|
|
theFrameOffset = (levelAfter & 1) ? start : end;
|
|
|
|
else // c7: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? end : start;
|
|
|
|
}
|
|
|
|
}
|
2005-11-16 12:37:01 +03:00
|
|
|
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
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
2006-04-26 05:57:22 +04:00
|
|
|
if (NS_SUCCEEDED(frameSelection->GetFrameFromLevel(frameBefore, eDirPrevious, aBidiLevel, &theFrame)))
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
2004-06-01 10:20:16 +04:00
|
|
|
levelBefore = NS_GET_EMBEDDING_LEVEL(theFrame);
|
2005-11-16 12:37:01 +03:00
|
|
|
if (aBidiLevel & 1) // c12: caret to the left of the leftmost character
|
2001-03-21 04:16:22 +03:00
|
|
|
theFrameOffset = (levelBefore & 1) ? end : start;
|
|
|
|
else // c11: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? start : end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-25 04:35:01 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(!theFrame || theFrame->PresContext()->PresShell() == presShell,
|
|
|
|
"caret frame is in wrong document");
|
2005-11-16 12:37:01 +03:00
|
|
|
*aReturnFrame = theFrame;
|
|
|
|
*aReturnOffset = theFrameOffset;
|
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2009-10-16 14:55:32 +04:00
|
|
|
nsresult nsCaret::CheckCaretDrawingState()
|
2008-04-01 12:35:11 +04:00
|
|
|
{
|
2009-10-16 14:55:32 +04:00
|
|
|
if (mDrawn) {
|
|
|
|
// The caret is drawn; if it shouldn't be, erase it.
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!mVisible || !MustDrawCaret(true))
|
2009-10-16 14:55:32 +04:00
|
|
|
EraseCaret();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The caret is not drawn; if it should be, draw it.
|
2011-10-17 18:59:28 +04:00
|
|
|
if (mPendingDraw && (mVisible && MustDrawCaret(true)))
|
|
|
|
DrawCaret(true);
|
2009-10-16 14:55:32 +04:00
|
|
|
}
|
2008-04-01 12:35:11 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-02 03:57:35 +04:00
|
|
|
|
1999-09-15 03:41:19 +04:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
MustDrawCaret
|
|
|
|
|
2008-04-01 12:35:11 +04:00
|
|
|
Find out if we need to do any caret drawing. This returns true if
|
|
|
|
either:
|
|
|
|
a) The caret has been drawn, and we need to erase it.
|
|
|
|
b) The caret is not drawn, and the selection is collapsed.
|
|
|
|
c) The caret is not hidden due to open XUL popups
|
|
|
|
(see IsMenuPopupHidingCaret()).
|
2000-09-07 09:26:25 +04:00
|
|
|
|
1999-09-15 03:41:19 +04:00
|
|
|
----------------------------------------------------------------------------- */
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsCaret::MustDrawCaret(bool aIgnoreDrawnState)
|
1999-09-15 03:41:19 +04:00
|
|
|
{
|
2008-04-01 12:35:11 +04:00
|
|
|
if (!aIgnoreDrawnState && mDrawn)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-02-28 02:09:51 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
2000-06-07 05:18:41 +04:00
|
|
|
if (!domSelection)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
1999-09-15 03:41:19 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2000-09-07 09:26:25 +04:00
|
|
|
if (NS_FAILED(domSelection->GetIsCollapsed(&isCollapsed)))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-12-28 06:27:23 +03:00
|
|
|
|
2001-04-05 23:53:13 +04:00
|
|
|
if (mShowDuringSelection)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true; // show the caret even in selections
|
2000-12-28 06:27:23 +03:00
|
|
|
|
2008-04-01 12:35:11 +04:00
|
|
|
if (IsMenuPopupHidingCaret())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-04-01 12:35:11 +04:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
return isCollapsed;
|
1999-09-15 03:41:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsCaret::IsMenuPopupHidingCaret()
|
2008-04-01 12:35:11 +04:00
|
|
|
{
|
2008-09-08 16:23:38 +04:00
|
|
|
#ifdef MOZ_XUL
|
2008-04-01 12:35:11 +04:00
|
|
|
// Check if there are open popups.
|
|
|
|
nsXULPopupManager *popMgr = nsXULPopupManager::GetInstance();
|
2012-11-29 20:14:13 +04:00
|
|
|
nsTArray<nsIFrame*> popups;
|
|
|
|
popMgr->GetVisiblePopups(popups);
|
2008-04-01 12:35:11 +04:00
|
|
|
|
|
|
|
if (popups.Length() == 0)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; // No popups, so caret can't be hidden by them.
|
2008-04-01 12:35:11 +04: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 18:59:28 +04:00
|
|
|
return true; // No selection/caret to draw.
|
2008-04-01 12:35:11 +04:00
|
|
|
domSelection->GetFocusNode(getter_AddRefs(node));
|
|
|
|
if (!node)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true; // No selection/caret to draw.
|
2008-04-01 12:35:11 +04:00
|
|
|
nsCOMPtr<nsIContent> caretContent = do_QueryInterface(node);
|
2008-04-09 10:16:09 +04:00
|
|
|
if (!caretContent)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true; // No selection/caret to draw.
|
2008-04-01 12:35:11 +04:00
|
|
|
|
|
|
|
// If there's a menu popup open before the popup with
|
|
|
|
// the caret, don't show the caret.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i=0; i<popups.Length(); i++) {
|
2008-04-01 12:35:11 +04: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 18:59:28 +04:00
|
|
|
return false;
|
2008-04-01 12:35:11 +04:00
|
|
|
}
|
|
|
|
|
2008-04-30 20:54:16 +04:00
|
|
|
if (popupFrame->PopupType() == ePopupTypeMenu && !popupFrame->IsContextMenu()) {
|
2008-04-01 12:35:11 +04: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 18:59:28 +04:00
|
|
|
return true;
|
2008-04-01 12:35:11 +04:00
|
|
|
}
|
|
|
|
}
|
2008-09-08 16:23:38 +04:00
|
|
|
#endif
|
2008-04-01 12:35:11 +04:00
|
|
|
|
|
|
|
// There are no open menu popups, no need to hide the caret.
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-04-01 12:35:11 +04:00
|
|
|
}
|
1999-09-15 03:41:19 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsCaret::DrawCaret(bool aInvalidate)
|
1999-04-02 03:57:35 +04:00
|
|
|
{
|
2009-10-16 14:55:32 +04:00
|
|
|
// Do we need to draw the caret at all?
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!MustDrawCaret(false))
|
2000-09-08 00:21:22 +04:00
|
|
|
return;
|
|
|
|
|
2009-10-16 14:55:32 +04:00
|
|
|
// Can we draw the caret now?
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
|
2012-12-24 08:45:57 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(presShell);
|
2009-10-16 14:55:32 +04:00
|
|
|
{
|
2010-03-31 16:43:49 +04:00
|
|
|
if (presShell->IsPaintingSuppressed())
|
2009-10-16 14:55:32 +04:00
|
|
|
{
|
|
|
|
if (!mDrawn)
|
2011-10-17 18:59:28 +04:00
|
|
|
mPendingDraw = true;
|
2009-10-16 14:55:32 +04:00
|
|
|
|
|
|
|
// PresShell::UnsuppressAndInvalidate() will call CheckCaretDrawingState()
|
|
|
|
// to get us drawn.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-11 07:44:16 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset;
|
2006-04-26 05:57:22 +04:00
|
|
|
nsFrameSelection::HINT hint;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t bidiLevel;
|
2005-08-11 07:44:16 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
if (!mDrawn)
|
|
|
|
{
|
2003-02-28 02:09:51 +03:00
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(domSelection));
|
|
|
|
if (!privateSelection) return;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed = false;
|
2003-02-28 02:09:51 +03:00
|
|
|
domSelection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (!mShowDuringSelection && !isCollapsed)
|
|
|
|
return;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hintRight;
|
2003-02-28 02:09:51 +03:00
|
|
|
privateSelection->GetInterlinePosition(&hintRight);//translate hint.
|
2006-04-26 05:57:22 +04:00
|
|
|
hint = hintRight ? nsFrameSelection::HINTRIGHT : nsFrameSelection::HINTLEFT;
|
2003-02-28 02:09:51 +03:00
|
|
|
|
|
|
|
// get the node and offset, which is where we want the caret to draw
|
2005-08-11 07:44:16 +04:00
|
|
|
domSelection->GetFocusNode(getter_AddRefs(node));
|
|
|
|
if (!node)
|
2003-02-28 02:09:51 +03:00
|
|
|
return;
|
|
|
|
|
2005-08-11 07:44:16 +04:00
|
|
|
if (NS_FAILED(domSelection->GetFocusOffset(&offset)))
|
2003-02-28 02:09:51 +03:00
|
|
|
return;
|
2005-11-16 12:37:01 +03:00
|
|
|
|
2011-05-19 07:10:49 +04:00
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
|
2006-08-08 09:14:52 +04:00
|
|
|
if (!frameSelection)
|
2006-07-21 03:26:05 +04:00
|
|
|
return;
|
2009-10-16 14:55:32 +04:00
|
|
|
|
2006-08-08 09:14:52 +04:00
|
|
|
bidiLevel = frameSelection->GetCaretBidiLevel();
|
2011-10-17 18:59:28 +04:00
|
|
|
mPendingDraw = false;
|
2005-08-11 07:44:16 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!mLastContent)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mDrawn = false;
|
2005-08-11 07:44:16 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-11-25 04:35:01 +03:00
|
|
|
if (!mLastContent->IsInDoc() ||
|
|
|
|
presShell->GetDocument() != mLastContent->GetCurrentDoc())
|
2005-08-11 07:44:16 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mLastContent = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mDrawn = false;
|
2000-09-08 00:21:22 +04:00
|
|
|
return;
|
2005-08-11 07:44:16 +04:00
|
|
|
}
|
|
|
|
node = do_QueryInterface(mLastContent);
|
|
|
|
offset = mLastContentOffset;
|
|
|
|
hint = mLastHint;
|
2005-11-16 12:37:01 +03:00
|
|
|
bidiLevel = mLastBidiLevel;
|
2000-09-08 00:21:22 +04:00
|
|
|
}
|
2003-02-28 02:09:51 +03:00
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
DrawAtPositionWithHint(node, offset, hint, bidiLevel, aInvalidate);
|
2006-04-18 03:16:46 +04:00
|
|
|
ToggleDrawnStatus();
|
2003-02-28 02:09:51 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCaret::UpdateCaretRects(nsIFrame* aFrame, int32_t aFrameOffset)
|
2003-02-28 02:09:51 +03:00
|
|
|
{
|
2005-08-11 07:44:16 +04:00
|
|
|
NS_ASSERTION(aFrame, "Should have a frame here");
|
|
|
|
|
2010-03-15 01:52:45 +03:00
|
|
|
nscoord bidiIndicatorSize;
|
2010-11-25 04:35:01 +03:00
|
|
|
nsresult rv =
|
|
|
|
GetGeometryForFrame(aFrame, aFrameOffset, &mCaretRect, &bidiIndicatorSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-11-25 04:35:01 +03:00
|
|
|
}
|
2007-08-14 00:51:16 +04:00
|
|
|
|
2006-11-01 21:31:42 +03:00
|
|
|
// on RTL frames the right edge of mCaretRect must be equal to framePos
|
2013-02-17 01:51:02 +04:00
|
|
|
const nsStyleVisibility* vis = aFrame->StyleVisibility();
|
2006-11-01 21:31:42 +03:00
|
|
|
if (NS_STYLE_DIRECTION_RTL == vis->mDirection)
|
|
|
|
mCaretRect.x -= mCaretRect.width;
|
|
|
|
|
2010-03-15 01:54:11 +03:00
|
|
|
#ifdef IBMBIDI
|
2011-04-19 07:07:23 +04:00
|
|
|
mHookRect.SetEmpty();
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2006-04-18 03:16:46 +04:00
|
|
|
// Simon -- make a hook to draw to the left or right of the caret to show keyboard language direction
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCaretRTL = false;
|
2006-08-08 09:14:52 +04:00
|
|
|
nsIBidiKeyboard* bidiKeyboard = nsContentUtils::GetBidiKeyboard();
|
2010-03-15 01:54:11 +03: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)) &&
|
|
|
|
mBidiUI) {
|
|
|
|
if (isCaretRTL != mKeyboardRTL) {
|
2006-04-18 03:16:46 +04:00
|
|
|
/* if the caret bidi level and the keyboard language direction are not in
|
|
|
|
* synch, the keyboard language must have been changed by the
|
|
|
|
* user, and if the caret is in a boundary condition (between left-to-right and
|
|
|
|
* right-to-left characters) it may have to change position to
|
|
|
|
* reflect the location in which the next character typed will
|
|
|
|
* appear. We will call |SelectionLanguageChange| and exit
|
|
|
|
* without drawing the caret in the old position.
|
|
|
|
*/
|
|
|
|
mKeyboardRTL = isCaretRTL;
|
2013-12-17 18:12:17 +04:00
|
|
|
nsCOMPtr<nsISelectionPrivate> domSelection = do_QueryReferent(mDomSelectionWeak);
|
2010-03-15 01:56:26 +03:00
|
|
|
if (!domSelection ||
|
|
|
|
NS_SUCCEEDED(domSelection->SelectionLanguageChange(mKeyboardRTL)))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-03-21 04:16:22 +03:00
|
|
|
}
|
2006-04-18 03:16:46 +04: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.
|
|
|
|
mHookRect.SetRect(mCaretRect.x + ((isCaretRTL) ?
|
2010-03-15 01:54:11 +03:00
|
|
|
bidiIndicatorSize * -1 :
|
2006-04-18 03:16:46 +04:00
|
|
|
mCaretRect.width),
|
2010-03-15 01:54:11 +03:00
|
|
|
mCaretRect.y + bidiIndicatorSize,
|
|
|
|
bidiIndicatorSize,
|
2006-04-18 03:16:46 +04:00
|
|
|
mCaretRect.width);
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
2006-04-18 03:16:46 +04:00
|
|
|
#endif //IBMBIDI
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2006-04-18 03:16:46 +04:00
|
|
|
}
|
2005-08-22 04:25:20 +04:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/* static */
|
|
|
|
void nsCaret::CaretBlinkCallback(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsCaret *theCaret = reinterpret_cast<nsCaret*>(aClosure);
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!theCaret) return;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
theCaret->DrawCaret(true);
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-08 09:14:52 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-16 00:09:28 +04:00
|
|
|
nsFrameSelection*
|
2007-11-22 23:47:33 +03:00
|
|
|
nsCaret::GetFrameSelection()
|
|
|
|
{
|
2013-02-16 00:09:28 +04:00
|
|
|
nsCOMPtr<nsISelection> sel = do_QueryReferent(mDomSelectionWeak);
|
|
|
|
if (!sel)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2013-02-16 00:09:28 +04:00
|
|
|
|
|
|
|
return static_cast<Selection*>(sel.get())->GetFrameSelection();
|
2006-08-08 09:14:52 +04:00
|
|
|
}
|
|
|
|
|
2007-08-10 17:19:13 +04:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsCaret::SetIgnoreUserModify(bool aIgnoreUserModify)
|
2007-08-10 17:19:13 +04:00
|
|
|
{
|
2008-02-27 04:49:46 +03:00
|
|
|
if (!aIgnoreUserModify && mIgnoreUserModify && mDrawn) {
|
|
|
|
// We're turning off mIgnoreUserModify. If the caret's drawn
|
|
|
|
// in a read-only node we must erase it, else the next call
|
|
|
|
// to DrawCaret() won't erase the old caret, due to the new
|
|
|
|
// mIgnoreUserModify value.
|
|
|
|
nsIFrame *frame = GetCaretFrame();
|
|
|
|
if (frame) {
|
2013-02-17 01:51:02 +04:00
|
|
|
const nsStyleUserInterface* userinterface = frame->StyleUserInterface();
|
2008-02-27 04:49:46 +03:00
|
|
|
if (userinterface->mUserModify == NS_STYLE_USER_MODIFY_READ_ONLY) {
|
|
|
|
StopBlinking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 17:19:13 +04:00
|
|
|
mIgnoreUserModify = aIgnoreUserModify;
|
|
|
|
}
|