]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
fix linking problem with boost
[lyx.git] / src / InsetList.C
1 #include <config.h>
2
3 #include "InsetList.h"
4 #include "debug.h"
5
6 #include "insets/inset.h"
7
8 #include <algorithm>
9
10 using lyx::pos_type;
11
12 using std::lower_bound;
13 using std::upper_bound;
14 using std::endl;
15
16 namespace {
17
18 struct MatchIt {
19         /// used by lower_bound and upper_bound
20         inline
21         int operator()(InsetList::InsetTable const & a,
22                        InsetList::InsetTable const & b) const {
23                 return a.pos < b.pos;
24         }
25 };
26
27 }
28
29
30 InsetList::iterator::iterator(InsetList::List::iterator const & iter)
31         : it(iter)
32 {}
33
34
35 InsetList::iterator & InsetList::iterator::operator++()
36 {
37         ++it;
38         return *this;
39 }
40
41
42 InsetList::iterator InsetList::iterator::operator++(int)
43 {
44         iterator tmp = *this;
45         ++*this;
46         return tmp;
47 }
48
49
50 pos_type InsetList::iterator::getPos() const
51 {
52         return it->pos;
53 }
54
55
56 Inset * InsetList::iterator::getInset() const
57 {
58         return it->inset;
59 }
60
61
62 void InsetList::iterator::setInset(Inset * inset)
63 {
64         it->inset = inset;
65 }
66
67
68 InsetList::~InsetList() 
69 {
70         // If we begin storing a shared_ptr in the List
71         // this code can be removed. (Lgb)
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
80 InsetList::iterator InsetList::begin()
81 {
82         return iterator(list.begin());
83 }
84
85
86 InsetList::iterator InsetList::end()
87 {
88         return iterator(list.end());
89 }
90
91
92 InsetList::iterator InsetList::begin() const
93 {
94         return iterator(const_cast<InsetList*>(this)->list.begin());
95 }
96
97
98 InsetList::iterator InsetList::end() const
99 {
100         return iterator(const_cast<InsetList*>(this)->list.end());
101 }
102
103
104 InsetList::iterator
105 InsetList::insetIterator(pos_type pos)
106 {
107         InsetTable search_elem(pos, 0);
108         List::iterator it = lower_bound(list.begin(),
109                                         list.end(),
110                                         search_elem, MatchIt());
111         return iterator(it);
112 }
113
114
115 void InsetList::insert(Inset * inset, lyx::pos_type pos)
116 {
117         InsetTable search_elem(pos, 0);
118         List::iterator it = lower_bound(list.begin(),
119                                         list.end(),
120                                         search_elem, MatchIt());
121         if (it != list.end() && it->pos == pos) {
122                 lyxerr << "ERROR (InsetList::insert): "
123                        << "There is an inset in position: " << pos << endl;
124         } else {
125                 list.insert(it, InsetTable(pos, inset));
126         }
127 }
128
129
130 void InsetList::erase(pos_type pos)
131 {
132         InsetTable search_elem(pos, 0);
133         List::iterator it =
134                 lower_bound(list.begin(),
135                             list.end(),
136                             search_elem, MatchIt());
137         if (it != list.end() && it->pos == pos) {
138                 delete it->inset;
139                 list.erase(it);
140         }
141 }
142
143
144 Inset * InsetList::release(pos_type pos)
145 {
146         InsetTable search_elem(pos, 0);
147         List::iterator it =
148                 lower_bound(list.begin(),
149                             list.end(),
150                             search_elem, MatchIt());
151         if (it != list.end() && it->pos == pos) {
152                 Inset * tmp = it->inset;
153                 it->inset = 0;
154                 return tmp;
155         }
156         return 0;
157 }
158
159         
160 Inset * InsetList::get(pos_type pos) const
161 {
162         InsetTable search_elem(pos, 0);
163         List::iterator it =
164                 lower_bound(const_cast<InsetList*>(this)->list.begin(),
165                             const_cast<InsetList*>(this)->list.end(),
166                             search_elem, MatchIt());
167         if (it != const_cast<InsetList*>(this)->list.end() && it->pos == pos)
168                 return it->inset;
169         return 0;
170 }
171
172
173 void InsetList::increasePosAfterPos(pos_type pos)
174 {
175         InsetTable search_elem(pos, 0);
176         List::iterator it = lower_bound(list.begin(),
177                                         list.end(),
178                                         search_elem, MatchIt());
179         List::iterator end = list.end();
180         for (; it != end; ++it) {
181                 ++it->pos;
182         }
183 }
184
185
186 void InsetList::decreasePosAfterPos(pos_type pos)
187 {
188         InsetTable search_elem(pos, 0);
189         List::iterator end = list.end();
190         List::iterator it = upper_bound(list.begin(),
191                                         end,
192                                         search_elem, MatchIt());
193         for (; it != end; ++it) {
194                 --it->pos;
195         }
196 }
197
198
199 void InsetList::deleteInsetsLyXText(BufferView * bv)
200 {
201         List::iterator it = list.begin();
202         List::iterator end = list.end();
203         for (; it != end; ++it) {
204                 if (it->inset) {
205                         if (it->inset->isTextInset()) {
206                                 static_cast<UpdatableInset*>
207                                         (it->inset)->deleteLyXText(bv, true);
208                         }
209                 }
210         }
211 }
212
213
214 void InsetList::resizeInsetsLyXText(BufferView * bv)
215 {
216         List::iterator it = list.begin();
217         List::iterator end = list.end();
218         for (; it != end; ++it) {
219                 if (it->inset) {
220                         if (it->inset->isTextInset()) {
221                                 static_cast<UpdatableInset*>
222                                         (it->inset)->resizeLyXText(bv, true);
223                         }
224                 }
225         }
226 }
227
228
229
230 bool operator==(InsetList::iterator const & i1,
231                 InsetList::iterator const & i2)
232 {
233         return i1.it == i2.it;
234         
235 }
236         
237
238 bool operator!=(InsetList::iterator const & i1,
239                 InsetList::iterator const & i2)
240 {
241         return !(i1 == i2);
242 }