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::textWidth() const
193 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
195 if (pos >= pit->size())
198 char const c = pit->getChar(pos);
199 return singleWidth(pit, pos, c, getFont(pit, pos));
203 int LyXText::singleWidth(ParagraphList::iterator pit,
204 pos_type pos, char c, LyXFont const & font) const
206 if (pos >= pit->size()) {
207 lyxerr << "in singleWidth(), pos: " << pos << endl;
212 // The most common case is handled first (Asger)
213 if (IsPrintable(c)) {
214 if (!font.language()->RightToLeft()) {
215 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
216 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
217 && font.language()->lang() == "arabic") {
218 if (Encodings::IsComposeChar_arabic(c))
221 c = pit->transformChar(c, pos);
222 } else if (font.language()->lang() == "hebrew" &&
223 Encodings::IsComposeChar_hebrew(c))
226 return font_metrics::width(c, font);
229 if (c == Paragraph::META_INSET)
230 return pit->getInset(pos)->width();
232 if (IsSeparatorChar(c))
234 return font_metrics::width(c, font);
238 int LyXText::leftMargin(ParagraphList::iterator pit) const
240 return leftMargin(pit, pit->size());
244 int LyXText::leftMargin(ParagraphList::iterator pit, pos_type pos) const
246 LyXTextClass const & tclass =
247 bv()->buffer()->params().getLyXTextClass();
248 LyXLayout_ptr const & layout = pit->layout();
250 string parindent = layout->parindent;
252 int x = NEST_MARGIN + CHANGEBAR_MARGIN;
254 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
256 // This is the way LyX handles LaTeX-Environments.
257 // I have had this idea very late, so it seems to be a
258 // later added hack and this is true
259 if (pit->getDepth() == 0) {
260 if (pit->layout() == tclass.defaultLayout()) {
261 // find the previous same level paragraph
262 if (pit != paragraphs().begin()) {
263 ParagraphList::iterator newpit =
264 depthHook(pit, paragraphs(), pit->getDepth());
265 if (newpit == pit && newpit->layout()->nextnoindent)
270 // find the next level paragraph
271 ParagraphList::iterator newpar =
272 outerHook(pit, paragraphs());
274 // Make a corresponding row. Need to call leftMargin()
275 // to check whether it is a sufficent paragraph.
276 if (newpar != paragraphs().end()
277 && newpar->layout()->isEnvironment()) {
278 x = leftMargin(newpar);
281 if (newpar != paragraphs().end()
282 && pit->layout() == tclass.defaultLayout()) {
283 if (newpar->params().noindent())
286 parindent = newpar->layout()->parindent;
290 LyXFont const labelfont = getLabelFont(pit);
291 switch (layout->margintype) {
293 if (!layout->leftmargin.empty())
294 x += font_metrics::signedWidth(layout->leftmargin,
295 tclass.defaultfont());
296 if (!pit->getLabelstring().empty()) {
297 x += font_metrics::signedWidth(layout->labelindent,
299 x += font_metrics::width(pit->getLabelstring(),
301 x += font_metrics::width(layout->labelsep, labelfont);
306 x += font_metrics::signedWidth(layout->labelindent, labelfont);
307 // The width of an empty par, even with manual label, should be 0
308 if (!pit->empty() && pos >= pit->beginOfBody()) {
309 if (!pit->getLabelWidthString().empty()) {
310 x += font_metrics::width(pit->getLabelWidthString(),
312 x += font_metrics::width(layout->labelsep, labelfont);
318 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
319 / (pit->getDepth() + 4);
322 case MARGIN_FIRST_DYNAMIC:
323 if (layout->labeltype == LABEL_MANUAL) {
324 if (pos >= pit->beginOfBody()) {
325 x += font_metrics::signedWidth(layout->leftmargin,
328 x += font_metrics::signedWidth(layout->labelindent,
332 // Special case to fix problems with
334 || (layout->labeltype == LABEL_STATIC
335 && layout->latextype == LATEX_ENVIRONMENT
336 && !isFirstInSequence(pit, paragraphs()))) {
337 x += font_metrics::signedWidth(layout->leftmargin,
339 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
340 && layout->labeltype != LABEL_BIBLIO
341 && layout->labeltype !=
342 LABEL_CENTERED_TOP_ENVIRONMENT) {
343 x += font_metrics::signedWidth(layout->labelindent,
345 x += font_metrics::width(layout->labelsep, labelfont);
346 x += font_metrics::width(pit->getLabelstring(),
351 case MARGIN_RIGHT_ADDRESS_BOX: {
353 // ok, a terrible hack. The left margin depends on the widest
354 // row in this paragraph.
355 RowList::iterator rit = pit->rows.begin();
356 RowList::iterator end = pit->rows.end();
357 #warning This is wrong.
358 int minfill = textwidth_;
359 for ( ; rit != end; ++rit)
360 if (rit->fill() < minfill)
361 minfill = rit->fill();
362 x += font_metrics::signedWidth(layout->leftmargin,
363 tclass.defaultfont());
366 // also wrong, but much shorter.
373 if (!pit->params().leftIndent().zero())
374 x += pit->params().leftIndent().inPixels(textWidth());
378 if (pit->params().align() == LYX_ALIGN_LAYOUT)
379 align = layout->align;
381 align = pit->params().align();
383 // set the correct parindent
385 && (layout->labeltype == LABEL_NO_LABEL
386 || layout->labeltype == LABEL_TOP_ENVIRONMENT
387 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
388 || (layout->labeltype == LABEL_STATIC
389 && layout->latextype == LATEX_ENVIRONMENT
390 && !isFirstInSequence(pit, paragraphs())))
391 && align == LYX_ALIGN_BLOCK
392 && !pit->params().noindent()
393 // in tabulars and ert paragraphs are never indented!
395 || !pit->inInset()->owner()
396 || (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE
397 && pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
398 && (pit->layout() != tclass.defaultLayout()
399 || bv()->buffer()->params().paragraph_separation ==
400 BufferParams::PARSEP_INDENT))
402 x += font_metrics::signedWidth(parindent, tclass.defaultfont());
409 int LyXText::rightMargin(Paragraph const & par) const
411 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
415 + font_metrics::signedWidth(tclass.rightmargin(),
416 tclass.defaultfont())
417 + font_metrics::signedWidth(par.layout()->rightmargin,
418 tclass.defaultfont())
419 * 4 / (par.getDepth() + 4);
423 int LyXText::labelEnd(ParagraphList::iterator pit) const
425 // labelEnd is only needed if the layout fills a flushleft label.
426 if (pit->layout()->margintype != MARGIN_MANUAL)
428 // return the beginning of the body
429 return leftMargin(pit);
435 // this needs special handling - only newlines count as a break point
436 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
438 pos_type const end = par.size();
441 if (par.isNewline(i))
450 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
452 pos_type const end = pit->size();
453 pos_type const pos = row.pos();
459 // maximum pixel width of a row
460 int width = textWidth() - rightMargin(*pit); // - leftMargin(pit, row);
466 LyXLayout_ptr const & layout = pit->layout();
468 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
469 row.endpos(addressBreakPoint(pos, *pit));
473 pos_type const body_pos = pit->beginOfBody();
476 // Now we iterate through until we reach the right margin
477 // or the end of the par, then choose the possible break
480 int const left = leftMargin(pit, pos);
483 // pixel width since last breakpoint
486 FontIterator fi = FontIterator(*this, pit, pos);
487 pos_type point = end;
489 for ( ; i < end; ++i, ++fi) {
490 if (pit->isNewline(i)) {
496 if (pit->isInset(i + 1) && pit->getInset(i + 1)->display()) {
501 if (pit->isInset(i) && pit->getInset(i)->display()) {
507 char const c = pit->getChar(i);
510 int thiswidth = singleWidth(pit, i, c, *fi);
512 // add the auto-hfill from label end to the body
513 if (body_pos && i == body_pos) {
514 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
515 if (pit->isLineSeparator(i - 1))
516 add -= singleWidth(pit, i - 1);
518 add = std::max(add, labelEnd(pit) - x);
523 chunkwidth += thiswidth;
526 // break before a character that will fall off
527 // the right of the row
529 // if no break before, break here
530 if (point == end || chunkwidth >= width - left) {
537 // exit on last registered breakpoint:
541 if (!pit->isInset(i) || pit->getInset(i)->isChar()) {
542 // some insets are line separators too
543 if (pit->isLineSeparator(i)) {
544 // register breakpoint:
551 if (i == end && x < width) {
552 // maybe found one, but the par is short enough.
556 // manual labels cannot be broken in LaTeX. But we
557 // want to make our on-screen rendering of footnotes
559 if (body_pos && point < body_pos)
566 void LyXText::setRowWidth(ParagraphList::iterator pit, Row & row) const
568 // get the pure distance
569 pos_type const end = row.endpos();
571 string labelsep = pit->layout()->labelsep;
572 int w = leftMargin(pit, row.pos());
574 pos_type const body_pos = pit->beginOfBody();
575 pos_type i = row.pos();
578 FontIterator fi = FontIterator(*this, pit, i);
579 for ( ; i < end; ++i, ++fi) {
580 if (body_pos > 0 && i == body_pos) {
581 w += font_metrics::width(labelsep, getLabelFont(pit));
582 if (pit->isLineSeparator(i - 1))
583 w -= singleWidth(pit, i - 1);
584 w = max(w, labelEnd(pit));
586 char const c = pit->getChar(i);
587 w += singleWidth(pit, i, c, *fi);
591 if (body_pos > 0 && body_pos >= end) {
592 w += font_metrics::width(labelsep, getLabelFont(pit));
593 if (end > 0 && pit->isLineSeparator(end - 1))
594 w -= singleWidth(pit, end - 1);
595 w = max(w, labelEnd(pit));
598 row.width(w + rightMargin(*pit));
602 // returns the minimum space a manual label needs on the screen in pixel
603 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
605 pos_type last = pit->beginOfBody();
607 BOOST_ASSERT(last > 0);
609 // -1 because a label ends with a space that is in the label
612 // a separator at this end does not count
613 if (pit->isLineSeparator(last))
617 for (pos_type i = row.pos(); i <= last; ++i)
618 w += singleWidth(pit, i);
620 string const & label = pit->params().labelWidthString();
624 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
628 LColor_color LyXText::backgroundColor() const
630 return LColor_color(LColor::color(background_color_));
634 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
636 // get the maximum ascent and the maximum descent
637 double layoutasc = 0;
638 double layoutdesc = 0;
639 double const dh = defaultRowHeight();
641 // ok, let us initialize the maxasc and maxdesc value.
642 // Only the fontsize count. The other properties
643 // are taken from the layoutfont. Nicer on the screen :)
644 LyXLayout_ptr const & layout = pit->layout();
646 // as max get the first character of this row then it can
647 // increase but not decrease the height. Just some point to
648 // start with so we don't have to do the assignment below too
650 LyXFont font = getFont(pit, row.pos());
651 LyXFont::FONT_SIZE const tmpsize = font.size();
652 font = getLayoutFont(pit);
653 LyXFont::FONT_SIZE const size = font.size();
654 font.setSize(tmpsize);
656 LyXFont labelfont = getLabelFont(pit);
658 // these are minimum values
659 double const spacing_val = layout->spacing.getValue() * spacing(*pit);
660 //lyxerr << "spacing_val = " << spacing_val << endl;
661 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
662 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
664 // insets may be taller
665 InsetList::iterator ii = pit->insetlist.begin();
666 InsetList::iterator iend = pit->insetlist.end();
667 for ( ; ii != iend; ++ii) {
668 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
669 maxasc = max(maxasc, ii->inset->ascent());
670 maxdesc = max(maxdesc, ii->inset->descent());
674 // Check if any custom fonts are larger (Asger)
675 // This is not completely correct, but we can live with the small,
676 // cosmetic error for now.
678 pos_type const pos_end = row.endpos();
680 LyXFont::FONT_SIZE maxsize =
681 pit->highestFontInRange(row.pos(), pos_end, size);
682 if (maxsize > font.size()) {
683 font.setSize(maxsize);
684 maxasc = max(maxasc, font_metrics::maxAscent(font));
685 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
688 // This is nicer with box insets:
692 row.ascent_of_text(maxasc);
695 if (row.pos() == 0) {
696 BufferParams const & bufparams = bv()->buffer()->params();
697 // some parksips VERY EASY IMPLEMENTATION
698 if (bv()->buffer()->params().paragraph_separation
699 == BufferParams::PARSEP_SKIP
700 && pit != paragraphs().begin()
701 && ((layout->isParagraph() && pit->getDepth() == 0)
702 || (boost::prior(pit)->layout()->isParagraph()
703 && boost::prior(pit)->getDepth() == 0)))
705 maxasc += bufparams.getDefSkip().inPixels(*bv());
708 if (pit->params().startOfAppendix())
709 maxasc += int(3 * dh);
711 // This is special code for the chapter, since the label of this
712 // layout is printed in an extra row
713 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
714 labeladdon = int(font_metrics::maxHeight(labelfont)
715 * layout->spacing.getValue() * spacing(*pit));
718 // special code for the top label
719 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
720 || layout->labeltype == LABEL_BIBLIO
721 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
722 && isFirstInSequence(pit, paragraphs())
723 && !pit->getLabelstring().empty())
726 font_metrics::maxHeight(labelfont)
727 * layout->spacing.getValue()
729 + (layout->topsep + layout->labelbottomsep) * dh);
732 // Add the layout spaces, for example before and after
733 // a section, or between the items of a itemize or enumerate
736 ParagraphList::iterator prev =
737 depthHook(pit, paragraphs(), pit->getDepth());
739 && prev->layout() == layout
740 && prev->getDepth() == pit->getDepth()
741 && prev->getLabelWidthString() == pit->getLabelWidthString())
743 layoutasc = layout->itemsep * dh;
744 } else if (pit != paragraphs().begin() || row.pos() != 0) {
745 if (layout->topsep > 0)
746 layoutasc = layout->topsep * dh;
749 prev = outerHook(pit, paragraphs());
750 if (prev != paragraphs().end()) {
751 maxasc += int(prev->layout()->parsep * dh);
752 } else if (pit != paragraphs().begin()) {
753 ParagraphList::iterator prior_pit = boost::prior(pit);
754 if (prior_pit->getDepth() != 0 ||
755 prior_pit->layout() == layout) {
756 maxasc += int(layout->parsep * dh);
761 // is it a bottom line?
762 if (row.endpos() >= pit->size()) {
763 // add the layout spaces, for example before and after
764 // a section, or between the items of a itemize or enumerate
766 ParagraphList::iterator nextpit = boost::next(pit);
767 if (nextpit != paragraphs().end()) {
768 ParagraphList::iterator cpit = pit;
772 if (cpit->getDepth() > nextpit->getDepth()) {
773 usual = cpit->layout()->bottomsep * dh;
774 cpit = depthHook(cpit, paragraphs(), nextpit->getDepth());
775 if (cpit->layout() != nextpit->layout()
776 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
778 unusual = cpit->layout()->bottomsep * dh;
780 layoutdesc = max(unusual, usual);
781 } else if (cpit->getDepth() == nextpit->getDepth()) {
782 if (cpit->layout() != nextpit->layout()
783 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
784 layoutdesc = int(cpit->layout()->bottomsep * dh);
789 // incalculate the layout spaces
790 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
791 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
793 row.height(maxasc + maxdesc + labeladdon);
794 row.baseline(maxasc + labeladdon);
795 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
799 void LyXText::breakParagraph(LCursor & cur, char keep_layout)
801 BOOST_ASSERT(this == cur.text());
802 // allow only if at start or end, or all previous is new text
803 Paragraph & cpar = cur.paragraph();
804 ParagraphList::iterator cpit = getPar(cur.par());
806 if (cur.pos() != 0 && cur.pos() != cur.lastpos()
807 && cpar.isChangeEdited(0, cur.pos()))
810 LyXTextClass const & tclass =
811 bv()->buffer()->params().getLyXTextClass();
812 LyXLayout_ptr const & layout = cpar.layout();
814 // this is only allowed, if the current paragraph is not empty
815 // or caption and if it has not the keepempty flag active
816 if (cur.lastpos() == 0 && !cpar.allowEmpty()
817 && layout->labeltype != LABEL_SENSITIVE)
820 // a layout change may affect also the following paragraph
821 recUndo(cur.par(), parOffset(undoSpan(cpit)) - 1);
823 // Always break behind a space
824 // It is better to erase the space (Dekel)
825 if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
826 cpar.erase(cur.pos());
828 // break the paragraph
832 keep_layout = layout->isEnvironment();
834 // we need to set this before we insert the paragraph. IMO the
835 // breakParagraph call should return a bool if it inserts the
836 // paragraph before or behind and we should react on that one
837 // but we can fix this in 1.3.0 (Jug 20020509)
838 bool const isempty = cpar.allowEmpty() && cpar.empty();
839 ::breakParagraph(bv()->buffer()->params(), paragraphs(), cpit,
840 cur.pos(), keep_layout);
842 cpit = getPar(cur.par());
843 ParagraphList::iterator next_par = boost::next(cpit);
845 // well this is the caption hack since one caption is really enough
846 if (layout->labeltype == LABEL_SENSITIVE) {
848 // set to standard-layout
849 cpit->applyLayout(tclass.defaultLayout());
851 // set to standard-layout
852 next_par->applyLayout(tclass.defaultLayout());
855 // if the cursor is at the beginning of a row without prior newline,
857 // This touches only the screen-update. Otherwise we would may have
858 // an empty row on the screen
859 if (cur.pos() != 0 && cur.textRow().pos() == cur.pos()
860 && !cpit->isNewline(cur.pos() - 1))
865 while (!next_par->empty() && next_par->isNewline(0))
870 redoParagraph(next_par);
872 // This check is necessary. Otherwise the new empty paragraph will
873 // be deleted automatically. And it is more friendly for the user!
874 if (cur.pos() != 0 || isempty)
875 setCursor(cur, cur.par() + 1, 0);
877 setCursor(cur, cur.par(), 0);
881 // convenience function
882 void LyXText::redoParagraph(LCursor & cur)
884 BOOST_ASSERT(this == cur.text());
885 cur.clearSelection();
886 redoParagraph(getPar(cur.par()));
887 setCursorIntern(cur, cur.par(), cur.pos());
891 // insert a character, moves all the following breaks in the
892 // same Paragraph one to the right and make a rebreak
893 void LyXText::insertChar(LCursor & cur, char c)
895 BOOST_ASSERT(this == cur.text());
896 recordUndo(cur, Undo::INSERT);
898 Paragraph & par = cur.paragraph();
899 // try to remove this
900 ParagraphList::iterator pit = getPar(cur.par());
902 bool const freeSpacing = par.layout()->free_spacing ||
905 if (lyxrc.auto_number) {
906 static string const number_operators = "+-/*";
907 static string const number_unary_operators = "+-";
908 static string const number_seperators = ".,:";
910 if (current_font.number() == LyXFont::ON) {
911 if (!IsDigit(c) && !contains(number_operators, c) &&
912 !(contains(number_seperators, c) &&
914 cur.pos() != cur.lastpos() &&
915 getFont(pit, cur.pos()).number() == LyXFont::ON &&
916 getFont(pit, cur.pos() - 1).number() == LyXFont::ON)
918 number(cur); // Set current_font.number to OFF
919 } else if (IsDigit(c) &&
920 real_current_font.isVisibleRightToLeft()) {
921 number(cur); // Set current_font.number to ON
923 if (cur.pos() != 0) {
924 char const c = par.getChar(cur.pos() - 1);
925 if (contains(number_unary_operators, c) &&
927 || par.isSeparator(cur.pos() - 2)
928 || par.isNewline(cur.pos() - 2))
930 setCharFont(pit, cur.pos() - 1, current_font);
931 } else if (contains(number_seperators, c)
933 && getFont(pit, cur.pos() - 2).number() == LyXFont::ON) {
934 setCharFont(pit, cur.pos() - 1, current_font);
940 // First check, if there will be two blanks together or a blank at
941 // the beginning of a paragraph.
942 // I decided to handle blanks like normal characters, the main
943 // difference are the special checks when calculating the row.fill
944 // (blank does not count at the end of a row) and the check here
946 // The bug is triggered when we type in a description environment:
947 // The current_font is not changed when we go from label to main text
948 // and it should (along with realtmpfont) when we type the space.
949 // CHECK There is a bug here! (Asger)
951 // store the current font. This is because of the use of cursor
952 // movements. The moving cursor would refresh the current font
953 LyXFont realtmpfont = real_current_font;
954 LyXFont rawtmpfont = current_font;
956 // When the free-spacing option is set for the current layout,
957 // disable the double-space checking
958 if (!freeSpacing && IsLineSeparatorChar(c)) {
959 if (cur.pos() == 0) {
960 static bool sent_space_message = false;
961 if (!sent_space_message) {
962 cur.message(_("You cannot insert a space at the "
963 "beginning of a paragraph. Please read the Tutorial."));
964 sent_space_message = true;
968 BOOST_ASSERT(cur.pos() > 0);
969 if (par.isLineSeparator(cur.pos() - 1)
970 || par.isNewline(cur.pos() - 1)) {
971 static bool sent_space_message = false;
972 if (!sent_space_message) {
973 cur.message(_("You cannot type two spaces this way. "
974 "Please read the Tutorial."));
975 sent_space_message = true;
981 // Here case LyXText::InsertInset already inserted the character
982 if (c != Paragraph::META_INSET)
983 par.insertChar(cur.pos(), c);
985 setCharFont(pit, cur.pos(), rawtmpfont);
987 current_font = rawtmpfont;
988 real_current_font = realtmpfont;
990 setCursor(cur, cur.par(), cur.pos() + 1, false, cur.boundary());
995 void LyXText::charInserted()
997 // Here we call finishUndo for every 20 characters inserted.
998 // This is from my experience how emacs does it. (Lgb)
999 static unsigned int counter;
1010 LyXText::prepareToPrint(ParagraphList::iterator pit, Row const & row) const
1014 double w = width - row.width();
1016 bool const is_rtl = isRTL(*pit);
1018 result.x = rightMargin(*pit);
1020 result.x = leftMargin(pit, row.pos());
1022 // is there a manual margin with a manual label
1023 LyXLayout_ptr const & layout = pit->layout();
1025 if (layout->margintype == MARGIN_MANUAL
1026 && layout->labeltype == LABEL_MANUAL) {
1027 /// We might have real hfills in the label part
1028 int nlh = numberOfLabelHfills(*pit, row);
1030 // A manual label par (e.g. List) has an auto-hfill
1031 // between the label text and the body of the
1033 // But we don't want to do this auto hfill if the par
1038 if (nlh && !pit->getLabelWidthString().empty())
1039 result.label_hfill = labelFill(pit, row) / double(nlh);
1042 // are there any hfills in the row?
1043 int const nh = numberOfHfills(*pit, row);
1047 result.hfill = w / nh;
1048 // we don't have to look at the alignment if it is ALIGN_LEFT and
1049 // if the row is already larger then the permitted width as then
1050 // we force the LEFT_ALIGN'edness!
1051 } else if (int(row.width()) < textWidth()) {
1052 // is it block, flushleft or flushright?
1053 // set x how you need it
1055 if (pit->params().align() == LYX_ALIGN_LAYOUT)
1056 align = layout->align;
1058 align = pit->params().align();
1060 // Display-style insets should always be on a centred row
1061 // The test on pit->size() is to catch zero-size pars, which
1062 // would trigger the assert in Paragraph::getInset().
1063 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1065 && pit->isInset(row.pos())
1066 && pit->getInset(row.pos())->display())
1068 align = LYX_ALIGN_CENTER;
1072 case LYX_ALIGN_BLOCK: {
1073 int const ns = numberOfSeparators(*pit, row);
1074 bool disp_inset = false;
1075 if (row.endpos() < pit->size()) {
1076 InsetBase * in = pit->getInset(row.endpos());
1078 disp_inset = in->display();
1080 // If we have separators, this is not the last row of a
1081 // par, does not end in newline, and is not row above a
1082 // display inset... then stretch it
1084 && row.endpos() < pit->size()
1085 && !pit->isNewline(row.endpos() - 1)
1088 result.separator = w / ns;
1089 } else if (is_rtl) {
1094 case LYX_ALIGN_RIGHT:
1097 case LYX_ALIGN_CENTER:
1103 bidi.computeTables(*pit, *bv()->buffer(), row);
1105 pos_type body_pos = pit->beginOfBody();
1106 pos_type end = row.endpos();
1109 && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1111 result.x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1112 if (body_pos <= end)
1113 result.x += result.label_hfill;
1121 // the cursor set functions have a special mechanism. When they
1122 // realize, that you left an empty paragraph, they will delete it.
1124 void LyXText::cursorRightOneWord(LCursor & cur)
1126 BOOST_ASSERT(this == cur.text());
1127 if (cur.pos() == cur.lastpos() && cur.par() != cur.lastpar()) {
1131 // Skip through initial nonword stuff.
1132 // Treat floats and insets as words.
1133 while (cur.pos() != cur.lastpos() && !cur.paragraph().isWord(cur.pos()))
1135 // Advance through word.
1136 while (cur.pos() != cur.lastpos() && cur.paragraph().isWord(cur.pos()))
1139 setCursor(cur, cur.par(), cur.pos());
1143 void LyXText::cursorLeftOneWord(LCursor & cur)
1145 BOOST_ASSERT(this == cur.text());
1146 if (cur.pos() == 0 && cur.par() != 0) {
1148 cur.pos() = cur.lastpos();
1150 // Skip through initial nonword stuff.
1151 // Treat floats and insets as words.
1152 while (cur.pos() != 0 && !cur.paragraph().isWord(cur.pos() - 1))
1154 // Advance through word.
1155 while (cur.pos() != 0 && cur.paragraph().isWord(cur.pos() - 1))
1158 setCursor(cur, cur.par(), cur.pos());
1162 void LyXText::selectWord(LCursor & cur, word_location loc)
1164 BOOST_ASSERT(this == cur.text());
1165 CursorSlice from = cur.current();
1166 CursorSlice to = cur.current();
1167 getWord(from, to, loc);
1168 if (cur.current() != from)
1169 setCursor(cur, from.par(), from.pos());
1173 setCursor(cur, to.par(), to.pos());
1178 // Select the word currently under the cursor when no
1179 // selection is currently set
1180 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1182 BOOST_ASSERT(this == cur.text());
1183 if (cur.selection())
1185 selectWord(cur, loc);
1186 return cur.selection();
1190 void LyXText::acceptChange(LCursor & cur)
1192 BOOST_ASSERT(this == cur.text());
1193 if (!cur.selection() && cur.lastpos() != 0)
1196 CursorSlice const & startc = cur.selBegin();
1197 CursorSlice const & endc = cur.selEnd();
1198 if (startc.par() == endc.par()) {
1199 recordUndoSelection(cur, Undo::INSERT);
1200 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1202 cur.clearSelection();
1203 redoParagraph(getPar(startc));
1204 setCursorIntern(cur, startc.par(), 0);
1206 #warning handle multi par selection
1210 void LyXText::rejectChange(LCursor & cur)
1212 BOOST_ASSERT(this == cur.text());
1213 if (!cur.selection() && cur.lastpos() != 0)
1216 CursorSlice const & startc = cur.selBegin();
1217 CursorSlice const & endc = cur.selEnd();
1218 if (startc.par() == endc.par()) {
1219 recordUndoSelection(cur, Undo::INSERT);
1220 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1222 cur.clearSelection();
1223 redoParagraph(getPar(startc));
1224 setCursorIntern(cur, startc.par(), 0);
1226 #warning handle multi par selection
1230 // Delete from cursor up to the end of the current or next word.
1231 void LyXText::deleteWordForward(LCursor & cur)
1233 BOOST_ASSERT(this == cur.text());
1234 if (cur.lastpos() == 0)
1238 cur.selection() = true;
1239 cursorRightOneWord(cur);
1241 cutSelection(cur, true, false);
1246 // Delete from cursor to start of current or prior word.
1247 void LyXText::deleteWordBackward(LCursor & cur)
1249 BOOST_ASSERT(this == cur.text());
1250 if (cur.lastpos() == 0)
1254 cur.selection() = true;
1255 cursorLeftOneWord(cur);
1257 cutSelection(cur, true, false);
1262 // Kill to end of line.
1263 void LyXText::deleteLineForward(LCursor & cur)
1265 BOOST_ASSERT(this == cur.text());
1266 if (cur.lastpos() == 0) {
1267 // Paragraph is empty, so we just go to the right
1271 cur.selection() = true; // to avoid deletion
1274 // What is this test for ??? (JMarc)
1275 if (!cur.selection())
1276 deleteWordForward(cur);
1278 cutSelection(cur, true, false);
1283 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1285 BOOST_ASSERT(this == cur.text());
1289 if (cur.selection()) {
1290 from = cur.selBegin();
1294 getWord(from, to, lyx::PARTIAL_WORD);
1295 setCursor(cur, to.par(), to.pos() + 1);
1298 recordUndoSelection(cur);
1300 pos_type pos = from.pos();
1301 int par = from.par();
1303 while (par != int(paragraphs().size()) &&
1304 (pos != to.pos() || par != to.par())) {
1305 ParagraphList::iterator pit = getPar(par);
1306 if (pos == pit->size()) {
1311 unsigned char c = pit->getChar(pos);
1312 if (c != Paragraph::META_INSET) {
1314 case text_lowercase:
1317 case text_capitalization:
1319 action = text_lowercase;
1321 case text_uppercase:
1327 pit->setChar(pos, c);
1333 void LyXText::Delete(LCursor & cur)
1335 BOOST_ASSERT(this == cur.text());
1336 // just move to the right, if we had success make a backspace
1337 CursorSlice sl = cur.current();
1339 if (sl == cur.current()) {
1340 recordUndo(cur, Undo::DELETE, cur.par(), max(0, cur.par() - 1));
1346 void LyXText::backspace(LCursor & cur)
1348 BOOST_ASSERT(this == cur.text());
1349 if (cur.pos() == 0) {
1350 // The cursor is at the beginning of a paragraph, so
1351 // the the backspace will collapse two paragraphs into
1354 // but it's not allowed unless it's new
1355 Paragraph & par = cur.paragraph();
1356 if (par.isChangeEdited(0, par.size()))
1359 // we may paste some paragraphs
1361 // is it an empty paragraph?
1362 pos_type lastpos = cur.lastpos();
1363 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1364 // This is an empty paragraph and we delete it just
1365 // by moving the cursor one step
1366 // left and let the DeleteEmptyParagraphMechanism
1367 // handle the actual deletion of the paragraph.
1369 if (cur.par() != 0) {
1371 // the layout things can change the height of a row !
1378 recordUndo(cur, Undo::DELETE, cur.par() - 1);
1380 ParagraphList::iterator tmppit = getPar(cur.par());
1381 // We used to do cursorLeftIntern() here, but it is
1382 // not a good idea since it triggers the auto-delete
1383 // mechanism. So we do a cursorLeftIntern()-lite,
1384 // without the dreaded mechanism. (JMarc)
1385 if (cur.par() != 0) {
1386 // steps into the above paragraph.
1387 setCursorIntern(cur, cur.par() - 1,
1388 getPar(cur.par() - 1)->size(),
1392 // Pasting is not allowed, if the paragraphs have different
1393 // layout. I think it is a real bug of all other
1394 // word processors to allow it. It confuses the user.
1395 // Correction: Pasting is always allowed with standard-layout
1396 Buffer & buf = *bv()->buffer();
1397 BufferParams const & bufparams = buf.params();
1398 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1399 ParagraphList::iterator const cpit = getPar(cur.par());
1402 && (cpit->layout() == tmppit->layout()
1403 || tmppit->layout() == tclass.defaultLayout())
1404 && cpit->getAlign() == tmppit->getAlign()) {
1405 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1407 if (cur.pos() != 0 && cpit->isSeparator(cur.pos() - 1))
1410 // the counters may have changed
1412 setCursor(cur, cur.par(), cur.pos(), false);
1415 // this is the code for a normal backspace, not pasting
1417 recordUndo(cur, Undo::DELETE);
1418 // We used to do cursorLeftIntern() here, but it is
1419 // not a good idea since it triggers the auto-delete
1420 // mechanism. So we do a cursorLeftIntern()-lite,
1421 // without the dreaded mechanism. (JMarc)
1422 setCursorIntern(cur, cur.par(), cur.pos() - 1,
1423 false, cur.boundary());
1424 cur.paragraph().erase(cur.pos());
1427 if (cur.pos() == cur.lastpos())
1428 setCurrentFont(cur);
1431 setCursor(cur, cur.par(), cur.pos(), false, cur.boundary());
1435 ParagraphList::iterator LyXText::cursorPar() const
1437 //lyxerr << "### cursorPar: cursor: " << bv()->cursor() << endl;
1438 //lyxerr << "xxx cursorPar: cursor: " << cursor() << endl;
1439 return getPar(cursor().par());
1443 ParagraphList::iterator LyXText::getPar(CursorSlice const & cur) const
1445 return getPar(cur.par());
1449 ParagraphList::iterator LyXText::getPar(int par) const
1451 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1452 BOOST_ASSERT(par >= 0);
1453 BOOST_ASSERT(par < int(paragraphs().size()));
1454 ParagraphList::iterator pit = paragraphs().begin();
1460 // y is relative to this LyXText's top
1462 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1464 BOOST_ASSERT(!paragraphs().empty());
1465 BOOST_ASSERT(!paragraphs().begin()->rows.empty());
1467 ParagraphList::iterator const
1468 pend = boost::prior(paragraphs().end());
1469 pit = paragraphs().begin();
1470 while (int(pit->y + pit->height) < y && pit != pend)
1473 RowList::iterator rit = pit->rows.end();
1474 RowList::iterator const rbegin = pit->rows.begin();
1477 } while (rit != rbegin && int(pit->y + rit->y_offset()) > y);
1481 pit = boost::prior(paragraphs().end());
1483 RowList::iterator rit = lastRow();
1484 RowList::iterator rbegin = firstRow();
1486 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1487 previousRow(pit, rit);
1494 int LyXText::getDepth() const
1496 return cursorPar()->getDepth();
1500 RowList::iterator LyXText::firstRow() const
1502 return paragraphs().front().rows.begin();
1506 RowList::iterator LyXText::lastRow() const
1508 return boost::prior(endRow());
1512 RowList::iterator LyXText::endRow() const
1514 return paragraphs().back().rows.end();
1518 void LyXText::nextRow(ParagraphList::iterator & pit,
1519 RowList::iterator & rit) const
1522 if (rit == pit->rows.end()) {
1524 if (pit == paragraphs().end())
1527 rit = pit->rows.begin();
1532 void LyXText::previousRow(ParagraphList::iterator & pit,
1533 RowList::iterator & rit) const
1535 if (rit != pit->rows.begin())
1538 BOOST_ASSERT(pit != paragraphs().begin());
1540 rit = boost::prior(pit->rows.end());
1545 int LyXText::parOffset(ParagraphList::iterator pit) const
1547 return distance(paragraphs().begin(), pit);
1551 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1553 // remove rows of paragraph, keep track of height changes
1554 height -= pit->height;
1562 InsetList::iterator ii = pit->insetlist.begin();
1563 InsetList::iterator iend = pit->insetlist.end();
1564 for (; ii != iend; ++ii) {
1566 int const w = textWidth() - leftMargin(pit) - rightMargin(*pit);
1567 MetricsInfo mi(bv(), getFont(pit, ii->pos), w);
1568 ii->inset->metrics(mi, dim);
1571 // rebreak the paragraph
1572 pit->setBeginOfBody();
1576 rowBreakPoint(pit, row);
1577 setRowWidth(pit, row);
1578 setHeightOfRow(pit, row);
1579 row.y_offset(pit->height);
1580 pit->rows.push_back(row);
1581 pit->width = std::max(pit->width, row.width());
1582 pit->height += row.height();
1584 } while (z < pit->size());
1586 height += pit->height;
1587 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1591 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1592 ParagraphList::iterator end)
1594 for ( ; pit != end; ++pit)
1595 redoParagraphInternal(pit);
1596 updateParPositions();
1600 void LyXText::redoParagraph(ParagraphList::iterator pit)
1602 redoParagraphInternal(pit);
1603 updateParPositions();
1607 void LyXText::fullRebreak()
1609 redoParagraphs(paragraphs().begin(), paragraphs().end());
1610 bv()->cursor().resetAnchor();
1614 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1616 //BOOST_ASSERT(mi.base.textwidth);
1617 if (mi.base.textwidth)
1618 textwidth_ = mi.base.textwidth;
1619 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1620 // << " textWidth: " << textWidth() << "\nfont: " << mi.base.font << endl;
1622 // Rebuild row cache. This recomputes height as well.
1623 redoParagraphs(paragraphs().begin(), paragraphs().end());
1625 width = maxParagraphWidth(paragraphs());
1628 dim.asc = firstRow()->ascent_of_text();
1629 dim.des = height - dim.asc;
1634 // only used for inset right now. should also be used for main text
1635 void LyXText::draw(PainterInfo & pi, int x, int y) const
1639 paintTextInset(*this, pi, x, y);
1643 // only used for inset right now. should also be used for main text
1644 void LyXText::drawSelection(PainterInfo &, int, int) const
1646 //lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
1650 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1652 return row.endpos() >= pit->size()
1653 && boost::next(pit) == paragraphs().end();
1657 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1659 return row.pos() == 0 && pit == paragraphs().begin();
1663 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1664 word_location const loc)
1666 Paragraph & from_par = *getPar(from);
1668 case lyx::WHOLE_WORD_STRICT:
1669 if (from.pos() == 0 || from.pos() == from_par.size()
1670 || from_par.isSeparator(from.pos())
1671 || from_par.isKomma(from.pos())
1672 || from_par.isNewline(from.pos())
1673 || from_par.isSeparator(from.pos() - 1)
1674 || from_par.isKomma(from.pos() - 1)
1675 || from_par.isNewline(from.pos() - 1)) {
1679 // no break here, we go to the next
1681 case lyx::WHOLE_WORD:
1682 // Move cursor to the beginning, when not already there.
1683 if (from.pos() && !from_par.isSeparator(from.pos() - 1)
1684 && !(from_par.isKomma(from.pos() - 1)
1685 || from_par.isNewline(from.pos() - 1)))
1686 cursorLeftOneWord(bv()->cursor());
1688 case lyx::PREVIOUS_WORD:
1689 // always move the cursor to the beginning of previous word
1690 cursorLeftOneWord(bv()->cursor());
1692 case lyx::NEXT_WORD:
1693 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1696 case lyx::PARTIAL_WORD:
1700 Paragraph & to_par = *getPar(to);
1701 while (to.pos() < to_par.size()
1702 && !to_par.isSeparator(to.pos())
1703 && !to_par.isKomma(to.pos())
1704 && !to_par.isNewline(to.pos())
1705 && !to_par.isHfill(to.pos())
1706 && !to_par.isInset(to.pos()))
1713 void LyXText::write(Buffer const & buf, std::ostream & os) const
1715 ParagraphList::const_iterator pit = paragraphs().begin();
1716 ParagraphList::const_iterator end = paragraphs().end();
1717 Paragraph::depth_type dth = 0;
1718 for (; pit != end; ++pit)
1719 pit->write(buf, os, buf.params(), dth);
1723 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1725 static Change current_change;
1727 bool the_end_read = false;
1728 ParagraphList::iterator pit = paragraphs().begin();
1729 Paragraph::depth_type depth = 0;
1731 while (lex.isOK()) {
1733 string token = lex.getString();
1740 if (token == "\\end_inset") {
1741 the_end_read = true;
1745 if (token == "\\end_document") {
1746 lex.printError("\\end_document read in inset! Error in document!");
1752 if (token == "\\end_document") {
1753 the_end_read = true;
1762 if (token == "\\begin_layout") {
1763 lex.pushToken(token);
1766 par.params().depth(depth);
1767 if (buf.params().tracking_changes)
1769 LyXFont f(LyXFont::ALL_INHERIT, buf.params().language);
1773 if (pit != paragraphs().end())
1776 pit = paragraphs().insert(pit, par);
1778 // FIXME: goddamn InsetTabular makes us pass a Buffer
1780 ::readParagraph(buf, *pit, lex);
1782 } else if (token == "\\begin_deeper") {
1784 } else if (token == "\\end_deeper") {
1786 lex.printError("\\end_deeper: " "depth is already null");
1795 return the_end_read;
1799 int LyXText::ascent() const
1801 return firstRow()->ascent_of_text();
1805 int LyXText::descent() const
1807 return height - firstRow()->ascent_of_text();
1811 int LyXText::cursorX(CursorSlice const & cur) const
1813 ParagraphList::iterator pit = getPar(cur);
1814 if (pit->rows.empty())
1816 Row const & row = *pit->getRow(cur.pos());
1819 pos_type pos = cur.pos();
1820 pos_type cursor_vpos = 0;
1822 RowMetrics const m = prepareToPrint(pit, row);
1825 pos_type const row_pos = row.pos();
1826 pos_type const end = row.endpos();
1829 cursor_vpos = row_pos;
1830 else if (pos >= end)
1831 cursor_vpos = isRTL(*pit) ? row_pos : end;
1832 else if (pos > row_pos && pos >= end)
1833 // Place cursor after char at (logical) position pos - 1
1834 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1835 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1837 // Place cursor before char at (logical) position pos
1838 cursor_vpos = (bidi.level(pos) % 2 == 0)
1839 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1841 pos_type body_pos = pit->beginOfBody();
1843 (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1846 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1847 pos_type pos = bidi.vis2log(vpos);
1848 if (body_pos > 0 && pos == body_pos - 1) {
1850 + font_metrics::width(pit->layout()->labelsep,
1852 if (pit->isLineSeparator(body_pos - 1))
1853 x -= singleWidth(pit, body_pos - 1);
1856 if (hfillExpansion(*pit, row, pos)) {
1857 x += singleWidth(pit, pos);
1858 if (pos >= body_pos)
1862 } else if (pit->isSeparator(pos)) {
1863 x += singleWidth(pit, pos);
1864 if (pos >= body_pos)
1867 x += singleWidth(pit, pos);
1869 return xo_ + int(x);
1873 int LyXText::cursorY(CursorSlice const & cur) const
1875 Paragraph & par = *getPar(cur);
1876 Row & row = *par.getRow(cur.pos());
1877 return yo_ + par.y + row.y_offset() + row.baseline();
1881 CursorSlice & LyXText::cursor()
1883 //lyxerr << "# accessing slice " << findText(this) << endl;
1884 if (this != bv()->cursor().text()) {
1885 lyxerr << "cursor: " << bv()->cursor()
1886 << "\ntext: " << bv()->cursor().text()
1887 << "\nthis: " << this << endl;
1888 BOOST_ASSERT(false);
1890 return bv()->cursor().current();
1894 CursorSlice const & LyXText::cursor() const
1896 if (this != bv()->cursor().text()) {
1897 lyxerr << "cursor: " << bv()->cursor()
1898 << "\ntext: " << bv()->cursor().text()
1899 << "\nthis: " << this << endl;
1900 BOOST_ASSERT(false);
1902 return bv()->cursor().current();
1906 void LyXText::replaceSelection(LCursor & cur)
1908 BOOST_ASSERT(this == cur.text());
1909 if (cur.selection()) {
1910 cutSelection(cur, true, false);
1916 // Returns the current font and depth as a message.
1917 string LyXText::currentState(LCursor & cur)
1919 BOOST_ASSERT(this == cur.text());
1920 Buffer * buffer = bv()->buffer();
1921 Paragraph const & par = cur.paragraph();
1922 std::ostringstream os;
1924 bool const show_change = buffer->params().tracking_changes
1925 && cur.pos() != cur.lastpos()
1926 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
1929 Change change = par.lookupChangeFull(cur.pos());
1930 Author const & a = buffer->params().authors().get(change.author);
1931 os << _("Change: ") << a.name();
1932 if (!a.email().empty())
1933 os << " (" << a.email() << ")";
1934 if (change.changetime)
1935 os << _(" at ") << ctime(&change.changetime);
1939 // I think we should only show changes from the default
1941 LyXFont font = real_current_font;
1942 font.reduce(buffer->params().getLyXTextClass().defaultfont());
1944 // avoid _(...) re-entrance problem
1945 string const s = font.stateText(&buffer->params());
1946 os << bformat(_("Font: %1$s"), s);
1948 // os << bformat(_("Font: %1$s"), font.stateText(&buffer->params));
1950 // The paragraph depth
1951 int depth = getDepth();
1953 os << bformat(_(", Depth: %1$s"), tostr(depth));
1955 // The paragraph spacing, but only if different from
1957 Spacing const & spacing = par.params().spacing();
1958 if (!spacing.isDefault()) {
1959 os << _(", Spacing: ");
1960 switch (spacing.getSpace()) {
1961 case Spacing::Single:
1964 case Spacing::Onehalf:
1967 case Spacing::Double:
1970 case Spacing::Other:
1971 os << _("Other (") << spacing.getValue() << ')';
1973 case Spacing::Default:
1974 // should never happen, do nothing
1978 #ifdef DEVEL_VERSION
1979 os << _(", Paragraph: ") << par.id();
1980 os << _(", Position: ") << cur.pos();
1981 Row & row = cur.textRow();
1982 os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1983 os << _(", Inset: ");
1984 InsetOld * inset = par.inInset();
1986 os << inset << " owner: " << inset->owner();
1994 string LyXText::getPossibleLabel(LCursor & cur) const
1996 ParagraphList & plist = paragraphs();
1997 ParagraphList::iterator pit = getPar(cur.par());
1999 LyXLayout_ptr layout = pit->layout();
2001 if (layout->latextype == LATEX_PARAGRAPH && pit != plist.begin()) {
2002 ParagraphList::iterator pit2 = boost::prior(pit);
2004 LyXLayout_ptr const & layout2 = pit2->layout();
2006 if (layout2->latextype != LATEX_PARAGRAPH) {
2012 string text = layout->latexname().substr(0, 3);
2013 if (layout->latexname() == "theorem")
2014 text = "thm"; // Create a correct prefix for prettyref
2017 if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2020 string par_text = pit->asString(*cur.bv().buffer(), false);
2021 for (int i = 0; i < lyxrc.label_init_length; ++i) {
2022 if (par_text.empty())
2025 par_text = split(par_text, head, ' ');
2026 // Is it legal to use spaces in labels ?
2036 int LyXText::dist(int x, int y) const
2043 else if (x > xo_ + width)
2044 xx = x - xo_ - width;
2046 if (y < yo_ - ascent())
2047 yy = yo_ - ascent() - y;
2048 else if (y > yo_ + descent())
2049 yy = y - yo_ - descent();