2003-12-15 11:36:19 +00:00
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
|
* \file CursorSlice.cpp
|
2003-12-15 11:36:19 +00:00
|
|
|
|
* 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>
|
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "CursorSlice.h"
|
2007-05-25 23:17:24 +00:00
|
|
|
|
|
2007-04-29 23:33:02 +00:00
|
|
|
|
#include "Text.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Paragraph.h"
|
2003-12-15 11:36:19 +00:00
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
|
#include "support/debug.h"
|
|
|
|
|
|
2007-05-25 23:17:24 +00:00
|
|
|
|
#include "insets/Inset.h"
|
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "mathed/InsetMath.h"
|
2007-04-26 16:06:39 +00:00
|
|
|
|
#include "mathed/MathData.h"
|
2004-01-14 18:17:02 +00:00
|
|
|
|
|
2003-12-15 11:36:19 +00:00
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
|
2007-11-28 22:12:03 +00:00
|
|
|
|
#include <ostream>
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
2007-07-17 17:46:54 +00:00
|
|
|
|
namespace lyx {
|
2003-12-15 11:36:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CursorSlice::CursorSlice()
|
2005-07-15 15:49:40 +00:00
|
|
|
|
: inset_(0), idx_(0), pit_(0), pos_(0)
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
CursorSlice::CursorSlice(Inset & p)
|
2005-07-15 15:49:40 +00:00
|
|
|
|
: inset_(&p), idx_(0), pit_(0), pos_(0)
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{
|
2004-03-18 16:41:45 +00:00
|
|
|
|
BOOST_ASSERT(inset_);
|
2007-05-25 23:17:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 16:06:39 +00:00
|
|
|
|
MathData & CursorSlice::cell() const
|
2004-01-16 10:55:19 +00:00
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
return inset_->asInsetMath()->cell(idx_);
|
2004-01-16 10:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-08-23 20:55:34 +00:00
|
|
|
|
Paragraph & CursorSlice::paragraph() const
|
2004-01-16 10:55:19 +00:00
|
|
|
|
{
|
2005-07-18 11:00:15 +00:00
|
|
|
|
return text()->getPar(pit_);
|
2004-01-16 10:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
pos_type CursorSlice::lastpos() const
|
2004-01-15 17:34:44 +00:00
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
|
BOOST_ASSERT(inset_);
|
2008-01-10 08:46:04 +00:00
|
|
|
|
return inset_->asInsetMath() ? cell().size()
|
|
|
|
|
: (text()->empty() ? 0 : paragraph().size());
|
2004-01-15 17:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-06-09 12:39:46 +00:00
|
|
|
|
pit_type CursorSlice::lastpit() const
|
|
|
|
|
{
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (inset_->inMathed())
|
2007-06-09 12:39:46 +00:00
|
|
|
|
return 0;
|
|
|
|
|
return text()->paragraphs().size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-15 17:34:44 +00:00
|
|
|
|
CursorSlice::row_type CursorSlice::row() const
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
BOOST_ASSERT(asInsetMath());
|
|
|
|
|
return asInsetMath()->row(idx_);
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-15 17:34:44 +00:00
|
|
|
|
CursorSlice::col_type CursorSlice::col() const
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
BOOST_ASSERT(asInsetMath());
|
|
|
|
|
return asInsetMath()->col(idx_);
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-08-13 13:36:19 +00:00
|
|
|
|
void CursorSlice::forwardPos()
|
|
|
|
|
{
|
|
|
|
|
// move on one position if possible
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (pos_ < lastpos()) {
|
2007-08-13 13:36:19 +00:00
|
|
|
|
//lyxerr << "... next pos" << endl;
|
2008-02-11 08:03:03 +00:00
|
|
|
|
++pos_;
|
2007-08-13 13:36:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise move on one paragraph if possible
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (pit_ < lastpit()) {
|
2007-08-13 13:36:19 +00:00
|
|
|
|
//lyxerr << "... next par" << endl;
|
2008-02-11 08:03:03 +00:00
|
|
|
|
++pit_;
|
|
|
|
|
pos_ = 0;
|
2007-08-13 13:36:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-23 21:00:41 +00:00
|
|
|
|
// otherwise move on one cell
|
|
|
|
|
//lyxerr << "... next idx" << endl;
|
2007-08-23 21:35:40 +00:00
|
|
|
|
|
2008-02-11 08:03:03 +00:00
|
|
|
|
BOOST_ASSERT(idx_ < nargs());
|
2007-08-23 21:35:40 +00:00
|
|
|
|
|
2008-02-11 08:03:03 +00:00
|
|
|
|
++idx_;
|
|
|
|
|
pit_ = 0;
|
|
|
|
|
pos_ = 0;
|
2007-08-23 21:35:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CursorSlice::forwardIdx()
|
|
|
|
|
{
|
2008-02-11 08:03:03 +00:00
|
|
|
|
BOOST_ASSERT(idx_ < nargs());
|
2007-08-23 21:35:40 +00:00
|
|
|
|
|
2008-02-11 08:03:03 +00:00
|
|
|
|
++idx_;
|
|
|
|
|
pit_ = 0;
|
|
|
|
|
pos_ = 0;
|
2007-08-13 13:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CursorSlice::backwardPos()
|
|
|
|
|
{
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (pos_ != 0) {
|
|
|
|
|
--pos_;
|
2007-08-13 13:36:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (pit_ != 0) {
|
|
|
|
|
--pit_;
|
|
|
|
|
pos_ = lastpos();
|
2007-08-13 13:36:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (idx_ != 0) {
|
|
|
|
|
--idx_;
|
|
|
|
|
pit_ = lastpit();
|
|
|
|
|
pos_ = lastpos();
|
2007-08-13 13:36:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_ASSERT(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CursorSlice::at_end() const
|
|
|
|
|
{
|
2008-02-11 08:03:03 +00:00
|
|
|
|
return idx_ == lastidx() && pit_ == lastpit() && pos_ == lastpos();
|
2007-08-13 13:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CursorSlice::at_begin() const
|
|
|
|
|
{
|
2008-02-11 08:03:03 +00:00
|
|
|
|
return idx_ == 0 && pit_ == 0 && pos_ == 0;
|
2007-08-13 13:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-12-15 11:36:19 +00:00
|
|
|
|
bool operator==(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
2008-02-11 12:26:06 +00:00
|
|
|
|
return p.inset_ == q.inset_
|
2008-02-11 08:03:03 +00:00
|
|
|
|
&& p.idx_ == q.idx_
|
|
|
|
|
&& p.pit_ == q.pit_
|
|
|
|
|
&& p.pos_ == q.pos_;
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
2008-02-11 12:26:06 +00:00
|
|
|
|
return p.inset_ != q.inset_
|
2008-02-11 08:03:03 +00:00
|
|
|
|
|| p.idx_ != q.idx_
|
|
|
|
|
|| p.pit_ != q.pit_
|
|
|
|
|
|| p.pos_ != q.pos_;
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
2008-02-11 12:26:06 +00:00
|
|
|
|
if (p.inset_ != q.inset_) {
|
2007-11-28 22:12:03 +00:00
|
|
|
|
LYXERR0("can't compare cursor and anchor in different insets\n"
|
|
|
|
|
<< "p: " << p << '\n' << "q: " << q);
|
2004-04-07 08:07:26 +00:00
|
|
|
|
BOOST_ASSERT(false);
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
2008-02-11 08:03:03 +00:00
|
|
|
|
if (p.idx_ != q.idx_)
|
|
|
|
|
return p.idx_ < q.idx_;
|
|
|
|
|
if (p.pit_ != q.pit_)
|
|
|
|
|
return p.pit_ < q.pit_;
|
|
|
|
|
return p.pos_ < q.pos_;
|
2003-12-15 11:36:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-13 14:13:51 +00:00
|
|
|
|
bool operator>(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
|
|
|
|
return q < p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-07 13:15:34 +00:00
|
|
|
|
bool operator<=(CursorSlice const & p, CursorSlice const & q)
|
|
|
|
|
{
|
|
|
|
|
return !(q < p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
|
ostream & operator<<(ostream & os, CursorSlice const & item)
|
2003-12-15 11:36:19 +00:00
|
|
|
|
{
|
2004-03-18 12:53:43 +00:00
|
|
|
|
return os
|
2008-02-11 12:26:06 +00:00
|
|
|
|
<< "inset: " << (void *)item.inset_
|
2004-02-16 11:58:51 +00:00
|
|
|
|
// << " text: " << item.text()
|
2008-02-11 08:03:03 +00:00
|
|
|
|
<< " idx: " << item.idx_
|
|
|
|
|
<< " par: " << item.pit_
|
|
|
|
|
<< " pos: " << item.pos_
|
2008-02-11 12:26:06 +00:00
|
|
|
|
// << " x: " << item.inset_->x()
|
|
|
|
|
// << " y: " << item.inset_->y()
|
2003-12-15 11:36:19 +00:00
|
|
|
|
;
|
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|