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.
22 #pragma implementation
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "BufferView.h"
30 #include "frontends/Painter.h"
31 #include "math_cursor.h"
32 #include "formulabase.h"
33 #include "funcrequest.h"
34 #include "math_autocorrect.h"
35 #include "math_arrayinset.h"
36 #include "math_braceinset.h"
37 #include "math_commentinset.h"
38 #include "math_charinset.h"
39 #include "math_extern.h"
40 #include "math_factory.h"
41 #include "math_hullinset.h"
42 #include "math_iterator.h"
43 #include "math_macroarg.h"
44 #include "math_macrotemplate.h"
45 #include "math_mathmlstream.h"
46 #include "math_parser.h"
47 #include "math_replace.h"
48 #include "math_scriptinset.h"
49 #include "math_spaceinset.h"
50 #include "math_support.h"
51 #include "math_unknowninset.h"
63 using std::ostringstream;
67 // matheds own cut buffer
68 MathGridInset theCutBuffer = MathGridInset(1, 1);
71 MathCursor::MathCursor(InsetFormulaBase * formula, bool front)
72 : formula_(formula), autocorrect_(false), selection_(false)
74 front ? first() : last();
79 MathCursor::~MathCursor()
81 // ensure that 'notifyCursorLeave' is called
87 void MathCursor::push(MathAtom & t)
89 Cursor_.push_back(MathCursorPos(t.nucleus()));
93 void MathCursor::pushLeft(MathAtom & t)
95 //lyxerr << "Entering atom " << t << " left\n";
97 t->idxFirst(idx(), pos());
101 void MathCursor::pushRight(MathAtom & t)
103 //lyxerr << "Entering atom " << t << " right\n";
106 t->idxLast(idx(), pos());
110 bool MathCursor::popLeft()
112 //cerr << "Leaving atom to the left\n";
115 par()->notifyCursorLeaves();
118 par()->notifyCursorLeaves();
124 bool MathCursor::popRight()
126 //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n";
129 par()->notifyCursorLeaves();
132 par()->notifyCursorLeaves();
141 void MathCursor::dump(char const * what) const
143 lyxerr << "MC: " << what << "\n";
144 lyxerr << " Cursor: " << depth() << "\n";
145 for (unsigned i = 0; i < depth(); ++i)
146 lyxerr << " i: " << i << " " << Cursor_[i] << "\n";
147 lyxerr << " Anchor: " << Anchor_.size() << "\n";
148 for (unsigned i = 0; i < Anchor_.size(); ++i)
149 lyxerr << " i: " << i << " " << Anchor_[i] << "\n";
150 lyxerr << " sel: " << selection_ << "\n";
153 void MathCursor::dump(char const *) const {}
157 bool MathCursor::isInside(MathInset const * p) const
159 for (unsigned i = 0; i < depth(); ++i)
160 if (Cursor_[i].par_ == p)
166 bool MathCursor::openable(MathAtom const & t, bool sel) const
175 // we can't move into anything new during selection
176 if (depth() == Anchor_.size())
178 if (t.operator->() != Anchor_[depth()].par_)
185 bool MathCursor::inNucleus() const
187 return par()->asScriptInset() && idx() == 2;
191 bool MathCursor::posLeft()
200 bool MathCursor::posRight()
209 bool MathCursor::left(bool sel)
212 autocorrect_ = false;
213 targetx_ = -1; // "no target"
220 if (hasPrevAtom() && openable(prevAtom(), sel)) {
221 pushRight(prevAtom());
225 return posLeft() || idxLeft() || popLeft() || selection_;
229 bool MathCursor::right(bool sel)
232 autocorrect_ = false;
233 targetx_ = -1; // "no target"
240 if (hasNextAtom() && openable(nextAtom(), sel)) {
241 pushLeft(nextAtom());
245 return posRight() || idxRight() || popRight() || selection_;
249 void MathCursor::first()
252 push(formula_->par());
253 par()->idxFirst(idx(), pos());
257 void MathCursor::last()
260 push(formula_->par());
261 par()->idxLast(idx(), pos());
265 bool positionable(MathIterator const & cursor, MathIterator const & anchor)
267 // avoid deeper nested insets when selecting
268 if (cursor.size() > anchor.size())
271 // anchor might be deeper, should have same path then
272 for (MathIterator::size_type i = 0; i < cursor.size(); ++i)
273 if (cursor[i].par_ != anchor[i].par_)
276 // position should be ok.
281 void MathCursor::setPos(int x, int y)
284 bool res = bruteFind(x, y,
285 formula()->xlow(), formula()->xhigh(),
286 formula()->ylow(), formula()->yhigh());
288 // this can happen on creation of "math-display"
292 targetx_ = -1; // "no target"
298 bool MathCursor::home(bool sel)
301 autocorrect_ = false;
304 if (!par()->idxHome(idx(), pos()))
307 targetx_ = -1; // "no target"
312 bool MathCursor::end(bool sel)
315 autocorrect_ = false;
318 if (!par()->idxEnd(idx(), pos()))
321 targetx_ = -1; // "no target"
326 void MathCursor::plainErase()
328 array().erase(pos());
332 void MathCursor::markInsert()
334 //lyxerr << "inserting mark\n";
335 array().insert(pos(), MathAtom(new MathCharInset(0)));
339 void MathCursor::markErase()
341 //lyxerr << "deleting mark\n";
342 array().erase(pos());
346 void MathCursor::plainInsert(MathAtom const & t)
349 array().insert(pos(), t);
354 void MathCursor::insert(string const & str)
356 //lyxerr << "inserting '" << str << "'\n";
358 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
359 plainInsert(MathAtom(new MathCharInset(*it)));
363 void MathCursor::insert(char c)
365 //lyxerr << "inserting '" << c << "'\n";
367 plainInsert(MathAtom(new MathCharInset(c)));
371 void MathCursor::insert(MathAtom const & t)
379 void MathCursor::niceInsert(MathAtom const & t)
382 MathGridInset safe = grabAndEraseSelection();
384 // enter the new inset and move the contents of the selection if possible
387 pushLeft(nextAtom());
393 void MathCursor::insert(MathArray const & ar)
399 array().insert(pos(), ar);
404 void MathCursor::paste(MathArray const & ar)
408 array().insert(pos(), ar);
413 void MathCursor::paste(MathGridInset const & data)
416 WriteStream wi(os, false, false);
418 dispatch(FuncRequest(LFUN_PASTE, os.str()));
422 void MathCursor::backspace()
424 autocorrect_ = false;
436 MathUnknownInset * p = activeMacro();
437 if (p->name().size() > 1) {
438 p->setName(p->name().substr(0, p->name().size() - 1));
444 if (prevAtom()->asScriptInset()) {
445 // simply enter nucleus
462 void MathCursor::erase()
464 autocorrect_ = false;
473 // delete empty cells if possible
475 if (par()->idxDelete(idx()))
478 // old behaviour when in last position of cell
479 if (pos() == size()) {
480 par()->idxGlue(idx());
485 // if we are standing in front of a script inset, grab item before us and
486 // move it into nucleus
487 // and remove first thing.
488 if (hasNextAtom() && nextAtom()->asScriptInset()) {
490 MathAtom at = prevAtom();
492 array().erase(pos());
493 pushLeft(nextAtom());
495 array().push_back(at);
500 pushLeft(nextAtom());
511 bool MathCursor::up(bool sel)
516 MathIterator save = Cursor_;
520 autocorrect_ = false;
525 bool MathCursor::down(bool sel)
530 MathIterator save = Cursor_;
534 autocorrect_ = false;
539 bool MathCursor::toggleLimits()
541 if (!hasNextAtom() || !nextAtom()->asScriptInset())
543 MathScriptInset * t = nextAtom().nucleus()->asScriptInset();
544 int old = t->limits();
545 t->limits(old < 0 ? 1 : -1);
546 return old != t->limits();
550 void MathCursor::macroModeClose()
554 MathUnknownInset * p = activeMacro();
556 string s = p->name();
558 array().erase(pos());
564 string MathCursor::macroName() const
566 return inMacroMode() ? activeMacro()->name() : string();
570 void MathCursor::selClear()
577 void MathCursor::selCopy()
581 theCutBuffer = grabSelection();
584 theCutBuffer = MathGridInset(1, 1);
589 void MathCursor::selCut()
592 theCutBuffer = grabAndEraseSelection();
596 void MathCursor::selDel()
606 void MathCursor::selPaste()
616 void MathCursor::selHandle(bool sel)
618 if (sel == selection_)
626 void MathCursor::selStart()
636 void MathCursor::selClearOrDel()
638 if (lyxrc.auto_region_delete)
645 void MathCursor::selGet(MathArray & ar)
649 ar = grabSelection().glue();
653 void MathCursor::drawSelection(MathPainterInfo & pi) const
659 getSelection(i1, i2);
660 i1.par_->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
664 void MathCursor::handleNest(MathAtom const & a)
667 at.nucleus()->cell(0) = grabAndEraseSelection().glue();
669 pushRight(prevAtom());
673 void MathCursor::getPos(int & x, int & y)
675 par()->getPos(idx(), pos(), x, y);
679 MathInset * MathCursor::par() const
681 return cursor().par_;
685 InsetFormulaBase * MathCursor::formula() const
691 MathCursor::idx_type MathCursor::idx() const
693 return cursor().idx_;
697 MathCursor::idx_type & MathCursor::idx()
699 return cursor().idx_;
703 MathCursor::pos_type MathCursor::pos() const
705 return cursor().pos_;
709 MathCursor::pos_type & MathCursor::pos()
711 return cursor().pos_;
715 bool MathCursor::inMacroMode() const
719 MathUnknownInset const * p = prevAtom()->asUnknownInset();
720 return p && !p->final();
724 MathUnknownInset * MathCursor::activeMacro()
726 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
730 MathUnknownInset const * MathCursor::activeMacro() const
732 return inMacroMode() ? prevAtom()->asUnknownInset() : 0;
736 bool MathCursor::inMacroArgMode() const
738 return pos() > 0 && prevAtom()->getChar() == '#';
742 bool MathCursor::selection() const
748 MathGridInset * MathCursor::enclosingGrid(MathCursor::idx_type & idx) const
750 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
751 MathGridInset * p = Cursor_[i].par_->asGridInset();
753 idx = Cursor_[i].idx_;
761 void MathCursor::popToHere(MathInset const * p)
763 while (depth() && Cursor_.back().par_ != p)
768 void MathCursor::popToEnclosingGrid()
770 while (depth() && !Cursor_.back().par_->asGridInset())
775 void MathCursor::popToEnclosingHull()
777 while (depth() && !Cursor_.back().par_->asHullInset())
782 void MathCursor::pullArg()
785 MathArray a = array();
788 array().insert(pos(), a);
791 formula()->mutateToText();
796 void MathCursor::touch()
798 MathIterator::const_iterator it = Cursor_.begin();
799 MathIterator::const_iterator et = Cursor_.end();
800 for ( ; it != et; ++it)
805 void MathCursor::normalize()
807 if (idx() >= par()->nargs()) {
808 lyxerr << "this should not really happen - 1: "
809 << idx() << " " << par()->nargs() << " in: " << par() << "\n";
812 idx() = min(idx(), par()->nargs() - 1);
814 if (pos() > size()) {
815 lyxerr << "this should not really happen - 2: "
816 << pos() << " " << size() << " in idx: " << idx()
818 WriteStream wi(lyxerr, false, true);
823 pos() = min(pos(), size());
825 // remove empty scripts if possible
827 for (pos_type i = 0; i < size(); ++i) {
828 MathScriptInset * p = array()[i].nucleus()->asScriptInset();
830 p->removeEmptyScripts();
837 // fix again position
838 pos() = min(pos(), size());
842 MathCursor::size_type MathCursor::size() const
844 return array().size();
848 bool MathCursor::hasPrevAtom() const
854 bool MathCursor::hasNextAtom() const
856 return pos() < size();
860 MathAtom const & MathCursor::prevAtom() const
862 lyx::Assert(pos() > 0);
863 return array()[pos() - 1];
867 MathAtom & MathCursor::prevAtom()
869 lyx::Assert(pos() > 0);
870 return array()[pos() - 1];
874 MathAtom const & MathCursor::nextAtom() const
876 lyx::Assert(pos() < size());
877 return array()[pos()];
881 MathAtom & MathCursor::nextAtom()
883 lyx::Assert(pos() < size());
884 return array()[pos()];
888 MathArray & MathCursor::array() const
890 static MathArray dummy;
892 if (idx() >= par()->nargs()) {
893 lyxerr << "############ idx_ " << idx() << " not valid\n";
898 lyxerr << "############ depth() == 0 not valid\n";
902 return cursor().cell();
906 void MathCursor::idxNext()
908 par()->idxNext(idx(), pos());
912 void MathCursor::idxPrev()
914 par()->idxPrev(idx(), pos());
918 char MathCursor::valign() const
921 MathGridInset * p = enclosingGrid(idx);
922 return p ? p->valign() : '\0';
926 char MathCursor::halign() const
929 MathGridInset * p = enclosingGrid(idx);
930 return p ? p->halign(idx % p->ncols()) : '\0';
934 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
936 MathCursorPos anc = normalAnchor();
937 if (anc < cursor()) {
947 MathCursorPos & MathCursor::cursor()
949 lyx::Assert(depth());
950 return Cursor_.back();
954 MathCursorPos const & MathCursor::cursor() const
956 lyx::Assert(depth());
957 return Cursor_.back();
961 bool MathCursor::goUpDown(bool up)
963 // Be warned: The 'logic' implemented in this function is highly fragile.
964 // A distance of one pixel or a '<' vs '<=' _really_ matters.
965 // So fiddle around with it only if you know what you are doing!
970 // check if we had something else in mind, if not, this is the future goal
976 // try neigbouring script insets
979 MathScriptInset const * p = prevAtom()->asScriptInset();
980 if (p && p->has(up)) {
983 idx() = up; // the superscript has index 1
985 ///lyxerr << "updown: handled by scriptinset to the left\n";
992 MathScriptInset const * p = nextAtom()->asScriptInset();
993 if (p && p->has(up)) {
997 ///lyxerr << "updown: handled by scriptinset to the right\n";
1003 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1008 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1009 // lyxerr << "updown: handled by brute find in the same cell\n";
1013 // try to find an inset that knows better then we
1015 ///lyxerr << "updown: We are in " << *par() << " idx: " << idx() << '\n';
1017 if (par()->idxUpDown(idx(), pos(), up, targetx_))
1020 // no such inset found, just take something "above"
1021 ///lyxerr << "updown: handled by strange case\n";
1027 up ? formula()->ylow() : yo + 4,
1028 up ? yo - 4 : formula()->yhigh()
1031 // any improvement so far?
1034 if (up ? ynew < yo : ynew > yo)
1040 bool MathCursor::bruteFind
1041 (int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1043 MathIterator best_cursor;
1044 double best_dist = 1e10;
1046 MathIterator it = ibegin(formula()->par().nucleus());
1047 MathIterator et = iend(formula()->par().nucleus());
1049 // avoid invalid nesting when selecting
1050 if (!selection_ || positionable(it, Anchor_)) {
1052 it.back().getPos(xo, yo);
1053 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1054 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1055 // '<=' in order to take the last possible position
1056 // this is important for clicking behind \sum in e.g. '\sum_i a'
1057 if (d <= best_dist) {
1069 if (best_dist < 1e10)
1070 Cursor_ = best_cursor;
1071 return best_dist < 1e10;
1075 bool MathCursor::idxLineLast()
1077 idx() -= idx() % par()->ncols();
1078 idx() += par()->ncols() - 1;
1083 bool MathCursor::idxLeft()
1085 return par()->idxLeft(idx(), pos());
1089 bool MathCursor::idxRight()
1091 return par()->idxRight(idx(), pos());
1095 bool MathCursor::interpret(string const & s)
1097 //lyxerr << "interpret 1: '" << s << "'\n";
1101 //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl;
1102 //owner_->getIntl()->getTransManager().TranslateAndInsert(s[0], lt);
1103 //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl;
1105 if (s.size() >= 6 && s.substr(0, 6) == "matrix") {
1110 istringstream is(s.substr(6).c_str());
1111 is >> m >> n >> v_align >> h_align;
1115 niceInsert(MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
1119 if (s.size() >= 7 && s.substr(0, 7) == "replace") {
1121 istringstream is(s.substr(7).c_str());
1124 mathed_parse_cell(rep.from, from);
1125 mathed_parse_cell(rep.to, to);
1126 lyxerr << "replacing '" << from << "' with '" << to << "'\n";
1127 par()->replace(rep);
1131 string name = s.substr(1);
1133 if (name == "over" || name == "choose" || name == "atop") {
1134 MathAtom t(createMathInset(name));
1135 t.nucleus()->asNestInset()->cell(0) = array();
1144 // prevent entering of recursive macros
1145 if (formula()->lyxCode() == Inset::MATHMACRO_CODE
1146 && formula()->getInsetName() == name)
1148 lyxerr << "can't enter recursive macro\n";
1152 niceInsert(createMathInset(name));
1157 bool MathCursor::script(bool up)
1159 // Hack to get \\^ and \\_ working
1160 if (inMacroMode() && macroName() == "\\") {
1162 niceInsert(createMathInset("mathcircumflex"));
1169 MathGridInset safe = grabAndEraseSelection();
1171 // we are in a nucleus of a script inset, move to _our_ script
1172 par()->asScriptInset()->ensure(up);
1175 } else if (hasPrevAtom() && prevAtom()->asScriptInset()) {
1176 prevAtom().nucleus()->asScriptInset()->ensure(up);
1177 pushRight(prevAtom());
1180 } else if (hasPrevAtom()) {
1182 array()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1183 pushLeft(nextAtom());
1187 plainInsert(MathAtom(new MathScriptInset(up)));
1188 prevAtom().nucleus()->asScriptInset()->ensure(up);
1189 pushRight(prevAtom());
1199 bool MathCursor::interpret(char c)
1201 //lyxerr << "interpret 2: '" << c << "'\n";
1202 targetx_ = -1; // "no target"
1203 if (inMacroArgMode()) {
1207 MathMacroTemplate const * p = formula()->par()->asMacroTemplate();
1208 if (p && 1 <= n && n <= p->numargs())
1209 insert(MathAtom(new MathMacroArgument(c - '0')));
1211 insert(createMathInset("#"));
1212 interpret(c); // try again
1218 if (inMacroMode()) {
1219 string name = macroName();
1220 //lyxerr << "interpret name: '" << name << "'\n";
1223 activeMacro()->setName(activeMacro()->name() + c);
1227 // handle 'special char' macros
1232 if (currentMode() == MathInset::TEXT_MODE)
1233 niceInsert(createMathInset("textbackslash"));
1235 niceInsert(createMathInset("backslash"));
1237 niceInsert(createMathInset(string(1, c)));
1241 // leave macro mode and try again if necessary
1248 // This is annoying as one has to press <space> far too often.
1252 // leave autocorrect mode if necessary
1253 if (autocorrect_ && c == ' ') {
1254 autocorrect_ = false;
1259 // just clear selection on pressing the space bar
1260 if (selection_ && c == ' ') {
1268 //lyxerr << "starting with macro\n";
1269 insert(MathAtom(new MathUnknownInset("\\", false)));
1274 if (currentMode() == MathInset::TEXT_MODE)
1280 if (currentMode() == MathInset::TEXT_MODE) {
1281 // insert spaces in text mode,
1282 // but suppress direct insertion of two spaces in a row
1283 // the still allows typing '<space>a<space>' and deleting the 'a', but
1284 // it is better than nothing...
1285 if (!hasPrevAtom() || prevAtom()->getChar() != ' ')
1289 if (hasPrevAtom() && prevAtom()->asSpaceInset()) {
1290 prevAtom().nucleus()->asSpaceInset()->incSpace();
1295 // if are at the very end, leave the formula
1296 return pos() != size();
1300 insert(c); // LM_TC_TEX;
1305 if (c == '{' || c == '}', c)) {
1306 insert(c); // LM_TC_TEX;
1312 niceInsert(MathAtom(new MathBraceInset));
1321 insert(createMathInset("$"));
1326 niceInsert(MathAtom(new MathCommentInset));
1331 if (isalpha(c) && lastcode_ == LM_TC_GREEK) {
1332 insert(c, LM_TC_VAR);
1336 if (isalpha(c) && lastcode_ == LM_TC_GREEK1) {
1337 insert(c, LM_TC_VAR);
1338 lastcode_ = LM_TC_VAR;
1343 insert(c, LM_TC_TEX);
1344 //bv->owner()->message(_("TeX mode"));
1349 // try auto-correction
1350 //if (autocorrect_ && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1353 // no special circumstances, so insert the character without any fuss
1355 autocorrect_ = true;
1360 void MathCursor::setSelection(MathIterator const & where, size_type n)
1369 void MathCursor::insetToggle()
1371 if (hasNextAtom()) {
1372 // toggle previous inset ...
1373 nextAtom().nucleus()->lock(!nextAtom()->lock());
1374 } else if (popLeft() && hasNextAtom()) {
1375 // ... or enclosing inset if we are in the last inset position
1376 nextAtom().nucleus()->lock(!nextAtom()->lock());
1382 string MathCursor::info() const
1385 os << "Math editor mode. ";
1386 for (int i = 0, n = depth(); i < n; ++i) {
1387 Cursor_[i].par_->infoize(os);
1391 if (prevAtom()->asSymbolInset() || prevAtom()->asScriptInset())
1392 prevAtom()->infoize(os);
1394 return os.str().c_str(); // .c_str() needed for lyxstring
1398 unsigned MathCursor::depth() const
1400 return Cursor_.size();
1408 void region(MathCursorPos const & i1, MathCursorPos const & i2,
1409 MathInset::row_type & r1, MathInset::row_type & r2,
1410 MathInset::col_type & c1, MathInset::col_type & c2)
1412 MathInset * p = i1.par_;
1413 c1 = p->col(i1.idx_);
1414 c2 = p->col(i2.idx_);
1417 r1 = p->row(i1.idx_);
1418 r2 = p->row(i2.idx_);
1426 MathGridInset MathCursor::grabSelection() const
1429 return MathGridInset();
1432 getSelection(i1, i2);
1433 // shouldn't we assert on i1.par_ == i2.par_?
1434 if (i1.idx_ == i2.idx_) {
1435 MathGridInset data(1, 1);
1436 MathArray::const_iterator it = i1.cell().begin();
1437 data.cell(0) = MathArray(it + i1.pos_, it + i2.pos_);
1442 region(i1, i2, r1, r2, c1, c2);
1443 MathGridInset data(c2 - c1 + 1, r2 - r1 + 1);
1444 for (row_type row = 0; row < data.nrows(); ++row)
1445 for (col_type col = 0; col < data.ncols(); ++col) {
1446 idx_type i = i1.par_->index(row + r1, col + c1);
1447 data.cell(data.index(row, col)) = i1.par_->cell(i);
1453 void MathCursor::eraseSelection()
1457 getSelection(i1, i2);
1458 if (i1.idx_ == i2.idx_)
1459 i1.cell().erase(i1.pos_, i2.pos_);
1461 MathInset * p = i1.par_;
1464 region(i1, i2, r1, r2, c1, c2);
1465 for (row_type row = r1; row <= r2; ++row)
1466 for (col_type col = c1; col <= c2; ++col)
1467 p->cell(p->index(row, col)).clear();
1473 MathGridInset MathCursor::grabAndEraseSelection()
1476 return MathGridInset();
1477 MathGridInset res = grabSelection();
1484 MathCursorPos MathCursor::normalAnchor() const
1486 if (Anchor_.size() < depth()) {
1488 lyxerr << "unusual Anchor size\n";
1490 //lyx::Assert(Anchor_.size() >= cursor.depth());
1491 // use Anchor on the same level as Cursor
1492 MathCursorPos normal = Anchor_[depth() - 1];
1493 if (depth() < Anchor_.size() && !(normal < cursor())) {
1494 // anchor is behind cursor -> move anchor behind the inset
1501 MathInset::result_type MathCursor::dispatch(FuncRequest const & cmd)
1503 // try to dispatch to adajcent items if they are not editable
1504 // actually, this should only happen for mouse clicks...
1507 if (hasNextAtom() && !openable(nextAtom(), false)) {
1508 MathInset::result_type res = nextAtom().nucleus()->dispatch(cmd, d1, d2);
1509 if (res != MathInset::UNDISPATCHED)
1512 if (hasPrevAtom() && !openable(prevAtom(), false)) {
1513 MathInset::result_type res = prevAtom().nucleus()->dispatch(cmd, d1, d2);
1514 if (res != MathInset::UNDISPATCHED)
1518 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1519 MathCursorPos & pos = Cursor_[i];
1520 MathInset::result_type const res
1521 = pos.par_->dispatch(cmd, pos.idx_, pos.pos_);
1522 if (res != MathInset::UNDISPATCHED) {
1523 if (res == MathInset::DISPATCHED_POP) {
1524 Cursor_.shrink(i + 1);
1530 return MathInset::UNDISPATCHED;
1534 MathInset::mode_type MathCursor::currentMode() const
1536 for (int i = Cursor_.size() - 1; i >= 0; --i) {
1537 MathInset::mode_type res = Cursor_[i].par_->currentMode();
1538 if (res != MathInset::UNDECIDED_MODE)
1541 return MathInset::UNDECIDED_MODE;
1545 void releaseMathCursor(BufferView * bv)
1549 mathcursor->formula()->hideInsetCursor(bv);