1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #include "lyxtextclasslist.h"
16 #include "paragraph.h"
18 #include "bufferparams.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "bufferview_funcs.h"
28 #include "BufferView.h"
30 #include "ParagraphParameters.h"
31 #include "undo_funcs.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
36 #include "support/textutils.h"
37 #include "support/LAssert.h"
38 #include "support/lstrings.h"
50 int const LYX_PAPER_MARGIN = 20;
54 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
57 int LyXText::workWidth(BufferView * bview) const
60 return inset_owner->textWidth(bview);
62 return bview->workWidth();
66 int LyXText::workWidth(BufferView * bview, Inset * inset) const
71 par = inset->parOwner();
73 pos = par->getPositionOfInset(inset);
75 if (!par || pos == -1) {
76 lyxerr << "LyXText::workWidth: something is wrong,"
77 " fall back to the brute force method" << endl;
78 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
79 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
80 for (; it != end; ++it) {
90 return workWidth(bview);
93 LyXLayout const & layout =
94 textclasslist[bview->buffer()->params.textclass][par->layout()];
96 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
97 // Optimization here: in most cases, the real row is
98 // not needed, but only the par/pos values. So we just
99 // construct a dummy row for leftMargin. (JMarc)
103 return workWidth(bview) - leftMargin(bview, &dummyrow);
106 Row * row = getRow(par, pos, dummy_y);
108 while (frow->previous() && frow->par() == frow->previous()->par())
109 frow = frow->previous();
110 unsigned int maxw = 0;
111 while (frow->next() && frow->par() == frow->next()->par()) {
112 if ((frow != row) && (maxw < frow->width()))
113 maxw = frow->width();
119 return workWidth(bview);
123 int LyXText::getRealCursorX(BufferView * bview) const
126 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
127 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
132 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
135 if (!Encodings::is_arabic(c))
136 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
137 return c + (0xb0 - '0');
141 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
142 unsigned char next_char = ' ';
144 for (pos_type i = pos+1; i < par->size(); ++i)
145 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
146 next_char = par->getChar(i);
150 if (Encodings::is_arabic(next_char)) {
151 if (Encodings::is_arabic(prev_char))
152 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
154 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
156 if (Encodings::is_arabic(prev_char))
157 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
159 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
163 // This is the comments that some of the warnings below refers to.
164 // There are some issues in this file and I don't think they are
165 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
166 // this is a problem that has been here almost from day one and that a
167 // larger userbase with differenct access patters triggers the bad
168 // behaviour. (segfaults.) What I think happen is: In several places
169 // we store the paragraph in the current cursor and then moves the
170 // cursor. This movement of the cursor will delete paragraph at the
171 // old position if it is now empty. This will make the temporary
172 // pointer to the old cursor paragraph invalid and dangerous to use.
173 // And is some cases this will trigger a segfault. I have marked some
174 // of the cases where this happens with a warning, but I am sure there
175 // are others in this file and in text2.C. There is also a note in
176 // Delete() that you should read. In Delete I store the paragraph->id
177 // instead of a pointer to the paragraph. I am pretty sure this faulty
178 // use of temporary pointers to paragraphs that might have gotten
179 // invalidated (through a cursor movement) before they are used, are
180 // the cause of the strange crashes we get reported often.
182 // It is very tiresom to change this code, especially when it is as
183 // hard to read as it is. Help to fix all the cases where this is done
184 // would be greately appreciated.
188 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
191 char const c = par->getChar(pos);
192 return singleWidth(bview, par, pos, c);
196 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
197 pos_type pos, char c) const
199 LyXFont const font = getFont(bview->buffer(), par, pos);
201 // The most common case is handled first (Asger)
202 if (IsPrintable(c)) {
203 if (font.language()->RightToLeft()) {
204 if (font.language()->lang() == "arabic" &&
205 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
206 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
207 if (Encodings::IsComposeChar_arabic(c))
210 c = transformChar(c, par, pos);
211 else if (font.language()->lang() == "hebrew" &&
212 Encodings::IsComposeChar_hebrew(c))
215 return font_metrics::width(c, font);
217 } else if (IsHfillChar(c)) {
218 // Because of the representation as vertical lines
220 } else if (c == Paragraph::META_INSET) {
221 Inset * tmpinset = par->getInset(pos);
224 // this IS needed otherwise on initialitation we don't get the fill
225 // of the row right (ONLY on initialization if we read a file!)
226 // should be changed! (Jug 20011204)
227 tmpinset->update(bview, font);
229 return tmpinset->width(bview, font);
233 } else if (IsSeparatorChar(c))
235 else if (IsNewlineChar(c))
237 return font_metrics::width(c, font);
241 // Returns the paragraph position of the last character in the specified row
242 pos_type LyXText::rowLast(Row const * row) const
244 if (!row->next() || row->next()->par() != row->par()) {
245 return row->par()->size() - 1;
247 return row->next()->pos() - 1;
252 pos_type LyXText::rowLastPrintable(Row const * row) const
254 pos_type const last = rowLast(row);
255 bool ignore_the_space_on_the_last_position = true;
257 // we have to consider a space on the last position in this case!
258 if (row->next() && row->par() == row->next()->par() &&
259 row->next()->par()->getChar(last+1) == Paragraph::META_INSET &&
260 (ins=row->next()->par()->getInset(last+1)) &&
261 (ins->needFullRow() || ins->display()))
263 ignore_the_space_on_the_last_position = false;
265 if (last >= row->pos()
267 && row->next()->par() == row->par()
268 && row->par()->isSeparator(last)
269 && ignore_the_space_on_the_last_position)
276 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
278 bidi_same_direction = true;
279 if (!lyxrc.rtl_support) {
284 Inset * inset = row->par()->inInset();
285 if (inset && inset->owner() &&
286 inset->owner()->lyxCode() == Inset::ERT_CODE) {
291 bidi_start = row->pos();
292 bidi_end = rowLastPrintable(row);
294 if (bidi_start > bidi_end) {
299 if (bidi_end + 2 - bidi_start >
300 static_cast<pos_type>(log2vis_list.size())) {
302 (bidi_end + 2 - bidi_start < 500) ?
303 500 : 2 * (bidi_end + 2 - bidi_start);
304 log2vis_list.resize(new_size);
305 vis2log_list.resize(new_size);
306 bidi_levels.resize(new_size);
309 vis2log_list[bidi_end + 1 - bidi_start] = -1;
310 log2vis_list[bidi_end + 1 - bidi_start] = -1;
314 row->par()->isRightToLeftPar(buf->params);
318 pos_type const main_body = beginningOfMainBody(buf, row->par());
320 for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
321 bool is_space = row->par()->isLineSeparator(lpos);
323 (is_space && lpos + 1 <= bidi_end &&
324 !row->par()->isLineSeparator(lpos + 1) &&
325 !row->par()->isNewline(lpos + 1))
327 LyXFont font = row->par()->getFontSettings(buf->params, pos);
328 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
329 font.number() == LyXFont::ON &&
330 row->par()->getFontSettings(buf->params, lpos - 1).number()
332 font = row->par()->getFontSettings(buf->params, lpos);
337 bool new_rtl = font.isVisibleRightToLeft();
338 bool new_rtl0 = font.isRightToLeft();
341 if (lpos == main_body - 1
342 && row->pos() < main_body - 1
344 new_level = (rtl_par) ? 1 : 0;
345 new_rtl = new_rtl0 = rtl_par;
347 new_level = (new_rtl) ? 1 : 2;
349 new_level = (rtl_par) ? 2 : 0;
351 if (is_space && new_level >= level) {
357 int new_level2 = new_level;
359 if (level == new_level && rtl0 != new_rtl0) {
361 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
362 } else if (level < new_level) {
363 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
364 if (new_level > rtl_par)
365 bidi_same_direction = false;
367 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
370 bidi_levels[lpos - bidi_start] = new_level;
372 while (level > new_level2) {
373 pos_type old_lpos = stack[--level];
374 int delta = lpos - old_lpos - 1;
377 log2vis_list[lpos - bidi_start] += delta;
378 log2vis_list[old_lpos - bidi_start] += delta;
380 while (level < new_level)
381 stack[level++] = lpos;
385 pos_type const old_lpos = stack[--level];
386 int delta = bidi_end - old_lpos;
389 log2vis_list[old_lpos - bidi_start] += delta;
392 pos_type vpos = bidi_start - 1;
393 for (pos_type lpos = bidi_start;
394 lpos <= bidi_end; ++lpos) {
395 vpos += log2vis_list[lpos - bidi_start];
396 vis2log_list[vpos - bidi_start] = lpos;
397 log2vis_list[lpos - bidi_start] = vpos;
402 // This method requires a previous call to ComputeBidiTables()
403 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
406 if (!lyxrc.rtl_support || pos == 0)
409 if (!bidi_InRange(pos - 1)) {
410 /// This can happen if pos is the first char of a row.
411 /// Returning false in this case is incorrect!
415 bool const rtl = bidi_level(pos - 1) % 2;
416 bool const rtl2 = bidi_InRange(pos)
417 ? bidi_level(pos) % 2
418 : par->isRightToLeftPar(buf->params);
423 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
424 pos_type pos, LyXFont const & font) const
426 if (!lyxrc.rtl_support)
427 return false; // This is just for speedup
429 bool const rtl = font.isVisibleRightToLeft();
430 bool const rtl2 = bidi_InRange(pos)
431 ? bidi_level(pos) % 2
432 : par->isRightToLeftPar(buf->params);
437 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
439 // Draw end-of-line marker
440 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
441 int const wid = font_metrics::width('n', font);
442 int const asc = font_metrics::maxAscent(font);
443 int const y = p.yo + p.row->baseline();
447 yp[0] = int(y - 0.875 * asc * 0.75);
448 yp[1] = int(y - 0.500 * asc * 0.75);
449 yp[2] = int(y - 0.125 * asc * 0.75);
451 if (bidi_level(pos) % 2 == 0) {
452 xp[0] = int(p.x + wid * 0.375);
454 xp[2] = int(p.x + wid * 0.375);
456 xp[0] = int(p.x + wid * 0.625);
457 xp[1] = int(p.x + wid);
458 xp[2] = int(p.x + wid * 0.625);
461 p.pain->lines(xp, yp, 3, LColor::eolmarker);
463 yp[0] = int(y - 0.500 * asc * 0.75);
464 yp[1] = int(y - 0.500 * asc * 0.75);
465 yp[2] = int(y - asc * 0.75);
467 if (bidi_level(pos) % 2 == 0) {
469 xp[1] = int(p.x + wid);
470 xp[2] = int(p.x + wid);
472 xp[0] = int(p.x + wid);
477 p.pain->lines(xp, yp, 3, LColor::eolmarker);
483 bool LyXText::drawInset(DrawRowParams & p, pos_type const pos)
485 Inset * inset = p.row->par()->getInset(pos);
487 // FIXME: shouldn't happen
492 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
493 // we need this here as the row pointer may be illegal
494 // at a later time (Jug20020502)
495 Row * prev = p.row->previous();
497 inset->update(p.bv, font, false);
498 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
500 if (!need_break_row && !inset_owner
501 && p.bv->text->status() == CHANGED_IN_DRAW) {
502 if (prev && prev->par() == p.row->par()) {
503 breakAgainOneRow(p.bv, prev);
504 if (prev->next() != p.row) {
505 // breakAgainOneRow() has removed p.row
506 p.row = 0; // see what this breaks
507 need_break_row = prev;
509 need_break_row = p.row;
512 need_break_row = firstrow;
514 need_break_row = prev->next();
516 setCursor(p.bv, cursor.par(), cursor.pos());
523 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
525 if (!lyxrc.mark_foreign_language)
527 if (orig_font.language() == latex_language)
529 if (orig_font.language() == p.bv->buffer()->params.language)
532 int const y = p.yo + p.row->height() - 1;
533 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
537 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
539 pos_type pos = vis2log(vpos);
544 char c = p.row->par()->getChar(pos);
548 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
549 int const width = font_metrics::width(c, font);
552 for (pos_type i = pos-1; i >= 0; --i) {
553 c = p.row->par()->getChar(i);
554 if (!Encodings::IsComposeChar_hebrew(c)) {
555 if (IsPrintableNonspace(c)) {
557 singleWidth(p.bv, p.row->par(), i, c);
559 dx = (c == 'ø' || c == 'ã')
561 : (width2 - width) / 2;
568 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
572 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
574 pos_type pos = vis2log(vpos);
578 char c = p.row->par()->getChar(pos);
579 c = transformChar(c, p.row->par(), pos);
583 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
584 int const width = font_metrics::width(c, font);
587 for (pos_type i = pos-1; i >= 0; --i) {
588 c = p.row->par()->getChar(i);
589 if (!Encodings::IsComposeChar_arabic(c)) {
590 if (IsPrintableNonspace(c)) {
592 singleWidth(p.bv, p.row->par(), i, c);
593 dx = (width2 - width) / 2;
599 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
603 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
604 bool hebrew, bool arabic)
606 pos_type pos = vis2log(vpos);
607 pos_type const last = rowLastPrintable(p.row);
608 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
612 str += p.row->par()->getChar(pos);
614 unsigned char c = str[0];
615 str[0] = transformChar(c, p.row->par(), pos);
619 // collect as much similar chars as we can
620 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
621 char c = p.row->par()->getChar(pos);
623 if (!IsPrintableNonspace(c))
626 if (arabic && Encodings::IsComposeChar_arabic(c))
628 if (hebrew && Encodings::IsComposeChar_hebrew(c))
631 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
638 // Draw text and set the new x position
639 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
640 p.x += font_metrics::width(str, orig_font);
644 bool LyXText::draw(DrawRowParams & p, pos_type & vpos)
646 pos_type const pos = vis2log(vpos);
647 Paragraph * par = p.row->par();
649 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
651 float const orig_x = p.x;
653 char const c = par->getChar(pos);
655 if (IsNewlineChar(c)) {
659 } else if (IsInsetChar(c)) {
660 if (!drawInset(p, pos))
663 drawForeignMark(p, orig_x, orig_font);
667 // usual characters, no insets
669 // special case languages
670 bool const hebrew = (orig_font.language()->lang() == "hebrew");
672 orig_font.language()->lang() == "arabic" &&
673 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
674 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
676 // draw as many chars as we can
677 if ((!hebrew && !arabic)
678 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
679 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
680 drawChars(p, vpos, true, false);
682 drawHebrewComposeChar(p, vpos);
684 drawArabicComposeChar(p, vpos);
687 drawForeignMark(p, orig_x, orig_font);
689 #ifdef INHERIT_LANGUAGE
691 if ((font.language() == inherit_language) ||
692 (font.language() == ignore_language))
693 lyxerr << "No this shouldn't happen!\n";
700 int LyXText::leftMargin(BufferView * bview, Row const * row) const
703 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
704 (ins=row->par()->getInset(row->pos())) &&
705 (ins->needFullRow() || ins->display()))
706 return LYX_PAPER_MARGIN;
708 LyXTextClass const & tclass =
709 textclasslist[bview->buffer()->params.textclass];
710 LyXLayout const & layout = tclass[row->par()->layout()];
712 string parindent = layout.parindent;
714 int x = LYX_PAPER_MARGIN;
716 x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
718 // this is the way, LyX handles the LaTeX-Environments.
719 // I have had this idea very late, so it seems to be a
720 // later added hack and this is true
721 if (!row->par()->getDepth()) {
722 if (row->par()->layout() == tclass.defaultLayoutName()) {
723 // find the previous same level paragraph
724 if (row->par()->previous()) {
725 Paragraph * newpar = row->par()
726 ->depthHook(row->par()->getDepth());
728 tclass[newpar->layout()].nextnoindent)
733 // find the next level paragraph
736 row->par()->outerHook();
738 // make a corresponding row. Needed to call LeftMargin()
740 // check wether it is a sufficent paragraph
741 if (newpar && tclass[newpar->layout()].isEnvironment()) {
743 dummyrow.par(newpar);
744 dummyrow.pos(newpar->size());
745 x = leftMargin(bview, &dummyrow);
747 // this is no longer an error, because this function
748 // is used to clear impossible depths after changing
749 // a layout. Since there is always a redo,
750 // LeftMargin() is always called
751 row->par()->params().depth(0);
754 if (newpar && row->par()->layout() == tclass.defaultLayoutName()) {
755 if (newpar->params().noindent())
758 parindent = tclass[newpar->layout()].parindent;
762 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
763 switch (layout.margintype) {
765 if (!layout.leftmargin.empty()) {
766 x += font_metrics::signedWidth(layout.leftmargin,
767 tclass.defaultfont());
769 if (!row->par()->getLabelstring().empty()) {
770 x += font_metrics::signedWidth(layout.labelindent,
772 x += font_metrics::width(row->par()->getLabelstring(),
774 x += font_metrics::width(layout.labelsep, labelfont);
778 x += font_metrics::signedWidth(layout.labelindent, labelfont);
779 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
780 if (!row->par()->getLabelWidthString().empty()) {
781 x += font_metrics::width(row->par()->getLabelWidthString(),
783 x += font_metrics::width(layout.labelsep, labelfont);
788 x += font_metrics::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
789 / (row->par()->getDepth() + 4);
791 case MARGIN_FIRST_DYNAMIC:
792 if (layout.labeltype == LABEL_MANUAL) {
793 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
794 x += font_metrics::signedWidth(layout.leftmargin,
797 x += font_metrics::signedWidth(layout.labelindent,
800 } else if (row->pos()
801 // Special case to fix problems with
803 || (layout.labeltype == LABEL_STATIC
804 && layout.latextype == LATEX_ENVIRONMENT
805 && ! row->par()->isFirstInSequence())) {
806 x += font_metrics::signedWidth(layout.leftmargin,
808 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
809 && layout.labeltype != LABEL_BIBLIO
810 && layout.labeltype !=
811 LABEL_CENTERED_TOP_ENVIRONMENT) {
812 x += font_metrics::signedWidth(layout.labelindent,
814 x += font_metrics::width(layout.labelsep, labelfont);
815 x += font_metrics::width(row->par()->getLabelstring(),
820 case MARGIN_RIGHT_ADDRESS_BOX:
822 // ok, a terrible hack. The left margin depends on the widest
823 // row in this paragraph. Do not care about footnotes, they
824 // are *NOT* allowed in the LaTeX realisation of this layout.
826 // find the first row of this paragraph
827 Row const * tmprow = row;
828 while (tmprow->previous()
829 && tmprow->previous()->par() == row->par())
830 tmprow = tmprow->previous();
832 int minfill = tmprow->fill();
833 while (tmprow->next() && tmprow->next()->par() == row->par()) {
834 tmprow = tmprow->next();
835 if (tmprow->fill() < minfill)
836 minfill = tmprow->fill();
839 x += font_metrics::signedWidth(layout.leftmargin,
840 tclass.defaultfont());
846 if ((workWidth(bview) > 0) &&
847 !row->par()->params().leftIndent().zero())
849 LyXLength const len = row->par()->params().leftIndent();
850 int const tw = inset_owner ?
851 inset_owner->latexTextWidth(bview) : workWidth(bview);
852 x += len.inPixels(tw, bview->text->defaultHeight());
855 LyXAlignment align; // wrong type
857 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
858 align = layout.align;
860 align = row->par()->params().align();
862 // set the correct parindent
863 if (row->pos() == 0) {
864 if ((layout.labeltype == LABEL_NO_LABEL
865 || layout.labeltype == LABEL_TOP_ENVIRONMENT
866 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
867 || (layout.labeltype == LABEL_STATIC
868 && layout.latextype == LATEX_ENVIRONMENT
869 && ! row->par()->isFirstInSequence()))
870 && align == LYX_ALIGN_BLOCK
871 && !row->par()->params().noindent()
872 // in tabulars and ert paragraphs are never indented!
873 && (!row->par()->inInset() || !row->par()->inInset()->owner() ||
874 (row->par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
875 row->par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
876 && (row->par()->layout() != tclass.defaultLayoutName() ||
877 bview->buffer()->params.paragraph_separation ==
878 BufferParams::PARSEP_INDENT)) {
879 x += font_metrics::signedWidth(parindent,
880 tclass.defaultfont());
881 } else if (layout.labeltype == LABEL_BIBLIO) {
882 // ale970405 Right width for bibitems
883 x += bibitemMaxWidth(bview, tclass.defaultfont());
891 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
894 if ((row->par()->getChar(row->pos()) == Paragraph::META_INSET) &&
895 (ins=row->par()->getInset(row->pos())) &&
896 (ins->needFullRow() || ins->display()))
897 return LYX_PAPER_MARGIN;
899 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
900 LyXLayout const & layout = tclass[row->par()->layout()];
902 int x = LYX_PAPER_MARGIN
903 + font_metrics::signedWidth(tclass.rightmargin(),
904 tclass.defaultfont());
906 // this is the way, LyX handles the LaTeX-Environments.
907 // I have had this idea very late, so it seems to be a
908 // later added hack and this is true
909 if (row->par()->getDepth()) {
910 // find the next level paragraph
912 Paragraph * newpar = row->par();
915 newpar = newpar->previous();
917 && newpar->getDepth() >= row->par()->getDepth());
919 // make a corresponding row. Needed to call LeftMargin()
921 // check wether it is a sufficent paragraph
922 if (newpar && tclass[newpar->layout()].isEnvironment()) {
924 dummyrow.par(newpar);
926 x = rightMargin(buf, &dummyrow);
928 // this is no longer an error, because this function
929 // is used to clear impossible depths after changing
930 // a layout. Since there is always a redo,
931 // LeftMargin() is always called
932 row->par()->params().depth(0);
936 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
937 x += font_metrics::signedWidth(layout.rightmargin, tclass.defaultfont())
938 * 4 / (row->par()->getDepth() + 4);
943 int LyXText::labelEnd(BufferView * bview, Row const * row) const
945 if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
949 tmprow.pos(row->par()->size());
950 // just the beginning of the main body
951 return leftMargin(bview, &tmprow);
953 // LabelEnd is only needed,
954 // if the layout fills a flushleft label.
960 // get the next breakpoint in a given paragraph
962 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
964 Paragraph * par = row->par();
969 pos_type const pos = row->pos();
971 // position of the last possible breakpoint
972 // -1 isn't a suitable value, but a flag
973 pos_type last_separator = -1;
974 width -= rightMargin(bview->buffer(), row);
976 pos_type const main_body =
977 beginningOfMainBody(bview->buffer(), par);
978 LyXLayout const & layout =
979 textclasslist[bview->buffer()->params.textclass][par->layout()];
982 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
983 // special code for right address boxes, only newlines count
984 while (i < par->size()) {
985 if (par->isNewline(i)) {
987 i = par->size() - 1; // this means break
989 } else if (par->isInset(i) && par->getInset(i)
990 && par->getInset(i)->display()) {
991 par->getInset(i)->display(false);
996 // Last position is an invariant
997 pos_type const last = par->size();
998 // this is the usual handling
999 int x = leftMargin(bview, row);
1000 bool doitonetime = true;
1001 while (doitonetime || ((x < width) && (i < last))) {
1002 doitonetime = false;
1003 char const c = par->getChar(i);
1005 if (c == Paragraph::META_INSET)
1006 in = par->getInset(i);
1007 if (IsNewlineChar(c)) {
1009 x = width; // this means break
1010 } else if (in && !in->isChar()) {
1011 // check wether a Display() inset is
1012 // valid here. if not, change it to
1014 if (in->display() &&
1015 (layout.isCommand() ||
1016 (layout.labeltype == LABEL_MANUAL
1017 && i < beginningOfMainBody(bview->buffer(), par))))
1019 // display istn't allowd
1021 x += singleWidth(bview, par, i, c);
1022 } else if (in->display() || in->needFullRow()) {
1023 // So break the line here
1027 if (par->isLineSeparator(i+1))
1030 last_separator = last; // to avoid extra rows
1032 last_separator = i - 1;
1033 x = width; // this means break
1035 x += singleWidth(bview, par, i, c);
1036 // we have to check this separately as we could have a
1037 // lineseparator and then the algorithm below would prefer
1038 // that which IS wrong! We should always break on an inset
1039 // if it's too long and not on the last separator.
1040 // Maybe the only exeption is insets used as chars but
1041 // then we would have to have a special function inside
1042 // the inset to tell us this. Till then we leave it as
1043 // it is now. (Jug 20020106)
1044 if (pos < i && x >= width && last_separator >= 0)
1045 last_separator = i - 1;
1048 if (par->isLineSeparator(i))
1050 x += singleWidth(bview, par, i, c);
1053 if (i == main_body) {
1054 x += font_metrics::width(layout.labelsep,
1055 getLabelFont(bview->buffer(), par));
1056 if (par->isLineSeparator(i - 1))
1057 x-= singleWidth(bview, par, i - 1);
1058 int left_margin = labelEnd(bview, row);
1059 if (x < left_margin)
1063 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1064 last_separator = i - 2;
1065 else if ((pos < i) && (last_separator < 0) && (x >= width))
1066 last_separator = i - 1;
1067 // end of paragraph is always a suitable separator
1068 else if (i == last && x < width)
1072 // well, if last_separator is still 0, the line isn't breakable.
1073 // don't care and cut simply at the end
1074 if (last_separator < 0) {
1078 // manual labels cannot be broken in LaTeX, do not care
1079 if (main_body && last_separator < main_body)
1080 last_separator = main_body - 1;
1082 return last_separator;
1086 // returns the minimum space a row needs on the screen in pixel
1087 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1089 if (paper_width < 0)
1093 // get the pure distance
1094 pos_type const last = rowLastPrintable(row);
1096 // special handling of the right address boxes
1097 if (textclasslist[bview->buffer()->params.textclass][row->par()->layout()].margintype
1098 == MARGIN_RIGHT_ADDRESS_BOX)
1100 int const tmpfill = row->fill();
1101 row->fill(0); // the minfill in MarginLeft()
1102 w = leftMargin(bview, row);
1105 w = leftMargin(bview, row);
1107 LyXLayout const & layout = textclasslist[bview->buffer()->params.textclass][row->par()->layout()];
1108 pos_type const main_body =
1109 beginningOfMainBody(bview->buffer(), row->par());
1110 pos_type i = row->pos();
1113 if (main_body > 0 && i == main_body) {
1114 w += font_metrics::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1115 if (row->par()->isLineSeparator(i - 1))
1116 w -= singleWidth(bview, row->par(), i - 1);
1117 int left_margin = labelEnd(bview, row);
1118 if (w < left_margin)
1121 w += singleWidth(bview, row->par(), i);
1124 if (main_body > 0 && main_body > last) {
1125 w += font_metrics::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1126 if (last >= 0 && row->par()->isLineSeparator(last))
1127 w -= singleWidth(bview, row->par(), last);
1128 int const left_margin = labelEnd(bview, row);
1129 if (w < left_margin)
1133 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1138 // returns the minimum space a manual label needs on the screen in pixel
1139 int LyXText::labelFill(BufferView * bview, Row const * row) const
1141 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1142 // -1 because a label ends either with a space that is in the label,
1143 // or with the beginning of a footnote that is outside the label.
1145 // I don't understand this code in depth, but sometimes "last" is
1146 // less than 0 and this causes a crash. This fix seems to work
1147 // correctly, but I bet the real error is elsewhere. The bug is
1148 // triggered when you have an open footnote in a paragraph
1149 // environment with a manual label. (Asger)
1150 if (last < 0) last = 0;
1152 // a separator at this end does not count
1153 if (row->par()->isLineSeparator(last))
1157 pos_type i = row->pos();
1159 w += singleWidth(bview, row->par(), i);
1164 if (!row->par()->params().labelWidthString().empty()) {
1165 fill = max(font_metrics::width(row->par()->params().labelWidthString(),
1166 getLabelFont(bview->buffer(), row->par())) - w,
1174 // returns the number of separators in the specified row. The separator
1175 // on the very last column doesnt count
1176 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1178 pos_type last = rowLastPrintable(row);
1179 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1182 for (; p <= last; ++p) {
1183 if (row->par()->isSeparator(p)) {
1191 // returns the number of hfills in the specified row. The LyX-Hfill is
1192 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1193 // ignored. This is *MUCH* more usefull than not to ignore!
1194 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1196 pos_type const last = rowLast(row);
1197 pos_type first = row->pos();
1200 // hfill *DO* count at the beginning of paragraphs!
1201 while (first <= last && row->par()->isHfill(first)) {
1206 first = max(first, beginningOfMainBody(buf, row->par()));
1208 for (pos_type p = first; p <= last; ++p) {
1209 // last, because the end is ignored!
1211 if (row->par()->isHfill(p)) {
1219 // like NumberOfHfills, but only those in the manual label!
1220 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1222 pos_type last = rowLast(row);
1223 pos_type first = row->pos();
1225 // hfill *DO* count at the beginning of paragraphs!
1226 while (first < last && row->par()->isHfill(first))
1230 last = min(last, beginningOfMainBody(buf, row->par()));
1232 for (pos_type p = first; p < last; ++p) {
1233 // last, because the end is ignored!
1234 if (row->par()->isHfill(p)) {
1242 // returns true, if a expansion is needed.
1243 // Rules are given by LaTeX
1244 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1247 // by the way, is it a hfill?
1248 if (!row_ptr->par()->isHfill(pos))
1251 // at the end of a row it does not count
1252 // unless another hfill exists on the line
1253 if (pos >= rowLast(row_ptr)) {
1254 pos_type i = row_ptr->pos();
1255 while (i < pos && !row_ptr->par()->isHfill(i)) {
1263 // at the beginning of a row it does not count, if it is not
1264 // the first row of a paragaph
1265 if (!row_ptr->pos())
1268 // in some labels it does not count
1269 if (textclasslist[buf->params.textclass][row_ptr->par()->layout()].margintype
1271 && pos < beginningOfMainBody(buf, row_ptr->par()))
1274 // if there is anything between the first char of the row and
1275 // the sepcified position that is not a newline and not a hfill,
1276 // the hfill will count, otherwise not
1277 pos_type i = row_ptr->pos();
1278 while (i < pos && (row_ptr->par()->isNewline(i)
1279 || row_ptr->par()->isHfill(i)))
1286 LColor::color LyXText::backgroundColor()
1289 return inset_owner->backgroundColor();
1291 return LColor::background;
1294 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1296 // get the maximum ascent and the maximum descent
1299 float layoutasc = 0;
1300 float layoutdesc = 0;
1303 Inset * tmpinset = 0;
1305 // ok , let us initialize the maxasc and maxdesc value.
1306 // This depends in LaTeX of the font of the last character
1307 // in the paragraph. The hack below is necessary because
1308 // of the possibility of open footnotes
1310 // Correction: only the fontsize count. The other properties
1311 // are taken from the layoutfont. Nicer on the screen :)
1312 Paragraph * par = row_ptr->par();
1313 Paragraph * firstpar = row_ptr->par();
1315 LyXLayout const & layout
1316 = textclasslist[bview->buffer()->params.textclass][firstpar->layout()];
1318 // as max get the first character of this row then it can increes but not
1319 // decrees the height. Just some point to start with so we don't have to
1320 // do the assignment below too often.
1321 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1322 LyXFont::FONT_SIZE const tmpsize = font.size();
1323 font = getLayoutFont(bview->buffer(), par);
1324 LyXFont::FONT_SIZE const size = font.size();
1325 font.setSize(tmpsize);
1327 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1329 float spacing_val = 1.0;
1330 if (!row_ptr->par()->params().spacing().isDefault()) {
1331 spacing_val = row_ptr->par()->params().spacing().getValue();
1333 spacing_val = bview->buffer()->params.spacing.getValue();
1335 //lyxerr << "spacing_val = " << spacing_val << endl;
1337 int maxasc = int(font_metrics::maxAscent(font) *
1338 layout.spacing.getValue() *
1340 int maxdesc = int(font_metrics::maxDescent(font) *
1341 layout.spacing.getValue() *
1344 pos_type const pos_end = rowLast(row_ptr);
1348 // Check if any insets are larger
1349 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1350 if (row_ptr->par()->isInset(pos)) {
1351 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1352 tmpinset = row_ptr->par()->getInset(pos);
1354 #if 1 // this is needed for deep update on initialitation
1355 tmpinset->update(bview, tmpfont);
1357 asc = tmpinset->ascent(bview, tmpfont);
1358 desc = tmpinset->descent(bview, tmpfont);
1359 maxwidth += tmpinset->width(bview, tmpfont);
1360 maxasc = max(maxasc, asc);
1361 maxdesc = max(maxdesc, desc);
1364 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1368 // Check if any custom fonts are larger (Asger)
1369 // This is not completely correct, but we can live with the small,
1370 // cosmetic error for now.
1371 LyXFont::FONT_SIZE maxsize =
1372 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1373 if (maxsize > font.size()) {
1374 font.setSize(maxsize);
1376 asc = font_metrics::maxAscent(font);
1377 desc = font_metrics::maxDescent(font);
1384 // This is nicer with box insets:
1388 row_ptr->ascent_of_text(maxasc);
1390 // is it a top line?
1391 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1393 // some parksips VERY EASY IMPLEMENTATION
1394 if (bview->buffer()->params.paragraph_separation ==
1395 BufferParams::PARSEP_SKIP)
1397 if (layout.isParagraph()
1398 && firstpar->getDepth() == 0
1399 && firstpar->previous())
1401 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1402 } else if (firstpar->previous() &&
1403 textclasslist[bview->buffer()->params.textclass][firstpar->previous()->layout()].isParagraph() &&
1404 firstpar->previous()->getDepth() == 0)
1406 // is it right to use defskip here too? (AS)
1407 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1411 // the paper margins
1412 if (!row_ptr->par()->previous() && bv_owner)
1413 maxasc += LYX_PAPER_MARGIN;
1415 // add the vertical spaces, that the user added
1416 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1418 // do not forget the DTP-lines!
1419 // there height depends on the font of the nearest character
1420 if (firstpar->params().lineTop())
1422 maxasc += 2 * font_metrics::ascent('x', getFont(bview->buffer(),
1424 // and now the pagebreaks
1425 if (firstpar->params().pagebreakTop())
1426 maxasc += 3 * defaultHeight();
1428 // This is special code for the chapter, since the label of this
1429 // layout is printed in an extra row
1430 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1431 && bview->buffer()->params.secnumdepth >= 0)
1433 float spacing_val = 1.0;
1434 if (!row_ptr->par()->params().spacing().isDefault()) {
1435 spacing_val = row_ptr->par()->params().spacing().getValue();
1437 spacing_val = bview->buffer()->params.spacing.getValue();
1440 labeladdon = int(font_metrics::maxDescent(labelfont) *
1441 layout.spacing.getValue() *
1443 + int(font_metrics::maxAscent(labelfont) *
1444 layout.spacing.getValue() *
1448 // special code for the top label
1449 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1450 || layout.labeltype == LABEL_BIBLIO
1451 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1452 && row_ptr->par()->isFirstInSequence()
1453 && !row_ptr->par()->getLabelstring().empty())
1455 float spacing_val = 1.0;
1456 if (!row_ptr->par()->params().spacing().isDefault()) {
1457 spacing_val = row_ptr->par()->params().spacing().getValue();
1459 spacing_val = bview->buffer()->params.spacing.getValue();
1463 (font_metrics::maxAscent(labelfont) *
1464 layout.spacing.getValue() *
1466 +(font_metrics::maxDescent(labelfont) *
1467 layout.spacing.getValue() *
1469 + layout.topsep * defaultHeight()
1470 + layout.labelbottomsep * defaultHeight());
1473 // and now the layout spaces, for example before and after a section,
1474 // or between the items of a itemize or enumerate environment
1476 if (!firstpar->params().pagebreakTop()) {
1477 Paragraph * prev = row_ptr->par()->previous();
1479 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1480 if (prev && prev->layout() == firstpar->layout() &&
1481 prev->getDepth() == firstpar->getDepth() &&
1482 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1484 layoutasc = (layout.itemsep * defaultHeight());
1485 } else if (row_ptr->previous()) {
1486 tmptop = layout.topsep;
1488 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1489 tmptop -= textclasslist[bview->buffer()->params.textclass][row_ptr->previous()->par()->layout()].bottomsep;
1492 layoutasc = (tmptop * defaultHeight());
1493 } else if (row_ptr->par()->params().lineTop()) {
1494 tmptop = layout.topsep;
1497 layoutasc = (tmptop * defaultHeight());
1500 prev = row_ptr->par()->outerHook();
1502 maxasc += int(textclasslist[bview->buffer()->params.textclass][prev->layout()].parsep * defaultHeight());
1504 if (firstpar->previous() &&
1505 firstpar->previous()->getDepth() == 0 &&
1506 firstpar->previous()->layout() !=
1510 } else if (firstpar->previous()) {
1511 maxasc += int(layout.parsep * defaultHeight());
1517 // is it a bottom line?
1518 if (row_ptr->par() == par
1519 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1521 // the paper margins
1522 if (!par->next() && bv_owner)
1523 maxdesc += LYX_PAPER_MARGIN;
1525 // add the vertical spaces, that the user added
1526 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1528 // do not forget the DTP-lines!
1529 // there height depends on the font of the nearest character
1530 if (firstpar->params().lineBottom())
1531 maxdesc += 2 * font_metrics::ascent('x',
1532 getFont(bview->buffer(),
1534 max(pos_type(0), par->size() - 1)));
1536 // and now the pagebreaks
1537 if (firstpar->params().pagebreakBottom())
1538 maxdesc += 3 * defaultHeight();
1540 // and now the layout spaces, for example before and after
1541 // a section, or between the items of a itemize or enumerate
1543 if (!firstpar->params().pagebreakBottom()
1544 && row_ptr->par()->next()) {
1545 Paragraph * nextpar = row_ptr->par()->next();
1546 Paragraph * comparepar = row_ptr->par();
1550 if (comparepar->getDepth() > nextpar->getDepth()) {
1551 usual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1552 comparepar = comparepar->depthHook(nextpar->getDepth());
1553 if (comparepar->layout()!= nextpar->layout()
1554 || nextpar->getLabelWidthString() !=
1555 comparepar->getLabelWidthString())
1557 unusual = (textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1559 if (unusual > usual)
1560 layoutdesc = unusual;
1563 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1565 if (comparepar->layout() != nextpar->layout()
1566 || nextpar->getLabelWidthString() !=
1567 comparepar->getLabelWidthString())
1568 layoutdesc = int(textclasslist[bview->buffer()->params.textclass][comparepar->layout()].bottomsep * defaultHeight());
1573 // incalculate the layout spaces
1574 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1575 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1577 // calculate the new height of the text
1578 height -= row_ptr->height();
1580 row_ptr->height(maxasc + maxdesc + labeladdon);
1581 row_ptr->baseline(maxasc + labeladdon);
1583 height += row_ptr->height();
1585 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1588 row_ptr->width(maxwidth);
1589 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1591 row_ptr->width(int(maxwidth + x));
1594 width = max(0,workWidth(bview));
1596 if (r->width() > width)
1604 // Appends the implicit specified paragraph behind the specified row,
1605 // start at the implicit given position
1606 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1608 bool not_ready = true;
1610 // The last character position of a paragraph is an invariant so we can
1611 // safely get it here. (Asger)
1612 pos_type const lastposition = row->par()->size();
1614 // Get the next breakpoint
1615 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1619 // Insert the new row
1620 if (z < lastposition) {
1622 insertRow(row, row->par(), z);
1629 // Set the dimensions of the row
1630 // fixed fill setting now by calling inset->update() in
1631 // SingleWidth when needed!
1632 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1633 setHeightOfRow(bview, tmprow);
1635 } while (not_ready);
1639 void LyXText::breakAgain(BufferView * bview, Row * row) const
1641 bool not_ready = true;
1644 // get the next breakpoint
1645 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1648 if (z < row->par()->size()) {
1649 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1652 insertRow(row, row->par(), z);
1658 if (row->pos() == z)
1659 not_ready = false; // the rest will not change
1665 // if there are some rows too much, delete them
1666 // only if you broke the whole paragraph!
1667 Row * tmprow2 = row;
1668 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1669 tmprow2 = tmprow2->next();
1671 while (tmprow2 != row) {
1672 tmprow2 = tmprow2->previous();
1673 removeRow(tmprow2->next());
1678 // set the dimensions of the row
1679 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1680 setHeightOfRow(bview, tmprow);
1681 } while (not_ready);
1685 // this is just a little changed version of break again
1686 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1688 // get the next breakpoint
1689 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1692 if (z < row->par()->size()) {
1694 || (row->next() && row->next()->par() != row->par())) {
1697 insertRow(row, row->par(), z);
1703 if (row->pos() != z)
1707 // if there are some rows too much, delete them
1708 // only if you broke the whole paragraph!
1709 Row * tmprow2 = row;
1710 while (tmprow2->next()
1711 && tmprow2->next()->par() == row->par()) {
1712 tmprow2 = tmprow2->next();
1714 while (tmprow2 != row) {
1715 tmprow2 = tmprow2->previous();
1716 removeRow(tmprow2->next());
1720 // set the dimensions of the row
1721 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1722 setHeightOfRow(bview, tmprow);
1726 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1728 LyXTextClass const & tclass =
1729 textclasslist[bview->buffer()->params.textclass];
1730 LyXLayout const & layout = tclass[cursor.par()->layout()];
1732 // this is only allowed, if the current paragraph is not empty or caption
1733 // and if it has not the keepempty flag aktive
1734 if ((cursor.par()->size() <= 0)
1735 && layout.labeltype != LABEL_SENSITIVE
1736 && !layout.keepempty)
1739 setUndo(bview, Undo::FINISH, cursor.par(), cursor.par()->next());
1741 // Always break behind a space
1743 // It is better to erase the space (Dekel)
1744 if (cursor.pos() < cursor.par()->size()
1745 && cursor.par()->isLineSeparator(cursor.pos()))
1746 cursor.par()->erase(cursor.pos());
1747 // cursor.pos(cursor.pos() + 1);
1749 // break the paragraph
1753 keep_layout = layout.isEnvironment();
1755 // we need to set this before we insert the paragraph. IMO the
1756 // breakParagraph call should return a bool if it inserts the
1757 // paragraph before or behind and we should react on that one
1758 // but we can fix this in 1.3.0 (Jug 20020509)
1759 bool const isempty = (layout.keepempty && !cursor.par()->size());
1760 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1763 // well this is the caption hack since one caption is really enough
1764 if (layout.labeltype == LABEL_SENSITIVE) {
1766 // set to standard-layout
1767 cursor.par()->applyLayout(tclass.defaultLayoutName());
1769 // set to standard-layout
1770 cursor.par()->next()->applyLayout(tclass.defaultLayoutName());
1773 // if the cursor is at the beginning of a row without prior newline,
1775 // This touches only the screen-update. Otherwise we would may have
1776 // an empty row on the screen
1777 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1778 && cursor.row()->pos() == cursor.pos())
1783 status(bview, LyXText::NEED_MORE_REFRESH);
1784 refresh_row = cursor.row();
1785 refresh_y = cursor.y() - cursor.row()->baseline();
1787 // Do not forget the special right address boxes
1788 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1789 while (refresh_row->previous() &&
1790 refresh_row->previous()->par() == refresh_row->par())
1792 refresh_row = refresh_row->previous();
1793 refresh_y -= refresh_row->height();
1796 removeParagraph(cursor.row());
1798 // set the dimensions of the cursor row
1799 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1801 setHeightOfRow(bview, cursor.row());
1803 while (cursor.par()->next()->size()
1804 && cursor.par()->next()->isNewline(0))
1805 cursor.par()->next()->erase(0);
1807 insertParagraph(bview, cursor.par()->next(), cursor.row());
1809 updateCounters(bview, cursor.row()->previous());
1811 // This check is necessary. Otherwise the new empty paragraph will
1812 // be deleted automatically. And it is more friendly for the user!
1813 if (cursor.pos() || isempty)
1814 setCursor(bview, cursor.par()->next(), 0);
1816 setCursor(bview, cursor.par(), 0);
1818 if (cursor.row()->next())
1819 breakAgain(bview, cursor.row()->next());
1825 // Just a macro to make some thing easier.
1826 void LyXText::redoParagraph(BufferView * bview) const
1829 redoParagraphs(bview, cursor, cursor.par()->next());
1830 setCursorIntern(bview, cursor.par(), cursor.pos());
1834 // insert a character, moves all the following breaks in the
1835 // same Paragraph one to the right and make a rebreak
1836 void LyXText::insertChar(BufferView * bview, char c)
1838 setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
1840 // When the free-spacing option is set for the current layout,
1841 // disable the double-space checking
1843 bool const freeSpacing =
1844 textclasslist[bview->buffer()->params.textclass][cursor.row()->par()->layout()].free_spacing ||
1845 cursor.row()->par()->isFreeSpacing();
1847 if (lyxrc.auto_number) {
1848 static string const number_operators = "+-/*";
1849 static string const number_unary_operators = "+-";
1850 static string const number_seperators = ".,:";
1852 if (current_font.number() == LyXFont::ON) {
1853 if (!IsDigit(c) && !contains(number_operators, c) &&
1854 !(contains(number_seperators, c) &&
1855 cursor.pos() >= 1 &&
1856 cursor.pos() < cursor.par()->size() &&
1857 getFont(bview->buffer(),
1859 cursor.pos()).number() == LyXFont::ON &&
1860 getFont(bview->buffer(),
1862 cursor.pos() - 1).number() == LyXFont::ON)
1864 number(bview); // Set current_font.number to OFF
1865 } else if (IsDigit(c) &&
1866 real_current_font.isVisibleRightToLeft()) {
1867 number(bview); // Set current_font.number to ON
1869 if (cursor.pos() > 0) {
1870 char const c = cursor.par()->getChar(cursor.pos() - 1);
1871 if (contains(number_unary_operators, c) &&
1872 (cursor.pos() == 1 ||
1873 cursor.par()->isSeparator(cursor.pos() - 2) ||
1874 cursor.par()->isNewline(cursor.pos() - 2))
1876 setCharFont(bview->buffer(),
1880 } else if (contains(number_seperators, c) &&
1881 cursor.pos() >= 2 &&
1882 getFont(bview->buffer(),
1884 cursor.pos() - 2).number() == LyXFont::ON) {
1885 setCharFont(bview->buffer(),
1895 // First check, if there will be two blanks together or a blank at
1896 // the beginning of a paragraph.
1897 // I decided to handle blanks like normal characters, the main
1898 // difference are the special checks when calculating the row.fill
1899 // (blank does not count at the end of a row) and the check here
1901 // The bug is triggered when we type in a description environment:
1902 // The current_font is not changed when we go from label to main text
1903 // and it should (along with realtmpfont) when we type the space.
1904 // CHECK There is a bug here! (Asger)
1906 LyXFont realtmpfont = real_current_font;
1907 LyXFont rawtmpfont = current_font;
1908 // store the current font. This is because of the use of cursor
1909 // movements. The moving cursor would refresh the current font
1911 // Get the font that is used to calculate the baselineskip
1912 pos_type const lastpos = cursor.par()->size();
1913 LyXFont rawparfont =
1914 cursor.par()->getFontSettings(bview->buffer()->params,
1917 bool jumped_over_space = false;
1919 if (!freeSpacing && IsLineSeparatorChar(c)) {
1920 if ((cursor.pos() > 0
1921 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1922 || (cursor.pos() > 0
1923 && cursor.par()->isNewline(cursor.pos() - 1))
1924 || (cursor.pos() == 0)) {
1925 static bool sent_space_message = false;
1926 if (!sent_space_message) {
1927 if (cursor.pos() == 0)
1928 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1930 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1931 sent_space_message = true;
1936 } else if (IsNewlineChar(c)) {
1937 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1943 // No newline at first position of a paragraph or behind labels.
1944 // TeX does not allow that
1946 if (cursor.pos() < cursor.par()->size() &&
1947 cursor.par()->isLineSeparator(cursor.pos()))
1948 // newline always after a blank!
1950 cursor.row()->fill(-1); // to force a new break
1953 // the display inset stuff
1954 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1955 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1956 if (inset && (inset->display() || inset->needFullRow())) {
1957 // force a new break
1958 cursor.row()->fill(-1); // to force a new break
1962 // get the cursor row fist
1963 Row * row = cursor.row();
1964 int y = cursor.y() - row->baseline();
1965 if (c != Paragraph::META_INSET) {
1966 // Here case LyXText::InsertInset already insertet the character
1967 cursor.par()->insertChar(cursor.pos(), c);
1969 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1971 if (!jumped_over_space) {
1972 // refresh the positions
1974 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1975 tmprow = tmprow->next();
1976 tmprow->pos(tmprow->pos() + 1);
1980 // Is there a break one row above
1981 if (row->previous() && row->previous()->par() == row->par()
1982 && (cursor.par()->isLineSeparator(cursor.pos())
1983 || cursor.par()->isNewline(cursor.pos())
1984 || ((cursor.pos() < cursor.par()->size()) &&
1985 cursor.par()->isInset(cursor.pos()+1))
1986 || cursor.row()->fill() == -1))
1988 pos_type z = nextBreakPoint(bview,
1991 if (z >= row->pos()) {
1994 // set the dimensions of the row above
1995 row->previous()->fill(fill(bview,
1999 setHeightOfRow(bview, row->previous());
2001 y -= row->previous()->height();
2003 refresh_row = row->previous();
2004 status(bview, LyXText::NEED_MORE_REFRESH);
2006 breakAgainOneRow(bview, row);
2008 current_font = rawtmpfont;
2009 real_current_font = realtmpfont;
2010 setCursor(bview, cursor.par(), cursor.pos() + 1,
2011 false, cursor.boundary());
2012 // cursor MUST be in row now.
2014 if (row->next() && row->next()->par() == row->par())
2015 need_break_row = row->next();
2019 // check, wether the last characters font has changed.
2020 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2021 && rawparfont != rawtmpfont)
2022 redoHeightOfParagraph(bview, cursor);
2029 // recalculate the fill of the row
2030 if (row->fill() >= 0) {
2031 // needed because a newline will set fill to -1. Otherwise
2032 // we would not get a rebreak!
2033 row->fill(fill(bview, row, workWidth(bview)));
2035 if (c == Paragraph::META_INSET || row->fill() < 0) {
2038 refresh_x = cursor.x();
2039 refresh_pos = cursor.pos();
2040 status(bview, LyXText::NEED_MORE_REFRESH);
2041 breakAgainOneRow(bview, row);
2042 // will the cursor be in another row now?
2043 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2044 if (row->next() && row->next()->par() == row->par())
2045 // this should always be true
2047 breakAgainOneRow(bview, row);
2049 current_font = rawtmpfont;
2050 real_current_font = realtmpfont;
2052 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2054 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2055 != cursor.boundary())
2056 setCursor(bview, cursor.par(), cursor.pos(), false,
2057 !cursor.boundary());
2058 if (row->next() && row->next()->par() == row->par())
2059 need_break_row = row->next();
2064 refresh_x = cursor.x();
2066 refresh_pos = cursor.pos();
2068 int const tmpheight = row->height();
2069 setHeightOfRow(bview, row);
2070 if (tmpheight == row->height())
2071 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2073 status(bview, LyXText::NEED_MORE_REFRESH);
2075 current_font = rawtmpfont;
2076 real_current_font = realtmpfont;
2077 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2081 // check, wether the last characters font has changed.
2082 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2083 && rawparfont != rawtmpfont) {
2084 redoHeightOfParagraph(bview, cursor);
2086 // now the special right address boxes
2087 if (textclasslist[bview->buffer()->params.textclass][cursor.par()->layout()].margintype
2088 == MARGIN_RIGHT_ADDRESS_BOX) {
2089 redoDrawingOfParagraph(bview, cursor);
2097 void LyXText::charInserted()
2099 // Here we could call FinishUndo for every 20 characters inserted.
2100 // This is from my experience how emacs does it.
2101 static unsigned int counter;
2111 void LyXText::prepareToPrint(BufferView * bview,
2112 Row * row, float & x,
2113 float & fill_separator,
2115 float & fill_label_hfill,
2121 float w = row->fill();
2123 fill_label_hfill = 0;
2125 fill_label_hfill = 0;
2128 row->par()->isRightToLeftPar(bview->buffer()->params);
2130 x = (workWidth(bview) > 0)
2131 ? rightMargin(bview->buffer(), row) : 0;
2133 x = (workWidth(bview) > 0)
2134 ? leftMargin(bview, row) : 0;
2136 // is there a manual margin with a manual label
2137 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2138 LyXLayout const & layout = tclass[row->par()->layout()];
2140 if (layout.margintype == MARGIN_MANUAL
2141 && layout.labeltype == LABEL_MANUAL) {
2142 // one more since labels are left aligned
2143 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2144 if (nlh && !row->par()->getLabelWidthString().empty()) {
2145 fill_label_hfill = labelFill(bview, row) / nlh;
2149 // are there any hfills in the row?
2150 float const nh = numberOfHfills(bview->buffer(), row);
2154 fill_hfill = w / nh;
2155 // we don't have to look at the alignment if it is ALIGN_LEFT and
2156 // if the row is already larger then the permitted width as then
2157 // we force the LEFT_ALIGN'edness!
2158 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2159 // is it block, flushleft or flushright?
2160 // set x how you need it
2162 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2163 align = layout.align;
2165 align = row->par()->params().align();
2168 // center displayed insets
2170 if (row->par()->isInset(row->pos())
2171 && (inset=row->par()->getInset(row->pos()))
2172 && (inset->display())) // || (inset->scroll() < 0)))
2173 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2174 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2175 // ERT insets should always be LEFT ALIGNED on screen
2176 inset = row->par()->inInset();
2177 if (inset && inset->owner() &&
2178 inset->owner()->lyxCode() == Inset::ERT_CODE)
2180 align = LYX_ALIGN_LEFT;
2184 case LYX_ALIGN_BLOCK:
2185 ns = numberOfSeparators(bview->buffer(), row);
2186 if (ns && row->next() && row->next()->par() == row->par() &&
2187 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2188 && !(row->next()->par()->isInset(row->next()->pos())
2189 && row->next()->par()->getInset(row->next()->pos())
2190 && row->next()->par()->getInset(row->next()->pos())->display())
2193 fill_separator = w / ns;
2194 } else if (is_rtl) {
2198 case LYX_ALIGN_RIGHT:
2201 case LYX_ALIGN_CENTER:
2209 computeBidiTables(bview->buffer(), row);
2211 pos_type main_body =
2212 beginningOfMainBody(bview->buffer(), row->par());
2213 pos_type last = rowLast(row);
2215 if (main_body > 0 &&
2216 (main_body - 1 > last ||
2217 !row->par()->isLineSeparator(main_body - 1))) {
2218 x += font_metrics::width(layout.labelsep,
2219 getLabelFont(bview->buffer(), row->par()));
2220 if (main_body - 1 <= last)
2221 x += fill_label_hfill;
2227 // important for the screen
2230 // the cursor set functions have a special mechanism. When they
2231 // realize, that you left an empty paragraph, they will delete it.
2232 // They also delete the corresponding row
2234 void LyXText::cursorRightOneWord(BufferView * bview) const
2236 // treat floats, HFills and Insets as words
2237 LyXCursor tmpcursor = cursor;
2238 // CHECK See comment on top of text.C
2240 if (tmpcursor.pos() == tmpcursor.par()->size()
2241 && tmpcursor.par()->next()) {
2242 tmpcursor.par(tmpcursor.par()->next());
2247 // Skip through initial nonword stuff.
2248 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2249 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2250 // printf("Current pos1 %d", tmpcursor.pos()) ;
2251 tmpcursor.pos(tmpcursor.pos() + 1);
2254 // Advance through word.
2255 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2256 tmpcursor.par()->isWord(tmpcursor.pos())) {
2257 // printf("Current pos2 %d", tmpcursor.pos()) ;
2258 tmpcursor.pos(tmpcursor.pos() + 1);
2262 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2266 void LyXText::cursorTab(BufferView * bview) const
2268 LyXCursor tmpcursor = cursor;
2269 while (tmpcursor.pos() < tmpcursor.par()->size()
2270 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2271 tmpcursor.pos(tmpcursor.pos() + 1);
2273 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2274 if (tmpcursor.par()->next()) {
2275 tmpcursor.par(tmpcursor.par()->next());
2279 tmpcursor.pos(tmpcursor.pos() + 1);
2280 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2284 // Skip initial whitespace at end of word and move cursor to *start*
2285 // of prior word, not to end of next prior word.
2286 void LyXText::cursorLeftOneWord(BufferView * bview) const
2288 LyXCursor tmpcursor = cursor;
2289 cursorLeftOneWord(tmpcursor);
2290 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2294 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2296 // treat HFills, floats and Insets as words
2299 && (cur.par()->isSeparator(cur.pos() - 1)
2300 || cur.par()->isKomma(cur.pos() - 1))
2301 && !(cur.par()->isHfill(cur.pos() - 1)
2302 || cur.par()->isInset(cur.pos() - 1)))
2303 cur.pos(cur.pos() - 1);
2306 && (cur.par()->isInset(cur.pos() - 1)
2307 || cur.par()->isHfill(cur.pos() - 1))) {
2308 cur.pos(cur.pos() - 1);
2309 } else if (!cur.pos()) {
2310 if (cur.par()->previous()) {
2311 cur.par(cur.par()->previous());
2312 cur.pos(cur.par()->size());
2314 } else { // Here, cur != 0
2315 while (cur.pos() > 0 &&
2316 cur.par()->isWord(cur.pos()-1))
2317 cur.pos(cur.pos() - 1);
2322 // Select current word. This depends on behaviour of
2323 // CursorLeftOneWord(), so it is patched as well.
2324 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2325 word_location const loc) const
2327 // first put the cursor where we wana start to select the word
2330 case WHOLE_WORD_STRICT:
2331 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2332 || cursor.par()->isSeparator(cursor.pos())
2333 || cursor.par()->isKomma(cursor.pos())
2334 || cursor.par()->isSeparator(cursor.pos() -1)
2335 || cursor.par()->isKomma(cursor.pos() -1)) {
2339 // no break here, we go to the next
2342 // Move cursor to the beginning, when not already there.
2343 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2344 && !from.par()->isKomma(from.pos() - 1))
2345 cursorLeftOneWord(from);
2348 // always move the cursor to the beginning of previous word
2349 cursorLeftOneWord(from);
2352 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2358 while (to.pos() < to.par()->size()
2359 && !to.par()->isSeparator(to.pos())
2360 && !to.par()->isKomma(to.pos())
2361 && !to.par()->isHfill(to.pos()))
2363 to.pos(to.pos() + 1);
2368 void LyXText::selectWord(BufferView * bview, word_location const loc)
2372 getWord(from, to, loc);
2374 setCursor(bview, from.par(), from.pos());
2377 selection.cursor = cursor;
2378 setCursor(bview, to.par(), to.pos());
2379 setSelection(bview);
2383 // Select the word currently under the cursor when no
2384 // selection is currently set
2385 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2386 word_location const loc)
2388 if (!selection.set()) {
2389 selectWord(bview, loc);
2390 return selection.set();
2396 // This function is only used by the spellchecker for NextWord().
2397 // It doesn't handle LYX_ACCENTs and probably never will.
2398 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2399 float & value) const
2401 if (the_locking_inset) {
2402 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2404 value += float(cursor.y())/float(height);
2407 // we have to go on checking so move cusor to the next char
2408 if (cursor.pos() == cursor.par()->size()) {
2409 if (!cursor.par()->next())
2411 cursor.par(cursor.par()->next());
2414 cursor.pos(cursor.pos() + 1);
2416 Paragraph * tmppar = cursor.par();
2418 // If this is not the very first word, skip rest of
2419 // current word because we are probably in the middle
2420 // of a word if there is text here.
2421 if (cursor.pos() || cursor.par()->previous()) {
2422 while (cursor.pos() < cursor.par()->size()
2423 && cursor.par()->isLetter(cursor.pos()))
2424 cursor.pos(cursor.pos() + 1);
2427 // Now, skip until we have real text (will jump paragraphs)
2428 while ((cursor.par()->size() > cursor.pos()
2429 && (!cursor.par()->isLetter(cursor.pos()))
2430 && (!cursor.par()->isInset(cursor.pos()) ||
2431 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2432 || (cursor.par()->size() == cursor.pos()
2433 && cursor.par()->next()))
2435 if (cursor.pos() == cursor.par()->size()) {
2436 cursor.par(cursor.par()->next());
2439 cursor.pos(cursor.pos() + 1);
2442 // now check if we hit an inset so it has to be a inset containing text!
2443 if (cursor.pos() < cursor.par()->size() &&
2444 cursor.par()->isInset(cursor.pos()))
2447 cursor.par()->getInset(cursor.pos())->edit(bview);
2448 // now call us again to do the above trick
2449 // but obviously we have to start from down below ;)
2450 return bview->text->selectNextWordToSpellcheck(bview, value);
2453 // Update the value if we changed paragraphs
2454 if (cursor.par() != tmppar) {
2455 setCursor(bview, cursor.par(), cursor.pos());
2456 value = float(cursor.y())/float(height);
2459 // Start the selection from here
2460 selection.cursor = cursor;
2462 // and find the end of the word (insets like optional hyphens
2463 // and ligature break are part of a word)
2464 while (cursor.pos() < cursor.par()->size()
2465 && (cursor.par()->isLetter(cursor.pos())))
2466 cursor.pos(cursor.pos() + 1);
2468 // Finally, we copy the word to a string and return it
2470 if (selection.cursor.pos() < cursor.pos()) {
2472 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2473 if (!cursor.par()->isInset(i))
2474 str += cursor.par()->getChar(i);
2481 // This one is also only for the spellchecker
2482 void LyXText::selectSelectedWord(BufferView * bview)
2484 if (the_locking_inset) {
2485 the_locking_inset->selectSelectedWord(bview);
2488 // move cursor to the beginning
2489 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2491 // set the sel cursor
2492 selection.cursor = cursor;
2494 // now find the end of the word
2495 while (cursor.pos() < cursor.par()->size()
2496 && (cursor.par()->isLetter(cursor.pos())))
2497 cursor.pos(cursor.pos() + 1);
2499 setCursor(bview, cursor.par(), cursor.pos());
2501 // finally set the selection
2502 setSelection(bview);
2506 // Delete from cursor up to the end of the current or next word.
2507 void LyXText::deleteWordForward(BufferView * bview)
2509 if (!cursor.par()->size())
2512 LyXCursor tmpcursor = cursor;
2513 tmpcursor.row(0); //Â ??
2514 selection.set(true); // to avoid deletion
2515 cursorRightOneWord(bview);
2516 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2517 selection.cursor = cursor;
2519 setSelection(bview);
2521 // Great, CutSelection() gets rid of multiple spaces.
2522 cutSelection(bview, true, false);
2527 // Delete from cursor to start of current or prior word.
2528 void LyXText::deleteWordBackward(BufferView * bview)
2530 if (!cursor.par()->size())
2533 LyXCursor tmpcursor = cursor;
2534 tmpcursor.row(0); // ??
2535 selection.set(true); // to avoid deletion
2536 cursorLeftOneWord(bview);
2537 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2538 selection.cursor = cursor;
2540 setSelection(bview);
2541 cutSelection(bview, true, false);
2546 // Kill to end of line.
2547 void LyXText::deleteLineForward(BufferView * bview)
2549 if (!cursor.par()->size())
2550 // Paragraph is empty, so we just go to the right
2553 LyXCursor tmpcursor = cursor;
2554 // We can't store the row over a regular setCursor
2555 // so we set it to 0 and reset it afterwards.
2556 tmpcursor.row(0); //Â ??
2557 selection.set(true); // to avoid deletion
2559 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2560 selection.cursor = cursor;
2562 setSelection(bview);
2563 // What is this test for ??? (JMarc)
2564 if (!selection.set()) {
2565 deleteWordForward(bview);
2567 cutSelection(bview, true, false);
2573 // Change the case of a word at cursor position.
2574 // This function directly manipulates Paragraph::text because there
2575 // is no Paragraph::SetChar currently. I did what I could to ensure
2576 // that it is correct. I guess part of it should be moved to
2577 // Paragraph, but it will have to change for 1.1 anyway. At least
2578 // it does not access outside of the allocated array as the older
2579 // version did. (JMarc)
2580 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2585 if (selection.set()) {
2586 from = selection.start;
2589 getWord(from, to, PARTIAL_WORD);
2590 setCursor(bview, to.par(), to.pos() + 1);
2593 changeRegionCase(bview, from, to, action);
2597 void LyXText::changeRegionCase(BufferView * bview,
2598 LyXCursor const & from,
2599 LyXCursor const & to,
2600 LyXText::TextCase action)
2602 lyx::Assert(from <= to);
2604 setUndo(bview, Undo::FINISH, from.par(), to.par()->next());
2606 pos_type pos = from.pos();
2607 Paragraph * par = from.par();
2609 while (par && (pos != to.pos() || par != to.par())) {
2610 unsigned char c = par->getChar(pos);
2611 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2613 case text_lowercase:
2616 case text_capitalization:
2618 action = text_lowercase;
2620 case text_uppercase:
2625 par->setChar(pos, c);
2626 checkParagraph(bview, par, pos);
2629 if (pos == par->size()) {
2634 if (to.row() != from.row()) {
2635 refresh_y = from.y() - from.row()->baseline();
2636 refresh_row = from.row();
2637 status(bview, LyXText::NEED_MORE_REFRESH);
2642 void LyXText::transposeChars(BufferView & bview)
2644 Paragraph * tmppar = cursor.par();
2646 setUndo(&bview, Undo::FINISH, tmppar, tmppar->next());
2648 pos_type tmppos = cursor.pos();
2650 // First decide if it is possible to transpose at all
2652 // We are at the beginning of a paragraph.
2653 if (tmppos == 0) return;
2655 // We are at the end of a paragraph.
2656 if (tmppos == tmppar->size() - 1) return;
2658 unsigned char c1 = tmppar->getChar(tmppos);
2659 unsigned char c2 = tmppar->getChar(tmppos - 1);
2661 if (c1 != Paragraph::META_INSET
2662 && c2 != Paragraph::META_INSET) {
2663 tmppar->setChar(tmppos, c2);
2664 tmppar->setChar(tmppos - 1, c1);
2666 // We should have an implementation that handles insets
2667 // as well, but that will have to come later. (Lgb)
2668 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2672 void LyXText::Delete(BufferView * bview)
2674 // this is a very easy implementation
2676 LyXCursor old_cursor = cursor;
2677 int const old_cur_par_id = old_cursor.par()->id();
2678 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2679 old_cursor.par()->previous()->id() : 0;
2681 // just move to the right
2684 // CHECK Look at the comment here.
2685 // This check is not very good...
2686 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2687 // and that can very well delete the par or par->previous in
2688 // old_cursor. Will a solution where we compare paragraph id's
2690 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2691 == old_cur_par_prev_id
2692 && cursor.par()->id() != old_cur_par_id) {
2693 // delete-empty-paragraph-mechanism has done it
2697 // if you had success make a backspace
2698 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2699 LyXCursor tmpcursor = cursor;
2700 // to make sure undo gets the right cursor position
2701 cursor = old_cursor;
2702 setUndo(bview, Undo::DELETE,
2703 cursor.par(), cursor.par()->next());
2710 void LyXText::backspace(BufferView * bview)
2712 // Get the font that is used to calculate the baselineskip
2713 pos_type lastpos = cursor.par()->size();
2714 LyXFont rawparfont =
2715 cursor.par()->getFontSettings(bview->buffer()->params,
2718 if (cursor.pos() == 0) {
2719 // The cursor is at the beginning of a paragraph,
2720 // so the the backspace will collapse two paragraphs into one.
2722 // we may paste some paragraphs
2724 // is it an empty paragraph?
2727 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2728 // This is an empty paragraph and we delete it just by moving the cursor one step
2729 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2730 // of the paragraph.
2732 if (cursor.par()->previous()) {
2733 Paragraph * tmppar = cursor.par()->previous();
2734 if (cursor.par()->layout() == tmppar->layout()
2735 && cursor.par()->getAlign() == tmppar->getAlign()) {
2736 // Inherit bottom DTD from the paragraph below.
2737 // (the one we are deleting)
2738 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2739 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2740 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2745 // the layout things can change the height of a row !
2746 int const tmpheight = cursor.row()->height();
2747 setHeightOfRow(bview, cursor.row());
2748 if (cursor.row()->height() != tmpheight) {
2749 refresh_y = cursor.y() - cursor.row()->baseline();
2750 refresh_row = cursor.row();
2751 status(bview, LyXText::NEED_MORE_REFRESH);
2757 if (cursor.par()->previous()) {
2758 setUndo(bview, Undo::DELETE,
2759 cursor.par()->previous(), cursor.par()->next());
2762 Paragraph * tmppar = cursor.par();
2763 Row * tmprow = cursor.row();
2765 // We used to do cursorLeftIntern() here, but it is
2766 // not a good idea since it triggers the auto-delete
2767 // mechanism. So we do a cursorLeftIntern()-lite,
2768 // without the dreaded mechanism. (JMarc)
2769 if (cursor.par()->previous()) {
2770 // steps into the above paragraph.
2771 setCursorIntern(bview, cursor.par()->previous(),
2772 cursor.par()->previous()->size(),
2776 // Pasting is not allowed, if the paragraphs have different
2777 // layout. I think it is a real bug of all other
2778 // word processors to allow it. It confuses the user.
2779 // Even so with a footnote paragraph and a non-footnote
2780 // paragraph. I will not allow pasting in this case,
2781 // because the user would be confused if the footnote behaves
2782 // different wether it is open or closed.
2784 // Correction: Pasting is always allowed with standard-layout
2785 LyXTextClass const & tclass = textclasslist[bview->buffer()->params.textclass];
2787 if (cursor.par() != tmppar
2788 && (cursor.par()->layout() == tmppar->layout()
2789 || tmppar->layout() == tclass.defaultLayoutName())
2790 && cursor.par()->getAlign() == tmppar->getAlign()) {
2791 removeParagraph(tmprow);
2793 cursor.par()->pasteParagraph(bview->buffer()->params);
2795 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2796 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2797 // strangely enough it seems that commenting out the line above removes
2798 // most or all of the segfaults. I will however also try to move the
2799 // two Remove... lines in front of the PasteParagraph too.
2802 cursor.pos(cursor.pos() - 1);
2804 status(bview, LyXText::NEED_MORE_REFRESH);
2805 refresh_row = cursor.row();
2806 refresh_y = cursor.y() - cursor.row()->baseline();
2808 // remove the lost paragraph
2809 // This one is not safe, since the paragraph that the tmprow and the
2810 // following rows belong to has been deleted by the PasteParagraph
2811 // above. The question is... could this be moved in front of the
2813 //RemoveParagraph(tmprow);
2814 //RemoveRow(tmprow);
2816 // This rebuilds the rows.
2817 appendParagraph(bview, cursor.row());
2818 updateCounters(bview, cursor.row());
2820 // the row may have changed, block, hfills etc.
2821 setCursor(bview, cursor.par(), cursor.pos(), false);
2824 // this is the code for a normal backspace, not pasting
2826 setUndo(bview, Undo::DELETE,
2827 cursor.par(), cursor.par()->next());
2828 // We used to do cursorLeftIntern() here, but it is
2829 // not a good idea since it triggers the auto-delete
2830 // mechanism. So we do a cursorLeftIntern()-lite,
2831 // without the dreaded mechanism. (JMarc)
2832 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2833 false, cursor.boundary());
2835 // some insets are undeletable here
2836 if (cursor.par()->isInset(cursor.pos())) {
2837 if (!cursor.par()->getInset(cursor.pos())->deletable())
2839 // force complete redo when erasing display insets
2840 // this is a cruel method but safe..... Matthias
2841 if (cursor.par()->getInset(cursor.pos())->display() ||
2842 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2843 cursor.par()->erase(cursor.pos());
2844 redoParagraph(bview);
2849 Row * row = cursor.row();
2850 int y = cursor.y() - row->baseline();
2852 // remember that a space at the end of a row doesnt count
2853 // when calculating the fill
2854 if (cursor.pos() < rowLast(row) ||
2855 !cursor.par()->isLineSeparator(cursor.pos())) {
2856 row->fill(row->fill() + singleWidth(bview,
2861 // some special code when deleting a newline. This is similar
2862 // to the behavior when pasting paragraphs
2863 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2864 cursor.par()->erase(cursor.pos());
2865 // refresh the positions
2867 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2868 tmprow = tmprow->next();
2869 tmprow->pos(tmprow->pos() - 1);
2871 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2872 cursor.pos(cursor.pos() - 1);
2874 if (cursor.pos() < cursor.par()->size()
2875 && !cursor.par()->isSeparator(cursor.pos())) {
2876 cursor.par()->insertChar(cursor.pos(), ' ');
2877 setCharFont(bview->buffer(), cursor.par(),
2878 cursor.pos(), current_font);
2879 // refresh the positions
2881 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2882 tmprow = tmprow->next();
2883 tmprow->pos(tmprow->pos() + 1);
2887 cursor.par()->erase(cursor.pos());
2889 // refresh the positions
2891 while (tmprow->next()
2892 && tmprow->next()->par() == row->par()) {
2893 tmprow = tmprow->next();
2894 tmprow->pos(tmprow->pos() - 1);
2897 // delete newlines at the beginning of paragraphs
2898 while (cursor.par()->size() &&
2899 cursor.par()->isNewline(cursor.pos()) &&
2900 cursor.pos() == beginningOfMainBody(bview->buffer(),
2902 cursor.par()->erase(cursor.pos());
2903 // refresh the positions
2905 while (tmprow->next() &&
2906 tmprow->next()->par() == row->par()) {
2907 tmprow = tmprow->next();
2908 tmprow->pos(tmprow->pos() - 1);
2913 // is there a break one row above
2914 if (row->previous() && row->previous()->par() == row->par()) {
2915 z = nextBreakPoint(bview, row->previous(),
2917 if (z >= row->pos()) {
2920 Row * tmprow = row->previous();
2922 // maybe the current row is now empty
2923 if (row->pos() >= row->par()->size()) {
2928 breakAgainOneRow(bview, row);
2929 if (row->next() && row->next()->par() == row->par())
2930 need_break_row = row->next();
2935 // set the dimensions of the row above
2936 y -= tmprow->height();
2937 tmprow->fill(fill(bview, tmprow,
2939 setHeightOfRow(bview, tmprow);
2942 refresh_row = tmprow;
2943 status(bview, LyXText::NEED_MORE_REFRESH);
2944 setCursor(bview, cursor.par(), cursor.pos(),
2945 false, cursor.boundary());
2946 //current_font = rawtmpfont;
2947 //real_current_font = realtmpfont;
2948 // check, whether the last character's font has changed.
2950 cursor.par()->getFontSettings(bview->buffer()->params,
2951 cursor.par()->size() - 1))
2952 redoHeightOfParagraph(bview, cursor);
2957 // break the cursor row again
2958 if (row->next() && row->next()->par() == row->par() &&
2959 (rowLast(row) == row->par()->size() - 1 ||
2960 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2962 // it can happen that a paragraph loses one row
2963 // without a real breakup. This is when a word
2964 // is to long to be broken. Well, I don t care this
2966 if (rowLast(row) == row->par()->size() - 1)
2967 removeRow(row->next());
2971 status(bview, LyXText::NEED_MORE_REFRESH);
2973 breakAgainOneRow(bview, row);
2974 // will the cursor be in another row now?
2975 if (row->next() && row->next()->par() == row->par() &&
2976 rowLast(row) <= cursor.pos()) {
2978 breakAgainOneRow(bview, row);
2981 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2983 if (row->next() && row->next()->par() == row->par())
2984 need_break_row = row->next();
2988 // set the dimensions of the row
2989 row->fill(fill(bview, row, workWidth(bview)));
2990 int const tmpheight = row->height();
2991 setHeightOfRow(bview, row);
2992 if (tmpheight == row->height())
2993 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2995 status(bview, LyXText::NEED_MORE_REFRESH);
2998 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3002 // current_font = rawtmpfont;
3003 // real_current_font = realtmpfont;
3005 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
3006 != cursor.boundary())
3007 setCursor(bview, cursor.par(), cursor.pos(), false,
3008 !cursor.boundary());
3010 lastpos = cursor.par()->size();
3011 if (cursor.pos() == lastpos)
3012 setCurrentFont(bview);
3014 // check, whether the last characters font has changed.
3016 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
3017 redoHeightOfParagraph(bview, cursor);
3019 // now the special right address boxes
3021 [bview->buffer()->params.textclass]
3022 [cursor.par()->layout()].margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3023 redoDrawingOfParagraph(bview, cursor);
3029 bool LyXText::paintRowBackground(DrawRowParams & p)
3031 bool clear_area = true;
3033 LyXFont font(LyXFont::ALL_SANE);
3035 pos_type const last = rowLastPrintable(p.row);
3037 if (!p.bv->screen()->forceClear() && last == p.row->pos()
3038 && p.row->par()->isInset(p.row->pos())) {
3039 inset = p.row->par()->getInset(p.row->pos());
3041 clear_area = inset->doClearArea();
3051 int const y = p.yo < 0 ? 0 : p.yo;
3052 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3053 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3060 int h = p.row->baseline() - inset->ascent(p.bv, font);
3062 // first clear the whole row above the inset!
3064 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3067 // clear the space below the inset!
3068 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3069 if ((p.row->height() - h) > 0) {
3070 p.pain->fillRectangle(p.xo, p.yo + h,
3071 p.width, p.row->height() - h, backgroundColor());
3074 // clear the space behind the inset, if needed
3075 if (!inset->display() && !inset->needFullRow()) {
3076 int const xp = int(p.x) + inset->width(p.bv, font);
3077 if (p.width - xp > 0) {
3078 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3079 p.row->height(), backgroundColor());
3087 void LyXText::paintRowSelection(DrawRowParams & p)
3089 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3091 // the current selection
3092 int const startx = selection.start.x();
3093 int const endx = selection.end.x();
3094 int const starty = selection.start.y();
3095 int const endy = selection.end.y();
3096 Row const * startrow = selection.start.row();
3097 Row const * endrow = selection.end.row();
3101 if (bidi_same_direction) {
3105 int h = row->height();
3107 if (startrow == row && endrow == row) {
3108 if (startx < endx) {
3111 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3115 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3117 } else if (startrow == row) {
3118 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3119 int const w = (is_rtl) ? startx : (p.width - startx);
3120 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3121 } else if (endrow == row) {
3122 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3123 int const w = (is_rtl) ? (p.width - endx) : endx;
3124 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3125 } else if (p.y > starty && p.y < endy) {
3126 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3129 } else if (startrow != row && endrow != row) {
3131 int h = row->height();
3132 if (p.y > starty && p.y < endy) {
3133 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3138 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3144 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3146 Buffer const * buffer = p.bv->buffer();
3147 Paragraph * par = row->par();
3148 pos_type main_body = beginningOfMainBody(buffer, par);
3149 pos_type const last = rowLastPrintable(row);
3151 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3152 pos_type pos = vis2log(vpos);
3153 float const old_tmpx = tmpx;
3154 if (main_body > 0 && pos == main_body - 1) {
3155 LyXLayout const & layout =
3157 [buffer->params.textclass]
3159 LyXFont const lfont = getLabelFont(buffer, par);
3161 tmpx += p.label_hfill + font_metrics::width(layout.labelsep, lfont);
3163 if (par->isLineSeparator(main_body - 1))
3164 tmpx -= singleWidth(p.bv, par, main_body - 1);
3167 if (hfillExpansion(buffer, row, pos)) {
3168 tmpx += singleWidth(p.bv, par, pos);
3169 if (pos >= main_body)
3172 tmpx += p.label_hfill;
3175 else if (par->isSeparator(pos)) {
3176 tmpx += singleWidth(p.bv, par, pos);
3177 if (pos >= main_body)
3178 tmpx += p.separator;
3180 tmpx += singleWidth(p.bv, par, pos);
3183 if ((startrow != row || selection.start.pos() <= pos) &&
3184 (endrow != row || pos < selection.end.pos())) {
3185 // Here we do not use p.x as p.xo was added to p.x.
3186 p.pain->fillRectangle(int(old_tmpx), p.yo,
3187 int(tmpx - old_tmpx + 1),
3188 row->height(), LColor::selection);
3191 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3192 p.pain->fillRectangle(p.xo + int(tmpx),
3193 p.yo, int(p.bv->workWidth() - tmpx),
3194 row->height(), LColor::selection);
3200 void LyXText::paintRowAppendix(DrawRowParams & p)
3202 // FIXME: can be just p.width ?
3203 int const ww = p.bv->workWidth();
3204 Paragraph * firstpar = p.row->par();
3206 if (firstpar->params().appendix()) {
3207 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3208 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3213 void LyXText::paintRowDepthBar(DrawRowParams & p)
3215 Paragraph::depth_type const depth = p.row->par()->getDepth();
3220 Paragraph::depth_type prev_depth = 0;
3221 if (p.row->previous())
3222 prev_depth = p.row->previous()->par()->getDepth();
3223 Paragraph::depth_type next_depth = 0;
3225 next_depth = p.row->next()->par()->getDepth();
3227 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3228 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3229 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3231 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3233 int const w = LYX_PAPER_MARGIN / 5;
3235 if (i > prev_depth) {
3236 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3238 if (i > next_depth) {
3239 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3245 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3247 int const arrow_size = 4;
3248 int const space_size = int(vsp.inPixels(bv));
3250 if (vsp.kind() != VSpace::LENGTH) {
3256 int const min_size = max(3 * arrow_size,
3257 font_metrics::maxAscent(font)
3258 + font_metrics::maxDescent(font));
3260 if (vsp.length().len().value() < 0.0)
3263 return max(min_size, space_size);
3267 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3268 VSpace const & vsp, int start)
3270 int const arrow_size = 4;
3271 int const size = getLengthMarkerHeight(p.bv, vsp);
3272 int const end = start + size;
3274 // the label to display (if any)
3276 // y-values for top arrow
3278 // y-values for bottom arrow
3280 switch (vsp.kind()) {
3281 case VSpace::LENGTH:
3283 str = prefix + " (" + vsp.asLyXCommand() + ")";
3284 // adding or removing space
3285 bool const added = !(vsp.length().len().value() < 0.0);
3286 ty1 = added ? (start + arrow_size) : start;
3287 ty2 = added ? start : (start + arrow_size);
3288 by1 = added ? (end - arrow_size) : end;
3289 by2 = added ? end : (end - arrow_size);
3292 case VSpace:: VFILL:
3293 str = prefix + " (vertical fill)";
3298 // nothing to draw here
3302 int const leftx = p.xo + leftMargin(p.bv, p.row);
3303 int const midx = leftx + arrow_size;
3304 int const rightx = midx + arrow_size;
3312 font.setColor(LColor::added_space).decSize();
3313 font_metrics::rectText(str, font, w, a, d);
3315 p.pain->rectText(leftx + 2 * arrow_size + 5,
3316 start + ((end - start) / 2) + d,
3322 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3323 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3326 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3327 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3330 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3336 void LyXText::paintFirstRow(DrawRowParams & p)
3338 Paragraph * par = p.row->par();
3339 ParagraphParameters const & parparams = par->params();
3341 // start of appendix?
3342 if (parparams.startOfAppendix()) {
3343 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3348 // think about the margins
3349 if (!p.row->previous() && bv_owner)
3350 y_top += LYX_PAPER_MARGIN;
3352 // draw a top pagebreak
3353 if (parparams.pagebreakTop()) {
3354 int const y = p.yo + y_top + 2*defaultHeight();
3355 p.pain->line(p.xo, y, p.xo + p.width, y,
3356 LColor::pagebreak, Painter::line_onoffdash);
3363 pb_font.setColor(LColor::pagebreak).decSize();
3364 font_metrics::rectText(_("Page Break (top)"), pb_font, w, a, d);
3365 p.pain->rectText((p.width - w)/2, y + d,
3366 _("Page Break (top)"), pb_font,
3369 y_top += 3 * defaultHeight();
3372 // draw the additional space if needed:
3373 y_top += drawLengthMarker(p, _("Space above"),
3374 parparams.spaceTop(), p.yo + y_top);
3376 Buffer const * buffer = p.bv->buffer();
3378 LyXTextClass const & tclass = textclasslist[buffer->params.textclass];
3379 LyXLayout const & layout = tclass[par->layout()];
3381 // think about the parskip
3382 // some parskips VERY EASY IMPLEMENTATION
3383 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3384 if (par->previous()) {
3385 if (layout.latextype == LATEX_PARAGRAPH
3386 && !par->getDepth()) {
3387 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3389 LyXLayout const & playout =
3390 tclass[par->previous()->layout()];
3391 if (playout.latextype == LATEX_PARAGRAPH
3392 && !par->previous()->getDepth()) {
3393 // is it right to use defskip here, too? (AS)
3394 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3400 int const ww = p.bv->workWidth();
3403 if (parparams.lineTop()) {
3404 LyXFont font(LyXFont::ALL_SANE);
3405 int const asc = font_metrics::ascent('x', getFont(buffer, par, 0));
3409 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3410 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3411 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3412 LColor::topline, Painter::line_solid,
3413 Painter::line_thick);
3418 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3420 // should we print a label?
3421 if (layout.labeltype >= LABEL_STATIC
3422 && (layout.labeltype != LABEL_STATIC
3423 || layout.latextype != LATEX_ENVIRONMENT
3424 || par->isFirstInSequence())) {
3426 LyXFont font = getLabelFont(buffer, par);
3427 if (!par->getLabelstring().empty()) {
3429 string const str = par->getLabelstring();
3431 // this is special code for the chapter layout. This is
3432 // printed in an extra row and has a pagebreak at
3434 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3435 if (buffer->params.secnumdepth >= 0) {
3436 float spacing_val = 1.0;
3437 if (!parparams.spacing().isDefault()) {
3438 spacing_val = parparams.spacing().getValue();
3440 spacing_val = buffer->params.spacing.getValue();
3444 int(font_metrics::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3445 + int(layout.parsep) * defaultHeight();
3448 x = ww - leftMargin(p.bv, p.row) -
3449 font_metrics::width(str, font);
3452 p.pain->text(int(x),
3453 p.yo + p.row->baseline() -
3454 p.row->ascent_of_text() - maxdesc,
3459 x = ww - leftMargin(p.bv, p.row)
3460 + font_metrics::width(layout.labelsep, font);
3462 x = p.x - font_metrics::width(layout.labelsep, font)
3463 - font_metrics::width(str, font);
3466 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3469 // the labels at the top of an environment.
3470 // More or less for bibliography
3471 } else if (par->isFirstInSequence() &&
3472 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3473 layout.labeltype == LABEL_BIBLIO ||
3474 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3475 LyXFont font = getLabelFont(buffer, par);
3476 if (!par->getLabelstring().empty()) {
3477 string const str = par->getLabelstring();
3478 float spacing_val = 1.0;
3479 if (!parparams.spacing().isDefault()) {
3480 spacing_val = parparams.spacing().getValue();
3482 spacing_val = buffer->params.spacing.getValue();
3486 int(font_metrics::maxDescent(font) * layout.spacing.getValue() * spacing_val
3487 + (layout.labelbottomsep * defaultHeight()));
3490 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3491 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3492 + ww - rightMargin(buffer, p.row)) / 2;
3493 x -= font_metrics::width(str, font) / 2;
3494 } else if (is_rtl) {
3495 x = ww - leftMargin(p.bv, p.row) -
3496 font_metrics::width(str, font);
3498 p.pain->text(int(x), p.yo + p.row->baseline()
3499 - p.row->ascent_of_text() - maxdesc,
3504 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3505 LyXFont font = getLayoutFont(buffer, par);
3508 x = ww - leftMargin(p.bv, p.row)
3509 + font_metrics::width(layout.labelsep, font);
3511 x = p.x - font_metrics::width(layout.labelsep, font)
3512 - par->bibkey->width(p.bv, font);
3514 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3519 void LyXText::paintLastRow(DrawRowParams & p)
3521 Paragraph * par = p.row->par();
3522 ParagraphParameters const & parparams = par->params();
3523 int y_bottom = p.row->height() - 1;
3525 // think about the margins
3526 if (!p.row->next() && bv_owner)
3527 y_bottom -= LYX_PAPER_MARGIN;
3529 int const ww = p.bv->workWidth();
3531 // draw a bottom pagebreak
3532 if (parparams.pagebreakBottom()) {
3534 pb_font.setColor(LColor::pagebreak).decSize();
3535 int const y = p.yo + y_bottom - 2 * defaultHeight();
3537 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3538 Painter::line_onoffdash);
3543 font_metrics::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3544 p.pain->rectText((ww - w) / 2, y + d,
3545 _("Page Break (bottom)"),
3546 pb_font, backgroundColor(), backgroundColor());
3548 y_bottom -= 3 * defaultHeight();
3551 // draw the additional space if needed:
3552 int const height = getLengthMarkerHeight(p.bv,
3553 parparams.spaceBottom());
3554 y_bottom -= drawLengthMarker(p, _("Space below"),
3555 parparams.spaceBottom(),
3556 p.yo + y_bottom - height);
3558 Buffer const * buffer = p.bv->buffer();
3560 // draw a bottom line
3561 if (parparams.lineBottom()) {
3562 LyXFont font(LyXFont::ALL_SANE);
3563 int const asc = font_metrics::ascent('x',
3564 getFont(buffer, par,
3565 max(pos_type(0), par->size() - 1)));
3569 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3570 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3571 int const y = p.yo + y_bottom;
3572 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3573 Painter::line_thick);
3578 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3579 int const endlabel = par->getEndLabel(buffer->params);
3584 case END_LABEL_FILLED_BOX:
3586 LyXFont const font = getLabelFont(buffer, par);
3587 int const size = int(0.75 * font_metrics::maxAscent(font));
3588 int const y = (p.yo + p.row->baseline()) - size;
3589 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3591 if (p.row->fill() <= size)
3592 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3594 if (endlabel == END_LABEL_BOX) {
3595 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3597 p.pain->fillRectangle(x, y, size, size,
3602 case END_LABEL_STATIC:
3604 LyXFont font(LyXFont::ALL_SANE);
3605 string const & layout = par->layout();
3606 string const str = textclasslist[buffer->params.textclass][layout].endlabelstring();
3607 font = getLabelFont(buffer, par);
3608 int const x = is_rtl ?
3609 int(p.x) - font_metrics::width(str, font)
3610 : ww - rightMargin(buffer, p.row) - p.row->fill();
3611 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3614 case END_LABEL_NO_LABEL:
3619 void LyXText::paintRowText(DrawRowParams & p)
3621 Paragraph * par = p.row->par();
3622 Buffer const * buffer = p.bv->buffer();
3624 pos_type const last = rowLastPrintable(p.row);
3625 pos_type main_body =
3626 beginningOfMainBody(buffer, par);
3627 if (main_body > 0 &&
3628 (main_body - 1 > last ||
3629 !par->isLineSeparator(main_body - 1))) {
3633 LyXLayout const & layout =
3634 textclasslist[buffer->params.textclass][par->layout()];
3636 pos_type vpos = p.row->pos();
3637 while (vpos <= last) {
3638 if (p.x > p.bv->workWidth())
3640 pos_type pos = vis2log(vpos);
3641 if (p.x + singleWidth(p.bv, par, pos) < 0) {
3642 p.x += singleWidth(p.bv, par, pos);
3646 if (main_body > 0 && pos == main_body - 1) {
3647 int const lwidth = font_metrics::width(layout.labelsep,
3648 getLabelFont(buffer, par));
3650 p.x += p.label_hfill + lwidth
3651 - singleWidth(p.bv, par, main_body - 1);
3654 if (par->isHfill(pos)) {
3657 int const y0 = p.yo + p.row->baseline();
3658 int const y1 = y0 - defaultHeight() / 2;
3660 p.pain->line(int(p.x), y1, int(p.x), y0,
3661 LColor::added_space);
3663 if (hfillExpansion(buffer, p.row, pos)) {
3664 int const y2 = (y0 + y1) / 2;
3666 if (pos >= main_body) {
3667 p.pain->line(int(p.x), y2,
3668 int(p.x + p.hfill), y2,
3669 LColor::added_space,
3670 Painter::line_onoffdash);
3673 p.pain->line(int(p.x), y2,
3674 int(p.x + p.label_hfill), y2,
3675 LColor::added_space,
3676 Painter::line_onoffdash);
3677 p.x += p.label_hfill;
3679 p.pain->line(int(p.x), y1,
3681 LColor::added_space);
3685 } else if (par->isSeparator(pos)) {
3686 p.x += singleWidth(p.bv, par, pos);
3687 if (pos >= main_body)
3698 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3699 Row * row, int y, bool cleared)
3701 if (row->height() <= 0) {
3702 lyxerr << "LYX_ERROR: row.height: "
3703 << row->height() << endl;
3709 // set up drawing parameters
3711 p.pain = &bv->painter();
3715 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3716 if (inset_owner && (p.x < 0))
3720 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3721 p.cleared = cleared;
3725 // clear to background if necessary
3726 p.cleared = paintRowBackground(p);
3728 // paint the selection background
3729 if (selection.set()) {
3730 paintRowSelection(p);
3733 // vertical lines for appendix
3734 paintRowAppendix(p);
3736 // environment depth brackets
3737 paintRowDepthBar(p);
3739 // draw any stuff wanted for a first row of a paragraph
3744 // draw any stuff wanted for the last row of a paragraph
3745 if (!row->next() || (row->next()->par() != row->par())) {
3754 int LyXText::defaultHeight() const
3756 LyXFont font(LyXFont::ALL_SANE);
3757 return int(font_metrics::maxAscent(font)
3758 + font_metrics::maxDescent(font) * 1.5);
3762 // returns the column near the specified x-coordinate of the row
3763 // x is set to the real beginning of this column
3765 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3766 bool & boundary) const
3769 float fill_separator;
3771 float fill_label_hfill;
3773 prepareToPrint(bview, row, tmpx, fill_separator,
3774 fill_hfill, fill_label_hfill);
3776 pos_type vc = row->pos();
3777 pos_type last = rowLastPrintable(row);
3779 LyXLayout const & layout =
3780 textclasslist[bview->buffer()->params.textclass][
3781 row->par()->layout()];
3782 bool left_side = false;
3784 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3785 float last_tmpx = tmpx;
3787 if (main_body > 0 &&
3788 (main_body - 1 > last ||
3789 !row->par()->isLineSeparator(main_body - 1)))
3792 while (vc <= last && tmpx <= x) {
3795 if (main_body > 0 && c == main_body-1) {
3796 tmpx += fill_label_hfill +
3797 font_metrics::width(layout.labelsep,
3798 getLabelFont(bview->buffer(), row->par()));
3799 if (row->par()->isLineSeparator(main_body - 1))
3800 tmpx -= singleWidth(bview, row->par(), main_body-1);
3803 if (hfillExpansion(bview->buffer(), row, c)) {
3804 x += singleWidth(bview, row->par(), c);
3808 tmpx += fill_label_hfill;
3810 else if (row->par()->isSeparator(c)) {
3811 tmpx += singleWidth(bview, row->par(), c);
3813 tmpx+= fill_separator;
3815 tmpx += singleWidth(bview, row->par(), c);
3819 if ((tmpx + last_tmpx) / 2 > x) {
3824 if (vc > last + 1) // This shouldn't happen.
3828 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3829 // some speedup if rtl_support=false
3830 && (!row->next() || row->next()->par() != row->par());
3831 bool const rtl = (lastrow)
3832 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3833 : false; // If lastrow is false, we don't need to compute
3834 // the value of rtl.
3836 if (row->pos() > last) // Row is empty?
3839 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3840 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3842 else if (vc == row->pos()) {
3844 if (bidi_level(c) % 2 == 1)
3847 c = vis2log(vc - 1);
3848 bool const rtl = (bidi_level(c) % 2 == 1);
3849 if (left_side == rtl) {
3851 boundary = isBoundary(bview->buffer(), row->par(), c);
3855 if (row->pos() <= last && c > last
3856 && row->par()->isNewline(last)) {
3857 if (bidi_level(last) % 2 == 0)
3858 tmpx -= singleWidth(bview, row->par(), last);
3860 tmpx += singleWidth(bview, row->par(), last);
3870 // returns pointer to a specified row
3871 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3876 Row * tmprow = firstrow;
3879 // find the first row of the specified paragraph
3880 while (tmprow->next() && tmprow->par() != par) {
3881 y += tmprow->height();
3882 tmprow = tmprow->next();
3885 // now find the wanted row
3886 while (tmprow->pos() < pos
3888 && tmprow->next()->par() == par
3889 && tmprow->next()->pos() <= pos) {
3890 y += tmprow->height();
3891 tmprow = tmprow->next();
3898 Row * LyXText::getRowNearY(int & y) const
3901 // If possible we should optimize this method. (Lgb)
3902 Row * tmprow = firstrow;
3905 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3906 tmpy += tmprow->height();
3907 tmprow = tmprow->next();
3910 y = tmpy; // return the real y
3912 //lyxerr << "returned y = " << y << endl;
3916 // Search from the current cursor position.
3918 Row * tmprow = cursor.row();
3919 int tmpy = cursor.y() - tmprow->baseline();
3921 lyxerr << "cursor.y() = " << tmpy << endl;
3922 lyxerr << "tmprow->height() = " << tmprow->height() << endl;
3923 lyxerr << "tmprow->baseline() = " << tmprow->baseline() << endl;
3924 lyxerr << "first = " << first << endl;
3925 lyxerr << "y = " << y << endl;
3928 lyxerr << "up" << endl;
3930 while (tmprow && tmpy - tmprow->height() >= y) {
3931 tmpy -= tmprow->height();
3932 tmprow = tmprow->previous();
3936 tmpy -= tmprow->height();
3937 tmprow = tmprow->previous();
3938 } while (tmprow && tmpy - tmprow->height() >= y);
3940 } else if (y > tmpy) {
3941 lyxerr << "down" << endl;
3943 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3944 tmpy += tmprow->height();
3945 tmprow = tmprow->next();
3948 lyxerr << "equal" << endl;
3951 y = tmpy; // return the real y
3953 lyxerr << "returned y = " << y << endl;
3961 int LyXText::getDepth() const
3963 return cursor.par()->getDepth();