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 "frontends/alert.h"
32 #include "insets/InsetCommand.h"
33 #include "insets/InsetText.h"
35 #include "support/convert.h"
42 using support::bformat;
44 using std::istringstream;
45 using std::ostringstream;
53 // Set data using font and toggle
54 // If successful, returns true
55 bool font2string(Font const & font, bool const toggle, string & data)
57 string lang = "ignore";
59 lang = font.language()->lang();
62 os << "family " << font.family() << '\n'
63 << "series " << font.series() << '\n'
64 << "shape " << font.shape() << '\n'
65 << "size " << font.size() << '\n'
66 << "emph " << font.emph() << '\n'
67 << "underbar " << font.underbar() << '\n'
68 << "noun " << font.noun() << '\n'
69 << "number " << font.number() << '\n'
70 << "color " << font.color() << '\n'
71 << "language " << lang << '\n'
72 << "toggleall " << convert<string>(toggle);
78 // Set font and toggle using data
79 // If successful, returns true
80 bool string2font(string const & data, Font & font, bool & toggle)
82 istringstream is(data);
90 token = lex.getString();
92 if (token.empty() || !lex.next())
95 if (token == "family") {
96 int const next = lex.getInteger();
97 font.setFamily(Font::FONT_FAMILY(next));
99 } else if (token == "series") {
100 int const next = lex.getInteger();
101 font.setSeries(Font::FONT_SERIES(next));
103 } else if (token == "shape") {
104 int const next = lex.getInteger();
105 font.setShape(Font::FONT_SHAPE(next));
107 } else if (token == "size") {
108 int const next = lex.getInteger();
109 font.setSize(Font::FONT_SIZE(next));
111 } else if (token == "emph" || token == "underbar" ||
112 token == "noun" || token == "number") {
114 int const next = lex.getInteger();
115 Font::FONT_MISC_STATE const misc =
116 Font::FONT_MISC_STATE(next);
120 else if (token == "underbar")
121 font.setUnderbar(misc);
122 else if (token == "noun")
124 else if (token == "number")
125 font.setNumber(misc);
127 } else if (token == "color") {
128 int const next = lex.getInteger();
129 font.setColor(Color::color(next));
131 } else if (token == "language") {
132 string const next = lex.getString();
133 if (next == "ignore")
134 font.setLanguage(ignore_language);
136 font.setLanguage(languages.getLanguage(next));
138 } else if (token == "toggleall") {
139 toggle = lex.getBool();
142 // Unrecognised token
152 // the next two should probably go elsewhere
153 // this give the position relative to (0, baseline) of outermost
155 Point coordOffset(BufferView const & bv, DocIterator const & dit,
162 // Addup ontribution of nested insets, from inside to outside,
163 // keeping the outer paragraph for a special handling below
164 for (size_t i = dit.depth() - 1; i >= 1; --i) {
165 CursorSlice const & sl = dit[i];
169 // get relative position inside sl.inset()
170 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
172 // Make relative position inside of the edited inset relative to sl.inset()
176 // In case of an RTL inset, the edited inset will be positioned to the left
179 bool boundary_i = boundary && i + 1 == dit.depth();
180 bool rtl = sl.text()->isRTL(*bv.buffer(), sl, boundary_i);
185 // remember width for the case that sl.inset() is positioned in an RTL inset
186 lastw = sl.inset().width();
188 //lyxerr << "Cursor::getPos, i: "
189 // << i << " x: " << xx << " y: " << y << endl;
192 // Add contribution of initial rows of outermost paragraph
193 CursorSlice const & sl = dit[0];
194 ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
195 BOOST_ASSERT(!pm.rows().empty());
196 y -= pm.rows()[0].ascent();
198 // FIXME: document this mess
200 if (sl.pos() > 0 && dit.depth() == 1) {
204 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
205 rend = pm.pos2row(pos);
207 rend = pm.pos2row(sl.pos());
209 size_t rend = pm.pos2row(sl.pos());
211 for (size_t rit = 0; rit != rend; ++rit)
212 y += pm.rows()[rit].height();
213 y += pm.rows()[rend].ascent();
215 // Make relative position from the nested inset now bufferview absolute.
216 int xx = dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
219 // In the RTL case place the nested inset at the left of the cursor in
220 // the outer paragraph
221 bool boundary_1 = boundary && 1 == dit.depth();
222 bool rtl = dit.bottom().text()->isRTL(*bv.buffer(), dit.bottom(), boundary_1);
230 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
232 CursorSlice const & bot = dit.bottom();
233 CoordCache::ParPosCache::const_iterator cache_it =
234 bv.coordCache().getParPos().find(bot.text());
235 if (cache_it == bv.coordCache().getParPos().end())
236 return Point(-1, -1);
238 CoordCache::InnerParPosCache const & cache = cache_it->second;
239 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
240 if (it == cache.end()) {
241 //lyxerr << "cursor out of view" << std::endl;
242 return Point(-1, -1);
244 Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
245 p.y_ += it->second.y_;
250 // this could be used elsewhere as well?
251 // FIXME: This does not work within mathed!
252 CurStatus status(BufferView const * bv, DocIterator const & dit)
254 CoordCache::InnerParPosCache const & cache =
255 bv->coordCache().getParPos().find(dit.bottom().text())->second;
257 if (cache.find(dit.bottom().pit()) != cache.end())
259 else if (dit.bottom().pit() < bv->anchor_ref())
267 bool findNextInset(DocIterator & dit,
268 vector<Inset_code> const & codes,
269 string const & contents)
271 DocIterator tmpdit = dit;
274 Inset const * inset = tmpdit.nextInset();
276 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
277 && (contents.empty() ||
278 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
282 tmpdit.forwardInset();
291 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
295 DocIterator tmpdit = dit;
296 tmpdit.forwardInset();
301 Inset const * inset = tmpdit.nextInset();
303 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
304 contents = static_cast<InsetCommand const *>(inset)->getContents();
308 if (!findNextInset(tmpdit, codes, contents)) {
309 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
310 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
311 if (!findNextInset(tmpdit, codes, contents)) {
323 void findInset(DocIterator & dit, Inset_code code, bool same_content)
325 findInset(dit, vector<Inset_code>(1, code), same_content);
329 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
332 Cursor tmpcur = bv->cursor();
333 if (!findInset(tmpcur, codes, same_content)) {
334 bv->cursor().message(_("No more insets"));
338 tmpcur.clearSelection();
339 bv->setCursor(tmpcur);
343 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
345 gotoInset(bv, vector<Inset_code>(1, code), same_content);
349 } // namespace bv_funcs