]> git.lyx.org Git - lyx.git/blob - src/cursor_slice.C
Fix bug 886 and others not reported related with the document paper size.
[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 <boost/assert.hpp>
25
26 using std::endl;
27
28
29 CursorSlice::CursorSlice()
30         : inset_(0), idx_(0), pit_(0), pos_(0)
31 {}
32
33
34 CursorSlice::CursorSlice(InsetBase & p)
35         : inset_(&p), idx_(0), pit_(0), pos_(0)
36 {
37         BOOST_ASSERT(inset_);
38 }
39
40
41 size_t CursorSlice::nargs() const
42 {
43         BOOST_ASSERT(inset_);
44         return inset_->nargs();
45 }
46
47
48 size_t CursorSlice::nrows() const
49 {
50         BOOST_ASSERT(inset_);
51         return inset_->nrows();
52 }
53
54
55 size_t CursorSlice::ncols() const
56 {
57         BOOST_ASSERT(inset_);
58         return inset_->ncols();
59 }
60
61
62 CursorSlice::pos_type CursorSlice::lastpos() const
63 {
64         BOOST_ASSERT(inset_);
65         return inset_->asMathInset() ? cell().size() : paragraph().size();
66 }
67
68
69 CursorSlice::row_type CursorSlice::row() const
70 {
71         BOOST_ASSERT(asMathInset());
72         return asMathInset()->row(idx_);
73 }
74
75
76 CursorSlice::col_type CursorSlice::col() const
77 {
78         BOOST_ASSERT(asMathInset());
79         return asMathInset()->col(idx_);
80 }
81
82
83 MathInset * CursorSlice::asMathInset() const
84 {
85         BOOST_ASSERT(inset_);
86         return inset_->asMathInset();
87 }
88
89
90 MathArray & CursorSlice::cell() const
91 {
92         BOOST_ASSERT(asMathInset());
93         return asMathInset()->cell(idx_);
94 }
95
96
97 LyXText * CursorSlice::text()
98 {
99         BOOST_ASSERT(inset_);
100         return inset_->getText(idx_);
101 }
102
103 LyXText const * CursorSlice::text() const
104 {
105         BOOST_ASSERT(inset_);
106         return inset_->getText(idx_);
107 }
108
109
110 Paragraph & CursorSlice::paragraph()
111 {
112         // access to the main lyx text must be handled in the cursor
113         BOOST_ASSERT(text());
114         return text()->getPar(pit_);
115 }
116
117
118 Paragraph const & CursorSlice::paragraph() const
119 {
120         // access to the main lyx text must be handled in the cursor
121         BOOST_ASSERT(text());
122         return text()->getPar(pit_);
123 }
124
125
126 bool operator==(CursorSlice const & p, CursorSlice const & q)
127 {
128         return &p.inset() == &q.inset()
129                && p.idx() == q.idx()
130                && p.pit() == q.pit()
131                && p.pos() == q.pos();
132 }
133
134
135 bool operator!=(CursorSlice const & p, CursorSlice const & q)
136 {
137         return &p.inset() != &q.inset()
138                || p.idx() != q.idx()
139                || p.pit() != q.pit()
140                || p.pos() != q.pos();
141 }
142
143
144 bool operator<(CursorSlice const & p, CursorSlice const & q)
145 {
146         if (&p.inset() != &q.inset()) {
147                 lyxerr << "can't compare cursor and anchor in different insets\n"
148                        << "p: " << p << '\n' << "q: " << q << endl;
149                 BOOST_ASSERT(false);
150         }
151         if (p.idx() != q.idx())
152                 return p.idx() < q.idx();
153         if (p.pit() != q.pit())
154                 return p.pit() < q.pit();
155         return p.pos() < q.pos();
156 }
157
158
159 bool operator>(CursorSlice const & p, CursorSlice const & q)
160 {
161         return q < p;
162 }
163
164
165 bool operator<=(CursorSlice const & p, CursorSlice const & q)
166 {
167         return !(q < p);
168 }
169
170
171 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
172 {
173         return os
174            << "inset: " << &item.inset()
175 //         << " text: " << item.text()
176            << " idx: " << item.idx()
177            << " par: " << item.pit()
178            << " pos: " << item.pos()
179 //         << " x: " << item.inset().x()
180 //         << " y: " << item.inset().y()
181 ;
182 }