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"
35 #include "lyxrow_funcs.h"
36 #include "metricsinfo.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
43 #include "WordLangTuple.h"
45 #include "frontends/font_metrics.h"
46 #include "frontends/LyXView.h"
48 #include "insets/insettext.h"
50 #include "support/lstrings.h"
51 #include "support/textutils.h"
54 using lyx::word_location;
56 using lyx::support::contains;
57 using lyx::support::lowercase;
58 using lyx::support::uppercase;
65 /// some space for drawing the 'nested' markers (in pixel)
66 extern int const NEST_MARGIN = 20;
67 /// margin for changebar
68 extern int const CHANGEBAR_MARGIN = 10;
70 extern int const RIGHT_MARGIN = 10;
75 unsigned int maxParagraphWidth(ParagraphList const & plist)
77 unsigned int width = 0;
78 ParagraphList::const_iterator pit = plist.begin();
79 ParagraphList::const_iterator end = plist.end();
80 for (; pit != end; ++pit)
81 width = std::max(width, pit->width);
88 BufferView * LyXText::bv()
90 BOOST_ASSERT(bv_owner != 0);
95 double LyXText::spacing(Paragraph const & par) const
97 if (par.params().spacing().isDefault())
98 return bv()->buffer()->params().spacing().getValue();
99 return par.params().spacing().getValue();
103 BufferView * LyXText::bv() const
105 BOOST_ASSERT(bv_owner != 0);
110 void LyXText::updateParPositions()
112 ParagraphList::iterator pit = paragraphs().begin();
113 ParagraphList::iterator end = paragraphs().end();
114 for (height = 0; pit != end; ++pit) {
116 height += pit->height;
121 int LyXText::textWidth() const
127 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
129 if (pos >= pit->size())
132 char const c = pit->getChar(pos);
133 return singleWidth(pit, pos, c, getFont(pit, pos));
137 int LyXText::singleWidth(ParagraphList::iterator pit,
138 pos_type pos, char c, LyXFont const & font) const
140 if (pos >= pit->size()) {
141 lyxerr << "in singleWidth(), pos: " << pos << endl;
146 // The most common case is handled first (Asger)
147 if (IsPrintable(c)) {
148 if (!font.language()->RightToLeft()) {
149 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
150 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
151 && font.language()->lang() == "arabic") {
152 if (Encodings::IsComposeChar_arabic(c))
155 c = pit->transformChar(c, pos);
156 } else if (font.language()->lang() == "hebrew" &&
157 Encodings::IsComposeChar_hebrew(c))
160 return font_metrics::width(c, font);
163 if (c == Paragraph::META_INSET)
164 return pit->getInset(pos)->width();
166 if (IsSeparatorChar(c))
168 return font_metrics::width(c, font);
172 int LyXText::leftMargin(ParagraphList::iterator pit) const
174 return leftMargin(pit, pit->size());
178 int LyXText::leftMargin(ParagraphList::iterator pit, pos_type pos) const
180 LyXTextClass const & tclass =
181 bv()->buffer()->params().getLyXTextClass();
182 LyXLayout_ptr const & layout = pit->layout();
184 string parindent = layout->parindent;
186 int x = NEST_MARGIN + CHANGEBAR_MARGIN;
188 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
190 // This is the way LyX handles LaTeX-Environments.
191 // I have had this idea very late, so it seems to be a
192 // later added hack and this is true
193 if (pit->getDepth() == 0) {
194 if (pit->layout() == tclass.defaultLayout()) {
195 // find the previous same level paragraph
196 if (pit != paragraphs().begin()) {
197 ParagraphList::iterator newpit =
198 depthHook(pit, paragraphs(), pit->getDepth());
199 if (newpit == pit && newpit->layout()->nextnoindent)
204 // find the next level paragraph
205 ParagraphList::iterator newpar =
206 outerHook(pit, paragraphs());
208 // Make a corresponding row. Need to call leftMargin()
209 // to check whether it is a sufficent paragraph.
210 if (newpar != paragraphs().end()
211 && newpar->layout()->isEnvironment()) {
212 x = leftMargin(newpar);
215 if (newpar != paragraphs().end()
216 && pit->layout() == tclass.defaultLayout()) {
217 if (newpar->params().noindent())
220 parindent = newpar->layout()->parindent;
224 LyXFont const labelfont = getLabelFont(pit);
225 switch (layout->margintype) {
227 if (!layout->leftmargin.empty())
228 x += font_metrics::signedWidth(layout->leftmargin,
229 tclass.defaultfont());
230 if (!pit->getLabelstring().empty()) {
231 x += font_metrics::signedWidth(layout->labelindent,
233 x += font_metrics::width(pit->getLabelstring(),
235 x += font_metrics::width(layout->labelsep, labelfont);
240 x += font_metrics::signedWidth(layout->labelindent, labelfont);
241 // The width of an empty par, even with manual label, should be 0
242 if (!pit->empty() && pos >= pit->beginOfBody()) {
243 if (!pit->getLabelWidthString().empty()) {
244 x += font_metrics::width(pit->getLabelWidthString(),
246 x += font_metrics::width(layout->labelsep, labelfont);
252 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
253 / (pit->getDepth() + 4);
256 case MARGIN_FIRST_DYNAMIC:
257 if (layout->labeltype == LABEL_MANUAL) {
258 if (pos >= pit->beginOfBody()) {
259 x += font_metrics::signedWidth(layout->leftmargin,
262 x += font_metrics::signedWidth(layout->labelindent,
266 // Special case to fix problems with
268 || (layout->labeltype == LABEL_STATIC
269 && layout->latextype == LATEX_ENVIRONMENT
270 && !isFirstInSequence(pit, paragraphs()))) {
271 x += font_metrics::signedWidth(layout->leftmargin,
273 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
274 && layout->labeltype != LABEL_BIBLIO
275 && layout->labeltype !=
276 LABEL_CENTERED_TOP_ENVIRONMENT) {
277 x += font_metrics::signedWidth(layout->labelindent,
279 x += font_metrics::width(layout->labelsep, labelfont);
280 x += font_metrics::width(pit->getLabelstring(),
285 case MARGIN_RIGHT_ADDRESS_BOX: {
287 // ok, a terrible hack. The left margin depends on the widest
288 // row in this paragraph.
289 RowList::iterator rit = pit->rows.begin();
290 RowList::iterator end = pit->rows.end();
291 #warning This is wrong.
292 int minfill = textwidth_;
293 for ( ; rit != end; ++rit)
294 if (rit->fill() < minfill)
295 minfill = rit->fill();
296 x += font_metrics::signedWidth(layout->leftmargin,
297 tclass.defaultfont());
300 // also wrong, but much shorter.
306 if (!pit->params().leftIndent().zero())
307 x += pit->params().leftIndent().inPixels(textWidth());
311 if (pit->params().align() == LYX_ALIGN_LAYOUT)
312 align = layout->align;
314 align = pit->params().align();
316 // set the correct parindent
318 && (layout->labeltype == LABEL_NO_LABEL
319 || layout->labeltype == LABEL_TOP_ENVIRONMENT
320 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
321 || (layout->labeltype == LABEL_STATIC
322 && layout->latextype == LATEX_ENVIRONMENT
323 && !isFirstInSequence(pit, paragraphs())))
324 && align == LYX_ALIGN_BLOCK
325 && !pit->params().noindent()
326 // in tabulars and ert paragraphs are never indented!
328 || !pit->inInset()->owner()
329 || (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE
330 && pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
331 && (pit->layout() != tclass.defaultLayout()
332 || bv()->buffer()->params().paragraph_separation ==
333 BufferParams::PARSEP_INDENT))
335 x += font_metrics::signedWidth(parindent, tclass.defaultfont());
342 int LyXText::rightMargin(Paragraph const & par) const
344 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
348 + font_metrics::signedWidth(tclass.rightmargin(),
349 tclass.defaultfont())
350 + font_metrics::signedWidth(par.layout()->rightmargin,
351 tclass.defaultfont())
352 * 4 / (par.getDepth() + 4);
356 int LyXText::labelEnd(ParagraphList::iterator pit) const
358 // labelEnd is only needed if the layout fills a flushleft label.
359 if (pit->layout()->margintype != MARGIN_MANUAL)
361 // return the beginning of the body
362 return leftMargin(pit);
368 // this needs special handling - only newlines count as a break point
369 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
371 pos_type const end = par.size();
374 if (par.isNewline(i))
383 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
385 pos_type const end = pit->size();
386 pos_type const pos = row.pos();
392 // maximum pixel width of a row
393 int width = textWidth() - rightMargin(*pit); // - leftMargin(pit, row);
399 LyXLayout_ptr const & layout = pit->layout();
401 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
402 row.endpos(addressBreakPoint(pos, *pit));
406 pos_type const body_pos = pit->beginOfBody();
409 // Now we iterate through until we reach the right margin
410 // or the end of the par, then choose the possible break
413 int const left = leftMargin(pit, pos);
416 // pixel width since last breakpoint
420 // We re-use the font resolution for the entire font span when possible
421 LyXFont font = getFont(pit, pos);
422 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(pos);
424 pos_type point = end;
426 for ( ; i < end; ++i) {
427 if (pit->isNewline(i)) {
433 if (pit->isInset(i + 1) && pit->getInset(i + 1)->display()) {
438 if (pit->isInset(i) && pit->getInset(i)->display()) {
444 char const c = pit->getChar(i);
446 if (i > endPosOfFontSpan) {
447 font = getFont(pit, i);
448 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
452 int thiswidth = singleWidth(pit, i, c, font);
454 // add the auto-hfill from label end to the body
455 if (body_pos && i == body_pos) {
456 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
457 if (pit->isLineSeparator(i - 1))
458 add -= singleWidth(pit, i - 1);
460 add = std::max(add, labelEnd(pit) - x);
465 //lyxerr << "i: " << i << " x: "
466 //<< x << " width: " << width << endl;
467 chunkwidth += thiswidth;
470 // break before a character that will fall off
471 // the right of the row
473 // if no break before, break here
474 if (point == end || chunkwidth >= width - left) {
480 // exit on last registered breakpoint:
484 if (!pit->isInset(i) || pit->getInset(i)->isChar()) {
485 // some insets are line separators too
486 if (pit->isLineSeparator(i)) {
487 // register breakpoint:
494 if (i == end && x < width) {
495 // maybe found one, but the par is short enough.
499 // manual labels cannot be broken in LaTeX. But we
500 // want to make our on-screen rendering of footnotes
502 if (body_pos && point < body_pos)
509 // returns the minimum space a row needs on the screen in pixel
510 void LyXText::fill(ParagraphList::iterator pit, Row & row, int workwidth) const
512 // get the pure distance
513 pos_type const end = row.endpos();
515 string labelsep = pit->layout()->labelsep;
516 int w = leftMargin(pit, row.pos());
518 pos_type const body_pos = pit->beginOfBody();
519 pos_type i = row.pos();
522 // We re-use the font resolution for the entire span when possible
523 LyXFont font = getFont(pit, i);
524 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
525 for ( ; i < end; ++i) {
526 if (body_pos > 0 && i == body_pos) {
527 w += font_metrics::width(labelsep, getLabelFont(pit));
528 if (pit->isLineSeparator(i - 1))
529 w -= singleWidth(pit, i - 1);
530 w = max(w, labelEnd(pit));
532 char const c = pit->getChar(i);
533 if (IsPrintable(c) && i > endPosOfFontSpan) {
534 // We need to get the next font
535 font = getFont(pit, i);
536 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
538 w += singleWidth(pit, i, c, font);
542 if (body_pos > 0 && body_pos >= end) {
543 w += font_metrics::width(labelsep, getLabelFont(pit));
544 if (end > 0 && pit->isLineSeparator(end - 1))
545 w -= singleWidth(pit, end - 1);
546 w = max(w, labelEnd(pit));
549 int const fill = workwidth - w - rightMargin(*pit);
551 row.width(workwidth - fill);
555 // returns the minimum space a manual label needs on the screen in pixel
556 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
558 pos_type last = pit->beginOfBody();
560 BOOST_ASSERT(last > 0);
562 // -1 because a label ends with a space that is in the label
565 // a separator at this end does not count
566 if (pit->isLineSeparator(last))
570 for (pos_type i = row.pos(); i <= last; ++i)
571 w += singleWidth(pit, i);
573 string const & label = pit->params().labelWidthString();
577 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
581 LColor_color LyXText::backgroundColor() const
583 return LColor_color(LColor::color(background_color_));
587 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
589 // get the maximum ascent and the maximum descent
590 double layoutasc = 0;
591 double layoutdesc = 0;
592 double const dh = defaultRowHeight();
594 // ok, let us initialize the maxasc and maxdesc value.
595 // Only the fontsize count. The other properties
596 // are taken from the layoutfont. Nicer on the screen :)
597 LyXLayout_ptr const & layout = pit->layout();
599 // as max get the first character of this row then it can increase but not
600 // decrease the height. Just some point to start with so we don't have to
601 // do the assignment below too often.
602 LyXFont font = getFont(pit, row.pos());
603 LyXFont::FONT_SIZE const tmpsize = font.size();
604 font = getLayoutFont(pit);
605 LyXFont::FONT_SIZE const size = font.size();
606 font.setSize(tmpsize);
608 LyXFont labelfont = getLabelFont(pit);
610 // these are minimum values
611 double const spacing_val = layout->spacing.getValue() * spacing(*pit);
612 //lyxerr << "spacing_val = " << spacing_val << endl;
613 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
614 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
616 // insets may be taller
617 InsetList::iterator ii = pit->insetlist.begin();
618 InsetList::iterator iend = pit->insetlist.end();
619 for ( ; ii != iend; ++ii) {
620 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
621 maxasc = max(maxasc, ii->inset->ascent());
622 maxdesc = max(maxdesc, ii->inset->descent());
626 // Check if any custom fonts are larger (Asger)
627 // This is not completely correct, but we can live with the small,
628 // cosmetic error for now.
630 pos_type const pos_end = row.endpos();
632 LyXFont::FONT_SIZE maxsize =
633 pit->highestFontInRange(row.pos(), pos_end, size);
634 if (maxsize > font.size()) {
635 font.setSize(maxsize);
636 maxasc = max(maxasc, font_metrics::maxAscent(font));
637 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
640 // This is nicer with box insets:
644 row.ascent_of_text(maxasc);
647 if (row.pos() == 0) {
648 BufferParams const & bufparams = bv()->buffer()->params();
649 // some parksips VERY EASY IMPLEMENTATION
650 if (bv()->buffer()->params().paragraph_separation
651 == BufferParams::PARSEP_SKIP
652 && pit != paragraphs().begin()
653 && ((layout->isParagraph() && pit->getDepth() == 0)
654 || (boost::prior(pit)->layout()->isParagraph()
655 && boost::prior(pit)->getDepth() == 0)))
657 maxasc += bufparams.getDefSkip().inPixels(*bv());
660 if (pit->params().startOfAppendix())
661 maxasc += int(3 * dh);
663 // This is special code for the chapter, since the label of this
664 // layout is printed in an extra row
665 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
666 labeladdon = int(font_metrics::maxHeight(labelfont)
667 * layout->spacing.getValue() * spacing(*pit));
670 // special code for the top label
671 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
672 || layout->labeltype == LABEL_BIBLIO
673 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
674 && isFirstInSequence(pit, paragraphs())
675 && !pit->getLabelstring().empty())
678 font_metrics::maxHeight(labelfont)
679 * layout->spacing.getValue()
681 + (layout->topsep + layout->labelbottomsep) * dh);
684 // Add the layout spaces, for example before and after
685 // a section, or between the items of a itemize or enumerate
688 ParagraphList::iterator prev =
689 depthHook(pit, paragraphs(), pit->getDepth());
691 && prev->layout() == layout
692 && prev->getDepth() == pit->getDepth()
693 && prev->getLabelWidthString() == pit->getLabelWidthString())
695 layoutasc = layout->itemsep * dh;
696 } else if (pit != paragraphs().begin() || row.pos() != 0) {
697 if (layout->topsep > 0)
698 layoutasc = layout->topsep * dh;
701 prev = outerHook(pit, paragraphs());
702 if (prev != paragraphs().end()) {
703 maxasc += int(prev->layout()->parsep * dh);
704 } else if (pit != paragraphs().begin()) {
705 ParagraphList::iterator prior_pit = boost::prior(pit);
706 if (prior_pit->getDepth() != 0 ||
707 prior_pit->layout() == layout) {
708 maxasc += int(layout->parsep * dh);
713 // is it a bottom line?
714 if (row.endpos() >= pit->size()) {
715 // add the layout spaces, for example before and after
716 // a section, or between the items of a itemize or enumerate
718 ParagraphList::iterator nextpit = boost::next(pit);
719 if (nextpit != paragraphs().end()) {
720 ParagraphList::iterator cpit = pit;
724 if (cpit->getDepth() > nextpit->getDepth()) {
725 usual = cpit->layout()->bottomsep * dh;
726 cpit = depthHook(cpit, paragraphs(), nextpit->getDepth());
727 if (cpit->layout() != nextpit->layout()
728 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
730 unusual = cpit->layout()->bottomsep * dh;
732 layoutdesc = max(unusual, usual);
733 } else if (cpit->getDepth() == nextpit->getDepth()) {
734 if (cpit->layout() != nextpit->layout()
735 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
736 layoutdesc = int(cpit->layout()->bottomsep * dh);
741 // incalculate the layout spaces
742 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
743 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
745 row.height(maxasc + maxdesc + labeladdon);
746 row.baseline(maxasc + labeladdon);
747 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
751 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
753 // allow only if at start or end, or all previous is new text
754 ParagraphList::iterator cpit = cursorPar();
755 if (cursor.pos() && cursor.pos() != cpit->size()
756 && cpit->isChangeEdited(0, cursor.pos()))
759 LyXTextClass const & tclass =
760 bv()->buffer()->params().getLyXTextClass();
761 LyXLayout_ptr const & layout = cpit->layout();
763 // this is only allowed, if the current paragraph is not empty or caption
764 // and if it has not the keepempty flag active
765 if (cpit->empty() && !cpit->allowEmpty()
766 && layout->labeltype != LABEL_SENSITIVE)
769 recUndo(cursor.par());
771 // Always break behind a space
773 // It is better to erase the space (Dekel)
774 if (cursor.pos() < cpit->size() && cpit->isLineSeparator(cursor.pos()))
775 cpit->erase(cursor.pos());
777 // break the paragraph
781 keep_layout = layout->isEnvironment();
783 // we need to set this before we insert the paragraph. IMO the
784 // breakParagraph call should return a bool if it inserts the
785 // paragraph before or behind and we should react on that one
786 // but we can fix this in 1.3.0 (Jug 20020509)
787 bool const isempty = cpit->allowEmpty() && cpit->empty();
788 ::breakParagraph(bv()->buffer()->params(), paragraphs, cpit,
789 cursor.pos(), keep_layout);
791 #warning Trouble Point! (Lgb)
792 // When ::breakParagraph is called from within an inset we must
793 // ensure that the correct ParagraphList is used. Today that is not
794 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
796 ParagraphList::iterator next_par = boost::next(cpit);
798 // well this is the caption hack since one caption is really enough
799 if (layout->labeltype == LABEL_SENSITIVE) {
801 // set to standard-layout
802 cpit->applyLayout(tclass.defaultLayout());
804 // set to standard-layout
805 next_par->applyLayout(tclass.defaultLayout());
808 // if the cursor is at the beginning of a row without prior newline,
810 // This touches only the screen-update. Otherwise we would may have
811 // an empty row on the screen
812 RowList::iterator crit = cpit->getRow(cursor.pos());
813 if (cursor.pos() && crit->pos() == cursor.pos()
814 && !cpit->isNewline(cursor.pos() - 1))
819 while (!next_par->empty() && next_par->isNewline(0))
824 redoParagraph(next_par);
826 // This check is necessary. Otherwise the new empty paragraph will
827 // be deleted automatically. And it is more friendly for the user!
828 if (cursor.pos() || isempty)
829 setCursor(next_par, 0);
835 // convenience function
836 void LyXText::redoParagraph()
839 redoParagraph(cursorPar());
840 setCursorIntern(cursor.par(), cursor.pos());
844 // insert a character, moves all the following breaks in the
845 // same Paragraph one to the right and make a rebreak
846 void LyXText::insertChar(char c)
848 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
850 // When the free-spacing option is set for the current layout,
851 // disable the double-space checking
853 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
854 cursorPar()->isFreeSpacing();
856 if (lyxrc.auto_number) {
857 static string const number_operators = "+-/*";
858 static string const number_unary_operators = "+-";
859 static string const number_seperators = ".,:";
861 if (current_font.number() == LyXFont::ON) {
862 if (!IsDigit(c) && !contains(number_operators, c) &&
863 !(contains(number_seperators, c) &&
865 cursor.pos() < cursorPar()->size() &&
866 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
867 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
869 number(); // Set current_font.number to OFF
870 } else if (IsDigit(c) &&
871 real_current_font.isVisibleRightToLeft()) {
872 number(); // Set current_font.number to ON
874 if (cursor.pos() > 0) {
875 char const c = cursorPar()->getChar(cursor.pos() - 1);
876 if (contains(number_unary_operators, c) &&
877 (cursor.pos() == 1 ||
878 cursorPar()->isSeparator(cursor.pos() - 2) ||
879 cursorPar()->isNewline(cursor.pos() - 2))
885 } else if (contains(number_seperators, c) &&
889 cursor.pos() - 2).number() == LyXFont::ON) {
899 // First check, if there will be two blanks together or a blank at
900 // the beginning of a paragraph.
901 // I decided to handle blanks like normal characters, the main
902 // difference are the special checks when calculating the row.fill
903 // (blank does not count at the end of a row) and the check here
905 // The bug is triggered when we type in a description environment:
906 // The current_font is not changed when we go from label to main text
907 // and it should (along with realtmpfont) when we type the space.
908 // CHECK There is a bug here! (Asger)
910 // store the current font. This is because of the use of cursor
911 // movements. The moving cursor would refresh the current font
912 LyXFont realtmpfont = real_current_font;
913 LyXFont rawtmpfont = current_font;
915 if (!freeSpacing && IsLineSeparatorChar(c)) {
916 if ((cursor.pos() > 0
917 && cursorPar()->isLineSeparator(cursor.pos() - 1))
919 && cursorPar()->isNewline(cursor.pos() - 1))
920 || (cursor.pos() == 0)) {
921 static bool sent_space_message = false;
922 if (!sent_space_message) {
923 if (cursor.pos() == 0)
924 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
926 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
927 sent_space_message = true;
934 // Here case LyXText::InsertInset already inserted the character
935 if (c != Paragraph::META_INSET)
936 cursorPar()->insertChar(cursor.pos(), c);
938 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
940 current_font = rawtmpfont;
941 real_current_font = realtmpfont;
942 redoParagraph(cursorPar());
943 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
949 void LyXText::charInserted()
951 // Here we call finishUndo for every 20 characters inserted.
952 // This is from my experience how emacs does it. (Lgb)
953 static unsigned int counter;
963 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
965 double w = row.fill();
966 double fill_hfill = 0;
967 double fill_label_hfill = 0;
968 double fill_separator = 0;
972 pit->isRightToLeftPar(bv()->buffer()->params());
974 x = rightMargin(*pit);
976 x = leftMargin(pit, row.pos());
978 // is there a manual margin with a manual label
979 LyXLayout_ptr const & layout = pit->layout();
981 if (layout->margintype == MARGIN_MANUAL
982 && layout->labeltype == LABEL_MANUAL) {
983 /// We might have real hfills in the label part
984 int nlh = numberOfLabelHfills(*pit, row);
986 // A manual label par (e.g. List) has an auto-hfill
987 // between the label text and the body of the
989 // But we don't want to do this auto hfill if the par
994 if (nlh && !pit->getLabelWidthString().empty())
995 fill_label_hfill = labelFill(pit, row) / double(nlh);
998 // are there any hfills in the row?
999 int const nh = numberOfHfills(*pit, row);
1003 fill_hfill = w / nh;
1004 // we don't have to look at the alignment if it is ALIGN_LEFT and
1005 // if the row is already larger then the permitted width as then
1006 // we force the LEFT_ALIGN'edness!
1007 } else if (int(row.width()) < textWidth()) {
1008 // is it block, flushleft or flushright?
1009 // set x how you need it
1011 if (pit->params().align() == LYX_ALIGN_LAYOUT)
1012 align = layout->align;
1014 align = pit->params().align();
1016 // Display-style insets should always be on a centred row
1017 // The test on pit->size() is to catch zero-size pars, which
1018 // would trigger the assert in Paragraph::getInset().
1019 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1021 && pit->isInset(row.pos())
1022 && pit->getInset(row.pos())->display())
1024 align = LYX_ALIGN_CENTER;
1028 case LYX_ALIGN_BLOCK: {
1029 int const ns = numberOfSeparators(*pit, row);
1030 bool disp_inset = false;
1031 if (row.endpos() < pit->size()) {
1032 InsetOld * in = pit->getInset(row.endpos());
1034 disp_inset = in->display();
1036 // If we have separators, this is not the last row of a
1037 // par, does not end in newline, and is not row above a
1038 // display inset... then stretch it
1040 && row.endpos() < pit->size()
1041 && !pit->isNewline(row.endpos() - 1)
1044 fill_separator = w / ns;
1045 } else if (is_rtl) {
1050 case LYX_ALIGN_RIGHT:
1053 case LYX_ALIGN_CENTER:
1059 bidi.computeTables(*pit, *bv()->buffer(), row);
1061 pos_type body_pos = pit->beginOfBody();
1062 pos_type end = row.endpos();
1065 && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1067 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1068 if (body_pos <= end)
1069 x += fill_label_hfill;
1073 row.fill_hfill(fill_hfill);
1074 row.fill_label_hfill(fill_label_hfill);
1075 row.fill_separator(fill_separator);
1080 // the cursor set functions have a special mechanism. When they
1081 // realize, that you left an empty paragraph, they will delete it.
1083 void LyXText::cursorRightOneWord()
1085 cursorRightOneWord(cursor);
1086 setCursor(cursorPar(), cursor.pos());
1090 // Skip initial whitespace at end of word and move cursor to *start*
1091 // of prior word, not to end of next prior word.
1092 void LyXText::cursorLeftOneWord()
1094 LyXCursor tmpcursor = cursor;
1095 cursorLeftOneWord(tmpcursor);
1096 setCursor(getPar(tmpcursor), tmpcursor.pos());
1100 void LyXText::selectWord(word_location loc)
1102 LyXCursor from = cursor;
1103 LyXCursor to = cursor;
1104 getWord(from, to, loc);
1106 setCursor(from.par(), from.pos());
1109 selection.cursor = cursor;
1110 setCursor(to.par(), to.pos());
1115 // Select the word currently under the cursor when no
1116 // selection is currently set
1117 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1119 if (!selection.set()) {
1121 return selection.set();
1127 void LyXText::acceptChange()
1129 if (!selection.set() && cursorPar()->size())
1132 if (selection.start.par() == selection.end.par()) {
1133 LyXCursor & startc = selection.start;
1134 LyXCursor & endc = selection.end;
1135 recordUndo(Undo::INSERT, this, startc.par());
1136 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1139 redoParagraph(getPar(startc));
1140 setCursorIntern(startc.par(), 0);
1142 #warning handle multi par selection
1146 void LyXText::rejectChange()
1148 if (!selection.set() && cursorPar()->size())
1151 if (selection.start.par() == selection.end.par()) {
1152 LyXCursor & startc = selection.start;
1153 LyXCursor & endc = selection.end;
1154 recordUndo(Undo::INSERT, this, startc.par());
1155 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1158 redoParagraph(getPar(startc));
1159 setCursorIntern(startc.par(), 0);
1161 #warning handle multi par selection
1165 // Delete from cursor up to the end of the current or next word.
1166 void LyXText::deleteWordForward()
1168 if (cursorPar()->empty())
1171 LyXCursor tmpcursor = cursor;
1172 selection.set(true); // to avoid deletion
1173 cursorRightOneWord();
1174 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1175 selection.cursor = cursor;
1178 cutSelection(true, false);
1183 // Delete from cursor to start of current or prior word.
1184 void LyXText::deleteWordBackward()
1186 if (cursorPar()->empty())
1189 LyXCursor tmpcursor = cursor;
1190 selection.set(true); // to avoid deletion
1191 cursorLeftOneWord();
1192 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1193 selection.cursor = cursor;
1196 cutSelection(true, false);
1201 // Kill to end of line.
1202 void LyXText::deleteLineForward()
1204 if (cursorPar()->empty()) {
1205 // Paragraph is empty, so we just go to the right
1208 LyXCursor tmpcursor = cursor;
1209 // We can't store the row over a regular setCursor
1210 // so we set it to 0 and reset it afterwards.
1211 selection.set(true); // to avoid deletion
1213 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1214 selection.cursor = cursor;
1217 // What is this test for ??? (JMarc)
1218 if (!selection.set())
1219 deleteWordForward();
1221 cutSelection(true, false);
1226 void LyXText::changeCase(LyXText::TextCase action)
1231 if (selection.set()) {
1232 from = selection.start;
1236 getWord(from, to, lyx::PARTIAL_WORD);
1237 setCursor(to.par(), to.pos() + 1);
1240 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1242 pos_type pos = from.pos();
1243 int par = from.par();
1245 while (par != int(paragraphs().size()) &&
1246 (pos != to.pos() || par != to.par())) {
1247 ParagraphList::iterator pit = getPar(par);
1248 if (pos == pit->size()) {
1253 unsigned char c = pit->getChar(pos);
1254 if (c != Paragraph::META_INSET) {
1256 case text_lowercase:
1259 case text_capitalization:
1261 action = text_lowercase;
1263 case text_uppercase:
1269 pit->setChar(pos, c);
1275 void LyXText::Delete()
1277 // this is a very easy implementation
1278 LyXCursor old_cursor = cursor;
1280 // just move to the right
1283 // if you had success make a backspace
1284 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1285 recordUndo(Undo::DELETE, this, old_cursor.par());
1291 void LyXText::backspace()
1293 // Get the font that is used to calculate the baselineskip
1294 ParagraphList::iterator pit = cursorPar();
1295 pos_type lastpos = pit->size();
1297 if (cursor.pos() == 0) {
1298 // The cursor is at the beginning of a paragraph,
1299 // so the the backspace will collapse two paragraphs into one.
1301 // but it's not allowed unless it's new
1302 if (pit->isChangeEdited(0, pit->size()))
1305 // we may paste some paragraphs
1307 // is it an empty paragraph?
1309 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1310 // This is an empty paragraph and we delete it just
1311 // by moving the cursor one step
1312 // left and let the DeleteEmptyParagraphMechanism
1313 // handle the actual deletion of the paragraph.
1317 // the layout things can change the height of a row !
1323 if (cursor.par() != 0)
1324 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1326 ParagraphList::iterator tmppit = cursorPar();
1327 // We used to do cursorLeftIntern() here, but it is
1328 // not a good idea since it triggers the auto-delete
1329 // mechanism. So we do a cursorLeftIntern()-lite,
1330 // without the dreaded mechanism. (JMarc)
1331 if (cursor.par() != 0) {
1332 // steps into the above paragraph.
1333 setCursorIntern(cursor.par() - 1,
1334 getPar(cursor.par() - 1)->size(),
1338 // Pasting is not allowed, if the paragraphs have different
1339 // layout. I think it is a real bug of all other
1340 // word processors to allow it. It confuses the user.
1341 // Correction: Pasting is always allowed with standard-layout
1342 Buffer & buf = *bv()->buffer();
1343 BufferParams const & bufparams = buf.params();
1344 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1345 ParagraphList::iterator const cpit = cursorPar();
1348 && (cpit->layout() == tmppit->layout()
1349 || tmppit->layout() == tclass.defaultLayout())
1350 && cpit->getAlign() == tmppit->getAlign()) {
1351 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1353 if (cursor.pos() && cpit->isSeparator(cursor.pos() - 1))
1354 cursor.pos(cursor.pos() - 1);
1356 // the counters may have changed
1358 setCursor(cursor.par(), cursor.pos(), false);
1361 // this is the code for a normal backspace, not pasting
1363 recordUndo(Undo::DELETE, this, cursor.par());
1364 // We used to do cursorLeftIntern() here, but it is
1365 // not a good idea since it triggers the auto-delete
1366 // mechanism. So we do a cursorLeftIntern()-lite,
1367 // without the dreaded mechanism. (JMarc)
1368 setCursorIntern(cursor.par(), cursor.pos() - 1,
1369 false, cursor.boundary());
1370 cursorPar()->erase(cursor.pos());
1373 lastpos = cursorPar()->size();
1374 if (cursor.pos() == lastpos)
1378 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
1382 ParagraphList::iterator LyXText::cursorPar() const
1384 return getPar(cursor.par());
1388 RowList::iterator LyXText::cursorRow() const
1390 return cursorPar()->getRow(cursor.pos());
1394 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1396 return getPar(cur.par());
1400 ParagraphList::iterator LyXText::getPar(int par) const
1402 BOOST_ASSERT(par >= 0);
1403 BOOST_ASSERT(par < int(paragraphs().size()));
1404 ParagraphList::iterator pit = paragraphs().begin();
1405 std::advance(pit, par);
1411 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1413 //lyxerr << "getRowNearY: y " << y << endl;
1415 ParagraphList::iterator const
1416 pend = boost::prior(paragraphs().end());
1417 pit = paragraphs().begin();
1418 while (int(pit->y + pit->height) < y && pit != pend)
1421 RowList::iterator rit = pit->rows.end();
1422 RowList::iterator const rbegin = pit->rows.begin();
1425 } while (rit != rbegin && int(pit->y + rit->y_offset()) > y);
1429 pit = boost::prior(paragraphs().end());
1431 RowList::iterator rit = lastRow();
1432 RowList::iterator rbegin = firstRow();
1434 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1435 previousRow(pit, rit);
1442 int LyXText::getDepth() const
1444 return cursorPar()->getDepth();
1448 RowList::iterator LyXText::firstRow() const
1450 return paragraphs().front().rows.begin();
1454 ParagraphList::iterator LyXText::firstPar() const
1456 return paragraphs().begin();
1460 RowList::iterator LyXText::lastRow() const
1462 return boost::prior(endRow());
1466 ParagraphList::iterator LyXText::lastPar() const
1468 return boost::prior(endPar());
1472 RowList::iterator LyXText::endRow() const
1474 return paragraphs().back().rows.end();
1478 ParagraphList::iterator LyXText::endPar() const
1480 return paragraphs().end();
1484 void LyXText::nextRow(ParagraphList::iterator & pit,
1485 RowList::iterator & rit) const
1488 if (rit == pit->rows.end()) {
1490 if (pit == paragraphs().end())
1493 rit = pit->rows.begin();
1498 void LyXText::previousRow(ParagraphList::iterator & pit,
1499 RowList::iterator & rit) const
1501 if (rit != pit->rows.begin())
1504 BOOST_ASSERT(pit != paragraphs().begin());
1506 rit = boost::prior(pit->rows.end());
1511 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1513 if (!selection.set())
1516 // should be const ...
1517 ParagraphList::iterator startpit = getPar(selection.start);
1518 ParagraphList::iterator endpit = getPar(selection.end);
1519 size_t const startpos = selection.start.pos();
1520 size_t const endpos = selection.end.pos();
1522 if (startpit == endpit)
1523 return startpit->asString(buffer, startpos, endpos, label);
1525 // First paragraph in selection
1527 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1529 // The paragraphs in between (if any)
1530 ParagraphList::iterator pit = startpit;
1531 for (++pit; pit != endpit; ++pit)
1532 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1534 // Last paragraph in selection
1535 result += endpit->asString(buffer, 0, endpos, label);
1541 int LyXText::parOffset(ParagraphList::iterator pit) const
1543 return std::distance(paragraphs().begin(), pit);
1547 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1549 // remove rows of paragraph, keep track of height changes
1550 height -= pit->height;
1558 InsetList::iterator ii = pit->insetlist.begin();
1559 InsetList::iterator iend = pit->insetlist.end();
1560 for (; ii != iend; ++ii) {
1562 int const w = textWidth() - leftMargin(pit) - rightMargin(*pit);
1563 MetricsInfo mi(bv(), getFont(pit, ii->pos), w);
1564 ii->inset->metrics(mi, dim);
1567 // rebreak the paragraph
1568 pit->setBeginOfBody();
1572 rowBreakPoint(pit, row);
1574 fill(pit, row, textwidth_);
1575 prepareToPrint(pit, row);
1576 setHeightOfRow(pit, row);
1577 row.y_offset(pit->height);
1578 pit->rows.push_back(row);
1579 pit->width = std::max(pit->width, row.width());
1580 pit->height += row.height();
1581 } while (z < pit->size());
1583 height += pit->height;
1584 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1588 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1589 ParagraphList::iterator end)
1591 for ( ; pit != end; ++pit)
1592 redoParagraphInternal(pit);
1593 updateParPositions();
1597 void LyXText::redoParagraph(ParagraphList::iterator pit)
1599 redoParagraphInternal(pit);
1600 updateParPositions();
1604 void LyXText::fullRebreak()
1606 redoParagraphs(paragraphs().begin(), paragraphs().end());
1608 selection.cursor = cursor;
1612 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1614 //BOOST_ASSERT(mi.base.textwidth);
1615 if (mi.base.textwidth)
1616 textwidth_ = mi.base.textwidth;
1617 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1618 // << " textWidth: " << textWidth() << "\nfont: " << mi.base.font << endl;
1620 // Rebuild row cache. This recomputes height as well.
1621 redoParagraphs(paragraphs().begin(), paragraphs().end());
1623 width = maxParagraphWidth(paragraphs());
1626 dim.asc = firstRow()->ascent_of_text();
1627 dim.des = height - dim.asc;
1632 // only used for inset right now. should also be used for main text
1633 void LyXText::draw(PainterInfo &, int x, int y) const
1637 paintTextInset(*bv(), *this, x, y);
1641 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1643 return row.endpos() >= pit->size()
1644 && boost::next(pit) == paragraphs().end();
1648 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1650 return row.pos() == 0 && pit == paragraphs().begin();
1654 void LyXText::cursorLeftOneWord(LyXCursor & cursor)
1656 // treat HFills, floats and Insets as words
1658 ParagraphList::iterator pit = cursorPar();
1659 size_t pos = cursor.pos();
1662 (pit->isSeparator(pos - 1) ||
1663 pit->isKomma(pos - 1) ||
1664 pit->isNewline(pos - 1)) &&
1665 !(pit->isHfill(pos - 1) ||
1666 pit->isInset(pos - 1)))
1670 (pit->isInset(pos - 1) ||
1671 pit->isHfill(pos - 1))) {
1674 if (pit != paragraphs().begin()) {
1678 } else { // Here, cur != 0
1679 while (pos > 0 && pit->isWord(pos - 1))
1683 cursor.par(parOffset(pit));
1688 void LyXText::cursorRightOneWord(LyXCursor & cursor)
1690 // treat floats, HFills and Insets as words
1691 ParagraphList::iterator pit = cursorPar();
1692 pos_type pos = cursor.pos();
1694 if (pos == pit->size() &&
1695 boost::next(pit) != paragraphs().end()) {
1699 // Skip through initial nonword stuff.
1700 while (pos < pit->size() && !pit->isWord(pos)) {
1703 // Advance through word.
1704 while (pos < pit->size() && pit->isWord(pos)) {
1709 cursor.par(parOffset(pit));
1714 void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location const loc)
1716 ParagraphList::iterator from_par = getPar(from);
1717 ParagraphList::iterator to_par = getPar(to);
1719 case lyx::WHOLE_WORD_STRICT:
1720 if (from.pos() == 0 || from.pos() == from_par->size()
1721 || from_par->isSeparator(from.pos())
1722 || from_par->isKomma(from.pos())
1723 || from_par->isNewline(from.pos())
1724 || from_par->isSeparator(from.pos() - 1)
1725 || from_par->isKomma(from.pos() - 1)
1726 || from_par->isNewline(from.pos() - 1)) {
1730 // no break here, we go to the next
1732 case lyx::WHOLE_WORD:
1733 // Move cursor to the beginning, when not already there.
1734 if (from.pos() && !from_par->isSeparator(from.pos() - 1)
1735 && !(from_par->isKomma(from.pos() - 1)
1736 || from_par->isNewline(from.pos() - 1)))
1737 cursorLeftOneWord(from);
1739 case lyx::PREVIOUS_WORD:
1740 // always move the cursor to the beginning of previous word
1741 cursorLeftOneWord(from);
1743 case lyx::NEXT_WORD:
1744 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1747 case lyx::PARTIAL_WORD:
1751 while (to.pos() < to_par->size()
1752 && !to_par->isSeparator(to.pos())
1753 && !to_par->isKomma(to.pos())
1754 && !to_par->isNewline(to.pos())
1755 && !to_par->isHfill(to.pos())
1756 && !to_par->isInset(to.pos()))
1758 to.pos(to.pos() + 1);
1763 void LyXText::write(Buffer const & buf, std::ostream & os) const
1765 ParagraphList::const_iterator pit = paragraphs().begin();
1766 ParagraphList::const_iterator end = paragraphs().end();
1767 Paragraph::depth_type dth = 0;
1768 for (; pit != end; ++pit)
1769 pit->write(buf, os, buf.params(), dth);
1773 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1775 static Change current_change;
1777 bool the_end_read = false;
1778 ParagraphList::iterator pit = paragraphs().begin();
1779 Paragraph::depth_type depth = 0;
1781 while (lex.isOK()) {
1783 string token = lex.getString();
1790 if (token == "\\end_inset") {
1791 the_end_read = true;
1795 if (token == "\\end_document") {
1796 lex.printError("\\end_document read in inset! Error in document!");
1802 if (token == "\\end_document") {
1803 the_end_read = true;
1812 if (token == "\\begin_layout") {
1813 lex.pushToken(token);
1816 par.params().depth(depth);
1817 if (buf.params().tracking_changes)
1819 LyXFont f(LyXFont::ALL_INHERIT, buf.params().language);
1823 if (pit != paragraphs().end())
1826 pit = paragraphs().insert(pit, par);
1828 // FIXME: goddamn InsetTabular makes us pass a Buffer
1830 ::readParagraph(buf, *pit, lex);
1832 } else if (token == "\\begin_deeper") {
1834 } else if (token == "\\end_deeper") {
1836 lex.printError("\\end_deeper: " "depth is already null");
1845 return the_end_read;
1849 int LyXText::ascent() const
1851 return firstRow()->ascent_of_text();
1855 int LyXText::descent() const
1857 return height - firstRow()->ascent_of_text();