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