2003-12-15 11:36:19 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file cursor_slice.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
2004-01-13 12:28:35 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Matthias Ettrich
|
2003-12-15 11:36:19 +00:00
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
2004-01-13 12:28:35 +00:00
|
|
|
|
* \author J<EFBFBD>rgen Vigna
|
2003-12-15 11:36:19 +00:00
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "cursor_slice.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
#include "mathed/math_inset.h"
|
2004-01-14 18:17:02 +00:00
|
|
|
|
#include "mathed/math_data.h"
|
|
|
|
|
|
2003-12-15 11:36:19 +00:00
|
|
|
|
#include "insets/updatableinset.h"
|
|
|
|
|
|
2004-01-14 18:17:02 +00:00
|
|
|
|
|
2003-12-15 11:36:19 +00:00
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
|
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorSlice::CursorSlice()
|
2004-01-13 12:28:35 +00:00
|
|
|
|
: inset_(0), idx_(0), par_(0), pos_(0), boundary_(false)
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorSlice::CursorSlice(InsetBase * p)
|
2004-01-13 12:28:35 +00:00
|
|
|
|
: inset_(p), idx_(0), par_(0), pos_(0), boundary_(false)
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{
|
|
|
|
|
///BOOST_ASSERT(inset_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-13 18:08:13 +00:00
|
|
|
|
void CursorSlice::idx(idx_type idx)
|
|
|
|
|
{
|
|
|
|
|
idx_ = idx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorSlice::idx_type CursorSlice::idx() const
|
|
|
|
|
{
|
|
|
|
|
return idx_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CursorSlice::par(par_type par)
|
2004-01-13 12:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
par_ = par;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-13 18:08:13 +00:00
|
|
|
|
CursorSlice::par_type CursorSlice::par() const
|
2004-01-13 12:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
return par_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-13 18:08:13 +00:00
|
|
|
|
void CursorSlice::pos(pos_type pos)
|
2004-01-13 12:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
pos_ = pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-13 18:08:13 +00:00
|
|
|
|
CursorSlice::pos_type CursorSlice::pos() const
|
2004-01-13 12:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
return pos_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CursorSlice::boundary(bool boundary)
|
|
|
|
|
{
|
|
|
|
|
boundary_ = boundary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CursorSlice::boundary() const
|
|
|
|
|
{
|
|
|
|
|
return boundary_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-12-15 11:36:19 +00:00
|
|
|
|
MathInset * CursorSlice::asMathInset() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<MathInset *>(const_cast<InsetBase *>(inset_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UpdatableInset * CursorSlice::asUpdatableInset() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<UpdatableInset *>(const_cast<InsetBase *>(inset_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MathArray & CursorSlice::cell(CursorSlice::idx_type idx) const
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(inset_);
|
2004-01-13 18:08:13 +00:00
|
|
|
|
BOOST_ASSERT(asMathInset());
|
2003-12-15 11:36:19 +00:00
|
|
|
|
return asMathInset()->cell(idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MathArray & CursorSlice::cell() const
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(inset_);
|
|
|
|
|
return asMathInset()->cell(idx_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CursorSlice::getPos(int & x, int & y) const
|
|
|
|
|
{
|
2004-01-13 18:08:13 +00:00
|
|
|
|
BOOST_ASSERT(inset_);
|
2003-12-15 11:36:19 +00:00
|
|
|
|
asMathInset()->getPos(idx_, pos_, x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CursorSlice::setPos(int pos)
|
|
|
|
|
{
|
|
|
|
|
pos_ = pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXText * CursorSlice::text() const
|
|
|
|
|
{
|
2004-01-13 15:25:52 +00:00
|
|
|
|
return asUpdatableInset() ? asUpdatableInset()->getText(idx_) : 0;
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
|
|
|
|
return p.inset_ == q.inset_
|
|
|
|
|
&& p.idx_ == q.idx_
|
|
|
|
|
&& p.par_ == q.par_
|
|
|
|
|
&& p.pos_ == q.pos_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
|
|
|
|
return p.inset_ != q.inset_
|
|
|
|
|
|| p.idx_ != q.idx_
|
|
|
|
|
|| p.par_ != q.par_
|
|
|
|
|
|| p.pos_ != q.pos_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
|
|
|
|
if (p.inset_ != q.inset_) {
|
|
|
|
|
lyxerr << "can't compare cursor and anchor in different insets\n"
|
|
|
|
|
<< "p: " << p << '\n' << "q: " << q << endl;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (p.idx_ != q.idx_)
|
|
|
|
|
return p.idx_ < q.idx_;
|
|
|
|
|
if (p.par_ != q.par_)
|
|
|
|
|
return p.par_ < q.par_;
|
|
|
|
|
return p.pos_ < q.pos_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-13 14:13:51 +00:00
|
|
|
|
bool operator>(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
|
|
|
|
return q < p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-12-15 11:36:19 +00:00
|
|
|
|
std::ostream & operator<<(std::ostream & os, CursorSlice const & item)
|
|
|
|
|
{
|
|
|
|
|
os << " inset: " << item.inset_
|
2004-01-13 15:25:52 +00:00
|
|
|
|
<< " text: " << item.text()
|
2003-12-15 11:36:19 +00:00
|
|
|
|
<< " idx: " << item.idx_
|
2004-01-13 15:25:52 +00:00
|
|
|
|
<< " par: " << item.par_
|
|
|
|
|
<< " pos: " << item.pos_
|
2003-12-15 11:36:19 +00:00
|
|
|
|
// << " x: " << item.inset_->x()
|
|
|
|
|
// << " y: " << item.inset_->y()
|
|
|
|
|
;
|
|
|
|
|
return os;
|
|
|
|
|
}
|
2004-01-14 18:17:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void increment(CursorBase & it)
|
|
|
|
|
{
|
|
|
|
|
CursorSlice & top = it.back();
|
|
|
|
|
MathArray & ar = top.asMathInset()->cell(top.idx_);
|
|
|
|
|
|
|
|
|
|
// move into the current inset if possible
|
|
|
|
|
// it is impossible for pos() == size()!
|
|
|
|
|
MathInset * n = 0;
|
|
|
|
|
if (top.pos_ != ar.size())
|
|
|
|
|
n = (ar.begin() + top.pos_)->nucleus();
|
|
|
|
|
if (n && n->isActive()) {
|
|
|
|
|
it.push_back(CursorSlice(n));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise move on one cell back if possible
|
|
|
|
|
if (top.pos_ < ar.size()) {
|
|
|
|
|
// pos() == size() is valid!
|
|
|
|
|
++top.pos_;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise try to move on one cell if possible
|
|
|
|
|
while (top.idx_ + 1 < top.asMathInset()->nargs()) {
|
|
|
|
|
// idx() == nargs() is _not_ valid!
|
|
|
|
|
++top.idx_;
|
|
|
|
|
if (top.asMathInset()->validCell(top.idx_)) {
|
|
|
|
|
top.pos_ = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise leave array, move on one back
|
|
|
|
|
// this might yield pos() == size(), but that's a ok.
|
|
|
|
|
it.pop_back();
|
|
|
|
|
// it certainly invalidates top
|
|
|
|
|
++it.back().pos_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorBase ibegin(InsetBase * p)
|
|
|
|
|
{
|
|
|
|
|
CursorBase it;
|
|
|
|
|
it.push_back(CursorSlice(p));
|
|
|
|
|
return it;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorBase iend(InsetBase * p)
|
|
|
|
|
{
|
|
|
|
|
CursorBase it;
|
|
|
|
|
it.push_back(CursorSlice(p));
|
|
|
|
|
CursorSlice & top = it.back();
|
|
|
|
|
top.idx_ = top.asMathInset()->nargs() - 1;
|
|
|
|
|
top.pos_ = top.asMathInset()->cell(top.idx_).size();
|
|
|
|
|
return it;
|
|
|
|
|
}
|