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 BufferView * LyXText::bv()
63 lyx::Assert(bv_owner != 0);
68 BufferView * LyXText::bv() const
70 lyx::Assert(bv_owner != 0);
75 int LyXText::top_y() const
81 for (Row * row = firstrow; row && row != top_row_; row = row->next()) {
84 return y + top_row_offset_;
88 void LyXText::top_y(int newy)
92 lyxerr[Debug::GUI] << "setting top y = " << newy << endl;
95 top_row_ = getRowNearY(y);
96 top_row_offset_ = newy - y;
97 lyxerr[Debug::GUI] << "changing reference to row: " << top_row_
98 << " offset: " << top_row_offset_ << endl;
102 int LyXText::workWidth() const
105 // FIXME: pass (const ?) ref
106 return inset_owner->textWidth(bv());
108 return bv()->workWidth();
112 int LyXText::workWidth(Inset * inset) const
114 Paragraph * par = inset->parOwner();
117 pos_type pos = par->getPositionOfInset(inset);
118 lyx::Assert(pos != -1);
120 LyXLayout_ptr const & layout = par->layout();
122 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
123 // Optimization here: in most cases, the real row is
124 // not needed, but only the par/pos values. So we just
125 // construct a dummy row for leftMargin. (JMarc)
129 return workWidth() - leftMargin(&dummyrow);
132 Row * row = getRow(par, pos, dummy_y);
134 while (frow->previous() && frow->par() == frow->previous()->par())
135 frow = frow->previous();
137 // FIXME: I don't understand this code - jbl
139 unsigned int maxw = 0;
140 while (!frow->isParEnd()) {
141 if ((frow != row) && (maxw < frow->width()))
142 maxw = frow->width();
153 int LyXText::getRealCursorX() const
156 if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
157 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
162 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
165 if (!Encodings::is_arabic(c))
166 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
167 return c + (0xb0 - '0');
171 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
172 unsigned char next_char = ' ';
174 for (pos_type i = pos+1; i < par->size(); ++i)
175 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
176 next_char = par->getChar(i);
180 if (Encodings::is_arabic(next_char)) {
181 if (Encodings::is_arabic(prev_char) &&
182 !Encodings::is_arabic_special(prev_char))
183 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
185 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
187 if (Encodings::is_arabic(prev_char) &&
188 !Encodings::is_arabic_special(prev_char))
189 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
191 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
195 // This is the comments that some of the warnings below refers to.
196 // There are some issues in this file and I don't think they are
197 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
198 // this is a problem that has been here almost from day one and that a
199 // larger userbase with differenct access patters triggers the bad
200 // behaviour. (segfaults.) What I think happen is: In several places
201 // we store the paragraph in the current cursor and then moves the
202 // cursor. This movement of the cursor will delete paragraph at the
203 // old position if it is now empty. This will make the temporary
204 // pointer to the old cursor paragraph invalid and dangerous to use.
205 // And is some cases this will trigger a segfault. I have marked some
206 // of the cases where this happens with a warning, but I am sure there
207 // are others in this file and in text2.C. There is also a note in
208 // Delete() that you should read. In Delete I store the paragraph->id
209 // instead of a pointer to the paragraph. I am pretty sure this faulty
210 // use of temporary pointers to paragraphs that might have gotten
211 // invalidated (through a cursor movement) before they are used, are
212 // the cause of the strange crashes we get reported often.
214 // It is very tiresom to change this code, especially when it is as
215 // hard to read as it is. Help to fix all the cases where this is done
216 // would be greately appreciated.
220 int LyXText::singleWidth(Paragraph * par,
223 if (pos >= par->size())
226 char const c = par->getChar(pos);
227 return singleWidth(par, pos, c);
231 int LyXText::singleWidth(Paragraph * par,
232 pos_type pos, char c) const
234 if (pos >= par->size())
237 LyXFont const font = getFont(bv()->buffer(), par, pos);
239 // The most common case is handled first (Asger)
240 if (IsPrintable(c)) {
241 if (font.language()->RightToLeft()) {
242 if (font.language()->lang() == "arabic" &&
243 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
244 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
245 if (Encodings::IsComposeChar_arabic(c))
248 c = transformChar(c, par, pos);
249 } else if (font.language()->lang() == "hebrew" &&
250 Encodings::IsComposeChar_hebrew(c))
253 return font_metrics::width(c, font);
257 if (c == Paragraph::META_INSET) {
258 Inset * tmpinset = par->getInset(pos);
260 if (tmpinset->lyxCode() == Inset::HFILL_CODE) {
261 // Because of the representation as vertical lines
265 // this IS needed otherwise on initialitation we don't get the fill
266 // of the row right (ONLY on initialization if we read a file!)
267 // should be changed! (Jug 20011204)
268 tmpinset->update(bv(), font);
270 return tmpinset->width(bv(), font);
275 if (IsSeparatorChar(c))
277 return font_metrics::width(c, font);
281 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
283 bidi_same_direction = true;
284 if (!lyxrc.rtl_support) {
289 Inset * inset = row->par()->inInset();
290 if (inset && inset->owner() &&
291 inset->owner()->lyxCode() == Inset::ERT_CODE) {
296 bidi_start = row->pos();
297 bidi_end = row->lastPrintablePos();
299 if (bidi_start > bidi_end) {
304 if (bidi_end + 2 - bidi_start >
305 static_cast<pos_type>(log2vis_list.size())) {
307 (bidi_end + 2 - bidi_start < 500) ?
308 500 : 2 * (bidi_end + 2 - bidi_start);
309 log2vis_list.resize(new_size);
310 vis2log_list.resize(new_size);
311 bidi_levels.resize(new_size);
314 vis2log_list[bidi_end + 1 - bidi_start] = -1;
315 log2vis_list[bidi_end + 1 - bidi_start] = -1;
319 row->par()->isRightToLeftPar(buf->params);
323 pos_type const body_pos = row->par()->beginningOfBody();
325 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
326 bool is_space = row->par()->isLineSeparator(lpos);
328 (is_space && lpos + 1 <= bidi_end &&
329 !row->par()->isLineSeparator(lpos + 1) &&
330 !row->par()->isNewline(lpos + 1))
332 LyXFont font = row->par()->getFontSettings(buf->params, pos);
333 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
334 font.number() == LyXFont::ON &&
335 row->par()->getFontSettings(buf->params, lpos - 1).number()
337 font = row->par()->getFontSettings(buf->params, lpos);
342 bool new_rtl = font.isVisibleRightToLeft();
343 bool new_rtl0 = font.isRightToLeft();
346 if (lpos == body_pos - 1
347 && row->pos() < body_pos - 1
349 new_level = (rtl_par) ? 1 : 0;
350 new_rtl = new_rtl0 = rtl_par;
352 new_level = (new_rtl) ? 1 : 2;
354 new_level = (rtl_par) ? 2 : 0;
356 if (is_space && new_level >= level) {
362 int new_level2 = new_level;
364 if (level == new_level && rtl0 != new_rtl0) {
366 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
367 } else if (level < new_level) {
368 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
369 if (new_level > rtl_par)
370 bidi_same_direction = false;
372 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
375 bidi_levels[lpos - bidi_start] = new_level;
377 while (level > new_level2) {
378 pos_type old_lpos = stack[--level];
379 int delta = lpos - old_lpos - 1;
382 log2vis_list[lpos - bidi_start] += delta;
383 log2vis_list[old_lpos - bidi_start] += delta;
385 while (level < new_level)
386 stack[level++] = lpos;
390 pos_type const old_lpos = stack[--level];
391 int delta = bidi_end - old_lpos;
394 log2vis_list[old_lpos - bidi_start] += delta;
397 pos_type vpos = bidi_start - 1;
398 for (pos_type lpos = bidi_start;
399 lpos <= bidi_end; ++lpos) {
400 vpos += log2vis_list[lpos - bidi_start];
401 vis2log_list[vpos - bidi_start] = lpos;
402 log2vis_list[lpos - bidi_start] = vpos;
407 // This method requires a previous call to ComputeBidiTables()
408 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
411 if (!lyxrc.rtl_support || pos == 0)
414 if (!bidi_InRange(pos - 1)) {
415 /// This can happen if pos is the first char of a row.
416 /// Returning false in this case is incorrect!
420 bool const rtl = bidi_level(pos - 1) % 2;
421 bool const rtl2 = bidi_InRange(pos)
422 ? bidi_level(pos) % 2
423 : par->isRightToLeftPar(buf->params);
428 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
429 pos_type pos, LyXFont const & font) const
431 if (!lyxrc.rtl_support)
432 return false; // This is just for speedup
434 bool const rtl = font.isVisibleRightToLeft();
435 bool const rtl2 = bidi_InRange(pos)
436 ? bidi_level(pos) % 2
437 : par->isRightToLeftPar(buf->params);
442 int LyXText::leftMargin(Row const * row) const
446 if (row->pos() < row->par()->size())
447 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
448 (ins = row->par()->getInset(row->pos())) &&
449 (ins->needFullRow() || ins->display()))
452 LyXTextClass const & tclass =
453 bv()->buffer()->params.getLyXTextClass();
454 LyXLayout_ptr const & layout = row->par()->layout();
456 string parindent = layout->parindent;
460 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
462 // this is the way, LyX handles the LaTeX-Environments.
463 // I have had this idea very late, so it seems to be a
464 // later added hack and this is true
465 if (!row->par()->getDepth()) {
466 if (row->par()->layout() == tclass.defaultLayout()) {
467 // find the previous same level paragraph
468 if (row->par()->previous()) {
469 Paragraph * newpar = row->par()
470 ->depthHook(row->par()->getDepth());
472 newpar->layout()->nextnoindent)
477 // find the next level paragraph
479 Paragraph * newpar = row->par()->outerHook();
481 // make a corresponding row. Needed to call LeftMargin()
483 // check wether it is a sufficent paragraph
484 if (newpar && newpar->layout()->isEnvironment()) {
486 dummyrow.par(newpar);
487 dummyrow.pos(newpar->size());
488 x = leftMargin(&dummyrow);
490 // this is no longer an error, because this function
491 // is used to clear impossible depths after changing
492 // a layout. Since there is always a redo,
493 // LeftMargin() is always called
494 row->par()->params().depth(0);
497 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
498 if (newpar->params().noindent())
501 parindent = newpar->layout()->parindent;
507 LyXFont const labelfont = getLabelFont(bv()->buffer(), row->par());
508 switch (layout->margintype) {
510 if (!layout->leftmargin.empty()) {
511 x += font_metrics::signedWidth(layout->leftmargin,
512 tclass.defaultfont());
514 if (!row->par()->getLabelstring().empty()) {
515 x += font_metrics::signedWidth(layout->labelindent,
517 x += font_metrics::width(row->par()->getLabelstring(),
519 x += font_metrics::width(layout->labelsep, labelfont);
523 x += font_metrics::signedWidth(layout->labelindent, labelfont);
524 // The width of an empty par, even with manual label, should be 0
525 if (!row->par()->empty() && row->pos() >= row->par()->beginningOfBody()) {
526 if (!row->par()->getLabelWidthString().empty()) {
527 x += font_metrics::width(row->par()->getLabelWidthString(),
529 x += font_metrics::width(layout->labelsep, labelfont);
534 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
535 / (row->par()->getDepth() + 4);
537 case MARGIN_FIRST_DYNAMIC:
538 if (layout->labeltype == LABEL_MANUAL) {
539 if (row->pos() >= row->par()->beginningOfBody()) {
540 x += font_metrics::signedWidth(layout->leftmargin,
543 x += font_metrics::signedWidth(layout->labelindent,
546 } else if (row->pos()
547 // Special case to fix problems with
549 || (layout->labeltype == LABEL_STATIC
550 && layout->latextype == LATEX_ENVIRONMENT
551 && ! row->par()->isFirstInSequence())) {
552 x += font_metrics::signedWidth(layout->leftmargin,
554 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
555 && layout->labeltype != LABEL_BIBLIO
556 && layout->labeltype !=
557 LABEL_CENTERED_TOP_ENVIRONMENT) {
558 x += font_metrics::signedWidth(layout->labelindent,
560 x += font_metrics::width(layout->labelsep, labelfont);
561 x += font_metrics::width(row->par()->getLabelstring(),
566 case MARGIN_RIGHT_ADDRESS_BOX:
568 // ok, a terrible hack. The left margin depends on the widest
569 // row in this paragraph. Do not care about footnotes, they
570 // are *NOT* allowed in the LaTeX realisation of this layout.
572 // find the first row of this paragraph
573 Row const * tmprow = row;
574 while (tmprow->previous()
575 && tmprow->previous()->par() == row->par())
576 tmprow = tmprow->previous();
578 int minfill = tmprow->fill();
579 while (tmprow->next() && tmprow->next()->par() == row->par()) {
580 tmprow = tmprow->next();
581 if (tmprow->fill() < minfill)
582 minfill = tmprow->fill();
585 x += font_metrics::signedWidth(layout->leftmargin,
586 tclass.defaultfont());
592 if ((workWidth() > 0) &&
593 !row->par()->params().leftIndent().zero())
595 LyXLength const len = row->par()->params().leftIndent();
596 int const tw = inset_owner ?
597 inset_owner->latexTextWidth(bv()) : workWidth();
598 x += len.inPixels(tw);
601 LyXAlignment align; // wrong type
603 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
604 align = layout->align;
606 align = row->par()->params().align();
608 // set the correct parindent
609 if (row->pos() == 0) {
610 if ((layout->labeltype == LABEL_NO_LABEL
611 || layout->labeltype == LABEL_TOP_ENVIRONMENT
612 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
613 || (layout->labeltype == LABEL_STATIC
614 && layout->latextype == LATEX_ENVIRONMENT
615 && ! row->par()->isFirstInSequence()))
616 && align == LYX_ALIGN_BLOCK
617 && !row->par()->params().noindent()
618 // in tabulars and ert paragraphs are never indented!
619 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
620 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
621 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
622 && (row->par()->layout() != tclass.defaultLayout() ||
623 bv()->buffer()->params.paragraph_separation ==
624 BufferParams::PARSEP_INDENT)) {
625 x += font_metrics::signedWidth(parindent,
626 tclass.defaultfont());
627 } else if (layout->labeltype == LABEL_BIBLIO) {
628 // ale970405 Right width for bibitems
629 x += bibitemMaxWidth(bv(), tclass.defaultfont());
637 int LyXText::rightMargin(Buffer const & buf, Row const & row) const
641 if (row.pos() < row.par()->size())
642 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
643 (ins=row.par()->getInset(row.pos())) &&
644 (ins->needFullRow() || ins->display()))
647 LyXTextClass const & tclass = buf.params.getLyXTextClass();
648 LyXLayout_ptr const & layout = row.par()->layout();
651 + font_metrics::signedWidth(tclass.rightmargin(),
652 tclass.defaultfont());
654 // this is the way, LyX handles the LaTeX-Environments.
655 // I have had this idea very late, so it seems to be a
656 // later added hack and this is true
657 if (row.par()->getDepth()) {
658 // find the next level paragraph
660 Paragraph const * newpar = row.par();
663 newpar = newpar->previous();
665 && newpar->getDepth() >= row.par()->getDepth());
667 // make a corresponding row. Needed to call LeftMargin()
669 // check wether it is a sufficent paragraph
670 if (newpar && newpar->layout()->isEnvironment()) {
672 dummyrow.par(const_cast<Paragraph *>(newpar));
674 x = rightMargin(buf, dummyrow);
676 // this is no longer an error, because this function
677 // is used to clear impossible depths after changing
678 // a layout. Since there is always a redo,
679 // LeftMargin() is always called
680 row.par()->params().depth(0);
684 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
685 x += font_metrics::signedWidth(layout->rightmargin,
686 tclass.defaultfont())
687 * 4 / (row.par()->getDepth() + 4);
692 int LyXText::labelEnd(Row const & row) const
694 if (row.par()->layout()->margintype == MARGIN_MANUAL) {
696 tmprow.pos(row.par()->size());
697 // return the beginning of the body
698 return leftMargin(&tmprow);
701 // LabelEnd is only needed if the layout
702 // fills a flushleft label.
709 // this needs special handling - only newlines count as a break point
710 pos_type addressBreakPoint(pos_type i, Paragraph * par)
712 for (; i < par->size(); ++i) {
713 if (par->isNewline(i)) {
715 } else if (par->isInset(i) && par->getInset(i)->display()) {
716 // FIXME: what are we doing modifying stuff here !
717 par->getInset(i)->display(false);
728 LyXText::rowBreakPoint(Row const & row) const
730 Paragraph * par = row.par();
732 // maximum pixel width of a row.
733 int width = workWidth() - rightMargin(*bv()->buffer(), row);
735 // inset->textWidth() returns -1 via workWidth(),
740 LyXLayout_ptr const & layout = par->layout();
742 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
743 return addressBreakPoint(row.pos(), par);
745 pos_type const pos = row.pos();
746 pos_type const body_pos = par->beginningOfBody();
747 pos_type const last = par->size();
748 pos_type point = last;
753 // Now we iterate through until we reach the right margin
754 // or the end of the par, then choose the possible break
757 int const left = leftMargin(&row);
760 // pixel width since last breakpoint
764 for (; i < last; ++i) {
766 if (par->isNewline(i)) {
771 char const c = par->getChar(i);
773 int thiswidth = singleWidth(par, i, c);
775 // add the auto-hfill from label end to the body
776 if (body_pos && i == body_pos) {
777 thiswidth += font_metrics::width(layout->labelsep,
778 getLabelFont(bv()->buffer(), par));
779 if (par->isLineSeparator(i - 1))
780 thiswidth -= singleWidth(par, i - 1);
781 int left_margin = labelEnd(row);
782 if (thiswidth < left_margin)
783 thiswidth = left_margin;
787 chunkwidth += thiswidth;
789 Inset * in = par->isInset(i) ? par->getInset(i) : 0;
790 bool display = (in && (in->display() || in->needFullRow()));
792 // check whether a Display() inset is valid here.
793 // If not, change it to non-display. FIXME:
794 // we should not be modifying things at this
796 if (in && in->display() && (layout->isCommand() ||
797 (layout->labeltype == LABEL_MANUAL && i < body_pos)))
800 // break before a character that will fall off
801 // the right of the row
803 // if no break before or we are at an inset
804 // that will take up a row, break here
805 if (point == last || display || chunkwidth >= (width - left)) {
814 if (!in || in->isChar()) {
815 // some insets are line separators too
816 if (par->isLineSeparator(i)) {
826 // full row insets start at a new row
828 if (pos < last - 1) {
830 if (par->isLineSeparator(i + 1))
833 // to avoid extra rows
842 if (point == last && x >= width) {
843 // didn't find one, break at the point we reached the edge
845 } else if (i == last && x < width) {
846 // found one, but we fell off the end of the par, so prefer
851 // manual labels cannot be broken in LaTeX
852 if (body_pos && point < body_pos)
853 point = body_pos - 1;
859 // returns the minimum space a row needs on the screen in pixel
860 int LyXText::fill(Row & row, int paper_width) const
866 // get the pure distance
867 pos_type const last = row.lastPrintablePos();
869 // special handling of the right address boxes
870 if (row.par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
871 int const tmpfill = row.fill();
872 row.fill(0); // the minfill in MarginLeft()
873 w = leftMargin(&row);
876 w = leftMargin(&row);
878 Paragraph * par = row.par();
879 LyXLayout_ptr const & layout = par->layout();
881 pos_type const body_pos = par->beginningOfBody();
882 pos_type i = row.pos();
885 if (body_pos > 0 && i == body_pos) {
886 w += font_metrics::width(layout->labelsep, getLabelFont(bv()->buffer(), par));
887 if (par->isLineSeparator(i - 1))
888 w -= singleWidth(par, i - 1);
889 int left_margin = labelEnd(row);
893 w += singleWidth(par, i);
896 if (body_pos > 0 && body_pos > last) {
897 w += font_metrics::width(layout->labelsep, getLabelFont(bv()->buffer(), par));
898 if (last >= 0 && par->isLineSeparator(last))
899 w -= singleWidth(par, last);
900 int const left_margin = labelEnd(row);
905 int const fill = paper_width - w - rightMargin(*bv()->buffer(), row);
910 // returns the minimum space a manual label needs on the screen in pixel
911 int LyXText::labelFill(Row const & row) const
913 pos_type last = row.par()->beginningOfBody();
915 lyx::Assert(last > 0);
917 // -1 because a label ends either with a space that is in the label,
918 // or with the beginning of a footnote that is outside the label.
921 // a separator at this end does not count
922 if (row.par()->isLineSeparator(last))
926 pos_type i = row.pos();
928 w += singleWidth(row.par(), i);
933 string const & labwidstr = row.par()->params().labelWidthString();
934 if (!labwidstr.empty()) {
935 LyXFont const labfont = getLabelFont(bv()->buffer(), row.par());
936 int const labwidth = font_metrics::width(labwidstr, labfont);
937 fill = max(labwidth - w, 0);
944 LColor::color LyXText::backgroundColor() const
947 return inset_owner->backgroundColor();
949 return LColor::background;
952 void LyXText::setHeightOfRow(Row * row) const
954 // get the maximum ascent and the maximum descent
958 float layoutdesc = 0;
961 Inset * tmpinset = 0;
963 // ok , let us initialize the maxasc and maxdesc value.
964 // This depends in LaTeX of the font of the last character
965 // in the paragraph. The hack below is necessary because
966 // of the possibility of open footnotes
968 // Correction: only the fontsize count. The other properties
969 // are taken from the layoutfont. Nicer on the screen :)
970 Paragraph * par = row->par();
971 Paragraph * firstpar = row->par();
973 LyXLayout_ptr const & layout = firstpar->layout();
975 // as max get the first character of this row then it can increase but not
976 // decrease the height. Just some point to start with so we don't have to
977 // do the assignment below too often.
978 LyXFont font = getFont(bv()->buffer(), par, row->pos());
979 LyXFont::FONT_SIZE const tmpsize = font.size();
980 font = getLayoutFont(bv()->buffer(), par);
981 LyXFont::FONT_SIZE const size = font.size();
982 font.setSize(tmpsize);
984 LyXFont labelfont = getLabelFont(bv()->buffer(), par);
986 float spacing_val = 1.0;
987 if (!row->par()->params().spacing().isDefault()) {
988 spacing_val = row->par()->params().spacing().getValue();
990 spacing_val = bv()->buffer()->params.spacing.getValue();
992 //lyxerr << "spacing_val = " << spacing_val << endl;
994 int maxasc = int(font_metrics::maxAscent(font) *
995 layout->spacing.getValue() *
997 int maxdesc = int(font_metrics::maxDescent(font) *
998 layout->spacing.getValue() *
1001 pos_type const pos_end = row->lastPos();
1005 if (!row->par()->empty()) {
1006 // Check if any insets are larger
1007 for (pos_type pos = row->pos(); pos <= pos_end; ++pos) {
1008 if (row->par()->isInset(pos)) {
1009 tmpfont = getFont(bv()->buffer(), row->par(), pos);
1010 tmpinset = row->par()->getInset(pos);
1012 #if 1 // this is needed for deep update on initialitation
1013 tmpinset->update(bv(), tmpfont);
1015 asc = tmpinset->ascent(bv(), tmpfont);
1016 desc = tmpinset->descent(bv(), tmpfont);
1017 maxwidth += tmpinset->width(bv(), tmpfont);
1018 maxasc = max(maxasc, asc);
1019 maxdesc = max(maxdesc, desc);
1022 maxwidth += singleWidth(row->par(), pos);
1027 // Check if any custom fonts are larger (Asger)
1028 // This is not completely correct, but we can live with the small,
1029 // cosmetic error for now.
1030 LyXFont::FONT_SIZE maxsize =
1031 row->par()->highestFontInRange(row->pos(), pos_end, size);
1032 if (maxsize > font.size()) {
1033 font.setSize(maxsize);
1035 asc = font_metrics::maxAscent(font);
1036 desc = font_metrics::maxDescent(font);
1043 // This is nicer with box insets:
1047 row->ascent_of_text(maxasc);
1049 // is it a top line?
1050 if (!row->pos() && (row->par() == firstpar)) {
1052 // some parksips VERY EASY IMPLEMENTATION
1053 if (bv()->buffer()->params.paragraph_separation ==
1054 BufferParams::PARSEP_SKIP)
1056 if (layout->isParagraph()
1057 && firstpar->getDepth() == 0
1058 && firstpar->previous())
1060 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1061 } else if (firstpar->previous() &&
1062 firstpar->previous()->layout()->isParagraph() &&
1063 firstpar->previous()->getDepth() == 0)
1065 // is it right to use defskip here too? (AS)
1066 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1071 if (!row->par()->previous() && !isInInset())
1072 maxasc += PAPER_MARGIN;
1074 // add the vertical spaces, that the user added
1075 maxasc += getLengthMarkerHeight(*bv(), firstpar->params().spaceTop());
1077 // do not forget the DTP-lines!
1078 // there height depends on the font of the nearest character
1079 if (firstpar->params().lineTop())
1081 maxasc += 2 * font_metrics::ascent('x', getFont(bv()->buffer(),
1083 // and now the pagebreaks
1084 if (firstpar->params().pagebreakTop())
1085 maxasc += 3 * defaultRowHeight();
1087 if (firstpar->params().startOfAppendix())
1088 maxasc += 3 * defaultRowHeight();
1090 // This is special code for the chapter, since the label of this
1091 // layout is printed in an extra row
1092 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1093 && bv()->buffer()->params.secnumdepth >= 0)
1095 float spacing_val = 1.0;
1096 if (!row->par()->params().spacing().isDefault()) {
1097 spacing_val = row->par()->params().spacing().getValue();
1099 spacing_val = bv()->buffer()->params.spacing.getValue();
1102 labeladdon = int(font_metrics::maxDescent(labelfont) *
1103 layout->spacing.getValue() *
1105 + int(font_metrics::maxAscent(labelfont) *
1106 layout->spacing.getValue() *
1110 // special code for the top label
1111 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1112 || layout->labeltype == LABEL_BIBLIO
1113 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1114 && row->par()->isFirstInSequence()
1115 && !row->par()->getLabelstring().empty())
1117 float spacing_val = 1.0;
1118 if (!row->par()->params().spacing().isDefault()) {
1119 spacing_val = row->par()->params().spacing().getValue();
1121 spacing_val = bv()->buffer()->params.spacing.getValue();
1125 (font_metrics::maxAscent(labelfont) *
1126 layout->spacing.getValue() *
1128 +(font_metrics::maxDescent(labelfont) *
1129 layout->spacing.getValue() *
1131 + layout->topsep * defaultRowHeight()
1132 + layout->labelbottomsep * defaultRowHeight());
1135 // and now the layout spaces, for example before and after a section,
1136 // or between the items of a itemize or enumerate environment
1138 if (!firstpar->params().pagebreakTop()) {
1139 Paragraph * prev = row->par()->previous();
1141 prev = row->par()->depthHook(row->par()->getDepth());
1142 if (prev && prev->layout() == firstpar->layout() &&
1143 prev->getDepth() == firstpar->getDepth() &&
1144 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1146 layoutasc = (layout->itemsep * defaultRowHeight());
1147 } else if (row->previous()) {
1148 tmptop = layout->topsep;
1150 if (row->previous()->par()->getDepth() >= row->par()->getDepth())
1151 tmptop -= row->previous()->par()->layout()->bottomsep;
1154 layoutasc = (tmptop * defaultRowHeight());
1155 } else if (row->par()->params().lineTop()) {
1156 tmptop = layout->topsep;
1159 layoutasc = (tmptop * defaultRowHeight());
1162 prev = row->par()->outerHook();
1164 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1166 if (firstpar->previous() &&
1167 firstpar->previous()->getDepth() == 0 &&
1168 firstpar->previous()->layout() !=
1172 } else if (firstpar->previous()) {
1173 maxasc += int(layout->parsep * defaultRowHeight());
1179 // is it a bottom line?
1180 if (row->par() == par
1181 && (!row->next() || row->next()->par() != row->par())) {
1182 // the bottom margin
1183 if (!par->next() && !isInInset())
1184 maxdesc += PAPER_MARGIN;
1186 // add the vertical spaces, that the user added
1187 maxdesc += getLengthMarkerHeight(*bv(), firstpar->params().spaceBottom());
1189 // do not forget the DTP-lines!
1190 // there height depends on the font of the nearest character
1191 if (firstpar->params().lineBottom())
1192 maxdesc += 2 * font_metrics::ascent('x',
1193 getFont(bv()->buffer(),
1195 max(pos_type(0), par->size() - 1)));
1197 // and now the pagebreaks
1198 if (firstpar->params().pagebreakBottom())
1199 maxdesc += 3 * defaultRowHeight();
1201 // and now the layout spaces, for example before and after
1202 // a section, or between the items of a itemize or enumerate
1204 if (!firstpar->params().pagebreakBottom()
1205 && row->par()->next()) {
1206 Paragraph * nextpar = row->par()->next();
1207 Paragraph * comparepar = row->par();
1211 if (comparepar->getDepth() > nextpar->getDepth()) {
1212 usual = (comparepar->layout()->bottomsep * defaultRowHeight());
1213 comparepar = comparepar->depthHook(nextpar->getDepth());
1214 if (comparepar->layout()!= nextpar->layout()
1215 || nextpar->getLabelWidthString() !=
1216 comparepar->getLabelWidthString())
1218 unusual = (comparepar->layout()->bottomsep * defaultRowHeight());
1220 if (unusual > usual)
1221 layoutdesc = unusual;
1224 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1226 if (comparepar->layout() != nextpar->layout()
1227 || nextpar->getLabelWidthString() !=
1228 comparepar->getLabelWidthString())
1229 layoutdesc = int(comparepar->layout()->bottomsep * defaultRowHeight());
1234 // incalculate the layout spaces
1235 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1236 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1238 // calculate the new height of the text
1239 height -= row->height();
1241 row->height(maxasc + maxdesc + labeladdon);
1242 row->baseline(maxasc + labeladdon);
1244 height += row->height();
1246 row->top_of_text(row->baseline() - font_metrics::maxAscent(font));
1249 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1252 row->width(maxwidth);
1253 prepareToPrint(row, x, dummy, dummy, dummy, false);
1255 row->width(int(maxwidth + x));
1258 width = max(0, workWidth());
1260 if (r->width() > width)
1268 // Appends the implicit specified paragraph before the specified row,
1269 // start at the implicit given position
1270 void LyXText::appendParagraph(Row * row) const
1272 pos_type const last = row->par()->size();
1276 pos_type z = rowBreakPoint(*row);
1282 insertRow(row, row->par(), z);
1289 // Set the dimensions of the row
1290 // fixed fill setting now by calling inset->update() in
1291 // SingleWidth when needed!
1292 tmprow->fill(fill(*tmprow, workWidth()));
1293 setHeightOfRow(tmprow);
1299 // Do we even need this at all ? Code that uses RowPainter *already*
1300 // sets need_break_row when it sees a CHANGED_IN_DRAW, though not
1302 void LyXText::markChangeInDraw(Row * row, Row * prev)
1304 if (prev && prev->par() == row->par()) {
1305 breakAgainOneRow(prev);
1306 if (prev->next() != row) {
1307 // breakAgainOneRow() has removed row_
1308 need_break_row = prev;
1310 need_break_row = row;
1313 need_break_row = firstrow;
1315 need_break_row = prev->next();
1317 setCursor(cursor.par(), cursor.pos());
1322 void LyXText::breakAgain(Row * row) const
1324 bool not_ready = true;
1327 pos_type z = rowBreakPoint(*row);
1330 if (z < row->par()->size()) {
1331 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1334 insertRow(row, row->par(), z);
1340 if (row->pos() == z)
1341 not_ready = false; // the rest will not change
1347 // if there are some rows too much, delete them
1348 // only if you broke the whole paragraph!
1349 Row * tmprow2 = row;
1350 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1351 tmprow2 = tmprow2->next();
1353 while (tmprow2 != row) {
1354 tmprow2 = tmprow2->previous();
1355 removeRow(tmprow2->next());
1360 // set the dimensions of the row
1361 tmprow->fill(fill(*tmprow, workWidth()));
1362 setHeightOfRow(tmprow);
1363 } while (not_ready);
1367 // this is just a little changed version of break again
1368 void LyXText::breakAgainOneRow(Row * row)
1370 pos_type z = rowBreakPoint(*row);
1373 if (z < row->par()->size()) {
1375 || (row->next() && row->next()->par() != row->par())) {
1378 insertRow(row, row->par(), z);
1384 if (row->pos() != z)
1388 // if there are some rows too much, delete them
1389 // only if you broke the whole paragraph!
1390 Row * tmprow2 = row;
1391 while (tmprow2->next()
1392 && tmprow2->next()->par() == row->par()) {
1393 tmprow2 = tmprow2->next();
1395 while (tmprow2 != row) {
1396 tmprow2 = tmprow2->previous();
1397 removeRow(tmprow2->next());
1401 // set the dimensions of the row
1402 tmprow->fill(fill(*tmprow, workWidth()));
1403 setHeightOfRow(tmprow);
1407 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1409 // allow only if at start or end, or all previous is new text
1410 if (cursor.pos() && cursor.pos() != cursor.par()->size()
1411 && cursor.par()->isChangeEdited(0, cursor.pos()))
1414 LyXTextClass const & tclass =
1415 bv()->buffer()->params.getLyXTextClass();
1416 LyXLayout_ptr const & layout = cursor.par()->layout();
1418 // this is only allowed, if the current paragraph is not empty or caption
1419 // and if it has not the keepempty flag aktive
1420 if (cursor.par()->empty()
1421 && layout->labeltype != LABEL_SENSITIVE
1422 && !layout->keepempty)
1425 setUndo(bv(), Undo::FINISH, cursor.par(), cursor.par()->next());
1427 // Always break behind a space
1429 // It is better to erase the space (Dekel)
1430 if (cursor.pos() < cursor.par()->size()
1431 && cursor.par()->isLineSeparator(cursor.pos()))
1432 cursor.par()->erase(cursor.pos());
1433 // cursor.pos(cursor.pos() + 1);
1435 // break the paragraph
1439 keep_layout = layout->isEnvironment();
1441 // we need to set this before we insert the paragraph. IMO the
1442 // breakParagraph call should return a bool if it inserts the
1443 // paragraph before or behind and we should react on that one
1444 // but we can fix this in 1.3.0 (Jug 20020509)
1445 bool const isempty = (layout->keepempty && cursor.par()->empty());
1446 ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(), cursor.pos(),
1449 // well this is the caption hack since one caption is really enough
1450 if (layout->labeltype == LABEL_SENSITIVE) {
1452 // set to standard-layout
1453 cursor.par()->applyLayout(tclass.defaultLayout());
1455 // set to standard-layout
1456 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1459 // if the cursor is at the beginning of a row without prior newline,
1461 // This touches only the screen-update. Otherwise we would may have
1462 // an empty row on the screen
1463 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1464 && cursor.row()->pos() == cursor.pos())
1469 int y = cursor.y() - cursor.row()->baseline();
1471 // Do not forget the special right address boxes
1472 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1473 Row * r = cursor.row();
1474 while (r->previous() && r->previous()->par() == r->par()) {
1482 removeParagraph(cursor.row());
1484 // set the dimensions of the cursor row
1485 cursor.row()->fill(fill(*cursor.row(), workWidth()));
1487 setHeightOfRow(cursor.row());
1489 #warning Trouble Point! (Lgb)
1490 // When ::breakParagraph is called from within an inset we must
1491 // ensure that the correct ParagraphList is used. Today that is not
1492 // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1493 while (!cursor.par()->next()->empty()
1494 && cursor.par()->next()->isNewline(0))
1495 cursor.par()->next()->erase(0);
1497 insertParagraph(cursor.par()->next(), cursor.row());
1501 // This check is necessary. Otherwise the new empty paragraph will
1502 // be deleted automatically. And it is more friendly for the user!
1503 if (cursor.pos() || isempty)
1504 setCursor(cursor.par()->next(), 0);
1506 setCursor(cursor.par(), 0);
1508 if (cursor.row()->next())
1509 breakAgain(cursor.row()->next());
1515 // Just a macro to make some thing easier.
1516 void LyXText::redoParagraph() const
1519 redoParagraphs(cursor, cursor.par()->next());
1520 setCursorIntern(cursor.par(), cursor.pos());
1524 // insert a character, moves all the following breaks in the
1525 // same Paragraph one to the right and make a rebreak
1526 void LyXText::insertChar(char c)
1528 setUndo(bv(), Undo::INSERT, cursor.par(), cursor.par()->next());
1530 // When the free-spacing option is set for the current layout,
1531 // disable the double-space checking
1533 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1534 cursor.row()->par()->isFreeSpacing();
1536 if (lyxrc.auto_number) {
1537 static string const number_operators = "+-/*";
1538 static string const number_unary_operators = "+-";
1539 static string const number_seperators = ".,:";
1541 if (current_font.number() == LyXFont::ON) {
1542 if (!IsDigit(c) && !contains(number_operators, c) &&
1543 !(contains(number_seperators, c) &&
1544 cursor.pos() >= 1 &&
1545 cursor.pos() < cursor.par()->size() &&
1546 getFont(bv()->buffer(),
1548 cursor.pos()).number() == LyXFont::ON &&
1549 getFont(bv()->buffer(),
1551 cursor.pos() - 1).number() == LyXFont::ON)
1553 number(bv()); // Set current_font.number to OFF
1554 } else if (IsDigit(c) &&
1555 real_current_font.isVisibleRightToLeft()) {
1556 number(bv()); // Set current_font.number to ON
1558 if (cursor.pos() > 0) {
1559 char const c = cursor.par()->getChar(cursor.pos() - 1);
1560 if (contains(number_unary_operators, c) &&
1561 (cursor.pos() == 1 ||
1562 cursor.par()->isSeparator(cursor.pos() - 2) ||
1563 cursor.par()->isNewline(cursor.pos() - 2))
1565 setCharFont(bv()->buffer(),
1569 } else if (contains(number_seperators, c) &&
1570 cursor.pos() >= 2 &&
1571 getFont(bv()->buffer(),
1573 cursor.pos() - 2).number() == LyXFont::ON) {
1574 setCharFont(bv()->buffer(),
1584 // First check, if there will be two blanks together or a blank at
1585 // the beginning of a paragraph.
1586 // I decided to handle blanks like normal characters, the main
1587 // difference are the special checks when calculating the row.fill
1588 // (blank does not count at the end of a row) and the check here
1590 // The bug is triggered when we type in a description environment:
1591 // The current_font is not changed when we go from label to main text
1592 // and it should (along with realtmpfont) when we type the space.
1593 // CHECK There is a bug here! (Asger)
1595 LyXFont realtmpfont = real_current_font;
1596 LyXFont rawtmpfont = current_font;
1597 // store the current font. This is because of the use of cursor
1598 // movements. The moving cursor would refresh the current font
1600 // Get the font that is used to calculate the baselineskip
1601 pos_type const lastpos = cursor.par()->size();
1602 LyXFont rawparfont =
1603 cursor.par()->getFontSettings(bv()->buffer()->params,
1606 bool jumped_over_space = false;
1608 if (!freeSpacing && IsLineSeparatorChar(c)) {
1609 if ((cursor.pos() > 0
1610 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1611 || (cursor.pos() > 0
1612 && cursor.par()->isNewline(cursor.pos() - 1))
1613 || (cursor.pos() == 0)) {
1614 static bool sent_space_message = false;
1615 if (!sent_space_message) {
1616 if (cursor.pos() == 0)
1617 bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1619 bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1620 sent_space_message = true;
1627 // the display inset stuff
1628 if (cursor.row()->pos() < cursor.row()->par()->size()
1629 && cursor.row()->par()->isInset(cursor.row()->pos())) {
1630 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1631 if (inset && (inset->display() || inset->needFullRow())) {
1632 // force a new break
1633 cursor.row()->fill(-1); // to force a new break
1637 // get the cursor row fist
1638 Row * row = cursor.row();
1639 int y = cursor.y() - row->baseline();
1640 if (c != Paragraph::META_INSET) {
1641 // Here case LyXText::InsertInset already insertet the character
1642 cursor.par()->insertChar(cursor.pos(), c);
1644 setCharFont(bv()->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1646 if (!jumped_over_space) {
1647 // refresh the positions
1649 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1650 tmprow = tmprow->next();
1651 tmprow->pos(tmprow->pos() + 1);
1655 // Is there a break one row above
1656 if (row->previous() && row->previous()->par() == row->par()
1657 && (cursor.par()->isLineSeparator(cursor.pos())
1658 || cursor.par()->isNewline(cursor.pos())
1659 || ((cursor.pos() < cursor.par()->size()) &&
1660 cursor.par()->isInset(cursor.pos()+1))
1661 || cursor.row()->fill() == -1))
1663 pos_type z = rowBreakPoint(*row->previous());
1665 if (z >= row->pos()) {
1668 // set the dimensions of the row above
1669 row->previous()->fill(fill(
1673 setHeightOfRow(row->previous());
1675 y -= row->previous()->height();
1679 breakAgainOneRow(row);
1681 current_font = rawtmpfont;
1682 real_current_font = realtmpfont;
1683 setCursor(cursor.par(), cursor.pos() + 1,
1684 false, cursor.boundary());
1685 // cursor MUST be in row now.
1687 if (row->next() && row->next()->par() == row->par())
1688 need_break_row = row->next();
1692 // check, wether the last characters font has changed.
1693 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1694 && rawparfont != rawtmpfont)
1695 redoHeightOfParagraph();
1702 // recalculate the fill of the row
1703 if (row->fill() >= 0) {
1704 // needed because a newline will set fill to -1. Otherwise
1705 // we would not get a rebreak!
1706 row->fill(fill(*row, workWidth()));
1709 if (c == Paragraph::META_INSET || row->fill() < 0) {
1711 breakAgainOneRow(row);
1712 // will the cursor be in another row now?
1713 if (row->lastPos() <= cursor.pos() + 1 && row->next()) {
1714 if (row->next() && row->next()->par() == row->par())
1715 // this should always be true
1717 breakAgainOneRow(row);
1719 current_font = rawtmpfont;
1720 real_current_font = realtmpfont;
1722 setCursor(cursor.par(), cursor.pos() + 1, false,
1724 if (isBoundary(bv()->buffer(), cursor.par(), cursor.pos())
1725 != cursor.boundary())
1726 setCursor(cursor.par(), cursor.pos(), false,
1727 !cursor.boundary());
1728 if (row->next() && row->next()->par() == row->par())
1729 need_break_row = row->next();
1733 // FIXME: similar code is duplicated all over - make resetHeightOfRow
1734 int const tmpheight = row->height();
1736 setHeightOfRow(row);
1738 if (tmpheight == row->height()) {
1739 postRowPaint(row, y);
1744 current_font = rawtmpfont;
1745 real_current_font = realtmpfont;
1746 setCursor(cursor.par(), cursor.pos() + 1, false,
1750 // check, wether the last characters font has changed.
1751 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1752 && rawparfont != rawtmpfont) {
1753 redoHeightOfParagraph();
1755 // now the special right address boxes
1756 if (cursor.par()->layout()->margintype
1757 == MARGIN_RIGHT_ADDRESS_BOX) {
1758 redoDrawingOfParagraph(cursor);
1766 void LyXText::charInserted()
1768 // Here we could call FinishUndo for every 20 characters inserted.
1769 // This is from my experience how emacs does it.
1770 static unsigned int counter;
1780 void LyXText::prepareToPrint(Row * row, float & x,
1781 float & fill_separator,
1783 float & fill_label_hfill,
1789 float w = row->fill();
1791 fill_label_hfill = 0;
1793 fill_label_hfill = 0;
1796 row->par()->isRightToLeftPar(bv()->buffer()->params);
1798 x = (workWidth() > 0)
1799 ? rightMargin(*bv()->buffer(), *row) : 0;
1801 x = (workWidth() > 0)
1802 ? leftMargin(row) : 0;
1804 // is there a manual margin with a manual label
1805 LyXLayout_ptr const & layout = row->par()->layout();
1807 if (layout->margintype == MARGIN_MANUAL
1808 && layout->labeltype == LABEL_MANUAL) {
1809 /// We might have real hfills in the label part
1810 nlh = row->numberOfLabelHfills();
1812 // A manual label par (e.g. List) has an auto-hfill
1813 // between the label text and the body of the
1815 // But we don't want to do this auto hfill if the par
1817 if (!row->par()->empty())
1820 if (nlh && !row->par()->getLabelWidthString().empty()) {
1821 fill_label_hfill = labelFill(*row) / nlh;
1825 // are there any hfills in the row?
1826 float const nh = row->numberOfHfills();
1830 fill_hfill = w / nh;
1831 // we don't have to look at the alignment if it is ALIGN_LEFT and
1832 // if the row is already larger then the permitted width as then
1833 // we force the LEFT_ALIGN'edness!
1834 } else if (static_cast<int>(row->width()) < workWidth()) {
1835 // is it block, flushleft or flushright?
1836 // set x how you need it
1838 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
1839 align = layout->align;
1841 align = row->par()->params().align();
1844 // center displayed insets
1846 if (row->pos() < row->par()->size()
1847 && row->par()->isInset(row->pos())
1848 && (inset = row->par()->getInset(row->pos()))
1849 && (inset->display())) // || (inset->scroll() < 0)))
1850 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
1851 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1852 // ERT insets should always be LEFT ALIGNED on screen
1853 inset = row->par()->inInset();
1854 if (inset && inset->owner() &&
1855 inset->owner()->lyxCode() == Inset::ERT_CODE)
1857 align = LYX_ALIGN_LEFT;
1861 case LYX_ALIGN_BLOCK:
1862 ns = row->numberOfSeparators();
1863 if (ns && row->next() && row->next()->par() == row->par() &&
1864 !(row->next()->par()->isNewline(row->next()->pos() - 1))
1865 && !(row->next()->par()->isInset(row->next()->pos())
1866 && row->next()->par()->getInset(row->next()->pos())
1867 && row->next()->par()->getInset(row->next()->pos())->display())
1870 fill_separator = w / ns;
1871 } else if (is_rtl) {
1875 case LYX_ALIGN_RIGHT:
1878 case LYX_ALIGN_CENTER:
1886 computeBidiTables(bv()->buffer(), row);
1888 pos_type body_pos = row->par()->beginningOfBody();
1889 pos_type last = row->lastPos();
1892 (body_pos - 1 > last ||
1893 !row->par()->isLineSeparator(body_pos - 1))) {
1894 x += font_metrics::width(layout->labelsep,
1895 getLabelFont(bv()->buffer(), row->par()));
1896 if (body_pos - 1 <= last)
1897 x += fill_label_hfill;
1903 // important for the screen
1906 // the cursor set functions have a special mechanism. When they
1907 // realize, that you left an empty paragraph, they will delete it.
1908 // They also delete the corresponding row
1910 void LyXText::cursorRightOneWord() const
1912 // treat floats, HFills and Insets as words
1913 LyXCursor tmpcursor = cursor;
1914 // CHECK See comment on top of text.C
1916 if (tmpcursor.pos() == tmpcursor.par()->size()
1917 && tmpcursor.par()->next()) {
1918 tmpcursor.par(tmpcursor.par()->next());
1923 // Skip through initial nonword stuff.
1924 while (tmpcursor.pos() < tmpcursor.par()->size() &&
1925 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
1926 // printf("Current pos1 %d", tmpcursor.pos()) ;
1927 tmpcursor.pos(tmpcursor.pos() + 1);
1930 // Advance through word.
1931 while (tmpcursor.pos() < tmpcursor.par()->size() &&
1932 tmpcursor.par()->isWord(tmpcursor.pos())) {
1933 // printf("Current pos2 %d", tmpcursor.pos()) ;
1934 tmpcursor.pos(tmpcursor.pos() + 1);
1938 setCursor(tmpcursor.par(), tmpcursor.pos());
1942 void LyXText::cursorTab() const
1944 LyXCursor tmpcursor = cursor;
1945 while (tmpcursor.pos() < tmpcursor.par()->size()
1946 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
1947 tmpcursor.pos(tmpcursor.pos() + 1);
1949 if (tmpcursor.pos() == tmpcursor.par()->size()) {
1950 if (tmpcursor.par()->next()) {
1951 tmpcursor.par(tmpcursor.par()->next());
1955 tmpcursor.pos(tmpcursor.pos() + 1);
1956 setCursor(tmpcursor.par(), tmpcursor.pos());
1960 // Skip initial whitespace at end of word and move cursor to *start*
1961 // of prior word, not to end of next prior word.
1962 void LyXText::cursorLeftOneWord() const
1964 LyXCursor tmpcursor = cursor;
1965 cursorLeftOneWord(tmpcursor);
1966 setCursor(tmpcursor.par(), tmpcursor.pos());
1970 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
1972 // treat HFills, floats and Insets as words
1975 && (cur.par()->isSeparator(cur.pos() - 1)
1976 || cur.par()->isKomma(cur.pos() - 1)
1977 || cur.par()->isNewline(cur.pos() - 1))
1978 && !(cur.par()->isHfill(cur.pos() - 1)
1979 || cur.par()->isInset(cur.pos() - 1)))
1980 cur.pos(cur.pos() - 1);
1983 && (cur.par()->isInset(cur.pos() - 1)
1984 || cur.par()->isHfill(cur.pos() - 1))) {
1985 cur.pos(cur.pos() - 1);
1986 } else if (!cur.pos()) {
1987 if (cur.par()->previous()) {
1988 cur.par(cur.par()->previous());
1989 cur.pos(cur.par()->size());
1991 } else { // Here, cur != 0
1992 while (cur.pos() > 0 &&
1993 cur.par()->isWord(cur.pos() - 1))
1994 cur.pos(cur.pos() - 1);
1999 // Select current word. This depends on behaviour of
2000 // CursorLeftOneWord(), so it is patched as well.
2001 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2002 word_location const loc) const
2004 // first put the cursor where we wana start to select the word
2007 case WHOLE_WORD_STRICT:
2008 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2009 || cursor.par()->isSeparator(cursor.pos())
2010 || cursor.par()->isKomma(cursor.pos())
2011 || cursor.par()->isNewline(cursor.pos())
2012 || cursor.par()->isSeparator(cursor.pos() - 1)
2013 || cursor.par()->isKomma(cursor.pos() - 1)
2014 || cursor.par()->isNewline(cursor.pos() - 1)) {
2018 // no break here, we go to the next
2021 // Move cursor to the beginning, when not already there.
2022 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2023 && !(from.par()->isKomma(from.pos() - 1)
2024 || from.par()->isNewline(from.pos() - 1)))
2025 cursorLeftOneWord(from);
2028 // always move the cursor to the beginning of previous word
2029 cursorLeftOneWord(from);
2032 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2038 while (to.pos() < to.par()->size()
2039 && !to.par()->isSeparator(to.pos())
2040 && !to.par()->isKomma(to.pos())
2041 && !to.par()->isNewline(to.pos())
2042 && !to.par()->isHfill(to.pos())
2043 && !to.par()->isInset(to.pos()))
2045 to.pos(to.pos() + 1);
2050 void LyXText::selectWord(word_location loc)
2054 getWord(from, to, loc);
2056 setCursor(from.par(), from.pos());
2059 selection.cursor = cursor;
2060 setCursor(to.par(), to.pos());
2065 // Select the word currently under the cursor when no
2066 // selection is currently set
2067 bool LyXText::selectWordWhenUnderCursor(word_location loc)
2069 if (!selection.set()) {
2071 return selection.set();
2077 void LyXText::acceptChange()
2079 if (!selection.set() && cursor.par()->size())
2084 if (selection.start.par() == selection.end.par()) {
2085 LyXCursor & startc = selection.start;
2086 LyXCursor & endc = selection.end;
2087 setUndo(bv(), Undo::INSERT, startc.par(), startc.par()->next());
2088 startc.par()->acceptChange(startc.pos(), endc.pos());
2091 redoParagraphs(startc, startc.par()->next());
2092 setCursorIntern(startc.par(), 0);
2094 #warning handle multi par selection
2098 void LyXText::rejectChange()
2100 if (!selection.set() && cursor.par()->size())
2105 if (selection.start.par() == selection.end.par()) {
2106 LyXCursor & startc = selection.start;
2107 LyXCursor & endc = selection.end;
2108 setUndo(bv(), Undo::INSERT, startc.par(), startc.par()->next());
2109 startc.par()->rejectChange(startc.pos(), endc.pos());
2112 redoParagraphs(startc, startc.par()->next());
2113 setCursorIntern(startc.par(), 0);
2115 #warning handle multi par selection
2119 // This function is only used by the spellchecker for NextWord().
2120 // It doesn't handle LYX_ACCENTs and probably never will.
2122 LyXText::selectNextWordToSpellcheck(float & value) const
2124 if (the_locking_inset) {
2125 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
2126 if (!word.word().empty()) {
2127 value += float(cursor.y());
2128 value /= float(height);
2131 // we have to go on checking so move cursor to the next char
2132 if (cursor.pos() == cursor.par()->size()) {
2133 if (!cursor.par()->next())
2135 cursor.par(cursor.par()->next());
2138 cursor.pos(cursor.pos() + 1);
2140 Paragraph * tmppar = cursor.par();
2142 // If this is not the very first word, skip rest of
2143 // current word because we are probably in the middle
2144 // of a word if there is text here.
2145 if (cursor.pos() || cursor.par()->previous()) {
2146 while (cursor.pos() < cursor.par()->size()
2147 && cursor.par()->isLetter(cursor.pos()))
2148 cursor.pos(cursor.pos() + 1);
2151 // Now, skip until we have real text (will jump paragraphs)
2153 Paragraph * cpar(cursor.par());
2154 pos_type const cpos(cursor.pos());
2156 if (cpos == cpar->size()) {
2158 cursor.par(cpar->next());
2165 bool const is_bad_inset(cpar->isInset(cpos)
2166 && !cpar->getInset(cpos)->allowSpellcheck());
2168 if (cpar->isLetter(cpos) && !isDeletedText(*cpar, cpos)
2172 cursor.pos(cpos + 1);
2175 // now check if we hit an inset so it has to be a inset containing text!
2176 if (cursor.pos() < cursor.par()->size() &&
2177 cursor.par()->isInset(cursor.pos())) {
2179 cursor.par()->getInset(cursor.pos())->edit(bv());
2180 // now call us again to do the above trick
2181 // but obviously we have to start from down below ;)
2182 return bv()->text->selectNextWordToSpellcheck(value);
2185 // Update the value if we changed paragraphs
2186 if (cursor.par() != tmppar) {
2187 setCursor(cursor.par(), cursor.pos());
2188 value = float(cursor.y())/float(height);
2191 // Start the selection from here
2192 selection.cursor = cursor;
2195 getFont(bv()->buffer(), cursor.par(), cursor.pos())
2196 .language()->code());
2197 // and find the end of the word (insets like optional hyphens
2198 // and ligature break are part of a word)
2199 while (cursor.pos() < cursor.par()->size()
2200 && cursor.par()->isLetter(cursor.pos())
2201 && !isDeletedText(*cursor.par(), cursor.pos()))
2202 cursor.pos(cursor.pos() + 1);
2204 // Finally, we copy the word to a string and return it
2206 if (selection.cursor.pos() < cursor.pos()) {
2208 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2209 if (!cursor.par()->isInset(i))
2210 str += cursor.par()->getChar(i);
2213 return WordLangTuple(str, lang_code);
2217 // This one is also only for the spellchecker
2218 void LyXText::selectSelectedWord()
2220 if (the_locking_inset) {
2221 the_locking_inset->selectSelectedWord(bv());
2224 // move cursor to the beginning
2225 setCursor(selection.cursor.par(), selection.cursor.pos());
2227 // set the sel cursor
2228 selection.cursor = cursor;
2230 // now find the end of the word
2231 while (cursor.pos() < cursor.par()->size()
2232 && (cursor.par()->isLetter(cursor.pos())))
2233 cursor.pos(cursor.pos() + 1);
2235 setCursor(cursor.par(), cursor.pos());
2237 // finally set the selection
2242 // Delete from cursor up to the end of the current or next word.
2243 void LyXText::deleteWordForward()
2245 if (cursor.par()->empty())
2248 LyXCursor tmpcursor = cursor;
2249 tmpcursor.row(0); //Â ??
2250 selection.set(true); // to avoid deletion
2251 cursorRightOneWord();
2252 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2253 selection.cursor = cursor;
2257 // Great, CutSelection() gets rid of multiple spaces.
2258 cutSelection(true, false);
2263 // Delete from cursor to start of current or prior word.
2264 void LyXText::deleteWordBackward()
2266 if (cursor.par()->empty())
2269 LyXCursor tmpcursor = cursor;
2270 tmpcursor.row(0); // ??
2271 selection.set(true); // to avoid deletion
2272 cursorLeftOneWord();
2273 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2274 selection.cursor = cursor;
2277 cutSelection(true, false);
2282 // Kill to end of line.
2283 void LyXText::deleteLineForward()
2285 if (cursor.par()->empty())
2286 // Paragraph is empty, so we just go to the right
2289 LyXCursor tmpcursor = cursor;
2290 // We can't store the row over a regular setCursor
2291 // so we set it to 0 and reset it afterwards.
2292 tmpcursor.row(0); //Â ??
2293 selection.set(true); // to avoid deletion
2295 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2296 selection.cursor = cursor;
2299 // What is this test for ??? (JMarc)
2300 if (!selection.set()) {
2301 deleteWordForward();
2303 cutSelection(true, false);
2309 void LyXText::changeCase(LyXText::TextCase action)
2314 if (selection.set()) {
2315 from = selection.start;
2318 getWord(from, to, PARTIAL_WORD);
2319 setCursor(to.par(), to.pos() + 1);
2322 lyx::Assert(from <= to);
2324 setUndo(bv(), Undo::FINISH, from.par(), to.par()->next());
2326 pos_type pos = from.pos();
2327 Paragraph * par = from.par();
2329 while (par && (pos != to.pos() || par != to.par())) {
2330 if (pos == par->size()) {
2335 unsigned char c = par->getChar(pos);
2336 if (!IsInsetChar(c)) {
2338 case text_lowercase:
2341 case text_capitalization:
2343 action = text_lowercase;
2345 case text_uppercase:
2351 par->setChar(pos, c);
2352 checkParagraph(par, pos);
2357 if (to.row() != from.row())
2358 postPaint(from.y() - from.row()->baseline());
2362 void LyXText::transposeChars()
2364 Paragraph * tmppar = cursor.par();
2366 setUndo(bv(), Undo::FINISH, tmppar, tmppar->next());
2368 pos_type tmppos = cursor.pos();
2370 // First decide if it is possible to transpose at all
2372 if (tmppos == 0 || tmppos == tmppar->size())
2375 if (isDeletedText(*tmppar, tmppos - 1)
2376 || isDeletedText(*tmppar, tmppos))
2379 unsigned char c1 = tmppar->getChar(tmppos);
2380 unsigned char c2 = tmppar->getChar(tmppos - 1);
2382 // We should have an implementation that handles insets
2383 // as well, but that will have to come later. (Lgb)
2384 if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
2387 bool const erased = tmppar->erase(tmppos - 1, tmppos + 1);
2388 pos_type const ipos(erased ? tmppos - 1 : tmppos + 1);
2390 tmppar->insertChar(ipos, c1);
2391 tmppar->insertChar(ipos + 1, c2);
2393 checkParagraph(tmppar, tmppos);
2397 void LyXText::Delete()
2399 // this is a very easy implementation
2401 LyXCursor old_cursor = cursor;
2402 int const old_cur_par_id = old_cursor.par()->id();
2403 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2404 old_cursor.par()->previous()->id() : -1;
2406 // just move to the right
2409 // CHECK Look at the comment here.
2410 // This check is not very good...
2411 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2412 // and that can very well delete the par or par->previous in
2413 // old_cursor. Will a solution where we compare paragraph id's
2415 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : -1)
2416 == old_cur_par_prev_id
2417 && cursor.par()->id() != old_cur_par_id) {
2418 // delete-empty-paragraph-mechanism has done it
2422 // if you had success make a backspace
2423 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2424 LyXCursor tmpcursor = cursor;
2425 // to make sure undo gets the right cursor position
2426 cursor = old_cursor;
2427 setUndo(bv(), Undo::DELETE,
2428 cursor.par(), cursor.par()->next());
2435 void LyXText::backspace()
2437 // Get the font that is used to calculate the baselineskip
2438 pos_type lastpos = cursor.par()->size();
2439 LyXFont rawparfont =
2440 cursor.par()->getFontSettings(bv()->buffer()->params,
2443 if (cursor.pos() == 0) {
2444 // The cursor is at the beginning of a paragraph,
2445 // so the the backspace will collapse two paragraphs into one.
2447 // but it's not allowed unless it's new
2448 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2451 // we may paste some paragraphs
2453 // is it an empty paragraph?
2456 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2457 // This is an empty paragraph and we delete it just by moving the cursor one step
2458 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2459 // of the paragraph.
2461 if (cursor.par()->previous()) {
2462 Paragraph * tmppar = cursor.par()->previous();
2463 if (cursor.par()->layout() == tmppar->layout()
2464 && cursor.par()->getAlign() == tmppar->getAlign()) {
2465 // Inherit bottom DTD from the paragraph below.
2466 // (the one we are deleting)
2467 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2468 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2469 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2474 // the layout things can change the height of a row !
2475 int const tmpheight = cursor.row()->height();
2476 setHeightOfRow(cursor.row());
2477 if (cursor.row()->height() != tmpheight) {
2478 postPaint(cursor.y() - cursor.row()->baseline());
2484 if (cursor.par()->previous()) {
2485 setUndo(bv(), Undo::DELETE,
2486 cursor.par()->previous(), cursor.par()->next());
2489 Paragraph * tmppar = cursor.par();
2490 Row * tmprow = cursor.row();
2492 // We used to do cursorLeftIntern() here, but it is
2493 // not a good idea since it triggers the auto-delete
2494 // mechanism. So we do a cursorLeftIntern()-lite,
2495 // without the dreaded mechanism. (JMarc)
2496 if (cursor.par()->previous()) {
2497 // steps into the above paragraph.
2498 setCursorIntern(cursor.par()->previous(),
2499 cursor.par()->previous()->size(),
2503 // Pasting is not allowed, if the paragraphs have different
2504 // layout. I think it is a real bug of all other
2505 // word processors to allow it. It confuses the user.
2506 // Even so with a footnote paragraph and a non-footnote
2507 // paragraph. I will not allow pasting in this case,
2508 // because the user would be confused if the footnote behaves
2509 // different wether it is open or closed.
2511 // Correction: Pasting is always allowed with standard-layout
2512 LyXTextClass const & tclass =
2513 bv()->buffer()->params.getLyXTextClass();
2515 if (cursor.par() != tmppar
2516 && (cursor.par()->layout() == tmppar->layout()
2517 || tmppar->layout() == tclass.defaultLayout())
2518 && cursor.par()->getAlign() == tmppar->getAlign()) {
2519 removeParagraph(tmprow);
2521 mergeParagraph(bv()->buffer()->params, bv()->buffer()->paragraphs, cursor.par());
2523 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2524 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2525 // strangely enough it seems that commenting out the line above removes
2526 // most or all of the segfaults. I will however also try to move the
2527 // two Remove... lines in front of the PasteParagraph too.
2530 cursor.pos(cursor.pos() - 1);
2532 postPaint(cursor.y() - cursor.row()->baseline());
2534 // remove the lost paragraph
2535 // This one is not safe, since the paragraph that the tmprow and the
2536 // following rows belong to has been deleted by the PasteParagraph
2537 // above. The question is... could this be moved in front of the
2539 //RemoveParagraph(tmprow);
2540 //RemoveRow(tmprow);
2542 // This rebuilds the rows.
2543 appendParagraph(cursor.row());
2546 // the row may have changed, block, hfills etc.
2547 setCursor(cursor.par(), cursor.pos(), false);
2550 // this is the code for a normal backspace, not pasting
2552 setUndo(bv(), Undo::DELETE,
2553 cursor.par(), cursor.par()->next());
2554 // We used to do cursorLeftIntern() here, but it is
2555 // not a good idea since it triggers the auto-delete
2556 // mechanism. So we do a cursorLeftIntern()-lite,
2557 // without the dreaded mechanism. (JMarc)
2558 setCursorIntern(cursor.par(), cursor.pos()- 1,
2559 false, cursor.boundary());
2561 if (cursor.par()->isInset(cursor.pos())) {
2562 // force complete redo when erasing display insets
2563 // this is a cruel method but safe..... Matthias
2564 if (cursor.par()->getInset(cursor.pos())->display() ||
2565 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2566 cursor.par()->erase(cursor.pos());
2572 Row * row = cursor.row();
2573 int y = cursor.y() - row->baseline();
2575 // remember that a space at the end of a row doesnt count
2576 // when calculating the fill
2577 if (cursor.pos() < row->lastPos() ||
2578 !cursor.par()->isLineSeparator(cursor.pos())) {
2579 row->fill(row->fill() + singleWidth(
2584 // some special code when deleting a newline. This is similar
2585 // to the behavior when pasting paragraphs
2586 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2587 cursor.par()->erase(cursor.pos());
2588 // refresh the positions
2590 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2591 tmprow = tmprow->next();
2592 tmprow->pos(tmprow->pos() - 1);
2594 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2595 cursor.pos(cursor.pos() - 1);
2597 if (cursor.pos() < cursor.par()->size()
2598 && !cursor.par()->isSeparator(cursor.pos())) {
2599 cursor.par()->insertChar(cursor.pos(), ' ');
2600 setCharFont(bv()->buffer(), cursor.par(),
2601 cursor.pos(), current_font);
2602 // refresh the positions
2604 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2605 tmprow = tmprow->next();
2606 tmprow->pos(tmprow->pos() + 1);
2610 cursor.par()->erase(cursor.pos());
2612 // refresh the positions
2614 while (tmprow->next()
2615 && tmprow->next()->par() == row->par()) {
2616 tmprow = tmprow->next();
2617 tmprow->pos(tmprow->pos() - 1);
2620 // delete newlines at the beginning of paragraphs
2621 while (!cursor.par()->empty() &&
2622 cursor.par()->isNewline(cursor.pos()) &&
2623 cursor.pos() == cursor.par()->beginningOfBody()) {
2624 cursor.par()->erase(cursor.pos());
2625 // refresh the positions
2627 while (tmprow->next() &&
2628 tmprow->next()->par() == row->par()) {
2629 tmprow = tmprow->next();
2630 tmprow->pos(tmprow->pos() - 1);
2635 // is there a break one row above
2636 if (row->previous() && row->previous()->par() == row->par()) {
2637 z = rowBreakPoint(*row->previous());
2638 if (z >= row->pos()) {
2641 Row * tmprow = row->previous();
2643 // maybe the current row is now empty
2644 if (row->pos() >= row->par()->size()) {
2649 breakAgainOneRow(row);
2650 if (row->next() && row->next()->par() == row->par())
2651 need_break_row = row->next();
2656 // set the dimensions of the row above
2657 y -= tmprow->height();
2658 tmprow->fill(fill(*tmprow, workWidth()));
2659 setHeightOfRow(tmprow);
2663 setCursor(cursor.par(), cursor.pos(),
2664 false, cursor.boundary());
2665 //current_font = rawtmpfont;
2666 //real_current_font = realtmpfont;
2667 // check, whether the last character's font has changed.
2669 cursor.par()->getFontSettings(bv()->buffer()->params,
2670 cursor.par()->size() - 1))
2671 redoHeightOfParagraph();
2676 // break the cursor row again
2677 if (row->next() && row->next()->par() == row->par() &&
2678 (row->lastPos() == row->par()->size() - 1 ||
2679 rowBreakPoint(*row) != row->lastPos())) {
2681 // it can happen that a paragraph loses one row
2682 // without a real breakup. This is when a word
2683 // is to long to be broken. Well, I don t care this
2685 if (row->lastPos() == row->par()->size() - 1)
2686 removeRow(row->next());
2690 breakAgainOneRow(row);
2691 // will the cursor be in another row now?
2692 if (row->next() && row->next()->par() == row->par() &&
2693 row->lastPos() <= cursor.pos()) {
2695 breakAgainOneRow(row);
2698 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2700 if (row->next() && row->next()->par() == row->par())
2701 need_break_row = row->next();
2705 // set the dimensions of the row
2706 row->fill(fill(*row, workWidth()));
2707 int const tmpheight = row->height();
2708 setHeightOfRow(row);
2709 if (tmpheight == row->height()) {
2710 postRowPaint(row, y);
2714 setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2718 // current_font = rawtmpfont;
2719 // real_current_font = realtmpfont;
2721 if (isBoundary(bv()->buffer(), cursor.par(), cursor.pos())
2722 != cursor.boundary())
2723 setCursor(cursor.par(), cursor.pos(), false,
2724 !cursor.boundary());
2726 lastpos = cursor.par()->size();
2727 if (cursor.pos() == lastpos)
2730 // check, whether the last characters font has changed.
2732 cursor.par()->getFontSettings(bv()->buffer()->params, lastpos - 1)) {
2733 redoHeightOfParagraph();
2735 // now the special right address boxes
2736 if (cursor.par()->layout()->margintype
2737 == MARGIN_RIGHT_ADDRESS_BOX) {
2738 redoDrawingOfParagraph(cursor);
2744 // returns pointer to a specified row
2745 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
2750 Row * tmprow = firstrow;
2753 // find the first row of the specified paragraph
2754 while (tmprow->next() && tmprow->par() != par) {
2755 y += tmprow->height();
2756 tmprow = tmprow->next();
2759 // now find the wanted row
2760 while (tmprow->pos() < pos
2762 && tmprow->next()->par() == par
2763 && tmprow->next()->pos() <= pos) {
2764 y += tmprow->height();
2765 tmprow = tmprow->next();
2772 Row * LyXText::getRowNearY(int & y) const
2775 // If possible we should optimize this method. (Lgb)
2776 Row * tmprow = firstrow;
2779 while (tmprow->next() && tmpy + tmprow->height() <= y) {
2780 tmpy += tmprow->height();
2781 tmprow = tmprow->next();
2784 y = tmpy; // return the real y
2786 //lyxerr << "returned y = " << y << endl;
2790 // Search from the current cursor position.
2792 Row * tmprow = cursor.row();
2793 int tmpy = cursor.y() - tmprow->baseline();
2795 lyxerr << "cursor.y() = " << tmpy << endl;
2796 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
2797 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
2798 lyxerr << "first = " << first << endl;
2799 lyxerr << "y = " << y << endl;
2802 lyxerr << "up" << endl;
2804 tmpy -= tmprow->height();
2805 tmprow = tmprow->previous();
2806 } while (tmprow && tmpy - tmprow->height() >= y);
2807 } else if (y > tmpy) {
2808 lyxerr << "down" << endl;
2810 while (tmprow->next() && tmpy + tmprow->height() <= y) {
2811 tmpy += tmprow->height();
2812 tmprow = tmprow->next();
2815 lyxerr << "equal" << endl;
2818 y = tmpy; // return the real y
2820 lyxerr << "returned y = " << y << endl;
2828 int LyXText::getDepth() const
2830 return cursor.par()->getDepth();