]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
a bit nicer functor usage
[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 namespace {
30
31 class InsetTablePosLess : public std::binary_function<InsetList::InsetTable, InsetList::InsetTable, bool> {
32 public:
33         bool operator()(InsetList::InsetTable const & t1,
34                       InsetList::InsetTable const & t2) const
35         {
36                 return t1.pos < t2.pos;
37         }
38 };
39
40 } // namespace anon
41
42 InsetList::~InsetList()
43 {
44         // If we begin storing a shared_ptr in the List
45         // this code can be removed. (Lgb)
46         List::iterator it = list.begin();
47         List::iterator end = list.end();
48         for (; it != end; ++it) {
49                 delete it->inset;
50         }
51 }
52
53
54 InsetList::iterator InsetList::insetIterator(pos_type pos)
55 {
56         InsetTable search_elem(pos, 0);
57         return lower_bound(list.begin(), list.end(), search_elem,
58                            InsetTablePosLess());
59 }
60
61
62 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
63 {
64         InsetTable search_elem(pos, 0);
65         return lower_bound(list.begin(), list.end(), search_elem,
66                            InsetTablePosLess());
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 ||
143                     it->inset->lyxCode() != InsetOld::BRANCH_CODE)
144                         continue;
145
146                 InsetBranch * inset = static_cast<InsetBranch *>(it->inset);
147                 if (inset->isBranchSelected(buf.params().branchlist())) {
148                         inset->open();
149                 } else {
150                         inset->close();
151                 }
152         }
153 }