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