]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
the 'lots of small stuff' patch
[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::insetIterator(pos_type pos)
57 {
58         InsetTable search_elem(pos, 0);
59         return lower_bound(list.begin(), list.end(), search_elem, MatchIt());
60 }
61
62
63 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
64 {
65         InsetTable search_elem(pos, 0);
66         return lower_bound(list.begin(), list.end(), search_elem, MatchIt());
67 }
68
69
70 void InsetList::insert(InsetOld * inset, lyx::pos_type pos)
71 {
72         List::iterator end = list.end();
73         List::iterator it = insetIterator(pos);
74         if (it != end && it->pos == pos) {
75                 lyxerr << "ERROR (InsetList::insert): "
76                        << "There is an inset in position: " << pos << endl;
77         } else {
78                 list.insert(it, InsetTable(pos, inset));
79         }
80 }
81
82
83 void InsetList::erase(pos_type pos)
84 {
85         List::iterator end = list.end();
86         List::iterator it = insetIterator(pos);
87         if (it != end && it->pos == pos) {
88                 delete it->inset;
89                 list.erase(it);
90         }
91 }
92
93
94 InsetOld * InsetList::release(pos_type pos)
95 {
96         List::iterator end = list.end();
97         List::iterator it = insetIterator(pos);
98         if (it != end && it->pos == pos) {
99                 InsetOld * tmp = it->inset;
100                 it->inset = 0;
101                 return tmp;
102         }
103         return 0;
104 }
105
106
107 InsetOld * InsetList::get(pos_type pos) const
108 {
109         List::const_iterator end = list.end();
110         List::const_iterator it = insetIterator(pos);
111         if (it != end && it->pos == pos)
112                 return it->inset;
113         return 0;
114 }
115
116
117 void InsetList::increasePosAfterPos(pos_type pos)
118 {
119         List::iterator end = list.end();
120         List::iterator it = insetIterator(pos);
121         for (; it != end; ++it) {
122                 ++it->pos;
123         }
124 }
125
126
127 void InsetList::decreasePosAfterPos(pos_type pos)
128 {
129         List::iterator end = list.end();
130         List::iterator it = insetIterator(pos);
131         for (; it != end; ++it) {
132                 --it->pos;
133         }
134 }
135
136
137 void InsetList::insetsOpenCloseBranch(Buffer const & buf)
138 {
139         List::iterator it = list.begin();
140         List::iterator end = list.end();
141         for (; it != end; ++it) {
142                 if (it->inset && it->inset->lyxCode() == InsetOld::BRANCH_CODE) {
143                         InsetBranch * inset = static_cast<InsetBranch *>(it->inset);
144                         if (buf.params().branchlist().selected(inset->params().branch)) {
145                                 inset->open();
146                         } else {
147                                 inset->close();
148                         }
149                 }
150         }
151 }