#ifndef VisibleSelection_h
#define VisibleSelection_h
#include "core/editing/SelectionType.h"
#include "core/editing/TextGranularity.h"
#include "core/editing/VisiblePosition.h"
namespace WebCore {
class LayoutPoint;
class Position;
const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM;
enum SelectionDirection { DirectionForward, DirectionBackward, DirectionRight, DirectionLeft };
class VisibleSelection {
public:
VisibleSelection();
VisibleSelection(const Position&, EAffinity, bool isDirectional = false);
VisibleSelection(const Position&, const Position&, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false);
explicit VisibleSelection(const Range*, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false);
explicit VisibleSelection(const VisiblePosition&, bool isDirectional = false);
VisibleSelection(const VisiblePosition&, const VisiblePosition&, bool isDirectional = false);
VisibleSelection(const VisibleSelection&);
VisibleSelection& operator=(const VisibleSelection&);
~VisibleSelection();
static VisibleSelection selectionFromContentsOfNode(Node*);
SelectionType selectionType() const { return m_selectionType; }
void setAffinity(EAffinity affinity) { m_affinity = affinity; }
EAffinity affinity() const { return m_affinity; }
void setBase(const Position&);
void setBase(const VisiblePosition&);
void setExtent(const Position&);
void setExtent(const VisiblePosition&);
Position base() const { return m_base; }
Position extent() const { return m_extent; }
Position start() const { return m_start; }
Position end() const { return m_end; }
VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); }
VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
VisiblePosition visibleBase() const { return VisiblePosition(m_base, isRange() ? (isBaseFirst() ? UPSTREAM : DOWNSTREAM) : affinity()); }
VisiblePosition visibleExtent() const { return VisiblePosition(m_extent, isRange() ? (isBaseFirst() ? DOWNSTREAM : UPSTREAM) : affinity()); }
bool isNone() const { return selectionType() == NoSelection; }
bool isCaret() const { return selectionType() == CaretSelection; }
bool isRange() const { return selectionType() == RangeSelection; }
bool isCaretOrRange() const { return selectionType() != NoSelection; }
bool isNonOrphanedRange() const { return isRange() && !start().isOrphan() && !end().isOrphan(); }
bool isNonOrphanedCaretOrRange() const { return isCaretOrRange() && !start().isOrphan() && !end().isOrphan(); }
bool isBaseFirst() const { return m_baseIsFirst; }
bool isDirectional() const { return m_isDirectional; }
void setIsDirectional(bool isDirectional) { m_isDirectional = isDirectional; }
void appendTrailingWhitespace();
bool expandUsingGranularity(TextGranularity granularity);
PassRefPtrWillBeRawPtr<Range> firstRange() const;
PassRefPtrWillBeRawPtr<Range> toNormalizedRange() const;
Element* rootEditableElement() const;
bool isContentEditable() const;
bool rendererIsEditable() const;
bool isContentRichlyEditable() const;
Node* nonBoundaryShadowTreeRootNode() const;
VisiblePosition visiblePositionRespectingEditingBoundary(const LayoutPoint& localPoint, Node* targetNode) const;
void setWithoutValidation(const Position&, const Position&);
class ChangeObserver {
WTF_MAKE_NONCOPYABLE(ChangeObserver);
public:
ChangeObserver();
virtual ~ChangeObserver();
virtual void didChangeVisibleSelection() = 0;
};
void setChangeObserver(ChangeObserver&);
void clearChangeObserver();
void didChange();
#ifndef NDEBUG
void debugPosition() const;
void formatForDebugger(char* buffer, unsigned length) const;
void showTreeForThis() const;
#endif
private:
void validate(TextGranularity = CharacterGranularity);
void setBaseAndExtentToDeepEquivalents();
void setStartAndEndFromBaseAndExtentRespectingGranularity(TextGranularity);
void adjustSelectionToAvoidCrossingShadowBoundaries();
void adjustSelectionToAvoidCrossingEditingBoundaries();
void updateSelectionType();
Position m_base;
Position m_extent;
Position m_start;
Position m_end;
EAffinity m_affinity;
ChangeObserver* m_changeObserver;
SelectionType m_selectionType;
bool m_baseIsFirst : 1;
bool m_isDirectional : 1;
};
inline bool operator==(const VisibleSelection& a, const VisibleSelection& b)
{
return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.isBaseFirst() == b.isBaseFirst()
&& a.isDirectional() == b.isDirectional();
}
inline bool operator!=(const VisibleSelection& a, const VisibleSelection& b)
{
return !(a == b);
}
}
#ifndef NDEBUG
void showTree(const WebCore::VisibleSelection&);
void showTree(const WebCore::VisibleSelection*);
#endif
#endif