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
469 // maximum pixel width of a row.
470 int width = workWidth() - rightMargin(*pit, *bv()->buffer(), row);
472 // inset->textWidth() returns -1 via workWidth(),
475 row.endpos(pit->size() + 1);
479 LyXLayout_ptr const & layout = pit->layout();
481 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
482 row.endpos(addressBreakPoint(row.pos(), *pit) + 1);
486 pos_type const pos = row.pos();
487 pos_type const body_pos = pit->beginningOfBody();
488 pos_type const last = pit->size();
489 pos_type point = last;
492 row.endpos(last + 1);
496 // Now we iterate through until we reach the right margin
497 // or the end of the par, then choose the possible break
500 int const left = leftMargin(pit, row);
503 // pixel width since last breakpoint
508 // We re-use the font resolution for the entire font span when possible
509 LyXFont font = getFont(pit, i);
510 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
512 for (; i < last; ++i) {
513 if (pit->isNewline(i)) {
519 InsetOld * in = pit->getInset(i + 1);
520 if (in && in->display()) {
525 in = pit->getInset(i);
526 if (in && in->display()) {
532 char const c = pit->getChar(i);
533 if (i > endPosOfFontSpan) {
534 font = getFont(pit, i);
535 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
540 // add the auto-hfill from label end to the body
541 if (body_pos && i == body_pos) {
542 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
543 if (pit->isLineSeparator(i - 1))
544 thiswidth -= singleWidth(pit, i - 1);
545 int left_margin = labelEnd(pit, row);
546 if (thiswidth + x < left_margin)
547 thiswidth = left_margin - x;
548 thiswidth += singleWidth(pit, i, c, font);
550 thiswidth = singleWidth(pit, i, c, font);
554 //lyxerr << "i: " << i << " x: " << x << " width: " << width << endl;
555 chunkwidth += thiswidth;
557 // break before a character that will fall off
558 // the right of the row
560 // if no break before, break here
561 if (point == last || chunkwidth >= width - left) {
564 // exit on last registered breakpoint:
573 InsetOld * in = pit->getInset(i);
574 if (!in || in->isChar()) {
575 // some insets are line separators too
576 if (pit->isLineSeparator(i)) {
577 // register breakpoint:
584 if (point == last && x >= width) {
585 // didn't find one, break at the point we reached the edge
587 } else if (i == last && x < width) {
588 // found one, but we fell off the end of the par, so prefer
593 // manual labels cannot be broken in LaTeX. But we
594 // want to make our on-screen rendering of footnotes
596 if (body_pos && point < body_pos)
597 point = body_pos - 1;
599 row.endpos(point + 1);
603 // returns the minimum space a row needs on the screen in pixel
604 void LyXText::fill(ParagraphList::iterator pit, Row & row, int paper_width) const
607 // get the pure distance
608 pos_type const last = lastPos(*pit, row);
610 LyXLayout_ptr const & layout = pit->layout();
612 // special handling of the right address boxes
613 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
614 int const tmpfill = row.fill();
615 row.fill(0); // the minfill in leftMargin()
616 w = leftMargin(pit, row);
619 w = leftMargin(pit, row);
622 pos_type const body_pos = pit->beginningOfBody();
623 pos_type i = row.pos();
625 if (! pit->empty() && i <= last) {
626 // We re-use the font resolution for the entire span when possible
627 LyXFont font = getFont(pit, i);
628 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
630 if (body_pos > 0 && i == body_pos) {
631 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
632 if (pit->isLineSeparator(i - 1))
633 w -= singleWidth(pit, i - 1);
634 int left_margin = labelEnd(pit, row);
638 char const c = pit->getChar(i);
639 if (IsPrintable(c) && i > endPosOfFontSpan) {
640 // We need to get the next font
641 font = getFont(pit, i);
642 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
644 w += singleWidth(pit, i, c, font);
648 if (body_pos > 0 && body_pos > last) {
649 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
650 if (last >= 0 && pit->isLineSeparator(last))
651 w -= singleWidth(pit, last);
652 int const left_margin = labelEnd(pit, row);
657 int const fill = paper_width - w - rightMargin(*pit, *bv()->buffer(), row);
659 row.width(paper_width - fill);
663 // returns the minimum space a manual label needs on the screen in pixel
664 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
666 pos_type last = pit->beginningOfBody();
668 BOOST_ASSERT(last > 0);
670 // -1 because a label ends either with a space that is in the label,
671 // or with the beginning of a footnote that is outside the label.
674 // a separator at this end does not count
675 if (pit->isLineSeparator(last))
679 pos_type i = row.pos();
681 w += singleWidth(pit, i);
686 string const & labwidstr = pit->params().labelWidthString();
687 if (!labwidstr.empty()) {
688 LyXFont const labfont = getLabelFont(pit);
689 int const labwidth = font_metrics::width(labwidstr, labfont);
690 fill = max(labwidth - w, 0);
697 LColor_color LyXText::backgroundColor() const
700 return inset_owner->backgroundColor();
702 return LColor::background;
706 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
708 // get the maximum ascent and the maximum descent
709 double layoutasc = 0;
710 double layoutdesc = 0;
713 // ok, let us initialize the maxasc and maxdesc value.
714 // Only the fontsize count. The other properties
715 // are taken from the layoutfont. Nicer on the screen :)
716 LyXLayout_ptr const & layout = pit->layout();
718 // as max get the first character of this row then it can increase but not
719 // decrease the height. Just some point to start with so we don't have to
720 // do the assignment below too often.
721 LyXFont font = getFont(pit, row.pos());
722 LyXFont::FONT_SIZE const tmpsize = font.size();
723 font = getLayoutFont(pit);
724 LyXFont::FONT_SIZE const size = font.size();
725 font.setSize(tmpsize);
727 LyXFont labelfont = getLabelFont(pit);
729 double spacing_val = 1.0;
730 if (!pit->params().spacing().isDefault())
731 spacing_val = pit->params().spacing().getValue();
733 spacing_val = bv()->buffer()->params().spacing().getValue();
734 //lyxerr << "spacing_val = " << spacing_val << endl;
736 int maxasc = int(font_metrics::maxAscent(font) *
737 layout->spacing.getValue() * spacing_val);
738 int maxdesc = int(font_metrics::maxDescent(font) *
739 layout->spacing.getValue() * spacing_val);
741 pos_type const pos_end = lastPos(*pit, row);
746 // We re-use the font resolution for the entire font span when possible
747 LyXFont font = getFont(pit, row.pos());
748 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(row.pos());
751 Paragraph const & par = *pit;
753 // Check if any insets are larger
754 for (pos_type pos = row.pos(); pos <= pos_end; ++pos) {
755 // Manual inlined optimised version of common case of
756 // "maxwidth += singleWidth(pit, pos);"
757 char const c = par.getChar(pos);
759 if (IsPrintable(c)) {
760 if (pos > endPosOfFontSpan) {
761 // We need to get the next font
762 font = getFont(pit, pos);
763 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
765 if (! font.language()->RightToLeft()) {
766 maxwidth += font_metrics::width(c, font);
768 // Fall-back to normal case
769 maxwidth += singleWidth(pit, pos, c, font);
770 // And flush font cache
771 endPosOfFontSpan = 0;
774 // Special handling of insets - are any larger?
775 if (par.isInset(pos)) {
776 InsetOld const * tmpinset = par.getInset(pos);
778 maxwidth += tmpinset->width();
779 maxasc = max(maxasc, tmpinset->ascent());
780 maxdesc = max(maxdesc, tmpinset->descent());
783 // Fall-back to normal case
784 maxwidth += singleWidth(pit, pos, c, font);
785 // And flush font cache
786 endPosOfFontSpan = 0;
792 // Check if any custom fonts are larger (Asger)
793 // This is not completely correct, but we can live with the small,
794 // cosmetic error for now.
795 LyXFont::FONT_SIZE maxsize =
796 pit->highestFontInRange(row.pos(), pos_end, size);
797 if (maxsize > font.size()) {
798 font.setSize(maxsize);
799 maxasc = max(maxasc, font_metrics::maxAscent(font));
800 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
803 // This is nicer with box insets:
807 row.ascent_of_text(maxasc);
811 BufferParams const & bufparams = bv()->buffer()->params();
812 // some parksips VERY EASY IMPLEMENTATION
813 if (bv()->buffer()->params().paragraph_separation ==
814 BufferParams::PARSEP_SKIP)
816 if (layout->isParagraph()
817 && pit->getDepth() == 0
818 && pit != ownerParagraphs().begin())
820 maxasc += bufparams.getDefSkip().inPixels(*bv());
821 } else if (pit != ownerParagraphs().begin() &&
822 boost::prior(pit)->layout()->isParagraph() &&
823 boost::prior(pit)->getDepth() == 0)
825 // is it right to use defskip here too? (AS)
826 maxasc += bufparams.getDefSkip().inPixels(*bv());
831 if (pit == ownerParagraphs().begin() && !isInInset())
832 maxasc += PAPER_MARGIN;
834 // add the vertical spaces, that the user added
835 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
837 // do not forget the DTP-lines!
838 // there height depends on the font of the nearest character
839 if (pit->params().lineTop())
841 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
842 // and now the pagebreaks
843 if (pit->params().pagebreakTop())
844 maxasc += 3 * defaultRowHeight();
846 if (pit->params().startOfAppendix())
847 maxasc += 3 * defaultRowHeight();
849 // This is special code for the chapter, since the label of this
850 // layout is printed in an extra row
851 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
852 float spacing_val = 1.0;
853 if (!pit->params().spacing().isDefault()) {
854 spacing_val = pit->params().spacing().getValue();
856 spacing_val = bufparams.spacing().getValue();
859 labeladdon = int(font_metrics::maxDescent(labelfont) *
860 layout->spacing.getValue() *
862 + int(font_metrics::maxAscent(labelfont) *
863 layout->spacing.getValue() *
867 // special code for the top label
868 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
869 || layout->labeltype == LABEL_BIBLIO
870 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
871 && isFirstInSequence(pit, ownerParagraphs())
872 && !pit->getLabelstring().empty())
874 float spacing_val = 1.0;
875 if (!pit->params().spacing().isDefault()) {
876 spacing_val = pit->params().spacing().getValue();
878 spacing_val = bufparams.spacing().getValue();
882 (font_metrics::maxAscent(labelfont) +
883 font_metrics::maxDescent(labelfont)) *
884 layout->spacing.getValue() *
886 + layout->topsep * defaultRowHeight()
887 + layout->labelbottomsep * defaultRowHeight());
890 // And now the layout spaces, for example before and after
891 // a section, or between the items of a itemize or enumerate
894 if (!pit->params().pagebreakTop()) {
895 ParagraphList::iterator prev =
896 depthHook(pit, ownerParagraphs(),
898 if (prev != pit && prev->layout() == layout &&
899 prev->getDepth() == pit->getDepth() &&
900 prev->getLabelWidthString() == pit->getLabelWidthString())
902 layoutasc = (layout->itemsep * defaultRowHeight());
903 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
904 tmptop = layout->topsep;
907 layoutasc = (tmptop * defaultRowHeight());
908 } else if (pit->params().lineTop()) {
909 tmptop = layout->topsep;
912 layoutasc = (tmptop * defaultRowHeight());
915 prev = outerHook(pit, ownerParagraphs());
916 if (prev != ownerParagraphs().end()) {
917 maxasc += int(prev->layout()->parsep * defaultRowHeight());
918 } else if (pit != ownerParagraphs().begin()) {
919 ParagraphList::iterator prior_pit = boost::prior(pit);
920 if (prior_pit->getDepth() != 0 ||
921 prior_pit->layout() == layout) {
922 maxasc += int(layout->parsep * defaultRowHeight());
928 // is it a bottom line?
929 if (row.endpos() >= pit->size()) {
931 ParagraphList::iterator nextpit = boost::next(pit);
932 if (nextpit == ownerParagraphs().end() && !isInInset())
933 maxdesc += PAPER_MARGIN;
935 // add the vertical spaces, that the user added
936 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
938 // do not forget the DTP-lines!
939 // there height depends on the font of the nearest character
940 if (pit->params().lineBottom())
941 maxdesc += 2 * font_metrics::ascent('x',
942 getFont(pit, max(pos_type(0), pit->size() - 1)));
944 // and now the pagebreaks
945 if (pit->params().pagebreakBottom())
946 maxdesc += 3 * defaultRowHeight();
948 // and now the layout spaces, for example before and after
949 // a section, or between the items of a itemize or enumerate
951 if (!pit->params().pagebreakBottom()
952 && nextpit != ownerParagraphs().end()) {
953 ParagraphList::iterator comparepit = pit;
957 if (comparepit->getDepth() > nextpit->getDepth()) {
958 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
959 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
960 if (comparepit->layout()!= nextpit->layout()
961 || nextpit->getLabelWidthString() !=
962 comparepit->getLabelWidthString())
964 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
967 layoutdesc = unusual;
970 } else if (comparepit->getDepth() == nextpit->getDepth()) {
972 if (comparepit->layout() != nextpit->layout()
973 || nextpit->getLabelWidthString() !=
974 comparepit->getLabelWidthString())
975 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
980 // incalculate the layout spaces
981 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
982 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
984 row.height(maxasc + maxdesc + labeladdon);
985 row.baseline(maxasc + labeladdon);
986 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
991 width = max(workWidth(), int(maxParagraphWidth(ownerParagraphs())));
995 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
997 // allow only if at start or end, or all previous is new text
998 if (cursor.pos() && cursor.pos() != cursorPar()->size()
999 && cursorPar()->isChangeEdited(0, cursor.pos()))
1002 LyXTextClass const & tclass =
1003 bv()->buffer()->params().getLyXTextClass();
1004 LyXLayout_ptr const & layout = cursorPar()->layout();
1006 // this is only allowed, if the current paragraph is not empty or caption
1007 // and if it has not the keepempty flag active
1008 if (cursorPar()->empty() && !cursorPar()->allowEmpty()
1009 && layout->labeltype != LABEL_SENSITIVE)
1012 recUndo(cursor.par());
1014 // Always break behind a space
1016 // It is better to erase the space (Dekel)
1017 if (cursor.pos() < cursorPar()->size()
1018 && cursorPar()->isLineSeparator(cursor.pos()))
1019 cursorPar()->erase(cursor.pos());
1021 // break the paragraph
1025 keep_layout = layout->isEnvironment();
1027 // we need to set this before we insert the paragraph. IMO the
1028 // breakParagraph call should return a bool if it inserts the
1029 // paragraph before or behind and we should react on that one
1030 // but we can fix this in 1.3.0 (Jug 20020509)
1031 bool const isempty = (cursorPar()->allowEmpty() && cursorPar()->empty());
1032 ::breakParagraph(bv()->buffer()->params(), paragraphs, cursorPar(),
1033 cursor.pos(), keep_layout);
1035 #warning Trouble Point! (Lgb)
1036 // When ::breakParagraph is called from within an inset we must
1037 // ensure that the correct ParagraphList is used. Today that is not
1038 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1039 ParagraphList::iterator next_par = boost::next(cursorPar());
1041 // well this is the caption hack since one caption is really enough
1042 if (layout->labeltype == LABEL_SENSITIVE) {
1044 // set to standard-layout
1045 cursorPar()->applyLayout(tclass.defaultLayout());
1047 // set to standard-layout
1048 next_par->applyLayout(tclass.defaultLayout());
1051 // if the cursor is at the beginning of a row without prior newline,
1053 // This touches only the screen-update. Otherwise we would may have
1054 // an empty row on the screen
1055 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1056 && !cursorPar()->isNewline(cursor.pos() - 1))
1061 while (!next_par->empty() && next_par->isNewline(0))
1065 redoParagraph(cursorPar());
1066 redoParagraph(next_par);
1068 // This check is necessary. Otherwise the new empty paragraph will
1069 // be deleted automatically. And it is more friendly for the user!
1070 if (cursor.pos() || isempty)
1071 setCursor(next_par, 0);
1073 setCursor(cursorPar(), 0);
1077 // convenience function
1078 void LyXText::redoParagraph()
1081 redoParagraph(cursorPar());
1082 setCursorIntern(cursor.par(), cursor.pos());
1086 // insert a character, moves all the following breaks in the
1087 // same Paragraph one to the right and make a rebreak
1088 void LyXText::insertChar(char c)
1090 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
1092 // When the free-spacing option is set for the current layout,
1093 // disable the double-space checking
1095 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1096 cursorPar()->isFreeSpacing();
1098 if (lyxrc.auto_number) {
1099 static string const number_operators = "+-/*";
1100 static string const number_unary_operators = "+-";
1101 static string const number_seperators = ".,:";
1103 if (current_font.number() == LyXFont::ON) {
1104 if (!IsDigit(c) && !contains(number_operators, c) &&
1105 !(contains(number_seperators, c) &&
1106 cursor.pos() >= 1 &&
1107 cursor.pos() < cursorPar()->size() &&
1108 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1109 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1111 number(bv()); // Set current_font.number to OFF
1112 } else if (IsDigit(c) &&
1113 real_current_font.isVisibleRightToLeft()) {
1114 number(bv()); // Set current_font.number to ON
1116 if (cursor.pos() > 0) {
1117 char const c = cursorPar()->getChar(cursor.pos() - 1);
1118 if (contains(number_unary_operators, c) &&
1119 (cursor.pos() == 1 ||
1120 cursorPar()->isSeparator(cursor.pos() - 2) ||
1121 cursorPar()->isNewline(cursor.pos() - 2))
1127 } else if (contains(number_seperators, c) &&
1128 cursor.pos() >= 2 &&
1131 cursor.pos() - 2).number() == LyXFont::ON) {
1142 // First check, if there will be two blanks together or a blank at
1143 // the beginning of a paragraph.
1144 // I decided to handle blanks like normal characters, the main
1145 // difference are the special checks when calculating the row.fill
1146 // (blank does not count at the end of a row) and the check here
1148 // The bug is triggered when we type in a description environment:
1149 // The current_font is not changed when we go from label to main text
1150 // and it should (along with realtmpfont) when we type the space.
1151 // CHECK There is a bug here! (Asger)
1153 // store the current font. This is because of the use of cursor
1154 // movements. The moving cursor would refresh the current font
1155 LyXFont realtmpfont = real_current_font;
1156 LyXFont rawtmpfont = current_font;
1158 if (!freeSpacing && IsLineSeparatorChar(c)) {
1159 if ((cursor.pos() > 0
1160 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1161 || (cursor.pos() > 0
1162 && cursorPar()->isNewline(cursor.pos() - 1))
1163 || (cursor.pos() == 0)) {
1164 static bool sent_space_message = false;
1165 if (!sent_space_message) {
1166 if (cursor.pos() == 0)
1167 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1169 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1170 sent_space_message = true;
1177 // Here case LyXText::InsertInset already inserted the character
1178 if (c != Paragraph::META_INSET)
1179 cursorPar()->insertChar(cursor.pos(), c);
1181 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1183 current_font = rawtmpfont;
1184 real_current_font = realtmpfont;
1185 redoParagraph(cursorPar());
1186 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1192 void LyXText::charInserted()
1194 // Here we could call finishUndo for every 20 characters inserted.
1195 // This is from my experience how emacs does it. (Lgb)
1196 static unsigned int counter;
1206 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
1208 double w = row.fill();
1209 double fill_hfill = 0;
1210 double fill_label_hfill = 0;
1211 double fill_separator = 0;
1215 pit->isRightToLeftPar(bv()->buffer()->params());
1217 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer(), row) : 0;
1219 x = workWidth() > 0 ? leftMargin(pit, row) : 0;
1221 // is there a manual margin with a manual label
1222 LyXLayout_ptr const & layout = pit->layout();
1224 if (layout->margintype == MARGIN_MANUAL
1225 && layout->labeltype == LABEL_MANUAL) {
1226 /// We might have real hfills in the label part
1227 int nlh = numberOfLabelHfills(*pit, row);
1229 // A manual label par (e.g. List) has an auto-hfill
1230 // between the label text and the body of the
1232 // But we don't want to do this auto hfill if the par
1237 if (nlh && !pit->getLabelWidthString().empty()) {
1238 fill_label_hfill = labelFill(pit, row) / double(nlh);
1242 // are there any hfills in the row?
1243 int const nh = numberOfHfills(*pit, row);
1247 fill_hfill = w / nh;
1248 // we don't have to look at the alignment if it is ALIGN_LEFT and
1249 // if the row is already larger then the permitted width as then
1250 // we force the LEFT_ALIGN'edness!
1251 } else if (int(row.width()) < workWidth()) {
1252 // is it block, flushleft or flushright?
1253 // set x how you need it
1255 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1256 align = layout->align;
1258 align = pit->params().align();
1260 InsetOld * inset = 0;
1261 // ERT insets should always be LEFT ALIGNED on screen
1262 inset = pit->inInset();
1263 if (inset && inset->owner() &&
1264 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1266 align = LYX_ALIGN_LEFT;
1269 // Display-style insets should always be on a centred row
1270 // The test on pit->size() is to catch zero-size pars, which
1271 // would trigger the assert in Paragraph::getInset().
1272 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1273 inset = pit->isInset(row.pos()) ? pit->getInset(row.pos()) : 0;
1274 if (inset && inset->display()) {
1275 align = LYX_ALIGN_CENTER;
1279 case LYX_ALIGN_BLOCK:
1281 int const ns = numberOfSeparators(*pit, row);
1282 bool disp_inset = false;
1283 if (row.endpos() < pit->size()) {
1284 InsetOld * in = pit->getInset(row.endpos());
1286 disp_inset = in->display();
1288 // If we have separators, this is not the last row of a
1289 // par, does not end in newline, and is not row above a
1290 // display inset... then stretch it
1292 && row.endpos() < pit->size()
1293 && !pit->isNewline(row.endpos())
1296 fill_separator = w / ns;
1297 } else if (is_rtl) {
1302 case LYX_ALIGN_RIGHT:
1305 case LYX_ALIGN_CENTER:
1311 bidi.computeTables(*pit, *bv()->buffer(), row);
1313 pos_type body_pos = pit->beginningOfBody();
1314 pos_type last = lastPos(*pit, row);
1317 (body_pos - 1 > last ||
1318 !pit->isLineSeparator(body_pos - 1))) {
1319 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1320 if (body_pos - 1 <= last)
1321 x += fill_label_hfill;
1325 row.fill_hfill(fill_hfill);
1326 row.fill_label_hfill(fill_label_hfill);
1327 row.fill_separator(fill_separator);
1332 // important for the screen
1335 // the cursor set functions have a special mechanism. When they
1336 // realize, that you left an empty paragraph, they will delete it.
1337 // They also delete the corresponding row
1339 void LyXText::cursorRightOneWord()
1341 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1342 setCursor(cursorPar(), cursor.pos());
1346 // Skip initial whitespace at end of word and move cursor to *start*
1347 // of prior word, not to end of next prior word.
1348 void LyXText::cursorLeftOneWord()
1350 LyXCursor tmpcursor = cursor;
1351 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1352 setCursor(getPar(tmpcursor), tmpcursor.pos());
1356 void LyXText::selectWord(word_location loc)
1358 LyXCursor from = cursor;
1359 LyXCursor to = cursor;
1360 ::getWord(*this, from, to, loc, ownerParagraphs());
1362 setCursor(from.par(), from.pos());
1365 selection.cursor = cursor;
1366 setCursor(to.par(), to.pos());
1371 // Select the word currently under the cursor when no
1372 // selection is currently set
1373 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1375 if (!selection.set()) {
1377 return selection.set();
1383 void LyXText::acceptChange()
1385 if (!selection.set() && cursorPar()->size())
1388 if (selection.start.par() == selection.end.par()) {
1389 LyXCursor & startc = selection.start;
1390 LyXCursor & endc = selection.end;
1391 recordUndo(Undo::INSERT, this, startc.par());
1392 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1395 redoParagraph(getPar(startc));
1396 setCursorIntern(startc.par(), 0);
1398 #warning handle multi par selection
1402 void LyXText::rejectChange()
1404 if (!selection.set() && cursorPar()->size())
1407 if (selection.start.par() == selection.end.par()) {
1408 LyXCursor & startc = selection.start;
1409 LyXCursor & endc = selection.end;
1410 recordUndo(Undo::INSERT, this, startc.par());
1411 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1414 redoParagraph(getPar(startc));
1415 setCursorIntern(startc.par(), 0);
1417 #warning handle multi par selection
1421 // This function is only used by the spellchecker for NextWord().
1422 // It doesn't handle LYX_ACCENTs and probably never will.
1423 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1425 if (the_locking_inset) {
1426 WordLangTuple word =
1427 the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1428 if (!word.word().empty()) {
1429 value += float(cursor.y());
1430 value /= float(height);
1433 // we have to go on checking so move cursor to the next char
1434 if (cursor.pos() == cursorPar()->size()) {
1435 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1437 cursor.par(cursor.par() + 1);
1440 cursor.pos(cursor.pos() + 1);
1443 int const tmppar = cursor.par();
1445 // If this is not the very first word, skip rest of
1446 // current word because we are probably in the middle
1447 // of a word if there is text here.
1448 if (cursor.pos() || cursor.par() != 0) {
1449 while (cursor.pos() < cursorPar()->size()
1450 && cursorPar()->isLetter(cursor.pos()))
1451 cursor.pos(cursor.pos() + 1);
1454 // Now, skip until we have real text (will jump paragraphs)
1456 ParagraphList::iterator cpit = cursorPar();
1457 pos_type const cpos = cursor.pos();
1459 if (cpos == cpit->size()) {
1460 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1461 cursor.par(cursor.par() + 1);
1468 bool const is_good_inset = cpit->isInset(cpos)
1469 && cpit->getInset(cpos)->allowSpellcheck();
1471 if (!isDeletedText(*cpit, cpos)
1472 && (is_good_inset || cpit->isLetter(cpos)))
1475 cursor.pos(cpos + 1);
1478 // now check if we hit an inset so it has to be a inset containing text!
1479 if (cursor.pos() < cursorPar()->size() &&
1480 cursorPar()->isInset(cursor.pos())) {
1482 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1483 cursorPar()->getInset(cursor.pos())->dispatch(cmd);
1484 // now call us again to do the above trick
1485 // but obviously we have to start from down below ;)
1486 return bv()->text->selectNextWordToSpellcheck(value);
1489 // Update the value if we changed paragraphs
1490 if (cursor.par() != tmppar) {
1491 setCursor(cursor.par(), cursor.pos());
1492 value = float(cursor.y())/float(height);
1495 // Start the selection from here
1496 selection.cursor = cursor;
1498 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1499 // and find the end of the word (insets like optional hyphens
1500 // and ligature break are part of a word)
1501 while (cursor.pos() < cursorPar()->size()
1502 && cursorPar()->isLetter(cursor.pos())
1503 && !isDeletedText(*cursorPar(), cursor.pos()))
1504 cursor.pos(cursor.pos() + 1);
1506 // Finally, we copy the word to a string and return it
1508 if (selection.cursor.pos() < cursor.pos()) {
1509 for (pos_type i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1510 if (!cursorPar()->isInset(i))
1511 str += cursorPar()->getChar(i);
1514 return WordLangTuple(str, lang_code);
1518 // This one is also only for the spellchecker
1519 void LyXText::selectSelectedWord()
1521 if (the_locking_inset) {
1522 the_locking_inset->selectSelectedWord(bv());
1525 // move cursor to the beginning
1526 setCursor(selection.cursor.par(), selection.cursor.pos());
1528 // set the sel cursor
1529 selection.cursor = cursor;
1531 // now find the end of the word
1532 while (cursor.pos() < cursorPar()->size()
1533 && cursorPar()->isLetter(cursor.pos()))
1534 cursor.pos(cursor.pos() + 1);
1536 setCursor(cursorPar(), cursor.pos());
1538 // finally set the selection
1543 // Delete from cursor up to the end of the current or next word.
1544 void LyXText::deleteWordForward()
1546 if (cursorPar()->empty())
1549 LyXCursor tmpcursor = cursor;
1550 selection.set(true); // to avoid deletion
1551 cursorRightOneWord();
1552 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1553 selection.cursor = cursor;
1556 cutSelection(true, false);
1561 // Delete from cursor to start of current or prior word.
1562 void LyXText::deleteWordBackward()
1564 if (cursorPar()->empty())
1567 LyXCursor tmpcursor = cursor;
1568 selection.set(true); // to avoid deletion
1569 cursorLeftOneWord();
1570 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1571 selection.cursor = cursor;
1574 cutSelection(true, false);
1579 // Kill to end of line.
1580 void LyXText::deleteLineForward()
1582 if (cursorPar()->empty())
1583 // Paragraph is empty, so we just go to the right
1586 LyXCursor tmpcursor = cursor;
1587 // We can't store the row over a regular setCursor
1588 // so we set it to 0 and reset it afterwards.
1589 selection.set(true); // to avoid deletion
1591 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1592 selection.cursor = cursor;
1595 // What is this test for ??? (JMarc)
1596 if (!selection.set()) {
1597 deleteWordForward();
1599 cutSelection(true, false);
1605 void LyXText::changeCase(LyXText::TextCase action)
1610 if (selection.set()) {
1611 from = selection.start;
1615 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1616 setCursor(to.par(), to.pos() + 1);
1619 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1621 pos_type pos = from.pos();
1622 int par = from.par();
1624 while (par != int(ownerParagraphs().size()) &&
1625 (pos != to.pos() || par != to.par())) {
1626 ParagraphList::iterator pit = getPar(par);
1627 if (pos == pit->size()) {
1632 unsigned char c = pit->getChar(pos);
1633 if (c != Paragraph::META_INSET) {
1635 case text_lowercase:
1638 case text_capitalization:
1640 action = text_lowercase;
1642 case text_uppercase:
1648 pit->setChar(pos, c);
1654 void LyXText::Delete()
1656 // this is a very easy implementation
1658 LyXCursor old_cursor = cursor;
1659 int const old_cur_par_id = cursorPar()->id();
1660 int const old_cur_par_prev_id =
1661 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1663 // just move to the right
1666 // CHECK Look at the comment here.
1667 // This check is not very good...
1668 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1669 // and that can very well delete the par or par->previous in
1670 // old_cursor. Will a solution where we compare paragraph id's
1672 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1673 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1674 // delete-empty-paragraph-mechanism has done it
1678 // if you had success make a backspace
1679 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1680 recordUndo(Undo::DELETE, this, old_cursor.par());
1686 void LyXText::backspace()
1688 // Get the font that is used to calculate the baselineskip
1689 ParagraphList::iterator pit = cursorPar();
1690 pos_type lastpos = pit->size();
1692 if (cursor.pos() == 0) {
1693 // The cursor is at the beginning of a paragraph,
1694 // so the the backspace will collapse two paragraphs into one.
1696 // but it's not allowed unless it's new
1697 if (pit->isChangeEdited(0, pit->size()))
1700 // we may paste some paragraphs
1702 // is it an empty paragraph?
1704 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1705 // This is an empty paragraph and we delete it just
1706 // by moving the cursor one step
1707 // left and let the DeleteEmptyParagraphMechanism
1708 // handle the actual deletion of the paragraph.
1711 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1712 if (cursorPar()->layout() == tmppit->layout()
1713 && cursorPar()->getAlign() == tmppit->getAlign()) {
1714 // Inherit bottom DTD from the paragraph below.
1715 // (the one we are deleting)
1716 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1717 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1718 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1723 // the layout things can change the height of a row !
1729 if (cursor.par() != 0)
1730 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1732 ParagraphList::iterator tmppit = cursorPar();
1733 // We used to do cursorLeftIntern() here, but it is
1734 // not a good idea since it triggers the auto-delete
1735 // mechanism. So we do a cursorLeftIntern()-lite,
1736 // without the dreaded mechanism. (JMarc)
1737 if (cursor.par() != 0) {
1738 // steps into the above paragraph.
1739 setCursorIntern(cursor.par() - 1,
1740 getPar(cursor.par() - 1)->size(),
1744 // Pasting is not allowed, if the paragraphs have different
1745 // layout. I think it is a real bug of all other
1746 // word processors to allow it. It confuses the user.
1747 // Correction: Pasting is always allowed with standard-layout
1748 Buffer & buf = *bv()->buffer();
1749 BufferParams const & bufparams = buf.params();
1750 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1752 if (cursorPar() != tmppit
1753 && (cursorPar()->layout() == tmppit->layout()
1754 || tmppit->layout() == tclass.defaultLayout())
1755 && cursorPar()->getAlign() == tmppit->getAlign()) {
1756 mergeParagraph(bufparams,
1757 buf.paragraphs(), cursorPar());
1759 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1760 cursor.pos(cursor.pos() - 1);
1762 // the row may have changed, block, hfills etc.
1764 setCursor(cursor.par(), cursor.pos(), false);
1767 // this is the code for a normal backspace, not pasting
1769 recordUndo(Undo::DELETE, this, cursor.par());
1770 // We used to do cursorLeftIntern() here, but it is
1771 // not a good idea since it triggers the auto-delete
1772 // mechanism. So we do a cursorLeftIntern()-lite,
1773 // without the dreaded mechanism. (JMarc)
1774 setCursorIntern(cursor.par(), cursor.pos() - 1,
1775 false, cursor.boundary());
1776 cursorPar()->erase(cursor.pos());
1779 lastpos = cursorPar()->size();
1780 if (cursor.pos() == lastpos)
1784 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1788 ParagraphList::iterator LyXText::cursorPar() const
1790 return getPar(cursor.par());
1794 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1796 return getPar(cur.par());
1800 ParagraphList::iterator LyXText::getPar(int par) const
1802 BOOST_ASSERT(par >= 0);
1803 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1804 ParagraphList::iterator pit = ownerParagraphs().begin();
1805 std::advance(pit, par);
1811 RowList::iterator LyXText::cursorRow() const
1813 return getRow(*cursorPar(), cursor.pos());
1817 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
1819 return getRow(*getPar(cur), cur.pos());
1823 RowList::iterator LyXText::getRow(Paragraph & par, pos_type pos) const
1825 RowList::iterator rit = boost::prior(par.rows.end());
1826 RowList::iterator const begin = par.rows.begin();
1828 while (rit != begin && rit->pos() > pos)
1836 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1838 //lyxerr << "getRowNearY: y " << y << endl;
1840 pit = boost::prior(ownerParagraphs().end());
1842 RowList::iterator rit = lastRow();
1843 RowList::iterator rbegin = firstRow();
1845 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1846 previousRow(pit, rit);
1852 int LyXText::getDepth() const
1854 return cursorPar()->getDepth();
1858 RowList::iterator LyXText::firstRow() const
1860 return ownerParagraphs().front().rows.begin();
1864 RowList::iterator LyXText::lastRow() const
1866 return boost::prior(endRow());
1870 RowList::iterator LyXText::endRow() const
1872 return ownerParagraphs().back().rows.end();
1876 void LyXText::nextRow(ParagraphList::iterator & pit,
1877 RowList::iterator & rit) const
1880 if (rit == pit->rows.end()) {
1882 if (pit == ownerParagraphs().end())
1885 rit = pit->rows.begin();
1890 void LyXText::previousRow(ParagraphList::iterator & pit,
1891 RowList::iterator & rit) const
1893 if (rit != pit->rows.begin())
1896 BOOST_ASSERT(pit != ownerParagraphs().begin());
1898 rit = boost::prior(pit->rows.end());
1903 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1905 if (!selection.set())
1908 // should be const ...
1909 ParagraphList::iterator startpit = getPar(selection.start);
1910 ParagraphList::iterator endpit = getPar(selection.end);
1911 size_t const startpos = selection.start.pos();
1912 size_t const endpos = selection.end.pos();
1914 if (startpit == endpit)
1915 return startpit->asString(buffer, startpos, endpos, label);
1917 // First paragraph in selection
1919 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1921 // The paragraphs in between (if any)
1922 ParagraphList::iterator pit = startpit;
1923 for (++pit; pit != endpit; ++pit)
1924 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1926 // Last paragraph in selection
1927 result += endpit->asString(buffer, 0, endpos, label);
1933 int LyXText::parOffset(ParagraphList::iterator pit) const
1935 return std::distance(ownerParagraphs().begin(), pit);
1939 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1941 // remove rows of paragraph, keep track of height changes
1942 height -= pit->height;
1950 InsetList::iterator ii = pit->insetlist.begin();
1951 InsetList::iterator iend = pit->insetlist.end();
1952 for (; ii != iend; ++ii) {
1954 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
1955 ii->inset->metrics(mi, dim);
1958 // rebreak the paragraph
1959 int const ww = workWidth();
1960 for (pos_type z = 0; z < pit->size() + 1; ) {
1962 rowBreakPoint(pit, row);
1965 prepareToPrint(pit, row);
1966 setHeightOfRow(pit, row);
1967 row.y_offset(pit->height);
1968 pit->rows.push_back(row);
1969 pit->width = std::max(pit->width, row.width());
1970 pit->height += row.height();
1972 height += pit->height;
1973 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1977 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1978 ParagraphList::iterator end)
1980 for ( ; pit != end; ++pit)
1981 redoParagraphInternal(pit);
1982 updateRowPositions();
1986 void LyXText::redoParagraph(ParagraphList::iterator pit)
1988 redoParagraphInternal(pit);
1989 updateRowPositions();
1993 void LyXText::fullRebreak()
1995 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1997 selection.cursor = cursor;
2001 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
2003 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
2004 // << " workWidth: " << workWidth() << "\nfont: " << mi.base.font << endl;
2005 //BOOST_ASSERT(mi.base.textwidth);
2008 // rebuild row cache. This recomputes height as well.
2009 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2011 width = maxParagraphWidth(ownerParagraphs());
2014 dim.asc = firstRow()->ascent_of_text();
2015 dim.des = height - dim.asc;
2016 dim.wid = std::max(mi.base.textwidth, int(width));
2020 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
2022 return row.endpos() >= pit->size()
2023 && boost::next(pit) == ownerParagraphs().end();
2027 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
2029 return row.pos() == 0 && pit == ownerParagraphs().begin();