widget/TextRange.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/TextRange.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,196 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_TextRage_h_
    1.10 +#define mozilla_TextRage_h_
    1.11 +
    1.12 +#include <stdint.h>
    1.13 +
    1.14 +#include "nsAutoPtr.h"
    1.15 +#include "nsColor.h"
    1.16 +#include "nsStyleConsts.h"
    1.17 +#include "nsTArray.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +
    1.21 +/******************************************************************************
    1.22 + * mozilla::TextRangeStyle
    1.23 + ******************************************************************************/
    1.24 +
    1.25 +struct TextRangeStyle
    1.26 +{
    1.27 +  enum
    1.28 +  {
    1.29 +    LINESTYLE_NONE   = NS_STYLE_TEXT_DECORATION_STYLE_NONE,
    1.30 +    LINESTYLE_SOLID  = NS_STYLE_TEXT_DECORATION_STYLE_SOLID,
    1.31 +    LINESTYLE_DOTTED = NS_STYLE_TEXT_DECORATION_STYLE_DOTTED,
    1.32 +    LINESTYLE_DASHED = NS_STYLE_TEXT_DECORATION_STYLE_DASHED,
    1.33 +    LINESTYLE_DOUBLE = NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE,
    1.34 +    LINESTYLE_WAVY   = NS_STYLE_TEXT_DECORATION_STYLE_WAVY
    1.35 +  };
    1.36 +
    1.37 +  enum
    1.38 +  {
    1.39 +    DEFINED_NONE             = 0x00,
    1.40 +    DEFINED_LINESTYLE        = 0x01,
    1.41 +    DEFINED_FOREGROUND_COLOR = 0x02,
    1.42 +    DEFINED_BACKGROUND_COLOR = 0x04,
    1.43 +    DEFINED_UNDERLINE_COLOR  = 0x08
    1.44 +  };
    1.45 +
    1.46 +  // Initialize all members, because TextRange instances may be compared by
    1.47 +  // memcomp.
    1.48 +  TextRangeStyle()
    1.49 +  {
    1.50 +    Clear();
    1.51 +  }
    1.52 +
    1.53 +  void Clear()
    1.54 +  {
    1.55 +    mDefinedStyles = DEFINED_NONE;
    1.56 +    mLineStyle = LINESTYLE_NONE;
    1.57 +    mIsBoldLine = false;
    1.58 +    mForegroundColor = mBackgroundColor = mUnderlineColor = NS_RGBA(0, 0, 0, 0);
    1.59 +  }
    1.60 +
    1.61 +  bool IsDefined() const { return mDefinedStyles != DEFINED_NONE; }
    1.62 +
    1.63 +  bool IsLineStyleDefined() const
    1.64 +  {
    1.65 +    return (mDefinedStyles & DEFINED_LINESTYLE) != 0;
    1.66 +  }
    1.67 +
    1.68 +  bool IsForegroundColorDefined() const
    1.69 +  {
    1.70 +    return (mDefinedStyles & DEFINED_FOREGROUND_COLOR) != 0;
    1.71 +  }
    1.72 +
    1.73 +  bool IsBackgroundColorDefined() const
    1.74 +  {
    1.75 +    return (mDefinedStyles & DEFINED_BACKGROUND_COLOR) != 0;
    1.76 +  }
    1.77 +
    1.78 +  bool IsUnderlineColorDefined() const
    1.79 +  {
    1.80 +    return (mDefinedStyles & DEFINED_UNDERLINE_COLOR) != 0;
    1.81 +  }
    1.82 +
    1.83 +  bool IsNoChangeStyle() const
    1.84 +  {
    1.85 +    return !IsForegroundColorDefined() && !IsBackgroundColorDefined() &&
    1.86 +           IsLineStyleDefined() && mLineStyle == LINESTYLE_NONE;
    1.87 +  }
    1.88 +
    1.89 +  bool Equals(const TextRangeStyle& aOther)
    1.90 +  {
    1.91 +    if (mDefinedStyles != aOther.mDefinedStyles)
    1.92 +      return false;
    1.93 +    if (IsLineStyleDefined() && (mLineStyle != aOther.mLineStyle ||
    1.94 +                                 !mIsBoldLine != !aOther.mIsBoldLine))
    1.95 +      return false;
    1.96 +    if (IsForegroundColorDefined() &&
    1.97 +        (mForegroundColor != aOther.mForegroundColor))
    1.98 +      return false;
    1.99 +    if (IsBackgroundColorDefined() &&
   1.100 +        (mBackgroundColor != aOther.mBackgroundColor))
   1.101 +      return false;
   1.102 +    if (IsUnderlineColorDefined() &&
   1.103 +        (mUnderlineColor != aOther.mUnderlineColor))
   1.104 +      return false;
   1.105 +    return true;
   1.106 +  }
   1.107 +
   1.108 +  bool operator !=(const TextRangeStyle &aOther)
   1.109 +  {
   1.110 +    return !Equals(aOther);
   1.111 +  }
   1.112 +
   1.113 +  bool operator ==(const TextRangeStyle &aOther)
   1.114 +  {
   1.115 +    return Equals(aOther);
   1.116 +  }
   1.117 +
   1.118 +  uint8_t mDefinedStyles;
   1.119 +  uint8_t mLineStyle;        // DEFINED_LINESTYLE
   1.120 +
   1.121 +  bool mIsBoldLine;  // DEFINED_LINESTYLE
   1.122 +
   1.123 +  nscolor mForegroundColor;  // DEFINED_FOREGROUND_COLOR
   1.124 +  nscolor mBackgroundColor;  // DEFINED_BACKGROUND_COLOR
   1.125 +  nscolor mUnderlineColor;   // DEFINED_UNDERLINE_COLOR
   1.126 +};
   1.127 +
   1.128 +/******************************************************************************
   1.129 + * mozilla::TextRange
   1.130 + ******************************************************************************/
   1.131 +
   1.132 +#define NS_TEXTRANGE_CARETPOSITION         0x01
   1.133 +#define NS_TEXTRANGE_RAWINPUT              0x02
   1.134 +#define NS_TEXTRANGE_SELECTEDRAWTEXT       0x03
   1.135 +#define NS_TEXTRANGE_CONVERTEDTEXT         0x04
   1.136 +#define NS_TEXTRANGE_SELECTEDCONVERTEDTEXT 0x05
   1.137 +
   1.138 +struct TextRange
   1.139 +{
   1.140 +  TextRange() :
   1.141 +    mStartOffset(0), mEndOffset(0), mRangeType(0)
   1.142 +  {
   1.143 +  }
   1.144 +
   1.145 +  uint32_t mStartOffset;
   1.146 +  // XXX Storing end offset makes the initializing code very complicated.
   1.147 +  //     We should replace it with mLength.
   1.148 +  uint32_t mEndOffset;
   1.149 +  uint32_t mRangeType;
   1.150 +
   1.151 +  TextRangeStyle mRangeStyle;
   1.152 +
   1.153 +  uint32_t Length() const { return mEndOffset - mStartOffset; }
   1.154 +
   1.155 +  bool IsClause() const
   1.156 +  {
   1.157 +    MOZ_ASSERT(mRangeType >= NS_TEXTRANGE_CARETPOSITION &&
   1.158 +                 mRangeType <= NS_TEXTRANGE_SELECTEDCONVERTEDTEXT,
   1.159 +               "Invalid range type");
   1.160 +    return mRangeType != NS_TEXTRANGE_CARETPOSITION;
   1.161 +  }
   1.162 +};
   1.163 +
   1.164 +/******************************************************************************
   1.165 + * mozilla::TextRangeArray
   1.166 + ******************************************************************************/
   1.167 +class TextRangeArray MOZ_FINAL : public nsAutoTArray<TextRange, 10>
   1.168 +{
   1.169 +  NS_INLINE_DECL_REFCOUNTING(TextRangeArray)
   1.170 +
   1.171 +public:
   1.172 +  bool IsComposing() const
   1.173 +  {
   1.174 +    for (uint32_t i = 0; i < Length(); ++i) {
   1.175 +      if (ElementAt(i).IsClause()) {
   1.176 +        return true;
   1.177 +      }
   1.178 +    }
   1.179 +    return false;
   1.180 +  }
   1.181 +
   1.182 +  // Returns target clase offset.  If there are selected clauses, this returns
   1.183 +  // the first selected clause offset.  Otherwise, 0.
   1.184 +  uint32_t TargetClauseOffset() const
   1.185 +  {
   1.186 +    for (uint32_t i = 0; i < Length(); ++i) {
   1.187 +      const TextRange& range = ElementAt(i);
   1.188 +      if (range.mRangeType == NS_TEXTRANGE_SELECTEDRAWTEXT ||
   1.189 +          range.mRangeType == NS_TEXTRANGE_SELECTEDCONVERTEDTEXT) {
   1.190 +        return range.mStartOffset;
   1.191 +      }
   1.192 +    }
   1.193 +    return 0;
   1.194 +  }
   1.195 +};
   1.196 +
   1.197 +} // namespace mozilla
   1.198 +
   1.199 +#endif // mozilla_TextRage_h_

mercurial