2013-03-07 16:16:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/a11y/SelectionManager.h"
|
|
|
|
|
|
|
|
#include "DocAccessible-inl.h"
|
2014-05-01 19:45:40 +04:00
|
|
|
#include "HyperTextAccessible.h"
|
|
|
|
#include "HyperTextAccessible-inl.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
#include "nsAccessibilityService.h"
|
|
|
|
#include "nsAccUtils.h"
|
|
|
|
#include "nsCoreUtils.h"
|
2013-10-26 18:58:53 +04:00
|
|
|
#include "nsEventShell.h"
|
2014-08-06 09:19:27 +04:00
|
|
|
#include "nsFrameSelection.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
|
2013-09-11 02:18:59 +04:00
|
|
|
#include "nsIAccessibleTypes.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIPresShell.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2013-09-11 02:18:59 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::a11y;
|
2014-04-10 20:09:40 +04:00
|
|
|
using mozilla::dom::Selection;
|
2013-03-07 16:16:10 +04:00
|
|
|
|
2014-04-02 04:21:24 +04:00
|
|
|
struct mozilla::a11y::SelData MOZ_FINAL
|
2014-02-12 05:18:31 +04:00
|
|
|
{
|
|
|
|
SelData(Selection* aSel, int32_t aReason) :
|
|
|
|
mSel(aSel), mReason(aReason) {}
|
|
|
|
|
|
|
|
nsRefPtr<Selection> mSel;
|
|
|
|
int16_t mReason;
|
|
|
|
|
2014-04-02 04:21:24 +04:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(SelData)
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~SelData() {}
|
2014-02-12 05:18:31 +04:00
|
|
|
};
|
|
|
|
|
2014-05-01 19:45:40 +04:00
|
|
|
SelectionManager::SelectionManager() :
|
|
|
|
mCaretOffset(-1), mAccWithCaret(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:16:10 +04:00
|
|
|
void
|
|
|
|
SelectionManager::ClearControlSelectionListener()
|
|
|
|
{
|
2013-03-23 05:59:48 +04:00
|
|
|
if (!mCurrCtrlFrame)
|
2013-03-07 16:16:10 +04:00
|
|
|
return;
|
|
|
|
|
2013-03-23 05:59:48 +04:00
|
|
|
const nsFrameSelection* frameSel = mCurrCtrlFrame->GetConstFrameSelection();
|
2013-03-07 16:16:10 +04:00
|
|
|
NS_ASSERTION(frameSel, "No frame selection for the element!");
|
2013-03-23 05:59:48 +04:00
|
|
|
|
|
|
|
mCurrCtrlFrame = nullptr;
|
2013-03-07 16:16:10 +04:00
|
|
|
if (!frameSel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->RemoveSelectionListener(this);
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the spellcheck
|
|
|
|
// selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->RemoveSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::SetControlSelectionListener(dom::Element* aFocusedElm)
|
|
|
|
{
|
|
|
|
// When focus moves such that the caret is part of a new frame selection
|
|
|
|
// this removes the old selection listener and attaches a new one for
|
|
|
|
// the current focus.
|
|
|
|
ClearControlSelectionListener();
|
|
|
|
|
2013-03-23 05:59:48 +04:00
|
|
|
mCurrCtrlFrame = aFocusedElm->GetPrimaryFrame();
|
|
|
|
if (!mCurrCtrlFrame)
|
2013-03-07 16:16:10 +04:00
|
|
|
return;
|
|
|
|
|
2013-03-23 05:59:48 +04:00
|
|
|
const nsFrameSelection* frameSel = mCurrCtrlFrame->GetConstFrameSelection();
|
2013-03-07 16:16:10 +04:00
|
|
|
NS_ASSERTION(frameSel, "No frame selection for focused element!");
|
|
|
|
if (!frameSel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->AddSelectionListener(this);
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the spell check selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->AddSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::AddDocSelectionListener(nsIPresShell* aPresShell)
|
|
|
|
{
|
|
|
|
const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->AddSelectionListener(this);
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the spell check selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->AddSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::RemoveDocSelectionListener(nsIPresShell* aPresShell)
|
|
|
|
{
|
|
|
|
const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->RemoveSelectionListener(this);
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the spellcheck
|
|
|
|
// selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->RemoveSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
void
|
|
|
|
SelectionManager::ProcessTextSelChangeEvent(AccEvent* aEvent)
|
|
|
|
{
|
2014-02-12 05:18:31 +04:00
|
|
|
// Fire selection change event if it's not pure caret-move selection change,
|
|
|
|
// i.e. the accessible has or had not collapsed selection.
|
2013-10-26 18:58:53 +04:00
|
|
|
AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
|
2014-02-12 05:18:31 +04:00
|
|
|
if (!event->IsCaretMoveOnly())
|
2013-10-26 18:58:53 +04:00
|
|
|
nsEventShell::FireEvent(aEvent);
|
|
|
|
|
|
|
|
// Fire caret move event if there's a caret in the selection.
|
|
|
|
nsINode* caretCntrNode =
|
2014-02-12 05:18:31 +04:00
|
|
|
nsCoreUtils::GetDOMNodeFromDOMPoint(event->mSel->GetFocusNode(),
|
|
|
|
event->mSel->FocusOffset());
|
2013-10-26 18:58:53 +04:00
|
|
|
if (!caretCntrNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HyperTextAccessible* caretCntr = nsAccUtils::GetTextContainer(caretCntrNode);
|
|
|
|
NS_ASSERTION(caretCntr,
|
|
|
|
"No text container for focus while there's one for common ancestor?!");
|
|
|
|
if (!caretCntr)
|
|
|
|
return;
|
|
|
|
|
2014-05-01 19:45:40 +04:00
|
|
|
Selection* selection = caretCntr->DOMSelection();
|
2014-08-28 00:14:53 +04:00
|
|
|
|
|
|
|
// XXX Sometimes we can't get a selection for caretCntr, in that case assume
|
|
|
|
// event->mSel is correct.
|
|
|
|
if (!selection)
|
|
|
|
selection = event->mSel;
|
|
|
|
|
2014-05-01 19:45:40 +04:00
|
|
|
mCaretOffset = caretCntr->DOMPointToOffset(selection->GetFocusNode(),
|
|
|
|
selection->FocusOffset());
|
|
|
|
mAccWithCaret = caretCntr;
|
|
|
|
if (mCaretOffset != -1) {
|
2013-10-26 18:58:53 +04:00
|
|
|
nsRefPtr<AccCaretMoveEvent> caretMoveEvent =
|
2014-05-01 19:45:40 +04:00
|
|
|
new AccCaretMoveEvent(caretCntr, mCaretOffset, aEvent->FromUserInput());
|
2013-10-26 18:58:53 +04:00
|
|
|
nsEventShell::FireEvent(caretMoveEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:16:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SelectionManager::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
|
|
|
|
nsISelection* aSelection,
|
|
|
|
int16_t aReason)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aDOMDocument);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> documentNode(do_QueryInterface(aDOMDocument));
|
|
|
|
DocAccessible* document = GetAccService()->GetDocAccessible(documentNode);
|
|
|
|
|
|
|
|
#ifdef A11Y_LOG
|
|
|
|
if (logging::IsEnabled(logging::eSelection))
|
2014-02-12 05:18:31 +04:00
|
|
|
logging::SelChange(aSelection, document, aReason);
|
2013-03-07 16:16:10 +04:00
|
|
|
#endif
|
|
|
|
|
2014-08-28 00:14:53 +04:00
|
|
|
if (document) {
|
2013-03-07 16:16:10 +04:00
|
|
|
// Selection manager has longer lifetime than any document accessible,
|
|
|
|
// so that we are guaranteed that the notification is processed before
|
|
|
|
// the selection manager is destroyed.
|
2014-02-12 05:18:31 +04:00
|
|
|
nsRefPtr<SelData> selData =
|
|
|
|
new SelData(static_cast<Selection*>(aSelection), aReason);
|
|
|
|
document->HandleNotification<SelectionManager, SelData>
|
|
|
|
(this, &SelectionManager::ProcessSelectionChanged, selData);
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-12 05:18:31 +04:00
|
|
|
SelectionManager::ProcessSelectionChanged(SelData* aSelData)
|
2013-03-07 16:16:10 +04:00
|
|
|
{
|
2014-02-12 05:18:31 +04:00
|
|
|
Selection* selection = aSelData->mSel;
|
2013-10-30 07:19:04 +04:00
|
|
|
if (!selection->GetPresShell())
|
|
|
|
return;
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
const nsRange* range = selection->GetAnchorFocusRange();
|
|
|
|
nsINode* cntrNode = nullptr;
|
|
|
|
if (range)
|
|
|
|
cntrNode = range->GetCommonAncestor();
|
2014-02-12 05:18:31 +04:00
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
if (!cntrNode) {
|
|
|
|
cntrNode = selection->GetFrameSelection()->GetAncestorLimiter();
|
|
|
|
if (!cntrNode) {
|
|
|
|
cntrNode = selection->GetPresShell()->GetDocument();
|
2014-02-12 05:18:31 +04:00
|
|
|
NS_ASSERTION(aSelData->mSel->GetPresShell()->ConstFrameSelection() == selection->GetFrameSelection(),
|
2013-10-26 18:58:53 +04:00
|
|
|
"Wrong selection container was used!");
|
|
|
|
}
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
HyperTextAccessible* text = nsAccUtils::GetTextContainer(cntrNode);
|
|
|
|
if (!text) {
|
|
|
|
NS_NOTREACHED("We must reach document accessible implementing text interface!");
|
2013-03-07 16:16:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
if (selection->GetType() == nsISelectionController::SELECTION_NORMAL) {
|
2014-02-12 05:18:31 +04:00
|
|
|
nsRefPtr<AccEvent> event =
|
|
|
|
new AccTextSelChangeEvent(text, selection, aSelData->mReason);
|
2013-10-26 18:58:53 +04:00
|
|
|
text->Document()->FireDelayedEvent(event);
|
2013-03-07 16:16:10 +04:00
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
} else if (selection->GetType() == nsISelectionController::SELECTION_SPELLCHECK) {
|
|
|
|
// XXX: fire an event for container accessible of the focus/anchor range
|
|
|
|
// of the spelcheck selection.
|
|
|
|
text->Document()->FireDelayedEvent(nsIAccessibleEvent::EVENT_TEXT_ATTRIBUTE_CHANGED,
|
|
|
|
text);
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
}
|