]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
some tabular fixes for the problems reported by Helge
[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), pit_(0), pos_(0), boundary_(false)
34 {}
35
36
37 CursorSlice::CursorSlice(InsetBase & p)
38         : inset_(&p), idx_(0), pit_(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::pos_type CursorSlice::lastpos() const
66 {
67         BOOST_ASSERT(inset_);
68         return inset_->asMathInset() ? cell().size() : paragraph().size();
69 }
70
71
72 CursorSlice::row_type CursorSlice::row() const
73 {
74         BOOST_ASSERT(asMathInset());
75         return asMathInset()->row(idx_);
76 }
77
78
79 CursorSlice::col_type CursorSlice::col() const
80 {
81         BOOST_ASSERT(asMathInset());
82         return asMathInset()->col(idx_);
83 }
84
85
86 MathInset * CursorSlice::asMathInset() const
87 {
88         BOOST_ASSERT(inset_);
89         return inset_->asMathInset();
90 }
91
92
93 UpdatableInset * CursorSlice::asUpdatableInset() const
94 {
95         BOOST_ASSERT(inset_);
96         return inset_->asUpdatableInset();
97 }
98
99
100 MathArray & CursorSlice::cell() const
101 {
102         BOOST_ASSERT(asMathInset());
103         return asMathInset()->cell(idx_);
104 }
105
106
107 LyXText * CursorSlice::text()
108 {
109         BOOST_ASSERT(inset_);
110         return inset_->getText(idx_);
111 }
112
113 LyXText const * CursorSlice::text() const
114 {
115         BOOST_ASSERT(inset_);
116         return inset_->getText(idx_);
117 }
118
119
120 Paragraph & CursorSlice::paragraph()
121 {
122         // access to the main lyx text must be handled in the cursor
123         BOOST_ASSERT(text());
124         return text()->getPar(pit_);
125 }
126
127
128 Paragraph const & CursorSlice::paragraph() const
129 {
130         // access to the main lyx text must be handled in the cursor
131         BOOST_ASSERT(text());
132         return text()->getPar(pit_);
133 }
134
135
136 bool operator==(CursorSlice const & p, CursorSlice const & q)
137 {
138         return &p.inset() == &q.inset()
139                && p.idx() == q.idx()
140                && p.pit() == q.pit()
141                && p.pos() == q.pos();
142 }
143
144
145 bool operator!=(CursorSlice const & p, CursorSlice const & q)
146 {
147         return &p.inset() != &q.inset()
148                || p.idx() != q.idx()
149                || p.pit() != q.pit()
150                || p.pos() != q.pos();
151 }
152
153
154 bool operator<(CursorSlice const & p, CursorSlice const & q)
155 {
156         if (&p.inset() != &q.inset()) {
157                 lyxerr << "can't compare cursor and anchor in different insets\n"
158                        << "p: " << p << '\n' << "q: " << q << endl;
159                 BOOST_ASSERT(false);
160         }
161         if (p.idx() != q.idx())
162                 return p.idx() < q.idx();
163         if (p.pit() != q.pit())
164                 return p.pit() < q.pit();
165         return p.pos() < q.pos();
166 }
167
168
169 bool operator>(CursorSlice const & p, CursorSlice const & q)
170 {
171         return q < p;
172 }
173
174
175 bool operator<=(CursorSlice const & p, CursorSlice const & q)
176 {
177         return !(q < p);
178 }
179
180
181 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
182 {
183         return os
184            << "inset: " << &item.inset()
185 //         << " text: " << item.text()
186            << " idx: " << item.idx()
187            << " par: " << item.pit()
188            << " pos: " << item.pos()
189 //         << " x: " << item.inset().x()
190 //         << " y: " << item.inset().y()
191 ;
192 }