1999-09-27 18:44:28 +00:00
|
|
|
/*
|
2001-04-24 16:13:38 +00:00
|
|
|
* File: math_cursor.C
|
|
|
|
* Purpose: Interaction for mathed
|
|
|
|
* Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
|
|
|
|
* Created: January 1996
|
|
|
|
* Description: Math interaction for a WYSIWYG math editor.
|
|
|
|
*
|
|
|
|
* Dependencies: Xlib, XForms
|
|
|
|
*
|
|
|
|
* Copyright: 1996, Alejandro Aguilar Sierra
|
|
|
|
*
|
2001-06-25 00:06:33 +00:00
|
|
|
* Version: 0.8beta, Math & Lyx project.
|
2001-04-24 16:13:38 +00:00
|
|
|
*
|
|
|
|
* You are free to use and modify this code under the terms of
|
|
|
|
* the GNU General Public Licence version 2 or later.
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-12-18 03:16:46 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
2001-02-14 17:58:40 +00:00
|
|
|
|
2001-12-18 12:21:33 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/LAssert.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "LColor.h"
|
|
|
|
#include "Painter.h"
|
2001-12-18 03:16:46 +00:00
|
|
|
#include "math_cursor.h"
|
2001-07-17 07:38:41 +00:00
|
|
|
#include "formulabase.h"
|
|
|
|
#include "math_arrayinset.h"
|
2001-10-29 15:45:24 +00:00
|
|
|
#include "math_braceinset.h"
|
2001-12-18 12:21:33 +00:00
|
|
|
#include "math_boxinset.h"
|
2001-12-05 17:50:18 +00:00
|
|
|
#include "math_casesinset.h"
|
2001-08-09 09:19:18 +00:00
|
|
|
#include "math_charinset.h"
|
2001-07-10 13:17:43 +00:00
|
|
|
#include "math_deliminset.h"
|
2001-12-05 17:50:18 +00:00
|
|
|
#include "math_factory.h"
|
2001-11-08 12:28:33 +00:00
|
|
|
#include "math_hullinset.h"
|
2001-12-05 17:50:18 +00:00
|
|
|
#include "math_iterator.h"
|
|
|
|
#include "math_mathmlstream.h"
|
|
|
|
#include "math_parser.h"
|
|
|
|
#include "math_replace.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "math_scriptinset.h"
|
2001-08-13 14:02:37 +00:00
|
|
|
#include "math_spaceinset.h"
|
2001-08-30 08:55:13 +00:00
|
|
|
#include "math_specialcharinset.h"
|
2001-12-05 17:50:18 +00:00
|
|
|
#include "math_support.h"
|
2001-04-24 16:13:38 +00:00
|
|
|
|
2001-12-18 03:16:46 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
|
2001-09-26 15:20:45 +00:00
|
|
|
#define FILEDEBUG 0
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
2001-06-25 00:06:33 +00:00
|
|
|
using std::min;
|
|
|
|
using std::max;
|
2001-09-25 06:47:47 +00:00
|
|
|
using std::swap;
|
2001-07-06 12:09:32 +00:00
|
|
|
using std::isalnum;
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
struct Selection
|
|
|
|
{
|
2001-12-15 16:13:11 +00:00
|
|
|
typedef MathInset::col_type col_type;
|
|
|
|
typedef MathInset::row_type row_type;
|
|
|
|
typedef MathInset::idx_type idx_type;
|
|
|
|
|
|
|
|
Selection()
|
|
|
|
: data_(1, 1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void region(MathCursorPos const & i1, MathCursorPos const & i2,
|
|
|
|
row_type & r1, row_type & r2, col_type & c1, col_type & c2)
|
|
|
|
{
|
|
|
|
MathInset * p = i1.par_;
|
|
|
|
c1 = p->col(i1.idx_);
|
|
|
|
c2 = p->col(i2.idx_);
|
|
|
|
if (c1 > c2)
|
|
|
|
std::swap(c1, c2);
|
|
|
|
r1 = p->row(i1.idx_);
|
|
|
|
r2 = p->row(i2.idx_);
|
|
|
|
if (r1 > r2)
|
|
|
|
std::swap(r1, r2);
|
|
|
|
}
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
void grab(MathCursor const & cursor)
|
|
|
|
{
|
|
|
|
MathCursorPos i1;
|
|
|
|
MathCursorPos i2;
|
|
|
|
cursor.getSelection(i1, i2);
|
2001-12-15 16:13:11 +00:00
|
|
|
// shouldn'tt we assert on i1.par_ == i2.par_?
|
|
|
|
if (i1.idx_ == i2.idx_) {
|
|
|
|
data_ = MathGridInset(1, 1);
|
|
|
|
data_.cell(0) = MathArray(i1.cell(), i1.pos_, i2.pos_);
|
|
|
|
} else {
|
|
|
|
row_type r1, r2;
|
|
|
|
col_type c1, c2;
|
|
|
|
region(i1, i2, r1, r2, c1, c2);
|
|
|
|
data_ = MathGridInset(c2 - c1 + 1, r2 - r1 + 1);
|
|
|
|
for (row_type row = 0; row < data_.nrows(); ++row)
|
|
|
|
for (col_type col = 0; col < data_.ncols(); ++col) {
|
|
|
|
idx_type i = i1.par_->index(row + r1, col + c1);
|
|
|
|
data_.cell(data_.index(row, col)) = i1.par_->cell(i);
|
|
|
|
}
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void erase(MathCursor & cursor)
|
|
|
|
{
|
|
|
|
MathCursorPos i1;
|
|
|
|
MathCursorPos i2;
|
|
|
|
cursor.getSelection(i1, i2);
|
2001-10-25 14:46:57 +00:00
|
|
|
if (i1.idx_ == i2.idx_)
|
2001-07-16 15:53:25 +00:00
|
|
|
i1.cell().erase(i1.pos_, i2.pos_);
|
2001-10-25 14:46:57 +00:00
|
|
|
else {
|
2001-12-15 16:13:11 +00:00
|
|
|
MathInset * p = i1.par_;
|
|
|
|
row_type r1, r2;
|
|
|
|
col_type c1, c2;
|
|
|
|
region(i1, i2, r1, r2, c1, c2);
|
|
|
|
for (row_type row = r1; row <= r2; ++row)
|
|
|
|
for (col_type col = c1; col <= c2; ++col)
|
|
|
|
p->cell(p->index(row, col)).erase();
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
|
|
|
cursor.cursor() = i1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void paste(MathCursor & cursor) const
|
2002-01-02 13:43:13 +00:00
|
|
|
{
|
|
|
|
if (data_.nargs() == 1) {
|
|
|
|
// single cell/part of cell
|
|
|
|
cursor.insert(data_.cell(0));
|
|
|
|
} else {
|
|
|
|
// mulitple cells
|
|
|
|
idx_type idx;
|
|
|
|
MathGridInset * p = cursor.enclosingGrid(idx);
|
|
|
|
col_type const numcols = min(data_.ncols(), p->ncols() - p->col(idx));
|
|
|
|
row_type const numrows = min(data_.nrows(), p->nrows() - p->row(idx));
|
|
|
|
for (row_type row = 0; row < numrows; ++row)
|
|
|
|
for (col_type col = 0; col < numcols; ++col) {
|
|
|
|
idx_type i = p->index(row + p->row(idx), col + p->col(idx));
|
|
|
|
p->cell(i).push_back(data_.cell(data_.index(row, col)));
|
|
|
|
}
|
|
|
|
}
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// glues selection to one cell
|
|
|
|
MathArray glue() const
|
|
|
|
{
|
|
|
|
MathArray ar;
|
2001-12-15 16:13:11 +00:00
|
|
|
for (unsigned i = 0; i < data_.nargs(); ++i)
|
|
|
|
ar.push_back(data_.cell(i));
|
2001-07-16 15:53:25 +00:00
|
|
|
return ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2001-12-15 16:13:11 +00:00
|
|
|
data_ = MathGridInset(1, 1);
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
2001-12-15 16:13:11 +00:00
|
|
|
MathGridInset data_;
|
2001-07-16 15:53:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
Selection theSelection;
|
2001-07-16 15:53:25 +00:00
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathCursor::MathCursor(InsetFormulaBase * formula, bool left)
|
2001-08-14 11:19:19 +00:00
|
|
|
: formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
left ? first() : last();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
void MathCursor::push(MathAtom & t)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
Cursor_.push_back(MathCursorPos(t.nucleus()));
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
void MathCursor::pushLeft(MathAtom & t)
|
|
|
|
{
|
|
|
|
//cerr << "Entering atom "; t->write(cerr, false); cerr << " left\n";
|
|
|
|
push(t);
|
|
|
|
t->idxFirst(idx(), pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathCursor::pushRight(MathAtom & t)
|
2001-08-08 07:12:09 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
//cerr << "Entering atom "; t->write(cerr, false); cerr << " right\n";
|
2001-08-08 07:46:16 +00:00
|
|
|
posLeft();
|
2001-10-12 12:02:49 +00:00
|
|
|
push(t);
|
|
|
|
t->idxLast(idx(), pos());
|
2001-08-08 07:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MathCursor::popLeft()
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
//cerr << "Leaving atom "; par()->write(cerr, false); cerr << " left\n";
|
2001-07-16 15:53:25 +00:00
|
|
|
if (Cursor_.size() <= 1)
|
2001-06-27 14:10:35 +00:00
|
|
|
return false;
|
2001-12-12 13:11:28 +00:00
|
|
|
if (par()->asScriptInset())
|
|
|
|
par()->asScriptInset()->removeEmptyScripts();
|
2001-07-16 15:53:25 +00:00
|
|
|
Cursor_.pop_back();
|
2001-06-27 14:10:35 +00:00
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-09-11 10:58:17 +00:00
|
|
|
|
2001-08-08 07:12:09 +00:00
|
|
|
bool MathCursor::popRight()
|
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
//cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
|
2001-08-08 07:12:09 +00:00
|
|
|
if (Cursor_.size() <= 1)
|
|
|
|
return false;
|
2001-12-12 13:11:28 +00:00
|
|
|
if (par()->asScriptInset())
|
|
|
|
par()->asScriptInset()->removeEmptyScripts();
|
2001-08-08 07:12:09 +00:00
|
|
|
Cursor_.pop_back();
|
2001-08-08 07:46:16 +00:00
|
|
|
posRight();
|
2001-08-08 07:12:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-09-26 15:20:45 +00:00
|
|
|
#if FILEDEBUG
|
2001-12-05 17:50:18 +00:00
|
|
|
void MathCursor::dump(char const * what) const
|
|
|
|
{
|
|
|
|
lyxerr << "MC: " << what << "\n";
|
|
|
|
lyxerr << " Cursor: " << Cursor_.size() << "\n";
|
|
|
|
for (unsigned i = 0; i < Cursor_.size(); ++i)
|
|
|
|
lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
|
|
|
|
lyxerr << " Anchor: " << Anchor_.size() << "\n";
|
|
|
|
for (unsigned i = 0; i < Anchor_.size(); ++i)
|
|
|
|
lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
|
|
|
|
lyxerr << " sel: " << selection_ << "\n";
|
|
|
|
}
|
2001-09-26 15:20:45 +00:00
|
|
|
#else
|
2001-12-05 17:50:18 +00:00
|
|
|
void MathCursor::dump(char const *) const {}
|
2001-09-26 15:20:45 +00:00
|
|
|
#endif
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-10-24 19:09:31 +00:00
|
|
|
UpdatableInset * MathCursor::asHyperActiveInset() const
|
|
|
|
{
|
|
|
|
return par()->asHyperActiveInset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 17:20:26 +00:00
|
|
|
bool MathCursor::isInside(MathInset const * p) const
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-07-16 15:53:25 +00:00
|
|
|
for (unsigned i = 0; i < Cursor_.size(); ++i)
|
2001-12-11 15:35:18 +00:00
|
|
|
if (Cursor_[i].par_ == p)
|
2001-06-25 00:06:33 +00:00
|
|
|
return true;
|
2001-07-16 15:53:25 +00:00
|
|
|
return false;
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
bool MathCursor::openable(MathAtom const & t, bool sel) const
|
2001-07-16 15:53:25 +00:00
|
|
|
{
|
2001-10-24 18:29:55 +00:00
|
|
|
if (t->isHyperActive())
|
|
|
|
return true;
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (!t->isActive())
|
2001-07-16 15:53:25 +00:00
|
|
|
return false;
|
2001-08-07 12:02:21 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (t->asScriptInset())
|
2001-08-20 13:45:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (sel) {
|
|
|
|
// we can't move into anything new during selection
|
|
|
|
if (Cursor_.size() == Anchor_.size())
|
|
|
|
return false;
|
2001-12-11 15:35:18 +00:00
|
|
|
if (t.nucleus() != Anchor_[Cursor_.size()].par_)
|
2001-08-20 13:45:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-08 07:46:16 +00:00
|
|
|
bool MathCursor::posLeft()
|
2001-07-13 14:54:56 +00:00
|
|
|
{
|
2001-08-08 07:46:16 +00:00
|
|
|
if (pos() == 0)
|
|
|
|
return false;
|
|
|
|
--pos();
|
|
|
|
return true;
|
2001-07-13 14:54:56 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
|
2001-08-08 07:46:16 +00:00
|
|
|
bool MathCursor::posRight()
|
2001-08-07 12:02:21 +00:00
|
|
|
{
|
2001-08-08 07:46:16 +00:00
|
|
|
if (pos() == size())
|
|
|
|
return false;
|
2001-10-12 12:02:49 +00:00
|
|
|
++pos();
|
2001-08-08 07:46:16 +00:00
|
|
|
return true;
|
2001-08-07 12:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
bool MathCursor::left(bool sel)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
dump("Left 1");
|
2001-08-14 11:19:19 +00:00
|
|
|
if (inMacroMode()) {
|
|
|
|
macroModeClose();
|
2001-08-15 05:50:39 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-02-14 17:58:40 +00:00
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2001-07-26 09:01:36 +00:00
|
|
|
selHandle(sel);
|
2001-08-13 14:02:37 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-02-14 17:58:40 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (hasPrevAtom() && openable(prevAtom(), sel)) {
|
2001-10-24 18:29:55 +00:00
|
|
|
if (prevAtom()->isHyperActive()) {
|
|
|
|
lyxerr << "entering hyperactive inset\n";
|
|
|
|
}
|
2001-10-12 12:02:49 +00:00
|
|
|
pushRight(prevAtom());
|
2001-07-16 15:53:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2001-10-15 14:19:16 +00:00
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
return posLeft() || idxLeft() || popLeft() || selection_;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
bool MathCursor::right(bool sel)
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
dump("Right 1");
|
2001-08-14 11:19:19 +00:00
|
|
|
if (inMacroMode()) {
|
2001-07-26 09:01:36 +00:00
|
|
|
macroModeClose();
|
2001-08-15 05:50:39 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-02-14 17:58:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2001-07-26 09:01:36 +00:00
|
|
|
selHandle(sel);
|
2001-08-13 14:02:37 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-02-14 17:58:40 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (hasNextAtom() && openable(nextAtom(), sel)) {
|
2001-10-24 18:29:55 +00:00
|
|
|
if (nextAtom()->isHyperActive()) {
|
|
|
|
lyxerr << "entering hyperactive inset\n";
|
2001-10-24 19:09:31 +00:00
|
|
|
int x, y;
|
|
|
|
getPos(x, y);
|
|
|
|
nextAtom()->edit(formula()->view(), x, y, 0);
|
2001-10-24 18:29:55 +00:00
|
|
|
}
|
2001-10-12 12:02:49 +00:00
|
|
|
pushLeft(nextAtom());
|
2001-07-16 15:53:25 +00:00
|
|
|
return true;
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
2001-08-08 07:46:16 +00:00
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
return posRight() || idxRight() || popRight() || selection_;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathCursor::first()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-07-16 15:53:25 +00:00
|
|
|
Cursor_.clear();
|
2001-10-12 12:02:49 +00:00
|
|
|
pushLeft(formula_->par());
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
2001-02-14 17:58:40 +00:00
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathCursor::last()
|
|
|
|
{
|
2001-08-08 07:12:09 +00:00
|
|
|
first();
|
|
|
|
end();
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 18:51:47 +00:00
|
|
|
bool positionable(MathCursor::cursor_type const & cursor,
|
|
|
|
MathCursor::cursor_type const & anchor)
|
|
|
|
{
|
|
|
|
// avoid deeper nested insets when selecting
|
|
|
|
if (cursor.size() > anchor.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// anchor might be deeper, should have same path then
|
|
|
|
for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i)
|
|
|
|
if (cursor[i].par_ != anchor[i].par_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// position should be ok.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-12-05 17:50:18 +00:00
|
|
|
|
|
|
|
void MathCursor::setPos(int x, int y)
|
|
|
|
{
|
2001-12-17 10:25:17 +00:00
|
|
|
dump("setPos 1");
|
2001-12-12 14:05:38 +00:00
|
|
|
bool res = bruteFind(x, y,
|
|
|
|
formula()->xlow(), formula()->xhigh(),
|
|
|
|
formula()->ylow(), formula()->yhigh());
|
2001-12-17 10:25:17 +00:00
|
|
|
if (!res) {
|
|
|
|
// this ccan happen on creation of "math-display"
|
|
|
|
dump("setPos 1.5");
|
|
|
|
first();
|
|
|
|
}
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("setPos 2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-08-30 10:17:28 +00:00
|
|
|
void MathCursor::home(bool sel)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
dump("home 1");
|
2001-08-30 10:17:28 +00:00
|
|
|
selHandle(sel);
|
2001-07-26 09:01:36 +00:00
|
|
|
macroModeClose();
|
2001-08-13 14:02:37 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-08-07 15:04:57 +00:00
|
|
|
if (!par()->idxHome(idx(), pos()))
|
2001-08-08 07:12:09 +00:00
|
|
|
popLeft();
|
2001-07-26 09:01:36 +00:00
|
|
|
dump("home 2");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-08-30 10:17:28 +00:00
|
|
|
void MathCursor::end(bool sel)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
dump("end 1");
|
2001-08-30 10:17:28 +00:00
|
|
|
selHandle(sel);
|
2001-07-26 09:01:36 +00:00
|
|
|
macroModeClose();
|
2001-08-13 14:02:37 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-08-08 07:12:09 +00:00
|
|
|
if (!par()->idxEnd(idx(), pos()))
|
|
|
|
popRight();
|
2001-07-26 09:01:36 +00:00
|
|
|
dump("end 2");
|
2001-02-03 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
2001-02-13 19:10:18 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::plainErase()
|
2001-07-26 06:46:50 +00:00
|
|
|
{
|
2001-08-07 15:04:57 +00:00
|
|
|
array().erase(pos());
|
2001-07-26 06:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
void MathCursor::plainInsert(MathAtom const & t)
|
2001-10-10 13:20:51 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
array().insert(pos(), t);
|
2001-10-10 13:20:51 +00:00
|
|
|
++pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
void MathCursor::insert(char c, MathTextCodes t)
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-07-10 13:17:43 +00:00
|
|
|
//lyxerr << "inserting '" << c << "'\n";
|
2001-10-12 12:02:49 +00:00
|
|
|
plainInsert(MathAtom(new MathCharInset(c, t)));
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
void MathCursor::insert(MathAtom const & t)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
macroModeClose();
|
2001-02-14 17:58:40 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (selection_) {
|
|
|
|
if (t->nargs())
|
2001-07-26 09:01:36 +00:00
|
|
|
selCut();
|
2001-06-25 00:06:33 +00:00
|
|
|
else
|
2001-07-26 09:01:36 +00:00
|
|
|
selDel();
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
plainInsert(t);
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
void MathCursor::niceInsert(MathAtom const & t)
|
2001-08-13 14:02:37 +00:00
|
|
|
{
|
|
|
|
selCut();
|
2001-10-12 12:02:49 +00:00
|
|
|
insert(t); // inserting invalidates the pointer!
|
|
|
|
MathAtom const & p = prevAtom();
|
2001-08-13 14:02:37 +00:00
|
|
|
if (p->nargs()) {
|
|
|
|
posLeft();
|
|
|
|
right(); // do not push for e.g. MathSymbolInset
|
|
|
|
selPaste();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
void MathCursor::insert(MathArray const & ar)
|
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
macroModeClose();
|
|
|
|
if (selection_)
|
|
|
|
selCut();
|
2001-07-16 15:53:25 +00:00
|
|
|
|
2001-08-07 15:04:57 +00:00
|
|
|
array().insert(pos(), ar);
|
|
|
|
pos() += ar.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
void MathCursor::paste(MathArray const & ar)
|
2001-10-10 13:20:51 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
Anchor_ = Cursor_;
|
|
|
|
selection_ = true;
|
|
|
|
array().insert(pos(), ar);
|
|
|
|
pos() += ar.size();
|
2001-10-10 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-07 15:04:57 +00:00
|
|
|
void MathCursor::backspace()
|
|
|
|
{
|
2001-10-10 13:20:51 +00:00
|
|
|
if (pos() == 0) {
|
2001-10-10 16:35:18 +00:00
|
|
|
pullArg(false);
|
2001-10-10 13:20:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (selection_) {
|
|
|
|
selDel();
|
2001-08-07 15:04:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathScriptInset * p = prevAtom()->asScriptInset();
|
|
|
|
if (p) {
|
|
|
|
p->removeScript(p->hasUp());
|
|
|
|
// Don't delete if there is anything left
|
|
|
|
if (p->hasUp() || p->hasDown())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-10 13:20:51 +00:00
|
|
|
--pos();
|
|
|
|
plainErase();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::erase()
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-08-14 11:19:19 +00:00
|
|
|
if (inMacroMode())
|
2001-02-14 17:58:40 +00:00
|
|
|
return;
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
|
|
|
selDel();
|
2001-02-14 17:58:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-07-09 16:59:57 +00:00
|
|
|
// delete empty cells if necessary
|
2001-10-10 13:20:51 +00:00
|
|
|
if (array().empty()) {
|
2001-07-09 16:59:57 +00:00
|
|
|
bool popit;
|
|
|
|
bool removeit;
|
2001-08-07 15:04:57 +00:00
|
|
|
par()->idxDelete(idx(), popit, removeit);
|
2001-08-08 07:12:09 +00:00
|
|
|
if (popit && popLeft() && removeit)
|
2001-07-26 09:01:36 +00:00
|
|
|
plainErase();
|
2001-07-23 14:21:34 +00:00
|
|
|
return;
|
2001-06-27 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
if (pos() == size())
|
2001-09-24 16:25:06 +00:00
|
|
|
return;
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathScriptInset * p = nextAtom()->asScriptInset();
|
|
|
|
if (p) {
|
|
|
|
p->removeScript(p->hasUp());
|
|
|
|
// Don't delete if there is anything left
|
|
|
|
if (p->hasUp() || p->hasDown())
|
|
|
|
return;
|
2001-09-24 16:25:06 +00:00
|
|
|
}
|
2001-07-23 14:21:34 +00:00
|
|
|
|
2001-09-24 16:25:06 +00:00
|
|
|
plainErase();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::delLine()
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
macroModeClose();
|
2001-02-14 17:58:40 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
|
|
|
selDel();
|
2001-02-14 17:58:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-11 15:04:02 +00:00
|
|
|
if (par()->nrows() > 1) {
|
|
|
|
// grid are the only things with more than one row...
|
|
|
|
lyx::Assert(par()->asGridInset());
|
|
|
|
par()->asGridInset()->delRow(hullRow());
|
|
|
|
}
|
2001-10-23 13:48:52 +00:00
|
|
|
|
2001-11-28 13:09:40 +00:00
|
|
|
if (idx() > par()->nargs())
|
|
|
|
idx() = par()->nargs();
|
|
|
|
|
2001-10-23 13:48:52 +00:00
|
|
|
if (pos() > size())
|
|
|
|
pos() = size();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
bool MathCursor::up(bool sel)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
dump("up 1");
|
|
|
|
macroModeClose();
|
|
|
|
selHandle(sel);
|
2001-12-11 18:51:47 +00:00
|
|
|
cursor_type save = Cursor_;
|
|
|
|
if (goUpDown(true))
|
|
|
|
return true;
|
|
|
|
Cursor_ = save;
|
|
|
|
return selection_;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
bool MathCursor::down(bool sel)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
dump("down 1");
|
|
|
|
macroModeClose();
|
|
|
|
selHandle(sel);
|
2001-12-11 18:51:47 +00:00
|
|
|
cursor_type save = Cursor_;
|
|
|
|
if (goUpDown(false))
|
|
|
|
return true;
|
|
|
|
Cursor_ = save;
|
|
|
|
return selection_;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathCursor::toggleLimits()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-11-27 13:36:23 +00:00
|
|
|
if (!hasNextAtom())
|
2001-10-12 12:02:49 +00:00
|
|
|
return false;
|
2001-11-27 13:36:23 +00:00
|
|
|
MathScriptInset * t = nextAtom()->asScriptInset();
|
2001-09-11 10:58:17 +00:00
|
|
|
if (!t)
|
2001-08-06 17:20:26 +00:00
|
|
|
return false;
|
2001-09-11 10:58:17 +00:00
|
|
|
int old = t->limits();
|
|
|
|
t->limits(old < 0 ? 1 : -1);
|
|
|
|
return old != t->limits();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-14 11:19:19 +00:00
|
|
|
void MathCursor::macroModeClose()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-14 11:19:19 +00:00
|
|
|
string s = macroName();
|
|
|
|
if (s.size()) {
|
2001-10-10 16:35:18 +00:00
|
|
|
size_type old = pos();
|
|
|
|
pos() -= s.size();
|
|
|
|
array().erase(pos(), old);
|
|
|
|
interpret(s);
|
2001-08-14 11:19:19 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
int MathCursor::macroNamePos() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-10 16:35:18 +00:00
|
|
|
for (int i = pos() - 1; i >= 0; --i) {
|
2001-10-12 12:02:49 +00:00
|
|
|
MathAtom & p = array().at(i);
|
|
|
|
if (p->code() == LM_TC_TEX && p->getChar() == '\\')
|
2001-10-10 16:35:18 +00:00
|
|
|
return i;
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
2001-10-10 16:35:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string MathCursor::macroName() const
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
for (int i = macroNamePos(); i >= 0 && i < int(pos()); ++i)
|
2001-10-12 12:02:49 +00:00
|
|
|
s += array().at(i)->getChar();
|
2001-08-14 11:19:19 +00:00
|
|
|
return s;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selCopy()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selCopy");
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
2001-07-16 15:53:25 +00:00
|
|
|
theSelection.grab(*this);
|
2001-07-26 09:01:36 +00:00
|
|
|
selClear();
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selCut()
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selCut");
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
2001-07-16 15:53:25 +00:00
|
|
|
theSelection.grab(*this);
|
|
|
|
theSelection.erase(*this);
|
2001-08-07 13:23:16 +00:00
|
|
|
selClear();
|
|
|
|
} else {
|
|
|
|
theSelection.clear();
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selDel()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selDel");
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
2001-07-16 15:53:25 +00:00
|
|
|
theSelection.erase(*this);
|
2001-10-24 16:10:38 +00:00
|
|
|
if (pos() > size())
|
|
|
|
pos() = size();
|
2001-07-26 09:01:36 +00:00
|
|
|
selClear();
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selPaste()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selPaste");
|
2001-07-16 15:53:25 +00:00
|
|
|
theSelection.paste(*this);
|
2001-11-29 16:13:23 +00:00
|
|
|
//theSelection.grab(*this);
|
2001-10-12 12:02:49 +00:00
|
|
|
//selClear();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selHandle(bool sel)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-07 13:23:16 +00:00
|
|
|
if (sel == selection_)
|
|
|
|
return;
|
2001-11-29 16:39:44 +00:00
|
|
|
//theSelection.clear();
|
2001-08-07 13:23:16 +00:00
|
|
|
Anchor_ = Cursor_;
|
|
|
|
selection_ = sel;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-13 00:05:34 +00:00
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selStart()
|
2001-02-14 17:58:40 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selStart 1");
|
2001-11-29 16:39:44 +00:00
|
|
|
//theSelection.clear();
|
2001-07-16 15:53:25 +00:00
|
|
|
Anchor_ = Cursor_;
|
2001-07-26 09:01:36 +00:00
|
|
|
selection_ = true;
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selStart 2");
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::selClear()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selClear 1");
|
2001-07-26 09:01:36 +00:00
|
|
|
selection_ = false;
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selClear 2");
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2001-10-23 13:48:52 +00:00
|
|
|
void MathCursor::selGet(MathArray & ar)
|
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
dump("selGet");
|
2001-10-24 09:16:06 +00:00
|
|
|
if (!selection_)
|
2001-10-23 13:48:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
theSelection.grab(*this);
|
|
|
|
ar = theSelection.glue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
void MathCursor::drawSelection(Painter & pain) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
if (!selection_)
|
2001-02-14 17:58:40 +00:00
|
|
|
return;
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
MathCursorPos i1;
|
|
|
|
MathCursorPos i2;
|
|
|
|
getSelection(i1, i2);
|
|
|
|
|
|
|
|
if (i1.idx_ == i2.idx_) {
|
|
|
|
MathXArray & c = i1.xcell();
|
2001-10-12 12:02:49 +00:00
|
|
|
int x1 = c.xo() + c.pos2x(i1.pos_);
|
2001-07-16 15:53:25 +00:00
|
|
|
int y1 = c.yo() - c.ascent();
|
2001-10-12 12:02:49 +00:00
|
|
|
int x2 = c.xo() + c.pos2x(i2.pos_);
|
2001-07-16 15:53:25 +00:00
|
|
|
int y2 = c.yo() + c.descent();
|
|
|
|
pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
|
|
|
|
} else {
|
2001-10-12 12:02:49 +00:00
|
|
|
std::vector<MathInset::idx_type> indices
|
2001-12-11 15:35:18 +00:00
|
|
|
= i1.par_->idxBetween(i1.idx_, i2.idx_);
|
2001-07-16 15:53:25 +00:00
|
|
|
for (unsigned i = 0; i < indices.size(); ++i) {
|
|
|
|
MathXArray & c = i1.xcell(indices[i]);
|
|
|
|
int x1 = c.xo();
|
|
|
|
int y1 = c.yo() - c.ascent();
|
|
|
|
int x2 = c.xo() + c.width();
|
|
|
|
int y2 = c.yo() + c.descent();
|
|
|
|
pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
|
|
|
|
}
|
2001-02-14 17:58:40 +00:00
|
|
|
}
|
2001-10-29 15:45:24 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// draw anchor if different from selection boundary
|
|
|
|
MathCursorPos anc = Anchor_.back();
|
|
|
|
if (anc != i1 && anc != i2) {
|
|
|
|
MathXArray & c = anc.xcell();
|
|
|
|
int x = c.xo() + c.pos2x(anc.pos_);
|
|
|
|
int y1 = c.yo() - c.ascent();
|
|
|
|
int y2 = c.yo() + c.descent();
|
2001-12-03 16:24:50 +00:00
|
|
|
pain.line(x, y1, x, y2, LColor::math);
|
2001-10-29 15:45:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2001-07-03 07:56:55 +00:00
|
|
|
void MathCursor::handleFont(MathTextCodes t)
|
|
|
|
{
|
2001-08-13 14:02:37 +00:00
|
|
|
macroModeClose();
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
2001-07-16 15:53:25 +00:00
|
|
|
MathCursorPos i1;
|
|
|
|
MathCursorPos i2;
|
|
|
|
getSelection(i1, i2);
|
|
|
|
if (i1.idx_ == i2.idx_) {
|
|
|
|
MathArray & ar = i1.cell();
|
2001-10-12 12:02:49 +00:00
|
|
|
for (MathInset::pos_type pos = i1.pos_; pos != i2.pos_; ++pos)
|
|
|
|
ar.at(pos)->handleFont(t);
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
2001-07-26 09:01:36 +00:00
|
|
|
} else
|
|
|
|
lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
|
2001-01-09 13:58:48 +00:00
|
|
|
}
|
2001-02-13 19:10:18 +00:00
|
|
|
|
|
|
|
|
2001-09-12 12:51:55 +00:00
|
|
|
void MathCursor::handleDelim(string const & l, string const & r)
|
2001-07-10 13:17:43 +00:00
|
|
|
{
|
2001-08-08 07:46:16 +00:00
|
|
|
handleNest(new MathDelimInset(l, r));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathCursor::handleNest(MathInset * p)
|
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
if (selection_) {
|
|
|
|
selCut();
|
2001-07-16 15:53:25 +00:00
|
|
|
p->cell(0) = theSelection.glue();
|
2001-07-10 13:17:43 +00:00
|
|
|
}
|
2001-10-12 12:02:49 +00:00
|
|
|
insert(MathAtom(p)); // this invalidates p!
|
|
|
|
pushRight(prevAtom());
|
2001-07-10 13:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
void MathCursor::getPos(int & x, int & y)
|
2001-02-13 19:10:18 +00:00
|
|
|
{
|
2001-08-08 09:31:36 +00:00
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
#warning This should probably take cellXOffset and cellYOffset into account
|
|
|
|
#endif
|
2001-10-12 12:02:49 +00:00
|
|
|
x = xarray().xo() + xarray().pos2x(pos());
|
2001-06-25 00:06:33 +00:00
|
|
|
y = xarray().yo();
|
2001-02-13 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
MathInset * MathCursor::par() const
|
2001-02-13 19:10:18 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
return cursor().par_;
|
2001-02-13 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
InsetFormulaBase * MathCursor::formula()
|
2001-02-13 19:10:18 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return formula_;
|
2001-02-13 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::idx_type MathCursor::idx() const
|
2001-08-07 15:04:57 +00:00
|
|
|
{
|
|
|
|
return cursor().idx_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::idx_type & MathCursor::idx()
|
2001-08-07 15:04:57 +00:00
|
|
|
{
|
|
|
|
return cursor().idx_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::pos_type MathCursor::pos() const
|
2001-02-13 19:10:18 +00:00
|
|
|
{
|
2001-07-16 15:53:25 +00:00
|
|
|
return cursor().pos_;
|
2001-02-13 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::pos_type & MathCursor::pos()
|
2001-08-07 15:04:57 +00:00
|
|
|
{
|
|
|
|
return cursor().pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
bool MathCursor::inMacroMode() const
|
2001-02-13 19:10:18 +00:00
|
|
|
{
|
2001-10-10 16:35:18 +00:00
|
|
|
return macroNamePos() != -1;
|
2001-02-13 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-26 09:01:36 +00:00
|
|
|
bool MathCursor::selection() const
|
2001-02-13 19:10:18 +00:00
|
|
|
{
|
2001-07-26 09:01:36 +00:00
|
|
|
return selection_;
|
2001-02-13 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 08:21:03 +00:00
|
|
|
MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-07-16 15:53:25 +00:00
|
|
|
for (int i = Cursor_.size() - 1; i >= 0; --i) {
|
2001-12-11 15:35:18 +00:00
|
|
|
MathGridInset * p = Cursor_[i].par_->asGridInset();
|
2001-10-12 12:02:49 +00:00
|
|
|
if (p) {
|
2001-07-16 15:53:25 +00:00
|
|
|
idx = Cursor_[i].idx_;
|
2001-10-12 12:02:49 +00:00
|
|
|
return p;
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-07-23 14:15:14 +00:00
|
|
|
void MathCursor::pullArg(bool goright)
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-07-23 13:52:48 +00:00
|
|
|
dump("pullarg");
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray a = array();
|
2001-10-10 16:35:18 +00:00
|
|
|
|
|
|
|
MathScriptInset const * p = par()->asScriptInset();
|
|
|
|
if (p) {
|
|
|
|
// special handling for scripts
|
2001-10-12 12:02:49 +00:00
|
|
|
const bool up = p->hasUp();
|
2001-10-10 16:35:18 +00:00
|
|
|
popLeft();
|
2001-10-12 12:02:49 +00:00
|
|
|
MathScriptInset * q = nextAtom()->asScriptInset();
|
|
|
|
if (q)
|
|
|
|
q->removeScript(up);
|
2001-10-10 16:35:18 +00:00
|
|
|
++pos();
|
|
|
|
array().insert(pos(), a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-08-08 07:12:09 +00:00
|
|
|
if (popLeft()) {
|
2001-07-26 09:01:36 +00:00
|
|
|
plainErase();
|
2001-08-07 15:04:57 +00:00
|
|
|
array().insert(pos(), a);
|
2001-07-23 14:15:14 +00:00
|
|
|
if (goright)
|
2001-08-07 15:04:57 +00:00
|
|
|
pos() += a.size();
|
2001-07-23 13:52:48 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-18 12:21:33 +00:00
|
|
|
void MathCursor::normalize()
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-12-18 12:21:33 +00:00
|
|
|
// rebreak
|
|
|
|
{
|
|
|
|
MathIterator it = ibegin(formula()->par().nucleus());
|
|
|
|
MathIterator et = iend(formula()->par().nucleus());
|
|
|
|
for ( ; it != et; ++it)
|
|
|
|
if (it.par()->asBoxInset())
|
|
|
|
it.par()->asBoxInset()->rebreak();
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-08-08 07:12:09 +00:00
|
|
|
if (idx() >= par()->nargs()) {
|
2001-09-26 15:20:45 +00:00
|
|
|
lyxerr << "this should not really happen - 1: "
|
2001-08-08 07:12:09 +00:00
|
|
|
<< idx() << " " << par()->nargs() << "\n";
|
|
|
|
dump("error 2");
|
|
|
|
}
|
2001-12-18 12:21:33 +00:00
|
|
|
idx() = min(idx(), par()->nargs() - 1);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-08-08 07:46:16 +00:00
|
|
|
if (pos() > size()) {
|
2001-09-26 15:20:45 +00:00
|
|
|
lyxerr << "this should not really happen - 2: "
|
2001-12-18 12:21:33 +00:00
|
|
|
<< pos() << " " << size() << " in idx: " << idx()
|
2001-10-12 15:38:58 +00:00
|
|
|
<< " in atom: '";
|
2001-12-03 16:24:50 +00:00
|
|
|
WriteStream wi(lyxerr, false);
|
2001-12-18 12:21:33 +00:00
|
|
|
par()->write(wi);
|
2001-10-12 15:38:58 +00:00
|
|
|
lyxerr << "\n";
|
2001-08-08 07:12:09 +00:00
|
|
|
dump("error 4");
|
|
|
|
}
|
2001-12-18 12:21:33 +00:00
|
|
|
pos() = min(pos(), size());
|
2001-08-08 07:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-01 12:10:32 +00:00
|
|
|
MathCursor::size_type MathCursor::size() const
|
2001-08-08 07:46:16 +00:00
|
|
|
{
|
|
|
|
return array().size();
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:04:02 +00:00
|
|
|
MathCursor::col_type MathCursor::hullCol() const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
return Cursor_[0].par_->asGridInset()->col(Cursor_[0].idx_);
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 15:04:02 +00:00
|
|
|
MathCursor::row_type MathCursor::hullRow() const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-12-11 15:35:18 +00:00
|
|
|
return Cursor_[0].par_->asGridInset()->row(Cursor_[0].idx_);
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
bool MathCursor::hasPrevAtom() const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
return pos() > 0;
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
bool MathCursor::hasNextAtom() const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
return pos() < size();
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathAtom const & MathCursor::prevAtom() const
|
2001-07-25 14:15:05 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
lyx::Assert(pos() > 0);
|
2001-09-11 10:58:17 +00:00
|
|
|
return array().at(pos() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathAtom & MathCursor::prevAtom()
|
2001-09-11 10:58:17 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
lyx::Assert(pos() > 0);
|
2001-09-11 10:58:17 +00:00
|
|
|
return array().at(pos() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathAtom const & MathCursor::nextAtom() const
|
2001-09-11 10:58:17 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
lyx::Assert(pos() < size());
|
2001-09-11 10:58:17 +00:00
|
|
|
return array().at(pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathAtom & MathCursor::nextAtom()
|
2001-09-11 10:58:17 +00:00
|
|
|
{
|
2001-10-12 12:02:49 +00:00
|
|
|
lyx::Assert(pos() < size());
|
2001-09-11 10:58:17 +00:00
|
|
|
return array().at(pos());
|
2001-07-25 14:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
MathArray & MathCursor::array() const
|
|
|
|
{
|
|
|
|
static MathArray dummy;
|
|
|
|
|
2001-09-26 15:20:45 +00:00
|
|
|
if (idx() >= par()->nargs()) {
|
2001-08-07 15:04:57 +00:00
|
|
|
lyxerr << "############ idx_ " << idx() << " not valid\n";
|
2001-06-25 00:06:33 +00:00
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
if (Cursor_.size() == 0) {
|
|
|
|
lyxerr << "############ Cursor_.size() == 0 not valid\n";
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
return cursor().cell();
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathXArray & MathCursor::xarray() const
|
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
static MathXArray dummy;
|
|
|
|
|
|
|
|
if (Cursor_.size() == 0) {
|
|
|
|
lyxerr << "############ Cursor_.size() == 0 not valid\n";
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
2001-07-16 15:53:25 +00:00
|
|
|
return cursor().xcell();
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
void MathCursor::idxNext()
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-08-07 15:04:57 +00:00
|
|
|
par()->idxNext(idx(), pos());
|
2001-07-06 12:09:32 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
void MathCursor::idxPrev()
|
|
|
|
{
|
2001-08-07 15:04:57 +00:00
|
|
|
par()->idxPrev(idx(), pos());
|
2001-07-06 12:09:32 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
void MathCursor::splitCell()
|
|
|
|
{
|
2001-11-27 09:44:32 +00:00
|
|
|
if (idx() + 1 == par()->nargs())
|
2001-07-06 12:09:32 +00:00
|
|
|
return;
|
|
|
|
MathArray ar = array();
|
2001-08-07 15:04:57 +00:00
|
|
|
ar.erase(0, pos());
|
2001-08-08 07:46:16 +00:00
|
|
|
array().erase(pos(), size());
|
2001-08-07 15:04:57 +00:00
|
|
|
++idx();
|
|
|
|
pos() = 0;
|
2001-07-06 12:09:32 +00:00
|
|
|
array().insert(0, ar);
|
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
void MathCursor::breakLine()
|
|
|
|
{
|
2001-08-08 15:35:49 +00:00
|
|
|
// leave inner cells
|
|
|
|
while (popRight())
|
|
|
|
;
|
|
|
|
|
2001-11-09 08:35:57 +00:00
|
|
|
MathHullInset * p = formula()->par()->asHullInset();
|
2001-10-12 12:02:49 +00:00
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
|
2001-07-06 12:09:32 +00:00
|
|
|
p->mutate(LM_OT_EQNARRAY);
|
2001-08-08 15:35:49 +00:00
|
|
|
idx() = 0;
|
|
|
|
pos() = size();
|
2001-12-10 10:09:00 +00:00
|
|
|
} else {
|
2001-12-11 15:04:02 +00:00
|
|
|
p->addRow(hullRow());
|
2001-07-09 16:59:57 +00:00
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
// split line
|
2001-12-11 15:04:02 +00:00
|
|
|
const row_type r = hullRow();
|
|
|
|
for (col_type c = hullCol() + 1; c < p->ncols(); ++c)
|
2001-12-10 10:09:00 +00:00
|
|
|
p->cell(p->index(r, c)).swap(p->cell(p->index(r + 1, c)));
|
2001-07-06 12:09:32 +00:00
|
|
|
|
2001-12-10 10:09:00 +00:00
|
|
|
// split cell
|
|
|
|
splitCell();
|
|
|
|
p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
|
|
|
|
}
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-06-27 14:10:35 +00:00
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-12-11 15:04:02 +00:00
|
|
|
//void MathCursor::readLine(MathArray & ar) const
|
|
|
|
//{
|
|
|
|
// idx_type base = row() * par()->ncols();
|
|
|
|
// for (idx_type off = 0; off < par()->ncols(); ++off)
|
|
|
|
// ar.push_back(par()->cell(base + off));
|
|
|
|
//}
|
2001-11-12 16:45:09 +00:00
|
|
|
|
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
char MathCursor::valign() const
|
|
|
|
{
|
2001-10-01 12:10:32 +00:00
|
|
|
idx_type idx;
|
2001-11-13 08:21:03 +00:00
|
|
|
MathGridInset * p = enclosingGrid(idx);
|
2001-10-01 12:10:32 +00:00
|
|
|
return p ? p->valign() : '\0';
|
2001-06-27 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-06-27 14:10:35 +00:00
|
|
|
char MathCursor::halign() const
|
|
|
|
{
|
2001-10-01 12:10:32 +00:00
|
|
|
idx_type idx;
|
2001-11-13 08:21:03 +00:00
|
|
|
MathGridInset * p = enclosingGrid(idx);
|
2001-10-01 12:10:32 +00:00
|
|
|
return p ? p->halign(idx % p->ncols()) : '\0';
|
2001-06-27 14:10:35 +00:00
|
|
|
}
|
2001-07-16 15:53:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
|
|
|
|
{
|
|
|
|
MathCursorPos anc = normalAnchor();
|
|
|
|
if (anc < cursor()) {
|
|
|
|
i1 = anc;
|
|
|
|
i2 = cursor();
|
|
|
|
} else {
|
|
|
|
i1 = cursor();
|
|
|
|
i2 = anc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursorPos & MathCursor::cursor()
|
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
lyx::Assert(Cursor_.size());
|
2001-07-16 15:53:25 +00:00
|
|
|
return Cursor_.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MathCursorPos const & MathCursor::cursor() const
|
|
|
|
{
|
2001-12-10 10:09:00 +00:00
|
|
|
lyx::Assert(Cursor_.size());
|
2001-07-16 15:53:25 +00:00
|
|
|
return Cursor_.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-11 18:51:47 +00:00
|
|
|
bool MathCursor::goUpDown(bool up)
|
2001-07-16 15:53:25 +00:00
|
|
|
{
|
2001-12-12 14:05:38 +00:00
|
|
|
int xlow, xhigh, ylow, yhigh;
|
|
|
|
|
|
|
|
int xo, yo;
|
2001-12-11 18:51:47 +00:00
|
|
|
getPos(xo, yo);
|
2001-07-16 15:53:25 +00:00
|
|
|
|
2001-12-12 14:05:38 +00:00
|
|
|
// try current cell first
|
|
|
|
xarray().boundingBox(xlow, xhigh, ylow, yhigh);
|
|
|
|
if (up)
|
|
|
|
yhigh = yo - 4;
|
|
|
|
else
|
|
|
|
ylow = yo + 4;
|
|
|
|
if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh))
|
|
|
|
return true;
|
|
|
|
|
2001-12-11 18:51:47 +00:00
|
|
|
// try to find an inset that knows better then we
|
|
|
|
while (1) {
|
|
|
|
// we found a cell that think something "below" us.
|
|
|
|
if (up) {
|
2001-12-12 13:11:28 +00:00
|
|
|
if (par()->idxUp(idx()))
|
2001-12-11 18:51:47 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2001-12-12 13:11:28 +00:00
|
|
|
if (par()->idxDown(idx()))
|
2001-12-11 18:51:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-12-11 18:51:47 +00:00
|
|
|
if (!popLeft()) {
|
2001-12-12 14:05:38 +00:00
|
|
|
// no such inset found, just take something "above"
|
2001-12-11 18:51:47 +00:00
|
|
|
return
|
|
|
|
bruteFind(xo, yo,
|
|
|
|
formula()->xlow(),
|
|
|
|
formula()->xhigh(),
|
|
|
|
up ? formula()->ylow() : yo + 4,
|
|
|
|
up ? yo - 4 : formula()->yhigh()
|
|
|
|
);
|
|
|
|
}
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
2001-12-11 18:51:47 +00:00
|
|
|
xarray().boundingBox(xlow, xhigh, ylow, yhigh);
|
|
|
|
bruteFind(xo, yo, xlow, xhigh, ylow, yhigh);
|
|
|
|
return true;
|
2001-07-16 15:53:25 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 18:58:13 +00:00
|
|
|
|
2001-12-11 18:51:47 +00:00
|
|
|
bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
|
2001-12-05 17:50:18 +00:00
|
|
|
{
|
|
|
|
cursor_type best_cursor;
|
|
|
|
double best_dist = 1e10;
|
|
|
|
|
2001-12-11 15:35:18 +00:00
|
|
|
MathIterator it = ibegin(formula()->par().nucleus());
|
|
|
|
MathIterator et = iend(formula()->par().nucleus());
|
2001-12-05 17:50:18 +00:00
|
|
|
for ( ; it != et; ++it) {
|
2001-12-12 14:05:38 +00:00
|
|
|
// avoid invalid nesting hen selecting
|
|
|
|
if (selection_ && !positionable(it.cursor(), Anchor_))
|
|
|
|
continue;
|
2001-12-05 17:50:18 +00:00
|
|
|
int xo = it.position().xpos();
|
|
|
|
int yo = it.position().ypos();
|
2001-12-12 14:05:38 +00:00
|
|
|
if (xlow - 2 <= xo && xo <= xhigh + 2 &&
|
|
|
|
ylow - 2 <= yo && yo <= yhigh + 2)
|
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
|
|
|
|
if (d < best_dist) {
|
|
|
|
best_dist = d;
|
|
|
|
best_cursor = it.cursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_dist < 1e10)
|
|
|
|
Cursor_ = best_cursor;
|
2001-12-05 18:55:45 +00:00
|
|
|
return best_dist < 1e10;
|
2001-12-05 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-08 09:47:44 +00:00
|
|
|
bool MathCursor::idxLeft()
|
2001-08-08 09:31:36 +00:00
|
|
|
{
|
2001-08-08 09:47:44 +00:00
|
|
|
return par()->idxLeft(idx(), pos());
|
2001-08-08 09:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-08 09:47:44 +00:00
|
|
|
bool MathCursor::idxRight()
|
2001-08-08 09:31:36 +00:00
|
|
|
{
|
2001-08-08 09:47:44 +00:00
|
|
|
return par()->idxRight(idx(), pos());
|
2001-08-08 09:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 08:03:52 +00:00
|
|
|
bool MathCursor::interpret(string const & s)
|
2001-08-13 14:02:37 +00:00
|
|
|
{
|
2001-10-10 16:35:18 +00:00
|
|
|
//lyxerr << "interpret 1: '" << s << "'\n";
|
2001-08-13 14:02:37 +00:00
|
|
|
if (s.empty())
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-11-13 08:03:52 +00:00
|
|
|
if (s.size() == 1)
|
|
|
|
return interpret(s[0]);
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
//lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
|
|
|
|
//owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt);
|
|
|
|
//lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-11-12 16:45:09 +00:00
|
|
|
if (s.size() >= 5 && s.substr(0, 5) == "cases") {
|
|
|
|
unsigned int n = 1;
|
2001-11-13 12:51:43 +00:00
|
|
|
istringstream is(s.substr(5).c_str());
|
2001-11-12 16:45:09 +00:00
|
|
|
is >> n;
|
|
|
|
n = std::max(1u, n);
|
|
|
|
niceInsert(MathAtom(new MathCasesInset(n)));
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-11-12 16:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
|
2001-09-26 15:20:45 +00:00
|
|
|
unsigned int m = 1;
|
|
|
|
unsigned int n = 1;
|
2001-08-13 14:02:37 +00:00
|
|
|
string v_align;
|
|
|
|
string h_align;
|
2001-11-13 12:51:43 +00:00
|
|
|
istringstream is(s.substr(6).c_str());
|
2001-08-13 14:02:37 +00:00
|
|
|
is >> m >> n >> v_align >> h_align;
|
2001-09-26 15:20:45 +00:00
|
|
|
m = std::max(1u, m);
|
|
|
|
n = std::max(1u, n);
|
2001-08-13 14:02:37 +00:00
|
|
|
v_align += 'c';
|
2001-10-12 12:02:49 +00:00
|
|
|
niceInsert(MathAtom(new MathArrayInset(m, n, v_align[0], h_align)));
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 09:55:37 +00:00
|
|
|
if (s.size() >= 7 && s.substr(0, 7) == "replace") {
|
|
|
|
ReplaceData rep;
|
|
|
|
istringstream is(s.substr(7).c_str());
|
|
|
|
string from, to;
|
|
|
|
is >> from >> to;
|
|
|
|
mathed_parse_cell(rep.from, from);
|
|
|
|
mathed_parse_cell(rep.to, to);
|
|
|
|
lyxerr << "replacing '" << from << "' with '" << to << "'\n";
|
|
|
|
par()->replace(rep);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-08-17 17:50:00 +00:00
|
|
|
if (s == "\\over" || s == "\\choose" || s == "\\atop") {
|
2001-08-13 14:46:06 +00:00
|
|
|
MathArray ar = array();
|
2001-12-18 03:16:46 +00:00
|
|
|
MathAtom t(createMathInset(s.substr(1)));
|
2001-10-12 12:02:49 +00:00
|
|
|
t->asNestInset()->cell(0).swap(array());
|
2001-08-13 14:46:06 +00:00
|
|
|
pos() = 0;
|
2001-10-12 12:02:49 +00:00
|
|
|
niceInsert(t);
|
2001-08-15 05:50:39 +00:00
|
|
|
popRight();
|
|
|
|
left();
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-12-11 11:33:43 +00:00
|
|
|
latexkeys const * l = in_word_set(s.substr(1));
|
|
|
|
if (l && (l->token == LM_TK_FONT || l->token == LM_TK_OLDFONT)) {
|
|
|
|
lastcode_ = static_cast<MathTextCodes>(l->id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
niceInsert(createMathInset(s.substr(1)));
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
2002-01-03 12:02:54 +00:00
|
|
|
|
2001-12-12 14:37:59 +00:00
|
|
|
bool MathCursor::script(bool up)
|
|
|
|
{
|
|
|
|
macroModeClose();
|
|
|
|
selCut();
|
|
|
|
if (hasPrevAtom() && prevAtom()->asScriptInset()) {
|
|
|
|
prevAtom()->asScriptInset()->ensure(up);
|
|
|
|
pushRight(prevAtom());
|
|
|
|
idx() = up;
|
|
|
|
pos() = size();
|
|
|
|
} else if (hasNextAtom() && nextAtom()->asScriptInset()) {
|
|
|
|
nextAtom()->asScriptInset()->ensure(up);
|
|
|
|
pushLeft(nextAtom());
|
|
|
|
idx() = up;
|
|
|
|
pos() = 0;
|
|
|
|
} else {
|
|
|
|
plainInsert(MathAtom(new MathScriptInset(up)));
|
|
|
|
prevAtom()->asScriptInset()->ensure(up);
|
|
|
|
pushRight(prevAtom());
|
|
|
|
idx() = up;
|
|
|
|
pos() = 0;
|
|
|
|
}
|
|
|
|
selPaste();
|
|
|
|
dump("1");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
|
2001-11-13 08:03:52 +00:00
|
|
|
bool MathCursor::interpret(char c)
|
2001-10-10 16:35:18 +00:00
|
|
|
{
|
2001-12-12 14:39:33 +00:00
|
|
|
//lyxerr << "interpret 2: '" << c << "'\n";
|
2001-11-09 13:51:24 +00:00
|
|
|
// handle macroMode
|
2001-10-10 16:35:18 +00:00
|
|
|
if (inMacroMode()) {
|
|
|
|
string name = macroName();
|
|
|
|
|
|
|
|
if (name == "\\" && c == '#') {
|
|
|
|
insert(c, LM_TC_TEX);
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "\\" && c == '\\') {
|
2001-10-10 16:39:42 +00:00
|
|
|
backspace();
|
2001-10-10 16:35:18 +00:00
|
|
|
interpret("\\backslash");
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
if (name == "\\#" && '1' <= c && c <= '9') {
|
|
|
|
insert(c, LM_TC_TEX);
|
|
|
|
macroModeClose();
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
if (isalpha(c)) {
|
|
|
|
insert(c, LM_TC_TEX);
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name == "\\") {
|
|
|
|
insert(c, LM_TC_TEX);
|
|
|
|
macroModeClose();
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macroModeClose();
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 16:35:18 +00:00
|
|
|
}
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-12-15 16:13:11 +00:00
|
|
|
if (selection_) {
|
2001-11-29 16:39:44 +00:00
|
|
|
selClear();
|
2001-12-15 16:13:11 +00:00
|
|
|
if (c == ' ')
|
|
|
|
return true;
|
|
|
|
// fall through in the other cases
|
|
|
|
}
|
2001-08-13 14:02:37 +00:00
|
|
|
|
2001-12-18 12:21:33 +00:00
|
|
|
if (lastcode_ == LM_TC_TEXTRM || par()->asBoxInset()) {
|
2001-12-15 16:13:11 +00:00
|
|
|
// suppress direct insertion of two spaces in a row
|
2001-10-25 14:46:57 +00:00
|
|
|
// the still allows typing '<space>a<space>' and deleting the 'a', but
|
2001-12-15 16:13:11 +00:00
|
|
|
// it is better than nothing...
|
2001-11-13 08:24:41 +00:00
|
|
|
if (c == ' ' && hasPrevAtom() && prevAtom()->getChar() == ' ')
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-28 13:34:04 +00:00
|
|
|
insert(c, LM_TC_TEXTRM);
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == ' ') {
|
2001-10-12 13:29:46 +00:00
|
|
|
if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
|
|
|
|
prevAtom()->asSpaceInset()->incSpace();
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-17 09:48:24 +00:00
|
|
|
}
|
2001-12-18 12:21:33 +00:00
|
|
|
if (popRight())
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
|
|
|
// if are at the very end, leave the formula
|
|
|
|
return pos() != size();
|
2001-08-13 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-10-29 15:45:24 +00:00
|
|
|
/*
|
2001-10-10 16:35:18 +00:00
|
|
|
if (strchr("{}", c)) {
|
2001-10-10 13:20:51 +00:00
|
|
|
insert(c, LM_TC_TEX);
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-10 13:20:51 +00:00
|
|
|
}
|
2001-10-29 15:45:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (c == '{') {
|
|
|
|
niceInsert(MathAtom(new MathBraceInset));
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-29 15:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '}') {
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-29 15:45:24 +00:00
|
|
|
}
|
2001-10-10 13:20:51 +00:00
|
|
|
|
2001-10-10 16:35:18 +00:00
|
|
|
if (strchr("#$%", c)) {
|
2001-10-12 12:02:49 +00:00
|
|
|
insert(MathAtom(new MathSpecialCharInset(c)));
|
2001-09-13 16:14:43 +00:00
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-30 08:55:13 +00:00
|
|
|
}
|
|
|
|
|
2001-10-23 08:14:52 +00:00
|
|
|
if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
|
|
|
|
insert(c, LM_TC_VAR);
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-10-23 08:14:52 +00:00
|
|
|
}
|
2001-08-30 14:40:43 +00:00
|
|
|
|
2001-10-23 08:14:52 +00:00
|
|
|
if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
|
|
|
|
insert(c, LM_TC_VAR);
|
|
|
|
lastcode_ = LM_TC_VAR;
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-08-28 13:34:04 +00:00
|
|
|
if (c == '\\') {
|
2001-10-02 15:01:57 +00:00
|
|
|
insert(c, LM_TC_TEX);
|
2001-08-28 13:34:04 +00:00
|
|
|
//bv->owner()->message(_("TeX mode"));
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-08-28 13:34:04 +00:00
|
|
|
// no special circumstances, so insert the character without any fuss
|
2001-12-11 11:33:43 +00:00
|
|
|
insert(c, lastcode_);
|
|
|
|
lastcode_ = LM_TC_MIN;
|
2001-11-13 08:03:52 +00:00
|
|
|
return true;
|
2001-08-13 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 07:46:11 +00:00
|
|
|
|
|
|
|
|
2001-12-05 17:50:18 +00:00
|
|
|
MathCursorPos MathCursor::normalAnchor() const
|
2001-08-14 07:46:11 +00:00
|
|
|
{
|
2001-12-12 14:05:38 +00:00
|
|
|
if (Anchor_.size() < Cursor_.size()) {
|
|
|
|
Anchor_ = Cursor_;
|
|
|
|
lyxerr << "unusual Anchor size\n";
|
|
|
|
dump("1");
|
|
|
|
}
|
|
|
|
//lyx::Assert(Anchor_.size() >= Cursor_.size());
|
2001-12-05 17:50:18 +00:00
|
|
|
// use Anchor on the same level as Cursor
|
|
|
|
MathCursorPos normal = Anchor_[Cursor_.size() - 1];
|
|
|
|
if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
|
|
|
|
// anchor is behind cursor -> move anchor behind the inset
|
|
|
|
++normal.pos_;
|
|
|
|
}
|
|
|
|
return normal;
|
2001-08-14 07:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-05 17:50:18 +00:00
|
|
|
void MathCursor::stripFromLastEqualSign()
|
2001-08-14 07:46:11 +00:00
|
|
|
{
|
2001-12-05 17:50:18 +00:00
|
|
|
// find position of last '=' in the array
|
|
|
|
MathArray & ar = cursor().cell();
|
|
|
|
MathArray::const_iterator et = ar.end();
|
|
|
|
for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
if ((*it)->getChar() == '=')
|
|
|
|
et = it;
|
|
|
|
|
|
|
|
// delete everything behind this position
|
|
|
|
ar.erase(et - ar.begin(), ar.size());
|
|
|
|
pos() = ar.size();
|
2001-08-14 07:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-03 09:41:26 +00:00
|
|
|
void MathCursor::setSelection(cursor_type const & where, size_type n)
|
|
|
|
{
|
|
|
|
selection_ = true;
|
|
|
|
Anchor_ = where;
|
|
|
|
Cursor_ = where;
|
|
|
|
cursor().pos_ += n;
|
|
|
|
}
|