]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
Add support for the jurabib package (www.jurabib.org), a package for elegant BibTeX...
[lyx.git] / src / cursor_slice.C
1 /**
2  * \file cursor_slice.C
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 "cursor_slice.h"
17 #include "debug.h"
18 #include "lyxtext.h"
19 #include "paragraph.h"
20
21 #include "mathed/math_inset.h"
22 #include "mathed/math_data.h"
23
24 #include "insets/updatableinset.h"
25
26
27 #include <boost/assert.hpp>
28
29 using std::endl;
30
31
32 CursorSlice::CursorSlice()
33         : inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
34 {}
35
36
37 CursorSlice::CursorSlice(InsetBase * p)
38         : inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
39 {
40         ///BOOST_ASSERT(inset_);
41 }
42
43
44 size_t CursorSlice::nargs() const
45 {
46         BOOST_ASSERT(inset_);
47         return inset_->nargs();
48 }
49
50
51 size_t CursorSlice::nrows() const
52 {
53         BOOST_ASSERT(inset_);
54         return inset_->nrows();
55 }
56
57
58 size_t CursorSlice::ncols() const
59 {
60         BOOST_ASSERT(inset_);
61         return inset_->ncols();
62 }
63
64
65 CursorSlice::idx_type CursorSlice::idx() const
66 {
67         return idx_;
68 }
69
70
71 CursorSlice::idx_type & CursorSlice::idx()
72 {
73         return idx_;
74 }
75
76
77 CursorSlice::par_type CursorSlice::par() const
78 {
79         return par_;
80 }
81
82
83 CursorSlice::par_type & CursorSlice::par()
84 {
85         return par_;
86 }
87
88
89 CursorSlice::pos_type CursorSlice::pos() const
90 {
91         return pos_;
92 }
93
94
95 CursorSlice::pos_type & CursorSlice::pos()
96 {
97         return pos_;
98 }
99
100
101 CursorSlice::pos_type CursorSlice::lastpos() const
102 {
103         return (inset_ && inset_->asMathInset()) ? cell().size() : paragraph().size();
104 }
105
106
107 bool CursorSlice::boundary() const
108 {
109         return boundary_;
110 }
111
112
113 bool & CursorSlice::boundary()
114 {
115         return boundary_;
116 }
117
118
119 CursorSlice::row_type CursorSlice::row() const
120 {
121         BOOST_ASSERT(asMathInset());
122         return asMathInset()->row(idx_);
123 }
124
125
126 CursorSlice::col_type CursorSlice::col() const
127 {
128         BOOST_ASSERT(asMathInset());
129         return asMathInset()->col(idx_);
130 }
131
132
133 MathInset * CursorSlice::asMathInset() const
134 {
135         return inset_ ? inset_->asMathInset() : 0;
136 }
137
138
139 UpdatableInset * CursorSlice::asUpdatableInset() const
140 {
141         return inset_ ? inset_->asUpdatableInset() : 0;
142 }
143
144
145 MathArray & CursorSlice::cell() const
146 {
147         BOOST_ASSERT(asMathInset());
148         return asMathInset()->cell(idx_);
149 }
150
151
152 LyXText * CursorSlice::text() const
153 {
154         return inset_ ? inset_->getText(idx_) : 0;
155 }
156
157
158 Paragraph & CursorSlice::paragraph()
159 {
160         // access to the main lyx text must be handled in the cursor
161         BOOST_ASSERT(text());
162         return *text()->getPar(par_);
163 }
164
165
166 Paragraph const & CursorSlice::paragraph() const
167 {
168         // access to the main lyx text must be handled in the cursor
169         BOOST_ASSERT(text());
170         return *text()->getPar(par_);
171 }
172
173
174 bool operator==(CursorSlice const & p, CursorSlice const & q)
175 {
176         return p.inset_ == q.inset_
177                && p.idx_ == q.idx_
178                && p.par_ == q.par_
179                && p.pos_ == q.pos_;
180 }
181
182
183 bool operator!=(CursorSlice const & p, CursorSlice const & q)
184 {
185         return p.inset_ != q.inset_
186                || p.idx_ != q.idx_
187                || p.par_ != q.par_
188                || p.pos_ != q.pos_;
189 }
190
191
192 bool operator<(CursorSlice const & p, CursorSlice const & q)
193 {
194         if (p.inset_ != q.inset_) {
195                 lyxerr << "can't compare cursor and anchor in different insets\n"
196                        << "p: " << p << '\n' << "q: " << q << endl;
197                 return true;
198         }
199         if (p.idx_ != q.idx_)
200                 return p.idx_ < q.idx_;
201         if (p.par_ != q.par_)
202                 return p.par_ < q.par_;
203         return p.pos_ < q.pos_;
204 }
205
206
207 bool operator>(CursorSlice const & p, CursorSlice const & q)
208 {
209         return q < p;
210 }
211
212
213 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
214 {
215         os
216            << "inset: " << item.inset_
217 //         << " text: " << item.text()
218            << " idx: " << item.idx_
219            << " par: " << item.par_
220            << " pos: " << item.pos_
221 //         << " x: " << item.inset_->x()
222 //         << " y: " << item.inset_->y()
223 ;
224         return os;
225 }