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_parser.h"
52 void grab(MathCursor const & cursor)
57 cursor.getSelection(i1, i2);
58 if (i1.idx_ == i2.idx_)
59 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
61 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
62 for (unsigned i = 0; i < indices.size(); ++i)
63 data_.push_back(i1.cell(indices[i]));
67 void erase(MathCursor & cursor)
71 cursor.getSelection(i1, i2);
72 if (i1.idx_ == i2.idx_)
73 i1.cell().erase(i1.pos_, i2.pos_);
75 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
76 for (unsigned i = 0; i < indices.size(); ++i)
77 i1.cell(indices[i]).erase();
82 void paste(MathCursor & cursor) const
84 cursor.insert(glue());
87 // glues selection to one cell
88 MathArray glue() const
91 for (unsigned i = 0; i < data_.size(); ++i)
92 ar.push_back(data_[i]);
101 std::vector<MathArray> data_;
105 Selection theSelection;
108 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
110 os << "(par: " << p.par_ << " idx: " << p.idx_
111 << " pos: " << p.pos_ << ")";
118 MathCursor::MathCursor(InsetFormulaBase * formula)
119 : formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
125 void MathCursor::pushLeft(MathInset * par)
129 par->idxFirst(p.idx_, p.pos_);
130 Cursor_.push_back(p);
134 void MathCursor::pushRight(MathInset * par)
139 par->idxLast(p.idx_, p.pos_);
140 Cursor_.push_back(p);
144 bool MathCursor::popLeft()
146 if (Cursor_.size() <= 1)
152 bool MathCursor::popRight()
154 if (Cursor_.size() <= 1)
162 MathInset * MathCursor::parInset(int i) const
164 return Cursor_[i].par_;
168 void MathCursor::dump(char const * what) const
172 lyxerr << "MC: " << what << "\n";
173 for (unsigned i = 0; i < Cursor_.size(); ++i)
174 lyxerr << " i: " << i
175 << " pos: " << Cursor_[i].pos_
176 << " idx: " << Cursor_[i].idx_
177 << " par: " << Cursor_[i].par_ << "\n";
179 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
183 void MathCursor::seldump(char const * str) const
185 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
189 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
190 << str << "\ntheSelection: " << selection_
191 << " '" << theSelection.glue() << "'\n";
192 for (unsigned int i = 0; i < Cursor_.size(); ++i)
193 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
195 for (unsigned int i = 0; i < Anchor_.size(); ++i)
196 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
197 //lyxerr << "\ncursor.pos_: " << pos();
198 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
199 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
203 bool MathCursor::isInside(MathInset const * p) const
205 for (unsigned i = 0; i < Cursor_.size(); ++i)
206 if (parInset(i) == p)
212 bool MathCursor::openable(MathInset * p, bool sel) const
221 // we can't move into anything new during selection
222 if (Cursor_.size() == Anchor_.size())
224 if (p != Anchor_[Cursor_.size()].par_)
231 bool MathCursor::positionable(MathInset * p, bool sel) const
240 // we can't move into anything new during selection
241 if (Cursor_.size() == Anchor_.size())
243 if (p != Anchor_[Cursor_.size()].par_)
250 bool MathCursor::posLeft()
259 bool MathCursor::posRight()
268 bool MathCursor::left(bool sel)
273 lastcode_ = LM_TC_VAR;
277 lastcode_ = LM_TC_VAR;
279 MathInset * p = prevInset();
280 if (openable(p, sel)) {
285 return posLeft() || idxLeft() || popLeft();
289 bool MathCursor::right(bool sel)
294 lastcode_ = LM_TC_VAR;
298 lastcode_ = LM_TC_VAR;
300 MathInset * p = nextInset();
301 if (openable(p, sel)) {
306 return posRight() || idxRight() || popRight();
310 void MathCursor::first()
313 pushLeft(outerPar());
317 void MathCursor::last()
324 void MathCursor::setPos(int x, int y)
327 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
330 lastcode_ = LM_TC_VAR;
333 cursor().par_ = outerPar();
338 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
339 int distmin = 1 << 30; // large enough
340 for (int i = 0; i < par()->nargs(); ++i) {
341 MathXArray const & ar = par()->xcell(i);
342 int x1 = x - ar.xo();
343 int y1 = y - ar.yo();
344 int c = ar.x2pos(x1);
345 int xx = abs(x1 - ar.pos2x(c));
347 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
348 // << " c: " << c << " xo: " << ar.xo() << "\n";
349 if (yy + xx <= distmin) {
355 //lyxerr << "found idx: " << idx() << " cursor: "
357 MathInset * n = nextInset();
358 MathInset * p = prevInset();
359 if (positionable(n, selection_) && n->covers(x, y))
361 else if (positionable(p, selection_) && p->covers(x, y))
370 void MathCursor::home()
374 lastcode_ = LM_TC_VAR;
375 if (!par()->idxHome(idx(), pos()))
381 void MathCursor::end()
385 lastcode_ = LM_TC_VAR;
386 if (!par()->idxEnd(idx(), pos()))
392 void MathCursor::plainErase()
394 array().erase(pos());
398 void MathCursor::insert(char c, MathTextCodes t)
400 //lyxerr << "inserting '" << c << "'\n";
401 array().insert(pos(), new MathCharInset(c, t));
406 void MathCursor::insert(MathInset * p)
417 array().insert(pos(), p);
422 void MathCursor::niceInsert(MathInset * p)
425 lyxerr << "should not happen\n";
432 right(); // do not push for e.g. MathSymbolInset
435 p->metrics(p->size());
439 void MathCursor::insert(MathArray const & ar)
445 array().insert(pos(), ar);
450 void MathCursor::backspace()
471 void MathCursor::erase()
482 // delete empty cells if necessary
483 if (pos() == 0 && array().empty()) {
486 par()->idxDelete(idx(), popit, removeit);
487 if (popit && popLeft() && removeit)
499 void MathCursor::delLine()
508 if (par()->nrows() > 1)
509 par()->delRow(row());
513 bool MathCursor::up(bool sel)
522 // check whether we could move into an inset on the right or on the left
523 MathInset * p = nextInset();
526 if (p->idxFirstUp(idxx, poss)) {
538 if (p->idxLastUp(idxx, poss)) {
551 bool MathCursor::down(bool sel)
560 // check whether we could move into an inset on the right or on the left
561 MathInset * p = nextInset();
565 if (p->idxFirstDown(idxx, poss)) {
578 if (p->idxLastDown(idxx, poss)) {
591 bool MathCursor::toggleLimits()
593 MathScriptInset * p = prevScriptInset();
596 int old = p->limits();
597 p->limits(old < 0 ? 1 : -1);
598 return old != p->limits();
602 void MathCursor::setSize(MathStyles size)
604 par()->userSetSize(size);
608 void MathCursor::macroModeClose()
610 string s = macroName();
612 pos() = pos() - s.size();
613 for (unsigned i = 0; i < s.size(); ++i)
615 lastcode_ = LM_TC_VAR;
621 string MathCursor::macroName() const
625 while (p >= 0 && array().nextInset(p)->code() == LM_TC_TEX) {
626 s = array().nextInset(p)->getChar() + s;
633 void MathCursor::selCopy()
637 theSelection.grab(*this);
643 void MathCursor::selCut()
647 theSelection.grab(*this);
648 theSelection.erase(*this);
651 theSelection.clear();
656 void MathCursor::selDel()
660 theSelection.erase(*this);
666 void MathCursor::selPaste()
669 theSelection.paste(*this);
674 void MathCursor::selHandle(bool sel)
676 if (sel == selection_)
679 theSelection.clear();
685 void MathCursor::selStart()
691 theSelection.clear();
697 void MathCursor::selClear()
704 void MathCursor::drawSelection(Painter & pain) const
711 getSelection(i1, i2);
713 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
715 if (i1.idx_ == i2.idx_) {
716 MathXArray & c = i1.xcell();
717 int x1 = c.xo() + c.pos2x(i1.pos_);
718 int y1 = c.yo() - c.ascent();
719 int x2 = c.xo() + c.pos2x(i2.pos_);
720 int y2 = c.yo() + c.descent();
721 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
723 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
724 for (unsigned i = 0; i < indices.size(); ++i) {
725 MathXArray & c = i1.xcell(indices[i]);
727 int y1 = c.yo() - c.ascent();
728 int x2 = c.xo() + c.width();
729 int y2 = c.yo() + c.descent();
730 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
736 MathTextCodes MathCursor::nextCode() const
738 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
743 void MathCursor::handleFont(MathTextCodes t)
749 getSelection(i1, i2);
750 if (i1.idx_ == i2.idx_) {
751 MathArray & ar = i1.cell();
752 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
753 ar.nextInset(pos)->handleFont(t);
756 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
760 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
762 handleNest(new MathDelimInset(l, r));
766 void MathCursor::handleNest(MathInset * p)
770 p->cell(0) = theSelection.glue();
777 void MathCursor::getPos(int & x, int & y)
780 #warning This should probably take cellXOffset and cellYOffset into account
782 x = xarray().xo() + xarray().pos2x(pos());
787 MathInset * MathCursor::par() const
789 return cursor().par_;
793 InsetFormulaBase const * MathCursor::formula()
799 int MathCursor::idx() const
801 return cursor().idx_;
805 int & MathCursor::idx()
807 return cursor().idx_;
811 int MathCursor::pos() const
813 return cursor().pos_;
817 int & MathCursor::pos()
819 return cursor().pos_;
823 bool MathCursor::inMacroMode() const
825 return lastcode_ == LM_TC_TEX;
829 bool MathCursor::selection() const
835 MathArrayInset * MathCursor::enclosingArray(int & idx) const
837 for (int i = Cursor_.size() - 1; i >= 0; --i) {
838 if (Cursor_[i].par_->isArray()) {
839 idx = Cursor_[i].idx_;
840 return static_cast<MathArrayInset *>(Cursor_[i].par_);
847 void MathCursor::pullArg(bool goright)
851 MathArray a = array();
854 array().insert(pos(), a);
861 MathStyles MathCursor::style() const
863 return xarray().style();
867 void MathCursor::normalize() const
870 #warning This is evil!
872 MathCursor * it = const_cast<MathCursor *>(this);
875 lyxerr << "this should not really happen - 1: " << idx() << "\n";
876 if (idx() >= par()->nargs()) {
877 lyxerr << "this should not really happen - 2: "
878 << idx() << " " << par()->nargs() << "\n";
881 it->idx() = max(idx(), 0);
882 it->idx() = min(idx(), par()->nargs() - 1);
885 lyxerr << "this should not really happen - 3: " << pos() << "\n";
886 if (pos() > size()) {
887 lyxerr << "this should not really happen - 4: "
888 << pos() << " " << size() << "\n";
891 it->pos() = max(pos(), 0);
892 it->pos() = min(pos(), size());
896 int MathCursor::size() const
898 return array().size();
902 int MathCursor::col() const
904 return par()->col(idx());
908 int MathCursor::row() const
910 return par()->row(idx());
915 char MathCursorPos::getChar() const
917 return array().getChar(pos());
921 string MathCursorPos::readString()
924 int code = nextCode();
925 for ( ; OK() && nextCode() == code; Next())
933 MathInset * MathCursor::prevInset() const
938 return array().nextInset(pos() - 1);
942 MathInset * MathCursor::nextInset() const
945 return array().nextInset(pos());
949 MathScriptInset * MathCursor::prevScriptInset() const
952 MathInset * p = prevInset();
953 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
957 MathSpaceInset * MathCursor::prevSpaceInset() const
960 MathInset * p = prevInset();
961 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
965 MathArray & MathCursor::array() const
967 static MathArray dummy;
969 lyxerr << "############ par_ not valid\n";
973 if (idx() < 0 || idx() >= par()->nargs()) {
974 lyxerr << "############ idx_ " << idx() << " not valid\n";
978 return cursor().cell();
982 MathXArray & MathCursor::xarray() const
984 return cursor().xcell();
988 void MathCursor::idxNext()
990 par()->idxNext(idx(), pos());
994 void MathCursor::idxPrev()
996 par()->idxPrev(idx(), pos());
1000 void MathCursor::splitCell()
1002 if (idx() == par()->nargs() - 1)
1004 MathArray ar = array();
1006 array().erase(pos(), size());
1009 array().insert(0, ar);
1013 void MathCursor::breakLine()
1015 // leave inner cells
1019 MathMatrixInset * p = outerPar();
1020 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1021 p->mutate(LM_OT_EQNARRAY);
1028 const int r = row();
1029 for (int c = col() + 1; c < p->ncols(); ++c) {
1030 const int i1 = p->index(r, c);
1031 const int i2 = p->index(r + 1, c);
1032 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1033 p->cell(i1).swap(p->cell(i2));
1038 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1043 char MathCursor::valign() const
1046 MathArrayInset * p = enclosingArray(idx);
1047 return p ? p->valign() : 0;
1051 char MathCursor::halign() const
1054 MathArrayInset * p = enclosingArray(idx);
1055 return p ? p->halign(idx % p->ncols()) : 0;
1059 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1061 MathCursorPos anc = normalAnchor();
1062 if (anc < cursor()) {
1072 MathCursorPos & MathCursor::cursor()
1074 return Cursor_.back();
1078 MathCursorPos const & MathCursor::cursor() const
1080 return Cursor_.back();
1084 int MathCursor::cellXOffset() const
1086 return par()->cellXOffset(idx());
1090 int MathCursor::cellYOffset() const
1092 return par()->cellYOffset(idx());
1096 int MathCursor::xpos() const
1098 return cellXOffset() + xarray().pos2x(pos());
1102 int MathCursor::ypos() const
1104 return cellYOffset();
1109 void MathCursor::gotoX(int x)
1111 pos() = xarray().x2pos(x - cellXOffset());
1115 bool MathCursor::goUp()
1117 // first ask the inset if it knows better then we
1118 if (par()->idxUp(idx(), pos()))
1121 // if not, apply brute force.
1125 std::vector<MathCursorPos> save = Cursor_;
1126 y0 -= xarray().ascent();
1127 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1129 if (save != Cursor_ && xarray().yo() < y0)
1137 bool MathCursor::goDown()
1139 // first ask the inset if it knows better then we
1140 if (par()->idxDown(idx(), pos()))
1143 // if not, apply brute force.
1147 std::vector<MathCursorPos> save = Cursor_;
1148 y0 += xarray().descent();
1149 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1151 if (save != Cursor_ && xarray().yo() > y0)
1159 bool MathCursor::idxLeft()
1161 return par()->idxLeft(idx(), pos());
1165 bool MathCursor::idxRight()
1167 return par()->idxRight(idx(), pos());
1171 MathMatrixInset * MathCursor::outerPar() const
1174 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1178 void MathCursor::interpret(string const & s)
1180 //lyxerr << "interpret: '" << s << "'\n";
1181 //lyxerr << "in: " << in_word_set(s) << " \n";
1188 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1189 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1190 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1192 if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
1197 istringstream is(s.substr(7).c_str());
1198 is >> m >> n >> v_align >> h_align;
1202 MathArrayInset * p = new MathArrayInset(m, n);
1203 p->valign(v_align[0]);
1209 if (s == "\\over" || s == "\\choose" || s == "\\atop") {
1210 MathArray ar = array();
1211 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1212 p->cell(0).swap(array());
1221 niceInsert(createMathInset(s.substr(1)));
1226 // we got just a single char now
1228 if (c == '^' || c == '_') {
1229 bool const up = (s[0] == '^');
1231 MathScriptInset * p = prevScriptInset();
1233 MathInset * b = prevInset();
1234 if (b && b->isScriptable()) {
1235 p = new MathScriptInset(up, !up, b->clone());
1239 p = new MathScriptInset(up, !up);
1256 if (lastcode_ == LM_TC_TEXTRM) {
1257 insert(c, LM_TC_TEXTRM);
1262 if (inMacroMode()) {
1264 lastcode_ = LM_TC_VAR;
1268 MathSpaceInset * p = prevSpaceInset();
1274 if (mathcursor->popRight())
1278 // this would not work if the inset is in an table!
1279 //bv->text->cursorRight(bv, true);
1280 //result = FINISHED;
1284 if (lastcode_ == LM_TC_TEX) {
1285 if (macroName().empty()) {
1286 insert(c, LM_TC_TEX);
1289 lastcode_ = LM_TC_VAR;
1293 insert(c, LM_TC_TEX);
1296 lastcode_ = LM_TC_VAR;
1302 if (c == '{' || c == '}') {
1303 niceInsert(new MathCharInset(c, LM_TC_SPECIAL));
1307 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1308 static char const greek[26] =
1309 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1310 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1311 0, 0, 0, 0, 0 , 'Z' };
1313 MathTextCodes code = LM_TC_SYMB;
1314 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1320 #warning greek insert problem? look here!
1321 //if (lastcode_ == LM_TC_GREEK1)
1322 lastcode_ = LM_TC_VAR;
1327 lastcode_ = LM_TC_TEX;
1328 //bv->owner()->message(_("TeX mode"));
1332 // no special circumstances, so insert the character without any fuss
1338 ////////////////////////////////////////////////////////////////////////
1341 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1343 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1347 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1349 if (ti.par_ != it.par_) {
1350 lyxerr << "can't compare cursor and anchor in different insets\n";
1353 if (ti.idx_ != it.idx_)
1354 return ti.idx_ < it.idx_;
1355 return ti.pos_ < it.pos_;
1359 MathArray & MathCursorPos::cell(int idx) const
1361 return par_->cell(idx);
1365 MathArray & MathCursorPos::cell() const
1367 return par_->cell(idx_);
1371 MathXArray & MathCursorPos::xcell(int idx) const
1373 return par_->xcell(idx);
1377 MathXArray & MathCursorPos::xcell() const
1379 return par_->xcell(idx_);
1383 MathCursorPos MathCursor::normalAnchor() const
1385 // use Anchor on the same level as Cursor
1386 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1387 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1388 // anchor is behind cursor -> move anchor behind the inset