3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * \author Jürgen Vigna
14 * Full author contact details are available in file CREDITS.
22 #include "bufferparams.h"
23 #include "BufferView.h"
26 #include "funcrequest.h"
30 #include "lyxlength.h"
33 #include "lyxrow_funcs.h"
34 #include "metricsinfo.h"
35 #include "paragraph.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
38 #include "rowpainter.h"
39 #include "text_funcs.h"
42 #include "WordLangTuple.h"
44 #include "frontends/font_metrics.h"
45 #include "frontends/LyXView.h"
47 #include "insets/insettext.h"
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
52 using bv_funcs::number;
55 using lyx::word_location;
57 using lyx::support::contains;
58 using lyx::support::lowercase;
59 using lyx::support::uppercase;
66 /// top, right, bottom pixel margin
67 extern int const PAPER_MARGIN = 20;
68 /// margin for changebar
69 extern int const CHANGEBAR_MARGIN = 10;
71 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
75 int bibitemMaxWidth(BufferView *, LyXFont const &);
78 BufferView * LyXText::bv()
80 BOOST_ASSERT(bv_owner != 0);
85 BufferView * LyXText::bv() const
87 BOOST_ASSERT(bv_owner != 0);
92 void LyXText::updateRowPositions()
94 ParagraphList::iterator pit = ownerParagraphs().begin();
95 ParagraphList::iterator end = ownerParagraphs().end();
96 for (height = 0; pit != end; ++pit) {
98 height += pit->height;
103 int LyXText::workWidth() const
105 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
109 int LyXText::getRealCursorX() const
112 if (the_locking_inset && the_locking_inset->getLyXText(bv()) != this)
113 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
118 #warning FIXME This function seems to belong outside of LyxText.
119 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
122 if (!Encodings::is_arabic(c))
123 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
124 return c + (0xb0 - '0');
128 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
129 unsigned char next_char = ' ';
131 pos_type const par_size = par.size();
133 for (pos_type i = pos + 1; i < par_size; ++i) {
134 unsigned char const par_char = par.getChar(i);
135 if (!Encodings::IsComposeChar_arabic(par_char)) {
136 next_char = par_char;
141 if (Encodings::is_arabic(next_char)) {
142 if (Encodings::is_arabic(prev_char) &&
143 !Encodings::is_arabic_special(prev_char))
144 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
146 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
148 if (Encodings::is_arabic(prev_char) &&
149 !Encodings::is_arabic_special(prev_char))
150 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
152 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
156 // This is the comments that some of the warnings below refers to.
157 // There are some issues in this file and I don't think they are
158 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
159 // this is a problem that has been here almost from day one and that a
160 // larger userbase with different access patters triggers the bad
161 // behaviour. (segfaults.) What I think happen is: In several places
162 // we store the paragraph in the current cursor and then moves the
163 // cursor. This movement of the cursor will delete paragraph at the
164 // old position if it is now empty. This will make the temporary
165 // pointer to the old cursor paragraph invalid and dangerous to use.
166 // And is some cases this will trigger a segfault. I have marked some
167 // of the cases where this happens with a warning, but I am sure there
168 // are others in this file and in text2.C. There is also a note in
169 // Delete() that you should read. In Delete I store the paragraph->id
170 // instead of a pointer to the paragraph. I am pretty sure this faulty
171 // use of temporary pointers to paragraphs that might have gotten
172 // invalidated (through a cursor movement) before they are used, are
173 // the cause of the strange crashes we get reported often.
175 // It is very tiresom to change this code, especially when it is as
176 // hard to read as it is. Help to fix all the cases where this is done
177 // would be greately appreciated.
181 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
183 if (pos >= pit->size())
186 char const c = pit->getChar(pos);
187 LyXFont const & font = getFont(pit, pos);
188 return singleWidth(pit, pos, c, font);
192 int LyXText::singleWidth(ParagraphList::iterator pit,
193 pos_type pos, char c, LyXFont const & font) const
195 if (pos >= pit->size()) {
196 lyxerr << "in singleWidth(), pos: " << pos << endl;
202 // The most common case is handled first (Asger)
203 if (IsPrintable(c)) {
204 if (font.language()->RightToLeft()) {
205 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
206 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
207 && font.language()->lang() == "arabic") {
208 if (Encodings::IsComposeChar_arabic(c))
211 c = transformChar(c, *pit, pos);
212 } else if (font.language()->lang() == "hebrew" &&
213 Encodings::IsComposeChar_hebrew(c))
216 return font_metrics::width(c, font);
219 if (c == Paragraph::META_INSET) {
220 InsetOld * tmpinset = pit->getInset(pos);
221 BOOST_ASSERT(tmpinset);
222 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
223 // Because of the representation as vertical lines
226 return tmpinset->width();
229 if (IsSeparatorChar(c))
231 return font_metrics::width(c, font);
235 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
237 if (bidi_start == -1)
240 return log2vis_list[pos - bidi_start];
244 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
246 if (bidi_start == -1)
249 return vis2log_list[pos - bidi_start];
253 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
255 if (bidi_start == -1)
258 return bidi_levels[pos - bidi_start];
262 bool LyXText::bidi_InRange(lyx::pos_type pos) const
264 return bidi_start == -1 ||
265 (bidi_start <= pos && pos <= bidi_end);
269 void LyXText::computeBidiTables(Paragraph const & par,
270 Buffer const & buf, Row & row) const
272 bidi_same_direction = true;
273 if (!lyxrc.rtl_support) {
278 InsetOld * inset = par.inInset();
279 if (inset && inset->owner() &&
280 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
285 bidi_start = row.pos();
286 bidi_end = lastPos(par, row);
288 if (bidi_start > bidi_end) {
293 if (bidi_end + 2 - bidi_start >
294 static_cast<pos_type>(log2vis_list.size())) {
296 (bidi_end + 2 - bidi_start < 500) ?
297 500 : 2 * (bidi_end + 2 - bidi_start);
298 log2vis_list.resize(new_size);
299 vis2log_list.resize(new_size);
300 bidi_levels.resize(new_size);
303 vis2log_list[bidi_end + 1 - bidi_start] = -1;
304 log2vis_list[bidi_end + 1 - bidi_start] = -1;
306 BufferParams const & bufparams = buf.params();
308 bool const rtl_par = par.isRightToLeftPar(bufparams);
312 pos_type const body_pos = par.beginningOfBody();
314 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
315 bool is_space = par.isLineSeparator(lpos);
317 (is_space && lpos + 1 <= bidi_end &&
318 !par.isLineSeparator(lpos + 1) &&
319 !par.isNewline(lpos + 1))
321 LyXFont font = par.getFontSettings(bufparams, pos);
322 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
323 font.number() == LyXFont::ON &&
324 par.getFontSettings(bufparams, lpos - 1).number()
326 font = par.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;
342 new_level = new_rtl ? 1 : 2;
344 new_level = rtl_par ? 2 : 0;
346 if (is_space && new_level >= level) {
352 int new_level2 = new_level;
354 if (level == new_level && rtl0 != new_rtl0) {
356 log2vis_list[lpos - bidi_start] = rtl ? 1 : -1;
357 } else if (level < new_level) {
358 log2vis_list[lpos - bidi_start] = rtl ? -1 : 1;
359 if (new_level > rtl_par)
360 bidi_same_direction = false;
362 log2vis_list[lpos - bidi_start] = new_rtl ? -1 : 1;
365 bidi_levels[lpos - bidi_start] = new_level;
367 while (level > new_level2) {
368 pos_type old_lpos = stack[--level];
369 int delta = lpos - old_lpos - 1;
372 log2vis_list[lpos - bidi_start] += delta;
373 log2vis_list[old_lpos - bidi_start] += delta;
375 while (level < new_level)
376 stack[level++] = lpos;
380 pos_type const old_lpos = stack[--level];
381 int delta = bidi_end - old_lpos;
384 log2vis_list[old_lpos - bidi_start] += delta;
387 pos_type vpos = bidi_start - 1;
388 for (pos_type lpos = bidi_start;
389 lpos <= bidi_end; ++lpos) {
390 vpos += log2vis_list[lpos - bidi_start];
391 vis2log_list[vpos - bidi_start] = lpos;
392 log2vis_list[lpos - bidi_start] = vpos;
397 // This method requires a previous call to ComputeBidiTables()
398 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
401 if (!lyxrc.rtl_support || pos == 0)
404 if (!bidi_InRange(pos - 1)) {
405 /// This can happen if pos is the first char of a row.
406 /// Returning false in this case is incorrect!
410 bool const rtl = bidi_level(pos - 1) % 2;
411 bool const rtl2 = bidi_InRange(pos)
412 ? bidi_level(pos) % 2
413 : par.isRightToLeftPar(buf.params());
418 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
419 pos_type pos, LyXFont const & font) const
421 if (!lyxrc.rtl_support)
422 return false; // This is just for speedup
424 bool const rtl = font.isVisibleRightToLeft();
425 bool const rtl2 = bidi_InRange(pos)
426 ? bidi_level(pos) % 2
427 : par.isRightToLeftPar(buf.params());
432 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
434 LyXTextClass const & tclass =
435 bv()->buffer()->params().getLyXTextClass();
436 LyXLayout_ptr const & layout = pit->layout();
438 string parindent = layout->parindent;
442 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
444 // this is the way, LyX handles the LaTeX-Environments.
445 // I have had this idea very late, so it seems to be a
446 // later added hack and this is true
447 if (!pit->getDepth()) {
448 if (pit->layout() == tclass.defaultLayout()) {
449 // find the previous same level paragraph
450 if (pit != ownerParagraphs().begin()) {
451 ParagraphList::iterator newpit =
452 depthHook(pit, ownerParagraphs(),
455 newpit->layout()->nextnoindent)
460 // find the next level paragraph
462 ParagraphList::iterator newpar = outerHook(pit,
465 // make a corresponding row. Needed to call leftMargin()
467 // check wether it is a sufficent paragraph
468 if (newpar != ownerParagraphs().end() &&
469 newpar->layout()->isEnvironment()) {
470 x = leftMargin(newpar, Row(newpar->size()));
473 if (newpar != ownerParagraphs().end() &&
474 pit->layout() == tclass.defaultLayout()) {
475 if (newpar->params().noindent())
478 parindent = newpar->layout()->parindent;
484 LyXFont const labelfont = getLabelFont(pit);
485 switch (layout->margintype) {
487 if (!layout->leftmargin.empty()) {
488 x += font_metrics::signedWidth(layout->leftmargin,
489 tclass.defaultfont());
491 if (!pit->getLabelstring().empty()) {
492 x += font_metrics::signedWidth(layout->labelindent,
494 x += font_metrics::width(pit->getLabelstring(),
496 x += font_metrics::width(layout->labelsep, labelfont);
500 x += font_metrics::signedWidth(layout->labelindent, labelfont);
501 // The width of an empty par, even with manual label, should be 0
502 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
503 if (!pit->getLabelWidthString().empty()) {
504 x += font_metrics::width(pit->getLabelWidthString(),
506 x += font_metrics::width(layout->labelsep, labelfont);
511 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
512 / (pit->getDepth() + 4);
514 case MARGIN_FIRST_DYNAMIC:
515 if (layout->labeltype == LABEL_MANUAL) {
516 if (row.pos() >= pit->beginningOfBody()) {
517 x += font_metrics::signedWidth(layout->leftmargin,
520 x += font_metrics::signedWidth(layout->labelindent,
524 // Special case to fix problems with
526 || (layout->labeltype == LABEL_STATIC
527 && layout->latextype == LATEX_ENVIRONMENT
528 && !isFirstInSequence(pit, ownerParagraphs()))) {
529 x += font_metrics::signedWidth(layout->leftmargin,
531 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
532 && layout->labeltype != LABEL_BIBLIO
533 && layout->labeltype !=
534 LABEL_CENTERED_TOP_ENVIRONMENT) {
535 x += font_metrics::signedWidth(layout->labelindent,
537 x += font_metrics::width(layout->labelsep, labelfont);
538 x += font_metrics::width(pit->getLabelstring(),
543 case MARGIN_RIGHT_ADDRESS_BOX:
545 // ok, a terrible hack. The left margin depends on the widest
546 // row in this paragraph.
547 RowList::iterator rit = pit->rows.begin();
548 RowList::iterator end = pit->rows.end();
549 int minfill = rit->fill();
550 for ( ; rit != end; ++rit)
551 if (rit->fill() < minfill)
552 minfill = rit->fill();
554 x += font_metrics::signedWidth(layout->leftmargin,
555 tclass.defaultfont());
561 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
562 LyXLength const len = pit->params().leftIndent();
563 int const tw = inset_owner ?
564 inset_owner->latexTextWidth(bv()) : workWidth();
565 x += len.inPixels(tw);
570 if (pit->params().align() == LYX_ALIGN_LAYOUT)
571 align = layout->align;
573 align = pit->params().align();
575 // set the correct parindent
576 if (row.pos() == 0) {
577 if ((layout->labeltype == LABEL_NO_LABEL
578 || layout->labeltype == LABEL_TOP_ENVIRONMENT
579 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
580 || (layout->labeltype == LABEL_STATIC
581 && layout->latextype == LATEX_ENVIRONMENT
582 && !isFirstInSequence(pit, ownerParagraphs())))
583 && align == LYX_ALIGN_BLOCK
584 && !pit->params().noindent()
585 // in tabulars and ert paragraphs are never indented!
586 && (!pit->inInset() || !pit->inInset()->owner() ||
587 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
588 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
589 && (pit->layout() != tclass.defaultLayout() ||
590 bv()->buffer()->params().paragraph_separation ==
591 BufferParams::PARSEP_INDENT)) {
592 x += font_metrics::signedWidth(parindent,
593 tclass.defaultfont());
594 } else if (layout->labeltype == LABEL_BIBLIO) {
595 // ale970405 Right width for bibitems
596 x += bibitemMaxWidth(bv(), tclass.defaultfont());
604 int LyXText::rightMargin(Paragraph const & par,
605 Buffer const & buf, Row const &) const
607 LyXTextClass const & tclass = buf.params().getLyXTextClass();
608 LyXLayout_ptr const & layout = par.layout();
611 + font_metrics::signedWidth(tclass.rightmargin(),
612 tclass.defaultfont())
613 + font_metrics::signedWidth(layout->rightmargin,
614 tclass.defaultfont())
615 * 4 / (par.getDepth() + 4);
619 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
621 if (pit->layout()->margintype == MARGIN_MANUAL) {
623 tmprow.pos(pit->size());
624 // return the beginning of the body
625 return leftMargin(pit, tmprow);
628 // LabelEnd is only needed if the layout
629 // fills a flushleft label.
636 // this needs special handling - only newlines count as a break point
637 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
639 for (; i < par.size(); ++i)
640 if (par.isNewline(i))
649 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
650 Row const & row) const
652 // maximum pixel width of a row.
653 int width = workWidth() - rightMargin(*pit, *bv()->buffer(), row);
655 // inset->textWidth() returns -1 via workWidth(),
660 LyXLayout_ptr const & layout = pit->layout();
662 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
663 return addressBreakPoint(row.pos(), *pit);
665 pos_type const pos = row.pos();
666 pos_type const body_pos = pit->beginningOfBody();
667 pos_type const last = pit->size();
668 pos_type point = last;
673 // Now we iterate through until we reach the right margin
674 // or the end of the par, then choose the possible break
677 int const left = leftMargin(pit, row);
680 // pixel width since last breakpoint
685 // We re-use the font resolution for the entire font span when possible
686 LyXFont font = getFont(pit, i);
687 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
689 for (; i < last; ++i) {
690 if (pit->isNewline(i)) {
697 in = pit->getInset(i + 1);
698 if (in && in->display()) {
703 in = pit->getInset(i);
704 if (in && in->display()) {
710 char const c = pit->getChar(i);
711 if (i > endPosOfFontSpan) {
712 font = getFont(pit, i);
713 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
718 // add the auto-hfill from label end to the body
719 if (body_pos && i == body_pos) {
720 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
721 if (pit->isLineSeparator(i - 1))
722 thiswidth -= singleWidth(pit, i - 1);
723 int left_margin = labelEnd(pit, row);
724 if (thiswidth + x < left_margin)
725 thiswidth = left_margin - x;
726 thiswidth += singleWidth(pit, i, c, font);
728 thiswidth = singleWidth(pit, i, c, font);
732 chunkwidth += thiswidth;
734 in = pit->getInset(i);
736 // break before a character that will fall off
737 // the right of the row
739 // if no break before, break here
740 if (point == last || chunkwidth >= width - left)
741 point = (pos < i) ? i - 1 : i;
745 if (!in || in->isChar()) {
746 // some insets are line separators too
747 if (pit->isLineSeparator(i)) {
754 if (point == last && x >= width) {
755 // didn't find one, break at the point we reached the edge
757 } else if (i == last && x < width) {
758 // found one, but we fell off the end of the par, so prefer
763 // manual labels cannot be broken in LaTeX. But we
764 // want to make our on-screen rendering of footnotes
766 if (body_pos && point < body_pos)
767 point = body_pos - 1;
773 // returns the minimum space a row needs on the screen in pixel
774 int LyXText::fill(ParagraphList::iterator pit, Row & row, int paper_width) const
780 // get the pure distance
781 pos_type const last = lastPos(*pit, row);
783 LyXLayout_ptr const & layout = pit->layout();
785 // special handling of the right address boxes
786 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
787 int const tmpfill = row.fill();
788 row.fill(0); // the minfill in MarginLeft()
789 w = leftMargin(pit, row);
792 w = leftMargin(pit, row);
794 pos_type const body_pos = pit->beginningOfBody();
795 pos_type i = row.pos();
797 if (! pit->empty() && i <= last) {
798 // We re-use the font resolution for the entire span when possible
799 LyXFont font = getFont(pit, i);
800 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
802 if (body_pos > 0 && i == body_pos) {
803 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
804 if (pit->isLineSeparator(i - 1))
805 w -= singleWidth(pit, i - 1);
806 int left_margin = labelEnd(pit, row);
810 char const c = pit->getChar(i);
811 if (IsPrintable(c) && i > endPosOfFontSpan) {
812 // We need to get the next font
813 font = getFont(pit, i);
814 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
816 w += singleWidth(pit, i, c, font);
820 if (body_pos > 0 && body_pos > last) {
821 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
822 if (last >= 0 && pit->isLineSeparator(last))
823 w -= singleWidth(pit, last);
824 int const left_margin = labelEnd(pit, row);
829 int const fill = paper_width - w - rightMargin(*pit, *bv()->buffer(), row);
831 // If this case happens, it means that our calculation
832 // of the widths of the chars when we do rowBreakPoint()
833 // went wrong for some reason. Typically in list bodies.
834 // Things just about hobble on anyway, though you'll end
835 // up with a "fill_separator" less than zero, which corresponds
836 // to inter-word spacing being too small. Hopefully this problem
837 // will die when the label hacks die.
838 if (lyxerr.debugging() && fill < 0) {
839 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
840 << " w " << w << " paper_width " << paper_width
841 << " right margin " << rightMargin(*pit, *bv()->buffer(), row) << endl;
847 // returns the minimum space a manual label needs on the screen in pixel
848 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
850 pos_type last = pit->beginningOfBody();
852 BOOST_ASSERT(last > 0);
854 // -1 because a label ends either with a space that is in the label,
855 // or with the beginning of a footnote that is outside the label.
858 // a separator at this end does not count
859 if (pit->isLineSeparator(last))
863 pos_type i = row.pos();
865 w += singleWidth(pit, i);
870 string const & labwidstr = pit->params().labelWidthString();
871 if (!labwidstr.empty()) {
872 LyXFont const labfont = getLabelFont(pit);
873 int const labwidth = font_metrics::width(labwidstr, labfont);
874 fill = max(labwidth - w, 0);
881 LColor_color LyXText::backgroundColor() const
884 return inset_owner->backgroundColor();
886 return LColor::background;
890 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
892 // get the maximum ascent and the maximum descent
893 double layoutasc = 0;
894 double layoutdesc = 0;
897 // ok, let us initialize the maxasc and maxdesc value.
898 // Only the fontsize count. The other properties
899 // are taken from the layoutfont. Nicer on the screen :)
900 LyXLayout_ptr const & layout = pit->layout();
902 // as max get the first character of this row then it can increase but not
903 // decrease the height. Just some point to start with so we don't have to
904 // do the assignment below too often.
905 LyXFont font = getFont(pit, row.pos());
906 LyXFont::FONT_SIZE const tmpsize = font.size();
907 font = getLayoutFont(pit);
908 LyXFont::FONT_SIZE const size = font.size();
909 font.setSize(tmpsize);
911 LyXFont labelfont = getLabelFont(pit);
913 double spacing_val = 1.0;
914 if (!pit->params().spacing().isDefault())
915 spacing_val = pit->params().spacing().getValue();
917 spacing_val = bv()->buffer()->params().spacing().getValue();
918 //lyxerr << "spacing_val = " << spacing_val << endl;
920 int maxasc = int(font_metrics::maxAscent(font) *
921 layout->spacing.getValue() * spacing_val);
922 int maxdesc = int(font_metrics::maxDescent(font) *
923 layout->spacing.getValue() * spacing_val);
925 pos_type const pos_end = lastPos(*pit, row);
930 // We re-use the font resolution for the entire font span when possible
931 LyXFont font = getFont(pit, row.pos());
932 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(row.pos());
935 Paragraph const & par = *pit;
937 // Check if any insets are larger
938 for (pos_type pos = row.pos(); pos <= pos_end; ++pos) {
939 // Manual inlined optimised version of common case of
940 // "maxwidth += singleWidth(pit, pos);"
941 char const c = par.getChar(pos);
943 if (IsPrintable(c)) {
944 if (pos > endPosOfFontSpan) {
945 // We need to get the next font
946 font = getFont(pit, pos);
947 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
949 if (! font.language()->RightToLeft()) {
950 maxwidth += font_metrics::width(c, font);
952 // Fall-back to normal case
953 maxwidth += singleWidth(pit, pos, c, font);
954 // And flush font cache
955 endPosOfFontSpan = 0;
958 // Special handling of insets - are any larger?
959 if (par.isInset(pos)) {
960 InsetOld const * tmpinset = par.getInset(pos);
962 maxwidth += tmpinset->width();
963 maxasc = max(maxasc, tmpinset->ascent());
964 maxdesc = max(maxdesc, tmpinset->descent());
967 // Fall-back to normal case
968 maxwidth += singleWidth(pit, pos, c, font);
969 // And flush font cache
970 endPosOfFontSpan = 0;
976 // Check if any custom fonts are larger (Asger)
977 // This is not completely correct, but we can live with the small,
978 // cosmetic error for now.
979 LyXFont::FONT_SIZE maxsize =
980 pit->highestFontInRange(row.pos(), pos_end, size);
981 if (maxsize > font.size()) {
982 font.setSize(maxsize);
983 maxasc = max(maxasc, font_metrics::maxAscent(font));
984 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
987 // This is nicer with box insets:
991 row.ascent_of_text(maxasc);
995 BufferParams const & bufparams = bv()->buffer()->params();
996 // some parksips VERY EASY IMPLEMENTATION
997 if (bv()->buffer()->params().paragraph_separation ==
998 BufferParams::PARSEP_SKIP)
1000 if (layout->isParagraph()
1001 && pit->getDepth() == 0
1002 && pit != ownerParagraphs().begin())
1004 maxasc += bufparams.getDefSkip().inPixels(*bv());
1005 } else if (pit != ownerParagraphs().begin() &&
1006 boost::prior(pit)->layout()->isParagraph() &&
1007 boost::prior(pit)->getDepth() == 0)
1009 // is it right to use defskip here too? (AS)
1010 maxasc += bufparams.getDefSkip().inPixels(*bv());
1015 if (pit == ownerParagraphs().begin() && !isInInset())
1016 maxasc += PAPER_MARGIN;
1018 // add the vertical spaces, that the user added
1019 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1021 // do not forget the DTP-lines!
1022 // there height depends on the font of the nearest character
1023 if (pit->params().lineTop())
1025 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1026 // and now the pagebreaks
1027 if (pit->params().pagebreakTop())
1028 maxasc += 3 * defaultRowHeight();
1030 if (pit->params().startOfAppendix())
1031 maxasc += 3 * defaultRowHeight();
1033 // This is special code for the chapter, since the label of this
1034 // layout is printed in an extra row
1035 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
1036 float spacing_val = 1.0;
1037 if (!pit->params().spacing().isDefault()) {
1038 spacing_val = pit->params().spacing().getValue();
1040 spacing_val = bufparams.spacing().getValue();
1043 labeladdon = int(font_metrics::maxDescent(labelfont) *
1044 layout->spacing.getValue() *
1046 + int(font_metrics::maxAscent(labelfont) *
1047 layout->spacing.getValue() *
1051 // special code for the top label
1052 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1053 || layout->labeltype == LABEL_BIBLIO
1054 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1055 && isFirstInSequence(pit, ownerParagraphs())
1056 && !pit->getLabelstring().empty())
1058 float spacing_val = 1.0;
1059 if (!pit->params().spacing().isDefault()) {
1060 spacing_val = pit->params().spacing().getValue();
1062 spacing_val = bufparams.spacing().getValue();
1066 (font_metrics::maxAscent(labelfont) +
1067 font_metrics::maxDescent(labelfont)) *
1068 layout->spacing.getValue() *
1070 + layout->topsep * defaultRowHeight()
1071 + layout->labelbottomsep * defaultRowHeight());
1074 // And now the layout spaces, for example before and after
1075 // a section, or between the items of a itemize or enumerate
1078 if (!pit->params().pagebreakTop()) {
1079 ParagraphList::iterator prev =
1080 depthHook(pit, ownerParagraphs(),
1082 if (prev != pit && prev->layout() == layout &&
1083 prev->getDepth() == pit->getDepth() &&
1084 prev->getLabelWidthString() == pit->getLabelWidthString())
1086 layoutasc = (layout->itemsep * defaultRowHeight());
1087 // } else if (rit != firstRow()) {
1088 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
1089 tmptop = layout->topsep;
1092 layoutasc = (tmptop * defaultRowHeight());
1093 } else if (pit->params().lineTop()) {
1094 tmptop = layout->topsep;
1097 layoutasc = (tmptop * defaultRowHeight());
1100 prev = outerHook(pit, ownerParagraphs());
1101 if (prev != ownerParagraphs().end()) {
1102 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1103 } else if (pit != ownerParagraphs().begin()) {
1104 ParagraphList::iterator prior_pit = boost::prior(pit);
1105 if (prior_pit->getDepth() != 0 ||
1106 prior_pit->layout() == layout) {
1107 maxasc += int(layout->parsep * defaultRowHeight());
1113 // is it a bottom line?
1114 if (row.end() == pit->size()) {
1115 // the bottom margin
1116 ParagraphList::iterator nextpit = boost::next(pit);
1117 if (nextpit == ownerParagraphs().end() && !isInInset())
1118 maxdesc += PAPER_MARGIN;
1120 // add the vertical spaces, that the user added
1121 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1123 // do not forget the DTP-lines!
1124 // there height depends on the font of the nearest character
1125 if (pit->params().lineBottom())
1126 maxdesc += 2 * font_metrics::ascent('x',
1127 getFont(pit, max(pos_type(0), pit->size() - 1)));
1129 // and now the pagebreaks
1130 if (pit->params().pagebreakBottom())
1131 maxdesc += 3 * defaultRowHeight();
1133 // and now the layout spaces, for example before and after
1134 // a section, or between the items of a itemize or enumerate
1136 if (!pit->params().pagebreakBottom()
1137 && nextpit != ownerParagraphs().end()) {
1138 ParagraphList::iterator comparepit = pit;
1142 if (comparepit->getDepth() > nextpit->getDepth()) {
1143 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1144 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1145 if (comparepit->layout()!= nextpit->layout()
1146 || nextpit->getLabelWidthString() !=
1147 comparepit->getLabelWidthString())
1149 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1151 if (unusual > usual)
1152 layoutdesc = unusual;
1155 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1157 if (comparepit->layout() != nextpit->layout()
1158 || nextpit->getLabelWidthString() !=
1159 comparepit->getLabelWidthString())
1160 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1165 // incalculate the layout spaces
1166 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1167 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1169 row.height(maxasc + maxdesc + labeladdon);
1170 row.baseline(maxasc + labeladdon);
1171 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1173 row.width(maxwidth);
1175 width = max(0, workWidth());
1176 RowList::iterator rit = firstRow();
1177 RowList::iterator end = endRow();
1178 ParagraphList::iterator it = ownerParagraphs().begin();
1179 while (rit != end) {
1180 if (rit->width() > width)
1181 width = rit->width();
1188 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1190 // allow only if at start or end, or all previous is new text
1191 if (cursor.pos() && cursor.pos() != cursorPar()->size()
1192 && cursorPar()->isChangeEdited(0, cursor.pos()))
1195 LyXTextClass const & tclass =
1196 bv()->buffer()->params().getLyXTextClass();
1197 LyXLayout_ptr const & layout = cursorPar()->layout();
1199 // this is only allowed, if the current paragraph is not empty or caption
1200 // and if it has not the keepempty flag active
1201 if (cursorPar()->empty() && !cursorPar()->allowEmpty()
1202 && layout->labeltype != LABEL_SENSITIVE)
1205 recUndo(cursor.par());
1207 // Always break behind a space
1209 // It is better to erase the space (Dekel)
1210 if (cursor.pos() < cursorPar()->size()
1211 && cursorPar()->isLineSeparator(cursor.pos()))
1212 cursorPar()->erase(cursor.pos());
1214 // break the paragraph
1218 keep_layout = layout->isEnvironment();
1220 // we need to set this before we insert the paragraph. IMO the
1221 // breakParagraph call should return a bool if it inserts the
1222 // paragraph before or behind and we should react on that one
1223 // but we can fix this in 1.3.0 (Jug 20020509)
1224 bool const isempty = (cursorPar()->allowEmpty() && cursorPar()->empty());
1225 ::breakParagraph(bv()->buffer()->params(), paragraphs, cursorPar(),
1226 cursor.pos(), keep_layout);
1228 #warning Trouble Point! (Lgb)
1229 // When ::breakParagraph is called from within an inset we must
1230 // ensure that the correct ParagraphList is used. Today that is not
1231 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1232 ParagraphList::iterator next_par = boost::next(cursorPar());
1234 // well this is the caption hack since one caption is really enough
1235 if (layout->labeltype == LABEL_SENSITIVE) {
1237 // set to standard-layout
1238 cursorPar()->applyLayout(tclass.defaultLayout());
1240 // set to standard-layout
1241 next_par->applyLayout(tclass.defaultLayout());
1244 // if the cursor is at the beginning of a row without prior newline,
1246 // This touches only the screen-update. Otherwise we would may have
1247 // an empty row on the screen
1248 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1249 && !cursorPar()->isNewline(cursor.pos() - 1))
1254 while (!next_par->empty() && next_par->isNewline(0))
1258 redoParagraph(cursorPar());
1259 redoParagraph(next_par);
1261 // This check is necessary. Otherwise the new empty paragraph will
1262 // be deleted automatically. And it is more friendly for the user!
1263 if (cursor.pos() || isempty)
1264 setCursor(next_par, 0);
1266 setCursor(cursorPar(), 0);
1270 // convenience function
1271 void LyXText::redoParagraph()
1274 redoParagraph(cursorPar());
1275 setCursorIntern(cursor.par(), cursor.pos());
1279 // insert a character, moves all the following breaks in the
1280 // same Paragraph one to the right and make a rebreak
1281 void LyXText::insertChar(char c)
1283 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
1285 // When the free-spacing option is set for the current layout,
1286 // disable the double-space checking
1288 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1289 cursorPar()->isFreeSpacing();
1291 if (lyxrc.auto_number) {
1292 static string const number_operators = "+-/*";
1293 static string const number_unary_operators = "+-";
1294 static string const number_seperators = ".,:";
1296 if (current_font.number() == LyXFont::ON) {
1297 if (!IsDigit(c) && !contains(number_operators, c) &&
1298 !(contains(number_seperators, c) &&
1299 cursor.pos() >= 1 &&
1300 cursor.pos() < cursorPar()->size() &&
1301 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1302 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1304 number(bv()); // Set current_font.number to OFF
1305 } else if (IsDigit(c) &&
1306 real_current_font.isVisibleRightToLeft()) {
1307 number(bv()); // Set current_font.number to ON
1309 if (cursor.pos() > 0) {
1310 char const c = cursorPar()->getChar(cursor.pos() - 1);
1311 if (contains(number_unary_operators, c) &&
1312 (cursor.pos() == 1 ||
1313 cursorPar()->isSeparator(cursor.pos() - 2) ||
1314 cursorPar()->isNewline(cursor.pos() - 2))
1320 } else if (contains(number_seperators, c) &&
1321 cursor.pos() >= 2 &&
1324 cursor.pos() - 2).number() == LyXFont::ON) {
1335 // First check, if there will be two blanks together or a blank at
1336 // the beginning of a paragraph.
1337 // I decided to handle blanks like normal characters, the main
1338 // difference are the special checks when calculating the row.fill
1339 // (blank does not count at the end of a row) and the check here
1341 // The bug is triggered when we type in a description environment:
1342 // The current_font is not changed when we go from label to main text
1343 // and it should (along with realtmpfont) when we type the space.
1344 // CHECK There is a bug here! (Asger)
1346 // store the current font. This is because of the use of cursor
1347 // movements. The moving cursor would refresh the current font
1348 LyXFont realtmpfont = real_current_font;
1349 LyXFont rawtmpfont = current_font;
1351 if (!freeSpacing && IsLineSeparatorChar(c)) {
1352 if ((cursor.pos() > 0
1353 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1354 || (cursor.pos() > 0
1355 && cursorPar()->isNewline(cursor.pos() - 1))
1356 || (cursor.pos() == 0)) {
1357 static bool sent_space_message = false;
1358 if (!sent_space_message) {
1359 if (cursor.pos() == 0)
1360 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1362 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1363 sent_space_message = true;
1370 // Here case LyXText::InsertInset already inserted the character
1371 if (c != Paragraph::META_INSET)
1372 cursorPar()->insertChar(cursor.pos(), c);
1374 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1376 current_font = rawtmpfont;
1377 real_current_font = realtmpfont;
1378 redoParagraph(cursorPar());
1379 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1385 void LyXText::charInserted()
1387 // Here we could call finishUndo for every 20 characters inserted.
1388 // This is from my experience how emacs does it. (Lgb)
1389 static unsigned int counter;
1399 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1400 RowList::iterator const rit) const
1402 double w = rit->fill();
1403 double fill_hfill = 0;
1404 double fill_label_hfill = 0;
1405 double fill_separator = 0;
1409 pit->isRightToLeftPar(bv()->buffer()->params());
1411 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer(), *rit) : 0;
1413 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1415 // is there a manual margin with a manual label
1416 LyXLayout_ptr const & layout = pit->layout();
1418 if (layout->margintype == MARGIN_MANUAL
1419 && layout->labeltype == LABEL_MANUAL) {
1420 /// We might have real hfills in the label part
1421 int nlh = numberOfLabelHfills(*pit, *rit);
1423 // A manual label par (e.g. List) has an auto-hfill
1424 // between the label text and the body of the
1426 // But we don't want to do this auto hfill if the par
1431 if (nlh && !pit->getLabelWidthString().empty()) {
1432 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1436 // are there any hfills in the row?
1437 int const nh = numberOfHfills(*pit, *rit);
1441 fill_hfill = w / nh;
1442 // we don't have to look at the alignment if it is ALIGN_LEFT and
1443 // if the row is already larger then the permitted width as then
1444 // we force the LEFT_ALIGN'edness!
1445 } else if (int(rit->width()) < workWidth()) {
1446 // is it block, flushleft or flushright?
1447 // set x how you need it
1449 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1450 align = layout->align;
1452 align = pit->params().align();
1454 InsetOld * inset = 0;
1455 // ERT insets should always be LEFT ALIGNED on screen
1456 inset = pit->inInset();
1457 if (inset && inset->owner() &&
1458 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1460 align = LYX_ALIGN_LEFT;
1463 // Display-style insets should always be on a centred row
1464 // (Simplify this to inset = pit->getInset(rit->pos()) once
1465 // the "bit silly" bug is fixed, MV)
1466 inset = pit->isInset(rit->pos()) ? pit->getInset(rit->pos()) : 0;
1467 if (inset && inset->display()) {
1468 align = LYX_ALIGN_CENTER;
1472 case LYX_ALIGN_BLOCK:
1474 int const ns = numberOfSeparators(*pit, *rit);
1475 RowList::iterator next_row = boost::next(rit);
1476 bool disp_inset = false;
1477 if (next_row != pit->rows.end()) {
1478 InsetOld * in = pit->getInset(next_row->pos());
1480 disp_inset = in->display();
1482 // If we have separators, this is not the last row of a
1483 // par, does not end in newline, and is not row above a
1484 // display inset... then stretch it
1486 && next_row != pit->rows.end()
1487 && !pit->isNewline(next_row->pos() - 1)
1490 fill_separator = w / ns;
1491 } else if (is_rtl) {
1496 case LYX_ALIGN_RIGHT:
1499 case LYX_ALIGN_CENTER:
1505 computeBidiTables(*pit, *bv()->buffer(), *rit);
1507 pos_type body_pos = pit->beginningOfBody();
1508 pos_type last = lastPos(*pit, *rit);
1511 (body_pos - 1 > last ||
1512 !pit->isLineSeparator(body_pos - 1))) {
1513 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1514 if (body_pos - 1 <= last)
1515 x += fill_label_hfill;
1519 rit->fill_hfill(fill_hfill);
1520 rit->fill_label_hfill(fill_label_hfill);
1521 rit->fill_separator(fill_separator);
1526 // important for the screen
1529 // the cursor set functions have a special mechanism. When they
1530 // realize, that you left an empty paragraph, they will delete it.
1531 // They also delete the corresponding row
1533 void LyXText::cursorRightOneWord()
1535 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1536 setCursor(cursorPar(), cursor.pos());
1540 // Skip initial whitespace at end of word and move cursor to *start*
1541 // of prior word, not to end of next prior word.
1542 void LyXText::cursorLeftOneWord()
1544 LyXCursor tmpcursor = cursor;
1545 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1546 setCursor(getPar(tmpcursor), tmpcursor.pos());
1550 void LyXText::selectWord(word_location loc)
1552 LyXCursor from = cursor;
1553 LyXCursor to = cursor;
1554 ::getWord(*this, from, to, loc, ownerParagraphs());
1556 setCursor(from.par(), from.pos());
1559 selection.cursor = cursor;
1560 setCursor(to.par(), to.pos());
1565 // Select the word currently under the cursor when no
1566 // selection is currently set
1567 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1569 if (!selection.set()) {
1571 return selection.set();
1577 void LyXText::acceptChange()
1579 if (!selection.set() && cursorPar()->size())
1582 if (selection.start.par() == selection.end.par()) {
1583 LyXCursor & startc = selection.start;
1584 LyXCursor & endc = selection.end;
1585 recordUndo(Undo::INSERT, this, startc.par());
1586 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1589 redoParagraph(getPar(startc));
1590 setCursorIntern(startc.par(), 0);
1592 #warning handle multi par selection
1596 void LyXText::rejectChange()
1598 if (!selection.set() && cursorPar()->size())
1601 if (selection.start.par() == selection.end.par()) {
1602 LyXCursor & startc = selection.start;
1603 LyXCursor & endc = selection.end;
1604 recordUndo(Undo::INSERT, this, startc.par());
1605 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1608 redoParagraph(getPar(startc));
1609 setCursorIntern(startc.par(), 0);
1611 #warning handle multi par selection
1615 // This function is only used by the spellchecker for NextWord().
1616 // It doesn't handle LYX_ACCENTs and probably never will.
1617 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1619 if (the_locking_inset) {
1620 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1621 if (!word.word().empty()) {
1622 value += float(cursor.y());
1623 value /= float(height);
1626 // we have to go on checking so move cursor to the next char
1627 if (cursor.pos() == cursorPar()->size()) {
1628 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1630 cursor.par(cursor.par() + 1);
1633 cursor.pos(cursor.pos() + 1);
1635 int const tmppar = cursor.par();
1637 // If this is not the very first word, skip rest of
1638 // current word because we are probably in the middle
1639 // of a word if there is text here.
1640 if (cursor.pos() || cursor.par() != 0) {
1641 while (cursor.pos() < cursorPar()->size()
1642 && cursorPar()->isLetter(cursor.pos()))
1643 cursor.pos(cursor.pos() + 1);
1646 // Now, skip until we have real text (will jump paragraphs)
1648 ParagraphList::iterator cpit = cursorPar();
1649 pos_type const cpos = cursor.pos();
1651 if (cpos == cpit->size()) {
1652 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1653 cursor.par(cursor.par() + 1);
1660 bool const is_good_inset = cpit->isInset(cpos)
1661 && cpit->getInset(cpos)->allowSpellcheck();
1663 if (!isDeletedText(*cpit, cpos)
1664 && (is_good_inset || cpit->isLetter(cpos)))
1667 cursor.pos(cpos + 1);
1670 // now check if we hit an inset so it has to be a inset containing text!
1671 if (cursor.pos() < cursorPar()->size() &&
1672 cursorPar()->isInset(cursor.pos())) {
1674 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1675 cursorPar()->getInset(cursor.pos())->dispatch(cmd);
1676 // now call us again to do the above trick
1677 // but obviously we have to start from down below ;)
1678 return bv()->text->selectNextWordToSpellcheck(value);
1681 // Update the value if we changed paragraphs
1682 if (cursor.par() != tmppar) {
1683 setCursor(cursor.par(), cursor.pos());
1684 value = float(cursor.y())/float(height);
1687 // Start the selection from here
1688 selection.cursor = cursor;
1690 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1691 // and find the end of the word (insets like optional hyphens
1692 // and ligature break are part of a word)
1693 while (cursor.pos() < cursorPar()->size()
1694 && cursorPar()->isLetter(cursor.pos())
1695 && !isDeletedText(*cursorPar(), cursor.pos()))
1696 cursor.pos(cursor.pos() + 1);
1698 // Finally, we copy the word to a string and return it
1700 if (selection.cursor.pos() < cursor.pos()) {
1702 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1703 if (!cursorPar()->isInset(i))
1704 str += cursorPar()->getChar(i);
1707 return WordLangTuple(str, lang_code);
1711 // This one is also only for the spellchecker
1712 void LyXText::selectSelectedWord()
1714 if (the_locking_inset) {
1715 the_locking_inset->selectSelectedWord(bv());
1718 // move cursor to the beginning
1719 setCursor(selection.cursor.par(), selection.cursor.pos());
1721 // set the sel cursor
1722 selection.cursor = cursor;
1724 // now find the end of the word
1725 while (cursor.pos() < cursorPar()->size()
1726 && cursorPar()->isLetter(cursor.pos()))
1727 cursor.pos(cursor.pos() + 1);
1729 setCursor(cursorPar(), cursor.pos());
1731 // finally set the selection
1736 // Delete from cursor up to the end of the current or next word.
1737 void LyXText::deleteWordForward()
1739 if (cursorPar()->empty())
1742 LyXCursor tmpcursor = cursor;
1743 selection.set(true); // to avoid deletion
1744 cursorRightOneWord();
1745 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1746 selection.cursor = cursor;
1750 // Great, CutSelection() gets rid of multiple spaces.
1751 cutSelection(true, false);
1756 // Delete from cursor to start of current or prior word.
1757 void LyXText::deleteWordBackward()
1759 if (cursorPar()->empty())
1762 LyXCursor tmpcursor = cursor;
1763 selection.set(true); // to avoid deletion
1764 cursorLeftOneWord();
1765 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1766 selection.cursor = cursor;
1769 cutSelection(true, false);
1774 // Kill to end of line.
1775 void LyXText::deleteLineForward()
1777 if (cursorPar()->empty())
1778 // Paragraph is empty, so we just go to the right
1781 LyXCursor tmpcursor = cursor;
1782 // We can't store the row over a regular setCursor
1783 // so we set it to 0 and reset it afterwards.
1784 selection.set(true); // to avoid deletion
1786 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1787 selection.cursor = cursor;
1790 // What is this test for ??? (JMarc)
1791 if (!selection.set()) {
1792 deleteWordForward();
1794 cutSelection(true, false);
1800 void LyXText::changeCase(LyXText::TextCase action)
1805 if (selection.set()) {
1806 from = selection.start;
1810 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1811 setCursor(to.par(), to.pos() + 1);
1814 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1816 pos_type pos = from.pos();
1817 int par = from.par();
1819 while (par != int(ownerParagraphs().size()) &&
1820 (pos != to.pos() || par != to.par())) {
1821 ParagraphList::iterator pit = getPar(par);
1822 if (pos == pit->size()) {
1827 unsigned char c = pit->getChar(pos);
1828 if (c != Paragraph::META_INSET) {
1830 case text_lowercase:
1833 case text_capitalization:
1835 action = text_lowercase;
1837 case text_uppercase:
1843 pit->setChar(pos, c);
1849 void LyXText::Delete()
1851 // this is a very easy implementation
1853 LyXCursor old_cursor = cursor;
1854 int const old_cur_par_id = cursorPar()->id();
1855 int const old_cur_par_prev_id =
1856 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1858 // just move to the right
1861 // CHECK Look at the comment here.
1862 // This check is not very good...
1863 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1864 // and that can very well delete the par or par->previous in
1865 // old_cursor. Will a solution where we compare paragraph id's
1867 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1868 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1869 // delete-empty-paragraph-mechanism has done it
1873 // if you had success make a backspace
1874 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1875 recordUndo(Undo::DELETE, this, old_cursor.par());
1881 void LyXText::backspace()
1883 // Get the font that is used to calculate the baselineskip
1884 ParagraphList::iterator pit = cursorPar();
1885 pos_type lastpos = pit->size();
1887 if (cursor.pos() == 0) {
1888 // The cursor is at the beginning of a paragraph,
1889 // so the the backspace will collapse two paragraphs into one.
1891 // but it's not allowed unless it's new
1892 if (pit->isChangeEdited(0, pit->size()))
1895 // we may paste some paragraphs
1897 // is it an empty paragraph?
1899 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1900 // This is an empty paragraph and we delete it just
1901 // by moving the cursor one step
1902 // left and let the DeleteEmptyParagraphMechanism
1903 // handle the actual deletion of the paragraph.
1906 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1907 if (cursorPar()->layout() == tmppit->layout()
1908 && cursorPar()->getAlign() == tmppit->getAlign()) {
1909 // Inherit bottom DTD from the paragraph below.
1910 // (the one we are deleting)
1911 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1912 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1913 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1918 // the layout things can change the height of a row !
1924 if (cursor.par() != 0)
1925 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1927 ParagraphList::iterator tmppit = cursorPar();
1928 // We used to do cursorLeftIntern() here, but it is
1929 // not a good idea since it triggers the auto-delete
1930 // mechanism. So we do a cursorLeftIntern()-lite,
1931 // without the dreaded mechanism. (JMarc)
1932 if (cursor.par() != 0) {
1933 // steps into the above paragraph.
1934 setCursorIntern(cursor.par() - 1,
1935 getPar(cursor.par() - 1)->size(),
1939 // Pasting is not allowed, if the paragraphs have different
1940 // layout. I think it is a real bug of all other
1941 // word processors to allow it. It confuses the user.
1942 // Correction: Pasting is always allowed with standard-layout
1943 Buffer & buf = *bv()->buffer();
1944 BufferParams const & bufparams = buf.params();
1945 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1947 if (cursorPar() != tmppit
1948 && (cursorPar()->layout() == tmppit->layout()
1949 || tmppit->layout() == tclass.defaultLayout())
1950 && cursorPar()->getAlign() == tmppit->getAlign()) {
1951 mergeParagraph(bufparams,
1952 buf.paragraphs(), cursorPar());
1954 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1955 cursor.pos(cursor.pos() - 1);
1957 // the row may have changed, block, hfills etc.
1959 setCursor(cursor.par(), cursor.pos(), false);
1962 // this is the code for a normal backspace, not pasting
1964 recordUndo(Undo::DELETE, this, cursor.par());
1965 // We used to do cursorLeftIntern() here, but it is
1966 // not a good idea since it triggers the auto-delete
1967 // mechanism. So we do a cursorLeftIntern()-lite,
1968 // without the dreaded mechanism. (JMarc)
1969 setCursorIntern(cursor.par(), cursor.pos() - 1,
1970 false, cursor.boundary());
1971 cursorPar()->erase(cursor.pos());
1974 lastpos = cursorPar()->size();
1975 if (cursor.pos() == lastpos)
1979 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1983 ParagraphList::iterator LyXText::cursorPar() const
1985 return getPar(cursor.par());
1989 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1991 return getPar(cur.par());
1995 ParagraphList::iterator LyXText::getPar(int par) const
1997 BOOST_ASSERT(par >= 0);
1998 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1999 ParagraphList::iterator pit = ownerParagraphs().begin();
2000 std::advance(pit, par);
2006 RowList::iterator LyXText::cursorRow() const
2008 return getRow(*cursorPar(), cursor.pos());
2012 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2014 return getRow(*getPar(cur), cur.pos());
2018 RowList::iterator LyXText::getRow(Paragraph & par, pos_type pos) const
2020 RowList::iterator rit = boost::prior(par.rows.end());
2021 RowList::iterator const begin = par.rows.begin();
2023 while (rit != begin && rit->pos() > pos)
2031 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
2033 //lyxerr << "getRowNearY: y " << y << endl;
2035 pit = boost::prior(ownerParagraphs().end());
2037 RowList::iterator rit = lastRow();
2038 RowList::iterator rbegin = firstRow();
2040 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
2041 previousRow(pit, rit);
2047 int LyXText::getDepth() const
2049 return cursorPar()->getDepth();
2053 RowList::iterator LyXText::firstRow() const
2055 return ownerParagraphs().front().rows.begin();
2059 RowList::iterator LyXText::lastRow() const
2061 return boost::prior(endRow());
2065 RowList::iterator LyXText::endRow() const
2067 return ownerParagraphs().back().rows.end();
2071 void LyXText::nextRow(ParagraphList::iterator & pit,
2072 RowList::iterator & rit) const
2075 if (rit == pit->rows.end()) {
2077 if (pit == ownerParagraphs().end())
2080 rit = pit->rows.begin();
2085 void LyXText::previousRow(ParagraphList::iterator & pit,
2086 RowList::iterator & rit) const
2088 if (rit != pit->rows.begin())
2091 BOOST_ASSERT(pit != ownerParagraphs().begin());
2093 rit = boost::prior(pit->rows.end());
2098 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
2100 if (!selection.set())
2103 // should be const ...
2104 ParagraphList::iterator startpit = getPar(selection.start);
2105 ParagraphList::iterator endpit = getPar(selection.end);
2106 size_t const startpos = selection.start.pos();
2107 size_t const endpos = selection.end.pos();
2109 if (startpit == endpit)
2110 return startpit->asString(buffer, startpos, endpos, label);
2112 // First paragraph in selection
2114 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
2116 // The paragraphs in between (if any)
2117 ParagraphList::iterator pit = startpit;
2118 for (++pit; pit != endpit; ++pit)
2119 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
2121 // Last paragraph in selection
2122 result += endpit->asString(buffer, 0, endpos, label);
2128 int LyXText::parOffset(ParagraphList::iterator pit) const
2130 return std::distance(ownerParagraphs().begin(), pit);
2134 int LyXText::redoParagraphInternal(ParagraphList::iterator pit)
2136 // remove rows of paragraph, keep track of height changes
2137 height -= pit->height;
2141 InsetList::iterator ii = pit->insetlist.begin();
2142 InsetList::iterator iend = pit->insetlist.end();
2143 for (; ii != iend; ++ii) {
2145 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
2146 ii->inset->metrics(mi, dim);
2149 // rebreak the paragraph
2151 int const ww = workWidth();
2154 for (pos_type z = 0; z < pit->size() + 1; ) {
2156 z = rowBreakPoint(pit, row) + 1;
2158 pit->rows.push_back(row);
2161 RowList::iterator rit = pit->rows.begin();
2162 RowList::iterator end = pit->rows.end();
2163 for (rit = pit->rows.begin(); rit != end; ++rit) {
2164 int const f = fill(pit, *rit, ww);
2165 int const w = ww - f;
2166 par_width = std::max(par_width, w);
2169 prepareToPrint(pit, rit);
2170 setHeightOfRow(pit, *rit);
2171 rit->y_offset(pit->height);
2172 pit->height += rit->height();
2174 height += pit->height;
2176 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
2181 int LyXText::redoParagraphs(ParagraphList::iterator start,
2182 ParagraphList::iterator end)
2185 for ( ; start != end; ++start) {
2186 int par_width = redoParagraphInternal(start);
2187 pars_width = std::max(par_width, pars_width);
2189 updateRowPositions();
2194 void LyXText::redoParagraph(ParagraphList::iterator pit)
2196 redoParagraphInternal(pit);
2197 updateRowPositions();
2201 void LyXText::fullRebreak()
2203 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2205 selection.cursor = cursor;
2209 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
2211 //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
2212 // << " workWidth: " << workWidth() << endl;
2213 //BOOST_ASSERT(mi.base.textwidth);
2215 // rebuild row cache
2220 width = redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2223 dim.asc = firstRow()->ascent_of_text();
2224 dim.des = height - dim.asc;
2225 dim.wid = std::max(mi.base.textwidth, int(width));