michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_a11y_HyperTextAccessible_inl_h__ michael@0: #define mozilla_a11y_HyperTextAccessible_inl_h__ michael@0: michael@0: #include "HyperTextAccessible.h" michael@0: michael@0: #include "nsAccUtils.h" michael@0: michael@0: #include "nsIClipboard.h" michael@0: #include "nsIEditor.h" michael@0: #include "nsIPersistentProperties2.h" michael@0: #include "nsIPlaintextEditor.h" michael@0: michael@0: namespace mozilla { michael@0: namespace a11y { michael@0: michael@0: inline bool michael@0: HyperTextAccessible::IsValidOffset(int32_t aOffset) michael@0: { michael@0: int32_t offset = ConvertMagicOffset(aOffset); michael@0: return offset >= 0 && offset <= static_cast(CharacterCount()); michael@0: } michael@0: michael@0: inline bool michael@0: HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset) michael@0: { michael@0: int32_t startOffset = ConvertMagicOffset(aStartOffset); michael@0: if (startOffset < 0) michael@0: return false; michael@0: michael@0: int32_t endOffset = ConvertMagicOffset(aEndOffset); michael@0: if (endOffset < 0 || startOffset > endOffset) michael@0: return false; michael@0: michael@0: return endOffset <= static_cast(CharacterCount()); michael@0: } michael@0: michael@0: inline bool michael@0: HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset) michael@0: { michael@0: dom::Selection* domSel = DOMSelection(); michael@0: return domSel && michael@0: SetSelectionBoundsAt(domSel->GetRangeCount(), aStartOffset, aEndOffset); michael@0: } michael@0: michael@0: inline void michael@0: HyperTextAccessible::ReplaceText(const nsAString& aText) michael@0: { michael@0: int32_t numChars = CharacterCount(); michael@0: if (numChars != 0) michael@0: DeleteText(0, numChars); michael@0: michael@0: InsertText(aText, 0); michael@0: } michael@0: michael@0: inline void michael@0: HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition) michael@0: { michael@0: nsCOMPtr editor = GetEditor(); michael@0: nsCOMPtr peditor(do_QueryInterface(editor)); michael@0: if (peditor) { michael@0: SetSelectionRange(aPosition, aPosition); michael@0: peditor->InsertText(aText); michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos) michael@0: { michael@0: nsCOMPtr editor = GetEditor(); michael@0: if (editor) { michael@0: SetSelectionRange(aStartPos, aEndPos); michael@0: editor->Copy(); michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos) michael@0: { michael@0: nsCOMPtr editor = GetEditor(); michael@0: if (editor) { michael@0: SetSelectionRange(aStartPos, aEndPos); michael@0: editor->Cut(); michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos) michael@0: { michael@0: nsCOMPtr editor = GetEditor(); michael@0: if (editor) { michael@0: SetSelectionRange(aStartPos, aEndPos); michael@0: editor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip); michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: HyperTextAccessible::PasteText(int32_t aPosition) michael@0: { michael@0: nsCOMPtr editor = GetEditor(); michael@0: if (editor) { michael@0: SetSelectionRange(aPosition, aPosition); michael@0: editor->Paste(nsIClipboard::kGlobalClipboard); michael@0: } michael@0: } michael@0: michael@0: inline int32_t michael@0: HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) michael@0: { michael@0: if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT) michael@0: return CharacterCount(); michael@0: michael@0: if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET) michael@0: return CaretOffset(); michael@0: michael@0: return aOffset; michael@0: } michael@0: michael@0: inline int32_t michael@0: HyperTextAccessible::AdjustCaretOffset(int32_t aOffset) const michael@0: { michael@0: // It is the same character offset when the caret is visually at the very michael@0: // end of a line or the start of a new line (soft line break). Getting text michael@0: // at the line should provide the line with the visual caret, otherwise michael@0: // screen readers will announce the wrong line as the user presses up or michael@0: // down arrow and land at the end of a line. michael@0: if (aOffset > 0 && IsCaretAtEndOfLine()) michael@0: return aOffset - 1; michael@0: michael@0: return aOffset; michael@0: } michael@0: michael@0: inline bool michael@0: HyperTextAccessible::IsCaretAtEndOfLine() const michael@0: { michael@0: nsRefPtr frameSelection = FrameSelection(); michael@0: return frameSelection && michael@0: frameSelection->GetHint() == nsFrameSelection::HINTLEFT; michael@0: } michael@0: michael@0: inline already_AddRefed michael@0: HyperTextAccessible::FrameSelection() const michael@0: { michael@0: nsIFrame* frame = GetFrame(); michael@0: return frame ? frame->GetFrameSelection() : nullptr; michael@0: } michael@0: michael@0: inline dom::Selection* michael@0: HyperTextAccessible::DOMSelection() const michael@0: { michael@0: nsRefPtr frameSelection = FrameSelection(); michael@0: return frameSelection ? michael@0: frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL) : michael@0: nullptr; michael@0: } michael@0: michael@0: } // namespace a11y michael@0: } // namespace mozilla michael@0: michael@0: #endif michael@0: