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";
632 MathUpDownInset * p = nearbyUpDownInset();
634 p = new MathScriptInset(true, false);
645 MathUpDownInset * p = nearbyUpDownInset();
647 p = new MathScriptInset(false, true);
657 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
658 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
659 insert(new MathSpaceInset(sp));
664 latexkeys const * l = in_word_set(s);
668 p = new MathRootInset;
669 else if (MathMacroTable::hasTemplate(s))
670 p = new MathMacro(MathMacroTable::provideTemplate(s));
671 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
676 istringstream is(s.substr(7).c_str());
677 is >> m >> n >> v_align >> h_align;
681 MathArrayInset * pp = new MathArrayInset(m, n);
682 pp->valign(v_align[0]);
687 p = new MathFuncInset(s, LM_OT_UNDEF);
691 p = new MathBigopInset(l->name, l->id);
695 MathTextCodes code = static_cast<MathTextCodes>(l->id);
697 insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
699 p = new MathFuncInset(l->name);
704 p = new MathFracInset("stackrel");
708 p = new MathFracInset("frac");
712 p = new MathSqrtInset;
715 case LM_TK_DECORATION:
716 p = new MathDecorationInset(l->name, l->id);
720 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
724 p = new MathSpaceInset(l->id);
728 p = new MathDotsInset(l->name, l->id);
732 p = new MathMacro(MathMacroTable::provideTemplate(s));
736 p = new MathFuncInset(l->name);
742 bool oldsel = selection;
752 p->Metrics(p->size());
757 void MathCursor::MacroModeOpen()
760 imacro = new MathFuncInset("");
764 lyxerr << "Math Warning: Already in macro mode" << endl;
768 void MathCursor::MacroModeClose()
772 latexkeys const * l = in_word_set(imacro->name());
773 if (!imacro->name().empty()
774 && (!l || (l && IsMacro(l->token, l->id)))
775 && !MathMacroTable::hasTemplate(imacro->name()))
778 //imacro->SetName(macrobf);
779 // This guarantees that the string will be removed by destructor
780 imacro->SetType(LM_OT_UNDEF);
782 imacro->SetName(l->name);
785 array().erase(cursor().pos_);
786 if (l || MathMacroTable::hasTemplate(imacro->name()))
787 Interpret(imacro->name());
788 imacro->SetName(string());
795 void MathCursor::SelCopy()
799 theSelection.grab(*this);
805 void MathCursor::SelCut()
809 theSelection.grab(*this);
810 theSelection.erase(*this);
816 void MathCursor::SelDel()
820 theSelection.erase(*this);
826 void MathCursor::SelPaste()
829 theSelection.paste(*this);
834 void MathCursor::SelHandle(bool sel)
836 if (sel && !selection)
838 if (!sel && selection)
843 void MathCursor::SelStart()
854 void MathCursor::SelClear()
860 void MathCursor::drawSelection(Painter & pain) const
867 getSelection(i1, i2);
869 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
871 if (i1.idx_ == i2.idx_) {
872 MathXArray & c = i1.xcell();
873 int x1 = c.xo() + c.pos2x(i1.pos_);
874 int y1 = c.yo() - c.ascent();
875 int x2 = c.xo() + c.pos2x(i2.pos_);
876 int y2 = c.yo() + c.descent();
877 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
880 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
881 for (unsigned i = 0; i < indices.size(); ++i) {
882 MathXArray & c = i1.xcell(indices[i]);
884 int y1 = c.yo() - c.ascent();
885 int x2 = c.xo() + c.width();
886 int y2 = c.yo() + c.descent();
887 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
893 void MathCursor::handleFont(MathTextCodes t)
898 getSelection(i1, i2);
899 if (i1.idx_ == i2.idx_) {
900 MathArray & ar = i1.cell();
901 for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
902 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
903 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
908 lastcode = (lastcode == t) ? LM_TC_VAR : t;
913 void MathCursor::handleAccent(string const & name, int code)
915 MathDecorationInset * p = new MathDecorationInset(name, code);
918 p->cell(0) = theSelection.glue();
925 void MathCursor::handleDelim(int l, int r)
927 MathDelimInset * p = new MathDelimInset(l, r);
930 p->cell(0) = theSelection.glue();
938 void MathCursor::GetPos(int & x, int & y)
940 x = xarray().xo() + xarray().pos2x(cursor().pos_);
945 MathTextCodes MathCursor::nextCode() const
947 return array().GetCode(cursor().pos_);
951 MathTextCodes MathCursor::prevCode() const
953 return array().GetCode(cursor().pos_ - 1);
957 MathInset * MathCursor::par() const
959 return cursor().par_;
963 InsetFormulaBase const * MathCursor::formula()
969 int MathCursor::pos() const
971 return cursor().pos_;
975 bool MathCursor::InMacroMode() const
981 bool MathCursor::Selection() const
987 void MathCursor::clearLastCode()
989 lastcode = LM_TC_MIN;
993 void MathCursor::setLastCode(MathTextCodes t)
999 MathTextCodes MathCursor::getLastCode() const
1005 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
1007 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1008 //lyxerr << "checking level " << i << "\n";
1009 if (Cursor_[i].par_->GetType() == t) {
1010 idx = Cursor_[i].idx_;
1011 return Cursor_[i].par_;
1018 void MathCursor::pullArg(bool goright)
1022 MathArray a = array();
1024 array().erase(cursor().pos_);
1025 array().insert(cursor().pos_, a);
1027 cursor().pos_ += a.size();
1032 MathStyles MathCursor::style() const
1034 return xarray().style();
1038 void MathCursor::normalize() const
1040 #ifdef WITH_WARNINGS
1041 #warning This is evil!
1043 MathCursor * it = const_cast<MathCursor *>(this);
1045 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
1046 lyxerr << "this should not really happen - 1\n";
1047 it->cursor().idx_ = max(cursor().idx_, 0);
1048 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs());
1050 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1051 lyxerr << "this should not really happen - 2\n";
1052 it->cursor().pos_ = max(cursor().pos_, 0);
1053 it->cursor().pos_ = min(cursor().pos_, array().size());
1057 int MathCursor::col() const
1059 return par()->col(cursor().idx_);
1063 int MathCursor::row() const
1065 return par()->row(cursor().idx_);
1070 char MathCursorPos::GetChar() const
1072 return array().GetChar(cursor().pos_);
1076 string MathCursorPos::readString()
1079 int code = nextCode();
1080 for ( ; OK() && nextCode() == code; Next())
1088 MathInset * MathCursor::prevInset() const
1091 int c = cursor().pos_;
1092 if (!array().prev(c))
1094 return array().nextInset(c);
1098 MathInset * MathCursor::nextInset() const
1101 return array().nextInset(cursor().pos_);
1105 MathUpDownInset * MathCursor::nearbyUpDownInset() const
1108 MathInset * p = array().prevInset(cursor().pos_);
1109 if (p && p->isUpDownInset())
1110 return static_cast<MathUpDownInset *>(p);
1111 p = array().nextInset(cursor().pos_);
1112 if (p && p->isUpDownInset())
1113 return static_cast<MathUpDownInset *>(p);
1118 MathArray & MathCursor::array() const
1120 static MathArray dummy;
1121 if (!cursor().par_) {
1122 lyxerr << "############ par_ not valid\n";
1126 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1127 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1131 return cursor().cell();
1135 MathXArray & MathCursor::xarray() const
1137 return cursor().xcell();
1141 bool MathCursor::nextIsInset() const
1143 return cursor().pos_ < array().size() && MathIsInset(nextCode());
1147 bool MathCursor::prevIsInset() const
1149 return cursor().pos_ > 0 && MathIsInset(prevCode());
1153 int MathCursor::xpos() const
1156 return xarray().pos2x(cursor().pos_);
1160 void MathCursor::gotoX(int x)
1162 cursor().pos_ = xarray().x2pos(x);
1166 void MathCursor::idxNext()
1168 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1172 void MathCursor::idxPrev()
1174 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1178 void MathCursor::splitCell()
1180 if (cursor().idx_ == cursor().par_->nargs() - 1)
1182 MathArray ar = array();
1183 ar.erase(0, cursor().pos_);
1184 array().erase(cursor().pos_, array().size());
1187 array().insert(0, ar);
1191 void MathCursor::breakLine()
1193 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1194 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1195 p->mutate(LM_OT_EQNARRAY);
1197 cursor().idx_ = p->nrows();
1203 const int r = row();
1204 for (int c = col() + 1; c < p->ncols(); ++c) {
1205 const int i1 = p->index(r, c);
1206 const int i2 = p->index(r + 1, c);
1207 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1208 p->cell(i1).swap(p->cell(i2));
1213 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1218 char MathCursor::valign() const
1222 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1223 return p ? p->valign() : 0;
1227 char MathCursor::halign() const
1231 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1232 return p ? p->halign(idx % p->ncols()) : 0;
1236 MathCursorPos MathCursor::firstSelectionPos() const
1238 MathCursorPos anc = normalAnchor();
1239 return anc < cursor() ? anc : cursor();
1243 MathCursorPos MathCursor::lastSelectionPos() const
1245 MathCursorPos anc = normalAnchor();
1246 return anc < cursor() ? cursor() : anc;
1250 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1252 MathCursorPos anc = normalAnchor();
1253 if (anc < cursor()) {
1263 MathCursorPos & MathCursor::cursor()
1265 return Cursor_.back();
1269 MathCursorPos const & MathCursor::cursor() const
1271 return Cursor_.back();
1276 ////////////////////////////////////////////////////////////////////////
1279 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1281 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1285 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1287 if (ti.par_ != it.par_) {
1288 lyxerr << "can't compare cursor and anchor in different insets\n";
1291 if (ti.idx_ != it.idx_)
1292 return ti.idx_ < it.idx_;
1293 return ti.pos_ < it.pos_;
1297 MathArray & MathCursorPos::cell(int idx) const
1299 return par_->cell(idx);
1302 MathArray & MathCursorPos::cell() const
1304 return par_->cell(idx_);
1308 MathXArray & MathCursorPos::xcell(int idx) const
1310 return par_->xcell(idx);
1314 MathXArray & MathCursorPos::xcell() const
1316 return par_->xcell(idx_);
1320 MathCursorPos MathCursor::normalAnchor() const
1322 // use Anchor on the same level as Cursor
1323 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1324 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1325 // anchor is behind cursor -> move anchor behind the inset
1326 normal.cell().next(normal.pos_);
1328 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1329 // << normal << "\n";
1334 bool MathCursorPos::idxUp()
1336 return par_->idxUp(idx_, pos_);
1340 bool MathCursorPos::idxDown()
1342 return par_->idxDown(idx_, pos_);
1346 bool MathCursorPos::idxLeft()
1348 return par_->idxLeft(idx_, pos_);
1352 bool MathCursorPos::idxRight()
1354 return par_->idxRight(idx_, pos_);