1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #include "lyxtextclasslist.h"
16 #include "paragraph.h"
18 #include "bufferparams.h"
27 #include "lyxscreen.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
38 #include "support/textutils.h"
39 #include "support/LAssert.h"
40 #include "support/lstrings.h"
52 int const LYX_PAPER_MARGIN = 20;
56 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
59 int LyXText::workWidth(BufferView * bview) const
62 return inset_owner->textWidth(bview);
64 return bview->workWidth();
68 int LyXText::workWidth(BufferView * bview, Inset * inset) const
73 par = inset->parOwner();
75 pos = par->getPositionOfInset(inset);
77 if (!par || pos == -1) {
78 lyxerr << "LyXText::workWidth: something is wrong,"
79 " fall back to the brute force method" << endl;
80 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
81 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
82 for (; it != end; ++it) {
92 return workWidth(bview);
95 LyXLayout const & layout =
96 textclasslist[bview->buffer()->params.textclass][par->layout()];
98 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
99 // Optimization here: in most cases, the real row is
100 // not needed, but only the par/pos values. So we just
101 // construct a dummy row for leftMargin. (JMarc)
105 return workWidth(bview) - leftMargin(bview, &dummyrow);
108 Row * row = getRow(par, pos, dummy_y);
110 while (frow->previous() && frow->par() == frow->previous()->par())
111 frow = frow->previous();
112 unsigned int maxw = 0;
113 while (frow->next() && frow->par() == frow->next()->par()) {
114 if ((frow != row) && (maxw < frow->width()))
115 maxw = frow->width();
121 return workWidth(bview);
125 int LyXText::getRealCursorX(BufferView * bview) const
128 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
129 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
134 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
137 if (!Encodings::is_arabic(c))
138 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
139 return c + (0xb0 - '0');
143 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
144 unsigned char next_char = ' ';
146 for (pos_type i = pos+1; i < par->size(); ++i)
147 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
148 next_char = par->getChar(i);
152 if (Encodings::is_arabic(next_char)) {
153 if (Encodings::is_arabic(prev_char))
154 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
156 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
158 if (Encodings::is_arabic(prev_char))
159 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
161 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
165 // This is the comments that some of the warnings below refers to.
166 // There are some issues in this file and I don't think they are
167 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
168 // this is a problem that has been here almost from day one and that a
169 // larger userbase with differenct access patters triggers the bad
170 // behaviour. (segfaults.) What I think happen is: In several places
171 // we store the paragraph in the current cursor and then moves the
172 // cursor. This movement of the cursor will delete paragraph at the
173 // old position if it is now empty. This will make the temporary
174 // pointer to the old cursor paragraph invalid and dangerous to use.
175 // And is some cases this will trigger a segfault. I have marked some
176 // of the cases where this happens with a warning, but I am sure there
177 // are others in this file and in text2.C. There is also a note in
178 // Delete() that you should read. In Delete I store the paragraph->id
179 // instead of a pointer to the paragraph. I am pretty sure this faulty
180 // use of temporary pointers to paragraphs that might have gotten
181 // invalidated (through a cursor movement) before they are used, are
182 // the cause of the strange crashes we get reported often.
184 // It is very tiresom to change this code, especially when it is as
185 // hard to read as it is. Help to fix all the cases where this is done
186 // would be greately appreciated.
190 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
193 char const c = par->getChar(pos);
194 return singleWidth(bview, par, pos, c);
198 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
199 pos_type pos, char c) const
201 LyXFont const font = getFont(bview->buffer(), par, pos);
203 // The most common case is handled first (Asger)
204 if (IsPrintable(c)) {
205 if (font.language()->RightToLeft()) {
206 if (font.language()->lang() == "arabic" &&
207 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
208 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
209 if (Encodings::IsComposeChar_arabic(c))
212 c = transformChar(c, par, pos);
213 else if (font.language()->lang() == "hebrew" &&
214 Encodings::IsComposeChar_hebrew(c))
217 return lyxfont::width(c, font);
219 } else if (IsHfillChar(c)) {
220 return 3; /* Because of the representation
221 * as vertical lines */
222 } else if (c == Paragraph::META_INSET) {
223 Inset * tmpinset = par->getInset(pos);
226 // this IS needed otherwise on initialitation we don't get the fill
227 // of the row right (ONLY on initialization if we read a file!)
228 // should be changed! (Jug 20011204)
229 tmpinset->update(bview, font);
231 return tmpinset->width(bview, font);
235 } else if (IsSeparatorChar(c))
237 else if (IsNewlineChar(c))
239 return lyxfont::width(c, font);
243 // Returns the paragraph position of the last character in the specified row
244 pos_type LyXText::rowLast(Row const * row) const
246 if (!row->next() || row->next()->par() != row->par()) {
247 return row->par()->size() - 1;
249 return row->next()->pos() - 1;
254 pos_type LyXText::rowLastPrintable(Row const * row) const
256 pos_type const last = rowLast(row);
257 bool ignore_the_space_on_the_last_position = true;
259 // we have to consider a space on the last position in this case!
260 if (row->next() && row->par() == row->next()->par() &&
261 row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
262 (ins=row->next()->par()->getInset(last+1)) &&
263 (ins->needFullRow() || ins->display()))
265 ignore_the_space_on_the_last_position = false;
267 if (last >= row->pos()
269 && row->next()->par() == row->par()
270 && row->par()->isSeparator(last)
271 && ignore_the_space_on_the_last_position)
278 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
280 bidi_same_direction = true;
281 if (!lyxrc.rtl_support) {
286 Inset * inset = row->par()->inInset();
287 if (inset && inset->owner() &&
288 inset->owner()->lyxCode() == Inset::ERT_CODE) {
293 bidi_start = row->pos();
294 bidi_end = rowLastPrintable(row);
296 if (bidi_start > bidi_end) {
301 if (bidi_end + 2 - bidi_start >
302 static_cast<pos_type>(log2vis_list.size())) {
304 (bidi_end + 2 - bidi_start < 500) ?
305 500 : 2 * (bidi_end + 2 - bidi_start);
306 log2vis_list.resize(new_size);
307 vis2log_list.resize(new_size);
308 bidi_levels.resize(new_size);
311 vis2log_list[bidi_end + 1 - bidi_start] = -1;
312 log2vis_list[bidi_end + 1 - bidi_start] = -1;
316 row->par()->isRightToLeftPar(buf->params);
320 pos_type const main_body = beginningOfMainBody(buf, row->par());
322 for (pos_type lpos = bidi_start;
323 lpos <= bidi_end; ++lpos) {
324 bool is_space = row->par()->isLineSeparator(lpos);
326 (is_space && lpos + 1 <= bidi_end &&
327 !row->par()->isLineSeparator(lpos + 1) &&
328 !row->par()->isNewline(lpos + 1))
330 LyXFont font = row->par()->getFontSettings(buf->params, pos);
331 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
332 font.number() == LyXFont::ON &&
333 row->par()->getFontSettings(buf->params, lpos - 1).number()
335 font = row->par()->getFontSettings(buf->params, lpos);
340 bool new_rtl = font.isVisibleRightToLeft();
341 bool new_rtl0 = font.isRightToLeft();
344 if (lpos == main_body - 1
345 && row->pos() < main_body - 1
347 new_level = (rtl_par) ? 1 : 0;
348 new_rtl = new_rtl0 = rtl_par;
350 new_level = (new_rtl) ? 1 : 2;
352 new_level = (rtl_par) ? 2 : 0;
354 if (is_space && new_level >= level) {
360 int new_level2 = new_level;
362 if (level == new_level && rtl0 != new_rtl0) {
364 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
365 } else if (level < new_level) {
366 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
367 if (new_level > rtl_par)
368 bidi_same_direction = false;
370 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
373 bidi_levels[lpos - bidi_start] = new_level;
375 while (level > new_level2) {
376 pos_type old_lpos = stack[--level];
377 int delta = lpos - old_lpos - 1;
380 log2vis_list[lpos - bidi_start] += delta;
381 log2vis_list[old_lpos - bidi_start] += delta;
383 while (level < new_level)
384 stack[level++] = lpos;
388 pos_type const old_lpos = stack[--level];
389 int delta = bidi_end - old_lpos;
392 log2vis_list[old_lpos - bidi_start] += delta;
395 pos_type vpos = bidi_start - 1;
396 for (pos_type lpos = bidi_start;
397 lpos <= bidi_end; ++lpos) {
398 vpos += log2vis_list[lpos - bidi_start];
399 vis2log_list[vpos - bidi_start] = lpos;
400 log2vis_list[lpos - bidi_start] = vpos;
405 // This method requires a previous call to ComputeBidiTables()
406 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
409 if (!lyxrc.rtl_support || pos == 0)
412 if (!bidi_InRange(pos - 1)) {
413 /// This can happen if pos is the first char of a row.
414 /// Returning false in this case is incorrect!
418 bool const rtl = bidi_level(pos - 1) % 2;
419 bool const rtl2 = bidi_InRange(pos)
420 ? bidi_level(pos) % 2
421 : par->isRightToLeftPar(buf->params);
426 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
427 pos_type pos, LyXFont const & font) const
429 if (!lyxrc.rtl_support)
430 return false; // This is just for speedup
432 bool const rtl = font.isVisibleRightToLeft();
433 bool const rtl2 = bidi_InRange(pos)
434 ? bidi_level(pos) % 2
435 : par->isRightToLeftPar(buf->params);
439 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
441 // Draw end-of-line marker
442 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
443 int const wid = lyxfont::width('n', font);
444 int const asc = lyxfont::maxAscent(font);
445 int const y = p.yo + p.row->baseline();
449 yp[0] = int(y - 0.875 * asc * 0.75);
450 yp[1] = int(y - 0.500 * asc * 0.75);
451 yp[2] = int(y - 0.125 * asc * 0.75);
453 if (bidi_level(pos) % 2 == 0) {
454 xp[0] = int(p.x + wid * 0.375);
456 xp[2] = int(p.x + wid * 0.375);
458 xp[0] = int(p.x + wid * 0.625);
459 xp[1] = int(p.x + wid);
460 xp[2] = int(p.x + wid * 0.625);
463 p.pain->lines(xp, yp, 3, LColor::eolmarker);
465 yp[0] = int(y - 0.500 * asc * 0.75);
466 yp[1] = int(y - 0.500 * asc * 0.75);
467 yp[2] = int(y - asc * 0.75);
469 if (bidi_level(pos) % 2 == 0) {
471 xp[1] = int(p.x + wid);
472 xp[2] = int(p.x + wid);
474 xp[0] = int(p.x + wid);
479 p.pain->lines(xp, yp, 3, LColor::eolmarker);
485 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
487 Inset * inset = p.row->par()->getInset(pos);
489 // FIXME: shouldn't happen
494 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
495 // we need this here as the row pointer may be illegal
496 // at a later time (Jug20020502)
497 Row * prev = p.row->previous();
499 inset->update(p.bv, font, false);
500 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
502 if (!need_break_row && !inset_owner
503 && p.bv->text->status() == CHANGED_IN_DRAW) {
504 if (prev && prev->par() == p.row->par()) {
505 breakAgainOneRow(p.bv, prev);
506 if (prev->next() != p.row) {
507 // breakAgainOneRow() has removed p.row
508 p.row = 0; // see what this breaks
509 need_break_row = prev;
511 need_break_row = p.row;
514 need_break_row = firstrow;
516 need_break_row = prev->next();
518 setCursor(p.bv, cursor.par(), cursor.pos());
525 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
527 if (!lyxrc.mark_foreign_language)
529 if (orig_font.language() == latex_language)
531 if (orig_font.language() == p.bv->buffer()->params.language)
534 int const y = p.yo + p.row->height() - 1;
535 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
539 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
541 pos_type pos = vis2log(vpos);
546 char c = p.row->par()->getChar(pos);
550 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
551 int const width = lyxfont::width(c, font);
554 for (pos_type i = pos-1; i >= 0; --i) {
555 c = p.row->par()->getChar(i);
556 if (!Encodings::IsComposeChar_hebrew(c)) {
557 if (IsPrintableNonspace(c)) {
559 singleWidth(p.bv, p.row->par(), i, c);
561 dx = (c == 'ø' || c == 'ã')
563 : (width2 - width) / 2;
570 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
574 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
576 pos_type pos = vis2log(vpos);
580 char c = p.row->par()->getChar(pos);
581 c = transformChar(c, p.row->par(), pos);
585 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
586 int const width = lyxfont::width(c, font);
589 for (pos_type i = pos-1; i >= 0; --i) {
590 c = p.row->par()->getChar(i);
591 if (!Encodings::IsComposeChar_arabic(c)) {
592 if (IsPrintableNonspace(c)) {
594 singleWidth(p.bv, p.row->par(), i, c);
595 dx = (width2 - width) / 2;
601 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
605 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
606 bool hebrew, bool arabic)
608 pos_type pos = vis2log(vpos);
609 pos_type const last = rowLastPrintable(p.row);
610 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
614 str += p.row->par()->getChar(pos);
616 unsigned char c = str[0];
617 str[0] = transformChar(c, p.row->par(), pos);
621 // collect as much similar chars as we can
622 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
623 char c = p.row->par()->getChar(pos);
625 if (!IsPrintableNonspace(c))
628 if (arabic && Encodings::IsComposeChar_arabic(c))
630 if (hebrew && Encodings::IsComposeChar_hebrew(c))
633 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
640 // Draw text and set the new x position
641 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
642 p.x += lyxfont::width(str, orig_font);
646 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
648 pos_type const pos = vis2log(vpos);
649 Paragraph * par = p.row->par();
651 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
653 float const orig_x = p.x;
655 char const c = par->getChar(pos);
657 if (IsNewlineChar(c)) {
661 } else if (IsInsetChar(c)) {
662 if (!drawInset(p, pos))
665 drawForeignMark(p, orig_x, orig_font);
669 // usual characters, no insets
671 // special case languages
672 bool const hebrew = (orig_font.language()->lang() == "hebrew");
674 orig_font.language()->lang() == "arabic" &&
675 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
676 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
678 // draw as many chars as we can
679 if ((!hebrew && !arabic)
680 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
681 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
682 drawChars(p, vpos, true, false);
684 drawHebrewComposeChar(p, vpos);
686 drawArabicComposeChar(p, vpos);
689 drawForeignMark(p, orig_x, orig_font);
691 #ifdef INHERIT_LANGUAGE
693 if ((font.language() == inherit_language) ||
694 (font.language() == ignore_language))
695 lyxerr << "No this shouldn't happen!\n";
702 // Returns the left beginning of the text.
703 // This information cannot be taken from the layouts-objekt, because in
704 // LaTeX the beginning of the text fits in some cases (for example sections)
705 // exactly the label-width.
706 int LyXText::leftMargin(BufferView * bview, Row const * row) const
709 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
710 (ins=row->par()->getInset(row->pos())) &&
711 (ins->needFullRow() || ins->display()))
712 return LYX_PAPER_MARGIN;
714 LyXTextClass const & tclass =
715 textclasslist[bview->buffer()->params.textclass];
716 LyXLayout const & layout = tclass[row->par()->layout()];
718 string parindent = layout.parindent;
720 int x = LYX_PAPER_MARGIN;
722 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
724 // this is the way, LyX handles the LaTeX-Environments.
725 // I have had this idea very late, so it seems to be a
726 // later added hack and this is true
727 if (!row->par()->getDepth()) {
728 if (row->par()->layout() == tclass.defaultLayoutName()) {
729 // find the previous same level paragraph
730 if (row->par()->previous()) {
731 Paragraph * newpar = row->par()
732 ->depthHook(row->par()->getDepth());
734 tclass[newpar->layout()].nextnoindent)
739 // find the next level paragraph
742 row->par()->outerHook();
744 // make a corresponding row. Needed to call LeftMargin()
746 // check wether it is a sufficent paragraph
747 if (newpar && tclass[newpar->layout()].isEnvironment()) {
749 dummyrow.par(newpar);
750 dummyrow.pos(newpar->size());
751 x = leftMargin(bview, &dummyrow);
753 // this is no longer an error, because this function
754 // is used to clear impossible depths after changing
755 // a layout. Since there is always a redo,
756 // LeftMargin() is always called
757 row->par()->params().depth(0);
760 if (newpar && row->par()->layout() == tclass.defaultLayoutName()) {
761 if (newpar->params().noindent())
764 parindent = tclass[newpar->layout()].parindent;
768 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
769 switch (layout.margintype) {
771 if (!layout.leftmargin.empty()) {
772 x += lyxfont::signedWidth(layout.leftmargin,
773 tclass.defaultfont());
775 if (!row->par()->getLabelstring().empty()) {
776 x += lyxfont::signedWidth(layout.labelindent,
778 x += lyxfont::width(row->par()->getLabelstring(),
780 x += lyxfont::width(layout.labelsep, labelfont);
784 x += lyxfont::signedWidth(layout.labelindent, labelfont);
785 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
786 if (!row->par()->getLabelWidthString().empty()) {
787 x += lyxfont::width(row->par()->getLabelWidthString(),
789 x += lyxfont::width(layout.labelsep, labelfont);
794 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
795 / (row->par()->getDepth() + 4);
797 case MARGIN_FIRST_DYNAMIC:
798 if (layout.labeltype == LABEL_MANUAL) {
799 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
800 x += lyxfont::signedWidth(layout.leftmargin,
803 x += lyxfont::signedWidth(layout.labelindent,
806 } else if (row->pos()
807 // Special case to fix problems with
809 || (layout.labeltype == LABEL_STATIC
810 && layout.latextype == LATEX_ENVIRONMENT
811 && ! row->par()->isFirstInSequence())) {
812 x += lyxfont::signedWidth(layout.leftmargin,
814 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
815 && layout.labeltype != LABEL_BIBLIO
816 && layout.labeltype !=
817 LABEL_CENTERED_TOP_ENVIRONMENT) {
818 x += lyxfont::signedWidth(layout.labelindent,
820 x += lyxfont::width(layout.labelsep, labelfont);
821 x += lyxfont::width(row->par()->getLabelstring(),
826 case MARGIN_RIGHT_ADDRESS_BOX:
828 // ok, a terrible hack. The left margin depends on the widest
829 // row in this paragraph. Do not care about footnotes, they
830 // are *NOT* allowed in the LaTeX realisation of this layout.
832 // find the first row of this paragraph
833 Row const * tmprow = row;
834 while (tmprow->previous()
835 && tmprow->previous()->par() == row->par())
836 tmprow = tmprow->previous();
838 int minfill = tmprow->fill();
839 while (tmprow->next() && tmprow->next()->par() == row->par()) {
840 tmprow = tmprow->next();
841 if (tmprow->fill() < minfill)
842 minfill = tmprow->fill();
845 x += lyxfont::signedWidth(layout.leftmargin,
846 tclass.defaultfont());
852 LyXAlignment align; // wrong type
854 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
855 align = layout.align;
857 align = row->par()->params().align();
859 // set the correct parindent
860 if (row->pos() == 0) {
861 if ((layout.labeltype == LABEL_NO_LABEL
862 || layout.labeltype == LABEL_TOP_ENVIRONMENT
863 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
864 || (layout.labeltype == LABEL_STATIC
865 && layout.latextype == LATEX_ENVIRONMENT
866 && ! row->par()->isFirstInSequence()))
867 && align == LYX_ALIGN_BLOCK
868 && !row->par()->params().noindent()
869 // in tabulars and ert paragraphs are never indented!
870 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
871 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
872 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
873 && (row->par()->layout() != tclass.defaultLayoutName() ||
874 bview->buffer()->params.paragraph_separation ==
875 BufferParams::PARSEP_INDENT)) {
876 x += lyxfont::signedWidth(parindent,
877 tclass.defaultfont());
878 } else if (layout.labeltype == LABEL_BIBLIO) {
879 // ale970405 Right width for bibitems
880 x += bibitemMaxWidth(bview, tclass.defaultfont());
888 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
891 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
892 (ins=row->par()->getInset(row->pos())) &&
893 (ins->needFullRow() || ins->display()))
894 return LYX_PAPER_MARGIN;
896 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
897 LyXLayout const & layout = tclass[row->par()->layout()];
899 int x = LYX_PAPER_MARGIN
900 + lyxfont::signedWidth(tclass.rightmargin(),
901 tclass.defaultfont());
903 // this is the way, LyX handles the LaTeX-Environments.
904 // I have had this idea very late, so it seems to be a
905 // later added hack and this is true
906 if (row->par()->getDepth()) {
907 // find the next level paragraph
909 Paragraph * newpar = row->par();
912 newpar = newpar->previous();
914 && newpar->getDepth() >= row->par()->getDepth());
916 // make a corresponding row. Needed to call LeftMargin()
918 // check wether it is a sufficent paragraph
919 if (newpar && tclass[newpar->layout()].isEnvironment()) {
921 dummyrow.par(newpar);
923 x = rightMargin(buf, &dummyrow);
925 // this is no longer an error, because this function
926 // is used to clear impossible depths after changing
927 // a layout. Since there is always a redo,
928 // LeftMargin() is always called
929 row->par()->params().depth(0);
933 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
934 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
935 * 4 / (row->par()->getDepth() + 4);
940 int LyXText::labelEnd(BufferView * bview, Row const * row) const
942 if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
946 tmprow.pos(row->par()->size());
947 return leftMargin(bview, &tmprow); /* just the beginning
950 return 0; /* LabelEnd is only needed, if the
951 layout fills a flushleft
956 // get the next breakpoint in a given paragraph
958 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
960 Paragraph * par = row->par();
965 pos_type const pos = row->pos();
967 // position of the last possible breakpoint
968 // -1 isn't a suitable value, but a flag
969 pos_type last_separator = -1;
970 width -= rightMargin(bview->buffer(), row);
972 pos_type const main_body =
973 beginningOfMainBody(bview->buffer(), par);
974 LyXLayout const & layout =
975 textclasslist[bview->buffer()->params.textclass][par->layout()];
978 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
979 /* special code for right address boxes, only newlines count */
980 while (i < par->size()) {
981 if (par->isNewline(i)) {
983 i = par->size() - 1; // this means break
985 } else if (par->isInset(i) && par->getInset(i)
986 && par->getInset(i)->display()) {
987 par->getInset(i)->display(false);
992 // Last position is an invariant
993 pos_type const last =
995 // this is the usual handling
996 int x = leftMargin(bview, row);
997 bool doitonetime = true;
998 while (doitonetime || ((x < width) && (i < last))) {
1000 char const c = par->getChar(i);
1002 if (c == Paragraph::META_INSET)
1003 in = par->getInset(i);
1004 if (IsNewlineChar(c)) {
1006 x = width; // this means break
1007 } else if (in && !in->isChar()) {
1008 // check wether a Display() inset is
1009 // valid here. if not, change it to
1011 if (in->display() &&
1012 (layout.isCommand() ||
1013 (layout.labeltype == LABEL_MANUAL
1014 && i < beginningOfMainBody(bview->buffer(), par))))
1016 // display istn't allowd
1018 x += singleWidth(bview, par, i, c);
1019 } else if (in->display() || in->needFullRow()) {
1020 // So break the line here
1024 if (par->isLineSeparator(i+1))
1027 last_separator = last; // to avoid extra rows
1029 last_separator = i - 1;
1030 x = width; // this means break
1032 x += singleWidth(bview, par, i, c);
1033 // we have to check this separately as we could have a
1034 // lineseparator and then the algorithm below would prefer
1035 // that which IS wrong! We should always break on an inset
1036 // if it's too long and not on the last separator.
1037 // Maybe the only exeption is insets used as chars but
1038 // then we would have to have a special function inside
1039 // the inset to tell us this. Till then we leave it as
1040 // it is now. (Jug 20020106)
1041 if (pos < i && x >= width && last_separator >= 0)
1042 last_separator = i - 1;
1045 if (par->isLineSeparator(i))
1047 x += singleWidth(bview, par, i, c);
1050 if (i == main_body) {
1051 x += lyxfont::width(layout.labelsep,
1052 getLabelFont(bview->buffer(), par));
1053 if (par->isLineSeparator(i - 1))
1054 x-= singleWidth(bview, par, i - 1);
1055 int left_margin = labelEnd(bview, row);
1056 if (x < left_margin)
1060 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1061 last_separator = i - 2;
1062 else if ((pos < i) && (last_separator < 0) && (x >= width))
1063 last_separator = i - 1;
1064 // end of paragraph is always a suitable separator
1065 else if (i == last && x < width)
1069 // well, if last_separator is still 0, the line isn't breakable.
1070 // don't care and cut simply at the end
1071 if (last_separator < 0) {
1075 // manual labels cannot be broken in LaTeX, do not care
1076 if (main_body && last_separator < main_body)
1077 last_separator = main_body - 1;
1079 return last_separator;
1083 // returns the minimum space a row needs on the screen in pixel
1084 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1086 if (paper_width < 0)
1090 // get the pure distance
1091 pos_type const last = rowLastPrintable(row);
1093 // special handling of the right address boxes
1094 if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
1095 == MARGIN_RIGHT_ADDRESS_BOX)
1097 int const tmpfill = row->fill();
1098 row->fill(0); // the minfill in MarginLeft()
1099 w = leftMargin(bview, row);
1102 w = leftMargin(bview, row);
1104 LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][row->par()->layout()];
1105 pos_type const main_body =
1106 beginningOfMainBody(bview->buffer(), row->par());
1107 pos_type i = row->pos();
1110 if (main_body > 0 && i == main_body) {
1111 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1112 if (row->par()->isLineSeparator(i - 1))
1113 w -= singleWidth(bview, row->par(), i - 1);
1114 int left_margin = labelEnd(bview, row);
1115 if (w < left_margin)
1118 w += singleWidth(bview, row->par(), i);
1121 if (main_body > 0 && main_body > last) {
1122 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1123 if (last >= 0 && row->par()->isLineSeparator(last))
1124 w -= singleWidth(bview, row->par(), last);
1125 int const left_margin = labelEnd(bview, row);
1126 if (w < left_margin)
1130 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1135 // returns the minimum space a manual label needs on the screen in pixel
1136 int LyXText::labelFill(BufferView * bview, Row const * row) const
1138 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1139 // -1 because a label ends either with a space that is in the label,
1140 // or with the beginning of a footnote that is outside the label.
1142 // I don't understand this code in depth, but sometimes "last" is
1143 // less than 0 and this causes a crash. This fix seems to work
1144 // correctly, but I bet the real error is elsewhere. The bug is
1145 // triggered when you have an open footnote in a paragraph
1146 // environment with a manual label. (Asger)
1147 if (last < 0) last = 0;
1149 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1154 pos_type i = row->pos();
1156 w += singleWidth(bview, row->par(), i);
1161 if (!row->par()->params().labelWidthString().empty()) {
1162 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1163 getLabelFont(bview->buffer(), row->par())) - w,
1171 // returns the number of separators in the specified row. The separator
1172 // on the very last column doesnt count
1173 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1175 pos_type last = rowLastPrintable(row);
1176 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1179 for (; p <= last; ++p) {
1180 if (row->par()->isSeparator(p)) {
1188 // returns the number of hfills in the specified row. The LyX-Hfill is
1189 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1190 // ignored. This is *MUCH* more usefull than not to ignore!
1191 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1193 pos_type const last = rowLast(row);
1194 pos_type first = row->pos();
1196 if (first) { /* hfill *DO* count at the beginning
1198 while (first <= last && row->par()->isHfill(first)) {
1203 first = max(first, beginningOfMainBody(buf, row->par()));
1205 for (pos_type p = first; p <= last; ++p) {
1206 // last, because the end is ignored!
1208 if (row->par()->isHfill(p)) {
1216 // like NumberOfHfills, but only those in the manual label!
1217 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1219 pos_type last = rowLast(row);
1220 pos_type first = row->pos();
1221 if (first) { /* hfill *DO* count at the beginning
1223 while (first < last && row->par()->isHfill(first))
1227 last = min(last, beginningOfMainBody(buf, row->par()));
1229 for (pos_type p = first; p < last; ++p) {
1230 // last, because the end is ignored!
1231 if (row->par()->isHfill(p)) {
1239 // returns true, if a expansion is needed.
1240 // Rules are given by LaTeX
1241 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1244 // by the way, is it a hfill?
1245 if (!row_ptr->par()->isHfill(pos))
1248 // at the end of a row it does not count
1249 // unless another hfill exists on the line
1250 if (pos >= rowLast(row_ptr)) {
1251 pos_type i = row_ptr->pos();
1252 while (i < pos && !row_ptr->par()->isHfill(i)) {
1260 // at the beginning of a row it does not count, if it is not
1261 // the first row of a paragaph
1262 if (!row_ptr->pos())
1265 // in some labels it does not count
1266 if (textclasslist[buf->params.textclass][row_ptr->par()->layout()].margintype
1268 && pos < beginningOfMainBody(buf, row_ptr->par()))
1271 // if there is anything between the first char of the row and
1272 // the sepcified position that is not a newline and not a hfill,
1273 // the hfill will count, otherwise not
1274 pos_type i = row_ptr->pos();
1275 while (i < pos && (row_ptr->par()->isNewline(i)
1276 || row_ptr->par()->isHfill(i)))
1283 LColor::color LyXText::backgroundColor()
1286 return inset_owner->backgroundColor();
1288 return LColor::background;
1291 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1293 /* get the maximum ascent and the maximum descent */
1296 float layoutasc = 0;
1297 float layoutdesc = 0;
1300 Inset * tmpinset = 0;
1302 /* ok , let us initialize the maxasc and maxdesc value.
1303 * This depends in LaTeX of the font of the last character
1304 * in the paragraph. The hack below is necessary because
1305 * of the possibility of open footnotes */
1307 /* Correction: only the fontsize count. The other properties
1308 are taken from the layoutfont. Nicer on the screen :) */
1309 Paragraph * par = row_ptr->par();
1310 Paragraph * firstpar = row_ptr->par();
1312 LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][firstpar->layout()];
1314 // as max get the first character of this row then it can increes but not
1315 // decrees the height. Just some point to start with so we don't have to
1316 // do the assignment below too often.
1317 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1318 LyXFont::FONT_SIZE const tmpsize = font.size();
1319 font = getLayoutFont(bview->buffer(), par);
1320 LyXFont::FONT_SIZE const size = font.size();
1321 font.setSize(tmpsize);
1323 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1325 float spacing_val = 1.0;
1326 if (!row_ptr->par()->params().spacing().isDefault()) {
1327 spacing_val = row_ptr->par()->params().spacing().getValue();
1329 spacing_val = bview->buffer()->params.spacing.getValue();
1331 //lyxerr << "spacing_val = " << spacing_val << endl;
1333 int maxasc = int(lyxfont::maxAscent(font) *
1334 layout.spacing.getValue() *
1336 int maxdesc = int(lyxfont::maxDescent(font) *
1337 layout.spacing.getValue() *
1339 pos_type const pos_end = rowLast(row_ptr);
1343 // Check if any insets are larger
1344 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1345 if (row_ptr->par()->isInset(pos)) {
1346 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1347 tmpinset = row_ptr->par()->getInset(pos);
1349 #if 1 // this is needed for deep update on initialitation
1350 tmpinset->update(bview, tmpfont);
1352 asc = tmpinset->ascent(bview, tmpfont);
1353 desc = tmpinset->descent(bview, tmpfont);
1354 maxwidth += tmpinset->width(bview, tmpfont);
1355 maxasc = max(maxasc, asc);
1356 maxdesc = max(maxdesc, desc);
1359 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1363 // Check if any custom fonts are larger (Asger)
1364 // This is not completely correct, but we can live with the small,
1365 // cosmetic error for now.
1366 LyXFont::FONT_SIZE maxsize =
1367 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1368 if (maxsize > font.size()) {
1369 font.setSize(maxsize);
1371 asc = lyxfont::maxAscent(font);
1372 desc = lyxfont::maxDescent(font);
1379 // This is nicer with box insets:
1383 row_ptr->ascent_of_text(maxasc);
1385 // is it a top line?
1386 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1388 // some parksips VERY EASY IMPLEMENTATION
1389 if (bview->buffer()->params.paragraph_separation ==
1390 BufferParams::PARSEP_SKIP)
1392 if (layout.isParagraph()
1393 && firstpar->getDepth() == 0
1394 && firstpar->previous())
1396 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1397 } else if (firstpar->previous() &&
1398 textclasslist[bview->buffer()->params.textclass][firstpar->previous()->layout()].isParagraph() &&
1399 firstpar->previous()->getDepth() == 0)
1401 // is it right to use defskip here too? (AS)
1402 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1406 // the paper margins
1407 if (!row_ptr->par()->previous() && bv_owner)
1408 maxasc += LYX_PAPER_MARGIN;
1410 // add the vertical spaces, that the user added
1411 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1413 // do not forget the DTP-lines!
1414 // there height depends on the font of the nearest character
1415 if (firstpar->params().lineTop())
1416 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1419 // and now the pagebreaks
1420 if (firstpar->params().pagebreakTop())
1421 maxasc += 3 * defaultHeight();
1423 // This is special code for the chapter, since the label of this
1424 // layout is printed in an extra row
1425 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1426 && bview->buffer()->params.secnumdepth >= 0)
1428 float spacing_val = 1.0;
1429 if (!row_ptr->par()->params().spacing().isDefault()) {
1430 spacing_val = row_ptr->par()->params().spacing().getValue();
1432 spacing_val = bview->buffer()->params.spacing.getValue();
1435 labeladdon = int(lyxfont::maxDescent(labelfont) *
1436 layout.spacing.getValue() *
1438 + int(lyxfont::maxAscent(labelfont) *
1439 layout.spacing.getValue() *
1443 // special code for the top label
1444 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1445 || layout.labeltype == LABEL_BIBLIO
1446 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1447 && row_ptr->par()->isFirstInSequence()
1448 && !row_ptr->par()->getLabelstring().empty())
1450 float spacing_val = 1.0;
1451 if (!row_ptr->par()->params().spacing().isDefault()) {
1452 spacing_val = row_ptr->par()->params().spacing().getValue();
1454 spacing_val = bview->buffer()->params.spacing.getValue();
1458 (lyxfont::maxAscent(labelfont) *
1459 layout.spacing.getValue() *
1461 +(lyxfont::maxDescent(labelfont) *
1462 layout.spacing.getValue() *
1464 + layout.topsep * defaultHeight()
1465 + layout.labelbottomsep * defaultHeight());
1468 // and now the layout spaces, for example before and after a section,
1469 // or between the items of a itemize or enumerate environment
1471 if (!firstpar->params().pagebreakTop()) {
1472 Paragraph * prev = row_ptr->par()->previous();
1474 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1475 if (prev && prev->layout() == firstpar->layout() &&
1476 prev->getDepth() == firstpar->getDepth() &&
1477 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1479 layoutasc = (layout.itemsep * defaultHeight());
1480 } else if (row_ptr->previous()) {
1481 tmptop = layout.topsep;
1483 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1484 tmptop -= textclasslist[bview->buffer()->params.textclass][row_ptr->previous()->par()->layout()].bottomsep;
1487 layoutasc = (tmptop * defaultHeight());
1488 } else if (row_ptr->par()->params().lineTop()) {
1489 tmptop = layout.topsep;
1492 layoutasc = (tmptop * defaultHeight());
1495 prev = row_ptr->par()->outerHook();
1497 maxasc += int(textclasslist[bview->buffer()->params.textclass][prev->layout()].parsep * defaultHeight());
1499 if (firstpar->previous() &&
1500 firstpar->previous()->getDepth() == 0 &&
1501 firstpar->previous()->layout() !=
1505 } else if (firstpar->previous()) {
1506 maxasc += int(layout.parsep * defaultHeight());
1512 // is it a bottom line?
1513 if (row_ptr->par() == par
1514 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1516 // the paper margins
1517 if (!par->next() && bv_owner)
1518 maxdesc += LYX_PAPER_MARGIN;
1520 // add the vertical spaces, that the user added
1521 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1523 // do not forget the DTP-lines!
1524 // there height depends on the font of the nearest character
1525 if (firstpar->params().lineBottom())
1526 maxdesc += 2 * lyxfont::ascent('x',
1527 getFont(bview->buffer(),
1529 max(pos_type(0), par->size() - 1)));
1531 // and now the pagebreaks
1532 if (firstpar->params().pagebreakBottom())
1533 maxdesc += 3 * defaultHeight();
1535 // and now the layout spaces, for example before and after
1536 // a section, or between the items of a itemize or enumerate
1538 if (!firstpar->params().pagebreakBottom()
1539 && row_ptr->par()->next()) {
1540 Paragraph * nextpar = row_ptr->par()->next();
1541 Paragraph * comparepar = row_ptr->par();
1545 if (comparepar->getDepth() > nextpar->getDepth()) {
1546 usual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1547 comparepar = comparepar->depthHook(nextpar->getDepth());
1548 if (comparepar->layout()!= nextpar->layout()
1549 || nextpar->getLabelWidthString() !=
1550 comparepar->getLabelWidthString())
1552 unusual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1554 if (unusual > usual)
1555 layoutdesc = unusual;
1558 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1560 if (comparepar->layout() != nextpar->layout()
1561 || nextpar->getLabelWidthString() !=
1562 comparepar->getLabelWidthString())
1563 layoutdesc = int(textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1568 // incalculate the layout spaces
1569 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1570 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1572 // calculate the new height of the text
1573 height -= row_ptr->height();
1575 row_ptr->height(maxasc + maxdesc + labeladdon);
1576 row_ptr->baseline(maxasc + labeladdon);
1578 height += row_ptr->height();
1580 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1583 row_ptr->width(maxwidth);
1584 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1586 row_ptr->width(int(maxwidth + x));
1589 width = max(0,workWidth(bview));
1591 if (r->width() > width)
1599 /* Appends the implicit specified paragraph behind the specified row,
1600 * start at the implicit given position */
1601 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1603 bool not_ready = true;
1605 // The last character position of a paragraph is an invariant so we can
1606 // safely get it here. (Asger)
1607 pos_type const lastposition = row->par()->size();
1609 // Get the next breakpoint
1610 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1614 // Insert the new row
1615 if (z < lastposition) {
1617 insertRow(row, row->par(), z);
1624 // Set the dimensions of the row
1625 // fixed fill setting now by calling inset->update() in
1626 // SingleWidth when needed!
1627 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1628 setHeightOfRow(bview, tmprow);
1630 } while (not_ready);
1634 void LyXText::breakAgain(BufferView * bview, Row * row) const
1636 bool not_ready = true;
1639 // get the next breakpoint
1640 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1643 if (z < row->par()->size()) {
1644 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1647 insertRow(row, row->par(), z);
1653 if (row->pos() == z)
1654 not_ready = false; // the rest will not change
1660 /* if there are some rows too much, delete them */
1661 /* only if you broke the whole paragraph! */
1662 Row * tmprow2 = row;
1663 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1664 tmprow2 = tmprow2->next();
1666 while (tmprow2 != row) {
1667 tmprow2 = tmprow2->previous();
1668 removeRow(tmprow2->next());
1673 /* set the dimensions of the row */
1674 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1675 setHeightOfRow(bview, tmprow);
1676 } while (not_ready);
1680 // this is just a little changed version of break again
1681 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1683 // get the next breakpoint
1684 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1687 if (z < row->par()->size()) {
1689 || (row->next() && row->next()->par() != row->par())) {
1690 /* insert a new row */
1692 insertRow(row, row->par(), z);
1698 if (row->pos() != z)
1702 // if there are some rows too much, delete them
1703 // only if you broke the whole paragraph!
1704 Row * tmprow2 = row;
1705 while (tmprow2->next()
1706 && tmprow2->next()->par() == row->par()) {
1707 tmprow2 = tmprow2->next();
1709 while (tmprow2 != row) {
1710 tmprow2 = tmprow2->previous();
1711 removeRow(tmprow2->next());
1715 // set the dimensions of the row
1716 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1717 setHeightOfRow(bview, tmprow);
1721 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1723 LyXTextClass const & tclass =
1724 textclasslist[bview->buffer()->params.textclass];
1725 LyXLayout const & layout = tclass[cursor.par()->layout()];
1727 // this is only allowed, if the current paragraph is not empty or caption
1728 // and if it has not the keepempty flag aktive
1729 if ((cursor.par()->size() <= 0)
1730 && layout.labeltype != LABEL_SENSITIVE
1731 && !layout.keepempty)
1734 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1736 // Always break behind a space
1738 // It is better to erase the space (Dekel)
1739 if (cursor.pos() < cursor.par()->size()
1740 && cursor.par()->isLineSeparator(cursor.pos()))
1741 cursor.par()->erase(cursor.pos());
1742 // cursor.pos(cursor.pos() + 1);
1744 // break the paragraph
1748 keep_layout = layout.isEnvironment();
1749 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1752 // well this is the caption hack since one caption is really enough
1753 if (layout.labeltype == LABEL_SENSITIVE) {
1755 // set to standard-layout
1756 cursor.par()->applyLayout(tclass.defaultLayoutName());
1758 // set to standard-layout
1759 cursor.par()->next()->applyLayout(tclass.defaultLayoutName());
1762 /* if the cursor is at the beginning of a row without prior newline,
1764 * This touches only the screen-update. Otherwise we would may have
1765 * an empty row on the screen */
1766 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1767 && cursor.row()->pos() == cursor.pos())
1772 status(bview, LyXText::NEED_MORE_REFRESH);
1773 refresh_row = cursor.row();
1774 refresh_y = cursor.y() - cursor.row()->baseline();
1776 // Do not forget the special right address boxes
1777 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1778 while (refresh_row->previous() &&
1779 refresh_row->previous()->par() == refresh_row->par()) {
1780 refresh_row = refresh_row->previous();
1781 refresh_y -= refresh_row->height();
1784 removeParagraph(cursor.row());
1786 // set the dimensions of the cursor row
1787 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1789 setHeightOfRow(bview, cursor.row());
1791 while (cursor.par()->next()->size()
1792 && cursor.par()->next()->isNewline(0))
1793 cursor.par()->next()->erase(0);
1795 insertParagraph(bview, cursor.par()->next(), cursor.row());
1797 updateCounters(bview, cursor.row()->previous());
1799 /* This check is necessary. Otherwise the new empty paragraph will
1800 * be deleted automatically. And it is more friendly for the user! */
1801 if (cursor.pos() || layout.keepempty)
1802 setCursor(bview, cursor.par()->next(), 0);
1804 setCursor(bview, cursor.par(), 0);
1806 if (cursor.row()->next())
1807 breakAgain(bview, cursor.row()->next());
1813 // Just a macro to make some thing easier.
1814 void LyXText::redoParagraph(BufferView * bview) const
1817 redoParagraphs(bview, cursor, cursor.par()->next());
1818 setCursorIntern(bview, cursor.par(), cursor.pos());
1822 /* insert a character, moves all the following breaks in the
1823 * same Paragraph one to the right and make a rebreak */
1824 void LyXText::insertChar(BufferView * bview, char c)
1826 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1828 // When the free-spacing option is set for the current layout,
1829 // disable the double-space checking
1831 bool const freeSpacing =
1832 textclasslist[bview->buffer()->params.textclass][cursor.row()->par()->layout()].free_spacing ||
1833 cursor.row()->par()->isFreeSpacing();
1835 if (lyxrc.auto_number) {
1836 static string const number_operators = "+-/*";
1837 static string const number_unary_operators = "+-";
1838 static string const number_seperators = ".,:";
1840 if (current_font.number() == LyXFont::ON) {
1841 if (!IsDigit(c) && !contains(number_operators, c) &&
1842 !(contains(number_seperators, c) &&
1843 cursor.pos() >= 1 &&
1844 cursor.pos() < cursor.par()->size() &&
1845 getFont(bview->buffer(),
1847 cursor.pos()).number() == LyXFont::ON &&
1848 getFont(bview->buffer(),
1850 cursor.pos() - 1).number() == LyXFont::ON)
1852 number(bview); // Set current_font.number to OFF
1853 } else if (IsDigit(c) &&
1854 real_current_font.isVisibleRightToLeft()) {
1855 number(bview); // Set current_font.number to ON
1857 if (cursor.pos() > 0) {
1858 char const c = cursor.par()->getChar(cursor.pos() - 1);
1859 if (contains(number_unary_operators, c) &&
1860 (cursor.pos() == 1 ||
1861 cursor.par()->isSeparator(cursor.pos() - 2) ||
1862 cursor.par()->isNewline(cursor.pos() - 2))
1864 setCharFont(bview->buffer(),
1868 } else if (contains(number_seperators, c) &&
1869 cursor.pos() >= 2 &&
1870 getFont(bview->buffer(),
1872 cursor.pos() - 2).number() == LyXFont::ON) {
1873 setCharFont(bview->buffer(),
1883 /* First check, if there will be two blanks together or a blank at
1884 the beginning of a paragraph.
1885 I decided to handle blanks like normal characters, the main
1886 difference are the special checks when calculating the row.fill
1887 (blank does not count at the end of a row) and the check here */
1889 // The bug is triggered when we type in a description environment:
1890 // The current_font is not changed when we go from label to main text
1891 // and it should (along with realtmpfont) when we type the space.
1892 // CHECK There is a bug here! (Asger)
1894 LyXFont realtmpfont = real_current_font;
1895 LyXFont rawtmpfont = current_font; /* store the current font.
1896 * This is because of the use
1897 * of cursor movements. The moving
1898 * cursor would refresh the
1901 // Get the font that is used to calculate the baselineskip
1902 pos_type const lastpos = cursor.par()->size();
1903 LyXFont rawparfont =
1904 cursor.par()->getFontSettings(bview->buffer()->params,
1907 bool jumped_over_space = false;
1909 if (!freeSpacing && IsLineSeparatorChar(c)) {
1910 if ((cursor.pos() > 0
1911 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1912 || (cursor.pos() > 0
1913 && cursor.par()->isNewline(cursor.pos() - 1))
1914 || (cursor.pos() == 0)) {
1915 static bool sent_space_message = false;
1916 if (!sent_space_message) {
1917 if (cursor.pos() == 0)
1918 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1920 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1921 sent_space_message = true;
1926 } else if (IsNewlineChar(c)) {
1927 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1933 /* No newline at first position
1934 * of a paragraph or behind labels.
1935 * TeX does not allow that */
1937 if (cursor.pos() < cursor.par()->size() &&
1938 cursor.par()->isLineSeparator(cursor.pos()))
1939 // newline always after a blank!
1941 cursor.row()->fill(-1); // to force a new break
1944 // the display inset stuff
1945 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1946 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1947 if (inset && (inset->display() || inset->needFullRow())) {
1948 // force a new break
1949 cursor.row()->fill(-1); // to force a new break
1953 // get the cursor row fist
1954 Row * row = cursor.row();
1955 int y = cursor.y() - row->baseline();
1956 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1957 * already insertet the character */
1958 cursor.par()->insertChar(cursor.pos(), c);
1959 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1961 if (!jumped_over_space) {
1962 // refresh the positions
1964 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1965 tmprow = tmprow->next();
1966 tmprow->pos(tmprow->pos() + 1);
1970 // Is there a break one row above
1971 if (row->previous() && row->previous()->par() == row->par()
1972 && (cursor.par()->isLineSeparator(cursor.pos())
1973 || cursor.par()->isNewline(cursor.pos())
1974 || ((cursor.pos() < cursor.par()->size()) &&
1975 cursor.par()->isInset(cursor.pos()+1))
1976 || cursor.row()->fill() == -1))
1978 pos_type z = nextBreakPoint(bview,
1981 if (z >= row->pos()) {
1984 // set the dimensions of the row above
1985 row->previous()->fill(fill(bview,
1989 setHeightOfRow(bview, row->previous());
1991 y -= row->previous()->height();
1993 refresh_row = row->previous();
1994 status(bview, LyXText::NEED_MORE_REFRESH);
1996 breakAgainOneRow(bview, row);
1998 current_font = rawtmpfont;
1999 real_current_font = realtmpfont;
2000 setCursor(bview, cursor.par(), cursor.pos() + 1,
2001 false, cursor.boundary());
2002 // cursor MUST be in row now.
2004 if (row->next() && row->next()->par() == row->par())
2005 need_break_row = row->next();
2009 // check, wether the last characters font has changed.
2010 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2011 && rawparfont != rawtmpfont)
2012 redoHeightOfParagraph(bview, cursor);
2019 // recalculate the fill of the row
2020 if (row->fill() >= 0) /* needed because a newline
2021 * will set fill to -1. Otherwise
2022 * we would not get a rebreak! */
2023 row->fill(fill(bview, row, workWidth(bview)));
2024 if (c == Paragraph::META_INSET || row->fill() < 0) {
2027 refresh_x = cursor.x();
2028 refresh_pos = cursor.pos();
2029 status(bview, LyXText::NEED_MORE_REFRESH);
2030 breakAgainOneRow(bview, row);
2031 // will the cursor be in another row now?
2032 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2033 if (row->next() && row->next()->par() == row->par())
2034 // this should always be true
2036 breakAgainOneRow(bview, row);
2038 current_font = rawtmpfont;
2039 real_current_font = realtmpfont;
2041 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2043 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2044 != cursor.boundary())
2045 setCursor(bview, cursor.par(), cursor.pos(), false,
2046 !cursor.boundary());
2047 if (row->next() && row->next()->par() == row->par())
2048 need_break_row = row->next();
2053 refresh_x = cursor.x();
2055 refresh_pos = cursor.pos();
2057 int const tmpheight = row->height();
2058 setHeightOfRow(bview, row);
2059 if (tmpheight == row->height())
2060 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2062 status(bview, LyXText::NEED_MORE_REFRESH);
2064 current_font = rawtmpfont;
2065 real_current_font = realtmpfont;
2066 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2070 // check, wether the last characters font has changed.
2071 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2072 && rawparfont != rawtmpfont) {
2073 redoHeightOfParagraph(bview, cursor);
2075 // now the special right address boxes
2076 if (textclasslist[bview->buffer()->params.textclass][cursor.par()->layout()].margintype
2077 == MARGIN_RIGHT_ADDRESS_BOX) {
2078 redoDrawingOfParagraph(bview, cursor);
2086 void LyXText::charInserted()
2088 // Here we could call FinishUndo for every 20 characters inserted.
2089 // This is from my experience how emacs does it.
2090 static unsigned int counter;
2100 void LyXText::prepareToPrint(BufferView * bview,
2101 Row * row, float & x,
2102 float & fill_separator,
2104 float & fill_label_hfill,
2110 float w = row->fill();
2112 fill_label_hfill = 0;
2114 fill_label_hfill = 0;
2117 row->par()->isRightToLeftPar(bview->buffer()->params);
2119 x = (workWidth(bview) > 0)
2120 ? rightMargin(bview->buffer(), row) : 0;
2122 x = (workWidth(bview) > 0)
2123 ? leftMargin(bview, row) : 0;
2125 // is there a manual margin with a manual label
2126 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2127 LyXLayout const & layout = tclass[row->par()->layout()];
2129 if (layout.margintype == MARGIN_MANUAL
2130 && layout.labeltype == LABEL_MANUAL) {
2131 // one more since labels are left aligned
2132 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2133 if (nlh && !row->par()->getLabelWidthString().empty()) {
2134 fill_label_hfill = labelFill(bview, row) / nlh;
2138 // are there any hfills in the row?
2139 float const nh = numberOfHfills(bview->buffer(), row);
2143 fill_hfill = w / nh;
2144 // we don't have to look at the alignment if it is ALIGN_LEFT and
2145 // if the row is already larger then the permitted width as then
2146 // we force the LEFT_ALIGN'edness!
2147 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2148 // is it block, flushleft or flushright?
2149 // set x how you need it
2151 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2152 align = layout.align;
2154 align = row->par()->params().align();
2157 // center displayed insets
2159 if (row->par()->isInset(row->pos())
2160 && (inset=row->par()->getInset(row->pos()))
2161 && (inset->display())) // || (inset->scroll() < 0)))
2162 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2163 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2164 // ERT insets should always be LEFT ALIGNED on screen
2165 inset = row->par()->inInset();
2166 if (inset && inset->owner() &&
2167 inset->owner()->lyxCode() == Inset::ERT_CODE)
2169 align = LYX_ALIGN_LEFT;
2173 case LYX_ALIGN_BLOCK:
2174 ns = numberOfSeparators(bview->buffer(), row);
2175 if (ns && row->next() && row->next()->par() == row->par() &&
2176 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2177 && !(row->next()->par()->isInset(row->next()->pos())
2178 && row->next()->par()->getInset(row->next()->pos())
2179 && row->next()->par()->getInset(row->next()->pos())->display())
2182 fill_separator = w / ns;
2183 } else if (is_rtl) {
2187 case LYX_ALIGN_RIGHT:
2190 case LYX_ALIGN_CENTER:
2198 computeBidiTables(bview->buffer(), row);
2200 pos_type main_body =
2201 beginningOfMainBody(bview->buffer(), row->par());
2202 pos_type last = rowLast(row);
2204 if (main_body > 0 &&
2205 (main_body - 1 > last ||
2206 !row->par()->isLineSeparator(main_body - 1))) {
2207 x += lyxfont::width(layout.labelsep,
2208 getLabelFont(bview->buffer(), row->par()));
2209 if (main_body - 1 <= last)
2210 x += fill_label_hfill;
2215 /* important for the screen */
2218 /* the cursor set functions have a special mechanism. When they
2219 * realize, that you left an empty paragraph, they will delete it.
2220 * They also delete the corresponding row */
2222 void LyXText::cursorRightOneWord(BufferView * bview) const
2224 // treat floats, HFills and Insets as words
2225 LyXCursor tmpcursor = cursor;
2226 // CHECK See comment on top of text.C
2228 if (tmpcursor.pos() == tmpcursor.par()->size()
2229 && tmpcursor.par()->next()) {
2230 tmpcursor.par(tmpcursor.par()->next());
2235 // Skip through initial nonword stuff.
2236 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2237 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2238 // printf("Current pos1 %d", tmpcursor.pos()) ;
2239 tmpcursor.pos(tmpcursor.pos() + 1);
2242 // Advance through word.
2243 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2244 tmpcursor.par()->isWord(tmpcursor.pos())) {
2245 // printf("Current pos2 %d", tmpcursor.pos()) ;
2246 tmpcursor.pos(tmpcursor.pos() + 1);
2250 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2254 void LyXText::cursorTab(BufferView * bview) const
2256 LyXCursor tmpcursor = cursor;
2257 while (tmpcursor.pos() < tmpcursor.par()->size()
2258 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2259 tmpcursor.pos(tmpcursor.pos() + 1);
2261 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2262 if (tmpcursor.par()->next()) {
2263 tmpcursor.par(tmpcursor.par()->next());
2267 tmpcursor.pos(tmpcursor.pos() + 1);
2268 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2272 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2273 of prior word, not to end of next prior word. */
2275 void LyXText::cursorLeftOneWord(BufferView * bview) const
2277 LyXCursor tmpcursor = cursor;
2278 cursorLeftOneWord(tmpcursor);
2279 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2282 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2284 // treat HFills, floats and Insets as words
2287 && (cur.par()->isSeparator(cur.pos() - 1)
2288 || cur.par()->isKomma(cur.pos() - 1))
2289 && !(cur.par()->isHfill(cur.pos() - 1)
2290 || cur.par()->isInset(cur.pos() - 1)))
2291 cur.pos(cur.pos() - 1);
2294 && (cur.par()->isInset(cur.pos() - 1)
2295 || cur.par()->isHfill(cur.pos() - 1))) {
2296 cur.pos(cur.pos() - 1);
2297 } else if (!cur.pos()) {
2298 if (cur.par()->previous()) {
2299 cur.par(cur.par()->previous());
2300 cur.pos(cur.par()->size());
2302 } else { // Here, cur != 0
2303 while (cur.pos() > 0 &&
2304 cur.par()->isWord(cur.pos()-1))
2305 cur.pos(cur.pos() - 1);
2309 /* -------> Select current word. This depends on behaviour of
2310 CursorLeftOneWord(), so it is patched as well. */
2311 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2312 word_location const loc) const
2314 // first put the cursor where we wana start to select the word
2317 case WHOLE_WORD_STRICT:
2318 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2319 || cursor.par()->isSeparator(cursor.pos())
2320 || cursor.par()->isKomma(cursor.pos())
2321 || cursor.par()->isSeparator(cursor.pos() -1)
2322 || cursor.par()->isKomma(cursor.pos() -1)) {
2326 // no break here, we go to the next
2329 // Move cursor to the beginning, when not already there.
2330 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2331 && !from.par()->isKomma(from.pos() - 1))
2332 cursorLeftOneWord(from);
2335 // always move the cursor to the beginning of previous word
2336 cursorLeftOneWord(from);
2339 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2345 while (to.pos() < to.par()->size()
2346 && !to.par()->isSeparator(to.pos())
2347 && !to.par()->isKomma(to.pos())
2348 && !to.par()->isHfill(to.pos()))
2350 to.pos(to.pos() + 1);
2355 void LyXText::selectWord(BufferView * bview, word_location const loc)
2359 getWord(from, to, loc);
2361 setCursor(bview, from.par(), from.pos());
2364 selection.cursor = cursor;
2365 setCursor(bview, to.par(), to.pos());
2366 setSelection(bview);
2370 /* -------> Select the word currently under the cursor when no
2371 selection is currently set */
2372 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2373 word_location const loc)
2375 if (!selection.set()) {
2376 selectWord(bview, loc);
2377 return selection.set();
2383 // This function is only used by the spellchecker for NextWord().
2384 // It doesn't handle LYX_ACCENTs and probably never will.
2385 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2386 float & value) const
2388 if (the_locking_inset) {
2389 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2391 value += float(cursor.y())/float(height);
2394 // we have to go on checking so move cusor to the next char
2395 if (cursor.pos() == cursor.par()->size()) {
2396 if (!cursor.par()->next())
2398 cursor.par(cursor.par()->next());
2401 cursor.pos(cursor.pos() + 1);
2403 Paragraph * tmppar = cursor.par();
2405 // If this is not the very first word, skip rest of
2406 // current word because we are probably in the middle
2407 // of a word if there is text here.
2408 if (cursor.pos() || cursor.par()->previous()) {
2409 while (cursor.pos() < cursor.par()->size()
2410 && cursor.par()->isLetter(cursor.pos()))
2411 cursor.pos(cursor.pos() + 1);
2414 // Now, skip until we have real text (will jump paragraphs)
2415 while ((cursor.par()->size() > cursor.pos()
2416 && (!cursor.par()->isLetter(cursor.pos()))
2417 && (!cursor.par()->isInset(cursor.pos()) ||
2418 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2419 || (cursor.par()->size() == cursor.pos()
2420 && cursor.par()->next()))
2422 if (cursor.pos() == cursor.par()->size()) {
2423 cursor.par(cursor.par()->next());
2426 cursor.pos(cursor.pos() + 1);
2429 // now check if we hit an inset so it has to be a inset containing text!
2430 if (cursor.pos() < cursor.par()->size() &&
2431 cursor.par()->isInset(cursor.pos()))
2434 cursor.par()->getInset(cursor.pos())->edit(bview);
2435 // now call us again to do the above trick
2436 // but obviously we have to start from down below ;)
2437 return bview->text->selectNextWordToSpellcheck(bview, value);
2440 // Update the value if we changed paragraphs
2441 if (cursor.par() != tmppar) {
2442 setCursor(bview, cursor.par(), cursor.pos());
2443 value = float(cursor.y())/float(height);
2446 // Start the selection from here
2447 selection.cursor = cursor;
2449 // and find the end of the word (insets like optional hyphens
2450 // and ligature break are part of a word)
2451 while (cursor.pos() < cursor.par()->size()
2452 && (cursor.par()->isLetter(cursor.pos())))
2453 cursor.pos(cursor.pos() + 1);
2455 // Finally, we copy the word to a string and return it
2457 if (selection.cursor.pos() < cursor.pos()) {
2459 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2460 if (!cursor.par()->isInset(i))
2461 str += cursor.par()->getChar(i);
2468 // This one is also only for the spellchecker
2469 void LyXText::selectSelectedWord(BufferView * bview)
2471 if (the_locking_inset) {
2472 the_locking_inset->selectSelectedWord(bview);
2475 // move cursor to the beginning
2476 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2478 // set the sel cursor
2479 selection.cursor = cursor;
2481 // now find the end of the word
2482 while (cursor.pos() < cursor.par()->size()
2483 && (cursor.par()->isLetter(cursor.pos())))
2484 cursor.pos(cursor.pos() + 1);
2486 setCursor(bview, cursor.par(), cursor.pos());
2488 // finally set the selection
2489 setSelection(bview);
2493 /* -------> Delete from cursor up to the end of the current or next word. */
2494 void LyXText::deleteWordForward(BufferView * bview)
2496 if (!cursor.par()->size())
2499 LyXCursor tmpcursor = cursor;
2500 tmpcursor.row(0); //Â ??
2501 selection.set(true); // to avoid deletion
2502 cursorRightOneWord(bview);
2503 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2504 selection.cursor = cursor;
2506 setSelection(bview);
2508 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2509 cutSelection(bview, true, false);
2514 /* -------> Delete from cursor to start of current or prior word. */
2515 void LyXText::deleteWordBackward(BufferView * bview)
2517 if (!cursor.par()->size())
2520 LyXCursor tmpcursor = cursor;
2521 tmpcursor.row(0); // ??
2522 selection.set(true); // to avoid deletion
2523 cursorLeftOneWord(bview);
2524 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2525 selection.cursor = cursor;
2527 setSelection(bview);
2528 cutSelection(bview, true, false);
2533 /* -------> Kill to end of line. */
2534 void LyXText::deleteLineForward(BufferView * bview)
2536 if (!cursor.par()->size())
2537 // Paragraph is empty, so we just go to the right
2540 LyXCursor tmpcursor = cursor;
2541 // We can't store the row over a regular setCursor
2542 // so we set it to 0 and reset it afterwards.
2543 tmpcursor.row(0); //Â ??
2544 selection.set(true); // to avoid deletion
2546 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2547 selection.cursor = cursor;
2549 setSelection(bview);
2550 // What is this test for ??? (JMarc)
2551 if (!selection.set()) {
2552 deleteWordForward(bview);
2554 cutSelection(bview, true, false);
2560 // Change the case of a word at cursor position.
2561 // This function directly manipulates Paragraph::text because there
2562 // is no Paragraph::SetChar currently. I did what I could to ensure
2563 // that it is correct. I guess part of it should be moved to
2564 // Paragraph, but it will have to change for 1.1 anyway. At least
2565 // it does not access outside of the allocated array as the older
2566 // version did. (JMarc)
2567 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2572 if (selection.set()) {
2573 from = selection.start;
2576 getWord(from, to, PARTIAL_WORD);
2577 setCursor(bview, to.par(), to.pos() + 1);
2580 changeRegionCase(bview, from, to, action);
2584 void LyXText::changeRegionCase(BufferView * bview,
2585 LyXCursor const & from,
2586 LyXCursor const & to,
2587 LyXText::TextCase action)
2589 lyx::Assert(from <= to);
2591 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2593 pos_type pos = from.pos();
2594 Paragraph * par = from.par();
2596 while (par && (pos != to.pos() || par != to.par())) {
2597 unsigned char c = par->getChar(pos);
2598 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2600 case text_lowercase:
2603 case text_capitalization:
2605 action = text_lowercase;
2607 case text_uppercase:
2612 par->setChar(pos, c);
2613 checkParagraph(bview, par, pos);
2616 if (pos == par->size()) {
2621 if (to.row() != from.row()) {
2622 refresh_y = from.y() - from.row()->baseline();
2623 refresh_row = from.row();
2624 status(bview, LyXText::NEED_MORE_REFRESH);
2629 void LyXText::transposeChars(BufferView & bview)
2631 Paragraph * tmppar = cursor.par();
2633 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2635 pos_type tmppos = cursor.pos();
2637 // First decide if it is possible to transpose at all
2639 // We are at the beginning of a paragraph.
2640 if (tmppos == 0) return;
2642 // We are at the end of a paragraph.
2643 if (tmppos == tmppar->size() - 1) return;
2645 unsigned char c1 = tmppar->getChar(tmppos);
2646 unsigned char c2 = tmppar->getChar(tmppos - 1);
2648 if (c1 != Paragraph::META_INSET
2649 && c2 != Paragraph::META_INSET) {
2650 tmppar->setChar(tmppos, c2);
2651 tmppar->setChar(tmppos - 1, c1);
2653 // We should have an implementation that handles insets
2654 // as well, but that will have to come later. (Lgb)
2655 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2659 void LyXText::Delete(BufferView * bview)
2661 // this is a very easy implementation
2663 LyXCursor old_cursor = cursor;
2664 int const old_cur_par_id = old_cursor.par()->id();
2665 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2666 old_cursor.par()->previous()->id() : 0;
2668 // just move to the right
2671 // CHECK Look at the comment here.
2672 // This check is not very good...
2673 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2674 // and that can very well delete the par or par->previous in
2675 // old_cursor. Will a solution where we compare paragraph id's
2677 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2678 == old_cur_par_prev_id
2679 && cursor.par()->id() != old_cur_par_id) {
2680 // delete-empty-paragraph-mechanism has done it
2684 // if you had success make a backspace
2685 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2686 LyXCursor tmpcursor = cursor;
2687 // to make sure undo gets the right cursor position
2688 cursor = old_cursor;
2689 setUndo(bview, Undo::DELETE,
2690 cursor.par(), cursor.par()->next());
2697 void LyXText::backspace(BufferView * bview)
2699 // Get the font that is used to calculate the baselineskip
2700 pos_type lastpos = cursor.par()->size();
2701 LyXFont rawparfont =
2702 cursor.par()->getFontSettings(bview->buffer()->params,
2705 if (cursor.pos() == 0) {
2706 // The cursor is at the beginning of a paragraph,
2707 // so the the backspace will collapse two paragraphs into one.
2709 // we may paste some paragraphs
2711 // is it an empty paragraph?
2714 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2715 // This is an empty paragraph and we delete it just by moving the cursor one step
2716 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2717 // of the paragraph.
2719 if (cursor.par()->previous()) {
2720 Paragraph * tmppar = cursor.par()->previous();
2721 if (cursor.par()->layout() == tmppar->layout()
2722 && cursor.par()->getAlign() == tmppar->getAlign()) {
2723 // Inherit bottom DTD from the paragraph below.
2724 // (the one we are deleting)
2725 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2726 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2727 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2732 // the layout things can change the height of a row !
2733 int const tmpheight = cursor.row()->height();
2734 setHeightOfRow(bview, cursor.row());
2735 if (cursor.row()->height() != tmpheight) {
2736 refresh_y = cursor.y() - cursor.row()->baseline();
2737 refresh_row = cursor.row();
2738 status(bview, LyXText::NEED_MORE_REFRESH);
2744 if (cursor.par()->previous()) {
2745 setUndo(bview, Undo::DELETE,
2746 cursor.par()->previous(), cursor.par()->next());
2749 Paragraph * tmppar = cursor.par();
2750 Row * tmprow = cursor.row();
2752 // We used to do cursorLeftIntern() here, but it is
2753 // not a good idea since it triggers the auto-delete
2754 // mechanism. So we do a cursorLeftIntern()-lite,
2755 // without the dreaded mechanism. (JMarc)
2756 if (cursor.par()->previous()) {
2757 // steps into the above paragraph.
2758 setCursorIntern(bview, cursor.par()->previous(),
2759 cursor.par()->previous()->size(),
2763 /* Pasting is not allowed, if the paragraphs have different
2764 layout. I think it is a real bug of all other
2765 word processors to allow it. It confuses the user.
2766 Even so with a footnote paragraph and a non-footnote
2767 paragraph. I will not allow pasting in this case,
2768 because the user would be confused if the footnote behaves
2769 different wether it is open or closed.
2771 Correction: Pasting is always allowed with standard-layout
2773 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2775 if (cursor.par() != tmppar
2776 && (cursor.par()->layout() == tmppar->layout()
2777 || tmppar->layout() == tclass.defaultLayoutName())
2778 && cursor.par()->getAlign() == tmppar->getAlign()) {
2779 removeParagraph(tmprow);
2781 cursor.par()->pasteParagraph(bview->buffer()->params);
2783 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2784 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2785 // strangely enough it seems that commenting out the line above removes
2786 // most or all of the segfaults. I will however also try to move the
2787 // two Remove... lines in front of the PasteParagraph too.
2790 cursor.pos(cursor.pos() - 1);
2792 status(bview, LyXText::NEED_MORE_REFRESH);
2793 refresh_row = cursor.row();
2794 refresh_y = cursor.y() - cursor.row()->baseline();
2796 // remove the lost paragraph
2797 // This one is not safe, since the paragraph that the tmprow and the
2798 // following rows belong to has been deleted by the PasteParagraph
2799 // above. The question is... could this be moved in front of the
2801 //RemoveParagraph(tmprow);
2802 //RemoveRow(tmprow);
2804 // This rebuilds the rows.
2805 appendParagraph(bview, cursor.row());
2806 updateCounters(bview, cursor.row());
2808 // the row may have changed, block, hfills etc.
2809 setCursor(bview, cursor.par(), cursor.pos(), false);
2812 /* this is the code for a normal backspace, not pasting
2814 setUndo(bview, Undo::DELETE,
2815 cursor.par(), cursor.par()->next());
2816 // We used to do cursorLeftIntern() here, but it is
2817 // not a good idea since it triggers the auto-delete
2818 // mechanism. So we do a cursorLeftIntern()-lite,
2819 // without the dreaded mechanism. (JMarc)
2820 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2821 false, cursor.boundary());
2823 // some insets are undeletable here
2824 if (cursor.par()->isInset(cursor.pos())) {
2825 if (!cursor.par()->getInset(cursor.pos())->deletable())
2827 // force complete redo when erasing display insets
2828 // this is a cruel method but safe..... Matthias
2829 if (cursor.par()->getInset(cursor.pos())->display() ||
2830 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2831 cursor.par()->erase(cursor.pos());
2832 redoParagraph(bview);
2837 Row * row = cursor.row();
2838 int y = cursor.y() - row->baseline();
2840 /* remember that a space at the end of a row doesnt count
2841 * when calculating the fill */
2842 if (cursor.pos() < rowLast(row) ||
2843 !cursor.par()->isLineSeparator(cursor.pos())) {
2844 row->fill(row->fill() + singleWidth(bview,
2849 /* some special code when deleting a newline. This is similar
2850 * to the behavior when pasting paragraphs */
2851 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2852 cursor.par()->erase(cursor.pos());
2853 // refresh the positions
2855 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2856 tmprow = tmprow->next();
2857 tmprow->pos(tmprow->pos() - 1);
2859 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2860 cursor.pos(cursor.pos() - 1);
2862 if (cursor.pos() < cursor.par()->size()
2863 && !cursor.par()->isSeparator(cursor.pos())) {
2864 cursor.par()->insertChar(cursor.pos(), ' ');
2865 setCharFont(bview->buffer(), cursor.par(),
2866 cursor.pos(), current_font);
2867 // refresh the positions
2869 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2870 tmprow = tmprow->next();
2871 tmprow->pos(tmprow->pos() + 1);
2875 cursor.par()->erase(cursor.pos());
2877 // refresh the positions
2879 while (tmprow->next()
2880 && tmprow->next()->par() == row->par()) {
2881 tmprow = tmprow->next();
2882 tmprow->pos(tmprow->pos() - 1);
2885 // delete newlines at the beginning of paragraphs
2886 while (cursor.par()->size() &&
2887 cursor.par()->isNewline(cursor.pos()) &&
2888 cursor.pos() == beginningOfMainBody(bview->buffer(),
2890 cursor.par()->erase(cursor.pos());
2891 // refresh the positions
2893 while (tmprow->next() &&
2894 tmprow->next()->par() == row->par()) {
2895 tmprow = tmprow->next();
2896 tmprow->pos(tmprow->pos() - 1);
2901 // is there a break one row above
2902 if (row->previous() && row->previous()->par() == row->par()) {
2903 z = nextBreakPoint(bview, row->previous(),
2905 if (z >= row->pos()) {
2908 Row * tmprow = row->previous();
2910 // maybe the current row is now empty
2911 if (row->pos() >= row->par()->size()) {
2916 breakAgainOneRow(bview, row);
2917 if (row->next() && row->next()->par() == row->par())
2918 need_break_row = row->next();
2923 // set the dimensions of the row above
2924 y -= tmprow->height();
2925 tmprow->fill(fill(bview, tmprow,
2927 setHeightOfRow(bview, tmprow);
2930 refresh_row = tmprow;
2931 status(bview, LyXText::NEED_MORE_REFRESH);
2932 setCursor(bview, cursor.par(), cursor.pos(),
2933 false, cursor.boundary());
2934 //current_font = rawtmpfont;
2935 //real_current_font = realtmpfont;
2936 // check, whether the last character's font has changed.
2938 cursor.par()->getFontSettings(bview->buffer()->params,
2939 cursor.par()->size() - 1))
2940 redoHeightOfParagraph(bview, cursor);
2945 // break the cursor row again
2946 if (row->next() && row->next()->par() == row->par() &&
2947 (rowLast(row) == row->par()->size() - 1 ||
2948 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2950 /* it can happen that a paragraph loses one row
2951 * without a real breakup. This is when a word
2952 * is to long to be broken. Well, I don t care this
2954 if (rowLast(row) == row->par()->size() - 1)
2955 removeRow(row->next());
2959 status(bview, LyXText::NEED_MORE_REFRESH);
2961 breakAgainOneRow(bview, row);
2962 // will the cursor be in another row now?
2963 if (row->next() && row->next()->par() == row->par() &&
2964 rowLast(row) <= cursor.pos()) {
2966 breakAgainOneRow(bview, row);
2969 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2971 if (row->next() && row->next()->par() == row->par())
2972 need_break_row = row->next();
2976 // set the dimensions of the row
2977 row->fill(fill(bview, row, workWidth(bview)));
2978 int const tmpheight = row->height();
2979 setHeightOfRow(bview, row);
2980 if (tmpheight == row->height())
2981 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2983 status(bview, LyXText::NEED_MORE_REFRESH);
2986 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2990 // current_font = rawtmpfont;
2991 // real_current_font = realtmpfont;
2993 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2994 != cursor.boundary())
2995 setCursor(bview, cursor.par(), cursor.pos(), false,
2996 !cursor.boundary());
2998 lastpos = cursor.par()->size();
2999 if (cursor.pos() == lastpos)
3000 setCurrentFont(bview);
3002 // check, whether the last characters font has changed.
3004 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3005 redoHeightOfParagraph(bview, cursor);
3007 // now the special right address boxes
3009 [bview->buffer()->params.textclass]
3010 [cursor.par()->layout()].margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3011 redoDrawingOfParagraph(bview, cursor);
3017 bool LyXText::paintRowBackground(DrawRowParams & p)
3019 bool clear_area = true;
3021 LyXFont font(LyXFont::ALL_SANE);
3023 pos_type const last = rowLastPrintable(p.row);
3025 if (!p.bv->screen()->forceClear() && last == p.row->pos()
3026 && p.row->par()->isInset(p.row->pos())) {
3027 inset = p.row->par()->getInset(p.row->pos());
3029 clear_area = inset->doClearArea();
3039 int const y = p.yo < 0 ? 0 : p.yo;
3040 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3041 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3048 int h = p.row->baseline() - inset->ascent(p.bv, font);
3050 // first clear the whole row above the inset!
3052 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3055 // clear the space below the inset!
3056 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3057 if ((p.row->height() - h) > 0) {
3058 p.pain->fillRectangle(p.xo, p.yo + h,
3059 p.width, p.row->height() - h, backgroundColor());
3062 // clear the space behind the inset, if needed
3063 if (!inset->display() && !inset->needFullRow()) {
3064 int const xp = int(p.x) + inset->width(p.bv, font);
3065 if (p.width - xp > 0) {
3066 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3067 p.row->height(), backgroundColor());
3075 void LyXText::paintRowSelection(DrawRowParams & p)
3077 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3079 // the current selection
3080 int const startx = selection.start.x();
3081 int const endx = selection.end.x();
3082 int const starty = selection.start.y();
3083 int const endy = selection.end.y();
3084 Row const * startrow = selection.start.row();
3085 Row const * endrow = selection.end.row();
3089 if (bidi_same_direction) {
3093 int h = row->height();
3095 if (startrow == row && endrow == row) {
3096 if (startx < endx) {
3099 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3103 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3105 } else if (startrow == row) {
3106 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3107 int const w = (is_rtl) ? startx : (p.width - startx);
3108 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3109 } else if (endrow == row) {
3110 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3111 int const w = (is_rtl) ? (p.width - endx) : endx;
3112 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3113 } else if (p.y > starty && p.y < endy) {
3114 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3117 } else if (startrow != row && endrow != row) {
3119 int h = row->height();
3120 if (p.y > starty && p.y < endy) {
3121 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3126 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3132 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3134 Buffer const * buffer = p.bv->buffer();
3135 Paragraph * par = row->par();
3136 pos_type main_body = beginningOfMainBody(buffer, par);
3137 pos_type const last = rowLastPrintable(row);
3139 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3140 pos_type pos = vis2log(vpos);
3141 float const old_tmpx = tmpx;
3142 if (main_body > 0 && pos == main_body - 1) {
3143 LyXLayout const & layout =
3145 [buffer->params.textclass]
3147 LyXFont const lfont = getLabelFont(buffer, par);
3150 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3152 if (par->isLineSeparator(main_body - 1))
3153 tmpx -= singleWidth(p.bv, par, main_body - 1);
3156 if (hfillExpansion(buffer, row, pos)) {
3157 tmpx += singleWidth(p.bv, par, pos);
3158 if (pos >= main_body)
3161 tmpx += p.label_hfill;
3164 else if (par->isSeparator(pos)) {
3165 tmpx += singleWidth(p.bv, par, pos);
3166 if (pos >= main_body)
3167 tmpx += p.separator;
3169 tmpx += singleWidth(p.bv, par, pos);
3172 if ((startrow != row || selection.start.pos() <= pos) &&
3173 (endrow != row || pos < selection.end.pos())) {
3174 // Here we do not use p.x as p.xo was added to p.x.
3175 p.pain->fillRectangle(int(old_tmpx), p.yo,
3176 int(tmpx - old_tmpx + 1),
3177 row->height(), LColor::selection);
3180 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3181 p.pain->fillRectangle(p.xo + int(tmpx),
3182 p.yo, int(p.bv->workWidth() - tmpx),
3183 row->height(), LColor::selection);
3189 void LyXText::paintRowAppendix(DrawRowParams & p)
3191 // FIXME: can be just p.width ?
3192 int const ww = p.bv->workWidth();
3193 Paragraph * firstpar = p.row->par();
3195 if (firstpar->params().appendix()) {
3196 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3197 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3202 void LyXText::paintRowDepthBar(DrawRowParams & p)
3204 Paragraph::depth_type const depth = p.row->par()->getDepth();
3209 Paragraph::depth_type prev_depth = 0;
3210 if (p.row->previous())
3211 prev_depth = p.row->previous()->par()->getDepth();
3212 Paragraph::depth_type next_depth = 0;
3214 next_depth = p.row->next()->par()->getDepth();
3216 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3217 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3218 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3220 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3222 int const w = LYX_PAPER_MARGIN / 5;
3224 if (i > prev_depth) {
3225 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3227 if (i > next_depth) {
3228 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3234 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3236 int const arrow_size = 4;
3237 int const space_size = int(vsp.inPixels(bv));
3239 if (vsp.kind() != VSpace::LENGTH) {
3245 int const min_size = max(3 * arrow_size,
3246 lyxfont::maxAscent(font)
3247 + lyxfont::maxDescent(font));
3249 if (vsp.length().len().value() < 0.0)
3252 return max(min_size, space_size);
3256 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3257 VSpace const & vsp, int start)
3259 int const arrow_size = 4;
3260 int const size = getLengthMarkerHeight(p.bv, vsp);
3261 int const end = start + size;
3263 // the label to display (if any)
3265 // y-values for top arrow
3267 // y-values for bottom arrow
3269 switch (vsp.kind()) {
3270 case VSpace::LENGTH:
3272 str = prefix + " (" + vsp.asLyXCommand() + ")";
3273 // adding or removing space
3274 bool const added = !(vsp.length().len().value() < 0.0);
3275 ty1 = added ? (start + arrow_size) : start;
3276 ty2 = added ? start : (start + arrow_size);
3277 by1 = added ? (end - arrow_size) : end;
3278 by2 = added ? end : (end - arrow_size);
3281 case VSpace:: VFILL:
3282 str = prefix + " (vertical fill)";
3287 // nothing to draw here
3291 int const leftx = p.xo + leftMargin(p.bv, p.row);
3292 int const midx = leftx + arrow_size;
3293 int const rightx = midx + arrow_size;
3301 font.setColor(LColor::added_space).decSize();
3302 lyxfont::rectText(str, font, w, a, d);
3304 p.pain->rectText(leftx + 2 * arrow_size + 5,
3305 start + ((end - start) / 2) + d,
3311 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3312 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3315 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3316 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3319 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3325 void LyXText::paintFirstRow(DrawRowParams & p)
3327 Paragraph * par = p.row->par();
3328 ParagraphParameters const & parparams = par->params();
3330 // start of appendix?
3331 if (parparams.startOfAppendix()) {
3332 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3337 // think about the margins
3338 if (!p.row->previous() && bv_owner)
3339 y_top += LYX_PAPER_MARGIN;
3341 // draw a top pagebreak
3342 if (parparams.pagebreakTop()) {
3343 int const y = p.yo + y_top + 2*defaultHeight();
3344 p.pain->line(p.xo, y, p.xo + p.width, y,
3345 LColor::pagebreak, Painter::line_onoffdash);
3352 pb_font.setColor(LColor::pagebreak).decSize();
3353 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3354 p.pain->rectText((p.width - w)/2, y + d,
3355 _("Page Break (top)"), pb_font,
3358 y_top += 3 * defaultHeight();
3361 // draw the additional space if needed:
3362 y_top += drawLengthMarker(p, _("Space above"),
3363 parparams.spaceTop(), p.yo + y_top);
3365 Buffer const * buffer = p.bv->buffer();
3367 LyXTextClass const & tclass = textclasslist[buffer->params.textclass];
3368 LyXLayout const & layout = tclass[par->layout()];
3370 // think about the parskip
3371 // some parskips VERY EASY IMPLEMENTATION
3372 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3373 if (par->previous()) {
3374 if (layout.latextype == LATEX_PARAGRAPH
3375 && !par->getDepth()) {
3376 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3378 LyXLayout const & playout =
3379 tclass[par->previous()->layout()];
3380 if (playout.latextype == LATEX_PARAGRAPH
3381 && !par->previous()->getDepth()) {
3382 // is it right to use defskip here, too? (AS)
3383 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3389 int const ww = p.bv->workWidth();
3392 if (parparams.lineTop()) {
3393 LyXFont font(LyXFont::ALL_SANE);
3394 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3398 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3399 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3400 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3401 LColor::topline, Painter::line_solid,
3402 Painter::line_thick);
3407 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3409 // should we print a label?
3410 if (layout.labeltype >= LABEL_STATIC
3411 && (layout.labeltype != LABEL_STATIC
3412 || layout.latextype != LATEX_ENVIRONMENT
3413 || par->isFirstInSequence())) {
3415 LyXFont font = getLabelFont(buffer, par);
3416 if (!par->getLabelstring().empty()) {
3418 string const str = par->getLabelstring();
3420 // this is special code for the chapter layout. This is
3421 // printed in an extra row and has a pagebreak at
3423 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3424 if (buffer->params.secnumdepth >= 0) {
3425 float spacing_val = 1.0;
3426 if (!parparams.spacing().isDefault()) {
3427 spacing_val = parparams.spacing().getValue();
3429 spacing_val = buffer->params.spacing.getValue();
3433 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3434 + int(layout.parsep) * defaultHeight();
3437 x = ww - leftMargin(p.bv, p.row) -
3438 lyxfont::width(str, font);
3441 p.pain->text(int(x),
3442 p.yo + p.row->baseline() -
3443 p.row->ascent_of_text() - maxdesc,
3448 x = ww - leftMargin(p.bv, p.row)
3449 + lyxfont::width(layout.labelsep, font);
3451 x = p.x - lyxfont::width(layout.labelsep, font)
3452 - lyxfont::width(str, font);
3455 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3458 // the labels at the top of an environment.
3459 // More or less for bibliography
3460 } else if (par->isFirstInSequence() &&
3461 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3462 layout.labeltype == LABEL_BIBLIO ||
3463 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3464 LyXFont font = getLabelFont(buffer, par);
3465 if (!par->getLabelstring().empty()) {
3466 string const str = par->getLabelstring();
3467 float spacing_val = 1.0;
3468 if (!parparams.spacing().isDefault()) {
3469 spacing_val = parparams.spacing().getValue();
3471 spacing_val = buffer->params.spacing.getValue();
3475 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3476 + (layout.labelbottomsep * defaultHeight()));
3479 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3480 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3481 + ww - rightMargin(buffer, p.row)) / 2;
3482 x -= lyxfont::width(str, font) / 2;
3483 } else if (is_rtl) {
3484 x = ww - leftMargin(p.bv, p.row) -
3485 lyxfont::width(str, font);
3487 p.pain->text(int(x), p.yo + p.row->baseline()
3488 - p.row->ascent_of_text() - maxdesc,
3493 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3494 LyXFont font = getLayoutFont(buffer, par);
3497 x = ww - leftMargin(p.bv, p.row)
3498 + lyxfont::width(layout.labelsep, font);
3500 x = p.x - lyxfont::width(layout.labelsep, font)
3501 - par->bibkey->width(p.bv, font);
3503 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3508 void LyXText::paintLastRow(DrawRowParams & p)
3510 Paragraph * par = p.row->par();
3511 ParagraphParameters const & parparams = par->params();
3512 int y_bottom = p.row->height() - 1;
3514 // think about the margins
3515 if (!p.row->next() && bv_owner)
3516 y_bottom -= LYX_PAPER_MARGIN;
3518 int const ww = p.bv->workWidth();
3520 // draw a bottom pagebreak
3521 if (parparams.pagebreakBottom()) {
3523 pb_font.setColor(LColor::pagebreak).decSize();
3524 int const y = p.yo + y_bottom - 2 * defaultHeight();
3526 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3527 Painter::line_onoffdash);
3532 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3533 p.pain->rectText((ww - w) / 2, y + d,
3534 _("Page Break (bottom)"),
3535 pb_font, backgroundColor(), backgroundColor());
3537 y_bottom -= 3 * defaultHeight();
3540 // draw the additional space if needed:
3541 int const height = getLengthMarkerHeight(p.bv,
3542 parparams.spaceBottom());
3543 y_bottom -= drawLengthMarker(p, _("Space below"),
3544 parparams.spaceBottom(),
3545 p.yo + y_bottom - height);
3547 Buffer const * buffer = p.bv->buffer();
3549 // draw a bottom line
3550 if (parparams.lineBottom()) {
3551 LyXFont font(LyXFont::ALL_SANE);
3552 int const asc = lyxfont::ascent('x',
3553 getFont(buffer, par,
3554 max(pos_type(0), par->size() - 1)));
3558 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3559 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3560 int const y = p.yo + y_bottom;
3561 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3562 Painter::line_thick);
3567 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3568 int const endlabel = par->getEndLabel(buffer->params);
3573 case END_LABEL_FILLED_BOX:
3575 LyXFont const font = getLabelFont(buffer, par);
3576 int const size = int(0.75 * lyxfont::maxAscent(font));
3577 int const y = (p.yo + p.row->baseline()) - size;
3578 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3580 if (p.row->fill() <= size)
3581 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3583 if (endlabel == END_LABEL_BOX) {
3584 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3586 p.pain->fillRectangle(x, y, size, size,
3591 case END_LABEL_STATIC:
3593 LyXFont font(LyXFont::ALL_SANE);
3594 string const & layout = par->layout();
3595 string const str = textclasslist[buffer->params.textclass][layout].endlabelstring();
3596 font = getLabelFont(buffer, par);
3597 int const x = is_rtl ?
3598 int(p.x) - lyxfont::width(str, font)
3599 : ww - rightMargin(buffer, p.row) - p.row->fill();
3600 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3603 case END_LABEL_NO_LABEL:
3608 void LyXText::paintRowText(DrawRowParams & p)
3610 Paragraph * par = p.row->par();
3611 Buffer const * buffer = p.bv->buffer();
3613 pos_type const last = rowLastPrintable(p.row);
3614 pos_type main_body =
3615 beginningOfMainBody(buffer, par);
3616 if (main_body > 0 &&
3617 (main_body - 1 > last ||
3618 !par->isLineSeparator(main_body - 1))) {
3622 LyXLayout const & layout =
3623 textclasslist[buffer->params.textclass][par->layout()];
3625 pos_type vpos = p.row->pos();
3626 while (vpos <= last) {
3627 if (p.x > p.bv->workWidth())
3629 pos_type pos = vis2log(vpos);
3630 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3631 p.x += singleWidth(p.bv, par, pos);
3635 if (main_body > 0 && pos == main_body - 1) {
3636 int const lwidth = lyxfont::width(layout.labelsep,
3637 getLabelFont(buffer, par));
3639 p.x += p.label_hfill + lwidth
3640 - singleWidth(p.bv, par, main_body - 1);
3643 if (par->isHfill(pos)) {
3646 int const y0 = p.yo + p.row->baseline();
3647 int const y1 = y0 - defaultHeight() / 2;
3649 p.pain->line(int(p.x), y1, int(p.x), y0,
3650 LColor::added_space);
3652 if (hfillExpansion(buffer, p.row, pos)) {
3653 int const y2 = (y0 + y1) / 2;
3655 if (pos >= main_body) {
3656 p.pain->line(int(p.x), y2,
3657 int(p.x + p.hfill), y2,
3658 LColor::added_space,
3659 Painter::line_onoffdash);
3662 p.pain->line(int(p.x), y2,
3663 int(p.x + p.label_hfill), y2,
3664 LColor::added_space,
3665 Painter::line_onoffdash);
3666 p.x += p.label_hfill;
3668 p.pain->line(int(p.x), y1,
3670 LColor::added_space);
3674 } else if (par->isSeparator(pos)) {
3675 p.x += singleWidth(p.bv, par, pos);
3676 if (pos >= main_body)
3687 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3688 Row * row, int y, bool cleared)
3690 if (row->height() <= 0) {
3691 lyxerr << "LYX_ERROR: row.height: "
3692 << row->height() << endl;
3698 // set up drawing parameters
3700 p.pain = &bv->painter();
3704 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3705 if (inset_owner && (p.x < 0))
3709 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3710 p.cleared = cleared;
3714 // clear to background if necessary
3715 p.cleared = paintRowBackground(p);
3717 // paint the selection background
3718 if (selection.set()) {
3719 paintRowSelection(p);
3722 // vertical lines for appendix
3723 paintRowAppendix(p);
3725 // environment depth brackets
3726 paintRowDepthBar(p);
3728 // draw any stuff wanted for a first row of a paragraph
3733 // draw any stuff wanted for the last row of a paragraph
3734 if (!row->next() || (row->next()->par() != row->par())) {
3743 int LyXText::defaultHeight() const
3745 LyXFont font(LyXFont::ALL_SANE);
3746 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3750 /* returns the column near the specified x-coordinate of the row
3751 * x is set to the real beginning of this column */
3753 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3754 bool & boundary) const
3757 float fill_separator;
3759 float fill_label_hfill;
3761 prepareToPrint(bview, row, tmpx, fill_separator,
3762 fill_hfill, fill_label_hfill);
3764 pos_type vc = row->pos();
3765 pos_type last = rowLastPrintable(row);
3767 LyXLayout const & layout =
3768 textclasslist[bview->buffer()->params.textclass][
3769 row->par()->layout()];
3770 bool left_side = false;
3772 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3773 float last_tmpx = tmpx;
3775 if (main_body > 0 &&
3776 (main_body - 1 > last ||
3777 !row->par()->isLineSeparator(main_body - 1)))
3780 while (vc <= last && tmpx <= x) {
3783 if (main_body > 0 && c == main_body-1) {
3784 tmpx += fill_label_hfill +
3785 lyxfont::width(layout.labelsep,
3786 getLabelFont(bview->buffer(), row->par()));
3787 if (row->par()->isLineSeparator(main_body - 1))
3788 tmpx -= singleWidth(bview, row->par(), main_body-1);
3791 if (hfillExpansion(bview->buffer(), row, c)) {
3792 x += singleWidth(bview, row->par(), c);
3796 tmpx += fill_label_hfill;
3798 else if (row->par()->isSeparator(c)) {
3799 tmpx += singleWidth(bview, row->par(), c);
3801 tmpx+= fill_separator;
3803 tmpx += singleWidth(bview, row->par(), c);
3807 if ((tmpx + last_tmpx) / 2 > x) {
3812 if (vc > last + 1) // This shouldn't happen.
3816 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3817 // some speedup if rtl_support=false
3818 && (!row->next() || row->next()->par() != row->par());
3819 bool const rtl = (lastrow)
3820 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3821 : false; // If lastrow is false, we don't need to compute
3822 // the value of rtl.
3824 if (row->pos() > last) // Row is empty?
3827 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3828 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3830 else if (vc == row->pos()) {
3832 if (bidi_level(c) % 2 == 1)
3835 c = vis2log(vc - 1);
3836 bool const rtl = (bidi_level(c) % 2 == 1);
3837 if (left_side == rtl) {
3839 boundary = isBoundary(bview->buffer(), row->par(), c);
3843 if (row->pos() <= last && c > last
3844 && row->par()->isNewline(last)) {
3845 if (bidi_level(last) % 2 == 0)
3846 tmpx -= singleWidth(bview, row->par(), last);
3848 tmpx += singleWidth(bview, row->par(), last);
3858 // returns pointer to a specified row
3859 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3864 Row * tmprow = firstrow;
3867 // find the first row of the specified paragraph
3868 while (tmprow->next() && tmprow->par() != par) {
3869 y += tmprow->height();
3870 tmprow = tmprow->next();
3873 // now find the wanted row
3874 while (tmprow->pos() < pos
3876 && tmprow->next()->par() == par
3877 && tmprow->next()->pos() <= pos) {
3878 y += tmprow->height();
3879 tmprow = tmprow->next();
3886 Row * LyXText::getRowNearY(int & y) const
3889 // If possible we should optimize this method. (Lgb)
3890 Row * tmprow = firstrow;
3893 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3894 tmpy += tmprow->height();
3895 tmprow = tmprow->next();
3898 y = tmpy; // return the real y
3900 //lyxerr << "returned y = " << y << endl;
3904 // Search from the current cursor position.
3906 Row * tmprow = cursor.row();
3907 int tmpy = cursor.y() - tmprow->baseline();
3909 lyxerr << "cursor.y() = " << tmpy << endl;
3910 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3911 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3912 lyxerr << "first = " << first << endl;
3913 lyxerr << "y = " << y << endl;
3916 lyxerr << "up" << endl;
3918 while (tmprow && tmpy - tmprow->height() >= y) {
3919 tmpy -= tmprow->height();
3920 tmprow = tmprow->previous();
3924 tmpy -= tmprow->height();
3925 tmprow = tmprow->previous();
3926 } while (tmprow && tmpy - tmprow->height() >= y);
3928 } else if (y > tmpy) {
3929 lyxerr << "down" << endl;
3931 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3932 tmpy += tmprow->height();
3933 tmprow = tmprow->next();
3936 lyxerr << "equal" << endl;
3939 y = tmpy; // return the real y
3941 lyxerr << "returned y = " << y << endl;
3949 int LyXText::getDepth() const
3951 return cursor.par()->getDepth();