Prepare ANNOUNCE and NEWS for rc2
[lyx.git] / src / InsetList.cpp
blob057c82d00ec1c376510d9a949a83848f7d4a1e9b
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()
57 List::iterator it = list_.begin();
58 List::iterator end = list_.end();
59 for (; it != end; ++it)
60 delete it->inset;
64 void InsetList::setBuffer(Buffer & b)
66 List::iterator it = list_.begin();
67 List::iterator end = list_.end();
68 for (; it != end; ++it)
69 it->inset->setBuffer(b);
73 InsetList::iterator InsetList::insetIterator(pos_type pos)
75 InsetTable search_elem(pos, 0);
76 return lower_bound(list_.begin(), list_.end(), search_elem,
77 InsetTablePosLess());
81 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
83 InsetTable search_elem(pos, 0);
84 return lower_bound(list_.begin(), list_.end(), search_elem,
85 InsetTablePosLess());
89 void InsetList::insert(Inset * inset, pos_type pos)
91 List::iterator end = list_.end();
92 List::iterator it = insetIterator(pos);
93 if (it != end && it->pos == pos) {
94 LYXERR0("ERROR (InsetList::insert): "
95 << "There is an inset in position: " << pos);
96 } else {
97 list_.insert(it, InsetTable(pos, inset));
102 void InsetList::erase(pos_type pos)
104 List::iterator end = list_.end();
105 List::iterator it = insetIterator(pos);
106 if (it != end && it->pos == pos) {
107 delete it->inset;
108 list_.erase(it);
113 Inset * InsetList::release(pos_type pos)
115 List::iterator end = list_.end();
116 List::iterator it = insetIterator(pos);
117 if (it != end && it->pos == pos) {
118 Inset * tmp = it->inset;
119 it->inset = 0;
120 return tmp;
122 return 0;
126 Inset * InsetList::get(pos_type pos) const
128 List::const_iterator end = list_.end();
129 List::const_iterator it = insetIterator(pos);
130 if (it != end && it->pos == pos)
131 return it->inset;
132 return 0;
136 void InsetList::increasePosAfterPos(pos_type pos)
138 List::iterator end = list_.end();
139 List::iterator it = insetIterator(pos);
140 for (; it != end; ++it)
141 ++it->pos;
145 void InsetList::decreasePosAfterPos(pos_type pos)
147 List::iterator end = list_.end();
148 List::iterator it = insetIterator(pos);
149 for (; it != end; ++it)
150 --it->pos;
154 pos_type InsetList::find(InsetCode code, pos_type startpos) const
156 List::const_iterator it = insetIterator(startpos);
157 List::const_iterator end = list_.end();
158 for (; it != end ; ++it) {
159 if (it->inset->lyxCode() == code)
160 return it->pos;
162 return -1;
166 int InsetList::count(InsetCode code, pos_type startpos) const
168 int num = 0;
169 List::const_iterator it = insetIterator(startpos);
170 List::const_iterator end = list_.end();
171 for (; it != end ; ++it) {
172 if (it->inset->lyxCode() == code)
173 ++num;
175 return num;
178 } // namespace lyx