1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
17 #include "bufferparams.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
35 #include "rowpainter.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
51 /// top, right, bottom pixel margin
52 extern int const PAPER_MARGIN = 20;
53 /// margin for changebar
54 extern int const CHANGEBAR_MARGIN = 10;
56 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
58 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
61 BufferView * LyXText::bv()
63 lyx::Assert(bv_owner != 0);
68 BufferView * LyXText::bv() const
70 lyx::Assert(bv_owner != 0);
75 int LyXText::top_y() const
81 for (Row * row = firstrow;
82 row && row != anchor_row_; row = row->next()) {
85 return y + anchor_row_offset_;
89 void LyXText::top_y(int newy)
93 lyxerr[Debug::GUI] << "setting top y = " << newy << endl;
96 Row * row = getRowNearY(y);
98 if (row == anchor_row_ && anchor_row_offset_ == newy - y) {
99 lyxerr[Debug::GUI] << "top_y to same value, skipping update" << endl;
104 anchor_row_offset_ = newy - y;
105 lyxerr[Debug::GUI] << "changing reference to row: " << anchor_row_
106 << " offset: " << anchor_row_offset_ << endl;
111 void LyXText::anchor_row(Row * row)
114 anchor_row_offset_ = 0;
116 anchor_row_offset_ = old_y - top_y();
117 lyxerr[Debug::GUI] << "anchor_row(): changing reference to row: "
118 << anchor_row_ << " offset: " << anchor_row_offset_
123 int LyXText::workWidth() const
126 // FIXME: pass (const ?) ref
127 return inset_owner->textWidth(bv());
129 return bv()->workWidth();
133 int LyXText::workWidth(Inset * inset) const
135 Paragraph * par = inset->parOwner();
138 pos_type pos = par->getPositionOfInset(inset);
139 lyx::Assert(pos != -1);
141 LyXLayout_ptr const & layout = par->layout();
143 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
144 // Optimization here: in most cases, the real row is
145 // not needed, but only the par/pos values. So we just
146 // construct a dummy row for leftMargin. (JMarc)
150 return workWidth() - leftMargin(&dummyrow);
153 Row * row = getRow(par, pos, dummy_y);
155 while (frow->previous() && frow->par() == frow->previous()->par())
156 frow = frow->previous();
158 // FIXME: I don't understand this code - jbl
160 unsigned int maxw = 0;
161 while (!frow->isParEnd()) {
162 if ((frow != row) && (maxw < frow->width()))
163 maxw = frow->width();
174 int LyXText::getRealCursorX() const
177 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
178 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
183 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
186 if (!Encodings::is_arabic(c))
187 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
188 return c + (0xb0 - '0');
192 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
193 unsigned char next_char = ' ';
195 for (pos_type i = pos+1; i < par->size(); ++i)
196 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
197 next_char = par->getChar(i);
201 if (Encodings::is_arabic(next_char)) {
202 if (Encodings::is_arabic(prev_char) &&
203 !Encodings::is_arabic_special(prev_char))
204 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
206 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
208 if (Encodings::is_arabic(prev_char) &&
209 !Encodings::is_arabic_special(prev_char))
210 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
212 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
216 // This is the comments that some of the warnings below refers to.
217 // There are some issues in this file and I don't think they are
218 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
219 // this is a problem that has been here almost from day one and that a
220 // larger userbase with differenct access patters triggers the bad
221 // behaviour. (segfaults.) What I think happen is: In several places
222 // we store the paragraph in the current cursor and then moves the
223 // cursor. This movement of the cursor will delete paragraph at the
224 // old position if it is now empty. This will make the temporary
225 // pointer to the old cursor paragraph invalid and dangerous to use.
226 // And is some cases this will trigger a segfault. I have marked some
227 // of the cases where this happens with a warning, but I am sure there
228 // are others in this file and in text2.C. There is also a note in
229 // Delete() that you should read. In Delete I store the paragraph->id
230 // instead of a pointer to the paragraph. I am pretty sure this faulty
231 // use of temporary pointers to paragraphs that might have gotten
232 // invalidated (through a cursor movement) before they are used, are
233 // the cause of the strange crashes we get reported often.
235 // It is very tiresom to change this code, especially when it is as
236 // hard to read as it is. Help to fix all the cases where this is done
237 // would be greately appreciated.
241 int LyXText::singleWidth(Paragraph * par,
244 if (pos >= par->size())
247 char const c = par->getChar(pos);
248 return singleWidth(par, pos, c);
252 int LyXText::singleWidth(Paragraph * par,
253 pos_type pos, char c) const
255 if (pos >= par->size())
258 LyXFont const font = getFont(bv()->buffer(), par, pos);
260 // The most common case is handled first (Asger)
261 if (IsPrintable(c)) {
262 if (font.language()->RightToLeft()) {
263 if (font.language()->lang() == "arabic" &&
264 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
265 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
266 if (Encodings::IsComposeChar_arabic(c))
269 c = transformChar(c, par, pos);
270 } else if (font.language()->lang() == "hebrew" &&
271 Encodings::IsComposeChar_hebrew(c))
274 return font_metrics::width(c, font);
278 if (c == Paragraph::META_INSET) {
279 Inset * tmpinset = par->getInset(pos);
281 if (tmpinset->lyxCode() == Inset::HFILL_CODE) {
282 // Because of the representation as vertical lines
286 #warning inset->update FIXME
287 // this IS needed otherwise on initialitation we don't get the fill
288 // of the row right (ONLY on initialization if we read a file!)
289 // should be changed! (Jug 20011204)
290 tmpinset->update(bv());
292 return tmpinset->width(bv(), font);
297 if (IsSeparatorChar(c))
299 return font_metrics::width(c, font);
303 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
305 if (bidi_start == -1)
308 return log2vis_list[pos-bidi_start];
312 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
314 if (bidi_start == -1)
317 return vis2log_list[pos-bidi_start];
321 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
323 if (bidi_start == -1)
326 return bidi_levels[pos-bidi_start];
330 bool LyXText::bidi_InRange(lyx::pos_type pos) const
332 return bidi_start == -1 ||
333 (bidi_start <= pos && pos <= bidi_end);
337 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
339 bidi_same_direction = true;
340 if (!lyxrc.rtl_support) {
345 Inset * inset = row->par()->inInset();
346 if (inset && inset->owner() &&
347 inset->owner()->lyxCode() == Inset::ERT_CODE) {
352 bidi_start = row->pos();
353 bidi_end = row->lastPrintablePos();
355 if (bidi_start > bidi_end) {
360 if (bidi_end + 2 - bidi_start >
361 static_cast<pos_type>(log2vis_list.size())) {
363 (bidi_end + 2 - bidi_start < 500) ?
364 500 : 2 * (bidi_end + 2 - bidi_start);
365 log2vis_list.resize(new_size);
366 vis2log_list.resize(new_size);
367 bidi_levels.resize(new_size);
370 vis2log_list[bidi_end + 1 - bidi_start] = -1;
371 log2vis_list[bidi_end + 1 - bidi_start] = -1;
375 row->par()->isRightToLeftPar(buf->params);
379 pos_type const body_pos = row->par()->beginningOfBody();
381 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
382 bool is_space = row->par()->isLineSeparator(lpos);
384 (is_space && lpos + 1 <= bidi_end &&
385 !row->par()->isLineSeparator(lpos + 1) &&
386 !row->par()->isNewline(lpos + 1))
388 LyXFont font = row->par()->getFontSettings(buf->params, pos);
389 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
390 font.number() == LyXFont::ON &&
391 row->par()->getFontSettings(buf->params, lpos - 1).number()
393 font = row->par()->getFontSettings(buf->params, lpos);
398 bool new_rtl = font.isVisibleRightToLeft();
399 bool new_rtl0 = font.isRightToLeft();
402 if (lpos == body_pos - 1
403 && row->pos() < body_pos - 1
405 new_level = (rtl_par) ? 1 : 0;
406 new_rtl = new_rtl0 = rtl_par;
408 new_level = (new_rtl) ? 1 : 2;
410 new_level = (rtl_par) ? 2 : 0;
412 if (is_space && new_level >= level) {
418 int new_level2 = new_level;
420 if (level == new_level && rtl0 != new_rtl0) {
422 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
423 } else if (level < new_level) {
424 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
425 if (new_level > rtl_par)
426 bidi_same_direction = false;
428 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
431 bidi_levels[lpos - bidi_start] = new_level;
433 while (level > new_level2) {
434 pos_type old_lpos = stack[--level];
435 int delta = lpos - old_lpos - 1;
438 log2vis_list[lpos - bidi_start] += delta;
439 log2vis_list[old_lpos - bidi_start] += delta;
441 while (level < new_level)
442 stack[level++] = lpos;
446 pos_type const old_lpos = stack[--level];
447 int delta = bidi_end - old_lpos;
450 log2vis_list[old_lpos - bidi_start] += delta;
453 pos_type vpos = bidi_start - 1;
454 for (pos_type lpos = bidi_start;
455 lpos <= bidi_end; ++lpos) {
456 vpos += log2vis_list[lpos - bidi_start];
457 vis2log_list[vpos - bidi_start] = lpos;
458 log2vis_list[lpos - bidi_start] = vpos;
463 // This method requires a previous call to ComputeBidiTables()
464 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
467 if (!lyxrc.rtl_support || pos == 0)
470 if (!bidi_InRange(pos - 1)) {
471 /// This can happen if pos is the first char of a row.
472 /// Returning false in this case is incorrect!
476 bool const rtl = bidi_level(pos - 1) % 2;
477 bool const rtl2 = bidi_InRange(pos)
478 ? bidi_level(pos) % 2
479 : par->isRightToLeftPar(buf->params);
484 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
485 pos_type pos, LyXFont const & font) const
487 if (!lyxrc.rtl_support)
488 return false; // This is just for speedup
490 bool const rtl = font.isVisibleRightToLeft();
491 bool const rtl2 = bidi_InRange(pos)
492 ? bidi_level(pos) % 2
493 : par->isRightToLeftPar(buf->params);
498 int LyXText::leftMargin(Row const * row) const
502 if (row->pos() < row->par()->size())
503 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
504 (ins = row->par()->getInset(row->pos())) &&
505 (ins->needFullRow() || ins->display()))
508 LyXTextClass const & tclass =
509 bv()->buffer()->params.getLyXTextClass();
510 LyXLayout_ptr const & layout = row->par()->layout();
512 string parindent = layout->parindent;
516 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
518 // this is the way, LyX handles the LaTeX-Environments.
519 // I have had this idea very late, so it seems to be a
520 // later added hack and this is true
521 if (!row->par()->getDepth()) {
522 if (row->par()->layout() == tclass.defaultLayout()) {
523 // find the previous same level paragraph
524 if (row->par()->previous()) {
525 Paragraph * newpar = row->par()
526 ->depthHook(row->par()->getDepth());
528 newpar->layout()->nextnoindent)
533 // find the next level paragraph
535 Paragraph * newpar = row->par()->outerHook();
537 // make a corresponding row. Needed to call LeftMargin()
539 // check wether it is a sufficent paragraph
540 if (newpar && newpar->layout()->isEnvironment()) {
542 dummyrow.par(newpar);
543 dummyrow.pos(newpar->size());
544 x = leftMargin(&dummyrow);
546 // this is no longer an error, because this function
547 // is used to clear impossible depths after changing
548 // a layout. Since there is always a redo,
549 // LeftMargin() is always called
550 row->par()->params().depth(0);
553 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
554 if (newpar->params().noindent())
557 parindent = newpar->layout()->parindent;
563 LyXFont const labelfont = getLabelFont(bv()->buffer(), row->par());
564 switch (layout->margintype) {
566 if (!layout->leftmargin.empty()) {
567 x += font_metrics::signedWidth(layout->leftmargin,
568 tclass.defaultfont());
570 if (!row->par()->getLabelstring().empty()) {
571 x += font_metrics::signedWidth(layout->labelindent,
573 x += font_metrics::width(row->par()->getLabelstring(),
575 x += font_metrics::width(layout->labelsep, labelfont);
579 x += font_metrics::signedWidth(layout->labelindent, labelfont);
580 // The width of an empty par, even with manual label, should be 0
581 if (!row->par()->empty() && row->pos() >= row->par()->beginningOfBody()) {
582 if (!row->par()->getLabelWidthString().empty()) {
583 x += font_metrics::width(row->par()->getLabelWidthString(),
585 x += font_metrics::width(layout->labelsep, labelfont);
590 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
591 / (row->par()->getDepth() + 4);
593 case MARGIN_FIRST_DYNAMIC:
594 if (layout->labeltype == LABEL_MANUAL) {
595 if (row->pos() >= row->par()->beginningOfBody()) {
596 x += font_metrics::signedWidth(layout->leftmargin,
599 x += font_metrics::signedWidth(layout->labelindent,
602 } else if (row->pos()
603 // Special case to fix problems with
605 || (layout->labeltype == LABEL_STATIC
606 && layout->latextype == LATEX_ENVIRONMENT
607 && ! row->par()->isFirstInSequence())) {
608 x += font_metrics::signedWidth(layout->leftmargin,
610 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
611 && layout->labeltype != LABEL_BIBLIO
612 && layout->labeltype !=
613 LABEL_CENTERED_TOP_ENVIRONMENT) {
614 x += font_metrics::signedWidth(layout->labelindent,
616 x += font_metrics::width(layout->labelsep, labelfont);
617 x += font_metrics::width(row->par()->getLabelstring(),
622 case MARGIN_RIGHT_ADDRESS_BOX:
624 // ok, a terrible hack. The left margin depends on the widest
625 // row in this paragraph. Do not care about footnotes, they
626 // are *NOT* allowed in the LaTeX realisation of this layout.
628 // find the first row of this paragraph
629 Row const * tmprow = row;
630 while (tmprow->previous()
631 && tmprow->previous()->par() == row->par())
632 tmprow = tmprow->previous();
634 int minfill = tmprow->fill();
635 while (tmprow->next() && tmprow->next()->par() == row->par()) {
636 tmprow = tmprow->next();
637 if (tmprow->fill() < minfill)
638 minfill = tmprow->fill();
641 x += font_metrics::signedWidth(layout->leftmargin,
642 tclass.defaultfont());
648 if ((workWidth() > 0) &&
649 !row->par()->params().leftIndent().zero())
651 LyXLength const len = row->par()->params().leftIndent();
652 int const tw = inset_owner ?
653 inset_owner->latexTextWidth(bv()) : workWidth();
654 x += len.inPixels(tw);
657 LyXAlignment align; // wrong type
659 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
660 align = layout->align;
662 align = row->par()->params().align();
664 // set the correct parindent
665 if (row->pos() == 0) {
666 if ((layout->labeltype == LABEL_NO_LABEL
667 || layout->labeltype == LABEL_TOP_ENVIRONMENT
668 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
669 || (layout->labeltype == LABEL_STATIC
670 && layout->latextype == LATEX_ENVIRONMENT
671 && ! row->par()->isFirstInSequence()))
672 && align == LYX_ALIGN_BLOCK
673 && !row->par()->params().noindent()
674 // in tabulars and ert paragraphs are never indented!
675 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
676 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
677 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
678 && (row->par()->layout() != tclass.defaultLayout() ||
679 bv()->buffer()->params.paragraph_separation ==
680 BufferParams::PARSEP_INDENT)) {
681 x += font_metrics::signedWidth(parindent,
682 tclass.defaultfont());
683 } else if (layout->labeltype == LABEL_BIBLIO) {
684 // ale970405 Right width for bibitems
685 x += bibitemMaxWidth(bv(), tclass.defaultfont());
693 int LyXText::rightMargin(Buffer const & buf, Row const & row) const
697 if (row.pos() < row.par()->size())
698 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
699 (ins=row.par()->getInset(row.pos())) &&
700 (ins->needFullRow() || ins->display()))
703 LyXTextClass const & tclass = buf.params.getLyXTextClass();
704 LyXLayout_ptr const & layout = row.par()->layout();
707 + font_metrics::signedWidth(tclass.rightmargin(),
708 tclass.defaultfont());
710 // this is the way, LyX handles the LaTeX-Environments.
711 // I have had this idea very late, so it seems to be a
712 // later added hack and this is true
713 if (row.par()->getDepth()) {
714 // find the next level paragraph
716 Paragraph const * newpar = row.par();
719 newpar = newpar->previous();
721 && newpar->getDepth() >= row.par()->getDepth());
723 // make a corresponding row. Needed to call LeftMargin()
725 // check wether it is a sufficent paragraph
726 if (newpar && newpar->layout()->isEnvironment()) {
728 dummyrow.par(const_cast<Paragraph *>(newpar));
730 x = rightMargin(buf, dummyrow);
732 // this is no longer an error, because this function
733 // is used to clear impossible depths after changing
734 // a layout. Since there is always a redo,
735 // LeftMargin() is always called
736 row.par()->params().depth(0);
740 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
741 x += font_metrics::signedWidth(layout->rightmargin,
742 tclass.defaultfont())
743 * 4 / (row.par()->getDepth() + 4);
748 int LyXText::labelEnd(Row const & row) const
750 if (row.par()->layout()->margintype == MARGIN_MANUAL) {
752 tmprow.pos(row.par()->size());
753 // return the beginning of the body
754 return leftMargin(&tmprow);
757 // LabelEnd is only needed if the layout
758 // fills a flushleft label.
765 // this needs special handling - only newlines count as a break point
766 pos_type addressBreakPoint(pos_type i, Paragraph * par)
768 for (; i < par->size(); ++i) {
769 if (par->isNewline(i))
780 LyXText::rowBreakPoint(Row const & row) const
782 Paragraph * par = row.par();
784 // maximum pixel width of a row.
785 int width = workWidth() - rightMargin(*bv()->buffer(), row);
787 // inset->textWidth() returns -1 via workWidth(),
792 LyXLayout_ptr const & layout = par->layout();
794 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
795 return addressBreakPoint(row.pos(), par);
797 pos_type const pos = row.pos();
798 pos_type const body_pos = par->beginningOfBody();
799 pos_type const last = par->size();
800 pos_type point = last;
805 // Now we iterate through until we reach the right margin
806 // or the end of the par, then choose the possible break
809 int const left = leftMargin(&row);
812 // pixel width since last breakpoint
816 for (; i < last; ++i) {
818 if (par->isNewline(i)) {
823 char const c = par->getChar(i);
825 int thiswidth = singleWidth(par, i, c);
827 // add the auto-hfill from label end to the body
828 if (body_pos && i == body_pos) {
829 thiswidth += font_metrics::width(layout->labelsep,
830 getLabelFont(bv()->buffer(), par));
831 if (par->isLineSeparator(i - 1))
832 thiswidth -= singleWidth(par, i - 1);
833 int left_margin = labelEnd(row);
834 if (thiswidth < left_margin)
835 thiswidth = left_margin;
839 chunkwidth += thiswidth;
841 Inset * in = par->isInset(i) ? par->getInset(i) : 0;
842 bool fullrow = (in && (in->display() || in->needFullRow()));
844 // break before a character that will fall off
845 // the right of the row
847 // if no break before or we are at an inset
848 // that will take up a row, break here
849 if (point == last || fullrow || chunkwidth >= (width - left)) {
858 if (!in || in->isChar()) {
859 // some insets are line separators too
860 if (par->isLineSeparator(i)) {
870 // full row insets start at a new row
872 if (pos < last - 1) {
874 if (par->isLineSeparator(i + 1))
877 // to avoid extra rows
886 if (point == last && x >= width) {
887 // didn't find one, break at the point we reached the edge
889 } else if (i == last && x < width) {
890 // found one, but we fell off the end of the par, so prefer
895 // manual labels cannot be broken in LaTeX
896 if (body_pos && point < body_pos)
897 point = body_pos - 1;
903 // returns the minimum space a row needs on the screen in pixel
904 int LyXText::fill(Row & row, int paper_width) const
910 // get the pure distance
911 pos_type const last = row.lastPrintablePos();
913 // special handling of the right address boxes
914 if (row.par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
915 int const tmpfill = row.fill();
916 row.fill(0); // the minfill in MarginLeft()
917 w = leftMargin(&row);
920 w = leftMargin(&row);
922 Paragraph * par = row.par();
923 LyXLayout_ptr const & layout = par->layout();
925 pos_type const body_pos = par->beginningOfBody();
926 pos_type i = row.pos();
929 if (body_pos > 0 && i == body_pos) {
930 w += font_metrics::width(layout->labelsep, getLabelFont(bv()->buffer(), par));
931 if (par->isLineSeparator(i - 1))
932 w -= singleWidth(par, i - 1);
933 int left_margin = labelEnd(row);
937 w += singleWidth(par, i);
940 if (body_pos > 0 && body_pos > last) {
941 w += font_metrics::width(layout->labelsep, getLabelFont(bv()->buffer(), par));
942 if (last >= 0 && par->isLineSeparator(last))
943 w -= singleWidth(par, last);
944 int const left_margin = labelEnd(row);
949 int const fill = paper_width - w - rightMargin(*bv()->buffer(), row);
954 // returns the minimum space a manual label needs on the screen in pixel
955 int LyXText::labelFill(Row const & row) const
957 pos_type last = row.par()->beginningOfBody();
959 lyx::Assert(last > 0);
961 // -1 because a label ends either with a space that is in the label,
962 // or with the beginning of a footnote that is outside the label.
965 // a separator at this end does not count
966 if (row.par()->isLineSeparator(last))
970 pos_type i = row.pos();
972 w += singleWidth(row.par(), i);
977 string const & labwidstr = row.par()->params().labelWidthString();
978 if (!labwidstr.empty()) {
979 LyXFont const labfont = getLabelFont(bv()->buffer(), row.par());
980 int const labwidth = font_metrics::width(labwidstr, labfont);
981 fill = max(labwidth - w, 0);
988 LColor::color LyXText::backgroundColor() const
991 return inset_owner->backgroundColor();
993 return LColor::background;
996 void LyXText::setHeightOfRow(Row * row)
998 // get the maximum ascent and the maximum descent
1001 float layoutasc = 0;
1002 float layoutdesc = 0;
1005 Inset * tmpinset = 0;
1007 // ok , let us initialize the maxasc and maxdesc value.
1008 // This depends in LaTeX of the font of the last character
1009 // in the paragraph. The hack below is necessary because
1010 // of the possibility of open footnotes
1012 // Correction: only the fontsize count. The other properties
1013 // are taken from the layoutfont. Nicer on the screen :)
1014 Paragraph * par = row->par();
1015 Paragraph * firstpar = row->par();
1017 LyXLayout_ptr const & layout = firstpar->layout();
1019 // as max get the first character of this row then it can increase but not
1020 // decrease the height. Just some point to start with so we don't have to
1021 // do the assignment below too often.
1022 LyXFont font = getFont(bv()->buffer(), par, row->pos());
1023 LyXFont::FONT_SIZE const tmpsize = font.size();
1024 font = getLayoutFont(bv()->buffer(), par);
1025 LyXFont::FONT_SIZE const size = font.size();
1026 font.setSize(tmpsize);
1028 LyXFont labelfont = getLabelFont(bv()->buffer(), par);
1030 float spacing_val = 1.0;
1031 if (!row->par()->params().spacing().isDefault()) {
1032 spacing_val = row->par()->params().spacing().getValue();
1034 spacing_val = bv()->buffer()->params.spacing.getValue();
1036 //lyxerr << "spacing_val = " << spacing_val << endl;
1038 int maxasc = int(font_metrics::maxAscent(font) *
1039 layout->spacing.getValue() *
1041 int maxdesc = int(font_metrics::maxDescent(font) *
1042 layout->spacing.getValue() *
1045 pos_type const pos_end = row->lastPos();
1049 if (!row->par()->empty()) {
1050 // Check if any insets are larger
1051 for (pos_type pos = row->pos(); pos <= pos_end; ++pos) {
1052 if (row->par()->isInset(pos)) {
1053 tmpfont = getFont(bv()->buffer(), row->par(), pos);
1054 tmpinset = row->par()->getInset(pos);
1056 #if 1 // this is needed for deep update on initialitation
1057 #warning inset->update FIXME
1058 tmpinset->update(bv());
1060 asc = tmpinset->ascent(bv(), tmpfont);
1061 desc = tmpinset->descent(bv(), tmpfont);
1062 maxwidth += tmpinset->width(bv(), tmpfont);
1063 maxasc = max(maxasc, asc);
1064 maxdesc = max(maxdesc, desc);
1067 maxwidth += singleWidth(row->par(), pos);
1072 // Check if any custom fonts are larger (Asger)
1073 // This is not completely correct, but we can live with the small,
1074 // cosmetic error for now.
1075 LyXFont::FONT_SIZE maxsize =
1076 row->par()->highestFontInRange(row->pos(), pos_end, size);
1077 if (maxsize > font.size()) {
1078 font.setSize(maxsize);
1080 asc = font_metrics::maxAscent(font);
1081 desc = font_metrics::maxDescent(font);
1088 // This is nicer with box insets:
1092 row->ascent_of_text(maxasc);
1094 // is it a top line?
1095 if (!row->pos() && (row->par() == firstpar)) {
1097 // some parksips VERY EASY IMPLEMENTATION
1098 if (bv()->buffer()->params.paragraph_separation ==
1099 BufferParams::PARSEP_SKIP)
1101 if (layout->isParagraph()
1102 && firstpar->getDepth() == 0
1103 && firstpar->previous())
1105 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1106 } else if (firstpar->previous() &&
1107 firstpar->previous()->layout()->isParagraph() &&
1108 firstpar->previous()->getDepth() == 0)
1110 // is it right to use defskip here too? (AS)
1111 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1116 if (!row->par()->previous() && !isInInset())
1117 maxasc += PAPER_MARGIN;
1119 // add the vertical spaces, that the user added
1120 maxasc += getLengthMarkerHeight(*bv(), firstpar->params().spaceTop());
1122 // do not forget the DTP-lines!
1123 // there height depends on the font of the nearest character
1124 if (firstpar->params().lineTop())
1126 maxasc += 2 * font_metrics::ascent('x', getFont(bv()->buffer(),
1128 // and now the pagebreaks
1129 if (firstpar->params().pagebreakTop())
1130 maxasc += 3 * defaultRowHeight();
1132 if (firstpar->params().startOfAppendix())
1133 maxasc += 3 * defaultRowHeight();
1135 // This is special code for the chapter, since the label of this
1136 // layout is printed in an extra row
1137 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1138 && bv()->buffer()->params.secnumdepth >= 0)
1140 float spacing_val = 1.0;
1141 if (!row->par()->params().spacing().isDefault()) {
1142 spacing_val = row->par()->params().spacing().getValue();
1144 spacing_val = bv()->buffer()->params.spacing.getValue();
1147 labeladdon = int(font_metrics::maxDescent(labelfont) *
1148 layout->spacing.getValue() *
1150 + int(font_metrics::maxAscent(labelfont) *
1151 layout->spacing.getValue() *
1155 // special code for the top label
1156 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1157 || layout->labeltype == LABEL_BIBLIO
1158 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1159 && row->par()->isFirstInSequence()
1160 && !row->par()->getLabelstring().empty())
1162 float spacing_val = 1.0;
1163 if (!row->par()->params().spacing().isDefault()) {
1164 spacing_val = row->par()->params().spacing().getValue();
1166 spacing_val = bv()->buffer()->params.spacing.getValue();
1170 (font_metrics::maxAscent(labelfont) *
1171 layout->spacing.getValue() *
1173 +(font_metrics::maxDescent(labelfont) *
1174 layout->spacing.getValue() *
1176 + layout->topsep * defaultRowHeight()
1177 + layout->labelbottomsep * defaultRowHeight());
1180 // and now the layout spaces, for example before and after a section,
1181 // or between the items of a itemize or enumerate environment
1183 if (!firstpar->params().pagebreakTop()) {
1184 Paragraph * prev = row->par()->previous();
1186 prev = row->par()->depthHook(row->par()->getDepth());
1187 if (prev && prev->layout() == firstpar->layout() &&
1188 prev->getDepth() == firstpar->getDepth() &&
1189 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1191 layoutasc = (layout->itemsep * defaultRowHeight());
1192 } else if (row->previous()) {
1193 tmptop = layout->topsep;
1195 if (row->previous()->par()->getDepth() >= row->par()->getDepth())
1196 tmptop -= row->previous()->par()->layout()->bottomsep;
1199 layoutasc = (tmptop * defaultRowHeight());
1200 } else if (row->par()->params().lineTop()) {
1201 tmptop = layout->topsep;
1204 layoutasc = (tmptop * defaultRowHeight());
1207 prev = row->par()->outerHook();
1209 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1211 if (firstpar->previous() &&
1212 firstpar->previous()->getDepth() == 0 &&
1213 firstpar->previous()->layout() !=
1217 } else if (firstpar->previous()) {
1218 maxasc += int(layout->parsep * defaultRowHeight());
1224 // is it a bottom line?
1225 if (row->par() == par
1226 && (!row->next() || row->next()->par() != row->par())) {
1227 // the bottom margin
1228 if (!par->next() && !isInInset())
1229 maxdesc += PAPER_MARGIN;
1231 // add the vertical spaces, that the user added
1232 maxdesc += getLengthMarkerHeight(*bv(), firstpar->params().spaceBottom());
1234 // do not forget the DTP-lines!
1235 // there height depends on the font of the nearest character
1236 if (firstpar->params().lineBottom())
1237 maxdesc += 2 * font_metrics::ascent('x',
1238 getFont(bv()->buffer(),
1240 max(pos_type(0), par->size() - 1)));
1242 // and now the pagebreaks
1243 if (firstpar->params().pagebreakBottom())
1244 maxdesc += 3 * defaultRowHeight();
1246 // and now the layout spaces, for example before and after
1247 // a section, or between the items of a itemize or enumerate
1249 if (!firstpar->params().pagebreakBottom()
1250 && row->par()->next()) {
1251 Paragraph * nextpar = row->par()->next();
1252 Paragraph * comparepar = row->par();
1256 if (comparepar->getDepth() > nextpar->getDepth()) {
1257 usual = (comparepar->layout()->bottomsep * defaultRowHeight());
1258 comparepar = comparepar->depthHook(nextpar->getDepth());
1259 if (comparepar->layout()!= nextpar->layout()
1260 || nextpar->getLabelWidthString() !=
1261 comparepar->getLabelWidthString())
1263 unusual = (comparepar->layout()->bottomsep * defaultRowHeight());
1265 if (unusual > usual)
1266 layoutdesc = unusual;
1269 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1271 if (comparepar->layout() != nextpar->layout()
1272 || nextpar->getLabelWidthString() !=
1273 comparepar->getLabelWidthString())
1274 layoutdesc = int(comparepar->layout()->bottomsep * defaultRowHeight());
1279 // incalculate the layout spaces
1280 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1281 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1283 // calculate the new height of the text
1284 height -= row->height();
1286 row->height(maxasc + maxdesc + labeladdon);
1287 row->baseline(maxasc + labeladdon);
1289 height += row->height();
1291 row->top_of_text(row->baseline() - font_metrics::maxAscent(font));
1294 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1297 row->width(maxwidth);
1298 prepareToPrint(row, x, dummy, dummy, dummy, false);
1300 row->width(int(maxwidth + x));
1303 width = max(0, workWidth());
1305 if (r->width() > width)
1313 // Appends the implicit specified paragraph before the specified row,
1314 // start at the implicit given position
1315 void LyXText::appendParagraph(Row * row)
1317 pos_type const last = row->par()->size();
1321 pos_type z = rowBreakPoint(*row);
1327 insertRow(row, row->par(), z);
1333 // Set the dimensions of the row
1334 // fixed fill setting now by calling inset->update() in
1335 // SingleWidth when needed!
1336 tmprow->fill(fill(*tmprow, workWidth()));
1337 setHeightOfRow(tmprow);
1343 void LyXText::breakAgain(Row * row)
1345 bool not_ready = true;
1348 pos_type z = rowBreakPoint(*row);
1351 if (z < row->par()->size()) {
1352 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1355 insertRow(row, row->par(), z);
1360 if (row->pos() == z)
1361 not_ready = false; // the rest will not change
1367 // if there are some rows too much, delete them
1368 // only if you broke the whole paragraph!
1369 Row * tmprow2 = row;
1370 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1371 tmprow2 = tmprow2->next();
1373 while (tmprow2 != row) {
1374 tmprow2 = tmprow2->previous();
1375 removeRow(tmprow2->next());
1380 // set the dimensions of the row
1381 tmprow->fill(fill(*tmprow, workWidth()));
1382 setHeightOfRow(tmprow);
1383 } while (not_ready);
1387 // this is just a little changed version of break again
1388 void LyXText::breakAgainOneRow(Row * row)
1390 pos_type z = rowBreakPoint(*row);
1393 if (z < row->par()->size()) {
1395 || (row->next() && row->next()->par() != row->par())) {
1398 insertRow(row, row->par(), z);
1402 if (row->pos() != z)
1406 // if there are some rows too much, delete them
1407 // only if you broke the whole paragraph!
1408 Row * tmprow2 = row;
1409 while (tmprow2->next()
1410 && tmprow2->next()->par() == row->par()) {
1411 tmprow2 = tmprow2->next();
1413 while (tmprow2 != row) {
1414 tmprow2 = tmprow2->previous();
1415 removeRow(tmprow2->next());
1419 // set the dimensions of the row
1420 tmprow->fill(fill(*tmprow, workWidth()));
1421 setHeightOfRow(tmprow);
1425 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1427 // allow only if at start or end, or all previous is new text
1428 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1429 && cursor.par()->isChangeEdited(0, cursor.pos()))
1432 LyXTextClass const & tclass =
1433 bv()->buffer()->params.getLyXTextClass();
1434 LyXLayout_ptr const & layout = cursor.par()->layout();
1436 // this is only allowed, if the current paragraph is not empty or caption
1437 // and if it has not the keepempty flag aktive
1438 if (cursor.par()->empty()
1439 && layout->labeltype != LABEL_SENSITIVE
1440 && !layout->keepempty)
1443 setUndo(bv(), Undo::FINISH, cursor.par(), cursor.par()->next());
1445 // Always break behind a space
1447 // It is better to erase the space (Dekel)
1448 if (cursor.pos() < cursor.par()->size()
1449 && cursor.par()->isLineSeparator(cursor.pos()))
1450 cursor.par()->erase(cursor.pos());
1451 // cursor.pos(cursor.pos() + 1);
1453 // break the paragraph
1457 keep_layout = layout->isEnvironment();
1459 // we need to set this before we insert the paragraph. IMO the
1460 // breakParagraph call should return a bool if it inserts the
1461 // paragraph before or behind and we should react on that one
1462 // but we can fix this in 1.3.0 (Jug 20020509)
1463 bool const isempty = (layout->keepempty && cursor.par()->empty());
1464 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(), cursor.pos(),
1467 // well this is the caption hack since one caption is really enough
1468 if (layout->labeltype == LABEL_SENSITIVE) {
1470 // set to standard-layout
1471 cursor.par()->applyLayout(tclass.defaultLayout());
1473 // set to standard-layout
1474 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1477 // if the cursor is at the beginning of a row without prior newline,
1479 // This touches only the screen-update. Otherwise we would may have
1480 // an empty row on the screen
1481 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1482 && cursor.row()->pos() == cursor.pos())
1487 int y = cursor.y() - cursor.row()->baseline();
1489 // Do not forget the special right address boxes
1490 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1491 Row * r = cursor.row();
1492 while (r->previous() && r->previous()->par() == r->par()) {
1500 removeParagraph(cursor.row());
1502 // set the dimensions of the cursor row
1503 cursor.row()->fill(fill(*cursor.row(), workWidth()));
1505 setHeightOfRow(cursor.row());
1507 #warning Trouble Point! (Lgb)
1508 // When ::breakParagraph is called from within an inset we must
1509 // ensure that the correct ParagraphList is used. Today that is not
1510 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1511 while (!cursor.par()->next()->empty()
1512 && cursor.par()->next()->isNewline(0))
1513 cursor.par()->next()->erase(0);
1515 insertParagraph(cursor.par()->next(), cursor.row());
1519 // This check is necessary. Otherwise the new empty paragraph will
1520 // be deleted automatically. And it is more friendly for the user!
1521 if (cursor.pos() || isempty)
1522 setCursor(cursor.par()->next(), 0);
1524 setCursor(cursor.par(), 0);
1526 if (cursor.row()->next())
1527 breakAgain(cursor.row()->next());
1533 // Just a macro to make some thing easier.
1534 void LyXText::redoParagraph()
1537 redoParagraphs(cursor, cursor.par()->next());
1538 setCursorIntern(cursor.par(), cursor.pos());
1542 // insert a character, moves all the following breaks in the
1543 // same Paragraph one to the right and make a rebreak
1544 void LyXText::insertChar(char c)
1546 setUndo(bv(), Undo::INSERT, cursor.par(), cursor.par()->next());
1548 // When the free-spacing option is set for the current layout,
1549 // disable the double-space checking
1551 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1552 cursor.row()->par()->isFreeSpacing();
1554 if (lyxrc.auto_number) {
1555 static string const number_operators = "+-/*";
1556 static string const number_unary_operators = "+-";
1557 static string const number_seperators = ".,:";
1559 if (current_font.number() == LyXFont::ON) {
1560 if (!IsDigit(c) && !contains(number_operators, c) &&
1561 !(contains(number_seperators, c) &&
1562 cursor.pos() >= 1 &&
1563 cursor.pos() < cursor.par()->size() &&
1564 getFont(bv()->buffer(),
1566 cursor.pos()).number() == LyXFont::ON &&
1567 getFont(bv()->buffer(),
1569 cursor.pos() - 1).number() == LyXFont::ON)
1571 number(bv()); // Set current_font.number to OFF
1572 } else if (IsDigit(c) &&
1573 real_current_font.isVisibleRightToLeft()) {
1574 number(bv()); // Set current_font.number to ON
1576 if (cursor.pos() > 0) {
1577 char const c = cursor.par()->getChar(cursor.pos() - 1);
1578 if (contains(number_unary_operators, c) &&
1579 (cursor.pos() == 1 ||
1580 cursor.par()->isSeparator(cursor.pos() - 2) ||
1581 cursor.par()->isNewline(cursor.pos() - 2))
1583 setCharFont(bv()->buffer(),
1587 } else if (contains(number_seperators, c) &&
1588 cursor.pos() >= 2 &&
1589 getFont(bv()->buffer(),
1591 cursor.pos() - 2).number() == LyXFont::ON) {
1592 setCharFont(bv()->buffer(),
1602 // First check, if there will be two blanks together or a blank at
1603 // the beginning of a paragraph.
1604 // I decided to handle blanks like normal characters, the main
1605 // difference are the special checks when calculating the row.fill
1606 // (blank does not count at the end of a row) and the check here
1608 // The bug is triggered when we type in a description environment:
1609 // The current_font is not changed when we go from label to main text
1610 // and it should (along with realtmpfont) when we type the space.
1611 // CHECK There is a bug here! (Asger)
1613 LyXFont realtmpfont = real_current_font;
1614 LyXFont rawtmpfont = current_font;
1615 // store the current font. This is because of the use of cursor
1616 // movements. The moving cursor would refresh the current font
1618 // Get the font that is used to calculate the baselineskip
1619 pos_type const lastpos = cursor.par()->size();
1620 LyXFont rawparfont =
1621 cursor.par()->getFontSettings(bv()->buffer()->params,
1624 bool jumped_over_space = false;
1626 if (!freeSpacing && IsLineSeparatorChar(c)) {
1627 if ((cursor.pos() > 0
1628 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1629 || (cursor.pos() > 0
1630 && cursor.par()->isNewline(cursor.pos() - 1))
1631 || (cursor.pos() == 0)) {
1632 static bool sent_space_message = false;
1633 if (!sent_space_message) {
1634 if (cursor.pos() == 0)
1635 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1637 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1638 sent_space_message = true;
1645 // the display inset stuff
1646 if (cursor.row()->pos() < cursor.row()->par()->size()
1647 && cursor.row()->par()->isInset(cursor.row()->pos())) {
1648 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1649 if (inset && (inset->display() || inset->needFullRow())) {
1650 // force a new break
1651 cursor.row()->fill(-1); // to force a new break
1655 // get the cursor row fist
1656 Row * row = cursor.row();
1657 int y = cursor.y() - row->baseline();
1658 if (c != Paragraph::META_INSET) {
1659 // Here case LyXText::InsertInset already insertet the character
1660 cursor.par()->insertChar(cursor.pos(), c);
1662 setCharFont(bv()->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1664 if (!jumped_over_space) {
1665 // refresh the positions
1667 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1668 tmprow = tmprow->next();
1669 tmprow->pos(tmprow->pos() + 1);
1673 // Is there a break one row above
1674 if (row->previous() && row->previous()->par() == row->par()
1675 && (cursor.par()->isLineSeparator(cursor.pos())
1676 || cursor.par()->isNewline(cursor.pos())
1677 || ((cursor.pos() + 1 < cursor.par()->size()) &&
1678 cursor.par()->isInset(cursor.pos() + 1))
1679 || cursor.row()->fill() == -1))
1681 pos_type z = rowBreakPoint(*row->previous());
1683 if (z >= row->pos()) {
1686 // set the dimensions of the row above
1687 row->previous()->fill(fill(
1691 setHeightOfRow(row->previous());
1693 y -= row->previous()->height();
1697 breakAgainOneRow(row);
1699 current_font = rawtmpfont;
1700 real_current_font = realtmpfont;
1701 setCursor(cursor.par(), cursor.pos() + 1,
1702 false, cursor.boundary());
1703 // cursor MUST be in row now.
1705 if (row->next() && row->next()->par() == row->par())
1706 need_break_row = row->next();
1710 // check, wether the last characters font has changed.
1711 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1712 && rawparfont != rawtmpfont)
1713 redoHeightOfParagraph();
1720 // recalculate the fill of the row
1721 if (row->fill() >= 0) {
1722 // needed because a newline will set fill to -1. Otherwise
1723 // we would not get a rebreak!
1724 row->fill(fill(*row, workWidth()));
1727 if (c == Paragraph::META_INSET || row->fill() < 0) {
1729 breakAgainOneRow(row);
1730 // will the cursor be in another row now?
1731 if (row->lastPos() <= cursor.pos() + 1 && row->next()) {
1732 if (row->next() && row->next()->par() == row->par())
1733 // this should always be true
1735 breakAgainOneRow(row);
1737 current_font = rawtmpfont;
1738 real_current_font = realtmpfont;
1740 setCursor(cursor.par(), cursor.pos() + 1, false,
1742 if (isBoundary(bv()->buffer(), cursor.par(), cursor.pos())
1743 != cursor.boundary())
1744 setCursor(cursor.par(), cursor.pos(), false,
1745 !cursor.boundary());
1746 if (row->next() && row->next()->par() == row->par())
1747 need_break_row = row->next();
1751 // FIXME: similar code is duplicated all over - make resetHeightOfRow
1752 int const tmpheight = row->height();
1754 setHeightOfRow(row);
1756 if (tmpheight == row->height()) {
1757 postRowPaint(row, y);
1762 current_font = rawtmpfont;
1763 real_current_font = realtmpfont;
1764 setCursor(cursor.par(), cursor.pos() + 1, false,
1768 // check, wether the last characters font has changed.
1769 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1770 && rawparfont != rawtmpfont) {
1771 redoHeightOfParagraph();
1773 // now the special right address boxes
1774 if (cursor.par()->layout()->margintype
1775 == MARGIN_RIGHT_ADDRESS_BOX) {
1776 redoDrawingOfParagraph(cursor);
1784 void LyXText::charInserted()
1786 // Here we could call FinishUndo for every 20 characters inserted.
1787 // This is from my experience how emacs does it.
1788 static unsigned int counter;
1798 void LyXText::prepareToPrint(Row * row, float & x,
1799 float & fill_separator,
1801 float & fill_label_hfill,
1807 float w = row->fill();
1809 fill_label_hfill = 0;
1811 fill_label_hfill = 0;
1814 row->par()->isRightToLeftPar(bv()->buffer()->params);
1816 x = (workWidth() > 0)
1817 ? rightMargin(*bv()->buffer(), *row) : 0;
1819 x = (workWidth() > 0)
1820 ? leftMargin(row) : 0;
1822 // is there a manual margin with a manual label
1823 LyXLayout_ptr const & layout = row->par()->layout();
1825 if (layout->margintype == MARGIN_MANUAL
1826 && layout->labeltype == LABEL_MANUAL) {
1827 /// We might have real hfills in the label part
1828 nlh = row->numberOfLabelHfills();
1830 // A manual label par (e.g. List) has an auto-hfill
1831 // between the label text and the body of the
1833 // But we don't want to do this auto hfill if the par
1835 if (!row->par()->empty())
1838 if (nlh && !row->par()->getLabelWidthString().empty()) {
1839 fill_label_hfill = labelFill(*row) / nlh;
1843 // are there any hfills in the row?
1844 float const nh = row->numberOfHfills();
1848 fill_hfill = w / nh;
1849 // we don't have to look at the alignment if it is ALIGN_LEFT and
1850 // if the row is already larger then the permitted width as then
1851 // we force the LEFT_ALIGN'edness!
1852 } else if (static_cast<int>(row->width()) < workWidth()) {
1853 // is it block, flushleft or flushright?
1854 // set x how you need it
1856 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
1857 align = layout->align;
1859 align = row->par()->params().align();
1862 // center displayed insets
1864 if (row->pos() < row->par()->size()
1865 && row->par()->isInset(row->pos())
1866 && (inset = row->par()->getInset(row->pos()))
1867 && (inset->display())) // || (inset->scroll() < 0)))
1868 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
1869 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1870 // ERT insets should always be LEFT ALIGNED on screen
1871 inset = row->par()->inInset();
1872 if (inset && inset->owner() &&
1873 inset->owner()->lyxCode() == Inset::ERT_CODE)
1875 align = LYX_ALIGN_LEFT;
1879 case LYX_ALIGN_BLOCK:
1880 ns = row->numberOfSeparators();
1881 if (ns && row->next() && row->next()->par() == row->par() &&
1882 !(row->next()->par()->isNewline(row->next()->pos() - 1))
1883 && !(row->next()->par()->isInset(row->next()->pos())
1884 && row->next()->par()->getInset(row->next()->pos())
1885 && row->next()->par()->getInset(row->next()->pos())->display())
1888 fill_separator = w / ns;
1889 } else if (is_rtl) {
1893 case LYX_ALIGN_RIGHT:
1896 case LYX_ALIGN_CENTER:
1904 computeBidiTables(bv()->buffer(), row);
1906 pos_type body_pos = row->par()->beginningOfBody();
1907 pos_type last = row->lastPos();
1910 (body_pos - 1 > last ||
1911 !row->par()->isLineSeparator(body_pos - 1))) {
1912 x += font_metrics::width(layout->labelsep,
1913 getLabelFont(bv()->buffer(), row->par()));
1914 if (body_pos - 1 <= last)
1915 x += fill_label_hfill;
1921 // important for the screen
1924 // the cursor set functions have a special mechanism. When they
1925 // realize, that you left an empty paragraph, they will delete it.
1926 // They also delete the corresponding row
1928 void LyXText::cursorRightOneWord()
1930 // treat floats, HFills and Insets as words
1931 LyXCursor tmpcursor = cursor;
1932 // CHECK See comment on top of text.C
1934 if (tmpcursor.pos() == tmpcursor.par()->size()
1935 && tmpcursor.par()->next()) {
1936 tmpcursor.par(tmpcursor.par()->next());
1941 // Skip through initial nonword stuff.
1942 while (tmpcursor.pos() < tmpcursor.par()->size() &&
1943 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
1944 // printf("Current pos1 %d", tmpcursor.pos()) ;
1945 tmpcursor.pos(tmpcursor.pos() + 1);
1948 // Advance through word.
1949 while (tmpcursor.pos() < tmpcursor.par()->size() &&
1950 tmpcursor.par()->isWord(tmpcursor.pos())) {
1951 // printf("Current pos2 %d", tmpcursor.pos()) ;
1952 tmpcursor.pos(tmpcursor.pos() + 1);
1956 setCursor(tmpcursor.par(), tmpcursor.pos());
1960 void LyXText::cursorTab()
1962 LyXCursor tmpcursor = cursor;
1963 while (tmpcursor.pos() < tmpcursor.par()->size()
1964 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
1965 tmpcursor.pos(tmpcursor.pos() + 1);
1967 if (tmpcursor.pos() == tmpcursor.par()->size()) {
1968 if (tmpcursor.par()->next()) {
1969 tmpcursor.par(tmpcursor.par()->next());
1973 tmpcursor.pos(tmpcursor.pos() + 1);
1974 setCursor(tmpcursor.par(), tmpcursor.pos());
1978 // Skip initial whitespace at end of word and move cursor to *start*
1979 // of prior word, not to end of next prior word.
1980 void LyXText::cursorLeftOneWord()
1982 LyXCursor tmpcursor = cursor;
1983 cursorLeftOneWord(tmpcursor);
1984 setCursor(tmpcursor.par(), tmpcursor.pos());
1988 void LyXText::cursorLeftOneWord(LyXCursor & cur)
1990 // treat HFills, floats and Insets as words
1993 && (cur.par()->isSeparator(cur.pos() - 1)
1994 || cur.par()->isKomma(cur.pos() - 1)
1995 || cur.par()->isNewline(cur.pos() - 1))
1996 && !(cur.par()->isHfill(cur.pos() - 1)
1997 || cur.par()->isInset(cur.pos() - 1)))
1998 cur.pos(cur.pos() - 1);
2001 && (cur.par()->isInset(cur.pos() - 1)
2002 || cur.par()->isHfill(cur.pos() - 1))) {
2003 cur.pos(cur.pos() - 1);
2004 } else if (!cur.pos()) {
2005 if (cur.par()->previous()) {
2006 cur.par(cur.par()->previous());
2007 cur.pos(cur.par()->size());
2009 } else { // Here, cur != 0
2010 while (cur.pos() > 0 &&
2011 cur.par()->isWord(cur.pos() - 1))
2012 cur.pos(cur.pos() - 1);
2017 // Select current word. This depends on behaviour of
2018 // CursorLeftOneWord(), so it is patched as well.
2019 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2020 word_location const loc)
2022 // first put the cursor where we wana start to select the word
2025 case WHOLE_WORD_STRICT:
2026 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2027 || cursor.par()->isSeparator(cursor.pos())
2028 || cursor.par()->isKomma(cursor.pos())
2029 || cursor.par()->isNewline(cursor.pos())
2030 || cursor.par()->isSeparator(cursor.pos() - 1)
2031 || cursor.par()->isKomma(cursor.pos() - 1)
2032 || cursor.par()->isNewline(cursor.pos() - 1)) {
2036 // no break here, we go to the next
2039 // Move cursor to the beginning, when not already there.
2040 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2041 && !(from.par()->isKomma(from.pos() - 1)
2042 || from.par()->isNewline(from.pos() - 1)))
2043 cursorLeftOneWord(from);
2046 // always move the cursor to the beginning of previous word
2047 cursorLeftOneWord(from);
2050 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2056 while (to.pos() < to.par()->size()
2057 && !to.par()->isSeparator(to.pos())
2058 && !to.par()->isKomma(to.pos())
2059 && !to.par()->isNewline(to.pos())
2060 && !to.par()->isHfill(to.pos())
2061 && !to.par()->isInset(to.pos()))
2063 to.pos(to.pos() + 1);
2068 void LyXText::selectWord(word_location loc)
2072 getWord(from, to, loc);
2074 setCursor(from.par(), from.pos());
2077 selection.cursor = cursor;
2078 setCursor(to.par(), to.pos());
2083 // Select the word currently under the cursor when no
2084 // selection is currently set
2085 bool LyXText::selectWordWhenUnderCursor(word_location loc)
2087 if (!selection.set()) {
2089 return selection.set();
2095 void LyXText::acceptChange()
2097 if (!selection.set() && cursor.par()->size())
2102 if (selection.start.par() == selection.end.par()) {
2103 LyXCursor & startc = selection.start;
2104 LyXCursor & endc = selection.end;
2105 setUndo(bv(), Undo::INSERT, startc.par(), startc.par()->next());
2106 startc.par()->acceptChange(startc.pos(), endc.pos());
2109 redoParagraphs(startc, startc.par()->next());
2110 setCursorIntern(startc.par(), 0);
2112 #warning handle multi par selection
2116 void LyXText::rejectChange()
2118 if (!selection.set() && cursor.par()->size())
2123 if (selection.start.par() == selection.end.par()) {
2124 LyXCursor & startc = selection.start;
2125 LyXCursor & endc = selection.end;
2126 setUndo(bv(), Undo::INSERT, startc.par(), startc.par()->next());
2127 startc.par()->rejectChange(startc.pos(), endc.pos());
2130 redoParagraphs(startc, startc.par()->next());
2131 setCursorIntern(startc.par(), 0);
2133 #warning handle multi par selection
2137 // This function is only used by the spellchecker for NextWord().
2138 // It doesn't handle LYX_ACCENTs and probably never will.
2140 LyXText::selectNextWordToSpellcheck(float & value)
2142 if (the_locking_inset) {
2143 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
2144 if (!word.word().empty()) {
2145 value += float(cursor.y());
2146 value /= float(height);
2149 // we have to go on checking so move cursor to the next char
2150 if (cursor.pos() == cursor.par()->size()) {
2151 if (!cursor.par()->next())
2153 cursor.par(cursor.par()->next());
2156 cursor.pos(cursor.pos() + 1);
2158 Paragraph * tmppar = cursor.par();
2160 // If this is not the very first word, skip rest of
2161 // current word because we are probably in the middle
2162 // of a word if there is text here.
2163 if (cursor.pos() || cursor.par()->previous()) {
2164 while (cursor.pos() < cursor.par()->size()
2165 && cursor.par()->isLetter(cursor.pos()))
2166 cursor.pos(cursor.pos() + 1);
2169 // Now, skip until we have real text (will jump paragraphs)
2171 Paragraph * cpar(cursor.par());
2172 pos_type const cpos(cursor.pos());
2174 if (cpos == cpar->size()) {
2176 cursor.par(cpar->next());
2183 bool const is_bad_inset(cpar->isInset(cpos)
2184 && !cpar->getInset(cpos)->allowSpellcheck());
2186 if (cpar->isLetter(cpos) && !isDeletedText(*cpar, cpos)
2190 cursor.pos(cpos + 1);
2193 // now check if we hit an inset so it has to be a inset containing text!
2194 if (cursor.pos() < cursor.par()->size() &&
2195 cursor.par()->isInset(cursor.pos())) {
2197 cursor.par()->getInset(cursor.pos())->edit(bv());
2198 // now call us again to do the above trick
2199 // but obviously we have to start from down below ;)
2200 return bv()->text->selectNextWordToSpellcheck(value);
2203 // Update the value if we changed paragraphs
2204 if (cursor.par() != tmppar) {
2205 setCursor(cursor.par(), cursor.pos());
2206 value = float(cursor.y())/float(height);
2209 // Start the selection from here
2210 selection.cursor = cursor;
2213 getFont(bv()->buffer(), cursor.par(), cursor.pos())
2214 .language()->code());
2215 // and find the end of the word (insets like optional hyphens
2216 // and ligature break are part of a word)
2217 while (cursor.pos() < cursor.par()->size()
2218 && cursor.par()->isLetter(cursor.pos())
2219 && !isDeletedText(*cursor.par(), cursor.pos()))
2220 cursor.pos(cursor.pos() + 1);
2222 // Finally, we copy the word to a string and return it
2224 if (selection.cursor.pos() < cursor.pos()) {
2226 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2227 if (!cursor.par()->isInset(i))
2228 str += cursor.par()->getChar(i);
2231 return WordLangTuple(str, lang_code);
2235 // This one is also only for the spellchecker
2236 void LyXText::selectSelectedWord()
2238 if (the_locking_inset) {
2239 the_locking_inset->selectSelectedWord(bv());
2242 // move cursor to the beginning
2243 setCursor(selection.cursor.par(), selection.cursor.pos());
2245 // set the sel cursor
2246 selection.cursor = cursor;
2248 // now find the end of the word
2249 while (cursor.pos() < cursor.par()->size()
2250 && (cursor.par()->isLetter(cursor.pos())))
2251 cursor.pos(cursor.pos() + 1);
2253 setCursor(cursor.par(), cursor.pos());
2255 // finally set the selection
2260 // Delete from cursor up to the end of the current or next word.
2261 void LyXText::deleteWordForward()
2263 if (cursor.par()->empty())
2266 LyXCursor tmpcursor = cursor;
2267 tmpcursor.row(0); //Â ??
2268 selection.set(true); // to avoid deletion
2269 cursorRightOneWord();
2270 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2271 selection.cursor = cursor;
2275 // Great, CutSelection() gets rid of multiple spaces.
2276 cutSelection(true, false);
2281 // Delete from cursor to start of current or prior word.
2282 void LyXText::deleteWordBackward()
2284 if (cursor.par()->empty())
2287 LyXCursor tmpcursor = cursor;
2288 tmpcursor.row(0); // ??
2289 selection.set(true); // to avoid deletion
2290 cursorLeftOneWord();
2291 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2292 selection.cursor = cursor;
2295 cutSelection(true, false);
2300 // Kill to end of line.
2301 void LyXText::deleteLineForward()
2303 if (cursor.par()->empty())
2304 // Paragraph is empty, so we just go to the right
2307 LyXCursor tmpcursor = cursor;
2308 // We can't store the row over a regular setCursor
2309 // so we set it to 0 and reset it afterwards.
2310 tmpcursor.row(0); //Â ??
2311 selection.set(true); // to avoid deletion
2313 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2314 selection.cursor = cursor;
2317 // What is this test for ??? (JMarc)
2318 if (!selection.set()) {
2319 deleteWordForward();
2321 cutSelection(true, false);
2327 void LyXText::changeCase(LyXText::TextCase action)
2332 if (selection.set()) {
2333 from = selection.start;
2336 getWord(from, to, PARTIAL_WORD);
2337 setCursor(to.par(), to.pos() + 1);
2340 lyx::Assert(from <= to);
2342 setUndo(bv(), Undo::FINISH, from.par(), to.par()->next());
2344 pos_type pos = from.pos();
2345 Paragraph * par = from.par();
2347 while (par && (pos != to.pos() || par != to.par())) {
2348 if (pos == par->size()) {
2353 unsigned char c = par->getChar(pos);
2354 if (!IsInsetChar(c)) {
2356 case text_lowercase:
2359 case text_capitalization:
2361 action = text_lowercase;
2363 case text_uppercase:
2369 par->setChar(pos, c);
2370 checkParagraph(par, pos);
2375 if (to.row() != from.row())
2376 postPaint(from.y() - from.row()->baseline());
2380 void LyXText::transposeChars()
2382 Paragraph * tmppar = cursor.par();
2384 setUndo(bv(), Undo::FINISH, tmppar, tmppar->next());
2386 pos_type tmppos = cursor.pos();
2388 // First decide if it is possible to transpose at all
2390 if (tmppos == 0 || tmppos == tmppar->size())
2393 if (isDeletedText(*tmppar, tmppos - 1)
2394 || isDeletedText(*tmppar, tmppos))
2397 unsigned char c1 = tmppar->getChar(tmppos);
2398 unsigned char c2 = tmppar->getChar(tmppos - 1);
2400 // We should have an implementation that handles insets
2401 // as well, but that will have to come later. (Lgb)
2402 if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
2405 bool const erased = tmppar->erase(tmppos - 1, tmppos + 1);
2406 pos_type const ipos(erased ? tmppos - 1 : tmppos + 1);
2408 tmppar->insertChar(ipos, c1);
2409 tmppar->insertChar(ipos + 1, c2);
2411 checkParagraph(tmppar, tmppos);
2415 void LyXText::Delete()
2417 // this is a very easy implementation
2419 LyXCursor old_cursor = cursor;
2420 int const old_cur_par_id = old_cursor.par()->id();
2421 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2422 old_cursor.par()->previous()->id() : -1;
2424 // just move to the right
2427 // CHECK Look at the comment here.
2428 // This check is not very good...
2429 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2430 // and that can very well delete the par or par->previous in
2431 // old_cursor. Will a solution where we compare paragraph id's
2433 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : -1)
2434 == old_cur_par_prev_id
2435 && cursor.par()->id() != old_cur_par_id) {
2436 // delete-empty-paragraph-mechanism has done it
2440 // if you had success make a backspace
2441 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2442 LyXCursor tmpcursor = cursor;
2443 // to make sure undo gets the right cursor position
2444 cursor = old_cursor;
2445 setUndo(bv(), Undo::DELETE,
2446 cursor.par(), cursor.par()->next());
2453 void LyXText::backspace()
2455 // Get the font that is used to calculate the baselineskip
2456 pos_type lastpos = cursor.par()->size();
2457 LyXFont rawparfont =
2458 cursor.par()->getFontSettings(bv()->buffer()->params,
2461 if (cursor.pos() == 0) {
2462 // The cursor is at the beginning of a paragraph,
2463 // so the the backspace will collapse two paragraphs into one.
2465 // but it's not allowed unless it's new
2466 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2469 // we may paste some paragraphs
2471 // is it an empty paragraph?
2474 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2475 // This is an empty paragraph and we delete it just by moving the cursor one step
2476 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2477 // of the paragraph.
2479 if (cursor.par()->previous()) {
2480 Paragraph * tmppar = cursor.par()->previous();
2481 if (cursor.par()->layout() == tmppar->layout()
2482 && cursor.par()->getAlign() == tmppar->getAlign()) {
2483 // Inherit bottom DTD from the paragraph below.
2484 // (the one we are deleting)
2485 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2486 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2487 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2492 // the layout things can change the height of a row !
2493 int const tmpheight = cursor.row()->height();
2494 setHeightOfRow(cursor.row());
2495 if (cursor.row()->height() != tmpheight) {
2496 postPaint(cursor.y() - cursor.row()->baseline());
2502 if (cursor.par()->previous()) {
2503 setUndo(bv(), Undo::DELETE,
2504 cursor.par()->previous(), cursor.par()->next());
2507 Paragraph * tmppar = cursor.par();
2508 Row * tmprow = cursor.row();
2510 // We used to do cursorLeftIntern() here, but it is
2511 // not a good idea since it triggers the auto-delete
2512 // mechanism. So we do a cursorLeftIntern()-lite,
2513 // without the dreaded mechanism. (JMarc)
2514 if (cursor.par()->previous()) {
2515 // steps into the above paragraph.
2516 setCursorIntern(cursor.par()->previous(),
2517 cursor.par()->previous()->size(),
2521 // Pasting is not allowed, if the paragraphs have different
2522 // layout. I think it is a real bug of all other
2523 // word processors to allow it. It confuses the user.
2524 // Even so with a footnote paragraph and a non-footnote
2525 // paragraph. I will not allow pasting in this case,
2526 // because the user would be confused if the footnote behaves
2527 // different wether it is open or closed.
2529 // Correction: Pasting is always allowed with standard-layout
2530 LyXTextClass const & tclass =
2531 bv()->buffer()->params.getLyXTextClass();
2533 if (cursor.par() != tmppar
2534 && (cursor.par()->layout() == tmppar->layout()
2535 || tmppar->layout() == tclass.defaultLayout())
2536 && cursor.par()->getAlign() == tmppar->getAlign()) {
2537 removeParagraph(tmprow);
2539 mergeParagraph(bv()->buffer()->params, bv()->buffer()->paragraphs, cursor.par());
2541 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2542 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2543 // strangely enough it seems that commenting out the line above removes
2544 // most or all of the segfaults. I will however also try to move the
2545 // two Remove... lines in front of the PasteParagraph too.
2548 cursor.pos(cursor.pos() - 1);
2550 postPaint(cursor.y() - cursor.row()->baseline());
2552 // remove the lost paragraph
2553 // This one is not safe, since the paragraph that the tmprow and the
2554 // following rows belong to has been deleted by the PasteParagraph
2555 // above. The question is... could this be moved in front of the
2557 //RemoveParagraph(tmprow);
2558 //RemoveRow(tmprow);
2560 // This rebuilds the rows.
2561 appendParagraph(cursor.row());
2564 // the row may have changed, block, hfills etc.
2565 setCursor(cursor.par(), cursor.pos(), false);
2568 // this is the code for a normal backspace, not pasting
2570 setUndo(bv(), Undo::DELETE,
2571 cursor.par(), cursor.par()->next());
2572 // We used to do cursorLeftIntern() here, but it is
2573 // not a good idea since it triggers the auto-delete
2574 // mechanism. So we do a cursorLeftIntern()-lite,
2575 // without the dreaded mechanism. (JMarc)
2576 setCursorIntern(cursor.par(), cursor.pos()- 1,
2577 false, cursor.boundary());
2579 if (cursor.par()->isInset(cursor.pos())) {
2580 // force complete redo when erasing display insets
2581 // this is a cruel method but safe..... Matthias
2582 if (cursor.par()->getInset(cursor.pos())->display() ||
2583 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2584 cursor.par()->erase(cursor.pos());
2590 Row * row = cursor.row();
2591 int y = cursor.y() - row->baseline();
2593 // remember that a space at the end of a row doesnt count
2594 // when calculating the fill
2595 if (cursor.pos() < row->lastPos() ||
2596 !cursor.par()->isLineSeparator(cursor.pos())) {
2597 row->fill(row->fill() + singleWidth(
2602 // some special code when deleting a newline. This is similar
2603 // to the behavior when pasting paragraphs
2604 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2605 cursor.par()->erase(cursor.pos());
2606 // refresh the positions
2608 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2609 tmprow = tmprow->next();
2610 tmprow->pos(tmprow->pos() - 1);
2612 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2613 cursor.pos(cursor.pos() - 1);
2615 if (cursor.pos() < cursor.par()->size()
2616 && !cursor.par()->isSeparator(cursor.pos())) {
2617 cursor.par()->insertChar(cursor.pos(), ' ');
2618 setCharFont(bv()->buffer(), cursor.par(),
2619 cursor.pos(), current_font);
2620 // refresh the positions
2622 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2623 tmprow = tmprow->next();
2624 tmprow->pos(tmprow->pos() + 1);
2628 cursor.par()->erase(cursor.pos());
2630 // refresh the positions
2632 while (tmprow->next()
2633 && tmprow->next()->par() == row->par()) {
2634 tmprow = tmprow->next();
2635 tmprow->pos(tmprow->pos() - 1);
2638 // delete newlines at the beginning of paragraphs
2639 while (!cursor.par()->empty() &&
2640 cursor.pos() < cursor.par()->size() &&
2641 cursor.par()->isNewline(cursor.pos()) &&
2642 cursor.pos() == cursor.par()->beginningOfBody()) {
2643 cursor.par()->erase(cursor.pos());
2644 // refresh the positions
2646 while (tmprow->next() &&
2647 tmprow->next()->par() == row->par()) {
2648 tmprow = tmprow->next();
2649 tmprow->pos(tmprow->pos() - 1);
2654 // is there a break one row above
2655 if (row->previous() && row->previous()->par() == row->par()) {
2656 z = rowBreakPoint(*row->previous());
2657 if (z >= row->pos()) {
2660 Row * tmprow = row->previous();
2662 // maybe the current row is now empty
2663 if (row->pos() >= row->par()->size()) {
2668 breakAgainOneRow(row);
2669 if (row->next() && row->next()->par() == row->par())
2670 need_break_row = row->next();
2675 // set the dimensions of the row above
2676 y -= tmprow->height();
2677 tmprow->fill(fill(*tmprow, workWidth()));
2678 setHeightOfRow(tmprow);
2682 setCursor(cursor.par(), cursor.pos(),
2683 false, cursor.boundary());
2684 //current_font = rawtmpfont;
2685 //real_current_font = realtmpfont;
2686 // check, whether the last character's font has changed.
2688 cursor.par()->getFontSettings(bv()->buffer()->params,
2689 cursor.par()->size() - 1))
2690 redoHeightOfParagraph();
2695 // break the cursor row again
2696 if (row->next() && row->next()->par() == row->par() &&
2697 (row->lastPos() == row->par()->size() - 1 ||
2698 rowBreakPoint(*row) != row->lastPos())) {
2700 // it can happen that a paragraph loses one row
2701 // without a real breakup. This is when a word
2702 // is to long to be broken. Well, I don t care this
2704 if (row->lastPos() == row->par()->size() - 1)
2705 removeRow(row->next());
2709 breakAgainOneRow(row);
2710 // will the cursor be in another row now?
2711 if (row->next() && row->next()->par() == row->par() &&
2712 row->lastPos() <= cursor.pos()) {
2714 breakAgainOneRow(row);
2717 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2719 if (row->next() && row->next()->par() == row->par())
2720 need_break_row = row->next();
2724 // set the dimensions of the row
2725 row->fill(fill(*row, workWidth()));
2726 int const tmpheight = row->height();
2727 setHeightOfRow(row);
2728 if (tmpheight == row->height()) {
2729 postRowPaint(row, y);
2733 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2737 // current_font = rawtmpfont;
2738 // real_current_font = realtmpfont;
2740 if (isBoundary(bv()->buffer(), cursor.par(), cursor.pos())
2741 != cursor.boundary())
2742 setCursor(cursor.par(), cursor.pos(), false,
2743 !cursor.boundary());
2745 lastpos = cursor.par()->size();
2746 if (cursor.pos() == lastpos)
2749 // check, whether the last characters font has changed.
2751 cursor.par()->getFontSettings(bv()->buffer()->params, lastpos - 1)) {
2752 redoHeightOfParagraph();
2754 // now the special right address boxes
2755 if (cursor.par()->layout()->margintype
2756 == MARGIN_RIGHT_ADDRESS_BOX) {
2757 redoDrawingOfParagraph(cursor);
2763 // returns pointer to a specified row
2764 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
2769 Row * tmprow = firstrow;
2772 // find the first row of the specified paragraph
2773 while (tmprow->next() && tmprow->par() != par) {
2774 y += tmprow->height();
2775 tmprow = tmprow->next();
2778 // now find the wanted row
2779 while (tmprow->pos() < pos
2781 && tmprow->next()->par() == par
2782 && tmprow->next()->pos() <= pos) {
2783 y += tmprow->height();
2784 tmprow = tmprow->next();
2791 Row * LyXText::getRowNearY(int & y) const
2794 // If possible we should optimize this method. (Lgb)
2795 Row * tmprow = firstrow;
2798 while (tmprow->next() && tmpy + tmprow->height() <= y) {
2799 tmpy += tmprow->height();
2800 tmprow = tmprow->next();
2803 y = tmpy; // return the real y
2805 //lyxerr << "returned y = " << y << endl;
2809 // Search from the current cursor position.
2811 Row * tmprow = cursor.row();
2812 int tmpy = cursor.y() - tmprow->baseline();
2814 lyxerr << "cursor.y() = " << tmpy << endl;
2815 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
2816 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
2817 lyxerr << "first = " << first << endl;
2818 lyxerr << "y = " << y << endl;
2821 lyxerr << "up" << endl;
2823 tmpy -= tmprow->height();
2824 tmprow = tmprow->previous();
2825 } while (tmprow && tmpy - tmprow->height() >= y);
2826 } else if (y > tmpy) {
2827 lyxerr << "down" << endl;
2829 while (tmprow->next() && tmpy + tmprow->height() <= y) {
2830 tmpy += tmprow->height();
2831 tmprow = tmprow->next();
2834 lyxerr << "equal" << endl;
2837 y = tmpy; // return the real y
2839 lyxerr << "returned y = " << y << endl;
2847 int LyXText::getDepth() const
2849 return cursor.par()->getDepth();