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
11 * Full author contact details are available in file CREDITS.
16 #include "bufferview_funcs.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
23 #include "coordcache.h"
29 #include "paragraph.h"
30 #include "ParagraphParameters.h"
31 #include "pariterator.h"
33 #include "frontends/Alert.h"
34 #include "frontends/LyXView.h"
36 #include "insets/insettext.h"
38 #include "support/convert.h"
42 using lyx::support::bformat;
44 using std::istringstream;
45 using std::ostringstream;
51 // Set data using font and toggle
52 // If successful, returns true
53 bool font2string(LyXFont const & font, bool const toggle, string & data)
55 string lang = "ignore";
57 lang = font.language()->lang();
60 os << "family " << font.family() << '\n'
61 << "series " << font.series() << '\n'
62 << "shape " << font.shape() << '\n'
63 << "size " << font.size() << '\n'
64 << "emph " << font.emph() << '\n'
65 << "underbar " << font.underbar() << '\n'
66 << "noun " << font.noun() << '\n'
67 << "number " << font.number() << '\n'
68 << "color " << font.color() << '\n'
69 << "language " << lang << '\n'
70 << "toggleall " << convert<string>(toggle);
76 // Set font and toggle using data
77 // If successful, returns true
78 bool string2font(string const & data, LyXFont & font, bool & toggle)
80 istringstream is(data);
88 token = lex.getString();
90 if (token.empty() || !lex.next())
93 if (token == "family") {
94 int const next = lex.getInteger();
95 font.setFamily(LyXFont::FONT_FAMILY(next));
97 } else if (token == "series") {
98 int const next = lex.getInteger();
99 font.setSeries(LyXFont::FONT_SERIES(next));
101 } else if (token == "shape") {
102 int const next = lex.getInteger();
103 font.setShape(LyXFont::FONT_SHAPE(next));
105 } else if (token == "size") {
106 int const next = lex.getInteger();
107 font.setSize(LyXFont::FONT_SIZE(next));
109 } else if (token == "emph" || token == "underbar" ||
110 token == "noun" || token == "number") {
112 int const next = lex.getInteger();
113 LyXFont::FONT_MISC_STATE const misc =
114 LyXFont::FONT_MISC_STATE(next);
118 else if (token == "underbar")
119 font.setUnderbar(misc);
120 else if (token == "noun")
122 else if (token == "number")
123 font.setNumber(misc);
125 } else if (token == "color") {
126 int const next = lex.getInteger();
127 font.setColor(LColor::color(next));
129 } else if (token == "language") {
130 string const next = lex.getString();
131 if (next == "ignore")
132 font.setLanguage(ignore_language);
134 font.setLanguage(languages.getLanguage(next));
136 } else if (token == "toggleall") {
137 toggle = lex.getBool();
140 // Unrecognised token
150 // the next two should probably go elsewhere
151 // this give the position relative to (0, baseline) of outermost
153 Point coordOffset(DocIterator const & dit)
158 // Contribution of nested insets
159 for (size_t i = 1; i != dit.depth(); ++i) {
160 CursorSlice const & sl = dit[i];
163 sl.inset().getCursorPos(sl, xx, yy);
166 //lyxerr << "LCursor::getPos, i: " << i << " x: " << xx << " y: " << y << endl;
169 // Add contribution of initial rows of outermost paragraph
170 CursorSlice const & sl = dit[0];
171 Paragraph const & par = sl.text()->getPar(sl.pit());
172 y -= par.rows()[0].ascent();
173 for (size_t rit = 0, rend = par.pos2row(sl.pos()); rit != rend; ++rit)
174 y += par.rows()[rit].height();
175 y += par.rows()[par.pos2row(sl.pos())].ascent();
176 x += dit.bottom().text()->cursorX(dit.bottom());
181 Point getPos(DocIterator const & dit)
183 CursorSlice const & bot = dit.bottom();
184 CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(bot.text())->second;
185 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
186 if (it == cache.end()) {
187 //lyxerr << "cursor out of view" << std::endl;
188 return Point(-1, -1);
190 Point p = coordOffset(dit); // offset from outer paragraph
191 p.y_ += it->second.y_;
196 // this could be used elsewhere as well?
197 CurStatus status(BufferView const * bv, DocIterator const & dit)
199 CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
201 if (cache.find(dit.bottom().pit()) != cache.end())
203 else if (dit.bottom().pit() < bv->anchor_ref())
210 } // namespace bv_funcs