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::push(MathInset * par, bool first)
147 par->idxFirst(p.idx_, p.pos_);
149 par->idxLast(p.idx_, p.pos_);
150 Cursor_.push_back(p);
154 bool MathCursor::pop()
156 if (Cursor_.size() <= 1)
163 MathInset * MathCursor::parInset(int i) const
165 return Cursor_[i].par_;
169 void MathCursor::dump(char const * what) const
173 lyxerr << "MC: " << what << "\n";
174 for (unsigned i = 0; i < Cursor_.size(); ++i)
175 lyxerr << " i: " << i
176 << " pos: " << Cursor_[i].pos_
177 << " idx: " << Cursor_[i].idx_
178 << " par: " << Cursor_[i].par_ << "\n";
180 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
184 void MathCursor::seldump(char const * str) const
186 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
190 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
191 << str << "\ntheSelection: " << selection_
192 << " '" << theSelection.glue() << "'\n";
193 for (unsigned int i = 0; i < Cursor_.size(); ++i)
194 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
196 for (unsigned int i = 0; i < Anchor_.size(); ++i)
197 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
198 //lyxerr << "\ncursor.pos_: " << cursor().pos_;
199 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
200 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
204 bool MathCursor::isInside(MathInset const * p) const
206 for (unsigned i = 0; i < Cursor_.size(); ++i)
207 if (parInset(i) == p)
213 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
218 if (!(p->isActive() || (useupdown && p->isScriptInset())))
222 // we can't move into anything new during selection
223 if (Cursor_.size() == Anchor_.size())
225 if (p != Anchor_[Cursor_.size()].par_)
232 void MathCursor::plainLeft()
238 void MathCursor::plainRight()
244 bool MathCursor::left(bool sel)
248 // was MacroModeBack()
249 if (!imacro_->name().empty()) {
250 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
251 imacro_->metrics(imacro_->size());
259 MathInset * p = prevInset();
260 if (openable(p, sel, false)) {
269 if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
277 bool MathCursor::right(bool sel)
287 MathInset * p = nextInset();
288 if (openable(p, sel, false)) {
292 if (cursor().pos_ != array().size()) {
296 if (cursor().par_->idxRight(cursor().idx_, cursor().pos_)) {
307 void MathCursor::first()
310 push(outerPar(), true);
314 void MathCursor::last()
317 push(outerPar(), false);
321 void MathCursor::setPos(int x, int y)
324 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
327 lastcode_ = LM_TC_MIN;
330 cursor().par_ = outerPar();
335 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_ << "\n";
336 int distmin = 1 << 30; // large enough
337 for (int i = 0; i < cursor().par_->nargs(); ++i) {
338 MathXArray const & ar = cursor().par_->xcell(i);
339 int x1 = x - ar.xo();
340 int y1 = y - ar.yo();
341 int c = ar.x2pos(x1);
342 int xx = abs(x1 - ar.pos2x(c));
344 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
345 // << " c: " << c << " xo: " << ar.xo() << "\n";
346 if (yy + xx <= distmin) {
352 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
353 // << cursor().pos_ << "\n";
354 MathInset * n = nextInset();
355 MathInset * p = prevInset();
356 if (openable(n, selection_, true) && n->covers(x, y))
358 else if (openable(p, selection_, true) && p->covers(x, y)) {
368 void MathCursor::home()
373 if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_))
379 void MathCursor::end()
384 if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
392 void MathCursor::plainErase()
394 array().erase(cursor().pos_);
398 void MathCursor::insert(char c, MathTextCodes t)
400 //lyxerr << "inserting '" << c << "'\n";
407 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
411 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
412 // was MacroModeinsert(c);
413 imacro_->setName(imacro_->name() + c);
418 array().insert(cursor().pos_, c, t);
423 void MathCursor::insert(MathInset * p)
434 array().insert(cursor().pos_, p);
439 void MathCursor::insert(MathArray const & ar)
445 array().insert(cursor().pos_, ar);
446 cursor().pos_ += ar.size();
450 void MathCursor::erase()
461 // delete empty cells if necessary
462 if (cursor().pos_ == 0 && array().empty()) {
465 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
466 if (popit && pop() && removeit)
471 if (cursor().pos_ < array().size())
478 void MathCursor::delLine()
487 if (cursor().par_->nrows() > 1)
488 cursor().par_->delRow(row());
492 bool MathCursor::up(bool sel)
499 int x = xarray().pos2x(cursor().pos_);
500 if (cursor().idxDown()) {
501 cursor().pos_ = xarray().x2pos(x);
509 // check whether we could move into an inset on the right or on the left
510 MathInset * p = nextInset();
513 if (p->idxFirstUp(idx, pos)) {
525 if (p->idxLastUp(idx, pos)) {
535 int x = xarray().pos2x(cursor().pos_);
536 if (cursor().idxUp()) {
537 cursor().pos_ = xarray().x2pos(x);
546 bool MathCursor::down(bool sel)
553 int x = xarray().pos2x(cursor().pos_);
554 if (cursor().idxDown()) {
555 cursor().pos_ = xarray().x2pos(x);
563 // check whether we could move into an inset on the right or on the left
564 MathInset * p = nextInset();
567 if (p->idxFirstDown(idx, pos)) {
579 if (p->idxLastDown(idx, pos)) {
589 int x = xarray().pos2x(cursor().pos_);
590 if (cursor().idxDown()) {
591 cursor().pos_ = xarray().x2pos(x);
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 cursor().par_->userSetSize(size);
618 void MathCursor::interpret(string const & s)
620 //lyxerr << "interpret: '" << s << "'\n";
621 //lyxerr << "in: " << in_word_set(s) << " \n";
626 if (s[0] == '^' || s[0] == '_') {
627 bool const up = (s[0] == '^');
629 MathScriptInset * p = prevScriptInset();
631 MathInset * b = prevInset();
632 if (b && b->isScriptable()) {
633 p = new MathScriptInset(up, !up, b->clone());
637 p = new MathScriptInset(up, !up);
647 cursor().idx_ = up ? 0 : 1;
653 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
654 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
655 insert(new MathSpaceInset(sp));
660 latexkeys const * l = in_word_set(s);
664 p = new MathRootInset;
665 else if (MathMacroTable::hasTemplate(s))
666 p = new MathMacro(MathMacroTable::provideTemplate(s));
667 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
672 istringstream is(s.substr(7).c_str());
673 is >> m >> n >> v_align >> h_align;
677 MathArrayInset * pp = new MathArrayInset(m, n);
678 pp->valign(v_align[0]);
683 p = new MathFuncInset(s);
687 p = new MathBigopInset(l);
691 p = new MathFuncLimInset(l);
695 p = new MathSymbolInset(l);
699 p = new MathFracInset("stackrel");
703 p = new MathFracInset("frac");
707 p = new MathSqrtInset;
710 case LM_TK_DECORATION:
711 p = new MathDecorationInset(l);
715 p = new MathSpaceInset(l->id);
719 p = new MathDotsInset(l);
723 p = new MathMacro(MathMacroTable::provideTemplate(s));
727 p = new MathFuncInset(l->name);
737 right(); // do not push for e.g. MathSymbolInset
740 p->metrics(p->size());
745 void MathCursor::macroModeOpen()
748 imacro_ = new MathFuncInset("");
749 array().insert(cursor().pos_, imacro_);
753 lyxerr << "Math Warning: Already in macro mode" << endl;
757 void MathCursor::macroModeClose()
760 string name = imacro_->name();
769 void MathCursor::selCopy()
773 theSelection.grab(*this);
779 void MathCursor::selCut()
783 theSelection.grab(*this);
784 theSelection.erase(*this);
787 theSelection.clear();
792 void MathCursor::selDel()
796 theSelection.erase(*this);
802 void MathCursor::selPaste()
805 theSelection.paste(*this);
810 void MathCursor::selHandle(bool sel)
812 if (sel == selection_)
815 theSelection.clear();
821 void MathCursor::selStart()
827 theSelection.clear();
833 void MathCursor::selClear()
840 void MathCursor::drawSelection(Painter & pain) const
847 getSelection(i1, i2);
849 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
851 if (i1.idx_ == i2.idx_) {
852 MathXArray & c = i1.xcell();
853 int x1 = c.xo() + c.pos2x(i1.pos_);
854 int y1 = c.yo() - c.ascent();
855 int x2 = c.xo() + c.pos2x(i2.pos_);
856 int y2 = c.yo() + c.descent();
857 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
860 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
861 for (unsigned i = 0; i < indices.size(); ++i) {
862 MathXArray & c = i1.xcell(indices[i]);
864 int y1 = c.yo() - c.ascent();
865 int x2 = c.xo() + c.width();
866 int y2 = c.yo() + c.descent();
867 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
873 void MathCursor::handleFont(MathTextCodes t)
878 getSelection(i1, i2);
879 if (i1.idx_ == i2.idx_) {
880 MathArray & ar = i1.cell();
881 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
882 if (isalnum(ar.getChar(pos))) {
883 MathTextCodes c = ar.getCode(pos) == 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);
898 MathDecorationInset * p = new MathDecorationInset(l);
901 p->cell(0) = theSelection.glue();
908 void MathCursor::handleDelim(int l, int r)
910 MathDelimInset * p = new MathDelimInset(l, r);
913 p->cell(0) = theSelection.glue();
921 void MathCursor::getPos(int & x, int & y)
923 x = xarray().xo() + xarray().pos2x(cursor().pos_);
928 MathTextCodes MathCursor::nextCode() const
930 return array().getCode(cursor().pos_);
934 MathTextCodes MathCursor::prevCode() const
936 return array().getCode(cursor().pos_ - 1);
940 MathInset * MathCursor::par() const
942 return cursor().par_;
946 InsetFormulaBase const * MathCursor::formula()
952 int MathCursor::pos() const
954 return cursor().pos_;
958 bool MathCursor::inMacroMode() const
964 bool MathCursor::selection() const
970 void MathCursor::clearLastCode()
972 lastcode_ = LM_TC_MIN;
976 void MathCursor::setLastCode(MathTextCodes t)
982 MathTextCodes MathCursor::getLastCode() const
988 MathArrayInset * MathCursor::enclosingArray(int & idx) const
990 for (int i = Cursor_.size() - 1; i >= 0; --i) {
991 if (Cursor_[i].par_->isArray()) {
992 idx = Cursor_[i].idx_;
993 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1000 void MathCursor::pullArg(bool goright)
1004 MathArray a = array();
1007 array().insert(cursor().pos_, a);
1009 cursor().pos_ += a.size();
1014 MathStyles MathCursor::style() const
1016 return xarray().style();
1020 void MathCursor::normalize() const
1022 #ifdef WITH_WARNINGS
1023 #warning This is evil!
1025 MathCursor * it = const_cast<MathCursor *>(this);
1027 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs() - 1)
1028 lyxerr << "this should not really happen - 1\n";
1029 it->cursor().idx_ = max(cursor().idx_, 0);
1030 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs() - 1);
1032 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1033 lyxerr << "this should not really happen - 2\n";
1034 it->cursor().pos_ = max(cursor().pos_, 0);
1035 it->cursor().pos_ = min(cursor().pos_, array().size());
1039 int MathCursor::col() const
1041 return par()->col(cursor().idx_);
1045 int MathCursor::row() const
1047 return par()->row(cursor().idx_);
1052 char MathCursorPos::getChar() const
1054 return array().getChar(cursor().pos_);
1058 string MathCursorPos::readString()
1061 int code = nextCode();
1062 for ( ; OK() && nextCode() == code; Next())
1070 MathInset * MathCursor::prevInset() const
1073 int c = cursor().pos_;
1076 return array().nextInset(c);
1080 MathInset * MathCursor::nextInset() const
1083 return array().nextInset(cursor().pos_);
1087 MathScriptInset * MathCursor::prevScriptInset() const
1090 MathInset * p = prevInset();
1091 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1095 MathSpaceInset * MathCursor::prevSpaceInset() const
1098 MathInset * p = prevInset();
1099 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1103 MathArray & MathCursor::array() const
1105 static MathArray dummy;
1106 if (!cursor().par_) {
1107 lyxerr << "############ par_ not valid\n";
1111 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1112 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1116 return cursor().cell();
1120 MathXArray & MathCursor::xarray() const
1122 return cursor().xcell();
1126 int MathCursor::xpos() const
1129 return xarray().pos2x(cursor().pos_);
1133 void MathCursor::gotoX(int x)
1135 cursor().pos_ = xarray().x2pos(x);
1139 void MathCursor::idxNext()
1141 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1145 void MathCursor::idxPrev()
1147 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1151 void MathCursor::splitCell()
1153 if (cursor().idx_ == cursor().par_->nargs() - 1)
1155 MathArray ar = array();
1156 ar.erase(0, cursor().pos_);
1157 array().erase(cursor().pos_, array().size());
1160 array().insert(0, ar);
1164 void MathCursor::breakLine()
1166 MathMatrixInset * p = outerPar();
1167 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1168 p->mutate(LM_OT_EQNARRAY);
1170 cursor().idx_ = p->nrows();
1176 const int r = row();
1177 for (int c = col() + 1; c < p->ncols(); ++c) {
1178 const int i1 = p->index(r, c);
1179 const int i2 = p->index(r + 1, c);
1180 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1181 p->cell(i1).swap(p->cell(i2));
1186 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1191 char MathCursor::valign() const
1194 MathArrayInset * p = enclosingArray(idx);
1195 return p ? p->valign() : 0;
1199 char MathCursor::halign() const
1202 MathArrayInset * p = enclosingArray(idx);
1203 return p ? p->halign(idx % p->ncols()) : 0;
1207 MathCursorPos MathCursor::firstSelectionPos() const
1209 MathCursorPos anc = normalAnchor();
1210 return anc < cursor() ? anc : cursor();
1214 MathCursorPos MathCursor::lastSelectionPos() const
1216 MathCursorPos anc = normalAnchor();
1217 return anc < cursor() ? cursor() : anc;
1221 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1223 MathCursorPos anc = normalAnchor();
1224 if (anc < cursor()) {
1234 MathCursorPos & MathCursor::cursor()
1236 return Cursor_.back();
1240 MathCursorPos const & MathCursor::cursor() const
1242 return Cursor_.back();
1247 ////////////////////////////////////////////////////////////////////////
1250 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1252 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1256 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1258 if (ti.par_ != it.par_) {
1259 lyxerr << "can't compare cursor and anchor in different insets\n";
1262 if (ti.idx_ != it.idx_)
1263 return ti.idx_ < it.idx_;
1264 return ti.pos_ < it.pos_;
1268 MathArray & MathCursorPos::cell(int idx) const
1270 return par_->cell(idx);
1273 MathArray & MathCursorPos::cell() const
1275 return par_->cell(idx_);
1279 MathXArray & MathCursorPos::xcell(int idx) const
1281 return par_->xcell(idx);
1285 MathXArray & MathCursorPos::xcell() const
1287 return par_->xcell(idx_);
1291 MathCursorPos MathCursor::normalAnchor() const
1293 // use Anchor on the same level as Cursor
1294 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1295 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1296 // anchor is behind cursor -> move anchor behind the inset
1299 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1300 // << normal << "\n";
1305 bool MathCursorPos::idxUp()
1307 return par_->idxUp(idx_, pos_);
1311 bool MathCursorPos::idxDown()
1313 return par_->idxDown(idx_, pos_);
1317 bool MathCursorPos::idxLeft()
1319 return par_->idxLeft(idx_, pos_);
1323 bool MathCursorPos::idxRight()
1325 return par_->idxRight(idx_, pos_);
1329 MathMatrixInset * MathCursor::outerPar() const
1332 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));