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(static_cast<byte>(l->id),
620 LM_TC_BOPS : LM_TC_SYMB);
623 p = new MathFuncInset(l->name);
628 p = new MathFracInset("stackrel");
632 p = new MathFracInset("frac");
636 p = new MathSqrtInset;
639 case LM_TK_DECORATION:
640 p = new MathDecorationInset(l->name, l->id);
644 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
648 p = new MathSpaceInset(l->id);
652 p = new MathDotsInset(l->name, l->id);
656 p = new MathMacro(MathMacroTable::provideTemplate(s));
660 p = new MathFuncInset(l->name);
666 bool oldsel = selection;
671 array().prev(cursor_);
676 p->Metrics(p->size());
681 void MathCursor::MacroModeOpen()
684 imacro = new MathFuncInset("");
688 lyxerr << "Math Warning: Already in macro mode" << endl;
692 void MathCursor::MacroModeClose()
696 latexkeys const * l = in_word_set(imacro->name());
697 if (!imacro->name().empty()
698 && (!l || (l && IsMacro(l->token, l->id)))
699 && !MathMacroTable::hasTemplate(imacro->name()))
702 //imacro->SetName(macrobf);
703 // This guarantees that the string will be removed by destructor
704 imacro->SetType(LM_OT_UNDEF);
706 imacro->SetName(l->name);
709 array().erase(cursor_);
710 if (l || MathMacroTable::hasTemplate(imacro->name()))
711 Interpret(imacro->name());
712 imacro->SetName(string());
719 void MathCursor::SelCopy()
723 int const p1 = min(cursor_, anchor_);
724 int const p2 = max(cursor_, anchor_);
726 selarray.erase(p2, selarray.size());
727 selarray.erase(0, p1);
732 void MathCursor::SelCut()
736 int const p1 = min(cursor_, anchor_);
737 int const p2 = max(cursor_, anchor_);
738 cursor_ = p1; // move cursor to a same position
740 selarray.erase(p2, selarray.size());
741 selarray.erase(0, p1);
742 array().erase(p1, p2);
748 void MathCursor::SelDel()
752 int const p1 = min(cursor_, anchor_);
753 int const p2 = max(cursor_, anchor_);
754 array().erase(p1, p2);
760 void MathCursor::SelPaste()
763 array().insert(cursor_, selarray);
764 cursor_ += selarray.size();
768 void MathCursor::SelHandle(bool sel)
770 if (sel && !selection)
772 if (!sel && selection)
777 void MathCursor::SelStart()
788 void MathCursor::SelClear()
795 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
804 // Balance anchor and cursor
807 par()->GetXY(xo, yo);
808 int w = par()->width();
810 int x1 = xarray().xo() + xarray().pos2x(cursor_);
811 int y1 = xarray().yo();
812 //int a1 = xarray().ascent();
813 //int d1 = xarray().descent();
816 int x = xarray().xo() + xarray().pos2x(anchor_);
817 int y = xarray().yo();
818 int a = xarray().ascent();
819 int d = xarray().descent();
821 // single row selection
834 ypoint[n++] = y1 - a;
839 ypoint[n++] = y1 - a;
841 ypoint[n++] = y1 + d;
845 ypoint[n++] = y1 + d;
851 xpoint[n] = xpoint[0];
852 ypoint[n++] = ypoint[0];
854 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
855 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
856 //for (i = 0; i < np; ++i)
857 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
861 void MathCursor::handleFont(MathTextCodes t)
864 int const p1 = std::min(cursor_, anchor_);
865 int const p2 = std::max(cursor_, anchor_);
866 MathArray & ar = array();
867 for (int pos = p1; pos != p2; ar.next(pos))
868 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
869 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
874 lastcode = LM_TC_VAR;
881 void MathCursor::handleAccent(string const & name, int code)
883 MathDecorationInset * p = new MathDecorationInset(name, code);
886 p->cell(0) = selarray;
892 void MathCursor::handleDelim(int l, int r)
894 MathDelimInset * p = new MathDelimInset(l, r);
897 p->cell(0) = selarray;
904 void MathCursor::GetPos(int & x, int & y)
906 x = xarray().xo() + xarray().pos2x(cursor_);
911 MathTextCodes MathCursor::nextCode() const
913 return array().GetCode(cursor_);
917 MathTextCodes MathCursor::prevCode() const
919 return array().GetCode(cursor_ - 1);
923 MathInset * MathCursor::par() const
929 InsetFormulaBase const * MathCursor::formula()
935 int MathCursor::pos() const
941 bool MathCursor::InMacroMode() const
947 bool MathCursor::Selection() const
953 void MathCursor::clearLastCode()
955 lastcode = LM_TC_MIN;
959 void MathCursor::setLastCode(MathTextCodes t)
965 MathTextCodes MathCursor::getLastCode() const
971 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
973 if (par_->GetType() == t) {
974 //lyxerr << "enclosing par is current\n";
978 for (int i = path_.size() - 1; i >= 0; --i) {
979 lyxerr << "checking level " << i << "\n";
980 if (path_[i].par_->GetType() == t) {
982 return path_[i].par_;
988 void MathCursor::pullArg()
991 MathArray a = array();
995 array().erase(cursor_);
996 array().insert(cursor_, a);
1000 MathStyles MathCursor::style() const
1002 return xarray().style();
1006 void MathCursor::normalize() const
1008 #ifdef WITH_WARNINGS
1009 #warning This is evil!
1011 MathCursor * it = const_cast<MathCursor *>(this);
1013 if (idx_ < 0 || idx_ > par_->nargs())
1014 lyxerr << "this should not really happen - 1\n";
1015 it->idx_ = max(idx_, 0);
1016 it->idx_ = min(idx_, par_->nargs());
1018 if (cursor_ < 0 || cursor_ > array().size())
1019 lyxerr << "this should not really happen - 2\n";
1020 it->cursor_ = max(cursor_, 0);
1021 it->cursor_ = min(cursor_, array().size());
1025 int MathCursor::col() const
1027 return par_->col(idx_);
1031 int MathCursor::row() const
1033 return par_->row(idx_);
1038 char MathIter::GetChar() const
1040 return array().GetChar(cursor_);
1044 string MathIter::readString()
1047 int code = nextCode();
1048 for ( ; OK() && nextCode() == code; Next())
1055 MathInset * MathCursor::prevInset() const
1059 if (!array().prev(c))
1061 return array().nextInset(c);
1065 MathInset * MathCursor::nextInset() const
1068 return array().nextInset(cursor_);
1072 MathUpDownInset * MathCursor::nearbyUpDownInset() const
1075 MathInset * p = array().prevInset(cursor_);
1076 if (p && p->isUpDownInset())
1077 return static_cast<MathUpDownInset *>(p);
1078 p = array().nextInset(cursor_);
1079 if (p && p->isUpDownInset())
1080 return static_cast<MathUpDownInset *>(p);
1085 MathArray & MathCursor::array() const
1087 static MathArray dummy;
1089 lyxerr << "############ par_ not valid\n";
1093 if (idx_ < 0 || idx_ >= par_->nargs()) {
1094 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1098 return par_->cell(idx_);
1102 MathXArray & MathCursor::xarray() const
1104 return par_->xcell(idx_);
1109 bool MathCursor::nextIsInset() const
1111 return cursor_ < array().size() && MathIsInset(nextCode());
1115 bool MathCursor::prevIsInset() const
1117 return cursor_ > 0 && MathIsInset(prevCode());
1121 int MathCursor::xpos() const
1124 return xarray().pos2x(cursor_);
1127 void MathCursor::gotoX(int x)
1129 cursor_ = xarray().x2pos(x);
1132 void MathCursor::idxNext()
1134 par_->idxNext(idx_, cursor_);
1137 void MathCursor::idxPrev()
1139 par_->idxPrev(idx_, cursor_);
1142 void MathCursor::splitCell()
1144 if (idx_ == par_->nargs() - 1)
1146 MathArray ar = array();
1147 ar.erase(0, cursor_);
1148 array().erase(cursor_, array().size());
1151 array().insert(0, ar);
1154 void MathCursor::breakLine()
1156 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1157 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1158 p->mutate(LM_OT_EQNARRAY);
1166 const int r = row();
1167 for (int c = col() + 1; c < p->ncols(); ++c) {
1168 const int i1 = p->index(r, c);
1169 const int i2 = p->index(r + 1, c);
1170 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1171 p->cell(i1).swap(p->cell(i2));
1176 p->cell(idx_).swap(p->cell(idx_ + p->ncols() - 1));
1180 char MathCursor::valign() const
1184 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1185 return p ? p->valign() : 0;
1188 char MathCursor::halign() const
1192 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1193 return p ? p->halign(idx % p->ncols()) : 0;