Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_a11y_HyperTextAccessible_inl_h__ |
michael@0 | 7 | #define mozilla_a11y_HyperTextAccessible_inl_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "HyperTextAccessible.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsAccUtils.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsIClipboard.h" |
michael@0 | 14 | #include "nsIEditor.h" |
michael@0 | 15 | #include "nsIPersistentProperties2.h" |
michael@0 | 16 | #include "nsIPlaintextEditor.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace a11y { |
michael@0 | 20 | |
michael@0 | 21 | inline bool |
michael@0 | 22 | HyperTextAccessible::IsValidOffset(int32_t aOffset) |
michael@0 | 23 | { |
michael@0 | 24 | int32_t offset = ConvertMagicOffset(aOffset); |
michael@0 | 25 | return offset >= 0 && offset <= static_cast<int32_t>(CharacterCount()); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | inline bool |
michael@0 | 29 | HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset) |
michael@0 | 30 | { |
michael@0 | 31 | int32_t startOffset = ConvertMagicOffset(aStartOffset); |
michael@0 | 32 | if (startOffset < 0) |
michael@0 | 33 | return false; |
michael@0 | 34 | |
michael@0 | 35 | int32_t endOffset = ConvertMagicOffset(aEndOffset); |
michael@0 | 36 | if (endOffset < 0 || startOffset > endOffset) |
michael@0 | 37 | return false; |
michael@0 | 38 | |
michael@0 | 39 | return endOffset <= static_cast<int32_t>(CharacterCount()); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | inline bool |
michael@0 | 43 | HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset) |
michael@0 | 44 | { |
michael@0 | 45 | dom::Selection* domSel = DOMSelection(); |
michael@0 | 46 | return domSel && |
michael@0 | 47 | SetSelectionBoundsAt(domSel->GetRangeCount(), aStartOffset, aEndOffset); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | inline void |
michael@0 | 51 | HyperTextAccessible::ReplaceText(const nsAString& aText) |
michael@0 | 52 | { |
michael@0 | 53 | int32_t numChars = CharacterCount(); |
michael@0 | 54 | if (numChars != 0) |
michael@0 | 55 | DeleteText(0, numChars); |
michael@0 | 56 | |
michael@0 | 57 | InsertText(aText, 0); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | inline void |
michael@0 | 61 | HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition) |
michael@0 | 62 | { |
michael@0 | 63 | nsCOMPtr<nsIEditor> editor = GetEditor(); |
michael@0 | 64 | nsCOMPtr<nsIPlaintextEditor> peditor(do_QueryInterface(editor)); |
michael@0 | 65 | if (peditor) { |
michael@0 | 66 | SetSelectionRange(aPosition, aPosition); |
michael@0 | 67 | peditor->InsertText(aText); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | inline void |
michael@0 | 72 | HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos) |
michael@0 | 73 | { |
michael@0 | 74 | nsCOMPtr<nsIEditor> editor = GetEditor(); |
michael@0 | 75 | if (editor) { |
michael@0 | 76 | SetSelectionRange(aStartPos, aEndPos); |
michael@0 | 77 | editor->Copy(); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | inline void |
michael@0 | 82 | HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos) |
michael@0 | 83 | { |
michael@0 | 84 | nsCOMPtr<nsIEditor> editor = GetEditor(); |
michael@0 | 85 | if (editor) { |
michael@0 | 86 | SetSelectionRange(aStartPos, aEndPos); |
michael@0 | 87 | editor->Cut(); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | inline void |
michael@0 | 92 | HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos) |
michael@0 | 93 | { |
michael@0 | 94 | nsCOMPtr<nsIEditor> editor = GetEditor(); |
michael@0 | 95 | if (editor) { |
michael@0 | 96 | SetSelectionRange(aStartPos, aEndPos); |
michael@0 | 97 | editor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip); |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | inline void |
michael@0 | 102 | HyperTextAccessible::PasteText(int32_t aPosition) |
michael@0 | 103 | { |
michael@0 | 104 | nsCOMPtr<nsIEditor> editor = GetEditor(); |
michael@0 | 105 | if (editor) { |
michael@0 | 106 | SetSelectionRange(aPosition, aPosition); |
michael@0 | 107 | editor->Paste(nsIClipboard::kGlobalClipboard); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | inline int32_t |
michael@0 | 112 | HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) |
michael@0 | 113 | { |
michael@0 | 114 | if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT) |
michael@0 | 115 | return CharacterCount(); |
michael@0 | 116 | |
michael@0 | 117 | if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET) |
michael@0 | 118 | return CaretOffset(); |
michael@0 | 119 | |
michael@0 | 120 | return aOffset; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | inline int32_t |
michael@0 | 124 | HyperTextAccessible::AdjustCaretOffset(int32_t aOffset) const |
michael@0 | 125 | { |
michael@0 | 126 | // It is the same character offset when the caret is visually at the very |
michael@0 | 127 | // end of a line or the start of a new line (soft line break). Getting text |
michael@0 | 128 | // at the line should provide the line with the visual caret, otherwise |
michael@0 | 129 | // screen readers will announce the wrong line as the user presses up or |
michael@0 | 130 | // down arrow and land at the end of a line. |
michael@0 | 131 | if (aOffset > 0 && IsCaretAtEndOfLine()) |
michael@0 | 132 | return aOffset - 1; |
michael@0 | 133 | |
michael@0 | 134 | return aOffset; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | inline bool |
michael@0 | 138 | HyperTextAccessible::IsCaretAtEndOfLine() const |
michael@0 | 139 | { |
michael@0 | 140 | nsRefPtr<nsFrameSelection> frameSelection = FrameSelection(); |
michael@0 | 141 | return frameSelection && |
michael@0 | 142 | frameSelection->GetHint() == nsFrameSelection::HINTLEFT; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | inline already_AddRefed<nsFrameSelection> |
michael@0 | 146 | HyperTextAccessible::FrameSelection() const |
michael@0 | 147 | { |
michael@0 | 148 | nsIFrame* frame = GetFrame(); |
michael@0 | 149 | return frame ? frame->GetFrameSelection() : nullptr; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | inline dom::Selection* |
michael@0 | 153 | HyperTextAccessible::DOMSelection() const |
michael@0 | 154 | { |
michael@0 | 155 | nsRefPtr<nsFrameSelection> frameSelection = FrameSelection(); |
michael@0 | 156 | return frameSelection ? |
michael@0 | 157 | frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL) : |
michael@0 | 158 | nullptr; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | } // namespace a11y |
michael@0 | 162 | } // namespace mozilla |
michael@0 | 163 | |
michael@0 | 164 | #endif |
michael@0 | 165 |