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_grid.h"
37 #include "math_spaceinset.h"
38 #include "math_funcinset.h"
39 #include "math_bigopinset.h"
40 #include "math_fracinset.h"
41 #include "math_decorationinset.h"
42 #include "math_dotsinset.h"
43 #include "math_accentinset.h"
44 #include "math_macrotemplate.h"
45 #include "math_sqrtinset.h"
46 #include "math_scriptinset.h"
47 #include "mathed/support.h"
48 #include "formulabase.h"
60 bool IsMacro(short tok, int id)
62 return tok != LM_TK_STACK &&
68 tok != LM_TK_FUNCLIM &&
69 tok != LM_TK_BIGSYM &&
70 tok != LM_TK_ACCENT &&
71 !(tok == LM_TK_SYM && id < 255);
76 MathCursor::MathCursor(InsetFormulaBase * formula)
86 void MathCursor::push(MathInset * par, bool first)
88 path_.push_back(MathIter());
89 path_.back().par_ = par_;
90 path_.back().idx_ = idx_;
91 path_.back().cursor_ = cursor_;
94 first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
98 bool MathCursor::pop()
102 par_ = path_.back().par_;
103 idx_ = path_.back().idx_;
104 cursor_ = path_.back().cursor_;
111 MathInset * MathCursor::parInset(int i) const
113 return path_[i].par_;
116 void MathCursor::dump(char const * what) const
118 lyxerr << "MC: " << what
119 << " cursor: " << cursor_
120 << " anchor: " << anchor_
123 << " sel: " << selection
124 << " data: " << array()
128 void MathCursor::seldump(char const * str) const
130 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
134 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
135 << str << "\nselarray: " << selarray;
136 for (unsigned int i = 0; i < path_.size(); ++i)
137 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
138 lyxerr << "\ncursor: " << cursor_;
139 lyxerr << "\nanchor: " << anchor_;
140 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
144 bool MathCursor::isInside(MathInset * p) const
146 for (unsigned i = 0; i < path_.size(); ++i)
147 if (parInset(i) == p)
153 bool MathCursor::Left(bool sel)
157 // was MacroModeBack()
158 if (!imacro->name().empty()) {
159 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
160 imacro->Metrics(imacro->size());
171 result = array().prev(cursor_);
172 if (!result && pop()) {
174 result = array().next(anchor_);
177 MathInset * p = prevActiveInset();
179 // We have to move deeper into the previous inset
180 array().prev(cursor_);
184 // The common case, where we are not
185 // entering a deeper inset
186 result = array().prev(cursor_);
188 if (par_->idxLeft(idx_, cursor_)) {
201 bool MathCursor::plainRight()
203 return array().next(cursor_);
207 bool MathCursor::Right(bool sel)
221 result = array().next(cursor_);
222 if (!result && pop()) {
224 result = array().next(cursor_);
227 MathInset * p = nextActiveInset();
232 result = array().next(cursor_);
234 if (par_->idxRight(idx_, cursor_)) {
238 array().next(cursor_);
248 void MathCursor::first()
251 par_ = formula_->par();
255 par_->idxFirst(idx_, cursor_);
259 void MathCursor::last()
262 par_ = formula_->par();
266 par_->idxLast(idx_, cursor_);
270 void MathCursor::SetPos(int x, int y)
273 lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
276 lastcode = LM_TC_MIN;
279 par_ = formula()->par();
284 int distmin = 1 << 30; // large enough
285 for (int i = 0; i < par_->nargs(); ++i) {
286 MathXArray const & ar = par_->xcell(i);
287 int x1 = x - ar.xo();
288 int y1 = y - ar.yo();
289 int c = ar.x2pos(x1);
290 int xx = abs(x1 - ar.pos2x(c));
292 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
293 // << " c: " << c << " xo: " << ar.xo() << "\n";
294 if (yy + xx <= distmin) {
300 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
301 if (nextIsActive() && nextInset()->covers(x, y)) {
302 MathInset * p = nextActiveInset();
304 } else if (prevIsActive() && prevInset()->covers(x, y)) {
305 MathInset * p = prevActiveInset();
306 array().prev(cursor_);
315 void MathCursor::Home()
321 if (!par_->idxHome(idx_, cursor_)) {
328 void MathCursor::End()
334 if (!par_->idxEnd(idx_, cursor_)) {
336 array().next(cursor_);
343 void MathCursor::insert(char c, MathTextCodes t)
345 lyxerr << "inserting '" << c << "'\n";
352 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
356 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
357 // was MacroModeinsert(c);
358 imacro->SetName(imacro->name() + static_cast<char>(c));
366 array().insert(cursor_, c, t);
367 array().next(cursor_);
375 void MathCursor::insert(MathInset * p)
386 if (accent && !p->nargs())
389 array().insert(cursor_, p);
390 array().next(cursor_);
398 void MathCursor::Delete()
409 if (cursor_ < array().size())
410 array().erase(cursor_);
412 // delete empty cells parts if necessary
413 if (cursor_ == 0 && array().size() == 0) {
414 bool removeit = par_->idxDelete(idx_);
415 if (pop() && removeit)
420 #warning pullArg disabled
422 //if (cursor_ == 0 && !path_.empty()) {
423 // lyxerr << "Delete: popping...\n";
431 void MathCursor::DelLine()
440 if (par_->nrows() > 1)
445 bool MathCursor::Up(bool sel)
452 // check whether we could move into an inset on the right or on the left
453 MathInset * p = nextInset();
456 if (p->idxFirstUp(idx, cursor)) {
469 if (p->idxLastUp(idx, cursor)) {
470 array().prev(cursor_);
481 int x = xarray().pos2x(cursor_);
482 bool result = par_->idxUp(idx_, cursor_);
483 if (!result && pop()) {
484 result = par_->idxUp(idx_, cursor_);
486 cursor_ = xarray().x2pos(x);
493 bool MathCursor::Down(bool sel)
500 // check whether we could move into an inset on the right or on the left
501 MathInset * p = nextInset();
504 if (p->idxFirstDown(idx, cursor)) {
516 if (p->idxLastDown(idx, cursor)) {
517 array().prev(cursor_);
526 int x = xarray().pos2x(cursor_);
527 bool result = par_->idxDown(idx_, cursor_);
528 if (!result && pop()) {
529 result = par_->idxDown(idx_, cursor_);
531 cursor_ = xarray().x2pos(x);
538 bool MathCursor::toggleLimits()
542 MathInset * p = prevInset();
543 int old = p->limits();
544 p->limits(old == -1 ? 1 : -1);
545 return old != p->limits();
549 void MathCursor::SetSize(MathStyles size)
551 par_->UserSetSize(size);
556 void MathCursor::Interpret(string const & s)
558 lyxerr << "Interpret: '" << s << "'\n";
561 MathScriptInset * p = nearbyScriptInset();
563 p = new MathScriptInset;
565 array().prev(cursor_);
575 MathScriptInset * p = nearbyScriptInset();
577 p = new MathScriptInset;
579 array().prev(cursor_);
588 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
589 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
590 insert(new MathSpaceInset(sp));
595 latexkeys const * l = in_word_set(s);
599 p = new MathRootInset;
600 else if (MathMacroTable::hasTemplate(s))
601 p = new MathMacro(MathMacroTable::provideTemplate(s));
603 p = new MathFuncInset(s, LM_OT_UNDEF);
607 p = new MathBigopInset(l->name, l->id);
612 insert(static_cast<byte>(l->id),
614 LM_TC_BOPS : LM_TC_SYMB);
617 p = new MathFuncInset(l->name);
622 p = new MathFracInset(LM_OT_STACKREL);
626 p = new MathFracInset(LM_OT_FRAC);
630 p = new MathSqrtInset;
634 p = new MathDecorationInset(l->id);
638 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
642 p = new MathSpaceInset(l->id);
646 p = new MathDotsInset(l->name, l->id);
654 p = new MathMacro(MathMacroTable::provideTemplate(s));
658 p = new MathFuncInset(l->name);
666 array().prev(cursor_);
669 p->Metrics(p->size());
674 void MathCursor::MacroModeOpen()
677 imacro = new MathFuncInset("");
681 lyxerr << "Math Warning: Already in macro mode" << endl;
685 void MathCursor::MacroModeClose()
689 latexkeys const * l = in_word_set(imacro->name());
690 if (!imacro->name().empty()
691 && (!l || (l && IsMacro(l->token, l->id)))
692 && !MathMacroTable::hasTemplate(imacro->name()))
695 //imacro->SetName(macrobf);
696 // This guarantees that the string will be removed by destructor
697 imacro->SetType(LM_OT_UNDEF);
699 imacro->SetName(l->name);
702 if (nextInset()->GetType() == LM_OT_ACCENT)
704 static_cast<MathAccentInset*>(nextInset())->getAccentCode());
705 array().erase(cursor_);
706 if (l || MathMacroTable::hasTemplate(imacro->name()))
707 Interpret(imacro->name());
708 imacro->SetName(string());
715 void MathCursor::SelCopy()
719 int const p1 = min(cursor_, anchor_);
720 int const p2 = max(cursor_, anchor_);
722 selarray.erase(p2, selarray.size());
723 selarray.erase(0, p1);
728 void MathCursor::selArray(MathArray & ar) const
730 int const p1 = min(cursor_, anchor_);
731 int const p2 = max(cursor_, anchor_);
733 ar.erase(p2, ar.size());
738 void MathCursor::SelCut()
742 int const p1 = min(cursor_, anchor_);
743 int const p2 = max(cursor_, anchor_);
744 cursor_ = p1; // move cursor to a same position
746 selarray.erase(p2, selarray.size());
747 selarray.erase(0, p1);
748 array().erase(p1, p2);
754 void MathCursor::SelDel()
758 int const p1 = min(cursor_, anchor_);
759 int const p2 = max(cursor_, anchor_);
760 array().erase(p1, p2);
766 void MathCursor::SelPaste()
769 array().insert(cursor_, selarray);
770 cursor_ += selarray.size();
774 void MathCursor::SelHandle(bool sel)
776 if (sel && !selection)
778 if (!sel && selection)
783 void MathCursor::SelStart()
794 void MathCursor::SelClear()
801 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
810 // Balance anchor and cursor
813 par()->GetXY(xo, yo);
814 int w = par()->width();
816 int x1 = xarray().xo() + xarray().pos2x(cursor_);
817 int y1 = xarray().yo();
818 //int a1 = xarray().ascent();
819 //int d1 = xarray().descent();
822 int x = xarray().xo() + xarray().pos2x(anchor_);
823 int y = xarray().yo();
824 int a = xarray().ascent();
825 int d = xarray().descent();
827 // single row selection
840 ypoint[n++] = y1 - a;
845 ypoint[n++] = y1 - a;
847 ypoint[n++] = y1 + d;
851 ypoint[n++] = y1 + d;
857 xpoint[n] = xpoint[0];
858 ypoint[n++] = ypoint[0];
860 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
861 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
862 //for (i = 0; i < np; ++i)
863 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
867 void MathCursor::setAccent(int ac)
869 if (ac > 0 && accent < 8)
870 nestaccent[accent++] = ac;
872 accent = 0; // consumed!
876 int MathCursor::getAccent() const
878 return accent > 0 ? nestaccent[accent - 1] : 0;
882 void MathCursor::doAccent(char c, MathTextCodes t)
886 for (int i = accent - 1; i >= 0; --i) {
888 ac = new MathAccentInset(c, t, nestaccent[i]);
890 ac = new MathAccentInset(ac, nestaccent[i]);
896 accent = 0; // consumed!
900 void MathCursor::doAccent(MathInset * p)
904 for (int i = accent - 1; i >= 0; --i) {
906 ac = new MathAccentInset(p, nestaccent[i]);
908 ac = new MathAccentInset(ac, nestaccent[i]);
914 accent = 0; // consumed!
918 void MathCursor::toggleLastCode(MathTextCodes t)
921 lastcode = LM_TC_VAR;
927 void MathCursor::GetPos(int & x, int & y)
929 x = xarray().xo() + xarray().pos2x(cursor_);
934 MathTextCodes MathCursor::nextCode() const
936 return array().GetCode(cursor_);
940 MathTextCodes MathCursor::prevCode() const
942 return array().GetCode(cursor_ - 1);
946 MathInset * MathCursor::par() const
952 InsetFormulaBase const * MathCursor::formula()
958 int MathCursor::pos() const
964 bool MathCursor::InMacroMode() const
970 bool MathCursor::Selection() const
976 void MathCursor::clearLastCode()
978 lastcode = LM_TC_MIN;
982 void MathCursor::setLastCode(MathTextCodes t)
988 MathTextCodes MathCursor::getLastCode() const
994 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
996 if (par_->GetType() == t) {
997 lyxerr << "enclosing par is current\n";
1001 for (int i = path_.size() - 1; i >= 0; --i) {
1002 lyxerr << "checking level " << i << "\n";
1003 if (path_[i].par_->GetType() == t) {
1004 idx = path_[i].idx_;
1005 return path_[i].par_;
1011 void MathCursor::pullArg()
1014 MathArray a = array();
1018 array().erase(cursor_);
1019 array().insert(cursor_, a);
1023 MathStyles MathCursor::style() const
1025 return xarray().style();
1029 void MathCursor::normalize() const
1031 #ifdef WITH_WARNINGS
1032 #warning This is evil!
1034 MathCursor * it = const_cast<MathCursor *>(this);
1036 if (idx_ < 0 || idx_ > par_->nargs())
1037 lyxerr << "this should not really happen - 1\n";
1038 it->idx_ = max(idx_, 0);
1039 it->idx_ = min(idx_, par_->nargs());
1041 if (cursor_ < 0 || cursor_ > array().size())
1042 lyxerr << "this should not really happen - 2\n";
1043 it->cursor_ = max(cursor_, 0);
1044 it->cursor_ = min(cursor_, array().size());
1048 int MathCursor::col() const
1050 return par_->col(idx_);
1054 int MathCursor::row() const
1056 return par_->row(idx_);
1061 char MathIter::GetChar() const
1063 return array().GetChar(cursor_);
1067 string MathIter::readString()
1070 int code = nextCode();
1071 for ( ; OK() && nextCode() == code; Next())
1078 MathInset * MathCursor::prevInset() const
1082 if (!array().prev(c))
1084 return array().GetInset(c);
1087 MathInset * MathCursor::prevActiveInset() const
1089 if (cursor_ <= 0 || !array().isInset(cursor_ - 1))
1091 MathInset * inset = prevInset();
1092 return inset->isActive() ? inset : 0;
1096 MathInset * MathCursor::nextInset() const
1099 return array().GetInset(cursor_);
1103 MathInset * MathCursor::nextActiveInset() const
1105 if (!array().isInset(cursor_))
1107 MathInset * inset = nextInset();
1108 return inset->isActive() ? inset : 0;
1112 MathScriptInset * MathCursor::nearbyScriptInset() const
1115 MathScriptInset * p = array().prevScriptInset(cursor_);
1118 return array().nextScriptInset(cursor_);
1123 MathArray & MathCursor::array() const
1125 static MathArray dummy;
1127 lyxerr << "############ par_ not valid\n";
1131 if (idx_ < 0 || idx_ >= par_->nargs()) {
1132 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1136 return par_->cell(idx_);
1140 MathXArray & MathCursor::xarray() const
1142 return par_->xcell(idx_);
1147 bool MathCursor::nextIsInset() const
1149 return cursor_ < array().size() && MathIsInset(nextCode());
1153 bool MathCursor::nextIsActive() const
1155 return nextIsInset() && nextInset()->isActive();
1159 bool MathCursor::prevIsInset() const
1161 return cursor_ > 0 && MathIsInset(prevCode());
1165 bool MathCursor::prevIsActive() const
1167 return prevIsInset() && prevInset()->isActive();
1171 bool MathCursor::IsFont() const
1173 return MathIsFont(nextCode());
1177 bool MathCursor::IsScript() const
1180 return MathIsScript(nextCode());
1184 int MathCursor::xpos() const
1187 return xarray().pos2x(cursor_);
1190 void MathCursor::gotoX(int x)
1192 cursor_ = xarray().x2pos(x);
1195 void MathCursor::idxRight()
1197 par_->idxRight(idx_, cursor_);
1200 char MathCursor::valign() const
1204 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1205 return p ? p->valign() : 0;
1208 char MathCursor::halign() const
1212 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1213 return p ? p->halign(idx % p->ncols()) : 0;