2005-04-26 10:30:24 +00:00
|
|
|
// -*- C++ -*-
|
2007-04-26 04:41:58 +00:00
|
|
|
/* \file CoordCache.h
|
2005-04-26 10:30:24 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2005-04-26 10:30:24 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
#ifndef COORDCACHE_H
|
|
|
|
#define COORDCACHE_H
|
|
|
|
|
2006-10-14 12:39:18 +00:00
|
|
|
// It seems that MacOSX define the check macro.
|
|
|
|
#undef check
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
#include "Dimension.h"
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
#include <map>
|
|
|
|
|
2006-10-15 22:32:56 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
class Inset;
|
2007-04-26 16:06:39 +00:00
|
|
|
class MathData;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
void lyxbreaker(void const * data, const char * hint, int size);
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
struct Geometry {
|
|
|
|
Point pos;
|
|
|
|
Dimension dim;
|
2007-09-24 08:05:49 +00:00
|
|
|
|
|
|
|
bool covers(int x, int y) const
|
|
|
|
{
|
|
|
|
return x >= pos.x_
|
|
|
|
&& x <= pos.x_ + dim.wid
|
|
|
|
&& y >= pos.y_ - dim.asc
|
|
|
|
&& y <= pos.y_ + dim.des;
|
|
|
|
}
|
2007-09-24 13:47:03 +00:00
|
|
|
|
|
|
|
int squareDistance(int x, int y) const
|
|
|
|
{
|
|
|
|
int xx = 0;
|
|
|
|
int yy = 0;
|
|
|
|
|
|
|
|
if (x < pos.x_)
|
|
|
|
xx = pos.x_ - x;
|
|
|
|
else if (x > pos.x_ + dim.wid)
|
|
|
|
xx = x - pos.x_ - dim.wid;
|
|
|
|
|
|
|
|
if (y < pos.y_ - dim.asc)
|
|
|
|
yy = pos.y_ - dim.asc - y;
|
|
|
|
else if (y > pos.y_ + dim.des)
|
|
|
|
yy = y - pos.y_ - dim.des;
|
|
|
|
|
|
|
|
// Optimisation: We avoid to compute the sqrt on purpose.
|
|
|
|
return xx*xx + yy*yy;
|
|
|
|
}
|
2007-09-23 22:39:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
template <class T> class CoordCacheBase {
|
|
|
|
public:
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
data_.clear();
|
|
|
|
}
|
|
|
|
|
2007-09-16 10:36:57 +00:00
|
|
|
bool empty() const
|
2006-10-21 09:45:11 +00:00
|
|
|
{
|
|
|
|
return data_.empty();
|
|
|
|
}
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
void add(T const * thing, int x, int y)
|
|
|
|
{
|
2007-09-23 22:39:49 +00:00
|
|
|
data_[thing].pos = Point(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(T const * thing, Dimension const & dim)
|
|
|
|
{
|
Fix bug #7261: Assertion when moving up
There are two possibilities to add an Inset to the CoordCache:
- 1), use CoordCache::add(Inset * p, int x, int y),
- 2), use CoordCache::add(Inset * p, Dimension dim).
In the latter case, the x and y are happily initialized as 0.
Also, when one call CoordCache::has(Inset * p), it will happily
return true because the cache knows something about this inset.
In #7261, LyX asked for the (x,y) position of an Inset and
received (0,0) as an answer, although that is not the position
of the Inset.
By adding some defaults for x,y in case of initializing with
dim only will give an extra check whether the cache really
"has" the position object.
In the long run, we might want to split the cache for
dimensions and positions.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37968 a592a061-630c-0410-9148-cb99ea01b6c8
2011-03-19 17:54:21 +00:00
|
|
|
if (!has(thing))
|
|
|
|
data_[thing].pos = Point(-10000, -10000);
|
2007-09-23 22:39:49 +00:00
|
|
|
data_[thing].dim = dim;
|
|
|
|
}
|
|
|
|
|
2007-09-24 13:47:03 +00:00
|
|
|
Geometry const & geometry(T const * thing) const
|
|
|
|
{
|
|
|
|
check(thing, "geometry");
|
|
|
|
return data_.find(thing)->second;
|
|
|
|
}
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
Dimension const & dim(T const * thing) const
|
|
|
|
{
|
Fix bug #7261: Assertion when moving up
There are two possibilities to add an Inset to the CoordCache:
- 1), use CoordCache::add(Inset * p, int x, int y),
- 2), use CoordCache::add(Inset * p, Dimension dim).
In the latter case, the x and y are happily initialized as 0.
Also, when one call CoordCache::has(Inset * p), it will happily
return true because the cache knows something about this inset.
In #7261, LyX asked for the (x,y) position of an Inset and
received (0,0) as an answer, although that is not the position
of the Inset.
By adding some defaults for x,y in case of initializing with
dim only will give an extra check whether the cache really
"has" the position object.
In the long run, we might want to split the cache for
dimensions and positions.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37968 a592a061-630c-0410-9148-cb99ea01b6c8
2011-03-19 17:54:21 +00:00
|
|
|
checkDim(thing, "dim");
|
2007-09-23 22:39:49 +00:00
|
|
|
return data_.find(thing)->second.dim;
|
2004-08-14 14:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int x(T const * thing) const
|
|
|
|
{
|
|
|
|
check(thing, "x");
|
2007-09-23 22:39:49 +00:00
|
|
|
return data_.find(thing)->second.pos.x_;
|
2004-08-14 14:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int y(T const * thing) const
|
|
|
|
{
|
|
|
|
check(thing, "y");
|
2007-09-23 22:39:49 +00:00
|
|
|
return data_.find(thing)->second.pos.y_;
|
2004-08-14 14:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Point xy(T const * thing) const
|
|
|
|
{
|
|
|
|
check(thing, "xy");
|
2007-09-23 22:39:49 +00:00
|
|
|
return data_.find(thing)->second.pos;
|
2004-08-14 14:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool has(T const * thing) const
|
Fix bug #7261: Assertion when moving up
There are two possibilities to add an Inset to the CoordCache:
- 1), use CoordCache::add(Inset * p, int x, int y),
- 2), use CoordCache::add(Inset * p, Dimension dim).
In the latter case, the x and y are happily initialized as 0.
Also, when one call CoordCache::has(Inset * p), it will happily
return true because the cache knows something about this inset.
In #7261, LyX asked for the (x,y) position of an Inset and
received (0,0) as an answer, although that is not the position
of the Inset.
By adding some defaults for x,y in case of initializing with
dim only will give an extra check whether the cache really
"has" the position object.
In the long run, we might want to split the cache for
dimensions and positions.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37968 a592a061-630c-0410-9148-cb99ea01b6c8
2011-03-19 17:54:21 +00:00
|
|
|
{
|
|
|
|
typename cache_type::const_iterator it = data_.find(thing);
|
|
|
|
|
|
|
|
if (it == data_.end())
|
|
|
|
return false;
|
|
|
|
return it->second.pos.x_ != -10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasDim(T const * thing) const
|
2004-08-14 14:04:37 +00:00
|
|
|
{
|
|
|
|
return data_.find(thing) != data_.end();
|
|
|
|
}
|
|
|
|
|
2007-09-24 08:05:49 +00:00
|
|
|
bool covers(T const * thing, int x, int y) const
|
|
|
|
{
|
2007-09-24 17:43:29 +00:00
|
|
|
typename cache_type::const_iterator it = data_.find(thing);
|
2007-09-24 08:05:49 +00:00
|
|
|
return it != data_.end() && it->second.covers(x, y);
|
|
|
|
}
|
2004-08-14 21:56:40 +00:00
|
|
|
|
2007-09-24 13:47:03 +00:00
|
|
|
int squareDistance(T const * thing, int x, int y) const
|
|
|
|
{
|
2007-09-24 19:20:40 +00:00
|
|
|
typename cache_type::const_iterator it = data_.find(thing);
|
2007-09-24 13:47:03 +00:00
|
|
|
if (it == data_.end())
|
|
|
|
return 1000000;
|
|
|
|
return it->second.squareDistance(x, y);
|
|
|
|
}
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
private:
|
|
|
|
friend class CoordCache;
|
|
|
|
|
Fix bug #7261: Assertion when moving up
There are two possibilities to add an Inset to the CoordCache:
- 1), use CoordCache::add(Inset * p, int x, int y),
- 2), use CoordCache::add(Inset * p, Dimension dim).
In the latter case, the x and y are happily initialized as 0.
Also, when one call CoordCache::has(Inset * p), it will happily
return true because the cache knows something about this inset.
In #7261, LyX asked for the (x,y) position of an Inset and
received (0,0) as an answer, although that is not the position
of the Inset.
By adding some defaults for x,y in case of initializing with
dim only will give an extra check whether the cache really
"has" the position object.
In the long run, we might want to split the cache for
dimensions and positions.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37968 a592a061-630c-0410-9148-cb99ea01b6c8
2011-03-19 17:54:21 +00:00
|
|
|
void checkDim(T const * thing, char const * hint) const
|
|
|
|
{
|
|
|
|
if (!hasDim(thing))
|
|
|
|
lyxbreaker(thing, hint, data_.size());
|
|
|
|
}
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
void check(T const * thing, char const * hint) const
|
|
|
|
{
|
2004-11-30 01:59:49 +00:00
|
|
|
if (!has(thing))
|
2004-08-14 14:04:37 +00:00
|
|
|
lyxbreaker(thing, hint, data_.size());
|
|
|
|
}
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
typedef std::map<T const *, Geometry> cache_type;
|
2004-08-14 21:56:40 +00:00
|
|
|
cache_type data_;
|
2004-08-14 14:04:37 +00:00
|
|
|
};
|
|
|
|
|
2005-01-31 16:29:48 +00:00
|
|
|
/**
|
2007-09-24 08:05:49 +00:00
|
|
|
* A BufferView dependent cache that allows us to come from an inset in
|
|
|
|
* a document to a position point and dimension on the screen.
|
2005-01-31 16:29:48 +00:00
|
|
|
* All points cached in this cache are only valid between subsequent
|
2006-10-20 20:57:21 +00:00
|
|
|
* updates. (x,y) == (0,0) is the upper left screen corner, x increases
|
2005-01-31 16:29:48 +00:00
|
|
|
* to the right, y increases downwords.
|
2015-10-12 14:11:58 +00:00
|
|
|
* The dimension part is built in BufferView::updateMetrics() and the
|
2007-09-24 08:05:49 +00:00
|
|
|
* diverse Inset::metrics() calls.
|
|
|
|
* The individual points are added at drawing time in
|
2015-10-12 14:11:58 +00:00
|
|
|
* BufferView::draw(). The math inset position are cached in
|
2007-09-24 08:05:49 +00:00
|
|
|
* the diverse InsetMathXXX::draw() calls and the in-text inset position
|
|
|
|
* are cached in RowPainter::paintInset().
|
|
|
|
* FIXME: For mathed, it would be nice if the insets did not saves their
|
|
|
|
* position themselves. That should be the duty of the containing math
|
|
|
|
* array.
|
2005-01-31 16:29:48 +00:00
|
|
|
*/
|
2004-08-14 14:04:37 +00:00
|
|
|
class CoordCache {
|
|
|
|
public:
|
|
|
|
void clear();
|
|
|
|
|
2007-04-26 16:06:39 +00:00
|
|
|
/// A map from MathData to position on the screen
|
2015-10-12 14:11:58 +00:00
|
|
|
typedef CoordCacheBase<MathData> Arrays;
|
|
|
|
Arrays & arrays() { return arrays_; }
|
|
|
|
Arrays const & getArrays() const { return arrays_; }
|
2005-01-31 16:29:48 +00:00
|
|
|
/// A map from insets to positions on the screen
|
2015-10-12 14:11:58 +00:00
|
|
|
typedef CoordCacheBase<Inset> Insets;
|
|
|
|
Insets & insets() { return insets_; }
|
|
|
|
Insets const & getInsets() const { return insets_; }
|
2006-04-05 23:56:29 +00:00
|
|
|
|
2006-10-20 20:57:21 +00:00
|
|
|
/// Dump the contents of the cache to lyxerr in debugging form
|
|
|
|
void dump() const;
|
2005-01-31 16:29:48 +00:00
|
|
|
private:
|
2007-04-26 16:06:39 +00:00
|
|
|
/// MathDatas
|
2015-10-12 14:11:58 +00:00
|
|
|
Arrays arrays_;
|
2005-07-18 12:13:32 +00:00
|
|
|
// All insets
|
2015-10-12 14:11:58 +00:00
|
|
|
Insets insets_;
|
2004-08-14 14:04:37 +00:00
|
|
|
};
|
|
|
|
|
2006-10-15 22:32:56 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2004-08-14 14:04:37 +00:00
|
|
|
#endif
|