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