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/. */
6 #ifndef mozilla_TypeInState_h
7 #define mozilla_TypeInState_h
9 #include "mozilla/EditorDOMPoint.h"
10 #include "mozilla/EventForwards.h"
11 #include "mozilla/UniquePtr.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsGkAtoms.h"
15 #include "nsISupportsImpl.h"
20 // Workaround for windows headers
40 PropItem() : tag(nullptr), attr(nullptr) { MOZ_COUNT_CTOR(PropItem
); }
41 PropItem(nsAtom
* aTag
, nsAtom
* aAttr
, const nsAString
& aValue
)
43 attr(aAttr
!= nsGkAtoms::_empty
? aAttr
: nullptr),
45 MOZ_COUNT_CTOR(PropItem
);
47 MOZ_COUNTED_DTOR(PropItem
)
50 class StyleCache final
{
52 StyleCache() = delete;
53 StyleCache(nsStaticAtom
* aTag
, nsStaticAtom
* aAttribute
,
54 const nsAString
& aValue
)
55 : mTag(aTag
), mAttribute(aAttribute
), mValue(aValue
) {
59 nsStaticAtom
* Tag() const { return mTag
; }
60 nsStaticAtom
* GetAttribute() const { return mAttribute
; }
61 const nsString
& Value() const { return mValue
; }
65 nsStaticAtom
* mAttribute
;
69 class MOZ_STACK_CLASS AutoStyleCacheArray final
70 : public AutoTArray
<StyleCache
, 19> {
72 index_type
IndexOf(const nsStaticAtom
* aTag
,
73 const nsStaticAtom
* aAttribute
) const {
74 for (index_type index
= 0; index
< Length(); ++index
) {
75 const StyleCache
& styleCache
= ElementAt(index
);
76 if (styleCache
.Tag() == aTag
&& styleCache
.GetAttribute() == aAttribute
) {
84 class TypeInState final
{
86 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TypeInState
)
87 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(TypeInState
)
92 nsresult
UpdateSelState(dom::Selection
* aSelection
);
95 * PreHandleMouseEvent() is called when `HTMLEditorEventListener` receives
96 * "mousedown" and "mouseup" events. Note that `aMouseDownOrUpEvent` may not
97 * be acceptable event for the `HTMLEditor`, but this is called even in
98 * the case because the event may cause a following `OnSelectionChange()`
101 void PreHandleMouseEvent(const dom::MouseEvent
& aMouseDownOrUpEvent
);
103 void PreHandleSelectionChangeCommand(Command aCommand
);
104 void PostHandleSelectionChangeCommand(const HTMLEditor
& aHTMLEditor
,
107 void OnSelectionChange(const HTMLEditor
& aHTMLEditor
, int16_t aReason
);
109 void SetProp(nsAtom
* aProp
, nsAtom
* aAttr
, const nsAString
& aValue
);
111 void ClearAllProps();
112 void ClearProp(nsAtom
* aProp
, nsAtom
* aAttr
);
115 * TakeClearProperty() hands back next property item on the clear list.
116 * Caller assumes ownership of PropItem and must delete it.
118 UniquePtr
<PropItem
> TakeClearProperty();
121 * TakeSetProperty() hands back next property item on the set list.
122 * Caller assumes ownership of PropItem and must delete it.
124 UniquePtr
<PropItem
> TakeSetProperty();
127 * TakeRelativeFontSize() hands back relative font value, which is then
130 int32_t TakeRelativeFontSize();
132 void GetTypingState(bool& isSet
, bool& theSetting
, nsAtom
* aProp
,
133 nsAtom
* aAttr
= nullptr, nsString
* outValue
= nullptr);
135 static bool FindPropInList(nsAtom
* aProp
, nsAtom
* aAttr
, nsAString
* outValue
,
136 const nsTArray
<PropItem
*>& aList
,
140 virtual ~TypeInState();
142 void RemovePropFromSetList(nsAtom
* aProp
, nsAtom
* aAttr
);
143 void RemovePropFromClearedList(nsAtom
* aProp
, nsAtom
* aAttr
);
144 bool IsPropSet(nsAtom
* aProp
, nsAtom
* aAttr
, nsAString
* outValue
);
145 bool IsPropSet(nsAtom
* aProp
, nsAtom
* aAttr
, nsAString
* outValue
,
147 bool IsPropCleared(nsAtom
* aProp
, nsAtom
* aAttr
);
148 bool IsPropCleared(nsAtom
* aProp
, nsAtom
* aAttr
, int32_t& outIndex
);
150 bool IsLinkStyleSet() const {
151 int32_t unusedIndex
= -1;
152 return FindPropInList(nsGkAtoms::a
, nullptr, nullptr, mSetArray
,
155 bool IsExplicitlyLinkStyleCleared() const {
156 int32_t unusedIndex
= -1;
157 return FindPropInList(nsGkAtoms::a
, nullptr, nullptr, mClearedArray
,
160 bool IsOnlyLinkStyleCleared() const {
161 return mClearedArray
.Length() == 1 && IsExplicitlyLinkStyleCleared();
163 bool AreAllStylesCleared() const {
164 int32_t unusedIndex
= -1;
165 return FindPropInList(nullptr, nullptr, nullptr, mClearedArray
,
168 bool AreSomeStylesSet() const { return !mSetArray
.IsEmpty(); }
169 bool AreSomeStylesCleared() const { return !mClearedArray
.IsEmpty(); }
171 nsTArray
<PropItem
*> mSetArray
;
172 nsTArray
<PropItem
*> mClearedArray
;
173 EditorDOMPoint mLastSelectionPoint
;
174 int32_t mRelativeFontSize
;
175 Command mLastSelectionCommand
;
176 bool mMouseDownFiredInLinkElement
;
177 bool mMouseUpFiredInLinkElement
;
180 } // namespace mozilla
182 #endif // #ifndef mozilla_TypeInState_h