]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
Overhaul the branches code.
[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 "BranchList.h"
19 #include "BufferView.h"
20 #include "debug.h"
21
22 #include "insets/insetbranch.h"
23
24 using lyx::pos_type;
25
26 using std::endl;
27 using std::lower_bound;
28
29
30 namespace {
31
32 struct MatchIt {
33         /// used by lower_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::insetIterator(pos_type pos)
58 {
59         InsetTable search_elem(pos, 0);
60         return lower_bound(list.begin(), list.end(), search_elem, MatchIt());
61 }
62
63
64 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
65 {
66         InsetTable search_elem(pos, 0);
67         return lower_bound(list.begin(), list.end(), search_elem, MatchIt());
68 }
69
70
71 void InsetList::insert(InsetOld * inset, lyx::pos_type pos)
72 {
73         List::iterator end = list.end();
74         List::iterator it = insetIterator(pos);
75         if (it != end && it->pos == pos) {
76                 lyxerr << "ERROR (InsetList::insert): "
77                        << "There is an inset in position: " << pos << endl;
78         } else {
79                 list.insert(it, InsetTable(pos, inset));
80         }
81 }
82
83
84 void InsetList::erase(pos_type pos)
85 {
86         List::iterator end = list.end();
87         List::iterator it = insetIterator(pos);
88         if (it != end && it->pos == pos) {
89                 delete it->inset;
90                 list.erase(it);
91         }
92 }
93
94
95 InsetOld * InsetList::release(pos_type pos)
96 {
97         List::iterator end = list.end();
98         List::iterator it = insetIterator(pos);
99         if (it != end && it->pos == pos) {
100                 InsetOld * tmp = it->inset;
101                 it->inset = 0;
102                 return tmp;
103         }
104         return 0;
105 }
106
107
108 InsetOld * InsetList::get(pos_type pos) const
109 {
110         List::const_iterator end = list.end();
111         List::const_iterator it = insetIterator(pos);
112         if (it != end && it->pos == pos)
113                 return it->inset;
114         return 0;
115 }
116
117
118 void InsetList::increasePosAfterPos(pos_type pos)
119 {
120         List::iterator end = list.end();
121         List::iterator it = insetIterator(pos);
122         for (; it != end; ++it) {
123                 ++it->pos;
124         }
125 }
126
127
128 void InsetList::decreasePosAfterPos(pos_type pos)
129 {
130         List::iterator end = list.end();
131         List::iterator it = insetIterator(pos);
132         for (; it != end; ++it) {
133                 --it->pos;
134         }
135 }
136
137
138 void InsetList::insetsOpenCloseBranch(Buffer const & buf)
139 {
140         List::iterator it = list.begin();
141         List::iterator end = list.end();
142         for (; it != end; ++it) {
143                 if (!it->inset ||
144                     it->inset->lyxCode() != InsetOld::BRANCH_CODE)
145                         continue;
146
147                 InsetBranch * inset = static_cast<InsetBranch *>(it->inset);
148                 if (inset->isBranchSelected(buf.params().branchlist())) {
149                         inset->open();
150                 } else {
151                         inset->close();
152                 }
153         }
154 }