3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Alfredo Braunstein
10 * Full author contact details are available in file CREDITS.
16 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "iterators.h"
23 #include "lyxfunc.h" // only for setMessage()
27 #include "paragraph.h"
29 #include "insets/updatableinset.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
33 #include "mathed/math_data.h"
34 #include "mathed/math_hullinset.h"
35 #include "mathed/math_support.h"
37 #include "support/limited_stack.h"
38 #include "support/std_sstream.h"
40 #include "frontends/LyXView.h"
42 #include <boost/assert.hpp>
47 #ifndef CXX_GLOBAL_CSTD
56 limited_stack<string> theCutBuffer;
59 LCursor::LCursor(BufferView & bv)
60 : cursor_(1), anchor_(1), bv_(&bv), current_(0),
61 cached_y_(0), x_target_(-1),
62 selection_(false), mark_(false)
70 cursor_.push_back(CursorSlice());
71 anchor_.push_back(CursorSlice());
80 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
82 lyxerr << "\nLCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
83 BOOST_ASSERT(pos() <= lastpos());
84 BOOST_ASSERT(idx() <= lastidx());
85 BOOST_ASSERT(par() <= lastpar());
86 FuncRequest cmd = cmd0;
89 for (current_ = cursor_.size() - 1; current_ >= 1; --current_) {
90 // the inset's dispatch() is supposed to reset the update and
91 // val flags if necessary
92 inset()->dispatch(*this, cmd);
94 // "Mutate" the request for semi-handled requests that need
95 // additional handling in outer levels.
96 switch (disp_.val()) {
98 // the inset handled the event fully
99 current_ = cursor_.size() - 1;
100 return DispatchResult(true, true);
102 // the inset handled the event partially
103 cmd = FuncRequest(LFUN_FINISHED_LEFT);
106 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
109 cmd = FuncRequest(LFUN_FINISHED_UP);
112 cmd = FuncRequest(LFUN_FINISHED_DOWN);
115 //lyxerr << "not handled on level " << current_
116 // << " val: " << disp_.val() << endl;
120 BOOST_ASSERT(current_ == 0);
121 bv_->text()->dispatch(*this, cmd);
122 //lyxerr << " result: " << res.val() << endl;
123 current_ = cursor_.size() - 1;
128 bool LCursor::getStatus(FuncRequest const & cmd, FuncStatus & status)
130 lyxerr << "\nLCursor::getStatus: cmd: " << cmd << endl << *this << endl;
131 BOOST_ASSERT(pos() <= lastpos());
132 BOOST_ASSERT(idx() <= lastidx());
133 BOOST_ASSERT(par() <= lastpar());
134 for (current_ = cursor_.size() - 1; current_ >= 1; --current_) {
135 // the inset's getStatus() will return 'true' if it made
136 // a definitive decision on whether it want to handle the
137 // request or not. The result of this decision is put into
138 // the 'status' parameter.
139 bool const res = inset()->getStatus(*this, cmd, status);
141 current_ = cursor_.size() - 1;
145 BOOST_ASSERT(current_ == 0);
146 bool const res = bv_->text()->getStatus(*this, cmd, status);
147 current_ = cursor_.size() - 1;
152 void LCursor::push(InsetBase * inset)
154 lyxerr << "LCursor::push() inset: " << inset << endl;
155 cursor_.push_back(CursorSlice(inset));
156 anchor_.push_back(CursorSlice(inset));
162 void LCursor::pop(int depth)
164 while (int(cursor_.size()) > depth + 1)
166 lyxerr << "LCursor::pop() result: " << *this << endl;
172 BOOST_ASSERT(cursor_.size() >= 1);
175 current_ = cursor_.size() - 1;
179 void LCursor::pushLeft(InsetBase * p)
181 BOOST_ASSERT(!cursor_.empty());
182 //lyxerr << "Entering inset " << t << " left" << endl;
188 bool LCursor::popLeft()
190 BOOST_ASSERT(!cursor_.empty());
191 //lyxerr << "Leaving inset to the left" << endl;
194 inset()->notifyCursorLeaves(idx());
197 inset()->notifyCursorLeaves(idx());
203 bool LCursor::popRight()
205 BOOST_ASSERT(!cursor_.empty());
206 //lyxerr << "Leaving inset to the right" << endl;
209 inset()->notifyCursorLeaves(idx());
212 inset()->notifyCursorLeaves(idx());
219 CursorSlice & LCursor::current()
221 BOOST_ASSERT(!cursor_.empty());
222 //lyxerr << "accessing cursor slice " << current_
223 // << ": " << cursor_[current_] << endl;
224 return cursor_[current_];
228 CursorSlice const & LCursor::current() const
230 //lyxerr << "accessing cursor slice " << current_
231 // << ": " << cursor_[current_] << endl;
232 return cursor_[current_];
236 int LCursor::currentMode()
238 BOOST_ASSERT(!cursor_.empty());
239 for (int i = cursor_.size() - 1; i >= 1; --i) {
240 int res = cursor_[i].inset()->currentMode();
241 if (res != MathInset::UNDECIDED_MODE)
244 return MathInset::TEXT_MODE;
248 LyXText * LCursor::innerText() const
250 BOOST_ASSERT(!cursor_.empty());
251 if (cursor_.size() > 1) {
252 // go up until first non-0 text is hit
253 // (innermost text is 0 in mathed)
254 for (int i = cursor_.size() - 1; i >= 1; --i)
255 if (cursor_[i].text())
256 return cursor_[i].text();
262 CursorSlice const & LCursor::innerTextSlice() const
264 BOOST_ASSERT(!cursor_.empty());
265 if (cursor_.size() > 1) {
266 // go up until first non-0 text is hit
267 // (innermost text is 0 in mathed)
268 for (int i = cursor_.size() - 1; i >= 1; --i)
269 if (cursor_[i].text())
276 void LCursor::updatePos()
278 BOOST_ASSERT(!cursor_.empty());
279 if (cursor_.size() > 1)
280 cached_y_ = bv_->top_y() + cursor_.back().inset()->yo();
281 //cached_y_ = cursor_.back().inset()->yo();
285 void LCursor::getDim(int & asc, int & des) const
287 BOOST_ASSERT(!cursor_.empty());
289 BOOST_ASSERT(inset());
290 BOOST_ASSERT(inset()->asMathInset());
291 //inset()->asMathInset()->getCursorDim(asc, des);
295 Row const & row = textRow();
296 asc = row.baseline();
297 des = row.height() - asc;
302 void LCursor::getPos(int & x, int & y) const
304 BOOST_ASSERT(!cursor_.empty());
307 if (cursor_.size() == 1) {
308 x = bv_->text()->cursorX(cursor_.front());
309 y = bv_->text()->cursorY(cursor_.front());
312 lyxerr << "#### LCursor::getPos: " << *this << endl;
313 BOOST_ASSERT(inset());
315 inset()->getCursorPos(cursor_.back(), x, y);
316 // getCursorPos gives _screen_ coordinates. We need to add
317 // top_y to get document coordinates. This is hidden in cached_y_.
318 //y += cached_y_ - inset()->yo();
319 // The rest is non-obvious. The reason we have to have these
320 // extra computation is that the getCursorPos() calls rely
321 // on the inset's own knowledge of its screen position.
322 // If we scroll up or down in a big enough increment,
323 // inset->draw() is not called: this doesn't update
324 // inset.yo_, so getCursor() returns an old value.
327 //lyxerr << "#### LCursor::getPos: " << *this
328 // << " x: " << x << " y: " << y << endl;
332 void LCursor::paste(string const & data)
334 dispatch(FuncRequest(LFUN_PASTE, data));
338 InsetBase * LCursor::innerInsetOfType(int code) const
340 for (int i = cursor_.size() - 1; i >= 1; --i)
341 if (cursor_[i].inset_->lyxCode() == code)
342 return cursor_[i].inset_;
347 InsetTabular * LCursor::innerInsetTabular() const
349 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
353 void LCursor::resetAnchor()
359 BufferView & LCursor::bv() const
365 MathAtom const & LCursor::prevAtom() const
367 BOOST_ASSERT(pos() > 0);
368 return cell()[pos() - 1];
372 MathAtom & LCursor::prevAtom()
374 BOOST_ASSERT(pos() > 0);
375 return cell()[pos() - 1];
379 MathAtom const & LCursor::nextAtom() const
381 BOOST_ASSERT(pos() < lastpos());
382 return cell()[pos()];
386 MathAtom & LCursor::nextAtom()
388 BOOST_ASSERT(pos() < lastpos());
389 return cell()[pos()];
393 bool LCursor::posLeft()
402 bool LCursor::posRight()
404 if (pos() == lastpos())
411 CursorSlice & LCursor::anchor()
413 return anchor_.back();
417 CursorSlice const & LCursor::anchor() const
419 return anchor_.back();
423 CursorSlice const & LCursor::selBegin() const
426 return cursor_.back();
427 return anchor() < cursor_.back() ? anchor() : cursor_.back();
431 CursorSlice & LCursor::selBegin()
434 return cursor_.back();
435 // can't use std::min as this returns a const ref
436 return anchor() < cursor_.back() ? anchor() : cursor_.back();
440 CursorSlice const & LCursor::selEnd() const
443 return cursor_.back();
444 return anchor() > cursor_.back() ? anchor() : cursor_.back();
448 CursorSlice & LCursor::selEnd()
451 return cursor_.back();
452 // can't use std::min as this returns a const ref
453 return anchor() > cursor_.back() ? anchor() : cursor_.back();
457 void LCursor::setSelection()
460 // a selection with no contents is not a selection
461 if (par() == anchor().par() && pos() == anchor().pos())
466 void LCursor::setSelection(CursorBase const & where, size_t n)
475 void LCursor::clearSelection()
484 int & LCursor::x_target()
490 int LCursor::x_target() const
496 void LCursor::clearTargetX()
502 LyXText * LCursor::text() const
504 return current_ ? current().text() : bv_->text();
508 Paragraph & LCursor::paragraph()
510 BOOST_ASSERT(inTexted());
511 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
515 Paragraph const & LCursor::paragraph() const
517 BOOST_ASSERT(inTexted());
518 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
522 Row & LCursor::textRow()
524 return *paragraph().getRow(pos());
528 Row const & LCursor::textRow() const
530 return *paragraph().getRow(pos());
534 LCursor::par_type LCursor::lastpar() const
536 return inMathed() ? 0 : text()->paragraphs().size() - 1;
540 LCursor::pos_type LCursor::lastpos() const
542 InsetBase * inset = current().inset();
543 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
547 LCursor::row_type LCursor::crow() const
549 return paragraph().row(pos());
553 LCursor::row_type LCursor::lastcrow() const
555 return paragraph().rows.size();
559 LCursor::idx_type LCursor::lastidx() const
561 return current_ ? current().lastidx() : 0;
565 size_t LCursor::nargs() const
567 // assume 1x1 grid for 'plain text'
568 return current_ ? current().nargs() : 1;
572 size_t LCursor::ncols() const
574 // assume 1x1 grid for 'plain text'
575 return current_ ? current().ncols() : 1;
579 size_t LCursor::nrows() const
581 // assume 1x1 grid for 'plain text'
582 return current_ ? current().nrows() : 1;
586 LCursor::row_type LCursor::row() const
588 BOOST_ASSERT(current_ > 0);
589 return current().row();
593 LCursor::col_type LCursor::col() const
595 BOOST_ASSERT(current_ > 0);
596 return current().col();
600 MathArray const & LCursor::cell() const
602 BOOST_ASSERT(current_ > 0);
603 return current().cell();
607 MathArray & LCursor::cell()
609 BOOST_ASSERT(current_ > 0);
610 return current().cell();
614 void LCursor::info(std::ostream & os) const
616 for (int i = 1, n = depth(); i < n; ++i) {
617 cursor_[i].inset()->infoize(os);
621 prevInset()->infoize2(os);
622 // overwite old message
629 void region(CursorSlice const & i1, CursorSlice const & i2,
630 LCursor::row_type & r1, LCursor::row_type & r2,
631 LCursor::col_type & c1, LCursor::col_type & c2)
633 InsetBase * p = i1.inset();
634 c1 = p->col(i1.idx_);
635 c2 = p->col(i2.idx_);
638 r1 = p->row(i1.idx_);
639 r2 = p->row(i2.idx_);
647 string LCursor::grabSelection()
652 CursorSlice i1 = selBegin();
653 CursorSlice i2 = selEnd();
655 if (i1.idx_ == i2.idx_) {
656 if (i1.inset()->asMathInset()) {
657 MathArray::const_iterator it = i1.cell().begin();
658 return asString(MathArray(it + i1.pos_, it + i2.pos_));
660 return "unknown selection 1";
666 region(i1, i2, r1, r2, c1, c2);
669 if (i1.inset()->asMathInset()) {
670 for (row_type row = r1; row <= r2; ++row) {
673 for (col_type col = c1; col <= c2; ++col) {
676 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
680 data = "unknown selection 2";
686 void LCursor::eraseSelection()
688 //lyxerr << "LCursor::eraseSelection" << endl;
689 CursorSlice const & i1 = selBegin();
690 CursorSlice const & i2 = selEnd();
692 if (i1.inset()->asMathInset()) {
693 if (i1.idx_ == i2.idx_) {
694 i1.cell().erase(i1.pos_, i2.pos_);
696 MathInset * p = i1.asMathInset();
699 region(i1, i2, r1, r2, c1, c2);
700 for (row_type row = r1; row <= r2; ++row)
701 for (col_type col = c1; col <= c2; ++col)
702 p->cell(p->index(row, col)).clear();
706 lyxerr << "can't erase this selection 1" << endl;
708 //lyxerr << "LCursor::eraseSelection end" << endl;
712 string LCursor::grabAndEraseSelection()
716 string res = grabSelection();
723 void LCursor::selClear()
730 void LCursor::selCopy()
733 theCutBuffer.push(grabSelection());
736 //theCutBuffer.erase();
741 void LCursor::selCut()
743 theCutBuffer.push(grabAndEraseSelection());
747 void LCursor::selDel()
749 //lyxerr << "LCursor::selDel" << endl;
757 void LCursor::selPaste(size_t n)
760 if (n < theCutBuffer.size())
761 paste(theCutBuffer[n]);
767 void LCursor::selHandle(bool sel)
769 //lyxerr << "LCursor::selHandle" << endl;
770 if (sel == selection())
777 void LCursor::selClearOrDel()
779 //lyxerr << "LCursor::selClearOrDel" << endl;
780 if (lyxrc.auto_region_delete)
787 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
789 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
790 os << " " << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
791 os << " current: " << cur.current_ << endl;
792 os << " selection: " << cur.selection_ << endl;
804 void increment(CursorBase & it)
806 CursorSlice & top = it.back();
807 MathArray & ar = top.asMathInset()->cell(top.idx_);
809 // move into the current inset if possible
810 // it is impossible for pos() == size()!
812 if (top.pos() != top.lastpos())
813 n = (ar.begin() + top.pos_)->nucleus();
814 if (n && n->isActive()) {
815 it.push_back(CursorSlice(n));
819 // otherwise move on one cell back if possible
820 if (top.pos() < top.lastpos()) {
821 // pos() == lastpos() is valid!
826 // otherwise try to move on one cell if possible
827 while (top.idx() < top.lastidx()) {
829 if (top.asMathInset()->validCell(top.idx_)) {
835 // otherwise leave array, move on one back
836 // this might yield pos() == size(), but that's a ok.
838 // it certainly invalidates top
843 CursorBase ibegin(InsetBase * p)
846 it.push_back(CursorSlice(p));
851 CursorBase iend(InsetBase * p)
854 it.push_back(CursorSlice(p));
855 CursorSlice & cur = it.back();
856 cur.idx() = cur.lastidx();
857 cur.pos() = cur.lastpos();
864 ///////////////////////////////////////////////////////////////////
866 // The part below is the non-integrated rest of the original math
867 // cursor. This should be either generalized for texted or moved
868 // back to the math insets.
870 ///////////////////////////////////////////////////////////////////
872 #include "mathed/math_braceinset.h"
873 #include "mathed/math_charinset.h"
874 #include "mathed/math_commentinset.h"
875 #include "mathed/math_factory.h"
876 #include "mathed/math_gridinset.h"
877 #include "mathed/math_macroarg.h"
878 #include "mathed/math_macrotemplate.h"
879 #include "mathed/math_mathmlstream.h"
880 #include "mathed/math_scriptinset.h"
881 #include "mathed/math_spaceinset.h"
882 #include "mathed/math_support.h"
883 #include "mathed/math_unknowninset.h"
885 //#define FILEDEBUG 1
888 bool LCursor::isInside(InsetBase const * p)
890 for (unsigned i = 0; i < depth(); ++i)
891 if (cursor_[i].inset() == p)
897 bool LCursor::openable(MathAtom const & t) const
908 // we can't move into anything new during selection
909 if (depth() == anchor_.size())
911 if (!ptr_cmp(t.nucleus(), anchor_[depth()].inset()))
918 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
920 // avoid deeper nested insets when selecting
921 if (cursor.size() > anchor.size())
924 // anchor might be deeper, should have same path then
925 for (size_t i = 0; i < cursor.size(); ++i)
926 if (cursor[i].inset() != anchor[i].inset())
929 // position should be ok.
934 void LCursor::setScreenPos(int x, int y)
936 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
937 formula()->ylow(), formula()->yhigh());
939 // this can happen on creation of "math-display"
948 void LCursor::plainErase()
954 void LCursor::markInsert()
956 cell().insert(pos(), MathAtom(new MathCharInset(0)));
960 void LCursor::markErase()
966 void LCursor::plainInsert(MathAtom const & t)
968 cell().insert(pos(), t);
973 void LCursor::insert(string const & str)
975 lyxerr << "LCursor::insert str '" << str << "'" << endl;
978 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
979 plainInsert(MathAtom(new MathCharInset(*it)));
988 void LCursor::insert(char c)
990 //lyxerr << "LCursor::insert char '" << c << "'" << endl;
992 plainInsert(MathAtom(new MathCharInset(c)));
996 void LCursor::insert(MathAtom const & t)
998 //lyxerr << "LCursor::insert MathAtom: " << endl;
1005 void LCursor::insert(InsetBase * inset)
1008 insert(MathAtom(inset));
1010 text()->insertInset(*this, inset);
1014 void LCursor::niceInsert(string const & t)
1025 void LCursor::niceInsert(MathAtom const & t)
1028 string safe = grabAndEraseSelection();
1030 // enter the new inset and move the contents of the selection if possible
1031 if (t->isActive()) {
1033 // be careful here: don't use 'pushLeft(t)' as this we need to
1034 // push the clone, not the original
1035 pushLeft(nextAtom().nucleus());
1041 void LCursor::insert(MathArray const & ar)
1046 cell().insert(pos(), ar);
1051 bool LCursor::backspace()
1053 autocorrect() = false;
1061 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1067 if (inMacroMode()) {
1068 MathUnknownInset * p = activeMacro();
1069 if (p->name().size() > 1) {
1070 p->setName(p->name().substr(0, p->name().size() - 1));
1075 if (pos() != 0 && prevAtom()->nargs() > 0) {
1076 // let's require two backspaces for 'big stuff' and
1077 // highlight on the first
1088 bool LCursor::erase()
1090 autocorrect() = false;
1099 // delete empty cells if possible
1100 if (pos() == lastpos() && inset()->idxDelete(idx()))
1103 // special behaviour when in last position of cell
1104 if (pos() == lastpos()) {
1105 bool one_cell = inset()->nargs() == 1;
1106 if (one_cell && depth() == 1 && lastpos() == 0)
1112 inset()->idxGlue(idx());
1116 if (pos() != lastpos() && inset()->nargs() > 0) {
1130 CursorBase save = cursor_;
1134 autocorrect() = false;
1139 bool LCursor::down()
1142 CursorBase save = cursor_;
1143 if (goUpDown(false))
1146 autocorrect() = false;
1151 void LCursor::macroModeClose()
1155 MathUnknownInset * p = activeMacro();
1157 string s = p->name();
1159 cell().erase(pos());
1161 // do nothing if the macro name is empty
1165 string const name = s.substr(1);
1167 // prevent entering of recursive macros
1168 InsetBase const * macro = innerInsetOfType(InsetBase::MATHMACRO_CODE);
1169 if (macro && macro->getInsetName() == name)
1170 lyxerr << "can't enter recursive macro" << endl;
1172 niceInsert(createMathInset(name));
1176 string LCursor::macroName()
1178 return inMacroMode() ? activeMacro()->name() : string();
1182 void LCursor::handleNest(MathAtom const & a, int c)
1184 //lyxerr << "LCursor::handleNest: " << c << endl;
1186 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1189 pushLeft(nextAtom().nucleus());
1193 int LCursor::targetX() const
1195 if (x_target() != -1)
1204 MathHullInset * LCursor::formula() const
1206 for (int i = cursor_.size() - 1; i >= 1; --i) {
1207 MathInset * inset = cursor_[i].inset()->asMathInset();
1208 if (inset && inset->asHullInset())
1209 return static_cast<MathHullInset *>(inset);
1215 void LCursor::adjust(pos_type from, int diff)
1219 if (anchor().pos_ > from)
1220 anchor().pos_ += diff;
1221 // just to be on the safe side
1222 // theoretically unecessary
1227 bool LCursor::inMacroMode() const
1231 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1232 return p && !p->final();
1236 MathUnknownInset * LCursor::activeMacro()
1238 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1242 bool LCursor::inMacroArgMode() const
1244 return pos() > 0 && prevAtom()->getChar() == '#';
1248 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1250 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1251 MathInset * m = cursor_[i].inset()->asMathInset();
1254 MathGridInset * p = m->asGridInset();
1256 idx = cursor_[i].idx_;
1264 void LCursor::pullArg()
1267 MathArray ar = cell();
1268 if (popLeft() && inMathed()) {
1270 cell().insert(pos(), ar);
1273 //formula()->mutateToText();
1278 void LCursor::touch()
1282 CursorBase::const_iterator it = cursor_.begin();
1283 CursorBase::const_iterator et = cursor_.end();
1284 for ( ; it != et; ++it)
1290 void LCursor::normalize()
1292 if (idx() >= nargs()) {
1293 lyxerr << "this should not really happen - 1: "
1294 << idx() << ' ' << nargs()
1295 << " in: " << inset() << endl;
1297 idx() = min(idx(), lastidx());
1299 if (pos() > lastpos()) {
1300 lyxerr << "this should not really happen - 2: "
1301 << pos() << ' ' << lastpos() << " in idx: " << idx()
1303 WriteStream wi(lyxerr, false, true);
1304 inset()->asMathInset()->write(wi);
1307 pos() = min(pos(), lastpos());
1311 char LCursor::valign()
1314 MathGridInset * p = enclosingGrid(idx);
1315 return p ? p->valign() : '\0';
1319 char LCursor::halign()
1322 MathGridInset * p = enclosingGrid(idx);
1323 return p ? p->halign(idx % p->ncols()) : '\0';
1327 bool LCursor::goUpDown(bool up)
1329 // Be warned: The 'logic' implemented in this function is highly
1330 // fragile. A distance of one pixel or a '<' vs '<=' _really
1331 // matters. So fiddle around with it only if you think you know
1332 // what you are doing!
1337 // check if we had something else in mind, if not, this is the future goal
1338 if (x_target() == -1)
1343 // try neigbouring script insets
1347 MathScriptInset const * p = prevAtom()->asScriptInset();
1348 if (p && p->has(up)) {
1351 idx() = up; // the superscript has index 1
1353 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1359 if (pos() != lastpos()) {
1360 MathScriptInset const * p = nextAtom()->asScriptInset();
1361 if (p && p->has(up)) {
1365 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1371 // try current cell for e.g. text insets
1372 if (inset()->idxUpDown2(*this, up))
1375 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1380 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1381 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1385 // try to find an inset that knows better then we
1387 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1389 if (inset()->idxUpDown(*this, up)) {
1390 // try to find best position within this inset
1396 // no such inset found, just take something "above"
1397 //lyxerr << "updown: handled by strange case" << endl;
1403 up ? formula()->ylow() : yo + 4,
1404 up ? yo - 4 : formula()->yhigh()
1408 // any improvement so far?
1411 if (up ? ynew < yo : ynew > yo)
1417 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1419 CursorBase best_cursor;
1420 double best_dist = 1e10;
1422 CursorBase it = ibegin(formula());
1423 CursorBase et = iend(formula());
1425 // avoid invalid nesting when selecting
1426 if (!selection() || positionable(it, anchor_)) {
1428 CursorSlice & cur = it.back();
1429 cur.inset()->getCursorPos(cur, xo, yo);
1430 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1431 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1432 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1433 // '<=' in order to take the last possible position
1434 // this is important for clicking behind \sum in e.g. '\sum_i a'
1435 if (d <= best_dist) {
1447 if (best_dist < 1e10)
1448 cursor_ = best_cursor;
1449 return best_dist < 1e10;
1453 void LCursor::bruteFind2(int x, int y)
1455 double best_dist = 1e10;
1457 CursorBase it = cursor_;
1458 it.back().pos() = 0;
1459 CursorBase et = cursor_;
1460 et.back().pos() = et.back().asMathInset()->cell(et.back().idx_).size();
1461 for (int i = 0; ; ++i) {
1463 CursorSlice & cur = it.back();
1464 cur.inset()->getCursorPos(cur, xo, yo);
1465 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1466 // '<=' in order to take the last possible position
1467 // this is important for clicking behind \sum in e.g. '\sum_i a'
1468 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1469 if (d <= best_dist) {
1480 void LCursor::lockToggle()
1482 if (pos() != lastpos()) {
1483 // toggle previous inset ...
1484 nextAtom().nucleus()->lock(!nextAtom()->lock());
1485 } else if (popLeft() && pos() != lastpos()) {
1486 // ... or enclosing inset if we are in the last inset position
1487 nextAtom().nucleus()->lock(!nextAtom()->lock());
1493 CursorSlice LCursor::normalAnchor()
1495 if (anchor_.size() < depth()) {
1497 lyxerr << "unusual Anchor size" << endl;
1499 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1500 // use Anchor on the same level as Cursor
1501 CursorSlice normal = anchor_[current_];
1503 if (depth() < anchor_.size() && !(normal < xx())) {
1504 // anchor is behind cursor -> move anchor behind the inset
1513 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1515 // mouse clicks are somewhat special
1517 switch (cmd.action) {
1518 case LFUN_MOUSE_PRESS:
1519 case LFUN_MOUSE_MOTION:
1520 case LFUN_MOUSE_RELEASE:
1521 case LFUN_MOUSE_DOUBLE: {
1522 CursorSlice & pos = cursor_.back();
1526 if (x < cmd.x && pos() != 0) {
1527 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1528 if (res.dispatched())
1531 if (x > cmd.x && pos() != lastpos()) {
1532 DispatchResult const res = inset()->dispatch(cmd);
1533 if (res.dispatched())
1544 void LCursor::handleFont(string const & font)
1546 lyxerr << "LCursor::handleFont: " << font << endl;
1550 safe = grabAndEraseSelection();
1553 if (lastpos() != 0) {
1554 // something left in the cell
1556 // cursor in first position
1558 } else if (pos() == lastpos()) {
1559 // cursor in last position
1562 // cursor in between. split cell
1563 MathArray::iterator bt = cell().begin();
1564 MathAtom at = createMathInset(font);
1565 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1566 cell().erase(bt, bt + pos());
1571 // nothing left in the cell
1579 bool LCursor::inMathed() const
1581 return current_ && inset()->inMathed();
1585 bool LCursor::inTexted() const
1591 InsetBase * LCursor::nextInset()
1593 if (pos() == lastpos())
1596 return nextAtom().nucleus();
1597 return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
1601 InsetBase * LCursor::prevInset()
1606 return prevAtom().nucleus();
1607 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
1611 InsetBase const * LCursor::prevInset() const
1616 return prevAtom().nucleus();
1617 return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
1621 void LCursor::message(string const & msg) const
1623 bv().owner()->getLyXFunc().setMessage(msg);
1627 void LCursor::errorMessage(string const & msg) const
1629 bv().owner()->getLyXFunc().setErrorMessage(msg);
1633 string LCursor::selectionAsString(bool label) const
1639 Buffer const & buffer = *bv().buffer();
1641 // should be const ...
1642 ParagraphList::iterator startpit = text()->getPar(selBegin());
1643 ParagraphList::iterator endpit = text()->getPar(selEnd());
1644 size_t const startpos = selBegin().pos();
1645 size_t const endpos = selEnd().pos();
1647 if (startpit == endpit)
1648 return startpit->asString(buffer, startpos, endpos, label);
1650 // First paragraph in selection
1652 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1654 // The paragraphs in between (if any)
1655 ParagraphList::iterator pit = startpit;
1656 for (++pit; pit != endpit; ++pit)
1657 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1659 // Last paragraph in selection
1660 result += endpit->asString(buffer, 0, endpos, label);
1670 string LCursor::currentState()
1673 std::ostringstream os;
1677 return text() ? text()->currentState(*this) : string();
1681 // only used by the spellchecker
1682 void LCursor::replaceWord(string const & replacestring)
1684 LyXText * t = text();
1687 t->replaceSelectionWithString(*this, replacestring);
1688 t->setSelectionRange(*this, replacestring.length());
1690 // Go back so that replacement string is also spellchecked
1691 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
1692 t->cursorLeft(*this);
1696 void LCursor::update()
1702 string LCursor::getPossibleLabel()
1704 return inMathed() ? "eq:" : text()->getPossibleLabel(*this);
1708 void LCursor::notdispatched()
1710 disp_.dispatched(false);
1714 void LCursor::dispatched(dispatch_result_t res)
1720 void LCursor::noupdate()
1722 disp_.update(false);