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