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(MathIter());
90 path_.back().par_ = par_;
91 path_.back().idx_ = idx_;
92 path_.back().cursor_ = cursor_;
95 first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
99 bool MathCursor::pop()
103 par_ = path_.back().par_;
104 idx_ = path_.back().idx_;
105 cursor_ = path_.back().cursor_;
112 MathInset * MathCursor::parInset(int i) const
114 return path_[i].par_;
117 void MathCursor::dump(char const * what) const
121 lyxerr << "MC: " << what
122 << " cursor: " << cursor_
123 << " anchor: " << anchor_
126 << " sel: " << selection
127 << " data: " << array()
131 void MathCursor::seldump(char const * str) const
133 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
137 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
138 << str << "\nselarray: " << selarray;
139 for (unsigned int i = 0; i < path_.size(); ++i)
140 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
141 lyxerr << "\ncursor: " << cursor_;
142 lyxerr << "\nanchor: " << anchor_;
143 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
147 bool MathCursor::isInside(MathInset * p) const
149 for (unsigned i = 0; i < path_.size(); ++i)
150 if (parInset(i) == p)
156 bool MathCursor::Left(bool sel)
160 // was MacroModeBack()
161 if (!imacro->name().empty()) {
162 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
163 imacro->Metrics(imacro->size());
174 result = array().prev(cursor_);
175 if (!result && pop()) {
177 result = array().next(anchor_);
180 MathInset * p = prevInset();
181 if (p && p->isActive()) {
182 // We have to move deeper into the previous inset
183 array().prev(cursor_);
187 // The common case, where we are not
188 // entering a deeper inset
189 result = array().prev(cursor_);
191 if (par_->idxLeft(idx_, cursor_)) {
204 bool MathCursor::plainRight()
206 return array().next(cursor_);
210 bool MathCursor::Right(bool sel)
224 result = array().next(cursor_);
225 if (!result && pop()) {
227 result = array().next(cursor_);
230 MathInset * p = nextInset();
231 if (p && p->isActive()) {
235 result = array().next(cursor_);
237 if (par_->idxRight(idx_, cursor_)) {
241 array().next(cursor_);
251 void MathCursor::first()
254 par_ = formula_->par();
258 par_->idxFirst(idx_, cursor_);
262 void MathCursor::last()
265 par_ = formula_->par();
269 par_->idxLast(idx_, cursor_);
273 void MathCursor::SetPos(int x, int y)
276 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
279 lastcode = LM_TC_MIN;
282 par_ = formula()->par();
287 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
288 int distmin = 1 << 30; // large enough
289 for (int i = 0; i < par_->nargs(); ++i) {
290 MathXArray const & ar = par_->xcell(i);
291 int x1 = x - ar.xo();
292 int y1 = y - ar.yo();
293 int c = ar.x2pos(x1);
294 int xx = abs(x1 - ar.pos2x(c));
296 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
297 // << " c: " << c << " xo: " << ar.xo() << "\n";
298 if (yy + xx <= distmin) {
304 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
305 MathInset * n = nextInset();
306 MathInset * p = prevInset();
307 if (n && (n->isActive() || n->isUpDownInset()) && n->covers(x, y))
309 else if (p && (p->isActive() || p->isUpDownInset()) && p->covers(x, y)) {
310 array().prev(cursor_);
319 void MathCursor::Home()
325 if (!par_->idxHome(idx_, cursor_)) {
332 void MathCursor::End()
338 if (!par_->idxEnd(idx_, cursor_)) {
340 array().next(cursor_);
346 void MathCursor::insert(char c, MathTextCodes t)
348 //lyxerr << "inserting '" << c << "'\n";
355 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
359 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
360 // was MacroModeinsert(c);
361 imacro->SetName(imacro->name() + static_cast<char>(c));
366 array().insert(cursor_, c, t);
367 array().next(cursor_);
373 void MathCursor::insert(MathInset * p)
384 array().insert(cursor_, p);
385 array().next(cursor_);
389 void MathCursor::Delete()
400 if (cursor_ < array().size())
401 array().erase(cursor_);
403 // delete empty cells if necessary
404 if (cursor_ == 0 && array().size() == 0) {
407 par_->idxDelete(idx_, popit, removeit);
408 if (popit && pop() && removeit)
413 #warning pullArg disabled
415 //if (cursor_ == 0 && !path_.empty()) {
416 // lyxerr << "Delete: popping...\n";
424 void MathCursor::DelLine()
433 if (par_->nrows() > 1)
438 bool MathCursor::Up(bool sel)
445 // check whether we could move into an inset on the right or on the left
446 MathInset * p = nextInset();
449 if (p->idxFirstUp(idx, cursor)) {
462 if (p->idxLastUp(idx, cursor)) {
463 array().prev(cursor_);
474 int x = xarray().pos2x(cursor_);
475 bool result = par_->idxUp(idx_, cursor_);
476 if (!result && pop()) {
477 result = par_->idxUp(idx_, cursor_);
479 cursor_ = xarray().x2pos(x);
486 bool MathCursor::Down(bool sel)
493 // check whether we could move into an inset on the right or on the left
494 MathInset * p = nextInset();
497 if (p->idxFirstDown(idx, cursor)) {
509 if (p->idxLastDown(idx, cursor)) {
510 array().prev(cursor_);
519 int x = xarray().pos2x(cursor_);
520 bool result = par_->idxDown(idx_, cursor_);
521 if (!result && pop()) {
522 result = par_->idxDown(idx_, cursor_);
524 cursor_ = xarray().x2pos(x);
531 bool MathCursor::toggleLimits()
535 MathInset * p = prevInset();
536 int old = p->limits();
537 p->limits(old < 0 ? 1 : -1);
538 return old != p->limits();
542 void MathCursor::SetSize(MathStyles size)
544 par_->UserSetSize(size);
549 void MathCursor::Interpret(string const & s)
551 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
552 in_word_set(s) << " \n";
555 MathUpDownInset * p = nearbyUpDownInset();
557 p = new MathScriptInset(true, false);
559 array().prev(cursor_);
568 MathUpDownInset * p = nearbyUpDownInset();
570 p = new MathScriptInset(false, true);
572 array().prev(cursor_);
580 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
581 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
582 insert(new MathSpaceInset(sp));
587 latexkeys const * l = in_word_set(s);
591 p = new MathRootInset;
592 else if (MathMacroTable::hasTemplate(s))
593 p = new MathMacro(MathMacroTable::provideTemplate(s));
594 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
599 std::istringstream is(s.substr(7).c_str());
600 is >> m >> n >> v_align >> h_align;
603 MathArrayInset * pp = new MathArrayInset(m, n);
604 pp->valign(v_align[0]);
609 p = new MathFuncInset(s, LM_OT_UNDEF);
613 p = new MathBigopInset(l->name, l->id);
618 insert(l->id, MathIsBOPS(l->id) ? LM_TC_BOPS : LM_TC_SYMB);
620 p = new MathFuncInset(l->name);
624 p = new MathFracInset("stackrel");
628 p = new MathFracInset("frac");
632 p = new MathSqrtInset;
635 case LM_TK_DECORATION:
636 p = new MathDecorationInset(l->name, l->id);
640 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
644 p = new MathSpaceInset(l->id);
648 p = new MathDotsInset(l->name, l->id);
652 p = new MathMacro(MathMacroTable::provideTemplate(s));
656 p = new MathFuncInset(l->name);
662 bool oldsel = selection;
667 array().prev(cursor_);
672 p->Metrics(p->size());
677 void MathCursor::MacroModeOpen()
680 imacro = new MathFuncInset("");
684 lyxerr << "Math Warning: Already in macro mode" << endl;
688 void MathCursor::MacroModeClose()
692 latexkeys const * l = in_word_set(imacro->name());
693 if (!imacro->name().empty()
694 && (!l || (l && IsMacro(l->token, l->id)))
695 && !MathMacroTable::hasTemplate(imacro->name()))
698 //imacro->SetName(macrobf);
699 // This guarantees that the string will be removed by destructor
700 imacro->SetType(LM_OT_UNDEF);
702 imacro->SetName(l->name);
705 array().erase(cursor_);
706 if (l || MathMacroTable::hasTemplate(imacro->name()))
707 Interpret(imacro->name());
708 imacro->SetName(string());
715 void MathCursor::SelCopy()
719 int const p1 = min(cursor_, anchor_);
720 int const p2 = max(cursor_, anchor_);
722 selarray.erase(p2, selarray.size());
723 selarray.erase(0, p1);
728 void MathCursor::SelCut()
732 int const p1 = min(cursor_, anchor_);
733 int const p2 = max(cursor_, anchor_);
734 cursor_ = p1; // move cursor to a same position
736 selarray.erase(p2, selarray.size());
737 selarray.erase(0, p1);
738 array().erase(p1, p2);
744 void MathCursor::SelDel()
748 int const p1 = min(cursor_, anchor_);
749 int const p2 = max(cursor_, anchor_);
750 array().erase(p1, p2);
756 void MathCursor::SelPaste()
759 array().insert(cursor_, selarray);
760 cursor_ += selarray.size();
764 void MathCursor::SelHandle(bool sel)
766 if (sel && !selection)
768 if (!sel && selection)
773 void MathCursor::SelStart()
784 void MathCursor::SelClear()
791 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
800 // Balance anchor and cursor
803 par()->GetXY(xo, yo);
804 int w = par()->width();
806 int x1 = xarray().xo() + xarray().pos2x(cursor_);
807 int y1 = xarray().yo();
808 //int a1 = xarray().ascent();
809 //int d1 = xarray().descent();
812 int x = xarray().xo() + xarray().pos2x(anchor_);
813 int y = xarray().yo();
814 int a = xarray().ascent();
815 int d = xarray().descent();
817 // single row selection
830 ypoint[n++] = y1 - a;
835 ypoint[n++] = y1 - a;
837 ypoint[n++] = y1 + d;
841 ypoint[n++] = y1 + d;
847 xpoint[n] = xpoint[0];
848 ypoint[n++] = ypoint[0];
850 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
851 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
852 //for (i = 0; i < np; ++i)
853 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
857 void MathCursor::handleFont(MathTextCodes t)
860 int const p1 = std::min(cursor_, anchor_);
861 int const p2 = std::max(cursor_, anchor_);
862 MathArray & ar = array();
863 for (int pos = p1; pos != p2; ar.next(pos))
864 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
865 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
870 lastcode = LM_TC_VAR;
877 void MathCursor::handleAccent(string const & name, int code)
879 MathDecorationInset * p = new MathDecorationInset(name, code);
882 p->cell(0) = selarray;
888 void MathCursor::handleDelim(int l, int r)
890 MathDelimInset * p = new MathDelimInset(l, r);
893 p->cell(0) = selarray;
900 void MathCursor::GetPos(int & x, int & y)
902 x = xarray().xo() + xarray().pos2x(cursor_);
907 MathTextCodes MathCursor::nextCode() const
909 return array().GetCode(cursor_);
913 MathTextCodes MathCursor::prevCode() const
915 return array().GetCode(cursor_ - 1);
919 MathInset * MathCursor::par() const
925 InsetFormulaBase const * MathCursor::formula()
931 int MathCursor::pos() const
937 bool MathCursor::InMacroMode() const
943 bool MathCursor::Selection() const
949 void MathCursor::clearLastCode()
951 lastcode = LM_TC_MIN;
955 void MathCursor::setLastCode(MathTextCodes t)
961 MathTextCodes MathCursor::getLastCode() const
967 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
969 if (par_->GetType() == t) {
970 //lyxerr << "enclosing par is current\n";
974 for (int i = path_.size() - 1; i >= 0; --i) {
975 lyxerr << "checking level " << i << "\n";
976 if (path_[i].par_->GetType() == t) {
978 return path_[i].par_;
984 void MathCursor::pullArg()
987 MathArray a = array();
991 array().erase(cursor_);
992 array().insert(cursor_, a);
996 MathStyles MathCursor::style() const
998 return xarray().style();
1002 void MathCursor::normalize() const
1004 #ifdef WITH_WARNINGS
1005 #warning This is evil!
1007 MathCursor * it = const_cast<MathCursor *>(this);
1009 if (idx_ < 0 || idx_ > par_->nargs())
1010 lyxerr << "this should not really happen - 1\n";
1011 it->idx_ = max(idx_, 0);
1012 it->idx_ = min(idx_, par_->nargs());
1014 if (cursor_ < 0 || cursor_ > array().size())
1015 lyxerr << "this should not really happen - 2\n";
1016 it->cursor_ = max(cursor_, 0);
1017 it->cursor_ = min(cursor_, array().size());
1021 int MathCursor::col() const
1023 return par_->col(idx_);
1027 int MathCursor::row() const
1029 return par_->row(idx_);
1034 char MathIter::GetChar() const
1036 return array().GetChar(cursor_);
1040 string MathIter::readString()
1043 int code = nextCode();
1044 for ( ; OK() && nextCode() == code; Next())
1051 MathInset * MathCursor::prevInset() const
1055 if (!array().prev(c))
1057 return array().nextInset(c);
1061 MathInset * MathCursor::nextInset() const
1064 return array().nextInset(cursor_);
1068 MathUpDownInset * MathCursor::nearbyUpDownInset() const
1071 MathInset * p = array().prevInset(cursor_);
1072 if (p && p->isUpDownInset())
1073 return static_cast<MathUpDownInset *>(p);
1074 p = array().nextInset(cursor_);
1075 if (p && p->isUpDownInset())
1076 return static_cast<MathUpDownInset *>(p);
1081 MathArray & MathCursor::array() const
1083 static MathArray dummy;
1085 lyxerr << "############ par_ not valid\n";
1089 if (idx_ < 0 || idx_ >= par_->nargs()) {
1090 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1094 return par_->cell(idx_);
1098 MathXArray & MathCursor::xarray() const
1100 return par_->xcell(idx_);
1105 bool MathCursor::nextIsInset() const
1107 return cursor_ < array().size() && MathIsInset(nextCode());
1111 bool MathCursor::prevIsInset() const
1113 return cursor_ > 0 && MathIsInset(prevCode());
1117 int MathCursor::xpos() const
1120 return xarray().pos2x(cursor_);
1123 void MathCursor::gotoX(int x)
1125 cursor_ = xarray().x2pos(x);
1128 void MathCursor::idxNext()
1130 par_->idxNext(idx_, cursor_);
1133 void MathCursor::idxPrev()
1135 par_->idxPrev(idx_, cursor_);
1138 void MathCursor::splitCell()
1140 if (idx_ == par_->nargs() - 1)
1142 MathArray ar = array();
1143 ar.erase(0, cursor_);
1144 array().erase(cursor_, array().size());
1147 array().insert(0, ar);
1150 void MathCursor::breakLine()
1152 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1153 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1154 p->mutate(LM_OT_EQNARRAY);
1162 const int r = row();
1163 for (int c = col() + 1; c < p->ncols(); ++c) {
1164 const int i1 = p->index(r, c);
1165 const int i2 = p->index(r + 1, c);
1166 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1167 p->cell(i1).swap(p->cell(i2));
1172 p->cell(idx_).swap(p->cell(idx_ + p->ncols() - 1));
1176 char MathCursor::valign() const
1180 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1181 return p ? p->valign() : 0;
1184 char MathCursor::halign() const
1188 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1189 return p ? p->halign(idx % p->ncols()) : 0;