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 Inset
& inset
= bv
.buffer().inset();
209 DocIterator it
= doc_iterator_begin(inset
);
211 DocIterator et
= doc_iterator_end(inset
);
213 double best_dist
= numeric_limits
<double>::max();
214 DocIterator best_cursor
= et
;
216 for ( ; it
!= et
; it
.forwardPos()) {
217 // avoid invalid nesting when selecting
218 if (bv
.cursorStatus(it
) == CUR_INSIDE
219 && (!cur
.selection() || positionable(it
, cur
.anchor_
))) {
220 Point p
= bv
.getPos(it
, false);
223 if (xlow
<= xo
&& xo
<= xhigh
&& ylow
<= yo
&& yo
<= yhigh
) {
224 double const dx
= xo
- x
;
225 double const dy
= yo
- y
;
226 double const d
= dx
* dx
+ dy
* dy
;
227 //lyxerr << "itx: " << xo << " ity: " << yo << " d: " << d
228 // << " dx: " << dx << " dy: " << dy
229 // << " idx: " << it.idx() << " pos: " << it.pos()
232 // '<=' in order to take the last possible position
233 // this is important for clicking behind \sum in e.g. '\sum_i a'
234 if (d
<= best_dist
) {
235 //lyxerr << "*" << endl;
243 //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
244 if (best_cursor
== et
)
246 cur
.setCursor(best_cursor
);
250 docstring
parbreak(Paragraph
const & par
)
254 // only add blank line if we're not in an ERT or Listings inset
255 if (par
.ownerCode() != ERT_CODE
256 && par
.ownerCode() != LISTINGS_CODE
)
264 // be careful: this is called from the bv's constructor, too, so
265 // bv functions are not yet available!
266 Cursor::Cursor(BufferView
& bv
)
267 : DocIterator(), bv_(&bv
), anchor_(), x_target_(-1), textTargetOffset_(0),
268 selection_(false), mark_(false), logicalpos_(false),
269 current_font(inherit_font
)
273 void Cursor::reset(Inset
& inset
)
276 push_back(CursorSlice(inset
));
277 anchor_
= doc_iterator_begin(inset
);
285 // this (intentionally) does neither touch anchor nor selection status
286 void Cursor::setCursor(DocIterator
const & cur
)
288 DocIterator::operator=(cur
);
292 void Cursor::dispatch(FuncRequest
const & cmd0
)
294 LYXERR(Debug::DEBUG
, "cmd: " << cmd0
<< '\n' << *this);
299 FuncRequest cmd
= cmd0
;
302 // store some values to be used inside of the handlers
303 beforeDispatchCursor_
= *this;
304 for (; depth(); pop(), boundary(false)) {
305 LYXERR(Debug::DEBUG
, "Cursor::dispatch: cmd: "
306 << cmd0
<< endl
<< *this);
307 LASSERT(pos() <= lastpos(), /**/);
308 LASSERT(idx() <= lastidx(), /**/);
309 LASSERT(pit() <= lastpit(), /**/);
311 // The common case is 'LFUN handled, need update', so make the
312 // LFUN handler's life easier by assuming this as default value.
313 // The handler can reset the update and val flags if necessary.
314 disp_
.update(Update::FitCursor
| Update::Force
);
315 disp_
.dispatched(true);
316 inset().dispatch(*this, cmd
);
317 if (disp_
.dispatched())
321 // it completely to get a 'bomb early' behaviour in case this
322 // object will be used again.
323 if (!disp_
.dispatched()) {
324 LYXERR(Debug::DEBUG
, "RESTORING OLD CURSOR!");
326 disp_
.update(Update::None
);
327 disp_
.dispatched(false);
329 // restore the previous one because nested Cursor::dispatch calls
330 // are possible which would change it
331 beforeDispatchCursor_
= safe
.beforeDispatchCursor_
;
336 DispatchResult
Cursor::result() const
342 BufferView
& Cursor::bv() const
349 Buffer
& Cursor::buffer() const
352 return bv_
->buffer();
358 LASSERT(depth() >= 1, /**/);
363 void Cursor::push(Inset
& p
)
365 push_back(CursorSlice(p
));
366 p
.setBuffer(bv_
->buffer());
370 void Cursor::pushBackward(Inset
& p
)
372 LASSERT(!empty(), /**/);
373 //lyxerr << "Entering inset " << t << " front" << endl;
379 bool Cursor::popBackward()
381 LASSERT(!empty(), /**/);
389 bool Cursor::popForward()
391 LASSERT(!empty(), /**/);
392 //lyxerr << "Leaving inset from in back" << endl;
393 const pos_type lp
= (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
397 pos() += lastpos() - lp
+ 1;
402 int Cursor::currentMode()
404 LASSERT(!empty(), /**/);
405 for (int i
= depth() - 1; i
>= 0; --i
) {
406 int res
= operator[](i
).inset().currentMode();
407 if (res
!= Inset::UNDECIDED_MODE
)
410 return Inset::TEXT_MODE
;
414 void Cursor::getPos(int & x
, int & y
) const
416 Point p
= bv().getPos(*this, boundary());
422 Row
const & Cursor::textRow() const
424 CursorSlice
const & cs
= innerTextSlice();
425 ParagraphMetrics
const & pm
= bv().parMetrics(cs
.text(), cs
.pit());
426 LASSERT(!pm
.rows().empty(), /**/);
427 return pm
.getRow(pos(), boundary());
431 void Cursor::resetAnchor()
438 bool Cursor::posBackward()
447 bool Cursor::posForward()
449 if (pos() == lastpos())
456 bool Cursor::posVisRight(bool skip_inset
)
458 Cursor new_cur
= *this; // where we will move to
459 pos_type left_pos
; // position visually left of current cursor
460 pos_type right_pos
; // position visually right of current cursor
461 bool new_pos_is_RTL
; // is new position we're moving to RTL?
463 getSurroundingPos(left_pos
, right_pos
);
465 LYXERR(Debug::RTL
, left_pos
<<"|"<< right_pos
<< " (pos: "<< pos() <<")");
467 // Are we at an inset?
468 new_cur
.pos() = right_pos
;
469 new_cur
.boundary(false);
471 text()->checkAndActivateInsetVisual(new_cur
, right_pos
>= pos(), false)) {
472 // we actually move the cursor at the end of this function, for now
473 // we just keep track of the new position in new_cur...
474 LYXERR(Debug::RTL
, "entering inset at: " << new_cur
.pos());
477 // Are we already at rightmost pos in row?
478 else if (text()->empty() || right_pos
== -1) {
481 if (!new_cur
.posVisToNewRow(false)) {
482 LYXERR(Debug::RTL
, "not moving!");
486 // we actually move the cursor at the end of this function, for now
487 // just keep track of the new position in new_cur...
488 LYXERR(Debug::RTL
, "right edge, moving: " << int(new_cur
.pit()) << ","
489 << int(new_cur
.pos()) << "," << (new_cur
.boundary() ? 1 : 0));
492 // normal movement to the right
495 // Recall, if the cursor is at position 'x', that means *before*
496 // the character at position 'x'. In RTL, "before" means "to the
497 // right of", in LTR, "to the left of". So currently our situation
498 // is this: the position to our right is 'right_pos' (i.e., we're
499 // currently to the left of 'right_pos'). In order to move to the
500 // right, it depends whether or not the character at 'right_pos' is RTL.
501 new_pos_is_RTL
= paragraph().getFontSettings(
502 bv().buffer().params(), right_pos
).isVisibleRightToLeft();
503 // If the character at 'right_pos' *is* LTR, then in order to move to
504 // the right of it, we need to be *after* 'right_pos', i.e., move to
505 // position 'right_pos' + 1.
506 if (!new_pos_is_RTL
) {
507 new_cur
.pos() = right_pos
+ 1;
508 // set the boundary to true in two situations:
510 // 1. if new_pos is now lastpos (which means that we're moving
511 // right to the end of an LTR chunk which is at the end of an
513 new_cur
.pos() == lastpos()
514 // 2. if the position *after* right_pos is RTL (we want to be
515 // *after* right_pos, not before right_pos + 1!)
516 || paragraph().getFontSettings(bv().buffer().params(),
517 new_cur
.pos()).isVisibleRightToLeft()
519 new_cur
.boundary(true);
520 else // set the boundary to false
521 new_cur
.boundary(false);
523 // Otherwise (if the character at position 'right_pos' is RTL), then
524 // moving to the right of it is as easy as setting the new position
527 new_cur
.pos() = right_pos
;
528 new_cur
.boundary(false);
533 bool moved
= (new_cur
.pos() != pos()
534 || new_cur
.pit() != pit()
535 || new_cur
.boundary() != boundary());
538 LYXERR(Debug::RTL
, "moving to: " << new_cur
.pos()
539 << (new_cur
.boundary() ? " (boundary)" : ""));
547 bool Cursor::posVisLeft(bool skip_inset
)
549 Cursor new_cur
= *this; // where we will move to
550 pos_type left_pos
; // position visually left of current cursor
551 pos_type right_pos
; // position visually right of current cursor
552 bool new_pos_is_RTL
; // is new position we're moving to RTL?
554 getSurroundingPos(left_pos
, right_pos
);
556 LYXERR(Debug::RTL
, left_pos
<<"|"<< right_pos
<< " (pos: "<< pos() <<")");
558 // Are we at an inset?
559 new_cur
.pos() = left_pos
;
560 new_cur
.boundary(false);
562 text()->checkAndActivateInsetVisual(new_cur
, left_pos
>= pos(), true)) {
563 // we actually move the cursor at the end of this function, for now
564 // we just keep track of the new position in new_cur...
565 LYXERR(Debug::RTL
, "entering inset at: " << new_cur
.pos());
568 // Are we already at leftmost pos in row?
569 else if (text()->empty() || left_pos
== -1) {
572 if (!new_cur
.posVisToNewRow(true)) {
573 LYXERR(Debug::RTL
, "not moving!");
577 // we actually move the cursor at the end of this function, for now
578 // just keep track of the new position in new_cur...
579 LYXERR(Debug::RTL
, "left edge, moving: " << int(new_cur
.pit()) << ","
580 << int(new_cur
.pos()) << "," << (new_cur
.boundary() ? 1 : 0));
583 // normal movement to the left
586 // Recall, if the cursor is at position 'x', that means *before*
587 // the character at position 'x'. In RTL, "before" means "to the
588 // right of", in LTR, "to the left of". So currently our situation
589 // is this: the position to our left is 'left_pos' (i.e., we're
590 // currently to the right of 'left_pos'). In order to move to the
591 // left, it depends whether or not the character at 'left_pos' is RTL.
592 new_pos_is_RTL
= paragraph().getFontSettings(
593 bv().buffer().params(), left_pos
).isVisibleRightToLeft();
594 // If the character at 'left_pos' *is* RTL, then in order to move to
595 // the left of it, we need to be *after* 'left_pos', i.e., move to
596 // position 'left_pos' + 1.
597 if (new_pos_is_RTL
) {
598 new_cur
.pos() = left_pos
+ 1;
599 // set the boundary to true in two situations:
601 // 1. if new_pos is now lastpos (which means that we're moving left
602 // to the end of an RTL chunk which is at the end of an LTR
604 new_cur
.pos() == lastpos()
605 // 2. if the position *after* left_pos is not RTL (we want to be
606 // *after* left_pos, not before left_pos + 1!)
607 || !paragraph().getFontSettings(bv().buffer().params(),
608 new_cur
.pos()).isVisibleRightToLeft()
610 new_cur
.boundary(true);
611 else // set the boundary to false
612 new_cur
.boundary(false);
614 // Otherwise (if the character at position 'left_pos' is LTR), then
615 // moving to the left of it is as easy as setting the new position
618 new_cur
.pos() = left_pos
;
619 new_cur
.boundary(false);
624 bool moved
= (new_cur
.pos() != pos()
625 || new_cur
.pit() != pit()
626 || new_cur
.boundary() != boundary());
629 LYXERR(Debug::RTL
, "moving to: " << new_cur
.pos()
630 << (new_cur
.boundary() ? " (boundary)" : ""));
638 void Cursor::getSurroundingPos(pos_type
& left_pos
, pos_type
& right_pos
)
640 // preparing bidi tables
641 Paragraph
const & par
= paragraph();
642 Buffer
const & buf
= buffer();
643 Row
const & row
= textRow();
645 bidi
.computeTables(par
, buf
, row
);
647 LYXERR(Debug::RTL
, "bidi: " << row
.pos() << "--" << row
.endpos());
649 // The cursor is painted *before* the character at pos(), or, if 'boundary'
650 // is true, *after* the character at (pos() - 1). So we already have one
651 // known position around the cursor:
652 pos_type known_pos
= boundary() ? pos() - 1 : pos();
654 // edge case: if we're at the end of the paragraph, things are a little
655 // different (because lastpos is a position which does not really "exist"
656 // --- there's no character there yet).
657 if (known_pos
== lastpos()) {
658 if (par
.isRTL(buf
.params())) {
660 right_pos
= bidi
.vis2log(row
.pos());
662 else { // LTR paragraph
664 left_pos
= bidi
.vis2log(row
.endpos() - 1);
669 // Whether 'known_pos' is to the left or to the right of the cursor depends
670 // on whether it is an RTL or LTR character...
671 bool const cur_is_RTL
=
672 par
.getFontSettings(buf
.params(), known_pos
).isVisibleRightToLeft();
673 // ... in the following manner:
674 // For an RTL character, "before" means "to the right" and "after" means
675 // "to the left"; and for LTR, it's the reverse. So, 'known_pos' is to the
676 // right of the cursor if (RTL && boundary) or (!RTL && !boundary):
677 bool known_pos_on_right
= (cur_is_RTL
== boundary());
679 // So we now know one of the positions surrounding the cursor. Let's
680 // determine the other one:
682 if (known_pos_on_right
) {
683 right_pos
= known_pos
;
684 // *visual* position of 'left_pos':
685 pos_type v_left_pos
= bidi
.log2vis(right_pos
) - 1;
686 // If the position we just identified as 'left_pos' is a "skipped
687 // separator" (a separator which is at the logical end of a row,
688 // except for the last row in a paragraph; such separators are not
689 // painted, so they "are not really there"; note that in bidi text,
690 // such a separator could appear visually in the middle of a row),
691 // set 'left_pos' to the *next* position to the left.
692 if (bidi
.inRange(v_left_pos
)
693 && bidi
.vis2log(v_left_pos
) + 1 == row
.endpos()
694 && row
.endpos() < lastpos()
695 && par
.isSeparator(bidi
.vis2log(v_left_pos
))) {
698 // calculate the logical position of 'left_pos', if in row
699 if (!bidi
.inRange(v_left_pos
))
702 left_pos
= bidi
.vis2log(v_left_pos
);
703 // If the position we identified as 'right_pos' is a "skipped
704 // separator", set 'right_pos' to the *next* position to the right.
705 if (right_pos
+ 1 == row
.endpos() && row
.endpos() < lastpos()
706 && par
.isSeparator(right_pos
)) {
707 pos_type v_right_pos
= bidi
.log2vis(right_pos
) + 1;
708 if (!bidi
.inRange(v_right_pos
))
711 right_pos
= bidi
.vis2log(v_right_pos
);
714 else { // known_pos is on the left
715 left_pos
= known_pos
;
716 // *visual* position of 'right_pos'
717 pos_type v_right_pos
= bidi
.log2vis(left_pos
) + 1;
718 // If the position we just identified as 'right_pos' is a "skipped
719 // separator", set 'right_pos' to the *next* position to the right.
720 if (bidi
.inRange(v_right_pos
)
721 && bidi
.vis2log(v_right_pos
) + 1 == row
.endpos()
722 && row
.endpos() < lastpos()
723 && par
.isSeparator(bidi
.vis2log(v_right_pos
))) {
726 // calculate the logical position of 'right_pos', if in row
727 if (!bidi
.inRange(v_right_pos
))
730 right_pos
= bidi
.vis2log(v_right_pos
);
731 // If the position we identified as 'left_pos' is a "skipped
732 // separator", set 'left_pos' to the *next* position to the left.
733 if (left_pos
+ 1 == row
.endpos() && row
.endpos() < lastpos()
734 && par
.isSeparator(left_pos
)) {
735 pos_type v_left_pos
= bidi
.log2vis(left_pos
) - 1;
736 if (!bidi
.inRange(v_left_pos
))
739 left_pos
= bidi
.vis2log(v_left_pos
);
746 bool Cursor::posVisToNewRow(bool movingLeft
)
748 Paragraph
const & par
= paragraph();
749 Buffer
const & buf
= buffer();
750 Row
const & row
= textRow();
751 bool par_is_LTR
= !par
.isRTL(buf
.params());
753 // Inside a table, determining whether to move to the next or previous row
754 // should be done based on the table's direction.
756 if (s
>= 1 && (*this)[s
].inset().asInsetTabular()) {
757 par_is_LTR
= !(*this)[s
].inset().asInsetTabular()->isRightToLeft(*this);
758 LYXERR(Debug::RTL
, "Inside table! par_is_LTR=" << (par_is_LTR
? 1 : 0));
761 // if moving left in an LTR paragraph or moving right in an RTL one,
762 // move to previous row
763 if (par_is_LTR
== movingLeft
) {
764 if (row
.pos() == 0) { // we're at first row in paragraph
765 if (pit() == 0) // no previous paragraph! don't move
767 // move to last pos in previous par
771 } else { // move to previous row in this par
772 pos() = row
.pos() - 1; // this is guaranteed to be in previous row
776 // if moving left in an RTL paragraph or moving right in an LTR one,
779 if (row
.endpos() == lastpos()) { // we're at last row in paragraph
780 if (pit() == lastpit()) // last paragraph! don't move
782 // move to first row in next par
786 } else { // move to next row in this par
787 pos() = row
.endpos();
792 // make sure we're at left-/right-most pos in new row
793 posVisToRowExtremity(!movingLeft
);
799 void Cursor::posVisToRowExtremity(bool left
)
801 // prepare bidi tables
802 Paragraph
const & par
= paragraph();
803 Buffer
const & buf
= buffer();
804 Row
const & row
= textRow();
806 bidi
.computeTables(par
, buf
, row
);
808 LYXERR(Debug::RTL
, "entering extremity: " << pit() << "," << pos() << ","
809 << (boundary() ? 1 : 0));
811 if (left
) { // move to leftmost position
812 // if this is an RTL paragraph, and we're at the last row in the
813 // paragraph, move to lastpos
814 if (par
.isRTL(buf
.params()) && row
.endpos() == lastpos())
817 pos() = bidi
.vis2log(row
.pos());
819 // Moving to the leftmost position in the row, the cursor should
820 // normally be placed to the *left* of the leftmost position.
821 // A very common exception, though, is if the leftmost character
822 // also happens to be the separator at the (logical) end of the row
823 // --- in this case, the separator is positioned beyond the left
824 // margin, and we don't want to move the cursor there (moving to
825 // the left of the separator is equivalent to moving to the next
826 // line). So, in this case we actually want to place the cursor
827 // to the *right* of the leftmost position (the separator).
828 // Another exception is if we're moving to the logically last
829 // position in the row, which is *not* a separator: this means
830 // that the entire row has no separators (if there were any, the
831 // row would have been broken there); and therefore in this case
832 // we also move to the *right* of the last position (this indicates
833 // to the user that there is no space after this position, and is
834 // consistent with the behavior in the middle of a row --- moving
835 // right or left moves to the next/previous character; if we were
836 // to move to the *left* of this position, that would simulate
837 // a separator which is not really there!).
838 // Finally, there is an exception to the previous exception: if
839 // this non-separator-but-last-position-in-row is an inset, then
840 // we *do* want to stay to the left of it anyway: this is the
841 // "boundary" which we simulate at insets.
843 bool right_of_pos
= false; // do we want to be to the right of pos?
845 // as explained above, if at last pos in row, stay to the right
846 if ((pos() == row
.endpos() - 1) && !par
.isInset(pos()))
849 // Now we know if we want to be to the left or to the right of pos,
850 // let's make sure we are where we want to be.
851 bool new_pos_is_RTL
=
852 par
.getFontSettings(buf
.params(), pos()).isVisibleRightToLeft();
854 if (new_pos_is_RTL
== !right_of_pos
) {
861 else { // move to rightmost position
862 // if this is an LTR paragraph, and we're at the last row in the
863 // paragraph, move to lastpos
864 if (!par
.isRTL(buf
.params()) && row
.endpos() == lastpos())
867 pos() = bidi
.vis2log(row
.endpos() - 1);
869 // Moving to the rightmost position in the row, the cursor should
870 // normally be placed to the *right* of the rightmost position.
871 // A very common exception, though, is if the rightmost character
872 // also happens to be the separator at the (logical) end of the row
873 // --- in this case, the separator is positioned beyond the right
874 // margin, and we don't want to move the cursor there (moving to
875 // the right of the separator is equivalent to moving to the next
876 // line). So, in this case we actually want to place the cursor
877 // to the *left* of the rightmost position (the separator).
878 // Another exception is if we're moving to the logically last
879 // position in the row, which is *not* a separator: this means
880 // that the entire row has no separators (if there were any, the
881 // row would have been broken there); and therefore in this case
882 // we also move to the *left* of the last position (this indicates
883 // to the user that there is no space after this position, and is
884 // consistent with the behavior in the middle of a row --- moving
885 // right or left moves to the next/previous character; if we were
886 // to move to the *right* of this position, that would simulate
887 // a separator which is not really there!).
888 // Finally, there is an exception to the previous exception: if
889 // this non-separator-but-last-position-in-row is an inset, then
890 // we *do* want to stay to the right of it anyway: this is the
891 // "boundary" which we simulate at insets.
893 bool left_of_pos
= false; // do we want to be to the left of pos?
895 // as explained above, if at last pos in row, stay to the left
896 if ((pos() == row
.endpos() - 1) && !par
.isInset(pos()))
899 // Now we know if we want to be to the left or to the right of pos,
900 // let's make sure we are where we want to be.
901 bool new_pos_is_RTL
=
902 par
.getFontSettings(buf
.params(), pos()).isVisibleRightToLeft();
904 if (new_pos_is_RTL
== left_of_pos
) {
910 LYXERR(Debug::RTL
, "leaving extremity: " << pit() << "," << pos() << ","
911 << (boundary() ? 1 : 0));
915 CursorSlice
Cursor::anchor() const
917 LASSERT(anchor_
.depth() >= depth(), /**/);
918 CursorSlice normal
= anchor_
[depth() - 1];
919 if (depth() < anchor_
.depth() && top() <= normal
) {
920 // anchor is behind cursor -> move anchor behind the inset
927 CursorSlice
Cursor::selBegin() const
931 return anchor() < top() ? anchor() : top();
935 CursorSlice
Cursor::selEnd() const
939 return anchor() > top() ? anchor() : top();
943 DocIterator
Cursor::selectionBegin() const
949 // FIXME: This is a work-around for the problem that
950 // CursorSlice doesn't keep track of the boundary.
951 if (anchor() == top())
952 di
= anchor_
.boundary() > boundary() ? anchor_
: *this;
954 di
= anchor() < top() ? anchor_
: *this;
960 DocIterator
Cursor::selectionEnd() const
966 // FIXME: This is a work-around for the problem that
967 // CursorSlice doesn't keep track of the boundary.
968 if (anchor() == top())
969 di
= anchor_
.boundary() < boundary() ? anchor_
: *this;
971 di
= anchor() > top() ? anchor_
: *this;
973 if (di
.depth() > depth()) {
981 void Cursor::setSelection()
984 // A selection with no contents is not a selection
985 // FIXME: doesnt look ok
986 if (idx() == anchor().idx() &&
987 pit() == anchor().pit() &&
988 pos() == anchor().pos())
993 void Cursor::setSelection(DocIterator
const & where
, int n
)
1002 void Cursor::clearSelection()
1004 setSelection(false);
1010 void Cursor::setTargetX(int x
)
1013 textTargetOffset_
= 0;
1017 int Cursor::x_target() const
1023 void Cursor::clearTargetX()
1026 textTargetOffset_
= 0;
1030 void Cursor::updateTextTargetOffset()
1035 textTargetOffset_
= x
- x_target_
;
1039 void Cursor::info(odocstream
& os
) const
1041 for (int i
= 1, n
= depth(); i
< n
; ++i
) {
1042 operator[](i
).inset().infoize(os
);
1046 Inset
const * inset
= prevInset();
1047 // prevInset() can return 0 in certain case.
1049 prevInset()->infoize2(os
);
1051 // overwite old message
1056 bool Cursor::selHandle(bool sel
)
1058 //lyxerr << "Cursor::selHandle" << endl;
1061 if (sel
== selection())
1065 cap::saveSelection(*this);
1073 ostream
& operator<<(ostream
& os
, Cursor
const & cur
)
1075 os
<< "\n cursor: | anchor:\n";
1076 for (size_t i
= 0, n
= cur
.depth(); i
!= n
; ++i
) {
1077 os
<< " " << cur
[i
] << " | ";
1078 if (i
< cur
.anchor_
.depth())
1079 os
<< cur
.anchor_
[i
];
1081 os
<< "-------------------------------";
1084 for (size_t i
= cur
.depth(), n
= cur
.anchor_
.depth(); i
< n
; ++i
) {
1085 os
<< "------------------------------- | " << cur
.anchor_
[i
] << "\n";
1087 os
<< " selection: " << cur
.selection_
1088 << " x_target: " << cur
.x_target_
<< endl
;
1093 LyXErr
& operator<<(LyXErr
& os
, Cursor
const & cur
)
1103 ///////////////////////////////////////////////////////////////////
1106 // The part below is the non-integrated rest of the original math
1107 // cursor. This should be either generalized for texted or moved
1108 // back to mathed (in most cases to InsetMathNest).
1110 ///////////////////////////////////////////////////////////////////
1112 #include "mathed/InsetMathChar.h"
1113 #include "mathed/InsetMathGrid.h"
1114 #include "mathed/InsetMathScript.h"
1115 #include "mathed/InsetMathUnknown.h"
1116 #include "mathed/MathFactory.h"
1117 #include "mathed/MathStream.h"
1118 #include "mathed/MathSupport.h"
1123 //#define FILEDEBUG 1
1126 bool Cursor::isInside(Inset
const * p
) const
1128 for (size_t i
= 0; i
!= depth(); ++i
)
1129 if (&operator[](i
).inset() == p
)
1135 void Cursor::leaveInset(Inset
const & inset
)
1137 for (size_t i
= 0; i
!= depth(); ++i
) {
1138 if (&operator[](i
).inset() == &inset
) {
1146 bool Cursor::openable(MathAtom
const & t
) const
1157 // we can't move into anything new during selection
1158 if (depth() >= anchor_
.depth())
1160 if (t
.nucleus() != &anchor_
[depth()].inset())
1167 void Cursor::setScreenPos(int x
, int /*y*/)
1170 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1175 void Cursor::plainErase()
1177 cell().erase(pos());
1181 void Cursor::markInsert()
1183 insert(char_type(0));
1187 void Cursor::markErase()
1189 cell().erase(pos());
1193 void Cursor::plainInsert(MathAtom
const & t
)
1195 cell().insert(pos(), t
);
1197 inset().setBuffer(bv_
->buffer());
1202 void Cursor::insert(docstring
const & str
)
1204 for_each(str
.begin(), str
.end(),
1205 boost::bind(static_cast<void(Cursor::*)(char_type
)>
1206 (&Cursor::insert
), this, _1
));
1210 void Cursor::insert(char_type c
)
1212 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1213 LASSERT(!empty(), /**/);
1215 cap::selClearOrDel(*this);
1216 insert(new InsetMathChar(c
));
1218 text()->insertChar(*this, c
);
1223 void Cursor::insert(MathAtom
const & t
)
1225 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1227 cap::selClearOrDel(*this);
1232 void Cursor::insert(Inset
* inset0
)
1234 LASSERT(inset0
, /**/);
1236 insert(MathAtom(inset0
));
1238 text()->insertInset(*this, inset0
);
1239 inset0
->setBuffer(bv_
->buffer());
1245 void Cursor::niceInsert(docstring
const & t
)
1256 void Cursor::niceInsert(MathAtom
const & t
)
1259 docstring
const safe
= cap::grabAndEraseSelection(*this);
1261 // enter the new inset and move the contents of the selection if possible
1262 if (t
->isActive()) {
1264 // be careful here: don't use 'pushBackward(t)' as this we need to
1265 // push the clone, not the original
1266 pushBackward(*nextInset());
1267 // We may not use niceInsert here (recursion)
1275 void Cursor::insert(MathData
const & ar
)
1279 cap::eraseSelection(*this);
1280 cell().insert(pos(), ar
);
1285 bool Cursor::backspace()
1287 autocorrect() = false;
1290 cap::eraseSelection(*this);
1295 // If empty cell, and not part of a big cell
1296 if (lastpos() == 0 && inset().nargs() == 1) {
1298 // Directly delete empty cell: [|[]] => [|]
1304 // [|], can not delete from inside
1315 if (inMacroMode()) {
1316 InsetMathUnknown
* p
= activeMacro();
1317 if (p
->name().size() > 1) {
1318 p
->setName(p
->name().substr(0, p
->name().size() - 1));
1323 if (pos() != 0 && prevAtom()->nargs() > 0) {
1324 // let's require two backspaces for 'big stuff' and
1325 // highlight on the first
1337 bool Cursor::erase()
1339 autocorrect() = false;
1344 cap::eraseSelection(*this);
1348 // delete empty cells if possible
1349 if (pos() == lastpos() && inset().idxDelete(idx()))
1352 // special behaviour when in last position of cell
1353 if (pos() == lastpos()) {
1354 bool one_cell
= inset().nargs() == 1;
1355 if (one_cell
&& lastpos() == 0) {
1357 // Directly delete empty cell: [|[]] => [|]
1363 // [|], can not delete from inside
1368 inset().idxGlue(idx());
1372 // 'clever' UI hack: only erase large items if previously slected
1373 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1388 DocIterator save
= *this;
1389 FuncRequest
cmd(selection() ? LFUN_UP_SELECT
: LFUN_UP
, docstring());
1390 this->dispatch(cmd
);
1391 if (disp_
.dispatched())
1394 autocorrect() = false;
1402 DocIterator save
= *this;
1403 FuncRequest
cmd(selection() ? LFUN_DOWN_SELECT
: LFUN_DOWN
, docstring());
1404 this->dispatch(cmd
);
1405 if (disp_
.dispatched())
1408 autocorrect() = false;
1413 bool Cursor::macroModeClose()
1417 InsetMathUnknown
* p
= activeMacro();
1420 asArray(p
->selection(), selection
);
1421 docstring
const s
= p
->name();
1423 cell().erase(pos());
1425 // do nothing if the macro name is empty
1429 // trigger updates of macros, at least, if no full
1430 // updates take place anyway
1431 updateFlags(Update::Force
);
1433 docstring
const name
= s
.substr(1);
1434 InsetMathNest
* const in
= inset().asInsetMath()->asNestInset();
1435 if (in
&& in
->interpretString(*this, s
))
1437 MathAtom atom
= createInsetMath(name
);
1439 // try to put argument into macro, if we just inserted a macro
1440 bool macroArg
= false;
1441 MathMacro
* atomAsMacro
= atom
.nucleus()->asMacro();
1443 // macros here are still unfolded (in init mode in fact). So
1444 // we have to resolve the macro here manually and check its arity
1445 // to put the selection behind it if arity > 0.
1446 MacroData
const * data
= buffer().getMacro(atomAsMacro
->name());
1447 if (selection
.size() > 0 && data
&& data
->numargs() - data
->optionals() > 0) {
1449 atomAsMacro
->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT
, 1);
1451 // non-greedy case. Do not touch the arguments behind
1452 atomAsMacro
->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT
, 0);
1455 // insert remembered selection into first argument of a non-macro
1456 else if (atom
.nucleus()->nargs() > 0)
1457 atom
.nucleus()->cell(0).append(selection
);
1461 // finally put the macro argument behind, if needed
1463 if (selection
.size() > 1)
1464 plainInsert(MathAtom(new InsetMathBrace(selection
)));
1473 docstring
Cursor::macroName()
1475 return inMacroMode() ? activeMacro()->name() : docstring();
1479 void Cursor::handleNest(MathAtom
const & a
, int c
)
1481 //lyxerr << "Cursor::handleNest: " << c << endl;
1483 asArray(cap::grabAndEraseSelection(*this), t
.nucleus()->cell(c
));
1486 pushBackward(*nextInset());
1490 int Cursor::targetX() const
1492 if (x_target() != -1)
1501 int Cursor::textTargetOffset() const
1503 return textTargetOffset_
;
1507 void Cursor::setTargetX()
1516 bool Cursor::inMacroMode() const
1522 InsetMathUnknown
const * p
= prevAtom()->asUnknownInset();
1523 return p
&& !p
->final();
1527 InsetMathUnknown
* Cursor::activeMacro()
1529 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1533 InsetMathUnknown
const * Cursor::activeMacro() const
1535 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1539 void Cursor::pullArg()
1542 MathData ar
= cell();
1543 if (popBackward() && inMathed()) {
1545 cell().insert(pos(), ar
);
1548 //formula()->mutateToText();
1553 void Cursor::touch()
1557 DocIterator::const_iterator it
= begin();
1558 DocIterator::const_iterator et
= end();
1559 for ( ; it
!= et
; ++it
)
1565 void Cursor::normalize()
1567 if (idx() > lastidx()) {
1568 lyxerr
<< "this should not really happen - 1: "
1569 << idx() << ' ' << nargs()
1570 << " in: " << &inset() << endl
;
1574 if (pos() > lastpos()) {
1575 lyxerr
<< "this should not really happen - 2: "
1576 << pos() << ' ' << lastpos() << " in idx: " << idx()
1578 odocstringstream os
;
1579 WriteStream
wi(os
, false, true, false);
1580 inset().asInsetMath()->write(wi
);
1581 lyxerr
<< to_utf8(os
.str()) << endl
;
1587 bool Cursor::upDownInMath(bool up
)
1589 // Be warned: The 'logic' implemented in this function is highly
1590 // fragile. A distance of one pixel or a '<' vs '<=' _really
1591 // matters. So fiddle around with it only if you think you know
1592 // what you are doing!
1596 xo
= theLyXFunc().cursorBeforeDispatchX();
1598 // check if we had something else in mind, if not, this is the future
1600 if (x_target_
== -1)
1602 else if (inset().asInsetText() && xo
- textTargetOffset() != x_target()) {
1603 // In text mode inside the line (not left or right) possibly set a new target_x,
1604 // but only if we are somewhere else than the previous target-offset.
1606 // We want to keep the x-target on subsequent up/down movements
1607 // that cross beyond the end of short lines. Thus a special
1608 // handling when the cursor is at the end of line: Use the new
1609 // x-target only if the old one was before the end of line
1610 // or the old one was after the beginning of the line
1611 bool inRTL
= isWithinRtlParagraph(*this);
1615 left
= pos() == textRow().endpos();
1616 right
= pos() == textRow().pos();
1618 left
= pos() == textRow().pos();
1619 right
= pos() == textRow().endpos();
1621 if ((!left
&& !right
) ||
1622 (left
&& !right
&& xo
< x_target_
) ||
1623 (!left
&& right
&& x_target_
< xo
))
1630 // try neigbouring script insets
1632 if (inMathed() && !selection()) {
1635 InsetMathScript
const * p
= prevAtom()->asScriptInset();
1636 if (p
&& p
->has(up
)) {
1638 push(*const_cast<InsetMathScript
*>(p
));
1639 idx() = p
->idxOfScript(up
);
1642 // we went in the right direction? Otherwise don't jump into the script
1646 int oy
= theLyXFunc().cursorBeforeDispatchY();
1647 if ((!up
&& y
<= oy
) ||
1656 if (pos() != lastpos()) {
1657 InsetMathScript
const * p
= nextAtom()->asScriptInset();
1658 if (p
&& p
->has(up
)) {
1659 push(*const_cast<InsetMathScript
*>(p
));
1660 idx() = p
->idxOfScript(up
);
1663 // we went in the right direction? Otherwise don't jump into the script
1667 int oy
= theLyXFunc().cursorBeforeDispatchY();
1668 if ((!up
&& y
<= oy
) ||
1677 // try to find an inset that knows better then we,
1678 if (inset().idxUpDown(*this, up
)) {
1679 //lyxerr << "idxUpDown triggered" << endl;
1680 // try to find best position within this inset
1682 setCursor(bruteFind2(*this, xo
, yo
));
1686 // any improvement going just out of inset?
1687 if (popBackward() && inMathed()) {
1688 //lyxerr << "updown: popBackward succeeded" << endl;
1691 int yold
= theLyXFunc().cursorBeforeDispatchY();
1693 if (up
? ynew
< yold
: ynew
> yold
)
1697 // no success, we are probably at the document top or bottom
1703 bool Cursor::upDownInText(bool up
, bool & updateNeeded
)
1705 LASSERT(text(), /**/);
1711 xo
= theLyXFunc().cursorBeforeDispatchX();
1713 // update the targetX - this is here before the "return false"
1714 // to set a new target which can be used by InsetTexts above
1715 // if we cannot move up/down inside this inset anymore
1716 if (x_target_
== -1)
1718 else if (xo
- textTargetOffset() != x_target() &&
1719 depth() == beforeDispatchCursor_
.depth()) {
1720 // In text mode inside the line (not left or right) possibly set a new target_x,
1721 // but only if we are somewhere else than the previous target-offset.
1723 // We want to keep the x-target on subsequent up/down movements
1724 // that cross beyond the end of short lines. Thus a special
1725 // handling when the cursor is at the end of line: Use the new
1726 // x-target only if the old one was before the end of line
1727 // or the old one was after the beginning of the line
1728 bool inRTL
= isWithinRtlParagraph(*this);
1732 left
= pos() == textRow().endpos();
1733 right
= pos() == textRow().pos();
1735 left
= pos() == textRow().pos();
1736 right
= pos() == textRow().endpos();
1738 if ((!left
&& !right
) ||
1739 (left
&& !right
&& xo
< x_target_
) ||
1740 (!left
&& right
&& x_target_
< xo
))
1747 // first get the current line
1748 TextMetrics
& tm
= bv_
->textMetrics(text());
1749 ParagraphMetrics
const & pm
= tm
.parMetrics(pit());
1751 if (pos() && boundary())
1752 row
= pm
.pos2row(pos() - 1);
1754 row
= pm
.pos2row(pos());
1756 // are we not at the start or end?
1758 if (pit() == 0 && row
== 0)
1761 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1762 row
+ 1 >= int(pm
.rows().size()))
1766 // with and without selection are handled differently
1768 int yo
= bv().getPos(*this, boundary()).y_
;
1770 // To next/previous row
1772 tm
.editXY(*this, xo
, yo
- textRow().ascent() - 1);
1774 tm
.editXY(*this, xo
, yo
+ textRow().descent() + 1);
1777 // This happens when you move out of an inset.
1778 // And to give the DEPM the possibility of doing
1779 // something we must provide it with two different
1781 Cursor dummy
= *this;
1784 if (bv().checkDepm(dummy
, old
)) {
1785 updateNeeded
= true;
1786 // Make sure that cur gets back whatever happened to dummy(Lgb)
1790 // if there is a selection, we stay out of any inset, and just jump to the right position:
1794 top().pos() = min(tm
.x2pos(pit(), row
- 1, xo
), top().lastpos());
1795 } else if (pit() > 0) {
1797 TextMetrics
& tm
= bv_
->textMetrics(text());
1798 if (!tm
.contains(pit()))
1799 tm
.newParMetricsUp();
1800 ParagraphMetrics
const & pmcur
= tm
.parMetrics(pit());
1801 top().pos() = min(tm
.x2pos(pit(), pmcur
.rows().size() - 1, xo
), top().lastpos());
1804 if (row
+ 1 < int(pm
.rows().size())) {
1805 top().pos() = min(tm
.x2pos(pit(), row
+ 1, xo
), top().lastpos());
1806 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1808 TextMetrics
& tm
= bv_
->textMetrics(text());
1809 if (!tm
.contains(pit()))
1810 tm
.newParMetricsDown();
1811 top().pos() = min(tm
.x2pos(pit(), 0, xo
), top().lastpos());
1815 updateNeeded
|= bv().checkDepm(*this, old
);
1818 updateTextTargetOffset();
1823 void Cursor::handleFont(string
const & font
)
1825 LYXERR(Debug::DEBUG
, font
);
1829 safe
= cap::grabAndEraseSelection(*this);
1834 if (lastpos() != 0) {
1835 // something left in the cell
1837 // cursor in first position
1839 } else if (pos() == lastpos()) {
1840 // cursor in last position
1843 // cursor in between. split cell
1844 MathData::iterator bt
= cell().begin();
1845 MathAtom at
= createInsetMath(from_utf8(font
));
1846 at
.nucleus()->cell(0) = MathData(bt
, bt
+ pos());
1847 cell().erase(bt
, bt
+ pos());
1852 // nothing left in the cell
1861 void Cursor::message(docstring
const & msg
) const
1863 theLyXFunc().setMessage(msg
);
1867 void Cursor::errorMessage(docstring
const & msg
) const
1869 theLyXFunc().setErrorMessage(msg
);
1873 docstring
Cursor::selectionAsString(bool with_label
) const
1878 int const label
= with_label
1879 ? AS_STR_LABEL
| AS_STR_INSETS
: AS_STR_INSETS
;
1882 idx_type
const startidx
= selBegin().idx();
1883 idx_type
const endidx
= selEnd().idx();
1884 if (startidx
!= endidx
) {
1885 // multicell selection
1886 InsetTabular
* table
= inset().asInsetTabular();
1887 LASSERT(table
, return docstring());
1888 return table
->asString(startidx
, endidx
);
1891 ParagraphList
const & pars
= text()->paragraphs();
1893 pit_type
const startpit
= selBegin().pit();
1894 pit_type
const endpit
= selEnd().pit();
1895 size_t const startpos
= selBegin().pos();
1896 size_t const endpos
= selEnd().pos();
1898 if (startpit
== endpit
)
1899 return pars
[startpit
].asString(startpos
, endpos
, label
);
1901 // First paragraph in selection
1902 docstring result
= pars
[startpit
].
1903 asString(startpos
, pars
[startpit
].size(), label
)
1904 + parbreak(pars
[startpit
]);
1906 // The paragraphs in between (if any)
1907 for (pit_type pit
= startpit
+ 1; pit
!= endpit
; ++pit
) {
1908 Paragraph
const & par
= pars
[pit
];
1909 result
+= par
.asString(0, par
.size(), label
)
1910 + parbreak(pars
[pit
]);
1913 // Last paragraph in selection
1914 result
+= pars
[endpit
].asString(0, endpos
, label
);
1920 return cap::grabSelection(*this);
1926 docstring
Cursor::currentState() const
1929 odocstringstream os
;
1935 return text()->currentState(*this);
1941 docstring
Cursor::getPossibleLabel() const
1943 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
1947 Encoding
const * Cursor::getEncoding() const
1951 CursorSlice
const & sl
= innerTextSlice();
1952 Text
const & text
= *sl
.text();
1953 Font font
= text
.getPar(sl
.pit()).getFont(
1954 bv().buffer().params(), sl
.pos(), outerFont(sl
.pit(), text
.paragraphs()));
1955 return font
.language()->encoding();
1959 void Cursor::undispatched()
1961 disp_
.dispatched(false);
1965 void Cursor::dispatched()
1967 disp_
.dispatched(true);
1971 void Cursor::updateFlags(Update::flags f
)
1977 void Cursor::noUpdate()
1979 disp_
.update(Update::None
);
1983 Font
Cursor::getFont() const
1985 // The logic here should more or less match to the Cursor::setCurrentFont
1986 // logic, i.e. the cursor height should give a hint what will happen
1987 // if a character is entered.
1989 // HACK. far from being perfect...
1991 CursorSlice
const & sl
= innerTextSlice();
1992 Text
const & text
= *sl
.text();
1993 Paragraph
const & par
= text
.getPar(sl
.pit());
1995 // on boundary, so we are really at the character before
1996 pos_type pos
= sl
.pos();
1997 if (pos
> 0 && boundary())
2000 // on space? Take the font before (only for RTL boundary stay)
2002 TextMetrics
const & tm
= bv().textMetrics(&text
);
2003 if (pos
== sl
.lastpos()
2004 || (par
.isSeparator(pos
)
2005 && !tm
.isRTLBoundary(sl
.pit(), pos
)))
2009 // get font at the position
2010 Font font
= par
.getFont(bv().buffer().params(), pos
,
2011 outerFont(sl
.pit(), text
.paragraphs()));
2017 bool Cursor::fixIfBroken()
2019 if (DocIterator::fixIfBroken()) {
2027 bool notifyCursorLeaves(Cursor
const & old
, Cursor
& cur
)
2029 // find inset in common
2031 for (i
= 0; i
< old
.depth() && i
< cur
.depth(); ++i
) {
2032 if (&old
[i
].inset() != &cur
[i
].inset())
2036 // update words if we just moved to another paragraph
2037 if (i
== old
.depth() && i
== cur
.depth()
2038 && !cur
.buffer().isClean()
2039 && cur
.inTexted() && old
.inTexted()
2040 && cur
.pit() != old
.pit()) {
2041 old
.paragraph().updateWords(old
.top());
2045 // notify everything on top of the common part in old cursor,
2046 // but stop if the inset claims the cursor to be invalid now
2047 for (; i
< old
.depth(); ++i
) {
2048 Cursor insetPos
= old
;
2050 if (old
[i
].inset().notifyCursorLeaves(insetPos
, cur
))
2058 void Cursor::setCurrentFont()
2060 CursorSlice
const & cs
= innerTextSlice();
2061 Paragraph
const & par
= cs
.paragraph();
2062 pos_type cpit
= cs
.pit();
2063 pos_type cpos
= cs
.pos();
2064 Text
const & ctext
= *cs
.text();
2065 TextMetrics
const & tm
= bv().textMetrics(&ctext
);
2067 // are we behind previous char in fact? -> go to that char
2068 if (cpos
> 0 && boundary())
2071 // find position to take the font from
2073 // paragraph end? -> font of last char
2074 if (cpos
== lastpos())
2076 // on space? -> look at the words in front of space
2077 else if (cpos
> 0 && par
.isSeparator(cpos
)) {
2078 // abc| def -> font of c
2079 // abc |[WERBEH], i.e. boundary==true -> font of c
2080 // abc [WERBEH]| def, font of the space
2081 if (!tm
.isRTLBoundary(cpit
, cpos
))
2087 BufferParams
const & bufparams
= buffer().params();
2088 current_font
= par
.getFontSettings(bufparams
, cpos
);
2089 real_current_font
= tm
.displayFont(cpit
, cpos
);
2091 // special case for paragraph end
2092 if (cs
.pos() == lastpos()
2093 && tm
.isRTLBoundary(cpit
, cs
.pos())
2095 Language
const * lang
= par
.getParLanguage(bufparams
);
2096 current_font
.setLanguage(lang
);
2097 current_font
.fontInfo().setNumber(FONT_OFF
);
2098 real_current_font
.setLanguage(lang
);
2099 real_current_font
.fontInfo().setNumber(FONT_OFF
);
2104 bool Cursor::textUndo()
2106 DocIterator dit
= *this;
2107 // Undo::textUndo() will modify dit.
2108 if (!bv_
->buffer().undo().textUndo(dit
))
2118 bool Cursor::textRedo()
2120 DocIterator dit
= *this;
2121 // Undo::textRedo() will modify dit.
2122 if (!bv_
->buffer().undo().textRedo(dit
))
2132 void Cursor::finishUndo() const
2134 bv_
->buffer().undo().finishUndo();
2138 void Cursor::beginUndoGroup() const
2140 bv_
->buffer().undo().beginUndoGroup();
2144 void Cursor::endUndoGroup() const
2146 bv_
->buffer().undo().endUndoGroup();
2150 void Cursor::recordUndo(UndoKind kind
, pit_type from
, pit_type to
) const
2152 bv_
->buffer().undo().recordUndo(*this, kind
, from
, to
);
2156 void Cursor::recordUndo(UndoKind kind
, pit_type from
) const
2158 bv_
->buffer().undo().recordUndo(*this, kind
, from
);
2162 void Cursor::recordUndo(UndoKind kind
) const
2164 bv_
->buffer().undo().recordUndo(*this, kind
);
2168 void Cursor::recordUndoInset(UndoKind kind
) const
2170 bv_
->buffer().undo().recordUndoInset(*this, kind
);
2174 void Cursor::recordUndoFullDocument() const
2176 bv_
->buffer().undo().recordUndoFullDocument(*this);
2180 void Cursor::recordUndoSelection() const
2183 if (cap::multipleCellsSelected(*this))
2188 bv_
->buffer().undo().recordUndo(*this, ATOMIC_UNDO
,
2189 selBegin().pit(), selEnd().pit());
2193 void Cursor::checkBufferStructure()
2195 Buffer
const * master
= buffer().masterBuffer();
2196 if (master
->tocBackend().updateItem(*this))
2197 master
->structureChanged();