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_decorationinset.h"
37 #include "math_deliminset.h"
38 #include "math_funcinset.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), imacro_(0), selection_(false)
130 MathCursor::~MathCursor()
136 void MathCursor::pushLeft(MathInset * par)
140 par->idxFirst(p.idx_, p.pos_);
141 Cursor_.push_back(p);
145 void MathCursor::pushRight(MathInset * par)
150 par->idxLast(p.idx_, p.pos_);
151 Cursor_.push_back(p);
155 bool MathCursor::popLeft()
157 if (Cursor_.size() <= 1)
163 bool MathCursor::popRight()
165 if (Cursor_.size() <= 1)
173 MathInset * MathCursor::parInset(int i) const
175 return Cursor_[i].par_;
179 void MathCursor::dump(char const * what) const
183 lyxerr << "MC: " << what << "\n";
184 for (unsigned i = 0; i < Cursor_.size(); ++i)
185 lyxerr << " i: " << i
186 << " pos: " << Cursor_[i].pos_
187 << " idx: " << Cursor_[i].idx_
188 << " par: " << Cursor_[i].par_ << "\n";
190 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
194 void MathCursor::seldump(char const * str) const
196 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
200 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
201 << str << "\ntheSelection: " << selection_
202 << " '" << theSelection.glue() << "'\n";
203 for (unsigned int i = 0; i < Cursor_.size(); ++i)
204 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
206 for (unsigned int i = 0; i < Anchor_.size(); ++i)
207 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
208 //lyxerr << "\ncursor.pos_: " << pos();
209 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
210 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
214 bool MathCursor::isInside(MathInset const * p) const
216 for (unsigned i = 0; i < Cursor_.size(); ++i)
217 if (parInset(i) == p)
223 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
228 if (!(p->isActive() || (useupdown && p->isScriptInset())))
232 // we can't move into anything new during selection
233 if (Cursor_.size() == Anchor_.size())
235 if (p != Anchor_[Cursor_.size()].par_)
242 bool MathCursor::posLeft()
251 bool MathCursor::posRight()
260 bool MathCursor::left(bool sel)
264 // was MacroModeBack()
265 if (!imacro_->name().empty()) {
266 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
267 imacro_->metrics(imacro_->size());
273 lastcode_ = LM_TC_VAR;
275 MathInset * p = prevInset();
276 if (openable(p, sel, false)) {
281 return posLeft() || idxLeft() || popLeft();
285 bool MathCursor::right(bool sel)
293 lastcode_ = LM_TC_VAR;
295 MathInset * p = nextInset();
296 if (openable(p, sel, false)) {
301 return posRight() || idxRight() || popRight();
305 void MathCursor::first()
308 pushLeft(outerPar());
312 void MathCursor::last()
319 void MathCursor::setPos(int x, int y)
322 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
325 lastcode_ = LM_TC_VAR;
328 cursor().par_ = outerPar();
333 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
334 int distmin = 1 << 30; // large enough
335 for (int i = 0; i < par()->nargs(); ++i) {
336 MathXArray const & ar = par()->xcell(i);
337 int x1 = x - ar.xo();
338 int y1 = y - ar.yo();
339 int c = ar.x2pos(x1);
340 int xx = abs(x1 - ar.pos2x(c));
342 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
343 // << " c: " << c << " xo: " << ar.xo() << "\n";
344 if (yy + xx <= distmin) {
350 //lyxerr << "found idx: " << idx() << " cursor: "
352 MathInset * n = nextInset();
353 MathInset * p = prevInset();
354 if (openable(n, selection_, true) && n->covers(x, y))
356 else if (openable(p, selection_, true) && p->covers(x, y))
365 void MathCursor::home()
369 lastcode_ = LM_TC_VAR;
370 if (!par()->idxHome(idx(), pos()))
376 void MathCursor::end()
380 lastcode_ = LM_TC_VAR;
381 if (!par()->idxEnd(idx(), pos()))
387 void MathCursor::plainErase()
389 array().erase(pos());
393 void MathCursor::insert(char c, MathTextCodes t)
395 //lyxerr << "inserting '" << c << "'\n";
402 if (imacro_ && t != LM_TC_TEX)
406 imacro_->setName(imacro_->name() + c);
410 array().insert(pos(), new MathCharInset(c, t));
415 void MathCursor::insert(MathInset * p)
426 array().insert(pos(), p);
431 void MathCursor::niceInsert(MathInset * p)
434 lyxerr << "should not happen\n";
441 right(); // do not push for e.g. MathSymbolInset
444 p->metrics(p->size());
448 void MathCursor::insert(MathArray const & ar)
454 array().insert(pos(), ar);
459 void MathCursor::backspace()
480 void MathCursor::erase()
491 // delete empty cells if necessary
492 if (pos() == 0 && array().empty()) {
495 par()->idxDelete(idx(), popit, removeit);
496 if (popit && popLeft() && removeit)
508 void MathCursor::delLine()
517 if (par()->nrows() > 1)
518 par()->delRow(row());
522 bool MathCursor::up(bool sel)
531 // check whether we could move into an inset on the right or on the left
532 MathInset * p = nextInset();
535 if (p->idxFirstUp(idxx, poss)) {
547 if (p->idxLastUp(idxx, poss)) {
560 bool MathCursor::down(bool sel)
569 // check whether we could move into an inset on the right or on the left
570 MathInset * p = nextInset();
574 if (p->idxFirstDown(idxx, poss)) {
587 if (p->idxLastDown(idxx, poss)) {
600 bool MathCursor::toggleLimits()
602 MathScriptInset * p = prevScriptInset();
605 int old = p->limits();
606 p->limits(old < 0 ? 1 : -1);
607 return old != p->limits();
611 void MathCursor::setSize(MathStyles size)
613 par()->userSetSize(size);
618 void MathCursor::macroModeOpen()
621 imacro_ = new MathFuncInset("");
622 array().insert(pos(), imacro_);
626 lyxerr << "Math Warning: Already in macro mode" << endl;
630 void MathCursor::macroModeClose()
633 string name = imacro_->name();
637 interpret("\\" + name);
642 void MathCursor::selCopy()
646 theSelection.grab(*this);
652 void MathCursor::selCut()
656 theSelection.grab(*this);
657 theSelection.erase(*this);
660 theSelection.clear();
665 void MathCursor::selDel()
669 theSelection.erase(*this);
675 void MathCursor::selPaste()
678 theSelection.paste(*this);
683 void MathCursor::selHandle(bool sel)
685 if (sel == selection_)
688 theSelection.clear();
694 void MathCursor::selStart()
700 theSelection.clear();
706 void MathCursor::selClear()
713 void MathCursor::drawSelection(Painter & pain) const
720 getSelection(i1, i2);
722 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
724 if (i1.idx_ == i2.idx_) {
725 MathXArray & c = i1.xcell();
726 int x1 = c.xo() + c.pos2x(i1.pos_);
727 int y1 = c.yo() - c.ascent();
728 int x2 = c.xo() + c.pos2x(i2.pos_);
729 int y2 = c.yo() + c.descent();
730 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
732 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
733 for (unsigned i = 0; i < indices.size(); ++i) {
734 MathXArray & c = i1.xcell(indices[i]);
736 int y1 = c.yo() - c.ascent();
737 int x2 = c.xo() + c.width();
738 int y2 = c.yo() + c.descent();
739 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
745 MathTextCodes MathCursor::nextCode() const
747 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
752 void MathCursor::handleFont(MathTextCodes t)
758 getSelection(i1, i2);
759 if (i1.idx_ == i2.idx_) {
760 MathArray & ar = i1.cell();
761 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
762 ar.nextInset(pos)->handleFont(t);
765 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
769 void MathCursor::handleAccent(string const & name)
771 latexkeys const * l = in_word_set(name);
773 handleNest(new MathDecorationInset(l));
777 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
779 handleNest(new MathDelimInset(l, r));
783 void MathCursor::handleNest(MathInset * p)
787 p->cell(0) = theSelection.glue();
794 void MathCursor::getPos(int & x, int & y)
797 #warning This should probably take cellXOffset and cellYOffset into account
799 x = xarray().xo() + xarray().pos2x(pos());
804 MathInset * MathCursor::par() const
806 return cursor().par_;
810 InsetFormulaBase const * MathCursor::formula()
816 int MathCursor::idx() const
818 return cursor().idx_;
822 int & MathCursor::idx()
824 return cursor().idx_;
828 int MathCursor::pos() const
830 return cursor().pos_;
834 int & MathCursor::pos()
836 return cursor().pos_;
840 bool MathCursor::inMacroMode() const
846 bool MathCursor::selection() const
852 MathArrayInset * MathCursor::enclosingArray(int & idx) const
854 for (int i = Cursor_.size() - 1; i >= 0; --i) {
855 if (Cursor_[i].par_->isArray()) {
856 idx = Cursor_[i].idx_;
857 return static_cast<MathArrayInset *>(Cursor_[i].par_);
864 void MathCursor::pullArg(bool goright)
868 MathArray a = array();
871 array().insert(pos(), a);
878 MathStyles MathCursor::style() const
880 return xarray().style();
884 void MathCursor::normalize() const
887 #warning This is evil!
889 MathCursor * it = const_cast<MathCursor *>(this);
892 lyxerr << "this should not really happen - 1: " << idx() << "\n";
893 if (idx() >= par()->nargs()) {
894 lyxerr << "this should not really happen - 2: "
895 << idx() << " " << par()->nargs() << "\n";
898 it->idx() = max(idx(), 0);
899 it->idx() = min(idx(), par()->nargs() - 1);
902 lyxerr << "this should not really happen - 3: " << pos() << "\n";
903 if (pos() > size()) {
904 lyxerr << "this should not really happen - 4: "
905 << pos() << " " << size() << "\n";
908 it->pos() = max(pos(), 0);
909 it->pos() = min(pos(), size());
913 int MathCursor::size() const
915 return array().size();
919 int MathCursor::col() const
921 return par()->col(idx());
925 int MathCursor::row() const
927 return par()->row(idx());
932 char MathCursorPos::getChar() const
934 return array().getChar(pos());
938 string MathCursorPos::readString()
941 int code = nextCode();
942 for ( ; OK() && nextCode() == code; Next())
950 MathInset * MathCursor::prevInset() const
955 return array().nextInset(pos() - 1);
959 MathInset * MathCursor::nextInset() const
962 return array().nextInset(pos());
966 MathScriptInset * MathCursor::prevScriptInset() const
969 MathInset * p = prevInset();
970 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
974 MathSpaceInset * MathCursor::prevSpaceInset() const
977 MathInset * p = prevInset();
978 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
982 MathArray & MathCursor::array() const
984 static MathArray dummy;
986 lyxerr << "############ par_ not valid\n";
990 if (idx() < 0 || idx() >= par()->nargs()) {
991 lyxerr << "############ idx_ " << idx() << " not valid\n";
995 return cursor().cell();
999 MathXArray & MathCursor::xarray() const
1001 return cursor().xcell();
1005 void MathCursor::idxNext()
1007 par()->idxNext(idx(), pos());
1011 void MathCursor::idxPrev()
1013 par()->idxPrev(idx(), pos());
1017 void MathCursor::splitCell()
1019 if (idx() == par()->nargs() - 1)
1021 MathArray ar = array();
1023 array().erase(pos(), size());
1026 array().insert(0, ar);
1030 void MathCursor::breakLine()
1032 // leave inner cells
1036 MathMatrixInset * p = outerPar();
1037 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1038 p->mutate(LM_OT_EQNARRAY);
1045 const int r = row();
1046 for (int c = col() + 1; c < p->ncols(); ++c) {
1047 const int i1 = p->index(r, c);
1048 const int i2 = p->index(r + 1, c);
1049 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1050 p->cell(i1).swap(p->cell(i2));
1055 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1060 char MathCursor::valign() const
1063 MathArrayInset * p = enclosingArray(idx);
1064 return p ? p->valign() : 0;
1068 char MathCursor::halign() const
1071 MathArrayInset * p = enclosingArray(idx);
1072 return p ? p->halign(idx % p->ncols()) : 0;
1076 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1078 MathCursorPos anc = normalAnchor();
1079 if (anc < cursor()) {
1089 MathCursorPos & MathCursor::cursor()
1091 return Cursor_.back();
1095 MathCursorPos const & MathCursor::cursor() const
1097 return Cursor_.back();
1101 int MathCursor::cellXOffset() const
1103 return par()->cellXOffset(idx());
1107 int MathCursor::cellYOffset() const
1109 return par()->cellYOffset(idx());
1113 int MathCursor::xpos() const
1115 return cellXOffset() + xarray().pos2x(pos());
1119 int MathCursor::ypos() const
1121 return cellYOffset();
1126 void MathCursor::gotoX(int x)
1128 pos() = xarray().x2pos(x - cellXOffset());
1132 bool MathCursor::goUp()
1134 // first ask the inset if it knows better then we
1135 if (par()->idxUp(idx(), pos()))
1138 // if not, apply brute force.
1142 std::vector<MathCursorPos> save = Cursor_;
1143 y0 -= xarray().ascent();
1144 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1146 if (save != Cursor_ && xarray().yo() < y0)
1154 bool MathCursor::goDown()
1156 // first ask the inset if it knows better then we
1157 if (par()->idxDown(idx(), pos()))
1160 // if not, apply brute force.
1164 std::vector<MathCursorPos> save = Cursor_;
1165 y0 += xarray().descent();
1166 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1168 if (save != Cursor_ && xarray().yo() > y0)
1176 bool MathCursor::idxLeft()
1178 return par()->idxLeft(idx(), pos());
1182 bool MathCursor::idxRight()
1184 return par()->idxRight(idx(), pos());
1188 MathMatrixInset * MathCursor::outerPar() const
1191 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1195 void MathCursor::interpret(string const & s)
1197 //lyxerr << "interpret: '" << s << "'\n";
1198 //lyxerr << "in: " << in_word_set(s) << " \n";
1205 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1206 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1207 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1209 if (s.size() > 8 && s.substr(0, 8) == "\\matrix ") {
1214 istringstream is(s.substr(8).c_str());
1215 is >> m >> n >> v_align >> h_align;
1219 MathArrayInset * p = new MathArrayInset(m, n);
1220 p->valign(v_align[0]);
1226 if (s == "\\over" || s == "\\choose") {
1227 MathArray ar = array();
1228 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1229 p->cell(0).swap(array());
1236 latexkeys const * l = in_word_set(s.substr(1));
1238 lastcode_ = LM_TC_VAR;
1239 niceInsert(createMathInset(l));
1243 if (MathMacroTable::hasTemplate(s.substr(1))) {
1244 niceInsert(new MathMacro(MathMacroTable::provideTemplate(s.substr(1))));
1249 niceInsert(new MathFuncInset(s.substr(1)));
1254 // we got just a single char now
1256 if (c == '^' || c == '_') {
1257 bool const up = (s[0] == '^');
1259 MathScriptInset * p = prevScriptInset();
1261 MathInset * b = prevInset();
1262 if (b && b->isScriptable()) {
1263 p = new MathScriptInset(up, !up, b->clone());
1267 p = new MathScriptInset(up, !up);
1282 niceInsert(new MathScopeInset);
1286 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1287 static char const greek[26] =
1288 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1289 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1290 0, 0, 0, 0, 0 , 'Z' };
1292 MathTextCodes code = LM_TC_SYMB;
1293 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1299 #warning greek insert problem? look here!
1300 //if (lastcode_ == LM_TC_GREEK1)
1301 lastcode_ = LM_TC_VAR;
1305 if (c == '_' && lastcode_ == LM_TC_TEX) {
1306 lastcode_ = LM_TC_VAR;
1307 insert(c, LM_TC_SPECIAL);
1311 if ('0' <= c && c <= '9' && (lastcode_ == LM_TC_TEX || imacro_)) {
1313 lastcode_ = LM_TC_VAR;
1314 insert(c, lastcode_);
1318 if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
1319 if (lastcode_ != LM_TC_TEXTRM)
1320 lastcode_ = LM_TC_CONST;
1321 insert(c, lastcode_);
1325 if (strchr("+/-*<>=", c)) {
1326 if (lastcode_ != LM_TC_TEXTRM)
1327 lastcode_ = LM_TC_BOP;
1328 insert(c, lastcode_);
1332 if (strchr("#$%{|}", c)) {
1333 if (lastcode_ != LM_TC_TEXTRM)
1334 lastcode_ = LM_TC_SPECIAL;
1335 insert(c, lastcode_);
1341 lastcode_ = LM_TC_VAR;
1346 if (lastcode_ == LM_TC_TEXTRM) {
1347 insert(c, LM_TC_TEXTRM);
1351 if (mathcursor->popRight())
1355 // this would not work if the inset is in an table!
1356 //bv->text->cursorRight(bv, true);
1357 //result = FINISHED;
1361 if (c == '\'' || c == '@') {
1362 insert(c, LM_TC_VAR);
1369 //bv->owner()->message(_("TeX mode"));
1370 lastcode_ = LM_TC_TEX;
1375 if (lastcode_ == LM_TC_TEX) {
1377 lastcode_ = LM_TC_VAR;
1379 insert(c, lastcode_);
1387 ////////////////////////////////////////////////////////////////////////
1390 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1392 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1396 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1398 if (ti.par_ != it.par_) {
1399 lyxerr << "can't compare cursor and anchor in different insets\n";
1402 if (ti.idx_ != it.idx_)
1403 return ti.idx_ < it.idx_;
1404 return ti.pos_ < it.pos_;
1408 MathArray & MathCursorPos::cell(int idx) const
1410 return par_->cell(idx);
1414 MathArray & MathCursorPos::cell() const
1416 return par_->cell(idx_);
1420 MathXArray & MathCursorPos::xcell(int idx) const
1422 return par_->xcell(idx);
1426 MathXArray & MathCursorPos::xcell() const
1428 return par_->xcell(idx_);
1432 MathCursorPos MathCursor::normalAnchor() const
1434 // use Anchor on the same level as Cursor
1435 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1436 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1437 // anchor is behind cursor -> move anchor behind the inset