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_charinset.h"
35 #include "math_symbolinset.h"
36 #include "math_decorationinset.h"
37 #include "math_deliminset.h"
38 #include "math_dotsinset.h"
39 #include "math_fracinset.h"
40 #include "math_funcinset.h"
41 #include "math_funcliminset.h"
42 #include "math_gridinset.h"
43 #include "math_macro.h"
44 #include "math_macroarg.h"
45 #include "math_macrotable.h"
46 #include "math_macrotemplate.h"
47 #include "math_matrixinset.h"
48 #include "math_rootinset.h"
49 #include "math_spaceinset.h"
50 #include "math_sqrtinset.h"
51 #include "math_stackrelinset.h"
52 #include "support/lstrings.h"
53 #include "math_scriptinset.h"
54 #include "math_parser.h"
66 void grab(MathCursor const & cursor)
71 cursor.getSelection(i1, i2);
72 if (i1.idx_ == i2.idx_)
73 data_.push_back(MathArray(i1.cell(), 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 data_.push_back(i1.cell(indices[i]));
81 void erase(MathCursor & cursor)
85 cursor.getSelection(i1, i2);
86 if (i1.idx_ == i2.idx_) {
87 i1.cell().erase(i1.pos_, i2.pos_);
89 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
90 for (unsigned i = 0; i < indices.size(); ++i)
91 i1.cell(indices[i]).erase();
96 void paste(MathCursor & cursor) const
98 cursor.insert(glue());
101 // glues selection to one cell
102 MathArray glue() const
105 for (unsigned i = 0; i < data_.size(); ++i)
106 ar.push_back(data_[i]);
115 std::vector<MathArray> data_;
119 Selection theSelection;
122 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
124 os << "(par: " << p.par_ << " idx: " << p.idx_
125 << " pos: " << p.pos_ << ")";
132 MathCursor::MathCursor(InsetFormulaBase * formula)
133 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
139 MathCursor::~MathCursor()
144 void MathCursor::pushLeft(MathInset * par)
148 par->idxFirst(p.idx_, p.pos_);
149 Cursor_.push_back(p);
153 void MathCursor::pushRight(MathInset * par)
158 par->idxLast(p.idx_, p.pos_);
159 Cursor_.push_back(p);
163 bool MathCursor::popLeft()
165 if (Cursor_.size() <= 1)
171 bool MathCursor::popRight()
173 if (Cursor_.size() <= 1)
181 MathInset * MathCursor::parInset(int i) const
183 return Cursor_[i].par_;
187 void MathCursor::dump(char const * what) const
191 lyxerr << "MC: " << what << "\n";
192 for (unsigned i = 0; i < Cursor_.size(); ++i)
193 lyxerr << " i: " << i
194 << " pos: " << Cursor_[i].pos_
195 << " idx: " << Cursor_[i].idx_
196 << " par: " << Cursor_[i].par_ << "\n";
198 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
202 void MathCursor::seldump(char const * str) const
204 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
208 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
209 << str << "\ntheSelection: " << selection_
210 << " '" << theSelection.glue() << "'\n";
211 for (unsigned int i = 0; i < Cursor_.size(); ++i)
212 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
214 for (unsigned int i = 0; i < Anchor_.size(); ++i)
215 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
216 //lyxerr << "\ncursor.pos_: " << pos();
217 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
218 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
222 bool MathCursor::isInside(MathInset const * p) const
224 for (unsigned i = 0; i < Cursor_.size(); ++i)
225 if (parInset(i) == p)
231 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
236 if (!(p->isActive() || (useupdown && p->isScriptInset())))
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)
272 // was MacroModeBack()
273 if (!imacro_->name().empty()) {
274 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
275 imacro_->metrics(imacro_->size());
283 MathInset * p = prevInset();
284 if (openable(p, sel, false)) {
289 return posLeft() || idxLeft() || popLeft();
293 bool MathCursor::right(bool sel)
303 MathInset * p = nextInset();
304 if (openable(p, sel, false)) {
309 return posRight() || idxRight() || popRight();
313 void MathCursor::first()
316 pushLeft(outerPar());
320 void MathCursor::last()
327 void MathCursor::setPos(int x, int y)
330 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
333 lastcode_ = LM_TC_MIN;
336 cursor().par_ = outerPar();
341 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
342 int distmin = 1 << 30; // large enough
343 for (int i = 0; i < par()->nargs(); ++i) {
344 MathXArray const & ar = par()->xcell(i);
345 int x1 = x - ar.xo();
346 int y1 = y - ar.yo();
347 int c = ar.x2pos(x1);
348 int xx = abs(x1 - ar.pos2x(c));
350 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
351 // << " c: " << c << " xo: " << ar.xo() << "\n";
352 if (yy + xx <= distmin) {
358 //lyxerr << "found idx: " << idx() << " cursor: "
360 MathInset * n = nextInset();
361 MathInset * p = prevInset();
362 if (openable(n, selection_, true) && n->covers(x, y))
364 else if (openable(p, selection_, true) && p->covers(x, y))
373 void MathCursor::home()
378 if (!par()->idxHome(idx(), pos()))
384 void MathCursor::end()
389 if (!par()->idxEnd(idx(), pos()))
395 void MathCursor::plainErase()
397 array().erase(pos());
401 void MathCursor::insert(char c, MathTextCodes t)
403 //lyxerr << "inserting '" << c << "'\n";
410 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
414 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
415 // was MacroModeinsert(c);
416 imacro_->setName(imacro_->name() + c);
421 array().insert(pos(), new MathCharInset(c, t));
426 void MathCursor::insert(MathInset * p)
437 array().insert(pos(), p);
442 void MathCursor::insert(MathArray const & ar)
448 array().insert(pos(), ar);
453 void MathCursor::backspace()
474 void MathCursor::erase()
485 // delete empty cells if necessary
486 if (pos() == 0 && array().empty()) {
489 par()->idxDelete(idx(), popit, removeit);
490 if (popit && popLeft() && removeit)
502 void MathCursor::delLine()
511 if (par()->nrows() > 1)
512 par()->delRow(row());
516 bool MathCursor::up(bool sel)
525 // check whether we could move into an inset on the right or on the left
526 MathInset * p = nextInset();
529 if (p->idxFirstUp(idxx, poss)) {
541 if (p->idxLastUp(idxx, poss)) {
554 bool MathCursor::down(bool sel)
563 // check whether we could move into an inset on the right or on the left
564 MathInset * p = nextInset();
568 if (p->idxFirstDown(idxx, poss)) {
581 if (p->idxLastDown(idxx, poss)) {
594 bool MathCursor::toggleLimits()
596 MathScriptInset * p = prevScriptInset();
599 int old = p->limits();
600 p->limits(old < 0 ? 1 : -1);
601 return old != p->limits();
605 void MathCursor::setSize(MathStyles size)
607 par()->userSetSize(size);
611 void MathCursor::interpret(string const & s)
613 //lyxerr << "interpret: '" << s << "'\n";
614 //lyxerr << "in: " << in_word_set(s) << " \n";
619 if (s[0] == '^' || s[0] == '_') {
620 bool const up = (s[0] == '^');
622 MathScriptInset * p = prevScriptInset();
624 MathInset * b = prevInset();
625 if (b && b->isScriptable()) {
626 p = new MathScriptInset(up, !up, b->clone());
630 p = new MathScriptInset(up, !up);
645 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
646 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
647 insert(new MathSpaceInset(sp));
652 latexkeys const * l = in_word_set(s);
656 p = new MathRootInset;
657 else if (MathMacroTable::hasTemplate(s))
658 p = new MathMacro(MathMacroTable::provideTemplate(s));
659 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
664 istringstream is(s.substr(7).c_str());
665 is >> m >> n >> v_align >> h_align;
669 MathArrayInset * pp = new MathArrayInset(m, n);
670 pp->valign(v_align[0]);
675 p = new MathFuncInset(s);
679 p = new MathBigopInset(l);
683 p = new MathFuncLimInset(l);
687 p = new MathSymbolInset(l);
691 p = new MathStackrelInset;
695 p = new MathFracInset;
699 p = new MathSqrtInset;
702 case LM_TK_DECORATION:
703 p = new MathDecorationInset(l);
707 p = new MathSpaceInset(l->id);
711 p = new MathDotsInset(l);
715 p = new MathMacro(MathMacroTable::provideTemplate(s));
719 p = new MathFuncInset(l->name);
729 right(); // do not push for e.g. MathSymbolInset
732 p->metrics(p->size());
737 void MathCursor::macroModeOpen()
740 imacro_ = new MathFuncInset("");
741 array().insert(pos(), imacro_);
745 lyxerr << "Math Warning: Already in macro mode" << endl;
749 void MathCursor::macroModeClose()
752 string name = imacro_->name();
761 void MathCursor::selCopy()
765 theSelection.grab(*this);
771 void MathCursor::selCut()
775 theSelection.grab(*this);
776 theSelection.erase(*this);
779 theSelection.clear();
784 void MathCursor::selDel()
788 theSelection.erase(*this);
794 void MathCursor::selPaste()
797 theSelection.paste(*this);
802 void MathCursor::selHandle(bool sel)
804 if (sel == selection_)
807 theSelection.clear();
813 void MathCursor::selStart()
819 theSelection.clear();
825 void MathCursor::selClear()
832 void MathCursor::drawSelection(Painter & pain) const
839 getSelection(i1, i2);
841 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
843 if (i1.idx_ == i2.idx_) {
844 MathXArray & c = i1.xcell();
845 int x1 = c.xo() + c.pos2x(i1.pos_);
846 int y1 = c.yo() - c.ascent();
847 int x2 = c.xo() + c.pos2x(i2.pos_);
848 int y2 = c.yo() + c.descent();
849 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
852 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
853 for (unsigned i = 0; i < indices.size(); ++i) {
854 MathXArray & c = i1.xcell(indices[i]);
856 int y1 = c.yo() - c.ascent();
857 int x2 = c.xo() + c.width();
858 int y2 = c.yo() + c.descent();
859 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
865 MathTextCodes MathCursor::nextCode() const
867 return (pos() == size()) ? LM_TC_MIN : nextInset()->code();
871 void MathCursor::handleFont(MathTextCodes t)
876 getSelection(i1, i2);
877 if (i1.idx_ == i2.idx_) {
878 MathArray & ar = i1.cell();
879 for (int pos = i1.pos_; pos != i2.pos_; ++pos) {
880 MathInset * p = ar.nextInset(pos);
881 if (isalnum(p->getChar())) {
882 MathTextCodes c = (p->code() == t) ? LM_TC_VAR : t;
888 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
892 void MathCursor::handleAccent(string const & name)
894 latexkeys const * l = in_word_set(name);
896 handleNest(new MathDecorationInset(l));
900 void MathCursor::handleDelim(int l, int r)
902 handleNest(new MathDelimInset(l, r));
906 void MathCursor::handleNest(MathInset * p)
910 p->cell(0) = theSelection.glue();
917 void MathCursor::getPos(int & x, int & y)
920 #warning This should probably take cellXOffset and cellYOffset into account
922 x = xarray().xo() + xarray().pos2x(pos());
927 MathInset * MathCursor::par() const
929 return cursor().par_;
933 InsetFormulaBase const * MathCursor::formula()
939 int MathCursor::idx() const
941 return cursor().idx_;
945 int & MathCursor::idx()
947 return cursor().idx_;
951 int MathCursor::pos() const
953 return cursor().pos_;
957 int & MathCursor::pos()
959 return cursor().pos_;
963 bool MathCursor::inMacroMode() const
969 bool MathCursor::selection() const
975 void MathCursor::clearLastCode()
977 lastcode_ = LM_TC_MIN;
981 void MathCursor::setLastCode(MathTextCodes t)
987 MathTextCodes MathCursor::getLastCode() const
993 MathArrayInset * MathCursor::enclosingArray(int & idx) const
995 for (int i = Cursor_.size() - 1; i >= 0; --i) {
996 if (Cursor_[i].par_->isArray()) {
997 idx = Cursor_[i].idx_;
998 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1005 void MathCursor::pullArg(bool goright)
1009 MathArray a = array();
1012 array().insert(pos(), a);
1019 MathStyles MathCursor::style() const
1021 return xarray().style();
1025 void MathCursor::normalize() const
1027 #ifdef WITH_WARNINGS
1028 #warning This is evil!
1030 MathCursor * it = const_cast<MathCursor *>(this);
1033 lyxerr << "this should not really happen - 1: " << idx() << "\n";
1034 if (idx() >= par()->nargs()) {
1035 lyxerr << "this should not really happen - 2: "
1036 << idx() << " " << par()->nargs() << "\n";
1039 it->idx() = max(idx(), 0);
1040 it->idx() = min(idx(), par()->nargs() - 1);
1043 lyxerr << "this should not really happen - 3: " << pos() << "\n";
1044 if (pos() > size()) {
1045 lyxerr << "this should not really happen - 4: "
1046 << pos() << " " << size() << "\n";
1049 it->pos() = max(pos(), 0);
1050 it->pos() = min(pos(), size());
1054 int MathCursor::size() const
1056 return array().size();
1060 int MathCursor::col() const
1062 return par()->col(idx());
1066 int MathCursor::row() const
1068 return par()->row(idx());
1073 char MathCursorPos::getChar() const
1075 return array().getChar(pos());
1079 string MathCursorPos::readString()
1082 int code = nextCode();
1083 for ( ; OK() && nextCode() == code; Next())
1091 MathInset * MathCursor::prevInset() const
1096 return array().nextInset(pos() - 1);
1100 MathInset * MathCursor::nextInset() const
1103 return array().nextInset(pos());
1107 MathScriptInset * MathCursor::prevScriptInset() const
1110 MathInset * p = prevInset();
1111 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1115 MathSpaceInset * MathCursor::prevSpaceInset() const
1118 MathInset * p = prevInset();
1119 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1123 MathArray & MathCursor::array() const
1125 static MathArray dummy;
1127 lyxerr << "############ par_ not valid\n";
1131 if (idx() < 0 || idx() >= par()->nargs()) {
1132 lyxerr << "############ idx_ " << idx() << " not valid\n";
1136 return cursor().cell();
1140 MathXArray & MathCursor::xarray() const
1142 return cursor().xcell();
1146 void MathCursor::idxNext()
1148 par()->idxNext(idx(), pos());
1152 void MathCursor::idxPrev()
1154 par()->idxPrev(idx(), pos());
1158 void MathCursor::splitCell()
1160 if (idx() == par()->nargs() - 1)
1162 MathArray ar = array();
1164 array().erase(pos(), size());
1167 array().insert(0, ar);
1171 void MathCursor::breakLine()
1173 // leave inner cells
1177 MathMatrixInset * p = outerPar();
1178 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1179 p->mutate(LM_OT_EQNARRAY);
1186 const int r = row();
1187 for (int c = col() + 1; c < p->ncols(); ++c) {
1188 const int i1 = p->index(r, c);
1189 const int i2 = p->index(r + 1, c);
1190 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1191 p->cell(i1).swap(p->cell(i2));
1196 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1201 char MathCursor::valign() const
1204 MathArrayInset * p = enclosingArray(idx);
1205 return p ? p->valign() : 0;
1209 char MathCursor::halign() const
1212 MathArrayInset * p = enclosingArray(idx);
1213 return p ? p->halign(idx % p->ncols()) : 0;
1217 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1219 MathCursorPos anc = normalAnchor();
1220 if (anc < cursor()) {
1230 MathCursorPos & MathCursor::cursor()
1232 return Cursor_.back();
1236 MathCursorPos const & MathCursor::cursor() const
1238 return Cursor_.back();
1242 int MathCursor::cellXOffset() const
1244 return par()->cellXOffset(idx());
1248 int MathCursor::cellYOffset() const
1250 return par()->cellYOffset(idx());
1254 int MathCursor::xpos() const
1256 return cellXOffset() + xarray().pos2x(pos());
1260 int MathCursor::ypos() const
1262 return cellYOffset();
1267 void MathCursor::gotoX(int x)
1269 pos() = xarray().x2pos(x - cellXOffset());
1273 bool MathCursor::goUp()
1275 // first ask the inset if it knows better then we
1276 if (par()->idxUp(idx(), pos()))
1279 // if not, apply brute force.
1283 std::vector<MathCursorPos> save = Cursor_;
1284 y0 -= xarray().ascent();
1285 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1287 if (save != Cursor_ && xarray().yo() < y0)
1295 bool MathCursor::goDown()
1297 // first ask the inset if it knows better then we
1298 if (par()->idxDown(idx(), pos()))
1301 // if not, apply brute force.
1305 std::vector<MathCursorPos> save = Cursor_;
1306 y0 += xarray().descent();
1307 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1309 if (save != Cursor_ && xarray().yo() > y0)
1317 bool MathCursor::idxLeft()
1319 return par()->idxLeft(idx(), pos());
1323 bool MathCursor::idxRight()
1325 return par()->idxRight(idx(), pos());
1329 MathMatrixInset * MathCursor::outerPar() const
1332 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1335 ////////////////////////////////////////////////////////////////////////
1338 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1340 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1344 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1346 if (ti.par_ != it.par_) {
1347 lyxerr << "can't compare cursor and anchor in different insets\n";
1350 if (ti.idx_ != it.idx_)
1351 return ti.idx_ < it.idx_;
1352 return ti.pos_ < it.pos_;
1356 MathArray & MathCursorPos::cell(int idx) const
1358 return par_->cell(idx);
1361 MathArray & MathCursorPos::cell() const
1363 return par_->cell(idx_);
1367 MathXArray & MathCursorPos::xcell(int idx) const
1369 return par_->xcell(idx);
1373 MathXArray & MathCursorPos::xcell() const
1375 return par_->xcell(idx_);
1379 MathCursorPos MathCursor::normalAnchor() const
1381 // use Anchor on the same level as Cursor
1382 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1383 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1384 // anchor is behind cursor -> move anchor behind the inset