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.
20 #include "paragraph.h"
22 #include "bufferparams.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/Painter.h"
30 #include "frontends/font_metrics.h"
31 #include "frontends/screen.h"
32 #include "frontends/WorkArea.h"
33 #include "bufferview_funcs.h"
34 #include "BufferView.h"
36 #include "ParagraphParameters.h"
37 #include "undo_funcs.h"
38 #include "text_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
41 #include "rowpainter.h"
42 #include "lyxrow_funcs.h"
43 #include "metricsinfo.h"
45 #include "insets/insettext.h"
47 #include "support/textutils.h"
48 #include "support/LAssert.h"
49 #include "support/lstrings.h"
53 using namespace lyx::support;
61 using lyx::word_location;
63 using namespace bv_funcs;
65 /// top, right, bottom pixel margin
66 extern int const PAPER_MARGIN = 20;
67 /// margin for changebar
68 extern int const CHANGEBAR_MARGIN = 10;
70 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
72 int bibitemMaxWidth(BufferView *, LyXFont const &);
75 BufferView * LyXText::bv()
77 Assert(bv_owner != 0);
82 BufferView * LyXText::bv() const
84 Assert(bv_owner != 0);
89 void LyXText::updateRowPositions()
91 ParagraphList::iterator pit = ownerParagraphs().begin();
92 ParagraphList::iterator end = ownerParagraphs().end();
93 for (height = 0; pit != end; ++pit) {
94 RowList::iterator rit = pit->rows.begin();
95 RowList::iterator rend = pit->rows.end();
96 for ( ; rit != rend ; rit = ++rit) {
98 height += rit->height();
104 int LyXText::top_y() const
110 void LyXText::top_y(int newy)
113 lyxerr[Debug::GUI] << "changing reference to offset: " << anchor_y_ << endl;
117 int LyXText::workWidth() const
119 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
123 int LyXText::getRealCursorX() const
126 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
127 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
132 #warning FIXME This function seems to belong outside of LyxText.
133 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
136 if (!Encodings::is_arabic(c))
137 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
138 return c + (0xb0 - '0');
142 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
143 unsigned char next_char = ' ';
145 pos_type const par_size = par.size();
147 for (pos_type i = pos + 1; i < par_size; ++i) {
148 unsigned char const par_char = par.getChar(i);
149 if (!Encodings::IsComposeChar_arabic(par_char)) {
150 next_char = par_char;
155 if (Encodings::is_arabic(next_char)) {
156 if (Encodings::is_arabic(prev_char) &&
157 !Encodings::is_arabic_special(prev_char))
158 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
160 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
162 if (Encodings::is_arabic(prev_char) &&
163 !Encodings::is_arabic_special(prev_char))
164 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
166 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
170 // This is the comments that some of the warnings below refers to.
171 // There are some issues in this file and I don't think they are
172 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
173 // this is a problem that has been here almost from day one and that a
174 // larger userbase with differenct access patters triggers the bad
175 // behaviour. (segfaults.) What I think happen is: In several places
176 // we store the paragraph in the current cursor and then moves the
177 // cursor. This movement of the cursor will delete paragraph at the
178 // old position if it is now empty. This will make the temporary
179 // pointer to the old cursor paragraph invalid and dangerous to use.
180 // And is some cases this will trigger a segfault. I have marked some
181 // of the cases where this happens with a warning, but I am sure there
182 // are others in this file and in text2.C. There is also a note in
183 // Delete() that you should read. In Delete I store the paragraph->id
184 // instead of a pointer to the paragraph. I am pretty sure this faulty
185 // use of temporary pointers to paragraphs that might have gotten
186 // invalidated (through a cursor movement) before they are used, are
187 // the cause of the strange crashes we get reported often.
189 // It is very tiresom to change this code, especially when it is as
190 // hard to read as it is. Help to fix all the cases where this is done
191 // would be greately appreciated.
195 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
197 if (pos >= pit->size())
200 char const c = pit->getChar(pos);
201 LyXFont const & font = getFont(pit, pos);
202 return singleWidth(pit, pos, c, font);
206 int LyXText::singleWidth(ParagraphList::iterator pit,
207 pos_type pos, char c, LyXFont const & font) const
209 if (pos >= pit->size()) {
210 lyxerr << "in singleWidth(), pos: " << pos << endl;
216 // The most common case is handled first (Asger)
217 if (IsPrintable(c)) {
218 if (font.language()->RightToLeft()) {
219 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
220 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
221 && font.language()->lang() == "arabic") {
222 if (Encodings::IsComposeChar_arabic(c))
225 c = transformChar(c, *pit, pos);
226 } else if (font.language()->lang() == "hebrew" &&
227 Encodings::IsComposeChar_hebrew(c))
230 return font_metrics::width(c, font);
233 if (c == Paragraph::META_INSET) {
234 InsetOld * tmpinset = pit->getInset(pos);
236 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
237 // Because of the representation as vertical lines
240 return tmpinset->width();
243 if (IsSeparatorChar(c))
245 return font_metrics::width(c, font);
249 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
251 if (bidi_start == -1)
254 return log2vis_list[pos - bidi_start];
258 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
260 if (bidi_start == -1)
263 return vis2log_list[pos - bidi_start];
267 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
269 if (bidi_start == -1)
272 return bidi_levels[pos - bidi_start];
276 bool LyXText::bidi_InRange(lyx::pos_type pos) const
278 return bidi_start == -1 ||
279 (bidi_start <= pos && pos <= bidi_end);
283 void LyXText::computeBidiTables(ParagraphList::iterator pit,
284 Buffer const * buf, RowList::iterator row) const
286 bidi_same_direction = true;
287 if (!lyxrc.rtl_support) {
292 InsetOld * inset = pit->inInset();
293 if (inset && inset->owner() &&
294 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
299 bidi_start = row->pos();
300 bidi_end = lastPrintablePos(*pit, row);
302 if (bidi_start > bidi_end) {
307 if (bidi_end + 2 - bidi_start >
308 static_cast<pos_type>(log2vis_list.size())) {
310 (bidi_end + 2 - bidi_start < 500) ?
311 500 : 2 * (bidi_end + 2 - bidi_start);
312 log2vis_list.resize(new_size);
313 vis2log_list.resize(new_size);
314 bidi_levels.resize(new_size);
317 vis2log_list[bidi_end + 1 - bidi_start] = -1;
318 log2vis_list[bidi_end + 1 - bidi_start] = -1;
322 pit->isRightToLeftPar(buf->params);
326 pos_type const body_pos = pit->beginningOfBody();
328 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
329 bool is_space = pit->isLineSeparator(lpos);
331 (is_space && lpos + 1 <= bidi_end &&
332 !pit->isLineSeparator(lpos + 1) &&
333 !pit->isNewline(lpos + 1))
335 LyXFont font = pit->getFontSettings(buf->params, pos);
336 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
337 font.number() == LyXFont::ON &&
338 pit->getFontSettings(buf->params, lpos - 1).number()
340 font = pit->getFontSettings(buf->params, lpos);
345 bool new_rtl = font.isVisibleRightToLeft();
346 bool new_rtl0 = font.isRightToLeft();
349 if (lpos == body_pos - 1
350 && row->pos() < body_pos - 1
352 new_level = (rtl_par) ? 1 : 0;
353 new_rtl = new_rtl0 = rtl_par;
355 new_level = (new_rtl) ? 1 : 2;
357 new_level = (rtl_par) ? 2 : 0;
359 if (is_space && new_level >= level) {
365 int new_level2 = new_level;
367 if (level == new_level && rtl0 != new_rtl0) {
369 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
370 } else if (level < new_level) {
371 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
372 if (new_level > rtl_par)
373 bidi_same_direction = false;
375 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
378 bidi_levels[lpos - bidi_start] = new_level;
380 while (level > new_level2) {
381 pos_type old_lpos = stack[--level];
382 int delta = lpos - old_lpos - 1;
385 log2vis_list[lpos - bidi_start] += delta;
386 log2vis_list[old_lpos - bidi_start] += delta;
388 while (level < new_level)
389 stack[level++] = lpos;
393 pos_type const old_lpos = stack[--level];
394 int delta = bidi_end - old_lpos;
397 log2vis_list[old_lpos - bidi_start] += delta;
400 pos_type vpos = bidi_start - 1;
401 for (pos_type lpos = bidi_start;
402 lpos <= bidi_end; ++lpos) {
403 vpos += log2vis_list[lpos - bidi_start];
404 vis2log_list[vpos - bidi_start] = lpos;
405 log2vis_list[lpos - bidi_start] = vpos;
410 // This method requires a previous call to ComputeBidiTables()
411 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
414 if (!lyxrc.rtl_support || pos == 0)
417 if (!bidi_InRange(pos - 1)) {
418 /// This can happen if pos is the first char of a row.
419 /// Returning false in this case is incorrect!
423 bool const rtl = bidi_level(pos - 1) % 2;
424 bool const rtl2 = bidi_InRange(pos)
425 ? bidi_level(pos) % 2
426 : par.isRightToLeftPar(buf->params);
431 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
432 pos_type pos, LyXFont const & font) const
434 if (!lyxrc.rtl_support)
435 return false; // This is just for speedup
437 bool const rtl = font.isVisibleRightToLeft();
438 bool const rtl2 = bidi_InRange(pos)
439 ? bidi_level(pos) % 2
440 : par.isRightToLeftPar(buf->params);
445 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
449 if (row.pos() < pit->size())
450 if (pit->getChar(row.pos()) == Paragraph::META_INSET &&
451 (ins = pit->getInset(row.pos())) &&
452 (ins->needFullRow() || ins->display()))
455 LyXTextClass const & tclass =
456 bv()->buffer()->params.getLyXTextClass();
457 LyXLayout_ptr const & layout = pit->layout();
459 string parindent = layout->parindent;
463 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
465 // this is the way, LyX handles the LaTeX-Environments.
466 // I have had this idea very late, so it seems to be a
467 // later added hack and this is true
468 if (!pit->getDepth()) {
469 if (pit->layout() == tclass.defaultLayout()) {
470 // find the previous same level paragraph
471 if (pit != ownerParagraphs().begin()) {
472 ParagraphList::iterator newpit =
473 depthHook(pit, ownerParagraphs(),
476 newpit->layout()->nextnoindent)
481 // find the next level paragraph
483 ParagraphList::iterator newpar = outerHook(pit,
486 // make a corresponding row. Needed to call leftMargin()
488 // check wether it is a sufficent paragraph
489 if (newpar != ownerParagraphs().end() &&
490 newpar->layout()->isEnvironment()) {
491 x = leftMargin(newpar, Row(newpar->size()));
494 if (newpar != ownerParagraphs().end() &&
495 pit->layout() == tclass.defaultLayout()) {
496 if (newpar->params().noindent())
499 parindent = newpar->layout()->parindent;
505 LyXFont const labelfont = getLabelFont(pit);
506 switch (layout->margintype) {
508 if (!layout->leftmargin.empty()) {
509 x += font_metrics::signedWidth(layout->leftmargin,
510 tclass.defaultfont());
512 if (!pit->getLabelstring().empty()) {
513 x += font_metrics::signedWidth(layout->labelindent,
515 x += font_metrics::width(pit->getLabelstring(),
517 x += font_metrics::width(layout->labelsep, labelfont);
521 x += font_metrics::signedWidth(layout->labelindent, labelfont);
522 // The width of an empty par, even with manual label, should be 0
523 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
524 if (!pit->getLabelWidthString().empty()) {
525 x += font_metrics::width(pit->getLabelWidthString(),
527 x += font_metrics::width(layout->labelsep, labelfont);
532 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
533 / (pit->getDepth() + 4);
535 case MARGIN_FIRST_DYNAMIC:
536 if (layout->labeltype == LABEL_MANUAL) {
537 if (row.pos() >= pit->beginningOfBody()) {
538 x += font_metrics::signedWidth(layout->leftmargin,
541 x += font_metrics::signedWidth(layout->labelindent,
545 // Special case to fix problems with
547 || (layout->labeltype == LABEL_STATIC
548 && layout->latextype == LATEX_ENVIRONMENT
549 && !isFirstInSequence(pit, ownerParagraphs()))) {
550 x += font_metrics::signedWidth(layout->leftmargin,
552 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
553 && layout->labeltype != LABEL_BIBLIO
554 && layout->labeltype !=
555 LABEL_CENTERED_TOP_ENVIRONMENT) {
556 x += font_metrics::signedWidth(layout->labelindent,
558 x += font_metrics::width(layout->labelsep, labelfont);
559 x += font_metrics::width(pit->getLabelstring(),
564 case MARGIN_RIGHT_ADDRESS_BOX:
566 // ok, a terrible hack. The left margin depends on the widest
567 // row in this paragraph. Do not care about footnotes, they
568 // are *NOT* allowed in the LaTeX realisation of this layout.
570 // find the first row of this paragraph
571 RowList::iterator rit = pit->rows.begin();
572 RowList::iterator end = pit->rows.end();
573 int minfill = rit->fill();
574 for ( ; rit != end; ++rit)
575 if (rit->fill() < minfill)
576 minfill = rit->fill();
578 x += font_metrics::signedWidth(layout->leftmargin,
579 tclass.defaultfont());
585 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
586 LyXLength const len = pit->params().leftIndent();
587 int const tw = inset_owner ?
588 inset_owner->latexTextWidth(bv()) : workWidth();
589 x += len.inPixels(tw);
594 if (pit->params().align() == LYX_ALIGN_LAYOUT)
595 align = layout->align;
597 align = pit->params().align();
599 // set the correct parindent
600 if (row.pos() == 0) {
601 if ((layout->labeltype == LABEL_NO_LABEL
602 || layout->labeltype == LABEL_TOP_ENVIRONMENT
603 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
604 || (layout->labeltype == LABEL_STATIC
605 && layout->latextype == LATEX_ENVIRONMENT
606 && !isFirstInSequence(pit, ownerParagraphs())))
607 && align == LYX_ALIGN_BLOCK
608 && !pit->params().noindent()
609 // in tabulars and ert paragraphs are never indented!
610 && (!pit->inInset() || !pit->inInset()->owner() ||
611 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
612 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
613 && (pit->layout() != tclass.defaultLayout() ||
614 bv()->buffer()->params.paragraph_separation ==
615 BufferParams::PARSEP_INDENT)) {
616 x += font_metrics::signedWidth(parindent,
617 tclass.defaultfont());
618 } else if (layout->labeltype == LABEL_BIBLIO) {
619 // ale970405 Right width for bibitems
620 x += bibitemMaxWidth(bv(), tclass.defaultfont());
628 int LyXText::rightMargin(ParagraphList::iterator pit,
629 Buffer const & buf, Row const & row) const
633 if (row.pos() < pit->size())
634 if ((pit->getChar(row.pos()) == Paragraph::META_INSET) &&
635 (ins = pit->getInset(row.pos())) &&
636 (ins->needFullRow() || ins->display()))
639 LyXTextClass const & tclass = buf.params.getLyXTextClass();
640 LyXLayout_ptr const & layout = pit->layout();
643 + font_metrics::signedWidth(tclass.rightmargin(),
644 tclass.defaultfont());
645 + font_metrics::signedWidth(layout->rightmargin,
646 tclass.defaultfont())
647 * 4 / (pit->getDepth() + 4);
651 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
653 if (pit->layout()->margintype == MARGIN_MANUAL) {
655 tmprow.pos(pit->size());
656 // return the beginning of the body
657 return leftMargin(pit, tmprow);
660 // LabelEnd is only needed if the layout
661 // fills a flushleft label.
668 // this needs special handling - only newlines count as a break point
669 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
671 for (; i < par.size(); ++i) {
672 if (par.isNewline(i))
682 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
683 Row const & row) const
685 // maximum pixel width of a row.
686 int width = workWidth() - rightMargin(pit, *bv()->buffer(), row);
688 // inset->textWidth() returns -1 via workWidth(),
693 LyXLayout_ptr const & layout = pit->layout();
695 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
696 return addressBreakPoint(row.pos(), *pit);
698 pos_type const pos = row.pos();
699 pos_type const body_pos = pit->beginningOfBody();
700 pos_type const last = pit->size();
701 pos_type point = last;
706 // Now we iterate through until we reach the right margin
707 // or the end of the par, then choose the possible break
710 int const left = leftMargin(pit, row);
713 // pixel width since last breakpoint
715 bool fullrow = false;
719 // We re-use the font resolution for the entire font span when possible
720 LyXFont font = getFont(pit, i);
721 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
723 for (; i < last; ++i) {
724 if (pit->isNewline(i)) {
729 char const c = pit->getChar(i);
730 if (i > endPosOfFontSpan) {
731 font = getFont(pit, i);
732 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
737 // add the auto-hfill from label end to the body
738 if (body_pos && i == body_pos) {
739 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
740 if (pit->isLineSeparator(i - 1))
741 thiswidth -= singleWidth(pit, i - 1);
742 int left_margin = labelEnd(pit, row);
743 if (thiswidth + x < left_margin)
744 thiswidth = left_margin - x;
745 thiswidth += singleWidth(pit, i, c, font);
747 thiswidth = singleWidth(pit, i, c, font);
751 chunkwidth += thiswidth;
753 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
754 fullrow = in && (in->display() || in->needFullRow());
756 // break before a character that will fall off
757 // the right of the row
759 // if no break before or we are at an inset
760 // that will take up a row, break here
761 if (point == last || fullrow || chunkwidth >= (width - left)) {
770 if (!in || in->isChar()) {
771 // some insets are line separators too
772 if (pit->isLineSeparator(i)) {
782 // full row insets start at a new row
784 if (pos < last - 1) {
786 if (pit->isLineSeparator(i + 1))
789 // to avoid extra rows
799 if (point == last && x >= width) {
800 // didn't find one, break at the point we reached the edge
802 } else if (i == last && x < width) {
803 // found one, but we fell off the end of the par, so prefer
808 // manual labels cannot be broken in LaTeX. But we
809 // want to make our on-screen rendering of footnotes
811 if (!fullrow && body_pos && point < body_pos)
812 point = body_pos - 1;
818 // returns the minimum space a row needs on the screen in pixel
819 int LyXText::fill(ParagraphList::iterator pit,
820 RowList::iterator row, int paper_width) const
826 // get the pure distance
827 pos_type const last = lastPrintablePos(*pit, row);
829 LyXLayout_ptr const & layout = pit->layout();
831 // special handling of the right address boxes
832 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
833 int const tmpfill = row->fill();
834 row->fill(0); // the minfill in MarginLeft()
835 w = leftMargin(pit, *row);
838 w = leftMargin(pit, *row);
840 pos_type const body_pos = pit->beginningOfBody();
841 pos_type i = row->pos();
843 if (! pit->empty() && i <= last) {
844 // We re-use the font resolution for the entire span when possible
845 LyXFont font = getFont(pit, i);
846 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
848 if (body_pos > 0 && i == body_pos) {
849 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
850 if (pit->isLineSeparator(i - 1))
851 w -= singleWidth(pit, i - 1);
852 int left_margin = labelEnd(pit, *row);
856 char const c = pit->getChar(i);
857 if (IsPrintable(c) && i > endPosOfFontSpan) {
858 // We need to get the next font
859 font = getFont(pit, i);
860 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
862 w += singleWidth(pit, i, c, font);
866 if (body_pos > 0 && body_pos > last) {
867 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
868 if (last >= 0 && pit->isLineSeparator(last))
869 w -= singleWidth(pit, last);
870 int const left_margin = labelEnd(pit, *row);
875 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
877 // If this case happens, it means that our calculation
878 // of the widths of the chars when we do rowBreakPoint()
879 // went wrong for some reason. Typically in list bodies.
880 // Things just about hobble on anyway, though you'll end
881 // up with a "fill_separator" less than zero, which corresponds
882 // to inter-word spacing being too small. Hopefully this problem
883 // will die when the label hacks die.
884 if (lyxerr.debugging() && fill < 0) {
885 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
886 << " w " << w << " paper_width " << paper_width
887 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
893 // returns the minimum space a manual label needs on the screen in pixel
894 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
896 pos_type last = pit->beginningOfBody();
900 // -1 because a label ends either with a space that is in the label,
901 // or with the beginning of a footnote that is outside the label.
904 // a separator at this end does not count
905 if (pit->isLineSeparator(last))
909 pos_type i = row.pos();
911 w += singleWidth(pit, i);
916 string const & labwidstr = pit->params().labelWidthString();
917 if (!labwidstr.empty()) {
918 LyXFont const labfont = getLabelFont(pit);
919 int const labwidth = font_metrics::width(labwidstr, labfont);
920 fill = max(labwidth - w, 0);
927 LColor::color LyXText::backgroundColor() const
930 return inset_owner->backgroundColor();
932 return LColor::background;
936 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
938 // get the maximum ascent and the maximum descent
939 double layoutasc = 0;
940 double layoutdesc = 0;
943 // ok, let us initialize the maxasc and maxdesc value.
944 // Only the fontsize count. The other properties
945 // are taken from the layoutfont. Nicer on the screen :)
946 LyXLayout_ptr const & layout = pit->layout();
948 // as max get the first character of this row then it can increase but not
949 // decrease the height. Just some point to start with so we don't have to
950 // do the assignment below too often.
951 LyXFont font = getFont(pit, rit->pos());
952 LyXFont::FONT_SIZE const tmpsize = font.size();
953 font = getLayoutFont(pit);
954 LyXFont::FONT_SIZE const size = font.size();
955 font.setSize(tmpsize);
957 LyXFont labelfont = getLabelFont(pit);
959 double spacing_val = 1.0;
960 if (!pit->params().spacing().isDefault())
961 spacing_val = pit->params().spacing().getValue();
963 spacing_val = bv()->buffer()->params.spacing.getValue();
964 //lyxerr << "spacing_val = " << spacing_val << endl;
966 int maxasc = int(font_metrics::maxAscent(font) *
967 layout->spacing.getValue() * spacing_val);
968 int maxdesc = int(font_metrics::maxDescent(font) *
969 layout->spacing.getValue() * spacing_val);
971 pos_type const pos_end = lastPos(*pit, rit);
976 // We re-use the font resolution for the entire font span when possible
977 LyXFont font = getFont(pit, rit->pos());
978 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
981 Paragraph const & par = *pit;
983 // Check if any insets are larger
984 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
985 // Manual inlined optimised version of common case of
986 // "maxwidth += singleWidth(pit, pos);"
987 char const c = par.getChar(pos);
989 if (IsPrintable(c)) {
990 if (pos > endPosOfFontSpan) {
991 // We need to get the next font
992 font = getFont(pit, pos);
993 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
995 if (! font.language()->RightToLeft()) {
996 maxwidth += font_metrics::width(c, font);
998 // Fall-back to normal case
999 maxwidth += singleWidth(pit, pos, c, font);
1000 // And flush font cache
1001 endPosOfFontSpan = 0;
1004 // Special handling of insets - are any larger?
1005 if (par.isInset(pos)) {
1006 InsetOld const * tmpinset = par.getInset(pos);
1008 maxwidth += tmpinset->width();
1009 maxasc = max(maxasc, tmpinset->ascent());
1010 maxdesc = max(maxdesc, tmpinset->descent());
1013 // Fall-back to normal case
1014 maxwidth += singleWidth(pit, pos, c, font);
1015 // And flush font cache
1016 endPosOfFontSpan = 0;
1022 // Check if any custom fonts are larger (Asger)
1023 // This is not completely correct, but we can live with the small,
1024 // cosmetic error for now.
1025 LyXFont::FONT_SIZE maxsize =
1026 pit->highestFontInRange(rit->pos(), pos_end, size);
1027 if (maxsize > font.size()) {
1028 font.setSize(maxsize);
1029 maxasc = max(maxasc, font_metrics::maxAscent(font));
1030 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
1033 // This is nicer with box insets:
1037 rit->ascent_of_text(maxasc);
1039 // is it a top line?
1042 // some parksips VERY EASY IMPLEMENTATION
1043 if (bv()->buffer()->params.paragraph_separation ==
1044 BufferParams::PARSEP_SKIP)
1046 if (layout->isParagraph()
1047 && pit->getDepth() == 0
1048 && pit != ownerParagraphs().begin())
1050 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1051 } else if (pit != ownerParagraphs().begin() &&
1052 boost::prior(pit)->layout()->isParagraph() &&
1053 boost::prior(pit)->getDepth() == 0)
1055 // is it right to use defskip here too? (AS)
1056 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1061 if (pit == ownerParagraphs().begin() && !isInInset())
1062 maxasc += PAPER_MARGIN;
1064 // add the vertical spaces, that the user added
1065 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1067 // do not forget the DTP-lines!
1068 // there height depends on the font of the nearest character
1069 if (pit->params().lineTop())
1071 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1072 // and now the pagebreaks
1073 if (pit->params().pagebreakTop())
1074 maxasc += 3 * defaultRowHeight();
1076 if (pit->params().startOfAppendix())
1077 maxasc += 3 * defaultRowHeight();
1079 // This is special code for the chapter, since the label of this
1080 // layout is printed in an extra row
1081 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1082 && bv()->buffer()->params.secnumdepth >= 0)
1084 float spacing_val = 1.0;
1085 if (!pit->params().spacing().isDefault()) {
1086 spacing_val = pit->params().spacing().getValue();
1088 spacing_val = bv()->buffer()->params.spacing.getValue();
1091 labeladdon = int(font_metrics::maxDescent(labelfont) *
1092 layout->spacing.getValue() *
1094 + int(font_metrics::maxAscent(labelfont) *
1095 layout->spacing.getValue() *
1099 // special code for the top label
1100 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1101 || layout->labeltype == LABEL_BIBLIO
1102 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1103 && isFirstInSequence(pit, ownerParagraphs())
1104 && !pit->getLabelstring().empty())
1106 float spacing_val = 1.0;
1107 if (!pit->params().spacing().isDefault()) {
1108 spacing_val = pit->params().spacing().getValue();
1110 spacing_val = bv()->buffer()->params.spacing.getValue();
1114 (font_metrics::maxAscent(labelfont) +
1115 font_metrics::maxDescent(labelfont)) *
1116 layout->spacing.getValue() *
1118 + layout->topsep * defaultRowHeight()
1119 + layout->labelbottomsep * defaultRowHeight());
1122 // And now the layout spaces, for example before and after
1123 // a section, or between the items of a itemize or enumerate
1126 if (!pit->params().pagebreakTop()) {
1127 ParagraphList::iterator prev =
1128 depthHook(pit, ownerParagraphs(),
1130 if (prev != pit && prev->layout() == layout &&
1131 prev->getDepth() == pit->getDepth() &&
1132 prev->getLabelWidthString() == pit->getLabelWidthString())
1134 layoutasc = (layout->itemsep * defaultRowHeight());
1135 } else if (rit != firstRow()) {
1136 tmptop = layout->topsep;
1138 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1139 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1142 layoutasc = (tmptop * defaultRowHeight());
1143 } else if (pit->params().lineTop()) {
1144 tmptop = layout->topsep;
1147 layoutasc = (tmptop * defaultRowHeight());
1150 prev = outerHook(pit, ownerParagraphs());
1151 if (prev != ownerParagraphs().end()) {
1152 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1153 } else if (pit != ownerParagraphs().begin()) {
1154 ParagraphList::iterator prior_pit = boost::prior(pit);
1155 if (prior_pit->getDepth() != 0 ||
1156 prior_pit->layout() == layout) {
1157 maxasc += int(layout->parsep * defaultRowHeight());
1163 // is it a bottom line?
1164 if (boost::next(rit) == pit->rows.end()) {
1165 // the bottom margin
1166 ParagraphList::iterator nextpit = boost::next(pit);
1167 if (nextpit == ownerParagraphs().end() && !isInInset())
1168 maxdesc += PAPER_MARGIN;
1170 // add the vertical spaces, that the user added
1171 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1173 // do not forget the DTP-lines!
1174 // there height depends on the font of the nearest character
1175 if (pit->params().lineBottom())
1176 maxdesc += 2 * font_metrics::ascent('x',
1177 getFont(pit, max(pos_type(0), pit->size() - 1)));
1179 // and now the pagebreaks
1180 if (pit->params().pagebreakBottom())
1181 maxdesc += 3 * defaultRowHeight();
1183 // and now the layout spaces, for example before and after
1184 // a section, or between the items of a itemize or enumerate
1186 if (!pit->params().pagebreakBottom()
1187 && nextpit != ownerParagraphs().end()) {
1188 ParagraphList::iterator comparepit = pit;
1192 if (comparepit->getDepth() > nextpit->getDepth()) {
1193 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1194 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1195 if (comparepit->layout()!= nextpit->layout()
1196 || nextpit->getLabelWidthString() !=
1197 comparepit->getLabelWidthString())
1199 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1201 if (unusual > usual)
1202 layoutdesc = unusual;
1205 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1207 if (comparepit->layout() != nextpit->layout()
1208 || nextpit->getLabelWidthString() !=
1209 comparepit->getLabelWidthString())
1210 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1215 // incalculate the layout spaces
1216 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1217 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1219 rit->height(maxasc + maxdesc + labeladdon);
1220 rit->baseline(maxasc + labeladdon);
1221 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1224 rit->width(int(maxwidth + x));
1226 width = max(0, workWidth());
1227 RowList::iterator rit = firstRow();
1228 RowList::iterator end = endRow();
1229 ParagraphList::iterator it = ownerParagraphs().begin();
1230 while (rit != end) {
1231 if (rit->width() > width)
1232 width = rit->width();
1239 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1241 // allow only if at start or end, or all previous is new text
1242 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1243 && cursor.par()->isChangeEdited(0, cursor.pos()))
1246 LyXTextClass const & tclass =
1247 bv()->buffer()->params.getLyXTextClass();
1248 LyXLayout_ptr const & layout = cursor.par()->layout();
1250 // this is only allowed, if the current paragraph is not empty or caption
1251 // and if it has not the keepempty flag active
1252 if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1253 && layout->labeltype != LABEL_SENSITIVE)
1256 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1258 // Always break behind a space
1260 // It is better to erase the space (Dekel)
1261 if (cursor.pos() < cursor.par()->size()
1262 && cursor.par()->isLineSeparator(cursor.pos()))
1263 cursor.par()->erase(cursor.pos());
1265 // break the paragraph
1269 keep_layout = layout->isEnvironment();
1271 // we need to set this before we insert the paragraph. IMO the
1272 // breakParagraph call should return a bool if it inserts the
1273 // paragraph before or behind and we should react on that one
1274 // but we can fix this in 1.3.0 (Jug 20020509)
1275 bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1276 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1277 cursor.pos(), keep_layout);
1279 #warning Trouble Point! (Lgb)
1280 // When ::breakParagraph is called from within an inset we must
1281 // ensure that the correct ParagraphList is used. Today that is not
1282 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1283 ParagraphList::iterator next_par = boost::next(cursor.par());
1285 // well this is the caption hack since one caption is really enough
1286 if (layout->labeltype == LABEL_SENSITIVE) {
1288 // set to standard-layout
1289 cursor.par()->applyLayout(tclass.defaultLayout());
1291 // set to standard-layout
1292 next_par->applyLayout(tclass.defaultLayout());
1295 // if the cursor is at the beginning of a row without prior newline,
1297 // This touches only the screen-update. Otherwise we would may have
1298 // an empty row on the screen
1299 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1300 && !cursor.par()->isNewline(cursor.pos() - 1))
1305 while (!next_par->empty() && next_par->isNewline(0))
1309 redoParagraph(cursor.par());
1310 redoParagraph(next_par);
1312 // This check is necessary. Otherwise the new empty paragraph will
1313 // be deleted automatically. And it is more friendly for the user!
1314 if (cursor.pos() || isempty)
1315 setCursor(next_par, 0);
1317 setCursor(cursor.par(), 0);
1321 // convenience function
1322 void LyXText::redoParagraph()
1325 redoParagraph(cursor.par());
1326 setCursorIntern(cursor.par(), cursor.pos());
1330 // insert a character, moves all the following breaks in the
1331 // same Paragraph one to the right and make a rebreak
1332 void LyXText::insertChar(char c)
1334 recordUndo(bv(), Undo::INSERT, cursor.par());
1336 // When the free-spacing option is set for the current layout,
1337 // disable the double-space checking
1339 bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1340 cursor.par()->isFreeSpacing();
1342 if (lyxrc.auto_number) {
1343 static string const number_operators = "+-/*";
1344 static string const number_unary_operators = "+-";
1345 static string const number_seperators = ".,:";
1347 if (current_font.number() == LyXFont::ON) {
1348 if (!IsDigit(c) && !contains(number_operators, c) &&
1349 !(contains(number_seperators, c) &&
1350 cursor.pos() >= 1 &&
1351 cursor.pos() < cursor.par()->size() &&
1352 getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1353 getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1355 number(bv()); // Set current_font.number to OFF
1356 } else if (IsDigit(c) &&
1357 real_current_font.isVisibleRightToLeft()) {
1358 number(bv()); // Set current_font.number to ON
1360 if (cursor.pos() > 0) {
1361 char const c = cursor.par()->getChar(cursor.pos() - 1);
1362 if (contains(number_unary_operators, c) &&
1363 (cursor.pos() == 1 ||
1364 cursor.par()->isSeparator(cursor.pos() - 2) ||
1365 cursor.par()->isNewline(cursor.pos() - 2))
1371 } else if (contains(number_seperators, c) &&
1372 cursor.pos() >= 2 &&
1375 cursor.pos() - 2).number() == LyXFont::ON) {
1386 // First check, if there will be two blanks together or a blank at
1387 // the beginning of a paragraph.
1388 // I decided to handle blanks like normal characters, the main
1389 // difference are the special checks when calculating the row.fill
1390 // (blank does not count at the end of a row) and the check here
1392 // The bug is triggered when we type in a description environment:
1393 // The current_font is not changed when we go from label to main text
1394 // and it should (along with realtmpfont) when we type the space.
1395 // CHECK There is a bug here! (Asger)
1397 // store the current font. This is because of the use of cursor
1398 // movements. The moving cursor would refresh the current font
1399 LyXFont realtmpfont = real_current_font;
1400 LyXFont rawtmpfont = current_font;
1402 if (!freeSpacing && IsLineSeparatorChar(c)) {
1403 if ((cursor.pos() > 0
1404 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1405 || (cursor.pos() > 0
1406 && cursor.par()->isNewline(cursor.pos() - 1))
1407 || (cursor.pos() == 0)) {
1408 static bool sent_space_message = false;
1409 if (!sent_space_message) {
1410 if (cursor.pos() == 0)
1411 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1413 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1414 sent_space_message = true;
1421 // Here case LyXText::InsertInset already inserted the character
1422 if (c != Paragraph::META_INSET)
1423 cursor.par()->insertChar(cursor.pos(), c);
1425 setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1427 current_font = rawtmpfont;
1428 real_current_font = realtmpfont;
1429 redoParagraph(cursor.par());
1430 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1436 void LyXText::charInserted()
1438 // Here we could call finishUndo for every 20 characters inserted.
1439 // This is from my experience how emacs does it. (Lgb)
1440 static unsigned int counter;
1450 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1451 RowList::iterator const rit) const
1453 double w = rit->fill();
1454 double fill_hfill = 0;
1455 double fill_label_hfill = 0;
1456 double fill_separator = 0;
1460 pit->isRightToLeftPar(bv()->buffer()->params);
1462 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1464 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1466 // is there a manual margin with a manual label
1467 LyXLayout_ptr const & layout = pit->layout();
1469 if (layout->margintype == MARGIN_MANUAL
1470 && layout->labeltype == LABEL_MANUAL) {
1471 /// We might have real hfills in the label part
1472 int nlh = numberOfLabelHfills(*pit, rit);
1474 // A manual label par (e.g. List) has an auto-hfill
1475 // between the label text and the body of the
1477 // But we don't want to do this auto hfill if the par
1482 if (nlh && !pit->getLabelWidthString().empty()) {
1483 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1487 // are there any hfills in the row?
1488 int const nh = numberOfHfills(*pit, rit);
1492 fill_hfill = w / nh;
1493 // we don't have to look at the alignment if it is ALIGN_LEFT and
1494 // if the row is already larger then the permitted width as then
1495 // we force the LEFT_ALIGN'edness!
1496 } else if (int(rit->width()) < workWidth()) {
1497 // is it block, flushleft or flushright?
1498 // set x how you need it
1500 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1501 align = layout->align;
1503 align = pit->params().align();
1506 // center displayed insets
1507 InsetOld * inset = 0;
1508 if (rit->pos() < pit->size()
1509 && pit->isInset(rit->pos())
1510 && (inset = pit->getInset(rit->pos()))
1511 && (inset->display())) // || (inset->scroll() < 0)))
1512 align = (inset->lyxCode() == InsetOld::MATHMACRO_CODE)
1513 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1514 // ERT insets should always be LEFT ALIGNED on screen
1515 inset = pit->inInset();
1516 if (inset && inset->owner() &&
1517 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1519 align = LYX_ALIGN_LEFT;
1523 case LYX_ALIGN_BLOCK:
1525 int const ns = numberOfSeparators(*pit, rit);
1526 RowList::iterator next_row = boost::next(rit);
1528 && next_row != pit->rows.end()
1529 && !pit->isNewline(next_row->pos() - 1)
1530 && !(pit->isInset(next_row->pos())
1531 && pit->getInset(next_row->pos())
1532 && pit->getInset(next_row->pos())->display())
1534 fill_separator = w / ns;
1535 } else if (is_rtl) {
1540 case LYX_ALIGN_RIGHT:
1543 case LYX_ALIGN_CENTER:
1549 computeBidiTables(pit, bv()->buffer(), rit);
1551 pos_type body_pos = pit->beginningOfBody();
1552 pos_type last = lastPos(*pit, rit);
1555 (body_pos - 1 > last ||
1556 !pit->isLineSeparator(body_pos - 1))) {
1557 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1558 if (body_pos - 1 <= last)
1559 x += fill_label_hfill;
1563 rit->fill_hfill(fill_hfill);
1564 rit->fill_label_hfill(fill_label_hfill);
1565 rit->fill_separator(fill_separator);
1570 // important for the screen
1573 // the cursor set functions have a special mechanism. When they
1574 // realize, that you left an empty paragraph, they will delete it.
1575 // They also delete the corresponding row
1577 void LyXText::cursorRightOneWord()
1579 ::cursorRightOneWord(cursor, ownerParagraphs());
1580 setCursor(cursor.par(), cursor.pos());
1584 // Skip initial whitespace at end of word and move cursor to *start*
1585 // of prior word, not to end of next prior word.
1586 void LyXText::cursorLeftOneWord()
1588 LyXCursor tmpcursor = cursor;
1589 ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1590 setCursor(tmpcursor.par(), tmpcursor.pos());
1594 void LyXText::selectWord(word_location loc)
1596 LyXCursor from = cursor;
1598 ::getWord(from, to, loc, ownerParagraphs());
1600 setCursor(from.par(), from.pos());
1603 selection.cursor = cursor;
1604 setCursor(to.par(), to.pos());
1609 // Select the word currently under the cursor when no
1610 // selection is currently set
1611 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1613 if (!selection.set()) {
1615 return selection.set();
1621 void LyXText::acceptChange()
1623 if (!selection.set() && cursor.par()->size())
1626 if (selection.start.par() == selection.end.par()) {
1627 LyXCursor & startc = selection.start;
1628 LyXCursor & endc = selection.end;
1629 recordUndo(bv(), Undo::INSERT, startc.par());
1630 startc.par()->acceptChange(startc.pos(), endc.pos());
1633 redoParagraph(startc.par());
1634 setCursorIntern(startc.par(), 0);
1636 #warning handle multi par selection
1640 void LyXText::rejectChange()
1642 if (!selection.set() && cursor.par()->size())
1645 if (selection.start.par() == selection.end.par()) {
1646 LyXCursor & startc = selection.start;
1647 LyXCursor & endc = selection.end;
1648 recordUndo(bv(), Undo::INSERT, startc.par());
1649 startc.par()->rejectChange(startc.pos(), endc.pos());
1652 redoParagraph(startc.par());
1653 setCursorIntern(startc.par(), 0);
1655 #warning handle multi par selection
1659 // This function is only used by the spellchecker for NextWord().
1660 // It doesn't handle LYX_ACCENTs and probably never will.
1662 LyXText::selectNextWordToSpellcheck(float & value)
1664 if (the_locking_inset) {
1665 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1666 if (!word.word().empty()) {
1667 value += float(cursor.y());
1668 value /= float(height);
1671 // we have to go on checking so move cursor to the next char
1672 if (cursor.pos() == cursor.par()->size()) {
1673 if (boost::next(cursor.par()) == ownerParagraphs().end())
1675 cursor.par(boost::next(cursor.par()));
1678 cursor.pos(cursor.pos() + 1);
1680 ParagraphList::iterator tmppit = cursor.par();
1682 // If this is not the very first word, skip rest of
1683 // current word because we are probably in the middle
1684 // of a word if there is text here.
1685 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1686 while (cursor.pos() < cursor.par()->size()
1687 && cursor.par()->isLetter(cursor.pos()))
1688 cursor.pos(cursor.pos() + 1);
1691 // Now, skip until we have real text (will jump paragraphs)
1693 ParagraphList::iterator cpit = cursor.par();
1694 pos_type const cpos(cursor.pos());
1696 if (cpos == cpit->size()) {
1697 if (boost::next(cpit) != ownerParagraphs().end()) {
1698 cursor.par(boost::next(cpit));
1705 bool const is_good_inset = cpit->isInset(cpos)
1706 && cpit->getInset(cpos)->allowSpellcheck();
1708 if (!isDeletedText(*cpit, cpos)
1709 && (is_good_inset || cpit->isLetter(cpos)))
1712 cursor.pos(cpos + 1);
1715 // now check if we hit an inset so it has to be a inset containing text!
1716 if (cursor.pos() < cursor.par()->size() &&
1717 cursor.par()->isInset(cursor.pos())) {
1719 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1720 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1721 // now call us again to do the above trick
1722 // but obviously we have to start from down below ;)
1723 return bv()->text->selectNextWordToSpellcheck(value);
1726 // Update the value if we changed paragraphs
1727 if (cursor.par() != tmppit) {
1728 setCursor(cursor.par(), cursor.pos());
1729 value = float(cursor.y())/float(height);
1732 // Start the selection from here
1733 selection.cursor = cursor;
1735 string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1736 // and find the end of the word (insets like optional hyphens
1737 // and ligature break are part of a word)
1738 while (cursor.pos() < cursor.par()->size()
1739 && cursor.par()->isLetter(cursor.pos())
1740 && !isDeletedText(*cursor.par(), cursor.pos()))
1741 cursor.pos(cursor.pos() + 1);
1743 // Finally, we copy the word to a string and return it
1745 if (selection.cursor.pos() < cursor.pos()) {
1747 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1748 if (!cursor.par()->isInset(i))
1749 str += cursor.par()->getChar(i);
1752 return WordLangTuple(str, lang_code);
1756 // This one is also only for the spellchecker
1757 void LyXText::selectSelectedWord()
1759 if (the_locking_inset) {
1760 the_locking_inset->selectSelectedWord(bv());
1763 // move cursor to the beginning
1764 setCursor(selection.cursor.par(), selection.cursor.pos());
1766 // set the sel cursor
1767 selection.cursor = cursor;
1769 // now find the end of the word
1770 while (cursor.pos() < cursor.par()->size()
1771 && cursor.par()->isLetter(cursor.pos()))
1772 cursor.pos(cursor.pos() + 1);
1774 setCursor(cursor.par(), cursor.pos());
1776 // finally set the selection
1781 // Delete from cursor up to the end of the current or next word.
1782 void LyXText::deleteWordForward()
1784 if (cursor.par()->empty())
1787 LyXCursor tmpcursor = cursor;
1788 selection.set(true); // to avoid deletion
1789 cursorRightOneWord();
1790 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1791 selection.cursor = cursor;
1795 // Great, CutSelection() gets rid of multiple spaces.
1796 cutSelection(true, false);
1801 // Delete from cursor to start of current or prior word.
1802 void LyXText::deleteWordBackward()
1804 if (cursor.par()->empty())
1807 LyXCursor tmpcursor = cursor;
1808 selection.set(true); // to avoid deletion
1809 cursorLeftOneWord();
1810 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1811 selection.cursor = cursor;
1814 cutSelection(true, false);
1819 // Kill to end of line.
1820 void LyXText::deleteLineForward()
1822 if (cursor.par()->empty())
1823 // Paragraph is empty, so we just go to the right
1826 LyXCursor tmpcursor = cursor;
1827 // We can't store the row over a regular setCursor
1828 // so we set it to 0 and reset it afterwards.
1829 selection.set(true); // to avoid deletion
1831 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1832 selection.cursor = cursor;
1835 // What is this test for ??? (JMarc)
1836 if (!selection.set()) {
1837 deleteWordForward();
1839 cutSelection(true, false);
1845 void LyXText::changeCase(LyXText::TextCase action)
1850 if (selection.set()) {
1851 from = selection.start;
1855 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1856 setCursor(to.par(), to.pos() + 1);
1859 recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1861 pos_type pos = from.pos();
1862 ParagraphList::iterator pit = from.par();
1864 while (pit != ownerParagraphs().end() &&
1865 (pos != to.pos() || pit != to.par())) {
1866 if (pos == pit->size()) {
1871 unsigned char c = pit->getChar(pos);
1872 if (!IsInsetChar(c)) {
1874 case text_lowercase:
1877 case text_capitalization:
1879 action = text_lowercase;
1881 case text_uppercase:
1887 pit->setChar(pos, c);
1893 void LyXText::Delete()
1895 // this is a very easy implementation
1897 LyXCursor old_cursor = cursor;
1898 int const old_cur_par_id = old_cursor.par()->id();
1899 int const old_cur_par_prev_id =
1900 (old_cursor.par() != ownerParagraphs().begin() ?
1901 boost::prior(old_cursor.par())->id() : -1);
1903 // just move to the right
1906 // CHECK Look at the comment here.
1907 // This check is not very good...
1908 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1909 // and that can very well delete the par or par->previous in
1910 // old_cursor. Will a solution where we compare paragraph id's
1912 if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1913 == old_cur_par_prev_id
1914 && cursor.par()->id() != old_cur_par_id) {
1915 // delete-empty-paragraph-mechanism has done it
1919 // if you had success make a backspace
1920 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1921 LyXCursor tmpcursor = cursor;
1922 // to make sure undo gets the right cursor position
1923 cursor = old_cursor;
1924 recordUndo(bv(), Undo::DELETE, cursor.par());
1931 void LyXText::backspace()
1933 // Get the font that is used to calculate the baselineskip
1934 pos_type lastpos = cursor.par()->size();
1936 if (cursor.pos() == 0) {
1937 // The cursor is at the beginning of a paragraph,
1938 // so the the backspace will collapse two paragraphs into one.
1940 // but it's not allowed unless it's new
1941 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1944 // we may paste some paragraphs
1946 // is it an empty paragraph?
1949 || (lastpos == 1 && cursor.par()->isSeparator(0))) {
1950 // This is an empty paragraph and we delete it just
1951 // by moving the cursor one step
1952 // left and let the DeleteEmptyParagraphMechanism
1953 // handle the actual deletion of the paragraph.
1955 if (cursor.par() != ownerParagraphs().begin()) {
1956 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1957 if (cursor.par()->layout() == tmppit->layout()
1958 && cursor.par()->getAlign() == tmppit->getAlign()) {
1959 // Inherit bottom DTD from the paragraph below.
1960 // (the one we are deleting)
1961 tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1962 tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1963 tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1968 // the layout things can change the height of a row !
1974 if (cursor.par() != ownerParagraphs().begin()) {
1975 recordUndo(bv(), Undo::DELETE,
1976 boost::prior(cursor.par()),
1980 ParagraphList::iterator tmppit = cursor.par();
1981 // We used to do cursorLeftIntern() here, but it is
1982 // not a good idea since it triggers the auto-delete
1983 // mechanism. So we do a cursorLeftIntern()-lite,
1984 // without the dreaded mechanism. (JMarc)
1985 if (cursor.par() != ownerParagraphs().begin()) {
1986 // steps into the above paragraph.
1987 setCursorIntern(boost::prior(cursor.par()),
1988 boost::prior(cursor.par())->size(),
1992 // Pasting is not allowed, if the paragraphs have different
1993 // layout. I think it is a real bug of all other
1994 // word processors to allow it. It confuses the user.
1995 //Correction: Pasting is always allowed with standard-layout
1996 LyXTextClass const & tclass =
1997 bv()->buffer()->params.getLyXTextClass();
1999 if (cursor.par() != tmppit
2000 && (cursor.par()->layout() == tmppit->layout()
2001 || tmppit->layout() == tclass.defaultLayout())
2002 && cursor.par()->getAlign() == tmppit->getAlign()) {
2003 mergeParagraph(bv()->buffer()->params,
2004 bv()->buffer()->paragraphs, cursor.par());
2006 if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
2007 cursor.pos(cursor.pos() - 1);
2009 // the row may have changed, block, hfills etc.
2011 setCursor(cursor.par(), cursor.pos(), false);
2014 // this is the code for a normal backspace, not pasting
2016 recordUndo(bv(), Undo::DELETE, cursor.par());
2017 // We used to do cursorLeftIntern() here, but it is
2018 // not a good idea since it triggers the auto-delete
2019 // mechanism. So we do a cursorLeftIntern()-lite,
2020 // without the dreaded mechanism. (JMarc)
2021 setCursorIntern(cursor.par(), cursor.pos() - 1,
2022 false, cursor.boundary());
2023 cursor.par()->erase(cursor.pos());
2026 lastpos = cursor.par()->size();
2027 if (cursor.pos() == lastpos)
2031 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
2035 RowList::iterator LyXText::cursorRow() const
2037 return getRow(cursor.par(), cursor.pos());
2041 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2043 return getRow(cur.par(), cur.pos());
2048 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
2050 RowList::iterator rit = boost::prior(pit->rows.end());
2051 RowList::iterator const begin = pit->rows.begin();
2053 while (rit != begin && rit->pos() > pos)
2060 // returns pointer to some fancy row 'below' specified row
2061 RowList::iterator LyXText::cursorIRow() const
2063 return getRow(cursor.par(), cursor.pos());
2067 RowList::iterator LyXText::getRowNearY(int y,
2068 ParagraphList::iterator & pit) const
2070 //lyxerr << "getRowNearY: y " << y << endl;
2072 pit = boost::prior(ownerParagraphs().end());
2074 RowList::iterator rit = lastRow();
2075 RowList::iterator rbegin = firstRow();
2077 while (rit != rbegin && static_cast<int>(rit->y()) > y)
2078 previousRow(pit, rit);
2084 int LyXText::getDepth() const
2086 return cursor.par()->getDepth();
2090 RowList::iterator LyXText::firstRow() const
2092 return ownerParagraphs().front().rows.begin();
2096 RowList::iterator LyXText::lastRow() const
2098 return boost::prior(endRow());
2102 RowList::iterator LyXText::endRow() const
2104 return ownerParagraphs().back().rows.end();
2108 void LyXText::nextRow(ParagraphList::iterator & pit,
2109 RowList::iterator & rit) const
2112 if (rit == pit->rows.end()) {
2114 if (pit == ownerParagraphs().end())
2117 rit = pit->rows.begin();
2122 void LyXText::previousRow(ParagraphList::iterator & pit,
2123 RowList::iterator & rit) const
2125 if (rit != pit->rows.begin())
2128 Assert(pit != ownerParagraphs().begin());
2130 rit = boost::prior(pit->rows.end());
2135 bool LyXText::noRows() const
2137 return ownerParagraphs().begin()->rows.empty();