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_deliminset.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 &&
68 tok != LM_TK_DECORATION &&
71 tok != LM_TK_FUNCLIM &&
72 tok != LM_TK_BIGSYM &&
73 !(tok == LM_TK_SYM && id < 255);
78 MathCursor::MathCursor(InsetFormulaBase * formula)
87 void MathCursor::push(MathInset * par, bool first)
89 path_.push_back(MathIter());
90 path_.back().par_ = par_;
91 path_.back().idx_ = idx_;
92 path_.back().cursor_ = cursor_;
95 first ? par_->idxFirst(idx_, cursor_) : par_->idxLast(idx_, cursor_);
99 bool MathCursor::pop()
103 par_ = path_.back().par_;
104 idx_ = path_.back().idx_;
105 cursor_ = path_.back().cursor_;
112 MathInset * MathCursor::parInset(int i) const
114 return path_[i].par_;
117 void MathCursor::dump(char const * what) const
121 lyxerr << "MC: " << what
122 << " cursor: " << cursor_
123 << " anchor: " << anchor_
126 << " sel: " << selection
127 << " data: " << array()
131 void MathCursor::seldump(char const * str) const
133 lyxerr << "SEL: " << str << ": '" << selarray << "'\n";
137 lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
138 << str << "\nselarray: " << selarray;
139 for (unsigned int i = 0; i < path_.size(); ++i)
140 lyxerr << path_[i].par_ << "\n'" << path_[i].par_->cell(0) << "'\n";
141 lyxerr << "\ncursor: " << cursor_;
142 lyxerr << "\nanchor: " << anchor_;
143 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
147 bool MathCursor::isInside(MathInset * p) const
149 for (unsigned i = 0; i < path_.size(); ++i)
150 if (parInset(i) == p)
156 bool MathCursor::plainLeft()
158 return array().prev(cursor_);
161 bool MathCursor::Left(bool sel)
165 // was MacroModeBack()
166 if (!imacro->name().empty()) {
167 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
168 imacro->Metrics(imacro->size());
179 result = array().prev(cursor_);
180 if (!result && pop()) {
182 result = array().next(anchor_);
185 MathInset * p = prevInset();
186 if (p && p->isActive()) {
187 // We have to move deeper into the previous inset
188 array().prev(cursor_);
192 // The common case, where we are not
193 // entering a deeper inset
194 result = array().prev(cursor_);
196 if (par_->idxLeft(idx_, cursor_)) {
209 bool MathCursor::plainRight()
211 return array().next(cursor_);
215 bool MathCursor::Right(bool sel)
228 result = array().next(cursor_);
229 if (!result && pop()) {
231 result = array().next(cursor_);
234 MathInset * p = nextInset();
235 if (p && p->isActive()) {
239 result = array().next(cursor_);
241 if (par_->idxRight(idx_, cursor_)) {
245 array().next(cursor_);
255 void MathCursor::first()
258 par_ = formula_->par();
262 par_->idxFirst(idx_, cursor_);
266 void MathCursor::last()
269 par_ = formula_->par();
273 par_->idxLast(idx_, cursor_);
277 void MathCursor::SetPos(int x, int y)
280 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
283 lastcode = LM_TC_MIN;
286 par_ = formula()->par();
291 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
292 int distmin = 1 << 30; // large enough
293 for (int i = 0; i < par_->nargs(); ++i) {
294 MathXArray const & ar = par_->xcell(i);
295 int x1 = x - ar.xo();
296 int y1 = y - ar.yo();
297 int c = ar.x2pos(x1);
298 int xx = abs(x1 - ar.pos2x(c));
300 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
301 // << " c: " << c << " xo: " << ar.xo() << "\n";
302 if (yy + xx <= distmin) {
308 lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n";
309 MathInset * n = nextInset();
310 MathInset * p = prevInset();
311 if (n && (n->isActive() || n->isUpDownInset()) && n->covers(x, y))
313 else if (p && (p->isActive() || p->isUpDownInset()) && p->covers(x, y)) {
314 array().prev(cursor_);
323 void MathCursor::Home()
329 if (!par_->idxHome(idx_, cursor_))
335 void MathCursor::End()
341 if (!par_->idxEnd(idx_, cursor_)) {
343 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));
369 array().insert(cursor_, c, t);
370 array().next(cursor_);
376 void MathCursor::insert(MathInset * p)
387 array().insert(cursor_, p);
388 array().next(cursor_);
392 void MathCursor::Delete()
403 if (cursor_ < array().size())
404 array().erase(cursor_);
406 // delete empty cells if necessary
407 if (cursor_ == 0 && array().size() == 0) {
410 par_->idxDelete(idx_, popit, removeit);
411 if (popit && pop() && removeit)
416 #warning pullArg disabled
418 //if (cursor_ == 0 && !path_.empty()) {
419 // lyxerr << "Delete: popping...\n";
427 void MathCursor::DelLine()
436 if (par_->nrows() > 1)
441 bool MathCursor::Up(bool sel)
448 // check whether we could move into an inset on the right or on the left
449 MathInset * p = nextInset();
452 if (p->idxFirstUp(idx, cursor)) {
465 if (p->idxLastUp(idx, cursor)) {
466 array().prev(cursor_);
477 int x = xarray().pos2x(cursor_);
478 bool result = par_->idxUp(idx_, cursor_);
479 if (!result && pop()) {
480 result = par_->idxUp(idx_, cursor_);
482 cursor_ = xarray().x2pos(x);
489 bool MathCursor::Down(bool sel)
496 // check whether we could move into an inset on the right or on the left
497 MathInset * p = nextInset();
500 if (p->idxFirstDown(idx, cursor)) {
512 if (p->idxLastDown(idx, cursor)) {
513 array().prev(cursor_);
522 int x = xarray().pos2x(cursor_);
523 bool result = par_->idxDown(idx_, cursor_);
524 if (!result && pop()) {
525 result = par_->idxDown(idx_, cursor_);
527 cursor_ = xarray().x2pos(x);
534 bool MathCursor::toggleLimits()
538 MathInset * p = prevInset();
539 int old = p->limits();
540 p->limits(old < 0 ? 1 : -1);
541 return old != p->limits();
545 void MathCursor::SetSize(MathStyles size)
547 par_->UserSetSize(size);
552 void MathCursor::Interpret(string const & s)
554 lyxerr << "Interpret: '" << s << "' ('" << s.substr(0, 7) << "' " <<
555 in_word_set(s) << " \n";
558 MathUpDownInset * p = nearbyUpDownInset();
560 p = new MathScriptInset(true, false);
562 array().prev(cursor_);
571 MathUpDownInset * p = nearbyUpDownInset();
573 p = new MathScriptInset(false, true);
575 array().prev(cursor_);
583 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
584 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
585 insert(new MathSpaceInset(sp));
590 latexkeys const * l = in_word_set(s);
594 p = new MathRootInset;
595 else if (MathMacroTable::hasTemplate(s))
596 p = new MathMacro(MathMacroTable::provideTemplate(s));
597 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
602 istringstream is(s.substr(7).c_str());
603 is >> m >> n >> v_align >> h_align;
606 MathArrayInset * pp = new MathArrayInset(m, n);
607 pp->valign(v_align[0]);
612 p = new MathFuncInset(s, LM_OT_UNDEF);
616 p = new MathBigopInset(l->name, l->id);
621 insert(l->id, MathIsBOPS(l->id) ? LM_TC_BOPS : LM_TC_SYMB);
623 p = new MathFuncInset(l->name);
627 p = new MathFracInset("stackrel");
631 p = new MathFracInset("frac");
635 p = new MathSqrtInset;
638 case LM_TK_DECORATION:
639 p = new MathDecorationInset(l->name, 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);
655 p = new MathMacro(MathMacroTable::provideTemplate(s));
659 p = new MathFuncInset(l->name);
665 bool oldsel = selection;
670 array().prev(cursor_);
675 p->Metrics(p->size());
680 void MathCursor::MacroModeOpen()
683 imacro = new MathFuncInset("");
687 lyxerr << "Math Warning: Already in macro mode" << endl;
691 void MathCursor::MacroModeClose()
695 latexkeys const * l = in_word_set(imacro->name());
696 if (!imacro->name().empty()
697 && (!l || (l && IsMacro(l->token, l->id)))
698 && !MathMacroTable::hasTemplate(imacro->name()))
701 //imacro->SetName(macrobf);
702 // This guarantees that the string will be removed by destructor
703 imacro->SetType(LM_OT_UNDEF);
705 imacro->SetName(l->name);
708 array().erase(cursor_);
709 if (l || MathMacroTable::hasTemplate(imacro->name()))
710 Interpret(imacro->name());
711 imacro->SetName(string());
718 void MathCursor::SelCopy()
722 int const p1 = min(cursor_, anchor_);
723 int const p2 = max(cursor_, anchor_);
725 selarray.erase(p2, selarray.size());
726 selarray.erase(0, p1);
731 void MathCursor::SelCut()
735 int const p1 = min(cursor_, anchor_);
736 int const p2 = max(cursor_, anchor_);
737 cursor_ = p1; // move cursor to a same position
739 selarray.erase(p2, selarray.size());
740 selarray.erase(0, p1);
741 array().erase(p1, p2);
747 void MathCursor::SelDel()
751 int const p1 = min(cursor_, anchor_);
752 int const p2 = max(cursor_, anchor_);
753 array().erase(p1, p2);
759 void MathCursor::SelPaste()
762 array().insert(cursor_, selarray);
763 cursor_ += selarray.size();
767 void MathCursor::SelHandle(bool sel)
769 if (sel && !selection)
771 if (!sel && selection)
776 void MathCursor::SelStart()
787 void MathCursor::SelClear()
794 void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
803 // Balance anchor and cursor
806 par()->GetXY(xo, yo);
807 int w = par()->width();
809 int x1 = xarray().xo() + xarray().pos2x(cursor_);
810 int y1 = xarray().yo();
811 //int a1 = xarray().ascent();
812 //int d1 = xarray().descent();
815 int x = xarray().xo() + xarray().pos2x(anchor_);
816 int y = xarray().yo();
817 int a = xarray().ascent();
818 int d = xarray().descent();
820 // single row selection
833 ypoint[n++] = y1 - a;
838 ypoint[n++] = y1 - a;
840 ypoint[n++] = y1 + d;
844 ypoint[n++] = y1 + d;
850 xpoint[n] = xpoint[0];
851 ypoint[n++] = ypoint[0];
853 //lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "]\n";
854 //lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "]\n";
855 //for (i = 0; i < np; ++i)
856 // lyxerr << "XY[" << xpoint[i] << " " << ypoint[i] << "]\n";
860 void MathCursor::handleFont(MathTextCodes t)
863 int const p1 = std::min(cursor_, anchor_);
864 int const p2 = std::max(cursor_, anchor_);
865 MathArray & ar = array();
866 for (int pos = p1; pos != p2; ar.next(pos))
867 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
868 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
873 lastcode = LM_TC_VAR;
880 void MathCursor::handleAccent(string const & name, int code)
882 MathDecorationInset * p = new MathDecorationInset(name, code);
885 p->cell(0) = selarray;
891 void MathCursor::handleDelim(int l, int r)
893 MathDelimInset * p = new MathDelimInset(l, r);
896 p->cell(0) = selarray;
903 void MathCursor::GetPos(int & x, int & y)
905 x = xarray().xo() + xarray().pos2x(cursor_);
910 MathTextCodes MathCursor::nextCode() const
912 return array().GetCode(cursor_);
916 MathTextCodes MathCursor::prevCode() const
918 return array().GetCode(cursor_ - 1);
922 MathInset * MathCursor::par() const
928 InsetFormulaBase const * MathCursor::formula()
934 int MathCursor::pos() const
940 bool MathCursor::InMacroMode() const
946 bool MathCursor::Selection() const
952 void MathCursor::clearLastCode()
954 lastcode = LM_TC_MIN;
958 void MathCursor::setLastCode(MathTextCodes t)
964 MathTextCodes MathCursor::getLastCode() const
970 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
972 if (par_->GetType() == t) {
973 //lyxerr << "enclosing par is current\n";
977 for (int i = path_.size() - 1; i >= 0; --i) {
978 lyxerr << "checking level " << i << "\n";
979 if (path_[i].par_->GetType() == t) {
981 return path_[i].par_;
987 void MathCursor::pullArg()
990 MathArray a = array();
994 array().erase(cursor_);
995 array().insert(cursor_, a);
999 MathStyles MathCursor::style() const
1001 return xarray().style();
1005 void MathCursor::normalize() const
1007 #ifdef WITH_WARNINGS
1008 #warning This is evil!
1010 MathCursor * it = const_cast<MathCursor *>(this);
1012 if (idx_ < 0 || idx_ > par_->nargs())
1013 lyxerr << "this should not really happen - 1\n";
1014 it->idx_ = max(idx_, 0);
1015 it->idx_ = min(idx_, par_->nargs());
1017 if (cursor_ < 0 || cursor_ > array().size())
1018 lyxerr << "this should not really happen - 2\n";
1019 it->cursor_ = max(cursor_, 0);
1020 it->cursor_ = min(cursor_, array().size());
1024 int MathCursor::col() const
1026 return par_->col(idx_);
1030 int MathCursor::row() const
1032 return par_->row(idx_);
1037 char MathIter::GetChar() const
1039 return array().GetChar(cursor_);
1043 string MathIter::readString()
1046 int code = nextCode();
1047 for ( ; OK() && nextCode() == code; Next())
1054 MathInset * MathCursor::prevInset() const
1058 if (!array().prev(c))
1060 return array().nextInset(c);
1064 MathInset * MathCursor::nextInset() const
1067 return array().nextInset(cursor_);
1071 MathUpDownInset * MathCursor::nearbyUpDownInset() const
1074 MathInset * p = array().prevInset(cursor_);
1075 if (p && p->isUpDownInset())
1076 return static_cast<MathUpDownInset *>(p);
1077 p = array().nextInset(cursor_);
1078 if (p && p->isUpDownInset())
1079 return static_cast<MathUpDownInset *>(p);
1084 MathArray & MathCursor::array() const
1086 static MathArray dummy;
1088 lyxerr << "############ par_ not valid\n";
1092 if (idx_ < 0 || idx_ >= par_->nargs()) {
1093 lyxerr << "############ idx_ " << idx_ << " not valid\n";
1097 return par_->cell(idx_);
1101 MathXArray & MathCursor::xarray() const
1103 return par_->xcell(idx_);
1108 bool MathCursor::nextIsInset() const
1110 return cursor_ < array().size() && MathIsInset(nextCode());
1114 bool MathCursor::prevIsInset() const
1116 return cursor_ > 0 && MathIsInset(prevCode());
1120 int MathCursor::xpos() const
1123 return xarray().pos2x(cursor_);
1126 void MathCursor::gotoX(int x)
1128 cursor_ = xarray().x2pos(x);
1131 void MathCursor::idxNext()
1133 par_->idxNext(idx_, cursor_);
1136 void MathCursor::idxPrev()
1138 par_->idxPrev(idx_, cursor_);
1141 void MathCursor::splitCell()
1143 if (idx_ == par_->nargs() - 1)
1145 MathArray ar = array();
1146 ar.erase(0, cursor_);
1147 array().erase(cursor_, array().size());
1150 array().insert(0, ar);
1153 void MathCursor::breakLine()
1155 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1156 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1157 p->mutate(LM_OT_EQNARRAY);
1165 const int r = row();
1166 for (int c = col() + 1; c < p->ncols(); ++c) {
1167 const int i1 = p->index(r, c);
1168 const int i2 = p->index(r + 1, c);
1169 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1170 p->cell(i1).swap(p->cell(i2));
1175 p->cell(idx_).swap(p->cell(idx_ + p->ncols() - 1));
1179 char MathCursor::valign() const
1183 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1184 return p ? p->valign() : 0;
1187 char MathCursor::halign() const
1191 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1192 return p ? p->halign(idx % p->ncols()) : 0;