some cursor movement improvements to MathTextInset

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6790 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2003-04-14 06:17:19 +00:00
parent c212ed81e9
commit 329471b466
8 changed files with 50 additions and 23 deletions

View File

@ -102,7 +102,7 @@ void MathCursor::pushRight(MathAtom & t)
bool MathCursor::popLeft() bool MathCursor::popLeft()
{ {
//cerr << "Leaving atom to the left\n"; //lyxerr << "Leaving atom to the left\n";
if (depth() <= 1) { if (depth() <= 1) {
if (depth() == 1) if (depth() == 1)
par()->notifyCursorLeaves(idx()); par()->notifyCursorLeaves(idx());
@ -116,7 +116,7 @@ bool MathCursor::popLeft()
bool MathCursor::popRight() bool MathCursor::popRight()
{ {
//cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n"; //lyxerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
if (depth() <= 1) { if (depth() <= 1) {
if (depth() == 1) if (depth() == 1)
par()->notifyCursorLeaves(idx()); par()->notifyCursorLeaves(idx());
@ -965,7 +965,10 @@ bool MathCursor::goUpDown(bool up)
} }
} }
// try current cell // try current cell for e.g. text insets
if (par()->idxUpDown2(idx(), pos(), up, targetx_))
return true;
//xarray().boundingBox(xlow, xhigh, ylow, yhigh); //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
//if (up) //if (up)
// yhigh = yo - 4; // yhigh = yo - 4;
@ -1022,6 +1025,7 @@ bool MathCursor::bruteFind
it.back().getPos(xo, yo); it.back().getPos(xo, yo);
if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) { if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
double d = (x - xo) * (x - xo) + (y - yo) * (y - yo); double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
//lyxerr << "x: " << x << " y: " << y << " d: " << endl;
// '<=' in order to take the last possible position // '<=' in order to take the last possible position
// this is important for clicking behind \sum in e.g. '\sum_i a' // this is important for clicking behind \sum in e.g. '\sum_i a'
if (d <= best_dist) { if (d <= best_dist) {
@ -1050,12 +1054,13 @@ void MathCursor::bruteFind2(int x, int y)
it.back().setPos(0); it.back().setPos(0);
MathIterator et = Cursor_; MathIterator et = Cursor_;
et.back().setPos(it.cell().size()); et.back().setPos(it.cell().size());
while (1) { for (int i = 0; ; ++i) {
int xo, yo; int xo, yo;
it.back().getPos(xo, yo); it.back().getPos(xo, yo);
double d = (x - xo) * (x - xo) + (y - yo) * (y - yo); double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
// '<=' in order to take the last possible position // '<=' in order to take the last possible position
// this is important for clicking behind \sum in e.g. '\sum_i a' // this is important for clicking behind \sum in e.g. '\sum_i a'
lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
if (d <= best_dist) { if (d <= best_dist) {
best_dist = d; best_dist = d;
Cursor_ = it; Cursor_ = it;

View File

@ -286,15 +286,15 @@ void MathArray::drawT(TextPainter & pain, int x, int y) const
int MathArray::pos2x(size_type pos) const int MathArray::pos2x(size_type pos) const
{ {
return pos2x(0, pos, 0); return pos2x(pos, 0);
} }
int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const int MathArray::pos2x(size_type pos, int glue) const
{ {
int x = 0; int x = 0;
size_type target = min(pos2, size()); size_type target = min(pos, size());
for (size_type i = pos1; i < target; ++i) { for (size_type i = 0; i < target; ++i) {
const_iterator it = begin() + i; const_iterator it = begin() + i;
if ((*it)->getChar() == ' ') if ((*it)->getChar() == ' ')
x += glue; x += glue;
@ -306,14 +306,13 @@ int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
MathArray::size_type MathArray::x2pos(int targetx) const MathArray::size_type MathArray::x2pos(int targetx) const
{ {
return x2pos(0, targetx, 0); return x2pos(targetx, 0);
} }
MathArray::size_type MathArray::x2pos(size_type startpos, int targetx, MathArray::size_type MathArray::x2pos(int targetx, int glue) const
int glue) const
{ {
const_iterator it = begin() + startpos; const_iterator it = begin();
int lastx = 0; int lastx = 0;
int currx = 0; int currx = 0;
for (; currx < targetx && it < end(); ++it) { for (; currx < targetx && it < end(); ++it) {
@ -322,7 +321,7 @@ MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
currx += glue; currx += glue;
currx += (*it)->width(); currx += (*it)->width();
} }
if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos) if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
--it; --it;
return it - begin(); return it - begin();
} }

View File

@ -128,11 +128,11 @@ public:
/// returns x coordinate of given position in the array /// returns x coordinate of given position in the array
int pos2x(size_type pos) const; int pos2x(size_type pos) const;
/// returns position of given x coordinate /// returns position of given x coordinate
int pos2x(size_type pos1, size_type pos2, int glue) const; int pos2x(size_type pos, int glue) const;
/// returns position of given x coordinate /// returns position of given x coordinate
size_type x2pos(int pos) const; size_type x2pos(int pos) const;
/// returns position of given x coordinate fstarting from a certain pos /// returns position of given x coordinate fstarting from a certain pos
size_type x2pos(size_type startpos, int targetx, int glue) const; size_type x2pos(int targetx, int glue) const;
/// returns distance of this cell to the point given by x and y /// returns distance of this cell to the point given by x and y
// assumes valid position and size cache // assumes valid position and size cache
int dist(int x, int y) const; int dist(int x, int y) const;

View File

@ -120,6 +120,12 @@ bool MathInset::idxUpDown(idx_type &, pos_type &, bool, int) const
} }
bool MathInset::idxUpDown2(idx_type &, pos_type &, bool, int) const
{
return false;
}
bool MathInset::idxFirst(idx_type &, pos_type &) const bool MathInset::idxFirst(idx_type &, pos_type &) const
{ {
return false; return false;

View File

@ -132,6 +132,9 @@ public:
/// Where should we go when we press the up or down cursor key? /// Where should we go when we press the up or down cursor key?
virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up, virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up,
int targetx) const; int targetx) const;
/// Where should we go when we press the up or down cursor key?
virtual bool idxUpDown2(idx_type & idx, pos_type & pos, bool up,
int targetx) const;
/// The left key /// The left key
virtual bool idxLeft(idx_type & idx, pos_type & pos) const; virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
/// The right key /// The right key

View File

@ -694,8 +694,10 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
cell->push_back(MathAtom(new MathCharInset(t.character()))); cell->push_back(MathAtom(new MathCharInset(t.character())));
} }
else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
cell->push_back(MathAtom(new MathCharInset(t.character()))); if (cell->empty() || cell->back()->getChar() != ' ')
cell->push_back(MathAtom(new MathCharInset(' ')));
}
else if (t.cat() == catParameter) { else if (t.cat() == catParameter) {
Token const & n = getToken(); Token const & n = getToken();
@ -1209,7 +1211,8 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
else { else {
MathAtom at = createMathInset(t.cs()); MathAtom at = createMathInset(t.cs());
MathInset::mode_type m = mode; MathInset::mode_type m = mode;
if (m == MathInset::UNDECIDED_MODE) //if (m == MathInset::UNDECIDED_MODE)
if (at->currentMode() != MathInset::UNDECIDED_MODE)
m = at->currentMode(); m = at->currentMode();
MathInset::idx_type start = 0; MathInset::idx_type start = 0;
// this fails on \bigg[...\bigg] // this fails on \bigg[...\bigg]

View File

@ -25,21 +25,23 @@ MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
} }
void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const void MathTextInset::getPos(idx_type /*idx*/, pos_type pos, int & x, int & y) const
{ {
idx_type const i = pos2row(pos); idx_type const i = pos2row(pos);
pos_type const p = pos - cache_.cellinfo_[i].begin_; pos_type const p = pos - cache_.cellinfo_[i].begin_;
cache_.getPos(i, p, x, y); cache_.getPos(i, p, x, y);
y = cache_.cell(i).yo();
} }
bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up, bool MathTextInset::idxUpDown2(idx_type &, pos_type & pos, bool up,
int /*targetx*/) const int /*targetx*/) const
{ {
// try to move only one screen row up or down if possible // try to move only one screen row up or down if possible
idx_type i = pos2row(pos); idx_type i = pos2row(pos);
//lyxerr << "\nMathTextInset::idxUpDown() i: " << i << endl;
MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i]; MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_); int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_);
if (up) { if (up) {
if (i == 0) if (i == 0)
return false; return false;
@ -50,7 +52,7 @@ bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up,
return false; return false;
} }
MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i]; MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
pos = cell(0).x2pos(cell2.begin_, x, cell2.glue_); pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_);
return true; return true;
} }
@ -60,6 +62,10 @@ void MathTextInset::metrics(MetricsInfo & mi) const
cell(0).metrics(mi); cell(0).metrics(mi);
// we do our own metrics fiddling // we do our own metrics fiddling
// save old positional information
int const old_xo = cache_.cell(0).xo();
int const old_yo = cache_.cell(0).yo();
// delete old cache // delete old cache
cache_ = MathGridInset(1, 0); cache_ = MathGridInset(1, 0);
@ -148,6 +154,11 @@ void MathTextInset::metrics(MetricsInfo & mi) const
cache_.metrics(mi); cache_.metrics(mi);
dim_ = cache_.dimensions(); dim_ = cache_.dimensions();
//lyxerr << "outer dim: " << dim_ << endl; //lyxerr << "outer dim: " << dim_ << endl;
// reset position cache
for (idx_type i = 0; i < cache_.nargs(); ++i)
cache_.cell(i).setXY(old_xo, old_yo);
} }

View File

@ -22,7 +22,7 @@ public:
void drawSelection(PainterInfo & pi, void drawSelection(PainterInfo & pi,
idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const; idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const;
/// moves cursor up or down /// moves cursor up or down
bool idxUpDown(idx_type &, pos_type & pos, bool up, int targetx) const; bool idxUpDown2(idx_type &, pos_type & pos, bool up, int targetx) const;
protected: protected:
/// row corresponding to given position /// row corresponding to given position
idx_type pos2row(pos_type pos) const; idx_type pos2row(pos_type pos) const;