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 "lyxtextclasslist.h"
16 #include "paragraph.h"
18 #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 "bufferview_funcs.h"
28 #include "BufferView.h"
30 #include "ParagraphParameters.h"
31 #include "undo_funcs.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
36 #include "support/textutils.h"
37 #include "support/LAssert.h"
38 #include "support/lstrings.h"
50 int const LYX_PAPER_MARGIN = 20;
54 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
57 int LyXText::workWidth(BufferView * bview) const
60 return inset_owner->textWidth(bview);
62 return bview->workWidth();
66 int LyXText::workWidth(BufferView * bview, Inset * inset) const
71 par = inset->parOwner();
73 pos = par->getPositionOfInset(inset);
75 if (!par || pos == -1) {
76 lyxerr << "LyXText::workWidth: something is wrong,"
77 " fall back to the brute force method" << endl;
78 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
79 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
80 for (; it != end; ++it) {
90 return workWidth(bview);
93 LyXLayout_ptr const & layout = par->layout();
95 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
96 // Optimization here: in most cases, the real row is
97 // not needed, but only the par/pos values. So we just
98 // construct a dummy row for leftMargin. (JMarc)
102 return workWidth(bview) - leftMargin(bview, &dummyrow);
105 Row * row = getRow(par, pos, dummy_y);
107 while (frow->previous() && frow->par() == frow->previous()->par())
108 frow = frow->previous();
109 unsigned int maxw = 0;
110 while (frow->next() && frow->par() == frow->next()->par()) {
111 if ((frow != row) && (maxw < frow->width()))
112 maxw = frow->width();
118 return workWidth(bview);
122 int LyXText::getRealCursorX(BufferView * bview) const
125 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
126 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
131 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
134 if (!Encodings::is_arabic(c))
135 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
136 return c + (0xb0 - '0');
140 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
141 unsigned char next_char = ' ';
143 for (pos_type i = pos+1; i < par->size(); ++i)
144 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
145 next_char = par->getChar(i);
149 if (Encodings::is_arabic(next_char)) {
150 if (Encodings::is_arabic(prev_char))
151 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
153 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
155 if (Encodings::is_arabic(prev_char))
156 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
158 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
162 // This is the comments that some of the warnings below refers to.
163 // There are some issues in this file and I don't think they are
164 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
165 // this is a problem that has been here almost from day one and that a
166 // larger userbase with differenct access patters triggers the bad
167 // behaviour. (segfaults.) What I think happen is: In several places
168 // we store the paragraph in the current cursor and then moves the
169 // cursor. This movement of the cursor will delete paragraph at the
170 // old position if it is now empty. This will make the temporary
171 // pointer to the old cursor paragraph invalid and dangerous to use.
172 // And is some cases this will trigger a segfault. I have marked some
173 // of the cases where this happens with a warning, but I am sure there
174 // are others in this file and in text2.C. There is also a note in
175 // Delete() that you should read. In Delete I store the paragraph->id
176 // instead of a pointer to the paragraph. I am pretty sure this faulty
177 // use of temporary pointers to paragraphs that might have gotten
178 // invalidated (through a cursor movement) before they are used, are
179 // the cause of the strange crashes we get reported often.
181 // It is very tiresom to change this code, especially when it is as
182 // hard to read as it is. Help to fix all the cases where this is done
183 // would be greately appreciated.
187 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
190 char const c = par->getChar(pos);
191 return singleWidth(bview, par, pos, c);
195 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
196 pos_type pos, char c) const
198 LyXFont const font = getFont(bview->buffer(), par, pos);
200 // The most common case is handled first (Asger)
201 if (IsPrintable(c)) {
202 if (font.language()->RightToLeft()) {
203 if (font.language()->lang() == "arabic" &&
204 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
205 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
206 if (Encodings::IsComposeChar_arabic(c))
209 c = transformChar(c, par, pos);
210 else if (font.language()->lang() == "hebrew" &&
211 Encodings::IsComposeChar_hebrew(c))
214 return font_metrics::width(c, font);
216 } else if (IsHfillChar(c)) {
217 // Because of the representation as vertical lines
219 } else if (c == Paragraph::META_INSET) {
220 Inset * tmpinset = par->getInset(pos);
223 // this IS needed otherwise on initialitation we don't get the fill
224 // of the row right (ONLY on initialization if we read a file!)
225 // should be changed! (Jug 20011204)
226 tmpinset->update(bview, font);
228 return tmpinset->width(bview, font);
232 } else if (IsSeparatorChar(c))
234 else if (IsNewlineChar(c))
236 return font_metrics::width(c, font);
240 // Returns the paragraph position of the last character in the specified row
241 pos_type LyXText::rowLast(Row const * row) const
243 if (!row->next() || row->next()->par() != row->par()) {
244 return row->par()->size() - 1;
246 return row->next()->pos() - 1;
251 pos_type LyXText::rowLastPrintable(Row const * row) const
253 pos_type const last = rowLast(row);
254 bool ignore_the_space_on_the_last_position = true;
256 // we have to consider a space on the last position in this case!
257 if (row->next() && row->par() == row->next()->par() &&
258 row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
259 (ins=row->next()->par()->getInset(last+1)) &&
260 (ins->needFullRow() || ins->display()))
262 ignore_the_space_on_the_last_position = false;
264 if (last >= row->pos()
266 && row->next()->par() == row->par()
267 && row->par()->isSeparator(last)
268 && ignore_the_space_on_the_last_position)
275 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
277 bidi_same_direction = true;
278 if (!lyxrc.rtl_support) {
283 Inset * inset = row->par()->inInset();
284 if (inset && inset->owner() &&
285 inset->owner()->lyxCode() == Inset::ERT_CODE) {
290 bidi_start = row->pos();
291 bidi_end = rowLastPrintable(row);
293 if (bidi_start > bidi_end) {
298 if (bidi_end + 2 - bidi_start >
299 static_cast<pos_type>(log2vis_list.size())) {
301 (bidi_end + 2 - bidi_start < 500) ?
302 500 : 2 * (bidi_end + 2 - bidi_start);
303 log2vis_list.resize(new_size);
304 vis2log_list.resize(new_size);
305 bidi_levels.resize(new_size);
308 vis2log_list[bidi_end + 1 - bidi_start] = -1;
309 log2vis_list[bidi_end + 1 - bidi_start] = -1;
313 row->par()->isRightToLeftPar(buf->params);
317 pos_type const main_body = beginningOfMainBody(buf, row->par());
319 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
320 bool is_space = row->par()->isLineSeparator(lpos);
322 (is_space && lpos + 1 <= bidi_end &&
323 !row->par()->isLineSeparator(lpos + 1) &&
324 !row->par()->isNewline(lpos + 1))
326 LyXFont font = row->par()->getFontSettings(buf->params, pos);
327 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
328 font.number() == LyXFont::ON &&
329 row->par()->getFontSettings(buf->params, lpos - 1).number()
331 font = row->par()->getFontSettings(buf->params, lpos);
336 bool new_rtl = font.isVisibleRightToLeft();
337 bool new_rtl0 = font.isRightToLeft();
340 if (lpos == main_body - 1
341 && row->pos() < main_body - 1
343 new_level = (rtl_par) ? 1 : 0;
344 new_rtl = new_rtl0 = rtl_par;
346 new_level = (new_rtl) ? 1 : 2;
348 new_level = (rtl_par) ? 2 : 0;
350 if (is_space && new_level >= level) {
356 int new_level2 = new_level;
358 if (level == new_level && rtl0 != new_rtl0) {
360 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
361 } else if (level < new_level) {
362 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
363 if (new_level > rtl_par)
364 bidi_same_direction = false;
366 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
369 bidi_levels[lpos - bidi_start] = new_level;
371 while (level > new_level2) {
372 pos_type old_lpos = stack[--level];
373 int delta = lpos - old_lpos - 1;
376 log2vis_list[lpos - bidi_start] += delta;
377 log2vis_list[old_lpos - bidi_start] += delta;
379 while (level < new_level)
380 stack[level++] = lpos;
384 pos_type const old_lpos = stack[--level];
385 int delta = bidi_end - old_lpos;
388 log2vis_list[old_lpos - bidi_start] += delta;
391 pos_type vpos = bidi_start - 1;
392 for (pos_type lpos = bidi_start;
393 lpos <= bidi_end; ++lpos) {
394 vpos += log2vis_list[lpos - bidi_start];
395 vis2log_list[vpos - bidi_start] = lpos;
396 log2vis_list[lpos - bidi_start] = vpos;
401 // This method requires a previous call to ComputeBidiTables()
402 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
405 if (!lyxrc.rtl_support || pos == 0)
408 if (!bidi_InRange(pos - 1)) {
409 /// This can happen if pos is the first char of a row.
410 /// Returning false in this case is incorrect!
414 bool const rtl = bidi_level(pos - 1) % 2;
415 bool const rtl2 = bidi_InRange(pos)
416 ? bidi_level(pos) % 2
417 : par->isRightToLeftPar(buf->params);
422 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
423 pos_type pos, LyXFont const & font) const
425 if (!lyxrc.rtl_support)
426 return false; // This is just for speedup
428 bool const rtl = font.isVisibleRightToLeft();
429 bool const rtl2 = bidi_InRange(pos)
430 ? bidi_level(pos) % 2
431 : par->isRightToLeftPar(buf->params);
436 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
438 // Draw end-of-line marker
439 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
440 int const wid = font_metrics::width('n', font);
441 int const asc = font_metrics::maxAscent(font);
442 int const y = p.yo + p.row->baseline();
446 yp[0] = int(y - 0.875 * asc * 0.75);
447 yp[1] = int(y - 0.500 * asc * 0.75);
448 yp[2] = int(y - 0.125 * asc * 0.75);
450 if (bidi_level(pos) % 2 == 0) {
451 xp[0] = int(p.x + wid * 0.375);
453 xp[2] = int(p.x + wid * 0.375);
455 xp[0] = int(p.x + wid * 0.625);
456 xp[1] = int(p.x + wid);
457 xp[2] = int(p.x + wid * 0.625);
460 p.pain->lines(xp, yp, 3, LColor::eolmarker);
462 yp[0] = int(y - 0.500 * asc * 0.75);
463 yp[1] = int(y - 0.500 * asc * 0.75);
464 yp[2] = int(y - asc * 0.75);
466 if (bidi_level(pos) % 2 == 0) {
468 xp[1] = int(p.x + wid);
469 xp[2] = int(p.x + wid);
471 xp[0] = int(p.x + wid);
476 p.pain->lines(xp, yp, 3, LColor::eolmarker);
482 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
484 Inset * inset = p.row->par()->getInset(pos);
486 // FIXME: shouldn't happen
491 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
492 // we need this here as the row pointer may be illegal
493 // at a later time (Jug20020502)
494 Row * prev = p.row->previous();
496 inset->update(p.bv, font, false);
497 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
499 if (!need_break_row && !inset_owner
500 && p.bv->text->status() == CHANGED_IN_DRAW) {
501 if (prev && prev->par() == p.row->par()) {
502 breakAgainOneRow(p.bv, prev);
503 if (prev->next() != p.row) {
504 // breakAgainOneRow() has removed p.row
505 p.row = 0; // see what this breaks
506 need_break_row = prev;
508 need_break_row = p.row;
511 need_break_row = firstrow;
513 need_break_row = prev->next();
515 setCursor(p.bv, cursor.par(), cursor.pos());
522 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
524 if (!lyxrc.mark_foreign_language)
526 if (orig_font.language() == latex_language)
528 if (orig_font.language() == p.bv->buffer()->params.language)
531 int const y = p.yo + p.row->height() - 1;
532 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
536 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
538 pos_type pos = vis2log(vpos);
543 char c = p.row->par()->getChar(pos);
547 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
548 int const width = font_metrics::width(c, font);
551 for (pos_type i = pos-1; i >= 0; --i) {
552 c = p.row->par()->getChar(i);
553 if (!Encodings::IsComposeChar_hebrew(c)) {
554 if (IsPrintableNonspace(c)) {
556 singleWidth(p.bv, p.row->par(), i, c);
558 dx = (c == 'ø' || c == 'ã')
560 : (width2 - width) / 2;
567 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
571 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
573 pos_type pos = vis2log(vpos);
577 char c = p.row->par()->getChar(pos);
578 c = transformChar(c, p.row->par(), pos);
582 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
583 int const width = font_metrics::width(c, font);
586 for (pos_type i = pos-1; i >= 0; --i) {
587 c = p.row->par()->getChar(i);
588 if (!Encodings::IsComposeChar_arabic(c)) {
589 if (IsPrintableNonspace(c)) {
591 singleWidth(p.bv, p.row->par(), i, c);
592 dx = (width2 - width) / 2;
598 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
602 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
603 bool hebrew, bool arabic)
605 pos_type pos = vis2log(vpos);
606 pos_type const last = rowLastPrintable(p.row);
607 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
611 str += p.row->par()->getChar(pos);
613 unsigned char c = str[0];
614 str[0] = transformChar(c, p.row->par(), pos);
618 // collect as much similar chars as we can
619 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
620 char c = p.row->par()->getChar(pos);
622 if (!IsPrintableNonspace(c))
625 if (arabic && Encodings::IsComposeChar_arabic(c))
627 if (hebrew && Encodings::IsComposeChar_hebrew(c))
630 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
637 // Draw text and set the new x position
638 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
639 p.x += font_metrics::width(str, orig_font);
643 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
645 pos_type const pos = vis2log(vpos);
646 Paragraph * par = p.row->par();
648 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
650 float const orig_x = p.x;
652 char const c = par->getChar(pos);
654 if (IsNewlineChar(c)) {
658 } else if (IsInsetChar(c)) {
659 if (!drawInset(p, pos))
662 drawForeignMark(p, orig_x, orig_font);
666 // usual characters, no insets
668 // special case languages
669 bool const hebrew = (orig_font.language()->lang() == "hebrew");
671 orig_font.language()->lang() == "arabic" &&
672 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
673 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
675 // draw as many chars as we can
676 if ((!hebrew && !arabic)
677 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
678 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
679 drawChars(p, vpos, true, false);
681 drawHebrewComposeChar(p, vpos);
683 drawArabicComposeChar(p, vpos);
686 drawForeignMark(p, orig_x, orig_font);
688 #ifdef INHERIT_LANGUAGE
690 if ((font.language() == inherit_language) ||
691 (font.language() == ignore_language))
692 lyxerr << "No this shouldn't happen!\n";
699 int LyXText::leftMargin(BufferView * bview, Row const * row) const
702 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
703 (ins=row->par()->getInset(row->pos())) &&
704 (ins->needFullRow() || ins->display()))
705 return LYX_PAPER_MARGIN;
707 LyXTextClass const & tclass =
708 textclasslist[bview->buffer()->params.textclass];
709 LyXLayout_ptr const & layout = row->par()->layout();
711 string parindent = layout->parindent;
713 int x = LYX_PAPER_MARGIN;
715 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
717 // this is the way, LyX handles the LaTeX-Environments.
718 // I have had this idea very late, so it seems to be a
719 // later added hack and this is true
720 if (!row->par()->getDepth()) {
721 if (row->par()->layout() == tclass.defaultLayout()) {
722 // find the previous same level paragraph
723 if (row->par()->previous()) {
724 Paragraph * newpar = row->par()
725 ->depthHook(row->par()->getDepth());
727 newpar->layout()->nextnoindent)
732 // find the next level paragraph
735 row->par()->outerHook();
737 // make a corresponding row. Needed to call LeftMargin()
739 // check wether it is a sufficent paragraph
740 if (newpar && newpar->layout()->isEnvironment()) {
742 dummyrow.par(newpar);
743 dummyrow.pos(newpar->size());
744 x = leftMargin(bview, &dummyrow);
746 // this is no longer an error, because this function
747 // is used to clear impossible depths after changing
748 // a layout. Since there is always a redo,
749 // LeftMargin() is always called
750 row->par()->params().depth(0);
753 if (newpar && row->par()->layout() == tclass.defaultLayout()) {
754 if (newpar->params().noindent())
757 parindent = newpar->layout()->parindent;
763 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
764 switch (layout->margintype) {
766 if (!layout->leftmargin.empty()) {
767 x += font_metrics::signedWidth(layout->leftmargin,
768 tclass.defaultfont());
770 if (!row->par()->getLabelstring().empty()) {
771 x += font_metrics::signedWidth(layout->labelindent,
773 x += font_metrics::width(row->par()->getLabelstring(),
775 x += font_metrics::width(layout->labelsep, labelfont);
779 x += font_metrics::signedWidth(layout->labelindent, labelfont);
780 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
781 if (!row->par()->getLabelWidthString().empty()) {
782 x += font_metrics::width(row->par()->getLabelWidthString(),
784 x += font_metrics::width(layout->labelsep, labelfont);
789 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
790 / (row->par()->getDepth() + 4);
792 case MARGIN_FIRST_DYNAMIC:
793 if (layout->labeltype == LABEL_MANUAL) {
794 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
795 x += font_metrics::signedWidth(layout->leftmargin,
798 x += font_metrics::signedWidth(layout->labelindent,
801 } else if (row->pos()
802 // Special case to fix problems with
804 || (layout->labeltype == LABEL_STATIC
805 && layout->latextype == LATEX_ENVIRONMENT
806 && ! row->par()->isFirstInSequence())) {
807 x += font_metrics::signedWidth(layout->leftmargin,
809 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
810 && layout->labeltype != LABEL_BIBLIO
811 && layout->labeltype !=
812 LABEL_CENTERED_TOP_ENVIRONMENT) {
813 x += font_metrics::signedWidth(layout->labelindent,
815 x += font_metrics::width(layout->labelsep, labelfont);
816 x += font_metrics::width(row->par()->getLabelstring(),
821 case MARGIN_RIGHT_ADDRESS_BOX:
823 // ok, a terrible hack. The left margin depends on the widest
824 // row in this paragraph. Do not care about footnotes, they
825 // are *NOT* allowed in the LaTeX realisation of this layout.
827 // find the first row of this paragraph
828 Row const * tmprow = row;
829 while (tmprow->previous()
830 && tmprow->previous()->par() == row->par())
831 tmprow = tmprow->previous();
833 int minfill = tmprow->fill();
834 while (tmprow->next() && tmprow->next()->par() == row->par()) {
835 tmprow = tmprow->next();
836 if (tmprow->fill() < minfill)
837 minfill = tmprow->fill();
840 x += font_metrics::signedWidth(layout->leftmargin,
841 tclass.defaultfont());
847 if ((workWidth(bview) > 0) &&
848 !row->par()->params().leftIndent().zero())
850 LyXLength const len = row->par()->params().leftIndent();
851 int const tw = inset_owner ?
852 inset_owner->latexTextWidth(bview) : workWidth(bview);
853 x += len.inPixels(tw, bview->text->defaultHeight());
856 LyXAlignment align; // wrong type
858 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
859 align = layout->align;
861 align = row->par()->params().align();
863 // set the correct parindent
864 if (row->pos() == 0) {
865 if ((layout->labeltype == LABEL_NO_LABEL
866 || layout->labeltype == LABEL_TOP_ENVIRONMENT
867 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
868 || (layout->labeltype == LABEL_STATIC
869 && layout->latextype == LATEX_ENVIRONMENT
870 && ! row->par()->isFirstInSequence()))
871 && align == LYX_ALIGN_BLOCK
872 && !row->par()->params().noindent()
873 // in tabulars and ert paragraphs are never indented!
874 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
875 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
876 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
877 && (row->par()->layout() != tclass.defaultLayout() ||
878 bview->buffer()->params.paragraph_separation ==
879 BufferParams::PARSEP_INDENT)) {
880 x += font_metrics::signedWidth(parindent,
881 tclass.defaultfont());
882 } else if (layout->labeltype == LABEL_BIBLIO) {
883 // ale970405 Right width for bibitems
884 x += bibitemMaxWidth(bview, tclass.defaultfont());
892 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
895 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
896 (ins=row->par()->getInset(row->pos())) &&
897 (ins->needFullRow() || ins->display()))
898 return LYX_PAPER_MARGIN;
900 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
901 LyXLayout_ptr const & layout = row->par()->layout();
903 int x = LYX_PAPER_MARGIN
904 + font_metrics::signedWidth(tclass.rightmargin(),
905 tclass.defaultfont());
907 // this is the way, LyX handles the LaTeX-Environments.
908 // I have had this idea very late, so it seems to be a
909 // later added hack and this is true
910 if (row->par()->getDepth()) {
911 // find the next level paragraph
913 Paragraph * newpar = row->par();
916 newpar = newpar->previous();
918 && newpar->getDepth() >= row->par()->getDepth());
920 // make a corresponding row. Needed to call LeftMargin()
922 // check wether it is a sufficent paragraph
923 if (newpar && newpar->layout()->isEnvironment()) {
925 dummyrow.par(newpar);
927 x = rightMargin(buf, &dummyrow);
929 // this is no longer an error, because this function
930 // is used to clear impossible depths after changing
931 // a layout. Since there is always a redo,
932 // LeftMargin() is always called
933 row->par()->params().depth(0);
937 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
938 x += font_metrics::signedWidth(layout->rightmargin,
939 tclass.defaultfont())
940 * 4 / (row->par()->getDepth() + 4);
945 int LyXText::labelEnd(BufferView * bview, Row const * row) const
947 if (row->par()->layout()->margintype == MARGIN_MANUAL) {
950 tmprow.pos(row->par()->size());
951 // just the beginning of the main body
952 return leftMargin(bview, &tmprow);
954 // LabelEnd is only needed,
955 // if the layout fills a flushleft label.
961 // get the next breakpoint in a given paragraph
963 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
965 Paragraph * par = row->par();
970 pos_type const pos = row->pos();
972 // position of the last possible breakpoint
973 // -1 isn't a suitable value, but a flag
974 pos_type last_separator = -1;
975 width -= rightMargin(bview->buffer(), row);
977 pos_type const main_body =
978 beginningOfMainBody(bview->buffer(), par);
979 LyXLayout_ptr const & layout = par->layout();
983 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
984 // special code for right address boxes, only newlines count
985 while (i < par->size()) {
986 if (par->isNewline(i)) {
988 i = par->size() - 1; // this means break
990 } else if (par->isInset(i) && par->getInset(i)
991 && par->getInset(i)->display()) {
992 par->getInset(i)->display(false);
997 // Last position is an invariant
998 pos_type const last = par->size();
999 // this is the usual handling
1000 int x = leftMargin(bview, row);
1001 bool doitonetime = true;
1002 while (doitonetime || ((x < width) && (i < last))) {
1003 doitonetime = false;
1004 char const c = par->getChar(i);
1006 if (c == Paragraph::META_INSET)
1007 in = par->getInset(i);
1008 if (IsNewlineChar(c)) {
1010 x = width; // this means break
1011 } else if (in && !in->isChar()) {
1012 // check wether a Display() inset is
1013 // valid here. if not, change it to
1015 if (in->display() &&
1016 (layout->isCommand() ||
1017 (layout->labeltype == LABEL_MANUAL
1018 && i < beginningOfMainBody(bview->buffer(), par))))
1020 // display istn't allowd
1022 x += singleWidth(bview, par, i, c);
1023 } else if (in->display() || in->needFullRow()) {
1024 // So break the line here
1028 if (par->isLineSeparator(i+1))
1031 last_separator = last; // to avoid extra rows
1033 last_separator = i - 1;
1034 x = width; // this means break
1036 x += singleWidth(bview, par, i, c);
1037 // we have to check this separately as we could have a
1038 // lineseparator and then the algorithm below would prefer
1039 // that which IS wrong! We should always break on an inset
1040 // if it's too long and not on the last separator.
1041 // Maybe the only exeption is insets used as chars but
1042 // then we would have to have a special function inside
1043 // the inset to tell us this. Till then we leave it as
1044 // it is now. (Jug 20020106)
1045 if (pos < i && x >= width && last_separator >= 0)
1046 last_separator = i - 1;
1049 if (par->isLineSeparator(i))
1051 x += singleWidth(bview, par, i, c);
1054 if (i == main_body) {
1055 x += font_metrics::width(layout->labelsep,
1056 getLabelFont(bview->buffer(), par));
1057 if (par->isLineSeparator(i - 1))
1058 x-= singleWidth(bview, par, i - 1);
1059 int left_margin = labelEnd(bview, row);
1060 if (x < left_margin)
1064 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1065 last_separator = i - 2;
1066 else if ((pos < i) && (last_separator < 0) && (x >= width))
1067 last_separator = i - 1;
1068 // end of paragraph is always a suitable separator
1069 else if (i == last && x < width)
1073 // well, if last_separator is still 0, the line isn't breakable.
1074 // don't care and cut simply at the end
1075 if (last_separator < 0) {
1079 // manual labels cannot be broken in LaTeX, do not care
1080 if (main_body && last_separator < main_body)
1081 last_separator = main_body - 1;
1083 return last_separator;
1087 // returns the minimum space a row needs on the screen in pixel
1088 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1090 if (paper_width < 0)
1094 // get the pure distance
1095 pos_type const last = rowLastPrintable(row);
1097 // special handling of the right address boxes
1098 if (row->par()->layout()->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1099 int const tmpfill = row->fill();
1100 row->fill(0); // the minfill in MarginLeft()
1101 w = leftMargin(bview, row);
1104 w = leftMargin(bview, row);
1106 LyXLayout_ptr const & layout = row->par()->layout();
1108 pos_type const main_body =
1109 beginningOfMainBody(bview->buffer(), row->par());
1110 pos_type i = row->pos();
1113 if (main_body > 0 && i == main_body) {
1114 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1115 if (row->par()->isLineSeparator(i - 1))
1116 w -= singleWidth(bview, row->par(), i - 1);
1117 int left_margin = labelEnd(bview, row);
1118 if (w < left_margin)
1121 w += singleWidth(bview, row->par(), i);
1124 if (main_body > 0 && main_body > last) {
1125 w += font_metrics::width(layout->labelsep, getLabelFont(bview->buffer(), row->par()));
1126 if (last >= 0 && row->par()->isLineSeparator(last))
1127 w -= singleWidth(bview, row->par(), last);
1128 int const left_margin = labelEnd(bview, row);
1129 if (w < left_margin)
1133 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1138 // returns the minimum space a manual label needs on the screen in pixel
1139 int LyXText::labelFill(BufferView * bview, Row const * row) const
1141 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1142 // -1 because a label ends either with a space that is in the label,
1143 // or with the beginning of a footnote that is outside the label.
1145 // I don't understand this code in depth, but sometimes "last" is
1146 // less than 0 and this causes a crash. This fix seems to work
1147 // correctly, but I bet the real error is elsewhere. The bug is
1148 // triggered when you have an open footnote in a paragraph
1149 // environment with a manual label. (Asger)
1150 if (last < 0) last = 0;
1152 // a separator at this end does not count
1153 if (row->par()->isLineSeparator(last))
1157 pos_type i = row->pos();
1159 w += singleWidth(bview, row->par(), i);
1164 if (!row->par()->params().labelWidthString().empty()) {
1165 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1166 getLabelFont(bview->buffer(), row->par())) - w,
1174 // returns the number of separators in the specified row. The separator
1175 // on the very last column doesnt count
1176 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1178 pos_type last = rowLastPrintable(row);
1179 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1182 for (; p <= last; ++p) {
1183 if (row->par()->isSeparator(p)) {
1191 // returns the number of hfills in the specified row. The LyX-Hfill is
1192 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1193 // ignored. This is *MUCH* more usefull than not to ignore!
1194 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1196 pos_type const last = rowLast(row);
1197 pos_type first = row->pos();
1200 // hfill *DO* count at the beginning of paragraphs!
1201 while (first <= last && row->par()->isHfill(first)) {
1206 first = max(first, beginningOfMainBody(buf, row->par()));
1208 for (pos_type p = first; p <= last; ++p) {
1209 // last, because the end is ignored!
1211 if (row->par()->isHfill(p)) {
1219 // like NumberOfHfills, but only those in the manual label!
1220 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1222 pos_type last = rowLast(row);
1223 pos_type first = row->pos();
1225 // hfill *DO* count at the beginning of paragraphs!
1226 while (first < last && row->par()->isHfill(first))
1230 last = min(last, beginningOfMainBody(buf, row->par()));
1232 for (pos_type p = first; p < last; ++p) {
1233 // last, because the end is ignored!
1234 if (row->par()->isHfill(p)) {
1242 // returns true, if a expansion is needed.
1243 // Rules are given by LaTeX
1244 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1247 // by the way, is it a hfill?
1248 if (!row_ptr->par()->isHfill(pos))
1251 // at the end of a row it does not count
1252 // unless another hfill exists on the line
1253 if (pos >= rowLast(row_ptr)) {
1254 pos_type i = row_ptr->pos();
1255 while (i < pos && !row_ptr->par()->isHfill(i)) {
1263 // at the beginning of a row it does not count, if it is not
1264 // the first row of a paragaph
1265 if (!row_ptr->pos())
1268 // in some labels it does not count
1269 if (row_ptr->par()->layout()->margintype != MARGIN_MANUAL
1270 && pos < beginningOfMainBody(buf, row_ptr->par()))
1273 // if there is anything between the first char of the row and
1274 // the sepcified position that is not a newline and not a hfill,
1275 // the hfill will count, otherwise not
1276 pos_type i = row_ptr->pos();
1277 while (i < pos && (row_ptr->par()->isNewline(i)
1278 || row_ptr->par()->isHfill(i)))
1285 LColor::color LyXText::backgroundColor()
1288 return inset_owner->backgroundColor();
1290 return LColor::background;
1293 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1295 // get the maximum ascent and the maximum descent
1298 float layoutasc = 0;
1299 float layoutdesc = 0;
1302 Inset * tmpinset = 0;
1304 // ok , let us initialize the maxasc and maxdesc value.
1305 // This depends in LaTeX of the font of the last character
1306 // in the paragraph. The hack below is necessary because
1307 // of the possibility of open footnotes
1309 // Correction: only the fontsize count. The other properties
1310 // are taken from the layoutfont. Nicer on the screen :)
1311 Paragraph * par = row_ptr->par();
1312 Paragraph * firstpar = row_ptr->par();
1314 LyXLayout_ptr const & layout = firstpar->layout();
1316 // as max get the first character of this row then it can increes but not
1317 // decrees the height. Just some point to start with so we don't have to
1318 // do the assignment below too often.
1319 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1320 LyXFont::FONT_SIZE const tmpsize = font.size();
1321 font = getLayoutFont(bview->buffer(), par);
1322 LyXFont::FONT_SIZE const size = font.size();
1323 font.setSize(tmpsize);
1325 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1327 float spacing_val = 1.0;
1328 if (!row_ptr->par()->params().spacing().isDefault()) {
1329 spacing_val = row_ptr->par()->params().spacing().getValue();
1331 spacing_val = bview->buffer()->params.spacing.getValue();
1333 //lyxerr << "spacing_val = " << spacing_val << endl;
1335 int maxasc = int(font_metrics::maxAscent(font) *
1336 layout->spacing.getValue() *
1338 int maxdesc = int(font_metrics::maxDescent(font) *
1339 layout->spacing.getValue() *
1342 pos_type const pos_end = rowLast(row_ptr);
1346 // Check if any insets are larger
1347 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1348 if (row_ptr->par()->isInset(pos)) {
1349 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1350 tmpinset = row_ptr->par()->getInset(pos);
1352 #if 1 // this is needed for deep update on initialitation
1353 tmpinset->update(bview, tmpfont);
1355 asc = tmpinset->ascent(bview, tmpfont);
1356 desc = tmpinset->descent(bview, tmpfont);
1357 maxwidth += tmpinset->width(bview, tmpfont);
1358 maxasc = max(maxasc, asc);
1359 maxdesc = max(maxdesc, desc);
1362 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1366 // Check if any custom fonts are larger (Asger)
1367 // This is not completely correct, but we can live with the small,
1368 // cosmetic error for now.
1369 LyXFont::FONT_SIZE maxsize =
1370 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1371 if (maxsize > font.size()) {
1372 font.setSize(maxsize);
1374 asc = font_metrics::maxAscent(font);
1375 desc = font_metrics::maxDescent(font);
1382 // This is nicer with box insets:
1386 row_ptr->ascent_of_text(maxasc);
1388 // is it a top line?
1389 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1391 // some parksips VERY EASY IMPLEMENTATION
1392 if (bview->buffer()->params.paragraph_separation ==
1393 BufferParams::PARSEP_SKIP)
1395 if (layout->isParagraph()
1396 && firstpar->getDepth() == 0
1397 && firstpar->previous())
1399 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1400 } else if (firstpar->previous() &&
1401 firstpar->previous()->layout()->isParagraph() &&
1402 firstpar->previous()->getDepth() == 0)
1404 // is it right to use defskip here too? (AS)
1405 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1409 // the paper margins
1410 if (!row_ptr->par()->previous() && bv_owner)
1411 maxasc += LYX_PAPER_MARGIN;
1413 // add the vertical spaces, that the user added
1414 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1416 // do not forget the DTP-lines!
1417 // there height depends on the font of the nearest character
1418 if (firstpar->params().lineTop())
1420 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1422 // and now the pagebreaks
1423 if (firstpar->params().pagebreakTop())
1424 maxasc += 3 * defaultHeight();
1426 // This is special code for the chapter, since the label of this
1427 // layout is printed in an extra row
1428 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1429 && bview->buffer()->params.secnumdepth >= 0)
1431 float spacing_val = 1.0;
1432 if (!row_ptr->par()->params().spacing().isDefault()) {
1433 spacing_val = row_ptr->par()->params().spacing().getValue();
1435 spacing_val = bview->buffer()->params.spacing.getValue();
1438 labeladdon = int(font_metrics::maxDescent(labelfont) *
1439 layout->spacing.getValue() *
1441 + int(font_metrics::maxAscent(labelfont) *
1442 layout->spacing.getValue() *
1446 // special code for the top label
1447 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1448 || layout->labeltype == LABEL_BIBLIO
1449 || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1450 && row_ptr->par()->isFirstInSequence()
1451 && !row_ptr->par()->getLabelstring().empty())
1453 float spacing_val = 1.0;
1454 if (!row_ptr->par()->params().spacing().isDefault()) {
1455 spacing_val = row_ptr->par()->params().spacing().getValue();
1457 spacing_val = bview->buffer()->params.spacing.getValue();
1461 (font_metrics::maxAscent(labelfont) *
1462 layout->spacing.getValue() *
1464 +(font_metrics::maxDescent(labelfont) *
1465 layout->spacing.getValue() *
1467 + layout->topsep * defaultHeight()
1468 + layout->labelbottomsep * defaultHeight());
1471 // and now the layout spaces, for example before and after a section,
1472 // or between the items of a itemize or enumerate environment
1474 if (!firstpar->params().pagebreakTop()) {
1475 Paragraph * prev = row_ptr->par()->previous();
1477 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1478 if (prev && prev->layout() == firstpar->layout() &&
1479 prev->getDepth() == firstpar->getDepth() &&
1480 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1482 layoutasc = (layout->itemsep * defaultHeight());
1483 } else if (row_ptr->previous()) {
1484 tmptop = layout->topsep;
1486 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1487 tmptop -= row_ptr->previous()->par()->layout()->bottomsep;
1490 layoutasc = (tmptop * defaultHeight());
1491 } else if (row_ptr->par()->params().lineTop()) {
1492 tmptop = layout->topsep;
1495 layoutasc = (tmptop * defaultHeight());
1498 prev = row_ptr->par()->outerHook();
1500 maxasc += int(prev->layout()->parsep * defaultHeight());
1502 if (firstpar->previous() &&
1503 firstpar->previous()->getDepth() == 0 &&
1504 firstpar->previous()->layout() !=
1508 } else if (firstpar->previous()) {
1509 maxasc += int(layout->parsep * defaultHeight());
1515 // is it a bottom line?
1516 if (row_ptr->par() == par
1517 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1519 // the paper margins
1520 if (!par->next() && bv_owner)
1521 maxdesc += LYX_PAPER_MARGIN;
1523 // add the vertical spaces, that the user added
1524 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1526 // do not forget the DTP-lines!
1527 // there height depends on the font of the nearest character
1528 if (firstpar->params().lineBottom())
1529 maxdesc += 2 * font_metrics::ascent('x',
1530 getFont(bview->buffer(),
1532 max(pos_type(0), par->size() - 1)));
1534 // and now the pagebreaks
1535 if (firstpar->params().pagebreakBottom())
1536 maxdesc += 3 * defaultHeight();
1538 // and now the layout spaces, for example before and after
1539 // a section, or between the items of a itemize or enumerate
1541 if (!firstpar->params().pagebreakBottom()
1542 && row_ptr->par()->next()) {
1543 Paragraph * nextpar = row_ptr->par()->next();
1544 Paragraph * comparepar = row_ptr->par();
1548 if (comparepar->getDepth() > nextpar->getDepth()) {
1549 usual = (comparepar->layout()->bottomsep * defaultHeight());
1550 comparepar = comparepar->depthHook(nextpar->getDepth());
1551 if (comparepar->layout()!= nextpar->layout()
1552 || nextpar->getLabelWidthString() !=
1553 comparepar->getLabelWidthString())
1555 unusual = (comparepar->layout()->bottomsep * defaultHeight());
1557 if (unusual > usual)
1558 layoutdesc = unusual;
1561 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1563 if (comparepar->layout() != nextpar->layout()
1564 || nextpar->getLabelWidthString() !=
1565 comparepar->getLabelWidthString())
1566 layoutdesc = int(comparepar->layout()->bottomsep * defaultHeight());
1571 // incalculate the layout spaces
1572 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1573 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1575 // calculate the new height of the text
1576 height -= row_ptr->height();
1578 row_ptr->height(maxasc + maxdesc + labeladdon);
1579 row_ptr->baseline(maxasc + labeladdon);
1581 height += row_ptr->height();
1583 if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1586 row_ptr->width(maxwidth);
1587 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1589 row_ptr->width(int(maxwidth + x));
1592 width = max(0,workWidth(bview));
1594 if (r->width() > width)
1602 // Appends the implicit specified paragraph behind the specified row,
1603 // start at the implicit given position
1604 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1606 bool not_ready = true;
1608 // The last character position of a paragraph is an invariant so we can
1609 // safely get it here. (Asger)
1610 pos_type const lastposition = row->par()->size();
1612 // Get the next breakpoint
1613 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1617 // Insert the new row
1618 if (z < lastposition) {
1620 insertRow(row, row->par(), z);
1627 // Set the dimensions of the row
1628 // fixed fill setting now by calling inset->update() in
1629 // SingleWidth when needed!
1630 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1631 setHeightOfRow(bview, tmprow);
1633 } while (not_ready);
1637 void LyXText::breakAgain(BufferView * bview, Row * row) const
1639 bool not_ready = true;
1642 // get the next breakpoint
1643 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1646 if (z < row->par()->size()) {
1647 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1650 insertRow(row, row->par(), z);
1656 if (row->pos() == z)
1657 not_ready = false; // the rest will not change
1663 // if there are some rows too much, delete them
1664 // only if you broke the whole paragraph!
1665 Row * tmprow2 = row;
1666 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1667 tmprow2 = tmprow2->next();
1669 while (tmprow2 != row) {
1670 tmprow2 = tmprow2->previous();
1671 removeRow(tmprow2->next());
1676 // set the dimensions of the row
1677 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1678 setHeightOfRow(bview, tmprow);
1679 } while (not_ready);
1683 // this is just a little changed version of break again
1684 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1686 // get the next breakpoint
1687 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1690 if (z < row->par()->size()) {
1692 || (row->next() && row->next()->par() != row->par())) {
1695 insertRow(row, row->par(), z);
1701 if (row->pos() != z)
1705 // if there are some rows too much, delete them
1706 // only if you broke the whole paragraph!
1707 Row * tmprow2 = row;
1708 while (tmprow2->next()
1709 && tmprow2->next()->par() == row->par()) {
1710 tmprow2 = tmprow2->next();
1712 while (tmprow2 != row) {
1713 tmprow2 = tmprow2->previous();
1714 removeRow(tmprow2->next());
1718 // set the dimensions of the row
1719 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1720 setHeightOfRow(bview, tmprow);
1724 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1726 LyXTextClass const & tclass =
1727 textclasslist[bview->buffer()->params.textclass];
1728 LyXLayout_ptr const & layout = cursor.par()->layout();
1730 // this is only allowed, if the current paragraph is not empty or caption
1731 // and if it has not the keepempty flag aktive
1732 if ((cursor.par()->size() <= 0)
1733 && layout->labeltype != LABEL_SENSITIVE
1734 && !layout->keepempty)
1737 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1739 // Always break behind a space
1741 // It is better to erase the space (Dekel)
1742 if (cursor.pos() < cursor.par()->size()
1743 && cursor.par()->isLineSeparator(cursor.pos()))
1744 cursor.par()->erase(cursor.pos());
1745 // cursor.pos(cursor.pos() + 1);
1747 // break the paragraph
1751 keep_layout = layout->isEnvironment();
1753 // we need to set this before we insert the paragraph. IMO the
1754 // breakParagraph call should return a bool if it inserts the
1755 // paragraph before or behind and we should react on that one
1756 // but we can fix this in 1.3.0 (Jug 20020509)
1757 bool const isempty = (layout->keepempty && !cursor.par()->size());
1758 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1761 // well this is the caption hack since one caption is really enough
1762 if (layout->labeltype == LABEL_SENSITIVE) {
1764 // set to standard-layout
1765 cursor.par()->applyLayout(tclass.defaultLayout());
1767 // set to standard-layout
1768 cursor.par()->next()->applyLayout(tclass.defaultLayout());
1771 // if the cursor is at the beginning of a row without prior newline,
1773 // This touches only the screen-update. Otherwise we would may have
1774 // an empty row on the screen
1775 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1776 && cursor.row()->pos() == cursor.pos())
1781 status(bview, LyXText::NEED_MORE_REFRESH);
1782 refresh_row = cursor.row();
1783 refresh_y = cursor.y() - cursor.row()->baseline();
1785 // Do not forget the special right address boxes
1786 if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1787 while (refresh_row->previous() &&
1788 refresh_row->previous()->par() == refresh_row->par())
1790 refresh_row = refresh_row->previous();
1791 refresh_y -= refresh_row->height();
1794 removeParagraph(cursor.row());
1796 // set the dimensions of the cursor row
1797 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1799 setHeightOfRow(bview, cursor.row());
1801 while (cursor.par()->next()->size()
1802 && cursor.par()->next()->isNewline(0))
1803 cursor.par()->next()->erase(0);
1805 insertParagraph(bview, cursor.par()->next(), cursor.row());
1807 updateCounters(bview, cursor.row()->previous());
1809 // This check is necessary. Otherwise the new empty paragraph will
1810 // be deleted automatically. And it is more friendly for the user!
1811 if (cursor.pos() || isempty)
1812 setCursor(bview, cursor.par()->next(), 0);
1814 setCursor(bview, cursor.par(), 0);
1816 if (cursor.row()->next())
1817 breakAgain(bview, cursor.row()->next());
1823 // Just a macro to make some thing easier.
1824 void LyXText::redoParagraph(BufferView * bview) const
1827 redoParagraphs(bview, cursor, cursor.par()->next());
1828 setCursorIntern(bview, cursor.par(), cursor.pos());
1832 // insert a character, moves all the following breaks in the
1833 // same Paragraph one to the right and make a rebreak
1834 void LyXText::insertChar(BufferView * bview, char c)
1836 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1838 // When the free-spacing option is set for the current layout,
1839 // disable the double-space checking
1841 bool const freeSpacing = cursor.row()->par()->layout()->free_spacing ||
1842 cursor.row()->par()->isFreeSpacing();
1844 if (lyxrc.auto_number) {
1845 static string const number_operators = "+-/*";
1846 static string const number_unary_operators = "+-";
1847 static string const number_seperators = ".,:";
1849 if (current_font.number() == LyXFont::ON) {
1850 if (!IsDigit(c) && !contains(number_operators, c) &&
1851 !(contains(number_seperators, c) &&
1852 cursor.pos() >= 1 &&
1853 cursor.pos() < cursor.par()->size() &&
1854 getFont(bview->buffer(),
1856 cursor.pos()).number() == LyXFont::ON &&
1857 getFont(bview->buffer(),
1859 cursor.pos() - 1).number() == LyXFont::ON)
1861 number(bview); // Set current_font.number to OFF
1862 } else if (IsDigit(c) &&
1863 real_current_font.isVisibleRightToLeft()) {
1864 number(bview); // Set current_font.number to ON
1866 if (cursor.pos() > 0) {
1867 char const c = cursor.par()->getChar(cursor.pos() - 1);
1868 if (contains(number_unary_operators, c) &&
1869 (cursor.pos() == 1 ||
1870 cursor.par()->isSeparator(cursor.pos() - 2) ||
1871 cursor.par()->isNewline(cursor.pos() - 2))
1873 setCharFont(bview->buffer(),
1877 } else if (contains(number_seperators, c) &&
1878 cursor.pos() >= 2 &&
1879 getFont(bview->buffer(),
1881 cursor.pos() - 2).number() == LyXFont::ON) {
1882 setCharFont(bview->buffer(),
1892 // First check, if there will be two blanks together or a blank at
1893 // the beginning of a paragraph.
1894 // I decided to handle blanks like normal characters, the main
1895 // difference are the special checks when calculating the row.fill
1896 // (blank does not count at the end of a row) and the check here
1898 // The bug is triggered when we type in a description environment:
1899 // The current_font is not changed when we go from label to main text
1900 // and it should (along with realtmpfont) when we type the space.
1901 // CHECK There is a bug here! (Asger)
1903 LyXFont realtmpfont = real_current_font;
1904 LyXFont rawtmpfont = current_font;
1905 // store the current font. This is because of the use of cursor
1906 // movements. The moving cursor would refresh the current font
1908 // Get the font that is used to calculate the baselineskip
1909 pos_type const lastpos = cursor.par()->size();
1910 LyXFont rawparfont =
1911 cursor.par()->getFontSettings(bview->buffer()->params,
1914 bool jumped_over_space = false;
1916 if (!freeSpacing && IsLineSeparatorChar(c)) {
1917 if ((cursor.pos() > 0
1918 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1919 || (cursor.pos() > 0
1920 && cursor.par()->isNewline(cursor.pos() - 1))
1921 || (cursor.pos() == 0)) {
1922 static bool sent_space_message = false;
1923 if (!sent_space_message) {
1924 if (cursor.pos() == 0)
1925 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1927 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1928 sent_space_message = true;
1933 } else if (IsNewlineChar(c)) {
1934 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1940 // No newline at first position of a paragraph or behind labels.
1941 // TeX does not allow that
1943 if (cursor.pos() < cursor.par()->size() &&
1944 cursor.par()->isLineSeparator(cursor.pos()))
1945 // newline always after a blank!
1947 cursor.row()->fill(-1); // to force a new break
1950 // the display inset stuff
1951 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1952 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1953 if (inset && (inset->display() || inset->needFullRow())) {
1954 // force a new break
1955 cursor.row()->fill(-1); // to force a new break
1959 // get the cursor row fist
1960 Row * row = cursor.row();
1961 int y = cursor.y() - row->baseline();
1962 if (c != Paragraph::META_INSET) {
1963 // Here case LyXText::InsertInset already insertet the character
1964 cursor.par()->insertChar(cursor.pos(), c);
1966 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1968 if (!jumped_over_space) {
1969 // refresh the positions
1971 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1972 tmprow = tmprow->next();
1973 tmprow->pos(tmprow->pos() + 1);
1977 // Is there a break one row above
1978 if (row->previous() && row->previous()->par() == row->par()
1979 && (cursor.par()->isLineSeparator(cursor.pos())
1980 || cursor.par()->isNewline(cursor.pos())
1981 || ((cursor.pos() < cursor.par()->size()) &&
1982 cursor.par()->isInset(cursor.pos()+1))
1983 || cursor.row()->fill() == -1))
1985 pos_type z = nextBreakPoint(bview,
1988 if (z >= row->pos()) {
1991 // set the dimensions of the row above
1992 row->previous()->fill(fill(bview,
1996 setHeightOfRow(bview, row->previous());
1998 y -= row->previous()->height();
2000 refresh_row = row->previous();
2001 status(bview, LyXText::NEED_MORE_REFRESH);
2003 breakAgainOneRow(bview, row);
2005 current_font = rawtmpfont;
2006 real_current_font = realtmpfont;
2007 setCursor(bview, cursor.par(), cursor.pos() + 1,
2008 false, cursor.boundary());
2009 // cursor MUST be in row now.
2011 if (row->next() && row->next()->par() == row->par())
2012 need_break_row = row->next();
2016 // check, wether the last characters font has changed.
2017 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2018 && rawparfont != rawtmpfont)
2019 redoHeightOfParagraph(bview, cursor);
2026 // recalculate the fill of the row
2027 if (row->fill() >= 0) {
2028 // needed because a newline will set fill to -1. Otherwise
2029 // we would not get a rebreak!
2030 row->fill(fill(bview, row, workWidth(bview)));
2032 if (c == Paragraph::META_INSET || row->fill() < 0) {
2035 refresh_x = cursor.x();
2036 refresh_pos = cursor.pos();
2037 status(bview, LyXText::NEED_MORE_REFRESH);
2038 breakAgainOneRow(bview, row);
2039 // will the cursor be in another row now?
2040 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2041 if (row->next() && row->next()->par() == row->par())
2042 // this should always be true
2044 breakAgainOneRow(bview, row);
2046 current_font = rawtmpfont;
2047 real_current_font = realtmpfont;
2049 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2051 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2052 != cursor.boundary())
2053 setCursor(bview, cursor.par(), cursor.pos(), false,
2054 !cursor.boundary());
2055 if (row->next() && row->next()->par() == row->par())
2056 need_break_row = row->next();
2061 refresh_x = cursor.x();
2063 refresh_pos = cursor.pos();
2065 int const tmpheight = row->height();
2066 setHeightOfRow(bview, row);
2067 if (tmpheight == row->height())
2068 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2070 status(bview, LyXText::NEED_MORE_REFRESH);
2072 current_font = rawtmpfont;
2073 real_current_font = realtmpfont;
2074 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2078 // check, wether the last characters font has changed.
2079 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2080 && rawparfont != rawtmpfont) {
2081 redoHeightOfParagraph(bview, cursor);
2083 // now the special right address boxes
2084 if (cursor.par()->layout()->margintype
2085 == MARGIN_RIGHT_ADDRESS_BOX) {
2086 redoDrawingOfParagraph(bview, cursor);
2094 void LyXText::charInserted()
2096 // Here we could call FinishUndo for every 20 characters inserted.
2097 // This is from my experience how emacs does it.
2098 static unsigned int counter;
2108 void LyXText::prepareToPrint(BufferView * bview,
2109 Row * row, float & x,
2110 float & fill_separator,
2112 float & fill_label_hfill,
2118 float w = row->fill();
2120 fill_label_hfill = 0;
2122 fill_label_hfill = 0;
2125 row->par()->isRightToLeftPar(bview->buffer()->params);
2127 x = (workWidth(bview) > 0)
2128 ? rightMargin(bview->buffer(), row) : 0;
2130 x = (workWidth(bview) > 0)
2131 ? leftMargin(bview, row) : 0;
2133 // is there a manual margin with a manual label
2134 LyXLayout_ptr const & layout = row->par()->layout();
2136 if (layout->margintype == MARGIN_MANUAL
2137 && layout->labeltype == LABEL_MANUAL) {
2138 // one more since labels are left aligned
2139 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2140 if (nlh && !row->par()->getLabelWidthString().empty()) {
2141 fill_label_hfill = labelFill(bview, row) / nlh;
2145 // are there any hfills in the row?
2146 float const nh = numberOfHfills(bview->buffer(), row);
2150 fill_hfill = w / nh;
2151 // we don't have to look at the alignment if it is ALIGN_LEFT and
2152 // if the row is already larger then the permitted width as then
2153 // we force the LEFT_ALIGN'edness!
2154 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2155 // is it block, flushleft or flushright?
2156 // set x how you need it
2158 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2159 align = layout->align;
2161 align = row->par()->params().align();
2164 // center displayed insets
2166 if (row->par()->isInset(row->pos())
2167 && (inset=row->par()->getInset(row->pos()))
2168 && (inset->display())) // || (inset->scroll() < 0)))
2169 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2170 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2171 // ERT insets should always be LEFT ALIGNED on screen
2172 inset = row->par()->inInset();
2173 if (inset && inset->owner() &&
2174 inset->owner()->lyxCode() == Inset::ERT_CODE)
2176 align = LYX_ALIGN_LEFT;
2180 case LYX_ALIGN_BLOCK:
2181 ns = numberOfSeparators(bview->buffer(), row);
2182 if (ns && row->next() && row->next()->par() == row->par() &&
2183 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2184 && !(row->next()->par()->isInset(row->next()->pos())
2185 && row->next()->par()->getInset(row->next()->pos())
2186 && row->next()->par()->getInset(row->next()->pos())->display())
2189 fill_separator = w / ns;
2190 } else if (is_rtl) {
2194 case LYX_ALIGN_RIGHT:
2197 case LYX_ALIGN_CENTER:
2205 computeBidiTables(bview->buffer(), row);
2207 pos_type main_body =
2208 beginningOfMainBody(bview->buffer(), row->par());
2209 pos_type last = rowLast(row);
2211 if (main_body > 0 &&
2212 (main_body - 1 > last ||
2213 !row->par()->isLineSeparator(main_body - 1))) {
2214 x += font_metrics::width(layout->labelsep,
2215 getLabelFont(bview->buffer(), row->par()));
2216 if (main_body - 1 <= last)
2217 x += fill_label_hfill;
2223 // important for the screen
2226 // the cursor set functions have a special mechanism. When they
2227 // realize, that you left an empty paragraph, they will delete it.
2228 // They also delete the corresponding row
2230 void LyXText::cursorRightOneWord(BufferView * bview) const
2232 // treat floats, HFills and Insets as words
2233 LyXCursor tmpcursor = cursor;
2234 // CHECK See comment on top of text.C
2236 if (tmpcursor.pos() == tmpcursor.par()->size()
2237 && tmpcursor.par()->next()) {
2238 tmpcursor.par(tmpcursor.par()->next());
2243 // Skip through initial nonword stuff.
2244 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2245 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2246 // printf("Current pos1 %d", tmpcursor.pos()) ;
2247 tmpcursor.pos(tmpcursor.pos() + 1);
2250 // Advance through word.
2251 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2252 tmpcursor.par()->isWord(tmpcursor.pos())) {
2253 // printf("Current pos2 %d", tmpcursor.pos()) ;
2254 tmpcursor.pos(tmpcursor.pos() + 1);
2258 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2262 void LyXText::cursorTab(BufferView * bview) const
2264 LyXCursor tmpcursor = cursor;
2265 while (tmpcursor.pos() < tmpcursor.par()->size()
2266 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2267 tmpcursor.pos(tmpcursor.pos() + 1);
2269 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2270 if (tmpcursor.par()->next()) {
2271 tmpcursor.par(tmpcursor.par()->next());
2275 tmpcursor.pos(tmpcursor.pos() + 1);
2276 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2280 // Skip initial whitespace at end of word and move cursor to *start*
2281 // of prior word, not to end of next prior word.
2282 void LyXText::cursorLeftOneWord(BufferView * bview) const
2284 LyXCursor tmpcursor = cursor;
2285 cursorLeftOneWord(tmpcursor);
2286 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2290 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2292 // treat HFills, floats and Insets as words
2295 && (cur.par()->isSeparator(cur.pos() - 1)
2296 || cur.par()->isKomma(cur.pos() - 1))
2297 && !(cur.par()->isHfill(cur.pos() - 1)
2298 || cur.par()->isInset(cur.pos() - 1)))
2299 cur.pos(cur.pos() - 1);
2302 && (cur.par()->isInset(cur.pos() - 1)
2303 || cur.par()->isHfill(cur.pos() - 1))) {
2304 cur.pos(cur.pos() - 1);
2305 } else if (!cur.pos()) {
2306 if (cur.par()->previous()) {
2307 cur.par(cur.par()->previous());
2308 cur.pos(cur.par()->size());
2310 } else { // Here, cur != 0
2311 while (cur.pos() > 0 &&
2312 cur.par()->isWord(cur.pos()-1))
2313 cur.pos(cur.pos() - 1);
2318 // Select current word. This depends on behaviour of
2319 // CursorLeftOneWord(), so it is patched as well.
2320 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2321 word_location const loc) const
2323 // first put the cursor where we wana start to select the word
2326 case WHOLE_WORD_STRICT:
2327 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2328 || cursor.par()->isSeparator(cursor.pos())
2329 || cursor.par()->isKomma(cursor.pos())
2330 || cursor.par()->isSeparator(cursor.pos() -1)
2331 || cursor.par()->isKomma(cursor.pos() -1)) {
2335 // no break here, we go to the next
2338 // Move cursor to the beginning, when not already there.
2339 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2340 && !from.par()->isKomma(from.pos() - 1))
2341 cursorLeftOneWord(from);
2344 // always move the cursor to the beginning of previous word
2345 cursorLeftOneWord(from);
2348 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2354 while (to.pos() < to.par()->size()
2355 && !to.par()->isSeparator(to.pos())
2356 && !to.par()->isKomma(to.pos())
2357 && !to.par()->isHfill(to.pos())
2358 && !to.par()->isInset(to.pos()))
2360 to.pos(to.pos() + 1);
2365 void LyXText::selectWord(BufferView * bview, word_location const loc)
2369 getWord(from, to, loc);
2371 setCursor(bview, from.par(), from.pos());
2374 selection.cursor = cursor;
2375 setCursor(bview, to.par(), to.pos());
2376 setSelection(bview);
2380 // Select the word currently under the cursor when no
2381 // selection is currently set
2382 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2383 word_location const loc)
2385 if (!selection.set()) {
2386 selectWord(bview, loc);
2387 return selection.set();
2393 // This function is only used by the spellchecker for NextWord().
2394 // It doesn't handle LYX_ACCENTs and probably never will.
2395 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2396 float & value) const
2398 if (the_locking_inset) {
2399 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2401 value += float(cursor.y())/float(height);
2404 // we have to go on checking so move cusor to the next char
2405 if (cursor.pos() == cursor.par()->size()) {
2406 if (!cursor.par()->next())
2408 cursor.par(cursor.par()->next());
2411 cursor.pos(cursor.pos() + 1);
2413 Paragraph * tmppar = cursor.par();
2415 // If this is not the very first word, skip rest of
2416 // current word because we are probably in the middle
2417 // of a word if there is text here.
2418 if (cursor.pos() || cursor.par()->previous()) {
2419 while (cursor.pos() < cursor.par()->size()
2420 && cursor.par()->isLetter(cursor.pos()))
2421 cursor.pos(cursor.pos() + 1);
2424 // Now, skip until we have real text (will jump paragraphs)
2425 while ((cursor.par()->size() > cursor.pos()
2426 && (!cursor.par()->isLetter(cursor.pos()))
2427 && (!cursor.par()->isInset(cursor.pos()) ||
2428 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2429 || (cursor.par()->size() == cursor.pos()
2430 && cursor.par()->next()))
2432 if (cursor.pos() == cursor.par()->size()) {
2433 cursor.par(cursor.par()->next());
2436 cursor.pos(cursor.pos() + 1);
2439 // now check if we hit an inset so it has to be a inset containing text!
2440 if (cursor.pos() < cursor.par()->size() &&
2441 cursor.par()->isInset(cursor.pos()))
2444 cursor.par()->getInset(cursor.pos())->edit(bview);
2445 // now call us again to do the above trick
2446 // but obviously we have to start from down below ;)
2447 return bview->text->selectNextWordToSpellcheck(bview, value);
2450 // Update the value if we changed paragraphs
2451 if (cursor.par() != tmppar) {
2452 setCursor(bview, cursor.par(), cursor.pos());
2453 value = float(cursor.y())/float(height);
2456 // Start the selection from here
2457 selection.cursor = cursor;
2459 // and find the end of the word (insets like optional hyphens
2460 // and ligature break are part of a word)
2461 while (cursor.pos() < cursor.par()->size()
2462 && (cursor.par()->isLetter(cursor.pos())))
2463 cursor.pos(cursor.pos() + 1);
2465 // Finally, we copy the word to a string and return it
2467 if (selection.cursor.pos() < cursor.pos()) {
2469 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2470 if (!cursor.par()->isInset(i))
2471 str += cursor.par()->getChar(i);
2478 // This one is also only for the spellchecker
2479 void LyXText::selectSelectedWord(BufferView * bview)
2481 if (the_locking_inset) {
2482 the_locking_inset->selectSelectedWord(bview);
2485 // move cursor to the beginning
2486 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2488 // set the sel cursor
2489 selection.cursor = cursor;
2491 // now find the end of the word
2492 while (cursor.pos() < cursor.par()->size()
2493 && (cursor.par()->isLetter(cursor.pos())))
2494 cursor.pos(cursor.pos() + 1);
2496 setCursor(bview, cursor.par(), cursor.pos());
2498 // finally set the selection
2499 setSelection(bview);
2503 // Delete from cursor up to the end of the current or next word.
2504 void LyXText::deleteWordForward(BufferView * bview)
2506 if (!cursor.par()->size())
2509 LyXCursor tmpcursor = cursor;
2510 tmpcursor.row(0); //Â ??
2511 selection.set(true); // to avoid deletion
2512 cursorRightOneWord(bview);
2513 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2514 selection.cursor = cursor;
2516 setSelection(bview);
2518 // Great, CutSelection() gets rid of multiple spaces.
2519 cutSelection(bview, true, false);
2524 // Delete from cursor to start of current or prior word.
2525 void LyXText::deleteWordBackward(BufferView * bview)
2527 if (!cursor.par()->size())
2530 LyXCursor tmpcursor = cursor;
2531 tmpcursor.row(0); // ??
2532 selection.set(true); // to avoid deletion
2533 cursorLeftOneWord(bview);
2534 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2535 selection.cursor = cursor;
2537 setSelection(bview);
2538 cutSelection(bview, true, false);
2543 // Kill to end of line.
2544 void LyXText::deleteLineForward(BufferView * bview)
2546 if (!cursor.par()->size())
2547 // Paragraph is empty, so we just go to the right
2550 LyXCursor tmpcursor = cursor;
2551 // We can't store the row over a regular setCursor
2552 // so we set it to 0 and reset it afterwards.
2553 tmpcursor.row(0); //Â ??
2554 selection.set(true); // to avoid deletion
2556 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2557 selection.cursor = cursor;
2559 setSelection(bview);
2560 // What is this test for ??? (JMarc)
2561 if (!selection.set()) {
2562 deleteWordForward(bview);
2564 cutSelection(bview, true, false);
2570 // Change the case of a word at cursor position.
2571 // This function directly manipulates Paragraph::text because there
2572 // is no Paragraph::SetChar currently. I did what I could to ensure
2573 // that it is correct. I guess part of it should be moved to
2574 // Paragraph, but it will have to change for 1.1 anyway. At least
2575 // it does not access outside of the allocated array as the older
2576 // version did. (JMarc)
2577 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2582 if (selection.set()) {
2583 from = selection.start;
2586 getWord(from, to, PARTIAL_WORD);
2587 setCursor(bview, to.par(), to.pos() + 1);
2590 changeRegionCase(bview, from, to, action);
2594 void LyXText::changeRegionCase(BufferView * bview,
2595 LyXCursor const & from,
2596 LyXCursor const & to,
2597 LyXText::TextCase action)
2599 lyx::Assert(from <= to);
2601 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2603 pos_type pos = from.pos();
2604 Paragraph * par = from.par();
2606 while (par && (pos != to.pos() || par != to.par())) {
2607 if (pos == par->size()) {
2612 unsigned char c = par->getChar(pos);
2613 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2615 case text_lowercase:
2618 case text_capitalization:
2620 action = text_lowercase;
2622 case text_uppercase:
2627 par->setChar(pos, c);
2628 checkParagraph(bview, par, pos);
2632 if (to.row() != from.row()) {
2633 refresh_y = from.y() - from.row()->baseline();
2634 refresh_row = from.row();
2635 status(bview, LyXText::NEED_MORE_REFRESH);
2640 void LyXText::transposeChars(BufferView & bview)
2642 Paragraph * tmppar = cursor.par();
2644 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2646 pos_type tmppos = cursor.pos();
2648 // First decide if it is possible to transpose at all
2650 // We are at the beginning of a paragraph.
2651 if (tmppos == 0) return;
2653 // We are at the end of a paragraph.
2654 if (tmppos == tmppar->size() - 1) return;
2656 unsigned char c1 = tmppar->getChar(tmppos);
2657 unsigned char c2 = tmppar->getChar(tmppos - 1);
2659 if (c1 != Paragraph::META_INSET
2660 && c2 != Paragraph::META_INSET) {
2661 tmppar->setChar(tmppos, c2);
2662 tmppar->setChar(tmppos - 1, c1);
2664 // We should have an implementation that handles insets
2665 // as well, but that will have to come later. (Lgb)
2666 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2670 void LyXText::Delete(BufferView * bview)
2672 // this is a very easy implementation
2674 LyXCursor old_cursor = cursor;
2675 int const old_cur_par_id = old_cursor.par()->id();
2676 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2677 old_cursor.par()->previous()->id() : 0;
2679 // just move to the right
2682 // CHECK Look at the comment here.
2683 // This check is not very good...
2684 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2685 // and that can very well delete the par or par->previous in
2686 // old_cursor. Will a solution where we compare paragraph id's
2688 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2689 == old_cur_par_prev_id
2690 && cursor.par()->id() != old_cur_par_id) {
2691 // delete-empty-paragraph-mechanism has done it
2695 // if you had success make a backspace
2696 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2697 LyXCursor tmpcursor = cursor;
2698 // to make sure undo gets the right cursor position
2699 cursor = old_cursor;
2700 setUndo(bview, Undo::DELETE,
2701 cursor.par(), cursor.par()->next());
2708 void LyXText::backspace(BufferView * bview)
2710 // Get the font that is used to calculate the baselineskip
2711 pos_type lastpos = cursor.par()->size();
2712 LyXFont rawparfont =
2713 cursor.par()->getFontSettings(bview->buffer()->params,
2716 if (cursor.pos() == 0) {
2717 // The cursor is at the beginning of a paragraph,
2718 // so the the backspace will collapse two paragraphs into one.
2720 // we may paste some paragraphs
2722 // is it an empty paragraph?
2725 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2726 // This is an empty paragraph and we delete it just by moving the cursor one step
2727 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2728 // of the paragraph.
2730 if (cursor.par()->previous()) {
2731 Paragraph * tmppar = cursor.par()->previous();
2732 if (cursor.par()->layout() == tmppar->layout()
2733 && cursor.par()->getAlign() == tmppar->getAlign()) {
2734 // Inherit bottom DTD from the paragraph below.
2735 // (the one we are deleting)
2736 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2737 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2738 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2743 // the layout things can change the height of a row !
2744 int const tmpheight = cursor.row()->height();
2745 setHeightOfRow(bview, cursor.row());
2746 if (cursor.row()->height() != tmpheight) {
2747 refresh_y = cursor.y() - cursor.row()->baseline();
2748 refresh_row = cursor.row();
2749 status(bview, LyXText::NEED_MORE_REFRESH);
2755 if (cursor.par()->previous()) {
2756 setUndo(bview, Undo::DELETE,
2757 cursor.par()->previous(), cursor.par()->next());
2760 Paragraph * tmppar = cursor.par();
2761 Row * tmprow = cursor.row();
2763 // We used to do cursorLeftIntern() here, but it is
2764 // not a good idea since it triggers the auto-delete
2765 // mechanism. So we do a cursorLeftIntern()-lite,
2766 // without the dreaded mechanism. (JMarc)
2767 if (cursor.par()->previous()) {
2768 // steps into the above paragraph.
2769 setCursorIntern(bview, cursor.par()->previous(),
2770 cursor.par()->previous()->size(),
2774 // Pasting is not allowed, if the paragraphs have different
2775 // layout. I think it is a real bug of all other
2776 // word processors to allow it. It confuses the user.
2777 // Even so with a footnote paragraph and a non-footnote
2778 // paragraph. I will not allow pasting in this case,
2779 // because the user would be confused if the footnote behaves
2780 // different wether it is open or closed.
2782 // Correction: Pasting is always allowed with standard-layout
2783 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2785 if (cursor.par() != tmppar
2786 && (cursor.par()->layout() == tmppar->layout()
2787 || tmppar->layout() == tclass.defaultLayout())
2788 && cursor.par()->getAlign() == tmppar->getAlign()) {
2789 removeParagraph(tmprow);
2791 cursor.par()->pasteParagraph(bview->buffer()->params);
2793 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2794 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2795 // strangely enough it seems that commenting out the line above removes
2796 // most or all of the segfaults. I will however also try to move the
2797 // two Remove... lines in front of the PasteParagraph too.
2800 cursor.pos(cursor.pos() - 1);
2802 status(bview, LyXText::NEED_MORE_REFRESH);
2803 refresh_row = cursor.row();
2804 refresh_y = cursor.y() - cursor.row()->baseline();
2806 // remove the lost paragraph
2807 // This one is not safe, since the paragraph that the tmprow and the
2808 // following rows belong to has been deleted by the PasteParagraph
2809 // above. The question is... could this be moved in front of the
2811 //RemoveParagraph(tmprow);
2812 //RemoveRow(tmprow);
2814 // This rebuilds the rows.
2815 appendParagraph(bview, cursor.row());
2816 updateCounters(bview, cursor.row());
2818 // the row may have changed, block, hfills etc.
2819 setCursor(bview, cursor.par(), cursor.pos(), false);
2822 // this is the code for a normal backspace, not pasting
2824 setUndo(bview, Undo::DELETE,
2825 cursor.par(), cursor.par()->next());
2826 // We used to do cursorLeftIntern() here, but it is
2827 // not a good idea since it triggers the auto-delete
2828 // mechanism. So we do a cursorLeftIntern()-lite,
2829 // without the dreaded mechanism. (JMarc)
2830 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2831 false, cursor.boundary());
2833 // some insets are undeletable here
2834 if (cursor.par()->isInset(cursor.pos())) {
2835 if (!cursor.par()->getInset(cursor.pos())->deletable())
2837 // force complete redo when erasing display insets
2838 // this is a cruel method but safe..... Matthias
2839 if (cursor.par()->getInset(cursor.pos())->display() ||
2840 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2841 cursor.par()->erase(cursor.pos());
2842 redoParagraph(bview);
2847 Row * row = cursor.row();
2848 int y = cursor.y() - row->baseline();
2850 // remember that a space at the end of a row doesnt count
2851 // when calculating the fill
2852 if (cursor.pos() < rowLast(row) ||
2853 !cursor.par()->isLineSeparator(cursor.pos())) {
2854 row->fill(row->fill() + singleWidth(bview,
2859 // some special code when deleting a newline. This is similar
2860 // to the behavior when pasting paragraphs
2861 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2862 cursor.par()->erase(cursor.pos());
2863 // refresh the positions
2865 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2866 tmprow = tmprow->next();
2867 tmprow->pos(tmprow->pos() - 1);
2869 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2870 cursor.pos(cursor.pos() - 1);
2872 if (cursor.pos() < cursor.par()->size()
2873 && !cursor.par()->isSeparator(cursor.pos())) {
2874 cursor.par()->insertChar(cursor.pos(), ' ');
2875 setCharFont(bview->buffer(), cursor.par(),
2876 cursor.pos(), current_font);
2877 // refresh the positions
2879 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2880 tmprow = tmprow->next();
2881 tmprow->pos(tmprow->pos() + 1);
2885 cursor.par()->erase(cursor.pos());
2887 // refresh the positions
2889 while (tmprow->next()
2890 && tmprow->next()->par() == row->par()) {
2891 tmprow = tmprow->next();
2892 tmprow->pos(tmprow->pos() - 1);
2895 // delete newlines at the beginning of paragraphs
2896 while (cursor.par()->size() &&
2897 cursor.par()->isNewline(cursor.pos()) &&
2898 cursor.pos() == beginningOfMainBody(bview->buffer(),
2900 cursor.par()->erase(cursor.pos());
2901 // refresh the positions
2903 while (tmprow->next() &&
2904 tmprow->next()->par() == row->par()) {
2905 tmprow = tmprow->next();
2906 tmprow->pos(tmprow->pos() - 1);
2911 // is there a break one row above
2912 if (row->previous() && row->previous()->par() == row->par()) {
2913 z = nextBreakPoint(bview, row->previous(),
2915 if (z >= row->pos()) {
2918 Row * tmprow = row->previous();
2920 // maybe the current row is now empty
2921 if (row->pos() >= row->par()->size()) {
2926 breakAgainOneRow(bview, row);
2927 if (row->next() && row->next()->par() == row->par())
2928 need_break_row = row->next();
2933 // set the dimensions of the row above
2934 y -= tmprow->height();
2935 tmprow->fill(fill(bview, tmprow,
2937 setHeightOfRow(bview, tmprow);
2940 refresh_row = tmprow;
2941 status(bview, LyXText::NEED_MORE_REFRESH);
2942 setCursor(bview, cursor.par(), cursor.pos(),
2943 false, cursor.boundary());
2944 //current_font = rawtmpfont;
2945 //real_current_font = realtmpfont;
2946 // check, whether the last character's font has changed.
2948 cursor.par()->getFontSettings(bview->buffer()->params,
2949 cursor.par()->size() - 1))
2950 redoHeightOfParagraph(bview, cursor);
2955 // break the cursor row again
2956 if (row->next() && row->next()->par() == row->par() &&
2957 (rowLast(row) == row->par()->size() - 1 ||
2958 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2960 // it can happen that a paragraph loses one row
2961 // without a real breakup. This is when a word
2962 // is to long to be broken. Well, I don t care this
2964 if (rowLast(row) == row->par()->size() - 1)
2965 removeRow(row->next());
2969 status(bview, LyXText::NEED_MORE_REFRESH);
2971 breakAgainOneRow(bview, row);
2972 // will the cursor be in another row now?
2973 if (row->next() && row->next()->par() == row->par() &&
2974 rowLast(row) <= cursor.pos()) {
2976 breakAgainOneRow(bview, row);
2979 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2981 if (row->next() && row->next()->par() == row->par())
2982 need_break_row = row->next();
2986 // set the dimensions of the row
2987 row->fill(fill(bview, row, workWidth(bview)));
2988 int const tmpheight = row->height();
2989 setHeightOfRow(bview, row);
2990 if (tmpheight == row->height())
2991 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2993 status(bview, LyXText::NEED_MORE_REFRESH);
2996 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3000 // current_font = rawtmpfont;
3001 // real_current_font = realtmpfont;
3003 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
3004 != cursor.boundary())
3005 setCursor(bview, cursor.par(), cursor.pos(), false,
3006 !cursor.boundary());
3008 lastpos = cursor.par()->size();
3009 if (cursor.pos() == lastpos)
3010 setCurrentFont(bview);
3012 // check, whether the last characters font has changed.
3014 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3015 redoHeightOfParagraph(bview, cursor);
3017 // now the special right address boxes
3018 if (cursor.par()->layout()->margintype
3019 == MARGIN_RIGHT_ADDRESS_BOX) {
3020 redoDrawingOfParagraph(bview, cursor);
3026 bool LyXText::paintRowBackground(DrawRowParams & p)
3028 bool clear_area = true;
3030 LyXFont font(LyXFont::ALL_SANE);
3032 pos_type const last = rowLastPrintable(p.row);
3034 if (!p.bv->screen().forceClear() && last == p.row->pos()
3035 && p.row->par()->isInset(p.row->pos())) {
3036 inset = p.row->par()->getInset(p.row->pos());
3038 clear_area = inset->doClearArea();
3048 int const y = p.yo < 0 ? 0 : p.yo;
3049 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3050 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3057 int h = p.row->baseline() - inset->ascent(p.bv, font);
3059 // first clear the whole row above the inset!
3061 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3064 // clear the space below the inset!
3065 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3066 if ((p.row->height() - h) > 0) {
3067 p.pain->fillRectangle(p.xo, p.yo + h,
3068 p.width, p.row->height() - h, backgroundColor());
3071 // clear the space behind the inset, if needed
3072 if (!inset->display() && !inset->needFullRow()) {
3073 int const xp = int(p.x) + inset->width(p.bv, font);
3074 if (p.width - xp > 0) {
3075 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3076 p.row->height(), backgroundColor());
3084 void LyXText::paintRowSelection(DrawRowParams & p)
3086 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3088 // the current selection
3089 int const startx = selection.start.x();
3090 int const endx = selection.end.x();
3091 int const starty = selection.start.y();
3092 int const endy = selection.end.y();
3093 Row const * startrow = selection.start.row();
3094 Row const * endrow = selection.end.row();
3098 if (bidi_same_direction) {
3102 int h = row->height();
3104 if (startrow == row && endrow == row) {
3105 if (startx < endx) {
3108 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3112 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3114 } else if (startrow == row) {
3115 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3116 int const w = (is_rtl) ? startx : (p.width - startx);
3117 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3118 } else if (endrow == row) {
3119 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3120 int const w = (is_rtl) ? (p.width - endx) : endx;
3121 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3122 } else if (p.y > starty && p.y < endy) {
3123 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3126 } else if (startrow != row && endrow != row) {
3128 int h = row->height();
3129 if (p.y > starty && p.y < endy) {
3130 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3135 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3141 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3143 Buffer const * buffer = p.bv->buffer();
3144 Paragraph * par = row->par();
3145 pos_type main_body = beginningOfMainBody(buffer, par);
3146 pos_type const last = rowLastPrintable(row);
3148 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3149 pos_type pos = vis2log(vpos);
3150 float const old_tmpx = tmpx;
3151 if (main_body > 0 && pos == main_body - 1) {
3152 LyXLayout_ptr const & layout = par->layout();
3153 LyXFont const lfont = getLabelFont(buffer, par);
3155 tmpx += p.label_hfill + font_metrics::width(layout->labelsep, lfont);
3157 if (par->isLineSeparator(main_body - 1))
3158 tmpx -= singleWidth(p.bv, par, main_body - 1);
3161 if (hfillExpansion(buffer, row, pos)) {
3162 tmpx += singleWidth(p.bv, par, pos);
3163 if (pos >= main_body)
3166 tmpx += p.label_hfill;
3169 else if (par->isSeparator(pos)) {
3170 tmpx += singleWidth(p.bv, par, pos);
3171 if (pos >= main_body)
3172 tmpx += p.separator;
3174 tmpx += singleWidth(p.bv, par, pos);
3177 if ((startrow != row || selection.start.pos() <= pos) &&
3178 (endrow != row || pos < selection.end.pos())) {
3179 // Here we do not use p.x as p.xo was added to p.x.
3180 p.pain->fillRectangle(int(old_tmpx), p.yo,
3181 int(tmpx - old_tmpx + 1),
3182 row->height(), LColor::selection);
3185 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3186 p.pain->fillRectangle(p.xo + int(tmpx),
3187 p.yo, int(p.bv->workWidth() - tmpx),
3188 row->height(), LColor::selection);
3194 void LyXText::paintRowAppendix(DrawRowParams & p)
3196 // FIXME: can be just p.width ?
3197 int const ww = p.bv->workWidth();
3198 Paragraph * firstpar = p.row->par();
3200 if (firstpar->params().appendix()) {
3201 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3202 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3207 void LyXText::paintRowDepthBar(DrawRowParams & p)
3209 Paragraph::depth_type const depth = p.row->par()->getDepth();
3214 Paragraph::depth_type prev_depth = 0;
3215 if (p.row->previous())
3216 prev_depth = p.row->previous()->par()->getDepth();
3217 Paragraph::depth_type next_depth = 0;
3219 next_depth = p.row->next()->par()->getDepth();
3221 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3222 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3223 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3225 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3227 int const w = LYX_PAPER_MARGIN / 5;
3229 if (i > prev_depth) {
3230 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3232 if (i > next_depth) {
3233 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3239 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3241 int const arrow_size = 4;
3242 int const space_size = int(vsp.inPixels(bv));
3244 if (vsp.kind() != VSpace::LENGTH) {
3250 int const min_size = max(3 * arrow_size,
3251 font_metrics::maxAscent(font)
3252 + font_metrics::maxDescent(font));
3254 if (vsp.length().len().value() < 0.0)
3257 return max(min_size, space_size);
3261 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3262 VSpace const & vsp, int start)
3264 int const arrow_size = 4;
3265 int const size = getLengthMarkerHeight(p.bv, vsp);
3266 int const end = start + size;
3268 // the label to display (if any)
3270 // y-values for top arrow
3272 // y-values for bottom arrow
3274 switch (vsp.kind()) {
3275 case VSpace::LENGTH:
3277 str = prefix + " (" + vsp.asLyXCommand() + ")";
3278 // adding or removing space
3279 bool const added = !(vsp.length().len().value() < 0.0);
3280 ty1 = added ? (start + arrow_size) : start;
3281 ty2 = added ? start : (start + arrow_size);
3282 by1 = added ? (end - arrow_size) : end;
3283 by2 = added ? end : (end - arrow_size);
3286 case VSpace:: VFILL:
3287 str = prefix + " (vertical fill)";
3292 // nothing to draw here
3296 int const leftx = p.xo + leftMargin(p.bv, p.row);
3297 int const midx = leftx + arrow_size;
3298 int const rightx = midx + arrow_size;
3306 font.setColor(LColor::added_space).decSize();
3307 font_metrics::rectText(str, font, w, a, d);
3309 p.pain->rectText(leftx + 2 * arrow_size + 5,
3310 start + ((end - start) / 2) + d,
3316 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3317 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3320 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3321 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3324 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3330 void LyXText::paintFirstRow(DrawRowParams & p)
3332 Paragraph * par = p.row->par();
3333 ParagraphParameters const & parparams = par->params();
3335 // start of appendix?
3336 if (parparams.startOfAppendix()) {
3337 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3342 // think about the margins
3343 if (!p.row->previous() && bv_owner)
3344 y_top += LYX_PAPER_MARGIN;
3346 // draw a top pagebreak
3347 if (parparams.pagebreakTop()) {
3348 int const y = p.yo + y_top + 2*defaultHeight();
3349 p.pain->line(p.xo, y, p.xo + p.width, y,
3350 LColor::pagebreak, Painter::line_onoffdash);
3357 pb_font.setColor(LColor::pagebreak).decSize();
3358 font_metrics::rectText(_("Page Break (top)"), pb_font, w, a, d);
3359 p.pain->rectText((p.width - w)/2, y + d,
3360 _("Page Break (top)"), pb_font,
3363 y_top += 3 * defaultHeight();
3366 // draw the additional space if needed:
3367 y_top += drawLengthMarker(p, _("Space above"),
3368 parparams.spaceTop(), p.yo + y_top);
3370 Buffer const * buffer = p.bv->buffer();
3372 LyXLayout_ptr const & layout = par->layout();
3374 // think about the parskip
3375 // some parskips VERY EASY IMPLEMENTATION
3376 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3377 if (par->previous()) {
3378 if (layout->latextype == LATEX_PARAGRAPH
3379 && !par->getDepth()) {
3380 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3382 LyXLayout_ptr const & playout =
3383 par->previous()->layout();
3384 if (playout->latextype == LATEX_PARAGRAPH
3385 && !par->previous()->getDepth()) {
3386 // is it right to use defskip here, too? (AS)
3387 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3393 int const ww = p.bv->workWidth();
3396 if (parparams.lineTop()) {
3397 LyXFont font(LyXFont::ALL_SANE);
3398 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3402 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3403 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3404 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3405 LColor::topline, Painter::line_solid,
3406 Painter::line_thick);
3411 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3413 // should we print a label?
3414 if (layout->labeltype >= LABEL_STATIC
3415 && (layout->labeltype != LABEL_STATIC
3416 || layout->latextype != LATEX_ENVIRONMENT
3417 || par->isFirstInSequence())) {
3419 LyXFont font = getLabelFont(buffer, par);
3420 if (!par->getLabelstring().empty()) {
3422 string const str = par->getLabelstring();
3424 // this is special code for the chapter layout. This is
3425 // printed in an extra row and has a pagebreak at
3427 if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
3428 if (buffer->params.secnumdepth >= 0) {
3429 float spacing_val = 1.0;
3430 if (!parparams.spacing().isDefault()) {
3431 spacing_val = parparams.spacing().getValue();
3433 spacing_val = buffer->params.spacing.getValue();
3437 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
3438 + int(layout->parsep) * defaultHeight();
3441 x = ww - leftMargin(p.bv, p.row) -
3442 font_metrics::width(str, font);
3445 p.pain->text(int(x),
3446 p.yo + p.row->baseline() -
3447 p.row->ascent_of_text() - maxdesc,
3452 x = ww - leftMargin(p.bv, p.row)
3453 + font_metrics::width(layout->labelsep, font);
3455 x = p.x - font_metrics::width(layout->labelsep, font)
3456 - font_metrics::width(str, font);
3459 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3462 // the labels at the top of an environment.
3463 // More or less for bibliography
3464 } else if (par->isFirstInSequence() &&
3465 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
3466 layout->labeltype == LABEL_BIBLIO ||
3467 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3468 LyXFont font = getLabelFont(buffer, par);
3469 if (!par->getLabelstring().empty()) {
3470 string const str = par->getLabelstring();
3471 float spacing_val = 1.0;
3472 if (!parparams.spacing().isDefault()) {
3473 spacing_val = parparams.spacing().getValue();
3475 spacing_val = buffer->params.spacing.getValue();
3479 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
3480 + (layout->labelbottomsep * defaultHeight()));
3483 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3484 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3485 + ww - rightMargin(buffer, p.row)) / 2;
3486 x -= font_metrics::width(str, font) / 2;
3487 } else if (is_rtl) {
3488 x = ww - leftMargin(p.bv, p.row) -
3489 font_metrics::width(str, font);
3491 p.pain->text(int(x), p.yo + p.row->baseline()
3492 - p.row->ascent_of_text() - maxdesc,
3497 if (layout->labeltype == LABEL_BIBLIO && par->bibkey) {
3498 LyXFont font = getLayoutFont(buffer, par);
3501 x = ww - leftMargin(p.bv, p.row)
3502 + font_metrics::width(layout->labelsep, font);
3504 x = p.x - font_metrics::width(layout->labelsep, font)
3505 - par->bibkey->width(p.bv, font);
3507 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3512 void LyXText::paintLastRow(DrawRowParams & p)
3514 Paragraph * par = p.row->par();
3515 ParagraphParameters const & parparams = par->params();
3516 int y_bottom = p.row->height() - 1;
3518 // think about the margins
3519 if (!p.row->next() && bv_owner)
3520 y_bottom -= LYX_PAPER_MARGIN;
3522 int const ww = p.bv->workWidth();
3524 // draw a bottom pagebreak
3525 if (parparams.pagebreakBottom()) {
3527 pb_font.setColor(LColor::pagebreak).decSize();
3528 int const y = p.yo + y_bottom - 2 * defaultHeight();
3530 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3531 Painter::line_onoffdash);
3536 font_metrics::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3537 p.pain->rectText((ww - w) / 2, y + d,
3538 _("Page Break (bottom)"),
3539 pb_font, backgroundColor(), backgroundColor());
3541 y_bottom -= 3 * defaultHeight();
3544 // draw the additional space if needed:
3545 int const height = getLengthMarkerHeight(p.bv,
3546 parparams.spaceBottom());
3547 y_bottom -= drawLengthMarker(p, _("Space below"),
3548 parparams.spaceBottom(),
3549 p.yo + y_bottom - height);
3551 Buffer const * buffer = p.bv->buffer();
3553 // draw a bottom line
3554 if (parparams.lineBottom()) {
3555 LyXFont font(LyXFont::ALL_SANE);
3556 int const asc = font_metrics::ascent('x',
3557 getFont(buffer, par,
3558 max(pos_type(0), par->size() - 1)));
3562 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3563 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3564 int const y = p.yo + y_bottom;
3565 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3566 Painter::line_thick);
3571 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3572 int const endlabel = par->getEndLabel();
3577 case END_LABEL_FILLED_BOX:
3579 LyXFont const font = getLabelFont(buffer, par);
3580 int const size = int(0.75 * font_metrics::maxAscent(font));
3581 int const y = (p.yo + p.row->baseline()) - size;
3582 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3584 if (p.row->fill() <= size)
3585 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3587 if (endlabel == END_LABEL_BOX) {
3588 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3590 p.pain->fillRectangle(x, y, size, size,
3595 case END_LABEL_STATIC:
3598 LyXFont font(LyXFont::ALL_SANE);
3599 font = getLabelFont(buffer, par);
3601 LyXFont font = getLabelFont(buffer, par);
3603 string const & str = par->layout()->endlabelstring();
3604 int const x = is_rtl ?
3605 int(p.x) - font_metrics::width(str, font)
3606 : ww - rightMargin(buffer, p.row) - p.row->fill();
3607 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3610 case END_LABEL_NO_LABEL:
3615 void LyXText::paintRowText(DrawRowParams & p)
3617 Paragraph * par = p.row->par();
3618 Buffer const * buffer = p.bv->buffer();
3620 pos_type const last = rowLastPrintable(p.row);
3621 pos_type main_body =
3622 beginningOfMainBody(buffer, par);
3623 if (main_body > 0 &&
3624 (main_body - 1 > last ||
3625 !par->isLineSeparator(main_body - 1))) {
3629 LyXLayout_ptr const & layout = par->layout();
3631 pos_type vpos = p.row->pos();
3632 while (vpos <= last) {
3633 if (p.x > p.bv->workWidth())
3635 pos_type pos = vis2log(vpos);
3636 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3637 p.x += singleWidth(p.bv, par, pos);
3641 if (main_body > 0 && pos == main_body - 1) {
3642 int const lwidth = font_metrics::width(layout->labelsep,
3643 getLabelFont(buffer, par));
3645 p.x += p.label_hfill + lwidth
3646 - singleWidth(p.bv, par, main_body - 1);
3649 if (par->isHfill(pos)) {
3652 int const y0 = p.yo + p.row->baseline();
3653 int const y1 = y0 - defaultHeight() / 2;
3655 p.pain->line(int(p.x), y1, int(p.x), y0,
3656 LColor::added_space);
3658 if (hfillExpansion(buffer, p.row, pos)) {
3659 int const y2 = (y0 + y1) / 2;
3661 if (pos >= main_body) {
3662 p.pain->line(int(p.x), y2,
3663 int(p.x + p.hfill), y2,
3664 LColor::added_space,
3665 Painter::line_onoffdash);
3668 p.pain->line(int(p.x), y2,
3669 int(p.x + p.label_hfill), y2,
3670 LColor::added_space,
3671 Painter::line_onoffdash);
3672 p.x += p.label_hfill;
3674 p.pain->line(int(p.x), y1,
3676 LColor::added_space);
3680 } else if (par->isSeparator(pos)) {
3681 p.x += singleWidth(p.bv, par, pos);
3682 if (pos >= main_body)
3693 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3694 Row * row, int y, bool cleared)
3696 if (row->height() <= 0) {
3697 lyxerr << "LYX_ERROR: row.height: "
3698 << row->height() << endl;
3704 // set up drawing parameters
3706 p.pain = &bv->painter();
3710 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3711 if (inset_owner && (p.x < 0))
3715 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3716 p.cleared = cleared;
3720 // clear to background if necessary
3721 p.cleared = paintRowBackground(p);
3723 // paint the selection background
3724 if (selection.set()) {
3725 paintRowSelection(p);
3728 // vertical lines for appendix
3729 paintRowAppendix(p);
3731 // environment depth brackets
3732 paintRowDepthBar(p);
3734 // draw any stuff wanted for a first row of a paragraph
3739 // draw any stuff wanted for the last row of a paragraph
3740 if (!row->next() || (row->next()->par() != row->par())) {
3749 int LyXText::defaultHeight() const
3751 LyXFont font(LyXFont::ALL_SANE);
3752 return int(font_metrics::maxAscent(font)
3753 + font_metrics::maxDescent(font) * 1.5);
3757 // returns the column near the specified x-coordinate of the row
3758 // x is set to the real beginning of this column
3760 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3761 bool & boundary) const
3764 float fill_separator;
3766 float fill_label_hfill;
3768 prepareToPrint(bview, row, tmpx, fill_separator,
3769 fill_hfill, fill_label_hfill);
3771 pos_type vc = row->pos();
3772 pos_type last = rowLastPrintable(row);
3775 LyXLayout_ptr const & layout = row->par()->layout();
3777 bool left_side = false;
3779 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3780 float last_tmpx = tmpx;
3782 if (main_body > 0 &&
3783 (main_body - 1 > last ||
3784 !row->par()->isLineSeparator(main_body - 1)))
3787 while (vc <= last && tmpx <= x) {
3790 if (main_body > 0 && c == main_body-1) {
3791 tmpx += fill_label_hfill +
3792 font_metrics::width(layout->labelsep,
3793 getLabelFont(bview->buffer(), row->par()));
3794 if (row->par()->isLineSeparator(main_body - 1))
3795 tmpx -= singleWidth(bview, row->par(), main_body-1);
3798 if (hfillExpansion(bview->buffer(), row, c)) {
3799 x += singleWidth(bview, row->par(), c);
3803 tmpx += fill_label_hfill;
3805 else if (row->par()->isSeparator(c)) {
3806 tmpx += singleWidth(bview, row->par(), c);
3808 tmpx+= fill_separator;
3810 tmpx += singleWidth(bview, row->par(), c);
3814 if ((tmpx + last_tmpx) / 2 > x) {
3819 if (vc > last + 1) // This shouldn't happen.
3823 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3824 // some speedup if rtl_support=false
3825 && (!row->next() || row->next()->par() != row->par());
3826 bool const rtl = (lastrow)
3827 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3828 : false; // If lastrow is false, we don't need to compute
3829 // the value of rtl.
3831 if (row->pos() > last) // Row is empty?
3834 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3835 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3837 else if (vc == row->pos()) {
3839 if (bidi_level(c) % 2 == 1)
3842 c = vis2log(vc - 1);
3843 bool const rtl = (bidi_level(c) % 2 == 1);
3844 if (left_side == rtl) {
3846 boundary = isBoundary(bview->buffer(), row->par(), c);
3850 if (row->pos() <= last && c > last
3851 && row->par()->isNewline(last)) {
3852 if (bidi_level(last) % 2 == 0)
3853 tmpx -= singleWidth(bview, row->par(), last);
3855 tmpx += singleWidth(bview, row->par(), last);
3865 // returns pointer to a specified row
3866 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3871 Row * tmprow = firstrow;
3874 // find the first row of the specified paragraph
3875 while (tmprow->next() && tmprow->par() != par) {
3876 y += tmprow->height();
3877 tmprow = tmprow->next();
3880 // now find the wanted row
3881 while (tmprow->pos() < pos
3883 && tmprow->next()->par() == par
3884 && tmprow->next()->pos() <= pos) {
3885 y += tmprow->height();
3886 tmprow = tmprow->next();
3893 Row * LyXText::getRowNearY(int & y) const
3896 // If possible we should optimize this method. (Lgb)
3897 Row * tmprow = firstrow;
3900 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3901 tmpy += tmprow->height();
3902 tmprow = tmprow->next();
3905 y = tmpy; // return the real y
3907 //lyxerr << "returned y = " << y << endl;
3911 // Search from the current cursor position.
3913 Row * tmprow = cursor.row();
3914 int tmpy = cursor.y() - tmprow->baseline();
3916 lyxerr << "cursor.y() = " << tmpy << endl;
3917 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3918 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3919 lyxerr << "first = " << first << endl;
3920 lyxerr << "y = " << y << endl;
3923 lyxerr << "up" << endl;
3925 tmpy -= tmprow->height();
3926 tmprow = tmprow->previous();
3927 } while (tmprow && tmpy - tmprow->height() >= y);
3928 } else if (y > tmpy) {
3929 lyxerr << "down" << endl;
3931 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3932 tmpy += tmprow->height();
3933 tmprow = tmprow->next();
3936 lyxerr << "equal" << endl;
3939 y = tmpy; // return the real y
3941 lyxerr << "returned y = " << y << endl;
3949 int LyXText::getDepth() const
3951 return cursor.par()->getDepth();