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 LYX_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 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
156 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
158 if (Encodings::is_arabic(prev_char))
159 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
161 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
165 // This is the comments that some of the warnings below refers to.
166 // There are some issues in this file and I don't think they are
167 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
168 // this is a problem that has been here almost from day one and that a
169 // larger userbase with differenct access patters triggers the bad
170 // behaviour. (segfaults.) What I think happen is: In several places
171 // we store the paragraph in the current cursor and then moves the
172 // cursor. This movement of the cursor will delete paragraph at the
173 // old position if it is now empty. This will make the temporary
174 // pointer to the old cursor paragraph invalid and dangerous to use.
175 // And is some cases this will trigger a segfault. I have marked some
176 // of the cases where this happens with a warning, but I am sure there
177 // are others in this file and in text2.C. There is also a note in
178 // Delete() that you should read. In Delete I store the paragraph->id
179 // instead of a pointer to the paragraph. I am pretty sure this faulty
180 // use of temporary pointers to paragraphs that might have gotten
181 // invalidated (through a cursor movement) before they are used, are
182 // the cause of the strange crashes we get reported often.
184 // It is very tiresom to change this code, especially when it is as
185 // hard to read as it is. Help to fix all the cases where this is done
186 // would be greately appreciated.
190 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
193 char const c = par->getChar(pos);
194 return singleWidth(bview, par, pos, c);
198 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
199 pos_type pos, char c) const
201 LyXFont const font = getFont(bview->buffer(), par, pos);
203 // The most common case is handled first (Asger)
204 if (IsPrintable(c)) {
205 if (font.language()->RightToLeft()) {
206 if (font.language()->lang() == "arabic" &&
207 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
208 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
209 if (Encodings::IsComposeChar_arabic(c))
212 c = transformChar(c, par, pos);
213 else if (font.language()->lang() == "hebrew" &&
214 Encodings::IsComposeChar_hebrew(c))
217 return font_metrics::width(c, font);
219 } else if (IsHfillChar(c)) {
220 // Because of the representation as vertical lines
222 } else if (c == Paragraph::META_INSET) {
223 Inset * tmpinset = par->getInset(pos);
226 // this IS needed otherwise on initialitation we don't get the fill
227 // of the row right (ONLY on initialization if we read a file!)
228 // should be changed! (Jug 20011204)
229 tmpinset->update(bview, font);
231 return tmpinset->width(bview, font);
235 } else if (IsSeparatorChar(c))
237 else if (IsNewlineChar(c))
239 return font_metrics::width(c, font);
243 // Returns the paragraph position of the last character in the specified row
244 pos_type LyXText::rowLast(Row const * row) const
246 if (!row->next() || row->next()->par() != row->par()) {
247 return row->par()->size() - 1;
249 return row->next()->pos() - 1;
254 pos_type LyXText::rowLastPrintable(Row const * row) const
256 pos_type const last = rowLast(row);
257 bool ignore_the_space_on_the_last_position = true;
259 // we have to consider a space on the last position in this case!
260 if (row->next() && row->par() == row->next()->par() &&
261 row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
262 (ins=row->next()->par()->getInset(last+1)) &&
263 (ins->needFullRow() || ins->display()))
265 ignore_the_space_on_the_last_position = false;
267 if (last >= row->pos()
269 && row->next()->par() == row->par()
270 && row->par()->isSeparator(last)
271 && ignore_the_space_on_the_last_position)
278 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
280 bidi_same_direction = true;
281 if (!lyxrc.rtl_support) {
286 Inset * inset = row->par()->inInset();
287 if (inset && inset->owner() &&
288 inset->owner()->lyxCode() == Inset::ERT_CODE) {
293 bidi_start = row->pos();
294 bidi_end = rowLastPrintable(row);
296 if (bidi_start > bidi_end) {
301 if (bidi_end + 2 - bidi_start >
302 static_cast<pos_type>(log2vis_list.size())) {
304 (bidi_end + 2 - bidi_start < 500) ?
305 500 : 2 * (bidi_end + 2 - bidi_start);
306 log2vis_list.resize(new_size);
307 vis2log_list.resize(new_size);
308 bidi_levels.resize(new_size);
311 vis2log_list[bidi_end + 1 - bidi_start] = -1;
312 log2vis_list[bidi_end + 1 - bidi_start] = -1;
316 row->par()->isRightToLeftPar(buf->params);
320 pos_type const main_body = beginningOfMainBody(buf, row->par());
322 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
323 bool is_space = row->par()->isLineSeparator(lpos);
325 (is_space && lpos + 1 <= bidi_end &&
326 !row->par()->isLineSeparator(lpos + 1) &&
327 !row->par()->isNewline(lpos + 1))
329 LyXFont font = row->par()->getFontSettings(buf->params, pos);
330 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
331 font.number() == LyXFont::ON &&
332 row->par()->getFontSettings(buf->params, lpos - 1).number()
334 font = row->par()->getFontSettings(buf->params, lpos);
339 bool new_rtl = font.isVisibleRightToLeft();
340 bool new_rtl0 = font.isRightToLeft();
343 if (lpos == main_body - 1
344 && row->pos() < main_body - 1
346 new_level = (rtl_par) ? 1 : 0;
347 new_rtl = new_rtl0 = rtl_par;
349 new_level = (new_rtl) ? 1 : 2;
351 new_level = (rtl_par) ? 2 : 0;
353 if (is_space && new_level >= level) {
359 int new_level2 = new_level;
361 if (level == new_level && rtl0 != new_rtl0) {
363 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
364 } else if (level < new_level) {
365 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
366 if (new_level > rtl_par)
367 bidi_same_direction = false;
369 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
372 bidi_levels[lpos - bidi_start] = new_level;
374 while (level > new_level2) {
375 pos_type old_lpos = stack[--level];
376 int delta = lpos - old_lpos - 1;
379 log2vis_list[lpos - bidi_start] += delta;
380 log2vis_list[old_lpos - bidi_start] += delta;
382 while (level < new_level)
383 stack[level++] = lpos;
387 pos_type const old_lpos = stack[--level];
388 int delta = bidi_end - old_lpos;
391 log2vis_list[old_lpos - bidi_start] += delta;
394 pos_type vpos = bidi_start - 1;
395 for (pos_type lpos = bidi_start;
396 lpos <= bidi_end; ++lpos) {
397 vpos += log2vis_list[lpos - bidi_start];
398 vis2log_list[vpos - bidi_start] = lpos;
399 log2vis_list[lpos - bidi_start] = vpos;
404 // This method requires a previous call to ComputeBidiTables()
405 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
408 if (!lyxrc.rtl_support || pos == 0)
411 if (!bidi_InRange(pos - 1)) {
412 /// This can happen if pos is the first char of a row.
413 /// Returning false in this case is incorrect!
417 bool const rtl = bidi_level(pos - 1) % 2;
418 bool const rtl2 = bidi_InRange(pos)
419 ? bidi_level(pos) % 2
420 : par->isRightToLeftPar(buf->params);
425 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
426 pos_type pos, LyXFont const & font) const
428 if (!lyxrc.rtl_support)
429 return false; // This is just for speedup
431 bool const rtl = font.isVisibleRightToLeft();
432 bool const rtl2 = bidi_InRange(pos)
433 ? bidi_level(pos) % 2
434 : par->isRightToLeftPar(buf->params);
439 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
441 // Draw end-of-line marker
442 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
443 int const wid = font_metrics::width('n', font);
444 int const asc = font_metrics::maxAscent(font);
445 int const y = p.yo + p.row->baseline();
449 yp[0] = int(y - 0.875 * asc * 0.75);
450 yp[1] = int(y - 0.500 * asc * 0.75);
451 yp[2] = int(y - 0.125 * asc * 0.75);
453 if (bidi_level(pos) % 2 == 0) {
454 xp[0] = int(p.x + wid * 0.375);
456 xp[2] = int(p.x + wid * 0.375);
458 xp[0] = int(p.x + wid * 0.625);
459 xp[1] = int(p.x + wid);
460 xp[2] = int(p.x + wid * 0.625);
463 p.pain->lines(xp, yp, 3, LColor::eolmarker);
465 yp[0] = int(y - 0.500 * asc * 0.75);
466 yp[1] = int(y - 0.500 * asc * 0.75);
467 yp[2] = int(y - asc * 0.75);
469 if (bidi_level(pos) % 2 == 0) {
471 xp[1] = int(p.x + wid);
472 xp[2] = int(p.x + wid);
474 xp[0] = int(p.x + wid);
479 p.pain->lines(xp, yp, 3, LColor::eolmarker);
485 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
487 Inset * inset = p.row->par()->getInset(pos);
489 // FIXME: shouldn't happen
494 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
495 // we need this here as the row pointer may be illegal
496 // at a later time (Jug20020502)
497 Row * prev = p.row->previous();
499 inset->update(p.bv, font, false);
500 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
502 if (!need_break_row && !inset_owner
503 && p.bv->text->status() == CHANGED_IN_DRAW) {
504 if (prev && prev->par() == p.row->par()) {
505 breakAgainOneRow(p.bv, prev);
506 if (prev->next() != p.row) {
507 // breakAgainOneRow() has removed p.row
508 p.row = 0; // see what this breaks
509 need_break_row = prev;
511 need_break_row = p.row;
514 need_break_row = firstrow;
516 need_break_row = prev->next();
518 setCursor(p.bv, cursor.par(), cursor.pos());
525 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
527 if (!lyxrc.mark_foreign_language)
529 if (orig_font.language() == latex_language)
531 if (orig_font.language() == p.bv->buffer()->params.language)
534 int const y = p.yo + p.row->height() - 1;
535 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
539 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
541 pos_type pos = vis2log(vpos);
546 char c = p.row->par()->getChar(pos);
550 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
551 int const width = font_metrics::width(c, font);
554 for (pos_type i = pos-1; i >= 0; --i) {
555 c = p.row->par()->getChar(i);
556 if (!Encodings::IsComposeChar_hebrew(c)) {
557 if (IsPrintableNonspace(c)) {
559 singleWidth(p.bv, p.row->par(), i, c);
561 dx = (c == 'ø' || c == 'ã')
563 : (width2 - width) / 2;
570 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
574 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
576 pos_type pos = vis2log(vpos);
580 char c = p.row->par()->getChar(pos);
581 c = transformChar(c, p.row->par(), pos);
585 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
586 int const width = font_metrics::width(c, font);
589 for (pos_type i = pos-1; i >= 0; --i) {
590 c = p.row->par()->getChar(i);
591 if (!Encodings::IsComposeChar_arabic(c)) {
592 if (IsPrintableNonspace(c)) {
594 singleWidth(p.bv, p.row->par(), i, c);
595 dx = (width2 - width) / 2;
601 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
605 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
606 bool hebrew, bool arabic)
608 pos_type pos = vis2log(vpos);
609 pos_type const last = rowLastPrintable(p.row);
610 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
614 str += p.row->par()->getChar(pos);
616 unsigned char c = str[0];
617 str[0] = transformChar(c, p.row->par(), pos);
621 // collect as much similar chars as we can
622 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
623 char c = p.row->par()->getChar(pos);
625 if (!IsPrintableNonspace(c))
628 if (arabic && Encodings::IsComposeChar_arabic(c))
630 if (hebrew && Encodings::IsComposeChar_hebrew(c))
633 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
637 c = transformChar(c, p.row->par(), pos);
642 // Draw text and set the new x position
643 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
644 p.x += font_metrics::width(str, orig_font);
648 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
650 pos_type const pos = vis2log(vpos);
651 Paragraph * par = p.row->par();
653 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
655 float const orig_x = p.x;
657 char const c = par->getChar(pos);
659 if (IsNewlineChar(c)) {
663 } else if (IsInsetChar(c)) {
664 if (!drawInset(p, pos))
667 drawForeignMark(p, orig_x, orig_font);
671 // usual characters, no insets
673 // special case languages
674 bool const hebrew = (orig_font.language()->lang() == "hebrew");
676 orig_font.language()->lang() == "arabic" &&
677 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
678 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
680 // draw as many chars as we can
681 if ((!hebrew && !arabic)
682 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
683 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
684 drawChars(p, vpos, hebrew, arabic);
686 drawHebrewComposeChar(p, vpos);
688 drawArabicComposeChar(p, vpos);
691 drawForeignMark(p, orig_x, orig_font);
697 int LyXText::leftMargin(BufferView * bview, Row const * row) const
700 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
701 (ins=row->par()->getInset(row->pos())) &&
702 (ins->needFullRow() || ins->display()))
703 return LYX_PAPER_MARGIN;
705 LyXTextClass const & tclass =
706 bview->buffer()->params.getLyXTextClass();
707 LyXLayout_ptr const & layout = row->par()->layout();
709 string parindent = layout->parindent;
711 int x = LYX_PAPER_MARGIN;
713 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
715 // this is the way, LyX handles the LaTeX-Environments.
716 // I have had this idea very late, so it seems to be a
717 // later added hack and this is true
718 if (!row->par()->getDepth()) {
719 if (row->par()->layout() == tclass.defaultLayout()) {
720 // find the previous same level paragraph
721 if (row->par()->previous()) {
722 Paragraph * newpar = row->par()
723 ->depthHook(row->par()->getDepth());
725 newpar->layout()->nextnoindent)
730 // find the next level paragraph
732 Paragraph * newpar = row->par()->outerHook();
734 // make a corresponding row. Needed to call LeftMargin()
736 // check wether it is a sufficent paragraph
737 if (newpar && newpar->layout()->isEnvironment()) {
739 dummyrow.par(newpar);
740 dummyrow.pos(newpar->size());
741 x = leftMargin(bview, &dummyrow);
743 // this is no longer an error, because this function
744 // is used to clear impossible depths after changing
745 // a layout. Since there is always a redo,
746 // LeftMargin() is always called
747 row->par()->params().depth(0);
750 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
751 if (newpar->params().noindent())
754 parindent = newpar->layout()->parindent;
760 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
761 switch (layout->margintype) {
763 if (!layout->leftmargin.empty()) {
764 x += font_metrics::signedWidth(layout->leftmargin,
765 tclass.defaultfont());
767 if (!row->par()->getLabelstring().empty()) {
768 x += font_metrics::signedWidth(layout->labelindent,
770 x += font_metrics::width(row->par()->getLabelstring(),
772 x += font_metrics::width(layout->labelsep, labelfont);
776 x += font_metrics::signedWidth(layout->labelindent, labelfont);
777 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
778 if (!row->par()->getLabelWidthString().empty()) {
779 x += font_metrics::width(row->par()->getLabelWidthString(),
781 x += font_metrics::width(layout->labelsep, labelfont);
786 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
787 / (row->par()->getDepth() + 4);
789 case MARGIN_FIRST_DYNAMIC:
790 if (layout->labeltype == LABEL_MANUAL) {
791 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
792 x += font_metrics::signedWidth(layout->leftmargin,
795 x += font_metrics::signedWidth(layout->labelindent,
798 } else if (row->pos()
799 // Special case to fix problems with
801 || (layout->labeltype == LABEL_STATIC
802 && layout->latextype == LATEX_ENVIRONMENT
803 && ! row->par()->isFirstInSequence())) {
804 x += font_metrics::signedWidth(layout->leftmargin,
806 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
807 && layout->labeltype != LABEL_BIBLIO
808 && layout->labeltype !=
809 LABEL_CENTERED_TOP_ENVIRONMENT) {
810 x += font_metrics::signedWidth(layout->labelindent,
812 x += font_metrics::width(layout->labelsep, labelfont);
813 x += font_metrics::width(row->par()->getLabelstring(),
818 case MARGIN_RIGHT_ADDRESS_BOX:
820 // ok, a terrible hack. The left margin depends on the widest
821 // row in this paragraph. Do not care about footnotes, they
822 // are *NOT* allowed in the LaTeX realisation of this layout.
824 // find the first row of this paragraph
825 Row const * tmprow = row;
826 while (tmprow->previous()
827 && tmprow->previous()->par() == row->par())
828 tmprow = tmprow->previous();
830 int minfill = tmprow->fill();
831 while (tmprow->next() && tmprow->next()->par() == row->par()) {
832 tmprow = tmprow->next();
833 if (tmprow->fill() < minfill)
834 minfill = tmprow->fill();
837 x += font_metrics::signedWidth(layout->leftmargin,
838 tclass.defaultfont());
844 if ((workWidth(bview) > 0) &&
845 !row->par()->params().leftIndent().zero())
847 LyXLength const len = row->par()->params().leftIndent();
848 int const tw = inset_owner ?
849 inset_owner->latexTextWidth(bview) : workWidth(bview);
850 x += len.inPixels(tw);
853 LyXAlignment align; // wrong type
855 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
856 align = layout->align;
858 align = row->par()->params().align();
860 // set the correct parindent
861 if (row->pos() == 0) {
862 if ((layout->labeltype == LABEL_NO_LABEL
863 || layout->labeltype == LABEL_TOP_ENVIRONMENT
864 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
865 || (layout->labeltype == LABEL_STATIC
866 && layout->latextype == LATEX_ENVIRONMENT
867 && ! row->par()->isFirstInSequence()))
868 && align == LYX_ALIGN_BLOCK
869 && !row->par()->params().noindent()
870 // in tabulars and ert paragraphs are never indented!
871 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
872 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
873 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
874 && (row->par()->layout() != tclass.defaultLayout() ||
875 bview->buffer()->params.paragraph_separation ==
876 BufferParams::PARSEP_INDENT)) {
877 x += font_metrics::signedWidth(parindent,
878 tclass.defaultfont());
879 } else if (layout->labeltype == LABEL_BIBLIO) {
880 // ale970405 Right width for bibitems
881 x += bibitemMaxWidth(bview, tclass.defaultfont());
889 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
892 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
893 (ins=row->par()->getInset(row->pos())) &&
894 (ins->needFullRow() || ins->display()))
895 return LYX_PAPER_MARGIN;
897 LyXTextClass const & tclass = buf->params.getLyXTextClass();
898 LyXLayout_ptr const & layout = row->par()->layout();
900 int x = LYX_PAPER_MARGIN
901 + font_metrics::signedWidth(tclass.rightmargin(),
902 tclass.defaultfont());
904 // this is the way, LyX handles the LaTeX-Environments.
905 // I have had this idea very late, so it seems to be a
906 // later added hack and this is true
907 if (row->par()->getDepth()) {
908 // find the next level paragraph
910 Paragraph * newpar = row->par();
913 newpar = newpar->previous();
915 && newpar->getDepth() >= row->par()->getDepth());
917 // make a corresponding row. Needed to call LeftMargin()
919 // check wether it is a sufficent paragraph
920 if (newpar && newpar->layout()->isEnvironment()) {
922 dummyrow.par(newpar);
924 x = rightMargin(buf, &dummyrow);
926 // this is no longer an error, because this function
927 // is used to clear impossible depths after changing
928 // a layout. Since there is always a redo,
929 // LeftMargin() is always called
930 row->par()->params().depth(0);
934 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
935 x += font_metrics::signedWidth(layout->rightmargin,
936 tclass.defaultfont())
937 * 4 / (row->par()->getDepth() + 4);
942 int LyXText::labelEnd(BufferView * bview, Row const * row) const
944 if (row->par()->layout()->margintype == MARGIN_MANUAL) {
947 tmprow.pos(row->par()->size());
948 // just the beginning of the main body
949 return leftMargin(bview, &tmprow);
951 // LabelEnd is only needed,
952 // if the layout fills a flushleft label.
958 // get the next breakpoint in a given paragraph
960 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
962 Paragraph * par = row->par();
967 pos_type const pos = row->pos();
969 // position of the last possible breakpoint
970 // -1 isn't a suitable value, but a flag
971 pos_type last_separator = -1;
972 width -= rightMargin(bview->buffer(), row);
974 pos_type const main_body =
975 beginningOfMainBody(bview->buffer(), par);
976 LyXLayout_ptr const & layout = par->layout();
980 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
981 // special code for right address boxes, only newlines count
982 while (i < par->size()) {
983 if (par->isNewline(i)) {
985 i = par->size() - 1; // this means break
987 } else if (par->isInset(i) && par->getInset(i)
988 && par->getInset(i)->display()) {
989 par->getInset(i)->display(false);
994 // Last position is an invariant
995 pos_type const last = par->size();
996 // this is the usual handling
997 int x = leftMargin(bview, row);
998 bool doitonetime = true;
999 while (doitonetime || ((x < width) && (i < last))) {
1000 doitonetime = false;
1001 char const c = par->getChar(i);
1003 if (c == Paragraph::META_INSET)
1004 in = par->getInset(i);
1005 if (IsNewlineChar(c)) {
1007 x = width; // this means break
1008 } else if (in && !in->isChar()) {
1009 // check wether a Display() inset is
1010 // valid here. if not, change it to
1012 if (in->display() &&
1013 (layout->isCommand() ||
1014 (layout->labeltype == LABEL_MANUAL
1015 && i < beginningOfMainBody(bview->buffer(), par))))
1017 // display istn't allowd
1019 x += singleWidth(bview, par, i, c);
1020 } else if (in->display() || in->needFullRow()) {
1021 // So break the line here
1025 if (par->isLineSeparator(i+1))
1028 last_separator = last; // to avoid extra rows
1030 last_separator = i - 1;
1031 x = width; // this means break
1033 x += singleWidth(bview, par, i, c);
1034 // we have to check this separately as we could have a
1035 // lineseparator and then the algorithm below would prefer
1036 // that which IS wrong! We should always break on an inset
1037 // if it's too long and not on the last separator.
1038 // Maybe the only exeption is insets used as chars but
1039 // then we would have to have a special function inside
1040 // the inset to tell us this. Till then we leave it as
1041 // it is now. (Jug 20020106)
1042 if (pos < i && x >= width && last_separator >= 0)
1043 last_separator = i - 1;
1046 if (par->isLineSeparator(i))
1048 x += singleWidth(bview, par, i, c);
1051 if (i == main_body) {
1052 x += font_metrics::width(layout->labelsep,
1053 getLabelFont(bview->buffer(), par));
1054 if (par->isLineSeparator(i - 1))
1055 x-= singleWidth(bview, par, i - 1);
1056 int left_margin = labelEnd(bview, row);
1057 if (x < left_margin)
1061 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1062 last_separator = i - 2;
1063 else if ((pos < i) && (last_separator < 0) && (x >= width))
1064 last_separator = i - 1;
1065 // end of paragraph is always a suitable separator
1066 else if (i == last && x < width)
1070 // well, if last_separator is still 0, the line isn't breakable.
1071 // don't care and cut simply at the end
1072 if (last_separator < 0) {
1076 // manual labels cannot be broken in LaTeX, do not care
1077 if (main_body && last_separator < main_body)
1078 last_separator = main_body - 1;
1080 return last_separator;
1084 // returns the minimum space a row needs on the screen in pixel
1085 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1087 if (paper_width < 0)
1091 // get the pure distance
1092 pos_type const last = rowLastPrintable(row);
1094 // special handling of the right address boxes
1095 if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1096 int const tmpfill = row->fill();
1097 row->fill(0); // the minfill in MarginLeft()
1098 w = leftMargin(bview, row);
1101 w = leftMargin(bview, row);
1103 LyXLayout_ptr const & layout = row->par()->layout();
1105 pos_type const main_body =
1106 beginningOfMainBody(bview->buffer(), row->par());
1107 pos_type i = row->pos();
1110 if (main_body > 0 && i == main_body) {
1111 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1112 if (row->par()->isLineSeparator(i - 1))
1113 w -= singleWidth(bview, row->par(), i - 1);
1114 int left_margin = labelEnd(bview, row);
1115 if (w < left_margin)
1118 w += singleWidth(bview, row->par(), i);
1121 if (main_body > 0 && main_body > last) {
1122 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1123 if (last >= 0 && row->par()->isLineSeparator(last))
1124 w -= singleWidth(bview, row->par(), last);
1125 int const left_margin = labelEnd(bview, row);
1126 if (w < left_margin)
1130 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1135 // returns the minimum space a manual label needs on the screen in pixel
1136 int LyXText::labelFill(BufferView * bview, Row const * row) const
1138 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1139 // -1 because a label ends either with a space that is in the label,
1140 // or with the beginning of a footnote that is outside the label.
1142 // I don't understand this code in depth, but sometimes "last" is
1143 // less than 0 and this causes a crash. This fix seems to work
1144 // correctly, but I bet the real error is elsewhere. The bug is
1145 // triggered when you have an open footnote in a paragraph
1146 // environment with a manual label. (Asger)
1147 if (last < 0) last = 0;
1149 // a separator at this end does not count
1150 if (row->par()->isLineSeparator(last))
1154 pos_type i = row->pos();
1156 w += singleWidth(bview, row->par(), i);
1161 if (!row->par()->params().labelWidthString().empty()) {
1162 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1163 getLabelFont(bview->buffer(), row->par())) - w,
1171 // returns the number of separators in the specified row. The separator
1172 // on the very last column doesnt count
1173 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1175 pos_type last = rowLastPrintable(row);
1176 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1179 for (; p <= last; ++p) {
1180 if (row->par()->isSeparator(p)) {
1188 // returns the number of hfills in the specified row. The LyX-Hfill is
1189 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1190 // ignored. This is *MUCH* more usefull than not to ignore!
1191 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1193 pos_type const last = rowLast(row);
1194 pos_type first = row->pos();
1197 // hfill *DO* count at the beginning of paragraphs!
1198 while (first <= last && row->par()->isHfill(first)) {
1203 first = max(first, beginningOfMainBody(buf, row->par()));
1205 for (pos_type p = first; p <= last; ++p) {
1206 // last, because the end is ignored!
1208 if (row->par()->isHfill(p)) {
1216 // like NumberOfHfills, but only those in the manual label!
1217 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1219 pos_type last = rowLast(row);
1220 pos_type first = row->pos();
1222 // hfill *DO* count at the beginning of paragraphs!
1223 while (first < last && row->par()->isHfill(first))
1227 last = min(last, beginningOfMainBody(buf, row->par()));
1229 for (pos_type p = first; p < last; ++p) {
1230 // last, because the end is ignored!
1231 if (row->par()->isHfill(p)) {
1239 // returns true, if a expansion is needed.
1240 // Rules are given by LaTeX
1241 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1244 // by the way, is it a hfill?
1245 if (!row_ptr->par()->isHfill(pos))
1248 // at the end of a row it does not count
1249 // unless another hfill exists on the line
1250 if (pos >= rowLast(row_ptr)) {
1251 pos_type i = row_ptr->pos();
1252 while (i < pos && !row_ptr->par()->isHfill(i)) {
1260 // at the beginning of a row it does not count, if it is not
1261 // the first row of a paragaph
1262 if (!row_ptr->pos())
1265 // in some labels it does not count
1266 if (row_ptr->par()->layout()->margintype != MARGIN_MANUAL
1267 && pos < beginningOfMainBody(buf, row_ptr->par()))
1270 // if there is anything between the first char of the row and
1271 // the sepcified position that is not a newline and not a hfill,
1272 // the hfill will count, otherwise not
1273 pos_type i = row_ptr->pos();
1274 while (i < pos && (row_ptr->par()->isNewline(i)
1275 || row_ptr->par()->isHfill(i)))
1282 LColor::color LyXText::backgroundColor()
1285 return inset_owner->backgroundColor();
1287 return LColor::background;
1290 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1292 // get the maximum ascent and the maximum descent
1295 float layoutasc = 0;
1296 float layoutdesc = 0;
1299 Inset * tmpinset = 0;
1301 // ok , let us initialize the maxasc and maxdesc value.
1302 // This depends in LaTeX of the font of the last character
1303 // in the paragraph. The hack below is necessary because
1304 // of the possibility of open footnotes
1306 // Correction: only the fontsize count. The other properties
1307 // are taken from the layoutfont. Nicer on the screen :)
1308 Paragraph * par = row_ptr->par();
1309 Paragraph * firstpar = row_ptr->par();
1311 LyXLayout_ptr const & layout = firstpar->layout();
1313 // as max get the first character of this row then it can increes but not
1314 // decrees the height. Just some point to start with so we don't have to
1315 // do the assignment below too often.
1316 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1317 LyXFont::FONT_SIZE const tmpsize = font.size();
1318 font = getLayoutFont(bview->buffer(), par);
1319 LyXFont::FONT_SIZE const size = font.size();
1320 font.setSize(tmpsize);
1322 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1324 float spacing_val = 1.0;
1325 if (!row_ptr->par()->params().spacing().isDefault()) {
1326 spacing_val = row_ptr->par()->params().spacing().getValue();
1328 spacing_val = bview->buffer()->params.spacing.getValue();
1330 //lyxerr << "spacing_val = " << spacing_val << endl;
1332 int maxasc = int(font_metrics::maxAscent(font) *
1333 layout->spacing.getValue() *
1335 int maxdesc = int(font_metrics::maxDescent(font) *
1336 layout->spacing.getValue() *
1339 pos_type const pos_end = rowLast(row_ptr);
1343 // Check if any insets are larger
1344 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1345 if (row_ptr->par()->isInset(pos)) {
1346 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1347 tmpinset = row_ptr->par()->getInset(pos);
1349 #if 1 // this is needed for deep update on initialitation
1350 tmpinset->update(bview, tmpfont);
1352 asc = tmpinset->ascent(bview, tmpfont);
1353 desc = tmpinset->descent(bview, tmpfont);
1354 maxwidth += tmpinset->width(bview, tmpfont);
1355 maxasc = max(maxasc, asc);
1356 maxdesc = max(maxdesc, desc);
1359 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1363 // Check if any custom fonts are larger (Asger)
1364 // This is not completely correct, but we can live with the small,
1365 // cosmetic error for now.
1366 LyXFont::FONT_SIZE maxsize =
1367 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1368 if (maxsize > font.size()) {
1369 font.setSize(maxsize);
1371 asc = font_metrics::maxAscent(font);
1372 desc = font_metrics::maxDescent(font);
1379 // This is nicer with box insets:
1383 row_ptr->ascent_of_text(maxasc);
1385 // is it a top line?
1386 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1388 // some parksips VERY EASY IMPLEMENTATION
1389 if (bview->buffer()->params.paragraph_separation ==
1390 BufferParams::PARSEP_SKIP)
1392 if (layout->isParagraph()
1393 && firstpar->getDepth() == 0
1394 && firstpar->previous())
1396 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1397 } else if (firstpar->previous() &&
1398 firstpar->previous()->layout()->isParagraph() &&
1399 firstpar->previous()->getDepth() == 0)
1401 // is it right to use defskip here too? (AS)
1402 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1406 // the paper margins
1407 if (!row_ptr->par()->previous() && bv_owner)
1408 maxasc += LYX_PAPER_MARGIN;
1410 // add the vertical spaces, that the user added
1411 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1413 // do not forget the DTP-lines!
1414 // there height depends on the font of the nearest character
1415 if (firstpar->params().lineTop())
1417 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1419 // and now the pagebreaks
1420 if (firstpar->params().pagebreakTop())
1421 maxasc += 3 * defaultHeight();
1423 // This is special code for the chapter, since the label of this
1424 // layout is printed in an extra row
1425 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1426 && bview->buffer()->params.secnumdepth >= 0)
1428 float spacing_val = 1.0;
1429 if (!row_ptr->par()->params().spacing().isDefault()) {
1430 spacing_val = row_ptr->par()->params().spacing().getValue();
1432 spacing_val = bview->buffer()->params.spacing.getValue();
1435 labeladdon = int(font_metrics::maxDescent(labelfont) *
1436 layout->spacing.getValue() *
1438 + int(font_metrics::maxAscent(labelfont) *
1439 layout->spacing.getValue() *
1443 // special code for the top label
1444 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1445 || layout->labeltype == LABEL_BIBLIO
1446 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1447 && row_ptr->par()->isFirstInSequence()
1448 && !row_ptr->par()->getLabelstring().empty())
1450 float spacing_val = 1.0;
1451 if (!row_ptr->par()->params().spacing().isDefault()) {
1452 spacing_val = row_ptr->par()->params().spacing().getValue();
1454 spacing_val = bview->buffer()->params.spacing.getValue();
1458 (font_metrics::maxAscent(labelfont) *
1459 layout->spacing.getValue() *
1461 +(font_metrics::maxDescent(labelfont) *
1462 layout->spacing.getValue() *
1464 + layout->topsep * defaultHeight()
1465 + layout->labelbottomsep * defaultHeight());
1468 // and now the layout spaces, for example before and after a section,
1469 // or between the items of a itemize or enumerate environment
1471 if (!firstpar->params().pagebreakTop()) {
1472 Paragraph * prev = row_ptr->par()->previous();
1474 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1475 if (prev && prev->layout() == firstpar->layout() &&
1476 prev->getDepth() == firstpar->getDepth() &&
1477 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1479 layoutasc = (layout->itemsep * defaultHeight());
1480 } else if (row_ptr->previous()) {
1481 tmptop = layout->topsep;
1483 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1484 tmptop -= row_ptr->previous()->par()->layout()->bottomsep;
1487 layoutasc = (tmptop * defaultHeight());
1488 } else if (row_ptr->par()->params().lineTop()) {
1489 tmptop = layout->topsep;
1492 layoutasc = (tmptop * defaultHeight());
1495 prev = row_ptr->par()->outerHook();
1497 maxasc += int(prev->layout()->parsep * defaultHeight());
1499 if (firstpar->previous() &&
1500 firstpar->previous()->getDepth() == 0 &&
1501 firstpar->previous()->layout() !=
1505 } else if (firstpar->previous()) {
1506 maxasc += int(layout->parsep * defaultHeight());
1512 // is it a bottom line?
1513 if (row_ptr->par() == par
1514 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1516 // the paper margins
1517 if (!par->next() && bv_owner)
1518 maxdesc += LYX_PAPER_MARGIN;
1520 // add the vertical spaces, that the user added
1521 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1523 // do not forget the DTP-lines!
1524 // there height depends on the font of the nearest character
1525 if (firstpar->params().lineBottom())
1526 maxdesc += 2 * font_metrics::ascent('x',
1527 getFont(bview->buffer(),
1529 max(pos_type(0), par->size() - 1)));
1531 // and now the pagebreaks
1532 if (firstpar->params().pagebreakBottom())
1533 maxdesc += 3 * defaultHeight();
1535 // and now the layout spaces, for example before and after
1536 // a section, or between the items of a itemize or enumerate
1538 if (!firstpar->params().pagebreakBottom()
1539 && row_ptr->par()->next()) {
1540 Paragraph * nextpar = row_ptr->par()->next();
1541 Paragraph * comparepar = row_ptr->par();
1545 if (comparepar->getDepth() > nextpar->getDepth()) {
1546 usual = (comparepar->layout()->bottomsep * defaultHeight());
1547 comparepar = comparepar->depthHook(nextpar->getDepth());
1548 if (comparepar->layout()!= nextpar->layout()
1549 || nextpar->getLabelWidthString() !=
1550 comparepar->getLabelWidthString())
1552 unusual = (comparepar->layout()->bottomsep * defaultHeight());
1554 if (unusual > usual)
1555 layoutdesc = unusual;
1558 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1560 if (comparepar->layout() != nextpar->layout()
1561 || nextpar->getLabelWidthString() !=
1562 comparepar->getLabelWidthString())
1563 layoutdesc = int(comparepar->layout()->bottomsep * defaultHeight());
1568 // incalculate the layout spaces
1569 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1570 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1572 // calculate the new height of the text
1573 height -= row_ptr->height();
1575 row_ptr->height(maxasc + maxdesc + labeladdon);
1576 row_ptr->baseline(maxasc + labeladdon);
1578 height += row_ptr->height();
1580 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1583 row_ptr->width(maxwidth);
1584 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1586 row_ptr->width(int(maxwidth + x));
1589 width = max(0,workWidth(bview));
1591 if (r->width() > width)
1599 // Appends the implicit specified paragraph behind the specified row,
1600 // start at the implicit given position
1601 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1603 bool not_ready = true;
1605 // The last character position of a paragraph is an invariant so we can
1606 // safely get it here. (Asger)
1607 pos_type const lastposition = row->par()->size();
1609 // Get the next breakpoint
1610 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1614 // Insert the new row
1615 if (z < lastposition) {
1617 insertRow(row, row->par(), z);
1624 // Set the dimensions of the row
1625 // fixed fill setting now by calling inset->update() in
1626 // SingleWidth when needed!
1627 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1628 setHeightOfRow(bview, tmprow);
1630 } while (not_ready);
1634 void LyXText::breakAgain(BufferView * bview, Row * row) const
1636 bool not_ready = true;
1639 // get the next breakpoint
1640 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1643 if (z < row->par()->size()) {
1644 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1647 insertRow(row, row->par(), z);
1653 if (row->pos() == z)
1654 not_ready = false; // the rest will not change
1660 // if there are some rows too much, delete them
1661 // only if you broke the whole paragraph!
1662 Row * tmprow2 = row;
1663 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1664 tmprow2 = tmprow2->next();
1666 while (tmprow2 != row) {
1667 tmprow2 = tmprow2->previous();
1668 removeRow(tmprow2->next());
1673 // set the dimensions of the row
1674 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1675 setHeightOfRow(bview, tmprow);
1676 } while (not_ready);
1680 // this is just a little changed version of break again
1681 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1683 // get the next breakpoint
1684 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1687 if (z < row->par()->size()) {
1689 || (row->next() && row->next()->par() != row->par())) {
1692 insertRow(row, row->par(), z);
1698 if (row->pos() != z)
1702 // if there are some rows too much, delete them
1703 // only if you broke the whole paragraph!
1704 Row * tmprow2 = row;
1705 while (tmprow2->next()
1706 && tmprow2->next()->par() == row->par()) {
1707 tmprow2 = tmprow2->next();
1709 while (tmprow2 != row) {
1710 tmprow2 = tmprow2->previous();
1711 removeRow(tmprow2->next());
1715 // set the dimensions of the row
1716 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1717 setHeightOfRow(bview, tmprow);
1721 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1723 LyXTextClass const & tclass =
1724 bview->buffer()->params.getLyXTextClass();
1725 LyXLayout_ptr const & layout = cursor.par()->layout();
1727 // this is only allowed, if the current paragraph is not empty or caption
1728 // and if it has not the keepempty flag aktive
1729 if (cursor.par()->empty()
1730 && layout->labeltype != LABEL_SENSITIVE
1731 && !layout->keepempty)
1734 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1736 // Always break behind a space
1738 // It is better to erase the space (Dekel)
1739 if (cursor.pos() < cursor.par()->size()
1740 && cursor.par()->isLineSeparator(cursor.pos()))
1741 cursor.par()->erase(cursor.pos());
1742 // cursor.pos(cursor.pos() + 1);
1744 // break the paragraph
1748 keep_layout = layout->isEnvironment();
1750 // we need to set this before we insert the paragraph. IMO the
1751 // breakParagraph call should return a bool if it inserts the
1752 // paragraph before or behind and we should react on that one
1753 // but we can fix this in 1.3.0 (Jug 20020509)
1754 bool const isempty = (layout->keepempty && cursor.par()->empty());
1755 ::breakParagraph(bview->buffer()->params, cursor.par(), cursor.pos(),
1758 // well this is the caption hack since one caption is really enough
1759 if (layout->labeltype == LABEL_SENSITIVE) {
1761 // set to standard-layout
1762 cursor.par()->applyLayout(tclass.defaultLayout());
1764 // set to standard-layout
1765 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1768 // if the cursor is at the beginning of a row without prior newline,
1770 // This touches only the screen-update. Otherwise we would may have
1771 // an empty row on the screen
1772 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1773 && cursor.row()->pos() == cursor.pos())
1778 status(bview, LyXText::NEED_MORE_REFRESH);
1779 refresh_row = cursor.row();
1780 refresh_y = cursor.y() - cursor.row()->baseline();
1782 // Do not forget the special right address boxes
1783 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1784 while (refresh_row->previous() &&
1785 refresh_row->previous()->par() == refresh_row->par())
1787 refresh_row = refresh_row->previous();
1788 refresh_y -= refresh_row->height();
1791 removeParagraph(cursor.row());
1793 // set the dimensions of the cursor row
1794 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1796 setHeightOfRow(bview, cursor.row());
1798 while (!cursor.par()->next()->empty()
1799 && cursor.par()->next()->isNewline(0))
1800 cursor.par()->next()->erase(0);
1802 insertParagraph(bview, cursor.par()->next(), cursor.row());
1804 updateCounters(bview);
1806 // This check is necessary. Otherwise the new empty paragraph will
1807 // be deleted automatically. And it is more friendly for the user!
1808 if (cursor.pos() || isempty)
1809 setCursor(bview, cursor.par()->next(), 0);
1811 setCursor(bview, cursor.par(), 0);
1813 if (cursor.row()->next())
1814 breakAgain(bview, cursor.row()->next());
1820 // Just a macro to make some thing easier.
1821 void LyXText::redoParagraph(BufferView * bview) const
1824 redoParagraphs(bview, cursor, cursor.par()->next());
1825 setCursorIntern(bview, cursor.par(), cursor.pos());
1829 // insert a character, moves all the following breaks in the
1830 // same Paragraph one to the right and make a rebreak
1831 void LyXText::insertChar(BufferView * bview, char c)
1833 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1835 // When the free-spacing option is set for the current layout,
1836 // disable the double-space checking
1838 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1839 cursor.row()->par()->isFreeSpacing();
1841 if (lyxrc.auto_number) {
1842 static string const number_operators = "+-/*";
1843 static string const number_unary_operators = "+-";
1844 static string const number_seperators = ".,:";
1846 if (current_font.number() == LyXFont::ON) {
1847 if (!IsDigit(c) && !contains(number_operators, c) &&
1848 !(contains(number_seperators, c) &&
1849 cursor.pos() >= 1 &&
1850 cursor.pos() < cursor.par()->size() &&
1851 getFont(bview->buffer(),
1853 cursor.pos()).number() == LyXFont::ON &&
1854 getFont(bview->buffer(),
1856 cursor.pos() - 1).number() == LyXFont::ON)
1858 number(bview); // Set current_font.number to OFF
1859 } else if (IsDigit(c) &&
1860 real_current_font.isVisibleRightToLeft()) {
1861 number(bview); // Set current_font.number to ON
1863 if (cursor.pos() > 0) {
1864 char const c = cursor.par()->getChar(cursor.pos() - 1);
1865 if (contains(number_unary_operators, c) &&
1866 (cursor.pos() == 1 ||
1867 cursor.par()->isSeparator(cursor.pos() - 2) ||
1868 cursor.par()->isNewline(cursor.pos() - 2))
1870 setCharFont(bview->buffer(),
1874 } else if (contains(number_seperators, c) &&
1875 cursor.pos() >= 2 &&
1876 getFont(bview->buffer(),
1878 cursor.pos() - 2).number() == LyXFont::ON) {
1879 setCharFont(bview->buffer(),
1889 // First check, if there will be two blanks together or a blank at
1890 // the beginning of a paragraph.
1891 // I decided to handle blanks like normal characters, the main
1892 // difference are the special checks when calculating the row.fill
1893 // (blank does not count at the end of a row) and the check here
1895 // The bug is triggered when we type in a description environment:
1896 // The current_font is not changed when we go from label to main text
1897 // and it should (along with realtmpfont) when we type the space.
1898 // CHECK There is a bug here! (Asger)
1900 LyXFont realtmpfont = real_current_font;
1901 LyXFont rawtmpfont = current_font;
1902 // store the current font. This is because of the use of cursor
1903 // movements. The moving cursor would refresh the current font
1905 // Get the font that is used to calculate the baselineskip
1906 pos_type const lastpos = cursor.par()->size();
1907 LyXFont rawparfont =
1908 cursor.par()->getFontSettings(bview->buffer()->params,
1911 bool jumped_over_space = false;
1913 if (!freeSpacing && IsLineSeparatorChar(c)) {
1914 if ((cursor.pos() > 0
1915 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1916 || (cursor.pos() > 0
1917 && cursor.par()->isNewline(cursor.pos() - 1))
1918 || (cursor.pos() == 0)) {
1919 static bool sent_space_message = false;
1920 if (!sent_space_message) {
1921 if (cursor.pos() == 0)
1922 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1924 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1925 sent_space_message = true;
1930 } else if (IsNewlineChar(c)) {
1931 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1937 // No newline at first position of a paragraph or behind labels.
1938 // TeX does not allow that
1940 if (cursor.pos() < cursor.par()->size() &&
1941 cursor.par()->isLineSeparator(cursor.pos()))
1942 // newline always after a blank!
1944 cursor.row()->fill(-1); // to force a new break
1947 // the display inset stuff
1948 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1949 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1950 if (inset && (inset->display() || inset->needFullRow())) {
1951 // force a new break
1952 cursor.row()->fill(-1); // to force a new break
1956 // get the cursor row fist
1957 Row * row = cursor.row();
1958 int y = cursor.y() - row->baseline();
1959 if (c != Paragraph::META_INSET) {
1960 // Here case LyXText::InsertInset already insertet the character
1961 cursor.par()->insertChar(cursor.pos(), c);
1963 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1965 if (!jumped_over_space) {
1966 // refresh the positions
1968 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1969 tmprow = tmprow->next();
1970 tmprow->pos(tmprow->pos() + 1);
1974 // Is there a break one row above
1975 if (row->previous() && row->previous()->par() == row->par()
1976 && (cursor.par()->isLineSeparator(cursor.pos())
1977 || cursor.par()->isNewline(cursor.pos())
1978 || ((cursor.pos() < cursor.par()->size()) &&
1979 cursor.par()->isInset(cursor.pos()+1))
1980 || cursor.row()->fill() == -1))
1982 pos_type z = nextBreakPoint(bview,
1985 if (z >= row->pos()) {
1988 // set the dimensions of the row above
1989 row->previous()->fill(fill(bview,
1993 setHeightOfRow(bview, row->previous());
1995 y -= row->previous()->height();
1997 refresh_row = row->previous();
1998 status(bview, LyXText::NEED_MORE_REFRESH);
2000 breakAgainOneRow(bview, row);
2002 current_font = rawtmpfont;
2003 real_current_font = realtmpfont;
2004 setCursor(bview, cursor.par(), cursor.pos() + 1,
2005 false, cursor.boundary());
2006 // cursor MUST be in row now.
2008 if (row->next() && row->next()->par() == row->par())
2009 need_break_row = row->next();
2013 // check, wether the last characters font has changed.
2014 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2015 && rawparfont != rawtmpfont)
2016 redoHeightOfParagraph(bview, cursor);
2023 // recalculate the fill of the row
2024 if (row->fill() >= 0) {
2025 // needed because a newline will set fill to -1. Otherwise
2026 // we would not get a rebreak!
2027 row->fill(fill(bview, row, workWidth(bview)));
2030 if (c == Paragraph::META_INSET || row->fill() < 0) {
2033 status(bview, LyXText::NEED_MORE_REFRESH);
2034 breakAgainOneRow(bview, row);
2035 // will the cursor be in another row now?
2036 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2037 if (row->next() && row->next()->par() == row->par())
2038 // this should always be true
2040 breakAgainOneRow(bview, row);
2042 current_font = rawtmpfont;
2043 real_current_font = realtmpfont;
2045 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2047 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2048 != cursor.boundary())
2049 setCursor(bview, cursor.par(), cursor.pos(), false,
2050 !cursor.boundary());
2051 if (row->next() && row->next()->par() == row->par())
2052 need_break_row = row->next();
2059 int const tmpheight = row->height();
2060 setHeightOfRow(bview, row);
2061 if (tmpheight == row->height())
2062 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2064 status(bview, LyXText::NEED_MORE_REFRESH);
2066 current_font = rawtmpfont;
2067 real_current_font = realtmpfont;
2068 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2072 // check, wether the last characters font has changed.
2073 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2074 && rawparfont != rawtmpfont) {
2075 redoHeightOfParagraph(bview, cursor);
2077 // now the special right address boxes
2078 if (cursor.par()->layout()->margintype
2079 == MARGIN_RIGHT_ADDRESS_BOX) {
2080 redoDrawingOfParagraph(bview, cursor);
2088 void LyXText::charInserted()
2090 // Here we could call FinishUndo for every 20 characters inserted.
2091 // This is from my experience how emacs does it.
2092 static unsigned int counter;
2102 void LyXText::prepareToPrint(BufferView * bview,
2103 Row * row, float & x,
2104 float & fill_separator,
2106 float & fill_label_hfill,
2112 float w = row->fill();
2114 fill_label_hfill = 0;
2116 fill_label_hfill = 0;
2119 row->par()->isRightToLeftPar(bview->buffer()->params);
2121 x = (workWidth(bview) > 0)
2122 ? rightMargin(bview->buffer(), row) : 0;
2124 x = (workWidth(bview) > 0)
2125 ? leftMargin(bview, row) : 0;
2127 // is there a manual margin with a manual label
2128 LyXLayout_ptr const & layout = row->par()->layout();
2130 if (layout->margintype == MARGIN_MANUAL
2131 && layout->labeltype == LABEL_MANUAL) {
2132 // one more since labels are left aligned
2133 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2134 if (nlh && !row->par()->getLabelWidthString().empty()) {
2135 fill_label_hfill = labelFill(bview, row) / nlh;
2139 // are there any hfills in the row?
2140 float const nh = numberOfHfills(bview->buffer(), row);
2144 fill_hfill = w / nh;
2145 // we don't have to look at the alignment if it is ALIGN_LEFT and
2146 // if the row is already larger then the permitted width as then
2147 // we force the LEFT_ALIGN'edness!
2148 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2149 // is it block, flushleft or flushright?
2150 // set x how you need it
2152 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2153 align = layout->align;
2155 align = row->par()->params().align();
2158 // center displayed insets
2160 if (row->par()->isInset(row->pos())
2161 && (inset=row->par()->getInset(row->pos()))
2162 && (inset->display())) // || (inset->scroll() < 0)))
2163 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2164 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2165 // ERT insets should always be LEFT ALIGNED on screen
2166 inset = row->par()->inInset();
2167 if (inset && inset->owner() &&
2168 inset->owner()->lyxCode() == Inset::ERT_CODE)
2170 align = LYX_ALIGN_LEFT;
2174 case LYX_ALIGN_BLOCK:
2175 ns = numberOfSeparators(bview->buffer(), row);
2176 if (ns && row->next() && row->next()->par() == row->par() &&
2177 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2178 && !(row->next()->par()->isInset(row->next()->pos())
2179 && row->next()->par()->getInset(row->next()->pos())
2180 && row->next()->par()->getInset(row->next()->pos())->display())
2183 fill_separator = w / ns;
2184 } else if (is_rtl) {
2188 case LYX_ALIGN_RIGHT:
2191 case LYX_ALIGN_CENTER:
2199 computeBidiTables(bview->buffer(), row);
2201 pos_type main_body =
2202 beginningOfMainBody(bview->buffer(), row->par());
2203 pos_type last = rowLast(row);
2205 if (main_body > 0 &&
2206 (main_body - 1 > last ||
2207 !row->par()->isLineSeparator(main_body - 1))) {
2208 x += font_metrics::width(layout->labelsep,
2209 getLabelFont(bview->buffer(), row->par()));
2210 if (main_body - 1 <= last)
2211 x += fill_label_hfill;
2217 // important for the screen
2220 // the cursor set functions have a special mechanism. When they
2221 // realize, that you left an empty paragraph, they will delete it.
2222 // They also delete the corresponding row
2224 void LyXText::cursorRightOneWord(BufferView * bview) const
2226 // treat floats, HFills and Insets as words
2227 LyXCursor tmpcursor = cursor;
2228 // CHECK See comment on top of text.C
2230 if (tmpcursor.pos() == tmpcursor.par()->size()
2231 && tmpcursor.par()->next()) {
2232 tmpcursor.par(tmpcursor.par()->next());
2237 // Skip through initial nonword stuff.
2238 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2239 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2240 // printf("Current pos1 %d", tmpcursor.pos()) ;
2241 tmpcursor.pos(tmpcursor.pos() + 1);
2244 // Advance through word.
2245 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2246 tmpcursor.par()->isWord(tmpcursor.pos())) {
2247 // printf("Current pos2 %d", tmpcursor.pos()) ;
2248 tmpcursor.pos(tmpcursor.pos() + 1);
2252 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2256 void LyXText::cursorTab(BufferView * bview) const
2258 LyXCursor tmpcursor = cursor;
2259 while (tmpcursor.pos() < tmpcursor.par()->size()
2260 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2261 tmpcursor.pos(tmpcursor.pos() + 1);
2263 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2264 if (tmpcursor.par()->next()) {
2265 tmpcursor.par(tmpcursor.par()->next());
2269 tmpcursor.pos(tmpcursor.pos() + 1);
2270 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2274 // Skip initial whitespace at end of word and move cursor to *start*
2275 // of prior word, not to end of next prior word.
2276 void LyXText::cursorLeftOneWord(BufferView * bview) const
2278 LyXCursor tmpcursor = cursor;
2279 cursorLeftOneWord(tmpcursor);
2280 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2284 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2286 // treat HFills, floats and Insets as words
2289 && (cur.par()->isSeparator(cur.pos() - 1)
2290 || cur.par()->isKomma(cur.pos() - 1))
2291 && !(cur.par()->isHfill(cur.pos() - 1)
2292 || cur.par()->isInset(cur.pos() - 1)))
2293 cur.pos(cur.pos() - 1);
2296 && (cur.par()->isInset(cur.pos() - 1)
2297 || cur.par()->isHfill(cur.pos() - 1))) {
2298 cur.pos(cur.pos() - 1);
2299 } else if (!cur.pos()) {
2300 if (cur.par()->previous()) {
2301 cur.par(cur.par()->previous());
2302 cur.pos(cur.par()->size());
2304 } else { // Here, cur != 0
2305 while (cur.pos() > 0 &&
2306 cur.par()->isWord(cur.pos() - 1))
2307 cur.pos(cur.pos() - 1);
2312 // Select current word. This depends on behaviour of
2313 // CursorLeftOneWord(), so it is patched as well.
2314 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2315 word_location const loc) const
2317 // first put the cursor where we wana start to select the word
2320 case WHOLE_WORD_STRICT:
2321 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2322 || cursor.par()->isSeparator(cursor.pos())
2323 || cursor.par()->isKomma(cursor.pos())
2324 || cursor.par()->isSeparator(cursor.pos() - 1)
2325 || cursor.par()->isKomma(cursor.pos() - 1)) {
2329 // no break here, we go to the next
2332 // Move cursor to the beginning, when not already there.
2333 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2334 && !from.par()->isKomma(from.pos() - 1))
2335 cursorLeftOneWord(from);
2338 // always move the cursor to the beginning of previous word
2339 cursorLeftOneWord(from);
2342 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2348 while (to.pos() < to.par()->size()
2349 && !to.par()->isSeparator(to.pos())
2350 && !to.par()->isKomma(to.pos())
2351 && !to.par()->isHfill(to.pos())
2352 && !to.par()->isInset(to.pos()))
2354 to.pos(to.pos() + 1);
2359 void LyXText::selectWord(BufferView * bview, word_location const loc)
2363 getWord(from, to, loc);
2365 setCursor(bview, from.par(), from.pos());
2368 selection.cursor = cursor;
2369 setCursor(bview, to.par(), to.pos());
2370 setSelection(bview);
2374 // Select the word currently under the cursor when no
2375 // selection is currently set
2376 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2377 word_location const loc)
2379 if (!selection.set()) {
2380 selectWord(bview, loc);
2381 return selection.set();
2387 // This function is only used by the spellchecker for NextWord().
2388 // It doesn't handle LYX_ACCENTs and probably never will.
2390 LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const
2392 if (the_locking_inset) {
2393 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2394 if (!word.word().empty()) {
2395 value += float(cursor.y())/float(height);
2398 // we have to go on checking so move cursor to the next char
2399 if (cursor.pos() == cursor.par()->size()) {
2400 if (!cursor.par()->next())
2402 cursor.par(cursor.par()->next());
2405 cursor.pos(cursor.pos() + 1);
2407 Paragraph * tmppar = cursor.par();
2409 // If this is not the very first word, skip rest of
2410 // current word because we are probably in the middle
2411 // of a word if there is text here.
2412 if (cursor.pos() || cursor.par()->previous()) {
2413 while (cursor.pos() < cursor.par()->size()
2414 && cursor.par()->isLetter(cursor.pos()))
2415 cursor.pos(cursor.pos() + 1);
2418 // Now, skip until we have real text (will jump paragraphs)
2419 while ((cursor.par()->size() > cursor.pos()
2420 && (!cursor.par()->isLetter(cursor.pos()))
2421 && (!cursor.par()->isInset(cursor.pos()) ||
2422 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2423 || (cursor.par()->size() == cursor.pos()
2424 && cursor.par()->next()))
2426 if (cursor.pos() == cursor.par()->size()) {
2427 cursor.par(cursor.par()->next());
2430 cursor.pos(cursor.pos() + 1);
2433 // now check if we hit an inset so it has to be a inset containing text!
2434 if (cursor.pos() < cursor.par()->size() &&
2435 cursor.par()->isInset(cursor.pos()))
2438 cursor.par()->getInset(cursor.pos())->edit(bview);
2439 // now call us again to do the above trick
2440 // but obviously we have to start from down below ;)
2441 return bview->text->selectNextWordToSpellcheck(bview, value);
2444 // Update the value if we changed paragraphs
2445 if (cursor.par() != tmppar) {
2446 setCursor(bview, cursor.par(), cursor.pos());
2447 value = float(cursor.y())/float(height);
2450 // Start the selection from here
2451 selection.cursor = cursor;
2454 getFont(bview->buffer(), cursor.par(), cursor.pos())
2455 .language()->code());
2456 // and find the end of the word (insets like optional hyphens
2457 // and ligature break are part of a word)
2458 while (cursor.pos() < cursor.par()->size()
2459 && (cursor.par()->isLetter(cursor.pos())))
2460 cursor.pos(cursor.pos() + 1);
2462 // Finally, we copy the word to a string and return it
2464 if (selection.cursor.pos() < cursor.pos()) {
2466 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2467 if (!cursor.par()->isInset(i))
2468 str += cursor.par()->getChar(i);
2471 return WordLangTuple(str, lang_code);
2475 // This one is also only for the spellchecker
2476 void LyXText::selectSelectedWord(BufferView * bview)
2478 if (the_locking_inset) {
2479 the_locking_inset->selectSelectedWord(bview);
2482 // move cursor to the beginning
2483 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2485 // set the sel cursor
2486 selection.cursor = cursor;
2488 // now find the end of the word
2489 while (cursor.pos() < cursor.par()->size()
2490 && (cursor.par()->isLetter(cursor.pos())))
2491 cursor.pos(cursor.pos() + 1);
2493 setCursor(bview, cursor.par(), cursor.pos());
2495 // finally set the selection
2496 setSelection(bview);
2500 // Delete from cursor up to the end of the current or next word.
2501 void LyXText::deleteWordForward(BufferView * bview)
2503 if (cursor.par()->empty())
2506 LyXCursor tmpcursor = cursor;
2507 tmpcursor.row(0); //Â ??
2508 selection.set(true); // to avoid deletion
2509 cursorRightOneWord(bview);
2510 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2511 selection.cursor = cursor;
2513 setSelection(bview);
2515 // Great, CutSelection() gets rid of multiple spaces.
2516 cutSelection(bview, true, false);
2521 // Delete from cursor to start of current or prior word.
2522 void LyXText::deleteWordBackward(BufferView * bview)
2524 if (cursor.par()->empty())
2527 LyXCursor tmpcursor = cursor;
2528 tmpcursor.row(0); // ??
2529 selection.set(true); // to avoid deletion
2530 cursorLeftOneWord(bview);
2531 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2532 selection.cursor = cursor;
2534 setSelection(bview);
2535 cutSelection(bview, true, false);
2540 // Kill to end of line.
2541 void LyXText::deleteLineForward(BufferView * bview)
2543 if (cursor.par()->empty())
2544 // Paragraph is empty, so we just go to the right
2547 LyXCursor tmpcursor = cursor;
2548 // We can't store the row over a regular setCursor
2549 // so we set it to 0 and reset it afterwards.
2550 tmpcursor.row(0); //Â ??
2551 selection.set(true); // to avoid deletion
2553 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2554 selection.cursor = cursor;
2556 setSelection(bview);
2557 // What is this test for ??? (JMarc)
2558 if (!selection.set()) {
2559 deleteWordForward(bview);
2561 cutSelection(bview, true, false);
2567 // Change the case of a word at cursor position.
2568 // This function directly manipulates Paragraph::text because there
2569 // is no Paragraph::SetChar currently. I did what I could to ensure
2570 // that it is correct. I guess part of it should be moved to
2571 // Paragraph, but it will have to change for 1.1 anyway. At least
2572 // it does not access outside of the allocated array as the older
2573 // version did. (JMarc)
2574 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2579 if (selection.set()) {
2580 from = selection.start;
2583 getWord(from, to, PARTIAL_WORD);
2584 setCursor(bview, to.par(), to.pos() + 1);
2587 changeRegionCase(bview, from, to, action);
2591 void LyXText::changeRegionCase(BufferView * bview,
2592 LyXCursor const & from,
2593 LyXCursor const & to,
2594 LyXText::TextCase action)
2596 lyx::Assert(from <= to);
2598 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2600 pos_type pos = from.pos();
2601 Paragraph * par = from.par();
2603 while (par && (pos != to.pos() || par != to.par())) {
2604 if (pos == par->size()) {
2609 unsigned char c = par->getChar(pos);
2610 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2612 case text_lowercase:
2615 case text_capitalization:
2617 action = text_lowercase;
2619 case text_uppercase:
2624 par->setChar(pos, c);
2625 checkParagraph(bview, par, pos);
2629 if (to.row() != from.row()) {
2630 refresh_y = from.y() - from.row()->baseline();
2631 refresh_row = from.row();
2632 status(bview, LyXText::NEED_MORE_REFRESH);
2637 void LyXText::transposeChars(BufferView & bview)
2639 Paragraph * tmppar = cursor.par();
2641 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2643 pos_type tmppos = cursor.pos();
2645 // First decide if it is possible to transpose at all
2647 // We are at the beginning of a paragraph.
2648 if (tmppos == 0) return;
2650 // We are at the end of a paragraph.
2651 if (tmppos == tmppar->size() - 1) return;
2653 unsigned char c1 = tmppar->getChar(tmppos);
2654 unsigned char c2 = tmppar->getChar(tmppos - 1);
2656 if (c1 != Paragraph::META_INSET
2657 && c2 != Paragraph::META_INSET) {
2658 tmppar->setChar(tmppos, c2);
2659 tmppar->setChar(tmppos - 1, c1);
2661 // We should have an implementation that handles insets
2662 // as well, but that will have to come later. (Lgb)
2663 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2667 void LyXText::Delete(BufferView * bview)
2669 // this is a very easy implementation
2671 LyXCursor old_cursor = cursor;
2672 int const old_cur_par_id = old_cursor.par()->id();
2673 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2674 old_cursor.par()->previous()->id() : 0;
2676 // just move to the right
2679 // CHECK Look at the comment here.
2680 // This check is not very good...
2681 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2682 // and that can very well delete the par or par->previous in
2683 // old_cursor. Will a solution where we compare paragraph id's
2685 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2686 == old_cur_par_prev_id
2687 && cursor.par()->id() != old_cur_par_id) {
2688 // delete-empty-paragraph-mechanism has done it
2692 // if you had success make a backspace
2693 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2694 LyXCursor tmpcursor = cursor;
2695 // to make sure undo gets the right cursor position
2696 cursor = old_cursor;
2697 setUndo(bview, Undo::DELETE,
2698 cursor.par(), cursor.par()->next());
2705 void LyXText::backspace(BufferView * bview)
2707 // Get the font that is used to calculate the baselineskip
2708 pos_type lastpos = cursor.par()->size();
2709 LyXFont rawparfont =
2710 cursor.par()->getFontSettings(bview->buffer()->params,
2713 if (cursor.pos() == 0) {
2714 // The cursor is at the beginning of a paragraph,
2715 // so the the backspace will collapse two paragraphs into one.
2717 // we may paste some paragraphs
2719 // is it an empty paragraph?
2722 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2723 // This is an empty paragraph and we delete it just by moving the cursor one step
2724 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2725 // of the paragraph.
2727 if (cursor.par()->previous()) {
2728 Paragraph * tmppar = cursor.par()->previous();
2729 if (cursor.par()->layout() == tmppar->layout()
2730 && cursor.par()->getAlign() == tmppar->getAlign()) {
2731 // Inherit bottom DTD from the paragraph below.
2732 // (the one we are deleting)
2733 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2734 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2735 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2740 // the layout things can change the height of a row !
2741 int const tmpheight = cursor.row()->height();
2742 setHeightOfRow(bview, cursor.row());
2743 if (cursor.row()->height() != tmpheight) {
2744 refresh_y = cursor.y() - cursor.row()->baseline();
2745 refresh_row = cursor.row();
2746 status(bview, LyXText::NEED_MORE_REFRESH);
2752 if (cursor.par()->previous()) {
2753 setUndo(bview, Undo::DELETE,
2754 cursor.par()->previous(), cursor.par()->next());
2757 Paragraph * tmppar = cursor.par();
2758 Row * tmprow = cursor.row();
2760 // We used to do cursorLeftIntern() here, but it is
2761 // not a good idea since it triggers the auto-delete
2762 // mechanism. So we do a cursorLeftIntern()-lite,
2763 // without the dreaded mechanism. (JMarc)
2764 if (cursor.par()->previous()) {
2765 // steps into the above paragraph.
2766 setCursorIntern(bview, cursor.par()->previous(),
2767 cursor.par()->previous()->size(),
2771 // Pasting is not allowed, if the paragraphs have different
2772 // layout. I think it is a real bug of all other
2773 // word processors to allow it. It confuses the user.
2774 // Even so with a footnote paragraph and a non-footnote
2775 // paragraph. I will not allow pasting in this case,
2776 // because the user would be confused if the footnote behaves
2777 // different wether it is open or closed.
2779 // Correction: Pasting is always allowed with standard-layout
2780 LyXTextClass const & tclass =
2781 bview->buffer()->params.getLyXTextClass();
2783 if (cursor.par() != tmppar
2784 && (cursor.par()->layout() == tmppar->layout()
2785 || tmppar->layout() == tclass.defaultLayout())
2786 && cursor.par()->getAlign() == tmppar->getAlign()) {
2787 removeParagraph(tmprow);
2789 mergeParagraph(bview->buffer()->params, cursor.par());
2791 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2792 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2793 // strangely enough it seems that commenting out the line above removes
2794 // most or all of the segfaults. I will however also try to move the
2795 // two Remove... lines in front of the PasteParagraph too.
2798 cursor.pos(cursor.pos() - 1);
2800 status(bview, LyXText::NEED_MORE_REFRESH);
2801 refresh_row = cursor.row();
2802 refresh_y = cursor.y() - cursor.row()->baseline();
2804 // remove the lost paragraph
2805 // This one is not safe, since the paragraph that the tmprow and the
2806 // following rows belong to has been deleted by the PasteParagraph
2807 // above. The question is... could this be moved in front of the
2809 //RemoveParagraph(tmprow);
2810 //RemoveRow(tmprow);
2812 // This rebuilds the rows.
2813 appendParagraph(bview, cursor.row());
2814 updateCounters(bview);
2816 // the row may have changed, block, hfills etc.
2817 setCursor(bview, cursor.par(), cursor.pos(), false);
2820 // this is the code for a normal backspace, not pasting
2822 setUndo(bview, Undo::DELETE,
2823 cursor.par(), cursor.par()->next());
2824 // We used to do cursorLeftIntern() here, but it is
2825 // not a good idea since it triggers the auto-delete
2826 // mechanism. So we do a cursorLeftIntern()-lite,
2827 // without the dreaded mechanism. (JMarc)
2828 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2829 false, cursor.boundary());
2831 // some insets are undeletable here
2832 if (cursor.par()->isInset(cursor.pos())) {
2833 if (!cursor.par()->getInset(cursor.pos())->deletable())
2835 // force complete redo when erasing display insets
2836 // this is a cruel method but safe..... Matthias
2837 if (cursor.par()->getInset(cursor.pos())->display() ||
2838 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2839 cursor.par()->erase(cursor.pos());
2840 redoParagraph(bview);
2845 Row * row = cursor.row();
2846 int y = cursor.y() - row->baseline();
2848 // remember that a space at the end of a row doesnt count
2849 // when calculating the fill
2850 if (cursor.pos() < rowLast(row) ||
2851 !cursor.par()->isLineSeparator(cursor.pos())) {
2852 row->fill(row->fill() + singleWidth(bview,
2857 // some special code when deleting a newline. This is similar
2858 // to the behavior when pasting paragraphs
2859 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2860 cursor.par()->erase(cursor.pos());
2861 // refresh the positions
2863 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2864 tmprow = tmprow->next();
2865 tmprow->pos(tmprow->pos() - 1);
2867 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2868 cursor.pos(cursor.pos() - 1);
2870 if (cursor.pos() < cursor.par()->size()
2871 && !cursor.par()->isSeparator(cursor.pos())) {
2872 cursor.par()->insertChar(cursor.pos(), ' ');
2873 setCharFont(bview->buffer(), cursor.par(),
2874 cursor.pos(), current_font);
2875 // refresh the positions
2877 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2878 tmprow = tmprow->next();
2879 tmprow->pos(tmprow->pos() + 1);
2883 cursor.par()->erase(cursor.pos());
2885 // refresh the positions
2887 while (tmprow->next()
2888 && tmprow->next()->par() == row->par()) {
2889 tmprow = tmprow->next();
2890 tmprow->pos(tmprow->pos() - 1);
2893 // delete newlines at the beginning of paragraphs
2894 while (!cursor.par()->empty() &&
2895 cursor.par()->isNewline(cursor.pos()) &&
2896 cursor.pos() == beginningOfMainBody(bview->buffer(),
2898 cursor.par()->erase(cursor.pos());
2899 // refresh the positions
2901 while (tmprow->next() &&
2902 tmprow->next()->par() == row->par()) {
2903 tmprow = tmprow->next();
2904 tmprow->pos(tmprow->pos() - 1);
2909 // is there a break one row above
2910 if (row->previous() && row->previous()->par() == row->par()) {
2911 z = nextBreakPoint(bview, row->previous(),
2913 if (z >= row->pos()) {
2916 Row * tmprow = row->previous();
2918 // maybe the current row is now empty
2919 if (row->pos() >= row->par()->size()) {
2924 breakAgainOneRow(bview, row);
2925 if (row->next() && row->next()->par() == row->par())
2926 need_break_row = row->next();
2931 // set the dimensions of the row above
2932 y -= tmprow->height();
2933 tmprow->fill(fill(bview, tmprow,
2935 setHeightOfRow(bview, tmprow);
2938 refresh_row = tmprow;
2939 status(bview, LyXText::NEED_MORE_REFRESH);
2940 setCursor(bview, cursor.par(), cursor.pos(),
2941 false, cursor.boundary());
2942 //current_font = rawtmpfont;
2943 //real_current_font = realtmpfont;
2944 // check, whether the last character's font has changed.
2946 cursor.par()->getFontSettings(bview->buffer()->params,
2947 cursor.par()->size() - 1))
2948 redoHeightOfParagraph(bview, cursor);
2953 // break the cursor row again
2954 if (row->next() && row->next()->par() == row->par() &&
2955 (rowLast(row) == row->par()->size() - 1 ||
2956 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2958 // it can happen that a paragraph loses one row
2959 // without a real breakup. This is when a word
2960 // is to long to be broken. Well, I don t care this
2962 if (rowLast(row) == row->par()->size() - 1)
2963 removeRow(row->next());
2967 status(bview, LyXText::NEED_MORE_REFRESH);
2969 breakAgainOneRow(bview, row);
2970 // will the cursor be in another row now?
2971 if (row->next() && row->next()->par() == row->par() &&
2972 rowLast(row) <= cursor.pos()) {
2974 breakAgainOneRow(bview, row);
2977 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2979 if (row->next() && row->next()->par() == row->par())
2980 need_break_row = row->next();
2984 // set the dimensions of the row
2985 row->fill(fill(bview, row, workWidth(bview)));
2986 int const tmpheight = row->height();
2987 setHeightOfRow(bview, row);
2988 if (tmpheight == row->height())
2989 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2991 status(bview, LyXText::NEED_MORE_REFRESH);
2994 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2998 // current_font = rawtmpfont;
2999 // real_current_font = realtmpfont;
3001 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
3002 != cursor.boundary())
3003 setCursor(bview, cursor.par(), cursor.pos(), false,
3004 !cursor.boundary());
3006 lastpos = cursor.par()->size();
3007 if (cursor.pos() == lastpos)
3008 setCurrentFont(bview);
3010 // check, whether the last characters font has changed.
3012 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3013 redoHeightOfParagraph(bview, cursor);
3015 // now the special right address boxes
3016 if (cursor.par()->layout()->margintype
3017 == MARGIN_RIGHT_ADDRESS_BOX) {
3018 redoDrawingOfParagraph(bview, cursor);
3024 bool LyXText::paintRowBackground(DrawRowParams & p)
3026 bool clear_area = true;
3028 LyXFont font(LyXFont::ALL_SANE);
3030 pos_type const last = rowLastPrintable(p.row);
3032 if (!p.bv->screen().forceClear() && last == p.row->pos()
3033 && p.row->par()->isInset(p.row->pos())) {
3034 inset = p.row->par()->getInset(p.row->pos());
3036 clear_area = inset->doClearArea();
3046 int const y = p.yo < 0 ? 0 : p.yo;
3047 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3048 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3055 int h = p.row->baseline() - inset->ascent(p.bv, font);
3057 // first clear the whole row above the inset!
3059 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3062 // clear the space below the inset!
3063 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3064 if ((p.row->height() - h) > 0) {
3065 p.pain->fillRectangle(p.xo, p.yo + h,
3066 p.width, p.row->height() - h, backgroundColor());
3069 // clear the space behind the inset, if needed
3070 if (!inset->display() && !inset->needFullRow()) {
3071 int const xp = int(p.x) + inset->width(p.bv, font);
3072 if (p.width - xp > 0) {
3073 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3074 p.row->height(), backgroundColor());
3082 void LyXText::paintRowSelection(DrawRowParams & p)
3084 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3086 // the current selection
3087 int const startx = selection.start.x();
3088 int const endx = selection.end.x();
3089 int const starty = selection.start.y();
3090 int const endy = selection.end.y();
3091 Row const * startrow = selection.start.row();
3092 Row const * endrow = selection.end.row();
3096 if (bidi_same_direction) {
3100 int h = row->height();
3102 if (startrow == row && endrow == row) {
3103 if (startx < endx) {
3106 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3110 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3112 } else if (startrow == row) {
3113 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3114 int const w = (is_rtl) ? startx : (p.width - startx);
3115 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3116 } else if (endrow == row) {
3117 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3118 int const w = (is_rtl) ? (p.width - endx) : endx;
3119 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3120 } else if (p.y > starty && p.y < endy) {
3121 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3124 } else if (startrow != row && endrow != row) {
3125 if (p.y > starty && p.y < endy) {
3127 int h = row->height();
3128 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3133 if ((startrow != row && !is_rtl) || (endrow != row && is_rtl))
3134 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3136 Buffer const * buffer = p.bv->buffer();
3137 Paragraph * par = row->par();
3138 pos_type main_body = beginningOfMainBody(buffer, par);
3139 pos_type const last = rowLastPrintable(row);
3142 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3143 pos_type pos = vis2log(vpos);
3144 float const old_tmpx = tmpx;
3145 if (main_body > 0 && pos == main_body - 1) {
3146 LyXLayout_ptr const & layout = par->layout();
3147 LyXFont const lfont = getLabelFont(buffer, par);
3149 tmpx += p.label_hfill + font_metrics::width(layout->labelsep, lfont);
3151 if (par->isLineSeparator(main_body - 1))
3152 tmpx -= singleWidth(p.bv, par, main_body - 1);
3155 if (hfillExpansion(buffer, row, pos)) {
3156 tmpx += singleWidth(p.bv, par, pos);
3157 if (pos >= main_body)
3160 tmpx += p.label_hfill;
3163 else if (par->isSeparator(pos)) {
3164 tmpx += singleWidth(p.bv, par, pos);
3165 if (pos >= main_body)
3166 tmpx += p.separator;
3168 tmpx += singleWidth(p.bv, par, pos);
3171 if ((startrow != row || selection.start.pos() <= pos) &&
3172 (endrow != row || pos < selection.end.pos())) {
3173 // Here we do not use p.x as p.xo was added to p.x.
3174 p.pain->fillRectangle(int(old_tmpx), p.yo,
3175 int(tmpx - old_tmpx + 1),
3176 row->height(), LColor::selection);
3180 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3181 p.pain->fillRectangle(p.xo + int(tmpx),
3182 p.yo, int(p.bv->workWidth() - tmpx),
3183 row->height(), LColor::selection);
3188 void LyXText::paintRowAppendix(DrawRowParams & p)
3190 // FIXME: can be just p.width ?
3191 int const ww = p.bv->workWidth();
3192 Paragraph * firstpar = p.row->par();
3194 if (firstpar->params().appendix()) {
3195 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3196 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3201 void LyXText::paintRowDepthBar(DrawRowParams & p)
3203 Paragraph::depth_type const depth = p.row->par()->getDepth();
3208 Paragraph::depth_type prev_depth = 0;
3209 if (p.row->previous())
3210 prev_depth = p.row->previous()->par()->getDepth();
3211 Paragraph::depth_type next_depth = 0;
3213 next_depth = p.row->next()->par()->getDepth();
3215 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3216 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3217 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3219 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3221 int const w = LYX_PAPER_MARGIN / 5;
3223 if (i > prev_depth) {
3224 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3226 if (i > next_depth) {
3227 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3233 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3235 int const arrow_size = 4;
3236 int const space_size = int(vsp.inPixels(bv));
3238 if (vsp.kind() != VSpace::LENGTH) {
3244 int const min_size = max(3 * arrow_size,
3245 font_metrics::maxAscent(font)
3246 + font_metrics::maxDescent(font));
3248 if (vsp.length().len().value() < 0.0)
3251 return max(min_size, space_size);
3255 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3256 VSpace const & vsp, int start)
3258 int const arrow_size = 4;
3259 int const size = getLengthMarkerHeight(p.bv, vsp);
3260 int const end = start + size;
3262 // the label to display (if any)
3264 // y-values for top arrow
3266 // y-values for bottom arrow
3268 switch (vsp.kind()) {
3269 case VSpace::LENGTH:
3271 str = prefix + " (" + vsp.asLyXCommand() + ")";
3272 // adding or removing space
3273 bool const added = !(vsp.length().len().value() < 0.0);
3274 ty1 = added ? (start + arrow_size) : start;
3275 ty2 = added ? start : (start + arrow_size);
3276 by1 = added ? (end - arrow_size) : end;
3277 by2 = added ? end : (end - arrow_size);
3281 str = prefix + _(" (vertical fill)");
3286 // nothing to draw here
3290 int const leftx = p.xo + leftMargin(p.bv, p.row);
3291 int const midx = leftx + arrow_size;
3292 int const rightx = midx + arrow_size;
3300 font.setColor(LColor::added_space).decSize();
3301 font_metrics::rectText(str, font, w, a, d);
3303 p.pain->rectText(leftx + 2 * arrow_size + 5,
3304 start + ((end - start) / 2) + d,
3308 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3309 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3312 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3313 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3316 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3322 int LyXText::paintPageBreak(string const & label, int y, DrawRowParams & p)
3325 pb_font.setColor(LColor::pagebreak).decSize();
3330 font_metrics::rectText(label, pb_font, w, a, d);
3332 int const text_start = p.xo + ((p.width - w) / 2);
3333 int const text_end = text_start + w;
3335 p.pain->rectText(text_start, y + d, label, pb_font);
3337 p.pain->line(p.xo, y, text_start, y,
3338 LColor::pagebreak, Painter::line_onoffdash);
3339 p.pain->line(text_end, y, p.xo + p.width, y,
3340 LColor::pagebreak, Painter::line_onoffdash);
3342 return 3 * defaultHeight();
3346 void LyXText::paintFirstRow(DrawRowParams & p)
3348 Paragraph * par = p.row->par();
3349 ParagraphParameters const & parparams = par->params();
3351 // start of appendix?
3352 if (parparams.startOfAppendix()) {
3353 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3358 // think about the margins
3359 if (!p.row->previous() && bv_owner)
3360 y_top += LYX_PAPER_MARGIN;
3362 // draw a top pagebreak
3363 if (parparams.pagebreakTop()) {
3364 y_top += paintPageBreak(_("Page Break (top)"),
3365 p.yo + y_top + 2 * defaultHeight(), p);
3368 // draw the additional space if needed:
3369 y_top += drawLengthMarker(p, _("Space above"),
3370 parparams.spaceTop(), p.yo + y_top);
3372 Buffer const * buffer = p.bv->buffer();
3374 LyXLayout_ptr const & layout = par->layout();
3376 // think about the parskip
3377 // some parskips VERY EASY IMPLEMENTATION
3378 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3379 if (par->previous()) {
3380 if (layout->latextype == LATEX_PARAGRAPH
3381 && !par->getDepth()) {
3382 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3384 LyXLayout_ptr const & playout =
3385 par->previous()->layout();
3386 if (playout->latextype == LATEX_PARAGRAPH
3387 && !par->previous()->getDepth()) {
3388 // is it right to use defskip here, too? (AS)
3389 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3395 int const ww = p.bv->workWidth();
3398 if (parparams.lineTop()) {
3399 LyXFont font(LyXFont::ALL_SANE);
3400 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3404 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3405 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3406 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3407 LColor::topline, Painter::line_solid,
3408 Painter::line_thick);
3413 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3415 // should we print a label?
3416 if (layout->labeltype >= LABEL_STATIC
3417 && (layout->labeltype != LABEL_STATIC
3418 || layout->latextype != LATEX_ENVIRONMENT
3419 || par->isFirstInSequence())) {
3421 LyXFont font = getLabelFont(buffer, par);
3422 if (!par->getLabelstring().empty()) {
3424 string const str = par->getLabelstring();
3426 // this is special code for the chapter layout. This is
3427 // printed in an extra row and has a pagebreak at
3429 if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3430 if (buffer->params.secnumdepth >= 0) {
3431 float spacing_val = 1.0;
3432 if (!parparams.spacing().isDefault()) {
3433 spacing_val = parparams.spacing().getValue();
3435 spacing_val = buffer->params.spacing.getValue();
3439 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
3440 + int(layout->parsep) * defaultHeight();
3443 x = ww - leftMargin(p.bv, p.row) -
3444 font_metrics::width(str, font);
3447 p.pain->text(int(x),
3448 p.yo + p.row->baseline() -
3449 p.row->ascent_of_text() - maxdesc,
3454 x = ww - leftMargin(p.bv, p.row)
3455 + font_metrics::width(layout->labelsep, font);
3457 x = p.x - font_metrics::width(layout->labelsep, font)
3458 - font_metrics::width(str, font);
3461 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3464 // the labels at the top of an environment.
3465 // More or less for bibliography
3466 } else if (par->isFirstInSequence() &&
3467 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3468 layout->labeltype == LABEL_BIBLIO ||
3469 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3470 LyXFont font = getLabelFont(buffer, par);
3471 if (!par->getLabelstring().empty()) {
3472 string const str = par->getLabelstring();
3473 float spacing_val = 1.0;
3474 if (!parparams.spacing().isDefault()) {
3475 spacing_val = parparams.spacing().getValue();
3477 spacing_val = buffer->params.spacing.getValue();
3481 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
3482 + (layout->labelbottomsep * defaultHeight()));
3485 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3486 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3487 + ww - rightMargin(buffer, p.row)) / 2;
3488 x -= font_metrics::width(str, font) / 2;
3489 } else if (is_rtl) {
3490 x = ww - leftMargin(p.bv, p.row) -
3491 font_metrics::width(str, font);
3493 p.pain->text(int(x), p.yo + p.row->baseline()
3494 - p.row->ascent_of_text() - maxdesc,
3499 if (layout->labeltype == LABEL_BIBLIO && par->bibkey) {
3500 LyXFont font = getLayoutFont(buffer, par);
3503 x = ww - leftMargin(p.bv, p.row)
3504 + font_metrics::width(layout->labelsep, font);
3506 x = p.x - font_metrics::width(layout->labelsep, font)
3507 - par->bibkey->width(p.bv, font);
3509 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3514 void LyXText::paintLastRow(DrawRowParams & p)
3516 Paragraph * par = p.row->par();
3517 ParagraphParameters const & parparams = par->params();
3518 int y_bottom = p.row->height() - 1;
3520 // think about the margins
3521 if (!p.row->next() && bv_owner)
3522 y_bottom -= LYX_PAPER_MARGIN;
3524 int const ww = p.bv->workWidth();
3526 // draw a bottom pagebreak
3527 if (parparams.pagebreakBottom()) {
3528 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
3529 p.yo + y_bottom - 2 * defaultHeight(), p);
3532 // draw the additional space if needed:
3533 int const height = getLengthMarkerHeight(p.bv,
3534 parparams.spaceBottom());
3535 y_bottom -= drawLengthMarker(p, _("Space below"),
3536 parparams.spaceBottom(),
3537 p.yo + y_bottom - height);
3539 Buffer const * buffer = p.bv->buffer();
3541 // draw a bottom line
3542 if (parparams.lineBottom()) {
3543 LyXFont font(LyXFont::ALL_SANE);
3544 int const asc = font_metrics::ascent('x',
3545 getFont(buffer, par,
3546 max(pos_type(0), par->size() - 1)));
3550 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3551 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3552 int const y = p.yo + y_bottom;
3553 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3554 Painter::line_thick);
3559 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3560 int const endlabel = par->getEndLabel();
3565 case END_LABEL_FILLED_BOX:
3567 LyXFont const font = getLabelFont(buffer, par);
3568 int const size = int(0.75 * font_metrics::maxAscent(font));
3569 int const y = (p.yo + p.row->baseline()) - size;
3570 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3572 if (p.row->fill() <= size)
3573 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3575 if (endlabel == END_LABEL_BOX) {
3576 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3578 p.pain->fillRectangle(x, y, size, size,
3583 case END_LABEL_STATIC:
3586 LyXFont font(LyXFont::ALL_SANE);
3587 font = getLabelFont(buffer, par);
3589 LyXFont font = getLabelFont(buffer, par);
3591 string const & str = par->layout()->endlabelstring();
3592 int const x = is_rtl ?
3593 int(p.x) - font_metrics::width(str, font)
3594 : ww - rightMargin(buffer, p.row) - p.row->fill();
3595 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3598 case END_LABEL_NO_LABEL:
3604 void LyXText::paintRowText(DrawRowParams & p)
3606 Paragraph * par = p.row->par();
3607 Buffer const * buffer = p.bv->buffer();
3609 pos_type const last = rowLastPrintable(p.row);
3610 pos_type main_body =
3611 beginningOfMainBody(buffer, par);
3612 if (main_body > 0 &&
3613 (main_body - 1 > last ||
3614 !par->isLineSeparator(main_body - 1))) {
3618 LyXLayout_ptr const & layout = par->layout();
3620 pos_type vpos = p.row->pos();
3621 while (vpos <= last) {
3622 if (p.x > p.bv->workWidth())
3624 pos_type pos = vis2log(vpos);
3625 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3626 p.x += singleWidth(p.bv, par, pos);
3630 if (main_body > 0 && pos == main_body - 1) {
3631 int const lwidth = font_metrics::width(layout->labelsep,
3632 getLabelFont(buffer, par));
3634 p.x += p.label_hfill + lwidth
3635 - singleWidth(p.bv, par, main_body - 1);
3638 if (par->isHfill(pos)) {
3641 int const y0 = p.yo + p.row->baseline();
3642 int const y1 = y0 - defaultHeight() / 2;
3644 p.pain->line(int(p.x), y1, int(p.x), y0,
3645 LColor::added_space);
3647 if (hfillExpansion(buffer, p.row, pos)) {
3648 int const y2 = (y0 + y1) / 2;
3650 if (pos >= main_body) {
3651 p.pain->line(int(p.x), y2,
3652 int(p.x + p.hfill), y2,
3653 LColor::added_space,
3654 Painter::line_onoffdash);
3657 p.pain->line(int(p.x), y2,
3658 int(p.x + p.label_hfill), y2,
3659 LColor::added_space,
3660 Painter::line_onoffdash);
3661 p.x += p.label_hfill;
3663 p.pain->line(int(p.x), y1,
3665 LColor::added_space);
3669 } else if (par->isSeparator(pos)) {
3670 p.x += singleWidth(p.bv, par, pos);
3671 if (pos >= main_body)
3682 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3683 Row * row, int y, bool cleared)
3685 if (row->height() <= 0) {
3686 lyxerr << "LYX_ERROR: row.height: "
3687 << row->height() << endl;
3693 // set up drawing parameters
3695 p.pain = &bv->painter();
3699 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3700 if (inset_owner && (p.x < 0))
3704 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3705 p.cleared = cleared;
3709 // clear to background if necessary
3710 p.cleared = paintRowBackground(p);
3712 // paint the selection background
3713 if (selection.set()) {
3714 paintRowSelection(p);
3717 // vertical lines for appendix
3718 paintRowAppendix(p);
3720 // environment depth brackets
3721 paintRowDepthBar(p);
3723 // draw any stuff wanted for a first row of a paragraph
3728 // draw any stuff wanted for the last row of a paragraph
3729 if (!row->next() || (row->next()->par() != row->par())) {
3738 int LyXText::defaultHeight() const
3740 LyXFont font(LyXFont::ALL_SANE);
3741 return int(font_metrics::maxAscent(font)
3742 + font_metrics::maxDescent(font) * 1.5);
3746 // returns the column near the specified x-coordinate of the row
3747 // x is set to the real beginning of this column
3749 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3750 bool & boundary) const
3753 float fill_separator;
3755 float fill_label_hfill;
3757 prepareToPrint(bview, row, tmpx, fill_separator,
3758 fill_hfill, fill_label_hfill);
3760 pos_type vc = row->pos();
3761 pos_type last = rowLastPrintable(row);
3764 LyXLayout_ptr const & layout = row->par()->layout();
3766 bool left_side = false;
3768 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3769 float last_tmpx = tmpx;
3771 if (main_body > 0 &&
3772 (main_body - 1 > last ||
3773 !row->par()->isLineSeparator(main_body - 1)))
3776 while (vc <= last && tmpx <= x) {
3779 if (main_body > 0 && c == main_body-1) {
3780 tmpx += fill_label_hfill +
3781 font_metrics::width(layout->labelsep,
3782 getLabelFont(bview->buffer(), row->par()));
3783 if (row->par()->isLineSeparator(main_body - 1))
3784 tmpx -= singleWidth(bview, row->par(), main_body-1);
3787 if (hfillExpansion(bview->buffer(), row, c)) {
3788 tmpx += singleWidth(bview, row->par(), c);
3792 tmpx += fill_label_hfill;
3794 else if (row->par()->isSeparator(c)) {
3795 tmpx += singleWidth(bview, row->par(), c);
3797 tmpx+= fill_separator;
3799 tmpx += singleWidth(bview, row->par(), c);
3803 if ((tmpx + last_tmpx) / 2 > x) {
3808 if (vc > last + 1) // This shouldn't happen.
3812 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3813 // some speedup if rtl_support=false
3814 && (!row->next() || row->next()->par() != row->par());
3815 bool const rtl = (lastrow)
3816 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3817 : false; // If lastrow is false, we don't need to compute
3818 // the value of rtl.
3820 if (row->pos() > last) // Row is empty?
3823 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3824 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3826 else if (vc == row->pos()) {
3828 if (bidi_level(c) % 2 == 1)
3831 c = vis2log(vc - 1);
3832 bool const rtl = (bidi_level(c) % 2 == 1);
3833 if (left_side == rtl) {
3835 boundary = isBoundary(bview->buffer(), row->par(), c);
3839 if (row->pos() <= last && c > last
3840 && row->par()->isNewline(last)) {
3841 if (bidi_level(last) % 2 == 0)
3842 tmpx -= singleWidth(bview, row->par(), last);
3844 tmpx += singleWidth(bview, row->par(), last);
3854 // returns pointer to a specified row
3855 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3860 Row * tmprow = firstrow;
3863 // find the first row of the specified paragraph
3864 while (tmprow->next() && tmprow->par() != par) {
3865 y += tmprow->height();
3866 tmprow = tmprow->next();
3869 // now find the wanted row
3870 while (tmprow->pos() < pos
3872 && tmprow->next()->par() == par
3873 && tmprow->next()->pos() <= pos) {
3874 y += tmprow->height();
3875 tmprow = tmprow->next();
3882 Row * LyXText::getRowNearY(int & y) const
3885 // If possible we should optimize this method. (Lgb)
3886 Row * tmprow = firstrow;
3889 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3890 tmpy += tmprow->height();
3891 tmprow = tmprow->next();
3894 y = tmpy; // return the real y
3896 //lyxerr << "returned y = " << y << endl;
3900 // Search from the current cursor position.
3902 Row * tmprow = cursor.row();
3903 int tmpy = cursor.y() - tmprow->baseline();
3905 lyxerr << "cursor.y() = " << tmpy << endl;
3906 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3907 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3908 lyxerr << "first = " << first << endl;
3909 lyxerr << "y = " << y << endl;
3912 lyxerr << "up" << endl;
3914 tmpy -= tmprow->height();
3915 tmprow = tmprow->previous();
3916 } while (tmprow && tmpy - tmprow->height() >= y);
3917 } else if (y > tmpy) {
3918 lyxerr << "down" << endl;
3920 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3921 tmpy += tmprow->height();
3922 tmprow = tmprow->next();
3925 lyxerr << "equal" << endl;
3928 y = tmpy; // return the real y
3930 lyxerr << "returned y = " << y << endl;
3938 int LyXText::getDepth() const
3940 return cursor.par()->getDepth();