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 &);
80 unsigned int maxParagraphWidth(ParagraphList const & plist)
82 unsigned int width = 0;
83 ParagraphList::const_iterator pit = plist.begin();
84 ParagraphList::const_iterator end = plist.end();
85 for (; pit != end; ++pit)
86 width = std::max(width, pit->width);
93 BufferView * LyXText::bv()
95 BOOST_ASSERT(bv_owner != 0);
100 BufferView * LyXText::bv() const
102 BOOST_ASSERT(bv_owner != 0);
107 void LyXText::updateRowPositions()
109 ParagraphList::iterator pit = ownerParagraphs().begin();
110 ParagraphList::iterator end = ownerParagraphs().end();
111 for (height = 0; pit != end; ++pit) {
113 height += pit->height;
118 int LyXText::workWidth() const
120 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
124 int LyXText::getRealCursorX() const
127 if (the_locking_inset && the_locking_inset->getLyXText(bv()) != this)
128 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
133 #warning FIXME This function seems to belong outside of LyxText.
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
137 if (!Encodings::is_arabic(c))
138 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139 return c + (0xb0 - '0');
143 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
144 unsigned char next_char = ' ';
146 pos_type const par_size = par.size();
148 for (pos_type i = pos + 1; i < par_size; ++i) {
149 unsigned char const par_char = par.getChar(i);
150 if (!Encodings::IsComposeChar_arabic(par_char)) {
151 next_char = par_char;
156 if (Encodings::is_arabic(next_char)) {
157 if (Encodings::is_arabic(prev_char) &&
158 !Encodings::is_arabic_special(prev_char))
159 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
161 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
163 if (Encodings::is_arabic(prev_char) &&
164 !Encodings::is_arabic_special(prev_char))
165 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
167 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
171 // This is the comments that some of the warnings below refers to.
172 // There are some issues in this file and I don't think they are
173 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
174 // this is a problem that has been here almost from day one and that a
175 // larger userbase with different access patters triggers the bad
176 // behaviour. (segfaults.) What I think happen is: In several places
177 // we store the paragraph in the current cursor and then moves the
178 // cursor. This movement of the cursor will delete paragraph at the
179 // old position if it is now empty. This will make the temporary
180 // pointer to the old cursor paragraph invalid and dangerous to use.
181 // And is some cases this will trigger a segfault. I have marked some
182 // of the cases where this happens with a warning, but I am sure there
183 // are others in this file and in text2.C. There is also a note in
184 // Delete() that you should read. In Delete I store the paragraph->id
185 // instead of a pointer to the paragraph. I am pretty sure this faulty
186 // use of temporary pointers to paragraphs that might have gotten
187 // invalidated (through a cursor movement) before they are used, are
188 // the cause of the strange crashes we get reported often.
190 // It is very tiresom to change this code, especially when it is as
191 // hard to read as it is. Help to fix all the cases where this is done
192 // would be greately appreciated.
196 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
198 if (pos >= pit->size())
201 char const c = pit->getChar(pos);
202 LyXFont const & font = getFont(pit, pos);
203 return singleWidth(pit, pos, c, font);
207 int LyXText::singleWidth(ParagraphList::iterator pit,
208 pos_type pos, char c, LyXFont const & font) const
210 if (pos >= pit->size()) {
211 lyxerr << "in singleWidth(), pos: " << pos << endl;
217 // The most common case is handled first (Asger)
218 if (IsPrintable(c)) {
219 if (!font.language()->RightToLeft()) {
220 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
221 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
222 && font.language()->lang() == "arabic") {
223 if (Encodings::IsComposeChar_arabic(c))
226 c = transformChar(c, *pit, pos);
227 } else if (font.language()->lang() == "hebrew" &&
228 Encodings::IsComposeChar_hebrew(c))
231 return font_metrics::width(c, font);
234 if (c == Paragraph::META_INSET) {
235 InsetOld * tmpinset = pit->getInset(pos);
236 BOOST_ASSERT(tmpinset);
237 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
238 // Because of the representation as vertical lines
241 return tmpinset->width();
244 if (IsSeparatorChar(c))
246 return font_metrics::width(c, font);
250 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
252 return (bidi_start == -1) ? pos : log2vis_list[pos - bidi_start];
256 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
258 return (bidi_start == -1) ? pos : vis2log_list[pos - bidi_start];
262 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
264 return (bidi_start == -1) ? 0 : bidi_levels[pos - bidi_start];
268 bool LyXText::bidi_InRange(lyx::pos_type pos) const
270 return bidi_start == -1 || (bidi_start <= pos && pos <= bidi_end);
274 void LyXText::computeBidiTables(Paragraph const & par,
275 Buffer const & buf, Row & row) const
277 bidi_same_direction = true;
278 if (!lyxrc.rtl_support) {
283 InsetOld * inset = par.inInset();
284 if (inset && inset->owner() &&
285 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
290 bidi_start = row.pos();
291 bidi_end = lastPos(par, row);
293 if (bidi_start > bidi_end) {
298 if (bidi_end + 2 - bidi_start >
299 static_cast<pos_type>(log2vis_list.size())) {
301 (bidi_end + 2 - bidi_start < 500) ?
302 500 : 2 * (bidi_end + 2 - bidi_start);
303 log2vis_list.resize(new_size);
304 vis2log_list.resize(new_size);
305 bidi_levels.resize(new_size);
308 vis2log_list[bidi_end + 1 - bidi_start] = -1;
309 log2vis_list[bidi_end + 1 - bidi_start] = -1;
311 BufferParams const & bufparams = buf.params();
313 bool const rtl_par = par.isRightToLeftPar(bufparams);
317 pos_type const body_pos = par.beginningOfBody();
319 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
320 bool is_space = par.isLineSeparator(lpos);
322 (is_space && lpos + 1 <= bidi_end &&
323 !par.isLineSeparator(lpos + 1) &&
324 !par.isNewline(lpos + 1))
326 LyXFont font = par.getFontSettings(bufparams, pos);
327 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
328 font.number() == LyXFont::ON &&
329 par.getFontSettings(bufparams, lpos - 1).number()
331 font = par.getFontSettings(bufparams, lpos);
335 bool new_rtl = font.isVisibleRightToLeft();
336 bool new_rtl0 = font.isRightToLeft();
339 if (lpos == body_pos - 1
340 && row.pos() < body_pos - 1
342 new_level = rtl_par ? 1 : 0;
346 new_level = new_rtl ? 1 : 2;
348 new_level = rtl_par ? 2 : 0;
350 if (is_space && new_level >= level) {
356 int new_level2 = new_level;
358 if (level == new_level && rtl0 != new_rtl0) {
360 log2vis_list[lpos - bidi_start] = rtl ? 1 : -1;
361 } else if (level < new_level) {
362 log2vis_list[lpos - bidi_start] = rtl ? -1 : 1;
363 if (new_level > rtl_par)
364 bidi_same_direction = false;
366 log2vis_list[lpos - bidi_start] = new_rtl ? -1 : 1;
369 bidi_levels[lpos - bidi_start] = new_level;
371 while (level > new_level2) {
372 pos_type old_lpos = stack[--level];
373 int delta = lpos - old_lpos - 1;
376 log2vis_list[lpos - bidi_start] += delta;
377 log2vis_list[old_lpos - bidi_start] += delta;
379 while (level < new_level)
380 stack[level++] = lpos;
384 pos_type const old_lpos = stack[--level];
385 int delta = bidi_end - old_lpos;
388 log2vis_list[old_lpos - bidi_start] += delta;
391 pos_type vpos = bidi_start - 1;
392 for (pos_type lpos = bidi_start;
393 lpos <= bidi_end; ++lpos) {
394 vpos += log2vis_list[lpos - bidi_start];
395 vis2log_list[vpos - bidi_start] = lpos;
396 log2vis_list[lpos - bidi_start] = vpos;
401 // This method requires a previous call to ComputeBidiTables()
402 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
405 if (!lyxrc.rtl_support || pos == 0)
408 if (!bidi_InRange(pos - 1)) {
409 // This can happen if pos is the first char of a row.
410 // Returning false in this case is incorrect!
414 bool const rtl = bidi_level(pos - 1) % 2;
415 bool const rtl2 = bidi_InRange(pos)
416 ? bidi_level(pos) % 2
417 : par.isRightToLeftPar(buf.params());
422 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
423 pos_type pos, LyXFont const & font) const
425 if (!lyxrc.rtl_support)
426 return false; // This is just for speedup
428 bool const rtl = font.isVisibleRightToLeft();
429 bool const rtl2 = bidi_InRange(pos)
430 ? bidi_level(pos) % 2
431 : par.isRightToLeftPar(buf.params());
436 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
438 LyXTextClass const & tclass =
439 bv()->buffer()->params().getLyXTextClass();
440 LyXLayout_ptr const & layout = pit->layout();
442 string parindent = layout->parindent;
446 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
448 // this is the way, LyX handles the LaTeX-Environments.
449 // I have had this idea very late, so it seems to be a
450 // later added hack and this is true
451 if (!pit->getDepth()) {
452 if (pit->layout() == tclass.defaultLayout()) {
453 // find the previous same level paragraph
454 if (pit != ownerParagraphs().begin()) {
455 ParagraphList::iterator newpit =
456 depthHook(pit, ownerParagraphs(),
459 newpit->layout()->nextnoindent)
464 // find the next level paragraph
466 ParagraphList::iterator newpar = outerHook(pit,
469 // make a corresponding row. Needed to call leftMargin()
471 // check wether it is a sufficent paragraph
472 if (newpar != ownerParagraphs().end() &&
473 newpar->layout()->isEnvironment()) {
474 x = leftMargin(newpar, Row(newpar->size()));
477 if (newpar != ownerParagraphs().end() &&
478 pit->layout() == tclass.defaultLayout()) {
479 if (newpar->params().noindent())
482 parindent = newpar->layout()->parindent;
488 LyXFont const labelfont = getLabelFont(pit);
489 switch (layout->margintype) {
491 if (!layout->leftmargin.empty()) {
492 x += font_metrics::signedWidth(layout->leftmargin,
493 tclass.defaultfont());
495 if (!pit->getLabelstring().empty()) {
496 x += font_metrics::signedWidth(layout->labelindent,
498 x += font_metrics::width(pit->getLabelstring(),
500 x += font_metrics::width(layout->labelsep, labelfont);
504 x += font_metrics::signedWidth(layout->labelindent, labelfont);
505 // The width of an empty par, even with manual label, should be 0
506 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
507 if (!pit->getLabelWidthString().empty()) {
508 x += font_metrics::width(pit->getLabelWidthString(),
510 x += font_metrics::width(layout->labelsep, labelfont);
515 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
516 / (pit->getDepth() + 4);
518 case MARGIN_FIRST_DYNAMIC:
519 if (layout->labeltype == LABEL_MANUAL) {
520 if (row.pos() >= pit->beginningOfBody()) {
521 x += font_metrics::signedWidth(layout->leftmargin,
524 x += font_metrics::signedWidth(layout->labelindent,
528 // Special case to fix problems with
530 || (layout->labeltype == LABEL_STATIC
531 && layout->latextype == LATEX_ENVIRONMENT
532 && !isFirstInSequence(pit, ownerParagraphs()))) {
533 x += font_metrics::signedWidth(layout->leftmargin,
535 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
536 && layout->labeltype != LABEL_BIBLIO
537 && layout->labeltype !=
538 LABEL_CENTERED_TOP_ENVIRONMENT) {
539 x += font_metrics::signedWidth(layout->labelindent,
541 x += font_metrics::width(layout->labelsep, labelfont);
542 x += font_metrics::width(pit->getLabelstring(),
547 case MARGIN_RIGHT_ADDRESS_BOX:
549 // ok, a terrible hack. The left margin depends on the widest
550 // row in this paragraph.
551 RowList::iterator rit = pit->rows.begin();
552 RowList::iterator end = pit->rows.end();
553 int minfill = rit->fill();
554 for ( ; rit != end; ++rit)
555 if (rit->fill() < minfill)
556 minfill = rit->fill();
558 x += font_metrics::signedWidth(layout->leftmargin,
559 tclass.defaultfont());
565 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
566 LyXLength const len = pit->params().leftIndent();
567 int const tw = inset_owner ?
568 inset_owner->latexTextWidth(bv()) : workWidth();
569 x += len.inPixels(tw);
574 if (pit->params().align() == LYX_ALIGN_LAYOUT)
575 align = layout->align;
577 align = pit->params().align();
579 // set the correct parindent
580 if (row.pos() == 0) {
581 if ((layout->labeltype == LABEL_NO_LABEL
582 || layout->labeltype == LABEL_TOP_ENVIRONMENT
583 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
584 || (layout->labeltype == LABEL_STATIC
585 && layout->latextype == LATEX_ENVIRONMENT
586 && !isFirstInSequence(pit, ownerParagraphs())))
587 && align == LYX_ALIGN_BLOCK
588 && !pit->params().noindent()
589 // in tabulars and ert paragraphs are never indented!
590 && (!pit->inInset() || !pit->inInset()->owner() ||
591 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
592 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
593 && (pit->layout() != tclass.defaultLayout() ||
594 bv()->buffer()->params().paragraph_separation ==
595 BufferParams::PARSEP_INDENT)) {
596 x += font_metrics::signedWidth(parindent,
597 tclass.defaultfont());
598 } else if (layout->labeltype == LABEL_BIBLIO) {
599 // ale970405 Right width for bibitems
600 x += bibitemMaxWidth(bv(), tclass.defaultfont());
608 int LyXText::rightMargin(Paragraph const & par,
609 Buffer const & buf, Row const &) const
611 LyXTextClass const & tclass = buf.params().getLyXTextClass();
612 LyXLayout_ptr const & layout = par.layout();
615 + font_metrics::signedWidth(tclass.rightmargin(),
616 tclass.defaultfont())
617 + font_metrics::signedWidth(layout->rightmargin,
618 tclass.defaultfont())
619 * 4 / (par.getDepth() + 4);
623 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
625 if (pit->layout()->margintype == MARGIN_MANUAL) {
627 tmprow.pos(pit->size());
628 // return the beginning of the body
629 return leftMargin(pit, tmprow);
632 // LabelEnd is only needed if the layout
633 // fills a flushleft label.
640 // this needs special handling - only newlines count as a break point
641 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
643 for (; i < par.size(); ++i)
644 if (par.isNewline(i))
653 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
654 Row const & row) const
656 // maximum pixel width of a row.
657 int width = workWidth() - rightMargin(*pit, *bv()->buffer(), row);
659 // inset->textWidth() returns -1 via workWidth(),
664 LyXLayout_ptr const & layout = pit->layout();
666 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
667 return addressBreakPoint(row.pos(), *pit);
669 pos_type const pos = row.pos();
670 pos_type const body_pos = pit->beginningOfBody();
671 pos_type const last = pit->size();
672 pos_type point = last;
677 // Now we iterate through until we reach the right margin
678 // or the end of the par, then choose the possible break
681 int const left = leftMargin(pit, row);
684 // pixel width since last breakpoint
689 // We re-use the font resolution for the entire font span when possible
690 LyXFont font = getFont(pit, i);
691 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
693 for (; i < last; ++i) {
694 if (pit->isNewline(i)) {
700 InsetOld * in = pit->getInset(i + 1);
701 if (in && in->display()) {
706 in = pit->getInset(i);
707 if (in && in->display()) {
713 char const c = pit->getChar(i);
714 if (i > endPosOfFontSpan) {
715 font = getFont(pit, i);
716 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
721 // add the auto-hfill from label end to the body
722 if (body_pos && i == body_pos) {
723 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
724 if (pit->isLineSeparator(i - 1))
725 thiswidth -= singleWidth(pit, i - 1);
726 int left_margin = labelEnd(pit, row);
727 if (thiswidth + x < left_margin)
728 thiswidth = left_margin - x;
729 thiswidth += singleWidth(pit, i, c, font);
731 thiswidth = singleWidth(pit, i, c, font);
735 //lyxerr << "i: " << i << " x: " << x << " width: " << width << endl;
736 chunkwidth += thiswidth;
738 // break before a character that will fall off
739 // the right of the row
741 // if no break before, break here
742 if (point == last || chunkwidth >= width - left) {
745 // exit on last registered breakpoint:
754 InsetOld * in = pit->getInset(i);
755 if (!in || in->isChar()) {
756 // some insets are line separators too
757 if (pit->isLineSeparator(i)) {
758 // register breakpoint:
765 if (point == last && x >= width) {
766 // didn't find one, break at the point we reached the edge
768 } else if (i == last && x < width) {
769 // found one, but we fell off the end of the par, so prefer
774 // manual labels cannot be broken in LaTeX. But we
775 // want to make our on-screen rendering of footnotes
777 if (body_pos && point < body_pos)
778 point = body_pos - 1;
784 // returns the minimum space a row needs on the screen in pixel
785 int LyXText::fill(ParagraphList::iterator pit, Row & row, int paper_width) const
787 if (paper_width < 0) {
788 lyxerr << "paperwidth < 0: " << paper_width << " Why?" << endl;
793 // get the pure distance
794 pos_type const last = lastPos(*pit, row);
796 LyXLayout_ptr const & layout = pit->layout();
798 // special handling of the right address boxes
799 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
800 int const tmpfill = row.fill();
801 row.fill(0); // the minfill in leftMargin()
802 w = leftMargin(pit, row);
805 w = leftMargin(pit, row);
807 pos_type const body_pos = pit->beginningOfBody();
808 pos_type i = row.pos();
810 if (! pit->empty() && i <= last) {
811 // We re-use the font resolution for the entire span when possible
812 LyXFont font = getFont(pit, i);
813 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
815 if (body_pos > 0 && i == body_pos) {
816 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
817 if (pit->isLineSeparator(i - 1))
818 w -= singleWidth(pit, i - 1);
819 int left_margin = labelEnd(pit, row);
823 char const c = pit->getChar(i);
824 if (IsPrintable(c) && i > endPosOfFontSpan) {
825 // We need to get the next font
826 font = getFont(pit, i);
827 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
829 w += singleWidth(pit, i, c, font);
833 if (body_pos > 0 && body_pos > last) {
834 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
835 if (last >= 0 && pit->isLineSeparator(last))
836 w -= singleWidth(pit, last);
837 int const left_margin = labelEnd(pit, row);
842 int const fill = paper_width - w - rightMargin(*pit, *bv()->buffer(), row);
844 // If this case happens, it means that our calculation
845 // of the widths of the chars when we do rowBreakPoint()
846 // went wrong for some reason. Typically in list bodies.
847 // Things just about hobble on anyway, though you'll end
848 // up with a "fill_separator" less than zero, which corresponds
849 // to inter-word spacing being too small. Hopefully this problem
850 // will die when the label hacks die.
851 if (lyxerr.debugging() && fill < 0) {
852 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
853 << " w " << w << " paper_width " << paper_width
854 << " right margin " << rightMargin(*pit, *bv()->buffer(), row) << endl;
860 // returns the minimum space a manual label needs on the screen in pixel
861 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
863 pos_type last = pit->beginningOfBody();
865 BOOST_ASSERT(last > 0);
867 // -1 because a label ends either with a space that is in the label,
868 // or with the beginning of a footnote that is outside the label.
871 // a separator at this end does not count
872 if (pit->isLineSeparator(last))
876 pos_type i = row.pos();
878 w += singleWidth(pit, i);
883 string const & labwidstr = pit->params().labelWidthString();
884 if (!labwidstr.empty()) {
885 LyXFont const labfont = getLabelFont(pit);
886 int const labwidth = font_metrics::width(labwidstr, labfont);
887 fill = max(labwidth - w, 0);
894 LColor_color LyXText::backgroundColor() const
897 return inset_owner->backgroundColor();
899 return LColor::background;
903 void LyXText::setHeightOfRow(ParagraphList::iterator pit, Row & row)
905 // get the maximum ascent and the maximum descent
906 double layoutasc = 0;
907 double layoutdesc = 0;
910 // ok, let us initialize the maxasc and maxdesc value.
911 // Only the fontsize count. The other properties
912 // are taken from the layoutfont. Nicer on the screen :)
913 LyXLayout_ptr const & layout = pit->layout();
915 // as max get the first character of this row then it can increase but not
916 // decrease the height. Just some point to start with so we don't have to
917 // do the assignment below too often.
918 LyXFont font = getFont(pit, row.pos());
919 LyXFont::FONT_SIZE const tmpsize = font.size();
920 font = getLayoutFont(pit);
921 LyXFont::FONT_SIZE const size = font.size();
922 font.setSize(tmpsize);
924 LyXFont labelfont = getLabelFont(pit);
926 double spacing_val = 1.0;
927 if (!pit->params().spacing().isDefault())
928 spacing_val = pit->params().spacing().getValue();
930 spacing_val = bv()->buffer()->params().spacing().getValue();
931 //lyxerr << "spacing_val = " << spacing_val << endl;
933 int maxasc = int(font_metrics::maxAscent(font) *
934 layout->spacing.getValue() * spacing_val);
935 int maxdesc = int(font_metrics::maxDescent(font) *
936 layout->spacing.getValue() * spacing_val);
938 pos_type const pos_end = lastPos(*pit, row);
943 // We re-use the font resolution for the entire font span when possible
944 LyXFont font = getFont(pit, row.pos());
945 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(row.pos());
948 Paragraph const & par = *pit;
950 // Check if any insets are larger
951 for (pos_type pos = row.pos(); pos <= pos_end; ++pos) {
952 // Manual inlined optimised version of common case of
953 // "maxwidth += singleWidth(pit, pos);"
954 char const c = par.getChar(pos);
956 if (IsPrintable(c)) {
957 if (pos > endPosOfFontSpan) {
958 // We need to get the next font
959 font = getFont(pit, pos);
960 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
962 if (! font.language()->RightToLeft()) {
963 maxwidth += font_metrics::width(c, font);
965 // Fall-back to normal case
966 maxwidth += singleWidth(pit, pos, c, font);
967 // And flush font cache
968 endPosOfFontSpan = 0;
971 // Special handling of insets - are any larger?
972 if (par.isInset(pos)) {
973 InsetOld const * tmpinset = par.getInset(pos);
975 maxwidth += tmpinset->width();
976 maxasc = max(maxasc, tmpinset->ascent());
977 maxdesc = max(maxdesc, tmpinset->descent());
980 // Fall-back to normal case
981 maxwidth += singleWidth(pit, pos, c, font);
982 // And flush font cache
983 endPosOfFontSpan = 0;
989 // Check if any custom fonts are larger (Asger)
990 // This is not completely correct, but we can live with the small,
991 // cosmetic error for now.
992 LyXFont::FONT_SIZE maxsize =
993 pit->highestFontInRange(row.pos(), pos_end, size);
994 if (maxsize > font.size()) {
995 font.setSize(maxsize);
996 maxasc = max(maxasc, font_metrics::maxAscent(font));
997 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
1000 // This is nicer with box insets:
1004 row.ascent_of_text(maxasc);
1006 // is it a top line?
1008 BufferParams const & bufparams = bv()->buffer()->params();
1009 // some parksips VERY EASY IMPLEMENTATION
1010 if (bv()->buffer()->params().paragraph_separation ==
1011 BufferParams::PARSEP_SKIP)
1013 if (layout->isParagraph()
1014 && pit->getDepth() == 0
1015 && pit != ownerParagraphs().begin())
1017 maxasc += bufparams.getDefSkip().inPixels(*bv());
1018 } else if (pit != ownerParagraphs().begin() &&
1019 boost::prior(pit)->layout()->isParagraph() &&
1020 boost::prior(pit)->getDepth() == 0)
1022 // is it right to use defskip here too? (AS)
1023 maxasc += bufparams.getDefSkip().inPixels(*bv());
1028 if (pit == ownerParagraphs().begin() && !isInInset())
1029 maxasc += PAPER_MARGIN;
1031 // add the vertical spaces, that the user added
1032 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1034 // do not forget the DTP-lines!
1035 // there height depends on the font of the nearest character
1036 if (pit->params().lineTop())
1038 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1039 // and now the pagebreaks
1040 if (pit->params().pagebreakTop())
1041 maxasc += 3 * defaultRowHeight();
1043 if (pit->params().startOfAppendix())
1044 maxasc += 3 * defaultRowHeight();
1046 // This is special code for the chapter, since the label of this
1047 // layout is printed in an extra row
1048 if (layout->counter == "chapter" && bufparams.secnumdepth >= 0) {
1049 float spacing_val = 1.0;
1050 if (!pit->params().spacing().isDefault()) {
1051 spacing_val = pit->params().spacing().getValue();
1053 spacing_val = bufparams.spacing().getValue();
1056 labeladdon = int(font_metrics::maxDescent(labelfont) *
1057 layout->spacing.getValue() *
1059 + int(font_metrics::maxAscent(labelfont) *
1060 layout->spacing.getValue() *
1064 // special code for the top label
1065 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1066 || layout->labeltype == LABEL_BIBLIO
1067 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1068 && isFirstInSequence(pit, ownerParagraphs())
1069 && !pit->getLabelstring().empty())
1071 float spacing_val = 1.0;
1072 if (!pit->params().spacing().isDefault()) {
1073 spacing_val = pit->params().spacing().getValue();
1075 spacing_val = bufparams.spacing().getValue();
1079 (font_metrics::maxAscent(labelfont) +
1080 font_metrics::maxDescent(labelfont)) *
1081 layout->spacing.getValue() *
1083 + layout->topsep * defaultRowHeight()
1084 + layout->labelbottomsep * defaultRowHeight());
1087 // And now the layout spaces, for example before and after
1088 // a section, or between the items of a itemize or enumerate
1091 if (!pit->params().pagebreakTop()) {
1092 ParagraphList::iterator prev =
1093 depthHook(pit, ownerParagraphs(),
1095 if (prev != pit && prev->layout() == layout &&
1096 prev->getDepth() == pit->getDepth() &&
1097 prev->getLabelWidthString() == pit->getLabelWidthString())
1099 layoutasc = (layout->itemsep * defaultRowHeight());
1100 } else if (pit != ownerParagraphs().begin() || row.pos() != 0) {
1101 tmptop = layout->topsep;
1104 layoutasc = (tmptop * defaultRowHeight());
1105 } else if (pit->params().lineTop()) {
1106 tmptop = layout->topsep;
1109 layoutasc = (tmptop * defaultRowHeight());
1112 prev = outerHook(pit, ownerParagraphs());
1113 if (prev != ownerParagraphs().end()) {
1114 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1115 } else if (pit != ownerParagraphs().begin()) {
1116 ParagraphList::iterator prior_pit = boost::prior(pit);
1117 if (prior_pit->getDepth() != 0 ||
1118 prior_pit->layout() == layout) {
1119 maxasc += int(layout->parsep * defaultRowHeight());
1125 // is it a bottom line?
1126 if (row.endpos() >= pit->size()) {
1127 // the bottom margin
1128 ParagraphList::iterator nextpit = boost::next(pit);
1129 if (nextpit == ownerParagraphs().end() && !isInInset())
1130 maxdesc += PAPER_MARGIN;
1132 // add the vertical spaces, that the user added
1133 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1135 // do not forget the DTP-lines!
1136 // there height depends on the font of the nearest character
1137 if (pit->params().lineBottom())
1138 maxdesc += 2 * font_metrics::ascent('x',
1139 getFont(pit, max(pos_type(0), pit->size() - 1)));
1141 // and now the pagebreaks
1142 if (pit->params().pagebreakBottom())
1143 maxdesc += 3 * defaultRowHeight();
1145 // and now the layout spaces, for example before and after
1146 // a section, or between the items of a itemize or enumerate
1148 if (!pit->params().pagebreakBottom()
1149 && nextpit != ownerParagraphs().end()) {
1150 ParagraphList::iterator comparepit = pit;
1154 if (comparepit->getDepth() > nextpit->getDepth()) {
1155 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1156 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1157 if (comparepit->layout()!= nextpit->layout()
1158 || nextpit->getLabelWidthString() !=
1159 comparepit->getLabelWidthString())
1161 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1163 if (unusual > usual)
1164 layoutdesc = unusual;
1167 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1169 if (comparepit->layout() != nextpit->layout()
1170 || nextpit->getLabelWidthString() !=
1171 comparepit->getLabelWidthString())
1172 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1177 // incalculate the layout spaces
1178 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1179 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1181 row.height(maxasc + maxdesc + labeladdon);
1182 row.baseline(maxasc + labeladdon);
1183 row.top_of_text(row.baseline() - font_metrics::maxAscent(font));
1185 row.width(maxwidth);
1188 width = max(workWidth(), int(maxParagraphWidth(ownerParagraphs())));
1192 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1194 // allow only if at start or end, or all previous is new text
1195 if (cursor.pos() && cursor.pos() != cursorPar()->size()
1196 && cursorPar()->isChangeEdited(0, cursor.pos()))
1199 LyXTextClass const & tclass =
1200 bv()->buffer()->params().getLyXTextClass();
1201 LyXLayout_ptr const & layout = cursorPar()->layout();
1203 // this is only allowed, if the current paragraph is not empty or caption
1204 // and if it has not the keepempty flag active
1205 if (cursorPar()->empty() && !cursorPar()->allowEmpty()
1206 && layout->labeltype != LABEL_SENSITIVE)
1209 recUndo(cursor.par());
1211 // Always break behind a space
1213 // It is better to erase the space (Dekel)
1214 if (cursor.pos() < cursorPar()->size()
1215 && cursorPar()->isLineSeparator(cursor.pos()))
1216 cursorPar()->erase(cursor.pos());
1218 // break the paragraph
1222 keep_layout = layout->isEnvironment();
1224 // we need to set this before we insert the paragraph. IMO the
1225 // breakParagraph call should return a bool if it inserts the
1226 // paragraph before or behind and we should react on that one
1227 // but we can fix this in 1.3.0 (Jug 20020509)
1228 bool const isempty = (cursorPar()->allowEmpty() && cursorPar()->empty());
1229 ::breakParagraph(bv()->buffer()->params(), paragraphs, cursorPar(),
1230 cursor.pos(), keep_layout);
1232 #warning Trouble Point! (Lgb)
1233 // When ::breakParagraph is called from within an inset we must
1234 // ensure that the correct ParagraphList is used. Today that is not
1235 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1236 ParagraphList::iterator next_par = boost::next(cursorPar());
1238 // well this is the caption hack since one caption is really enough
1239 if (layout->labeltype == LABEL_SENSITIVE) {
1241 // set to standard-layout
1242 cursorPar()->applyLayout(tclass.defaultLayout());
1244 // set to standard-layout
1245 next_par->applyLayout(tclass.defaultLayout());
1248 // if the cursor is at the beginning of a row without prior newline,
1250 // This touches only the screen-update. Otherwise we would may have
1251 // an empty row on the screen
1252 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1253 && !cursorPar()->isNewline(cursor.pos() - 1))
1258 while (!next_par->empty() && next_par->isNewline(0))
1262 redoParagraph(cursorPar());
1263 redoParagraph(next_par);
1265 // This check is necessary. Otherwise the new empty paragraph will
1266 // be deleted automatically. And it is more friendly for the user!
1267 if (cursor.pos() || isempty)
1268 setCursor(next_par, 0);
1270 setCursor(cursorPar(), 0);
1274 // convenience function
1275 void LyXText::redoParagraph()
1278 redoParagraph(cursorPar());
1279 setCursorIntern(cursor.par(), cursor.pos());
1283 // insert a character, moves all the following breaks in the
1284 // same Paragraph one to the right and make a rebreak
1285 void LyXText::insertChar(char c)
1287 recordUndo(Undo::INSERT, this, cursor.par(), cursor.par());
1289 // When the free-spacing option is set for the current layout,
1290 // disable the double-space checking
1292 bool const freeSpacing = cursorPar()->layout()->free_spacing ||
1293 cursorPar()->isFreeSpacing();
1295 if (lyxrc.auto_number) {
1296 static string const number_operators = "+-/*";
1297 static string const number_unary_operators = "+-";
1298 static string const number_seperators = ".,:";
1300 if (current_font.number() == LyXFont::ON) {
1301 if (!IsDigit(c) && !contains(number_operators, c) &&
1302 !(contains(number_seperators, c) &&
1303 cursor.pos() >= 1 &&
1304 cursor.pos() < cursorPar()->size() &&
1305 getFont(cursorPar(), cursor.pos()).number() == LyXFont::ON &&
1306 getFont(cursorPar(), cursor.pos() - 1).number() == LyXFont::ON)
1308 number(bv()); // Set current_font.number to OFF
1309 } else if (IsDigit(c) &&
1310 real_current_font.isVisibleRightToLeft()) {
1311 number(bv()); // Set current_font.number to ON
1313 if (cursor.pos() > 0) {
1314 char const c = cursorPar()->getChar(cursor.pos() - 1);
1315 if (contains(number_unary_operators, c) &&
1316 (cursor.pos() == 1 ||
1317 cursorPar()->isSeparator(cursor.pos() - 2) ||
1318 cursorPar()->isNewline(cursor.pos() - 2))
1324 } else if (contains(number_seperators, c) &&
1325 cursor.pos() >= 2 &&
1328 cursor.pos() - 2).number() == LyXFont::ON) {
1339 // First check, if there will be two blanks together or a blank at
1340 // the beginning of a paragraph.
1341 // I decided to handle blanks like normal characters, the main
1342 // difference are the special checks when calculating the row.fill
1343 // (blank does not count at the end of a row) and the check here
1345 // The bug is triggered when we type in a description environment:
1346 // The current_font is not changed when we go from label to main text
1347 // and it should (along with realtmpfont) when we type the space.
1348 // CHECK There is a bug here! (Asger)
1350 // store the current font. This is because of the use of cursor
1351 // movements. The moving cursor would refresh the current font
1352 LyXFont realtmpfont = real_current_font;
1353 LyXFont rawtmpfont = current_font;
1355 if (!freeSpacing && IsLineSeparatorChar(c)) {
1356 if ((cursor.pos() > 0
1357 && cursorPar()->isLineSeparator(cursor.pos() - 1))
1358 || (cursor.pos() > 0
1359 && cursorPar()->isNewline(cursor.pos() - 1))
1360 || (cursor.pos() == 0)) {
1361 static bool sent_space_message = false;
1362 if (!sent_space_message) {
1363 if (cursor.pos() == 0)
1364 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1366 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1367 sent_space_message = true;
1374 // Here case LyXText::InsertInset already inserted the character
1375 if (c != Paragraph::META_INSET)
1376 cursorPar()->insertChar(cursor.pos(), c);
1378 setCharFont(cursorPar(), cursor.pos(), rawtmpfont);
1380 current_font = rawtmpfont;
1381 real_current_font = realtmpfont;
1382 redoParagraph(cursorPar());
1383 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1389 void LyXText::charInserted()
1391 // Here we could call finishUndo for every 20 characters inserted.
1392 // This is from my experience how emacs does it. (Lgb)
1393 static unsigned int counter;
1403 void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
1405 double w = row.fill();
1406 double fill_hfill = 0;
1407 double fill_label_hfill = 0;
1408 double fill_separator = 0;
1412 pit->isRightToLeftPar(bv()->buffer()->params());
1414 x = workWidth() > 0 ? rightMargin(*pit, *bv()->buffer(), row) : 0;
1416 x = workWidth() > 0 ? leftMargin(pit, row) : 0;
1418 // is there a manual margin with a manual label
1419 LyXLayout_ptr const & layout = pit->layout();
1421 if (layout->margintype == MARGIN_MANUAL
1422 && layout->labeltype == LABEL_MANUAL) {
1423 /// We might have real hfills in the label part
1424 int nlh = numberOfLabelHfills(*pit, row);
1426 // A manual label par (e.g. List) has an auto-hfill
1427 // between the label text and the body of the
1429 // But we don't want to do this auto hfill if the par
1434 if (nlh && !pit->getLabelWidthString().empty()) {
1435 fill_label_hfill = labelFill(pit, row) / double(nlh);
1439 // are there any hfills in the row?
1440 int const nh = numberOfHfills(*pit, row);
1444 fill_hfill = w / nh;
1445 // we don't have to look at the alignment if it is ALIGN_LEFT and
1446 // if the row is already larger then the permitted width as then
1447 // we force the LEFT_ALIGN'edness!
1448 } else if (int(row.width()) < workWidth()) {
1449 // is it block, flushleft or flushright?
1450 // set x how you need it
1452 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1453 align = layout->align;
1455 align = pit->params().align();
1457 InsetOld * inset = 0;
1458 // ERT insets should always be LEFT ALIGNED on screen
1459 inset = pit->inInset();
1460 if (inset && inset->owner() &&
1461 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1463 align = LYX_ALIGN_LEFT;
1466 // Display-style insets should always be on a centred row
1467 // The test on pit->size() is to catch zero-size pars, which
1468 // would trigger the assert in Paragraph::getInset().
1469 //inset = pit->size() ? pit->getInset(row.pos()) : 0;
1470 inset = pit->isInset(row.pos()) ? pit->getInset(row.pos()) : 0;
1471 if (inset && inset->display()) {
1472 align = LYX_ALIGN_CENTER;
1476 case LYX_ALIGN_BLOCK:
1478 int const ns = numberOfSeparators(*pit, row);
1479 bool disp_inset = false;
1480 if (row.endpos() < pit->size()) {
1481 InsetOld * in = pit->getInset(row.endpos());
1483 disp_inset = in->display();
1485 // If we have separators, this is not the last row of a
1486 // par, does not end in newline, and is not row above a
1487 // display inset... then stretch it
1489 && row.endpos() < pit->size()
1490 && !pit->isNewline(row.endpos())
1493 fill_separator = w / ns;
1494 } else if (is_rtl) {
1499 case LYX_ALIGN_RIGHT:
1502 case LYX_ALIGN_CENTER:
1508 computeBidiTables(*pit, *bv()->buffer(), row);
1510 pos_type body_pos = pit->beginningOfBody();
1511 pos_type last = lastPos(*pit, row);
1514 (body_pos - 1 > last ||
1515 !pit->isLineSeparator(body_pos - 1))) {
1516 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1517 if (body_pos - 1 <= last)
1518 x += fill_label_hfill;
1522 row.fill_hfill(fill_hfill);
1523 row.fill_label_hfill(fill_label_hfill);
1524 row.fill_separator(fill_separator);
1529 // important for the screen
1532 // the cursor set functions have a special mechanism. When they
1533 // realize, that you left an empty paragraph, they will delete it.
1534 // They also delete the corresponding row
1536 void LyXText::cursorRightOneWord()
1538 ::cursorRightOneWord(*this, cursor, ownerParagraphs());
1539 setCursor(cursorPar(), cursor.pos());
1543 // Skip initial whitespace at end of word and move cursor to *start*
1544 // of prior word, not to end of next prior word.
1545 void LyXText::cursorLeftOneWord()
1547 LyXCursor tmpcursor = cursor;
1548 ::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
1549 setCursor(getPar(tmpcursor), tmpcursor.pos());
1553 void LyXText::selectWord(word_location loc)
1555 LyXCursor from = cursor;
1556 LyXCursor to = cursor;
1557 ::getWord(*this, from, to, loc, ownerParagraphs());
1559 setCursor(from.par(), from.pos());
1562 selection.cursor = cursor;
1563 setCursor(to.par(), to.pos());
1568 // Select the word currently under the cursor when no
1569 // selection is currently set
1570 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1572 if (!selection.set()) {
1574 return selection.set();
1580 void LyXText::acceptChange()
1582 if (!selection.set() && cursorPar()->size())
1585 if (selection.start.par() == selection.end.par()) {
1586 LyXCursor & startc = selection.start;
1587 LyXCursor & endc = selection.end;
1588 recordUndo(Undo::INSERT, this, startc.par());
1589 getPar(startc)->acceptChange(startc.pos(), endc.pos());
1592 redoParagraph(getPar(startc));
1593 setCursorIntern(startc.par(), 0);
1595 #warning handle multi par selection
1599 void LyXText::rejectChange()
1601 if (!selection.set() && cursorPar()->size())
1604 if (selection.start.par() == selection.end.par()) {
1605 LyXCursor & startc = selection.start;
1606 LyXCursor & endc = selection.end;
1607 recordUndo(Undo::INSERT, this, startc.par());
1608 getPar(startc)->rejectChange(startc.pos(), endc.pos());
1611 redoParagraph(getPar(startc));
1612 setCursorIntern(startc.par(), 0);
1614 #warning handle multi par selection
1618 // This function is only used by the spellchecker for NextWord().
1619 // It doesn't handle LYX_ACCENTs and probably never will.
1620 WordLangTuple const LyXText::selectNextWordToSpellcheck(float & value)
1622 if (the_locking_inset) {
1623 WordLangTuple word =
1624 the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1625 if (!word.word().empty()) {
1626 value += float(cursor.y());
1627 value /= float(height);
1630 // we have to go on checking so move cursor to the next char
1631 if (cursor.pos() == cursorPar()->size()) {
1632 if (cursor.par() + 1 == int(ownerParagraphs().size()))
1634 cursor.par(cursor.par() + 1);
1637 cursor.pos(cursor.pos() + 1);
1640 int const tmppar = cursor.par();
1642 // If this is not the very first word, skip rest of
1643 // current word because we are probably in the middle
1644 // of a word if there is text here.
1645 if (cursor.pos() || cursor.par() != 0) {
1646 while (cursor.pos() < cursorPar()->size()
1647 && cursorPar()->isLetter(cursor.pos()))
1648 cursor.pos(cursor.pos() + 1);
1651 // Now, skip until we have real text (will jump paragraphs)
1653 ParagraphList::iterator cpit = cursorPar();
1654 pos_type const cpos = cursor.pos();
1656 if (cpos == cpit->size()) {
1657 if (cursor.par() + 1 != int(ownerParagraphs().size())) {
1658 cursor.par(cursor.par() + 1);
1665 bool const is_good_inset = cpit->isInset(cpos)
1666 && cpit->getInset(cpos)->allowSpellcheck();
1668 if (!isDeletedText(*cpit, cpos)
1669 && (is_good_inset || cpit->isLetter(cpos)))
1672 cursor.pos(cpos + 1);
1675 // now check if we hit an inset so it has to be a inset containing text!
1676 if (cursor.pos() < cursorPar()->size() &&
1677 cursorPar()->isInset(cursor.pos())) {
1679 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1680 cursorPar()->getInset(cursor.pos())->dispatch(cmd);
1681 // now call us again to do the above trick
1682 // but obviously we have to start from down below ;)
1683 return bv()->text->selectNextWordToSpellcheck(value);
1686 // Update the value if we changed paragraphs
1687 if (cursor.par() != tmppar) {
1688 setCursor(cursor.par(), cursor.pos());
1689 value = float(cursor.y())/float(height);
1692 // Start the selection from here
1693 selection.cursor = cursor;
1695 string lang_code = getFont(cursorPar(), cursor.pos()).language()->code();
1696 // and find the end of the word (insets like optional hyphens
1697 // and ligature break are part of a word)
1698 while (cursor.pos() < cursorPar()->size()
1699 && cursorPar()->isLetter(cursor.pos())
1700 && !isDeletedText(*cursorPar(), cursor.pos()))
1701 cursor.pos(cursor.pos() + 1);
1703 // Finally, we copy the word to a string and return it
1705 if (selection.cursor.pos() < cursor.pos()) {
1706 for (pos_type i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1707 if (!cursorPar()->isInset(i))
1708 str += cursorPar()->getChar(i);
1711 return WordLangTuple(str, lang_code);
1715 // This one is also only for the spellchecker
1716 void LyXText::selectSelectedWord()
1718 if (the_locking_inset) {
1719 the_locking_inset->selectSelectedWord(bv());
1722 // move cursor to the beginning
1723 setCursor(selection.cursor.par(), selection.cursor.pos());
1725 // set the sel cursor
1726 selection.cursor = cursor;
1728 // now find the end of the word
1729 while (cursor.pos() < cursorPar()->size()
1730 && cursorPar()->isLetter(cursor.pos()))
1731 cursor.pos(cursor.pos() + 1);
1733 setCursor(cursorPar(), cursor.pos());
1735 // finally set the selection
1740 // Delete from cursor up to the end of the current or next word.
1741 void LyXText::deleteWordForward()
1743 if (cursorPar()->empty())
1746 LyXCursor tmpcursor = cursor;
1747 selection.set(true); // to avoid deletion
1748 cursorRightOneWord();
1749 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1750 selection.cursor = cursor;
1753 cutSelection(true, false);
1758 // Delete from cursor to start of current or prior word.
1759 void LyXText::deleteWordBackward()
1761 if (cursorPar()->empty())
1764 LyXCursor tmpcursor = cursor;
1765 selection.set(true); // to avoid deletion
1766 cursorLeftOneWord();
1767 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1768 selection.cursor = cursor;
1771 cutSelection(true, false);
1776 // Kill to end of line.
1777 void LyXText::deleteLineForward()
1779 if (cursorPar()->empty())
1780 // Paragraph is empty, so we just go to the right
1783 LyXCursor tmpcursor = cursor;
1784 // We can't store the row over a regular setCursor
1785 // so we set it to 0 and reset it afterwards.
1786 selection.set(true); // to avoid deletion
1788 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1789 selection.cursor = cursor;
1792 // What is this test for ??? (JMarc)
1793 if (!selection.set()) {
1794 deleteWordForward();
1796 cutSelection(true, false);
1802 void LyXText::changeCase(LyXText::TextCase action)
1807 if (selection.set()) {
1808 from = selection.start;
1812 ::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1813 setCursor(to.par(), to.pos() + 1);
1816 recordUndo(Undo::ATOMIC, this, from.par(), to.par());
1818 pos_type pos = from.pos();
1819 int par = from.par();
1821 while (par != int(ownerParagraphs().size()) &&
1822 (pos != to.pos() || par != to.par())) {
1823 ParagraphList::iterator pit = getPar(par);
1824 if (pos == pit->size()) {
1829 unsigned char c = pit->getChar(pos);
1830 if (c != Paragraph::META_INSET) {
1832 case text_lowercase:
1835 case text_capitalization:
1837 action = text_lowercase;
1839 case text_uppercase:
1845 pit->setChar(pos, c);
1851 void LyXText::Delete()
1853 // this is a very easy implementation
1855 LyXCursor old_cursor = cursor;
1856 int const old_cur_par_id = cursorPar()->id();
1857 int const old_cur_par_prev_id =
1858 old_cursor.par() ? getPar(old_cursor.par() - 1)->id() : -1;
1860 // just move to the right
1863 // CHECK Look at the comment here.
1864 // This check is not very good...
1865 // The cursorRightIntern calls DeleteEmptyParagraphMechanism
1866 // and that can very well delete the par or par->previous in
1867 // old_cursor. Will a solution where we compare paragraph id's
1869 int iid = cursor.par() ? getPar(cursor.par() - 1)->id() : -1;
1870 if (iid == old_cur_par_prev_id && cursorPar()->id() != old_cur_par_id) {
1871 // delete-empty-paragraph-mechanism has done it
1875 // if you had success make a backspace
1876 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1877 recordUndo(Undo::DELETE, this, old_cursor.par());
1883 void LyXText::backspace()
1885 // Get the font that is used to calculate the baselineskip
1886 ParagraphList::iterator pit = cursorPar();
1887 pos_type lastpos = pit->size();
1889 if (cursor.pos() == 0) {
1890 // The cursor is at the beginning of a paragraph,
1891 // so the the backspace will collapse two paragraphs into one.
1893 // but it's not allowed unless it's new
1894 if (pit->isChangeEdited(0, pit->size()))
1897 // we may paste some paragraphs
1899 // is it an empty paragraph?
1901 if (lastpos == 0 || (lastpos == 1 && pit->isSeparator(0))) {
1902 // This is an empty paragraph and we delete it just
1903 // by moving the cursor one step
1904 // left and let the DeleteEmptyParagraphMechanism
1905 // handle the actual deletion of the paragraph.
1908 ParagraphList::iterator tmppit = getPar(cursor.par() - 1);
1909 if (cursorPar()->layout() == tmppit->layout()
1910 && cursorPar()->getAlign() == tmppit->getAlign()) {
1911 // Inherit bottom DTD from the paragraph below.
1912 // (the one we are deleting)
1913 tmppit->params().lineBottom(cursorPar()->params().lineBottom());
1914 tmppit->params().spaceBottom(cursorPar()->params().spaceBottom());
1915 tmppit->params().pagebreakBottom(cursorPar()->params().pagebreakBottom());
1920 // the layout things can change the height of a row !
1926 if (cursor.par() != 0)
1927 recordUndo(Undo::DELETE, this, cursor.par() - 1, cursor.par());
1929 ParagraphList::iterator tmppit = cursorPar();
1930 // We used to do cursorLeftIntern() here, but it is
1931 // not a good idea since it triggers the auto-delete
1932 // mechanism. So we do a cursorLeftIntern()-lite,
1933 // without the dreaded mechanism. (JMarc)
1934 if (cursor.par() != 0) {
1935 // steps into the above paragraph.
1936 setCursorIntern(cursor.par() - 1,
1937 getPar(cursor.par() - 1)->size(),
1941 // Pasting is not allowed, if the paragraphs have different
1942 // layout. I think it is a real bug of all other
1943 // word processors to allow it. It confuses the user.
1944 // Correction: Pasting is always allowed with standard-layout
1945 Buffer & buf = *bv()->buffer();
1946 BufferParams const & bufparams = buf.params();
1947 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1949 if (cursorPar() != tmppit
1950 && (cursorPar()->layout() == tmppit->layout()
1951 || tmppit->layout() == tclass.defaultLayout())
1952 && cursorPar()->getAlign() == tmppit->getAlign()) {
1953 mergeParagraph(bufparams,
1954 buf.paragraphs(), cursorPar());
1956 if (cursor.pos() && cursorPar()->isSeparator(cursor.pos() - 1))
1957 cursor.pos(cursor.pos() - 1);
1959 // the row may have changed, block, hfills etc.
1961 setCursor(cursor.par(), cursor.pos(), false);
1964 // this is the code for a normal backspace, not pasting
1966 recordUndo(Undo::DELETE, this, cursor.par());
1967 // We used to do cursorLeftIntern() here, but it is
1968 // not a good idea since it triggers the auto-delete
1969 // mechanism. So we do a cursorLeftIntern()-lite,
1970 // without the dreaded mechanism. (JMarc)
1971 setCursorIntern(cursor.par(), cursor.pos() - 1,
1972 false, cursor.boundary());
1973 cursorPar()->erase(cursor.pos());
1976 lastpos = cursorPar()->size();
1977 if (cursor.pos() == lastpos)
1981 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1985 ParagraphList::iterator LyXText::cursorPar() const
1987 return getPar(cursor.par());
1991 ParagraphList::iterator LyXText::getPar(LyXCursor const & cur) const
1993 return getPar(cur.par());
1997 ParagraphList::iterator LyXText::getPar(int par) const
1999 BOOST_ASSERT(par >= 0);
2000 BOOST_ASSERT(par < int(ownerParagraphs().size()));
2001 ParagraphList::iterator pit = ownerParagraphs().begin();
2002 std::advance(pit, par);
2008 RowList::iterator LyXText::cursorRow() const
2010 return getRow(*cursorPar(), cursor.pos());
2014 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2016 return getRow(*getPar(cur), cur.pos());
2020 RowList::iterator LyXText::getRow(Paragraph & par, pos_type pos) const
2022 RowList::iterator rit = boost::prior(par.rows.end());
2023 RowList::iterator const begin = par.rows.begin();
2025 while (rit != begin && rit->pos() > pos)
2033 LyXText::getRowNearY(int y, ParagraphList::iterator & pit) const
2035 //lyxerr << "getRowNearY: y " << y << endl;
2037 pit = boost::prior(ownerParagraphs().end());
2039 RowList::iterator rit = lastRow();
2040 RowList::iterator rbegin = firstRow();
2042 while (rit != rbegin && int(pit->y + rit->y_offset()) > y)
2043 previousRow(pit, rit);
2049 int LyXText::getDepth() const
2051 return cursorPar()->getDepth();
2055 RowList::iterator LyXText::firstRow() const
2057 return ownerParagraphs().front().rows.begin();
2061 RowList::iterator LyXText::lastRow() const
2063 return boost::prior(endRow());
2067 RowList::iterator LyXText::endRow() const
2069 return ownerParagraphs().back().rows.end();
2073 void LyXText::nextRow(ParagraphList::iterator & pit,
2074 RowList::iterator & rit) const
2077 if (rit == pit->rows.end()) {
2079 if (pit == ownerParagraphs().end())
2082 rit = pit->rows.begin();
2087 void LyXText::previousRow(ParagraphList::iterator & pit,
2088 RowList::iterator & rit) const
2090 if (rit != pit->rows.begin())
2093 BOOST_ASSERT(pit != ownerParagraphs().begin());
2095 rit = boost::prior(pit->rows.end());
2100 string LyXText::selectionAsString(Buffer const & buffer, bool label) const
2102 if (!selection.set())
2105 // should be const ...
2106 ParagraphList::iterator startpit = getPar(selection.start);
2107 ParagraphList::iterator endpit = getPar(selection.end);
2108 size_t const startpos = selection.start.pos();
2109 size_t const endpos = selection.end.pos();
2111 if (startpit == endpit)
2112 return startpit->asString(buffer, startpos, endpos, label);
2114 // First paragraph in selection
2116 startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
2118 // The paragraphs in between (if any)
2119 ParagraphList::iterator pit = startpit;
2120 for (++pit; pit != endpit; ++pit)
2121 result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
2123 // Last paragraph in selection
2124 result += endpit->asString(buffer, 0, endpos, label);
2130 int LyXText::parOffset(ParagraphList::iterator pit) const
2132 return std::distance(ownerParagraphs().begin(), pit);
2136 void LyXText::redoParagraphInternal(ParagraphList::iterator pit)
2138 // remove rows of paragraph, keep track of height changes
2139 height -= pit->height;
2147 InsetList::iterator ii = pit->insetlist.begin();
2148 InsetList::iterator iend = pit->insetlist.end();
2149 for (; ii != iend; ++ii) {
2151 MetricsInfo mi(bv(), getFont(pit, ii->pos), workWidth());
2152 ii->inset->metrics(mi, dim);
2155 // rebreak the paragraph
2156 int const ww = workWidth();
2157 for (pos_type z = 0; z < pit->size() + 1; ) {
2159 z = rowBreakPoint(pit, row) + 1;
2161 int const f = fill(pit, row, ww);
2162 unsigned int const w = ww - f;
2163 pit->width = std::max(pit->width, w);
2166 prepareToPrint(pit, row);
2167 setHeightOfRow(pit, row);
2168 row.y_offset(pit->height);
2169 pit->height += row.height();
2170 pit->rows.push_back(row);
2172 height += pit->height;
2173 //lyxerr << "redoParagraph: " << pit->rows.size() << " rows\n";
2177 void LyXText::redoParagraphs(ParagraphList::iterator pit,
2178 ParagraphList::iterator end)
2180 for ( ; pit != end; ++pit)
2181 redoParagraphInternal(pit);
2182 updateRowPositions();
2186 void LyXText::redoParagraph(ParagraphList::iterator pit)
2188 redoParagraphInternal(pit);
2189 updateRowPositions();
2193 void LyXText::fullRebreak()
2195 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2197 selection.cursor = cursor;
2201 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
2203 lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
2204 << " workWidth: " << workWidth() << "\nfont: " << mi.base.font << endl;
2205 //BOOST_ASSERT(mi.base.textwidth);
2208 // rebuild row cache. This recomputes height as well.
2209 redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
2211 width = maxParagraphWidth(ownerParagraphs());
2214 dim.asc = firstRow()->ascent_of_text();
2215 dim.des = height - dim.asc;
2216 dim.wid = std::max(mi.base.textwidth, int(width));
2220 bool LyXText::isLastRow(ParagraphList::iterator pit, Row const & row) const
2222 return row.endpos() >= pit->size()
2223 && boost::next(pit) == ownerParagraphs().end();
2227 bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
2229 return row.pos() == 0 && pit == ownerParagraphs().begin();