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"
27 #include "lyxscreen.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
38 #include "support/textutils.h"
39 #include "support/LAssert.h"
40 #include "support/lstrings.h"
52 int const LYX_PAPER_MARGIN = 20;
56 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
59 int LyXText::workWidth(BufferView * bview) const
62 return inset_owner->textWidth(bview);
64 return bview->workWidth();
68 int LyXText::workWidth(BufferView * bview, Inset * inset) const
73 par = inset->parOwner();
75 pos = par->getPositionOfInset(inset);
77 if (!par || pos == -1) {
78 lyxerr << "LyXText::workWidth: something is wrong,"
79 " fall back to the brute force method" << endl;
80 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
81 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
82 for (; it != end; ++it) {
92 return workWidth(bview);
95 LyXLayout const & layout =
96 textclasslist[bview->buffer()->params.textclass][par->layout()];
98 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
99 // Optimization here: in most cases, the real row is
100 // not needed, but only the par/pos values. So we just
101 // construct a dummy row for leftMargin. (JMarc)
105 return workWidth(bview) - leftMargin(bview, &dummyrow);
108 Row * row = getRow(par, pos, dummy_y);
110 while (frow->previous() && frow->par() == frow->previous()->par())
111 frow = frow->previous();
112 unsigned int maxw = 0;
113 while (frow->next() && frow->par() == frow->next()->par()) {
114 if ((frow != row) && (maxw < frow->width()))
115 maxw = frow->width();
121 return workWidth(bview);
125 int LyXText::getRealCursorX(BufferView * bview) const
128 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
129 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
137 if (!Encodings::is_arabic(c))
138 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139 return c + (0xb0 - '0');
143 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
144 unsigned char next_char = ' ';
146 for (pos_type i = pos+1; i < par->size(); ++i)
147 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
148 next_char = par->getChar(i);
152 if (Encodings::is_arabic(next_char)) {
153 if (Encodings::is_arabic(prev_char))
154 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
156 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
158 if (Encodings::is_arabic(prev_char))
159 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
161 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
165 // This is the comments that some of the warnings below refers to.
166 // There are some issues in this file and I don't think they are
167 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
168 // this is a problem that has been here almost from day one and that a
169 // larger userbase with differenct access patters triggers the bad
170 // behaviour. (segfaults.) What I think happen is: In several places
171 // we store the paragraph in the current cursor and then moves the
172 // cursor. This movement of the cursor will delete paragraph at the
173 // old position if it is now empty. This will make the temporary
174 // pointer to the old cursor paragraph invalid and dangerous to use.
175 // And is some cases this will trigger a segfault. I have marked some
176 // of the cases where this happens with a warning, but I am sure there
177 // are others in this file and in text2.C. There is also a note in
178 // Delete() that you should read. In Delete I store the paragraph->id
179 // instead of a pointer to the paragraph. I am pretty sure this faulty
180 // use of temporary pointers to paragraphs that might have gotten
181 // invalidated (through a cursor movement) before they are used, are
182 // the cause of the strange crashes we get reported often.
184 // It is very tiresom to change this code, especially when it is as
185 // hard to read as it is. Help to fix all the cases where this is done
186 // would be greately appreciated.
190 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
193 char const c = par->getChar(pos);
194 return singleWidth(bview, par, pos, c);
198 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
199 pos_type pos, char c) const
201 LyXFont const font = getFont(bview->buffer(), par, pos);
203 // The most common case is handled first (Asger)
204 if (IsPrintable(c)) {
205 if (font.language()->RightToLeft()) {
206 if (font.language()->lang() == "arabic" &&
207 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
208 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
209 if (Encodings::IsComposeChar_arabic(c))
212 c = transformChar(c, par, pos);
213 else if (font.language()->lang() == "hebrew" &&
214 Encodings::IsComposeChar_hebrew(c))
217 return lyxfont::width(c, font);
219 } else if (IsHfillChar(c)) {
220 return 3; /* Because of the representation
221 * as vertical lines */
222 } else if (c == Paragraph::META_INSET) {
223 Inset * tmpinset = par->getInset(pos);
226 // this IS needed otherwise on initialitation we don't get the fill
227 // of the row right (ONLY on initialization if we read a file!)
228 // should be changed! (Jug 20011204)
229 tmpinset->update(bview, font);
231 return tmpinset->width(bview, font);
235 } else if (IsSeparatorChar(c))
237 else if (IsNewlineChar(c))
239 return lyxfont::width(c, font);
243 // Returns the paragraph position of the last character in the specified row
244 pos_type LyXText::rowLast(Row const * row) const
246 if (!row->next() || row->next()->par() != row->par()) {
247 return row->par()->size() - 1;
249 return row->next()->pos() - 1;
254 pos_type LyXText::rowLastPrintable(Row const * row) const
256 pos_type const last = rowLast(row);
257 if (last >= row->pos()
259 && row->next()->par() == row->par()
260 && row->par()->isSeparator(last))
267 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
269 bidi_same_direction = true;
270 if (!lyxrc.rtl_support) {
275 bidi_start = row->pos();
276 bidi_end = rowLastPrintable(row);
278 if (bidi_start > bidi_end) {
283 if (bidi_end + 2 - bidi_start >
284 static_cast<pos_type>(log2vis_list.size())) {
286 (bidi_end + 2 - bidi_start < 500) ?
287 500 : 2 * (bidi_end + 2 - bidi_start);
288 log2vis_list.resize(new_size);
289 vis2log_list.resize(new_size);
290 bidi_levels.resize(new_size);
293 vis2log_list[bidi_end + 1 - bidi_start] = -1;
294 log2vis_list[bidi_end + 1 - bidi_start] = -1;
298 row->par()->getParLanguage(buf->params)->RightToLeft();
302 pos_type const main_body = beginningOfMainBody(buf, row->par());
304 for (pos_type lpos = bidi_start;
305 lpos <= bidi_end; ++lpos) {
306 bool is_space = row->par()->isLineSeparator(lpos);
308 (is_space && lpos + 1 <= bidi_end &&
309 !row->par()->isLineSeparator(lpos + 1) &&
310 !row->par()->isNewline(lpos + 1))
312 LyXFont font = row->par()->getFontSettings(buf->params, pos);
313 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
314 font.number() == LyXFont::ON &&
315 row->par()->getFontSettings(buf->params, lpos - 1).number()
317 font = row->par()->getFontSettings(buf->params, lpos);
322 bool new_rtl = font.isVisibleRightToLeft();
323 bool new_rtl0 = font.isRightToLeft();
326 if (lpos == main_body - 1
327 && row->pos() < main_body - 1
329 new_level = (rtl_par) ? 1 : 0;
330 new_rtl = new_rtl0 = rtl_par;
332 new_level = (new_rtl) ? 1 : 2;
334 new_level = (rtl_par) ? 2 : 0;
336 if (is_space && new_level >= level) {
342 int new_level2 = new_level;
344 if (level == new_level && rtl0 != new_rtl0) {
346 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
347 } else if (level < new_level) {
348 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
349 if (new_level > rtl_par)
350 bidi_same_direction = false;
352 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
355 bidi_levels[lpos - bidi_start] = new_level;
357 while (level > new_level2) {
358 pos_type old_lpos = stack[--level];
359 int delta = lpos - old_lpos - 1;
362 log2vis_list[lpos - bidi_start] += delta;
363 log2vis_list[old_lpos - bidi_start] += delta;
365 while (level < new_level)
366 stack[level++] = lpos;
370 pos_type const old_lpos = stack[--level];
371 int delta = bidi_end - old_lpos;
374 log2vis_list[old_lpos - bidi_start] += delta;
377 pos_type vpos = bidi_start - 1;
378 for (pos_type lpos = bidi_start;
379 lpos <= bidi_end; ++lpos) {
380 vpos += log2vis_list[lpos - bidi_start];
381 vis2log_list[vpos - bidi_start] = lpos;
382 log2vis_list[lpos - bidi_start] = vpos;
387 // This method requires a previous call to ComputeBidiTables()
388 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
391 if (!lyxrc.rtl_support || pos == 0)
394 if (!bidi_InRange(pos - 1)) {
395 /// This can happen if pos is the first char of a row.
396 /// Returning false in this case is incorrect!
400 bool const rtl = bidi_level(pos - 1) % 2;
401 bool const rtl2 = bidi_InRange(pos)
402 ? bidi_level(pos) % 2
403 : par->isRightToLeftPar(buf->params);
408 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
409 pos_type pos, LyXFont const & font) const
411 if (!lyxrc.rtl_support)
412 return false; // This is just for speedup
414 bool const rtl = font.isVisibleRightToLeft();
415 bool const rtl2 = bidi_InRange(pos)
416 ? bidi_level(pos) % 2
417 : par->isRightToLeftPar(buf->params);
421 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
423 // Draw end-of-line marker
424 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
425 int const wid = lyxfont::width('n', font);
426 int const asc = lyxfont::maxAscent(font);
427 int const y = p.yo + p.row->baseline();
431 yp[0] = int(y - 0.875 * asc * 0.75);
432 yp[1] = int(y - 0.500 * asc * 0.75);
433 yp[2] = int(y - 0.125 * asc * 0.75);
435 if (bidi_level(pos) % 2 == 0) {
436 xp[0] = int(p.x + wid * 0.375);
438 xp[2] = int(p.x + wid * 0.375);
440 xp[0] = int(p.x + wid * 0.625);
441 xp[1] = int(p.x + wid);
442 xp[2] = int(p.x + wid * 0.625);
445 p.pain->lines(xp, yp, 3, LColor::eolmarker);
447 yp[0] = int(y - 0.500 * asc * 0.75);
448 yp[1] = int(y - 0.500 * asc * 0.75);
449 yp[2] = int(y - asc * 0.75);
451 if (bidi_level(pos) % 2 == 0) {
453 xp[1] = int(p.x + wid);
454 xp[2] = int(p.x + wid);
456 xp[0] = int(p.x + wid);
461 p.pain->lines(xp, yp, 3, LColor::eolmarker);
467 void LyXText::drawInset(DrawRowParams & p, pos_type const pos)
469 Inset * inset = p.row->par()->getInset(pos);
471 // FIXME: shouldn't happen
476 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
478 inset->update(p.bv, font, false);
479 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
481 if (!need_break_row && !inset_owner
482 && p.bv->text->status() == CHANGED_IN_DRAW) {
483 Row * prev = p.row->previous();
484 if (prev && prev->par() == p.row->par()) {
485 breakAgainOneRow(p.bv, prev);
486 if (prev->next() != p.row) {
487 // breakAgainOneRow() has removed p.row
488 p.row = 0; // see what this breaks
489 need_break_row = prev;
491 need_break_row = p.row;
494 need_break_row = p.row;
496 setCursor(p.bv, cursor.par(), cursor.pos());
501 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
503 if (!lyxrc.mark_foreign_language)
505 if (orig_font.language() == latex_language)
507 if (orig_font.language() == p.bv->buffer()->params.language)
510 int const y = p.yo + p.row->height() - 1;
511 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
515 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
517 pos_type pos = vis2log(vpos);
522 char c = p.row->par()->getChar(pos);
526 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
527 int const width = lyxfont::width(c, font);
530 for (pos_type i = pos-1; i >= 0; --i) {
531 c = p.row->par()->getChar(i);
532 if (!Encodings::IsComposeChar_hebrew(c)) {
533 if (IsPrintableNonspace(c)) {
535 singleWidth(p.bv, p.row->par(), i, c);
537 dx = (c == 'ø' || c == 'ã')
539 : (width2 - width) / 2;
546 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
550 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
552 pos_type pos = vis2log(vpos);
556 char c = p.row->par()->getChar(pos);
557 c = transformChar(c, p.row->par(), pos);
561 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
562 int const width = lyxfont::width(c, font);
565 for (pos_type i = pos-1; i >= 0; --i) {
566 c = p.row->par()->getChar(i);
567 if (!Encodings::IsComposeChar_arabic(c)) {
568 if (IsPrintableNonspace(c)) {
570 singleWidth(p.bv, p.row->par(), i, c);
571 dx = (width2 - width) / 2;
577 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
581 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
582 bool hebrew, bool arabic)
584 pos_type pos = vis2log(vpos);
585 pos_type const last = rowLastPrintable(p.row);
586 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
590 str += p.row->par()->getChar(pos);
592 unsigned char c = str[0];
593 str[0] = transformChar(c, p.row->par(), pos);
597 // collect as much similar chars as we can
598 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
599 char c = p.row->par()->getChar(pos);
601 if (!IsPrintableNonspace(c))
604 if (arabic && Encodings::IsComposeChar_arabic(c))
606 if (hebrew && Encodings::IsComposeChar_hebrew(c))
609 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
616 // Draw text and set the new x position
617 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
618 p.x += lyxfont::width(str, orig_font);
622 void LyXText::draw(DrawRowParams & p, pos_type & vpos)
624 pos_type const pos = vis2log(vpos);
625 Paragraph * par = p.row->par();
627 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
629 float const orig_x = p.x;
631 char const c = par->getChar(pos);
633 if (IsNewlineChar(c)) {
637 } else if (IsInsetChar(c)) {
640 drawForeignMark(p, orig_x, orig_font);
644 // usual characters, no insets
646 // special case languages
647 bool const hebrew = (orig_font.language()->lang() == "hebrew");
649 orig_font.language()->lang() == "arabic" &&
650 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
651 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
653 // draw as many chars as we can
654 if ((!hebrew && !arabic)
655 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
656 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
657 drawChars(p, vpos, true, false);
659 drawHebrewComposeChar(p, vpos);
661 drawArabicComposeChar(p, vpos);
664 drawForeignMark(p, orig_x, orig_font);
666 #ifdef INHERIT_LANGUAGE
668 if ((font.language() == inherit_language) ||
669 (font.language() == ignore_language))
670 lyxerr << "No this shouldn't happen!\n";
676 // Returns the left beginning of the text.
677 // This information cannot be taken from the layouts-objekt, because in
678 // LaTeX the beginning of the text fits in some cases (for example sections)
679 // exactly the label-width.
680 int LyXText::leftMargin(BufferView * bview, Row const * row) const
682 LyXTextClass const & tclass =
683 textclasslist[bview->buffer()->params.textclass];
684 LyXLayout const & layout = tclass[row->par()->layout()];
686 string parindent = layout.parindent;
688 int x = LYX_PAPER_MARGIN;
690 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
692 // this is the way, LyX handles the LaTeX-Environments.
693 // I have had this idea very late, so it seems to be a
694 // later added hack and this is true
695 if (!row->par()->getDepth()) {
696 if (row->par()->layout() == tclass.defaultLayoutName()) {
697 // find the previous same level paragraph
698 if (row->par()->previous()) {
699 Paragraph * newpar = row->par()
700 ->depthHook(row->par()->getDepth());
702 tclass[newpar->layout()].nextnoindent)
707 // find the next level paragraph
710 row->par()->outerHook();
712 // make a corresponding row. Needed to call LeftMargin()
714 // check wether it is a sufficent paragraph
715 if (newpar && tclass[newpar->layout()].isEnvironment()) {
717 dummyrow.par(newpar);
718 dummyrow.pos(newpar->size());
719 x = leftMargin(bview, &dummyrow);
721 // this is no longer an error, because this function
722 // is used to clear impossible depths after changing
723 // a layout. Since there is always a redo,
724 // LeftMargin() is always called
725 row->par()->params().depth(0);
728 if (newpar && row->par()->layout() == tclass.defaultLayoutName()) {
729 if (newpar->params().noindent())
732 parindent = tclass[newpar->layout()].parindent;
736 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
737 switch (layout.margintype) {
739 if (!layout.leftmargin.empty()) {
740 x += lyxfont::signedWidth(layout.leftmargin,
741 tclass.defaultfont());
743 if (!row->par()->getLabelstring().empty()) {
744 x += lyxfont::signedWidth(layout.labelindent,
746 x += lyxfont::width(row->par()->getLabelstring(),
748 x += lyxfont::width(layout.labelsep, labelfont);
752 x += lyxfont::signedWidth(layout.labelindent, labelfont);
753 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
754 if (!row->par()->getLabelWidthString().empty()) {
755 x += lyxfont::width(row->par()->getLabelWidthString(),
757 x += lyxfont::width(layout.labelsep, labelfont);
762 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
763 / (row->par()->getDepth() + 4);
765 case MARGIN_FIRST_DYNAMIC:
766 if (layout.labeltype == LABEL_MANUAL) {
767 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
768 x += lyxfont::signedWidth(layout.leftmargin,
771 x += lyxfont::signedWidth(layout.labelindent,
774 } else if (row->pos()
775 // Special case to fix problems with
777 || (layout.labeltype == LABEL_STATIC
778 && layout.latextype == LATEX_ENVIRONMENT
779 && ! row->par()->isFirstInSequence())) {
780 x += lyxfont::signedWidth(layout.leftmargin,
782 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
783 && layout.labeltype != LABEL_BIBLIO
784 && layout.labeltype !=
785 LABEL_CENTERED_TOP_ENVIRONMENT) {
786 x += lyxfont::signedWidth(layout.labelindent,
788 x += lyxfont::width(layout.labelsep, labelfont);
789 x += lyxfont::width(row->par()->getLabelstring(),
794 case MARGIN_RIGHT_ADDRESS_BOX:
796 // ok, a terrible hack. The left margin depends on the widest
797 // row in this paragraph. Do not care about footnotes, they
798 // are *NOT* allowed in the LaTeX realisation of this layout.
800 // find the first row of this paragraph
801 Row const * tmprow = row;
802 while (tmprow->previous()
803 && tmprow->previous()->par() == row->par())
804 tmprow = tmprow->previous();
806 int minfill = tmprow->fill();
807 while (tmprow->next() && tmprow->next()->par() == row->par()) {
808 tmprow = tmprow->next();
809 if (tmprow->fill() < minfill)
810 minfill = tmprow->fill();
813 x += lyxfont::signedWidth(layout.leftmargin,
814 tclass.defaultfont());
820 LyXAlignment align; // wrong type
822 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
823 align = layout.align;
825 align = row->par()->params().align();
827 // set the correct parindent
828 if (row->pos() == 0) {
829 if ((layout.labeltype == LABEL_NO_LABEL
830 || layout.labeltype == LABEL_TOP_ENVIRONMENT
831 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
832 || (layout.labeltype == LABEL_STATIC
833 && layout.latextype == LATEX_ENVIRONMENT
834 && ! row->par()->isFirstInSequence()))
835 && align == LYX_ALIGN_BLOCK
836 && !row->par()->params().noindent()
837 // in tabulars paragraphs are never indented!
838 && (!row->par()->inInset() || row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE)
839 && (row->par()->layout() != tclass.defaultLayoutName() ||
840 bview->buffer()->params.paragraph_separation ==
841 BufferParams::PARSEP_INDENT)) {
842 x += lyxfont::signedWidth(parindent,
843 tclass.defaultfont());
844 } else if (layout.labeltype == LABEL_BIBLIO) {
845 // ale970405 Right width for bibitems
846 x += bibitemMaxWidth(bview, tclass.defaultfont());
854 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
856 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
857 LyXLayout const & layout = tclass[row->par()->layout()];
859 int x = LYX_PAPER_MARGIN
860 + lyxfont::signedWidth(tclass.rightmargin(),
861 tclass.defaultfont());
863 // this is the way, LyX handles the LaTeX-Environments.
864 // I have had this idea very late, so it seems to be a
865 // later added hack and this is true
866 if (row->par()->getDepth()) {
867 // find the next level paragraph
869 Paragraph * newpar = row->par();
872 newpar = newpar->previous();
874 && newpar->getDepth() >= row->par()->getDepth());
876 // make a corresponding row. Needed to call LeftMargin()
878 // check wether it is a sufficent paragraph
879 if (newpar && tclass[newpar->layout()].isEnvironment()) {
881 dummyrow.par(newpar);
883 x = rightMargin(buf, &dummyrow);
885 // this is no longer an error, because this function
886 // is used to clear impossible depths after changing
887 // a layout. Since there is always a redo,
888 // LeftMargin() is always called
889 row->par()->params().depth(0);
893 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
894 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
895 * 4 / (row->par()->getDepth() + 4);
900 int LyXText::labelEnd(BufferView * bview, Row const * row) const
902 if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
906 tmprow.pos(row->par()->size());
907 return leftMargin(bview, &tmprow); /* just the beginning
910 return 0; /* LabelEnd is only needed, if the
911 layout fills a flushleft
916 // get the next breakpoint in a given paragraph
918 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
920 Paragraph * par = row->par();
925 pos_type const pos = row->pos();
927 // position of the last possible breakpoint
928 // -1 isn't a suitable value, but a flag
929 pos_type last_separator = -1;
930 width -= rightMargin(bview->buffer(), row);
932 pos_type const main_body =
933 beginningOfMainBody(bview->buffer(), par);
934 LyXLayout const & layout =
935 textclasslist[bview->buffer()->params.textclass][par->layout()];
938 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
939 /* special code for right address boxes, only newlines count */
940 while (i < par->size()) {
941 if (par->isNewline(i)) {
943 i = par->size() - 1; // this means break
945 } else if (par->isInset(i) && par->getInset(i)
946 && par->getInset(i)->display()) {
947 par->getInset(i)->display(false);
952 // Last position is an invariant
953 pos_type const last =
955 // this is the usual handling
956 int x = leftMargin(bview, row);
957 bool doitonetime = true;
958 while (doitonetime || ((x < width) && (i < last))) {
960 char const c = par->getChar(i);
962 if (c == Paragraph::META_INSET)
963 in = par->getInset(i);
964 if (IsNewlineChar(c)) {
966 x = width; // this means break
967 } else if (in && !in->isChar()) {
968 // check wether a Display() inset is
969 // valid here. if not, change it to
972 (layout.isCommand() ||
973 (layout.labeltype == LABEL_MANUAL
974 && i < beginningOfMainBody(bview->buffer(), par))))
976 // display istn't allowd
978 x += singleWidth(bview, par, i, c);
979 } else if (in->display() || in->needFullRow()) {
980 // So break the line here
984 if (par->isLineSeparator(i+1))
987 last_separator = last; // to avoid extra rows
989 last_separator = i - 1;
990 x = width; // this means break
992 x += singleWidth(bview, par, i, c);
993 // we have to check this separately as we could have a
994 // lineseparator and then the algorithm below would prefer
995 // that which IS wrong! We should always break on an inset
996 // if it's too long and not on the last separator.
997 // Maybe the only exeption is insets used as chars but
998 // then we would have to have a special function inside
999 // the inset to tell us this. Till then we leave it as
1000 // it is now. (Jug 20020106)
1001 if (pos < i && x >= width && last_separator >= 0)
1002 last_separator = i - 1;
1005 if (par->isLineSeparator(i))
1007 x += singleWidth(bview, par, i, c);
1010 if (i == main_body) {
1011 x += lyxfont::width(layout.labelsep,
1012 getLabelFont(bview->buffer(), par));
1013 if (par->isLineSeparator(i - 1))
1014 x-= singleWidth(bview, par, i - 1);
1015 int left_margin = labelEnd(bview, row);
1016 if (x < left_margin)
1020 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1021 last_separator = i - 2;
1022 else if ((pos < i) && (last_separator < 0) && (x >= width))
1023 last_separator = i - 1;
1024 // end of paragraph is always a suitable separator
1025 else if (i == last && x < width)
1029 // well, if last_separator is still 0, the line isn't breakable.
1030 // don't care and cut simply at the end
1031 if (last_separator < 0) {
1035 // manual labels cannot be broken in LaTeX, do not care
1036 if (main_body && last_separator < main_body)
1037 last_separator = main_body - 1;
1039 return last_separator;
1043 // returns the minimum space a row needs on the screen in pixel
1044 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1046 if (paper_width < 0)
1050 // get the pure distance
1051 pos_type const last = rowLastPrintable(row);
1053 // special handling of the right address boxes
1054 if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
1055 == MARGIN_RIGHT_ADDRESS_BOX)
1057 int const tmpfill = row->fill();
1058 row->fill(0); // the minfill in MarginLeft()
1059 w = leftMargin(bview, row);
1062 w = leftMargin(bview, row);
1064 LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][row->par()->layout()];
1065 pos_type const main_body =
1066 beginningOfMainBody(bview->buffer(), row->par());
1067 pos_type i = row->pos();
1070 if (main_body > 0 && i == main_body) {
1071 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1072 if (row->par()->isLineSeparator(i - 1))
1073 w -= singleWidth(bview, row->par(), i - 1);
1074 int left_margin = labelEnd(bview, row);
1075 if (w < left_margin)
1078 w += singleWidth(bview, row->par(), i);
1081 if (main_body > 0 && main_body > last) {
1082 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1083 if (last >= 0 && row->par()->isLineSeparator(last))
1084 w -= singleWidth(bview, row->par(), last);
1085 int const left_margin = labelEnd(bview, row);
1086 if (w < left_margin)
1090 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1095 // returns the minimum space a manual label needs on the screen in pixel
1096 int LyXText::labelFill(BufferView * bview, Row const * row) const
1098 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1099 // -1 because a label ends either with a space that is in the label,
1100 // or with the beginning of a footnote that is outside the label.
1102 // I don't understand this code in depth, but sometimes "last" is
1103 // less than 0 and this causes a crash. This fix seems to work
1104 // correctly, but I bet the real error is elsewhere. The bug is
1105 // triggered when you have an open footnote in a paragraph
1106 // environment with a manual label. (Asger)
1107 if (last < 0) last = 0;
1109 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1114 pos_type i = row->pos();
1116 w += singleWidth(bview, row->par(), i);
1121 if (!row->par()->params().labelWidthString().empty()) {
1122 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1123 getLabelFont(bview->buffer(), row->par())) - w,
1131 // returns the number of separators in the specified row. The separator
1132 // on the very last column doesnt count
1133 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1135 pos_type const last = rowLast(row);
1136 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1138 for (; p < last; ++p) {
1139 if (row->par()->isSeparator(p)) {
1147 // returns the number of hfills in the specified row. The LyX-Hfill is
1148 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1149 // ignored. This is *MUCH* more usefull than not to ignore!
1150 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1152 pos_type const last = rowLast(row);
1153 pos_type first = row->pos();
1155 if (first) { /* hfill *DO* count at the beginning
1157 while (first <= last && row->par()->isHfill(first)) {
1162 first = max(first, beginningOfMainBody(buf, row->par()));
1164 for (pos_type p = first; p <= last; ++p) {
1165 // last, because the end is ignored!
1167 if (row->par()->isHfill(p)) {
1175 // like NumberOfHfills, but only those in the manual label!
1176 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1178 pos_type last = rowLast(row);
1179 pos_type first = row->pos();
1180 if (first) { /* hfill *DO* count at the beginning
1182 while (first < last && row->par()->isHfill(first))
1186 last = min(last, beginningOfMainBody(buf, row->par()));
1188 for (pos_type p = first; p < last; ++p) {
1189 // last, because the end is ignored!
1190 if (row->par()->isHfill(p)) {
1198 // returns true, if a expansion is needed.
1199 // Rules are given by LaTeX
1200 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1203 // by the way, is it a hfill?
1204 if (!row_ptr->par()->isHfill(pos))
1207 // at the end of a row it does not count
1208 // unless another hfill exists on the line
1209 if (pos >= rowLast(row_ptr)) {
1210 pos_type i = row_ptr->pos();
1211 while (i < pos && !row_ptr->par()->isHfill(i)) {
1219 // at the beginning of a row it does not count, if it is not
1220 // the first row of a paragaph
1221 if (!row_ptr->pos())
1224 // in some labels it does not count
1225 if (textclasslist[buf->params.textclass][row_ptr->par()->layout()].margintype
1227 && pos < beginningOfMainBody(buf, row_ptr->par()))
1230 // if there is anything between the first char of the row and
1231 // the sepcified position that is not a newline and not a hfill,
1232 // the hfill will count, otherwise not
1233 pos_type i = row_ptr->pos();
1234 while (i < pos && (row_ptr->par()->isNewline(i)
1235 || row_ptr->par()->isHfill(i)))
1242 LColor::color LyXText::backgroundColor()
1245 return inset_owner->backgroundColor();
1247 return LColor::background;
1250 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1252 /* get the maximum ascent and the maximum descent */
1255 float layoutasc = 0;
1256 float layoutdesc = 0;
1259 Inset * tmpinset = 0;
1261 /* ok , let us initialize the maxasc and maxdesc value.
1262 * This depends in LaTeX of the font of the last character
1263 * in the paragraph. The hack below is necessary because
1264 * of the possibility of open footnotes */
1266 /* Correction: only the fontsize count. The other properties
1267 are taken from the layoutfont. Nicer on the screen :) */
1268 Paragraph * par = row_ptr->par();
1269 Paragraph * firstpar = row_ptr->par();
1271 LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][firstpar->layout()];
1273 // as max get the first character of this row then it can increes but not
1274 // decrees the height. Just some point to start with so we don't have to
1275 // do the assignment below too often.
1276 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1277 LyXFont::FONT_SIZE const tmpsize = font.size();
1278 font = getLayoutFont(bview->buffer(), par);
1279 LyXFont::FONT_SIZE const size = font.size();
1280 font.setSize(tmpsize);
1282 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1284 float spacing_val = 1.0;
1285 if (!row_ptr->par()->params().spacing().isDefault()) {
1286 spacing_val = row_ptr->par()->params().spacing().getValue();
1288 spacing_val = bview->buffer()->params.spacing.getValue();
1290 //lyxerr << "spacing_val = " << spacing_val << endl;
1292 int maxasc = int(lyxfont::maxAscent(font) *
1293 layout.spacing.getValue() *
1295 int maxdesc = int(lyxfont::maxDescent(font) *
1296 layout.spacing.getValue() *
1298 pos_type const pos_end = rowLast(row_ptr);
1302 // Check if any insets are larger
1303 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1304 if (row_ptr->par()->isInset(pos)) {
1305 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1306 tmpinset = row_ptr->par()->getInset(pos);
1308 #if 1 // this is needed for deep update on initialitation
1309 tmpinset->update(bview, tmpfont);
1311 asc = tmpinset->ascent(bview, tmpfont);
1312 desc = tmpinset->descent(bview, tmpfont);
1313 maxwidth += tmpinset->width(bview, tmpfont);
1314 maxasc = max(maxasc, asc);
1315 maxdesc = max(maxdesc, desc);
1318 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1322 // Check if any custom fonts are larger (Asger)
1323 // This is not completely correct, but we can live with the small,
1324 // cosmetic error for now.
1325 LyXFont::FONT_SIZE maxsize =
1326 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1327 if (maxsize > font.size()) {
1328 font.setSize(maxsize);
1330 asc = lyxfont::maxAscent(font);
1331 desc = lyxfont::maxDescent(font);
1338 // This is nicer with box insets:
1342 row_ptr->ascent_of_text(maxasc);
1344 // is it a top line?
1345 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1347 // some parksips VERY EASY IMPLEMENTATION
1348 if (bview->buffer()->params.paragraph_separation ==
1349 BufferParams::PARSEP_SKIP)
1351 if (layout.isParagraph()
1352 && firstpar->getDepth() == 0
1353 && firstpar->previous())
1355 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1356 } else if (firstpar->previous() &&
1357 textclasslist[bview->buffer()->params.textclass][firstpar->previous()->layout()].isParagraph() &&
1358 firstpar->previous()->getDepth() == 0)
1360 // is it right to use defskip here too? (AS)
1361 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1365 // the paper margins
1366 if (!row_ptr->par()->previous() && bv_owner)
1367 maxasc += LYX_PAPER_MARGIN;
1369 // add the vertical spaces, that the user added
1370 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1372 // do not forget the DTP-lines!
1373 // there height depends on the font of the nearest character
1374 if (firstpar->params().lineTop())
1375 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1378 // and now the pagebreaks
1379 if (firstpar->params().pagebreakTop())
1380 maxasc += 3 * defaultHeight();
1382 // This is special code for the chapter, since the label of this
1383 // layout is printed in an extra row
1384 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1385 && bview->buffer()->params.secnumdepth >= 0)
1387 float spacing_val = 1.0;
1388 if (!row_ptr->par()->params().spacing().isDefault()) {
1389 spacing_val = row_ptr->par()->params().spacing().getValue();
1391 spacing_val = bview->buffer()->params.spacing.getValue();
1394 labeladdon = int(lyxfont::maxDescent(labelfont) *
1395 layout.spacing.getValue() *
1397 + int(lyxfont::maxAscent(labelfont) *
1398 layout.spacing.getValue() *
1402 // special code for the top label
1403 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1404 || layout.labeltype == LABEL_BIBLIO
1405 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1406 && row_ptr->par()->isFirstInSequence()
1407 && !row_ptr->par()->getLabelstring().empty())
1409 float spacing_val = 1.0;
1410 if (!row_ptr->par()->params().spacing().isDefault()) {
1411 spacing_val = row_ptr->par()->params().spacing().getValue();
1413 spacing_val = bview->buffer()->params.spacing.getValue();
1417 (lyxfont::maxAscent(labelfont) *
1418 layout.spacing.getValue() *
1420 +(lyxfont::maxDescent(labelfont) *
1421 layout.spacing.getValue() *
1423 + layout.topsep * defaultHeight()
1424 + layout.labelbottomsep * defaultHeight());
1427 // and now the layout spaces, for example before and after a section,
1428 // or between the items of a itemize or enumerate environment
1430 if (!firstpar->params().pagebreakTop()) {
1431 Paragraph * prev = row_ptr->par()->previous();
1433 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1434 if (prev && prev->layout() == firstpar->layout() &&
1435 prev->getDepth() == firstpar->getDepth() &&
1436 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1438 layoutasc = (layout.itemsep * defaultHeight());
1439 } else if (row_ptr->previous()) {
1440 tmptop = layout.topsep;
1442 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1443 tmptop -= textclasslist[bview->buffer()->params.textclass][row_ptr->previous()->par()->layout()].bottomsep;
1446 layoutasc = (tmptop * defaultHeight());
1447 } else if (row_ptr->par()->params().lineTop()) {
1448 tmptop = layout.topsep;
1451 layoutasc = (tmptop * defaultHeight());
1454 prev = row_ptr->par()->outerHook();
1456 maxasc += int(textclasslist[bview->buffer()->params.textclass][prev->layout()].parsep * defaultHeight());
1458 if (firstpar->previous() &&
1459 firstpar->previous()->getDepth() == 0 &&
1460 firstpar->previous()->layout() !=
1464 } else if (firstpar->previous()) {
1465 maxasc += int(layout.parsep * defaultHeight());
1471 // is it a bottom line?
1472 if (row_ptr->par() == par
1473 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1475 // the paper margins
1476 if (!par->next() && bv_owner)
1477 maxdesc += LYX_PAPER_MARGIN;
1479 // add the vertical spaces, that the user added
1480 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1482 // do not forget the DTP-lines!
1483 // there height depends on the font of the nearest character
1484 if (firstpar->params().lineBottom())
1485 maxdesc += 2 * lyxfont::ascent('x',
1486 getFont(bview->buffer(),
1488 max(pos_type(0), par->size() - 1)));
1490 // and now the pagebreaks
1491 if (firstpar->params().pagebreakBottom())
1492 maxdesc += 3 * defaultHeight();
1494 // and now the layout spaces, for example before and after
1495 // a section, or between the items of a itemize or enumerate
1497 if (!firstpar->params().pagebreakBottom()
1498 && row_ptr->par()->next()) {
1499 Paragraph * nextpar = row_ptr->par()->next();
1500 Paragraph * comparepar = row_ptr->par();
1504 if (comparepar->getDepth() > nextpar->getDepth()) {
1505 usual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1506 comparepar = comparepar->depthHook(nextpar->getDepth());
1507 if (comparepar->layout()!= nextpar->layout()
1508 || nextpar->getLabelWidthString() !=
1509 comparepar->getLabelWidthString())
1511 unusual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1513 if (unusual > usual)
1514 layoutdesc = unusual;
1517 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1519 if (comparepar->layout() != nextpar->layout()
1520 || nextpar->getLabelWidthString() !=
1521 comparepar->getLabelWidthString())
1522 layoutdesc = int(textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1527 // incalculate the layout spaces
1528 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1529 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1531 // calculate the new height of the text
1532 height -= row_ptr->height();
1534 row_ptr->height(maxasc + maxdesc + labeladdon);
1535 row_ptr->baseline(maxasc + labeladdon);
1537 height += row_ptr->height();
1539 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1542 row_ptr->width(maxwidth);
1543 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1545 row_ptr->width(int(maxwidth + x));
1548 width = max(0,workWidth(bview));
1550 if (r->width() > width)
1558 /* Appends the implicit specified paragraph behind the specified row,
1559 * start at the implicit given position */
1560 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1562 bool not_ready = true;
1564 // The last character position of a paragraph is an invariant so we can
1565 // safely get it here. (Asger)
1566 pos_type const lastposition = row->par()->size();
1568 // Get the next breakpoint
1569 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1573 // Insert the new row
1574 if (z < lastposition) {
1576 insertRow(row, row->par(), z);
1583 // Set the dimensions of the row
1584 // fixed fill setting now by calling inset->update() in
1585 // SingleWidth when needed!
1586 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1587 setHeightOfRow(bview, tmprow);
1589 } while (not_ready);
1593 void LyXText::breakAgain(BufferView * bview, Row * row) const
1595 bool not_ready = true;
1598 // get the next breakpoint
1599 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1602 if (z < row->par()->size()) {
1603 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1606 insertRow(row, row->par(), z);
1612 if (row->pos() == z)
1613 not_ready = false; // the rest will not change
1619 /* if there are some rows too much, delete them */
1620 /* only if you broke the whole paragraph! */
1621 Row * tmprow2 = row;
1622 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1623 tmprow2 = tmprow2->next();
1625 while (tmprow2 != row) {
1626 tmprow2 = tmprow2->previous();
1627 removeRow(tmprow2->next());
1632 /* set the dimensions of the row */
1633 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1634 setHeightOfRow(bview, tmprow);
1635 } while (not_ready);
1639 // this is just a little changed version of break again
1640 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1642 // get the next breakpoint
1643 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1646 if (z < row->par()->size()) {
1648 || (row->next() && row->next()->par() != row->par())) {
1649 /* insert a new row */
1651 insertRow(row, row->par(), z);
1657 if (row->pos() != z)
1661 // if there are some rows too much, delete them
1662 // only if you broke the whole paragraph!
1663 Row * tmprow2 = row;
1664 while (tmprow2->next()
1665 && tmprow2->next()->par() == row->par()) {
1666 tmprow2 = tmprow2->next();
1668 while (tmprow2 != row) {
1669 tmprow2 = tmprow2->previous();
1670 removeRow(tmprow2->next());
1674 // set the dimensions of the row
1675 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1676 setHeightOfRow(bview, tmprow);
1680 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1682 LyXTextClass const & tclass =
1683 textclasslist[bview->buffer()->params.textclass];
1684 LyXLayout const & layout = tclass[cursor.par()->layout()];
1686 // this is only allowed, if the current paragraph is not empty or caption
1687 if ((cursor.par()->size() <= 0)
1688 && layout.labeltype!= LABEL_SENSITIVE)
1691 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1693 // Always break behind a space
1695 // It is better to erase the space (Dekel)
1696 if (cursor.pos() < cursor.par()->size()
1697 && cursor.par()->isLineSeparator(cursor.pos()))
1698 cursor.par()->erase(cursor.pos());
1699 // cursor.pos(cursor.pos() + 1);
1701 // break the paragraph
1705 keep_layout = layout.isEnvironment();
1706 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1709 // well this is the caption hack since one caption is really enough
1710 if (layout.labeltype == LABEL_SENSITIVE) {
1712 // set to standard-layout
1713 cursor.par()->applyLayout(tclass.defaultLayoutName());
1715 // set to standard-layout
1716 cursor.par()->next()->applyLayout(tclass.defaultLayoutName());
1719 /* if the cursor is at the beginning of a row without prior newline,
1721 * This touches only the screen-update. Otherwise we would may have
1722 * an empty row on the screen */
1723 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1724 && cursor.row()->pos() == cursor.pos()) {
1728 status(bview, LyXText::NEED_MORE_REFRESH);
1729 refresh_row = cursor.row();
1730 refresh_y = cursor.y() - cursor.row()->baseline();
1732 // Do not forget the special right address boxes
1733 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1734 while (refresh_row->previous() &&
1735 refresh_row->previous()->par() == refresh_row->par()) {
1736 refresh_row = refresh_row->previous();
1737 refresh_y -= refresh_row->height();
1740 removeParagraph(cursor.row());
1742 // set the dimensions of the cursor row
1743 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1745 setHeightOfRow(bview, cursor.row());
1747 while (cursor.par()->next()->size()
1748 && cursor.par()->next()->isNewline(0))
1749 cursor.par()->next()->erase(0);
1751 insertParagraph(bview, cursor.par()->next(), cursor.row());
1753 updateCounters(bview, cursor.row()->previous());
1755 /* This check is necessary. Otherwise the new empty paragraph will
1756 * be deleted automatically. And it is more friendly for the user! */
1758 setCursor(bview, cursor.par()->next(), 0);
1760 setCursor(bview, cursor.par(), 0);
1762 if (cursor.row()->next())
1763 breakAgain(bview, cursor.row()->next());
1769 // Just a macro to make some thing easier.
1770 void LyXText::redoParagraph(BufferView * bview) const
1773 redoParagraphs(bview, cursor, cursor.par()->next());
1774 setCursorIntern(bview, cursor.par(), cursor.pos());
1778 /* insert a character, moves all the following breaks in the
1779 * same Paragraph one to the right and make a rebreak */
1780 void LyXText::insertChar(BufferView * bview, char c)
1782 setUndo(bview, Undo::INSERT,
1783 cursor.par(), cursor.par()->next());
1785 // When the free-spacing option is set for the current layout,
1786 // disable the double-space checking
1788 bool const freeSpacing =
1789 textclasslist[bview->buffer()->params.textclass][cursor.row()->par()->layout()].free_spacing ||
1790 cursor.row()->par()->isFreeSpacing();
1792 if (lyxrc.auto_number) {
1793 static string const number_operators = "+-/*";
1794 static string const number_unary_operators = "+-";
1795 static string const number_seperators = ".,:";
1797 if (current_font.number() == LyXFont::ON) {
1798 if (!IsDigit(c) && !contains(number_operators, c) &&
1799 !(contains(number_seperators, c) &&
1800 cursor.pos() >= 1 &&
1801 cursor.pos() < cursor.par()->size() &&
1802 getFont(bview->buffer(),
1804 cursor.pos()).number() == LyXFont::ON &&
1805 getFont(bview->buffer(),
1807 cursor.pos() - 1).number() == LyXFont::ON)
1809 number(bview); // Set current_font.number to OFF
1810 } else if (IsDigit(c) &&
1811 real_current_font.isVisibleRightToLeft()) {
1812 number(bview); // Set current_font.number to ON
1814 if (cursor.pos() > 0) {
1815 char const c = cursor.par()->getChar(cursor.pos() - 1);
1816 if (contains(number_unary_operators, c) &&
1817 (cursor.pos() == 1 ||
1818 cursor.par()->isSeparator(cursor.pos() - 2) ||
1819 cursor.par()->isNewline(cursor.pos() - 2))
1821 setCharFont(bview->buffer(),
1825 } else if (contains(number_seperators, c) &&
1826 cursor.pos() >= 2 &&
1827 getFont(bview->buffer(),
1829 cursor.pos() - 2).number() == LyXFont::ON) {
1830 setCharFont(bview->buffer(),
1840 /* First check, if there will be two blanks together or a blank at
1841 the beginning of a paragraph.
1842 I decided to handle blanks like normal characters, the main
1843 difference are the special checks when calculating the row.fill
1844 (blank does not count at the end of a row) and the check here */
1846 // The bug is triggered when we type in a description environment:
1847 // The current_font is not changed when we go from label to main text
1848 // and it should (along with realtmpfont) when we type the space.
1849 // CHECK There is a bug here! (Asger)
1851 LyXFont realtmpfont = real_current_font;
1852 LyXFont rawtmpfont = current_font; /* store the current font.
1853 * This is because of the use
1854 * of cursor movements. The moving
1855 * cursor would refresh the
1858 // Get the font that is used to calculate the baselineskip
1859 pos_type const lastpos = cursor.par()->size();
1860 LyXFont rawparfont =
1861 cursor.par()->getFontSettings(bview->buffer()->params,
1864 bool jumped_over_space = false;
1866 if (!freeSpacing && IsLineSeparatorChar(c)) {
1867 if ((cursor.pos() > 0
1868 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1869 || (cursor.pos() > 0
1870 && cursor.par()->isNewline(cursor.pos() - 1))
1871 || (cursor.pos() == 0)) {
1872 static bool sent_space_message = false;
1873 if (!sent_space_message) {
1874 if (cursor.pos() == 0)
1875 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1877 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1878 sent_space_message = true;
1883 } else if (IsNewlineChar(c)) {
1884 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1889 /* No newline at first position
1890 * of a paragraph or behind labels.
1891 * TeX does not allow that. */
1893 if (cursor.pos() < cursor.par()->size() &&
1894 cursor.par()->isLineSeparator(cursor.pos()))
1895 // newline always after a blank!
1897 cursor.row()->fill(-1); // to force a new break
1900 // the display inset stuff
1901 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1902 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1903 if (inset && (inset->display() || inset->needFullRow())) {
1904 // force a new break
1905 cursor.row()->fill(-1); // to force a new break
1909 // get the cursor row fist
1910 Row * row = cursor.row();
1911 int y = cursor.y() - row->baseline();
1912 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1913 * already insertet the character */
1914 cursor.par()->insertChar(cursor.pos(), c);
1915 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1917 if (!jumped_over_space) {
1918 // refresh the positions
1920 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1921 tmprow = tmprow->next();
1922 tmprow->pos(tmprow->pos() + 1);
1926 // Is there a break one row above
1927 if ((cursor.par()->isLineSeparator(cursor.pos())
1928 || cursor.par()->isNewline(cursor.pos())
1929 || ((cursor.pos() < cursor.par()->size()) &&
1930 cursor.par()->isInset(cursor.pos()+1))
1931 || cursor.row()->fill() == -1)
1932 && row->previous() && row->previous()->par() == row->par())
1934 pos_type z = nextBreakPoint(bview,
1937 if (z >= row->pos()) {
1940 // set the dimensions of the row above
1941 row->previous()->fill(fill(bview,
1945 setHeightOfRow(bview, row->previous());
1947 y -= row->previous()->height();
1949 refresh_row = row->previous();
1950 status(bview, LyXText::NEED_MORE_REFRESH);
1952 breakAgainOneRow(bview, row);
1954 current_font = rawtmpfont;
1955 real_current_font = realtmpfont;
1956 setCursor(bview, cursor.par(), cursor.pos() + 1,
1957 false, cursor.boundary());
1958 // cursor MUST be in row now.
1960 if (row->next() && row->next()->par() == row->par())
1961 need_break_row = row->next();
1965 // check, wether the last characters font has changed.
1966 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1967 && rawparfont != rawtmpfont)
1968 redoHeightOfParagraph(bview, cursor);
1975 // recalculate the fill of the row
1976 if (row->fill() >= 0) /* needed because a newline
1977 * will set fill to -1. Otherwise
1978 * we would not get a rebreak! */
1979 row->fill(fill(bview, row, workWidth(bview)));
1980 if (row->fill() < 0) {
1983 refresh_x = cursor.x();
1984 refresh_pos = cursor.pos();
1985 status(bview, LyXText::NEED_MORE_REFRESH);
1986 breakAgainOneRow(bview, row);
1987 // will the cursor be in another row now?
1988 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1989 if (row->next() && row->next()->par() == row->par())
1990 // this should always be true
1992 breakAgainOneRow(bview, row);
1994 current_font = rawtmpfont;
1995 real_current_font = realtmpfont;
1997 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1999 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2000 != cursor.boundary())
2001 setCursor(bview, cursor.par(), cursor.pos(), false,
2002 !cursor.boundary());
2003 if (row->next() && row->next()->par() == row->par())
2004 need_break_row = row->next();
2009 refresh_x = cursor.x();
2011 refresh_pos = cursor.pos();
2013 int const tmpheight = row->height();
2014 setHeightOfRow(bview, row);
2015 if (tmpheight == row->height())
2016 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2018 status(bview, LyXText::NEED_MORE_REFRESH);
2020 current_font = rawtmpfont;
2021 real_current_font = realtmpfont;
2022 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2026 // check, wether the last characters font has changed.
2027 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2028 && rawparfont != rawtmpfont) {
2029 redoHeightOfParagraph(bview, cursor);
2031 // now the special right address boxes
2032 if (textclasslist[bview->buffer()->params.textclass][cursor.par()->layout()].margintype
2033 == MARGIN_RIGHT_ADDRESS_BOX) {
2034 redoDrawingOfParagraph(bview, cursor);
2042 void LyXText::charInserted()
2044 // Here we could call FinishUndo for every 20 characters inserted.
2045 // This is from my experience how emacs does it.
2046 static unsigned int counter;
2056 void LyXText::prepareToPrint(BufferView * bview,
2057 Row * row, float & x,
2058 float & fill_separator,
2060 float & fill_label_hfill,
2066 float w = row->fill();
2068 fill_label_hfill = 0;
2070 fill_label_hfill = 0;
2073 row->par()->isRightToLeftPar(bview->buffer()->params);
2075 x = (workWidth(bview) > 0)
2076 ? rightMargin(bview->buffer(), row) : 0;
2078 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2080 // is there a manual margin with a manual label
2081 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2082 LyXLayout const & layout = tclass[row->par()->layout()];
2084 if (layout.margintype == MARGIN_MANUAL
2085 && layout.labeltype == LABEL_MANUAL) {
2086 // one more since labels are left aligned
2087 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2088 if (nlh && !row->par()->getLabelWidthString().empty()) {
2089 fill_label_hfill = labelFill(bview, row) / nlh;
2093 // are there any hfills in the row?
2094 float const nh = numberOfHfills(bview->buffer(), row);
2098 fill_hfill = w / nh;
2099 // we don't have to look at the alignment if it is ALIGN_LEFT and
2100 // if the row is already larger then the permitted width as then
2101 // we force the LEFT_ALIGN'edness!
2102 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2103 // is it block, flushleft or flushright?
2104 // set x how you need it
2106 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2107 align = layout.align;
2109 align = row->par()->params().align();
2112 // center displayed insets
2114 if (row->par()->isInset(row->pos())
2115 && (inset=row->par()->getInset(row->pos()))
2116 && (inset->display())) // || (inset->scroll() < 0)))
2117 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2118 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2121 case LYX_ALIGN_BLOCK:
2122 ns = numberOfSeparators(bview->buffer(), row);
2123 if (ns && row->next() && row->next()->par() == row->par() &&
2124 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2125 && !(row->next()->par()->isInset(row->next()->pos())
2126 && row->next()->par()->getInset(row->next()->pos())
2127 && row->next()->par()->getInset(row->next()->pos())->display())
2130 fill_separator = w / ns;
2131 } else if (is_rtl) {
2135 case LYX_ALIGN_RIGHT:
2138 case LYX_ALIGN_CENTER:
2146 computeBidiTables(bview->buffer(), row);
2148 pos_type main_body =
2149 beginningOfMainBody(bview->buffer(), row->par());
2150 pos_type last = rowLast(row);
2152 if (main_body > 0 &&
2153 (main_body - 1 > last ||
2154 !row->par()->isLineSeparator(main_body - 1))) {
2155 x += lyxfont::width(layout.labelsep,
2156 getLabelFont(bview->buffer(), row->par()));
2157 if (main_body - 1 <= last)
2158 x += fill_label_hfill;
2163 /* important for the screen */
2166 /* the cursor set functions have a special mechanism. When they
2167 * realize, that you left an empty paragraph, they will delete it.
2168 * They also delete the corresponding row */
2170 void LyXText::cursorRightOneWord(BufferView * bview) const
2172 // treat floats, HFills and Insets as words
2173 LyXCursor tmpcursor = cursor;
2174 // CHECK See comment on top of text.C
2176 if (tmpcursor.pos() == tmpcursor.par()->size()
2177 && tmpcursor.par()->next()) {
2178 tmpcursor.par(tmpcursor.par()->next());
2183 // Skip through initial nonword stuff.
2184 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2185 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2186 // printf("Current pos1 %d", tmpcursor.pos()) ;
2187 tmpcursor.pos(tmpcursor.pos() + 1);
2190 // Advance through word.
2191 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2192 tmpcursor.par()->isWord(tmpcursor.pos())) {
2193 // printf("Current pos2 %d", tmpcursor.pos()) ;
2194 tmpcursor.pos(tmpcursor.pos() + 1);
2198 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2202 void LyXText::cursorTab(BufferView * bview) const
2204 LyXCursor tmpcursor = cursor;
2205 while (tmpcursor.pos() < tmpcursor.par()->size()
2206 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2207 tmpcursor.pos(tmpcursor.pos() + 1);
2209 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2210 if (tmpcursor.par()->next()) {
2211 tmpcursor.par(tmpcursor.par()->next());
2215 tmpcursor.pos(tmpcursor.pos() + 1);
2216 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2220 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2221 of prior word, not to end of next prior word. */
2223 void LyXText::cursorLeftOneWord(BufferView * bview) const
2225 LyXCursor tmpcursor = cursor;
2226 cursorLeftOneWord(tmpcursor);
2227 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2230 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2232 // treat HFills, floats and Insets as words
2235 && (cur.par()->isSeparator(cur.pos() - 1)
2236 || cur.par()->isKomma(cur.pos() - 1))
2237 && !(cur.par()->isHfill(cur.pos() - 1)
2238 || cur.par()->isInset(cur.pos() - 1)))
2239 cur.pos(cur.pos() - 1);
2242 && (cur.par()->isInset(cur.pos() - 1)
2243 || cur.par()->isHfill(cur.pos() - 1))) {
2244 cur.pos(cur.pos() - 1);
2245 } else if (!cur.pos()) {
2246 if (cur.par()->previous()) {
2247 cur.par(cur.par()->previous());
2248 cur.pos(cur.par()->size());
2250 } else { // Here, cur != 0
2251 while (cur.pos() > 0 &&
2252 cur.par()->isWord(cur.pos()-1))
2253 cur.pos(cur.pos() - 1);
2257 /* -------> Select current word. This depends on behaviour of
2258 CursorLeftOneWord(), so it is patched as well. */
2259 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2260 word_location const loc) const
2262 // first put the cursor where we wana start to select the word
2265 case WHOLE_WORD_STRICT:
2266 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2267 || cursor.par()->isSeparator(cursor.pos())
2268 || cursor.par()->isKomma(cursor.pos())
2269 || cursor.par()->isSeparator(cursor.pos() -1)
2270 || cursor.par()->isKomma(cursor.pos() -1)) {
2274 // no break here, we go to the next
2277 // Move cursor to the beginning, when not already there.
2278 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2279 && !from.par()->isKomma(from.pos() - 1))
2280 cursorLeftOneWord(from);
2283 // always move the cursor to the beginning of previous word
2284 cursorLeftOneWord(from);
2287 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2293 while (to.pos() < to.par()->size()
2294 && !to.par()->isSeparator(to.pos())
2295 && !to.par()->isKomma(to.pos())
2296 && !to.par()->isHfill(to.pos()))
2298 to.pos(to.pos() + 1);
2303 void LyXText::selectWord(BufferView * bview, word_location const loc)
2307 getWord(from, to, loc);
2309 setCursor(bview, from.par(), from.pos());
2312 selection.cursor = cursor;
2313 setCursor(bview, to.par(), to.pos());
2314 setSelection(bview);
2318 /* -------> Select the word currently under the cursor when no
2319 selection is currently set */
2320 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2321 word_location const loc)
2323 if (!selection.set()) {
2324 selectWord(bview, loc);
2325 return selection.set();
2331 // This function is only used by the spellchecker for NextWord().
2332 // It doesn't handle LYX_ACCENTs and probably never will.
2333 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2334 float & value) const
2336 if (the_locking_inset) {
2337 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2339 value += float(cursor.y())/float(height);
2342 // we have to go on checking so move cusor to the next char
2343 if (cursor.pos() == cursor.par()->size()) {
2344 if (!cursor.par()->next())
2346 cursor.par(cursor.par()->next());
2349 cursor.pos(cursor.pos() + 1);
2351 Paragraph * tmppar = cursor.par();
2353 // If this is not the very first word, skip rest of
2354 // current word because we are probably in the middle
2355 // of a word if there is text here.
2356 if (cursor.pos() || cursor.par()->previous()) {
2357 while (cursor.pos() < cursor.par()->size()
2358 && cursor.par()->isLetter(cursor.pos()))
2359 cursor.pos(cursor.pos() + 1);
2362 // Now, skip until we have real text (will jump paragraphs)
2363 while ((cursor.par()->size() > cursor.pos()
2364 && (!cursor.par()->isLetter(cursor.pos()))
2365 && (!cursor.par()->isInset(cursor.pos()) ||
2366 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2367 || (cursor.par()->size() == cursor.pos()
2368 && cursor.par()->next()))
2370 if (cursor.pos() == cursor.par()->size()) {
2371 cursor.par(cursor.par()->next());
2374 cursor.pos(cursor.pos() + 1);
2377 // now check if we hit an inset so it has to be a inset containing text!
2378 if (cursor.pos() < cursor.par()->size() &&
2379 cursor.par()->isInset(cursor.pos()))
2382 cursor.par()->getInset(cursor.pos())->edit(bview);
2383 // now call us again to do the above trick
2384 // but obviously we have to start from down below ;)
2385 return bview->text->selectNextWordToSpellcheck(bview, value);
2388 // Update the value if we changed paragraphs
2389 if (cursor.par() != tmppar) {
2390 setCursor(bview, cursor.par(), cursor.pos());
2391 value = float(cursor.y())/float(height);
2394 // Start the selection from here
2395 selection.cursor = cursor;
2397 // and find the end of the word (insets like optional hyphens
2398 // and ligature break are part of a word)
2399 while (cursor.pos() < cursor.par()->size()
2400 && (cursor.par()->isLetter(cursor.pos())))
2401 cursor.pos(cursor.pos() + 1);
2403 // Finally, we copy the word to a string and return it
2405 if (selection.cursor.pos() < cursor.pos()) {
2407 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2408 if (!cursor.par()->isInset(i))
2409 str += cursor.par()->getChar(i);
2416 // This one is also only for the spellchecker
2417 void LyXText::selectSelectedWord(BufferView * bview)
2419 if (the_locking_inset) {
2420 the_locking_inset->selectSelectedWord(bview);
2423 // move cursor to the beginning
2424 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2426 // set the sel cursor
2427 selection.cursor = cursor;
2429 // now find the end of the word
2430 while (cursor.pos() < cursor.par()->size()
2431 && (cursor.par()->isLetter(cursor.pos())))
2432 cursor.pos(cursor.pos() + 1);
2434 setCursor(bview, cursor.par(), cursor.pos());
2436 // finally set the selection
2437 setSelection(bview);
2441 /* -------> Delete from cursor up to the end of the current or next word. */
2442 void LyXText::deleteWordForward(BufferView * bview)
2444 if (!cursor.par()->size())
2447 LyXCursor tmpcursor = cursor;
2448 tmpcursor.row(0); //Â ??
2449 selection.set(true); // to avoid deletion
2450 cursorRightOneWord(bview);
2451 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2452 selection.cursor = cursor;
2454 setSelection(bview);
2456 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2457 cutSelection(bview, true, false);
2462 /* -------> Delete from cursor to start of current or prior word. */
2463 void LyXText::deleteWordBackward(BufferView * bview)
2465 if (!cursor.par()->size())
2468 LyXCursor tmpcursor = cursor;
2469 tmpcursor.row(0); // ??
2470 selection.set(true); // to avoid deletion
2471 cursorLeftOneWord(bview);
2472 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2473 selection.cursor = cursor;
2475 setSelection(bview);
2476 cutSelection(bview, true, false);
2481 /* -------> Kill to end of line. */
2482 void LyXText::deleteLineForward(BufferView * bview)
2484 if (!cursor.par()->size())
2485 // Paragraph is empty, so we just go to the right
2488 LyXCursor tmpcursor = cursor;
2489 // We can't store the row over a regular setCursor
2490 // so we set it to 0 and reset it afterwards.
2491 tmpcursor.row(0); //Â ??
2492 selection.set(true); // to avoid deletion
2494 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2495 selection.cursor = cursor;
2497 setSelection(bview);
2498 // What is this test for ??? (JMarc)
2499 if (!selection.set()) {
2500 deleteWordForward(bview);
2502 cutSelection(bview, true, false);
2508 // Change the case of a word at cursor position.
2509 // This function directly manipulates Paragraph::text because there
2510 // is no Paragraph::SetChar currently. I did what I could to ensure
2511 // that it is correct. I guess part of it should be moved to
2512 // Paragraph, but it will have to change for 1.1 anyway. At least
2513 // it does not access outside of the allocated array as the older
2514 // version did. (JMarc)
2515 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2520 if (selection.set()) {
2521 from = selection.start;
2524 getWord(from, to, PARTIAL_WORD);
2525 setCursor(bview, to.par(), to.pos() + 1);
2528 changeRegionCase(bview, from, to, action);
2532 void LyXText::changeRegionCase(BufferView * bview,
2533 LyXCursor const & from,
2534 LyXCursor const & to,
2535 LyXText::TextCase action)
2537 lyx::Assert(from <= to);
2539 setUndo(bview, Undo::FINISH,
2540 from.par(), to.par()->next());
2542 pos_type pos = from.pos();
2543 Paragraph * par = from.par();
2545 while (par && (pos != to.pos() || par != to.par())) {
2546 unsigned char c = par->getChar(pos);
2547 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2549 case text_lowercase:
2552 case text_capitalization:
2554 action = text_lowercase;
2556 case text_uppercase:
2561 par->setChar(pos, c);
2562 checkParagraph(bview, par, pos);
2565 if (pos == par->size()) {
2570 if (to.row() != from.row()) {
2571 refresh_y = from.y() - from.row()->baseline();
2572 refresh_row = from.row();
2573 status(bview, LyXText::NEED_MORE_REFRESH);
2578 void LyXText::transposeChars(BufferView & bview)
2580 Paragraph * tmppar = cursor.par();
2582 setUndo(&bview, Undo::FINISH,
2583 tmppar, tmppar->next());
2585 pos_type tmppos = cursor.pos();
2587 // First decide if it is possible to transpose at all
2589 // We are at the beginning of a paragraph.
2590 if (tmppos == 0) return;
2592 // We are at the end of a paragraph.
2593 if (tmppos == tmppar->size() - 1) return;
2595 unsigned char c1 = tmppar->getChar(tmppos);
2596 unsigned char c2 = tmppar->getChar(tmppos - 1);
2598 if (c1 != Paragraph::META_INSET
2599 && c2 != Paragraph::META_INSET) {
2600 tmppar->setChar(tmppos, c2);
2601 tmppar->setChar(tmppos - 1, c1);
2603 // We should have an implementation that handles insets
2604 // as well, but that will have to come later. (Lgb)
2605 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2609 void LyXText::Delete(BufferView * bview)
2611 // this is a very easy implementation
2613 LyXCursor old_cursor = cursor;
2614 int const old_cur_par_id = old_cursor.par()->id();
2615 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2616 old_cursor.par()->previous()->id() : 0;
2618 // just move to the right
2621 // CHECK Look at the comment here.
2622 // This check is not very good...
2623 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2624 // and that can very well delete the par or par->previous in
2625 // old_cursor. Will a solution where we compare paragraph id's
2627 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2628 == old_cur_par_prev_id
2629 && cursor.par()->id() != old_cur_par_id) {
2630 // delete-empty-paragraph-mechanism has done it
2634 // if you had success make a backspace
2635 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2636 LyXCursor tmpcursor = cursor;
2637 // to make sure undo gets the right cursor position
2638 cursor = old_cursor;
2639 setUndo(bview, Undo::DELETE,
2640 cursor.par(), cursor.par()->next());
2647 void LyXText::backspace(BufferView * bview)
2649 // Get the font that is used to calculate the baselineskip
2650 pos_type lastpos = cursor.par()->size();
2651 LyXFont rawparfont =
2652 cursor.par()->getFontSettings(bview->buffer()->params,
2655 if (cursor.pos() == 0) {
2656 // The cursor is at the beginning of a paragraph,
2657 // so the the backspace will collapse two paragraphs into one.
2659 // we may paste some paragraphs
2661 // is it an empty paragraph?
2664 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2665 // This is an empty paragraph and we delete it just by moving the cursor one step
2666 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2667 // of the paragraph.
2669 if (cursor.par()->previous()) {
2670 Paragraph * tmppar = cursor.par()->previous();
2671 if (cursor.par()->layout() == tmppar->layout()
2672 && cursor.par()->getAlign() == tmppar->getAlign()) {
2673 // Inherit bottom DTD from the paragraph below.
2674 // (the one we are deleting)
2675 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2676 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2677 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2682 // the layout things can change the height of a row !
2683 int const tmpheight = cursor.row()->height();
2684 setHeightOfRow(bview, cursor.row());
2685 if (cursor.row()->height() != tmpheight) {
2686 refresh_y = cursor.y() - cursor.row()->baseline();
2687 refresh_row = cursor.row();
2688 status(bview, LyXText::NEED_MORE_REFRESH);
2694 if (cursor.par()->previous()) {
2695 setUndo(bview, Undo::DELETE,
2696 cursor.par()->previous(), cursor.par()->next());
2699 Paragraph * tmppar = cursor.par();
2700 Row * tmprow = cursor.row();
2702 // We used to do cursorLeftIntern() here, but it is
2703 // not a good idea since it triggers the auto-delete
2704 // mechanism. So we do a cursorLeftIntern()-lite,
2705 // without the dreaded mechanism. (JMarc)
2706 if (cursor.par()->previous()) {
2707 // steps into the above paragraph.
2708 setCursorIntern(bview, cursor.par()->previous(),
2709 cursor.par()->previous()->size(),
2713 /* Pasting is not allowed, if the paragraphs have different
2714 layout. I think it is a real bug of all other
2715 word processors to allow it. It confuses the user.
2716 Even so with a footnote paragraph and a non-footnote
2717 paragraph. I will not allow pasting in this case,
2718 because the user would be confused if the footnote behaves
2719 different wether it is open or closed.
2721 Correction: Pasting is always allowed with standard-layout
2723 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2725 if (cursor.par() != tmppar
2726 && (cursor.par()->layout() == tmppar->layout()
2727 || tmppar->layout() == tclass.defaultLayoutName())
2728 && cursor.par()->getAlign() == tmppar->getAlign()) {
2729 removeParagraph(tmprow);
2731 cursor.par()->pasteParagraph(bview->buffer()->params);
2733 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2734 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2735 // strangely enough it seems that commenting out the line above removes
2736 // most or all of the segfaults. I will however also try to move the
2737 // two Remove... lines in front of the PasteParagraph too.
2740 cursor.pos(cursor.pos() - 1);
2742 status(bview, LyXText::NEED_MORE_REFRESH);
2743 refresh_row = cursor.row();
2744 refresh_y = cursor.y() - cursor.row()->baseline();
2746 // remove the lost paragraph
2747 // This one is not safe, since the paragraph that the tmprow and the
2748 // following rows belong to has been deleted by the PasteParagraph
2749 // above. The question is... could this be moved in front of the
2751 //RemoveParagraph(tmprow);
2752 //RemoveRow(tmprow);
2754 // This rebuilds the rows.
2755 appendParagraph(bview, cursor.row());
2756 updateCounters(bview, cursor.row());
2758 // the row may have changed, block, hfills etc.
2759 setCursor(bview, cursor.par(), cursor.pos(), false);
2762 /* this is the code for a normal backspace, not pasting
2764 setUndo(bview, Undo::DELETE,
2765 cursor.par(), cursor.par()->next());
2766 // We used to do cursorLeftIntern() here, but it is
2767 // not a good idea since it triggers the auto-delete
2768 // mechanism. So we do a cursorLeftIntern()-lite,
2769 // without the dreaded mechanism. (JMarc)
2770 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2771 false, cursor.boundary());
2773 // some insets are undeletable here
2774 if (cursor.par()->isInset(cursor.pos())) {
2775 if (!cursor.par()->getInset(cursor.pos())->deletable())
2777 // force complete redo when erasing display insets
2778 // this is a cruel method but safe..... Matthias
2779 if (cursor.par()->getInset(cursor.pos())->display() ||
2780 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2781 cursor.par()->erase(cursor.pos());
2782 redoParagraph(bview);
2787 Row * row = cursor.row();
2788 int y = cursor.y() - row->baseline();
2790 /* remember that a space at the end of a row doesnt count
2791 * when calculating the fill */
2792 if (cursor.pos() < rowLast(row) ||
2793 !cursor.par()->isLineSeparator(cursor.pos())) {
2794 row->fill(row->fill() + singleWidth(bview,
2799 /* some special code when deleting a newline. This is similar
2800 * to the behavior when pasting paragraphs */
2801 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2802 cursor.par()->erase(cursor.pos());
2803 // refresh the positions
2805 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2806 tmprow = tmprow->next();
2807 tmprow->pos(tmprow->pos() - 1);
2809 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2810 cursor.pos(cursor.pos() - 1);
2812 if (cursor.pos() < cursor.par()->size()
2813 && !cursor.par()->isSeparator(cursor.pos())) {
2814 cursor.par()->insertChar(cursor.pos(), ' ');
2815 setCharFont(bview->buffer(), cursor.par(),
2816 cursor.pos(), current_font);
2817 // refresh the positions
2819 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2820 tmprow = tmprow->next();
2821 tmprow->pos(tmprow->pos() + 1);
2825 cursor.par()->erase(cursor.pos());
2827 // refresh the positions
2829 while (tmprow->next()
2830 && tmprow->next()->par() == row->par()) {
2831 tmprow = tmprow->next();
2832 tmprow->pos(tmprow->pos() - 1);
2835 // delete newlines at the beginning of paragraphs
2836 while (cursor.par()->size() &&
2837 cursor.par()->isNewline(cursor.pos()) &&
2838 cursor.pos() == beginningOfMainBody(bview->buffer(),
2840 cursor.par()->erase(cursor.pos());
2841 // refresh the positions
2843 while (tmprow->next() &&
2844 tmprow->next()->par() == row->par()) {
2845 tmprow = tmprow->next();
2846 tmprow->pos(tmprow->pos() - 1);
2851 // is there a break one row above
2852 if (row->previous() && row->previous()->par() == row->par()) {
2853 z = nextBreakPoint(bview, row->previous(),
2855 if (z >= row->pos()) {
2858 Row * tmprow = row->previous();
2860 // maybe the current row is now empty
2861 if (row->pos() >= row->par()->size()) {
2866 breakAgainOneRow(bview, row);
2867 if (row->next() && row->next()->par() == row->par())
2868 need_break_row = row->next();
2873 // set the dimensions of the row above
2874 y -= tmprow->height();
2875 tmprow->fill(fill(bview, tmprow,
2877 setHeightOfRow(bview, tmprow);
2880 refresh_row = tmprow;
2881 status(bview, LyXText::NEED_MORE_REFRESH);
2882 setCursor(bview, cursor.par(), cursor.pos(),
2883 false, cursor.boundary());
2884 //current_font = rawtmpfont;
2885 //real_current_font = realtmpfont;
2886 // check, whether the last character's font has changed.
2888 cursor.par()->getFontSettings(bview->buffer()->params,
2889 cursor.par()->size() - 1))
2890 redoHeightOfParagraph(bview, cursor);
2895 // break the cursor row again
2896 if (row->next() && row->next()->par() == row->par() &&
2897 (rowLast(row) == row->par()->size() - 1 ||
2898 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2900 /* it can happen that a paragraph loses one row
2901 * without a real breakup. This is when a word
2902 * is to long to be broken. Well, I don t care this
2904 if (rowLast(row) == row->par()->size() - 1)
2905 removeRow(row->next());
2909 status(bview, LyXText::NEED_MORE_REFRESH);
2911 breakAgainOneRow(bview, row);
2912 // will the cursor be in another row now?
2913 if (row->next() && row->next()->par() == row->par() &&
2914 rowLast(row) <= cursor.pos()) {
2916 breakAgainOneRow(bview, row);
2919 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2921 if (row->next() && row->next()->par() == row->par())
2922 need_break_row = row->next();
2926 // set the dimensions of the row
2927 row->fill(fill(bview, row, workWidth(bview)));
2928 int const tmpheight = row->height();
2929 setHeightOfRow(bview, row);
2930 if (tmpheight == row->height())
2931 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2933 status(bview, LyXText::NEED_MORE_REFRESH);
2936 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2940 // current_font = rawtmpfont;
2941 // real_current_font = realtmpfont;
2943 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2944 != cursor.boundary())
2945 setCursor(bview, cursor.par(), cursor.pos(), false,
2946 !cursor.boundary());
2948 lastpos = cursor.par()->size();
2949 if (cursor.pos() == lastpos)
2950 setCurrentFont(bview);
2952 // check, whether the last characters font has changed.
2954 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2955 redoHeightOfParagraph(bview, cursor);
2957 // now the special right address boxes
2959 [bview->buffer()->params.textclass]
2960 [cursor.par()->layout()].margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2961 redoDrawingOfParagraph(bview, cursor);
2967 bool LyXText::paintRowBackground(DrawRowParams & p)
2969 bool clear_area = true;
2971 LyXFont font(LyXFont::ALL_SANE);
2973 pos_type const last = rowLastPrintable(p.row);
2975 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2976 && p.row->par()->isInset(p.row->pos())) {
2977 inset = p.row->par()->getInset(p.row->pos());
2979 clear_area = inset->doClearArea();
2989 int const y = p.yo < 0 ? 0 : p.yo;
2990 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
2991 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
2998 int h = p.row->baseline() - inset->ascent(p.bv, font);
3000 // first clear the whole row above the inset!
3002 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3005 // clear the space below the inset!
3006 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3007 if ((p.row->height() - h) > 0) {
3008 p.pain->fillRectangle(p.xo, p.yo + h,
3009 p.width, p.row->height() - h, backgroundColor());
3012 // clear the space behind the inset, if needed
3013 if (!inset->display() && !inset->needFullRow()) {
3014 int const xp = int(p.x) + inset->width(p.bv, font);
3015 if (p.width - xp > 0) {
3016 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3017 p.row->height(), backgroundColor());
3025 void LyXText::paintRowSelection(DrawRowParams & p)
3027 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3029 // the current selection
3030 int const startx = selection.start.x();
3031 int const endx = selection.end.x();
3032 int const starty = selection.start.y();
3033 int const endy = selection.end.y();
3034 Row const * startrow = selection.start.row();
3035 Row const * endrow = selection.end.row();
3039 if (bidi_same_direction) {
3043 int h = row->height();
3045 if (startrow == row && endrow == row) {
3046 if (startx < endx) {
3049 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3053 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3055 } else if (startrow == row) {
3056 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3057 int const w = (is_rtl) ? startx : (p.width - startx);
3058 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3059 } else if (endrow == row) {
3060 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3061 int const w = (is_rtl) ? (p.width - endx) : endx;
3062 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3063 } else if (p.y > starty && p.y < endy) {
3064 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3067 } else if (startrow != row && endrow != row) {
3069 int h = row->height();
3070 if (p.y > starty && p.y < endy) {
3071 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3076 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3082 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3084 Buffer const * buffer = p.bv->buffer();
3085 Paragraph * par = row->par();
3086 pos_type main_body = beginningOfMainBody(buffer, par);
3087 pos_type const last = rowLastPrintable(row);
3089 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3090 pos_type pos = vis2log(vpos);
3091 float const old_tmpx = tmpx;
3092 if (main_body > 0 && pos == main_body - 1) {
3093 LyXLayout const & layout =
3095 [buffer->params.textclass]
3097 LyXFont const lfont = getLabelFont(buffer, par);
3100 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3102 if (par->isLineSeparator(main_body - 1))
3103 tmpx -= singleWidth(p.bv, par, main_body - 1);
3106 if (hfillExpansion(buffer, row, pos)) {
3107 tmpx += singleWidth(p.bv, par, pos);
3108 if (pos >= main_body)
3111 tmpx += p.label_hfill;
3114 else if (par->isSeparator(pos)) {
3115 tmpx += singleWidth(p.bv, par, pos);
3116 if (pos >= main_body)
3117 tmpx += p.separator;
3119 tmpx += singleWidth(p.bv, par, pos);
3122 if ((startrow != row || selection.start.pos() <= pos) &&
3123 (endrow != row || pos < selection.end.pos())) {
3124 // Here we do not use p.x as p.xo was added to p.x.
3125 p.pain->fillRectangle(int(old_tmpx), p.yo,
3126 int(tmpx - old_tmpx + 1),
3127 row->height(), LColor::selection);
3130 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3131 p.pain->fillRectangle(p.xo + int(tmpx),
3132 p.yo, int(p.bv->workWidth() - tmpx),
3133 row->height(), LColor::selection);
3139 void LyXText::paintRowAppendix(DrawRowParams & p)
3141 // FIXME: can be just p.width ?
3142 int const ww = p.bv->workWidth();
3143 Paragraph * firstpar = p.row->par();
3145 if (firstpar->params().appendix()) {
3146 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3147 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3152 void LyXText::paintRowDepthBar(DrawRowParams & p)
3154 Paragraph::depth_type const depth = p.row->par()->getDepth();
3159 Paragraph::depth_type prev_depth = 0;
3160 if (p.row->previous())
3161 prev_depth = p.row->previous()->par()->getDepth();
3162 Paragraph::depth_type next_depth = 0;
3164 next_depth = p.row->next()->par()->getDepth();
3166 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3167 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3168 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3170 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3172 int const w = LYX_PAPER_MARGIN / 5;
3174 if (i > prev_depth) {
3175 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3177 if (i > next_depth) {
3178 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3184 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3186 int const arrow_size = 4;
3187 int const space_size = int(vsp.inPixels(bv));
3189 if (vsp.kind() != VSpace::LENGTH) {
3195 int const min_size = max(3 * arrow_size,
3196 lyxfont::maxAscent(font)
3197 + lyxfont::maxDescent(font));
3199 if (vsp.length().len().value() < 0.0)
3202 return max(min_size, space_size);
3206 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3207 VSpace const & vsp, int start)
3209 int const arrow_size = 4;
3210 int const size = getLengthMarkerHeight(p.bv, vsp);
3211 int const end = start + size;
3213 // the label to display (if any)
3215 // y-values for top arrow
3217 // y-values for bottom arrow
3219 switch (vsp.kind()) {
3220 case VSpace::LENGTH:
3222 str = prefix + " (" + vsp.asLyXCommand() + ")";
3223 // adding or removing space
3224 bool const added = !(vsp.length().len().value() < 0.0);
3225 ty1 = added ? (start + arrow_size) : start;
3226 ty2 = added ? start : (start + arrow_size);
3227 by1 = added ? (end - arrow_size) : end;
3228 by2 = added ? end : (end - arrow_size);
3231 case VSpace:: VFILL:
3232 str = prefix + " (vertical fill)";
3237 // nothing to draw here
3241 int const leftx = p.xo + leftMargin(p.bv, p.row);
3242 int const midx = leftx + arrow_size;
3243 int const rightx = midx + arrow_size;
3251 font.setColor(LColor::added_space).decSize();
3252 lyxfont::rectText(str, font, w, a, d);
3254 p.pain->rectText(leftx + 2 * arrow_size + 5,
3255 start + ((end - start) / 2) + d,
3261 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3262 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3265 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3266 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3269 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3275 void LyXText::paintFirstRow(DrawRowParams & p)
3277 Paragraph * par = p.row->par();
3278 ParagraphParameters const & parparams = par->params();
3280 // start of appendix?
3281 if (parparams.startOfAppendix()) {
3282 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3287 // think about the margins
3288 if (!p.row->previous() && bv_owner)
3289 y_top += LYX_PAPER_MARGIN;
3291 // draw a top pagebreak
3292 if (parparams.pagebreakTop()) {
3293 int const y = p.yo + y_top + 2*defaultHeight();
3294 p.pain->line(p.xo, y, p.xo + p.width, y,
3295 LColor::pagebreak, Painter::line_onoffdash);
3302 pb_font.setColor(LColor::pagebreak).decSize();
3303 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3304 p.pain->rectText((p.width - w)/2, y + d,
3305 _("Page Break (top)"), pb_font,
3308 y_top += 3 * defaultHeight();
3311 // draw the additional space if needed:
3312 y_top += drawLengthMarker(p, _("Space above"),
3313 parparams.spaceTop(), p.yo + y_top);
3315 Buffer const * buffer = p.bv->buffer();
3317 LyXTextClass const & tclass = textclasslist[buffer->params.textclass];
3318 LyXLayout const & layout = tclass[par->layout()];
3320 // think about the parskip
3321 // some parskips VERY EASY IMPLEMENTATION
3322 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3323 if (par->previous()) {
3324 if (layout.latextype == LATEX_PARAGRAPH
3325 && !par->getDepth()) {
3326 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3328 LyXLayout const & playout =
3329 tclass[par->previous()->layout()];
3330 if (playout.latextype == LATEX_PARAGRAPH
3331 && !par->previous()->getDepth()) {
3332 // is it right to use defskip here, too? (AS)
3333 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3339 int const ww = p.bv->workWidth();
3342 if (parparams.lineTop()) {
3343 LyXFont font(LyXFont::ALL_SANE);
3344 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3348 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3349 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3350 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3351 LColor::topline, Painter::line_solid,
3352 Painter::line_thick);
3357 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3359 // should we print a label?
3360 if (layout.labeltype >= LABEL_STATIC
3361 && (layout.labeltype != LABEL_STATIC
3362 || layout.latextype != LATEX_ENVIRONMENT
3363 || par->isFirstInSequence())) {
3365 LyXFont font = getLabelFont(buffer, par);
3366 if (!par->getLabelstring().empty()) {
3368 string const str = par->getLabelstring();
3370 // this is special code for the chapter layout. This is
3371 // printed in an extra row and has a pagebreak at
3373 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3374 if (buffer->params.secnumdepth >= 0) {
3375 float spacing_val = 1.0;
3376 if (!parparams.spacing().isDefault()) {
3377 spacing_val = parparams.spacing().getValue();
3379 spacing_val = buffer->params.spacing.getValue();
3383 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3384 + int(layout.parsep) * defaultHeight();
3387 x = ww - leftMargin(p.bv, p.row) -
3388 lyxfont::width(str, font);
3391 p.pain->text(int(x),
3392 p.yo + p.row->baseline() -
3393 p.row->ascent_of_text() - maxdesc,
3398 x = ww - leftMargin(p.bv, p.row)
3399 + lyxfont::width(layout.labelsep, font);
3401 x = p.x - lyxfont::width(layout.labelsep, font)
3402 - lyxfont::width(str, font);
3405 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3408 // the labels at the top of an environment.
3409 // More or less for bibliography
3410 } else if (par->isFirstInSequence() &&
3411 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3412 layout.labeltype == LABEL_BIBLIO ||
3413 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3414 LyXFont font = getLabelFont(buffer, par);
3415 if (!par->getLabelstring().empty()) {
3416 string const str = par->getLabelstring();
3417 float spacing_val = 1.0;
3418 if (!parparams.spacing().isDefault()) {
3419 spacing_val = parparams.spacing().getValue();
3421 spacing_val = buffer->params.spacing.getValue();
3425 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3426 + (layout.labelbottomsep * defaultHeight()));
3429 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3430 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3431 + ww - rightMargin(buffer, p.row)) / 2;
3432 x -= lyxfont::width(str, font) / 2;
3433 } else if (is_rtl) {
3434 x = ww - leftMargin(p.bv, p.row) -
3435 lyxfont::width(str, font);
3437 p.pain->text(int(x), p.yo + p.row->baseline()
3438 - p.row->ascent_of_text() - maxdesc,
3443 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3444 LyXFont font = getLayoutFont(buffer, par);
3447 x = ww - leftMargin(p.bv, p.row)
3448 + lyxfont::width(layout.labelsep, font);
3450 x = p.x - lyxfont::width(layout.labelsep, font)
3451 - par->bibkey->width(p.bv, font);
3453 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3458 void LyXText::paintLastRow(DrawRowParams & p)
3460 Paragraph * par = p.row->par();
3461 ParagraphParameters const & parparams = par->params();
3462 int y_bottom = p.row->height() - 1;
3464 // think about the margins
3465 if (!p.row->next() && bv_owner)
3466 y_bottom -= LYX_PAPER_MARGIN;
3468 int const ww = p.bv->workWidth();
3470 // draw a bottom pagebreak
3471 if (parparams.pagebreakBottom()) {
3473 pb_font.setColor(LColor::pagebreak).decSize();
3474 int const y = p.yo + y_bottom - 2 * defaultHeight();
3476 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3477 Painter::line_onoffdash);
3482 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3483 p.pain->rectText((ww - w) / 2, y + d,
3484 _("Page Break (bottom)"),
3485 pb_font, backgroundColor(), backgroundColor());
3487 y_bottom -= 3 * defaultHeight();
3490 // draw the additional space if needed:
3491 int const height = getLengthMarkerHeight(p.bv,
3492 parparams.spaceBottom());
3493 y_bottom -= drawLengthMarker(p, _("Space below"),
3494 parparams.spaceBottom(),
3495 p.yo + y_bottom - height);
3497 Buffer const * buffer = p.bv->buffer();
3499 // draw a bottom line
3500 if (parparams.lineBottom()) {
3501 LyXFont font(LyXFont::ALL_SANE);
3502 int const asc = lyxfont::ascent('x',
3503 getFont(buffer, par,
3504 max(pos_type(0), par->size() - 1)));
3508 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3509 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3510 int const y = p.yo + y_bottom;
3511 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3512 Painter::line_thick);
3517 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3518 int const endlabel = par->getEndLabel(buffer->params);
3523 case END_LABEL_FILLED_BOX:
3525 LyXFont const font = getLabelFont(buffer, par);
3526 int const size = int(0.75 * lyxfont::maxAscent(font));
3527 int const y = (p.yo + p.row->baseline()) - size;
3528 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3530 if (p.row->fill() <= size)
3531 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3533 if (endlabel == END_LABEL_BOX) {
3534 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3536 p.pain->fillRectangle(x, y, size, size,
3541 case END_LABEL_STATIC:
3543 LyXFont font(LyXFont::ALL_SANE);
3544 string const & layout = par->layout();
3545 string const str = textclasslist[buffer->params.textclass][layout].endlabelstring();
3546 font = getLabelFont(buffer, par);
3547 int const x = is_rtl ?
3548 int(p.x) - lyxfont::width(str, font)
3549 : ww - rightMargin(buffer, p.row) - p.row->fill();
3550 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3553 case END_LABEL_NO_LABEL:
3558 void LyXText::paintRowText(DrawRowParams & p)
3560 Paragraph * par = p.row->par();
3561 Buffer const * buffer = p.bv->buffer();
3563 pos_type const last = rowLastPrintable(p.row);
3564 pos_type main_body =
3565 beginningOfMainBody(buffer, par);
3566 if (main_body > 0 &&
3567 (main_body - 1 > last ||
3568 !par->isLineSeparator(main_body - 1))) {
3572 LyXLayout const & layout =
3573 textclasslist[buffer->params.textclass][par->layout()];
3575 pos_type vpos = p.row->pos();
3576 while (vpos <= last) {
3577 pos_type pos = vis2log(vpos);
3578 if (main_body > 0 && pos == main_body - 1) {
3579 int const lwidth = lyxfont::width(layout.labelsep,
3580 getLabelFont(buffer, par));
3582 p.x += p.label_hfill + lwidth
3583 - singleWidth(p.bv, par, main_body - 1);
3586 if (par->isHfill(pos)) {
3589 int const y0 = p.yo + p.row->baseline();
3590 int const y1 = y0 - defaultHeight() / 2;
3592 p.pain->line(int(p.x), y1, int(p.x), y0,
3593 LColor::added_space);
3595 if (hfillExpansion(buffer, p.row, pos)) {
3596 int const y2 = (y0 + y1) / 2;
3598 if (pos >= main_body) {
3599 p.pain->line(int(p.x), y2,
3600 int(p.x + p.hfill), y2,
3601 LColor::added_space,
3602 Painter::line_onoffdash);
3605 p.pain->line(int(p.x), y2,
3606 int(p.x + p.label_hfill), y2,
3607 LColor::added_space,
3608 Painter::line_onoffdash);
3609 p.x += p.label_hfill;
3611 p.pain->line(int(p.x), y1,
3613 LColor::added_space);
3617 } else if (par->isSeparator(pos)) {
3618 p.x += singleWidth(p.bv, par, pos);
3619 if (pos >= main_body)
3629 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3630 Row * row, int y, bool cleared)
3632 if (row->height() <= 0) {
3633 lyxerr << "LYX_ERROR: row.height: "
3634 << row->height() << endl;
3640 // set up drawing parameters
3642 p.pain = &bv->painter();
3646 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3647 if (inset_owner && (p.x < 0))
3651 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3652 p.cleared = cleared;
3656 // clear to background if necessary
3657 p.cleared = paintRowBackground(p);
3659 // paint the selection background
3660 if (selection.set()) {
3661 paintRowSelection(p);
3664 // vertical lines for appendix
3665 paintRowAppendix(p);
3667 // environment depth brackets
3668 paintRowDepthBar(p);
3670 // draw any stuff wanted for a first row of a paragraph
3675 // draw any stuff wanted for the last row of a paragraph
3676 if (!row->next() || (row->next()->par() != row->par())) {
3685 int LyXText::defaultHeight() const
3687 LyXFont font(LyXFont::ALL_SANE);
3688 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3692 /* returns the column near the specified x-coordinate of the row
3693 * x is set to the real beginning of this column */
3695 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3696 bool & boundary) const
3699 float fill_separator;
3701 float fill_label_hfill;
3703 prepareToPrint(bview, row, tmpx, fill_separator,
3704 fill_hfill, fill_label_hfill);
3706 pos_type vc = row->pos();
3707 pos_type last = rowLastPrintable(row);
3709 LyXLayout const & layout =
3710 textclasslist[bview->buffer()->params.textclass][
3711 row->par()->layout()];
3712 bool left_side = false;
3714 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3715 float last_tmpx = tmpx;
3717 if (main_body > 0 &&
3718 (main_body - 1 > last ||
3719 !row->par()->isLineSeparator(main_body - 1)))
3722 while (vc <= last && tmpx <= x) {
3725 if (main_body > 0 && c == main_body-1) {
3726 tmpx += fill_label_hfill +
3727 lyxfont::width(layout.labelsep,
3728 getLabelFont(bview->buffer(), row->par()));
3729 if (row->par()->isLineSeparator(main_body - 1))
3730 tmpx -= singleWidth(bview, row->par(), main_body-1);
3733 if (hfillExpansion(bview->buffer(), row, c)) {
3734 x += singleWidth(bview, row->par(), c);
3738 tmpx += fill_label_hfill;
3740 else if (row->par()->isSeparator(c)) {
3741 tmpx += singleWidth(bview, row->par(), c);
3743 tmpx+= fill_separator;
3745 tmpx += singleWidth(bview, row->par(), c);
3749 if ((tmpx + last_tmpx) / 2 > x) {
3754 if (vc > last + 1) // This shouldn't happen.
3758 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3759 // some speedup if rtl_support=false
3760 && (!row->next() || row->next()->par() != row->par());
3761 bool const rtl = (lastrow)
3762 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3763 : false; // If lastrow is false, we don't need to compute
3764 // the value of rtl.
3766 if (row->pos() > last) // Row is empty?
3769 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3770 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3772 else if (vc == row->pos()) {
3774 if (bidi_level(c) % 2 == 1)
3777 c = vis2log(vc - 1);
3778 bool const rtl = (bidi_level(c) % 2 == 1);
3779 if (left_side == rtl) {
3781 boundary = isBoundary(bview->buffer(), row->par(), c);
3785 if (row->pos() <= last && c > last
3786 && row->par()->isNewline(last)) {
3787 if (bidi_level(last) % 2 == 0)
3788 tmpx -= singleWidth(bview, row->par(), last);
3790 tmpx += singleWidth(bview, row->par(), last);
3800 // returns pointer to a specified row
3801 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3806 Row * tmprow = firstrow;
3809 // find the first row of the specified paragraph
3810 while (tmprow->next() && tmprow->par() != par) {
3811 y += tmprow->height();
3812 tmprow = tmprow->next();
3815 // now find the wanted row
3816 while (tmprow->pos() < pos
3818 && tmprow->next()->par() == par
3819 && tmprow->next()->pos() <= pos) {
3820 y += tmprow->height();
3821 tmprow = tmprow->next();
3828 Row * LyXText::getRowNearY(int & y) const
3831 // If possible we should optimize this method. (Lgb)
3832 Row * tmprow = firstrow;
3835 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3836 tmpy += tmprow->height();
3837 tmprow = tmprow->next();
3840 y = tmpy; // return the real y
3842 //lyxerr << "returned y = " << y << endl;
3846 // Search from the current cursor position.
3848 Row * tmprow = cursor.row();
3849 int tmpy = cursor.y() - tmprow->baseline();
3851 lyxerr << "cursor.y() = " << tmpy << endl;
3852 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3853 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3854 lyxerr << "first = " << first << endl;
3855 lyxerr << "y = " << y << endl;
3858 lyxerr << "up" << endl;
3860 while (tmprow && tmpy - tmprow->height() >= y) {
3861 tmpy -= tmprow->height();
3862 tmprow = tmprow->previous();
3866 tmpy -= tmprow->height();
3867 tmprow = tmprow->previous();
3868 } while (tmprow && tmpy - tmprow->height() >= y);
3870 } else if (y > tmpy) {
3871 lyxerr << "down" << endl;
3873 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3874 tmpy += tmprow->height();
3875 tmprow = tmprow->next();
3878 lyxerr << "equal" << endl;
3881 y = tmpy; // return the real y
3883 lyxerr << "returned y = " << y << endl;
3891 int LyXText::getDepth() const
3893 return cursor.par()->getDepth();