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
29 #include "mathed/support.h"
30 #include "formulabase.h"
31 #include "math_cursor.h"
32 #include "math_arrayinset.h"
33 #include "math_bigopinset.h"
34 #include "math_decorationinset.h"
35 #include "math_deliminset.h"
36 #include "math_dotsinset.h"
37 #include "math_fracinset.h"
38 #include "math_funcinset.h"
39 #include "math_gridinset.h"
40 #include "math_macro.h"
41 #include "math_macroarg.h"
42 #include "math_macrotable.h"
43 #include "math_macrotemplate.h"
44 #include "math_matrixinset.h"
45 #include "math_rootinset.h"
46 #include "math_spaceinset.h"
47 #include "math_sqrtinset.h"
48 #include "support/lstrings.h"
49 #include "math_scriptinset.h"
50 #include "math_parser.h"
62 void grab(MathCursor const & cursor)
67 cursor.getSelection(i1, i2);
68 if (i1.idx_ == i2.idx_)
69 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
71 #ifdef RECTANGULAR_SELECT
72 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
73 for (unsigned i = 0; i < indices.size(); ++i)
74 data_.push_back(i1.cell(indices[i]));
76 data_.push_back(MathArray(i1.cell(), i1.pos_, i1.cell().size()));
77 for (int i = i1.idx_ + 1; i < i2.idx_; ++i)
78 data_.push_back(i1.cell(i));
79 data_.push_back(MathArray(i2.cell(), 0, i2.pos_));
84 void erase(MathCursor & cursor)
88 cursor.getSelection(i1, i2);
89 if (i1.idx_ == i2.idx_) {
90 i1.cell().erase(i1.pos_, i2.pos_);
92 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
93 for (unsigned i = 0; i < indices.size(); ++i)
94 i1.cell(indices[i]).erase();
99 void paste(MathCursor & cursor) const
101 cursor.insert(glue());
104 // glues selection to one cell
105 MathArray glue() const
108 for (unsigned i = 0; i < data_.size(); ++i)
109 ar.push_back(data_[i]);
118 std::vector<MathArray> data_;
122 Selection theSelection;
125 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
127 os << "(par: " << p.par_ << " idx: " << p.idx_
128 << " pos: " << p.pos_ << ")";
135 MathCursor::MathCursor(InsetFormulaBase * formula)
138 lastcode = LM_TC_MIN;
145 void MathCursor::push(MathInset * par, bool first)
150 par->idxFirst(p.idx_, p.pos_);
152 par->idxLast(p.idx_, p.pos_);
153 Cursor_.push_back(p);
157 bool MathCursor::pop()
159 if (Cursor_.size() <= 1)
166 MathInset * MathCursor::parInset(int i) const
168 return Cursor_[i].par_;
172 void MathCursor::dump(char const * what) const
176 lyxerr << "MC: " << what << "\n";
177 for (unsigned i = 0; i < Cursor_.size(); ++i)
178 lyxerr << " i: " << i
179 << " pos: " << Cursor_[i].pos_
180 << " idx: " << Cursor_[i].idx_
181 << " par: " << Cursor_[i].par_ << "\n";
183 //lyxerr << " sel: " << selection << " data: " << array() << "\n";
187 void MathCursor::seldump(char const *) const
189 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
193 //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
194 // << str << "\ntheSelection: " << theSelection;
195 //for (unsigned int i = 0; i < Cursor_.size(); ++i)
196 // lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
197 //lyxerr << "\ncursor.pos_: " << cursor().pos_;
198 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
199 //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
203 bool MathCursor::isInside(MathInset * p) const
205 for (unsigned i = 0; i < Cursor_.size(); ++i)
206 if (parInset(i) == p)
212 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
216 if (!(p->isActive() || (useupdown && p->isScriptInset())))
220 // we can't move into anything new during selection
221 if (Cursor_.size() == Anchor_.size())
223 if (p != Anchor_[Cursor_.size()].par_)
230 bool MathCursor::plainLeft()
232 return array().prev(cursor().pos_);
236 bool MathCursor::Left(bool sel)
240 // was MacroModeBack()
241 if (!imacro->name().empty()) {
242 imacro->SetName(imacro->name().substr(0, imacro->name().length()-1));
243 imacro->Metrics(imacro->size());
251 MathInset * p = prevInset();
252 if (openable(p, sel, false)) {
259 if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
267 bool MathCursor::plainRight()
269 return array().next(cursor().pos_);
273 bool MathCursor::Right(bool sel)
283 MathInset * p = nextInset();
284 if (openable(p, sel, false)) {
288 if (array().next(cursor().pos_))
290 if (cursor().par_->idxRight(cursor().idx_, cursor().pos_))
294 array().next(cursor().pos_);
299 void MathCursor::first()
302 push(formula_->par(), true);
306 void MathCursor::last()
309 push(formula_->par(), false);
313 void MathCursor::SetPos(int x, int y)
316 //lyxerr << "MathCursor::SetPos x: " << x << " y: " << y << "\n";
319 lastcode = LM_TC_MIN;
322 cursor().par_ = formula()->par();
327 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_ << "\n";
328 int distmin = 1 << 30; // large enough
329 for (int i = 0; i < cursor().par_->nargs(); ++i) {
330 MathXArray const & ar = cursor().par_->xcell(i);
331 int x1 = x - ar.xo();
332 int y1 = y - ar.yo();
333 int c = ar.x2pos(x1);
334 int xx = abs(x1 - ar.pos2x(c));
336 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
337 // << " c: " << c << " xo: " << ar.xo() << "\n";
338 if (yy + xx <= distmin) {
344 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
345 // << cursor().pos_ << "\n";
346 MathInset * n = nextInset();
347 MathInset * p = prevInset();
348 if (openable(n, selection, true) && n->covers(x, y))
350 else if (openable(p, selection, true) && p->covers(x, y)) {
360 void MathCursor::Home()
365 if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_))
371 void MathCursor::End()
376 if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
378 array().next(cursor().pos_);
384 void MathCursor::erase()
386 array().erase(cursor().pos_);
390 void MathCursor::insert(char c, MathTextCodes t)
392 //lyxerr << "inserting '" << c << "'\n";
399 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
403 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
404 // was MacroModeinsert(c);
405 imacro->SetName(imacro->name() + static_cast<char>(c));
410 array().insert(cursor().pos_, c, t);
411 array().next(cursor().pos_);
417 void MathCursor::insert(MathInset * p)
428 array().insert(cursor().pos_, p);
429 array().next(cursor().pos_);
433 void MathCursor::insert(MathArray const & ar)
439 array().insert(cursor().pos_, ar);
440 cursor().pos_ += ar.size();
444 void MathCursor::Delete()
455 // delete empty cells if necessary
456 if (cursor().pos_ == 0 && array().size() == 0) {
459 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
460 if (popit && pop() && removeit)
465 if (cursor().pos_ < array().size())
466 array().erase(cursor().pos_);
472 void MathCursor::DelLine()
481 if (cursor().par_->nrows() > 1)
482 cursor().par_->delRow(row());
486 bool MathCursor::Up(bool sel)
493 int x = xarray().pos2x(cursor().pos_);
494 if (cursor().idxDown()) {
495 cursor().pos_ = xarray().x2pos(x);
503 // check whether we could move into an inset on the right or on the left
504 MathInset * p = nextInset();
507 if (p->idxFirstUp(idx, pos)) {
519 if (p->idxLastUp(idx, pos)) {
529 int x = xarray().pos2x(cursor().pos_);
530 if (cursor().idxUp()) {
531 cursor().pos_ = xarray().x2pos(x);
540 bool MathCursor::Down(bool sel)
547 int x = xarray().pos2x(cursor().pos_);
548 if (cursor().idxDown()) {
549 cursor().pos_ = xarray().x2pos(x);
557 // check whether we could move into an inset on the right or on the left
558 MathInset * p = nextInset();
561 if (p->idxFirstDown(idx, pos)) {
573 if (p->idxLastDown(idx, pos)) {
583 int x = xarray().pos2x(cursor().pos_);
584 if (cursor().idxDown()) {
585 cursor().pos_ = xarray().x2pos(x);
594 bool MathCursor::toggleLimits()
598 MathInset * p = prevInset();
599 int old = p->limits();
600 p->limits(old < 0 ? 1 : -1);
601 return old != p->limits();
605 void MathCursor::SetSize(MathStyles size)
607 cursor().par_->UserSetSize(size);
612 void MathCursor::Interpret(string const & s)
614 //lyxerr << "Interpret: '" << s << "'\n";
615 //lyxerr << "in: " << in_word_set(s) << " \n";
617 if (s.size() && (s[0] == '^' || s[0] == '_')) {
618 bool const up = (s[0] == '^');
620 MathScriptInset * p = prevScriptInset();
622 MathInset * b = prevInset();
623 if (b && b->isScriptable()) {
624 p = new MathScriptInset(up, !up, b->clone());
628 p = new MathScriptInset(up, !up);
638 cursor().idx_ = up ? 0 : 1;
644 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
645 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
646 insert(new MathSpaceInset(sp));
651 latexkeys const * l = in_word_set(s);
655 p = new MathRootInset;
656 else if (MathMacroTable::hasTemplate(s))
657 p = new MathMacro(MathMacroTable::provideTemplate(s));
658 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
663 istringstream is(s.substr(7).c_str());
664 is >> m >> n >> v_align >> h_align;
668 MathArrayInset * pp = new MathArrayInset(m, n);
669 pp->valign(v_align[0]);
674 p = new MathFuncInset(s, LM_OT_UNDEF);
678 p = new MathBigopInset(s, l->id);
682 MathTextCodes code = static_cast<MathTextCodes>(l->id);
684 insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
686 p = new MathFuncInset(l->name);
691 p = new MathFracInset("stackrel");
695 p = new MathFracInset("frac");
699 p = new MathSqrtInset;
702 case LM_TK_DECORATION:
703 p = new MathDecorationInset(l->name, l->id);
707 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
711 p = new MathSpaceInset(l->id);
715 p = new MathDotsInset(l->name, l->id);
719 p = new MathMacro(MathMacroTable::provideTemplate(s));
723 p = new MathFuncInset(l->name);
729 bool oldsel = selection;
736 Right(); // do not push for e.g. MathBigopInset
740 p->Metrics(p->size());
745 void MathCursor::MacroModeOpen()
748 imacro = new MathFuncInset("");
752 lyxerr << "Math Warning: Already in macro mode" << endl;
756 void MathCursor::MacroModeClose()
759 string name = imacro->name();
770 void MathCursor::SelCopy()
774 theSelection.grab(*this);
780 void MathCursor::SelCut()
784 theSelection.grab(*this);
785 theSelection.erase(*this);
791 void MathCursor::SelDel()
795 theSelection.erase(*this);
801 void MathCursor::SelPaste()
804 theSelection.paste(*this);
809 void MathCursor::SelHandle(bool sel)
811 if (sel && !selection)
813 if (!sel && selection)
818 void MathCursor::SelStart()
829 void MathCursor::SelClear()
835 void MathCursor::drawSelection(Painter & pain) const
842 getSelection(i1, i2);
844 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
846 if (i1.idx_ == i2.idx_) {
847 MathXArray & c = i1.xcell();
848 int x1 = c.xo() + c.pos2x(i1.pos_);
849 int y1 = c.yo() - c.ascent();
850 int x2 = c.xo() + c.pos2x(i2.pos_);
851 int y2 = c.yo() + c.descent();
852 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
855 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
856 for (unsigned i = 0; i < indices.size(); ++i) {
857 MathXArray & c = i1.xcell(indices[i]);
859 int y1 = c.yo() - c.ascent();
860 int x2 = c.xo() + c.width();
861 int y2 = c.yo() + c.descent();
862 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
868 void MathCursor::handleFont(MathTextCodes t)
873 getSelection(i1, i2);
874 if (i1.idx_ == i2.idx_) {
875 MathArray & ar = i1.cell();
876 for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
877 if (!ar.isInset(pos) && isalnum(ar.GetChar(pos))) {
878 MathTextCodes c = ar.GetCode(pos) == t ? LM_TC_VAR : t;
883 lastcode = (lastcode == t) ? LM_TC_VAR : t;
888 void MathCursor::handleAccent(string const & name, int code)
890 MathDecorationInset * p = new MathDecorationInset(name, code);
893 p->cell(0) = theSelection.glue();
900 void MathCursor::handleDelim(int l, int r)
902 MathDelimInset * p = new MathDelimInset(l, r);
905 p->cell(0) = theSelection.glue();
913 void MathCursor::GetPos(int & x, int & y)
915 x = xarray().xo() + xarray().pos2x(cursor().pos_);
920 MathTextCodes MathCursor::nextCode() const
922 return array().GetCode(cursor().pos_);
926 MathTextCodes MathCursor::prevCode() const
928 return array().GetCode(cursor().pos_ - 1);
932 MathInset * MathCursor::par() const
934 return cursor().par_;
938 InsetFormulaBase const * MathCursor::formula()
944 int MathCursor::pos() const
946 return cursor().pos_;
950 bool MathCursor::InMacroMode() const
956 bool MathCursor::Selection() const
962 void MathCursor::clearLastCode()
964 lastcode = LM_TC_MIN;
968 void MathCursor::setLastCode(MathTextCodes t)
974 MathTextCodes MathCursor::getLastCode() const
980 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
982 for (int i = Cursor_.size() - 1; i >= 0; --i) {
983 //lyxerr << "checking level " << i << "\n";
984 if (Cursor_[i].par_->GetType() == t) {
985 idx = Cursor_[i].idx_;
986 return Cursor_[i].par_;
993 void MathCursor::pullArg(bool goright)
997 MathArray a = array();
999 array().erase(cursor().pos_);
1000 array().insert(cursor().pos_, a);
1002 cursor().pos_ += a.size();
1007 MathStyles MathCursor::style() const
1009 return xarray().style();
1013 void MathCursor::normalize() const
1015 #ifdef WITH_WARNINGS
1016 #warning This is evil!
1018 MathCursor * it = const_cast<MathCursor *>(this);
1020 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
1021 lyxerr << "this should not really happen - 1\n";
1022 it->cursor().idx_ = max(cursor().idx_, 0);
1023 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs());
1025 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1026 lyxerr << "this should not really happen - 2\n";
1027 it->cursor().pos_ = max(cursor().pos_, 0);
1028 it->cursor().pos_ = min(cursor().pos_, array().size());
1032 int MathCursor::col() const
1034 return par()->col(cursor().idx_);
1038 int MathCursor::row() const
1040 return par()->row(cursor().idx_);
1045 char MathCursorPos::GetChar() const
1047 return array().GetChar(cursor().pos_);
1051 string MathCursorPos::readString()
1054 int code = nextCode();
1055 for ( ; OK() && nextCode() == code; Next())
1063 MathInset * MathCursor::prevInset() const
1066 int c = cursor().pos_;
1067 if (!array().prev(c))
1069 return array().nextInset(c);
1073 MathInset * MathCursor::nextInset() const
1076 return array().nextInset(cursor().pos_);
1080 MathScriptInset * MathCursor::prevScriptInset() const
1083 MathInset * p = array().prevInset(cursor().pos_);
1084 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1088 MathSpaceInset * MathCursor::prevSpaceInset() const
1091 MathInset * p = array().prevInset(cursor().pos_);
1092 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1096 MathArray & MathCursor::array() const
1098 static MathArray dummy;
1099 if (!cursor().par_) {
1100 lyxerr << "############ par_ not valid\n";
1104 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1105 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1109 return cursor().cell();
1113 MathXArray & MathCursor::xarray() const
1115 return cursor().xcell();
1119 bool MathCursor::nextIsInset() const
1121 return cursor().pos_ < array().size() && MathIsInset(nextCode());
1125 bool MathCursor::prevIsInset() const
1127 return cursor().pos_ > 0 && MathIsInset(prevCode());
1131 int MathCursor::xpos() const
1134 return xarray().pos2x(cursor().pos_);
1138 void MathCursor::gotoX(int x)
1140 cursor().pos_ = xarray().x2pos(x);
1144 void MathCursor::idxNext()
1146 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1150 void MathCursor::idxPrev()
1152 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1156 void MathCursor::splitCell()
1158 if (cursor().idx_ == cursor().par_->nargs() - 1)
1160 MathArray ar = array();
1161 ar.erase(0, cursor().pos_);
1162 array().erase(cursor().pos_, array().size());
1165 array().insert(0, ar);
1169 void MathCursor::breakLine()
1171 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1172 if (p->GetType() == LM_OT_SIMPLE || p->GetType() == LM_OT_EQUATION) {
1173 p->mutate(LM_OT_EQNARRAY);
1175 cursor().idx_ = p->nrows();
1181 const int r = row();
1182 for (int c = col() + 1; c < p->ncols(); ++c) {
1183 const int i1 = p->index(r, c);
1184 const int i2 = p->index(r + 1, c);
1185 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1186 p->cell(i1).swap(p->cell(i2));
1191 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1196 char MathCursor::valign() const
1200 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1201 return p ? p->valign() : 0;
1205 char MathCursor::halign() const
1209 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1210 return p ? p->halign(idx % p->ncols()) : 0;
1214 MathCursorPos MathCursor::firstSelectionPos() const
1216 MathCursorPos anc = normalAnchor();
1217 return anc < cursor() ? anc : cursor();
1221 MathCursorPos MathCursor::lastSelectionPos() const
1223 MathCursorPos anc = normalAnchor();
1224 return anc < cursor() ? cursor() : anc;
1228 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1230 MathCursorPos anc = normalAnchor();
1231 if (anc < cursor()) {
1241 MathCursorPos & MathCursor::cursor()
1243 return Cursor_.back();
1247 MathCursorPos const & MathCursor::cursor() const
1249 return Cursor_.back();
1254 ////////////////////////////////////////////////////////////////////////
1257 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1259 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1263 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1265 if (ti.par_ != it.par_) {
1266 lyxerr << "can't compare cursor and anchor in different insets\n";
1269 if (ti.idx_ != it.idx_)
1270 return ti.idx_ < it.idx_;
1271 return ti.pos_ < it.pos_;
1275 MathArray & MathCursorPos::cell(int idx) const
1277 return par_->cell(idx);
1280 MathArray & MathCursorPos::cell() const
1282 return par_->cell(idx_);
1286 MathXArray & MathCursorPos::xcell(int idx) const
1288 return par_->xcell(idx);
1292 MathXArray & MathCursorPos::xcell() const
1294 return par_->xcell(idx_);
1298 MathCursorPos MathCursor::normalAnchor() const
1300 // use Anchor on the same level as Cursor
1301 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1302 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1303 // anchor is behind cursor -> move anchor behind the inset
1304 normal.cell().next(normal.pos_);
1306 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1307 // << normal << "\n";
1312 bool MathCursorPos::idxUp()
1314 return par_->idxUp(idx_, pos_);
1318 bool MathCursorPos::idxDown()
1320 return par_->idxDown(idx_, pos_);
1324 bool MathCursorPos::idxLeft()
1326 return par_->idxLeft(idx_, pos_);
1330 bool MathCursorPos::idxRight()
1332 return par_->idxRight(idx_, pos_);