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 * what) const
176 lyxerr << "MC: " << what << "\n";
177 for (unsigned i = 0; i < Cursor_.size(); ++i)
178 lyxerr << " i: " << i
179 << " pos: " << Cursor_[i].pos_
180 << " idx: " << Cursor_[i].idx_
181 << " par: " << Cursor_[i].par_ << "\n";
183 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
187 void MathCursor::seldump(char const * str) const
189 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
193 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
194 << str << "\ntheSelection: " << selection_
195 << " '" << theSelection.glue() << "'\n";
196 for (unsigned int i = 0; i < Cursor_.size(); ++i)
197 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
199 for (unsigned int i = 0; i < Anchor_.size(); ++i)
200 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
201 //lyxerr << "\ncursor.pos_: " << pos();
202 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
203 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
207 bool MathCursor::isInside(MathInset const * p) const
209 for (unsigned i = 0; i < Cursor_.size(); ++i)
210 if (parInset(i) == p)
216 bool MathCursor::openable(MathInset * p, bool sel) const
225 // we can't move into anything new during selection
226 if (Cursor_.size() == Anchor_.size())
228 if (p != Anchor_[Cursor_.size()].par_)
235 MathInset * MathCursor::positionable(MathAtom * t, int x, int y) const
241 // we can't move into anything new during selection
242 if (Cursor_.size() == Anchor_.size())
244 //if (t != Anchor_[Cursor_.size()].par_)
251 if (p && p->nargs() && p->covers(x, y))
255 if (p && p->nargs() && p->covers(x, y))
259 if (p && p->nargs() && p->covers(x, y))
266 bool MathCursor::posLeft()
275 bool MathCursor::posRight()
284 bool MathCursor::left(bool sel)
289 lastcode_ = LM_TC_VAR;
293 lastcode_ = LM_TC_VAR;
295 MathInset * p = prevInset();
296 if (openable(p, sel)) {
301 return posLeft() || idxLeft() || popLeft();
305 bool MathCursor::right(bool sel)
310 lastcode_ = LM_TC_VAR;
314 lastcode_ = LM_TC_VAR;
316 MathInset * p = nextInset();
317 if (openable(p, sel)) {
322 return posRight() || idxRight() || popRight();
326 void MathCursor::first()
329 pushLeft(outerPar());
333 void MathCursor::last()
340 void MathCursor::setPos(int x, int y)
343 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
346 lastcode_ = LM_TC_VAR;
349 cursor().par_ = outerPar();
354 //lyxerr << "found idx: " << idx() << " cursor: " << pos() << "\n";
355 int distmin = 1 << 30; // large enough
356 for (int i = 0; i < par()->nargs(); ++i) {
357 MathXArray const & ar = par()->xcell(i);
358 int x1 = x - ar.xo();
359 int y1 = y - ar.yo();
360 int c = ar.x2pos(x1);
361 int xx = abs(x1 - ar.pos2x(c));
363 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
364 // << " c: " << c << " xo: " << ar.xo() << "\n";
365 if (yy + xx <= distmin) {
371 //lyxerr << "found idx: " << idx() << " cursor: "
373 if (MathInset * p = positionable(nextAtom(), x, y))
375 else if (MathInset * p = positionable(prevAtom(), x, y))
384 void MathCursor::home(bool sel)
389 lastcode_ = LM_TC_VAR;
390 if (!par()->idxHome(idx(), pos()))
396 void MathCursor::end(bool sel)
401 lastcode_ = LM_TC_VAR;
402 if (!par()->idxEnd(idx(), pos()))
408 void MathCursor::plainErase()
410 array().erase(pos());
414 void MathCursor::insert(char c, MathTextCodes t)
416 //lyxerr << "inserting '" << c << "'\n";
417 array().insert(pos(), new MathCharInset(c, t));
422 void MathCursor::insert(MathInset * p)
433 array().insert(pos(), p); // this invalidates the pointer!
434 p = array().at(pos())->nucleus();
439 void MathCursor::niceInsert(MathInset * p)
442 lyxerr << "should not happen\n";
446 //cerr << "\n2: "; p->write(cerr, true); cerr << "\n";
447 insert(p); // inserting invalidates the pointer!
448 p = prevAtom()->nucleus();
449 //cerr << "\n3: "; p->write(cerr, true); cerr << "\n";
452 right(); // do not push for e.g. MathSymbolInset
455 p->metrics(p->size());
459 void MathCursor::insert(MathArray const & ar)
465 array().insert(pos(), ar);
470 void MathCursor::backspace()
491 void MathCursor::erase()
502 // delete empty cells if necessary
503 if (pos() == 0 && array().empty()) {
506 par()->idxDelete(idx(), popit, removeit);
507 if (popit && popLeft() && removeit)
519 void MathCursor::delLine()
528 if (par()->nrows() > 1)
529 par()->delRow(row());
533 bool MathCursor::up(bool sel)
542 // check whether we could move into a superscript on the right or on the left
543 if (prevAtom() && prevAtom()->up()) {
544 pushRight(prevAtom()->up());
548 if (nextAtom() && nextAtom()->up()) {
549 pushLeft(nextAtom()->up());
557 bool MathCursor::down(bool sel)
566 // check whether we could move into an subscript on the right or on the left
567 if (prevAtom() && prevAtom()->down()) {
568 pushRight(prevAtom()->down());
572 if (nextAtom() && nextAtom()->down()) {
573 pushLeft(nextAtom()->down());
581 bool MathCursor::toggleLimits()
583 MathAtom * t = prevAtom();
586 int old = t->limits();
587 t->limits(old < 0 ? 1 : -1);
588 return old != t->limits();
592 void MathCursor::setSize(MathStyles size)
594 par()->userSetSize(size);
598 void MathCursor::macroModeClose()
600 string s = macroName();
602 pos() = pos() - s.size();
603 for (unsigned i = 0; i < s.size(); ++i)
605 lastcode_ = LM_TC_VAR;
611 string MathCursor::macroName() const
614 for (int i = pos() - 1; i >= 0; --i) {
615 MathInset * p = array().at(i)->nucleus();
616 if (p && p->code() == LM_TC_TEX)
617 s = p->getChar() + s;
625 void MathCursor::selCopy()
629 theSelection.grab(*this);
635 void MathCursor::selCut()
639 theSelection.grab(*this);
640 theSelection.erase(*this);
643 theSelection.clear();
648 void MathCursor::selDel()
652 theSelection.erase(*this);
658 void MathCursor::selPaste()
661 theSelection.paste(*this);
666 void MathCursor::selHandle(bool sel)
668 if (sel == selection_)
671 theSelection.clear();
677 void MathCursor::selStart()
683 theSelection.clear();
689 void MathCursor::selClear()
696 void MathCursor::drawSelection(Painter & pain) const
703 getSelection(i1, i2);
705 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
707 if (i1.idx_ == i2.idx_) {
708 MathXArray & c = i1.xcell();
709 int x1 = c.xo() + c.pos2x(i1.pos_);
710 int y1 = c.yo() - c.ascent();
711 int x2 = c.xo() + c.pos2x(i2.pos_);
712 int y2 = c.yo() + c.descent();
713 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
715 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
716 for (unsigned i = 0; i < indices.size(); ++i) {
717 MathXArray & c = i1.xcell(indices[i]);
719 int y1 = c.yo() - c.ascent();
720 int x2 = c.xo() + c.width();
721 int y2 = c.yo() + c.descent();
722 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
728 MathTextCodes MathCursor::nextCode() const
730 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
735 void MathCursor::handleFont(MathTextCodes t)
741 getSelection(i1, i2);
742 if (i1.idx_ == i2.idx_) {
743 MathArray & ar = i1.cell();
744 for (int pos = i1.pos_; pos != i2.pos_; ++pos) {
745 MathInset * p = ar.at(pos)->nucleus();
751 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
755 void MathCursor::handleDelim(string const & l, string const & r)
757 handleNest(new MathDelimInset(l, r));
761 void MathCursor::handleNest(MathInset * p)
765 p->cell(0) = theSelection.glue();
767 insert(p); // this invalidates p!
768 p = prevAtom()->nucleus();
773 void MathCursor::getPos(int & x, int & y)
776 #warning This should probably take cellXOffset and cellYOffset into account
778 x = xarray().xo() + xarray().pos2x(pos());
783 MathInset * MathCursor::par() const
785 return cursor().par_;
789 InsetFormulaBase const * MathCursor::formula()
795 int MathCursor::idx() const
797 return cursor().idx_;
801 int & MathCursor::idx()
803 return cursor().idx_;
807 int MathCursor::pos() const
809 return cursor().pos_;
813 int & MathCursor::pos()
815 return cursor().pos_;
819 bool MathCursor::inMacroMode() const
821 return lastcode_ == LM_TC_TEX;
825 bool MathCursor::selection() const
831 MathArrayInset * MathCursor::enclosingArray(int & idx) const
833 for (int i = Cursor_.size() - 1; i >= 0; --i) {
834 if (Cursor_[i].par_->isArray()) {
835 idx = Cursor_[i].idx_;
836 return static_cast<MathArrayInset *>(Cursor_[i].par_);
843 void MathCursor::pullArg(bool goright)
847 MathArray a = array();
850 array().insert(pos(), a);
857 MathStyles MathCursor::style() const
859 return xarray().style();
863 void MathCursor::normalize() const
866 #warning This is evil!
868 MathCursor * it = const_cast<MathCursor *>(this);
871 lyxerr << "this should not really happen - 1: " << idx() << "\n";
872 if (idx() >= par()->nargs()) {
873 lyxerr << "this should not really happen - 2: "
874 << idx() << " " << par()->nargs() << "\n";
877 it->idx() = max(idx(), 0);
878 it->idx() = min(idx(), par()->nargs() - 1);
881 lyxerr << "this should not really happen - 3: " << pos() << "\n";
882 if (pos() > size()) {
883 lyxerr << "this should not really happen - 4: "
884 << pos() << " " << size() << "\n";
887 it->pos() = max(pos(), 0);
888 it->pos() = min(pos(), size());
892 int MathCursor::size() const
894 return array().size();
898 int MathCursor::col() const
900 return par()->col(idx());
904 int MathCursor::row() const
906 return par()->row(idx());
911 char MathCursorPos::getChar() const
913 return array().getChar(pos());
917 string MathCursorPos::readString()
920 int code = nextCode();
921 for ( ; OK() && nextCode() == code; Next())
929 MathInset * MathCursor::prevInset() const
931 return prevAtom() ? prevAtom()->nucleus() : 0;
935 MathInset * MathCursor::nextInset() const
937 return nextAtom() ? nextAtom()->nucleus() : 0;
941 MathSpaceInset * MathCursor::prevSpaceInset() const
943 MathInset * p = prevInset();
944 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
948 MathAtom const * MathCursor::prevAtom() const
950 return array().at(pos() - 1);
954 MathAtom * MathCursor::prevAtom()
956 return array().at(pos() - 1);
960 MathAtom const * MathCursor::nextAtom() const
962 return array().at(pos());
966 MathAtom * MathCursor::nextAtom()
968 return array().at(pos());
972 MathArray & MathCursor::array() const
974 static MathArray dummy;
976 lyxerr << "############ par_ not valid\n";
980 if (idx() < 0 || idx() >= par()->nargs()) {
981 lyxerr << "############ idx_ " << idx() << " not valid\n";
985 return cursor().cell();
989 MathXArray & MathCursor::xarray() const
991 return cursor().xcell();
995 void MathCursor::idxNext()
997 par()->idxNext(idx(), pos());
1001 void MathCursor::idxPrev()
1003 par()->idxPrev(idx(), pos());
1007 void MathCursor::splitCell()
1009 if (idx() == par()->nargs() - 1)
1011 MathArray ar = array();
1013 array().erase(pos(), size());
1016 array().insert(0, ar);
1020 void MathCursor::breakLine()
1022 // leave inner cells
1026 MathMatrixInset * p = outerPar();
1027 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1028 p->mutate(LM_OT_EQNARRAY);
1035 const int r = row();
1036 for (int c = col() + 1; c < p->ncols(); ++c) {
1037 const int i1 = p->index(r, c);
1038 const int i2 = p->index(r + 1, c);
1039 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1040 p->cell(i1).swap(p->cell(i2));
1045 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1050 char MathCursor::valign() const
1053 MathArrayInset * p = enclosingArray(idx);
1054 return p ? p->valign() : 0;
1058 char MathCursor::halign() const
1061 MathArrayInset * p = enclosingArray(idx);
1062 return p ? p->halign(idx % p->ncols()) : 0;
1066 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1068 MathCursorPos anc = normalAnchor();
1069 if (anc < cursor()) {
1079 MathCursorPos & MathCursor::cursor()
1081 return Cursor_.back();
1085 MathCursorPos const & MathCursor::cursor() const
1087 return Cursor_.back();
1091 int MathCursor::cellXOffset() const
1093 return par()->cellXOffset(idx());
1097 int MathCursor::cellYOffset() const
1099 return par()->cellYOffset(idx());
1103 int MathCursor::xpos() const
1105 return cellXOffset() + xarray().pos2x(pos());
1109 int MathCursor::ypos() const
1111 return cellYOffset();
1116 void MathCursor::gotoX(int x)
1118 pos() = xarray().x2pos(x - cellXOffset());
1122 bool MathCursor::goUp()
1124 // first ask the inset if it knows better then we
1125 if (par()->idxUp(idx(), pos()))
1128 // if not, apply brute force.
1132 std::vector<MathCursorPos> save = Cursor_;
1133 y0 -= xarray().ascent();
1134 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1136 if (save != Cursor_ && xarray().yo() < y0)
1144 bool MathCursor::goDown()
1146 // first ask the inset if it knows better then we
1147 if (par()->idxDown(idx(), pos()))
1150 // if not, apply brute force.
1154 std::vector<MathCursorPos> save = Cursor_;
1155 y0 += xarray().descent();
1156 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1158 if (save != Cursor_ && xarray().yo() > y0)
1166 bool MathCursor::idxLeft()
1168 return par()->idxLeft(idx(), pos());
1172 bool MathCursor::idxRight()
1174 return par()->idxRight(idx(), pos());
1178 MathMatrixInset * MathCursor::outerPar() const
1181 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1185 void MathCursor::interpret(string const & s)
1187 //lyxerr << "interpret: '" << s << "'\n";
1188 //lyxerr << "in: " << in_word_set(s) << " \n";
1195 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1196 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1197 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1199 if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
1204 istringstream is(s.substr(7).c_str());
1205 is >> m >> n >> v_align >> h_align;
1209 MathArrayInset * p = new MathArrayInset(m, n);
1210 p->valign(v_align[0]);
1216 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1217 MathArray ar = array();
1218 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1219 p->cell(0).swap(array());
1228 niceInsert(createMathInset(s.substr(1)));
1233 // we got just a single char now
1235 if (c == '^' || c == '_') {
1236 bool const up = (s[0] == '^');
1239 insert('{', LM_TC_TEX);
1240 insert('}', LM_TC_TEX);
1242 MathInset * par = prevAtom()->ensure(up);
1251 if (lastcode_ == LM_TC_TEXTRM) {
1252 insert(c, LM_TC_TEXTRM);
1257 if (inMacroMode()) {
1259 lastcode_ = LM_TC_VAR;
1263 MathSpaceInset * p = prevSpaceInset();
1269 if (mathcursor->popRight())
1273 // this would not work if the inset is in an table!
1274 //bv->text->cursorRight(bv, true);
1275 //result = FINISHED;
1279 if (lastcode_ != LM_TC_TEX && strchr("#$%{}", c)) {
1280 insert(new MathSpecialCharInset(c));
1284 if (lastcode_ == LM_TC_TEX) {
1285 if (macroName().empty()) {
1286 if (strchr("#$%{}", c)) {
1287 insert(new MathCharInset(c, LM_TC_TEX));
1288 lastcode_ = LM_TC_VAR;
1291 insert(c, LM_TC_TEX);
1294 lastcode_ = LM_TC_VAR;
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