]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
rename Inset to InsetOld
[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 } // namespace anon
29
30
31 InsetList::~InsetList()
32 {
33         // If we begin storing a shared_ptr in the List
34         // this code can be removed. (Lgb)
35         List::iterator it = list.begin();
36         List::iterator end = list.end();
37         for (; it != end; ++it) {
38                 delete it->inset;
39         }
40 }
41
42
43 InsetList::iterator InsetList::begin()
44 {
45         return list.begin();
46 }
47
48
49 InsetList::iterator InsetList::end()
50 {
51         return list.end();
52 }
53
54
55 InsetList::const_iterator InsetList::begin() const
56 {
57         return list.begin();
58 }
59
60
61 InsetList::const_iterator InsetList::end() const
62 {
63         return list.end();
64 }
65
66
67 InsetList::iterator
68 InsetList::insetIterator(pos_type pos)
69 {
70         InsetTable search_elem(pos, 0);
71         List::iterator it = lower_bound(list.begin(),
72                                         list.end(),
73                                         search_elem, MatchIt());
74         return it;
75 }
76
77
78 void InsetList::insert(InsetOld * inset, lyx::pos_type pos)
79 {
80         InsetTable search_elem(pos, 0);
81         List::iterator end = list.end();
82         List::iterator it = lower_bound(list.begin(),
83                                         end,
84                                         search_elem, MatchIt());
85         if (it != end && it->pos == pos) {
86                 lyxerr << "ERROR (InsetList::insert): "
87                        << "There is an inset in position: " << pos << endl;
88         } else {
89                 list.insert(it, InsetTable(pos, inset));
90         }
91 }
92
93
94 void InsetList::erase(pos_type pos)
95 {
96         InsetTable search_elem(pos, 0);
97         List::iterator end = list.end();
98         List::iterator it =
99                 lower_bound(list.begin(),
100                             end,
101                             search_elem, MatchIt());
102         if (it != end && it->pos == pos) {
103                 delete it->inset;
104                 list.erase(it);
105         }
106 }
107
108
109 InsetOld * InsetList::release(pos_type pos)
110 {
111         InsetTable search_elem(pos, 0);
112         List::iterator end = list.end();
113         List::iterator it =
114                 lower_bound(list.begin(),
115                             end,
116                             search_elem, MatchIt());
117         if (it != end && it->pos == pos) {
118                 InsetOld * tmp = it->inset;
119                 it->inset = 0;
120                 return tmp;
121         }
122         return 0;
123 }
124
125
126 InsetOld * InsetList::get(pos_type pos) const
127 {
128         InsetTable search_elem(pos, 0);
129         List::const_iterator end = list.end();
130         List::const_iterator it =
131                 lower_bound(list.begin(),
132                             end,
133                             search_elem, MatchIt());
134         if (it != end && it->pos == pos)
135                 return it->inset;
136         return 0;
137 }
138
139
140 void InsetList::increasePosAfterPos(pos_type pos)
141 {
142         InsetTable search_elem(pos, 0);
143         List::iterator end = list.end();
144         List::iterator it = lower_bound(list.begin(),
145                                         end,
146                                         search_elem, MatchIt());
147         for (; it != end; ++it) {
148                 ++it->pos;
149         }
150 }
151
152
153 void InsetList::decreasePosAfterPos(pos_type pos)
154 {
155         InsetTable search_elem(pos, 0);
156         List::iterator end = list.end();
157         List::iterator it = upper_bound(list.begin(),
158                                         end,
159                                         search_elem, MatchIt());
160         for (; it != end; ++it) {
161                 --it->pos;
162         }
163 }
164
165
166 void InsetList::deleteInsetsLyXText(BufferView * bv)
167 {
168         List::iterator it = list.begin();
169         List::iterator end = list.end();
170         for (; it != end; ++it) {
171                 if (it->inset) {
172                         if (it->inset->isTextInset()) {
173                                 static_cast<UpdatableInset*>
174                                         (it->inset)->deleteLyXText(bv, true);
175                         }
176                 }
177         }
178 }
179
180
181 void InsetList::resizeInsetsLyXText(BufferView * bv)
182 {
183         List::iterator it = list.begin();
184         List::iterator end = list.end();
185         for (; it != end; ++it) {
186                 if (it->inset) {
187                         if (it->inset->isTextInset()) {
188                                 static_cast<UpdatableInset*>
189                                         (it->inset)->resizeLyXText(bv, true);
190                         }
191                 }
192         }
193 }