Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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_TextRage_h_ |
michael@0 | 7 | #define mozilla_TextRage_h_ |
michael@0 | 8 | |
michael@0 | 9 | #include <stdint.h> |
michael@0 | 10 | |
michael@0 | 11 | #include "nsAutoPtr.h" |
michael@0 | 12 | #include "nsColor.h" |
michael@0 | 13 | #include "nsStyleConsts.h" |
michael@0 | 14 | #include "nsTArray.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | |
michael@0 | 18 | /****************************************************************************** |
michael@0 | 19 | * mozilla::TextRangeStyle |
michael@0 | 20 | ******************************************************************************/ |
michael@0 | 21 | |
michael@0 | 22 | struct TextRangeStyle |
michael@0 | 23 | { |
michael@0 | 24 | enum |
michael@0 | 25 | { |
michael@0 | 26 | LINESTYLE_NONE = NS_STYLE_TEXT_DECORATION_STYLE_NONE, |
michael@0 | 27 | LINESTYLE_SOLID = NS_STYLE_TEXT_DECORATION_STYLE_SOLID, |
michael@0 | 28 | LINESTYLE_DOTTED = NS_STYLE_TEXT_DECORATION_STYLE_DOTTED, |
michael@0 | 29 | LINESTYLE_DASHED = NS_STYLE_TEXT_DECORATION_STYLE_DASHED, |
michael@0 | 30 | LINESTYLE_DOUBLE = NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE, |
michael@0 | 31 | LINESTYLE_WAVY = NS_STYLE_TEXT_DECORATION_STYLE_WAVY |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | enum |
michael@0 | 35 | { |
michael@0 | 36 | DEFINED_NONE = 0x00, |
michael@0 | 37 | DEFINED_LINESTYLE = 0x01, |
michael@0 | 38 | DEFINED_FOREGROUND_COLOR = 0x02, |
michael@0 | 39 | DEFINED_BACKGROUND_COLOR = 0x04, |
michael@0 | 40 | DEFINED_UNDERLINE_COLOR = 0x08 |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | // Initialize all members, because TextRange instances may be compared by |
michael@0 | 44 | // memcomp. |
michael@0 | 45 | TextRangeStyle() |
michael@0 | 46 | { |
michael@0 | 47 | Clear(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | void Clear() |
michael@0 | 51 | { |
michael@0 | 52 | mDefinedStyles = DEFINED_NONE; |
michael@0 | 53 | mLineStyle = LINESTYLE_NONE; |
michael@0 | 54 | mIsBoldLine = false; |
michael@0 | 55 | mForegroundColor = mBackgroundColor = mUnderlineColor = NS_RGBA(0, 0, 0, 0); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | bool IsDefined() const { return mDefinedStyles != DEFINED_NONE; } |
michael@0 | 59 | |
michael@0 | 60 | bool IsLineStyleDefined() const |
michael@0 | 61 | { |
michael@0 | 62 | return (mDefinedStyles & DEFINED_LINESTYLE) != 0; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | bool IsForegroundColorDefined() const |
michael@0 | 66 | { |
michael@0 | 67 | return (mDefinedStyles & DEFINED_FOREGROUND_COLOR) != 0; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | bool IsBackgroundColorDefined() const |
michael@0 | 71 | { |
michael@0 | 72 | return (mDefinedStyles & DEFINED_BACKGROUND_COLOR) != 0; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | bool IsUnderlineColorDefined() const |
michael@0 | 76 | { |
michael@0 | 77 | return (mDefinedStyles & DEFINED_UNDERLINE_COLOR) != 0; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool IsNoChangeStyle() const |
michael@0 | 81 | { |
michael@0 | 82 | return !IsForegroundColorDefined() && !IsBackgroundColorDefined() && |
michael@0 | 83 | IsLineStyleDefined() && mLineStyle == LINESTYLE_NONE; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | bool Equals(const TextRangeStyle& aOther) |
michael@0 | 87 | { |
michael@0 | 88 | if (mDefinedStyles != aOther.mDefinedStyles) |
michael@0 | 89 | return false; |
michael@0 | 90 | if (IsLineStyleDefined() && (mLineStyle != aOther.mLineStyle || |
michael@0 | 91 | !mIsBoldLine != !aOther.mIsBoldLine)) |
michael@0 | 92 | return false; |
michael@0 | 93 | if (IsForegroundColorDefined() && |
michael@0 | 94 | (mForegroundColor != aOther.mForegroundColor)) |
michael@0 | 95 | return false; |
michael@0 | 96 | if (IsBackgroundColorDefined() && |
michael@0 | 97 | (mBackgroundColor != aOther.mBackgroundColor)) |
michael@0 | 98 | return false; |
michael@0 | 99 | if (IsUnderlineColorDefined() && |
michael@0 | 100 | (mUnderlineColor != aOther.mUnderlineColor)) |
michael@0 | 101 | return false; |
michael@0 | 102 | return true; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | bool operator !=(const TextRangeStyle &aOther) |
michael@0 | 106 | { |
michael@0 | 107 | return !Equals(aOther); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | bool operator ==(const TextRangeStyle &aOther) |
michael@0 | 111 | { |
michael@0 | 112 | return Equals(aOther); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | uint8_t mDefinedStyles; |
michael@0 | 116 | uint8_t mLineStyle; // DEFINED_LINESTYLE |
michael@0 | 117 | |
michael@0 | 118 | bool mIsBoldLine; // DEFINED_LINESTYLE |
michael@0 | 119 | |
michael@0 | 120 | nscolor mForegroundColor; // DEFINED_FOREGROUND_COLOR |
michael@0 | 121 | nscolor mBackgroundColor; // DEFINED_BACKGROUND_COLOR |
michael@0 | 122 | nscolor mUnderlineColor; // DEFINED_UNDERLINE_COLOR |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | /****************************************************************************** |
michael@0 | 126 | * mozilla::TextRange |
michael@0 | 127 | ******************************************************************************/ |
michael@0 | 128 | |
michael@0 | 129 | #define NS_TEXTRANGE_CARETPOSITION 0x01 |
michael@0 | 130 | #define NS_TEXTRANGE_RAWINPUT 0x02 |
michael@0 | 131 | #define NS_TEXTRANGE_SELECTEDRAWTEXT 0x03 |
michael@0 | 132 | #define NS_TEXTRANGE_CONVERTEDTEXT 0x04 |
michael@0 | 133 | #define NS_TEXTRANGE_SELECTEDCONVERTEDTEXT 0x05 |
michael@0 | 134 | |
michael@0 | 135 | struct TextRange |
michael@0 | 136 | { |
michael@0 | 137 | TextRange() : |
michael@0 | 138 | mStartOffset(0), mEndOffset(0), mRangeType(0) |
michael@0 | 139 | { |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | uint32_t mStartOffset; |
michael@0 | 143 | // XXX Storing end offset makes the initializing code very complicated. |
michael@0 | 144 | // We should replace it with mLength. |
michael@0 | 145 | uint32_t mEndOffset; |
michael@0 | 146 | uint32_t mRangeType; |
michael@0 | 147 | |
michael@0 | 148 | TextRangeStyle mRangeStyle; |
michael@0 | 149 | |
michael@0 | 150 | uint32_t Length() const { return mEndOffset - mStartOffset; } |
michael@0 | 151 | |
michael@0 | 152 | bool IsClause() const |
michael@0 | 153 | { |
michael@0 | 154 | MOZ_ASSERT(mRangeType >= NS_TEXTRANGE_CARETPOSITION && |
michael@0 | 155 | mRangeType <= NS_TEXTRANGE_SELECTEDCONVERTEDTEXT, |
michael@0 | 156 | "Invalid range type"); |
michael@0 | 157 | return mRangeType != NS_TEXTRANGE_CARETPOSITION; |
michael@0 | 158 | } |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | /****************************************************************************** |
michael@0 | 162 | * mozilla::TextRangeArray |
michael@0 | 163 | ******************************************************************************/ |
michael@0 | 164 | class TextRangeArray MOZ_FINAL : public nsAutoTArray<TextRange, 10> |
michael@0 | 165 | { |
michael@0 | 166 | NS_INLINE_DECL_REFCOUNTING(TextRangeArray) |
michael@0 | 167 | |
michael@0 | 168 | public: |
michael@0 | 169 | bool IsComposing() const |
michael@0 | 170 | { |
michael@0 | 171 | for (uint32_t i = 0; i < Length(); ++i) { |
michael@0 | 172 | if (ElementAt(i).IsClause()) { |
michael@0 | 173 | return true; |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | return false; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | // Returns target clase offset. If there are selected clauses, this returns |
michael@0 | 180 | // the first selected clause offset. Otherwise, 0. |
michael@0 | 181 | uint32_t TargetClauseOffset() const |
michael@0 | 182 | { |
michael@0 | 183 | for (uint32_t i = 0; i < Length(); ++i) { |
michael@0 | 184 | const TextRange& range = ElementAt(i); |
michael@0 | 185 | if (range.mRangeType == NS_TEXTRANGE_SELECTEDRAWTEXT || |
michael@0 | 186 | range.mRangeType == NS_TEXTRANGE_SELECTEDCONVERTEDTEXT) { |
michael@0 | 187 | return range.mStartOffset; |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | return 0; |
michael@0 | 191 | } |
michael@0 | 192 | }; |
michael@0 | 193 | |
michael@0 | 194 | } // namespace mozilla |
michael@0 | 195 | |
michael@0 | 196 | #endif // mozilla_TextRage_h_ |