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 "BufferView.h"
25 #include "funcrequest.h"
30 #include "lyxrow_funcs.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "rowpainter.h"
34 #include "text_funcs.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/LyXView.h"
41 #include "insets/insettext.h"
43 #include "support/LAssert.h"
44 #include "support/lstrings.h"
45 #include "support/textutils.h"
48 using namespace lyx::support;
54 using lyx::word_location;
56 using namespace bv_funcs;
58 /// top, right, bottom pixel margin
59 extern int const PAPER_MARGIN = 20;
60 /// margin for changebar
61 extern int const CHANGEBAR_MARGIN = 10;
63 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
67 int bibitemMaxWidth(BufferView *, LyXFont const &);
70 BufferView * LyXText::bv()
72 Assert(bv_owner != 0);
77 BufferView * LyXText::bv() const
79 Assert(bv_owner != 0);
84 void LyXText::updateRowPositions()
86 ParagraphList::iterator pit = ownerParagraphs().begin();
87 ParagraphList::iterator end = ownerParagraphs().end();
88 for (height = 0; pit != end; ++pit) {
89 RowList::iterator rit = pit->rows.begin();
90 RowList::iterator rend = pit->rows.end();
91 for ( ; rit != rend ; rit = ++rit) {
93 height += rit->height();
99 int LyXText::workWidth() const
101 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
105 int LyXText::getRealCursorX() const
108 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
109 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
114 #warning FIXME This function seems to belong outside of LyxText.
115 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
118 if (!Encodings::is_arabic(c))
119 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
120 return c + (0xb0 - '0');
124 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
125 unsigned char next_char = ' ';
127 pos_type const par_size = par.size();
129 for (pos_type i = pos + 1; i < par_size; ++i) {
130 unsigned char const par_char = par.getChar(i);
131 if (!Encodings::IsComposeChar_arabic(par_char)) {
132 next_char = par_char;
137 if (Encodings::is_arabic(next_char)) {
138 if (Encodings::is_arabic(prev_char) &&
139 !Encodings::is_arabic_special(prev_char))
140 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
142 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
144 if (Encodings::is_arabic(prev_char) &&
145 !Encodings::is_arabic_special(prev_char))
146 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
148 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
152 // This is the comments that some of the warnings below refers to.
153 // There are some issues in this file and I don't think they are
154 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
155 // this is a problem that has been here almost from day one and that a
156 // larger userbase with differenct access patters triggers the bad
157 // behaviour. (segfaults.) What I think happen is: In several places
158 // we store the paragraph in the current cursor and then moves the
159 // cursor. This movement of the cursor will delete paragraph at the
160 // old position if it is now empty. This will make the temporary
161 // pointer to the old cursor paragraph invalid and dangerous to use.
162 // And is some cases this will trigger a segfault. I have marked some
163 // of the cases where this happens with a warning, but I am sure there
164 // are others in this file and in text2.C. There is also a note in
165 // Delete() that you should read. In Delete I store the paragraph->id
166 // instead of a pointer to the paragraph. I am pretty sure this faulty
167 // use of temporary pointers to paragraphs that might have gotten
168 // invalidated (through a cursor movement) before they are used, are
169 // the cause of the strange crashes we get reported often.
171 // It is very tiresom to change this code, especially when it is as
172 // hard to read as it is. Help to fix all the cases where this is done
173 // would be greately appreciated.
177 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
179 if (pos >= pit->size())
182 char const c = pit->getChar(pos);
183 LyXFont const & font = getFont(pit, pos);
184 return singleWidth(pit, pos, c, font);
188 int LyXText::singleWidth(ParagraphList::iterator pit,
189 pos_type pos, char c, LyXFont const & font) const
191 if (pos >= pit->size()) {
192 lyxerr << "in singleWidth(), pos: " << pos << endl;
198 // The most common case is handled first (Asger)
199 if (IsPrintable(c)) {
200 if (font.language()->RightToLeft()) {
201 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
202 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
203 && font.language()->lang() == "arabic") {
204 if (Encodings::IsComposeChar_arabic(c))
207 c = transformChar(c, *pit, pos);
208 } else if (font.language()->lang() == "hebrew" &&
209 Encodings::IsComposeChar_hebrew(c))
212 return font_metrics::width(c, font);
215 if (c == Paragraph::META_INSET) {
216 InsetOld * tmpinset = pit->getInset(pos);
218 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
219 // Because of the representation as vertical lines
222 return tmpinset->width();
225 if (IsSeparatorChar(c))
227 return font_metrics::width(c, font);
231 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
233 if (bidi_start == -1)
236 return log2vis_list[pos - bidi_start];
240 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
242 if (bidi_start == -1)
245 return vis2log_list[pos - bidi_start];
249 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
251 if (bidi_start == -1)
254 return bidi_levels[pos - bidi_start];
258 bool LyXText::bidi_InRange(lyx::pos_type pos) const
260 return bidi_start == -1 ||
261 (bidi_start <= pos && pos <= bidi_end);
265 void LyXText::computeBidiTables(ParagraphList::iterator pit,
266 Buffer const & buf, RowList::iterator row) const
268 bidi_same_direction = true;
269 if (!lyxrc.rtl_support) {
274 InsetOld * inset = pit->inInset();
275 if (inset && inset->owner() &&
276 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
281 bidi_start = row->pos();
282 bidi_end = lastPos(*pit, row);
284 if (bidi_start > bidi_end) {
289 if (bidi_end + 2 - bidi_start >
290 static_cast<pos_type>(log2vis_list.size())) {
292 (bidi_end + 2 - bidi_start < 500) ?
293 500 : 2 * (bidi_end + 2 - bidi_start);
294 log2vis_list.resize(new_size);
295 vis2log_list.resize(new_size);
296 bidi_levels.resize(new_size);
299 vis2log_list[bidi_end + 1 - bidi_start] = -1;
300 log2vis_list[bidi_end + 1 - bidi_start] = -1;
304 pit->isRightToLeftPar(buf.params);
308 pos_type const body_pos = pit->beginningOfBody();
310 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
311 bool is_space = pit->isLineSeparator(lpos);
313 (is_space && lpos + 1 <= bidi_end &&
314 !pit->isLineSeparator(lpos + 1) &&
315 !pit->isNewline(lpos + 1))
317 LyXFont font = pit->getFontSettings(buf.params, pos);
318 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
319 font.number() == LyXFont::ON &&
320 pit->getFontSettings(buf.params, lpos - 1).number()
322 font = pit->getFontSettings(buf.params, lpos);
327 bool new_rtl = font.isVisibleRightToLeft();
328 bool new_rtl0 = font.isRightToLeft();
331 if (lpos == body_pos - 1
332 && row->pos() < body_pos - 1
334 new_level = (rtl_par) ? 1 : 0;
335 new_rtl = new_rtl0 = rtl_par;
337 new_level = (new_rtl) ? 1 : 2;
339 new_level = (rtl_par) ? 2 : 0;
341 if (is_space && new_level >= level) {
347 int new_level2 = new_level;
349 if (level == new_level && rtl0 != new_rtl0) {
351 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
352 } else if (level < new_level) {
353 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
354 if (new_level > rtl_par)
355 bidi_same_direction = false;
357 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
360 bidi_levels[lpos - bidi_start] = new_level;
362 while (level > new_level2) {
363 pos_type old_lpos = stack[--level];
364 int delta = lpos - old_lpos - 1;
367 log2vis_list[lpos - bidi_start] += delta;
368 log2vis_list[old_lpos - bidi_start] += delta;
370 while (level < new_level)
371 stack[level++] = lpos;
375 pos_type const old_lpos = stack[--level];
376 int delta = bidi_end - old_lpos;
379 log2vis_list[old_lpos - bidi_start] += delta;
382 pos_type vpos = bidi_start - 1;
383 for (pos_type lpos = bidi_start;
384 lpos <= bidi_end; ++lpos) {
385 vpos += log2vis_list[lpos - bidi_start];
386 vis2log_list[vpos - bidi_start] = lpos;
387 log2vis_list[lpos - bidi_start] = vpos;
392 // This method requires a previous call to ComputeBidiTables()
393 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
396 if (!lyxrc.rtl_support || pos == 0)
399 if (!bidi_InRange(pos - 1)) {
400 /// This can happen if pos is the first char of a row.
401 /// Returning false in this case is incorrect!
405 bool const rtl = bidi_level(pos - 1) % 2;
406 bool const rtl2 = bidi_InRange(pos)
407 ? bidi_level(pos) % 2
408 : par.isRightToLeftPar(buf.params);
413 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
414 pos_type pos, LyXFont const & font) const
416 if (!lyxrc.rtl_support)
417 return false; // This is just for speedup
419 bool const rtl = font.isVisibleRightToLeft();
420 bool const rtl2 = bidi_InRange(pos)
421 ? bidi_level(pos) % 2
422 : par.isRightToLeftPar(buf.params);
427 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
429 LyXTextClass const & tclass =
430 bv()->buffer()->params.getLyXTextClass();
431 LyXLayout_ptr const & layout = pit->layout();
433 string parindent = layout->parindent;
437 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
439 // this is the way, LyX handles the LaTeX-Environments.
440 // I have had this idea very late, so it seems to be a
441 // later added hack and this is true
442 if (!pit->getDepth()) {
443 if (pit->layout() == tclass.defaultLayout()) {
444 // find the previous same level paragraph
445 if (pit != ownerParagraphs().begin()) {
446 ParagraphList::iterator newpit =
447 depthHook(pit, ownerParagraphs(),
450 newpit->layout()->nextnoindent)
455 // find the next level paragraph
457 ParagraphList::iterator newpar = outerHook(pit,
460 // make a corresponding row. Needed to call leftMargin()
462 // check wether it is a sufficent paragraph
463 if (newpar != ownerParagraphs().end() &&
464 newpar->layout()->isEnvironment()) {
465 x = leftMargin(newpar, Row(newpar->size()));
468 if (newpar != ownerParagraphs().end() &&
469 pit->layout() == tclass.defaultLayout()) {
470 if (newpar->params().noindent())
473 parindent = newpar->layout()->parindent;
479 LyXFont const labelfont = getLabelFont(pit);
480 switch (layout->margintype) {
482 if (!layout->leftmargin.empty()) {
483 x += font_metrics::signedWidth(layout->leftmargin,
484 tclass.defaultfont());
486 if (!pit->getLabelstring().empty()) {
487 x += font_metrics::signedWidth(layout->labelindent,
489 x += font_metrics::width(pit->getLabelstring(),
491 x += font_metrics::width(layout->labelsep, labelfont);
495 x += font_metrics::signedWidth(layout->labelindent, labelfont);
496 // The width of an empty par, even with manual label, should be 0
497 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
498 if (!pit->getLabelWidthString().empty()) {
499 x += font_metrics::width(pit->getLabelWidthString(),
501 x += font_metrics::width(layout->labelsep, labelfont);
506 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
507 / (pit->getDepth() + 4);
509 case MARGIN_FIRST_DYNAMIC:
510 if (layout->labeltype == LABEL_MANUAL) {
511 if (row.pos() >= pit->beginningOfBody()) {
512 x += font_metrics::signedWidth(layout->leftmargin,
515 x += font_metrics::signedWidth(layout->labelindent,
519 // Special case to fix problems with
521 || (layout->labeltype == LABEL_STATIC
522 && layout->latextype == LATEX_ENVIRONMENT
523 && !isFirstInSequence(pit, ownerParagraphs()))) {
524 x += font_metrics::signedWidth(layout->leftmargin,
526 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
527 && layout->labeltype != LABEL_BIBLIO
528 && layout->labeltype !=
529 LABEL_CENTERED_TOP_ENVIRONMENT) {
530 x += font_metrics::signedWidth(layout->labelindent,
532 x += font_metrics::width(layout->labelsep, labelfont);
533 x += font_metrics::width(pit->getLabelstring(),
538 case MARGIN_RIGHT_ADDRESS_BOX:
540 // ok, a terrible hack. The left margin depends on the widest
541 // row in this paragraph. Do not care about footnotes, they
542 // are *NOT* allowed in the LaTeX realisation of this layout.
544 // find the first row of this paragraph
545 RowList::iterator rit = pit->rows.begin();
546 RowList::iterator end = pit->rows.end();
547 int minfill = rit->fill();
548 for ( ; rit != end; ++rit)
549 if (rit->fill() < minfill)
550 minfill = rit->fill();
552 x += font_metrics::signedWidth(layout->leftmargin,
553 tclass.defaultfont());
559 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
560 LyXLength const len = pit->params().leftIndent();
561 int const tw = inset_owner ?
562 inset_owner->latexTextWidth(bv()) : workWidth();
563 x += len.inPixels(tw);
568 if (pit->params().align() == LYX_ALIGN_LAYOUT)
569 align = layout->align;
571 align = pit->params().align();
573 // set the correct parindent
574 if (row.pos() == 0) {
575 if ((layout->labeltype == LABEL_NO_LABEL
576 || layout->labeltype == LABEL_TOP_ENVIRONMENT
577 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
578 || (layout->labeltype == LABEL_STATIC
579 && layout->latextype == LATEX_ENVIRONMENT
580 && !isFirstInSequence(pit, ownerParagraphs())))
581 && align == LYX_ALIGN_BLOCK
582 && !pit->params().noindent()
583 // in tabulars and ert paragraphs are never indented!
584 && (!pit->inInset() || !pit->inInset()->owner() ||
585 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
586 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
587 && (pit->layout() != tclass.defaultLayout() ||
588 bv()->buffer()->params.paragraph_separation ==
589 BufferParams::PARSEP_INDENT)) {
590 x += font_metrics::signedWidth(parindent,
591 tclass.defaultfont());
592 } else if (layout->labeltype == LABEL_BIBLIO) {
593 // ale970405 Right width for bibitems
594 x += bibitemMaxWidth(bv(), tclass.defaultfont());
602 int LyXText::rightMargin(ParagraphList::iterator pit,
603 Buffer const & buf, Row const &) const
605 LyXTextClass const & tclass = buf.params.getLyXTextClass();
606 LyXLayout_ptr const & layout = pit->layout();
609 + font_metrics::signedWidth(tclass.rightmargin(),
610 tclass.defaultfont());
611 + font_metrics::signedWidth(layout->rightmargin,
612 tclass.defaultfont())
613 * 4 / (pit->getDepth() + 4);
617 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
619 if (pit->layout()->margintype == MARGIN_MANUAL) {
621 tmprow.pos(pit->size());
622 // return the beginning of the body
623 return leftMargin(pit, tmprow);
626 // LabelEnd is only needed if the layout
627 // fills a flushleft label.
634 // this needs special handling - only newlines count as a break point
635 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
637 for (; i < par.size(); ++i) {
638 if (par.isNewline(i))
648 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
649 Row const & row) const
651 // maximum pixel width of a row.
652 int width = workWidth()
653 - rightMargin(pit, *bv()->buffer(), row);
655 // inset->textWidth() returns -1 via workWidth(),
660 LyXLayout_ptr const & layout = pit->layout();
662 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
663 return addressBreakPoint(row.pos(), *pit);
665 pos_type const pos = row.pos();
666 pos_type const body_pos = pit->beginningOfBody();
667 pos_type const last = pit->size();
668 pos_type point = last;
673 // Now we iterate through until we reach the right margin
674 // or the end of the par, then choose the possible break
677 int const left = leftMargin(pit, row);
680 // pixel width since last breakpoint
685 // We re-use the font resolution for the entire font span when possible
686 LyXFont font = getFont(pit, i);
687 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
689 for (; i < last; ++i) {
690 if (pit->isNewline(i)) {
695 char const c = pit->getChar(i);
696 if (i > endPosOfFontSpan) {
697 font = getFont(pit, i);
698 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
703 // add the auto-hfill from label end to the body
704 if (body_pos && i == body_pos) {
705 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
706 if (pit->isLineSeparator(i - 1))
707 thiswidth -= singleWidth(pit, i - 1);
708 int left_margin = labelEnd(pit, row);
709 if (thiswidth + x < left_margin)
710 thiswidth = left_margin - x;
711 thiswidth += singleWidth(pit, i, c, font);
713 thiswidth = singleWidth(pit, i, c, font);
717 chunkwidth += thiswidth;
719 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
721 // break before a character that will fall off
722 // the right of the row
724 // if no break before, break here
725 if (point == last || chunkwidth >= (width - left)) {
734 if (!in || in->isChar()) {
735 // some insets are line separators too
736 if (pit->isLineSeparator(i)) {
746 if (point == last && x >= width) {
747 // didn't find one, break at the point we reached the edge
749 } else if (i == last && x < width) {
750 // found one, but we fell off the end of the par, so prefer
755 // manual labels cannot be broken in LaTeX. But we
756 // want to make our on-screen rendering of footnotes
758 if (body_pos && point < body_pos)
759 point = body_pos - 1;
765 // returns the minimum space a row needs on the screen in pixel
766 int LyXText::fill(ParagraphList::iterator pit,
767 RowList::iterator row, int paper_width) const
773 // get the pure distance
774 pos_type const last = lastPos(*pit, row);
776 LyXLayout_ptr const & layout = pit->layout();
778 // special handling of the right address boxes
779 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
780 int const tmpfill = row->fill();
781 row->fill(0); // the minfill in MarginLeft()
782 w = leftMargin(pit, *row);
785 w = leftMargin(pit, *row);
787 pos_type const body_pos = pit->beginningOfBody();
788 pos_type i = row->pos();
790 if (! pit->empty() && i <= last) {
791 // We re-use the font resolution for the entire span when possible
792 LyXFont font = getFont(pit, i);
793 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
795 if (body_pos > 0 && i == body_pos) {
796 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
797 if (pit->isLineSeparator(i - 1))
798 w -= singleWidth(pit, i - 1);
799 int left_margin = labelEnd(pit, *row);
803 char const c = pit->getChar(i);
804 if (IsPrintable(c) && i > endPosOfFontSpan) {
805 // We need to get the next font
806 font = getFont(pit, i);
807 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
809 w += singleWidth(pit, i, c, font);
813 if (body_pos > 0 && body_pos > last) {
814 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
815 if (last >= 0 && pit->isLineSeparator(last))
816 w -= singleWidth(pit, last);
817 int const left_margin = labelEnd(pit, *row);
822 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
824 // If this case happens, it means that our calculation
825 // of the widths of the chars when we do rowBreakPoint()
826 // went wrong for some reason. Typically in list bodies.
827 // Things just about hobble on anyway, though you'll end
828 // up with a "fill_separator" less than zero, which corresponds
829 // to inter-word spacing being too small. Hopefully this problem
830 // will die when the label hacks die.
831 if (lyxerr.debugging() && fill < 0) {
832 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
833 << " w " << w << " paper_width " << paper_width
834 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
840 // returns the minimum space a manual label needs on the screen in pixel
841 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
843 pos_type last = pit->beginningOfBody();
847 // -1 because a label ends either with a space that is in the label,
848 // or with the beginning of a footnote that is outside the label.
851 // a separator at this end does not count
852 if (pit->isLineSeparator(last))
856 pos_type i = row.pos();
858 w += singleWidth(pit, i);
863 string const & labwidstr = pit->params().labelWidthString();
864 if (!labwidstr.empty()) {
865 LyXFont const labfont = getLabelFont(pit);
866 int const labwidth = font_metrics::width(labwidstr, labfont);
867 fill = max(labwidth - w, 0);
874 LColor::color LyXText::backgroundColor() const
877 return inset_owner->backgroundColor();
879 return LColor::background;
883 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
885 // get the maximum ascent and the maximum descent
886 double layoutasc = 0;
887 double layoutdesc = 0;
890 // ok, let us initialize the maxasc and maxdesc value.
891 // Only the fontsize count. The other properties
892 // are taken from the layoutfont. Nicer on the screen :)
893 LyXLayout_ptr const & layout = pit->layout();
895 // as max get the first character of this row then it can increase but not
896 // decrease the height. Just some point to start with so we don't have to
897 // do the assignment below too often.
898 LyXFont font = getFont(pit, rit->pos());
899 LyXFont::FONT_SIZE const tmpsize = font.size();
900 font = getLayoutFont(pit);
901 LyXFont::FONT_SIZE const size = font.size();
902 font.setSize(tmpsize);
904 LyXFont labelfont = getLabelFont(pit);
906 double spacing_val = 1.0;
907 if (!pit->params().spacing().isDefault())
908 spacing_val = pit->params().spacing().getValue();
910 spacing_val = bv()->buffer()->params.spacing.getValue();
911 //lyxerr << "spacing_val = " << spacing_val << endl;
913 int maxasc = int(font_metrics::maxAscent(font) *
914 layout->spacing.getValue() * spacing_val);
915 int maxdesc = int(font_metrics::maxDescent(font) *
916 layout->spacing.getValue() * spacing_val);
918 pos_type const pos_end = lastPos(*pit, rit);
923 // We re-use the font resolution for the entire font span when possible
924 LyXFont font = getFont(pit, rit->pos());
925 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
928 Paragraph const & par = *pit;
930 // Check if any insets are larger
931 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
932 // Manual inlined optimised version of common case of
933 // "maxwidth += singleWidth(pit, pos);"
934 char const c = par.getChar(pos);
936 if (IsPrintable(c)) {
937 if (pos > endPosOfFontSpan) {
938 // We need to get the next font
939 font = getFont(pit, pos);
940 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
942 if (! font.language()->RightToLeft()) {
943 maxwidth += font_metrics::width(c, font);
945 // Fall-back to normal case
946 maxwidth += singleWidth(pit, pos, c, font);
947 // And flush font cache
948 endPosOfFontSpan = 0;
951 // Special handling of insets - are any larger?
952 if (par.isInset(pos)) {
953 InsetOld const * tmpinset = par.getInset(pos);
955 maxwidth += tmpinset->width();
956 maxasc = max(maxasc, tmpinset->ascent());
957 maxdesc = max(maxdesc, tmpinset->descent());
960 // Fall-back to normal case
961 maxwidth += singleWidth(pit, pos, c, font);
962 // And flush font cache
963 endPosOfFontSpan = 0;
969 // Check if any custom fonts are larger (Asger)
970 // This is not completely correct, but we can live with the small,
971 // cosmetic error for now.
972 LyXFont::FONT_SIZE maxsize =
973 pit->highestFontInRange(rit->pos(), pos_end, size);
974 if (maxsize > font.size()) {
975 font.setSize(maxsize);
976 maxasc = max(maxasc, font_metrics::maxAscent(font));
977 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
980 // This is nicer with box insets:
984 rit->ascent_of_text(maxasc);
989 // some parksips VERY EASY IMPLEMENTATION
990 if (bv()->buffer()->params.paragraph_separation ==
991 BufferParams::PARSEP_SKIP)
993 if (layout->isParagraph()
994 && pit->getDepth() == 0
995 && pit != ownerParagraphs().begin())
997 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
998 } else if (pit != ownerParagraphs().begin() &&
999 boost::prior(pit)->layout()->isParagraph() &&
1000 boost::prior(pit)->getDepth() == 0)
1002 // is it right to use defskip here too? (AS)
1003 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1008 if (pit == ownerParagraphs().begin() && !isInInset())
1009 maxasc += PAPER_MARGIN;
1011 // add the vertical spaces, that the user added
1012 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1014 // do not forget the DTP-lines!
1015 // there height depends on the font of the nearest character
1016 if (pit->params().lineTop())
1018 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1019 // and now the pagebreaks
1020 if (pit->params().pagebreakTop())
1021 maxasc += 3 * defaultRowHeight();
1023 if (pit->params().startOfAppendix())
1024 maxasc += 3 * defaultRowHeight();
1026 // This is special code for the chapter, since the label of this
1027 // layout is printed in an extra row
1028 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1029 && bv()->buffer()->params.secnumdepth >= 0)
1031 float spacing_val = 1.0;
1032 if (!pit->params().spacing().isDefault()) {
1033 spacing_val = pit->params().spacing().getValue();
1035 spacing_val = bv()->buffer()->params.spacing.getValue();
1038 labeladdon = int(font_metrics::maxDescent(labelfont) *
1039 layout->spacing.getValue() *
1041 + int(font_metrics::maxAscent(labelfont) *
1042 layout->spacing.getValue() *
1046 // special code for the top label
1047 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1048 || layout->labeltype == LABEL_BIBLIO
1049 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1050 && isFirstInSequence(pit, ownerParagraphs())
1051 && !pit->getLabelstring().empty())
1053 float spacing_val = 1.0;
1054 if (!pit->params().spacing().isDefault()) {
1055 spacing_val = pit->params().spacing().getValue();
1057 spacing_val = bv()->buffer()->params.spacing.getValue();
1061 (font_metrics::maxAscent(labelfont) +
1062 font_metrics::maxDescent(labelfont)) *
1063 layout->spacing.getValue() *
1065 + layout->topsep * defaultRowHeight()
1066 + layout->labelbottomsep * defaultRowHeight());
1069 // And now the layout spaces, for example before and after
1070 // a section, or between the items of a itemize or enumerate
1073 if (!pit->params().pagebreakTop()) {
1074 ParagraphList::iterator prev =
1075 depthHook(pit, ownerParagraphs(),
1077 if (prev != pit && prev->layout() == layout &&
1078 prev->getDepth() == pit->getDepth() &&
1079 prev->getLabelWidthString() == pit->getLabelWidthString())
1081 layoutasc = (layout->itemsep * defaultRowHeight());
1082 } else if (rit != firstRow()) {
1083 tmptop = layout->topsep;
1085 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1086 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1089 layoutasc = (tmptop * defaultRowHeight());
1090 } else if (pit->params().lineTop()) {
1091 tmptop = layout->topsep;
1094 layoutasc = (tmptop * defaultRowHeight());
1097 prev = outerHook(pit, ownerParagraphs());
1098 if (prev != ownerParagraphs().end()) {
1099 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1100 } else if (pit != ownerParagraphs().begin()) {
1101 ParagraphList::iterator prior_pit = boost::prior(pit);
1102 if (prior_pit->getDepth() != 0 ||
1103 prior_pit->layout() == layout) {
1104 maxasc += int(layout->parsep * defaultRowHeight());
1110 // is it a bottom line?
1111 if (boost::next(rit) == pit->rows.end()) {
1112 // the bottom margin
1113 ParagraphList::iterator nextpit = boost::next(pit);
1114 if (nextpit == ownerParagraphs().end() && !isInInset())
1115 maxdesc += PAPER_MARGIN;
1117 // add the vertical spaces, that the user added
1118 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1120 // do not forget the DTP-lines!
1121 // there height depends on the font of the nearest character
1122 if (pit->params().lineBottom())
1123 maxdesc += 2 * font_metrics::ascent('x',
1124 getFont(pit, max(pos_type(0), pit->size() - 1)));
1126 // and now the pagebreaks
1127 if (pit->params().pagebreakBottom())
1128 maxdesc += 3 * defaultRowHeight();
1130 // and now the layout spaces, for example before and after
1131 // a section, or between the items of a itemize or enumerate
1133 if (!pit->params().pagebreakBottom()
1134 && nextpit != ownerParagraphs().end()) {
1135 ParagraphList::iterator comparepit = pit;
1139 if (comparepit->getDepth() > nextpit->getDepth()) {
1140 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1141 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1142 if (comparepit->layout()!= nextpit->layout()
1143 || nextpit->getLabelWidthString() !=
1144 comparepit->getLabelWidthString())
1146 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1148 if (unusual > usual)
1149 layoutdesc = unusual;
1152 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1154 if (comparepit->layout() != nextpit->layout()
1155 || nextpit->getLabelWidthString() !=
1156 comparepit->getLabelWidthString())
1157 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1162 // incalculate the layout spaces
1163 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1164 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1166 rit->height(maxasc + maxdesc + labeladdon);
1167 rit->baseline(maxasc + labeladdon);
1168 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1171 rit->width(int(maxwidth + x));
1173 width = max(0, workWidth());
1174 RowList::iterator rit = firstRow();
1175 RowList::iterator end = endRow();
1176 ParagraphList::iterator it = ownerParagraphs().begin();
1177 while (rit != end) {
1178 if (rit->width() > width)
1179 width = rit->width();
1186 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1188 // allow only if at start or end, or all previous is new text
1189 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1190 && cursor.par()->isChangeEdited(0, cursor.pos()))
1193 LyXTextClass const & tclass =
1194 bv()->buffer()->params.getLyXTextClass();
1195 LyXLayout_ptr const & layout = cursor.par()->layout();
1197 // this is only allowed, if the current paragraph is not empty or caption
1198 // and if it has not the keepempty flag active
1199 if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1200 && layout->labeltype != LABEL_SENSITIVE)
1203 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1205 // Always break behind a space
1207 // It is better to erase the space (Dekel)
1208 if (cursor.pos() < cursor.par()->size()
1209 && cursor.par()->isLineSeparator(cursor.pos()))
1210 cursor.par()->erase(cursor.pos());
1212 // break the paragraph
1216 keep_layout = layout->isEnvironment();
1218 // we need to set this before we insert the paragraph. IMO the
1219 // breakParagraph call should return a bool if it inserts the
1220 // paragraph before or behind and we should react on that one
1221 // but we can fix this in 1.3.0 (Jug 20020509)
1222 bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1223 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1224 cursor.pos(), keep_layout);
1226 #warning Trouble Point! (Lgb)
1227 // When ::breakParagraph is called from within an inset we must
1228 // ensure that the correct ParagraphList is used. Today that is not
1229 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1230 ParagraphList::iterator next_par = boost::next(cursor.par());
1232 // well this is the caption hack since one caption is really enough
1233 if (layout->labeltype == LABEL_SENSITIVE) {
1235 // set to standard-layout
1236 cursor.par()->applyLayout(tclass.defaultLayout());
1238 // set to standard-layout
1239 next_par->applyLayout(tclass.defaultLayout());
1242 // if the cursor is at the beginning of a row without prior newline,
1244 // This touches only the screen-update. Otherwise we would may have
1245 // an empty row on the screen
1246 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1247 && !cursor.par()->isNewline(cursor.pos() - 1))
1252 while (!next_par->empty() && next_par->isNewline(0))
1256 redoParagraph(cursor.par());
1257 redoParagraph(next_par);
1259 // This check is necessary. Otherwise the new empty paragraph will
1260 // be deleted automatically. And it is more friendly for the user!
1261 if (cursor.pos() || isempty)
1262 setCursor(next_par, 0);
1264 setCursor(cursor.par(), 0);
1268 // convenience function
1269 void LyXText::redoParagraph()
1272 redoParagraph(cursor.par());
1273 setCursorIntern(cursor.par(), cursor.pos());
1277 // insert a character, moves all the following breaks in the
1278 // same Paragraph one to the right and make a rebreak
1279 void LyXText::insertChar(char c)
1281 recordUndo(bv(), Undo::INSERT, cursor.par());
1283 // When the free-spacing option is set for the current layout,
1284 // disable the double-space checking
1286 bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1287 cursor.par()->isFreeSpacing();
1289 if (lyxrc.auto_number) {
1290 static string const number_operators = "+-/*";
1291 static string const number_unary_operators = "+-";
1292 static string const number_seperators = ".,:";
1294 if (current_font.number() == LyXFont::ON) {
1295 if (!IsDigit(c) && !contains(number_operators, c) &&
1296 !(contains(number_seperators, c) &&
1297 cursor.pos() >= 1 &&
1298 cursor.pos() < cursor.par()->size() &&
1299 getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1300 getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1302 number(bv()); // Set current_font.number to OFF
1303 } else if (IsDigit(c) &&
1304 real_current_font.isVisibleRightToLeft()) {
1305 number(bv()); // Set current_font.number to ON
1307 if (cursor.pos() > 0) {
1308 char const c = cursor.par()->getChar(cursor.pos() - 1);
1309 if (contains(number_unary_operators, c) &&
1310 (cursor.pos() == 1 ||
1311 cursor.par()->isSeparator(cursor.pos() - 2) ||
1312 cursor.par()->isNewline(cursor.pos() - 2))
1318 } else if (contains(number_seperators, c) &&
1319 cursor.pos() >= 2 &&
1322 cursor.pos() - 2).number() == LyXFont::ON) {
1333 // First check, if there will be two blanks together or a blank at
1334 // the beginning of a paragraph.
1335 // I decided to handle blanks like normal characters, the main
1336 // difference are the special checks when calculating the row.fill
1337 // (blank does not count at the end of a row) and the check here
1339 // The bug is triggered when we type in a description environment:
1340 // The current_font is not changed when we go from label to main text
1341 // and it should (along with realtmpfont) when we type the space.
1342 // CHECK There is a bug here! (Asger)
1344 // store the current font. This is because of the use of cursor
1345 // movements. The moving cursor would refresh the current font
1346 LyXFont realtmpfont = real_current_font;
1347 LyXFont rawtmpfont = current_font;
1349 if (!freeSpacing && IsLineSeparatorChar(c)) {
1350 if ((cursor.pos() > 0
1351 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1352 || (cursor.pos() > 0
1353 && cursor.par()->isNewline(cursor.pos() - 1))
1354 || (cursor.pos() == 0)) {
1355 static bool sent_space_message = false;
1356 if (!sent_space_message) {
1357 if (cursor.pos() == 0)
1358 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1360 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1361 sent_space_message = true;
1368 // Here case LyXText::InsertInset already inserted the character
1369 if (c != Paragraph::META_INSET)
1370 cursor.par()->insertChar(cursor.pos(), c);
1372 setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1374 current_font = rawtmpfont;
1375 real_current_font = realtmpfont;
1376 redoParagraph(cursor.par());
1377 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1383 void LyXText::charInserted()
1385 // Here we could call finishUndo for every 20 characters inserted.
1386 // This is from my experience how emacs does it. (Lgb)
1387 static unsigned int counter;
1397 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1398 RowList::iterator const rit) const
1400 double w = rit->fill();
1401 double fill_hfill = 0;
1402 double fill_label_hfill = 0;
1403 double fill_separator = 0;
1407 pit->isRightToLeftPar(bv()->buffer()->params);
1409 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1411 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1413 // is there a manual margin with a manual label
1414 LyXLayout_ptr const & layout = pit->layout();
1416 if (layout->margintype == MARGIN_MANUAL
1417 && layout->labeltype == LABEL_MANUAL) {
1418 /// We might have real hfills in the label part
1419 int nlh = numberOfLabelHfills(*pit, rit);
1421 // A manual label par (e.g. List) has an auto-hfill
1422 // between the label text and the body of the
1424 // But we don't want to do this auto hfill if the par
1429 if (nlh && !pit->getLabelWidthString().empty()) {
1430 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1434 // are there any hfills in the row?
1435 int const nh = numberOfHfills(*pit, rit);
1439 fill_hfill = w / nh;
1440 // we don't have to look at the alignment if it is ALIGN_LEFT and
1441 // if the row is already larger then the permitted width as then
1442 // we force the LEFT_ALIGN'edness!
1443 } else if (int(rit->width()) < workWidth()) {
1444 // is it block, flushleft or flushright?
1445 // set x how you need it
1447 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1448 align = layout->align;
1450 align = pit->params().align();
1452 InsetOld * inset = 0;
1453 // ERT insets should always be LEFT ALIGNED on screen
1454 inset = pit->inInset();
1455 if (inset && inset->owner() &&
1456 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1458 align = LYX_ALIGN_LEFT;
1462 case LYX_ALIGN_BLOCK:
1464 int const ns = numberOfSeparators(*pit, rit);
1465 RowList::iterator next_row = boost::next(rit);
1467 && next_row != pit->rows.end()
1468 && !pit->isNewline(next_row->pos() - 1)
1470 fill_separator = w / ns;
1471 } else if (is_rtl) {
1476 case LYX_ALIGN_RIGHT:
1479 case LYX_ALIGN_CENTER:
1485 computeBidiTables(pit, *bv()->buffer(), rit);
1487 pos_type body_pos = pit->beginningOfBody();
1488 pos_type last = lastPos(*pit, rit);
1491 (body_pos - 1 > last ||
1492 !pit->isLineSeparator(body_pos - 1))) {
1493 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1494 if (body_pos - 1 <= last)
1495 x += fill_label_hfill;
1499 rit->fill_hfill(fill_hfill);
1500 rit->fill_label_hfill(fill_label_hfill);
1501 rit->fill_separator(fill_separator);
1506 // important for the screen
1509 // the cursor set functions have a special mechanism. When they
1510 // realize, that you left an empty paragraph, they will delete it.
1511 // They also delete the corresponding row
1513 void LyXText::cursorRightOneWord()
1515 ::cursorRightOneWord(cursor, ownerParagraphs());
1516 setCursor(cursor.par(), cursor.pos());
1520 // Skip initial whitespace at end of word and move cursor to *start*
1521 // of prior word, not to end of next prior word.
1522 void LyXText::cursorLeftOneWord()
1524 LyXCursor tmpcursor = cursor;
1525 ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1526 setCursor(tmpcursor.par(), tmpcursor.pos());
1530 void LyXText::selectWord(word_location loc)
1532 LyXCursor from = cursor;
1534 ::getWord(from, to, loc, ownerParagraphs());
1536 setCursor(from.par(), from.pos());
1539 selection.cursor = cursor;
1540 setCursor(to.par(), to.pos());
1545 // Select the word currently under the cursor when no
1546 // selection is currently set
1547 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1549 if (!selection.set()) {
1551 return selection.set();
1557 void LyXText::acceptChange()
1559 if (!selection.set() && cursor.par()->size())
1562 if (selection.start.par() == selection.end.par()) {
1563 LyXCursor & startc = selection.start;
1564 LyXCursor & endc = selection.end;
1565 recordUndo(bv(), Undo::INSERT, startc.par());
1566 startc.par()->acceptChange(startc.pos(), endc.pos());
1569 redoParagraph(startc.par());
1570 setCursorIntern(startc.par(), 0);
1572 #warning handle multi par selection
1576 void LyXText::rejectChange()
1578 if (!selection.set() && cursor.par()->size())
1581 if (selection.start.par() == selection.end.par()) {
1582 LyXCursor & startc = selection.start;
1583 LyXCursor & endc = selection.end;
1584 recordUndo(bv(), Undo::INSERT, startc.par());
1585 startc.par()->rejectChange(startc.pos(), endc.pos());
1588 redoParagraph(startc.par());
1589 setCursorIntern(startc.par(), 0);
1591 #warning handle multi par selection
1595 // This function is only used by the spellchecker for NextWord().
1596 // It doesn't handle LYX_ACCENTs and probably never will.
1598 LyXText::selectNextWordToSpellcheck(float & value)
1600 if (the_locking_inset) {
1601 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1602 if (!word.word().empty()) {
1603 value += float(cursor.y());
1604 value /= float(height);
1607 // we have to go on checking so move cursor to the next char
1608 if (cursor.pos() == cursor.par()->size()) {
1609 if (boost::next(cursor.par()) == ownerParagraphs().end())
1611 cursor.par(boost::next(cursor.par()));
1614 cursor.pos(cursor.pos() + 1);
1616 ParagraphList::iterator tmppit = cursor.par();
1618 // If this is not the very first word, skip rest of
1619 // current word because we are probably in the middle
1620 // of a word if there is text here.
1621 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1622 while (cursor.pos() < cursor.par()->size()
1623 && cursor.par()->isLetter(cursor.pos()))
1624 cursor.pos(cursor.pos() + 1);
1627 // Now, skip until we have real text (will jump paragraphs)
1629 ParagraphList::iterator cpit = cursor.par();
1630 pos_type const cpos(cursor.pos());
1632 if (cpos == cpit->size()) {
1633 if (boost::next(cpit) != ownerParagraphs().end()) {
1634 cursor.par(boost::next(cpit));
1641 bool const is_good_inset = cpit->isInset(cpos)
1642 && cpit->getInset(cpos)->allowSpellcheck();
1644 if (!isDeletedText(*cpit, cpos)
1645 && (is_good_inset || cpit->isLetter(cpos)))
1648 cursor.pos(cpos + 1);
1651 // now check if we hit an inset so it has to be a inset containing text!
1652 if (cursor.pos() < cursor.par()->size() &&
1653 cursor.par()->isInset(cursor.pos())) {
1655 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1656 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1657 // now call us again to do the above trick
1658 // but obviously we have to start from down below ;)
1659 return bv()->text->selectNextWordToSpellcheck(value);
1662 // Update the value if we changed paragraphs
1663 if (cursor.par() != tmppit) {
1664 setCursor(cursor.par(), cursor.pos());
1665 value = float(cursor.y())/float(height);
1668 // Start the selection from here
1669 selection.cursor = cursor;
1671 string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1672 // and find the end of the word (insets like optional hyphens
1673 // and ligature break are part of a word)
1674 while (cursor.pos() < cursor.par()->size()
1675 && cursor.par()->isLetter(cursor.pos())
1676 && !isDeletedText(*cursor.par(), cursor.pos()))
1677 cursor.pos(cursor.pos() + 1);
1679 // Finally, we copy the word to a string and return it
1681 if (selection.cursor.pos() < cursor.pos()) {
1683 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1684 if (!cursor.par()->isInset(i))
1685 str += cursor.par()->getChar(i);
1688 return WordLangTuple(str, lang_code);
1692 // This one is also only for the spellchecker
1693 void LyXText::selectSelectedWord()
1695 if (the_locking_inset) {
1696 the_locking_inset->selectSelectedWord(bv());
1699 // move cursor to the beginning
1700 setCursor(selection.cursor.par(), selection.cursor.pos());
1702 // set the sel cursor
1703 selection.cursor = cursor;
1705 // now find the end of the word
1706 while (cursor.pos() < cursor.par()->size()
1707 && cursor.par()->isLetter(cursor.pos()))
1708 cursor.pos(cursor.pos() + 1);
1710 setCursor(cursor.par(), cursor.pos());
1712 // finally set the selection
1717 // Delete from cursor up to the end of the current or next word.
1718 void LyXText::deleteWordForward()
1720 if (cursor.par()->empty())
1723 LyXCursor tmpcursor = cursor;
1724 selection.set(true); // to avoid deletion
1725 cursorRightOneWord();
1726 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1727 selection.cursor = cursor;
1731 // Great, CutSelection() gets rid of multiple spaces.
1732 cutSelection(true, false);
1737 // Delete from cursor to start of current or prior word.
1738 void LyXText::deleteWordBackward()
1740 if (cursor.par()->empty())
1743 LyXCursor tmpcursor = cursor;
1744 selection.set(true); // to avoid deletion
1745 cursorLeftOneWord();
1746 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1747 selection.cursor = cursor;
1750 cutSelection(true, false);
1755 // Kill to end of line.
1756 void LyXText::deleteLineForward()
1758 if (cursor.par()->empty())
1759 // Paragraph is empty, so we just go to the right
1762 LyXCursor tmpcursor = cursor;
1763 // We can't store the row over a regular setCursor
1764 // so we set it to 0 and reset it afterwards.
1765 selection.set(true); // to avoid deletion
1767 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1768 selection.cursor = cursor;
1771 // What is this test for ??? (JMarc)
1772 if (!selection.set()) {
1773 deleteWordForward();
1775 cutSelection(true, false);
1781 void LyXText::changeCase(LyXText::TextCase action)
1786 if (selection.set()) {
1787 from = selection.start;
1791 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1792 setCursor(to.par(), to.pos() + 1);
1795 recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1797 pos_type pos = from.pos();
1798 ParagraphList::iterator pit = from.par();
1800 while (pit != ownerParagraphs().end() &&
1801 (pos != to.pos() || pit != to.par())) {
1802 if (pos == pit->size()) {
1807 unsigned char c = pit->getChar(pos);
1808 if (!IsInsetChar(c)) {
1810 case text_lowercase:
1813 case text_capitalization:
1815 action = text_lowercase;
1817 case text_uppercase:
1823 pit->setChar(pos, c);
1829 void LyXText::Delete()
1831 // this is a very easy implementation
1833 LyXCursor old_cursor = cursor;
1834 int const old_cur_par_id = old_cursor.par()->id();
1835 int const old_cur_par_prev_id =
1836 (old_cursor.par() != ownerParagraphs().begin() ?
1837 boost::prior(old_cursor.par())->id() : -1);
1839 // just move to the right
1842 // CHECK Look at the comment here.
1843 // This check is not very good...
1844 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1845 // and that can very well delete the par or par->previous in
1846 // old_cursor. Will a solution where we compare paragraph id's
1848 if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1849 == old_cur_par_prev_id
1850 && cursor.par()->id() != old_cur_par_id) {
1851 // delete-empty-paragraph-mechanism has done it
1855 // if you had success make a backspace
1856 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1857 LyXCursor tmpcursor = cursor;
1858 // to make sure undo gets the right cursor position
1859 cursor = old_cursor;
1860 recordUndo(bv(), Undo::DELETE, cursor.par());
1867 void LyXText::backspace()
1869 // Get the font that is used to calculate the baselineskip
1870 pos_type lastpos = cursor.par()->size();
1872 if (cursor.pos() == 0) {
1873 // The cursor is at the beginning of a paragraph,
1874 // so the the backspace will collapse two paragraphs into one.
1876 // but it's not allowed unless it's new
1877 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1880 // we may paste some paragraphs
1882 // is it an empty paragraph?
1885 || (lastpos == 1 && cursor.par()->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.
1891 if (cursor.par() != ownerParagraphs().begin()) {
1892 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1893 if (cursor.par()->layout() == tmppit->layout()
1894 && cursor.par()->getAlign() == tmppit->getAlign()) {
1895 // Inherit bottom DTD from the paragraph below.
1896 // (the one we are deleting)
1897 tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1898 tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1899 tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1904 // the layout things can change the height of a row !
1910 if (cursor.par() != ownerParagraphs().begin()) {
1911 recordUndo(bv(), Undo::DELETE,
1912 boost::prior(cursor.par()),
1916 ParagraphList::iterator tmppit = cursor.par();
1917 // We used to do cursorLeftIntern() here, but it is
1918 // not a good idea since it triggers the auto-delete
1919 // mechanism. So we do a cursorLeftIntern()-lite,
1920 // without the dreaded mechanism. (JMarc)
1921 if (cursor.par() != ownerParagraphs().begin()) {
1922 // steps into the above paragraph.
1923 setCursorIntern(boost::prior(cursor.par()),
1924 boost::prior(cursor.par())->size(),
1928 // Pasting is not allowed, if the paragraphs have different
1929 // layout. I think it is a real bug of all other
1930 // word processors to allow it. It confuses the user.
1931 // Correction: Pasting is always allowed with standard-layout
1932 LyXTextClass const & tclass =
1933 bv()->buffer()->params.getLyXTextClass();
1935 if (cursor.par() != tmppit
1936 && (cursor.par()->layout() == tmppit->layout()
1937 || tmppit->layout() == tclass.defaultLayout())
1938 && cursor.par()->getAlign() == tmppit->getAlign()) {
1939 mergeParagraph(bv()->buffer()->params,
1940 bv()->buffer()->paragraphs, cursor.par());
1942 if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
1943 cursor.pos(cursor.pos() - 1);
1945 // the row may have changed, block, hfills etc.
1947 setCursor(cursor.par(), cursor.pos(), false);
1950 // this is the code for a normal backspace, not pasting
1952 recordUndo(bv(), Undo::DELETE, cursor.par());
1953 // We used to do cursorLeftIntern() here, but it is
1954 // not a good idea since it triggers the auto-delete
1955 // mechanism. So we do a cursorLeftIntern()-lite,
1956 // without the dreaded mechanism. (JMarc)
1957 setCursorIntern(cursor.par(), cursor.pos() - 1,
1958 false, cursor.boundary());
1959 cursor.par()->erase(cursor.pos());
1962 lastpos = cursor.par()->size();
1963 if (cursor.pos() == lastpos)
1967 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1971 RowList::iterator LyXText::cursorRow() const
1973 return getRow(cursor.par(), cursor.pos());
1977 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
1979 return getRow(cur.par(), cur.pos());
1984 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
1986 RowList::iterator rit = boost::prior(pit->rows.end());
1987 RowList::iterator const begin = pit->rows.begin();
1989 while (rit != begin && rit->pos() > pos)
1996 // returns pointer to some fancy row 'below' specified row
1997 RowList::iterator LyXText::cursorIRow() const
1999 return getRow(cursor.par(), cursor.pos());
2003 RowList::iterator LyXText::getRowNearY(int y,
2004 ParagraphList::iterator & pit) const
2006 //lyxerr << "getRowNearY: y " << y << endl;
2008 pit = boost::prior(ownerParagraphs().end());
2010 RowList::iterator rit = lastRow();
2011 RowList::iterator rbegin = firstRow();
2013 while (rit != rbegin && static_cast<int>(rit->y()) > y)
2014 previousRow(pit, rit);
2020 int LyXText::getDepth() const
2022 return cursor.par()->getDepth();
2026 RowList::iterator LyXText::firstRow() const
2028 return ownerParagraphs().front().rows.begin();
2032 RowList::iterator LyXText::lastRow() const
2034 return boost::prior(endRow());
2038 RowList::iterator LyXText::endRow() const
2040 return ownerParagraphs().back().rows.end();
2044 void LyXText::nextRow(ParagraphList::iterator & pit,
2045 RowList::iterator & rit) const
2048 if (rit == pit->rows.end()) {
2050 if (pit == ownerParagraphs().end())
2053 rit = pit->rows.begin();
2058 void LyXText::previousRow(ParagraphList::iterator & pit,
2059 RowList::iterator & rit) const
2061 if (rit != pit->rows.begin())
2064 Assert(pit != ownerParagraphs().begin());
2066 rit = boost::prior(pit->rows.end());
2071 bool LyXText::noRows() const
2073 return ownerParagraphs().begin()->rows.empty();