2 * \file bufferview_funcs.C
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(LyXFont 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, LyXFont & 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(LyXFont::FONT_FAMILY(next));
103 } else if (token == "series") {
104 int const next = lex.getInteger();
105 font.setSeries(LyXFont::FONT_SERIES(next));
107 } else if (token == "shape") {
108 int const next = lex.getInteger();
109 font.setShape(LyXFont::FONT_SHAPE(next));
111 } else if (token == "size") {
112 int const next = lex.getInteger();
113 font.setSize(LyXFont::FONT_SIZE(next));
115 } else if (token == "emph" || token == "underbar" ||
116 token == "noun" || token == "number") {
118 int const next = lex.getInteger();
119 LyXFont::FONT_MISC_STATE const misc =
120 LyXFont::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(LColor::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 << "LCursor::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 Paragraph const & par = sl.text()->getPar(sl.pit());
180 y -= par.rows()[0].ascent();
183 if (sl.pos() > 0 && dit.depth() == 1) {
187 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
188 rend = par.pos2row(pos);
190 rend = par.pos2row(sl.pos());
192 size_t rend = par.pos2row(sl.pos());
194 for (size_t rit = 0; rit != rend; ++rit)
195 y += par.rows()[rit].height();
196 y += par.rows()[rend].ascent();
197 x += dit.bottom().text()->cursorX(*bv.buffer(), dit.bottom(), boundary && dit.depth() == 1);
198 // FIXME: The following correction should not be there at all.
199 // The cursor looks much better with the +1, though.
205 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
207 CursorSlice const & bot = dit.bottom();
208 CoordCache::ParPosCache::const_iterator cache_it =
209 bv.coordCache().getParPos().find(bot.text());
210 if (cache_it == bv.coordCache().getParPos().end())
211 return Point(-1, -1);
213 CoordCache::InnerParPosCache const & cache = cache_it->second;
214 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
215 if (it == cache.end()) {
216 //lyxerr << "cursor out of view" << std::endl;
217 return Point(-1, -1);
219 Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
220 p.y_ += it->second.y_;
225 // this could be used elsewhere as well?
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<InsetBase_code> const & codes,
243 string const & contents)
245 DocIterator tmpdit = dit;
248 InsetBase 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<InsetBase_code> const & codes,
269 DocIterator tmpdit = dit;
270 tmpdit.forwardInset();
275 InsetBase 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, InsetBase_code code, bool same_content)
299 findInset(dit, vector<InsetBase_code>(1, code), same_content);
303 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
306 LCursor 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, InsetBase_code code, bool same_content)
319 gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
323 } // namespace bv_funcs