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_decorationinset.h"
35 #include "math_deliminset.h"
36 #include "math_dotsinset.h"
37 #include "math_fracinset.h"
38 #include "math_funcinset.h"
39 #include "math_gridinset.h"
40 #include "math_macro.h"
41 #include "math_macroarg.h"
42 #include "math_macrotable.h"
43 #include "math_macrotemplate.h"
44 #include "math_matrixinset.h"
45 #include "math_rootinset.h"
46 #include "math_spaceinset.h"
47 #include "math_sqrtinset.h"
48 #include "support/lstrings.h"
49 #include "math_scriptinset.h"
50 #include "math_parser.h"
62 void grab(MathCursor const & cursor)
67 cursor.getSelection(i1, i2);
68 if (i1.idx_ == i2.idx_)
69 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
71 #ifdef RECTANGULAR_SELECT
72 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
73 for (unsigned i = 0; i < indices.size(); ++i)
74 data_.push_back(i1.cell(indices[i]));
76 data_.push_back(MathArray(i1.cell(), i1.pos_, i1.cell().size()));
77 for (int i = i1.idx_ + 1; i < i2.idx_; ++i)
78 data_.push_back(i1.cell(i));
79 data_.push_back(MathArray(i2.cell(), 0, i2.pos_));
84 void erase(MathCursor & cursor)
88 cursor.getSelection(i1, i2);
89 if (i1.idx_ == i2.idx_) {
90 i1.cell().erase(i1.pos_, i2.pos_);
92 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
93 for (unsigned i = 0; i < indices.size(); ++i)
94 i1.cell(indices[i]).erase();
99 void paste(MathCursor & cursor) const
101 cursor.cursor().cell().push_back(glue());
104 // glues selection to one cell
105 MathArray glue() const
108 for (unsigned i = 0; i < data_.size(); ++i)
109 ar.push_back(data_[i]);
120 std::vector<MathArray> data_;
124 Selection theSelection;
127 bool IsMacro(short tok, int id)
129 return tok != LM_TK_STACK &&
132 tok != LM_TK_DECORATION &&
133 tok != LM_TK_SPACE &&
135 tok != LM_TK_FUNCLIM &&
136 tok != LM_TK_BIGSYM &&
137 !(tok == LM_TK_SYM && id < 255);
141 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
143 os << "(par: " << p.par_ << " idx: " << p.idx_
144 << " pos: " << p.pos_ << ")";
151 MathCursor::MathCursor(InsetFormulaBase * formula)
154 lastcode = LM_TC_MIN;
161 void MathCursor::push(MathInset * par, bool first)
166 par->idxFirst(p.idx_, p.pos_);
168 par->idxLast(p.idx_, p.pos_);
169 Cursor_.push_back(p);
173 bool MathCursor::pop()
175 if (Cursor_.size() <= 1)
182 MathInset * MathCursor::parInset(int i) const
184 return Cursor_[i].par_;
188 void MathCursor::dump(char const * what) const
192 lyxerr << "MC: " << what << "\n";
193 for (unsigned i = 0; i < Cursor_.size(); ++i)
194 lyxerr << " i: " << i
195 << " pos: " << Cursor_[i].pos_
196 << " idx: " << Cursor_[i].idx_
197 << " par: " << Cursor_[i].par_ << "\n";
199 //lyxerr << " sel: " << selection << " data: " << array() << "\n";
203 void MathCursor::seldump(char const *) const
205 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
209 //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
210 // << str << "\ntheSelection: " << theSelection;
211 //for (unsigned int i = 0; i < Cursor_.size(); ++i)
212 // lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
213 //lyxerr << "\ncursor.pos_: " << cursor().pos_;
214 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
215 //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
219 bool MathCursor::isInside(MathInset * p) const
221 for (unsigned i = 0; i < Cursor_.size(); ++i)
222 if (parInset(i) == p)
228 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
232 if (!(p->isActive() || (useupdown && p->isUpDownInset())))
236 // we can't move into everything during selection
237 if (Cursor_.size() == Anchor_.size())
239 if (p != Anchor_[Cursor_.size()].par_)
246 bool MathCursor::plainLeft()
248 return array().prev(cursor().pos_);
252 bool MathCursor::Left(bool sel)
256 // was MacroModeBack()
257 if (!imacro->name().empty()) {
258 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
259 imacro->Metrics(imacro->size());
267 MathInset * p = prevInset();
268 if (openable(p, sel, false)) {
275 if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
283 bool MathCursor::plainRight()
285 return array().next(cursor().pos_);
289 bool MathCursor::Right(bool sel)
299 MathInset * p = nextInset();
300 if (openable(p, sel, false)) {
304 if (array().next(cursor().pos_))
306 if (cursor().par_->idxRight(cursor().idx_, cursor().pos_))
310 array().next(cursor().pos_);
315 void MathCursor::first()
318 push(formula_->par(), true);
322 void MathCursor::last()
325 push(formula_->par(), false);
329 void MathCursor::SetPos(int x, int y)
332 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
335 lastcode = LM_TC_MIN;
338 cursor().par_ = formula()->par();
343 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_ << "\n";
344 int distmin = 1 << 30; // large enough
345 for (int i = 0; i < cursor().par_->nargs(); ++i) {
346 MathXArray const & ar = cursor().par_->xcell(i);
347 int x1 = x - ar.xo();
348 int y1 = y - ar.yo();
349 int c = ar.x2pos(x1);
350 int xx = abs(x1 - ar.pos2x(c));
352 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
353 // << " c: " << c << " xo: " << ar.xo() << "\n";
354 if (yy + xx <= distmin) {
360 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
361 // << cursor().pos_ << "\n";
362 MathInset * n = nextInset();
363 MathInset * p = prevInset();
364 if (openable(n, selection, true) && n->covers(x, y))
366 else if (openable(p, selection, true) && p->covers(x, y)) {
376 void MathCursor::Home()
382 if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_))
388 void MathCursor::End()
394 if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
396 array().next(cursor().pos_);
402 void MathCursor::insert(char c, MathTextCodes t)
404 //lyxerr << "inserting '" << c << "'\n";
411 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
415 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
416 // was MacroModeinsert(c);
417 imacro->SetName(imacro->name() + static_cast<char>(c));
422 array().insert(cursor().pos_, c, t);
423 array().next(cursor().pos_);
429 void MathCursor::insert(MathInset * p)
440 array().insert(cursor().pos_, p);
441 array().next(cursor().pos_);
445 void MathCursor::insert(MathArray const & ar)
451 array().insert(cursor().pos_, ar);
452 cursor().pos_ += ar.size();
456 void MathCursor::Delete()
467 // delete empty cells if necessary
468 if (cursor().pos_ == 0 && array().size() == 0) {
471 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
472 if (popit && pop() && removeit)
477 if (cursor().pos_ < array().size())
478 array().erase(cursor().pos_);
484 void MathCursor::DelLine()
493 if (cursor().par_->nrows() > 1)
494 cursor().par_->delRow(row());
498 bool MathCursor::Up(bool sel)
505 int x = xarray().pos2x(cursor().pos_);
506 if (cursor().par_->idxUp(cursor().idx_, cursor().pos_)) {
507 cursor().pos_ = xarray().x2pos(x);
515 // check whether we could move into an inset on the right or on the left
516 MathInset * p = nextInset();
519 if (p->idxFirstUp(idx, pos)) {
532 if (p->idxLastUp(idx, pos)) {
543 int x = xarray().pos2x(cursor().pos_);
544 if (cursor().idxUp()) {
545 cursor().pos_ = xarray().x2pos(x);
554 bool MathCursor::Down(bool sel)
561 int x = xarray().pos2x(cursor().pos_);
562 if (cursor().idxDown()) {
563 cursor().pos_ = xarray().x2pos(x);
571 // check whether we could move into an inset on the right or on the left
572 MathInset * p = nextInset();
575 if (p->idxFirstDown(idx, pos)) {
587 if (p->idxLastDown(idx, pos)) {
597 int x = xarray().pos2x(cursor().pos_);
598 if (cursor().par_->idxDown(cursor().idx_, cursor().pos_)) {
599 cursor().pos_ = xarray().x2pos(x);
608 bool MathCursor::toggleLimits()
612 MathInset * p = prevInset();
613 int old = p->limits();
614 p->limits(old < 0 ? 1 : -1);
615 return old != p->limits();
619 void MathCursor::SetSize(MathStyles size)
621 cursor().par_->UserSetSize(size);
626 void MathCursor::Interpret(string const & s)
628 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
629 in_word_set(s) << " \n";
631 if (s[0] == '^' || s[0] == '_') {
632 bool const up = (s[0] == '^');
634 MathUpDownInset * p = prevUpDownInset();
636 p = new MathScriptInset(up, !up);
645 cursor().idx_ = up ? 0 : 1;
651 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
652 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
653 insert(new MathSpaceInset(sp));
658 latexkeys const * l = in_word_set(s);
662 p = new MathRootInset;
663 else if (MathMacroTable::hasTemplate(s))
664 p = new MathMacro(MathMacroTable::provideTemplate(s));
665 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
670 istringstream is(s.substr(7).c_str());
671 is >> m >> n >> v_align >> h_align;
675 MathArrayInset * pp = new MathArrayInset(m, n);
676 pp->valign(v_align[0]);
681 p = new MathFuncInset(s, LM_OT_UNDEF);
685 p = new MathBigopInset(l->name, l->id);
689 MathTextCodes code = static_cast<MathTextCodes>(l->id);
691 insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
693 p = new MathFuncInset(l->name);
698 p = new MathFracInset("stackrel");
702 p = new MathFracInset("frac");
706 p = new MathSqrtInset;
709 case LM_TK_DECORATION:
710 p = new MathDecorationInset(l->name, l->id);
714 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
718 p = new MathSpaceInset(l->id);
722 p = new MathDotsInset(l->name, l->id);
726 p = new MathMacro(MathMacroTable::provideTemplate(s));
730 p = new MathFuncInset(l->name);
736 bool oldsel = selection;
743 Right(); // do not push for e.g. MathBigopInset
747 p->Metrics(p->size());
752 void MathCursor::MacroModeOpen()
755 imacro = new MathFuncInset("");
759 lyxerr << "Math Warning: Already in macro mode" << endl;
763 void MathCursor::MacroModeClose()
767 latexkeys const * l = in_word_set(imacro->name());
768 if (!imacro->name().empty()
769 && (!l || (l && IsMacro(l->token, l->id)))
770 && !MathMacroTable::hasTemplate(imacro->name()))
773 //imacro->SetName(macrobf);
774 // This guarantees that the string will be removed by destructor
775 imacro->SetType(LM_OT_UNDEF);
777 imacro->SetName(l->name);
780 array().erase(cursor().pos_);
781 if (l || MathMacroTable::hasTemplate(imacro->name()))
782 Interpret(imacro->name());
783 imacro->SetName(string());
790 void MathCursor::SelCopy()
794 theSelection.grab(*this);
800 void MathCursor::SelCut()
804 theSelection.grab(*this);
805 theSelection.erase(*this);
811 void MathCursor::SelDel()
815 theSelection.erase(*this);
821 void MathCursor::SelPaste()
824 theSelection.paste(*this);
829 void MathCursor::SelHandle(bool sel)
831 if (sel && !selection)
833 if (!sel && selection)
838 void MathCursor::SelStart()
849 void MathCursor::SelClear()
855 void MathCursor::drawSelection(Painter & pain) const
862 getSelection(i1, i2);
864 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
866 if (i1.idx_ == i2.idx_) {
867 MathXArray & c = i1.xcell();
868 int x1 = c.xo() + c.pos2x(i1.pos_);
869 int y1 = c.yo() - c.ascent();
870 int x2 = c.xo() + c.pos2x(i2.pos_);
871 int y2 = c.yo() + c.descent();
872 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
875 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
876 for (unsigned i = 0; i < indices.size(); ++i) {
877 MathXArray & c = i1.xcell(indices[i]);
879 int y1 = c.yo() - c.ascent();
880 int x2 = c.xo() + c.width();
881 int y2 = c.yo() + c.descent();
882 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
888 void MathCursor::handleFont(MathTextCodes t)
893 getSelection(i1, i2);
894 if (i1.idx_ == i2.idx_) {
895 MathArray & ar = i1.cell();
896 for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
897 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
898 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
903 lastcode = (lastcode == t) ? LM_TC_VAR : t;
908 void MathCursor::handleAccent(string const & name, int code)
910 MathDecorationInset * p = new MathDecorationInset(name, code);
913 p->cell(0) = theSelection.glue();
920 void MathCursor::handleDelim(int l, int r)
922 MathDelimInset * p = new MathDelimInset(l, r);
925 p->cell(0) = theSelection.glue();
933 void MathCursor::GetPos(int & x, int & y)
935 x = xarray().xo() + xarray().pos2x(cursor().pos_);
940 MathTextCodes MathCursor::nextCode() const
942 return array().GetCode(cursor().pos_);
946 MathTextCodes MathCursor::prevCode() const
948 return array().GetCode(cursor().pos_ - 1);
952 MathInset * MathCursor::par() const
954 return cursor().par_;
958 InsetFormulaBase const * MathCursor::formula()
964 int MathCursor::pos() const
966 return cursor().pos_;
970 bool MathCursor::InMacroMode() const
976 bool MathCursor::Selection() const
982 void MathCursor::clearLastCode()
984 lastcode = LM_TC_MIN;
988 void MathCursor::setLastCode(MathTextCodes t)
994 MathTextCodes MathCursor::getLastCode() const
1000 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
1002 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1003 //lyxerr << "checking level " << i << "\n";
1004 if (Cursor_[i].par_->GetType() == t) {
1005 idx = Cursor_[i].idx_;
1006 return Cursor_[i].par_;
1013 void MathCursor::pullArg(bool goright)
1017 MathArray a = array();
1019 array().erase(cursor().pos_);
1020 array().insert(cursor().pos_, a);
1022 cursor().pos_ += a.size();
1027 MathStyles MathCursor::style() const
1029 return xarray().style();
1033 void MathCursor::normalize() const
1035 #ifdef WITH_WARNINGS
1036 #warning This is evil!
1038 MathCursor * it = const_cast<MathCursor *>(this);
1040 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
1041 lyxerr << "this should not really happen - 1\n";
1042 it->cursor().idx_ = max(cursor().idx_, 0);
1043 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs());
1045 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1046 lyxerr << "this should not really happen - 2\n";
1047 it->cursor().pos_ = max(cursor().pos_, 0);
1048 it->cursor().pos_ = min(cursor().pos_, array().size());
1052 int MathCursor::col() const
1054 return par()->col(cursor().idx_);
1058 int MathCursor::row() const
1060 return par()->row(cursor().idx_);
1065 char MathCursorPos::GetChar() const
1067 return array().GetChar(cursor().pos_);
1071 string MathCursorPos::readString()
1074 int code = nextCode();
1075 for ( ; OK() && nextCode() == code; Next())
1083 MathInset * MathCursor::prevInset() const
1086 int c = cursor().pos_;
1087 if (!array().prev(c))
1089 return array().nextInset(c);
1093 MathInset * MathCursor::nextInset() const
1096 return array().nextInset(cursor().pos_);
1100 MathUpDownInset * MathCursor::prevUpDownInset() const
1103 MathInset * p = array().prevInset(cursor().pos_);
1104 return (p && p->isUpDownInset()) ? static_cast<MathUpDownInset *>(p) : 0;
1108 MathArray & MathCursor::array() const
1110 static MathArray dummy;
1111 if (!cursor().par_) {
1112 lyxerr << "############ par_ not valid\n";
1116 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1117 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1121 return cursor().cell();
1125 MathXArray & MathCursor::xarray() const
1127 return cursor().xcell();
1131 bool MathCursor::nextIsInset() const
1133 return cursor().pos_ < array().size() && MathIsInset(nextCode());
1137 bool MathCursor::prevIsInset() const
1139 return cursor().pos_ > 0 && MathIsInset(prevCode());
1143 int MathCursor::xpos() const
1146 return xarray().pos2x(cursor().pos_);
1150 void MathCursor::gotoX(int x)
1152 cursor().pos_ = xarray().x2pos(x);
1156 void MathCursor::idxNext()
1158 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1162 void MathCursor::idxPrev()
1164 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1168 void MathCursor::splitCell()
1170 if (cursor().idx_ == cursor().par_->nargs() - 1)
1172 MathArray ar = array();
1173 ar.erase(0, cursor().pos_);
1174 array().erase(cursor().pos_, array().size());
1177 array().insert(0, ar);
1181 void MathCursor::breakLine()
1183 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1184 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1185 p->mutate(LM_OT_EQNARRAY);
1187 cursor().idx_ = p->nrows();
1193 const int r = row();
1194 for (int c = col() + 1; c < p->ncols(); ++c) {
1195 const int i1 = p->index(r, c);
1196 const int i2 = p->index(r + 1, c);
1197 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1198 p->cell(i1).swap(p->cell(i2));
1203 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1208 char MathCursor::valign() const
1212 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1213 return p ? p->valign() : 0;
1217 char MathCursor::halign() const
1221 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1222 return p ? p->halign(idx % p->ncols()) : 0;
1226 MathCursorPos MathCursor::firstSelectionPos() const
1228 MathCursorPos anc = normalAnchor();
1229 return anc < cursor() ? anc : cursor();
1233 MathCursorPos MathCursor::lastSelectionPos() const
1235 MathCursorPos anc = normalAnchor();
1236 return anc < cursor() ? cursor() : anc;
1240 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1242 MathCursorPos anc = normalAnchor();
1243 if (anc < cursor()) {
1253 MathCursorPos & MathCursor::cursor()
1255 return Cursor_.back();
1259 MathCursorPos const & MathCursor::cursor() const
1261 return Cursor_.back();
1266 ////////////////////////////////////////////////////////////////////////
1269 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1271 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1275 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1277 if (ti.par_ != it.par_) {
1278 lyxerr << "can't compare cursor and anchor in different insets\n";
1281 if (ti.idx_ != it.idx_)
1282 return ti.idx_ < it.idx_;
1283 return ti.pos_ < it.pos_;
1287 MathArray & MathCursorPos::cell(int idx) const
1289 return par_->cell(idx);
1292 MathArray & MathCursorPos::cell() const
1294 return par_->cell(idx_);
1298 MathXArray & MathCursorPos::xcell(int idx) const
1300 return par_->xcell(idx);
1304 MathXArray & MathCursorPos::xcell() const
1306 return par_->xcell(idx_);
1310 MathCursorPos MathCursor::normalAnchor() const
1312 // use Anchor on the same level as Cursor
1313 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1314 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1315 // anchor is behind cursor -> move anchor behind the inset
1316 normal.cell().next(normal.pos_);
1318 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1319 // << normal << "\n";
1324 bool MathCursorPos::idxUp()
1326 return par_->idxUp(idx_, pos_);
1330 bool MathCursorPos::idxDown()
1332 return par_->idxDown(idx_, pos_);
1336 bool MathCursorPos::idxLeft()
1338 return par_->idxLeft(idx_, pos_);
1342 bool MathCursorPos::idxRight()
1344 return par_->idxRight(idx_, pos_);