1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #include "paragraph.h"
17 #include "bufferparams.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "paragraph_funcs.h"
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
53 int const PAPER_MARGIN = 20;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
60 int LyXText::workWidth(BufferView * bview) const
63 return inset_owner->textWidth(bview);
65 return bview->workWidth();
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
74 par = inset->parOwner();
76 pos = par->getPositionOfInset(inset);
78 if (!par || pos == -1) {
79 lyxerr << "LyXText::workWidth: something is wrong,"
80 " fall back to the brute force method" << endl;
81 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
82 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
83 for (; it != end; ++it) {
84 if (&(*it) == inset) {
93 return workWidth(bview);
96 LyXLayout_ptr const & layout = par->layout();
98 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
99 // Optimization here: in most cases, the real row is
100 // not needed, but only the par/pos values. So we just
101 // construct a dummy row for leftMargin. (JMarc)
105 return workWidth(bview) - leftMargin(bview, &dummyrow);
108 Row * row = getRow(par, pos, dummy_y);
110 while (frow->previous() && frow->par() == frow->previous()->par())
111 frow = frow->previous();
112 unsigned int maxw = 0;
113 while (frow->next() && frow->par() == frow->next()->par()) {
114 if ((frow != row) && (maxw < frow->width()))
115 maxw = frow->width();
121 return workWidth(bview);
125 int LyXText::getRealCursorX(BufferView * bview) const
128 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
129 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
137 if (!Encodings::is_arabic(c))
138 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139 return c + (0xb0 - '0');
143 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
144 unsigned char next_char = ' ';
146 for (pos_type i = pos+1; i < par->size(); ++i)
147 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
148 next_char = par->getChar(i);
152 if (Encodings::is_arabic(next_char)) {
153 if (Encodings::is_arabic(prev_char) &&
154 !Encodings::is_arabic_special(prev_char))
155 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
157 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
159 if (Encodings::is_arabic(prev_char) &&
160 !Encodings::is_arabic_special(prev_char))
161 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
163 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
167 // This is the comments that some of the warnings below refers to.
168 // There are some issues in this file and I don't think they are
169 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
170 // this is a problem that has been here almost from day one and that a
171 // larger userbase with differenct access patters triggers the bad
172 // behaviour. (segfaults.) What I think happen is: In several places
173 // we store the paragraph in the current cursor and then moves the
174 // cursor. This movement of the cursor will delete paragraph at the
175 // old position if it is now empty. This will make the temporary
176 // pointer to the old cursor paragraph invalid and dangerous to use.
177 // And is some cases this will trigger a segfault. I have marked some
178 // of the cases where this happens with a warning, but I am sure there
179 // are others in this file and in text2.C. There is also a note in
180 // Delete() that you should read. In Delete I store the paragraph->id
181 // instead of a pointer to the paragraph. I am pretty sure this faulty
182 // use of temporary pointers to paragraphs that might have gotten
183 // invalidated (through a cursor movement) before they are used, are
184 // the cause of the strange crashes we get reported often.
186 // It is very tiresom to change this code, especially when it is as
187 // hard to read as it is. Help to fix all the cases where this is done
188 // would be greately appreciated.
192 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
195 char const c = par->getChar(pos);
196 return singleWidth(bview, par, pos, c);
200 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
201 pos_type pos, char c) const
203 LyXFont const font = getFont(bview->buffer(), par, pos);
205 // The most common case is handled first (Asger)
206 if (IsPrintable(c)) {
207 if (font.language()->RightToLeft()) {
208 if (font.language()->lang() == "arabic" &&
209 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
210 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
211 if (Encodings::IsComposeChar_arabic(c))
214 c = transformChar(c, par, pos);
215 else if (font.language()->lang() == "hebrew" &&
216 Encodings::IsComposeChar_hebrew(c))
219 return font_metrics::width(c, font);
221 } else if (IsHfillChar(c)) {
222 // Because of the representation as vertical lines
224 } else if (c == Paragraph::META_INSET) {
225 Inset * tmpinset = par->getInset(pos);
228 // this IS needed otherwise on initialitation we don't get the fill
229 // of the row right (ONLY on initialization if we read a file!)
230 // should be changed! (Jug 20011204)
231 tmpinset->update(bview, font);
233 return tmpinset->width(bview, font);
237 } else if (IsSeparatorChar(c))
239 else if (IsNewlineChar(c))
241 return font_metrics::width(c, font);
245 // Returns the paragraph position of the last character in the specified row
246 pos_type LyXText::rowLast(Row const * row) const
248 if (!row->next() || row->next()->par() != row->par()) {
249 return row->par()->size() - 1;
251 return row->next()->pos() - 1;
256 pos_type LyXText::rowLastPrintable(Row const * row) const
258 pos_type const last = rowLast(row);
259 bool ignore_the_space_on_the_last_position = true;
261 // we have to consider a space on the last position in this case!
262 if (row->next() && row->par() == row->next()->par() &&
263 row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
264 (ins=row->next()->par()->getInset(last+1)) &&
265 (ins->needFullRow() || ins->display()))
267 ignore_the_space_on_the_last_position = false;
269 if (last >= row->pos()
271 && row->next()->par() == row->par()
272 && row->par()->isSeparator(last)
273 && ignore_the_space_on_the_last_position)
280 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
282 bidi_same_direction = true;
283 if (!lyxrc.rtl_support) {
288 Inset * inset = row->par()->inInset();
289 if (inset && inset->owner() &&
290 inset->owner()->lyxCode() == Inset::ERT_CODE) {
295 bidi_start = row->pos();
296 bidi_end = rowLastPrintable(row);
298 if (bidi_start > bidi_end) {
303 if (bidi_end + 2 - bidi_start >
304 static_cast<pos_type>(log2vis_list.size())) {
306 (bidi_end + 2 - bidi_start < 500) ?
307 500 : 2 * (bidi_end + 2 - bidi_start);
308 log2vis_list.resize(new_size);
309 vis2log_list.resize(new_size);
310 bidi_levels.resize(new_size);
313 vis2log_list[bidi_end + 1 - bidi_start] = -1;
314 log2vis_list[bidi_end + 1 - bidi_start] = -1;
318 row->par()->isRightToLeftPar(buf->params);
322 pos_type const main_body = beginningOfMainBody(buf, row->par());
324 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
325 bool is_space = row->par()->isLineSeparator(lpos);
327 (is_space && lpos + 1 <= bidi_end &&
328 !row->par()->isLineSeparator(lpos + 1) &&
329 !row->par()->isNewline(lpos + 1))
331 LyXFont font = row->par()->getFontSettings(buf->params, pos);
332 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
333 font.number() == LyXFont::ON &&
334 row->par()->getFontSettings(buf->params, lpos - 1).number()
336 font = row->par()->getFontSettings(buf->params, lpos);
341 bool new_rtl = font.isVisibleRightToLeft();
342 bool new_rtl0 = font.isRightToLeft();
345 if (lpos == main_body - 1
346 && row->pos() < main_body - 1
348 new_level = (rtl_par) ? 1 : 0;
349 new_rtl = new_rtl0 = rtl_par;
351 new_level = (new_rtl) ? 1 : 2;
353 new_level = (rtl_par) ? 2 : 0;
355 if (is_space && new_level >= level) {
361 int new_level2 = new_level;
363 if (level == new_level && rtl0 != new_rtl0) {
365 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
366 } else if (level < new_level) {
367 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
368 if (new_level > rtl_par)
369 bidi_same_direction = false;
371 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
374 bidi_levels[lpos - bidi_start] = new_level;
376 while (level > new_level2) {
377 pos_type old_lpos = stack[--level];
378 int delta = lpos - old_lpos - 1;
381 log2vis_list[lpos - bidi_start] += delta;
382 log2vis_list[old_lpos - bidi_start] += delta;
384 while (level < new_level)
385 stack[level++] = lpos;
389 pos_type const old_lpos = stack[--level];
390 int delta = bidi_end - old_lpos;
393 log2vis_list[old_lpos - bidi_start] += delta;
396 pos_type vpos = bidi_start - 1;
397 for (pos_type lpos = bidi_start;
398 lpos <= bidi_end; ++lpos) {
399 vpos += log2vis_list[lpos - bidi_start];
400 vis2log_list[vpos - bidi_start] = lpos;
401 log2vis_list[lpos - bidi_start] = vpos;
406 // This method requires a previous call to ComputeBidiTables()
407 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
410 if (!lyxrc.rtl_support || pos == 0)
413 if (!bidi_InRange(pos - 1)) {
414 /// This can happen if pos is the first char of a row.
415 /// Returning false in this case is incorrect!
419 bool const rtl = bidi_level(pos - 1) % 2;
420 bool const rtl2 = bidi_InRange(pos)
421 ? bidi_level(pos) % 2
422 : par->isRightToLeftPar(buf->params);
427 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
428 pos_type pos, LyXFont const & font) const
430 if (!lyxrc.rtl_support)
431 return false; // This is just for speedup
433 bool const rtl = font.isVisibleRightToLeft();
434 bool const rtl2 = bidi_InRange(pos)
435 ? bidi_level(pos) % 2
436 : par->isRightToLeftPar(buf->params);
441 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
443 // Draw end-of-line marker
444 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
445 int const wid = font_metrics::width('n', font);
446 int const asc = font_metrics::maxAscent(font);
447 int const y = p.yo + p.row->baseline();
451 yp[0] = int(y - 0.875 * asc * 0.75);
452 yp[1] = int(y - 0.500 * asc * 0.75);
453 yp[2] = int(y - 0.125 * asc * 0.75);
455 if (bidi_level(pos) % 2 == 0) {
456 xp[0] = int(p.x + wid * 0.375);
458 xp[2] = int(p.x + wid * 0.375);
460 xp[0] = int(p.x + wid * 0.625);
461 xp[1] = int(p.x + wid);
462 xp[2] = int(p.x + wid * 0.625);
465 p.pain->lines(xp, yp, 3, LColor::eolmarker);
467 yp[0] = int(y - 0.500 * asc * 0.75);
468 yp[1] = int(y - 0.500 * asc * 0.75);
469 yp[2] = int(y - asc * 0.75);
471 if (bidi_level(pos) % 2 == 0) {
473 xp[1] = int(p.x + wid);
474 xp[2] = int(p.x + wid);
476 xp[0] = int(p.x + wid);
481 p.pain->lines(xp, yp, 3, LColor::eolmarker);
487 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
489 Inset * inset = p.row->par()->getInset(pos);
491 // FIXME: shouldn't happen
496 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
497 // we need this here as the row pointer may be illegal
498 // at a later time (Jug20020502)
499 Row * prev = p.row->previous();
501 inset->update(p.bv, font, false);
502 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
504 if (!need_break_row && !inset_owner
505 && p.bv->text->status() == CHANGED_IN_DRAW) {
506 if (prev && prev->par() == p.row->par()) {
507 breakAgainOneRow(p.bv, prev);
508 if (prev->next() != p.row) {
509 // breakAgainOneRow() has removed p.row
510 p.row = 0; // see what this breaks
511 need_break_row = prev;
513 need_break_row = p.row;
516 need_break_row = firstrow;
518 need_break_row = prev->next();
520 setCursor(p.bv, cursor.par(), cursor.pos());
527 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
529 if (!lyxrc.mark_foreign_language)
531 if (orig_font.language() == latex_language)
533 if (orig_font.language() == p.bv->buffer()->params.language)
536 int const y = p.yo + p.row->height() - 1;
537 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
541 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
543 pos_type pos = vis2log(vpos);
548 char c = p.row->par()->getChar(pos);
552 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
553 int const width = font_metrics::width(c, font);
556 for (pos_type i = pos-1; i >= 0; --i) {
557 c = p.row->par()->getChar(i);
558 if (!Encodings::IsComposeChar_hebrew(c)) {
559 if (IsPrintableNonspace(c)) {
561 singleWidth(p.bv, p.row->par(), i, c);
563 dx = (c == 'ø' || c == 'ã')
565 : (width2 - width) / 2;
572 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
576 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
578 pos_type pos = vis2log(vpos);
582 char c = p.row->par()->getChar(pos);
583 c = transformChar(c, p.row->par(), pos);
587 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
588 int const width = font_metrics::width(c, font);
591 for (pos_type i = pos-1; i >= 0; --i) {
592 c = p.row->par()->getChar(i);
593 if (!Encodings::IsComposeChar_arabic(c)) {
594 if (IsPrintableNonspace(c)) {
596 singleWidth(p.bv, p.row->par(), i, c);
597 dx = (width2 - width) / 2;
603 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
607 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
608 bool hebrew, bool arabic)
610 pos_type pos = vis2log(vpos);
611 pos_type const last = rowLastPrintable(p.row);
612 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
616 str += p.row->par()->getChar(pos);
618 unsigned char c = str[0];
619 str[0] = transformChar(c, p.row->par(), pos);
623 // collect as much similar chars as we can
624 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
625 char c = p.row->par()->getChar(pos);
627 if (!IsPrintableNonspace(c))
630 if (arabic && Encodings::IsComposeChar_arabic(c))
632 if (hebrew && Encodings::IsComposeChar_hebrew(c))
635 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
639 c = transformChar(c, p.row->par(), pos);
644 // Draw text and set the new x position
645 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
646 p.x += font_metrics::width(str, orig_font);
650 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
652 pos_type const pos = vis2log(vpos);
653 Paragraph * par = p.row->par();
655 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
657 float const orig_x = p.x;
659 char const c = par->getChar(pos);
661 if (IsNewlineChar(c)) {
665 } else if (IsInsetChar(c)) {
666 if (!drawInset(p, pos))
669 drawForeignMark(p, orig_x, orig_font);
673 // usual characters, no insets
675 // special case languages
676 bool const hebrew = (orig_font.language()->lang() == "hebrew");
678 orig_font.language()->lang() == "arabic" &&
679 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
680 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
682 // draw as many chars as we can
683 if ((!hebrew && !arabic)
684 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
685 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
686 drawChars(p, vpos, hebrew, arabic);
688 drawHebrewComposeChar(p, vpos);
690 drawArabicComposeChar(p, vpos);
693 drawForeignMark(p, orig_x, orig_font);
699 int LyXText::leftMargin(BufferView * bview, Row const * row) const
702 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
703 (ins=row->par()->getInset(row->pos())) &&
704 (ins->needFullRow() || ins->display()))
707 LyXTextClass const & tclass =
708 bview->buffer()->params.getLyXTextClass();
709 LyXLayout_ptr const & layout = row->par()->layout();
711 string parindent = layout->parindent;
713 int x = PAPER_MARGIN;
715 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
717 // this is the way, LyX handles the LaTeX-Environments.
718 // I have had this idea very late, so it seems to be a
719 // later added hack and this is true
720 if (!row->par()->getDepth()) {
721 if (row->par()->layout() == tclass.defaultLayout()) {
722 // find the previous same level paragraph
723 if (row->par()->previous()) {
724 Paragraph * newpar = row->par()
725 ->depthHook(row->par()->getDepth());
727 newpar->layout()->nextnoindent)
732 // find the next level paragraph
734 Paragraph * newpar = row->par()->outerHook();
736 // make a corresponding row. Needed to call LeftMargin()
738 // check wether it is a sufficent paragraph
739 if (newpar && newpar->layout()->isEnvironment()) {
741 dummyrow.par(newpar);
742 dummyrow.pos(newpar->size());
743 x = leftMargin(bview, &dummyrow);
745 // this is no longer an error, because this function
746 // is used to clear impossible depths after changing
747 // a layout. Since there is always a redo,
748 // LeftMargin() is always called
749 row->par()->params().depth(0);
752 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
753 if (newpar->params().noindent())
756 parindent = newpar->layout()->parindent;
762 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
763 switch (layout->margintype) {
765 if (!layout->leftmargin.empty()) {
766 x += font_metrics::signedWidth(layout->leftmargin,
767 tclass.defaultfont());
769 if (!row->par()->getLabelstring().empty()) {
770 x += font_metrics::signedWidth(layout->labelindent,
772 x += font_metrics::width(row->par()->getLabelstring(),
774 x += font_metrics::width(layout->labelsep, labelfont);
778 x += font_metrics::signedWidth(layout->labelindent, labelfont);
779 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
780 if (!row->par()->getLabelWidthString().empty()) {
781 x += font_metrics::width(row->par()->getLabelWidthString(),
783 x += font_metrics::width(layout->labelsep, labelfont);
788 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
789 / (row->par()->getDepth() + 4);
791 case MARGIN_FIRST_DYNAMIC:
792 if (layout->labeltype == LABEL_MANUAL) {
793 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
794 x += font_metrics::signedWidth(layout->leftmargin,
797 x += font_metrics::signedWidth(layout->labelindent,
800 } else if (row->pos()
801 // Special case to fix problems with
803 || (layout->labeltype == LABEL_STATIC
804 && layout->latextype == LATEX_ENVIRONMENT
805 && ! row->par()->isFirstInSequence())) {
806 x += font_metrics::signedWidth(layout->leftmargin,
808 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
809 && layout->labeltype != LABEL_BIBLIO
810 && layout->labeltype !=
811 LABEL_CENTERED_TOP_ENVIRONMENT) {
812 x += font_metrics::signedWidth(layout->labelindent,
814 x += font_metrics::width(layout->labelsep, labelfont);
815 x += font_metrics::width(row->par()->getLabelstring(),
820 case MARGIN_RIGHT_ADDRESS_BOX:
822 // ok, a terrible hack. The left margin depends on the widest
823 // row in this paragraph. Do not care about footnotes, they
824 // are *NOT* allowed in the LaTeX realisation of this layout.
826 // find the first row of this paragraph
827 Row const * tmprow = row;
828 while (tmprow->previous()
829 && tmprow->previous()->par() == row->par())
830 tmprow = tmprow->previous();
832 int minfill = tmprow->fill();
833 while (tmprow->next() && tmprow->next()->par() == row->par()) {
834 tmprow = tmprow->next();
835 if (tmprow->fill() < minfill)
836 minfill = tmprow->fill();
839 x += font_metrics::signedWidth(layout->leftmargin,
840 tclass.defaultfont());
846 if ((workWidth(bview) > 0) &&
847 !row->par()->params().leftIndent().zero())
849 LyXLength const len = row->par()->params().leftIndent();
850 int const tw = inset_owner ?
851 inset_owner->latexTextWidth(bview) : workWidth(bview);
852 x += len.inPixels(tw);
855 LyXAlignment align; // wrong type
857 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
858 align = layout->align;
860 align = row->par()->params().align();
862 // set the correct parindent
863 if (row->pos() == 0) {
864 if ((layout->labeltype == LABEL_NO_LABEL
865 || layout->labeltype == LABEL_TOP_ENVIRONMENT
866 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
867 || (layout->labeltype == LABEL_STATIC
868 && layout->latextype == LATEX_ENVIRONMENT
869 && ! row->par()->isFirstInSequence()))
870 && align == LYX_ALIGN_BLOCK
871 && !row->par()->params().noindent()
872 // in tabulars and ert paragraphs are never indented!
873 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
874 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
875 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
876 && (row->par()->layout() != tclass.defaultLayout() ||
877 bview->buffer()->params.paragraph_separation ==
878 BufferParams::PARSEP_INDENT)) {
879 x += font_metrics::signedWidth(parindent,
880 tclass.defaultfont());
881 } else if (layout->labeltype == LABEL_BIBLIO) {
882 // ale970405 Right width for bibitems
883 x += bibitemMaxWidth(bview, tclass.defaultfont());
891 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
894 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
895 (ins=row->par()->getInset(row->pos())) &&
896 (ins->needFullRow() || ins->display()))
899 LyXTextClass const & tclass = buf->params.getLyXTextClass();
900 LyXLayout_ptr const & layout = row->par()->layout();
903 + font_metrics::signedWidth(tclass.rightmargin(),
904 tclass.defaultfont());
906 // this is the way, LyX handles the LaTeX-Environments.
907 // I have had this idea very late, so it seems to be a
908 // later added hack and this is true
909 if (row->par()->getDepth()) {
910 // find the next level paragraph
912 Paragraph * newpar = row->par();
915 newpar = newpar->previous();
917 && newpar->getDepth() >= row->par()->getDepth());
919 // make a corresponding row. Needed to call LeftMargin()
921 // check wether it is a sufficent paragraph
922 if (newpar && newpar->layout()->isEnvironment()) {
924 dummyrow.par(newpar);
926 x = rightMargin(buf, &dummyrow);
928 // this is no longer an error, because this function
929 // is used to clear impossible depths after changing
930 // a layout. Since there is always a redo,
931 // LeftMargin() is always called
932 row->par()->params().depth(0);
936 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
937 x += font_metrics::signedWidth(layout->rightmargin,
938 tclass.defaultfont())
939 * 4 / (row->par()->getDepth() + 4);
944 int LyXText::labelEnd(BufferView * bview, Row const * row) const
946 if (row->par()->layout()->margintype == MARGIN_MANUAL) {
949 tmprow.pos(row->par()->size());
950 // just the beginning of the main body
951 return leftMargin(bview, &tmprow);
953 // LabelEnd is only needed,
954 // if the layout fills a flushleft label.
960 // get the next breakpoint in a given paragraph
962 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
964 Paragraph * par = row->par();
969 pos_type const pos = row->pos();
971 // position of the last possible breakpoint
972 // -1 isn't a suitable value, but a flag
973 pos_type last_separator = -1;
974 width -= rightMargin(bview->buffer(), row);
976 pos_type const main_body =
977 beginningOfMainBody(bview->buffer(), par);
978 LyXLayout_ptr const & layout = par->layout();
982 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
983 // special code for right address boxes, only newlines count
984 while (i < par->size()) {
985 if (par->isNewline(i)) {
987 i = par->size() - 1; // this means break
989 } else if (par->isInset(i) && par->getInset(i)
990 && par->getInset(i)->display()) {
991 par->getInset(i)->display(false);
996 // Last position is an invariant
997 pos_type const last = par->size();
998 // this is the usual handling
999 int x = leftMargin(bview, row);
1000 bool doitonetime = true;
1001 while (doitonetime || ((x < width) && (i < last))) {
1002 doitonetime = false;
1003 char const c = par->getChar(i);
1005 if (c == Paragraph::META_INSET)
1006 in = par->getInset(i);
1007 if (IsNewlineChar(c)) {
1009 x = width; // this means break
1010 } else if (in && !in->isChar()) {
1011 // check wether a Display() inset is
1012 // valid here. if not, change it to
1014 if (in->display() &&
1015 (layout->isCommand() ||
1016 (layout->labeltype == LABEL_MANUAL
1017 && i < beginningOfMainBody(bview->buffer(), par))))
1019 // display istn't allowd
1021 x += singleWidth(bview, par, i, c);
1022 } else if (in->display() || in->needFullRow()) {
1023 // So break the line here
1027 if (par->isLineSeparator(i+1))
1030 last_separator = last; // to avoid extra rows
1032 last_separator = i - 1;
1033 x = width; // this means break
1035 x += singleWidth(bview, par, i, c);
1036 // we have to check this separately as we could have a
1037 // lineseparator and then the algorithm below would prefer
1038 // that which IS wrong! We should always break on an inset
1039 // if it's too long and not on the last separator.
1040 // Maybe the only exeption is insets used as chars but
1041 // then we would have to have a special function inside
1042 // the inset to tell us this. Till then we leave it as
1043 // it is now. (Jug 20020106)
1044 if (pos < i && x >= width && last_separator >= 0)
1045 last_separator = i - 1;
1048 if (par->isLineSeparator(i))
1050 x += singleWidth(bview, par, i, c);
1053 if (i == main_body) {
1054 x += font_metrics::width(layout->labelsep,
1055 getLabelFont(bview->buffer(), par));
1056 if (par->isLineSeparator(i - 1))
1057 x-= singleWidth(bview, par, i - 1);
1058 int left_margin = labelEnd(bview, row);
1059 if (x < left_margin)
1063 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1064 last_separator = i - 2;
1065 else if ((pos < i) && (last_separator < 0) && (x >= width))
1066 last_separator = i - 1;
1067 // end of paragraph is always a suitable separator
1068 else if (i == last && x < width)
1072 // well, if last_separator is still 0, the line isn't breakable.
1073 // don't care and cut simply at the end
1074 if (last_separator < 0) {
1078 // manual labels cannot be broken in LaTeX, do not care
1079 if (main_body && last_separator < main_body)
1080 last_separator = main_body - 1;
1082 return last_separator;
1086 // returns the minimum space a row needs on the screen in pixel
1087 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1089 if (paper_width < 0)
1093 // get the pure distance
1094 pos_type const last = rowLastPrintable(row);
1096 // special handling of the right address boxes
1097 if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1098 int const tmpfill = row->fill();
1099 row->fill(0); // the minfill in MarginLeft()
1100 w = leftMargin(bview, row);
1103 w = leftMargin(bview, row);
1105 LyXLayout_ptr const & layout = row->par()->layout();
1107 pos_type const main_body =
1108 beginningOfMainBody(bview->buffer(), row->par());
1109 pos_type i = row->pos();
1112 if (main_body > 0 && i == main_body) {
1113 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1114 if (row->par()->isLineSeparator(i - 1))
1115 w -= singleWidth(bview, row->par(), i - 1);
1116 int left_margin = labelEnd(bview, row);
1117 if (w < left_margin)
1120 w += singleWidth(bview, row->par(), i);
1123 if (main_body > 0 && main_body > last) {
1124 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1125 if (last >= 0 && row->par()->isLineSeparator(last))
1126 w -= singleWidth(bview, row->par(), last);
1127 int const left_margin = labelEnd(bview, row);
1128 if (w < left_margin)
1132 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1137 // returns the minimum space a manual label needs on the screen in pixel
1138 int LyXText::labelFill(BufferView * bview, Row const * row) const
1140 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1141 // -1 because a label ends either with a space that is in the label,
1142 // or with the beginning of a footnote that is outside the label.
1144 // I don't understand this code in depth, but sometimes "last" is
1145 // less than 0 and this causes a crash. This fix seems to work
1146 // correctly, but I bet the real error is elsewhere. The bug is
1147 // triggered when you have an open footnote in a paragraph
1148 // environment with a manual label. (Asger)
1149 if (last < 0) last = 0;
1151 // a separator at this end does not count
1152 if (row->par()->isLineSeparator(last))
1156 pos_type i = row->pos();
1158 w += singleWidth(bview, row->par(), i);
1163 if (!row->par()->params().labelWidthString().empty()) {
1164 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1165 getLabelFont(bview->buffer(), row->par())) - w,
1173 // returns the number of separators in the specified row. The separator
1174 // on the very last column doesnt count
1175 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1177 pos_type last = rowLastPrintable(row);
1178 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1181 for (; p <= last; ++p) {
1182 if (row->par()->isSeparator(p)) {
1190 // returns the number of hfills in the specified row. The LyX-Hfill is
1191 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1192 // ignored. This is *MUCH* more usefull than not to ignore!
1193 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1195 pos_type const last = rowLast(row);
1196 pos_type first = row->pos();
1199 // hfill *DO* count at the beginning of paragraphs!
1200 while (first <= last && row->par()->isHfill(first)) {
1205 first = max(first, beginningOfMainBody(buf, row->par()));
1207 for (pos_type p = first; p <= last; ++p) {
1208 // last, because the end is ignored!
1210 if (row->par()->isHfill(p)) {
1218 // like NumberOfHfills, but only those in the manual label!
1219 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1221 pos_type last = rowLast(row);
1222 pos_type first = row->pos();
1224 // hfill *DO* count at the beginning of paragraphs!
1225 while (first < last && row->par()->isHfill(first))
1229 last = min(last, beginningOfMainBody(buf, row->par()));
1231 for (pos_type p = first; p < last; ++p) {
1232 // last, because the end is ignored!
1233 if (row->par()->isHfill(p)) {
1241 // returns true, if a expansion is needed.
1242 // Rules are given by LaTeX
1243 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1246 // by the way, is it a hfill?
1247 if (!row_ptr->par()->isHfill(pos))
1250 // at the end of a row it does not count
1251 // unless another hfill exists on the line
1252 if (pos >= rowLast(row_ptr)) {
1253 pos_type i = row_ptr->pos();
1254 while (i < pos && !row_ptr->par()->isHfill(i)) {
1262 // at the beginning of a row it does not count, if it is not
1263 // the first row of a paragaph
1264 if (!row_ptr->pos())
1267 // in some labels it does not count
1268 if (row_ptr->par()->layout()->margintype != MARGIN_MANUAL
1269 && pos < beginningOfMainBody(buf, row_ptr->par()))
1272 // if there is anything between the first char of the row and
1273 // the sepcified position that is not a newline and not a hfill,
1274 // the hfill will count, otherwise not
1275 pos_type i = row_ptr->pos();
1276 while (i < pos && (row_ptr->par()->isNewline(i)
1277 || row_ptr->par()->isHfill(i)))
1284 LColor::color LyXText::backgroundColor()
1287 return inset_owner->backgroundColor();
1289 return LColor::background;
1292 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1294 // get the maximum ascent and the maximum descent
1297 float layoutasc = 0;
1298 float layoutdesc = 0;
1301 Inset * tmpinset = 0;
1303 // ok , let us initialize the maxasc and maxdesc value.
1304 // This depends in LaTeX of the font of the last character
1305 // in the paragraph. The hack below is necessary because
1306 // of the possibility of open footnotes
1308 // Correction: only the fontsize count. The other properties
1309 // are taken from the layoutfont. Nicer on the screen :)
1310 Paragraph * par = row_ptr->par();
1311 Paragraph * firstpar = row_ptr->par();
1313 LyXLayout_ptr const & layout = firstpar->layout();
1315 // as max get the first character of this row then it can increes but not
1316 // decrees the height. Just some point to start with so we don't have to
1317 // do the assignment below too often.
1318 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1319 LyXFont::FONT_SIZE const tmpsize = font.size();
1320 font = getLayoutFont(bview->buffer(), par);
1321 LyXFont::FONT_SIZE const size = font.size();
1322 font.setSize(tmpsize);
1324 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1326 float spacing_val = 1.0;
1327 if (!row_ptr->par()->params().spacing().isDefault()) {
1328 spacing_val = row_ptr->par()->params().spacing().getValue();
1330 spacing_val = bview->buffer()->params.spacing.getValue();
1332 //lyxerr << "spacing_val = " << spacing_val << endl;
1334 int maxasc = int(font_metrics::maxAscent(font) *
1335 layout->spacing.getValue() *
1337 int maxdesc = int(font_metrics::maxDescent(font) *
1338 layout->spacing.getValue() *
1341 pos_type const pos_end = rowLast(row_ptr);
1345 // Check if any insets are larger
1346 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1347 if (row_ptr->par()->isInset(pos)) {
1348 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1349 tmpinset = row_ptr->par()->getInset(pos);
1351 #if 1 // this is needed for deep update on initialitation
1352 tmpinset->update(bview, tmpfont);
1354 asc = tmpinset->ascent(bview, tmpfont);
1355 desc = tmpinset->descent(bview, tmpfont);
1356 maxwidth += tmpinset->width(bview, tmpfont);
1357 maxasc = max(maxasc, asc);
1358 maxdesc = max(maxdesc, desc);
1361 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1365 // Check if any custom fonts are larger (Asger)
1366 // This is not completely correct, but we can live with the small,
1367 // cosmetic error for now.
1368 LyXFont::FONT_SIZE maxsize =
1369 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1370 if (maxsize > font.size()) {
1371 font.setSize(maxsize);
1373 asc = font_metrics::maxAscent(font);
1374 desc = font_metrics::maxDescent(font);
1381 // This is nicer with box insets:
1385 row_ptr->ascent_of_text(maxasc);
1387 // is it a top line?
1388 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1390 // some parksips VERY EASY IMPLEMENTATION
1391 if (bview->buffer()->params.paragraph_separation ==
1392 BufferParams::PARSEP_SKIP)
1394 if (layout->isParagraph()
1395 && firstpar->getDepth() == 0
1396 && firstpar->previous())
1398 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1399 } else if (firstpar->previous() &&
1400 firstpar->previous()->layout()->isParagraph() &&
1401 firstpar->previous()->getDepth() == 0)
1403 // is it right to use defskip here too? (AS)
1404 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1408 // the paper margins
1409 if (!row_ptr->par()->previous() && bv_owner)
1410 maxasc += PAPER_MARGIN;
1412 // add the vertical spaces, that the user added
1413 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1415 // do not forget the DTP-lines!
1416 // there height depends on the font of the nearest character
1417 if (firstpar->params().lineTop())
1419 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1421 // and now the pagebreaks
1422 if (firstpar->params().pagebreakTop())
1423 maxasc += 3 * defaultHeight();
1425 // This is special code for the chapter, since the label of this
1426 // layout is printed in an extra row
1427 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1428 && bview->buffer()->params.secnumdepth >= 0)
1430 float spacing_val = 1.0;
1431 if (!row_ptr->par()->params().spacing().isDefault()) {
1432 spacing_val = row_ptr->par()->params().spacing().getValue();
1434 spacing_val = bview->buffer()->params.spacing.getValue();
1437 labeladdon = int(font_metrics::maxDescent(labelfont) *
1438 layout->spacing.getValue() *
1440 + int(font_metrics::maxAscent(labelfont) *
1441 layout->spacing.getValue() *
1445 // special code for the top label
1446 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1447 || layout->labeltype == LABEL_BIBLIO
1448 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1449 && row_ptr->par()->isFirstInSequence()
1450 && !row_ptr->par()->getLabelstring().empty())
1452 float spacing_val = 1.0;
1453 if (!row_ptr->par()->params().spacing().isDefault()) {
1454 spacing_val = row_ptr->par()->params().spacing().getValue();
1456 spacing_val = bview->buffer()->params.spacing.getValue();
1460 (font_metrics::maxAscent(labelfont) *
1461 layout->spacing.getValue() *
1463 +(font_metrics::maxDescent(labelfont) *
1464 layout->spacing.getValue() *
1466 + layout->topsep * defaultHeight()
1467 + layout->labelbottomsep * defaultHeight());
1470 // and now the layout spaces, for example before and after a section,
1471 // or between the items of a itemize or enumerate environment
1473 if (!firstpar->params().pagebreakTop()) {
1474 Paragraph * prev = row_ptr->par()->previous();
1476 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1477 if (prev && prev->layout() == firstpar->layout() &&
1478 prev->getDepth() == firstpar->getDepth() &&
1479 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1481 layoutasc = (layout->itemsep * defaultHeight());
1482 } else if (row_ptr->previous()) {
1483 tmptop = layout->topsep;
1485 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1486 tmptop -= row_ptr->previous()->par()->layout()->bottomsep;
1489 layoutasc = (tmptop * defaultHeight());
1490 } else if (row_ptr->par()->params().lineTop()) {
1491 tmptop = layout->topsep;
1494 layoutasc = (tmptop * defaultHeight());
1497 prev = row_ptr->par()->outerHook();
1499 maxasc += int(prev->layout()->parsep * defaultHeight());
1501 if (firstpar->previous() &&
1502 firstpar->previous()->getDepth() == 0 &&
1503 firstpar->previous()->layout() !=
1507 } else if (firstpar->previous()) {
1508 maxasc += int(layout->parsep * defaultHeight());
1514 // is it a bottom line?
1515 if (row_ptr->par() == par
1516 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1518 // the paper margins
1519 if (!par->next() && bv_owner)
1520 maxdesc += PAPER_MARGIN;
1522 // add the vertical spaces, that the user added
1523 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1525 // do not forget the DTP-lines!
1526 // there height depends on the font of the nearest character
1527 if (firstpar->params().lineBottom())
1528 maxdesc += 2 * font_metrics::ascent('x',
1529 getFont(bview->buffer(),
1531 max(pos_type(0), par->size() - 1)));
1533 // and now the pagebreaks
1534 if (firstpar->params().pagebreakBottom())
1535 maxdesc += 3 * defaultHeight();
1537 // and now the layout spaces, for example before and after
1538 // a section, or between the items of a itemize or enumerate
1540 if (!firstpar->params().pagebreakBottom()
1541 && row_ptr->par()->next()) {
1542 Paragraph * nextpar = row_ptr->par()->next();
1543 Paragraph * comparepar = row_ptr->par();
1547 if (comparepar->getDepth() > nextpar->getDepth()) {
1548 usual = (comparepar->layout()->bottomsep * defaultHeight());
1549 comparepar = comparepar->depthHook(nextpar->getDepth());
1550 if (comparepar->layout()!= nextpar->layout()
1551 || nextpar->getLabelWidthString() !=
1552 comparepar->getLabelWidthString())
1554 unusual = (comparepar->layout()->bottomsep * defaultHeight());
1556 if (unusual > usual)
1557 layoutdesc = unusual;
1560 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1562 if (comparepar->layout() != nextpar->layout()
1563 || nextpar->getLabelWidthString() !=
1564 comparepar->getLabelWidthString())
1565 layoutdesc = int(comparepar->layout()->bottomsep * defaultHeight());
1570 // incalculate the layout spaces
1571 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1572 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1574 // calculate the new height of the text
1575 height -= row_ptr->height();
1577 row_ptr->height(maxasc + maxdesc + labeladdon);
1578 row_ptr->baseline(maxasc + labeladdon);
1580 height += row_ptr->height();
1582 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1585 row_ptr->width(maxwidth);
1586 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1588 row_ptr->width(int(maxwidth + x));
1591 width = max(0,workWidth(bview));
1593 if (r->width() > width)
1601 // Appends the implicit specified paragraph behind the specified row,
1602 // start at the implicit given position
1603 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1605 bool not_ready = true;
1607 // The last character position of a paragraph is an invariant so we can
1608 // safely get it here. (Asger)
1609 pos_type const lastposition = row->par()->size();
1611 // Get the next breakpoint
1612 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1616 // Insert the new row
1617 if (z < lastposition) {
1619 insertRow(row, row->par(), z);
1626 // Set the dimensions of the row
1627 // fixed fill setting now by calling inset->update() in
1628 // SingleWidth when needed!
1629 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1630 setHeightOfRow(bview, tmprow);
1632 } while (not_ready);
1636 void LyXText::breakAgain(BufferView * bview, Row * row) const
1638 bool not_ready = true;
1641 // get the next breakpoint
1642 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1645 if (z < row->par()->size()) {
1646 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1649 insertRow(row, row->par(), z);
1655 if (row->pos() == z)
1656 not_ready = false; // the rest will not change
1662 // if there are some rows too much, delete them
1663 // only if you broke the whole paragraph!
1664 Row * tmprow2 = row;
1665 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1666 tmprow2 = tmprow2->next();
1668 while (tmprow2 != row) {
1669 tmprow2 = tmprow2->previous();
1670 removeRow(tmprow2->next());
1675 // set the dimensions of the row
1676 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1677 setHeightOfRow(bview, tmprow);
1678 } while (not_ready);
1682 // this is just a little changed version of break again
1683 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1685 // get the next breakpoint
1686 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1689 if (z < row->par()->size()) {
1691 || (row->next() && row->next()->par() != row->par())) {
1694 insertRow(row, row->par(), z);
1700 if (row->pos() != z)
1704 // if there are some rows too much, delete them
1705 // only if you broke the whole paragraph!
1706 Row * tmprow2 = row;
1707 while (tmprow2->next()
1708 && tmprow2->next()->par() == row->par()) {
1709 tmprow2 = tmprow2->next();
1711 while (tmprow2 != row) {
1712 tmprow2 = tmprow2->previous();
1713 removeRow(tmprow2->next());
1717 // set the dimensions of the row
1718 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1719 setHeightOfRow(bview, tmprow);
1723 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1725 LyXTextClass const & tclass =
1726 bview->buffer()->params.getLyXTextClass();
1727 LyXLayout_ptr const & layout = cursor.par()->layout();
1729 // this is only allowed, if the current paragraph is not empty or caption
1730 // and if it has not the keepempty flag aktive
1731 if (cursor.par()->empty()
1732 && layout->labeltype != LABEL_SENSITIVE
1733 && !layout->keepempty)
1736 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1738 // Always break behind a space
1740 // It is better to erase the space (Dekel)
1741 if (cursor.pos() < cursor.par()->size()
1742 && cursor.par()->isLineSeparator(cursor.pos()))
1743 cursor.par()->erase(cursor.pos());
1744 // cursor.pos(cursor.pos() + 1);
1746 // break the paragraph
1750 keep_layout = layout->isEnvironment();
1752 // we need to set this before we insert the paragraph. IMO the
1753 // breakParagraph call should return a bool if it inserts the
1754 // paragraph before or behind and we should react on that one
1755 // but we can fix this in 1.3.0 (Jug 20020509)
1756 bool const isempty = (layout->keepempty && cursor.par()->empty());
1757 ::breakParagraph(bview->buffer()->params, cursor.par(), cursor.pos(),
1760 // well this is the caption hack since one caption is really enough
1761 if (layout->labeltype == LABEL_SENSITIVE) {
1763 // set to standard-layout
1764 cursor.par()->applyLayout(tclass.defaultLayout());
1766 // set to standard-layout
1767 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1770 // if the cursor is at the beginning of a row without prior newline,
1772 // This touches only the screen-update. Otherwise we would may have
1773 // an empty row on the screen
1774 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1775 && cursor.row()->pos() == cursor.pos())
1780 status(bview, LyXText::NEED_MORE_REFRESH);
1781 refresh_row = cursor.row();
1782 refresh_y = cursor.y() - cursor.row()->baseline();
1784 // Do not forget the special right address boxes
1785 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1786 while (refresh_row->previous() &&
1787 refresh_row->previous()->par() == refresh_row->par())
1789 refresh_row = refresh_row->previous();
1790 refresh_y -= refresh_row->height();
1793 removeParagraph(cursor.row());
1795 // set the dimensions of the cursor row
1796 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1798 setHeightOfRow(bview, cursor.row());
1800 while (!cursor.par()->next()->empty()
1801 && cursor.par()->next()->isNewline(0))
1802 cursor.par()->next()->erase(0);
1804 insertParagraph(bview, cursor.par()->next(), cursor.row());
1806 updateCounters(bview);
1808 // This check is necessary. Otherwise the new empty paragraph will
1809 // be deleted automatically. And it is more friendly for the user!
1810 if (cursor.pos() || isempty)
1811 setCursor(bview, cursor.par()->next(), 0);
1813 setCursor(bview, cursor.par(), 0);
1815 if (cursor.row()->next())
1816 breakAgain(bview, cursor.row()->next());
1822 // Just a macro to make some thing easier.
1823 void LyXText::redoParagraph(BufferView * bview) const
1826 redoParagraphs(bview, cursor, cursor.par()->next());
1827 setCursorIntern(bview, cursor.par(), cursor.pos());
1831 // insert a character, moves all the following breaks in the
1832 // same Paragraph one to the right and make a rebreak
1833 void LyXText::insertChar(BufferView * bview, char c)
1835 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1837 // When the free-spacing option is set for the current layout,
1838 // disable the double-space checking
1840 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1841 cursor.row()->par()->isFreeSpacing();
1843 if (lyxrc.auto_number) {
1844 static string const number_operators = "+-/*";
1845 static string const number_unary_operators = "+-";
1846 static string const number_seperators = ".,:";
1848 if (current_font.number() == LyXFont::ON) {
1849 if (!IsDigit(c) && !contains(number_operators, c) &&
1850 !(contains(number_seperators, c) &&
1851 cursor.pos() >= 1 &&
1852 cursor.pos() < cursor.par()->size() &&
1853 getFont(bview->buffer(),
1855 cursor.pos()).number() == LyXFont::ON &&
1856 getFont(bview->buffer(),
1858 cursor.pos() - 1).number() == LyXFont::ON)
1860 number(bview); // Set current_font.number to OFF
1861 } else if (IsDigit(c) &&
1862 real_current_font.isVisibleRightToLeft()) {
1863 number(bview); // Set current_font.number to ON
1865 if (cursor.pos() > 0) {
1866 char const c = cursor.par()->getChar(cursor.pos() - 1);
1867 if (contains(number_unary_operators, c) &&
1868 (cursor.pos() == 1 ||
1869 cursor.par()->isSeparator(cursor.pos() - 2) ||
1870 cursor.par()->isNewline(cursor.pos() - 2))
1872 setCharFont(bview->buffer(),
1876 } else if (contains(number_seperators, c) &&
1877 cursor.pos() >= 2 &&
1878 getFont(bview->buffer(),
1880 cursor.pos() - 2).number() == LyXFont::ON) {
1881 setCharFont(bview->buffer(),
1891 // First check, if there will be two blanks together or a blank at
1892 // the beginning of a paragraph.
1893 // I decided to handle blanks like normal characters, the main
1894 // difference are the special checks when calculating the row.fill
1895 // (blank does not count at the end of a row) and the check here
1897 // The bug is triggered when we type in a description environment:
1898 // The current_font is not changed when we go from label to main text
1899 // and it should (along with realtmpfont) when we type the space.
1900 // CHECK There is a bug here! (Asger)
1902 LyXFont realtmpfont = real_current_font;
1903 LyXFont rawtmpfont = current_font;
1904 // store the current font. This is because of the use of cursor
1905 // movements. The moving cursor would refresh the current font
1907 // Get the font that is used to calculate the baselineskip
1908 pos_type const lastpos = cursor.par()->size();
1909 LyXFont rawparfont =
1910 cursor.par()->getFontSettings(bview->buffer()->params,
1913 bool jumped_over_space = false;
1915 if (!freeSpacing && IsLineSeparatorChar(c)) {
1916 if ((cursor.pos() > 0
1917 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1918 || (cursor.pos() > 0
1919 && cursor.par()->isNewline(cursor.pos() - 1))
1920 || (cursor.pos() == 0)) {
1921 static bool sent_space_message = false;
1922 if (!sent_space_message) {
1923 if (cursor.pos() == 0)
1924 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1926 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1927 sent_space_message = true;
1932 } else if (IsNewlineChar(c)) {
1933 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1939 // No newline at first position of a paragraph or behind labels.
1940 // TeX does not allow that
1942 if (cursor.pos() < cursor.par()->size() &&
1943 cursor.par()->isLineSeparator(cursor.pos()))
1944 // newline always after a blank!
1946 cursor.row()->fill(-1); // to force a new break
1949 // the display inset stuff
1950 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1951 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1952 if (inset && (inset->display() || inset->needFullRow())) {
1953 // force a new break
1954 cursor.row()->fill(-1); // to force a new break
1958 // get the cursor row fist
1959 Row * row = cursor.row();
1960 int y = cursor.y() - row->baseline();
1961 if (c != Paragraph::META_INSET) {
1962 // Here case LyXText::InsertInset already insertet the character
1963 cursor.par()->insertChar(cursor.pos(), c);
1965 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1967 if (!jumped_over_space) {
1968 // refresh the positions
1970 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1971 tmprow = tmprow->next();
1972 tmprow->pos(tmprow->pos() + 1);
1976 // Is there a break one row above
1977 if (row->previous() && row->previous()->par() == row->par()
1978 && (cursor.par()->isLineSeparator(cursor.pos())
1979 || cursor.par()->isNewline(cursor.pos())
1980 || ((cursor.pos() < cursor.par()->size()) &&
1981 cursor.par()->isInset(cursor.pos()+1))
1982 || cursor.row()->fill() == -1))
1984 pos_type z = nextBreakPoint(bview,
1987 if (z >= row->pos()) {
1990 // set the dimensions of the row above
1991 row->previous()->fill(fill(bview,
1995 setHeightOfRow(bview, row->previous());
1997 y -= row->previous()->height();
1999 refresh_row = row->previous();
2000 status(bview, LyXText::NEED_MORE_REFRESH);
2002 breakAgainOneRow(bview, row);
2004 current_font = rawtmpfont;
2005 real_current_font = realtmpfont;
2006 setCursor(bview, cursor.par(), cursor.pos() + 1,
2007 false, cursor.boundary());
2008 // cursor MUST be in row now.
2010 if (row->next() && row->next()->par() == row->par())
2011 need_break_row = row->next();
2015 // check, wether the last characters font has changed.
2016 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2017 && rawparfont != rawtmpfont)
2018 redoHeightOfParagraph(bview, cursor);
2025 // recalculate the fill of the row
2026 if (row->fill() >= 0) {
2027 // needed because a newline will set fill to -1. Otherwise
2028 // we would not get a rebreak!
2029 row->fill(fill(bview, row, workWidth(bview)));
2032 if (c == Paragraph::META_INSET || row->fill() < 0) {
2035 status(bview, LyXText::NEED_MORE_REFRESH);
2036 breakAgainOneRow(bview, row);
2037 // will the cursor be in another row now?
2038 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2039 if (row->next() && row->next()->par() == row->par())
2040 // this should always be true
2042 breakAgainOneRow(bview, row);
2044 current_font = rawtmpfont;
2045 real_current_font = realtmpfont;
2047 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2049 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2050 != cursor.boundary())
2051 setCursor(bview, cursor.par(), cursor.pos(), false,
2052 !cursor.boundary());
2053 if (row->next() && row->next()->par() == row->par())
2054 need_break_row = row->next();
2061 int const tmpheight = row->height();
2062 setHeightOfRow(bview, row);
2063 if (tmpheight == row->height())
2064 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2066 status(bview, LyXText::NEED_MORE_REFRESH);
2068 current_font = rawtmpfont;
2069 real_current_font = realtmpfont;
2070 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2074 // check, wether the last characters font has changed.
2075 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2076 && rawparfont != rawtmpfont) {
2077 redoHeightOfParagraph(bview, cursor);
2079 // now the special right address boxes
2080 if (cursor.par()->layout()->margintype
2081 == MARGIN_RIGHT_ADDRESS_BOX) {
2082 redoDrawingOfParagraph(bview, cursor);
2090 void LyXText::charInserted()
2092 // Here we could call FinishUndo for every 20 characters inserted.
2093 // This is from my experience how emacs does it.
2094 static unsigned int counter;
2104 void LyXText::prepareToPrint(BufferView * bview,
2105 Row * row, float & x,
2106 float & fill_separator,
2108 float & fill_label_hfill,
2114 float w = row->fill();
2116 fill_label_hfill = 0;
2118 fill_label_hfill = 0;
2121 row->par()->isRightToLeftPar(bview->buffer()->params);
2123 x = (workWidth(bview) > 0)
2124 ? rightMargin(bview->buffer(), row) : 0;
2126 x = (workWidth(bview) > 0)
2127 ? leftMargin(bview, row) : 0;
2129 // is there a manual margin with a manual label
2130 LyXLayout_ptr const & layout = row->par()->layout();
2132 if (layout->margintype == MARGIN_MANUAL
2133 && layout->labeltype == LABEL_MANUAL) {
2134 // one more since labels are left aligned
2135 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2136 if (nlh && !row->par()->getLabelWidthString().empty()) {
2137 fill_label_hfill = labelFill(bview, row) / nlh;
2141 // are there any hfills in the row?
2142 float const nh = numberOfHfills(bview->buffer(), row);
2146 fill_hfill = w / nh;
2147 // we don't have to look at the alignment if it is ALIGN_LEFT and
2148 // if the row is already larger then the permitted width as then
2149 // we force the LEFT_ALIGN'edness!
2150 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2151 // is it block, flushleft or flushright?
2152 // set x how you need it
2154 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2155 align = layout->align;
2157 align = row->par()->params().align();
2160 // center displayed insets
2162 if (row->par()->isInset(row->pos())
2163 && (inset=row->par()->getInset(row->pos()))
2164 && (inset->display())) // || (inset->scroll() < 0)))
2165 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2166 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2167 // ERT insets should always be LEFT ALIGNED on screen
2168 inset = row->par()->inInset();
2169 if (inset && inset->owner() &&
2170 inset->owner()->lyxCode() == Inset::ERT_CODE)
2172 align = LYX_ALIGN_LEFT;
2176 case LYX_ALIGN_BLOCK:
2177 ns = numberOfSeparators(bview->buffer(), row);
2178 if (ns && row->next() && row->next()->par() == row->par() &&
2179 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2180 && !(row->next()->par()->isInset(row->next()->pos())
2181 && row->next()->par()->getInset(row->next()->pos())
2182 && row->next()->par()->getInset(row->next()->pos())->display())
2185 fill_separator = w / ns;
2186 } else if (is_rtl) {
2190 case LYX_ALIGN_RIGHT:
2193 case LYX_ALIGN_CENTER:
2201 computeBidiTables(bview->buffer(), row);
2203 pos_type main_body =
2204 beginningOfMainBody(bview->buffer(), row->par());
2205 pos_type last = rowLast(row);
2207 if (main_body > 0 &&
2208 (main_body - 1 > last ||
2209 !row->par()->isLineSeparator(main_body - 1))) {
2210 x += font_metrics::width(layout->labelsep,
2211 getLabelFont(bview->buffer(), row->par()));
2212 if (main_body - 1 <= last)
2213 x += fill_label_hfill;
2219 // important for the screen
2222 // the cursor set functions have a special mechanism. When they
2223 // realize, that you left an empty paragraph, they will delete it.
2224 // They also delete the corresponding row
2226 void LyXText::cursorRightOneWord(BufferView * bview) const
2228 // treat floats, HFills and Insets as words
2229 LyXCursor tmpcursor = cursor;
2230 // CHECK See comment on top of text.C
2232 if (tmpcursor.pos() == tmpcursor.par()->size()
2233 && tmpcursor.par()->next()) {
2234 tmpcursor.par(tmpcursor.par()->next());
2239 // Skip through initial nonword stuff.
2240 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2241 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2242 // printf("Current pos1 %d", tmpcursor.pos()) ;
2243 tmpcursor.pos(tmpcursor.pos() + 1);
2246 // Advance through word.
2247 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2248 tmpcursor.par()->isWord(tmpcursor.pos())) {
2249 // printf("Current pos2 %d", tmpcursor.pos()) ;
2250 tmpcursor.pos(tmpcursor.pos() + 1);
2254 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2258 void LyXText::cursorTab(BufferView * bview) const
2260 LyXCursor tmpcursor = cursor;
2261 while (tmpcursor.pos() < tmpcursor.par()->size()
2262 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2263 tmpcursor.pos(tmpcursor.pos() + 1);
2265 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2266 if (tmpcursor.par()->next()) {
2267 tmpcursor.par(tmpcursor.par()->next());
2271 tmpcursor.pos(tmpcursor.pos() + 1);
2272 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2276 // Skip initial whitespace at end of word and move cursor to *start*
2277 // of prior word, not to end of next prior word.
2278 void LyXText::cursorLeftOneWord(BufferView * bview) const
2280 LyXCursor tmpcursor = cursor;
2281 cursorLeftOneWord(tmpcursor);
2282 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2286 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2288 // treat HFills, floats and Insets as words
2291 && (cur.par()->isSeparator(cur.pos() - 1)
2292 || cur.par()->isKomma(cur.pos() - 1))
2293 && !(cur.par()->isHfill(cur.pos() - 1)
2294 || cur.par()->isInset(cur.pos() - 1)))
2295 cur.pos(cur.pos() - 1);
2298 && (cur.par()->isInset(cur.pos() - 1)
2299 || cur.par()->isHfill(cur.pos() - 1))) {
2300 cur.pos(cur.pos() - 1);
2301 } else if (!cur.pos()) {
2302 if (cur.par()->previous()) {
2303 cur.par(cur.par()->previous());
2304 cur.pos(cur.par()->size());
2306 } else { // Here, cur != 0
2307 while (cur.pos() > 0 &&
2308 cur.par()->isWord(cur.pos() - 1))
2309 cur.pos(cur.pos() - 1);
2314 // Select current word. This depends on behaviour of
2315 // CursorLeftOneWord(), so it is patched as well.
2316 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2317 word_location const loc) const
2319 // first put the cursor where we wana start to select the word
2322 case WHOLE_WORD_STRICT:
2323 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2324 || cursor.par()->isSeparator(cursor.pos())
2325 || cursor.par()->isKomma(cursor.pos())
2326 || cursor.par()->isSeparator(cursor.pos() - 1)
2327 || cursor.par()->isKomma(cursor.pos() - 1)) {
2331 // no break here, we go to the next
2334 // Move cursor to the beginning, when not already there.
2335 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2336 && !from.par()->isKomma(from.pos() - 1))
2337 cursorLeftOneWord(from);
2340 // always move the cursor to the beginning of previous word
2341 cursorLeftOneWord(from);
2344 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2350 while (to.pos() < to.par()->size()
2351 && !to.par()->isSeparator(to.pos())
2352 && !to.par()->isKomma(to.pos())
2353 && !to.par()->isHfill(to.pos())
2354 && !to.par()->isInset(to.pos()))
2356 to.pos(to.pos() + 1);
2361 void LyXText::selectWord(BufferView * bview, word_location const loc)
2365 getWord(from, to, loc);
2367 setCursor(bview, from.par(), from.pos());
2370 selection.cursor = cursor;
2371 setCursor(bview, to.par(), to.pos());
2372 setSelection(bview);
2376 // Select the word currently under the cursor when no
2377 // selection is currently set
2378 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2379 word_location const loc)
2381 if (!selection.set()) {
2382 selectWord(bview, loc);
2383 return selection.set();
2389 // This function is only used by the spellchecker for NextWord().
2390 // It doesn't handle LYX_ACCENTs and probably never will.
2392 LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const
2394 if (the_locking_inset) {
2395 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2396 if (!word.word().empty()) {
2397 value += float(cursor.y());
2398 value /= float(height);
2401 // we have to go on checking so move cursor to the next char
2402 if (cursor.pos() == cursor.par()->size()) {
2403 if (!cursor.par()->next())
2405 cursor.par(cursor.par()->next());
2408 cursor.pos(cursor.pos() + 1);
2410 Paragraph * tmppar = cursor.par();
2412 // If this is not the very first word, skip rest of
2413 // current word because we are probably in the middle
2414 // of a word if there is text here.
2415 if (cursor.pos() || cursor.par()->previous()) {
2416 while (cursor.pos() < cursor.par()->size()
2417 && cursor.par()->isLetter(cursor.pos()))
2418 cursor.pos(cursor.pos() + 1);
2421 // Now, skip until we have real text (will jump paragraphs)
2422 while ((cursor.par()->size() > cursor.pos()
2423 && (!cursor.par()->isLetter(cursor.pos()))
2424 && (!cursor.par()->isInset(cursor.pos()) ||
2425 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2426 || (cursor.par()->size() == cursor.pos()
2427 && cursor.par()->next()))
2429 if (cursor.pos() == cursor.par()->size()) {
2430 cursor.par(cursor.par()->next());
2433 cursor.pos(cursor.pos() + 1);
2436 // now check if we hit an inset so it has to be a inset containing text!
2437 if (cursor.pos() < cursor.par()->size() &&
2438 cursor.par()->isInset(cursor.pos()))
2441 cursor.par()->getInset(cursor.pos())->edit(bview);
2442 // now call us again to do the above trick
2443 // but obviously we have to start from down below ;)
2444 return bview->text->selectNextWordToSpellcheck(bview, value);
2447 // Update the value if we changed paragraphs
2448 if (cursor.par() != tmppar) {
2449 setCursor(bview, cursor.par(), cursor.pos());
2450 value = float(cursor.y())/float(height);
2453 // Start the selection from here
2454 selection.cursor = cursor;
2457 getFont(bview->buffer(), cursor.par(), cursor.pos())
2458 .language()->code());
2459 // and find the end of the word (insets like optional hyphens
2460 // and ligature break are part of a word)
2461 while (cursor.pos() < cursor.par()->size()
2462 && (cursor.par()->isLetter(cursor.pos())))
2463 cursor.pos(cursor.pos() + 1);
2465 // Finally, we copy the word to a string and return it
2467 if (selection.cursor.pos() < cursor.pos()) {
2469 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2470 if (!cursor.par()->isInset(i))
2471 str += cursor.par()->getChar(i);
2474 return WordLangTuple(str, lang_code);
2478 // This one is also only for the spellchecker
2479 void LyXText::selectSelectedWord(BufferView * bview)
2481 if (the_locking_inset) {
2482 the_locking_inset->selectSelectedWord(bview);
2485 // move cursor to the beginning
2486 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2488 // set the sel cursor
2489 selection.cursor = cursor;
2491 // now find the end of the word
2492 while (cursor.pos() < cursor.par()->size()
2493 && (cursor.par()->isLetter(cursor.pos())))
2494 cursor.pos(cursor.pos() + 1);
2496 setCursor(bview, cursor.par(), cursor.pos());
2498 // finally set the selection
2499 setSelection(bview);
2503 // Delete from cursor up to the end of the current or next word.
2504 void LyXText::deleteWordForward(BufferView * bview)
2506 if (cursor.par()->empty())
2509 LyXCursor tmpcursor = cursor;
2510 tmpcursor.row(0); //Â ??
2511 selection.set(true); // to avoid deletion
2512 cursorRightOneWord(bview);
2513 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2514 selection.cursor = cursor;
2516 setSelection(bview);
2518 // Great, CutSelection() gets rid of multiple spaces.
2519 cutSelection(bview, true, false);
2524 // Delete from cursor to start of current or prior word.
2525 void LyXText::deleteWordBackward(BufferView * bview)
2527 if (cursor.par()->empty())
2530 LyXCursor tmpcursor = cursor;
2531 tmpcursor.row(0); // ??
2532 selection.set(true); // to avoid deletion
2533 cursorLeftOneWord(bview);
2534 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2535 selection.cursor = cursor;
2537 setSelection(bview);
2538 cutSelection(bview, true, false);
2543 // Kill to end of line.
2544 void LyXText::deleteLineForward(BufferView * bview)
2546 if (cursor.par()->empty())
2547 // Paragraph is empty, so we just go to the right
2550 LyXCursor tmpcursor = cursor;
2551 // We can't store the row over a regular setCursor
2552 // so we set it to 0 and reset it afterwards.
2553 tmpcursor.row(0); //Â ??
2554 selection.set(true); // to avoid deletion
2556 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2557 selection.cursor = cursor;
2559 setSelection(bview);
2560 // What is this test for ??? (JMarc)
2561 if (!selection.set()) {
2562 deleteWordForward(bview);
2564 cutSelection(bview, true, false);
2570 // Change the case of a word at cursor position.
2571 // This function directly manipulates Paragraph::text because there
2572 // is no Paragraph::SetChar currently. I did what I could to ensure
2573 // that it is correct. I guess part of it should be moved to
2574 // Paragraph, but it will have to change for 1.1 anyway. At least
2575 // it does not access outside of the allocated array as the older
2576 // version did. (JMarc)
2577 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2582 if (selection.set()) {
2583 from = selection.start;
2586 getWord(from, to, PARTIAL_WORD);
2587 setCursor(bview, to.par(), to.pos() + 1);
2590 changeRegionCase(bview, from, to, action);
2594 void LyXText::changeRegionCase(BufferView * bview,
2595 LyXCursor const & from,
2596 LyXCursor const & to,
2597 LyXText::TextCase action)
2599 lyx::Assert(from <= to);
2601 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2603 pos_type pos = from.pos();
2604 Paragraph * par = from.par();
2606 while (par && (pos != to.pos() || par != to.par())) {
2607 if (pos == par->size()) {
2612 unsigned char c = par->getChar(pos);
2613 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2615 case text_lowercase:
2618 case text_capitalization:
2620 action = text_lowercase;
2622 case text_uppercase:
2627 par->setChar(pos, c);
2628 checkParagraph(bview, par, pos);
2632 if (to.row() != from.row()) {
2633 refresh_y = from.y() - from.row()->baseline();
2634 refresh_row = from.row();
2635 status(bview, LyXText::NEED_MORE_REFRESH);
2640 void LyXText::transposeChars(BufferView & bview)
2642 Paragraph * tmppar = cursor.par();
2644 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2646 pos_type tmppos = cursor.pos();
2648 // First decide if it is possible to transpose at all
2650 // We are at the beginning of a paragraph.
2651 if (tmppos == 0) return;
2653 // We are at the end of a paragraph.
2654 if (tmppos == tmppar->size() - 1) return;
2656 unsigned char c1 = tmppar->getChar(tmppos);
2657 unsigned char c2 = tmppar->getChar(tmppos - 1);
2659 if (c1 != Paragraph::META_INSET
2660 && c2 != Paragraph::META_INSET) {
2661 tmppar->setChar(tmppos, c2);
2662 tmppar->setChar(tmppos - 1, c1);
2664 // We should have an implementation that handles insets
2665 // as well, but that will have to come later. (Lgb)
2666 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2670 void LyXText::Delete(BufferView * bview)
2672 // this is a very easy implementation
2674 LyXCursor old_cursor = cursor;
2675 int const old_cur_par_id = old_cursor.par()->id();
2676 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2677 old_cursor.par()->previous()->id() : 0;
2679 // just move to the right
2682 // CHECK Look at the comment here.
2683 // This check is not very good...
2684 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2685 // and that can very well delete the par or par->previous in
2686 // old_cursor. Will a solution where we compare paragraph id's
2688 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2689 == old_cur_par_prev_id
2690 && cursor.par()->id() != old_cur_par_id) {
2691 // delete-empty-paragraph-mechanism has done it
2695 // if you had success make a backspace
2696 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2697 LyXCursor tmpcursor = cursor;
2698 // to make sure undo gets the right cursor position
2699 cursor = old_cursor;
2700 setUndo(bview, Undo::DELETE,
2701 cursor.par(), cursor.par()->next());
2708 void LyXText::backspace(BufferView * bview)
2710 // Get the font that is used to calculate the baselineskip
2711 pos_type lastpos = cursor.par()->size();
2712 LyXFont rawparfont =
2713 cursor.par()->getFontSettings(bview->buffer()->params,
2716 if (cursor.pos() == 0) {
2717 // The cursor is at the beginning of a paragraph,
2718 // so the the backspace will collapse two paragraphs into one.
2720 // we may paste some paragraphs
2722 // is it an empty paragraph?
2725 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2726 // This is an empty paragraph and we delete it just by moving the cursor one step
2727 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2728 // of the paragraph.
2730 if (cursor.par()->previous()) {
2731 Paragraph * tmppar = cursor.par()->previous();
2732 if (cursor.par()->layout() == tmppar->layout()
2733 && cursor.par()->getAlign() == tmppar->getAlign()) {
2734 // Inherit bottom DTD from the paragraph below.
2735 // (the one we are deleting)
2736 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2737 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2738 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2743 // the layout things can change the height of a row !
2744 int const tmpheight = cursor.row()->height();
2745 setHeightOfRow(bview, cursor.row());
2746 if (cursor.row()->height() != tmpheight) {
2747 refresh_y = cursor.y() - cursor.row()->baseline();
2748 refresh_row = cursor.row();
2749 status(bview, LyXText::NEED_MORE_REFRESH);
2755 if (cursor.par()->previous()) {
2756 setUndo(bview, Undo::DELETE,
2757 cursor.par()->previous(), cursor.par()->next());
2760 Paragraph * tmppar = cursor.par();
2761 Row * tmprow = cursor.row();
2763 // We used to do cursorLeftIntern() here, but it is
2764 // not a good idea since it triggers the auto-delete
2765 // mechanism. So we do a cursorLeftIntern()-lite,
2766 // without the dreaded mechanism. (JMarc)
2767 if (cursor.par()->previous()) {
2768 // steps into the above paragraph.
2769 setCursorIntern(bview, cursor.par()->previous(),
2770 cursor.par()->previous()->size(),
2774 // Pasting is not allowed, if the paragraphs have different
2775 // layout. I think it is a real bug of all other
2776 // word processors to allow it. It confuses the user.
2777 // Even so with a footnote paragraph and a non-footnote
2778 // paragraph. I will not allow pasting in this case,
2779 // because the user would be confused if the footnote behaves
2780 // different wether it is open or closed.
2782 // Correction: Pasting is always allowed with standard-layout
2783 LyXTextClass const & tclass =
2784 bview->buffer()->params.getLyXTextClass();
2786 if (cursor.par() != tmppar
2787 && (cursor.par()->layout() == tmppar->layout()
2788 || tmppar->layout() == tclass.defaultLayout())
2789 && cursor.par()->getAlign() == tmppar->getAlign()) {
2790 removeParagraph(tmprow);
2792 mergeParagraph(bview->buffer()->params, cursor.par());
2794 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2795 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2796 // strangely enough it seems that commenting out the line above removes
2797 // most or all of the segfaults. I will however also try to move the
2798 // two Remove... lines in front of the PasteParagraph too.
2801 cursor.pos(cursor.pos() - 1);
2803 status(bview, LyXText::NEED_MORE_REFRESH);
2804 refresh_row = cursor.row();
2805 refresh_y = cursor.y() - cursor.row()->baseline();
2807 // remove the lost paragraph
2808 // This one is not safe, since the paragraph that the tmprow and the
2809 // following rows belong to has been deleted by the PasteParagraph
2810 // above. The question is... could this be moved in front of the
2812 //RemoveParagraph(tmprow);
2813 //RemoveRow(tmprow);
2815 // This rebuilds the rows.
2816 appendParagraph(bview, cursor.row());
2817 updateCounters(bview);
2819 // the row may have changed, block, hfills etc.
2820 setCursor(bview, cursor.par(), cursor.pos(), false);
2823 // this is the code for a normal backspace, not pasting
2825 setUndo(bview, Undo::DELETE,
2826 cursor.par(), cursor.par()->next());
2827 // We used to do cursorLeftIntern() here, but it is
2828 // not a good idea since it triggers the auto-delete
2829 // mechanism. So we do a cursorLeftIntern()-lite,
2830 // without the dreaded mechanism. (JMarc)
2831 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2832 false, cursor.boundary());
2834 // some insets are undeletable here
2835 if (cursor.par()->isInset(cursor.pos())) {
2836 if (!cursor.par()->getInset(cursor.pos())->deletable())
2838 // force complete redo when erasing display insets
2839 // this is a cruel method but safe..... Matthias
2840 if (cursor.par()->getInset(cursor.pos())->display() ||
2841 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2842 cursor.par()->erase(cursor.pos());
2843 redoParagraph(bview);
2848 Row * row = cursor.row();
2849 int y = cursor.y() - row->baseline();
2851 // remember that a space at the end of a row doesnt count
2852 // when calculating the fill
2853 if (cursor.pos() < rowLast(row) ||
2854 !cursor.par()->isLineSeparator(cursor.pos())) {
2855 row->fill(row->fill() + singleWidth(bview,
2860 // some special code when deleting a newline. This is similar
2861 // to the behavior when pasting paragraphs
2862 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2863 cursor.par()->erase(cursor.pos());
2864 // refresh the positions
2866 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2867 tmprow = tmprow->next();
2868 tmprow->pos(tmprow->pos() - 1);
2870 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2871 cursor.pos(cursor.pos() - 1);
2873 if (cursor.pos() < cursor.par()->size()
2874 && !cursor.par()->isSeparator(cursor.pos())) {
2875 cursor.par()->insertChar(cursor.pos(), ' ');
2876 setCharFont(bview->buffer(), cursor.par(),
2877 cursor.pos(), current_font);
2878 // refresh the positions
2880 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2881 tmprow = tmprow->next();
2882 tmprow->pos(tmprow->pos() + 1);
2886 cursor.par()->erase(cursor.pos());
2888 // refresh the positions
2890 while (tmprow->next()
2891 && tmprow->next()->par() == row->par()) {
2892 tmprow = tmprow->next();
2893 tmprow->pos(tmprow->pos() - 1);
2896 // delete newlines at the beginning of paragraphs
2897 while (!cursor.par()->empty() &&
2898 cursor.par()->isNewline(cursor.pos()) &&
2899 cursor.pos() == beginningOfMainBody(bview->buffer(),
2901 cursor.par()->erase(cursor.pos());
2902 // refresh the positions
2904 while (tmprow->next() &&
2905 tmprow->next()->par() == row->par()) {
2906 tmprow = tmprow->next();
2907 tmprow->pos(tmprow->pos() - 1);
2912 // is there a break one row above
2913 if (row->previous() && row->previous()->par() == row->par()) {
2914 z = nextBreakPoint(bview, row->previous(),
2916 if (z >= row->pos()) {
2919 Row * tmprow = row->previous();
2921 // maybe the current row is now empty
2922 if (row->pos() >= row->par()->size()) {
2927 breakAgainOneRow(bview, row);
2928 if (row->next() && row->next()->par() == row->par())
2929 need_break_row = row->next();
2934 // set the dimensions of the row above
2935 y -= tmprow->height();
2936 tmprow->fill(fill(bview, tmprow,
2938 setHeightOfRow(bview, tmprow);
2941 refresh_row = tmprow;
2942 status(bview, LyXText::NEED_MORE_REFRESH);
2943 setCursor(bview, cursor.par(), cursor.pos(),
2944 false, cursor.boundary());
2945 //current_font = rawtmpfont;
2946 //real_current_font = realtmpfont;
2947 // check, whether the last character's font has changed.
2949 cursor.par()->getFontSettings(bview->buffer()->params,
2950 cursor.par()->size() - 1))
2951 redoHeightOfParagraph(bview, cursor);
2956 // break the cursor row again
2957 if (row->next() && row->next()->par() == row->par() &&
2958 (rowLast(row) == row->par()->size() - 1 ||
2959 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2961 // it can happen that a paragraph loses one row
2962 // without a real breakup. This is when a word
2963 // is to long to be broken. Well, I don t care this
2965 if (rowLast(row) == row->par()->size() - 1)
2966 removeRow(row->next());
2970 status(bview, LyXText::NEED_MORE_REFRESH);
2972 breakAgainOneRow(bview, row);
2973 // will the cursor be in another row now?
2974 if (row->next() && row->next()->par() == row->par() &&
2975 rowLast(row) <= cursor.pos()) {
2977 breakAgainOneRow(bview, row);
2980 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2982 if (row->next() && row->next()->par() == row->par())
2983 need_break_row = row->next();
2987 // set the dimensions of the row
2988 row->fill(fill(bview, row, workWidth(bview)));
2989 int const tmpheight = row->height();
2990 setHeightOfRow(bview, row);
2991 if (tmpheight == row->height())
2992 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2994 status(bview, LyXText::NEED_MORE_REFRESH);
2997 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3001 // current_font = rawtmpfont;
3002 // real_current_font = realtmpfont;
3004 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
3005 != cursor.boundary())
3006 setCursor(bview, cursor.par(), cursor.pos(), false,
3007 !cursor.boundary());
3009 lastpos = cursor.par()->size();
3010 if (cursor.pos() == lastpos)
3011 setCurrentFont(bview);
3013 // check, whether the last characters font has changed.
3015 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3016 redoHeightOfParagraph(bview, cursor);
3018 // now the special right address boxes
3019 if (cursor.par()->layout()->margintype
3020 == MARGIN_RIGHT_ADDRESS_BOX) {
3021 redoDrawingOfParagraph(bview, cursor);
3027 bool LyXText::paintRowBackground(DrawRowParams & p)
3029 bool clear_area = true;
3031 LyXFont font(LyXFont::ALL_SANE);
3033 pos_type const last = rowLastPrintable(p.row);
3035 if (!p.bv->screen().forceClear() && last == p.row->pos()
3036 && p.row->par()->isInset(p.row->pos())) {
3037 inset = p.row->par()->getInset(p.row->pos());
3039 clear_area = inset->doClearArea();
3049 int const y = p.yo < 0 ? 0 : p.yo;
3050 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3051 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3058 int h = p.row->baseline() - inset->ascent(p.bv, font);
3060 // first clear the whole row above the inset!
3062 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3065 // clear the space below the inset!
3066 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3067 if ((p.row->height() - h) > 0) {
3068 p.pain->fillRectangle(p.xo, p.yo + h,
3069 p.width, p.row->height() - h, backgroundColor());
3072 // clear the space behind the inset, if needed
3073 if (!inset->display() && !inset->needFullRow()) {
3074 int const xp = int(p.x) + inset->width(p.bv, font);
3075 if (p.width - xp > 0) {
3076 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3077 p.row->height(), backgroundColor());
3085 void LyXText::paintRowSelection(DrawRowParams & p)
3087 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3089 // the current selection
3090 int const startx = selection.start.x();
3091 int const endx = selection.end.x();
3092 int const starty = selection.start.y();
3093 int const endy = selection.end.y();
3094 Row const * startrow = selection.start.row();
3095 Row const * endrow = selection.end.row();
3099 if (bidi_same_direction) {
3103 int h = row->height();
3105 if (startrow == row && endrow == row) {
3106 if (startx < endx) {
3109 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3113 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3115 } else if (startrow == row) {
3116 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3117 int const w = (is_rtl) ? startx : (p.width - startx);
3118 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3119 } else if (endrow == row) {
3120 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3121 int const w = (is_rtl) ? (p.width - endx) : endx;
3122 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3123 } else if (p.y > starty && p.y < endy) {
3124 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3127 } else if (startrow != row && endrow != row) {
3128 if (p.y > starty && p.y < endy) {
3130 int h = row->height();
3131 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3136 if ((startrow != row && !is_rtl) || (endrow != row && is_rtl))
3137 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3139 Buffer const * buffer = p.bv->buffer();
3140 Paragraph * par = row->par();
3141 pos_type main_body = beginningOfMainBody(buffer, par);
3142 pos_type const last = rowLastPrintable(row);
3145 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3146 pos_type pos = vis2log(vpos);
3147 float const old_tmpx = tmpx;
3148 if (main_body > 0 && pos == main_body - 1) {
3149 LyXLayout_ptr const & layout = par->layout();
3150 LyXFont const lfont = getLabelFont(buffer, par);
3152 tmpx += p.label_hfill + font_metrics::width(layout->labelsep, lfont);
3154 if (par->isLineSeparator(main_body - 1))
3155 tmpx -= singleWidth(p.bv, par, main_body - 1);
3158 if (hfillExpansion(buffer, row, pos)) {
3159 tmpx += singleWidth(p.bv, par, pos);
3160 if (pos >= main_body)
3163 tmpx += p.label_hfill;
3166 else if (par->isSeparator(pos)) {
3167 tmpx += singleWidth(p.bv, par, pos);
3168 if (pos >= main_body)
3169 tmpx += p.separator;
3171 tmpx += singleWidth(p.bv, par, pos);
3174 if ((startrow != row || selection.start.pos() <= pos) &&
3175 (endrow != row || pos < selection.end.pos())) {
3176 // Here we do not use p.x as p.xo was added to p.x.
3177 p.pain->fillRectangle(int(old_tmpx), p.yo,
3178 int(tmpx - old_tmpx + 1),
3179 row->height(), LColor::selection);
3183 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3184 p.pain->fillRectangle(p.xo + int(tmpx),
3185 p.yo, int(p.bv->workWidth() - tmpx),
3186 row->height(), LColor::selection);
3191 void LyXText::paintRowAppendix(DrawRowParams & p)
3193 // FIXME: can be just p.width ?
3194 int const ww = p.bv->workWidth();
3195 Paragraph * firstpar = p.row->par();
3197 if (firstpar->params().appendix()) {
3198 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3199 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3204 void LyXText::paintRowDepthBar(DrawRowParams & p)
3206 Paragraph::depth_type const depth = p.row->par()->getDepth();
3211 Paragraph::depth_type prev_depth = 0;
3212 if (p.row->previous())
3213 prev_depth = p.row->previous()->par()->getDepth();
3214 Paragraph::depth_type next_depth = 0;
3216 next_depth = p.row->next()->par()->getDepth();
3218 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3219 int const x = (PAPER_MARGIN / 5) * i + p.xo;
3220 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3222 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3224 int const w = PAPER_MARGIN / 5;
3226 if (i > prev_depth) {
3227 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3229 if (i > next_depth) {
3230 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3236 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3238 if (vsp.kind() == VSpace::NONE)
3241 int const arrow_size = 4;
3242 int const space_size = int(vsp.inPixels(bv));
3246 int const min_size = max(3 * arrow_size,
3247 font_metrics::maxAscent(font)
3248 + font_metrics::maxDescent(font));
3250 if (vsp.length().len().value() < 0.0)
3253 return max(min_size, space_size);
3257 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3258 VSpace const & vsp, int start)
3260 if (vsp.kind() == VSpace::NONE)
3263 int const arrow_size = 4;
3264 int const size = getLengthMarkerHeight(p.bv, vsp);
3265 int const end = start + size;
3267 // the label to display (if any)
3269 // y-values for top arrow
3271 // y-values for bottom arrow
3274 str = prefix + " (" + vsp.asLyXCommand() + ")";
3276 switch (vsp.kind()) {
3277 case VSpace::LENGTH: {
3278 // adding or removing space
3279 bool const added = !(vsp.length().len().value() < 0.0);
3280 ty1 = added ? (start + arrow_size) : start;
3281 ty2 = added ? start : (start + arrow_size);
3282 by1 = added ? (end - arrow_size) : end;
3283 by2 = added ? end : (end - arrow_size);
3292 int const leftx = p.xo + leftMargin(p.bv, p.row);
3293 int const midx = leftx + arrow_size;
3294 int const rightx = midx + arrow_size;
3302 font.setColor(LColor::added_space).decSize();
3303 font_metrics::rectText(str, font, w, a, d);
3305 p.pain->rectText(leftx + 2 * arrow_size + 5,
3306 start + ((end - start) / 2) + d,
3309 if (vsp.kind() != VSpace::LENGTH && vsp.kind() != VSpace::VFILL )
3313 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3314 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3317 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3318 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3321 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3327 int LyXText::paintPageBreak(string const & label, int y, DrawRowParams & p)
3330 pb_font.setColor(LColor::pagebreak).decSize();
3335 font_metrics::rectText(label, pb_font, w, a, d);
3337 int const text_start = p.xo + ((p.width - w) / 2);
3338 int const text_end = text_start + w;
3340 p.pain->rectText(text_start, y + d, label, pb_font);
3342 p.pain->line(p.xo, y, text_start, y,
3343 LColor::pagebreak, Painter::line_onoffdash);
3344 p.pain->line(text_end, y, p.xo + p.width, y,
3345 LColor::pagebreak, Painter::line_onoffdash);
3347 return 3 * defaultHeight();
3351 void LyXText::paintFirstRow(DrawRowParams & p)
3353 Paragraph * par = p.row->par();
3354 ParagraphParameters const & parparams = par->params();
3356 // start of appendix?
3357 if (parparams.startOfAppendix()) {
3358 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3363 // think about the margins
3364 if (!p.row->previous() && bv_owner)
3365 y_top += PAPER_MARGIN;
3367 // draw a top pagebreak
3368 if (parparams.pagebreakTop()) {
3369 y_top += paintPageBreak(_("Page Break (top)"),
3370 p.yo + y_top + 2 * defaultHeight(), p);
3373 // draw the additional space if needed:
3374 y_top += drawLengthMarker(p, _("Space above"),
3375 parparams.spaceTop(), p.yo + y_top);
3377 Buffer const * buffer = p.bv->buffer();
3379 LyXLayout_ptr const & layout = par->layout();
3381 // think about the parskip
3382 // some parskips VERY EASY IMPLEMENTATION
3383 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3384 if (par->previous()) {
3385 if (layout->latextype == LATEX_PARAGRAPH
3386 && !par->getDepth()) {
3387 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3389 LyXLayout_ptr const & playout =
3390 par->previous()->layout();
3391 if (playout->latextype == LATEX_PARAGRAPH
3392 && !par->previous()->getDepth()) {
3393 // is it right to use defskip here, too? (AS)
3394 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3400 int const ww = p.bv->workWidth();
3403 if (parparams.lineTop()) {
3404 LyXFont font(LyXFont::ALL_SANE);
3405 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3409 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3410 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3411 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3412 LColor::topline, Painter::line_solid,
3413 Painter::line_thick);
3418 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3420 // should we print a label?
3421 if (layout->labeltype >= LABEL_STATIC
3422 && (layout->labeltype != LABEL_STATIC
3423 || layout->latextype != LATEX_ENVIRONMENT
3424 || par->isFirstInSequence())) {
3426 LyXFont font = getLabelFont(buffer, par);
3427 if (!par->getLabelstring().empty()) {
3429 string const str = par->getLabelstring();
3431 // this is special code for the chapter layout. This is
3432 // printed in an extra row and has a pagebreak at
3434 if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3435 if (buffer->params.secnumdepth >= 0) {
3436 float spacing_val = 1.0;
3437 if (!parparams.spacing().isDefault()) {
3438 spacing_val = parparams.spacing().getValue();
3440 spacing_val = buffer->params.spacing.getValue();
3444 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
3445 + int(layout->parsep) * defaultHeight();
3448 x = ww - leftMargin(p.bv, p.row) -
3449 font_metrics::width(str, font);
3452 p.pain->text(int(x),
3453 p.yo + p.row->baseline() -
3454 p.row->ascent_of_text() - maxdesc,
3459 x = ww - leftMargin(p.bv, p.row)
3460 + font_metrics::width(layout->labelsep, font);
3462 x = p.x - font_metrics::width(layout->labelsep, font)
3463 - font_metrics::width(str, font);
3466 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3469 // the labels at the top of an environment.
3470 // More or less for bibliography
3471 } else if (par->isFirstInSequence() &&
3472 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3473 layout->labeltype == LABEL_BIBLIO ||
3474 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3475 LyXFont font = getLabelFont(buffer, par);
3476 if (!par->getLabelstring().empty()) {
3477 string const str = par->getLabelstring();
3478 float spacing_val = 1.0;
3479 if (!parparams.spacing().isDefault()) {
3480 spacing_val = parparams.spacing().getValue();
3482 spacing_val = buffer->params.spacing.getValue();
3486 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
3487 + (layout->labelbottomsep * defaultHeight()));
3490 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3491 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3492 + ww - rightMargin(buffer, p.row)) / 2;
3493 x -= font_metrics::width(str, font) / 2;
3494 } else if (is_rtl) {
3495 x = ww - leftMargin(p.bv, p.row) -
3496 font_metrics::width(str, font);
3498 p.pain->text(int(x), p.yo + p.row->baseline()
3499 - p.row->ascent_of_text() - maxdesc,
3504 if (layout->labeltype == LABEL_BIBLIO && par->bibkey) {
3505 LyXFont font = getLayoutFont(buffer, par);
3508 x = ww - leftMargin(p.bv, p.row)
3509 + font_metrics::width(layout->labelsep, font);
3511 x = p.x - font_metrics::width(layout->labelsep, font)
3512 - par->bibkey->width(p.bv, font);
3514 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3519 void LyXText::paintLastRow(DrawRowParams & p)
3521 Paragraph * par = p.row->par();
3522 ParagraphParameters const & parparams = par->params();
3523 int y_bottom = p.row->height() - 1;
3525 // think about the margins
3526 if (!p.row->next() && bv_owner)
3527 y_bottom -= PAPER_MARGIN;
3529 int const ww = p.bv->workWidth();
3531 // draw a bottom pagebreak
3532 if (parparams.pagebreakBottom()) {
3533 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
3534 p.yo + y_bottom - 2 * defaultHeight(), p);
3537 // draw the additional space if needed:
3538 int const height = getLengthMarkerHeight(p.bv,
3539 parparams.spaceBottom());
3540 y_bottom -= drawLengthMarker(p, _("Space below"),
3541 parparams.spaceBottom(),
3542 p.yo + y_bottom - height);
3544 Buffer const * buffer = p.bv->buffer();
3546 // draw a bottom line
3547 if (parparams.lineBottom()) {
3548 LyXFont font(LyXFont::ALL_SANE);
3549 int const asc = font_metrics::ascent('x',
3550 getFont(buffer, par,
3551 max(pos_type(0), par->size() - 1)));
3555 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3556 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3557 int const y = p.yo + y_bottom;
3558 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3559 Painter::line_thick);
3564 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3565 int const endlabel = par->getEndLabel();
3570 case END_LABEL_FILLED_BOX:
3572 LyXFont const font = getLabelFont(buffer, par);
3573 int const size = int(0.75 * font_metrics::maxAscent(font));
3574 int const y = (p.yo + p.row->baseline()) - size;
3575 int x = is_rtl ? PAPER_MARGIN : ww - PAPER_MARGIN - size;
3577 if (p.row->fill() <= size)
3578 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3580 if (endlabel == END_LABEL_BOX) {
3581 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3583 p.pain->fillRectangle(x, y, size, size,
3588 case END_LABEL_STATIC:
3591 LyXFont font(LyXFont::ALL_SANE);
3592 font = getLabelFont(buffer, par);
3594 LyXFont font = getLabelFont(buffer, par);
3596 string const & str = par->layout()->endlabelstring();
3597 int const x = is_rtl ?
3598 int(p.x) - font_metrics::width(str, font)
3599 : ww - rightMargin(buffer, p.row) - p.row->fill();
3600 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3603 case END_LABEL_NO_LABEL:
3609 void LyXText::paintRowText(DrawRowParams & p)
3611 Paragraph * par = p.row->par();
3612 Buffer const * buffer = p.bv->buffer();
3614 pos_type const last = rowLastPrintable(p.row);
3615 pos_type main_body =
3616 beginningOfMainBody(buffer, par);
3617 if (main_body > 0 &&
3618 (main_body - 1 > last ||
3619 !par->isLineSeparator(main_body - 1))) {
3623 LyXLayout_ptr const & layout = par->layout();
3625 pos_type vpos = p.row->pos();
3626 while (vpos <= last) {
3627 if (p.x > p.bv->workWidth())
3629 pos_type pos = vis2log(vpos);
3630 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3631 p.x += singleWidth(p.bv, par, pos);
3635 if (main_body > 0 && pos == main_body - 1) {
3636 int const lwidth = font_metrics::width(layout->labelsep,
3637 getLabelFont(buffer, par));
3639 p.x += p.label_hfill + lwidth
3640 - singleWidth(p.bv, par, main_body - 1);
3643 if (par->isHfill(pos)) {
3646 int const y0 = p.yo + p.row->baseline();
3647 int const y1 = y0 - defaultHeight() / 2;
3649 p.pain->line(int(p.x), y1, int(p.x), y0,
3650 LColor::added_space);
3652 if (hfillExpansion(buffer, p.row, pos)) {
3653 int const y2 = (y0 + y1) / 2;
3655 if (pos >= main_body) {
3656 p.pain->line(int(p.x), y2,
3657 int(p.x + p.hfill), y2,
3658 LColor::added_space,
3659 Painter::line_onoffdash);
3662 p.pain->line(int(p.x), y2,
3663 int(p.x + p.label_hfill), y2,
3664 LColor::added_space,
3665 Painter::line_onoffdash);
3666 p.x += p.label_hfill;
3668 p.pain->line(int(p.x), y1,
3670 LColor::added_space);
3674 } else if (par->isSeparator(pos)) {
3675 p.x += singleWidth(p.bv, par, pos);
3676 if (pos >= main_body)
3687 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3688 Row * row, int y, bool cleared)
3690 if (row->height() <= 0) {
3691 lyxerr << "LYX_ERROR: row.height: "
3692 << row->height() << endl;
3698 // set up drawing parameters
3700 p.pain = &bv->painter();
3704 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3705 if (inset_owner && (p.x < 0))
3709 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3710 p.cleared = cleared;
3714 // clear to background if necessary
3715 p.cleared = paintRowBackground(p);
3717 // paint the selection background
3718 if (selection.set()) {
3719 paintRowSelection(p);
3722 // vertical lines for appendix
3723 paintRowAppendix(p);
3725 // environment depth brackets
3726 paintRowDepthBar(p);
3728 // draw any stuff wanted for a first row of a paragraph
3733 // draw any stuff wanted for the last row of a paragraph
3734 if (!row->next() || (row->next()->par() != row->par())) {
3743 int LyXText::defaultHeight() const
3745 LyXFont font(LyXFont::ALL_SANE);
3746 return int(font_metrics::maxAscent(font)
3747 + font_metrics::maxDescent(font) * 1.5);
3751 // returns the column near the specified x-coordinate of the row
3752 // x is set to the real beginning of this column
3754 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3755 bool & boundary) const
3758 float fill_separator;
3760 float fill_label_hfill;
3762 prepareToPrint(bview, row, tmpx, fill_separator,
3763 fill_hfill, fill_label_hfill);
3765 pos_type vc = row->pos();
3766 pos_type last = rowLastPrintable(row);
3769 LyXLayout_ptr const & layout = row->par()->layout();
3771 bool left_side = false;
3773 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3774 float last_tmpx = tmpx;
3776 if (main_body > 0 &&
3777 (main_body - 1 > last ||
3778 !row->par()->isLineSeparator(main_body - 1)))
3781 while (vc <= last && tmpx <= x) {
3784 if (main_body > 0 && c == main_body-1) {
3785 tmpx += fill_label_hfill +
3786 font_metrics::width(layout->labelsep,
3787 getLabelFont(bview->buffer(), row->par()));
3788 if (row->par()->isLineSeparator(main_body - 1))
3789 tmpx -= singleWidth(bview, row->par(), main_body-1);
3792 if (hfillExpansion(bview->buffer(), row, c)) {
3793 tmpx += singleWidth(bview, row->par(), c);
3797 tmpx += fill_label_hfill;
3799 else if (row->par()->isSeparator(c)) {
3800 tmpx += singleWidth(bview, row->par(), c);
3802 tmpx+= fill_separator;
3804 tmpx += singleWidth(bview, row->par(), c);
3808 if ((tmpx + last_tmpx) / 2 > x) {
3813 if (vc > last + 1) // This shouldn't happen.
3817 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3818 // some speedup if rtl_support=false
3819 && (!row->next() || row->next()->par() != row->par());
3820 bool const rtl = (lastrow)
3821 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3822 : false; // If lastrow is false, we don't need to compute
3823 // the value of rtl.
3825 if (row->pos() > last) // Row is empty?
3828 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3829 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3831 else if (vc == row->pos()) {
3833 if (bidi_level(c) % 2 == 1)
3836 c = vis2log(vc - 1);
3837 bool const rtl = (bidi_level(c) % 2 == 1);
3838 if (left_side == rtl) {
3840 boundary = isBoundary(bview->buffer(), row->par(), c);
3844 if (row->pos() <= last && c > last
3845 && row->par()->isNewline(last)) {
3846 if (bidi_level(last) % 2 == 0)
3847 tmpx -= singleWidth(bview, row->par(), last);
3849 tmpx += singleWidth(bview, row->par(), last);
3859 // returns pointer to a specified row
3860 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3865 Row * tmprow = firstrow;
3868 // find the first row of the specified paragraph
3869 while (tmprow->next() && tmprow->par() != par) {
3870 y += tmprow->height();
3871 tmprow = tmprow->next();
3874 // now find the wanted row
3875 while (tmprow->pos() < pos
3877 && tmprow->next()->par() == par
3878 && tmprow->next()->pos() <= pos) {
3879 y += tmprow->height();
3880 tmprow = tmprow->next();
3887 Row * LyXText::getRowNearY(int & y) const
3890 // If possible we should optimize this method. (Lgb)
3891 Row * tmprow = firstrow;
3894 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3895 tmpy += tmprow->height();
3896 tmprow = tmprow->next();
3899 y = tmpy; // return the real y
3901 //lyxerr << "returned y = " << y << endl;
3905 // Search from the current cursor position.
3907 Row * tmprow = cursor.row();
3908 int tmpy = cursor.y() - tmprow->baseline();
3910 lyxerr << "cursor.y() = " << tmpy << endl;
3911 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3912 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3913 lyxerr << "first = " << first << endl;
3914 lyxerr << "y = " << y << endl;
3917 lyxerr << "up" << endl;
3919 tmpy -= tmprow->height();
3920 tmprow = tmprow->previous();
3921 } while (tmprow && tmpy - tmprow->height() >= y);
3922 } else if (y > tmpy) {
3923 lyxerr << "down" << endl;
3925 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3926 tmpy += tmprow->height();
3927 tmprow = tmprow->next();
3930 lyxerr << "equal" << endl;
3933 y = tmpy; // return the real y
3935 lyxerr << "returned y = " << y << endl;
3943 int LyXText::getDepth() const
3945 return cursor.par()->getDepth();