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_ && !(MathIsAlphaFont(t) || t == LM_TC_VAR))
406 if (MathIsAlphaFont(t) || t == LM_TC_VAR) {
407 // was MacroModeinsert(c);
408 imacro_->setName(imacro_->name() + c);
413 array().insert(pos(), new MathCharInset(c, t));
418 void MathCursor::insert(MathInset * p)
429 array().insert(pos(), p);
434 void MathCursor::niceInsert(MathInset * p)
437 lyxerr << "should not happen\n";
444 right(); // do not push for e.g. MathSymbolInset
447 p->metrics(p->size());
451 void MathCursor::insert(MathArray const & ar)
457 array().insert(pos(), ar);
462 void MathCursor::backspace()
483 void MathCursor::erase()
494 // delete empty cells if necessary
495 if (pos() == 0 && array().empty()) {
498 par()->idxDelete(idx(), popit, removeit);
499 if (popit && popLeft() && removeit)
511 void MathCursor::delLine()
520 if (par()->nrows() > 1)
521 par()->delRow(row());
525 bool MathCursor::up(bool sel)
534 // check whether we could move into an inset on the right or on the left
535 MathInset * p = nextInset();
538 if (p->idxFirstUp(idxx, poss)) {
550 if (p->idxLastUp(idxx, poss)) {
563 bool MathCursor::down(bool sel)
572 // check whether we could move into an inset on the right or on the left
573 MathInset * p = nextInset();
577 if (p->idxFirstDown(idxx, poss)) {
590 if (p->idxLastDown(idxx, poss)) {
603 bool MathCursor::toggleLimits()
605 MathScriptInset * p = prevScriptInset();
608 int old = p->limits();
609 p->limits(old < 0 ? 1 : -1);
610 return old != p->limits();
614 void MathCursor::setSize(MathStyles size)
616 par()->userSetSize(size);
621 void MathCursor::macroModeOpen()
624 imacro_ = new MathFuncInset("");
625 array().insert(pos(), imacro_);
629 lyxerr << "Math Warning: Already in macro mode" << endl;
633 void MathCursor::macroModeClose()
636 string name = imacro_->name();
640 interpret("\\" + name);
645 void MathCursor::selCopy()
649 theSelection.grab(*this);
655 void MathCursor::selCut()
659 theSelection.grab(*this);
660 theSelection.erase(*this);
663 theSelection.clear();
668 void MathCursor::selDel()
672 theSelection.erase(*this);
678 void MathCursor::selPaste()
681 theSelection.paste(*this);
686 void MathCursor::selHandle(bool sel)
688 if (sel == selection_)
691 theSelection.clear();
697 void MathCursor::selStart()
703 theSelection.clear();
709 void MathCursor::selClear()
716 void MathCursor::drawSelection(Painter & pain) const
723 getSelection(i1, i2);
725 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
727 if (i1.idx_ == i2.idx_) {
728 MathXArray & c = i1.xcell();
729 int x1 = c.xo() + c.pos2x(i1.pos_);
730 int y1 = c.yo() - c.ascent();
731 int x2 = c.xo() + c.pos2x(i2.pos_);
732 int y2 = c.yo() + c.descent();
733 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
735 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
736 for (unsigned i = 0; i < indices.size(); ++i) {
737 MathXArray & c = i1.xcell(indices[i]);
739 int y1 = c.yo() - c.ascent();
740 int x2 = c.xo() + c.width();
741 int y2 = c.yo() + c.descent();
742 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
748 MathTextCodes MathCursor::nextCode() const
750 //return (pos() == size()) ? LM_TC_VAR : nextInset()->code();
755 void MathCursor::handleFont(MathTextCodes t)
761 getSelection(i1, i2);
762 if (i1.idx_ == i2.idx_) {
763 MathArray & ar = i1.cell();
764 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
765 ar.nextInset(pos)->handleFont(t);
768 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
772 void MathCursor::handleAccent(string const & name)
774 latexkeys const * l = in_word_set(name);
776 handleNest(new MathDecorationInset(l));
780 void MathCursor::handleDelim(latexkeys const * l, latexkeys const * r)
782 handleNest(new MathDelimInset(l, r));
786 void MathCursor::handleNest(MathInset * p)
790 p->cell(0) = theSelection.glue();
797 void MathCursor::getPos(int & x, int & y)
800 #warning This should probably take cellXOffset and cellYOffset into account
802 x = xarray().xo() + xarray().pos2x(pos());
807 MathInset * MathCursor::par() const
809 return cursor().par_;
813 InsetFormulaBase const * MathCursor::formula()
819 int MathCursor::idx() const
821 return cursor().idx_;
825 int & MathCursor::idx()
827 return cursor().idx_;
831 int MathCursor::pos() const
833 return cursor().pos_;
837 int & MathCursor::pos()
839 return cursor().pos_;
843 bool MathCursor::inMacroMode() const
849 bool MathCursor::selection() const
855 MathArrayInset * MathCursor::enclosingArray(int & idx) const
857 for (int i = Cursor_.size() - 1; i >= 0; --i) {
858 if (Cursor_[i].par_->isArray()) {
859 idx = Cursor_[i].idx_;
860 return static_cast<MathArrayInset *>(Cursor_[i].par_);
867 void MathCursor::pullArg(bool goright)
871 MathArray a = array();
874 array().insert(pos(), a);
881 MathStyles MathCursor::style() const
883 return xarray().style();
887 void MathCursor::normalize() const
890 #warning This is evil!
892 MathCursor * it = const_cast<MathCursor *>(this);
895 lyxerr << "this should not really happen - 1: " << idx() << "\n";
896 if (idx() >= par()->nargs()) {
897 lyxerr << "this should not really happen - 2: "
898 << idx() << " " << par()->nargs() << "\n";
901 it->idx() = max(idx(), 0);
902 it->idx() = min(idx(), par()->nargs() - 1);
905 lyxerr << "this should not really happen - 3: " << pos() << "\n";
906 if (pos() > size()) {
907 lyxerr << "this should not really happen - 4: "
908 << pos() << " " << size() << "\n";
911 it->pos() = max(pos(), 0);
912 it->pos() = min(pos(), size());
916 int MathCursor::size() const
918 return array().size();
922 int MathCursor::col() const
924 return par()->col(idx());
928 int MathCursor::row() const
930 return par()->row(idx());
935 char MathCursorPos::getChar() const
937 return array().getChar(pos());
941 string MathCursorPos::readString()
944 int code = nextCode();
945 for ( ; OK() && nextCode() == code; Next())
953 MathInset * MathCursor::prevInset() const
958 return array().nextInset(pos() - 1);
962 MathInset * MathCursor::nextInset() const
965 return array().nextInset(pos());
969 MathScriptInset * MathCursor::prevScriptInset() const
972 MathInset * p = prevInset();
973 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
977 MathSpaceInset * MathCursor::prevSpaceInset() const
980 MathInset * p = prevInset();
981 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
985 MathArray & MathCursor::array() const
987 static MathArray dummy;
989 lyxerr << "############ par_ not valid\n";
993 if (idx() < 0 || idx() >= par()->nargs()) {
994 lyxerr << "############ idx_ " << idx() << " not valid\n";
998 return cursor().cell();
1002 MathXArray & MathCursor::xarray() const
1004 return cursor().xcell();
1008 void MathCursor::idxNext()
1010 par()->idxNext(idx(), pos());
1014 void MathCursor::idxPrev()
1016 par()->idxPrev(idx(), pos());
1020 void MathCursor::splitCell()
1022 if (idx() == par()->nargs() - 1)
1024 MathArray ar = array();
1026 array().erase(pos(), size());
1029 array().insert(0, ar);
1033 void MathCursor::breakLine()
1035 // leave inner cells
1039 MathMatrixInset * p = outerPar();
1040 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1041 p->mutate(LM_OT_EQNARRAY);
1048 const int r = row();
1049 for (int c = col() + 1; c < p->ncols(); ++c) {
1050 const int i1 = p->index(r, c);
1051 const int i2 = p->index(r + 1, c);
1052 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1053 p->cell(i1).swap(p->cell(i2));
1058 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1063 char MathCursor::valign() const
1066 MathArrayInset * p = enclosingArray(idx);
1067 return p ? p->valign() : 0;
1071 char MathCursor::halign() const
1074 MathArrayInset * p = enclosingArray(idx);
1075 return p ? p->halign(idx % p->ncols()) : 0;
1079 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1081 MathCursorPos anc = normalAnchor();
1082 if (anc < cursor()) {
1092 MathCursorPos & MathCursor::cursor()
1094 return Cursor_.back();
1098 MathCursorPos const & MathCursor::cursor() const
1100 return Cursor_.back();
1104 int MathCursor::cellXOffset() const
1106 return par()->cellXOffset(idx());
1110 int MathCursor::cellYOffset() const
1112 return par()->cellYOffset(idx());
1116 int MathCursor::xpos() const
1118 return cellXOffset() + xarray().pos2x(pos());
1122 int MathCursor::ypos() const
1124 return cellYOffset();
1129 void MathCursor::gotoX(int x)
1131 pos() = xarray().x2pos(x - cellXOffset());
1135 bool MathCursor::goUp()
1137 // first ask the inset if it knows better then we
1138 if (par()->idxUp(idx(), pos()))
1141 // if not, apply brute force.
1145 std::vector<MathCursorPos> save = Cursor_;
1146 y0 -= xarray().ascent();
1147 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1149 if (save != Cursor_ && xarray().yo() < y0)
1157 bool MathCursor::goDown()
1159 // first ask the inset if it knows better then we
1160 if (par()->idxDown(idx(), pos()))
1163 // if not, apply brute force.
1167 std::vector<MathCursorPos> save = Cursor_;
1168 y0 += xarray().descent();
1169 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1171 if (save != Cursor_ && xarray().yo() > y0)
1179 bool MathCursor::idxLeft()
1181 return par()->idxLeft(idx(), pos());
1185 bool MathCursor::idxRight()
1187 return par()->idxRight(idx(), pos());
1191 MathMatrixInset * MathCursor::outerPar() const
1194 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1197 ////////////////////////////////////////////////////////////////////////
1200 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1202 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1206 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1208 if (ti.par_ != it.par_) {
1209 lyxerr << "can't compare cursor and anchor in different insets\n";
1212 if (ti.idx_ != it.idx_)
1213 return ti.idx_ < it.idx_;
1214 return ti.pos_ < it.pos_;
1218 MathArray & MathCursorPos::cell(int idx) const
1220 return par_->cell(idx);
1224 MathArray & MathCursorPos::cell() const
1226 return par_->cell(idx_);
1230 MathXArray & MathCursorPos::xcell(int idx) const
1232 return par_->xcell(idx);
1236 MathXArray & MathCursorPos::xcell() const
1238 return par_->xcell(idx_);
1242 MathCursorPos MathCursor::normalAnchor() const
1244 // use Anchor on the same level as Cursor
1245 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1246 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1247 // anchor is behind cursor -> move anchor behind the inset
1254 void MathCursor::interpret(string const & s)
1256 //lyxerr << "interpret: '" << s << "'\n";
1257 //lyxerr << "in: " << in_word_set(s) << " \n";
1264 //lyxerr << "char: '" << c << "' int: " << int(c) << endl;
1265 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
1266 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
1268 if (s.size() > 8 && s.substr(0, 8) == "\\matrix ") {
1273 istringstream is(s.substr(8).c_str());
1274 is >> m >> n >> v_align >> h_align;
1278 MathArrayInset * p = new MathArrayInset(m, n);
1279 p->valign(v_align[0]);
1285 if (s == "\\over" || s == "\\choose") {
1286 MathArray ar = array();
1287 MathInset * p = createMathInset(in_word_set(s.substr(1)));
1288 p->cell(0).swap(array());
1295 latexkeys const * l = in_word_set(s.substr(1));
1297 lastcode_ = LM_TC_VAR;
1298 niceInsert(createMathInset(l));
1302 if (MathMacroTable::hasTemplate(s.substr(1))) {
1303 niceInsert(new MathMacro(MathMacroTable::provideTemplate(s.substr(1))));
1308 niceInsert(new MathFuncInset(s.substr(1)));
1313 // we got just a single char now
1315 if (c == '^' || c == '_') {
1316 bool const up = (s[0] == '^');
1318 MathScriptInset * p = prevScriptInset();
1320 MathInset * b = prevInset();
1321 if (b && b->isScriptable()) {
1322 p = new MathScriptInset(up, !up, b->clone());
1326 p = new MathScriptInset(up, !up);
1341 niceInsert(new MathScopeInset);
1345 if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) {
1346 static char const greek[26] =
1347 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1348 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1349 0, 0, 0, 0, 0 , 'Z' };
1351 MathTextCodes code = LM_TC_SYMB;
1352 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1358 #warning greek insert problem? look here!
1359 //if (lastcode_ == LM_TC_GREEK1)
1360 lastcode_ = LM_TC_VAR;
1364 if (c == '_' && lastcode_ == LM_TC_TEX) {
1365 lastcode_ = LM_TC_VAR;
1366 insert(c, LM_TC_SPECIAL);
1370 if ('0' <= c && c <= '9' && (lastcode_ == LM_TC_TEX || imacro_)) {
1372 lastcode_ = LM_TC_VAR;
1373 insert(c, lastcode_);
1377 if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
1378 if (lastcode_ != LM_TC_TEXTRM)
1379 lastcode_ = LM_TC_CONST;
1380 insert(c, lastcode_);
1384 if (strchr("+/-*<>=", c)) {
1385 if (lastcode_ != LM_TC_TEXTRM)
1386 lastcode_ = LM_TC_BOP;
1387 insert(c, lastcode_);
1391 if (strchr("#$%{|}", c)) {
1392 if (lastcode_ != LM_TC_TEXTRM)
1393 lastcode_ = LM_TC_SPECIAL;
1394 insert(c, lastcode_);
1400 lastcode_ = LM_TC_VAR;
1405 if (lastcode_ == LM_TC_TEXTRM) {
1406 insert(c, LM_TC_TEXTRM);
1410 if (mathcursor->popRight())
1414 // this would not work if the inset is in an table!
1415 //bv->text->cursorRight(bv, true);
1416 //result = FINISHED;
1420 if (c == '\'' || c == '@') {
1421 insert(c, LM_TC_VAR);
1428 //bv->owner()->message(_("TeX mode"));
1429 lastcode_ = LM_TC_TEX;
1434 if (lastcode_ == LM_TC_TEX) {
1436 lastcode_ = LM_TC_VAR;
1438 insert(c, lastcode_);