]> git.lyx.org Git - lyx.git/blob - src/InsetList.cpp
* Doxy: polish html output.
[lyx.git] / src / InsetList.cpp
1 /**
2  * \file InsetList.cpp
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 "support/debug.h"
20
21 #include "insets/InsetBranch.h"
22
23 using namespace std;
24
25 namespace lyx {
26
27
28 namespace {
29
30 typedef InsetList::InsetTable Table;
31
32 struct InsetTablePosLess
33 {
34         bool operator()(Table const & t1, Table const & t2) const
35         {
36                 return t1.pos < t2.pos;
37         }
38 };
39
40 } // namespace anon
41
42
43 InsetList::InsetList(InsetList const & il)
44 {
45         list_ = il.list_;
46         List::iterator it = list_.begin();
47         List::iterator end = list_.end();
48         for (; it != end; ++it)
49                 it->inset = it->inset->clone();
50 }
51
52
53 InsetList::~InsetList()
54 {
55         List::iterator it = list_.begin();
56         List::iterator end = list_.end();
57         for (; it != end; ++it)
58                 delete it->inset;
59 }
60
61
62 InsetList::iterator InsetList::insetIterator(pos_type pos)
63 {
64         InsetTable search_elem(pos, 0);
65         return lower_bound(list_.begin(), list_.end(), search_elem,
66                            InsetTablePosLess());
67 }
68
69
70 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
71 {
72         InsetTable search_elem(pos, 0);
73         return lower_bound(list_.begin(), list_.end(), search_elem,
74                            InsetTablePosLess());
75 }
76
77
78 void InsetList::insert(Inset * inset, pos_type pos)
79 {
80         List::iterator end = list_.end();
81         List::iterator it = insetIterator(pos);
82         if (it != end && it->pos == pos) {
83                 LYXERR0("ERROR (InsetList::insert): "
84                        << "There is an inset in position: " << pos);
85         } else {
86                 list_.insert(it, InsetTable(pos, inset));
87         }
88 }
89
90
91 void InsetList::erase(pos_type pos)
92 {
93         List::iterator end = list_.end();
94         List::iterator it = insetIterator(pos);
95         if (it != end && it->pos == pos) {
96                 delete it->inset;
97                 list_.erase(it);
98         }
99 }
100
101
102 Inset * InsetList::release(pos_type pos)
103 {
104         List::iterator end = list_.end();
105         List::iterator it = insetIterator(pos);
106         if (it != end && it->pos == pos) {
107                 Inset * tmp = it->inset;
108                 it->inset = 0;
109                 return tmp;
110         }
111         return 0;
112 }
113
114
115 Inset * InsetList::get(pos_type pos) const
116 {
117         List::const_iterator end = list_.end();
118         List::const_iterator it = insetIterator(pos);
119         if (it != end && it->pos == pos)
120                 return it->inset;
121         return 0;
122 }
123
124
125 void InsetList::increasePosAfterPos(pos_type pos)
126 {
127         List::iterator end = list_.end();
128         List::iterator it = insetIterator(pos);
129         for (; it != end; ++it)
130                 ++it->pos;
131 }
132
133
134 void InsetList::decreasePosAfterPos(pos_type pos)
135 {
136         List::iterator end = list_.end();
137         List::iterator it = insetIterator(pos);
138         for (; it != end; ++it)
139                 --it->pos;
140 }
141
142
143 pos_type InsetList::find(InsetCode code, pos_type startpos) const
144 {
145         List::const_iterator it = insetIterator(startpos);
146         List::const_iterator end = list_.end();
147         for (; it != end ; ++it) {
148                 if (it->inset->lyxCode() == code)
149                         return it->pos;
150         }
151         return -1;
152 }
153
154
155 int InsetList::count(InsetCode code, pos_type startpos) const
156 {
157         int num = 0;
158         List::const_iterator it = insetIterator(startpos);
159         List::const_iterator end = list_.end();
160         for (; it != end ; ++it) {
161                 if (it->inset->lyxCode() == code)
162                         ++num;
163         }
164         return num;
165 }
166
167 } // namespace lyx