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"
20 #include "BufferParams.h"
21 #include "BufferView.h"
23 #include "CoordCache.h"
29 #include "frontends/alert.h"
31 #include "insets/InsetCommand.h"
32 #include "insets/InsetText.h"
34 #include "support/convert.h"
41 using support::bformat;
43 using std::istringstream;
44 using std::ostringstream;
52 // Set data using font and toggle
53 // If successful, returns true
54 bool font2string(Font const & font, bool const toggle, string & data)
56 string lang = "ignore";
58 lang = font.language()->lang();
61 os << "family " << font.family() << '\n'
62 << "series " << font.series() << '\n'
63 << "shape " << font.shape() << '\n'
64 << "size " << font.size() << '\n'
65 << "emph " << font.emph() << '\n'
66 << "underbar " << font.underbar() << '\n'
67 << "noun " << font.noun() << '\n'
68 << "number " << font.number() << '\n'
69 << "color " << font.color() << '\n'
70 << "language " << lang << '\n'
71 << "toggleall " << convert<string>(toggle);
77 // Set font and toggle using data
78 // If successful, returns true
79 bool string2font(string const & data, Font & font, bool & toggle)
81 istringstream is(data);
89 token = lex.getString();
91 if (token.empty() || !lex.next())
94 if (token == "family") {
95 int const next = lex.getInteger();
96 font.setFamily(Font::FONT_FAMILY(next));
98 } else if (token == "series") {
99 int const next = lex.getInteger();
100 font.setSeries(Font::FONT_SERIES(next));
102 } else if (token == "shape") {
103 int const next = lex.getInteger();
104 font.setShape(Font::FONT_SHAPE(next));
106 } else if (token == "size") {
107 int const next = lex.getInteger();
108 font.setSize(Font::FONT_SIZE(next));
110 } else if (token == "emph" || token == "underbar" ||
111 token == "noun" || token == "number") {
113 int const next = lex.getInteger();
114 Font::FONT_MISC_STATE const misc =
115 Font::FONT_MISC_STATE(next);
119 else if (token == "underbar")
120 font.setUnderbar(misc);
121 else if (token == "noun")
123 else if (token == "number")
124 font.setNumber(misc);
126 } else if (token == "color") {
127 int const next = lex.getInteger();
128 font.setColor(Color::color(next));
130 } else if (token == "language") {
131 string const next = lex.getString();
132 if (next == "ignore")
133 font.setLanguage(ignore_language);
135 font.setLanguage(languages.getLanguage(next));
137 } else if (token == "toggleall") {
138 toggle = lex.getBool();
141 // Unrecognised token
151 // the next two should probably go elsewhere
152 // this give the position relative to (0, baseline) of outermost
154 Point coordOffset(BufferView const & bv, DocIterator const & dit,
161 // Addup ontribution of nested insets, from inside to outside,
162 // keeping the outer paragraph for a special handling below
163 for (size_t i = dit.depth() - 1; i >= 1; --i) {
164 CursorSlice const & sl = dit[i];
168 // get relative position inside sl.inset()
169 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
171 // Make relative position inside of the edited inset relative to sl.inset()
175 // In case of an RTL inset, the edited inset will be positioned to the left
178 bool boundary_i = boundary && i + 1 == dit.depth();
179 bool rtl = sl.text()->isRTL(*bv.buffer(), sl, boundary_i);
184 // remember width for the case that sl.inset() is positioned in an RTL inset
185 lastw = sl.inset().width();
187 //lyxerr << "Cursor::getPos, i: "
188 // << i << " x: " << xx << " y: " << y << endl;
191 // Add contribution of initial rows of outermost paragraph
192 CursorSlice const & sl = dit[0];
193 ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
194 BOOST_ASSERT(!pm.rows().empty());
195 y -= pm.rows()[0].ascent();
197 // FIXME: document this mess
199 if (sl.pos() > 0 && dit.depth() == 1) {
203 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
204 rend = pm.pos2row(pos);
206 rend = pm.pos2row(sl.pos());
208 size_t rend = pm.pos2row(sl.pos());
210 for (size_t rit = 0; rit != rend; ++rit)
211 y += pm.rows()[rit].height();
212 y += pm.rows()[rend].ascent();
214 // Make relative position from the nested inset now bufferview absolute.
215 int xx = dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
218 // In the RTL case place the nested inset at the left of the cursor in
219 // the outer paragraph
220 bool boundary_1 = boundary && 1 == dit.depth();
221 bool rtl = dit.bottom().text()->isRTL(*bv.buffer(), dit.bottom(), boundary_1);
229 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
231 CursorSlice const & bot = dit.bottom();
232 CoordCache::ParPosCache::const_iterator cache_it =
233 bv.coordCache().getParPos().find(bot.text());
234 if (cache_it == bv.coordCache().getParPos().end())
235 return Point(-1, -1);
237 CoordCache::InnerParPosCache const & cache = cache_it->second;
238 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
239 if (it == cache.end()) {
240 //lyxerr << "cursor out of view" << std::endl;
241 return Point(-1, -1);
243 Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
244 p.y_ += it->second.y_;
249 // this could be used elsewhere as well?
250 // FIXME: This does not work within mathed!
251 CurStatus status(BufferView const * bv, DocIterator const & dit)
253 CoordCache::InnerParPosCache const & cache =
254 bv->coordCache().getParPos().find(dit.bottom().text())->second;
256 if (cache.find(dit.bottom().pit()) != cache.end())
258 else if (dit.bottom().pit() < bv->anchor_ref())
266 bool findNextInset(DocIterator & dit,
267 vector<Inset_code> const & codes,
268 string const & contents)
270 DocIterator tmpdit = dit;
273 Inset const * inset = tmpdit.nextInset();
275 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
276 && (contents.empty() ||
277 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
281 tmpdit.forwardInset();
290 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
294 DocIterator tmpdit = dit;
295 tmpdit.forwardInset();
300 Inset const * inset = tmpdit.nextInset();
302 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
303 contents = static_cast<InsetCommand const *>(inset)->getContents();
307 if (!findNextInset(tmpdit, codes, contents)) {
308 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
309 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
310 if (!findNextInset(tmpdit, codes, contents)) {
322 void findInset(DocIterator & dit, Inset_code code, bool same_content)
324 findInset(dit, vector<Inset_code>(1, code), same_content);
328 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
331 Cursor tmpcur = bv->cursor();
332 if (!findInset(tmpcur, codes, same_content)) {
333 bv->cursor().message(_("No more insets"));
337 tmpcur.clearSelection();
338 bv->setCursor(tmpcur);
342 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
344 gotoInset(bv, vector<Inset_code>(1, code), same_content);
348 } // namespace bv_funcs