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 parts if necessary
419 if (cursor_ == 0 && array().size() == 0) {
420 bool removeit = par_->idxDelete(idx_);
421 if (pop() && removeit)
426 #warning pullArg disabled
428 //if (cursor_ == 0 && !path_.empty()) {
429 // lyxerr << "Delete: popping...\n";
437 void MathCursor::DelLine()
446 if (par_->nrows() > 1)
451 bool MathCursor::Up(bool sel)
458 // check whether we could move into an inset on the right or on the left
459 MathInset * p = nextInset();
462 if (p->idxFirstUp(idx, cursor)) {
475 if (p->idxLastUp(idx, cursor)) {
476 array().prev(cursor_);
487 int x = xarray().pos2x(cursor_);
488 bool result = par_->idxUp(idx_, cursor_);
489 if (!result && pop()) {
490 result = par_->idxUp(idx_, cursor_);
492 cursor_ = xarray().x2pos(x);
499 bool MathCursor::Down(bool sel)
506 // check whether we could move into an inset on the right or on the left
507 MathInset * p = nextInset();
510 if (p->idxFirstDown(idx, cursor)) {
522 if (p->idxLastDown(idx, cursor)) {
523 array().prev(cursor_);
532 int x = xarray().pos2x(cursor_);
533 bool result = par_->idxDown(idx_, cursor_);
534 if (!result && pop()) {
535 result = par_->idxDown(idx_, cursor_);
537 cursor_ = xarray().x2pos(x);
544 bool MathCursor::toggleLimits()
548 MathInset * p = prevInset();
549 int old = p->limits();
550 p->limits(old == -1 ? 1 : -1);
551 return old != p->limits();
555 void MathCursor::SetSize(MathStyles size)
557 par_->UserSetSize(size);
562 void MathCursor::Interpret(string const & s)
564 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
565 in_word_set(s) << " \n";
568 MathScriptInset * p = nearbyScriptInset();
570 p = new MathScriptInset;
572 array().prev(cursor_);
582 MathScriptInset * p = nearbyScriptInset();
584 p = new MathScriptInset;
586 array().prev(cursor_);
595 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
596 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
597 insert(new MathSpaceInset(sp));
602 latexkeys const * l = in_word_set(s);
606 p = new MathRootInset;
607 else if (MathMacroTable::hasTemplate(s))
608 p = new MathMacro(MathMacroTable::provideTemplate(s));
609 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
614 std::istringstream is(s.substr(7).c_str());
615 is >> m >> n >> v_align >> h_align;
618 MathArrayInset * pp = new MathArrayInset(m, n);
619 pp->valign(v_align[0]);
624 p = new MathFuncInset(s, LM_OT_UNDEF);
628 p = new MathBigopInset(l->name, l->id);
633 insert(static_cast<byte>(l->id),
635 LM_TC_BOPS : LM_TC_SYMB);
638 p = new MathFuncInset(l->name);
643 p = new MathFracInset("stackrel");
647 p = new MathFracInset("frac");
651 p = new MathSqrtInset;
655 p = new MathDecorationInset(l->id);
659 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
663 p = new MathSpaceInset(l->id);
667 p = new MathDotsInset(l->name, l->id);
675 p = new MathMacro(MathMacroTable::provideTemplate(s));
679 p = new MathFuncInset(l->name);
685 bool oldsel = selection;
690 array().prev(cursor_);
695 p->Metrics(p->size());
700 void MathCursor::MacroModeOpen()
703 imacro = new MathFuncInset("");
707 lyxerr << "Math Warning: Already in macro mode" << endl;
711 void MathCursor::MacroModeClose()
715 latexkeys const * l = in_word_set(imacro->name());
716 if (!imacro->name().empty()
717 && (!l || (l && IsMacro(l->token, l->id)))
718 && !MathMacroTable::hasTemplate(imacro->name()))
721 //imacro->SetName(macrobf);
722 // This guarantees that the string will be removed by destructor
723 imacro->SetType(LM_OT_UNDEF);
725 imacro->SetName(l->name);
728 if (nextInset()->isAccentInset())
730 static_cast<MathAccentInset*>(nextInset())->getAccentCode());
731 array().erase(cursor_);
732 if (l || MathMacroTable::hasTemplate(imacro->name()))
733 Interpret(imacro->name());
734 imacro->SetName(string());
741 void MathCursor::SelCopy()
745 int const p1 = min(cursor_, anchor_);
746 int const p2 = max(cursor_, anchor_);
748 selarray.erase(p2, selarray.size());
749 selarray.erase(0, p1);
754 void MathCursor::selArray(MathArray & ar) const
756 int const p1 = min(cursor_, anchor_);
757 int const p2 = max(cursor_, anchor_);
759 ar.erase(p2, ar.size());
764 void MathCursor::SelCut()
768 int const p1 = min(cursor_, anchor_);
769 int const p2 = max(cursor_, anchor_);
770 cursor_ = p1; // move cursor to a same position
772 selarray.erase(p2, selarray.size());
773 selarray.erase(0, p1);
774 array().erase(p1, p2);
780 void MathCursor::SelDel()
784 int const p1 = min(cursor_, anchor_);
785 int const p2 = max(cursor_, anchor_);
786 array().erase(p1, p2);
792 void MathCursor::SelPaste()
795 array().insert(cursor_, selarray);
796 cursor_ += selarray.size();
800 void MathCursor::SelHandle(bool sel)
802 if (sel && !selection)
804 if (!sel && selection)
809 void MathCursor::SelStart()
820 void MathCursor::SelClear()
827 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
836 // Balance anchor and cursor
839 par()->GetXY(xo, yo);
840 int w = par()->width();
842 int x1 = xarray().xo() + xarray().pos2x(cursor_);
843 int y1 = xarray().yo();
844 //int a1 = xarray().ascent();
845 //int d1 = xarray().descent();
848 int x = xarray().xo() + xarray().pos2x(anchor_);
849 int y = xarray().yo();
850 int a = xarray().ascent();
851 int d = xarray().descent();
853 // single row selection
866 ypoint[n++] = y1 - a;
871 ypoint[n++] = y1 - a;
873 ypoint[n++] = y1 + d;
877 ypoint[n++] = y1 + d;
883 xpoint[n] = xpoint[0];
884 ypoint[n++] = ypoint[0];
886 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
887 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
888 //for (i = 0; i < np; ++i)
889 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
893 void MathCursor::setAccent(int ac)
895 if (ac > 0 && accent < 8)
896 nestaccent[accent++] = ac;
898 accent = 0; // consumed!
902 int MathCursor::getAccent() const
904 return accent > 0 ? nestaccent[accent - 1] : 0;
908 void MathCursor::doAccent(char c, MathTextCodes t)
912 for (int i = accent - 1; i >= 0; --i) {
914 ac = new MathAccentInset(c, t, nestaccent[i]);
916 ac = new MathAccentInset(ac, nestaccent[i]);
922 accent = 0; // consumed!
926 void MathCursor::doAccent(MathInset * p)
930 for (int i = accent - 1; i >= 0; --i) {
932 ac = new MathAccentInset(p, nestaccent[i]);
934 ac = new MathAccentInset(ac, nestaccent[i]);
940 accent = 0; // consumed!
944 void MathCursor::handleFont(MathTextCodes t)
947 int const p1 = std::min(cursor_, anchor_);
948 int const p2 = std::max(cursor_, anchor_);
949 MathArray & ar = array();
950 for (int pos = p1; pos != p2; ar.next(pos))
951 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
952 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
957 lastcode = LM_TC_VAR;
964 void MathCursor::GetPos(int & x, int & y)
966 x = xarray().xo() + xarray().pos2x(cursor_);
971 MathTextCodes MathCursor::nextCode() const
973 return array().GetCode(cursor_);
977 MathTextCodes MathCursor::prevCode() const
979 return array().GetCode(cursor_ - 1);
983 MathInset * MathCursor::par() const
989 InsetFormulaBase const * MathCursor::formula()
995 int MathCursor::pos() const
1001 bool MathCursor::InMacroMode() const
1007 bool MathCursor::Selection() const
1013 void MathCursor::clearLastCode()
1015 lastcode = LM_TC_MIN;
1019 void MathCursor::setLastCode(MathTextCodes t)
1025 MathTextCodes MathCursor::getLastCode() const
1031 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
1033 if (par_->GetType() == t) {
1034 //lyxerr << "enclosing par is current\n";
1038 for (int i = path_.size() - 1; i >= 0; --i) {
1039 lyxerr << "checking level " << i << "\n";
1040 if (path_[i].par_->GetType() == t) {
1041 idx = path_[i].idx_;
1042 return path_[i].par_;
1048 void MathCursor::pullArg()
1051 MathArray a = array();
1055 array().erase(cursor_);
1056 array().insert(cursor_, a);
1060 MathStyles MathCursor::style() const
1062 return xarray().style();
1066 void MathCursor::normalize() const
1068 #ifdef WITH_WARNINGS
1069 #warning This is evil!
1071 MathCursor * it = const_cast<MathCursor *>(this);
1073 if (idx_ < 0 || idx_ > par_->nargs())
1074 lyxerr << "this should not really happen - 1\n";
1075 it->idx_ = max(idx_, 0);
1076 it->idx_ = min(idx_, par_->nargs());
1078 if (cursor_ < 0 || cursor_ > array().size())
1079 lyxerr << "this should not really happen - 2\n";
1080 it->cursor_ = max(cursor_, 0);
1081 it->cursor_ = min(cursor_, array().size());
1085 int MathCursor::col() const
1087 return par_->col(idx_);
1091 int MathCursor::row() const
1093 return par_->row(idx_);
1098 char MathIter::GetChar() const
1100 return array().GetChar(cursor_);
1104 string MathIter::readString()
1107 int code = nextCode();
1108 for ( ; OK() && nextCode() == code; Next())
1115 MathInset * MathCursor::prevInset() const
1119 if (!array().prev(c))
1121 return array().GetInset(c);
1125 MathInset * MathCursor::nextInset() const
1128 return array().GetInset(cursor_);
1132 MathScriptInset * MathCursor::nearbyScriptInset() const
1135 MathScriptInset * p = array().prevScriptInset(cursor_);
1138 return array().nextScriptInset(cursor_);
1143 MathArray & MathCursor::array() const
1145 static MathArray dummy;
1147 lyxerr << "############ par_ not valid\n";
1151 if (idx_ < 0 || idx_ >= par_->nargs()) {
1152 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1156 return par_->cell(idx_);
1160 MathXArray & MathCursor::xarray() const
1162 return par_->xcell(idx_);
1167 bool MathCursor::nextIsInset() const
1169 return cursor_ < array().size() && MathIsInset(nextCode());
1173 bool MathCursor::prevIsInset() const
1175 return cursor_ > 0 && MathIsInset(prevCode());
1179 bool MathCursor::IsFont() const
1181 return MathIsFont(nextCode());
1185 bool MathCursor::IsScript() const
1188 return MathIsScript(nextCode());
1192 int MathCursor::xpos() const
1195 return xarray().pos2x(cursor_);
1198 void MathCursor::gotoX(int x)
1200 cursor_ = xarray().x2pos(x);
1203 void MathCursor::idxNext()
1205 par_->idxNext(idx_, cursor_);
1208 void MathCursor::idxPrev()
1210 par_->idxPrev(idx_, cursor_);
1213 void MathCursor::splitCell()
1215 if (idx_ == par_->nargs() - 1)
1217 MathArray ar = array();
1218 ar.erase(0, cursor_);
1219 array().erase(cursor_, array().size());
1222 array().insert(0, ar);
1225 void MathCursor::breakLine()
1227 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1228 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION)
1229 p->mutate(LM_OT_EQNARRAY);
1233 const int r = row();
1234 for (int c = col() + 1; c < p->ncols(); ++c) {
1235 const int i1 = p->index(r, c);
1236 const int i2 = p->index(r + 1, c);
1237 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1238 p->cell(i1).swap(p->cell(i2));
1243 MathArray & halfcell = array();
1244 idx_ += p->ncols() - 1;
1245 halfcell.swap(array());
1248 char MathCursor::valign() const
1252 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1253 return p ? p->valign() : 0;
1256 char MathCursor::halign() const
1260 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1261 return p ? p->halign(idx % p->ncols()) : 0;