3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
22 #include "bufferparams.h"
23 #include "BufferView.h"
26 #include "dispatchresult.h"
28 #include "funcrequest.h"
32 #include "lyxlength.h"
36 #include "lyxrow_funcs.h"
37 #include "metricsinfo.h"
38 #include "paragraph.h"
39 #include "paragraph_funcs.h"
40 #include "ParagraphParameters.h"
41 #include "rowpainter.h"
44 #include "WordLangTuple.h"
46 #include "frontends/font_metrics.h"
47 #include "frontends/LyXView.h"
49 #include "insets/insettext.h"
51 #include "support/lstrings.h"
52 #include "support/textutils.h"
55 using lyx::word_location;
57 using lyx::support::contains;
58 using lyx::support::lowercase;
59 using lyx::support::uppercase;
68 /// some space for drawing the 'nested' markers (in pixel)
69 extern int const NEST_MARGIN = 20;
70 /// margin for changebar
71 extern int const CHANGEBAR_MARGIN = 10;
73 extern int const RIGHT_MARGIN = 10;
78 unsigned int maxParagraphWidth(ParagraphList const & plist)
80 unsigned int width = 0;
81 ParagraphList::const_iterator pit = plist.begin();
82 ParagraphList::const_iterator end = plist.end();
83 for (; pit != end; ++pit)
84 width = std::max(width, pit->width);
91 BufferView * LyXText::bv()
93 BOOST_ASSERT(bv_owner != 0);
98 double LyXText::spacing(Paragraph const & par) const
100 if (par.params().spacing().isDefault())
101 return bv()->buffer()->params().spacing().getValue();
102 return par.params().spacing().getValue();
106 BufferView * LyXText::bv() const
108 BOOST_ASSERT(bv_owner != 0);
113 void LyXText::updateParPositions()
115 ParagraphList::iterator pit = paragraphs().begin();
116 ParagraphList::iterator end = paragraphs().end();
117 for (height = 0; pit != end; ++pit) {
119 height += pit->height;
124 int LyXText::textWidth() const
130 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
132 if (pos >= pit->size())
135 char const c = pit->getChar(pos);
136 return singleWidth(pit, pos, c, getFont(pit, pos));
140 int LyXText::singleWidth(ParagraphList::iterator pit,
141 pos_type pos, char c, LyXFont const & font) const
143 if (pos >= pit->size()) {
144 lyxerr << "in singleWidth(), pos: " << pos << endl;
149 // The most common case is handled first (Asger)
150 if (IsPrintable(c)) {
151 if (!font.language()->RightToLeft()) {
152 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
153 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
154 && font.language()->lang() == "arabic") {
155 if (Encodings::IsComposeChar_arabic(c))
158 c = pit->transformChar(c, pos);
159 } else if (font.language()->lang() == "hebrew" &&
160 Encodings::IsComposeChar_hebrew(c))
163 return font_metrics::width(c, font);
166 if (c == Paragraph::META_INSET)
167 return pit->getInset(pos)->width();
169 if (IsSeparatorChar(c))
171 return font_metrics::width(c, font);
175 int LyXText::leftMargin(ParagraphList::iterator pit) const
177 return leftMargin(pit, pit->size());
181 int LyXText::leftMargin(ParagraphList::iterator pit, pos_type pos) const
183 LyXTextClass const & tclass =
184 bv()->buffer()->params().getLyXTextClass();
185 LyXLayout_ptr const & layout = pit->layout();
187 string parindent = layout->parindent;
189 int x = NEST_MARGIN + CHANGEBAR_MARGIN;
191 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
193 // This is the way LyX handles LaTeX-Environments.
194 // I have had this idea very late, so it seems to be a
195 // later added hack and this is true
196 if (pit->getDepth() == 0) {
197 if (pit->layout() == tclass.defaultLayout()) {
198 // find the previous same level paragraph
199 if (pit != paragraphs().begin()) {
200 ParagraphList::iterator newpit =
201 depthHook(pit, paragraphs(), pit->getDepth());
202 if (newpit == pit && newpit->layout()->nextnoindent)
207 // find the next level paragraph
208 ParagraphList::iterator newpar =
209 outerHook(pit, paragraphs());
211 // Make a corresponding row. Need to call leftMargin()
212 // to check whether it is a sufficent paragraph.
213 if (newpar != paragraphs().end()
214 && newpar->layout()->isEnvironment()) {
215 x = leftMargin(newpar);
218 if (newpar != paragraphs().end()
219 && pit->layout() == tclass.defaultLayout()) {
220 if (newpar->params().noindent())
223 parindent = newpar->layout()->parindent;
227 LyXFont const labelfont = getLabelFont(pit);
228 switch (layout->margintype) {
230 if (!layout->leftmargin.empty())
231 x += font_metrics::signedWidth(layout->leftmargin,
232 tclass.defaultfont());
233 if (!pit->getLabelstring().empty()) {
234 x += font_metrics::signedWidth(layout->labelindent,
236 x += font_metrics::width(pit->getLabelstring(),
238 x += font_metrics::width(layout->labelsep, labelfont);
243 x += font_metrics::signedWidth(layout->labelindent, labelfont);
244 // The width of an empty par, even with manual label, should be 0
245 if (!pit->empty() && pos >= pit->beginOfBody()) {
246 if (!pit->getLabelWidthString().empty()) {
247 x += font_metrics::width(pit->getLabelWidthString(),
249 x += font_metrics::width(layout->labelsep, labelfont);
255 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
256 / (pit->getDepth() + 4);
259 case MARGIN_FIRST_DYNAMIC:
260 if (layout->labeltype == LABEL_MANUAL) {
261 if (pos >= pit->beginOfBody()) {
262 x += font_metrics::signedWidth(layout->leftmargin,
265 x += font_metrics::signedWidth(layout->labelindent,
269 // Special case to fix problems with
271 || (layout->labeltype == LABEL_STATIC
272 && layout->latextype == LATEX_ENVIRONMENT
273 && !isFirstInSequence(pit, paragraphs()))) {
274 x += font_metrics::signedWidth(layout->leftmargin,
276 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
277 && layout->labeltype != LABEL_BIBLIO
278 && layout->labeltype !=
279 LABEL_CENTERED_TOP_ENVIRONMENT) {
280 x += font_metrics::signedWidth(layout->labelindent,
282 x += font_metrics::width(layout->labelsep, labelfont);
283 x += font_metrics::width(pit->getLabelstring(),
288 case MARGIN_RIGHT_ADDRESS_BOX: {
290 // ok, a terrible hack. The left margin depends on the widest
291 // row in this paragraph.
292 RowList::iterator rit = pit->rows.begin();
293 RowList::iterator end = pit->rows.end();
294 #warning This is wrong.
295 int minfill = textwidth_;
296 for ( ; rit != end; ++rit)
297 if (rit->fill() < minfill)
298 minfill = rit->fill();
299 x += font_metrics::signedWidth(layout->leftmargin,
300 tclass.defaultfont());
303 // also wrong, but much shorter.
309 if (!pit->params().leftIndent().zero())
310 x += pit->params().leftIndent().inPixels(textWidth());
314 if (pit->params().align() == LYX_ALIGN_LAYOUT)
315 align = layout->align;
317 align = pit->params().align();
319 // set the correct parindent
321 && (layout->labeltype == LABEL_NO_LABEL
322 || layout->labeltype == LABEL_TOP_ENVIRONMENT
323 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
324 || (layout->labeltype == LABEL_STATIC
325 && layout->latextype == LATEX_ENVIRONMENT
326 && !isFirstInSequence(pit, paragraphs())))
327 && align == LYX_ALIGN_BLOCK
328 && !pit->params().noindent()
329 // in tabulars and ert paragraphs are never indented!
331 || !pit->inInset()->owner()
332 || (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE
333 && pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
334 && (pit->layout() != tclass.defaultLayout()
335 || bv()->buffer()->params().paragraph_separation ==
336 BufferParams::PARSEP_INDENT))
338 x += font_metrics::signedWidth(parindent, tclass.defaultfont());
345 int LyXText::rightMargin(Paragraph const & par) const
347 LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
351 + font_metrics::signedWidth(tclass.rightmargin(),
352 tclass.defaultfont())
353 + font_metrics::signedWidth(par.layout()->rightmargin,
354 tclass.defaultfont())
355 * 4 / (par.getDepth() + 4);
359 int LyXText::labelEnd(ParagraphList::iterator pit) const
361 // labelEnd is only needed if the layout fills a flushleft label.
362 if (pit->layout()->margintype != MARGIN_MANUAL)
364 // return the beginning of the body
365 return leftMargin(pit);
371 // this needs special handling - only newlines count as a break point
372 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
374 pos_type const end = par.size();
377 if (par.isNewline(i))
386 void LyXText::rowBreakPoint(ParagraphList::iterator pit, Row & row) const
388 pos_type const end = pit->size();
389 pos_type const pos = row.pos();
395 // maximum pixel width of a row
396 int width = textWidth() - rightMargin(*pit); // - leftMargin(pit, row);
402 LyXLayout_ptr const & layout = pit->layout();
404 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
405 row.endpos(addressBreakPoint(pos, *pit));
409 pos_type const body_pos = pit->beginOfBody();
412 // Now we iterate through until we reach the right margin
413 // or the end of the par, then choose the possible break
416 int const left = leftMargin(pit, pos);
419 // pixel width since last breakpoint
423 // We re-use the font resolution for the entire font span when possible
424 LyXFont font = getFont(pit, pos);
425 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(pos);
427 pos_type point = end;
429 for ( ; i < end; ++i) {
430 if (pit->isNewline(i)) {
436 if (pit->isInset(i + 1) && pit->getInset(i + 1)->display()) {
441 if (pit->isInset(i) && pit->getInset(i)->display()) {
447 char const c = pit->getChar(i);
449 if (i > endPosOfFontSpan) {
450 font = getFont(pit, i);
451 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
455 int thiswidth = singleWidth(pit, i, c, font);
457 // add the auto-hfill from label end to the body
458 if (body_pos && i == body_pos) {
459 int add = font_metrics::width(layout->labelsep, getLabelFont(pit));
460 if (pit->isLineSeparator(i - 1))
461 add -= singleWidth(pit, i - 1);
463 add = std::max(add, labelEnd(pit) - x);
468 //lyxerr << "i: " << i << " x: "
469 //<< x << " width: " << width << endl;
470 chunkwidth += thiswidth;
473 // break before a character that will fall off
474 // the right of the row
476 // if no break before, break here
477 if (point == end || chunkwidth >= width - left) {
483 // exit on last registered breakpoint:
487 if (!pit->isInset(i) || pit->getInset(i)->isChar()) {
488 // some insets are line separators too
489 if (pit->isLineSeparator(i)) {
490 // register breakpoint:
497 if (i == end && x < width) {
498 // maybe found one, but the par is short enough.
502 // manual labels cannot be broken in LaTeX. But we
503 // want to make our on-screen rendering of footnotes
505 if (body_pos && point < body_pos)
512 // returns the minimum space a row needs on the screen in pixel
513 void LyXText::fill(ParagraphList::iterator pit, Row & row, int workwidth) const
515 // get the pure distance
516 pos_type const end = row.endpos();
518 string labelsep = pit->layout()->labelsep;
519 int w = leftMargin(pit, row.pos());
521 pos_type const body_pos = pit->beginOfBody();
522 pos_type i = row.pos();
525 // We re-use the font resolution for the entire span when possible
526 LyXFont font = getFont(pit, i);
527 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
528 for ( ; i < end; ++i) {
529 if (body_pos > 0 && i == body_pos) {
530 w += font_metrics::width(labelsep, getLabelFont(pit));
531 if (pit->isLineSeparator(i - 1))
532 w -= singleWidth(pit, i - 1);
533 w = max(w, labelEnd(pit));
535 char const c = pit->getChar(i);
536 if (IsPrintable(c) && i > endPosOfFontSpan) {
537 // We need to get the next font
538 font = getFont(pit, i);
539 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
541 w += singleWidth(pit, i, c, font);
545 if (body_pos > 0 && body_pos >= end) {
546 w += font_metrics::width(labelsep, getLabelFont(pit));
547 if (end > 0 && pit->isLineSeparator(end - 1))
548 w -= singleWidth(pit, end - 1);
549 w = max(w, labelEnd(pit));
552 int const fill = workwidth - w - rightMargin(*pit);
554 row.width(workwidth - fill);
558 // returns the minimum space a manual label needs on the screen in pixel
559 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
561 pos_type last = pit->beginOfBody();
563 BOOST_ASSERT(last > 0);
565 // -1 because a label ends with a space that is in the label
568 // a separator at this end does not count
569 if (pit->isLineSeparator(last))
573 for (pos_type i = row.pos(); i <= last; ++i)
574 w += singleWidth(pit, i);
576 string const & label = pit->params().labelWidthString();
580 return max(0, font_metrics::width(label, getLabelFont(pit)) - w);
584 LColor_color LyXText::backgroundColor() const
586 return LColor_color(LColor::color(background_color_));
590 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
592 // get the maximum ascent and the maximum descent
593 double layoutasc = 0;
594 double layoutdesc = 0;
595 double const dh = defaultRowHeight();
597 // ok, let us initialize the maxasc and maxdesc value.
598 // Only the fontsize count. The other properties
599 // are taken from the layoutfont. Nicer on the screen :)
600 LyXLayout_ptr const & layout = pit->layout();
602 // as max get the first character of this row then it can
603 // increase but not decrease the height. Just some point to
604 // start with so we don't have to do the assignment below too
606 LyXFont font = getFont(pit, row.pos());
607 LyXFont::FONT_SIZE const tmpsize = font.size();
608 font = getLayoutFont(pit);
609 LyXFont::FONT_SIZE const size = font.size();
610 font.setSize(tmpsize);
612 LyXFont labelfont = getLabelFont(pit);
614 // these are minimum values
615 double const spacing_val = layout->spacing.getValue() * spacing(*pit);
616 //lyxerr << "spacing_val = " << spacing_val << endl;
617 int maxasc = int(font_metrics::maxAscent(font) * spacing_val);
618 int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
620 // insets may be taller
621 InsetList::iterator ii = pit->insetlist.begin();
622 InsetList::iterator iend = pit->insetlist.end();
623 for ( ; ii != iend; ++ii) {
624 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
625 maxasc = max(maxasc, ii->inset->ascent());
626 maxdesc = max(maxdesc, ii->inset->descent());
630 // Check if any custom fonts are larger (Asger)
631 // This is not completely correct, but we can live with the small,
632 // cosmetic error for now.
634 pos_type const pos_end = row.endpos();
636 LyXFont::FONT_SIZE maxsize =
637 pit->highestFontInRange(row.pos(), pos_end, size);
638 if (maxsize > font.size()) {
639 font.setSize(maxsize);
640 maxasc = max(maxasc, font_metrics::maxAscent(font));
641 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
644 // This is nicer with box insets:
648 row.ascent_of_text(maxasc);
651 if (row.pos() == 0) {
652 BufferParams const & bufparams = bv()->buffer()->params();
653 // some parksips VERY EASY IMPLEMENTATION
654 if (bv()->buffer()->params().paragraph_separation
655 == BufferParams::PARSEP_SKIP
656 && pit != paragraphs().begin()
657 && ((layout->isParagraph() && pit->getDepth() == 0)
658 || (boost::prior(pit)->layout()->isParagraph()
659 && boost::prior(pit)->getDepth() == 0)))
661 maxasc += bufparams.getDefSkip().inPixels(*bv());
664 if (pit->params().startOfAppendix())
665 maxasc += int(3 * dh);
667 // This is special code for the chapter, since the label of this
668 // layout is printed in an extra row
669 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
670 labeladdon = int(font_metrics::maxHeight(labelfont)
671 * layout->spacing.getValue() * spacing(*pit));
674 // special code for the top label
675 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
676 || layout->labeltype == LABEL_BIBLIO
677 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
678 && isFirstInSequence(pit, paragraphs())
679 && !pit->getLabelstring().empty())
682 font_metrics::maxHeight(labelfont)
683 * layout->spacing.getValue()
685 + (layout->topsep + layout->labelbottomsep) * dh);
688 // Add the layout spaces, for example before and after
689 // a section, or between the items of a itemize or enumerate
692 ParagraphList::iterator prev =
693 depthHook(pit, paragraphs(), pit->getDepth());
695 && prev->layout() == layout
696 && prev->getDepth() == pit->getDepth()
697 && prev->getLabelWidthString() == pit->getLabelWidthString())
699 layoutasc = layout->itemsep * dh;
700 } else if (pit != paragraphs().begin() || row.pos() != 0) {
701 if (layout->topsep > 0)
702 layoutasc = layout->topsep * dh;
705 prev = outerHook(pit, paragraphs());
706 if (prev != paragraphs().end()) {
707 maxasc += int(prev->layout()->parsep * dh);
708 } else if (pit != paragraphs().begin()) {
709 ParagraphList::iterator prior_pit = boost::prior(pit);
710 if (prior_pit->getDepth() != 0 ||
711 prior_pit->layout() == layout) {
712 maxasc += int(layout->parsep * dh);
717 // is it a bottom line?
718 if (row.endpos() >= pit->size()) {
719 // add the layout spaces, for example before and after
720 // a section, or between the items of a itemize or enumerate
722 ParagraphList::iterator nextpit = boost::next(pit);
723 if (nextpit != paragraphs().end()) {
724 ParagraphList::iterator cpit = pit;
728 if (cpit->getDepth() > nextpit->getDepth()) {
729 usual = cpit->layout()->bottomsep * dh;
730 cpit = depthHook(cpit, paragraphs(), nextpit->getDepth());
731 if (cpit->layout() != nextpit->layout()
732 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
734 unusual = cpit->layout()->bottomsep * dh;
736 layoutdesc = max(unusual, usual);
737 } else if (cpit->getDepth() == nextpit->getDepth()) {
738 if (cpit->layout() != nextpit->layout()
739 || nextpit->getLabelWidthString() != cpit->getLabelWidthString())
740 layoutdesc = int(cpit->layout()->bottomsep * dh);
745 // incalculate the layout spaces
746 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
747 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
749 row.height(maxasc + maxdesc + labeladdon);
750 row.baseline(maxasc + labeladdon);
751 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
755 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
757 // allow only if at start or end, or all previous is new text
758 ParagraphList::iterator cpit = cursorPar();
759 if (cursor().pos() && cursor().pos() != cpit->size()
760 && cpit->isChangeEdited(0, cursor().pos()))
763 LyXTextClass const & tclass =
764 bv()->buffer()->params().getLyXTextClass();
765 LyXLayout_ptr const & layout = cpit->layout();
767 // this is only allowed, if the current paragraph is not empty
768 // or caption and if it has not the keepempty flag active
769 if (cpit->empty() && !cpit->allowEmpty()
770 && layout->labeltype != LABEL_SENSITIVE)
773 // a layout change may affect also the following paragraph
774 recUndo(cursor().par(), parOffset(undoSpan(cpit)) - 1);
776 // Always break behind a space
777 // It is better to erase the space (Dekel)
778 if (cursor().pos() < cpit->size() && cpit->isLineSeparator(cursor().pos()))
779 cpit->erase(cursor().pos());
781 // break the paragraph
785 keep_layout = layout->isEnvironment();
787 // we need to set this before we insert the paragraph. IMO the
788 // breakParagraph call should return a bool if it inserts the
789 // paragraph before or behind and we should react on that one
790 // but we can fix this in 1.3.0 (Jug 20020509)
791 bool const isempty = cpit->allowEmpty() && cpit->empty();
792 ::breakParagraph(bv()->buffer()->params(), paragraphs, cpit,
793 cursor().pos(), keep_layout);
796 ParagraphList::iterator next_par = boost::next(cpit);
798 // well this is the caption hack since one caption is really enough
799 if (layout->labeltype == LABEL_SENSITIVE) {
801 // set to standard-layout
802 cpit->applyLayout(tclass.defaultLayout());
804 // set to standard-layout
805 next_par->applyLayout(tclass.defaultLayout());
808 // if the cursor is at the beginning of a row without prior newline,
810 // This touches only the screen-update. Otherwise we would may have
811 // an empty row on the screen
812 RowList::iterator crit = cpit->getRow(cursor().pos());
813 if (cursor().pos() && crit->pos() == cursor().pos()
814 && !cpit->isNewline(cursor().pos() - 1))
819 while (!next_par->empty() && next_par->isNewline(0))
824 redoParagraph(next_par);
826 // This check is necessary. Otherwise the new empty paragraph will
827 // be deleted automatically. And it is more friendly for the user!
828 if (cursor().pos() || isempty)
829 setCursor(next_par, 0);
835 // convenience function
836 void LyXText::redoParagraph()
838 bv()->cursor().clearSelection();
839 redoParagraph(cursorPar());
840 setCursorIntern(cursor().par(), cursor().pos());
844 // insert a character, moves all the following breaks in the
845 // same Paragraph one to the right and make a rebreak
846 void LyXText::insertChar(char c)
848 recordUndo(bv()->cursor(), Undo::INSERT);
850 // When the free-spacing option is set for the current layout,
851 // disable the double-space checking
853 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
854 cursorPar()->isFreeSpacing();
856 if (lyxrc.auto_number) {
857 static string const number_operators = "+-/*";
858 static string const number_unary_operators = "+-";
859 static string const number_seperators = ".,:";
861 if (current_font.number() == LyXFont::ON) {
862 if (!IsDigit(c) && !contains(number_operators, c) &&
863 !(contains(number_seperators, c) &&
864 cursor().pos() >= 1 &&
865 cursor().pos() < cursorPar()->size() &&
866 getFont(cursorPar(), cursor().pos()).number() == LyXFont::ON &&
867 getFont(cursorPar(), cursor().pos() - 1).number() == LyXFont::ON)
869 number(); // Set current_font.number to OFF
870 } else if (IsDigit(c) &&
871 real_current_font.isVisibleRightToLeft()) {
872 number(); // Set current_font.number to ON
874 if (cursor().pos() > 0) {
875 char const c = cursorPar()->getChar(cursor().pos() - 1);
876 if (contains(number_unary_operators, c) &&
877 (cursor().pos() == 1 ||
878 cursorPar()->isSeparator(cursor().pos() - 2) ||
879 cursorPar()->isNewline(cursor().pos() - 2))
885 } else if (contains(number_seperators, c) &&
886 cursor().pos() >= 2 &&
889 cursor().pos() - 2).number() == LyXFont::ON) {
899 // First check, if there will be two blanks together or a blank at
900 // the beginning of a paragraph.
901 // I decided to handle blanks like normal characters, the main
902 // difference are the special checks when calculating the row.fill
903 // (blank does not count at the end of a row) and the check here
905 // The bug is triggered when we type in a description environment:
906 // The current_font is not changed when we go from label to main text
907 // and it should (along with realtmpfont) when we type the space.
908 // CHECK There is a bug here! (Asger)
910 // store the current font. This is because of the use of cursor
911 // movements. The moving cursor would refresh the current font
912 LyXFont realtmpfont = real_current_font;
913 LyXFont rawtmpfont = current_font;
915 if (!freeSpacing && IsLineSeparatorChar(c)) {
916 if ((cursor().pos() > 0
917 && cursorPar()->isLineSeparator(cursor().pos() - 1))
918 || (cursor().pos() > 0
919 && cursorPar()->isNewline(cursor().pos() - 1))
920 || (cursor().pos() == 0)) {
921 static bool sent_space_message = false;
922 if (!sent_space_message) {
923 if (cursor().pos() == 0)
924 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
926 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
927 sent_space_message = true;
934 // Here case LyXText::InsertInset already inserted the character
935 if (c != Paragraph::META_INSET)
936 cursorPar()->insertChar(cursor().pos(), c);
938 setCharFont(cursorPar(), cursor().pos(), rawtmpfont);
940 current_font = rawtmpfont;
941 real_current_font = realtmpfont;
942 redoParagraph(cursorPar());
943 setCursor(cursor().par(), cursor().pos() + 1, false, cursor().boundary());
949 void LyXText::charInserted()
951 // Here we call finishUndo for every 20 characters inserted.
952 // This is from my experience how emacs does it. (Lgb)
953 static unsigned int counter;
963 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
965 double w = row.fill();
966 double fill_hfill = 0;
967 double fill_label_hfill = 0;
968 double fill_separator = 0;
972 pit->isRightToLeftPar(bv()->buffer()->params());
974 x = rightMargin(*pit);
976 x = leftMargin(pit, row.pos());
978 // is there a manual margin with a manual label
979 LyXLayout_ptr const & layout = pit->layout();
981 if (layout->margintype == MARGIN_MANUAL
982 && layout->labeltype == LABEL_MANUAL) {
983 /// We might have real hfills in the label part
984 int nlh = numberOfLabelHfills(*pit, row);
986 // A manual label par (e.g. List) has an auto-hfill
987 // between the label text and the body of the
989 // But we don't want to do this auto hfill if the par
994 if (nlh && !pit->getLabelWidthString().empty())
995 fill_label_hfill = labelFill(pit, row) / double(nlh);
998 // are there any hfills in the row?
999 int const nh = numberOfHfills(*pit, row);
1003 fill_hfill = w / nh;
1004 // we don't have to look at the alignment if it is ALIGN_LEFT and
1005 // if the row is already larger then the permitted width as then
1006 // we force the LEFT_ALIGN'edness!
1007 } else if (int(row.width()) < textWidth()) {
1008 // is it block, flushleft or flushright?
1009 // set x how you need it
1011 if (pit->params().align() == LYX_ALIGN_LAYOUT)
1012 align = layout->align;
1014 align = pit->params().align();
1016 // Display-style insets should always be on a centred row
1017 // The test on pit->size() is to catch zero-size pars, which
1018 // would trigger the assert in Paragraph::getInset().
1019 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1021 && pit->isInset(row.pos())
1022 && pit->getInset(row.pos())->display())
1024 align = LYX_ALIGN_CENTER;
1028 case LYX_ALIGN_BLOCK: {
1029 int const ns = numberOfSeparators(*pit, row);
1030 bool disp_inset = false;
1031 if (row.endpos() < pit->size()) {
1032 InsetBase * in = pit->getInset(row.endpos());
1034 disp_inset = in->display();
1036 // If we have separators, this is not the last row of a
1037 // par, does not end in newline, and is not row above a
1038 // display inset... then stretch it
1040 && row.endpos() < pit->size()
1041 && !pit->isNewline(row.endpos() - 1)
1044 fill_separator = w / ns;
1045 } else if (is_rtl) {
1050 case LYX_ALIGN_RIGHT:
1053 case LYX_ALIGN_CENTER:
1059 bidi.computeTables(*pit, *bv()->buffer(), row);
1061 pos_type body_pos = pit->beginOfBody();
1062 pos_type end = row.endpos();
1065 && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1067 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1068 if (body_pos <= end)
1069 x += fill_label_hfill;
1073 row.fill_hfill(fill_hfill);
1074 row.fill_label_hfill(fill_label_hfill);
1075 row.fill_separator(fill_separator);
1080 // the cursor set functions have a special mechanism. When they
1081 // realize, that you left an empty paragraph, they will delete it.
1083 void LyXText::cursorRightOneWord()
1085 cursorRightOneWord(cursor());
1086 setCursor(cursorPar(), cursor().pos());
1090 // Skip initial whitespace at end of word and move cursor to *start*
1091 // of prior word, not to end of next prior word.
1092 void LyXText::cursorLeftOneWord()
1094 CursorSlice tmpcursor = cursor();
1095 cursorLeftOneWord(tmpcursor);
1096 setCursor(getPar(tmpcursor), tmpcursor.pos());
1100 void LyXText::selectWord(word_location loc)
1102 CursorSlice from = cursor();
1103 CursorSlice to = cursor();
1104 getWord(from, to, loc);
1105 if (cursor() != from)
1106 setCursor(from.par(), from.pos());
1109 bv()->cursor().resetAnchor();
1110 setCursor(to.par(), to.pos());
1111 bv()->cursor().setSelection();
1115 // Select the word currently under the cursor when no
1116 // selection is currently set
1117 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1119 if (bv()->cursor().selection())
1122 return bv()->cursor().selection();
1126 void LyXText::acceptChange()
1128 LCursor & cur = bv()->cursor();
1129 if (!cur.selection() && cursorPar()->size())
1132 if (cur.selBegin().par() == cur.par()) {
1133 CursorSlice const & startc = cur.selBegin();
1134 CursorSlice const & endc = cur.selEnd();
1135 recordUndo(cur, Undo::INSERT, cur.anchor().par());
1136 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1138 cur.clearSelection();
1139 redoParagraph(getPar(startc));
1140 setCursorIntern(startc.par(), 0);
1142 #warning handle multi par selection
1146 void LyXText::rejectChange()
1148 LCursor & cur = bv()->cursor();
1149 if (!cur.selection() && cursorPar()->size())
1152 if (cur.selBegin().par() == cur.selEnd().par()) {
1153 CursorSlice const & startc = cur.selBegin();
1154 CursorSlice const & endc = cur.selEnd();
1155 recordUndo(cur, Undo::INSERT, cur.anchor().par());
1156 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1158 cur.clearSelection();
1159 redoParagraph(getPar(startc));
1160 setCursorIntern(startc.par(), 0);
1162 #warning handle multi par selection
1166 // Delete from cursor up to the end of the current or next word.
1167 void LyXText::deleteWordForward()
1169 LCursor & cur = bv()->cursor();
1170 if (cursorPar()->empty())
1173 CursorSlice tmpcursor = cursor();
1174 cur.selection() = true; // to avoid deletion
1175 cursorRightOneWord();
1176 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1178 cursor() = tmpcursor;
1180 cutSelection(true, false);
1185 // Delete from cursor to start of current or prior word.
1186 void LyXText::deleteWordBackward()
1188 LCursor & cur = bv()->cursor();
1189 if (cursorPar()->empty())
1192 CursorSlice tmpcursor = cursor();
1193 cur.selection() = true; // to avoid deletion
1194 cursorLeftOneWord();
1195 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1197 cursor() = tmpcursor;
1199 cutSelection(true, false);
1204 // Kill to end of line.
1205 void LyXText::deleteLineForward()
1207 LCursor & cur = bv()->cursor();
1208 if (cursorPar()->empty()) {
1209 // Paragraph is empty, so we just go to the right
1212 CursorSlice tmpcursor = cursor();
1213 cur.selection() = true; // to avoid deletion
1215 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1217 cursor() = tmpcursor;
1219 // What is this test for ??? (JMarc)
1220 if (!cur.selection())
1221 deleteWordForward();
1223 cutSelection(true, false);
1228 void LyXText::changeCase(LyXText::TextCase action)
1230 LCursor & cur = bv()->cursor();
1234 if (cur.selection()) {
1235 from = cur.selBegin();
1239 getWord(from, to, lyx::PARTIAL_WORD);
1240 setCursor(to.par(), to.pos() + 1);
1243 recordUndo(cur, Undo::ATOMIC, cur.anchor().par());
1245 pos_type pos = from.pos();
1246 int par = from.par();
1248 while (par != int(paragraphs().size()) &&
1249 (pos != to.pos() || par != to.par())) {
1250 ParagraphList::iterator pit = getPar(par);
1251 if (pos == pit->size()) {
1256 unsigned char c = pit->getChar(pos);
1257 if (c != Paragraph::META_INSET) {
1259 case text_lowercase:
1262 case text_capitalization:
1264 action = text_lowercase;
1266 case text_uppercase:
1272 pit->setChar(pos, c);
1278 void LyXText::Delete()
1280 // this is a very easy implementation
1281 CursorSlice old_cursor = cursor();
1283 // just move to the right
1286 // if you had success make a backspace
1287 if (old_cursor.par() != cursor().par()
1288 || old_cursor.pos() != cursor().pos()) {
1289 recordUndo(bv()->cursor(), Undo::DELETE, old_cursor.par());
1295 void LyXText::backspace()
1297 // Get the font that is used to calculate the baselineskip
1298 ParagraphList::iterator pit = cursorPar();
1299 pos_type lastpos = pit->size();
1301 if (cursor().pos() == 0) {
1302 // The cursor is at the beginning of a paragraph, so
1303 // the the backspace will collapse two paragraphs into
1306 // but it's not allowed unless it's new
1307 if (pit->isChangeEdited(0, pit->size()))
1310 // we may paste some paragraphs
1312 // is it an empty paragraph?
1313 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1314 // This is an empty paragraph and we delete it just
1315 // by moving the cursor one step
1316 // left and let the DeleteEmptyParagraphMechanism
1317 // handle the actual deletion of the paragraph.
1319 if (cursor().par()) {
1321 // the layout things can change the height of a row !
1327 if (cursor().par() != 0)
1328 recordUndo(bv()->cursor(), Undo::DELETE, cursor().par() - 1);
1330 ParagraphList::iterator tmppit = cursorPar();
1331 // We used to do cursorLeftIntern() here, but it is
1332 // not a good idea since it triggers the auto-delete
1333 // mechanism. So we do a cursorLeftIntern()-lite,
1334 // without the dreaded mechanism. (JMarc)
1335 if (cursor().par() != 0) {
1336 // steps into the above paragraph.
1337 setCursorIntern(cursor().par() - 1,
1338 getPar(cursor().par() - 1)->size(),
1342 // Pasting is not allowed, if the paragraphs have different
1343 // layout. I think it is a real bug of all other
1344 // word processors to allow it. It confuses the user.
1345 // Correction: Pasting is always allowed with standard-layout
1346 Buffer & buf = *bv()->buffer();
1347 BufferParams const & bufparams = buf.params();
1348 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1349 ParagraphList::iterator const cpit = cursorPar();
1352 && (cpit->layout() == tmppit->layout()
1353 || tmppit->layout() == tclass.defaultLayout())
1354 && cpit->getAlign() == tmppit->getAlign()) {
1355 mergeParagraph(bufparams, buf.paragraphs(), cpit);
1357 if (cursor().pos() && cpit->isSeparator(cursor().pos() - 1))
1358 cursor().pos(cursor().pos() - 1);
1360 // the counters may have changed
1362 setCursor(cursor().par(), cursor().pos(), false);
1365 // this is the code for a normal backspace, not pasting
1367 recordUndo(bv()->cursor(), Undo::DELETE);
1368 // We used to do cursorLeftIntern() here, but it is
1369 // not a good idea since it triggers the auto-delete
1370 // mechanism. So we do a cursorLeftIntern()-lite,
1371 // without the dreaded mechanism. (JMarc)
1372 setCursorIntern(cursor().par(), cursor().pos() - 1,
1373 false, cursor().boundary());
1374 cursorPar()->erase(cursor().pos());
1377 lastpos = cursorPar()->size();
1378 if (cursor().pos() == lastpos)
1382 setCursor(cursor().par(), cursor().pos(), false, cursor().boundary());
1386 ParagraphList::iterator LyXText::cursorPar() const
1388 //lyxerr << "### cursorPar: cursor: " << bv()->cursor() << endl;
1389 //lyxerr << "xxx cursorPar: cursor: " << cursor() << endl;
1390 return getPar(cursor().par());
1394 RowList::iterator LyXText::cursorRow() const
1396 return cursorPar()->getRow(cursor().pos());
1400 ParagraphList::iterator LyXText::getPar(CursorSlice const & cur) const
1402 return getPar(cur.par());
1406 ParagraphList::iterator LyXText::getPar(int par) const
1408 //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
1409 BOOST_ASSERT(par >= 0);
1410 BOOST_ASSERT(par < int(paragraphs().size()));
1411 ParagraphList::iterator pit = paragraphs().begin();
1417 // y is relative to this LyXText's top
1419 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
1421 BOOST_ASSERT(!paragraphs().empty());
1422 BOOST_ASSERT(!paragraphs().begin()->rows.empty());
1424 ParagraphList::iterator const
1425 pend = boost::prior(paragraphs().end());
1426 pit = paragraphs().begin();
1427 while (int(pit->y + pit->height) < y && pit != pend)
1430 RowList::iterator rit = pit->rows.end();
1431 RowList::iterator const rbegin = pit->rows.begin();
1434 } while (rit != rbegin && int(pit->y + rit->y_offset()) > y);
1438 pit = boost::prior(paragraphs().end());
1440 RowList::iterator rit = lastRow();
1441 RowList::iterator rbegin = firstRow();
1443 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
1444 previousRow(pit, rit);
1451 int LyXText::getDepth() const
1453 return cursorPar()->getDepth();
1457 RowList::iterator LyXText::firstRow() const
1459 return paragraphs().front().rows.begin();
1463 RowList::iterator LyXText::lastRow() const
1465 return boost::prior(endRow());
1469 RowList::iterator LyXText::endRow() const
1471 return paragraphs().back().rows.end();
1475 void LyXText::nextRow(ParagraphList::iterator & pit,
1476 RowList::iterator & rit) const
1479 if (rit == pit->rows.end()) {
1481 if (pit == paragraphs().end())
1484 rit = pit->rows.begin();
1489 void LyXText::previousRow(ParagraphList::iterator & pit,
1490 RowList::iterator & rit) const
1492 if (rit != pit->rows.begin())
1495 BOOST_ASSERT(pit != paragraphs().begin());
1497 rit = boost::prior(pit->rows.end());
1502 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
1504 LCursor & cur = bv()->cursor();
1505 if (!cur.selection())
1508 // should be const ...
1509 ParagraphList::iterator startpit = getPar(cur.selBegin());
1510 ParagraphList::iterator endpit = getPar(cur.selEnd());
1511 size_t const startpos = cur.selBegin().pos();
1512 size_t const endpos = cur.selEnd().pos();
1514 if (startpit == endpit)
1515 return startpit->asString(buffer, startpos, endpos, label);
1517 // First paragraph in selection
1519 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
1521 // The paragraphs in between (if any)
1522 ParagraphList::iterator pit = startpit;
1523 for (++pit; pit != endpit; ++pit)
1524 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
1526 // Last paragraph in selection
1527 result += endpit->asString(buffer, 0, endpos, label);
1533 int LyXText::parOffset(ParagraphList::iterator pit) const
1535 return distance(paragraphs().begin(), pit);
1539 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
1541 // remove rows of paragraph, keep track of height changes
1542 height -= pit->height;
1550 InsetList::iterator ii = pit->insetlist.begin();
1551 InsetList::iterator iend = pit->insetlist.end();
1552 for (; ii != iend; ++ii) {
1554 int const w = textWidth() - leftMargin(pit) - rightMargin(*pit);
1555 MetricsInfo mi(bv(), getFont(pit, ii->pos), w);
1556 ii->inset->metrics(mi, dim);
1559 // rebreak the paragraph
1560 pit->setBeginOfBody();
1564 rowBreakPoint(pit, row);
1566 fill(pit, row, textwidth_);
1567 prepareToPrint(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();
1573 } while (z < pit->size());
1575 height += pit->height;
1576 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
1580 void LyXText::redoParagraphs(ParagraphList::iterator pit,
1581 ParagraphList::iterator end)
1583 for ( ; pit != end; ++pit)
1584 redoParagraphInternal(pit);
1585 updateParPositions();
1589 void LyXText::redoParagraph(ParagraphList::iterator pit)
1591 redoParagraphInternal(pit);
1592 updateParPositions();
1596 void LyXText::fullRebreak()
1598 redoParagraphs(paragraphs().begin(), paragraphs().end());
1599 bv()->cursor().resetAnchor();
1603 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1605 //BOOST_ASSERT(mi.base.textwidth);
1606 if (mi.base.textwidth)
1607 textwidth_ = mi.base.textwidth;
1608 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1609 // << " textWidth: " << textWidth() << "\nfont: " << mi.base.font << endl;
1611 // Rebuild row cache. This recomputes height as well.
1612 redoParagraphs(paragraphs().begin(), paragraphs().end());
1614 width = maxParagraphWidth(paragraphs());
1617 dim.asc = firstRow()->ascent_of_text();
1618 dim.des = height - dim.asc;
1623 // only used for inset right now. should also be used for main text
1624 void LyXText::draw(PainterInfo & pi, int x, int y) const
1628 paintTextInset(*this, pi, x, y);
1632 // only used for inset right now. should also be used for main text
1633 void LyXText::drawSelection(PainterInfo & pi, int x, int y) const
1635 lyxerr << "LyXText::drawSelection at " << x << " " << y << endl;
1639 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
1641 return row.endpos() >= pit->size()
1642 && boost::next(pit) == paragraphs().end();
1646 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
1648 return row.pos() == 0 && pit == paragraphs().begin();
1652 void LyXText::cursorLeftOneWord(CursorSlice & cur)
1654 // treat HFills, floats and Insets as words
1656 ParagraphList::iterator pit = cursorPar();
1657 size_t pos = cur.pos();
1660 (pit->isSeparator(pos - 1) ||
1661 pit->isKomma(pos - 1) ||
1662 pit->isNewline(pos - 1)) &&
1663 !(pit->isHfill(pos - 1) ||
1664 pit->isInset(pos - 1)))
1668 (pit->isInset(pos - 1) ||
1669 pit->isHfill(pos - 1))) {
1672 if (pit != paragraphs().begin()) {
1676 } else { // Here, cur != 0
1677 while (pos > 0 && pit->isWord(pos - 1))
1681 cur.par(parOffset(pit));
1686 void LyXText::cursorRightOneWord(CursorSlice & cur)
1688 // treat floats, HFills and Insets as words
1689 ParagraphList::iterator pit = cursorPar();
1690 pos_type pos = cur.pos();
1692 if (pos == pit->size() &&
1693 boost::next(pit) != paragraphs().end()) {
1697 // Skip through initial nonword stuff.
1698 while (pos < pit->size() && !pit->isWord(pos)) {
1701 // Advance through word.
1702 while (pos < pit->size() && pit->isWord(pos)) {
1707 cur.par(parOffset(pit));
1712 void LyXText::getWord(CursorSlice & from, CursorSlice & to, word_location const loc)
1714 ParagraphList::iterator from_par = getPar(from);
1716 case lyx::WHOLE_WORD_STRICT:
1717 if (from.pos() == 0 || from.pos() == from_par->size()
1718 || from_par->isSeparator(from.pos())
1719 || from_par->isKomma(from.pos())
1720 || from_par->isNewline(from.pos())
1721 || from_par->isSeparator(from.pos() - 1)
1722 || from_par->isKomma(from.pos() - 1)
1723 || from_par->isNewline(from.pos() - 1)) {
1727 // no break here, we go to the next
1729 case lyx::WHOLE_WORD:
1730 // Move cursor to the beginning, when not already there.
1731 if (from.pos() && !from_par->isSeparator(from.pos() - 1)
1732 && !(from_par->isKomma(from.pos() - 1)
1733 || from_par->isNewline(from.pos() - 1)))
1734 cursorLeftOneWord(from);
1736 case lyx::PREVIOUS_WORD:
1737 // always move the cursor to the beginning of previous word
1738 cursorLeftOneWord(from);
1740 case lyx::NEXT_WORD:
1741 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1744 case lyx::PARTIAL_WORD:
1748 ParagraphList::iterator to_par = getPar(to);
1749 while (to.pos() < to_par->size()
1750 && !to_par->isSeparator(to.pos())
1751 && !to_par->isKomma(to.pos())
1752 && !to_par->isNewline(to.pos())
1753 && !to_par->isHfill(to.pos())
1754 && !to_par->isInset(to.pos()))
1756 to.pos(to.pos() + 1);
1761 void LyXText::write(Buffer const & buf, std::ostream & os) const
1763 ParagraphList::const_iterator pit = paragraphs().begin();
1764 ParagraphList::const_iterator end = paragraphs().end();
1765 Paragraph::depth_type dth = 0;
1766 for (; pit != end; ++pit)
1767 pit->write(buf, os, buf.params(), dth);
1771 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1773 static Change current_change;
1775 bool the_end_read = false;
1776 ParagraphList::iterator pit = paragraphs().begin();
1777 Paragraph::depth_type depth = 0;
1779 while (lex.isOK()) {
1781 string token = lex.getString();
1788 if (token == "\\end_inset") {
1789 the_end_read = true;
1793 if (token == "\\end_document") {
1794 lex.printError("\\end_document read in inset! Error in document!");
1800 if (token == "\\end_document") {
1801 the_end_read = true;
1810 if (token == "\\begin_layout") {
1811 lex.pushToken(token);
1814 par.params().depth(depth);
1815 if (buf.params().tracking_changes)
1817 LyXFont f(LyXFont::ALL_INHERIT, buf.params().language);
1821 if (pit != paragraphs().end())
1824 pit = paragraphs().insert(pit, par);
1826 // FIXME: goddamn InsetTabular makes us pass a Buffer
1828 ::readParagraph(buf, *pit, lex);
1830 } else if (token == "\\begin_deeper") {
1832 } else if (token == "\\end_deeper") {
1834 lex.printError("\\end_deeper: " "depth is already null");
1843 return the_end_read;
1847 int LyXText::ascent() const
1849 return firstRow()->ascent_of_text();
1853 int LyXText::descent() const
1855 return height - firstRow()->ascent_of_text();
1859 int LyXText::cursorX(CursorSlice const & cur) const
1861 ParagraphList::iterator pit = getPar(cur);
1862 if (pit->rows.empty())
1864 Row const & row = *pit->getRow(cur.pos());
1865 pos_type pos = cur.pos();
1866 pos_type cursor_vpos = 0;
1868 double fill_separator = row.fill_separator();
1869 double fill_hfill = row.fill_hfill();
1870 double fill_label_hfill = row.fill_label_hfill();
1871 pos_type const row_pos = row.pos();
1872 pos_type const end = row.endpos();
1875 cursor_vpos = row_pos;
1876 else if (pos >= end)
1877 cursor_vpos = (pit->isRightToLeftPar(bv()->buffer()->params()))
1879 else if (pos > row_pos && pos >= end)
1880 // Place cursor after char at (logical) position pos - 1
1881 cursor_vpos = (bidi.level(pos - 1) % 2 == 0)
1882 ? bidi.log2vis(pos - 1) + 1 : bidi.log2vis(pos - 1);
1884 // Place cursor before char at (logical) position pos
1885 cursor_vpos = (bidi.level(pos) % 2 == 0)
1886 ? bidi.log2vis(pos) : bidi.log2vis(pos) + 1;
1888 pos_type body_pos = pit->beginOfBody();
1890 (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
1893 for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1894 pos_type pos = bidi.vis2log(vpos);
1895 if (body_pos > 0 && pos == body_pos - 1) {
1896 x += fill_label_hfill
1897 + font_metrics::width(pit->layout()->labelsep,
1899 if (pit->isLineSeparator(body_pos - 1))
1900 x -= singleWidth(pit, body_pos - 1);
1903 if (hfillExpansion(*pit, row, pos)) {
1904 x += singleWidth(pit, pos);
1905 if (pos >= body_pos)
1908 x += fill_label_hfill;
1909 } else if (pit->isSeparator(pos)) {
1910 x += singleWidth(pit, pos);
1911 if (pos >= body_pos)
1912 x += fill_separator;
1914 x += singleWidth(pit, pos);
1916 return xo_ + int(x);
1920 int LyXText::cursorY(CursorSlice const & cur) const
1922 Paragraph & par = *getPar(cur);
1923 Row & row = *par.getRow(cur.pos());
1924 return yo_ + par.y + row.y_offset() + row.baseline();
1928 CursorSlice & LyXText::cursor()
1930 //lyxerr << "# accessing slice " << findText(this) << endl;
1931 if (this != bv()->cursor().text()) {
1932 lyxerr << "cursor: " << bv()->cursor()
1933 << "\ntext: " << bv()->cursor().text()
1934 << "\nthis: " << this << endl;
1935 BOOST_ASSERT(false);
1937 return bv()->cursor().current();
1941 CursorSlice const & LyXText::cursor() const
1943 if (this != bv()->cursor().text()) {
1944 lyxerr << "cursor: " << bv()->cursor()
1945 << "\ntext: " << bv()->cursor().text()
1946 << "\nthis: " << this << endl;
1947 BOOST_ASSERT(false);
1949 return bv()->cursor().current();