3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
22 #include "bufferparams.h"
23 #include "BufferView.h"
26 #include "funcrequest.h"
30 #include "lyxlength.h"
33 #include "lyxrow_funcs.h"
34 #include "metricsinfo.h"
35 #include "paragraph.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
38 #include "rowpainter.h"
39 #include "text_funcs.h"
42 #include "WordLangTuple.h"
44 #include "frontends/font_metrics.h"
45 #include "frontends/LyXView.h"
47 #include "insets/insettext.h"
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
52 using bv_funcs::number;
55 using lyx::word_location;
57 using lyx::support::contains;
58 using lyx::support::lowercase;
59 using lyx::support::uppercase;
66 /// top, right, bottom pixel margin
67 extern int const PAPER_MARGIN = 20;
68 /// margin for changebar
69 extern int const CHANGEBAR_MARGIN = 10;
71 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
75 int bibitemMaxWidth(BufferView *, LyXFont const &);
80 unsigned int maxParagraphWidth(ParagraphList const & plist)
82 unsigned int width = 0;
83 ParagraphList::const_iterator pit = plist.begin();
84 ParagraphList::const_iterator end = plist.end();
85 for (; pit != end; ++pit)
86 width = std::max(width, pit->width);
93 BufferView * LyXText::bv()
95 BOOST_ASSERT(bv_owner != 0);
100 BufferView * LyXText::bv() const
102 BOOST_ASSERT(bv_owner != 0);
107 void LyXText::updateRowPositions()
109 ParagraphList::iterator pit = ownerParagraphs().begin();
110 ParagraphList::iterator end = ownerParagraphs().end();
111 for (height = 0; pit != end; ++pit) {
113 height += pit->height;
118 int LyXText::workWidth() const
120 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
124 int LyXText::getRealCursorX() const
127 if (the_locking_inset && the_locking_inset->getLyXText(bv()) != this)
128 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
133 #warning FIXME This function seems to belong outside of LyxText.
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
137 if (!Encodings::is_arabic(c))
138 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139 return c + (0xb0 - '0');
143 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
144 unsigned char next_char = ' ';
146 pos_type const par_size = par.size();
148 for (pos_type i = pos + 1; i < par_size; ++i) {
149 unsigned char const par_char = par.getChar(i);
150 if (!Encodings::IsComposeChar_arabic(par_char)) {
151 next_char = par_char;
156 if (Encodings::is_arabic(next_char)) {
157 if (Encodings::is_arabic(prev_char) &&
158 !Encodings::is_arabic_special(prev_char))
159 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
161 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
163 if (Encodings::is_arabic(prev_char) &&
164 !Encodings::is_arabic_special(prev_char))
165 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
167 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
171 // This is the comments that some of the warnings below refers to.
172 // There are some issues in this file and I don't think they are
173 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
174 // this is a problem that has been here almost from day one and that a
175 // larger userbase with different access patters triggers the bad
176 // behaviour. (segfaults.) What I think happen is: In several places
177 // we store the paragraph in the current cursor and then moves the
178 // cursor. This movement of the cursor will delete paragraph at the
179 // old position if it is now empty. This will make the temporary
180 // pointer to the old cursor paragraph invalid and dangerous to use.
181 // And is some cases this will trigger a segfault. I have marked some
182 // of the cases where this happens with a warning, but I am sure there
183 // are others in this file and in text2.C. There is also a note in
184 // Delete() that you should read. In Delete I store the paragraph->id
185 // instead of a pointer to the paragraph. I am pretty sure this faulty
186 // use of temporary pointers to paragraphs that might have gotten
187 // invalidated (through a cursor movement) before they are used, are
188 // the cause of the strange crashes we get reported often.
190 // It is very tiresom to change this code, especially when it is as
191 // hard to read as it is. Help to fix all the cases where this is done
192 // would be greately appreciated.
196 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
198 if (pos >= pit->size())
201 char const c = pit->getChar(pos);
202 LyXFont const & font = getFont(pit, pos);
203 return singleWidth(pit, pos, c, font);
207 int LyXText::singleWidth(ParagraphList::iterator pit,
208 pos_type pos, char c, LyXFont const & font) const
210 if (pos >= pit->size()) {
211 lyxerr << "in singleWidth(), pos: " << pos << endl;
217 // The most common case is handled first (Asger)
218 if (IsPrintable(c)) {
219 if (!font.language()->RightToLeft()) {
220 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
221 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
222 && font.language()->lang() == "arabic") {
223 if (Encodings::IsComposeChar_arabic(c))
226 c = transformChar(c, *pit, pos);
227 } else if (font.language()->lang() == "hebrew" &&
228 Encodings::IsComposeChar_hebrew(c))
231 return font_metrics::width(c, font);
234 if (c == Paragraph::META_INSET) {
235 InsetOld * tmpinset = pit->getInset(pos);
236 BOOST_ASSERT(tmpinset);
237 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
238 // Because of the representation as vertical lines
241 return tmpinset->width();
244 if (IsSeparatorChar(c))
246 return font_metrics::width(c, font);
250 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
252 LyXTextClass const & tclass =
253 bv()->buffer()->params().getLyXTextClass();
254 LyXLayout_ptr const & layout = pit->layout();
256 string parindent = layout->parindent;
260 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
262 // this is the way, LyX handles the LaTeX-Environments.
263 // I have had this idea very late, so it seems to be a
264 // later added hack and this is true
265 if (!pit->getDepth()) {
266 if (pit->layout() == tclass.defaultLayout()) {
267 // find the previous same level paragraph
268 if (pit != ownerParagraphs().begin()) {
269 ParagraphList::iterator newpit =
270 depthHook(pit, ownerParagraphs(),
273 newpit->layout()->nextnoindent)
278 // find the next level paragraph
280 ParagraphList::iterator newpar = outerHook(pit,
283 // make a corresponding row. Needed to call leftMargin()
285 // check wether it is a sufficent paragraph
286 if (newpar != ownerParagraphs().end() &&
287 newpar->layout()->isEnvironment()) {
288 x = leftMargin(newpar, Row(newpar->size()));
291 if (newpar != ownerParagraphs().end() &&
292 pit->layout() == tclass.defaultLayout()) {
293 if (newpar->params().noindent())
296 parindent = newpar->layout()->parindent;
302 LyXFont const labelfont = getLabelFont(pit);
303 switch (layout->margintype) {
305 if (!layout->leftmargin.empty()) {
306 x += font_metrics::signedWidth(layout->leftmargin,
307 tclass.defaultfont());
309 if (!pit->getLabelstring().empty()) {
310 x += font_metrics::signedWidth(layout->labelindent,
312 x += font_metrics::width(pit->getLabelstring(),
314 x += font_metrics::width(layout->labelsep, labelfont);
318 x += font_metrics::signedWidth(layout->labelindent, labelfont);
319 // The width of an empty par, even with manual label, should be 0
320 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
321 if (!pit->getLabelWidthString().empty()) {
322 x += font_metrics::width(pit->getLabelWidthString(),
324 x += font_metrics::width(layout->labelsep, labelfont);
329 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
330 / (pit->getDepth() + 4);
332 case MARGIN_FIRST_DYNAMIC:
333 if (layout->labeltype == LABEL_MANUAL) {
334 if (row.pos() >= pit->beginningOfBody()) {
335 x += font_metrics::signedWidth(layout->leftmargin,
338 x += font_metrics::signedWidth(layout->labelindent,
342 // Special case to fix problems with
344 || (layout->labeltype == LABEL_STATIC
345 && layout->latextype == LATEX_ENVIRONMENT
346 && !isFirstInSequence(pit, ownerParagraphs()))) {
347 x += font_metrics::signedWidth(layout->leftmargin,
349 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
350 && layout->labeltype != LABEL_BIBLIO
351 && layout->labeltype !=
352 LABEL_CENTERED_TOP_ENVIRONMENT) {
353 x += font_metrics::signedWidth(layout->labelindent,
355 x += font_metrics::width(layout->labelsep, labelfont);
356 x += font_metrics::width(pit->getLabelstring(),
361 case MARGIN_RIGHT_ADDRESS_BOX:
363 // ok, a terrible hack. The left margin depends on the widest
364 // row in this paragraph.
365 RowList::iterator rit = pit->rows.begin();
366 RowList::iterator end = pit->rows.end();
367 #warning This is wrong.
368 int minfill = workWidth() / 2;
369 for ( ; rit != end; ++rit)
370 if (rit->fill() < minfill)
371 minfill = rit->fill();
372 x += font_metrics::signedWidth(layout->leftmargin,
373 tclass.defaultfont());
379 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
380 LyXLength const len = pit->params().leftIndent();
381 int const tw = inset_owner ?
382 inset_owner->latexTextWidth(bv()) : workWidth();
383 x += len.inPixels(tw);
388 if (pit->params().align() == LYX_ALIGN_LAYOUT)
389 align = layout->align;
391 align = pit->params().align();
393 // set the correct parindent
394 if (row.pos() == 0) {
395 if ((layout->labeltype == LABEL_NO_LABEL
396 || layout->labeltype == LABEL_TOP_ENVIRONMENT
397 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
398 || (layout->labeltype == LABEL_STATIC
399 && layout->latextype == LATEX_ENVIRONMENT
400 && !isFirstInSequence(pit, ownerParagraphs())))
401 && align == LYX_ALIGN_BLOCK
402 && !pit->params().noindent()
403 // in tabulars and ert paragraphs are never indented!
404 && (!pit->inInset() || !pit->inInset()->owner() ||
405 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
406 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
407 && (pit->layout() != tclass.defaultLayout() ||
408 bv()->buffer()->params().paragraph_separation ==
409 BufferParams::PARSEP_INDENT)) {
410 x += font_metrics::signedWidth(parindent,
411 tclass.defaultfont());
412 } else if (layout->labeltype == LABEL_BIBLIO) {
413 // ale970405 Right width for bibitems
414 x += bibitemMaxWidth(bv(), tclass.defaultfont());
422 int LyXText::rightMargin(Paragraph const & par,
423 Buffer const & buf, Row const &) const
425 LyXTextClass const & tclass = buf.params().getLyXTextClass();
426 LyXLayout_ptr const & layout = par.layout();
429 + font_metrics::signedWidth(tclass.rightmargin(),
430 tclass.defaultfont())
431 + font_metrics::signedWidth(layout->rightmargin,
432 tclass.defaultfont())
433 * 4 / (par.getDepth() + 4);
437 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
439 if (pit->layout()->margintype == MARGIN_MANUAL) {
441 tmprow.pos(pit->size());
442 // return the beginning of the body
443 return leftMargin(pit, tmprow);
446 // LabelEnd is only needed if the layout
447 // fills a flushleft label.
454 // this needs special handling - only newlines count as a break point
455 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
457 for (; i < par.size(); ++i)
458 if (par.isNewline(i))
467 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
470 row.endpos(pit->size());
474 // maximum pixel width of a row.
475 int width = workWidth()
476 - rightMargin(*pit, *bv()->buffer(), row)
477 - leftMargin(pit, row);
479 // inset->textWidth() returns -1 via workWidth(),
482 row.endpos(pit->size());
486 LyXLayout_ptr const & layout = pit->layout();
488 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
489 row.endpos(addressBreakPoint(row.pos(), *pit));
493 pos_type const pos = row.pos();
494 pos_type const body_pos = pit->beginningOfBody();
495 pos_type const end = pit->size();
496 pos_type point = end - 1;
503 // Now we iterate through until we reach the right margin
504 // or the end of the par, then choose the possible break
507 int const left = leftMargin(pit, row);
510 // pixel width since last breakpoint
515 // We re-use the font resolution for the entire font span when possible
516 LyXFont font = getFont(pit, i);
517 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
519 for ( ; i < end; ++i) {
520 if (pit->isNewline(i)) {
526 InsetOld * in = pit->getInset(i + 1);
527 if (in && in->display()) {
532 in = pit->getInset(i);
533 if (in && in->display()) {
539 char const c = pit->getChar(i);
540 if (i > endPosOfFontSpan) {
541 font = getFont(pit, i);
542 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
547 // add the auto-hfill from label end to the body
548 if (body_pos && i == body_pos) {
549 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
550 if (pit->isLineSeparator(i - 1))
551 thiswidth -= singleWidth(pit, i - 1);
552 int left_margin = labelEnd(pit, row);
553 if (thiswidth + x < left_margin)
554 thiswidth = left_margin - x;
555 thiswidth += singleWidth(pit, i, c, font);
557 thiswidth = singleWidth(pit, i, c, font);
561 //lyxerr << "i: " << i << " x: " << x << " width: " << width << endl;
562 chunkwidth += thiswidth;
564 // break before a character that will fall off
565 // the right of the row
567 // if no break before, break here
568 if (point == end || chunkwidth >= width - left) {
571 // exit on last registered breakpoint:
580 InsetOld * in = pit->getInset(i);
581 if (!in || in->isChar()) {
582 // some insets are line separators too
583 if (pit->isLineSeparator(i)) {
584 // register breakpoint:
591 if (point == end && x >= width) {
592 // didn't find one, break at the point we reached the edge
594 } else if (i == end && x < width) {
595 // found one, but we fell off the end of the par, so prefer
600 // manual labels cannot be broken in LaTeX. But we
601 // want to make our on-screen rendering of footnotes
603 if (body_pos && point < body_pos)
604 point = body_pos - 1;
606 row.endpos(point + 1);
610 // returns the minimum space a row needs on the screen in pixel
611 void LyXText::fill(ParagraphList::iterator pit, Row & row, int paper_width) const
614 // get the pure distance
615 pos_type const last = lastPos(*pit, row);
617 LyXLayout_ptr const & layout = pit->layout();
619 // special handling of the right address boxes
620 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
621 int const tmpfill = row.fill();
622 row.fill(0); // the minfill in leftMargin()
623 w = leftMargin(pit, row);
626 w = leftMargin(pit, row);
629 pos_type const body_pos = pit->beginningOfBody();
630 pos_type i = row.pos();
632 if (! pit->empty() && i <= last) {
633 // We re-use the font resolution for the entire span when possible
634 LyXFont font = getFont(pit, i);
635 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
637 if (body_pos > 0 && i == body_pos) {
638 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
639 if (pit->isLineSeparator(i - 1))
640 w -= singleWidth(pit, i - 1);
641 int left_margin = labelEnd(pit, row);
645 char const c = pit->getChar(i);
646 if (IsPrintable(c) && i > endPosOfFontSpan) {
647 // We need to get the next font
648 font = getFont(pit, i);
649 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
651 w += singleWidth(pit, i, c, font);
655 if (body_pos > 0 && body_pos > last) {
656 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
657 if (last >= 0 && pit->isLineSeparator(last))
658 w -= singleWidth(pit, last);
659 int const left_margin = labelEnd(pit, row);
664 int const fill = paper_width - w - rightMargin(*pit, *bv()->buffer(), row);
666 row.width(paper_width - fill);
670 // returns the minimum space a manual label needs on the screen in pixel
671 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
673 pos_type last = pit->beginningOfBody();
675 BOOST_ASSERT(last > 0);
677 // -1 because a label ends with a space that is in the label
680 // a separator at this end does not count
681 if (pit->isLineSeparator(last))
685 for (pos_type i = row.pos(); i <= last; ++i)
686 w += singleWidth(pit, i);
689 string const & labwidstr = pit->params().labelWidthString();
690 if (!labwidstr.empty()) {
691 LyXFont const labfont = getLabelFont(pit);
692 int const labwidth = font_metrics::width(labwidstr, labfont);
693 fill = max(labwidth - w, 0);
700 LColor_color LyXText::backgroundColor() const
703 return inset_owner->backgroundColor();
705 return LColor::background;
709 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
711 // get the maximum ascent and the maximum descent
712 double layoutasc = 0;
713 double layoutdesc = 0;
716 // ok, let us initialize the maxasc and maxdesc value.
717 // Only the fontsize count. The other properties
718 // are taken from the layoutfont. Nicer on the screen :)
719 LyXLayout_ptr const & layout = pit->layout();
721 // as max get the first character of this row then it can increase but not
722 // decrease the height. Just some point to start with so we don't have to
723 // do the assignment below too often.
724 LyXFont font = getFont(pit, row.pos());
725 LyXFont::FONT_SIZE const tmpsize = font.size();
726 font = getLayoutFont(pit);
727 LyXFont::FONT_SIZE const size = font.size();
728 font.setSize(tmpsize);
730 LyXFont labelfont = getLabelFont(pit);
732 double spacing_val = 1.0;
733 if (!pit->params().spacing().isDefault())
734 spacing_val = pit->params().spacing().getValue();
736 spacing_val = bv()->buffer()->params().spacing().getValue();
737 //lyxerr << "spacing_val = " << spacing_val << endl;
739 // these are minimum values
740 int maxasc = int(font_metrics::maxAscent(font) *
741 layout->spacing.getValue() * spacing_val);
742 int maxdesc = int(font_metrics::maxDescent(font) *
743 layout->spacing.getValue() * spacing_val);
745 // insets may be taller
746 InsetList::iterator ii = pit->insetlist.begin();
747 InsetList::iterator iend = pit->insetlist.end();
748 for ( ; ii != iend; ++ii) {
749 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
750 maxasc = max(maxasc, ii->inset->ascent());
751 maxdesc = max(maxdesc, ii->inset->descent());
755 // Check if any custom fonts are larger (Asger)
756 // This is not completely correct, but we can live with the small,
757 // cosmetic error for now.
759 pos_type const pos_end = lastPos(*pit, row);
761 LyXFont::FONT_SIZE maxsize =
762 pit->highestFontInRange(row.pos(), pos_end, size);
763 if (maxsize > font.size()) {
764 font.setSize(maxsize);
765 maxasc = max(maxasc, font_metrics::maxAscent(font));
766 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
769 // This is nicer with box insets:
773 row.ascent_of_text(maxasc);
777 BufferParams const & bufparams = bv()->buffer()->params();
778 // some parksips VERY EASY IMPLEMENTATION
779 if (bv()->buffer()->params().paragraph_separation ==
780 BufferParams::PARSEP_SKIP)
782 if (layout->isParagraph()
783 && pit->getDepth() == 0
784 && pit != ownerParagraphs().begin())
786 maxasc += bufparams.getDefSkip().inPixels(*bv());
787 } else if (pit != ownerParagraphs().begin() &&
788 boost::prior(pit)->layout()->isParagraph() &&
789 boost::prior(pit)->getDepth() == 0)
791 // is it right to use defskip here too? (AS)
792 maxasc += bufparams.getDefSkip().inPixels(*bv());
797 if (pit == ownerParagraphs().begin() && !isInInset())
798 maxasc += PAPER_MARGIN;
800 // add the vertical spaces, that the user added
801 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
803 // do not forget the DTP-lines!
804 // there height depends on the font of the nearest character
805 if (pit->params().lineTop())
807 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
808 // and now the pagebreaks
809 if (pit->params().pagebreakTop())
810 maxasc += 3 * defaultRowHeight();
812 if (pit->params().startOfAppendix())
813 maxasc += 3 * defaultRowHeight();
815 // This is special code for the chapter, since the label of this
816 // layout is printed in an extra row
817 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
818 float spacing_val = 1.0;
819 if (!pit->params().spacing().isDefault()) {
820 spacing_val = pit->params().spacing().getValue();
822 spacing_val = bufparams.spacing().getValue();
825 labeladdon = int(font_metrics::maxDescent(labelfont) *
826 layout->spacing.getValue() *
828 + int(font_metrics::maxAscent(labelfont) *
829 layout->spacing.getValue() *
833 // special code for the top label
834 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
835 || layout->labeltype == LABEL_BIBLIO
836 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
837 && isFirstInSequence(pit, ownerParagraphs())
838 && !pit->getLabelstring().empty())
840 float spacing_val = 1.0;
841 if (!pit->params().spacing().isDefault()) {
842 spacing_val = pit->params().spacing().getValue();
844 spacing_val = bufparams.spacing().getValue();
848 (font_metrics::maxAscent(labelfont) +
849 font_metrics::maxDescent(labelfont)) *
850 layout->spacing.getValue() *
852 + layout->topsep * defaultRowHeight()
853 + layout->labelbottomsep * defaultRowHeight());
856 // And now the layout spaces, for example before and after
857 // a section, or between the items of a itemize or enumerate
860 if (!pit->params().pagebreakTop()) {
861 ParagraphList::iterator prev =
862 depthHook(pit, ownerParagraphs(),
864 if (prev != pit && prev->layout() == layout &&
865 prev->getDepth() == pit->getDepth() &&
866 prev->getLabelWidthString() == pit->getLabelWidthString())
868 layoutasc = (layout->itemsep * defaultRowHeight());
869 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
870 tmptop = layout->topsep;
873 layoutasc = (tmptop * defaultRowHeight());
874 } else if (pit->params().lineTop()) {
875 tmptop = layout->topsep;
878 layoutasc = (tmptop * defaultRowHeight());
881 prev = outerHook(pit, ownerParagraphs());
882 if (prev != ownerParagraphs().end()) {
883 maxasc += int(prev->layout()->parsep * defaultRowHeight());
884 } else if (pit != ownerParagraphs().begin()) {
885 ParagraphList::iterator prior_pit = boost::prior(pit);
886 if (prior_pit->getDepth() != 0 ||
887 prior_pit->layout() == layout) {
888 maxasc += int(layout->parsep * defaultRowHeight());
894 // is it a bottom line?
895 if (row.endpos() >= pit->size()) {
897 ParagraphList::iterator nextpit = boost::next(pit);
898 if (nextpit == ownerParagraphs().end() && !isInInset())
899 maxdesc += PAPER_MARGIN;
901 // add the vertical spaces, that the user added
902 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
904 // do not forget the DTP-lines!
905 // there height depends on the font of the nearest character
906 if (pit->params().lineBottom())
907 maxdesc += 2 * font_metrics::ascent('x',
908 getFont(pit, max(pos_type(0), pit->size() - 1)));
910 // and now the pagebreaks
911 if (pit->params().pagebreakBottom())
912 maxdesc += 3 * defaultRowHeight();
914 // and now the layout spaces, for example before and after
915 // a section, or between the items of a itemize or enumerate
917 if (!pit->params().pagebreakBottom()
918 && nextpit != ownerParagraphs().end()) {
919 ParagraphList::iterator comparepit = pit;
923 if (comparepit->getDepth() > nextpit->getDepth()) {
924 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
925 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
926 if (comparepit->layout()!= nextpit->layout()
927 || nextpit->getLabelWidthString() !=
928 comparepit->getLabelWidthString())
930 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
933 layoutdesc = unusual;
936 } else if (comparepit->getDepth() == nextpit->getDepth()) {
938 if (comparepit->layout() != nextpit->layout()
939 || nextpit->getLabelWidthString() !=
940 comparepit->getLabelWidthString())
941 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
946 // incalculate the layout spaces
947 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
948 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
950 row.height(maxasc + maxdesc + labeladdon);
951 row.baseline(maxasc + labeladdon);
952 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
956 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
958 // allow only if at start or end, or all previous is new text
959 ParagraphList::iterator cpit = cursorPar();
960 if (cursor.pos() && cursor.pos() != cpit->size()
961 && cpit->isChangeEdited(0, cursor.pos()))
964 LyXTextClass const & tclass =
965 bv()->buffer()->params().getLyXTextClass();
966 LyXLayout_ptr const & layout = cpit->layout();
968 // this is only allowed, if the current paragraph is not empty or caption
969 // and if it has not the keepempty flag active
970 if (cpit->empty() && !cpit->allowEmpty()
971 && layout->labeltype != LABEL_SENSITIVE)
974 recUndo(cursor.par());
976 // Always break behind a space
978 // It is better to erase the space (Dekel)
979 if (cursor.pos() < cpit->size() && cpit->isLineSeparator(cursor.pos()))
980 cpit->erase(cursor.pos());
982 // break the paragraph
986 keep_layout = layout->isEnvironment();
988 // we need to set this before we insert the paragraph. IMO the
989 // breakParagraph call should return a bool if it inserts the
990 // paragraph before or behind and we should react on that one
991 // but we can fix this in 1.3.0 (Jug 20020509)
992 bool const isempty = cpit->allowEmpty() && cpit->empty();
993 ::breakParagraph(bv()->buffer()->params(), paragraphs, cpit,
994 cursor.pos(), keep_layout);
996 #warning Trouble Point! (Lgb)
997 // When ::breakParagraph is called from within an inset we must
998 // ensure that the correct ParagraphList is used. Today that is not
999 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1001 ParagraphList::iterator next_par = boost::next(cpit);
1003 // well this is the caption hack since one caption is really enough
1004 if (layout->labeltype == LABEL_SENSITIVE) {
1006 // set to standard-layout
1007 cpit->applyLayout(tclass.defaultLayout());
1009 // set to standard-layout
1010 next_par->applyLayout(tclass.defaultLayout());
1013 // if the cursor is at the beginning of a row without prior newline,
1015 // This touches only the screen-update. Otherwise we would may have
1016 // an empty row on the screen
1017 RowList::iterator crit = cpit->getRow(cursor.pos());
1018 if (cursor.pos() && crit->pos() == cursor.pos()
1019 && !cpit->isNewline(cursor.pos() - 1))
1024 while (!next_par->empty() && next_par->isNewline(0))
1028 redoParagraph(cpit);
1029 redoParagraph(next_par);
1031 // This check is necessary. Otherwise the new empty paragraph will
1032 // be deleted automatically. And it is more friendly for the user!
1033 if (cursor.pos() || isempty)
1034 setCursor(next_par, 0);
1040 // convenience function
1041 void LyXText::redoParagraph()
1044 redoParagraph(cursorPar());
1045 setCursorIntern(cursor.par(), cursor.pos());
1049 // insert a character, moves all the following breaks in the
1050 // same Paragraph one to the right and make a rebreak
1051 void LyXText::insertChar(char c)
1053 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
1055 // When the free-spacing option is set for the current layout,
1056 // disable the double-space checking
1058 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1059 cursorPar()->isFreeSpacing();
1061 if (lyxrc.auto_number) {
1062 static string const number_operators = "+-/*";
1063 static string const number_unary_operators = "+-";
1064 static string const number_seperators = ".,:";
1066 if (current_font.number() == LyXFont::ON) {
1067 if (!IsDigit(c) && !contains(number_operators, c) &&
1068 !(contains(number_seperators, c) &&
1069 cursor.pos() >= 1 &&
1070 cursor.pos() < cursorPar()->size() &&
1071 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1072 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1074 number(bv()); // Set current_font.number to OFF
1075 } else if (IsDigit(c) &&
1076 real_current_font.isVisibleRightToLeft()) {
1077 number(bv()); // Set current_font.number to ON
1079 if (cursor.pos() > 0) {
1080 char const c = cursorPar()->getChar(cursor.pos() - 1);
1081 if (contains(number_unary_operators, c) &&
1082 (cursor.pos() == 1 ||
1083 cursorPar()->isSeparator(cursor.pos() - 2) ||
1084 cursorPar()->isNewline(cursor.pos() - 2))
1090 } else if (contains(number_seperators, c) &&
1091 cursor.pos() >= 2 &&
1094 cursor.pos() - 2).number() == LyXFont::ON) {
1105 // First check, if there will be two blanks together or a blank at
1106 // the beginning of a paragraph.
1107 // I decided to handle blanks like normal characters, the main
1108 // difference are the special checks when calculating the row.fill
1109 // (blank does not count at the end of a row) and the check here
1111 // The bug is triggered when we type in a description environment:
1112 // The current_font is not changed when we go from label to main text
1113 // and it should (along with realtmpfont) when we type the space.
1114 // CHECK There is a bug here! (Asger)
1116 // store the current font. This is because of the use of cursor
1117 // movements. The moving cursor would refresh the current font
1118 LyXFont realtmpfont = real_current_font;
1119 LyXFont rawtmpfont = current_font;
1121 if (!freeSpacing && IsLineSeparatorChar(c)) {
1122 if ((cursor.pos() > 0
1123 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1124 || (cursor.pos() > 0
1125 && cursorPar()->isNewline(cursor.pos() - 1))
1126 || (cursor.pos() == 0)) {
1127 static bool sent_space_message = false;
1128 if (!sent_space_message) {
1129 if (cursor.pos() == 0)
1130 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1132 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1133 sent_space_message = true;
1140 // Here case LyXText::InsertInset already inserted the character
1141 if (c != Paragraph::META_INSET)
1142 cursorPar()->insertChar(cursor.pos(), c);
1144 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1146 current_font = rawtmpfont;
1147 real_current_font = realtmpfont;
1148 redoParagraph(cursorPar());
1149 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1155 void LyXText::charInserted()
1157 // Here we could call finishUndo for every 20 characters inserted.
1158 // This is from my experience how emacs does it. (Lgb)
1159 static unsigned int counter;
1169 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
1171 double w = row.fill();
1172 double fill_hfill = 0;
1173 double fill_label_hfill = 0;
1174 double fill_separator = 0;
1178 pit->isRightToLeftPar(bv()->buffer()->params());
1180 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer(), row) : 0;
1182 x = workWidth() > 0 ? leftMargin(pit, row) : 0;
1184 // is there a manual margin with a manual label
1185 LyXLayout_ptr const & layout = pit->layout();
1187 if (layout->margintype == MARGIN_MANUAL
1188 && layout->labeltype == LABEL_MANUAL) {
1189 /// We might have real hfills in the label part
1190 int nlh = numberOfLabelHfills(*pit, row);
1192 // A manual label par (e.g. List) has an auto-hfill
1193 // between the label text and the body of the
1195 // But we don't want to do this auto hfill if the par
1200 if (nlh && !pit->getLabelWidthString().empty()) {
1201 fill_label_hfill = labelFill(pit, row) / double(nlh);
1205 // are there any hfills in the row?
1206 int const nh = numberOfHfills(*pit, row);
1210 fill_hfill = w / nh;
1211 // we don't have to look at the alignment if it is ALIGN_LEFT and
1212 // if the row is already larger then the permitted width as then
1213 // we force the LEFT_ALIGN'edness!
1214 } else if (int(row.width()) < workWidth()) {
1215 // is it block, flushleft or flushright?
1216 // set x how you need it
1218 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1219 align = layout->align;
1221 align = pit->params().align();
1223 // ERT insets should always be LEFT ALIGNED on screen
1224 InsetOld * inset = pit->inInset();
1225 if (inset && inset->owner() &&
1226 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1228 align = LYX_ALIGN_LEFT;
1231 // Display-style insets should always be on a centred row
1232 // The test on pit->size() is to catch zero-size pars, which
1233 // would trigger the assert in Paragraph::getInset().
1234 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1236 && pit->isInset(row.pos())
1237 && pit->getInset(row.pos())->display())
1239 align = LYX_ALIGN_CENTER;
1243 case LYX_ALIGN_BLOCK:
1245 int const ns = numberOfSeparators(*pit, row);
1246 bool disp_inset = false;
1247 if (row.endpos() < pit->size()) {
1248 InsetOld * in = pit->getInset(row.endpos());
1250 disp_inset = in->display();
1252 // If we have separators, this is not the last row of a
1253 // par, does not end in newline, and is not row above a
1254 // display inset... then stretch it
1256 && row.endpos() < pit->size()
1257 && !pit->isNewline(row.endpos())
1260 fill_separator = w / ns;
1261 } else if (is_rtl) {
1266 case LYX_ALIGN_RIGHT:
1269 case LYX_ALIGN_CENTER:
1275 bidi.computeTables(*pit, *bv()->buffer(), row);
1277 pos_type body_pos = pit->beginningOfBody();
1278 pos_type last = lastPos(*pit, row);
1281 (body_pos - 1 > last ||
1282 !pit->isLineSeparator(body_pos - 1))) {
1283 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1284 if (body_pos - 1 <= last)
1285 x += fill_label_hfill;
1289 row.fill_hfill(fill_hfill);
1290 row.fill_label_hfill(fill_label_hfill);
1291 row.fill_separator(fill_separator);
1296 // important for the screen
1299 // the cursor set functions have a special mechanism. When they
1300 // realize, that you left an empty paragraph, they will delete it.
1301 // They also delete the corresponding row
1303 void LyXText::cursorRightOneWord()
1305 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1306 setCursor(cursorPar(), cursor.pos());
1310 // Skip initial whitespace at end of word and move cursor to *start*
1311 // of prior word, not to end of next prior word.
1312 void LyXText::cursorLeftOneWord()
1314 LyXCursor tmpcursor = cursor;
1315 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1316 setCursor(getPar(tmpcursor), tmpcursor.pos());
1320 void LyXText::selectWord(word_location loc)
1322 LyXCursor from = cursor;
1323 LyXCursor to = cursor;
1324 ::getWord(*this, from, to, loc, ownerParagraphs());
1326 setCursor(from.par(), from.pos());
1329 selection.cursor = cursor;
1330 setCursor(to.par(), to.pos());
1335 // Select the word currently under the cursor when no
1336 // selection is currently set
1337 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1339 if (!selection.set()) {
1341 return selection.set();
1347 void LyXText::acceptChange()
1349 if (!selection.set() && cursorPar()->size())
1352 if (selection.start.par() == selection.end.par()) {
1353 LyXCursor & startc = selection.start;
1354 LyXCursor & endc = selection.end;
1355 recordUndo(Undo::INSERT, this, startc.par());
1356 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1359 redoParagraph(getPar(startc));
1360 setCursorIntern(startc.par(), 0);
1362 #warning handle multi par selection
1366 void LyXText::rejectChange()
1368 if (!selection.set() && cursorPar()->size())
1371 if (selection.start.par() == selection.end.par()) {
1372 LyXCursor & startc = selection.start;
1373 LyXCursor & endc = selection.end;
1374 recordUndo(Undo::INSERT, this, startc.par());
1375 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1378 redoParagraph(getPar(startc));
1379 setCursorIntern(startc.par(), 0);
1381 #warning handle multi par selection
1385 // This function is only used by the spellchecker for NextWord().
1386 // It doesn't handle LYX_ACCENTs and probably never will.
1387 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1389 if (the_locking_inset) {
1390 WordLangTuple word =
1391 the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1392 if (!word.word().empty()) {
1393 value += float(cursor.y());
1394 value /= float(height);
1397 // we have to go on checking so move cursor to the next char
1398 if (cursor.pos() == cursorPar()->size()) {
1399 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1401 cursor.par(cursor.par() + 1);
1404 cursor.pos(cursor.pos() + 1);
1407 int const tmppar = cursor.par();
1409 // If this is not the very first word, skip rest of
1410 // current word because we are probably in the middle
1411 // of a word if there is text here.
1412 if (cursor.pos() || cursor.par() != 0) {
1413 while (cursor.pos() < cursorPar()->size()
1414 && cursorPar()->isLetter(cursor.pos()))
1415 cursor.pos(cursor.pos() + 1);
1418 // Now, skip until we have real text (will jump paragraphs)
1420 ParagraphList::iterator cpit = cursorPar();
1421 pos_type const cpos = cursor.pos();
1423 if (cpos == cpit->size()) {
1424 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1425 cursor.par(cursor.par() + 1);
1432 bool const is_good_inset = cpit->isInset(cpos)
1433 && cpit->getInset(cpos)->allowSpellcheck();
1435 if (!isDeletedText(*cpit, cpos)
1436 && (is_good_inset || cpit->isLetter(cpos)))
1439 cursor.pos(cpos + 1);
1442 // now check if we hit an inset so it has to be a inset containing text!
1443 if (cursor.pos() < cursorPar()->size() &&
1444 cursorPar()->isInset(cursor.pos())) {
1446 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1447 cursorPar()->getInset(cursor.pos())->dispatch(cmd);
1448 // now call us again to do the above trick
1449 // but obviously we have to start from down below ;)
1450 return bv()->text->selectNextWordToSpellcheck(value);
1453 // Update the value if we changed paragraphs
1454 if (cursor.par() != tmppar) {
1455 setCursor(cursor.par(), cursor.pos());
1456 value = float(cursor.y())/float(height);
1459 // Start the selection from here
1460 selection.cursor = cursor;
1462 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1463 // and find the end of the word (insets like optional hyphens
1464 // and ligature break are part of a word)
1465 while (cursor.pos() < cursorPar()->size()
1466 && cursorPar()->isLetter(cursor.pos())
1467 && !isDeletedText(*cursorPar(), cursor.pos()))
1468 cursor.pos(cursor.pos() + 1);
1470 // Finally, we copy the word to a string and return it
1472 if (selection.cursor.pos() < cursor.pos()) {
1473 for (pos_type i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1474 if (!cursorPar()->isInset(i))
1475 str += cursorPar()->getChar(i);
1478 return WordLangTuple(str, lang_code);
1482 // This one is also only for the spellchecker
1483 void LyXText::selectSelectedWord()
1485 if (the_locking_inset) {
1486 the_locking_inset->selectSelectedWord(bv());
1489 // move cursor to the beginning
1490 setCursor(selection.cursor.par(), selection.cursor.pos());
1492 // set the sel cursor
1493 selection.cursor = cursor;
1495 // now find the end of the word
1496 while (cursor.pos() < cursorPar()->size()
1497 && cursorPar()->isLetter(cursor.pos()))
1498 cursor.pos(cursor.pos() + 1);
1500 setCursor(cursorPar(), cursor.pos());
1502 // finally set the selection
1507 // Delete from cursor up to the end of the current or next word.
1508 void LyXText::deleteWordForward()
1510 if (cursorPar()->empty())
1513 LyXCursor tmpcursor = cursor;
1514 selection.set(true); // to avoid deletion
1515 cursorRightOneWord();
1516 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1517 selection.cursor = cursor;
1520 cutSelection(true, false);
1525 // Delete from cursor to start of current or prior word.
1526 void LyXText::deleteWordBackward()
1528 if (cursorPar()->empty())
1531 LyXCursor tmpcursor = cursor;
1532 selection.set(true); // to avoid deletion
1533 cursorLeftOneWord();
1534 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1535 selection.cursor = cursor;
1538 cutSelection(true, false);
1543 // Kill to end of line.
1544 void LyXText::deleteLineForward()
1546 if (cursorPar()->empty())
1547 // Paragraph is empty, so we just go to the right
1550 LyXCursor tmpcursor = cursor;
1551 // We can't store the row over a regular setCursor
1552 // so we set it to 0 and reset it afterwards.
1553 selection.set(true); // to avoid deletion
1555 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1556 selection.cursor = cursor;
1559 // What is this test for ??? (JMarc)
1560 if (!selection.set()) {
1561 deleteWordForward();
1563 cutSelection(true, false);
1569 void LyXText::changeCase(LyXText::TextCase action)
1574 if (selection.set()) {
1575 from = selection.start;
1579 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1580 setCursor(to.par(), to.pos() + 1);
1583 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1585 pos_type pos = from.pos();
1586 int par = from.par();
1588 while (par != int(ownerParagraphs().size()) &&
1589 (pos != to.pos() || par != to.par())) {
1590 ParagraphList::iterator pit = getPar(par);
1591 if (pos == pit->size()) {
1596 unsigned char c = pit->getChar(pos);
1597 if (c != Paragraph::META_INSET) {
1599 case text_lowercase:
1602 case text_capitalization:
1604 action = text_lowercase;
1606 case text_uppercase:
1612 pit->setChar(pos, c);
1618 void LyXText::Delete()
1620 // this is a very easy implementation
1622 LyXCursor old_cursor = cursor;
1623 int const old_cur_par_id = cursorPar()->id();
1624 int const old_cur_par_prev_id =
1625 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1627 // just move to the right
1630 // CHECK Look at the comment here.
1631 // This check is not very good...
1632 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1633 // and that can very well delete the par or par->previous in
1634 // old_cursor. Will a solution where we compare paragraph id's
1636 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1637 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1638 // delete-empty-paragraph-mechanism has done it
1642 // if you had success make a backspace
1643 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1644 recordUndo(Undo::DELETE, this, old_cursor.par());
1650 void LyXText::backspace()
1652 // Get the font that is used to calculate the baselineskip
1653 ParagraphList::iterator pit = cursorPar();
1654 pos_type lastpos = pit->size();
1656 if (cursor.pos() == 0) {
1657 // The cursor is at the beginning of a paragraph,
1658 // so the the backspace will collapse two paragraphs into one.
1660 // but it's not allowed unless it's new
1661 if (pit->isChangeEdited(0, pit->size()))
1664 // we may paste some paragraphs
1666 // is it an empty paragraph?
1668 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1669 // This is an empty paragraph and we delete it just
1670 // by moving the cursor one step
1671 // left and let the DeleteEmptyParagraphMechanism
1672 // handle the actual deletion of the paragraph.
1675 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1676 if (cursorPar()->layout() == tmppit->layout()
1677 && cursorPar()->getAlign() == tmppit->getAlign()) {
1678 // Inherit bottom DTD from the paragraph below.
1679 // (the one we are deleting)
1680 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1681 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1682 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1687 // the layout things can change the height of a row !
1693 if (cursor.par() != 0)
1694 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1696 ParagraphList::iterator tmppit = cursorPar();
1697 // We used to do cursorLeftIntern() here, but it is
1698 // not a good idea since it triggers the auto-delete
1699 // mechanism. So we do a cursorLeftIntern()-lite,
1700 // without the dreaded mechanism. (JMarc)
1701 if (cursor.par() != 0) {
1702 // steps into the above paragraph.
1703 setCursorIntern(cursor.par() - 1,
1704 getPar(cursor.par() - 1)->size(),
1708 // Pasting is not allowed, if the paragraphs have different
1709 // layout. I think it is a real bug of all other
1710 // word processors to allow it. It confuses the user.
1711 // Correction: Pasting is always allowed with standard-layout
1712 Buffer & buf = *bv()->buffer();
1713 BufferParams const & bufparams = buf.params();
1714 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1716 if (cursorPar() != tmppit
1717 && (cursorPar()->layout() == tmppit->layout()
1718 || tmppit->layout() == tclass.defaultLayout())
1719 && cursorPar()->getAlign() == tmppit->getAlign()) {
1720 mergeParagraph(bufparams,
1721 buf.paragraphs(), cursorPar());
1723 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1724 cursor.pos(cursor.pos() - 1);
1726 // the row may have changed, block, hfills etc.
1728 setCursor(cursor.par(), cursor.pos(), false);
1731 // this is the code for a normal backspace, not pasting
1733 recordUndo(Undo::DELETE, this, cursor.par());
1734 // We used to do cursorLeftIntern() here, but it is
1735 // not a good idea since it triggers the auto-delete
1736 // mechanism. So we do a cursorLeftIntern()-lite,
1737 // without the dreaded mechanism. (JMarc)
1738 setCursorIntern(cursor.par(), cursor.pos() - 1,
1739 false, cursor.boundary());
1740 cursorPar()->erase(cursor.pos());
1743 lastpos = cursorPar()->size();
1744 if (cursor.pos() == lastpos)
1748 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1752 ParagraphList::iterator LyXText::cursorPar() const
1754 return getPar(cursor.par());
1758 RowList::iterator LyXText::cursorRow() const
1760 return cursorPar()->getRow(cursor.pos());
1764 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1766 return getPar(cur.par());
1770 ParagraphList::iterator LyXText::getPar(int par) const
1772 BOOST_ASSERT(par >= 0);
1773 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1774 ParagraphList::iterator pit = ownerParagraphs().begin();
1775 std::advance(pit, par);
1781 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1783 //lyxerr << "getRowNearY: y " << y << endl;
1785 pit = boost::prior(ownerParagraphs().end());
1787 RowList::iterator rit = lastRow();
1788 RowList::iterator rbegin = firstRow();
1790 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1791 previousRow(pit, rit);
1797 int LyXText::getDepth() const
1799 return cursorPar()->getDepth();
1803 RowList::iterator LyXText::firstRow() const
1805 return ownerParagraphs().front().rows.begin();
1809 RowList::iterator LyXText::lastRow() const
1811 return boost::prior(endRow());
1815 RowList::iterator LyXText::endRow() const
1817 return ownerParagraphs().back().rows.end();
1821 void LyXText::nextRow(ParagraphList::iterator & pit,
1822 RowList::iterator & rit) const
1825 if (rit == pit->rows.end()) {
1827 if (pit == ownerParagraphs().end())
1830 rit = pit->rows.begin();
1835 void LyXText::previousRow(ParagraphList::iterator & pit,
1836 RowList::iterator & rit) const
1838 if (rit != pit->rows.begin())
1841 BOOST_ASSERT(pit != ownerParagraphs().begin());
1843 rit = boost::prior(pit->rows.end());
1848 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1850 if (!selection.set())
1853 // should be const ...
1854 ParagraphList::iterator startpit = getPar(selection.start);
1855 ParagraphList::iterator endpit = getPar(selection.end);
1856 size_t const startpos = selection.start.pos();
1857 size_t const endpos = selection.end.pos();
1859 if (startpit == endpit)
1860 return startpit->asString(buffer, startpos, endpos, label);
1862 // First paragraph in selection
1864 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1866 // The paragraphs in between (if any)
1867 ParagraphList::iterator pit = startpit;
1868 for (++pit; pit != endpit; ++pit)
1869 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1871 // Last paragraph in selection
1872 result += endpit->asString(buffer, 0, endpos, label);
1878 int LyXText::parOffset(ParagraphList::iterator pit) const
1880 return std::distance(ownerParagraphs().begin(), pit);
1884 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1886 // remove rows of paragraph, keep track of height changes
1887 height -= pit->height;
1895 InsetList::iterator ii = pit->insetlist.begin();
1896 InsetList::iterator iend = pit->insetlist.end();
1897 for (; ii != iend; ++ii) {
1899 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
1900 ii->inset->metrics(mi, dim);
1903 // rebreak the paragraph
1904 int const ww = workWidth();
1908 rowBreakPoint(pit, row);
1911 prepareToPrint(pit, row);
1912 setHeightOfRow(pit, row);
1913 row.y_offset(pit->height);
1914 pit->rows.push_back(row);
1915 pit->width = std::max(pit->width, row.width());
1916 pit->height += row.height();
1917 } while (z < pit->size());
1919 height += pit->height;
1920 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1924 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1925 ParagraphList::iterator end)
1927 for ( ; pit != end; ++pit)
1928 redoParagraphInternal(pit);
1929 updateRowPositions();
1933 void LyXText::redoParagraph(ParagraphList::iterator pit)
1935 redoParagraphInternal(pit);
1936 updateRowPositions();
1940 void LyXText::fullRebreak()
1942 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1944 selection.cursor = cursor;
1948 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1950 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1951 // << " workWidth: " << workWidth() << "\nfont: " << mi.base.font << endl;
1952 //BOOST_ASSERT(mi.base.textwidth);
1955 // rebuild row cache. This recomputes height as well.
1956 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1958 width = maxParagraphWidth(ownerParagraphs());
1961 dim.asc = firstRow()->ascent_of_text();
1962 dim.des = height - dim.asc;
1963 dim.wid = std::max(mi.base.textwidth, int(width));
1967 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1969 return row.endpos() >= pit->size()
1970 && boost::next(pit) == ownerParagraphs().end();
1974 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1976 return row.pos() == 0 && pit == ownerParagraphs().begin();