]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
Point fix, earlier forgotten
[lyx.git] / src / InsetList.C
1 /**
2  * \file InsetList.C
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
14 #include "InsetList.h"
15 #include "BufferView.h"
16 #include "buffer.h"
17 #include "debug.h"
18
19 #include "insets/updatableinset.h"
20 #include "insets/insetbranch.h"
21
22 #include <algorithm>
23
24 using lyx::pos_type;
25
26 using std::lower_bound;
27 using std::upper_bound;
28 using std::endl;
29
30 namespace {
31
32 struct MatchIt {
33         /// used by lower_bound and upper_bound
34         inline
35         int operator()(InsetList::InsetTable const & a,
36                        InsetList::InsetTable const & b) const
37         {
38                 return a.pos < b.pos;
39         }
40 };
41
42 } // namespace anon
43
44
45 InsetList::~InsetList()
46 {
47         // If we begin storing a shared_ptr in the List
48         // this code can be removed. (Lgb)
49         List::iterator it = list.begin();
50         List::iterator end = list.end();
51         for (; it != end; ++it) {
52                 delete it->inset;
53         }
54 }
55
56
57 InsetList::iterator InsetList::begin()
58 {
59         return list.begin();
60 }
61
62
63 InsetList::iterator InsetList::end()
64 {
65         return list.end();
66 }
67
68
69 InsetList::const_iterator InsetList::begin() const
70 {
71         return list.begin();
72 }
73
74
75 InsetList::const_iterator InsetList::end() const
76 {
77         return list.end();
78 }
79
80
81 InsetList::iterator
82 InsetList::insetIterator(pos_type pos)
83 {
84         InsetTable search_elem(pos, 0);
85         List::iterator it = lower_bound(list.begin(),
86                                         list.end(),
87                                         search_elem, MatchIt());
88         return it;
89 }
90
91
92 void InsetList::insert(InsetOld * inset, lyx::pos_type pos)
93 {
94         InsetTable search_elem(pos, 0);
95         List::iterator end = list.end();
96         List::iterator it = lower_bound(list.begin(),
97                                         end,
98                                         search_elem, MatchIt());
99         if (it != end && it->pos == pos) {
100                 lyxerr << "ERROR (InsetList::insert): "
101                        << "There is an inset in position: " << pos << endl;
102         } else {
103                 list.insert(it, InsetTable(pos, inset));
104         }
105 }
106
107
108 void InsetList::erase(pos_type pos)
109 {
110         InsetTable search_elem(pos, 0);
111         List::iterator end = list.end();
112         List::iterator it =
113                 lower_bound(list.begin(),
114                             end,
115                             search_elem, MatchIt());
116         if (it != end && it->pos == pos) {
117                 delete it->inset;
118                 list.erase(it);
119         }
120 }
121
122
123 InsetOld * InsetList::release(pos_type pos)
124 {
125         InsetTable search_elem(pos, 0);
126         List::iterator end = list.end();
127         List::iterator it =
128                 lower_bound(list.begin(),
129                             end,
130                             search_elem, MatchIt());
131         if (it != end && it->pos == pos) {
132                 InsetOld * tmp = it->inset;
133                 it->inset = 0;
134                 return tmp;
135         }
136         return 0;
137 }
138
139
140 InsetOld * InsetList::get(pos_type pos) const
141 {
142         InsetTable search_elem(pos, 0);
143         List::const_iterator end = list.end();
144         List::const_iterator it =
145                 lower_bound(list.begin(),
146                             end,
147                             search_elem, MatchIt());
148         if (it != end && it->pos == pos)
149                 return it->inset;
150         return 0;
151 }
152
153
154 void InsetList::increasePosAfterPos(pos_type pos)
155 {
156         InsetTable search_elem(pos, 0);
157         List::iterator end = list.end();
158         List::iterator it = lower_bound(list.begin(),
159                                         end,
160                                         search_elem, MatchIt());
161         for (; it != end; ++it) {
162                 ++it->pos;
163         }
164 }
165
166
167 void InsetList::decreasePosAfterPos(pos_type pos)
168 {
169         InsetTable search_elem(pos, 0);
170         List::iterator end = list.end();
171         List::iterator it = upper_bound(list.begin(),
172                                         end,
173                                         search_elem, MatchIt());
174         for (; it != end; ++it) {
175                 --it->pos;
176         }
177 }
178
179
180 void InsetList::deleteInsetsLyXText(BufferView * bv)
181 {
182         List::iterator it = list.begin();
183         List::iterator end = list.end();
184         for (; it != end; ++it) {
185                 if (it->inset) {
186                         if (it->inset->isTextInset()) {
187                                 static_cast<UpdatableInset*>
188                                         (it->inset)->deleteLyXText(bv, true);
189                         }
190                 }
191         }
192 }
193
194
195 void InsetList::insetsOpenCloseBranch(BufferView * bv)
196 {
197         BufferParams bp = bv->buffer()->params;
198         List::iterator it = list.begin();
199         List::iterator end = list.end();
200         for (; it != end; ++it) {
201                 if (it->inset && it->inset->lyxCode() == InsetOld::BRANCH_CODE) {
202                         InsetBranch * inset = static_cast<InsetBranch *>(it->inset);
203                         if (bp.branchlist.selected(inset->params().branch)) {
204                                 inset->open(bv);
205                         } else {
206                                 inset->close(bv);
207                         }
208                 }
209         }
210 }
211
212