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.
24 #include "funcrequest.h"
25 #include "frontends/LyXView.h"
26 #include "frontends/font_metrics.h"
27 #include "BufferView.h"
29 #include "ParagraphParameters.h"
30 #include "undo_funcs.h"
31 #include "text_funcs.h"
32 #include "WordLangTuple.h"
33 #include "paragraph_funcs.h"
34 #include "rowpainter.h"
36 #include "lyxrow_funcs.h"
38 #include "insets/insettext.h"
40 #include "support/textutils.h"
41 #include "support/LAssert.h"
42 #include "support/lstrings.h"
45 using namespace lyx::support;
53 using lyx::word_location;
55 using namespace bv_funcs;
57 /// top, right, bottom pixel margin
58 extern int const PAPER_MARGIN = 20;
59 /// margin for changebar
60 extern int const CHANGEBAR_MARGIN = 10;
62 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
66 int bibitemMaxWidth(BufferView *, LyXFont const &);
69 BufferView * LyXText::bv()
71 Assert(bv_owner != 0);
76 BufferView * LyXText::bv() const
78 Assert(bv_owner != 0);
83 void LyXText::updateRowPositions()
85 ParagraphList::iterator pit = ownerParagraphs().begin();
86 ParagraphList::iterator end = ownerParagraphs().end();
87 for (height = 0; pit != end; ++pit) {
88 RowList::iterator rit = pit->rows.begin();
89 RowList::iterator rend = pit->rows.end();
90 for ( ; rit != rend ; rit = ++rit) {
92 height += rit->height();
98 int LyXText::workWidth() const
100 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
104 int LyXText::getRealCursorX() const
107 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
108 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
113 #warning FIXME This function seems to belong outside of LyxText.
114 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
117 if (!Encodings::is_arabic(c))
118 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
119 return c + (0xb0 - '0');
123 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
124 unsigned char next_char = ' ';
126 pos_type const par_size = par.size();
128 for (pos_type i = pos + 1; i < par_size; ++i) {
129 unsigned char const par_char = par.getChar(i);
130 if (!Encodings::IsComposeChar_arabic(par_char)) {
131 next_char = par_char;
136 if (Encodings::is_arabic(next_char)) {
137 if (Encodings::is_arabic(prev_char) &&
138 !Encodings::is_arabic_special(prev_char))
139 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
141 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
143 if (Encodings::is_arabic(prev_char) &&
144 !Encodings::is_arabic_special(prev_char))
145 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
147 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
151 // This is the comments that some of the warnings below refers to.
152 // There are some issues in this file and I don't think they are
153 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
154 // this is a problem that has been here almost from day one and that a
155 // larger userbase with differenct access patters triggers the bad
156 // behaviour. (segfaults.) What I think happen is: In several places
157 // we store the paragraph in the current cursor and then moves the
158 // cursor. This movement of the cursor will delete paragraph at the
159 // old position if it is now empty. This will make the temporary
160 // pointer to the old cursor paragraph invalid and dangerous to use.
161 // And is some cases this will trigger a segfault. I have marked some
162 // of the cases where this happens with a warning, but I am sure there
163 // are others in this file and in text2.C. There is also a note in
164 // Delete() that you should read. In Delete I store the paragraph->id
165 // instead of a pointer to the paragraph. I am pretty sure this faulty
166 // use of temporary pointers to paragraphs that might have gotten
167 // invalidated (through a cursor movement) before they are used, are
168 // the cause of the strange crashes we get reported often.
170 // It is very tiresom to change this code, especially when it is as
171 // hard to read as it is. Help to fix all the cases where this is done
172 // would be greately appreciated.
176 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
178 if (pos >= pit->size())
181 char const c = pit->getChar(pos);
182 LyXFont const & font = getFont(pit, pos);
183 return singleWidth(pit, pos, c, font);
187 int LyXText::singleWidth(ParagraphList::iterator pit,
188 pos_type pos, char c, LyXFont const & font) const
190 if (pos >= pit->size()) {
191 lyxerr << "in singleWidth(), pos: " << pos << endl;
197 // The most common case is handled first (Asger)
198 if (IsPrintable(c)) {
199 if (font.language()->RightToLeft()) {
200 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
201 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
202 && font.language()->lang() == "arabic") {
203 if (Encodings::IsComposeChar_arabic(c))
206 c = transformChar(c, *pit, pos);
207 } else if (font.language()->lang() == "hebrew" &&
208 Encodings::IsComposeChar_hebrew(c))
211 return font_metrics::width(c, font);
214 if (c == Paragraph::META_INSET) {
215 InsetOld * tmpinset = pit->getInset(pos);
217 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
218 // Because of the representation as vertical lines
221 return tmpinset->width();
224 if (IsSeparatorChar(c))
226 return font_metrics::width(c, font);
230 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
232 if (bidi_start == -1)
235 return log2vis_list[pos - bidi_start];
239 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
241 if (bidi_start == -1)
244 return vis2log_list[pos - bidi_start];
248 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
250 if (bidi_start == -1)
253 return bidi_levels[pos - bidi_start];
257 bool LyXText::bidi_InRange(lyx::pos_type pos) const
259 return bidi_start == -1 ||
260 (bidi_start <= pos && pos <= bidi_end);
264 void LyXText::computeBidiTables(ParagraphList::iterator pit,
265 Buffer const & buf, RowList::iterator row) const
267 bidi_same_direction = true;
268 if (!lyxrc.rtl_support) {
273 InsetOld * inset = pit->inInset();
274 if (inset && inset->owner() &&
275 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
280 bidi_start = row->pos();
281 bidi_end = lastPos(*pit, row);
283 if (bidi_start > bidi_end) {
288 if (bidi_end + 2 - bidi_start >
289 static_cast<pos_type>(log2vis_list.size())) {
291 (bidi_end + 2 - bidi_start < 500) ?
292 500 : 2 * (bidi_end + 2 - bidi_start);
293 log2vis_list.resize(new_size);
294 vis2log_list.resize(new_size);
295 bidi_levels.resize(new_size);
298 vis2log_list[bidi_end + 1 - bidi_start] = -1;
299 log2vis_list[bidi_end + 1 - bidi_start] = -1;
303 pit->isRightToLeftPar(buf.params);
307 pos_type const body_pos = pit->beginningOfBody();
309 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
310 bool is_space = pit->isLineSeparator(lpos);
312 (is_space && lpos + 1 <= bidi_end &&
313 !pit->isLineSeparator(lpos + 1) &&
314 !pit->isNewline(lpos + 1))
316 LyXFont font = pit->getFontSettings(buf.params, pos);
317 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
318 font.number() == LyXFont::ON &&
319 pit->getFontSettings(buf.params, lpos - 1).number()
321 font = pit->getFontSettings(buf.params, lpos);
326 bool new_rtl = font.isVisibleRightToLeft();
327 bool new_rtl0 = font.isRightToLeft();
330 if (lpos == body_pos - 1
331 && row->pos() < body_pos - 1
333 new_level = (rtl_par) ? 1 : 0;
334 new_rtl = new_rtl0 = rtl_par;
336 new_level = (new_rtl) ? 1 : 2;
338 new_level = (rtl_par) ? 2 : 0;
340 if (is_space && new_level >= level) {
346 int new_level2 = new_level;
348 if (level == new_level && rtl0 != new_rtl0) {
350 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
351 } else if (level < new_level) {
352 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
353 if (new_level > rtl_par)
354 bidi_same_direction = false;
356 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
359 bidi_levels[lpos - bidi_start] = new_level;
361 while (level > new_level2) {
362 pos_type old_lpos = stack[--level];
363 int delta = lpos - old_lpos - 1;
366 log2vis_list[lpos - bidi_start] += delta;
367 log2vis_list[old_lpos - bidi_start] += delta;
369 while (level < new_level)
370 stack[level++] = lpos;
374 pos_type const old_lpos = stack[--level];
375 int delta = bidi_end - old_lpos;
378 log2vis_list[old_lpos - bidi_start] += delta;
381 pos_type vpos = bidi_start - 1;
382 for (pos_type lpos = bidi_start;
383 lpos <= bidi_end; ++lpos) {
384 vpos += log2vis_list[lpos - bidi_start];
385 vis2log_list[vpos - bidi_start] = lpos;
386 log2vis_list[lpos - bidi_start] = vpos;
391 // This method requires a previous call to ComputeBidiTables()
392 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
395 if (!lyxrc.rtl_support || pos == 0)
398 if (!bidi_InRange(pos - 1)) {
399 /// This can happen if pos is the first char of a row.
400 /// Returning false in this case is incorrect!
404 bool const rtl = bidi_level(pos - 1) % 2;
405 bool const rtl2 = bidi_InRange(pos)
406 ? bidi_level(pos) % 2
407 : par.isRightToLeftPar(buf.params);
412 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
413 pos_type pos, LyXFont const & font) const
415 if (!lyxrc.rtl_support)
416 return false; // This is just for speedup
418 bool const rtl = font.isVisibleRightToLeft();
419 bool const rtl2 = bidi_InRange(pos)
420 ? bidi_level(pos) % 2
421 : par.isRightToLeftPar(buf.params);
426 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
428 LyXTextClass const & tclass =
429 bv()->buffer()->params.getLyXTextClass();
430 LyXLayout_ptr const & layout = pit->layout();
432 string parindent = layout->parindent;
436 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
438 // this is the way, LyX handles the LaTeX-Environments.
439 // I have had this idea very late, so it seems to be a
440 // later added hack and this is true
441 if (!pit->getDepth()) {
442 if (pit->layout() == tclass.defaultLayout()) {
443 // find the previous same level paragraph
444 if (pit != ownerParagraphs().begin()) {
445 ParagraphList::iterator newpit =
446 depthHook(pit, ownerParagraphs(),
449 newpit->layout()->nextnoindent)
454 // find the next level paragraph
456 ParagraphList::iterator newpar = outerHook(pit,
459 // make a corresponding row. Needed to call leftMargin()
461 // check wether it is a sufficent paragraph
462 if (newpar != ownerParagraphs().end() &&
463 newpar->layout()->isEnvironment()) {
464 x = leftMargin(newpar, Row(newpar->size()));
467 if (newpar != ownerParagraphs().end() &&
468 pit->layout() == tclass.defaultLayout()) {
469 if (newpar->params().noindent())
472 parindent = newpar->layout()->parindent;
478 LyXFont const labelfont = getLabelFont(pit);
479 switch (layout->margintype) {
481 if (!layout->leftmargin.empty()) {
482 x += font_metrics::signedWidth(layout->leftmargin,
483 tclass.defaultfont());
485 if (!pit->getLabelstring().empty()) {
486 x += font_metrics::signedWidth(layout->labelindent,
488 x += font_metrics::width(pit->getLabelstring(),
490 x += font_metrics::width(layout->labelsep, labelfont);
494 x += font_metrics::signedWidth(layout->labelindent, labelfont);
495 // The width of an empty par, even with manual label, should be 0
496 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
497 if (!pit->getLabelWidthString().empty()) {
498 x += font_metrics::width(pit->getLabelWidthString(),
500 x += font_metrics::width(layout->labelsep, labelfont);
505 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
506 / (pit->getDepth() + 4);
508 case MARGIN_FIRST_DYNAMIC:
509 if (layout->labeltype == LABEL_MANUAL) {
510 if (row.pos() >= pit->beginningOfBody()) {
511 x += font_metrics::signedWidth(layout->leftmargin,
514 x += font_metrics::signedWidth(layout->labelindent,
518 // Special case to fix problems with
520 || (layout->labeltype == LABEL_STATIC
521 && layout->latextype == LATEX_ENVIRONMENT
522 && !isFirstInSequence(pit, ownerParagraphs()))) {
523 x += font_metrics::signedWidth(layout->leftmargin,
525 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
526 && layout->labeltype != LABEL_BIBLIO
527 && layout->labeltype !=
528 LABEL_CENTERED_TOP_ENVIRONMENT) {
529 x += font_metrics::signedWidth(layout->labelindent,
531 x += font_metrics::width(layout->labelsep, labelfont);
532 x += font_metrics::width(pit->getLabelstring(),
537 case MARGIN_RIGHT_ADDRESS_BOX:
539 // ok, a terrible hack. The left margin depends on the widest
540 // row in this paragraph. Do not care about footnotes, they
541 // are *NOT* allowed in the LaTeX realisation of this layout.
543 // find the first row of this paragraph
544 RowList::iterator rit = pit->rows.begin();
545 RowList::iterator end = pit->rows.end();
546 int minfill = rit->fill();
547 for ( ; rit != end; ++rit)
548 if (rit->fill() < minfill)
549 minfill = rit->fill();
551 x += font_metrics::signedWidth(layout->leftmargin,
552 tclass.defaultfont());
558 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
559 LyXLength const len = pit->params().leftIndent();
560 int const tw = inset_owner ?
561 inset_owner->latexTextWidth(bv()) : workWidth();
562 x += len.inPixels(tw);
567 if (pit->params().align() == LYX_ALIGN_LAYOUT)
568 align = layout->align;
570 align = pit->params().align();
572 // set the correct parindent
573 if (row.pos() == 0) {
574 if ((layout->labeltype == LABEL_NO_LABEL
575 || layout->labeltype == LABEL_TOP_ENVIRONMENT
576 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
577 || (layout->labeltype == LABEL_STATIC
578 && layout->latextype == LATEX_ENVIRONMENT
579 && !isFirstInSequence(pit, ownerParagraphs())))
580 && align == LYX_ALIGN_BLOCK
581 && !pit->params().noindent()
582 // in tabulars and ert paragraphs are never indented!
583 && (!pit->inInset() || !pit->inInset()->owner() ||
584 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
585 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
586 && (pit->layout() != tclass.defaultLayout() ||
587 bv()->buffer()->params.paragraph_separation ==
588 BufferParams::PARSEP_INDENT)) {
589 x += font_metrics::signedWidth(parindent,
590 tclass.defaultfont());
591 } else if (layout->labeltype == LABEL_BIBLIO) {
592 // ale970405 Right width for bibitems
593 x += bibitemMaxWidth(bv(), tclass.defaultfont());
601 int LyXText::rightMargin(ParagraphList::iterator pit,
602 Buffer const & buf, Row const &) const
604 LyXTextClass const & tclass = buf.params.getLyXTextClass();
605 LyXLayout_ptr const & layout = pit->layout();
608 + font_metrics::signedWidth(tclass.rightmargin(),
609 tclass.defaultfont());
610 + font_metrics::signedWidth(layout->rightmargin,
611 tclass.defaultfont())
612 * 4 / (pit->getDepth() + 4);
616 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
618 if (pit->layout()->margintype == MARGIN_MANUAL) {
620 tmprow.pos(pit->size());
621 // return the beginning of the body
622 return leftMargin(pit, tmprow);
625 // LabelEnd is only needed if the layout
626 // fills a flushleft label.
633 // this needs special handling - only newlines count as a break point
634 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
636 for (; i < par.size(); ++i) {
637 if (par.isNewline(i))
647 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
648 Row const & row) const
650 // maximum pixel width of a row.
651 int width = workWidth()
652 - rightMargin(pit, *bv()->buffer(), row);
654 // inset->textWidth() returns -1 via workWidth(),
659 LyXLayout_ptr const & layout = pit->layout();
661 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
662 return addressBreakPoint(row.pos(), *pit);
664 pos_type const pos = row.pos();
665 pos_type const body_pos = pit->beginningOfBody();
666 pos_type const last = pit->size();
667 pos_type point = last;
672 // Now we iterate through until we reach the right margin
673 // or the end of the par, then choose the possible break
676 int const left = leftMargin(pit, row);
679 // pixel width since last breakpoint
684 // We re-use the font resolution for the entire font span when possible
685 LyXFont font = getFont(pit, i);
686 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
688 for (; i < last; ++i) {
689 if (pit->isNewline(i)) {
694 char const c = pit->getChar(i);
695 if (i > endPosOfFontSpan) {
696 font = getFont(pit, i);
697 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
702 // add the auto-hfill from label end to the body
703 if (body_pos && i == body_pos) {
704 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
705 if (pit->isLineSeparator(i - 1))
706 thiswidth -= singleWidth(pit, i - 1);
707 int left_margin = labelEnd(pit, row);
708 if (thiswidth + x < left_margin)
709 thiswidth = left_margin - x;
710 thiswidth += singleWidth(pit, i, c, font);
712 thiswidth = singleWidth(pit, i, c, font);
716 chunkwidth += thiswidth;
718 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
720 // break before a character that will fall off
721 // the right of the row
723 // if no break before, break here
724 if (point == last || chunkwidth >= (width - left)) {
733 if (!in || in->isChar()) {
734 // some insets are line separators too
735 if (pit->isLineSeparator(i)) {
745 if (point == last && x >= width) {
746 // didn't find one, break at the point we reached the edge
748 } else if (i == last && x < width) {
749 // found one, but we fell off the end of the par, so prefer
754 // manual labels cannot be broken in LaTeX. But we
755 // want to make our on-screen rendering of footnotes
757 if (body_pos && point < body_pos)
758 point = body_pos - 1;
764 // returns the minimum space a row needs on the screen in pixel
765 int LyXText::fill(ParagraphList::iterator pit,
766 RowList::iterator row, int paper_width) const
772 // get the pure distance
773 pos_type const last = lastPos(*pit, row);
775 LyXLayout_ptr const & layout = pit->layout();
777 // special handling of the right address boxes
778 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
779 int const tmpfill = row->fill();
780 row->fill(0); // the minfill in MarginLeft()
781 w = leftMargin(pit, *row);
784 w = leftMargin(pit, *row);
786 pos_type const body_pos = pit->beginningOfBody();
787 pos_type i = row->pos();
789 if (! pit->empty() && i <= last) {
790 // We re-use the font resolution for the entire span when possible
791 LyXFont font = getFont(pit, i);
792 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
794 if (body_pos > 0 && i == body_pos) {
795 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
796 if (pit->isLineSeparator(i - 1))
797 w -= singleWidth(pit, i - 1);
798 int left_margin = labelEnd(pit, *row);
802 char const c = pit->getChar(i);
803 if (IsPrintable(c) && i > endPosOfFontSpan) {
804 // We need to get the next font
805 font = getFont(pit, i);
806 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
808 w += singleWidth(pit, i, c, font);
812 if (body_pos > 0 && body_pos > last) {
813 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
814 if (last >= 0 && pit->isLineSeparator(last))
815 w -= singleWidth(pit, last);
816 int const left_margin = labelEnd(pit, *row);
821 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
823 // If this case happens, it means that our calculation
824 // of the widths of the chars when we do rowBreakPoint()
825 // went wrong for some reason. Typically in list bodies.
826 // Things just about hobble on anyway, though you'll end
827 // up with a "fill_separator" less than zero, which corresponds
828 // to inter-word spacing being too small. Hopefully this problem
829 // will die when the label hacks die.
830 if (lyxerr.debugging() && fill < 0) {
831 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
832 << " w " << w << " paper_width " << paper_width
833 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
839 // returns the minimum space a manual label needs on the screen in pixel
840 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
842 pos_type last = pit->beginningOfBody();
846 // -1 because a label ends either with a space that is in the label,
847 // or with the beginning of a footnote that is outside the label.
850 // a separator at this end does not count
851 if (pit->isLineSeparator(last))
855 pos_type i = row.pos();
857 w += singleWidth(pit, i);
862 string const & labwidstr = pit->params().labelWidthString();
863 if (!labwidstr.empty()) {
864 LyXFont const labfont = getLabelFont(pit);
865 int const labwidth = font_metrics::width(labwidstr, labfont);
866 fill = max(labwidth - w, 0);
873 LColor::color LyXText::backgroundColor() const
876 return inset_owner->backgroundColor();
878 return LColor::background;
882 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
884 // get the maximum ascent and the maximum descent
885 double layoutasc = 0;
886 double layoutdesc = 0;
889 // ok, let us initialize the maxasc and maxdesc value.
890 // Only the fontsize count. The other properties
891 // are taken from the layoutfont. Nicer on the screen :)
892 LyXLayout_ptr const & layout = pit->layout();
894 // as max get the first character of this row then it can increase but not
895 // decrease the height. Just some point to start with so we don't have to
896 // do the assignment below too often.
897 LyXFont font = getFont(pit, rit->pos());
898 LyXFont::FONT_SIZE const tmpsize = font.size();
899 font = getLayoutFont(pit);
900 LyXFont::FONT_SIZE const size = font.size();
901 font.setSize(tmpsize);
903 LyXFont labelfont = getLabelFont(pit);
905 double spacing_val = 1.0;
906 if (!pit->params().spacing().isDefault())
907 spacing_val = pit->params().spacing().getValue();
909 spacing_val = bv()->buffer()->params.spacing.getValue();
910 //lyxerr << "spacing_val = " << spacing_val << endl;
912 int maxasc = int(font_metrics::maxAscent(font) *
913 layout->spacing.getValue() * spacing_val);
914 int maxdesc = int(font_metrics::maxDescent(font) *
915 layout->spacing.getValue() * spacing_val);
917 pos_type const pos_end = lastPos(*pit, rit);
922 // We re-use the font resolution for the entire font span when possible
923 LyXFont font = getFont(pit, rit->pos());
924 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
927 Paragraph const & par = *pit;
929 // Check if any insets are larger
930 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
931 // Manual inlined optimised version of common case of
932 // "maxwidth += singleWidth(pit, pos);"
933 char const c = par.getChar(pos);
935 if (IsPrintable(c)) {
936 if (pos > endPosOfFontSpan) {
937 // We need to get the next font
938 font = getFont(pit, pos);
939 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
941 if (! font.language()->RightToLeft()) {
942 maxwidth += font_metrics::width(c, font);
944 // Fall-back to normal case
945 maxwidth += singleWidth(pit, pos, c, font);
946 // And flush font cache
947 endPosOfFontSpan = 0;
950 // Special handling of insets - are any larger?
951 if (par.isInset(pos)) {
952 InsetOld const * tmpinset = par.getInset(pos);
954 maxwidth += tmpinset->width();
955 maxasc = max(maxasc, tmpinset->ascent());
956 maxdesc = max(maxdesc, tmpinset->descent());
959 // Fall-back to normal case
960 maxwidth += singleWidth(pit, pos, c, font);
961 // And flush font cache
962 endPosOfFontSpan = 0;
968 // Check if any custom fonts are larger (Asger)
969 // This is not completely correct, but we can live with the small,
970 // cosmetic error for now.
971 LyXFont::FONT_SIZE maxsize =
972 pit->highestFontInRange(rit->pos(), pos_end, size);
973 if (maxsize > font.size()) {
974 font.setSize(maxsize);
975 maxasc = max(maxasc, font_metrics::maxAscent(font));
976 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
979 // This is nicer with box insets:
983 rit->ascent_of_text(maxasc);
988 // some parksips VERY EASY IMPLEMENTATION
989 if (bv()->buffer()->params.paragraph_separation ==
990 BufferParams::PARSEP_SKIP)
992 if (layout->isParagraph()
993 && pit->getDepth() == 0
994 && pit != ownerParagraphs().begin())
996 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
997 } else if (pit != ownerParagraphs().begin() &&
998 boost::prior(pit)->layout()->isParagraph() &&
999 boost::prior(pit)->getDepth() == 0)
1001 // is it right to use defskip here too? (AS)
1002 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1007 if (pit == ownerParagraphs().begin() && !isInInset())
1008 maxasc += PAPER_MARGIN;
1010 // add the vertical spaces, that the user added
1011 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1013 // do not forget the DTP-lines!
1014 // there height depends on the font of the nearest character
1015 if (pit->params().lineTop())
1017 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1018 // and now the pagebreaks
1019 if (pit->params().pagebreakTop())
1020 maxasc += 3 * defaultRowHeight();
1022 if (pit->params().startOfAppendix())
1023 maxasc += 3 * defaultRowHeight();
1025 // This is special code for the chapter, since the label of this
1026 // layout is printed in an extra row
1027 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1028 && bv()->buffer()->params.secnumdepth >= 0)
1030 float spacing_val = 1.0;
1031 if (!pit->params().spacing().isDefault()) {
1032 spacing_val = pit->params().spacing().getValue();
1034 spacing_val = bv()->buffer()->params.spacing.getValue();
1037 labeladdon = int(font_metrics::maxDescent(labelfont) *
1038 layout->spacing.getValue() *
1040 + int(font_metrics::maxAscent(labelfont) *
1041 layout->spacing.getValue() *
1045 // special code for the top label
1046 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1047 || layout->labeltype == LABEL_BIBLIO
1048 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1049 && isFirstInSequence(pit, ownerParagraphs())
1050 && !pit->getLabelstring().empty())
1052 float spacing_val = 1.0;
1053 if (!pit->params().spacing().isDefault()) {
1054 spacing_val = pit->params().spacing().getValue();
1056 spacing_val = bv()->buffer()->params.spacing.getValue();
1060 (font_metrics::maxAscent(labelfont) +
1061 font_metrics::maxDescent(labelfont)) *
1062 layout->spacing.getValue() *
1064 + layout->topsep * defaultRowHeight()
1065 + layout->labelbottomsep * defaultRowHeight());
1068 // And now the layout spaces, for example before and after
1069 // a section, or between the items of a itemize or enumerate
1072 if (!pit->params().pagebreakTop()) {
1073 ParagraphList::iterator prev =
1074 depthHook(pit, ownerParagraphs(),
1076 if (prev != pit && prev->layout() == layout &&
1077 prev->getDepth() == pit->getDepth() &&
1078 prev->getLabelWidthString() == pit->getLabelWidthString())
1080 layoutasc = (layout->itemsep * defaultRowHeight());
1081 } else if (rit != firstRow()) {
1082 tmptop = layout->topsep;
1084 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1085 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1088 layoutasc = (tmptop * defaultRowHeight());
1089 } else if (pit->params().lineTop()) {
1090 tmptop = layout->topsep;
1093 layoutasc = (tmptop * defaultRowHeight());
1096 prev = outerHook(pit, ownerParagraphs());
1097 if (prev != ownerParagraphs().end()) {
1098 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1099 } else if (pit != ownerParagraphs().begin()) {
1100 ParagraphList::iterator prior_pit = boost::prior(pit);
1101 if (prior_pit->getDepth() != 0 ||
1102 prior_pit->layout() == layout) {
1103 maxasc += int(layout->parsep * defaultRowHeight());
1109 // is it a bottom line?
1110 if (boost::next(rit) == pit->rows.end()) {
1111 // the bottom margin
1112 ParagraphList::iterator nextpit = boost::next(pit);
1113 if (nextpit == ownerParagraphs().end() && !isInInset())
1114 maxdesc += PAPER_MARGIN;
1116 // add the vertical spaces, that the user added
1117 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1119 // do not forget the DTP-lines!
1120 // there height depends on the font of the nearest character
1121 if (pit->params().lineBottom())
1122 maxdesc += 2 * font_metrics::ascent('x',
1123 getFont(pit, max(pos_type(0), pit->size() - 1)));
1125 // and now the pagebreaks
1126 if (pit->params().pagebreakBottom())
1127 maxdesc += 3 * defaultRowHeight();
1129 // and now the layout spaces, for example before and after
1130 // a section, or between the items of a itemize or enumerate
1132 if (!pit->params().pagebreakBottom()
1133 && nextpit != ownerParagraphs().end()) {
1134 ParagraphList::iterator comparepit = pit;
1138 if (comparepit->getDepth() > nextpit->getDepth()) {
1139 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1140 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1141 if (comparepit->layout()!= nextpit->layout()
1142 || nextpit->getLabelWidthString() !=
1143 comparepit->getLabelWidthString())
1145 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1147 if (unusual > usual)
1148 layoutdesc = unusual;
1151 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1153 if (comparepit->layout() != nextpit->layout()
1154 || nextpit->getLabelWidthString() !=
1155 comparepit->getLabelWidthString())
1156 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1161 // incalculate the layout spaces
1162 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1163 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1165 rit->height(maxasc + maxdesc + labeladdon);
1166 rit->baseline(maxasc + labeladdon);
1167 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1170 rit->width(int(maxwidth + x));
1172 width = max(0, workWidth());
1173 RowList::iterator rit = firstRow();
1174 RowList::iterator end = endRow();
1175 ParagraphList::iterator it = ownerParagraphs().begin();
1176 while (rit != end) {
1177 if (rit->width() > width)
1178 width = rit->width();
1185 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1187 // allow only if at start or end, or all previous is new text
1188 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1189 && cursor.par()->isChangeEdited(0, cursor.pos()))
1192 LyXTextClass const & tclass =
1193 bv()->buffer()->params.getLyXTextClass();
1194 LyXLayout_ptr const & layout = cursor.par()->layout();
1196 // this is only allowed, if the current paragraph is not empty or caption
1197 // and if it has not the keepempty flag active
1198 if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1199 && layout->labeltype != LABEL_SENSITIVE)
1202 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1204 // Always break behind a space
1206 // It is better to erase the space (Dekel)
1207 if (cursor.pos() < cursor.par()->size()
1208 && cursor.par()->isLineSeparator(cursor.pos()))
1209 cursor.par()->erase(cursor.pos());
1211 // break the paragraph
1215 keep_layout = layout->isEnvironment();
1217 // we need to set this before we insert the paragraph. IMO the
1218 // breakParagraph call should return a bool if it inserts the
1219 // paragraph before or behind and we should react on that one
1220 // but we can fix this in 1.3.0 (Jug 20020509)
1221 bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1222 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1223 cursor.pos(), keep_layout);
1225 #warning Trouble Point! (Lgb)
1226 // When ::breakParagraph is called from within an inset we must
1227 // ensure that the correct ParagraphList is used. Today that is not
1228 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1229 ParagraphList::iterator next_par = boost::next(cursor.par());
1231 // well this is the caption hack since one caption is really enough
1232 if (layout->labeltype == LABEL_SENSITIVE) {
1234 // set to standard-layout
1235 cursor.par()->applyLayout(tclass.defaultLayout());
1237 // set to standard-layout
1238 next_par->applyLayout(tclass.defaultLayout());
1241 // if the cursor is at the beginning of a row without prior newline,
1243 // This touches only the screen-update. Otherwise we would may have
1244 // an empty row on the screen
1245 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1246 && !cursor.par()->isNewline(cursor.pos() - 1))
1251 while (!next_par->empty() && next_par->isNewline(0))
1255 redoParagraph(cursor.par());
1256 redoParagraph(next_par);
1258 // This check is necessary. Otherwise the new empty paragraph will
1259 // be deleted automatically. And it is more friendly for the user!
1260 if (cursor.pos() || isempty)
1261 setCursor(next_par, 0);
1263 setCursor(cursor.par(), 0);
1267 // convenience function
1268 void LyXText::redoParagraph()
1271 redoParagraph(cursor.par());
1272 setCursorIntern(cursor.par(), cursor.pos());
1276 // insert a character, moves all the following breaks in the
1277 // same Paragraph one to the right and make a rebreak
1278 void LyXText::insertChar(char c)
1280 recordUndo(bv(), Undo::INSERT, cursor.par());
1282 // When the free-spacing option is set for the current layout,
1283 // disable the double-space checking
1285 bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1286 cursor.par()->isFreeSpacing();
1288 if (lyxrc.auto_number) {
1289 static string const number_operators = "+-/*";
1290 static string const number_unary_operators = "+-";
1291 static string const number_seperators = ".,:";
1293 if (current_font.number() == LyXFont::ON) {
1294 if (!IsDigit(c) && !contains(number_operators, c) &&
1295 !(contains(number_seperators, c) &&
1296 cursor.pos() >= 1 &&
1297 cursor.pos() < cursor.par()->size() &&
1298 getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1299 getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1301 number(bv()); // Set current_font.number to OFF
1302 } else if (IsDigit(c) &&
1303 real_current_font.isVisibleRightToLeft()) {
1304 number(bv()); // Set current_font.number to ON
1306 if (cursor.pos() > 0) {
1307 char const c = cursor.par()->getChar(cursor.pos() - 1);
1308 if (contains(number_unary_operators, c) &&
1309 (cursor.pos() == 1 ||
1310 cursor.par()->isSeparator(cursor.pos() - 2) ||
1311 cursor.par()->isNewline(cursor.pos() - 2))
1317 } else if (contains(number_seperators, c) &&
1318 cursor.pos() >= 2 &&
1321 cursor.pos() - 2).number() == LyXFont::ON) {
1332 // First check, if there will be two blanks together or a blank at
1333 // the beginning of a paragraph.
1334 // I decided to handle blanks like normal characters, the main
1335 // difference are the special checks when calculating the row.fill
1336 // (blank does not count at the end of a row) and the check here
1338 // The bug is triggered when we type in a description environment:
1339 // The current_font is not changed when we go from label to main text
1340 // and it should (along with realtmpfont) when we type the space.
1341 // CHECK There is a bug here! (Asger)
1343 // store the current font. This is because of the use of cursor
1344 // movements. The moving cursor would refresh the current font
1345 LyXFont realtmpfont = real_current_font;
1346 LyXFont rawtmpfont = current_font;
1348 if (!freeSpacing && IsLineSeparatorChar(c)) {
1349 if ((cursor.pos() > 0
1350 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1351 || (cursor.pos() > 0
1352 && cursor.par()->isNewline(cursor.pos() - 1))
1353 || (cursor.pos() == 0)) {
1354 static bool sent_space_message = false;
1355 if (!sent_space_message) {
1356 if (cursor.pos() == 0)
1357 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1359 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1360 sent_space_message = true;
1367 // Here case LyXText::InsertInset already inserted the character
1368 if (c != Paragraph::META_INSET)
1369 cursor.par()->insertChar(cursor.pos(), c);
1371 setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1373 current_font = rawtmpfont;
1374 real_current_font = realtmpfont;
1375 redoParagraph(cursor.par());
1376 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1382 void LyXText::charInserted()
1384 // Here we could call finishUndo for every 20 characters inserted.
1385 // This is from my experience how emacs does it. (Lgb)
1386 static unsigned int counter;
1396 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1397 RowList::iterator const rit) const
1399 double w = rit->fill();
1400 double fill_hfill = 0;
1401 double fill_label_hfill = 0;
1402 double fill_separator = 0;
1406 pit->isRightToLeftPar(bv()->buffer()->params);
1408 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1410 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1412 // is there a manual margin with a manual label
1413 LyXLayout_ptr const & layout = pit->layout();
1415 if (layout->margintype == MARGIN_MANUAL
1416 && layout->labeltype == LABEL_MANUAL) {
1417 /// We might have real hfills in the label part
1418 int nlh = numberOfLabelHfills(*pit, rit);
1420 // A manual label par (e.g. List) has an auto-hfill
1421 // between the label text and the body of the
1423 // But we don't want to do this auto hfill if the par
1428 if (nlh && !pit->getLabelWidthString().empty()) {
1429 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1433 // are there any hfills in the row?
1434 int const nh = numberOfHfills(*pit, rit);
1438 fill_hfill = w / nh;
1439 // we don't have to look at the alignment if it is ALIGN_LEFT and
1440 // if the row is already larger then the permitted width as then
1441 // we force the LEFT_ALIGN'edness!
1442 } else if (int(rit->width()) < workWidth()) {
1443 // is it block, flushleft or flushright?
1444 // set x how you need it
1446 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1447 align = layout->align;
1449 align = pit->params().align();
1451 InsetOld * inset = 0;
1452 // ERT insets should always be LEFT ALIGNED on screen
1453 inset = pit->inInset();
1454 if (inset && inset->owner() &&
1455 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1457 align = LYX_ALIGN_LEFT;
1461 case LYX_ALIGN_BLOCK:
1463 int const ns = numberOfSeparators(*pit, rit);
1464 RowList::iterator next_row = boost::next(rit);
1466 && next_row != pit->rows.end()
1467 && !pit->isNewline(next_row->pos() - 1)
1469 fill_separator = w / ns;
1470 } else if (is_rtl) {
1475 case LYX_ALIGN_RIGHT:
1478 case LYX_ALIGN_CENTER:
1484 computeBidiTables(pit, *bv()->buffer(), rit);
1486 pos_type body_pos = pit->beginningOfBody();
1487 pos_type last = lastPos(*pit, rit);
1490 (body_pos - 1 > last ||
1491 !pit->isLineSeparator(body_pos - 1))) {
1492 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1493 if (body_pos - 1 <= last)
1494 x += fill_label_hfill;
1498 rit->fill_hfill(fill_hfill);
1499 rit->fill_label_hfill(fill_label_hfill);
1500 rit->fill_separator(fill_separator);
1505 // important for the screen
1508 // the cursor set functions have a special mechanism. When they
1509 // realize, that you left an empty paragraph, they will delete it.
1510 // They also delete the corresponding row
1512 void LyXText::cursorRightOneWord()
1514 ::cursorRightOneWord(cursor, ownerParagraphs());
1515 setCursor(cursor.par(), cursor.pos());
1519 // Skip initial whitespace at end of word and move cursor to *start*
1520 // of prior word, not to end of next prior word.
1521 void LyXText::cursorLeftOneWord()
1523 LyXCursor tmpcursor = cursor;
1524 ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1525 setCursor(tmpcursor.par(), tmpcursor.pos());
1529 void LyXText::selectWord(word_location loc)
1531 LyXCursor from = cursor;
1533 ::getWord(from, to, loc, ownerParagraphs());
1535 setCursor(from.par(), from.pos());
1538 selection.cursor = cursor;
1539 setCursor(to.par(), to.pos());
1544 // Select the word currently under the cursor when no
1545 // selection is currently set
1546 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1548 if (!selection.set()) {
1550 return selection.set();
1556 void LyXText::acceptChange()
1558 if (!selection.set() && cursor.par()->size())
1561 if (selection.start.par() == selection.end.par()) {
1562 LyXCursor & startc = selection.start;
1563 LyXCursor & endc = selection.end;
1564 recordUndo(bv(), Undo::INSERT, startc.par());
1565 startc.par()->acceptChange(startc.pos(), endc.pos());
1568 redoParagraph(startc.par());
1569 setCursorIntern(startc.par(), 0);
1571 #warning handle multi par selection
1575 void LyXText::rejectChange()
1577 if (!selection.set() && cursor.par()->size())
1580 if (selection.start.par() == selection.end.par()) {
1581 LyXCursor & startc = selection.start;
1582 LyXCursor & endc = selection.end;
1583 recordUndo(bv(), Undo::INSERT, startc.par());
1584 startc.par()->rejectChange(startc.pos(), endc.pos());
1587 redoParagraph(startc.par());
1588 setCursorIntern(startc.par(), 0);
1590 #warning handle multi par selection
1594 // This function is only used by the spellchecker for NextWord().
1595 // It doesn't handle LYX_ACCENTs and probably never will.
1597 LyXText::selectNextWordToSpellcheck(float & value)
1599 if (the_locking_inset) {
1600 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1601 if (!word.word().empty()) {
1602 value += float(cursor.y());
1603 value /= float(height);
1606 // we have to go on checking so move cursor to the next char
1607 if (cursor.pos() == cursor.par()->size()) {
1608 if (boost::next(cursor.par()) == ownerParagraphs().end())
1610 cursor.par(boost::next(cursor.par()));
1613 cursor.pos(cursor.pos() + 1);
1615 ParagraphList::iterator tmppit = cursor.par();
1617 // If this is not the very first word, skip rest of
1618 // current word because we are probably in the middle
1619 // of a word if there is text here.
1620 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1621 while (cursor.pos() < cursor.par()->size()
1622 && cursor.par()->isLetter(cursor.pos()))
1623 cursor.pos(cursor.pos() + 1);
1626 // Now, skip until we have real text (will jump paragraphs)
1628 ParagraphList::iterator cpit = cursor.par();
1629 pos_type const cpos(cursor.pos());
1631 if (cpos == cpit->size()) {
1632 if (boost::next(cpit) != ownerParagraphs().end()) {
1633 cursor.par(boost::next(cpit));
1640 bool const is_good_inset = cpit->isInset(cpos)
1641 && cpit->getInset(cpos)->allowSpellcheck();
1643 if (!isDeletedText(*cpit, cpos)
1644 && (is_good_inset || cpit->isLetter(cpos)))
1647 cursor.pos(cpos + 1);
1650 // now check if we hit an inset so it has to be a inset containing text!
1651 if (cursor.pos() < cursor.par()->size() &&
1652 cursor.par()->isInset(cursor.pos())) {
1654 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1655 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1656 // now call us again to do the above trick
1657 // but obviously we have to start from down below ;)
1658 return bv()->text->selectNextWordToSpellcheck(value);
1661 // Update the value if we changed paragraphs
1662 if (cursor.par() != tmppit) {
1663 setCursor(cursor.par(), cursor.pos());
1664 value = float(cursor.y())/float(height);
1667 // Start the selection from here
1668 selection.cursor = cursor;
1670 string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1671 // and find the end of the word (insets like optional hyphens
1672 // and ligature break are part of a word)
1673 while (cursor.pos() < cursor.par()->size()
1674 && cursor.par()->isLetter(cursor.pos())
1675 && !isDeletedText(*cursor.par(), cursor.pos()))
1676 cursor.pos(cursor.pos() + 1);
1678 // Finally, we copy the word to a string and return it
1680 if (selection.cursor.pos() < cursor.pos()) {
1682 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1683 if (!cursor.par()->isInset(i))
1684 str += cursor.par()->getChar(i);
1687 return WordLangTuple(str, lang_code);
1691 // This one is also only for the spellchecker
1692 void LyXText::selectSelectedWord()
1694 if (the_locking_inset) {
1695 the_locking_inset->selectSelectedWord(bv());
1698 // move cursor to the beginning
1699 setCursor(selection.cursor.par(), selection.cursor.pos());
1701 // set the sel cursor
1702 selection.cursor = cursor;
1704 // now find the end of the word
1705 while (cursor.pos() < cursor.par()->size()
1706 && cursor.par()->isLetter(cursor.pos()))
1707 cursor.pos(cursor.pos() + 1);
1709 setCursor(cursor.par(), cursor.pos());
1711 // finally set the selection
1716 // Delete from cursor up to the end of the current or next word.
1717 void LyXText::deleteWordForward()
1719 if (cursor.par()->empty())
1722 LyXCursor tmpcursor = cursor;
1723 selection.set(true); // to avoid deletion
1724 cursorRightOneWord();
1725 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1726 selection.cursor = cursor;
1730 // Great, CutSelection() gets rid of multiple spaces.
1731 cutSelection(true, false);
1736 // Delete from cursor to start of current or prior word.
1737 void LyXText::deleteWordBackward()
1739 if (cursor.par()->empty())
1742 LyXCursor tmpcursor = cursor;
1743 selection.set(true); // to avoid deletion
1744 cursorLeftOneWord();
1745 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1746 selection.cursor = cursor;
1749 cutSelection(true, false);
1754 // Kill to end of line.
1755 void LyXText::deleteLineForward()
1757 if (cursor.par()->empty())
1758 // Paragraph is empty, so we just go to the right
1761 LyXCursor tmpcursor = cursor;
1762 // We can't store the row over a regular setCursor
1763 // so we set it to 0 and reset it afterwards.
1764 selection.set(true); // to avoid deletion
1766 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1767 selection.cursor = cursor;
1770 // What is this test for ??? (JMarc)
1771 if (!selection.set()) {
1772 deleteWordForward();
1774 cutSelection(true, false);
1780 void LyXText::changeCase(LyXText::TextCase action)
1785 if (selection.set()) {
1786 from = selection.start;
1790 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1791 setCursor(to.par(), to.pos() + 1);
1794 recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1796 pos_type pos = from.pos();
1797 ParagraphList::iterator pit = from.par();
1799 while (pit != ownerParagraphs().end() &&
1800 (pos != to.pos() || pit != to.par())) {
1801 if (pos == pit->size()) {
1806 unsigned char c = pit->getChar(pos);
1807 if (!IsInsetChar(c)) {
1809 case text_lowercase:
1812 case text_capitalization:
1814 action = text_lowercase;
1816 case text_uppercase:
1822 pit->setChar(pos, c);
1828 void LyXText::Delete()
1830 // this is a very easy implementation
1832 LyXCursor old_cursor = cursor;
1833 int const old_cur_par_id = old_cursor.par()->id();
1834 int const old_cur_par_prev_id =
1835 (old_cursor.par() != ownerParagraphs().begin() ?
1836 boost::prior(old_cursor.par())->id() : -1);
1838 // just move to the right
1841 // CHECK Look at the comment here.
1842 // This check is not very good...
1843 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1844 // and that can very well delete the par or par->previous in
1845 // old_cursor. Will a solution where we compare paragraph id's
1847 if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1848 == old_cur_par_prev_id
1849 && cursor.par()->id() != old_cur_par_id) {
1850 // delete-empty-paragraph-mechanism has done it
1854 // if you had success make a backspace
1855 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1856 LyXCursor tmpcursor = cursor;
1857 // to make sure undo gets the right cursor position
1858 cursor = old_cursor;
1859 recordUndo(bv(), Undo::DELETE, cursor.par());
1866 void LyXText::backspace()
1868 // Get the font that is used to calculate the baselineskip
1869 pos_type lastpos = cursor.par()->size();
1871 if (cursor.pos() == 0) {
1872 // The cursor is at the beginning of a paragraph,
1873 // so the the backspace will collapse two paragraphs into one.
1875 // but it's not allowed unless it's new
1876 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1879 // we may paste some paragraphs
1881 // is it an empty paragraph?
1884 || (lastpos == 1 && cursor.par()->isSeparator(0))) {
1885 // This is an empty paragraph and we delete it just
1886 // by moving the cursor one step
1887 // left and let the DeleteEmptyParagraphMechanism
1888 // handle the actual deletion of the paragraph.
1890 if (cursor.par() != ownerParagraphs().begin()) {
1891 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1892 if (cursor.par()->layout() == tmppit->layout()
1893 && cursor.par()->getAlign() == tmppit->getAlign()) {
1894 // Inherit bottom DTD from the paragraph below.
1895 // (the one we are deleting)
1896 tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1897 tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1898 tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1903 // the layout things can change the height of a row !
1909 if (cursor.par() != ownerParagraphs().begin()) {
1910 recordUndo(bv(), Undo::DELETE,
1911 boost::prior(cursor.par()),
1915 ParagraphList::iterator tmppit = cursor.par();
1916 // We used to do cursorLeftIntern() here, but it is
1917 // not a good idea since it triggers the auto-delete
1918 // mechanism. So we do a cursorLeftIntern()-lite,
1919 // without the dreaded mechanism. (JMarc)
1920 if (cursor.par() != ownerParagraphs().begin()) {
1921 // steps into the above paragraph.
1922 setCursorIntern(boost::prior(cursor.par()),
1923 boost::prior(cursor.par())->size(),
1927 // Pasting is not allowed, if the paragraphs have different
1928 // layout. I think it is a real bug of all other
1929 // word processors to allow it. It confuses the user.
1930 // Correction: Pasting is always allowed with standard-layout
1931 LyXTextClass const & tclass =
1932 bv()->buffer()->params.getLyXTextClass();
1934 if (cursor.par() != tmppit
1935 && (cursor.par()->layout() == tmppit->layout()
1936 || tmppit->layout() == tclass.defaultLayout())
1937 && cursor.par()->getAlign() == tmppit->getAlign()) {
1938 mergeParagraph(bv()->buffer()->params,
1939 bv()->buffer()->paragraphs, cursor.par());
1941 if (cursor.pos() && cursor.par()->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, 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 cursor.par()->erase(cursor.pos());
1961 lastpos = cursor.par()->size();
1962 if (cursor.pos() == lastpos)
1966 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1970 RowList::iterator LyXText::cursorRow() const
1972 return getRow(cursor.par(), cursor.pos());
1976 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
1978 return getRow(cur.par(), cur.pos());
1983 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
1985 RowList::iterator rit = boost::prior(pit->rows.end());
1986 RowList::iterator const begin = pit->rows.begin();
1988 while (rit != begin && rit->pos() > pos)
1995 // returns pointer to some fancy row 'below' specified row
1996 RowList::iterator LyXText::cursorIRow() const
1998 return getRow(cursor.par(), cursor.pos());
2002 RowList::iterator LyXText::getRowNearY(int y,
2003 ParagraphList::iterator & pit) const
2005 //lyxerr << "getRowNearY: y " << y << endl;
2007 pit = boost::prior(ownerParagraphs().end());
2009 RowList::iterator rit = lastRow();
2010 RowList::iterator rbegin = firstRow();
2012 while (rit != rbegin && static_cast<int>(rit->y()) > y)
2013 previousRow(pit, rit);
2019 int LyXText::getDepth() const
2021 return cursor.par()->getDepth();
2025 RowList::iterator LyXText::firstRow() const
2027 return ownerParagraphs().front().rows.begin();
2031 RowList::iterator LyXText::lastRow() const
2033 return boost::prior(endRow());
2037 RowList::iterator LyXText::endRow() const
2039 return ownerParagraphs().back().rows.end();
2043 void LyXText::nextRow(ParagraphList::iterator & pit,
2044 RowList::iterator & rit) const
2047 if (rit == pit->rows.end()) {
2049 if (pit == ownerParagraphs().end())
2052 rit = pit->rows.begin();
2057 void LyXText::previousRow(ParagraphList::iterator & pit,
2058 RowList::iterator & rit) const
2060 if (rit != pit->rows.begin())
2063 Assert(pit != ownerParagraphs().begin());
2065 rit = boost::prior(pit->rows.end());
2070 bool LyXText::noRows() const
2072 return ownerParagraphs().begin()->rows.empty();