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_symbolinset.h"
35 #include "math_decorationinset.h"
36 #include "math_deliminset.h"
37 #include "math_dotsinset.h"
38 #include "math_fracinset.h"
39 #include "math_funcinset.h"
40 #include "math_funcliminset.h"
41 #include "math_gridinset.h"
42 #include "math_macro.h"
43 #include "math_macroarg.h"
44 #include "math_macrotable.h"
45 #include "math_macrotemplate.h"
46 #include "math_matrixinset.h"
47 #include "math_rootinset.h"
48 #include "math_spaceinset.h"
49 #include "math_sqrtinset.h"
50 #include "support/lstrings.h"
51 #include "math_scriptinset.h"
52 #include "math_parser.h"
64 void grab(MathCursor const & cursor)
69 cursor.getSelection(i1, i2);
70 if (i1.idx_ == i2.idx_)
71 data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
73 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
74 for (unsigned i = 0; i < indices.size(); ++i)
75 data_.push_back(i1.cell(indices[i]));
79 void erase(MathCursor & cursor)
83 cursor.getSelection(i1, i2);
84 if (i1.idx_ == i2.idx_) {
85 i1.cell().erase(i1.pos_, i2.pos_);
87 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
88 for (unsigned i = 0; i < indices.size(); ++i)
89 i1.cell(indices[i]).erase();
94 void paste(MathCursor & cursor) const
96 cursor.insert(glue());
99 // glues selection to one cell
100 MathArray glue() const
103 for (unsigned i = 0; i < data_.size(); ++i)
104 ar.push_back(data_[i]);
113 std::vector<MathArray> data_;
117 Selection theSelection;
120 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
122 os << "(par: " << p.par_ << " idx: " << p.idx_
123 << " pos: " << p.pos_ << ")";
130 MathCursor::MathCursor(InsetFormulaBase * formula)
131 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
137 MathCursor::~MathCursor()
142 void MathCursor::push(MathInset * par, bool first)
147 par->idxFirst(p.idx_, p.pos_);
149 par->idxLast(p.idx_, p.pos_);
150 Cursor_.push_back(p);
154 bool MathCursor::pop()
156 if (Cursor_.size() <= 1)
163 MathInset * MathCursor::parInset(int i) const
165 return Cursor_[i].par_;
169 void MathCursor::dump(char const * what) const
173 lyxerr << "MC: " << what << "\n";
174 for (unsigned i = 0; i < Cursor_.size(); ++i)
175 lyxerr << " i: " << i
176 << " pos: " << Cursor_[i].pos_
177 << " idx: " << Cursor_[i].idx_
178 << " par: " << Cursor_[i].par_ << "\n";
180 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
184 void MathCursor::seldump(char const * str) const
186 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
190 lyxerr << "\n\n\n=================vvvvvvvvvvvvv======================= "
191 << str << "\ntheSelection: " << selection_
192 << " '" << theSelection.glue() << "'\n";
193 for (unsigned int i = 0; i < Cursor_.size(); ++i)
194 lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
196 for (unsigned int i = 0; i < Anchor_.size(); ++i)
197 lyxerr << Anchor_[i].par_ << "\n'" << Anchor_[i].cell() << "'\n";
198 //lyxerr << "\ncursor.pos_: " << pos();
199 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
200 lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
204 bool MathCursor::isInside(MathInset const * p) const
206 for (unsigned i = 0; i < Cursor_.size(); ++i)
207 if (parInset(i) == p)
213 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
218 if (!(p->isActive() || (useupdown && p->isScriptInset())))
222 // we can't move into anything new during selection
223 if (Cursor_.size() == Anchor_.size())
225 if (p != Anchor_[Cursor_.size()].par_)
232 void MathCursor::plainLeft()
238 void MathCursor::plainRight()
244 bool MathCursor::left(bool sel)
248 // was MacroModeBack()
249 if (!imacro_->name().empty()) {
250 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
251 imacro_->metrics(imacro_->size());
259 MathInset * p = prevInset();
260 if (openable(p, sel, false)) {
269 if (par()->idxLeft(idx(), pos()))
277 bool MathCursor::right(bool sel)
287 MathInset * p = nextInset();
288 if (openable(p, sel, false)) {
292 if (pos() != array().size()) {
296 if (par()->idxRight(idx(), pos())) {
307 void MathCursor::first()
310 push(outerPar(), true);
314 void MathCursor::last()
317 push(outerPar(), false);
321 void MathCursor::setPos(int x, int y)
324 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
327 lastcode_ = LM_TC_MIN;
330 cursor().par_ = outerPar();
335 //lyxerr << "found idx: " << idx_ << " cursor: " << pos() << "\n";
336 int distmin = 1 << 30; // large enough
337 for (int i = 0; i < par()->nargs(); ++i) {
338 MathXArray const & ar = par()->xcell(i);
339 int x1 = x - ar.xo();
340 int y1 = y - ar.yo();
341 int c = ar.x2pos(x1);
342 int xx = abs(x1 - ar.pos2x(c));
344 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
345 // << " c: " << c << " xo: " << ar.xo() << "\n";
346 if (yy + xx <= distmin) {
352 //lyxerr << "found idx: " << idx() << " cursor: "
354 MathInset * n = nextInset();
355 MathInset * p = prevInset();
356 if (openable(n, selection_, true) && n->covers(x, y))
358 else if (openable(p, selection_, true) && p->covers(x, y)) {
368 void MathCursor::home()
373 if (!par()->idxHome(idx(), pos()))
379 void MathCursor::end()
384 if (!par()->idxEnd(idx(), pos())) {
392 void MathCursor::plainErase()
394 array().erase(pos());
398 void MathCursor::insert(char c, MathTextCodes t)
400 //lyxerr << "inserting '" << c << "'\n";
407 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
411 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
412 // was MacroModeinsert(c);
413 imacro_->setName(imacro_->name() + c);
418 array().insert(pos(), c, t);
423 void MathCursor::insert(MathInset * p)
434 array().insert(pos(), p);
439 void MathCursor::insert(MathArray const & ar)
445 array().insert(pos(), ar);
450 void MathCursor::backspace()
463 if (array().size()) {
472 void MathCursor::erase()
483 // delete empty cells if necessary
484 if (pos() == 0 && array().empty()) {
487 par()->idxDelete(idx(), popit, removeit);
488 if (popit && pop() && removeit)
493 if (pos() < array().size())
500 void MathCursor::delLine()
509 if (par()->nrows() > 1)
510 par()->delRow(row());
514 bool MathCursor::up(bool sel)
521 int x = xarray().pos2x(pos());
522 if (cursor().idxDown()) {
523 pos() = xarray().x2pos(x);
531 // check whether we could move into an inset on the right or on the left
532 MathInset * p = nextInset();
535 if (p->idxFirstUp(idxx, poss)) {
547 if (p->idxLastUp(idxx, poss)) {
557 int x = xarray().pos2x(pos());
558 if (cursor().idxUp()) {
559 pos() = xarray().x2pos(x);
568 bool MathCursor::down(bool sel)
575 int x = xarray().pos2x(pos());
576 if (cursor().idxDown()) {
577 pos() = xarray().x2pos(x);
585 // check whether we could move into an inset on the right or on the left
586 MathInset * p = nextInset();
590 if (p->idxFirstDown(idxx, poss)) {
603 if (p->idxLastDown(idxx, poss)) {
613 int x = xarray().pos2x(pos());
614 if (cursor().idxDown()) {
615 pos() = xarray().x2pos(x);
624 bool MathCursor::toggleLimits()
626 MathScriptInset * p = prevScriptInset();
629 int old = p->limits();
630 p->limits(old < 0 ? 1 : -1);
631 return old != p->limits();
635 void MathCursor::setSize(MathStyles size)
637 par()->userSetSize(size);
642 void MathCursor::interpret(string const & s)
644 //lyxerr << "interpret: '" << s << "'\n";
645 //lyxerr << "in: " << in_word_set(s) << " \n";
650 if (s[0] == '^' || s[0] == '_') {
651 bool const up = (s[0] == '^');
653 MathScriptInset * p = prevScriptInset();
655 MathInset * b = prevInset();
656 if (b && b->isScriptable()) {
657 p = new MathScriptInset(up, !up, b->clone());
661 p = new MathScriptInset(up, !up);
677 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
678 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
679 insert(new MathSpaceInset(sp));
684 latexkeys const * l = in_word_set(s);
688 p = new MathRootInset;
689 else if (MathMacroTable::hasTemplate(s))
690 p = new MathMacro(MathMacroTable::provideTemplate(s));
691 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
696 istringstream is(s.substr(7).c_str());
697 is >> m >> n >> v_align >> h_align;
701 MathArrayInset * pp = new MathArrayInset(m, n);
702 pp->valign(v_align[0]);
707 p = new MathFuncInset(s);
711 p = new MathBigopInset(l);
715 p = new MathFuncLimInset(l);
719 p = new MathSymbolInset(l);
723 p = new MathFracInset("stackrel");
727 p = new MathFracInset("frac");
731 p = new MathSqrtInset;
734 case LM_TK_DECORATION:
735 p = new MathDecorationInset(l);
739 p = new MathSpaceInset(l->id);
743 p = new MathDotsInset(l);
747 p = new MathMacro(MathMacroTable::provideTemplate(s));
751 p = new MathFuncInset(l->name);
761 right(); // do not push for e.g. MathSymbolInset
764 p->metrics(p->size());
769 void MathCursor::macroModeOpen()
772 imacro_ = new MathFuncInset("");
773 array().insert(pos(), imacro_);
777 lyxerr << "Math Warning: Already in macro mode" << endl;
781 void MathCursor::macroModeClose()
784 string name = imacro_->name();
793 void MathCursor::selCopy()
797 theSelection.grab(*this);
803 void MathCursor::selCut()
807 theSelection.grab(*this);
808 theSelection.erase(*this);
811 theSelection.clear();
816 void MathCursor::selDel()
820 theSelection.erase(*this);
826 void MathCursor::selPaste()
829 theSelection.paste(*this);
834 void MathCursor::selHandle(bool sel)
836 if (sel == selection_)
839 theSelection.clear();
845 void MathCursor::selStart()
851 theSelection.clear();
857 void MathCursor::selClear()
864 void MathCursor::drawSelection(Painter & pain) const
871 getSelection(i1, i2);
873 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
875 if (i1.idx_ == i2.idx_) {
876 MathXArray & c = i1.xcell();
877 int x1 = c.xo() + c.pos2x(i1.pos_);
878 int y1 = c.yo() - c.ascent();
879 int x2 = c.xo() + c.pos2x(i2.pos_);
880 int y2 = c.yo() + c.descent();
881 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
884 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
885 for (unsigned i = 0; i < indices.size(); ++i) {
886 MathXArray & c = i1.xcell(indices[i]);
888 int y1 = c.yo() - c.ascent();
889 int x2 = c.xo() + c.width();
890 int y2 = c.yo() + c.descent();
891 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
897 void MathCursor::handleFont(MathTextCodes t)
902 getSelection(i1, i2);
903 if (i1.idx_ == i2.idx_) {
904 MathArray & ar = i1.cell();
905 for (int pos = i1.pos_; pos != i2.pos_; ++pos)
906 if (isalnum(ar.getChar(pos))) {
907 MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
912 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
916 void MathCursor::handleAccent(string const & name)
918 latexkeys const * l = in_word_set(name);
922 MathDecorationInset * p = new MathDecorationInset(l);
925 p->cell(0) = theSelection.glue();
932 void MathCursor::handleDelim(int l, int r)
934 MathDelimInset * p = new MathDelimInset(l, r);
937 p->cell(0) = theSelection.glue();
945 void MathCursor::getPos(int & x, int & y)
947 x = xarray().xo() + xarray().pos2x(pos());
952 MathTextCodes MathCursor::nextCode() const
954 return array().getCode(pos());
958 MathTextCodes MathCursor::prevCode() const
960 return array().getCode(pos() - 1);
964 MathInset * MathCursor::par() const
966 return cursor().par_;
970 InsetFormulaBase const * MathCursor::formula()
976 int MathCursor::idx() const
978 return cursor().idx_;
982 int & MathCursor::idx()
984 return cursor().idx_;
988 int MathCursor::pos() const
990 return cursor().pos_;
994 int & MathCursor::pos()
996 return cursor().pos_;
1000 bool MathCursor::inMacroMode() const
1006 bool MathCursor::selection() const
1012 void MathCursor::clearLastCode()
1014 lastcode_ = LM_TC_MIN;
1018 void MathCursor::setLastCode(MathTextCodes t)
1024 MathTextCodes MathCursor::getLastCode() const
1030 MathArrayInset * MathCursor::enclosingArray(int & idx) const
1032 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1033 if (Cursor_[i].par_->isArray()) {
1034 idx = Cursor_[i].idx_;
1035 return static_cast<MathArrayInset *>(Cursor_[i].par_);
1042 void MathCursor::pullArg(bool goright)
1046 MathArray a = array();
1049 array().insert(pos(), a);
1056 MathStyles MathCursor::style() const
1058 return xarray().style();
1062 void MathCursor::normalize() const
1064 #ifdef WITH_WARNINGS
1065 #warning This is evil!
1067 MathCursor * it = const_cast<MathCursor *>(this);
1069 if (idx() < 0 || idx() > par()->nargs() - 1)
1070 lyxerr << "this should not really happen - 1\n";
1071 it->idx() = max(idx(), 0);
1072 it->idx() = min(idx(), par()->nargs() - 1);
1074 if (pos() < 0 || pos() > array().size())
1075 lyxerr << "this should not really happen - 2\n";
1076 it->pos() = max(pos(), 0);
1077 it->pos() = min(pos(), array().size());
1081 int MathCursor::col() const
1083 return par()->col(idx());
1087 int MathCursor::row() const
1089 return par()->row(idx());
1094 char MathCursorPos::getChar() const
1096 return array().getChar(pos());
1100 string MathCursorPos::readString()
1103 int code = nextCode();
1104 for ( ; OK() && nextCode() == code; Next())
1112 MathInset * MathCursor::prevInset() const
1117 return array().nextInset(pos() - 1);
1121 MathInset * MathCursor::nextInset() const
1124 return array().nextInset(pos());
1128 MathScriptInset * MathCursor::prevScriptInset() const
1131 MathInset * p = prevInset();
1132 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1136 MathSpaceInset * MathCursor::prevSpaceInset() const
1139 MathInset * p = prevInset();
1140 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1144 MathArray & MathCursor::array() const
1146 static MathArray dummy;
1148 lyxerr << "############ par_ not valid\n";
1152 if (idx() < 0 || idx() >= par()->nargs()) {
1153 lyxerr << "############ idx_ " << idx() << " not valid\n";
1157 return cursor().cell();
1161 MathXArray & MathCursor::xarray() const
1163 return cursor().xcell();
1167 int MathCursor::xpos() const
1170 return xarray().pos2x(pos());
1174 void MathCursor::gotoX(int x)
1176 pos() = xarray().x2pos(x);
1180 void MathCursor::idxNext()
1182 par()->idxNext(idx(), pos());
1186 void MathCursor::idxPrev()
1188 par()->idxPrev(idx(), pos());
1192 void MathCursor::splitCell()
1194 if (idx() == par()->nargs() - 1)
1196 MathArray ar = array();
1198 array().erase(pos(), array().size());
1201 array().insert(0, ar);
1205 void MathCursor::breakLine()
1207 MathMatrixInset * p = outerPar();
1208 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1209 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 p->cell(idx()).swap(p->cell(idx() + p->ncols() - 1));
1232 char MathCursor::valign() const
1235 MathArrayInset * p = enclosingArray(idx);
1236 return p ? p->valign() : 0;
1240 char MathCursor::halign() const
1243 MathArrayInset * p = enclosingArray(idx);
1244 return p ? p->halign(idx % p->ncols()) : 0;
1248 MathCursorPos MathCursor::firstSelectionPos() const
1250 MathCursorPos anc = normalAnchor();
1251 return anc < cursor() ? anc : cursor();
1255 MathCursorPos MathCursor::lastSelectionPos() const
1257 MathCursorPos anc = normalAnchor();
1258 return anc < cursor() ? cursor() : anc;
1262 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1264 MathCursorPos anc = normalAnchor();
1265 if (anc < cursor()) {
1275 MathCursorPos & MathCursor::cursor()
1277 return Cursor_.back();
1281 MathCursorPos const & MathCursor::cursor() const
1283 return Cursor_.back();
1288 ////////////////////////////////////////////////////////////////////////
1291 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1293 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1297 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1299 if (ti.par_ != it.par_) {
1300 lyxerr << "can't compare cursor and anchor in different insets\n";
1303 if (ti.idx_ != it.idx_)
1304 return ti.idx_ < it.idx_;
1305 return ti.pos_ < it.pos_;
1309 MathArray & MathCursorPos::cell(int idx) const
1311 return par_->cell(idx);
1314 MathArray & MathCursorPos::cell() const
1316 return par_->cell(idx_);
1320 MathXArray & MathCursorPos::xcell(int idx) const
1322 return par_->xcell(idx);
1326 MathXArray & MathCursorPos::xcell() const
1328 return par_->xcell(idx_);
1332 MathCursorPos MathCursor::normalAnchor() const
1334 // use Anchor on the same level as Cursor
1335 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1336 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1337 // anchor is behind cursor -> move anchor behind the inset
1340 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1341 // << normal << "\n";
1346 bool MathCursorPos::idxUp()
1348 return par_->idxUp(idx_, pos_);
1352 bool MathCursorPos::idxDown()
1354 return par_->idxDown(idx_, pos_);
1358 bool MathCursorPos::idxLeft()
1360 return par_->idxLeft(idx_, pos_);
1364 bool MathCursorPos::idxRight()
1366 return par_->idxRight(idx_, pos_);
1370 MathMatrixInset * MathCursor::outerPar() const
1373 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));