2 * \file CursorSlice.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
11 * Full author contact details are available in file CREDITS.
16 #include "CursorSlice.h"
20 #include "Paragraph.h"
22 #include "insets/Inset.h"
24 #include "mathed/InsetMath.h"
25 #include "mathed/MathData.h"
27 #include <boost/assert.hpp>
28 #include <boost/bind.hpp>
36 CursorSlice::CursorSlice()
37 : inset_(0), idx_(0), pit_(0), pos_(0)
41 CursorSlice::CursorSlice(Inset & p)
42 : inset_(&p), idx_(0), pit_(0), pos_(0)
45 boost::signal<void()> * destroyed_signal = inset_->destroyedSignal();
47 inset_connection_ = destroyed_signal->connect(
48 boost::bind(&CursorSlice::invalidate, this));
52 CursorSlice::CursorSlice(CursorSlice const & cs)
58 CursorSlice::~CursorSlice()
60 inset_connection_.disconnect();
64 CursorSlice & CursorSlice::operator=(CursorSlice const & cs)
70 if (inset_ && inset_->destroyedSignal()) {
71 inset_connection_ = inset_->destroyedSignal()->connect(
72 boost::bind(&CursorSlice::invalidate, this));
78 void CursorSlice::invalidate()
84 bool CursorSlice::isValid() const
90 MathData & CursorSlice::cell() const
92 return inset_->asInsetMath()->cell(idx_);
96 Paragraph & CursorSlice::paragraph()
98 return text()->getPar(pit_);
102 Paragraph const & CursorSlice::paragraph() const
104 return text()->getPar(pit_);
108 pos_type CursorSlice::lastpos() const
110 BOOST_ASSERT(inset_);
111 return inset_->asInsetMath() ? cell().size() : paragraph().size();
115 CursorSlice::row_type CursorSlice::row() const
117 BOOST_ASSERT(asInsetMath());
118 return asInsetMath()->row(idx_);
122 CursorSlice::col_type CursorSlice::col() const
124 BOOST_ASSERT(asInsetMath());
125 return asInsetMath()->col(idx_);
129 bool operator==(CursorSlice const & p, CursorSlice const & q)
131 return &p.inset() == &q.inset()
132 && p.idx() == q.idx()
133 && p.pit() == q.pit()
134 && p.pos() == q.pos();
138 bool operator!=(CursorSlice const & p, CursorSlice const & q)
140 return &p.inset() != &q.inset()
141 || p.idx() != q.idx()
142 || p.pit() != q.pit()
143 || p.pos() != q.pos();
147 bool operator<(CursorSlice const & p, CursorSlice const & q)
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;
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();
162 bool operator>(CursorSlice const & p, CursorSlice const & q)
168 bool operator<=(CursorSlice const & p, CursorSlice const & q)
174 std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
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()