1 /*
2 * Copyright (C) 2014 Igalia S.L.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#pragma once
32
33#include "RenderSelectionInfo.h"
34
35#if ENABLE(SERVICE_CONTROLS)
36#include "SelectionRectGatherer.h"
37#endif
38
39namespace WebCore {
40
41struct OldSelectionData;
42
43class SelectionRangeData {
44public:
45 SelectionRangeData(RenderView&);
46
47 class Context {
48 public:
49 Context() = default;
50 Context(RenderObject* start, RenderObject* end, unsigned startOffset, unsigned endOffset)
51 : m_start(makeWeakPtr(start))
52 , m_end(makeWeakPtr(end))
53 , m_startPosition(startOffset)
54 , m_endPosition(endOffset)
55 {
56 }
57
58 RenderObject* start() const { return m_start.get(); }
59 RenderObject* end() const { return m_end.get(); }
60 Optional<unsigned> startPosition() const { return m_startPosition; }
61 Optional<unsigned> endPosition() const { return m_endPosition; }
62
63 bool operator==(const Context& other) const
64 {
65 return m_start == other.m_start && m_end == other.m_end && m_startPosition == other.m_startPosition && m_endPosition == other.m_endPosition;
66 }
67
68 private:
69 WeakPtr<RenderObject> m_start;
70 WeakPtr<RenderObject> m_end;
71 Optional<unsigned> m_startPosition;
72 Optional<unsigned> m_endPosition;
73 };
74
75 enum class RepaintMode { NewXOROld, NewMinusOld, Nothing };
76 void set(const Context&, RepaintMode = RepaintMode::NewXOROld);
77 const Context& get() const { return m_selectionContext; }
78
79 RenderObject* start() const { return m_selectionContext.start(); }
80 RenderObject* end() const { return m_selectionContext.end(); }
81
82 unsigned startPosition() const { ASSERT(m_selectionContext.startPosition()); return m_selectionContext.startPosition().valueOr(0); }
83 unsigned endPosition() const { ASSERT(m_selectionContext.endPosition()); return m_selectionContext.endPosition().valueOr(0); }
84
85 void clear();
86 IntRect bounds() const { return collectBounds(ClipToVisibleContent::No); }
87 IntRect boundsClippedToVisibleContent() const { return collectBounds(ClipToVisibleContent::Yes); }
88 void repaint() const;
89
90private:
91 enum class ClipToVisibleContent { Yes, No };
92 IntRect collectBounds(ClipToVisibleContent) const;
93 void apply(const Context&, RepaintMode);
94
95 const RenderView& m_renderView;
96#if ENABLE(SERVICE_CONTROLS)
97 SelectionRectGatherer m_selectionRectGatherer;
98#endif
99 Context m_selectionContext;
100 bool m_selectionWasCaret { false };
101};
102
103} // namespace WebCore
104