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 "funcrequest.h"
30 #include "lyxlength.h"
33 #include "lyxrow_funcs.h"
34 #include "paragraph.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphParameters.h"
37 #include "rowpainter.h"
38 #include "text_funcs.h"
39 #include "undo_funcs.h"
41 #include "WordLangTuple.h"
43 #include "frontends/font_metrics.h"
44 #include "frontends/LyXView.h"
46 #include "insets/insettext.h"
48 #include "support/lstrings.h"
49 #include "support/textutils.h"
51 using bv_funcs::number;
54 using lyx::word_location;
56 using lyx::support::contains;
57 using lyx::support::lowercase;
58 using lyx::support::uppercase;
65 /// top, right, bottom pixel margin
66 extern int const PAPER_MARGIN = 20;
67 /// margin for changebar
68 extern int const CHANGEBAR_MARGIN = 10;
70 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
74 int bibitemMaxWidth(BufferView *, LyXFont const &);
77 BufferView * LyXText::bv()
79 BOOST_ASSERT(bv_owner != 0);
84 BufferView * LyXText::bv() const
86 BOOST_ASSERT(bv_owner != 0);
91 void LyXText::updateRowPositions()
93 ParagraphList::iterator pit = ownerParagraphs().begin();
94 ParagraphList::iterator end = ownerParagraphs().end();
95 for (height = 0; pit != end; ++pit) {
96 RowList::iterator rit = pit->rows.begin();
97 RowList::iterator rend = pit->rows.end();
98 for ( ; rit != rend ; ++rit) {
100 height += rit->height();
106 int LyXText::workWidth() const
108 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
112 int LyXText::getRealCursorX() const
115 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
116 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
121 #warning FIXME This function seems to belong outside of LyxText.
122 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
125 if (!Encodings::is_arabic(c))
126 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
127 return c + (0xb0 - '0');
131 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
132 unsigned char next_char = ' ';
134 pos_type const par_size = par.size();
136 for (pos_type i = pos + 1; i < par_size; ++i) {
137 unsigned char const par_char = par.getChar(i);
138 if (!Encodings::IsComposeChar_arabic(par_char)) {
139 next_char = par_char;
144 if (Encodings::is_arabic(next_char)) {
145 if (Encodings::is_arabic(prev_char) &&
146 !Encodings::is_arabic_special(prev_char))
147 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
149 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
151 if (Encodings::is_arabic(prev_char) &&
152 !Encodings::is_arabic_special(prev_char))
153 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
155 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
159 // This is the comments that some of the warnings below refers to.
160 // There are some issues in this file and I don't think they are
161 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
162 // this is a problem that has been here almost from day one and that a
163 // larger userbase with differenct access patters triggers the bad
164 // behaviour. (segfaults.) What I think happen is: In several places
165 // we store the paragraph in the current cursor and then moves the
166 // cursor. This movement of the cursor will delete paragraph at the
167 // old position if it is now empty. This will make the temporary
168 // pointer to the old cursor paragraph invalid and dangerous to use.
169 // And is some cases this will trigger a segfault. I have marked some
170 // of the cases where this happens with a warning, but I am sure there
171 // are others in this file and in text2.C. There is also a note in
172 // Delete() that you should read. In Delete I store the paragraph->id
173 // instead of a pointer to the paragraph. I am pretty sure this faulty
174 // use of temporary pointers to paragraphs that might have gotten
175 // invalidated (through a cursor movement) before they are used, are
176 // the cause of the strange crashes we get reported often.
178 // It is very tiresom to change this code, especially when it is as
179 // hard to read as it is. Help to fix all the cases where this is done
180 // would be greately appreciated.
184 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
186 if (pos >= pit->size())
189 char const c = pit->getChar(pos);
190 LyXFont const & font = getFont(pit, pos);
191 return singleWidth(pit, pos, c, font);
195 int LyXText::singleWidth(ParagraphList::iterator pit,
196 pos_type pos, char c, LyXFont const & font) const
198 if (pos >= pit->size()) {
199 lyxerr << "in singleWidth(), pos: " << pos << endl;
205 // The most common case is handled first (Asger)
206 if (IsPrintable(c)) {
207 if (font.language()->RightToLeft()) {
208 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
209 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
210 && font.language()->lang() == "arabic") {
211 if (Encodings::IsComposeChar_arabic(c))
214 c = transformChar(c, *pit, pos);
215 } else if (font.language()->lang() == "hebrew" &&
216 Encodings::IsComposeChar_hebrew(c))
219 return font_metrics::width(c, font);
222 if (c == Paragraph::META_INSET) {
223 InsetOld * tmpinset = pit->getInset(pos);
224 BOOST_ASSERT(tmpinset);
225 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
226 // Because of the representation as vertical lines
229 return tmpinset->width();
232 if (IsSeparatorChar(c))
234 return font_metrics::width(c, font);
238 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
240 if (bidi_start == -1)
243 return log2vis_list[pos - bidi_start];
247 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
249 if (bidi_start == -1)
252 return vis2log_list[pos - bidi_start];
256 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
258 if (bidi_start == -1)
261 return bidi_levels[pos - bidi_start];
265 bool LyXText::bidi_InRange(lyx::pos_type pos) const
267 return bidi_start == -1 ||
268 (bidi_start <= pos && pos <= bidi_end);
272 void LyXText::computeBidiTables(ParagraphList::iterator pit,
273 Buffer const & buf, RowList::iterator row) const
275 bidi_same_direction = true;
276 if (!lyxrc.rtl_support) {
281 InsetOld * inset = pit->inInset();
282 if (inset && inset->owner() &&
283 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
288 bidi_start = row->pos();
289 bidi_end = lastPos(*pit, row);
291 if (bidi_start > bidi_end) {
296 if (bidi_end + 2 - bidi_start >
297 static_cast<pos_type>(log2vis_list.size())) {
299 (bidi_end + 2 - bidi_start < 500) ?
300 500 : 2 * (bidi_end + 2 - bidi_start);
301 log2vis_list.resize(new_size);
302 vis2log_list.resize(new_size);
303 bidi_levels.resize(new_size);
306 vis2log_list[bidi_end + 1 - bidi_start] = -1;
307 log2vis_list[bidi_end + 1 - bidi_start] = -1;
309 BufferParams const & bufparams = buf.params();
312 pit->isRightToLeftPar(bufparams);
316 pos_type const body_pos = pit->beginningOfBody();
318 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
319 bool is_space = pit->isLineSeparator(lpos);
321 (is_space && lpos + 1 <= bidi_end &&
322 !pit->isLineSeparator(lpos + 1) &&
323 !pit->isNewline(lpos + 1))
325 LyXFont font = pit->getFontSettings(bufparams, pos);
326 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
327 font.number() == LyXFont::ON &&
328 pit->getFontSettings(bufparams, lpos - 1).number()
330 font = pit->getFontSettings(bufparams, lpos);
335 bool new_rtl = font.isVisibleRightToLeft();
336 bool new_rtl0 = font.isRightToLeft();
339 if (lpos == body_pos - 1
340 && row->pos() < body_pos - 1
342 new_level = (rtl_par) ? 1 : 0;
343 new_rtl = new_rtl0 = rtl_par;
345 new_level = (new_rtl) ? 1 : 2;
347 new_level = (rtl_par) ? 2 : 0;
349 if (is_space && new_level >= level) {
355 int new_level2 = new_level;
357 if (level == new_level && rtl0 != new_rtl0) {
359 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
360 } else if (level < new_level) {
361 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
362 if (new_level > rtl_par)
363 bidi_same_direction = false;
365 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
368 bidi_levels[lpos - bidi_start] = new_level;
370 while (level > new_level2) {
371 pos_type old_lpos = stack[--level];
372 int delta = lpos - old_lpos - 1;
375 log2vis_list[lpos - bidi_start] += delta;
376 log2vis_list[old_lpos - bidi_start] += delta;
378 while (level < new_level)
379 stack[level++] = lpos;
383 pos_type const old_lpos = stack[--level];
384 int delta = bidi_end - old_lpos;
387 log2vis_list[old_lpos - bidi_start] += delta;
390 pos_type vpos = bidi_start - 1;
391 for (pos_type lpos = bidi_start;
392 lpos <= bidi_end; ++lpos) {
393 vpos += log2vis_list[lpos - bidi_start];
394 vis2log_list[vpos - bidi_start] = lpos;
395 log2vis_list[lpos - bidi_start] = vpos;
400 // This method requires a previous call to ComputeBidiTables()
401 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
404 if (!lyxrc.rtl_support || pos == 0)
407 if (!bidi_InRange(pos - 1)) {
408 /// This can happen if pos is the first char of a row.
409 /// Returning false in this case is incorrect!
413 bool const rtl = bidi_level(pos - 1) % 2;
414 bool const rtl2 = bidi_InRange(pos)
415 ? bidi_level(pos) % 2
416 : par.isRightToLeftPar(buf.params());
421 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
422 pos_type pos, LyXFont const & font) const
424 if (!lyxrc.rtl_support)
425 return false; // This is just for speedup
427 bool const rtl = font.isVisibleRightToLeft();
428 bool const rtl2 = bidi_InRange(pos)
429 ? bidi_level(pos) % 2
430 : par.isRightToLeftPar(buf.params());
435 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
437 LyXTextClass const & tclass =
438 bv()->buffer()->params().getLyXTextClass();
439 LyXLayout_ptr const & layout = pit->layout();
441 string parindent = layout->parindent;
445 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
447 // this is the way, LyX handles the LaTeX-Environments.
448 // I have had this idea very late, so it seems to be a
449 // later added hack and this is true
450 if (!pit->getDepth()) {
451 if (pit->layout() == tclass.defaultLayout()) {
452 // find the previous same level paragraph
453 if (pit != ownerParagraphs().begin()) {
454 ParagraphList::iterator newpit =
455 depthHook(pit, ownerParagraphs(),
458 newpit->layout()->nextnoindent)
463 // find the next level paragraph
465 ParagraphList::iterator newpar = outerHook(pit,
468 // make a corresponding row. Needed to call leftMargin()
470 // check wether it is a sufficent paragraph
471 if (newpar != ownerParagraphs().end() &&
472 newpar->layout()->isEnvironment()) {
473 x = leftMargin(newpar, Row(newpar->size()));
476 if (newpar != ownerParagraphs().end() &&
477 pit->layout() == tclass.defaultLayout()) {
478 if (newpar->params().noindent())
481 parindent = newpar->layout()->parindent;
487 LyXFont const labelfont = getLabelFont(pit);
488 switch (layout->margintype) {
490 if (!layout->leftmargin.empty()) {
491 x += font_metrics::signedWidth(layout->leftmargin,
492 tclass.defaultfont());
494 if (!pit->getLabelstring().empty()) {
495 x += font_metrics::signedWidth(layout->labelindent,
497 x += font_metrics::width(pit->getLabelstring(),
499 x += font_metrics::width(layout->labelsep, labelfont);
503 x += font_metrics::signedWidth(layout->labelindent, labelfont);
504 // The width of an empty par, even with manual label, should be 0
505 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
506 if (!pit->getLabelWidthString().empty()) {
507 x += font_metrics::width(pit->getLabelWidthString(),
509 x += font_metrics::width(layout->labelsep, labelfont);
514 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
515 / (pit->getDepth() + 4);
517 case MARGIN_FIRST_DYNAMIC:
518 if (layout->labeltype == LABEL_MANUAL) {
519 if (row.pos() >= pit->beginningOfBody()) {
520 x += font_metrics::signedWidth(layout->leftmargin,
523 x += font_metrics::signedWidth(layout->labelindent,
527 // Special case to fix problems with
529 || (layout->labeltype == LABEL_STATIC
530 && layout->latextype == LATEX_ENVIRONMENT
531 && !isFirstInSequence(pit, ownerParagraphs()))) {
532 x += font_metrics::signedWidth(layout->leftmargin,
534 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
535 && layout->labeltype != LABEL_BIBLIO
536 && layout->labeltype !=
537 LABEL_CENTERED_TOP_ENVIRONMENT) {
538 x += font_metrics::signedWidth(layout->labelindent,
540 x += font_metrics::width(layout->labelsep, labelfont);
541 x += font_metrics::width(pit->getLabelstring(),
546 case MARGIN_RIGHT_ADDRESS_BOX:
548 // ok, a terrible hack. The left margin depends on the widest
549 // row in this paragraph. Do not care about footnotes, they
550 // are *NOT* allowed in the LaTeX realisation of this layout.
552 // find the first row of this paragraph
553 RowList::iterator rit = pit->rows.begin();
554 RowList::iterator end = pit->rows.end();
555 int minfill = rit->fill();
556 for ( ; rit != end; ++rit)
557 if (rit->fill() < minfill)
558 minfill = rit->fill();
560 x += font_metrics::signedWidth(layout->leftmargin,
561 tclass.defaultfont());
567 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
568 LyXLength const len = pit->params().leftIndent();
569 int const tw = inset_owner ?
570 inset_owner->latexTextWidth(bv()) : workWidth();
571 x += len.inPixels(tw);
576 if (pit->params().align() == LYX_ALIGN_LAYOUT)
577 align = layout->align;
579 align = pit->params().align();
581 // set the correct parindent
582 if (row.pos() == 0) {
583 if ((layout->labeltype == LABEL_NO_LABEL
584 || layout->labeltype == LABEL_TOP_ENVIRONMENT
585 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
586 || (layout->labeltype == LABEL_STATIC
587 && layout->latextype == LATEX_ENVIRONMENT
588 && !isFirstInSequence(pit, ownerParagraphs())))
589 && align == LYX_ALIGN_BLOCK
590 && !pit->params().noindent()
591 // in tabulars and ert paragraphs are never indented!
592 && (!pit->inInset() || !pit->inInset()->owner() ||
593 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
594 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
595 && (pit->layout() != tclass.defaultLayout() ||
596 bv()->buffer()->params().paragraph_separation ==
597 BufferParams::PARSEP_INDENT)) {
598 x += font_metrics::signedWidth(parindent,
599 tclass.defaultfont());
600 } else if (layout->labeltype == LABEL_BIBLIO) {
601 // ale970405 Right width for bibitems
602 x += bibitemMaxWidth(bv(), tclass.defaultfont());
610 int LyXText::rightMargin(ParagraphList::iterator pit,
611 Buffer const & buf, Row const &) const
613 LyXTextClass const & tclass = buf.params().getLyXTextClass();
614 LyXLayout_ptr const & layout = pit->layout();
617 + font_metrics::signedWidth(tclass.rightmargin(),
618 tclass.defaultfont());
619 + font_metrics::signedWidth(layout->rightmargin,
620 tclass.defaultfont())
621 * 4 / (pit->getDepth() + 4);
625 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
627 if (pit->layout()->margintype == MARGIN_MANUAL) {
629 tmprow.pos(pit->size());
630 // return the beginning of the body
631 return leftMargin(pit, tmprow);
634 // LabelEnd is only needed if the layout
635 // fills a flushleft label.
642 // this needs special handling - only newlines count as a break point
643 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
645 for (; i < par.size(); ++i) {
646 if (par.isNewline(i))
656 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
657 Row const & row) const
659 // maximum pixel width of a row.
660 int width = workWidth()
661 - rightMargin(pit, *bv()->buffer(), row);
663 // inset->textWidth() returns -1 via workWidth(),
668 LyXLayout_ptr const & layout = pit->layout();
670 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
671 return addressBreakPoint(row.pos(), *pit);
673 pos_type const pos = row.pos();
674 pos_type const body_pos = pit->beginningOfBody();
675 pos_type const last = pit->size();
676 pos_type point = last;
681 // Now we iterate through until we reach the right margin
682 // or the end of the par, then choose the possible break
685 int const left = leftMargin(pit, row);
688 // pixel width since last breakpoint
693 // We re-use the font resolution for the entire font span when possible
694 LyXFont font = getFont(pit, i);
695 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
697 for (; i < last; ++i) {
698 if (pit->isNewline(i)) {
703 char const c = pit->getChar(i);
704 if (i > endPosOfFontSpan) {
705 font = getFont(pit, i);
706 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
711 // add the auto-hfill from label end to the body
712 if (body_pos && i == body_pos) {
713 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
714 if (pit->isLineSeparator(i - 1))
715 thiswidth -= singleWidth(pit, i - 1);
716 int left_margin = labelEnd(pit, row);
717 if (thiswidth + x < left_margin)
718 thiswidth = left_margin - x;
719 thiswidth += singleWidth(pit, i, c, font);
721 thiswidth = singleWidth(pit, i, c, font);
725 chunkwidth += thiswidth;
727 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
729 // break before a character that will fall off
730 // the right of the row
732 // if no break before, break here
733 if (point == last || chunkwidth >= (width - left)) {
742 if (!in || in->isChar()) {
743 // some insets are line separators too
744 if (pit->isLineSeparator(i)) {
754 if (point == last && x >= width) {
755 // didn't find one, break at the point we reached the edge
757 } else if (i == last && x < width) {
758 // found one, but we fell off the end of the par, so prefer
763 // manual labels cannot be broken in LaTeX. But we
764 // want to make our on-screen rendering of footnotes
766 if (body_pos && point < body_pos)
767 point = body_pos - 1;
773 // returns the minimum space a row needs on the screen in pixel
774 int LyXText::fill(ParagraphList::iterator pit,
775 RowList::iterator row, int paper_width) const
781 // get the pure distance
782 pos_type const last = lastPos(*pit, row);
784 LyXLayout_ptr const & layout = pit->layout();
786 // special handling of the right address boxes
787 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
788 int const tmpfill = row->fill();
789 row->fill(0); // the minfill in MarginLeft()
790 w = leftMargin(pit, *row);
793 w = leftMargin(pit, *row);
795 pos_type const body_pos = pit->beginningOfBody();
796 pos_type i = row->pos();
798 if (! pit->empty() && i <= last) {
799 // We re-use the font resolution for the entire span when possible
800 LyXFont font = getFont(pit, i);
801 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
803 if (body_pos > 0 && i == body_pos) {
804 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
805 if (pit->isLineSeparator(i - 1))
806 w -= singleWidth(pit, i - 1);
807 int left_margin = labelEnd(pit, *row);
811 char const c = pit->getChar(i);
812 if (IsPrintable(c) && i > endPosOfFontSpan) {
813 // We need to get the next font
814 font = getFont(pit, i);
815 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
817 w += singleWidth(pit, i, c, font);
821 if (body_pos > 0 && body_pos > last) {
822 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
823 if (last >= 0 && pit->isLineSeparator(last))
824 w -= singleWidth(pit, last);
825 int const left_margin = labelEnd(pit, *row);
830 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
832 // If this case happens, it means that our calculation
833 // of the widths of the chars when we do rowBreakPoint()
834 // went wrong for some reason. Typically in list bodies.
835 // Things just about hobble on anyway, though you'll end
836 // up with a "fill_separator" less than zero, which corresponds
837 // to inter-word spacing being too small. Hopefully this problem
838 // will die when the label hacks die.
839 if (lyxerr.debugging() && fill < 0) {
840 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
841 << " w " << w << " paper_width " << paper_width
842 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
848 // returns the minimum space a manual label needs on the screen in pixel
849 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
851 pos_type last = pit->beginningOfBody();
853 BOOST_ASSERT(last > 0);
855 // -1 because a label ends either with a space that is in the label,
856 // or with the beginning of a footnote that is outside the label.
859 // a separator at this end does not count
860 if (pit->isLineSeparator(last))
864 pos_type i = row.pos();
866 w += singleWidth(pit, i);
871 string const & labwidstr = pit->params().labelWidthString();
872 if (!labwidstr.empty()) {
873 LyXFont const labfont = getLabelFont(pit);
874 int const labwidth = font_metrics::width(labwidstr, labfont);
875 fill = max(labwidth - w, 0);
882 LColor_color LyXText::backgroundColor() const
885 return inset_owner->backgroundColor();
887 return LColor::background;
891 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
893 // get the maximum ascent and the maximum descent
894 double layoutasc = 0;
895 double layoutdesc = 0;
898 // ok, let us initialize the maxasc and maxdesc value.
899 // Only the fontsize count. The other properties
900 // are taken from the layoutfont. Nicer on the screen :)
901 LyXLayout_ptr const & layout = pit->layout();
903 // as max get the first character of this row then it can increase but not
904 // decrease the height. Just some point to start with so we don't have to
905 // do the assignment below too often.
906 LyXFont font = getFont(pit, rit->pos());
907 LyXFont::FONT_SIZE const tmpsize = font.size();
908 font = getLayoutFont(pit);
909 LyXFont::FONT_SIZE const size = font.size();
910 font.setSize(tmpsize);
912 LyXFont labelfont = getLabelFont(pit);
914 double spacing_val = 1.0;
915 if (!pit->params().spacing().isDefault())
916 spacing_val = pit->params().spacing().getValue();
918 spacing_val = bv()->buffer()->params().spacing().getValue();
919 //lyxerr << "spacing_val = " << spacing_val << endl;
921 int maxasc = int(font_metrics::maxAscent(font) *
922 layout->spacing.getValue() * spacing_val);
923 int maxdesc = int(font_metrics::maxDescent(font) *
924 layout->spacing.getValue() * spacing_val);
926 pos_type const pos_end = lastPos(*pit, rit);
931 // We re-use the font resolution for the entire font span when possible
932 LyXFont font = getFont(pit, rit->pos());
933 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
936 Paragraph const & par = *pit;
938 // Check if any insets are larger
939 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
940 // Manual inlined optimised version of common case of
941 // "maxwidth += singleWidth(pit, pos);"
942 char const c = par.getChar(pos);
944 if (IsPrintable(c)) {
945 if (pos > endPosOfFontSpan) {
946 // We need to get the next font
947 font = getFont(pit, pos);
948 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
950 if (! font.language()->RightToLeft()) {
951 maxwidth += font_metrics::width(c, font);
953 // Fall-back to normal case
954 maxwidth += singleWidth(pit, pos, c, font);
955 // And flush font cache
956 endPosOfFontSpan = 0;
959 // Special handling of insets - are any larger?
960 if (par.isInset(pos)) {
961 InsetOld const * tmpinset = par.getInset(pos);
963 maxwidth += tmpinset->width();
964 maxasc = max(maxasc, tmpinset->ascent());
965 maxdesc = max(maxdesc, tmpinset->descent());
968 // Fall-back to normal case
969 maxwidth += singleWidth(pit, pos, c, font);
970 // And flush font cache
971 endPosOfFontSpan = 0;
977 // Check if any custom fonts are larger (Asger)
978 // This is not completely correct, but we can live with the small,
979 // cosmetic error for now.
980 LyXFont::FONT_SIZE maxsize =
981 pit->highestFontInRange(rit->pos(), pos_end, size);
982 if (maxsize > font.size()) {
983 font.setSize(maxsize);
984 maxasc = max(maxasc, font_metrics::maxAscent(font));
985 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
988 // This is nicer with box insets:
992 rit->ascent_of_text(maxasc);
996 BufferParams const & bufparams = bv()->buffer()->params();
997 // some parksips VERY EASY IMPLEMENTATION
998 if (bv()->buffer()->params().paragraph_separation ==
999 BufferParams::PARSEP_SKIP)
1001 if (layout->isParagraph()
1002 && pit->getDepth() == 0
1003 && pit != ownerParagraphs().begin())
1005 maxasc += bufparams.getDefSkip().inPixels(*bv());
1006 } else if (pit != ownerParagraphs().begin() &&
1007 boost::prior(pit)->layout()->isParagraph() &&
1008 boost::prior(pit)->getDepth() == 0)
1010 // is it right to use defskip here too? (AS)
1011 maxasc += bufparams.getDefSkip().inPixels(*bv());
1016 if (pit == ownerParagraphs().begin() && !isInInset())
1017 maxasc += PAPER_MARGIN;
1019 // add the vertical spaces, that the user added
1020 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1022 // do not forget the DTP-lines!
1023 // there height depends on the font of the nearest character
1024 if (pit->params().lineTop())
1026 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1027 // and now the pagebreaks
1028 if (pit->params().pagebreakTop())
1029 maxasc += 3 * defaultRowHeight();
1031 if (pit->params().startOfAppendix())
1032 maxasc += 3 * defaultRowHeight();
1034 // This is special code for the chapter, since the label of this
1035 // layout is printed in an extra row
1036 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
1037 float spacing_val = 1.0;
1038 if (!pit->params().spacing().isDefault()) {
1039 spacing_val = pit->params().spacing().getValue();
1041 spacing_val = bufparams.spacing().getValue();
1044 labeladdon = int(font_metrics::maxDescent(labelfont) *
1045 layout->spacing.getValue() *
1047 + int(font_metrics::maxAscent(labelfont) *
1048 layout->spacing.getValue() *
1052 // special code for the top label
1053 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1054 || layout->labeltype == LABEL_BIBLIO
1055 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1056 && isFirstInSequence(pit, ownerParagraphs())
1057 && !pit->getLabelstring().empty())
1059 float spacing_val = 1.0;
1060 if (!pit->params().spacing().isDefault()) {
1061 spacing_val = pit->params().spacing().getValue();
1063 spacing_val = bufparams.spacing().getValue();
1067 (font_metrics::maxAscent(labelfont) +
1068 font_metrics::maxDescent(labelfont)) *
1069 layout->spacing.getValue() *
1071 + layout->topsep * defaultRowHeight()
1072 + layout->labelbottomsep * defaultRowHeight());
1075 // And now the layout spaces, for example before and after
1076 // a section, or between the items of a itemize or enumerate
1079 if (!pit->params().pagebreakTop()) {
1080 ParagraphList::iterator prev =
1081 depthHook(pit, ownerParagraphs(),
1083 if (prev != pit && prev->layout() == layout &&
1084 prev->getDepth() == pit->getDepth() &&
1085 prev->getLabelWidthString() == pit->getLabelWidthString())
1087 layoutasc = (layout->itemsep * defaultRowHeight());
1088 } else if (rit != firstRow()) {
1089 tmptop = layout->topsep;
1091 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1092 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1095 layoutasc = (tmptop * defaultRowHeight());
1096 } else if (pit->params().lineTop()) {
1097 tmptop = layout->topsep;
1100 layoutasc = (tmptop * defaultRowHeight());
1103 prev = outerHook(pit, ownerParagraphs());
1104 if (prev != ownerParagraphs().end()) {
1105 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1106 } else if (pit != ownerParagraphs().begin()) {
1107 ParagraphList::iterator prior_pit = boost::prior(pit);
1108 if (prior_pit->getDepth() != 0 ||
1109 prior_pit->layout() == layout) {
1110 maxasc += int(layout->parsep * defaultRowHeight());
1116 // is it a bottom line?
1117 if (boost::next(rit) == pit->rows.end()) {
1118 // the bottom margin
1119 ParagraphList::iterator nextpit = boost::next(pit);
1120 if (nextpit == ownerParagraphs().end() && !isInInset())
1121 maxdesc += PAPER_MARGIN;
1123 // add the vertical spaces, that the user added
1124 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1126 // do not forget the DTP-lines!
1127 // there height depends on the font of the nearest character
1128 if (pit->params().lineBottom())
1129 maxdesc += 2 * font_metrics::ascent('x',
1130 getFont(pit, max(pos_type(0), pit->size() - 1)));
1132 // and now the pagebreaks
1133 if (pit->params().pagebreakBottom())
1134 maxdesc += 3 * defaultRowHeight();
1136 // and now the layout spaces, for example before and after
1137 // a section, or between the items of a itemize or enumerate
1139 if (!pit->params().pagebreakBottom()
1140 && nextpit != ownerParagraphs().end()) {
1141 ParagraphList::iterator comparepit = pit;
1145 if (comparepit->getDepth() > nextpit->getDepth()) {
1146 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1147 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1148 if (comparepit->layout()!= nextpit->layout()
1149 || nextpit->getLabelWidthString() !=
1150 comparepit->getLabelWidthString())
1152 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1154 if (unusual > usual)
1155 layoutdesc = unusual;
1158 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1160 if (comparepit->layout() != nextpit->layout()
1161 || nextpit->getLabelWidthString() !=
1162 comparepit->getLabelWidthString())
1163 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1168 // incalculate the layout spaces
1169 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1170 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1172 rit->height(maxasc + maxdesc + labeladdon);
1173 rit->baseline(maxasc + labeladdon);
1174 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1177 rit->width(int(maxwidth + x));
1179 width = max(0, workWidth());
1180 RowList::iterator rit = firstRow();
1181 RowList::iterator end = endRow();
1182 ParagraphList::iterator it = ownerParagraphs().begin();
1183 while (rit != end) {
1184 if (rit->width() > width)
1185 width = rit->width();
1192 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1194 // allow only if at start or end, or all previous is new text
1195 if (cursor.pos() && cursor.pos() != cursorPar()->size()
1196 && cursorPar()->isChangeEdited(0, cursor.pos()))
1199 LyXTextClass const & tclass =
1200 bv()->buffer()->params().getLyXTextClass();
1201 LyXLayout_ptr const & layout = cursorPar()->layout();
1203 // this is only allowed, if the current paragraph is not empty or caption
1204 // and if it has not the keepempty flag active
1205 if (cursorPar()->empty() && !cursorPar()->allowEmpty()
1206 && layout->labeltype != LABEL_SENSITIVE)
1209 recUndo(cursor.par());
1211 // Always break behind a space
1213 // It is better to erase the space (Dekel)
1214 if (cursor.pos() < cursorPar()->size()
1215 && cursorPar()->isLineSeparator(cursor.pos()))
1216 cursorPar()->erase(cursor.pos());
1218 // break the paragraph
1222 keep_layout = layout->isEnvironment();
1224 // we need to set this before we insert the paragraph. IMO the
1225 // breakParagraph call should return a bool if it inserts the
1226 // paragraph before or behind and we should react on that one
1227 // but we can fix this in 1.3.0 (Jug 20020509)
1228 bool const isempty = (cursorPar()->allowEmpty() && cursorPar()->empty());
1229 ::breakParagraph(bv()->buffer()->params(), paragraphs, cursorPar(),
1230 cursor.pos(), keep_layout);
1232 #warning Trouble Point! (Lgb)
1233 // When ::breakParagraph is called from within an inset we must
1234 // ensure that the correct ParagraphList is used. Today that is not
1235 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1236 ParagraphList::iterator next_par = boost::next(cursorPar());
1238 // well this is the caption hack since one caption is really enough
1239 if (layout->labeltype == LABEL_SENSITIVE) {
1241 // set to standard-layout
1242 cursorPar()->applyLayout(tclass.defaultLayout());
1244 // set to standard-layout
1245 next_par->applyLayout(tclass.defaultLayout());
1248 // if the cursor is at the beginning of a row without prior newline,
1250 // This touches only the screen-update. Otherwise we would may have
1251 // an empty row on the screen
1252 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1253 && !cursorPar()->isNewline(cursor.pos() - 1))
1258 while (!next_par->empty() && next_par->isNewline(0))
1262 redoParagraph(cursorPar());
1263 redoParagraph(next_par);
1265 // This check is necessary. Otherwise the new empty paragraph will
1266 // be deleted automatically. And it is more friendly for the user!
1267 if (cursor.pos() || isempty)
1268 setCursor(next_par, 0);
1270 setCursor(cursorPar(), 0);
1274 // convenience function
1275 void LyXText::redoParagraph()
1278 redoParagraph(cursorPar());
1279 setCursorIntern(cursor.par(), cursor.pos());
1283 // insert a character, moves all the following breaks in the
1284 // same Paragraph one to the right and make a rebreak
1285 void LyXText::insertChar(char c)
1287 recordUndo(bv(), Undo::INSERT, ownerParagraphs(), cursor.par(), cursor.par());
1289 // When the free-spacing option is set for the current layout,
1290 // disable the double-space checking
1292 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1293 cursorPar()->isFreeSpacing();
1295 if (lyxrc.auto_number) {
1296 static string const number_operators = "+-/*";
1297 static string const number_unary_operators = "+-";
1298 static string const number_seperators = ".,:";
1300 if (current_font.number() == LyXFont::ON) {
1301 if (!IsDigit(c) && !contains(number_operators, c) &&
1302 !(contains(number_seperators, c) &&
1303 cursor.pos() >= 1 &&
1304 cursor.pos() < cursorPar()->size() &&
1305 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1306 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1308 number(bv()); // Set current_font.number to OFF
1309 } else if (IsDigit(c) &&
1310 real_current_font.isVisibleRightToLeft()) {
1311 number(bv()); // Set current_font.number to ON
1313 if (cursor.pos() > 0) {
1314 char const c = cursorPar()->getChar(cursor.pos() - 1);
1315 if (contains(number_unary_operators, c) &&
1316 (cursor.pos() == 1 ||
1317 cursorPar()->isSeparator(cursor.pos() - 2) ||
1318 cursorPar()->isNewline(cursor.pos() - 2))
1324 } else if (contains(number_seperators, c) &&
1325 cursor.pos() >= 2 &&
1328 cursor.pos() - 2).number() == LyXFont::ON) {
1339 // First check, if there will be two blanks together or a blank at
1340 // the beginning of a paragraph.
1341 // I decided to handle blanks like normal characters, the main
1342 // difference are the special checks when calculating the row.fill
1343 // (blank does not count at the end of a row) and the check here
1345 // The bug is triggered when we type in a description environment:
1346 // The current_font is not changed when we go from label to main text
1347 // and it should (along with realtmpfont) when we type the space.
1348 // CHECK There is a bug here! (Asger)
1350 // store the current font. This is because of the use of cursor
1351 // movements. The moving cursor would refresh the current font
1352 LyXFont realtmpfont = real_current_font;
1353 LyXFont rawtmpfont = current_font;
1355 if (!freeSpacing && IsLineSeparatorChar(c)) {
1356 if ((cursor.pos() > 0
1357 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1358 || (cursor.pos() > 0
1359 && cursorPar()->isNewline(cursor.pos() - 1))
1360 || (cursor.pos() == 0)) {
1361 static bool sent_space_message = false;
1362 if (!sent_space_message) {
1363 if (cursor.pos() == 0)
1364 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1366 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1367 sent_space_message = true;
1374 // Here case LyXText::InsertInset already inserted the character
1375 if (c != Paragraph::META_INSET)
1376 cursorPar()->insertChar(cursor.pos(), c);
1378 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1380 current_font = rawtmpfont;
1381 real_current_font = realtmpfont;
1382 redoParagraph(cursorPar());
1383 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1389 void LyXText::charInserted()
1391 // Here we could call finishUndo for every 20 characters inserted.
1392 // This is from my experience how emacs does it. (Lgb)
1393 static unsigned int counter;
1403 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1404 RowList::iterator const rit) const
1406 double w = rit->fill();
1407 double fill_hfill = 0;
1408 double fill_label_hfill = 0;
1409 double fill_separator = 0;
1413 pit->isRightToLeftPar(bv()->buffer()->params());
1415 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1417 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1419 // is there a manual margin with a manual label
1420 LyXLayout_ptr const & layout = pit->layout();
1422 if (layout->margintype == MARGIN_MANUAL
1423 && layout->labeltype == LABEL_MANUAL) {
1424 /// We might have real hfills in the label part
1425 int nlh = numberOfLabelHfills(*pit, rit);
1427 // A manual label par (e.g. List) has an auto-hfill
1428 // between the label text and the body of the
1430 // But we don't want to do this auto hfill if the par
1435 if (nlh && !pit->getLabelWidthString().empty()) {
1436 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1440 // are there any hfills in the row?
1441 int const nh = numberOfHfills(*pit, rit);
1445 fill_hfill = w / nh;
1446 // we don't have to look at the alignment if it is ALIGN_LEFT and
1447 // if the row is already larger then the permitted width as then
1448 // we force the LEFT_ALIGN'edness!
1449 } else if (int(rit->width()) < workWidth()) {
1450 // is it block, flushleft or flushright?
1451 // set x how you need it
1453 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1454 align = layout->align;
1456 align = pit->params().align();
1458 InsetOld * inset = 0;
1459 // ERT insets should always be LEFT ALIGNED on screen
1460 inset = pit->inInset();
1461 if (inset && inset->owner() &&
1462 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1464 align = LYX_ALIGN_LEFT;
1468 case LYX_ALIGN_BLOCK:
1470 int const ns = numberOfSeparators(*pit, rit);
1471 RowList::iterator next_row = boost::next(rit);
1473 && next_row != pit->rows.end()
1474 && !pit->isNewline(next_row->pos() - 1)
1476 fill_separator = w / ns;
1477 } else if (is_rtl) {
1482 case LYX_ALIGN_RIGHT:
1485 case LYX_ALIGN_CENTER:
1491 computeBidiTables(pit, *bv()->buffer(), rit);
1493 pos_type body_pos = pit->beginningOfBody();
1494 pos_type last = lastPos(*pit, rit);
1497 (body_pos - 1 > last ||
1498 !pit->isLineSeparator(body_pos - 1))) {
1499 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1500 if (body_pos - 1 <= last)
1501 x += fill_label_hfill;
1505 rit->fill_hfill(fill_hfill);
1506 rit->fill_label_hfill(fill_label_hfill);
1507 rit->fill_separator(fill_separator);
1512 // important for the screen
1515 // the cursor set functions have a special mechanism. When they
1516 // realize, that you left an empty paragraph, they will delete it.
1517 // They also delete the corresponding row
1519 void LyXText::cursorRightOneWord()
1521 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1522 setCursor(cursorPar(), cursor.pos());
1526 // Skip initial whitespace at end of word and move cursor to *start*
1527 // of prior word, not to end of next prior word.
1528 void LyXText::cursorLeftOneWord()
1530 LyXCursor tmpcursor = cursor;
1531 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1532 setCursor(getPar(tmpcursor), tmpcursor.pos());
1536 void LyXText::selectWord(word_location loc)
1538 LyXCursor from = cursor;
1539 LyXCursor to = cursor;
1540 ::getWord(*this, from, to, loc, ownerParagraphs());
1542 setCursor(from.par(), from.pos());
1545 selection.cursor = cursor;
1546 setCursor(to.par(), to.pos());
1551 // Select the word currently under the cursor when no
1552 // selection is currently set
1553 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1555 if (!selection.set()) {
1557 return selection.set();
1563 void LyXText::acceptChange()
1565 if (!selection.set() && cursorPar()->size())
1568 if (selection.start.par() == selection.end.par()) {
1569 LyXCursor & startc = selection.start;
1570 LyXCursor & endc = selection.end;
1571 recordUndo(bv(), Undo::INSERT, ownerParagraphs(), startc.par());
1572 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1575 redoParagraph(getPar(startc));
1576 setCursorIntern(startc.par(), 0);
1578 #warning handle multi par selection
1582 void LyXText::rejectChange()
1584 if (!selection.set() && cursorPar()->size())
1587 if (selection.start.par() == selection.end.par()) {
1588 LyXCursor & startc = selection.start;
1589 LyXCursor & endc = selection.end;
1590 recordUndo(bv(), Undo::INSERT, ownerParagraphs(), startc.par());
1591 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1594 redoParagraph(getPar(startc));
1595 setCursorIntern(startc.par(), 0);
1597 #warning handle multi par selection
1601 // This function is only used by the spellchecker for NextWord().
1602 // It doesn't handle LYX_ACCENTs and probably never will.
1603 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1605 if (the_locking_inset) {
1606 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1607 if (!word.word().empty()) {
1608 value += float(cursor.y());
1609 value /= float(height);
1612 // we have to go on checking so move cursor to the next char
1613 if (cursor.pos() == cursorPar()->size()) {
1614 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1616 cursor.par(cursor.par() + 1);
1619 cursor.pos(cursor.pos() + 1);
1621 int const tmppar = cursor.par();
1623 // If this is not the very first word, skip rest of
1624 // current word because we are probably in the middle
1625 // of a word if there is text here.
1626 if (cursor.pos() || cursor.par() != 0) {
1627 while (cursor.pos() < cursorPar()->size()
1628 && cursorPar()->isLetter(cursor.pos()))
1629 cursor.pos(cursor.pos() + 1);
1632 // Now, skip until we have real text (will jump paragraphs)
1634 ParagraphList::iterator cpit = cursorPar();
1635 pos_type const cpos = cursor.pos();
1637 if (cpos == cpit->size()) {
1638 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1639 cursor.par(cursor.par() + 1);
1646 bool const is_good_inset = cpit->isInset(cpos)
1647 && cpit->getInset(cpos)->allowSpellcheck();
1649 if (!isDeletedText(*cpit, cpos)
1650 && (is_good_inset || cpit->isLetter(cpos)))
1653 cursor.pos(cpos + 1);
1656 // now check if we hit an inset so it has to be a inset containing text!
1657 if (cursor.pos() < cursorPar()->size() &&
1658 cursorPar()->isInset(cursor.pos())) {
1660 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1661 cursorPar()->getInset(cursor.pos())->localDispatch(cmd);
1662 // now call us again to do the above trick
1663 // but obviously we have to start from down below ;)
1664 return bv()->text->selectNextWordToSpellcheck(value);
1667 // Update the value if we changed paragraphs
1668 if (cursor.par() != tmppar) {
1669 setCursor(cursor.par(), cursor.pos());
1670 value = float(cursor.y())/float(height);
1673 // Start the selection from here
1674 selection.cursor = cursor;
1676 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1677 // and find the end of the word (insets like optional hyphens
1678 // and ligature break are part of a word)
1679 while (cursor.pos() < cursorPar()->size()
1680 && cursorPar()->isLetter(cursor.pos())
1681 && !isDeletedText(*cursorPar(), cursor.pos()))
1682 cursor.pos(cursor.pos() + 1);
1684 // Finally, we copy the word to a string and return it
1686 if (selection.cursor.pos() < cursor.pos()) {
1688 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1689 if (!cursorPar()->isInset(i))
1690 str += cursorPar()->getChar(i);
1693 return WordLangTuple(str, lang_code);
1697 // This one is also only for the spellchecker
1698 void LyXText::selectSelectedWord()
1700 if (the_locking_inset) {
1701 the_locking_inset->selectSelectedWord(bv());
1704 // move cursor to the beginning
1705 setCursor(selection.cursor.par(), selection.cursor.pos());
1707 // set the sel cursor
1708 selection.cursor = cursor;
1710 // now find the end of the word
1711 while (cursor.pos() < cursorPar()->size()
1712 && cursorPar()->isLetter(cursor.pos()))
1713 cursor.pos(cursor.pos() + 1);
1715 setCursor(cursorPar(), cursor.pos());
1717 // finally set the selection
1722 // Delete from cursor up to the end of the current or next word.
1723 void LyXText::deleteWordForward()
1725 if (cursorPar()->empty())
1728 LyXCursor tmpcursor = cursor;
1729 selection.set(true); // to avoid deletion
1730 cursorRightOneWord();
1731 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1732 selection.cursor = cursor;
1736 // Great, CutSelection() gets rid of multiple spaces.
1737 cutSelection(true, false);
1742 // Delete from cursor to start of current or prior word.
1743 void LyXText::deleteWordBackward()
1745 if (cursorPar()->empty())
1748 LyXCursor tmpcursor = cursor;
1749 selection.set(true); // to avoid deletion
1750 cursorLeftOneWord();
1751 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1752 selection.cursor = cursor;
1755 cutSelection(true, false);
1760 // Kill to end of line.
1761 void LyXText::deleteLineForward()
1763 if (cursorPar()->empty())
1764 // Paragraph is empty, so we just go to the right
1767 LyXCursor tmpcursor = cursor;
1768 // We can't store the row over a regular setCursor
1769 // so we set it to 0 and reset it afterwards.
1770 selection.set(true); // to avoid deletion
1772 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1773 selection.cursor = cursor;
1776 // What is this test for ??? (JMarc)
1777 if (!selection.set()) {
1778 deleteWordForward();
1780 cutSelection(true, false);
1786 void LyXText::changeCase(LyXText::TextCase action)
1791 if (selection.set()) {
1792 from = selection.start;
1796 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1797 setCursor(to.par(), to.pos() + 1);
1800 recordUndo(bv(), Undo::ATOMIC, ownerParagraphs(), from.par(), to.par());
1802 pos_type pos = from.pos();
1803 int par = from.par();
1805 while (par != int(ownerParagraphs().size()) &&
1806 (pos != to.pos() || par != to.par())) {
1807 ParagraphList::iterator pit = getPar(par);
1808 if (pos == pit->size()) {
1813 unsigned char c = pit->getChar(pos);
1814 if (c != Paragraph::META_INSET) {
1816 case text_lowercase:
1819 case text_capitalization:
1821 action = text_lowercase;
1823 case text_uppercase:
1829 pit->setChar(pos, c);
1835 void LyXText::Delete()
1837 // this is a very easy implementation
1839 LyXCursor old_cursor = cursor;
1840 int const old_cur_par_id = cursorPar()->id();
1841 int const old_cur_par_prev_id =
1842 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1844 // just move to the right
1847 // CHECK Look at the comment here.
1848 // This check is not very good...
1849 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1850 // and that can very well delete the par or par->previous in
1851 // old_cursor. Will a solution where we compare paragraph id's
1853 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1854 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1855 // delete-empty-paragraph-mechanism has done it
1859 // if you had success make a backspace
1860 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1861 recordUndo(bv(), Undo::DELETE, ownerParagraphs(), old_cursor.par());
1867 void LyXText::backspace()
1869 // Get the font that is used to calculate the baselineskip
1870 ParagraphList::iterator pit = cursorPar();
1871 pos_type lastpos = pit->size();
1873 if (cursor.pos() == 0) {
1874 // The cursor is at the beginning of a paragraph,
1875 // so the the backspace will collapse two paragraphs into one.
1877 // but it's not allowed unless it's new
1878 if (pit->isChangeEdited(0, pit->size()))
1881 // we may paste some paragraphs
1883 // is it an empty paragraph?
1885 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1886 // This is an empty paragraph and we delete it just
1887 // by moving the cursor one step
1888 // left and let the DeleteEmptyParagraphMechanism
1889 // handle the actual deletion of the paragraph.
1892 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1893 if (cursorPar()->layout() == tmppit->layout()
1894 && cursorPar()->getAlign() == tmppit->getAlign()) {
1895 // Inherit bottom DTD from the paragraph below.
1896 // (the one we are deleting)
1897 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1898 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1899 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1904 // the layout things can change the height of a row !
1910 if (cursor.par() != 0)
1911 recordUndo(bv(), Undo::DELETE, ownerParagraphs(),
1912 cursor.par() - 1, cursor.par());
1914 ParagraphList::iterator tmppit = cursorPar();
1915 // We used to do cursorLeftIntern() here, but it is
1916 // not a good idea since it triggers the auto-delete
1917 // mechanism. So we do a cursorLeftIntern()-lite,
1918 // without the dreaded mechanism. (JMarc)
1919 if (cursor.par() != 0) {
1920 // steps into the above paragraph.
1921 setCursorIntern(cursor.par() - 1,
1922 getPar(cursor.par() - 1)->size(),
1926 // Pasting is not allowed, if the paragraphs have different
1927 // layout. I think it is a real bug of all other
1928 // word processors to allow it. It confuses the user.
1929 // Correction: Pasting is always allowed with standard-layout
1930 Buffer & buf = *bv()->buffer();
1931 BufferParams const & bufparams = buf.params();
1932 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1934 if (cursorPar() != tmppit
1935 && (cursorPar()->layout() == tmppit->layout()
1936 || tmppit->layout() == tclass.defaultLayout())
1937 && cursorPar()->getAlign() == tmppit->getAlign()) {
1938 mergeParagraph(bufparams,
1939 buf.paragraphs(), cursorPar());
1941 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1942 cursor.pos(cursor.pos() - 1);
1944 // the row may have changed, block, hfills etc.
1946 setCursor(cursor.par(), cursor.pos(), false);
1949 // this is the code for a normal backspace, not pasting
1951 recordUndo(bv(), Undo::DELETE, ownerParagraphs(), cursor.par());
1952 // We used to do cursorLeftIntern() here, but it is
1953 // not a good idea since it triggers the auto-delete
1954 // mechanism. So we do a cursorLeftIntern()-lite,
1955 // without the dreaded mechanism. (JMarc)
1956 setCursorIntern(cursor.par(), cursor.pos() - 1,
1957 false, cursor.boundary());
1958 cursorPar()->erase(cursor.pos());
1961 lastpos = cursorPar()->size();
1962 if (cursor.pos() == lastpos)
1966 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1970 ParagraphList::iterator LyXText::cursorPar() const
1972 return getPar(cursor.par());
1976 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1978 return getPar(cur.par());
1982 ParagraphList::iterator LyXText::getPar(int par) const
1984 BOOST_ASSERT(par >= 0);
1985 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1986 ParagraphList::iterator pit = ownerParagraphs().begin();
1987 std::advance(pit, par);
1993 RowList::iterator LyXText::cursorRow() const
1995 return getRow(cursorPar(), cursor.pos());
1999 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2001 return getRow(getPar(cur), cur.pos());
2006 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
2008 RowList::iterator rit = boost::prior(pit->rows.end());
2009 RowList::iterator const begin = pit->rows.begin();
2011 while (rit != begin && rit->pos() > pos)
2018 // returns pointer to some fancy row 'below' specified row
2019 RowList::iterator LyXText::cursorIRow() const
2021 return getRow(cursorPar(), cursor.pos());
2025 RowList::iterator LyXText::getRowNearY(int y,
2026 ParagraphList::iterator & pit) const
2028 //lyxerr << "getRowNearY: y " << y << endl;
2030 pit = boost::prior(ownerParagraphs().end());
2032 RowList::iterator rit = lastRow();
2033 RowList::iterator rbegin = firstRow();
2035 while (rit != rbegin && static_cast<int>(rit->y()) > y)
2036 previousRow(pit, rit);
2042 int LyXText::getDepth() const
2044 return cursorPar()->getDepth();
2048 RowList::iterator LyXText::firstRow() const
2050 return ownerParagraphs().front().rows.begin();
2054 RowList::iterator LyXText::lastRow() const
2056 return boost::prior(endRow());
2060 RowList::iterator LyXText::endRow() const
2062 return ownerParagraphs().back().rows.end();
2066 void LyXText::nextRow(ParagraphList::iterator & pit,
2067 RowList::iterator & rit) const
2070 if (rit == pit->rows.end()) {
2072 if (pit == ownerParagraphs().end())
2075 rit = pit->rows.begin();
2080 void LyXText::previousRow(ParagraphList::iterator & pit,
2081 RowList::iterator & rit) const
2083 if (rit != pit->rows.begin())
2086 BOOST_ASSERT(pit != ownerParagraphs().begin());
2088 rit = boost::prior(pit->rows.end());
2093 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
2095 if (!selection.set())
2098 // should be const ...
2099 ParagraphList::iterator startpit = getPar(selection.start);
2100 ParagraphList::iterator endpit = getPar(selection.end);
2101 size_t const startpos = selection.start.pos();
2102 size_t const endpos = selection.end.pos();
2104 if (startpit == endpit)
2105 return startpit->asString(buffer, startpos, endpos, label);
2107 // First paragraph in selection
2109 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
2111 // The paragraphs in between (if any)
2112 ParagraphList::iterator pit = startpit;
2113 for (++pit; pit != endpit; ++pit)
2114 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
2116 // Last paragraph in selection
2117 result += endpit->asString(buffer, 0, endpos, label);
2123 int LyXText::parOffset(ParagraphList::iterator pit) const
2125 return std::distance(ownerParagraphs().begin(), pit);