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_parser.h"
27 #include "math_cursor.h"
28 #include "math_macro.h"
29 #include "math_macroarg.h"
30 #include "math_macrotable.h"
31 #include "math_root.h"
32 #include "support/lstrings.h"
36 #include "math_matrixinset.h"
37 #include "math_grid.h"
38 #include "math_spaceinset.h"
39 #include "math_funcinset.h"
40 #include "math_bigopinset.h"
41 #include "math_fracinset.h"
42 #include "math_decorationinset.h"
43 #include "math_dotsinset.h"
44 #include "math_accentinset.h"
45 #include "math_macrotemplate.h"
46 #include "math_sqrtinset.h"
47 #include "math_scriptinset.h"
48 #include "mathed/support.h"
49 #include "formulabase.h"
62 bool IsMacro(short tok, int id)
64 return tok != LM_TK_STACK &&
70 tok != LM_TK_FUNCLIM &&
71 tok != LM_TK_BIGSYM &&
72 tok != LM_TK_ACCENT &&
73 !(tok == LM_TK_SYM && id < 255);
78 MathCursor::MathCursor(InsetFormulaBase * formula)
88 void MathCursor::push(MathInset * par, bool first)
90 path_.push_back(MathIter());
91 path_.back().par_ = par_;
92 path_.back().idx_ = idx_;
93 path_.back().cursor_ = cursor_;
96 first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
100 bool MathCursor::pop()
104 par_ = path_.back().par_;
105 idx_ = path_.back().idx_;
106 cursor_ = path_.back().cursor_;
113 MathInset * MathCursor::parInset(int i) const
115 return path_[i].par_;
118 void MathCursor::dump(char const * what) const
122 lyxerr << "MC: " << what
123 << " cursor: " << cursor_
124 << " anchor: " << anchor_
127 << " sel: " << selection
128 << " data: " << array()
132 void MathCursor::seldump(char const * str) const
134 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
138 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
139 << str << "\nselarray: " << selarray;
140 for (unsigned int i = 0; i < path_.size(); ++i)
141 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
142 lyxerr << "\ncursor: " << cursor_;
143 lyxerr << "\nanchor: " << anchor_;
144 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
148 bool MathCursor::isInside(MathInset * p) const
150 for (unsigned i = 0; i < path_.size(); ++i)
151 if (parInset(i) == p)
157 bool MathCursor::Left(bool sel)
161 // was MacroModeBack()
162 if (!imacro->name().empty()) {
163 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
164 imacro->Metrics(imacro->size());
175 result = array().prev(cursor_);
176 if (!result && pop()) {
178 result = array().next(anchor_);
181 MathInset * p = prevInset();
182 if (p && p->isActive()) {
183 // We have to move deeper into the previous inset
184 array().prev(cursor_);
188 // The common case, where we are not
189 // entering a deeper inset
190 result = array().prev(cursor_);
192 if (par_->idxLeft(idx_, cursor_)) {
205 bool MathCursor::plainRight()
207 return array().next(cursor_);
211 bool MathCursor::Right(bool sel)
225 result = array().next(cursor_);
226 if (!result && pop()) {
228 result = array().next(cursor_);
231 MathInset * p = nextInset();
232 if (p && p->isActive()) {
236 result = array().next(cursor_);
238 if (par_->idxRight(idx_, cursor_)) {
242 array().next(cursor_);
252 void MathCursor::first()
255 par_ = formula_->par();
259 par_->idxFirst(idx_, cursor_);
263 void MathCursor::last()
266 par_ = formula_->par();
270 par_->idxLast(idx_, cursor_);
274 void MathCursor::SetPos(int x, int y)
277 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
280 lastcode = LM_TC_MIN;
283 par_ = formula()->par();
288 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
289 int distmin = 1 << 30; // large enough
290 for (int i = 0; i < par_->nargs(); ++i) {
291 MathXArray const & ar = par_->xcell(i);
292 int x1 = x - ar.xo();
293 int y1 = y - ar.yo();
294 int c = ar.x2pos(x1);
295 int xx = abs(x1 - ar.pos2x(c));
297 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
298 // << " c: " << c << " xo: " << ar.xo() << "\n";
299 if (yy + xx <= distmin) {
305 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
306 MathInset * n = nextInset();
307 MathInset * p = prevInset();
308 if (n && (n->isActive() || n->isScriptInset()) && n->covers(x, y))
310 else if (p && (p->isActive() || p->isScriptInset()) && p->covers(x, y)) {
311 array().prev(cursor_);
320 void MathCursor::Home()
326 if (!par_->idxHome(idx_, cursor_)) {
333 void MathCursor::End()
339 if (!par_->idxEnd(idx_, cursor_)) {
341 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));
371 array().insert(cursor_, c, t);
372 array().next(cursor_);
380 void MathCursor::insert(MathInset * p)
391 if (accent && !p->nargs())
394 array().insert(cursor_, p);
395 array().next(cursor_);
403 void MathCursor::Delete()
414 if (cursor_ < array().size())
415 array().erase(cursor_);
417 // delete empty cells parts if necessary
418 if (cursor_ == 0 && array().size() == 0) {
419 bool removeit = par_->idxDelete(idx_);
420 if (pop() && removeit)
425 #warning pullArg disabled
427 //if (cursor_ == 0 && !path_.empty()) {
428 // lyxerr << "Delete: popping...\n";
436 void MathCursor::DelLine()
445 if (par_->nrows() > 1)
450 bool MathCursor::Up(bool sel)
457 // check whether we could move into an inset on the right or on the left
458 MathInset * p = nextInset();
461 if (p->idxFirstUp(idx, cursor)) {
474 if (p->idxLastUp(idx, cursor)) {
475 array().prev(cursor_);
486 int x = xarray().pos2x(cursor_);
487 bool result = par_->idxUp(idx_, cursor_);
488 if (!result && pop()) {
489 result = par_->idxUp(idx_, cursor_);
491 cursor_ = xarray().x2pos(x);
498 bool MathCursor::Down(bool sel)
505 // check whether we could move into an inset on the right or on the left
506 MathInset * p = nextInset();
509 if (p->idxFirstDown(idx, cursor)) {
521 if (p->idxLastDown(idx, cursor)) {
522 array().prev(cursor_);
531 int x = xarray().pos2x(cursor_);
532 bool result = par_->idxDown(idx_, cursor_);
533 if (!result && pop()) {
534 result = par_->idxDown(idx_, cursor_);
536 cursor_ = xarray().x2pos(x);
543 bool MathCursor::toggleLimits()
547 MathInset * p = prevInset();
548 int old = p->limits();
549 p->limits(old == -1 ? 1 : -1);
550 return old != p->limits();
554 void MathCursor::SetSize(MathStyles size)
556 par_->UserSetSize(size);
561 void MathCursor::Interpret(string const & s)
563 lyxerr << "Interpret: '" << s << "'\n";
566 MathScriptInset * p = nearbyScriptInset();
568 p = new MathScriptInset;
570 array().prev(cursor_);
580 MathScriptInset * p = nearbyScriptInset();
582 p = new MathScriptInset;
584 array().prev(cursor_);
593 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
594 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
595 insert(new MathSpaceInset(sp));
600 latexkeys const * l = in_word_set(s);
604 p = new MathRootInset;
605 else if (MathMacroTable::hasTemplate(s))
606 p = new MathMacro(MathMacroTable::provideTemplate(s));
608 p = new MathFuncInset(s, LM_OT_UNDEF);
612 p = new MathBigopInset(l->name, l->id);
617 insert(static_cast<byte>(l->id),
619 LM_TC_BOPS : LM_TC_SYMB);
622 p = new MathFuncInset(l->name);
627 p = new MathFracInset(LM_OT_STACKREL);
631 p = new MathFracInset(LM_OT_FRAC);
635 p = new MathSqrtInset;
639 p = new MathDecorationInset(l->id);
643 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
647 p = new MathSpaceInset(l->id);
651 p = new MathDotsInset(l->name, l->id);
659 p = new MathMacro(MathMacroTable::provideTemplate(s));
663 p = new MathFuncInset(l->name);
669 bool oldsel = selection;
674 array().prev(cursor_);
679 p->Metrics(p->size());
684 void MathCursor::MacroModeOpen()
687 imacro = new MathFuncInset("");
691 lyxerr << "Math Warning: Already in macro mode" << endl;
695 void MathCursor::MacroModeClose()
699 latexkeys const * l = in_word_set(imacro->name());
700 if (!imacro->name().empty()
701 && (!l || (l && IsMacro(l->token, l->id)))
702 && !MathMacroTable::hasTemplate(imacro->name()))
705 //imacro->SetName(macrobf);
706 // This guarantees that the string will be removed by destructor
707 imacro->SetType(LM_OT_UNDEF);
709 imacro->SetName(l->name);
712 if (nextInset()->GetType() == LM_OT_ACCENT)
714 static_cast<MathAccentInset*>(nextInset())->getAccentCode());
715 array().erase(cursor_);
716 if (l || MathMacroTable::hasTemplate(imacro->name()))
717 Interpret(imacro->name());
718 imacro->SetName(string());
725 void MathCursor::SelCopy()
729 int const p1 = min(cursor_, anchor_);
730 int const p2 = max(cursor_, anchor_);
732 selarray.erase(p2, selarray.size());
733 selarray.erase(0, p1);
738 void MathCursor::selArray(MathArray & ar) const
740 int const p1 = min(cursor_, anchor_);
741 int const p2 = max(cursor_, anchor_);
743 ar.erase(p2, ar.size());
748 void MathCursor::SelCut()
752 int const p1 = min(cursor_, anchor_);
753 int const p2 = max(cursor_, anchor_);
754 cursor_ = p1; // move cursor to a same position
756 selarray.erase(p2, selarray.size());
757 selarray.erase(0, p1);
758 array().erase(p1, p2);
764 void MathCursor::SelDel()
768 int const p1 = min(cursor_, anchor_);
769 int const p2 = max(cursor_, anchor_);
770 array().erase(p1, p2);
776 void MathCursor::SelPaste()
779 array().insert(cursor_, selarray);
780 cursor_ += selarray.size();
784 void MathCursor::SelHandle(bool sel)
786 if (sel && !selection)
788 if (!sel && selection)
793 void MathCursor::SelStart()
804 void MathCursor::SelClear()
811 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
820 // Balance anchor and cursor
823 par()->GetXY(xo, yo);
824 int w = par()->width();
826 int x1 = xarray().xo() + xarray().pos2x(cursor_);
827 int y1 = xarray().yo();
828 //int a1 = xarray().ascent();
829 //int d1 = xarray().descent();
832 int x = xarray().xo() + xarray().pos2x(anchor_);
833 int y = xarray().yo();
834 int a = xarray().ascent();
835 int d = xarray().descent();
837 // single row selection
850 ypoint[n++] = y1 - a;
855 ypoint[n++] = y1 - a;
857 ypoint[n++] = y1 + d;
861 ypoint[n++] = y1 + d;
867 xpoint[n] = xpoint[0];
868 ypoint[n++] = ypoint[0];
870 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
871 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
872 //for (i = 0; i < np; ++i)
873 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
877 void MathCursor::setAccent(int ac)
879 if (ac > 0 && accent < 8)
880 nestaccent[accent++] = ac;
882 accent = 0; // consumed!
886 int MathCursor::getAccent() const
888 return accent > 0 ? nestaccent[accent - 1] : 0;
892 void MathCursor::doAccent(char c, MathTextCodes t)
896 for (int i = accent - 1; i >= 0; --i) {
898 ac = new MathAccentInset(c, t, nestaccent[i]);
900 ac = new MathAccentInset(ac, nestaccent[i]);
906 accent = 0; // consumed!
910 void MathCursor::doAccent(MathInset * p)
914 for (int i = accent - 1; i >= 0; --i) {
916 ac = new MathAccentInset(p, nestaccent[i]);
918 ac = new MathAccentInset(ac, nestaccent[i]);
924 accent = 0; // consumed!
928 void MathCursor::handleFont(MathTextCodes t)
931 int const p1 = std::min(cursor_, anchor_);
932 int const p2 = std::max(cursor_, anchor_);
933 MathArray & ar = array();
934 for (int pos = p1; pos != p2; ar.next(pos))
935 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
936 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
941 lastcode = LM_TC_VAR;
948 void MathCursor::GetPos(int & x, int & y)
950 x = xarray().xo() + xarray().pos2x(cursor_);
955 MathTextCodes MathCursor::nextCode() const
957 return array().GetCode(cursor_);
961 MathTextCodes MathCursor::prevCode() const
963 return array().GetCode(cursor_ - 1);
967 MathInset * MathCursor::par() const
973 InsetFormulaBase const * MathCursor::formula()
979 int MathCursor::pos() const
985 bool MathCursor::InMacroMode() const
991 bool MathCursor::Selection() const
997 void MathCursor::clearLastCode()
999 lastcode = LM_TC_MIN;
1003 void MathCursor::setLastCode(MathTextCodes t)
1009 MathTextCodes MathCursor::getLastCode() const
1015 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
1017 if (par_->GetType() == t) {
1018 //lyxerr << "enclosing par is current\n";
1022 for (int i = path_.size() - 1; i >= 0; --i) {
1023 lyxerr << "checking level " << i << "\n";
1024 if (path_[i].par_->GetType() == t) {
1025 idx = path_[i].idx_;
1026 return path_[i].par_;
1032 void MathCursor::pullArg()
1035 MathArray a = array();
1039 array().erase(cursor_);
1040 array().insert(cursor_, a);
1044 MathStyles MathCursor::style() const
1046 return xarray().style();
1050 void MathCursor::normalize() const
1052 #ifdef WITH_WARNINGS
1053 #warning This is evil!
1055 MathCursor * it = const_cast<MathCursor *>(this);
1057 if (idx_ < 0 || idx_ > par_->nargs())
1058 lyxerr << "this should not really happen - 1\n";
1059 it->idx_ = max(idx_, 0);
1060 it->idx_ = min(idx_, par_->nargs());
1062 if (cursor_ < 0 || cursor_ > array().size())
1063 lyxerr << "this should not really happen - 2\n";
1064 it->cursor_ = max(cursor_, 0);
1065 it->cursor_ = min(cursor_, array().size());
1069 int MathCursor::col() const
1071 return par_->col(idx_);
1075 int MathCursor::row() const
1077 return par_->row(idx_);
1082 char MathIter::GetChar() const
1084 return array().GetChar(cursor_);
1088 string MathIter::readString()
1091 int code = nextCode();
1092 for ( ; OK() && nextCode() == code; Next())
1099 MathInset * MathCursor::prevInset() const
1103 if (!array().prev(c))
1105 return array().GetInset(c);
1109 MathInset * MathCursor::nextInset() const
1112 return array().GetInset(cursor_);
1116 MathScriptInset * MathCursor::nearbyScriptInset() const
1119 MathScriptInset * p = array().prevScriptInset(cursor_);
1122 return array().nextScriptInset(cursor_);
1127 MathArray & MathCursor::array() const
1129 static MathArray dummy;
1131 lyxerr << "############ par_ not valid\n";
1135 if (idx_ < 0 || idx_ >= par_->nargs()) {
1136 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1140 return par_->cell(idx_);
1144 MathXArray & MathCursor::xarray() const
1146 return par_->xcell(idx_);
1151 bool MathCursor::nextIsInset() const
1153 return cursor_ < array().size() && MathIsInset(nextCode());
1157 bool MathCursor::prevIsInset() const
1159 return cursor_ > 0 && MathIsInset(prevCode());
1163 bool MathCursor::IsFont() const
1165 return MathIsFont(nextCode());
1169 bool MathCursor::IsScript() const
1172 return MathIsScript(nextCode());
1176 int MathCursor::xpos() const
1179 return xarray().pos2x(cursor_);
1182 void MathCursor::gotoX(int x)
1184 cursor_ = xarray().x2pos(x);
1187 void MathCursor::idxNext()
1189 par_->idxNext(idx_, cursor_);
1192 void MathCursor::idxPrev()
1194 par_->idxPrev(idx_, cursor_);
1197 void MathCursor::splitCell()
1199 if (idx_ == par_->nargs() - 1)
1201 MathArray ar = array();
1202 ar.erase(0, cursor_);
1203 array().erase(cursor_, array().size());
1206 array().insert(0, ar);
1209 void MathCursor::breakLine()
1211 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1212 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION)
1213 p->mutate(LM_OT_EQNARRAY);
1217 const int r = row();
1218 for (int c = col() + 1; c < p->ncols(); ++c) {
1219 const int i1 = p->index(r, c);
1220 const int i2 = p->index(r + 1, c);
1221 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1222 p->cell(i1).swap(p->cell(i2));
1227 MathArray & halfcell = array();
1228 idx_ += p->ncols() - 1;
1229 halfcell.swap(array());
1232 char MathCursor::valign() const
1236 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1237 return p ? p->valign() : 0;
1240 char MathCursor::halign() const
1244 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1245 return p ? p->halign(idx % p->ncols()) : 0;