3 * Purpose: Interaction for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Math interaction for a WYSIWYG math editor.
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, Alejandro Aguilar Sierra
12 * Version: 0.8beta, Math & Lyx project.
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
19 #pragma implementation
26 #include "support/lstrings.h"
31 #include "formulabase.h"
32 #include "math_cursor.h"
33 #include "math_factory.h"
34 #include "math_arrayinset.h"
35 #include "math_charinset.h"
36 #include "math_deliminset.h"
37 #include "math_matrixinset.h"
38 #include "math_scriptinset.h"
39 #include "math_spaceinset.h"
40 #include "math_specialcharinset.h"
41 #include "math_parser.h"
53 void grab(MathCursor const & cursor)
58 cursor.getSelection(i1, i2);
59 if (i1.idx_ == i2.idx_)
60 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
62 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
63 for (unsigned i = 0; i < indices.size(); ++i)
64 data_.push_back(i1.cell(indices[i]));
68 void erase(MathCursor & cursor)
72 cursor.getSelection(i1, i2);
73 if (i1.idx_ == i2.idx_)
74 i1.cell().erase(i1.pos_, i2.pos_);
76 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
77 for (unsigned i = 0; i < indices.size(); ++i)
78 i1.cell(indices[i]).erase();
83 void paste(MathCursor & cursor) const
85 cursor.insert(glue());
88 // glues selection to one cell
89 MathArray glue() const
92 for (unsigned i = 0; i < data_.size(); ++i)
93 ar.push_back(data_[i]);
102 std::vector<MathArray> data_;
106 Selection theSelection;
109 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
111 os << "(par: " << p.par_ << " idx: " << p.idx_
112 << " pos: " << p.pos_ << ")";
119 MathCursor::MathCursor(InsetFormulaBase * formula)
120 : formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
126 void MathCursor::pushLeft(MathInset * par)
130 par->idxFirst(p.idx_, p.pos_);
131 Cursor_.push_back(p);
135 void MathCursor::pushRight(MathInset * par)
140 par->idxLast(p.idx_, p.pos_);
141 Cursor_.push_back(p);
145 bool MathCursor::popLeft()
147 if (Cursor_.size() <= 1)
150 //array().at(pos())->removeEmptyScripts();
155 bool MathCursor::popRight()
157 if (Cursor_.size() <= 1)
160 //array().at(pos())->removeEmptyScripts();
166 MathInset * MathCursor::parInset(int i) const
168 return Cursor_[i].par_;
172 void MathCursor::dump(char const *) const
175 lyxerr << "MC: " << what << "\n";
176 for (unsigned i = 0; i < Cursor_.size(); ++i)
177 lyxerr << " i: " << i
178 << " pos: " << Cursor_[i].pos_
179 << " idx: " << Cursor_[i].idx_
180 << " par: " << Cursor_[i].par_ << "\n";
181 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
186 void MathCursor::seldump(char const * str) const
188 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
192 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
193 << str << "\ntheSelection: " << selection_
194 << " '" << theSelection.glue() << "'\n";
195 for (unsigned int i = 0; i < Cursor_.size(); ++i)
196 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
198 for (unsigned int i = 0; i < Anchor_.size(); ++i)
199 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
200 //lyxerr << "\ncursor.pos_: " << pos();
201 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
202 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
206 bool MathCursor::isInside(MathInset const * p) const
208 for (unsigned i = 0; i < Cursor_.size(); ++i)
209 if (parInset(i) == p)
215 bool MathCursor::openable(MathInset * p, bool sel) const
224 // we can't move into anything new during selection
225 if (Cursor_.size() == Anchor_.size())
227 if (p != Anchor_[Cursor_.size()].par_)
234 MathInset * MathCursor::positionable(MathAtom * t, int x, int y) const
240 // we can't move into anything new during selection
241 if (Cursor_.size() == Anchor_.size())
243 //if (t != Anchor_[Cursor_.size()].par_)
250 if (p && p->nargs() && p->covers(x, y))
254 if (p && p->nargs() && p->covers(x, y))
258 if (p && p->nargs() && p->covers(x, y))
265 bool MathCursor::posLeft()
274 bool MathCursor::posRight()
283 bool MathCursor::left(bool sel)
288 lastcode_ = LM_TC_VAR;
292 lastcode_ = LM_TC_VAR;
294 MathInset * p = prevInset();
295 if (openable(p, sel)) {
300 return posLeft() || idxLeft() || popLeft();
304 bool MathCursor::right(bool sel)
309 lastcode_ = LM_TC_VAR;
313 lastcode_ = LM_TC_VAR;
315 MathInset * p = nextInset();
316 if (openable(p, sel)) {
321 return posRight() || idxRight() || popRight();
325 void MathCursor::first()
328 pushLeft(outerPar());
332 void MathCursor::last()
339 void MathCursor::setPos(int x, int y)
342 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
345 lastcode_ = LM_TC_VAR;
348 cursor().par_ = outerPar();
353 //lyxerr << "found idx: " << idx() << " cursor: " << pos() << "\n";
354 int distmin = 1 << 30; // large enough
355 for (int i = 0; i < par()->nargs(); ++i) {
356 MathXArray const & ar = par()->xcell(i);
357 int x1 = x - ar.xo();
358 int y1 = y - ar.yo();
359 int c = ar.x2pos(x1);
360 int xx = abs(x1 - ar.pos2x(c));
362 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
363 // << " c: " << c << " xo: " << ar.xo() << "\n";
364 if (yy + xx <= distmin) {
370 //lyxerr << "found idx: " << idx() << " cursor: "
372 if (MathInset * p = positionable(nextAtom(), x, y))
374 else if (MathInset * p = positionable(prevAtom(), x, y))
383 void MathCursor::home(bool sel)
388 lastcode_ = LM_TC_VAR;
389 if (!par()->idxHome(idx(), pos()))
395 void MathCursor::end(bool sel)
400 lastcode_ = LM_TC_VAR;
401 if (!par()->idxEnd(idx(), pos()))
407 void MathCursor::plainErase()
409 array().erase(pos());
413 void MathCursor::insert(char c, MathTextCodes t)
415 //lyxerr << "inserting '" << c << "'\n";
416 array().insert(pos(), new MathCharInset(c, t));
421 void MathCursor::insert(MathInset * p)
432 array().insert(pos(), p); // this invalidates the pointer!
433 p = array().at(pos())->nucleus();
438 void MathCursor::niceInsert(MathInset * p)
441 lyxerr << "should not happen\n";
445 //cerr << "\n2: "; p->write(cerr, true); cerr << "\n";
446 insert(p); // inserting invalidates the pointer!
447 p = prevAtom()->nucleus();
448 //cerr << "\n3: "; p->write(cerr, true); cerr << "\n";
451 right(); // do not push for e.g. MathSymbolInset
454 p->metrics(p->size());
458 void MathCursor::insert(MathArray const & ar)
464 array().insert(pos(), ar);
469 void MathCursor::backspace()
485 void MathCursor::erase()
496 // delete empty cells if necessary
497 if (pos() == 0 && array().empty()) {
500 par()->idxDelete(idx(), popit, removeit);
501 if (popit && popLeft() && removeit)
513 void MathCursor::delLine()
522 if (par()->nrows() > 1)
523 par()->delRow(row());
527 bool MathCursor::up(bool sel)
536 // check whether we could move into a superscript on the right or on the left
537 if (prevAtom() && prevAtom()->up()) {
538 pushRight(prevAtom()->up());
542 if (nextAtom() && nextAtom()->up()) {
543 pushLeft(nextAtom()->up());
551 bool MathCursor::down(bool sel)
560 // check whether we could move into an subscript on the right or on the left
561 if (prevAtom() && prevAtom()->down()) {
562 pushRight(prevAtom()->down());
566 if (nextAtom() && nextAtom()->down()) {
567 pushLeft(nextAtom()->down());
575 bool MathCursor::toggleLimits()
577 MathAtom * t = prevAtom();
580 int old = t->limits();
581 t->limits(old < 0 ? 1 : -1);
582 return old != t->limits();
586 void MathCursor::setSize(MathStyles size)
588 par()->userSetSize(size);
592 void MathCursor::macroModeClose()
594 string s = macroName();
596 pos() = pos() - s.size();
597 for (unsigned i = 0; i < s.size(); ++i)
599 lastcode_ = LM_TC_VAR;
605 string MathCursor::macroName() const
608 for (int i = pos() - 1; i >= 0; --i) {
609 MathInset * p = array().at(i)->nucleus();
610 if (p && p->code() == LM_TC_TEX)
611 s = p->getChar() + s;
619 void MathCursor::selCopy()
623 theSelection.grab(*this);
629 void MathCursor::selCut()
633 theSelection.grab(*this);
634 theSelection.erase(*this);
637 theSelection.clear();
642 void MathCursor::selDel()
646 theSelection.erase(*this);
652 void MathCursor::selPaste()
655 theSelection.paste(*this);
660 void MathCursor::selHandle(bool sel)
662 if (sel == selection_)
665 theSelection.clear();
671 void MathCursor::selStart()
677 theSelection.clear();
683 void MathCursor::selClear()
690 void MathCursor::drawSelection(Painter & pain) const
697 getSelection(i1, i2);
699 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
701 if (i1.idx_ == i2.idx_) {
702 MathXArray & c = i1.xcell();
703 int x1 = c.xo() + c.pos2x(i1.pos_);
704 int y1 = c.yo() - c.ascent();
705 int x2 = c.xo() + c.pos2x(i2.pos_);
706 int y2 = c.yo() + c.descent();
707 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
709 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
710 for (unsigned i = 0; i < indices.size(); ++i) {
711 MathXArray & c = i1.xcell(indices[i]);
713 int y1 = c.yo() - c.ascent();
714 int x2 = c.xo() + c.width();
715 int y2 = c.yo() + c.descent();
716 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
722 MathTextCodes MathCursor::nextCode() const
724 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
729 void MathCursor::handleFont(MathTextCodes t)
735 getSelection(i1, i2);
736 if (i1.idx_ == i2.idx_) {
737 MathArray & ar = i1.cell();
738 for (int pos = i1.pos_; pos != i2.pos_; ++pos) {
739 MathInset * p = ar.at(pos)->nucleus();
745 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
749 void MathCursor::handleDelim(string const & l, string const & r)
751 handleNest(new MathDelimInset(l, r));
755 void MathCursor::handleNest(MathInset * p)
759 p->cell(0) = theSelection.glue();
761 insert(p); // this invalidates p!
762 p = prevAtom()->nucleus();
767 void MathCursor::getPos(int & x, int & y)
770 #warning This should probably take cellXOffset and cellYOffset into account
772 x = xarray().xo() + xarray().pos2x(pos());
777 MathInset * MathCursor::par() const
779 return cursor().par_;
783 InsetFormulaBase const * MathCursor::formula()
789 int MathCursor::idx() const
791 return cursor().idx_;
795 int & MathCursor::idx()
797 return cursor().idx_;
801 int MathCursor::pos() const
803 return cursor().pos_;
807 int & MathCursor::pos()
809 return cursor().pos_;
813 bool MathCursor::inMacroMode() const
815 return lastcode_ == LM_TC_TEX;
819 bool MathCursor::selection() const
825 MathArrayInset * MathCursor::enclosingArray(int & idx) const
827 for (int i = Cursor_.size() - 1; i >= 0; --i) {
828 if (Cursor_[i].par_->isArray()) {
829 idx = Cursor_[i].idx_;
830 return static_cast<MathArrayInset *>(Cursor_[i].par_);
837 void MathCursor::pullArg(bool goright)
841 MathArray a = array();
844 array().insert(pos(), a);
851 MathStyles MathCursor::style() const
853 return xarray().style();
857 void MathCursor::normalize() const
860 #warning This is evil!
862 MathCursor * it = const_cast<MathCursor *>(this);
865 lyxerr << "this should not really happen - 1: " << idx() << "\n";
866 if (idx() >= par()->nargs()) {
867 lyxerr << "this should not really happen - 2: "
868 << idx() << " " << par()->nargs() << "\n";
871 it->idx() = max(idx(), 0);
872 it->idx() = min(idx(), par()->nargs() - 1);
875 lyxerr << "this should not really happen - 3: " << pos() << "\n";
876 if (pos() > size()) {
877 lyxerr << "this should not really happen - 4: "
878 << pos() << " " << size() << "\n";
881 it->pos() = max(pos(), 0);
882 it->pos() = min(pos(), size());
886 int MathCursor::size() const
888 return array().size();
892 int MathCursor::col() const
894 return par()->col(idx());
898 int MathCursor::row() const
900 return par()->row(idx());
905 char MathCursorPos::getChar() const
907 return array().getChar(pos());
911 string MathCursorPos::readString()
914 int code = nextCode();
915 for ( ; OK() && nextCode() == code; Next())
923 MathInset * MathCursor::prevInset() const
925 return prevAtom() ? prevAtom()->nucleus() : 0;
929 MathInset * MathCursor::nextInset() const
931 return nextAtom() ? nextAtom()->nucleus() : 0;
935 MathSpaceInset * MathCursor::prevSpaceInset() const
937 MathInset * p = prevInset();
938 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
942 MathAtom const * MathCursor::prevAtom() const
944 return array().at(pos() - 1);
948 MathAtom * MathCursor::prevAtom()
950 return array().at(pos() - 1);
954 MathAtom const * MathCursor::nextAtom() const
956 return array().at(pos());
960 MathAtom * MathCursor::nextAtom()
962 return array().at(pos());
966 MathArray & MathCursor::array() const
968 static MathArray dummy;
970 lyxerr << "############ par_ not valid\n";
974 if (idx() < 0 || idx() >= par()->nargs()) {
975 lyxerr << "############ idx_ " << idx() << " not valid\n";
979 return cursor().cell();
983 MathXArray & MathCursor::xarray() const
985 return cursor().xcell();
989 void MathCursor::idxNext()
991 par()->idxNext(idx(), pos());
995 void MathCursor::idxPrev()
997 par()->idxPrev(idx(), pos());
1001 void MathCursor::splitCell()
1003 if (idx() == par()->nargs() - 1)
1005 MathArray ar = array();
1007 array().erase(pos(), size());
1010 array().insert(0, ar);
1014 void MathCursor::breakLine()
1016 // leave inner cells
1020 MathMatrixInset * p = outerPar();
1021 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1022 p->mutate(LM_OT_EQNARRAY);
1029 const int r = row();
1030 for (int c = col() + 1; c < p->ncols(); ++c) {
1031 const int i1 = p->index(r, c);
1032 const int i2 = p->index(r + 1, c);
1033 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1034 p->cell(i1).swap(p->cell(i2));
1039 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1044 char MathCursor::valign() const
1047 MathArrayInset * p = enclosingArray(idx);
1048 return p ? p->valign() : 0;
1052 char MathCursor::halign() const
1055 MathArrayInset * p = enclosingArray(idx);
1056 return p ? p->halign(idx % p->ncols()) : 0;
1060 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1062 MathCursorPos anc = normalAnchor();
1063 if (anc < cursor()) {
1073 MathCursorPos & MathCursor::cursor()
1075 return Cursor_.back();
1079 MathCursorPos const & MathCursor::cursor() const
1081 return Cursor_.back();
1085 int MathCursor::cellXOffset() const
1087 return par()->cellXOffset(idx());
1091 int MathCursor::cellYOffset() const
1093 return par()->cellYOffset(idx());
1097 int MathCursor::xpos() const
1099 return cellXOffset() + xarray().pos2x(pos());
1103 int MathCursor::ypos() const
1105 return cellYOffset();
1110 void MathCursor::gotoX(int x)
1112 pos() = xarray().x2pos(x - cellXOffset());
1116 bool MathCursor::goUp()
1118 // first ask the inset if it knows better then we
1119 if (par()->idxUp(idx(), pos()))
1122 // if not, apply brute force.
1126 std::vector<MathCursorPos> save = Cursor_;
1127 y0 -= xarray().ascent();
1128 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1130 if (save != Cursor_ && xarray().yo() < y0)
1138 bool MathCursor::goDown()
1140 // first ask the inset if it knows better then we
1141 if (par()->idxDown(idx(), pos()))
1144 // if not, apply brute force.
1148 std::vector<MathCursorPos> save = Cursor_;
1149 y0 += xarray().descent();
1150 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1152 if (save != Cursor_ && xarray().yo() > y0)
1160 bool MathCursor::idxLeft()
1162 return par()->idxLeft(idx(), pos());
1166 bool MathCursor::idxRight()
1168 return par()->idxRight(idx(), pos());
1172 MathMatrixInset * MathCursor::outerPar() const
1175 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1179 void MathCursor::interpret(string const & s)
1181 //lyxerr << "interpret: '" << s << "'\n";
1182 //lyxerr << "in: " << in_word_set(s) << " \n";
1189 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1190 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1191 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1193 if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
1198 istringstream is(s.substr(7).c_str());
1199 is >> m >> n >> v_align >> h_align;
1203 MathArrayInset * p = new MathArrayInset(m, n);
1204 p->valign(v_align[0]);
1210 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1211 MathArray ar = array();
1212 MathInset * p = createMathInset(s.substr(1));
1213 p->cell(0).swap(array());
1222 niceInsert(createMathInset(s.substr(1)));
1227 // we got just a single char now
1229 if (c == '^' || c == '_') {
1230 bool const up = (s[0] == '^');
1233 insert('{', LM_TC_TEX);
1234 insert('}', LM_TC_TEX);
1236 MathInset * par = prevAtom()->ensure(up);
1245 if (lastcode_ == LM_TC_TEXTRM) {
1246 insert(c, LM_TC_TEXTRM);
1251 if (inMacroMode()) {
1253 lastcode_ = LM_TC_VAR;
1257 MathSpaceInset * p = prevSpaceInset();
1263 if (mathcursor->popRight())
1267 // this would not work if the inset is in an table!
1268 //bv->text->cursorRight(bv, true);
1269 //result = FINISHED;
1273 if (lastcode_ != LM_TC_TEX && strchr("#$%{}", c)) {
1274 insert(new MathSpecialCharInset(c));
1275 lastcode_ = LM_TC_VAR;
1279 if (lastcode_ == LM_TC_TEX) {
1280 if (macroName().empty()) {
1281 if (strchr("$%{}", c)) {
1282 insert(new MathCharInset(c, LM_TC_TEX));
1283 lastcode_ = LM_TC_VAR;
1286 insert(c, LM_TC_TEX);
1287 if (!isalpha(c) && c != '#') {
1289 lastcode_ = LM_TC_VAR;
1292 if ('1' <= c && c <= '9' && macroName() == "#") {
1293 insert(c, LM_TC_TEX);
1295 lastcode_ = LM_TC_VAR;
1297 else if (isalpha(c))
1298 insert(c, LM_TC_TEX);
1301 lastcode_ = LM_TC_VAR;
1307 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1308 static char const greekl[][26] =
1309 {"alpha", "beta", "chi", "delta", "epsilon", "phi",
1310 "gamma", "eta", "iota", "", "kappa", "lambda", "mu",
1311 "nu", "omikron", "pi", "omega", "rho", "sigma",
1312 "tau", "upsilon", "theta", "", "xi", "upsilon", "zeta"};
1313 static char const greeku[][26] =
1314 {"Alpha", "Beta", "Chi", "Delta", "Epsilon", "Phi",
1315 "Gamma", "Eta", "Iota", "", "Kappa", "Lambda", "Mu",
1316 "Nu", "Omikron", "Pi", "Omega", "Rho", "Sigma", "Tau",
1317 "Upsilon", "Theta", "", "xi", "Upsilon", "Zeta"};
1319 latexkeys const * l = 0;
1320 if ('a' <= c && c <= 'z')
1321 l = in_word_set(greekl[c - 'a']);
1322 if ('A' <= c && c <= 'Z')
1323 l = in_word_set(greeku[c - 'A']);
1326 insert(createMathInset(l));
1328 insert(c, LM_TC_VAR);
1330 #warning greek insert problem? look here!
1331 if (lastcode_ == LM_TC_GREEK1)
1332 lastcode_ = LM_TC_VAR;
1337 lastcode_ = LM_TC_TEX;
1338 //bv->owner()->message(_("TeX mode"));
1342 // no special circumstances, so insert the character without any fuss
1348 ////////////////////////////////////////////////////////////////////////
1351 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1353 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1357 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1359 if (ti.par_ != it.par_) {
1360 lyxerr << "can't compare cursor and anchor in different insets\n";
1363 if (ti.idx_ != it.idx_)
1364 return ti.idx_ < it.idx_;
1365 return ti.pos_ < it.pos_;
1369 MathArray & MathCursorPos::cell(int idx) const
1371 return par_->cell(idx);
1375 MathArray & MathCursorPos::cell() const
1377 return par_->cell(idx_);
1381 MathXArray & MathCursorPos::xcell(int idx) const
1383 return par_->xcell(idx);
1387 MathXArray & MathCursorPos::xcell() const
1389 return par_->xcell(idx_);
1393 MathCursorPos MathCursor::normalAnchor() const
1395 // use Anchor on the same level as Cursor
1396 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1397 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1398 // anchor is behind cursor -> move anchor behind the inset