]> git.lyx.org Git - lyx.git/blob - src/InsetList.cpp
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / InsetList.cpp
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.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Martin Vermeer
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13 #include <algorithm>
14
15 #include "InsetList.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BranchList.h"
20
21 #include "insets/InsetBranch.h"
22
23 #include "support/debug.h"
24
25 using namespace std;
26
27 namespace lyx {
28
29
30 namespace {
31
32 typedef InsetList::InsetTable Table;
33
34 struct InsetTablePosLess
35 {
36         bool operator()(Table const & t1, Table const & t2) const
37         {
38                 return t1.pos < t2.pos;
39         }
40 };
41
42 } // namespace anon
43
44
45 InsetList::InsetList(InsetList const & il) : list_(il.list_)
46 {
47         List::iterator it = list_.begin();
48         List::iterator end = list_.end();
49         for (; it != end; ++it)
50                 it->inset = it->inset->clone();
51 }
52
53
54 InsetList::InsetList(InsetList const & il, pos_type beg, pos_type end)
55 {
56         InsetList::const_iterator cit = il.begin();
57         InsetList::const_iterator cend = il.end();
58         for (; cit != cend; ++cit) {
59                 if (cit->pos < beg)
60                         continue;
61                 if (cit->pos >= end)
62                         break;
63                 // Add a new entry in the insetlist_.
64                 insert(cit->inset->clone(), cit->pos - beg);
65         }
66 }
67
68
69 InsetList::~InsetList()
70 {
71         List::iterator it = list_.begin();
72         List::iterator end = list_.end();
73         for (; it != end; ++it)
74                 delete it->inset;
75 }
76
77
78 void InsetList::setBuffer(Buffer & b)
79 {
80         List::iterator it = list_.begin();
81         List::iterator end = list_.end();
82         for (; it != end; ++it)
83                 it->inset->setBuffer(b);
84 }
85
86
87 InsetList::iterator InsetList::insetIterator(pos_type pos)
88 {
89         InsetTable search_elem(pos, 0);
90         return lower_bound(list_.begin(), list_.end(), search_elem,
91                            InsetTablePosLess());
92 }
93
94
95 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
96 {
97         InsetTable search_elem(pos, 0);
98         return lower_bound(list_.begin(), list_.end(), search_elem,
99                            InsetTablePosLess());
100 }
101
102
103 void InsetList::insert(Inset * inset, pos_type pos)
104 {
105         List::iterator end = list_.end();
106         List::iterator it = insetIterator(pos);
107         if (it != end && it->pos == pos) {
108                 LYXERR0("ERROR (InsetList::insert): "
109                        << "There is an inset in position: " << pos);
110         } else {
111                 list_.insert(it, InsetTable(pos, inset));
112         }
113 }
114
115
116 void InsetList::erase(pos_type pos)
117 {
118         List::iterator end = list_.end();
119         List::iterator it = insetIterator(pos);
120         if (it != end && it->pos == pos) {
121                 delete it->inset;
122                 list_.erase(it);
123         }
124 }
125
126
127 Inset * InsetList::release(pos_type pos)
128 {
129         List::iterator end = list_.end();
130         List::iterator it = insetIterator(pos);
131         if (it != end && it->pos == pos) {
132                 Inset * tmp = it->inset;
133                 it->inset = 0;
134                 return tmp;
135         }
136         return 0;
137 }
138
139
140 Inset * InsetList::get(pos_type pos) const
141 {
142         List::const_iterator end = list_.end();
143         List::const_iterator it = insetIterator(pos);
144         if (it != end && it->pos == pos)
145                 return it->inset;
146         return 0;
147 }
148
149
150 void InsetList::increasePosAfterPos(pos_type pos)
151 {
152         List::iterator end = list_.end();
153         List::iterator it = insetIterator(pos);
154         for (; it != end; ++it)
155                 ++it->pos;
156 }
157
158
159 void InsetList::decreasePosAfterPos(pos_type pos)
160 {
161         List::iterator end = list_.end();
162         List::iterator it = insetIterator(pos);
163         for (; it != end; ++it)
164                 --it->pos;
165 }
166
167
168 pos_type InsetList::find(InsetCode code, pos_type startpos) const
169 {
170         List::const_iterator it = insetIterator(startpos);
171         List::const_iterator end = list_.end();
172         for (; it != end ; ++it) {
173                 if (it->inset->lyxCode() == code)
174                         return it->pos;
175         }
176         return -1;
177 }
178
179
180 int InsetList::count(InsetCode code, pos_type startpos) const
181 {
182         int num = 0;
183         List::const_iterator it = insetIterator(startpos);
184         List::const_iterator end = list_.end();
185         for (; it != end ; ++it) {
186                 if (it->inset->lyxCode() == code)
187                         ++num;
188         }
189         return num;
190 }
191
192 } // namespace lyx