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"
25 #include "dispatchresult.h"
27 #include "funcrequest.h"
31 #include "lyxlength.h"
34 #include "lyxrow_funcs.h"
35 #include "metricsinfo.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "rowpainter.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"
53 using lyx::word_location;
55 using lyx::support::contains;
56 using lyx::support::lowercase;
57 using lyx::support::uppercase;
64 /// top, right, bottom pixel margin
65 extern int const PAPER_MARGIN = 20;
66 /// margin for changebar
67 extern int const CHANGEBAR_MARGIN = 10;
69 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
73 int bibitemMaxWidth(BufferView *, LyXFont const &);
78 unsigned int maxParagraphWidth(ParagraphList const & plist)
80 unsigned int width = 0;
81 ParagraphList::const_iterator pit = plist.begin();
82 ParagraphList::const_iterator end = plist.end();
83 for (; pit != end; ++pit)
84 width = std::max(width, pit->width);
91 BufferView * LyXText::bv()
93 BOOST_ASSERT(bv_owner != 0);
98 double LyXText::spacing(Paragraph const & par) const
100 if (par.params().spacing().isDefault())
101 return bv()->buffer()->params().spacing().getValue();
102 return par.params().spacing().getValue();
106 BufferView * LyXText::bv() const
108 BOOST_ASSERT(bv_owner != 0);
113 void LyXText::updateRowPositions()
115 ParagraphList::iterator pit = ownerParagraphs().begin();
116 ParagraphList::iterator end = ownerParagraphs().end();
117 for (height = 0; pit != end; ++pit) {
119 height += pit->height;
124 int LyXText::workWidth() const
126 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
130 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
132 if (pos >= pit->size())
135 char const c = pit->getChar(pos);
136 LyXFont const & font = getFont(pit, pos);
137 return singleWidth(pit, pos, c, font);
141 int LyXText::singleWidth(ParagraphList::iterator pit,
142 pos_type pos, char c, LyXFont const & font) const
144 if (pos >= pit->size()) {
145 lyxerr << "in singleWidth(), pos: " << pos << endl;
150 // The most common case is handled first (Asger)
151 if (IsPrintable(c)) {
152 if (!font.language()->RightToLeft()) {
153 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
154 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
155 && font.language()->lang() == "arabic") {
156 if (Encodings::IsComposeChar_arabic(c))
159 c = pit->transformChar(c, pos);
160 } else if (font.language()->lang() == "hebrew" &&
161 Encodings::IsComposeChar_hebrew(c))
164 return font_metrics::width(c, font);
167 if (c == Paragraph::META_INSET)
168 return pit->getInset(pos)->width();
170 if (IsSeparatorChar(c))
172 return font_metrics::width(c, font);
176 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
178 LyXTextClass const & tclass =
179 bv()->buffer()->params().getLyXTextClass();
180 LyXLayout_ptr const & layout = pit->layout();
182 string parindent = layout->parindent;
186 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
188 // this is the way, LyX handles the LaTeX-Environments.
189 // I have had this idea very late, so it seems to be a
190 // later added hack and this is true
191 if (pit->getDepth() == 0) {
192 if (pit->layout() == tclass.defaultLayout()) {
193 // find the previous same level paragraph
194 if (pit != ownerParagraphs().begin()) {
195 ParagraphList::iterator newpit =
196 depthHook(pit, ownerParagraphs(), pit->getDepth());
197 if (newpit == pit && newpit->layout()->nextnoindent)
202 // find the next level paragraph
203 ParagraphList::iterator newpar =
204 outerHook(pit, ownerParagraphs());
206 // make a corresponding row. Needed to call leftMargin()
207 // check wether it is a sufficent paragraph
208 if (newpar != ownerParagraphs().end()
209 && newpar->layout()->isEnvironment()) {
210 x = leftMargin(newpar, Row(newpar->size()));
213 if (newpar != ownerParagraphs().end()
214 && pit->layout() == tclass.defaultLayout()) {
215 if (newpar->params().noindent())
218 parindent = newpar->layout()->parindent;
222 LyXFont const labelfont = getLabelFont(pit);
223 switch (layout->margintype) {
225 if (!layout->leftmargin.empty())
226 x += font_metrics::signedWidth(layout->leftmargin,
227 tclass.defaultfont());
228 if (!pit->getLabelstring().empty()) {
229 x += font_metrics::signedWidth(layout->labelindent,
231 x += font_metrics::width(pit->getLabelstring(),
233 x += font_metrics::width(layout->labelsep, labelfont);
238 x += font_metrics::signedWidth(layout->labelindent, labelfont);
239 // The width of an empty par, even with manual label, should be 0
240 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
241 if (!pit->getLabelWidthString().empty()) {
242 x += font_metrics::width(pit->getLabelWidthString(),
244 x += font_metrics::width(layout->labelsep, labelfont);
250 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
251 / (pit->getDepth() + 4);
254 case MARGIN_FIRST_DYNAMIC:
255 if (layout->labeltype == LABEL_MANUAL) {
256 if (row.pos() >= pit->beginningOfBody()) {
257 x += font_metrics::signedWidth(layout->leftmargin,
260 x += font_metrics::signedWidth(layout->labelindent,
264 // Special case to fix problems with
266 || (layout->labeltype == LABEL_STATIC
267 && layout->latextype == LATEX_ENVIRONMENT
268 && !isFirstInSequence(pit, ownerParagraphs()))) {
269 x += font_metrics::signedWidth(layout->leftmargin,
271 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
272 && layout->labeltype != LABEL_BIBLIO
273 && layout->labeltype !=
274 LABEL_CENTERED_TOP_ENVIRONMENT) {
275 x += font_metrics::signedWidth(layout->labelindent,
277 x += font_metrics::width(layout->labelsep, labelfont);
278 x += font_metrics::width(pit->getLabelstring(),
283 case MARGIN_RIGHT_ADDRESS_BOX: {
284 // ok, a terrible hack. The left margin depends on the widest
285 // row in this paragraph.
286 RowList::iterator rit = pit->rows.begin();
287 RowList::iterator end = pit->rows.end();
288 #warning This is wrong.
289 int minfill = workWidth() / 2;
290 for ( ; rit != end; ++rit)
291 if (rit->fill() < minfill)
292 minfill = rit->fill();
293 x += font_metrics::signedWidth(layout->leftmargin,
294 tclass.defaultfont());
300 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
301 LyXLength const len = pit->params().leftIndent();
302 int const tw = inset_owner ?
303 inset_owner->latexTextWidth(bv()) : workWidth();
304 x += len.inPixels(tw);
309 if (pit->params().align() == LYX_ALIGN_LAYOUT)
310 align = layout->align;
312 align = pit->params().align();
314 // set the correct parindent
315 if (row.pos() == 0) {
316 if ((layout->labeltype == LABEL_NO_LABEL
317 || layout->labeltype == LABEL_TOP_ENVIRONMENT
318 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
319 || (layout->labeltype == LABEL_STATIC
320 && layout->latextype == LATEX_ENVIRONMENT
321 && !isFirstInSequence(pit, ownerParagraphs())))
322 && align == LYX_ALIGN_BLOCK
323 && !pit->params().noindent()
324 // in tabulars and ert paragraphs are never indented!
325 && (!pit->inInset() || !pit->inInset()->owner() ||
326 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
327 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
328 && (pit->layout() != tclass.defaultLayout() ||
329 bv()->buffer()->params().paragraph_separation ==
330 BufferParams::PARSEP_INDENT)) {
331 x += font_metrics::signedWidth(parindent,
332 tclass.defaultfont());
333 } else if (layout->labeltype == LABEL_BIBLIO) {
334 // ale970405 Right width for bibitems
335 x += bibitemMaxWidth(bv(), tclass.defaultfont());
343 int LyXText::rightMargin(Paragraph const & par, Buffer const & buf) const
345 LyXTextClass const & tclass = buf.params().getLyXTextClass();
346 LyXLayout_ptr const & layout = par.layout();
349 + font_metrics::signedWidth(tclass.rightmargin(),
350 tclass.defaultfont())
351 + font_metrics::signedWidth(layout->rightmargin,
352 tclass.defaultfont())
353 * 4 / (par.getDepth() + 4);
357 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
359 // labelEnd is only needed if the layout fills a flushleft label.
360 if (pit->layout()->margintype != MARGIN_MANUAL)
364 tmprow.pos(pit->size());
365 // return the beginning of the body
366 return leftMargin(pit, tmprow);
372 // this needs special handling - only newlines count as a break point
373 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
375 pos_type const end = par.size();
378 if (par.isNewline(i))
387 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
389 pos_type const end = pit->size();
390 pos_type const pos = row.pos();
396 // maximum pixel width of a row.
397 int width = workWidth() - rightMargin(*pit, *bv()->buffer());
398 // - leftMargin(pit, row);
400 // inset->textWidth() returns -1 via workWidth(),
407 LyXLayout_ptr const & layout = pit->layout();
409 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
410 row.endpos(addressBreakPoint(pos, *pit));
414 pos_type const body_pos = pit->beginningOfBody();
417 // Now we iterate through until we reach the right margin
418 // or the end of the par, then choose the possible break
421 int const left = leftMargin(pit, row);
424 // pixel width since last breakpoint
428 // We re-use the font resolution for the entire font span when possible
429 LyXFont font = getFont(pit, pos);
430 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(pos);
432 pos_type point = end;
434 for ( ; i < end; ++i) {
435 if (pit->isNewline(i)) {
441 InsetOld * in = pit->getInset(i + 1);
442 if (in && in->display()) {
447 in = pit->getInset(i);
448 if (in && in->display()) {
454 char const c = pit->getChar(i);
456 if (i > endPosOfFontSpan) {
457 font = getFont(pit, i);
458 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
462 int thiswidth = singleWidth(pit, i, c, font);
464 // add the auto-hfill from label end to the body
465 if (body_pos && i == body_pos) {
466 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
467 if (pit->isLineSeparator(i - 1))
468 add -= singleWidth(pit, i - 1);
470 add = std::max(add, labelEnd(pit, row) - x);
475 //lyxerr << "i: " << i << " x: "
476 //<< x << " width: " << width << endl;
477 chunkwidth += thiswidth;
481 // break before a character that will fall off
482 // the right of the row
484 // if no break before, break here
485 if (point == end || chunkwidth >= width - left) {
491 // exit on last registered breakpoint:
499 InsetOld * in = pit->getInset(i);
500 if (!in || in->isChar()) {
501 // some insets are line separators too
502 if (pit->isLineSeparator(i)) {
503 // register breakpoint:
511 // hack removed (connected with the #if 0 above):
512 if (point == end && i != end && x >= width) {
513 // didn't find one, break at the point we reached the edge
517 if (i == end && x < width) {
518 // maybe found one, but the par is short enough.
522 // manual labels cannot be broken in LaTeX. But we
523 // want to make our on-screen rendering of footnotes
525 if (body_pos && point < body_pos)
532 // returns the minimum space a row needs on the screen in pixel
533 void LyXText::fill(ParagraphList::iterator pit, Row & row, int workwidth) const
535 // get the pure distance
536 pos_type const end = row.endpos();
538 LyXLayout_ptr const & layout = pit->layout();
539 int w = leftMargin(pit, row);
541 pos_type const body_pos = pit->beginningOfBody();
542 pos_type i = row.pos();
545 // We re-use the font resolution for the entire span when possible
546 LyXFont font = getFont(pit, i);
547 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
548 for ( ; i < end; ++i) {
549 if (body_pos > 0 && i == body_pos) {
550 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
551 if (pit->isLineSeparator(i - 1))
552 w -= singleWidth(pit, i - 1);
553 w = max(w, labelEnd(pit, row));
555 char const c = pit->getChar(i);
556 if (IsPrintable(c) && i > endPosOfFontSpan) {
557 // We need to get the next font
558 font = getFont(pit, i);
559 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
561 w += singleWidth(pit, i, c, font);
565 if (body_pos > 0 && body_pos >= end) {
566 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
567 if (end > 0 && pit->isLineSeparator(end - 1))
568 w -= singleWidth(pit, end - 1);
569 w = max(w, labelEnd(pit, row));
572 int const fill = workwidth - w - rightMargin(*pit, *bv()->buffer());
574 row.width(workwidth - fill);
578 // returns the minimum space a manual label needs on the screen in pixel
579 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
581 pos_type last = pit->beginningOfBody();
583 BOOST_ASSERT(last > 0);
585 // -1 because a label ends with a space that is in the label
588 // a separator at this end does not count
589 if (pit->isLineSeparator(last))
593 for (pos_type i = row.pos(); i <= last; ++i)
594 w += singleWidth(pit, i);
596 string const & label = pit->params().labelWidthString();
600 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
604 LColor_color LyXText::backgroundColor() const
607 return inset_owner->backgroundColor();
608 return LColor::background;
612 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
614 // get the maximum ascent and the maximum descent
615 double layoutasc = 0;
616 double layoutdesc = 0;
617 double const dh = defaultRowHeight();
619 // ok, let us initialize the maxasc and maxdesc value.
620 // Only the fontsize count. The other properties
621 // are taken from the layoutfont. Nicer on the screen :)
622 LyXLayout_ptr const & layout = pit->layout();
624 // as max get the first character of this row then it can increase but not
625 // decrease the height. Just some point to start with so we don't have to
626 // do the assignment below too often.
627 LyXFont font = getFont(pit, row.pos());
628 LyXFont::FONT_SIZE const tmpsize = font.size();
629 font = getLayoutFont(pit);
630 LyXFont::FONT_SIZE const size = font.size();
631 font.setSize(tmpsize);
633 LyXFont labelfont = getLabelFont(pit);
635 // these are minimum values
636 double const spacing_val = layout->spacing.getValue() * spacing(*pit);
637 //lyxerr << "spacing_val = " << spacing_val << endl;
638 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
639 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
641 // insets may be taller
642 InsetList::iterator ii = pit->insetlist.begin();
643 InsetList::iterator iend = pit->insetlist.end();
644 for ( ; ii != iend; ++ii) {
645 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
646 maxasc = max(maxasc, ii->inset->ascent());
647 maxdesc = max(maxdesc, ii->inset->descent());
651 // Check if any custom fonts are larger (Asger)
652 // This is not completely correct, but we can live with the small,
653 // cosmetic error for now.
655 pos_type const pos_end = row.endpos();
657 LyXFont::FONT_SIZE maxsize =
658 pit->highestFontInRange(row.pos(), pos_end, size);
659 if (maxsize > font.size()) {
660 font.setSize(maxsize);
661 maxasc = max(maxasc, font_metrics::maxAscent(font));
662 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
665 // This is nicer with box insets:
669 row.ascent_of_text(maxasc);
672 if (row.pos() == 0) {
673 BufferParams const & bufparams = bv()->buffer()->params();
674 // some parksips VERY EASY IMPLEMENTATION
675 if (bv()->buffer()->params().paragraph_separation
676 == BufferParams::PARSEP_SKIP
677 && pit != ownerParagraphs().begin()
678 && ((layout->isParagraph() && pit->getDepth() == 0)
679 || (boost::prior(pit)->layout()->isParagraph()
680 && boost::prior(pit)->getDepth() == 0)))
682 maxasc += bufparams.getDefSkip().inPixels(*bv());
686 if (pit == ownerParagraphs().begin() && !isInInset())
687 maxasc += PAPER_MARGIN;
689 // add user added vertical space
690 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
692 if (pit->params().startOfAppendix())
693 maxasc += int(3 * dh);
695 // This is special code for the chapter, since the label of this
696 // layout is printed in an extra row
697 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
698 labeladdon = int(font_metrics::maxHeight(labelfont)
699 * layout->spacing.getValue() * spacing(*pit));
702 // special code for the top label
703 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
704 || layout->labeltype == LABEL_BIBLIO
705 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
706 && isFirstInSequence(pit, ownerParagraphs())
707 && !pit->getLabelstring().empty())
710 font_metrics::maxHeight(labelfont)
711 * layout->spacing.getValue()
713 + (layout->topsep + layout->labelbottomsep) * dh);
716 // And now the layout spaces, for example before and after
717 // a section, or between the items of a itemize or enumerate
720 ParagraphList::iterator prev =
721 depthHook(pit, ownerParagraphs(), pit->getDepth());
723 && prev->layout() == layout
724 && prev->getDepth() == pit->getDepth()
725 && prev->getLabelWidthString() == pit->getLabelWidthString())
727 layoutasc = layout->itemsep * dh;
728 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
729 if (layout->topsep > 0)
730 layoutasc = layout->topsep * dh;
733 prev = outerHook(pit, ownerParagraphs());
734 if (prev != ownerParagraphs().end()) {
735 maxasc += int(prev->layout()->parsep * dh);
736 } else if (pit != ownerParagraphs().begin()) {
737 ParagraphList::iterator prior_pit = boost::prior(pit);
738 if (prior_pit->getDepth() != 0 ||
739 prior_pit->layout() == layout) {
740 maxasc += int(layout->parsep * dh);
745 // is it a bottom line?
746 if (row.endpos() >= pit->size()) {
748 ParagraphList::iterator nextpit = boost::next(pit);
749 if (nextpit == ownerParagraphs().end() && !isInInset())
750 maxdesc += PAPER_MARGIN;
752 // add the vertical spaces, that the user added
753 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
755 // and now the layout spaces, for example before and after
756 // a section, or between the items of a itemize or enumerate
758 if (nextpit != ownerParagraphs().end()) {
759 ParagraphList::iterator cpit = pit;
763 if (cpit->getDepth() > nextpit->getDepth()) {
764 usual = cpit->layout()->bottomsep * dh;
765 cpit = depthHook(cpit, ownerParagraphs(), nextpit->getDepth());
766 if (cpit->layout() != nextpit->layout()
767 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
769 unusual = cpit->layout()->bottomsep * dh;
771 layoutdesc = max(unusual, usual);
772 } else if (cpit->getDepth() == nextpit->getDepth()) {
773 if (cpit->layout() != nextpit->layout()
774 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
775 layoutdesc = int(cpit->layout()->bottomsep * dh);
780 // incalculate the layout spaces
781 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
782 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
784 row.height(maxasc + maxdesc + labeladdon);
785 row.baseline(maxasc + labeladdon);
786 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
790 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
792 // allow only if at start or end, or all previous is new text
793 ParagraphList::iterator cpit = cursorPar();
794 if (cursor.pos() && cursor.pos() != cpit->size()
795 && cpit->isChangeEdited(0, cursor.pos()))
798 LyXTextClass const & tclass =
799 bv()->buffer()->params().getLyXTextClass();
800 LyXLayout_ptr const & layout = cpit->layout();
802 // this is only allowed, if the current paragraph is not empty or caption
803 // and if it has not the keepempty flag active
804 if (cpit->empty() && !cpit->allowEmpty()
805 && layout->labeltype != LABEL_SENSITIVE)
808 recUndo(cursor.par());
810 // Always break behind a space
812 // It is better to erase the space (Dekel)
813 if (cursor.pos() < cpit->size() && cpit->isLineSeparator(cursor.pos()))
814 cpit->erase(cursor.pos());
816 // break the paragraph
820 keep_layout = layout->isEnvironment();
822 // we need to set this before we insert the paragraph. IMO the
823 // breakParagraph call should return a bool if it inserts the
824 // paragraph before or behind and we should react on that one
825 // but we can fix this in 1.3.0 (Jug 20020509)
826 bool const isempty = cpit->allowEmpty() && cpit->empty();
827 ::breakParagraph(bv()->buffer()->params(), paragraphs, cpit,
828 cursor.pos(), keep_layout);
830 #warning Trouble Point! (Lgb)
831 // When ::breakParagraph is called from within an inset we must
832 // ensure that the correct ParagraphList is used. Today that is not
833 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
835 ParagraphList::iterator next_par = boost::next(cpit);
837 // well this is the caption hack since one caption is really enough
838 if (layout->labeltype == LABEL_SENSITIVE) {
840 // set to standard-layout
841 cpit->applyLayout(tclass.defaultLayout());
843 // set to standard-layout
844 next_par->applyLayout(tclass.defaultLayout());
847 // if the cursor is at the beginning of a row without prior newline,
849 // This touches only the screen-update. Otherwise we would may have
850 // an empty row on the screen
851 RowList::iterator crit = cpit->getRow(cursor.pos());
852 if (cursor.pos() && crit->pos() == cursor.pos()
853 && !cpit->isNewline(cursor.pos() - 1))
858 while (!next_par->empty() && next_par->isNewline(0))
863 redoParagraph(next_par);
865 // This check is necessary. Otherwise the new empty paragraph will
866 // be deleted automatically. And it is more friendly for the user!
867 if (cursor.pos() || isempty)
868 setCursor(next_par, 0);
874 // convenience function
875 void LyXText::redoParagraph()
878 redoParagraph(cursorPar());
879 setCursorIntern(cursor.par(), cursor.pos());
883 // insert a character, moves all the following breaks in the
884 // same Paragraph one to the right and make a rebreak
885 void LyXText::insertChar(char c)
887 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
889 // When the free-spacing option is set for the current layout,
890 // disable the double-space checking
892 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
893 cursorPar()->isFreeSpacing();
895 if (lyxrc.auto_number) {
896 static string const number_operators = "+-/*";
897 static string const number_unary_operators = "+-";
898 static string const number_seperators = ".,:";
900 if (current_font.number() == LyXFont::ON) {
901 if (!IsDigit(c) && !contains(number_operators, c) &&
902 !(contains(number_seperators, c) &&
904 cursor.pos() < cursorPar()->size() &&
905 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
906 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
908 number(); // Set current_font.number to OFF
909 } else if (IsDigit(c) &&
910 real_current_font.isVisibleRightToLeft()) {
911 number(); // Set current_font.number to ON
913 if (cursor.pos() > 0) {
914 char const c = cursorPar()->getChar(cursor.pos() - 1);
915 if (contains(number_unary_operators, c) &&
916 (cursor.pos() == 1 ||
917 cursorPar()->isSeparator(cursor.pos() - 2) ||
918 cursorPar()->isNewline(cursor.pos() - 2))
924 } else if (contains(number_seperators, c) &&
928 cursor.pos() - 2).number() == LyXFont::ON) {
938 // First check, if there will be two blanks together or a blank at
939 // the beginning of a paragraph.
940 // I decided to handle blanks like normal characters, the main
941 // difference are the special checks when calculating the row.fill
942 // (blank does not count at the end of a row) and the check here
944 // The bug is triggered when we type in a description environment:
945 // The current_font is not changed when we go from label to main text
946 // and it should (along with realtmpfont) when we type the space.
947 // CHECK There is a bug here! (Asger)
949 // store the current font. This is because of the use of cursor
950 // movements. The moving cursor would refresh the current font
951 LyXFont realtmpfont = real_current_font;
952 LyXFont rawtmpfont = current_font;
954 if (!freeSpacing && IsLineSeparatorChar(c)) {
955 if ((cursor.pos() > 0
956 && cursorPar()->isLineSeparator(cursor.pos() - 1))
958 && cursorPar()->isNewline(cursor.pos() - 1))
959 || (cursor.pos() == 0)) {
960 static bool sent_space_message = false;
961 if (!sent_space_message) {
962 if (cursor.pos() == 0)
963 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
965 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
966 sent_space_message = true;
973 // Here case LyXText::InsertInset already inserted the character
974 if (c != Paragraph::META_INSET)
975 cursorPar()->insertChar(cursor.pos(), c);
977 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
979 current_font = rawtmpfont;
980 real_current_font = realtmpfont;
981 redoParagraph(cursorPar());
982 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
988 void LyXText::charInserted()
990 // Here we call finishUndo for every 20 characters inserted.
991 // This is from my experience how emacs does it. (Lgb)
992 static unsigned int counter;
1002 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
1004 double w = row.fill();
1005 double fill_hfill = 0;
1006 double fill_label_hfill = 0;
1007 double fill_separator = 0;
1011 pit->isRightToLeftPar(bv()->buffer()->params());
1013 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer()) : 0;
1015 x = workWidth() > 0 ? leftMargin(pit, row) : 0;
1017 // is there a manual margin with a manual label
1018 LyXLayout_ptr const & layout = pit->layout();
1020 if (layout->margintype == MARGIN_MANUAL
1021 && layout->labeltype == LABEL_MANUAL) {
1022 /// We might have real hfills in the label part
1023 int nlh = numberOfLabelHfills(*pit, row);
1025 // A manual label par (e.g. List) has an auto-hfill
1026 // between the label text and the body of the
1028 // But we don't want to do this auto hfill if the par
1033 if (nlh && !pit->getLabelWidthString().empty())
1034 fill_label_hfill = labelFill(pit, row) / double(nlh);
1037 // are there any hfills in the row?
1038 int const nh = numberOfHfills(*pit, row);
1042 fill_hfill = w / nh;
1043 // we don't have to look at the alignment if it is ALIGN_LEFT and
1044 // if the row is already larger then the permitted width as then
1045 // we force the LEFT_ALIGN'edness!
1046 } else if (int(row.width()) < workWidth()) {
1047 // is it block, flushleft or flushright?
1048 // set x how you need it
1050 if (pit->params().align() == LYX_ALIGN_LAYOUT)
1051 align = layout->align;
1053 align = pit->params().align();
1055 // Display-style insets should always be on a centred row
1056 // The test on pit->size() is to catch zero-size pars, which
1057 // would trigger the assert in Paragraph::getInset().
1058 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1060 && pit->isInset(row.pos())
1061 && pit->getInset(row.pos())->display())
1063 align = LYX_ALIGN_CENTER;
1067 case LYX_ALIGN_BLOCK: {
1068 int const ns = numberOfSeparators(*pit, row);
1069 bool disp_inset = false;
1070 if (row.endpos() < pit->size()) {
1071 InsetOld * in = pit->getInset(row.endpos());
1073 disp_inset = in->display();
1075 // If we have separators, this is not the last row of a
1076 // par, does not end in newline, and is not row above a
1077 // display inset... then stretch it
1079 && row.endpos() < pit->size()
1080 && !pit->isNewline(row.endpos() - 1)
1083 fill_separator = w / ns;
1084 } else if (is_rtl) {
1089 case LYX_ALIGN_RIGHT:
1092 case LYX_ALIGN_CENTER:
1098 bidi.computeTables(*pit, *bv()->buffer(), row);
1100 pos_type body_pos = pit->beginningOfBody();
1101 pos_type end = row.endpos();
1104 && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1106 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1107 if (body_pos <= end)
1108 x += fill_label_hfill;
1112 row.fill_hfill(fill_hfill);
1113 row.fill_label_hfill(fill_label_hfill);
1114 row.fill_separator(fill_separator);
1119 // the cursor set functions have a special mechanism. When they
1120 // realize, that you left an empty paragraph, they will delete it.
1122 void LyXText::cursorRightOneWord()
1124 cursorRightOneWord(cursor);
1125 setCursor(cursorPar(), cursor.pos());
1129 // Skip initial whitespace at end of word and move cursor to *start*
1130 // of prior word, not to end of next prior word.
1131 void LyXText::cursorLeftOneWord()
1133 LyXCursor tmpcursor = cursor;
1134 cursorLeftOneWord(tmpcursor);
1135 setCursor(getPar(tmpcursor), tmpcursor.pos());
1139 void LyXText::selectWord(word_location loc)
1141 LyXCursor from = cursor;
1142 LyXCursor to = cursor;
1143 getWord(from, to, loc);
1145 setCursor(from.par(), from.pos());
1148 selection.cursor = cursor;
1149 setCursor(to.par(), to.pos());
1154 // Select the word currently under the cursor when no
1155 // selection is currently set
1156 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1158 if (!selection.set()) {
1160 return selection.set();
1166 void LyXText::acceptChange()
1168 if (!selection.set() && cursorPar()->size())
1171 if (selection.start.par() == selection.end.par()) {
1172 LyXCursor & startc = selection.start;
1173 LyXCursor & endc = selection.end;
1174 recordUndo(Undo::INSERT, this, startc.par());
1175 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1178 redoParagraph(getPar(startc));
1179 setCursorIntern(startc.par(), 0);
1181 #warning handle multi par selection
1185 void LyXText::rejectChange()
1187 if (!selection.set() && cursorPar()->size())
1190 if (selection.start.par() == selection.end.par()) {
1191 LyXCursor & startc = selection.start;
1192 LyXCursor & endc = selection.end;
1193 recordUndo(Undo::INSERT, this, startc.par());
1194 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1197 redoParagraph(getPar(startc));
1198 setCursorIntern(startc.par(), 0);
1200 #warning handle multi par selection
1204 // Delete from cursor up to the end of the current or next word.
1205 void LyXText::deleteWordForward()
1207 if (cursorPar()->empty())
1210 LyXCursor tmpcursor = cursor;
1211 selection.set(true); // to avoid deletion
1212 cursorRightOneWord();
1213 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1214 selection.cursor = cursor;
1217 cutSelection(true, false);
1222 // Delete from cursor to start of current or prior word.
1223 void LyXText::deleteWordBackward()
1225 if (cursorPar()->empty())
1228 LyXCursor tmpcursor = cursor;
1229 selection.set(true); // to avoid deletion
1230 cursorLeftOneWord();
1231 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1232 selection.cursor = cursor;
1235 cutSelection(true, false);
1240 // Kill to end of line.
1241 void LyXText::deleteLineForward()
1243 if (cursorPar()->empty()) {
1244 // Paragraph is empty, so we just go to the right
1247 LyXCursor tmpcursor = cursor;
1248 // We can't store the row over a regular setCursor
1249 // so we set it to 0 and reset it afterwards.
1250 selection.set(true); // to avoid deletion
1252 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1253 selection.cursor = cursor;
1256 // What is this test for ??? (JMarc)
1257 if (!selection.set())
1258 deleteWordForward();
1260 cutSelection(true, false);
1265 void LyXText::changeCase(LyXText::TextCase action)
1270 if (selection.set()) {
1271 from = selection.start;
1275 getWord(from, to, lyx::PARTIAL_WORD);
1276 setCursor(to.par(), to.pos() + 1);
1279 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1281 pos_type pos = from.pos();
1282 int par = from.par();
1284 while (par != int(ownerParagraphs().size()) &&
1285 (pos != to.pos() || par != to.par())) {
1286 ParagraphList::iterator pit = getPar(par);
1287 if (pos == pit->size()) {
1292 unsigned char c = pit->getChar(pos);
1293 if (c != Paragraph::META_INSET) {
1295 case text_lowercase:
1298 case text_capitalization:
1300 action = text_lowercase;
1302 case text_uppercase:
1308 pit->setChar(pos, c);
1314 void LyXText::Delete()
1316 // this is a very easy implementation
1317 LyXCursor old_cursor = cursor;
1319 // just move to the right
1322 // if you had success make a backspace
1323 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1324 recordUndo(Undo::DELETE, this, old_cursor.par());
1330 void LyXText::backspace()
1332 // Get the font that is used to calculate the baselineskip
1333 ParagraphList::iterator pit = cursorPar();
1334 pos_type lastpos = pit->size();
1336 if (cursor.pos() == 0) {
1337 // The cursor is at the beginning of a paragraph,
1338 // so the the backspace will collapse two paragraphs into one.
1340 // but it's not allowed unless it's new
1341 if (pit->isChangeEdited(0, pit->size()))
1344 // we may paste some paragraphs
1346 // is it an empty paragraph?
1348 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1349 // This is an empty paragraph and we delete it just
1350 // by moving the cursor one step
1351 // left and let the DeleteEmptyParagraphMechanism
1352 // handle the actual deletion of the paragraph.
1355 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1356 if (cursorPar()->layout() == tmppit->layout()
1357 && cursorPar()->getAlign() == tmppit->getAlign()) {
1358 // Inherit bottom DTD from the paragraph below.
1359 // (the one we are deleting)
1360 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1365 // the layout things can change the height of a row !
1371 if (cursor.par() != 0)
1372 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1374 ParagraphList::iterator tmppit = cursorPar();
1375 // We used to do cursorLeftIntern() here, but it is
1376 // not a good idea since it triggers the auto-delete
1377 // mechanism. So we do a cursorLeftIntern()-lite,
1378 // without the dreaded mechanism. (JMarc)
1379 if (cursor.par() != 0) {
1380 // steps into the above paragraph.
1381 setCursorIntern(cursor.par() - 1,
1382 getPar(cursor.par() - 1)->size(),
1386 // Pasting is not allowed, if the paragraphs have different
1387 // layout. I think it is a real bug of all other
1388 // word processors to allow it. It confuses the user.
1389 // Correction: Pasting is always allowed with standard-layout
1390 Buffer & buf = *bv()->buffer();
1391 BufferParams const & bufparams = buf.params();
1392 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1393 ParagraphList::iterator const cpit = cursorPar();
1396 && (cpit->layout() == tmppit->layout()
1397 || tmppit->layout() == tclass.defaultLayout())
1398 && cpit->getAlign() == tmppit->getAlign()) {
1399 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1401 if (cursor.pos() && cpit->isSeparator(cursor.pos() - 1))
1402 cursor.pos(cursor.pos() - 1);
1404 // the counters may have changed
1406 setCursor(cursor.par(), cursor.pos(), false);
1409 // this is the code for a normal backspace, not pasting
1411 recordUndo(Undo::DELETE, this, cursor.par());
1412 // We used to do cursorLeftIntern() here, but it is
1413 // not a good idea since it triggers the auto-delete
1414 // mechanism. So we do a cursorLeftIntern()-lite,
1415 // without the dreaded mechanism. (JMarc)
1416 setCursorIntern(cursor.par(), cursor.pos() - 1,
1417 false, cursor.boundary());
1418 cursorPar()->erase(cursor.pos());
1421 lastpos = cursorPar()->size();
1422 if (cursor.pos() == lastpos)
1426 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
1430 ParagraphList::iterator LyXText::cursorPar() const
1432 return getPar(cursor.par());
1433 #warning have a look at this again later.
1434 // We need some method to mark the cache as invalidated when
1435 // the paragraph it points get removed, for this to work.
1436 if (cursor.par() != cache_pos_) {
1437 cache_pos_ = cursor.par();
1438 cache_par_ = getPar(cache_pos_);
1444 RowList::iterator LyXText::cursorRow() const
1446 return cursorPar()->getRow(cursor.pos());
1450 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1452 return getPar(cur.par());
1456 ParagraphList::iterator LyXText::getPar(int par) const
1458 BOOST_ASSERT(par >= 0);
1459 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1460 ParagraphList::iterator pit = ownerParagraphs().begin();
1461 std::advance(pit, par);
1467 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1469 //lyxerr << "getRowNearY: y " << y << endl;
1471 ParagraphList::iterator const pend = ownerParagraphs().end();
1472 pit = ownerParagraphs().begin();
1473 while (int(pit->y + pit->height) < y && pit != pend)
1476 RowList::iterator rit = pit->rows.begin();
1477 RowList::iterator const rend = pit->rows.end();
1478 while (int(pit->y + rit->y_offset()) < y && rit != rend)
1483 pit = boost::prior(ownerParagraphs().end());
1485 RowList::iterator rit = lastRow();
1486 RowList::iterator rbegin = firstRow();
1488 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1489 previousRow(pit, rit);
1496 int LyXText::getDepth() const
1498 return cursorPar()->getDepth();
1502 RowList::iterator LyXText::firstRow() const
1504 return ownerParagraphs().front().rows.begin();
1508 RowList::iterator LyXText::lastRow() const
1510 return boost::prior(endRow());
1514 RowList::iterator LyXText::endRow() const
1516 return ownerParagraphs().back().rows.end();
1520 void LyXText::nextRow(ParagraphList::iterator & pit,
1521 RowList::iterator & rit) const
1524 if (rit == pit->rows.end()) {
1526 if (pit == ownerParagraphs().end())
1529 rit = pit->rows.begin();
1534 void LyXText::previousRow(ParagraphList::iterator & pit,
1535 RowList::iterator & rit) const
1537 if (rit != pit->rows.begin())
1540 BOOST_ASSERT(pit != ownerParagraphs().begin());
1542 rit = boost::prior(pit->rows.end());
1547 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1549 if (!selection.set())
1552 // should be const ...
1553 ParagraphList::iterator startpit = getPar(selection.start);
1554 ParagraphList::iterator endpit = getPar(selection.end);
1555 size_t const startpos = selection.start.pos();
1556 size_t const endpos = selection.end.pos();
1558 if (startpit == endpit)
1559 return startpit->asString(buffer, startpos, endpos, label);
1561 // First paragraph in selection
1563 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1565 // The paragraphs in between (if any)
1566 ParagraphList::iterator pit = startpit;
1567 for (++pit; pit != endpit; ++pit)
1568 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1570 // Last paragraph in selection
1571 result += endpit->asString(buffer, 0, endpos, label);
1577 int LyXText::parOffset(ParagraphList::iterator pit) const
1579 return std::distance(ownerParagraphs().begin(), pit);
1583 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1585 // remove rows of paragraph, keep track of height changes
1586 height -= pit->height;
1594 InsetList::iterator ii = pit->insetlist.begin();
1595 InsetList::iterator iend = pit->insetlist.end();
1596 for (; ii != iend; ++ii) {
1598 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
1599 ii->inset->metrics(mi, dim);
1602 // rebreak the paragraph
1603 int const ww = workWidth();
1607 rowBreakPoint(pit, row);
1610 prepareToPrint(pit, row);
1611 setHeightOfRow(pit, row);
1612 row.y_offset(pit->height);
1613 pit->rows.push_back(row);
1614 pit->width = std::max(pit->width, row.width());
1615 pit->height += row.height();
1616 } while (z < pit->size());
1618 height += pit->height;
1619 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1623 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1624 ParagraphList::iterator end)
1626 for ( ; pit != end; ++pit)
1627 redoParagraphInternal(pit);
1628 updateRowPositions();
1632 void LyXText::redoParagraph(ParagraphList::iterator pit)
1634 redoParagraphInternal(pit);
1635 updateRowPositions();
1639 void LyXText::fullRebreak()
1641 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1643 selection.cursor = cursor;
1647 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1649 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1650 // << " workWidth: " << workWidth() << "\nfont: " << mi.base.font << endl;
1651 //BOOST_ASSERT(mi.base.textwidth);
1653 // rebuild row cache. This recomputes height as well.
1654 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1656 width = maxParagraphWidth(ownerParagraphs());
1659 dim.asc = firstRow()->ascent_of_text();
1660 dim.des = height - dim.asc;
1661 dim.wid = std::max(mi.base.textwidth, int(width));
1665 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1667 return row.endpos() >= pit->size()
1668 && boost::next(pit) == ownerParagraphs().end();
1672 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1674 return row.pos() == 0 && pit == ownerParagraphs().begin();
1678 void LyXText::cursorLeftOneWord(LyXCursor & cursor)
1680 // treat HFills, floats and Insets as words
1682 ParagraphList::iterator pit = cursorPar();
1683 size_t pos = cursor.pos();
1686 (pit->isSeparator(pos - 1) ||
1687 pit->isKomma(pos - 1) ||
1688 pit->isNewline(pos - 1)) &&
1689 !(pit->isHfill(pos - 1) ||
1690 pit->isInset(pos - 1)))
1694 (pit->isInset(pos - 1) ||
1695 pit->isHfill(pos - 1))) {
1698 if (pit != ownerParagraphs().begin()) {
1702 } else { // Here, cur != 0
1703 while (pos > 0 && pit->isWord(pos - 1))
1707 cursor.par(parOffset(pit));
1712 void LyXText::cursorRightOneWord(LyXCursor & cursor)
1714 // treat floats, HFills and Insets as words
1715 ParagraphList::iterator pit = cursorPar();
1716 pos_type pos = cursor.pos();
1718 if (pos == pit->size() &&
1719 boost::next(pit) != ownerParagraphs().end()) {
1723 // Skip through initial nonword stuff.
1724 while (pos < pit->size() && !pit->isWord(pos)) {
1727 // Advance through word.
1728 while (pos < pit->size() && pit->isWord(pos)) {
1733 cursor.par(parOffset(pit));
1738 void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location const loc)
1740 ParagraphList::iterator from_par = getPar(from);
1741 ParagraphList::iterator to_par = getPar(to);
1743 case lyx::WHOLE_WORD_STRICT:
1744 if (from.pos() == 0 || from.pos() == from_par->size()
1745 || from_par->isSeparator(from.pos())
1746 || from_par->isKomma(from.pos())
1747 || from_par->isNewline(from.pos())
1748 || from_par->isSeparator(from.pos() - 1)
1749 || from_par->isKomma(from.pos() - 1)
1750 || from_par->isNewline(from.pos() - 1)) {
1754 // no break here, we go to the next
1756 case lyx::WHOLE_WORD:
1757 // Move cursor to the beginning, when not already there.
1758 if (from.pos() && !from_par->isSeparator(from.pos() - 1)
1759 && !(from_par->isKomma(from.pos() - 1)
1760 || from_par->isNewline(from.pos() - 1)))
1761 cursorLeftOneWord(from);
1763 case lyx::PREVIOUS_WORD:
1764 // always move the cursor to the beginning of previous word
1765 cursorLeftOneWord(from);
1767 case lyx::NEXT_WORD:
1768 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1771 case lyx::PARTIAL_WORD:
1775 while (to.pos() < to_par->size()
1776 && !to_par->isSeparator(to.pos())
1777 && !to_par->isKomma(to.pos())
1778 && !to_par->isNewline(to.pos())
1779 && !to_par->isHfill(to.pos())
1780 && !to_par->isInset(to.pos()))
1782 to.pos(to.pos() + 1);