2003-03-12 22:17:50 +00:00
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
|
* \file bufferview_funcs.cpp
|
2003-03-12 22:17:50 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2003-03-12 22:17:50 +00:00
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
* \author John Levon
|
|
|
|
|
* \author Angus Leeming
|
2005-07-16 00:04:54 +00:00
|
|
|
|
* \author Juergen Vigna
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-03-12 22:17:50 +00:00
|
|
|
|
*/
|
2000-07-24 13:53:19 +00:00
|
|
|
|
|
2000-04-12 15:11:29 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "bufferview_funcs.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Author.h"
|
|
|
|
|
#include "Buffer.h"
|
|
|
|
|
#include "BufferParams.h"
|
2000-04-12 15:11:29 +00:00
|
|
|
|
#include "BufferView.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "LCursor.h"
|
|
|
|
|
#include "CoordCache.h"
|
2001-04-17 15:15:59 +00:00
|
|
|
|
#include "gettext.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Language.h"
|
2003-09-16 10:30:59 +00:00
|
|
|
|
#include "LColor.h"
|
2007-04-26 11:30:54 +00:00
|
|
|
|
#include "Lexer.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Row.h"
|
|
|
|
|
#include "Paragraph.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
#include "ParagraphParameters.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "ParIterator.h"
|
2000-04-12 15:11:29 +00:00
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
#include "frontends/Alert.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
|
#include "insets/InsetCommand.h"
|
|
|
|
|
#include "insets/InsetText.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
|
#include "support/convert.h"
|
2004-07-24 10:55:30 +00:00
|
|
|
|
|
|
|
|
|
#include <sstream>
|
2003-02-20 17:39:48 +00:00
|
|
|
|
|
2006-10-15 22:32:56 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::bformat;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2003-09-05 18:02:24 +00:00
|
|
|
|
using std::istringstream;
|
|
|
|
|
using std::ostringstream;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2005-02-22 11:41:22 +00:00
|
|
|
|
using std::vector;
|
2006-09-28 00:42:47 +00:00
|
|
|
|
using std::find;
|
2003-09-05 18:02:24 +00:00
|
|
|
|
|
2002-11-21 18:33:09 +00:00
|
|
|
|
|
2003-04-03 00:36:31 +00:00
|
|
|
|
namespace bv_funcs {
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
// Set data using font and toggle
|
|
|
|
|
// If successful, returns true
|
2005-01-05 20:21:27 +00:00
|
|
|
|
bool font2string(LyXFont const & font, bool const toggle, string & data)
|
2003-03-12 22:17:50 +00:00
|
|
|
|
{
|
|
|
|
|
string lang = "ignore";
|
|
|
|
|
if (font.language())
|
|
|
|
|
lang = font.language()->lang();
|
|
|
|
|
|
|
|
|
|
ostringstream os;
|
|
|
|
|
os << "family " << font.family() << '\n'
|
|
|
|
|
<< "series " << font.series() << '\n'
|
|
|
|
|
<< "shape " << font.shape() << '\n'
|
|
|
|
|
<< "size " << font.size() << '\n'
|
|
|
|
|
<< "emph " << font.emph() << '\n'
|
|
|
|
|
<< "underbar " << font.underbar() << '\n'
|
|
|
|
|
<< "noun " << font.noun() << '\n'
|
|
|
|
|
<< "number " << font.number() << '\n'
|
|
|
|
|
<< "color " << font.color() << '\n'
|
|
|
|
|
<< "language " << lang << '\n'
|
2005-01-06 15:40:49 +00:00
|
|
|
|
<< "toggleall " << convert<string>(toggle);
|
2003-09-15 11:00:00 +00:00
|
|
|
|
data = os.str();
|
2003-03-12 22:17:50 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set font and toggle using data
|
|
|
|
|
// If successful, returns true
|
|
|
|
|
bool string2font(string const & data, LyXFont & font, bool & toggle)
|
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
istringstream is(data);
|
2007-04-26 11:30:54 +00:00
|
|
|
|
Lexer lex(0,0);
|
2003-03-12 22:17:50 +00:00
|
|
|
|
lex.setStream(is);
|
|
|
|
|
|
|
|
|
|
int nset = 0;
|
|
|
|
|
while (lex.isOK()) {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
string token;
|
|
|
|
|
if (lex.next())
|
|
|
|
|
token = lex.getString();
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
2003-04-01 09:39:06 +00:00
|
|
|
|
if (token.empty() || !lex.next())
|
2003-03-12 22:17:50 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (token == "family") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
int const next = lex.getInteger();
|
2003-04-01 10:07:08 +00:00
|
|
|
|
font.setFamily(LyXFont::FONT_FAMILY(next));
|
2003-03-30 21:42:09 +00:00
|
|
|
|
|
2003-03-12 22:17:50 +00:00
|
|
|
|
} else if (token == "series") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
int const next = lex.getInteger();
|
2003-04-01 10:07:08 +00:00
|
|
|
|
font.setSeries(LyXFont::FONT_SERIES(next));
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
} else if (token == "shape") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
int const next = lex.getInteger();
|
2003-04-01 10:07:08 +00:00
|
|
|
|
font.setShape(LyXFont::FONT_SHAPE(next));
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
} else if (token == "size") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
int const next = lex.getInteger();
|
2003-04-01 10:07:08 +00:00
|
|
|
|
font.setSize(LyXFont::FONT_SIZE(next));
|
2003-04-01 09:39:06 +00:00
|
|
|
|
|
|
|
|
|
} else if (token == "emph" || token == "underbar" ||
|
|
|
|
|
token == "noun" || token == "number") {
|
|
|
|
|
|
|
|
|
|
int const next = lex.getInteger();
|
|
|
|
|
LyXFont::FONT_MISC_STATE const misc =
|
2003-04-01 10:07:08 +00:00
|
|
|
|
LyXFont::FONT_MISC_STATE(next);
|
2003-04-01 09:39:06 +00:00
|
|
|
|
|
|
|
|
|
if (token == "emph")
|
|
|
|
|
font.setEmph(misc);
|
|
|
|
|
else if (token == "underbar")
|
|
|
|
|
font.setUnderbar(misc);
|
|
|
|
|
else if (token == "noun")
|
|
|
|
|
font.setNoun(misc);
|
|
|
|
|
else if (token == "number")
|
|
|
|
|
font.setNumber(misc);
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
} else if (token == "color") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
int const next = lex.getInteger();
|
2003-04-01 10:07:08 +00:00
|
|
|
|
font.setColor(LColor::color(next));
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
} else if (token == "language") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
string const next = lex.getString();
|
|
|
|
|
if (next == "ignore")
|
2003-03-12 22:17:50 +00:00
|
|
|
|
font.setLanguage(ignore_language);
|
|
|
|
|
else
|
2003-04-01 09:39:06 +00:00
|
|
|
|
font.setLanguage(languages.getLanguage(next));
|
2003-03-12 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
} else if (token == "toggleall") {
|
2003-04-01 09:39:06 +00:00
|
|
|
|
toggle = lex.getBool();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Unrecognised token
|
|
|
|
|
break;
|
2003-03-12 22:17:50 +00:00
|
|
|
|
}
|
2003-04-01 09:39:06 +00:00
|
|
|
|
|
|
|
|
|
++nset;
|
2003-03-12 22:17:50 +00:00
|
|
|
|
}
|
|
|
|
|
return (nset > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-30 01:59:49 +00:00
|
|
|
|
|
|
|
|
|
// the next two should probably go elsewhere
|
|
|
|
|
// this give the position relative to (0, baseline) of outermost
|
|
|
|
|
// paragraph
|
2006-10-17 16:23:27 +00:00
|
|
|
|
Point coordOffset(BufferView const & bv, DocIterator const & dit,
|
|
|
|
|
bool boundary)
|
2004-11-30 01:59:49 +00:00
|
|
|
|
{
|
|
|
|
|
int x = 0;
|
|
|
|
|
int y = 0;
|
2005-01-06 15:40:49 +00:00
|
|
|
|
|
2004-11-30 01:59:49 +00:00
|
|
|
|
// Contribution of nested insets
|
2005-02-08 13:18:05 +00:00
|
|
|
|
for (size_t i = 1; i != dit.depth(); ++i) {
|
2004-11-30 01:59:49 +00:00
|
|
|
|
CursorSlice const & sl = dit[i];
|
2005-02-08 13:18:05 +00:00
|
|
|
|
int xx = 0;
|
|
|
|
|
int yy = 0;
|
2006-10-17 16:23:27 +00:00
|
|
|
|
sl.inset().cursorPos(bv, sl, boundary && ((i+1) == dit.depth()), xx, yy);
|
2004-11-30 01:59:49 +00:00
|
|
|
|
x += xx;
|
|
|
|
|
y += yy;
|
2005-07-15 15:49:40 +00:00
|
|
|
|
//lyxerr << "LCursor::getPos, i: "
|
|
|
|
|
// << i << " x: " << xx << " y: " << y << endl;
|
2004-11-30 01:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add contribution of initial rows of outermost paragraph
|
|
|
|
|
CursorSlice const & sl = dit[0];
|
2006-12-29 23:54:48 +00:00
|
|
|
|
ParagraphMetrics const & pm = bv.parMetrics(sl.text(), sl.pit());
|
|
|
|
|
BOOST_ASSERT(!pm.rows().empty());
|
|
|
|
|
y -= pm.rows()[0].ascent();
|
2005-07-16 00:04:54 +00:00
|
|
|
|
#if 1
|
|
|
|
|
size_t rend;
|
|
|
|
|
if (sl.pos() > 0 && dit.depth() == 1) {
|
|
|
|
|
int pos = sl.pos();
|
|
|
|
|
if (pos && boundary)
|
|
|
|
|
--pos;
|
|
|
|
|
// lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
|
2006-12-29 23:54:48 +00:00
|
|
|
|
rend = pm.pos2row(pos);
|
2005-07-16 00:04:54 +00:00
|
|
|
|
} else
|
2006-12-29 23:54:48 +00:00
|
|
|
|
rend = pm.pos2row(sl.pos());
|
2005-07-16 00:04:54 +00:00
|
|
|
|
#else
|
2006-12-29 23:54:48 +00:00
|
|
|
|
size_t rend = pm.pos2row(sl.pos());
|
2005-07-16 00:04:54 +00:00
|
|
|
|
#endif
|
2005-07-15 15:49:40 +00:00
|
|
|
|
for (size_t rit = 0; rit != rend; ++rit)
|
2006-12-29 23:54:48 +00:00
|
|
|
|
y += pm.rows()[rit].height();
|
|
|
|
|
y += pm.rows()[rend].ascent();
|
|
|
|
|
x += dit.bottom().text()->cursorX(bv, dit.bottom(), boundary && dit.depth() == 1);
|
2006-11-12 08:39:11 +00:00
|
|
|
|
|
2005-02-15 19:53:42 +00:00
|
|
|
|
return Point(x, y);
|
2004-11-30 01:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-08 11:38:51 +00:00
|
|
|
|
Point getPos(BufferView const & bv, DocIterator const & dit, bool boundary)
|
2004-11-30 01:59:49 +00:00
|
|
|
|
{
|
|
|
|
|
CursorSlice const & bot = dit.bottom();
|
2006-07-08 14:40:22 +00:00
|
|
|
|
CoordCache::ParPosCache::const_iterator cache_it =
|
2006-10-13 16:44:44 +00:00
|
|
|
|
bv.coordCache().getParPos().find(bot.text());
|
|
|
|
|
if (cache_it == bv.coordCache().getParPos().end())
|
2006-07-08 14:40:22 +00:00
|
|
|
|
return Point(-1, -1);
|
|
|
|
|
|
|
|
|
|
CoordCache::InnerParPosCache const & cache = cache_it->second;
|
2005-01-31 16:29:48 +00:00
|
|
|
|
CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
|
2004-11-30 01:59:49 +00:00
|
|
|
|
if (it == cache.end()) {
|
|
|
|
|
//lyxerr << "cursor out of view" << std::endl;
|
2005-02-08 13:18:05 +00:00
|
|
|
|
return Point(-1, -1);
|
2004-11-30 01:59:49 +00:00
|
|
|
|
}
|
2006-10-17 16:23:27 +00:00
|
|
|
|
Point p = coordOffset(bv, dit, boundary); // offset from outer paragraph
|
2004-11-30 01:59:49 +00:00
|
|
|
|
p.y_ += it->second.y_;
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this could be used elsewhere as well?
|
2007-01-27 09:04:36 +00:00
|
|
|
|
// FIXME: This does not work within mathed!
|
2004-11-30 01:59:49 +00:00
|
|
|
|
CurStatus status(BufferView const * bv, DocIterator const & dit)
|
|
|
|
|
{
|
2006-10-13 16:44:44 +00:00
|
|
|
|
CoordCache::InnerParPosCache const & cache =
|
|
|
|
|
bv->coordCache().getParPos().find(dit.bottom().text())->second;
|
2005-01-06 15:40:49 +00:00
|
|
|
|
|
2004-11-30 01:59:49 +00:00
|
|
|
|
if (cache.find(dit.bottom().pit()) != cache.end())
|
|
|
|
|
return CUR_INSIDE;
|
|
|
|
|
else if (dit.bottom().pit() < bv->anchor_ref())
|
|
|
|
|
return CUR_ABOVE;
|
|
|
|
|
else
|
|
|
|
|
return CUR_BELOW;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-22 11:41:22 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2005-06-25 15:57:15 +00:00
|
|
|
|
bool findNextInset(DocIterator & dit,
|
2005-04-26 11:12:20 +00:00
|
|
|
|
vector<InsetBase_code> const & codes,
|
2005-02-22 11:41:22 +00:00
|
|
|
|
string const & contents)
|
|
|
|
|
{
|
2005-06-25 15:57:15 +00:00
|
|
|
|
DocIterator tmpdit = dit;
|
2005-04-26 11:12:20 +00:00
|
|
|
|
|
2005-06-25 15:57:15 +00:00
|
|
|
|
while (tmpdit) {
|
|
|
|
|
InsetBase const * inset = tmpdit.nextInset();
|
2005-04-26 11:12:20 +00:00
|
|
|
|
if (inset
|
|
|
|
|
&& find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
|
2005-02-22 11:41:22 +00:00
|
|
|
|
&& (contents.empty() ||
|
|
|
|
|
static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
|
2005-06-25 15:57:15 +00:00
|
|
|
|
dit = tmpdit;
|
2005-02-22 11:41:22 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2005-06-25 15:57:15 +00:00
|
|
|
|
tmpdit.forwardInset();
|
2005-02-22 11:41:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-25 15:57:15 +00:00
|
|
|
|
} // namespace anon
|
2005-02-22 11:41:22 +00:00
|
|
|
|
|
|
|
|
|
|
2005-06-25 15:57:15 +00:00
|
|
|
|
bool findInset(DocIterator & dit, vector<InsetBase_code> const & codes,
|
2005-02-22 11:41:22 +00:00
|
|
|
|
bool same_content)
|
|
|
|
|
{
|
|
|
|
|
string contents;
|
2005-06-25 15:57:15 +00:00
|
|
|
|
DocIterator tmpdit = dit;
|
|
|
|
|
tmpdit.forwardInset();
|
2006-08-16 21:06:40 +00:00
|
|
|
|
if (!tmpdit)
|
|
|
|
|
return false;
|
2005-02-22 11:41:22 +00:00
|
|
|
|
|
|
|
|
|
if (same_content) {
|
2005-06-25 15:57:15 +00:00
|
|
|
|
InsetBase const * inset = tmpdit.nextInset();
|
2005-04-26 11:12:20 +00:00
|
|
|
|
if (inset
|
2005-02-22 11:41:22 +00:00
|
|
|
|
&& find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
|
|
|
|
|
contents = static_cast<InsetCommand const *>(inset)->getContents();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-25 15:57:15 +00:00
|
|
|
|
if (!findNextInset(tmpdit, codes, contents)) {
|
|
|
|
|
if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
|
|
|
|
|
tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
|
|
|
|
|
if (!findNextInset(tmpdit, codes, contents)) {
|
|
|
|
|
return false;
|
2005-05-31 10:20:42 +00:00
|
|
|
|
}
|
2005-06-25 15:57:15 +00:00
|
|
|
|
} else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-04-05 23:56:29 +00:00
|
|
|
|
|
2005-06-25 15:57:15 +00:00
|
|
|
|
dit = tmpdit;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void findInset(DocIterator & dit, InsetBase_code code, bool same_content)
|
|
|
|
|
{
|
|
|
|
|
findInset(dit, vector<InsetBase_code>(1, code), same_content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
|
|
|
|
|
bool same_content)
|
|
|
|
|
{
|
|
|
|
|
LCursor tmpcur = bv->cursor();
|
|
|
|
|
if (!findInset(tmpcur, codes, same_content)) {
|
2006-09-11 08:54:10 +00:00
|
|
|
|
bv->cursor().message(_("No more insets"));
|
2005-06-25 15:57:15 +00:00
|
|
|
|
return;
|
2005-02-22 11:41:22 +00:00
|
|
|
|
}
|
2005-04-26 11:12:20 +00:00
|
|
|
|
|
2005-02-22 11:41:22 +00:00
|
|
|
|
tmpcur.clearSelection();
|
|
|
|
|
bv->setCursor(tmpcur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
|
|
|
|
|
{
|
|
|
|
|
gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-30 01:59:49 +00:00
|
|
|
|
|
2003-12-10 09:45:32 +00:00
|
|
|
|
} // namespace bv_funcs
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|