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_symbolinset.h"
35 #include "math_decorationinset.h"
36 #include "math_deliminset.h"
37 #include "math_dotsinset.h"
38 #include "math_fracinset.h"
39 #include "math_funcinset.h"
40 #include "math_funcliminset.h"
41 #include "math_gridinset.h"
42 #include "math_macro.h"
43 #include "math_macroarg.h"
44 #include "math_macrotable.h"
45 #include "math_macrotemplate.h"
46 #include "math_matrixinset.h"
47 #include "math_rootinset.h"
48 #include "math_spaceinset.h"
49 #include "math_sqrtinset.h"
50 #include "math_stackrelinset.h"
51 #include "support/lstrings.h"
52 #include "math_scriptinset.h"
53 #include "math_parser.h"
65 void grab(MathCursor const & cursor)
70 cursor.getSelection(i1, i2);
71 if (i1.idx_ == i2.idx_)
72 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
74 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
75 for (unsigned i = 0; i < indices.size(); ++i)
76 data_.push_back(i1.cell(indices[i]));
80 void erase(MathCursor & cursor)
84 cursor.getSelection(i1, i2);
85 if (i1.idx_ == i2.idx_) {
86 i1.cell().erase(i1.pos_, i2.pos_);
88 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
89 for (unsigned i = 0; i < indices.size(); ++i)
90 i1.cell(indices[i]).erase();
95 void paste(MathCursor & cursor) const
97 cursor.insert(glue());
100 // glues selection to one cell
101 MathArray glue() const
104 for (unsigned i = 0; i < data_.size(); ++i)
105 ar.push_back(data_[i]);
114 std::vector<MathArray> data_;
118 Selection theSelection;
121 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
123 os << "(par: " << p.par_ << " idx: " << p.idx_
124 << " pos: " << p.pos_ << ")";
131 MathCursor::MathCursor(InsetFormulaBase * formula)
132 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
138 MathCursor::~MathCursor()
143 void MathCursor::pushLeft(MathInset * par)
147 par->idxFirst(p.idx_, p.pos_);
148 Cursor_.push_back(p);
152 void MathCursor::pushRight(MathInset * par)
157 par->idxLast(p.idx_, p.pos_);
158 Cursor_.push_back(p);
162 bool MathCursor::popLeft()
164 if (Cursor_.size() <= 1)
170 bool MathCursor::popRight()
172 if (Cursor_.size() <= 1)
180 MathInset * MathCursor::parInset(int i) const
182 return Cursor_[i].par_;
186 void MathCursor::dump(char const * what) const
190 lyxerr << "MC: " << what << "\n";
191 for (unsigned i = 0; i < Cursor_.size(); ++i)
192 lyxerr << " i: " << i
193 << " pos: " << Cursor_[i].pos_
194 << " idx: " << Cursor_[i].idx_
195 << " par: " << Cursor_[i].par_ << "\n";
197 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
201 void MathCursor::seldump(char const * str) const
203 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
207 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
208 << str << "\ntheSelection: " << selection_
209 << " '" << theSelection.glue() << "'\n";
210 for (unsigned int i = 0; i < Cursor_.size(); ++i)
211 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
213 for (unsigned int i = 0; i < Anchor_.size(); ++i)
214 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
215 //lyxerr << "\ncursor.pos_: " << pos();
216 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
217 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
221 bool MathCursor::isInside(MathInset const * p) const
223 for (unsigned i = 0; i < Cursor_.size(); ++i)
224 if (parInset(i) == p)
230 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
235 if (!(p->isActive() || (useupdown && p->isScriptInset())))
239 // we can't move into anything new during selection
240 if (Cursor_.size() == Anchor_.size())
242 if (p != Anchor_[Cursor_.size()].par_)
249 bool MathCursor::posLeft()
258 bool MathCursor::posRight()
267 bool MathCursor::left(bool sel)
271 // was MacroModeBack()
272 if (!imacro_->name().empty()) {
273 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
274 imacro_->metrics(imacro_->size());
282 MathInset * p = prevInset();
283 if (openable(p, sel, false)) {
288 return posLeft() || idxLeft() || popLeft();
292 bool MathCursor::right(bool sel)
302 MathInset * p = nextInset();
303 if (openable(p, sel, false)) {
308 return posRight() || idxRight() || popRight();
312 void MathCursor::first()
315 pushLeft(outerPar());
319 void MathCursor::last()
326 void MathCursor::setPos(int x, int y)
329 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
332 lastcode_ = LM_TC_MIN;
335 cursor().par_ = outerPar();
340 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
341 int distmin = 1 << 30; // large enough
342 for (int i = 0; i < par()->nargs(); ++i) {
343 MathXArray const & ar = par()->xcell(i);
344 int x1 = x - ar.xo();
345 int y1 = y - ar.yo();
346 int c = ar.x2pos(x1);
347 int xx = abs(x1 - ar.pos2x(c));
349 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
350 // << " c: " << c << " xo: " << ar.xo() << "\n";
351 if (yy + xx <= distmin) {
357 //lyxerr << "found idx: " << idx() << " cursor: "
359 MathInset * n = nextInset();
360 MathInset * p = prevInset();
361 if (openable(n, selection_, true) && n->covers(x, y))
363 else if (openable(p, selection_, true) && p->covers(x, y))
372 void MathCursor::home()
377 if (!par()->idxHome(idx(), pos()))
383 void MathCursor::end()
388 if (!par()->idxEnd(idx(), pos()))
394 void MathCursor::plainErase()
396 array().erase(pos());
400 void MathCursor::insert(char c, MathTextCodes t)
402 //lyxerr << "inserting '" << c << "'\n";
409 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
413 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
414 // was MacroModeinsert(c);
415 imacro_->setName(imacro_->name() + c);
420 array().insert(pos(), c, t);
425 void MathCursor::insert(MathInset * p)
436 array().insert(pos(), p);
441 void MathCursor::insert(MathArray const & ar)
447 array().insert(pos(), ar);
452 void MathCursor::backspace()
473 void MathCursor::erase()
484 // delete empty cells if necessary
485 if (pos() == 0 && array().empty()) {
488 par()->idxDelete(idx(), popit, removeit);
489 if (popit && popLeft() && removeit)
501 void MathCursor::delLine()
510 if (par()->nrows() > 1)
511 par()->delRow(row());
515 bool MathCursor::up(bool sel)
524 // check whether we could move into an inset on the right or on the left
525 MathInset * p = nextInset();
528 if (p->idxFirstUp(idxx, poss)) {
540 if (p->idxLastUp(idxx, poss)) {
553 bool MathCursor::down(bool sel)
562 // check whether we could move into an inset on the right or on the left
563 MathInset * p = nextInset();
567 if (p->idxFirstDown(idxx, poss)) {
580 if (p->idxLastDown(idxx, poss)) {
593 bool MathCursor::toggleLimits()
595 MathScriptInset * p = prevScriptInset();
598 int old = p->limits();
599 p->limits(old < 0 ? 1 : -1);
600 return old != p->limits();
604 void MathCursor::setSize(MathStyles size)
606 par()->userSetSize(size);
610 void MathCursor::interpret(string const & s)
612 //lyxerr << "interpret: '" << s << "'\n";
613 //lyxerr << "in: " << in_word_set(s) << " \n";
618 if (s[0] == '^' || s[0] == '_') {
619 bool const up = (s[0] == '^');
621 MathScriptInset * p = prevScriptInset();
623 MathInset * b = prevInset();
624 if (b && b->isScriptable()) {
625 p = new MathScriptInset(up, !up, b->clone());
629 p = new MathScriptInset(up, !up);
644 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
645 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
646 insert(new MathSpaceInset(sp));
651 latexkeys const * l = in_word_set(s);
655 p = new MathRootInset;
656 else if (MathMacroTable::hasTemplate(s))
657 p = new MathMacro(MathMacroTable::provideTemplate(s));
658 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
663 istringstream is(s.substr(7).c_str());
664 is >> m >> n >> v_align >> h_align;
668 MathArrayInset * pp = new MathArrayInset(m, n);
669 pp->valign(v_align[0]);
674 p = new MathFuncInset(s);
678 p = new MathBigopInset(l);
682 p = new MathFuncLimInset(l);
686 p = new MathSymbolInset(l);
690 p = new MathStackrelInset;
694 p = new MathFracInset;
698 p = new MathSqrtInset;
701 case LM_TK_DECORATION:
702 p = new MathDecorationInset(l);
706 p = new MathSpaceInset(l->id);
710 p = new MathDotsInset(l);
714 p = new MathMacro(MathMacroTable::provideTemplate(s));
718 p = new MathFuncInset(l->name);
728 right(); // do not push for e.g. MathSymbolInset
731 p->metrics(p->size());
736 void MathCursor::macroModeOpen()
739 imacro_ = new MathFuncInset("");
740 array().insert(pos(), imacro_);
744 lyxerr << "Math Warning: Already in macro mode" << endl;
748 void MathCursor::macroModeClose()
751 string name = imacro_->name();
760 void MathCursor::selCopy()
764 theSelection.grab(*this);
770 void MathCursor::selCut()
774 theSelection.grab(*this);
775 theSelection.erase(*this);
778 theSelection.clear();
783 void MathCursor::selDel()
787 theSelection.erase(*this);
793 void MathCursor::selPaste()
796 theSelection.paste(*this);
801 void MathCursor::selHandle(bool sel)
803 if (sel == selection_)
806 theSelection.clear();
812 void MathCursor::selStart()
818 theSelection.clear();
824 void MathCursor::selClear()
831 void MathCursor::drawSelection(Painter & pain) const
838 getSelection(i1, i2);
840 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
842 if (i1.idx_ == i2.idx_) {
843 MathXArray & c = i1.xcell();
844 int x1 = c.xo() + c.pos2x(i1.pos_);
845 int y1 = c.yo() - c.ascent();
846 int x2 = c.xo() + c.pos2x(i2.pos_);
847 int y2 = c.yo() + c.descent();
848 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
851 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
852 for (unsigned i = 0; i < indices.size(); ++i) {
853 MathXArray & c = i1.xcell(indices[i]);
855 int y1 = c.yo() - c.ascent();
856 int x2 = c.xo() + c.width();
857 int y2 = c.yo() + c.descent();
858 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
864 void MathCursor::handleFont(MathTextCodes t)
869 getSelection(i1, i2);
870 if (i1.idx_ == i2.idx_) {
871 MathArray & ar = i1.cell();
872 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
873 if (isalnum(ar.getChar(pos))) {
874 MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
879 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
883 void MathCursor::handleAccent(string const & name)
885 latexkeys const * l = in_word_set(name);
887 handleNest(new MathDecorationInset(l));
891 void MathCursor::handleDelim(int l, int r)
893 handleNest(new MathDelimInset(l, r));
897 void MathCursor::handleNest(MathInset * p)
901 p->cell(0) = theSelection.glue();
908 void MathCursor::getPos(int & x, int & y)
911 #warning This should probably take cellXOffset and cellYOffset into account
913 x = xarray().xo() + xarray().pos2x(pos());
918 MathTextCodes MathCursor::nextCode() const
920 return array().getCode(pos());
924 MathTextCodes MathCursor::prevCode() const
926 return array().getCode(pos() - 1);
930 MathInset * MathCursor::par() const
932 return cursor().par_;
936 InsetFormulaBase const * MathCursor::formula()
942 int MathCursor::idx() const
944 return cursor().idx_;
948 int & MathCursor::idx()
950 return cursor().idx_;
954 int MathCursor::pos() const
956 return cursor().pos_;
960 int & MathCursor::pos()
962 return cursor().pos_;
966 bool MathCursor::inMacroMode() const
972 bool MathCursor::selection() const
978 void MathCursor::clearLastCode()
980 lastcode_ = LM_TC_MIN;
984 void MathCursor::setLastCode(MathTextCodes t)
990 MathTextCodes MathCursor::getLastCode() const
996 MathArrayInset * MathCursor::enclosingArray(int & idx) const
998 for (int i = Cursor_.size() - 1; i >= 0; --i) {
999 if (Cursor_[i].par_->isArray()) {
1000 idx = Cursor_[i].idx_;
1001 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1008 void MathCursor::pullArg(bool goright)
1012 MathArray a = array();
1015 array().insert(pos(), a);
1022 MathStyles MathCursor::style() const
1024 return xarray().style();
1028 void MathCursor::normalize() const
1030 #ifdef WITH_WARNINGS
1031 #warning This is evil!
1033 MathCursor * it = const_cast<MathCursor *>(this);
1036 lyxerr << "this should not really happen - 1: " << idx() << "\n";
1037 if (idx() >= par()->nargs()) {
1038 lyxerr << "this should not really happen - 2: "
1039 << idx() << " " << par()->nargs() << "\n";
1042 it->idx() = max(idx(), 0);
1043 it->idx() = min(idx(), par()->nargs() - 1);
1046 lyxerr << "this should not really happen - 3: " << pos() << "\n";
1047 if (pos() > size()) {
1048 lyxerr << "this should not really happen - 4: "
1049 << pos() << " " << size() << "\n";
1052 it->pos() = max(pos(), 0);
1053 it->pos() = min(pos(), size());
1057 int MathCursor::size() const
1059 return array().size();
1063 int MathCursor::col() const
1065 return par()->col(idx());
1069 int MathCursor::row() const
1071 return par()->row(idx());
1076 char MathCursorPos::getChar() const
1078 return array().getChar(pos());
1082 string MathCursorPos::readString()
1085 int code = nextCode();
1086 for ( ; OK() && nextCode() == code; Next())
1094 MathInset * MathCursor::prevInset() const
1099 return array().nextInset(pos() - 1);
1103 MathInset * MathCursor::nextInset() const
1106 return array().nextInset(pos());
1110 MathScriptInset * MathCursor::prevScriptInset() const
1113 MathInset * p = prevInset();
1114 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1118 MathSpaceInset * MathCursor::prevSpaceInset() const
1121 MathInset * p = prevInset();
1122 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1126 MathArray & MathCursor::array() const
1128 static MathArray dummy;
1130 lyxerr << "############ par_ not valid\n";
1134 if (idx() < 0 || idx() >= par()->nargs()) {
1135 lyxerr << "############ idx_ " << idx() << " not valid\n";
1139 return cursor().cell();
1143 MathXArray & MathCursor::xarray() const
1145 return cursor().xcell();
1149 void MathCursor::idxNext()
1151 par()->idxNext(idx(), pos());
1155 void MathCursor::idxPrev()
1157 par()->idxPrev(idx(), pos());
1161 void MathCursor::splitCell()
1163 if (idx() == par()->nargs() - 1)
1165 MathArray ar = array();
1167 array().erase(pos(), size());
1170 array().insert(0, ar);
1174 void MathCursor::breakLine()
1176 // leave inner cells
1180 MathMatrixInset * p = outerPar();
1181 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1182 p->mutate(LM_OT_EQNARRAY);
1189 const int r = row();
1190 for (int c = col() + 1; c < p->ncols(); ++c) {
1191 const int i1 = p->index(r, c);
1192 const int i2 = p->index(r + 1, c);
1193 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1194 p->cell(i1).swap(p->cell(i2));
1199 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1204 char MathCursor::valign() const
1207 MathArrayInset * p = enclosingArray(idx);
1208 return p ? p->valign() : 0;
1212 char MathCursor::halign() const
1215 MathArrayInset * p = enclosingArray(idx);
1216 return p ? p->halign(idx % p->ncols()) : 0;
1220 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1222 MathCursorPos anc = normalAnchor();
1223 if (anc < cursor()) {
1233 MathCursorPos & MathCursor::cursor()
1235 return Cursor_.back();
1239 MathCursorPos const & MathCursor::cursor() const
1241 return Cursor_.back();
1245 int MathCursor::cellXOffset() const
1247 return par()->cellXOffset(idx());
1251 int MathCursor::cellYOffset() const
1253 return par()->cellYOffset(idx());
1257 int MathCursor::xpos() const
1259 return cellXOffset() + xarray().pos2x(pos());
1263 int MathCursor::ypos() const
1265 return cellYOffset();
1270 void MathCursor::gotoX(int x)
1272 pos() = xarray().x2pos(x - cellXOffset());
1276 bool MathCursor::goUp()
1278 // first ask the inset if it knows better then we
1279 if (par()->idxUp(idx(), pos()))
1282 // if not, apply brute force.
1286 std::vector<MathCursorPos> save = Cursor_;
1287 y0 -= xarray().ascent();
1288 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1290 if (save != Cursor_ && xarray().yo() < y0)
1298 bool MathCursor::goDown()
1300 // first ask the inset if it knows better then we
1301 if (par()->idxDown(idx(), pos()))
1304 // if not, apply brute force.
1308 std::vector<MathCursorPos> save = Cursor_;
1309 y0 += xarray().descent();
1310 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1312 if (save != Cursor_ && xarray().yo() > y0)
1320 bool MathCursor::idxLeft()
1322 return par()->idxLeft(idx(), pos());
1326 bool MathCursor::idxRight()
1328 return par()->idxRight(idx(), pos());
1332 MathMatrixInset * MathCursor::outerPar() const
1335 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
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);
1364 MathArray & MathCursorPos::cell() const
1366 return par_->cell(idx_);
1370 MathXArray & MathCursorPos::xcell(int idx) const
1372 return par_->xcell(idx);
1376 MathXArray & MathCursorPos::xcell() const
1378 return par_->xcell(idx_);
1382 MathCursorPos MathCursor::normalAnchor() const
1384 // use Anchor on the same level as Cursor
1385 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1386 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1387 // anchor is behind cursor -> move anchor behind the inset