accessible/src/base/SelectionManager.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/accessible/src/base/SelectionManager.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,222 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/a11y/SelectionManager.h"
    1.10 +
    1.11 +#include "DocAccessible-inl.h"
    1.12 +#include "nsAccessibilityService.h"
    1.13 +#include "nsAccUtils.h"
    1.14 +#include "nsCoreUtils.h"
    1.15 +#include "nsEventShell.h"
    1.16 +
    1.17 +#include "nsIAccessibleTypes.h"
    1.18 +#include "nsIDOMDocument.h"
    1.19 +#include "nsIPresShell.h"
    1.20 +#include "mozilla/dom/Selection.h"
    1.21 +#include "mozilla/dom/Element.h"
    1.22 +
    1.23 +using namespace mozilla;
    1.24 +using namespace mozilla::a11y;
    1.25 +using mozilla::dom::Selection;
    1.26 +
    1.27 +struct mozilla::a11y::SelData MOZ_FINAL
    1.28 +{
    1.29 +  SelData(Selection* aSel, int32_t aReason) :
    1.30 +    mSel(aSel), mReason(aReason) {}
    1.31 +
    1.32 +  nsRefPtr<Selection> mSel;
    1.33 +  int16_t mReason;
    1.34 +
    1.35 +  NS_INLINE_DECL_REFCOUNTING(SelData)
    1.36 +
    1.37 +private:
    1.38 +  // Private destructor, to discourage deletion outside of Release():
    1.39 +  ~SelData() {}
    1.40 +};
    1.41 +
    1.42 +void
    1.43 +SelectionManager::ClearControlSelectionListener()
    1.44 +{
    1.45 +  if (!mCurrCtrlFrame)
    1.46 +    return;
    1.47 +
    1.48 +  const nsFrameSelection* frameSel = mCurrCtrlFrame->GetConstFrameSelection();
    1.49 +  NS_ASSERTION(frameSel, "No frame selection for the element!");
    1.50 +
    1.51 +  mCurrCtrlFrame = nullptr;
    1.52 +  if (!frameSel)
    1.53 +    return;
    1.54 +
    1.55 +  // Remove 'this' registered as selection listener for the normal selection.
    1.56 +  Selection* normalSel =
    1.57 +    frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
    1.58 +  normalSel->RemoveSelectionListener(this);
    1.59 +
    1.60 +  // Remove 'this' registered as selection listener for the spellcheck
    1.61 +  // selection.
    1.62 +  Selection* spellSel =
    1.63 +    frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
    1.64 +  spellSel->RemoveSelectionListener(this);
    1.65 +}
    1.66 +
    1.67 +void
    1.68 +SelectionManager::SetControlSelectionListener(dom::Element* aFocusedElm)
    1.69 +{
    1.70 +  // When focus moves such that the caret is part of a new frame selection
    1.71 +  // this removes the old selection listener and attaches a new one for
    1.72 +  // the current focus.
    1.73 +  ClearControlSelectionListener();
    1.74 +
    1.75 +  mCurrCtrlFrame = aFocusedElm->GetPrimaryFrame();
    1.76 +  if (!mCurrCtrlFrame)
    1.77 +    return;
    1.78 +
    1.79 +  const nsFrameSelection* frameSel = mCurrCtrlFrame->GetConstFrameSelection();
    1.80 +  NS_ASSERTION(frameSel, "No frame selection for focused element!");
    1.81 +  if (!frameSel)
    1.82 +    return;
    1.83 +
    1.84 +  // Register 'this' as selection listener for the normal selection.
    1.85 +  Selection* normalSel =
    1.86 +    frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
    1.87 +  normalSel->AddSelectionListener(this);
    1.88 +
    1.89 +  // Register 'this' as selection listener for the spell check selection.
    1.90 +  Selection* spellSel =
    1.91 +    frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
    1.92 +  spellSel->AddSelectionListener(this);
    1.93 +}
    1.94 +
    1.95 +void
    1.96 +SelectionManager::AddDocSelectionListener(nsIPresShell* aPresShell)
    1.97 +{
    1.98 +  const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
    1.99 +
   1.100 +  // Register 'this' as selection listener for the normal selection.
   1.101 +  Selection* normalSel =
   1.102 +    frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
   1.103 +  normalSel->AddSelectionListener(this);
   1.104 +
   1.105 +  // Register 'this' as selection listener for the spell check selection.
   1.106 +  Selection* spellSel =
   1.107 +    frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
   1.108 +  spellSel->AddSelectionListener(this);
   1.109 +}
   1.110 +
   1.111 +void
   1.112 +SelectionManager::RemoveDocSelectionListener(nsIPresShell* aPresShell)
   1.113 +{
   1.114 +  const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
   1.115 +
   1.116 +  // Remove 'this' registered as selection listener for the normal selection.
   1.117 +  Selection* normalSel =
   1.118 +    frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
   1.119 +  normalSel->RemoveSelectionListener(this);
   1.120 +
   1.121 +  // Remove 'this' registered as selection listener for the spellcheck
   1.122 +  // selection.
   1.123 +  Selection* spellSel =
   1.124 +    frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
   1.125 +  spellSel->RemoveSelectionListener(this);
   1.126 +}
   1.127 +
   1.128 +void
   1.129 +SelectionManager::ProcessTextSelChangeEvent(AccEvent* aEvent)
   1.130 +{
   1.131 +  // Fire selection change event if it's not pure caret-move selection change,
   1.132 +  // i.e. the accessible has or had not collapsed selection.
   1.133 +  AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
   1.134 +  if (!event->IsCaretMoveOnly())
   1.135 +    nsEventShell::FireEvent(aEvent);
   1.136 +
   1.137 +  // Fire caret move event if there's a caret in the selection.
   1.138 +  nsINode* caretCntrNode =
   1.139 +    nsCoreUtils::GetDOMNodeFromDOMPoint(event->mSel->GetFocusNode(),
   1.140 +                                        event->mSel->FocusOffset());
   1.141 +  if (!caretCntrNode)
   1.142 +    return;
   1.143 +
   1.144 +  HyperTextAccessible* caretCntr = nsAccUtils::GetTextContainer(caretCntrNode);
   1.145 +  NS_ASSERTION(caretCntr,
   1.146 +               "No text container for focus while there's one for common ancestor?!");
   1.147 +  if (!caretCntr)
   1.148 +    return;
   1.149 +
   1.150 +  int32_t caretOffset = caretCntr->CaretOffset();
   1.151 +  if (caretOffset != -1) {
   1.152 +    nsRefPtr<AccCaretMoveEvent> caretMoveEvent =
   1.153 +      new AccCaretMoveEvent(caretCntr, caretOffset, aEvent->FromUserInput());
   1.154 +    nsEventShell::FireEvent(caretMoveEvent);
   1.155 +  }
   1.156 +}
   1.157 +
   1.158 +NS_IMETHODIMP
   1.159 +SelectionManager::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
   1.160 +                                         nsISelection* aSelection,
   1.161 +                                         int16_t aReason)
   1.162 +{
   1.163 +  NS_ENSURE_ARG(aDOMDocument);
   1.164 +
   1.165 +  nsCOMPtr<nsIDocument> documentNode(do_QueryInterface(aDOMDocument));
   1.166 +  DocAccessible* document = GetAccService()->GetDocAccessible(documentNode);
   1.167 +
   1.168 +#ifdef A11Y_LOG
   1.169 +  if (logging::IsEnabled(logging::eSelection))
   1.170 +    logging::SelChange(aSelection, document, aReason);
   1.171 +#endif
   1.172 +
   1.173 +  // Don't fire events until document is loaded.
   1.174 +  if (document && document->IsContentLoaded()) {
   1.175 +    // Selection manager has longer lifetime than any document accessible,
   1.176 +    // so that we are guaranteed that the notification is processed before
   1.177 +    // the selection manager is destroyed.
   1.178 +    nsRefPtr<SelData> selData =
   1.179 +      new SelData(static_cast<Selection*>(aSelection), aReason);
   1.180 +    document->HandleNotification<SelectionManager, SelData>
   1.181 +      (this, &SelectionManager::ProcessSelectionChanged, selData);
   1.182 +  }
   1.183 +
   1.184 +  return NS_OK;
   1.185 +}
   1.186 +
   1.187 +void
   1.188 +SelectionManager::ProcessSelectionChanged(SelData* aSelData)
   1.189 +{
   1.190 +  Selection* selection = aSelData->mSel;
   1.191 +  if (!selection->GetPresShell())
   1.192 +    return;
   1.193 +
   1.194 +  const nsRange* range = selection->GetAnchorFocusRange();
   1.195 +  nsINode* cntrNode = nullptr;
   1.196 +  if (range)
   1.197 +    cntrNode = range->GetCommonAncestor();
   1.198 +
   1.199 +  if (!cntrNode) {
   1.200 +    cntrNode = selection->GetFrameSelection()->GetAncestorLimiter();
   1.201 +    if (!cntrNode) {
   1.202 +      cntrNode = selection->GetPresShell()->GetDocument();
   1.203 +      NS_ASSERTION(aSelData->mSel->GetPresShell()->ConstFrameSelection() == selection->GetFrameSelection(),
   1.204 +                   "Wrong selection container was used!");
   1.205 +    }
   1.206 +  }
   1.207 +
   1.208 +  HyperTextAccessible* text = nsAccUtils::GetTextContainer(cntrNode);
   1.209 +  if (!text) {
   1.210 +    NS_NOTREACHED("We must reach document accessible implementing text interface!");
   1.211 +    return;
   1.212 +  }
   1.213 +
   1.214 +  if (selection->GetType() == nsISelectionController::SELECTION_NORMAL) {
   1.215 +    nsRefPtr<AccEvent> event =
   1.216 +      new AccTextSelChangeEvent(text, selection, aSelData->mReason);
   1.217 +    text->Document()->FireDelayedEvent(event);
   1.218 +
   1.219 +  } else if (selection->GetType() == nsISelectionController::SELECTION_SPELLCHECK) {
   1.220 +    // XXX: fire an event for container accessible of the focus/anchor range
   1.221 +    // of the spelcheck selection.
   1.222 +    text->Document()->FireDelayedEvent(nsIAccessibleEvent::EVENT_TEXT_ATTRIBUTE_CHANGED,
   1.223 +                                       text);
   1.224 +  }
   1.225 +}

mercurial