3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
22 #include "bufferparams.h"
23 #include "BufferView.h"
26 #include "funcrequest.h"
29 #include "lyxlength.h"
32 #include "lyxrow_funcs.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "rowpainter.h"
36 #include "text_funcs.h"
37 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
41 #include "frontends/font_metrics.h"
42 #include "frontends/LyXView.h"
44 #include "insets/insettext.h"
46 #include "support/LAssert.h"
47 #include "support/lstrings.h"
48 #include "support/textutils.h"
51 using namespace lyx::support;
57 using lyx::word_location;
59 using namespace bv_funcs;
61 /// top, right, bottom pixel margin
62 extern int const PAPER_MARGIN = 20;
63 /// margin for changebar
64 extern int const CHANGEBAR_MARGIN = 10;
66 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
70 int bibitemMaxWidth(BufferView *, LyXFont const &);
73 BufferView * LyXText::bv()
75 Assert(bv_owner != 0);
80 BufferView * LyXText::bv() const
82 Assert(bv_owner != 0);
87 void LyXText::updateRowPositions()
89 ParagraphList::iterator pit = ownerParagraphs().begin();
90 ParagraphList::iterator end = ownerParagraphs().end();
91 for (height = 0; pit != end; ++pit) {
92 RowList::iterator rit = pit->rows.begin();
93 RowList::iterator rend = pit->rows.end();
94 for ( ; rit != rend ; rit = ++rit) {
96 height += rit->height();
102 int LyXText::workWidth() const
104 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
108 int LyXText::getRealCursorX() const
111 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
112 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
117 #warning FIXME This function seems to belong outside of LyxText.
118 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
121 if (!Encodings::is_arabic(c))
122 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
123 return c + (0xb0 - '0');
127 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
128 unsigned char next_char = ' ';
130 pos_type const par_size = par.size();
132 for (pos_type i = pos + 1; i < par_size; ++i) {
133 unsigned char const par_char = par.getChar(i);
134 if (!Encodings::IsComposeChar_arabic(par_char)) {
135 next_char = par_char;
140 if (Encodings::is_arabic(next_char)) {
141 if (Encodings::is_arabic(prev_char) &&
142 !Encodings::is_arabic_special(prev_char))
143 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
145 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
147 if (Encodings::is_arabic(prev_char) &&
148 !Encodings::is_arabic_special(prev_char))
149 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
151 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
155 // This is the comments that some of the warnings below refers to.
156 // There are some issues in this file and I don't think they are
157 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
158 // this is a problem that has been here almost from day one and that a
159 // larger userbase with differenct access patters triggers the bad
160 // behaviour. (segfaults.) What I think happen is: In several places
161 // we store the paragraph in the current cursor and then moves the
162 // cursor. This movement of the cursor will delete paragraph at the
163 // old position if it is now empty. This will make the temporary
164 // pointer to the old cursor paragraph invalid and dangerous to use.
165 // And is some cases this will trigger a segfault. I have marked some
166 // of the cases where this happens with a warning, but I am sure there
167 // are others in this file and in text2.C. There is also a note in
168 // Delete() that you should read. In Delete I store the paragraph->id
169 // instead of a pointer to the paragraph. I am pretty sure this faulty
170 // use of temporary pointers to paragraphs that might have gotten
171 // invalidated (through a cursor movement) before they are used, are
172 // the cause of the strange crashes we get reported often.
174 // It is very tiresom to change this code, especially when it is as
175 // hard to read as it is. Help to fix all the cases where this is done
176 // would be greately appreciated.
180 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
182 if (pos >= pit->size())
185 char const c = pit->getChar(pos);
186 LyXFont const & font = getFont(pit, pos);
187 return singleWidth(pit, pos, c, font);
191 int LyXText::singleWidth(ParagraphList::iterator pit,
192 pos_type pos, char c, LyXFont const & font) const
194 if (pos >= pit->size()) {
195 lyxerr << "in singleWidth(), pos: " << pos << endl;
201 // The most common case is handled first (Asger)
202 if (IsPrintable(c)) {
203 if (font.language()->RightToLeft()) {
204 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
205 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
206 && font.language()->lang() == "arabic") {
207 if (Encodings::IsComposeChar_arabic(c))
210 c = transformChar(c, *pit, pos);
211 } else if (font.language()->lang() == "hebrew" &&
212 Encodings::IsComposeChar_hebrew(c))
215 return font_metrics::width(c, font);
218 if (c == Paragraph::META_INSET) {
219 InsetOld * tmpinset = pit->getInset(pos);
221 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
222 // Because of the representation as vertical lines
225 return tmpinset->width();
228 if (IsSeparatorChar(c))
230 return font_metrics::width(c, font);
234 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
236 if (bidi_start == -1)
239 return log2vis_list[pos - bidi_start];
243 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
245 if (bidi_start == -1)
248 return vis2log_list[pos - bidi_start];
252 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
254 if (bidi_start == -1)
257 return bidi_levels[pos - bidi_start];
261 bool LyXText::bidi_InRange(lyx::pos_type pos) const
263 return bidi_start == -1 ||
264 (bidi_start <= pos && pos <= bidi_end);
268 void LyXText::computeBidiTables(ParagraphList::iterator pit,
269 Buffer const & buf, RowList::iterator row) const
271 bidi_same_direction = true;
272 if (!lyxrc.rtl_support) {
277 InsetOld * inset = pit->inInset();
278 if (inset && inset->owner() &&
279 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
284 bidi_start = row->pos();
285 bidi_end = lastPos(*pit, row);
287 if (bidi_start > bidi_end) {
292 if (bidi_end + 2 - bidi_start >
293 static_cast<pos_type>(log2vis_list.size())) {
295 (bidi_end + 2 - bidi_start < 500) ?
296 500 : 2 * (bidi_end + 2 - bidi_start);
297 log2vis_list.resize(new_size);
298 vis2log_list.resize(new_size);
299 bidi_levels.resize(new_size);
302 vis2log_list[bidi_end + 1 - bidi_start] = -1;
303 log2vis_list[bidi_end + 1 - bidi_start] = -1;
305 BufferParams const & bufparams = buf.params();
308 pit->isRightToLeftPar(bufparams);
312 pos_type const body_pos = pit->beginningOfBody();
314 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
315 bool is_space = pit->isLineSeparator(lpos);
317 (is_space && lpos + 1 <= bidi_end &&
318 !pit->isLineSeparator(lpos + 1) &&
319 !pit->isNewline(lpos + 1))
321 LyXFont font = pit->getFontSettings(bufparams, pos);
322 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
323 font.number() == LyXFont::ON &&
324 pit->getFontSettings(bufparams, lpos - 1).number()
326 font = pit->getFontSettings(bufparams, lpos);
331 bool new_rtl = font.isVisibleRightToLeft();
332 bool new_rtl0 = font.isRightToLeft();
335 if (lpos == body_pos - 1
336 && row->pos() < body_pos - 1
338 new_level = (rtl_par) ? 1 : 0;
339 new_rtl = new_rtl0 = rtl_par;
341 new_level = (new_rtl) ? 1 : 2;
343 new_level = (rtl_par) ? 2 : 0;
345 if (is_space && new_level >= level) {
351 int new_level2 = new_level;
353 if (level == new_level && rtl0 != new_rtl0) {
355 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
356 } else if (level < new_level) {
357 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
358 if (new_level > rtl_par)
359 bidi_same_direction = false;
361 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
364 bidi_levels[lpos - bidi_start] = new_level;
366 while (level > new_level2) {
367 pos_type old_lpos = stack[--level];
368 int delta = lpos - old_lpos - 1;
371 log2vis_list[lpos - bidi_start] += delta;
372 log2vis_list[old_lpos - bidi_start] += delta;
374 while (level < new_level)
375 stack[level++] = lpos;
379 pos_type const old_lpos = stack[--level];
380 int delta = bidi_end - old_lpos;
383 log2vis_list[old_lpos - bidi_start] += delta;
386 pos_type vpos = bidi_start - 1;
387 for (pos_type lpos = bidi_start;
388 lpos <= bidi_end; ++lpos) {
389 vpos += log2vis_list[lpos - bidi_start];
390 vis2log_list[vpos - bidi_start] = lpos;
391 log2vis_list[lpos - bidi_start] = vpos;
396 // This method requires a previous call to ComputeBidiTables()
397 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
400 if (!lyxrc.rtl_support || pos == 0)
403 if (!bidi_InRange(pos - 1)) {
404 /// This can happen if pos is the first char of a row.
405 /// Returning false in this case is incorrect!
409 bool const rtl = bidi_level(pos - 1) % 2;
410 bool const rtl2 = bidi_InRange(pos)
411 ? bidi_level(pos) % 2
412 : par.isRightToLeftPar(buf.params());
417 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
418 pos_type pos, LyXFont const & font) const
420 if (!lyxrc.rtl_support)
421 return false; // This is just for speedup
423 bool const rtl = font.isVisibleRightToLeft();
424 bool const rtl2 = bidi_InRange(pos)
425 ? bidi_level(pos) % 2
426 : par.isRightToLeftPar(buf.params());
431 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
433 LyXTextClass const & tclass =
434 bv()->buffer()->params().getLyXTextClass();
435 LyXLayout_ptr const & layout = pit->layout();
437 string parindent = layout->parindent;
441 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
443 // this is the way, LyX handles the LaTeX-Environments.
444 // I have had this idea very late, so it seems to be a
445 // later added hack and this is true
446 if (!pit->getDepth()) {
447 if (pit->layout() == tclass.defaultLayout()) {
448 // find the previous same level paragraph
449 if (pit != ownerParagraphs().begin()) {
450 ParagraphList::iterator newpit =
451 depthHook(pit, ownerParagraphs(),
454 newpit->layout()->nextnoindent)
459 // find the next level paragraph
461 ParagraphList::iterator newpar = outerHook(pit,
464 // make a corresponding row. Needed to call leftMargin()
466 // check wether it is a sufficent paragraph
467 if (newpar != ownerParagraphs().end() &&
468 newpar->layout()->isEnvironment()) {
469 x = leftMargin(newpar, Row(newpar->size()));
472 if (newpar != ownerParagraphs().end() &&
473 pit->layout() == tclass.defaultLayout()) {
474 if (newpar->params().noindent())
477 parindent = newpar->layout()->parindent;
483 LyXFont const labelfont = getLabelFont(pit);
484 switch (layout->margintype) {
486 if (!layout->leftmargin.empty()) {
487 x += font_metrics::signedWidth(layout->leftmargin,
488 tclass.defaultfont());
490 if (!pit->getLabelstring().empty()) {
491 x += font_metrics::signedWidth(layout->labelindent,
493 x += font_metrics::width(pit->getLabelstring(),
495 x += font_metrics::width(layout->labelsep, labelfont);
499 x += font_metrics::signedWidth(layout->labelindent, labelfont);
500 // The width of an empty par, even with manual label, should be 0
501 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
502 if (!pit->getLabelWidthString().empty()) {
503 x += font_metrics::width(pit->getLabelWidthString(),
505 x += font_metrics::width(layout->labelsep, labelfont);
510 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
511 / (pit->getDepth() + 4);
513 case MARGIN_FIRST_DYNAMIC:
514 if (layout->labeltype == LABEL_MANUAL) {
515 if (row.pos() >= pit->beginningOfBody()) {
516 x += font_metrics::signedWidth(layout->leftmargin,
519 x += font_metrics::signedWidth(layout->labelindent,
523 // Special case to fix problems with
525 || (layout->labeltype == LABEL_STATIC
526 && layout->latextype == LATEX_ENVIRONMENT
527 && !isFirstInSequence(pit, ownerParagraphs()))) {
528 x += font_metrics::signedWidth(layout->leftmargin,
530 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
531 && layout->labeltype != LABEL_BIBLIO
532 && layout->labeltype !=
533 LABEL_CENTERED_TOP_ENVIRONMENT) {
534 x += font_metrics::signedWidth(layout->labelindent,
536 x += font_metrics::width(layout->labelsep, labelfont);
537 x += font_metrics::width(pit->getLabelstring(),
542 case MARGIN_RIGHT_ADDRESS_BOX:
544 // ok, a terrible hack. The left margin depends on the widest
545 // row in this paragraph. Do not care about footnotes, they
546 // are *NOT* allowed in the LaTeX realisation of this layout.
548 // find the first row of this paragraph
549 RowList::iterator rit = pit->rows.begin();
550 RowList::iterator end = pit->rows.end();
551 int minfill = rit->fill();
552 for ( ; rit != end; ++rit)
553 if (rit->fill() < minfill)
554 minfill = rit->fill();
556 x += font_metrics::signedWidth(layout->leftmargin,
557 tclass.defaultfont());
563 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
564 LyXLength const len = pit->params().leftIndent();
565 int const tw = inset_owner ?
566 inset_owner->latexTextWidth(bv()) : workWidth();
567 x += len.inPixels(tw);
572 if (pit->params().align() == LYX_ALIGN_LAYOUT)
573 align = layout->align;
575 align = pit->params().align();
577 // set the correct parindent
578 if (row.pos() == 0) {
579 if ((layout->labeltype == LABEL_NO_LABEL
580 || layout->labeltype == LABEL_TOP_ENVIRONMENT
581 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
582 || (layout->labeltype == LABEL_STATIC
583 && layout->latextype == LATEX_ENVIRONMENT
584 && !isFirstInSequence(pit, ownerParagraphs())))
585 && align == LYX_ALIGN_BLOCK
586 && !pit->params().noindent()
587 // in tabulars and ert paragraphs are never indented!
588 && (!pit->inInset() || !pit->inInset()->owner() ||
589 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
590 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
591 && (pit->layout() != tclass.defaultLayout() ||
592 bv()->buffer()->params().paragraph_separation ==
593 BufferParams::PARSEP_INDENT)) {
594 x += font_metrics::signedWidth(parindent,
595 tclass.defaultfont());
596 } else if (layout->labeltype == LABEL_BIBLIO) {
597 // ale970405 Right width for bibitems
598 x += bibitemMaxWidth(bv(), tclass.defaultfont());
606 int LyXText::rightMargin(ParagraphList::iterator pit,
607 Buffer const & buf, Row const &) const
609 LyXTextClass const & tclass = buf.params().getLyXTextClass();
610 LyXLayout_ptr const & layout = pit->layout();
613 + font_metrics::signedWidth(tclass.rightmargin(),
614 tclass.defaultfont());
615 + font_metrics::signedWidth(layout->rightmargin,
616 tclass.defaultfont())
617 * 4 / (pit->getDepth() + 4);
621 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
623 if (pit->layout()->margintype == MARGIN_MANUAL) {
625 tmprow.pos(pit->size());
626 // return the beginning of the body
627 return leftMargin(pit, tmprow);
630 // LabelEnd is only needed if the layout
631 // fills a flushleft label.
638 // this needs special handling - only newlines count as a break point
639 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
641 for (; i < par.size(); ++i) {
642 if (par.isNewline(i))
652 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
653 Row const & row) const
655 // maximum pixel width of a row.
656 int width = workWidth()
657 - rightMargin(pit, *bv()->buffer(), row);
659 // inset->textWidth() returns -1 via workWidth(),
664 LyXLayout_ptr const & layout = pit->layout();
666 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
667 return addressBreakPoint(row.pos(), *pit);
669 pos_type const pos = row.pos();
670 pos_type const body_pos = pit->beginningOfBody();
671 pos_type const last = pit->size();
672 pos_type point = last;
677 // Now we iterate through until we reach the right margin
678 // or the end of the par, then choose the possible break
681 int const left = leftMargin(pit, row);
684 // pixel width since last breakpoint
689 // We re-use the font resolution for the entire font span when possible
690 LyXFont font = getFont(pit, i);
691 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
693 for (; i < last; ++i) {
694 if (pit->isNewline(i)) {
699 char const c = pit->getChar(i);
700 if (i > endPosOfFontSpan) {
701 font = getFont(pit, i);
702 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
707 // add the auto-hfill from label end to the body
708 if (body_pos && i == body_pos) {
709 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
710 if (pit->isLineSeparator(i - 1))
711 thiswidth -= singleWidth(pit, i - 1);
712 int left_margin = labelEnd(pit, row);
713 if (thiswidth + x < left_margin)
714 thiswidth = left_margin - x;
715 thiswidth += singleWidth(pit, i, c, font);
717 thiswidth = singleWidth(pit, i, c, font);
721 chunkwidth += thiswidth;
723 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
725 // break before a character that will fall off
726 // the right of the row
728 // if no break before, break here
729 if (point == last || chunkwidth >= (width - left)) {
738 if (!in || in->isChar()) {
739 // some insets are line separators too
740 if (pit->isLineSeparator(i)) {
750 if (point == last && x >= width) {
751 // didn't find one, break at the point we reached the edge
753 } else if (i == last && x < width) {
754 // found one, but we fell off the end of the par, so prefer
759 // manual labels cannot be broken in LaTeX. But we
760 // want to make our on-screen rendering of footnotes
762 if (body_pos && point < body_pos)
763 point = body_pos - 1;
769 // returns the minimum space a row needs on the screen in pixel
770 int LyXText::fill(ParagraphList::iterator pit,
771 RowList::iterator row, int paper_width) const
777 // get the pure distance
778 pos_type const last = lastPos(*pit, row);
780 LyXLayout_ptr const & layout = pit->layout();
782 // special handling of the right address boxes
783 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
784 int const tmpfill = row->fill();
785 row->fill(0); // the minfill in MarginLeft()
786 w = leftMargin(pit, *row);
789 w = leftMargin(pit, *row);
791 pos_type const body_pos = pit->beginningOfBody();
792 pos_type i = row->pos();
794 if (! pit->empty() && i <= last) {
795 // We re-use the font resolution for the entire span when possible
796 LyXFont font = getFont(pit, i);
797 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
799 if (body_pos > 0 && i == body_pos) {
800 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
801 if (pit->isLineSeparator(i - 1))
802 w -= singleWidth(pit, i - 1);
803 int left_margin = labelEnd(pit, *row);
807 char const c = pit->getChar(i);
808 if (IsPrintable(c) && i > endPosOfFontSpan) {
809 // We need to get the next font
810 font = getFont(pit, i);
811 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
813 w += singleWidth(pit, i, c, font);
817 if (body_pos > 0 && body_pos > last) {
818 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
819 if (last >= 0 && pit->isLineSeparator(last))
820 w -= singleWidth(pit, last);
821 int const left_margin = labelEnd(pit, *row);
826 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
828 // If this case happens, it means that our calculation
829 // of the widths of the chars when we do rowBreakPoint()
830 // went wrong for some reason. Typically in list bodies.
831 // Things just about hobble on anyway, though you'll end
832 // up with a "fill_separator" less than zero, which corresponds
833 // to inter-word spacing being too small. Hopefully this problem
834 // will die when the label hacks die.
835 if (lyxerr.debugging() && fill < 0) {
836 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
837 << " w " << w << " paper_width " << paper_width
838 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
844 // returns the minimum space a manual label needs on the screen in pixel
845 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
847 pos_type last = pit->beginningOfBody();
851 // -1 because a label ends either with a space that is in the label,
852 // or with the beginning of a footnote that is outside the label.
855 // a separator at this end does not count
856 if (pit->isLineSeparator(last))
860 pos_type i = row.pos();
862 w += singleWidth(pit, i);
867 string const & labwidstr = pit->params().labelWidthString();
868 if (!labwidstr.empty()) {
869 LyXFont const labfont = getLabelFont(pit);
870 int const labwidth = font_metrics::width(labwidstr, labfont);
871 fill = max(labwidth - w, 0);
878 LColor::color LyXText::backgroundColor() const
881 return inset_owner->backgroundColor();
883 return LColor::background;
887 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
889 // get the maximum ascent and the maximum descent
890 double layoutasc = 0;
891 double layoutdesc = 0;
894 // ok, let us initialize the maxasc and maxdesc value.
895 // Only the fontsize count. The other properties
896 // are taken from the layoutfont. Nicer on the screen :)
897 LyXLayout_ptr const & layout = pit->layout();
899 // as max get the first character of this row then it can increase but not
900 // decrease the height. Just some point to start with so we don't have to
901 // do the assignment below too often.
902 LyXFont font = getFont(pit, rit->pos());
903 LyXFont::FONT_SIZE const tmpsize = font.size();
904 font = getLayoutFont(pit);
905 LyXFont::FONT_SIZE const size = font.size();
906 font.setSize(tmpsize);
908 LyXFont labelfont = getLabelFont(pit);
910 double spacing_val = 1.0;
911 if (!pit->params().spacing().isDefault())
912 spacing_val = pit->params().spacing().getValue();
914 spacing_val = bv()->buffer()->params().spacing().getValue();
915 //lyxerr << "spacing_val = " << spacing_val << endl;
917 int maxasc = int(font_metrics::maxAscent(font) *
918 layout->spacing.getValue() * spacing_val);
919 int maxdesc = int(font_metrics::maxDescent(font) *
920 layout->spacing.getValue() * spacing_val);
922 pos_type const pos_end = lastPos(*pit, rit);
927 // We re-use the font resolution for the entire font span when possible
928 LyXFont font = getFont(pit, rit->pos());
929 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
932 Paragraph const & par = *pit;
934 // Check if any insets are larger
935 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
936 // Manual inlined optimised version of common case of
937 // "maxwidth += singleWidth(pit, pos);"
938 char const c = par.getChar(pos);
940 if (IsPrintable(c)) {
941 if (pos > endPosOfFontSpan) {
942 // We need to get the next font
943 font = getFont(pit, pos);
944 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
946 if (! font.language()->RightToLeft()) {
947 maxwidth += font_metrics::width(c, font);
949 // Fall-back to normal case
950 maxwidth += singleWidth(pit, pos, c, font);
951 // And flush font cache
952 endPosOfFontSpan = 0;
955 // Special handling of insets - are any larger?
956 if (par.isInset(pos)) {
957 InsetOld const * tmpinset = par.getInset(pos);
959 maxwidth += tmpinset->width();
960 maxasc = max(maxasc, tmpinset->ascent());
961 maxdesc = max(maxdesc, tmpinset->descent());
964 // Fall-back to normal case
965 maxwidth += singleWidth(pit, pos, c, font);
966 // And flush font cache
967 endPosOfFontSpan = 0;
973 // Check if any custom fonts are larger (Asger)
974 // This is not completely correct, but we can live with the small,
975 // cosmetic error for now.
976 LyXFont::FONT_SIZE maxsize =
977 pit->highestFontInRange(rit->pos(), pos_end, size);
978 if (maxsize > font.size()) {
979 font.setSize(maxsize);
980 maxasc = max(maxasc, font_metrics::maxAscent(font));
981 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
984 // This is nicer with box insets:
988 rit->ascent_of_text(maxasc);
992 BufferParams const & bufparams = bv()->buffer()->params();
993 // some parksips VERY EASY IMPLEMENTATION
994 if (bv()->buffer()->params().paragraph_separation ==
995 BufferParams::PARSEP_SKIP)
997 if (layout->isParagraph()
998 && pit->getDepth() == 0
999 && pit != ownerParagraphs().begin())
1001 maxasc += bufparams.getDefSkip().inPixels(*bv());
1002 } else if (pit != ownerParagraphs().begin() &&
1003 boost::prior(pit)->layout()->isParagraph() &&
1004 boost::prior(pit)->getDepth() == 0)
1006 // is it right to use defskip here too? (AS)
1007 maxasc += bufparams.getDefSkip().inPixels(*bv());
1012 if (pit == ownerParagraphs().begin() && !isInInset())
1013 maxasc += PAPER_MARGIN;
1015 // add the vertical spaces, that the user added
1016 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1018 // do not forget the DTP-lines!
1019 // there height depends on the font of the nearest character
1020 if (pit->params().lineTop())
1022 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1023 // and now the pagebreaks
1024 if (pit->params().pagebreakTop())
1025 maxasc += 3 * defaultRowHeight();
1027 if (pit->params().startOfAppendix())
1028 maxasc += 3 * defaultRowHeight();
1030 // This is special code for the chapter, since the label of this
1031 // layout is printed in an extra row
1032 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1033 && bufparams.secnumdepth >= 0)
1035 float spacing_val = 1.0;
1036 if (!pit->params().spacing().isDefault()) {
1037 spacing_val = pit->params().spacing().getValue();
1039 spacing_val = bufparams.spacing().getValue();
1042 labeladdon = int(font_metrics::maxDescent(labelfont) *
1043 layout->spacing.getValue() *
1045 + int(font_metrics::maxAscent(labelfont) *
1046 layout->spacing.getValue() *
1050 // special code for the top label
1051 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1052 || layout->labeltype == LABEL_BIBLIO
1053 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1054 && isFirstInSequence(pit, ownerParagraphs())
1055 && !pit->getLabelstring().empty())
1057 float spacing_val = 1.0;
1058 if (!pit->params().spacing().isDefault()) {
1059 spacing_val = pit->params().spacing().getValue();
1061 spacing_val = bufparams.spacing().getValue();
1065 (font_metrics::maxAscent(labelfont) +
1066 font_metrics::maxDescent(labelfont)) *
1067 layout->spacing.getValue() *
1069 + layout->topsep * defaultRowHeight()
1070 + layout->labelbottomsep * defaultRowHeight());
1073 // And now the layout spaces, for example before and after
1074 // a section, or between the items of a itemize or enumerate
1077 if (!pit->params().pagebreakTop()) {
1078 ParagraphList::iterator prev =
1079 depthHook(pit, ownerParagraphs(),
1081 if (prev != pit && prev->layout() == layout &&
1082 prev->getDepth() == pit->getDepth() &&
1083 prev->getLabelWidthString() == pit->getLabelWidthString())
1085 layoutasc = (layout->itemsep * defaultRowHeight());
1086 } else if (rit != firstRow()) {
1087 tmptop = layout->topsep;
1089 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1090 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1093 layoutasc = (tmptop * defaultRowHeight());
1094 } else if (pit->params().lineTop()) {
1095 tmptop = layout->topsep;
1098 layoutasc = (tmptop * defaultRowHeight());
1101 prev = outerHook(pit, ownerParagraphs());
1102 if (prev != ownerParagraphs().end()) {
1103 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1104 } else if (pit != ownerParagraphs().begin()) {
1105 ParagraphList::iterator prior_pit = boost::prior(pit);
1106 if (prior_pit->getDepth() != 0 ||
1107 prior_pit->layout() == layout) {
1108 maxasc += int(layout->parsep * defaultRowHeight());
1114 // is it a bottom line?
1115 if (boost::next(rit) == pit->rows.end()) {
1116 // the bottom margin
1117 ParagraphList::iterator nextpit = boost::next(pit);
1118 if (nextpit == ownerParagraphs().end() && !isInInset())
1119 maxdesc += PAPER_MARGIN;
1121 // add the vertical spaces, that the user added
1122 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1124 // do not forget the DTP-lines!
1125 // there height depends on the font of the nearest character
1126 if (pit->params().lineBottom())
1127 maxdesc += 2 * font_metrics::ascent('x',
1128 getFont(pit, max(pos_type(0), pit->size() - 1)));
1130 // and now the pagebreaks
1131 if (pit->params().pagebreakBottom())
1132 maxdesc += 3 * defaultRowHeight();
1134 // and now the layout spaces, for example before and after
1135 // a section, or between the items of a itemize or enumerate
1137 if (!pit->params().pagebreakBottom()
1138 && nextpit != ownerParagraphs().end()) {
1139 ParagraphList::iterator comparepit = pit;
1143 if (comparepit->getDepth() > nextpit->getDepth()) {
1144 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1145 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1146 if (comparepit->layout()!= nextpit->layout()
1147 || nextpit->getLabelWidthString() !=
1148 comparepit->getLabelWidthString())
1150 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1152 if (unusual > usual)
1153 layoutdesc = unusual;
1156 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1158 if (comparepit->layout() != nextpit->layout()
1159 || nextpit->getLabelWidthString() !=
1160 comparepit->getLabelWidthString())
1161 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1166 // incalculate the layout spaces
1167 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1168 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1170 rit->height(maxasc + maxdesc + labeladdon);
1171 rit->baseline(maxasc + labeladdon);
1172 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1175 rit->width(int(maxwidth + x));
1177 width = max(0, workWidth());
1178 RowList::iterator rit = firstRow();
1179 RowList::iterator end = endRow();
1180 ParagraphList::iterator it = ownerParagraphs().begin();
1181 while (rit != end) {
1182 if (rit->width() > width)
1183 width = rit->width();
1190 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1192 // allow only if at start or end, or all previous is new text
1193 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1194 && cursor.par()->isChangeEdited(0, cursor.pos()))
1197 LyXTextClass const & tclass =
1198 bv()->buffer()->params().getLyXTextClass();
1199 LyXLayout_ptr const & layout = cursor.par()->layout();
1201 // this is only allowed, if the current paragraph is not empty or caption
1202 // and if it has not the keepempty flag active
1203 if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1204 && layout->labeltype != LABEL_SENSITIVE)
1207 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1209 // Always break behind a space
1211 // It is better to erase the space (Dekel)
1212 if (cursor.pos() < cursor.par()->size()
1213 && cursor.par()->isLineSeparator(cursor.pos()))
1214 cursor.par()->erase(cursor.pos());
1216 // break the paragraph
1220 keep_layout = layout->isEnvironment();
1222 // we need to set this before we insert the paragraph. IMO the
1223 // breakParagraph call should return a bool if it inserts the
1224 // paragraph before or behind and we should react on that one
1225 // but we can fix this in 1.3.0 (Jug 20020509)
1226 bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1227 ::breakParagraph(bv()->buffer()->params(), paragraphs, cursor.par(),
1228 cursor.pos(), keep_layout);
1230 #warning Trouble Point! (Lgb)
1231 // When ::breakParagraph is called from within an inset we must
1232 // ensure that the correct ParagraphList is used. Today that is not
1233 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1234 ParagraphList::iterator next_par = boost::next(cursor.par());
1236 // well this is the caption hack since one caption is really enough
1237 if (layout->labeltype == LABEL_SENSITIVE) {
1239 // set to standard-layout
1240 cursor.par()->applyLayout(tclass.defaultLayout());
1242 // set to standard-layout
1243 next_par->applyLayout(tclass.defaultLayout());
1246 // if the cursor is at the beginning of a row without prior newline,
1248 // This touches only the screen-update. Otherwise we would may have
1249 // an empty row on the screen
1250 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1251 && !cursor.par()->isNewline(cursor.pos() - 1))
1256 while (!next_par->empty() && next_par->isNewline(0))
1260 redoParagraph(cursor.par());
1261 redoParagraph(next_par);
1263 // This check is necessary. Otherwise the new empty paragraph will
1264 // be deleted automatically. And it is more friendly for the user!
1265 if (cursor.pos() || isempty)
1266 setCursor(next_par, 0);
1268 setCursor(cursor.par(), 0);
1272 // convenience function
1273 void LyXText::redoParagraph()
1276 redoParagraph(cursor.par());
1277 setCursorIntern(cursor.par(), cursor.pos());
1281 // insert a character, moves all the following breaks in the
1282 // same Paragraph one to the right and make a rebreak
1283 void LyXText::insertChar(char c)
1285 recordUndo(bv(), Undo::INSERT, cursor.par());
1287 // When the free-spacing option is set for the current layout,
1288 // disable the double-space checking
1290 bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1291 cursor.par()->isFreeSpacing();
1293 if (lyxrc.auto_number) {
1294 static string const number_operators = "+-/*";
1295 static string const number_unary_operators = "+-";
1296 static string const number_seperators = ".,:";
1298 if (current_font.number() == LyXFont::ON) {
1299 if (!IsDigit(c) && !contains(number_operators, c) &&
1300 !(contains(number_seperators, c) &&
1301 cursor.pos() >= 1 &&
1302 cursor.pos() < cursor.par()->size() &&
1303 getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1304 getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1306 number(bv()); // Set current_font.number to OFF
1307 } else if (IsDigit(c) &&
1308 real_current_font.isVisibleRightToLeft()) {
1309 number(bv()); // Set current_font.number to ON
1311 if (cursor.pos() > 0) {
1312 char const c = cursor.par()->getChar(cursor.pos() - 1);
1313 if (contains(number_unary_operators, c) &&
1314 (cursor.pos() == 1 ||
1315 cursor.par()->isSeparator(cursor.pos() - 2) ||
1316 cursor.par()->isNewline(cursor.pos() - 2))
1322 } else if (contains(number_seperators, c) &&
1323 cursor.pos() >= 2 &&
1326 cursor.pos() - 2).number() == LyXFont::ON) {
1337 // First check, if there will be two blanks together or a blank at
1338 // the beginning of a paragraph.
1339 // I decided to handle blanks like normal characters, the main
1340 // difference are the special checks when calculating the row.fill
1341 // (blank does not count at the end of a row) and the check here
1343 // The bug is triggered when we type in a description environment:
1344 // The current_font is not changed when we go from label to main text
1345 // and it should (along with realtmpfont) when we type the space.
1346 // CHECK There is a bug here! (Asger)
1348 // store the current font. This is because of the use of cursor
1349 // movements. The moving cursor would refresh the current font
1350 LyXFont realtmpfont = real_current_font;
1351 LyXFont rawtmpfont = current_font;
1353 if (!freeSpacing && IsLineSeparatorChar(c)) {
1354 if ((cursor.pos() > 0
1355 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1356 || (cursor.pos() > 0
1357 && cursor.par()->isNewline(cursor.pos() - 1))
1358 || (cursor.pos() == 0)) {
1359 static bool sent_space_message = false;
1360 if (!sent_space_message) {
1361 if (cursor.pos() == 0)
1362 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1364 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1365 sent_space_message = true;
1372 // Here case LyXText::InsertInset already inserted the character
1373 if (c != Paragraph::META_INSET)
1374 cursor.par()->insertChar(cursor.pos(), c);
1376 setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1378 current_font = rawtmpfont;
1379 real_current_font = realtmpfont;
1380 redoParagraph(cursor.par());
1381 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1387 void LyXText::charInserted()
1389 // Here we could call finishUndo for every 20 characters inserted.
1390 // This is from my experience how emacs does it. (Lgb)
1391 static unsigned int counter;
1401 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1402 RowList::iterator const rit) const
1404 double w = rit->fill();
1405 double fill_hfill = 0;
1406 double fill_label_hfill = 0;
1407 double fill_separator = 0;
1411 pit->isRightToLeftPar(bv()->buffer()->params());
1413 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1415 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1417 // is there a manual margin with a manual label
1418 LyXLayout_ptr const & layout = pit->layout();
1420 if (layout->margintype == MARGIN_MANUAL
1421 && layout->labeltype == LABEL_MANUAL) {
1422 /// We might have real hfills in the label part
1423 int nlh = numberOfLabelHfills(*pit, rit);
1425 // A manual label par (e.g. List) has an auto-hfill
1426 // between the label text and the body of the
1428 // But we don't want to do this auto hfill if the par
1433 if (nlh && !pit->getLabelWidthString().empty()) {
1434 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1438 // are there any hfills in the row?
1439 int const nh = numberOfHfills(*pit, rit);
1443 fill_hfill = w / nh;
1444 // we don't have to look at the alignment if it is ALIGN_LEFT and
1445 // if the row is already larger then the permitted width as then
1446 // we force the LEFT_ALIGN'edness!
1447 } else if (int(rit->width()) < workWidth()) {
1448 // is it block, flushleft or flushright?
1449 // set x how you need it
1451 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1452 align = layout->align;
1454 align = pit->params().align();
1456 InsetOld * inset = 0;
1457 // ERT insets should always be LEFT ALIGNED on screen
1458 inset = pit->inInset();
1459 if (inset && inset->owner() &&
1460 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1462 align = LYX_ALIGN_LEFT;
1466 case LYX_ALIGN_BLOCK:
1468 int const ns = numberOfSeparators(*pit, rit);
1469 RowList::iterator next_row = boost::next(rit);
1471 && next_row != pit->rows.end()
1472 && !pit->isNewline(next_row->pos() - 1)
1474 fill_separator = w / ns;
1475 } else if (is_rtl) {
1480 case LYX_ALIGN_RIGHT:
1483 case LYX_ALIGN_CENTER:
1489 computeBidiTables(pit, *bv()->buffer(), rit);
1491 pos_type body_pos = pit->beginningOfBody();
1492 pos_type last = lastPos(*pit, rit);
1495 (body_pos - 1 > last ||
1496 !pit->isLineSeparator(body_pos - 1))) {
1497 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1498 if (body_pos - 1 <= last)
1499 x += fill_label_hfill;
1503 rit->fill_hfill(fill_hfill);
1504 rit->fill_label_hfill(fill_label_hfill);
1505 rit->fill_separator(fill_separator);
1510 // important for the screen
1513 // the cursor set functions have a special mechanism. When they
1514 // realize, that you left an empty paragraph, they will delete it.
1515 // They also delete the corresponding row
1517 void LyXText::cursorRightOneWord()
1519 ::cursorRightOneWord(cursor, ownerParagraphs());
1520 setCursor(cursor.par(), cursor.pos());
1524 // Skip initial whitespace at end of word and move cursor to *start*
1525 // of prior word, not to end of next prior word.
1526 void LyXText::cursorLeftOneWord()
1528 LyXCursor tmpcursor = cursor;
1529 ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1530 setCursor(tmpcursor.par(), tmpcursor.pos());
1534 void LyXText::selectWord(word_location loc)
1536 LyXCursor from = cursor;
1538 ::getWord(from, to, loc, ownerParagraphs());
1540 setCursor(from.par(), from.pos());
1543 selection.cursor = cursor;
1544 setCursor(to.par(), to.pos());
1549 // Select the word currently under the cursor when no
1550 // selection is currently set
1551 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1553 if (!selection.set()) {
1555 return selection.set();
1561 void LyXText::acceptChange()
1563 if (!selection.set() && cursor.par()->size())
1566 if (selection.start.par() == selection.end.par()) {
1567 LyXCursor & startc = selection.start;
1568 LyXCursor & endc = selection.end;
1569 recordUndo(bv(), Undo::INSERT, startc.par());
1570 startc.par()->acceptChange(startc.pos(), endc.pos());
1573 redoParagraph(startc.par());
1574 setCursorIntern(startc.par(), 0);
1576 #warning handle multi par selection
1580 void LyXText::rejectChange()
1582 if (!selection.set() && cursor.par()->size())
1585 if (selection.start.par() == selection.end.par()) {
1586 LyXCursor & startc = selection.start;
1587 LyXCursor & endc = selection.end;
1588 recordUndo(bv(), Undo::INSERT, startc.par());
1589 startc.par()->rejectChange(startc.pos(), endc.pos());
1592 redoParagraph(startc.par());
1593 setCursorIntern(startc.par(), 0);
1595 #warning handle multi par selection
1599 // This function is only used by the spellchecker for NextWord().
1600 // It doesn't handle LYX_ACCENTs and probably never will.
1602 LyXText::selectNextWordToSpellcheck(float & value)
1604 if (the_locking_inset) {
1605 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1606 if (!word.word().empty()) {
1607 value += float(cursor.y());
1608 value /= float(height);
1611 // we have to go on checking so move cursor to the next char
1612 if (cursor.pos() == cursor.par()->size()) {
1613 if (boost::next(cursor.par()) == ownerParagraphs().end())
1615 cursor.par(boost::next(cursor.par()));
1618 cursor.pos(cursor.pos() + 1);
1620 ParagraphList::iterator tmppit = cursor.par();
1622 // If this is not the very first word, skip rest of
1623 // current word because we are probably in the middle
1624 // of a word if there is text here.
1625 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1626 while (cursor.pos() < cursor.par()->size()
1627 && cursor.par()->isLetter(cursor.pos()))
1628 cursor.pos(cursor.pos() + 1);
1631 // Now, skip until we have real text (will jump paragraphs)
1633 ParagraphList::iterator cpit = cursor.par();
1634 pos_type const cpos(cursor.pos());
1636 if (cpos == cpit->size()) {
1637 if (boost::next(cpit) != ownerParagraphs().end()) {
1638 cursor.par(boost::next(cpit));
1645 bool const is_good_inset = cpit->isInset(cpos)
1646 && cpit->getInset(cpos)->allowSpellcheck();
1648 if (!isDeletedText(*cpit, cpos)
1649 && (is_good_inset || cpit->isLetter(cpos)))
1652 cursor.pos(cpos + 1);
1655 // now check if we hit an inset so it has to be a inset containing text!
1656 if (cursor.pos() < cursor.par()->size() &&
1657 cursor.par()->isInset(cursor.pos())) {
1659 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1660 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1661 // now call us again to do the above trick
1662 // but obviously we have to start from down below ;)
1663 return bv()->text->selectNextWordToSpellcheck(value);
1666 // Update the value if we changed paragraphs
1667 if (cursor.par() != tmppit) {
1668 setCursor(cursor.par(), cursor.pos());
1669 value = float(cursor.y())/float(height);
1672 // Start the selection from here
1673 selection.cursor = cursor;
1675 string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1676 // and find the end of the word (insets like optional hyphens
1677 // and ligature break are part of a word)
1678 while (cursor.pos() < cursor.par()->size()
1679 && cursor.par()->isLetter(cursor.pos())
1680 && !isDeletedText(*cursor.par(), cursor.pos()))
1681 cursor.pos(cursor.pos() + 1);
1683 // Finally, we copy the word to a string and return it
1685 if (selection.cursor.pos() < cursor.pos()) {
1687 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1688 if (!cursor.par()->isInset(i))
1689 str += cursor.par()->getChar(i);
1692 return WordLangTuple(str, lang_code);
1696 // This one is also only for the spellchecker
1697 void LyXText::selectSelectedWord()
1699 if (the_locking_inset) {
1700 the_locking_inset->selectSelectedWord(bv());
1703 // move cursor to the beginning
1704 setCursor(selection.cursor.par(), selection.cursor.pos());
1706 // set the sel cursor
1707 selection.cursor = cursor;
1709 // now find the end of the word
1710 while (cursor.pos() < cursor.par()->size()
1711 && cursor.par()->isLetter(cursor.pos()))
1712 cursor.pos(cursor.pos() + 1);
1714 setCursor(cursor.par(), cursor.pos());
1716 // finally set the selection
1721 // Delete from cursor up to the end of the current or next word.
1722 void LyXText::deleteWordForward()
1724 if (cursor.par()->empty())
1727 LyXCursor tmpcursor = cursor;
1728 selection.set(true); // to avoid deletion
1729 cursorRightOneWord();
1730 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1731 selection.cursor = cursor;
1735 // Great, CutSelection() gets rid of multiple spaces.
1736 cutSelection(true, false);
1741 // Delete from cursor to start of current or prior word.
1742 void LyXText::deleteWordBackward()
1744 if (cursor.par()->empty())
1747 LyXCursor tmpcursor = cursor;
1748 selection.set(true); // to avoid deletion
1749 cursorLeftOneWord();
1750 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1751 selection.cursor = cursor;
1754 cutSelection(true, false);
1759 // Kill to end of line.
1760 void LyXText::deleteLineForward()
1762 if (cursor.par()->empty())
1763 // Paragraph is empty, so we just go to the right
1766 LyXCursor tmpcursor = cursor;
1767 // We can't store the row over a regular setCursor
1768 // so we set it to 0 and reset it afterwards.
1769 selection.set(true); // to avoid deletion
1771 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1772 selection.cursor = cursor;
1775 // What is this test for ??? (JMarc)
1776 if (!selection.set()) {
1777 deleteWordForward();
1779 cutSelection(true, false);
1785 void LyXText::changeCase(LyXText::TextCase action)
1790 if (selection.set()) {
1791 from = selection.start;
1795 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1796 setCursor(to.par(), to.pos() + 1);
1799 recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1801 pos_type pos = from.pos();
1802 ParagraphList::iterator pit = from.par();
1804 while (pit != ownerParagraphs().end() &&
1805 (pos != to.pos() || pit != to.par())) {
1806 if (pos == pit->size()) {
1811 unsigned char c = pit->getChar(pos);
1812 if (!IsInsetChar(c)) {
1814 case text_lowercase:
1817 case text_capitalization:
1819 action = text_lowercase;
1821 case text_uppercase:
1827 pit->setChar(pos, c);
1833 void LyXText::Delete()
1835 // this is a very easy implementation
1837 LyXCursor old_cursor = cursor;
1838 int const old_cur_par_id = old_cursor.par()->id();
1839 int const old_cur_par_prev_id =
1840 (old_cursor.par() != ownerParagraphs().begin() ?
1841 boost::prior(old_cursor.par())->id() : -1);
1843 // just move to the right
1846 // CHECK Look at the comment here.
1847 // This check is not very good...
1848 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1849 // and that can very well delete the par or par->previous in
1850 // old_cursor. Will a solution where we compare paragraph id's
1852 if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1853 == old_cur_par_prev_id
1854 && cursor.par()->id() != old_cur_par_id) {
1855 // delete-empty-paragraph-mechanism has done it
1859 // if you had success make a backspace
1860 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1861 LyXCursor tmpcursor = cursor;
1862 // to make sure undo gets the right cursor position
1863 cursor = old_cursor;
1864 recordUndo(bv(), Undo::DELETE, cursor.par());
1871 void LyXText::backspace()
1873 // Get the font that is used to calculate the baselineskip
1874 pos_type lastpos = cursor.par()->size();
1876 if (cursor.pos() == 0) {
1877 // The cursor is at the beginning of a paragraph,
1878 // so the the backspace will collapse two paragraphs into one.
1880 // but it's not allowed unless it's new
1881 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1884 // we may paste some paragraphs
1886 // is it an empty paragraph?
1889 || (lastpos == 1 && cursor.par()->isSeparator(0))) {
1890 // This is an empty paragraph and we delete it just
1891 // by moving the cursor one step
1892 // left and let the DeleteEmptyParagraphMechanism
1893 // handle the actual deletion of the paragraph.
1895 if (cursor.par() != ownerParagraphs().begin()) {
1896 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1897 if (cursor.par()->layout() == tmppit->layout()
1898 && cursor.par()->getAlign() == tmppit->getAlign()) {
1899 // Inherit bottom DTD from the paragraph below.
1900 // (the one we are deleting)
1901 tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1902 tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1903 tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1908 // the layout things can change the height of a row !
1914 if (cursor.par() != ownerParagraphs().begin()) {
1915 recordUndo(bv(), Undo::DELETE,
1916 boost::prior(cursor.par()),
1920 ParagraphList::iterator tmppit = cursor.par();
1921 // We used to do cursorLeftIntern() here, but it is
1922 // not a good idea since it triggers the auto-delete
1923 // mechanism. So we do a cursorLeftIntern()-lite,
1924 // without the dreaded mechanism. (JMarc)
1925 if (cursor.par() != ownerParagraphs().begin()) {
1926 // steps into the above paragraph.
1927 setCursorIntern(boost::prior(cursor.par()),
1928 boost::prior(cursor.par())->size(),
1932 // Pasting is not allowed, if the paragraphs have different
1933 // layout. I think it is a real bug of all other
1934 // word processors to allow it. It confuses the user.
1935 // Correction: Pasting is always allowed with standard-layout
1936 Buffer & buf = *bv()->buffer();
1937 BufferParams const & bufparams = buf.params();
1938 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1940 if (cursor.par() != tmppit
1941 && (cursor.par()->layout() == tmppit->layout()
1942 || tmppit->layout() == tclass.defaultLayout())
1943 && cursor.par()->getAlign() == tmppit->getAlign()) {
1944 mergeParagraph(bufparams,
1945 buf.paragraphs(), cursor.par());
1947 if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
1948 cursor.pos(cursor.pos() - 1);
1950 // the row may have changed, block, hfills etc.
1952 setCursor(cursor.par(), cursor.pos(), false);
1955 // this is the code for a normal backspace, not pasting
1957 recordUndo(bv(), Undo::DELETE, cursor.par());
1958 // We used to do cursorLeftIntern() here, but it is
1959 // not a good idea since it triggers the auto-delete
1960 // mechanism. So we do a cursorLeftIntern()-lite,
1961 // without the dreaded mechanism. (JMarc)
1962 setCursorIntern(cursor.par(), cursor.pos() - 1,
1963 false, cursor.boundary());
1964 cursor.par()->erase(cursor.pos());
1967 lastpos = cursor.par()->size();
1968 if (cursor.pos() == lastpos)
1972 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1976 RowList::iterator LyXText::cursorRow() const
1978 return getRow(cursor.par(), cursor.pos());
1982 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
1984 return getRow(cur.par(), cur.pos());
1989 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
1991 RowList::iterator rit = boost::prior(pit->rows.end());
1992 RowList::iterator const begin = pit->rows.begin();
1994 while (rit != begin && rit->pos() > pos)
2001 // returns pointer to some fancy row 'below' specified row
2002 RowList::iterator LyXText::cursorIRow() const
2004 return getRow(cursor.par(), cursor.pos());
2008 RowList::iterator LyXText::getRowNearY(int y,
2009 ParagraphList::iterator & pit) const
2011 //lyxerr << "getRowNearY: y " << y << endl;
2013 pit = boost::prior(ownerParagraphs().end());
2015 RowList::iterator rit = lastRow();
2016 RowList::iterator rbegin = firstRow();
2018 while (rit != rbegin && static_cast<int>(rit->y()) > y)
2019 previousRow(pit, rit);
2025 int LyXText::getDepth() const
2027 return cursor.par()->getDepth();
2031 RowList::iterator LyXText::firstRow() const
2033 return ownerParagraphs().front().rows.begin();
2037 RowList::iterator LyXText::lastRow() const
2039 return boost::prior(endRow());
2043 RowList::iterator LyXText::endRow() const
2045 return ownerParagraphs().back().rows.end();
2049 void LyXText::nextRow(ParagraphList::iterator & pit,
2050 RowList::iterator & rit) const
2053 if (rit == pit->rows.end()) {
2055 if (pit == ownerParagraphs().end())
2058 rit = pit->rows.begin();
2063 void LyXText::previousRow(ParagraphList::iterator & pit,
2064 RowList::iterator & rit) const
2066 if (rit != pit->rows.begin())
2069 Assert(pit != ownerParagraphs().begin());
2071 rit = boost::prior(pit->rows.end());
2076 bool LyXText::noRows() const
2078 return ownerParagraphs().begin()->rows.empty();