]> git.lyx.org Git - lyx.git/blob - src/InsetList.cpp
prepare Qt 5.6 builds
[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)
46 {
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();
52 }
53
54
55 InsetList::InsetList(InsetList const & il, pos_type beg, pos_type end)
56 {
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);
66         }
67 }
68
69
70 InsetList::~InsetList()
71 {
72         List::iterator it = list_.begin();
73         List::iterator end = list_.end();
74         for (; it != end; ++it)
75                 delete it->inset;
76 }
77
78
79 void InsetList::setBuffer(Buffer & b)
80 {
81         List::iterator it = list_.begin();
82         List::iterator end = list_.end();
83         for (; it != end; ++it)
84                 it->inset->setBuffer(b);
85 }
86
87
88 InsetList::iterator InsetList::insetIterator(pos_type pos)
89 {
90         InsetTable search_elem(pos, 0);
91         return lower_bound(list_.begin(), list_.end(), search_elem,
92                            InsetTablePosLess());
93 }
94
95
96 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
97 {
98         InsetTable search_elem(pos, 0);
99         return lower_bound(list_.begin(), list_.end(), search_elem,
100                            InsetTablePosLess());
101 }
102
103
104 void InsetList::insert(Inset * inset, pos_type pos)
105 {
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));
113         }
114 }
115
116
117 void InsetList::erase(pos_type pos)
118 {
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);
124         }
125 }
126
127
128 Inset * InsetList::release(pos_type pos)
129 {
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;
136         }
137         return 0;
138 }
139
140
141 Inset * InsetList::get(pos_type pos) const
142 {
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;
148 }
149
150
151 void InsetList::increasePosAfterPos(pos_type pos)
152 {
153         List::iterator end = list_.end();
154         List::iterator it = insetIterator(pos);
155         for (; it != end; ++it)
156                 ++it->pos;
157 }
158
159
160 void InsetList::decreasePosAfterPos(pos_type pos)
161 {
162         List::iterator end = list_.end();
163         List::iterator it = insetIterator(pos);
164         for (; it != end; ++it)
165                 --it->pos;
166 }
167
168
169 pos_type InsetList::find(InsetCode code, pos_type startpos) const
170 {
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;
176         }
177         return -1;
178 }
179
180
181 int InsetList::count(InsetCode code, pos_type startpos) const
182 {
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;
189         }
190         return num;
191 }
192
193 } // namespace lyx