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, bool useupdown) const
218 if (!(p->isActive() || (useupdown && p->isScriptInset())))
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::posLeft()
241 bool MathCursor::posRight()
250 bool MathCursor::left(bool sel)
255 lastcode_ = LM_TC_VAR;
259 lastcode_ = LM_TC_VAR;
261 MathInset * p = prevInset();
262 if (openable(p, sel, false)) {
267 return posLeft() || idxLeft() || popLeft();
271 bool MathCursor::right(bool sel)
276 lastcode_ = LM_TC_VAR;
280 lastcode_ = LM_TC_VAR;
282 MathInset * p = nextInset();
283 if (openable(p, sel, false)) {
288 return posRight() || idxRight() || popRight();
292 void MathCursor::first()
295 pushLeft(outerPar());
299 void MathCursor::last()
306 void MathCursor::setPos(int x, int y)
309 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
312 lastcode_ = LM_TC_VAR;
315 cursor().par_ = outerPar();
320 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
321 int distmin = 1 << 30; // large enough
322 for (int i = 0; i < par()->nargs(); ++i) {
323 MathXArray const & ar = par()->xcell(i);
324 int x1 = x - ar.xo();
325 int y1 = y - ar.yo();
326 int c = ar.x2pos(x1);
327 int xx = abs(x1 - ar.pos2x(c));
329 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
330 // << " c: " << c << " xo: " << ar.xo() << "\n";
331 if (yy + xx <= distmin) {
337 //lyxerr << "found idx: " << idx() << " cursor: "
339 MathInset * n = nextInset();
340 MathInset * p = prevInset();
341 if (openable(n, selection_, true) && n->covers(x, y))
343 else if (openable(p, selection_, true) && p->covers(x, y))
352 void MathCursor::home()
356 lastcode_ = LM_TC_VAR;
357 if (!par()->idxHome(idx(), pos()))
363 void MathCursor::end()
367 lastcode_ = LM_TC_VAR;
368 if (!par()->idxEnd(idx(), pos()))
374 void MathCursor::plainErase()
376 array().erase(pos());
380 void MathCursor::insert(char c, MathTextCodes t)
382 //lyxerr << "inserting '" << c << "'\n";
383 array().insert(pos(), new MathCharInset(c, t));
388 void MathCursor::insert(MathInset * p)
399 array().insert(pos(), p);
404 void MathCursor::niceInsert(MathInset * p)
407 lyxerr << "should not happen\n";
414 right(); // do not push for e.g. MathSymbolInset
417 p->metrics(p->size());
421 void MathCursor::insert(MathArray const & ar)
427 array().insert(pos(), ar);
432 void MathCursor::backspace()
453 void MathCursor::erase()
464 // delete empty cells if necessary
465 if (pos() == 0 && array().empty()) {
468 par()->idxDelete(idx(), popit, removeit);
469 if (popit && popLeft() && removeit)
481 void MathCursor::delLine()
490 if (par()->nrows() > 1)
491 par()->delRow(row());
495 bool MathCursor::up(bool sel)
504 // check whether we could move into an inset on the right or on the left
505 MathInset * p = nextInset();
508 if (p->idxFirstUp(idxx, poss)) {
520 if (p->idxLastUp(idxx, poss)) {
533 bool MathCursor::down(bool sel)
542 // check whether we could move into an inset on the right or on the left
543 MathInset * p = nextInset();
547 if (p->idxFirstDown(idxx, poss)) {
560 if (p->idxLastDown(idxx, poss)) {
573 bool MathCursor::toggleLimits()
575 MathScriptInset * p = prevScriptInset();
578 int old = p->limits();
579 p->limits(old < 0 ? 1 : -1);
580 return old != p->limits();
584 void MathCursor::setSize(MathStyles size)
586 par()->userSetSize(size);
590 void MathCursor::macroModeClose()
592 string s = macroName();
594 pos() = pos() - s.size();
595 for (unsigned i = 0; i < s.size(); ++i)
597 lastcode_ = LM_TC_VAR;
603 string MathCursor::macroName() const
607 while (p >= 0 && array().nextInset(p)->code() == LM_TC_TEX) {
608 s = array().nextInset(p)->getChar() + s;
615 void MathCursor::selCopy()
619 theSelection.grab(*this);
625 void MathCursor::selCut()
629 theSelection.grab(*this);
630 theSelection.erase(*this);
633 theSelection.clear();
638 void MathCursor::selDel()
642 theSelection.erase(*this);
648 void MathCursor::selPaste()
651 theSelection.paste(*this);
656 void MathCursor::selHandle(bool sel)
658 if (sel == selection_)
661 theSelection.clear();
667 void MathCursor::selStart()
673 theSelection.clear();
679 void MathCursor::selClear()
686 void MathCursor::drawSelection(Painter & pain) const
693 getSelection(i1, i2);
695 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
697 if (i1.idx_ == i2.idx_) {
698 MathXArray & c = i1.xcell();
699 int x1 = c.xo() + c.pos2x(i1.pos_);
700 int y1 = c.yo() - c.ascent();
701 int x2 = c.xo() + c.pos2x(i2.pos_);
702 int y2 = c.yo() + c.descent();
703 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
705 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
706 for (unsigned i = 0; i < indices.size(); ++i) {
707 MathXArray & c = i1.xcell(indices[i]);
709 int y1 = c.yo() - c.ascent();
710 int x2 = c.xo() + c.width();
711 int y2 = c.yo() + c.descent();
712 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
718 MathTextCodes MathCursor::nextCode() const
720 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
725 void MathCursor::handleFont(MathTextCodes t)
731 getSelection(i1, i2);
732 if (i1.idx_ == i2.idx_) {
733 MathArray & ar = i1.cell();
734 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
735 ar.nextInset(pos)->handleFont(t);
738 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
742 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
744 handleNest(new MathDelimInset(l, r));
748 void MathCursor::handleNest(MathInset * p)
752 p->cell(0) = theSelection.glue();
759 void MathCursor::getPos(int & x, int & y)
762 #warning This should probably take cellXOffset and cellYOffset into account
764 x = xarray().xo() + xarray().pos2x(pos());
769 MathInset * MathCursor::par() const
771 return cursor().par_;
775 InsetFormulaBase const * MathCursor::formula()
781 int MathCursor::idx() const
783 return cursor().idx_;
787 int & MathCursor::idx()
789 return cursor().idx_;
793 int MathCursor::pos() const
795 return cursor().pos_;
799 int & MathCursor::pos()
801 return cursor().pos_;
805 bool MathCursor::inMacroMode() const
807 return lastcode_ == LM_TC_TEX;
811 bool MathCursor::selection() const
817 MathArrayInset * MathCursor::enclosingArray(int & idx) const
819 for (int i = Cursor_.size() - 1; i >= 0; --i) {
820 if (Cursor_[i].par_->isArray()) {
821 idx = Cursor_[i].idx_;
822 return static_cast<MathArrayInset *>(Cursor_[i].par_);
829 void MathCursor::pullArg(bool goright)
833 MathArray a = array();
836 array().insert(pos(), a);
843 MathStyles MathCursor::style() const
845 return xarray().style();
849 void MathCursor::normalize() const
852 #warning This is evil!
854 MathCursor * it = const_cast<MathCursor *>(this);
857 lyxerr << "this should not really happen - 1: " << idx() << "\n";
858 if (idx() >= par()->nargs()) {
859 lyxerr << "this should not really happen - 2: "
860 << idx() << " " << par()->nargs() << "\n";
863 it->idx() = max(idx(), 0);
864 it->idx() = min(idx(), par()->nargs() - 1);
867 lyxerr << "this should not really happen - 3: " << pos() << "\n";
868 if (pos() > size()) {
869 lyxerr << "this should not really happen - 4: "
870 << pos() << " " << size() << "\n";
873 it->pos() = max(pos(), 0);
874 it->pos() = min(pos(), size());
878 int MathCursor::size() const
880 return array().size();
884 int MathCursor::col() const
886 return par()->col(idx());
890 int MathCursor::row() const
892 return par()->row(idx());
897 char MathCursorPos::getChar() const
899 return array().getChar(pos());
903 string MathCursorPos::readString()
906 int code = nextCode();
907 for ( ; OK() && nextCode() == code; Next())
915 MathInset * MathCursor::prevInset() const
920 return array().nextInset(pos() - 1);
924 MathInset * MathCursor::nextInset() const
927 return array().nextInset(pos());
931 MathScriptInset * MathCursor::prevScriptInset() const
934 MathInset * p = prevInset();
935 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
939 MathSpaceInset * MathCursor::prevSpaceInset() const
942 MathInset * p = prevInset();
943 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
947 MathArray & MathCursor::array() const
949 static MathArray dummy;
951 lyxerr << "############ par_ not valid\n";
955 if (idx() < 0 || idx() >= par()->nargs()) {
956 lyxerr << "############ idx_ " << idx() << " not valid\n";
960 return cursor().cell();
964 MathXArray & MathCursor::xarray() const
966 return cursor().xcell();
970 void MathCursor::idxNext()
972 par()->idxNext(idx(), pos());
976 void MathCursor::idxPrev()
978 par()->idxPrev(idx(), pos());
982 void MathCursor::splitCell()
984 if (idx() == par()->nargs() - 1)
986 MathArray ar = array();
988 array().erase(pos(), size());
991 array().insert(0, ar);
995 void MathCursor::breakLine()
1001 MathMatrixInset * p = outerPar();
1002 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1003 p->mutate(LM_OT_EQNARRAY);
1010 const int r = row();
1011 for (int c = col() + 1; c < p->ncols(); ++c) {
1012 const int i1 = p->index(r, c);
1013 const int i2 = p->index(r + 1, c);
1014 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1015 p->cell(i1).swap(p->cell(i2));
1020 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1025 char MathCursor::valign() const
1028 MathArrayInset * p = enclosingArray(idx);
1029 return p ? p->valign() : 0;
1033 char MathCursor::halign() const
1036 MathArrayInset * p = enclosingArray(idx);
1037 return p ? p->halign(idx % p->ncols()) : 0;
1041 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1043 MathCursorPos anc = normalAnchor();
1044 if (anc < cursor()) {
1054 MathCursorPos & MathCursor::cursor()
1056 return Cursor_.back();
1060 MathCursorPos const & MathCursor::cursor() const
1062 return Cursor_.back();
1066 int MathCursor::cellXOffset() const
1068 return par()->cellXOffset(idx());
1072 int MathCursor::cellYOffset() const
1074 return par()->cellYOffset(idx());
1078 int MathCursor::xpos() const
1080 return cellXOffset() + xarray().pos2x(pos());
1084 int MathCursor::ypos() const
1086 return cellYOffset();
1091 void MathCursor::gotoX(int x)
1093 pos() = xarray().x2pos(x - cellXOffset());
1097 bool MathCursor::goUp()
1099 // first ask the inset if it knows better then we
1100 if (par()->idxUp(idx(), pos()))
1103 // if not, apply brute force.
1107 std::vector<MathCursorPos> save = Cursor_;
1108 y0 -= xarray().ascent();
1109 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1111 if (save != Cursor_ && xarray().yo() < y0)
1119 bool MathCursor::goDown()
1121 // first ask the inset if it knows better then we
1122 if (par()->idxDown(idx(), pos()))
1125 // if not, apply brute force.
1129 std::vector<MathCursorPos> save = Cursor_;
1130 y0 += xarray().descent();
1131 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1133 if (save != Cursor_ && xarray().yo() > y0)
1141 bool MathCursor::idxLeft()
1143 return par()->idxLeft(idx(), pos());
1147 bool MathCursor::idxRight()
1149 return par()->idxRight(idx(), pos());
1153 MathMatrixInset * MathCursor::outerPar() const
1156 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1160 void MathCursor::interpret(string const & s)
1162 //lyxerr << "interpret: '" << s << "'\n";
1163 //lyxerr << "in: " << in_word_set(s) << " \n";
1170 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1171 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1172 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1174 if (s.size() > 8 && s.substr(0, 8) == "\\matrix ") {
1179 istringstream is(s.substr(8).c_str());
1180 is >> m >> n >> v_align >> h_align;
1184 MathArrayInset * p = new MathArrayInset(m, n);
1185 p->valign(v_align[0]);
1191 if (s == "\\over" || s == "\\choose") {
1192 MathArray ar = array();
1193 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1194 p->cell(0).swap(array());
1203 niceInsert(createMathInset(s.substr(1)));
1208 // we got just a single char now
1210 if (c == '^' || c == '_') {
1211 bool const up = (s[0] == '^');
1213 MathScriptInset * p = prevScriptInset();
1215 MathInset * b = prevInset();
1216 if (b && b->isScriptable()) {
1217 p = new MathScriptInset(up, !up, b->clone());
1221 p = new MathScriptInset(up, !up);
1236 niceInsert(new MathScopeInset);
1240 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1241 static char const greek[26] =
1242 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1243 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1244 0, 0, 0, 0, 0 , 'Z' };
1246 MathTextCodes code = LM_TC_SYMB;
1247 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1253 #warning greek insert problem? look here!
1254 //if (lastcode_ == LM_TC_GREEK1)
1255 lastcode_ = LM_TC_VAR;
1262 if (lastcode_ == LM_TC_TEX) {
1263 if (macroName().empty()) {
1264 insert(c, LM_TC_TEX);
1267 lastcode_ = LM_TC_VAR;
1271 insert(c, LM_TC_TEX);
1274 lastcode_ = LM_TC_VAR;
1280 if (strchr("0123456789;:!|[]().,?+/-*<>=", c)) {
1281 if (lastcode_ != LM_TC_TEXTRM)
1282 lastcode_ = LM_TC_VAR;
1283 insert(c, lastcode_);
1287 if (strchr("#$%{|}", c)) {
1288 if (lastcode_ != LM_TC_TEXTRM)
1289 lastcode_ = LM_TC_SPECIAL;
1290 insert(c, lastcode_);
1295 if (inMacroMode()) {
1297 lastcode_ = LM_TC_VAR;
1301 MathSpaceInset * p = prevSpaceInset();
1307 if (lastcode_ == LM_TC_TEXTRM) {
1308 insert(c, LM_TC_TEXTRM);
1312 if (mathcursor->popRight())
1316 // this would not work if the inset is in an table!
1317 //bv->text->cursorRight(bv, true);
1318 //result = FINISHED;
1322 if (c == '\'' || c == '@') {
1323 insert(c, LM_TC_VAR);
1328 lastcode_ = LM_TC_TEX;
1329 //bv->owner()->message(_("TeX mode"));
1334 insert(c, lastcode_);
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