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
8 * \author Dov Feldstern
10 * \author Stefan Schimanski
12 * Full author contact details are available in file CREDITS.
19 #include "BufferView.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
27 #include "FuncRequest.h"
29 #include "LyXFunc.h" // only for setMessage()
31 #include "Paragraph.h"
32 #include "ParIterator.h"
35 #include "TextMetrics.h"
36 #include "TocBackend.h"
38 #include "support/lassert.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
42 #include "insets/InsetTabular.h"
43 #include "insets/InsetText.h"
45 #include "mathed/InsetMath.h"
46 #include "mathed/InsetMathBrace.h"
47 #include "mathed/InsetMathScript.h"
48 #include "mathed/MacroTable.h"
49 #include "mathed/MathData.h"
50 #include "mathed/MathMacro.h"
52 #include <boost/bind.hpp>
64 bool positionable(DocIterator const & cursor, DocIterator const & anchor)
66 // avoid deeper nested insets when selecting
67 if (cursor.depth() > anchor.depth())
70 // anchor might be deeper, should have same path then
71 for (size_t i = 0; i < cursor.depth(); ++i)
72 if (&cursor[i].inset() != &anchor[i].inset())
75 // position should be ok.
80 // Find position closest to (x, y) in cell given by iter.
81 // Used only in mathed
82 DocIterator bruteFind2(Cursor const & c, int x, int y)
84 double best_dist = numeric_limits<double>::max();
91 et.top().pos() = et.top().asInsetMath()->cell(et.top().idx()).size();
92 for (size_t i = 0;; ++i) {
95 Inset const * inset = &it.inset();
96 map<Inset const *, Geometry> const & data =
97 c.bv().coordCache().getInsets().getData();
98 map<Inset const *, Geometry>::const_iterator I = data.find(inset);
100 // FIXME: in the case where the inset is not in the cache, this
101 // means that no part of it is visible on screen. In this case
102 // we don't do elaborate search and we just return the forwarded
103 // DocIterator at its beginning.
104 if (I == data.end()) {
109 Point o = I->second.pos;
110 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
111 // Convert to absolute
114 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
115 // '<=' in order to take the last possible position
116 // this is important for clicking behind \sum in e.g. '\sum_i a'
117 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
118 << " best: " << best_dist);
119 if (d <= best_dist) {
132 /// moves position closest to (x, y) in given box
133 bool bruteFind(Cursor & cursor,
134 int x, int y, int xlow, int xhigh, int ylow, int yhigh)
136 LASSERT(!cursor.empty(), return false);
137 Inset & inset = cursor[0].inset();
138 BufferView & bv = cursor.bv();
140 CoordCache::InnerParPosCache const & cache =
141 bv.coordCache().getParPos().find(cursor.bottom().text())->second;
142 // Get an iterator on the first paragraph in the cache
143 DocIterator it(inset);
144 it.push_back(CursorSlice(inset));
145 it.pit() = cache.begin()->first;
146 // Get an iterator after the last paragraph in the cache
147 DocIterator et(inset);
148 et.push_back(CursorSlice(inset));
149 et.pit() = boost::prior(cache.end())->first;
150 if (et.pit() >= et.lastpit())
151 et = doc_iterator_end(inset);
155 double best_dist = numeric_limits<double>::max();;
156 DocIterator best_cursor = et;
158 for ( ; it != et; it.forwardPos(true)) {
159 // avoid invalid nesting when selecting
160 if (!cursor.selection() || positionable(it, cursor.anchor_)) {
161 Point p = bv.getPos(it, false);
164 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
165 double const dx = xo - x;
166 double const dy = yo - y;
167 double const d = dx * dx + dy * dy;
168 // '<=' in order to take the last possible position
169 // this is important for clicking behind \sum in e.g. '\sum_i a'
170 if (d <= best_dist) {
171 // lyxerr << "*" << endl;
179 if (best_cursor != et) {
180 cursor.setCursor(best_cursor);
189 /// moves position closest to (x, y) in given box
190 bool bruteFind3(Cursor & cur, int x, int y, bool up)
192 BufferView & bv = cur.bv();
193 int ylow = up ? 0 : y + 1;
194 int yhigh = up ? y - 1 : bv.workHeight();
196 int xhigh = bv.workWidth();
198 // FIXME: bit more work needed to get 'from' and 'to' right.
199 pit_type from = cur.bottom().pit();
200 //pit_type to = cur.bottom().pit();
201 //lyxerr << "Pit start: " << from << endl;
203 //lyxerr << "bruteFind3: x: " << x << " y: " << y
204 // << " xlow: " << xlow << " xhigh: " << xhigh
205 // << " ylow: " << ylow << " yhigh: " << yhigh
207 DocIterator it = doc_iterator_begin(cur.buffer());
209 DocIterator et = doc_iterator_end(cur.buffer());
211 double best_dist = numeric_limits<double>::max();
212 DocIterator best_cursor = et;
214 for ( ; it != et; it.forwardPos()) {
215 // avoid invalid nesting when selecting
216 if (bv.cursorStatus(it) == CUR_INSIDE
217 && (!cur.selection() || positionable(it, cur.anchor_))) {
218 Point p = bv.getPos(it, false);
221 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
222 double const dx = xo - x;
223 double const dy = yo - y;
224 double const d = dx * dx + dy * dy;
225 //lyxerr << "itx: " << xo << " ity: " << yo << " d: " << d
226 // << " dx: " << dx << " dy: " << dy
227 // << " idx: " << it.idx() << " pos: " << it.pos()
230 // '<=' in order to take the last possible position
231 // this is important for clicking behind \sum in e.g. '\sum_i a'
232 if (d <= best_dist) {
233 //lyxerr << "*" << endl;
241 //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
242 if (best_cursor == et)
244 cur.setCursor(best_cursor);
251 // be careful: this is called from the bv's constructor, too, so
252 // bv functions are not yet available!
253 Cursor::Cursor(BufferView & bv)
254 : DocIterator(&bv.buffer()), bv_(&bv), anchor_(),
255 x_target_(-1), textTargetOffset_(0),
256 selection_(false), mark_(false), logicalpos_(false),
257 current_font(inherit_font)
261 void Cursor::reset(Inset & inset)
264 push_back(CursorSlice(inset));
265 anchor_ = doc_iterator_begin(&inset.buffer(), &inset);
273 // this (intentionally) does neither touch anchor nor selection status
274 void Cursor::setCursor(DocIterator const & cur)
276 DocIterator::operator=(cur);
280 void Cursor::dispatch(FuncRequest const & cmd0)
282 LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
287 FuncRequest cmd = cmd0;
290 buffer()->undo().beginUndoGroup();
292 // store some values to be used inside of the handlers
293 beforeDispatchCursor_ = *this;
294 for (; depth(); pop(), boundary(false)) {
295 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
296 << cmd0 << endl << *this);
297 LASSERT(pos() <= lastpos(), /**/);
298 LASSERT(idx() <= lastidx(), /**/);
299 LASSERT(pit() <= lastpit(), /**/);
301 // The common case is 'LFUN handled, need update', so make the
302 // LFUN handler's life easier by assuming this as default value.
303 // The handler can reset the update and val flags if necessary.
304 disp_.update(Update::FitCursor | Update::Force);
305 disp_.dispatched(true);
306 inset().dispatch(*this, cmd);
307 if (disp_.dispatched())
311 // it completely to get a 'bomb early' behaviour in case this
312 // object will be used again.
313 if (!disp_.dispatched()) {
314 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
315 // We might have invalidated the cursor when removing an empty
316 // paragraph while the cursor could not be moved out the inset
317 // while we initially thought we could. This might happen when
318 // a multiline inset becomes an inline inset when the second
319 // paragraph is removed.
320 if (safe.pit() > safe.lastpit()) {
321 safe.pit() = safe.lastpit();
322 safe.pos() = safe.lastpos();
325 disp_.update(Update::None);
326 disp_.dispatched(false);
328 // restore the previous one because nested Cursor::dispatch calls
329 // are possible which would change it
330 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
332 buffer()->undo().endUndoGroup();
336 DispatchResult Cursor::result() const
342 BufferView & Cursor::bv() const
351 LASSERT(depth() >= 1, /**/);
356 void Cursor::push(Inset & p)
358 push_back(CursorSlice(p));
359 p.setBuffer(*buffer());
363 void Cursor::pushBackward(Inset & p)
365 LASSERT(!empty(), return);
366 //lyxerr << "Entering inset " << t << " front" << endl;
372 bool Cursor::popBackward()
374 LASSERT(!empty(), return false);
382 bool Cursor::popForward()
384 LASSERT(!empty(), return false);
385 //lyxerr << "Leaving inset from in back" << endl;
386 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
390 pos() += lastpos() - lp + 1;
395 int Cursor::currentMode()
397 LASSERT(!empty(), /**/);
398 for (int i = depth() - 1; i >= 0; --i) {
399 int res = operator[](i).inset().currentMode();
400 bool locked_mode = operator[](i).inset().lockedMode();
401 // Also return UNDECIDED_MODE when the mode is locked,
402 // as in this case it is treated the same as TEXT_MODE
403 if (res != Inset::UNDECIDED_MODE || locked_mode)
406 return Inset::TEXT_MODE;
410 void Cursor::getPos(int & x, int & y) const
412 Point p = bv().getPos(*this, boundary());
418 Row const & Cursor::textRow() const
420 CursorSlice const & cs = innerTextSlice();
421 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
422 LASSERT(!pm.rows().empty(), /**/);
423 return pm.getRow(pos(), boundary());
427 void Cursor::resetAnchor()
433 void Cursor::setCursorToAnchor()
440 bool Cursor::posBackward()
449 bool Cursor::posForward()
451 if (pos() == lastpos())
458 bool Cursor::posVisRight(bool skip_inset)
460 Cursor new_cur = *this; // where we will move to
461 pos_type left_pos; // position visually left of current cursor
462 pos_type right_pos; // position visually right of current cursor
463 bool new_pos_is_RTL; // is new position we're moving to RTL?
465 getSurroundingPos(left_pos, right_pos);
467 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
469 // Are we at an inset?
470 new_cur.pos() = right_pos;
471 new_cur.boundary(false);
473 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
474 // we actually move the cursor at the end of this function, for now
475 // we just keep track of the new position in new_cur...
476 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
479 // Are we already at rightmost pos in row?
480 else if (text()->empty() || right_pos == -1) {
483 if (!new_cur.posVisToNewRow(false)) {
484 LYXERR(Debug::RTL, "not moving!");
488 // we actually move the cursor at the end of this function, for now
489 // just keep track of the new position in new_cur...
490 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
491 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
494 // normal movement to the right
497 // Recall, if the cursor is at position 'x', that means *before*
498 // the character at position 'x'. In RTL, "before" means "to the
499 // right of", in LTR, "to the left of". So currently our situation
500 // is this: the position to our right is 'right_pos' (i.e., we're
501 // currently to the left of 'right_pos'). In order to move to the
502 // right, it depends whether or not the character at 'right_pos' is RTL.
503 new_pos_is_RTL = paragraph().getFontSettings(
504 buffer()->params(), right_pos).isVisibleRightToLeft();
505 // If the character at 'right_pos' *is* LTR, then in order to move to
506 // the right of it, we need to be *after* 'right_pos', i.e., move to
507 // position 'right_pos' + 1.
508 if (!new_pos_is_RTL) {
509 new_cur.pos() = right_pos + 1;
510 // set the boundary to true in two situations:
512 // 1. if new_pos is now lastpos, and we're in an RTL paragraph
513 // (this means that we're moving right to the end of an LTR chunk
514 // which is at the end of an RTL paragraph);
515 (new_cur.pos() == lastpos()
516 && paragraph().isRTL(buffer()->params()))
517 // 2. if the position *after* right_pos is RTL (we want to be
518 // *after* right_pos, not before right_pos + 1!)
519 || paragraph().getFontSettings(buffer()->params(),
520 new_cur.pos()).isVisibleRightToLeft()
522 new_cur.boundary(true);
523 else // set the boundary to false
524 new_cur.boundary(false);
526 // Otherwise (if the character at position 'right_pos' is RTL), then
527 // moving to the right of it is as easy as setting the new position
530 new_cur.pos() = right_pos;
531 new_cur.boundary(false);
536 bool moved = (new_cur.pos() != pos()
537 || new_cur.pit() != pit()
538 || new_cur.boundary() != boundary()
539 || &new_cur.inset() != &inset());
542 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
543 << (new_cur.boundary() ? " (boundary)" : ""));
551 bool Cursor::posVisLeft(bool skip_inset)
553 Cursor new_cur = *this; // where we will move to
554 pos_type left_pos; // position visually left of current cursor
555 pos_type right_pos; // position visually right of current cursor
556 bool new_pos_is_RTL; // is new position we're moving to RTL?
558 getSurroundingPos(left_pos, right_pos);
560 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
562 // Are we at an inset?
563 new_cur.pos() = left_pos;
564 new_cur.boundary(false);
566 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
567 // we actually move the cursor at the end of this function, for now
568 // we just keep track of the new position in new_cur...
569 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
572 // Are we already at leftmost pos in row?
573 else if (text()->empty() || left_pos == -1) {
576 if (!new_cur.posVisToNewRow(true)) {
577 LYXERR(Debug::RTL, "not moving!");
581 // we actually move the cursor at the end of this function, for now
582 // just keep track of the new position in new_cur...
583 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
584 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
587 // normal movement to the left
590 // Recall, if the cursor is at position 'x', that means *before*
591 // the character at position 'x'. In RTL, "before" means "to the
592 // right of", in LTR, "to the left of". So currently our situation
593 // is this: the position to our left is 'left_pos' (i.e., we're
594 // currently to the right of 'left_pos'). In order to move to the
595 // left, it depends whether or not the character at 'left_pos' is RTL.
596 new_pos_is_RTL = paragraph().getFontSettings(
597 buffer()->params(), left_pos).isVisibleRightToLeft();
598 // If the character at 'left_pos' *is* RTL, then in order to move to
599 // the left of it, we need to be *after* 'left_pos', i.e., move to
600 // position 'left_pos' + 1.
601 if (new_pos_is_RTL) {
602 new_cur.pos() = left_pos + 1;
603 // set the boundary to true in two situations:
605 // 1. if new_pos is now lastpos and we're in an LTR paragraph
606 // (this means that we're moving left to the end of an RTL chunk
607 // which is at the end of an LTR paragraph);
608 (new_cur.pos() == lastpos()
609 && !paragraph().isRTL(buffer()->params()))
610 // 2. if the position *after* left_pos is not RTL (we want to be
611 // *after* left_pos, not before left_pos + 1!)
612 || !paragraph().getFontSettings(buffer()->params(),
613 new_cur.pos()).isVisibleRightToLeft()
615 new_cur.boundary(true);
616 else // set the boundary to false
617 new_cur.boundary(false);
619 // Otherwise (if the character at position 'left_pos' is LTR), then
620 // moving to the left of it is as easy as setting the new position
623 new_cur.pos() = left_pos;
624 new_cur.boundary(false);
629 bool moved = (new_cur.pos() != pos()
630 || new_cur.pit() != pit()
631 || new_cur.boundary() != boundary());
634 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
635 << (new_cur.boundary() ? " (boundary)" : ""));
643 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos)
645 // preparing bidi tables
646 Paragraph const & par = paragraph();
647 Buffer const & buf = *buffer();
648 Row const & row = textRow();
650 bidi.computeTables(par, buf, row);
652 LYXERR(Debug::RTL, "bidi: " << row.pos() << "--" << row.endpos());
654 // The cursor is painted *before* the character at pos(), or, if 'boundary'
655 // is true, *after* the character at (pos() - 1). So we already have one
656 // known position around the cursor:
657 pos_type const known_pos = boundary() && pos() > 0 ? pos() - 1 : pos();
659 // edge case: if we're at the end of the paragraph, things are a little
660 // different (because lastpos is a position which does not really "exist"
661 // --- there's no character there yet).
662 if (known_pos == lastpos()) {
663 if (par.isRTL(buf.params())) {
665 right_pos = bidi.vis2log(row.pos());
669 left_pos = bidi.vis2log(row.endpos() - 1);
674 // Whether 'known_pos' is to the left or to the right of the cursor depends
675 // on whether it is an RTL or LTR character...
676 bool const cur_is_RTL =
677 par.getFontSettings(buf.params(), known_pos).isVisibleRightToLeft();
678 // ... in the following manner:
679 // For an RTL character, "before" means "to the right" and "after" means
680 // "to the left"; and for LTR, it's the reverse. So, 'known_pos' is to the
681 // right of the cursor if (RTL && boundary) or (!RTL && !boundary):
682 bool const known_pos_on_right = cur_is_RTL == boundary();
684 // So we now know one of the positions surrounding the cursor. Let's
685 // determine the other one:
686 if (known_pos_on_right) {
687 right_pos = known_pos;
688 // *visual* position of 'left_pos':
689 pos_type v_left_pos = bidi.log2vis(right_pos) - 1;
690 // If the position we just identified as 'left_pos' is a "skipped
691 // separator" (a separator which is at the logical end of a row,
692 // except for the last row in a paragraph; such separators are not
693 // painted, so they "are not really there"; note that in bidi text,
694 // such a separator could appear visually in the middle of a row),
695 // set 'left_pos' to the *next* position to the left.
696 if (bidi.inRange(v_left_pos)
697 && bidi.vis2log(v_left_pos) + 1 == row.endpos()
698 && row.endpos() < lastpos()
699 && par.isSeparator(bidi.vis2log(v_left_pos)))
702 // calculate the logical position of 'left_pos', if in row
703 if (!bidi.inRange(v_left_pos))
706 left_pos = bidi.vis2log(v_left_pos);
707 // If the position we identified as 'right_pos' is a "skipped
708 // separator", set 'right_pos' to the *next* position to the right.
709 if (right_pos + 1 == row.endpos() && row.endpos() < lastpos()
710 && par.isSeparator(right_pos)) {
711 pos_type const v_right_pos = bidi.log2vis(right_pos) + 1;
712 if (!bidi.inRange(v_right_pos))
715 right_pos = bidi.vis2log(v_right_pos);
718 // known_pos is on the left
719 left_pos = known_pos;
720 // *visual* position of 'right_pos'
721 pos_type v_right_pos = bidi.log2vis(left_pos) + 1;
722 // If the position we just identified as 'right_pos' is a "skipped
723 // separator", set 'right_pos' to the *next* position to the right.
724 if (bidi.inRange(v_right_pos)
725 && bidi.vis2log(v_right_pos) + 1 == row.endpos()
726 && row.endpos() < lastpos()
727 && par.isSeparator(bidi.vis2log(v_right_pos)))
730 // calculate the logical position of 'right_pos', if in row
731 if (!bidi.inRange(v_right_pos))
734 right_pos = bidi.vis2log(v_right_pos);
735 // If the position we identified as 'left_pos' is a "skipped
736 // separator", set 'left_pos' to the *next* position to the left.
737 if (left_pos + 1 == row.endpos() && row.endpos() < lastpos()
738 && par.isSeparator(left_pos)) {
739 pos_type const v_left_pos = bidi.log2vis(left_pos) - 1;
740 if (!bidi.inRange(v_left_pos))
743 left_pos = bidi.vis2log(v_left_pos);
750 bool Cursor::posVisToNewRow(bool movingLeft)
752 Paragraph const & par = paragraph();
753 Buffer const & buf = *buffer();
754 Row const & row = textRow();
755 bool par_is_LTR = !par.isRTL(buf.params());
757 // Inside a table, determining whether to move to the next or previous row
758 // should be done based on the table's direction.
760 if (s >= 1 && (*this)[s].inset().asInsetTabular()) {
761 par_is_LTR = !(*this)[s].inset().asInsetTabular()->isRightToLeft(*this);
762 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
765 // if moving left in an LTR paragraph or moving right in an RTL one,
766 // move to previous row
767 if (par_is_LTR == movingLeft) {
768 if (row.pos() == 0) { // we're at first row in paragraph
769 if (pit() == 0) // no previous paragraph! don't move
771 // move to last pos in previous par
775 } else { // move to previous row in this par
776 pos() = row.pos() - 1; // this is guaranteed to be in previous row
780 // if moving left in an RTL paragraph or moving right in an LTR one,
783 if (row.endpos() == lastpos()) { // we're at last row in paragraph
784 if (pit() == lastpit()) // last paragraph! don't move
786 // move to first row in next par
790 } else { // move to next row in this par
791 pos() = row.endpos();
796 // make sure we're at left-/right-most pos in new row
797 posVisToRowExtremity(!movingLeft);
803 void Cursor::posVisToRowExtremity(bool left)
805 // prepare bidi tables
806 Paragraph const & par = paragraph();
807 Buffer const & buf = *buffer();
808 Row const & row = textRow();
810 bidi.computeTables(par, buf, row);
812 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
813 << (boundary() ? 1 : 0));
815 if (left) { // move to leftmost position
816 // if this is an RTL paragraph, and we're at the last row in the
817 // paragraph, move to lastpos
818 if (par.isRTL(buf.params()) && row.endpos() == lastpos())
821 pos() = bidi.vis2log(row.pos());
823 // Moving to the leftmost position in the row, the cursor should
824 // normally be placed to the *left* of the leftmost position.
825 // A very common exception, though, is if the leftmost character
826 // also happens to be the separator at the (logical) end of the row
827 // --- in this case, the separator is positioned beyond the left
828 // margin, and we don't want to move the cursor there (moving to
829 // the left of the separator is equivalent to moving to the next
830 // line). So, in this case we actually want to place the cursor
831 // to the *right* of the leftmost position (the separator).
832 // Another exception is if we're moving to the logically last
833 // position in the row, which is *not* a separator: this means
834 // that the entire row has no separators (if there were any, the
835 // row would have been broken there); and therefore in this case
836 // we also move to the *right* of the last position (this indicates
837 // to the user that there is no space after this position, and is
838 // consistent with the behavior in the middle of a row --- moving
839 // right or left moves to the next/previous character; if we were
840 // to move to the *left* of this position, that would simulate
841 // a separator which is not really there!).
842 // Finally, there is an exception to the previous exception: if
843 // this non-separator-but-last-position-in-row is an inset, then
844 // we *do* want to stay to the left of it anyway: this is the
845 // "boundary" which we simulate at insets.
846 // Another exception is when row.endpos() is 0.
848 // do we want to be to the right of pos?
849 // as explained above, if at last pos in row, stay to the right
850 bool const right_of_pos = row.endpos() > 0
851 && pos() == row.endpos() - 1 && !par.isInset(pos());
853 // Now we know if we want to be to the left or to the right of pos,
854 // let's make sure we are where we want to be.
855 bool const new_pos_is_RTL =
856 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
858 if (new_pos_is_RTL != right_of_pos) {
864 // move to rightmost position
865 // if this is an LTR paragraph, and we're at the last row in the
866 // paragraph, move to lastpos
867 if (!par.isRTL(buf.params()) && row.endpos() == lastpos())
870 pos() = row.endpos() > 0 ? bidi.vis2log(row.endpos() - 1) : 0;
872 // Moving to the rightmost position in the row, the cursor should
873 // normally be placed to the *right* of the rightmost position.
874 // A very common exception, though, is if the rightmost character
875 // also happens to be the separator at the (logical) end of the row
876 // --- in this case, the separator is positioned beyond the right
877 // margin, and we don't want to move the cursor there (moving to
878 // the right of the separator is equivalent to moving to the next
879 // line). So, in this case we actually want to place the cursor
880 // to the *left* of the rightmost position (the separator).
881 // Another exception is if we're moving to the logically last
882 // position in the row, which is *not* a separator: this means
883 // that the entire row has no separators (if there were any, the
884 // row would have been broken there); and therefore in this case
885 // we also move to the *left* of the last position (this indicates
886 // to the user that there is no space after this position, and is
887 // consistent with the behavior in the middle of a row --- moving
888 // right or left moves to the next/previous character; if we were
889 // to move to the *right* of this position, that would simulate
890 // a separator which is not really there!).
891 // Finally, there is an exception to the previous exception: if
892 // this non-separator-but-last-position-in-row is an inset, then
893 // we *do* want to stay to the right of it anyway: this is the
894 // "boundary" which we simulate at insets.
895 // Another exception is when row.endpos() is 0.
897 // do we want to be to the left of pos?
898 // as explained above, if at last pos in row, stay to the left,
899 // unless the last position is the same as the first.
900 bool const left_of_pos = row.endpos() > 0
901 && pos() == row.endpos() - 1 && !par.isInset(pos());
903 // Now we know if we want to be to the left or to the right of pos,
904 // let's make sure we are where we want to be.
905 bool const new_pos_is_RTL =
906 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
908 if (new_pos_is_RTL == left_of_pos) {
914 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
915 << (boundary() ? 1 : 0));
919 CursorSlice Cursor::anchor() const
923 LASSERT(anchor_.depth() >= depth(), /**/);
924 CursorSlice normal = anchor_[depth() - 1];
925 if (depth() < anchor_.depth() && top() <= normal) {
926 // anchor is behind cursor -> move anchor behind the inset
933 CursorSlice Cursor::selBegin() const
937 return anchor() < top() ? anchor() : top();
941 CursorSlice Cursor::selEnd() const
945 return anchor() > top() ? anchor() : top();
949 DocIterator Cursor::selectionBegin() const
955 // FIXME: This is a work-around for the problem that
956 // CursorSlice doesn't keep track of the boundary.
957 if (anchor() == top())
958 di = anchor_.boundary() > boundary() ? anchor_ : *this;
960 di = anchor() < top() ? anchor_ : *this;
966 DocIterator Cursor::selectionEnd() const
972 // FIXME: This is a work-around for the problem that
973 // CursorSlice doesn't keep track of the boundary.
974 if (anchor() == top())
975 di = anchor_.boundary() < boundary() ? anchor_ : *this;
977 di = anchor() > top() ? anchor_ : *this;
979 if (di.depth() > depth()) {
987 void Cursor::setSelection()
990 // A selection with no contents is not a selection
991 // FIXME: doesnt look ok
992 if (idx() == anchor().idx() &&
993 pit() == anchor().pit() &&
994 pos() == anchor().pos())
999 void Cursor::setSelection(DocIterator const & where, int n)
1008 void Cursor::clearSelection()
1010 setSelection(false);
1016 void Cursor::setTargetX(int x)
1019 textTargetOffset_ = 0;
1023 int Cursor::x_target() const
1029 void Cursor::clearTargetX()
1032 textTargetOffset_ = 0;
1036 void Cursor::updateTextTargetOffset()
1041 textTargetOffset_ = x - x_target_;
1045 void Cursor::info(odocstream & os) const
1047 for (int i = 1, n = depth(); i < n; ++i) {
1048 operator[](i).inset().infoize(os);
1052 Inset const * inset = prevInset();
1053 // prevInset() can return 0 in certain case.
1055 prevInset()->infoize2(os);
1057 // overwite old message
1062 bool Cursor::selHandle(bool sel)
1064 //lyxerr << "Cursor::selHandle" << endl;
1067 if (sel == selection())
1071 cap::saveSelection(*this);
1079 ostream & operator<<(ostream & os, Cursor const & cur)
1081 os << "\n cursor: | anchor:\n";
1082 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
1083 os << " " << cur[i] << " | ";
1084 if (i < cur.anchor_.depth())
1085 os << cur.anchor_[i];
1087 os << "-------------------------------";
1090 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
1091 os << "------------------------------- | " << cur.anchor_[i] << "\n";
1093 os << " selection: " << cur.selection_
1094 << " x_target: " << cur.x_target_ << endl;
1099 LyXErr & operator<<(LyXErr & os, Cursor const & cur)
1109 ///////////////////////////////////////////////////////////////////
1112 // The part below is the non-integrated rest of the original math
1113 // cursor. This should be either generalized for texted or moved
1114 // back to mathed (in most cases to InsetMathNest).
1116 ///////////////////////////////////////////////////////////////////
1118 #include "mathed/InsetMathChar.h"
1119 #include "mathed/InsetMathGrid.h"
1120 #include "mathed/InsetMathScript.h"
1121 #include "mathed/InsetMathUnknown.h"
1122 #include "mathed/MathFactory.h"
1123 #include "mathed/MathStream.h"
1124 #include "mathed/MathSupport.h"
1129 //#define FILEDEBUG 1
1132 bool Cursor::isInside(Inset const * p) const
1134 for (size_t i = 0; i != depth(); ++i)
1135 if (&operator[](i).inset() == p)
1141 void Cursor::leaveInset(Inset const & inset)
1143 for (size_t i = 0; i != depth(); ++i) {
1144 if (&operator[](i).inset() == &inset) {
1152 bool Cursor::openable(MathAtom const & t) const
1163 // we can't move into anything new during selection
1164 if (depth() >= anchor_.depth())
1166 if (t.nucleus() != &anchor_[depth()].inset())
1173 void Cursor::setScreenPos(int x, int /*y*/)
1176 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1181 void Cursor::plainErase()
1183 cell().erase(pos());
1187 void Cursor::markInsert()
1189 insert(char_type(0));
1193 void Cursor::markErase()
1195 cell().erase(pos());
1199 void Cursor::plainInsert(MathAtom const & t)
1201 cell().insert(pos(), t);
1203 inset().setBuffer(bv_->buffer());
1208 void Cursor::insert(docstring const & str)
1210 for_each(str.begin(), str.end(),
1211 boost::bind(static_cast<void(Cursor::*)(char_type)>
1212 (&Cursor::insert), this, _1));
1216 void Cursor::insert(char_type c)
1218 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1219 LASSERT(!empty(), /**/);
1221 cap::selClearOrDel(*this);
1222 insert(new InsetMathChar(c));
1224 text()->insertChar(*this, c);
1229 void Cursor::insert(MathAtom const & t)
1231 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1233 cap::selClearOrDel(*this);
1238 void Cursor::insert(Inset * inset0)
1240 LASSERT(inset0, /**/);
1242 insert(MathAtom(inset0));
1244 text()->insertInset(*this, inset0);
1245 inset0->setBuffer(bv_->buffer());
1251 void Cursor::niceInsert(docstring const & t, Parse::flags f)
1262 void Cursor::niceInsert(MathAtom const & t)
1265 docstring const safe = cap::grabAndEraseSelection(*this);
1267 // enter the new inset and move the contents of the selection if possible
1268 if (t->isActive()) {
1270 // be careful here: don't use 'pushBackward(t)' as this we need to
1271 // push the clone, not the original
1272 pushBackward(*nextInset());
1273 // We may not use niceInsert here (recursion)
1281 void Cursor::insert(MathData const & ar)
1285 cap::eraseSelection(*this);
1286 cell().insert(pos(), ar);
1291 bool Cursor::backspace()
1293 autocorrect() = false;
1296 cap::eraseSelection(*this);
1301 // If empty cell, and not part of a big cell
1302 if (lastpos() == 0 && inset().nargs() == 1) {
1304 // Directly delete empty cell: [|[]] => [|]
1310 // [|], can not delete from inside
1321 if (inMacroMode()) {
1322 InsetMathUnknown * p = activeMacro();
1323 if (p->name().size() > 1) {
1324 p->setName(p->name().substr(0, p->name().size() - 1));
1329 if (pos() != 0 && prevAtom()->nargs() > 0) {
1330 // let's require two backspaces for 'big stuff' and
1331 // highlight on the first
1343 bool Cursor::erase()
1345 autocorrect() = false;
1350 cap::eraseSelection(*this);
1354 // delete empty cells if possible
1355 if (pos() == lastpos() && inset().idxDelete(idx()))
1358 // special behaviour when in last position of cell
1359 if (pos() == lastpos()) {
1360 bool one_cell = inset().nargs() == 1;
1361 if (one_cell && lastpos() == 0) {
1363 // Directly delete empty cell: [|[]] => [|]
1369 // [|], can not delete from inside
1374 inset().idxGlue(idx());
1378 // 'clever' UI hack: only erase large items if previously slected
1379 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1394 DocIterator save = *this;
1395 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1396 this->dispatch(cmd);
1397 if (disp_.dispatched())
1400 autocorrect() = false;
1408 DocIterator save = *this;
1409 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1410 this->dispatch(cmd);
1411 if (disp_.dispatched())
1414 autocorrect() = false;
1419 bool Cursor::macroModeClose()
1423 InsetMathUnknown * p = activeMacro();
1426 asArray(p->selection(), selection);
1427 docstring const s = p->name();
1429 cell().erase(pos());
1431 // do nothing if the macro name is empty
1435 // trigger updates of macros, at least, if no full
1436 // updates take place anyway
1437 updateFlags(Update::Force);
1439 docstring const name = s.substr(1);
1440 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1441 if (in && in->interpretString(*this, s))
1443 MathAtom atom = createInsetMath(name);
1445 // try to put argument into macro, if we just inserted a macro
1446 bool macroArg = false;
1447 MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1449 // macros here are still unfolded (in init mode in fact). So
1450 // we have to resolve the macro here manually and check its arity
1451 // to put the selection behind it if arity > 0.
1452 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1453 if (selection.size() > 0 && data && data->numargs() - data->optionals() > 0) {
1455 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1457 // non-greedy case. Do not touch the arguments behind
1458 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1461 // insert remembered selection into first argument of a non-macro
1462 else if (atom.nucleus()->nargs() > 0)
1463 atom.nucleus()->cell(0).append(selection);
1467 // finally put the macro argument behind, if needed
1469 if (selection.size() > 1 || selection[0]->asScriptInset())
1470 plainInsert(MathAtom(new InsetMathBrace(selection)));
1479 docstring Cursor::macroName()
1481 return inMacroMode() ? activeMacro()->name() : docstring();
1485 void Cursor::handleNest(MathAtom const & a, int c)
1487 //lyxerr << "Cursor::handleNest: " << c << endl;
1489 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1492 pushBackward(*nextInset());
1496 int Cursor::targetX() const
1498 if (x_target() != -1)
1507 int Cursor::textTargetOffset() const
1509 return textTargetOffset_;
1513 void Cursor::setTargetX()
1522 bool Cursor::inMacroMode() const
1528 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1529 return p && !p->final();
1533 InsetMathUnknown * Cursor::activeMacro()
1535 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1539 InsetMathUnknown const * Cursor::activeMacro() const
1541 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1545 void Cursor::pullArg()
1548 MathData ar = cell();
1549 if (popBackward() && inMathed()) {
1551 cell().insert(pos(), ar);
1554 //formula()->mutateToText();
1559 void Cursor::touch()
1563 DocIterator::const_iterator it = begin();
1564 DocIterator::const_iterator et = end();
1565 for ( ; it != et; ++it)
1571 void Cursor::normalize()
1573 if (idx() > lastidx()) {
1574 lyxerr << "this should not really happen - 1: "
1575 << idx() << ' ' << nargs()
1576 << " in: " << &inset() << endl;
1580 if (pos() > lastpos()) {
1581 lyxerr << "this should not really happen - 2: "
1582 << pos() << ' ' << lastpos() << " in idx: " << idx()
1584 odocstringstream os;
1585 WriteStream wi(os, false, true, WriteStream::wsDefault);
1586 inset().asInsetMath()->write(wi);
1587 lyxerr << to_utf8(os.str()) << endl;
1593 bool Cursor::upDownInMath(bool up)
1595 // Be warned: The 'logic' implemented in this function is highly
1596 // fragile. A distance of one pixel or a '<' vs '<=' _really
1597 // matters. So fiddle around with it only if you think you know
1598 // what you are doing!
1602 xo = theLyXFunc().cursorBeforeDispatchX();
1604 // check if we had something else in mind, if not, this is the future
1606 if (x_target_ == -1)
1608 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1609 // In text mode inside the line (not left or right) possibly set a new target_x,
1610 // but only if we are somewhere else than the previous target-offset.
1612 // We want to keep the x-target on subsequent up/down movements
1613 // that cross beyond the end of short lines. Thus a special
1614 // handling when the cursor is at the end of line: Use the new
1615 // x-target only if the old one was before the end of line
1616 // or the old one was after the beginning of the line
1617 bool inRTL = isWithinRtlParagraph(*this);
1621 left = pos() == textRow().endpos();
1622 right = pos() == textRow().pos();
1624 left = pos() == textRow().pos();
1625 right = pos() == textRow().endpos();
1627 if ((!left && !right) ||
1628 (left && !right && xo < x_target_) ||
1629 (!left && right && x_target_ < xo))
1636 // try neigbouring script insets
1638 if (inMathed() && !selection()) {
1641 InsetMathScript const * p = prevAtom()->asScriptInset();
1642 if (p && p->has(up)) {
1644 push(*const_cast<InsetMathScript*>(p));
1645 idx() = p->idxOfScript(up);
1648 // we went in the right direction? Otherwise don't jump into the script
1652 int oy = theLyXFunc().cursorBeforeDispatchY();
1653 if ((!up && y <= oy) ||
1662 if (pos() != lastpos()) {
1663 InsetMathScript const * p = nextAtom()->asScriptInset();
1664 if (p && p->has(up)) {
1665 push(*const_cast<InsetMathScript*>(p));
1666 idx() = p->idxOfScript(up);
1669 // we went in the right direction? Otherwise don't jump into the script
1673 int oy = theLyXFunc().cursorBeforeDispatchY();
1674 if ((!up && y <= oy) ||
1683 // try to find an inset that knows better then we,
1684 if (inset().idxUpDown(*this, up)) {
1685 //lyxerr << "idxUpDown triggered" << endl;
1686 // try to find best position within this inset
1688 setCursor(bruteFind2(*this, xo, yo));
1692 // any improvement going just out of inset?
1693 if (popBackward() && inMathed()) {
1694 //lyxerr << "updown: popBackward succeeded" << endl;
1697 int yold = theLyXFunc().cursorBeforeDispatchY();
1699 if (up ? ynew < yold : ynew > yold)
1703 // no success, we are probably at the document top or bottom
1709 bool Cursor::atFirstOrLastRow(bool up)
1711 TextMetrics const & tm = bv_->textMetrics(text());
1712 ParagraphMetrics const & pm = tm.parMetrics(pit());
1715 if (pos() && boundary())
1716 row = pm.pos2row(pos() - 1);
1718 row = pm.pos2row(pos());
1721 if (pit() == 0 && row == 0)
1724 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1725 row + 1 >= int(pm.rows().size()))
1731 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1733 LASSERT(text(), /**/);
1739 xo = theLyXFunc().cursorBeforeDispatchX();
1741 // update the targetX - this is here before the "return false"
1742 // to set a new target which can be used by InsetTexts above
1743 // if we cannot move up/down inside this inset anymore
1744 if (x_target_ == -1)
1746 else if (xo - textTargetOffset() != x_target() &&
1747 depth() == beforeDispatchCursor_.depth()) {
1748 // In text mode inside the line (not left or right) possibly set a new target_x,
1749 // but only if we are somewhere else than the previous target-offset.
1751 // We want to keep the x-target on subsequent up/down movements
1752 // that cross beyond the end of short lines. Thus a special
1753 // handling when the cursor is at the end of line: Use the new
1754 // x-target only if the old one was before the end of line
1755 // or the old one was after the beginning of the line
1756 bool inRTL = isWithinRtlParagraph(*this);
1760 left = pos() == textRow().endpos();
1761 right = pos() == textRow().pos();
1763 left = pos() == textRow().pos();
1764 right = pos() == textRow().endpos();
1766 if ((!left && !right) ||
1767 (left && !right && xo < x_target_) ||
1768 (!left && right && x_target_ < xo))
1775 // first get the current line
1776 TextMetrics & tm = bv_->textMetrics(text());
1777 ParagraphMetrics const & pm = tm.parMetrics(pit());
1779 if (pos() && boundary())
1780 row = pm.pos2row(pos() - 1);
1782 row = pm.pos2row(pos());
1784 if (atFirstOrLastRow(up)) {
1785 // Is there a place for the cursor to go ? If yes, we
1786 // can execute the DEPM, otherwise we should keep the
1787 // paragraph to host the cursor.
1788 Cursor dummy = *this;
1789 bool valid_destination = false;
1790 for(; dummy.depth(); dummy.pop())
1791 if (!dummy.atFirstOrLastRow(up)) {
1792 valid_destination = true;
1796 // will a next dispatch follow and if there is a new
1797 // dispatch will it move the cursor out ?
1798 if (depth() > 1 && valid_destination) {
1799 // The cursor hasn't changed yet. This happens when
1800 // you e.g. move out of an inset. And to give the
1801 // DEPM the possibility of doing something we must
1802 // provide it with two different cursors. (Lgb, vfr)
1804 dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1805 dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1807 updateNeeded |= bv().checkDepm(dummy, *this);
1808 updateTextTargetOffset();
1813 // with and without selection are handled differently
1815 int yo = bv().getPos(*this, boundary()).y_;
1817 // To next/previous row
1819 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1821 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1824 // This happens when you move out of an inset.
1825 // And to give the DEPM the possibility of doing
1826 // something we must provide it with two different
1828 Cursor dummy = *this;
1831 if (bv().checkDepm(dummy, old)) {
1832 updateNeeded = true;
1833 // Make sure that cur gets back whatever happened to dummy(Lgb)
1837 // if there is a selection, we stay out of any inset, and just jump to the right position:
1841 top().pos() = min(tm.x2pos(pit(), row - 1, xo), top().lastpos());
1842 } else if (pit() > 0) {
1844 TextMetrics & tm = bv_->textMetrics(text());
1845 if (!tm.contains(pit()))
1846 tm.newParMetricsUp();
1847 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1848 top().pos() = min(tm.x2pos(pit(), pmcur.rows().size() - 1, xo), top().lastpos());
1851 if (row + 1 < int(pm.rows().size())) {
1852 top().pos() = min(tm.x2pos(pit(), row + 1, xo), top().lastpos());
1853 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1855 TextMetrics & tm = bv_->textMetrics(text());
1856 if (!tm.contains(pit()))
1857 tm.newParMetricsDown();
1858 top().pos() = min(tm.x2pos(pit(), 0, xo), top().lastpos());
1862 updateNeeded |= bv().checkDepm(*this, old);
1865 updateTextTargetOffset();
1870 void Cursor::handleFont(string const & font)
1872 LYXERR(Debug::DEBUG, font);
1876 safe = cap::grabAndEraseSelection(*this);
1881 if (lastpos() != 0) {
1882 // something left in the cell
1884 // cursor in first position
1886 } else if (pos() == lastpos()) {
1887 // cursor in last position
1890 // cursor in between. split cell
1891 MathData::iterator bt = cell().begin();
1892 MathAtom at = createInsetMath(from_utf8(font));
1893 at.nucleus()->cell(0) = MathData(bt, bt + pos());
1894 cell().erase(bt, bt + pos());
1899 // nothing left in the cell
1908 void Cursor::message(docstring const & msg) const
1910 theLyXFunc().setMessage(msg);
1914 void Cursor::errorMessage(docstring const & msg) const
1916 theLyXFunc().setErrorMessage(msg);
1920 static docstring parbreak(InsetCode code)
1922 odocstringstream os;
1924 // only add blank line if we're not in an ERT or Listings inset
1925 if (code != ERT_CODE && code != LISTINGS_CODE)
1931 docstring Cursor::selectionAsString(bool with_label) const
1937 return cap::grabSelection(*this);
1939 int const label = with_label
1940 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
1942 idx_type const startidx = selBegin().idx();
1943 idx_type const endidx = selEnd().idx();
1944 if (startidx != endidx) {
1945 // multicell selection
1946 InsetTabular * table = inset().asInsetTabular();
1947 LASSERT(table, return docstring());
1948 return table->asString(startidx, endidx);
1951 ParagraphList const & pars = text()->paragraphs();
1953 pit_type const startpit = selBegin().pit();
1954 pit_type const endpit = selEnd().pit();
1955 size_t const startpos = selBegin().pos();
1956 size_t const endpos = selEnd().pos();
1958 if (startpit == endpit)
1959 return pars[startpit].asString(startpos, endpos, label);
1961 // First paragraph in selection
1962 docstring result = pars[startpit].
1963 asString(startpos, pars[startpit].size(), label)
1964 + parbreak(inset().lyxCode());
1966 // The paragraphs in between (if any)
1967 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
1968 Paragraph const & par = pars[pit];
1969 result += par.asString(0, par.size(), label)
1970 + parbreak(inset().lyxCode());
1973 // Last paragraph in selection
1974 result += pars[endpit].asString(0, endpos, label);
1980 docstring Cursor::currentState() const
1983 odocstringstream os;
1989 return text()->currentState(*this);
1995 docstring Cursor::getPossibleLabel() const
1997 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2001 Encoding const * Cursor::getEncoding() const
2005 CursorSlice const & sl = innerTextSlice();
2006 Text const & text = *sl.text();
2007 Font font = text.getPar(sl.pit()).getFont(
2008 bv().buffer().params(), sl.pos(), text.outerFont(sl.pit()));
2009 return font.language()->encoding();
2013 void Cursor::undispatched()
2015 disp_.dispatched(false);
2019 void Cursor::dispatched()
2021 disp_.dispatched(true);
2025 void Cursor::updateFlags(Update::flags f)
2031 void Cursor::noUpdate()
2033 disp_.update(Update::None);
2037 Font Cursor::getFont() const
2039 // The logic here should more or less match to the Cursor::setCurrentFont
2040 // logic, i.e. the cursor height should give a hint what will happen
2041 // if a character is entered.
2043 // HACK. far from being perfect...
2045 CursorSlice const & sl = innerTextSlice();
2046 Text const & text = *sl.text();
2047 Paragraph const & par = text.getPar(sl.pit());
2049 // on boundary, so we are really at the character before
2050 pos_type pos = sl.pos();
2051 if (pos > 0 && boundary())
2054 // on space? Take the font before (only for RTL boundary stay)
2056 TextMetrics const & tm = bv().textMetrics(&text);
2057 if (pos == sl.lastpos()
2058 || (par.isSeparator(pos)
2059 && !tm.isRTLBoundary(sl.pit(), pos)))
2063 // get font at the position
2064 Font font = par.getFont(buffer()->params(), pos,
2065 text.outerFont(sl.pit()));
2071 bool Cursor::fixIfBroken()
2073 if (DocIterator::fixIfBroken()) {
2081 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2083 // find inset in common
2085 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2086 if (&old[i].inset() != &cur[i].inset())
2090 // update words if we just moved to another paragraph
2091 if (i == old.depth() && i == cur.depth()
2092 && !cur.buffer()->isClean()
2093 && cur.inTexted() && old.inTexted()
2094 && cur.pit() != old.pit()) {
2095 old.paragraph().updateWords();
2098 // notify everything on top of the common part in old cursor,
2099 // but stop if the inset claims the cursor to be invalid now
2100 for (size_type j = i; j < old.depth(); ++j) {
2101 Cursor insetPos = old;
2103 if (old[j].inset().notifyCursorLeaves(insetPos, cur))
2107 // notify everything on top of the common part in new cursor,
2108 // but stop if the inset claims the cursor to be invalid now
2109 for (; i < cur.depth(); ++i) {
2110 if (cur[i].inset().notifyCursorEnters(cur))
2118 void Cursor::setCurrentFont()
2120 CursorSlice const & cs = innerTextSlice();
2121 Paragraph const & par = cs.paragraph();
2122 pos_type cpit = cs.pit();
2123 pos_type cpos = cs.pos();
2124 Text const & ctext = *cs.text();
2125 TextMetrics const & tm = bv().textMetrics(&ctext);
2127 // are we behind previous char in fact? -> go to that char
2128 if (cpos > 0 && boundary())
2131 // find position to take the font from
2133 // paragraph end? -> font of last char
2134 if (cpos == lastpos())
2136 // on space? -> look at the words in front of space
2137 else if (cpos > 0 && par.isSeparator(cpos)) {
2138 // abc| def -> font of c
2139 // abc |[WERBEH], i.e. boundary==true -> font of c
2140 // abc [WERBEH]| def, font of the space
2141 if (!tm.isRTLBoundary(cpit, cpos))
2147 BufferParams const & bufparams = buffer()->params();
2148 current_font = par.getFontSettings(bufparams, cpos);
2149 real_current_font = tm.displayFont(cpit, cpos);
2151 // special case for paragraph end
2152 if (cs.pos() == lastpos()
2153 && tm.isRTLBoundary(cpit, cs.pos())
2155 Language const * lang = par.getParLanguage(bufparams);
2156 current_font.setLanguage(lang);
2157 current_font.fontInfo().setNumber(FONT_OFF);
2158 real_current_font.setLanguage(lang);
2159 real_current_font.fontInfo().setNumber(FONT_OFF);
2164 bool Cursor::textUndo()
2166 DocIterator dit = *this;
2167 // Undo::textUndo() will modify dit.
2168 if (!buffer()->undo().textUndo(dit))
2178 bool Cursor::textRedo()
2180 DocIterator dit = *this;
2181 // Undo::textRedo() will modify dit.
2182 if (!buffer()->undo().textRedo(dit))
2192 void Cursor::finishUndo() const
2194 buffer()->undo().finishUndo();
2198 void Cursor::beginUndoGroup() const
2200 buffer()->undo().beginUndoGroup();
2204 void Cursor::endUndoGroup() const
2206 buffer()->undo().endUndoGroup();
2210 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2212 buffer()->undo().recordUndo(*this, kind, from, to);
2216 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2218 buffer()->undo().recordUndo(*this, kind, from);
2222 void Cursor::recordUndo(UndoKind kind) const
2224 buffer()->undo().recordUndo(*this, kind);
2228 void Cursor::recordUndoInset(UndoKind kind) const
2230 buffer()->undo().recordUndoInset(*this, kind);
2234 void Cursor::recordUndoFullDocument() const
2236 buffer()->undo().recordUndoFullDocument(*this);
2240 void Cursor::recordUndoSelection() const
2243 if (cap::multipleCellsSelected(*this))
2248 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2249 selBegin().pit(), selEnd().pit());
2254 void Cursor::checkBufferStructure()
2256 Buffer const * master = buffer()->masterBuffer();
2257 master->tocBackend().updateItem(*this);
2258 if (master != buffer() && !master->hasGuiDelegate())
2259 // In case the master has no gui associated with it,
2260 // the TocItem is not updated (part of bug 5699).
2261 buffer()->tocBackend().updateItem(*this);