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"
38 using std::istringstream;
39 using std::ostringstream;
47 using support::bformat;
51 // Set data using font and toggle
52 // If successful, returns true
53 bool font2string(Font 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, Font & 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(Font::FONT_FAMILY(next));
97 } else if (token == "series") {
98 int const next = lex.getInteger();
99 font.setSeries(Font::FONT_SERIES(next));
101 } else if (token == "shape") {
102 int const next = lex.getInteger();
103 font.setShape(Font::FONT_SHAPE(next));
105 } else if (token == "size") {
106 int const next = lex.getInteger();
107 font.setSize(Font::FONT_SIZE(next));
109 } else if (token == "emph" || token == "underbar" ||
110 token == "noun" || token == "number") {
112 int const next = lex.getInteger();
113 Font::FONT_MISC_STATE const misc =
114 Font::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(Color::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(BufferView const & bv, DocIterator const & dit,
160 // Addup contribution of nested insets, from inside to outside,
161 // keeping the outer paragraph for a special handling below
162 for (size_t i = dit.depth() - 1; i >= 1; --i) {
163 CursorSlice const & sl = dit[i];
167 // get relative position inside sl.inset()
168 sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
170 // Make relative position inside of the edited inset relative to sl.inset()
174 // In case of an RTL inset, the edited inset will be positioned to the left
177 bool boundary_i = boundary && i + 1 == dit.depth();
178 bool rtl = bv.textMetrics(sl.text()).isRTL(sl, boundary_i);
183 // remember width for the case that sl.inset() is positioned in an RTL inset
184 if (i && dit[i - 1].text()) {
185 // If this Inset is inside a Text Inset, retrieve the Dimension
186 // from the containing text instead of using Inset::dimension() which
187 // might not be implemented.
188 // FIXME (Abdel 23/09/2007): this is a bit messy because of the
189 // elimination of Inset::dim_ cache. This coordOffset() method needs
190 // to be rewritten in light of the new design.
191 Dimension const & dim = bv.parMetrics(dit[i - 1].text(),
192 dit[i - 1].pit()).insetDimension(&sl.inset());
195 Dimension const dim = sl.inset().dimension(bv);
199 //lyxerr << "Cursor::getPos, i: "
200 // << i << " x: " << xx << " y: " << y << endl;
203 // Add contribution of initial rows of outermost paragraph
204 CursorSlice const & sl = dit[0];
205 TextMetrics const & tm = bv.textMetrics(sl.text());
206 ParagraphMetrics const & pm = tm.parMetrics(sl.pit());
207 BOOST_ASSERT(!pm.rows().empty());
208 y -= pm.rows()[0].ascent();
210 // FIXME: document this mess
212 if (sl.pos() > 0 && dit.depth() == 1) {
216 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
217 rend = pm.pos2row(pos);
219 rend = pm.pos2row(sl.pos());
221 size_t rend = pm.pos2row(sl.pos());
223 for (size_t rit = 0; rit != rend; ++rit)
224 y += pm.rows()[rit].height();
225 y += pm.rows()[rend].ascent();
227 TextMetrics const & bottom_tm = bv.textMetrics(dit.bottom().text());
229 // Make relative position from the nested inset now bufferview absolute.
230 int xx = bottom_tm.cursorX(dit.bottom(), boundary && dit.depth() == 1);
233 // In the RTL case place the nested inset at the left of the cursor in
234 // the outer paragraph
235 bool boundary_1 = boundary && 1 == dit.depth();
236 bool rtl = bottom_tm.isRTL(dit.bottom(), boundary_1);
244 Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
246 CursorSlice const & bot = dit.bottom();
247 TextMetrics const & tm = bv.textMetrics(bot.text());
248 if (!tm.has(bot.pit()))
249 return Point(-1, -1);
251 Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
252 p.y_ += tm.parMetrics(bot.pit()).position();
257 // this could be used elsewhere as well?
258 // FIXME: This does not work within mathed!
259 CurStatus status(BufferView const * bv, DocIterator const & dit)
261 Point const p = bv_funcs::getPos(*bv, dit, dit.boundary());
264 else if (p.y_ > bv->workHeight())
272 bool findNextInset(DocIterator & dit,
273 vector<Inset_code> const & codes,
274 string const & contents)
276 DocIterator tmpdit = dit;
279 Inset const * inset = tmpdit.nextInset();
281 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
282 && (contents.empty() ||
283 static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
287 tmpdit.forwardInset();
296 bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
300 DocIterator tmpdit = dit;
301 tmpdit.forwardInset();
306 Inset const * inset = tmpdit.nextInset();
308 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
309 contents = static_cast<InsetCommand const *>(inset)->getContents();
313 if (!findNextInset(tmpdit, codes, contents)) {
314 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
315 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
316 if (!findNextInset(tmpdit, codes, contents)) {
328 void findInset(DocIterator & dit, Inset_code code, bool same_content)
330 findInset(dit, vector<Inset_code>(1, code), same_content);
334 void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
337 Cursor tmpcur = bv->cursor();
338 if (!findInset(tmpcur, codes, same_content)) {
339 bv->cursor().message(_("No more insets"));
343 tmpcur.clearSelection();
344 bv->setCursor(tmpcur);
348 void gotoInset(BufferView * bv, Inset_code code, bool same_content)
350 gotoInset(bv, vector<Inset_code>(1, code), same_content);
354 } // namespace bv_funcs