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 void MathCursor::plainLeft()
254 void MathCursor::plainRight()
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());
275 MathInset * p = prevInset();
276 if (openable(p, sel, false)) {
284 if (par()->idxLeft(idx(), pos()))
292 bool MathCursor::right(bool sel)
302 MathInset * p = nextInset();
303 if (openable(p, sel, false)) {
307 if (pos() != array().size()) {
311 if (par()->idxRight(idx(), pos()))
319 void MathCursor::first()
322 pushLeft(outerPar());
326 void MathCursor::last()
333 void MathCursor::setPos(int x, int y)
336 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
339 lastcode_ = LM_TC_MIN;
342 cursor().par_ = outerPar();
347 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
348 int distmin = 1 << 30; // large enough
349 for (int i = 0; i < par()->nargs(); ++i) {
350 MathXArray const & ar = par()->xcell(i);
351 int x1 = x - ar.xo();
352 int y1 = y - ar.yo();
353 int c = ar.x2pos(x1);
354 int xx = abs(x1 - ar.pos2x(c));
356 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
357 // << " c: " << c << " xo: " << ar.xo() << "\n";
358 if (yy + xx <= distmin) {
364 //lyxerr << "found idx: " << idx() << " cursor: "
366 MathInset * n = nextInset();
367 MathInset * p = prevInset();
368 if (openable(n, selection_, true) && n->covers(x, y))
370 else if (openable(p, selection_, true) && p->covers(x, y))
379 void MathCursor::home()
384 if (!par()->idxHome(idx(), pos()))
390 void MathCursor::end()
395 if (!par()->idxEnd(idx(), pos()))
401 void MathCursor::plainErase()
403 array().erase(pos());
407 void MathCursor::insert(char c, MathTextCodes t)
409 //lyxerr << "inserting '" << c << "'\n";
416 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
420 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
421 // was MacroModeinsert(c);
422 imacro_->setName(imacro_->name() + c);
427 array().insert(pos(), c, t);
432 void MathCursor::insert(MathInset * p)
443 array().insert(pos(), p);
448 void MathCursor::insert(MathArray const & ar)
454 array().insert(pos(), ar);
459 void MathCursor::backspace()
472 if (array().size()) {
481 void MathCursor::erase()
492 // delete empty cells if necessary
493 if (pos() == 0 && array().empty()) {
496 par()->idxDelete(idx(), popit, removeit);
497 if (popit && popLeft() && removeit)
502 if (pos() < array().size())
509 void MathCursor::delLine()
518 if (par()->nrows() > 1)
519 par()->delRow(row());
523 bool MathCursor::up(bool sel)
530 int x = xarray().pos2x(pos());
531 if (cursor().idxDown()) {
532 pos() = xarray().x2pos(x);
540 // check whether we could move into an inset on the right or on the left
541 MathInset * p = nextInset();
544 if (p->idxFirstUp(idxx, poss)) {
556 if (p->idxLastUp(idxx, poss)) {
565 int x = xarray().pos2x(pos());
566 if (cursor().idxUp()) {
567 pos() = xarray().x2pos(x);
576 bool MathCursor::down(bool sel)
583 int x = xarray().pos2x(pos());
584 if (cursor().idxDown()) {
585 pos() = xarray().x2pos(x);
593 // check whether we could move into an inset on the right or on the left
594 MathInset * p = nextInset();
598 if (p->idxFirstDown(idxx, poss)) {
611 if (p->idxLastDown(idxx, poss)) {
620 int x = xarray().pos2x(pos());
621 if (cursor().idxDown()) {
622 pos() = xarray().x2pos(x);
631 bool MathCursor::toggleLimits()
633 MathScriptInset * p = prevScriptInset();
636 int old = p->limits();
637 p->limits(old < 0 ? 1 : -1);
638 return old != p->limits();
642 void MathCursor::setSize(MathStyles size)
644 par()->userSetSize(size);
649 void MathCursor::interpret(string const & s)
651 //lyxerr << "interpret: '" << s << "'\n";
652 //lyxerr << "in: " << in_word_set(s) << " \n";
657 if (s[0] == '^' || s[0] == '_') {
658 bool const up = (s[0] == '^');
660 MathScriptInset * p = prevScriptInset();
662 MathInset * b = prevInset();
663 if (b && b->isScriptable()) {
664 p = new MathScriptInset(up, !up, b->clone());
668 p = new MathScriptInset(up, !up);
683 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
684 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
685 insert(new MathSpaceInset(sp));
690 latexkeys const * l = in_word_set(s);
694 p = new MathRootInset;
695 else if (MathMacroTable::hasTemplate(s))
696 p = new MathMacro(MathMacroTable::provideTemplate(s));
697 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
702 istringstream is(s.substr(7).c_str());
703 is >> m >> n >> v_align >> h_align;
707 MathArrayInset * pp = new MathArrayInset(m, n);
708 pp->valign(v_align[0]);
713 p = new MathFuncInset(s);
717 p = new MathBigopInset(l);
721 p = new MathFuncLimInset(l);
725 p = new MathSymbolInset(l);
729 p = new MathFracInset("stackrel");
733 p = new MathFracInset("frac");
737 p = new MathSqrtInset;
740 case LM_TK_DECORATION:
741 p = new MathDecorationInset(l);
745 p = new MathSpaceInset(l->id);
749 p = new MathDotsInset(l);
753 p = new MathMacro(MathMacroTable::provideTemplate(s));
757 p = new MathFuncInset(l->name);
767 right(); // do not push for e.g. MathSymbolInset
770 p->metrics(p->size());
775 void MathCursor::macroModeOpen()
778 imacro_ = new MathFuncInset("");
779 array().insert(pos(), imacro_);
783 lyxerr << "Math Warning: Already in macro mode" << endl;
787 void MathCursor::macroModeClose()
790 string name = imacro_->name();
799 void MathCursor::selCopy()
803 theSelection.grab(*this);
809 void MathCursor::selCut()
813 theSelection.grab(*this);
814 theSelection.erase(*this);
817 theSelection.clear();
822 void MathCursor::selDel()
826 theSelection.erase(*this);
832 void MathCursor::selPaste()
835 theSelection.paste(*this);
840 void MathCursor::selHandle(bool sel)
842 if (sel == selection_)
845 theSelection.clear();
851 void MathCursor::selStart()
857 theSelection.clear();
863 void MathCursor::selClear()
870 void MathCursor::drawSelection(Painter & pain) const
877 getSelection(i1, i2);
879 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
881 if (i1.idx_ == i2.idx_) {
882 MathXArray & c = i1.xcell();
883 int x1 = c.xo() + c.pos2x(i1.pos_);
884 int y1 = c.yo() - c.ascent();
885 int x2 = c.xo() + c.pos2x(i2.pos_);
886 int y2 = c.yo() + c.descent();
887 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
890 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
891 for (unsigned i = 0; i < indices.size(); ++i) {
892 MathXArray & c = i1.xcell(indices[i]);
894 int y1 = c.yo() - c.ascent();
895 int x2 = c.xo() + c.width();
896 int y2 = c.yo() + c.descent();
897 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
903 void MathCursor::handleFont(MathTextCodes t)
908 getSelection(i1, i2);
909 if (i1.idx_ == i2.idx_) {
910 MathArray & ar = i1.cell();
911 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
912 if (isalnum(ar.getChar(pos))) {
913 MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
918 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
922 void MathCursor::handleAccent(string const & name)
924 latexkeys const * l = in_word_set(name);
928 MathDecorationInset * p = new MathDecorationInset(l);
931 p->cell(0) = theSelection.glue();
938 void MathCursor::handleDelim(int l, int r)
940 MathDelimInset * p = new MathDelimInset(l, r);
943 p->cell(0) = theSelection.glue();
950 void MathCursor::getPos(int & x, int & y)
952 x = xarray().xo() + xarray().pos2x(pos());
957 MathTextCodes MathCursor::nextCode() const
959 return array().getCode(pos());
963 MathTextCodes MathCursor::prevCode() const
965 return array().getCode(pos() - 1);
969 MathInset * MathCursor::par() const
971 return cursor().par_;
975 InsetFormulaBase const * MathCursor::formula()
981 int MathCursor::idx() const
983 return cursor().idx_;
987 int & MathCursor::idx()
989 return cursor().idx_;
993 int MathCursor::pos() const
995 return cursor().pos_;
999 int & MathCursor::pos()
1001 return cursor().pos_;
1005 bool MathCursor::inMacroMode() const
1011 bool MathCursor::selection() const
1017 void MathCursor::clearLastCode()
1019 lastcode_ = LM_TC_MIN;
1023 void MathCursor::setLastCode(MathTextCodes t)
1029 MathTextCodes MathCursor::getLastCode() const
1035 MathArrayInset * MathCursor::enclosingArray(int & idx) const
1037 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1038 if (Cursor_[i].par_->isArray()) {
1039 idx = Cursor_[i].idx_;
1040 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1047 void MathCursor::pullArg(bool goright)
1051 MathArray a = array();
1054 array().insert(pos(), a);
1061 MathStyles MathCursor::style() const
1063 return xarray().style();
1067 void MathCursor::normalize() const
1069 #ifdef WITH_WARNINGS
1070 #warning This is evil!
1072 MathCursor * it = const_cast<MathCursor *>(this);
1075 lyxerr << "this should not really happen - 1: " << idx() << "\n";
1076 if (idx() >= par()->nargs()) {
1077 lyxerr << "this should not really happen - 2: "
1078 << idx() << " " << par()->nargs() << "\n";
1081 it->idx() = max(idx(), 0);
1082 it->idx() = min(idx(), par()->nargs() - 1);
1085 lyxerr << "this should not really happen - 3: " << pos() << "\n";
1086 if (pos() > array().size()) {
1087 lyxerr << "this should not really happen - 4: "
1088 << pos() << " " << array().size() << "\n";
1091 it->pos() = max(pos(), 0);
1092 it->pos() = min(pos(), array().size());
1096 int MathCursor::col() const
1098 return par()->col(idx());
1102 int MathCursor::row() const
1104 return par()->row(idx());
1109 char MathCursorPos::getChar() const
1111 return array().getChar(pos());
1115 string MathCursorPos::readString()
1118 int code = nextCode();
1119 for ( ; OK() && nextCode() == code; Next())
1127 MathInset * MathCursor::prevInset() const
1132 return array().nextInset(pos() - 1);
1136 MathInset * MathCursor::nextInset() const
1139 return array().nextInset(pos());
1143 MathScriptInset * MathCursor::prevScriptInset() const
1146 MathInset * p = prevInset();
1147 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1151 MathSpaceInset * MathCursor::prevSpaceInset() const
1154 MathInset * p = prevInset();
1155 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1159 MathArray & MathCursor::array() const
1161 static MathArray dummy;
1163 lyxerr << "############ par_ not valid\n";
1167 if (idx() < 0 || idx() >= par()->nargs()) {
1168 lyxerr << "############ idx_ " << idx() << " not valid\n";
1172 return cursor().cell();
1176 MathXArray & MathCursor::xarray() const
1178 return cursor().xcell();
1182 int MathCursor::xpos() const
1185 return xarray().pos2x(pos());
1189 void MathCursor::gotoX(int x)
1191 pos() = xarray().x2pos(x);
1195 void MathCursor::idxNext()
1197 par()->idxNext(idx(), pos());
1201 void MathCursor::idxPrev()
1203 par()->idxPrev(idx(), pos());
1207 void MathCursor::splitCell()
1209 if (idx() == par()->nargs() - 1)
1211 MathArray ar = array();
1213 array().erase(pos(), array().size());
1216 array().insert(0, ar);
1220 void MathCursor::breakLine()
1222 MathMatrixInset * p = outerPar();
1223 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1224 p->mutate(LM_OT_EQNARRAY);
1232 const int r = row();
1233 for (int c = col() + 1; c < p->ncols(); ++c) {
1234 const int i1 = p->index(r, c);
1235 const int i2 = p->index(r + 1, c);
1236 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1237 p->cell(i1).swap(p->cell(i2));
1242 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1247 char MathCursor::valign() const
1250 MathArrayInset * p = enclosingArray(idx);
1251 return p ? p->valign() : 0;
1255 char MathCursor::halign() const
1258 MathArrayInset * p = enclosingArray(idx);
1259 return p ? p->halign(idx % p->ncols()) : 0;
1263 MathCursorPos MathCursor::firstSelectionPos() const
1265 MathCursorPos anc = normalAnchor();
1266 return anc < cursor() ? anc : cursor();
1270 MathCursorPos MathCursor::lastSelectionPos() const
1272 MathCursorPos anc = normalAnchor();
1273 return anc < cursor() ? cursor() : anc;
1277 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1279 MathCursorPos anc = normalAnchor();
1280 if (anc < cursor()) {
1290 MathCursorPos & MathCursor::cursor()
1292 return Cursor_.back();
1296 MathCursorPos const & MathCursor::cursor() const
1298 return Cursor_.back();
1303 ////////////////////////////////////////////////////////////////////////
1306 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1308 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1312 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1314 if (ti.par_ != it.par_) {
1315 lyxerr << "can't compare cursor and anchor in different insets\n";
1318 if (ti.idx_ != it.idx_)
1319 return ti.idx_ < it.idx_;
1320 return ti.pos_ < it.pos_;
1324 MathArray & MathCursorPos::cell(int idx) const
1326 return par_->cell(idx);
1329 MathArray & MathCursorPos::cell() const
1331 return par_->cell(idx_);
1335 MathXArray & MathCursorPos::xcell(int idx) const
1337 return par_->xcell(idx);
1341 MathXArray & MathCursorPos::xcell() const
1343 return par_->xcell(idx_);
1347 MathCursorPos MathCursor::normalAnchor() const
1349 // use Anchor on the same level as Cursor
1350 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1351 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1352 // anchor is behind cursor -> move anchor behind the inset
1355 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1356 // << normal << "\n";
1361 bool MathCursorPos::idxUp()
1363 return par_->idxUp(idx_, pos_);
1367 bool MathCursorPos::idxDown()
1369 return par_->idxDown(idx_, pos_);
1373 bool MathCursorPos::idxLeft()
1375 return par_->idxLeft(idx_, pos_);
1379 bool MathCursorPos::idxRight()
1381 return par_->idxRight(idx_, pos_);
1385 MathMatrixInset * MathCursor::outerPar() const
1388 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));