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 *) const
186 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
190 //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
191 // << str << "\ntheSelection: " << theSelection;
192 //for (unsigned int i = 0; i < Cursor_.size(); ++i)
193 // lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
194 //lyxerr << "\ncursor.pos_: " << cursor().pos_;
195 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
196 //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
200 bool MathCursor::isInside(MathInset const * p) const
202 for (unsigned i = 0; i < Cursor_.size(); ++i)
203 if (parInset(i) == p)
209 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
214 if (!(p->isActive() || (useupdown && p->isScriptInset())))
218 // we can't move into anything new during selection
219 if (Cursor_.size() == Anchor_.size())
221 if (p != Anchor_[Cursor_.size()].par_)
228 void MathCursor::plainLeft()
234 void MathCursor::plainRight()
240 bool MathCursor::left(bool sel)
244 // was MacroModeBack()
245 if (!imacro_->name().empty()) {
246 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
247 imacro_->metrics(imacro_->size());
255 MathInset * p = prevInset();
256 if (openable(p, sel, false)) {
265 if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
273 bool MathCursor::right(bool sel)
283 MathInset * p = nextInset();
284 if (openable(p, sel, false)) {
288 if (cursor().pos_ != array().size()) {
292 if (cursor().par_->idxRight(cursor().idx_, cursor().pos_)) {
303 void MathCursor::first()
306 push(outerPar(), true);
310 void MathCursor::last()
313 push(outerPar(), false);
317 void MathCursor::setPos(int x, int y)
320 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
323 lastcode_ = LM_TC_MIN;
326 cursor().par_ = outerPar();
331 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_ << "\n";
332 int distmin = 1 << 30; // large enough
333 for (int i = 0; i < cursor().par_->nargs(); ++i) {
334 MathXArray const & ar = cursor().par_->xcell(i);
335 int x1 = x - ar.xo();
336 int y1 = y - ar.yo();
337 int c = ar.x2pos(x1);
338 int xx = abs(x1 - ar.pos2x(c));
340 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
341 // << " c: " << c << " xo: " << ar.xo() << "\n";
342 if (yy + xx <= distmin) {
348 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
349 // << cursor().pos_ << "\n";
350 MathInset * n = nextInset();
351 MathInset * p = prevInset();
352 if (openable(n, selection_, true) && n->covers(x, y))
354 else if (openable(p, selection_, true) && p->covers(x, y)) {
364 void MathCursor::home()
369 if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_))
375 void MathCursor::end()
380 if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
388 void MathCursor::plainErase()
390 array().erase(cursor().pos_);
394 void MathCursor::insert(char c, MathTextCodes t)
396 //lyxerr << "inserting '" << c << "'\n";
403 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
407 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
408 // was MacroModeinsert(c);
409 imacro_->setName(imacro_->name() + c);
414 array().insert(cursor().pos_, c, t);
419 void MathCursor::insert(MathInset * p)
430 array().insert(cursor().pos_, p);
435 void MathCursor::insert(MathArray const & ar)
441 array().insert(cursor().pos_, ar);
442 cursor().pos_ += ar.size();
446 void MathCursor::erase()
457 // delete empty cells if necessary
458 if (cursor().pos_ == 0 && array().empty()) {
461 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
462 if (popit && pop() && removeit)
467 if (cursor().pos_ < array().size())
474 void MathCursor::delLine()
483 if (cursor().par_->nrows() > 1)
484 cursor().par_->delRow(row());
488 bool MathCursor::up(bool sel)
495 int x = xarray().pos2x(cursor().pos_);
496 if (cursor().idxDown()) {
497 cursor().pos_ = xarray().x2pos(x);
505 // check whether we could move into an inset on the right or on the left
506 MathInset * p = nextInset();
509 if (p->idxFirstUp(idx, pos)) {
521 if (p->idxLastUp(idx, pos)) {
531 int x = xarray().pos2x(cursor().pos_);
532 if (cursor().idxUp()) {
533 cursor().pos_ = xarray().x2pos(x);
542 bool MathCursor::down(bool sel)
549 int x = xarray().pos2x(cursor().pos_);
550 if (cursor().idxDown()) {
551 cursor().pos_ = xarray().x2pos(x);
559 // check whether we could move into an inset on the right or on the left
560 MathInset * p = nextInset();
563 if (p->idxFirstDown(idx, pos)) {
575 if (p->idxLastDown(idx, pos)) {
585 int x = xarray().pos2x(cursor().pos_);
586 if (cursor().idxDown()) {
587 cursor().pos_ = xarray().x2pos(x);
596 bool MathCursor::toggleLimits()
598 MathScriptInset * p = prevScriptInset();
601 int old = p->limits();
602 p->limits(old < 0 ? 1 : -1);
603 return old != p->limits();
607 void MathCursor::setSize(MathStyles size)
609 cursor().par_->userSetSize(size);
614 void MathCursor::interpret(string const & s)
616 //lyxerr << "interpret: '" << s << "'\n";
617 //lyxerr << "in: " << in_word_set(s) << " \n";
622 if (s[0] == '^' || s[0] == '_') {
623 bool const up = (s[0] == '^');
625 MathScriptInset * p = prevScriptInset();
627 MathInset * b = prevInset();
628 if (b && b->isScriptable()) {
629 p = new MathScriptInset(up, !up, b->clone());
633 p = new MathScriptInset(up, !up);
643 cursor().idx_ = up ? 0 : 1;
649 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
650 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
651 insert(new MathSpaceInset(sp));
656 latexkeys const * l = in_word_set(s);
660 p = new MathRootInset;
661 else if (MathMacroTable::hasTemplate(s))
662 p = new MathMacro(MathMacroTable::provideTemplate(s));
663 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
668 istringstream is(s.substr(7).c_str());
669 is >> m >> n >> v_align >> h_align;
673 MathArrayInset * pp = new MathArrayInset(m, n);
674 pp->valign(v_align[0]);
679 p = new MathFuncInset(s);
683 p = new MathBigopInset(l);
687 p = new MathFuncLimInset(l);
691 p = new MathSymbolInset(l);
695 p = new MathFracInset("stackrel");
699 p = new MathFracInset("frac");
703 p = new MathSqrtInset;
706 case LM_TK_DECORATION:
707 p = new MathDecorationInset(l);
711 p = new MathSpaceInset(l->id);
715 p = new MathDotsInset(l);
719 p = new MathMacro(MathMacroTable::provideTemplate(s));
723 p = new MathFuncInset(l->name);
733 right(); // do not push for e.g. MathSymbolInset
736 p->metrics(p->size());
741 void MathCursor::macroModeOpen()
744 imacro_ = new MathFuncInset("");
745 array().insert(cursor().pos_, imacro_);
749 lyxerr << "Math Warning: Already in macro mode" << endl;
753 void MathCursor::macroModeClose()
756 string name = imacro_->name();
765 void MathCursor::selCopy()
769 theSelection.grab(*this);
775 void MathCursor::selCut()
779 theSelection.grab(*this);
780 theSelection.erase(*this);
786 void MathCursor::selDel()
790 theSelection.erase(*this);
796 void MathCursor::selPaste()
799 theSelection.paste(*this);
804 void MathCursor::selHandle(bool sel)
806 if (sel && !selection_)
808 if (!sel && selection_)
813 void MathCursor::selStart()
824 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);
888 MathDecorationInset * p = new MathDecorationInset(l);
891 p->cell(0) = theSelection.glue();
898 void MathCursor::handleDelim(int l, int r)
900 MathDelimInset * p = new MathDelimInset(l, r);
903 p->cell(0) = theSelection.glue();
911 void MathCursor::getPos(int & x, int & y)
913 x = xarray().xo() + xarray().pos2x(cursor().pos_);
918 MathTextCodes MathCursor::nextCode() const
920 return array().getCode(cursor().pos_);
924 MathTextCodes MathCursor::prevCode() const
926 return array().getCode(cursor().pos_ - 1);
930 MathInset * MathCursor::par() const
932 return cursor().par_;
936 InsetFormulaBase const * MathCursor::formula()
942 int MathCursor::pos() const
944 return cursor().pos_;
948 bool MathCursor::inMacroMode() const
954 bool MathCursor::selection() const
960 void MathCursor::clearLastCode()
962 lastcode_ = LM_TC_MIN;
966 void MathCursor::setLastCode(MathTextCodes t)
972 MathTextCodes MathCursor::getLastCode() const
978 MathArrayInset * MathCursor::enclosingArray(int & idx) const
980 for (int i = Cursor_.size() - 1; i >= 0; --i) {
981 if (Cursor_[i].par_->isArray()) {
982 idx = Cursor_[i].idx_;
983 return static_cast<MathArrayInset *>(Cursor_[i].par_);
990 void MathCursor::pullArg(bool goright)
994 MathArray a = array();
997 array().insert(cursor().pos_, a);
999 cursor().pos_ += a.size();
1004 MathStyles MathCursor::style() const
1006 return xarray().style();
1010 void MathCursor::normalize() const
1012 #ifdef WITH_WARNINGS
1013 #warning This is evil!
1015 MathCursor * it = const_cast<MathCursor *>(this);
1017 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs() - 1)
1018 lyxerr << "this should not really happen - 1\n";
1019 it->cursor().idx_ = max(cursor().idx_, 0);
1020 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs() - 1);
1022 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1023 lyxerr << "this should not really happen - 2\n";
1024 it->cursor().pos_ = max(cursor().pos_, 0);
1025 it->cursor().pos_ = min(cursor().pos_, array().size());
1029 int MathCursor::col() const
1031 return par()->col(cursor().idx_);
1035 int MathCursor::row() const
1037 return par()->row(cursor().idx_);
1042 char MathCursorPos::getChar() const
1044 return array().getChar(cursor().pos_);
1048 string MathCursorPos::readString()
1051 int code = nextCode();
1052 for ( ; OK() && nextCode() == code; Next())
1060 MathInset * MathCursor::prevInset() const
1063 int c = cursor().pos_;
1066 return array().nextInset(c);
1070 MathInset * MathCursor::nextInset() const
1073 return array().nextInset(cursor().pos_);
1077 MathScriptInset * MathCursor::prevScriptInset() const
1080 MathInset * p = prevInset();
1081 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1085 MathSpaceInset * MathCursor::prevSpaceInset() const
1088 MathInset * p = prevInset();
1089 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1093 MathArray & MathCursor::array() const
1095 static MathArray dummy;
1096 if (!cursor().par_) {
1097 lyxerr << "############ par_ not valid\n";
1101 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1102 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1106 return cursor().cell();
1110 MathXArray & MathCursor::xarray() const
1112 return cursor().xcell();
1116 int MathCursor::xpos() const
1119 return xarray().pos2x(cursor().pos_);
1123 void MathCursor::gotoX(int x)
1125 cursor().pos_ = xarray().x2pos(x);
1129 void MathCursor::idxNext()
1131 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1135 void MathCursor::idxPrev()
1137 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1141 void MathCursor::splitCell()
1143 if (cursor().idx_ == cursor().par_->nargs() - 1)
1145 MathArray ar = array();
1146 ar.erase(0, cursor().pos_);
1147 array().erase(cursor().pos_, array().size());
1150 array().insert(0, ar);
1154 void MathCursor::breakLine()
1156 MathMatrixInset * p = outerPar();
1157 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1158 p->mutate(LM_OT_EQNARRAY);
1160 cursor().idx_ = p->nrows();
1166 const int r = row();
1167 for (int c = col() + 1; c < p->ncols(); ++c) {
1168 const int i1 = p->index(r, c);
1169 const int i2 = p->index(r + 1, c);
1170 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1171 p->cell(i1).swap(p->cell(i2));
1176 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1181 char MathCursor::valign() const
1184 MathArrayInset * p = enclosingArray(idx);
1185 return p ? p->valign() : 0;
1189 char MathCursor::halign() const
1192 MathArrayInset * p = enclosingArray(idx);
1193 return p ? p->halign(idx % p->ncols()) : 0;
1197 MathCursorPos MathCursor::firstSelectionPos() const
1199 MathCursorPos anc = normalAnchor();
1200 return anc < cursor() ? anc : cursor();
1204 MathCursorPos MathCursor::lastSelectionPos() const
1206 MathCursorPos anc = normalAnchor();
1207 return anc < cursor() ? cursor() : anc;
1211 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1213 MathCursorPos anc = normalAnchor();
1214 if (anc < cursor()) {
1224 MathCursorPos & MathCursor::cursor()
1226 return Cursor_.back();
1230 MathCursorPos const & MathCursor::cursor() const
1232 return Cursor_.back();
1237 ////////////////////////////////////////////////////////////////////////
1240 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1242 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1246 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1248 if (ti.par_ != it.par_) {
1249 lyxerr << "can't compare cursor and anchor in different insets\n";
1252 if (ti.idx_ != it.idx_)
1253 return ti.idx_ < it.idx_;
1254 return ti.pos_ < it.pos_;
1258 MathArray & MathCursorPos::cell(int idx) const
1260 return par_->cell(idx);
1263 MathArray & MathCursorPos::cell() const
1265 return par_->cell(idx_);
1269 MathXArray & MathCursorPos::xcell(int idx) const
1271 return par_->xcell(idx);
1275 MathXArray & MathCursorPos::xcell() const
1277 return par_->xcell(idx_);
1281 MathCursorPos MathCursor::normalAnchor() const
1283 // use Anchor on the same level as Cursor
1284 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1285 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1286 // anchor is behind cursor -> move anchor behind the inset
1289 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1290 // << normal << "\n";
1295 bool MathCursorPos::idxUp()
1297 return par_->idxUp(idx_, pos_);
1301 bool MathCursorPos::idxDown()
1303 return par_->idxDown(idx_, pos_);
1307 bool MathCursorPos::idxLeft()
1309 return par_->idxLeft(idx_, pos_);
1313 bool MathCursorPos::idxRight()
1315 return par_->idxRight(idx_, pos_);
1319 MathMatrixInset * MathCursor::outerPar() const
1322 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));