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 return (bidi_start == -1) ? pos : log2vis_list[pos - bidi_start];
241 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
243 return (bidi_start == -1) ? pos : vis2log_list[pos - bidi_start];
247 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
249 return (bidi_start == -1) ? 0 : bidi_levels[pos - bidi_start];
253 bool LyXText::bidi_InRange(lyx::pos_type pos) const
255 return bidi_start == -1 || (bidi_start <= pos && pos <= bidi_end);
259 void LyXText::computeBidiTables(Paragraph const & par,
260 Buffer const & buf, Row & row) const
262 bidi_same_direction = true;
263 if (!lyxrc.rtl_support) {
268 InsetOld * inset = par.inInset();
269 if (inset && inset->owner() &&
270 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
275 bidi_start = row.pos();
276 bidi_end = lastPos(par, row);
278 if (bidi_start > bidi_end) {
283 if (bidi_end + 2 - bidi_start >
284 static_cast<pos_type>(log2vis_list.size())) {
286 (bidi_end + 2 - bidi_start < 500) ?
287 500 : 2 * (bidi_end + 2 - bidi_start);
288 log2vis_list.resize(new_size);
289 vis2log_list.resize(new_size);
290 bidi_levels.resize(new_size);
293 vis2log_list[bidi_end + 1 - bidi_start] = -1;
294 log2vis_list[bidi_end + 1 - bidi_start] = -1;
296 BufferParams const & bufparams = buf.params();
298 bool const rtl_par = par.isRightToLeftPar(bufparams);
302 pos_type const body_pos = par.beginningOfBody();
304 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
305 bool is_space = par.isLineSeparator(lpos);
307 (is_space && lpos + 1 <= bidi_end &&
308 !par.isLineSeparator(lpos + 1) &&
309 !par.isNewline(lpos + 1))
311 LyXFont font = par.getFontSettings(bufparams, pos);
312 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
313 font.number() == LyXFont::ON &&
314 par.getFontSettings(bufparams, lpos - 1).number()
316 font = par.getFontSettings(bufparams, lpos);
320 bool new_rtl = font.isVisibleRightToLeft();
321 bool new_rtl0 = font.isRightToLeft();
324 if (lpos == body_pos - 1
325 && row.pos() < body_pos - 1
327 new_level = rtl_par ? 1 : 0;
331 new_level = new_rtl ? 1 : 2;
333 new_level = rtl_par ? 2 : 0;
335 if (is_space && new_level >= level) {
341 int new_level2 = new_level;
343 if (level == new_level && rtl0 != new_rtl0) {
345 log2vis_list[lpos - bidi_start] = rtl ? 1 : -1;
346 } else if (level < new_level) {
347 log2vis_list[lpos - bidi_start] = rtl ? -1 : 1;
348 if (new_level > rtl_par)
349 bidi_same_direction = false;
351 log2vis_list[lpos - bidi_start] = new_rtl ? -1 : 1;
354 bidi_levels[lpos - bidi_start] = new_level;
356 while (level > new_level2) {
357 pos_type old_lpos = stack[--level];
358 int delta = lpos - old_lpos - 1;
361 log2vis_list[lpos - bidi_start] += delta;
362 log2vis_list[old_lpos - bidi_start] += delta;
364 while (level < new_level)
365 stack[level++] = lpos;
369 pos_type const old_lpos = stack[--level];
370 int delta = bidi_end - old_lpos;
373 log2vis_list[old_lpos - bidi_start] += delta;
376 pos_type vpos = bidi_start - 1;
377 for (pos_type lpos = bidi_start;
378 lpos <= bidi_end; ++lpos) {
379 vpos += log2vis_list[lpos - bidi_start];
380 vis2log_list[vpos - bidi_start] = lpos;
381 log2vis_list[lpos - bidi_start] = vpos;
386 // This method requires a previous call to ComputeBidiTables()
387 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
390 if (!lyxrc.rtl_support || pos == 0)
393 if (!bidi_InRange(pos - 1)) {
394 // This can happen if pos is the first char of a row.
395 // Returning false in this case is incorrect!
399 bool const rtl = bidi_level(pos - 1) % 2;
400 bool const rtl2 = bidi_InRange(pos)
401 ? bidi_level(pos) % 2
402 : par.isRightToLeftPar(buf.params());
407 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
408 pos_type pos, LyXFont const & font) const
410 if (!lyxrc.rtl_support)
411 return false; // This is just for speedup
413 bool const rtl = font.isVisibleRightToLeft();
414 bool const rtl2 = bidi_InRange(pos)
415 ? bidi_level(pos) % 2
416 : par.isRightToLeftPar(buf.params());
421 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
423 LyXTextClass const & tclass =
424 bv()->buffer()->params().getLyXTextClass();
425 LyXLayout_ptr const & layout = pit->layout();
427 string parindent = layout->parindent;
431 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
433 // this is the way, LyX handles the LaTeX-Environments.
434 // I have had this idea very late, so it seems to be a
435 // later added hack and this is true
436 if (!pit->getDepth()) {
437 if (pit->layout() == tclass.defaultLayout()) {
438 // find the previous same level paragraph
439 if (pit != ownerParagraphs().begin()) {
440 ParagraphList::iterator newpit =
441 depthHook(pit, ownerParagraphs(),
444 newpit->layout()->nextnoindent)
449 // find the next level paragraph
451 ParagraphList::iterator newpar = outerHook(pit,
454 // make a corresponding row. Needed to call leftMargin()
456 // check wether it is a sufficent paragraph
457 if (newpar != ownerParagraphs().end() &&
458 newpar->layout()->isEnvironment()) {
459 x = leftMargin(newpar, Row(newpar->size()));
462 if (newpar != ownerParagraphs().end() &&
463 pit->layout() == tclass.defaultLayout()) {
464 if (newpar->params().noindent())
467 parindent = newpar->layout()->parindent;
473 LyXFont const labelfont = getLabelFont(pit);
474 switch (layout->margintype) {
476 if (!layout->leftmargin.empty()) {
477 x += font_metrics::signedWidth(layout->leftmargin,
478 tclass.defaultfont());
480 if (!pit->getLabelstring().empty()) {
481 x += font_metrics::signedWidth(layout->labelindent,
483 x += font_metrics::width(pit->getLabelstring(),
485 x += font_metrics::width(layout->labelsep, labelfont);
489 x += font_metrics::signedWidth(layout->labelindent, labelfont);
490 // The width of an empty par, even with manual label, should be 0
491 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
492 if (!pit->getLabelWidthString().empty()) {
493 x += font_metrics::width(pit->getLabelWidthString(),
495 x += font_metrics::width(layout->labelsep, labelfont);
500 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
501 / (pit->getDepth() + 4);
503 case MARGIN_FIRST_DYNAMIC:
504 if (layout->labeltype == LABEL_MANUAL) {
505 if (row.pos() >= pit->beginningOfBody()) {
506 x += font_metrics::signedWidth(layout->leftmargin,
509 x += font_metrics::signedWidth(layout->labelindent,
513 // Special case to fix problems with
515 || (layout->labeltype == LABEL_STATIC
516 && layout->latextype == LATEX_ENVIRONMENT
517 && !isFirstInSequence(pit, ownerParagraphs()))) {
518 x += font_metrics::signedWidth(layout->leftmargin,
520 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
521 && layout->labeltype != LABEL_BIBLIO
522 && layout->labeltype !=
523 LABEL_CENTERED_TOP_ENVIRONMENT) {
524 x += font_metrics::signedWidth(layout->labelindent,
526 x += font_metrics::width(layout->labelsep, labelfont);
527 x += font_metrics::width(pit->getLabelstring(),
532 case MARGIN_RIGHT_ADDRESS_BOX:
534 // ok, a terrible hack. The left margin depends on the widest
535 // row in this paragraph.
536 RowList::iterator rit = pit->rows.begin();
537 RowList::iterator end = pit->rows.end();
538 int minfill = rit->fill();
539 for ( ; rit != end; ++rit)
540 if (rit->fill() < minfill)
541 minfill = rit->fill();
543 x += font_metrics::signedWidth(layout->leftmargin,
544 tclass.defaultfont());
550 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
551 LyXLength const len = pit->params().leftIndent();
552 int const tw = inset_owner ?
553 inset_owner->latexTextWidth(bv()) : workWidth();
554 x += len.inPixels(tw);
559 if (pit->params().align() == LYX_ALIGN_LAYOUT)
560 align = layout->align;
562 align = pit->params().align();
564 // set the correct parindent
565 if (row.pos() == 0) {
566 if ((layout->labeltype == LABEL_NO_LABEL
567 || layout->labeltype == LABEL_TOP_ENVIRONMENT
568 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
569 || (layout->labeltype == LABEL_STATIC
570 && layout->latextype == LATEX_ENVIRONMENT
571 && !isFirstInSequence(pit, ownerParagraphs())))
572 && align == LYX_ALIGN_BLOCK
573 && !pit->params().noindent()
574 // in tabulars and ert paragraphs are never indented!
575 && (!pit->inInset() || !pit->inInset()->owner() ||
576 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
577 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
578 && (pit->layout() != tclass.defaultLayout() ||
579 bv()->buffer()->params().paragraph_separation ==
580 BufferParams::PARSEP_INDENT)) {
581 x += font_metrics::signedWidth(parindent,
582 tclass.defaultfont());
583 } else if (layout->labeltype == LABEL_BIBLIO) {
584 // ale970405 Right width for bibitems
585 x += bibitemMaxWidth(bv(), tclass.defaultfont());
593 int LyXText::rightMargin(Paragraph const & par,
594 Buffer const & buf, Row const &) const
596 LyXTextClass const & tclass = buf.params().getLyXTextClass();
597 LyXLayout_ptr const & layout = par.layout();
600 + font_metrics::signedWidth(tclass.rightmargin(),
601 tclass.defaultfont())
602 + font_metrics::signedWidth(layout->rightmargin,
603 tclass.defaultfont())
604 * 4 / (par.getDepth() + 4);
608 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
610 if (pit->layout()->margintype == MARGIN_MANUAL) {
612 tmprow.pos(pit->size());
613 // return the beginning of the body
614 return leftMargin(pit, tmprow);
617 // LabelEnd is only needed if the layout
618 // fills a flushleft label.
625 // this needs special handling - only newlines count as a break point
626 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
628 for (; i < par.size(); ++i)
629 if (par.isNewline(i))
638 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
639 Row const & row) const
641 // maximum pixel width of a row.
642 int width = workWidth() - rightMargin(*pit, *bv()->buffer(), row);
644 // inset->textWidth() returns -1 via workWidth(),
649 LyXLayout_ptr const & layout = pit->layout();
651 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
652 return addressBreakPoint(row.pos(), *pit);
654 pos_type const pos = row.pos();
655 pos_type const body_pos = pit->beginningOfBody();
656 pos_type const last = pit->size();
657 pos_type point = last;
662 // Now we iterate through until we reach the right margin
663 // or the end of the par, then choose the possible break
666 int const left = leftMargin(pit, row);
669 // pixel width since last breakpoint
674 // We re-use the font resolution for the entire font span when possible
675 LyXFont font = getFont(pit, i);
676 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
678 for (; i < last; ++i) {
679 if (pit->isNewline(i)) {
685 InsetOld * in = pit->getInset(i + 1);
686 if (in && in->display()) {
691 in = pit->getInset(i);
692 if (in && in->display()) {
698 char const c = pit->getChar(i);
699 if (i > endPosOfFontSpan) {
700 font = getFont(pit, i);
701 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
706 // add the auto-hfill from label end to the body
707 if (body_pos && i == body_pos) {
708 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
709 if (pit->isLineSeparator(i - 1))
710 thiswidth -= singleWidth(pit, i - 1);
711 int left_margin = labelEnd(pit, row);
712 if (thiswidth + x < left_margin)
713 thiswidth = left_margin - x;
714 thiswidth += singleWidth(pit, i, c, font);
716 thiswidth = singleWidth(pit, i, c, font);
720 //lyxerr << "i: " << i << " x: " << x << " width: " << width << endl;
721 chunkwidth += thiswidth;
723 // break before a character that will fall off
724 // the right of the row
726 // if no break before, break here
727 if (point == last || chunkwidth >= width - left) {
730 // exit on last registered breakpoint:
739 InsetOld * in = pit->getInset(i);
740 if (!in || in->isChar()) {
741 // some insets are line separators too
742 if (pit->isLineSeparator(i)) {
743 // register breakpoint:
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, Row & row, int paper_width) const
772 if (paper_width < 0) {
773 lyxerr << "paperwidth < 0: " << paper_width << " Why?" << endl;
778 // get the pure distance
779 pos_type const last = lastPos(*pit, row);
781 LyXLayout_ptr const & layout = pit->layout();
783 // special handling of the right address boxes
784 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
785 int const tmpfill = row.fill();
786 row.fill(0); // the minfill in leftMargin()
787 w = leftMargin(pit, row);
790 w = leftMargin(pit, row);
792 pos_type const body_pos = pit->beginningOfBody();
793 pos_type i = row.pos();
795 if (! pit->empty() && i <= last) {
796 // We re-use the font resolution for the entire span when possible
797 LyXFont font = getFont(pit, i);
798 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
800 if (body_pos > 0 && i == body_pos) {
801 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
802 if (pit->isLineSeparator(i - 1))
803 w -= singleWidth(pit, i - 1);
804 int left_margin = labelEnd(pit, row);
808 char const c = pit->getChar(i);
809 if (IsPrintable(c) && i > endPosOfFontSpan) {
810 // We need to get the next font
811 font = getFont(pit, i);
812 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
814 w += singleWidth(pit, i, c, font);
818 if (body_pos > 0 && body_pos > last) {
819 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
820 if (last >= 0 && pit->isLineSeparator(last))
821 w -= singleWidth(pit, last);
822 int const left_margin = labelEnd(pit, row);
827 int const fill = paper_width - w - rightMargin(*pit, *bv()->buffer(), row);
829 // If this case happens, it means that our calculation
830 // of the widths of the chars when we do rowBreakPoint()
831 // went wrong for some reason. Typically in list bodies.
832 // Things just about hobble on anyway, though you'll end
833 // up with a "fill_separator" less than zero, which corresponds
834 // to inter-word spacing being too small. Hopefully this problem
835 // will die when the label hacks die.
836 if (lyxerr.debugging() && fill < 0) {
837 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
838 << " w " << w << " paper_width " << paper_width
839 << " right margin " << rightMargin(*pit, *bv()->buffer(), row) << endl;
845 // returns the minimum space a manual label needs on the screen in pixel
846 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
848 pos_type last = pit->beginningOfBody();
850 BOOST_ASSERT(last > 0);
852 // -1 because a label ends either with a space that is in the label,
853 // or with the beginning of a footnote that is outside the label.
856 // a separator at this end does not count
857 if (pit->isLineSeparator(last))
861 pos_type i = row.pos();
863 w += singleWidth(pit, i);
868 string const & labwidstr = pit->params().labelWidthString();
869 if (!labwidstr.empty()) {
870 LyXFont const labfont = getLabelFont(pit);
871 int const labwidth = font_metrics::width(labwidstr, labfont);
872 fill = max(labwidth - w, 0);
879 LColor_color LyXText::backgroundColor() const
882 return inset_owner->backgroundColor();
884 return LColor::background;
888 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
890 // get the maximum ascent and the maximum descent
891 double layoutasc = 0;
892 double layoutdesc = 0;
895 // ok, let us initialize the maxasc and maxdesc value.
896 // Only the fontsize count. The other properties
897 // are taken from the layoutfont. Nicer on the screen :)
898 LyXLayout_ptr const & layout = pit->layout();
900 // as max get the first character of this row then it can increase but not
901 // decrease the height. Just some point to start with so we don't have to
902 // do the assignment below too often.
903 LyXFont font = getFont(pit, row.pos());
904 LyXFont::FONT_SIZE const tmpsize = font.size();
905 font = getLayoutFont(pit);
906 LyXFont::FONT_SIZE const size = font.size();
907 font.setSize(tmpsize);
909 LyXFont labelfont = getLabelFont(pit);
911 double spacing_val = 1.0;
912 if (!pit->params().spacing().isDefault())
913 spacing_val = pit->params().spacing().getValue();
915 spacing_val = bv()->buffer()->params().spacing().getValue();
916 //lyxerr << "spacing_val = " << spacing_val << endl;
918 int maxasc = int(font_metrics::maxAscent(font) *
919 layout->spacing.getValue() * spacing_val);
920 int maxdesc = int(font_metrics::maxDescent(font) *
921 layout->spacing.getValue() * spacing_val);
923 pos_type const pos_end = lastPos(*pit, row);
928 // We re-use the font resolution for the entire font span when possible
929 LyXFont font = getFont(pit, row.pos());
930 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(row.pos());
933 Paragraph const & par = *pit;
935 // Check if any insets are larger
936 for (pos_type pos = row.pos(); pos <= pos_end; ++pos) {
937 // Manual inlined optimised version of common case of
938 // "maxwidth += singleWidth(pit, pos);"
939 char const c = par.getChar(pos);
941 if (IsPrintable(c)) {
942 if (pos > endPosOfFontSpan) {
943 // We need to get the next font
944 font = getFont(pit, pos);
945 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
947 if (! font.language()->RightToLeft()) {
948 maxwidth += font_metrics::width(c, font);
950 // Fall-back to normal case
951 maxwidth += singleWidth(pit, pos, c, font);
952 // And flush font cache
953 endPosOfFontSpan = 0;
956 // Special handling of insets - are any larger?
957 if (par.isInset(pos)) {
958 InsetOld const * tmpinset = par.getInset(pos);
960 maxwidth += tmpinset->width();
961 maxasc = max(maxasc, tmpinset->ascent());
962 maxdesc = max(maxdesc, tmpinset->descent());
965 // Fall-back to normal case
966 maxwidth += singleWidth(pit, pos, c, font);
967 // And flush font cache
968 endPosOfFontSpan = 0;
974 // Check if any custom fonts are larger (Asger)
975 // This is not completely correct, but we can live with the small,
976 // cosmetic error for now.
977 LyXFont::FONT_SIZE maxsize =
978 pit->highestFontInRange(row.pos(), pos_end, size);
979 if (maxsize > font.size()) {
980 font.setSize(maxsize);
981 maxasc = max(maxasc, font_metrics::maxAscent(font));
982 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
985 // This is nicer with box insets:
989 row.ascent_of_text(maxasc);
993 BufferParams const & bufparams = bv()->buffer()->params();
994 // some parksips VERY EASY IMPLEMENTATION
995 if (bv()->buffer()->params().paragraph_separation ==
996 BufferParams::PARSEP_SKIP)
998 if (layout->isParagraph()
999 && pit->getDepth() == 0
1000 && pit != ownerParagraphs().begin())
1002 maxasc += bufparams.getDefSkip().inPixels(*bv());
1003 } else if (pit != ownerParagraphs().begin() &&
1004 boost::prior(pit)->layout()->isParagraph() &&
1005 boost::prior(pit)->getDepth() == 0)
1007 // is it right to use defskip here too? (AS)
1008 maxasc += bufparams.getDefSkip().inPixels(*bv());
1013 if (pit == ownerParagraphs().begin() && !isInInset())
1014 maxasc += PAPER_MARGIN;
1016 // add the vertical spaces, that the user added
1017 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1019 // do not forget the DTP-lines!
1020 // there height depends on the font of the nearest character
1021 if (pit->params().lineTop())
1023 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1024 // and now the pagebreaks
1025 if (pit->params().pagebreakTop())
1026 maxasc += 3 * defaultRowHeight();
1028 if (pit->params().startOfAppendix())
1029 maxasc += 3 * defaultRowHeight();
1031 // This is special code for the chapter, since the label of this
1032 // layout is printed in an extra row
1033 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
1034 float spacing_val = 1.0;
1035 if (!pit->params().spacing().isDefault()) {
1036 spacing_val = pit->params().spacing().getValue();
1038 spacing_val = bufparams.spacing().getValue();
1041 labeladdon = int(font_metrics::maxDescent(labelfont) *
1042 layout->spacing.getValue() *
1044 + int(font_metrics::maxAscent(labelfont) *
1045 layout->spacing.getValue() *
1049 // special code for the top label
1050 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1051 || layout->labeltype == LABEL_BIBLIO
1052 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1053 && isFirstInSequence(pit, ownerParagraphs())
1054 && !pit->getLabelstring().empty())
1056 float spacing_val = 1.0;
1057 if (!pit->params().spacing().isDefault()) {
1058 spacing_val = pit->params().spacing().getValue();
1060 spacing_val = bufparams.spacing().getValue();
1064 (font_metrics::maxAscent(labelfont) +
1065 font_metrics::maxDescent(labelfont)) *
1066 layout->spacing.getValue() *
1068 + layout->topsep * defaultRowHeight()
1069 + layout->labelbottomsep * defaultRowHeight());
1072 // And now the layout spaces, for example before and after
1073 // a section, or between the items of a itemize or enumerate
1076 if (!pit->params().pagebreakTop()) {
1077 ParagraphList::iterator prev =
1078 depthHook(pit, ownerParagraphs(),
1080 if (prev != pit && prev->layout() == layout &&
1081 prev->getDepth() == pit->getDepth() &&
1082 prev->getLabelWidthString() == pit->getLabelWidthString())
1084 layoutasc = (layout->itemsep * defaultRowHeight());
1085 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
1086 tmptop = layout->topsep;
1089 layoutasc = (tmptop * defaultRowHeight());
1090 } else if (pit->params().lineTop()) {
1091 tmptop = layout->topsep;
1094 layoutasc = (tmptop * defaultRowHeight());
1097 prev = outerHook(pit, ownerParagraphs());
1098 if (prev != ownerParagraphs().end()) {
1099 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1100 } else if (pit != ownerParagraphs().begin()) {
1101 ParagraphList::iterator prior_pit = boost::prior(pit);
1102 if (prior_pit->getDepth() != 0 ||
1103 prior_pit->layout() == layout) {
1104 maxasc += int(layout->parsep * defaultRowHeight());
1110 // is it a bottom line?
1111 if (row.endpos() >= pit->size()) {
1112 // the bottom margin
1113 ParagraphList::iterator nextpit = boost::next(pit);
1114 if (nextpit == ownerParagraphs().end() && !isInInset())
1115 maxdesc += PAPER_MARGIN;
1117 // add the vertical spaces, that the user added
1118 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1120 // do not forget the DTP-lines!
1121 // there height depends on the font of the nearest character
1122 if (pit->params().lineBottom())
1123 maxdesc += 2 * font_metrics::ascent('x',
1124 getFont(pit, max(pos_type(0), pit->size() - 1)));
1126 // and now the pagebreaks
1127 if (pit->params().pagebreakBottom())
1128 maxdesc += 3 * defaultRowHeight();
1130 // and now the layout spaces, for example before and after
1131 // a section, or between the items of a itemize or enumerate
1133 if (!pit->params().pagebreakBottom()
1134 && nextpit != ownerParagraphs().end()) {
1135 ParagraphList::iterator comparepit = pit;
1139 if (comparepit->getDepth() > nextpit->getDepth()) {
1140 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1141 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1142 if (comparepit->layout()!= nextpit->layout()
1143 || nextpit->getLabelWidthString() !=
1144 comparepit->getLabelWidthString())
1146 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1148 if (unusual > usual)
1149 layoutdesc = unusual;
1152 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1154 if (comparepit->layout() != nextpit->layout()
1155 || nextpit->getLabelWidthString() !=
1156 comparepit->getLabelWidthString())
1157 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1162 // incalculate the layout spaces
1163 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1164 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1166 row.height(maxasc + maxdesc + labeladdon);
1167 row.baseline(maxasc + labeladdon);
1168 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1170 row.width(maxwidth);
1173 width = max(0, workWidth());
1174 ParagraphList::iterator pit = ownerParagraphs().begin();
1175 ParagraphList::iterator end = ownerParagraphs().end();
1176 for ( ; pit != end; ++pit) {
1177 if (width < pit->width)
1184 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1186 // allow only if at start or end, or all previous is new text
1187 if (cursor.pos() && cursor.pos() != cursorPar()->size()
1188 && cursorPar()->isChangeEdited(0, cursor.pos()))
1191 LyXTextClass const & tclass =
1192 bv()->buffer()->params().getLyXTextClass();
1193 LyXLayout_ptr const & layout = cursorPar()->layout();
1195 // this is only allowed, if the current paragraph is not empty or caption
1196 // and if it has not the keepempty flag active
1197 if (cursorPar()->empty() && !cursorPar()->allowEmpty()
1198 && layout->labeltype != LABEL_SENSITIVE)
1201 recUndo(cursor.par());
1203 // Always break behind a space
1205 // It is better to erase the space (Dekel)
1206 if (cursor.pos() < cursorPar()->size()
1207 && cursorPar()->isLineSeparator(cursor.pos()))
1208 cursorPar()->erase(cursor.pos());
1210 // break the paragraph
1214 keep_layout = layout->isEnvironment();
1216 // we need to set this before we insert the paragraph. IMO the
1217 // breakParagraph call should return a bool if it inserts the
1218 // paragraph before or behind and we should react on that one
1219 // but we can fix this in 1.3.0 (Jug 20020509)
1220 bool const isempty = (cursorPar()->allowEmpty() && cursorPar()->empty());
1221 ::breakParagraph(bv()->buffer()->params(), paragraphs, cursorPar(),
1222 cursor.pos(), keep_layout);
1224 #warning Trouble Point! (Lgb)
1225 // When ::breakParagraph is called from within an inset we must
1226 // ensure that the correct ParagraphList is used. Today that is not
1227 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1228 ParagraphList::iterator next_par = boost::next(cursorPar());
1230 // well this is the caption hack since one caption is really enough
1231 if (layout->labeltype == LABEL_SENSITIVE) {
1233 // set to standard-layout
1234 cursorPar()->applyLayout(tclass.defaultLayout());
1236 // set to standard-layout
1237 next_par->applyLayout(tclass.defaultLayout());
1240 // if the cursor is at the beginning of a row without prior newline,
1242 // This touches only the screen-update. Otherwise we would may have
1243 // an empty row on the screen
1244 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1245 && !cursorPar()->isNewline(cursor.pos() - 1))
1250 while (!next_par->empty() && next_par->isNewline(0))
1254 redoParagraph(cursorPar());
1255 redoParagraph(next_par);
1257 // This check is necessary. Otherwise the new empty paragraph will
1258 // be deleted automatically. And it is more friendly for the user!
1259 if (cursor.pos() || isempty)
1260 setCursor(next_par, 0);
1262 setCursor(cursorPar(), 0);
1266 // convenience function
1267 void LyXText::redoParagraph()
1270 redoParagraph(cursorPar());
1271 setCursorIntern(cursor.par(), cursor.pos());
1275 // insert a character, moves all the following breaks in the
1276 // same Paragraph one to the right and make a rebreak
1277 void LyXText::insertChar(char c)
1279 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
1281 // When the free-spacing option is set for the current layout,
1282 // disable the double-space checking
1284 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1285 cursorPar()->isFreeSpacing();
1287 if (lyxrc.auto_number) {
1288 static string const number_operators = "+-/*";
1289 static string const number_unary_operators = "+-";
1290 static string const number_seperators = ".,:";
1292 if (current_font.number() == LyXFont::ON) {
1293 if (!IsDigit(c) && !contains(number_operators, c) &&
1294 !(contains(number_seperators, c) &&
1295 cursor.pos() >= 1 &&
1296 cursor.pos() < cursorPar()->size() &&
1297 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1298 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1300 number(bv()); // Set current_font.number to OFF
1301 } else if (IsDigit(c) &&
1302 real_current_font.isVisibleRightToLeft()) {
1303 number(bv()); // Set current_font.number to ON
1305 if (cursor.pos() > 0) {
1306 char const c = cursorPar()->getChar(cursor.pos() - 1);
1307 if (contains(number_unary_operators, c) &&
1308 (cursor.pos() == 1 ||
1309 cursorPar()->isSeparator(cursor.pos() - 2) ||
1310 cursorPar()->isNewline(cursor.pos() - 2))
1316 } else if (contains(number_seperators, c) &&
1317 cursor.pos() >= 2 &&
1320 cursor.pos() - 2).number() == LyXFont::ON) {
1331 // First check, if there will be two blanks together or a blank at
1332 // the beginning of a paragraph.
1333 // I decided to handle blanks like normal characters, the main
1334 // difference are the special checks when calculating the row.fill
1335 // (blank does not count at the end of a row) and the check here
1337 // The bug is triggered when we type in a description environment:
1338 // The current_font is not changed when we go from label to main text
1339 // and it should (along with realtmpfont) when we type the space.
1340 // CHECK There is a bug here! (Asger)
1342 // store the current font. This is because of the use of cursor
1343 // movements. The moving cursor would refresh the current font
1344 LyXFont realtmpfont = real_current_font;
1345 LyXFont rawtmpfont = current_font;
1347 if (!freeSpacing && IsLineSeparatorChar(c)) {
1348 if ((cursor.pos() > 0
1349 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1350 || (cursor.pos() > 0
1351 && cursorPar()->isNewline(cursor.pos() - 1))
1352 || (cursor.pos() == 0)) {
1353 static bool sent_space_message = false;
1354 if (!sent_space_message) {
1355 if (cursor.pos() == 0)
1356 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1358 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1359 sent_space_message = true;
1366 // Here case LyXText::InsertInset already inserted the character
1367 if (c != Paragraph::META_INSET)
1368 cursorPar()->insertChar(cursor.pos(), c);
1370 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1372 current_font = rawtmpfont;
1373 real_current_font = realtmpfont;
1374 redoParagraph(cursorPar());
1375 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1381 void LyXText::charInserted()
1383 // Here we could call finishUndo for every 20 characters inserted.
1384 // This is from my experience how emacs does it. (Lgb)
1385 static unsigned int counter;
1395 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
1397 double w = row.fill();
1398 double fill_hfill = 0;
1399 double fill_label_hfill = 0;
1400 double fill_separator = 0;
1404 pit->isRightToLeftPar(bv()->buffer()->params());
1406 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer(), row) : 0;
1408 x = workWidth() > 0 ? leftMargin(pit, row) : 0;
1410 // is there a manual margin with a manual label
1411 LyXLayout_ptr const & layout = pit->layout();
1413 if (layout->margintype == MARGIN_MANUAL
1414 && layout->labeltype == LABEL_MANUAL) {
1415 /// We might have real hfills in the label part
1416 int nlh = numberOfLabelHfills(*pit, row);
1418 // A manual label par (e.g. List) has an auto-hfill
1419 // between the label text and the body of the
1421 // But we don't want to do this auto hfill if the par
1426 if (nlh && !pit->getLabelWidthString().empty()) {
1427 fill_label_hfill = labelFill(pit, row) / double(nlh);
1431 // are there any hfills in the row?
1432 int const nh = numberOfHfills(*pit, row);
1436 fill_hfill = w / nh;
1437 // we don't have to look at the alignment if it is ALIGN_LEFT and
1438 // if the row is already larger then the permitted width as then
1439 // we force the LEFT_ALIGN'edness!
1440 } else if (int(row.width()) < workWidth()) {
1441 // is it block, flushleft or flushright?
1442 // set x how you need it
1444 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1445 align = layout->align;
1447 align = pit->params().align();
1449 InsetOld * inset = 0;
1450 // ERT insets should always be LEFT ALIGNED on screen
1451 inset = pit->inInset();
1452 if (inset && inset->owner() &&
1453 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1455 align = LYX_ALIGN_LEFT;
1458 // Display-style insets should always be on a centred row
1459 // The test on pit->size() is to catch zero-size pars, which
1460 // would trigger the assert in Paragraph::getInset().
1461 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1462 inset = pit->isInset(row.pos()) ? pit->getInset(row.pos()) : 0;
1463 if (inset && inset->display()) {
1464 align = LYX_ALIGN_CENTER;
1468 case LYX_ALIGN_BLOCK:
1470 int const ns = numberOfSeparators(*pit, row);
1471 bool disp_inset = false;
1472 if (row.endpos() < pit->size()) {
1473 InsetOld * in = pit->getInset(row.endpos());
1475 disp_inset = in->display();
1477 // If we have separators, this is not the last row of a
1478 // par, does not end in newline, and is not row above a
1479 // display inset... then stretch it
1481 && row.endpos() < pit->size()
1482 && !pit->isNewline(row.endpos())
1485 fill_separator = w / ns;
1486 } else if (is_rtl) {
1491 case LYX_ALIGN_RIGHT:
1494 case LYX_ALIGN_CENTER:
1500 computeBidiTables(*pit, *bv()->buffer(), row);
1502 pos_type body_pos = pit->beginningOfBody();
1503 pos_type last = lastPos(*pit, row);
1506 (body_pos - 1 > last ||
1507 !pit->isLineSeparator(body_pos - 1))) {
1508 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1509 if (body_pos - 1 <= last)
1510 x += fill_label_hfill;
1514 row.fill_hfill(fill_hfill);
1515 row.fill_label_hfill(fill_label_hfill);
1516 row.fill_separator(fill_separator);
1521 // important for the screen
1524 // the cursor set functions have a special mechanism. When they
1525 // realize, that you left an empty paragraph, they will delete it.
1526 // They also delete the corresponding row
1528 void LyXText::cursorRightOneWord()
1530 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1531 setCursor(cursorPar(), cursor.pos());
1535 // Skip initial whitespace at end of word and move cursor to *start*
1536 // of prior word, not to end of next prior word.
1537 void LyXText::cursorLeftOneWord()
1539 LyXCursor tmpcursor = cursor;
1540 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1541 setCursor(getPar(tmpcursor), tmpcursor.pos());
1545 void LyXText::selectWord(word_location loc)
1547 LyXCursor from = cursor;
1548 LyXCursor to = cursor;
1549 ::getWord(*this, from, to, loc, ownerParagraphs());
1551 setCursor(from.par(), from.pos());
1554 selection.cursor = cursor;
1555 setCursor(to.par(), to.pos());
1560 // Select the word currently under the cursor when no
1561 // selection is currently set
1562 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1564 if (!selection.set()) {
1566 return selection.set();
1572 void LyXText::acceptChange()
1574 if (!selection.set() && cursorPar()->size())
1577 if (selection.start.par() == selection.end.par()) {
1578 LyXCursor & startc = selection.start;
1579 LyXCursor & endc = selection.end;
1580 recordUndo(Undo::INSERT, this, startc.par());
1581 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1584 redoParagraph(getPar(startc));
1585 setCursorIntern(startc.par(), 0);
1587 #warning handle multi par selection
1591 void LyXText::rejectChange()
1593 if (!selection.set() && cursorPar()->size())
1596 if (selection.start.par() == selection.end.par()) {
1597 LyXCursor & startc = selection.start;
1598 LyXCursor & endc = selection.end;
1599 recordUndo(Undo::INSERT, this, startc.par());
1600 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1603 redoParagraph(getPar(startc));
1604 setCursorIntern(startc.par(), 0);
1606 #warning handle multi par selection
1610 // This function is only used by the spellchecker for NextWord().
1611 // It doesn't handle LYX_ACCENTs and probably never will.
1612 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1614 if (the_locking_inset) {
1615 WordLangTuple word =
1616 the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1617 if (!word.word().empty()) {
1618 value += float(cursor.y());
1619 value /= float(height);
1622 // we have to go on checking so move cursor to the next char
1623 if (cursor.pos() == cursorPar()->size()) {
1624 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1626 cursor.par(cursor.par() + 1);
1629 cursor.pos(cursor.pos() + 1);
1632 int const tmppar = cursor.par();
1634 // If this is not the very first word, skip rest of
1635 // current word because we are probably in the middle
1636 // of a word if there is text here.
1637 if (cursor.pos() || cursor.par() != 0) {
1638 while (cursor.pos() < cursorPar()->size()
1639 && cursorPar()->isLetter(cursor.pos()))
1640 cursor.pos(cursor.pos() + 1);
1643 // Now, skip until we have real text (will jump paragraphs)
1645 ParagraphList::iterator cpit = cursorPar();
1646 pos_type const cpos = cursor.pos();
1648 if (cpos == cpit->size()) {
1649 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1650 cursor.par(cursor.par() + 1);
1657 bool const is_good_inset = cpit->isInset(cpos)
1658 && cpit->getInset(cpos)->allowSpellcheck();
1660 if (!isDeletedText(*cpit, cpos)
1661 && (is_good_inset || cpit->isLetter(cpos)))
1664 cursor.pos(cpos + 1);
1667 // now check if we hit an inset so it has to be a inset containing text!
1668 if (cursor.pos() < cursorPar()->size() &&
1669 cursorPar()->isInset(cursor.pos())) {
1671 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1672 cursorPar()->getInset(cursor.pos())->dispatch(cmd);
1673 // now call us again to do the above trick
1674 // but obviously we have to start from down below ;)
1675 return bv()->text->selectNextWordToSpellcheck(value);
1678 // Update the value if we changed paragraphs
1679 if (cursor.par() != tmppar) {
1680 setCursor(cursor.par(), cursor.pos());
1681 value = float(cursor.y())/float(height);
1684 // Start the selection from here
1685 selection.cursor = cursor;
1687 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1688 // and find the end of the word (insets like optional hyphens
1689 // and ligature break are part of a word)
1690 while (cursor.pos() < cursorPar()->size()
1691 && cursorPar()->isLetter(cursor.pos())
1692 && !isDeletedText(*cursorPar(), cursor.pos()))
1693 cursor.pos(cursor.pos() + 1);
1695 // Finally, we copy the word to a string and return it
1697 if (selection.cursor.pos() < cursor.pos()) {
1698 for (pos_type i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1699 if (!cursorPar()->isInset(i))
1700 str += cursorPar()->getChar(i);
1703 return WordLangTuple(str, lang_code);
1707 // This one is also only for the spellchecker
1708 void LyXText::selectSelectedWord()
1710 if (the_locking_inset) {
1711 the_locking_inset->selectSelectedWord(bv());
1714 // move cursor to the beginning
1715 setCursor(selection.cursor.par(), selection.cursor.pos());
1717 // set the sel cursor
1718 selection.cursor = cursor;
1720 // now find the end of the word
1721 while (cursor.pos() < cursorPar()->size()
1722 && cursorPar()->isLetter(cursor.pos()))
1723 cursor.pos(cursor.pos() + 1);
1725 setCursor(cursorPar(), cursor.pos());
1727 // finally set the selection
1732 // Delete from cursor up to the end of the current or next word.
1733 void LyXText::deleteWordForward()
1735 if (cursorPar()->empty())
1738 LyXCursor tmpcursor = cursor;
1739 selection.set(true); // to avoid deletion
1740 cursorRightOneWord();
1741 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1742 selection.cursor = cursor;
1745 cutSelection(true, false);
1750 // Delete from cursor to start of current or prior word.
1751 void LyXText::deleteWordBackward()
1753 if (cursorPar()->empty())
1756 LyXCursor tmpcursor = cursor;
1757 selection.set(true); // to avoid deletion
1758 cursorLeftOneWord();
1759 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1760 selection.cursor = cursor;
1763 cutSelection(true, false);
1768 // Kill to end of line.
1769 void LyXText::deleteLineForward()
1771 if (cursorPar()->empty())
1772 // Paragraph is empty, so we just go to the right
1775 LyXCursor tmpcursor = cursor;
1776 // We can't store the row over a regular setCursor
1777 // so we set it to 0 and reset it afterwards.
1778 selection.set(true); // to avoid deletion
1780 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1781 selection.cursor = cursor;
1784 // What is this test for ??? (JMarc)
1785 if (!selection.set()) {
1786 deleteWordForward();
1788 cutSelection(true, false);
1794 void LyXText::changeCase(LyXText::TextCase action)
1799 if (selection.set()) {
1800 from = selection.start;
1804 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1805 setCursor(to.par(), to.pos() + 1);
1808 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1810 pos_type pos = from.pos();
1811 int par = from.par();
1813 while (par != int(ownerParagraphs().size()) &&
1814 (pos != to.pos() || par != to.par())) {
1815 ParagraphList::iterator pit = getPar(par);
1816 if (pos == pit->size()) {
1821 unsigned char c = pit->getChar(pos);
1822 if (c != Paragraph::META_INSET) {
1824 case text_lowercase:
1827 case text_capitalization:
1829 action = text_lowercase;
1831 case text_uppercase:
1837 pit->setChar(pos, c);
1843 void LyXText::Delete()
1845 // this is a very easy implementation
1847 LyXCursor old_cursor = cursor;
1848 int const old_cur_par_id = cursorPar()->id();
1849 int const old_cur_par_prev_id =
1850 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1852 // just move to the right
1855 // CHECK Look at the comment here.
1856 // This check is not very good...
1857 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1858 // and that can very well delete the par or par->previous in
1859 // old_cursor. Will a solution where we compare paragraph id's
1861 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1862 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1863 // delete-empty-paragraph-mechanism has done it
1867 // if you had success make a backspace
1868 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1869 recordUndo(Undo::DELETE, this, old_cursor.par());
1875 void LyXText::backspace()
1877 // Get the font that is used to calculate the baselineskip
1878 ParagraphList::iterator pit = cursorPar();
1879 pos_type lastpos = pit->size();
1881 if (cursor.pos() == 0) {
1882 // The cursor is at the beginning of a paragraph,
1883 // so the the backspace will collapse two paragraphs into one.
1885 // but it's not allowed unless it's new
1886 if (pit->isChangeEdited(0, pit->size()))
1889 // we may paste some paragraphs
1891 // is it an empty paragraph?
1893 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1894 // This is an empty paragraph and we delete it just
1895 // by moving the cursor one step
1896 // left and let the DeleteEmptyParagraphMechanism
1897 // handle the actual deletion of the paragraph.
1900 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1901 if (cursorPar()->layout() == tmppit->layout()
1902 && cursorPar()->getAlign() == tmppit->getAlign()) {
1903 // Inherit bottom DTD from the paragraph below.
1904 // (the one we are deleting)
1905 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1906 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1907 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1912 // the layout things can change the height of a row !
1918 if (cursor.par() != 0)
1919 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1921 ParagraphList::iterator tmppit = cursorPar();
1922 // We used to do cursorLeftIntern() here, but it is
1923 // not a good idea since it triggers the auto-delete
1924 // mechanism. So we do a cursorLeftIntern()-lite,
1925 // without the dreaded mechanism. (JMarc)
1926 if (cursor.par() != 0) {
1927 // steps into the above paragraph.
1928 setCursorIntern(cursor.par() - 1,
1929 getPar(cursor.par() - 1)->size(),
1933 // Pasting is not allowed, if the paragraphs have different
1934 // layout. I think it is a real bug of all other
1935 // word processors to allow it. It confuses the user.
1936 // Correction: Pasting is always allowed with standard-layout
1937 Buffer & buf = *bv()->buffer();
1938 BufferParams const & bufparams = buf.params();
1939 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1941 if (cursorPar() != tmppit
1942 && (cursorPar()->layout() == tmppit->layout()
1943 || tmppit->layout() == tclass.defaultLayout())
1944 && cursorPar()->getAlign() == tmppit->getAlign()) {
1945 mergeParagraph(bufparams,
1946 buf.paragraphs(), cursorPar());
1948 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1949 cursor.pos(cursor.pos() - 1);
1951 // the row may have changed, block, hfills etc.
1953 setCursor(cursor.par(), cursor.pos(), false);
1956 // this is the code for a normal backspace, not pasting
1958 recordUndo(Undo::DELETE, this, cursor.par());
1959 // We used to do cursorLeftIntern() here, but it is
1960 // not a good idea since it triggers the auto-delete
1961 // mechanism. So we do a cursorLeftIntern()-lite,
1962 // without the dreaded mechanism. (JMarc)
1963 setCursorIntern(cursor.par(), cursor.pos() - 1,
1964 false, cursor.boundary());
1965 cursorPar()->erase(cursor.pos());
1968 lastpos = cursorPar()->size();
1969 if (cursor.pos() == lastpos)
1973 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1977 ParagraphList::iterator LyXText::cursorPar() const
1979 return getPar(cursor.par());
1983 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1985 return getPar(cur.par());
1989 ParagraphList::iterator LyXText::getPar(int par) const
1991 BOOST_ASSERT(par >= 0);
1992 BOOST_ASSERT(par < int(ownerParagraphs().size()));
1993 ParagraphList::iterator pit = ownerParagraphs().begin();
1994 std::advance(pit, par);
2000 RowList::iterator LyXText::cursorRow() const
2002 return getRow(*cursorPar(), cursor.pos());
2006 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2008 return getRow(*getPar(cur), cur.pos());
2012 RowList::iterator LyXText::getRow(Paragraph & par, pos_type pos) const
2014 RowList::iterator rit = boost::prior(par.rows.end());
2015 RowList::iterator const begin = par.rows.begin();
2017 while (rit != begin && rit->pos() > pos)
2025 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
2027 //lyxerr << "getRowNearY: y " << y << endl;
2029 pit = boost::prior(ownerParagraphs().end());
2031 RowList::iterator rit = lastRow();
2032 RowList::iterator rbegin = firstRow();
2034 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
2035 previousRow(pit, rit);
2041 int LyXText::getDepth() const
2043 return cursorPar()->getDepth();
2047 RowList::iterator LyXText::firstRow() const
2049 return ownerParagraphs().front().rows.begin();
2053 RowList::iterator LyXText::lastRow() const
2055 return boost::prior(endRow());
2059 RowList::iterator LyXText::endRow() const
2061 return ownerParagraphs().back().rows.end();
2065 void LyXText::nextRow(ParagraphList::iterator & pit,
2066 RowList::iterator & rit) const
2069 if (rit == pit->rows.end()) {
2071 if (pit == ownerParagraphs().end())
2074 rit = pit->rows.begin();
2079 void LyXText::previousRow(ParagraphList::iterator & pit,
2080 RowList::iterator & rit) const
2082 if (rit != pit->rows.begin())
2085 BOOST_ASSERT(pit != ownerParagraphs().begin());
2087 rit = boost::prior(pit->rows.end());
2092 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
2094 if (!selection.set())
2097 // should be const ...
2098 ParagraphList::iterator startpit = getPar(selection.start);
2099 ParagraphList::iterator endpit = getPar(selection.end);
2100 size_t const startpos = selection.start.pos();
2101 size_t const endpos = selection.end.pos();
2103 if (startpit == endpit)
2104 return startpit->asString(buffer, startpos, endpos, label);
2106 // First paragraph in selection
2108 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
2110 // The paragraphs in between (if any)
2111 ParagraphList::iterator pit = startpit;
2112 for (++pit; pit != endpit; ++pit)
2113 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
2115 // Last paragraph in selection
2116 result += endpit->asString(buffer, 0, endpos, label);
2122 int LyXText::parOffset(ParagraphList::iterator pit) const
2124 return std::distance(ownerParagraphs().begin(), pit);
2128 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
2130 // remove rows of paragraph, keep track of height changes
2131 height -= pit->height;
2139 InsetList::iterator ii = pit->insetlist.begin();
2140 InsetList::iterator iend = pit->insetlist.end();
2141 for (; ii != iend; ++ii) {
2143 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
2144 ii->inset->metrics(mi, dim);
2147 // rebreak the paragraph
2148 int const ww = workWidth();
2149 for (pos_type z = 0; z < pit->size() + 1; ) {
2151 z = rowBreakPoint(pit, row) + 1;
2153 int const f = fill(pit, row, ww);
2154 unsigned int const w = ww - f;
2155 pit->width = std::max(pit->width, w);
2158 prepareToPrint(pit, row);
2159 setHeightOfRow(pit, row);
2160 row.y_offset(pit->height);
2161 pit->height += row.height();
2162 pit->rows.push_back(row);
2164 height += pit->height;
2165 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
2169 void LyXText::redoParagraphs(ParagraphList::iterator pit,
2170 ParagraphList::iterator end)
2172 for ( ; pit != end; ++pit)
2173 redoParagraphInternal(pit);
2174 updateRowPositions();
2178 void LyXText::redoParagraph(ParagraphList::iterator pit)
2180 redoParagraphInternal(pit);
2181 updateRowPositions();
2185 void LyXText::fullRebreak()
2187 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2189 selection.cursor = cursor;
2193 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
2195 lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
2196 << " workWidth: " << workWidth() << "\nfont: " << mi.base.font << endl;
2197 //BOOST_ASSERT(mi.base.textwidth);
2199 // rebuild row cache
2201 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2204 ParagraphList::iterator pit = ownerParagraphs().begin();
2205 ParagraphList::iterator end = ownerParagraphs().end();
2206 for ( ; pit != end; ++pit)
2207 width = std::max(pit->width, width);
2210 dim.asc = firstRow()->ascent_of_text();
2211 dim.des = height - dim.asc;
2212 dim.wid = std::max(mi.base.textwidth, int(width));
2216 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
2218 return row.endpos() >= pit->size()
2219 && boost::next(pit) == ownerParagraphs().end();
2223 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
2225 return row.pos() == 0 && pit == ownerParagraphs().begin();