1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_TextRage_h_
7 #define mozilla_TextRage_h_
11 #include "nsAutoPtr.h"
13 #include "nsStyleConsts.h"
18 /******************************************************************************
19 * mozilla::TextRangeStyle
20 ******************************************************************************/
26 LINESTYLE_NONE
= NS_STYLE_TEXT_DECORATION_STYLE_NONE
,
27 LINESTYLE_SOLID
= NS_STYLE_TEXT_DECORATION_STYLE_SOLID
,
28 LINESTYLE_DOTTED
= NS_STYLE_TEXT_DECORATION_STYLE_DOTTED
,
29 LINESTYLE_DASHED
= NS_STYLE_TEXT_DECORATION_STYLE_DASHED
,
30 LINESTYLE_DOUBLE
= NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE
,
31 LINESTYLE_WAVY
= NS_STYLE_TEXT_DECORATION_STYLE_WAVY
37 DEFINED_LINESTYLE
= 0x01,
38 DEFINED_FOREGROUND_COLOR
= 0x02,
39 DEFINED_BACKGROUND_COLOR
= 0x04,
40 DEFINED_UNDERLINE_COLOR
= 0x08
43 // Initialize all members, because TextRange instances may be compared by
52 mDefinedStyles
= DEFINED_NONE
;
53 mLineStyle
= LINESTYLE_NONE
;
55 mForegroundColor
= mBackgroundColor
= mUnderlineColor
= NS_RGBA(0, 0, 0, 0);
58 bool IsDefined() const { return mDefinedStyles
!= DEFINED_NONE
; }
60 bool IsLineStyleDefined() const
62 return (mDefinedStyles
& DEFINED_LINESTYLE
) != 0;
65 bool IsForegroundColorDefined() const
67 return (mDefinedStyles
& DEFINED_FOREGROUND_COLOR
) != 0;
70 bool IsBackgroundColorDefined() const
72 return (mDefinedStyles
& DEFINED_BACKGROUND_COLOR
) != 0;
75 bool IsUnderlineColorDefined() const
77 return (mDefinedStyles
& DEFINED_UNDERLINE_COLOR
) != 0;
80 bool IsNoChangeStyle() const
82 return !IsForegroundColorDefined() && !IsBackgroundColorDefined() &&
83 IsLineStyleDefined() && mLineStyle
== LINESTYLE_NONE
;
86 bool Equals(const TextRangeStyle
& aOther
)
88 if (mDefinedStyles
!= aOther
.mDefinedStyles
)
90 if (IsLineStyleDefined() && (mLineStyle
!= aOther
.mLineStyle
||
91 !mIsBoldLine
!= !aOther
.mIsBoldLine
))
93 if (IsForegroundColorDefined() &&
94 (mForegroundColor
!= aOther
.mForegroundColor
))
96 if (IsBackgroundColorDefined() &&
97 (mBackgroundColor
!= aOther
.mBackgroundColor
))
99 if (IsUnderlineColorDefined() &&
100 (mUnderlineColor
!= aOther
.mUnderlineColor
))
105 bool operator !=(const TextRangeStyle
&aOther
)
107 return !Equals(aOther
);
110 bool operator ==(const TextRangeStyle
&aOther
)
112 return Equals(aOther
);
115 uint8_t mDefinedStyles
;
116 uint8_t mLineStyle
; // DEFINED_LINESTYLE
118 bool mIsBoldLine
; // DEFINED_LINESTYLE
120 nscolor mForegroundColor
; // DEFINED_FOREGROUND_COLOR
121 nscolor mBackgroundColor
; // DEFINED_BACKGROUND_COLOR
122 nscolor mUnderlineColor
; // DEFINED_UNDERLINE_COLOR
125 /******************************************************************************
127 ******************************************************************************/
129 #define NS_TEXTRANGE_CARETPOSITION 0x01
130 #define NS_TEXTRANGE_RAWINPUT 0x02
131 #define NS_TEXTRANGE_SELECTEDRAWTEXT 0x03
132 #define NS_TEXTRANGE_CONVERTEDTEXT 0x04
133 #define NS_TEXTRANGE_SELECTEDCONVERTEDTEXT 0x05
138 mStartOffset(0), mEndOffset(0), mRangeType(0)
142 uint32_t mStartOffset
;
143 // XXX Storing end offset makes the initializing code very complicated.
144 // We should replace it with mLength.
148 TextRangeStyle mRangeStyle
;
150 uint32_t Length() const { return mEndOffset
- mStartOffset
; }
152 bool IsClause() const
154 MOZ_ASSERT(mRangeType
>= NS_TEXTRANGE_CARETPOSITION
&&
155 mRangeType
<= NS_TEXTRANGE_SELECTEDCONVERTEDTEXT
,
156 "Invalid range type");
157 return mRangeType
!= NS_TEXTRANGE_CARETPOSITION
;
161 /******************************************************************************
162 * mozilla::TextRangeArray
163 ******************************************************************************/
164 class TextRangeArray MOZ_FINAL
: public nsAutoTArray
<TextRange
, 10>
168 NS_INLINE_DECL_REFCOUNTING(TextRangeArray
)
171 bool IsComposing() const
173 for (uint32_t i
= 0; i
< Length(); ++i
) {
174 if (ElementAt(i
).IsClause()) {
181 // Returns target clase offset. If there are selected clauses, this returns
182 // the first selected clause offset. Otherwise, 0.
183 uint32_t TargetClauseOffset() const
185 for (uint32_t i
= 0; i
< Length(); ++i
) {
186 const TextRange
& range
= ElementAt(i
);
187 if (range
.mRangeType
== NS_TEXTRANGE_SELECTEDRAWTEXT
||
188 range
.mRangeType
== NS_TEXTRANGE_SELECTEDCONVERTEDTEXT
) {
189 return range
.mStartOffset
;
196 } // namespace mozilla
198 #endif // mozilla_TextRage_h_