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
24 #include "math_inset.h"
25 #include "math_parser.h"
26 #include "math_cursor.h"
27 #include "math_macro.h"
28 #include "math_macroarg.h"
29 #include "math_macrotable.h"
30 #include "math_root.h"
31 #include "support/lstrings.h"
35 #include "math_matrixinset.h"
36 #include "math_spaceinset.h"
37 #include "math_funcinset.h"
38 #include "math_bigopinset.h"
39 #include "math_fracinset.h"
40 #include "math_decorationinset.h"
41 #include "math_dotsinset.h"
42 #include "math_accentinset.h"
43 #include "math_macrotemplate.h"
44 #include "math_sqrtinset.h"
45 #include "math_scriptinset.h"
46 #include "mathed/support.h"
47 #include "formulabase.h"
59 bool IsMacro(short tok, int id)
61 return tok != LM_TK_STACK &&
67 tok != LM_TK_FUNCLIM &&
68 tok != LM_TK_BIGSYM &&
69 tok != LM_TK_ACCENT &&
70 !(tok == LM_TK_SYM && id < 255);
75 MathCursor::MathCursor(InsetFormulaBase * formula)
85 void MathCursor::push(MathInset * par, bool first)
87 path_.push_back(MathIter());
88 path_.back().par_ = par_;
89 path_.back().idx_ = idx_;
90 path_.back().cursor_ = cursor_;
93 first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
97 void MathCursor::pop()
101 par_ = path_.back().par_;
102 idx_ = path_.back().idx_;
103 cursor_ = path_.back().cursor_;
109 MathInset * MathCursor::parInset(int i) const
111 return path_[i].par_;
114 void MathCursor::dump(char const * what) const
116 lyxerr << "MC: " << what
117 << " cursor: " << cursor_
118 << " anchor: " << anchor_
121 << " sel: " << selection
122 << " data: " << array()
126 void MathCursor::seldump(char const * str) const
128 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
132 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
133 << str << "\nselarray: " << selarray;
134 for (unsigned int i = 0; i < path_.size(); ++i)
135 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
136 lyxerr << "\ncursor: " << cursor_;
137 lyxerr << "\nanchor: " << anchor_;
138 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
142 bool MathCursor::isInside(MathInset * p) const
144 for (unsigned i = 0; i < path_.size(); ++i)
145 if (parInset(i) == p)
151 bool MathCursor::Left(bool sel)
155 // was MacroModeBack()
156 if (!imacro->name().empty()) {
157 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
158 imacro->Metrics(imacro->size());
169 result = array().prev(cursor_);
170 if (!result && !path_.empty()) {
173 result = array().next(anchor_);
176 MathInset * p = prevActiveInset();
178 // We have to move deeper into the previous inset
179 array().prev(cursor_);
183 // The common case, where we are not
184 // entering a deeper inset
185 result = array().prev(cursor_);
187 if (par_->idxLeft(idx_, cursor_)) {
189 } else if (!path_.empty()) {
201 bool MathCursor::Right(bool sel)
215 result = array().next(cursor_);
216 if (!result && !path_.empty()) {
219 result = array().prev(anchor_);
222 MathInset * p = nextActiveInset();
227 result = array().next(cursor_);
229 if (par_->idxRight(idx_, cursor_)) {
231 } else if (!path_.empty()) {
234 array().next(cursor_);
244 void MathCursor::first()
247 par_ = formula_->par();
251 par_->idxFirst(idx_, cursor_);
255 void MathCursor::last()
258 par_ = formula_->par();
262 par_->idxLast(idx_, cursor_);
266 void MathCursor::SetPos(int x, int y)
269 lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
272 lastcode = LM_TC_MIN;
275 par_ = formula()->par();
280 int distmin = 1 << 30; // large enough
281 for (int i = 0; i < par_->nargs(); ++i) {
282 MathXArray const & ar = par_->xcell(i);
283 int x1 = x - ar.xo();
284 int y1 = y - ar.yo();
285 int c = ar.x2pos(x1);
286 int xx = abs(x1 - ar.pos2x(c));
288 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
289 // << " c: " << c << " xo: " << ar.xo() << "\n";
290 if (yy + xx <= distmin) {
296 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
297 if (nextIsActive() && nextInset()->covers(x, y)) {
298 MathInset * p = nextActiveInset();
300 } else if (prevIsActive() && prevInset()->covers(x, y)) {
301 MathInset * p = prevActiveInset();
302 array().prev(cursor_);
311 void MathCursor::Home()
317 if (!par_->idxHome(idx_, cursor_)) {
324 void MathCursor::End()
330 if (!par_->idxEnd(idx_, cursor_)) {
332 array().next(cursor_);
339 void MathCursor::insert(char c, MathTextCodes t)
341 lyxerr << "inserting '" << c << "'\n";
348 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
352 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
353 // was MacroModeinsert(c);
354 imacro->SetName(imacro->name() + static_cast<char>(c));
362 array().insert(cursor_, c, t);
363 array().next(cursor_);
371 void MathCursor::insert(MathInset * p)
382 if (accent && !p->nargs())
385 array().insert(cursor_, p);
386 array().next(cursor_);
394 void MathCursor::Delete()
405 if (cursor_ < array().size())
406 array().erase(cursor_);
409 #warning pullArg disabled
411 //if (cursor_ == 0 && !path_.empty()) {
412 // lyxerr << "Delete: popping...\n";
420 void MathCursor::DelLine()
429 if (par_->nrows() > 1)
434 bool MathCursor::Up(bool sel)
441 int x = xarray().pos2x(cursor_);
442 bool result = par_->idxUp(idx_, cursor_);
443 if (!result && !path_.empty()) {
445 result = par_->idxUp(idx_, cursor_);
447 cursor_ = xarray().x2pos(x);
454 bool MathCursor::Down(bool sel)
461 int x = xarray().pos2x(cursor_);
462 bool result = par_->idxDown(idx_, cursor_);
463 if (!result && !path_.empty()) {
465 result = par_->idxDown(idx_, cursor_);
467 cursor_ = xarray().x2pos(x);
474 bool MathCursor::toggleLimits()
478 MathInset * p = prevInset();
479 bool old = p->GetLimits();
481 return old != p->GetLimits();
485 void MathCursor::SetSize(MathStyles size)
487 par_->UserSetSize(size);
492 void MathCursor::Interpret(string const & s)
494 lyxerr << "Interpret: '" << s << "'\n";
497 MathScriptInset * p = prevScriptInset();
499 p = new MathScriptInset;
509 MathScriptInset * p = prevScriptInset();
511 p = new MathScriptInset;
520 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
521 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
522 insert(new MathSpaceInset(sp));
527 latexkeys const * l = in_word_set(s);
531 p = new MathRootInset;
532 else if (MathMacroTable::hasTemplate(s))
533 p = new MathMacro(MathMacroTable::provideTemplate(s));
535 p = new MathFuncInset(s, LM_OT_UNDEF);
539 p = new MathBigopInset(l->name, l->id);
544 insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
545 LM_TC_BOPS : LM_TC_SYMB);
547 p = new MathFuncInset(l->name);
551 p = new MathFracInset(LM_OT_STACKREL);
555 p = new MathFracInset(LM_OT_FRAC);
559 p = new MathSqrtInset;
563 p = new MathDecorationInset(l->id);
567 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
571 p = new MathSpaceInset(l->id);
575 p = new MathDotsInset(l->name, l->id);
583 p = new MathMacro(MathMacroTable::provideTemplate(s));
587 p = new MathFuncInset(l->name);
595 array().prev(cursor_);
598 p->Metrics(p->size());
603 void MathCursor::MacroModeOpen()
606 imacro = new MathFuncInset("");
610 lyxerr << "Math Warning: Already in macro mode" << endl;
614 void MathCursor::MacroModeClose()
618 latexkeys const * l = in_word_set(imacro->name());
619 if (!imacro->name().empty()
620 && (!l || (l && IsMacro(l->token, l->id)))
621 && !MathMacroTable::hasTemplate(imacro->name()))
624 //imacro->SetName(macrobf);
625 // This guarantees that the string will be removed by destructor
626 imacro->SetType(LM_OT_UNDEF);
628 imacro->SetName(l->name);
631 if (nextInset()->GetType() == LM_OT_ACCENT)
633 static_cast<MathAccentInset*>(nextInset())->getAccentCode());
634 array().erase(cursor_);
635 if (l || MathMacroTable::hasTemplate(imacro->name()))
636 Interpret(imacro->name());
637 imacro->SetName(string());
644 void MathCursor::SelCopy()
648 int const p1 = min(cursor_, anchor_);
649 int const p2 = max(cursor_, anchor_);
651 selarray.erase(p2, selarray.size());
652 selarray.erase(0, p1);
657 void MathCursor::selArray(MathArray & ar) const
659 int const p1 = min(cursor_, anchor_);
660 int const p2 = max(cursor_, anchor_);
662 ar.erase(p2, ar.size());
667 void MathCursor::SelCut()
671 int const p1 = min(cursor_, anchor_);
672 int const p2 = max(cursor_, anchor_);
673 cursor_ = p1; // move cursor to a same position
675 selarray.erase(p2, selarray.size());
676 selarray.erase(0, p1);
677 array().erase(p1, p2);
683 void MathCursor::SelDel()
687 int const p1 = min(cursor_, anchor_);
688 int const p2 = max(cursor_, anchor_);
689 array().erase(p1, p2);
695 void MathCursor::SelPaste()
698 array().insert(cursor_, selarray);
699 cursor_ += selarray.size();
703 void MathCursor::SelHandle(bool sel)
705 if (sel && !selection)
707 if (!sel && selection)
712 void MathCursor::SelStart()
723 void MathCursor::SelClear()
730 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
739 // Balance anchor and cursor
742 par()->GetXY(xo, yo);
743 int w = par()->width();
745 int x1 = xarray().xo() + xarray().pos2x(cursor_);
746 int y1 = xarray().yo();
747 //int a1 = xarray().ascent();
748 //int d1 = xarray().descent();
751 int x = xarray().xo() + xarray().pos2x(anchor_);
752 int y = xarray().yo();
753 int a = xarray().ascent();
754 int d = xarray().descent();
756 // single row selection
769 ypoint[n++] = y1 - a;
774 ypoint[n++] = y1 - a;
776 ypoint[n++] = y1 + d;
780 ypoint[n++] = y1 + d;
786 xpoint[n] = xpoint[0];
787 ypoint[n++] = ypoint[0];
789 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
790 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
791 //for (i = 0; i < np; ++i)
792 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
796 void MathCursor::setAccent(int ac)
798 if (ac > 0 && accent < 8)
799 nestaccent[accent++] = ac;
801 accent = 0; // consumed!
805 int MathCursor::getAccent() const
807 return accent > 0 ? nestaccent[accent - 1] : 0;
811 void MathCursor::doAccent(char c, MathTextCodes t)
815 for (int i = accent - 1; i >= 0; --i) {
817 ac = new MathAccentInset(c, t, nestaccent[i]);
819 ac = new MathAccentInset(ac, nestaccent[i]);
825 accent = 0; // consumed!
829 void MathCursor::doAccent(MathInset * p)
833 for (int i = accent - 1; i >= 0; --i) {
835 ac = new MathAccentInset(p, nestaccent[i]);
837 ac = new MathAccentInset(ac, nestaccent[i]);
843 accent = 0; // consumed!
847 void MathCursor::toggleLastCode(MathTextCodes t)
850 lastcode = LM_TC_VAR;
856 void MathCursor::GetPos(int & x, int & y)
858 x = xarray().xo() + xarray().pos2x(cursor_);
863 MathTextCodes MathCursor::nextCode() const
865 return array().GetCode(cursor_);
869 MathTextCodes MathCursor::prevCode() const
871 return array().GetCode(cursor_ - 1);
875 MathInset * MathCursor::par() const
881 InsetFormulaBase const * MathCursor::formula()
887 int MathCursor::pos() const
893 bool MathCursor::InMacroMode() const
899 bool MathCursor::Selection() const
905 void MathCursor::clearLastCode()
907 lastcode = LM_TC_MIN;
911 void MathCursor::setLastCode(MathTextCodes t)
917 MathTextCodes MathCursor::getLastCode() const
923 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
925 if (par_->GetType() == t) {
926 lyxerr << "enclosing par is current\n";
930 for (int i = path_.size() - 1; i >= 0; --i) {
931 lyxerr << "checking level " << i << "\n";
932 if (path_[i].par_->GetType() == t) {
934 return path_[i].par_;
940 void MathCursor::pullArg()
943 MathArray a = array();
947 array().erase(cursor_);
948 array().insert(cursor_, a);
952 MathStyles MathCursor::style() const
954 return xarray().style();
958 void MathCursor::normalize() const
961 #warning This is evil!
963 MathCursor * it = const_cast<MathCursor *>(this);
965 if (idx_ < 0 || idx_ > par_->nargs())
966 lyxerr << "this should not really happen - 1\n";
967 it->idx_ = max(idx_, 0);
968 it->idx_ = min(idx_, par_->nargs());
970 if (cursor_ < 0 || cursor_ > array().size())
971 lyxerr << "this should not really happen - 2\n";
972 it->cursor_ = max(cursor_, 0);
973 it->cursor_ = min(cursor_, array().size());
977 int MathCursor::col() const
979 return par_->col(idx_);
983 int MathCursor::row() const
985 return par_->row(idx_);
990 char MathIter::GetChar() const
992 return array().GetChar(cursor_);
996 string MathIter::readString()
999 int code = nextCode();
1000 for ( ; OK() && nextCode() == code; Next())
1007 MathInset * MathCursor::prevInset() const
1011 if (!array().prev(c))
1013 return array().GetInset(c);
1016 MathInset * MathCursor::prevActiveInset() const
1018 if (cursor_ <= 0 || !array().isInset(cursor_ - 1))
1020 MathInset * inset = prevInset();
1021 return inset->nargs() ? inset : 0;
1025 MathInset * MathCursor::nextInset() const
1028 return array().GetInset(cursor_);
1032 MathInset * MathCursor::nextActiveInset() const
1034 if (!array().isInset(cursor_))
1036 MathInset * inset = nextInset();
1037 return inset->nargs() ? inset : 0;
1041 MathScriptInset * MathCursor::prevScriptInset() const
1044 return array().prevScriptInset(cursor_);
1049 MathArray & MathCursor::array() const
1051 static MathArray dummy;
1053 lyxerr << "############ par_ not valid\n";
1057 if (idx_ < 0 || idx_ >= par_->nargs()) {
1058 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1062 return par_->cell(idx_);
1066 MathXArray & MathCursor::xarray() const
1068 return par_->xcell(idx_);
1073 bool MathCursor::nextIsInset() const
1075 return cursor_ < array().size() && MathIsInset(nextCode());
1079 bool MathCursor::nextIsActive() const
1081 return nextIsInset() && nextInset()->nargs();
1085 bool MathCursor::prevIsInset() const
1087 return cursor_ > 0 && MathIsInset(prevCode());
1091 bool MathCursor::prevIsActive() const
1093 return prevIsInset() && prevInset()->nargs();
1097 bool MathCursor::IsFont() const
1099 return MathIsFont(nextCode());
1103 bool MathCursor::IsScript() const
1106 return MathIsScript(nextCode());
1110 int MathCursor::xpos() const
1113 return xarray().pos2x(cursor_);
1116 void MathCursor::gotoX(int x)
1118 cursor_ = xarray().x2pos(x);
1121 void MathCursor::idxRight()
1123 par_->idxRight(idx_, cursor_);