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 * ====================================================== */
16 #include "paragraph.h"
17 #include "lyx_gui_misc.h"
19 #include "bufferparams.h"
28 #include "lyxscreen.h"
29 #include "bufferview_funcs.h"
30 #include "BufferView.h"
32 #include "ParagraphParameters.h"
33 #include "undo_funcs.h"
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
53 int const LYX_PAPER_MARGIN = 20;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
60 int LyXText::workWidth(BufferView * bview) const
63 return inset_owner->textWidth(bview);
65 return bview->workWidth();
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
74 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
76 for (; it != bview->buffer()->inset_iterator_end(); ++it) {
84 return workWidth(bview);
87 LyXLayout const & layout =
88 textclasslist.Style(bview->buffer()->params.textclass,
91 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
92 // Optimization here: in most cases, the real row is
93 // not needed, but only the par/pos values. So we just
94 // construct a dummy row for leftMargin. (JMarc)
98 return workWidth(bview) - leftMargin(bview, &dummyrow);
101 Row * row = getRow(par, pos, dummy_y);
103 while (frow->previous() && frow->par() == frow->previous()->par())
104 frow = frow->previous();
105 unsigned int maxw = 0;
106 while (frow->next() && frow->par() == frow->next()->par()) {
107 if ((frow != row) && (maxw < frow->width()))
108 maxw = frow->width();
114 return workWidth(bview);
118 int LyXText::getRealCursorX(BufferView * bview) const
121 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
122 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
127 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
130 if (!Encodings::is_arabic(c))
131 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
132 return c + (0xb0 - '0');
136 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
137 unsigned char next_char = ' ';
139 for (pos_type i = pos+1; i < par->size(); ++i)
140 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
141 next_char = par->getChar(i);
145 if (Encodings::is_arabic(next_char)) {
146 if (Encodings::is_arabic(prev_char))
147 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
149 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
151 if (Encodings::is_arabic(prev_char))
152 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
154 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
158 // This is the comments that some of the warnings below refers to.
159 // There are some issues in this file and I don't think they are
160 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
161 // this is a problem that has been here almost from day one and that a
162 // larger userbase with differenct access patters triggers the bad
163 // behaviour. (segfaults.) What I think happen is: In several places
164 // we store the paragraph in the current cursor and then moves the
165 // cursor. This movement of the cursor will delete paragraph at the
166 // old position if it is now empty. This will make the temporary
167 // pointer to the old cursor paragraph invalid and dangerous to use.
168 // And is some cases this will trigger a segfault. I have marked some
169 // of the cases where this happens with a warning, but I am sure there
170 // are others in this file and in text2.C. There is also a note in
171 // Delete() that you should read. In Delete I store the paragraph->id
172 // instead of a pointer to the paragraph. I am pretty sure this faulty
173 // use of temporary pointers to paragraphs that might have gotten
174 // invalidated (through a cursor movement) before they are used, are
175 // the cause of the strange crashes we get reported often.
177 // It is very tiresom to change this code, especially when it is as
178 // hard to read as it is. Help to fix all the cases where this is done
179 // would be greately appreciated.
183 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
186 char const c = par->getChar(pos);
187 return singleWidth(bview, par, pos, c);
191 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
192 pos_type pos, char c) const
194 LyXFont const font = getFont(bview->buffer(), par, pos);
196 // The most common case is handled first (Asger)
197 if (IsPrintable(c)) {
198 if (font.language()->RightToLeft()) {
199 if (font.language()->lang() == "arabic" &&
200 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
201 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
202 if (Encodings::IsComposeChar_arabic(c))
205 c = transformChar(c, par, pos);
206 else if (font.language()->lang() == "hebrew" &&
207 Encodings::IsComposeChar_hebrew(c))
210 return lyxfont::width(c, font);
212 } else if (IsHfillChar(c)) {
213 return 3; /* Because of the representation
214 * as vertical lines */
215 } else if (c == Paragraph::META_INSET) {
216 Inset * tmpinset = par->getInset(pos);
219 // this IS needed otherwise on initialitation we don't get the fill
220 // of the row right (ONLY on initialization if we read a file!)
221 // should be changed! (Jug 20011204)
222 tmpinset->update(bview, font);
224 return tmpinset->width(bview, font);
228 } else if (IsSeparatorChar(c))
230 else if (IsNewlineChar(c))
232 return lyxfont::width(c, font);
236 // Returns the paragraph position of the last character in the specified row
237 pos_type LyXText::rowLast(Row const * row) const
239 if (!row->next() || row->next()->par() != row->par())
240 return row->par()->size() - 1;
242 return row->next()->pos() - 1;
246 pos_type LyXText::rowLastPrintable(Row const * row) const
248 pos_type const last = rowLast(row);
249 if (last >= row->pos()
251 && row->next()->par() == row->par()
252 && row->par()->isSeparator(last))
259 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
261 bidi_same_direction = true;
262 if (!lyxrc.rtl_support) {
267 bidi_start = row->pos();
268 bidi_end = rowLastPrintable(row);
270 if (bidi_start > bidi_end) {
275 if (bidi_end + 2 - bidi_start >
276 static_cast<pos_type>(log2vis_list.size())) {
278 (bidi_end + 2 - bidi_start < 500) ?
279 500 : 2 * (bidi_end + 2 - bidi_start);
280 log2vis_list.resize(new_size);
281 vis2log_list.resize(new_size);
282 bidi_levels.resize(new_size);
285 vis2log_list[bidi_end + 1 - bidi_start] = -1;
286 log2vis_list[bidi_end + 1 - bidi_start] = -1;
290 row->par()->getParLanguage(buf->params)->RightToLeft();
294 pos_type const main_body = beginningOfMainBody(buf, row->par());
296 for (pos_type lpos = bidi_start;
297 lpos <= bidi_end; ++lpos) {
298 bool is_space = row->par()->isLineSeparator(lpos);
300 (is_space && lpos + 1 <= bidi_end &&
301 !row->par()->isLineSeparator(lpos + 1) &&
302 !row->par()->isNewline(lpos + 1))
304 LyXFont font = row->par()->getFontSettings(buf->params, pos);
305 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
306 font.number() == LyXFont::ON &&
307 row->par()->getFontSettings(buf->params, lpos - 1).number()
309 font = row->par()->getFontSettings(buf->params, lpos);
314 bool new_rtl = font.isVisibleRightToLeft();
315 bool new_rtl0 = font.isRightToLeft();
318 if (lpos == main_body - 1
319 && row->pos() < main_body - 1
321 new_level = (rtl_par) ? 1 : 0;
322 new_rtl = new_rtl0 = rtl_par;
324 new_level = (new_rtl) ? 1 : 2;
326 new_level = (rtl_par) ? 2 : 0;
328 if (is_space && new_level >= level) {
334 int new_level2 = new_level;
336 if (level == new_level && rtl0 != new_rtl0) {
338 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
339 } else if (level < new_level) {
340 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
341 if (new_level > rtl_par)
342 bidi_same_direction = false;
344 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
347 bidi_levels[lpos - bidi_start] = new_level;
349 while (level > new_level2) {
350 pos_type old_lpos = stack[--level];
351 int delta = lpos - old_lpos - 1;
354 log2vis_list[lpos - bidi_start] += delta;
355 log2vis_list[old_lpos - bidi_start] += delta;
357 while (level < new_level)
358 stack[level++] = lpos;
362 pos_type const old_lpos = stack[--level];
363 int delta = bidi_end - old_lpos;
366 log2vis_list[old_lpos - bidi_start] += delta;
369 pos_type vpos = bidi_start - 1;
370 for (pos_type lpos = bidi_start;
371 lpos <= bidi_end; ++lpos) {
372 vpos += log2vis_list[lpos - bidi_start];
373 vis2log_list[vpos - bidi_start] = lpos;
374 log2vis_list[lpos - bidi_start] = vpos;
379 // This method requires a previous call to ComputeBidiTables()
380 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
383 if (!lyxrc.rtl_support || pos == 0)
386 if (!bidi_InRange(pos - 1)) {
387 /// This can happen if pos is the first char of a row.
388 /// Returning false in this case is incorrect!
392 bool const rtl = bidi_level(pos - 1) % 2;
393 bool const rtl2 = bidi_InRange(pos)
394 ? bidi_level(pos) % 2
395 : par->isRightToLeftPar(buf->params);
400 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
401 pos_type pos, LyXFont const & font) const
403 if (!lyxrc.rtl_support)
404 return false; // This is just for speedup
406 bool const rtl = font.isVisibleRightToLeft();
407 bool const rtl2 = bidi_InRange(pos)
408 ? bidi_level(pos) % 2
409 : par->isRightToLeftPar(buf->params);
413 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
415 // Draw end-of-line marker
416 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
417 int const wid = lyxfont::width('n', font);
418 int const asc = lyxfont::maxAscent(font);
419 int const y = p.yo + p.row->baseline();
423 yp[0] = int(y - 0.875 * asc * 0.75);
424 yp[1] = int(y - 0.500 * asc * 0.75);
425 yp[2] = int(y - 0.125 * asc * 0.75);
427 if (bidi_level(pos) % 2 == 0) {
428 xp[0] = int(p.x + wid * 0.375);
430 xp[2] = int(p.x + wid * 0.375);
432 xp[0] = int(p.x + wid * 0.625);
433 xp[1] = int(p.x + wid);
434 xp[2] = int(p.x + wid * 0.625);
437 p.pain->lines(xp, yp, 3, LColor::eolmarker);
439 yp[0] = int(y - 0.500 * asc * 0.75);
440 yp[1] = int(y - 0.500 * asc * 0.75);
441 yp[2] = int(y - asc * 0.75);
443 if (bidi_level(pos) % 2 == 0) {
445 xp[1] = int(p.x + wid);
446 xp[2] = int(p.x + wid);
448 xp[0] = int(p.x + wid);
453 p.pain->lines(xp, yp, 3, LColor::eolmarker);
459 void LyXText::drawInset(DrawRowParams & p, pos_type const pos)
461 Inset * inset = p.row->par()->getInset(pos);
463 // FIXME: shouldn't happen
468 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
470 inset->update(p.bv, font, false);
471 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
473 if (!need_break_row && !inset_owner
474 && p.bv->text->status() == CHANGED_IN_DRAW) {
475 Row * prev = p.row->previous();
476 if (prev && prev->par() == p.row->par()) {
477 breakAgainOneRow(p.bv, prev);
479 setCursor(p.bv, cursor.par(), cursor.pos());
480 need_break_row = p.row;
485 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
487 if (!lyxrc.mark_foreign_language)
489 if (orig_font.language() == latex_language)
491 if (orig_font.language() == p.bv->buffer()->params.language)
494 int const y = p.yo + p.row->height() - 1;
495 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
499 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
501 pos_type pos = vis2log(vpos);
506 char c = p.row->par()->getChar(pos);
510 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
511 int const width = lyxfont::width(c, font);
514 for (pos_type i = pos-1; i >= 0; --i) {
515 c = p.row->par()->getChar(i);
516 if (!Encodings::IsComposeChar_hebrew(c)) {
517 if (IsPrintableNonspace(c)) {
519 singleWidth(p.bv, p.row->par(), i, c);
521 dx = (c == 'ø' || c == 'ã')
523 : (width2 - width) / 2;
530 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
534 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
536 pos_type pos = vis2log(vpos);
540 char c = p.row->par()->getChar(pos);
541 c = transformChar(c, p.row->par(), pos);
545 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
546 int const width = lyxfont::width(c, font);
549 for (pos_type i = pos-1; i >= 0; --i) {
550 c = p.row->par()->getChar(i);
551 if (!Encodings::IsComposeChar_arabic(c)) {
552 if (IsPrintableNonspace(c)) {
554 singleWidth(p.bv, p.row->par(), i, c);
555 dx = (width2 - width) / 2;
561 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
565 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
566 bool hebrew, bool arabic)
568 pos_type pos = vis2log(vpos);
569 pos_type const last = rowLastPrintable(p.row);
570 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
574 str += p.row->par()->getChar(pos);
576 unsigned char c = str[0];
577 str[0] = transformChar(c, p.row->par(), pos);
581 // collect as much similar chars as we can
582 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
583 char c = p.row->par()->getChar(pos);
585 if (!IsPrintableNonspace(c))
588 if (arabic && Encodings::IsComposeChar_arabic(c))
590 if (hebrew && Encodings::IsComposeChar_hebrew(c))
593 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
600 // Draw text and set the new x position
601 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
602 p.x += lyxfont::width(str, orig_font);
606 void LyXText::draw(DrawRowParams & p, pos_type & vpos)
608 pos_type const pos = vis2log(vpos);
609 Paragraph * par = p.row->par();
611 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
613 float const orig_x = p.x;
615 char const c = par->getChar(pos);
617 if (IsNewlineChar(c)) {
621 } else if (IsInsetChar(c)) {
624 drawForeignMark(p, orig_x, orig_font);
628 // usual characters, no insets
630 // special case languages
631 bool const hebrew = (orig_font.language()->lang() == "hebrew");
633 orig_font.language()->lang() == "arabic" &&
634 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
635 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
637 // draw as many chars as we can
638 if ((!hebrew && !arabic)
639 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
640 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
641 drawChars(p, vpos, true, false);
643 drawHebrewComposeChar(p, vpos);
645 drawArabicComposeChar(p, vpos);
648 drawForeignMark(p, orig_x, orig_font);
650 #ifdef INHERIT_LANGUAGE
652 if ((font.language() == inherit_language) ||
653 (font.language() == ignore_language))
654 lyxerr << "No this shouldn't happen!\n";
660 // Returns the left beginning of the text.
661 // This information cannot be taken from the layouts-objekt, because in
662 // LaTeX the beginning of the text fits in some cases (for example sections)
663 // exactly the label-width.
664 int LyXText::leftMargin(BufferView * bview, Row const * row) const
666 LyXTextClass const & tclass =
667 textclasslist.TextClass(bview->buffer()->params.textclass);
668 LyXLayout const & layout = tclass[row->par()->getLayout()];
670 string parindent = layout.parindent;
672 int x = LYX_PAPER_MARGIN;
674 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
676 // this is the way, LyX handles the LaTeX-Environments.
677 // I have had this idea very late, so it seems to be a
678 // later added hack and this is true
679 if (!row->par()->getDepth()) {
680 if (!row->par()->getLayout()) {
681 // find the previous same level paragraph
682 if (row->par()->previous()) {
683 Paragraph * newpar = row->par()
684 ->depthHook(row->par()->getDepth());
686 tclass[newpar->getLayout()].nextnoindent)
691 // find the next level paragraph
694 row->par()->outerHook();
696 // make a corresponding row. Needed to call LeftMargin()
698 // check wether it is a sufficent paragraph
699 if (newpar && tclass[newpar->getLayout()].isEnvironment())
702 dummyrow.par(newpar);
703 dummyrow.pos(newpar->size());
704 x = leftMargin(bview, &dummyrow);
706 // this is no longer an error, because this function
707 // is used to clear impossible depths after changing
708 // a layout. Since there is always a redo,
709 // LeftMargin() is always called
710 row->par()->params().depth(0);
713 if (newpar && !row->par()->getLayout()) {
714 if (newpar->params().noindent())
717 parindent = tclass[newpar->getLayout()].parindent;
722 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
723 switch (layout.margintype) {
725 if (!layout.leftmargin.empty()) {
726 x += lyxfont::signedWidth(layout.leftmargin,
727 tclass.defaultfont());
729 if (!row->par()->getLabelstring().empty()) {
730 x += lyxfont::signedWidth(layout.labelindent,
732 x += lyxfont::width(row->par()->getLabelstring(),
734 x += lyxfont::width(layout.labelsep, labelfont);
738 x += lyxfont::signedWidth(layout.labelindent, labelfont);
739 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
740 if (!row->par()->getLabelWidthString().empty()) {
741 x += lyxfont::width(row->par()->getLabelWidthString(),
743 x += lyxfont::width(layout.labelsep, labelfont);
748 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
749 / (row->par()->getDepth() + 4);
751 case MARGIN_FIRST_DYNAMIC:
752 if (layout.labeltype == LABEL_MANUAL) {
753 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
754 x += lyxfont::signedWidth(layout.leftmargin,
757 x += lyxfont::signedWidth(layout.labelindent,
760 } else if (row->pos()
761 // Special case to fix problems with
763 || (layout.labeltype == LABEL_STATIC
764 && layout.latextype == LATEX_ENVIRONMENT
765 && ! row->par()->isFirstInSequence())) {
766 x += lyxfont::signedWidth(layout.leftmargin,
768 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
769 && layout.labeltype != LABEL_BIBLIO
770 && layout.labeltype !=
771 LABEL_CENTERED_TOP_ENVIRONMENT) {
772 x += lyxfont::signedWidth(layout.labelindent,
774 x += lyxfont::width(layout.labelsep, labelfont);
775 x += lyxfont::width(row->par()->getLabelstring(),
780 case MARGIN_RIGHT_ADDRESS_BOX:
782 // ok, a terrible hack. The left margin depends on the widest
783 // row in this paragraph. Do not care about footnotes, they
784 // are *NOT* allowed in the LaTeX realisation of this layout.
786 // find the first row of this paragraph
787 Row const * tmprow = row;
788 while (tmprow->previous()
789 && tmprow->previous()->par() == row->par())
790 tmprow = tmprow->previous();
792 int minfill = tmprow->fill();
793 while (tmprow->next() && tmprow->next()->par() == row->par()) {
794 tmprow = tmprow->next();
795 if (tmprow->fill() < minfill)
796 minfill = tmprow->fill();
799 x += lyxfont::signedWidth(layout.leftmargin,
800 tclass.defaultfont());
806 LyXAlignment align; // wrong type
808 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
809 align = layout.align;
811 align = row->par()->params().align();
813 // set the correct parindent
814 if (row->pos() == 0) {
815 if ((layout.labeltype == LABEL_NO_LABEL
816 || layout.labeltype == LABEL_TOP_ENVIRONMENT
817 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
818 || (layout.labeltype == LABEL_STATIC
819 && layout.latextype == LATEX_ENVIRONMENT
820 && ! row->par()->isFirstInSequence()))
821 && align == LYX_ALIGN_BLOCK
822 && !row->par()->params().noindent()
823 && (row->par()->layout ||
824 bview->buffer()->params.paragraph_separation ==
825 BufferParams::PARSEP_INDENT))
826 x += lyxfont::signedWidth(parindent,
827 tclass.defaultfont());
828 else if (layout.labeltype == LABEL_BIBLIO) {
829 // ale970405 Right width for bibitems
830 x += bibitemMaxWidth(bview, tclass.defaultfont());
837 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
839 LyXTextClass const & tclass =
840 textclasslist.TextClass(buf->params.textclass);
841 LyXLayout const & layout = tclass[row->par()->getLayout()];
843 int x = LYX_PAPER_MARGIN
844 + lyxfont::signedWidth(tclass.rightmargin(),
845 tclass.defaultfont());
847 // this is the way, LyX handles the LaTeX-Environments.
848 // I have had this idea very late, so it seems to be a
849 // later added hack and this is true
850 if (row->par()->getDepth()) {
851 // find the next level paragraph
853 Paragraph * newpar = row->par();
856 newpar = newpar->previous();
858 && newpar->getDepth() >= row->par()->getDepth());
860 // make a corresponding row. Needed to call LeftMargin()
862 // check wether it is a sufficent paragraph
864 && tclass[newpar->getLayout()].isEnvironment()) {
866 dummyrow.par(newpar);
868 x = rightMargin(buf, &dummyrow);
870 // this is no longer an error, because this function
871 // is used to clear impossible depths after changing
872 // a layout. Since there is always a redo,
873 // LeftMargin() is always called
874 row->par()->params().depth(0);
878 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
879 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
880 * 4 / (row->par()->getDepth() + 4);
885 int LyXText::labelEnd(BufferView * bview, Row const * row) const
887 if (textclasslist.Style(bview->buffer()->params.textclass,
888 row->par()->getLayout()).margintype
892 tmprow.pos(row->par()->size());
893 return leftMargin(bview, &tmprow); /* just the beginning
896 return 0; /* LabelEnd is only needed, if the
897 layout fills a flushleft
902 // get the next breakpoint in a given paragraph
904 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
906 Paragraph * par = row->par();
911 pos_type const pos = row->pos();
913 // position of the last possible breakpoint
914 // -1 isn't a suitable value, but a flag
915 pos_type last_separator = -1;
916 width -= rightMargin(bview->buffer(), row);
918 pos_type const main_body =
919 beginningOfMainBody(bview->buffer(), par);
920 LyXLayout const & layout =
921 textclasslist.Style(bview->buffer()->params.textclass,
925 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
926 /* special code for right address boxes, only newlines count */
927 while (i < par->size()) {
928 if (par->isNewline(i)) {
930 i = par->size() - 1; // this means break
932 } else if (par->isInset(i) && par->getInset(i)
933 && par->getInset(i)->display()) {
934 par->getInset(i)->display(false);
939 // Last position is an invariant
940 pos_type const last =
942 // this is the usual handling
943 int x = leftMargin(bview, row);
944 bool doitonetime = true;
945 while (doitonetime || ((x < width) && (i < last))) {
947 char const c = par->getChar(i);
948 if (IsNewlineChar(c)) {
950 x = width; // this means break
951 } else if (c == Paragraph::META_INSET &&
954 // check wether a Display() inset is
955 // valid here. if not, change it to
957 if (par->getInset(i)->display() &&
958 (layout.isCommand() ||
959 (layout.labeltype == LABEL_MANUAL
960 && i < beginningOfMainBody(bview->buffer(), par)))) {
961 // display istn't allowd
962 par->getInset(i)->display(false);
963 x += singleWidth(bview, par, i, c);
964 } else if (par->getInset(i)->display() ||
965 par->getInset(i)->needFullRow()) {
966 // So break the line here
970 if (IsLineSeparatorChar(par->getChar(i+1)))
973 last_separator = last; // to avoid extra rows
975 last_separator = i - 1;
976 x = width; // this means break
978 x += singleWidth(bview, par, i, c);
981 if (IsLineSeparatorChar(c))
983 x += singleWidth(bview, par, i, c);
986 if (i == main_body) {
987 x += lyxfont::width(layout.labelsep,
988 getLabelFont(bview->buffer(), par));
989 if (par->isLineSeparator(i - 1))
990 x-= singleWidth(bview, par, i - 1);
991 int left_margin = labelEnd(bview, row);
996 // end of paragraph is always a suitable separator
997 if (i == last && x < width)
1001 // well, if last_separator is still 0, the line isn't breakable.
1002 // don't care and cut simply at the end
1003 if (last_separator < 0) {
1007 // manual labels cannot be broken in LaTeX, do not care
1008 if (main_body && last_separator < main_body)
1009 last_separator = main_body - 1;
1011 return last_separator;
1015 // returns the minimum space a row needs on the screen in pixel
1016 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1018 if (paper_width < 0)
1022 // get the pure distance
1023 pos_type const last = rowLastPrintable(row);
1025 // special handling of the right address boxes
1026 if (textclasslist.Style(bview->buffer()->params.textclass,
1027 row->par()->getLayout()).margintype
1028 == MARGIN_RIGHT_ADDRESS_BOX)
1030 int const tmpfill = row->fill();
1031 row->fill(0); // the minfill in MarginLeft()
1032 w = leftMargin(bview, row);
1035 w = leftMargin(bview, row);
1037 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1038 row->par()->getLayout());
1039 pos_type const main_body =
1040 beginningOfMainBody(bview->buffer(), row->par());
1041 pos_type i = row->pos();
1044 if (main_body > 0 && i == main_body) {
1045 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1046 if (row->par()->isLineSeparator(i - 1))
1047 w -= singleWidth(bview, row->par(), i - 1);
1048 int left_margin = labelEnd(bview, row);
1049 if (w < left_margin)
1052 w += singleWidth(bview, row->par(), i);
1055 if (main_body > 0 && main_body > last) {
1056 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1057 if (last >= 0 && row->par()->isLineSeparator(last))
1058 w -= singleWidth(bview, row->par(), last);
1059 int const left_margin = labelEnd(bview, row);
1060 if (w < left_margin)
1064 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1069 // returns the minimum space a manual label needs on the screen in pixel
1070 int LyXText::labelFill(BufferView * bview, Row const * row) const
1072 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1073 // -1 because a label ends either with a space that is in the label,
1074 // or with the beginning of a footnote that is outside the label.
1076 // I don't understand this code in depth, but sometimes "last" is
1077 // less than 0 and this causes a crash. This fix seems to work
1078 // correctly, but I bet the real error is elsewhere. The bug is
1079 // triggered when you have an open footnote in a paragraph
1080 // environment with a manual label. (Asger)
1081 if (last < 0) last = 0;
1083 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1088 pos_type i = row->pos();
1090 w += singleWidth(bview, row->par(), i);
1095 if (!row->par()->params().labelWidthString().empty()) {
1096 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1097 getLabelFont(bview->buffer(), row->par())) - w,
1105 // returns the number of separators in the specified row. The separator
1106 // on the very last column doesnt count
1107 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1109 pos_type const last = rowLast(row);
1110 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1112 for (; p < last; ++p) {
1113 if (row->par()->isSeparator(p)) {
1121 // returns the number of hfills in the specified row. The LyX-Hfill is
1122 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1123 // ignored. This is *MUCH* more usefull than not to ignore!
1124 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1126 pos_type const last = rowLast(row);
1127 pos_type first = row->pos();
1128 if (first) { /* hfill *DO* count at the beginning
1130 while (first <= last && row->par()->isHfill(first))
1134 first = max(first, beginningOfMainBody(buf, row->par()));
1136 for (pos_type p = first; p <= last; ++p) {
1137 // last, because the end is ignored!
1138 if (row->par()->isHfill(p)) {
1146 // like NumberOfHfills, but only those in the manual label!
1147 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1149 pos_type last = rowLast(row);
1150 pos_type first = row->pos();
1151 if (first) { /* hfill *DO* count at the beginning
1153 while (first < last && row->par()->isHfill(first))
1157 last = min(last, beginningOfMainBody(buf, row->par()));
1159 for (pos_type p = first; p < last; ++p) {
1160 // last, because the end is ignored!
1161 if (row->par()->isHfill(p)) {
1169 // returns true, if a expansion is needed.
1170 // Rules are given by LaTeX
1171 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1174 // by the way, is it a hfill?
1175 if (!row_ptr->par()->isHfill(pos))
1178 // at the end of a row it does not count
1179 // unless another hfill exists on the line
1180 if (pos >= rowLast(row_ptr)) {
1181 pos_type i = row_ptr->pos();
1182 while (i < pos && !row_ptr->par()->isHfill(i)) {
1190 // at the beginning of a row it does not count, if it is not
1191 // the first row of a paragaph
1192 if (!row_ptr->pos())
1195 // in some labels it does not count
1196 if (textclasslist.Style(buf->params.textclass,
1197 row_ptr->par()->getLayout()).margintype
1199 && pos < beginningOfMainBody(buf, row_ptr->par()))
1202 // if there is anything between the first char of the row and
1203 // the sepcified position that is not a newline and not a hfill,
1204 // the hfill will count, otherwise not
1205 pos_type i = row_ptr->pos();
1206 while (i < pos && (row_ptr->par()->isNewline(i)
1207 || row_ptr->par()->isHfill(i)))
1214 LColor::color LyXText::backgroundColor()
1217 return inset_owner->backgroundColor();
1219 return LColor::background;
1222 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1224 /* get the maximum ascent and the maximum descent */
1227 float layoutasc = 0;
1228 float layoutdesc = 0;
1231 Inset * tmpinset = 0;
1233 /* ok , let us initialize the maxasc and maxdesc value.
1234 * This depends in LaTeX of the font of the last character
1235 * in the paragraph. The hack below is necessary because
1236 * of the possibility of open footnotes */
1238 /* Correction: only the fontsize count. The other properties
1239 are taken from the layoutfont. Nicer on the screen :) */
1240 Paragraph * par = row_ptr->par();
1241 Paragraph * firstpar = row_ptr->par();
1243 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1244 firstpar->getLayout());
1246 // as max get the first character of this row then it can increes but not
1247 // decrees the height. Just some point to start with so we don't have to
1248 // do the assignment below too often.
1249 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1250 LyXFont::FONT_SIZE const tmpsize = font.size();
1251 font = getLayoutFont(bview->buffer(), par);
1252 LyXFont::FONT_SIZE const size = font.size();
1253 font.setSize(tmpsize);
1255 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1257 float spacing_val = 1.0;
1258 if (!row_ptr->par()->params().spacing().isDefault()) {
1259 spacing_val = row_ptr->par()->params().spacing().getValue();
1261 spacing_val = bview->buffer()->params.spacing.getValue();
1263 //lyxerr << "spacing_val = " << spacing_val << endl;
1265 int maxasc = int(lyxfont::maxAscent(font) *
1266 layout.spacing.getValue() *
1268 int maxdesc = int(lyxfont::maxDescent(font) *
1269 layout.spacing.getValue() *
1271 pos_type const pos_end = rowLast(row_ptr);
1275 // Check if any insets are larger
1276 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1277 if (row_ptr->par()->isInset(pos)) {
1278 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1279 tmpinset = row_ptr->par()->getInset(pos);
1281 #if 1 // this is needed for deep update on initialitation
1282 tmpinset->update(bview, tmpfont);
1284 asc = tmpinset->ascent(bview, tmpfont);
1285 desc = tmpinset->descent(bview, tmpfont);
1286 maxwidth += tmpinset->width(bview, tmpfont);
1287 maxasc = max(maxasc, asc);
1288 maxdesc = max(maxdesc, desc);
1291 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1295 // Check if any custom fonts are larger (Asger)
1296 // This is not completely correct, but we can live with the small,
1297 // cosmetic error for now.
1298 LyXFont::FONT_SIZE maxsize =
1299 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1300 if (maxsize > font.size()) {
1301 font.setSize(maxsize);
1303 asc = lyxfont::maxAscent(font);
1304 desc = lyxfont::maxDescent(font);
1311 // This is nicer with box insets:
1315 row_ptr->ascent_of_text(maxasc);
1317 // is it a top line?
1318 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1320 // some parksips VERY EASY IMPLEMENTATION
1321 if (bview->buffer()->params.paragraph_separation ==
1322 BufferParams::PARSEP_SKIP)
1324 if (layout.isParagraph()
1325 && firstpar->getDepth() == 0
1326 && firstpar->previous())
1328 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1329 } else if (firstpar->previous() &&
1330 textclasslist.Style(bview->buffer()->params.textclass,
1331 firstpar->previous()->
1332 getLayout()).isParagraph() &&
1333 firstpar->previous()->getDepth() == 0)
1335 // is it right to use defskip here too? (AS)
1336 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1340 // the paper margins
1341 if (!row_ptr->par()->previous() && bv_owner)
1342 maxasc += LYX_PAPER_MARGIN;
1344 // add the vertical spaces, that the user added
1345 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1347 // do not forget the DTP-lines!
1348 // there height depends on the font of the nearest character
1349 if (firstpar->params().lineTop())
1350 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1353 // and now the pagebreaks
1354 if (firstpar->params().pagebreakTop())
1355 maxasc += 3 * defaultHeight();
1357 // This is special code for the chapter, since the label of this
1358 // layout is printed in an extra row
1359 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1360 && bview->buffer()->params.secnumdepth >= 0)
1362 float spacing_val = 1.0;
1363 if (!row_ptr->par()->params().spacing().isDefault()) {
1364 spacing_val = row_ptr->par()->params().spacing().getValue();
1366 spacing_val = bview->buffer()->params.spacing.getValue();
1369 labeladdon = int(lyxfont::maxDescent(labelfont) *
1370 layout.spacing.getValue() *
1372 + int(lyxfont::maxAscent(labelfont) *
1373 layout.spacing.getValue() *
1377 // special code for the top label
1378 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1379 || layout.labeltype == LABEL_BIBLIO
1380 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1381 && row_ptr->par()->isFirstInSequence()
1382 && !row_ptr->par()->getLabelstring().empty())
1384 float spacing_val = 1.0;
1385 if (!row_ptr->par()->params().spacing().isDefault()) {
1386 spacing_val = row_ptr->par()->params().spacing().getValue();
1388 spacing_val = bview->buffer()->params.spacing.getValue();
1392 (lyxfont::maxAscent(labelfont) *
1393 layout.spacing.getValue() *
1395 +(lyxfont::maxDescent(labelfont) *
1396 layout.spacing.getValue() *
1398 + layout.topsep * defaultHeight()
1399 + layout.labelbottomsep * defaultHeight());
1402 // and now the layout spaces, for example before and after a section,
1403 // or between the items of a itemize or enumerate environment
1405 if (!firstpar->params().pagebreakTop()) {
1406 Paragraph * prev = row_ptr->par()->previous();
1408 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1409 if (prev && prev->getLayout() == firstpar->getLayout() &&
1410 prev->getDepth() == firstpar->getDepth() &&
1411 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1413 layoutasc = (layout.itemsep * defaultHeight());
1414 } else if (row_ptr->previous()) {
1415 tmptop = layout.topsep;
1417 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1418 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1419 row_ptr->previous()->par()->
1420 getLayout()).bottomsep;
1423 layoutasc = (tmptop * defaultHeight());
1424 } else if (row_ptr->par()->params().lineTop()) {
1425 tmptop = layout.topsep;
1428 layoutasc = (tmptop * defaultHeight());
1431 prev = row_ptr->par()->outerHook();
1433 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1434 prev->getLayout()).parsep * defaultHeight());
1436 if (firstpar->previous() &&
1437 firstpar->previous()->getDepth() == 0 &&
1438 firstpar->previous()->getLayout() !=
1439 firstpar->getLayout())
1442 } else if (firstpar->previous()) {
1443 maxasc += int(layout.parsep * defaultHeight());
1449 // is it a bottom line?
1450 if (row_ptr->par() == par
1451 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1453 // the paper margins
1454 if (!par->next() && bv_owner)
1455 maxdesc += LYX_PAPER_MARGIN;
1457 // add the vertical spaces, that the user added
1458 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1460 // do not forget the DTP-lines!
1461 // there height depends on the font of the nearest character
1462 if (firstpar->params().lineBottom())
1463 maxdesc += 2 * lyxfont::ascent('x',
1464 getFont(bview->buffer(),
1466 max(pos_type(0), par->size() - 1)));
1468 // and now the pagebreaks
1469 if (firstpar->params().pagebreakBottom())
1470 maxdesc += 3 * defaultHeight();
1472 // and now the layout spaces, for example before and after
1473 // a section, or between the items of a itemize or enumerate
1475 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1476 Paragraph * nextpar = row_ptr->par()->next();
1477 Paragraph * comparepar = row_ptr->par();
1481 if (comparepar->getDepth() > nextpar->getDepth()) {
1482 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1483 comparepar->getLayout()).bottomsep * defaultHeight());
1484 comparepar = comparepar->depthHook(nextpar->getDepth());
1485 if (comparepar->getLayout()!= nextpar->getLayout()
1486 || nextpar->getLabelWidthString() !=
1487 comparepar->getLabelWidthString())
1489 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1490 comparepar->getLayout()).bottomsep * defaultHeight());
1492 if (unusual > usual)
1493 layoutdesc = unusual;
1496 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1498 if (comparepar->getLayout()!= nextpar->getLayout()
1499 || nextpar->getLabelWidthString() !=
1500 comparepar->getLabelWidthString())
1501 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1502 comparepar->getLayout()).bottomsep * defaultHeight());
1507 // incalculate the layout spaces
1508 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1509 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1511 // calculate the new height of the text
1512 height -= row_ptr->height();
1514 row_ptr->height(maxasc + maxdesc + labeladdon);
1515 row_ptr->baseline(maxasc + labeladdon);
1517 height += row_ptr->height();
1519 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1521 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1523 row_ptr->width(int(maxwidth + x));
1526 width = max(0,workWidth(bview));
1528 if (r->width() > width)
1536 /* Appends the implicit specified paragraph behind the specified row,
1537 * start at the implicit given position */
1538 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1540 bool not_ready = true;
1542 // The last character position of a paragraph is an invariant so we can
1543 // safely get it here. (Asger)
1544 pos_type const lastposition = row->par()->size();
1546 // Get the next breakpoint
1547 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1551 // Insert the new row
1552 if (z < lastposition) {
1554 insertRow(row, row->par(), z);
1561 // Set the dimensions of the row
1562 // fixed fill setting now by calling inset->update() in
1563 // SingleWidth when needed!
1564 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1565 setHeightOfRow(bview, tmprow);
1567 } while (not_ready);
1571 void LyXText::breakAgain(BufferView * bview, Row * row) const
1573 bool not_ready = true;
1576 // get the next breakpoint
1577 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1580 if (z < row->par()->size()) {
1581 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1584 insertRow(row, row->par(), z);
1590 if (row->pos() == z)
1591 not_ready = false; // the rest will not change
1597 /* if there are some rows too much, delete them */
1598 /* only if you broke the whole paragraph! */
1599 Row * tmprow2 = row;
1600 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1601 tmprow2 = tmprow2->next();
1603 while (tmprow2 != row) {
1604 tmprow2 = tmprow2->previous();
1605 removeRow(tmprow2->next());
1610 /* set the dimensions of the row */
1611 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1612 setHeightOfRow(bview, tmprow);
1613 } while (not_ready);
1617 // this is just a little changed version of break again
1618 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1620 // get the next breakpoint
1621 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1624 if (z < row->par()->size()) {
1626 || (row->next() && row->next()->par() != row->par())) {
1627 /* insert a new row */
1629 insertRow(row, row->par(), z);
1635 if (row->pos() != z)
1639 // if there are some rows too much, delete them
1640 // only if you broke the whole paragraph!
1641 Row * tmprow2 = row;
1642 while (tmprow2->next()
1643 && tmprow2->next()->par() == row->par()) {
1644 tmprow2 = tmprow2->next();
1646 while (tmprow2 != row) {
1647 tmprow2 = tmprow2->previous();
1648 removeRow(tmprow2->next());
1652 // set the dimensions of the row
1653 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1654 setHeightOfRow(bview, tmprow);
1658 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1660 LyXLayout const & layout =
1661 textclasslist.Style(bview->buffer()->params.textclass,
1662 cursor.par()->getLayout());
1664 // this is only allowed, if the current paragraph is not empty or caption
1665 if ((cursor.par()->size() <= 0)
1666 && layout.labeltype!= LABEL_SENSITIVE)
1669 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1671 // Always break behind a space
1673 // It is better to erase the space (Dekel)
1674 if (cursor.pos() < cursor.par()->size()
1675 && cursor.par()->isLineSeparator(cursor.pos()))
1676 cursor.par()->erase(cursor.pos());
1677 // cursor.pos(cursor.pos() + 1);
1679 // break the paragraph
1683 keep_layout = layout.isEnvironment();
1684 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1687 // well this is the caption hack since one caption is really enough
1688 if (layout.labeltype == LABEL_SENSITIVE) {
1690 // set to standard-layout
1691 cursor.par()->setLayout(0);
1693 // set to standard-layout
1694 cursor.par()->next()->setLayout(0);
1697 /* if the cursor is at the beginning of a row without prior newline,
1699 * This touches only the screen-update. Otherwise we would may have
1700 * an empty row on the screen */
1701 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1702 && cursor.row()->pos() == cursor.pos()) {
1706 status(bview, LyXText::NEED_MORE_REFRESH);
1707 refresh_row = cursor.row();
1708 refresh_y = cursor.y() - cursor.row()->baseline();
1710 // Do not forget the special right address boxes
1711 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1712 while (refresh_row->previous() &&
1713 refresh_row->previous()->par() == refresh_row->par()) {
1714 refresh_row = refresh_row->previous();
1715 refresh_y -= refresh_row->height();
1718 removeParagraph(cursor.row());
1720 // set the dimensions of the cursor row
1721 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1723 setHeightOfRow(bview, cursor.row());
1725 while (cursor.par()->next()->size()
1726 && cursor.par()->next()->isNewline(0))
1727 cursor.par()->next()->erase(0);
1729 insertParagraph(bview, cursor.par()->next(), cursor.row());
1731 updateCounters(bview, cursor.row()->previous());
1733 /* This check is necessary. Otherwise the new empty paragraph will
1734 * be deleted automatically. And it is more friendly for the user! */
1736 setCursor(bview, cursor.par()->next(), 0);
1738 setCursor(bview, cursor.par(), 0);
1740 if (cursor.row()->next())
1741 breakAgain(bview, cursor.row()->next());
1747 // Just a macro to make some thing easier.
1748 void LyXText::redoParagraph(BufferView * bview) const
1751 redoParagraphs(bview, cursor, cursor.par()->next());
1752 setCursorIntern(bview, cursor.par(), cursor.pos());
1756 /* insert a character, moves all the following breaks in the
1757 * same Paragraph one to the right and make a rebreak */
1758 void LyXText::insertChar(BufferView * bview, char c)
1760 setUndo(bview, Undo::INSERT,
1761 cursor.par(), cursor.par()->next());
1763 // When the free-spacing option is set for the current layout,
1764 // disable the double-space checking
1766 bool const freeSpacing =
1767 textclasslist.Style(bview->buffer()->params.textclass,
1768 cursor.row()->par()->getLayout()).free_spacing ||
1769 cursor.row()->par()->isFreeSpacing();
1772 if (lyxrc.auto_number) {
1773 static string const number_operators = "+-/*";
1774 static string const number_unary_operators = "+-";
1775 static string const number_seperators = ".,:";
1777 if (current_font.number() == LyXFont::ON) {
1778 if (!isdigit(c) && !contains(number_operators, c) &&
1779 !(contains(number_seperators, c) &&
1780 cursor.pos() >= 1 &&
1781 cursor.pos() < cursor.par()->size() &&
1782 getFont(bview->buffer(),
1784 cursor.pos()).number() == LyXFont::ON &&
1785 getFont(bview->buffer(),
1787 cursor.pos() - 1).number() == LyXFont::ON)
1789 number(bview); // Set current_font.number to OFF
1790 } else if (isdigit(c) &&
1791 real_current_font.isVisibleRightToLeft()) {
1792 number(bview); // Set current_font.number to ON
1794 if (cursor.pos() > 0) {
1795 char const c = cursor.par()->getChar(cursor.pos() - 1);
1796 if (contains(number_unary_operators, c) &&
1797 (cursor.pos() == 1 ||
1798 cursor.par()->isSeparator(cursor.pos() - 2) ||
1799 cursor.par()->isNewline(cursor.pos() - 2) )
1801 setCharFont(bview->buffer(),
1805 } else if (contains(number_seperators, c) &&
1806 cursor.pos() >= 2 &&
1807 getFont(bview->buffer(),
1809 cursor.pos() - 2).number() == LyXFont::ON) {
1810 setCharFont(bview->buffer(),
1820 /* First check, if there will be two blanks together or a blank at
1821 the beginning of a paragraph.
1822 I decided to handle blanks like normal characters, the main
1823 difference are the special checks when calculating the row.fill
1824 (blank does not count at the end of a row) and the check here */
1826 // The bug is triggered when we type in a description environment:
1827 // The current_font is not changed when we go from label to main text
1828 // and it should (along with realtmpfont) when we type the space.
1829 // CHECK There is a bug here! (Asger)
1831 LyXFont realtmpfont = real_current_font;
1832 LyXFont rawtmpfont = current_font; /* store the current font.
1833 * This is because of the use
1834 * of cursor movements. The moving
1835 * cursor would refresh the
1838 // Get the font that is used to calculate the baselineskip
1839 pos_type const lastpos = cursor.par()->size();
1840 LyXFont rawparfont =
1841 cursor.par()->getFontSettings(bview->buffer()->params,
1844 bool jumped_over_space = false;
1846 if (!freeSpacing && IsLineSeparatorChar(c)) {
1847 if ((cursor.pos() > 0
1848 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1849 || (cursor.pos() > 0
1850 && cursor.par()->isNewline(cursor.pos() - 1))
1851 || (cursor.pos() == 0)) {
1852 static bool sent_space_message = false;
1853 if (!sent_space_message) {
1854 if (cursor.pos() == 0)
1855 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1857 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1858 sent_space_message = true;
1863 } else if (IsNewlineChar(c)) {
1864 if (cursor.par() == cursor.par()
1865 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1869 /* No newline at first position
1870 * of a paragraph or behind labels.
1871 * TeX does not allow that. */
1873 if (cursor.pos() < cursor.par()->size() &&
1874 cursor.par()->isLineSeparator(cursor.pos()))
1875 // newline always after a blank!
1877 cursor.row()->fill(-1); // to force a new break
1880 // the display inset stuff
1881 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1882 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1883 if (inset && (inset->display() || inset->needFullRow())) {
1884 // force a new break
1885 cursor.row()->fill(-1); // to force a new break
1889 // get the cursor row fist
1890 Row * row = cursor.row();
1891 int y = cursor.y() - row->baseline();
1892 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1893 * already insertet the character */
1894 cursor.par()->insertChar(cursor.pos(), c);
1895 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1897 if (!jumped_over_space) {
1898 // refresh the positions
1900 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1901 tmprow = tmprow->next();
1902 tmprow->pos(tmprow->pos() + 1);
1906 // Is there a break one row above
1907 if ((cursor.par()->isLineSeparator(cursor.pos())
1908 || cursor.par()->isNewline(cursor.pos())
1909 || cursor.row()->fill() == -1)
1910 && row->previous() && row->previous()->par() == row->par()) {
1911 pos_type z = nextBreakPoint(bview,
1914 if (z >= row->pos()) {
1917 // set the dimensions of the row above
1918 row->previous()->fill(fill(bview,
1922 setHeightOfRow(bview, row->previous());
1924 y -= row->previous()->height();
1926 refresh_row = row->previous();
1927 status(bview, LyXText::NEED_MORE_REFRESH);
1929 breakAgainOneRow(bview, row);
1931 current_font = rawtmpfont;
1932 real_current_font = realtmpfont;
1933 setCursor(bview, cursor.par(), cursor.pos() + 1,
1934 false, cursor.boundary());
1935 // cursor MUST be in row now.
1937 if (row->next() && row->next()->par() == row->par())
1938 need_break_row = row->next();
1942 // check, wether the last characters font has changed.
1943 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1944 && rawparfont != rawtmpfont)
1945 redoHeightOfParagraph(bview, cursor);
1952 // recalculate the fill of the row
1953 if (row->fill() >= 0) /* needed because a newline
1954 * will set fill to -1. Otherwise
1955 * we would not get a rebreak! */
1956 row->fill(fill(bview, row, workWidth(bview)));
1957 if (row->fill() < 0) {
1960 refresh_x = cursor.x();
1961 refresh_pos = cursor.pos();
1962 status(bview, LyXText::NEED_MORE_REFRESH);
1963 breakAgainOneRow(bview, row);
1964 // will the cursor be in another row now?
1965 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1966 if (row->next() && row->next()->par() == row->par())
1967 // this should always be true
1969 breakAgainOneRow(bview, row);
1971 current_font = rawtmpfont;
1972 real_current_font = realtmpfont;
1974 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1976 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1977 != cursor.boundary())
1978 setCursor(bview, cursor.par(), cursor.pos(), false,
1979 !cursor.boundary());
1980 if (row->next() && row->next()->par() == row->par())
1981 need_break_row = row->next();
1986 refresh_x = cursor.x();
1988 refresh_pos = cursor.pos();
1990 int const tmpheight = row->height();
1991 setHeightOfRow(bview, row);
1992 if (tmpheight == row->height())
1993 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1995 status(bview, LyXText::NEED_MORE_REFRESH);
1997 current_font = rawtmpfont;
1998 real_current_font = realtmpfont;
1999 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2003 // check, wether the last characters font has changed.
2004 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2005 && rawparfont != rawtmpfont) {
2006 redoHeightOfParagraph(bview, cursor);
2008 // now the special right address boxes
2009 if (textclasslist.Style(bview->buffer()->params.textclass,
2010 cursor.par()->getLayout()).margintype
2011 == MARGIN_RIGHT_ADDRESS_BOX) {
2012 redoDrawingOfParagraph(bview, cursor);
2020 void LyXText::charInserted()
2022 // Here we could call FinishUndo for every 20 characters inserted.
2023 // This is from my experience how emacs does it.
2024 static unsigned int counter;
2034 void LyXText::prepareToPrint(BufferView * bview,
2035 Row * row, float & x,
2036 float & fill_separator,
2038 float & fill_label_hfill,
2044 float w = row->fill();
2046 fill_label_hfill = 0;
2048 fill_label_hfill = 0;
2051 row->par()->isRightToLeftPar(bview->buffer()->params);
2053 x = (workWidth(bview) > 0)
2054 ? rightMargin(bview->buffer(), row) : 0;
2056 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2058 // is there a manual margin with a manual label
2059 if (textclasslist.Style(bview->buffer()->params.textclass,
2060 row->par()->getLayout()).margintype == MARGIN_MANUAL
2061 && textclasslist.Style(bview->buffer()->params.textclass,
2062 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2064 /* one more since labels are left aligned */
2065 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2066 if (nlh && !row->par()->getLabelWidthString().empty()) {
2067 fill_label_hfill = labelFill(bview, row) / nlh;
2071 // are there any hfills in the row?
2072 float const nh = numberOfHfills(bview->buffer(), row);
2076 fill_hfill = w / nh;
2078 // is it block, flushleft or flushright?
2079 // set x how you need it
2081 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2082 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2084 align = row->par()->params().align();
2087 // center displayed insets
2089 if (row->par()->isInset(row->pos())
2090 && (inset=row->par()->getInset(row->pos()))
2091 && (inset->display())) // || (inset->scroll() < 0)))
2092 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2093 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2096 case LYX_ALIGN_BLOCK:
2097 ns = numberOfSeparators(bview->buffer(), row);
2098 if (ns && row->next() && row->next()->par() == row->par() &&
2099 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2100 && !(row->next()->par()->isInset(row->next()->pos())
2101 && row->next()->par()->getInset(row->next()->pos())
2102 && row->next()->par()->getInset(row->next()->pos())->display())
2105 fill_separator = w / ns;
2106 } else if (is_rtl) {
2110 case LYX_ALIGN_RIGHT:
2113 case LYX_ALIGN_CENTER:
2121 computeBidiTables(bview->buffer(), row);
2123 pos_type main_body =
2124 beginningOfMainBody(bview->buffer(), row->par());
2125 pos_type last = rowLast(row);
2127 if (main_body > 0 &&
2128 (main_body-1 > last ||
2129 !row->par()->isLineSeparator(main_body-1))) {
2130 LyXLayout const & layout =
2131 textclasslist.Style(bview->buffer()->params.textclass,
2132 row->par()->getLayout());
2133 x += lyxfont::width(layout.labelsep,
2134 getLabelFont(bview->buffer(), row->par()));
2135 if (main_body-1 <= last)
2136 x += fill_label_hfill;
2141 /* important for the screen */
2144 /* the cursor set functions have a special mechanism. When they
2145 * realize, that you left an empty paragraph, they will delete it.
2146 * They also delete the corresponding row */
2148 void LyXText::cursorRightOneWord(BufferView * bview) const
2150 // treat floats, HFills and Insets as words
2151 LyXCursor tmpcursor = cursor;
2152 // CHECK See comment on top of text.C
2154 if (tmpcursor.pos() == tmpcursor.par()->size()
2155 && tmpcursor.par()->next()) {
2156 tmpcursor.par(tmpcursor.par()->next());
2161 // Skip through initial nonword stuff.
2162 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2163 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2164 // printf("Current pos1 %d", tmpcursor.pos()) ;
2165 tmpcursor.pos(tmpcursor.pos() + 1);
2168 // Advance through word.
2169 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2170 tmpcursor.par()->isWord( tmpcursor.pos())) {
2171 // printf("Current pos2 %d", tmpcursor.pos()) ;
2172 tmpcursor.pos(tmpcursor.pos() + 1);
2176 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2180 void LyXText::cursorTab(BufferView * bview) const
2182 LyXCursor tmpcursor = cursor;
2183 while (tmpcursor.pos() < tmpcursor.par()->size()
2184 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2185 tmpcursor.pos(tmpcursor.pos() + 1);
2187 if (tmpcursor.pos() == tmpcursor.par()->size()){
2188 if (tmpcursor.par()->next()) {
2189 tmpcursor.par(tmpcursor.par()->next());
2193 tmpcursor.pos(tmpcursor.pos() + 1);
2194 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2198 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2199 of prior word, not to end of next prior word. */
2201 void LyXText::cursorLeftOneWord(BufferView * bview) const
2203 LyXCursor tmpcursor = cursor;
2204 cursorLeftOneWord(tmpcursor);
2205 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2208 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2210 // treat HFills, floats and Insets as words
2213 && (cur.par()->isSeparator(cur.pos() - 1)
2214 || cur.par()->isKomma(cur.pos() - 1))
2215 && !(cur.par()->isHfill(cur.pos() - 1)
2216 || cur.par()->isInset(cur.pos() - 1)))
2217 cur.pos(cur.pos() - 1);
2220 && (cur.par()->isInset(cur.pos() - 1)
2221 || cur.par()->isHfill(cur.pos() - 1))) {
2222 cur.pos(cur.pos() - 1);
2223 } else if (!cur.pos()) {
2224 if (cur.par()->previous()){
2225 cur.par(cur.par()->previous());
2226 cur.pos(cur.par()->size());
2228 } else { // Here, cur != 0
2229 while (cur.pos() > 0 &&
2230 cur.par()->isWord(cur.pos()-1) )
2231 cur.pos(cur.pos() - 1);
2235 /* -------> Select current word. This depends on behaviour of
2236 CursorLeftOneWord(), so it is patched as well. */
2237 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2238 word_location const loc) const
2240 // first put the cursor where we wana start to select the word
2243 case WHOLE_WORD_STRICT:
2244 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2245 || cursor.par()->isSeparator(cursor.pos())
2246 || cursor.par()->isKomma(cursor.pos())
2247 || cursor.par()->isSeparator(cursor.pos() -1)
2248 || cursor.par()->isKomma(cursor.pos() -1)) {
2252 // no break here, we go to the next
2255 // Move cursor to the beginning, when not already there.
2256 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2257 && !from.par()->isKomma(from.pos() - 1))
2258 cursorLeftOneWord(from);
2261 // always move the cursor to the beginning of previous word
2262 cursorLeftOneWord(from);
2265 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2271 while (to.pos() < to.par()->size()
2272 && !to.par()->isSeparator(to.pos())
2273 && !to.par()->isKomma(to.pos())
2274 && !to.par()->isHfill(to.pos()) )
2276 to.pos(to.pos() + 1);
2281 void LyXText::selectWord(BufferView * bview, word_location const loc)
2285 getWord(from, to, loc);
2287 setCursor(bview, from.par(), from.pos());
2290 selection.cursor = cursor;
2291 setCursor(bview, to.par(), to.pos() );
2292 setSelection(bview);
2296 /* -------> Select the word currently under the cursor when no
2297 selection is currently set */
2298 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2299 word_location const loc)
2301 if (!selection.set()) {
2302 selectWord(bview, loc);
2303 return selection.set();
2309 // This function is only used by the spellchecker for NextWord().
2310 // It doesn't handle LYX_ACCENTs and probably never will.
2311 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2312 float & value) const
2314 if (the_locking_inset) {
2315 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2317 value += float(cursor.y())/float(height);
2320 #warning Dekel please have a look on this one RTL? (Jug)
2322 // we have to go on checking so move cusor to the right
2323 if (cursor.pos() == cursor.par()->size()) {
2324 if (!cursor.par()->next())
2326 cursor.par(cursor.par()->next());
2329 cursor.pos(cursor.pos() + 1);
2331 Paragraph * tmppar = cursor.par();
2333 // If this is not the very first word, skip rest of
2334 // current word because we are probably in the middle
2335 // of a word if there is text here.
2336 if (cursor.pos() || cursor.par()->previous()) {
2337 while (cursor.pos() < cursor.par()->size()
2338 && cursor.par()->isLetter(cursor.pos()))
2339 cursor.pos(cursor.pos() + 1);
2342 // Now, skip until we have real text (will jump paragraphs)
2343 while ((cursor.par()->size() > cursor.pos()
2344 && (!cursor.par()->isLetter(cursor.pos()))
2345 && (!cursor.par()->isInset(cursor.pos()) ||
2346 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2347 || (cursor.par()->size() == cursor.pos()
2348 && cursor.par()->next()))
2350 if (cursor.pos() == cursor.par()->size()) {
2351 cursor.par(cursor.par()->next());
2354 cursor.pos(cursor.pos() + 1);
2357 // now check if we hit an inset so it has to be a inset containing text!
2358 if (cursor.pos() < cursor.par()->size() &&
2359 cursor.par()->isInset(cursor.pos()))
2362 cursor.par()->getInset(cursor.pos())->edit(bview);
2363 // now call us again to do the above trick
2364 // but obviously we have to start from down below ;)
2365 return bview->text->selectNextWordToSpellcheck(bview, value);
2368 // Update the value if we changed paragraphs
2369 if (cursor.par() != tmppar){
2370 setCursor(bview, cursor.par(), cursor.pos());
2371 value = float(cursor.y())/float(height);
2374 // Start the selection from here
2375 selection.cursor = cursor;
2377 // and find the end of the word (insets like optional hyphens
2378 // and ligature break are part of a word)
2379 while (cursor.pos() < cursor.par()->size()
2380 && (cursor.par()->isLetter(cursor.pos())))
2381 cursor.pos(cursor.pos() + 1);
2383 // Finally, we copy the word to a string and return it
2385 if (selection.cursor.pos() < cursor.pos()) {
2387 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2388 if (!cursor.par()->isInset(i))
2389 str += cursor.par()->getChar(i);
2396 // This one is also only for the spellchecker
2397 void LyXText::selectSelectedWord(BufferView * bview)
2399 if (the_locking_inset) {
2400 the_locking_inset->selectSelectedWord(bview);
2403 // move cursor to the beginning
2404 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2406 // set the sel cursor
2407 selection.cursor = cursor;
2409 // now find the end of the word
2410 while (cursor.pos() < cursor.par()->size()
2411 && (cursor.par()->isLetter(cursor.pos())))
2412 cursor.pos(cursor.pos() + 1);
2414 setCursor(bview, cursor.par(), cursor.pos());
2416 // finally set the selection
2417 setSelection(bview);
2421 /* -------> Delete from cursor up to the end of the current or next word. */
2422 void LyXText::deleteWordForward(BufferView * bview)
2424 if (!cursor.par()->size())
2427 LyXCursor tmpcursor = cursor;
2428 tmpcursor.row(0); //Â ??
2429 selection.set(true); // to avoid deletion
2430 cursorRightOneWord(bview);
2431 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2432 selection.cursor = cursor;
2434 setSelection(bview);
2436 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2437 cutSelection(bview, true, false);
2442 /* -------> Delete from cursor to start of current or prior word. */
2443 void LyXText::deleteWordBackward(BufferView * bview)
2445 if (!cursor.par()->size())
2448 LyXCursor tmpcursor = cursor;
2449 tmpcursor.row(0); // ??
2450 selection.set(true); // to avoid deletion
2451 cursorLeftOneWord(bview);
2452 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2453 selection.cursor = cursor;
2455 setSelection(bview);
2456 cutSelection(bview, true, false);
2461 /* -------> Kill to end of line. */
2462 void LyXText::deleteLineForward(BufferView * bview)
2464 if (!cursor.par()->size())
2465 // Paragraph is empty, so we just go to the right
2468 LyXCursor tmpcursor = cursor;
2469 // We can't store the row over a regular setCursor
2470 // so we set it to 0 and reset it afterwards.
2471 tmpcursor.row(0); //Â ??
2472 selection.set(true); // to avoid deletion
2474 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2475 selection.cursor = cursor;
2477 setSelection(bview);
2478 // What is this test for ??? (JMarc)
2479 if (!selection.set()) {
2480 deleteWordForward(bview);
2482 cutSelection(bview, true, false);
2488 // Change the case of a word at cursor position.
2489 // This function directly manipulates Paragraph::text because there
2490 // is no Paragraph::SetChar currently. I did what I could to ensure
2491 // that it is correct. I guess part of it should be moved to
2492 // Paragraph, but it will have to change for 1.1 anyway. At least
2493 // it does not access outside of the allocated array as the older
2494 // version did. (JMarc)
2495 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2500 if (selection.set()) {
2501 from = selection.start;
2504 getWord(from, to, PARTIAL_WORD);
2505 setCursor(bview, to.par(), to.pos() + 1);
2508 changeRegionCase(bview, from, to, action);
2512 void LyXText::changeRegionCase(BufferView * bview,
2513 LyXCursor const & from,
2514 LyXCursor const & to,
2515 LyXText::TextCase action)
2517 lyx::Assert(from <= to);
2519 setUndo(bview, Undo::FINISH,
2520 from.par(), to.par()->next());
2522 pos_type pos = from.pos();
2523 Paragraph * par = from.par();
2525 while (par && (pos != to.pos() || par != to.par())) {
2526 unsigned char c = par->getChar(pos);
2527 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2529 case text_lowercase:
2532 case text_capitalization:
2534 action = text_lowercase;
2536 case text_uppercase:
2541 par->setChar(pos, c);
2542 checkParagraph(bview, par, pos);
2545 if (pos == par->size()) {
2550 if (to.row() != from.row()) {
2551 refresh_y = from.y() - from.row()->baseline();
2552 refresh_row = from.row();
2553 status(bview, LyXText::NEED_MORE_REFRESH);
2558 void LyXText::transposeChars(BufferView & bview)
2560 Paragraph * tmppar = cursor.par();
2562 setUndo(&bview, Undo::FINISH,
2563 tmppar, tmppar->next());
2565 pos_type tmppos = cursor.pos();
2567 // First decide if it is possible to transpose at all
2569 // We are at the beginning of a paragraph.
2570 if (tmppos == 0) return;
2572 // We are at the end of a paragraph.
2573 if (tmppos == tmppar->size() - 1) return;
2575 unsigned char c1 = tmppar->getChar(tmppos);
2576 unsigned char c2 = tmppar->getChar(tmppos - 1);
2578 if (c1 != Paragraph::META_INSET
2579 && c2 != Paragraph::META_INSET) {
2580 tmppar->setChar(tmppos, c2);
2581 tmppar->setChar(tmppos - 1, c1);
2583 // We should have an implementation that handles insets
2584 // as well, but that will have to come later. (Lgb)
2585 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2589 void LyXText::Delete(BufferView * bview)
2591 // this is a very easy implementation
2593 LyXCursor old_cursor = cursor;
2594 int const old_cur_par_id = old_cursor.par()->id();
2595 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2596 old_cursor.par()->previous()->id() : 0;
2598 // just move to the right
2601 // CHECK Look at the comment here.
2602 // This check is not very good...
2603 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2604 // and that can very well delete the par or par->previous in
2605 // old_cursor. Will a solution where we compare paragraph id's
2607 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2608 == old_cur_par_prev_id
2609 && cursor.par()->id() != old_cur_par_id) {
2610 // delete-empty-paragraph-mechanism has done it
2614 // if you had success make a backspace
2615 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2616 LyXCursor tmpcursor = cursor;
2617 // to make sure undo gets the right cursor position
2618 cursor = old_cursor;
2619 setUndo(bview, Undo::DELETE,
2620 cursor.par(), cursor.par()->next());
2627 void LyXText::backspace(BufferView * bview)
2629 // Get the font that is used to calculate the baselineskip
2630 pos_type lastpos = cursor.par()->size();
2631 LyXFont rawparfont =
2632 cursor.par()->getFontSettings(bview->buffer()->params,
2635 if (cursor.pos() == 0) {
2636 // The cursor is at the beginning of a paragraph,
2637 // so the the backspace will collapse two paragraphs into one.
2639 // we may paste some paragraphs
2641 // is it an empty paragraph?
2644 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2645 // This is an empty paragraph and we delete it just by moving the cursor one step
2646 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2647 // of the paragraph.
2649 if (cursor.par()->previous()) {
2650 Paragraph * tmppar = cursor.par()->previous();
2651 if (cursor.par()->getLayout() == tmppar->getLayout()
2652 && cursor.par()->getAlign() == tmppar->getAlign()) {
2653 // Inherit bottom DTD from the paragraph below.
2654 // (the one we are deleting)
2655 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2656 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2657 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2662 // the layout things can change the height of a row !
2663 int const tmpheight = cursor.row()->height();
2664 setHeightOfRow(bview, cursor.row());
2665 if (cursor.row()->height() != tmpheight) {
2666 refresh_y = cursor.y() - cursor.row()->baseline();
2667 refresh_row = cursor.row();
2668 status(bview, LyXText::NEED_MORE_REFRESH);
2674 if (cursor.par()->previous()) {
2675 setUndo(bview, Undo::DELETE,
2676 cursor.par()->previous(), cursor.par()->next());
2679 Paragraph * tmppar = cursor.par();
2680 Row * tmprow = cursor.row();
2682 // We used to do cursorLeftIntern() here, but it is
2683 // not a good idea since it triggers the auto-delete
2684 // mechanism. So we do a cursorLeftIntern()-lite,
2685 // without the dreaded mechanism. (JMarc)
2686 if (cursor.par()->previous()) {
2687 // steps into the above paragraph.
2688 setCursorIntern(bview, cursor.par()->previous(),
2689 cursor.par()->previous()->size(),
2693 /* Pasting is not allowed, if the paragraphs have different
2694 layout. I think it is a real bug of all other
2695 word processors to allow it. It confuses the user.
2696 Even so with a footnote paragraph and a non-footnote
2697 paragraph. I will not allow pasting in this case,
2698 because the user would be confused if the footnote behaves
2699 different wether it is open or closed.
2701 Correction: Pasting is always allowed with standard-layout
2703 if (cursor.par() != tmppar
2704 && (cursor.par()->getLayout() == tmppar->getLayout()
2705 || tmppar->getLayout() == 0 /*standard*/)
2706 && cursor.par()->getAlign() == tmppar->getAlign())
2708 removeParagraph(tmprow);
2710 cursor.par()->pasteParagraph(bview->buffer()->params);
2712 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2713 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2714 // strangely enough it seems that commenting out the line above removes
2715 // most or all of the segfaults. I will however also try to move the
2716 // two Remove... lines in front of the PasteParagraph too.
2719 cursor.pos(cursor.pos() - 1);
2721 status(bview, LyXText::NEED_MORE_REFRESH);
2722 refresh_row = cursor.row();
2723 refresh_y = cursor.y() - cursor.row()->baseline();
2725 // remove the lost paragraph
2726 // This one is not safe, since the paragraph that the tmprow and the
2727 // following rows belong to has been deleted by the PasteParagraph
2728 // above. The question is... could this be moved in front of the
2730 //RemoveParagraph(tmprow);
2731 //RemoveRow(tmprow);
2733 // This rebuilds the rows.
2734 appendParagraph(bview, cursor.row());
2735 updateCounters(bview, cursor.row());
2737 // the row may have changed, block, hfills etc.
2738 setCursor(bview, cursor.par(), cursor.pos(), false);
2741 /* this is the code for a normal backspace, not pasting
2743 setUndo(bview, Undo::DELETE,
2744 cursor.par(), cursor.par()->next());
2745 // We used to do cursorLeftIntern() here, but it is
2746 // not a good idea since it triggers the auto-delete
2747 // mechanism. So we do a cursorLeftIntern()-lite,
2748 // without the dreaded mechanism. (JMarc)
2749 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2750 false, cursor.boundary());
2752 // some insets are undeletable here
2753 if (cursor.par()->isInset(cursor.pos())) {
2754 if (!cursor.par()->getInset(cursor.pos())->deletable())
2756 // force complete redo when erasing display insets
2757 // this is a cruel method but safe..... Matthias
2758 if (cursor.par()->getInset(cursor.pos())->display() ||
2759 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2760 cursor.par()->erase(cursor.pos());
2761 redoParagraph(bview);
2766 Row * row = cursor.row();
2767 int y = cursor.y() - row->baseline();
2769 /* remember that a space at the end of a row doesnt count
2770 * when calculating the fill */
2771 if (cursor.pos() < rowLast(row) ||
2772 !cursor.par()->isLineSeparator(cursor.pos())) {
2773 row->fill(row->fill() + singleWidth(bview,
2778 /* some special code when deleting a newline. This is similar
2779 * to the behavior when pasting paragraphs */
2780 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2781 cursor.par()->erase(cursor.pos());
2782 // refresh the positions
2784 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2785 tmprow = tmprow->next();
2786 tmprow->pos(tmprow->pos() - 1);
2788 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2789 cursor.pos(cursor.pos() - 1);
2791 if (cursor.pos() < cursor.par()->size()
2792 && !cursor.par()->isSeparator(cursor.pos())) {
2793 cursor.par()->insertChar(cursor.pos(), ' ');
2794 setCharFont(bview->buffer(), cursor.par(),
2795 cursor.pos(), current_font);
2796 // refresh the positions
2798 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2799 tmprow = tmprow->next();
2800 tmprow->pos(tmprow->pos() + 1);
2804 cursor.par()->erase(cursor.pos());
2806 // refresh the positions
2808 while (tmprow->next()
2809 && tmprow->next()->par() == row->par()) {
2810 tmprow = tmprow->next();
2811 tmprow->pos(tmprow->pos() - 1);
2814 // delete newlines at the beginning of paragraphs
2815 while (cursor.par()->size() &&
2816 cursor.par()->isNewline(cursor.pos()) &&
2817 cursor.pos() == beginningOfMainBody(bview->buffer(),
2819 cursor.par()->erase(cursor.pos());
2820 // refresh the positions
2822 while (tmprow->next() &&
2823 tmprow->next()->par() == row->par()) {
2824 tmprow = tmprow->next();
2825 tmprow->pos(tmprow->pos() - 1);
2830 // is there a break one row above
2831 if (row->previous() && row->previous()->par() == row->par()) {
2832 z = nextBreakPoint(bview, row->previous(),
2834 if (z >= row->pos()) {
2837 Row * tmprow = row->previous();
2839 // maybe the current row is now empty
2840 if (row->pos() >= row->par()->size()) {
2845 breakAgainOneRow(bview, row);
2846 if (row->next() && row->next()->par() == row->par())
2847 need_break_row = row->next();
2852 // set the dimensions of the row above
2853 y -= tmprow->height();
2854 tmprow->fill(fill(bview, tmprow,
2856 setHeightOfRow(bview, tmprow);
2859 refresh_row = tmprow;
2860 status(bview, LyXText::NEED_MORE_REFRESH);
2861 setCursor(bview, cursor.par(), cursor.pos(),
2862 false, cursor.boundary());
2863 //current_font = rawtmpfont;
2864 //real_current_font = realtmpfont;
2865 // check, whether the last character's font has changed.
2867 cursor.par()->getFontSettings(bview->buffer()->params,
2868 cursor.par()->size() - 1))
2869 redoHeightOfParagraph(bview, cursor);
2874 // break the cursor row again
2875 if (row->next() && row->next()->par() == row->par() &&
2876 (rowLast(row) == row->par()->size() - 1 ||
2877 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2879 /* it can happen that a paragraph loses one row
2880 * without a real breakup. This is when a word
2881 * is to long to be broken. Well, I don t care this
2883 if (rowLast(row) == row->par()->size() - 1)
2884 removeRow(row->next());
2888 status(bview, LyXText::NEED_MORE_REFRESH);
2890 breakAgainOneRow(bview, row);
2891 // will the cursor be in another row now?
2892 if (row->next() && row->next()->par() == row->par() &&
2893 rowLast(row) <= cursor.pos()) {
2895 breakAgainOneRow(bview, row);
2898 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2900 if (row->next() && row->next()->par() == row->par())
2901 need_break_row = row->next();
2905 // set the dimensions of the row
2906 row->fill(fill(bview, row, workWidth(bview)));
2907 int const tmpheight = row->height();
2908 setHeightOfRow(bview, row);
2909 if (tmpheight == row->height())
2910 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2912 status(bview, LyXText::NEED_MORE_REFRESH);
2915 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2919 // current_font = rawtmpfont;
2920 // real_current_font = realtmpfont;
2922 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2923 != cursor.boundary())
2924 setCursor(bview, cursor.par(), cursor.pos(), false,
2925 !cursor.boundary());
2927 lastpos = cursor.par()->size();
2928 if (cursor.pos() == lastpos)
2929 setCurrentFont(bview);
2931 // check, whether the last characters font has changed.
2933 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2934 redoHeightOfParagraph(bview, cursor);
2936 // now the special right address boxes
2937 if (textclasslist.Style(bview->buffer()->params.textclass,
2938 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2939 redoDrawingOfParagraph(bview, cursor);
2945 bool LyXText::paintRowBackground(DrawRowParams & p)
2947 bool clear_area = true;
2949 LyXFont font(LyXFont::ALL_SANE);
2951 pos_type const last = rowLastPrintable(p.row);
2953 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2954 && p.row->par()->isInset(p.row->pos())) {
2955 inset = p.row->par()->getInset(p.row->pos());
2957 clear_area = inset->doClearArea();
2967 int const y = p.yo < 0 ? 0 : p.yo;
2968 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
2969 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
2976 int h = p.row->baseline() - inset->ascent(p.bv, font);
2978 // first clear the whole row above the inset!
2980 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
2983 // clear the space below the inset!
2984 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
2985 if ((p.row->height() - h) > 0) {
2986 p.pain->fillRectangle(p.xo, p.yo + h,
2987 p.width, p.row->height() - h, backgroundColor());
2990 // clear the space behind the inset, if needed
2991 if (!inset->display() && !inset->needFullRow()) {
2992 int const xp = int(p.x) + inset->width(p.bv, font);
2993 if (p.width - xp > 0) {
2994 p.pain->fillRectangle(xp, p.yo, p.width - xp,
2995 p.row->height(), backgroundColor());
3003 void LyXText::paintRowSelection(DrawRowParams & p)
3005 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3007 // the current selection
3008 int const startx = selection.start.x();
3009 int const endx = selection.end.x();
3010 int const starty = selection.start.y();
3011 int const endy = selection.end.y();
3012 Row const * startrow = selection.start.row();
3013 Row const * endrow = selection.end.row();
3017 if (bidi_same_direction) {
3021 int h = row->height();
3023 if (startrow == row && endrow == row) {
3024 if (startx < endx) {
3027 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3031 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3033 } else if (startrow == row) {
3034 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3035 int const w = (is_rtl) ? startx : (p.width - startx);
3036 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3037 } else if (endrow == row) {
3038 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3039 int const w = (is_rtl) ? (p.width - endx) : endx;
3040 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3041 } else if (p.y > starty && p.y < endy) {
3042 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3045 } else if (startrow != row && endrow != row) {
3047 int h = row->height();
3048 if (p.y > starty && p.y < endy) {
3049 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3054 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3060 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3062 Buffer const * buffer = p.bv->buffer();
3063 Paragraph * par = row->par();
3064 pos_type main_body = beginningOfMainBody(buffer, par);
3065 pos_type const last = rowLastPrintable(row);
3067 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3068 pos_type pos = vis2log(vpos);
3069 float const old_tmpx = tmpx;
3070 if (main_body > 0 && pos == main_body - 1) {
3071 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3073 LyXFont const lfont = getLabelFont(buffer, par);
3076 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3078 if (par->isLineSeparator(main_body - 1))
3079 tmpx -= singleWidth(p.bv, par, main_body - 1);
3082 if (hfillExpansion(buffer, row, pos)) {
3083 tmpx += singleWidth(p.bv, par, pos);
3084 if (pos >= main_body)
3087 tmpx += p.label_hfill;
3090 else if (par->isSeparator(pos)) {
3091 tmpx += singleWidth(p.bv, par, pos);
3092 if (pos >= main_body)
3093 tmpx += p.separator;
3095 tmpx += singleWidth(p.bv, par, pos);
3098 if ((startrow != row || selection.start.pos() <= pos) &&
3099 (endrow != row || pos < selection.end.pos())) {
3100 // Here we do not use p.x as p.xo was added to p.x.
3101 p.pain->fillRectangle(int(old_tmpx), p.yo,
3102 int(tmpx - old_tmpx + 1),
3103 row->height(), LColor::selection);
3106 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3107 p.pain->fillRectangle(p.xo + int(tmpx),
3108 p.yo, int(p.bv->workWidth() - tmpx),
3109 row->height(), LColor::selection);
3115 void LyXText::paintRowAppendix(DrawRowParams & p)
3117 // FIXME: can be just p.width ?
3118 int const ww = p.bv->workWidth();
3119 Paragraph * firstpar = p.row->par();
3121 if (firstpar->params().appendix()) {
3122 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3123 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3128 void LyXText::paintRowDepthBar(DrawRowParams & p)
3130 Paragraph::depth_type const depth = p.row->par()->getDepth();
3135 Paragraph::depth_type prev_depth = 0;
3136 if (p.row->previous())
3137 prev_depth = p.row->previous()->par()->getDepth();
3138 Paragraph::depth_type next_depth = 0;
3140 next_depth = p.row->next()->par()->getDepth();
3142 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3143 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3144 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3146 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3148 int const w = LYX_PAPER_MARGIN / 5;
3150 if (i > prev_depth) {
3151 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3153 if (i > next_depth) {
3154 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3160 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3162 if (vsp.kind() != VSpace::LENGTH) {
3163 return int(vsp.inPixels(bv));
3166 int const space_size = int(vsp.inPixels(bv));
3167 int const arrow_size = 10;
3171 int const min_size = 2 * arrow_size + 10
3172 + lyxfont::maxAscent(font)
3173 + lyxfont::maxDescent(font);
3175 return std::max(min_size, space_size);
3179 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3180 VSpace const & vsp, int start)
3182 string const str(prefix
3183 + " (" + vsp.asLyXCommand() + ")");
3185 int const arrow_size = 10;
3187 int const size = getLengthMarkerHeight(p.bv, vsp);
3195 font.setColor(LColor::added_space).decSize();
3196 lyxfont::rectText(str, font, w, a, d);
3198 int const end = start + size;
3200 p.pain->rectText(p.xo + 2 * arrow_size + 5,
3201 start + ((end - start) / 2) + d,
3206 // adding or removing space
3207 bool const added = !(vsp.length().len().value() < 0.0);
3209 int const leftx = p.xo;
3210 int const midx = leftx + arrow_size;
3211 int const rightx = midx + arrow_size;
3214 int const ty1 = added ? (start + arrow_size) : start;
3215 int const ty2 = added ? start : (start + arrow_size);
3217 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3218 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3221 int const by1 = added ? (end - arrow_size) : end;
3222 int const by2 = added ? end : (end - arrow_size);
3224 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3225 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3228 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3234 void LyXText::paintFirstRow(DrawRowParams & p)
3236 Paragraph * par = p.row->par();
3237 ParagraphParameters const & parparams = par->params();
3239 // start of appendix?
3240 if (parparams.startOfAppendix()) {
3241 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3246 // think about the margins
3247 if (!p.row->previous() && bv_owner)
3248 y_top += LYX_PAPER_MARGIN;
3250 // draw a top pagebreak
3251 if (parparams.pagebreakTop()) {
3252 int const y = p.yo + y_top + 2*defaultHeight();
3253 p.pain->line(p.xo, y, p.xo + p.width, y,
3254 LColor::pagebreak, Painter::line_onoffdash);
3261 pb_font.setColor(LColor::pagebreak).decSize();
3262 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3263 p.pain->rectText((p.width - w)/2, y + d,
3264 _("Page Break (top)"), pb_font,
3267 y_top += 3 * defaultHeight();
3271 if (parparams.spaceTop().kind() == VSpace::VFILL) {
3272 int const y1 = p.yo + y_top + 3 * defaultHeight();
3273 int const y2 = p.yo + 2 + y_top;
3275 p.pain->line(0, y1, LYX_PAPER_MARGIN, y1, LColor::added_space);
3277 p.pain->line(0, y2, LYX_PAPER_MARGIN, y2, LColor::added_space);
3279 int const x = LYX_PAPER_MARGIN / 2;
3281 p.pain->line(x, y2, x, y1, LColor::added_space);
3283 y_top += 3 * defaultHeight();
3284 y_top += int(parparams.spaceTop().inPixels(p.bv));
3285 } else if (parparams.spaceTop().kind() == VSpace::LENGTH) {
3286 y_top += drawLengthMarker(p, _("Space above"),
3287 parparams.spaceTop(), p.yo + y_top);
3289 y_top += int(parparams.spaceTop().inPixels(p.bv));
3292 Buffer const * buffer = p.bv->buffer();
3294 LyXLayout const & layout =
3295 textclasslist.Style(buffer->params.textclass, par->getLayout());
3297 // think about the parskip
3298 // some parskips VERY EASY IMPLEMENTATION
3299 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3300 if (par->previous()) {
3301 if (layout.latextype == LATEX_PARAGRAPH
3302 && !par->getDepth()) {
3303 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3305 LyXLayout const & playout =
3306 textclasslist.Style(buffer->params.textclass,
3307 par->previous()->getLayout());
3308 if (playout.latextype == LATEX_PARAGRAPH
3309 && !par->previous()->getDepth()) {
3310 // is it right to use defskip here, too? (AS)
3311 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3317 int const ww = p.bv->workWidth();
3320 if (parparams.lineTop()) {
3321 LyXFont font(LyXFont::ALL_SANE);
3322 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3326 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3327 int const xp = static_cast<int>(inset_owner ? p.x : 0);
3328 p.pain->line(xp, p.yo + y_top, w, p.yo + y_top,
3329 LColor::topline, Painter::line_solid,
3330 Painter::line_thick);
3335 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3337 // should we print a label?
3338 if (layout.labeltype >= LABEL_STATIC
3339 && (layout.labeltype != LABEL_STATIC
3340 || layout.latextype != LATEX_ENVIRONMENT
3341 || par->isFirstInSequence())) {
3343 LyXFont font = getLabelFont(buffer, par);
3344 if (!par->getLabelstring().empty()) {
3346 string const str = par->getLabelstring();
3348 // this is special code for the chapter layout. This is
3349 // printed in an extra row and has a pagebreak at
3351 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3352 if (buffer->params.secnumdepth >= 0) {
3353 float spacing_val = 1.0;
3354 if (!parparams.spacing().isDefault()) {
3355 spacing_val = parparams.spacing().getValue();
3357 spacing_val = buffer->params.spacing.getValue();
3361 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3362 + int(layout.parsep) * defaultHeight();
3365 x = ww - leftMargin(p.bv, p.row) -
3366 lyxfont::width(str, font);
3369 p.pain->text(int(x),
3370 p.yo + p.row->baseline() -
3371 p.row->ascent_of_text() - maxdesc,
3376 x = ww - leftMargin(p.bv, p.row)
3377 + lyxfont::width(layout.labelsep, font);
3379 x = p.x - lyxfont::width(layout.labelsep, font)
3380 - lyxfont::width(str, font);
3383 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3386 // the labels at the top of an environment.
3387 // More or less for bibliography
3388 } else if (par->isFirstInSequence() &&
3389 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3390 layout.labeltype == LABEL_BIBLIO ||
3391 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3392 LyXFont font = getLabelFont(buffer, par);
3393 if (!par->getLabelstring().empty()) {
3394 string const str = par->getLabelstring();
3395 float spacing_val = 1.0;
3396 if (!parparams.spacing().isDefault()) {
3397 spacing_val = parparams.spacing().getValue();
3399 spacing_val = buffer->params.spacing.getValue();
3403 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3404 + (layout.labelbottomsep * defaultHeight()));
3407 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3408 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3409 + ww - rightMargin(buffer, p.row) ) / 2;
3410 x -= lyxfont::width(str, font) / 2;
3411 } else if (is_rtl) {
3412 x = ww - leftMargin(p.bv, p.row) -
3413 lyxfont::width(str, font);
3415 p.pain->text(int(x), p.yo + p.row->baseline()
3416 - p.row->ascent_of_text() - maxdesc,
3421 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3422 LyXFont font = getLayoutFont(buffer, par);
3425 x = ww - leftMargin(p.bv, p.row)
3426 + lyxfont::width(layout.labelsep, font);
3428 x = p.x - lyxfont::width(layout.labelsep, font)
3429 - par->bibkey->width(p.bv, font);
3431 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3436 void LyXText::paintLastRow(DrawRowParams & p)
3438 Paragraph * par = p.row->par();
3439 ParagraphParameters const & parparams = par->params();
3440 int y_bottom = p.row->height();
3442 // think about the margins
3443 if (!p.row->next() && bv_owner)
3444 y_bottom -= LYX_PAPER_MARGIN;
3446 int const ww = p.bv->workWidth();
3448 // draw a bottom pagebreak
3449 if (parparams.pagebreakBottom()) {
3451 pb_font.setColor(LColor::pagebreak).decSize();
3452 int const y = p.yo + y_bottom - 2 * defaultHeight();
3454 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak, Painter::line_onoffdash);
3459 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3460 p.pain->rectText((ww - w) / 2, y + d,
3461 _("Page Break (bottom)"),
3462 pb_font, backgroundColor(), backgroundColor());
3464 y_bottom -= 3 * defaultHeight();
3467 // draw a vfill bottom
3468 if (parparams.spaceBottom().kind() == VSpace::VFILL) {
3469 int const x = LYX_PAPER_MARGIN / 2;
3470 int const x2 = LYX_PAPER_MARGIN;
3471 int const y = p.yo + y_bottom - 3 * defaultHeight();
3472 int const y2 = p.yo + y_bottom - 2;
3474 p.pain->line(0, y, x2, y, LColor::added_space);
3475 p.pain->line(0, y2, x2, y2, LColor::added_space);
3476 p.pain->line(x, y, x, y2, LColor::added_space);
3478 y_bottom -= 3 * defaultHeight();
3479 y_bottom -= int(parparams.spaceBottom().inPixels(p.bv));
3480 } else if (parparams.spaceBottom().kind() == VSpace::LENGTH) {
3481 int const height = getLengthMarkerHeight(p.bv, parparams.spaceBottom());
3482 y_bottom -= drawLengthMarker(p, _("Space below"),
3483 parparams.spaceBottom(),
3484 p.yo + y_bottom - height);
3487 y_bottom -= int(parparams.spaceBottom().inPixels(p.bv));
3490 Buffer const * buffer = p.bv->buffer();
3492 // draw a bottom line
3493 if (parparams.lineBottom()) {
3494 LyXFont font(LyXFont::ALL_SANE);
3495 int const asc = lyxfont::ascent('x',
3496 getFont(buffer, par,
3497 max(pos_type(0), par->size() - 1)));
3501 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3502 int const xp = static_cast<int>(inset_owner ? p.x : 0);
3503 int const y = p.yo + y_bottom;
3504 p.pain->line(xp, y, w, y, LColor::topline, Painter::line_solid,
3505 Painter::line_thick);
3510 pos_type const last = rowLastPrintable(p.row);
3511 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3512 int const endlabel = par->getEndLabel(buffer->params);
3517 case END_LABEL_FILLED_BOX:
3519 LyXFont const font = getFont(buffer, par, last);
3520 int const size = int(0.75 * lyxfont::maxAscent(font));
3521 int const y = (p.yo + p.row->baseline()) - size;
3522 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3524 if (p.row->fill() <= size)
3525 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3527 if (endlabel == END_LABEL_BOX) {
3528 p.pain->line(x, y, x, y + size, LColor::eolmarker);
3529 p.pain->line(x + size, y, x + size , y + size, LColor::eolmarker);
3530 p.pain->line(x, y, x + size, y, LColor::eolmarker);
3531 p.pain->line(x, y + size, x + size, y + size, LColor::eolmarker);
3533 p.pain->fillRectangle(x, y, size, size, LColor::eolmarker);
3537 case END_LABEL_STATIC:
3539 LyXFont font(LyXFont::ALL_SANE);
3540 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3541 string const str = textclasslist.
3542 Style(buffer->params.textclass, layout).endlabelstring();
3543 font = getLabelFont(buffer, par);
3544 int const x = is_rtl ?
3545 int(p.x) - lyxfont::width(str, font)
3546 : ww - rightMargin(buffer, p.row) - p.row->fill();
3547 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3550 case END_LABEL_NO_LABEL:
3555 void LyXText::paintRowText(DrawRowParams & p)
3557 Paragraph * par = p.row->par();
3558 Buffer const * buffer = p.bv->buffer();
3560 pos_type const last = rowLastPrintable(p.row);
3561 pos_type main_body =
3562 beginningOfMainBody(buffer, par);
3563 if (main_body > 0 &&
3564 (main_body - 1 > last ||
3565 !par->isLineSeparator(main_body - 1))) {
3569 LyXLayout const & layout =
3570 textclasslist.Style(buffer->params.textclass, par->getLayout());
3572 pos_type vpos = p.row->pos();
3573 while (vpos <= last) {
3574 pos_type pos = vis2log(vpos);
3575 if (main_body > 0 && pos == main_body - 1) {
3576 int const lwidth = lyxfont::width(layout.labelsep,
3577 getLabelFont(buffer, par));
3579 p.x += p.label_hfill + lwidth
3580 - singleWidth(p.bv, par, main_body - 1);
3583 if (par->isHfill(pos)) {
3586 int const y0 = p.yo + p.row->baseline();
3587 int const y1 = y0 - defaultHeight() / 2;
3589 p.pain->line(int(p.x), y1, int(p.x), y0,
3590 LColor::added_space);
3592 if (hfillExpansion(buffer, p.row, pos)) {
3593 int const y2 = (y0 + y1) / 2;
3595 if (pos >= main_body) {
3596 p.pain->line(int(p.x), y2,
3597 int(p.x + p.hfill), y2,
3598 LColor::added_space,
3599 Painter::line_onoffdash);
3602 p.pain->line(int(p.x), y2,
3603 int(p.x + p.label_hfill), y2,
3604 LColor::added_space,
3605 Painter::line_onoffdash);
3606 p.x += p.label_hfill;
3608 p.pain->line(int(p.x), y1,
3610 LColor::added_space);
3614 } else if (par->isSeparator(pos)) {
3615 p.x += singleWidth(p.bv, par, pos);
3616 if (pos >= main_body)
3626 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3627 Row * row, int y, bool cleared)
3629 if (row->height() <= 0) {
3630 lyxerr << "LYX_ERROR: row.height: "
3631 << row->height() << endl;
3637 // set up drawing parameters
3639 p.pain = &bv->painter();
3643 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3644 if (inset_owner && (p.x < 0))
3648 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3649 p.cleared = cleared;
3653 // clear to background if necessary
3654 p.cleared = paintRowBackground(p);
3656 // paint the selection background
3657 if (selection.set()) {
3658 paintRowSelection(p);
3661 // vertical lines for appendix
3662 paintRowAppendix(p);
3664 // environment depth brackets
3665 paintRowDepthBar(p);
3667 // draw any stuff wanted for a first row of a paragraph
3672 // draw any stuff wanted for the last row of a paragraph
3673 if (!row->next() || (row->next()->par() != row->par())) {
3682 int LyXText::defaultHeight() const
3684 LyXFont font(LyXFont::ALL_SANE);
3685 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3689 /* returns the column near the specified x-coordinate of the row
3690 * x is set to the real beginning of this column */
3692 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3693 bool & boundary) const
3696 float fill_separator;
3698 float fill_label_hfill;
3700 prepareToPrint(bview, row, tmpx, fill_separator,
3701 fill_hfill, fill_label_hfill);
3703 pos_type vc = row->pos();
3704 pos_type last = rowLastPrintable(row);
3706 LyXLayout const & layout =
3707 textclasslist.Style(bview->buffer()->params.textclass,
3708 row->par()->getLayout());
3709 bool left_side = false;
3711 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3712 float last_tmpx = tmpx;
3714 if (main_body > 0 &&
3715 (main_body - 1 > last ||
3716 !row->par()->isLineSeparator(main_body - 1)))
3719 while (vc <= last && tmpx <= x) {
3722 if (main_body > 0 && c == main_body-1) {
3723 tmpx += fill_label_hfill +
3724 lyxfont::width(layout.labelsep,
3725 getLabelFont(bview->buffer(), row->par()));
3726 if (row->par()->isLineSeparator(main_body - 1))
3727 tmpx -= singleWidth(bview, row->par(), main_body-1);
3730 if (hfillExpansion(bview->buffer(), row, c)) {
3731 x += singleWidth(bview, row->par(), c);
3735 tmpx += fill_label_hfill;
3737 else if (row->par()->isSeparator(c)) {
3738 tmpx += singleWidth(bview, row->par(), c);
3740 tmpx+= fill_separator;
3742 tmpx += singleWidth(bview, row->par(), c);
3746 if ((tmpx + last_tmpx) / 2 > x) {
3751 if (vc > last + 1) // This shouldn't happen.
3755 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3756 // some speedup if rtl_support=false
3757 && (!row->next() || row->next()->par() != row->par());
3758 bool const rtl = (lastrow)
3759 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3760 : false; // If lastrow is false, we don't need to compute
3761 // the value of rtl.
3763 if (row->pos() > last) // Row is empty?
3766 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3767 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3769 else if (vc == row->pos()) {
3771 if (bidi_level(c) % 2 == 1)
3774 c = vis2log(vc - 1);
3775 bool const rtl = (bidi_level(c) % 2 == 1);
3776 if (left_side == rtl) {
3778 boundary = isBoundary(bview->buffer(), row->par(), c);
3782 if (row->pos() <= last && c > last
3783 && row->par()->isNewline(last)) {
3784 if (bidi_level(last) % 2 == 0)
3785 tmpx -= singleWidth(bview, row->par(), last);
3787 tmpx += singleWidth(bview, row->par(), last);
3797 // returns pointer to a specified row
3798 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3803 Row * tmprow = firstrow;
3806 // find the first row of the specified paragraph
3807 while (tmprow->next() && tmprow->par() != par) {
3808 y += tmprow->height();
3809 tmprow = tmprow->next();
3812 // now find the wanted row
3813 while (tmprow->pos() < pos
3815 && tmprow->next()->par() == par
3816 && tmprow->next()->pos() <= pos) {
3817 y += tmprow->height();
3818 tmprow = tmprow->next();
3825 Row * LyXText::getRowNearY(int & y) const
3827 // If possible we should optimize this method. (Lgb)
3828 Row * tmprow = firstrow;
3831 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3832 tmpy += tmprow->height();
3833 tmprow = tmprow->next();
3836 y = tmpy; // return the real y
3841 int LyXText::getDepth() const
3843 return cursor.par()->getDepth();