KHtml

dom_selection.h
1 /*
2  * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef __dom_selection_h__
27 #define __dom_selection_h__
28 
29 #include "xml/dom_position.h"
30 
31 class KHTMLPart;
32 class KHTMLView;
33 class QPainter;
34 class QRect;
35 
36 namespace khtml
37 {
38 class RenderObject;
39 }
40 
41 namespace DOM
42 {
43 
44 class NodeImpl;
45 class Position;
46 class Range;
47 
48 class Selection
49 {
50 public:
51  enum EState { NONE, CARET, RANGE };
52  enum EAlter { MOVE, EXTEND };
53  enum EDirection { FORWARD, BACKWARD, RIGHT, LEFT };
54  enum ETextGranularity { CHARACTER, WORD, LINE, PARAGRAPH };
55 
56  // These match the AppKit values for these concepts.
57  // From NSTextView.h:
58  // NSSelectionAffinityUpstream = 0
59  // NSSelectionAffinityDownstream = 1
60  enum EAffinity { UPSTREAM = 0, DOWNSTREAM = 1 };
61 
62  Selection();
63  Selection(const Range &);
64  Selection(const Position &);
65  Selection(const Position &, const Position &);
66  Selection(const Selection &);
67 
68  EState state() const
69  {
70  return m_state;
71  }
72  EAffinity affinity() const
73  {
74  return m_affinity;
75  }
76  void setAffinity(EAffinity);
77 
78  void moveTo(const Range &);
79  void moveTo(const Selection &);
80  void moveTo(const Position &);
81  void moveTo(const Position &, const Position &);
82  bool modify(EAlter, EDirection, ETextGranularity);
83  bool expandUsingGranularity(ETextGranularity);
84  void clear();
85  void collapse();
86 
87  bool moveToRenderedContent();
88 
89  void setBase(const Position &pos);
90  void setExtent(const Position &pos);
91  void setBaseAndExtent(const Position &base, const Position &extent);
92  void setStart(const Position &pos);
93  void setEnd(const Position &pos);
94  void setStartAndEnd(const Position &start, const Position &end);
95 
96  Position base() const
97  {
98  return m_base;
99  }
100  Position extent() const
101  {
102  return m_extent;
103  }
104  Position start() const
105  {
106  return m_start;
107  }
108  Position end() const
109  {
110  return m_end;
111  }
112  Position caretPos() const
113  {
114  return m_baseIsStart ? m_end : m_start;
115  }
116  Position nonCaretPos() const
117  {
118  return m_baseIsStart ? m_start : m_end;
119  }
120 
121  QRect getRepaintRect() const;
122  void setNeedsLayout(bool flag = true);
123  void clearModifyBias()
124  {
125  m_modifyBiasSet = false;
126  }
127 
128  bool isEmpty() const
129  {
130  return state() == NONE;
131  }
132  bool notEmpty() const
133  {
134  return !isEmpty();
135  }
136  Range toRange() const;
137  bool isCollapsed() const
138  {
139  return m_state == CARET;
140  }
141 
142  void getRange(NodeImpl *&start, long &so, NodeImpl *&end, long &eo) const;
143 
144  void debugPosition() const;
145  void debugRenderer(khtml::RenderObject *r, bool selected) const;
146 
147  Selection &operator=(const Selection &o);
148  Selection &operator=(const Range &r)
149  {
150  moveTo(r);
151  return *this;
152  }
153  Selection &operator=(const Position &r)
154  {
155  moveTo(r);
156  return *this;
157  }
158 
159  friend bool operator==(const Selection &a, const Selection &b);
160  friend bool operator!=(const Selection &a, const Selection &b);
161 
162  friend class ::KHTMLPart;
163  friend class ::KHTMLView;
164 
165 private:
166  enum EPositionType { START, END, BASE, EXTENT, CARETPOS };
167 
168  void init();
169  void validate(ETextGranularity granularity = CHARACTER);
170  void assignBase(const Position &pos)
171  {
172  m_base = pos;
173  }
174  void assignExtent(const Position &pos)
175  {
176  m_extent = pos;
177  }
178  void assignBaseAndExtent(const Position &base, const Position &extent)
179  {
180  m_base = base;
181  m_extent = extent;
182  }
183  void assignStart(const Position &pos)
184  {
185  m_start = pos;
186  }
187  void assignEnd(const Position &pos)
188  {
189  m_end = pos;
190  }
191  void assignStartAndEnd(const Position &start, const Position &end)
192  {
193  m_start = start;
194  m_end = end;
195  }
196 
197  void layoutCaret();
198  void needsCaretRepaint();
199  void paintCaret(QPainter *p, const QRect &rect);
200 
201  bool nodeIsBeforeNode(NodeImpl *n1, NodeImpl *n2) const;
202  int xPosForVerticalArrowNavigation(EPositionType, bool recalc = false) const;
203 
204  Position m_base; // base position for the selection
205  Position m_extent; // extent position for the selection
206  Position m_start; // start position for the selection
207  Position m_end; // end position for the selection
208 
209  EState m_state; // the state of the selection
210  EAffinity m_affinity; // the upstream/downstream affinity of the selection
211 
212  int m_caretX; // caret coordinates and size
213  int m_caretY;
214  int m_caretSize;
215 
216  bool m_baseIsStart : 1; // true if base node is before the extent node
217  bool m_needsCaretLayout : 1; // true if the caret position needs to be calculated
218  bool m_modifyBiasSet : 1; // true if the selection has been horizontally
219  // modified with EAlter::EXTEND
220  bool m_visible : 1; // true if caret should be painted
221 };
222 
223 inline bool operator==(const Selection &a, const Selection &b)
224 {
225  return a.start() == b.start() && a.end() == b.end();
226 }
227 
228 inline bool operator!=(const Selection &a, const Selection &b)
229 {
230  return !(a == b);
231 }
232 
233 QDebug operator<<(QDebug stream, const Selection &selection);
234 
235 } // namespace DOM
236 
237 #endif // __dom_selection_h__
This file is part of the HTML rendering engine for KDE.
This class is khtml&#39;s main class.
Definition: khtml_part.h:208
Renders and displays HTML in a QScrollArea.
Definition: khtmlview.h:97
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
QCA_EXPORT void init()
This library provides a full-featured HTML parser and widget.
const QList< QKeySequence > & end()
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
Base Class for all rendering tree objects.
bool operator!=(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.