1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #include "paragraph.h"
16 #include "bufferparams.h"
22 #include "frontends/LyXView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
25 #include "frontends/screen.h"
26 #include "frontends/WorkArea.h"
27 #include "bufferview_funcs.h"
28 #include "BufferView.h"
30 #include "ParagraphParameters.h"
31 #include "undo_funcs.h"
32 #include "text_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
35 #include "rowpainter.h"
36 #include "lyxrow_funcs.h"
37 #include "metricsinfo.h"
39 #include "insets/insettext.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
47 using namespace lyx::support;
55 using lyx::word_location;
57 using namespace bv_funcs;
59 /// top, right, bottom pixel margin
60 extern int const PAPER_MARGIN = 20;
61 /// margin for changebar
62 extern int const CHANGEBAR_MARGIN = 10;
64 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
66 int bibitemMaxWidth(BufferView *, LyXFont const &);
69 BufferView * LyXText::bv()
71 Assert(bv_owner != 0);
76 BufferView * LyXText::bv() const
78 Assert(bv_owner != 0);
83 void LyXText::updateRowPositions()
85 ParagraphList::iterator pit = ownerParagraphs().begin();
86 ParagraphList::iterator end = ownerParagraphs().end();
87 for (int y = 0; pit != end; ++pit) {
88 RowList::iterator rit = pit->rows.begin();
89 RowList::iterator rend = pit->rows.end();
90 for ( ; rit != rend ; rit = ++rit) {
98 int LyXText::top_y() const
104 void LyXText::top_y(int newy)
107 lyxerr[Debug::GUI] << "changing reference to offset: " << anchor_y_ << endl;
111 int LyXText::workWidth() const
113 return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
117 int LyXText::getRealCursorX() const
120 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
121 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
126 #warning FIXME This function seems to belong outside of LyxText.
127 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
130 if (!Encodings::is_arabic(c))
131 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
132 return c + (0xb0 - '0');
136 unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
137 unsigned char next_char = ' ';
139 pos_type const par_size = par.size();
141 for (pos_type i = pos + 1; i < par_size; ++i) {
142 unsigned char const par_char = par.getChar(i);
143 if (!Encodings::IsComposeChar_arabic(par_char)) {
144 next_char = par_char;
149 if (Encodings::is_arabic(next_char)) {
150 if (Encodings::is_arabic(prev_char) &&
151 !Encodings::is_arabic_special(prev_char))
152 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
154 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
156 if (Encodings::is_arabic(prev_char) &&
157 !Encodings::is_arabic_special(prev_char))
158 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
160 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
164 // This is the comments that some of the warnings below refers to.
165 // There are some issues in this file and I don't think they are
166 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
167 // this is a problem that has been here almost from day one and that a
168 // larger userbase with differenct access patters triggers the bad
169 // behaviour. (segfaults.) What I think happen is: In several places
170 // we store the paragraph in the current cursor and then moves the
171 // cursor. This movement of the cursor will delete paragraph at the
172 // old position if it is now empty. This will make the temporary
173 // pointer to the old cursor paragraph invalid and dangerous to use.
174 // And is some cases this will trigger a segfault. I have marked some
175 // of the cases where this happens with a warning, but I am sure there
176 // are others in this file and in text2.C. There is also a note in
177 // Delete() that you should read. In Delete I store the paragraph->id
178 // instead of a pointer to the paragraph. I am pretty sure this faulty
179 // use of temporary pointers to paragraphs that might have gotten
180 // invalidated (through a cursor movement) before they are used, are
181 // the cause of the strange crashes we get reported often.
183 // It is very tiresom to change this code, especially when it is as
184 // hard to read as it is. Help to fix all the cases where this is done
185 // would be greately appreciated.
189 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
191 if (pos >= pit->size())
194 char const c = pit->getChar(pos);
195 LyXFont const & font = getFont(pit, pos);
196 return singleWidth(pit, pos, c, font);
200 int LyXText::singleWidth(ParagraphList::iterator pit,
201 pos_type pos, char c, LyXFont const & font) const
203 if (pos >= pit->size()) {
204 lyxerr << "in singleWidth(), pos: " << pos << endl;
210 // The most common case is handled first (Asger)
211 if (IsPrintable(c)) {
212 if (font.language()->RightToLeft()) {
213 if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
214 lyxrc.font_norm_type == LyXRC::ISO_10646_1)
215 && font.language()->lang() == "arabic") {
216 if (Encodings::IsComposeChar_arabic(c))
219 c = transformChar(c, *pit, pos);
220 } else if (font.language()->lang() == "hebrew" &&
221 Encodings::IsComposeChar_hebrew(c))
224 return font_metrics::width(c, font);
227 if (c == Paragraph::META_INSET) {
228 InsetOld * tmpinset = pit->getInset(pos);
230 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
231 // Because of the representation as vertical lines
234 return tmpinset->width();
237 if (IsSeparatorChar(c))
239 return font_metrics::width(c, font);
243 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
245 if (bidi_start == -1)
248 return log2vis_list[pos - bidi_start];
252 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
254 if (bidi_start == -1)
257 return vis2log_list[pos - bidi_start];
261 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
263 if (bidi_start == -1)
266 return bidi_levels[pos - bidi_start];
270 bool LyXText::bidi_InRange(lyx::pos_type pos) const
272 return bidi_start == -1 ||
273 (bidi_start <= pos && pos <= bidi_end);
277 void LyXText::computeBidiTables(ParagraphList::iterator pit,
278 Buffer const * buf, RowList::iterator row) const
280 bidi_same_direction = true;
281 if (!lyxrc.rtl_support) {
286 InsetOld * inset = pit->inInset();
287 if (inset && inset->owner() &&
288 inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
293 bidi_start = row->pos();
294 bidi_end = lastPrintablePos(*pit, row);
296 if (bidi_start > bidi_end) {
301 if (bidi_end + 2 - bidi_start >
302 static_cast<pos_type>(log2vis_list.size())) {
304 (bidi_end + 2 - bidi_start < 500) ?
305 500 : 2 * (bidi_end + 2 - bidi_start);
306 log2vis_list.resize(new_size);
307 vis2log_list.resize(new_size);
308 bidi_levels.resize(new_size);
311 vis2log_list[bidi_end + 1 - bidi_start] = -1;
312 log2vis_list[bidi_end + 1 - bidi_start] = -1;
316 pit->isRightToLeftPar(buf->params);
320 pos_type const body_pos = pit->beginningOfBody();
322 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
323 bool is_space = pit->isLineSeparator(lpos);
325 (is_space && lpos + 1 <= bidi_end &&
326 !pit->isLineSeparator(lpos + 1) &&
327 !pit->isNewline(lpos + 1))
329 LyXFont font = pit->getFontSettings(buf->params, pos);
330 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
331 font.number() == LyXFont::ON &&
332 pit->getFontSettings(buf->params, lpos - 1).number()
334 font = pit->getFontSettings(buf->params, lpos);
339 bool new_rtl = font.isVisibleRightToLeft();
340 bool new_rtl0 = font.isRightToLeft();
343 if (lpos == body_pos - 1
344 && row->pos() < body_pos - 1
346 new_level = (rtl_par) ? 1 : 0;
347 new_rtl = new_rtl0 = rtl_par;
349 new_level = (new_rtl) ? 1 : 2;
351 new_level = (rtl_par) ? 2 : 0;
353 if (is_space && new_level >= level) {
359 int new_level2 = new_level;
361 if (level == new_level && rtl0 != new_rtl0) {
363 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
364 } else if (level < new_level) {
365 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
366 if (new_level > rtl_par)
367 bidi_same_direction = false;
369 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
372 bidi_levels[lpos - bidi_start] = new_level;
374 while (level > new_level2) {
375 pos_type old_lpos = stack[--level];
376 int delta = lpos - old_lpos - 1;
379 log2vis_list[lpos - bidi_start] += delta;
380 log2vis_list[old_lpos - bidi_start] += delta;
382 while (level < new_level)
383 stack[level++] = lpos;
387 pos_type const old_lpos = stack[--level];
388 int delta = bidi_end - old_lpos;
391 log2vis_list[old_lpos - bidi_start] += delta;
394 pos_type vpos = bidi_start - 1;
395 for (pos_type lpos = bidi_start;
396 lpos <= bidi_end; ++lpos) {
397 vpos += log2vis_list[lpos - bidi_start];
398 vis2log_list[vpos - bidi_start] = lpos;
399 log2vis_list[lpos - bidi_start] = vpos;
404 // This method requires a previous call to ComputeBidiTables()
405 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
408 if (!lyxrc.rtl_support || pos == 0)
411 if (!bidi_InRange(pos - 1)) {
412 /// This can happen if pos is the first char of a row.
413 /// Returning false in this case is incorrect!
417 bool const rtl = bidi_level(pos - 1) % 2;
418 bool const rtl2 = bidi_InRange(pos)
419 ? bidi_level(pos) % 2
420 : par.isRightToLeftPar(buf->params);
425 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
426 pos_type pos, LyXFont const & font) const
428 if (!lyxrc.rtl_support)
429 return false; // This is just for speedup
431 bool const rtl = font.isVisibleRightToLeft();
432 bool const rtl2 = bidi_InRange(pos)
433 ? bidi_level(pos) % 2
434 : par.isRightToLeftPar(buf->params);
439 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
443 if (row.pos() < pit->size())
444 if (pit->getChar(row.pos()) == Paragraph::META_INSET &&
445 (ins = pit->getInset(row.pos())) &&
446 (ins->needFullRow() || ins->display()))
449 LyXTextClass const & tclass =
450 bv()->buffer()->params.getLyXTextClass();
451 LyXLayout_ptr const & layout = pit->layout();
453 string parindent = layout->parindent;
457 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
459 // this is the way, LyX handles the LaTeX-Environments.
460 // I have had this idea very late, so it seems to be a
461 // later added hack and this is true
462 if (!pit->getDepth()) {
463 if (pit->layout() == tclass.defaultLayout()) {
464 // find the previous same level paragraph
465 if (pit != ownerParagraphs().begin()) {
466 ParagraphList::iterator newpit =
467 depthHook(pit, ownerParagraphs(),
470 newpit->layout()->nextnoindent)
475 // find the next level paragraph
477 ParagraphList::iterator newpar = outerHook(pit,
480 // make a corresponding row. Needed to call leftMargin()
482 // check wether it is a sufficent paragraph
483 if (newpar != ownerParagraphs().end() &&
484 newpar->layout()->isEnvironment()) {
485 x = leftMargin(newpar, Row(newpar->size()));
488 if (newpar != ownerParagraphs().end() &&
489 pit->layout() == tclass.defaultLayout()) {
490 if (newpar->params().noindent())
493 parindent = newpar->layout()->parindent;
499 LyXFont const labelfont = getLabelFont(pit);
500 switch (layout->margintype) {
502 if (!layout->leftmargin.empty()) {
503 x += font_metrics::signedWidth(layout->leftmargin,
504 tclass.defaultfont());
506 if (!pit->getLabelstring().empty()) {
507 x += font_metrics::signedWidth(layout->labelindent,
509 x += font_metrics::width(pit->getLabelstring(),
511 x += font_metrics::width(layout->labelsep, labelfont);
515 x += font_metrics::signedWidth(layout->labelindent, labelfont);
516 // The width of an empty par, even with manual label, should be 0
517 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
518 if (!pit->getLabelWidthString().empty()) {
519 x += font_metrics::width(pit->getLabelWidthString(),
521 x += font_metrics::width(layout->labelsep, labelfont);
526 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
527 / (pit->getDepth() + 4);
529 case MARGIN_FIRST_DYNAMIC:
530 if (layout->labeltype == LABEL_MANUAL) {
531 if (row.pos() >= pit->beginningOfBody()) {
532 x += font_metrics::signedWidth(layout->leftmargin,
535 x += font_metrics::signedWidth(layout->labelindent,
539 // Special case to fix problems with
541 || (layout->labeltype == LABEL_STATIC
542 && layout->latextype == LATEX_ENVIRONMENT
543 && !isFirstInSequence(pit, ownerParagraphs()))) {
544 x += font_metrics::signedWidth(layout->leftmargin,
546 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
547 && layout->labeltype != LABEL_BIBLIO
548 && layout->labeltype !=
549 LABEL_CENTERED_TOP_ENVIRONMENT) {
550 x += font_metrics::signedWidth(layout->labelindent,
552 x += font_metrics::width(layout->labelsep, labelfont);
553 x += font_metrics::width(pit->getLabelstring(),
558 case MARGIN_RIGHT_ADDRESS_BOX:
560 // ok, a terrible hack. The left margin depends on the widest
561 // row in this paragraph. Do not care about footnotes, they
562 // are *NOT* allowed in the LaTeX realisation of this layout.
564 // find the first row of this paragraph
565 RowList::iterator rit = pit->rows.begin();
566 RowList::iterator end = pit->rows.end();
567 int minfill = rit->fill();
568 for ( ; rit != end; ++rit)
569 if (rit->fill() < minfill)
570 minfill = rit->fill();
572 x += font_metrics::signedWidth(layout->leftmargin,
573 tclass.defaultfont());
579 if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
580 LyXLength const len = pit->params().leftIndent();
581 int const tw = inset_owner ?
582 inset_owner->latexTextWidth(bv()) : workWidth();
583 x += len.inPixels(tw);
588 if (pit->params().align() == LYX_ALIGN_LAYOUT)
589 align = layout->align;
591 align = pit->params().align();
593 // set the correct parindent
594 if (row.pos() == 0) {
595 if ((layout->labeltype == LABEL_NO_LABEL
596 || layout->labeltype == LABEL_TOP_ENVIRONMENT
597 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
598 || (layout->labeltype == LABEL_STATIC
599 && layout->latextype == LATEX_ENVIRONMENT
600 && !isFirstInSequence(pit, ownerParagraphs())))
601 && align == LYX_ALIGN_BLOCK
602 && !pit->params().noindent()
603 // in tabulars and ert paragraphs are never indented!
604 && (!pit->inInset() || !pit->inInset()->owner() ||
605 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
606 pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
607 && (pit->layout() != tclass.defaultLayout() ||
608 bv()->buffer()->params.paragraph_separation ==
609 BufferParams::PARSEP_INDENT)) {
610 x += font_metrics::signedWidth(parindent,
611 tclass.defaultfont());
612 } else if (layout->labeltype == LABEL_BIBLIO) {
613 // ale970405 Right width for bibitems
614 x += bibitemMaxWidth(bv(), tclass.defaultfont());
622 int LyXText::rightMargin(ParagraphList::iterator pit,
623 Buffer const & buf, Row const & row) const
627 if (row.pos() < pit->size())
628 if ((pit->getChar(row.pos()) == Paragraph::META_INSET) &&
629 (ins = pit->getInset(row.pos())) &&
630 (ins->needFullRow() || ins->display()))
633 LyXTextClass const & tclass = buf.params.getLyXTextClass();
634 LyXLayout_ptr const & layout = pit->layout();
637 + font_metrics::signedWidth(tclass.rightmargin(),
638 tclass.defaultfont());
639 + font_metrics::signedWidth(layout->rightmargin,
640 tclass.defaultfont())
641 * 4 / (pit->getDepth() + 4);
645 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
647 if (pit->layout()->margintype == MARGIN_MANUAL) {
649 tmprow.pos(pit->size());
650 // return the beginning of the body
651 return leftMargin(pit, tmprow);
654 // LabelEnd is only needed if the layout
655 // fills a flushleft label.
662 // this needs special handling - only newlines count as a break point
663 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
665 for (; i < par.size(); ++i) {
666 if (par.isNewline(i))
676 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
677 Row const & row) const
679 // maximum pixel width of a row.
680 int width = workWidth() - rightMargin(pit, *bv()->buffer(), row);
682 // inset->textWidth() returns -1 via workWidth(),
687 LyXLayout_ptr const & layout = pit->layout();
689 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
690 return addressBreakPoint(row.pos(), *pit);
692 pos_type const pos = row.pos();
693 pos_type const body_pos = pit->beginningOfBody();
694 pos_type const last = pit->size();
695 pos_type point = last;
700 // Now we iterate through until we reach the right margin
701 // or the end of the par, then choose the possible break
704 int const left = leftMargin(pit, row);
707 // pixel width since last breakpoint
709 bool fullrow = false;
713 // We re-use the font resolution for the entire font span when possible
714 LyXFont font = getFont(pit, i);
715 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
717 for (; i < last; ++i) {
718 if (pit->isNewline(i)) {
723 char const c = pit->getChar(i);
724 if (i > endPosOfFontSpan) {
725 font = getFont(pit, i);
726 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
731 // add the auto-hfill from label end to the body
732 if (body_pos && i == body_pos) {
733 thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
734 if (pit->isLineSeparator(i - 1))
735 thiswidth -= singleWidth(pit, i - 1);
736 int left_margin = labelEnd(pit, row);
737 if (thiswidth + x < left_margin)
738 thiswidth = left_margin - x;
739 thiswidth += singleWidth(pit, i, c, font);
741 thiswidth = singleWidth(pit, i, c, font);
745 chunkwidth += thiswidth;
747 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
748 fullrow = in && (in->display() || in->needFullRow());
750 // break before a character that will fall off
751 // the right of the row
753 // if no break before or we are at an inset
754 // that will take up a row, break here
755 if (point == last || fullrow || chunkwidth >= (width - left)) {
764 if (!in || in->isChar()) {
765 // some insets are line separators too
766 if (pit->isLineSeparator(i)) {
776 // full row insets start at a new row
778 if (pos < last - 1) {
780 if (pit->isLineSeparator(i + 1))
783 // to avoid extra rows
793 if (point == last && x >= width) {
794 // didn't find one, break at the point we reached the edge
796 } else if (i == last && x < width) {
797 // found one, but we fell off the end of the par, so prefer
802 // manual labels cannot be broken in LaTeX. But we
803 // want to make our on-screen rendering of footnotes
805 if (!fullrow && body_pos && point < body_pos)
806 point = body_pos - 1;
812 // returns the minimum space a row needs on the screen in pixel
813 int LyXText::fill(ParagraphList::iterator pit,
814 RowList::iterator row, int paper_width) const
820 // get the pure distance
821 pos_type const last = lastPrintablePos(*pit, row);
823 LyXLayout_ptr const & layout = pit->layout();
825 // special handling of the right address boxes
826 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
827 int const tmpfill = row->fill();
828 row->fill(0); // the minfill in MarginLeft()
829 w = leftMargin(pit, *row);
832 w = leftMargin(pit, *row);
834 pos_type const body_pos = pit->beginningOfBody();
835 pos_type i = row->pos();
837 if (! pit->empty() && i <= last) {
838 // We re-use the font resolution for the entire span when possible
839 LyXFont font = getFont(pit, i);
840 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
842 if (body_pos > 0 && i == body_pos) {
843 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
844 if (pit->isLineSeparator(i - 1))
845 w -= singleWidth(pit, i - 1);
846 int left_margin = labelEnd(pit, *row);
850 char const c = pit->getChar(i);
851 if (IsPrintable(c) && i > endPosOfFontSpan) {
852 // We need to get the next font
853 font = getFont(pit, i);
854 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
856 w += singleWidth(pit, i, c, font);
860 if (body_pos > 0 && body_pos > last) {
861 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
862 if (last >= 0 && pit->isLineSeparator(last))
863 w -= singleWidth(pit, last);
864 int const left_margin = labelEnd(pit, *row);
869 int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
871 // If this case happens, it means that our calculation
872 // of the widths of the chars when we do rowBreakPoint()
873 // went wrong for some reason. Typically in list bodies.
874 // Things just about hobble on anyway, though you'll end
875 // up with a "fill_separator" less than zero, which corresponds
876 // to inter-word spacing being too small. Hopefully this problem
877 // will die when the label hacks die.
878 if (lyxerr.debugging() && fill < 0) {
879 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
880 << " w " << w << " paper_width " << paper_width
881 << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
887 // returns the minimum space a manual label needs on the screen in pixel
888 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
890 pos_type last = pit->beginningOfBody();
894 // -1 because a label ends either with a space that is in the label,
895 // or with the beginning of a footnote that is outside the label.
898 // a separator at this end does not count
899 if (pit->isLineSeparator(last))
903 pos_type i = row.pos();
905 w += singleWidth(pit, i);
910 string const & labwidstr = pit->params().labelWidthString();
911 if (!labwidstr.empty()) {
912 LyXFont const labfont = getLabelFont(pit);
913 int const labwidth = font_metrics::width(labwidstr, labfont);
914 fill = max(labwidth - w, 0);
921 LColor::color LyXText::backgroundColor() const
924 return inset_owner->backgroundColor();
926 return LColor::background;
930 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
932 // get the maximum ascent and the maximum descent
933 double layoutasc = 0;
934 double layoutdesc = 0;
937 // ok, let us initialize the maxasc and maxdesc value.
938 // Only the fontsize count. The other properties
939 // are taken from the layoutfont. Nicer on the screen :)
940 LyXLayout_ptr const & layout = pit->layout();
942 // as max get the first character of this row then it can increase but not
943 // decrease the height. Just some point to start with so we don't have to
944 // do the assignment below too often.
945 LyXFont font = getFont(pit, rit->pos());
946 LyXFont::FONT_SIZE const tmpsize = font.size();
947 font = getLayoutFont(pit);
948 LyXFont::FONT_SIZE const size = font.size();
949 font.setSize(tmpsize);
951 LyXFont labelfont = getLabelFont(pit);
953 double spacing_val = 1.0;
954 if (!pit->params().spacing().isDefault())
955 spacing_val = pit->params().spacing().getValue();
957 spacing_val = bv()->buffer()->params.spacing.getValue();
958 //lyxerr << "spacing_val = " << spacing_val << endl;
960 int maxasc = int(font_metrics::maxAscent(font) *
961 layout->spacing.getValue() * spacing_val);
962 int maxdesc = int(font_metrics::maxDescent(font) *
963 layout->spacing.getValue() * spacing_val);
965 pos_type const pos_end = lastPos(*pit, rit);
970 // We re-use the font resolution for the entire font span when possible
971 LyXFont font = getFont(pit, rit->pos());
972 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
975 Paragraph const & par = *pit;
977 // Check if any insets are larger
978 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
979 // Manual inlined optimised version of common case of
980 // "maxwidth += singleWidth(pit, pos);"
981 char const c = par.getChar(pos);
983 if (IsPrintable(c)) {
984 if (pos > endPosOfFontSpan) {
985 // We need to get the next font
986 font = getFont(pit, pos);
987 endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
989 if (! font.language()->RightToLeft()) {
990 maxwidth += font_metrics::width(c, font);
992 // Fall-back to normal case
993 maxwidth += singleWidth(pit, pos, c, font);
994 // And flush font cache
995 endPosOfFontSpan = 0;
998 // Special handling of insets - are any larger?
999 if (par.isInset(pos)) {
1000 InsetOld const * tmpinset = par.getInset(pos);
1002 #if 1 // this is needed for deep update on initialitation
1003 #warning inset->update FIXME
1004 //tmpinset->update(bv());
1005 LyXFont const tmpfont = getFont(pit, pos);
1007 MetricsInfo mi(bv(), tmpfont, workWidth());
1008 tmpinset->metrics(mi, dim);
1009 maxwidth += dim.wid;
1010 maxasc = max(maxasc, dim.asc);
1011 maxdesc = max(maxdesc, dim.des);
1013 maxwidth += tmpinset->width();
1014 maxasc = max(maxasc, tmpinset->ascent());
1015 maxdesc = max(maxdesc, tmpinset->descent());
1019 // Fall-back to normal case
1020 maxwidth += singleWidth(pit, pos, c, font);
1021 // And flush font cache
1022 endPosOfFontSpan = 0;
1028 // Check if any custom fonts are larger (Asger)
1029 // This is not completely correct, but we can live with the small,
1030 // cosmetic error for now.
1031 LyXFont::FONT_SIZE maxsize =
1032 pit->highestFontInRange(rit->pos(), pos_end, size);
1033 if (maxsize > font.size()) {
1034 font.setSize(maxsize);
1035 maxasc = max(maxasc, font_metrics::maxAscent(font));
1036 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
1039 // This is nicer with box insets:
1043 rit->ascent_of_text(maxasc);
1045 // is it a top line?
1048 // some parksips VERY EASY IMPLEMENTATION
1049 if (bv()->buffer()->params.paragraph_separation ==
1050 BufferParams::PARSEP_SKIP)
1052 if (layout->isParagraph()
1053 && pit->getDepth() == 0
1054 && pit != ownerParagraphs().begin())
1056 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1057 } else if (pit != ownerParagraphs().begin() &&
1058 boost::prior(pit)->layout()->isParagraph() &&
1059 boost::prior(pit)->getDepth() == 0)
1061 // is it right to use defskip here too? (AS)
1062 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1067 if (pit == ownerParagraphs().begin() && !isInInset())
1068 maxasc += PAPER_MARGIN;
1070 // add the vertical spaces, that the user added
1071 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1073 // do not forget the DTP-lines!
1074 // there height depends on the font of the nearest character
1075 if (pit->params().lineTop())
1077 maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1078 // and now the pagebreaks
1079 if (pit->params().pagebreakTop())
1080 maxasc += 3 * defaultRowHeight();
1082 if (pit->params().startOfAppendix())
1083 maxasc += 3 * defaultRowHeight();
1085 // This is special code for the chapter, since the label of this
1086 // layout is printed in an extra row
1087 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1088 && bv()->buffer()->params.secnumdepth >= 0)
1090 float spacing_val = 1.0;
1091 if (!pit->params().spacing().isDefault()) {
1092 spacing_val = pit->params().spacing().getValue();
1094 spacing_val = bv()->buffer()->params.spacing.getValue();
1097 labeladdon = int(font_metrics::maxDescent(labelfont) *
1098 layout->spacing.getValue() *
1100 + int(font_metrics::maxAscent(labelfont) *
1101 layout->spacing.getValue() *
1105 // special code for the top label
1106 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1107 || layout->labeltype == LABEL_BIBLIO
1108 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1109 && isFirstInSequence(pit, ownerParagraphs())
1110 && !pit->getLabelstring().empty())
1112 float spacing_val = 1.0;
1113 if (!pit->params().spacing().isDefault()) {
1114 spacing_val = pit->params().spacing().getValue();
1116 spacing_val = bv()->buffer()->params.spacing.getValue();
1120 (font_metrics::maxAscent(labelfont) +
1121 font_metrics::maxDescent(labelfont)) *
1122 layout->spacing.getValue() *
1124 + layout->topsep * defaultRowHeight()
1125 + layout->labelbottomsep * defaultRowHeight());
1128 // And now the layout spaces, for example before and after
1129 // a section, or between the items of a itemize or enumerate
1132 if (!pit->params().pagebreakTop()) {
1133 ParagraphList::iterator prev =
1134 depthHook(pit, ownerParagraphs(),
1136 if (prev != pit && prev->layout() == layout &&
1137 prev->getDepth() == pit->getDepth() &&
1138 prev->getLabelWidthString() == pit->getLabelWidthString())
1140 layoutasc = (layout->itemsep * defaultRowHeight());
1141 } else if (rit != firstRow()) {
1142 tmptop = layout->topsep;
1144 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1145 // tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1148 layoutasc = (tmptop * defaultRowHeight());
1149 } else if (pit->params().lineTop()) {
1150 tmptop = layout->topsep;
1153 layoutasc = (tmptop * defaultRowHeight());
1156 prev = outerHook(pit, ownerParagraphs());
1157 if (prev != ownerParagraphs().end()) {
1158 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1159 } else if (pit != ownerParagraphs().begin()) {
1160 ParagraphList::iterator prior_pit = boost::prior(pit);
1161 if (prior_pit->getDepth() != 0 ||
1162 prior_pit->layout() == layout) {
1163 maxasc += int(layout->parsep * defaultRowHeight());
1169 // is it a bottom line?
1170 if (boost::next(rit) == pit->rows.end()) {
1171 // the bottom margin
1172 ParagraphList::iterator nextpit = boost::next(pit);
1173 if (nextpit == ownerParagraphs().end() && !isInInset())
1174 maxdesc += PAPER_MARGIN;
1176 // add the vertical spaces, that the user added
1177 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1179 // do not forget the DTP-lines!
1180 // there height depends on the font of the nearest character
1181 if (pit->params().lineBottom())
1182 maxdesc += 2 * font_metrics::ascent('x',
1183 getFont(pit, max(pos_type(0), pit->size() - 1)));
1185 // and now the pagebreaks
1186 if (pit->params().pagebreakBottom())
1187 maxdesc += 3 * defaultRowHeight();
1189 // and now the layout spaces, for example before and after
1190 // a section, or between the items of a itemize or enumerate
1192 if (!pit->params().pagebreakBottom()
1193 && nextpit != ownerParagraphs().end()) {
1194 ParagraphList::iterator comparepit = pit;
1198 if (comparepit->getDepth() > nextpit->getDepth()) {
1199 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1200 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1201 if (comparepit->layout()!= nextpit->layout()
1202 || nextpit->getLabelWidthString() !=
1203 comparepit->getLabelWidthString())
1205 unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1207 if (unusual > usual)
1208 layoutdesc = unusual;
1211 } else if (comparepit->getDepth() == nextpit->getDepth()) {
1213 if (comparepit->layout() != nextpit->layout()
1214 || nextpit->getLabelWidthString() !=
1215 comparepit->getLabelWidthString())
1216 layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1221 // incalculate the layout spaces
1222 maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1223 maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1225 // calculate the new height of the text
1226 height -= rit->height();
1228 rit->height(maxasc + maxdesc + labeladdon);
1229 rit->baseline(maxasc + labeladdon);
1231 height += rit->height();
1233 rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1236 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1240 rit->width(maxwidth);
1242 prepareToPrint(pit, rit, x, dummy, dummy, dummy, false);
1245 rit->width(int(maxwidth + x));
1247 width = max(0, workWidth());
1248 RowList::iterator rit = firstRow();
1249 RowList::iterator end = endRow();
1250 ParagraphList::iterator it = ownerParagraphs().begin();
1251 while (rit != end) {
1252 if (rit->width() > width)
1253 width = rit->width();
1260 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1262 // allow only if at start or end, or all previous is new text
1263 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1264 && cursor.par()->isChangeEdited(0, cursor.pos()))
1267 LyXTextClass const & tclass =
1268 bv()->buffer()->params.getLyXTextClass();
1269 LyXLayout_ptr const & layout = cursor.par()->layout();
1271 // this is only allowed, if the current paragraph is not empty or caption
1272 // and if it has not the keepempty flag active
1273 if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1274 && layout->labeltype != LABEL_SENSITIVE)
1277 recordUndo(bv(), Undo::ATOMIC, cursor.par());
1279 // Always break behind a space
1281 // It is better to erase the space (Dekel)
1282 if (cursor.pos() < cursor.par()->size()
1283 && cursor.par()->isLineSeparator(cursor.pos()))
1284 cursor.par()->erase(cursor.pos());
1286 // break the paragraph
1290 keep_layout = layout->isEnvironment();
1292 // we need to set this before we insert the paragraph. IMO the
1293 // breakParagraph call should return a bool if it inserts the
1294 // paragraph before or behind and we should react on that one
1295 // but we can fix this in 1.3.0 (Jug 20020509)
1296 bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1297 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1298 cursor.pos(), keep_layout);
1300 #warning Trouble Point! (Lgb)
1301 // When ::breakParagraph is called from within an inset we must
1302 // ensure that the correct ParagraphList is used. Today that is not
1303 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1304 ParagraphList::iterator next_par = boost::next(cursor.par());
1306 // well this is the caption hack since one caption is really enough
1307 if (layout->labeltype == LABEL_SENSITIVE) {
1309 // set to standard-layout
1310 cursor.par()->applyLayout(tclass.defaultLayout());
1312 // set to standard-layout
1313 next_par->applyLayout(tclass.defaultLayout());
1316 // if the cursor is at the beginning of a row without prior newline,
1318 // This touches only the screen-update. Otherwise we would may have
1319 // an empty row on the screen
1320 if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1321 && !cursor.par()->isNewline(cursor.pos() - 1))
1326 while (!next_par->empty() && next_par->isNewline(0))
1330 redoParagraph(cursor.par());
1331 redoParagraph(next_par);
1333 // This check is necessary. Otherwise the new empty paragraph will
1334 // be deleted automatically. And it is more friendly for the user!
1335 if (cursor.pos() || isempty)
1336 setCursor(next_par, 0);
1338 setCursor(cursor.par(), 0);
1342 // convenience function
1343 void LyXText::redoParagraph()
1346 redoParagraph(cursor.par());
1347 setCursorIntern(cursor.par(), cursor.pos());
1351 // insert a character, moves all the following breaks in the
1352 // same Paragraph one to the right and make a rebreak
1353 void LyXText::insertChar(char c)
1355 recordUndo(bv(), Undo::INSERT, cursor.par());
1357 // When the free-spacing option is set for the current layout,
1358 // disable the double-space checking
1360 bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1361 cursor.par()->isFreeSpacing();
1363 if (lyxrc.auto_number) {
1364 static string const number_operators = "+-/*";
1365 static string const number_unary_operators = "+-";
1366 static string const number_seperators = ".,:";
1368 if (current_font.number() == LyXFont::ON) {
1369 if (!IsDigit(c) && !contains(number_operators, c) &&
1370 !(contains(number_seperators, c) &&
1371 cursor.pos() >= 1 &&
1372 cursor.pos() < cursor.par()->size() &&
1373 getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1374 getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1376 number(bv()); // Set current_font.number to OFF
1377 } else if (IsDigit(c) &&
1378 real_current_font.isVisibleRightToLeft()) {
1379 number(bv()); // Set current_font.number to ON
1381 if (cursor.pos() > 0) {
1382 char const c = cursor.par()->getChar(cursor.pos() - 1);
1383 if (contains(number_unary_operators, c) &&
1384 (cursor.pos() == 1 ||
1385 cursor.par()->isSeparator(cursor.pos() - 2) ||
1386 cursor.par()->isNewline(cursor.pos() - 2))
1392 } else if (contains(number_seperators, c) &&
1393 cursor.pos() >= 2 &&
1396 cursor.pos() - 2).number() == LyXFont::ON) {
1407 // First check, if there will be two blanks together or a blank at
1408 // the beginning of a paragraph.
1409 // I decided to handle blanks like normal characters, the main
1410 // difference are the special checks when calculating the row.fill
1411 // (blank does not count at the end of a row) and the check here
1413 // The bug is triggered when we type in a description environment:
1414 // The current_font is not changed when we go from label to main text
1415 // and it should (along with realtmpfont) when we type the space.
1416 // CHECK There is a bug here! (Asger)
1418 // store the current font. This is because of the use of cursor
1419 // movements. The moving cursor would refresh the current font
1420 LyXFont realtmpfont = real_current_font;
1421 LyXFont rawtmpfont = current_font;
1423 if (!freeSpacing && IsLineSeparatorChar(c)) {
1424 if ((cursor.pos() > 0
1425 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1426 || (cursor.pos() > 0
1427 && cursor.par()->isNewline(cursor.pos() - 1))
1428 || (cursor.pos() == 0)) {
1429 static bool sent_space_message = false;
1430 if (!sent_space_message) {
1431 if (cursor.pos() == 0)
1432 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1434 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1435 sent_space_message = true;
1442 // Here case LyXText::InsertInset already inserted the character
1443 if (c != Paragraph::META_INSET)
1444 cursor.par()->insertChar(cursor.pos(), c);
1446 setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1448 current_font = rawtmpfont;
1449 real_current_font = realtmpfont;
1450 redoParagraph(cursor.par());
1451 setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1457 void LyXText::charInserted()
1459 // Here we could call finishUndo for every 20 characters inserted.
1460 // This is from my experience how emacs does it. (Lgb)
1461 static unsigned int counter;
1471 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1472 RowList::iterator const rit) const
1474 double w = rit->fill();
1475 double fill_hfill = 0;
1476 double fill_label_hfill = 0;
1477 double fill_separator = 0;
1481 pit->isRightToLeftPar(bv()->buffer()->params);
1483 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1485 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1487 // is there a manual margin with a manual label
1488 LyXLayout_ptr const & layout = pit->layout();
1490 if (layout->margintype == MARGIN_MANUAL
1491 && layout->labeltype == LABEL_MANUAL) {
1492 /// We might have real hfills in the label part
1493 int nlh = numberOfLabelHfills(*pit, rit);
1495 // A manual label par (e.g. List) has an auto-hfill
1496 // between the label text and the body of the
1498 // But we don't want to do this auto hfill if the par
1503 if (nlh && !pit->getLabelWidthString().empty()) {
1504 fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1508 // are there any hfills in the row?
1509 int const nh = numberOfHfills(*pit, rit);
1513 fill_hfill = w / nh;
1514 // we don't have to look at the alignment if it is ALIGN_LEFT and
1515 // if the row is already larger then the permitted width as then
1516 // we force the LEFT_ALIGN'edness!
1517 } else if (int(rit->width()) < workWidth()) {
1518 // is it block, flushleft or flushright?
1519 // set x how you need it
1521 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1522 align = layout->align;
1524 align = pit->params().align();
1527 // center displayed insets
1528 InsetOld * inset = 0;
1529 if (rit->pos() < pit->size()
1530 && pit->isInset(rit->pos())
1531 && (inset = pit->getInset(rit->pos()))
1532 && (inset->display())) // || (inset->scroll() < 0)))
1533 align = (inset->lyxCode() == InsetOld::MATHMACRO_CODE)
1534 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1535 // ERT insets should always be LEFT ALIGNED on screen
1536 inset = pit->inInset();
1537 if (inset && inset->owner() &&
1538 inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1540 align = LYX_ALIGN_LEFT;
1544 case LYX_ALIGN_BLOCK:
1546 int const ns = numberOfSeparators(*pit, rit);
1547 RowList::iterator next_row = boost::next(rit);
1549 && next_row != pit->rows.end()
1550 && !pit->isNewline(next_row->pos() - 1)
1551 && !(pit->isInset(next_row->pos())
1552 && pit->getInset(next_row->pos())
1553 && pit->getInset(next_row->pos())->display())
1555 fill_separator = w / ns;
1556 } else if (is_rtl) {
1561 case LYX_ALIGN_RIGHT:
1564 case LYX_ALIGN_CENTER:
1570 computeBidiTables(pit, bv()->buffer(), rit);
1572 pos_type body_pos = pit->beginningOfBody();
1573 pos_type last = lastPos(*pit, rit);
1576 (body_pos - 1 > last ||
1577 !pit->isLineSeparator(body_pos - 1))) {
1578 x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1579 if (body_pos - 1 <= last)
1580 x += fill_label_hfill;
1584 rit->fill_hfill(fill_hfill);
1585 rit->fill_label_hfill(fill_label_hfill);
1586 rit->fill_separator(fill_separator);
1591 // important for the screen
1594 // the cursor set functions have a special mechanism. When they
1595 // realize, that you left an empty paragraph, they will delete it.
1596 // They also delete the corresponding row
1598 void LyXText::cursorRightOneWord()
1600 ::cursorRightOneWord(cursor, ownerParagraphs());
1601 setCursor(cursor.par(), cursor.pos());
1605 // Skip initial whitespace at end of word and move cursor to *start*
1606 // of prior word, not to end of next prior word.
1607 void LyXText::cursorLeftOneWord()
1609 LyXCursor tmpcursor = cursor;
1610 ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1611 setCursor(tmpcursor.par(), tmpcursor.pos());
1615 void LyXText::selectWord(word_location loc)
1617 LyXCursor from = cursor;
1619 ::getWord(from, to, loc, ownerParagraphs());
1621 setCursor(from.par(), from.pos());
1624 selection.cursor = cursor;
1625 setCursor(to.par(), to.pos());
1630 // Select the word currently under the cursor when no
1631 // selection is currently set
1632 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1634 if (!selection.set()) {
1636 return selection.set();
1642 void LyXText::acceptChange()
1644 if (!selection.set() && cursor.par()->size())
1647 if (selection.start.par() == selection.end.par()) {
1648 LyXCursor & startc = selection.start;
1649 LyXCursor & endc = selection.end;
1650 recordUndo(bv(), Undo::INSERT, startc.par());
1651 startc.par()->acceptChange(startc.pos(), endc.pos());
1654 redoParagraph(startc.par());
1655 setCursorIntern(startc.par(), 0);
1657 #warning handle multi par selection
1661 void LyXText::rejectChange()
1663 if (!selection.set() && cursor.par()->size())
1666 if (selection.start.par() == selection.end.par()) {
1667 LyXCursor & startc = selection.start;
1668 LyXCursor & endc = selection.end;
1669 recordUndo(bv(), Undo::INSERT, startc.par());
1670 startc.par()->rejectChange(startc.pos(), endc.pos());
1673 redoParagraph(startc.par());
1674 setCursorIntern(startc.par(), 0);
1676 #warning handle multi par selection
1680 // This function is only used by the spellchecker for NextWord().
1681 // It doesn't handle LYX_ACCENTs and probably never will.
1683 LyXText::selectNextWordToSpellcheck(float & value)
1685 if (the_locking_inset) {
1686 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1687 if (!word.word().empty()) {
1688 value += float(cursor.y());
1689 value /= float(height);
1692 // we have to go on checking so move cursor to the next char
1693 if (cursor.pos() == cursor.par()->size()) {
1694 if (boost::next(cursor.par()) == ownerParagraphs().end())
1696 cursor.par(boost::next(cursor.par()));
1699 cursor.pos(cursor.pos() + 1);
1701 ParagraphList::iterator tmppit = cursor.par();
1703 // If this is not the very first word, skip rest of
1704 // current word because we are probably in the middle
1705 // of a word if there is text here.
1706 if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1707 while (cursor.pos() < cursor.par()->size()
1708 && cursor.par()->isLetter(cursor.pos()))
1709 cursor.pos(cursor.pos() + 1);
1712 // Now, skip until we have real text (will jump paragraphs)
1714 ParagraphList::iterator cpit = cursor.par();
1715 pos_type const cpos(cursor.pos());
1717 if (cpos == cpit->size()) {
1718 if (boost::next(cpit) != ownerParagraphs().end()) {
1719 cursor.par(boost::next(cpit));
1726 bool const is_good_inset = cpit->isInset(cpos)
1727 && cpit->getInset(cpos)->allowSpellcheck();
1729 if (!isDeletedText(*cpit, cpos)
1730 && (is_good_inset || cpit->isLetter(cpos)))
1733 cursor.pos(cpos + 1);
1736 // now check if we hit an inset so it has to be a inset containing text!
1737 if (cursor.pos() < cursor.par()->size() &&
1738 cursor.par()->isInset(cursor.pos())) {
1740 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1741 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1742 // now call us again to do the above trick
1743 // but obviously we have to start from down below ;)
1744 return bv()->text->selectNextWordToSpellcheck(value);
1747 // Update the value if we changed paragraphs
1748 if (cursor.par() != tmppit) {
1749 setCursor(cursor.par(), cursor.pos());
1750 value = float(cursor.y())/float(height);
1753 // Start the selection from here
1754 selection.cursor = cursor;
1756 string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1757 // and find the end of the word (insets like optional hyphens
1758 // and ligature break are part of a word)
1759 while (cursor.pos() < cursor.par()->size()
1760 && cursor.par()->isLetter(cursor.pos())
1761 && !isDeletedText(*cursor.par(), cursor.pos()))
1762 cursor.pos(cursor.pos() + 1);
1764 // Finally, we copy the word to a string and return it
1766 if (selection.cursor.pos() < cursor.pos()) {
1768 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1769 if (!cursor.par()->isInset(i))
1770 str += cursor.par()->getChar(i);
1773 return WordLangTuple(str, lang_code);
1777 // This one is also only for the spellchecker
1778 void LyXText::selectSelectedWord()
1780 if (the_locking_inset) {
1781 the_locking_inset->selectSelectedWord(bv());
1784 // move cursor to the beginning
1785 setCursor(selection.cursor.par(), selection.cursor.pos());
1787 // set the sel cursor
1788 selection.cursor = cursor;
1790 // now find the end of the word
1791 while (cursor.pos() < cursor.par()->size()
1792 && cursor.par()->isLetter(cursor.pos()))
1793 cursor.pos(cursor.pos() + 1);
1795 setCursor(cursor.par(), cursor.pos());
1797 // finally set the selection
1802 // Delete from cursor up to the end of the current or next word.
1803 void LyXText::deleteWordForward()
1805 if (cursor.par()->empty())
1808 LyXCursor tmpcursor = cursor;
1809 selection.set(true); // to avoid deletion
1810 cursorRightOneWord();
1811 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1812 selection.cursor = cursor;
1816 // Great, CutSelection() gets rid of multiple spaces.
1817 cutSelection(true, false);
1822 // Delete from cursor to start of current or prior word.
1823 void LyXText::deleteWordBackward()
1825 if (cursor.par()->empty())
1828 LyXCursor tmpcursor = cursor;
1829 selection.set(true); // to avoid deletion
1830 cursorLeftOneWord();
1831 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1832 selection.cursor = cursor;
1835 cutSelection(true, false);
1840 // Kill to end of line.
1841 void LyXText::deleteLineForward()
1843 if (cursor.par()->empty())
1844 // Paragraph is empty, so we just go to the right
1847 LyXCursor tmpcursor = cursor;
1848 // We can't store the row over a regular setCursor
1849 // so we set it to 0 and reset it afterwards.
1850 selection.set(true); // to avoid deletion
1852 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1853 selection.cursor = cursor;
1856 // What is this test for ??? (JMarc)
1857 if (!selection.set()) {
1858 deleteWordForward();
1860 cutSelection(true, false);
1866 void LyXText::changeCase(LyXText::TextCase action)
1871 if (selection.set()) {
1872 from = selection.start;
1876 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1877 setCursor(to.par(), to.pos() + 1);
1880 recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1882 pos_type pos = from.pos();
1883 ParagraphList::iterator pit = from.par();
1885 while (pit != ownerParagraphs().end() &&
1886 (pos != to.pos() || pit != to.par())) {
1887 if (pos == pit->size()) {
1892 unsigned char c = pit->getChar(pos);
1893 if (!IsInsetChar(c)) {
1895 case text_lowercase:
1898 case text_capitalization:
1900 action = text_lowercase;
1902 case text_uppercase:
1908 pit->setChar(pos, c);
1914 void LyXText::Delete()
1916 // this is a very easy implementation
1918 LyXCursor old_cursor = cursor;
1919 int const old_cur_par_id = old_cursor.par()->id();
1920 int const old_cur_par_prev_id =
1921 (old_cursor.par() != ownerParagraphs().begin() ?
1922 boost::prior(old_cursor.par())->id() : -1);
1924 // just move to the right
1927 // CHECK Look at the comment here.
1928 // This check is not very good...
1929 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1930 // and that can very well delete the par or par->previous in
1931 // old_cursor. Will a solution where we compare paragraph id's
1933 if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1934 == old_cur_par_prev_id
1935 && cursor.par()->id() != old_cur_par_id) {
1936 // delete-empty-paragraph-mechanism has done it
1940 // if you had success make a backspace
1941 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1942 LyXCursor tmpcursor = cursor;
1943 // to make sure undo gets the right cursor position
1944 cursor = old_cursor;
1945 recordUndo(bv(), Undo::DELETE, cursor.par());
1952 void LyXText::backspace()
1954 // Get the font that is used to calculate the baselineskip
1955 pos_type lastpos = cursor.par()->size();
1957 if (cursor.pos() == 0) {
1958 // The cursor is at the beginning of a paragraph,
1959 // so the the backspace will collapse two paragraphs into one.
1961 // but it's not allowed unless it's new
1962 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1965 // we may paste some paragraphs
1967 // is it an empty paragraph?
1970 || (lastpos == 1 && cursor.par()->isSeparator(0))) {
1971 // This is an empty paragraph and we delete it just
1972 // by moving the cursor one step
1973 // left and let the DeleteEmptyParagraphMechanism
1974 // handle the actual deletion of the paragraph.
1976 if (cursor.par() != ownerParagraphs().begin()) {
1977 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1978 if (cursor.par()->layout() == tmppit->layout()
1979 && cursor.par()->getAlign() == tmppit->getAlign()) {
1980 // Inherit bottom DTD from the paragraph below.
1981 // (the one we are deleting)
1982 tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1983 tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1984 tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1989 // the layout things can change the height of a row !
1990 setHeightOfRow(cursor.par(), cursorRow());
1995 if (cursor.par() != ownerParagraphs().begin()) {
1996 recordUndo(bv(), Undo::DELETE,
1997 boost::prior(cursor.par()),
2001 ParagraphList::iterator tmppit = cursor.par();
2002 // We used to do cursorLeftIntern() here, but it is
2003 // not a good idea since it triggers the auto-delete
2004 // mechanism. So we do a cursorLeftIntern()-lite,
2005 // without the dreaded mechanism. (JMarc)
2006 if (cursor.par() != ownerParagraphs().begin()) {
2007 // steps into the above paragraph.
2008 setCursorIntern(boost::prior(cursor.par()),
2009 boost::prior(cursor.par())->size(),
2013 // Pasting is not allowed, if the paragraphs have different
2014 // layout. I think it is a real bug of all other
2015 // word processors to allow it. It confuses the user.
2016 //Correction: Pasting is always allowed with standard-layout
2017 LyXTextClass const & tclass =
2018 bv()->buffer()->params.getLyXTextClass();
2020 if (cursor.par() != tmppit
2021 && (cursor.par()->layout() == tmppit->layout()
2022 || tmppit->layout() == tclass.defaultLayout())
2023 && cursor.par()->getAlign() == tmppit->getAlign()) {
2024 mergeParagraph(bv()->buffer()->params,
2025 bv()->buffer()->paragraphs, cursor.par());
2027 if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
2028 cursor.pos(cursor.pos() - 1);
2030 // the row may have changed, block, hfills etc.
2032 setCursor(cursor.par(), cursor.pos(), false);
2035 // this is the code for a normal backspace, not pasting
2037 recordUndo(bv(), Undo::DELETE, cursor.par());
2038 // We used to do cursorLeftIntern() here, but it is
2039 // not a good idea since it triggers the auto-delete
2040 // mechanism. So we do a cursorLeftIntern()-lite,
2041 // without the dreaded mechanism. (JMarc)
2042 setCursorIntern(cursor.par(), cursor.pos() - 1,
2043 false, cursor.boundary());
2044 cursor.par()->erase(cursor.pos());
2047 lastpos = cursor.par()->size();
2048 if (cursor.pos() == lastpos)
2052 setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
2056 RowList::iterator LyXText::cursorRow() const
2058 return getRow(cursor.par(), cursor.pos());
2062 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2064 return getRow(cur.par(), cur.pos());
2069 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
2071 RowList::iterator rit = pit->rows.begin();
2072 RowList::iterator end = pit->rows.end();
2074 #warning Why is this next thing needed? (Andre)
2077 && boost::next(rit) != end
2078 && boost::next(rit)->pos() <= pos)
2085 // returns pointer to a specified row
2087 LyXText::getRow(ParagraphList::iterator pit, pos_type pos, int & y) const
2094 ParagraphList::iterator it = ownerParagraphs().begin();
2095 for ( ; it != pit; ++it) {
2096 RowList::iterator beg = it->rows.begin();
2097 RowList::iterator end = it->rows.end();
2098 for (RowList::iterator rit = beg; rit != end; ++rit)
2102 RowList::iterator rit = pit->rows.begin();
2103 RowList::iterator end = pit->rows.end();
2106 && boost::next(rit) != end
2107 && boost::next(rit)->pos() <= pos) {
2116 // returns pointer to some fancy row 'below' specified row
2117 RowList::iterator LyXText::cursorIRow() const
2120 return getRow(cursor.par(), cursor.pos(), y);
2124 RowList::iterator LyXText::getRowNearY(int & y,
2125 ParagraphList::iterator & pit) const
2127 //lyxerr << "getRowNearY: y " << y << endl;
2128 pit = ownerParagraphs().begin();
2129 RowList::iterator rit = firstRow();
2130 RowList::iterator rend = endRow();
2132 for (; rit != rend; nextRow(pit, rit))
2136 previousRow(pit, rit);
2142 int LyXText::getDepth() const
2144 return cursor.par()->getDepth();
2148 RowList::iterator LyXText::firstRow() const
2150 return ownerParagraphs().front().rows.begin();
2154 RowList::iterator LyXText::lastRow() const
2156 return boost::prior(endRow());
2160 RowList::iterator LyXText::endRow() const
2162 return ownerParagraphs().back().rows.end();
2166 void LyXText::nextRow(ParagraphList::iterator & pit,
2167 RowList::iterator & rit) const
2170 if (rit == pit->rows.end()) {
2172 if (pit == ownerParagraphs().end())
2175 rit = pit->rows.begin();
2180 void LyXText::previousRow(ParagraphList::iterator & pit,
2181 RowList::iterator & rit) const
2183 if (rit != pit->rows.begin())
2186 Assert(pit != ownerParagraphs().begin());
2188 rit = boost::prior(pit->rows.end());
2193 bool LyXText::noRows() const
2195 return ownerParagraphs().begin()->rows.empty();