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 /// some space for drawing the 'nested' markers (in pixel)
65 extern int const NEST_MARGIN = 20;
66 /// margin for changebar
67 extern int const CHANGEBAR_MARGIN = 10;
72 unsigned int maxParagraphWidth(ParagraphList const & plist)
74 unsigned int width = 0;
75 ParagraphList::const_iterator pit = plist.begin();
76 ParagraphList::const_iterator end = plist.end();
77 for (; pit != end; ++pit)
78 width = std::max(width, pit->width);
85 BufferView * LyXText::bv()
87 BOOST_ASSERT(bv_owner != 0);
92 double LyXText::spacing(Paragraph const & par) const
94 if (par.params().spacing().isDefault())
95 return bv()->buffer()->params().spacing().getValue();
96 return par.params().spacing().getValue();
100 BufferView * LyXText::bv() const
102 BOOST_ASSERT(bv_owner != 0);
107 void LyXText::updateParPositions()
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::textWidth() const
124 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
126 if (pos >= pit->size())
129 char const c = pit->getChar(pos);
130 return singleWidth(pit, pos, c, getFont(pit, pos));
134 int LyXText::singleWidth(ParagraphList::iterator pit,
135 pos_type pos, char c, LyXFont const & font) const
137 if (pos >= pit->size()) {
138 lyxerr << "in singleWidth(), pos: " << pos << endl;
143 // The most common case is handled first (Asger)
144 if (IsPrintable(c)) {
145 if (!font.language()->RightToLeft()) {
146 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
147 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
148 && font.language()->lang() == "arabic") {
149 if (Encodings::IsComposeChar_arabic(c))
152 c = pit->transformChar(c, pos);
153 } else if (font.language()->lang() == "hebrew" &&
154 Encodings::IsComposeChar_hebrew(c))
157 return font_metrics::width(c, font);
160 if (c == Paragraph::META_INSET)
161 return pit->getInset(pos)->width();
163 if (IsSeparatorChar(c))
165 return font_metrics::width(c, font);
169 int LyXText::leftMargin(ParagraphList::iterator pit) const
171 return leftMargin(pit, pit->size());
175 int LyXText::leftMargin(ParagraphList::iterator pit, pos_type pos) const
177 LyXTextClass const & tclass =
178 bv()->buffer()->params().getLyXTextClass();
179 LyXLayout_ptr const & layout = pit->layout();
181 string parindent = layout->parindent;
183 int x = NEST_MARGIN + CHANGEBAR_MARGIN;
185 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
187 // This is the way LyX handles LaTeX-Environments.
188 // I have had this idea very late, so it seems to be a
189 // later added hack and this is true
190 if (pit->getDepth() == 0) {
191 if (pit->layout() == tclass.defaultLayout()) {
192 // find the previous same level paragraph
193 if (pit != ownerParagraphs().begin()) {
194 ParagraphList::iterator newpit =
195 depthHook(pit, ownerParagraphs(), pit->getDepth());
196 if (newpit == pit && newpit->layout()->nextnoindent)
201 // find the next level paragraph
202 ParagraphList::iterator newpar =
203 outerHook(pit, ownerParagraphs());
205 // Make a corresponding row. Need to call leftMargin()
206 // to check whether it is a sufficent paragraph.
207 if (newpar != ownerParagraphs().end()
208 && newpar->layout()->isEnvironment()) {
209 x = leftMargin(newpar);
212 if (newpar != ownerParagraphs().end()
213 && pit->layout() == tclass.defaultLayout()) {
214 if (newpar->params().noindent())
217 parindent = newpar->layout()->parindent;
221 LyXFont const labelfont = getLabelFont(pit);
222 switch (layout->margintype) {
224 if (!layout->leftmargin.empty())
225 x += font_metrics::signedWidth(layout->leftmargin,
226 tclass.defaultfont());
227 if (!pit->getLabelstring().empty()) {
228 x += font_metrics::signedWidth(layout->labelindent,
230 x += font_metrics::width(pit->getLabelstring(),
232 x += font_metrics::width(layout->labelsep, labelfont);
237 x += font_metrics::signedWidth(layout->labelindent, labelfont);
238 // The width of an empty par, even with manual label, should be 0
239 if (!pit->empty() && pos >= pit->beginOfBody()) {
240 if (!pit->getLabelWidthString().empty()) {
241 x += font_metrics::width(pit->getLabelWidthString(),
243 x += font_metrics::width(layout->labelsep, labelfont);
249 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
250 / (pit->getDepth() + 4);
253 case MARGIN_FIRST_DYNAMIC:
254 if (layout->labeltype == LABEL_MANUAL) {
255 if (pos >= pit->beginOfBody()) {
256 x += font_metrics::signedWidth(layout->leftmargin,
259 x += font_metrics::signedWidth(layout->labelindent,
263 // Special case to fix problems with
265 || (layout->labeltype == LABEL_STATIC
266 && layout->latextype == LATEX_ENVIRONMENT
267 && !isFirstInSequence(pit, ownerParagraphs()))) {
268 x += font_metrics::signedWidth(layout->leftmargin,
270 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
271 && layout->labeltype != LABEL_BIBLIO
272 && layout->labeltype !=
273 LABEL_CENTERED_TOP_ENVIRONMENT) {
274 x += font_metrics::signedWidth(layout->labelindent,
276 x += font_metrics::width(layout->labelsep, labelfont);
277 x += font_metrics::width(pit->getLabelstring(),
282 case MARGIN_RIGHT_ADDRESS_BOX: {
283 // ok, a terrible hack. The left margin depends on the widest
284 // row in this paragraph.
285 RowList::iterator rit = pit->rows.begin();
286 RowList::iterator end = pit->rows.end();
287 #warning This is wrong.
288 int minfill = textwidth_;
289 for ( ; rit != end; ++rit)
290 if (rit->fill() < minfill)
291 minfill = rit->fill();
292 x += font_metrics::signedWidth(layout->leftmargin,
293 tclass.defaultfont());
299 if (!pit->params().leftIndent().zero()) {
300 int const tw = inset_owner ?
301 inset_owner->latexTextWidth(bv()) : textWidth();
302 x += pit->params().leftIndent().inPixels(tw);
307 if (pit->params().align() == LYX_ALIGN_LAYOUT)
308 align = layout->align;
310 align = pit->params().align();
312 // set the correct parindent
314 if ((layout->labeltype == LABEL_NO_LABEL
315 || layout->labeltype == LABEL_TOP_ENVIRONMENT
316 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
317 || (layout->labeltype == LABEL_STATIC
318 && layout->latextype == LATEX_ENVIRONMENT
319 && !isFirstInSequence(pit, ownerParagraphs())))
320 && align == LYX_ALIGN_BLOCK
321 && !pit->params().noindent()
322 // in tabulars and ert paragraphs are never indented!
323 && (!pit->inInset() || !pit->inInset()->owner() ||
324 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
325 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
326 && (pit->layout() != tclass.defaultLayout() ||
327 bv()->buffer()->params().paragraph_separation ==
328 BufferParams::PARSEP_INDENT)) {
329 x += font_metrics::signedWidth(parindent,
330 tclass.defaultfont());
338 int LyXText::rightMargin(Paragraph const & par) const
340 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
343 + font_metrics::signedWidth(tclass.rightmargin(),
344 tclass.defaultfont())
345 + font_metrics::signedWidth(par.layout()->rightmargin,
346 tclass.defaultfont())
347 * 4 / (par.getDepth() + 4);
351 int LyXText::labelEnd(ParagraphList::iterator pit) const
353 // labelEnd is only needed if the layout fills a flushleft label.
354 if (pit->layout()->margintype != MARGIN_MANUAL)
356 // return the beginning of the body
357 return leftMargin(pit);
363 // this needs special handling - only newlines count as a break point
364 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
366 pos_type const end = par.size();
369 if (par.isNewline(i))
378 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
380 pos_type const end = pit->size();
381 pos_type const pos = row.pos();
387 // maximum pixel width of a row
388 int width = textWidth() - rightMargin(*pit); // - leftMargin(pit, row);
394 LyXLayout_ptr const & layout = pit->layout();
396 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
397 row.endpos(addressBreakPoint(pos, *pit));
401 pos_type const body_pos = pit->beginOfBody();
404 // Now we iterate through until we reach the right margin
405 // or the end of the par, then choose the possible break
408 int const left = leftMargin(pit, pos);
411 // pixel width since last breakpoint
415 // We re-use the font resolution for the entire font span when possible
416 LyXFont font = getFont(pit, pos);
417 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(pos);
419 pos_type point = end;
421 for ( ; i < end; ++i) {
422 if (pit->isNewline(i)) {
428 if (pit->isInset(i + 1) && pit->getInset(i + 1)->display()) {
433 if (pit->isInset(i) && pit->getInset(i)->display()) {
439 char const c = pit->getChar(i);
441 if (i > endPosOfFontSpan) {
442 font = getFont(pit, i);
443 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
447 int thiswidth = singleWidth(pit, i, c, font);
449 // add the auto-hfill from label end to the body
450 if (body_pos && i == body_pos) {
451 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
452 if (pit->isLineSeparator(i - 1))
453 add -= singleWidth(pit, i - 1);
455 add = std::max(add, labelEnd(pit) - x);
460 //lyxerr << "i: " << i << " x: "
461 //<< x << " width: " << width << endl;
462 chunkwidth += thiswidth;
465 // break before a character that will fall off
466 // the right of the row
468 // if no break before, break here
469 if (point == end || chunkwidth >= width - left) {
475 // exit on last registered breakpoint:
479 if (!pit->isInset(i) || pit->getInset(i)->isChar()) {
480 // some insets are line separators too
481 if (pit->isLineSeparator(i)) {
482 // register breakpoint:
489 if (i == end && x < width) {
490 // maybe found one, but the par is short enough.
494 // manual labels cannot be broken in LaTeX. But we
495 // want to make our on-screen rendering of footnotes
497 if (body_pos && point < body_pos)
504 // returns the minimum space a row needs on the screen in pixel
505 void LyXText::fill(ParagraphList::iterator pit, Row & row, int workwidth) const
507 // get the pure distance
508 pos_type const end = row.endpos();
510 string labelsep = pit->layout()->labelsep;
511 int w = leftMargin(pit, row.pos());
513 pos_type const body_pos = pit->beginOfBody();
514 pos_type i = row.pos();
517 // We re-use the font resolution for the entire span when possible
518 LyXFont font = getFont(pit, i);
519 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
520 for ( ; i < end; ++i) {
521 if (body_pos > 0 && i == body_pos) {
522 w += font_metrics::width(labelsep, getLabelFont(pit));
523 if (pit->isLineSeparator(i - 1))
524 w -= singleWidth(pit, i - 1);
525 w = max(w, labelEnd(pit));
527 char const c = pit->getChar(i);
528 if (IsPrintable(c) && i > endPosOfFontSpan) {
529 // We need to get the next font
530 font = getFont(pit, i);
531 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
533 w += singleWidth(pit, i, c, font);
537 if (body_pos > 0 && body_pos >= end) {
538 w += font_metrics::width(labelsep, getLabelFont(pit));
539 if (end > 0 && pit->isLineSeparator(end - 1))
540 w -= singleWidth(pit, end - 1);
541 w = max(w, labelEnd(pit));
544 int const fill = workwidth - w - rightMargin(*pit);
546 row.width(workwidth - fill);
550 // returns the minimum space a manual label needs on the screen in pixel
551 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
553 pos_type last = pit->beginOfBody();
555 BOOST_ASSERT(last > 0);
557 // -1 because a label ends with a space that is in the label
560 // a separator at this end does not count
561 if (pit->isLineSeparator(last))
565 for (pos_type i = row.pos(); i <= last; ++i)
566 w += singleWidth(pit, i);
568 string const & label = pit->params().labelWidthString();
572 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
576 LColor_color LyXText::backgroundColor() const
579 return inset_owner->backgroundColor();
580 return LColor::background;
584 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
586 // get the maximum ascent and the maximum descent
587 double layoutasc = 0;
588 double layoutdesc = 0;
589 double const dh = defaultRowHeight();
591 // ok, let us initialize the maxasc and maxdesc value.
592 // Only the fontsize count. The other properties
593 // are taken from the layoutfont. Nicer on the screen :)
594 LyXLayout_ptr const & layout = pit->layout();
596 // as max get the first character of this row then it can increase but not
597 // decrease the height. Just some point to start with so we don't have to
598 // do the assignment below too often.
599 LyXFont font = getFont(pit, row.pos());
600 LyXFont::FONT_SIZE const tmpsize = font.size();
601 font = getLayoutFont(pit);
602 LyXFont::FONT_SIZE const size = font.size();
603 font.setSize(tmpsize);
605 LyXFont labelfont = getLabelFont(pit);
607 // these are minimum values
608 double const spacing_val = layout->spacing.getValue() * spacing(*pit);
609 //lyxerr << "spacing_val = " << spacing_val << endl;
610 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
611 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
613 // insets may be taller
614 InsetList::iterator ii = pit->insetlist.begin();
615 InsetList::iterator iend = pit->insetlist.end();
616 for ( ; ii != iend; ++ii) {
617 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
618 maxasc = max(maxasc, ii->inset->ascent());
619 maxdesc = max(maxdesc, ii->inset->descent());
623 // Check if any custom fonts are larger (Asger)
624 // This is not completely correct, but we can live with the small,
625 // cosmetic error for now.
627 pos_type const pos_end = row.endpos();
629 LyXFont::FONT_SIZE maxsize =
630 pit->highestFontInRange(row.pos(), pos_end, size);
631 if (maxsize > font.size()) {
632 font.setSize(maxsize);
633 maxasc = max(maxasc, font_metrics::maxAscent(font));
634 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
637 // This is nicer with box insets:
641 row.ascent_of_text(maxasc);
644 if (row.pos() == 0) {
645 BufferParams const & bufparams = bv()->buffer()->params();
646 // some parksips VERY EASY IMPLEMENTATION
647 if (bv()->buffer()->params().paragraph_separation
648 == BufferParams::PARSEP_SKIP
649 && pit != ownerParagraphs().begin()
650 && ((layout->isParagraph() && pit->getDepth() == 0)
651 || (boost::prior(pit)->layout()->isParagraph()
652 && boost::prior(pit)->getDepth() == 0)))
654 maxasc += bufparams.getDefSkip().inPixels(*bv());
657 // add user added vertical space
658 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
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, ownerParagraphs())
675 && !pit->getLabelstring().empty())
678 font_metrics::maxHeight(labelfont)
679 * layout->spacing.getValue()
681 + (layout->topsep + layout->labelbottomsep) * dh);
684 // And now 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, ownerParagraphs(), 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 != ownerParagraphs().begin() || row.pos() != 0) {
697 if (layout->topsep > 0)
698 layoutasc = layout->topsep * dh;
701 prev = outerHook(pit, ownerParagraphs());
702 if (prev != ownerParagraphs().end()) {
703 maxasc += int(prev->layout()->parsep * dh);
704 } else if (pit != ownerParagraphs().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 vertical spaces, that the user added
716 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
718 // and now the layout spaces, for example before and after
719 // a section, or between the items of a itemize or enumerate
721 ParagraphList::iterator nextpit = boost::next(pit);
722 if (nextpit != ownerParagraphs().end()) {
723 ParagraphList::iterator cpit = pit;
727 if (cpit->getDepth() > nextpit->getDepth()) {
728 usual = cpit->layout()->bottomsep * dh;
729 cpit = depthHook(cpit, ownerParagraphs(), nextpit->getDepth());
730 if (cpit->layout() != nextpit->layout()
731 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
733 unusual = cpit->layout()->bottomsep * dh;
735 layoutdesc = max(unusual, usual);
736 } else if (cpit->getDepth() == nextpit->getDepth()) {
737 if (cpit->layout() != nextpit->layout()
738 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
739 layoutdesc = int(cpit->layout()->bottomsep * dh);
744 // incalculate the layout spaces
745 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
746 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
748 row.height(maxasc + maxdesc + labeladdon);
749 row.baseline(maxasc + labeladdon);
750 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
754 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
756 // allow only if at start or end, or all previous is new text
757 ParagraphList::iterator cpit = cursorPar();
758 if (cursor.pos() && cursor.pos() != cpit->size()
759 && cpit->isChangeEdited(0, cursor.pos()))
762 LyXTextClass const & tclass =
763 bv()->buffer()->params().getLyXTextClass();
764 LyXLayout_ptr const & layout = cpit->layout();
766 // this is only allowed, if the current paragraph is not empty or caption
767 // and if it has not the keepempty flag active
768 if (cpit->empty() && !cpit->allowEmpty()
769 && layout->labeltype != LABEL_SENSITIVE)
772 recUndo(cursor.par());
774 // Always break behind a space
776 // It is better to erase the space (Dekel)
777 if (cursor.pos() < cpit->size() && cpit->isLineSeparator(cursor.pos()))
778 cpit->erase(cursor.pos());
780 // break the paragraph
784 keep_layout = layout->isEnvironment();
786 // we need to set this before we insert the paragraph. IMO the
787 // breakParagraph call should return a bool if it inserts the
788 // paragraph before or behind and we should react on that one
789 // but we can fix this in 1.3.0 (Jug 20020509)
790 bool const isempty = cpit->allowEmpty() && cpit->empty();
791 ::breakParagraph(bv()->buffer()->params(), paragraphs, cpit,
792 cursor.pos(), keep_layout);
794 #warning Trouble Point! (Lgb)
795 // When ::breakParagraph is called from within an inset we must
796 // ensure that the correct ParagraphList is used. Today that is not
797 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
799 ParagraphList::iterator next_par = boost::next(cpit);
801 // well this is the caption hack since one caption is really enough
802 if (layout->labeltype == LABEL_SENSITIVE) {
804 // set to standard-layout
805 cpit->applyLayout(tclass.defaultLayout());
807 // set to standard-layout
808 next_par->applyLayout(tclass.defaultLayout());
811 // if the cursor is at the beginning of a row without prior newline,
813 // This touches only the screen-update. Otherwise we would may have
814 // an empty row on the screen
815 RowList::iterator crit = cpit->getRow(cursor.pos());
816 if (cursor.pos() && crit->pos() == cursor.pos()
817 && !cpit->isNewline(cursor.pos() - 1))
822 while (!next_par->empty() && next_par->isNewline(0))
827 redoParagraph(next_par);
829 // This check is necessary. Otherwise the new empty paragraph will
830 // be deleted automatically. And it is more friendly for the user!
831 if (cursor.pos() || isempty)
832 setCursor(next_par, 0);
838 // convenience function
839 void LyXText::redoParagraph()
842 redoParagraph(cursorPar());
843 setCursorIntern(cursor.par(), cursor.pos());
847 // insert a character, moves all the following breaks in the
848 // same Paragraph one to the right and make a rebreak
849 void LyXText::insertChar(char c)
851 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
853 // When the free-spacing option is set for the current layout,
854 // disable the double-space checking
856 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
857 cursorPar()->isFreeSpacing();
859 if (lyxrc.auto_number) {
860 static string const number_operators = "+-/*";
861 static string const number_unary_operators = "+-";
862 static string const number_seperators = ".,:";
864 if (current_font.number() == LyXFont::ON) {
865 if (!IsDigit(c) && !contains(number_operators, c) &&
866 !(contains(number_seperators, c) &&
868 cursor.pos() < cursorPar()->size() &&
869 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
870 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
872 number(); // Set current_font.number to OFF
873 } else if (IsDigit(c) &&
874 real_current_font.isVisibleRightToLeft()) {
875 number(); // Set current_font.number to ON
877 if (cursor.pos() > 0) {
878 char const c = cursorPar()->getChar(cursor.pos() - 1);
879 if (contains(number_unary_operators, c) &&
880 (cursor.pos() == 1 ||
881 cursorPar()->isSeparator(cursor.pos() - 2) ||
882 cursorPar()->isNewline(cursor.pos() - 2))
888 } else if (contains(number_seperators, c) &&
892 cursor.pos() - 2).number() == LyXFont::ON) {
902 // First check, if there will be two blanks together or a blank at
903 // the beginning of a paragraph.
904 // I decided to handle blanks like normal characters, the main
905 // difference are the special checks when calculating the row.fill
906 // (blank does not count at the end of a row) and the check here
908 // The bug is triggered when we type in a description environment:
909 // The current_font is not changed when we go from label to main text
910 // and it should (along with realtmpfont) when we type the space.
911 // CHECK There is a bug here! (Asger)
913 // store the current font. This is because of the use of cursor
914 // movements. The moving cursor would refresh the current font
915 LyXFont realtmpfont = real_current_font;
916 LyXFont rawtmpfont = current_font;
918 if (!freeSpacing && IsLineSeparatorChar(c)) {
919 if ((cursor.pos() > 0
920 && cursorPar()->isLineSeparator(cursor.pos() - 1))
922 && cursorPar()->isNewline(cursor.pos() - 1))
923 || (cursor.pos() == 0)) {
924 static bool sent_space_message = false;
925 if (!sent_space_message) {
926 if (cursor.pos() == 0)
927 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
929 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
930 sent_space_message = true;
937 // Here case LyXText::InsertInset already inserted the character
938 if (c != Paragraph::META_INSET)
939 cursorPar()->insertChar(cursor.pos(), c);
941 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
943 current_font = rawtmpfont;
944 real_current_font = realtmpfont;
945 redoParagraph(cursorPar());
946 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
952 void LyXText::charInserted()
954 // Here we call finishUndo for every 20 characters inserted.
955 // This is from my experience how emacs does it. (Lgb)
956 static unsigned int counter;
966 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
968 double w = row.fill();
969 double fill_hfill = 0;
970 double fill_label_hfill = 0;
971 double fill_separator = 0;
975 pit->isRightToLeftPar(bv()->buffer()->params());
977 x = rightMargin(*pit);
979 x = leftMargin(pit, row.pos());
981 // is there a manual margin with a manual label
982 LyXLayout_ptr const & layout = pit->layout();
984 if (layout->margintype == MARGIN_MANUAL
985 && layout->labeltype == LABEL_MANUAL) {
986 /// We might have real hfills in the label part
987 int nlh = numberOfLabelHfills(*pit, row);
989 // A manual label par (e.g. List) has an auto-hfill
990 // between the label text and the body of the
992 // But we don't want to do this auto hfill if the par
997 if (nlh && !pit->getLabelWidthString().empty())
998 fill_label_hfill = labelFill(pit, row) / double(nlh);
1001 // are there any hfills in the row?
1002 int const nh = numberOfHfills(*pit, row);
1006 fill_hfill = w / nh;
1007 // we don't have to look at the alignment if it is ALIGN_LEFT and
1008 // if the row is already larger then the permitted width as then
1009 // we force the LEFT_ALIGN'edness!
1010 } else if (int(row.width()) < textWidth()) {
1011 // is it block, flushleft or flushright?
1012 // set x how you need it
1014 if (pit->params().align() == LYX_ALIGN_LAYOUT)
1015 align = layout->align;
1017 align = pit->params().align();
1019 // Display-style insets should always be on a centred row
1020 // The test on pit->size() is to catch zero-size pars, which
1021 // would trigger the assert in Paragraph::getInset().
1022 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1024 && pit->isInset(row.pos())
1025 && pit->getInset(row.pos())->display())
1027 align = LYX_ALIGN_CENTER;
1031 case LYX_ALIGN_BLOCK: {
1032 int const ns = numberOfSeparators(*pit, row);
1033 bool disp_inset = false;
1034 if (row.endpos() < pit->size()) {
1035 InsetOld * in = pit->getInset(row.endpos());
1037 disp_inset = in->display();
1039 // If we have separators, this is not the last row of a
1040 // par, does not end in newline, and is not row above a
1041 // display inset... then stretch it
1043 && row.endpos() < pit->size()
1044 && !pit->isNewline(row.endpos() - 1)
1047 fill_separator = w / ns;
1048 } else if (is_rtl) {
1053 case LYX_ALIGN_RIGHT:
1056 case LYX_ALIGN_CENTER:
1062 bidi.computeTables(*pit, *bv()->buffer(), row);
1064 pos_type body_pos = pit->beginOfBody();
1065 pos_type end = row.endpos();
1068 && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1070 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1071 if (body_pos <= end)
1072 x += fill_label_hfill;
1076 row.fill_hfill(fill_hfill);
1077 row.fill_label_hfill(fill_label_hfill);
1078 row.fill_separator(fill_separator);
1083 // the cursor set functions have a special mechanism. When they
1084 // realize, that you left an empty paragraph, they will delete it.
1086 void LyXText::cursorRightOneWord()
1088 cursorRightOneWord(cursor);
1089 setCursor(cursorPar(), cursor.pos());
1093 // Skip initial whitespace at end of word and move cursor to *start*
1094 // of prior word, not to end of next prior word.
1095 void LyXText::cursorLeftOneWord()
1097 LyXCursor tmpcursor = cursor;
1098 cursorLeftOneWord(tmpcursor);
1099 setCursor(getPar(tmpcursor), tmpcursor.pos());
1103 void LyXText::selectWord(word_location loc)
1105 LyXCursor from = cursor;
1106 LyXCursor to = cursor;
1107 getWord(from, to, loc);
1109 setCursor(from.par(), from.pos());
1112 selection.cursor = cursor;
1113 setCursor(to.par(), to.pos());
1118 // Select the word currently under the cursor when no
1119 // selection is currently set
1120 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1122 if (!selection.set()) {
1124 return selection.set();
1130 void LyXText::acceptChange()
1132 if (!selection.set() && cursorPar()->size())
1135 if (selection.start.par() == selection.end.par()) {
1136 LyXCursor & startc = selection.start;
1137 LyXCursor & endc = selection.end;
1138 recordUndo(Undo::INSERT, this, startc.par());
1139 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1142 redoParagraph(getPar(startc));
1143 setCursorIntern(startc.par(), 0);
1145 #warning handle multi par selection
1149 void LyXText::rejectChange()
1151 if (!selection.set() && cursorPar()->size())
1154 if (selection.start.par() == selection.end.par()) {
1155 LyXCursor & startc = selection.start;
1156 LyXCursor & endc = selection.end;
1157 recordUndo(Undo::INSERT, this, startc.par());
1158 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1161 redoParagraph(getPar(startc));
1162 setCursorIntern(startc.par(), 0);
1164 #warning handle multi par selection
1168 // Delete from cursor up to the end of the current or next word.
1169 void LyXText::deleteWordForward()
1171 if (cursorPar()->empty())
1174 LyXCursor tmpcursor = cursor;
1175 selection.set(true); // to avoid deletion
1176 cursorRightOneWord();
1177 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1178 selection.cursor = cursor;
1181 cutSelection(true, false);
1186 // Delete from cursor to start of current or prior word.
1187 void LyXText::deleteWordBackward()
1189 if (cursorPar()->empty())
1192 LyXCursor tmpcursor = cursor;
1193 selection.set(true); // to avoid deletion
1194 cursorLeftOneWord();
1195 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1196 selection.cursor = cursor;
1199 cutSelection(true, false);
1204 // Kill to end of line.
1205 void LyXText::deleteLineForward()
1207 if (cursorPar()->empty()) {
1208 // Paragraph is empty, so we just go to the right
1211 LyXCursor tmpcursor = cursor;
1212 // We can't store the row over a regular setCursor
1213 // so we set it to 0 and reset it afterwards.
1214 selection.set(true); // to avoid deletion
1216 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1217 selection.cursor = cursor;
1220 // What is this test for ??? (JMarc)
1221 if (!selection.set())
1222 deleteWordForward();
1224 cutSelection(true, false);
1229 void LyXText::changeCase(LyXText::TextCase action)
1234 if (selection.set()) {
1235 from = selection.start;
1239 getWord(from, to, lyx::PARTIAL_WORD);
1240 setCursor(to.par(), to.pos() + 1);
1243 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1245 pos_type pos = from.pos();
1246 int par = from.par();
1248 while (par != int(ownerParagraphs().size()) &&
1249 (pos != to.pos() || par != to.par())) {
1250 ParagraphList::iterator pit = getPar(par);
1251 if (pos == pit->size()) {
1256 unsigned char c = pit->getChar(pos);
1257 if (c != Paragraph::META_INSET) {
1259 case text_lowercase:
1262 case text_capitalization:
1264 action = text_lowercase;
1266 case text_uppercase:
1272 pit->setChar(pos, c);
1278 void LyXText::Delete()
1280 // this is a very easy implementation
1281 LyXCursor old_cursor = cursor;
1283 // just move to the right
1286 // if you had success make a backspace
1287 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1288 recordUndo(Undo::DELETE, this, old_cursor.par());
1294 void LyXText::backspace()
1296 // Get the font that is used to calculate the baselineskip
1297 ParagraphList::iterator pit = cursorPar();
1298 pos_type lastpos = pit->size();
1300 if (cursor.pos() == 0) {
1301 // The cursor is at the beginning of a paragraph,
1302 // so the the backspace will collapse two paragraphs into one.
1304 // but it's not allowed unless it's new
1305 if (pit->isChangeEdited(0, pit->size()))
1308 // we may paste some paragraphs
1310 // is it an empty paragraph?
1312 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1313 // This is an empty paragraph and we delete it just
1314 // by moving the cursor one step
1315 // left and let the DeleteEmptyParagraphMechanism
1316 // handle the actual deletion of the paragraph.
1319 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1320 if (cursorPar()->layout() == tmppit->layout()
1321 && cursorPar()->getAlign() == tmppit->getAlign()) {
1322 // Inherit bottom DTD from the paragraph below.
1323 // (the one we are deleting)
1324 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1329 // the layout things can change the height of a row !
1335 if (cursor.par() != 0)
1336 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1338 ParagraphList::iterator tmppit = cursorPar();
1339 // We used to do cursorLeftIntern() here, but it is
1340 // not a good idea since it triggers the auto-delete
1341 // mechanism. So we do a cursorLeftIntern()-lite,
1342 // without the dreaded mechanism. (JMarc)
1343 if (cursor.par() != 0) {
1344 // steps into the above paragraph.
1345 setCursorIntern(cursor.par() - 1,
1346 getPar(cursor.par() - 1)->size(),
1350 // Pasting is not allowed, if the paragraphs have different
1351 // layout. I think it is a real bug of all other
1352 // word processors to allow it. It confuses the user.
1353 // Correction: Pasting is always allowed with standard-layout
1354 Buffer & buf = *bv()->buffer();
1355 BufferParams const & bufparams = buf.params();
1356 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1357 ParagraphList::iterator const cpit = cursorPar();
1360 && (cpit->layout() == tmppit->layout()
1361 || tmppit->layout() == tclass.defaultLayout())
1362 && cpit->getAlign() == tmppit->getAlign()) {
1363 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1365 if (cursor.pos() && cpit->isSeparator(cursor.pos() - 1))
1366 cursor.pos(cursor.pos() - 1);
1368 // the counters may have changed
1370 setCursor(cursor.par(), cursor.pos(), false);
1373 // this is the code for a normal backspace, not pasting
1375 recordUndo(Undo::DELETE, this, cursor.par());
1376 // We used to do cursorLeftIntern() here, but it is
1377 // not a good idea since it triggers the auto-delete
1378 // mechanism. So we do a cursorLeftIntern()-lite,
1379 // without the dreaded mechanism. (JMarc)
1380 setCursorIntern(cursor.par(), cursor.pos() - 1,
1381 false, cursor.boundary());
1382 cursorPar()->erase(cursor.pos());
1385 lastpos = cursorPar()->size();
1386 if (cursor.pos() == lastpos)
1390 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
1394 ParagraphList::iterator LyXText::cursorPar() const
1396 return getPar(cursor.par());
1400 RowList::iterator LyXText::cursorRow() const
1402 return cursorPar()->getRow(cursor.pos());
1406 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1408 return getPar(cur.par());
1412 ParagraphList::iterator LyXText::getPar(int par) const
1414 BOOST_ASSERT(par >= 0);
1415 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1416 ParagraphList::iterator pit = ownerParagraphs().begin();
1417 std::advance(pit, par);
1423 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1425 //lyxerr << "getRowNearY: y " << y << endl;
1427 ParagraphList::iterator const
1428 pend = boost::prior(ownerParagraphs().end());
1429 pit = ownerParagraphs().begin();
1430 while (int(pit->y + pit->height) < y && pit != pend)
1433 RowList::iterator rit = pit->rows.end();
1434 RowList::iterator const rbegin = pit->rows.begin();
1437 } while (rit != rbegin && int(pit->y + rit->y_offset()) > y);
1441 pit = boost::prior(ownerParagraphs().end());
1443 RowList::iterator rit = lastRow();
1444 RowList::iterator rbegin = firstRow();
1446 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1447 previousRow(pit, rit);
1454 int LyXText::getDepth() const
1456 return cursorPar()->getDepth();
1460 RowList::iterator LyXText::firstRow() const
1462 return ownerParagraphs().front().rows.begin();
1466 ParagraphList::iterator LyXText::firstPar() const
1468 return ownerParagraphs().begin();
1472 RowList::iterator LyXText::lastRow() const
1474 return boost::prior(endRow());
1478 ParagraphList::iterator LyXText::lastPar() const
1480 return boost::prior(endPar());
1484 RowList::iterator LyXText::endRow() const
1486 return ownerParagraphs().back().rows.end();
1490 ParagraphList::iterator LyXText::endPar() const
1492 return ownerParagraphs().end();
1496 void LyXText::nextRow(ParagraphList::iterator & pit,
1497 RowList::iterator & rit) const
1500 if (rit == pit->rows.end()) {
1502 if (pit == ownerParagraphs().end())
1505 rit = pit->rows.begin();
1510 void LyXText::previousRow(ParagraphList::iterator & pit,
1511 RowList::iterator & rit) const
1513 if (rit != pit->rows.begin())
1516 BOOST_ASSERT(pit != ownerParagraphs().begin());
1518 rit = boost::prior(pit->rows.end());
1523 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1525 if (!selection.set())
1528 // should be const ...
1529 ParagraphList::iterator startpit = getPar(selection.start);
1530 ParagraphList::iterator endpit = getPar(selection.end);
1531 size_t const startpos = selection.start.pos();
1532 size_t const endpos = selection.end.pos();
1534 if (startpit == endpit)
1535 return startpit->asString(buffer, startpos, endpos, label);
1537 // First paragraph in selection
1539 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1541 // The paragraphs in between (if any)
1542 ParagraphList::iterator pit = startpit;
1543 for (++pit; pit != endpit; ++pit)
1544 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1546 // Last paragraph in selection
1547 result += endpit->asString(buffer, 0, endpos, label);
1553 int LyXText::parOffset(ParagraphList::iterator pit) const
1555 return std::distance(ownerParagraphs().begin(), pit);
1559 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1561 // remove rows of paragraph, keep track of height changes
1562 height -= pit->height;
1570 InsetList::iterator ii = pit->insetlist.begin();
1571 InsetList::iterator iend = pit->insetlist.end();
1572 for (; ii != iend; ++ii) {
1574 int const w = textWidth() - leftMargin(pit) - rightMargin(*pit);
1575 MetricsInfo mi(bv(), getFont(pit, ii->pos), w);
1576 ii->inset->metrics(mi, dim);
1579 // rebreak the paragraph
1580 pit->setBeginOfBody();
1584 rowBreakPoint(pit, row);
1586 fill(pit, row, textwidth_);
1587 prepareToPrint(pit, row);
1588 setHeightOfRow(pit, row);
1589 row.y_offset(pit->height);
1590 pit->rows.push_back(row);
1591 pit->width = std::max(pit->width, row.width());
1592 pit->height += row.height();
1593 } while (z < pit->size());
1595 height += pit->height;
1596 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1600 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1601 ParagraphList::iterator end)
1603 for ( ; pit != end; ++pit)
1604 redoParagraphInternal(pit);
1605 updateParPositions();
1609 void LyXText::redoParagraph(ParagraphList::iterator pit)
1611 redoParagraphInternal(pit);
1612 updateParPositions();
1616 void LyXText::fullRebreak()
1618 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1620 selection.cursor = cursor;
1624 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1626 //BOOST_ASSERT(mi.base.textwidth);
1627 if (mi.base.textwidth)
1628 textwidth_ = mi.base.textwidth;
1629 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1630 // << " textWidth: " << textWidth() << "\nfont: " << mi.base.font << endl;
1632 // Rebuild row cache. This recomputes height as well.
1633 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
1635 width = maxParagraphWidth(ownerParagraphs());
1638 dim.asc = firstRow()->ascent_of_text();
1639 dim.des = height - dim.asc;
1640 dim.wid = std::max(mi.base.textwidth, int(width));
1644 // only used for inset right now. should also be used for main text
1645 void LyXText::draw(PainterInfo & pi, int x, int y) const
1649 paintTextInset(*bv(), *this, x, y);
1653 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1655 return row.endpos() >= pit->size()
1656 && boost::next(pit) == ownerParagraphs().end();
1660 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1662 return row.pos() == 0 && pit == ownerParagraphs().begin();
1666 void LyXText::cursorLeftOneWord(LyXCursor & cursor)
1668 // treat HFills, floats and Insets as words
1670 ParagraphList::iterator pit = cursorPar();
1671 size_t pos = cursor.pos();
1674 (pit->isSeparator(pos - 1) ||
1675 pit->isKomma(pos - 1) ||
1676 pit->isNewline(pos - 1)) &&
1677 !(pit->isHfill(pos - 1) ||
1678 pit->isInset(pos - 1)))
1682 (pit->isInset(pos - 1) ||
1683 pit->isHfill(pos - 1))) {
1686 if (pit != ownerParagraphs().begin()) {
1690 } else { // Here, cur != 0
1691 while (pos > 0 && pit->isWord(pos - 1))
1695 cursor.par(parOffset(pit));
1700 void LyXText::cursorRightOneWord(LyXCursor & cursor)
1702 // treat floats, HFills and Insets as words
1703 ParagraphList::iterator pit = cursorPar();
1704 pos_type pos = cursor.pos();
1706 if (pos == pit->size() &&
1707 boost::next(pit) != ownerParagraphs().end()) {
1711 // Skip through initial nonword stuff.
1712 while (pos < pit->size() && !pit->isWord(pos)) {
1715 // Advance through word.
1716 while (pos < pit->size() && pit->isWord(pos)) {
1721 cursor.par(parOffset(pit));
1726 void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location const loc)
1728 ParagraphList::iterator from_par = getPar(from);
1729 ParagraphList::iterator to_par = getPar(to);
1731 case lyx::WHOLE_WORD_STRICT:
1732 if (from.pos() == 0 || from.pos() == from_par->size()
1733 || from_par->isSeparator(from.pos())
1734 || from_par->isKomma(from.pos())
1735 || from_par->isNewline(from.pos())
1736 || from_par->isSeparator(from.pos() - 1)
1737 || from_par->isKomma(from.pos() - 1)
1738 || from_par->isNewline(from.pos() - 1)) {
1742 // no break here, we go to the next
1744 case lyx::WHOLE_WORD:
1745 // Move cursor to the beginning, when not already there.
1746 if (from.pos() && !from_par->isSeparator(from.pos() - 1)
1747 && !(from_par->isKomma(from.pos() - 1)
1748 || from_par->isNewline(from.pos() - 1)))
1749 cursorLeftOneWord(from);
1751 case lyx::PREVIOUS_WORD:
1752 // always move the cursor to the beginning of previous word
1753 cursorLeftOneWord(from);
1755 case lyx::NEXT_WORD:
1756 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1759 case lyx::PARTIAL_WORD:
1763 while (to.pos() < to_par->size()
1764 && !to_par->isSeparator(to.pos())
1765 && !to_par->isKomma(to.pos())
1766 && !to_par->isNewline(to.pos())
1767 && !to_par->isHfill(to.pos())
1768 && !to_par->isInset(to.pos()))
1770 to.pos(to.pos() + 1);