dom/events/EventStates.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/events/EventStates.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,297 @@
     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_EventStates_h_
    1.10 +#define mozilla_EventStates_h_
    1.11 +
    1.12 +#include "mozilla/Attributes.h"
    1.13 +#include "nsDebug.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +
    1.17 +/**
    1.18 + * EventStates is the class used to represent the event states of nsIContent
    1.19 + * instances. These states are calculated by IntrinsicState() and
    1.20 + * ContentStatesChanged() has to be called when one of them changes thus
    1.21 + * informing the layout/style engine of the change.
    1.22 + * Event states are associated with pseudo-classes.
    1.23 + */
    1.24 +class EventStates
    1.25 +{
    1.26 +public:
    1.27 +  typedef uint64_t InternalType;
    1.28 +
    1.29 +  MOZ_CONSTEXPR EventStates()
    1.30 +    : mStates(0)
    1.31 +  {
    1.32 +  }
    1.33 +
    1.34 +  // NOTE: the ideal scenario would be to have the default constructor public
    1.35 +  // setting mStates to 0 and this constructor (without = 0) private.
    1.36 +  // In that case, we could be sure that only macros at the end were creating
    1.37 +  // EventStates instances with mStates set to something else than 0.
    1.38 +  // Unfortunately, this constructor is needed at at least two places now.
    1.39 +  explicit MOZ_CONSTEXPR EventStates(InternalType aStates)
    1.40 +    : mStates(aStates)
    1.41 +  {
    1.42 +  }
    1.43 +
    1.44 +  MOZ_CONSTEXPR EventStates(const EventStates& aEventStates)
    1.45 +    : mStates(aEventStates.mStates)
    1.46 +  {
    1.47 +  }
    1.48 +
    1.49 +  EventStates& operator=(const EventStates& aEventStates)
    1.50 +  {
    1.51 +    mStates = aEventStates.mStates;
    1.52 +    return *this;
    1.53 +  }
    1.54 +
    1.55 +  EventStates MOZ_CONSTEXPR operator|(const EventStates& aEventStates) const
    1.56 +  {
    1.57 +    return EventStates(mStates | aEventStates.mStates);
    1.58 +  }
    1.59 +
    1.60 +  EventStates& operator|=(const EventStates& aEventStates)
    1.61 +  {
    1.62 +    mStates |= aEventStates.mStates;
    1.63 +    return *this;
    1.64 +  }
    1.65 +
    1.66 +  // NOTE: calling if (eventStates1 & eventStates2) will not build.
    1.67 +  // This might work correctly if operator bool() is defined
    1.68 +  // but using HasState, HasAllStates or HasAtLeastOneOfStates is recommended.
    1.69 +  EventStates MOZ_CONSTEXPR operator&(const EventStates& aEventStates) const
    1.70 +  {
    1.71 +    return EventStates(mStates & aEventStates.mStates);
    1.72 +  }
    1.73 +
    1.74 +  EventStates& operator&=(const EventStates& aEventStates)
    1.75 +  {
    1.76 +    mStates &= aEventStates.mStates;
    1.77 +    return *this;
    1.78 +  }
    1.79 +
    1.80 +  bool operator==(const EventStates& aEventStates) const
    1.81 +  {
    1.82 +    return mStates == aEventStates.mStates;
    1.83 +  }
    1.84 +
    1.85 +  bool operator!=(const EventStates& aEventStates) const
    1.86 +  {
    1.87 +    return mStates != aEventStates.mStates;
    1.88 +  }
    1.89 +
    1.90 +  EventStates operator~() const
    1.91 +  {
    1.92 +    return EventStates(~mStates);
    1.93 +  }
    1.94 +
    1.95 +  EventStates operator^(const EventStates& aEventStates) const
    1.96 +  {
    1.97 +    return EventStates(mStates ^ aEventStates.mStates);
    1.98 +  }
    1.99 +
   1.100 +  EventStates& operator^=(const EventStates& aEventStates)
   1.101 +  {
   1.102 +    mStates ^= aEventStates.mStates;
   1.103 +    return *this;
   1.104 +  }
   1.105 +
   1.106 +  /**
   1.107 +   * Returns true if the EventStates instance is empty.
   1.108 +   * A EventStates instance is empty if it contains no state.
   1.109 +   *
   1.110 +   * @return Whether if the object is empty.
   1.111 +   */
   1.112 +  bool IsEmpty() const
   1.113 +  {
   1.114 +    return mStates == 0;
   1.115 +  }
   1.116 +
   1.117 +  /**
   1.118 +   * Returns true if the EventStates instance contains the state
   1.119 +   * contained in aEventStates.
   1.120 +   * @note aEventStates should contain only one state.
   1.121 +   *
   1.122 +   * @param aEventStates The state to check.
   1.123 +   *
   1.124 +   * @return Whether the object has the state from aEventStates
   1.125 +   */
   1.126 +  bool HasState(EventStates aEventStates) const
   1.127 +  {
   1.128 +#ifdef DEBUG
   1.129 +    // If aEventStates.mStates is a power of two, it contains only one state
   1.130 +    // (or none, but we don't really care).
   1.131 +    if ((aEventStates.mStates & (aEventStates.mStates - 1))) {
   1.132 +      NS_ERROR("When calling HasState, "
   1.133 +               "EventStates object has to contain only one state!");
   1.134 +    }
   1.135 +#endif // DEBUG
   1.136 +    return mStates & aEventStates.mStates;
   1.137 +  }
   1.138 +
   1.139 +  /**
   1.140 +   * Returns true if the EventStates instance contains one of the states
   1.141 +   * contained in aEventStates.
   1.142 +   *
   1.143 +   * @param aEventStates The states to check.
   1.144 +   *
   1.145 +   * @return Whether the object has at least one state from aEventStates
   1.146 +   */
   1.147 +  bool HasAtLeastOneOfStates(EventStates aEventStates) const
   1.148 +  {
   1.149 +    return mStates & aEventStates.mStates;
   1.150 +  }
   1.151 +
   1.152 +  /**
   1.153 +   * Returns true if the EventStates instance contains all states
   1.154 +   * contained in aEventStates.
   1.155 +   *
   1.156 +   * @param aEventStates The states to check.
   1.157 +   *
   1.158 +   * @return Whether the object has all states from aEventStates
   1.159 +   */
   1.160 +  bool HasAllStates(EventStates aEventStates) const
   1.161 +  {
   1.162 +    return (mStates & aEventStates.mStates) == aEventStates.mStates;
   1.163 +  }
   1.164 +
   1.165 +  // We only need that method for inDOMUtils::GetContentState.
   1.166 +  // If inDOMUtils::GetContentState is removed, this method should be removed.
   1.167 +  InternalType GetInternalValue() const {
   1.168 +    return mStates;
   1.169 +  }
   1.170 +
   1.171 +private:
   1.172 +  InternalType mStates;
   1.173 +};
   1.174 +
   1.175 +} // namespace mozilla
   1.176 +
   1.177 +/**
   1.178 + * The following macros are creating EventStates instance with different
   1.179 + * values depending of their meaning.
   1.180 + * Ideally, EventStates instance with values different than 0 should only be
   1.181 + * created that way.
   1.182 + */
   1.183 +
   1.184 +// Helper to define a new EventStates macro.
   1.185 +#define NS_DEFINE_EVENT_STATE_MACRO(_val)               \
   1.186 +  (mozilla::EventStates(mozilla::EventStates::InternalType(1) << _val))
   1.187 +
   1.188 +// Mouse is down on content.
   1.189 +#define NS_EVENT_STATE_ACTIVE        NS_DEFINE_EVENT_STATE_MACRO(0)
   1.190 +// Content has focus.
   1.191 +#define NS_EVENT_STATE_FOCUS         NS_DEFINE_EVENT_STATE_MACRO(1)
   1.192 +// Mouse is hovering over content.
   1.193 +#define NS_EVENT_STATE_HOVER         NS_DEFINE_EVENT_STATE_MACRO(2)
   1.194 +// Drag is hovering over content.
   1.195 +#define NS_EVENT_STATE_DRAGOVER      NS_DEFINE_EVENT_STATE_MACRO(3)
   1.196 +// Content is URL's target (ref).
   1.197 +#define NS_EVENT_STATE_URLTARGET     NS_DEFINE_EVENT_STATE_MACRO(4)
   1.198 +// Content is checked.
   1.199 +#define NS_EVENT_STATE_CHECKED       NS_DEFINE_EVENT_STATE_MACRO(5)
   1.200 +// Content is enabled (and can be disabled).
   1.201 +#define NS_EVENT_STATE_ENABLED       NS_DEFINE_EVENT_STATE_MACRO(6)
   1.202 +// Content is disabled.
   1.203 +#define NS_EVENT_STATE_DISABLED      NS_DEFINE_EVENT_STATE_MACRO(7)
   1.204 +// Content is required.
   1.205 +#define NS_EVENT_STATE_REQUIRED      NS_DEFINE_EVENT_STATE_MACRO(8)
   1.206 +// Content is optional (and can be required).
   1.207 +#define NS_EVENT_STATE_OPTIONAL      NS_DEFINE_EVENT_STATE_MACRO(9)
   1.208 +// Link has been visited.
   1.209 +#define NS_EVENT_STATE_VISITED       NS_DEFINE_EVENT_STATE_MACRO(10)
   1.210 +// Link hasn't been visited.
   1.211 +#define NS_EVENT_STATE_UNVISITED     NS_DEFINE_EVENT_STATE_MACRO(11)
   1.212 +// Content is valid (and can be invalid).
   1.213 +#define NS_EVENT_STATE_VALID         NS_DEFINE_EVENT_STATE_MACRO(12)
   1.214 +// Content is invalid.
   1.215 +#define NS_EVENT_STATE_INVALID       NS_DEFINE_EVENT_STATE_MACRO(13)
   1.216 +// Content value is in-range (and can be out-of-range).
   1.217 +#define NS_EVENT_STATE_INRANGE       NS_DEFINE_EVENT_STATE_MACRO(14)
   1.218 +// Content value is out-of-range.
   1.219 +#define NS_EVENT_STATE_OUTOFRANGE    NS_DEFINE_EVENT_STATE_MACRO(15)
   1.220 +// These two are temporary (see bug 302188)
   1.221 +// Content is read-only.
   1.222 +#define NS_EVENT_STATE_MOZ_READONLY  NS_DEFINE_EVENT_STATE_MACRO(16)
   1.223 +// Content is editable.
   1.224 +#define NS_EVENT_STATE_MOZ_READWRITE NS_DEFINE_EVENT_STATE_MACRO(17)
   1.225 +// Content is the default one (meaning depends of the context).
   1.226 +#define NS_EVENT_STATE_DEFAULT       NS_DEFINE_EVENT_STATE_MACRO(18)
   1.227 +// Content could not be rendered (image/object/etc).
   1.228 +#define NS_EVENT_STATE_BROKEN        NS_DEFINE_EVENT_STATE_MACRO(19)
   1.229 +// Content disabled by the user (images turned off, say).
   1.230 +#define NS_EVENT_STATE_USERDISABLED  NS_DEFINE_EVENT_STATE_MACRO(20)
   1.231 +// Content suppressed by the user (ad blocking, etc).
   1.232 +#define NS_EVENT_STATE_SUPPRESSED    NS_DEFINE_EVENT_STATE_MACRO(21)
   1.233 +// Content is still loading such that there is nothing to show the
   1.234 +// user (eg an image which hasn't started coming in yet).
   1.235 +#define NS_EVENT_STATE_LOADING       NS_DEFINE_EVENT_STATE_MACRO(22)
   1.236 +// Content is of a type that gecko can't handle.
   1.237 +#define NS_EVENT_STATE_TYPE_UNSUPPORTED NS_DEFINE_EVENT_STATE_MACRO(23)
   1.238 +#define NS_EVENT_STATE_INCREMENT_SCRIPT_LEVEL NS_DEFINE_EVENT_STATE_MACRO(24)
   1.239 +// Handler for the content has been blocked.
   1.240 +#define NS_EVENT_STATE_HANDLER_BLOCKED NS_DEFINE_EVENT_STATE_MACRO(25)
   1.241 +// Handler for the content has been disabled.
   1.242 +#define NS_EVENT_STATE_HANDLER_DISABLED NS_DEFINE_EVENT_STATE_MACRO(26)
   1.243 +// Content is in the indeterminate state.
   1.244 +#define NS_EVENT_STATE_INDETERMINATE NS_DEFINE_EVENT_STATE_MACRO(27)
   1.245 +// Handler for the content has crashed
   1.246 +#define NS_EVENT_STATE_HANDLER_CRASHED NS_DEFINE_EVENT_STATE_MACRO(28)
   1.247 +// Content has focus and should show a ring.
   1.248 +#define NS_EVENT_STATE_FOCUSRING     NS_DEFINE_EVENT_STATE_MACRO(29)
   1.249 +// Content is a submit control and the form isn't valid.
   1.250 +#define NS_EVENT_STATE_MOZ_SUBMITINVALID NS_DEFINE_EVENT_STATE_MACRO(30)
   1.251 +// UI friendly version of :invalid pseudo-class.
   1.252 +#define NS_EVENT_STATE_MOZ_UI_INVALID NS_DEFINE_EVENT_STATE_MACRO(31)
   1.253 +// UI friendly version of :valid pseudo-class.
   1.254 +#define NS_EVENT_STATE_MOZ_UI_VALID NS_DEFINE_EVENT_STATE_MACRO(32)
   1.255 +// Content is the full screen element, or a frame containing the
   1.256 +// current full-screen element.
   1.257 +#define NS_EVENT_STATE_FULL_SCREEN   NS_DEFINE_EVENT_STATE_MACRO(33)
   1.258 +// Content is an ancestor of the DOM full-screen element.
   1.259 +#define NS_EVENT_STATE_FULL_SCREEN_ANCESTOR   NS_DEFINE_EVENT_STATE_MACRO(34)
   1.260 +// Handler for click to play plugin
   1.261 +#define NS_EVENT_STATE_TYPE_CLICK_TO_PLAY NS_DEFINE_EVENT_STATE_MACRO(35)
   1.262 +// Content is in the optimum region.
   1.263 +#define NS_EVENT_STATE_OPTIMUM NS_DEFINE_EVENT_STATE_MACRO(36)
   1.264 +// Content is in the suboptimal region.
   1.265 +#define NS_EVENT_STATE_SUB_OPTIMUM NS_DEFINE_EVENT_STATE_MACRO(37)
   1.266 +// Content is in the sub-suboptimal region.
   1.267 +#define NS_EVENT_STATE_SUB_SUB_OPTIMUM NS_DEFINE_EVENT_STATE_MACRO(38)
   1.268 +// Handler for click to play plugin (vulnerable w/update)
   1.269 +#define NS_EVENT_STATE_VULNERABLE_UPDATABLE NS_DEFINE_EVENT_STATE_MACRO(39)
   1.270 +// Handler for click to play plugin (vulnerable w/no update)
   1.271 +#define NS_EVENT_STATE_VULNERABLE_NO_UPDATE NS_DEFINE_EVENT_STATE_MACRO(40)
   1.272 +// Platform does not support plugin content (some mobile platforms)
   1.273 +#define NS_EVENT_STATE_TYPE_UNSUPPORTED_PLATFORM NS_DEFINE_EVENT_STATE_MACRO(41)
   1.274 +// Element is ltr (for :dir pseudo-class)
   1.275 +#define NS_EVENT_STATE_LTR NS_DEFINE_EVENT_STATE_MACRO(42)
   1.276 +// Element is rtl (for :dir pseudo-class)
   1.277 +#define NS_EVENT_STATE_RTL NS_DEFINE_EVENT_STATE_MACRO(43)
   1.278 +// Handler for play preview plugin
   1.279 +#define NS_EVENT_STATE_TYPE_PLAY_PREVIEW NS_DEFINE_EVENT_STATE_MACRO(44)
   1.280 +// Element is highlighted (devtools inspector)
   1.281 +#define NS_EVENT_STATE_DEVTOOLS_HIGHLIGHTED NS_DEFINE_EVENT_STATE_MACRO(45)
   1.282 +
   1.283 +// Event state that is used for values that need to be parsed but do nothing.
   1.284 +#define NS_EVENT_STATE_IGNORE NS_DEFINE_EVENT_STATE_MACRO(63)
   1.285 +
   1.286 +/**
   1.287 + * NOTE: do not go over 63 without updating EventStates::InternalType!
   1.288 + */
   1.289 +
   1.290 +#define DIRECTION_STATES (NS_EVENT_STATE_LTR | NS_EVENT_STATE_RTL)
   1.291 +
   1.292 +#define ESM_MANAGED_STATES (NS_EVENT_STATE_ACTIVE | NS_EVENT_STATE_FOCUS |     \
   1.293 +                            NS_EVENT_STATE_HOVER | NS_EVENT_STATE_DRAGOVER |   \
   1.294 +                            NS_EVENT_STATE_URLTARGET | NS_EVENT_STATE_FOCUSRING | \
   1.295 +                            NS_EVENT_STATE_FULL_SCREEN | NS_EVENT_STATE_FULL_SCREEN_ANCESTOR)
   1.296 +
   1.297 +#define INTRINSIC_STATES (~ESM_MANAGED_STATES)
   1.298 +
   1.299 +#endif // mozilla_EventStates_h_
   1.300 +

mercurial