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