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_funcinset.h"
35 #include "math_arrayinset.h"
36 #include "math_charinset.h"
37 #include "math_decorationinset.h"
38 #include "math_deliminset.h"
39 #include "math_macro.h"
40 #include "math_macrotable.h"
41 #include "math_matrixinset.h"
42 #include "math_scopeinset.h"
43 #include "math_scriptinset.h"
44 #include "math_spaceinset.h"
45 #include "math_parser.h"
57 void grab(MathCursor const & cursor)
62 cursor.getSelection(i1, i2);
63 if (i1.idx_ == i2.idx_)
64 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
66 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
67 for (unsigned i = 0; i < indices.size(); ++i)
68 data_.push_back(i1.cell(indices[i]));
72 void erase(MathCursor & cursor)
76 cursor.getSelection(i1, i2);
77 if (i1.idx_ == i2.idx_) {
78 i1.cell().erase(i1.pos_, i2.pos_);
80 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
81 for (unsigned i = 0; i < indices.size(); ++i)
82 i1.cell(indices[i]).erase();
87 void paste(MathCursor & cursor) const
89 cursor.insert(glue());
92 // glues selection to one cell
93 MathArray glue() const
96 for (unsigned i = 0; i < data_.size(); ++i)
97 ar.push_back(data_[i]);
106 std::vector<MathArray> data_;
110 Selection theSelection;
113 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
115 os << "(par: " << p.par_ << " idx: " << p.idx_
116 << " pos: " << p.pos_ << ")";
123 MathCursor::MathCursor(InsetFormulaBase * formula)
124 : formula_(formula), lastcode_(LM_TC_VAR), selection_(false)
130 MathCursor::~MathCursor()
134 void MathCursor::pushLeft(MathInset * par)
138 par->idxFirst(p.idx_, p.pos_);
139 Cursor_.push_back(p);
143 void MathCursor::pushRight(MathInset * par)
148 par->idxLast(p.idx_, p.pos_);
149 Cursor_.push_back(p);
153 bool MathCursor::popLeft()
155 if (Cursor_.size() <= 1)
161 bool MathCursor::popRight()
163 if (Cursor_.size() <= 1)
171 MathInset * MathCursor::parInset(int i) const
173 return Cursor_[i].par_;
177 void MathCursor::dump(char const * what) const
181 lyxerr << "MC: " << what << "\n";
182 for (unsigned i = 0; i < Cursor_.size(); ++i)
183 lyxerr << " i: " << i
184 << " pos: " << Cursor_[i].pos_
185 << " idx: " << Cursor_[i].idx_
186 << " par: " << Cursor_[i].par_ << "\n";
188 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
192 void MathCursor::seldump(char const * str) const
194 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
198 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
199 << str << "\ntheSelection: " << selection_
200 << " '" << theSelection.glue() << "'\n";
201 for (unsigned int i = 0; i < Cursor_.size(); ++i)
202 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
204 for (unsigned int i = 0; i < Anchor_.size(); ++i)
205 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
206 //lyxerr << "\ncursor.pos_: " << pos();
207 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
208 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
212 bool MathCursor::isInside(MathInset const * p) const
214 for (unsigned i = 0; i < Cursor_.size(); ++i)
215 if (parInset(i) == p)
221 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
226 if (!(p->isActive() || (useupdown && p->isScriptInset())))
230 // we can't move into anything new during selection
231 if (Cursor_.size() == Anchor_.size())
233 if (p != Anchor_[Cursor_.size()].par_)
240 bool MathCursor::posLeft()
249 bool MathCursor::posRight()
258 bool MathCursor::left(bool sel)
266 lastcode_ = LM_TC_VAR;
268 MathInset * p = prevInset();
269 if (openable(p, sel, false)) {
274 return posLeft() || idxLeft() || popLeft();
278 bool MathCursor::right(bool sel)
286 lastcode_ = LM_TC_VAR;
288 MathInset * p = nextInset();
289 if (openable(p, sel, false)) {
294 return posRight() || idxRight() || popRight();
298 void MathCursor::first()
301 pushLeft(outerPar());
305 void MathCursor::last()
312 void MathCursor::setPos(int x, int y)
315 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
318 lastcode_ = LM_TC_VAR;
321 cursor().par_ = outerPar();
326 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
327 int distmin = 1 << 30; // large enough
328 for (int i = 0; i < par()->nargs(); ++i) {
329 MathXArray const & ar = par()->xcell(i);
330 int x1 = x - ar.xo();
331 int y1 = y - ar.yo();
332 int c = ar.x2pos(x1);
333 int xx = abs(x1 - ar.pos2x(c));
335 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
336 // << " c: " << c << " xo: " << ar.xo() << "\n";
337 if (yy + xx <= distmin) {
343 //lyxerr << "found idx: " << idx() << " cursor: "
345 MathInset * n = nextInset();
346 MathInset * p = prevInset();
347 if (openable(n, selection_, true) && n->covers(x, y))
349 else if (openable(p, selection_, true) && p->covers(x, y))
358 void MathCursor::home()
362 lastcode_ = LM_TC_VAR;
363 if (!par()->idxHome(idx(), pos()))
369 void MathCursor::end()
373 lastcode_ = LM_TC_VAR;
374 if (!par()->idxEnd(idx(), pos()))
380 void MathCursor::plainErase()
382 array().erase(pos());
386 void MathCursor::insert(char c, MathTextCodes t)
388 //lyxerr << "inserting '" << c << "'\n";
389 array().insert(pos(), new MathCharInset(c, t));
394 void MathCursor::insert(MathInset * p)
405 array().insert(pos(), p);
410 void MathCursor::niceInsert(MathInset * p)
413 lyxerr << "should not happen\n";
420 right(); // do not push for e.g. MathSymbolInset
423 p->metrics(p->size());
427 void MathCursor::insert(MathArray const & ar)
433 array().insert(pos(), ar);
438 void MathCursor::backspace()
459 void MathCursor::erase()
470 // delete empty cells if necessary
471 if (pos() == 0 && array().empty()) {
474 par()->idxDelete(idx(), popit, removeit);
475 if (popit && popLeft() && removeit)
487 void MathCursor::delLine()
496 if (par()->nrows() > 1)
497 par()->delRow(row());
501 bool MathCursor::up(bool sel)
510 // check whether we could move into an inset on the right or on the left
511 MathInset * p = nextInset();
514 if (p->idxFirstUp(idxx, poss)) {
526 if (p->idxLastUp(idxx, poss)) {
539 bool MathCursor::down(bool sel)
548 // check whether we could move into an inset on the right or on the left
549 MathInset * p = nextInset();
553 if (p->idxFirstDown(idxx, poss)) {
566 if (p->idxLastDown(idxx, poss)) {
579 bool MathCursor::toggleLimits()
581 MathScriptInset * p = prevScriptInset();
584 int old = p->limits();
585 p->limits(old < 0 ? 1 : -1);
586 return old != p->limits();
590 void MathCursor::setSize(MathStyles size)
592 par()->userSetSize(size);
596 void MathCursor::macroModeClose()
598 string s = macroName();
600 pos() = pos() - s.size();
601 for (unsigned i = 0; i < s.size(); ++i)
605 lastcode_ = LM_TC_VAR;
609 string MathCursor::macroName() const
613 while (p >= 0 && array().nextInset(p)->code() == LM_TC_TEX) {
614 s = array().nextInset(p)->getChar() + s;
621 void MathCursor::selCopy()
625 theSelection.grab(*this);
631 void MathCursor::selCut()
635 theSelection.grab(*this);
636 theSelection.erase(*this);
639 theSelection.clear();
644 void MathCursor::selDel()
648 theSelection.erase(*this);
654 void MathCursor::selPaste()
657 theSelection.paste(*this);
662 void MathCursor::selHandle(bool sel)
664 if (sel == selection_)
667 theSelection.clear();
673 void MathCursor::selStart()
679 theSelection.clear();
685 void MathCursor::selClear()
692 void MathCursor::drawSelection(Painter & pain) const
699 getSelection(i1, i2);
701 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
703 if (i1.idx_ == i2.idx_) {
704 MathXArray & c = i1.xcell();
705 int x1 = c.xo() + c.pos2x(i1.pos_);
706 int y1 = c.yo() - c.ascent();
707 int x2 = c.xo() + c.pos2x(i2.pos_);
708 int y2 = c.yo() + c.descent();
709 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
711 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
712 for (unsigned i = 0; i < indices.size(); ++i) {
713 MathXArray & c = i1.xcell(indices[i]);
715 int y1 = c.yo() - c.ascent();
716 int x2 = c.xo() + c.width();
717 int y2 = c.yo() + c.descent();
718 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
724 MathTextCodes MathCursor::nextCode() const
726 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
731 void MathCursor::handleFont(MathTextCodes t)
737 getSelection(i1, i2);
738 if (i1.idx_ == i2.idx_) {
739 MathArray & ar = i1.cell();
740 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
741 ar.nextInset(pos)->handleFont(t);
744 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
748 void MathCursor::handleAccent(string const & name)
750 latexkeys const * l = in_word_set(name);
752 handleNest(new MathDecorationInset(l));
756 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
758 handleNest(new MathDelimInset(l, r));
762 void MathCursor::handleNest(MathInset * p)
766 p->cell(0) = theSelection.glue();
773 void MathCursor::getPos(int & x, int & y)
776 #warning This should probably take cellXOffset and cellYOffset into account
778 x = xarray().xo() + xarray().pos2x(pos());
783 MathInset * MathCursor::par() const
785 return cursor().par_;
789 InsetFormulaBase const * MathCursor::formula()
795 int MathCursor::idx() const
797 return cursor().idx_;
801 int & MathCursor::idx()
803 return cursor().idx_;
807 int MathCursor::pos() const
809 return cursor().pos_;
813 int & MathCursor::pos()
815 return cursor().pos_;
819 bool MathCursor::inMacroMode() const
821 return lastcode_ == LM_TC_TEX;
825 bool MathCursor::selection() const
831 MathArrayInset * MathCursor::enclosingArray(int & idx) const
833 for (int i = Cursor_.size() - 1; i >= 0; --i) {
834 if (Cursor_[i].par_->isArray()) {
835 idx = Cursor_[i].idx_;
836 return static_cast<MathArrayInset *>(Cursor_[i].par_);
843 void MathCursor::pullArg(bool goright)
847 MathArray a = array();
850 array().insert(pos(), a);
857 MathStyles MathCursor::style() const
859 return xarray().style();
863 void MathCursor::normalize() const
866 #warning This is evil!
868 MathCursor * it = const_cast<MathCursor *>(this);
871 lyxerr << "this should not really happen - 1: " << idx() << "\n";
872 if (idx() >= par()->nargs()) {
873 lyxerr << "this should not really happen - 2: "
874 << idx() << " " << par()->nargs() << "\n";
877 it->idx() = max(idx(), 0);
878 it->idx() = min(idx(), par()->nargs() - 1);
881 lyxerr << "this should not really happen - 3: " << pos() << "\n";
882 if (pos() > size()) {
883 lyxerr << "this should not really happen - 4: "
884 << pos() << " " << size() << "\n";
887 it->pos() = max(pos(), 0);
888 it->pos() = min(pos(), size());
892 int MathCursor::size() const
894 return array().size();
898 int MathCursor::col() const
900 return par()->col(idx());
904 int MathCursor::row() const
906 return par()->row(idx());
911 char MathCursorPos::getChar() const
913 return array().getChar(pos());
917 string MathCursorPos::readString()
920 int code = nextCode();
921 for ( ; OK() && nextCode() == code; Next())
929 MathInset * MathCursor::prevInset() const
934 return array().nextInset(pos() - 1);
938 MathInset * MathCursor::nextInset() const
941 return array().nextInset(pos());
945 MathScriptInset * MathCursor::prevScriptInset() const
948 MathInset * p = prevInset();
949 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
953 MathSpaceInset * MathCursor::prevSpaceInset() const
956 MathInset * p = prevInset();
957 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
961 MathArray & MathCursor::array() const
963 static MathArray dummy;
965 lyxerr << "############ par_ not valid\n";
969 if (idx() < 0 || idx() >= par()->nargs()) {
970 lyxerr << "############ idx_ " << idx() << " not valid\n";
974 return cursor().cell();
978 MathXArray & MathCursor::xarray() const
980 return cursor().xcell();
984 void MathCursor::idxNext()
986 par()->idxNext(idx(), pos());
990 void MathCursor::idxPrev()
992 par()->idxPrev(idx(), pos());
996 void MathCursor::splitCell()
998 if (idx() == par()->nargs() - 1)
1000 MathArray ar = array();
1002 array().erase(pos(), size());
1005 array().insert(0, ar);
1009 void MathCursor::breakLine()
1011 // leave inner cells
1015 MathMatrixInset * p = outerPar();
1016 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1017 p->mutate(LM_OT_EQNARRAY);
1024 const int r = row();
1025 for (int c = col() + 1; c < p->ncols(); ++c) {
1026 const int i1 = p->index(r, c);
1027 const int i2 = p->index(r + 1, c);
1028 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1029 p->cell(i1).swap(p->cell(i2));
1034 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1039 char MathCursor::valign() const
1042 MathArrayInset * p = enclosingArray(idx);
1043 return p ? p->valign() : 0;
1047 char MathCursor::halign() const
1050 MathArrayInset * p = enclosingArray(idx);
1051 return p ? p->halign(idx % p->ncols()) : 0;
1055 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1057 MathCursorPos anc = normalAnchor();
1058 if (anc < cursor()) {
1068 MathCursorPos & MathCursor::cursor()
1070 return Cursor_.back();
1074 MathCursorPos const & MathCursor::cursor() const
1076 return Cursor_.back();
1080 int MathCursor::cellXOffset() const
1082 return par()->cellXOffset(idx());
1086 int MathCursor::cellYOffset() const
1088 return par()->cellYOffset(idx());
1092 int MathCursor::xpos() const
1094 return cellXOffset() + xarray().pos2x(pos());
1098 int MathCursor::ypos() const
1100 return cellYOffset();
1105 void MathCursor::gotoX(int x)
1107 pos() = xarray().x2pos(x - cellXOffset());
1111 bool MathCursor::goUp()
1113 // first ask the inset if it knows better then we
1114 if (par()->idxUp(idx(), pos()))
1117 // if not, apply brute force.
1121 std::vector<MathCursorPos> save = Cursor_;
1122 y0 -= xarray().ascent();
1123 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1125 if (save != Cursor_ && xarray().yo() < y0)
1133 bool MathCursor::goDown()
1135 // first ask the inset if it knows better then we
1136 if (par()->idxDown(idx(), pos()))
1139 // if not, apply brute force.
1143 std::vector<MathCursorPos> save = Cursor_;
1144 y0 += xarray().descent();
1145 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1147 if (save != Cursor_ && xarray().yo() > y0)
1155 bool MathCursor::idxLeft()
1157 return par()->idxLeft(idx(), pos());
1161 bool MathCursor::idxRight()
1163 return par()->idxRight(idx(), pos());
1167 MathMatrixInset * MathCursor::outerPar() const
1170 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1174 void MathCursor::interpret(string const & s)
1176 //lyxerr << "interpret: '" << s << "'\n";
1177 //lyxerr << "in: " << in_word_set(s) << " \n";
1184 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1185 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1186 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1188 if (s.size() > 8 && s.substr(0, 8) == "\\matrix ") {
1193 istringstream is(s.substr(8).c_str());
1194 is >> m >> n >> v_align >> h_align;
1198 MathArrayInset * p = new MathArrayInset(m, n);
1199 p->valign(v_align[0]);
1205 if (s == "\\over" || s == "\\choose") {
1206 MathArray ar = array();
1207 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1208 p->cell(0).swap(array());
1215 latexkeys const * l = in_word_set(s.substr(1));
1217 lastcode_ = LM_TC_VAR;
1218 niceInsert(createMathInset(l));
1222 if (MathMacroTable::hasTemplate(s.substr(1))) {
1223 niceInsert(new MathMacro(MathMacroTable::provideTemplate(s.substr(1))));
1228 niceInsert(new MathFuncInset(s.substr(1)));
1233 // we got just a single char now
1235 if (c == '^' || c == '_') {
1236 bool const up = (s[0] == '^');
1238 MathScriptInset * p = prevScriptInset();
1240 MathInset * b = prevInset();
1241 if (b && b->isScriptable()) {
1242 p = new MathScriptInset(up, !up, b->clone());
1246 p = new MathScriptInset(up, !up);
1261 niceInsert(new MathScopeInset);
1265 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1266 static char const greek[26] =
1267 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1268 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1269 0, 0, 0, 0, 0 , 'Z' };
1271 MathTextCodes code = LM_TC_SYMB;
1272 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1278 #warning greek insert problem? look here!
1279 //if (lastcode_ == LM_TC_GREEK1)
1280 lastcode_ = LM_TC_VAR;
1287 if (lastcode_ == LM_TC_TEX) {
1288 if (macroName().empty()) {
1289 insert(c, LM_TC_TEX);
1294 insert(c, LM_TC_TEX);
1301 if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
1302 if (lastcode_ != LM_TC_TEXTRM)
1303 lastcode_ = LM_TC_CONST;
1304 insert(c, lastcode_);
1308 if (strchr("+/-*<>=", c)) {
1309 if (lastcode_ != LM_TC_TEXTRM)
1310 lastcode_ = LM_TC_BOP;
1311 insert(c, lastcode_);
1315 if (strchr("#$%{|}", c)) {
1316 if (lastcode_ != LM_TC_TEXTRM)
1317 lastcode_ = LM_TC_SPECIAL;
1318 insert(c, lastcode_);
1323 if (inMacroMode()) {
1324 lastcode_ = LM_TC_VAR;
1329 if (lastcode_ == LM_TC_TEXTRM) {
1330 insert(c, LM_TC_TEXTRM);
1334 if (mathcursor->popRight())
1338 // this would not work if the inset is in an table!
1339 //bv->text->cursorRight(bv, true);
1340 //result = FINISHED;
1344 if (c == '\'' || c == '@') {
1345 insert(c, LM_TC_VAR);
1350 lastcode_ = LM_TC_TEX;
1351 //bv->owner()->message(_("TeX mode"));
1356 insert(c, lastcode_);
1364 ////////////////////////////////////////////////////////////////////////
1367 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1369 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1373 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1375 if (ti.par_ != it.par_) {
1376 lyxerr << "can't compare cursor and anchor in different insets\n";
1379 if (ti.idx_ != it.idx_)
1380 return ti.idx_ < it.idx_;
1381 return ti.pos_ < it.pos_;
1385 MathArray & MathCursorPos::cell(int idx) const
1387 return par_->cell(idx);
1391 MathArray & MathCursorPos::cell() const
1393 return par_->cell(idx_);
1397 MathXArray & MathCursorPos::xcell(int idx) const
1399 return par_->xcell(idx);
1403 MathXArray & MathCursorPos::xcell() const
1405 return par_->xcell(idx_);
1409 MathCursorPos MathCursor::normalAnchor() const
1411 // use Anchor on the same level as Cursor
1412 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1413 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1414 // anchor is behind cursor -> move anchor behind the inset