1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
17 #include "bufferparams.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
35 #include "rowpainter.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
51 /// top, right, bottom pixel margin
52 extern int const PAPER_MARGIN = 20;
53 /// margin for changebar
54 extern int const CHANGEBAR_MARGIN = 10;
56 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
58 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
61 int LyXText::top_y() const
67 for (Row * row = firstrow; row && row != top_row_; row = row->next()) {
70 return y + top_row_offset_;
74 void LyXText::top_y(int newy)
78 lyxerr[Debug::GUI] << "setting top y = " << newy << endl;
81 top_row_ = getRowNearY(y);
82 top_row_offset_ = newy - y;
83 lyxerr[Debug::GUI] << "changing reference to row: " << top_row_
84 << " offset: " << top_row_offset_ << endl;
88 int LyXText::workWidth(BufferView & bview) const
91 // FIXME: pass (const ?) ref
92 return inset_owner->textWidth(&bview);
94 return bview.workWidth();
98 int LyXText::workWidth(BufferView & bview, Inset * inset) const
100 Paragraph * par = inset->parOwner();
103 pos_type pos = par->getPositionOfInset(inset);
104 lyx::Assert(pos != -1);
106 LyXLayout_ptr const & layout = par->layout();
108 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
109 // Optimization here: in most cases, the real row is
110 // not needed, but only the par/pos values. So we just
111 // construct a dummy row for leftMargin. (JMarc)
115 return workWidth(bview) - leftMargin(&bview, &dummyrow);
118 Row * row = getRow(par, pos, dummy_y);
120 while (frow->previous() && frow->par() == frow->previous()->par())
121 frow = frow->previous();
123 // FIXME: I don't understand this code - jbl
125 unsigned int maxw = 0;
126 while (!frow->isParEnd()) {
127 if ((frow != row) && (maxw < frow->width()))
128 maxw = frow->width();
135 return workWidth(bview);
139 int LyXText::getRealCursorX(BufferView * bview) const
142 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
143 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
148 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
151 if (!Encodings::is_arabic(c))
152 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
153 return c + (0xb0 - '0');
157 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
158 unsigned char next_char = ' ';
160 for (pos_type i = pos+1; i < par->size(); ++i)
161 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
162 next_char = par->getChar(i);
166 if (Encodings::is_arabic(next_char)) {
167 if (Encodings::is_arabic(prev_char) &&
168 !Encodings::is_arabic_special(prev_char))
169 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
171 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
173 if (Encodings::is_arabic(prev_char) &&
174 !Encodings::is_arabic_special(prev_char))
175 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
177 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
181 // This is the comments that some of the warnings below refers to.
182 // There are some issues in this file and I don't think they are
183 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
184 // this is a problem that has been here almost from day one and that a
185 // larger userbase with differenct access patters triggers the bad
186 // behaviour. (segfaults.) What I think happen is: In several places
187 // we store the paragraph in the current cursor and then moves the
188 // cursor. This movement of the cursor will delete paragraph at the
189 // old position if it is now empty. This will make the temporary
190 // pointer to the old cursor paragraph invalid and dangerous to use.
191 // And is some cases this will trigger a segfault. I have marked some
192 // of the cases where this happens with a warning, but I am sure there
193 // are others in this file and in text2.C. There is also a note in
194 // Delete() that you should read. In Delete I store the paragraph->id
195 // instead of a pointer to the paragraph. I am pretty sure this faulty
196 // use of temporary pointers to paragraphs that might have gotten
197 // invalidated (through a cursor movement) before they are used, are
198 // the cause of the strange crashes we get reported often.
200 // It is very tiresom to change this code, especially when it is as
201 // hard to read as it is. Help to fix all the cases where this is done
202 // would be greately appreciated.
206 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
209 if (pos >= par->size())
212 char const c = par->getChar(pos);
213 return singleWidth(bview, par, pos, c);
217 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
218 pos_type pos, char c) const
220 if (pos >= par->size())
223 LyXFont const font = getFont(bview->buffer(), par, pos);
225 // The most common case is handled first (Asger)
226 if (IsPrintable(c)) {
227 if (font.language()->RightToLeft()) {
228 if (font.language()->lang() == "arabic" &&
229 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
230 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
231 if (Encodings::IsComposeChar_arabic(c))
234 c = transformChar(c, par, pos);
235 } else if (font.language()->lang() == "hebrew" &&
236 Encodings::IsComposeChar_hebrew(c))
239 return font_metrics::width(c, font);
243 if (c == Paragraph::META_INSET) {
244 Inset * tmpinset = par->getInset(pos);
246 if (tmpinset->lyxCode() == Inset::HFILL_CODE) {
247 // Because of the representation as vertical lines
251 // this IS needed otherwise on initialitation we don't get the fill
252 // of the row right (ONLY on initialization if we read a file!)
253 // should be changed! (Jug 20011204)
254 tmpinset->update(bview, font);
256 return tmpinset->width(bview, font);
261 if (IsSeparatorChar(c))
263 return font_metrics::width(c, font);
267 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
269 bidi_same_direction = true;
270 if (!lyxrc.rtl_support) {
275 Inset * inset = row->par()->inInset();
276 if (inset && inset->owner() &&
277 inset->owner()->lyxCode() == Inset::ERT_CODE) {
282 bidi_start = row->pos();
283 bidi_end = row->lastPrintablePos();
285 if (bidi_start > bidi_end) {
290 if (bidi_end + 2 - bidi_start >
291 static_cast<pos_type>(log2vis_list.size())) {
293 (bidi_end + 2 - bidi_start < 500) ?
294 500 : 2 * (bidi_end + 2 - bidi_start);
295 log2vis_list.resize(new_size);
296 vis2log_list.resize(new_size);
297 bidi_levels.resize(new_size);
300 vis2log_list[bidi_end + 1 - bidi_start] = -1;
301 log2vis_list[bidi_end + 1 - bidi_start] = -1;
305 row->par()->isRightToLeftPar(buf->params);
309 pos_type const body_pos = row->par()->beginningOfBody();
311 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
312 bool is_space = row->par()->isLineSeparator(lpos);
314 (is_space && lpos + 1 <= bidi_end &&
315 !row->par()->isLineSeparator(lpos + 1) &&
316 !row->par()->isNewline(lpos + 1))
318 LyXFont font = row->par()->getFontSettings(buf->params, pos);
319 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
320 font.number() == LyXFont::ON &&
321 row->par()->getFontSettings(buf->params, lpos - 1).number()
323 font = row->par()->getFontSettings(buf->params, lpos);
328 bool new_rtl = font.isVisibleRightToLeft();
329 bool new_rtl0 = font.isRightToLeft();
332 if (lpos == body_pos - 1
333 && row->pos() < body_pos - 1
335 new_level = (rtl_par) ? 1 : 0;
336 new_rtl = new_rtl0 = rtl_par;
338 new_level = (new_rtl) ? 1 : 2;
340 new_level = (rtl_par) ? 2 : 0;
342 if (is_space && new_level >= level) {
348 int new_level2 = new_level;
350 if (level == new_level && rtl0 != new_rtl0) {
352 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
353 } else if (level < new_level) {
354 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
355 if (new_level > rtl_par)
356 bidi_same_direction = false;
358 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
361 bidi_levels[lpos - bidi_start] = new_level;
363 while (level > new_level2) {
364 pos_type old_lpos = stack[--level];
365 int delta = lpos - old_lpos - 1;
368 log2vis_list[lpos - bidi_start] += delta;
369 log2vis_list[old_lpos - bidi_start] += delta;
371 while (level < new_level)
372 stack[level++] = lpos;
376 pos_type const old_lpos = stack[--level];
377 int delta = bidi_end - old_lpos;
380 log2vis_list[old_lpos - bidi_start] += delta;
383 pos_type vpos = bidi_start - 1;
384 for (pos_type lpos = bidi_start;
385 lpos <= bidi_end; ++lpos) {
386 vpos += log2vis_list[lpos - bidi_start];
387 vis2log_list[vpos - bidi_start] = lpos;
388 log2vis_list[lpos - bidi_start] = vpos;
393 // This method requires a previous call to ComputeBidiTables()
394 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
397 if (!lyxrc.rtl_support || pos == 0)
400 if (!bidi_InRange(pos - 1)) {
401 /// This can happen if pos is the first char of a row.
402 /// Returning false in this case is incorrect!
406 bool const rtl = bidi_level(pos - 1) % 2;
407 bool const rtl2 = bidi_InRange(pos)
408 ? bidi_level(pos) % 2
409 : par->isRightToLeftPar(buf->params);
414 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
415 pos_type pos, LyXFont const & font) const
417 if (!lyxrc.rtl_support)
418 return false; // This is just for speedup
420 bool const rtl = font.isVisibleRightToLeft();
421 bool const rtl2 = bidi_InRange(pos)
422 ? bidi_level(pos) % 2
423 : par->isRightToLeftPar(buf->params);
428 int LyXText::leftMargin(BufferView * bview, Row const * row) const
432 if (row->pos() < row->par()->size())
433 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
434 (ins = row->par()->getInset(row->pos())) &&
435 (ins->needFullRow() || ins->display()))
438 LyXTextClass const & tclass =
439 bview->buffer()->params.getLyXTextClass();
440 LyXLayout_ptr const & layout = row->par()->layout();
442 string parindent = layout->parindent;
446 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
448 // this is the way, LyX handles the LaTeX-Environments.
449 // I have had this idea very late, so it seems to be a
450 // later added hack and this is true
451 if (!row->par()->getDepth()) {
452 if (row->par()->layout() == tclass.defaultLayout()) {
453 // find the previous same level paragraph
454 if (row->par()->previous()) {
455 Paragraph * newpar = row->par()
456 ->depthHook(row->par()->getDepth());
458 newpar->layout()->nextnoindent)
463 // find the next level paragraph
465 Paragraph * newpar = row->par()->outerHook();
467 // make a corresponding row. Needed to call LeftMargin()
469 // check wether it is a sufficent paragraph
470 if (newpar && newpar->layout()->isEnvironment()) {
472 dummyrow.par(newpar);
473 dummyrow.pos(newpar->size());
474 x = leftMargin(bview, &dummyrow);
476 // this is no longer an error, because this function
477 // is used to clear impossible depths after changing
478 // a layout. Since there is always a redo,
479 // LeftMargin() is always called
480 row->par()->params().depth(0);
483 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
484 if (newpar->params().noindent())
487 parindent = newpar->layout()->parindent;
493 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
494 switch (layout->margintype) {
496 if (!layout->leftmargin.empty()) {
497 x += font_metrics::signedWidth(layout->leftmargin,
498 tclass.defaultfont());
500 if (!row->par()->getLabelstring().empty()) {
501 x += font_metrics::signedWidth(layout->labelindent,
503 x += font_metrics::width(row->par()->getLabelstring(),
505 x += font_metrics::width(layout->labelsep, labelfont);
509 x += font_metrics::signedWidth(layout->labelindent, labelfont);
510 // The width of an empty par, even with manual label, should be 0
511 if (!row->par()->empty() && row->pos() >= row->par()->beginningOfBody()) {
512 if (!row->par()->getLabelWidthString().empty()) {
513 x += font_metrics::width(row->par()->getLabelWidthString(),
515 x += font_metrics::width(layout->labelsep, labelfont);
520 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
521 / (row->par()->getDepth() + 4);
523 case MARGIN_FIRST_DYNAMIC:
524 if (layout->labeltype == LABEL_MANUAL) {
525 if (row->pos() >= row->par()->beginningOfBody()) {
526 x += font_metrics::signedWidth(layout->leftmargin,
529 x += font_metrics::signedWidth(layout->labelindent,
532 } else if (row->pos()
533 // Special case to fix problems with
535 || (layout->labeltype == LABEL_STATIC
536 && layout->latextype == LATEX_ENVIRONMENT
537 && ! row->par()->isFirstInSequence())) {
538 x += font_metrics::signedWidth(layout->leftmargin,
540 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
541 && layout->labeltype != LABEL_BIBLIO
542 && layout->labeltype !=
543 LABEL_CENTERED_TOP_ENVIRONMENT) {
544 x += font_metrics::signedWidth(layout->labelindent,
546 x += font_metrics::width(layout->labelsep, labelfont);
547 x += font_metrics::width(row->par()->getLabelstring(),
552 case MARGIN_RIGHT_ADDRESS_BOX:
554 // ok, a terrible hack. The left margin depends on the widest
555 // row in this paragraph. Do not care about footnotes, they
556 // are *NOT* allowed in the LaTeX realisation of this layout.
558 // find the first row of this paragraph
559 Row const * tmprow = row;
560 while (tmprow->previous()
561 && tmprow->previous()->par() == row->par())
562 tmprow = tmprow->previous();
564 int minfill = tmprow->fill();
565 while (tmprow->next() && tmprow->next()->par() == row->par()) {
566 tmprow = tmprow->next();
567 if (tmprow->fill() < minfill)
568 minfill = tmprow->fill();
571 x += font_metrics::signedWidth(layout->leftmargin,
572 tclass.defaultfont());
578 if ((workWidth(*bview) > 0) &&
579 !row->par()->params().leftIndent().zero())
581 LyXLength const len = row->par()->params().leftIndent();
582 int const tw = inset_owner ?
583 inset_owner->latexTextWidth(bview) : workWidth(*bview);
584 x += len.inPixels(tw);
587 LyXAlignment align; // wrong type
589 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
590 align = layout->align;
592 align = row->par()->params().align();
594 // set the correct parindent
595 if (row->pos() == 0) {
596 if ((layout->labeltype == LABEL_NO_LABEL
597 || layout->labeltype == LABEL_TOP_ENVIRONMENT
598 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
599 || (layout->labeltype == LABEL_STATIC
600 && layout->latextype == LATEX_ENVIRONMENT
601 && ! row->par()->isFirstInSequence()))
602 && align == LYX_ALIGN_BLOCK
603 && !row->par()->params().noindent()
604 // in tabulars and ert paragraphs are never indented!
605 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
606 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
607 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
608 && (row->par()->layout() != tclass.defaultLayout() ||
609 bview->buffer()->params.paragraph_separation ==
610 BufferParams::PARSEP_INDENT)) {
611 x += font_metrics::signedWidth(parindent,
612 tclass.defaultfont());
613 } else if (layout->labeltype == LABEL_BIBLIO) {
614 // ale970405 Right width for bibitems
615 x += bibitemMaxWidth(bview, tclass.defaultfont());
623 int LyXText::rightMargin(Buffer const & buf, Row const & row) const
627 if (row.pos() < row.par()->size())
628 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
629 (ins=row.par()->getInset(row.pos())) &&
630 (ins->needFullRow() || ins->display()))
633 LyXTextClass const & tclass = buf.params.getLyXTextClass();
634 LyXLayout_ptr const & layout = row.par()->layout();
637 + font_metrics::signedWidth(tclass.rightmargin(),
638 tclass.defaultfont());
640 // this is the way, LyX handles the LaTeX-Environments.
641 // I have had this idea very late, so it seems to be a
642 // later added hack and this is true
643 if (row.par()->getDepth()) {
644 // find the next level paragraph
646 Paragraph const * newpar = row.par();
649 newpar = newpar->previous();
651 && newpar->getDepth() >= row.par()->getDepth());
653 // make a corresponding row. Needed to call LeftMargin()
655 // check wether it is a sufficent paragraph
656 if (newpar && newpar->layout()->isEnvironment()) {
658 dummyrow.par(const_cast<Paragraph *>(newpar));
660 x = rightMargin(buf, dummyrow);
662 // this is no longer an error, because this function
663 // is used to clear impossible depths after changing
664 // a layout. Since there is always a redo,
665 // LeftMargin() is always called
666 row.par()->params().depth(0);
670 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
671 x += font_metrics::signedWidth(layout->rightmargin,
672 tclass.defaultfont())
673 * 4 / (row.par()->getDepth() + 4);
678 int LyXText::labelEnd(BufferView & bview, Row const & row) const
680 if (row.par()->layout()->margintype == MARGIN_MANUAL) {
682 tmprow.pos(row.par()->size());
683 // return the beginning of the body
684 return leftMargin(&bview, &tmprow);
687 // LabelEnd is only needed if the layout
688 // fills a flushleft label.
695 // this needs special handling - only newlines count as a break point
696 pos_type addressBreakPoint(pos_type i, Paragraph * par)
698 for (; i < par->size(); ++i) {
699 if (par->isNewline(i)) {
701 } else if (par->isInset(i) && par->getInset(i)->display()) {
702 // FIXME: what are we doing modifying stuff here !
703 par->getInset(i)->display(false);
714 LyXText::rowBreakPoint(BufferView & bv, Row const & row) const
716 Paragraph * par = row.par();
718 // maximum pixel width of a row.
719 int width = workWidth(bv) - rightMargin(*bv.buffer(), row);
721 // inset->textWidth() returns -1 via workWidth(),
726 LyXLayout_ptr const & layout = par->layout();
728 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
729 return addressBreakPoint(row.pos(), par);
731 pos_type const pos = row.pos();
732 pos_type const body_pos = par->beginningOfBody();
733 pos_type const last = par->size();
734 pos_type point = last;
739 // Now we iterate through until we reach the right margin
740 // or the end of the par, then choose the possible break
743 int const left = leftMargin(&bv, &row);
746 // pixel width since last breakpoint
750 for (; i < last; ++i) {
752 if (par->isNewline(i)) {
757 char const c = par->getChar(i);
759 int thiswidth = singleWidth(&bv, par, i, c);
761 // add the auto-hfill from label end to the body
762 if (body_pos && i == body_pos) {
763 thiswidth += font_metrics::width(layout->labelsep,
764 getLabelFont(bv.buffer(), par));
765 if (par->isLineSeparator(i - 1))
766 thiswidth -= singleWidth(&bv, par, i - 1);
767 int left_margin = labelEnd(bv, row);
768 if (thiswidth < left_margin)
769 thiswidth = left_margin;
773 chunkwidth += thiswidth;
775 Inset * in = par->isInset(i) ? par->getInset(i) : 0;
776 bool display = (in && (in->display() || in->needFullRow()));
778 // check whether a Display() inset is valid here.
779 // If not, change it to non-display. FIXME:
780 // we should not be modifying things at this
782 if (in && in->display() && (layout->isCommand() ||
783 (layout->labeltype == LABEL_MANUAL && i < body_pos)))
786 // break before a character that will fall off
787 // the right of the row
789 // if no break before or we are at an inset
790 // that will take up a row, break here
791 if (point == last || display || chunkwidth >= (width - left)) {
800 if (!in || in->isChar()) {
801 // some insets are line separators too
802 if (par->isLineSeparator(i)) {
812 // full row insets start at a new row
814 if (pos < last - 1) {
816 if (par->isLineSeparator(i + 1))
819 // to avoid extra rows
828 if (point == last && x >= width) {
829 // didn't find one, break at the point we reached the edge
831 } else if (i == last && x < width) {
832 // found one, but we fell off the end of the par, so prefer
837 // manual labels cannot be broken in LaTeX
838 if (body_pos && point < body_pos)
839 point = body_pos - 1;
845 // returns the minimum space a row needs on the screen in pixel
846 int LyXText::fill(BufferView & bview, Row & row, int paper_width) const
852 // get the pure distance
853 pos_type const last = row.lastPrintablePos();
855 // special handling of the right address boxes
856 if (row.par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
857 int const tmpfill = row.fill();
858 row.fill(0); // the minfill in MarginLeft()
859 w = leftMargin(&bview, &row);
862 w = leftMargin(&bview, &row);
864 Paragraph * par = row.par();
865 LyXLayout_ptr const & layout = par->layout();
867 pos_type const body_pos = par->beginningOfBody();
868 pos_type i = row.pos();
871 if (body_pos > 0 && i == body_pos) {
872 w += font_metrics::width(layout->labelsep, getLabelFont(bview.buffer(), par));
873 if (par->isLineSeparator(i - 1))
874 w -= singleWidth(&bview, par, i - 1);
875 int left_margin = labelEnd(bview, row);
879 w += singleWidth(&bview, par, i);
882 if (body_pos > 0 && body_pos > last) {
883 w += font_metrics::width(layout->labelsep, getLabelFont(bview.buffer(), par));
884 if (last >= 0 && par->isLineSeparator(last))
885 w -= singleWidth(&bview, par, last);
886 int const left_margin = labelEnd(bview, row);
891 int const fill = paper_width - w - rightMargin(*bview.buffer(), row);
896 // returns the minimum space a manual label needs on the screen in pixel
897 int LyXText::labelFill(BufferView & bview, Row const & row) const
899 pos_type last = row.par()->beginningOfBody();
901 lyx::Assert(last > 0);
903 // -1 because a label ends either with a space that is in the label,
904 // or with the beginning of a footnote that is outside the label.
907 // a separator at this end does not count
908 if (row.par()->isLineSeparator(last))
912 pos_type i = row.pos();
914 w += singleWidth(&bview, row.par(), i);
919 string const & labwidstr = row.par()->params().labelWidthString();
920 if (!labwidstr.empty()) {
921 LyXFont const labfont = getLabelFont(bview.buffer(), row.par());
922 int const labwidth = font_metrics::width(labwidstr, labfont);
923 fill = max(labwidth - w, 0);
930 LColor::color LyXText::backgroundColor() const
933 return inset_owner->backgroundColor();
935 return LColor::background;
938 void LyXText::setHeightOfRow(BufferView * bview, Row * row) const
940 // get the maximum ascent and the maximum descent
944 float layoutdesc = 0;
947 Inset * tmpinset = 0;
949 // ok , let us initialize the maxasc and maxdesc value.
950 // This depends in LaTeX of the font of the last character
951 // in the paragraph. The hack below is necessary because
952 // of the possibility of open footnotes
954 // Correction: only the fontsize count. The other properties
955 // are taken from the layoutfont. Nicer on the screen :)
956 Paragraph * par = row->par();
957 Paragraph * firstpar = row->par();
959 LyXLayout_ptr const & layout = firstpar->layout();
961 // as max get the first character of this row then it can increase but not
962 // decrease the height. Just some point to start with so we don't have to
963 // do the assignment below too often.
964 LyXFont font = getFont(bview->buffer(), par, row->pos());
965 LyXFont::FONT_SIZE const tmpsize = font.size();
966 font = getLayoutFont(bview->buffer(), par);
967 LyXFont::FONT_SIZE const size = font.size();
968 font.setSize(tmpsize);
970 LyXFont labelfont = getLabelFont(bview->buffer(), par);
972 float spacing_val = 1.0;
973 if (!row->par()->params().spacing().isDefault()) {
974 spacing_val = row->par()->params().spacing().getValue();
976 spacing_val = bview->buffer()->params.spacing.getValue();
978 //lyxerr << "spacing_val = " << spacing_val << endl;
980 int maxasc = int(font_metrics::maxAscent(font) *
981 layout->spacing.getValue() *
983 int maxdesc = int(font_metrics::maxDescent(font) *
984 layout->spacing.getValue() *
987 pos_type const pos_end = row->lastPos();
991 if (!row->par()->empty()) {
992 // Check if any insets are larger
993 for (pos_type pos = row->pos(); pos <= pos_end; ++pos) {
994 if (row->par()->isInset(pos)) {
995 tmpfont = getFont(bview->buffer(), row->par(), pos);
996 tmpinset = row->par()->getInset(pos);
998 #if 1 // this is needed for deep update on initialitation
999 tmpinset->update(bview, tmpfont);
1001 asc = tmpinset->ascent(bview, tmpfont);
1002 desc = tmpinset->descent(bview, tmpfont);
1003 maxwidth += tmpinset->width(bview, tmpfont);
1004 maxasc = max(maxasc, asc);
1005 maxdesc = max(maxdesc, desc);
1008 maxwidth += singleWidth(bview, row->par(), pos);
1013 // Check if any custom fonts are larger (Asger)
1014 // This is not completely correct, but we can live with the small,
1015 // cosmetic error for now.
1016 LyXFont::FONT_SIZE maxsize =
1017 row->par()->highestFontInRange(row->pos(), pos_end, size);
1018 if (maxsize > font.size()) {
1019 font.setSize(maxsize);
1021 asc = font_metrics::maxAscent(font);
1022 desc = font_metrics::maxDescent(font);
1029 // This is nicer with box insets:
1033 row->ascent_of_text(maxasc);
1035 // is it a top line?
1036 if (!row->pos() && (row->par() == firstpar)) {
1038 // some parksips VERY EASY IMPLEMENTATION
1039 if (bview->buffer()->params.paragraph_separation ==
1040 BufferParams::PARSEP_SKIP)
1042 if (layout->isParagraph()
1043 && firstpar->getDepth() == 0
1044 && firstpar->previous())
1046 maxasc += bview->buffer()->params.getDefSkip().inPixels(*bview);
1047 } else if (firstpar->previous() &&
1048 firstpar->previous()->layout()->isParagraph() &&
1049 firstpar->previous()->getDepth() == 0)
1051 // is it right to use defskip here too? (AS)
1052 maxasc += bview->buffer()->params.getDefSkip().inPixels(*bview);
1057 if (!row->par()->previous() && isTopLevel())
1058 maxasc += PAPER_MARGIN;
1060 // add the vertical spaces, that the user added
1061 maxasc += getLengthMarkerHeight(*bview, firstpar->params().spaceTop());
1063 // do not forget the DTP-lines!
1064 // there height depends on the font of the nearest character
1065 if (firstpar->params().lineTop())
1067 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1069 // and now the pagebreaks
1070 if (firstpar->params().pagebreakTop())
1071 maxasc += 3 * defaultRowHeight();
1073 if (firstpar->params().startOfAppendix())
1074 maxasc += 3 * defaultRowHeight();
1076 // This is special code for the chapter, since the label of this
1077 // layout is printed in an extra row
1078 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1079 && bview->buffer()->params.secnumdepth >= 0)
1081 float spacing_val = 1.0;
1082 if (!row->par()->params().spacing().isDefault()) {
1083 spacing_val = row->par()->params().spacing().getValue();
1085 spacing_val = bview->buffer()->params.spacing.getValue();
1088 labeladdon = int(font_metrics::maxDescent(labelfont) *
1089 layout->spacing.getValue() *
1091 + int(font_metrics::maxAscent(labelfont) *
1092 layout->spacing.getValue() *
1096 // special code for the top label
1097 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1098 || layout->labeltype == LABEL_BIBLIO
1099 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1100 && row->par()->isFirstInSequence()
1101 && !row->par()->getLabelstring().empty())
1103 float spacing_val = 1.0;
1104 if (!row->par()->params().spacing().isDefault()) {
1105 spacing_val = row->par()->params().spacing().getValue();
1107 spacing_val = bview->buffer()->params.spacing.getValue();
1111 (font_metrics::maxAscent(labelfont) *
1112 layout->spacing.getValue() *
1114 +(font_metrics::maxDescent(labelfont) *
1115 layout->spacing.getValue() *
1117 + layout->topsep * defaultRowHeight()
1118 + layout->labelbottomsep * defaultRowHeight());
1121 // and now the layout spaces, for example before and after a section,
1122 // or between the items of a itemize or enumerate environment
1124 if (!firstpar->params().pagebreakTop()) {
1125 Paragraph * prev = row->par()->previous();
1127 prev = row->par()->depthHook(row->par()->getDepth());
1128 if (prev && prev->layout() == firstpar->layout() &&
1129 prev->getDepth() == firstpar->getDepth() &&
1130 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1132 layoutasc = (layout->itemsep * defaultRowHeight());
1133 } else if (row->previous()) {
1134 tmptop = layout->topsep;
1136 if (row->previous()->par()->getDepth() >= row->par()->getDepth())
1137 tmptop -= row->previous()->par()->layout()->bottomsep;
1140 layoutasc = (tmptop * defaultRowHeight());
1141 } else if (row->par()->params().lineTop()) {
1142 tmptop = layout->topsep;
1145 layoutasc = (tmptop * defaultRowHeight());
1148 prev = row->par()->outerHook();
1150 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1152 if (firstpar->previous() &&
1153 firstpar->previous()->getDepth() == 0 &&
1154 firstpar->previous()->layout() !=
1158 } else if (firstpar->previous()) {
1159 maxasc += int(layout->parsep * defaultRowHeight());
1165 // is it a bottom line?
1166 if (row->par() == par
1167 && (!row->next() || row->next()->par() != row->par())) {
1168 // the bottom margin
1169 if (!par->next() && isTopLevel())
1170 maxdesc += PAPER_MARGIN;
1172 // add the vertical spaces, that the user added
1173 maxdesc += getLengthMarkerHeight(*bview, firstpar->params().spaceBottom());
1175 // do not forget the DTP-lines!
1176 // there height depends on the font of the nearest character
1177 if (firstpar->params().lineBottom())
1178 maxdesc += 2 * font_metrics::ascent('x',
1179 getFont(bview->buffer(),
1181 max(pos_type(0), par->size() - 1)));
1183 // and now the pagebreaks
1184 if (firstpar->params().pagebreakBottom())
1185 maxdesc += 3 * defaultRowHeight();
1187 // and now the layout spaces, for example before and after
1188 // a section, or between the items of a itemize or enumerate
1190 if (!firstpar->params().pagebreakBottom()
1191 && row->par()->next()) {
1192 Paragraph * nextpar = row->par()->next();
1193 Paragraph * comparepar = row->par();
1197 if (comparepar->getDepth() > nextpar->getDepth()) {
1198 usual = (comparepar->layout()->bottomsep * defaultRowHeight());
1199 comparepar = comparepar->depthHook(nextpar->getDepth());
1200 if (comparepar->layout()!= nextpar->layout()
1201 || nextpar->getLabelWidthString() !=
1202 comparepar->getLabelWidthString())
1204 unusual = (comparepar->layout()->bottomsep * defaultRowHeight());
1206 if (unusual > usual)
1207 layoutdesc = unusual;
1210 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1212 if (comparepar->layout() != nextpar->layout()
1213 || nextpar->getLabelWidthString() !=
1214 comparepar->getLabelWidthString())
1215 layoutdesc = int(comparepar->layout()->bottomsep * defaultRowHeight());
1220 // incalculate the layout spaces
1221 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1222 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1224 // calculate the new height of the text
1225 height -= row->height();
1227 row->height(maxasc + maxdesc + labeladdon);
1228 row->baseline(maxasc + labeladdon);
1230 height += row->height();
1232 row->top_of_text(row->baseline() - font_metrics::maxAscent(font));
1235 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1238 row->width(maxwidth);
1239 prepareToPrint(bview, row, x, dummy, dummy, dummy, false);
1241 row->width(int(maxwidth + x));
1244 width = max(0, workWidth(*bview));
1246 if (r->width() > width)
1254 // Appends the implicit specified paragraph before the specified row,
1255 // start at the implicit given position
1256 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1258 pos_type const last = row->par()->size();
1262 pos_type z = rowBreakPoint(*bview, *row);
1268 insertRow(row, row->par(), z);
1275 // Set the dimensions of the row
1276 // fixed fill setting now by calling inset->update() in
1277 // SingleWidth when needed!
1278 tmprow->fill(fill(*bview, *tmprow, workWidth(*bview)));
1279 setHeightOfRow(bview, tmprow);
1285 // Do we even need this at all ? Code that uses RowPainter *already*
1286 // sets need_break_row when it sees a CHANGED_IN_DRAW, though not
1288 void LyXText::markChangeInDraw(BufferView * bv, Row * row, Row * prev)
1290 if (prev && prev->par() == row->par()) {
1291 breakAgainOneRow(bv, prev);
1292 if (prev->next() != row) {
1293 // breakAgainOneRow() has removed row_
1294 need_break_row = prev;
1296 need_break_row = row;
1299 need_break_row = firstrow;
1301 need_break_row = prev->next();
1303 setCursor(bv, cursor.par(), cursor.pos());
1308 void LyXText::breakAgain(BufferView * bview, Row * row) const
1310 bool not_ready = true;
1313 pos_type z = rowBreakPoint(*bview, *row);
1316 if (z < row->par()->size()) {
1317 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1320 insertRow(row, row->par(), z);
1326 if (row->pos() == z)
1327 not_ready = false; // the rest will not change
1333 // if there are some rows too much, delete them
1334 // only if you broke the whole paragraph!
1335 Row * tmprow2 = row;
1336 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1337 tmprow2 = tmprow2->next();
1339 while (tmprow2 != row) {
1340 tmprow2 = tmprow2->previous();
1341 removeRow(tmprow2->next());
1346 // set the dimensions of the row
1347 tmprow->fill(fill(*bview, *tmprow, workWidth(*bview)));
1348 setHeightOfRow(bview, tmprow);
1349 } while (not_ready);
1353 // this is just a little changed version of break again
1354 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1356 pos_type z = rowBreakPoint(*bview, *row);
1359 if (z < row->par()->size()) {
1361 || (row->next() && row->next()->par() != row->par())) {
1364 insertRow(row, row->par(), z);
1370 if (row->pos() != z)
1374 // if there are some rows too much, delete them
1375 // only if you broke the whole paragraph!
1376 Row * tmprow2 = row;
1377 while (tmprow2->next()
1378 && tmprow2->next()->par() == row->par()) {
1379 tmprow2 = tmprow2->next();
1381 while (tmprow2 != row) {
1382 tmprow2 = tmprow2->previous();
1383 removeRow(tmprow2->next());
1387 // set the dimensions of the row
1388 tmprow->fill(fill(*bview, *tmprow, workWidth(*bview)));
1389 setHeightOfRow(bview, tmprow);
1393 void LyXText::breakParagraph(BufferView * bview,
1394 ParagraphList & paragraphs, char keep_layout)
1396 // allow only if at start or end, or all previous is new text
1397 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1398 && cursor.par()->isChangeEdited(0, cursor.pos()))
1401 LyXTextClass const & tclass =
1402 bview->buffer()->params.getLyXTextClass();
1403 LyXLayout_ptr const & layout = cursor.par()->layout();
1405 // this is only allowed, if the current paragraph is not empty or caption
1406 // and if it has not the keepempty flag aktive
1407 if (cursor.par()->empty()
1408 && layout->labeltype != LABEL_SENSITIVE
1409 && !layout->keepempty)
1412 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1414 // Always break behind a space
1416 // It is better to erase the space (Dekel)
1417 if (cursor.pos() < cursor.par()->size()
1418 && cursor.par()->isLineSeparator(cursor.pos()))
1419 cursor.par()->erase(cursor.pos());
1420 // cursor.pos(cursor.pos() + 1);
1422 // break the paragraph
1426 keep_layout = layout->isEnvironment();
1428 // we need to set this before we insert the paragraph. IMO the
1429 // breakParagraph call should return a bool if it inserts the
1430 // paragraph before or behind and we should react on that one
1431 // but we can fix this in 1.3.0 (Jug 20020509)
1432 bool const isempty = (layout->keepempty && cursor.par()->empty());
1433 ::breakParagraph(bview->buffer()->params, paragraphs, cursor.par(), cursor.pos(),
1436 // well this is the caption hack since one caption is really enough
1437 if (layout->labeltype == LABEL_SENSITIVE) {
1439 // set to standard-layout
1440 cursor.par()->applyLayout(tclass.defaultLayout());
1442 // set to standard-layout
1443 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1446 // if the cursor is at the beginning of a row without prior newline,
1448 // This touches only the screen-update. Otherwise we would may have
1449 // an empty row on the screen
1450 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1451 && cursor.row()->pos() == cursor.pos())
1456 int y = cursor.y() - cursor.row()->baseline();
1458 // Do not forget the special right address boxes
1459 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1460 Row * r = cursor.row();
1461 while (r->previous() && r->previous()->par() == r->par()) {
1467 postPaint(*bview, y);
1469 removeParagraph(cursor.row());
1471 // set the dimensions of the cursor row
1472 cursor.row()->fill(fill(*bview, *cursor.row(), workWidth(*bview)));
1474 setHeightOfRow(bview, cursor.row());
1476 #warning Trouble Point! (Lgb)
1477 // When ::breakParagraph is called from within an inset we must
1478 // ensure that the correct ParagraphList is used. Today that is not
1479 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1480 while (!cursor.par()->next()->empty()
1481 && cursor.par()->next()->isNewline(0))
1482 cursor.par()->next()->erase(0);
1484 insertParagraph(bview, cursor.par()->next(), cursor.row());
1486 updateCounters(bview);
1488 // This check is necessary. Otherwise the new empty paragraph will
1489 // be deleted automatically. And it is more friendly for the user!
1490 if (cursor.pos() || isempty)
1491 setCursor(bview, cursor.par()->next(), 0);
1493 setCursor(bview, cursor.par(), 0);
1495 if (cursor.row()->next())
1496 breakAgain(bview, cursor.row()->next());
1502 // Just a macro to make some thing easier.
1503 void LyXText::redoParagraph(BufferView * bview) const
1506 redoParagraphs(bview, cursor, cursor.par()->next());
1507 setCursorIntern(bview, cursor.par(), cursor.pos());
1511 // insert a character, moves all the following breaks in the
1512 // same Paragraph one to the right and make a rebreak
1513 void LyXText::insertChar(BufferView * bview, char c)
1515 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1517 // When the free-spacing option is set for the current layout,
1518 // disable the double-space checking
1520 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1521 cursor.row()->par()->isFreeSpacing();
1523 if (lyxrc.auto_number) {
1524 static string const number_operators = "+-/*";
1525 static string const number_unary_operators = "+-";
1526 static string const number_seperators = ".,:";
1528 if (current_font.number() == LyXFont::ON) {
1529 if (!IsDigit(c) && !contains(number_operators, c) &&
1530 !(contains(number_seperators, c) &&
1531 cursor.pos() >= 1 &&
1532 cursor.pos() < cursor.par()->size() &&
1533 getFont(bview->buffer(),
1535 cursor.pos()).number() == LyXFont::ON &&
1536 getFont(bview->buffer(),
1538 cursor.pos() - 1).number() == LyXFont::ON)
1540 number(bview); // Set current_font.number to OFF
1541 } else if (IsDigit(c) &&
1542 real_current_font.isVisibleRightToLeft()) {
1543 number(bview); // Set current_font.number to ON
1545 if (cursor.pos() > 0) {
1546 char const c = cursor.par()->getChar(cursor.pos() - 1);
1547 if (contains(number_unary_operators, c) &&
1548 (cursor.pos() == 1 ||
1549 cursor.par()->isSeparator(cursor.pos() - 2) ||
1550 cursor.par()->isNewline(cursor.pos() - 2))
1552 setCharFont(bview->buffer(),
1556 } else if (contains(number_seperators, c) &&
1557 cursor.pos() >= 2 &&
1558 getFont(bview->buffer(),
1560 cursor.pos() - 2).number() == LyXFont::ON) {
1561 setCharFont(bview->buffer(),
1571 // First check, if there will be two blanks together or a blank at
1572 // the beginning of a paragraph.
1573 // I decided to handle blanks like normal characters, the main
1574 // difference are the special checks when calculating the row.fill
1575 // (blank does not count at the end of a row) and the check here
1577 // The bug is triggered when we type in a description environment:
1578 // The current_font is not changed when we go from label to main text
1579 // and it should (along with realtmpfont) when we type the space.
1580 // CHECK There is a bug here! (Asger)
1582 LyXFont realtmpfont = real_current_font;
1583 LyXFont rawtmpfont = current_font;
1584 // store the current font. This is because of the use of cursor
1585 // movements. The moving cursor would refresh the current font
1587 // Get the font that is used to calculate the baselineskip
1588 pos_type const lastpos = cursor.par()->size();
1589 LyXFont rawparfont =
1590 cursor.par()->getFontSettings(bview->buffer()->params,
1593 bool jumped_over_space = false;
1595 if (!freeSpacing && IsLineSeparatorChar(c)) {
1596 if ((cursor.pos() > 0
1597 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1598 || (cursor.pos() > 0
1599 && cursor.par()->isNewline(cursor.pos() - 1))
1600 || (cursor.pos() == 0)) {
1601 static bool sent_space_message = false;
1602 if (!sent_space_message) {
1603 if (cursor.pos() == 0)
1604 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1606 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1607 sent_space_message = true;
1614 // the display inset stuff
1615 if (cursor.row()->pos() < cursor.row()->par()->size()
1616 && cursor.row()->par()->isInset(cursor.row()->pos())) {
1617 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1618 if (inset && (inset->display() || inset->needFullRow())) {
1619 // force a new break
1620 cursor.row()->fill(-1); // to force a new break
1624 // get the cursor row fist
1625 Row * row = cursor.row();
1626 int y = cursor.y() - row->baseline();
1627 if (c != Paragraph::META_INSET) {
1628 // Here case LyXText::InsertInset already insertet the character
1629 cursor.par()->insertChar(cursor.pos(), c);
1631 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1633 if (!jumped_over_space) {
1634 // refresh the positions
1636 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1637 tmprow = tmprow->next();
1638 tmprow->pos(tmprow->pos() + 1);
1642 // Is there a break one row above
1643 if (row->previous() && row->previous()->par() == row->par()
1644 && (cursor.par()->isLineSeparator(cursor.pos())
1645 || cursor.par()->isNewline(cursor.pos())
1646 || ((cursor.pos() < cursor.par()->size()) &&
1647 cursor.par()->isInset(cursor.pos()+1))
1648 || cursor.row()->fill() == -1))
1650 pos_type z = rowBreakPoint(*bview, *row->previous());
1652 if (z >= row->pos()) {
1655 // set the dimensions of the row above
1656 row->previous()->fill(fill(*bview,
1658 workWidth(*bview)));
1660 setHeightOfRow(bview, row->previous());
1662 y -= row->previous()->height();
1664 postPaint(*bview, y);
1666 breakAgainOneRow(bview, row);
1668 current_font = rawtmpfont;
1669 real_current_font = realtmpfont;
1670 setCursor(bview, cursor.par(), cursor.pos() + 1,
1671 false, cursor.boundary());
1672 // cursor MUST be in row now.
1674 if (row->next() && row->next()->par() == row->par())
1675 need_break_row = row->next();
1679 // check, wether the last characters font has changed.
1680 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1681 && rawparfont != rawtmpfont)
1682 redoHeightOfParagraph(bview);
1689 // recalculate the fill of the row
1690 if (row->fill() >= 0) {
1691 // needed because a newline will set fill to -1. Otherwise
1692 // we would not get a rebreak!
1693 row->fill(fill(*bview, *row, workWidth(*bview)));
1696 if (c == Paragraph::META_INSET || row->fill() < 0) {
1697 postPaint(*bview, y);
1698 breakAgainOneRow(bview, row);
1699 // will the cursor be in another row now?
1700 if (row->lastPos() <= cursor.pos() + 1 && row->next()) {
1701 if (row->next() && row->next()->par() == row->par())
1702 // this should always be true
1704 breakAgainOneRow(bview, row);
1706 current_font = rawtmpfont;
1707 real_current_font = realtmpfont;
1709 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1711 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1712 != cursor.boundary())
1713 setCursor(bview, cursor.par(), cursor.pos(), false,
1714 !cursor.boundary());
1715 if (row->next() && row->next()->par() == row->par())
1716 need_break_row = row->next();
1720 // FIXME: similar code is duplicated all over - make resetHeightOfRow
1721 int const tmpheight = row->height();
1723 setHeightOfRow(bview, row);
1725 if (tmpheight == row->height()) {
1726 postRowPaint(*bview, row, y);
1728 postPaint(*bview, y);
1731 current_font = rawtmpfont;
1732 real_current_font = realtmpfont;
1733 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1737 // check, wether the last characters font has changed.
1738 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1739 && rawparfont != rawtmpfont) {
1740 redoHeightOfParagraph(bview);
1742 // now the special right address boxes
1743 if (cursor.par()->layout()->margintype
1744 == MARGIN_RIGHT_ADDRESS_BOX) {
1745 redoDrawingOfParagraph(bview, cursor);
1753 void LyXText::charInserted()
1755 // Here we could call FinishUndo for every 20 characters inserted.
1756 // This is from my experience how emacs does it.
1757 static unsigned int counter;
1767 void LyXText::prepareToPrint(BufferView * bview,
1768 Row * row, float & x,
1769 float & fill_separator,
1771 float & fill_label_hfill,
1777 float w = row->fill();
1779 fill_label_hfill = 0;
1781 fill_label_hfill = 0;
1784 row->par()->isRightToLeftPar(bview->buffer()->params);
1786 x = (workWidth(*bview) > 0)
1787 ? rightMargin(*bview->buffer(), *row) : 0;
1789 x = (workWidth(*bview) > 0)
1790 ? leftMargin(bview, row) : 0;
1792 // is there a manual margin with a manual label
1793 LyXLayout_ptr const & layout = row->par()->layout();
1795 if (layout->margintype == MARGIN_MANUAL
1796 && layout->labeltype == LABEL_MANUAL) {
1797 /// We might have real hfills in the label part
1798 nlh = row->numberOfLabelHfills();
1800 // A manual label par (e.g. List) has an auto-hfill
1801 // between the label text and the body of the
1803 // But we don't want to do this auto hfill if the par
1805 if (!row->par()->empty())
1808 if (nlh && !row->par()->getLabelWidthString().empty()) {
1809 fill_label_hfill = labelFill(*bview, *row) / nlh;
1813 // are there any hfills in the row?
1814 float const nh = row->numberOfHfills();
1818 fill_hfill = w / nh;
1819 // we don't have to look at the alignment if it is ALIGN_LEFT and
1820 // if the row is already larger then the permitted width as then
1821 // we force the LEFT_ALIGN'edness!
1822 } else if (static_cast<int>(row->width()) < workWidth(*bview)) {
1823 // is it block, flushleft or flushright?
1824 // set x how you need it
1826 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
1827 align = layout->align;
1829 align = row->par()->params().align();
1832 // center displayed insets
1834 if (row->pos() < row->par()->size()
1835 && row->par()->isInset(row->pos())
1836 && (inset = row->par()->getInset(row->pos()))
1837 && (inset->display())) // || (inset->scroll() < 0)))
1838 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
1839 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1840 // ERT insets should always be LEFT ALIGNED on screen
1841 inset = row->par()->inInset();
1842 if (inset && inset->owner() &&
1843 inset->owner()->lyxCode() == Inset::ERT_CODE)
1845 align = LYX_ALIGN_LEFT;
1849 case LYX_ALIGN_BLOCK:
1850 ns = row->numberOfSeparators();
1851 if (ns && row->next() && row->next()->par() == row->par() &&
1852 !(row->next()->par()->isNewline(row->next()->pos() - 1))
1853 && !(row->next()->par()->isInset(row->next()->pos())
1854 && row->next()->par()->getInset(row->next()->pos())
1855 && row->next()->par()->getInset(row->next()->pos())->display())
1858 fill_separator = w / ns;
1859 } else if (is_rtl) {
1863 case LYX_ALIGN_RIGHT:
1866 case LYX_ALIGN_CENTER:
1874 computeBidiTables(bview->buffer(), row);
1876 pos_type body_pos = row->par()->beginningOfBody();
1877 pos_type last = row->lastPos();
1880 (body_pos - 1 > last ||
1881 !row->par()->isLineSeparator(body_pos - 1))) {
1882 x += font_metrics::width(layout->labelsep,
1883 getLabelFont(bview->buffer(), row->par()));
1884 if (body_pos - 1 <= last)
1885 x += fill_label_hfill;
1891 // important for the screen
1894 // the cursor set functions have a special mechanism. When they
1895 // realize, that you left an empty paragraph, they will delete it.
1896 // They also delete the corresponding row
1898 void LyXText::cursorRightOneWord(BufferView * bview) const
1900 // treat floats, HFills and Insets as words
1901 LyXCursor tmpcursor = cursor;
1902 // CHECK See comment on top of text.C
1904 if (tmpcursor.pos() == tmpcursor.par()->size()
1905 && tmpcursor.par()->next()) {
1906 tmpcursor.par(tmpcursor.par()->next());
1911 // Skip through initial nonword stuff.
1912 while (tmpcursor.pos() < tmpcursor.par()->size() &&
1913 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
1914 // printf("Current pos1 %d", tmpcursor.pos()) ;
1915 tmpcursor.pos(tmpcursor.pos() + 1);
1918 // Advance through word.
1919 while (tmpcursor.pos() < tmpcursor.par()->size() &&
1920 tmpcursor.par()->isWord(tmpcursor.pos())) {
1921 // printf("Current pos2 %d", tmpcursor.pos()) ;
1922 tmpcursor.pos(tmpcursor.pos() + 1);
1926 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
1930 void LyXText::cursorTab(BufferView * bview) const
1932 LyXCursor tmpcursor = cursor;
1933 while (tmpcursor.pos() < tmpcursor.par()->size()
1934 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
1935 tmpcursor.pos(tmpcursor.pos() + 1);
1937 if (tmpcursor.pos() == tmpcursor.par()->size()) {
1938 if (tmpcursor.par()->next()) {
1939 tmpcursor.par(tmpcursor.par()->next());
1943 tmpcursor.pos(tmpcursor.pos() + 1);
1944 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
1948 // Skip initial whitespace at end of word and move cursor to *start*
1949 // of prior word, not to end of next prior word.
1950 void LyXText::cursorLeftOneWord(BufferView * bview) const
1952 LyXCursor tmpcursor = cursor;
1953 cursorLeftOneWord(tmpcursor);
1954 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
1958 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
1960 // treat HFills, floats and Insets as words
1963 && (cur.par()->isSeparator(cur.pos() - 1)
1964 || cur.par()->isKomma(cur.pos() - 1)
1965 || cur.par()->isNewline(cur.pos() - 1))
1966 && !(cur.par()->isHfill(cur.pos() - 1)
1967 || cur.par()->isInset(cur.pos() - 1)))
1968 cur.pos(cur.pos() - 1);
1971 && (cur.par()->isInset(cur.pos() - 1)
1972 || cur.par()->isHfill(cur.pos() - 1))) {
1973 cur.pos(cur.pos() - 1);
1974 } else if (!cur.pos()) {
1975 if (cur.par()->previous()) {
1976 cur.par(cur.par()->previous());
1977 cur.pos(cur.par()->size());
1979 } else { // Here, cur != 0
1980 while (cur.pos() > 0 &&
1981 cur.par()->isWord(cur.pos() - 1))
1982 cur.pos(cur.pos() - 1);
1987 // Select current word. This depends on behaviour of
1988 // CursorLeftOneWord(), so it is patched as well.
1989 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
1990 word_location const loc) const
1992 // first put the cursor where we wana start to select the word
1995 case WHOLE_WORD_STRICT:
1996 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
1997 || cursor.par()->isSeparator(cursor.pos())
1998 || cursor.par()->isKomma(cursor.pos())
1999 || cursor.par()->isNewline(cursor.pos())
2000 || cursor.par()->isSeparator(cursor.pos() - 1)
2001 || cursor.par()->isKomma(cursor.pos() - 1)
2002 || cursor.par()->isNewline(cursor.pos() - 1)) {
2006 // no break here, we go to the next
2009 // Move cursor to the beginning, when not already there.
2010 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2011 && !(from.par()->isKomma(from.pos() - 1)
2012 || from.par()->isNewline(from.pos() - 1)))
2013 cursorLeftOneWord(from);
2016 // always move the cursor to the beginning of previous word
2017 cursorLeftOneWord(from);
2020 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2026 while (to.pos() < to.par()->size()
2027 && !to.par()->isSeparator(to.pos())
2028 && !to.par()->isKomma(to.pos())
2029 && !to.par()->isNewline(to.pos())
2030 && !to.par()->isHfill(to.pos())
2031 && !to.par()->isInset(to.pos()))
2033 to.pos(to.pos() + 1);
2038 void LyXText::selectWord(BufferView * bview, word_location const loc)
2042 getWord(from, to, loc);
2044 setCursor(bview, from.par(), from.pos());
2047 selection.cursor = cursor;
2048 setCursor(bview, to.par(), to.pos());
2049 setSelection(bview);
2053 // Select the word currently under the cursor when no
2054 // selection is currently set
2055 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2056 word_location const loc)
2058 if (!selection.set()) {
2059 selectWord(bview, loc);
2060 return selection.set();
2066 void LyXText::acceptChange(BufferView * bv)
2068 if (!selection.set() && cursor.par()->size())
2073 if (selection.start.par() == selection.end.par()) {
2074 LyXCursor & startc = selection.start;
2075 LyXCursor & endc = selection.end;
2076 setUndo(bv, Undo::INSERT, startc.par(), startc.par()->next());
2077 startc.par()->acceptChange(startc.pos(), endc.pos());
2080 redoParagraphs(bv, startc, startc.par()->next());
2081 setCursorIntern(bv, startc.par(), 0);
2083 #warning handle multi par selection
2087 void LyXText::rejectChange(BufferView * bv)
2089 if (!selection.set() && cursor.par()->size())
2094 if (selection.start.par() == selection.end.par()) {
2095 LyXCursor & startc = selection.start;
2096 LyXCursor & endc = selection.end;
2097 setUndo(bv, Undo::INSERT, startc.par(), startc.par()->next());
2098 startc.par()->rejectChange(startc.pos(), endc.pos());
2101 redoParagraphs(bv, startc, startc.par()->next());
2102 setCursorIntern(bv, startc.par(), 0);
2104 #warning handle multi par selection
2108 // This function is only used by the spellchecker for NextWord().
2109 // It doesn't handle LYX_ACCENTs and probably never will.
2111 LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const
2113 if (the_locking_inset) {
2114 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2115 if (!word.word().empty()) {
2116 value += float(cursor.y());
2117 value /= float(height);
2120 // we have to go on checking so move cursor to the next char
2121 if (cursor.pos() == cursor.par()->size()) {
2122 if (!cursor.par()->next())
2124 cursor.par(cursor.par()->next());
2127 cursor.pos(cursor.pos() + 1);
2129 Paragraph * tmppar = cursor.par();
2131 // If this is not the very first word, skip rest of
2132 // current word because we are probably in the middle
2133 // of a word if there is text here.
2134 if (cursor.pos() || cursor.par()->previous()) {
2135 while (cursor.pos() < cursor.par()->size()
2136 && cursor.par()->isLetter(cursor.pos()))
2137 cursor.pos(cursor.pos() + 1);
2140 // Now, skip until we have real text (will jump paragraphs)
2142 Paragraph * cpar(cursor.par());
2143 pos_type const cpos(cursor.pos());
2145 if (cpos == cpar->size()) {
2147 cursor.par(cpar->next());
2154 bool const is_bad_inset(cpar->isInset(cpos)
2155 && !cpar->getInset(cpos)->allowSpellcheck());
2157 if (cpar->isLetter(cpos) && !isDeletedText(*cpar, cpos)
2161 cursor.pos(cpos + 1);
2164 // now check if we hit an inset so it has to be a inset containing text!
2165 if (cursor.pos() < cursor.par()->size() &&
2166 cursor.par()->isInset(cursor.pos())) {
2168 cursor.par()->getInset(cursor.pos())->edit(bview);
2169 // now call us again to do the above trick
2170 // but obviously we have to start from down below ;)
2171 return bview->text->selectNextWordToSpellcheck(bview, value);
2174 // Update the value if we changed paragraphs
2175 if (cursor.par() != tmppar) {
2176 setCursor(bview, cursor.par(), cursor.pos());
2177 value = float(cursor.y())/float(height);
2180 // Start the selection from here
2181 selection.cursor = cursor;
2184 getFont(bview->buffer(), cursor.par(), cursor.pos())
2185 .language()->code());
2186 // and find the end of the word (insets like optional hyphens
2187 // and ligature break are part of a word)
2188 while (cursor.pos() < cursor.par()->size()
2189 && cursor.par()->isLetter(cursor.pos())
2190 && !isDeletedText(*cursor.par(), cursor.pos()))
2191 cursor.pos(cursor.pos() + 1);
2193 // Finally, we copy the word to a string and return it
2195 if (selection.cursor.pos() < cursor.pos()) {
2197 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2198 if (!cursor.par()->isInset(i))
2199 str += cursor.par()->getChar(i);
2202 return WordLangTuple(str, lang_code);
2206 // This one is also only for the spellchecker
2207 void LyXText::selectSelectedWord(BufferView * bview)
2209 if (the_locking_inset) {
2210 the_locking_inset->selectSelectedWord(bview);
2213 // move cursor to the beginning
2214 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2216 // set the sel cursor
2217 selection.cursor = cursor;
2219 // now find the end of the word
2220 while (cursor.pos() < cursor.par()->size()
2221 && (cursor.par()->isLetter(cursor.pos())))
2222 cursor.pos(cursor.pos() + 1);
2224 setCursor(bview, cursor.par(), cursor.pos());
2226 // finally set the selection
2227 setSelection(bview);
2231 // Delete from cursor up to the end of the current or next word.
2232 void LyXText::deleteWordForward(BufferView * bview)
2234 if (cursor.par()->empty())
2237 LyXCursor tmpcursor = cursor;
2238 tmpcursor.row(0); //Â ??
2239 selection.set(true); // to avoid deletion
2240 cursorRightOneWord(bview);
2241 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2242 selection.cursor = cursor;
2244 setSelection(bview);
2246 // Great, CutSelection() gets rid of multiple spaces.
2247 cutSelection(bview, true, false);
2252 // Delete from cursor to start of current or prior word.
2253 void LyXText::deleteWordBackward(BufferView * bview)
2255 if (cursor.par()->empty())
2258 LyXCursor tmpcursor = cursor;
2259 tmpcursor.row(0); // ??
2260 selection.set(true); // to avoid deletion
2261 cursorLeftOneWord(bview);
2262 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2263 selection.cursor = cursor;
2265 setSelection(bview);
2266 cutSelection(bview, true, false);
2271 // Kill to end of line.
2272 void LyXText::deleteLineForward(BufferView * bview)
2274 if (cursor.par()->empty())
2275 // Paragraph is empty, so we just go to the right
2278 LyXCursor tmpcursor = cursor;
2279 // We can't store the row over a regular setCursor
2280 // so we set it to 0 and reset it afterwards.
2281 tmpcursor.row(0); //Â ??
2282 selection.set(true); // to avoid deletion
2284 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2285 selection.cursor = cursor;
2287 setSelection(bview);
2288 // What is this test for ??? (JMarc)
2289 if (!selection.set()) {
2290 deleteWordForward(bview);
2292 cutSelection(bview, true, false);
2298 void LyXText::changeCase(BufferView & bview, LyXText::TextCase action)
2303 if (selection.set()) {
2304 from = selection.start;
2307 getWord(from, to, PARTIAL_WORD);
2308 setCursor(&bview, to.par(), to.pos() + 1);
2311 lyx::Assert(from <= to);
2313 setUndo(&bview, Undo::FINISH, from.par(), to.par()->next());
2315 pos_type pos = from.pos();
2316 Paragraph * par = from.par();
2318 while (par && (pos != to.pos() || par != to.par())) {
2319 if (pos == par->size()) {
2324 unsigned char c = par->getChar(pos);
2325 if (!IsInsetChar(c)) {
2327 case text_lowercase:
2330 case text_capitalization:
2332 action = text_lowercase;
2334 case text_uppercase:
2340 par->setChar(pos, c);
2341 checkParagraph(&bview, par, pos);
2346 if (to.row() != from.row())
2347 postPaint(bview, from.y() - from.row()->baseline());
2351 void LyXText::transposeChars(BufferView & bview)
2353 Paragraph * tmppar = cursor.par();
2355 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2357 pos_type tmppos = cursor.pos();
2359 // First decide if it is possible to transpose at all
2361 if (tmppos == 0 || tmppos == tmppar->size())
2364 if (isDeletedText(*tmppar, tmppos - 1)
2365 || isDeletedText(*tmppar, tmppos))
2368 unsigned char c1 = tmppar->getChar(tmppos);
2369 unsigned char c2 = tmppar->getChar(tmppos - 1);
2371 // We should have an implementation that handles insets
2372 // as well, but that will have to come later. (Lgb)
2373 if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
2376 bool const erased = tmppar->erase(tmppos - 1, tmppos + 1);
2377 pos_type const ipos(erased ? tmppos - 1 : tmppos + 1);
2379 tmppar->insertChar(ipos, c1);
2380 tmppar->insertChar(ipos + 1, c2);
2382 checkParagraph(&bview, tmppar, tmppos);
2386 void LyXText::Delete(BufferView * bview)
2388 // this is a very easy implementation
2390 LyXCursor old_cursor = cursor;
2391 int const old_cur_par_id = old_cursor.par()->id();
2392 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2393 old_cursor.par()->previous()->id() : -1;
2395 // just move to the right
2398 // CHECK Look at the comment here.
2399 // This check is not very good...
2400 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2401 // and that can very well delete the par or par->previous in
2402 // old_cursor. Will a solution where we compare paragraph id's
2404 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : -1)
2405 == old_cur_par_prev_id
2406 && cursor.par()->id() != old_cur_par_id) {
2407 // delete-empty-paragraph-mechanism has done it
2411 // if you had success make a backspace
2412 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2413 LyXCursor tmpcursor = cursor;
2414 // to make sure undo gets the right cursor position
2415 cursor = old_cursor;
2416 setUndo(bview, Undo::DELETE,
2417 cursor.par(), cursor.par()->next());
2424 void LyXText::backspace(BufferView * bview)
2426 // Get the font that is used to calculate the baselineskip
2427 pos_type lastpos = cursor.par()->size();
2428 LyXFont rawparfont =
2429 cursor.par()->getFontSettings(bview->buffer()->params,
2432 if (cursor.pos() == 0) {
2433 // The cursor is at the beginning of a paragraph,
2434 // so the the backspace will collapse two paragraphs into one.
2436 // but it's not allowed unless it's new
2437 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2440 // we may paste some paragraphs
2442 // is it an empty paragraph?
2445 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2446 // This is an empty paragraph and we delete it just by moving the cursor one step
2447 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2448 // of the paragraph.
2450 if (cursor.par()->previous()) {
2451 Paragraph * tmppar = cursor.par()->previous();
2452 if (cursor.par()->layout() == tmppar->layout()
2453 && cursor.par()->getAlign() == tmppar->getAlign()) {
2454 // Inherit bottom DTD from the paragraph below.
2455 // (the one we are deleting)
2456 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2457 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2458 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2463 // the layout things can change the height of a row !
2464 int const tmpheight = cursor.row()->height();
2465 setHeightOfRow(bview, cursor.row());
2466 if (cursor.row()->height() != tmpheight) {
2467 postPaint(*bview, cursor.y() - cursor.row()->baseline());
2473 if (cursor.par()->previous()) {
2474 setUndo(bview, Undo::DELETE,
2475 cursor.par()->previous(), cursor.par()->next());
2478 Paragraph * tmppar = cursor.par();
2479 Row * tmprow = cursor.row();
2481 // We used to do cursorLeftIntern() here, but it is
2482 // not a good idea since it triggers the auto-delete
2483 // mechanism. So we do a cursorLeftIntern()-lite,
2484 // without the dreaded mechanism. (JMarc)
2485 if (cursor.par()->previous()) {
2486 // steps into the above paragraph.
2487 setCursorIntern(bview, cursor.par()->previous(),
2488 cursor.par()->previous()->size(),
2492 // Pasting is not allowed, if the paragraphs have different
2493 // layout. I think it is a real bug of all other
2494 // word processors to allow it. It confuses the user.
2495 // Even so with a footnote paragraph and a non-footnote
2496 // paragraph. I will not allow pasting in this case,
2497 // because the user would be confused if the footnote behaves
2498 // different wether it is open or closed.
2500 // Correction: Pasting is always allowed with standard-layout
2501 LyXTextClass const & tclass =
2502 bview->buffer()->params.getLyXTextClass();
2504 if (cursor.par() != tmppar
2505 && (cursor.par()->layout() == tmppar->layout()
2506 || tmppar->layout() == tclass.defaultLayout())
2507 && cursor.par()->getAlign() == tmppar->getAlign()) {
2508 removeParagraph(tmprow);
2510 mergeParagraph(bview->buffer()->params, bview->buffer()->paragraphs, cursor.par());
2512 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2513 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2514 // strangely enough it seems that commenting out the line above removes
2515 // most or all of the segfaults. I will however also try to move the
2516 // two Remove... lines in front of the PasteParagraph too.
2519 cursor.pos(cursor.pos() - 1);
2521 postPaint(*bview, cursor.y() - cursor.row()->baseline());
2523 // remove the lost paragraph
2524 // This one is not safe, since the paragraph that the tmprow and the
2525 // following rows belong to has been deleted by the PasteParagraph
2526 // above. The question is... could this be moved in front of the
2528 //RemoveParagraph(tmprow);
2529 //RemoveRow(tmprow);
2531 // This rebuilds the rows.
2532 appendParagraph(bview, cursor.row());
2533 updateCounters(bview);
2535 // the row may have changed, block, hfills etc.
2536 setCursor(bview, cursor.par(), cursor.pos(), false);
2539 // this is the code for a normal backspace, not pasting
2541 setUndo(bview, Undo::DELETE,
2542 cursor.par(), cursor.par()->next());
2543 // We used to do cursorLeftIntern() here, but it is
2544 // not a good idea since it triggers the auto-delete
2545 // mechanism. So we do a cursorLeftIntern()-lite,
2546 // without the dreaded mechanism. (JMarc)
2547 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2548 false, cursor.boundary());
2550 if (cursor.par()->isInset(cursor.pos())) {
2551 // force complete redo when erasing display insets
2552 // this is a cruel method but safe..... Matthias
2553 if (cursor.par()->getInset(cursor.pos())->display() ||
2554 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2555 cursor.par()->erase(cursor.pos());
2556 redoParagraph(bview);
2561 Row * row = cursor.row();
2562 int y = cursor.y() - row->baseline();
2564 // remember that a space at the end of a row doesnt count
2565 // when calculating the fill
2566 if (cursor.pos() < row->lastPos() ||
2567 !cursor.par()->isLineSeparator(cursor.pos())) {
2568 row->fill(row->fill() + singleWidth(bview,
2573 // some special code when deleting a newline. This is similar
2574 // to the behavior when pasting paragraphs
2575 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2576 cursor.par()->erase(cursor.pos());
2577 // refresh the positions
2579 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2580 tmprow = tmprow->next();
2581 tmprow->pos(tmprow->pos() - 1);
2583 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2584 cursor.pos(cursor.pos() - 1);
2586 if (cursor.pos() < cursor.par()->size()
2587 && !cursor.par()->isSeparator(cursor.pos())) {
2588 cursor.par()->insertChar(cursor.pos(), ' ');
2589 setCharFont(bview->buffer(), cursor.par(),
2590 cursor.pos(), current_font);
2591 // refresh the positions
2593 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2594 tmprow = tmprow->next();
2595 tmprow->pos(tmprow->pos() + 1);
2599 cursor.par()->erase(cursor.pos());
2601 // refresh the positions
2603 while (tmprow->next()
2604 && tmprow->next()->par() == row->par()) {
2605 tmprow = tmprow->next();
2606 tmprow->pos(tmprow->pos() - 1);
2609 // delete newlines at the beginning of paragraphs
2610 while (!cursor.par()->empty() &&
2611 cursor.par()->isNewline(cursor.pos()) &&
2612 cursor.pos() == cursor.par()->beginningOfBody()) {
2613 cursor.par()->erase(cursor.pos());
2614 // refresh the positions
2616 while (tmprow->next() &&
2617 tmprow->next()->par() == row->par()) {
2618 tmprow = tmprow->next();
2619 tmprow->pos(tmprow->pos() - 1);
2624 // is there a break one row above
2625 if (row->previous() && row->previous()->par() == row->par()) {
2626 z = rowBreakPoint(*bview, *row->previous());
2627 if (z >= row->pos()) {
2630 Row * tmprow = row->previous();
2632 // maybe the current row is now empty
2633 if (row->pos() >= row->par()->size()) {
2638 breakAgainOneRow(bview, row);
2639 if (row->next() && row->next()->par() == row->par())
2640 need_break_row = row->next();
2645 // set the dimensions of the row above
2646 y -= tmprow->height();
2647 tmprow->fill(fill(*bview, *tmprow, workWidth(*bview)));
2648 setHeightOfRow(bview, tmprow);
2650 postPaint(*bview, y);
2652 setCursor(bview, cursor.par(), cursor.pos(),
2653 false, cursor.boundary());
2654 //current_font = rawtmpfont;
2655 //real_current_font = realtmpfont;
2656 // check, whether the last character's font has changed.
2658 cursor.par()->getFontSettings(bview->buffer()->params,
2659 cursor.par()->size() - 1))
2660 redoHeightOfParagraph(bview);
2665 // break the cursor row again
2666 if (row->next() && row->next()->par() == row->par() &&
2667 (row->lastPos() == row->par()->size() - 1 ||
2668 rowBreakPoint(*bview, *row) != row->lastPos())) {
2670 // it can happen that a paragraph loses one row
2671 // without a real breakup. This is when a word
2672 // is to long to be broken. Well, I don t care this
2674 if (row->lastPos() == row->par()->size() - 1)
2675 removeRow(row->next());
2677 postPaint(*bview, y);
2679 breakAgainOneRow(bview, row);
2680 // will the cursor be in another row now?
2681 if (row->next() && row->next()->par() == row->par() &&
2682 row->lastPos() <= cursor.pos()) {
2684 breakAgainOneRow(bview, row);
2687 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2689 if (row->next() && row->next()->par() == row->par())
2690 need_break_row = row->next();
2694 // set the dimensions of the row
2695 row->fill(fill(*bview, *row, workWidth(*bview)));
2696 int const tmpheight = row->height();
2697 setHeightOfRow(bview, row);
2698 if (tmpheight == row->height()) {
2699 postRowPaint(*bview, row, y);
2701 postPaint(*bview, y);
2703 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2707 // current_font = rawtmpfont;
2708 // real_current_font = realtmpfont;
2710 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2711 != cursor.boundary())
2712 setCursor(bview, cursor.par(), cursor.pos(), false,
2713 !cursor.boundary());
2715 lastpos = cursor.par()->size();
2716 if (cursor.pos() == lastpos)
2717 setCurrentFont(bview);
2719 // check, whether the last characters font has changed.
2721 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2722 redoHeightOfParagraph(bview);
2724 // now the special right address boxes
2725 if (cursor.par()->layout()->margintype
2726 == MARGIN_RIGHT_ADDRESS_BOX) {
2727 redoDrawingOfParagraph(bview, cursor);
2733 // returns pointer to a specified row
2734 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
2739 Row * tmprow = firstrow;
2742 // find the first row of the specified paragraph
2743 while (tmprow->next() && tmprow->par() != par) {
2744 y += tmprow->height();
2745 tmprow = tmprow->next();
2748 // now find the wanted row
2749 while (tmprow->pos() < pos
2751 && tmprow->next()->par() == par
2752 && tmprow->next()->pos() <= pos) {
2753 y += tmprow->height();
2754 tmprow = tmprow->next();
2761 Row * LyXText::getRowNearY(int & y) const
2764 // If possible we should optimize this method. (Lgb)
2765 Row * tmprow = firstrow;
2768 while (tmprow->next() && tmpy + tmprow->height() <= y) {
2769 tmpy += tmprow->height();
2770 tmprow = tmprow->next();
2773 y = tmpy; // return the real y
2775 //lyxerr << "returned y = " << y << endl;
2779 // Search from the current cursor position.
2781 Row * tmprow = cursor.row();
2782 int tmpy = cursor.y() - tmprow->baseline();
2784 lyxerr << "cursor.y() = " << tmpy << endl;
2785 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
2786 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
2787 lyxerr << "first = " << first << endl;
2788 lyxerr << "y = " << y << endl;
2791 lyxerr << "up" << endl;
2793 tmpy -= tmprow->height();
2794 tmprow = tmprow->previous();
2795 } while (tmprow && tmpy - tmprow->height() >= y);
2796 } else if (y > tmpy) {
2797 lyxerr << "down" << endl;
2799 while (tmprow->next() && tmpy + tmprow->height() <= y) {
2800 tmpy += tmprow->height();
2801 tmprow = tmprow->next();
2804 lyxerr << "equal" << endl;
2807 y = tmpy; // return the real y
2809 lyxerr << "returned y = " << y << endl;
2817 int LyXText::getDepth() const
2819 return cursor.par()->getDepth();