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