]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
Sanitize cursor saving/restoring in a multi-view context.
[lyx.git] / src / CursorSlice.cpp
1 /**
2  * \file CursorSlice.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Matthias Ettrich
8  * \author André Pönitz
9  * \author Jürgen Vigna
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "CursorSlice.h"
17
18 #include "debug.h"
19 #include "Text.h"
20 #include "Paragraph.h"
21
22 #include "insets/Inset.h"
23
24 #include "mathed/InsetMath.h"
25 #include "mathed/MathData.h"
26
27 #include <boost/assert.hpp>
28 #include <boost/bind.hpp>
29
30
31 namespace lyx {
32
33 using std::endl;
34
35
36 CursorSlice::CursorSlice()
37         : inset_(0), idx_(0), pit_(0), pos_(0)
38 {}
39
40
41 CursorSlice::CursorSlice(Inset & p)
42         : inset_(&p), idx_(0), pit_(0), pos_(0)
43 {
44         BOOST_ASSERT(inset_);
45         inset_->destroyed.connect(
46                         boost::bind(&CursorSlice::invalidate, this));
47 }
48
49
50 CursorSlice::CursorSlice(CursorSlice const & cs)
51 {
52         operator=(cs);
53 }
54
55
56 CursorSlice & CursorSlice::operator=(CursorSlice const & cs)
57 {
58         inset_ = cs.inset_;
59         idx_ = cs.idx_;
60         pit_ = cs.pit_;
61         pos_ = cs.pos_;
62         if (inset_) {
63                 BOOST_ASSERT(inset_);
64                 inset_->destroyed.connect(
65                         boost::bind(&CursorSlice::invalidate, this));
66         }
67         return *this;
68 }
69
70
71 void CursorSlice::invalidate()
72 {
73         inset_ = 0;
74 }
75
76
77 bool CursorSlice::isValid() const
78 {
79         return inset_ != 0;
80 }
81
82
83 MathData & CursorSlice::cell() const
84 {
85         return inset_->asInsetMath()->cell(idx_);
86 }
87
88
89 Paragraph & CursorSlice::paragraph()
90 {
91         return text()->getPar(pit_);
92 }
93
94
95 Paragraph const & CursorSlice::paragraph() const
96 {
97         return text()->getPar(pit_);
98 }
99
100
101 pos_type CursorSlice::lastpos() const
102 {
103         BOOST_ASSERT(inset_);
104         return inset_->asInsetMath() ? cell().size() : paragraph().size();
105 }
106
107
108 CursorSlice::row_type CursorSlice::row() const
109 {
110         BOOST_ASSERT(asInsetMath());
111         return asInsetMath()->row(idx_);
112 }
113
114
115 CursorSlice::col_type CursorSlice::col() const
116 {
117         BOOST_ASSERT(asInsetMath());
118         return asInsetMath()->col(idx_);
119 }
120
121
122 bool operator==(CursorSlice const & p, CursorSlice const & q)
123 {
124         return &p.inset() == &q.inset()
125                && p.idx() == q.idx()
126                && p.pit() == q.pit()
127                && p.pos() == q.pos();
128 }
129
130
131 bool operator!=(CursorSlice const & p, CursorSlice const & q)
132 {
133         return &p.inset() != &q.inset()
134                || p.idx() != q.idx()
135                || p.pit() != q.pit()
136                || p.pos() != q.pos();
137 }
138
139
140 bool operator<(CursorSlice const & p, CursorSlice const & q)
141 {
142         if (&p.inset() != &q.inset()) {
143                 lyxerr << "can't compare cursor and anchor in different insets\n"
144                        << "p: " << p << '\n' << "q: " << q << endl;
145                 BOOST_ASSERT(false);
146         }
147         if (p.idx() != q.idx())
148                 return p.idx() < q.idx();
149         if (p.pit() != q.pit())
150                 return p.pit() < q.pit();
151         return p.pos() < q.pos();
152 }
153
154
155 bool operator>(CursorSlice const & p, CursorSlice const & q)
156 {
157         return q < p;
158 }
159
160
161 bool operator<=(CursorSlice const & p, CursorSlice const & q)
162 {
163         return !(q < p);
164 }
165
166
167 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
168 {
169         return os
170            << "inset: " << &item.inset()
171 //         << " text: " << item.text()
172            << " idx: " << item.idx()
173            << " par: " << item.pit()
174            << " pos: " << item.pos()
175 //         << " x: " << item.inset().x()
176 //         << " y: " << item.inset().y()
177 ;
178 }
179
180
181 } // namespace lyx