2 * \file bufferview_funcs.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
10 * \author Juergen Vigna
12 * Full author contact details are available in file CREDITS.
17 #include "bufferview_funcs.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
24 #include "CoordCache.h"
30 #include "Paragraph.h"
31 #include "ParagraphParameters.h"
32 #include "ParIterator.h"
34 #include "frontends/alert.h"
36 #include "insets/InsetCommand.h"
37 #include "insets/InsetText.h"
39 #include "support/convert.h"
46 using support::bformat;
48 using std::istringstream;
49 using std::ostringstream;
57 // Set data using font and toggle
58 // If successful, returns true
59 bool font2string(Font const & font, bool const toggle, string & data)
61 string lang = "ignore";
63 lang = font.language()->lang();
66 os << "family " << font.family() << '\n'
67 << "series " << font.series() << '\n'
68 << "shape " << font.shape() << '\n'
69 << "size " << font.size() << '\n'
70 << "emph " << font.emph() << '\n'
71 << "underbar " << font.underbar() << '\n'
72 << "noun " << font.noun() << '\n'
73 << "number " << font.number() << '\n'
74 << "color " << font.color() << '\n'
75 << "language " << lang << '\n'
76 << "toggleall " << convert<string>(toggle);
82 // Set font and toggle using data
83 // If successful, returns true
84 bool string2font(string const & data, Font & font, bool & toggle)
86 istringstream is(data);
94 token = lex.getString();
96 if (token.empty() || !lex.next())
99 if (token == "family") {
100 int const next = lex.getInteger();
101 font.setFamily(Font::FONT_FAMILY(next));
103 } else if (token == "series") {
104 int const next = lex.getInteger();
105 font.setSeries(Font::FONT_SERIES(next));
107 } else if (token == "shape") {
108 int const next = lex.getInteger();
109 font.setShape(Font::FONT_SHAPE(next));
111 } else if (token == "size") {
112 int const next = lex.getInteger();
113 font.setSize(Font::FONT_SIZE(next));
115 } else if (token == "emph" || token == "underbar" ||
116 token == "noun" || token == "number") {
118 int const next = lex.getInteger();
119 Font::FONT_MISC_STATE const misc =
120 Font::FONT_MISC_STATE(next);
124 else if (token == "underbar")
125 font.setUnderbar(misc);
126 else if (token == "noun")
128 else if (token == "number")
129 font.setNumber(misc);
131 } else if (token == "color") {
132 int const next = lex.getInteger();
133 font.setColor(Color::color(next));
135 } else if (token == "language") {
136 string const next = lex.getString();
137 if (next == "ignore")
138 font.setLanguage(ignore_language);
140 font.setLanguage(languages.getLanguage(next));
142 } else if (token == "toggleall") {
143 toggle = lex.getBool();
146 // Unrecognised token
156 // the next two should probably go elsewhere
157 // this give the position relative to (0, baseline) of outermost
159 Point coordOffset(BufferView const & bv, DocIterator const & dit,
165 // Contribution of nested insets
166 for (size_t i = 1; i != dit.depth(); ++i) {
167 CursorSlice const & sl = dit[i];
170 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
173 //lyxerr << "Cursor::getPos, i: "
174 // << i << " x: " << xx << " y: " << y << endl;
177 // Add contribution of initial rows of outermost paragraph
178 CursorSlice const & sl = dit[0];
179 ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
180 BOOST_ASSERT(!pm.rows().empty());
181 y -= pm.rows()[0].ascent();
184 if (sl.pos() > 0 && dit.depth() == 1) {
188 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
189 rend = pm.pos2row(pos);
191 rend = pm.pos2row(sl.pos());
193 size_t rend = pm.pos2row(sl.pos());
195 for (size_t rit = 0; rit != rend; ++rit)
196 y += pm.rows()[rit].height();
197 y += pm.rows()[rend].ascent();
198 x += dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
204 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
206 CursorSlice const & bot = dit.bottom();
207 CoordCache::ParPosCache::const_iterator cache_it =
208 bv.coordCache().getParPos().find(bot.text());
209 if (cache_it == bv.coordCache().getParPos().end())
210 return Point(-1, -1);
212 CoordCache::InnerParPosCache const & cache = cache_it->second;
213 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
214 if (it == cache.end()) {
215 //lyxerr << "cursor out of view" << std::endl;
216 return Point(-1, -1);
218 Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
219 p.y_ += it->second.y_;
224 // this could be used elsewhere as well?
225 // FIXME: This does not work within mathed!
226 CurStatus status(BufferView const * bv, DocIterator const & dit)
228 CoordCache::InnerParPosCache const & cache =
229 bv->coordCache().getParPos().find(dit.bottom().text())->second;
231 if (cache.find(dit.bottom().pit()) != cache.end())
233 else if (dit.bottom().pit() < bv->anchor_ref())
241 bool findNextInset(DocIterator & dit,
242 vector<Inset_code> const & codes,
243 string const & contents)
245 DocIterator tmpdit = dit;
248 Inset const * inset = tmpdit.nextInset();
250 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
251 && (contents.empty() ||
252 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
256 tmpdit.forwardInset();
265 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
269 DocIterator tmpdit = dit;
270 tmpdit.forwardInset();
275 Inset const * inset = tmpdit.nextInset();
277 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
278 contents = static_cast<InsetCommand const *>(inset)->getContents();
282 if (!findNextInset(tmpdit, codes, contents)) {
283 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
284 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
285 if (!findNextInset(tmpdit, codes, contents)) {
297 void findInset(DocIterator & dit, Inset_code code, bool same_content)
299 findInset(dit, vector<Inset_code>(1, code), same_content);
303 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
306 Cursor tmpcur = bv->cursor();
307 if (!findInset(tmpcur, codes, same_content)) {
308 bv->cursor().message(_("No more insets"));
312 tmpcur.clearSelection();
313 bv->setCursor(tmpcur);
317 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
319 gotoInset(bv, vector<Inset_code>(1, code), same_content);
323 } // namespace bv_funcs