]> git.lyx.org Git - lyx.git/blob - src/CursorSlice.cpp
1b4b8ca862402c9df4f52c4db30cc402ac4e7197
[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 }
46
47
48 CursorSlice::CursorSlice(CursorSlice const & cs)
49 {
50         operator=(cs);
51 }
52
53
54 CursorSlice & CursorSlice::operator=(CursorSlice const & cs)
55 {
56         inset_ = cs.inset_;
57         idx_ = cs.idx_;
58         pit_ = cs.pit_;
59         pos_ = cs.pos_;
60         return *this;
61 }
62
63
64 void CursorSlice::invalidate()
65 {
66         inset_ = 0;
67 }
68
69
70 bool CursorSlice::isValid() const
71 {
72         return inset_ != 0;
73 }
74
75
76 MathData & CursorSlice::cell() const
77 {
78         return inset_->asInsetMath()->cell(idx_);
79 }
80
81
82 Paragraph & CursorSlice::paragraph()
83 {
84         return text()->getPar(pit_);
85 }
86
87
88 Paragraph const & CursorSlice::paragraph() const
89 {
90         return text()->getPar(pit_);
91 }
92
93
94 pos_type CursorSlice::lastpos() const
95 {
96         BOOST_ASSERT(inset_);
97         return inset_->asInsetMath() ? cell().size() : paragraph().size();
98 }
99
100
101 pit_type CursorSlice::lastpit() const
102 {
103         if (inset().inMathed())
104                 return 0;
105         return text()->paragraphs().size() - 1;
106 }
107
108
109 CursorSlice::row_type CursorSlice::row() const
110 {
111         BOOST_ASSERT(asInsetMath());
112         return asInsetMath()->row(idx_);
113 }
114
115
116 CursorSlice::col_type CursorSlice::col() const
117 {
118         BOOST_ASSERT(asInsetMath());
119         return asInsetMath()->col(idx_);
120 }
121
122
123 bool operator==(CursorSlice const & p, CursorSlice const & q)
124 {
125         return &p.inset() == &q.inset()
126                && p.idx() == q.idx()
127                && p.pit() == q.pit()
128                && p.pos() == q.pos();
129 }
130
131
132 bool operator!=(CursorSlice const & p, CursorSlice const & q)
133 {
134         return &p.inset() != &q.inset()
135                || p.idx() != q.idx()
136                || p.pit() != q.pit()
137                || p.pos() != q.pos();
138 }
139
140
141 bool operator<(CursorSlice const & p, CursorSlice const & q)
142 {
143         if (&p.inset() != &q.inset()) {
144                 lyxerr << "can't compare cursor and anchor in different insets\n"
145                        << "p: " << p << '\n' << "q: " << q << endl;
146                 BOOST_ASSERT(false);
147         }
148         if (p.idx() != q.idx())
149                 return p.idx() < q.idx();
150         if (p.pit() != q.pit())
151                 return p.pit() < q.pit();
152         return p.pos() < q.pos();
153 }
154
155
156 bool operator>(CursorSlice const & p, CursorSlice const & q)
157 {
158         return q < p;
159 }
160
161
162 bool operator<=(CursorSlice const & p, CursorSlice const & q)
163 {
164         return !(q < p);
165 }
166
167
168 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
169 {
170         return os
171            << "inset: " << &item.inset()
172 //         << " text: " << item.text()
173            << " idx: " << item.idx()
174            << " par: " << item.pit()
175            << " pos: " << item.pos()
176 //         << " x: " << item.inset().x()
177 //         << " y: " << item.inset().y()
178 ;
179 }
180
181
182 } // namespace lyx