2013-10-22 13:27:36 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_TextRage_h_
|
|
|
|
#define mozilla_TextRage_h_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-06-04 00:49:21 +00:00
|
|
|
#include "mozilla/EventForwards.h"
|
|
|
|
|
2013-10-22 13:27:36 +00:00
|
|
|
#include "nsColor.h"
|
2016-06-11 02:06:37 +00:00
|
|
|
#include "nsISelectionController.h"
|
2015-01-28 06:27:33 +00:00
|
|
|
#include "nsITextInputProcessor.h"
|
2014-03-04 13:48:26 +00:00
|
|
|
#include "nsTArray.h"
|
2013-10-22 13:27:36 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::TextRangeStyle
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
struct TextRangeStyle {
|
2020-02-17 07:53:46 +00:00
|
|
|
typedef uint8_t LineStyleType;
|
|
|
|
// FYI: Modify IME_RANGE_LINE_* too when you modify LineStyle.
|
|
|
|
enum class LineStyle : LineStyleType {
|
|
|
|
None,
|
|
|
|
Solid,
|
|
|
|
Dotted,
|
|
|
|
Dashed,
|
|
|
|
Double,
|
|
|
|
Wavy,
|
2013-10-22 13:27:36 +00:00
|
|
|
};
|
2020-02-17 07:53:46 +00:00
|
|
|
inline static LineStyle ToLineStyle(RawTextRangeType aRawLineStyle) {
|
|
|
|
switch (static_cast<LineStyle>(aRawLineStyle)) {
|
|
|
|
case LineStyle::None:
|
|
|
|
case LineStyle::Solid:
|
|
|
|
case LineStyle::Dotted:
|
|
|
|
case LineStyle::Dashed:
|
|
|
|
case LineStyle::Double:
|
|
|
|
case LineStyle::Wavy:
|
|
|
|
return static_cast<LineStyle>(aRawLineStyle);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT_UNREACHABLE("aRawLineStyle value is invalid");
|
|
|
|
return LineStyle::None;
|
|
|
|
}
|
2013-10-22 13:27:36 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
DEFINED_NONE = 0x00,
|
|
|
|
DEFINED_LINESTYLE = 0x01,
|
|
|
|
DEFINED_FOREGROUND_COLOR = 0x02,
|
|
|
|
DEFINED_BACKGROUND_COLOR = 0x04,
|
|
|
|
DEFINED_UNDERLINE_COLOR = 0x08
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize all members, because TextRange instances may be compared by
|
|
|
|
// memcomp.
|
2020-02-11 01:23:12 +00:00
|
|
|
//
|
|
|
|
// FIXME(emilio): I don't think that'd be sound, as it has padding which the
|
|
|
|
// compiler is not guaranteed to initialize.
|
2013-10-22 13:27:36 +00:00
|
|
|
TextRangeStyle() { Clear(); }
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
mDefinedStyles = DEFINED_NONE;
|
2020-02-17 07:53:46 +00:00
|
|
|
mLineStyle = LineStyle::None;
|
2013-10-22 13:27:36 +00:00
|
|
|
mIsBoldLine = false;
|
|
|
|
mForegroundColor = mBackgroundColor = mUnderlineColor = NS_RGBA(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDefined() const { return mDefinedStyles != DEFINED_NONE; }
|
|
|
|
|
|
|
|
bool IsLineStyleDefined() const {
|
|
|
|
return (mDefinedStyles & DEFINED_LINESTYLE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsForegroundColorDefined() const {
|
|
|
|
return (mDefinedStyles & DEFINED_FOREGROUND_COLOR) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsBackgroundColorDefined() const {
|
|
|
|
return (mDefinedStyles & DEFINED_BACKGROUND_COLOR) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsUnderlineColorDefined() const {
|
|
|
|
return (mDefinedStyles & DEFINED_UNDERLINE_COLOR) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNoChangeStyle() const {
|
|
|
|
return !IsForegroundColorDefined() && !IsBackgroundColorDefined() &&
|
2020-02-17 07:53:46 +00:00
|
|
|
IsLineStyleDefined() && mLineStyle == LineStyle::None;
|
2013-10-22 13:27:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 03:20:02 +00:00
|
|
|
bool Equals(const TextRangeStyle& aOther) const {
|
2013-10-22 13:27:36 +00:00
|
|
|
if (mDefinedStyles != aOther.mDefinedStyles) return false;
|
|
|
|
if (IsLineStyleDefined() && (mLineStyle != aOther.mLineStyle ||
|
|
|
|
!mIsBoldLine != !aOther.mIsBoldLine))
|
|
|
|
return false;
|
|
|
|
if (IsForegroundColorDefined() &&
|
|
|
|
(mForegroundColor != aOther.mForegroundColor))
|
|
|
|
return false;
|
|
|
|
if (IsBackgroundColorDefined() &&
|
|
|
|
(mBackgroundColor != aOther.mBackgroundColor))
|
|
|
|
return false;
|
|
|
|
if (IsUnderlineColorDefined() &&
|
|
|
|
(mUnderlineColor != aOther.mUnderlineColor))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:20:02 +00:00
|
|
|
bool operator!=(const TextRangeStyle& aOther) const {
|
2013-10-22 13:27:36 +00:00
|
|
|
return !Equals(aOther);
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:20:02 +00:00
|
|
|
bool operator==(const TextRangeStyle& aOther) const { return Equals(aOther); }
|
2013-10-22 13:27:36 +00:00
|
|
|
|
|
|
|
uint8_t mDefinedStyles;
|
2020-02-17 07:53:46 +00:00
|
|
|
LineStyle mLineStyle; // DEFINED_LINESTYLE
|
2013-10-22 13:27:36 +00:00
|
|
|
|
|
|
|
bool mIsBoldLine; // DEFINED_LINESTYLE
|
|
|
|
|
|
|
|
nscolor mForegroundColor; // DEFINED_FOREGROUND_COLOR
|
|
|
|
nscolor mBackgroundColor; // DEFINED_BACKGROUND_COLOR
|
|
|
|
nscolor mUnderlineColor; // DEFINED_UNDERLINE_COLOR
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::TextRange
|
|
|
|
******************************************************************************/
|
|
|
|
|
2016-06-04 00:49:21 +00:00
|
|
|
enum class TextRangeType : RawTextRangeType {
|
2016-06-03 10:15:21 +00:00
|
|
|
eUninitialized = 0x00,
|
|
|
|
eCaret = 0x01,
|
|
|
|
eRawClause = nsITextInputProcessor::ATTR_RAW_CLAUSE,
|
2016-06-03 09:57:21 +00:00
|
|
|
eSelectedRawClause = nsITextInputProcessor::ATTR_SELECTED_RAW_CLAUSE,
|
2016-06-03 10:15:21 +00:00
|
|
|
eConvertedClause = nsITextInputProcessor::ATTR_CONVERTED_CLAUSE,
|
|
|
|
eSelectedClause = nsITextInputProcessor::ATTR_SELECTED_CLAUSE
|
2015-01-28 06:27:33 +00:00
|
|
|
};
|
2013-10-22 13:27:36 +00:00
|
|
|
|
2016-06-04 00:49:21 +00:00
|
|
|
bool IsValidRawTextRangeValue(RawTextRangeType aRawTextRangeValue);
|
|
|
|
RawTextRangeType ToRawTextRangeType(TextRangeType aTextRangeType);
|
|
|
|
TextRangeType ToTextRangeType(RawTextRangeType aRawTextRangeType);
|
|
|
|
const char* ToChar(TextRangeType aTextRangeType);
|
2016-06-11 02:06:37 +00:00
|
|
|
SelectionType ToSelectionType(TextRangeType aTextRangeType);
|
|
|
|
|
2013-10-22 13:27:36 +00:00
|
|
|
struct TextRange {
|
2016-06-04 00:49:21 +00:00
|
|
|
TextRange()
|
|
|
|
: mStartOffset(0),
|
|
|
|
mEndOffset(0),
|
2016-06-03 09:32:22 +00:00
|
|
|
mRangeType(TextRangeType::eUninitialized) {}
|
2013-10-22 13:27:36 +00:00
|
|
|
|
|
|
|
uint32_t mStartOffset;
|
|
|
|
// XXX Storing end offset makes the initializing code very complicated.
|
|
|
|
// We should replace it with mLength.
|
|
|
|
uint32_t mEndOffset;
|
|
|
|
|
|
|
|
TextRangeStyle mRangeStyle;
|
|
|
|
|
2016-06-04 00:49:21 +00:00
|
|
|
TextRangeType mRangeType;
|
|
|
|
|
2013-10-22 13:27:36 +00:00
|
|
|
uint32_t Length() const { return mEndOffset - mStartOffset; }
|
2014-02-12 13:02:56 +00:00
|
|
|
|
2016-06-03 09:40:06 +00:00
|
|
|
bool IsClause() const { return mRangeType != TextRangeType::eCaret; }
|
2015-02-11 03:20:02 +00:00
|
|
|
|
|
|
|
bool Equals(const TextRange& aOther) const {
|
|
|
|
return mStartOffset == aOther.mStartOffset &&
|
|
|
|
mEndOffset == aOther.mEndOffset && mRangeType == aOther.mRangeType &&
|
|
|
|
mRangeStyle == aOther.mRangeStyle;
|
|
|
|
}
|
2015-05-01 04:49:29 +00:00
|
|
|
|
|
|
|
void RemoveCharacter(uint32_t aOffset) {
|
|
|
|
if (mStartOffset > aOffset) {
|
|
|
|
--mStartOffset;
|
|
|
|
--mEndOffset;
|
|
|
|
} else if (mEndOffset > aOffset) {
|
|
|
|
--mEndOffset;
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 13:27:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::TextRangeArray
|
|
|
|
******************************************************************************/
|
2016-02-02 15:36:30 +00:00
|
|
|
class TextRangeArray final : public AutoTArray<TextRange, 10> {
|
2015-06-18 04:43:18 +00:00
|
|
|
friend class WidgetCompositionEvent;
|
|
|
|
|
2020-03-16 10:56:57 +00:00
|
|
|
~TextRangeArray() = default;
|
2014-06-20 11:08:25 +00:00
|
|
|
|
2014-03-04 13:48:26 +00:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(TextRangeArray)
|
|
|
|
|
2015-06-11 10:50:15 +00:00
|
|
|
const TextRange* GetTargetClause() const {
|
|
|
|
for (uint32_t i = 0; i < Length(); ++i) {
|
|
|
|
const TextRange& range = ElementAt(i);
|
2016-06-03 09:57:21 +00:00
|
|
|
if (range.mRangeType == TextRangeType::eSelectedRawClause ||
|
2016-06-03 10:15:21 +00:00
|
|
|
range.mRangeType == TextRangeType::eSelectedClause) {
|
2015-06-11 10:50:15 +00:00
|
|
|
return ⦥
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns target clause offset. If there are selected clauses, this returns
|
2014-03-04 13:48:26 +00:00
|
|
|
// the first selected clause offset. Otherwise, 0.
|
|
|
|
uint32_t TargetClauseOffset() const {
|
2015-06-11 10:50:15 +00:00
|
|
|
const TextRange* range = GetTargetClause();
|
|
|
|
return range ? range->mStartOffset : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns target clause length. If there are selected clauses, this returns
|
|
|
|
// the first selected clause length. Otherwise, UINT32_MAX.
|
|
|
|
uint32_t TargetClauseLength() const {
|
|
|
|
const TextRange* range = GetTargetClause();
|
|
|
|
return range ? range->Length() : UINT32_MAX;
|
2014-03-04 13:48:26 +00:00
|
|
|
}
|
2015-02-11 03:20:02 +00:00
|
|
|
|
2015-06-18 04:43:18 +00:00
|
|
|
public:
|
|
|
|
bool IsComposing() const {
|
|
|
|
for (uint32_t i = 0; i < Length(); ++i) {
|
|
|
|
if (ElementAt(i).IsClause()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-11 03:20:02 +00:00
|
|
|
bool Equals(const TextRangeArray& aOther) const {
|
|
|
|
size_t len = Length();
|
|
|
|
if (len != aOther.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
if (!ElementAt(i).Equals(aOther.ElementAt(i))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-01 04:49:29 +00:00
|
|
|
|
|
|
|
void RemoveCharacter(uint32_t aOffset) {
|
|
|
|
for (size_t i = 0, len = Length(); i < len; i++) {
|
|
|
|
ElementAt(i).RemoveCharacter(aOffset);
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 13:57:37 +00:00
|
|
|
|
|
|
|
bool HasCaret() const {
|
|
|
|
for (const TextRange& range : *this) {
|
2016-06-03 09:40:06 +00:00
|
|
|
if (range.mRangeType == TextRangeType::eCaret) {
|
2015-12-29 13:57:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-29 13:57:38 +00:00
|
|
|
|
2016-06-20 07:34:40 +00:00
|
|
|
bool HasClauses() const {
|
|
|
|
for (const TextRange& range : *this) {
|
|
|
|
if (range.IsClause()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:57:38 +00:00
|
|
|
uint32_t GetCaretPosition() const {
|
|
|
|
for (const TextRange& range : *this) {
|
2016-06-03 09:40:06 +00:00
|
|
|
if (range.mRangeType == TextRangeType::eCaret) {
|
2015-12-29 13:57:38 +00:00
|
|
|
return range.mStartOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
2016-06-20 07:34:40 +00:00
|
|
|
|
|
|
|
const TextRange* GetFirstClause() const {
|
|
|
|
for (const TextRange& range : *this) {
|
|
|
|
// Look for the range of a clause whose start offset is 0 because the
|
|
|
|
// first clause's start offset is always 0.
|
|
|
|
if (range.IsClause() && !range.mStartOffset) {
|
|
|
|
return ⦥
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!HasClauses());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-03-04 13:48:26 +00:00
|
|
|
};
|
2013-10-22 13:27:36 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_TextRage_h_
|