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_scopeinset.h"
39 #include "math_scriptinset.h"
40 #include "math_spaceinset.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)
153 bool MathCursor::popRight()
155 if (Cursor_.size() <= 1)
163 MathInset * MathCursor::parInset(int i) const
165 return Cursor_[i].par_;
169 void MathCursor::dump(char const * what) const
173 lyxerr << "MC: " << what << "\n";
174 for (unsigned i = 0; i < Cursor_.size(); ++i)
175 lyxerr << " i: " << i
176 << " pos: " << Cursor_[i].pos_
177 << " idx: " << Cursor_[i].idx_
178 << " par: " << Cursor_[i].par_ << "\n";
180 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
184 void MathCursor::seldump(char const * str) const
186 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
190 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
191 << str << "\ntheSelection: " << selection_
192 << " '" << theSelection.glue() << "'\n";
193 for (unsigned int i = 0; i < Cursor_.size(); ++i)
194 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
196 for (unsigned int i = 0; i < Anchor_.size(); ++i)
197 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
198 //lyxerr << "\ncursor.pos_: " << pos();
199 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
200 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
204 bool MathCursor::isInside(MathInset const * p) const
206 for (unsigned i = 0; i < Cursor_.size(); ++i)
207 if (parInset(i) == p)
213 bool MathCursor::openable(MathInset * p, bool sel) const
222 // we can't move into anything new during selection
223 if (Cursor_.size() == Anchor_.size())
225 if (p != Anchor_[Cursor_.size()].par_)
232 bool MathCursor::positionable(MathInset * p, bool sel) const
241 // we can't move into anything new during selection
242 if (Cursor_.size() == Anchor_.size())
244 if (p != Anchor_[Cursor_.size()].par_)
251 bool MathCursor::posLeft()
260 bool MathCursor::posRight()
269 bool MathCursor::left(bool sel)
274 lastcode_ = LM_TC_VAR;
278 lastcode_ = LM_TC_VAR;
280 MathInset * p = prevInset();
281 if (openable(p, sel)) {
286 return posLeft() || idxLeft() || popLeft();
290 bool MathCursor::right(bool sel)
295 lastcode_ = LM_TC_VAR;
299 lastcode_ = LM_TC_VAR;
301 MathInset * p = nextInset();
302 if (openable(p, sel)) {
307 return posRight() || idxRight() || popRight();
311 void MathCursor::first()
314 pushLeft(outerPar());
318 void MathCursor::last()
325 void MathCursor::setPos(int x, int y)
328 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
331 lastcode_ = LM_TC_VAR;
334 cursor().par_ = outerPar();
339 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
340 int distmin = 1 << 30; // large enough
341 for (int i = 0; i < par()->nargs(); ++i) {
342 MathXArray const & ar = par()->xcell(i);
343 int x1 = x - ar.xo();
344 int y1 = y - ar.yo();
345 int c = ar.x2pos(x1);
346 int xx = abs(x1 - ar.pos2x(c));
348 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
349 // << " c: " << c << " xo: " << ar.xo() << "\n";
350 if (yy + xx <= distmin) {
356 //lyxerr << "found idx: " << idx() << " cursor: "
358 MathInset * n = nextInset();
359 MathInset * p = prevInset();
360 if (positionable(n, selection_) && n->covers(x, y))
362 else if (positionable(p, selection_) && p->covers(x, y))
371 void MathCursor::home()
375 lastcode_ = LM_TC_VAR;
376 if (!par()->idxHome(idx(), pos()))
382 void MathCursor::end()
386 lastcode_ = LM_TC_VAR;
387 if (!par()->idxEnd(idx(), pos()))
393 void MathCursor::plainErase()
395 array().erase(pos());
399 void MathCursor::insert(char c, MathTextCodes t)
401 //lyxerr << "inserting '" << c << "'\n";
402 array().insert(pos(), new MathCharInset(c, t));
407 void MathCursor::insert(MathInset * p)
418 array().insert(pos(), p);
423 void MathCursor::niceInsert(MathInset * p)
426 lyxerr << "should not happen\n";
433 right(); // do not push for e.g. MathSymbolInset
436 p->metrics(p->size());
440 void MathCursor::insert(MathArray const & ar)
446 array().insert(pos(), ar);
451 void MathCursor::backspace()
472 void MathCursor::erase()
483 // delete empty cells if necessary
484 if (pos() == 0 && array().empty()) {
487 par()->idxDelete(idx(), popit, removeit);
488 if (popit && popLeft() && removeit)
500 void MathCursor::delLine()
509 if (par()->nrows() > 1)
510 par()->delRow(row());
514 bool MathCursor::up(bool sel)
523 // check whether we could move into an inset on the right or on the left
524 MathInset * p = nextInset();
527 if (p->idxFirstUp(idxx, poss)) {
539 if (p->idxLastUp(idxx, poss)) {
552 bool MathCursor::down(bool sel)
561 // check whether we could move into an inset on the right or on the left
562 MathInset * p = nextInset();
566 if (p->idxFirstDown(idxx, poss)) {
579 if (p->idxLastDown(idxx, poss)) {
592 bool MathCursor::toggleLimits()
594 MathScriptInset * p = prevScriptInset();
597 int old = p->limits();
598 p->limits(old < 0 ? 1 : -1);
599 return old != p->limits();
603 void MathCursor::setSize(MathStyles size)
605 par()->userSetSize(size);
609 void MathCursor::macroModeClose()
611 string s = macroName();
613 pos() = pos() - s.size();
614 for (unsigned i = 0; i < s.size(); ++i)
616 lastcode_ = LM_TC_VAR;
622 string MathCursor::macroName() const
626 while (p >= 0 && array().nextInset(p)->code() == LM_TC_TEX) {
627 s = array().nextInset(p)->getChar() + s;
634 void MathCursor::selCopy()
638 theSelection.grab(*this);
644 void MathCursor::selCut()
648 theSelection.grab(*this);
649 theSelection.erase(*this);
652 theSelection.clear();
657 void MathCursor::selDel()
661 theSelection.erase(*this);
667 void MathCursor::selPaste()
670 theSelection.paste(*this);
675 void MathCursor::selHandle(bool sel)
677 if (sel == selection_)
680 theSelection.clear();
686 void MathCursor::selStart()
692 theSelection.clear();
698 void MathCursor::selClear()
705 void MathCursor::drawSelection(Painter & pain) const
712 getSelection(i1, i2);
714 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
716 if (i1.idx_ == i2.idx_) {
717 MathXArray & c = i1.xcell();
718 int x1 = c.xo() + c.pos2x(i1.pos_);
719 int y1 = c.yo() - c.ascent();
720 int x2 = c.xo() + c.pos2x(i2.pos_);
721 int y2 = c.yo() + c.descent();
722 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
724 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
725 for (unsigned i = 0; i < indices.size(); ++i) {
726 MathXArray & c = i1.xcell(indices[i]);
728 int y1 = c.yo() - c.ascent();
729 int x2 = c.xo() + c.width();
730 int y2 = c.yo() + c.descent();
731 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
737 MathTextCodes MathCursor::nextCode() const
739 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
744 void MathCursor::handleFont(MathTextCodes t)
750 getSelection(i1, i2);
751 if (i1.idx_ == i2.idx_) {
752 MathArray & ar = i1.cell();
753 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
754 ar.nextInset(pos)->handleFont(t);
757 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
761 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
763 handleNest(new MathDelimInset(l, r));
767 void MathCursor::handleNest(MathInset * p)
771 p->cell(0) = theSelection.glue();
778 void MathCursor::getPos(int & x, int & y)
781 #warning This should probably take cellXOffset and cellYOffset into account
783 x = xarray().xo() + xarray().pos2x(pos());
788 MathInset * MathCursor::par() const
790 return cursor().par_;
794 InsetFormulaBase const * MathCursor::formula()
800 int MathCursor::idx() const
802 return cursor().idx_;
806 int & MathCursor::idx()
808 return cursor().idx_;
812 int MathCursor::pos() const
814 return cursor().pos_;
818 int & MathCursor::pos()
820 return cursor().pos_;
824 bool MathCursor::inMacroMode() const
826 return lastcode_ == LM_TC_TEX;
830 bool MathCursor::selection() const
836 MathArrayInset * MathCursor::enclosingArray(int & idx) const
838 for (int i = Cursor_.size() - 1; i >= 0; --i) {
839 if (Cursor_[i].par_->isArray()) {
840 idx = Cursor_[i].idx_;
841 return static_cast<MathArrayInset *>(Cursor_[i].par_);
848 void MathCursor::pullArg(bool goright)
852 MathArray a = array();
855 array().insert(pos(), a);
862 MathStyles MathCursor::style() const
864 return xarray().style();
868 void MathCursor::normalize() const
871 #warning This is evil!
873 MathCursor * it = const_cast<MathCursor *>(this);
876 lyxerr << "this should not really happen - 1: " << idx() << "\n";
877 if (idx() >= par()->nargs()) {
878 lyxerr << "this should not really happen - 2: "
879 << idx() << " " << par()->nargs() << "\n";
882 it->idx() = max(idx(), 0);
883 it->idx() = min(idx(), par()->nargs() - 1);
886 lyxerr << "this should not really happen - 3: " << pos() << "\n";
887 if (pos() > size()) {
888 lyxerr << "this should not really happen - 4: "
889 << pos() << " " << size() << "\n";
892 it->pos() = max(pos(), 0);
893 it->pos() = min(pos(), size());
897 int MathCursor::size() const
899 return array().size();
903 int MathCursor::col() const
905 return par()->col(idx());
909 int MathCursor::row() const
911 return par()->row(idx());
916 char MathCursorPos::getChar() const
918 return array().getChar(pos());
922 string MathCursorPos::readString()
925 int code = nextCode();
926 for ( ; OK() && nextCode() == code; Next())
934 MathInset * MathCursor::prevInset() const
939 return array().nextInset(pos() - 1);
943 MathInset * MathCursor::nextInset() const
946 return array().nextInset(pos());
950 MathScriptInset * MathCursor::prevScriptInset() const
953 MathInset * p = prevInset();
954 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
958 MathSpaceInset * MathCursor::prevSpaceInset() const
961 MathInset * p = prevInset();
962 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
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(in_word_set(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] == '^');
1232 MathScriptInset * p = prevScriptInset();
1234 MathInset * b = prevInset();
1235 if (b && b->isScriptable()) {
1236 p = new MathScriptInset(up, !up, b->clone());
1240 p = new MathScriptInset(up, !up);
1257 if (lastcode_ == LM_TC_TEXTRM) {
1258 insert(c, LM_TC_TEXTRM);
1263 if (inMacroMode()) {
1265 lastcode_ = LM_TC_VAR;
1269 MathSpaceInset * p = prevSpaceInset();
1275 if (mathcursor->popRight())
1279 // this would not work if the inset is in an table!
1280 //bv->text->cursorRight(bv, true);
1281 //result = FINISHED;
1285 if (lastcode_ == LM_TC_TEX) {
1286 if (macroName().empty()) {
1287 insert(c, LM_TC_TEX);
1290 lastcode_ = LM_TC_VAR;
1294 insert(c, LM_TC_TEX);
1297 lastcode_ = LM_TC_VAR;
1304 niceInsert(new MathScopeInset);
1308 if (c == '}') // ignore it
1311 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1312 static char const greek[26] =
1313 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1314 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1315 0, 0, 0, 0, 0 , 'Z' };
1317 MathTextCodes code = LM_TC_SYMB;
1318 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1324 #warning greek insert problem? look here!
1325 //if (lastcode_ == LM_TC_GREEK1)
1326 lastcode_ = LM_TC_VAR;
1331 lastcode_ = LM_TC_TEX;
1332 //bv->owner()->message(_("TeX mode"));
1336 // no special circumstances, so insert the character without any fuss
1342 ////////////////////////////////////////////////////////////////////////
1345 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1347 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1351 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1353 if (ti.par_ != it.par_) {
1354 lyxerr << "can't compare cursor and anchor in different insets\n";
1357 if (ti.idx_ != it.idx_)
1358 return ti.idx_ < it.idx_;
1359 return ti.pos_ < it.pos_;
1363 MathArray & MathCursorPos::cell(int idx) const
1365 return par_->cell(idx);
1369 MathArray & MathCursorPos::cell() const
1371 return par_->cell(idx_);
1375 MathXArray & MathCursorPos::xcell(int idx) const
1377 return par_->xcell(idx);
1381 MathXArray & MathCursorPos::xcell() const
1383 return par_->xcell(idx_);
1387 MathCursorPos MathCursor::normalAnchor() const
1389 // use Anchor on the same level as Cursor
1390 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1391 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1392 // anchor is behind cursor -> move anchor behind the inset