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.insert(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 anything new 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().idxDown()) {
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)) {
531 if (p->idxLastUp(idx, pos)) {
541 int x = xarray().pos2x(cursor().pos_);
542 if (cursor().idxUp()) {
543 cursor().pos_ = xarray().x2pos(x);
552 bool MathCursor::Down(bool sel)
559 int x = xarray().pos2x(cursor().pos_);
560 if (cursor().idxDown()) {
561 cursor().pos_ = xarray().x2pos(x);
569 // check whether we could move into an inset on the right or on the left
570 MathInset * p = nextInset();
573 if (p->idxFirstDown(idx, pos)) {
585 if (p->idxLastDown(idx, pos)) {
595 int x = xarray().pos2x(cursor().pos_);
596 if (cursor().idxDown()) {
597 cursor().pos_ = xarray().x2pos(x);
606 bool MathCursor::toggleLimits()
610 MathInset * p = prevInset();
611 int old = p->limits();
612 p->limits(old < 0 ? 1 : -1);
613 return old != p->limits();
617 void MathCursor::SetSize(MathStyles size)
619 cursor().par_->UserSetSize(size);
624 void MathCursor::Interpret(string const & s)
626 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
627 in_word_set(s) << " \n";
629 if (s[0] == '^' || s[0] == '_') {
630 bool const up = (s[0] == '^');
632 MathUpDownInset * p = prevUpDownInset();
634 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, LM_OT_UNDEF);
683 p = new MathBigopInset(l->name, l->id);
687 MathTextCodes code = static_cast<MathTextCodes>(l->id);
689 insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
691 p = new MathFuncInset(l->name);
696 p = new MathFracInset("stackrel");
700 p = new MathFracInset("frac");
704 p = new MathSqrtInset;
707 case LM_TK_DECORATION:
708 p = new MathDecorationInset(l->name, l->id);
712 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
716 p = new MathSpaceInset(l->id);
720 p = new MathDotsInset(l->name, l->id);
724 p = new MathMacro(MathMacroTable::provideTemplate(s));
728 p = new MathFuncInset(l->name);
734 bool oldsel = selection;
741 Right(); // do not push for e.g. MathBigopInset
745 p->Metrics(p->size());
750 void MathCursor::MacroModeOpen()
753 imacro = new MathFuncInset("");
757 lyxerr << "Math Warning: Already in macro mode" << endl;
761 void MathCursor::MacroModeClose()
765 latexkeys const * l = in_word_set(imacro->name());
766 if (!imacro->name().empty()
767 && (!l || (l && IsMacro(l->token, l->id)))
768 && !MathMacroTable::hasTemplate(imacro->name()))
771 //imacro->SetName(macrobf);
772 // This guarantees that the string will be removed by destructor
773 imacro->SetType(LM_OT_UNDEF);
775 imacro->SetName(l->name);
778 array().erase(cursor().pos_);
779 if (l || MathMacroTable::hasTemplate(imacro->name()))
780 Interpret(imacro->name());
781 imacro->SetName(string());
788 void MathCursor::SelCopy()
792 theSelection.grab(*this);
798 void MathCursor::SelCut()
802 theSelection.grab(*this);
803 theSelection.erase(*this);
809 void MathCursor::SelDel()
813 theSelection.erase(*this);
819 void MathCursor::SelPaste()
822 theSelection.paste(*this);
827 void MathCursor::SelHandle(bool sel)
829 if (sel && !selection)
831 if (!sel && selection)
836 void MathCursor::SelStart()
847 void MathCursor::SelClear()
853 void MathCursor::drawSelection(Painter & pain) const
860 getSelection(i1, i2);
862 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
864 if (i1.idx_ == i2.idx_) {
865 MathXArray & c = i1.xcell();
866 int x1 = c.xo() + c.pos2x(i1.pos_);
867 int y1 = c.yo() - c.ascent();
868 int x2 = c.xo() + c.pos2x(i2.pos_);
869 int y2 = c.yo() + c.descent();
870 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
873 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
874 for (unsigned i = 0; i < indices.size(); ++i) {
875 MathXArray & c = i1.xcell(indices[i]);
877 int y1 = c.yo() - c.ascent();
878 int x2 = c.xo() + c.width();
879 int y2 = c.yo() + c.descent();
880 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
886 void MathCursor::handleFont(MathTextCodes t)
891 getSelection(i1, i2);
892 if (i1.idx_ == i2.idx_) {
893 MathArray & ar = i1.cell();
894 for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
895 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
896 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
901 lastcode = (lastcode == t) ? LM_TC_VAR : t;
906 void MathCursor::handleAccent(string const & name, int code)
908 MathDecorationInset * p = new MathDecorationInset(name, code);
911 p->cell(0) = theSelection.glue();
918 void MathCursor::handleDelim(int l, int r)
920 MathDelimInset * p = new MathDelimInset(l, r);
923 p->cell(0) = theSelection.glue();
931 void MathCursor::GetPos(int & x, int & y)
933 x = xarray().xo() + xarray().pos2x(cursor().pos_);
938 MathTextCodes MathCursor::nextCode() const
940 return array().GetCode(cursor().pos_);
944 MathTextCodes MathCursor::prevCode() const
946 return array().GetCode(cursor().pos_ - 1);
950 MathInset * MathCursor::par() const
952 return cursor().par_;
956 InsetFormulaBase const * MathCursor::formula()
962 int MathCursor::pos() const
964 return cursor().pos_;
968 bool MathCursor::InMacroMode() const
974 bool MathCursor::Selection() const
980 void MathCursor::clearLastCode()
982 lastcode = LM_TC_MIN;
986 void MathCursor::setLastCode(MathTextCodes t)
992 MathTextCodes MathCursor::getLastCode() const
998 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
1000 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1001 //lyxerr << "checking level " << i << "\n";
1002 if (Cursor_[i].par_->GetType() == t) {
1003 idx = Cursor_[i].idx_;
1004 return Cursor_[i].par_;
1011 void MathCursor::pullArg(bool goright)
1015 MathArray a = array();
1017 array().erase(cursor().pos_);
1018 array().insert(cursor().pos_, a);
1020 cursor().pos_ += a.size();
1025 MathStyles MathCursor::style() const
1027 return xarray().style();
1031 void MathCursor::normalize() const
1033 #ifdef WITH_WARNINGS
1034 #warning This is evil!
1036 MathCursor * it = const_cast<MathCursor *>(this);
1038 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
1039 lyxerr << "this should not really happen - 1\n";
1040 it->cursor().idx_ = max(cursor().idx_, 0);
1041 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs());
1043 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1044 lyxerr << "this should not really happen - 2\n";
1045 it->cursor().pos_ = max(cursor().pos_, 0);
1046 it->cursor().pos_ = min(cursor().pos_, array().size());
1050 int MathCursor::col() const
1052 return par()->col(cursor().idx_);
1056 int MathCursor::row() const
1058 return par()->row(cursor().idx_);
1063 char MathCursorPos::GetChar() const
1065 return array().GetChar(cursor().pos_);
1069 string MathCursorPos::readString()
1072 int code = nextCode();
1073 for ( ; OK() && nextCode() == code; Next())
1081 MathInset * MathCursor::prevInset() const
1084 int c = cursor().pos_;
1085 if (!array().prev(c))
1087 return array().nextInset(c);
1091 MathInset * MathCursor::nextInset() const
1094 return array().nextInset(cursor().pos_);
1098 MathUpDownInset * MathCursor::prevUpDownInset() const
1101 MathInset * p = array().prevInset(cursor().pos_);
1102 return (p && p->isUpDownInset()) ? static_cast<MathUpDownInset *>(p) : 0;
1106 MathSpaceInset * MathCursor::prevSpaceInset() const
1109 MathInset * p = array().prevInset(cursor().pos_);
1110 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1114 MathArray & MathCursor::array() const
1116 static MathArray dummy;
1117 if (!cursor().par_) {
1118 lyxerr << "############ par_ not valid\n";
1122 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1123 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1127 return cursor().cell();
1131 MathXArray & MathCursor::xarray() const
1133 return cursor().xcell();
1137 bool MathCursor::nextIsInset() const
1139 return cursor().pos_ < array().size() && MathIsInset(nextCode());
1143 bool MathCursor::prevIsInset() const
1145 return cursor().pos_ > 0 && MathIsInset(prevCode());
1149 int MathCursor::xpos() const
1152 return xarray().pos2x(cursor().pos_);
1156 void MathCursor::gotoX(int x)
1158 cursor().pos_ = xarray().x2pos(x);
1162 void MathCursor::idxNext()
1164 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1168 void MathCursor::idxPrev()
1170 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1174 void MathCursor::splitCell()
1176 if (cursor().idx_ == cursor().par_->nargs() - 1)
1178 MathArray ar = array();
1179 ar.erase(0, cursor().pos_);
1180 array().erase(cursor().pos_, array().size());
1183 array().insert(0, ar);
1187 void MathCursor::breakLine()
1189 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1190 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1191 p->mutate(LM_OT_EQNARRAY);
1193 cursor().idx_ = p->nrows();
1199 const int r = row();
1200 for (int c = col() + 1; c < p->ncols(); ++c) {
1201 const int i1 = p->index(r, c);
1202 const int i2 = p->index(r + 1, c);
1203 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1204 p->cell(i1).swap(p->cell(i2));
1209 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1214 char MathCursor::valign() const
1218 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1219 return p ? p->valign() : 0;
1223 char MathCursor::halign() const
1227 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1228 return p ? p->halign(idx % p->ncols()) : 0;
1232 MathCursorPos MathCursor::firstSelectionPos() const
1234 MathCursorPos anc = normalAnchor();
1235 return anc < cursor() ? anc : cursor();
1239 MathCursorPos MathCursor::lastSelectionPos() const
1241 MathCursorPos anc = normalAnchor();
1242 return anc < cursor() ? cursor() : anc;
1246 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1248 MathCursorPos anc = normalAnchor();
1249 if (anc < cursor()) {
1259 MathCursorPos & MathCursor::cursor()
1261 return Cursor_.back();
1265 MathCursorPos const & MathCursor::cursor() const
1267 return Cursor_.back();
1272 ////////////////////////////////////////////////////////////////////////
1275 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1277 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1281 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1283 if (ti.par_ != it.par_) {
1284 lyxerr << "can't compare cursor and anchor in different insets\n";
1287 if (ti.idx_ != it.idx_)
1288 return ti.idx_ < it.idx_;
1289 return ti.pos_ < it.pos_;
1293 MathArray & MathCursorPos::cell(int idx) const
1295 return par_->cell(idx);
1298 MathArray & MathCursorPos::cell() const
1300 return par_->cell(idx_);
1304 MathXArray & MathCursorPos::xcell(int idx) const
1306 return par_->xcell(idx);
1310 MathXArray & MathCursorPos::xcell() const
1312 return par_->xcell(idx_);
1316 MathCursorPos MathCursor::normalAnchor() const
1318 // use Anchor on the same level as Cursor
1319 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1320 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1321 // anchor is behind cursor -> move anchor behind the inset
1322 normal.cell().next(normal.pos_);
1324 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1325 // << normal << "\n";
1330 bool MathCursorPos::idxUp()
1332 return par_->idxUp(idx_, pos_);
1336 bool MathCursorPos::idxDown()
1338 return par_->idxDown(idx_, pos_);
1342 bool MathCursorPos::idxLeft()
1344 return par_->idxLeft(idx_, pos_);
1348 bool MathCursorPos::idxRight()
1350 return par_->idxRight(idx_, pos_);