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"
22 #include "frontends/LyXView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
25 #include "frontends/screen.h"
26 #include "bufferview_funcs.h"
27 #include "BufferView.h"
29 #include "ParagraphParameters.h"
30 #include "undo_funcs.h"
32 #include "insets/insetbib.h"
33 #include "insets/insettext.h"
35 #include "support/textutils.h"
36 #include "support/LAssert.h"
37 #include "support/lstrings.h"
49 int const LYX_PAPER_MARGIN = 20;
53 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
56 int LyXText::workWidth(BufferView * bview) const
59 return inset_owner->textWidth(bview);
61 return bview->workWidth();
65 int LyXText::workWidth(BufferView * bview, Inset * inset) const
70 par = inset->parOwner();
72 pos = par->getPositionOfInset(inset);
74 if (!par || pos == -1) {
75 lyxerr << "LyXText::workWidth: something is wrong,"
76 " fall back to the brute force method" << endl;
77 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
78 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
79 for (; it != end; ++it) {
89 return workWidth(bview);
92 LyXLayout_ptr const & layout = par->layout();
94 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
95 // Optimization here: in most cases, the real row is
96 // not needed, but only the par/pos values. So we just
97 // construct a dummy row for leftMargin. (JMarc)
101 return workWidth(bview) - leftMargin(bview, &dummyrow);
104 Row * row = getRow(par, pos, dummy_y);
106 while (frow->previous() && frow->par() == frow->previous()->par())
107 frow = frow->previous();
108 unsigned int maxw = 0;
109 while (frow->next() && frow->par() == frow->next()->par()) {
110 if ((frow != row) && (maxw < frow->width()))
111 maxw = frow->width();
117 return workWidth(bview);
121 int LyXText::getRealCursorX(BufferView * bview) const
124 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
125 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
130 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
133 if (!Encodings::is_arabic(c))
134 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
135 return c + (0xb0 - '0');
139 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
140 unsigned char next_char = ' ';
142 for (pos_type i = pos+1; i < par->size(); ++i)
143 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
144 next_char = par->getChar(i);
148 if (Encodings::is_arabic(next_char)) {
149 if (Encodings::is_arabic(prev_char))
150 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
152 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
154 if (Encodings::is_arabic(prev_char))
155 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
157 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
161 // This is the comments that some of the warnings below refers to.
162 // There are some issues in this file and I don't think they are
163 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
164 // this is a problem that has been here almost from day one and that a
165 // larger userbase with differenct access patters triggers the bad
166 // behaviour. (segfaults.) What I think happen is: In several places
167 // we store the paragraph in the current cursor and then moves the
168 // cursor. This movement of the cursor will delete paragraph at the
169 // old position if it is now empty. This will make the temporary
170 // pointer to the old cursor paragraph invalid and dangerous to use.
171 // And is some cases this will trigger a segfault. I have marked some
172 // of the cases where this happens with a warning, but I am sure there
173 // are others in this file and in text2.C. There is also a note in
174 // Delete() that you should read. In Delete I store the paragraph->id
175 // instead of a pointer to the paragraph. I am pretty sure this faulty
176 // use of temporary pointers to paragraphs that might have gotten
177 // invalidated (through a cursor movement) before they are used, are
178 // the cause of the strange crashes we get reported often.
180 // It is very tiresom to change this code, especially when it is as
181 // hard to read as it is. Help to fix all the cases where this is done
182 // would be greately appreciated.
186 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
189 char const c = par->getChar(pos);
190 return singleWidth(bview, par, pos, c);
194 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
195 pos_type pos, char c) const
197 LyXFont const font = getFont(bview->buffer(), par, pos);
199 // The most common case is handled first (Asger)
200 if (IsPrintable(c)) {
201 if (font.language()->RightToLeft()) {
202 if (font.language()->lang() == "arabic" &&
203 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
204 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
205 if (Encodings::IsComposeChar_arabic(c))
208 c = transformChar(c, par, pos);
209 else if (font.language()->lang() == "hebrew" &&
210 Encodings::IsComposeChar_hebrew(c))
213 return font_metrics::width(c, font);
215 } else if (IsHfillChar(c)) {
216 // Because of the representation as vertical lines
218 } else if (c == Paragraph::META_INSET) {
219 Inset * tmpinset = par->getInset(pos);
222 // this IS needed otherwise on initialitation we don't get the fill
223 // of the row right (ONLY on initialization if we read a file!)
224 // should be changed! (Jug 20011204)
225 tmpinset->update(bview, font);
227 return tmpinset->width(bview, font);
231 } else if (IsSeparatorChar(c))
233 else if (IsNewlineChar(c))
235 return font_metrics::width(c, font);
239 // Returns the paragraph position of the last character in the specified row
240 pos_type LyXText::rowLast(Row const * row) const
242 if (!row->next() || row->next()->par() != row->par()) {
243 return row->par()->size() - 1;
245 return row->next()->pos() - 1;
250 pos_type LyXText::rowLastPrintable(Row const * row) const
252 pos_type const last = rowLast(row);
253 bool ignore_the_space_on_the_last_position = true;
255 // we have to consider a space on the last position in this case!
256 if (row->next() && row->par() == row->next()->par() &&
257 row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
258 (ins=row->next()->par()->getInset(last+1)) &&
259 (ins->needFullRow() || ins->display()))
261 ignore_the_space_on_the_last_position = false;
263 if (last >= row->pos()
265 && row->next()->par() == row->par()
266 && row->par()->isSeparator(last)
267 && ignore_the_space_on_the_last_position)
274 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
276 bidi_same_direction = true;
277 if (!lyxrc.rtl_support) {
282 Inset * inset = row->par()->inInset();
283 if (inset && inset->owner() &&
284 inset->owner()->lyxCode() == Inset::ERT_CODE) {
289 bidi_start = row->pos();
290 bidi_end = rowLastPrintable(row);
292 if (bidi_start > bidi_end) {
297 if (bidi_end + 2 - bidi_start >
298 static_cast<pos_type>(log2vis_list.size())) {
300 (bidi_end + 2 - bidi_start < 500) ?
301 500 : 2 * (bidi_end + 2 - bidi_start);
302 log2vis_list.resize(new_size);
303 vis2log_list.resize(new_size);
304 bidi_levels.resize(new_size);
307 vis2log_list[bidi_end + 1 - bidi_start] = -1;
308 log2vis_list[bidi_end + 1 - bidi_start] = -1;
312 row->par()->isRightToLeftPar(buf->params);
316 pos_type const main_body = beginningOfMainBody(buf, row->par());
318 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
319 bool is_space = row->par()->isLineSeparator(lpos);
321 (is_space && lpos + 1 <= bidi_end &&
322 !row->par()->isLineSeparator(lpos + 1) &&
323 !row->par()->isNewline(lpos + 1))
325 LyXFont font = row->par()->getFontSettings(buf->params, pos);
326 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
327 font.number() == LyXFont::ON &&
328 row->par()->getFontSettings(buf->params, lpos - 1).number()
330 font = row->par()->getFontSettings(buf->params, lpos);
335 bool new_rtl = font.isVisibleRightToLeft();
336 bool new_rtl0 = font.isRightToLeft();
339 if (lpos == main_body - 1
340 && row->pos() < main_body - 1
342 new_level = (rtl_par) ? 1 : 0;
343 new_rtl = new_rtl0 = rtl_par;
345 new_level = (new_rtl) ? 1 : 2;
347 new_level = (rtl_par) ? 2 : 0;
349 if (is_space && new_level >= level) {
355 int new_level2 = new_level;
357 if (level == new_level && rtl0 != new_rtl0) {
359 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
360 } else if (level < new_level) {
361 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
362 if (new_level > rtl_par)
363 bidi_same_direction = false;
365 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
368 bidi_levels[lpos - bidi_start] = new_level;
370 while (level > new_level2) {
371 pos_type old_lpos = stack[--level];
372 int delta = lpos - old_lpos - 1;
375 log2vis_list[lpos - bidi_start] += delta;
376 log2vis_list[old_lpos - bidi_start] += delta;
378 while (level < new_level)
379 stack[level++] = lpos;
383 pos_type const old_lpos = stack[--level];
384 int delta = bidi_end - old_lpos;
387 log2vis_list[old_lpos - bidi_start] += delta;
390 pos_type vpos = bidi_start - 1;
391 for (pos_type lpos = bidi_start;
392 lpos <= bidi_end; ++lpos) {
393 vpos += log2vis_list[lpos - bidi_start];
394 vis2log_list[vpos - bidi_start] = lpos;
395 log2vis_list[lpos - bidi_start] = vpos;
400 // This method requires a previous call to ComputeBidiTables()
401 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
404 if (!lyxrc.rtl_support || pos == 0)
407 if (!bidi_InRange(pos - 1)) {
408 /// This can happen if pos is the first char of a row.
409 /// Returning false in this case is incorrect!
413 bool const rtl = bidi_level(pos - 1) % 2;
414 bool const rtl2 = bidi_InRange(pos)
415 ? bidi_level(pos) % 2
416 : par->isRightToLeftPar(buf->params);
421 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
422 pos_type pos, LyXFont const & font) const
424 if (!lyxrc.rtl_support)
425 return false; // This is just for speedup
427 bool const rtl = font.isVisibleRightToLeft();
428 bool const rtl2 = bidi_InRange(pos)
429 ? bidi_level(pos) % 2
430 : par->isRightToLeftPar(buf->params);
435 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
437 // Draw end-of-line marker
438 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
439 int const wid = font_metrics::width('n', font);
440 int const asc = font_metrics::maxAscent(font);
441 int const y = p.yo + p.row->baseline();
445 yp[0] = int(y - 0.875 * asc * 0.75);
446 yp[1] = int(y - 0.500 * asc * 0.75);
447 yp[2] = int(y - 0.125 * asc * 0.75);
449 if (bidi_level(pos) % 2 == 0) {
450 xp[0] = int(p.x + wid * 0.375);
452 xp[2] = int(p.x + wid * 0.375);
454 xp[0] = int(p.x + wid * 0.625);
455 xp[1] = int(p.x + wid);
456 xp[2] = int(p.x + wid * 0.625);
459 p.pain->lines(xp, yp, 3, LColor::eolmarker);
461 yp[0] = int(y - 0.500 * asc * 0.75);
462 yp[1] = int(y - 0.500 * asc * 0.75);
463 yp[2] = int(y - asc * 0.75);
465 if (bidi_level(pos) % 2 == 0) {
467 xp[1] = int(p.x + wid);
468 xp[2] = int(p.x + wid);
470 xp[0] = int(p.x + wid);
475 p.pain->lines(xp, yp, 3, LColor::eolmarker);
481 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
483 Inset * inset = p.row->par()->getInset(pos);
485 // FIXME: shouldn't happen
490 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
491 // we need this here as the row pointer may be illegal
492 // at a later time (Jug20020502)
493 Row * prev = p.row->previous();
495 inset->update(p.bv, font, false);
496 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
498 if (!need_break_row && !inset_owner
499 && p.bv->text->status() == CHANGED_IN_DRAW) {
500 if (prev && prev->par() == p.row->par()) {
501 breakAgainOneRow(p.bv, prev);
502 if (prev->next() != p.row) {
503 // breakAgainOneRow() has removed p.row
504 p.row = 0; // see what this breaks
505 need_break_row = prev;
507 need_break_row = p.row;
510 need_break_row = firstrow;
512 need_break_row = prev->next();
514 setCursor(p.bv, cursor.par(), cursor.pos());
521 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
523 if (!lyxrc.mark_foreign_language)
525 if (orig_font.language() == latex_language)
527 if (orig_font.language() == p.bv->buffer()->params.language)
530 int const y = p.yo + p.row->height() - 1;
531 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
535 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
537 pos_type pos = vis2log(vpos);
542 char c = p.row->par()->getChar(pos);
546 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
547 int const width = font_metrics::width(c, font);
550 for (pos_type i = pos-1; i >= 0; --i) {
551 c = p.row->par()->getChar(i);
552 if (!Encodings::IsComposeChar_hebrew(c)) {
553 if (IsPrintableNonspace(c)) {
555 singleWidth(p.bv, p.row->par(), i, c);
557 dx = (c == 'ø' || c == 'ã')
559 : (width2 - width) / 2;
566 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
570 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
572 pos_type pos = vis2log(vpos);
576 char c = p.row->par()->getChar(pos);
577 c = transformChar(c, p.row->par(), pos);
581 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
582 int const width = font_metrics::width(c, font);
585 for (pos_type i = pos-1; i >= 0; --i) {
586 c = p.row->par()->getChar(i);
587 if (!Encodings::IsComposeChar_arabic(c)) {
588 if (IsPrintableNonspace(c)) {
590 singleWidth(p.bv, p.row->par(), i, c);
591 dx = (width2 - width) / 2;
597 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
601 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
602 bool hebrew, bool arabic)
604 pos_type pos = vis2log(vpos);
605 pos_type const last = rowLastPrintable(p.row);
606 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
610 str += p.row->par()->getChar(pos);
612 unsigned char c = str[0];
613 str[0] = transformChar(c, p.row->par(), pos);
617 // collect as much similar chars as we can
618 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
619 char c = p.row->par()->getChar(pos);
621 if (!IsPrintableNonspace(c))
624 if (arabic && Encodings::IsComposeChar_arabic(c))
626 if (hebrew && Encodings::IsComposeChar_hebrew(c))
629 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
633 c = transformChar(c, p.row->par(), pos);
638 // Draw text and set the new x position
639 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
640 p.x += font_metrics::width(str, orig_font);
644 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
646 pos_type const pos = vis2log(vpos);
647 Paragraph * par = p.row->par();
649 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
651 float const orig_x = p.x;
653 char const c = par->getChar(pos);
655 if (IsNewlineChar(c)) {
659 } else if (IsInsetChar(c)) {
660 if (!drawInset(p, pos))
663 drawForeignMark(p, orig_x, orig_font);
667 // usual characters, no insets
669 // special case languages
670 bool const hebrew = (orig_font.language()->lang() == "hebrew");
672 orig_font.language()->lang() == "arabic" &&
673 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
674 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
676 // draw as many chars as we can
677 if ((!hebrew && !arabic)
678 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
679 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
680 drawChars(p, vpos, hebrew, arabic);
682 drawHebrewComposeChar(p, vpos);
684 drawArabicComposeChar(p, vpos);
687 drawForeignMark(p, orig_x, orig_font);
689 #ifdef INHERIT_LANGUAGE
691 if ((font.language() == inherit_language) ||
692 (font.language() == ignore_language))
693 lyxerr << "No this shouldn't happen!\n";
700 int LyXText::leftMargin(BufferView * bview, Row const * row) const
703 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
704 (ins=row->par()->getInset(row->pos())) &&
705 (ins->needFullRow() || ins->display()))
706 return LYX_PAPER_MARGIN;
708 LyXTextClass const & tclass =
709 bview->buffer()->params.getLyXTextClass();
710 LyXLayout_ptr const & layout = row->par()->layout();
712 string parindent = layout->parindent;
714 int x = LYX_PAPER_MARGIN;
716 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
718 // this is the way, LyX handles the LaTeX-Environments.
719 // I have had this idea very late, so it seems to be a
720 // later added hack and this is true
721 if (!row->par()->getDepth()) {
722 if (row->par()->layout() == tclass.defaultLayout()) {
723 // find the previous same level paragraph
724 if (row->par()->previous()) {
725 Paragraph * newpar = row->par()
726 ->depthHook(row->par()->getDepth());
728 newpar->layout()->nextnoindent)
733 // find the next level paragraph
736 row->par()->outerHook();
738 // make a corresponding row. Needed to call LeftMargin()
740 // check wether it is a sufficent paragraph
741 if (newpar && newpar->layout()->isEnvironment()) {
743 dummyrow.par(newpar);
744 dummyrow.pos(newpar->size());
745 x = leftMargin(bview, &dummyrow);
747 // this is no longer an error, because this function
748 // is used to clear impossible depths after changing
749 // a layout. Since there is always a redo,
750 // LeftMargin() is always called
751 row->par()->params().depth(0);
754 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
755 if (newpar->params().noindent())
758 parindent = newpar->layout()->parindent;
764 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
765 switch (layout->margintype) {
767 if (!layout->leftmargin.empty()) {
768 x += font_metrics::signedWidth(layout->leftmargin,
769 tclass.defaultfont());
771 if (!row->par()->getLabelstring().empty()) {
772 x += font_metrics::signedWidth(layout->labelindent,
774 x += font_metrics::width(row->par()->getLabelstring(),
776 x += font_metrics::width(layout->labelsep, labelfont);
780 x += font_metrics::signedWidth(layout->labelindent, labelfont);
781 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
782 if (!row->par()->getLabelWidthString().empty()) {
783 x += font_metrics::width(row->par()->getLabelWidthString(),
785 x += font_metrics::width(layout->labelsep, labelfont);
790 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
791 / (row->par()->getDepth() + 4);
793 case MARGIN_FIRST_DYNAMIC:
794 if (layout->labeltype == LABEL_MANUAL) {
795 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
796 x += font_metrics::signedWidth(layout->leftmargin,
799 x += font_metrics::signedWidth(layout->labelindent,
802 } else if (row->pos()
803 // Special case to fix problems with
805 || (layout->labeltype == LABEL_STATIC
806 && layout->latextype == LATEX_ENVIRONMENT
807 && ! row->par()->isFirstInSequence())) {
808 x += font_metrics::signedWidth(layout->leftmargin,
810 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
811 && layout->labeltype != LABEL_BIBLIO
812 && layout->labeltype !=
813 LABEL_CENTERED_TOP_ENVIRONMENT) {
814 x += font_metrics::signedWidth(layout->labelindent,
816 x += font_metrics::width(layout->labelsep, labelfont);
817 x += font_metrics::width(row->par()->getLabelstring(),
822 case MARGIN_RIGHT_ADDRESS_BOX:
824 // ok, a terrible hack. The left margin depends on the widest
825 // row in this paragraph. Do not care about footnotes, they
826 // are *NOT* allowed in the LaTeX realisation of this layout.
828 // find the first row of this paragraph
829 Row const * tmprow = row;
830 while (tmprow->previous()
831 && tmprow->previous()->par() == row->par())
832 tmprow = tmprow->previous();
834 int minfill = tmprow->fill();
835 while (tmprow->next() && tmprow->next()->par() == row->par()) {
836 tmprow = tmprow->next();
837 if (tmprow->fill() < minfill)
838 minfill = tmprow->fill();
841 x += font_metrics::signedWidth(layout->leftmargin,
842 tclass.defaultfont());
848 if ((workWidth(bview) > 0) &&
849 !row->par()->params().leftIndent().zero())
851 LyXLength const len = row->par()->params().leftIndent();
852 int const tw = inset_owner ?
853 inset_owner->latexTextWidth(bview) : workWidth(bview);
854 x += len.inPixels(tw, bview->text->defaultHeight());
857 LyXAlignment align; // wrong type
859 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
860 align = layout->align;
862 align = row->par()->params().align();
864 // set the correct parindent
865 if (row->pos() == 0) {
866 if ((layout->labeltype == LABEL_NO_LABEL
867 || layout->labeltype == LABEL_TOP_ENVIRONMENT
868 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
869 || (layout->labeltype == LABEL_STATIC
870 && layout->latextype == LATEX_ENVIRONMENT
871 && ! row->par()->isFirstInSequence()))
872 && align == LYX_ALIGN_BLOCK
873 && !row->par()->params().noindent()
874 // in tabulars and ert paragraphs are never indented!
875 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
876 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
877 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
878 && (row->par()->layout() != tclass.defaultLayout() ||
879 bview->buffer()->params.paragraph_separation ==
880 BufferParams::PARSEP_INDENT)) {
881 x += font_metrics::signedWidth(parindent,
882 tclass.defaultfont());
883 } else if (layout->labeltype == LABEL_BIBLIO) {
884 // ale970405 Right width for bibitems
885 x += bibitemMaxWidth(bview, tclass.defaultfont());
893 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
896 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
897 (ins=row->par()->getInset(row->pos())) &&
898 (ins->needFullRow() || ins->display()))
899 return LYX_PAPER_MARGIN;
901 LyXTextClass const & tclass = buf->params.getLyXTextClass();
902 LyXLayout_ptr const & layout = row->par()->layout();
904 int x = LYX_PAPER_MARGIN
905 + font_metrics::signedWidth(tclass.rightmargin(),
906 tclass.defaultfont());
908 // this is the way, LyX handles the LaTeX-Environments.
909 // I have had this idea very late, so it seems to be a
910 // later added hack and this is true
911 if (row->par()->getDepth()) {
912 // find the next level paragraph
914 Paragraph * newpar = row->par();
917 newpar = newpar->previous();
919 && newpar->getDepth() >= row->par()->getDepth());
921 // make a corresponding row. Needed to call LeftMargin()
923 // check wether it is a sufficent paragraph
924 if (newpar && newpar->layout()->isEnvironment()) {
926 dummyrow.par(newpar);
928 x = rightMargin(buf, &dummyrow);
930 // this is no longer an error, because this function
931 // is used to clear impossible depths after changing
932 // a layout. Since there is always a redo,
933 // LeftMargin() is always called
934 row->par()->params().depth(0);
938 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
939 x += font_metrics::signedWidth(layout->rightmargin,
940 tclass.defaultfont())
941 * 4 / (row->par()->getDepth() + 4);
946 int LyXText::labelEnd(BufferView * bview, Row const * row) const
948 if (row->par()->layout()->margintype == MARGIN_MANUAL) {
951 tmprow.pos(row->par()->size());
952 // just the beginning of the main body
953 return leftMargin(bview, &tmprow);
955 // LabelEnd is only needed,
956 // if the layout fills a flushleft label.
962 // get the next breakpoint in a given paragraph
964 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
966 Paragraph * par = row->par();
971 pos_type const pos = row->pos();
973 // position of the last possible breakpoint
974 // -1 isn't a suitable value, but a flag
975 pos_type last_separator = -1;
976 width -= rightMargin(bview->buffer(), row);
978 pos_type const main_body =
979 beginningOfMainBody(bview->buffer(), par);
980 LyXLayout_ptr const & layout = par->layout();
984 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
985 // special code for right address boxes, only newlines count
986 while (i < par->size()) {
987 if (par->isNewline(i)) {
989 i = par->size() - 1; // this means break
991 } else if (par->isInset(i) && par->getInset(i)
992 && par->getInset(i)->display()) {
993 par->getInset(i)->display(false);
998 // Last position is an invariant
999 pos_type const last = par->size();
1000 // this is the usual handling
1001 int x = leftMargin(bview, row);
1002 bool doitonetime = true;
1003 while (doitonetime || ((x < width) && (i < last))) {
1004 doitonetime = false;
1005 char const c = par->getChar(i);
1007 if (c == Paragraph::META_INSET)
1008 in = par->getInset(i);
1009 if (IsNewlineChar(c)) {
1011 x = width; // this means break
1012 } else if (in && !in->isChar()) {
1013 // check wether a Display() inset is
1014 // valid here. if not, change it to
1016 if (in->display() &&
1017 (layout->isCommand() ||
1018 (layout->labeltype == LABEL_MANUAL
1019 && i < beginningOfMainBody(bview->buffer(), par))))
1021 // display istn't allowd
1023 x += singleWidth(bview, par, i, c);
1024 } else if (in->display() || in->needFullRow()) {
1025 // So break the line here
1029 if (par->isLineSeparator(i+1))
1032 last_separator = last; // to avoid extra rows
1034 last_separator = i - 1;
1035 x = width; // this means break
1037 x += singleWidth(bview, par, i, c);
1038 // we have to check this separately as we could have a
1039 // lineseparator and then the algorithm below would prefer
1040 // that which IS wrong! We should always break on an inset
1041 // if it's too long and not on the last separator.
1042 // Maybe the only exeption is insets used as chars but
1043 // then we would have to have a special function inside
1044 // the inset to tell us this. Till then we leave it as
1045 // it is now. (Jug 20020106)
1046 if (pos < i && x >= width && last_separator >= 0)
1047 last_separator = i - 1;
1050 if (par->isLineSeparator(i))
1052 x += singleWidth(bview, par, i, c);
1055 if (i == main_body) {
1056 x += font_metrics::width(layout->labelsep,
1057 getLabelFont(bview->buffer(), par));
1058 if (par->isLineSeparator(i - 1))
1059 x-= singleWidth(bview, par, i - 1);
1060 int left_margin = labelEnd(bview, row);
1061 if (x < left_margin)
1065 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1066 last_separator = i - 2;
1067 else if ((pos < i) && (last_separator < 0) && (x >= width))
1068 last_separator = i - 1;
1069 // end of paragraph is always a suitable separator
1070 else if (i == last && x < width)
1074 // well, if last_separator is still 0, the line isn't breakable.
1075 // don't care and cut simply at the end
1076 if (last_separator < 0) {
1080 // manual labels cannot be broken in LaTeX, do not care
1081 if (main_body && last_separator < main_body)
1082 last_separator = main_body - 1;
1084 return last_separator;
1088 // returns the minimum space a row needs on the screen in pixel
1089 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1091 if (paper_width < 0)
1095 // get the pure distance
1096 pos_type const last = rowLastPrintable(row);
1098 // special handling of the right address boxes
1099 if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1100 int const tmpfill = row->fill();
1101 row->fill(0); // the minfill in MarginLeft()
1102 w = leftMargin(bview, row);
1105 w = leftMargin(bview, row);
1107 LyXLayout_ptr const & layout = row->par()->layout();
1109 pos_type const main_body =
1110 beginningOfMainBody(bview->buffer(), row->par());
1111 pos_type i = row->pos();
1114 if (main_body > 0 && i == main_body) {
1115 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1116 if (row->par()->isLineSeparator(i - 1))
1117 w -= singleWidth(bview, row->par(), i - 1);
1118 int left_margin = labelEnd(bview, row);
1119 if (w < left_margin)
1122 w += singleWidth(bview, row->par(), i);
1125 if (main_body > 0 && main_body > last) {
1126 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1127 if (last >= 0 && row->par()->isLineSeparator(last))
1128 w -= singleWidth(bview, row->par(), last);
1129 int const left_margin = labelEnd(bview, row);
1130 if (w < left_margin)
1134 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1139 // returns the minimum space a manual label needs on the screen in pixel
1140 int LyXText::labelFill(BufferView * bview, Row const * row) const
1142 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1143 // -1 because a label ends either with a space that is in the label,
1144 // or with the beginning of a footnote that is outside the label.
1146 // I don't understand this code in depth, but sometimes "last" is
1147 // less than 0 and this causes a crash. This fix seems to work
1148 // correctly, but I bet the real error is elsewhere. The bug is
1149 // triggered when you have an open footnote in a paragraph
1150 // environment with a manual label. (Asger)
1151 if (last < 0) last = 0;
1153 // a separator at this end does not count
1154 if (row->par()->isLineSeparator(last))
1158 pos_type i = row->pos();
1160 w += singleWidth(bview, row->par(), i);
1165 if (!row->par()->params().labelWidthString().empty()) {
1166 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1167 getLabelFont(bview->buffer(), row->par())) - w,
1175 // returns the number of separators in the specified row. The separator
1176 // on the very last column doesnt count
1177 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1179 pos_type last = rowLastPrintable(row);
1180 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1183 for (; p <= last; ++p) {
1184 if (row->par()->isSeparator(p)) {
1192 // returns the number of hfills in the specified row. The LyX-Hfill is
1193 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1194 // ignored. This is *MUCH* more usefull than not to ignore!
1195 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1197 pos_type const last = rowLast(row);
1198 pos_type first = row->pos();
1201 // hfill *DO* count at the beginning of paragraphs!
1202 while (first <= last && row->par()->isHfill(first)) {
1207 first = max(first, beginningOfMainBody(buf, row->par()));
1209 for (pos_type p = first; p <= last; ++p) {
1210 // last, because the end is ignored!
1212 if (row->par()->isHfill(p)) {
1220 // like NumberOfHfills, but only those in the manual label!
1221 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1223 pos_type last = rowLast(row);
1224 pos_type first = row->pos();
1226 // hfill *DO* count at the beginning of paragraphs!
1227 while (first < last && row->par()->isHfill(first))
1231 last = min(last, beginningOfMainBody(buf, row->par()));
1233 for (pos_type p = first; p < last; ++p) {
1234 // last, because the end is ignored!
1235 if (row->par()->isHfill(p)) {
1243 // returns true, if a expansion is needed.
1244 // Rules are given by LaTeX
1245 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1248 // by the way, is it a hfill?
1249 if (!row_ptr->par()->isHfill(pos))
1252 // at the end of a row it does not count
1253 // unless another hfill exists on the line
1254 if (pos >= rowLast(row_ptr)) {
1255 pos_type i = row_ptr->pos();
1256 while (i < pos && !row_ptr->par()->isHfill(i)) {
1264 // at the beginning of a row it does not count, if it is not
1265 // the first row of a paragaph
1266 if (!row_ptr->pos())
1269 // in some labels it does not count
1270 if (row_ptr->par()->layout()->margintype != MARGIN_MANUAL
1271 && pos < beginningOfMainBody(buf, row_ptr->par()))
1274 // if there is anything between the first char of the row and
1275 // the sepcified position that is not a newline and not a hfill,
1276 // the hfill will count, otherwise not
1277 pos_type i = row_ptr->pos();
1278 while (i < pos && (row_ptr->par()->isNewline(i)
1279 || row_ptr->par()->isHfill(i)))
1286 LColor::color LyXText::backgroundColor()
1289 return inset_owner->backgroundColor();
1291 return LColor::background;
1294 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1296 // get the maximum ascent and the maximum descent
1299 float layoutasc = 0;
1300 float layoutdesc = 0;
1303 Inset * tmpinset = 0;
1305 // ok , let us initialize the maxasc and maxdesc value.
1306 // This depends in LaTeX of the font of the last character
1307 // in the paragraph. The hack below is necessary because
1308 // of the possibility of open footnotes
1310 // Correction: only the fontsize count. The other properties
1311 // are taken from the layoutfont. Nicer on the screen :)
1312 Paragraph * par = row_ptr->par();
1313 Paragraph * firstpar = row_ptr->par();
1315 LyXLayout_ptr const & layout = firstpar->layout();
1317 // as max get the first character of this row then it can increes but not
1318 // decrees the height. Just some point to start with so we don't have to
1319 // do the assignment below too often.
1320 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1321 LyXFont::FONT_SIZE const tmpsize = font.size();
1322 font = getLayoutFont(bview->buffer(), par);
1323 LyXFont::FONT_SIZE const size = font.size();
1324 font.setSize(tmpsize);
1326 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1328 float spacing_val = 1.0;
1329 if (!row_ptr->par()->params().spacing().isDefault()) {
1330 spacing_val = row_ptr->par()->params().spacing().getValue();
1332 spacing_val = bview->buffer()->params.spacing.getValue();
1334 //lyxerr << "spacing_val = " << spacing_val << endl;
1336 int maxasc = int(font_metrics::maxAscent(font) *
1337 layout->spacing.getValue() *
1339 int maxdesc = int(font_metrics::maxDescent(font) *
1340 layout->spacing.getValue() *
1343 pos_type const pos_end = rowLast(row_ptr);
1347 // Check if any insets are larger
1348 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1349 if (row_ptr->par()->isInset(pos)) {
1350 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1351 tmpinset = row_ptr->par()->getInset(pos);
1353 #if 1 // this is needed for deep update on initialitation
1354 tmpinset->update(bview, tmpfont);
1356 asc = tmpinset->ascent(bview, tmpfont);
1357 desc = tmpinset->descent(bview, tmpfont);
1358 maxwidth += tmpinset->width(bview, tmpfont);
1359 maxasc = max(maxasc, asc);
1360 maxdesc = max(maxdesc, desc);
1363 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1367 // Check if any custom fonts are larger (Asger)
1368 // This is not completely correct, but we can live with the small,
1369 // cosmetic error for now.
1370 LyXFont::FONT_SIZE maxsize =
1371 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1372 if (maxsize > font.size()) {
1373 font.setSize(maxsize);
1375 asc = font_metrics::maxAscent(font);
1376 desc = font_metrics::maxDescent(font);
1383 // This is nicer with box insets:
1387 row_ptr->ascent_of_text(maxasc);
1389 // is it a top line?
1390 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1392 // some parksips VERY EASY IMPLEMENTATION
1393 if (bview->buffer()->params.paragraph_separation ==
1394 BufferParams::PARSEP_SKIP)
1396 if (layout->isParagraph()
1397 && firstpar->getDepth() == 0
1398 && firstpar->previous())
1400 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1401 } else if (firstpar->previous() &&
1402 firstpar->previous()->layout()->isParagraph() &&
1403 firstpar->previous()->getDepth() == 0)
1405 // is it right to use defskip here too? (AS)
1406 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1410 // the paper margins
1411 if (!row_ptr->par()->previous() && bv_owner)
1412 maxasc += LYX_PAPER_MARGIN;
1414 // add the vertical spaces, that the user added
1415 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1417 // do not forget the DTP-lines!
1418 // there height depends on the font of the nearest character
1419 if (firstpar->params().lineTop())
1421 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1423 // and now the pagebreaks
1424 if (firstpar->params().pagebreakTop())
1425 maxasc += 3 * defaultHeight();
1427 // This is special code for the chapter, since the label of this
1428 // layout is printed in an extra row
1429 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1430 && bview->buffer()->params.secnumdepth >= 0)
1432 float spacing_val = 1.0;
1433 if (!row_ptr->par()->params().spacing().isDefault()) {
1434 spacing_val = row_ptr->par()->params().spacing().getValue();
1436 spacing_val = bview->buffer()->params.spacing.getValue();
1439 labeladdon = int(font_metrics::maxDescent(labelfont) *
1440 layout->spacing.getValue() *
1442 + int(font_metrics::maxAscent(labelfont) *
1443 layout->spacing.getValue() *
1447 // special code for the top label
1448 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1449 || layout->labeltype == LABEL_BIBLIO
1450 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1451 && row_ptr->par()->isFirstInSequence()
1452 && !row_ptr->par()->getLabelstring().empty())
1454 float spacing_val = 1.0;
1455 if (!row_ptr->par()->params().spacing().isDefault()) {
1456 spacing_val = row_ptr->par()->params().spacing().getValue();
1458 spacing_val = bview->buffer()->params.spacing.getValue();
1462 (font_metrics::maxAscent(labelfont) *
1463 layout->spacing.getValue() *
1465 +(font_metrics::maxDescent(labelfont) *
1466 layout->spacing.getValue() *
1468 + layout->topsep * defaultHeight()
1469 + layout->labelbottomsep * defaultHeight());
1472 // and now the layout spaces, for example before and after a section,
1473 // or between the items of a itemize or enumerate environment
1475 if (!firstpar->params().pagebreakTop()) {
1476 Paragraph * prev = row_ptr->par()->previous();
1478 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1479 if (prev && prev->layout() == firstpar->layout() &&
1480 prev->getDepth() == firstpar->getDepth() &&
1481 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1483 layoutasc = (layout->itemsep * defaultHeight());
1484 } else if (row_ptr->previous()) {
1485 tmptop = layout->topsep;
1487 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1488 tmptop -= row_ptr->previous()->par()->layout()->bottomsep;
1491 layoutasc = (tmptop * defaultHeight());
1492 } else if (row_ptr->par()->params().lineTop()) {
1493 tmptop = layout->topsep;
1496 layoutasc = (tmptop * defaultHeight());
1499 prev = row_ptr->par()->outerHook();
1501 maxasc += int(prev->layout()->parsep * defaultHeight());
1503 if (firstpar->previous() &&
1504 firstpar->previous()->getDepth() == 0 &&
1505 firstpar->previous()->layout() !=
1509 } else if (firstpar->previous()) {
1510 maxasc += int(layout->parsep * defaultHeight());
1516 // is it a bottom line?
1517 if (row_ptr->par() == par
1518 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1520 // the paper margins
1521 if (!par->next() && bv_owner)
1522 maxdesc += LYX_PAPER_MARGIN;
1524 // add the vertical spaces, that the user added
1525 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1527 // do not forget the DTP-lines!
1528 // there height depends on the font of the nearest character
1529 if (firstpar->params().lineBottom())
1530 maxdesc += 2 * font_metrics::ascent('x',
1531 getFont(bview->buffer(),
1533 max(pos_type(0), par->size() - 1)));
1535 // and now the pagebreaks
1536 if (firstpar->params().pagebreakBottom())
1537 maxdesc += 3 * defaultHeight();
1539 // and now the layout spaces, for example before and after
1540 // a section, or between the items of a itemize or enumerate
1542 if (!firstpar->params().pagebreakBottom()
1543 && row_ptr->par()->next()) {
1544 Paragraph * nextpar = row_ptr->par()->next();
1545 Paragraph * comparepar = row_ptr->par();
1549 if (comparepar->getDepth() > nextpar->getDepth()) {
1550 usual = (comparepar->layout()->bottomsep * defaultHeight());
1551 comparepar = comparepar->depthHook(nextpar->getDepth());
1552 if (comparepar->layout()!= nextpar->layout()
1553 || nextpar->getLabelWidthString() !=
1554 comparepar->getLabelWidthString())
1556 unusual = (comparepar->layout()->bottomsep * defaultHeight());
1558 if (unusual > usual)
1559 layoutdesc = unusual;
1562 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1564 if (comparepar->layout() != nextpar->layout()
1565 || nextpar->getLabelWidthString() !=
1566 comparepar->getLabelWidthString())
1567 layoutdesc = int(comparepar->layout()->bottomsep * defaultHeight());
1572 // incalculate the layout spaces
1573 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1574 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1576 // calculate the new height of the text
1577 height -= row_ptr->height();
1579 row_ptr->height(maxasc + maxdesc + labeladdon);
1580 row_ptr->baseline(maxasc + labeladdon);
1582 height += row_ptr->height();
1584 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1587 row_ptr->width(maxwidth);
1588 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1590 row_ptr->width(int(maxwidth + x));
1593 width = max(0,workWidth(bview));
1595 if (r->width() > width)
1603 // Appends the implicit specified paragraph behind the specified row,
1604 // start at the implicit given position
1605 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1607 bool not_ready = true;
1609 // The last character position of a paragraph is an invariant so we can
1610 // safely get it here. (Asger)
1611 pos_type const lastposition = row->par()->size();
1613 // Get the next breakpoint
1614 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1618 // Insert the new row
1619 if (z < lastposition) {
1621 insertRow(row, row->par(), z);
1628 // Set the dimensions of the row
1629 // fixed fill setting now by calling inset->update() in
1630 // SingleWidth when needed!
1631 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1632 setHeightOfRow(bview, tmprow);
1634 } while (not_ready);
1638 void LyXText::breakAgain(BufferView * bview, Row * row) const
1640 bool not_ready = true;
1643 // get the next breakpoint
1644 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1647 if (z < row->par()->size()) {
1648 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1651 insertRow(row, row->par(), z);
1657 if (row->pos() == z)
1658 not_ready = false; // the rest will not change
1664 // if there are some rows too much, delete them
1665 // only if you broke the whole paragraph!
1666 Row * tmprow2 = row;
1667 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1668 tmprow2 = tmprow2->next();
1670 while (tmprow2 != row) {
1671 tmprow2 = tmprow2->previous();
1672 removeRow(tmprow2->next());
1677 // set the dimensions of the row
1678 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1679 setHeightOfRow(bview, tmprow);
1680 } while (not_ready);
1684 // this is just a little changed version of break again
1685 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1687 // get the next breakpoint
1688 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1691 if (z < row->par()->size()) {
1693 || (row->next() && row->next()->par() != row->par())) {
1696 insertRow(row, row->par(), z);
1702 if (row->pos() != z)
1706 // if there are some rows too much, delete them
1707 // only if you broke the whole paragraph!
1708 Row * tmprow2 = row;
1709 while (tmprow2->next()
1710 && tmprow2->next()->par() == row->par()) {
1711 tmprow2 = tmprow2->next();
1713 while (tmprow2 != row) {
1714 tmprow2 = tmprow2->previous();
1715 removeRow(tmprow2->next());
1719 // set the dimensions of the row
1720 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1721 setHeightOfRow(bview, tmprow);
1725 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1727 LyXTextClass const & tclass =
1728 bview->buffer()->params.getLyXTextClass();
1729 LyXLayout_ptr const & layout = cursor.par()->layout();
1731 // this is only allowed, if the current paragraph is not empty or caption
1732 // and if it has not the keepempty flag aktive
1733 if (cursor.par()->empty()
1734 && layout->labeltype != LABEL_SENSITIVE
1735 && !layout->keepempty)
1738 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1740 // Always break behind a space
1742 // It is better to erase the space (Dekel)
1743 if (cursor.pos() < cursor.par()->size()
1744 && cursor.par()->isLineSeparator(cursor.pos()))
1745 cursor.par()->erase(cursor.pos());
1746 // cursor.pos(cursor.pos() + 1);
1748 // break the paragraph
1752 keep_layout = layout->isEnvironment();
1754 // we need to set this before we insert the paragraph. IMO the
1755 // breakParagraph call should return a bool if it inserts the
1756 // paragraph before or behind and we should react on that one
1757 // but we can fix this in 1.3.0 (Jug 20020509)
1758 bool const isempty = (layout->keepempty && cursor.par()->empty());
1759 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1762 // well this is the caption hack since one caption is really enough
1763 if (layout->labeltype == LABEL_SENSITIVE) {
1765 // set to standard-layout
1766 cursor.par()->applyLayout(tclass.defaultLayout());
1768 // set to standard-layout
1769 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1772 // if the cursor is at the beginning of a row without prior newline,
1774 // This touches only the screen-update. Otherwise we would may have
1775 // an empty row on the screen
1776 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1777 && cursor.row()->pos() == cursor.pos())
1782 status(bview, LyXText::NEED_MORE_REFRESH);
1783 refresh_row = cursor.row();
1784 refresh_y = cursor.y() - cursor.row()->baseline();
1786 // Do not forget the special right address boxes
1787 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1788 while (refresh_row->previous() &&
1789 refresh_row->previous()->par() == refresh_row->par())
1791 refresh_row = refresh_row->previous();
1792 refresh_y -= refresh_row->height();
1795 removeParagraph(cursor.row());
1797 // set the dimensions of the cursor row
1798 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1800 setHeightOfRow(bview, cursor.row());
1802 while (!cursor.par()->next()->empty()
1803 && cursor.par()->next()->isNewline(0))
1804 cursor.par()->next()->erase(0);
1806 insertParagraph(bview, cursor.par()->next(), cursor.row());
1808 updateCounters(bview);
1810 // This check is necessary. Otherwise the new empty paragraph will
1811 // be deleted automatically. And it is more friendly for the user!
1812 if (cursor.pos() || isempty)
1813 setCursor(bview, cursor.par()->next(), 0);
1815 setCursor(bview, cursor.par(), 0);
1817 if (cursor.row()->next())
1818 breakAgain(bview, cursor.row()->next());
1824 // Just a macro to make some thing easier.
1825 void LyXText::redoParagraph(BufferView * bview) const
1828 redoParagraphs(bview, cursor, cursor.par()->next());
1829 setCursorIntern(bview, cursor.par(), cursor.pos());
1833 // insert a character, moves all the following breaks in the
1834 // same Paragraph one to the right and make a rebreak
1835 void LyXText::insertChar(BufferView * bview, char c)
1837 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1839 // When the free-spacing option is set for the current layout,
1840 // disable the double-space checking
1842 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1843 cursor.row()->par()->isFreeSpacing();
1845 if (lyxrc.auto_number) {
1846 static string const number_operators = "+-/*";
1847 static string const number_unary_operators = "+-";
1848 static string const number_seperators = ".,:";
1850 if (current_font.number() == LyXFont::ON) {
1851 if (!IsDigit(c) && !contains(number_operators, c) &&
1852 !(contains(number_seperators, c) &&
1853 cursor.pos() >= 1 &&
1854 cursor.pos() < cursor.par()->size() &&
1855 getFont(bview->buffer(),
1857 cursor.pos()).number() == LyXFont::ON &&
1858 getFont(bview->buffer(),
1860 cursor.pos() - 1).number() == LyXFont::ON)
1862 number(bview); // Set current_font.number to OFF
1863 } else if (IsDigit(c) &&
1864 real_current_font.isVisibleRightToLeft()) {
1865 number(bview); // Set current_font.number to ON
1867 if (cursor.pos() > 0) {
1868 char const c = cursor.par()->getChar(cursor.pos() - 1);
1869 if (contains(number_unary_operators, c) &&
1870 (cursor.pos() == 1 ||
1871 cursor.par()->isSeparator(cursor.pos() - 2) ||
1872 cursor.par()->isNewline(cursor.pos() - 2))
1874 setCharFont(bview->buffer(),
1878 } else if (contains(number_seperators, c) &&
1879 cursor.pos() >= 2 &&
1880 getFont(bview->buffer(),
1882 cursor.pos() - 2).number() == LyXFont::ON) {
1883 setCharFont(bview->buffer(),
1893 // First check, if there will be two blanks together or a blank at
1894 // the beginning of a paragraph.
1895 // I decided to handle blanks like normal characters, the main
1896 // difference are the special checks when calculating the row.fill
1897 // (blank does not count at the end of a row) and the check here
1899 // The bug is triggered when we type in a description environment:
1900 // The current_font is not changed when we go from label to main text
1901 // and it should (along with realtmpfont) when we type the space.
1902 // CHECK There is a bug here! (Asger)
1904 LyXFont realtmpfont = real_current_font;
1905 LyXFont rawtmpfont = current_font;
1906 // store the current font. This is because of the use of cursor
1907 // movements. The moving cursor would refresh the current font
1909 // Get the font that is used to calculate the baselineskip
1910 pos_type const lastpos = cursor.par()->size();
1911 LyXFont rawparfont =
1912 cursor.par()->getFontSettings(bview->buffer()->params,
1915 bool jumped_over_space = false;
1917 if (!freeSpacing && IsLineSeparatorChar(c)) {
1918 if ((cursor.pos() > 0
1919 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1920 || (cursor.pos() > 0
1921 && cursor.par()->isNewline(cursor.pos() - 1))
1922 || (cursor.pos() == 0)) {
1923 static bool sent_space_message = false;
1924 if (!sent_space_message) {
1925 if (cursor.pos() == 0)
1926 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1928 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1929 sent_space_message = true;
1934 } else if (IsNewlineChar(c)) {
1935 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1941 // No newline at first position of a paragraph or behind labels.
1942 // TeX does not allow that
1944 if (cursor.pos() < cursor.par()->size() &&
1945 cursor.par()->isLineSeparator(cursor.pos()))
1946 // newline always after a blank!
1948 cursor.row()->fill(-1); // to force a new break
1951 // the display inset stuff
1952 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1953 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1954 if (inset && (inset->display() || inset->needFullRow())) {
1955 // force a new break
1956 cursor.row()->fill(-1); // to force a new break
1960 // get the cursor row fist
1961 Row * row = cursor.row();
1962 int y = cursor.y() - row->baseline();
1963 if (c != Paragraph::META_INSET) {
1964 // Here case LyXText::InsertInset already insertet the character
1965 cursor.par()->insertChar(cursor.pos(), c);
1967 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1969 if (!jumped_over_space) {
1970 // refresh the positions
1972 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1973 tmprow = tmprow->next();
1974 tmprow->pos(tmprow->pos() + 1);
1978 // Is there a break one row above
1979 if (row->previous() && row->previous()->par() == row->par()
1980 && (cursor.par()->isLineSeparator(cursor.pos())
1981 || cursor.par()->isNewline(cursor.pos())
1982 || ((cursor.pos() < cursor.par()->size()) &&
1983 cursor.par()->isInset(cursor.pos()+1))
1984 || cursor.row()->fill() == -1))
1986 pos_type z = nextBreakPoint(bview,
1989 if (z >= row->pos()) {
1992 // set the dimensions of the row above
1993 row->previous()->fill(fill(bview,
1997 setHeightOfRow(bview, row->previous());
1999 y -= row->previous()->height();
2001 refresh_row = row->previous();
2002 status(bview, LyXText::NEED_MORE_REFRESH);
2004 breakAgainOneRow(bview, row);
2006 current_font = rawtmpfont;
2007 real_current_font = realtmpfont;
2008 setCursor(bview, cursor.par(), cursor.pos() + 1,
2009 false, cursor.boundary());
2010 // cursor MUST be in row now.
2012 if (row->next() && row->next()->par() == row->par())
2013 need_break_row = row->next();
2017 // check, wether the last characters font has changed.
2018 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2019 && rawparfont != rawtmpfont)
2020 redoHeightOfParagraph(bview, cursor);
2027 // recalculate the fill of the row
2028 if (row->fill() >= 0) {
2029 // needed because a newline will set fill to -1. Otherwise
2030 // we would not get a rebreak!
2031 row->fill(fill(bview, row, workWidth(bview)));
2033 if (c == Paragraph::META_INSET || row->fill() < 0) {
2036 refresh_x = cursor.x();
2037 refresh_pos = cursor.pos();
2038 status(bview, LyXText::NEED_MORE_REFRESH);
2039 breakAgainOneRow(bview, row);
2040 // will the cursor be in another row now?
2041 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2042 if (row->next() && row->next()->par() == row->par())
2043 // this should always be true
2045 breakAgainOneRow(bview, row);
2047 current_font = rawtmpfont;
2048 real_current_font = realtmpfont;
2050 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2052 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2053 != cursor.boundary())
2054 setCursor(bview, cursor.par(), cursor.pos(), false,
2055 !cursor.boundary());
2056 if (row->next() && row->next()->par() == row->par())
2057 need_break_row = row->next();
2062 refresh_x = cursor.x();
2064 refresh_pos = cursor.pos();
2066 int const tmpheight = row->height();
2067 setHeightOfRow(bview, row);
2068 if (tmpheight == row->height())
2069 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2071 status(bview, LyXText::NEED_MORE_REFRESH);
2073 current_font = rawtmpfont;
2074 real_current_font = realtmpfont;
2075 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2079 // check, wether the last characters font has changed.
2080 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2081 && rawparfont != rawtmpfont) {
2082 redoHeightOfParagraph(bview, cursor);
2084 // now the special right address boxes
2085 if (cursor.par()->layout()->margintype
2086 == MARGIN_RIGHT_ADDRESS_BOX) {
2087 redoDrawingOfParagraph(bview, cursor);
2095 void LyXText::charInserted()
2097 // Here we could call FinishUndo for every 20 characters inserted.
2098 // This is from my experience how emacs does it.
2099 static unsigned int counter;
2109 void LyXText::prepareToPrint(BufferView * bview,
2110 Row * row, float & x,
2111 float & fill_separator,
2113 float & fill_label_hfill,
2119 float w = row->fill();
2121 fill_label_hfill = 0;
2123 fill_label_hfill = 0;
2126 row->par()->isRightToLeftPar(bview->buffer()->params);
2128 x = (workWidth(bview) > 0)
2129 ? rightMargin(bview->buffer(), row) : 0;
2131 x = (workWidth(bview) > 0)
2132 ? leftMargin(bview, row) : 0;
2134 // is there a manual margin with a manual label
2135 LyXLayout_ptr const & layout = row->par()->layout();
2137 if (layout->margintype == MARGIN_MANUAL
2138 && layout->labeltype == LABEL_MANUAL) {
2139 // one more since labels are left aligned
2140 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2141 if (nlh && !row->par()->getLabelWidthString().empty()) {
2142 fill_label_hfill = labelFill(bview, row) / nlh;
2146 // are there any hfills in the row?
2147 float const nh = numberOfHfills(bview->buffer(), row);
2151 fill_hfill = w / nh;
2152 // we don't have to look at the alignment if it is ALIGN_LEFT and
2153 // if the row is already larger then the permitted width as then
2154 // we force the LEFT_ALIGN'edness!
2155 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2156 // is it block, flushleft or flushright?
2157 // set x how you need it
2159 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2160 align = layout->align;
2162 align = row->par()->params().align();
2165 // center displayed insets
2167 if (row->par()->isInset(row->pos())
2168 && (inset=row->par()->getInset(row->pos()))
2169 && (inset->display())) // || (inset->scroll() < 0)))
2170 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2171 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2172 // ERT insets should always be LEFT ALIGNED on screen
2173 inset = row->par()->inInset();
2174 if (inset && inset->owner() &&
2175 inset->owner()->lyxCode() == Inset::ERT_CODE)
2177 align = LYX_ALIGN_LEFT;
2181 case LYX_ALIGN_BLOCK:
2182 ns = numberOfSeparators(bview->buffer(), row);
2183 if (ns && row->next() && row->next()->par() == row->par() &&
2184 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2185 && !(row->next()->par()->isInset(row->next()->pos())
2186 && row->next()->par()->getInset(row->next()->pos())
2187 && row->next()->par()->getInset(row->next()->pos())->display())
2190 fill_separator = w / ns;
2191 } else if (is_rtl) {
2195 case LYX_ALIGN_RIGHT:
2198 case LYX_ALIGN_CENTER:
2206 computeBidiTables(bview->buffer(), row);
2208 pos_type main_body =
2209 beginningOfMainBody(bview->buffer(), row->par());
2210 pos_type last = rowLast(row);
2212 if (main_body > 0 &&
2213 (main_body - 1 > last ||
2214 !row->par()->isLineSeparator(main_body - 1))) {
2215 x += font_metrics::width(layout->labelsep,
2216 getLabelFont(bview->buffer(), row->par()));
2217 if (main_body - 1 <= last)
2218 x += fill_label_hfill;
2224 // important for the screen
2227 // the cursor set functions have a special mechanism. When they
2228 // realize, that you left an empty paragraph, they will delete it.
2229 // They also delete the corresponding row
2231 void LyXText::cursorRightOneWord(BufferView * bview) const
2233 // treat floats, HFills and Insets as words
2234 LyXCursor tmpcursor = cursor;
2235 // CHECK See comment on top of text.C
2237 if (tmpcursor.pos() == tmpcursor.par()->size()
2238 && tmpcursor.par()->next()) {
2239 tmpcursor.par(tmpcursor.par()->next());
2244 // Skip through initial nonword stuff.
2245 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2246 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2247 // printf("Current pos1 %d", tmpcursor.pos()) ;
2248 tmpcursor.pos(tmpcursor.pos() + 1);
2251 // Advance through word.
2252 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2253 tmpcursor.par()->isWord(tmpcursor.pos())) {
2254 // printf("Current pos2 %d", tmpcursor.pos()) ;
2255 tmpcursor.pos(tmpcursor.pos() + 1);
2259 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2263 void LyXText::cursorTab(BufferView * bview) const
2265 LyXCursor tmpcursor = cursor;
2266 while (tmpcursor.pos() < tmpcursor.par()->size()
2267 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2268 tmpcursor.pos(tmpcursor.pos() + 1);
2270 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2271 if (tmpcursor.par()->next()) {
2272 tmpcursor.par(tmpcursor.par()->next());
2276 tmpcursor.pos(tmpcursor.pos() + 1);
2277 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2281 // Skip initial whitespace at end of word and move cursor to *start*
2282 // of prior word, not to end of next prior word.
2283 void LyXText::cursorLeftOneWord(BufferView * bview) const
2285 LyXCursor tmpcursor = cursor;
2286 cursorLeftOneWord(tmpcursor);
2287 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2291 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2293 // treat HFills, floats and Insets as words
2296 && (cur.par()->isSeparator(cur.pos() - 1)
2297 || cur.par()->isKomma(cur.pos() - 1))
2298 && !(cur.par()->isHfill(cur.pos() - 1)
2299 || cur.par()->isInset(cur.pos() - 1)))
2300 cur.pos(cur.pos() - 1);
2303 && (cur.par()->isInset(cur.pos() - 1)
2304 || cur.par()->isHfill(cur.pos() - 1))) {
2305 cur.pos(cur.pos() - 1);
2306 } else if (!cur.pos()) {
2307 if (cur.par()->previous()) {
2308 cur.par(cur.par()->previous());
2309 cur.pos(cur.par()->size());
2311 } else { // Here, cur != 0
2312 while (cur.pos() > 0 &&
2313 cur.par()->isWord(cur.pos()-1))
2314 cur.pos(cur.pos() - 1);
2319 // Select current word. This depends on behaviour of
2320 // CursorLeftOneWord(), so it is patched as well.
2321 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2322 word_location const loc) const
2324 // first put the cursor where we wana start to select the word
2327 case WHOLE_WORD_STRICT:
2328 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2329 || cursor.par()->isSeparator(cursor.pos())
2330 || cursor.par()->isKomma(cursor.pos())
2331 || cursor.par()->isSeparator(cursor.pos() -1)
2332 || cursor.par()->isKomma(cursor.pos() -1)) {
2336 // no break here, we go to the next
2339 // Move cursor to the beginning, when not already there.
2340 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2341 && !from.par()->isKomma(from.pos() - 1))
2342 cursorLeftOneWord(from);
2345 // always move the cursor to the beginning of previous word
2346 cursorLeftOneWord(from);
2349 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2355 while (to.pos() < to.par()->size()
2356 && !to.par()->isSeparator(to.pos())
2357 && !to.par()->isKomma(to.pos())
2358 && !to.par()->isHfill(to.pos())
2359 && !to.par()->isInset(to.pos()))
2361 to.pos(to.pos() + 1);
2366 void LyXText::selectWord(BufferView * bview, word_location const loc)
2370 getWord(from, to, loc);
2372 setCursor(bview, from.par(), from.pos());
2375 selection.cursor = cursor;
2376 setCursor(bview, to.par(), to.pos());
2377 setSelection(bview);
2381 // Select the word currently under the cursor when no
2382 // selection is currently set
2383 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2384 word_location const loc)
2386 if (!selection.set()) {
2387 selectWord(bview, loc);
2388 return selection.set();
2394 // This function is only used by the spellchecker for NextWord().
2395 // It doesn't handle LYX_ACCENTs and probably never will.
2396 WordLangTuple LyXText::selectNextWordToSpellcheck(BufferView * bview,
2397 float & value) const
2399 if (the_locking_inset) {
2400 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2401 if (!word.word().empty()) {
2402 value += float(cursor.y())/float(height);
2405 // we have to go on checking so move cursor to the next char
2406 if (cursor.pos() == cursor.par()->size()) {
2407 if (!cursor.par()->next())
2409 cursor.par(cursor.par()->next());
2412 cursor.pos(cursor.pos() + 1);
2414 Paragraph * tmppar = cursor.par();
2416 // If this is not the very first word, skip rest of
2417 // current word because we are probably in the middle
2418 // of a word if there is text here.
2419 if (cursor.pos() || cursor.par()->previous()) {
2420 while (cursor.pos() < cursor.par()->size()
2421 && cursor.par()->isLetter(cursor.pos()))
2422 cursor.pos(cursor.pos() + 1);
2425 // Now, skip until we have real text (will jump paragraphs)
2426 while ((cursor.par()->size() > cursor.pos()
2427 && (!cursor.par()->isLetter(cursor.pos()))
2428 && (!cursor.par()->isInset(cursor.pos()) ||
2429 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2430 || (cursor.par()->size() == cursor.pos()
2431 && cursor.par()->next()))
2433 if (cursor.pos() == cursor.par()->size()) {
2434 cursor.par(cursor.par()->next());
2437 cursor.pos(cursor.pos() + 1);
2440 // now check if we hit an inset so it has to be a inset containing text!
2441 if (cursor.pos() < cursor.par()->size() &&
2442 cursor.par()->isInset(cursor.pos()))
2445 cursor.par()->getInset(cursor.pos())->edit(bview);
2446 // now call us again to do the above trick
2447 // but obviously we have to start from down below ;)
2448 return bview->text->selectNextWordToSpellcheck(bview, value);
2451 // Update the value if we changed paragraphs
2452 if (cursor.par() != tmppar) {
2453 setCursor(bview, cursor.par(), cursor.pos());
2454 value = float(cursor.y())/float(height);
2457 // Start the selection from here
2458 selection.cursor = cursor;
2461 getFont(bview->buffer(), cursor.par(), cursor.pos())
2462 .language()->code());
2463 // and find the end of the word (insets like optional hyphens
2464 // and ligature break are part of a word)
2465 while (cursor.pos() < cursor.par()->size()
2466 && (cursor.par()->isLetter(cursor.pos())))
2467 cursor.pos(cursor.pos() + 1);
2469 // Finally, we copy the word to a string and return it
2471 if (selection.cursor.pos() < cursor.pos()) {
2473 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2474 if (!cursor.par()->isInset(i))
2475 str += cursor.par()->getChar(i);
2478 return WordLangTuple(str, lang_code);
2482 // This one is also only for the spellchecker
2483 void LyXText::selectSelectedWord(BufferView * bview)
2485 if (the_locking_inset) {
2486 the_locking_inset->selectSelectedWord(bview);
2489 // move cursor to the beginning
2490 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2492 // set the sel cursor
2493 selection.cursor = cursor;
2495 // now find the end of the word
2496 while (cursor.pos() < cursor.par()->size()
2497 && (cursor.par()->isLetter(cursor.pos())))
2498 cursor.pos(cursor.pos() + 1);
2500 setCursor(bview, cursor.par(), cursor.pos());
2502 // finally set the selection
2503 setSelection(bview);
2507 // Delete from cursor up to the end of the current or next word.
2508 void LyXText::deleteWordForward(BufferView * bview)
2510 if (cursor.par()->empty())
2513 LyXCursor tmpcursor = cursor;
2514 tmpcursor.row(0); //Â ??
2515 selection.set(true); // to avoid deletion
2516 cursorRightOneWord(bview);
2517 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2518 selection.cursor = cursor;
2520 setSelection(bview);
2522 // Great, CutSelection() gets rid of multiple spaces.
2523 cutSelection(bview, true, false);
2528 // Delete from cursor to start of current or prior word.
2529 void LyXText::deleteWordBackward(BufferView * bview)
2531 if (cursor.par()->empty())
2534 LyXCursor tmpcursor = cursor;
2535 tmpcursor.row(0); // ??
2536 selection.set(true); // to avoid deletion
2537 cursorLeftOneWord(bview);
2538 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2539 selection.cursor = cursor;
2541 setSelection(bview);
2542 cutSelection(bview, true, false);
2547 // Kill to end of line.
2548 void LyXText::deleteLineForward(BufferView * bview)
2550 if (cursor.par()->empty())
2551 // Paragraph is empty, so we just go to the right
2554 LyXCursor tmpcursor = cursor;
2555 // We can't store the row over a regular setCursor
2556 // so we set it to 0 and reset it afterwards.
2557 tmpcursor.row(0); //Â ??
2558 selection.set(true); // to avoid deletion
2560 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2561 selection.cursor = cursor;
2563 setSelection(bview);
2564 // What is this test for ??? (JMarc)
2565 if (!selection.set()) {
2566 deleteWordForward(bview);
2568 cutSelection(bview, true, false);
2574 // Change the case of a word at cursor position.
2575 // This function directly manipulates Paragraph::text because there
2576 // is no Paragraph::SetChar currently. I did what I could to ensure
2577 // that it is correct. I guess part of it should be moved to
2578 // Paragraph, but it will have to change for 1.1 anyway. At least
2579 // it does not access outside of the allocated array as the older
2580 // version did. (JMarc)
2581 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2586 if (selection.set()) {
2587 from = selection.start;
2590 getWord(from, to, PARTIAL_WORD);
2591 setCursor(bview, to.par(), to.pos() + 1);
2594 changeRegionCase(bview, from, to, action);
2598 void LyXText::changeRegionCase(BufferView * bview,
2599 LyXCursor const & from,
2600 LyXCursor const & to,
2601 LyXText::TextCase action)
2603 lyx::Assert(from <= to);
2605 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2607 pos_type pos = from.pos();
2608 Paragraph * par = from.par();
2610 while (par && (pos != to.pos() || par != to.par())) {
2611 if (pos == par->size()) {
2616 unsigned char c = par->getChar(pos);
2617 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2619 case text_lowercase:
2622 case text_capitalization:
2624 action = text_lowercase;
2626 case text_uppercase:
2631 par->setChar(pos, c);
2632 checkParagraph(bview, par, pos);
2636 if (to.row() != from.row()) {
2637 refresh_y = from.y() - from.row()->baseline();
2638 refresh_row = from.row();
2639 status(bview, LyXText::NEED_MORE_REFRESH);
2644 void LyXText::transposeChars(BufferView & bview)
2646 Paragraph * tmppar = cursor.par();
2648 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2650 pos_type tmppos = cursor.pos();
2652 // First decide if it is possible to transpose at all
2654 // We are at the beginning of a paragraph.
2655 if (tmppos == 0) return;
2657 // We are at the end of a paragraph.
2658 if (tmppos == tmppar->size() - 1) return;
2660 unsigned char c1 = tmppar->getChar(tmppos);
2661 unsigned char c2 = tmppar->getChar(tmppos - 1);
2663 if (c1 != Paragraph::META_INSET
2664 && c2 != Paragraph::META_INSET) {
2665 tmppar->setChar(tmppos, c2);
2666 tmppar->setChar(tmppos - 1, c1);
2668 // We should have an implementation that handles insets
2669 // as well, but that will have to come later. (Lgb)
2670 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2674 void LyXText::Delete(BufferView * bview)
2676 // this is a very easy implementation
2678 LyXCursor old_cursor = cursor;
2679 int const old_cur_par_id = old_cursor.par()->id();
2680 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2681 old_cursor.par()->previous()->id() : 0;
2683 // just move to the right
2686 // CHECK Look at the comment here.
2687 // This check is not very good...
2688 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2689 // and that can very well delete the par or par->previous in
2690 // old_cursor. Will a solution where we compare paragraph id's
2692 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2693 == old_cur_par_prev_id
2694 && cursor.par()->id() != old_cur_par_id) {
2695 // delete-empty-paragraph-mechanism has done it
2699 // if you had success make a backspace
2700 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2701 LyXCursor tmpcursor = cursor;
2702 // to make sure undo gets the right cursor position
2703 cursor = old_cursor;
2704 setUndo(bview, Undo::DELETE,
2705 cursor.par(), cursor.par()->next());
2712 void LyXText::backspace(BufferView * bview)
2714 // Get the font that is used to calculate the baselineskip
2715 pos_type lastpos = cursor.par()->size();
2716 LyXFont rawparfont =
2717 cursor.par()->getFontSettings(bview->buffer()->params,
2720 if (cursor.pos() == 0) {
2721 // The cursor is at the beginning of a paragraph,
2722 // so the the backspace will collapse two paragraphs into one.
2724 // we may paste some paragraphs
2726 // is it an empty paragraph?
2729 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2730 // This is an empty paragraph and we delete it just by moving the cursor one step
2731 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2732 // of the paragraph.
2734 if (cursor.par()->previous()) {
2735 Paragraph * tmppar = cursor.par()->previous();
2736 if (cursor.par()->layout() == tmppar->layout()
2737 && cursor.par()->getAlign() == tmppar->getAlign()) {
2738 // Inherit bottom DTD from the paragraph below.
2739 // (the one we are deleting)
2740 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2741 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2742 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2747 // the layout things can change the height of a row !
2748 int const tmpheight = cursor.row()->height();
2749 setHeightOfRow(bview, cursor.row());
2750 if (cursor.row()->height() != tmpheight) {
2751 refresh_y = cursor.y() - cursor.row()->baseline();
2752 refresh_row = cursor.row();
2753 status(bview, LyXText::NEED_MORE_REFRESH);
2759 if (cursor.par()->previous()) {
2760 setUndo(bview, Undo::DELETE,
2761 cursor.par()->previous(), cursor.par()->next());
2764 Paragraph * tmppar = cursor.par();
2765 Row * tmprow = cursor.row();
2767 // We used to do cursorLeftIntern() here, but it is
2768 // not a good idea since it triggers the auto-delete
2769 // mechanism. So we do a cursorLeftIntern()-lite,
2770 // without the dreaded mechanism. (JMarc)
2771 if (cursor.par()->previous()) {
2772 // steps into the above paragraph.
2773 setCursorIntern(bview, cursor.par()->previous(),
2774 cursor.par()->previous()->size(),
2778 // Pasting is not allowed, if the paragraphs have different
2779 // layout. I think it is a real bug of all other
2780 // word processors to allow it. It confuses the user.
2781 // Even so with a footnote paragraph and a non-footnote
2782 // paragraph. I will not allow pasting in this case,
2783 // because the user would be confused if the footnote behaves
2784 // different wether it is open or closed.
2786 // Correction: Pasting is always allowed with standard-layout
2787 LyXTextClass const & tclass =
2788 bview->buffer()->params.getLyXTextClass();
2790 if (cursor.par() != tmppar
2791 && (cursor.par()->layout() == tmppar->layout()
2792 || tmppar->layout() == tclass.defaultLayout())
2793 && cursor.par()->getAlign() == tmppar->getAlign()) {
2794 removeParagraph(tmprow);
2796 cursor.par()->pasteParagraph(bview->buffer()->params);
2798 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2799 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2800 // strangely enough it seems that commenting out the line above removes
2801 // most or all of the segfaults. I will however also try to move the
2802 // two Remove... lines in front of the PasteParagraph too.
2805 cursor.pos(cursor.pos() - 1);
2807 status(bview, LyXText::NEED_MORE_REFRESH);
2808 refresh_row = cursor.row();
2809 refresh_y = cursor.y() - cursor.row()->baseline();
2811 // remove the lost paragraph
2812 // This one is not safe, since the paragraph that the tmprow and the
2813 // following rows belong to has been deleted by the PasteParagraph
2814 // above. The question is... could this be moved in front of the
2816 //RemoveParagraph(tmprow);
2817 //RemoveRow(tmprow);
2819 // This rebuilds the rows.
2820 appendParagraph(bview, cursor.row());
2821 updateCounters(bview);
2823 // the row may have changed, block, hfills etc.
2824 setCursor(bview, cursor.par(), cursor.pos(), false);
2827 // this is the code for a normal backspace, not pasting
2829 setUndo(bview, Undo::DELETE,
2830 cursor.par(), cursor.par()->next());
2831 // We used to do cursorLeftIntern() here, but it is
2832 // not a good idea since it triggers the auto-delete
2833 // mechanism. So we do a cursorLeftIntern()-lite,
2834 // without the dreaded mechanism. (JMarc)
2835 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2836 false, cursor.boundary());
2838 // some insets are undeletable here
2839 if (cursor.par()->isInset(cursor.pos())) {
2840 if (!cursor.par()->getInset(cursor.pos())->deletable())
2842 // force complete redo when erasing display insets
2843 // this is a cruel method but safe..... Matthias
2844 if (cursor.par()->getInset(cursor.pos())->display() ||
2845 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2846 cursor.par()->erase(cursor.pos());
2847 redoParagraph(bview);
2852 Row * row = cursor.row();
2853 int y = cursor.y() - row->baseline();
2855 // remember that a space at the end of a row doesnt count
2856 // when calculating the fill
2857 if (cursor.pos() < rowLast(row) ||
2858 !cursor.par()->isLineSeparator(cursor.pos())) {
2859 row->fill(row->fill() + singleWidth(bview,
2864 // some special code when deleting a newline. This is similar
2865 // to the behavior when pasting paragraphs
2866 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2867 cursor.par()->erase(cursor.pos());
2868 // refresh the positions
2870 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2871 tmprow = tmprow->next();
2872 tmprow->pos(tmprow->pos() - 1);
2874 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2875 cursor.pos(cursor.pos() - 1);
2877 if (cursor.pos() < cursor.par()->size()
2878 && !cursor.par()->isSeparator(cursor.pos())) {
2879 cursor.par()->insertChar(cursor.pos(), ' ');
2880 setCharFont(bview->buffer(), cursor.par(),
2881 cursor.pos(), current_font);
2882 // refresh the positions
2884 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2885 tmprow = tmprow->next();
2886 tmprow->pos(tmprow->pos() + 1);
2890 cursor.par()->erase(cursor.pos());
2892 // refresh the positions
2894 while (tmprow->next()
2895 && tmprow->next()->par() == row->par()) {
2896 tmprow = tmprow->next();
2897 tmprow->pos(tmprow->pos() - 1);
2900 // delete newlines at the beginning of paragraphs
2901 while (!cursor.par()->empty() &&
2902 cursor.par()->isNewline(cursor.pos()) &&
2903 cursor.pos() == beginningOfMainBody(bview->buffer(),
2905 cursor.par()->erase(cursor.pos());
2906 // refresh the positions
2908 while (tmprow->next() &&
2909 tmprow->next()->par() == row->par()) {
2910 tmprow = tmprow->next();
2911 tmprow->pos(tmprow->pos() - 1);
2916 // is there a break one row above
2917 if (row->previous() && row->previous()->par() == row->par()) {
2918 z = nextBreakPoint(bview, row->previous(),
2920 if (z >= row->pos()) {
2923 Row * tmprow = row->previous();
2925 // maybe the current row is now empty
2926 if (row->pos() >= row->par()->size()) {
2931 breakAgainOneRow(bview, row);
2932 if (row->next() && row->next()->par() == row->par())
2933 need_break_row = row->next();
2938 // set the dimensions of the row above
2939 y -= tmprow->height();
2940 tmprow->fill(fill(bview, tmprow,
2942 setHeightOfRow(bview, tmprow);
2945 refresh_row = tmprow;
2946 status(bview, LyXText::NEED_MORE_REFRESH);
2947 setCursor(bview, cursor.par(), cursor.pos(),
2948 false, cursor.boundary());
2949 //current_font = rawtmpfont;
2950 //real_current_font = realtmpfont;
2951 // check, whether the last character's font has changed.
2953 cursor.par()->getFontSettings(bview->buffer()->params,
2954 cursor.par()->size() - 1))
2955 redoHeightOfParagraph(bview, cursor);
2960 // break the cursor row again
2961 if (row->next() && row->next()->par() == row->par() &&
2962 (rowLast(row) == row->par()->size() - 1 ||
2963 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2965 // it can happen that a paragraph loses one row
2966 // without a real breakup. This is when a word
2967 // is to long to be broken. Well, I don t care this
2969 if (rowLast(row) == row->par()->size() - 1)
2970 removeRow(row->next());
2974 status(bview, LyXText::NEED_MORE_REFRESH);
2976 breakAgainOneRow(bview, row);
2977 // will the cursor be in another row now?
2978 if (row->next() && row->next()->par() == row->par() &&
2979 rowLast(row) <= cursor.pos()) {
2981 breakAgainOneRow(bview, row);
2984 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2986 if (row->next() && row->next()->par() == row->par())
2987 need_break_row = row->next();
2991 // set the dimensions of the row
2992 row->fill(fill(bview, row, workWidth(bview)));
2993 int const tmpheight = row->height();
2994 setHeightOfRow(bview, row);
2995 if (tmpheight == row->height())
2996 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2998 status(bview, LyXText::NEED_MORE_REFRESH);
3001 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3005 // current_font = rawtmpfont;
3006 // real_current_font = realtmpfont;
3008 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
3009 != cursor.boundary())
3010 setCursor(bview, cursor.par(), cursor.pos(), false,
3011 !cursor.boundary());
3013 lastpos = cursor.par()->size();
3014 if (cursor.pos() == lastpos)
3015 setCurrentFont(bview);
3017 // check, whether the last characters font has changed.
3019 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3020 redoHeightOfParagraph(bview, cursor);
3022 // now the special right address boxes
3023 if (cursor.par()->layout()->margintype
3024 == MARGIN_RIGHT_ADDRESS_BOX) {
3025 redoDrawingOfParagraph(bview, cursor);
3031 bool LyXText::paintRowBackground(DrawRowParams & p)
3033 bool clear_area = true;
3035 LyXFont font(LyXFont::ALL_SANE);
3037 pos_type const last = rowLastPrintable(p.row);
3039 if (!p.bv->screen().forceClear() && last == p.row->pos()
3040 && p.row->par()->isInset(p.row->pos())) {
3041 inset = p.row->par()->getInset(p.row->pos());
3043 clear_area = inset->doClearArea();
3053 int const y = p.yo < 0 ? 0 : p.yo;
3054 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3055 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3062 int h = p.row->baseline() - inset->ascent(p.bv, font);
3064 // first clear the whole row above the inset!
3066 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3069 // clear the space below the inset!
3070 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3071 if ((p.row->height() - h) > 0) {
3072 p.pain->fillRectangle(p.xo, p.yo + h,
3073 p.width, p.row->height() - h, backgroundColor());
3076 // clear the space behind the inset, if needed
3077 if (!inset->display() && !inset->needFullRow()) {
3078 int const xp = int(p.x) + inset->width(p.bv, font);
3079 if (p.width - xp > 0) {
3080 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3081 p.row->height(), backgroundColor());
3089 void LyXText::paintRowSelection(DrawRowParams & p)
3091 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3093 // the current selection
3094 int const startx = selection.start.x();
3095 int const endx = selection.end.x();
3096 int const starty = selection.start.y();
3097 int const endy = selection.end.y();
3098 Row const * startrow = selection.start.row();
3099 Row const * endrow = selection.end.row();
3103 if (bidi_same_direction) {
3107 int h = row->height();
3109 if (startrow == row && endrow == row) {
3110 if (startx < endx) {
3113 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3117 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3119 } else if (startrow == row) {
3120 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3121 int const w = (is_rtl) ? startx : (p.width - startx);
3122 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3123 } else if (endrow == row) {
3124 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3125 int const w = (is_rtl) ? (p.width - endx) : endx;
3126 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3127 } else if (p.y > starty && p.y < endy) {
3128 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3131 } else if (startrow != row && endrow != row) {
3133 int h = row->height();
3134 if (p.y > starty && p.y < endy) {
3135 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3140 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3146 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3148 Buffer const * buffer = p.bv->buffer();
3149 Paragraph * par = row->par();
3150 pos_type main_body = beginningOfMainBody(buffer, par);
3151 pos_type const last = rowLastPrintable(row);
3153 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3154 pos_type pos = vis2log(vpos);
3155 float const old_tmpx = tmpx;
3156 if (main_body > 0 && pos == main_body - 1) {
3157 LyXLayout_ptr const & layout = par->layout();
3158 LyXFont const lfont = getLabelFont(buffer, par);
3160 tmpx += p.label_hfill + font_metrics::width(layout->labelsep, lfont);
3162 if (par->isLineSeparator(main_body - 1))
3163 tmpx -= singleWidth(p.bv, par, main_body - 1);
3166 if (hfillExpansion(buffer, row, pos)) {
3167 tmpx += singleWidth(p.bv, par, pos);
3168 if (pos >= main_body)
3171 tmpx += p.label_hfill;
3174 else if (par->isSeparator(pos)) {
3175 tmpx += singleWidth(p.bv, par, pos);
3176 if (pos >= main_body)
3177 tmpx += p.separator;
3179 tmpx += singleWidth(p.bv, par, pos);
3182 if ((startrow != row || selection.start.pos() <= pos) &&
3183 (endrow != row || pos < selection.end.pos())) {
3184 // Here we do not use p.x as p.xo was added to p.x.
3185 p.pain->fillRectangle(int(old_tmpx), p.yo,
3186 int(tmpx - old_tmpx + 1),
3187 row->height(), LColor::selection);
3190 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3191 p.pain->fillRectangle(p.xo + int(tmpx),
3192 p.yo, int(p.bv->workWidth() - tmpx),
3193 row->height(), LColor::selection);
3199 void LyXText::paintRowAppendix(DrawRowParams & p)
3201 // FIXME: can be just p.width ?
3202 int const ww = p.bv->workWidth();
3203 Paragraph * firstpar = p.row->par();
3205 if (firstpar->params().appendix()) {
3206 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3207 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3212 void LyXText::paintRowDepthBar(DrawRowParams & p)
3214 Paragraph::depth_type const depth = p.row->par()->getDepth();
3219 Paragraph::depth_type prev_depth = 0;
3220 if (p.row->previous())
3221 prev_depth = p.row->previous()->par()->getDepth();
3222 Paragraph::depth_type next_depth = 0;
3224 next_depth = p.row->next()->par()->getDepth();
3226 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3227 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3228 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3230 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3232 int const w = LYX_PAPER_MARGIN / 5;
3234 if (i > prev_depth) {
3235 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3237 if (i > next_depth) {
3238 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3244 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3246 int const arrow_size = 4;
3247 int const space_size = int(vsp.inPixels(bv));
3249 if (vsp.kind() != VSpace::LENGTH) {
3255 int const min_size = max(3 * arrow_size,
3256 font_metrics::maxAscent(font)
3257 + font_metrics::maxDescent(font));
3259 if (vsp.length().len().value() < 0.0)
3262 return max(min_size, space_size);
3266 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3267 VSpace const & vsp, int start)
3269 int const arrow_size = 4;
3270 int const size = getLengthMarkerHeight(p.bv, vsp);
3271 int const end = start + size;
3273 // the label to display (if any)
3275 // y-values for top arrow
3277 // y-values for bottom arrow
3279 switch (vsp.kind()) {
3280 case VSpace::LENGTH:
3282 str = prefix + " (" + vsp.asLyXCommand() + ")";
3283 // adding or removing space
3284 bool const added = !(vsp.length().len().value() < 0.0);
3285 ty1 = added ? (start + arrow_size) : start;
3286 ty2 = added ? start : (start + arrow_size);
3287 by1 = added ? (end - arrow_size) : end;
3288 by2 = added ? end : (end - arrow_size);
3291 case VSpace:: VFILL:
3292 str = prefix + " (vertical fill)";
3297 // nothing to draw here
3301 int const leftx = p.xo + leftMargin(p.bv, p.row);
3302 int const midx = leftx + arrow_size;
3303 int const rightx = midx + arrow_size;
3311 font.setColor(LColor::added_space).decSize();
3312 font_metrics::rectText(str, font, w, a, d);
3314 p.pain->rectText(leftx + 2 * arrow_size + 5,
3315 start + ((end - start) / 2) + d,
3319 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3320 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3323 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3324 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3327 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3333 int LyXText::paintPageBreak(string const & label, int y, DrawRowParams & p)
3336 pb_font.setColor(LColor::pagebreak).decSize();
3341 font_metrics::rectText(label, pb_font, w, a, d);
3343 int const text_start = p.xo + ((p.width - w) / 2);
3344 int const text_end = text_start + w;
3346 p.pain->rectText(text_start, y + d, label, pb_font);
3348 p.pain->line(p.xo, y, text_start, y,
3349 LColor::pagebreak, Painter::line_onoffdash);
3350 p.pain->line(text_end, y, p.xo + p.width, y,
3351 LColor::pagebreak, Painter::line_onoffdash);
3353 return 3 * defaultHeight();
3357 void LyXText::paintFirstRow(DrawRowParams & p)
3359 Paragraph * par = p.row->par();
3360 ParagraphParameters const & parparams = par->params();
3362 // start of appendix?
3363 if (parparams.startOfAppendix()) {
3364 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3369 // think about the margins
3370 if (!p.row->previous() && bv_owner)
3371 y_top += LYX_PAPER_MARGIN;
3373 // draw a top pagebreak
3374 if (parparams.pagebreakTop()) {
3375 y_top += paintPageBreak(_("Page Break (top)"),
3376 p.yo + y_top + 2 * defaultHeight(), p);
3379 // draw the additional space if needed:
3380 y_top += drawLengthMarker(p, _("Space above"),
3381 parparams.spaceTop(), p.yo + y_top);
3383 Buffer const * buffer = p.bv->buffer();
3385 LyXLayout_ptr const & layout = par->layout();
3387 // think about the parskip
3388 // some parskips VERY EASY IMPLEMENTATION
3389 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3390 if (par->previous()) {
3391 if (layout->latextype == LATEX_PARAGRAPH
3392 && !par->getDepth()) {
3393 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3395 LyXLayout_ptr const & playout =
3396 par->previous()->layout();
3397 if (playout->latextype == LATEX_PARAGRAPH
3398 && !par->previous()->getDepth()) {
3399 // is it right to use defskip here, too? (AS)
3400 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3406 int const ww = p.bv->workWidth();
3409 if (parparams.lineTop()) {
3410 LyXFont font(LyXFont::ALL_SANE);
3411 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3415 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3416 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3417 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3418 LColor::topline, Painter::line_solid,
3419 Painter::line_thick);
3424 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3426 // should we print a label?
3427 if (layout->labeltype >= LABEL_STATIC
3428 && (layout->labeltype != LABEL_STATIC
3429 || layout->latextype != LATEX_ENVIRONMENT
3430 || par->isFirstInSequence())) {
3432 LyXFont font = getLabelFont(buffer, par);
3433 if (!par->getLabelstring().empty()) {
3435 string const str = par->getLabelstring();
3437 // this is special code for the chapter layout. This is
3438 // printed in an extra row and has a pagebreak at
3440 if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3441 if (buffer->params.secnumdepth >= 0) {
3442 float spacing_val = 1.0;
3443 if (!parparams.spacing().isDefault()) {
3444 spacing_val = parparams.spacing().getValue();
3446 spacing_val = buffer->params.spacing.getValue();
3450 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
3451 + int(layout->parsep) * defaultHeight();
3454 x = ww - leftMargin(p.bv, p.row) -
3455 font_metrics::width(str, font);
3458 p.pain->text(int(x),
3459 p.yo + p.row->baseline() -
3460 p.row->ascent_of_text() - maxdesc,
3465 x = ww - leftMargin(p.bv, p.row)
3466 + font_metrics::width(layout->labelsep, font);
3468 x = p.x - font_metrics::width(layout->labelsep, font)
3469 - font_metrics::width(str, font);
3472 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3475 // the labels at the top of an environment.
3476 // More or less for bibliography
3477 } else if (par->isFirstInSequence() &&
3478 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3479 layout->labeltype == LABEL_BIBLIO ||
3480 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3481 LyXFont font = getLabelFont(buffer, par);
3482 if (!par->getLabelstring().empty()) {
3483 string const str = par->getLabelstring();
3484 float spacing_val = 1.0;
3485 if (!parparams.spacing().isDefault()) {
3486 spacing_val = parparams.spacing().getValue();
3488 spacing_val = buffer->params.spacing.getValue();
3492 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
3493 + (layout->labelbottomsep * defaultHeight()));
3496 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3497 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3498 + ww - rightMargin(buffer, p.row)) / 2;
3499 x -= font_metrics::width(str, font) / 2;
3500 } else if (is_rtl) {
3501 x = ww - leftMargin(p.bv, p.row) -
3502 font_metrics::width(str, font);
3504 p.pain->text(int(x), p.yo + p.row->baseline()
3505 - p.row->ascent_of_text() - maxdesc,
3510 if (layout->labeltype == LABEL_BIBLIO && par->bibkey) {
3511 LyXFont font = getLayoutFont(buffer, par);
3514 x = ww - leftMargin(p.bv, p.row)
3515 + font_metrics::width(layout->labelsep, font);
3517 x = p.x - font_metrics::width(layout->labelsep, font)
3518 - par->bibkey->width(p.bv, font);
3520 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3525 void LyXText::paintLastRow(DrawRowParams & p)
3527 Paragraph * par = p.row->par();
3528 ParagraphParameters const & parparams = par->params();
3529 int y_bottom = p.row->height() - 1;
3531 // think about the margins
3532 if (!p.row->next() && bv_owner)
3533 y_bottom -= LYX_PAPER_MARGIN;
3535 int const ww = p.bv->workWidth();
3537 // draw a bottom pagebreak
3538 if (parparams.pagebreakBottom()) {
3539 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
3540 p.yo + y_bottom - 2 * defaultHeight(), p);
3543 // draw the additional space if needed:
3544 int const height = getLengthMarkerHeight(p.bv,
3545 parparams.spaceBottom());
3546 y_bottom -= drawLengthMarker(p, _("Space below"),
3547 parparams.spaceBottom(),
3548 p.yo + y_bottom - height);
3550 Buffer const * buffer = p.bv->buffer();
3552 // draw a bottom line
3553 if (parparams.lineBottom()) {
3554 LyXFont font(LyXFont::ALL_SANE);
3555 int const asc = font_metrics::ascent('x',
3556 getFont(buffer, par,
3557 max(pos_type(0), par->size() - 1)));
3561 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3562 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3563 int const y = p.yo + y_bottom;
3564 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3565 Painter::line_thick);
3570 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3571 int const endlabel = par->getEndLabel();
3576 case END_LABEL_FILLED_BOX:
3578 LyXFont const font = getLabelFont(buffer, par);
3579 int const size = int(0.75 * font_metrics::maxAscent(font));
3580 int const y = (p.yo + p.row->baseline()) - size;
3581 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3583 if (p.row->fill() <= size)
3584 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3586 if (endlabel == END_LABEL_BOX) {
3587 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3589 p.pain->fillRectangle(x, y, size, size,
3594 case END_LABEL_STATIC:
3597 LyXFont font(LyXFont::ALL_SANE);
3598 font = getLabelFont(buffer, par);
3600 LyXFont font = getLabelFont(buffer, par);
3602 string const & str = par->layout()->endlabelstring();
3603 int const x = is_rtl ?
3604 int(p.x) - font_metrics::width(str, font)
3605 : ww - rightMargin(buffer, p.row) - p.row->fill();
3606 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3609 case END_LABEL_NO_LABEL:
3615 void LyXText::paintRowText(DrawRowParams & p)
3617 Paragraph * par = p.row->par();
3618 Buffer const * buffer = p.bv->buffer();
3620 pos_type const last = rowLastPrintable(p.row);
3621 pos_type main_body =
3622 beginningOfMainBody(buffer, par);
3623 if (main_body > 0 &&
3624 (main_body - 1 > last ||
3625 !par->isLineSeparator(main_body - 1))) {
3629 LyXLayout_ptr const & layout = par->layout();
3631 pos_type vpos = p.row->pos();
3632 while (vpos <= last) {
3633 if (p.x > p.bv->workWidth())
3635 pos_type pos = vis2log(vpos);
3636 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3637 p.x += singleWidth(p.bv, par, pos);
3641 if (main_body > 0 && pos == main_body - 1) {
3642 int const lwidth = font_metrics::width(layout->labelsep,
3643 getLabelFont(buffer, par));
3645 p.x += p.label_hfill + lwidth
3646 - singleWidth(p.bv, par, main_body - 1);
3649 if (par->isHfill(pos)) {
3652 int const y0 = p.yo + p.row->baseline();
3653 int const y1 = y0 - defaultHeight() / 2;
3655 p.pain->line(int(p.x), y1, int(p.x), y0,
3656 LColor::added_space);
3658 if (hfillExpansion(buffer, p.row, pos)) {
3659 int const y2 = (y0 + y1) / 2;
3661 if (pos >= main_body) {
3662 p.pain->line(int(p.x), y2,
3663 int(p.x + p.hfill), y2,
3664 LColor::added_space,
3665 Painter::line_onoffdash);
3668 p.pain->line(int(p.x), y2,
3669 int(p.x + p.label_hfill), y2,
3670 LColor::added_space,
3671 Painter::line_onoffdash);
3672 p.x += p.label_hfill;
3674 p.pain->line(int(p.x), y1,
3676 LColor::added_space);
3680 } else if (par->isSeparator(pos)) {
3681 p.x += singleWidth(p.bv, par, pos);
3682 if (pos >= main_body)
3693 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3694 Row * row, int y, bool cleared)
3696 if (row->height() <= 0) {
3697 lyxerr << "LYX_ERROR: row.height: "
3698 << row->height() << endl;
3704 // set up drawing parameters
3706 p.pain = &bv->painter();
3710 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3711 if (inset_owner && (p.x < 0))
3715 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3716 p.cleared = cleared;
3720 // clear to background if necessary
3721 p.cleared = paintRowBackground(p);
3723 // paint the selection background
3724 if (selection.set()) {
3725 paintRowSelection(p);
3728 // vertical lines for appendix
3729 paintRowAppendix(p);
3731 // environment depth brackets
3732 paintRowDepthBar(p);
3734 // draw any stuff wanted for a first row of a paragraph
3739 // draw any stuff wanted for the last row of a paragraph
3740 if (!row->next() || (row->next()->par() != row->par())) {
3749 int LyXText::defaultHeight() const
3751 LyXFont font(LyXFont::ALL_SANE);
3752 return int(font_metrics::maxAscent(font)
3753 + font_metrics::maxDescent(font) * 1.5);
3757 // returns the column near the specified x-coordinate of the row
3758 // x is set to the real beginning of this column
3760 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3761 bool & boundary) const
3764 float fill_separator;
3766 float fill_label_hfill;
3768 prepareToPrint(bview, row, tmpx, fill_separator,
3769 fill_hfill, fill_label_hfill);
3771 pos_type vc = row->pos();
3772 pos_type last = rowLastPrintable(row);
3775 LyXLayout_ptr const & layout = row->par()->layout();
3777 bool left_side = false;
3779 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3780 float last_tmpx = tmpx;
3782 if (main_body > 0 &&
3783 (main_body - 1 > last ||
3784 !row->par()->isLineSeparator(main_body - 1)))
3787 while (vc <= last && tmpx <= x) {
3790 if (main_body > 0 && c == main_body-1) {
3791 tmpx += fill_label_hfill +
3792 font_metrics::width(layout->labelsep,
3793 getLabelFont(bview->buffer(), row->par()));
3794 if (row->par()->isLineSeparator(main_body - 1))
3795 tmpx -= singleWidth(bview, row->par(), main_body-1);
3798 if (hfillExpansion(bview->buffer(), row, c)) {
3799 x += singleWidth(bview, row->par(), c);
3803 tmpx += fill_label_hfill;
3805 else if (row->par()->isSeparator(c)) {
3806 tmpx += singleWidth(bview, row->par(), c);
3808 tmpx+= fill_separator;
3810 tmpx += singleWidth(bview, row->par(), c);
3814 if ((tmpx + last_tmpx) / 2 > x) {
3819 if (vc > last + 1) // This shouldn't happen.
3823 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3824 // some speedup if rtl_support=false
3825 && (!row->next() || row->next()->par() != row->par());
3826 bool const rtl = (lastrow)
3827 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3828 : false; // If lastrow is false, we don't need to compute
3829 // the value of rtl.
3831 if (row->pos() > last) // Row is empty?
3834 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3835 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3837 else if (vc == row->pos()) {
3839 if (bidi_level(c) % 2 == 1)
3842 c = vis2log(vc - 1);
3843 bool const rtl = (bidi_level(c) % 2 == 1);
3844 if (left_side == rtl) {
3846 boundary = isBoundary(bview->buffer(), row->par(), c);
3850 if (row->pos() <= last && c > last
3851 && row->par()->isNewline(last)) {
3852 if (bidi_level(last) % 2 == 0)
3853 tmpx -= singleWidth(bview, row->par(), last);
3855 tmpx += singleWidth(bview, row->par(), last);
3865 // returns pointer to a specified row
3866 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3871 Row * tmprow = firstrow;
3874 // find the first row of the specified paragraph
3875 while (tmprow->next() && tmprow->par() != par) {
3876 y += tmprow->height();
3877 tmprow = tmprow->next();
3880 // now find the wanted row
3881 while (tmprow->pos() < pos
3883 && tmprow->next()->par() == par
3884 && tmprow->next()->pos() <= pos) {
3885 y += tmprow->height();
3886 tmprow = tmprow->next();
3893 Row * LyXText::getRowNearY(int & y) const
3896 // If possible we should optimize this method. (Lgb)
3897 Row * tmprow = firstrow;
3900 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3901 tmpy += tmprow->height();
3902 tmprow = tmprow->next();
3905 y = tmpy; // return the real y
3907 //lyxerr << "returned y = " << y << endl;
3911 // Search from the current cursor position.
3913 Row * tmprow = cursor.row();
3914 int tmpy = cursor.y() - tmprow->baseline();
3916 lyxerr << "cursor.y() = " << tmpy << endl;
3917 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3918 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3919 lyxerr << "first = " << first << endl;
3920 lyxerr << "y = " << y << endl;
3923 lyxerr << "up" << endl;
3925 tmpy -= tmprow->height();
3926 tmprow = tmprow->previous();
3927 } while (tmprow && tmpy - tmprow->height() >= y);
3928 } else if (y > tmpy) {
3929 lyxerr << "down" << endl;
3931 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3932 tmpy += tmprow->height();
3933 tmprow = tmprow->next();
3936 lyxerr << "equal" << endl;
3939 y = tmpy; // return the real y
3941 lyxerr << "returned y = " << y << endl;
3949 int LyXText::getDepth() const
3951 return cursor.par()->getDepth();