dom/events/EventStates.h

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

mercurial