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