2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file InsetList.cpp
|
2003-08-23 00:17:00 +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
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Martin Vermeer
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2002-08-11 15:03:52 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "InsetList.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
2016-12-05 13:47:13 +00:00
|
|
|
#include "insets/Inset.h"
|
2002-08-11 15:03:52 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
|
|
|
|
2016-12-05 13:47:13 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2004-01-26 10:13:15 +00:00
|
|
|
|
2007-07-17 17:46:54 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2002-08-11 15:03:52 +00:00
|
|
|
namespace {
|
|
|
|
|
2004-01-26 10:13:15 +00:00
|
|
|
typedef InsetList::InsetTable Table;
|
|
|
|
|
2007-11-28 22:12:03 +00:00
|
|
|
struct InsetTablePosLess
|
|
|
|
{
|
2004-01-26 10:13:15 +00:00
|
|
|
bool operator()(Table const & t1, Table const & t2) const
|
2003-02-20 17:39:48 +00:00
|
|
|
{
|
2004-01-05 18:00:12 +00:00
|
|
|
return t1.pos < t2.pos;
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-05-29 01:13:18 +00:00
|
|
|
} // namespace anon
|
2002-08-11 15:03:52 +00:00
|
|
|
|
2004-01-26 10:13:15 +00:00
|
|
|
|
2016-06-06 19:55:39 +00:00
|
|
|
InsetList::InsetList(InsetList const & il) : list_(il.list_)
|
2008-01-12 16:40:25 +00:00
|
|
|
{
|
|
|
|
List::iterator it = list_.begin();
|
|
|
|
List::iterator end = list_.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
it->inset = it->inset->clone();
|
|
|
|
}
|
|
|
|
|
2004-01-26 10:13:15 +00:00
|
|
|
|
2008-08-07 08:50:41 +00:00
|
|
|
InsetList::InsetList(InsetList const & il, pos_type beg, pos_type end)
|
|
|
|
{
|
|
|
|
InsetList::const_iterator cit = il.begin();
|
|
|
|
InsetList::const_iterator cend = il.end();
|
|
|
|
for (; cit != cend; ++cit) {
|
|
|
|
if (cit->pos < beg)
|
|
|
|
continue;
|
|
|
|
if (cit->pos >= end)
|
|
|
|
break;
|
|
|
|
// Add a new entry in the insetlist_.
|
|
|
|
insert(cit->inset->clone(), cit->pos - beg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-01 22:59:25 +00:00
|
|
|
InsetList::~InsetList()
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::iterator it = list_.begin();
|
|
|
|
List::iterator end = list_.end();
|
2008-01-12 16:40:25 +00:00
|
|
|
for (; it != end; ++it)
|
2002-08-11 15:03:52 +00:00
|
|
|
delete it->inset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-23 12:49:22 +00:00
|
|
|
void InsetList::setBuffer(Buffer & b)
|
|
|
|
{
|
|
|
|
List::iterator it = list_.begin();
|
|
|
|
List::iterator end = list_.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
it->inset->setBuffer(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 13:47:13 +00:00
|
|
|
void InsetList::resetBuffer()
|
|
|
|
{
|
|
|
|
List::iterator it = list_.begin();
|
|
|
|
List::iterator end = list_.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
it->inset->resetBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-13 13:43:44 +00:00
|
|
|
InsetList::iterator InsetList::insetIterator(pos_type pos)
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
InsetTable search_elem(pos, 0);
|
2004-07-25 00:04:42 +00:00
|
|
|
return lower_bound(list_.begin(), list_.end(), search_elem,
|
2004-01-05 18:00:12 +00:00
|
|
|
InsetTablePosLess());
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-13 13:43:44 +00:00
|
|
|
InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
|
|
|
InsetTable search_elem(pos, 0);
|
2004-07-25 00:04:42 +00:00
|
|
|
return lower_bound(list_.begin(), list_.end(), search_elem,
|
2004-01-05 18:00:12 +00:00
|
|
|
InsetTablePosLess());
|
2003-11-13 13:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
void InsetList::insert(Inset * inset, pos_type pos)
|
2003-11-13 13:43:44 +00:00
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::iterator end = list_.end();
|
2003-11-13 13:43:44 +00:00
|
|
|
List::iterator it = insetIterator(pos);
|
2003-05-29 01:13:18 +00:00
|
|
|
if (it != end && it->pos == pos) {
|
2007-11-28 22:12:03 +00:00
|
|
|
LYXERR0("ERROR (InsetList::insert): "
|
|
|
|
<< "There is an inset in position: " << pos);
|
2002-08-11 15:03:52 +00:00
|
|
|
} else {
|
2004-07-25 00:04:42 +00:00
|
|
|
list_.insert(it, InsetTable(pos, inset));
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetList::erase(pos_type pos)
|
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::iterator end = list_.end();
|
2003-11-13 13:43:44 +00:00
|
|
|
List::iterator it = insetIterator(pos);
|
2003-05-29 01:13:18 +00:00
|
|
|
if (it != end && it->pos == pos) {
|
2002-08-11 15:03:52 +00:00
|
|
|
delete it->inset;
|
2004-07-25 00:04:42 +00:00
|
|
|
list_.erase(it);
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset * InsetList::release(pos_type pos)
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::iterator end = list_.end();
|
2003-11-13 13:43:44 +00:00
|
|
|
List::iterator it = insetIterator(pos);
|
2003-05-29 01:13:18 +00:00
|
|
|
if (it != end && it->pos == pos) {
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset * tmp = it->inset;
|
2002-08-11 15:03:52 +00:00
|
|
|
it->inset = 0;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset * InsetList::get(pos_type pos) const
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::const_iterator end = list_.end();
|
2003-11-13 13:43:44 +00:00
|
|
|
List::const_iterator it = insetIterator(pos);
|
2003-05-29 01:13:18 +00:00
|
|
|
if (it != end && it->pos == pos)
|
2002-08-11 15:03:52 +00:00
|
|
|
return it->inset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetList::increasePosAfterPos(pos_type pos)
|
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::iterator end = list_.end();
|
2003-11-13 13:43:44 +00:00
|
|
|
List::iterator it = insetIterator(pos);
|
2008-01-12 16:40:25 +00:00
|
|
|
for (; it != end; ++it)
|
2002-08-11 15:03:52 +00:00
|
|
|
++it->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetList::decreasePosAfterPos(pos_type pos)
|
|
|
|
{
|
2004-07-25 00:04:42 +00:00
|
|
|
List::iterator end = list_.end();
|
2003-11-13 13:43:44 +00:00
|
|
|
List::iterator it = insetIterator(pos);
|
2007-10-18 15:29:51 +00:00
|
|
|
for (; it != end; ++it)
|
2008-01-12 16:40:25 +00:00
|
|
|
--it->pos;
|
2007-10-18 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2007-10-24 15:32:43 +00:00
|
|
|
|
|
|
|
pos_type InsetList::find(InsetCode code, pos_type startpos) const
|
|
|
|
{
|
|
|
|
List::const_iterator it = insetIterator(startpos);
|
|
|
|
List::const_iterator end = list_.end();
|
|
|
|
for (; it != end ; ++it) {
|
|
|
|
if (it->inset->lyxCode() == code)
|
|
|
|
return it->pos;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InsetList::count(InsetCode code, pos_type startpos) const
|
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
List::const_iterator it = insetIterator(startpos);
|
|
|
|
List::const_iterator end = list_.end();
|
|
|
|
for (; it != end ; ++it) {
|
|
|
|
if (it->inset->lyxCode() == code)
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|