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/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)
160 // Contribution of nested insets
161 for (size_t i = 1; i != dit.depth(); ++i) {
162 CursorSlice const & sl = dit[i];
165 sl.inset().getCursorPos(sl, xx, yy);
168 //lyxerr << "LCursor::getPos, i: " << i << " x: " << xx << " y: " << y << endl;
171 // Add contribution of initial rows of outermost paragraph
172 CursorSlice const & sl = dit[0];
173 Paragraph const & par = sl.text()->getPar(sl.pit());
174 y -= par.rows()[0].ascent();
175 for (size_t rit = 0, rend = par.pos2row(sl.pos()); rit != rend; ++rit)
176 y += par.rows()[rit].height();
177 y += par.rows()[par.pos2row(sl.pos())].ascent();
178 x += dit.bottom().text()->cursorX(dit.bottom());
179 // The following correction should not be there at all.
180 // The cusor looks much better with the -1, though.
186 Point getPos(DocIterator const & dit)
188 CursorSlice const & bot = dit.bottom();
189 CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(bot.text())->second;
190 CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
191 if (it == cache.end()) {
192 //lyxerr << "cursor out of view" << std::endl;
193 return Point(-1, -1);
195 Point p = coordOffset(dit); // offset from outer paragraph
196 p.y_ += it->second.y_;
201 // this could be used elsewhere as well?
202 CurStatus status(BufferView const * bv, DocIterator const & dit)
204 CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
206 if (cache.find(dit.bottom().pit()) != cache.end())
208 else if (dit.bottom().pit() < bv->anchor_ref())
216 bool gotoNextInset(LCursor & cur,
217 vector<InsetBase_code> const & codes,
218 string const & contents)
220 LCursor tmpcur = cur;
223 InsetBase const * inset = tmpcur.nextInset();
225 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
226 && (contents.empty() ||
227 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
231 tmpcur.forwardInset();
240 void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
244 LCursor tmpcur = bv->cursor();
245 tmpcur.forwardInset();
248 InsetBase const * inset = tmpcur.nextInset();
250 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
251 contents = static_cast<InsetCommand const *>(inset)->getContents();
255 if (!gotoNextInset(tmpcur, codes, contents)) {
256 if (tmpcur != doc_iterator_begin(tmpcur.inset())) {
257 tmpcur.reset(tmpcur.bottom().inset());
258 if (!gotoNextInset(tmpcur, codes, contents))
259 bv->cursor().message(_("No more insets"));
261 bv->cursor().message(_("No more insets"));
265 tmpcur.clearSelection();
266 bv->setCursor(tmpcur);
270 void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
272 gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
276 } // namespace bv_funcs