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.
23 #include "bufferparams.h"
24 #include "BufferView.h"
27 #include "dispatchresult.h"
29 #include "funcrequest.h"
30 #include "FontIterator.h"
34 #include "lyxlength.h"
38 #include "lyxrow_funcs.h"
39 #include "metricsinfo.h"
40 #include "paragraph.h"
41 #include "paragraph_funcs.h"
42 #include "ParagraphParameters.h"
43 #include "rowpainter.h"
46 #include "WordLangTuple.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
51 #include "insets/insettext.h"
53 #include "support/lstrings.h"
54 #include "support/textutils.h"
55 #include "support/tostr.h"
56 #include "support/std_sstream.h"
59 using lyx::word_location;
61 using lyx::support::bformat;
62 using lyx::support::contains;
63 using lyx::support::lowercase;
64 using lyx::support::split;
65 using lyx::support::uppercase;
75 /// some space for drawing the 'nested' markers (in pixel)
76 extern int const NEST_MARGIN = 20;
77 /// margin for changebar
78 extern int const CHANGEBAR_MARGIN = 10;
80 extern int const RIGHT_MARGIN = 10;
85 int numberOfSeparators(Paragraph const & par, Row const & row)
87 pos_type const first = max(row.pos(), par.beginOfBody());
88 pos_type const last = row.endpos() - 1;
90 for (pos_type p = first; p < last; ++p) {
91 if (par.isSeparator(p))
98 unsigned int maxParagraphWidth(ParagraphList const & plist)
100 unsigned int width = 0;
101 ParagraphList::const_iterator pit = plist.begin();
102 ParagraphList::const_iterator end = plist.end();
103 for (; pit != end; ++pit)
104 width = std::max(width, pit->width);
109 int numberOfLabelHfills(Paragraph const & par, Row const & row)
111 pos_type last = row.endpos() - 1;
112 pos_type first = row.pos();
114 // hfill *DO* count at the beginning of paragraphs!
116 while (first < last && par.isHfill(first))
120 last = min(last, par.beginOfBody());
122 for (pos_type p = first; p < last; ++p) {
130 int numberOfHfills(Paragraph const & par, Row const & row)
132 pos_type const last = row.endpos() - 1;
133 pos_type first = row.pos();
135 // hfill *DO* count at the beginning of paragraphs!
137 while (first < last && par.isHfill(first))
141 first = max(first, par.beginOfBody());
144 for (pos_type p = first; p < last; ++p) {
154 BufferView * LyXText::bv()
156 BOOST_ASSERT(bv_owner != 0);
161 double LyXText::spacing(Paragraph const & par) const
163 if (par.params().spacing().isDefault())
164 return bv()->buffer()->params().spacing().getValue();
165 return par.params().spacing().getValue();
169 BufferView * LyXText::bv() const
171 BOOST_ASSERT(bv_owner != 0);
176 void LyXText::updateParPositions()
178 ParagraphList::iterator pit = paragraphs().begin();
179 ParagraphList::iterator end = paragraphs().end();
180 for (height_ = 0; pit != end; ++pit) {
182 height_ += pit->height;
187 int LyXText::width() const
193 int LyXText::height() const
199 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
201 if (pos >= pit->size())
204 char const c = pit->getChar(pos);
205 return singleWidth(pit, pos, c, getFont(pit, pos));
209 int LyXText::singleWidth(ParagraphList::iterator pit,
210 pos_type pos, char c, LyXFont const & font) const
212 if (pos >= pit->size()) {
213 lyxerr << "in singleWidth(), pos: " << pos << endl;
218 // The most common case is handled first (Asger)
219 if (IsPrintable(c)) {
220 if (!font.language()->RightToLeft()) {
221 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
222 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
223 && font.language()->lang() == "arabic") {
224 if (Encodings::IsComposeChar_arabic(c))
227 c = pit->transformChar(c, pos);
228 } else if (font.language()->lang() == "hebrew" &&
229 Encodings::IsComposeChar_hebrew(c))
232 return font_metrics::width(c, font);
235 if (c == Paragraph::META_INSET)
236 return pit->getInset(pos)->width();
238 if (IsSeparatorChar(c))
240 return font_metrics::width(c, font);
244 int LyXText::leftMargin(ParagraphList::iterator pit) const
246 return leftMargin(pit, pit->size());
250 int LyXText::leftMargin(ParagraphList::iterator pit, pos_type pos) const
252 LyXTextClass const & tclass =
253 bv()->buffer()->params().getLyXTextClass();
254 LyXLayout_ptr const & layout = pit->layout();
256 string parindent = layout->parindent;
258 int x = NEST_MARGIN + CHANGEBAR_MARGIN;
260 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
262 // This is the way LyX handles LaTeX-Environments.
263 // I have had this idea very late, so it seems to be a
264 // later added hack and this is true
265 if (pit->getDepth() == 0) {
266 if (pit->layout() == tclass.defaultLayout()) {
267 // find the previous same level paragraph
268 if (pit != paragraphs().begin()) {
269 ParagraphList::iterator newpit =
270 depthHook(pit, paragraphs(), pit->getDepth());
271 if (newpit == pit && newpit->layout()->nextnoindent)
276 // find the next level paragraph
277 ParagraphList::iterator newpar =
278 outerHook(pit, paragraphs());
280 // Make a corresponding row. Need to call leftMargin()
281 // to check whether it is a sufficent paragraph.
282 if (newpar != paragraphs().end()
283 && newpar->layout()->isEnvironment()) {
284 x = leftMargin(newpar);
287 if (newpar != paragraphs().end()
288 && pit->layout() == tclass.defaultLayout()) {
289 if (newpar->params().noindent())
292 parindent = newpar->layout()->parindent;
296 LyXFont const labelfont = getLabelFont(pit);
297 switch (layout->margintype) {
299 if (!layout->leftmargin.empty())
300 x += font_metrics::signedWidth(layout->leftmargin,
301 tclass.defaultfont());
302 if (!pit->getLabelstring().empty()) {
303 x += font_metrics::signedWidth(layout->labelindent,
305 x += font_metrics::width(pit->getLabelstring(),
307 x += font_metrics::width(layout->labelsep, labelfont);
312 x += font_metrics::signedWidth(layout->labelindent, labelfont);
313 // The width of an empty par, even with manual label, should be 0
314 if (!pit->empty() && pos >= pit->beginOfBody()) {
315 if (!pit->getLabelWidthString().empty()) {
316 x += font_metrics::width(pit->getLabelWidthString(),
318 x += font_metrics::width(layout->labelsep, labelfont);
324 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
325 / (pit->getDepth() + 4);
328 case MARGIN_FIRST_DYNAMIC:
329 if (layout->labeltype == LABEL_MANUAL) {
330 if (pos >= pit->beginOfBody()) {
331 x += font_metrics::signedWidth(layout->leftmargin,
334 x += font_metrics::signedWidth(layout->labelindent,
338 // Special case to fix problems with
340 || (layout->labeltype == LABEL_STATIC
341 && layout->latextype == LATEX_ENVIRONMENT
342 && !isFirstInSequence(pit, paragraphs()))) {
343 x += font_metrics::signedWidth(layout->leftmargin,
345 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
346 && layout->labeltype != LABEL_BIBLIO
347 && layout->labeltype !=
348 LABEL_CENTERED_TOP_ENVIRONMENT) {
349 x += font_metrics::signedWidth(layout->labelindent,
351 x += font_metrics::width(layout->labelsep, labelfont);
352 x += font_metrics::width(pit->getLabelstring(),
357 case MARGIN_RIGHT_ADDRESS_BOX: {
359 // ok, a terrible hack. The left margin depends on the widest
360 // row in this paragraph.
361 RowList::iterator rit = pit->rows.begin();
362 RowList::iterator end = pit->rows.end();
363 #warning This is wrong.
364 int minfill = maxwidth_;
365 for ( ; rit != end; ++rit)
366 if (rit->fill() < minfill)
367 minfill = rit->fill();
368 x += font_metrics::signedWidth(layout->leftmargin,
369 tclass.defaultfont());
372 // also wrong, but much shorter.
379 if (!pit->params().leftIndent().zero())
380 x += pit->params().leftIndent().inPixels(maxwidth_);
384 if (pit->params().align() == LYX_ALIGN_LAYOUT)
385 align = layout->align;
387 align = pit->params().align();
389 // set the correct parindent
391 && (layout->labeltype == LABEL_NO_LABEL
392 || layout->labeltype == LABEL_TOP_ENVIRONMENT
393 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
394 || (layout->labeltype == LABEL_STATIC
395 && layout->latextype == LATEX_ENVIRONMENT
396 && !isFirstInSequence(pit, paragraphs())))
397 && align == LYX_ALIGN_BLOCK
398 && !pit->params().noindent()
399 // in tabulars and ert paragraphs are never indented!
401 || !pit->inInset()->owner()
402 || (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE
403 && pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
404 && (pit->layout() != tclass.defaultLayout()
405 || bv()->buffer()->params().paragraph_separation ==
406 BufferParams::PARSEP_INDENT))
408 x += font_metrics::signedWidth(parindent, tclass.defaultfont());
415 int LyXText::rightMargin(Paragraph const & par) const
417 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
421 + font_metrics::signedWidth(tclass.rightmargin(),
422 tclass.defaultfont())
423 + font_metrics::signedWidth(par.layout()->rightmargin,
424 tclass.defaultfont())
425 * 4 / (par.getDepth() + 4);
429 int LyXText::labelEnd(ParagraphList::iterator pit) const
431 // labelEnd is only needed if the layout fills a flushleft label.
432 if (pit->layout()->margintype != MARGIN_MANUAL)
434 // return the beginning of the body
435 return leftMargin(pit);
441 // this needs special handling - only newlines count as a break point
442 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
444 pos_type const end = par.size();
447 if (par.isNewline(i))
456 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
458 pos_type const end = pit->size();
459 pos_type const pos = row.pos();
465 // maximum pixel width of a row
466 int width = maxwidth_ - rightMargin(*pit); // - leftMargin(pit, row);
472 LyXLayout_ptr const & layout = pit->layout();
474 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
475 row.endpos(addressBreakPoint(pos, *pit));
479 pos_type const body_pos = pit->beginOfBody();
482 // Now we iterate through until we reach the right margin
483 // or the end of the par, then choose the possible break
486 int const left = leftMargin(pit, pos);
489 // pixel width since last breakpoint
492 FontIterator fi = FontIterator(*this, pit, pos);
493 pos_type point = end;
495 for ( ; i < end; ++i, ++fi) {
496 char const c = pit->getChar(i);
499 int thiswidth = singleWidth(pit, i, c, *fi);
501 // add the auto-hfill from label end to the body
502 if (body_pos && i == body_pos) {
503 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
504 if (pit->isLineSeparator(i - 1))
505 add -= singleWidth(pit, i - 1);
507 add = std::max(add, labelEnd(pit) - x);
512 chunkwidth += thiswidth;
515 // break before a character that will fall off
516 // the right of the row
518 // if no break before, break here
519 if (point == end || chunkwidth >= width - left) {
526 // exit on last registered breakpoint:
530 if (pit->isNewline(i)) {
536 if (pit->isInset(i + 1) && pit->getInset(i + 1)->display()) {
541 if (pit->isInset(i) && pit->getInset(i)->display()) {
547 if (!pit->isInset(i) || pit->getInset(i)->isChar()) {
548 // some insets are line separators too
549 if (pit->isLineSeparator(i)) {
550 // register breakpoint:
557 // maybe found one, but the par is short enough.
558 if (i == end && x < width)
561 // manual labels cannot be broken in LaTeX. But we
562 // want to make our on-screen rendering of footnotes
564 if (body_pos && point < body_pos)
571 void LyXText::setRowWidth(ParagraphList::iterator pit, Row & row) const
573 // get the pure distance
574 pos_type const end = row.endpos();
576 string labelsep = pit->layout()->labelsep;
577 int w = leftMargin(pit, row.pos());
579 pos_type const body_pos = pit->beginOfBody();
580 pos_type i = row.pos();
583 FontIterator fi = FontIterator(*this, pit, i);
584 for ( ; i < end; ++i, ++fi) {
585 if (body_pos > 0 && i == body_pos) {
586 w += font_metrics::width(labelsep, getLabelFont(pit));
587 if (pit->isLineSeparator(i - 1))
588 w -= singleWidth(pit, i - 1);
589 w = max(w, labelEnd(pit));
591 char const c = pit->getChar(i);
592 w += singleWidth(pit, i, c, *fi);
596 if (body_pos > 0 && body_pos >= end) {
597 w += font_metrics::width(labelsep, getLabelFont(pit));
598 if (end > 0 && pit->isLineSeparator(end - 1))
599 w -= singleWidth(pit, end - 1);
600 w = max(w, labelEnd(pit));
603 row.width(w + rightMargin(*pit));
607 // returns the minimum space a manual label needs on the screen in pixel
608 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
610 pos_type last = pit->beginOfBody();
612 BOOST_ASSERT(last > 0);
614 // -1 because a label ends with a space that is in the label
617 // a separator at this end does not count
618 if (pit->isLineSeparator(last))
622 for (pos_type i = row.pos(); i <= last; ++i)
623 w += singleWidth(pit, i);
625 string const & label = pit->params().labelWidthString();
629 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
633 LColor_color LyXText::backgroundColor() const
635 return LColor_color(LColor::color(background_color_));
639 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
641 // get the maximum ascent and the maximum descent
642 double layoutasc = 0;
643 double layoutdesc = 0;
644 double const dh = defaultRowHeight();
646 // ok, let us initialize the maxasc and maxdesc value.
647 // Only the fontsize count. The other properties
648 // are taken from the layoutfont. Nicer on the screen :)
649 LyXLayout_ptr const & layout = pit->layout();
651 // as max get the first character of this row then it can
652 // increase but not decrease the height. Just some point to
653 // start with so we don't have to do the assignment below too
655 LyXFont font = getFont(pit, row.pos());
656 LyXFont::FONT_SIZE const tmpsize = font.size();
657 font = getLayoutFont(pit);
658 LyXFont::FONT_SIZE const size = font.size();
659 font.setSize(tmpsize);
661 LyXFont labelfont = getLabelFont(pit);
663 // these are minimum values
664 double const spacing_val = layout->spacing.getValue() * spacing(*pit);
665 //lyxerr << "spacing_val = " << spacing_val << endl;
666 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
667 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
669 // insets may be taller
670 InsetList::iterator ii = pit->insetlist.begin();
671 InsetList::iterator iend = pit->insetlist.end();
672 for ( ; ii != iend; ++ii) {
673 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
674 maxasc = max(maxasc, ii->inset->ascent());
675 maxdesc = max(maxdesc, ii->inset->descent());
679 // Check if any custom fonts are larger (Asger)
680 // This is not completely correct, but we can live with the small,
681 // cosmetic error for now.
683 pos_type const pos_end = row.endpos();
685 LyXFont::FONT_SIZE maxsize =
686 pit->highestFontInRange(row.pos(), pos_end, size);
687 if (maxsize > font.size()) {
688 font.setSize(maxsize);
689 maxasc = max(maxasc, font_metrics::maxAscent(font));
690 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
693 // This is nicer with box insets:
697 row.ascent_of_text(maxasc);
700 if (row.pos() == 0) {
701 BufferParams const & bufparams = bv()->buffer()->params();
702 // some parksips VERY EASY IMPLEMENTATION
703 if (bv()->buffer()->params().paragraph_separation
704 == BufferParams::PARSEP_SKIP
705 && pit != paragraphs().begin()
706 && ((layout->isParagraph() && pit->getDepth() == 0)
707 || (boost::prior(pit)->layout()->isParagraph()
708 && boost::prior(pit)->getDepth() == 0)))
710 maxasc += bufparams.getDefSkip().inPixels(*bv());
713 if (pit->params().startOfAppendix())
714 maxasc += int(3 * dh);
716 // This is special code for the chapter, since the label of this
717 // layout is printed in an extra row
718 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
719 labeladdon = int(font_metrics::maxHeight(labelfont)
720 * layout->spacing.getValue() * spacing(*pit));
723 // special code for the top label
724 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
725 || layout->labeltype == LABEL_BIBLIO
726 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
727 && isFirstInSequence(pit, paragraphs())
728 && !pit->getLabelstring().empty())
731 font_metrics::maxHeight(labelfont)
732 * layout->spacing.getValue()
734 + (layout->topsep + layout->labelbottomsep) * dh);
737 // Add the layout spaces, for example before and after
738 // a section, or between the items of a itemize or enumerate
741 ParagraphList::iterator prev =
742 depthHook(pit, paragraphs(), pit->getDepth());
744 && prev->layout() == layout
745 && prev->getDepth() == pit->getDepth()
746 && prev->getLabelWidthString() == pit->getLabelWidthString())
748 layoutasc = layout->itemsep * dh;
749 } else if (pit != paragraphs().begin() || row.pos() != 0) {
750 if (layout->topsep > 0)
751 layoutasc = layout->topsep * dh;
754 prev = outerHook(pit, paragraphs());
755 if (prev != paragraphs().end()) {
756 maxasc += int(prev->layout()->parsep * dh);
757 } else if (pit != paragraphs().begin()) {
758 ParagraphList::iterator prior_pit = boost::prior(pit);
759 if (prior_pit->getDepth() != 0 ||
760 prior_pit->layout() == layout) {
761 maxasc += int(layout->parsep * dh);
766 // is it a bottom line?
767 if (row.endpos() >= pit->size()) {
768 // add the layout spaces, for example before and after
769 // a section, or between the items of a itemize or enumerate
771 ParagraphList::iterator nextpit = boost::next(pit);
772 if (nextpit != paragraphs().end()) {
773 ParagraphList::iterator cpit = pit;
777 if (cpit->getDepth() > nextpit->getDepth()) {
778 usual = cpit->layout()->bottomsep * dh;
779 cpit = depthHook(cpit, paragraphs(), nextpit->getDepth());
780 if (cpit->layout() != nextpit->layout()
781 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
783 unusual = cpit->layout()->bottomsep * dh;
785 layoutdesc = max(unusual, usual);
786 } else if (cpit->getDepth() == nextpit->getDepth()) {
787 if (cpit->layout() != nextpit->layout()
788 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
789 layoutdesc = int(cpit->layout()->bottomsep * dh);
794 // incalculate the layout spaces
795 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
796 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
798 row.height(maxasc + maxdesc + labeladdon);
799 row.baseline(maxasc + labeladdon);
800 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
804 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
806 BOOST_ASSERT(this == cur.text());
807 // allow only if at start or end, or all previous is new text
808 Paragraph & cpar = cur.paragraph();
809 ParagraphList::iterator cpit = getPar(cur.par());
811 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
812 && cpar.isChangeEdited(0, cur.pos()))
815 LyXTextClass const & tclass =
816 bv()->buffer()->params().getLyXTextClass();
817 LyXLayout_ptr const & layout = cpar.layout();
819 // this is only allowed, if the current paragraph is not empty
820 // or caption and if it has not the keepempty flag active
821 if (cur.lastpos() == 0 && !cpar.allowEmpty()
822 && layout->labeltype != LABEL_SENSITIVE)
825 // a layout change may affect also the following paragraph
826 recUndo(cur.par(), parOffset(undoSpan(cpit)) - 1);
828 // Always break behind a space
829 // It is better to erase the space (Dekel)
830 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
831 cpar.erase(cur.pos());
833 // break the paragraph
837 keep_layout = layout->isEnvironment();
839 // we need to set this before we insert the paragraph. IMO the
840 // breakParagraph call should return a bool if it inserts the
841 // paragraph before or behind and we should react on that one
842 // but we can fix this in 1.3.0 (Jug 20020509)
843 bool const isempty = cpar.allowEmpty() && cpar.empty();
844 ::breakParagraph(bv()->buffer()->params(), paragraphs(), cpit,
845 cur.pos(), keep_layout);
847 cpit = getPar(cur.par());
848 ParagraphList::iterator next_par = boost::next(cpit);
850 // well this is the caption hack since one caption is really enough
851 if (layout->labeltype == LABEL_SENSITIVE) {
853 // set to standard-layout
854 cpit->applyLayout(tclass.defaultLayout());
856 // set to standard-layout
857 next_par->applyLayout(tclass.defaultLayout());
860 // if the cursor is at the beginning of a row without prior newline,
862 // This touches only the screen-update. Otherwise we would may have
863 // an empty row on the screen
864 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
865 && !cpit->isNewline(cur.pos() - 1))
870 while (!next_par->empty() && next_par->isNewline(0))
875 redoParagraph(next_par);
877 // This check is necessary. Otherwise the new empty paragraph will
878 // be deleted automatically. And it is more friendly for the user!
879 if (cur.pos() != 0 || isempty)
880 setCursor(cur, cur.par() + 1, 0);
882 setCursor(cur, cur.par(), 0);
886 // convenience function
887 void LyXText::redoParagraph(LCursor & cur)
889 BOOST_ASSERT(this == cur.text());
890 cur.clearSelection();
891 redoParagraph(getPar(cur.par()));
892 setCursorIntern(cur, cur.par(), cur.pos());
896 // insert a character, moves all the following breaks in the
897 // same Paragraph one to the right and make a rebreak
898 void LyXText::insertChar(LCursor & cur, char c)
900 BOOST_ASSERT(this == cur.text());
901 BOOST_ASSERT(c != Paragraph::META_INSET);
903 recordUndo(cur, Undo::INSERT);
905 Paragraph & par = cur.paragraph();
906 // try to remove this
907 ParagraphList::iterator pit = getPar(cur.par());
909 bool const freeSpacing = par.layout()->free_spacing ||
912 if (lyxrc.auto_number) {
913 static string const number_operators = "+-/*";
914 static string const number_unary_operators = "+-";
915 static string const number_seperators = ".,:";
917 if (current_font.number() == LyXFont::ON) {
918 if (!IsDigit(c) && !contains(number_operators, c) &&
919 !(contains(number_seperators, c) &&
921 cur.pos() != cur.lastpos() &&
922 getFont(pit, cur.pos()).number() == LyXFont::ON &&
923 getFont(pit, cur.pos() - 1).number() == LyXFont::ON)
925 number(cur); // Set current_font.number to OFF
926 } else if (IsDigit(c) &&
927 real_current_font.isVisibleRightToLeft()) {
928 number(cur); // Set current_font.number to ON
930 if (cur.pos() != 0) {
931 char const c = par.getChar(cur.pos() - 1);
932 if (contains(number_unary_operators, c) &&
934 || par.isSeparator(cur.pos() - 2)
935 || par.isNewline(cur.pos() - 2))
937 setCharFont(pit, cur.pos() - 1, current_font);
938 } else if (contains(number_seperators, c)
940 && getFont(pit, cur.pos() - 2).number() == LyXFont::ON) {
941 setCharFont(pit, cur.pos() - 1, current_font);
947 // First check, if there will be two blanks together or a blank at
948 // the beginning of a paragraph.
949 // I decided to handle blanks like normal characters, the main
950 // difference are the special checks when calculating the row.fill
951 // (blank does not count at the end of a row) and the check here
953 // The bug is triggered when we type in a description environment:
954 // The current_font is not changed when we go from label to main text
955 // and it should (along with realtmpfont) when we type the space.
956 // CHECK There is a bug here! (Asger)
958 // store the current font. This is because of the use of cursor
959 // movements. The moving cursor would refresh the current font
960 LyXFont realtmpfont = real_current_font;
961 LyXFont rawtmpfont = current_font;
963 // When the free-spacing option is set for the current layout,
964 // disable the double-space checking
965 if (!freeSpacing && IsLineSeparatorChar(c)) {
966 if (cur.pos() == 0) {
967 static bool sent_space_message = false;
968 if (!sent_space_message) {
969 cur.message(_("You cannot insert a space at the "
970 "beginning of a paragraph. Please read the Tutorial."));
971 sent_space_message = true;
975 BOOST_ASSERT(cur.pos() > 0);
976 if (par.isLineSeparator(cur.pos() - 1)
977 || par.isNewline(cur.pos() - 1)) {
978 static bool sent_space_message = false;
979 if (!sent_space_message) {
980 cur.message(_("You cannot type two spaces this way. "
981 "Please read the Tutorial."));
982 sent_space_message = true;
988 par.insertChar(cur.pos(), c);
989 setCharFont(pit, cur.pos(), rawtmpfont);
991 current_font = rawtmpfont;
992 real_current_font = realtmpfont;
994 setCursor(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
999 void LyXText::charInserted()
1001 // Here we call finishUndo for every 20 characters inserted.
1002 // This is from my experience how emacs does it. (Lgb)
1003 static unsigned int counter;
1014 LyXText::computeRowMetrics(ParagraphList::iterator pit, Row const & row) const
1018 double w = width_ - row.width();
1020 bool const is_rtl = isRTL(*pit);
1022 result.x = rightMargin(*pit);
1024 result.x = leftMargin(pit, row.pos());
1026 // is there a manual margin with a manual label
1027 LyXLayout_ptr const & layout = pit->layout();
1029 if (layout->margintype == MARGIN_MANUAL
1030 && layout->labeltype == LABEL_MANUAL) {
1031 /// We might have real hfills in the label part
1032 int nlh = numberOfLabelHfills(*pit, row);
1034 // A manual label par (e.g. List) has an auto-hfill
1035 // between the label text and the body of the
1037 // But we don't want to do this auto hfill if the par
1042 if (nlh && !pit->getLabelWidthString().empty())
1043 result.label_hfill = labelFill(pit, row) / double(nlh);
1046 // are there any hfills in the row?
1047 int const nh = numberOfHfills(*pit, row);
1051 result.hfill = w / nh;
1052 // we don't have to look at the alignment if it is ALIGN_LEFT and
1053 // if the row is already larger then the permitted width as then
1054 // we force the LEFT_ALIGN'edness!
1055 } else if (int(row.width()) < maxwidth_) {
1056 // is it block, flushleft or flushright?
1057 // set x how you need it
1059 if (pit->params().align() == LYX_ALIGN_LAYOUT)
1060 align = layout->align;
1062 align = pit->params().align();
1064 // Display-style insets should always be on a centred row
1065 // The test on pit->size() is to catch zero-size pars, which
1066 // would trigger the assert in Paragraph::getInset().
1067 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1069 && pit->isInset(row.pos())
1070 && pit->getInset(row.pos())->display())
1072 align = LYX_ALIGN_CENTER;
1076 case LYX_ALIGN_BLOCK: {
1077 int const ns = numberOfSeparators(*pit, row);
1078 bool disp_inset = false;
1079 if (row.endpos() < pit->size()) {
1080 InsetBase * in = pit->getInset(row.endpos());
1082 disp_inset = in->display();
1084 // If we have separators, this is not the last row of a
1085 // par, does not end in newline, and is not row above a
1086 // display inset... then stretch it
1088 && row.endpos() < pit->size()
1089 && !pit->isNewline(row.endpos() - 1)
1092 result.separator = w / ns;
1093 } else if (is_rtl) {
1098 case LYX_ALIGN_RIGHT:
1101 case LYX_ALIGN_CENTER:
1107 bidi.computeTables(*pit, *bv()->buffer(), row);
1109 pos_type body_pos = pit->beginOfBody();
1110 pos_type end = row.endpos();
1113 && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1115 result.x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1116 if (body_pos <= end)
1117 result.x += result.label_hfill;
1125 // the cursor set functions have a special mechanism. When they
1126 // realize, that you left an empty paragraph, they will delete it.
1128 void LyXText::cursorRightOneWord(LCursor & cur)
1130 BOOST_ASSERT(this == cur.text());
1131 if (cur.pos() == cur.lastpos() && cur.par() != cur.lastpar()) {
1135 // Skip through initial nonword stuff.
1136 // Treat floats and insets as words.
1137 while (cur.pos() != cur.lastpos() && !cur.paragraph().isWord(cur.pos()))
1139 // Advance through word.
1140 while (cur.pos() != cur.lastpos() && cur.paragraph().isWord(cur.pos()))
1143 setCursor(cur, cur.par(), cur.pos());
1147 void LyXText::cursorLeftOneWord(LCursor & cur)
1149 BOOST_ASSERT(this == cur.text());
1150 if (cur.pos() == 0 && cur.par() != 0) {
1152 cur.pos() = cur.lastpos();
1154 // Skip through initial nonword stuff.
1155 // Treat floats and insets as words.
1156 while (cur.pos() != 0 && !cur.paragraph().isWord(cur.pos() - 1))
1158 // Advance through word.
1159 while (cur.pos() != 0 && cur.paragraph().isWord(cur.pos() - 1))
1162 setCursor(cur, cur.par(), cur.pos());
1166 void LyXText::selectWord(LCursor & cur, word_location loc)
1168 BOOST_ASSERT(this == cur.text());
1169 CursorSlice from = cur.top();
1170 CursorSlice to = cur.top();
1171 getWord(from, to, loc);
1172 if (cur.top() != from)
1173 setCursor(cur, from.par(), from.pos());
1177 setCursor(cur, to.par(), to.pos());
1182 // Select the word currently under the cursor when no
1183 // selection is currently set
1184 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1186 BOOST_ASSERT(this == cur.text());
1187 if (cur.selection())
1189 selectWord(cur, loc);
1190 return cur.selection();
1194 void LyXText::acceptChange(LCursor & cur)
1196 BOOST_ASSERT(this == cur.text());
1197 if (!cur.selection() && cur.lastpos() != 0)
1200 CursorSlice const & startc = cur.selBegin();
1201 CursorSlice const & endc = cur.selEnd();
1202 if (startc.par() == endc.par()) {
1203 recordUndoSelection(cur, Undo::INSERT);
1204 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1206 cur.clearSelection();
1207 redoParagraph(getPar(startc));
1208 setCursorIntern(cur, startc.par(), 0);
1210 #warning handle multi par selection
1214 void LyXText::rejectChange(LCursor & cur)
1216 BOOST_ASSERT(this == cur.text());
1217 if (!cur.selection() && cur.lastpos() != 0)
1220 CursorSlice const & startc = cur.selBegin();
1221 CursorSlice const & endc = cur.selEnd();
1222 if (startc.par() == endc.par()) {
1223 recordUndoSelection(cur, Undo::INSERT);
1224 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1226 cur.clearSelection();
1227 redoParagraph(getPar(startc));
1228 setCursorIntern(cur, startc.par(), 0);
1230 #warning handle multi par selection
1234 // Delete from cursor up to the end of the current or next word.
1235 void LyXText::deleteWordForward(LCursor & cur)
1237 BOOST_ASSERT(this == cur.text());
1238 if (cur.lastpos() == 0)
1242 cur.selection() = true;
1243 cursorRightOneWord(cur);
1245 cutSelection(cur, true, false);
1250 // Delete from cursor to start of current or prior word.
1251 void LyXText::deleteWordBackward(LCursor & cur)
1253 BOOST_ASSERT(this == cur.text());
1254 if (cur.lastpos() == 0)
1258 cur.selection() = true;
1259 cursorLeftOneWord(cur);
1261 cutSelection(cur, true, false);
1266 // Kill to end of line.
1267 void LyXText::deleteLineForward(LCursor & cur)
1269 BOOST_ASSERT(this == cur.text());
1270 if (cur.lastpos() == 0) {
1271 // Paragraph is empty, so we just go to the right
1275 cur.selection() = true; // to avoid deletion
1278 // What is this test for ??? (JMarc)
1279 if (!cur.selection())
1280 deleteWordForward(cur);
1282 cutSelection(cur, true, false);
1287 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1289 BOOST_ASSERT(this == cur.text());
1293 if (cur.selection()) {
1294 from = cur.selBegin();
1298 getWord(from, to, lyx::PARTIAL_WORD);
1299 setCursor(cur, to.par(), to.pos() + 1);
1302 recordUndoSelection(cur);
1304 pos_type pos = from.pos();
1305 int par = from.par();
1307 while (par != int(paragraphs().size()) &&
1308 (pos != to.pos() || par != to.par())) {
1309 ParagraphList::iterator pit = getPar(par);
1310 if (pos == pit->size()) {
1315 unsigned char c = pit->getChar(pos);
1316 if (c != Paragraph::META_INSET) {
1318 case text_lowercase:
1321 case text_capitalization:
1323 action = text_lowercase;
1325 case text_uppercase:
1331 pit->setChar(pos, c);
1337 void LyXText::Delete(LCursor & cur)
1339 BOOST_ASSERT(this == cur.text());
1340 // just move to the right, if we had success make a backspace
1341 CursorSlice sl = cur.top();
1343 if (sl == cur.top()) {
1344 recordUndo(cur, Undo::DELETE, cur.par(), max(0, cur.par() - 1));
1350 void LyXText::backspace(LCursor & cur)
1352 BOOST_ASSERT(this == cur.text());
1353 if (cur.pos() == 0) {
1354 // The cursor is at the beginning of a paragraph, so
1355 // the the backspace will collapse two paragraphs into
1358 // but it's not allowed unless it's new
1359 Paragraph & par = cur.paragraph();
1360 if (par.isChangeEdited(0, par.size()))
1363 // we may paste some paragraphs
1365 // is it an empty paragraph?
1366 pos_type lastpos = cur.lastpos();
1367 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1368 // This is an empty paragraph and we delete it just
1369 // by moving the cursor one step
1370 // left and let the DeleteEmptyParagraphMechanism
1371 // handle the actual deletion of the paragraph.
1373 if (cur.par() != 0) {
1375 // the layout things can change the height of a row !
1382 recordUndo(cur, Undo::DELETE, cur.par() - 1);
1384 ParagraphList::iterator tmppit = getPar(cur.par());
1385 // We used to do cursorLeftIntern() here, but it is
1386 // not a good idea since it triggers the auto-delete
1387 // mechanism. So we do a cursorLeftIntern()-lite,
1388 // without the dreaded mechanism. (JMarc)
1389 if (cur.par() != 0) {
1390 // steps into the above paragraph.
1391 setCursorIntern(cur, cur.par() - 1,
1392 getPar(cur.par() - 1)->size(),
1396 // Pasting is not allowed, if the paragraphs have different
1397 // layout. I think it is a real bug of all other
1398 // word processors to allow it. It confuses the user.
1399 // Correction: Pasting is always allowed with standard-layout
1400 Buffer & buf = *bv()->buffer();
1401 BufferParams const & bufparams = buf.params();
1402 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1403 ParagraphList::iterator const cpit = getPar(cur.par());
1406 && (cpit->layout() == tmppit->layout()
1407 || tmppit->layout() == tclass.defaultLayout())
1408 && cpit->getAlign() == tmppit->getAlign()) {
1409 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1411 if (cur.pos() != 0 && cpit->isSeparator(cur.pos() - 1))
1414 // the counters may have changed
1416 setCursor(cur, cur.par(), cur.pos(), false);
1419 // this is the code for a normal backspace, not pasting
1421 recordUndo(cur, Undo::DELETE);
1422 // We used to do cursorLeftIntern() here, but it is
1423 // not a good idea since it triggers the auto-delete
1424 // mechanism. So we do a cursorLeftIntern()-lite,
1425 // without the dreaded mechanism. (JMarc)
1426 setCursorIntern(cur, cur.par(), cur.pos() - 1,
1427 false, cur.boundary());
1428 cur.paragraph().erase(cur.pos());
1431 if (cur.pos() == cur.lastpos())
1432 setCurrentFont(cur);
1435 setCursor(cur, cur.par(), cur.pos(), false, cur.boundary());
1439 ParagraphList::iterator LyXText::getPar(CursorSlice const & cur) const
1441 return getPar(cur.par());
1445 ParagraphList::iterator LyXText::getPar(int par) const
1447 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1448 BOOST_ASSERT(par >= 0);
1449 BOOST_ASSERT(par < int(paragraphs().size()));
1450 ParagraphList::iterator pit = paragraphs().begin();
1456 // y is relative to this LyXText's top
1458 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1460 BOOST_ASSERT(!paragraphs().empty());
1461 BOOST_ASSERT(!paragraphs().begin()->rows.empty());
1463 ParagraphList::iterator const
1464 pend = boost::prior(paragraphs().end());
1465 pit = paragraphs().begin();
1466 while (int(pit->y + pit->height) < y && pit != pend)
1469 RowList::iterator rit = pit->rows.end();
1470 RowList::iterator const rbegin = pit->rows.begin();
1473 } while (rit != rbegin && int(pit->y + rit->y_offset()) > y);
1477 pit = boost::prior(paragraphs().end());
1479 RowList::iterator rit = lastRow();
1480 RowList::iterator rbegin = firstRow();
1482 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1483 previousRow(pit, rit);
1490 RowList::iterator LyXText::firstRow() const
1492 return paragraphs().front().rows.begin();
1496 RowList::iterator LyXText::lastRow() const
1498 return boost::prior(endRow());
1502 RowList::iterator LyXText::endRow() const
1504 return paragraphs().back().rows.end();
1508 void LyXText::nextRow(ParagraphList::iterator & pit,
1509 RowList::iterator & rit) const
1512 if (rit == pit->rows.end()) {
1514 if (pit == paragraphs().end())
1517 rit = pit->rows.begin();
1522 void LyXText::previousRow(ParagraphList::iterator & pit,
1523 RowList::iterator & rit) const
1525 if (rit != pit->rows.begin())
1528 BOOST_ASSERT(pit != paragraphs().begin());
1530 rit = boost::prior(pit->rows.end());
1535 int LyXText::parOffset(ParagraphList::iterator pit) const
1537 return distance(paragraphs().begin(), pit);
1541 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1543 // remove rows of paragraph, keep track of height changes
1544 height_ -= pit->height;
1552 InsetList::iterator ii = pit->insetlist.begin();
1553 InsetList::iterator iend = pit->insetlist.end();
1554 for (; ii != iend; ++ii) {
1556 int const w = maxwidth_ - leftMargin(pit) - rightMargin(*pit);
1557 MetricsInfo mi(bv(), getFont(pit, ii->pos), w);
1558 ii->inset->metrics(mi, dim);
1561 // rebreak the paragraph
1562 pit->setBeginOfBody();
1566 rowBreakPoint(pit, row);
1567 setRowWidth(pit, row);
1568 setHeightOfRow(pit, row);
1569 row.y_offset(pit->height);
1570 pit->rows.push_back(row);
1571 pit->width = std::max(pit->width, row.width());
1572 pit->height += row.height();
1574 } while (z < pit->size());
1576 height_ += pit->height;
1577 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1581 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1582 ParagraphList::iterator end)
1584 for ( ; pit != end; ++pit)
1585 redoParagraphInternal(pit);
1586 updateParPositions();
1590 void LyXText::redoParagraph(ParagraphList::iterator pit)
1592 redoParagraphInternal(pit);
1593 updateParPositions();
1597 void LyXText::fullRebreak()
1599 redoParagraphs(paragraphs().begin(), paragraphs().end());
1600 bv()->cursor().resetAnchor();
1604 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1606 //BOOST_ASSERT(mi.base.textwidth);
1607 if (mi.base.textwidth)
1608 maxwidth_ = mi.base.textwidth;
1609 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1610 //<< " maxWidth: " << maxwidth << "\nfont: " << mi.base.font
1613 // Rebuild row cache. This recomputes height as well.
1614 redoParagraphs(paragraphs().begin(), paragraphs().end());
1616 width_ = maxParagraphWidth(paragraphs());
1619 dim.asc = firstRow()->ascent_of_text();
1620 dim.des = height_ - dim.asc;
1625 // only used for inset right now. should also be used for main text
1626 void LyXText::draw(PainterInfo & pi, int x, int y) const
1630 paintTextInset(*this, pi, x, y);
1634 // only used for inset right now. should also be used for main text
1635 void LyXText::drawSelection(PainterInfo &, int, int) const
1637 //lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
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::getWord(CursorSlice & from, CursorSlice & to,
1655 word_location const loc)
1657 Paragraph & from_par = *getPar(from);
1659 case lyx::WHOLE_WORD_STRICT:
1660 if (from.pos() == 0 || from.pos() == from_par.size()
1661 || from_par.isSeparator(from.pos())
1662 || from_par.isKomma(from.pos())
1663 || from_par.isNewline(from.pos())
1664 || from_par.isSeparator(from.pos() - 1)
1665 || from_par.isKomma(from.pos() - 1)
1666 || from_par.isNewline(from.pos() - 1)) {
1670 // no break here, we go to the next
1672 case lyx::WHOLE_WORD:
1673 // Move cursor to the beginning, when not already there.
1674 if (from.pos() && !from_par.isSeparator(from.pos() - 1)
1675 && !(from_par.isKomma(from.pos() - 1)
1676 || from_par.isNewline(from.pos() - 1)))
1677 cursorLeftOneWord(bv()->cursor());
1679 case lyx::PREVIOUS_WORD:
1680 // always move the cursor to the beginning of previous word
1681 cursorLeftOneWord(bv()->cursor());
1683 case lyx::NEXT_WORD:
1684 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1687 case lyx::PARTIAL_WORD:
1691 Paragraph & to_par = *getPar(to);
1692 while (to.pos() < to_par.size()
1693 && !to_par.isSeparator(to.pos())
1694 && !to_par.isKomma(to.pos())
1695 && !to_par.isNewline(to.pos())
1696 && !to_par.isHfill(to.pos())
1697 && !to_par.isInset(to.pos()))
1704 void LyXText::write(Buffer const & buf, std::ostream & os) const
1706 ParagraphList::const_iterator pit = paragraphs().begin();
1707 ParagraphList::const_iterator end = paragraphs().end();
1708 Paragraph::depth_type dth = 0;
1709 for (; pit != end; ++pit)
1710 pit->write(buf, os, buf.params(), dth);
1714 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1716 static Change current_change;
1718 bool the_end_read = false;
1719 ParagraphList::iterator pit = paragraphs().begin();
1720 Paragraph::depth_type depth = 0;
1722 while (lex.isOK()) {
1724 string token = lex.getString();
1729 if (token == "\\end_inset") {
1730 the_end_read = true;
1734 if (token == "\\end_document") {
1737 lex.printError("\\end_document read in inset! Error in document!");
1745 if (token == "\\begin_layout") {
1746 lex.pushToken(token);
1749 par.params().depth(depth);
1750 if (buf.params().tracking_changes)
1752 par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1755 if (pit != paragraphs().end())
1758 pit = paragraphs().insert(pit, par);
1760 // FIXME: goddamn InsetTabular makes us pass a Buffer
1762 ::readParagraph(buf, *pit, lex);
1764 } else if (token == "\\begin_deeper") {
1766 } else if (token == "\\end_deeper") {
1768 lex.printError("\\end_deeper: " "depth is already null");
1777 return the_end_read;
1781 int LyXText::ascent() const
1783 return firstRow()->ascent_of_text();
1787 int LyXText::descent() const
1789 return height_ - firstRow()->ascent_of_text();
1793 int LyXText::cursorX(CursorSlice const & cur) const
1795 ParagraphList::iterator pit = getPar(cur);
1796 if (pit->rows.empty())
1799 Row const & row = *pit->getRow(cur.pos());
1801 pos_type pos = cur.pos();
1802 pos_type cursor_vpos = 0;
1804 RowMetrics const m = computeRowMetrics(pit, row);
1807 pos_type const row_pos = row.pos();
1808 pos_type const end = row.endpos();
1811 cursor_vpos = row_pos;
1812 else if (pos >= end)
1813 cursor_vpos = isRTL(*pit) ? row_pos : end;
1814 else if (pos > row_pos && pos >= end)
1815 // Place cursor after char at (logical) position pos - 1
1816 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1817 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1819 // Place cursor before char at (logical) position pos
1820 cursor_vpos = (bidi.level(pos) % 2 == 0)
1821 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1823 pos_type body_pos = pit->beginOfBody();
1825 (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1828 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1829 pos_type pos = bidi.vis2log(vpos);
1830 if (body_pos > 0 && pos == body_pos - 1) {
1832 + font_metrics::width(pit->layout()->labelsep,
1834 if (pit->isLineSeparator(body_pos - 1))
1835 x -= singleWidth(pit, body_pos - 1);
1838 if (hfillExpansion(*pit, row, pos)) {
1839 x += singleWidth(pit, pos);
1840 if (pos >= body_pos)
1844 } else if (pit->isSeparator(pos)) {
1845 x += singleWidth(pit, pos);
1846 if (pos >= body_pos)
1849 x += singleWidth(pit, pos);
1851 return xo_ + int(x);
1855 int LyXText::cursorY(CursorSlice const & cur) const
1857 Paragraph & par = *getPar(cur);
1858 Row & row = *par.getRow(cur.pos());
1859 return yo_ + par.y + row.y_offset() + row.baseline();
1863 void LyXText::replaceSelection(LCursor & cur)
1865 BOOST_ASSERT(this == cur.text());
1866 if (cur.selection()) {
1867 cutSelection(cur, true, false);
1873 // Returns the current font and depth as a message.
1874 string LyXText::currentState(LCursor & cur)
1876 BOOST_ASSERT(this == cur.text());
1877 Buffer * buffer = bv()->buffer();
1878 Paragraph const & par = cur.paragraph();
1879 std::ostringstream os;
1881 bool const show_change = buffer->params().tracking_changes
1882 && cur.pos() != cur.lastpos()
1883 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
1886 Change change = par.lookupChangeFull(cur.pos());
1887 Author const & a = buffer->params().authors().get(change.author);
1888 os << _("Change: ") << a.name();
1889 if (!a.email().empty())
1890 os << " (" << a.email() << ")";
1891 if (change.changetime)
1892 os << _(" at ") << ctime(&change.changetime);
1896 // I think we should only show changes from the default
1898 LyXFont font = real_current_font;
1899 font.reduce(buffer->params().getLyXTextClass().defaultfont());
1901 // avoid _(...) re-entrance problem
1902 string const s = font.stateText(&buffer->params());
1903 os << bformat(_("Font: %1$s"), s);
1905 // os << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
1907 // The paragraph depth
1908 int depth = cur.paragraph().getDepth();
1910 os << bformat(_(", Depth: %1$s"), tostr(depth));
1912 // The paragraph spacing, but only if different from
1914 Spacing const & spacing = par.params().spacing();
1915 if (!spacing.isDefault()) {
1916 os << _(", Spacing: ");
1917 switch (spacing.getSpace()) {
1918 case Spacing::Single:
1921 case Spacing::Onehalf:
1924 case Spacing::Double:
1927 case Spacing::Other:
1928 os << _("Other (") << spacing.getValue() << ')';
1930 case Spacing::Default:
1931 // should never happen, do nothing
1935 #ifdef DEVEL_VERSION
1936 os << _(", Paragraph: ") << par.id();
1937 os << _(", Position: ") << cur.pos();
1938 Row & row = cur.textRow();
1939 os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1940 os << _(", Inset: ");
1941 InsetOld * inset = par.inInset();
1943 os << inset << " owner: " << inset->owner();
1951 string LyXText::getPossibleLabel(LCursor & cur) const
1953 ParagraphList & plist = paragraphs();
1954 ParagraphList::iterator pit = getPar(cur.par());
1956 LyXLayout_ptr layout = pit->layout();
1958 if (layout->latextype == LATEX_PARAGRAPH && pit != plist.begin()) {
1959 ParagraphList::iterator pit2 = boost::prior(pit);
1961 LyXLayout_ptr const & layout2 = pit2->layout();
1963 if (layout2->latextype != LATEX_PARAGRAPH) {
1969 string text = layout->latexname().substr(0, 3);
1970 if (layout->latexname() == "theorem")
1971 text = "thm"; // Create a correct prefix for prettyref
1974 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
1977 string par_text = pit->asString(*cur.bv().buffer(), false);
1978 for (int i = 0; i < lyxrc.label_init_length; ++i) {
1979 if (par_text.empty())
1982 par_text = split(par_text, head, ' ');
1983 // Is it legal to use spaces in labels ?
1993 int LyXText::dist(int x, int y) const
2000 else if (x > xo_ + width_)
2001 xx = x - xo_ - width_;
2003 if (y < yo_ - ascent())
2004 yy = yo_ - ascent() - y;
2005 else if (y > yo_ + descent())
2006 yy = y - yo_ - descent();