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"
17 #include "lyx_gui_misc.h"
19 #include "bufferparams.h"
28 #include "lyxscreen.h"
29 #include "bufferview_funcs.h"
30 #include "BufferView.h"
32 #include "ParagraphParameters.h"
33 #include "undo_funcs.h"
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
53 int const LYX_PAPER_MARGIN = 20;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
60 int LyXText::workWidth(BufferView * bview) const
63 return inset_owner->textWidth(bview);
65 return bview->workWidth();
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
74 par = inset->parOwner();
76 pos = par->getPositionOfInset(inset);
78 if (!par || pos == -1) {
79 lyxerr << "LyXText::workWidth: something is wrong,"
80 " fall back to the brute force method" << endl;
81 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
82 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
83 for (; it != end; ++it) {
93 return workWidth(bview);
96 LyXLayout const & layout =
97 textclasslist.Style(bview->buffer()->params.textclass,
100 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
101 // Optimization here: in most cases, the real row is
102 // not needed, but only the par/pos values. So we just
103 // construct a dummy row for leftMargin. (JMarc)
107 return workWidth(bview) - leftMargin(bview, &dummyrow);
110 Row * row = getRow(par, pos, dummy_y);
112 while (frow->previous() && frow->par() == frow->previous()->par())
113 frow = frow->previous();
114 unsigned int maxw = 0;
115 while (frow->next() && frow->par() == frow->next()->par()) {
116 if ((frow != row) && (maxw < frow->width()))
117 maxw = frow->width();
123 return workWidth(bview);
127 int LyXText::getRealCursorX(BufferView * bview) const
130 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
131 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
136 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
139 if (!Encodings::is_arabic(c))
140 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
141 return c + (0xb0 - '0');
145 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
146 unsigned char next_char = ' ';
148 for (pos_type i = pos+1; i < par->size(); ++i)
149 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
150 next_char = par->getChar(i);
154 if (Encodings::is_arabic(next_char)) {
155 if (Encodings::is_arabic(prev_char))
156 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
158 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
160 if (Encodings::is_arabic(prev_char))
161 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
163 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
167 // This is the comments that some of the warnings below refers to.
168 // There are some issues in this file and I don't think they are
169 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
170 // this is a problem that has been here almost from day one and that a
171 // larger userbase with differenct access patters triggers the bad
172 // behaviour. (segfaults.) What I think happen is: In several places
173 // we store the paragraph in the current cursor and then moves the
174 // cursor. This movement of the cursor will delete paragraph at the
175 // old position if it is now empty. This will make the temporary
176 // pointer to the old cursor paragraph invalid and dangerous to use.
177 // And is some cases this will trigger a segfault. I have marked some
178 // of the cases where this happens with a warning, but I am sure there
179 // are others in this file and in text2.C. There is also a note in
180 // Delete() that you should read. In Delete I store the paragraph->id
181 // instead of a pointer to the paragraph. I am pretty sure this faulty
182 // use of temporary pointers to paragraphs that might have gotten
183 // invalidated (through a cursor movement) before they are used, are
184 // the cause of the strange crashes we get reported often.
186 // It is very tiresom to change this code, especially when it is as
187 // hard to read as it is. Help to fix all the cases where this is done
188 // would be greately appreciated.
192 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
195 char const c = par->getChar(pos);
196 return singleWidth(bview, par, pos, c);
200 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
201 pos_type pos, char c) const
203 LyXFont const font = getFont(bview->buffer(), par, pos);
205 // The most common case is handled first (Asger)
206 if (IsPrintable(c)) {
207 if (font.language()->RightToLeft()) {
208 if (font.language()->lang() == "arabic" &&
209 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
210 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
211 if (Encodings::IsComposeChar_arabic(c))
214 c = transformChar(c, par, pos);
215 else if (font.language()->lang() == "hebrew" &&
216 Encodings::IsComposeChar_hebrew(c))
219 return lyxfont::width(c, font);
221 } else if (IsHfillChar(c)) {
222 return 3; /* Because of the representation
223 * as vertical lines */
224 } else if (c == Paragraph::META_INSET) {
225 Inset * tmpinset = par->getInset(pos);
228 // this IS needed otherwise on initialitation we don't get the fill
229 // of the row right (ONLY on initialization if we read a file!)
230 // should be changed! (Jug 20011204)
231 tmpinset->update(bview, font);
233 return tmpinset->width(bview, font);
237 } else if (IsSeparatorChar(c))
239 else if (IsNewlineChar(c))
241 return lyxfont::width(c, font);
245 // Returns the paragraph position of the last character in the specified row
246 pos_type LyXText::rowLast(Row const * row) const
248 if (!row->next() || row->next()->par() != row->par()) {
249 return row->par()->size() - 1;
251 return row->next()->pos() - 1;
256 pos_type LyXText::rowLastPrintable(Row const * row) const
258 pos_type const last = rowLast(row);
259 if (last >= row->pos()
261 && row->next()->par() == row->par()
262 && row->par()->isSeparator(last))
269 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
271 bidi_same_direction = true;
272 if (!lyxrc.rtl_support) {
277 bidi_start = row->pos();
278 bidi_end = rowLastPrintable(row);
280 if (bidi_start > bidi_end) {
285 if (bidi_end + 2 - bidi_start >
286 static_cast<pos_type>(log2vis_list.size())) {
288 (bidi_end + 2 - bidi_start < 500) ?
289 500 : 2 * (bidi_end + 2 - bidi_start);
290 log2vis_list.resize(new_size);
291 vis2log_list.resize(new_size);
292 bidi_levels.resize(new_size);
295 vis2log_list[bidi_end + 1 - bidi_start] = -1;
296 log2vis_list[bidi_end + 1 - bidi_start] = -1;
300 row->par()->getParLanguage(buf->params)->RightToLeft();
304 pos_type const main_body = beginningOfMainBody(buf, row->par());
306 for (pos_type lpos = bidi_start;
307 lpos <= bidi_end; ++lpos) {
308 bool is_space = row->par()->isLineSeparator(lpos);
310 (is_space && lpos + 1 <= bidi_end &&
311 !row->par()->isLineSeparator(lpos + 1) &&
312 !row->par()->isNewline(lpos + 1))
314 LyXFont font = row->par()->getFontSettings(buf->params, pos);
315 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
316 font.number() == LyXFont::ON &&
317 row->par()->getFontSettings(buf->params, lpos - 1).number()
319 font = row->par()->getFontSettings(buf->params, lpos);
324 bool new_rtl = font.isVisibleRightToLeft();
325 bool new_rtl0 = font.isRightToLeft();
328 if (lpos == main_body - 1
329 && row->pos() < main_body - 1
331 new_level = (rtl_par) ? 1 : 0;
332 new_rtl = new_rtl0 = rtl_par;
334 new_level = (new_rtl) ? 1 : 2;
336 new_level = (rtl_par) ? 2 : 0;
338 if (is_space && new_level >= level) {
344 int new_level2 = new_level;
346 if (level == new_level && rtl0 != new_rtl0) {
348 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
349 } else if (level < new_level) {
350 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
351 if (new_level > rtl_par)
352 bidi_same_direction = false;
354 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
357 bidi_levels[lpos - bidi_start] = new_level;
359 while (level > new_level2) {
360 pos_type old_lpos = stack[--level];
361 int delta = lpos - old_lpos - 1;
364 log2vis_list[lpos - bidi_start] += delta;
365 log2vis_list[old_lpos - bidi_start] += delta;
367 while (level < new_level)
368 stack[level++] = lpos;
372 pos_type const old_lpos = stack[--level];
373 int delta = bidi_end - old_lpos;
376 log2vis_list[old_lpos - bidi_start] += delta;
379 pos_type vpos = bidi_start - 1;
380 for (pos_type lpos = bidi_start;
381 lpos <= bidi_end; ++lpos) {
382 vpos += log2vis_list[lpos - bidi_start];
383 vis2log_list[vpos - bidi_start] = lpos;
384 log2vis_list[lpos - bidi_start] = vpos;
389 // This method requires a previous call to ComputeBidiTables()
390 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
393 if (!lyxrc.rtl_support || pos == 0)
396 if (!bidi_InRange(pos - 1)) {
397 /// This can happen if pos is the first char of a row.
398 /// Returning false in this case is incorrect!
402 bool const rtl = bidi_level(pos - 1) % 2;
403 bool const rtl2 = bidi_InRange(pos)
404 ? bidi_level(pos) % 2
405 : par->isRightToLeftPar(buf->params);
410 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
411 pos_type pos, LyXFont const & font) const
413 if (!lyxrc.rtl_support)
414 return false; // This is just for speedup
416 bool const rtl = font.isVisibleRightToLeft();
417 bool const rtl2 = bidi_InRange(pos)
418 ? bidi_level(pos) % 2
419 : par->isRightToLeftPar(buf->params);
423 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
425 // Draw end-of-line marker
426 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
427 int const wid = lyxfont::width('n', font);
428 int const asc = lyxfont::maxAscent(font);
429 int const y = p.yo + p.row->baseline();
433 yp[0] = int(y - 0.875 * asc * 0.75);
434 yp[1] = int(y - 0.500 * asc * 0.75);
435 yp[2] = int(y - 0.125 * asc * 0.75);
437 if (bidi_level(pos) % 2 == 0) {
438 xp[0] = int(p.x + wid * 0.375);
440 xp[2] = int(p.x + wid * 0.375);
442 xp[0] = int(p.x + wid * 0.625);
443 xp[1] = int(p.x + wid);
444 xp[2] = int(p.x + wid * 0.625);
447 p.pain->lines(xp, yp, 3, LColor::eolmarker);
449 yp[0] = int(y - 0.500 * asc * 0.75);
450 yp[1] = int(y - 0.500 * asc * 0.75);
451 yp[2] = int(y - asc * 0.75);
453 if (bidi_level(pos) % 2 == 0) {
455 xp[1] = int(p.x + wid);
456 xp[2] = int(p.x + wid);
458 xp[0] = int(p.x + wid);
463 p.pain->lines(xp, yp, 3, LColor::eolmarker);
469 void LyXText::drawInset(DrawRowParams & p, pos_type const pos)
471 Inset * inset = p.row->par()->getInset(pos);
473 // FIXME: shouldn't happen
478 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
480 inset->update(p.bv, font, false);
481 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
483 if (!need_break_row && !inset_owner
484 && p.bv->text->status() == CHANGED_IN_DRAW) {
485 Row * prev = p.row->previous();
486 if (prev && prev->par() == p.row->par()) {
487 breakAgainOneRow(p.bv, prev);
488 if (prev->next() != p.row) {
489 // breakAgainOneRow() has removed p.row
490 p.row = 0; // see what this breaks
491 need_break_row = prev;
493 need_break_row = p.row;
496 need_break_row = p.row;
498 setCursor(p.bv, cursor.par(), cursor.pos());
503 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
505 if (!lyxrc.mark_foreign_language)
507 if (orig_font.language() == latex_language)
509 if (orig_font.language() == p.bv->buffer()->params.language)
512 int const y = p.yo + p.row->height() - 1;
513 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
517 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
519 pos_type pos = vis2log(vpos);
524 char c = p.row->par()->getChar(pos);
528 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
529 int const width = lyxfont::width(c, font);
532 for (pos_type i = pos-1; i >= 0; --i) {
533 c = p.row->par()->getChar(i);
534 if (!Encodings::IsComposeChar_hebrew(c)) {
535 if (IsPrintableNonspace(c)) {
537 singleWidth(p.bv, p.row->par(), i, c);
539 dx = (c == 'ø' || c == 'ã')
541 : (width2 - width) / 2;
548 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
552 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
554 pos_type pos = vis2log(vpos);
558 char c = p.row->par()->getChar(pos);
559 c = transformChar(c, p.row->par(), pos);
563 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
564 int const width = lyxfont::width(c, font);
567 for (pos_type i = pos-1; i >= 0; --i) {
568 c = p.row->par()->getChar(i);
569 if (!Encodings::IsComposeChar_arabic(c)) {
570 if (IsPrintableNonspace(c)) {
572 singleWidth(p.bv, p.row->par(), i, c);
573 dx = (width2 - width) / 2;
579 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
583 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
584 bool hebrew, bool arabic)
586 pos_type pos = vis2log(vpos);
587 pos_type const last = rowLastPrintable(p.row);
588 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
592 str += p.row->par()->getChar(pos);
594 unsigned char c = str[0];
595 str[0] = transformChar(c, p.row->par(), pos);
599 // collect as much similar chars as we can
600 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
601 char c = p.row->par()->getChar(pos);
603 if (!IsPrintableNonspace(c))
606 if (arabic && Encodings::IsComposeChar_arabic(c))
608 if (hebrew && Encodings::IsComposeChar_hebrew(c))
611 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
618 // Draw text and set the new x position
619 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
620 p.x += lyxfont::width(str, orig_font);
624 void LyXText::draw(DrawRowParams & p, pos_type & vpos)
626 pos_type const pos = vis2log(vpos);
627 Paragraph * par = p.row->par();
629 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
631 float const orig_x = p.x;
633 char const c = par->getChar(pos);
635 if (IsNewlineChar(c)) {
639 } else if (IsInsetChar(c)) {
642 drawForeignMark(p, orig_x, orig_font);
646 // usual characters, no insets
648 // special case languages
649 bool const hebrew = (orig_font.language()->lang() == "hebrew");
651 orig_font.language()->lang() == "arabic" &&
652 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
653 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
655 // draw as many chars as we can
656 if ((!hebrew && !arabic)
657 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
658 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
659 drawChars(p, vpos, true, false);
661 drawHebrewComposeChar(p, vpos);
663 drawArabicComposeChar(p, vpos);
666 drawForeignMark(p, orig_x, orig_font);
668 #ifdef INHERIT_LANGUAGE
670 if ((font.language() == inherit_language) ||
671 (font.language() == ignore_language))
672 lyxerr << "No this shouldn't happen!\n";
678 // Returns the left beginning of the text.
679 // This information cannot be taken from the layouts-objekt, because in
680 // LaTeX the beginning of the text fits in some cases (for example sections)
681 // exactly the label-width.
682 int LyXText::leftMargin(BufferView * bview, Row const * row) const
684 LyXTextClass const & tclass =
685 textclasslist.TextClass(bview->buffer()->params.textclass);
686 LyXLayout const & layout = tclass[row->par()->getLayout()];
688 string parindent = layout.parindent;
690 int x = LYX_PAPER_MARGIN;
692 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
694 // this is the way, LyX handles the LaTeX-Environments.
695 // I have had this idea very late, so it seems to be a
696 // later added hack and this is true
697 if (!row->par()->getDepth()) {
698 if (!row->par()->getLayout()) {
699 // find the previous same level paragraph
700 if (row->par()->previous()) {
701 Paragraph * newpar = row->par()
702 ->depthHook(row->par()->getDepth());
704 tclass[newpar->getLayout()].nextnoindent)
709 // find the next level paragraph
712 row->par()->outerHook();
714 // make a corresponding row. Needed to call LeftMargin()
716 // check wether it is a sufficent paragraph
717 if (newpar && tclass[newpar->getLayout()].isEnvironment())
720 dummyrow.par(newpar);
721 dummyrow.pos(newpar->size());
722 x = leftMargin(bview, &dummyrow);
724 // this is no longer an error, because this function
725 // is used to clear impossible depths after changing
726 // a layout. Since there is always a redo,
727 // LeftMargin() is always called
728 row->par()->params().depth(0);
731 if (newpar && !row->par()->getLayout()) {
732 if (newpar->params().noindent())
735 parindent = tclass[newpar->getLayout()].parindent;
740 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
741 switch (layout.margintype) {
743 if (!layout.leftmargin.empty()) {
744 x += lyxfont::signedWidth(layout.leftmargin,
745 tclass.defaultfont());
747 if (!row->par()->getLabelstring().empty()) {
748 x += lyxfont::signedWidth(layout.labelindent,
750 x += lyxfont::width(row->par()->getLabelstring(),
752 x += lyxfont::width(layout.labelsep, labelfont);
756 x += lyxfont::signedWidth(layout.labelindent, labelfont);
757 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
758 if (!row->par()->getLabelWidthString().empty()) {
759 x += lyxfont::width(row->par()->getLabelWidthString(),
761 x += lyxfont::width(layout.labelsep, labelfont);
766 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
767 / (row->par()->getDepth() + 4);
769 case MARGIN_FIRST_DYNAMIC:
770 if (layout.labeltype == LABEL_MANUAL) {
771 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
772 x += lyxfont::signedWidth(layout.leftmargin,
775 x += lyxfont::signedWidth(layout.labelindent,
778 } else if (row->pos()
779 // Special case to fix problems with
781 || (layout.labeltype == LABEL_STATIC
782 && layout.latextype == LATEX_ENVIRONMENT
783 && ! row->par()->isFirstInSequence())) {
784 x += lyxfont::signedWidth(layout.leftmargin,
786 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
787 && layout.labeltype != LABEL_BIBLIO
788 && layout.labeltype !=
789 LABEL_CENTERED_TOP_ENVIRONMENT) {
790 x += lyxfont::signedWidth(layout.labelindent,
792 x += lyxfont::width(layout.labelsep, labelfont);
793 x += lyxfont::width(row->par()->getLabelstring(),
798 case MARGIN_RIGHT_ADDRESS_BOX:
800 // ok, a terrible hack. The left margin depends on the widest
801 // row in this paragraph. Do not care about footnotes, they
802 // are *NOT* allowed in the LaTeX realisation of this layout.
804 // find the first row of this paragraph
805 Row const * tmprow = row;
806 while (tmprow->previous()
807 && tmprow->previous()->par() == row->par())
808 tmprow = tmprow->previous();
810 int minfill = tmprow->fill();
811 while (tmprow->next() && tmprow->next()->par() == row->par()) {
812 tmprow = tmprow->next();
813 if (tmprow->fill() < minfill)
814 minfill = tmprow->fill();
817 x += lyxfont::signedWidth(layout.leftmargin,
818 tclass.defaultfont());
824 LyXAlignment align; // wrong type
826 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
827 align = layout.align;
829 align = row->par()->params().align();
831 // set the correct parindent
832 if (row->pos() == 0) {
833 if ((layout.labeltype == LABEL_NO_LABEL
834 || layout.labeltype == LABEL_TOP_ENVIRONMENT
835 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
836 || (layout.labeltype == LABEL_STATIC
837 && layout.latextype == LATEX_ENVIRONMENT
838 && ! row->par()->isFirstInSequence()))
839 && align == LYX_ALIGN_BLOCK
840 && !row->par()->params().noindent()
841 && (row->par()->layout ||
842 bview->buffer()->params.paragraph_separation ==
843 BufferParams::PARSEP_INDENT))
844 x += lyxfont::signedWidth(parindent,
845 tclass.defaultfont());
846 else if (layout.labeltype == LABEL_BIBLIO) {
847 // ale970405 Right width for bibitems
848 x += bibitemMaxWidth(bview, tclass.defaultfont());
855 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
857 LyXTextClass const & tclass =
858 textclasslist.TextClass(buf->params.textclass);
859 LyXLayout const & layout = tclass[row->par()->getLayout()];
861 int x = LYX_PAPER_MARGIN
862 + lyxfont::signedWidth(tclass.rightmargin(),
863 tclass.defaultfont());
865 // this is the way, LyX handles the LaTeX-Environments.
866 // I have had this idea very late, so it seems to be a
867 // later added hack and this is true
868 if (row->par()->getDepth()) {
869 // find the next level paragraph
871 Paragraph * newpar = row->par();
874 newpar = newpar->previous();
876 && newpar->getDepth() >= row->par()->getDepth());
878 // make a corresponding row. Needed to call LeftMargin()
880 // check wether it is a sufficent paragraph
882 && tclass[newpar->getLayout()].isEnvironment()) {
884 dummyrow.par(newpar);
886 x = rightMargin(buf, &dummyrow);
888 // this is no longer an error, because this function
889 // is used to clear impossible depths after changing
890 // a layout. Since there is always a redo,
891 // LeftMargin() is always called
892 row->par()->params().depth(0);
896 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
897 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
898 * 4 / (row->par()->getDepth() + 4);
903 int LyXText::labelEnd(BufferView * bview, Row const * row) const
905 if (textclasslist.Style(bview->buffer()->params.textclass,
906 row->par()->getLayout()).margintype
910 tmprow.pos(row->par()->size());
911 return leftMargin(bview, &tmprow); /* just the beginning
914 return 0; /* LabelEnd is only needed, if the
915 layout fills a flushleft
920 // get the next breakpoint in a given paragraph
922 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
924 Paragraph * par = row->par();
929 pos_type const pos = row->pos();
931 // position of the last possible breakpoint
932 // -1 isn't a suitable value, but a flag
933 pos_type last_separator = -1;
934 width -= rightMargin(bview->buffer(), row);
936 pos_type const main_body =
937 beginningOfMainBody(bview->buffer(), par);
938 LyXLayout const & layout =
939 textclasslist.Style(bview->buffer()->params.textclass,
943 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
944 /* special code for right address boxes, only newlines count */
945 while (i < par->size()) {
946 if (par->isNewline(i)) {
948 i = par->size() - 1; // this means break
950 } else if (par->isInset(i) && par->getInset(i)
951 && par->getInset(i)->display()) {
952 par->getInset(i)->display(false);
957 // Last position is an invariant
958 pos_type const last =
960 // this is the usual handling
961 int x = leftMargin(bview, row);
962 bool doitonetime = true;
963 while (doitonetime || ((x < width) && (i < last))) {
965 char const c = par->getChar(i);
967 if (c == Paragraph::META_INSET)
968 in = par->getInset(i);
969 if (IsNewlineChar(c)) {
971 x = width; // this means break
972 } else if (in && !in->isChar()) {
973 // check wether a Display() inset is
974 // valid here. if not, change it to
977 (layout.isCommand() ||
978 (layout.labeltype == LABEL_MANUAL
979 && i < beginningOfMainBody(bview->buffer(), par))))
981 // display istn't allowd
983 x += singleWidth(bview, par, i, c);
984 } else if (in->display() || in->needFullRow()) {
985 // So break the line here
989 if (par->isLineSeparator(i+1))
992 last_separator = last; // to avoid extra rows
994 last_separator = i - 1;
995 x = width; // this means break
997 x += singleWidth(bview, par, i, c);
998 // we have to check this separately as we could have a
999 // lineseparator and then the algorithm below would prefer
1000 // that which IS wrong! We should always break on an inset
1001 // if it's too long and not on the last separator.
1002 // Maybe the only exeption is insets used as chars but
1003 // then we would have to have a special function inside
1004 // the inset to tell us this. Till then we leave it as
1005 // it is now. (Jug 20020106)
1006 if (pos < i && x >= width && last_separator >= 0)
1007 last_separator = i - 1;
1010 if (par->isLineSeparator(i))
1012 x += singleWidth(bview, par, i, c);
1015 if (i == main_body) {
1016 x += lyxfont::width(layout.labelsep,
1017 getLabelFont(bview->buffer(), par));
1018 if (par->isLineSeparator(i - 1))
1019 x-= singleWidth(bview, par, i - 1);
1020 int left_margin = labelEnd(bview, row);
1021 if (x < left_margin)
1025 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1026 last_separator = i - 2;
1027 else if ((pos < i) && (last_separator < 0) && (x >= width))
1028 last_separator = i - 1;
1029 // end of paragraph is always a suitable separator
1030 else if (i == last && x < width)
1034 // well, if last_separator is still 0, the line isn't breakable.
1035 // don't care and cut simply at the end
1036 if (last_separator < 0) {
1040 // manual labels cannot be broken in LaTeX, do not care
1041 if (main_body && last_separator < main_body)
1042 last_separator = main_body - 1;
1044 return last_separator;
1048 // returns the minimum space a row needs on the screen in pixel
1049 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1051 if (paper_width < 0)
1055 // get the pure distance
1056 pos_type const last = rowLastPrintable(row);
1058 // special handling of the right address boxes
1059 if (textclasslist.Style(bview->buffer()->params.textclass,
1060 row->par()->getLayout()).margintype
1061 == MARGIN_RIGHT_ADDRESS_BOX)
1063 int const tmpfill = row->fill();
1064 row->fill(0); // the minfill in MarginLeft()
1065 w = leftMargin(bview, row);
1068 w = leftMargin(bview, row);
1070 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1071 row->par()->getLayout());
1072 pos_type const main_body =
1073 beginningOfMainBody(bview->buffer(), row->par());
1074 pos_type i = row->pos();
1077 if (main_body > 0 && i == main_body) {
1078 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1079 if (row->par()->isLineSeparator(i - 1))
1080 w -= singleWidth(bview, row->par(), i - 1);
1081 int left_margin = labelEnd(bview, row);
1082 if (w < left_margin)
1085 w += singleWidth(bview, row->par(), i);
1088 if (main_body > 0 && main_body > last) {
1089 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1090 if (last >= 0 && row->par()->isLineSeparator(last))
1091 w -= singleWidth(bview, row->par(), last);
1092 int const left_margin = labelEnd(bview, row);
1093 if (w < left_margin)
1097 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1102 // returns the minimum space a manual label needs on the screen in pixel
1103 int LyXText::labelFill(BufferView * bview, Row const * row) const
1105 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1106 // -1 because a label ends either with a space that is in the label,
1107 // or with the beginning of a footnote that is outside the label.
1109 // I don't understand this code in depth, but sometimes "last" is
1110 // less than 0 and this causes a crash. This fix seems to work
1111 // correctly, but I bet the real error is elsewhere. The bug is
1112 // triggered when you have an open footnote in a paragraph
1113 // environment with a manual label. (Asger)
1114 if (last < 0) last = 0;
1116 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1121 pos_type i = row->pos();
1123 w += singleWidth(bview, row->par(), i);
1128 if (!row->par()->params().labelWidthString().empty()) {
1129 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1130 getLabelFont(bview->buffer(), row->par())) - w,
1138 // returns the number of separators in the specified row. The separator
1139 // on the very last column doesnt count
1140 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1142 pos_type const last = rowLast(row);
1143 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1145 for (; p < last; ++p) {
1146 if (row->par()->isSeparator(p)) {
1154 // returns the number of hfills in the specified row. The LyX-Hfill is
1155 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1156 // ignored. This is *MUCH* more usefull than not to ignore!
1157 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1159 pos_type const last = rowLast(row);
1160 pos_type first = row->pos();
1162 if (first) { /* hfill *DO* count at the beginning
1164 while (first <= last && row->par()->isHfill(first)) {
1169 first = max(first, beginningOfMainBody(buf, row->par()));
1171 for (pos_type p = first; p <= last; ++p) {
1172 // last, because the end is ignored!
1174 if (row->par()->isHfill(p)) {
1182 // like NumberOfHfills, but only those in the manual label!
1183 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1185 pos_type last = rowLast(row);
1186 pos_type first = row->pos();
1187 if (first) { /* hfill *DO* count at the beginning
1189 while (first < last && row->par()->isHfill(first))
1193 last = min(last, beginningOfMainBody(buf, row->par()));
1195 for (pos_type p = first; p < last; ++p) {
1196 // last, because the end is ignored!
1197 if (row->par()->isHfill(p)) {
1205 // returns true, if a expansion is needed.
1206 // Rules are given by LaTeX
1207 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1210 // by the way, is it a hfill?
1211 if (!row_ptr->par()->isHfill(pos))
1214 // at the end of a row it does not count
1215 // unless another hfill exists on the line
1216 if (pos >= rowLast(row_ptr)) {
1217 pos_type i = row_ptr->pos();
1218 while (i < pos && !row_ptr->par()->isHfill(i)) {
1226 // at the beginning of a row it does not count, if it is not
1227 // the first row of a paragaph
1228 if (!row_ptr->pos())
1231 // in some labels it does not count
1232 if (textclasslist.Style(buf->params.textclass,
1233 row_ptr->par()->getLayout()).margintype
1235 && pos < beginningOfMainBody(buf, row_ptr->par()))
1238 // if there is anything between the first char of the row and
1239 // the sepcified position that is not a newline and not a hfill,
1240 // the hfill will count, otherwise not
1241 pos_type i = row_ptr->pos();
1242 while (i < pos && (row_ptr->par()->isNewline(i)
1243 || row_ptr->par()->isHfill(i)))
1250 LColor::color LyXText::backgroundColor()
1253 return inset_owner->backgroundColor();
1255 return LColor::background;
1258 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1260 /* get the maximum ascent and the maximum descent */
1263 float layoutasc = 0;
1264 float layoutdesc = 0;
1267 Inset * tmpinset = 0;
1269 /* ok , let us initialize the maxasc and maxdesc value.
1270 * This depends in LaTeX of the font of the last character
1271 * in the paragraph. The hack below is necessary because
1272 * of the possibility of open footnotes */
1274 /* Correction: only the fontsize count. The other properties
1275 are taken from the layoutfont. Nicer on the screen :) */
1276 Paragraph * par = row_ptr->par();
1277 Paragraph * firstpar = row_ptr->par();
1279 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1280 firstpar->getLayout());
1282 // as max get the first character of this row then it can increes but not
1283 // decrees the height. Just some point to start with so we don't have to
1284 // do the assignment below too often.
1285 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1286 LyXFont::FONT_SIZE const tmpsize = font.size();
1287 font = getLayoutFont(bview->buffer(), par);
1288 LyXFont::FONT_SIZE const size = font.size();
1289 font.setSize(tmpsize);
1291 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1293 float spacing_val = 1.0;
1294 if (!row_ptr->par()->params().spacing().isDefault()) {
1295 spacing_val = row_ptr->par()->params().spacing().getValue();
1297 spacing_val = bview->buffer()->params.spacing.getValue();
1299 //lyxerr << "spacing_val = " << spacing_val << endl;
1301 int maxasc = int(lyxfont::maxAscent(font) *
1302 layout.spacing.getValue() *
1304 int maxdesc = int(lyxfont::maxDescent(font) *
1305 layout.spacing.getValue() *
1307 pos_type const pos_end = rowLast(row_ptr);
1311 // Check if any insets are larger
1312 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1313 if (row_ptr->par()->isInset(pos)) {
1314 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1315 tmpinset = row_ptr->par()->getInset(pos);
1317 #if 1 // this is needed for deep update on initialitation
1318 tmpinset->update(bview, tmpfont);
1320 asc = tmpinset->ascent(bview, tmpfont);
1321 desc = tmpinset->descent(bview, tmpfont);
1322 maxwidth += tmpinset->width(bview, tmpfont);
1323 maxasc = max(maxasc, asc);
1324 maxdesc = max(maxdesc, desc);
1327 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1331 // Check if any custom fonts are larger (Asger)
1332 // This is not completely correct, but we can live with the small,
1333 // cosmetic error for now.
1334 LyXFont::FONT_SIZE maxsize =
1335 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1336 if (maxsize > font.size()) {
1337 font.setSize(maxsize);
1339 asc = lyxfont::maxAscent(font);
1340 desc = lyxfont::maxDescent(font);
1347 // This is nicer with box insets:
1351 row_ptr->ascent_of_text(maxasc);
1353 // is it a top line?
1354 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1356 // some parksips VERY EASY IMPLEMENTATION
1357 if (bview->buffer()->params.paragraph_separation ==
1358 BufferParams::PARSEP_SKIP)
1360 if (layout.isParagraph()
1361 && firstpar->getDepth() == 0
1362 && firstpar->previous())
1364 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1365 } else if (firstpar->previous() &&
1366 textclasslist.Style(bview->buffer()->params.textclass,
1367 firstpar->previous()->
1368 getLayout()).isParagraph() &&
1369 firstpar->previous()->getDepth() == 0)
1371 // is it right to use defskip here too? (AS)
1372 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1376 // the paper margins
1377 if (!row_ptr->par()->previous() && bv_owner)
1378 maxasc += LYX_PAPER_MARGIN;
1380 // add the vertical spaces, that the user added
1381 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1383 // do not forget the DTP-lines!
1384 // there height depends on the font of the nearest character
1385 if (firstpar->params().lineTop())
1386 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1389 // and now the pagebreaks
1390 if (firstpar->params().pagebreakTop())
1391 maxasc += 3 * defaultHeight();
1393 // This is special code for the chapter, since the label of this
1394 // layout is printed in an extra row
1395 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1396 && bview->buffer()->params.secnumdepth >= 0)
1398 float spacing_val = 1.0;
1399 if (!row_ptr->par()->params().spacing().isDefault()) {
1400 spacing_val = row_ptr->par()->params().spacing().getValue();
1402 spacing_val = bview->buffer()->params.spacing.getValue();
1405 labeladdon = int(lyxfont::maxDescent(labelfont) *
1406 layout.spacing.getValue() *
1408 + int(lyxfont::maxAscent(labelfont) *
1409 layout.spacing.getValue() *
1413 // special code for the top label
1414 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1415 || layout.labeltype == LABEL_BIBLIO
1416 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1417 && row_ptr->par()->isFirstInSequence()
1418 && !row_ptr->par()->getLabelstring().empty())
1420 float spacing_val = 1.0;
1421 if (!row_ptr->par()->params().spacing().isDefault()) {
1422 spacing_val = row_ptr->par()->params().spacing().getValue();
1424 spacing_val = bview->buffer()->params.spacing.getValue();
1428 (lyxfont::maxAscent(labelfont) *
1429 layout.spacing.getValue() *
1431 +(lyxfont::maxDescent(labelfont) *
1432 layout.spacing.getValue() *
1434 + layout.topsep * defaultHeight()
1435 + layout.labelbottomsep * defaultHeight());
1438 // and now the layout spaces, for example before and after a section,
1439 // or between the items of a itemize or enumerate environment
1441 if (!firstpar->params().pagebreakTop()) {
1442 Paragraph * prev = row_ptr->par()->previous();
1444 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1445 if (prev && prev->getLayout() == firstpar->getLayout() &&
1446 prev->getDepth() == firstpar->getDepth() &&
1447 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1449 layoutasc = (layout.itemsep * defaultHeight());
1450 } else if (row_ptr->previous()) {
1451 tmptop = layout.topsep;
1453 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1454 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1455 row_ptr->previous()->par()->
1456 getLayout()).bottomsep;
1459 layoutasc = (tmptop * defaultHeight());
1460 } else if (row_ptr->par()->params().lineTop()) {
1461 tmptop = layout.topsep;
1464 layoutasc = (tmptop * defaultHeight());
1467 prev = row_ptr->par()->outerHook();
1469 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1470 prev->getLayout()).parsep * defaultHeight());
1472 if (firstpar->previous() &&
1473 firstpar->previous()->getDepth() == 0 &&
1474 firstpar->previous()->getLayout() !=
1475 firstpar->getLayout())
1478 } else if (firstpar->previous()) {
1479 maxasc += int(layout.parsep * defaultHeight());
1485 // is it a bottom line?
1486 if (row_ptr->par() == par
1487 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1489 // the paper margins
1490 if (!par->next() && bv_owner)
1491 maxdesc += LYX_PAPER_MARGIN;
1493 // add the vertical spaces, that the user added
1494 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1496 // do not forget the DTP-lines!
1497 // there height depends on the font of the nearest character
1498 if (firstpar->params().lineBottom())
1499 maxdesc += 2 * lyxfont::ascent('x',
1500 getFont(bview->buffer(),
1502 max(pos_type(0), par->size() - 1)));
1504 // and now the pagebreaks
1505 if (firstpar->params().pagebreakBottom())
1506 maxdesc += 3 * defaultHeight();
1508 // and now the layout spaces, for example before and after
1509 // a section, or between the items of a itemize or enumerate
1511 if (!firstpar->params().pagebreakBottom()
1512 && row_ptr->par()->next()) {
1513 Paragraph * nextpar = row_ptr->par()->next();
1514 Paragraph * comparepar = row_ptr->par();
1518 if (comparepar->getDepth() > nextpar->getDepth()) {
1519 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1520 comparepar->getLayout()).bottomsep * defaultHeight());
1521 comparepar = comparepar->depthHook(nextpar->getDepth());
1522 if (comparepar->getLayout()!= nextpar->getLayout()
1523 || nextpar->getLabelWidthString() !=
1524 comparepar->getLabelWidthString())
1526 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1527 comparepar->getLayout()).bottomsep * defaultHeight());
1529 if (unusual > usual)
1530 layoutdesc = unusual;
1533 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1535 if (comparepar->getLayout()!= nextpar->getLayout()
1536 || nextpar->getLabelWidthString() !=
1537 comparepar->getLabelWidthString())
1538 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1539 comparepar->getLayout()).bottomsep * defaultHeight());
1544 // incalculate the layout spaces
1545 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1546 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1548 // calculate the new height of the text
1549 height -= row_ptr->height();
1551 row_ptr->height(maxasc + maxdesc + labeladdon);
1552 row_ptr->baseline(maxasc + labeladdon);
1554 height += row_ptr->height();
1556 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1559 row_ptr->width(maxwidth);
1560 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1562 row_ptr->width(int(maxwidth + x));
1565 width = max(0,workWidth(bview));
1567 if (r->width() > width)
1575 /* Appends the implicit specified paragraph behind the specified row,
1576 * start at the implicit given position */
1577 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1579 bool not_ready = true;
1581 // The last character position of a paragraph is an invariant so we can
1582 // safely get it here. (Asger)
1583 pos_type const lastposition = row->par()->size();
1585 // Get the next breakpoint
1586 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1590 // Insert the new row
1591 if (z < lastposition) {
1593 insertRow(row, row->par(), z);
1600 // Set the dimensions of the row
1601 // fixed fill setting now by calling inset->update() in
1602 // SingleWidth when needed!
1603 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1604 setHeightOfRow(bview, tmprow);
1606 } while (not_ready);
1610 void LyXText::breakAgain(BufferView * bview, Row * row) const
1612 bool not_ready = true;
1615 // get the next breakpoint
1616 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1619 if (z < row->par()->size()) {
1620 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1623 insertRow(row, row->par(), z);
1629 if (row->pos() == z)
1630 not_ready = false; // the rest will not change
1636 /* if there are some rows too much, delete them */
1637 /* only if you broke the whole paragraph! */
1638 Row * tmprow2 = row;
1639 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1640 tmprow2 = tmprow2->next();
1642 while (tmprow2 != row) {
1643 tmprow2 = tmprow2->previous();
1644 removeRow(tmprow2->next());
1649 /* set the dimensions of the row */
1650 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1651 setHeightOfRow(bview, tmprow);
1652 } while (not_ready);
1656 // this is just a little changed version of break again
1657 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1659 // get the next breakpoint
1660 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1663 if (z < row->par()->size()) {
1665 || (row->next() && row->next()->par() != row->par())) {
1666 /* insert a new row */
1668 insertRow(row, row->par(), z);
1674 if (row->pos() != z)
1678 // if there are some rows too much, delete them
1679 // only if you broke the whole paragraph!
1680 Row * tmprow2 = row;
1681 while (tmprow2->next()
1682 && tmprow2->next()->par() == row->par()) {
1683 tmprow2 = tmprow2->next();
1685 while (tmprow2 != row) {
1686 tmprow2 = tmprow2->previous();
1687 removeRow(tmprow2->next());
1691 // set the dimensions of the row
1692 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1693 setHeightOfRow(bview, tmprow);
1697 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1699 LyXLayout const & layout =
1700 textclasslist.Style(bview->buffer()->params.textclass,
1701 cursor.par()->getLayout());
1703 // this is only allowed, if the current paragraph is not empty or caption
1704 if ((cursor.par()->size() <= 0)
1705 && layout.labeltype!= LABEL_SENSITIVE)
1708 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1710 // Always break behind a space
1712 // It is better to erase the space (Dekel)
1713 if (cursor.pos() < cursor.par()->size()
1714 && cursor.par()->isLineSeparator(cursor.pos()))
1715 cursor.par()->erase(cursor.pos());
1716 // cursor.pos(cursor.pos() + 1);
1718 // break the paragraph
1722 keep_layout = layout.isEnvironment();
1723 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1726 // well this is the caption hack since one caption is really enough
1727 if (layout.labeltype == LABEL_SENSITIVE) {
1729 // set to standard-layout
1730 cursor.par()->setLayout(0);
1732 // set to standard-layout
1733 cursor.par()->next()->setLayout(0);
1736 /* if the cursor is at the beginning of a row without prior newline,
1738 * This touches only the screen-update. Otherwise we would may have
1739 * an empty row on the screen */
1740 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1741 && cursor.row()->pos() == cursor.pos()) {
1745 status(bview, LyXText::NEED_MORE_REFRESH);
1746 refresh_row = cursor.row();
1747 refresh_y = cursor.y() - cursor.row()->baseline();
1749 // Do not forget the special right address boxes
1750 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1751 while (refresh_row->previous() &&
1752 refresh_row->previous()->par() == refresh_row->par()) {
1753 refresh_row = refresh_row->previous();
1754 refresh_y -= refresh_row->height();
1757 removeParagraph(cursor.row());
1759 // set the dimensions of the cursor row
1760 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1762 setHeightOfRow(bview, cursor.row());
1764 while (cursor.par()->next()->size()
1765 && cursor.par()->next()->isNewline(0))
1766 cursor.par()->next()->erase(0);
1768 insertParagraph(bview, cursor.par()->next(), cursor.row());
1770 updateCounters(bview, cursor.row()->previous());
1772 /* This check is necessary. Otherwise the new empty paragraph will
1773 * be deleted automatically. And it is more friendly for the user! */
1775 setCursor(bview, cursor.par()->next(), 0);
1777 setCursor(bview, cursor.par(), 0);
1779 if (cursor.row()->next())
1780 breakAgain(bview, cursor.row()->next());
1786 // Just a macro to make some thing easier.
1787 void LyXText::redoParagraph(BufferView * bview) const
1790 redoParagraphs(bview, cursor, cursor.par()->next());
1791 setCursorIntern(bview, cursor.par(), cursor.pos());
1795 /* insert a character, moves all the following breaks in the
1796 * same Paragraph one to the right and make a rebreak */
1797 void LyXText::insertChar(BufferView * bview, char c)
1799 setUndo(bview, Undo::INSERT,
1800 cursor.par(), cursor.par()->next());
1802 // When the free-spacing option is set for the current layout,
1803 // disable the double-space checking
1805 bool const freeSpacing =
1806 textclasslist.Style(bview->buffer()->params.textclass,
1807 cursor.row()->par()->getLayout()).free_spacing ||
1808 cursor.row()->par()->isFreeSpacing();
1811 if (lyxrc.auto_number) {
1812 static string const number_operators = "+-/*";
1813 static string const number_unary_operators = "+-";
1814 static string const number_seperators = ".,:";
1816 if (current_font.number() == LyXFont::ON) {
1817 if (!IsDigit(c) && !contains(number_operators, c) &&
1818 !(contains(number_seperators, c) &&
1819 cursor.pos() >= 1 &&
1820 cursor.pos() < cursor.par()->size() &&
1821 getFont(bview->buffer(),
1823 cursor.pos()).number() == LyXFont::ON &&
1824 getFont(bview->buffer(),
1826 cursor.pos() - 1).number() == LyXFont::ON)
1828 number(bview); // Set current_font.number to OFF
1829 } else if (IsDigit(c) &&
1830 real_current_font.isVisibleRightToLeft()) {
1831 number(bview); // Set current_font.number to ON
1833 if (cursor.pos() > 0) {
1834 char const c = cursor.par()->getChar(cursor.pos() - 1);
1835 if (contains(number_unary_operators, c) &&
1836 (cursor.pos() == 1 ||
1837 cursor.par()->isSeparator(cursor.pos() - 2) ||
1838 cursor.par()->isNewline(cursor.pos() - 2))
1840 setCharFont(bview->buffer(),
1844 } else if (contains(number_seperators, c) &&
1845 cursor.pos() >= 2 &&
1846 getFont(bview->buffer(),
1848 cursor.pos() - 2).number() == LyXFont::ON) {
1849 setCharFont(bview->buffer(),
1859 /* First check, if there will be two blanks together or a blank at
1860 the beginning of a paragraph.
1861 I decided to handle blanks like normal characters, the main
1862 difference are the special checks when calculating the row.fill
1863 (blank does not count at the end of a row) and the check here */
1865 // The bug is triggered when we type in a description environment:
1866 // The current_font is not changed when we go from label to main text
1867 // and it should (along with realtmpfont) when we type the space.
1868 // CHECK There is a bug here! (Asger)
1870 LyXFont realtmpfont = real_current_font;
1871 LyXFont rawtmpfont = current_font; /* store the current font.
1872 * This is because of the use
1873 * of cursor movements. The moving
1874 * cursor would refresh the
1877 // Get the font that is used to calculate the baselineskip
1878 pos_type const lastpos = cursor.par()->size();
1879 LyXFont rawparfont =
1880 cursor.par()->getFontSettings(bview->buffer()->params,
1883 bool jumped_over_space = false;
1885 if (!freeSpacing && IsLineSeparatorChar(c)) {
1886 if ((cursor.pos() > 0
1887 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1888 || (cursor.pos() > 0
1889 && cursor.par()->isNewline(cursor.pos() - 1))
1890 || (cursor.pos() == 0)) {
1891 static bool sent_space_message = false;
1892 if (!sent_space_message) {
1893 if (cursor.pos() == 0)
1894 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1896 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1897 sent_space_message = true;
1902 } else if (IsNewlineChar(c)) {
1903 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1908 /* No newline at first position
1909 * of a paragraph or behind labels.
1910 * TeX does not allow that. */
1912 if (cursor.pos() < cursor.par()->size() &&
1913 cursor.par()->isLineSeparator(cursor.pos()))
1914 // newline always after a blank!
1916 cursor.row()->fill(-1); // to force a new break
1919 // the display inset stuff
1920 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1921 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1922 if (inset && (inset->display() || inset->needFullRow())) {
1923 // force a new break
1924 cursor.row()->fill(-1); // to force a new break
1928 // get the cursor row fist
1929 Row * row = cursor.row();
1930 int y = cursor.y() - row->baseline();
1931 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1932 * already insertet the character */
1933 cursor.par()->insertChar(cursor.pos(), c);
1934 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1936 if (!jumped_over_space) {
1937 // refresh the positions
1939 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1940 tmprow = tmprow->next();
1941 tmprow->pos(tmprow->pos() + 1);
1945 // Is there a break one row above
1946 if ((cursor.par()->isLineSeparator(cursor.pos())
1947 || cursor.par()->isNewline(cursor.pos())
1948 || ((cursor.pos() < cursor.par()->size()) &&
1949 cursor.par()->isInset(cursor.pos()+1))
1950 || cursor.row()->fill() == -1)
1951 && row->previous() && row->previous()->par() == row->par()) {
1952 pos_type z = nextBreakPoint(bview,
1955 if (z >= row->pos()) {
1958 // set the dimensions of the row above
1959 row->previous()->fill(fill(bview,
1963 setHeightOfRow(bview, row->previous());
1965 y -= row->previous()->height();
1967 refresh_row = row->previous();
1968 status(bview, LyXText::NEED_MORE_REFRESH);
1970 breakAgainOneRow(bview, row);
1972 current_font = rawtmpfont;
1973 real_current_font = realtmpfont;
1974 setCursor(bview, cursor.par(), cursor.pos() + 1,
1975 false, cursor.boundary());
1976 // cursor MUST be in row now.
1978 if (row->next() && row->next()->par() == row->par())
1979 need_break_row = row->next();
1983 // check, wether the last characters font has changed.
1984 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1985 && rawparfont != rawtmpfont)
1986 redoHeightOfParagraph(bview, cursor);
1993 // recalculate the fill of the row
1994 if (row->fill() >= 0) /* needed because a newline
1995 * will set fill to -1. Otherwise
1996 * we would not get a rebreak! */
1997 row->fill(fill(bview, row, workWidth(bview)));
1998 if (row->fill() < 0) {
2001 refresh_x = cursor.x();
2002 refresh_pos = cursor.pos();
2003 status(bview, LyXText::NEED_MORE_REFRESH);
2004 breakAgainOneRow(bview, row);
2005 // will the cursor be in another row now?
2006 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2007 if (row->next() && row->next()->par() == row->par())
2008 // this should always be true
2010 breakAgainOneRow(bview, row);
2012 current_font = rawtmpfont;
2013 real_current_font = realtmpfont;
2015 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2017 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2018 != cursor.boundary())
2019 setCursor(bview, cursor.par(), cursor.pos(), false,
2020 !cursor.boundary());
2021 if (row->next() && row->next()->par() == row->par())
2022 need_break_row = row->next();
2027 refresh_x = cursor.x();
2029 refresh_pos = cursor.pos();
2031 int const tmpheight = row->height();
2032 setHeightOfRow(bview, row);
2033 if (tmpheight == row->height())
2034 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2036 status(bview, LyXText::NEED_MORE_REFRESH);
2038 current_font = rawtmpfont;
2039 real_current_font = realtmpfont;
2040 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2044 // check, wether the last characters font has changed.
2045 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2046 && rawparfont != rawtmpfont) {
2047 redoHeightOfParagraph(bview, cursor);
2049 // now the special right address boxes
2050 if (textclasslist.Style(bview->buffer()->params.textclass,
2051 cursor.par()->getLayout()).margintype
2052 == MARGIN_RIGHT_ADDRESS_BOX) {
2053 redoDrawingOfParagraph(bview, cursor);
2061 void LyXText::charInserted()
2063 // Here we could call FinishUndo for every 20 characters inserted.
2064 // This is from my experience how emacs does it.
2065 static unsigned int counter;
2075 void LyXText::prepareToPrint(BufferView * bview,
2076 Row * row, float & x,
2077 float & fill_separator,
2079 float & fill_label_hfill,
2085 float w = row->fill();
2087 fill_label_hfill = 0;
2089 fill_label_hfill = 0;
2092 row->par()->isRightToLeftPar(bview->buffer()->params);
2094 x = (workWidth(bview) > 0)
2095 ? rightMargin(bview->buffer(), row) : 0;
2097 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2099 // is there a manual margin with a manual label
2100 if (textclasslist.Style(bview->buffer()->params.textclass,
2101 row->par()->getLayout()).margintype == MARGIN_MANUAL
2102 && textclasslist.Style(bview->buffer()->params.textclass,
2103 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2105 /* one more since labels are left aligned */
2106 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2107 if (nlh && !row->par()->getLabelWidthString().empty()) {
2108 fill_label_hfill = labelFill(bview, row) / nlh;
2112 // are there any hfills in the row?
2113 float const nh = numberOfHfills(bview->buffer(), row);
2117 fill_hfill = w / nh;
2118 // we don't have to look at the alignment if it is ALIGN_LEFT and
2119 // if the row is already larger then the permitted width as then
2120 // we force the LEFT_ALIGN'edness!
2121 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2122 // is it block, flushleft or flushright?
2123 // set x how you need it
2125 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2126 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2128 align = row->par()->params().align();
2131 // center displayed insets
2133 if (row->par()->isInset(row->pos())
2134 && (inset=row->par()->getInset(row->pos()))
2135 && (inset->display())) // || (inset->scroll() < 0)))
2136 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2137 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2140 case LYX_ALIGN_BLOCK:
2141 ns = numberOfSeparators(bview->buffer(), row);
2142 if (ns && row->next() && row->next()->par() == row->par() &&
2143 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2144 && !(row->next()->par()->isInset(row->next()->pos())
2145 && row->next()->par()->getInset(row->next()->pos())
2146 && row->next()->par()->getInset(row->next()->pos())->display())
2149 fill_separator = w / ns;
2150 } else if (is_rtl) {
2154 case LYX_ALIGN_RIGHT:
2157 case LYX_ALIGN_CENTER:
2165 computeBidiTables(bview->buffer(), row);
2167 pos_type main_body =
2168 beginningOfMainBody(bview->buffer(), row->par());
2169 pos_type last = rowLast(row);
2171 if (main_body > 0 &&
2172 (main_body-1 > last ||
2173 !row->par()->isLineSeparator(main_body-1))) {
2174 LyXLayout const & layout =
2175 textclasslist.Style(bview->buffer()->params.textclass,
2176 row->par()->getLayout());
2177 x += lyxfont::width(layout.labelsep,
2178 getLabelFont(bview->buffer(), row->par()));
2179 if (main_body-1 <= last)
2180 x += fill_label_hfill;
2185 /* important for the screen */
2188 /* the cursor set functions have a special mechanism. When they
2189 * realize, that you left an empty paragraph, they will delete it.
2190 * They also delete the corresponding row */
2192 void LyXText::cursorRightOneWord(BufferView * bview) const
2194 // treat floats, HFills and Insets as words
2195 LyXCursor tmpcursor = cursor;
2196 // CHECK See comment on top of text.C
2198 if (tmpcursor.pos() == tmpcursor.par()->size()
2199 && tmpcursor.par()->next()) {
2200 tmpcursor.par(tmpcursor.par()->next());
2205 // Skip through initial nonword stuff.
2206 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2207 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2208 // printf("Current pos1 %d", tmpcursor.pos()) ;
2209 tmpcursor.pos(tmpcursor.pos() + 1);
2212 // Advance through word.
2213 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2214 tmpcursor.par()->isWord(tmpcursor.pos())) {
2215 // printf("Current pos2 %d", tmpcursor.pos()) ;
2216 tmpcursor.pos(tmpcursor.pos() + 1);
2220 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2224 void LyXText::cursorTab(BufferView * bview) const
2226 LyXCursor tmpcursor = cursor;
2227 while (tmpcursor.pos() < tmpcursor.par()->size()
2228 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2229 tmpcursor.pos(tmpcursor.pos() + 1);
2231 if (tmpcursor.pos() == tmpcursor.par()->size()) {
2232 if (tmpcursor.par()->next()) {
2233 tmpcursor.par(tmpcursor.par()->next());
2237 tmpcursor.pos(tmpcursor.pos() + 1);
2238 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2242 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2243 of prior word, not to end of next prior word. */
2245 void LyXText::cursorLeftOneWord(BufferView * bview) const
2247 LyXCursor tmpcursor = cursor;
2248 cursorLeftOneWord(tmpcursor);
2249 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2252 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2254 // treat HFills, floats and Insets as words
2257 && (cur.par()->isSeparator(cur.pos() - 1)
2258 || cur.par()->isKomma(cur.pos() - 1))
2259 && !(cur.par()->isHfill(cur.pos() - 1)
2260 || cur.par()->isInset(cur.pos() - 1)))
2261 cur.pos(cur.pos() - 1);
2264 && (cur.par()->isInset(cur.pos() - 1)
2265 || cur.par()->isHfill(cur.pos() - 1))) {
2266 cur.pos(cur.pos() - 1);
2267 } else if (!cur.pos()) {
2268 if (cur.par()->previous()) {
2269 cur.par(cur.par()->previous());
2270 cur.pos(cur.par()->size());
2272 } else { // Here, cur != 0
2273 while (cur.pos() > 0 &&
2274 cur.par()->isWord(cur.pos()-1))
2275 cur.pos(cur.pos() - 1);
2279 /* -------> Select current word. This depends on behaviour of
2280 CursorLeftOneWord(), so it is patched as well. */
2281 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2282 word_location const loc) const
2284 // first put the cursor where we wana start to select the word
2287 case WHOLE_WORD_STRICT:
2288 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2289 || cursor.par()->isSeparator(cursor.pos())
2290 || cursor.par()->isKomma(cursor.pos())
2291 || cursor.par()->isSeparator(cursor.pos() -1)
2292 || cursor.par()->isKomma(cursor.pos() -1)) {
2296 // no break here, we go to the next
2299 // Move cursor to the beginning, when not already there.
2300 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2301 && !from.par()->isKomma(from.pos() - 1))
2302 cursorLeftOneWord(from);
2305 // always move the cursor to the beginning of previous word
2306 cursorLeftOneWord(from);
2309 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2315 while (to.pos() < to.par()->size()
2316 && !to.par()->isSeparator(to.pos())
2317 && !to.par()->isKomma(to.pos())
2318 && !to.par()->isHfill(to.pos()))
2320 to.pos(to.pos() + 1);
2325 void LyXText::selectWord(BufferView * bview, word_location const loc)
2329 getWord(from, to, loc);
2331 setCursor(bview, from.par(), from.pos());
2334 selection.cursor = cursor;
2335 setCursor(bview, to.par(), to.pos());
2336 setSelection(bview);
2340 /* -------> Select the word currently under the cursor when no
2341 selection is currently set */
2342 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2343 word_location const loc)
2345 if (!selection.set()) {
2346 selectWord(bview, loc);
2347 return selection.set();
2353 // This function is only used by the spellchecker for NextWord().
2354 // It doesn't handle LYX_ACCENTs and probably never will.
2355 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2356 float & value) const
2358 if (the_locking_inset) {
2359 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2361 value += float(cursor.y())/float(height);
2364 // we have to go on checking so move cusor to the next char
2365 if (cursor.pos() == cursor.par()->size()) {
2366 if (!cursor.par()->next())
2368 cursor.par(cursor.par()->next());
2371 cursor.pos(cursor.pos() + 1);
2373 Paragraph * tmppar = cursor.par();
2375 // If this is not the very first word, skip rest of
2376 // current word because we are probably in the middle
2377 // of a word if there is text here.
2378 if (cursor.pos() || cursor.par()->previous()) {
2379 while (cursor.pos() < cursor.par()->size()
2380 && cursor.par()->isLetter(cursor.pos()))
2381 cursor.pos(cursor.pos() + 1);
2384 // Now, skip until we have real text (will jump paragraphs)
2385 while ((cursor.par()->size() > cursor.pos()
2386 && (!cursor.par()->isLetter(cursor.pos()))
2387 && (!cursor.par()->isInset(cursor.pos()) ||
2388 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2389 || (cursor.par()->size() == cursor.pos()
2390 && cursor.par()->next()))
2392 if (cursor.pos() == cursor.par()->size()) {
2393 cursor.par(cursor.par()->next());
2396 cursor.pos(cursor.pos() + 1);
2399 // now check if we hit an inset so it has to be a inset containing text!
2400 if (cursor.pos() < cursor.par()->size() &&
2401 cursor.par()->isInset(cursor.pos()))
2404 cursor.par()->getInset(cursor.pos())->edit(bview);
2405 // now call us again to do the above trick
2406 // but obviously we have to start from down below ;)
2407 return bview->text->selectNextWordToSpellcheck(bview, value);
2410 // Update the value if we changed paragraphs
2411 if (cursor.par() != tmppar) {
2412 setCursor(bview, cursor.par(), cursor.pos());
2413 value = float(cursor.y())/float(height);
2416 // Start the selection from here
2417 selection.cursor = cursor;
2419 // and find the end of the word (insets like optional hyphens
2420 // and ligature break are part of a word)
2421 while (cursor.pos() < cursor.par()->size()
2422 && (cursor.par()->isLetter(cursor.pos())))
2423 cursor.pos(cursor.pos() + 1);
2425 // Finally, we copy the word to a string and return it
2427 if (selection.cursor.pos() < cursor.pos()) {
2429 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2430 if (!cursor.par()->isInset(i))
2431 str += cursor.par()->getChar(i);
2438 // This one is also only for the spellchecker
2439 void LyXText::selectSelectedWord(BufferView * bview)
2441 if (the_locking_inset) {
2442 the_locking_inset->selectSelectedWord(bview);
2445 // move cursor to the beginning
2446 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2448 // set the sel cursor
2449 selection.cursor = cursor;
2451 // now find the end of the word
2452 while (cursor.pos() < cursor.par()->size()
2453 && (cursor.par()->isLetter(cursor.pos())))
2454 cursor.pos(cursor.pos() + 1);
2456 setCursor(bview, cursor.par(), cursor.pos());
2458 // finally set the selection
2459 setSelection(bview);
2463 /* -------> Delete from cursor up to the end of the current or next word. */
2464 void LyXText::deleteWordForward(BufferView * bview)
2466 if (!cursor.par()->size())
2469 LyXCursor tmpcursor = cursor;
2470 tmpcursor.row(0); //Â ??
2471 selection.set(true); // to avoid deletion
2472 cursorRightOneWord(bview);
2473 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2474 selection.cursor = cursor;
2476 setSelection(bview);
2478 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2479 cutSelection(bview, true, false);
2484 /* -------> Delete from cursor to start of current or prior word. */
2485 void LyXText::deleteWordBackward(BufferView * bview)
2487 if (!cursor.par()->size())
2490 LyXCursor tmpcursor = cursor;
2491 tmpcursor.row(0); // ??
2492 selection.set(true); // to avoid deletion
2493 cursorLeftOneWord(bview);
2494 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2495 selection.cursor = cursor;
2497 setSelection(bview);
2498 cutSelection(bview, true, false);
2503 /* -------> Kill to end of line. */
2504 void LyXText::deleteLineForward(BufferView * bview)
2506 if (!cursor.par()->size())
2507 // Paragraph is empty, so we just go to the right
2510 LyXCursor tmpcursor = cursor;
2511 // We can't store the row over a regular setCursor
2512 // so we set it to 0 and reset it afterwards.
2513 tmpcursor.row(0); //Â ??
2514 selection.set(true); // to avoid deletion
2516 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2517 selection.cursor = cursor;
2519 setSelection(bview);
2520 // What is this test for ??? (JMarc)
2521 if (!selection.set()) {
2522 deleteWordForward(bview);
2524 cutSelection(bview, true, false);
2530 // Change the case of a word at cursor position.
2531 // This function directly manipulates Paragraph::text because there
2532 // is no Paragraph::SetChar currently. I did what I could to ensure
2533 // that it is correct. I guess part of it should be moved to
2534 // Paragraph, but it will have to change for 1.1 anyway. At least
2535 // it does not access outside of the allocated array as the older
2536 // version did. (JMarc)
2537 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2542 if (selection.set()) {
2543 from = selection.start;
2546 getWord(from, to, PARTIAL_WORD);
2547 setCursor(bview, to.par(), to.pos() + 1);
2550 changeRegionCase(bview, from, to, action);
2554 void LyXText::changeRegionCase(BufferView * bview,
2555 LyXCursor const & from,
2556 LyXCursor const & to,
2557 LyXText::TextCase action)
2559 lyx::Assert(from <= to);
2561 setUndo(bview, Undo::FINISH,
2562 from.par(), to.par()->next());
2564 pos_type pos = from.pos();
2565 Paragraph * par = from.par();
2567 while (par && (pos != to.pos() || par != to.par())) {
2568 unsigned char c = par->getChar(pos);
2569 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2571 case text_lowercase:
2574 case text_capitalization:
2576 action = text_lowercase;
2578 case text_uppercase:
2583 par->setChar(pos, c);
2584 checkParagraph(bview, par, pos);
2587 if (pos == par->size()) {
2592 if (to.row() != from.row()) {
2593 refresh_y = from.y() - from.row()->baseline();
2594 refresh_row = from.row();
2595 status(bview, LyXText::NEED_MORE_REFRESH);
2600 void LyXText::transposeChars(BufferView & bview)
2602 Paragraph * tmppar = cursor.par();
2604 setUndo(&bview, Undo::FINISH,
2605 tmppar, tmppar->next());
2607 pos_type tmppos = cursor.pos();
2609 // First decide if it is possible to transpose at all
2611 // We are at the beginning of a paragraph.
2612 if (tmppos == 0) return;
2614 // We are at the end of a paragraph.
2615 if (tmppos == tmppar->size() - 1) return;
2617 unsigned char c1 = tmppar->getChar(tmppos);
2618 unsigned char c2 = tmppar->getChar(tmppos - 1);
2620 if (c1 != Paragraph::META_INSET
2621 && c2 != Paragraph::META_INSET) {
2622 tmppar->setChar(tmppos, c2);
2623 tmppar->setChar(tmppos - 1, c1);
2625 // We should have an implementation that handles insets
2626 // as well, but that will have to come later. (Lgb)
2627 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2631 void LyXText::Delete(BufferView * bview)
2633 // this is a very easy implementation
2635 LyXCursor old_cursor = cursor;
2636 int const old_cur_par_id = old_cursor.par()->id();
2637 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2638 old_cursor.par()->previous()->id() : 0;
2640 // just move to the right
2643 // CHECK Look at the comment here.
2644 // This check is not very good...
2645 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2646 // and that can very well delete the par or par->previous in
2647 // old_cursor. Will a solution where we compare paragraph id's
2649 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2650 == old_cur_par_prev_id
2651 && cursor.par()->id() != old_cur_par_id) {
2652 // delete-empty-paragraph-mechanism has done it
2656 // if you had success make a backspace
2657 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2658 LyXCursor tmpcursor = cursor;
2659 // to make sure undo gets the right cursor position
2660 cursor = old_cursor;
2661 setUndo(bview, Undo::DELETE,
2662 cursor.par(), cursor.par()->next());
2669 void LyXText::backspace(BufferView * bview)
2671 // Get the font that is used to calculate the baselineskip
2672 pos_type lastpos = cursor.par()->size();
2673 LyXFont rawparfont =
2674 cursor.par()->getFontSettings(bview->buffer()->params,
2677 if (cursor.pos() == 0) {
2678 // The cursor is at the beginning of a paragraph,
2679 // so the the backspace will collapse two paragraphs into one.
2681 // we may paste some paragraphs
2683 // is it an empty paragraph?
2686 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2687 // This is an empty paragraph and we delete it just by moving the cursor one step
2688 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2689 // of the paragraph.
2691 if (cursor.par()->previous()) {
2692 Paragraph * tmppar = cursor.par()->previous();
2693 if (cursor.par()->getLayout() == tmppar->getLayout()
2694 && cursor.par()->getAlign() == tmppar->getAlign()) {
2695 // Inherit bottom DTD from the paragraph below.
2696 // (the one we are deleting)
2697 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2698 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2699 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2704 // the layout things can change the height of a row !
2705 int const tmpheight = cursor.row()->height();
2706 setHeightOfRow(bview, cursor.row());
2707 if (cursor.row()->height() != tmpheight) {
2708 refresh_y = cursor.y() - cursor.row()->baseline();
2709 refresh_row = cursor.row();
2710 status(bview, LyXText::NEED_MORE_REFRESH);
2716 if (cursor.par()->previous()) {
2717 setUndo(bview, Undo::DELETE,
2718 cursor.par()->previous(), cursor.par()->next());
2721 Paragraph * tmppar = cursor.par();
2722 Row * tmprow = cursor.row();
2724 // We used to do cursorLeftIntern() here, but it is
2725 // not a good idea since it triggers the auto-delete
2726 // mechanism. So we do a cursorLeftIntern()-lite,
2727 // without the dreaded mechanism. (JMarc)
2728 if (cursor.par()->previous()) {
2729 // steps into the above paragraph.
2730 setCursorIntern(bview, cursor.par()->previous(),
2731 cursor.par()->previous()->size(),
2735 /* Pasting is not allowed, if the paragraphs have different
2736 layout. I think it is a real bug of all other
2737 word processors to allow it. It confuses the user.
2738 Even so with a footnote paragraph and a non-footnote
2739 paragraph. I will not allow pasting in this case,
2740 because the user would be confused if the footnote behaves
2741 different wether it is open or closed.
2743 Correction: Pasting is always allowed with standard-layout
2745 if (cursor.par() != tmppar
2746 && (cursor.par()->getLayout() == tmppar->getLayout()
2747 || tmppar->getLayout() == 0 /*standard*/)
2748 && cursor.par()->getAlign() == tmppar->getAlign())
2750 removeParagraph(tmprow);
2752 cursor.par()->pasteParagraph(bview->buffer()->params);
2754 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2755 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2756 // strangely enough it seems that commenting out the line above removes
2757 // most or all of the segfaults. I will however also try to move the
2758 // two Remove... lines in front of the PasteParagraph too.
2761 cursor.pos(cursor.pos() - 1);
2763 status(bview, LyXText::NEED_MORE_REFRESH);
2764 refresh_row = cursor.row();
2765 refresh_y = cursor.y() - cursor.row()->baseline();
2767 // remove the lost paragraph
2768 // This one is not safe, since the paragraph that the tmprow and the
2769 // following rows belong to has been deleted by the PasteParagraph
2770 // above. The question is... could this be moved in front of the
2772 //RemoveParagraph(tmprow);
2773 //RemoveRow(tmprow);
2775 // This rebuilds the rows.
2776 appendParagraph(bview, cursor.row());
2777 updateCounters(bview, cursor.row());
2779 // the row may have changed, block, hfills etc.
2780 setCursor(bview, cursor.par(), cursor.pos(), false);
2783 /* this is the code for a normal backspace, not pasting
2785 setUndo(bview, Undo::DELETE,
2786 cursor.par(), cursor.par()->next());
2787 // We used to do cursorLeftIntern() here, but it is
2788 // not a good idea since it triggers the auto-delete
2789 // mechanism. So we do a cursorLeftIntern()-lite,
2790 // without the dreaded mechanism. (JMarc)
2791 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2792 false, cursor.boundary());
2794 // some insets are undeletable here
2795 if (cursor.par()->isInset(cursor.pos())) {
2796 if (!cursor.par()->getInset(cursor.pos())->deletable())
2798 // force complete redo when erasing display insets
2799 // this is a cruel method but safe..... Matthias
2800 if (cursor.par()->getInset(cursor.pos())->display() ||
2801 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2802 cursor.par()->erase(cursor.pos());
2803 redoParagraph(bview);
2808 Row * row = cursor.row();
2809 int y = cursor.y() - row->baseline();
2811 /* remember that a space at the end of a row doesnt count
2812 * when calculating the fill */
2813 if (cursor.pos() < rowLast(row) ||
2814 !cursor.par()->isLineSeparator(cursor.pos())) {
2815 row->fill(row->fill() + singleWidth(bview,
2820 /* some special code when deleting a newline. This is similar
2821 * to the behavior when pasting paragraphs */
2822 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2823 cursor.par()->erase(cursor.pos());
2824 // refresh the positions
2826 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2827 tmprow = tmprow->next();
2828 tmprow->pos(tmprow->pos() - 1);
2830 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2831 cursor.pos(cursor.pos() - 1);
2833 if (cursor.pos() < cursor.par()->size()
2834 && !cursor.par()->isSeparator(cursor.pos())) {
2835 cursor.par()->insertChar(cursor.pos(), ' ');
2836 setCharFont(bview->buffer(), cursor.par(),
2837 cursor.pos(), current_font);
2838 // refresh the positions
2840 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2841 tmprow = tmprow->next();
2842 tmprow->pos(tmprow->pos() + 1);
2846 cursor.par()->erase(cursor.pos());
2848 // refresh the positions
2850 while (tmprow->next()
2851 && tmprow->next()->par() == row->par()) {
2852 tmprow = tmprow->next();
2853 tmprow->pos(tmprow->pos() - 1);
2856 // delete newlines at the beginning of paragraphs
2857 while (cursor.par()->size() &&
2858 cursor.par()->isNewline(cursor.pos()) &&
2859 cursor.pos() == beginningOfMainBody(bview->buffer(),
2861 cursor.par()->erase(cursor.pos());
2862 // refresh the positions
2864 while (tmprow->next() &&
2865 tmprow->next()->par() == row->par()) {
2866 tmprow = tmprow->next();
2867 tmprow->pos(tmprow->pos() - 1);
2872 // is there a break one row above
2873 if (row->previous() && row->previous()->par() == row->par()) {
2874 z = nextBreakPoint(bview, row->previous(),
2876 if (z >= row->pos()) {
2879 Row * tmprow = row->previous();
2881 // maybe the current row is now empty
2882 if (row->pos() >= row->par()->size()) {
2887 breakAgainOneRow(bview, row);
2888 if (row->next() && row->next()->par() == row->par())
2889 need_break_row = row->next();
2894 // set the dimensions of the row above
2895 y -= tmprow->height();
2896 tmprow->fill(fill(bview, tmprow,
2898 setHeightOfRow(bview, tmprow);
2901 refresh_row = tmprow;
2902 status(bview, LyXText::NEED_MORE_REFRESH);
2903 setCursor(bview, cursor.par(), cursor.pos(),
2904 false, cursor.boundary());
2905 //current_font = rawtmpfont;
2906 //real_current_font = realtmpfont;
2907 // check, whether the last character's font has changed.
2909 cursor.par()->getFontSettings(bview->buffer()->params,
2910 cursor.par()->size() - 1))
2911 redoHeightOfParagraph(bview, cursor);
2916 // break the cursor row again
2917 if (row->next() && row->next()->par() == row->par() &&
2918 (rowLast(row) == row->par()->size() - 1 ||
2919 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2921 /* it can happen that a paragraph loses one row
2922 * without a real breakup. This is when a word
2923 * is to long to be broken. Well, I don t care this
2925 if (rowLast(row) == row->par()->size() - 1)
2926 removeRow(row->next());
2930 status(bview, LyXText::NEED_MORE_REFRESH);
2932 breakAgainOneRow(bview, row);
2933 // will the cursor be in another row now?
2934 if (row->next() && row->next()->par() == row->par() &&
2935 rowLast(row) <= cursor.pos()) {
2937 breakAgainOneRow(bview, row);
2940 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2942 if (row->next() && row->next()->par() == row->par())
2943 need_break_row = row->next();
2947 // set the dimensions of the row
2948 row->fill(fill(bview, row, workWidth(bview)));
2949 int const tmpheight = row->height();
2950 setHeightOfRow(bview, row);
2951 if (tmpheight == row->height())
2952 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2954 status(bview, LyXText::NEED_MORE_REFRESH);
2957 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2961 // current_font = rawtmpfont;
2962 // real_current_font = realtmpfont;
2964 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2965 != cursor.boundary())
2966 setCursor(bview, cursor.par(), cursor.pos(), false,
2967 !cursor.boundary());
2969 lastpos = cursor.par()->size();
2970 if (cursor.pos() == lastpos)
2971 setCurrentFont(bview);
2973 // check, whether the last characters font has changed.
2975 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2976 redoHeightOfParagraph(bview, cursor);
2978 // now the special right address boxes
2979 if (textclasslist.Style(bview->buffer()->params.textclass,
2980 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2981 redoDrawingOfParagraph(bview, cursor);
2987 bool LyXText::paintRowBackground(DrawRowParams & p)
2989 bool clear_area = true;
2991 LyXFont font(LyXFont::ALL_SANE);
2993 pos_type const last = rowLastPrintable(p.row);
2995 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2996 && p.row->par()->isInset(p.row->pos())) {
2997 inset = p.row->par()->getInset(p.row->pos());
2999 clear_area = inset->doClearArea();
3009 int const y = p.yo < 0 ? 0 : p.yo;
3010 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3011 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3018 int h = p.row->baseline() - inset->ascent(p.bv, font);
3020 // first clear the whole row above the inset!
3022 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3025 // clear the space below the inset!
3026 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3027 if ((p.row->height() - h) > 0) {
3028 p.pain->fillRectangle(p.xo, p.yo + h,
3029 p.width, p.row->height() - h, backgroundColor());
3032 // clear the space behind the inset, if needed
3033 if (!inset->display() && !inset->needFullRow()) {
3034 int const xp = int(p.x) + inset->width(p.bv, font);
3035 if (p.width - xp > 0) {
3036 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3037 p.row->height(), backgroundColor());
3045 void LyXText::paintRowSelection(DrawRowParams & p)
3047 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3049 // the current selection
3050 int const startx = selection.start.x();
3051 int const endx = selection.end.x();
3052 int const starty = selection.start.y();
3053 int const endy = selection.end.y();
3054 Row const * startrow = selection.start.row();
3055 Row const * endrow = selection.end.row();
3059 if (bidi_same_direction) {
3063 int h = row->height();
3065 if (startrow == row && endrow == row) {
3066 if (startx < endx) {
3069 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3073 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3075 } else if (startrow == row) {
3076 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3077 int const w = (is_rtl) ? startx : (p.width - startx);
3078 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3079 } else if (endrow == row) {
3080 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3081 int const w = (is_rtl) ? (p.width - endx) : endx;
3082 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3083 } else if (p.y > starty && p.y < endy) {
3084 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3087 } else if (startrow != row && endrow != row) {
3089 int h = row->height();
3090 if (p.y > starty && p.y < endy) {
3091 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3096 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3102 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3104 Buffer const * buffer = p.bv->buffer();
3105 Paragraph * par = row->par();
3106 pos_type main_body = beginningOfMainBody(buffer, par);
3107 pos_type const last = rowLastPrintable(row);
3109 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3110 pos_type pos = vis2log(vpos);
3111 float const old_tmpx = tmpx;
3112 if (main_body > 0 && pos == main_body - 1) {
3113 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3115 LyXFont const lfont = getLabelFont(buffer, par);
3118 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3120 if (par->isLineSeparator(main_body - 1))
3121 tmpx -= singleWidth(p.bv, par, main_body - 1);
3124 if (hfillExpansion(buffer, row, pos)) {
3125 tmpx += singleWidth(p.bv, par, pos);
3126 if (pos >= main_body)
3129 tmpx += p.label_hfill;
3132 else if (par->isSeparator(pos)) {
3133 tmpx += singleWidth(p.bv, par, pos);
3134 if (pos >= main_body)
3135 tmpx += p.separator;
3137 tmpx += singleWidth(p.bv, par, pos);
3140 if ((startrow != row || selection.start.pos() <= pos) &&
3141 (endrow != row || pos < selection.end.pos())) {
3142 // Here we do not use p.x as p.xo was added to p.x.
3143 p.pain->fillRectangle(int(old_tmpx), p.yo,
3144 int(tmpx - old_tmpx + 1),
3145 row->height(), LColor::selection);
3148 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3149 p.pain->fillRectangle(p.xo + int(tmpx),
3150 p.yo, int(p.bv->workWidth() - tmpx),
3151 row->height(), LColor::selection);
3157 void LyXText::paintRowAppendix(DrawRowParams & p)
3159 // FIXME: can be just p.width ?
3160 int const ww = p.bv->workWidth();
3161 Paragraph * firstpar = p.row->par();
3163 if (firstpar->params().appendix()) {
3164 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3165 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3170 void LyXText::paintRowDepthBar(DrawRowParams & p)
3172 Paragraph::depth_type const depth = p.row->par()->getDepth();
3177 Paragraph::depth_type prev_depth = 0;
3178 if (p.row->previous())
3179 prev_depth = p.row->previous()->par()->getDepth();
3180 Paragraph::depth_type next_depth = 0;
3182 next_depth = p.row->next()->par()->getDepth();
3184 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3185 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3186 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3188 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3190 int const w = LYX_PAPER_MARGIN / 5;
3192 if (i > prev_depth) {
3193 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3195 if (i > next_depth) {
3196 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3202 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3204 int const arrow_size = 4;
3205 int const space_size = int(vsp.inPixels(bv));
3207 if (vsp.kind() != VSpace::LENGTH) {
3213 int const min_size = max(3 * arrow_size,
3214 lyxfont::maxAscent(font)
3215 + lyxfont::maxDescent(font));
3217 if (vsp.length().len().value() < 0.0)
3220 return max(min_size, space_size);
3224 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3225 VSpace const & vsp, int start)
3227 int const arrow_size = 4;
3228 int const size = getLengthMarkerHeight(p.bv, vsp);
3229 int const end = start + size;
3231 // the label to display (if any)
3233 // y-values for top arrow
3235 // y-values for bottom arrow
3237 switch (vsp.kind()) {
3238 case VSpace::LENGTH:
3240 str = prefix + " (" + vsp.asLyXCommand() + ")";
3241 // adding or removing space
3242 bool const added = !(vsp.length().len().value() < 0.0);
3243 ty1 = added ? (start + arrow_size) : start;
3244 ty2 = added ? start : (start + arrow_size);
3245 by1 = added ? (end - arrow_size) : end;
3246 by2 = added ? end : (end - arrow_size);
3249 case VSpace:: VFILL:
3250 str = prefix + " (vertical fill)";
3255 // nothing to draw here
3259 int const leftx = p.xo + leftMargin(p.bv, p.row);
3260 int const midx = leftx + arrow_size;
3261 int const rightx = midx + arrow_size;
3269 font.setColor(LColor::added_space).decSize();
3270 lyxfont::rectText(str, font, w, a, d);
3272 p.pain->rectText(leftx + 2 * arrow_size + 5,
3273 start + ((end - start) / 2) + d,
3279 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3280 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3283 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3284 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3287 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3293 void LyXText::paintFirstRow(DrawRowParams & p)
3295 Paragraph * par = p.row->par();
3296 ParagraphParameters const & parparams = par->params();
3298 // start of appendix?
3299 if (parparams.startOfAppendix()) {
3300 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3305 // think about the margins
3306 if (!p.row->previous() && bv_owner)
3307 y_top += LYX_PAPER_MARGIN;
3309 // draw a top pagebreak
3310 if (parparams.pagebreakTop()) {
3311 int const y = p.yo + y_top + 2*defaultHeight();
3312 p.pain->line(p.xo, y, p.xo + p.width, y,
3313 LColor::pagebreak, Painter::line_onoffdash);
3320 pb_font.setColor(LColor::pagebreak).decSize();
3321 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3322 p.pain->rectText((p.width - w)/2, y + d,
3323 _("Page Break (top)"), pb_font,
3326 y_top += 3 * defaultHeight();
3329 // draw the additional space if needed:
3330 y_top += drawLengthMarker(p, _("Space above"),
3331 parparams.spaceTop(), p.yo + y_top);
3333 Buffer const * buffer = p.bv->buffer();
3335 LyXLayout const & layout =
3336 textclasslist.Style(buffer->params.textclass, par->getLayout());
3338 // think about the parskip
3339 // some parskips VERY EASY IMPLEMENTATION
3340 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3341 if (par->previous()) {
3342 if (layout.latextype == LATEX_PARAGRAPH
3343 && !par->getDepth()) {
3344 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3346 LyXLayout const & playout =
3347 textclasslist.Style(buffer->params.textclass,
3348 par->previous()->getLayout());
3349 if (playout.latextype == LATEX_PARAGRAPH
3350 && !par->previous()->getDepth()) {
3351 // is it right to use defskip here, too? (AS)
3352 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3358 int const ww = p.bv->workWidth();
3361 if (parparams.lineTop()) {
3362 LyXFont font(LyXFont::ALL_SANE);
3363 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3367 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3368 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3369 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3370 LColor::topline, Painter::line_solid,
3371 Painter::line_thick);
3376 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3378 // should we print a label?
3379 if (layout.labeltype >= LABEL_STATIC
3380 && (layout.labeltype != LABEL_STATIC
3381 || layout.latextype != LATEX_ENVIRONMENT
3382 || par->isFirstInSequence())) {
3384 LyXFont font = getLabelFont(buffer, par);
3385 if (!par->getLabelstring().empty()) {
3387 string const str = par->getLabelstring();
3389 // this is special code for the chapter layout. This is
3390 // printed in an extra row and has a pagebreak at
3392 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3393 if (buffer->params.secnumdepth >= 0) {
3394 float spacing_val = 1.0;
3395 if (!parparams.spacing().isDefault()) {
3396 spacing_val = parparams.spacing().getValue();
3398 spacing_val = buffer->params.spacing.getValue();
3402 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3403 + int(layout.parsep) * defaultHeight();
3406 x = ww - leftMargin(p.bv, p.row) -
3407 lyxfont::width(str, font);
3410 p.pain->text(int(x),
3411 p.yo + p.row->baseline() -
3412 p.row->ascent_of_text() - maxdesc,
3417 x = ww - leftMargin(p.bv, p.row)
3418 + lyxfont::width(layout.labelsep, font);
3420 x = p.x - lyxfont::width(layout.labelsep, font)
3421 - lyxfont::width(str, font);
3424 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3427 // the labels at the top of an environment.
3428 // More or less for bibliography
3429 } else if (par->isFirstInSequence() &&
3430 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3431 layout.labeltype == LABEL_BIBLIO ||
3432 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3433 LyXFont font = getLabelFont(buffer, par);
3434 if (!par->getLabelstring().empty()) {
3435 string const str = par->getLabelstring();
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(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3445 + (layout.labelbottomsep * defaultHeight()));
3448 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3449 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3450 + ww - rightMargin(buffer, p.row)) / 2;
3451 x -= lyxfont::width(str, font) / 2;
3452 } else if (is_rtl) {
3453 x = ww - leftMargin(p.bv, p.row) -
3454 lyxfont::width(str, font);
3456 p.pain->text(int(x), p.yo + p.row->baseline()
3457 - p.row->ascent_of_text() - maxdesc,
3462 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3463 LyXFont font = getLayoutFont(buffer, par);
3466 x = ww - leftMargin(p.bv, p.row)
3467 + lyxfont::width(layout.labelsep, font);
3469 x = p.x - lyxfont::width(layout.labelsep, font)
3470 - par->bibkey->width(p.bv, font);
3472 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3477 void LyXText::paintLastRow(DrawRowParams & p)
3479 Paragraph * par = p.row->par();
3480 ParagraphParameters const & parparams = par->params();
3481 int y_bottom = p.row->height() - 1;
3483 // think about the margins
3484 if (!p.row->next() && bv_owner)
3485 y_bottom -= LYX_PAPER_MARGIN;
3487 int const ww = p.bv->workWidth();
3489 // draw a bottom pagebreak
3490 if (parparams.pagebreakBottom()) {
3492 pb_font.setColor(LColor::pagebreak).decSize();
3493 int const y = p.yo + y_bottom - 2 * defaultHeight();
3495 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3496 Painter::line_onoffdash);
3501 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3502 p.pain->rectText((ww - w) / 2, y + d,
3503 _("Page Break (bottom)"),
3504 pb_font, backgroundColor(), backgroundColor());
3506 y_bottom -= 3 * defaultHeight();
3509 // draw the additional space if needed:
3510 int const height = getLengthMarkerHeight(p.bv,
3511 parparams.spaceBottom());
3512 y_bottom -= drawLengthMarker(p, _("Space below"),
3513 parparams.spaceBottom(),
3514 p.yo + y_bottom - height);
3516 Buffer const * buffer = p.bv->buffer();
3518 // draw a bottom line
3519 if (parparams.lineBottom()) {
3520 LyXFont font(LyXFont::ALL_SANE);
3521 int const asc = lyxfont::ascent('x',
3522 getFont(buffer, par,
3523 max(pos_type(0), par->size() - 1)));
3527 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3528 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3529 int const y = p.yo + y_bottom;
3530 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3531 Painter::line_thick);
3536 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3537 int const endlabel = par->getEndLabel(buffer->params);
3542 case END_LABEL_FILLED_BOX:
3544 LyXFont const font = getLabelFont(buffer, par);
3545 int const size = int(0.75 * lyxfont::maxAscent(font));
3546 int const y = (p.yo + p.row->baseline()) - size;
3547 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3549 if (p.row->fill() <= size)
3550 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3552 if (endlabel == END_LABEL_BOX) {
3553 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3555 p.pain->fillRectangle(x, y, size, size,
3560 case END_LABEL_STATIC:
3562 LyXFont font(LyXFont::ALL_SANE);
3563 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3564 string const str = textclasslist.
3565 Style(buffer->params.textclass, layout).endlabelstring();
3566 font = getLabelFont(buffer, par);
3567 int const x = is_rtl ?
3568 int(p.x) - lyxfont::width(str, font)
3569 : ww - rightMargin(buffer, p.row) - p.row->fill();
3570 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3573 case END_LABEL_NO_LABEL:
3578 void LyXText::paintRowText(DrawRowParams & p)
3580 Paragraph * par = p.row->par();
3581 Buffer const * buffer = p.bv->buffer();
3583 pos_type const last = rowLastPrintable(p.row);
3584 pos_type main_body =
3585 beginningOfMainBody(buffer, par);
3586 if (main_body > 0 &&
3587 (main_body - 1 > last ||
3588 !par->isLineSeparator(main_body - 1))) {
3592 LyXLayout const & layout =
3593 textclasslist.Style(buffer->params.textclass, par->getLayout());
3595 pos_type vpos = p.row->pos();
3596 while (vpos <= last) {
3597 pos_type pos = vis2log(vpos);
3598 if (main_body > 0 && pos == main_body - 1) {
3599 int const lwidth = lyxfont::width(layout.labelsep,
3600 getLabelFont(buffer, par));
3602 p.x += p.label_hfill + lwidth
3603 - singleWidth(p.bv, par, main_body - 1);
3606 if (par->isHfill(pos)) {
3609 int const y0 = p.yo + p.row->baseline();
3610 int const y1 = y0 - defaultHeight() / 2;
3612 p.pain->line(int(p.x), y1, int(p.x), y0,
3613 LColor::added_space);
3615 if (hfillExpansion(buffer, p.row, pos)) {
3616 int const y2 = (y0 + y1) / 2;
3618 if (pos >= main_body) {
3619 p.pain->line(int(p.x), y2,
3620 int(p.x + p.hfill), y2,
3621 LColor::added_space,
3622 Painter::line_onoffdash);
3625 p.pain->line(int(p.x), y2,
3626 int(p.x + p.label_hfill), y2,
3627 LColor::added_space,
3628 Painter::line_onoffdash);
3629 p.x += p.label_hfill;
3631 p.pain->line(int(p.x), y1,
3633 LColor::added_space);
3637 } else if (par->isSeparator(pos)) {
3638 p.x += singleWidth(p.bv, par, pos);
3639 if (pos >= main_body)
3649 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3650 Row * row, int y, bool cleared)
3652 if (row->height() <= 0) {
3653 lyxerr << "LYX_ERROR: row.height: "
3654 << row->height() << endl;
3660 // set up drawing parameters
3662 p.pain = &bv->painter();
3666 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3667 if (inset_owner && (p.x < 0))
3671 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3672 p.cleared = cleared;
3676 // clear to background if necessary
3677 p.cleared = paintRowBackground(p);
3679 // paint the selection background
3680 if (selection.set()) {
3681 paintRowSelection(p);
3684 // vertical lines for appendix
3685 paintRowAppendix(p);
3687 // environment depth brackets
3688 paintRowDepthBar(p);
3690 // draw any stuff wanted for a first row of a paragraph
3695 // draw any stuff wanted for the last row of a paragraph
3696 if (!row->next() || (row->next()->par() != row->par())) {
3705 int LyXText::defaultHeight() const
3707 LyXFont font(LyXFont::ALL_SANE);
3708 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3712 /* returns the column near the specified x-coordinate of the row
3713 * x is set to the real beginning of this column */
3715 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3716 bool & boundary) const
3719 float fill_separator;
3721 float fill_label_hfill;
3723 prepareToPrint(bview, row, tmpx, fill_separator,
3724 fill_hfill, fill_label_hfill);
3726 pos_type vc = row->pos();
3727 pos_type last = rowLastPrintable(row);
3729 LyXLayout const & layout =
3730 textclasslist.Style(bview->buffer()->params.textclass,
3731 row->par()->getLayout());
3732 bool left_side = false;
3734 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3735 float last_tmpx = tmpx;
3737 if (main_body > 0 &&
3738 (main_body - 1 > last ||
3739 !row->par()->isLineSeparator(main_body - 1)))
3742 while (vc <= last && tmpx <= x) {
3745 if (main_body > 0 && c == main_body-1) {
3746 tmpx += fill_label_hfill +
3747 lyxfont::width(layout.labelsep,
3748 getLabelFont(bview->buffer(), row->par()));
3749 if (row->par()->isLineSeparator(main_body - 1))
3750 tmpx -= singleWidth(bview, row->par(), main_body-1);
3753 if (hfillExpansion(bview->buffer(), row, c)) {
3754 x += singleWidth(bview, row->par(), c);
3758 tmpx += fill_label_hfill;
3760 else if (row->par()->isSeparator(c)) {
3761 tmpx += singleWidth(bview, row->par(), c);
3763 tmpx+= fill_separator;
3765 tmpx += singleWidth(bview, row->par(), c);
3769 if ((tmpx + last_tmpx) / 2 > x) {
3774 if (vc > last + 1) // This shouldn't happen.
3778 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3779 // some speedup if rtl_support=false
3780 && (!row->next() || row->next()->par() != row->par());
3781 bool const rtl = (lastrow)
3782 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3783 : false; // If lastrow is false, we don't need to compute
3784 // the value of rtl.
3786 if (row->pos() > last) // Row is empty?
3789 ((rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3790 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5)))
3792 else if (vc == row->pos()) {
3794 if (bidi_level(c) % 2 == 1)
3797 c = vis2log(vc - 1);
3798 bool const rtl = (bidi_level(c) % 2 == 1);
3799 if (left_side == rtl) {
3801 boundary = isBoundary(bview->buffer(), row->par(), c);
3805 if (row->pos() <= last && c > last
3806 && row->par()->isNewline(last)) {
3807 if (bidi_level(last) % 2 == 0)
3808 tmpx -= singleWidth(bview, row->par(), last);
3810 tmpx += singleWidth(bview, row->par(), last);
3820 // returns pointer to a specified row
3821 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3826 Row * tmprow = firstrow;
3829 // find the first row of the specified paragraph
3830 while (tmprow->next() && tmprow->par() != par) {
3831 y += tmprow->height();
3832 tmprow = tmprow->next();
3835 // now find the wanted row
3836 while (tmprow->pos() < pos
3838 && tmprow->next()->par() == par
3839 && tmprow->next()->pos() <= pos) {
3840 y += tmprow->height();
3841 tmprow = tmprow->next();
3848 Row * LyXText::getRowNearY(int & y) const
3850 // If possible we should optimize this method. (Lgb)
3851 Row * tmprow = firstrow;
3854 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3855 tmpy += tmprow->height();
3856 tmprow = tmprow->next();
3859 y = tmpy; // return the real y
3864 int LyXText::getDepth() const
3866 return cursor.par()->getDepth();