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"
43 using lyx::support::bformat;
45 using std::istringstream;
46 using std::ostringstream;
53 // Set data using font and toggle
54 // If successful, returns true
55 bool font2string(LyXFont 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, LyXFont & 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(LyXFont::FONT_FAMILY(next));
99 } else if (token == "series") {
100 int const next = lex.getInteger();
101 font.setSeries(LyXFont::FONT_SERIES(next));
103 } else if (token == "shape") {
104 int const next = lex.getInteger();
105 font.setShape(LyXFont::FONT_SHAPE(next));
107 } else if (token == "size") {
108 int const next = lex.getInteger();
109 font.setSize(LyXFont::FONT_SIZE(next));
111 } else if (token == "emph" || token == "underbar" ||
112 token == "noun" || token == "number") {
114 int const next = lex.getInteger();
115 LyXFont::FONT_MISC_STATE const misc =
116 LyXFont::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(LColor::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(DocIterator const & dit, bool boundary)
160 // Contribution of nested insets
161 for (size_t i = 1; i != dit.depth(); ++i) {
162 CursorSlice const & sl = dit[i];
165 sl.inset().cursorPos(sl, boundary && ((i+1) == dit.depth()), xx, yy);
168 //lyxerr << "LCursor::getPos, i: "
169 // << i << " x: " << xx << " y: " << y << endl;
172 // Add contribution of initial rows of outermost paragraph
173 CursorSlice const & sl = dit[0];
174 Paragraph const & par = sl.text()->getPar(sl.pit());
175 y -= par.rows()[0].ascent();
178 if (sl.pos() > 0 && dit.depth() == 1) {
182 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
183 rend = par.pos2row(pos);
185 rend = par.pos2row(sl.pos());
187 size_t rend = par.pos2row(sl.pos());
189 for (size_t rit = 0; rit != rend; ++rit)
190 y += par.rows()[rit].height();
191 y += par.rows()[rend].ascent();
192 x += dit.bottom().text()->cursorX(dit.bottom(), boundary && dit.depth() == 1);
193 // The following correction should not be there at all.
194 // The cursor looks much better with the -1, though.
200 Point getPos(DocIterator const & dit, bool boundary)
202 CursorSlice const & bot = dit.bottom();
203 CoordCache::ParPosCache::const_iterator cache_it =
204 theCoords.getParPos().find(bot.text());
205 if (cache_it == theCoords.getParPos().end())
206 return Point(-1, -1);
208 CoordCache::InnerParPosCache const & cache = cache_it->second;
209 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
210 if (it == cache.end()) {
211 //lyxerr << "cursor out of view" << std::endl;
212 return Point(-1, -1);
214 Point p = coordOffset(dit, boundary); // offset from outer paragraph
215 p.y_ += it->second.y_;
220 // this could be used elsewhere as well?
221 CurStatus status(BufferView const * bv, DocIterator const & dit)
223 CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
225 if (cache.find(dit.bottom().pit()) != cache.end())
227 else if (dit.bottom().pit() < bv->anchor_ref())
235 bool findNextInset(DocIterator & dit,
236 vector<InsetBase_code> const & codes,
237 string const & contents)
239 DocIterator tmpdit = dit;
242 InsetBase const * inset = tmpdit.nextInset();
244 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
245 && (contents.empty() ||
246 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
250 tmpdit.forwardInset();
259 bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
263 DocIterator tmpdit = dit;
264 tmpdit.forwardInset();
269 InsetBase const * inset = tmpdit.nextInset();
271 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
272 contents = static_cast<InsetCommand const *>(inset)->getContents();
276 if (!findNextInset(tmpdit, codes, contents)) {
277 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
278 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
279 if (!findNextInset(tmpdit, codes, contents)) {
291 void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
293 findInset(dit, vector<InsetBase_code>(1, code), same_content);
297 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
300 LCursor tmpcur = bv->cursor();
301 if (!findInset(tmpcur, codes, same_content)) {
302 bv->cursor().message(_("No more insets"));
306 tmpcur.clearSelection();
307 bv->setCursor(tmpcur);
311 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
313 gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
317 } // namespace bv_funcs