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.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2004-01-13 12:28:35 +00:00
|
|
|
* \author Matthias Ettrich
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
|
|
|
* \author Jü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"
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "mathed/InsetMath.h"
|
2017-07-05 12:31:28 +00:00
|
|
|
#include "mathed/InsetMathMacro.h"
|
2004-01-14 18:17:02 +00:00
|
|
|
|
2013-04-25 21:27:10 +00:00
|
|
|
#include "support/ExceptionMessage.h"
|
|
|
|
#include "support/gettext.h"
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2003-12-15 11:36:19 +00:00
|
|
|
|
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()
|
2020-02-28 06:55:05 +00:00
|
|
|
: inset_(nullptr), 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
|
|
|
{
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(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
|
|
|
{
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(inset_);
|
2012-04-12 14:25:38 +00:00
|
|
|
InsetMath const * math = inset_->asInsetMath();
|
|
|
|
bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs();
|
2017-07-03 17:53:14 +00:00
|
|
|
return math ? (paramless_macro ? 0 : cell().size())
|
2012-04-12 14:25:38 +00:00
|
|
|
: (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
|
|
|
{
|
2015-03-18 21:06:59 +00:00
|
|
|
LASSERT(inset_, return 0);
|
|
|
|
return inset_->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
|
|
|
{
|
2015-03-18 21:06:59 +00:00
|
|
|
LASSERT(inset_, return 0);
|
|
|
|
return inset_->col(idx_);
|
2003-12-15 11:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-29 12:47:23 +00:00
|
|
|
void CursorSlice::setPitPos(pit_type pit, pos_type pos)
|
|
|
|
{
|
|
|
|
LASSERT(pit != int(text()->paragraphs().size()), return);
|
|
|
|
pit_ = pit;
|
|
|
|
pos_ = pos;
|
|
|
|
|
|
|
|
// Now some strict checking. None of these should happen, but
|
|
|
|
// we're scaredy-cats
|
|
|
|
if (pos < 0) {
|
|
|
|
LYXERR0("Don't like -1!");
|
|
|
|
LATTEST(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos > paragraph().size()) {
|
|
|
|
LYXERR0("Don't like 1, pos: " << pos
|
|
|
|
<< " size: " << paragraph().size()
|
|
|
|
<< " par: " << pit);
|
|
|
|
LATTEST(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(idx_ < nargs(), return);
|
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()
|
|
|
|
{
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(idx_ < nargs(), return);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-25 21:27:10 +00:00
|
|
|
LATTEST(false);
|
2007-08-13 13:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-19 18:43:17 +00:00
|
|
|
bool CursorSlice::at_cell_end() const
|
2007-08-13 13:36:19 +00:00
|
|
|
{
|
2014-10-19 18:43:17 +00:00
|
|
|
return pit_ == lastpit() && pos_ == lastpos();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CursorSlice::at_cell_begin() const
|
|
|
|
{
|
|
|
|
return pit_ == 0 && pos_ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CursorSlice::at_end() const
|
|
|
|
{
|
|
|
|
return idx_ == lastidx() && at_cell_end();
|
2007-08-13 13:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CursorSlice::at_begin() const
|
|
|
|
{
|
2014-10-19 18:43:17 +00:00
|
|
|
return idx_ == 0 && at_cell_begin();
|
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);
|
2013-04-25 21:27:10 +00:00
|
|
|
// It should be safe to continue, just registering the error.
|
|
|
|
LASSERT(false, return 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
|