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_funcs.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
36 #include "TextMetrics.h"
37 #include "TocBackend.h"
39 #include "support/lassert.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
43 #include "insets/InsetTabular.h"
44 #include "insets/InsetText.h"
46 #include "mathed/InsetMath.h"
47 #include "mathed/InsetMathBrace.h"
48 #include "mathed/InsetMathScript.h"
49 #include "mathed/MacroTable.h"
50 #include "mathed/MathData.h"
51 #include "mathed/MathMacro.h"
53 #include <boost/bind.hpp>
65 bool positionable(DocIterator const & cursor, DocIterator const & anchor)
67 // avoid deeper nested insets when selecting
68 if (cursor.depth() > anchor.depth())
71 // anchor might be deeper, should have same path then
72 for (size_t i = 0; i < cursor.depth(); ++i)
73 if (&cursor[i].inset() != &anchor[i].inset())
76 // position should be ok.
81 // Find position closest to (x, y) in cell given by iter.
82 // Used only in mathed
83 DocIterator bruteFind2(Cursor const & c, int x, int y)
85 double best_dist = numeric_limits<double>::max();
92 et.top().pos() = et.top().asInsetMath()->cell(et.top().idx()).size();
93 for (size_t i = 0;; ++i) {
96 Inset const * inset = &it.inset();
97 map<Inset const *, Geometry> const & data =
98 c.bv().coordCache().getInsets().getData();
99 map<Inset const *, Geometry>::const_iterator I = data.find(inset);
101 // FIXME: in the case where the inset is not in the cache, this
102 // means that no part of it is visible on screen. In this case
103 // we don't do elaborate search and we just return the forwarded
104 // DocIterator at its beginning.
105 if (I == data.end()) {
110 Point o = I->second.pos;
111 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
112 // Convert to absolute
115 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
116 // '<=' in order to take the last possible position
117 // this is important for clicking behind \sum in e.g. '\sum_i a'
118 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
119 << " best: " << best_dist);
120 if (d <= best_dist) {
133 /// moves position closest to (x, y) in given box
134 bool bruteFind(Cursor & cursor,
135 int x, int y, int xlow, int xhigh, int ylow, int yhigh)
137 LASSERT(!cursor.empty(), return false);
138 Inset & inset = cursor[0].inset();
139 BufferView & bv = cursor.bv();
141 CoordCache::InnerParPosCache const & cache =
142 bv.coordCache().getParPos().find(cursor.bottom().text())->second;
143 // Get an iterator on the first paragraph in the cache
144 DocIterator it(inset);
145 it.push_back(CursorSlice(inset));
146 it.pit() = cache.begin()->first;
147 // Get an iterator after the last paragraph in the cache
148 DocIterator et(inset);
149 et.push_back(CursorSlice(inset));
150 et.pit() = boost::prior(cache.end())->first;
151 if (et.pit() >= et.lastpit())
152 et = doc_iterator_end(inset);
156 double best_dist = numeric_limits<double>::max();;
157 DocIterator best_cursor = et;
159 for ( ; it != et; it.forwardPos(true)) {
160 // avoid invalid nesting when selecting
161 if (!cursor.selection() || positionable(it, cursor.anchor_)) {
162 Point p = bv.getPos(it, false);
165 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
166 double const dx = xo - x;
167 double const dy = yo - y;
168 double const d = dx * dx + dy * dy;
169 // '<=' in order to take the last possible position
170 // this is important for clicking behind \sum in e.g. '\sum_i a'
171 if (d <= best_dist) {
172 // lyxerr << "*" << endl;
180 if (best_cursor != et) {
181 cursor.setCursor(best_cursor);
190 /// moves position closest to (x, y) in given box
191 bool bruteFind3(Cursor & cur, int x, int y, bool up)
193 BufferView & bv = cur.bv();
194 int ylow = up ? 0 : y + 1;
195 int yhigh = up ? y - 1 : bv.workHeight();
197 int xhigh = bv.workWidth();
199 // FIXME: bit more work needed to get 'from' and 'to' right.
200 pit_type from = cur.bottom().pit();
201 //pit_type to = cur.bottom().pit();
202 //lyxerr << "Pit start: " << from << endl;
204 //lyxerr << "bruteFind3: x: " << x << " y: " << y
205 // << " xlow: " << xlow << " xhigh: " << xhigh
206 // << " ylow: " << ylow << " yhigh: " << yhigh
208 DocIterator it = doc_iterator_begin(cur.buffer());
210 DocIterator et = doc_iterator_end(cur.buffer());
212 double best_dist = numeric_limits<double>::max();
213 DocIterator best_cursor = et;
215 for ( ; it != et; it.forwardPos()) {
216 // avoid invalid nesting when selecting
217 if (bv.cursorStatus(it) == CUR_INSIDE
218 && (!cur.selection() || positionable(it, cur.anchor_))) {
219 Point p = bv.getPos(it, false);
222 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
223 double const dx = xo - x;
224 double const dy = yo - y;
225 double const d = dx * dx + dy * dy;
226 //lyxerr << "itx: " << xo << " ity: " << yo << " d: " << d
227 // << " dx: " << dx << " dy: " << dy
228 // << " idx: " << it.idx() << " pos: " << it.pos()
231 // '<=' in order to take the last possible position
232 // this is important for clicking behind \sum in e.g. '\sum_i a'
233 if (d <= best_dist) {
234 //lyxerr << "*" << endl;
242 //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
243 if (best_cursor == et)
245 cur.setCursor(best_cursor);
249 docstring parbreak(Paragraph const & par)
253 // only add blank line if we're not in an ERT or Listings inset
254 if (par.ownerCode() != ERT_CODE && par.ownerCode() != LISTINGS_CODE)
262 // be careful: this is called from the bv's constructor, too, so
263 // bv functions are not yet available!
264 Cursor::Cursor(BufferView & bv)
265 : DocIterator(&bv.buffer()), bv_(&bv), anchor_(),
266 x_target_(-1), textTargetOffset_(0),
267 selection_(false), mark_(false), logicalpos_(false),
268 current_font(inherit_font)
272 void Cursor::reset(Inset & inset)
275 push_back(CursorSlice(inset));
276 anchor_ = doc_iterator_begin(&inset.buffer(), &inset);
284 // this (intentionally) does neither touch anchor nor selection status
285 void Cursor::setCursor(DocIterator const & cur)
287 DocIterator::operator=(cur);
291 void Cursor::dispatch(FuncRequest const & cmd0)
293 LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
298 FuncRequest cmd = cmd0;
301 // store some values to be used inside of the handlers
302 beforeDispatchCursor_ = *this;
303 for (; depth(); pop(), boundary(false)) {
304 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
305 << cmd0 << endl << *this);
306 LASSERT(pos() <= lastpos(), /**/);
307 LASSERT(idx() <= lastidx(), /**/);
308 LASSERT(pit() <= lastpit(), /**/);
310 // The common case is 'LFUN handled, need update', so make the
311 // LFUN handler's life easier by assuming this as default value.
312 // The handler can reset the update and val flags if necessary.
313 disp_.update(Update::FitCursor | Update::Force);
314 disp_.dispatched(true);
315 inset().dispatch(*this, cmd);
316 if (disp_.dispatched())
320 // it completely to get a 'bomb early' behaviour in case this
321 // object will be used again.
322 if (!disp_.dispatched()) {
323 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
324 // We might have invalidated the cursor when removing an empty
325 // paragraph while the cursor could not be moved out the inset
326 // while we initially thought we could. This might happen when
327 // a multiline inset becomes an inline inset when the second
328 // paragraph is removed.
329 if (safe.pit() > safe.lastpit()) {
330 safe.pit() = safe.lastpit();
331 safe.pos() = safe.lastpos();
334 disp_.update(Update::None);
335 disp_.dispatched(false);
337 // restore the previous one because nested Cursor::dispatch calls
338 // are possible which would change it
339 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
344 DispatchResult Cursor::result() const
350 BufferView & Cursor::bv() const
359 LASSERT(depth() >= 1, /**/);
364 void Cursor::push(Inset & p)
366 push_back(CursorSlice(p));
367 p.setBuffer(*buffer());
371 void Cursor::pushBackward(Inset & p)
373 LASSERT(!empty(), return);
374 //lyxerr << "Entering inset " << t << " front" << endl;
380 bool Cursor::popBackward()
382 LASSERT(!empty(), return false);
390 bool Cursor::popForward()
392 LASSERT(!empty(), return false);
393 //lyxerr << "Leaving inset from in back" << endl;
394 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
398 pos() += lastpos() - lp + 1;
403 int Cursor::currentMode()
405 LASSERT(!empty(), /**/);
406 for (int i = depth() - 1; i >= 0; --i) {
407 int res = operator[](i).inset().currentMode();
408 if (res != Inset::UNDECIDED_MODE)
411 return Inset::TEXT_MODE;
415 void Cursor::getPos(int & x, int & y) const
417 Point p = bv().getPos(*this, boundary());
423 Row const & Cursor::textRow() const
425 CursorSlice const & cs = innerTextSlice();
426 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
427 LASSERT(!pm.rows().empty(), /**/);
428 return pm.getRow(pos(), boundary());
432 void Cursor::resetAnchor()
438 void Cursor::setCursorToAnchor()
445 bool Cursor::posBackward()
454 bool Cursor::posForward()
456 if (pos() == lastpos())
463 bool Cursor::posVisRight(bool skip_inset)
465 Cursor new_cur = *this; // where we will move to
466 pos_type left_pos; // position visually left of current cursor
467 pos_type right_pos; // position visually right of current cursor
468 bool new_pos_is_RTL; // is new position we're moving to RTL?
470 getSurroundingPos(left_pos, right_pos);
472 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
474 // Are we at an inset?
475 new_cur.pos() = right_pos;
476 new_cur.boundary(false);
478 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
479 // we actually move the cursor at the end of this function, for now
480 // we just keep track of the new position in new_cur...
481 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
484 // Are we already at rightmost pos in row?
485 else if (text()->empty() || right_pos == -1) {
488 if (!new_cur.posVisToNewRow(false)) {
489 LYXERR(Debug::RTL, "not moving!");
493 // we actually move the cursor at the end of this function, for now
494 // just keep track of the new position in new_cur...
495 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
496 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
499 // normal movement to the right
502 // Recall, if the cursor is at position 'x', that means *before*
503 // the character at position 'x'. In RTL, "before" means "to the
504 // right of", in LTR, "to the left of". So currently our situation
505 // is this: the position to our right is 'right_pos' (i.e., we're
506 // currently to the left of 'right_pos'). In order to move to the
507 // right, it depends whether or not the character at 'right_pos' is RTL.
508 new_pos_is_RTL = paragraph().getFontSettings(
509 buffer()->params(), right_pos).isVisibleRightToLeft();
510 // If the character at 'right_pos' *is* LTR, then in order to move to
511 // the right of it, we need to be *after* 'right_pos', i.e., move to
512 // position 'right_pos' + 1.
513 if (!new_pos_is_RTL) {
514 new_cur.pos() = right_pos + 1;
515 // set the boundary to true in two situations:
517 // 1. if new_pos is now lastpos, and we're in an RTL paragraph
518 // (this means that we're moving right to the end of an LTR chunk
519 // which is at the end of an RTL paragraph);
520 (new_cur.pos() == lastpos()
521 && paragraph().isRTL(buffer()->params()))
522 // 2. if the position *after* right_pos is RTL (we want to be
523 // *after* right_pos, not before right_pos + 1!)
524 || paragraph().getFontSettings(buffer()->params(),
525 new_cur.pos()).isVisibleRightToLeft()
527 new_cur.boundary(true);
528 else // set the boundary to false
529 new_cur.boundary(false);
531 // Otherwise (if the character at position 'right_pos' is RTL), then
532 // moving to the right of it is as easy as setting the new position
535 new_cur.pos() = right_pos;
536 new_cur.boundary(false);
541 bool moved = (new_cur.pos() != pos()
542 || new_cur.pit() != pit()
543 || new_cur.boundary() != boundary()
544 || &new_cur.inset() != &inset());
547 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
548 << (new_cur.boundary() ? " (boundary)" : ""));
556 bool Cursor::posVisLeft(bool skip_inset)
558 Cursor new_cur = *this; // where we will move to
559 pos_type left_pos; // position visually left of current cursor
560 pos_type right_pos; // position visually right of current cursor
561 bool new_pos_is_RTL; // is new position we're moving to RTL?
563 getSurroundingPos(left_pos, right_pos);
565 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
567 // Are we at an inset?
568 new_cur.pos() = left_pos;
569 new_cur.boundary(false);
571 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
572 // we actually move the cursor at the end of this function, for now
573 // we just keep track of the new position in new_cur...
574 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
577 // Are we already at leftmost pos in row?
578 else if (text()->empty() || left_pos == -1) {
581 if (!new_cur.posVisToNewRow(true)) {
582 LYXERR(Debug::RTL, "not moving!");
586 // we actually move the cursor at the end of this function, for now
587 // just keep track of the new position in new_cur...
588 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
589 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
592 // normal movement to the left
595 // Recall, if the cursor is at position 'x', that means *before*
596 // the character at position 'x'. In RTL, "before" means "to the
597 // right of", in LTR, "to the left of". So currently our situation
598 // is this: the position to our left is 'left_pos' (i.e., we're
599 // currently to the right of 'left_pos'). In order to move to the
600 // left, it depends whether or not the character at 'left_pos' is RTL.
601 new_pos_is_RTL = paragraph().getFontSettings(
602 buffer()->params(), left_pos).isVisibleRightToLeft();
603 // If the character at 'left_pos' *is* RTL, then in order to move to
604 // the left of it, we need to be *after* 'left_pos', i.e., move to
605 // position 'left_pos' + 1.
606 if (new_pos_is_RTL) {
607 new_cur.pos() = left_pos + 1;
608 // set the boundary to true in two situations:
610 // 1. if new_pos is now lastpos and we're in an LTR paragraph
611 // (this means that we're moving left to the end of an RTL chunk
612 // which is at the end of an LTR paragraph);
613 (new_cur.pos() == lastpos()
614 && !paragraph().isRTL(buffer()->params()))
615 // 2. if the position *after* left_pos is not RTL (we want to be
616 // *after* left_pos, not before left_pos + 1!)
617 || !paragraph().getFontSettings(buffer()->params(),
618 new_cur.pos()).isVisibleRightToLeft()
620 new_cur.boundary(true);
621 else // set the boundary to false
622 new_cur.boundary(false);
624 // Otherwise (if the character at position 'left_pos' is LTR), then
625 // moving to the left of it is as easy as setting the new position
628 new_cur.pos() = left_pos;
629 new_cur.boundary(false);
634 bool moved = (new_cur.pos() != pos()
635 || new_cur.pit() != pit()
636 || new_cur.boundary() != boundary());
639 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
640 << (new_cur.boundary() ? " (boundary)" : ""));
648 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos)
650 // preparing bidi tables
651 Paragraph const & par = paragraph();
652 Buffer const & buf = *buffer();
653 Row const & row = textRow();
655 bidi.computeTables(par, buf, row);
657 LYXERR(Debug::RTL, "bidi: " << row.pos() << "--" << row.endpos());
659 // The cursor is painted *before* the character at pos(), or, if 'boundary'
660 // is true, *after* the character at (pos() - 1). So we already have one
661 // known position around the cursor:
662 pos_type const known_pos = boundary() && pos() > 0 ? pos() - 1 : pos();
664 // edge case: if we're at the end of the paragraph, things are a little
665 // different (because lastpos is a position which does not really "exist"
666 // --- there's no character there yet).
667 if (known_pos == lastpos()) {
668 if (par.isRTL(buf.params())) {
670 right_pos = bidi.vis2log(row.pos());
674 left_pos = bidi.vis2log(row.endpos() - 1);
679 // Whether 'known_pos' is to the left or to the right of the cursor depends
680 // on whether it is an RTL or LTR character...
681 bool const cur_is_RTL =
682 par.getFontSettings(buf.params(), known_pos).isVisibleRightToLeft();
683 // ... in the following manner:
684 // For an RTL character, "before" means "to the right" and "after" means
685 // "to the left"; and for LTR, it's the reverse. So, 'known_pos' is to the
686 // right of the cursor if (RTL && boundary) or (!RTL && !boundary):
687 bool const known_pos_on_right = cur_is_RTL == boundary();
689 // So we now know one of the positions surrounding the cursor. Let's
690 // determine the other one:
691 if (known_pos_on_right) {
692 right_pos = known_pos;
693 // *visual* position of 'left_pos':
694 pos_type v_left_pos = bidi.log2vis(right_pos) - 1;
695 // If the position we just identified as 'left_pos' is a "skipped
696 // separator" (a separator which is at the logical end of a row,
697 // except for the last row in a paragraph; such separators are not
698 // painted, so they "are not really there"; note that in bidi text,
699 // such a separator could appear visually in the middle of a row),
700 // set 'left_pos' to the *next* position to the left.
701 if (bidi.inRange(v_left_pos)
702 && bidi.vis2log(v_left_pos) + 1 == row.endpos()
703 && row.endpos() < lastpos()
704 && par.isSeparator(bidi.vis2log(v_left_pos)))
707 // calculate the logical position of 'left_pos', if in row
708 if (!bidi.inRange(v_left_pos))
711 left_pos = bidi.vis2log(v_left_pos);
712 // If the position we identified as 'right_pos' is a "skipped
713 // separator", set 'right_pos' to the *next* position to the right.
714 if (right_pos + 1 == row.endpos() && row.endpos() < lastpos()
715 && par.isSeparator(right_pos)) {
716 pos_type const v_right_pos = bidi.log2vis(right_pos) + 1;
717 if (!bidi.inRange(v_right_pos))
720 right_pos = bidi.vis2log(v_right_pos);
723 // known_pos is on the left
724 left_pos = known_pos;
725 // *visual* position of 'right_pos'
726 pos_type v_right_pos = bidi.log2vis(left_pos) + 1;
727 // If the position we just identified as 'right_pos' is a "skipped
728 // separator", set 'right_pos' to the *next* position to the right.
729 if (bidi.inRange(v_right_pos)
730 && bidi.vis2log(v_right_pos) + 1 == row.endpos()
731 && row.endpos() < lastpos()
732 && par.isSeparator(bidi.vis2log(v_right_pos)))
735 // calculate the logical position of 'right_pos', if in row
736 if (!bidi.inRange(v_right_pos))
739 right_pos = bidi.vis2log(v_right_pos);
740 // If the position we identified as 'left_pos' is a "skipped
741 // separator", set 'left_pos' to the *next* position to the left.
742 if (left_pos + 1 == row.endpos() && row.endpos() < lastpos()
743 && par.isSeparator(left_pos)) {
744 pos_type const v_left_pos = bidi.log2vis(left_pos) - 1;
745 if (!bidi.inRange(v_left_pos))
748 left_pos = bidi.vis2log(v_left_pos);
755 bool Cursor::posVisToNewRow(bool movingLeft)
757 Paragraph const & par = paragraph();
758 Buffer const & buf = *buffer();
759 Row const & row = textRow();
760 bool par_is_LTR = !par.isRTL(buf.params());
762 // Inside a table, determining whether to move to the next or previous row
763 // should be done based on the table's direction.
765 if (s >= 1 && (*this)[s].inset().asInsetTabular()) {
766 par_is_LTR = !(*this)[s].inset().asInsetTabular()->isRightToLeft(*this);
767 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
770 // if moving left in an LTR paragraph or moving right in an RTL one,
771 // move to previous row
772 if (par_is_LTR == movingLeft) {
773 if (row.pos() == 0) { // we're at first row in paragraph
774 if (pit() == 0) // no previous paragraph! don't move
776 // move to last pos in previous par
780 } else { // move to previous row in this par
781 pos() = row.pos() - 1; // this is guaranteed to be in previous row
785 // if moving left in an RTL paragraph or moving right in an LTR one,
788 if (row.endpos() == lastpos()) { // we're at last row in paragraph
789 if (pit() == lastpit()) // last paragraph! don't move
791 // move to first row in next par
795 } else { // move to next row in this par
796 pos() = row.endpos();
801 // make sure we're at left-/right-most pos in new row
802 posVisToRowExtremity(!movingLeft);
808 void Cursor::posVisToRowExtremity(bool left)
810 // prepare bidi tables
811 Paragraph const & par = paragraph();
812 Buffer const & buf = *buffer();
813 Row const & row = textRow();
815 bidi.computeTables(par, buf, row);
817 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
818 << (boundary() ? 1 : 0));
820 if (left) { // move to leftmost position
821 // if this is an RTL paragraph, and we're at the last row in the
822 // paragraph, move to lastpos
823 if (par.isRTL(buf.params()) && row.endpos() == lastpos())
826 pos() = bidi.vis2log(row.pos());
828 // Moving to the leftmost position in the row, the cursor should
829 // normally be placed to the *left* of the leftmost position.
830 // A very common exception, though, is if the leftmost character
831 // also happens to be the separator at the (logical) end of the row
832 // --- in this case, the separator is positioned beyond the left
833 // margin, and we don't want to move the cursor there (moving to
834 // the left of the separator is equivalent to moving to the next
835 // line). So, in this case we actually want to place the cursor
836 // to the *right* of the leftmost position (the separator).
837 // Another exception is if we're moving to the logically last
838 // position in the row, which is *not* a separator: this means
839 // that the entire row has no separators (if there were any, the
840 // row would have been broken there); and therefore in this case
841 // we also move to the *right* of the last position (this indicates
842 // to the user that there is no space after this position, and is
843 // consistent with the behavior in the middle of a row --- moving
844 // right or left moves to the next/previous character; if we were
845 // to move to the *left* of this position, that would simulate
846 // a separator which is not really there!).
847 // Finally, there is an exception to the previous exception: if
848 // this non-separator-but-last-position-in-row is an inset, then
849 // we *do* want to stay to the left of it anyway: this is the
850 // "boundary" which we simulate at insets.
851 // Another exception is when row.endpos() is 0.
853 // do we want to be to the right of pos?
854 // as explained above, if at last pos in row, stay to the right
855 bool const right_of_pos = row.endpos() > 0
856 && pos() == row.endpos() - 1 && !par.isInset(pos());
858 // Now we know if we want to be to the left or to the right of pos,
859 // let's make sure we are where we want to be.
860 bool const new_pos_is_RTL =
861 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
863 if (new_pos_is_RTL != right_of_pos) {
869 // move to rightmost position
870 // if this is an LTR paragraph, and we're at the last row in the
871 // paragraph, move to lastpos
872 if (!par.isRTL(buf.params()) && row.endpos() == lastpos())
875 pos() = row.endpos() > 0 ? bidi.vis2log(row.endpos() - 1) : 0;
877 // Moving to the rightmost position in the row, the cursor should
878 // normally be placed to the *right* of the rightmost position.
879 // A very common exception, though, is if the rightmost character
880 // also happens to be the separator at the (logical) end of the row
881 // --- in this case, the separator is positioned beyond the right
882 // margin, and we don't want to move the cursor there (moving to
883 // the right of the separator is equivalent to moving to the next
884 // line). So, in this case we actually want to place the cursor
885 // to the *left* of the rightmost position (the separator).
886 // Another exception is if we're moving to the logically last
887 // position in the row, which is *not* a separator: this means
888 // that the entire row has no separators (if there were any, the
889 // row would have been broken there); and therefore in this case
890 // we also move to the *left* of the last position (this indicates
891 // to the user that there is no space after this position, and is
892 // consistent with the behavior in the middle of a row --- moving
893 // right or left moves to the next/previous character; if we were
894 // to move to the *right* of this position, that would simulate
895 // a separator which is not really there!).
896 // Finally, there is an exception to the previous exception: if
897 // this non-separator-but-last-position-in-row is an inset, then
898 // we *do* want to stay to the right of it anyway: this is the
899 // "boundary" which we simulate at insets.
900 // Another exception is when row.endpos() is 0.
902 // do we want to be to the left of pos?
903 // as explained above, if at last pos in row, stay to the left,
904 // unless the last position is the same as the first.
905 bool const left_of_pos = row.endpos() > 0
906 && pos() == row.endpos() - 1 && !par.isInset(pos());
908 // Now we know if we want to be to the left or to the right of pos,
909 // let's make sure we are where we want to be.
910 bool const new_pos_is_RTL =
911 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
913 if (new_pos_is_RTL == left_of_pos) {
919 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
920 << (boundary() ? 1 : 0));
924 CursorSlice Cursor::anchor() const
926 LASSERT(anchor_.depth() >= depth(), /**/);
927 CursorSlice normal = anchor_[depth() - 1];
928 if (depth() < anchor_.depth() && top() <= normal) {
929 // anchor is behind cursor -> move anchor behind the inset
936 CursorSlice Cursor::selBegin() const
940 return anchor() < top() ? anchor() : top();
944 CursorSlice Cursor::selEnd() const
948 return anchor() > top() ? anchor() : top();
952 DocIterator Cursor::selectionBegin() const
958 // FIXME: This is a work-around for the problem that
959 // CursorSlice doesn't keep track of the boundary.
960 if (anchor() == top())
961 di = anchor_.boundary() > boundary() ? anchor_ : *this;
963 di = anchor() < top() ? anchor_ : *this;
969 DocIterator Cursor::selectionEnd() const
975 // FIXME: This is a work-around for the problem that
976 // CursorSlice doesn't keep track of the boundary.
977 if (anchor() == top())
978 di = anchor_.boundary() < boundary() ? anchor_ : *this;
980 di = anchor() > top() ? anchor_ : *this;
982 if (di.depth() > depth()) {
990 void Cursor::setSelection()
993 // A selection with no contents is not a selection
994 // FIXME: doesnt look ok
995 if (idx() == anchor().idx() &&
996 pit() == anchor().pit() &&
997 pos() == anchor().pos())
1002 void Cursor::setSelection(DocIterator const & where, int n)
1011 void Cursor::clearSelection()
1013 setSelection(false);
1019 void Cursor::setTargetX(int x)
1022 textTargetOffset_ = 0;
1026 int Cursor::x_target() const
1032 void Cursor::clearTargetX()
1035 textTargetOffset_ = 0;
1039 void Cursor::updateTextTargetOffset()
1044 textTargetOffset_ = x - x_target_;
1048 void Cursor::info(odocstream & os) const
1050 for (int i = 1, n = depth(); i < n; ++i) {
1051 operator[](i).inset().infoize(os);
1055 Inset const * inset = prevInset();
1056 // prevInset() can return 0 in certain case.
1058 prevInset()->infoize2(os);
1060 // overwite old message
1065 bool Cursor::selHandle(bool sel)
1067 //lyxerr << "Cursor::selHandle" << endl;
1070 if (sel == selection())
1074 cap::saveSelection(*this);
1082 ostream & operator<<(ostream & os, Cursor const & cur)
1084 os << "\n cursor: | anchor:\n";
1085 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
1086 os << " " << cur[i] << " | ";
1087 if (i < cur.anchor_.depth())
1088 os << cur.anchor_[i];
1090 os << "-------------------------------";
1093 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
1094 os << "------------------------------- | " << cur.anchor_[i] << "\n";
1096 os << " selection: " << cur.selection_
1097 << " x_target: " << cur.x_target_ << endl;
1102 LyXErr & operator<<(LyXErr & os, Cursor const & cur)
1112 ///////////////////////////////////////////////////////////////////
1115 // The part below is the non-integrated rest of the original math
1116 // cursor. This should be either generalized for texted or moved
1117 // back to mathed (in most cases to InsetMathNest).
1119 ///////////////////////////////////////////////////////////////////
1121 #include "mathed/InsetMathChar.h"
1122 #include "mathed/InsetMathGrid.h"
1123 #include "mathed/InsetMathScript.h"
1124 #include "mathed/InsetMathUnknown.h"
1125 #include "mathed/MathFactory.h"
1126 #include "mathed/MathStream.h"
1127 #include "mathed/MathSupport.h"
1132 //#define FILEDEBUG 1
1135 bool Cursor::isInside(Inset const * p) const
1137 for (size_t i = 0; i != depth(); ++i)
1138 if (&operator[](i).inset() == p)
1144 void Cursor::leaveInset(Inset const & inset)
1146 for (size_t i = 0; i != depth(); ++i) {
1147 if (&operator[](i).inset() == &inset) {
1155 bool Cursor::openable(MathAtom const & t) const
1166 // we can't move into anything new during selection
1167 if (depth() >= anchor_.depth())
1169 if (t.nucleus() != &anchor_[depth()].inset())
1176 void Cursor::setScreenPos(int x, int /*y*/)
1179 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1184 void Cursor::plainErase()
1186 cell().erase(pos());
1190 void Cursor::markInsert()
1192 insert(char_type(0));
1196 void Cursor::markErase()
1198 cell().erase(pos());
1202 void Cursor::plainInsert(MathAtom const & t)
1204 cell().insert(pos(), t);
1206 inset().setBuffer(bv_->buffer());
1211 void Cursor::insert(docstring const & str)
1213 for_each(str.begin(), str.end(),
1214 boost::bind(static_cast<void(Cursor::*)(char_type)>
1215 (&Cursor::insert), this, _1));
1219 void Cursor::insert(char_type c)
1221 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1222 LASSERT(!empty(), /**/);
1224 cap::selClearOrDel(*this);
1225 insert(new InsetMathChar(c));
1227 text()->insertChar(*this, c);
1232 void Cursor::insert(MathAtom const & t)
1234 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1236 cap::selClearOrDel(*this);
1241 void Cursor::insert(Inset * inset0)
1243 LASSERT(inset0, /**/);
1245 insert(MathAtom(inset0));
1247 text()->insertInset(*this, inset0);
1248 inset0->setBuffer(bv_->buffer());
1254 void Cursor::niceInsert(docstring const & t, Parse::flags f)
1265 void Cursor::niceInsert(MathAtom const & t)
1268 docstring const safe = cap::grabAndEraseSelection(*this);
1270 // enter the new inset and move the contents of the selection if possible
1271 if (t->isActive()) {
1273 // be careful here: don't use 'pushBackward(t)' as this we need to
1274 // push the clone, not the original
1275 pushBackward(*nextInset());
1276 // We may not use niceInsert here (recursion)
1284 void Cursor::insert(MathData const & ar)
1288 cap::eraseSelection(*this);
1289 cell().insert(pos(), ar);
1294 bool Cursor::backspace()
1296 autocorrect() = false;
1299 cap::eraseSelection(*this);
1304 // If empty cell, and not part of a big cell
1305 if (lastpos() == 0 && inset().nargs() == 1) {
1307 // Directly delete empty cell: [|[]] => [|]
1313 // [|], can not delete from inside
1324 if (inMacroMode()) {
1325 InsetMathUnknown * p = activeMacro();
1326 if (p->name().size() > 1) {
1327 p->setName(p->name().substr(0, p->name().size() - 1));
1332 if (pos() != 0 && prevAtom()->nargs() > 0) {
1333 // let's require two backspaces for 'big stuff' and
1334 // highlight on the first
1346 bool Cursor::erase()
1348 autocorrect() = false;
1353 cap::eraseSelection(*this);
1357 // delete empty cells if possible
1358 if (pos() == lastpos() && inset().idxDelete(idx()))
1361 // special behaviour when in last position of cell
1362 if (pos() == lastpos()) {
1363 bool one_cell = inset().nargs() == 1;
1364 if (one_cell && lastpos() == 0) {
1366 // Directly delete empty cell: [|[]] => [|]
1372 // [|], can not delete from inside
1377 inset().idxGlue(idx());
1381 // 'clever' UI hack: only erase large items if previously slected
1382 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1397 DocIterator save = *this;
1398 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1399 this->dispatch(cmd);
1400 if (disp_.dispatched())
1403 autocorrect() = false;
1411 DocIterator save = *this;
1412 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1413 this->dispatch(cmd);
1414 if (disp_.dispatched())
1417 autocorrect() = false;
1422 bool Cursor::macroModeClose()
1426 InsetMathUnknown * p = activeMacro();
1429 asArray(p->selection(), selection);
1430 docstring const s = p->name();
1432 cell().erase(pos());
1434 // do nothing if the macro name is empty
1438 // trigger updates of macros, at least, if no full
1439 // updates take place anyway
1440 updateFlags(Update::Force);
1442 docstring const name = s.substr(1);
1443 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1444 if (in && in->interpretString(*this, s))
1446 MathAtom atom = createInsetMath(name);
1448 // try to put argument into macro, if we just inserted a macro
1449 bool macroArg = false;
1450 MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1452 // macros here are still unfolded (in init mode in fact). So
1453 // we have to resolve the macro here manually and check its arity
1454 // to put the selection behind it if arity > 0.
1455 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1456 if (selection.size() > 0 && data && data->numargs() - data->optionals() > 0) {
1458 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1460 // non-greedy case. Do not touch the arguments behind
1461 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1464 // insert remembered selection into first argument of a non-macro
1465 else if (atom.nucleus()->nargs() > 0)
1466 atom.nucleus()->cell(0).append(selection);
1470 // finally put the macro argument behind, if needed
1472 if (selection.size() > 1)
1473 plainInsert(MathAtom(new InsetMathBrace(selection)));
1482 docstring Cursor::macroName()
1484 return inMacroMode() ? activeMacro()->name() : docstring();
1488 void Cursor::handleNest(MathAtom const & a, int c)
1490 //lyxerr << "Cursor::handleNest: " << c << endl;
1492 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1495 pushBackward(*nextInset());
1499 int Cursor::targetX() const
1501 if (x_target() != -1)
1510 int Cursor::textTargetOffset() const
1512 return textTargetOffset_;
1516 void Cursor::setTargetX()
1525 bool Cursor::inMacroMode() const
1531 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1532 return p && !p->final();
1536 InsetMathUnknown * Cursor::activeMacro()
1538 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1542 InsetMathUnknown const * Cursor::activeMacro() const
1544 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1548 void Cursor::pullArg()
1551 MathData ar = cell();
1552 if (popBackward() && inMathed()) {
1554 cell().insert(pos(), ar);
1557 //formula()->mutateToText();
1562 void Cursor::touch()
1566 DocIterator::const_iterator it = begin();
1567 DocIterator::const_iterator et = end();
1568 for ( ; it != et; ++it)
1574 void Cursor::normalize()
1576 if (idx() > lastidx()) {
1577 lyxerr << "this should not really happen - 1: "
1578 << idx() << ' ' << nargs()
1579 << " in: " << &inset() << endl;
1583 if (pos() > lastpos()) {
1584 lyxerr << "this should not really happen - 2: "
1585 << pos() << ' ' << lastpos() << " in idx: " << idx()
1587 odocstringstream os;
1588 WriteStream wi(os, false, true, false);
1589 inset().asInsetMath()->write(wi);
1590 lyxerr << to_utf8(os.str()) << endl;
1596 bool Cursor::upDownInMath(bool up)
1598 // Be warned: The 'logic' implemented in this function is highly
1599 // fragile. A distance of one pixel or a '<' vs '<=' _really
1600 // matters. So fiddle around with it only if you think you know
1601 // what you are doing!
1605 xo = theLyXFunc().cursorBeforeDispatchX();
1607 // check if we had something else in mind, if not, this is the future
1609 if (x_target_ == -1)
1611 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1612 // In text mode inside the line (not left or right) possibly set a new target_x,
1613 // but only if we are somewhere else than the previous target-offset.
1615 // We want to keep the x-target on subsequent up/down movements
1616 // that cross beyond the end of short lines. Thus a special
1617 // handling when the cursor is at the end of line: Use the new
1618 // x-target only if the old one was before the end of line
1619 // or the old one was after the beginning of the line
1620 bool inRTL = isWithinRtlParagraph(*this);
1624 left = pos() == textRow().endpos();
1625 right = pos() == textRow().pos();
1627 left = pos() == textRow().pos();
1628 right = pos() == textRow().endpos();
1630 if ((!left && !right) ||
1631 (left && !right && xo < x_target_) ||
1632 (!left && right && x_target_ < xo))
1639 // try neigbouring script insets
1641 if (inMathed() && !selection()) {
1644 InsetMathScript const * p = prevAtom()->asScriptInset();
1645 if (p && p->has(up)) {
1647 push(*const_cast<InsetMathScript*>(p));
1648 idx() = p->idxOfScript(up);
1651 // we went in the right direction? Otherwise don't jump into the script
1655 int oy = theLyXFunc().cursorBeforeDispatchY();
1656 if ((!up && y <= oy) ||
1665 if (pos() != lastpos()) {
1666 InsetMathScript const * p = nextAtom()->asScriptInset();
1667 if (p && p->has(up)) {
1668 push(*const_cast<InsetMathScript*>(p));
1669 idx() = p->idxOfScript(up);
1672 // we went in the right direction? Otherwise don't jump into the script
1676 int oy = theLyXFunc().cursorBeforeDispatchY();
1677 if ((!up && y <= oy) ||
1686 // try to find an inset that knows better then we,
1687 if (inset().idxUpDown(*this, up)) {
1688 //lyxerr << "idxUpDown triggered" << endl;
1689 // try to find best position within this inset
1691 setCursor(bruteFind2(*this, xo, yo));
1695 // any improvement going just out of inset?
1696 if (popBackward() && inMathed()) {
1697 //lyxerr << "updown: popBackward succeeded" << endl;
1700 int yold = theLyXFunc().cursorBeforeDispatchY();
1702 if (up ? ynew < yold : ynew > yold)
1706 // no success, we are probably at the document top or bottom
1712 bool Cursor::atFirstOrLastRow(bool up)
1714 TextMetrics const & tm = bv_->textMetrics(text());
1715 ParagraphMetrics const & pm = tm.parMetrics(pit());
1718 if (pos() && boundary())
1719 row = pm.pos2row(pos() - 1);
1721 row = pm.pos2row(pos());
1724 if (pit() == 0 && row == 0)
1727 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1728 row + 1 >= int(pm.rows().size()))
1734 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1736 LASSERT(text(), /**/);
1742 xo = theLyXFunc().cursorBeforeDispatchX();
1744 // update the targetX - this is here before the "return false"
1745 // to set a new target which can be used by InsetTexts above
1746 // if we cannot move up/down inside this inset anymore
1747 if (x_target_ == -1)
1749 else if (xo - textTargetOffset() != x_target() &&
1750 depth() == beforeDispatchCursor_.depth()) {
1751 // In text mode inside the line (not left or right) possibly set a new target_x,
1752 // but only if we are somewhere else than the previous target-offset.
1754 // We want to keep the x-target on subsequent up/down movements
1755 // that cross beyond the end of short lines. Thus a special
1756 // handling when the cursor is at the end of line: Use the new
1757 // x-target only if the old one was before the end of line
1758 // or the old one was after the beginning of the line
1759 bool inRTL = isWithinRtlParagraph(*this);
1763 left = pos() == textRow().endpos();
1764 right = pos() == textRow().pos();
1766 left = pos() == textRow().pos();
1767 right = pos() == textRow().endpos();
1769 if ((!left && !right) ||
1770 (left && !right && xo < x_target_) ||
1771 (!left && right && x_target_ < xo))
1778 // first get the current line
1779 TextMetrics & tm = bv_->textMetrics(text());
1780 ParagraphMetrics const & pm = tm.parMetrics(pit());
1782 if (pos() && boundary())
1783 row = pm.pos2row(pos() - 1);
1785 row = pm.pos2row(pos());
1787 if (atFirstOrLastRow(up)) {
1788 // Is there a place for the cursor to go ? If yes, we
1789 // can execute the DEPM, otherwise we should keep the
1790 // paragraph to host the cursor.
1791 Cursor dummy = *this;
1792 bool valid_destination = false;
1793 for(; dummy.depth(); dummy.pop())
1794 if (!dummy.atFirstOrLastRow(up)) {
1795 valid_destination = true;
1799 // will a next dispatch follow and if there is a new
1800 // dispatch will it move the cursor out ?
1801 if (depth() > 1 && valid_destination) {
1802 // The cursor hasn't changed yet. This happens when
1803 // you e.g. move out of an inset. And to give the
1804 // DEPM the possibility of doing something we must
1805 // provide it with two different cursors. (Lgb, vfr)
1807 dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1808 dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1810 updateNeeded |= bv().checkDepm(dummy, *this);
1811 updateTextTargetOffset();
1816 // with and without selection are handled differently
1818 int yo = bv().getPos(*this, boundary()).y_;
1820 // To next/previous row
1822 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1824 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1827 // This happens when you move out of an inset.
1828 // And to give the DEPM the possibility of doing
1829 // something we must provide it with two different
1831 Cursor dummy = *this;
1834 if (bv().checkDepm(dummy, old)) {
1835 updateNeeded = true;
1836 // Make sure that cur gets back whatever happened to dummy(Lgb)
1840 // if there is a selection, we stay out of any inset, and just jump to the right position:
1844 top().pos() = min(tm.x2pos(pit(), row - 1, xo), top().lastpos());
1845 } else if (pit() > 0) {
1847 TextMetrics & tm = bv_->textMetrics(text());
1848 if (!tm.contains(pit()))
1849 tm.newParMetricsUp();
1850 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1851 top().pos() = min(tm.x2pos(pit(), pmcur.rows().size() - 1, xo), top().lastpos());
1854 if (row + 1 < int(pm.rows().size())) {
1855 top().pos() = min(tm.x2pos(pit(), row + 1, xo), top().lastpos());
1856 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1858 TextMetrics & tm = bv_->textMetrics(text());
1859 if (!tm.contains(pit()))
1860 tm.newParMetricsDown();
1861 top().pos() = min(tm.x2pos(pit(), 0, xo), top().lastpos());
1865 updateNeeded |= bv().checkDepm(*this, old);
1868 updateTextTargetOffset();
1873 void Cursor::handleFont(string const & font)
1875 LYXERR(Debug::DEBUG, font);
1879 safe = cap::grabAndEraseSelection(*this);
1884 if (lastpos() != 0) {
1885 // something left in the cell
1887 // cursor in first position
1889 } else if (pos() == lastpos()) {
1890 // cursor in last position
1893 // cursor in between. split cell
1894 MathData::iterator bt = cell().begin();
1895 MathAtom at = createInsetMath(from_utf8(font));
1896 at.nucleus()->cell(0) = MathData(bt, bt + pos());
1897 cell().erase(bt, bt + pos());
1902 // nothing left in the cell
1911 void Cursor::message(docstring const & msg) const
1913 theLyXFunc().setMessage(msg);
1917 void Cursor::errorMessage(docstring const & msg) const
1919 theLyXFunc().setErrorMessage(msg);
1923 docstring Cursor::selectionAsString(bool with_label) const
1928 int const label = with_label
1929 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
1932 idx_type const startidx = selBegin().idx();
1933 idx_type const endidx = selEnd().idx();
1934 if (startidx != endidx) {
1935 // multicell selection
1936 InsetTabular * table = inset().asInsetTabular();
1937 LASSERT(table, return docstring());
1938 return table->asString(startidx, endidx);
1941 ParagraphList const & pars = text()->paragraphs();
1943 pit_type const startpit = selBegin().pit();
1944 pit_type const endpit = selEnd().pit();
1945 size_t const startpos = selBegin().pos();
1946 size_t const endpos = selEnd().pos();
1948 if (startpit == endpit)
1949 return pars[startpit].asString(startpos, endpos, label);
1951 // First paragraph in selection
1952 docstring result = pars[startpit].
1953 asString(startpos, pars[startpit].size(), label)
1954 + parbreak(pars[startpit]);
1956 // The paragraphs in between (if any)
1957 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
1958 Paragraph const & par = pars[pit];
1959 result += par.asString(0, par.size(), label)
1960 + parbreak(pars[pit]);
1963 // Last paragraph in selection
1964 result += pars[endpit].asString(0, endpos, label);
1970 return cap::grabSelection(*this);
1976 docstring Cursor::currentState() const
1979 odocstringstream os;
1985 return text()->currentState(*this);
1991 docstring Cursor::getPossibleLabel() const
1993 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
1997 Encoding const * Cursor::getEncoding() const
2001 CursorSlice const & sl = innerTextSlice();
2002 Text const & text = *sl.text();
2003 Font font = text.getPar(sl.pit()).getFont(
2004 bv().buffer().params(), sl.pos(), outerFont(sl.pit(), text.paragraphs()));
2005 return font.language()->encoding();
2009 void Cursor::undispatched()
2011 disp_.dispatched(false);
2015 void Cursor::dispatched()
2017 disp_.dispatched(true);
2021 void Cursor::updateFlags(Update::flags f)
2027 void Cursor::noUpdate()
2029 disp_.update(Update::None);
2033 Font Cursor::getFont() const
2035 // The logic here should more or less match to the Cursor::setCurrentFont
2036 // logic, i.e. the cursor height should give a hint what will happen
2037 // if a character is entered.
2039 // HACK. far from being perfect...
2041 CursorSlice const & sl = innerTextSlice();
2042 Text const & text = *sl.text();
2043 Paragraph const & par = text.getPar(sl.pit());
2045 // on boundary, so we are really at the character before
2046 pos_type pos = sl.pos();
2047 if (pos > 0 && boundary())
2050 // on space? Take the font before (only for RTL boundary stay)
2052 TextMetrics const & tm = bv().textMetrics(&text);
2053 if (pos == sl.lastpos()
2054 || (par.isSeparator(pos)
2055 && !tm.isRTLBoundary(sl.pit(), pos)))
2059 // get font at the position
2060 Font font = par.getFont(buffer()->params(), pos,
2061 outerFont(sl.pit(), text.paragraphs()));
2067 bool Cursor::fixIfBroken()
2069 if (DocIterator::fixIfBroken()) {
2077 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2079 // find inset in common
2081 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2082 if (&old[i].inset() != &cur[i].inset())
2086 // update words if we just moved to another paragraph
2087 if (i == old.depth() && i == cur.depth()
2088 && !cur.buffer()->isClean()
2089 && cur.inTexted() && old.inTexted()
2090 && cur.pit() != old.pit()) {
2091 old.paragraph().updateWords();
2094 // notify everything on top of the common part in old cursor,
2095 // but stop if the inset claims the cursor to be invalid now
2096 for (size_type j = i; j < old.depth(); ++j) {
2097 Cursor insetPos = old;
2099 if (old[j].inset().notifyCursorLeaves(insetPos, cur))
2103 // notify everything on top of the common part in new cursor,
2104 // but stop if the inset claims the cursor to be invalid now
2105 for (; i < cur.depth(); ++i) {
2106 if (cur[i].inset().notifyCursorEnters(cur))
2114 void Cursor::setCurrentFont()
2116 CursorSlice const & cs = innerTextSlice();
2117 Paragraph const & par = cs.paragraph();
2118 pos_type cpit = cs.pit();
2119 pos_type cpos = cs.pos();
2120 Text const & ctext = *cs.text();
2121 TextMetrics const & tm = bv().textMetrics(&ctext);
2123 // are we behind previous char in fact? -> go to that char
2124 if (cpos > 0 && boundary())
2127 // find position to take the font from
2129 // paragraph end? -> font of last char
2130 if (cpos == lastpos())
2132 // on space? -> look at the words in front of space
2133 else if (cpos > 0 && par.isSeparator(cpos)) {
2134 // abc| def -> font of c
2135 // abc |[WERBEH], i.e. boundary==true -> font of c
2136 // abc [WERBEH]| def, font of the space
2137 if (!tm.isRTLBoundary(cpit, cpos))
2143 BufferParams const & bufparams = buffer()->params();
2144 current_font = par.getFontSettings(bufparams, cpos);
2145 real_current_font = tm.displayFont(cpit, cpos);
2147 // special case for paragraph end
2148 if (cs.pos() == lastpos()
2149 && tm.isRTLBoundary(cpit, cs.pos())
2151 Language const * lang = par.getParLanguage(bufparams);
2152 current_font.setLanguage(lang);
2153 current_font.fontInfo().setNumber(FONT_OFF);
2154 real_current_font.setLanguage(lang);
2155 real_current_font.fontInfo().setNumber(FONT_OFF);
2160 bool Cursor::textUndo()
2162 DocIterator dit = *this;
2163 // Undo::textUndo() will modify dit.
2164 if (!buffer()->undo().textUndo(dit))
2174 bool Cursor::textRedo()
2176 DocIterator dit = *this;
2177 // Undo::textRedo() will modify dit.
2178 if (!buffer()->undo().textRedo(dit))
2188 void Cursor::finishUndo() const
2190 buffer()->undo().finishUndo();
2194 void Cursor::beginUndoGroup() const
2196 buffer()->undo().beginUndoGroup();
2200 void Cursor::endUndoGroup() const
2202 buffer()->undo().endUndoGroup();
2206 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2208 buffer()->undo().recordUndo(*this, kind, from, to);
2212 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2214 buffer()->undo().recordUndo(*this, kind, from);
2218 void Cursor::recordUndo(UndoKind kind) const
2220 buffer()->undo().recordUndo(*this, kind);
2224 void Cursor::recordUndoInset(UndoKind kind) const
2226 buffer()->undo().recordUndoInset(*this, kind);
2230 void Cursor::recordUndoFullDocument() const
2232 buffer()->undo().recordUndoFullDocument(*this);
2236 void Cursor::recordUndoSelection() const
2239 if (cap::multipleCellsSelected(*this))
2244 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2245 selBegin().pit(), selEnd().pit());
2250 void Cursor::checkBufferStructure()
2252 Buffer const * master = buffer()->masterBuffer();
2253 master->tocBackend().updateItem(*this);
2254 if (master != buffer() && !master->hasGuiDelegate())
2255 // In case the master has no gui associated with it,
2256 // the TocItem is not updated (part of bug 5699).
2257 buffer()->tocBackend().updateItem(*this);