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_accentinset.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 &&
71 tok != LM_TK_FUNCLIM &&
72 tok != LM_TK_BIGSYM &&
73 tok != LM_TK_ACCENT &&
74 !(tok == LM_TK_SYM && id < 255);
79 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_);
349 void MathCursor::insert(char c, MathTextCodes t)
351 lyxerr << "inserting '" << c << "'\n";
358 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
362 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
363 // was MacroModeinsert(c);
364 imacro->SetName(imacro->name() + static_cast<char>(c));
372 array().insert(cursor_, c, t);
373 array().next(cursor_);
381 void MathCursor::insert(MathInset * p)
392 if (accent && !p->nargs())
395 array().insert(cursor_, p);
396 array().next(cursor_);
404 void MathCursor::Delete()
415 if (cursor_ < array().size())
416 array().erase(cursor_);
418 // delete empty cells if necessary
419 if (cursor_ == 0 && array().size() == 0) {
422 par_->idxDelete(idx_, popit, removeit);
423 if (popit && pop() && removeit)
428 #warning pullArg disabled
430 //if (cursor_ == 0 && !path_.empty()) {
431 // lyxerr << "Delete: popping...\n";
439 void MathCursor::DelLine()
448 if (par_->nrows() > 1)
453 bool MathCursor::Up(bool sel)
460 // check whether we could move into an inset on the right or on the left
461 MathInset * p = nextInset();
464 if (p->idxFirstUp(idx, cursor)) {
477 if (p->idxLastUp(idx, cursor)) {
478 array().prev(cursor_);
489 int x = xarray().pos2x(cursor_);
490 bool result = par_->idxUp(idx_, cursor_);
491 if (!result && pop()) {
492 result = par_->idxUp(idx_, cursor_);
494 cursor_ = xarray().x2pos(x);
501 bool MathCursor::Down(bool sel)
508 // check whether we could move into an inset on the right or on the left
509 MathInset * p = nextInset();
512 if (p->idxFirstDown(idx, cursor)) {
524 if (p->idxLastDown(idx, cursor)) {
525 array().prev(cursor_);
534 int x = xarray().pos2x(cursor_);
535 bool result = par_->idxDown(idx_, cursor_);
536 if (!result && pop()) {
537 result = par_->idxDown(idx_, cursor_);
539 cursor_ = xarray().x2pos(x);
546 bool MathCursor::toggleLimits()
550 MathInset * p = prevInset();
551 int old = p->limits();
552 p->limits(old == -1 ? 1 : -1);
553 return old != p->limits();
557 void MathCursor::SetSize(MathStyles size)
559 par_->UserSetSize(size);
564 void MathCursor::Interpret(string const & s)
566 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
567 in_word_set(s) << " \n";
570 MathScriptInset * p = nearbyScriptInset();
572 p = new MathScriptInset;
574 array().prev(cursor_);
584 MathScriptInset * p = nearbyScriptInset();
586 p = new MathScriptInset;
588 array().prev(cursor_);
597 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
598 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
599 insert(new MathSpaceInset(sp));
604 latexkeys const * l = in_word_set(s);
608 p = new MathRootInset;
609 else if (MathMacroTable::hasTemplate(s))
610 p = new MathMacro(MathMacroTable::provideTemplate(s));
611 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
616 std::istringstream is(s.substr(7).c_str());
617 is >> m >> n >> v_align >> h_align;
620 MathArrayInset * pp = new MathArrayInset(m, n);
621 pp->valign(v_align[0]);
626 p = new MathFuncInset(s, LM_OT_UNDEF);
630 p = new MathBigopInset(l->name, l->id);
635 insert(static_cast<byte>(l->id),
637 LM_TC_BOPS : LM_TC_SYMB);
640 p = new MathFuncInset(l->name);
645 p = new MathFracInset("stackrel");
649 p = new MathFracInset("frac");
653 p = new MathSqrtInset;
657 p = new MathDecorationInset(l->id);
661 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
665 p = new MathSpaceInset(l->id);
669 p = new MathDotsInset(l->name, l->id);
677 p = new MathMacro(MathMacroTable::provideTemplate(s));
681 p = new MathFuncInset(l->name);
687 bool oldsel = selection;
692 array().prev(cursor_);
697 p->Metrics(p->size());
702 void MathCursor::MacroModeOpen()
705 imacro = new MathFuncInset("");
709 lyxerr << "Math Warning: Already in macro mode" << endl;
713 void MathCursor::MacroModeClose()
717 latexkeys const * l = in_word_set(imacro->name());
718 if (!imacro->name().empty()
719 && (!l || (l && IsMacro(l->token, l->id)))
720 && !MathMacroTable::hasTemplate(imacro->name()))
723 //imacro->SetName(macrobf);
724 // This guarantees that the string will be removed by destructor
725 imacro->SetType(LM_OT_UNDEF);
727 imacro->SetName(l->name);
730 if (nextInset()->isAccentInset())
732 static_cast<MathAccentInset*>(nextInset())->getAccentCode());
733 array().erase(cursor_);
734 if (l || MathMacroTable::hasTemplate(imacro->name()))
735 Interpret(imacro->name());
736 imacro->SetName(string());
743 void MathCursor::SelCopy()
747 int const p1 = min(cursor_, anchor_);
748 int const p2 = max(cursor_, anchor_);
750 selarray.erase(p2, selarray.size());
751 selarray.erase(0, p1);
756 void MathCursor::selArray(MathArray & ar) const
758 int const p1 = min(cursor_, anchor_);
759 int const p2 = max(cursor_, anchor_);
761 ar.erase(p2, ar.size());
766 void MathCursor::SelCut()
770 int const p1 = min(cursor_, anchor_);
771 int const p2 = max(cursor_, anchor_);
772 cursor_ = p1; // move cursor to a same position
774 selarray.erase(p2, selarray.size());
775 selarray.erase(0, p1);
776 array().erase(p1, p2);
782 void MathCursor::SelDel()
786 int const p1 = min(cursor_, anchor_);
787 int const p2 = max(cursor_, anchor_);
788 array().erase(p1, p2);
794 void MathCursor::SelPaste()
797 array().insert(cursor_, selarray);
798 cursor_ += selarray.size();
802 void MathCursor::SelHandle(bool sel)
804 if (sel && !selection)
806 if (!sel && selection)
811 void MathCursor::SelStart()
822 void MathCursor::SelClear()
829 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
838 // Balance anchor and cursor
841 par()->GetXY(xo, yo);
842 int w = par()->width();
844 int x1 = xarray().xo() + xarray().pos2x(cursor_);
845 int y1 = xarray().yo();
846 //int a1 = xarray().ascent();
847 //int d1 = xarray().descent();
850 int x = xarray().xo() + xarray().pos2x(anchor_);
851 int y = xarray().yo();
852 int a = xarray().ascent();
853 int d = xarray().descent();
855 // single row selection
868 ypoint[n++] = y1 - a;
873 ypoint[n++] = y1 - a;
875 ypoint[n++] = y1 + d;
879 ypoint[n++] = y1 + d;
885 xpoint[n] = xpoint[0];
886 ypoint[n++] = ypoint[0];
888 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
889 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
890 //for (i = 0; i < np; ++i)
891 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
895 void MathCursor::setAccent(int ac)
897 if (ac > 0 && accent < 8)
898 nestaccent[accent++] = ac;
900 accent = 0; // consumed!
904 int MathCursor::getAccent() const
906 return accent > 0 ? nestaccent[accent - 1] : 0;
910 void MathCursor::doAccent(char c, MathTextCodes t)
914 for (int i = accent - 1; i >= 0; --i) {
916 ac = new MathAccentInset(c, t, nestaccent[i]);
918 ac = new MathAccentInset(ac, nestaccent[i]);
924 accent = 0; // consumed!
928 void MathCursor::doAccent(MathInset * p)
932 for (int i = accent - 1; i >= 0; --i) {
934 ac = new MathAccentInset(p, nestaccent[i]);
936 ac = new MathAccentInset(ac, nestaccent[i]);
942 accent = 0; // consumed!
946 void MathCursor::handleFont(MathTextCodes t)
949 int const p1 = std::min(cursor_, anchor_);
950 int const p2 = std::max(cursor_, anchor_);
951 MathArray & ar = array();
952 for (int pos = p1; pos != p2; ar.next(pos))
953 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
954 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
959 lastcode = LM_TC_VAR;
966 void MathCursor::GetPos(int & x, int & y)
968 x = xarray().xo() + xarray().pos2x(cursor_);
973 MathTextCodes MathCursor::nextCode() const
975 return array().GetCode(cursor_);
979 MathTextCodes MathCursor::prevCode() const
981 return array().GetCode(cursor_ - 1);
985 MathInset * MathCursor::par() const
991 InsetFormulaBase const * MathCursor::formula()
997 int MathCursor::pos() const
1003 bool MathCursor::InMacroMode() const
1009 bool MathCursor::Selection() const
1015 void MathCursor::clearLastCode()
1017 lastcode = LM_TC_MIN;
1021 void MathCursor::setLastCode(MathTextCodes t)
1027 MathTextCodes MathCursor::getLastCode() const
1033 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
1035 if (par_->GetType() == t) {
1036 //lyxerr << "enclosing par is current\n";
1040 for (int i = path_.size() - 1; i >= 0; --i) {
1041 lyxerr << "checking level " << i << "\n";
1042 if (path_[i].par_->GetType() == t) {
1043 idx = path_[i].idx_;
1044 return path_[i].par_;
1050 void MathCursor::pullArg()
1053 MathArray a = array();
1057 array().erase(cursor_);
1058 array().insert(cursor_, a);
1062 MathStyles MathCursor::style() const
1064 return xarray().style();
1068 void MathCursor::normalize() const
1070 #ifdef WITH_WARNINGS
1071 #warning This is evil!
1073 MathCursor * it = const_cast<MathCursor *>(this);
1075 if (idx_ < 0 || idx_ > par_->nargs())
1076 lyxerr << "this should not really happen - 1\n";
1077 it->idx_ = max(idx_, 0);
1078 it->idx_ = min(idx_, par_->nargs());
1080 if (cursor_ < 0 || cursor_ > array().size())
1081 lyxerr << "this should not really happen - 2\n";
1082 it->cursor_ = max(cursor_, 0);
1083 it->cursor_ = min(cursor_, array().size());
1087 int MathCursor::col() const
1089 return par_->col(idx_);
1093 int MathCursor::row() const
1095 return par_->row(idx_);
1100 char MathIter::GetChar() const
1102 return array().GetChar(cursor_);
1106 string MathIter::readString()
1109 int code = nextCode();
1110 for ( ; OK() && nextCode() == code; Next())
1117 MathInset * MathCursor::prevInset() const
1121 if (!array().prev(c))
1123 return array().GetInset(c);
1127 MathInset * MathCursor::nextInset() const
1130 return array().GetInset(cursor_);
1134 MathScriptInset * MathCursor::nearbyScriptInset() const
1137 MathScriptInset * p = array().prevScriptInset(cursor_);
1140 return array().nextScriptInset(cursor_);
1145 MathArray & MathCursor::array() const
1147 static MathArray dummy;
1149 lyxerr << "############ par_ not valid\n";
1153 if (idx_ < 0 || idx_ >= par_->nargs()) {
1154 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1158 return par_->cell(idx_);
1162 MathXArray & MathCursor::xarray() const
1164 return par_->xcell(idx_);
1169 bool MathCursor::nextIsInset() const
1171 return cursor_ < array().size() && MathIsInset(nextCode());
1175 bool MathCursor::prevIsInset() const
1177 return cursor_ > 0 && MathIsInset(prevCode());
1181 int MathCursor::xpos() const
1184 return xarray().pos2x(cursor_);
1187 void MathCursor::gotoX(int x)
1189 cursor_ = xarray().x2pos(x);
1192 void MathCursor::idxNext()
1194 par_->idxNext(idx_, cursor_);
1197 void MathCursor::idxPrev()
1199 par_->idxPrev(idx_, cursor_);
1202 void MathCursor::splitCell()
1204 if (idx_ == par_->nargs() - 1)
1206 MathArray ar = array();
1207 ar.erase(0, cursor_);
1208 array().erase(cursor_, array().size());
1211 array().insert(0, ar);
1214 void MathCursor::breakLine()
1216 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1217 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1218 p->mutate(LM_OT_EQNARRAY);
1226 const int r = row();
1227 for (int c = col() + 1; c < p->ncols(); ++c) {
1228 const int i1 = p->index(r, c);
1229 const int i2 = p->index(r + 1, c);
1230 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1231 p->cell(i1).swap(p->cell(i2));
1236 p->cell(idx_).swap(p->cell(idx_ + p->ncols() - 1));
1240 char MathCursor::valign() const
1244 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1245 return p ? p->valign() : 0;
1248 char MathCursor::halign() const
1252 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1253 return p ? p->halign(idx % p->ncols()) : 0;