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 * ====================================================== */
15 #include "paragraph.h"
17 #include "bufferparams.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
53 /// top, right, bottom pixel margin
54 int const PAPER_MARGIN = 20;
55 /// margin for changebar
56 int const CHANGEBAR_MARGIN = 10;
58 int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
62 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
65 int LyXText::workWidth(BufferView * bview) const
68 return inset_owner->textWidth(bview);
70 return bview->workWidth();
74 int LyXText::workWidth(BufferView * bview, Inset * inset) const
79 par = inset->parOwner();
81 pos = par->getPositionOfInset(inset);
83 if (!par || pos == -1) {
84 lyxerr << "LyXText::workWidth: something is wrong,"
85 " fall back to the brute force method" << endl;
86 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
87 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
88 for (; it != end; ++it) {
89 if (&(*it) == inset) {
98 return workWidth(bview);
101 LyXLayout_ptr const & layout = par->layout();
103 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
104 // Optimization here: in most cases, the real row is
105 // not needed, but only the par/pos values. So we just
106 // construct a dummy row for leftMargin. (JMarc)
110 return workWidth(bview) - leftMargin(bview, &dummyrow);
113 Row * row = getRow(par, pos, dummy_y);
115 while (frow->previous() && frow->par() == frow->previous()->par())
116 frow = frow->previous();
117 unsigned int maxw = 0;
118 while (frow->next() && frow->par() == frow->next()->par()) {
119 if ((frow != row) && (maxw < frow->width()))
120 maxw = frow->width();
126 return workWidth(bview);
130 int LyXText::getRealCursorX(BufferView * bview) const
133 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
134 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
139 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
142 if (!Encodings::is_arabic(c))
143 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
144 return c + (0xb0 - '0');
148 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
149 unsigned char next_char = ' ';
151 for (pos_type i = pos+1; i < par->size(); ++i)
152 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
153 next_char = par->getChar(i);
157 if (Encodings::is_arabic(next_char)) {
158 if (Encodings::is_arabic(prev_char) &&
159 !Encodings::is_arabic_special(prev_char))
160 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
162 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
164 if (Encodings::is_arabic(prev_char) &&
165 !Encodings::is_arabic_special(prev_char))
166 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
168 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
172 // This is the comments that some of the warnings below refers to.
173 // There are some issues in this file and I don't think they are
174 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
175 // this is a problem that has been here almost from day one and that a
176 // larger userbase with differenct access patters triggers the bad
177 // behaviour. (segfaults.) What I think happen is: In several places
178 // we store the paragraph in the current cursor and then moves the
179 // cursor. This movement of the cursor will delete paragraph at the
180 // old position if it is now empty. This will make the temporary
181 // pointer to the old cursor paragraph invalid and dangerous to use.
182 // And is some cases this will trigger a segfault. I have marked some
183 // of the cases where this happens with a warning, but I am sure there
184 // are others in this file and in text2.C. There is also a note in
185 // Delete() that you should read. In Delete I store the paragraph->id
186 // instead of a pointer to the paragraph. I am pretty sure this faulty
187 // use of temporary pointers to paragraphs that might have gotten
188 // invalidated (through a cursor movement) before they are used, are
189 // the cause of the strange crashes we get reported often.
191 // It is very tiresom to change this code, especially when it is as
192 // hard to read as it is. Help to fix all the cases where this is done
193 // would be greately appreciated.
197 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
200 char const c = par->getChar(pos);
201 return singleWidth(bview, par, pos, c);
205 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
206 pos_type pos, char c) const
208 LyXFont const font = getFont(bview->buffer(), par, pos);
210 // The most common case is handled first (Asger)
211 if (IsPrintable(c)) {
212 if (font.language()->RightToLeft()) {
213 if (font.language()->lang() == "arabic" &&
214 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
215 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
216 if (Encodings::IsComposeChar_arabic(c))
219 c = transformChar(c, par, pos);
220 else if (font.language()->lang() == "hebrew" &&
221 Encodings::IsComposeChar_hebrew(c))
224 return font_metrics::width(c, font);
226 } else if (IsHfillChar(c)) {
227 // Because of the representation as vertical lines
229 } else if (c == Paragraph::META_INSET) {
230 Inset * tmpinset = par->getInset(pos);
233 // this IS needed otherwise on initialitation we don't get the fill
234 // of the row right (ONLY on initialization if we read a file!)
235 // should be changed! (Jug 20011204)
236 tmpinset->update(bview, font);
238 return tmpinset->width(bview, font);
242 } else if (IsSeparatorChar(c))
244 else if (IsNewlineChar(c))
246 return font_metrics::width(c, font);
250 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
252 bidi_same_direction = true;
253 if (!lyxrc.rtl_support) {
258 Inset * inset = row->par()->inInset();
259 if (inset && inset->owner() &&
260 inset->owner()->lyxCode() == Inset::ERT_CODE) {
265 bidi_start = row->pos();
266 bidi_end = row->lastPrintablePos();
268 if (bidi_start > bidi_end) {
273 if (bidi_end + 2 - bidi_start >
274 static_cast<pos_type>(log2vis_list.size())) {
276 (bidi_end + 2 - bidi_start < 500) ?
277 500 : 2 * (bidi_end + 2 - bidi_start);
278 log2vis_list.resize(new_size);
279 vis2log_list.resize(new_size);
280 bidi_levels.resize(new_size);
283 vis2log_list[bidi_end + 1 - bidi_start] = -1;
284 log2vis_list[bidi_end + 1 - bidi_start] = -1;
288 row->par()->isRightToLeftPar(buf->params);
292 pos_type const main_body = row->par()->beginningOfMainBody();
294 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
295 bool is_space = row->par()->isLineSeparator(lpos);
297 (is_space && lpos + 1 <= bidi_end &&
298 !row->par()->isLineSeparator(lpos + 1) &&
299 !row->par()->isNewline(lpos + 1))
301 LyXFont font = row->par()->getFontSettings(buf->params, pos);
302 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
303 font.number() == LyXFont::ON &&
304 row->par()->getFontSettings(buf->params, lpos - 1).number()
306 font = row->par()->getFontSettings(buf->params, lpos);
311 bool new_rtl = font.isVisibleRightToLeft();
312 bool new_rtl0 = font.isRightToLeft();
315 if (lpos == main_body - 1
316 && row->pos() < main_body - 1
318 new_level = (rtl_par) ? 1 : 0;
319 new_rtl = new_rtl0 = rtl_par;
321 new_level = (new_rtl) ? 1 : 2;
323 new_level = (rtl_par) ? 2 : 0;
325 if (is_space && new_level >= level) {
331 int new_level2 = new_level;
333 if (level == new_level && rtl0 != new_rtl0) {
335 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
336 } else if (level < new_level) {
337 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
338 if (new_level > rtl_par)
339 bidi_same_direction = false;
341 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
344 bidi_levels[lpos - bidi_start] = new_level;
346 while (level > new_level2) {
347 pos_type old_lpos = stack[--level];
348 int delta = lpos - old_lpos - 1;
351 log2vis_list[lpos - bidi_start] += delta;
352 log2vis_list[old_lpos - bidi_start] += delta;
354 while (level < new_level)
355 stack[level++] = lpos;
359 pos_type const old_lpos = stack[--level];
360 int delta = bidi_end - old_lpos;
363 log2vis_list[old_lpos - bidi_start] += delta;
366 pos_type vpos = bidi_start - 1;
367 for (pos_type lpos = bidi_start;
368 lpos <= bidi_end; ++lpos) {
369 vpos += log2vis_list[lpos - bidi_start];
370 vis2log_list[vpos - bidi_start] = lpos;
371 log2vis_list[lpos - bidi_start] = vpos;
376 // This method requires a previous call to ComputeBidiTables()
377 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
380 if (!lyxrc.rtl_support || pos == 0)
383 if (!bidi_InRange(pos - 1)) {
384 /// This can happen if pos is the first char of a row.
385 /// Returning false in this case is incorrect!
389 bool const rtl = bidi_level(pos - 1) % 2;
390 bool const rtl2 = bidi_InRange(pos)
391 ? bidi_level(pos) % 2
392 : par->isRightToLeftPar(buf->params);
397 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
398 pos_type pos, LyXFont const & font) const
400 if (!lyxrc.rtl_support)
401 return false; // This is just for speedup
403 bool const rtl = font.isVisibleRightToLeft();
404 bool const rtl2 = bidi_InRange(pos)
405 ? bidi_level(pos) % 2
406 : par->isRightToLeftPar(buf->params);
411 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
413 // Draw end-of-line marker
414 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
415 int const wid = font_metrics::width('n', font);
416 int const asc = font_metrics::maxAscent(font);
417 int const y = p.yo + p.row->baseline();
421 yp[0] = int(y - 0.875 * asc * 0.75);
422 yp[1] = int(y - 0.500 * asc * 0.75);
423 yp[2] = int(y - 0.125 * asc * 0.75);
425 if (bidi_level(pos) % 2 == 0) {
426 xp[0] = int(p.x + wid * 0.375);
428 xp[2] = int(p.x + wid * 0.375);
430 xp[0] = int(p.x + wid * 0.625);
431 xp[1] = int(p.x + wid);
432 xp[2] = int(p.x + wid * 0.625);
435 p.pain->lines(xp, yp, 3, LColor::eolmarker);
437 yp[0] = int(y - 0.500 * asc * 0.75);
438 yp[1] = int(y - 0.500 * asc * 0.75);
439 yp[2] = int(y - asc * 0.75);
441 if (bidi_level(pos) % 2 == 0) {
443 xp[1] = int(p.x + wid);
444 xp[2] = int(p.x + wid);
446 xp[0] = int(p.x + wid);
451 p.pain->lines(xp, yp, 3, LColor::eolmarker);
457 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
459 Inset * inset = p.row->par()->getInset(pos);
461 // FIXME: shouldn't happen
466 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
467 // we need this here as the row pointer may be illegal
468 // at a later time (Jug20020502)
469 Row * prev = p.row->previous();
471 inset->update(p.bv, font, false);
472 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
474 if (!need_break_row && !inset_owner
475 && p.bv->text->status() == CHANGED_IN_DRAW) {
476 if (prev && prev->par() == p.row->par()) {
477 breakAgainOneRow(p.bv, prev);
478 if (prev->next() != p.row) {
479 // breakAgainOneRow() has removed p.row
480 p.row = 0; // see what this breaks
481 need_break_row = prev;
483 need_break_row = p.row;
486 need_break_row = firstrow;
488 need_break_row = prev->next();
490 setCursor(p.bv, cursor.par(), cursor.pos());
497 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
499 if (!lyxrc.mark_foreign_language)
501 if (orig_font.language() == latex_language)
503 if (orig_font.language() == p.bv->buffer()->params.language)
506 int const y = p.yo + p.row->baseline() + 1;
507 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
511 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
513 pos_type pos = vis2log(vpos);
518 char c = p.row->par()->getChar(pos);
522 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
523 int const width = font_metrics::width(c, font);
526 for (pos_type i = pos-1; i >= 0; --i) {
527 c = p.row->par()->getChar(i);
528 if (!Encodings::IsComposeChar_hebrew(c)) {
529 if (IsPrintableNonspace(c)) {
531 singleWidth(p.bv, p.row->par(), i, c);
533 dx = (c == 'ø' || c == 'ã')
535 : (width2 - width) / 2;
542 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
546 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
548 pos_type pos = vis2log(vpos);
552 char c = p.row->par()->getChar(pos);
553 c = transformChar(c, p.row->par(), pos);
557 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
558 int const width = font_metrics::width(c, font);
561 for (pos_type i = pos-1; i >= 0; --i) {
562 c = p.row->par()->getChar(i);
563 if (!Encodings::IsComposeChar_arabic(c)) {
564 if (IsPrintableNonspace(c)) {
566 singleWidth(p.bv, p.row->par(), i, c);
567 dx = (width2 - width) / 2;
573 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
577 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
578 bool hebrew, bool arabic)
580 pos_type pos = vis2log(vpos);
581 pos_type const last = p.row->lastPrintablePos();
582 LyXFont orig_font(getFont(p.bv->buffer(), p.row->par(), pos));
586 str += p.row->par()->getChar(pos);
588 unsigned char c = str[0];
589 str[0] = transformChar(c, p.row->par(), pos);
592 bool prev_struckout(isDeletedText(p.row->par(), pos));
593 bool prev_newtext(isInsertedText(p.row->par(), pos));
597 // collect as much similar chars as we can
598 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
599 char c = p.row->par()->getChar(pos);
601 if (!IsPrintableNonspace(c))
604 if (prev_struckout != isDeletedText(p.row->par(), pos))
607 if (prev_newtext != isInsertedText(p.row->par(), pos))
610 if (arabic && Encodings::IsComposeChar_arabic(c))
612 if (hebrew && Encodings::IsComposeChar_hebrew(c))
615 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
619 c = transformChar(c, p.row->par(), pos);
624 if (prev_struckout) {
625 orig_font.setColor(LColor::strikeout);
626 } else if (prev_newtext) {
627 orig_font.setColor(LColor::newtext);
630 // Draw text and set the new x position
631 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
632 p.x += font_metrics::width(str, orig_font);
636 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
638 pos_type const pos = vis2log(vpos);
639 Paragraph * par = p.row->par();
641 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
643 float const orig_x = p.x;
645 char const c = par->getChar(pos);
647 if (IsNewlineChar(c)) {
651 } else if (IsInsetChar(c)) {
652 if (!drawInset(p, pos))
655 drawForeignMark(p, orig_x, orig_font);
659 // usual characters, no insets
661 // special case languages
662 bool const hebrew = (orig_font.language()->lang() == "hebrew");
664 orig_font.language()->lang() == "arabic" &&
665 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
666 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
668 // draw as many chars as we can
669 if ((!hebrew && !arabic)
670 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
671 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
672 drawChars(p, vpos, hebrew, arabic);
674 drawHebrewComposeChar(p, vpos);
676 drawArabicComposeChar(p, vpos);
679 drawForeignMark(p, orig_x, orig_font);
685 int LyXText::leftMargin(BufferView * bview, Row const * row) const
688 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
689 (ins=row->par()->getInset(row->pos())) &&
690 (ins->needFullRow() || ins->display()))
693 LyXTextClass const & tclass =
694 bview->buffer()->params.getLyXTextClass();
695 LyXLayout_ptr const & layout = row->par()->layout();
697 string parindent = layout->parindent;
701 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
703 // this is the way, LyX handles the LaTeX-Environments.
704 // I have had this idea very late, so it seems to be a
705 // later added hack and this is true
706 if (!row->par()->getDepth()) {
707 if (row->par()->layout() == tclass.defaultLayout()) {
708 // find the previous same level paragraph
709 if (row->par()->previous()) {
710 Paragraph * newpar = row->par()
711 ->depthHook(row->par()->getDepth());
713 newpar->layout()->nextnoindent)
718 // find the next level paragraph
720 Paragraph * newpar = row->par()->outerHook();
722 // make a corresponding row. Needed to call LeftMargin()
724 // check wether it is a sufficent paragraph
725 if (newpar && newpar->layout()->isEnvironment()) {
727 dummyrow.par(newpar);
728 dummyrow.pos(newpar->size());
729 x = leftMargin(bview, &dummyrow);
731 // this is no longer an error, because this function
732 // is used to clear impossible depths after changing
733 // a layout. Since there is always a redo,
734 // LeftMargin() is always called
735 row->par()->params().depth(0);
738 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
739 if (newpar->params().noindent())
742 parindent = newpar->layout()->parindent;
748 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
749 switch (layout->margintype) {
751 if (!layout->leftmargin.empty()) {
752 x += font_metrics::signedWidth(layout->leftmargin,
753 tclass.defaultfont());
755 if (!row->par()->getLabelstring().empty()) {
756 x += font_metrics::signedWidth(layout->labelindent,
758 x += font_metrics::width(row->par()->getLabelstring(),
760 x += font_metrics::width(layout->labelsep, labelfont);
764 x += font_metrics::signedWidth(layout->labelindent, labelfont);
765 if (row->pos() >= row->par()->beginningOfMainBody()) {
766 if (!row->par()->getLabelWidthString().empty()) {
767 x += font_metrics::width(row->par()->getLabelWidthString(),
769 x += font_metrics::width(layout->labelsep, labelfont);
774 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
775 / (row->par()->getDepth() + 4);
777 case MARGIN_FIRST_DYNAMIC:
778 if (layout->labeltype == LABEL_MANUAL) {
779 if (row->pos() >= row->par()->beginningOfMainBody()) {
780 x += font_metrics::signedWidth(layout->leftmargin,
783 x += font_metrics::signedWidth(layout->labelindent,
786 } else if (row->pos()
787 // Special case to fix problems with
789 || (layout->labeltype == LABEL_STATIC
790 && layout->latextype == LATEX_ENVIRONMENT
791 && ! row->par()->isFirstInSequence())) {
792 x += font_metrics::signedWidth(layout->leftmargin,
794 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
795 && layout->labeltype != LABEL_BIBLIO
796 && layout->labeltype !=
797 LABEL_CENTERED_TOP_ENVIRONMENT) {
798 x += font_metrics::signedWidth(layout->labelindent,
800 x += font_metrics::width(layout->labelsep, labelfont);
801 x += font_metrics::width(row->par()->getLabelstring(),
806 case MARGIN_RIGHT_ADDRESS_BOX:
808 // ok, a terrible hack. The left margin depends on the widest
809 // row in this paragraph. Do not care about footnotes, they
810 // are *NOT* allowed in the LaTeX realisation of this layout.
812 // find the first row of this paragraph
813 Row const * tmprow = row;
814 while (tmprow->previous()
815 && tmprow->previous()->par() == row->par())
816 tmprow = tmprow->previous();
818 int minfill = tmprow->fill();
819 while (tmprow->next() && tmprow->next()->par() == row->par()) {
820 tmprow = tmprow->next();
821 if (tmprow->fill() < minfill)
822 minfill = tmprow->fill();
825 x += font_metrics::signedWidth(layout->leftmargin,
826 tclass.defaultfont());
832 if ((workWidth(bview) > 0) &&
833 !row->par()->params().leftIndent().zero())
835 LyXLength const len = row->par()->params().leftIndent();
836 int const tw = inset_owner ?
837 inset_owner->latexTextWidth(bview) : workWidth(bview);
838 x += len.inPixels(tw);
841 LyXAlignment align; // wrong type
843 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
844 align = layout->align;
846 align = row->par()->params().align();
848 // set the correct parindent
849 if (row->pos() == 0) {
850 if ((layout->labeltype == LABEL_NO_LABEL
851 || layout->labeltype == LABEL_TOP_ENVIRONMENT
852 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
853 || (layout->labeltype == LABEL_STATIC
854 && layout->latextype == LATEX_ENVIRONMENT
855 && ! row->par()->isFirstInSequence()))
856 && align == LYX_ALIGN_BLOCK
857 && !row->par()->params().noindent()
858 // in tabulars and ert paragraphs are never indented!
859 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
860 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
861 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
862 && (row->par()->layout() != tclass.defaultLayout() ||
863 bview->buffer()->params.paragraph_separation ==
864 BufferParams::PARSEP_INDENT)) {
865 x += font_metrics::signedWidth(parindent,
866 tclass.defaultfont());
867 } else if (layout->labeltype == LABEL_BIBLIO) {
868 // ale970405 Right width for bibitems
869 x += bibitemMaxWidth(bview, tclass.defaultfont());
877 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
880 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
881 (ins=row->par()->getInset(row->pos())) &&
882 (ins->needFullRow() || ins->display()))
885 LyXTextClass const & tclass = buf->params.getLyXTextClass();
886 LyXLayout_ptr const & layout = row->par()->layout();
889 + font_metrics::signedWidth(tclass.rightmargin(),
890 tclass.defaultfont());
892 // this is the way, LyX handles the LaTeX-Environments.
893 // I have had this idea very late, so it seems to be a
894 // later added hack and this is true
895 if (row->par()->getDepth()) {
896 // find the next level paragraph
898 Paragraph * newpar = row->par();
901 newpar = newpar->previous();
903 && newpar->getDepth() >= row->par()->getDepth());
905 // make a corresponding row. Needed to call LeftMargin()
907 // check wether it is a sufficent paragraph
908 if (newpar && newpar->layout()->isEnvironment()) {
910 dummyrow.par(newpar);
912 x = rightMargin(buf, &dummyrow);
914 // this is no longer an error, because this function
915 // is used to clear impossible depths after changing
916 // a layout. Since there is always a redo,
917 // LeftMargin() is always called
918 row->par()->params().depth(0);
922 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
923 x += font_metrics::signedWidth(layout->rightmargin,
924 tclass.defaultfont())
925 * 4 / (row->par()->getDepth() + 4);
930 int LyXText::labelEnd(BufferView * bview, Row const * row) const
932 if (row->par()->layout()->margintype == MARGIN_MANUAL) {
935 tmprow.pos(row->par()->size());
936 // just the beginning of the main body
937 return leftMargin(bview, &tmprow);
939 // LabelEnd is only needed,
940 // if the layout fills a flushleft label.
946 // get the next breakpoint in a given paragraph
948 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
950 Paragraph * par = row->par();
955 pos_type const pos = row->pos();
957 // position of the last possible breakpoint
958 // -1 isn't a suitable value, but a flag
959 pos_type last_separator = -1;
960 width -= rightMargin(bview->buffer(), row);
962 pos_type const main_body = par->beginningOfMainBody();
963 LyXLayout_ptr const & layout = par->layout();
967 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
968 // special code for right address boxes, only newlines count
969 while (i < par->size()) {
970 if (par->isNewline(i)) {
972 i = par->size() - 1; // this means break
974 } else if (par->isInset(i) && par->getInset(i)
975 && par->getInset(i)->display()) {
976 par->getInset(i)->display(false);
981 // Last position is an invariant
982 pos_type const last = par->size();
983 // this is the usual handling
984 int x = leftMargin(bview, row);
985 bool doitonetime = true;
986 while (doitonetime || ((x < width) && (i < last))) {
988 char const c = par->getChar(i);
990 if (c == Paragraph::META_INSET)
991 in = par->getInset(i);
992 if (IsNewlineChar(c)) {
994 x = width; // this means break
995 } else if (in && !in->isChar()) {
996 // check wether a Display() inset is
997 // valid here. if not, change it to
1000 (layout->isCommand() ||
1001 (layout->labeltype == LABEL_MANUAL
1002 && i < par->beginningOfMainBody())))
1004 // display istn't allowd
1006 x += singleWidth(bview, par, i, c);
1007 } else if (in->display() || in->needFullRow()) {
1008 // So break the line here
1012 if (par->isLineSeparator(i+1))
1015 last_separator = last; // to avoid extra rows
1017 last_separator = i - 1;
1018 x = width; // this means break
1020 x += singleWidth(bview, par, i, c);
1021 // we have to check this separately as we could have a
1022 // lineseparator and then the algorithm below would prefer
1023 // that which IS wrong! We should always break on an inset
1024 // if it's too long and not on the last separator.
1025 // Maybe the only exeption is insets used as chars but
1026 // then we would have to have a special function inside
1027 // the inset to tell us this. Till then we leave it as
1028 // it is now. (Jug 20020106)
1029 if (pos < i && x >= width && last_separator >= 0)
1030 last_separator = i - 1;
1033 if (par->isLineSeparator(i))
1035 x += singleWidth(bview, par, i, c);
1038 if (i == main_body) {
1039 x += font_metrics::width(layout->labelsep,
1040 getLabelFont(bview->buffer(), par));
1041 if (par->isLineSeparator(i - 1))
1042 x-= singleWidth(bview, par, i - 1);
1043 int left_margin = labelEnd(bview, row);
1044 if (x < left_margin)
1048 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1049 last_separator = i - 2;
1050 else if ((pos < i) && (last_separator < 0) && (x >= width))
1051 last_separator = i - 1;
1052 // end of paragraph is always a suitable separator
1053 else if (i == last && x < width)
1057 // well, if last_separator is still 0, the line isn't breakable.
1058 // don't care and cut simply at the end
1059 if (last_separator < 0) {
1063 // manual labels cannot be broken in LaTeX, do not care
1064 if (main_body && last_separator < main_body)
1065 last_separator = main_body - 1;
1067 return last_separator;
1071 // returns the minimum space a row needs on the screen in pixel
1072 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1074 if (paper_width < 0)
1078 // get the pure distance
1079 pos_type const last = row->lastPrintablePos();
1081 // special handling of the right address boxes
1082 if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1083 int const tmpfill = row->fill();
1084 row->fill(0); // the minfill in MarginLeft()
1085 w = leftMargin(bview, row);
1088 w = leftMargin(bview, row);
1090 LyXLayout_ptr const & layout = row->par()->layout();
1092 pos_type const main_body = row->par()->beginningOfMainBody();
1093 pos_type i = row->pos();
1096 if (main_body > 0 && i == main_body) {
1097 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1098 if (row->par()->isLineSeparator(i - 1))
1099 w -= singleWidth(bview, row->par(), i - 1);
1100 int left_margin = labelEnd(bview, row);
1101 if (w < left_margin)
1104 w += singleWidth(bview, row->par(), i);
1107 if (main_body > 0 && main_body > last) {
1108 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1109 if (last >= 0 && row->par()->isLineSeparator(last))
1110 w -= singleWidth(bview, row->par(), last);
1111 int const left_margin = labelEnd(bview, row);
1112 if (w < left_margin)
1116 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1121 // returns the minimum space a manual label needs on the screen in pixel
1122 int LyXText::labelFill(BufferView * bview, Row const * row) const
1124 pos_type last = row->par()->beginningOfMainBody() - 1;
1125 // -1 because a label ends either with a space that is in the label,
1126 // or with the beginning of a footnote that is outside the label.
1128 // I don't understand this code in depth, but sometimes "last" is
1129 // less than 0 and this causes a crash. This fix seems to work
1130 // correctly, but I bet the real error is elsewhere. The bug is
1131 // triggered when you have an open footnote in a paragraph
1132 // environment with a manual label. (Asger)
1133 if (last < 0) last = 0;
1135 // a separator at this end does not count
1136 if (row->par()->isLineSeparator(last))
1140 pos_type i = row->pos();
1142 w += singleWidth(bview, row->par(), i);
1147 if (!row->par()->params().labelWidthString().empty()) {
1148 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1149 getLabelFont(bview->buffer(), row->par())) - w,
1157 LColor::color LyXText::backgroundColor()
1160 return inset_owner->backgroundColor();
1162 return LColor::background;
1165 void LyXText::setHeightOfRow(BufferView * bview, Row * row) const
1167 // get the maximum ascent and the maximum descent
1170 float layoutasc = 0;
1171 float layoutdesc = 0;
1174 Inset * tmpinset = 0;
1176 // ok , let us initialize the maxasc and maxdesc value.
1177 // This depends in LaTeX of the font of the last character
1178 // in the paragraph. The hack below is necessary because
1179 // of the possibility of open footnotes
1181 // Correction: only the fontsize count. The other properties
1182 // are taken from the layoutfont. Nicer on the screen :)
1183 Paragraph * par = row->par();
1184 Paragraph * firstpar = row->par();
1186 LyXLayout_ptr const & layout = firstpar->layout();
1188 // as max get the first character of this row then it can increase but not
1189 // decrease the height. Just some point to start with so we don't have to
1190 // do the assignment below too often.
1191 LyXFont font = getFont(bview->buffer(), par, row->pos());
1192 LyXFont::FONT_SIZE const tmpsize = font.size();
1193 font = getLayoutFont(bview->buffer(), par);
1194 LyXFont::FONT_SIZE const size = font.size();
1195 font.setSize(tmpsize);
1197 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1199 float spacing_val = 1.0;
1200 if (!row->par()->params().spacing().isDefault()) {
1201 spacing_val = row->par()->params().spacing().getValue();
1203 spacing_val = bview->buffer()->params.spacing.getValue();
1205 //lyxerr << "spacing_val = " << spacing_val << endl;
1207 int maxasc = int(font_metrics::maxAscent(font) *
1208 layout->spacing.getValue() *
1210 int maxdesc = int(font_metrics::maxDescent(font) *
1211 layout->spacing.getValue() *
1214 pos_type const pos_end = row->lastPos();
1218 // Check if any insets are larger
1219 for (pos_type pos = row->pos(); pos <= pos_end; ++pos) {
1220 if (row->par()->isInset(pos)) {
1221 tmpfont = getFont(bview->buffer(), row->par(), pos);
1222 tmpinset = row->par()->getInset(pos);
1224 #if 1 // this is needed for deep update on initialitation
1225 tmpinset->update(bview, tmpfont);
1227 asc = tmpinset->ascent(bview, tmpfont);
1228 desc = tmpinset->descent(bview, tmpfont);
1229 maxwidth += tmpinset->width(bview, tmpfont);
1230 maxasc = max(maxasc, asc);
1231 maxdesc = max(maxdesc, desc);
1234 maxwidth += singleWidth(bview, row->par(), pos);
1238 // Check if any custom fonts are larger (Asger)
1239 // This is not completely correct, but we can live with the small,
1240 // cosmetic error for now.
1241 LyXFont::FONT_SIZE maxsize =
1242 row->par()->highestFontInRange(row->pos(), pos_end, size);
1243 if (maxsize > font.size()) {
1244 font.setSize(maxsize);
1246 asc = font_metrics::maxAscent(font);
1247 desc = font_metrics::maxDescent(font);
1254 // This is nicer with box insets:
1258 row->ascent_of_text(maxasc);
1260 // is it a top line?
1261 if (!row->pos() && (row->par() == firstpar)) {
1263 // some parksips VERY EASY IMPLEMENTATION
1264 if (bview->buffer()->params.paragraph_separation ==
1265 BufferParams::PARSEP_SKIP)
1267 if (layout->isParagraph()
1268 && firstpar->getDepth() == 0
1269 && firstpar->previous())
1271 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1272 } else if (firstpar->previous() &&
1273 firstpar->previous()->layout()->isParagraph() &&
1274 firstpar->previous()->getDepth() == 0)
1276 // is it right to use defskip here too? (AS)
1277 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1282 if (!row->par()->previous() && isTopLevel())
1283 maxasc += PAPER_MARGIN;
1285 // add the vertical spaces, that the user added
1286 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1288 // do not forget the DTP-lines!
1289 // there height depends on the font of the nearest character
1290 if (firstpar->params().lineTop())
1292 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1294 // and now the pagebreaks
1295 if (firstpar->params().pagebreakTop())
1296 maxasc += 3 * defaultRowHeight();
1298 // This is special code for the chapter, since the label of this
1299 // layout is printed in an extra row
1300 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1301 && bview->buffer()->params.secnumdepth >= 0)
1303 float spacing_val = 1.0;
1304 if (!row->par()->params().spacing().isDefault()) {
1305 spacing_val = row->par()->params().spacing().getValue();
1307 spacing_val = bview->buffer()->params.spacing.getValue();
1310 labeladdon = int(font_metrics::maxDescent(labelfont) *
1311 layout->spacing.getValue() *
1313 + int(font_metrics::maxAscent(labelfont) *
1314 layout->spacing.getValue() *
1318 // special code for the top label
1319 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1320 || layout->labeltype == LABEL_BIBLIO
1321 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1322 && row->par()->isFirstInSequence()
1323 && !row->par()->getLabelstring().empty())
1325 float spacing_val = 1.0;
1326 if (!row->par()->params().spacing().isDefault()) {
1327 spacing_val = row->par()->params().spacing().getValue();
1329 spacing_val = bview->buffer()->params.spacing.getValue();
1333 (font_metrics::maxAscent(labelfont) *
1334 layout->spacing.getValue() *
1336 +(font_metrics::maxDescent(labelfont) *
1337 layout->spacing.getValue() *
1339 + layout->topsep * defaultRowHeight()
1340 + layout->labelbottomsep * defaultRowHeight());
1343 // and now the layout spaces, for example before and after a section,
1344 // or between the items of a itemize or enumerate environment
1346 if (!firstpar->params().pagebreakTop()) {
1347 Paragraph * prev = row->par()->previous();
1349 prev = row->par()->depthHook(row->par()->getDepth());
1350 if (prev && prev->layout() == firstpar->layout() &&
1351 prev->getDepth() == firstpar->getDepth() &&
1352 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1354 layoutasc = (layout->itemsep * defaultRowHeight());
1355 } else if (row->previous()) {
1356 tmptop = layout->topsep;
1358 if (row->previous()->par()->getDepth() >= row->par()->getDepth())
1359 tmptop -= row->previous()->par()->layout()->bottomsep;
1362 layoutasc = (tmptop * defaultRowHeight());
1363 } else if (row->par()->params().lineTop()) {
1364 tmptop = layout->topsep;
1367 layoutasc = (tmptop * defaultRowHeight());
1370 prev = row->par()->outerHook();
1372 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1374 if (firstpar->previous() &&
1375 firstpar->previous()->getDepth() == 0 &&
1376 firstpar->previous()->layout() !=
1380 } else if (firstpar->previous()) {
1381 maxasc += int(layout->parsep * defaultRowHeight());
1387 // is it a bottom line?
1388 if (row->par() == par
1389 && (!row->next() || row->next()->par() != row->par()))
1391 // the bottom margin
1392 if (!par->next() && isTopLevel())
1393 maxdesc += PAPER_MARGIN;
1395 // add the vertical spaces, that the user added
1396 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1398 // do not forget the DTP-lines!
1399 // there height depends on the font of the nearest character
1400 if (firstpar->params().lineBottom())
1401 maxdesc += 2 * font_metrics::ascent('x',
1402 getFont(bview->buffer(),
1404 max(pos_type(0), par->size() - 1)));
1406 // and now the pagebreaks
1407 if (firstpar->params().pagebreakBottom())
1408 maxdesc += 3 * defaultRowHeight();
1410 // and now the layout spaces, for example before and after
1411 // a section, or between the items of a itemize or enumerate
1413 if (!firstpar->params().pagebreakBottom()
1414 && row->par()->next()) {
1415 Paragraph * nextpar = row->par()->next();
1416 Paragraph * comparepar = row->par();
1420 if (comparepar->getDepth() > nextpar->getDepth()) {
1421 usual = (comparepar->layout()->bottomsep * defaultRowHeight());
1422 comparepar = comparepar->depthHook(nextpar->getDepth());
1423 if (comparepar->layout()!= nextpar->layout()
1424 || nextpar->getLabelWidthString() !=
1425 comparepar->getLabelWidthString())
1427 unusual = (comparepar->layout()->bottomsep * defaultRowHeight());
1429 if (unusual > usual)
1430 layoutdesc = unusual;
1433 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1435 if (comparepar->layout() != nextpar->layout()
1436 || nextpar->getLabelWidthString() !=
1437 comparepar->getLabelWidthString())
1438 layoutdesc = int(comparepar->layout()->bottomsep * defaultRowHeight());
1443 // incalculate the layout spaces
1444 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1445 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1447 // calculate the new height of the text
1448 height -= row->height();
1450 row->height(maxasc + maxdesc + labeladdon);
1451 row->baseline(maxasc + labeladdon);
1453 height += row->height();
1455 row->top_of_text(row->baseline() - font_metrics::maxAscent(font));
1458 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1461 row->width(maxwidth);
1462 prepareToPrint(bview, row, x, dummy, dummy, dummy, false);
1464 row->width(int(maxwidth + x));
1467 width = max(0,workWidth(bview));
1469 if (r->width() > width)
1477 // Appends the implicit specified paragraph behind the specified row,
1478 // start at the implicit given position
1479 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1481 bool not_ready = true;
1483 // The last character position of a paragraph is an invariant so we can
1484 // safely get it here. (Asger)
1485 pos_type const lastposition = row->par()->size();
1487 // Get the next breakpoint
1488 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1492 // Insert the new row
1493 if (z < lastposition) {
1495 insertRow(row, row->par(), z);
1502 // Set the dimensions of the row
1503 // fixed fill setting now by calling inset->update() in
1504 // SingleWidth when needed!
1505 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1506 setHeightOfRow(bview, tmprow);
1508 } while (not_ready);
1512 void LyXText::breakAgain(BufferView * bview, Row * row) const
1514 bool not_ready = true;
1517 // get the next breakpoint
1518 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1521 if (z < row->par()->size()) {
1522 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1525 insertRow(row, row->par(), z);
1531 if (row->pos() == z)
1532 not_ready = false; // the rest will not change
1538 // if there are some rows too much, delete them
1539 // only if you broke the whole paragraph!
1540 Row * tmprow2 = row;
1541 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1542 tmprow2 = tmprow2->next();
1544 while (tmprow2 != row) {
1545 tmprow2 = tmprow2->previous();
1546 removeRow(tmprow2->next());
1551 // set the dimensions of the row
1552 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1553 setHeightOfRow(bview, tmprow);
1554 } while (not_ready);
1558 // this is just a little changed version of break again
1559 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1561 // get the next breakpoint
1562 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1565 if (z < row->par()->size()) {
1567 || (row->next() && row->next()->par() != row->par())) {
1570 insertRow(row, row->par(), z);
1576 if (row->pos() != z)
1580 // if there are some rows too much, delete them
1581 // only if you broke the whole paragraph!
1582 Row * tmprow2 = row;
1583 while (tmprow2->next()
1584 && tmprow2->next()->par() == row->par()) {
1585 tmprow2 = tmprow2->next();
1587 while (tmprow2 != row) {
1588 tmprow2 = tmprow2->previous();
1589 removeRow(tmprow2->next());
1593 // set the dimensions of the row
1594 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1595 setHeightOfRow(bview, tmprow);
1599 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1601 // allow only if at start or end, or all previous is new text
1602 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1603 && cursor.par()->isChangeEdited(0, cursor.pos()))
1606 LyXTextClass const & tclass =
1607 bview->buffer()->params.getLyXTextClass();
1608 LyXLayout_ptr const & layout = cursor.par()->layout();
1610 // this is only allowed, if the current paragraph is not empty or caption
1611 // and if it has not the keepempty flag aktive
1612 if (cursor.par()->empty()
1613 && layout->labeltype != LABEL_SENSITIVE
1614 && !layout->keepempty)
1617 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1619 // Always break behind a space
1621 // It is better to erase the space (Dekel)
1622 if (cursor.pos() < cursor.par()->size()
1623 && cursor.par()->isLineSeparator(cursor.pos()))
1624 cursor.par()->erase(cursor.pos());
1625 // cursor.pos(cursor.pos() + 1);
1627 // break the paragraph
1631 keep_layout = layout->isEnvironment();
1633 // we need to set this before we insert the paragraph. IMO the
1634 // breakParagraph call should return a bool if it inserts the
1635 // paragraph before or behind and we should react on that one
1636 // but we can fix this in 1.3.0 (Jug 20020509)
1637 bool const isempty = (layout->keepempty && cursor.par()->empty());
1638 ::breakParagraph(bview->buffer()->params, cursor.par(), cursor.pos(),
1641 // well this is the caption hack since one caption is really enough
1642 if (layout->labeltype == LABEL_SENSITIVE) {
1644 // set to standard-layout
1645 cursor.par()->applyLayout(tclass.defaultLayout());
1647 // set to standard-layout
1648 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1651 // if the cursor is at the beginning of a row without prior newline,
1653 // This touches only the screen-update. Otherwise we would may have
1654 // an empty row on the screen
1655 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1656 && cursor.row()->pos() == cursor.pos())
1661 status(bview, LyXText::NEED_MORE_REFRESH);
1662 refresh_row = cursor.row();
1663 refresh_y = cursor.y() - cursor.row()->baseline();
1665 // Do not forget the special right address boxes
1666 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1667 while (refresh_row->previous() &&
1668 refresh_row->previous()->par() == refresh_row->par())
1670 refresh_row = refresh_row->previous();
1671 refresh_y -= refresh_row->height();
1674 removeParagraph(cursor.row());
1676 // set the dimensions of the cursor row
1677 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1679 setHeightOfRow(bview, cursor.row());
1681 while (!cursor.par()->next()->empty()
1682 && cursor.par()->next()->isNewline(0))
1683 cursor.par()->next()->erase(0);
1685 insertParagraph(bview, cursor.par()->next(), cursor.row());
1687 updateCounters(bview);
1689 // This check is necessary. Otherwise the new empty paragraph will
1690 // be deleted automatically. And it is more friendly for the user!
1691 if (cursor.pos() || isempty)
1692 setCursor(bview, cursor.par()->next(), 0);
1694 setCursor(bview, cursor.par(), 0);
1696 if (cursor.row()->next())
1697 breakAgain(bview, cursor.row()->next());
1703 // Just a macro to make some thing easier.
1704 void LyXText::redoParagraph(BufferView * bview) const
1707 redoParagraphs(bview, cursor, cursor.par()->next());
1708 setCursorIntern(bview, cursor.par(), cursor.pos());
1712 // insert a character, moves all the following breaks in the
1713 // same Paragraph one to the right and make a rebreak
1714 void LyXText::insertChar(BufferView * bview, char c)
1716 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1718 // When the free-spacing option is set for the current layout,
1719 // disable the double-space checking
1721 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1722 cursor.row()->par()->isFreeSpacing();
1724 if (lyxrc.auto_number) {
1725 static string const number_operators = "+-/*";
1726 static string const number_unary_operators = "+-";
1727 static string const number_seperators = ".,:";
1729 if (current_font.number() == LyXFont::ON) {
1730 if (!IsDigit(c) && !contains(number_operators, c) &&
1731 !(contains(number_seperators, c) &&
1732 cursor.pos() >= 1 &&
1733 cursor.pos() < cursor.par()->size() &&
1734 getFont(bview->buffer(),
1736 cursor.pos()).number() == LyXFont::ON &&
1737 getFont(bview->buffer(),
1739 cursor.pos() - 1).number() == LyXFont::ON)
1741 number(bview); // Set current_font.number to OFF
1742 } else if (IsDigit(c) &&
1743 real_current_font.isVisibleRightToLeft()) {
1744 number(bview); // Set current_font.number to ON
1746 if (cursor.pos() > 0) {
1747 char const c = cursor.par()->getChar(cursor.pos() - 1);
1748 if (contains(number_unary_operators, c) &&
1749 (cursor.pos() == 1 ||
1750 cursor.par()->isSeparator(cursor.pos() - 2) ||
1751 cursor.par()->isNewline(cursor.pos() - 2))
1753 setCharFont(bview->buffer(),
1757 } else if (contains(number_seperators, c) &&
1758 cursor.pos() >= 2 &&
1759 getFont(bview->buffer(),
1761 cursor.pos() - 2).number() == LyXFont::ON) {
1762 setCharFont(bview->buffer(),
1772 // First check, if there will be two blanks together or a blank at
1773 // the beginning of a paragraph.
1774 // I decided to handle blanks like normal characters, the main
1775 // difference are the special checks when calculating the row.fill
1776 // (blank does not count at the end of a row) and the check here
1778 // The bug is triggered when we type in a description environment:
1779 // The current_font is not changed when we go from label to main text
1780 // and it should (along with realtmpfont) when we type the space.
1781 // CHECK There is a bug here! (Asger)
1783 LyXFont realtmpfont = real_current_font;
1784 LyXFont rawtmpfont = current_font;
1785 // store the current font. This is because of the use of cursor
1786 // movements. The moving cursor would refresh the current font
1788 // Get the font that is used to calculate the baselineskip
1789 pos_type const lastpos = cursor.par()->size();
1790 LyXFont rawparfont =
1791 cursor.par()->getFontSettings(bview->buffer()->params,
1794 bool jumped_over_space = false;
1796 if (!freeSpacing && IsLineSeparatorChar(c)) {
1797 if ((cursor.pos() > 0
1798 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1799 || (cursor.pos() > 0
1800 && cursor.par()->isNewline(cursor.pos() - 1))
1801 || (cursor.pos() == 0)) {
1802 static bool sent_space_message = false;
1803 if (!sent_space_message) {
1804 if (cursor.pos() == 0)
1805 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1807 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1808 sent_space_message = true;
1813 } else if (IsNewlineChar(c)) {
1814 if (cursor.pos() <= cursor.par()->beginningOfMainBody()) {
1818 // No newline at first position of a paragraph or behind labels.
1819 // TeX does not allow that
1821 if (cursor.pos() < cursor.par()->size() &&
1822 cursor.par()->isLineSeparator(cursor.pos()))
1823 // newline always after a blank!
1825 cursor.row()->fill(-1); // to force a new break
1828 // the display inset stuff
1829 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1830 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1831 if (inset && (inset->display() || inset->needFullRow())) {
1832 // force a new break
1833 cursor.row()->fill(-1); // to force a new break
1837 // get the cursor row fist
1838 Row * row = cursor.row();
1839 int y = cursor.y() - row->baseline();
1840 if (c != Paragraph::META_INSET) {
1841 // Here case LyXText::InsertInset already insertet the character
1842 cursor.par()->insertChar(cursor.pos(), c);
1844 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1846 if (!jumped_over_space) {
1847 // refresh the positions
1849 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1850 tmprow = tmprow->next();
1851 tmprow->pos(tmprow->pos() + 1);
1855 // Is there a break one row above
1856 if (row->previous() && row->previous()->par() == row->par()
1857 && (cursor.par()->isLineSeparator(cursor.pos())
1858 || cursor.par()->isNewline(cursor.pos())
1859 || ((cursor.pos() < cursor.par()->size()) &&
1860 cursor.par()->isInset(cursor.pos()+1))
1861 || cursor.row()->fill() == -1))
1863 pos_type z = nextBreakPoint(bview,
1866 if (z >= row->pos()) {
1869 // set the dimensions of the row above
1870 row->previous()->fill(fill(bview,
1874 setHeightOfRow(bview, row->previous());
1876 y -= row->previous()->height();
1878 refresh_row = row->previous();
1879 status(bview, LyXText::NEED_MORE_REFRESH);
1881 breakAgainOneRow(bview, row);
1883 current_font = rawtmpfont;
1884 real_current_font = realtmpfont;
1885 setCursor(bview, cursor.par(), cursor.pos() + 1,
1886 false, cursor.boundary());
1887 // cursor MUST be in row now.
1889 if (row->next() && row->next()->par() == row->par())
1890 need_break_row = row->next();
1894 // check, wether the last characters font has changed.
1895 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1896 && rawparfont != rawtmpfont)
1897 redoHeightOfParagraph(bview, cursor);
1904 // recalculate the fill of the row
1905 if (row->fill() >= 0) {
1906 // needed because a newline will set fill to -1. Otherwise
1907 // we would not get a rebreak!
1908 row->fill(fill(bview, row, workWidth(bview)));
1911 if (c == Paragraph::META_INSET || row->fill() < 0) {
1914 status(bview, LyXText::NEED_MORE_REFRESH);
1915 breakAgainOneRow(bview, row);
1916 // will the cursor be in another row now?
1917 if (row->lastPos() <= cursor.pos() + 1 && row->next()) {
1918 if (row->next() && row->next()->par() == row->par())
1919 // this should always be true
1921 breakAgainOneRow(bview, row);
1923 current_font = rawtmpfont;
1924 real_current_font = realtmpfont;
1926 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1928 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1929 != cursor.boundary())
1930 setCursor(bview, cursor.par(), cursor.pos(), false,
1931 !cursor.boundary());
1932 if (row->next() && row->next()->par() == row->par())
1933 need_break_row = row->next();
1940 int const tmpheight = row->height();
1941 setHeightOfRow(bview, row);
1942 if (tmpheight == row->height())
1943 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1945 status(bview, LyXText::NEED_MORE_REFRESH);
1947 current_font = rawtmpfont;
1948 real_current_font = realtmpfont;
1949 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1953 // check, wether the last characters font has changed.
1954 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1955 && rawparfont != rawtmpfont) {
1956 redoHeightOfParagraph(bview, cursor);
1958 // now the special right address boxes
1959 if (cursor.par()->layout()->margintype
1960 == MARGIN_RIGHT_ADDRESS_BOX) {
1961 redoDrawingOfParagraph(bview, cursor);
1969 void LyXText::charInserted()
1971 // Here we could call FinishUndo for every 20 characters inserted.
1972 // This is from my experience how emacs does it.
1973 static unsigned int counter;
1983 void LyXText::prepareToPrint(BufferView * bview,
1984 Row * row, float & x,
1985 float & fill_separator,
1987 float & fill_label_hfill,
1993 float w = row->fill();
1995 fill_label_hfill = 0;
1997 fill_label_hfill = 0;
2000 row->par()->isRightToLeftPar(bview->buffer()->params);
2002 x = (workWidth(bview) > 0)
2003 ? rightMargin(bview->buffer(), row) : 0;
2005 x = (workWidth(bview) > 0)
2006 ? leftMargin(bview, row) : 0;
2008 // is there a manual margin with a manual label
2009 LyXLayout_ptr const & layout = row->par()->layout();
2011 if (layout->margintype == MARGIN_MANUAL
2012 && layout->labeltype == LABEL_MANUAL) {
2013 // one more since labels are left aligned
2014 nlh = row->numberOfLabelHfills() + 1;
2015 if (nlh && !row->par()->getLabelWidthString().empty()) {
2016 fill_label_hfill = labelFill(bview, row) / nlh;
2020 // are there any hfills in the row?
2021 float const nh = row->numberOfHfills();
2025 fill_hfill = w / nh;
2026 // we don't have to look at the alignment if it is ALIGN_LEFT and
2027 // if the row is already larger then the permitted width as then
2028 // we force the LEFT_ALIGN'edness!
2029 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2030 // is it block, flushleft or flushright?
2031 // set x how you need it
2033 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2034 align = layout->align;
2036 align = row->par()->params().align();
2039 // center displayed insets
2041 if (row->par()->isInset(row->pos())
2042 && (inset=row->par()->getInset(row->pos()))
2043 && (inset->display())) // || (inset->scroll() < 0)))
2044 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2045 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2046 // ERT insets should always be LEFT ALIGNED on screen
2047 inset = row->par()->inInset();
2048 if (inset && inset->owner() &&
2049 inset->owner()->lyxCode() == Inset::ERT_CODE)
2051 align = LYX_ALIGN_LEFT;
2055 case LYX_ALIGN_BLOCK:
2056 ns = row->numberOfSeparators();
2057 if (ns && row->next() && row->next()->par() == row->par() &&
2058 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2059 && !(row->next()->par()->isInset(row->next()->pos())
2060 && row->next()->par()->getInset(row->next()->pos())
2061 && row->next()->par()->getInset(row->next()->pos())->display())
2064 fill_separator = w / ns;
2065 } else if (is_rtl) {
2069 case LYX_ALIGN_RIGHT:
2072 case LYX_ALIGN_CENTER:
2080 computeBidiTables(bview->buffer(), row);
2082 pos_type main_body = row->par()->beginningOfMainBody();
2083 pos_type last = row->lastPos();
2085 if (main_body > 0 &&
2086 (main_body - 1 > last ||
2087 !row->par()->isLineSeparator(main_body - 1))) {
2088 x += font_metrics::width(layout->labelsep,
2089 getLabelFont(bview->buffer(), row->par()));
2090 if (main_body - 1 <= last)
2091 x += fill_label_hfill;
2097 // important for the screen
2100 // the cursor set functions have a special mechanism. When they
2101 // realize, that you left an empty paragraph, they will delete it.
2102 // They also delete the corresponding row
2104 void LyXText::cursorRightOneWord(BufferView * bview) const
2106 // treat floats, HFills and Insets as words
2107 LyXCursor tmpcursor = cursor;
2108 // CHECK See comment on top of text.C
2110 if (tmpcursor.pos() == tmpcursor.par()->size()
2111 && tmpcursor.par()->next()) {
2112 tmpcursor.par(tmpcursor.par()->next());
2117 // Skip through initial nonword stuff.
2118 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2119 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2120 // printf("Current pos1 %d", tmpcursor.pos()) ;
2121 tmpcursor.pos(tmpcursor.pos() + 1);
2124 // Advance through word.
2125 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2126 tmpcursor.par()->isWord(tmpcursor.pos())) {
2127 // printf("Current pos2 %d", tmpcursor.pos()) ;
2128 tmpcursor.pos(tmpcursor.pos() + 1);
2132 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2136 void LyXText::cursorTab(BufferView * bview) const
2138 LyXCursor tmpcursor = cursor;
2139 while (tmpcursor.pos() < tmpcursor.par()->size()
2140 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2141 tmpcursor.pos(tmpcursor.pos() + 1);
2143 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2144 if (tmpcursor.par()->next()) {
2145 tmpcursor.par(tmpcursor.par()->next());
2149 tmpcursor.pos(tmpcursor.pos() + 1);
2150 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2154 // Skip initial whitespace at end of word and move cursor to *start*
2155 // of prior word, not to end of next prior word.
2156 void LyXText::cursorLeftOneWord(BufferView * bview) const
2158 LyXCursor tmpcursor = cursor;
2159 cursorLeftOneWord(tmpcursor);
2160 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2164 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2166 // treat HFills, floats and Insets as words
2169 && (cur.par()->isSeparator(cur.pos() - 1)
2170 || cur.par()->isKomma(cur.pos() - 1))
2171 && !(cur.par()->isHfill(cur.pos() - 1)
2172 || cur.par()->isInset(cur.pos() - 1)))
2173 cur.pos(cur.pos() - 1);
2176 && (cur.par()->isInset(cur.pos() - 1)
2177 || cur.par()->isHfill(cur.pos() - 1))) {
2178 cur.pos(cur.pos() - 1);
2179 } else if (!cur.pos()) {
2180 if (cur.par()->previous()) {
2181 cur.par(cur.par()->previous());
2182 cur.pos(cur.par()->size());
2184 } else { // Here, cur != 0
2185 while (cur.pos() > 0 &&
2186 cur.par()->isWord(cur.pos() - 1))
2187 cur.pos(cur.pos() - 1);
2192 // Select current word. This depends on behaviour of
2193 // CursorLeftOneWord(), so it is patched as well.
2194 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2195 word_location const loc) const
2197 // first put the cursor where we wana start to select the word
2200 case WHOLE_WORD_STRICT:
2201 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2202 || cursor.par()->isSeparator(cursor.pos())
2203 || cursor.par()->isKomma(cursor.pos())
2204 || cursor.par()->isSeparator(cursor.pos() - 1)
2205 || cursor.par()->isKomma(cursor.pos() - 1)) {
2209 // no break here, we go to the next
2212 // Move cursor to the beginning, when not already there.
2213 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2214 && !from.par()->isKomma(from.pos() - 1))
2215 cursorLeftOneWord(from);
2218 // always move the cursor to the beginning of previous word
2219 cursorLeftOneWord(from);
2222 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2228 while (to.pos() < to.par()->size()
2229 && !to.par()->isSeparator(to.pos())
2230 && !to.par()->isKomma(to.pos())
2231 && !to.par()->isHfill(to.pos())
2232 && !to.par()->isInset(to.pos()))
2234 to.pos(to.pos() + 1);
2239 void LyXText::selectWord(BufferView * bview, word_location const loc)
2243 getWord(from, to, loc);
2245 setCursor(bview, from.par(), from.pos());
2248 selection.cursor = cursor;
2249 setCursor(bview, to.par(), to.pos());
2250 setSelection(bview);
2254 // Select the word currently under the cursor when no
2255 // selection is currently set
2256 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2257 word_location const loc)
2259 if (!selection.set()) {
2260 selectWord(bview, loc);
2261 return selection.set();
2267 void LyXText::acceptChange(BufferView * bv)
2269 if (!selection.set() && cursor.par()->size())
2274 if (selection.start.par() == selection.end.par()) {
2275 LyXCursor & startc = selection.start;
2276 LyXCursor & endc = selection.end;
2277 setUndo(bv, Undo::INSERT, startc.par(), startc.par()->next());
2278 startc.par()->acceptChange(startc.pos(), endc.pos());
2281 redoParagraphs(bv, startc, startc.par()->next());
2282 setCursorIntern(bv, startc.par(), 0);
2284 #warning handle multi par selection
2288 void LyXText::rejectChange(BufferView * bv)
2290 if (!selection.set() && cursor.par()->size())
2295 if (selection.start.par() == selection.end.par()) {
2296 LyXCursor & startc = selection.start;
2297 LyXCursor & endc = selection.end;
2298 setUndo(bv, Undo::INSERT, startc.par(), startc.par()->next());
2299 startc.par()->rejectChange(startc.pos(), endc.pos());
2302 redoParagraphs(bv, startc, startc.par()->next());
2303 setCursorIntern(bv, startc.par(), 0);
2305 #warning handle multi par selection
2309 // This function is only used by the spellchecker for NextWord().
2310 // It doesn't handle LYX_ACCENTs and probably never will.
2312 LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const
2314 if (the_locking_inset) {
2315 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2316 if (!word.word().empty()) {
2317 value += float(cursor.y());
2318 value /= float(height);
2321 // we have to go on checking so move cursor to the next char
2322 if (cursor.pos() == cursor.par()->size()) {
2323 if (!cursor.par()->next())
2325 cursor.par(cursor.par()->next());
2328 cursor.pos(cursor.pos() + 1);
2330 Paragraph * tmppar = cursor.par();
2332 // If this is not the very first word, skip rest of
2333 // current word because we are probably in the middle
2334 // of a word if there is text here.
2335 if (cursor.pos() || cursor.par()->previous()) {
2336 while (cursor.pos() < cursor.par()->size()
2337 && cursor.par()->isLetter(cursor.pos()))
2338 cursor.pos(cursor.pos() + 1);
2341 // Now, skip until we have real text (will jump paragraphs)
2343 Paragraph * cpar(cursor.par());
2344 pos_type const cpos(cursor.pos());
2346 if (cpos == cpar->size()) {
2348 cursor.par(cpar->next());
2355 bool const is_bad_inset(cpar->isInset(cpos)
2356 && !cpar->getInset(cpos)->allowSpellcheck());
2358 if (cpar->isLetter(cpos) && !isDeletedText(cpar, cpos)
2362 cursor.pos(cpos + 1);
2365 // now check if we hit an inset so it has to be a inset containing text!
2366 if (cursor.pos() < cursor.par()->size() &&
2367 cursor.par()->isInset(cursor.pos())) {
2369 cursor.par()->getInset(cursor.pos())->edit(bview);
2370 // now call us again to do the above trick
2371 // but obviously we have to start from down below ;)
2372 return bview->text->selectNextWordToSpellcheck(bview, value);
2375 // Update the value if we changed paragraphs
2376 if (cursor.par() != tmppar) {
2377 setCursor(bview, cursor.par(), cursor.pos());
2378 value = float(cursor.y())/float(height);
2381 // Start the selection from here
2382 selection.cursor = cursor;
2385 getFont(bview->buffer(), cursor.par(), cursor.pos())
2386 .language()->code());
2387 // and find the end of the word (insets like optional hyphens
2388 // and ligature break are part of a word)
2389 while (cursor.pos() < cursor.par()->size()
2390 && cursor.par()->isLetter(cursor.pos())
2391 && !isDeletedText(cursor.par(), cursor.pos()))
2392 cursor.pos(cursor.pos() + 1);
2394 // Finally, we copy the word to a string and return it
2396 if (selection.cursor.pos() < cursor.pos()) {
2398 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2399 if (!cursor.par()->isInset(i))
2400 str += cursor.par()->getChar(i);
2403 return WordLangTuple(str, lang_code);
2407 // This one is also only for the spellchecker
2408 void LyXText::selectSelectedWord(BufferView * bview)
2410 if (the_locking_inset) {
2411 the_locking_inset->selectSelectedWord(bview);
2414 // move cursor to the beginning
2415 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2417 // set the sel cursor
2418 selection.cursor = cursor;
2420 // now find the end of the word
2421 while (cursor.pos() < cursor.par()->size()
2422 && (cursor.par()->isLetter(cursor.pos())))
2423 cursor.pos(cursor.pos() + 1);
2425 setCursor(bview, cursor.par(), cursor.pos());
2427 // finally set the selection
2428 setSelection(bview);
2432 // Delete from cursor up to the end of the current or next word.
2433 void LyXText::deleteWordForward(BufferView * bview)
2435 if (cursor.par()->empty())
2438 LyXCursor tmpcursor = cursor;
2439 tmpcursor.row(0); //Â ??
2440 selection.set(true); // to avoid deletion
2441 cursorRightOneWord(bview);
2442 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2443 selection.cursor = cursor;
2445 setSelection(bview);
2447 // Great, CutSelection() gets rid of multiple spaces.
2448 cutSelection(bview, true, false);
2453 // Delete from cursor to start of current or prior word.
2454 void LyXText::deleteWordBackward(BufferView * bview)
2456 if (cursor.par()->empty())
2459 LyXCursor tmpcursor = cursor;
2460 tmpcursor.row(0); // ??
2461 selection.set(true); // to avoid deletion
2462 cursorLeftOneWord(bview);
2463 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2464 selection.cursor = cursor;
2466 setSelection(bview);
2467 cutSelection(bview, true, false);
2472 // Kill to end of line.
2473 void LyXText::deleteLineForward(BufferView * bview)
2475 if (cursor.par()->empty())
2476 // Paragraph is empty, so we just go to the right
2479 LyXCursor tmpcursor = cursor;
2480 // We can't store the row over a regular setCursor
2481 // so we set it to 0 and reset it afterwards.
2482 tmpcursor.row(0); //Â ??
2483 selection.set(true); // to avoid deletion
2485 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2486 selection.cursor = cursor;
2488 setSelection(bview);
2489 // What is this test for ??? (JMarc)
2490 if (!selection.set()) {
2491 deleteWordForward(bview);
2493 cutSelection(bview, true, false);
2499 // Change the case of a word at cursor position.
2500 // This function directly manipulates Paragraph::text because there
2501 // is no Paragraph::SetChar currently. I did what I could to ensure
2502 // that it is correct. I guess part of it should be moved to
2503 // Paragraph, but it will have to change for 1.1 anyway. At least
2504 // it does not access outside of the allocated array as the older
2505 // version did. (JMarc)
2506 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2511 if (selection.set()) {
2512 from = selection.start;
2515 getWord(from, to, PARTIAL_WORD);
2516 setCursor(bview, to.par(), to.pos() + 1);
2519 changeRegionCase(bview, from, to, action);
2523 void LyXText::changeRegionCase(BufferView * bview,
2524 LyXCursor const & from,
2525 LyXCursor const & to,
2526 LyXText::TextCase action)
2528 lyx::Assert(from <= to);
2530 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2532 pos_type pos = from.pos();
2533 Paragraph * par = from.par();
2535 while (par && (pos != to.pos() || par != to.par())) {
2536 if (pos == par->size()) {
2541 unsigned char c = par->getChar(pos);
2542 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2544 case text_lowercase:
2547 case text_capitalization:
2549 action = text_lowercase;
2551 case text_uppercase:
2557 par->setChar(pos, c);
2558 checkParagraph(bview, par, pos);
2562 if (to.row() != from.row()) {
2563 refresh_y = from.y() - from.row()->baseline();
2564 refresh_row = from.row();
2565 status(bview, LyXText::NEED_MORE_REFRESH);
2570 void LyXText::transposeChars(BufferView & bview)
2572 Paragraph * tmppar = cursor.par();
2574 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2576 pos_type tmppos = cursor.pos();
2578 // First decide if it is possible to transpose at all
2580 if (tmppos == 0 || tmppos == tmppar->size())
2583 if (isDeletedText(tmppar, tmppos - 1)
2584 || isDeletedText(tmppar, tmppos))
2587 unsigned char c1 = tmppar->getChar(tmppos);
2588 unsigned char c2 = tmppar->getChar(tmppos - 1);
2590 // We should have an implementation that handles insets
2591 // as well, but that will have to come later. (Lgb)
2592 if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
2595 bool const erased = tmppar->erase(tmppos - 1, tmppos + 1);
2596 pos_type const ipos(erased ? tmppos - 1 : tmppos + 1);
2598 tmppar->insertChar(ipos, c1);
2599 tmppar->insertChar(ipos + 1, c2);
2602 BufferView * bv(const_cast<BufferView*>(&bview));
2604 checkParagraph(bv, tmppar, tmppos);
2608 void LyXText::Delete(BufferView * bview)
2610 // this is a very easy implementation
2612 LyXCursor old_cursor = cursor;
2613 int const old_cur_par_id = old_cursor.par()->id();
2614 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2615 old_cursor.par()->previous()->id() : 0;
2617 // just move to the right
2620 // CHECK Look at the comment here.
2621 // This check is not very good...
2622 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2623 // and that can very well delete the par or par->previous in
2624 // old_cursor. Will a solution where we compare paragraph id's
2626 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2627 == old_cur_par_prev_id
2628 && cursor.par()->id() != old_cur_par_id) {
2629 // delete-empty-paragraph-mechanism has done it
2633 // if you had success make a backspace
2634 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2635 LyXCursor tmpcursor = cursor;
2636 // to make sure undo gets the right cursor position
2637 cursor = old_cursor;
2638 setUndo(bview, Undo::DELETE,
2639 cursor.par(), cursor.par()->next());
2646 void LyXText::backspace(BufferView * bview)
2648 // Get the font that is used to calculate the baselineskip
2649 pos_type lastpos = cursor.par()->size();
2650 LyXFont rawparfont =
2651 cursor.par()->getFontSettings(bview->buffer()->params,
2654 if (cursor.pos() == 0) {
2655 // The cursor is at the beginning of a paragraph,
2656 // so the the backspace will collapse two paragraphs into one.
2658 // but it's not allowed unless it's new
2659 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2662 // we may paste some paragraphs
2664 // is it an empty paragraph?
2667 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2668 // This is an empty paragraph and we delete it just by moving the cursor one step
2669 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2670 // of the paragraph.
2672 if (cursor.par()->previous()) {
2673 Paragraph * tmppar = cursor.par()->previous();
2674 if (cursor.par()->layout() == tmppar->layout()
2675 && cursor.par()->getAlign() == tmppar->getAlign()) {
2676 // Inherit bottom DTD from the paragraph below.
2677 // (the one we are deleting)
2678 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2679 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2680 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2685 // the layout things can change the height of a row !
2686 int const tmpheight = cursor.row()->height();
2687 setHeightOfRow(bview, cursor.row());
2688 if (cursor.row()->height() != tmpheight) {
2689 refresh_y = cursor.y() - cursor.row()->baseline();
2690 refresh_row = cursor.row();
2691 status(bview, LyXText::NEED_MORE_REFRESH);
2697 if (cursor.par()->previous()) {
2698 setUndo(bview, Undo::DELETE,
2699 cursor.par()->previous(), cursor.par()->next());
2702 Paragraph * tmppar = cursor.par();
2703 Row * tmprow = cursor.row();
2705 // We used to do cursorLeftIntern() here, but it is
2706 // not a good idea since it triggers the auto-delete
2707 // mechanism. So we do a cursorLeftIntern()-lite,
2708 // without the dreaded mechanism. (JMarc)
2709 if (cursor.par()->previous()) {
2710 // steps into the above paragraph.
2711 setCursorIntern(bview, cursor.par()->previous(),
2712 cursor.par()->previous()->size(),
2716 // Pasting is not allowed, if the paragraphs have different
2717 // layout. I think it is a real bug of all other
2718 // word processors to allow it. It confuses the user.
2719 // Even so with a footnote paragraph and a non-footnote
2720 // paragraph. I will not allow pasting in this case,
2721 // because the user would be confused if the footnote behaves
2722 // different wether it is open or closed.
2724 // Correction: Pasting is always allowed with standard-layout
2725 LyXTextClass const & tclass =
2726 bview->buffer()->params.getLyXTextClass();
2728 if (cursor.par() != tmppar
2729 && (cursor.par()->layout() == tmppar->layout()
2730 || tmppar->layout() == tclass.defaultLayout())
2731 && cursor.par()->getAlign() == tmppar->getAlign()) {
2732 removeParagraph(tmprow);
2734 mergeParagraph(bview->buffer()->params, cursor.par());
2736 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2737 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2738 // strangely enough it seems that commenting out the line above removes
2739 // most or all of the segfaults. I will however also try to move the
2740 // two Remove... lines in front of the PasteParagraph too.
2743 cursor.pos(cursor.pos() - 1);
2745 status(bview, LyXText::NEED_MORE_REFRESH);
2746 refresh_row = cursor.row();
2747 refresh_y = cursor.y() - cursor.row()->baseline();
2749 // remove the lost paragraph
2750 // This one is not safe, since the paragraph that the tmprow and the
2751 // following rows belong to has been deleted by the PasteParagraph
2752 // above. The question is... could this be moved in front of the
2754 //RemoveParagraph(tmprow);
2755 //RemoveRow(tmprow);
2757 // This rebuilds the rows.
2758 appendParagraph(bview, cursor.row());
2759 updateCounters(bview);
2761 // the row may have changed, block, hfills etc.
2762 setCursor(bview, cursor.par(), cursor.pos(), false);
2765 // this is the code for a normal backspace, not pasting
2767 setUndo(bview, Undo::DELETE,
2768 cursor.par(), cursor.par()->next());
2769 // We used to do cursorLeftIntern() here, but it is
2770 // not a good idea since it triggers the auto-delete
2771 // mechanism. So we do a cursorLeftIntern()-lite,
2772 // without the dreaded mechanism. (JMarc)
2773 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2774 false, cursor.boundary());
2776 // some insets are undeletable here
2777 if (cursor.par()->isInset(cursor.pos())) {
2778 if (!cursor.par()->getInset(cursor.pos())->deletable())
2780 // force complete redo when erasing display insets
2781 // this is a cruel method but safe..... Matthias
2782 if (cursor.par()->getInset(cursor.pos())->display() ||
2783 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2784 cursor.par()->erase(cursor.pos());
2785 redoParagraph(bview);
2790 Row * row = cursor.row();
2791 int y = cursor.y() - row->baseline();
2793 // remember that a space at the end of a row doesnt count
2794 // when calculating the fill
2795 if (cursor.pos() < row->lastPos() ||
2796 !cursor.par()->isLineSeparator(cursor.pos())) {
2797 row->fill(row->fill() + singleWidth(bview,
2802 // some special code when deleting a newline. This is similar
2803 // to the behavior when pasting paragraphs
2804 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2805 cursor.par()->erase(cursor.pos());
2806 // refresh the positions
2808 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2809 tmprow = tmprow->next();
2810 tmprow->pos(tmprow->pos() - 1);
2812 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2813 cursor.pos(cursor.pos() - 1);
2815 if (cursor.pos() < cursor.par()->size()
2816 && !cursor.par()->isSeparator(cursor.pos())) {
2817 cursor.par()->insertChar(cursor.pos(), ' ');
2818 setCharFont(bview->buffer(), cursor.par(),
2819 cursor.pos(), current_font);
2820 // refresh the positions
2822 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2823 tmprow = tmprow->next();
2824 tmprow->pos(tmprow->pos() + 1);
2828 cursor.par()->erase(cursor.pos());
2830 // refresh the positions
2832 while (tmprow->next()
2833 && tmprow->next()->par() == row->par()) {
2834 tmprow = tmprow->next();
2835 tmprow->pos(tmprow->pos() - 1);
2838 // delete newlines at the beginning of paragraphs
2839 while (!cursor.par()->empty() &&
2840 cursor.par()->isNewline(cursor.pos()) &&
2841 cursor.pos() == cursor.par()->beginningOfMainBody()) {
2842 cursor.par()->erase(cursor.pos());
2843 // refresh the positions
2845 while (tmprow->next() &&
2846 tmprow->next()->par() == row->par()) {
2847 tmprow = tmprow->next();
2848 tmprow->pos(tmprow->pos() - 1);
2853 // is there a break one row above
2854 if (row->previous() && row->previous()->par() == row->par()) {
2855 z = nextBreakPoint(bview, row->previous(),
2857 if (z >= row->pos()) {
2860 Row * tmprow = row->previous();
2862 // maybe the current row is now empty
2863 if (row->pos() >= row->par()->size()) {
2868 breakAgainOneRow(bview, row);
2869 if (row->next() && row->next()->par() == row->par())
2870 need_break_row = row->next();
2875 // set the dimensions of the row above
2876 y -= tmprow->height();
2877 tmprow->fill(fill(bview, tmprow,
2879 setHeightOfRow(bview, tmprow);
2882 refresh_row = tmprow;
2883 status(bview, LyXText::NEED_MORE_REFRESH);
2884 setCursor(bview, cursor.par(), cursor.pos(),
2885 false, cursor.boundary());
2886 //current_font = rawtmpfont;
2887 //real_current_font = realtmpfont;
2888 // check, whether the last character's font has changed.
2890 cursor.par()->getFontSettings(bview->buffer()->params,
2891 cursor.par()->size() - 1))
2892 redoHeightOfParagraph(bview, cursor);
2897 // break the cursor row again
2898 if (row->next() && row->next()->par() == row->par() &&
2899 (row->lastPos() == row->par()->size() - 1 ||
2900 nextBreakPoint(bview, row, workWidth(bview)) != row->lastPos())) {
2902 // it can happen that a paragraph loses one row
2903 // without a real breakup. This is when a word
2904 // is to long to be broken. Well, I don t care this
2906 if (row->lastPos() == row->par()->size() - 1)
2907 removeRow(row->next());
2911 status(bview, LyXText::NEED_MORE_REFRESH);
2913 breakAgainOneRow(bview, row);
2914 // will the cursor be in another row now?
2915 if (row->next() && row->next()->par() == row->par() &&
2916 row->lastPos() <= cursor.pos()) {
2918 breakAgainOneRow(bview, row);
2921 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2923 if (row->next() && row->next()->par() == row->par())
2924 need_break_row = row->next();
2928 // set the dimensions of the row
2929 row->fill(fill(bview, row, workWidth(bview)));
2930 int const tmpheight = row->height();
2931 setHeightOfRow(bview, row);
2932 if (tmpheight == row->height())
2933 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2935 status(bview, LyXText::NEED_MORE_REFRESH);
2938 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2942 // current_font = rawtmpfont;
2943 // real_current_font = realtmpfont;
2945 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2946 != cursor.boundary())
2947 setCursor(bview, cursor.par(), cursor.pos(), false,
2948 !cursor.boundary());
2950 lastpos = cursor.par()->size();
2951 if (cursor.pos() == lastpos)
2952 setCurrentFont(bview);
2954 // check, whether the last characters font has changed.
2956 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2957 redoHeightOfParagraph(bview, cursor);
2959 // now the special right address boxes
2960 if (cursor.par()->layout()->margintype
2961 == MARGIN_RIGHT_ADDRESS_BOX) {
2962 redoDrawingOfParagraph(bview, cursor);
2968 bool LyXText::paintRowBackground(DrawRowParams & p)
2970 bool clear_area = true;
2972 LyXFont font(LyXFont::ALL_SANE);
2974 pos_type const last = p.row->lastPrintablePos();
2976 if (!p.bv->screen().forceClear() && last == p.row->pos()
2977 && p.row->par()->isInset(p.row->pos())) {
2978 inset = p.row->par()->getInset(p.row->pos());
2980 clear_area = inset->doClearArea();
2990 int const y = p.yo < 0 ? 0 : p.yo;
2991 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
2992 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
2999 int h = p.row->baseline() - inset->ascent(p.bv, font);
3001 // first clear the whole row above the inset!
3003 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3006 // clear the space below the inset!
3007 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3008 if ((p.row->height() - h) > 0) {
3009 p.pain->fillRectangle(p.xo, p.yo + h,
3010 p.width, p.row->height() - h, backgroundColor());
3013 // clear the space behind the inset, if needed
3014 if (!inset->display() && !inset->needFullRow()) {
3015 int const xp = int(p.x) + inset->width(p.bv, font);
3016 if (p.width - xp > 0) {
3017 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3018 p.row->height(), backgroundColor());
3026 void LyXText::paintRowSelection(DrawRowParams & p)
3028 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3030 // the current selection
3031 int const startx = selection.start.x();
3032 int const endx = selection.end.x();
3033 int const starty = selection.start.y();
3034 int const endy = selection.end.y();
3035 Row const * startrow = selection.start.row();
3036 Row const * endrow = selection.end.row();
3040 if (bidi_same_direction) {
3044 int h = row->height();
3046 if (startrow == row && endrow == row) {
3047 if (startx < endx) {
3050 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3054 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3056 } else if (startrow == row) {
3057 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3058 int const w = (is_rtl) ? startx : (p.width - startx);
3059 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3060 } else if (endrow == row) {
3061 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3062 int const w = (is_rtl) ? (p.width - endx) : endx;
3063 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3064 } else if (p.y > starty && p.y < endy) {
3065 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3068 } else if (startrow != row && endrow != row) {
3069 if (p.y > starty && p.y < endy) {
3071 int h = row->height();
3072 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3077 if ((startrow != row && !is_rtl) || (endrow != row && is_rtl))
3078 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3080 Buffer const * buffer = p.bv->buffer();
3081 Paragraph * par = row->par();
3082 pos_type const main_body = par->beginningOfMainBody();
3083 pos_type const last = row->lastPrintablePos();
3086 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3087 pos_type pos = vis2log(vpos);
3088 float const old_tmpx = tmpx;
3089 if (main_body > 0 && pos == main_body - 1) {
3090 LyXLayout_ptr const & layout = par->layout();
3091 LyXFont const lfont = getLabelFont(buffer, par);
3093 tmpx += p.label_hfill + font_metrics::width(layout->labelsep, lfont);
3095 if (par->isLineSeparator(main_body - 1))
3096 tmpx -= singleWidth(p.bv, par, main_body - 1);
3099 if (row->hfillExpansion(pos)) {
3100 tmpx += singleWidth(p.bv, par, pos);
3101 if (pos >= main_body)
3104 tmpx += p.label_hfill;
3107 else if (par->isSeparator(pos)) {
3108 tmpx += singleWidth(p.bv, par, pos);
3109 if (pos >= main_body)
3110 tmpx += p.separator;
3112 tmpx += singleWidth(p.bv, par, pos);
3115 if ((startrow != row || selection.start.pos() <= pos) &&
3116 (endrow != row || pos < selection.end.pos())) {
3117 // Here we do not use p.x as p.xo was added to p.x.
3118 p.pain->fillRectangle(int(old_tmpx), p.yo,
3119 int(tmpx - old_tmpx + 1),
3120 row->height(), LColor::selection);
3124 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3125 p.pain->fillRectangle(p.xo + int(tmpx),
3126 p.yo, int(p.bv->workWidth() - tmpx),
3127 row->height(), LColor::selection);
3132 void LyXText::paintChangeBar(DrawRowParams & p)
3134 pos_type const start = p.row->pos();
3135 pos_type const end = p.row->lastPrintablePos();
3137 if (!p.row->par()->isChanged(start, end))
3140 int const height = (p.row->next()
3141 ? p.row->height() + p.row->next()->top_of_text()
3142 : p.row->baseline());
3144 p.pain->fillRectangle(4, p.yo, 5,
3145 height, LColor::changebar);
3149 void LyXText::paintRowAppendix(DrawRowParams & p)
3151 // FIXME: can be just p.width ?
3152 int const ww = p.bv->workWidth();
3153 Paragraph * firstpar = p.row->par();
3155 if (firstpar->params().appendix()) {
3156 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3157 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3162 void LyXText::paintRowDepthBar(DrawRowParams & p)
3164 Paragraph::depth_type const depth = p.row->par()->getDepth();
3169 Paragraph::depth_type prev_depth = 0;
3170 if (p.row->previous())
3171 prev_depth = p.row->previous()->par()->getDepth();
3172 Paragraph::depth_type next_depth = 0;
3174 next_depth = p.row->next()->par()->getDepth();
3176 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3177 int x = (PAPER_MARGIN / 5) * i + p.xo;
3178 // only consider the changebar space if we're drawing outer left
3180 x += CHANGEBAR_MARGIN;
3181 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3183 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3185 int const w = PAPER_MARGIN / 5;
3187 if (i > prev_depth) {
3188 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3190 if (i > next_depth) {
3191 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3197 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3199 if (vsp.kind() == VSpace::NONE)
3202 int const arrow_size = 4;
3203 int const space_size = int(vsp.inPixels(bv));
3207 int const min_size = max(3 * arrow_size,
3208 font_metrics::maxAscent(font)
3209 + font_metrics::maxDescent(font));
3211 if (vsp.length().len().value() < 0.0)
3214 return max(min_size, space_size);
3218 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3219 VSpace const & vsp, int start)
3221 if (vsp.kind() == VSpace::NONE)
3224 int const arrow_size = 4;
3225 int const size = getLengthMarkerHeight(p.bv, vsp);
3226 int const end = start + size;
3228 // the label to display (if any)
3230 // y-values for top arrow
3232 // y-values for bottom arrow
3235 str = prefix + " (" + vsp.asLyXCommand() + ")";
3237 switch (vsp.kind()) {
3238 case VSpace::LENGTH: {
3239 // adding or removing space
3240 bool const added = !(vsp.length().len().value() < 0.0);
3241 ty1 = added ? (start + arrow_size) : start;
3242 ty2 = added ? start : (start + arrow_size);
3243 by1 = added ? (end - arrow_size) : end;
3244 by2 = added ? end : (end - arrow_size);
3253 int const leftx = p.xo + leftMargin(p.bv, p.row);
3254 int const midx = leftx + arrow_size;
3255 int const rightx = midx + arrow_size;
3263 font.setColor(LColor::added_space).decSize();
3264 font_metrics::rectText(str, font, w, a, d);
3266 p.pain->rectText(leftx + 2 * arrow_size + 5,
3267 start + ((end - start) / 2) + d,
3270 if (vsp.kind() != VSpace::LENGTH && vsp.kind() != VSpace::VFILL )
3274 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3275 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3278 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3279 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3282 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3288 int LyXText::paintPageBreak(string const & label, int y, DrawRowParams & p)
3291 pb_font.setColor(LColor::pagebreak).decSize();
3296 font_metrics::rectText(label, pb_font, w, a, d);
3298 int const text_start = p.xo + ((p.width - w) / 2);
3299 int const text_end = text_start + w;
3301 p.pain->rectText(text_start, y + d, label, pb_font);
3303 p.pain->line(p.xo, y, text_start, y,
3304 LColor::pagebreak, Painter::line_onoffdash);
3305 p.pain->line(text_end, y, p.xo + p.width, y,
3306 LColor::pagebreak, Painter::line_onoffdash);
3308 return 3 * defaultRowHeight();
3312 void LyXText::paintFirstRow(DrawRowParams & p)
3314 Paragraph * par = p.row->par();
3315 ParagraphParameters const & parparams = par->params();
3317 // start of appendix?
3318 if (parparams.startOfAppendix()) {
3319 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3325 if (!p.row->previous() && isTopLevel())
3326 y_top += PAPER_MARGIN;
3328 // draw a top pagebreak
3329 if (parparams.pagebreakTop()) {
3330 y_top += paintPageBreak(_("Page Break (top)"),
3331 p.yo + y_top + 2 * defaultRowHeight(), p);
3334 // draw the additional space if needed:
3335 y_top += drawLengthMarker(p, _("Space above"),
3336 parparams.spaceTop(), p.yo + y_top);
3338 Buffer const * buffer = p.bv->buffer();
3340 LyXLayout_ptr const & layout = par->layout();
3342 // think about the parskip
3343 // some parskips VERY EASY IMPLEMENTATION
3344 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3345 if (par->previous()) {
3346 if (layout->latextype == LATEX_PARAGRAPH
3347 && !par->getDepth()) {
3348 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3350 LyXLayout_ptr const & playout =
3351 par->previous()->layout();
3352 if (playout->latextype == LATEX_PARAGRAPH
3353 && !par->previous()->getDepth()) {
3354 // is it right to use defskip here, too? (AS)
3355 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3361 int const ww = p.bv->workWidth();
3364 if (parparams.lineTop()) {
3365 LyXFont font(LyXFont::ALL_SANE);
3366 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3370 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3371 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3372 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3373 LColor::topline, Painter::line_solid,
3374 Painter::line_thick);
3379 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3381 // should we print a label?
3382 if (layout->labeltype >= LABEL_STATIC
3383 && (layout->labeltype != LABEL_STATIC
3384 || layout->latextype != LATEX_ENVIRONMENT
3385 || par->isFirstInSequence())) {
3387 LyXFont font = getLabelFont(buffer, par);
3388 if (!par->getLabelstring().empty()) {
3390 string const str = par->getLabelstring();
3392 // this is special code for the chapter layout. This is
3393 // printed in an extra row and has a pagebreak at
3395 if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3396 if (buffer->params.secnumdepth >= 0) {
3397 float spacing_val = 1.0;
3398 if (!parparams.spacing().isDefault()) {
3399 spacing_val = parparams.spacing().getValue();
3401 spacing_val = buffer->params.spacing.getValue();
3405 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
3406 + int(layout->parsep) * defaultRowHeight();
3409 x = ww - leftMargin(p.bv, p.row) -
3410 font_metrics::width(str, font);
3413 p.pain->text(int(x),
3414 p.yo + p.row->baseline() -
3415 p.row->ascent_of_text() - maxdesc,
3420 x = ww - leftMargin(p.bv, p.row)
3421 + font_metrics::width(layout->labelsep, font);
3423 x = p.x - font_metrics::width(layout->labelsep, font)
3424 - font_metrics::width(str, font);
3427 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3430 // the labels at the top of an environment.
3431 // More or less for bibliography
3432 } else if (par->isFirstInSequence() &&
3433 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3434 layout->labeltype == LABEL_BIBLIO ||
3435 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3436 LyXFont font = getLabelFont(buffer, par);
3437 if (!par->getLabelstring().empty()) {
3438 string const str = par->getLabelstring();
3439 float spacing_val = 1.0;
3440 if (!parparams.spacing().isDefault()) {
3441 spacing_val = parparams.spacing().getValue();
3443 spacing_val = buffer->params.spacing.getValue();
3447 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
3448 + (layout->labelbottomsep * defaultRowHeight()));
3451 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3452 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3453 + ww - rightMargin(buffer, p.row)) / 2;
3454 x -= font_metrics::width(str, font) / 2;
3455 } else if (is_rtl) {
3456 x = ww - leftMargin(p.bv, p.row) -
3457 font_metrics::width(str, font);
3459 p.pain->text(int(x), p.yo + p.row->baseline()
3460 - p.row->ascent_of_text() - maxdesc,
3465 if (layout->labeltype == LABEL_BIBLIO && par->bibkey) {
3466 LyXFont font = getLayoutFont(buffer, par);
3469 x = ww - leftMargin(p.bv, p.row)
3470 + font_metrics::width(layout->labelsep, font);
3472 x = p.x - font_metrics::width(layout->labelsep, font)
3473 - par->bibkey->width(p.bv, font);
3475 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3480 void LyXText::paintLastRow(DrawRowParams & p)
3482 Paragraph * par = p.row->par();
3483 ParagraphParameters const & parparams = par->params();
3484 int y_bottom = p.row->height() - 1;
3486 // the bottom margin
3487 if (!p.row->next() && isTopLevel())
3488 y_bottom -= PAPER_MARGIN;
3490 int const ww = p.bv->workWidth();
3492 // draw a bottom pagebreak
3493 if (parparams.pagebreakBottom()) {
3494 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
3495 p.yo + y_bottom - 2 * defaultRowHeight(), p);
3498 // draw the additional space if needed:
3499 int const height = getLengthMarkerHeight(p.bv,
3500 parparams.spaceBottom());
3501 y_bottom -= drawLengthMarker(p, _("Space below"),
3502 parparams.spaceBottom(),
3503 p.yo + y_bottom - height);
3505 Buffer const * buffer = p.bv->buffer();
3507 // draw a bottom line
3508 if (parparams.lineBottom()) {
3509 LyXFont font(LyXFont::ALL_SANE);
3510 int const asc = font_metrics::ascent('x',
3511 getFont(buffer, par,
3512 max(pos_type(0), par->size() - 1)));
3516 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3517 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3518 int const y = p.yo + y_bottom;
3519 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3520 Painter::line_thick);
3525 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3526 int const endlabel = par->getEndLabel();
3531 case END_LABEL_FILLED_BOX:
3533 LyXFont const font = getLabelFont(buffer, par);
3534 int const size = int(0.75 * font_metrics::maxAscent(font));
3535 int const y = (p.yo + p.row->baseline()) - size;
3536 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
3538 if (p.row->fill() <= size)
3539 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3541 if (endlabel == END_LABEL_BOX) {
3542 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3544 p.pain->fillRectangle(x, y, size, size,
3549 case END_LABEL_STATIC:
3552 LyXFont font(LyXFont::ALL_SANE);
3553 font = getLabelFont(buffer, par);
3555 LyXFont font = getLabelFont(buffer, par);
3557 string const & str = par->layout()->endlabelstring();
3558 int const x = is_rtl ?
3559 int(p.x) - font_metrics::width(str, font)
3560 : ww - rightMargin(buffer, p.row) - p.row->fill();
3561 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3564 case END_LABEL_NO_LABEL:
3570 void LyXText::paintRowText(DrawRowParams & p)
3572 Paragraph * par = p.row->par();
3573 Buffer const * buffer = p.bv->buffer();
3575 pos_type const last = p.row->lastPrintablePos();
3576 pos_type main_body = par->beginningOfMainBody();
3577 if (main_body > 0 &&
3578 (main_body - 1 > last ||
3579 !par->isLineSeparator(main_body - 1))) {
3583 LyXLayout_ptr const & layout = par->layout();
3585 bool running_strikeout = false;
3586 bool is_struckout = false;
3587 float last_strikeout_x = 0.0;
3589 pos_type vpos = p.row->pos();
3590 while (vpos <= last) {
3591 if (p.x > p.bv->workWidth())
3593 pos_type pos = vis2log(vpos);
3595 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3596 p.x += singleWidth(p.bv, par, pos);
3601 is_struckout = isDeletedText(par, pos);
3603 if (is_struckout && !running_strikeout) {
3604 running_strikeout = true;
3605 last_strikeout_x = p.x;
3608 bool const highly_editable_inset = par->isInset(pos)
3609 && isHighlyEditableInset(par->getInset(pos));
3611 // if we reach the end of a struck out range, paint it
3612 // we also don't paint across things like tables
3613 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
3614 int const middle = p.yo + p.row->top_of_text()
3615 + ((p.row->baseline() - p.row->top_of_text()) / 2);
3616 p.pain->line(int(last_strikeout_x), middle, int(p.x), middle,
3617 LColor::strikeout, Painter::line_solid, Painter::line_thin);
3618 running_strikeout = false;
3621 if (main_body > 0 && pos == main_body - 1) {
3622 int const lwidth = font_metrics::width(layout->labelsep,
3623 getLabelFont(buffer, par));
3625 p.x += p.label_hfill + lwidth
3626 - singleWidth(p.bv, par, main_body - 1);
3629 if (par->isHfill(pos)) {
3632 int const y0 = p.yo + p.row->baseline();
3633 int const y1 = y0 - defaultRowHeight() / 2;
3635 p.pain->line(int(p.x), y1, int(p.x), y0,
3636 LColor::added_space);
3638 if (p.row->hfillExpansion(pos)) {
3639 int const y2 = (y0 + y1) / 2;
3641 if (pos >= main_body) {
3642 p.pain->line(int(p.x), y2,
3643 int(p.x + p.hfill), y2,
3644 LColor::added_space,
3645 Painter::line_onoffdash);
3648 p.pain->line(int(p.x), y2,
3649 int(p.x + p.label_hfill), y2,
3650 LColor::added_space,
3651 Painter::line_onoffdash);
3652 p.x += p.label_hfill;
3654 p.pain->line(int(p.x), y1,
3656 LColor::added_space);
3660 } else if (par->isSeparator(pos)) {
3661 p.x += singleWidth(p.bv, par, pos);
3662 if (pos >= main_body)
3671 // if we reach the end of a struck out range, paint it
3672 if (running_strikeout) {
3673 int const middle = p.yo + p.row->top_of_text()
3674 + ((p.row->baseline() - p.row->top_of_text()) / 2);
3675 p.pain->line(int(last_strikeout_x), middle, int(p.x), middle,
3676 LColor::strikeout, Painter::line_solid, Painter::line_thin);
3677 running_strikeout = false;
3682 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3683 Row * row, int y, bool cleared)
3685 if (row->height() <= 0) {
3686 lyxerr << "LYX_ERROR: row.height: "
3687 << row->height() << endl;
3693 // set up drawing parameters
3695 p.pain = &bv->painter();
3699 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3700 if (inset_owner && (p.x < 0))
3704 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3705 p.cleared = cleared;
3709 // clear to background if necessary
3710 p.cleared = paintRowBackground(p);
3712 // paint the selection background
3713 if (selection.set()) {
3714 paintRowSelection(p);
3717 // vertical lines for appendix
3718 paintRowAppendix(p);
3720 // environment depth brackets
3721 paintRowDepthBar(p);
3726 // draw any stuff wanted for a first row of a paragraph
3731 // draw any stuff wanted for the last row of a paragraph
3732 if (!row->next() || (row->next()->par() != row->par())) {
3741 // returns the column near the specified x-coordinate of the row
3742 // x is set to the real beginning of this column
3744 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3745 bool & boundary) const
3748 float fill_separator;
3750 float fill_label_hfill;
3752 prepareToPrint(bview, row, tmpx, fill_separator,
3753 fill_hfill, fill_label_hfill);
3755 pos_type vc = row->pos();
3756 pos_type last = row->lastPrintablePos();
3759 LyXLayout_ptr const & layout = row->par()->layout();
3761 bool left_side = false;
3763 pos_type main_body = row->par()->beginningOfMainBody();
3764 float last_tmpx = tmpx;
3766 if (main_body > 0 &&
3767 (main_body - 1 > last ||
3768 !row->par()->isLineSeparator(main_body - 1)))
3771 // check for empty row
3772 if (!row->par()->size()) {
3777 while (vc <= last && tmpx <= x) {
3780 if (main_body > 0 && c == main_body-1) {
3781 tmpx += fill_label_hfill +
3782 font_metrics::width(layout->labelsep,
3783 getLabelFont(bview->buffer(), row->par()));
3784 if (row->par()->isLineSeparator(main_body - 1))
3785 tmpx -= singleWidth(bview, row->par(), main_body-1);
3788 if (row->hfillExpansion(c)) {
3789 tmpx += singleWidth(bview, row->par(), c);
3793 tmpx += fill_label_hfill;
3794 } else if (row->par()->isSeparator(c)) {
3795 tmpx += singleWidth(bview, row->par(), c);
3797 tmpx+= fill_separator;
3799 tmpx += singleWidth(bview, row->par(), c);
3804 if ((tmpx + last_tmpx) / 2 > x) {
3809 if (vc > last + 1) // This shouldn't happen.
3813 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3814 // some speedup if rtl_support=false
3815 && (!row->next() || row->next()->par() != row->par());
3816 bool const rtl = (lastrow)
3817 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3818 : false; // If lastrow is false, we don't need to compute
3819 // the value of rtl.
3822 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3823 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3825 else if (vc == row->pos()) {
3827 if (bidi_level(c) % 2 == 1)
3830 c = vis2log(vc - 1);
3831 bool const rtl = (bidi_level(c) % 2 == 1);
3832 if (left_side == rtl) {
3834 boundary = isBoundary(bview->buffer(), row->par(), c);
3838 if (row->pos() <= last && c > last
3839 && row->par()->isNewline(last)) {
3840 if (bidi_level(last) % 2 == 0)
3841 tmpx -= singleWidth(bview, row->par(), last);
3843 tmpx += singleWidth(bview, row->par(), last);
3853 // returns pointer to a specified row
3854 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3859 Row * tmprow = firstrow;
3862 // find the first row of the specified paragraph
3863 while (tmprow->next() && tmprow->par() != par) {
3864 y += tmprow->height();
3865 tmprow = tmprow->next();
3868 // now find the wanted row
3869 while (tmprow->pos() < pos
3871 && tmprow->next()->par() == par
3872 && tmprow->next()->pos() <= pos) {
3873 y += tmprow->height();
3874 tmprow = tmprow->next();
3881 Row * LyXText::getRowNearY(int & y) const
3884 // If possible we should optimize this method. (Lgb)
3885 Row * tmprow = firstrow;
3888 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3889 tmpy += tmprow->height();
3890 tmprow = tmprow->next();
3893 y = tmpy; // return the real y
3895 //lyxerr << "returned y = " << y << endl;
3899 // Search from the current cursor position.
3901 Row * tmprow = cursor.row();
3902 int tmpy = cursor.y() - tmprow->baseline();
3904 lyxerr << "cursor.y() = " << tmpy << endl;
3905 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3906 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3907 lyxerr << "first = " << first << endl;
3908 lyxerr << "y = " << y << endl;
3911 lyxerr << "up" << endl;
3913 tmpy -= tmprow->height();
3914 tmprow = tmprow->previous();
3915 } while (tmprow && tmpy - tmprow->height() >= y);
3916 } else if (y > tmpy) {
3917 lyxerr << "down" << endl;
3919 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3920 tmpy += tmprow->height();
3921 tmprow = tmprow->next();
3924 lyxerr << "equal" << endl;
3927 y = tmpy; // return the real y
3929 lyxerr << "returned y = " << y << endl;
3937 int LyXText::getDepth() const
3939 return cursor.par()->getDepth();