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
25 #include "math_inset.h"
26 #include "math_arrayinset.h"
27 #include "math_parser.h"
28 #include "math_cursor.h"
29 #include "math_macro.h"
30 #include "math_macroarg.h"
31 #include "math_macrotable.h"
32 #include "math_root.h"
33 #include "support/lstrings.h"
37 #include "math_matrixinset.h"
38 #include "math_grid.h"
39 #include "math_spaceinset.h"
40 #include "math_funcinset.h"
41 #include "math_bigopinset.h"
42 #include "math_fracinset.h"
43 #include "math_decorationinset.h"
44 #include "math_dotsinset.h"
45 #include "math_deliminset.h"
46 #include "math_macrotemplate.h"
47 #include "math_sqrtinset.h"
48 #include "math_scriptinset.h"
49 #include "mathed/support.h"
50 #include "formulabase.h"
63 bool IsMacro(short tok, int id)
65 return tok != LM_TK_STACK &&
68 tok != LM_TK_DECORATION &&
71 tok != LM_TK_FUNCLIM &&
72 tok != LM_TK_BIGSYM &&
73 !(tok == LM_TK_SYM && id < 255);
78 MathCursor::MathCursor(InsetFormulaBase * formula)
87 void MathCursor::push(MathInset * par, bool first)
89 path_.push_back(cursor_);
92 par->idxFirst(cursor_.idx_, cursor_.pos_);
94 par->idxLast(cursor_.idx_, cursor_.pos_);
98 bool MathCursor::pop()
102 cursor_ = path_.back();
109 MathInset * MathCursor::parInset(int i) const
111 return path_[i].par_;
114 void MathCursor::dump(char const * what) const
118 lyxerr << "MC: " << what
119 << " cursor.pos: " << cursor_.pos_
120 << " cursor.idx: " << cursor_.idx_
121 << " cursor.par: " << cursor_.par_
122 << " anchor.pos: " << anchor_.pos_
123 << " anchor.idx: " << anchor_.idx_
124 << " anchor.par: " << anchor_.par_
125 << " sel: " << selection
126 << " data: " << array()
130 void MathCursor::seldump(char const * str) const
132 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
136 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
137 << str << "\nselarray: " << selarray;
138 for (unsigned int i = 0; i < path_.size(); ++i)
139 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
140 lyxerr << "\ncursor.pos_: " << cursor_.pos_;
141 lyxerr << "\nanchor.pos_: " << anchor_.pos_;
142 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
146 bool MathCursor::isInside(MathInset * p) const
148 for (unsigned i = 0; i < path_.size(); ++i)
149 if (parInset(i) == p)
151 return cursor_.par_ == p;
155 bool MathCursor::plainLeft()
157 return array().prev(cursor_.pos_);
160 bool MathCursor::Left(bool sel)
164 // was MacroModeBack()
165 if (!imacro->name().empty()) {
166 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
167 imacro->Metrics(imacro->size());
178 result = array().prev(cursor_.pos_);
179 if (!result && pop()) {
180 anchor_.pos_ = cursor_.pos_;
181 result = array().next(anchor_.pos_);
184 MathInset * p = prevInset();
185 if (p && p->isActive()) {
186 // We have to move deeper into the previous inset
187 array().prev(cursor_.pos_);
191 // The common case, where we are not
192 // entering a deeper inset
193 result = array().prev(cursor_.pos_);
195 if (cursor_.par_->idxLeft(cursor_.idx_, cursor_.pos_)) {
208 bool MathCursor::plainRight()
210 return array().next(cursor_.pos_);
214 bool MathCursor::Right(bool sel)
227 result = array().next(cursor_.pos_);
228 if (!result && pop()) {
229 anchor_.pos_ = cursor_.pos_;
230 result = array().next(cursor_.pos_);
233 MathInset * p = nextInset();
234 if (p && p->isActive()) {
238 result = array().next(cursor_.pos_);
240 if (cursor_.par_->idxRight(cursor_.idx_, cursor_.pos_)) {
244 array().next(cursor_.pos_);
254 void MathCursor::first()
257 cursor_.par_ = formula_->par();
261 cursor_.par_->idxFirst(cursor_.idx_, cursor_.pos_);
265 void MathCursor::last()
268 cursor_.par_ = formula_->par();
272 cursor_.par_->idxLast(cursor_.idx_, cursor_.pos_);
276 void MathCursor::SetPos(int x, int y)
279 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
282 lastcode = LM_TC_MIN;
285 cursor_.par_ = formula()->par();
290 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor_.pos_ << "\n";
291 int distmin = 1 << 30; // large enough
292 for (int i = 0; i < cursor_.par_->nargs(); ++i) {
293 MathXArray const & ar = cursor_.par_->xcell(i);
294 int x1 = x - ar.xo();
295 int y1 = y - ar.yo();
296 int c = ar.x2pos(x1);
297 int xx = abs(x1 - ar.pos2x(c));
299 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
300 // << " c: " << c << " xo: " << ar.xo() << "\n";
301 if (yy + xx <= distmin) {
307 lyxerr << "found idx: " << cursor_.idx_ << " cursor: " << cursor_.pos_ << "\n";
308 MathInset * n = nextInset();
309 MathInset * p = prevInset();
310 if (n && (n->isActive() || n->isUpDownInset()) && n->covers(x, y))
312 else if (p && (p->isActive() || p->isUpDownInset()) && p->covers(x, y)) {
313 array().prev(cursor_.pos_);
322 void MathCursor::Home()
328 if (!cursor_.par_->idxHome(cursor_.idx_, cursor_.pos_))
334 void MathCursor::End()
340 if (!cursor_.par_->idxEnd(cursor_.idx_, cursor_.pos_)) {
342 array().next(cursor_.pos_);
348 void MathCursor::insert(char c, MathTextCodes t)
350 //lyxerr << "inserting '" << c << "'\n";
357 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
361 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
362 // was MacroModeinsert(c);
363 imacro->SetName(imacro->name() + static_cast<char>(c));
368 array().insert(cursor_.pos_, c, t);
369 array().next(cursor_.pos_);
375 void MathCursor::insert(MathInset * p)
386 array().insert(cursor_.pos_, p);
387 array().next(cursor_.pos_);
391 void MathCursor::Delete()
402 if (cursor_.pos_ < array().size())
403 array().erase(cursor_.pos_);
405 // delete empty cells if necessary
406 if (cursor_.pos_ == 0 && array().size() == 0) {
409 cursor_.par_->idxDelete(cursor_.idx_, popit, removeit);
410 if (popit && pop() && removeit)
415 #warning pullArg disabled
417 //if (cursor_.pos_ == 0 && !path_.empty()) {
418 // lyxerr << "Delete: popping...\n";
426 void MathCursor::DelLine()
435 if (cursor_.par_->nrows() > 1)
436 cursor_.par_->delRow(row());
440 bool MathCursor::Up(bool sel)
447 // check whether we could move into an inset on the right or on the left
448 MathInset * p = nextInset();
451 if (p->idxFirstUp(idx, cursor)) {
455 cursor_.pos_ = cursor;
464 if (p->idxLastUp(idx, cursor)) {
465 array().prev(cursor_.pos_);
469 cursor_.pos_ = cursor;
476 int x = xarray().pos2x(cursor_.pos_);
477 bool result = cursor_.par_->idxUp(cursor_.idx_, cursor_.pos_);
478 if (!result && pop()) {
479 result = cursor_.par_->idxUp(cursor_.idx_, cursor_.pos_);
481 cursor_.pos_ = xarray().x2pos(x);
488 bool MathCursor::Down(bool sel)
495 // check whether we could move into an inset on the right or on the left
496 MathInset * p = nextInset();
499 if (p->idxFirstDown(idx, pos)) {
511 if (p->idxLastDown(idx, pos)) {
512 array().prev(cursor_.pos_);
521 int x = xarray().pos2x(cursor_.pos_);
522 bool result = cursor_.par_->idxDown(cursor_.idx_, cursor_.pos_);
523 if (!result && pop())
524 result = cursor_.par_->idxDown(cursor_.idx_, cursor_.pos_);
526 cursor_.pos_ = xarray().x2pos(x);
533 bool MathCursor::toggleLimits()
537 MathInset * p = prevInset();
538 int old = p->limits();
539 p->limits(old < 0 ? 1 : -1);
540 return old != p->limits();
544 void MathCursor::SetSize(MathStyles size)
546 cursor_.par_->UserSetSize(size);
551 void MathCursor::Interpret(string const & s)
553 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
554 in_word_set(s) << " \n";
557 MathUpDownInset * p = nearbyUpDownInset();
559 p = new MathScriptInset(true, false);
561 array().prev(cursor_.pos_);
570 MathUpDownInset * p = nearbyUpDownInset();
572 p = new MathScriptInset(false, true);
574 array().prev(cursor_.pos_);
582 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
583 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
584 insert(new MathSpaceInset(sp));
589 latexkeys const * l = in_word_set(s);
593 p = new MathRootInset;
594 else if (MathMacroTable::hasTemplate(s))
595 p = new MathMacro(MathMacroTable::provideTemplate(s));
596 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
601 istringstream is(s.substr(7).c_str());
602 is >> m >> n >> v_align >> h_align;
605 MathArrayInset * pp = new MathArrayInset(m, n);
606 pp->valign(v_align[0]);
611 p = new MathFuncInset(s, LM_OT_UNDEF);
615 p = new MathBigopInset(l->name, l->id);
620 insert(l->id, MathIsBOPS(l->id) ? LM_TC_BOPS : LM_TC_SYMB);
622 p = new MathFuncInset(l->name);
626 p = new MathFracInset("stackrel");
630 p = new MathFracInset("frac");
634 p = new MathSqrtInset;
637 case LM_TK_DECORATION:
638 p = new MathDecorationInset(l->name, l->id);
642 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
646 p = new MathSpaceInset(l->id);
650 p = new MathDotsInset(l->name, l->id);
654 p = new MathMacro(MathMacroTable::provideTemplate(s));
658 p = new MathFuncInset(l->name);
664 bool oldsel = selection;
669 array().prev(cursor_.pos_);
674 p->Metrics(p->size());
679 void MathCursor::MacroModeOpen()
682 imacro = new MathFuncInset("");
686 lyxerr << "Math Warning: Already in macro mode" << endl;
690 void MathCursor::MacroModeClose()
694 latexkeys const * l = in_word_set(imacro->name());
695 if (!imacro->name().empty()
696 && (!l || (l && IsMacro(l->token, l->id)))
697 && !MathMacroTable::hasTemplate(imacro->name()))
700 //imacro->SetName(macrobf);
701 // This guarantees that the string will be removed by destructor
702 imacro->SetType(LM_OT_UNDEF);
704 imacro->SetName(l->name);
707 array().erase(cursor_.pos_);
708 if (l || MathMacroTable::hasTemplate(imacro->name()))
709 Interpret(imacro->name());
710 imacro->SetName(string());
717 void MathCursor::SelCopy()
721 int const p1 = min(cursor_.pos_, anchor_.pos_);
722 int const p2 = max(cursor_.pos_, anchor_.pos_);
724 selarray.erase(p2, selarray.size());
725 selarray.erase(0, p1);
730 void MathCursor::SelCut()
734 int const p1 = min(cursor_.pos_, anchor_.pos_);
735 int const p2 = max(cursor_.pos_, anchor_.pos_);
736 cursor_.pos_ = p1; // move cursor to a same position
738 selarray.erase(p2, selarray.size());
739 selarray.erase(0, p1);
740 array().erase(p1, p2);
746 void MathCursor::SelDel()
750 int const p1 = min(cursor_.pos_, anchor_.pos_);
751 int const p2 = max(cursor_.pos_, anchor_.pos_);
752 array().erase(p1, p2);
758 void MathCursor::SelPaste()
761 array().insert(cursor_.pos_, selarray);
762 cursor_.pos_ += selarray.size();
766 void MathCursor::SelHandle(bool sel)
768 if (sel && !selection)
770 if (!sel && selection)
775 void MathCursor::SelStart()
786 void MathCursor::SelClear()
793 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
802 // Balance anchor and cursor
805 par()->GetXY(xo, yo);
806 int w = par()->width();
808 int x1 = xarray().xo() + xarray().pos2x(cursor_.pos_);
809 int y1 = xarray().yo();
810 //int a1 = xarray().ascent();
811 //int d1 = xarray().descent();
814 int x = xarray().xo() + xarray().pos2x(anchor_.pos_);
815 int y = xarray().yo();
816 int a = xarray().ascent();
817 int d = xarray().descent();
819 // single row selection
832 ypoint[n++] = y1 - a;
837 ypoint[n++] = y1 - a;
839 ypoint[n++] = y1 + d;
843 ypoint[n++] = y1 + d;
849 xpoint[n] = xpoint[0];
850 ypoint[n++] = ypoint[0];
852 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
853 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
854 //for (i = 0; i < np; ++i)
855 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
859 void MathCursor::handleFont(MathTextCodes t)
862 int const p1 = std::min(cursor_.pos_, anchor_.pos_);
863 int const p2 = std::max(cursor_.pos_, anchor_.pos_);
864 MathArray & ar = array();
865 for (int pos = p1; pos != p2; ar.next(pos))
866 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
867 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
872 lastcode = LM_TC_VAR;
879 void MathCursor::handleAccent(string const & name, int code)
881 MathDecorationInset * p = new MathDecorationInset(name, code);
884 p->cell(0) = selarray;
890 void MathCursor::handleDelim(int l, int r)
892 MathDelimInset * p = new MathDelimInset(l, r);
895 p->cell(0) = selarray;
902 void MathCursor::GetPos(int & x, int & y)
904 x = xarray().xo() + xarray().pos2x(cursor_.pos_);
909 MathTextCodes MathCursor::nextCode() const
911 return array().GetCode(cursor_.pos_);
915 MathTextCodes MathCursor::prevCode() const
917 return array().GetCode(cursor_.pos_ - 1);
921 MathInset * MathCursor::par() const
927 InsetFormulaBase const * MathCursor::formula()
933 int MathCursor::pos() const
939 bool MathCursor::InMacroMode() const
945 bool MathCursor::Selection() const
951 void MathCursor::clearLastCode()
953 lastcode = LM_TC_MIN;
957 void MathCursor::setLastCode(MathTextCodes t)
963 MathTextCodes MathCursor::getLastCode() const
969 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
971 if (cursor_.par_->GetType() == t) {
972 //lyxerr << "enclosing par is current\n";
976 for (int i = path_.size() - 1; i >= 0; --i) {
977 lyxerr << "checking level " << i << "\n";
978 if (path_[i].par_->GetType() == t) {
980 return path_[i].par_;
986 void MathCursor::pullArg()
989 MathArray a = array();
993 array().erase(cursor_.pos_);
994 array().insert(cursor_.pos_, a);
998 MathStyles MathCursor::style() const
1000 return xarray().style();
1004 void MathCursor::normalize() const
1006 #ifdef WITH_WARNINGS
1007 #warning This is evil!
1009 MathCursor * it = const_cast<MathCursor *>(this);
1011 if (cursor_.idx_ < 0 || cursor_.idx_ > cursor_.par_->nargs())
1012 lyxerr << "this should not really happen - 1\n";
1013 it->cursor_.idx_ = max(cursor_.idx_, 0);
1014 it->cursor_.idx_ = min(cursor_.idx_, cursor_.par_->nargs());
1016 if (cursor_.pos_ < 0 || cursor_.pos_ > array().size())
1017 lyxerr << "this should not really happen - 2\n";
1018 it->cursor_.pos_ = max(cursor_.pos_, 0);
1019 it->cursor_.pos_ = min(cursor_.pos_, array().size());
1023 int MathCursor::col() const
1025 return par()->col(cursor_.idx_);
1029 int MathCursor::row() const
1031 return par()->row(cursor_.idx_);
1036 char MathIter::GetChar() const
1038 return array().GetChar(cursor_.pos_);
1042 string MathIter::readString()
1045 int code = nextCode();
1046 for ( ; OK() && nextCode() == code; Next())
1053 MathInset * MathCursor::prevInset() const
1056 int c = cursor_.pos_;
1057 if (!array().prev(c))
1059 return array().nextInset(c);
1063 MathInset * MathCursor::nextInset() const
1066 return array().nextInset(cursor_.pos_);
1070 MathUpDownInset * MathCursor::nearbyUpDownInset() const
1073 MathInset * p = array().prevInset(cursor_.pos_);
1074 if (p && p->isUpDownInset())
1075 return static_cast<MathUpDownInset *>(p);
1076 p = array().nextInset(cursor_.pos_);
1077 if (p && p->isUpDownInset())
1078 return static_cast<MathUpDownInset *>(p);
1083 MathArray & MathCursor::array() const
1085 static MathArray dummy;
1086 if (!cursor_.par_) {
1087 lyxerr << "############ par_ not valid\n";
1091 if (cursor_.idx_ < 0 || cursor_.idx_ >= cursor_.par_->nargs()) {
1092 lyxerr << "############ idx_ " << cursor_.idx_ << " not valid\n";
1096 return cursor_.par_->cell(cursor_.idx_);
1100 MathXArray & MathCursor::xarray() const
1102 return cursor_.par_->xcell(cursor_.idx_);
1107 bool MathCursor::nextIsInset() const
1109 return cursor_.pos_ < array().size() && MathIsInset(nextCode());
1113 bool MathCursor::prevIsInset() const
1115 return cursor_.pos_ > 0 && MathIsInset(prevCode());
1119 int MathCursor::xpos() const
1122 return xarray().pos2x(cursor_.pos_);
1125 void MathCursor::gotoX(int x)
1127 cursor_.pos_ = xarray().x2pos(x);
1130 void MathCursor::idxNext()
1132 cursor_.par_->idxNext(cursor_.idx_, cursor_.pos_);
1135 void MathCursor::idxPrev()
1137 cursor_.par_->idxPrev(cursor_.idx_, cursor_.pos_);
1140 void MathCursor::splitCell()
1142 if (cursor_.idx_ == cursor_.par_->nargs() - 1)
1144 MathArray ar = array();
1145 ar.erase(0, cursor_.pos_);
1146 array().erase(cursor_.pos_, array().size());
1149 array().insert(0, ar);
1152 void MathCursor::breakLine()
1154 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1155 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1156 p->mutate(LM_OT_EQNARRAY);
1158 cursor_.idx_ = p->nrows();
1164 const int r = row();
1165 for (int c = col() + 1; c < p->ncols(); ++c) {
1166 const int i1 = p->index(r, c);
1167 const int i2 = p->index(r + 1, c);
1168 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1169 p->cell(i1).swap(p->cell(i2));
1174 p->cell(cursor_.idx_).swap(p->cell(cursor_.idx_ + p->ncols() - 1));
1178 char MathCursor::valign() const
1182 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1183 return p ? p->valign() : 0;
1186 char MathCursor::halign() const
1190 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1191 return p ? p->halign(idx % p->ncols()) : 0;