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 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
72 for (unsigned i = 0; i < indices.size(); ++i)
73 data_.push_back(i1.cell(indices[i]));
77 void erase(MathCursor & cursor)
81 cursor.getSelection(i1, i2);
82 if (i1.idx_ == i2.idx_) {
83 i1.cell().erase(i1.pos_, i2.pos_);
85 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
86 for (unsigned i = 0; i < indices.size(); ++i)
87 i1.cell(indices[i]).erase();
92 void paste(MathCursor & cursor) const
94 cursor.insert(glue());
97 // glues selection to one cell
98 MathArray glue() const
101 for (unsigned i = 0; i < data_.size(); ++i)
102 ar.push_back(data_[i]);
111 std::vector<MathArray> data_;
115 Selection theSelection;
118 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
120 os << "(par: " << p.par_ << " idx: " << p.idx_
121 << " pos: " << p.pos_ << ")";
128 MathCursor::MathCursor(InsetFormulaBase * formula)
129 : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
135 void MathCursor::push(MathInset * par, bool first)
140 par->idxFirst(p.idx_, p.pos_);
142 par->idxLast(p.idx_, p.pos_);
143 Cursor_.push_back(p);
147 bool MathCursor::pop()
149 if (Cursor_.size() <= 1)
156 MathInset * MathCursor::parInset(int i) const
158 return Cursor_[i].par_;
162 void MathCursor::dump(char const * what) const
166 lyxerr << "MC: " << what << "\n";
167 for (unsigned i = 0; i < Cursor_.size(); ++i)
168 lyxerr << " i: " << i
169 << " pos: " << Cursor_[i].pos_
170 << " idx: " << Cursor_[i].idx_
171 << " par: " << Cursor_[i].par_ << "\n";
173 //lyxerr << " sel: " << selection_ << " data: " << array() << "\n";
177 void MathCursor::seldump(char const *) const
179 //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
183 //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv======================= "
184 // << str << "\ntheSelection: " << theSelection;
185 //for (unsigned int i = 0; i < Cursor_.size(); ++i)
186 // lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
187 //lyxerr << "\ncursor.pos_: " << cursor().pos_;
188 //lyxerr << "\nanchor.pos_: " << anchor().pos_;
189 //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
193 bool MathCursor::isInside(MathInset * p) const
195 for (unsigned i = 0; i < Cursor_.size(); ++i)
196 if (parInset(i) == p)
202 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
206 if (!(p->isActive() || (useupdown && p->isScriptInset())))
210 // we can't move into anything new during selection
211 if (Cursor_.size() == Anchor_.size())
213 if (p != Anchor_[Cursor_.size()].par_)
220 bool MathCursor::plainLeft()
222 return array().prev(cursor().pos_);
226 bool MathCursor::left(bool sel)
230 // was MacroModeBack()
231 if (!imacro_->name().empty()) {
232 imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
233 imacro_->metrics(imacro_->size());
241 MathInset * p = prevInset();
242 if (openable(p, sel, false)) {
249 if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
257 bool MathCursor::plainRight()
259 return array().next(cursor().pos_);
263 bool MathCursor::right(bool sel)
273 MathInset * p = nextInset();
274 if (openable(p, sel, false)) {
278 if (array().next(cursor().pos_))
280 if (cursor().par_->idxRight(cursor().idx_, cursor().pos_))
284 array().next(cursor().pos_);
289 void MathCursor::first()
292 push(formula_->par(), true);
296 void MathCursor::last()
299 push(formula_->par(), false);
303 void MathCursor::setPos(int x, int y)
306 //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
309 lastcode_ = LM_TC_MIN;
312 cursor().par_ = formula()->par();
317 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_ << "\n";
318 int distmin = 1 << 30; // large enough
319 for (int i = 0; i < cursor().par_->nargs(); ++i) {
320 MathXArray const & ar = cursor().par_->xcell(i);
321 int x1 = x - ar.xo();
322 int y1 = y - ar.yo();
323 int c = ar.x2pos(x1);
324 int xx = abs(x1 - ar.pos2x(c));
326 //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
327 // << " c: " << c << " xo: " << ar.xo() << "\n";
328 if (yy + xx <= distmin) {
334 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
335 // << cursor().pos_ << "\n";
336 MathInset * n = nextInset();
337 MathInset * p = prevInset();
338 if (openable(n, selection_, true) && n->covers(x, y))
340 else if (openable(p, selection_, true) && p->covers(x, y)) {
350 void MathCursor::home()
355 if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_))
361 void MathCursor::end()
366 if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
368 array().next(cursor().pos_);
374 void MathCursor::plainErase()
376 array().erase(cursor().pos_);
380 void MathCursor::insert(char c, MathTextCodes t)
382 //lyxerr << "inserting '" << c << "'\n";
389 if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
393 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
394 // was MacroModeinsert(c);
395 imacro_->setName(imacro_->name() + c);
400 array().insert(cursor().pos_, c, t);
401 array().next(cursor().pos_);
405 void MathCursor::insert(MathInset * p)
416 array().insert(cursor().pos_, p);
417 array().next(cursor().pos_);
421 void MathCursor::insert(MathArray const & ar)
427 array().insert(cursor().pos_, ar);
428 cursor().pos_ += ar.size();
432 void MathCursor::erase()
443 // delete empty cells if necessary
444 if (cursor().pos_ == 0 && array().size() == 0) {
447 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
448 if (popit && pop() && removeit)
453 if (cursor().pos_ < array().size())
460 void MathCursor::delLine()
469 if (cursor().par_->nrows() > 1)
470 cursor().par_->delRow(row());
474 bool MathCursor::up(bool sel)
481 int x = xarray().pos2x(cursor().pos_);
482 if (cursor().idxDown()) {
483 cursor().pos_ = xarray().x2pos(x);
491 // check whether we could move into an inset on the right or on the left
492 MathInset * p = nextInset();
495 if (p->idxFirstUp(idx, pos)) {
507 if (p->idxLastUp(idx, pos)) {
517 int x = xarray().pos2x(cursor().pos_);
518 if (cursor().idxUp()) {
519 cursor().pos_ = xarray().x2pos(x);
528 bool MathCursor::down(bool sel)
535 int x = xarray().pos2x(cursor().pos_);
536 if (cursor().idxDown()) {
537 cursor().pos_ = xarray().x2pos(x);
545 // check whether we could move into an inset on the right or on the left
546 MathInset * p = nextInset();
549 if (p->idxFirstDown(idx, pos)) {
561 if (p->idxLastDown(idx, pos)) {
571 int x = xarray().pos2x(cursor().pos_);
572 if (cursor().idxDown()) {
573 cursor().pos_ = xarray().x2pos(x);
582 bool MathCursor::toggleLimits()
586 MathInset * p = prevInset();
587 int old = p->limits();
588 p->limits(old < 0 ? 1 : -1);
589 return old != p->limits();
593 void MathCursor::setSize(MathStyles size)
595 cursor().par_->userSetSize(size);
600 void MathCursor::interpret(string const & s)
602 //lyxerr << "interpret: '" << s << "'\n";
603 //lyxerr << "in: " << in_word_set(s) << " \n";
608 if (s[0] == '^' || s[0] == '_') {
609 bool const up = (s[0] == '^');
611 MathScriptInset * p = prevScriptInset();
613 MathInset * b = prevInset();
614 if (b && b->isScriptable()) {
615 p = new MathScriptInset(up, !up, b->clone());
619 p = new MathScriptInset(up, !up);
629 cursor().idx_ = up ? 0 : 1;
635 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
636 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
637 insert(new MathSpaceInset(sp));
642 latexkeys const * l = in_word_set(s);
646 p = new MathRootInset;
647 else if (MathMacroTable::hasTemplate(s))
648 p = new MathMacro(MathMacroTable::provideTemplate(s));
649 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
654 istringstream is(s.substr(7).c_str());
655 is >> m >> n >> v_align >> h_align;
659 MathArrayInset * pp = new MathArrayInset(m, n);
660 pp->valign(v_align[0]);
665 p = new MathFuncInset(s);
671 p = new MathBigopInset(l);
675 p = new MathFracInset("stackrel");
679 p = new MathFracInset("frac");
683 p = new MathSqrtInset;
686 case LM_TK_DECORATION:
687 p = new MathDecorationInset(l);
691 p = new MathSpaceInset(l->id);
695 p = new MathDotsInset(l);
699 p = new MathMacro(MathMacroTable::provideTemplate(s));
703 p = new MathFuncInset(l->name);
709 bool oldsel = selection_;
715 right(); // do not push for e.g. MathBigopInset
719 p->metrics(p->size());
724 void MathCursor::macroModeOpen()
727 imacro_ = new MathFuncInset("");
728 array().insert(cursor().pos_, imacro_);
729 array().next(cursor().pos_);
732 lyxerr << "Math Warning: Already in macro mode" << endl;
736 void MathCursor::macroModeClose()
739 string name = imacro_->name();
748 void MathCursor::selCopy()
752 theSelection.grab(*this);
758 void MathCursor::selCut()
762 theSelection.grab(*this);
763 theSelection.erase(*this);
769 void MathCursor::selDel()
773 theSelection.erase(*this);
779 void MathCursor::selPaste()
782 theSelection.paste(*this);
787 void MathCursor::selHandle(bool sel)
789 if (sel && !selection_)
791 if (!sel && selection_)
796 void MathCursor::selStart()
807 void MathCursor::selClear()
813 void MathCursor::drawSelection(Painter & pain) const
820 getSelection(i1, i2);
822 //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
824 if (i1.idx_ == i2.idx_) {
825 MathXArray & c = i1.xcell();
826 int x1 = c.xo() + c.pos2x(i1.pos_);
827 int y1 = c.yo() - c.ascent();
828 int x2 = c.xo() + c.pos2x(i2.pos_);
829 int y2 = c.yo() + c.descent();
830 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
833 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
834 for (unsigned i = 0; i < indices.size(); ++i) {
835 MathXArray & c = i1.xcell(indices[i]);
837 int y1 = c.yo() - c.ascent();
838 int x2 = c.xo() + c.width();
839 int y2 = c.yo() + c.descent();
840 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
846 void MathCursor::handleFont(MathTextCodes t)
851 getSelection(i1, i2);
852 if (i1.idx_ == i2.idx_) {
853 MathArray & ar = i1.cell();
854 for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
855 if (!ar.isInset(pos) && isalnum(ar.getChar(pos))) {
856 MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
861 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
865 void MathCursor::handleAccent(string const & name)
867 latexkeys const * l = in_word_set(name);
871 MathDecorationInset * p = new MathDecorationInset(l);
874 p->cell(0) = theSelection.glue();
881 void MathCursor::handleDelim(int l, int r)
883 MathDelimInset * p = new MathDelimInset(l, r);
886 p->cell(0) = theSelection.glue();
894 void MathCursor::getPos(int & x, int & y)
896 x = xarray().xo() + xarray().pos2x(cursor().pos_);
901 MathTextCodes MathCursor::nextCode() const
903 return array().getCode(cursor().pos_);
907 MathTextCodes MathCursor::prevCode() const
909 return array().getCode(cursor().pos_ - 1);
913 MathInset * MathCursor::par() const
915 return cursor().par_;
919 InsetFormulaBase const * MathCursor::formula()
925 int MathCursor::pos() const
927 return cursor().pos_;
931 bool MathCursor::inMacroMode() const
937 bool MathCursor::selection() const
943 void MathCursor::clearLastCode()
945 lastcode_ = LM_TC_MIN;
949 void MathCursor::setLastCode(MathTextCodes t)
955 MathTextCodes MathCursor::getLastCode() const
961 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
963 for (int i = Cursor_.size() - 1; i >= 0; --i) {
964 //lyxerr << "checking level " << i << "\n";
965 if (Cursor_[i].par_->getType() == t) {
966 idx = Cursor_[i].idx_;
967 return Cursor_[i].par_;
974 void MathCursor::pullArg(bool goright)
978 MathArray a = array();
981 array().insert(cursor().pos_, a);
983 cursor().pos_ += a.size();
988 MathStyles MathCursor::style() const
990 return xarray().style();
994 void MathCursor::normalize() const
997 #warning This is evil!
999 MathCursor * it = const_cast<MathCursor *>(this);
1001 if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
1002 lyxerr << "this should not really happen - 1\n";
1003 it->cursor().idx_ = max(cursor().idx_, 0);
1004 it->cursor().idx_ = min(cursor().idx_, cursor().par_->nargs());
1006 if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1007 lyxerr << "this should not really happen - 2\n";
1008 it->cursor().pos_ = max(cursor().pos_, 0);
1009 it->cursor().pos_ = min(cursor().pos_, array().size());
1013 int MathCursor::col() const
1015 return par()->col(cursor().idx_);
1019 int MathCursor::row() const
1021 return par()->row(cursor().idx_);
1026 char MathCursorPos::getChar() const
1028 return array().getChar(cursor().pos_);
1032 string MathCursorPos::readString()
1035 int code = nextCode();
1036 for ( ; OK() && nextCode() == code; Next())
1044 MathInset * MathCursor::prevInset() const
1047 int c = cursor().pos_;
1048 if (!array().prev(c))
1050 return array().nextInset(c);
1054 MathInset * MathCursor::nextInset() const
1057 return array().nextInset(cursor().pos_);
1061 MathScriptInset * MathCursor::prevScriptInset() const
1064 MathInset * p = array().prevInset(cursor().pos_);
1065 return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1069 MathSpaceInset * MathCursor::prevSpaceInset() const
1072 MathInset * p = array().prevInset(cursor().pos_);
1073 return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1077 MathArray & MathCursor::array() const
1079 static MathArray dummy;
1080 if (!cursor().par_) {
1081 lyxerr << "############ par_ not valid\n";
1085 if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1086 lyxerr << "############ idx_ " << cursor().idx_ << " not valid\n";
1090 return cursor().cell();
1094 MathXArray & MathCursor::xarray() const
1096 return cursor().xcell();
1100 bool MathCursor::nextIsInset() const
1102 return cursor().pos_ < array().size() && MathIsInset(nextCode());
1106 bool MathCursor::prevIsInset() const
1108 return cursor().pos_ > 0 && MathIsInset(prevCode());
1112 int MathCursor::xpos() const
1115 return xarray().pos2x(cursor().pos_);
1119 void MathCursor::gotoX(int x)
1121 cursor().pos_ = xarray().x2pos(x);
1125 void MathCursor::idxNext()
1127 cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1131 void MathCursor::idxPrev()
1133 cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1137 void MathCursor::splitCell()
1139 if (cursor().idx_ == cursor().par_->nargs() - 1)
1141 MathArray ar = array();
1142 ar.erase(0, cursor().pos_);
1143 array().erase(cursor().pos_, array().size());
1146 array().insert(0, ar);
1150 void MathCursor::breakLine()
1152 MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1153 if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1154 p->mutate(LM_OT_EQNARRAY);
1156 cursor().idx_ = p->nrows();
1162 const int r = row();
1163 for (int c = col() + 1; c < p->ncols(); ++c) {
1164 const int i1 = p->index(r, c);
1165 const int i2 = p->index(r + 1, c);
1166 lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1167 p->cell(i1).swap(p->cell(i2));
1172 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1177 char MathCursor::valign() const
1181 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1182 return p ? p->valign() : 0;
1186 char MathCursor::halign() const
1190 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1191 return p ? p->halign(idx % p->ncols()) : 0;
1195 MathCursorPos MathCursor::firstSelectionPos() const
1197 MathCursorPos anc = normalAnchor();
1198 return anc < cursor() ? anc : cursor();
1202 MathCursorPos MathCursor::lastSelectionPos() const
1204 MathCursorPos anc = normalAnchor();
1205 return anc < cursor() ? cursor() : anc;
1209 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1211 MathCursorPos anc = normalAnchor();
1212 if (anc < cursor()) {
1222 MathCursorPos & MathCursor::cursor()
1224 return Cursor_.back();
1228 MathCursorPos const & MathCursor::cursor() const
1230 return Cursor_.back();
1235 ////////////////////////////////////////////////////////////////////////
1238 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1240 return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1244 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1246 if (ti.par_ != it.par_) {
1247 lyxerr << "can't compare cursor and anchor in different insets\n";
1250 if (ti.idx_ != it.idx_)
1251 return ti.idx_ < it.idx_;
1252 return ti.pos_ < it.pos_;
1256 MathArray & MathCursorPos::cell(int idx) const
1258 return par_->cell(idx);
1261 MathArray & MathCursorPos::cell() const
1263 return par_->cell(idx_);
1267 MathXArray & MathCursorPos::xcell(int idx) const
1269 return par_->xcell(idx);
1273 MathXArray & MathCursorPos::xcell() const
1275 return par_->xcell(idx_);
1279 MathCursorPos MathCursor::normalAnchor() const
1281 // use Anchor on the same level as Cursor
1282 MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1283 if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1284 // anchor is behind cursor -> move anchor behind the inset
1285 normal.cell().next(normal.pos_);
1287 //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1288 // << normal << "\n";
1293 bool MathCursorPos::idxUp()
1295 return par_->idxUp(idx_, pos_);
1299 bool MathCursorPos::idxDown()
1301 return par_->idxDown(idx_, pos_);
1305 bool MathCursorPos::idxLeft()
1307 return par_->idxLeft(idx_, pos_);
1311 bool MathCursorPos::idxRight()
1313 return par_->idxRight(idx_, pos_);