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