dom/events/EventStates.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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_EventStates_h_
michael@0 7 #define mozilla_EventStates_h_
michael@0 8
michael@0 9 #include "mozilla/Attributes.h"
michael@0 10 #include "nsDebug.h"
michael@0 11
michael@0 12 namespace mozilla {
michael@0 13
michael@0 14 /**
michael@0 15 * EventStates is the class used to represent the event states of nsIContent
michael@0 16 * instances. These states are calculated by IntrinsicState() and
michael@0 17 * ContentStatesChanged() has to be called when one of them changes thus
michael@0 18 * informing the layout/style engine of the change.
michael@0 19 * Event states are associated with pseudo-classes.
michael@0 20 */
michael@0 21 class EventStates
michael@0 22 {
michael@0 23 public:
michael@0 24 typedef uint64_t InternalType;
michael@0 25
michael@0 26 MOZ_CONSTEXPR EventStates()
michael@0 27 : mStates(0)
michael@0 28 {
michael@0 29 }
michael@0 30
michael@0 31 // NOTE: the ideal scenario would be to have the default constructor public
michael@0 32 // setting mStates to 0 and this constructor (without = 0) private.
michael@0 33 // In that case, we could be sure that only macros at the end were creating
michael@0 34 // EventStates instances with mStates set to something else than 0.
michael@0 35 // Unfortunately, this constructor is needed at at least two places now.
michael@0 36 explicit MOZ_CONSTEXPR EventStates(InternalType aStates)
michael@0 37 : mStates(aStates)
michael@0 38 {
michael@0 39 }
michael@0 40
michael@0 41 MOZ_CONSTEXPR EventStates(const EventStates& aEventStates)
michael@0 42 : mStates(aEventStates.mStates)
michael@0 43 {
michael@0 44 }
michael@0 45
michael@0 46 EventStates& operator=(const EventStates& aEventStates)
michael@0 47 {
michael@0 48 mStates = aEventStates.mStates;
michael@0 49 return *this;
michael@0 50 }
michael@0 51
michael@0 52 EventStates MOZ_CONSTEXPR operator|(const EventStates& aEventStates) const
michael@0 53 {
michael@0 54 return EventStates(mStates | aEventStates.mStates);
michael@0 55 }
michael@0 56
michael@0 57 EventStates& operator|=(const EventStates& aEventStates)
michael@0 58 {
michael@0 59 mStates |= aEventStates.mStates;
michael@0 60 return *this;
michael@0 61 }
michael@0 62
michael@0 63 // NOTE: calling if (eventStates1 & eventStates2) will not build.
michael@0 64 // This might work correctly if operator bool() is defined
michael@0 65 // but using HasState, HasAllStates or HasAtLeastOneOfStates is recommended.
michael@0 66 EventStates MOZ_CONSTEXPR operator&(const EventStates& aEventStates) const
michael@0 67 {
michael@0 68 return EventStates(mStates & aEventStates.mStates);
michael@0 69 }
michael@0 70
michael@0 71 EventStates& operator&=(const EventStates& aEventStates)
michael@0 72 {
michael@0 73 mStates &= aEventStates.mStates;
michael@0 74 return *this;
michael@0 75 }
michael@0 76
michael@0 77 bool operator==(const EventStates& aEventStates) const
michael@0 78 {
michael@0 79 return mStates == aEventStates.mStates;
michael@0 80 }
michael@0 81
michael@0 82 bool operator!=(const EventStates& aEventStates) const
michael@0 83 {
michael@0 84 return mStates != aEventStates.mStates;
michael@0 85 }
michael@0 86
michael@0 87 EventStates operator~() const
michael@0 88 {
michael@0 89 return EventStates(~mStates);
michael@0 90 }
michael@0 91
michael@0 92 EventStates operator^(const EventStates& aEventStates) const
michael@0 93 {
michael@0 94 return EventStates(mStates ^ aEventStates.mStates);
michael@0 95 }
michael@0 96
michael@0 97 EventStates& operator^=(const EventStates& aEventStates)
michael@0 98 {
michael@0 99 mStates ^= aEventStates.mStates;
michael@0 100 return *this;
michael@0 101 }
michael@0 102
michael@0 103 /**
michael@0 104 * Returns true if the EventStates instance is empty.
michael@0 105 * A EventStates instance is empty if it contains no state.
michael@0 106 *
michael@0 107 * @return Whether if the object is empty.
michael@0 108 */
michael@0 109 bool IsEmpty() const
michael@0 110 {
michael@0 111 return mStates == 0;
michael@0 112 }
michael@0 113
michael@0 114 /**
michael@0 115 * Returns true if the EventStates instance contains the state
michael@0 116 * contained in aEventStates.
michael@0 117 * @note aEventStates should contain only one state.
michael@0 118 *
michael@0 119 * @param aEventStates The state to check.
michael@0 120 *
michael@0 121 * @return Whether the object has the state from aEventStates
michael@0 122 */
michael@0 123 bool HasState(EventStates aEventStates) const
michael@0 124 {
michael@0 125 #ifdef DEBUG
michael@0 126 // If aEventStates.mStates is a power of two, it contains only one state
michael@0 127 // (or none, but we don't really care).
michael@0 128 if ((aEventStates.mStates & (aEventStates.mStates - 1))) {
michael@0 129 NS_ERROR("When calling HasState, "
michael@0 130 "EventStates object has to contain only one state!");
michael@0 131 }
michael@0 132 #endif // DEBUG
michael@0 133 return mStates & aEventStates.mStates;
michael@0 134 }
michael@0 135
michael@0 136 /**
michael@0 137 * Returns true if the EventStates instance contains one of the states
michael@0 138 * contained in aEventStates.
michael@0 139 *
michael@0 140 * @param aEventStates The states to check.
michael@0 141 *
michael@0 142 * @return Whether the object has at least one state from aEventStates
michael@0 143 */
michael@0 144 bool HasAtLeastOneOfStates(EventStates aEventStates) const
michael@0 145 {
michael@0 146 return mStates & aEventStates.mStates;
michael@0 147 }
michael@0 148
michael@0 149 /**
michael@0 150 * Returns true if the EventStates instance contains all states
michael@0 151 * contained in aEventStates.
michael@0 152 *
michael@0 153 * @param aEventStates The states to check.
michael@0 154 *
michael@0 155 * @return Whether the object has all states from aEventStates
michael@0 156 */
michael@0 157 bool HasAllStates(EventStates aEventStates) const
michael@0 158 {
michael@0 159 return (mStates & aEventStates.mStates) == aEventStates.mStates;
michael@0 160 }
michael@0 161
michael@0 162 // We only need that method for inDOMUtils::GetContentState.
michael@0 163 // If inDOMUtils::GetContentState is removed, this method should be removed.
michael@0 164 InternalType GetInternalValue() const {
michael@0 165 return mStates;
michael@0 166 }
michael@0 167
michael@0 168 private:
michael@0 169 InternalType mStates;
michael@0 170 };
michael@0 171
michael@0 172 } // namespace mozilla
michael@0 173
michael@0 174 /**
michael@0 175 * The following macros are creating EventStates instance with different
michael@0 176 * values depending of their meaning.
michael@0 177 * Ideally, EventStates instance with values different than 0 should only be
michael@0 178 * created that way.
michael@0 179 */
michael@0 180
michael@0 181 // Helper to define a new EventStates macro.
michael@0 182 #define NS_DEFINE_EVENT_STATE_MACRO(_val) \
michael@0 183 (mozilla::EventStates(mozilla::EventStates::InternalType(1) << _val))
michael@0 184
michael@0 185 // Mouse is down on content.
michael@0 186 #define NS_EVENT_STATE_ACTIVE NS_DEFINE_EVENT_STATE_MACRO(0)
michael@0 187 // Content has focus.
michael@0 188 #define NS_EVENT_STATE_FOCUS NS_DEFINE_EVENT_STATE_MACRO(1)
michael@0 189 // Mouse is hovering over content.
michael@0 190 #define NS_EVENT_STATE_HOVER NS_DEFINE_EVENT_STATE_MACRO(2)
michael@0 191 // Drag is hovering over content.
michael@0 192 #define NS_EVENT_STATE_DRAGOVER NS_DEFINE_EVENT_STATE_MACRO(3)
michael@0 193 // Content is URL's target (ref).
michael@0 194 #define NS_EVENT_STATE_URLTARGET NS_DEFINE_EVENT_STATE_MACRO(4)
michael@0 195 // Content is checked.
michael@0 196 #define NS_EVENT_STATE_CHECKED NS_DEFINE_EVENT_STATE_MACRO(5)
michael@0 197 // Content is enabled (and can be disabled).
michael@0 198 #define NS_EVENT_STATE_ENABLED NS_DEFINE_EVENT_STATE_MACRO(6)
michael@0 199 // Content is disabled.
michael@0 200 #define NS_EVENT_STATE_DISABLED NS_DEFINE_EVENT_STATE_MACRO(7)
michael@0 201 // Content is required.
michael@0 202 #define NS_EVENT_STATE_REQUIRED NS_DEFINE_EVENT_STATE_MACRO(8)
michael@0 203 // Content is optional (and can be required).
michael@0 204 #define NS_EVENT_STATE_OPTIONAL NS_DEFINE_EVENT_STATE_MACRO(9)
michael@0 205 // Link has been visited.
michael@0 206 #define NS_EVENT_STATE_VISITED NS_DEFINE_EVENT_STATE_MACRO(10)
michael@0 207 // Link hasn't been visited.
michael@0 208 #define NS_EVENT_STATE_UNVISITED NS_DEFINE_EVENT_STATE_MACRO(11)
michael@0 209 // Content is valid (and can be invalid).
michael@0 210 #define NS_EVENT_STATE_VALID NS_DEFINE_EVENT_STATE_MACRO(12)
michael@0 211 // Content is invalid.
michael@0 212 #define NS_EVENT_STATE_INVALID NS_DEFINE_EVENT_STATE_MACRO(13)
michael@0 213 // Content value is in-range (and can be out-of-range).
michael@0 214 #define NS_EVENT_STATE_INRANGE NS_DEFINE_EVENT_STATE_MACRO(14)
michael@0 215 // Content value is out-of-range.
michael@0 216 #define NS_EVENT_STATE_OUTOFRANGE NS_DEFINE_EVENT_STATE_MACRO(15)
michael@0 217 // These two are temporary (see bug 302188)
michael@0 218 // Content is read-only.
michael@0 219 #define NS_EVENT_STATE_MOZ_READONLY NS_DEFINE_EVENT_STATE_MACRO(16)
michael@0 220 // Content is editable.
michael@0 221 #define NS_EVENT_STATE_MOZ_READWRITE NS_DEFINE_EVENT_STATE_MACRO(17)
michael@0 222 // Content is the default one (meaning depends of the context).
michael@0 223 #define NS_EVENT_STATE_DEFAULT NS_DEFINE_EVENT_STATE_MACRO(18)
michael@0 224 // Content could not be rendered (image/object/etc).
michael@0 225 #define NS_EVENT_STATE_BROKEN NS_DEFINE_EVENT_STATE_MACRO(19)
michael@0 226 // Content disabled by the user (images turned off, say).
michael@0 227 #define NS_EVENT_STATE_USERDISABLED NS_DEFINE_EVENT_STATE_MACRO(20)
michael@0 228 // Content suppressed by the user (ad blocking, etc).
michael@0 229 #define NS_EVENT_STATE_SUPPRESSED NS_DEFINE_EVENT_STATE_MACRO(21)
michael@0 230 // Content is still loading such that there is nothing to show the
michael@0 231 // user (eg an image which hasn't started coming in yet).
michael@0 232 #define NS_EVENT_STATE_LOADING NS_DEFINE_EVENT_STATE_MACRO(22)
michael@0 233 // Content is of a type that gecko can't handle.
michael@0 234 #define NS_EVENT_STATE_TYPE_UNSUPPORTED NS_DEFINE_EVENT_STATE_MACRO(23)
michael@0 235 #define NS_EVENT_STATE_INCREMENT_SCRIPT_LEVEL NS_DEFINE_EVENT_STATE_MACRO(24)
michael@0 236 // Handler for the content has been blocked.
michael@0 237 #define NS_EVENT_STATE_HANDLER_BLOCKED NS_DEFINE_EVENT_STATE_MACRO(25)
michael@0 238 // Handler for the content has been disabled.
michael@0 239 #define NS_EVENT_STATE_HANDLER_DISABLED NS_DEFINE_EVENT_STATE_MACRO(26)
michael@0 240 // Content is in the indeterminate state.
michael@0 241 #define NS_EVENT_STATE_INDETERMINATE NS_DEFINE_EVENT_STATE_MACRO(27)
michael@0 242 // Handler for the content has crashed
michael@0 243 #define NS_EVENT_STATE_HANDLER_CRASHED NS_DEFINE_EVENT_STATE_MACRO(28)
michael@0 244 // Content has focus and should show a ring.
michael@0 245 #define NS_EVENT_STATE_FOCUSRING NS_DEFINE_EVENT_STATE_MACRO(29)
michael@0 246 // Content is a submit control and the form isn't valid.
michael@0 247 #define NS_EVENT_STATE_MOZ_SUBMITINVALID NS_DEFINE_EVENT_STATE_MACRO(30)
michael@0 248 // UI friendly version of :invalid pseudo-class.
michael@0 249 #define NS_EVENT_STATE_MOZ_UI_INVALID NS_DEFINE_EVENT_STATE_MACRO(31)
michael@0 250 // UI friendly version of :valid pseudo-class.
michael@0 251 #define NS_EVENT_STATE_MOZ_UI_VALID NS_DEFINE_EVENT_STATE_MACRO(32)
michael@0 252 // Content is the full screen element, or a frame containing the
michael@0 253 // current full-screen element.
michael@0 254 #define NS_EVENT_STATE_FULL_SCREEN NS_DEFINE_EVENT_STATE_MACRO(33)
michael@0 255 // Content is an ancestor of the DOM full-screen element.
michael@0 256 #define NS_EVENT_STATE_FULL_SCREEN_ANCESTOR NS_DEFINE_EVENT_STATE_MACRO(34)
michael@0 257 // Handler for click to play plugin
michael@0 258 #define NS_EVENT_STATE_TYPE_CLICK_TO_PLAY NS_DEFINE_EVENT_STATE_MACRO(35)
michael@0 259 // Content is in the optimum region.
michael@0 260 #define NS_EVENT_STATE_OPTIMUM NS_DEFINE_EVENT_STATE_MACRO(36)
michael@0 261 // Content is in the suboptimal region.
michael@0 262 #define NS_EVENT_STATE_SUB_OPTIMUM NS_DEFINE_EVENT_STATE_MACRO(37)
michael@0 263 // Content is in the sub-suboptimal region.
michael@0 264 #define NS_EVENT_STATE_SUB_SUB_OPTIMUM NS_DEFINE_EVENT_STATE_MACRO(38)
michael@0 265 // Handler for click to play plugin (vulnerable w/update)
michael@0 266 #define NS_EVENT_STATE_VULNERABLE_UPDATABLE NS_DEFINE_EVENT_STATE_MACRO(39)
michael@0 267 // Handler for click to play plugin (vulnerable w/no update)
michael@0 268 #define NS_EVENT_STATE_VULNERABLE_NO_UPDATE NS_DEFINE_EVENT_STATE_MACRO(40)
michael@0 269 // Platform does not support plugin content (some mobile platforms)
michael@0 270 #define NS_EVENT_STATE_TYPE_UNSUPPORTED_PLATFORM NS_DEFINE_EVENT_STATE_MACRO(41)
michael@0 271 // Element is ltr (for :dir pseudo-class)
michael@0 272 #define NS_EVENT_STATE_LTR NS_DEFINE_EVENT_STATE_MACRO(42)
michael@0 273 // Element is rtl (for :dir pseudo-class)
michael@0 274 #define NS_EVENT_STATE_RTL NS_DEFINE_EVENT_STATE_MACRO(43)
michael@0 275 // Handler for play preview plugin
michael@0 276 #define NS_EVENT_STATE_TYPE_PLAY_PREVIEW NS_DEFINE_EVENT_STATE_MACRO(44)
michael@0 277 // Element is highlighted (devtools inspector)
michael@0 278 #define NS_EVENT_STATE_DEVTOOLS_HIGHLIGHTED NS_DEFINE_EVENT_STATE_MACRO(45)
michael@0 279
michael@0 280 // Event state that is used for values that need to be parsed but do nothing.
michael@0 281 #define NS_EVENT_STATE_IGNORE NS_DEFINE_EVENT_STATE_MACRO(63)
michael@0 282
michael@0 283 /**
michael@0 284 * NOTE: do not go over 63 without updating EventStates::InternalType!
michael@0 285 */
michael@0 286
michael@0 287 #define DIRECTION_STATES (NS_EVENT_STATE_LTR | NS_EVENT_STATE_RTL)
michael@0 288
michael@0 289 #define ESM_MANAGED_STATES (NS_EVENT_STATE_ACTIVE | NS_EVENT_STATE_FOCUS | \
michael@0 290 NS_EVENT_STATE_HOVER | NS_EVENT_STATE_DRAGOVER | \
michael@0 291 NS_EVENT_STATE_URLTARGET | NS_EVENT_STATE_FOCUSRING | \
michael@0 292 NS_EVENT_STATE_FULL_SCREEN | NS_EVENT_STATE_FULL_SCREEN_ANCESTOR)
michael@0 293
michael@0 294 #define INTRINSIC_STATES (~ESM_MANAGED_STATES)
michael@0 295
michael@0 296 #endif // mozilla_EventStates_h_
michael@0 297

mercurial