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()
471 - rightMargin(*pit, *bv()->buffer(), row)
472 - leftMargin(pit, row);
474 // inset->textWidth() returns -1 via workWidth(),
477 row.endpos(pit->size() + 1);
481 LyXLayout_ptr const & layout = pit->layout();
483 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
484 row.endpos(addressBreakPoint(row.pos(), *pit) + 1);
488 pos_type const pos = row.pos();
489 pos_type const body_pos = pit->beginningOfBody();
490 pos_type const last = pit->size();
491 pos_type point = last;
494 row.endpos(last + 1);
498 // Now we iterate through until we reach the right margin
499 // or the end of the par, then choose the possible break
502 int const left = leftMargin(pit, row);
505 // pixel width since last breakpoint
510 // We re-use the font resolution for the entire font span when possible
511 LyXFont font = getFont(pit, i);
512 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
514 for ( ; i < last; ++i) {
515 if (pit->isNewline(i)) {
521 InsetOld * in = pit->getInset(i + 1);
522 if (in && in->display()) {
527 in = pit->getInset(i);
528 if (in && in->display()) {
534 char const c = pit->getChar(i);
535 if (i > endPosOfFontSpan) {
536 font = getFont(pit, i);
537 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
542 // add the auto-hfill from label end to the body
543 if (body_pos && i == body_pos) {
544 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
545 if (pit->isLineSeparator(i - 1))
546 thiswidth -= singleWidth(pit, i - 1);
547 int left_margin = labelEnd(pit, row);
548 if (thiswidth + x < left_margin)
549 thiswidth = left_margin - x;
550 thiswidth += singleWidth(pit, i, c, font);
552 thiswidth = singleWidth(pit, i, c, font);
556 //lyxerr << "i: " << i << " x: " << x << " width: " << width << endl;
557 chunkwidth += thiswidth;
559 // break before a character that will fall off
560 // the right of the row
562 // if no break before, break here
563 if (point == last || chunkwidth >= width - left) {
566 // exit on last registered breakpoint:
575 InsetOld * in = pit->getInset(i);
576 if (!in || in->isChar()) {
577 // some insets are line separators too
578 if (pit->isLineSeparator(i)) {
579 // register breakpoint:
586 if (point == last && x >= width) {
587 // didn't find one, break at the point we reached the edge
589 } else if (i == last && x < width) {
590 // found one, but we fell off the end of the par, so prefer
595 // manual labels cannot be broken in LaTeX. But we
596 // want to make our on-screen rendering of footnotes
598 if (body_pos && point < body_pos)
599 point = body_pos - 1;
601 row.endpos(point + 1);
605 // returns the minimum space a row needs on the screen in pixel
606 void LyXText::fill(ParagraphList::iterator pit, Row & row, int paper_width) const
609 // get the pure distance
610 pos_type const last = lastPos(*pit, row);
612 LyXLayout_ptr const & layout = pit->layout();
614 // special handling of the right address boxes
615 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
616 int const tmpfill = row.fill();
617 row.fill(0); // the minfill in leftMargin()
618 w = leftMargin(pit, row);
621 w = leftMargin(pit, row);
624 pos_type const body_pos = pit->beginningOfBody();
625 pos_type i = row.pos();
627 if (! pit->empty() && i <= last) {
628 // We re-use the font resolution for the entire span when possible
629 LyXFont font = getFont(pit, i);
630 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
632 if (body_pos > 0 && i == body_pos) {
633 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
634 if (pit->isLineSeparator(i - 1))
635 w -= singleWidth(pit, i - 1);
636 int left_margin = labelEnd(pit, row);
640 char const c = pit->getChar(i);
641 if (IsPrintable(c) && i > endPosOfFontSpan) {
642 // We need to get the next font
643 font = getFont(pit, i);
644 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
646 w += singleWidth(pit, i, c, font);
650 if (body_pos > 0 && body_pos > last) {
651 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
652 if (last >= 0 && pit->isLineSeparator(last))
653 w -= singleWidth(pit, last);
654 int const left_margin = labelEnd(pit, row);
659 int const fill = paper_width - w - rightMargin(*pit, *bv()->buffer(), row);
661 row.width(paper_width - fill);
665 // returns the minimum space a manual label needs on the screen in pixel
666 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
668 pos_type last = pit->beginningOfBody();
670 BOOST_ASSERT(last > 0);
672 // -1 because a label ends with a space that is in the label
675 // a separator at this end does not count
676 if (pit->isLineSeparator(last))
680 for (pos_type i = row.pos(); i <= last; ++i)
681 w += singleWidth(pit, i);
684 string const & labwidstr = pit->params().labelWidthString();
685 if (!labwidstr.empty()) {
686 LyXFont const labfont = getLabelFont(pit);
687 int const labwidth = font_metrics::width(labwidstr, labfont);
688 fill = max(labwidth - w, 0);
695 LColor_color LyXText::backgroundColor() const
698 return inset_owner->backgroundColor();
700 return LColor::background;
704 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
706 // get the maximum ascent and the maximum descent
707 double layoutasc = 0;
708 double layoutdesc = 0;
711 // ok, let us initialize the maxasc and maxdesc value.
712 // Only the fontsize count. The other properties
713 // are taken from the layoutfont. Nicer on the screen :)
714 LyXLayout_ptr const & layout = pit->layout();
716 // as max get the first character of this row then it can increase but not
717 // decrease the height. Just some point to start with so we don't have to
718 // do the assignment below too often.
719 LyXFont font = getFont(pit, row.pos());
720 LyXFont::FONT_SIZE const tmpsize = font.size();
721 font = getLayoutFont(pit);
722 LyXFont::FONT_SIZE const size = font.size();
723 font.setSize(tmpsize);
725 LyXFont labelfont = getLabelFont(pit);
727 double spacing_val = 1.0;
728 if (!pit->params().spacing().isDefault())
729 spacing_val = pit->params().spacing().getValue();
731 spacing_val = bv()->buffer()->params().spacing().getValue();
732 //lyxerr << "spacing_val = " << spacing_val << endl;
734 // these are minimum values
735 int maxasc = int(font_metrics::maxAscent(font) *
736 layout->spacing.getValue() * spacing_val);
737 int maxdesc = int(font_metrics::maxDescent(font) *
738 layout->spacing.getValue() * spacing_val);
740 // insets may be taller
741 InsetList::iterator ii = pit->insetlist.begin();
742 InsetList::iterator iend = pit->insetlist.end();
743 for ( ; ii != iend; ++ii) {
744 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
745 maxasc = max(maxasc, ii->inset->ascent());
746 maxdesc = max(maxdesc, ii->inset->descent());
750 // Check if any custom fonts are larger (Asger)
751 // This is not completely correct, but we can live with the small,
752 // cosmetic error for now.
754 pos_type const pos_end = lastPos(*pit, row);
756 LyXFont::FONT_SIZE maxsize =
757 pit->highestFontInRange(row.pos(), pos_end, size);
758 if (maxsize > font.size()) {
759 font.setSize(maxsize);
760 maxasc = max(maxasc, font_metrics::maxAscent(font));
761 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
764 // This is nicer with box insets:
768 row.ascent_of_text(maxasc);
772 BufferParams const & bufparams = bv()->buffer()->params();
773 // some parksips VERY EASY IMPLEMENTATION
774 if (bv()->buffer()->params().paragraph_separation ==
775 BufferParams::PARSEP_SKIP)
777 if (layout->isParagraph()
778 && pit->getDepth() == 0
779 && pit != ownerParagraphs().begin())
781 maxasc += bufparams.getDefSkip().inPixels(*bv());
782 } else if (pit != ownerParagraphs().begin() &&
783 boost::prior(pit)->layout()->isParagraph() &&
784 boost::prior(pit)->getDepth() == 0)
786 // is it right to use defskip here too? (AS)
787 maxasc += bufparams.getDefSkip().inPixels(*bv());
792 if (pit == ownerParagraphs().begin() && !isInInset())
793 maxasc += PAPER_MARGIN;
795 // add the vertical spaces, that the user added
796 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
798 // do not forget the DTP-lines!
799 // there height depends on the font of the nearest character
800 if (pit->params().lineTop())
802 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
803 // and now the pagebreaks
804 if (pit->params().pagebreakTop())
805 maxasc += 3 * defaultRowHeight();
807 if (pit->params().startOfAppendix())
808 maxasc += 3 * defaultRowHeight();
810 // This is special code for the chapter, since the label of this
811 // layout is printed in an extra row
812 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
813 float spacing_val = 1.0;
814 if (!pit->params().spacing().isDefault()) {
815 spacing_val = pit->params().spacing().getValue();
817 spacing_val = bufparams.spacing().getValue();
820 labeladdon = int(font_metrics::maxDescent(labelfont) *
821 layout->spacing.getValue() *
823 + int(font_metrics::maxAscent(labelfont) *
824 layout->spacing.getValue() *
828 // special code for the top label
829 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
830 || layout->labeltype == LABEL_BIBLIO
831 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
832 && isFirstInSequence(pit, ownerParagraphs())
833 && !pit->getLabelstring().empty())
835 float spacing_val = 1.0;
836 if (!pit->params().spacing().isDefault()) {
837 spacing_val = pit->params().spacing().getValue();
839 spacing_val = bufparams.spacing().getValue();
843 (font_metrics::maxAscent(labelfont) +
844 font_metrics::maxDescent(labelfont)) *
845 layout->spacing.getValue() *
847 + layout->topsep * defaultRowHeight()
848 + layout->labelbottomsep * defaultRowHeight());
851 // And now the layout spaces, for example before and after
852 // a section, or between the items of a itemize or enumerate
855 if (!pit->params().pagebreakTop()) {
856 ParagraphList::iterator prev =
857 depthHook(pit, ownerParagraphs(),
859 if (prev != pit && prev->layout() == layout &&
860 prev->getDepth() == pit->getDepth() &&
861 prev->getLabelWidthString() == pit->getLabelWidthString())
863 layoutasc = (layout->itemsep * defaultRowHeight());
864 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
865 tmptop = layout->topsep;
868 layoutasc = (tmptop * defaultRowHeight());
869 } else if (pit->params().lineTop()) {
870 tmptop = layout->topsep;
873 layoutasc = (tmptop * defaultRowHeight());
876 prev = outerHook(pit, ownerParagraphs());
877 if (prev != ownerParagraphs().end()) {
878 maxasc += int(prev->layout()->parsep * defaultRowHeight());
879 } else if (pit != ownerParagraphs().begin()) {
880 ParagraphList::iterator prior_pit = boost::prior(pit);
881 if (prior_pit->getDepth() != 0 ||
882 prior_pit->layout() == layout) {
883 maxasc += int(layout->parsep * defaultRowHeight());
889 // is it a bottom line?
890 if (row.endpos() >= pit->size()) {
892 ParagraphList::iterator nextpit = boost::next(pit);
893 if (nextpit == ownerParagraphs().end() && !isInInset())
894 maxdesc += PAPER_MARGIN;
896 // add the vertical spaces, that the user added
897 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
899 // do not forget the DTP-lines!
900 // there height depends on the font of the nearest character
901 if (pit->params().lineBottom())
902 maxdesc += 2 * font_metrics::ascent('x',
903 getFont(pit, max(pos_type(0), pit->size() - 1)));
905 // and now the pagebreaks
906 if (pit->params().pagebreakBottom())
907 maxdesc += 3 * defaultRowHeight();
909 // and now the layout spaces, for example before and after
910 // a section, or between the items of a itemize or enumerate
912 if (!pit->params().pagebreakBottom()
913 && nextpit != ownerParagraphs().end()) {
914 ParagraphList::iterator comparepit = pit;
918 if (comparepit->getDepth() > nextpit->getDepth()) {
919 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
920 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
921 if (comparepit->layout()!= nextpit->layout()
922 || nextpit->getLabelWidthString() !=
923 comparepit->getLabelWidthString())
925 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
928 layoutdesc = unusual;
931 } else if (comparepit->getDepth() == nextpit->getDepth()) {
933 if (comparepit->layout() != nextpit->layout()
934 || nextpit->getLabelWidthString() !=
935 comparepit->getLabelWidthString())
936 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
941 // incalculate the layout spaces
942 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
943 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
945 row.height(maxasc + maxdesc + labeladdon);
946 row.baseline(maxasc + labeladdon);
947 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
951 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
953 // allow only if at start or end, or all previous is new text
954 ParagraphList::iterator cpit = cursorPar();
955 if (cursor.pos() && cursor.pos() != cpit->size()
956 && cpit->isChangeEdited(0, cursor.pos()))
959 LyXTextClass const & tclass =
960 bv()->buffer()->params().getLyXTextClass();
961 LyXLayout_ptr const & layout = cpit->layout();
963 // this is only allowed, if the current paragraph is not empty or caption
964 // and if it has not the keepempty flag active
965 if (cpit->empty() && !cpit->allowEmpty()
966 && layout->labeltype != LABEL_SENSITIVE)
969 recUndo(cursor.par());
971 // Always break behind a space
973 // It is better to erase the space (Dekel)
974 if (cursor.pos() < cpit->size() && cpit->isLineSeparator(cursor.pos()))
975 cpit->erase(cursor.pos());
977 // break the paragraph
981 keep_layout = layout->isEnvironment();
983 // we need to set this before we insert the paragraph. IMO the
984 // breakParagraph call should return a bool if it inserts the
985 // paragraph before or behind and we should react on that one
986 // but we can fix this in 1.3.0 (Jug 20020509)
987 bool const isempty = cpit->allowEmpty() && cpit->empty();
988 ::breakParagraph(bv()->buffer()->params(), paragraphs, cpit,
989 cursor.pos(), keep_layout);
991 #warning Trouble Point! (Lgb)
992 // When ::breakParagraph is called from within an inset we must
993 // ensure that the correct ParagraphList is used. Today that is not
994 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
996 ParagraphList::iterator next_par = boost::next(cpit);
998 // well this is the caption hack since one caption is really enough
999 if (layout->labeltype == LABEL_SENSITIVE) {
1001 // set to standard-layout
1002 cpit->applyLayout(tclass.defaultLayout());
1004 // set to standard-layout
1005 next_par->applyLayout(tclass.defaultLayout());
1008 // if the cursor is at the beginning of a row without prior newline,
1010 // This touches only the screen-update. Otherwise we would may have
1011 // an empty row on the screen
1012 RowList::iterator crit = cpit->getRow(cursor.pos());
1013 if (cursor.pos() && crit->pos() == cursor.pos()
1014 && !cpit->isNewline(cursor.pos() - 1))
1019 while (!next_par->empty() && next_par->isNewline(0))
1023 redoParagraph(cpit);
1024 redoParagraph(next_par);
1026 // This check is necessary. Otherwise the new empty paragraph will
1027 // be deleted automatically. And it is more friendly for the user!
1028 if (cursor.pos() || isempty)
1029 setCursor(next_par, 0);
1035 // convenience function
1036 void LyXText::redoParagraph()
1039 redoParagraph(cursorPar());
1040 setCursorIntern(cursor.par(), cursor.pos());
1044 // insert a character, moves all the following breaks in the
1045 // same Paragraph one to the right and make a rebreak
1046 void LyXText::insertChar(char c)
1048 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
1050 // When the free-spacing option is set for the current layout,
1051 // disable the double-space checking
1053 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1054 cursorPar()->isFreeSpacing();
1056 if (lyxrc.auto_number) {
1057 static string const number_operators = "+-/*";
1058 static string const number_unary_operators = "+-";
1059 static string const number_seperators = ".,:";
1061 if (current_font.number() == LyXFont::ON) {
1062 if (!IsDigit(c) && !contains(number_operators, c) &&
1063 !(contains(number_seperators, c) &&
1064 cursor.pos() >= 1 &&
1065 cursor.pos() < cursorPar()->size() &&
1066 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1067 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1069 number(bv()); // Set current_font.number to OFF
1070 } else if (IsDigit(c) &&
1071 real_current_font.isVisibleRightToLeft()) {
1072 number(bv()); // Set current_font.number to ON
1074 if (cursor.pos() > 0) {
1075 char const c = cursorPar()->getChar(cursor.pos() - 1);
1076 if (contains(number_unary_operators, c) &&
1077 (cursor.pos() == 1 ||
1078 cursorPar()->isSeparator(cursor.pos() - 2) ||
1079 cursorPar()->isNewline(cursor.pos() - 2))
1085 } else if (contains(number_seperators, c) &&
1086 cursor.pos() >= 2 &&
1089 cursor.pos() - 2).number() == LyXFont::ON) {
1100 // First check, if there will be two blanks together or a blank at
1101 // the beginning of a paragraph.
1102 // I decided to handle blanks like normal characters, the main
1103 // difference are the special checks when calculating the row.fill
1104 // (blank does not count at the end of a row) and the check here
1106 // The bug is triggered when we type in a description environment:
1107 // The current_font is not changed when we go from label to main text
1108 // and it should (along with realtmpfont) when we type the space.
1109 // CHECK There is a bug here! (Asger)
1111 // store the current font. This is because of the use of cursor
1112 // movements. The moving cursor would refresh the current font
1113 LyXFont realtmpfont = real_current_font;
1114 LyXFont rawtmpfont = current_font;
1116 if (!freeSpacing && IsLineSeparatorChar(c)) {
1117 if ((cursor.pos() > 0
1118 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1119 || (cursor.pos() > 0
1120 && cursorPar()->isNewline(cursor.pos() - 1))
1121 || (cursor.pos() == 0)) {
1122 static bool sent_space_message = false;
1123 if (!sent_space_message) {
1124 if (cursor.pos() == 0)
1125 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1127 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1128 sent_space_message = true;
1135 // Here case LyXText::InsertInset already inserted the character
1136 if (c != Paragraph::META_INSET)
1137 cursorPar()->insertChar(cursor.pos(), c);
1139 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1141 current_font = rawtmpfont;
1142 real_current_font = realtmpfont;
1143 redoParagraph(cursorPar());
1144 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1150 void LyXText::charInserted()
1152 // Here we could call finishUndo for every 20 characters inserted.
1153 // This is from my experience how emacs does it. (Lgb)
1154 static unsigned int counter;
1164 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
1166 double w = row.fill();
1167 double fill_hfill = 0;
1168 double fill_label_hfill = 0;
1169 double fill_separator = 0;
1173 pit->isRightToLeftPar(bv()->buffer()->params());
1175 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer(), row) : 0;
1177 x = workWidth() > 0 ? leftMargin(pit, row) : 0;
1179 // is there a manual margin with a manual label
1180 LyXLayout_ptr const & layout = pit->layout();
1182 if (layout->margintype == MARGIN_MANUAL
1183 && layout->labeltype == LABEL_MANUAL) {
1184 /// We might have real hfills in the label part
1185 int nlh = numberOfLabelHfills(*pit, row);
1187 // A manual label par (e.g. List) has an auto-hfill
1188 // between the label text and the body of the
1190 // But we don't want to do this auto hfill if the par
1195 if (nlh && !pit->getLabelWidthString().empty()) {
1196 fill_label_hfill = labelFill(pit, row) / double(nlh);
1200 // are there any hfills in the row?
1201 int const nh = numberOfHfills(*pit, row);
1205 fill_hfill = w / nh;
1206 // we don't have to look at the alignment if it is ALIGN_LEFT and
1207 // if the row is already larger then the permitted width as then
1208 // we force the LEFT_ALIGN'edness!
1209 } else if (int(row.width()) < workWidth()) {
1210 // is it block, flushleft or flushright?
1211 // set x how you need it
1213 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1214 align = layout->align;
1216 align = pit->params().align();
1218 // ERT insets should always be LEFT ALIGNED on screen
1219 InsetOld * inset = pit->inInset();
1220 if (inset && inset->owner() &&
1221 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1223 align = LYX_ALIGN_LEFT;
1226 // Display-style insets should always be on a centred row
1227 // The test on pit->size() is to catch zero-size pars, which
1228 // would trigger the assert in Paragraph::getInset().
1229 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1230 inset = pit->isInset(row.pos()) ? pit->getInset(row.pos()) : 0;
1231 if (inset && inset->display()) {
1232 align = LYX_ALIGN_CENTER;
1236 case LYX_ALIGN_BLOCK:
1238 int const ns = numberOfSeparators(*pit, row);
1239 bool disp_inset = false;
1240 if (row.endpos() < pit->size()) {
1241 InsetOld * in = pit->getInset(row.endpos());
1243 disp_inset = in->display();
1245 // If we have separators, this is not the last row of a
1246 // par, does not end in newline, and is not row above a
1247 // display inset... then stretch it
1249 && row.endpos() < pit->size()
1250 && !pit->isNewline(row.endpos())
1253 fill_separator = w / ns;
1254 } else if (is_rtl) {
1259 case LYX_ALIGN_RIGHT:
1262 case LYX_ALIGN_CENTER:
1268 bidi.computeTables(*pit, *bv()->buffer(), row);
1270 pos_type body_pos = pit->beginningOfBody();
1271 pos_type last = lastPos(*pit, row);
1274 (body_pos - 1 > last ||
1275 !pit->isLineSeparator(body_pos - 1))) {
1276 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1277 if (body_pos - 1 <= last)
1278 x += fill_label_hfill;
1282 row.fill_hfill(fill_hfill);
1283 row.fill_label_hfill(fill_label_hfill);
1284 row.fill_separator(fill_separator);
1289 // important for the screen
1292 // the cursor set functions have a special mechanism. When they
1293 // realize, that you left an empty paragraph, they will delete it.
1294 // They also delete the corresponding row
1296 void LyXText::cursorRightOneWord()
1298 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1299 setCursor(cursorPar(), cursor.pos());
1303 // Skip initial whitespace at end of word and move cursor to *start*
1304 // of prior word, not to end of next prior word.
1305 void LyXText::cursorLeftOneWord()
1307 LyXCursor tmpcursor = cursor;
1308 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1309 setCursor(getPar(tmpcursor), tmpcursor.pos());
1313 void LyXText::selectWord(word_location loc)
1315 LyXCursor from = cursor;
1316 LyXCursor to = cursor;
1317 ::getWord(*this, from, to, loc, ownerParagraphs());
1319 setCursor(from.par(), from.pos());
1322 selection.cursor = cursor;
1323 setCursor(to.par(), to.pos());
1328 // Select the word currently under the cursor when no
1329 // selection is currently set
1330 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1332 if (!selection.set()) {
1334 return selection.set();
1340 void LyXText::acceptChange()
1342 if (!selection.set() && cursorPar()->size())
1345 if (selection.start.par() == selection.end.par()) {
1346 LyXCursor & startc = selection.start;
1347 LyXCursor & endc = selection.end;
1348 recordUndo(Undo::INSERT, this, startc.par());
1349 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1352 redoParagraph(getPar(startc));
1353 setCursorIntern(startc.par(), 0);
1355 #warning handle multi par selection
1359 void LyXText::rejectChange()
1361 if (!selection.set() && cursorPar()->size())
1364 if (selection.start.par() == selection.end.par()) {
1365 LyXCursor & startc = selection.start;
1366 LyXCursor & endc = selection.end;
1367 recordUndo(Undo::INSERT, this, startc.par());
1368 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1371 redoParagraph(getPar(startc));
1372 setCursorIntern(startc.par(), 0);
1374 #warning handle multi par selection
1378 // This function is only used by the spellchecker for NextWord().
1379 // It doesn't handle LYX_ACCENTs and probably never will.
1380 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1382 if (the_locking_inset) {
1383 WordLangTuple word =
1384 the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1385 if (!word.word().empty()) {
1386 value += float(cursor.y());
1387 value /= float(height);
1390 // we have to go on checking so move cursor to the next char
1391 if (cursor.pos() == cursorPar()->size()) {
1392 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1394 cursor.par(cursor.par() + 1);
1397 cursor.pos(cursor.pos() + 1);
1400 int const tmppar = cursor.par();
1402 // If this is not the very first word, skip rest of
1403 // current word because we are probably in the middle
1404 // of a word if there is text here.
1405 if (cursor.pos() || cursor.par() != 0) {
1406 while (cursor.pos() < cursorPar()->size()
1407 && cursorPar()->isLetter(cursor.pos()))
1408 cursor.pos(cursor.pos() + 1);
1411 // Now, skip until we have real text (will jump paragraphs)
1413 ParagraphList::iterator cpit = cursorPar();
1414 pos_type const cpos = cursor.pos();
1416 if (cpos == cpit->size()) {
1417 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1418 cursor.par(cursor.par() + 1);
1425 bool const is_good_inset = cpit->isInset(cpos)
1426 && cpit->getInset(cpos)->allowSpellcheck();
1428 if (!isDeletedText(*cpit, cpos)
1429 && (is_good_inset || cpit->isLetter(cpos)))
1432 cursor.pos(cpos + 1);
1435 // now check if we hit an inset so it has to be a inset containing text!
1436 if (cursor.pos() < cursorPar()->size() &&
1437 cursorPar()->isInset(cursor.pos())) {
1439 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1440 cursorPar()->getInset(cursor.pos())->dispatch(cmd);
1441 // now call us again to do the above trick
1442 // but obviously we have to start from down below ;)
1443 return bv()->text->selectNextWordToSpellcheck(value);
1446 // Update the value if we changed paragraphs
1447 if (cursor.par() != tmppar) {
1448 setCursor(cursor.par(), cursor.pos());
1449 value = float(cursor.y())/float(height);
1452 // Start the selection from here
1453 selection.cursor = cursor;
1455 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1456 // and find the end of the word (insets like optional hyphens
1457 // and ligature break are part of a word)
1458 while (cursor.pos() < cursorPar()->size()
1459 && cursorPar()->isLetter(cursor.pos())
1460 && !isDeletedText(*cursorPar(), cursor.pos()))
1461 cursor.pos(cursor.pos() + 1);
1463 // Finally, we copy the word to a string and return it
1465 if (selection.cursor.pos() < cursor.pos()) {
1466 for (pos_type i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1467 if (!cursorPar()->isInset(i))
1468 str += cursorPar()->getChar(i);
1471 return WordLangTuple(str, lang_code);
1475 // This one is also only for the spellchecker
1476 void LyXText::selectSelectedWord()
1478 if (the_locking_inset) {
1479 the_locking_inset->selectSelectedWord(bv());
1482 // move cursor to the beginning
1483 setCursor(selection.cursor.par(), selection.cursor.pos());
1485 // set the sel cursor
1486 selection.cursor = cursor;
1488 // now find the end of the word
1489 while (cursor.pos() < cursorPar()->size()
1490 && cursorPar()->isLetter(cursor.pos()))
1491 cursor.pos(cursor.pos() + 1);
1493 setCursor(cursorPar(), cursor.pos());
1495 // finally set the selection
1500 // Delete from cursor up to the end of the current or next word.
1501 void LyXText::deleteWordForward()
1503 if (cursorPar()->empty())
1506 LyXCursor tmpcursor = cursor;
1507 selection.set(true); // to avoid deletion
1508 cursorRightOneWord();
1509 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1510 selection.cursor = cursor;
1513 cutSelection(true, false);
1518 // Delete from cursor to start of current or prior word.
1519 void LyXText::deleteWordBackward()
1521 if (cursorPar()->empty())
1524 LyXCursor tmpcursor = cursor;
1525 selection.set(true); // to avoid deletion
1526 cursorLeftOneWord();
1527 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1528 selection.cursor = cursor;
1531 cutSelection(true, false);
1536 // Kill to end of line.
1537 void LyXText::deleteLineForward()
1539 if (cursorPar()->empty())
1540 // Paragraph is empty, so we just go to the right
1543 LyXCursor tmpcursor = cursor;
1544 // We can't store the row over a regular setCursor
1545 // so we set it to 0 and reset it afterwards.
1546 selection.set(true); // to avoid deletion
1548 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1549 selection.cursor = cursor;
1552 // What is this test for ??? (JMarc)
1553 if (!selection.set()) {
1554 deleteWordForward();
1556 cutSelection(true, false);
1562 void LyXText::changeCase(LyXText::TextCase action)
1567 if (selection.set()) {
1568 from = selection.start;
1572 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1573 setCursor(to.par(), to.pos() + 1);
1576 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1578 pos_type pos = from.pos();
1579 int par = from.par();
1581 while (par != int(ownerParagraphs().size()) &&
1582 (pos != to.pos() || par != to.par())) {
1583 ParagraphList::iterator pit = getPar(par);
1584 if (pos == pit->size()) {
1589 unsigned char c = pit->getChar(pos);
1590 if (c != Paragraph::META_INSET) {
1592 case text_lowercase:
1595 case text_capitalization:
1597 action = text_lowercase;
1599 case text_uppercase:
1605 pit->setChar(pos, c);
1611 void LyXText::Delete()
1613 // this is a very easy implementation
1615 LyXCursor old_cursor = cursor;
1616 int const old_cur_par_id = cursorPar()->id();
1617 int const old_cur_par_prev_id =
1618 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1620 // just move to the right
1623 // CHECK Look at the comment here.
1624 // This check is not very good...
1625 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1626 // and that can very well delete the par or par->previous in
1627 // old_cursor. Will a solution where we compare paragraph id's
1629 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1630 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1631 // delete-empty-paragraph-mechanism has done it
1635 // if you had success make a backspace
1636 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1637 recordUndo(Undo::DELETE, this, old_cursor.par());
1643 void LyXText::backspace()
1645 // Get the font that is used to calculate the baselineskip
1646 ParagraphList::iterator pit = cursorPar();
1647 pos_type lastpos = pit->size();
1649 if (cursor.pos() == 0) {
1650 // The cursor is at the beginning of a paragraph,
1651 // so the the backspace will collapse two paragraphs into one.
1653 // but it's not allowed unless it's new
1654 if (pit->isChangeEdited(0, pit->size()))
1657 // we may paste some paragraphs
1659 // is it an empty paragraph?
1661 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1662 // This is an empty paragraph and we delete it just
1663 // by moving the cursor one step
1664 // left and let the DeleteEmptyParagraphMechanism
1665 // handle the actual deletion of the paragraph.
1668 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1669 if (cursorPar()->layout() == tmppit->layout()
1670 && cursorPar()->getAlign() == tmppit->getAlign()) {
1671 // Inherit bottom DTD from the paragraph below.
1672 // (the one we are deleting)
1673 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1674 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1675 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1680 // the layout things can change the height of a row !
1686 if (cursor.par() != 0)
1687 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1689 ParagraphList::iterator tmppit = cursorPar();
1690 // We used to do cursorLeftIntern() here, but it is
1691 // not a good idea since it triggers the auto-delete
1692 // mechanism. So we do a cursorLeftIntern()-lite,
1693 // without the dreaded mechanism. (JMarc)
1694 if (cursor.par() != 0) {
1695 // steps into the above paragraph.
1696 setCursorIntern(cursor.par() - 1,
1697 getPar(cursor.par() - 1)->size(),
1701 // Pasting is not allowed, if the paragraphs have different
1702 // layout. I think it is a real bug of all other
1703 // word processors to allow it. It confuses the user.
1704 // Correction: Pasting is always allowed with standard-layout
1705 Buffer & buf = *bv()->buffer();
1706 BufferParams const & bufparams = buf.params();
1707 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1709 if (cursorPar() != tmppit
1710 && (cursorPar()->layout() == tmppit->layout()
1711 || tmppit->layout() == tclass.defaultLayout())
1712 && cursorPar()->getAlign() == tmppit->getAlign()) {
1713 mergeParagraph(bufparams,
1714 buf.paragraphs(), cursorPar());
1716 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1717 cursor.pos(cursor.pos() - 1);
1719 // the row may have changed, block, hfills etc.
1721 setCursor(cursor.par(), cursor.pos(), false);
1724 // this is the code for a normal backspace, not pasting
1726 recordUndo(Undo::DELETE, this, cursor.par());
1727 // We used to do cursorLeftIntern() here, but it is
1728 // not a good idea since it triggers the auto-delete
1729 // mechanism. So we do a cursorLeftIntern()-lite,
1730 // without the dreaded mechanism. (JMarc)
1731 setCursorIntern(cursor.par(), cursor.pos() - 1,
1732 false, cursor.boundary());
1733 cursorPar()->erase(cursor.pos());
1736 lastpos = cursorPar()->size();
1737 if (cursor.pos() == lastpos)
1741 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1745 ParagraphList::iterator LyXText::cursorPar() const
1747 return getPar(cursor.par());
1751 RowList::iterator LyXText::cursorRow() const
1753 return cursorPar()->getRow(cursor.pos());
1757 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1759 return getPar(cur.par());
1763 ParagraphList::iterator LyXText::getPar(int par) const
1765 BOOST_ASSERT(par >= 0);
1766 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1767 ParagraphList::iterator pit = ownerParagraphs().begin();
1768 std::advance(pit, par);
1774 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1776 //lyxerr << "getRowNearY: y " << y << endl;
1778 pit = boost::prior(ownerParagraphs().end());
1780 RowList::iterator rit = lastRow();
1781 RowList::iterator rbegin = firstRow();
1783 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1784 previousRow(pit, rit);
1790 int LyXText::getDepth() const
1792 return cursorPar()->getDepth();
1796 RowList::iterator LyXText::firstRow() const
1798 return ownerParagraphs().front().rows.begin();
1802 RowList::iterator LyXText::lastRow() const
1804 return boost::prior(endRow());
1808 RowList::iterator LyXText::endRow() const
1810 return ownerParagraphs().back().rows.end();
1814 void LyXText::nextRow(ParagraphList::iterator & pit,
1815 RowList::iterator & rit) const
1818 if (rit == pit->rows.end()) {
1820 if (pit == ownerParagraphs().end())
1823 rit = pit->rows.begin();
1828 void LyXText::previousRow(ParagraphList::iterator & pit,
1829 RowList::iterator & rit) const
1831 if (rit != pit->rows.begin())
1834 BOOST_ASSERT(pit != ownerParagraphs().begin());
1836 rit = boost::prior(pit->rows.end());
1841 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1843 if (!selection.set())
1846 // should be const ...
1847 ParagraphList::iterator startpit = getPar(selection.start);
1848 ParagraphList::iterator endpit = getPar(selection.end);
1849 size_t const startpos = selection.start.pos();
1850 size_t const endpos = selection.end.pos();
1852 if (startpit == endpit)
1853 return startpit->asString(buffer, startpos, endpos, label);
1855 // First paragraph in selection
1857 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1859 // The paragraphs in between (if any)
1860 ParagraphList::iterator pit = startpit;
1861 for (++pit; pit != endpit; ++pit)
1862 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1864 // Last paragraph in selection
1865 result += endpit->asString(buffer, 0, endpos, label);
1871 int LyXText::parOffset(ParagraphList::iterator pit) const
1873 return std::distance(ownerParagraphs().begin(), pit);
1877 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1879 // remove rows of paragraph, keep track of height changes
1880 height -= pit->height;
1888 InsetList::iterator ii = pit->insetlist.begin();
1889 InsetList::iterator iend = pit->insetlist.end();
1890 for (; ii != iend; ++ii) {
1892 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
1893 ii->inset->metrics(mi, dim);
1896 // rebreak the paragraph
1897 int const ww = workWidth();
1898 for (pos_type z = 0; z < pit->size() + 1; ) {
1900 rowBreakPoint(pit, row);
1903 prepareToPrint(pit, row);
1904 setHeightOfRow(pit, row);
1905 row.y_offset(pit->height);
1906 pit->rows.push_back(row);
1907 pit->width = std::max(pit->width, row.width());
1908 pit->height += row.height();
1910 height += pit->height;
1911 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1915 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1916 ParagraphList::iterator end)
1918 for ( ; pit != end; ++pit)
1919 redoParagraphInternal(pit);
1920 updateRowPositions();
1924 void LyXText::redoParagraph(ParagraphList::iterator pit)
1926 redoParagraphInternal(pit);
1927 updateRowPositions();
1931 void LyXText::fullRebreak()
1933 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1935 selection.cursor = cursor;
1939 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1941 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1942 // << " workWidth: " << workWidth() << "\nfont: " << mi.base.font << endl;
1943 //BOOST_ASSERT(mi.base.textwidth);
1946 // rebuild row cache. This recomputes height as well.
1947 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1949 width = maxParagraphWidth(ownerParagraphs());
1952 dim.asc = firstRow()->ascent_of_text();
1953 dim.des = height - dim.asc;
1954 dim.wid = std::max(mi.base.textwidth, int(width));
1958 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1960 return row.endpos() >= pit->size()
1961 && boost::next(pit) == ownerParagraphs().end();
1965 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1967 return row.pos() == 0 && pit == ownerParagraphs().begin();