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 "support/lstrings.h"
51 #include "math_scriptinset.h"
52 #include "math_parser.h"
64 void grab(MathCursor const & cursor)
69 cursor.getSelection(i1, i2);
70 if (i1.idx_ == i2.idx_)
71 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
73 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
74 for (unsigned i = 0; i < indices.size(); ++i)
75 data_.push_back(i1.cell(indices[i]));
79 void erase(MathCursor & cursor)
83 cursor.getSelection(i1, i2);
84 if (i1.idx_ == i2.idx_) {
85 i1.cell().erase(i1.pos_, i2.pos_);
87 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
88 for (unsigned i = 0; i < indices.size(); ++i)
89 i1.cell(indices[i]).erase();
94 void paste(MathCursor & cursor) const
96 cursor.insert(glue());
99 // glues selection to one cell
100 MathArray glue() const
103 for (unsigned i = 0; i < data_.size(); ++i)
104 ar.push_back(data_[i]);
113 std::vector<MathArray> data_;
117 Selection theSelection;
120 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
122 os << "(par: " << p.par_ << " idx: " << p.idx_
123 << " pos: " << p.pos_ << ")";
130 MathCursor::MathCursor(InsetFormulaBase * formula)
131 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
137 MathCursor::~MathCursor()
142 void MathCursor::pushLeft(MathInset * par)
146 par->idxFirst(p.idx_, p.pos_);
147 Cursor_.push_back(p);
151 void MathCursor::pushRight(MathInset * par)
156 par->idxLast(p.idx_, p.pos_);
157 Cursor_.push_back(p);
161 bool MathCursor::popLeft()
163 if (Cursor_.size() <= 1)
169 bool MathCursor::popRight()
171 if (Cursor_.size() <= 1)
179 MathInset * MathCursor::parInset(int i) const
181 return Cursor_[i].par_;
185 void MathCursor::dump(char const * what) const
189 lyxerr << "MC: " << what << "\n";
190 for (unsigned i = 0; i < Cursor_.size(); ++i)
191 lyxerr << " i: " << i
192 << " pos: " << Cursor_[i].pos_
193 << " idx: " << Cursor_[i].idx_
194 << " par: " << Cursor_[i].par_ << "\n";
196 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
200 void MathCursor::seldump(char const * str) const
202 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
206 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
207 << str << "\ntheSelection: " << selection_
208 << " '" << theSelection.glue() << "'\n";
209 for (unsigned int i = 0; i < Cursor_.size(); ++i)
210 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
212 for (unsigned int i = 0; i < Anchor_.size(); ++i)
213 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
214 //lyxerr << "\ncursor.pos_: " << pos();
215 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
216 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
220 bool MathCursor::isInside(MathInset const * p) const
222 for (unsigned i = 0; i < Cursor_.size(); ++i)
223 if (parInset(i) == p)
229 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
234 if (!(p->isActive() || (useupdown && p->isScriptInset())))
238 // we can't move into anything new during selection
239 if (Cursor_.size() == Anchor_.size())
241 if (p != Anchor_[Cursor_.size()].par_)
248 bool MathCursor::posLeft()
257 bool MathCursor::posRight()
266 bool MathCursor::left(bool sel)
270 // was MacroModeBack()
271 if (!imacro_->name().empty()) {
272 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
273 imacro_->metrics(imacro_->size());
281 MathInset * p = prevInset();
282 if (openable(p, sel, false)) {
287 return posLeft() || idxLeft() || popLeft();
291 bool MathCursor::right(bool sel)
301 MathInset * p = nextInset();
302 if (openable(p, sel, false)) {
307 return posRight() || idxRight() || popRight();
311 void MathCursor::first()
314 pushLeft(outerPar());
318 void MathCursor::last()
325 void MathCursor::setPos(int x, int y)
328 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
331 lastcode_ = LM_TC_MIN;
334 cursor().par_ = outerPar();
339 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
340 int distmin = 1 << 30; // large enough
341 for (int i = 0; i < par()->nargs(); ++i) {
342 MathXArray const & ar = par()->xcell(i);
343 int x1 = x - ar.xo();
344 int y1 = y - ar.yo();
345 int c = ar.x2pos(x1);
346 int xx = abs(x1 - ar.pos2x(c));
348 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
349 // << " c: " << c << " xo: " << ar.xo() << "\n";
350 if (yy + xx <= distmin) {
356 //lyxerr << "found idx: " << idx() << " cursor: "
358 MathInset * n = nextInset();
359 MathInset * p = prevInset();
360 if (openable(n, selection_, true) && n->covers(x, y))
362 else if (openable(p, selection_, true) && p->covers(x, y))
371 void MathCursor::home()
376 if (!par()->idxHome(idx(), pos()))
382 void MathCursor::end()
387 if (!par()->idxEnd(idx(), pos()))
393 void MathCursor::plainErase()
395 array().erase(pos());
399 void MathCursor::insert(char c, MathTextCodes t)
401 //lyxerr << "inserting '" << c << "'\n";
408 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
412 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
413 // was MacroModeinsert(c);
414 imacro_->setName(imacro_->name() + c);
419 array().insert(pos(), c, t);
424 void MathCursor::insert(MathInset * p)
435 array().insert(pos(), p);
440 void MathCursor::insert(MathArray const & ar)
446 array().insert(pos(), ar);
451 void MathCursor::backspace()
472 void MathCursor::erase()
483 // delete empty cells if necessary
484 if (pos() == 0 && array().empty()) {
487 par()->idxDelete(idx(), popit, removeit);
488 if (popit && popLeft() && removeit)
500 void MathCursor::delLine()
509 if (par()->nrows() > 1)
510 par()->delRow(row());
514 bool MathCursor::up(bool sel)
523 // check whether we could move into an inset on the right or on the left
524 MathInset * p = nextInset();
527 if (p->idxFirstUp(idxx, poss)) {
539 if (p->idxLastUp(idxx, poss)) {
552 bool MathCursor::down(bool sel)
561 // check whether we could move into an inset on the right or on the left
562 MathInset * p = nextInset();
566 if (p->idxFirstDown(idxx, poss)) {
579 if (p->idxLastDown(idxx, poss)) {
592 bool MathCursor::toggleLimits()
594 MathScriptInset * p = prevScriptInset();
597 int old = p->limits();
598 p->limits(old < 0 ? 1 : -1);
599 return old != p->limits();
603 void MathCursor::setSize(MathStyles size)
605 par()->userSetSize(size);
609 void MathCursor::interpret(string const & s)
611 //lyxerr << "interpret: '" << s << "'\n";
612 //lyxerr << "in: " << in_word_set(s) << " \n";
617 if (s[0] == '^' || s[0] == '_') {
618 bool const up = (s[0] == '^');
620 MathScriptInset * p = prevScriptInset();
622 MathInset * b = prevInset();
623 if (b && b->isScriptable()) {
624 p = new MathScriptInset(up, !up, b->clone());
628 p = new MathScriptInset(up, !up);
643 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
644 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
645 insert(new MathSpaceInset(sp));
650 latexkeys const * l = in_word_set(s);
654 p = new MathRootInset;
655 else if (MathMacroTable::hasTemplate(s))
656 p = new MathMacro(MathMacroTable::provideTemplate(s));
657 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
662 istringstream is(s.substr(7).c_str());
663 is >> m >> n >> v_align >> h_align;
667 MathArrayInset * pp = new MathArrayInset(m, n);
668 pp->valign(v_align[0]);
673 p = new MathFuncInset(s);
677 p = new MathBigopInset(l);
681 p = new MathFuncLimInset(l);
685 p = new MathSymbolInset(l);
689 p = new MathFracInset("stackrel");
693 p = new MathFracInset("frac");
697 p = new MathSqrtInset;
700 case LM_TK_DECORATION:
701 p = new MathDecorationInset(l);
705 p = new MathSpaceInset(l->id);
709 p = new MathDotsInset(l);
713 p = new MathMacro(MathMacroTable::provideTemplate(s));
717 p = new MathFuncInset(l->name);
727 right(); // do not push for e.g. MathSymbolInset
730 p->metrics(p->size());
735 void MathCursor::macroModeOpen()
738 imacro_ = new MathFuncInset("");
739 array().insert(pos(), imacro_);
743 lyxerr << "Math Warning: Already in macro mode" << endl;
747 void MathCursor::macroModeClose()
750 string name = imacro_->name();
759 void MathCursor::selCopy()
763 theSelection.grab(*this);
769 void MathCursor::selCut()
773 theSelection.grab(*this);
774 theSelection.erase(*this);
777 theSelection.clear();
782 void MathCursor::selDel()
786 theSelection.erase(*this);
792 void MathCursor::selPaste()
795 theSelection.paste(*this);
800 void MathCursor::selHandle(bool sel)
802 if (sel == selection_)
805 theSelection.clear();
811 void MathCursor::selStart()
817 theSelection.clear();
823 void MathCursor::selClear()
830 void MathCursor::drawSelection(Painter & pain) const
837 getSelection(i1, i2);
839 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
841 if (i1.idx_ == i2.idx_) {
842 MathXArray & c = i1.xcell();
843 int x1 = c.xo() + c.pos2x(i1.pos_);
844 int y1 = c.yo() - c.ascent();
845 int x2 = c.xo() + c.pos2x(i2.pos_);
846 int y2 = c.yo() + c.descent();
847 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
850 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
851 for (unsigned i = 0; i < indices.size(); ++i) {
852 MathXArray & c = i1.xcell(indices[i]);
854 int y1 = c.yo() - c.ascent();
855 int x2 = c.xo() + c.width();
856 int y2 = c.yo() + c.descent();
857 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
863 void MathCursor::handleFont(MathTextCodes t)
868 getSelection(i1, i2);
869 if (i1.idx_ == i2.idx_) {
870 MathArray & ar = i1.cell();
871 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
872 if (isalnum(ar.getChar(pos))) {
873 MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
878 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
882 void MathCursor::handleAccent(string const & name)
884 latexkeys const * l = in_word_set(name);
886 handleNest(new MathDecorationInset(l));
890 void MathCursor::handleDelim(int l, int r)
892 handleNest(new MathDelimInset(l, r));
896 void MathCursor::handleNest(MathInset * p)
900 p->cell(0) = theSelection.glue();
907 void MathCursor::getPos(int & x, int & y)
910 #warning This should probably take cellXOffset and cellYOffset into account
912 x = xarray().xo() + xarray().pos2x(pos());
917 MathTextCodes MathCursor::nextCode() const
919 return array().getCode(pos());
923 MathTextCodes MathCursor::prevCode() const
925 return array().getCode(pos() - 1);
929 MathInset * MathCursor::par() const
931 return cursor().par_;
935 InsetFormulaBase const * MathCursor::formula()
941 int MathCursor::idx() const
943 return cursor().idx_;
947 int & MathCursor::idx()
949 return cursor().idx_;
953 int MathCursor::pos() const
955 return cursor().pos_;
959 int & MathCursor::pos()
961 return cursor().pos_;
965 bool MathCursor::inMacroMode() const
971 bool MathCursor::selection() const
977 void MathCursor::clearLastCode()
979 lastcode_ = LM_TC_MIN;
983 void MathCursor::setLastCode(MathTextCodes t)
989 MathTextCodes MathCursor::getLastCode() const
995 MathArrayInset * MathCursor::enclosingArray(int & idx) const
997 for (int i = Cursor_.size() - 1; i >= 0; --i) {
998 if (Cursor_[i].par_->isArray()) {
999 idx = Cursor_[i].idx_;
1000 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1007 void MathCursor::pullArg(bool goright)
1011 MathArray a = array();
1014 array().insert(pos(), a);
1021 MathStyles MathCursor::style() const
1023 return xarray().style();
1027 void MathCursor::normalize() const
1029 #ifdef WITH_WARNINGS
1030 #warning This is evil!
1032 MathCursor * it = const_cast<MathCursor *>(this);
1035 lyxerr << "this should not really happen - 1: " << idx() << "\n";
1036 if (idx() >= par()->nargs()) {
1037 lyxerr << "this should not really happen - 2: "
1038 << idx() << " " << par()->nargs() << "\n";
1041 it->idx() = max(idx(), 0);
1042 it->idx() = min(idx(), par()->nargs() - 1);
1045 lyxerr << "this should not really happen - 3: " << pos() << "\n";
1046 if (pos() > size()) {
1047 lyxerr << "this should not really happen - 4: "
1048 << pos() << " " << size() << "\n";
1051 it->pos() = max(pos(), 0);
1052 it->pos() = min(pos(), size());
1056 int MathCursor::size() const
1058 return array().size();
1062 int MathCursor::col() const
1064 return par()->col(idx());
1068 int MathCursor::row() const
1070 return par()->row(idx());
1075 char MathCursorPos::getChar() const
1077 return array().getChar(pos());
1081 string MathCursorPos::readString()
1084 int code = nextCode();
1085 for ( ; OK() && nextCode() == code; Next())
1093 MathInset * MathCursor::prevInset() const
1098 return array().nextInset(pos() - 1);
1102 MathInset * MathCursor::nextInset() const
1105 return array().nextInset(pos());
1109 MathScriptInset * MathCursor::prevScriptInset() const
1112 MathInset * p = prevInset();
1113 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1117 MathSpaceInset * MathCursor::prevSpaceInset() const
1120 MathInset * p = prevInset();
1121 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1125 MathArray & MathCursor::array() const
1127 static MathArray dummy;
1129 lyxerr << "############ par_ not valid\n";
1133 if (idx() < 0 || idx() >= par()->nargs()) {
1134 lyxerr << "############ idx_ " << idx() << " not valid\n";
1138 return cursor().cell();
1142 MathXArray & MathCursor::xarray() const
1144 return cursor().xcell();
1148 void MathCursor::idxNext()
1150 par()->idxNext(idx(), pos());
1154 void MathCursor::idxPrev()
1156 par()->idxPrev(idx(), pos());
1160 void MathCursor::splitCell()
1162 if (idx() == par()->nargs() - 1)
1164 MathArray ar = array();
1166 array().erase(pos(), size());
1169 array().insert(0, ar);
1173 void MathCursor::breakLine()
1175 // leave inner cells
1179 MathMatrixInset * p = outerPar();
1180 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1181 p->mutate(LM_OT_EQNARRAY);
1188 const int r = row();
1189 for (int c = col() + 1; c < p->ncols(); ++c) {
1190 const int i1 = p->index(r, c);
1191 const int i2 = p->index(r + 1, c);
1192 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1193 p->cell(i1).swap(p->cell(i2));
1198 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1203 char MathCursor::valign() const
1206 MathArrayInset * p = enclosingArray(idx);
1207 return p ? p->valign() : 0;
1211 char MathCursor::halign() const
1214 MathArrayInset * p = enclosingArray(idx);
1215 return p ? p->halign(idx % p->ncols()) : 0;
1219 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1221 MathCursorPos anc = normalAnchor();
1222 if (anc < cursor()) {
1232 MathCursorPos & MathCursor::cursor()
1234 return Cursor_.back();
1238 MathCursorPos const & MathCursor::cursor() const
1240 return Cursor_.back();
1244 int MathCursor::cellXOffset() const
1246 return par()->cellXOffset(idx());
1250 int MathCursor::cellYOffset() const
1252 return par()->cellYOffset(idx());
1256 int MathCursor::xpos() const
1258 return cellXOffset() + xarray().pos2x(pos());
1262 int MathCursor::ypos() const
1264 return cellYOffset();
1269 void MathCursor::gotoX(int x)
1271 pos() = xarray().x2pos(x - cellXOffset());
1275 bool MathCursor::goUp()
1277 // first ask the inset if it knows better then we
1278 if (par()->idxUp(idx(), pos()))
1281 // if not, apply brute force.
1285 std::vector<MathCursorPos> save = Cursor_;
1286 y0 -= xarray().ascent();
1287 for (int y = y0 - 4; y > outerPar()->yo() - outerPar()->ascent(); y -= 4) {
1289 if (save != Cursor_ && xarray().yo() < y0)
1297 bool MathCursor::goDown()
1299 // first ask the inset if it knows better then we
1300 if (par()->idxDown(idx(), pos()))
1303 // if not, apply brute force.
1307 std::vector<MathCursorPos> save = Cursor_;
1308 y0 += xarray().descent();
1309 for (int y = y0 + 4; y < outerPar()->yo() + outerPar()->descent(); y += 4) {
1311 if (save != Cursor_ && xarray().yo() > y0)
1319 bool MathCursor::idxLeft()
1321 return par()->idxLeft(idx(), pos());
1325 bool MathCursor::idxRight()
1327 return par()->idxRight(idx(), pos());
1331 MathMatrixInset * MathCursor::outerPar() const
1334 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1337 ////////////////////////////////////////////////////////////////////////
1340 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1342 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1346 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1348 if (ti.par_ != it.par_) {
1349 lyxerr << "can't compare cursor and anchor in different insets\n";
1352 if (ti.idx_ != it.idx_)
1353 return ti.idx_ < it.idx_;
1354 return ti.pos_ < it.pos_;
1358 MathArray & MathCursorPos::cell(int idx) const
1360 return par_->cell(idx);
1363 MathArray & MathCursorPos::cell() const
1365 return par_->cell(idx_);
1369 MathXArray & MathCursorPos::xcell(int idx) const
1371 return par_->xcell(idx);
1375 MathXArray & MathCursorPos::xcell() const
1377 return par_->xcell(idx_);
1381 MathCursorPos MathCursor::normalAnchor() const
1383 // use Anchor on the same level as Cursor
1384 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1385 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1386 // anchor is behind cursor -> move anchor behind the inset