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 {
|
|
|
|
|
2018-05-28 10:33:17 +00:00
|
|
|
typedef InsetList::Element Table;
|
2004-01-26 10:13:15 +00:00
|
|
|
|
2018-05-28 10:33:17 +00:00
|
|
|
struct ElementPosLess
|
2007-11-28 22:12:03 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
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
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
for (auto & i : list_)
|
|
|
|
i.inset = i.inset->clone();
|
2008-01-12 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
for (auto const & ci : il) {
|
|
|
|
if (ci.pos < beg)
|
2008-08-07 08:50:41 +00:00
|
|
|
continue;
|
2020-02-29 05:21:02 +00:00
|
|
|
if (ci.pos >= end)
|
2008-08-07 08:50:41 +00:00
|
|
|
break;
|
|
|
|
// Add a new entry in the insetlist_.
|
2020-02-29 05:21:02 +00:00
|
|
|
insert(ci.inset->clone(), ci.pos - beg);
|
2008-08-07 08:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-01 22:59:25 +00:00
|
|
|
InsetList::~InsetList()
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
for (auto & i : list_)
|
|
|
|
delete i.inset;
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-23 12:49:22 +00:00
|
|
|
void InsetList::setBuffer(Buffer & b)
|
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
for (auto & i : list_)
|
|
|
|
i.inset->setBuffer(b);
|
2008-07-23 12:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 13:47:13 +00:00
|
|
|
void InsetList::resetBuffer()
|
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
for (auto & i : list_)
|
|
|
|
i.inset->resetBuffer();
|
2016-12-05 13:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-13 13:43:44 +00:00
|
|
|
InsetList::iterator InsetList::insetIterator(pos_type pos)
|
2002-08-11 15:03:52 +00:00
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
Element search_elem(pos, nullptr);
|
2004-07-25 00:04:42 +00:00
|
|
|
return lower_bound(list_.begin(), list_.end(), search_elem,
|
2018-05-28 10:33:17 +00:00
|
|
|
ElementPosLess());
|
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
|
|
|
{
|
2020-02-29 05:21:02 +00:00
|
|
|
Element search_elem(pos, nullptr);
|
2004-07-25 00:04:42 +00:00
|
|
|
return lower_bound(list_.begin(), list_.end(), search_elem,
|
2018-05-28 10:33:17 +00:00
|
|
|
ElementPosLess());
|
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 {
|
2018-05-28 10:33:17 +00:00
|
|
|
list_.insert(it, Element(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;
|
2020-02-29 05:21:02 +00:00
|
|
|
it->inset = nullptr;
|
2002-08-11 15:03:52 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
2020-02-29 05:21:02 +00:00
|
|
|
return nullptr;
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2020-02-29 05:21:02 +00:00
|
|
|
return nullptr;
|
2002-08-11 15:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|