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_accentinset.h"
47 #include "math_macrotemplate.h"
48 #include "math_sqrtinset.h"
49 #include "math_scriptinset.h"
50 #include "mathed/support.h"
51 #include "formulabase.h"
64 bool IsMacro(short tok, int id)
66 return tok != LM_TK_STACK &&
72 tok != LM_TK_FUNCLIM &&
73 tok != LM_TK_BIGSYM &&
74 tok != LM_TK_ACCENT &&
75 !(tok == LM_TK_SYM && id < 255);
80 MathCursor::MathCursor(InsetFormulaBase * formula)
89 void MathCursor::push(MathInset * par, bool first)
91 path_.push_back(MathIter());
92 path_.back().par_ = par_;
93 path_.back().idx_ = idx_;
94 path_.back().cursor_ = cursor_;
97 first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
101 bool MathCursor::pop()
105 par_ = path_.back().par_;
106 idx_ = path_.back().idx_;
107 cursor_ = path_.back().cursor_;
114 MathInset * MathCursor::parInset(int i) const
116 return path_[i].par_;
119 void MathCursor::dump(char const * what) const
123 lyxerr << "MC: " << what
124 << " cursor: " << cursor_
125 << " anchor: " << anchor_
128 << " sel: " << selection
129 << " data: " << array()
133 void MathCursor::seldump(char const * str) const
135 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
139 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
140 << str << "\nselarray: " << selarray;
141 for (unsigned int i = 0; i < path_.size(); ++i)
142 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
143 lyxerr << "\ncursor: " << cursor_;
144 lyxerr << "\nanchor: " << anchor_;
145 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
149 bool MathCursor::isInside(MathInset * p) const
151 for (unsigned i = 0; i < path_.size(); ++i)
152 if (parInset(i) == p)
158 bool MathCursor::Left(bool sel)
162 // was MacroModeBack()
163 if (!imacro->name().empty()) {
164 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
165 imacro->Metrics(imacro->size());
176 result = array().prev(cursor_);
177 if (!result && pop()) {
179 result = array().next(anchor_);
182 MathInset * p = prevInset();
183 if (p && p->isActive()) {
184 // We have to move deeper into the previous inset
185 array().prev(cursor_);
189 // The common case, where we are not
190 // entering a deeper inset
191 result = array().prev(cursor_);
193 if (par_->idxLeft(idx_, cursor_)) {
206 bool MathCursor::plainRight()
208 return array().next(cursor_);
212 bool MathCursor::Right(bool sel)
226 result = array().next(cursor_);
227 if (!result && pop()) {
229 result = array().next(cursor_);
232 MathInset * p = nextInset();
233 if (p && p->isActive()) {
237 result = array().next(cursor_);
239 if (par_->idxRight(idx_, cursor_)) {
243 array().next(cursor_);
253 void MathCursor::first()
256 par_ = formula_->par();
260 par_->idxFirst(idx_, cursor_);
264 void MathCursor::last()
267 par_ = formula_->par();
271 par_->idxLast(idx_, cursor_);
275 void MathCursor::SetPos(int x, int y)
278 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
281 lastcode = LM_TC_MIN;
284 par_ = formula()->par();
289 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
290 int distmin = 1 << 30; // large enough
291 for (int i = 0; i < par_->nargs(); ++i) {
292 MathXArray const & ar = par_->xcell(i);
293 int x1 = x - ar.xo();
294 int y1 = y - ar.yo();
295 int c = ar.x2pos(x1);
296 int xx = abs(x1 - ar.pos2x(c));
298 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
299 // << " c: " << c << " xo: " << ar.xo() << "\n";
300 if (yy + xx <= distmin) {
306 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
307 MathInset * n = nextInset();
308 MathInset * p = prevInset();
309 if (n && (n->isActive() || n->isScriptInset()) && n->covers(x, y))
311 else if (p && (p->isActive() || p->isScriptInset()) && p->covers(x, y)) {
312 array().prev(cursor_);
321 void MathCursor::Home()
327 if (!par_->idxHome(idx_, cursor_)) {
334 void MathCursor::End()
340 if (!par_->idxEnd(idx_, cursor_)) {
342 array().next(cursor_);
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_, c, t);
369 array().next(cursor_);
375 void MathCursor::insert(MathInset * p)
386 array().insert(cursor_, p);
387 array().next(cursor_);
391 void MathCursor::Delete()
402 if (cursor_ < array().size())
403 array().erase(cursor_);
405 // delete empty cells if necessary
406 if (cursor_ == 0 && array().size() == 0) {
409 par_->idxDelete(idx_, popit, removeit);
410 if (popit && pop() && removeit)
415 #warning pullArg disabled
417 //if (cursor_ == 0 && !path_.empty()) {
418 // lyxerr << "Delete: popping...\n";
426 void MathCursor::DelLine()
435 if (par_->nrows() > 1)
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)) {
464 if (p->idxLastUp(idx, cursor)) {
465 array().prev(cursor_);
476 int x = xarray().pos2x(cursor_);
477 bool result = par_->idxUp(idx_, cursor_);
478 if (!result && pop()) {
479 result = par_->idxUp(idx_, cursor_);
481 cursor_ = 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, cursor)) {
511 if (p->idxLastDown(idx, cursor)) {
512 array().prev(cursor_);
521 int x = xarray().pos2x(cursor_);
522 bool result = par_->idxDown(idx_, cursor_);
523 if (!result && pop()) {
524 result = par_->idxDown(idx_, cursor_);
526 cursor_ = xarray().x2pos(x);
533 bool MathCursor::toggleLimits()
537 MathInset * p = prevInset();
538 int old = p->limits();
539 p->limits(old == -1 ? 1 : -1);
540 return old != p->limits();
544 void MathCursor::SetSize(MathStyles size)
546 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 MathScriptInset * p = nearbyScriptInset();
559 p = new MathScriptInset;
561 array().prev(cursor_);
571 MathScriptInset * p = nearbyScriptInset();
573 p = new MathScriptInset;
575 array().prev(cursor_);
584 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
585 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
586 insert(new MathSpaceInset(sp));
591 latexkeys const * l = in_word_set(s);
595 p = new MathRootInset;
596 else if (MathMacroTable::hasTemplate(s))
597 p = new MathMacro(MathMacroTable::provideTemplate(s));
598 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
603 std::istringstream is(s.substr(7).c_str());
604 is >> m >> n >> v_align >> h_align;
607 MathArrayInset * pp = new MathArrayInset(m, n);
608 pp->valign(v_align[0]);
613 p = new MathFuncInset(s, LM_OT_UNDEF);
617 p = new MathBigopInset(l->name, l->id);
622 insert(static_cast<byte>(l->id),
624 LM_TC_BOPS : LM_TC_SYMB);
627 p = new MathFuncInset(l->name);
632 p = new MathFracInset("stackrel");
636 p = new MathFracInset("frac");
640 p = new MathSqrtInset;
644 p = new MathDecorationInset(l->id);
648 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
652 p = new MathSpaceInset(l->id);
656 p = new MathDotsInset(l->name, l->id);
660 p = new MathAccentInset(l->id);
664 p = new MathMacro(MathMacroTable::provideTemplate(s));
668 p = new MathFuncInset(l->name);
674 bool oldsel = selection;
679 array().prev(cursor_);
684 p->Metrics(p->size());
689 void MathCursor::MacroModeOpen()
692 imacro = new MathFuncInset("");
696 lyxerr << "Math Warning: Already in macro mode" << endl;
700 void MathCursor::MacroModeClose()
704 latexkeys const * l = in_word_set(imacro->name());
705 if (!imacro->name().empty()
706 && (!l || (l && IsMacro(l->token, l->id)))
707 && !MathMacroTable::hasTemplate(imacro->name()))
710 //imacro->SetName(macrobf);
711 // This guarantees that the string will be removed by destructor
712 imacro->SetType(LM_OT_UNDEF);
714 imacro->SetName(l->name);
717 array().erase(cursor_);
718 if (l || MathMacroTable::hasTemplate(imacro->name()))
719 Interpret(imacro->name());
720 imacro->SetName(string());
727 void MathCursor::SelCopy()
731 int const p1 = min(cursor_, anchor_);
732 int const p2 = max(cursor_, anchor_);
734 selarray.erase(p2, selarray.size());
735 selarray.erase(0, p1);
740 void MathCursor::SelCut()
744 int const p1 = min(cursor_, anchor_);
745 int const p2 = max(cursor_, anchor_);
746 cursor_ = p1; // move cursor to a same position
748 selarray.erase(p2, selarray.size());
749 selarray.erase(0, p1);
750 array().erase(p1, p2);
756 void MathCursor::SelDel()
760 int const p1 = min(cursor_, anchor_);
761 int const p2 = max(cursor_, anchor_);
762 array().erase(p1, p2);
768 void MathCursor::SelPaste()
771 array().insert(cursor_, selarray);
772 cursor_ += selarray.size();
776 void MathCursor::SelHandle(bool sel)
778 if (sel && !selection)
780 if (!sel && selection)
785 void MathCursor::SelStart()
796 void MathCursor::SelClear()
803 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
812 // Balance anchor and cursor
815 par()->GetXY(xo, yo);
816 int w = par()->width();
818 int x1 = xarray().xo() + xarray().pos2x(cursor_);
819 int y1 = xarray().yo();
820 //int a1 = xarray().ascent();
821 //int d1 = xarray().descent();
824 int x = xarray().xo() + xarray().pos2x(anchor_);
825 int y = xarray().yo();
826 int a = xarray().ascent();
827 int d = xarray().descent();
829 // single row selection
842 ypoint[n++] = y1 - a;
847 ypoint[n++] = y1 - a;
849 ypoint[n++] = y1 + d;
853 ypoint[n++] = y1 + d;
859 xpoint[n] = xpoint[0];
860 ypoint[n++] = ypoint[0];
862 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
863 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
864 //for (i = 0; i < np; ++i)
865 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
869 void MathCursor::handleFont(MathTextCodes t)
872 int const p1 = std::min(cursor_, anchor_);
873 int const p2 = std::max(cursor_, anchor_);
874 MathArray & ar = array();
875 for (int pos = p1; pos != p2; ar.next(pos))
876 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
877 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
882 lastcode = LM_TC_VAR;
889 void MathCursor::handleAccent(int code)
891 MathAccentInset * p = new MathAccentInset(code);
894 p->cell(0) = selarray;
899 void MathCursor::handleDelim(int l, int r)
901 MathDelimInset * p = new MathDelimInset(l, r);
904 p->cell(0) = selarray;
910 void MathCursor::GetPos(int & x, int & y)
912 x = xarray().xo() + xarray().pos2x(cursor_);
917 MathTextCodes MathCursor::nextCode() const
919 return array().GetCode(cursor_);
923 MathTextCodes MathCursor::prevCode() const
925 return array().GetCode(cursor_ - 1);
929 MathInset * MathCursor::par() const
935 InsetFormulaBase const * MathCursor::formula()
941 int MathCursor::pos() const
947 bool MathCursor::InMacroMode() const
953 bool MathCursor::Selection() const
959 void MathCursor::clearLastCode()
961 lastcode = LM_TC_MIN;
965 void MathCursor::setLastCode(MathTextCodes t)
971 MathTextCodes MathCursor::getLastCode() const
977 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
979 if (par_->GetType() == t) {
980 //lyxerr << "enclosing par is current\n";
984 for (int i = path_.size() - 1; i >= 0; --i) {
985 lyxerr << "checking level " << i << "\n";
986 if (path_[i].par_->GetType() == t) {
988 return path_[i].par_;
994 void MathCursor::pullArg()
997 MathArray a = array();
1001 array().erase(cursor_);
1002 array().insert(cursor_, a);
1006 MathStyles MathCursor::style() const
1008 return xarray().style();
1012 void MathCursor::normalize() const
1014 #ifdef WITH_WARNINGS
1015 #warning This is evil!
1017 MathCursor * it = const_cast<MathCursor *>(this);
1019 if (idx_ < 0 || idx_ > par_->nargs())
1020 lyxerr << "this should not really happen - 1\n";
1021 it->idx_ = max(idx_, 0);
1022 it->idx_ = min(idx_, par_->nargs());
1024 if (cursor_ < 0 || cursor_ > array().size())
1025 lyxerr << "this should not really happen - 2\n";
1026 it->cursor_ = max(cursor_, 0);
1027 it->cursor_ = min(cursor_, array().size());
1031 int MathCursor::col() const
1033 return par_->col(idx_);
1037 int MathCursor::row() const
1039 return par_->row(idx_);
1044 char MathIter::GetChar() const
1046 return array().GetChar(cursor_);
1050 string MathIter::readString()
1053 int code = nextCode();
1054 for ( ; OK() && nextCode() == code; Next())
1061 MathInset * MathCursor::prevInset() const
1065 if (!array().prev(c))
1067 return array().GetInset(c);
1071 MathInset * MathCursor::nextInset() const
1074 return array().GetInset(cursor_);
1078 MathScriptInset * MathCursor::nearbyScriptInset() const
1081 MathScriptInset * p = array().prevScriptInset(cursor_);
1084 return array().nextScriptInset(cursor_);
1089 MathArray & MathCursor::array() const
1091 static MathArray dummy;
1093 lyxerr << "############ par_ not valid\n";
1097 if (idx_ < 0 || idx_ >= par_->nargs()) {
1098 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1102 return par_->cell(idx_);
1106 MathXArray & MathCursor::xarray() const
1108 return par_->xcell(idx_);
1113 bool MathCursor::nextIsInset() const
1115 return cursor_ < array().size() && MathIsInset(nextCode());
1119 bool MathCursor::prevIsInset() const
1121 return cursor_ > 0 && MathIsInset(prevCode());
1125 int MathCursor::xpos() const
1128 return xarray().pos2x(cursor_);
1131 void MathCursor::gotoX(int x)
1133 cursor_ = xarray().x2pos(x);
1136 void MathCursor::idxNext()
1138 par_->idxNext(idx_, cursor_);
1141 void MathCursor::idxPrev()
1143 par_->idxPrev(idx_, cursor_);
1146 void MathCursor::splitCell()
1148 if (idx_ == par_->nargs() - 1)
1150 MathArray ar = array();
1151 ar.erase(0, cursor_);
1152 array().erase(cursor_, array().size());
1155 array().insert(0, ar);
1158 void MathCursor::breakLine()
1160 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1161 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1162 p->mutate(LM_OT_EQNARRAY);
1170 const int r = row();
1171 for (int c = col() + 1; c < p->ncols(); ++c) {
1172 const int i1 = p->index(r, c);
1173 const int i2 = p->index(r + 1, c);
1174 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1175 p->cell(i1).swap(p->cell(i2));
1180 p->cell(idx_).swap(p->cell(idx_ + p->ncols() - 1));
1184 char MathCursor::valign() const
1188 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1189 return p ? p->valign() : 0;
1192 char MathCursor::halign() const
1196 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1197 return p ? p->halign(idx % p->ncols()) : 0;