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
29 #include "mathed/support.h"
30 #include "formulabase.h"
31 #include "math_cursor.h"
32 #include "math_arrayinset.h"
33 #include "math_bigopinset.h"
34 #include "math_charinset.h"
35 #include "math_symbolinset.h"
36 #include "math_decorationinset.h"
37 #include "math_deliminset.h"
38 #include "math_dotsinset.h"
39 #include "math_fracinset.h"
40 #include "math_funcinset.h"
41 #include "math_funcliminset.h"
42 #include "math_gridinset.h"
43 #include "math_macro.h"
44 #include "math_macroarg.h"
45 #include "math_macrotable.h"
46 #include "math_macrotemplate.h"
47 #include "math_matrixinset.h"
48 #include "math_noglyphinset.h"
49 #include "math_rootinset.h"
50 #include "math_spaceinset.h"
51 #include "math_sqrtinset.h"
52 #include "math_stackrelinset.h"
53 #include "support/lstrings.h"
54 #include "math_scriptinset.h"
55 #include "math_parser.h"
67 void grab(MathCursor const & cursor)
72 cursor.getSelection(i1, i2);
73 if (i1.idx_ == i2.idx_)
74 data_.push_back(MathArray(i1.cell(), 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 data_.push_back(i1.cell(indices[i]));
82 void erase(MathCursor & cursor)
86 cursor.getSelection(i1, i2);
87 if (i1.idx_ == i2.idx_) {
88 i1.cell().erase(i1.pos_, i2.pos_);
90 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
91 for (unsigned i = 0; i < indices.size(); ++i)
92 i1.cell(indices[i]).erase();
97 void paste(MathCursor & cursor) const
99 cursor.insert(glue());
102 // glues selection to one cell
103 MathArray glue() const
106 for (unsigned i = 0; i < data_.size(); ++i)
107 ar.push_back(data_[i]);
116 std::vector<MathArray> data_;
120 Selection theSelection;
123 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
125 os << "(par: " << p.par_ << " idx: " << p.idx_
126 << " pos: " << p.pos_ << ")";
133 MathCursor::MathCursor(InsetFormulaBase * formula)
134 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
140 MathCursor::~MathCursor()
145 void MathCursor::pushLeft(MathInset * par)
149 par->idxFirst(p.idx_, p.pos_);
150 Cursor_.push_back(p);
154 void MathCursor::pushRight(MathInset * par)
159 par->idxLast(p.idx_, p.pos_);
160 Cursor_.push_back(p);
164 bool MathCursor::popLeft()
166 if (Cursor_.size() <= 1)
172 bool MathCursor::popRight()
174 if (Cursor_.size() <= 1)
182 MathInset * MathCursor::parInset(int i) const
184 return Cursor_[i].par_;
188 void MathCursor::dump(char const * what) const
192 lyxerr << "MC: " << what << "\n";
193 for (unsigned i = 0; i < Cursor_.size(); ++i)
194 lyxerr << " i: " << i
195 << " pos: " << Cursor_[i].pos_
196 << " idx: " << Cursor_[i].idx_
197 << " par: " << Cursor_[i].par_ << "\n";
199 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
203 void MathCursor::seldump(char const * str) const
205 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
209 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
210 << str << "\ntheSelection: " << selection_
211 << " '" << theSelection.glue() << "'\n";
212 for (unsigned int i = 0; i < Cursor_.size(); ++i)
213 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
215 for (unsigned int i = 0; i < Anchor_.size(); ++i)
216 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
217 //lyxerr << "\ncursor.pos_: " << pos();
218 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
219 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
223 bool MathCursor::isInside(MathInset const * p) const
225 for (unsigned i = 0; i < Cursor_.size(); ++i)
226 if (parInset(i) == p)
232 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
237 if (!(p->isActive() || (useupdown && p->isScriptInset())))
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)
273 // was MacroModeBack()
274 if (!imacro_->name().empty()) {
275 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
276 imacro_->metrics(imacro_->size());
284 MathInset * p = prevInset();
285 if (openable(p, sel, false)) {
290 return posLeft() || idxLeft() || popLeft();
294 bool MathCursor::right(bool sel)
304 MathInset * p = nextInset();
305 if (openable(p, sel, false)) {
310 return posRight() || idxRight() || popRight();
314 void MathCursor::first()
317 pushLeft(outerPar());
321 void MathCursor::last()
328 void MathCursor::setPos(int x, int y)
331 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
334 lastcode_ = LM_TC_MIN;
337 cursor().par_ = outerPar();
342 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
343 int distmin = 1 << 30; // large enough
344 for (int i = 0; i < par()->nargs(); ++i) {
345 MathXArray const & ar = par()->xcell(i);
346 int x1 = x - ar.xo();
347 int y1 = y - ar.yo();
348 int c = ar.x2pos(x1);
349 int xx = abs(x1 - ar.pos2x(c));
351 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
352 // << " c: " << c << " xo: " << ar.xo() << "\n";
353 if (yy + xx <= distmin) {
359 //lyxerr << "found idx: " << idx() << " cursor: "
361 MathInset * n = nextInset();
362 MathInset * p = prevInset();
363 if (openable(n, selection_, true) && n->covers(x, y))
365 else if (openable(p, selection_, true) && p->covers(x, y))
374 void MathCursor::home()
379 if (!par()->idxHome(idx(), pos()))
385 void MathCursor::end()
390 if (!par()->idxEnd(idx(), pos()))
396 void MathCursor::plainErase()
398 array().erase(pos());
402 void MathCursor::insert(char c, MathTextCodes t)
404 //lyxerr << "inserting '" << c << "'\n";
411 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
415 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
416 // was MacroModeinsert(c);
417 imacro_->setName(imacro_->name() + c);
422 array().insert(pos(), new MathCharInset(c, t));
427 void MathCursor::insert(MathInset * p)
438 array().insert(pos(), p);
443 void MathCursor::insert(MathArray const & ar)
449 array().insert(pos(), ar);
454 void MathCursor::backspace()
475 void MathCursor::erase()
486 // delete empty cells if necessary
487 if (pos() == 0 && array().empty()) {
490 par()->idxDelete(idx(), popit, removeit);
491 if (popit && popLeft() && removeit)
503 void MathCursor::delLine()
512 if (par()->nrows() > 1)
513 par()->delRow(row());
517 bool MathCursor::up(bool sel)
526 // check whether we could move into an inset on the right or on the left
527 MathInset * p = nextInset();
530 if (p->idxFirstUp(idxx, poss)) {
542 if (p->idxLastUp(idxx, poss)) {
555 bool MathCursor::down(bool sel)
564 // check whether we could move into an inset on the right or on the left
565 MathInset * p = nextInset();
569 if (p->idxFirstDown(idxx, poss)) {
582 if (p->idxLastDown(idxx, poss)) {
595 bool MathCursor::toggleLimits()
597 MathScriptInset * p = prevScriptInset();
600 int old = p->limits();
601 p->limits(old < 0 ? 1 : -1);
602 return old != p->limits();
606 void MathCursor::setSize(MathStyles size)
608 par()->userSetSize(size);
612 void MathCursor::interpret(string const & t)
614 //lyxerr << "interpret: '" << s << "'\n";
615 //lyxerr << "in: " << in_word_set(s) << " \n";
620 // temporary glue code
625 if (s[0] == '^' || s[0] == '_') {
626 bool const up = (s[0] == '^');
628 MathScriptInset * p = prevScriptInset();
630 MathInset * b = prevInset();
631 if (b && b->isScriptable()) {
632 p = new MathScriptInset(up, !up, b->clone());
636 p = new MathScriptInset(up, !up);
651 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
652 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
653 insert(new MathSpaceInset(sp));
658 latexkeys const * l = in_word_set(s);
662 p = new MathRootInset;
663 else if (MathMacroTable::hasTemplate(s))
664 p = new MathMacro(MathMacroTable::provideTemplate(s));
665 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
670 istringstream is(s.substr(7).c_str());
671 is >> m >> n >> v_align >> h_align;
675 MathArrayInset * pp = new MathArrayInset(m, n);
676 pp->valign(v_align[0]);
681 p = new MathFuncInset(s);
686 p = new MathNoglyphInset(l);
690 p = new MathBigopInset(l);
694 p = new MathFuncLimInset(l);
698 p = new MathSymbolInset(l);
702 p = new MathStackrelInset;
706 p = new MathFracInset;
710 p = new MathSqrtInset;
713 case LM_TK_DECORATION:
714 p = new MathDecorationInset(l);
718 p = new MathSpaceInset(l->id);
722 p = new MathDotsInset(l);
726 p = new MathMacro(MathMacroTable::provideTemplate(s));
730 p = new MathFuncInset(l->name);
740 right(); // do not push for e.g. MathSymbolInset
743 p->metrics(p->size());
748 void MathCursor::macroModeOpen()
751 imacro_ = new MathFuncInset("");
752 array().insert(pos(), imacro_);
756 lyxerr << "Math Warning: Already in macro mode" << endl;
760 void MathCursor::macroModeClose()
763 string name = imacro_->name();
772 void MathCursor::selCopy()
776 theSelection.grab(*this);
782 void MathCursor::selCut()
786 theSelection.grab(*this);
787 theSelection.erase(*this);
790 theSelection.clear();
795 void MathCursor::selDel()
799 theSelection.erase(*this);
805 void MathCursor::selPaste()
808 theSelection.paste(*this);
813 void MathCursor::selHandle(bool sel)
815 if (sel == selection_)
818 theSelection.clear();
824 void MathCursor::selStart()
830 theSelection.clear();
836 void MathCursor::selClear()
843 void MathCursor::drawSelection(Painter & pain) const
850 getSelection(i1, i2);
852 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
854 if (i1.idx_ == i2.idx_) {
855 MathXArray & c = i1.xcell();
856 int x1 = c.xo() + c.pos2x(i1.pos_);
857 int y1 = c.yo() - c.ascent();
858 int x2 = c.xo() + c.pos2x(i2.pos_);
859 int y2 = c.yo() + c.descent();
860 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
863 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
864 for (unsigned i = 0; i < indices.size(); ++i) {
865 MathXArray & c = i1.xcell(indices[i]);
867 int y1 = c.yo() - c.ascent();
868 int x2 = c.xo() + c.width();
869 int y2 = c.yo() + c.descent();
870 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
876 MathTextCodes MathCursor::nextCode() const
878 //return (pos() == size()) ? LM_TC_MIN : nextInset()->code();
883 void MathCursor::handleFont(MathTextCodes t)
888 getSelection(i1, i2);
889 if (i1.idx_ == i2.idx_) {
890 MathArray & ar = i1.cell();
891 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
892 ar.nextInset(pos)->handleFont(t);
895 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
899 void MathCursor::handleAccent(string const & name)
901 latexkeys const * l = in_word_set(name);
903 handleNest(new MathDecorationInset(l));
907 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
909 handleNest(new MathDelimInset(l, r));
913 void MathCursor::handleNest(MathInset * p)
917 p->cell(0) = theSelection.glue();
924 void MathCursor::getPos(int & x, int & y)
927 #warning This should probably take cellXOffset and cellYOffset into account
929 x = xarray().xo() + xarray().pos2x(pos());
934 MathInset * MathCursor::par() const
936 return cursor().par_;
940 InsetFormulaBase const * MathCursor::formula()
946 int MathCursor::idx() const
948 return cursor().idx_;
952 int & MathCursor::idx()
954 return cursor().idx_;
958 int MathCursor::pos() const
960 return cursor().pos_;
964 int & MathCursor::pos()
966 return cursor().pos_;
970 bool MathCursor::inMacroMode() const
976 bool MathCursor::selection() const
982 void MathCursor::clearLastCode()
984 lastcode_ = LM_TC_MIN;
988 void MathCursor::setLastCode(MathTextCodes t)
994 MathTextCodes MathCursor::getLastCode() const
1000 MathArrayInset * MathCursor::enclosingArray(int & idx) const
1002 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1003 if (Cursor_[i].par_->isArray()) {
1004 idx = Cursor_[i].idx_;
1005 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1012 void MathCursor::pullArg(bool goright)
1016 MathArray a = array();
1019 array().insert(pos(), a);
1026 MathStyles MathCursor::style() const
1028 return xarray().style();
1032 void MathCursor::normalize() const
1034 #ifdef WITH_WARNINGS
1035 #warning This is evil!
1037 MathCursor * it = const_cast<MathCursor *>(this);
1040 lyxerr << "this should not really happen - 1: " << idx() << "\n";
1041 if (idx() >= par()->nargs()) {
1042 lyxerr << "this should not really happen - 2: "
1043 << idx() << " " << par()->nargs() << "\n";
1046 it->idx() = max(idx(), 0);
1047 it->idx() = min(idx(), par()->nargs() - 1);
1050 lyxerr << "this should not really happen - 3: " << pos() << "\n";
1051 if (pos() > size()) {
1052 lyxerr << "this should not really happen - 4: "
1053 << pos() << " " << size() << "\n";
1056 it->pos() = max(pos(), 0);
1057 it->pos() = min(pos(), size());
1061 int MathCursor::size() const
1063 return array().size();
1067 int MathCursor::col() const
1069 return par()->col(idx());
1073 int MathCursor::row() const
1075 return par()->row(idx());
1080 char MathCursorPos::getChar() const
1082 return array().getChar(pos());
1086 string MathCursorPos::readString()
1089 int code = nextCode();
1090 for ( ; OK() && nextCode() == code; Next())
1098 MathInset * MathCursor::prevInset() const
1103 return array().nextInset(pos() - 1);
1107 MathInset * MathCursor::nextInset() const
1110 return array().nextInset(pos());
1114 MathScriptInset * MathCursor::prevScriptInset() const
1117 MathInset * p = prevInset();
1118 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1122 MathSpaceInset * MathCursor::prevSpaceInset() const
1125 MathInset * p = prevInset();
1126 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1130 MathArray & MathCursor::array() const
1132 static MathArray dummy;
1134 lyxerr << "############ par_ not valid\n";
1138 if (idx() < 0 || idx() >= par()->nargs()) {
1139 lyxerr << "############ idx_ " << idx() << " not valid\n";
1143 return cursor().cell();
1147 MathXArray & MathCursor::xarray() const
1149 return cursor().xcell();
1153 void MathCursor::idxNext()
1155 par()->idxNext(idx(), pos());
1159 void MathCursor::idxPrev()
1161 par()->idxPrev(idx(), pos());
1165 void MathCursor::splitCell()
1167 if (idx() == par()->nargs() - 1)
1169 MathArray ar = array();
1171 array().erase(pos(), size());
1174 array().insert(0, ar);
1178 void MathCursor::breakLine()
1180 // leave inner cells
1184 MathMatrixInset * p = outerPar();
1185 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1186 p->mutate(LM_OT_EQNARRAY);
1193 const int r = row();
1194 for (int c = col() + 1; c < p->ncols(); ++c) {
1195 const int i1 = p->index(r, c);
1196 const int i2 = p->index(r + 1, c);
1197 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1198 p->cell(i1).swap(p->cell(i2));
1203 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1208 char MathCursor::valign() const
1211 MathArrayInset * p = enclosingArray(idx);
1212 return p ? p->valign() : 0;
1216 char MathCursor::halign() const
1219 MathArrayInset * p = enclosingArray(idx);
1220 return p ? p->halign(idx % p->ncols()) : 0;
1224 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1226 MathCursorPos anc = normalAnchor();
1227 if (anc < cursor()) {
1237 MathCursorPos & MathCursor::cursor()
1239 return Cursor_.back();
1243 MathCursorPos const & MathCursor::cursor() const
1245 return Cursor_.back();
1249 int MathCursor::cellXOffset() const
1251 return par()->cellXOffset(idx());
1255 int MathCursor::cellYOffset() const
1257 return par()->cellYOffset(idx());
1261 int MathCursor::xpos() const
1263 return cellXOffset() + xarray().pos2x(pos());
1267 int MathCursor::ypos() const
1269 return cellYOffset();
1274 void MathCursor::gotoX(int x)
1276 pos() = xarray().x2pos(x - cellXOffset());
1280 bool MathCursor::goUp()
1282 // first ask the inset if it knows better then we
1283 if (par()->idxUp(idx(), pos()))
1286 // if not, apply brute force.
1290 std::vector<MathCursorPos> save = Cursor_;
1291 y0 -= xarray().ascent();
1292 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1294 if (save != Cursor_ && xarray().yo() < y0)
1302 bool MathCursor::goDown()
1304 // first ask the inset if it knows better then we
1305 if (par()->idxDown(idx(), pos()))
1308 // if not, apply brute force.
1312 std::vector<MathCursorPos> save = Cursor_;
1313 y0 += xarray().descent();
1314 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1316 if (save != Cursor_ && xarray().yo() > y0)
1324 bool MathCursor::idxLeft()
1326 return par()->idxLeft(idx(), pos());
1330 bool MathCursor::idxRight()
1332 return par()->idxRight(idx(), pos());
1336 MathMatrixInset * MathCursor::outerPar() const
1339 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
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);
1368 MathArray & MathCursorPos::cell() const
1370 return par_->cell(idx_);
1374 MathXArray & MathCursorPos::xcell(int idx) const
1376 return par_->xcell(idx);
1380 MathXArray & MathCursorPos::xcell() const
1382 return par_->xcell(idx_);
1386 MathCursorPos MathCursor::normalAnchor() const
1388 // use Anchor on the same level as Cursor
1389 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1390 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1391 // anchor is behind cursor -> move anchor behind the inset