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"
34 #include "math_bigopinset.h"
36 #include "math_symbolinset.h"
37 #include "math_decorationinset.h"
38 #include "math_deliminset.h"
39 #include "math_dotsinset.h"
40 #include "math_fracinset.h"
41 #include "math_funcinset.h"
43 #include "math_funcliminset.h"
45 #include "math_gridinset.h"
46 #include "math_macro.h"
47 #include "math_macroarg.h"
48 #include "math_macrotable.h"
49 #include "math_macrotemplate.h"
50 #include "math_matrixinset.h"
51 #include "math_rootinset.h"
52 #include "math_spaceinset.h"
53 #include "math_sqrtinset.h"
54 #include "support/lstrings.h"
55 #include "math_scriptinset.h"
56 #include "math_parser.h"
68 void grab(MathCursor const & cursor)
73 cursor.getSelection(i1, i2);
74 if (i1.idx_ == i2.idx_)
75 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
77 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
78 for (unsigned i = 0; i < indices.size(); ++i)
79 data_.push_back(i1.cell(indices[i]));
83 void erase(MathCursor & cursor)
87 cursor.getSelection(i1, i2);
88 if (i1.idx_ == i2.idx_) {
89 i1.cell().erase(i1.pos_, i2.pos_);
91 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
92 for (unsigned i = 0; i < indices.size(); ++i)
93 i1.cell(indices[i]).erase();
98 void paste(MathCursor & cursor) const
100 cursor.insert(glue());
103 // glues selection to one cell
104 MathArray glue() const
107 for (unsigned i = 0; i < data_.size(); ++i)
108 ar.push_back(data_[i]);
117 std::vector<MathArray> data_;
121 Selection theSelection;
124 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
126 os << "(par: " << p.par_ << " idx: " << p.idx_
127 << " pos: " << p.pos_ << ")";
134 MathCursor::MathCursor(InsetFormulaBase * formula)
135 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
141 MathCursor::~MathCursor()
146 void MathCursor::push(MathInset * par, bool first)
151 par->idxFirst(p.idx_, p.pos_);
153 par->idxLast(p.idx_, p.pos_);
154 Cursor_.push_back(p);
158 bool MathCursor::pop()
160 if (Cursor_.size() <= 1)
167 MathInset * MathCursor::parInset(int i) const
169 return Cursor_[i].par_;
173 void MathCursor::dump(char const * what) const
177 lyxerr << "MC: " << what << "\n";
178 for (unsigned i = 0; i < Cursor_.size(); ++i)
179 lyxerr << " i: " << i
180 << " pos: " << Cursor_[i].pos_
181 << " idx: " << Cursor_[i].idx_
182 << " par: " << Cursor_[i].par_ << "\n";
184 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
188 void MathCursor::seldump(char const *) const
190 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
194 //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
195 // << str << "\ntheSelection: " << theSelection;
196 //for (unsigned int i = 0; i < Cursor_.size(); ++i)
197 // lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[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
217 if (!(p->isActive() || (useupdown && p->isScriptInset())))
221 // we can't move into anything new during selection
222 if (Cursor_.size() == Anchor_.size())
224 if (p != Anchor_[Cursor_.size()].par_)
231 void MathCursor::plainLeft()
237 bool MathCursor::left(bool sel)
241 // was MacroModeBack()
242 if (!imacro_->name().empty()) {
243 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
244 imacro_->metrics(imacro_->size());
252 MathInset * p = prevInset();
253 if (openable(p, sel, false)) {
262 if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
270 void MathCursor::plainRight()
276 bool MathCursor::right(bool sel)
286 MathInset * p = nextInset();
287 if (openable(p, sel, false)) {
291 if (array().next(cursor().pos_))
293 if (cursor().par_->idxRight(cursor().idx_, cursor().pos_))
297 array().next(cursor().pos_);
302 void MathCursor::first()
305 push(outerPar(), true);
309 void MathCursor::last()
312 push(outerPar(), false);
316 void MathCursor::setPos(int x, int y)
319 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
322 lastcode_ = LM_TC_MIN;
325 cursor().par_ = outerPar();
330 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_ << "\n";
331 int distmin = 1 << 30; // large enough
332 for (int i = 0; i < cursor().par_->nargs(); ++i) {
333 MathXArray const & ar = cursor().par_->xcell(i);
334 int x1 = x - ar.xo();
335 int y1 = y - ar.yo();
336 int c = ar.x2pos(x1);
337 int xx = abs(x1 - ar.pos2x(c));
339 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
340 // << " c: " << c << " xo: " << ar.xo() << "\n";
341 if (yy + xx <= distmin) {
347 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
348 // << cursor().pos_ << "\n";
349 MathInset * n = nextInset();
350 MathInset * p = prevInset();
351 if (openable(n, selection_, true) && n->covers(x, y))
353 else if (openable(p, selection_, true) && p->covers(x, y)) {
363 void MathCursor::home()
368 if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_))
374 void MathCursor::end()
379 if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
387 void MathCursor::plainErase()
389 array().erase(cursor().pos_);
393 void MathCursor::insert(char c, MathTextCodes t)
395 //lyxerr << "inserting '" << c << "'\n";
402 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
406 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
407 // was MacroModeinsert(c);
408 imacro_->setName(imacro_->name() + c);
413 array().insert(cursor().pos_, c, t);
418 void MathCursor::insert(MathInset * p)
429 array().insert(cursor().pos_, p);
434 void MathCursor::insert(MathArray const & ar)
440 array().insert(cursor().pos_, ar);
441 cursor().pos_ += ar.size();
445 void MathCursor::erase()
456 // delete empty cells if necessary
457 if (cursor().pos_ == 0 && array().size() == 0) {
460 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
461 if (popit && pop() && removeit)
466 if (cursor().pos_ < array().size())
473 void MathCursor::delLine()
482 if (cursor().par_->nrows() > 1)
483 cursor().par_->delRow(row());
487 bool MathCursor::up(bool sel)
494 int x = xarray().pos2x(cursor().pos_);
495 if (cursor().idxDown()) {
496 cursor().pos_ = xarray().x2pos(x);
504 // check whether we could move into an inset on the right or on the left
505 MathInset * p = nextInset();
508 if (p->idxFirstUp(idx, pos)) {
520 if (p->idxLastUp(idx, pos)) {
530 int x = xarray().pos2x(cursor().pos_);
531 if (cursor().idxUp()) {
532 cursor().pos_ = xarray().x2pos(x);
541 bool MathCursor::down(bool sel)
548 int x = xarray().pos2x(cursor().pos_);
549 if (cursor().idxDown()) {
550 cursor().pos_ = xarray().x2pos(x);
558 // check whether we could move into an inset on the right or on the left
559 MathInset * p = nextInset();
562 if (p->idxFirstDown(idx, pos)) {
574 if (p->idxLastDown(idx, pos)) {
584 int x = xarray().pos2x(cursor().pos_);
585 if (cursor().idxDown()) {
586 cursor().pos_ = xarray().x2pos(x);
595 bool MathCursor::toggleLimits()
597 MathScriptInset * p = prevScriptInset();
600 int old = p->limits();
601 p->limits(old < 0 ? 1 : -1);
602 return old != p->limits();
606 void MathCursor::setSize(MathStyles size)
608 cursor().par_->userSetSize(size);
613 void MathCursor::interpret(string const & s)
615 //lyxerr << "interpret: '" << s << "'\n";
616 //lyxerr << "in: " << in_word_set(s) << " \n";
621 if (s[0] == '^' || s[0] == '_') {
622 bool const up = (s[0] == '^');
624 MathScriptInset * p = prevScriptInset();
626 MathInset * b = prevInset();
627 if (b && b->isScriptable()) {
628 p = new MathScriptInset(up, !up, b->clone());
632 p = new MathScriptInset(up, !up);
642 cursor().idx_ = up ? 0 : 1;
648 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
649 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
650 insert(new MathSpaceInset(sp));
655 latexkeys const * l = in_word_set(s);
659 p = new MathRootInset;
660 else if (MathMacroTable::hasTemplate(s))
661 p = new MathMacro(MathMacroTable::provideTemplate(s));
662 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
667 istringstream is(s.substr(7).c_str());
668 is >> m >> n >> v_align >> h_align;
672 MathArrayInset * pp = new MathArrayInset(m, n);
673 pp->valign(v_align[0]);
678 p = new MathFuncInset(s);
683 p = new MathBigopInset(l);
689 p = new MathFuncLimInset(l);
694 p = new MathSymbolInset(l);
698 p = new MathFracInset("stackrel");
702 p = new MathFracInset("frac");
706 p = new MathSqrtInset;
709 case LM_TK_DECORATION:
710 p = new MathDecorationInset(l);
714 p = new MathSpaceInset(l->id);
718 p = new MathDotsInset(l);
722 p = new MathMacro(MathMacroTable::provideTemplate(s));
726 p = new MathFuncInset(l->name);
732 bool oldsel = selection_;
738 right(); // do not push for e.g. MathSymbolInset
742 p->metrics(p->size());
747 void MathCursor::macroModeOpen()
750 imacro_ = new MathFuncInset("");
751 array().insert(cursor().pos_, imacro_);
755 lyxerr << "Math Warning: Already in macro mode" << endl;
759 void MathCursor::macroModeClose()
762 string name = imacro_->name();
771 void MathCursor::selCopy()
775 theSelection.grab(*this);
781 void MathCursor::selCut()
785 theSelection.grab(*this);
786 theSelection.erase(*this);
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_)
814 if (!sel && selection_)
819 void MathCursor::selStart()
830 void MathCursor::selClear()
836 void MathCursor::drawSelection(Painter & pain) const
843 getSelection(i1, i2);
845 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
847 if (i1.idx_ == i2.idx_) {
848 MathXArray & c = i1.xcell();
849 int x1 = c.xo() + c.pos2x(i1.pos_);
850 int y1 = c.yo() - c.ascent();
851 int x2 = c.xo() + c.pos2x(i2.pos_);
852 int y2 = c.yo() + c.descent();
853 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
856 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
857 for (unsigned i = 0; i < indices.size(); ++i) {
858 MathXArray & c = i1.xcell(indices[i]);
860 int y1 = c.yo() - c.ascent();
861 int x2 = c.xo() + c.width();
862 int y2 = c.yo() + c.descent();
863 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
869 void MathCursor::handleFont(MathTextCodes t)
874 getSelection(i1, i2);
875 if (i1.idx_ == i2.idx_) {
876 MathArray & ar = i1.cell();
877 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
878 if (isalnum(ar.getChar(pos))) {
879 MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
884 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
888 void MathCursor::handleAccent(string const & name)
890 latexkeys const * l = in_word_set(name);
894 MathDecorationInset * p = new MathDecorationInset(l);
897 p->cell(0) = theSelection.glue();
904 void MathCursor::handleDelim(int l, int r)
906 MathDelimInset * p = new MathDelimInset(l, r);
909 p->cell(0) = theSelection.glue();
917 void MathCursor::getPos(int & x, int & y)
919 x = xarray().xo() + xarray().pos2x(cursor().pos_);
924 MathTextCodes MathCursor::nextCode() const
926 return array().getCode(cursor().pos_);
930 MathTextCodes MathCursor::prevCode() const
932 return array().getCode(cursor().pos_ - 1);
936 MathInset * MathCursor::par() const
938 return cursor().par_;
942 InsetFormulaBase const * MathCursor::formula()
948 int MathCursor::pos() const
950 return cursor().pos_;
954 bool MathCursor::inMacroMode() const
960 bool MathCursor::selection() const
966 void MathCursor::clearLastCode()
968 lastcode_ = LM_TC_MIN;
972 void MathCursor::setLastCode(MathTextCodes t)
978 MathTextCodes MathCursor::getLastCode() const
984 MathArrayInset * MathCursor::enclosingArray(int & idx) const
986 for (int i = Cursor_.size() - 1; i >= 0; --i) {
987 if (Cursor_[i].par_->isArray()) {
988 idx = Cursor_[i].idx_;
989 return static_cast<MathArrayInset *>(Cursor_[i].par_);
996 void MathCursor::pullArg(bool goright)
1000 MathArray a = array();
1003 array().insert(cursor().pos_, a);
1005 cursor().pos_ += a.size();
1010 MathStyles MathCursor::style() const
1012 return xarray().style();
1016 void MathCursor::normalize() const
1018 #ifdef WITH_WARNINGS
1019 #warning This is evil!
1021 MathCursor * it = const_cast<MathCursor *>(this);
1023 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs() - 1)
1024 lyxerr << "this should not really happen - 1\n";
1025 it->cursor().idx_ = max(cursor().idx_, 0);
1026 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs() - 1);
1028 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1029 lyxerr << "this should not really happen - 2\n";
1030 it->cursor().pos_ = max(cursor().pos_, 0);
1031 it->cursor().pos_ = min(cursor().pos_, array().size());
1035 int MathCursor::col() const
1037 return par()->col(cursor().idx_);
1041 int MathCursor::row() const
1043 return par()->row(cursor().idx_);
1048 char MathCursorPos::getChar() const
1050 return array().getChar(cursor().pos_);
1054 string MathCursorPos::readString()
1057 int code = nextCode();
1058 for ( ; OK() && nextCode() == code; Next())
1066 MathInset * MathCursor::prevInset() const
1069 int c = cursor().pos_;
1072 return array().nextInset(c);
1076 MathInset * MathCursor::nextInset() const
1079 return array().nextInset(cursor().pos_);
1083 MathScriptInset * MathCursor::prevScriptInset() const
1086 MathInset * p = prevInset();
1087 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1091 MathSpaceInset * MathCursor::prevSpaceInset() const
1094 MathInset * p = prevInset();
1095 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1099 MathArray & MathCursor::array() const
1101 static MathArray dummy;
1102 if (!cursor().par_) {
1103 lyxerr << "############ par_ not valid\n";
1107 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1108 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1112 return cursor().cell();
1116 MathXArray & MathCursor::xarray() const
1118 return cursor().xcell();
1122 int MathCursor::xpos() const
1125 return xarray().pos2x(cursor().pos_);
1129 void MathCursor::gotoX(int x)
1131 cursor().pos_ = xarray().x2pos(x);
1135 void MathCursor::idxNext()
1137 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1141 void MathCursor::idxPrev()
1143 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1147 void MathCursor::splitCell()
1149 if (cursor().idx_ == cursor().par_->nargs() - 1)
1151 MathArray ar = array();
1152 ar.erase(0, cursor().pos_);
1153 array().erase(cursor().pos_, array().size());
1156 array().insert(0, ar);
1160 void MathCursor::breakLine()
1162 MathMatrixInset * p = outerPar();
1163 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1164 p->mutate(LM_OT_EQNARRAY);
1166 cursor().idx_ = p->nrows();
1172 const int r = row();
1173 for (int c = col() + 1; c < p->ncols(); ++c) {
1174 const int i1 = p->index(r, c);
1175 const int i2 = p->index(r + 1, c);
1176 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1177 p->cell(i1).swap(p->cell(i2));
1182 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1187 char MathCursor::valign() const
1190 MathArrayInset * p = enclosingArray(idx);
1191 return p ? p->valign() : 0;
1195 char MathCursor::halign() const
1198 MathArrayInset * p = enclosingArray(idx);
1199 return p ? p->halign(idx % p->ncols()) : 0;
1203 MathCursorPos MathCursor::firstSelectionPos() const
1205 MathCursorPos anc = normalAnchor();
1206 return anc < cursor() ? anc : cursor();
1210 MathCursorPos MathCursor::lastSelectionPos() const
1212 MathCursorPos anc = normalAnchor();
1213 return anc < cursor() ? cursor() : anc;
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();
1243 ////////////////////////////////////////////////////////////////////////
1246 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1248 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1252 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1254 if (ti.par_ != it.par_) {
1255 lyxerr << "can't compare cursor and anchor in different insets\n";
1258 if (ti.idx_ != it.idx_)
1259 return ti.idx_ < it.idx_;
1260 return ti.pos_ < it.pos_;
1264 MathArray & MathCursorPos::cell(int idx) const
1266 return par_->cell(idx);
1269 MathArray & MathCursorPos::cell() const
1271 return par_->cell(idx_);
1275 MathXArray & MathCursorPos::xcell(int idx) const
1277 return par_->xcell(idx);
1281 MathXArray & MathCursorPos::xcell() const
1283 return par_->xcell(idx_);
1287 MathCursorPos MathCursor::normalAnchor() const
1289 // use Anchor on the same level as Cursor
1290 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1291 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1292 // anchor is behind cursor -> move anchor behind the inset
1293 normal.cell().next(normal.pos_);
1295 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1296 // << normal << "\n";
1301 bool MathCursorPos::idxUp()
1303 return par_->idxUp(idx_, pos_);
1307 bool MathCursorPos::idxDown()
1309 return par_->idxDown(idx_, pos_);
1313 bool MathCursorPos::idxLeft()
1315 return par_->idxLeft(idx_, pos_);
1319 bool MathCursorPos::idxRight()
1321 return par_->idxRight(idx_, pos_);
1325 MathMatrixInset * MathCursor::outerPar() const
1328 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));