* de.po: sync with branch.
[lyx.git] / src / InsetList.cpp
blob56401a4ba6d37112c555174a5da44e487c482702
1 /**
2 * \file InsetList.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Martin Vermeer
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
13 #include <algorithm>
15 #include "InsetList.h"
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BranchList.h"
21 #include "insets/InsetBranch.h"
23 #include "support/debug.h"
25 using namespace std;
27 namespace lyx {
30 namespace {
32 typedef InsetList::InsetTable Table;
34 struct InsetTablePosLess
36 bool operator()(Table const & t1, Table const & t2) const
38 return t1.pos < t2.pos;
42 } // namespace anon
45 InsetList::InsetList(InsetList const & il)
47 list_ = il.list_;
48 List::iterator it = list_.begin();
49 List::iterator end = list_.end();
50 for (; it != end; ++it)
51 it->inset = it->inset->clone();
55 InsetList::InsetList(InsetList const & il, pos_type beg, pos_type end)
57 InsetList::const_iterator cit = il.begin();
58 InsetList::const_iterator cend = il.end();
59 for (; cit != cend; ++cit) {
60 if (cit->pos < beg)
61 continue;
62 if (cit->pos >= end)
63 break;
64 // Add a new entry in the insetlist_.
65 insert(cit->inset->clone(), cit->pos - beg);
70 InsetList::~InsetList()
72 List::iterator it = list_.begin();
73 List::iterator end = list_.end();
74 for (; it != end; ++it)
75 delete it->inset;
79 void InsetList::setBuffer(Buffer & b)
81 List::iterator it = list_.begin();
82 List::iterator end = list_.end();
83 for (; it != end; ++it)
84 it->inset->setBuffer(b);
88 InsetList::iterator InsetList::insetIterator(pos_type pos)
90 InsetTable search_elem(pos, 0);
91 return lower_bound(list_.begin(), list_.end(), search_elem,
92 InsetTablePosLess());
96 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
98 InsetTable search_elem(pos, 0);
99 return lower_bound(list_.begin(), list_.end(), search_elem,
100 InsetTablePosLess());
104 void InsetList::insert(Inset * inset, pos_type pos)
106 List::iterator end = list_.end();
107 List::iterator it = insetIterator(pos);
108 if (it != end && it->pos == pos) {
109 LYXERR0("ERROR (InsetList::insert): "
110 << "There is an inset in position: " << pos);
111 } else {
112 list_.insert(it, InsetTable(pos, inset));
117 void InsetList::erase(pos_type pos)
119 List::iterator end = list_.end();
120 List::iterator it = insetIterator(pos);
121 if (it != end && it->pos == pos) {
122 delete it->inset;
123 list_.erase(it);
128 Inset * InsetList::release(pos_type pos)
130 List::iterator end = list_.end();
131 List::iterator it = insetIterator(pos);
132 if (it != end && it->pos == pos) {
133 Inset * tmp = it->inset;
134 it->inset = 0;
135 return tmp;
137 return 0;
141 Inset * InsetList::get(pos_type pos) const
143 List::const_iterator end = list_.end();
144 List::const_iterator it = insetIterator(pos);
145 if (it != end && it->pos == pos)
146 return it->inset;
147 return 0;
151 void InsetList::increasePosAfterPos(pos_type pos)
153 List::iterator end = list_.end();
154 List::iterator it = insetIterator(pos);
155 for (; it != end; ++it)
156 ++it->pos;
160 void InsetList::decreasePosAfterPos(pos_type pos)
162 List::iterator end = list_.end();
163 List::iterator it = insetIterator(pos);
164 for (; it != end; ++it)
165 --it->pos;
169 pos_type InsetList::find(InsetCode code, pos_type startpos) const
171 List::const_iterator it = insetIterator(startpos);
172 List::const_iterator end = list_.end();
173 for (; it != end ; ++it) {
174 if (it->inset->lyxCode() == code)
175 return it->pos;
177 return -1;
181 int InsetList::count(InsetCode code, pos_type startpos) const
183 int num = 0;
184 List::const_iterator it = insetIterator(startpos);
185 List::const_iterator end = list_.end();
186 for (; it != end ; ++it) {
187 if (it->inset->lyxCode() == code)
188 ++num;
190 return num;
193 } // namespace lyx