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