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,
166 // Addup ontribution of nested insets, from inside to outside,
167 // keeping the outer paragraph for a special handling below
168 for (size_t i = dit.depth() - 1; i >= 1; --i) {
169 CursorSlice const & sl = dit[i];
173 // get relative position inside sl.inset()
174 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
176 // Make relative position inside of the edited inset relative to sl.inset()
180 // In case of an RTL inset, the edited inset will be positioned to the left
183 bool boundary_i = boundary && i + 1 == dit.depth();
184 bool rtl = sl.text()->isRTL(*bv.buffer(), sl, boundary_i);
189 // remember width for the case that sl.inset() is positioned in an RTL inset
190 lastw = sl.inset().width();
192 //lyxerr << "Cursor::getPos, i: "
193 // << i << " x: " << xx << " y: " << y << endl;
196 // Add contribution of initial rows of outermost paragraph
197 CursorSlice const & sl = dit[0];
198 ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
199 BOOST_ASSERT(!pm.rows().empty());
200 y -= pm.rows()[0].ascent();
202 // FIXME: document this mess
204 if (sl.pos() > 0 && dit.depth() == 1) {
208 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
209 rend = pm.pos2row(pos);
211 rend = pm.pos2row(sl.pos());
213 size_t rend = pm.pos2row(sl.pos());
215 for (size_t rit = 0; rit != rend; ++rit)
216 y += pm.rows()[rit].height();
217 y += pm.rows()[rend].ascent();
219 // Make relative position from the nested inset now bufferview absolute.
220 int xx = dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
223 // In the RTL case place the nested inset at the left of the cursor in
224 // the outer paragraph
225 bool boundary_1 = boundary && 1 == dit.depth();
226 bool rtl = dit.bottom().text()->isRTL(*bv.buffer(), dit.bottom(), boundary_1);
234 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
236 CursorSlice const & bot = dit.bottom();
237 CoordCache::ParPosCache::const_iterator cache_it =
238 bv.coordCache().getParPos().find(bot.text());
239 if (cache_it == bv.coordCache().getParPos().end())
240 return Point(-1, -1);
242 CoordCache::InnerParPosCache const & cache = cache_it->second;
243 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
244 if (it == cache.end()) {
245 //lyxerr << "cursor out of view" << std::endl;
246 return Point(-1, -1);
248 Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
249 p.y_ += it->second.y_;
254 // this could be used elsewhere as well?
255 // FIXME: This does not work within mathed!
256 CurStatus status(BufferView const * bv, DocIterator const & dit)
258 CoordCache::InnerParPosCache const & cache =
259 bv->coordCache().getParPos().find(dit.bottom().text())->second;
261 if (cache.find(dit.bottom().pit()) != cache.end())
263 else if (dit.bottom().pit() < bv->anchor_ref())
271 bool findNextInset(DocIterator & dit,
272 vector<Inset_code> const & codes,
273 string const & contents)
275 DocIterator tmpdit = dit;
278 Inset const * inset = tmpdit.nextInset();
280 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
281 && (contents.empty() ||
282 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
286 tmpdit.forwardInset();
295 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
299 DocIterator tmpdit = dit;
300 tmpdit.forwardInset();
305 Inset const * inset = tmpdit.nextInset();
307 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
308 contents = static_cast<InsetCommand const *>(inset)->getContents();
312 if (!findNextInset(tmpdit, codes, contents)) {
313 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
314 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
315 if (!findNextInset(tmpdit, codes, contents)) {
327 void findInset(DocIterator & dit, Inset_code code, bool same_content)
329 findInset(dit, vector<Inset_code>(1, code), same_content);
333 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
336 Cursor tmpcur = bv->cursor();
337 if (!findInset(tmpcur, codes, same_content)) {
338 bv->cursor().message(_("No more insets"));
342 tmpcur.clearSelection();
343 bv->setCursor(tmpcur);
347 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
349 gotoInset(bv, vector<Inset_code>(1, code), same_content);
353 } // namespace bv_funcs