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 // breakAgainOneRow() may have removed p.row
489 // What about the x and y coordinates? par & pos ok.
490 if (prev->next() != p.row) {
493 // If there's only one row (after p.row was deleted)
494 // prev->next() == 0 and no breaking is required.
495 // Otherwise, check the new next row.
496 need_break_row = prev->next();
498 need_break_row = p.row;
500 setCursor(p.bv, cursor.par(), cursor.pos());
505 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
507 if (!lyxrc.mark_foreign_language)
509 if (orig_font.language() == latex_language)
511 if (orig_font.language() == p.bv->buffer()->params.language)
514 int const y = p.yo + p.row->height() - 1;
515 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
519 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
521 pos_type pos = vis2log(vpos);
526 char c = p.row->par()->getChar(pos);
530 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
531 int const width = lyxfont::width(c, font);
534 for (pos_type i = pos-1; i >= 0; --i) {
535 c = p.row->par()->getChar(i);
536 if (!Encodings::IsComposeChar_hebrew(c)) {
537 if (IsPrintableNonspace(c)) {
539 singleWidth(p.bv, p.row->par(), i, c);
541 dx = (c == 'ø' || c == 'ã')
543 : (width2 - width) / 2;
550 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
554 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
556 pos_type pos = vis2log(vpos);
560 char c = p.row->par()->getChar(pos);
561 c = transformChar(c, p.row->par(), pos);
565 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
566 int const width = lyxfont::width(c, font);
569 for (pos_type i = pos-1; i >= 0; --i) {
570 c = p.row->par()->getChar(i);
571 if (!Encodings::IsComposeChar_arabic(c)) {
572 if (IsPrintableNonspace(c)) {
574 singleWidth(p.bv, p.row->par(), i, c);
575 dx = (width2 - width) / 2;
581 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
585 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
586 bool hebrew, bool arabic)
588 pos_type pos = vis2log(vpos);
589 pos_type const last = rowLastPrintable(p.row);
590 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
594 str += p.row->par()->getChar(pos);
596 unsigned char c = str[0];
597 str[0] = transformChar(c, p.row->par(), pos);
601 // collect as much similar chars as we can
602 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
603 char c = p.row->par()->getChar(pos);
605 if (!IsPrintableNonspace(c))
608 if (arabic && Encodings::IsComposeChar_arabic(c))
610 if (hebrew && Encodings::IsComposeChar_hebrew(c))
613 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
620 // Draw text and set the new x position
621 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
622 p.x += lyxfont::width(str, orig_font);
626 void LyXText::draw(DrawRowParams & p, pos_type & vpos)
628 pos_type const pos = vis2log(vpos);
629 Paragraph * par = p.row->par();
631 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
633 float const orig_x = p.x;
635 char const c = par->getChar(pos);
637 if (IsNewlineChar(c)) {
641 } else if (IsInsetChar(c)) {
644 drawForeignMark(p, orig_x, orig_font);
648 // usual characters, no insets
650 // special case languages
651 bool const hebrew = (orig_font.language()->lang() == "hebrew");
653 orig_font.language()->lang() == "arabic" &&
654 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
655 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
657 // draw as many chars as we can
658 if ((!hebrew && !arabic)
659 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
660 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
661 drawChars(p, vpos, true, false);
663 drawHebrewComposeChar(p, vpos);
665 drawArabicComposeChar(p, vpos);
668 drawForeignMark(p, orig_x, orig_font);
670 #ifdef INHERIT_LANGUAGE
672 if ((font.language() == inherit_language) ||
673 (font.language() == ignore_language))
674 lyxerr << "No this shouldn't happen!\n";
680 // Returns the left beginning of the text.
681 // This information cannot be taken from the layouts-objekt, because in
682 // LaTeX the beginning of the text fits in some cases (for example sections)
683 // exactly the label-width.
684 int LyXText::leftMargin(BufferView * bview, Row const * row) const
686 LyXTextClass const & tclass =
687 textclasslist.TextClass(bview->buffer()->params.textclass);
688 LyXLayout const & layout = tclass[row->par()->getLayout()];
690 string parindent = layout.parindent;
692 int x = LYX_PAPER_MARGIN;
694 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
696 // this is the way, LyX handles the LaTeX-Environments.
697 // I have had this idea very late, so it seems to be a
698 // later added hack and this is true
699 if (!row->par()->getDepth()) {
700 if (!row->par()->getLayout()) {
701 // find the previous same level paragraph
702 if (row->par()->previous()) {
703 Paragraph * newpar = row->par()
704 ->depthHook(row->par()->getDepth());
706 tclass[newpar->getLayout()].nextnoindent)
711 // find the next level paragraph
714 row->par()->outerHook();
716 // make a corresponding row. Needed to call LeftMargin()
718 // check wether it is a sufficent paragraph
719 if (newpar && tclass[newpar->getLayout()].isEnvironment())
722 dummyrow.par(newpar);
723 dummyrow.pos(newpar->size());
724 x = leftMargin(bview, &dummyrow);
726 // this is no longer an error, because this function
727 // is used to clear impossible depths after changing
728 // a layout. Since there is always a redo,
729 // LeftMargin() is always called
730 row->par()->params().depth(0);
733 if (newpar && !row->par()->getLayout()) {
734 if (newpar->params().noindent())
737 parindent = tclass[newpar->getLayout()].parindent;
742 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
743 switch (layout.margintype) {
745 if (!layout.leftmargin.empty()) {
746 x += lyxfont::signedWidth(layout.leftmargin,
747 tclass.defaultfont());
749 if (!row->par()->getLabelstring().empty()) {
750 x += lyxfont::signedWidth(layout.labelindent,
752 x += lyxfont::width(row->par()->getLabelstring(),
754 x += lyxfont::width(layout.labelsep, labelfont);
758 x += lyxfont::signedWidth(layout.labelindent, labelfont);
759 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
760 if (!row->par()->getLabelWidthString().empty()) {
761 x += lyxfont::width(row->par()->getLabelWidthString(),
763 x += lyxfont::width(layout.labelsep, labelfont);
768 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
769 / (row->par()->getDepth() + 4);
771 case MARGIN_FIRST_DYNAMIC:
772 if (layout.labeltype == LABEL_MANUAL) {
773 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
774 x += lyxfont::signedWidth(layout.leftmargin,
777 x += lyxfont::signedWidth(layout.labelindent,
780 } else if (row->pos()
781 // Special case to fix problems with
783 || (layout.labeltype == LABEL_STATIC
784 && layout.latextype == LATEX_ENVIRONMENT
785 && ! row->par()->isFirstInSequence())) {
786 x += lyxfont::signedWidth(layout.leftmargin,
788 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
789 && layout.labeltype != LABEL_BIBLIO
790 && layout.labeltype !=
791 LABEL_CENTERED_TOP_ENVIRONMENT) {
792 x += lyxfont::signedWidth(layout.labelindent,
794 x += lyxfont::width(layout.labelsep, labelfont);
795 x += lyxfont::width(row->par()->getLabelstring(),
800 case MARGIN_RIGHT_ADDRESS_BOX:
802 // ok, a terrible hack. The left margin depends on the widest
803 // row in this paragraph. Do not care about footnotes, they
804 // are *NOT* allowed in the LaTeX realisation of this layout.
806 // find the first row of this paragraph
807 Row const * tmprow = row;
808 while (tmprow->previous()
809 && tmprow->previous()->par() == row->par())
810 tmprow = tmprow->previous();
812 int minfill = tmprow->fill();
813 while (tmprow->next() && tmprow->next()->par() == row->par()) {
814 tmprow = tmprow->next();
815 if (tmprow->fill() < minfill)
816 minfill = tmprow->fill();
819 x += lyxfont::signedWidth(layout.leftmargin,
820 tclass.defaultfont());
826 LyXAlignment align; // wrong type
828 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
829 align = layout.align;
831 align = row->par()->params().align();
833 // set the correct parindent
834 if (row->pos() == 0) {
835 if ((layout.labeltype == LABEL_NO_LABEL
836 || layout.labeltype == LABEL_TOP_ENVIRONMENT
837 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
838 || (layout.labeltype == LABEL_STATIC
839 && layout.latextype == LATEX_ENVIRONMENT
840 && ! row->par()->isFirstInSequence()))
841 && align == LYX_ALIGN_BLOCK
842 && !row->par()->params().noindent()
843 && (row->par()->layout ||
844 bview->buffer()->params.paragraph_separation ==
845 BufferParams::PARSEP_INDENT))
846 x += lyxfont::signedWidth(parindent,
847 tclass.defaultfont());
848 else if (layout.labeltype == LABEL_BIBLIO) {
849 // ale970405 Right width for bibitems
850 x += bibitemMaxWidth(bview, tclass.defaultfont());
857 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
859 LyXTextClass const & tclass =
860 textclasslist.TextClass(buf->params.textclass);
861 LyXLayout const & layout = tclass[row->par()->getLayout()];
863 int x = LYX_PAPER_MARGIN
864 + lyxfont::signedWidth(tclass.rightmargin(),
865 tclass.defaultfont());
867 // this is the way, LyX handles the LaTeX-Environments.
868 // I have had this idea very late, so it seems to be a
869 // later added hack and this is true
870 if (row->par()->getDepth()) {
871 // find the next level paragraph
873 Paragraph * newpar = row->par();
876 newpar = newpar->previous();
878 && newpar->getDepth() >= row->par()->getDepth());
880 // make a corresponding row. Needed to call LeftMargin()
882 // check wether it is a sufficent paragraph
884 && tclass[newpar->getLayout()].isEnvironment()) {
886 dummyrow.par(newpar);
888 x = rightMargin(buf, &dummyrow);
890 // this is no longer an error, because this function
891 // is used to clear impossible depths after changing
892 // a layout. Since there is always a redo,
893 // LeftMargin() is always called
894 row->par()->params().depth(0);
898 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
899 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
900 * 4 / (row->par()->getDepth() + 4);
905 int LyXText::labelEnd(BufferView * bview, Row const * row) const
907 if (textclasslist.Style(bview->buffer()->params.textclass,
908 row->par()->getLayout()).margintype
912 tmprow.pos(row->par()->size());
913 return leftMargin(bview, &tmprow); /* just the beginning
916 return 0; /* LabelEnd is only needed, if the
917 layout fills a flushleft
922 // get the next breakpoint in a given paragraph
924 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
926 Paragraph * par = row->par();
931 pos_type const pos = row->pos();
933 // position of the last possible breakpoint
934 // -1 isn't a suitable value, but a flag
935 pos_type last_separator = -1;
936 width -= rightMargin(bview->buffer(), row);
938 pos_type const main_body =
939 beginningOfMainBody(bview->buffer(), par);
940 LyXLayout const & layout =
941 textclasslist.Style(bview->buffer()->params.textclass,
945 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
946 /* special code for right address boxes, only newlines count */
947 while (i < par->size()) {
948 if (par->isNewline(i)) {
950 i = par->size() - 1; // this means break
952 } else if (par->isInset(i) && par->getInset(i)
953 && par->getInset(i)->display()) {
954 par->getInset(i)->display(false);
959 // Last position is an invariant
960 pos_type const last =
962 // this is the usual handling
963 int x = leftMargin(bview, row);
964 bool doitonetime = true;
965 while (doitonetime || ((x < width) && (i < last))) {
967 char const c = par->getChar(i);
969 if (c == Paragraph::META_INSET)
970 in = par->getInset(i);
971 if (IsNewlineChar(c)) {
973 x = width; // this means break
974 } else if (in && !in->isChar()) {
975 // check wether a Display() inset is
976 // valid here. if not, change it to
979 (layout.isCommand() ||
980 (layout.labeltype == LABEL_MANUAL
981 && i < beginningOfMainBody(bview->buffer(), par))))
983 // display istn't allowd
985 x += singleWidth(bview, par, i, c);
986 } else if (in->display() || in->needFullRow()) {
987 // So break the line here
991 if (IsLineSeparatorChar(par->getChar(i+1)))
994 last_separator = last; // to avoid extra rows
996 last_separator = i - 1;
997 x = width; // this means break
999 x += singleWidth(bview, par, i, c);
1000 // we have to check this separately as we could have a
1001 // lineseparator and then the algorithm below would prefer
1002 // that which IS wrong! We should always break on an inset
1003 // if it's too long and not on the last separator.
1004 // Maybe the only exeption is insets used as chars but
1005 // then we would have to have a special function inside
1006 // the inset to tell us this. Till then we leave it as
1007 // it is now. (Jug 20020106)
1008 if (pos < i && x >= width && last_separator >= 0)
1009 last_separator = i - 1;
1012 if (IsLineSeparatorChar(c, in))
1014 x += singleWidth(bview, par, i, c);
1017 if (i == main_body) {
1018 x += lyxfont::width(layout.labelsep,
1019 getLabelFont(bview->buffer(), par));
1020 if (par->isLineSeparator(i - 1))
1021 x-= singleWidth(bview, par, i - 1);
1022 int left_margin = labelEnd(bview, row);
1023 if (x < left_margin)
1027 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1028 last_separator = i - 2;
1029 else if ((pos < i) && (last_separator < 0) && (x >= width))
1030 last_separator = i - 1;
1031 // end of paragraph is always a suitable separator
1032 else if (i == last && x < width)
1036 // well, if last_separator is still 0, the line isn't breakable.
1037 // don't care and cut simply at the end
1038 if (last_separator < 0) {
1042 // manual labels cannot be broken in LaTeX, do not care
1043 if (main_body && last_separator < main_body)
1044 last_separator = main_body - 1;
1046 return last_separator;
1050 // returns the minimum space a row needs on the screen in pixel
1051 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1053 if (paper_width < 0)
1057 // get the pure distance
1058 pos_type const last = rowLastPrintable(row);
1060 // special handling of the right address boxes
1061 if (textclasslist.Style(bview->buffer()->params.textclass,
1062 row->par()->getLayout()).margintype
1063 == MARGIN_RIGHT_ADDRESS_BOX)
1065 int const tmpfill = row->fill();
1066 row->fill(0); // the minfill in MarginLeft()
1067 w = leftMargin(bview, row);
1070 w = leftMargin(bview, row);
1072 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1073 row->par()->getLayout());
1074 pos_type const main_body =
1075 beginningOfMainBody(bview->buffer(), row->par());
1076 pos_type i = row->pos();
1079 if (main_body > 0 && i == main_body) {
1080 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1081 if (row->par()->isLineSeparator(i - 1))
1082 w -= singleWidth(bview, row->par(), i - 1);
1083 int left_margin = labelEnd(bview, row);
1084 if (w < left_margin)
1087 w += singleWidth(bview, row->par(), i);
1090 if (main_body > 0 && main_body > last) {
1091 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1092 if (last >= 0 && row->par()->isLineSeparator(last))
1093 w -= singleWidth(bview, row->par(), last);
1094 int const left_margin = labelEnd(bview, row);
1095 if (w < left_margin)
1099 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1104 // returns the minimum space a manual label needs on the screen in pixel
1105 int LyXText::labelFill(BufferView * bview, Row const * row) const
1107 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1108 // -1 because a label ends either with a space that is in the label,
1109 // or with the beginning of a footnote that is outside the label.
1111 // I don't understand this code in depth, but sometimes "last" is
1112 // less than 0 and this causes a crash. This fix seems to work
1113 // correctly, but I bet the real error is elsewhere. The bug is
1114 // triggered when you have an open footnote in a paragraph
1115 // environment with a manual label. (Asger)
1116 if (last < 0) last = 0;
1118 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1123 pos_type i = row->pos();
1125 w += singleWidth(bview, row->par(), i);
1130 if (!row->par()->params().labelWidthString().empty()) {
1131 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1132 getLabelFont(bview->buffer(), row->par())) - w,
1140 // returns the number of separators in the specified row. The separator
1141 // on the very last column doesnt count
1142 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1144 pos_type const last = rowLast(row);
1145 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1147 for (; p < last; ++p) {
1148 if (row->par()->isSeparator(p)) {
1156 // returns the number of hfills in the specified row. The LyX-Hfill is
1157 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1158 // ignored. This is *MUCH* more usefull than not to ignore!
1159 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1161 pos_type const last = rowLast(row);
1162 pos_type first = row->pos();
1164 if (first) { /* hfill *DO* count at the beginning
1166 while (first <= last && row->par()->isHfill(first)) {
1171 first = max(first, beginningOfMainBody(buf, row->par()));
1173 for (pos_type p = first; p <= last; ++p) {
1174 // last, because the end is ignored!
1176 if (row->par()->isHfill(p)) {
1184 // like NumberOfHfills, but only those in the manual label!
1185 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1187 pos_type last = rowLast(row);
1188 pos_type first = row->pos();
1189 if (first) { /* hfill *DO* count at the beginning
1191 while (first < last && row->par()->isHfill(first))
1195 last = min(last, beginningOfMainBody(buf, row->par()));
1197 for (pos_type p = first; p < last; ++p) {
1198 // last, because the end is ignored!
1199 if (row->par()->isHfill(p)) {
1207 // returns true, if a expansion is needed.
1208 // Rules are given by LaTeX
1209 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1212 // by the way, is it a hfill?
1213 if (!row_ptr->par()->isHfill(pos))
1216 // at the end of a row it does not count
1217 // unless another hfill exists on the line
1218 if (pos >= rowLast(row_ptr)) {
1219 pos_type i = row_ptr->pos();
1220 while (i < pos && !row_ptr->par()->isHfill(i)) {
1228 // at the beginning of a row it does not count, if it is not
1229 // the first row of a paragaph
1230 if (!row_ptr->pos())
1233 // in some labels it does not count
1234 if (textclasslist.Style(buf->params.textclass,
1235 row_ptr->par()->getLayout()).margintype
1237 && pos < beginningOfMainBody(buf, row_ptr->par()))
1240 // if there is anything between the first char of the row and
1241 // the sepcified position that is not a newline and not a hfill,
1242 // the hfill will count, otherwise not
1243 pos_type i = row_ptr->pos();
1244 while (i < pos && (row_ptr->par()->isNewline(i)
1245 || row_ptr->par()->isHfill(i)))
1252 LColor::color LyXText::backgroundColor()
1255 return inset_owner->backgroundColor();
1257 return LColor::background;
1260 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1262 /* get the maximum ascent and the maximum descent */
1265 float layoutasc = 0;
1266 float layoutdesc = 0;
1269 Inset * tmpinset = 0;
1271 /* ok , let us initialize the maxasc and maxdesc value.
1272 * This depends in LaTeX of the font of the last character
1273 * in the paragraph. The hack below is necessary because
1274 * of the possibility of open footnotes */
1276 /* Correction: only the fontsize count. The other properties
1277 are taken from the layoutfont. Nicer on the screen :) */
1278 Paragraph * par = row_ptr->par();
1279 Paragraph * firstpar = row_ptr->par();
1281 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1282 firstpar->getLayout());
1284 // as max get the first character of this row then it can increes but not
1285 // decrees the height. Just some point to start with so we don't have to
1286 // do the assignment below too often.
1287 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1288 LyXFont::FONT_SIZE const tmpsize = font.size();
1289 font = getLayoutFont(bview->buffer(), par);
1290 LyXFont::FONT_SIZE const size = font.size();
1291 font.setSize(tmpsize);
1293 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1295 float spacing_val = 1.0;
1296 if (!row_ptr->par()->params().spacing().isDefault()) {
1297 spacing_val = row_ptr->par()->params().spacing().getValue();
1299 spacing_val = bview->buffer()->params.spacing.getValue();
1301 //lyxerr << "spacing_val = " << spacing_val << endl;
1303 int maxasc = int(lyxfont::maxAscent(font) *
1304 layout.spacing.getValue() *
1306 int maxdesc = int(lyxfont::maxDescent(font) *
1307 layout.spacing.getValue() *
1309 pos_type const pos_end = rowLast(row_ptr);
1313 // Check if any insets are larger
1314 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1315 if (row_ptr->par()->isInset(pos)) {
1316 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1317 tmpinset = row_ptr->par()->getInset(pos);
1319 #if 1 // this is needed for deep update on initialitation
1320 tmpinset->update(bview, tmpfont);
1322 asc = tmpinset->ascent(bview, tmpfont);
1323 desc = tmpinset->descent(bview, tmpfont);
1324 maxwidth += tmpinset->width(bview, tmpfont);
1325 maxasc = max(maxasc, asc);
1326 maxdesc = max(maxdesc, desc);
1329 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1333 // Check if any custom fonts are larger (Asger)
1334 // This is not completely correct, but we can live with the small,
1335 // cosmetic error for now.
1336 LyXFont::FONT_SIZE maxsize =
1337 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1338 if (maxsize > font.size()) {
1339 font.setSize(maxsize);
1341 asc = lyxfont::maxAscent(font);
1342 desc = lyxfont::maxDescent(font);
1349 // This is nicer with box insets:
1353 row_ptr->ascent_of_text(maxasc);
1355 // is it a top line?
1356 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1358 // some parksips VERY EASY IMPLEMENTATION
1359 if (bview->buffer()->params.paragraph_separation ==
1360 BufferParams::PARSEP_SKIP)
1362 if (layout.isParagraph()
1363 && firstpar->getDepth() == 0
1364 && firstpar->previous())
1366 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1367 } else if (firstpar->previous() &&
1368 textclasslist.Style(bview->buffer()->params.textclass,
1369 firstpar->previous()->
1370 getLayout()).isParagraph() &&
1371 firstpar->previous()->getDepth() == 0)
1373 // is it right to use defskip here too? (AS)
1374 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1378 // the paper margins
1379 if (!row_ptr->par()->previous() && bv_owner)
1380 maxasc += LYX_PAPER_MARGIN;
1382 // add the vertical spaces, that the user added
1383 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1385 // do not forget the DTP-lines!
1386 // there height depends on the font of the nearest character
1387 if (firstpar->params().lineTop())
1388 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1391 // and now the pagebreaks
1392 if (firstpar->params().pagebreakTop())
1393 maxasc += 3 * defaultHeight();
1395 // This is special code for the chapter, since the label of this
1396 // layout is printed in an extra row
1397 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1398 && bview->buffer()->params.secnumdepth >= 0)
1400 float spacing_val = 1.0;
1401 if (!row_ptr->par()->params().spacing().isDefault()) {
1402 spacing_val = row_ptr->par()->params().spacing().getValue();
1404 spacing_val = bview->buffer()->params.spacing.getValue();
1407 labeladdon = int(lyxfont::maxDescent(labelfont) *
1408 layout.spacing.getValue() *
1410 + int(lyxfont::maxAscent(labelfont) *
1411 layout.spacing.getValue() *
1415 // special code for the top label
1416 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1417 || layout.labeltype == LABEL_BIBLIO
1418 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1419 && row_ptr->par()->isFirstInSequence()
1420 && !row_ptr->par()->getLabelstring().empty())
1422 float spacing_val = 1.0;
1423 if (!row_ptr->par()->params().spacing().isDefault()) {
1424 spacing_val = row_ptr->par()->params().spacing().getValue();
1426 spacing_val = bview->buffer()->params.spacing.getValue();
1430 (lyxfont::maxAscent(labelfont) *
1431 layout.spacing.getValue() *
1433 +(lyxfont::maxDescent(labelfont) *
1434 layout.spacing.getValue() *
1436 + layout.topsep * defaultHeight()
1437 + layout.labelbottomsep * defaultHeight());
1440 // and now the layout spaces, for example before and after a section,
1441 // or between the items of a itemize or enumerate environment
1443 if (!firstpar->params().pagebreakTop()) {
1444 Paragraph * prev = row_ptr->par()->previous();
1446 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1447 if (prev && prev->getLayout() == firstpar->getLayout() &&
1448 prev->getDepth() == firstpar->getDepth() &&
1449 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1451 layoutasc = (layout.itemsep * defaultHeight());
1452 } else if (row_ptr->previous()) {
1453 tmptop = layout.topsep;
1455 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1456 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1457 row_ptr->previous()->par()->
1458 getLayout()).bottomsep;
1461 layoutasc = (tmptop * defaultHeight());
1462 } else if (row_ptr->par()->params().lineTop()) {
1463 tmptop = layout.topsep;
1466 layoutasc = (tmptop * defaultHeight());
1469 prev = row_ptr->par()->outerHook();
1471 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1472 prev->getLayout()).parsep * defaultHeight());
1474 if (firstpar->previous() &&
1475 firstpar->previous()->getDepth() == 0 &&
1476 firstpar->previous()->getLayout() !=
1477 firstpar->getLayout())
1480 } else if (firstpar->previous()) {
1481 maxasc += int(layout.parsep * defaultHeight());
1487 // is it a bottom line?
1488 if (row_ptr->par() == par
1489 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1491 // the paper margins
1492 if (!par->next() && bv_owner)
1493 maxdesc += LYX_PAPER_MARGIN;
1495 // add the vertical spaces, that the user added
1496 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1498 // do not forget the DTP-lines!
1499 // there height depends on the font of the nearest character
1500 if (firstpar->params().lineBottom())
1501 maxdesc += 2 * lyxfont::ascent('x',
1502 getFont(bview->buffer(),
1504 max(pos_type(0), par->size() - 1)));
1506 // and now the pagebreaks
1507 if (firstpar->params().pagebreakBottom())
1508 maxdesc += 3 * defaultHeight();
1510 // and now the layout spaces, for example before and after
1511 // a section, or between the items of a itemize or enumerate
1513 if (!firstpar->params().pagebreakBottom()
1514 && row_ptr->par()->next()) {
1515 Paragraph * nextpar = row_ptr->par()->next();
1516 Paragraph * comparepar = row_ptr->par();
1520 if (comparepar->getDepth() > nextpar->getDepth()) {
1521 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1522 comparepar->getLayout()).bottomsep * defaultHeight());
1523 comparepar = comparepar->depthHook(nextpar->getDepth());
1524 if (comparepar->getLayout()!= nextpar->getLayout()
1525 || nextpar->getLabelWidthString() !=
1526 comparepar->getLabelWidthString())
1528 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1529 comparepar->getLayout()).bottomsep * defaultHeight());
1531 if (unusual > usual)
1532 layoutdesc = unusual;
1535 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1537 if (comparepar->getLayout()!= nextpar->getLayout()
1538 || nextpar->getLabelWidthString() !=
1539 comparepar->getLabelWidthString())
1540 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1541 comparepar->getLayout()).bottomsep * defaultHeight());
1546 // incalculate the layout spaces
1547 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1548 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1550 // calculate the new height of the text
1551 height -= row_ptr->height();
1553 row_ptr->height(maxasc + maxdesc + labeladdon);
1554 row_ptr->baseline(maxasc + labeladdon);
1556 height += row_ptr->height();
1558 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1561 row_ptr->width(maxwidth);
1562 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1564 row_ptr->width(int(maxwidth + x));
1567 width = max(0,workWidth(bview));
1569 if (r->width() > width)
1577 /* Appends the implicit specified paragraph behind the specified row,
1578 * start at the implicit given position */
1579 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1581 bool not_ready = true;
1583 // The last character position of a paragraph is an invariant so we can
1584 // safely get it here. (Asger)
1585 pos_type const lastposition = row->par()->size();
1587 // Get the next breakpoint
1588 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1592 // Insert the new row
1593 if (z < lastposition) {
1595 insertRow(row, row->par(), z);
1602 // Set the dimensions of the row
1603 // fixed fill setting now by calling inset->update() in
1604 // SingleWidth when needed!
1605 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1606 setHeightOfRow(bview, tmprow);
1608 } while (not_ready);
1612 void LyXText::breakAgain(BufferView * bview, Row * row) const
1614 bool not_ready = true;
1617 // get the next breakpoint
1618 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1621 if (z < row->par()->size()) {
1622 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1625 insertRow(row, row->par(), z);
1631 if (row->pos() == z)
1632 not_ready = false; // the rest will not change
1638 /* if there are some rows too much, delete them */
1639 /* only if you broke the whole paragraph! */
1640 Row * tmprow2 = row;
1641 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1642 tmprow2 = tmprow2->next();
1644 while (tmprow2 != row) {
1645 tmprow2 = tmprow2->previous();
1646 removeRow(tmprow2->next());
1651 /* set the dimensions of the row */
1652 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1653 setHeightOfRow(bview, tmprow);
1654 } while (not_ready);
1658 // this is just a little changed version of break again
1659 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1661 // get the next breakpoint
1662 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1665 if (z < row->par()->size()) {
1667 || (row->next() && row->next()->par() != row->par())) {
1668 /* insert a new row */
1670 insertRow(row, row->par(), z);
1676 if (row->pos() != z)
1680 // if there are some rows too much, delete them
1681 // only if you broke the whole paragraph!
1682 Row * tmprow2 = row;
1683 while (tmprow2->next()
1684 && tmprow2->next()->par() == row->par()) {
1685 tmprow2 = tmprow2->next();
1687 while (tmprow2 != row) {
1688 tmprow2 = tmprow2->previous();
1689 removeRow(tmprow2->next());
1693 // set the dimensions of the row
1694 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1695 setHeightOfRow(bview, tmprow);
1699 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1701 LyXLayout const & layout =
1702 textclasslist.Style(bview->buffer()->params.textclass,
1703 cursor.par()->getLayout());
1705 // this is only allowed, if the current paragraph is not empty or caption
1706 if ((cursor.par()->size() <= 0)
1707 && layout.labeltype!= LABEL_SENSITIVE)
1710 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1712 // Always break behind a space
1714 // It is better to erase the space (Dekel)
1715 if (cursor.pos() < cursor.par()->size()
1716 && cursor.par()->isLineSeparator(cursor.pos()))
1717 cursor.par()->erase(cursor.pos());
1718 // cursor.pos(cursor.pos() + 1);
1720 // break the paragraph
1724 keep_layout = layout.isEnvironment();
1725 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1728 // well this is the caption hack since one caption is really enough
1729 if (layout.labeltype == LABEL_SENSITIVE) {
1731 // set to standard-layout
1732 cursor.par()->setLayout(0);
1734 // set to standard-layout
1735 cursor.par()->next()->setLayout(0);
1738 /* if the cursor is at the beginning of a row without prior newline,
1740 * This touches only the screen-update. Otherwise we would may have
1741 * an empty row on the screen */
1742 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1743 && cursor.row()->pos() == cursor.pos()) {
1747 status(bview, LyXText::NEED_MORE_REFRESH);
1748 refresh_row = cursor.row();
1749 refresh_y = cursor.y() - cursor.row()->baseline();
1751 // Do not forget the special right address boxes
1752 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1753 while (refresh_row->previous() &&
1754 refresh_row->previous()->par() == refresh_row->par()) {
1755 refresh_row = refresh_row->previous();
1756 refresh_y -= refresh_row->height();
1759 removeParagraph(cursor.row());
1761 // set the dimensions of the cursor row
1762 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1764 setHeightOfRow(bview, cursor.row());
1766 while (cursor.par()->next()->size()
1767 && cursor.par()->next()->isNewline(0))
1768 cursor.par()->next()->erase(0);
1770 insertParagraph(bview, cursor.par()->next(), cursor.row());
1772 updateCounters(bview, cursor.row()->previous());
1774 /* This check is necessary. Otherwise the new empty paragraph will
1775 * be deleted automatically. And it is more friendly for the user! */
1777 setCursor(bview, cursor.par()->next(), 0);
1779 setCursor(bview, cursor.par(), 0);
1781 if (cursor.row()->next())
1782 breakAgain(bview, cursor.row()->next());
1788 // Just a macro to make some thing easier.
1789 void LyXText::redoParagraph(BufferView * bview) const
1792 redoParagraphs(bview, cursor, cursor.par()->next());
1793 setCursorIntern(bview, cursor.par(), cursor.pos());
1797 /* insert a character, moves all the following breaks in the
1798 * same Paragraph one to the right and make a rebreak */
1799 void LyXText::insertChar(BufferView * bview, char c)
1801 setUndo(bview, Undo::INSERT,
1802 cursor.par(), cursor.par()->next());
1804 // When the free-spacing option is set for the current layout,
1805 // disable the double-space checking
1807 bool const freeSpacing =
1808 textclasslist.Style(bview->buffer()->params.textclass,
1809 cursor.row()->par()->getLayout()).free_spacing ||
1810 cursor.row()->par()->isFreeSpacing();
1813 if (lyxrc.auto_number) {
1814 static string const number_operators = "+-/*";
1815 static string const number_unary_operators = "+-";
1816 static string const number_seperators = ".,:";
1818 if (current_font.number() == LyXFont::ON) {
1819 if (!IsDigit(c) && !contains(number_operators, c) &&
1820 !(contains(number_seperators, c) &&
1821 cursor.pos() >= 1 &&
1822 cursor.pos() < cursor.par()->size() &&
1823 getFont(bview->buffer(),
1825 cursor.pos()).number() == LyXFont::ON &&
1826 getFont(bview->buffer(),
1828 cursor.pos() - 1).number() == LyXFont::ON)
1830 number(bview); // Set current_font.number to OFF
1831 } else if (IsDigit(c) &&
1832 real_current_font.isVisibleRightToLeft()) {
1833 number(bview); // Set current_font.number to ON
1835 if (cursor.pos() > 0) {
1836 char const c = cursor.par()->getChar(cursor.pos() - 1);
1837 if (contains(number_unary_operators, c) &&
1838 (cursor.pos() == 1 ||
1839 cursor.par()->isSeparator(cursor.pos() - 2) ||
1840 cursor.par()->isNewline(cursor.pos() - 2) )
1842 setCharFont(bview->buffer(),
1846 } else if (contains(number_seperators, c) &&
1847 cursor.pos() >= 2 &&
1848 getFont(bview->buffer(),
1850 cursor.pos() - 2).number() == LyXFont::ON) {
1851 setCharFont(bview->buffer(),
1861 /* First check, if there will be two blanks together or a blank at
1862 the beginning of a paragraph.
1863 I decided to handle blanks like normal characters, the main
1864 difference are the special checks when calculating the row.fill
1865 (blank does not count at the end of a row) and the check here */
1867 // The bug is triggered when we type in a description environment:
1868 // The current_font is not changed when we go from label to main text
1869 // and it should (along with realtmpfont) when we type the space.
1870 // CHECK There is a bug here! (Asger)
1872 LyXFont realtmpfont = real_current_font;
1873 LyXFont rawtmpfont = current_font; /* store the current font.
1874 * This is because of the use
1875 * of cursor movements. The moving
1876 * cursor would refresh the
1879 // Get the font that is used to calculate the baselineskip
1880 pos_type const lastpos = cursor.par()->size();
1881 LyXFont rawparfont =
1882 cursor.par()->getFontSettings(bview->buffer()->params,
1885 bool jumped_over_space = false;
1887 if (!freeSpacing && IsLineSeparatorChar(c)) {
1888 if ((cursor.pos() > 0
1889 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1890 || (cursor.pos() > 0
1891 && cursor.par()->isNewline(cursor.pos() - 1))
1892 || (cursor.pos() == 0)) {
1893 static bool sent_space_message = false;
1894 if (!sent_space_message) {
1895 if (cursor.pos() == 0)
1896 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1898 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1899 sent_space_message = true;
1904 } else if (IsNewlineChar(c)) {
1905 if (cursor.pos() <= beginningOfMainBody(bview->buffer(),
1910 /* No newline at first position
1911 * of a paragraph or behind labels.
1912 * TeX does not allow that. */
1914 if (cursor.pos() < cursor.par()->size() &&
1915 cursor.par()->isLineSeparator(cursor.pos()))
1916 // newline always after a blank!
1918 cursor.row()->fill(-1); // to force a new break
1921 // the display inset stuff
1922 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1923 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1924 if (inset && (inset->display() || inset->needFullRow())) {
1925 // force a new break
1926 cursor.row()->fill(-1); // to force a new break
1930 // get the cursor row fist
1931 Row * row = cursor.row();
1932 int y = cursor.y() - row->baseline();
1933 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1934 * already insertet the character */
1935 cursor.par()->insertChar(cursor.pos(), c);
1936 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1938 if (!jumped_over_space) {
1939 // refresh the positions
1941 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1942 tmprow = tmprow->next();
1943 tmprow->pos(tmprow->pos() + 1);
1947 // Is there a break one row above
1948 if ((cursor.par()->isLineSeparator(cursor.pos())
1949 || cursor.par()->isNewline(cursor.pos())
1950 || ((cursor.pos() < cursor.par()->size()) &&
1951 cursor.par()->isInset(cursor.pos()+1))
1952 || cursor.row()->fill() == -1)
1953 && row->previous() && row->previous()->par() == row->par()) {
1954 pos_type z = nextBreakPoint(bview,
1957 if (z >= row->pos()) {
1960 // set the dimensions of the row above
1961 row->previous()->fill(fill(bview,
1965 setHeightOfRow(bview, row->previous());
1967 y -= row->previous()->height();
1969 refresh_row = row->previous();
1970 status(bview, LyXText::NEED_MORE_REFRESH);
1972 breakAgainOneRow(bview, row);
1974 current_font = rawtmpfont;
1975 real_current_font = realtmpfont;
1976 setCursor(bview, cursor.par(), cursor.pos() + 1,
1977 false, cursor.boundary());
1978 // cursor MUST be in row now.
1980 if (row->next() && row->next()->par() == row->par())
1981 need_break_row = row->next();
1985 // check, wether the last characters font has changed.
1986 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1987 && rawparfont != rawtmpfont)
1988 redoHeightOfParagraph(bview, cursor);
1995 // recalculate the fill of the row
1996 if (row->fill() >= 0) /* needed because a newline
1997 * will set fill to -1. Otherwise
1998 * we would not get a rebreak! */
1999 row->fill(fill(bview, row, workWidth(bview)));
2000 if (row->fill() < 0) {
2003 refresh_x = cursor.x();
2004 refresh_pos = cursor.pos();
2005 status(bview, LyXText::NEED_MORE_REFRESH);
2006 breakAgainOneRow(bview, row);
2007 // will the cursor be in another row now?
2008 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
2009 if (row->next() && row->next()->par() == row->par())
2010 // this should always be true
2012 breakAgainOneRow(bview, row);
2014 current_font = rawtmpfont;
2015 real_current_font = realtmpfont;
2017 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2019 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2020 != cursor.boundary())
2021 setCursor(bview, cursor.par(), cursor.pos(), false,
2022 !cursor.boundary());
2023 if (row->next() && row->next()->par() == row->par())
2024 need_break_row = row->next();
2029 refresh_x = cursor.x();
2031 refresh_pos = cursor.pos();
2033 int const tmpheight = row->height();
2034 setHeightOfRow(bview, row);
2035 if (tmpheight == row->height())
2036 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2038 status(bview, LyXText::NEED_MORE_REFRESH);
2040 current_font = rawtmpfont;
2041 real_current_font = realtmpfont;
2042 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2046 // check, wether the last characters font has changed.
2047 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2048 && rawparfont != rawtmpfont) {
2049 redoHeightOfParagraph(bview, cursor);
2051 // now the special right address boxes
2052 if (textclasslist.Style(bview->buffer()->params.textclass,
2053 cursor.par()->getLayout()).margintype
2054 == MARGIN_RIGHT_ADDRESS_BOX) {
2055 redoDrawingOfParagraph(bview, cursor);
2063 void LyXText::charInserted()
2065 // Here we could call FinishUndo for every 20 characters inserted.
2066 // This is from my experience how emacs does it.
2067 static unsigned int counter;
2077 void LyXText::prepareToPrint(BufferView * bview,
2078 Row * row, float & x,
2079 float & fill_separator,
2081 float & fill_label_hfill,
2087 float w = row->fill();
2089 fill_label_hfill = 0;
2091 fill_label_hfill = 0;
2094 row->par()->isRightToLeftPar(bview->buffer()->params);
2096 x = (workWidth(bview) > 0)
2097 ? rightMargin(bview->buffer(), row) : 0;
2099 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2101 // is there a manual margin with a manual label
2102 if (textclasslist.Style(bview->buffer()->params.textclass,
2103 row->par()->getLayout()).margintype == MARGIN_MANUAL
2104 && textclasslist.Style(bview->buffer()->params.textclass,
2105 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2107 /* one more since labels are left aligned */
2108 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2109 if (nlh && !row->par()->getLabelWidthString().empty()) {
2110 fill_label_hfill = labelFill(bview, row) / nlh;
2114 // are there any hfills in the row?
2115 float const nh = numberOfHfills(bview->buffer(), row);
2119 fill_hfill = w / nh;
2120 // we don't have to look at the alignment if it is ALIGN_LEFT and
2121 // if the row is already larger then the permitted width as then
2122 // we force the LEFT_ALIGN'edness!
2123 } else if (static_cast<int>(row->width()) < workWidth(bview)) {
2124 // is it block, flushleft or flushright?
2125 // set x how you need it
2127 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2128 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2130 align = row->par()->params().align();
2133 // center displayed insets
2135 if (row->par()->isInset(row->pos())
2136 && (inset=row->par()->getInset(row->pos()))
2137 && (inset->display())) // || (inset->scroll() < 0)))
2138 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2139 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2142 case LYX_ALIGN_BLOCK:
2143 ns = numberOfSeparators(bview->buffer(), row);
2144 if (ns && row->next() && row->next()->par() == row->par() &&
2145 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2146 && !(row->next()->par()->isInset(row->next()->pos())
2147 && row->next()->par()->getInset(row->next()->pos())
2148 && row->next()->par()->getInset(row->next()->pos())->display())
2151 fill_separator = w / ns;
2152 } else if (is_rtl) {
2156 case LYX_ALIGN_RIGHT:
2159 case LYX_ALIGN_CENTER:
2167 computeBidiTables(bview->buffer(), row);
2169 pos_type main_body =
2170 beginningOfMainBody(bview->buffer(), row->par());
2171 pos_type last = rowLast(row);
2173 if (main_body > 0 &&
2174 (main_body-1 > last ||
2175 !row->par()->isLineSeparator(main_body-1))) {
2176 LyXLayout const & layout =
2177 textclasslist.Style(bview->buffer()->params.textclass,
2178 row->par()->getLayout());
2179 x += lyxfont::width(layout.labelsep,
2180 getLabelFont(bview->buffer(), row->par()));
2181 if (main_body-1 <= last)
2182 x += fill_label_hfill;
2187 /* important for the screen */
2190 /* the cursor set functions have a special mechanism. When they
2191 * realize, that you left an empty paragraph, they will delete it.
2192 * They also delete the corresponding row */
2194 void LyXText::cursorRightOneWord(BufferView * bview) const
2196 // treat floats, HFills and Insets as words
2197 LyXCursor tmpcursor = cursor;
2198 // CHECK See comment on top of text.C
2200 if (tmpcursor.pos() == tmpcursor.par()->size()
2201 && tmpcursor.par()->next()) {
2202 tmpcursor.par(tmpcursor.par()->next());
2207 // Skip through initial nonword stuff.
2208 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2209 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2210 // printf("Current pos1 %d", tmpcursor.pos()) ;
2211 tmpcursor.pos(tmpcursor.pos() + 1);
2214 // Advance through word.
2215 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2216 tmpcursor.par()->isWord( tmpcursor.pos())) {
2217 // printf("Current pos2 %d", tmpcursor.pos()) ;
2218 tmpcursor.pos(tmpcursor.pos() + 1);
2222 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2226 void LyXText::cursorTab(BufferView * bview) const
2228 LyXCursor tmpcursor = cursor;
2229 while (tmpcursor.pos() < tmpcursor.par()->size()
2230 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2231 tmpcursor.pos(tmpcursor.pos() + 1);
2233 if (tmpcursor.pos() == tmpcursor.par()->size()){
2234 if (tmpcursor.par()->next()) {
2235 tmpcursor.par(tmpcursor.par()->next());
2239 tmpcursor.pos(tmpcursor.pos() + 1);
2240 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2244 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2245 of prior word, not to end of next prior word. */
2247 void LyXText::cursorLeftOneWord(BufferView * bview) const
2249 LyXCursor tmpcursor = cursor;
2250 cursorLeftOneWord(tmpcursor);
2251 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2254 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2256 // treat HFills, floats and Insets as words
2259 && (cur.par()->isSeparator(cur.pos() - 1)
2260 || cur.par()->isKomma(cur.pos() - 1))
2261 && !(cur.par()->isHfill(cur.pos() - 1)
2262 || cur.par()->isInset(cur.pos() - 1)))
2263 cur.pos(cur.pos() - 1);
2266 && (cur.par()->isInset(cur.pos() - 1)
2267 || cur.par()->isHfill(cur.pos() - 1))) {
2268 cur.pos(cur.pos() - 1);
2269 } else if (!cur.pos()) {
2270 if (cur.par()->previous()){
2271 cur.par(cur.par()->previous());
2272 cur.pos(cur.par()->size());
2274 } else { // Here, cur != 0
2275 while (cur.pos() > 0 &&
2276 cur.par()->isWord(cur.pos()-1) )
2277 cur.pos(cur.pos() - 1);
2281 /* -------> Select current word. This depends on behaviour of
2282 CursorLeftOneWord(), so it is patched as well. */
2283 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2284 word_location const loc) const
2286 // first put the cursor where we wana start to select the word
2289 case WHOLE_WORD_STRICT:
2290 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2291 || cursor.par()->isSeparator(cursor.pos())
2292 || cursor.par()->isKomma(cursor.pos())
2293 || cursor.par()->isSeparator(cursor.pos() -1)
2294 || cursor.par()->isKomma(cursor.pos() -1)) {
2298 // no break here, we go to the next
2301 // Move cursor to the beginning, when not already there.
2302 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2303 && !from.par()->isKomma(from.pos() - 1))
2304 cursorLeftOneWord(from);
2307 // always move the cursor to the beginning of previous word
2308 cursorLeftOneWord(from);
2311 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2317 while (to.pos() < to.par()->size()
2318 && !to.par()->isSeparator(to.pos())
2319 && !to.par()->isKomma(to.pos())
2320 && !to.par()->isHfill(to.pos()) )
2322 to.pos(to.pos() + 1);
2327 void LyXText::selectWord(BufferView * bview, word_location const loc)
2331 getWord(from, to, loc);
2333 setCursor(bview, from.par(), from.pos());
2336 selection.cursor = cursor;
2337 setCursor(bview, to.par(), to.pos() );
2338 setSelection(bview);
2342 /* -------> Select the word currently under the cursor when no
2343 selection is currently set */
2344 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2345 word_location const loc)
2347 if (!selection.set()) {
2348 selectWord(bview, loc);
2349 return selection.set();
2355 // This function is only used by the spellchecker for NextWord().
2356 // It doesn't handle LYX_ACCENTs and probably never will.
2357 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2358 float & value) const
2360 if (the_locking_inset) {
2361 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2363 value += float(cursor.y())/float(height);
2366 // we have to go on checking so move cusor to the next char
2367 if (cursor.pos() == cursor.par()->size()) {
2368 if (!cursor.par()->next())
2370 cursor.par(cursor.par()->next());
2373 cursor.pos(cursor.pos() + 1);
2375 Paragraph * tmppar = cursor.par();
2377 // If this is not the very first word, skip rest of
2378 // current word because we are probably in the middle
2379 // of a word if there is text here.
2380 if (cursor.pos() || cursor.par()->previous()) {
2381 while (cursor.pos() < cursor.par()->size()
2382 && cursor.par()->isLetter(cursor.pos()))
2383 cursor.pos(cursor.pos() + 1);
2386 // Now, skip until we have real text (will jump paragraphs)
2387 while ((cursor.par()->size() > cursor.pos()
2388 && (!cursor.par()->isLetter(cursor.pos()))
2389 && (!cursor.par()->isInset(cursor.pos()) ||
2390 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2391 || (cursor.par()->size() == cursor.pos()
2392 && cursor.par()->next()))
2394 if (cursor.pos() == cursor.par()->size()) {
2395 cursor.par(cursor.par()->next());
2398 cursor.pos(cursor.pos() + 1);
2401 // now check if we hit an inset so it has to be a inset containing text!
2402 if (cursor.pos() < cursor.par()->size() &&
2403 cursor.par()->isInset(cursor.pos()))
2406 cursor.par()->getInset(cursor.pos())->edit(bview);
2407 // now call us again to do the above trick
2408 // but obviously we have to start from down below ;)
2409 return bview->text->selectNextWordToSpellcheck(bview, value);
2412 // Update the value if we changed paragraphs
2413 if (cursor.par() != tmppar){
2414 setCursor(bview, cursor.par(), cursor.pos());
2415 value = float(cursor.y())/float(height);
2418 // Start the selection from here
2419 selection.cursor = cursor;
2421 // and find the end of the word (insets like optional hyphens
2422 // and ligature break are part of a word)
2423 while (cursor.pos() < cursor.par()->size()
2424 && (cursor.par()->isLetter(cursor.pos())))
2425 cursor.pos(cursor.pos() + 1);
2427 // Finally, we copy the word to a string and return it
2429 if (selection.cursor.pos() < cursor.pos()) {
2431 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2432 if (!cursor.par()->isInset(i))
2433 str += cursor.par()->getChar(i);
2440 // This one is also only for the spellchecker
2441 void LyXText::selectSelectedWord(BufferView * bview)
2443 if (the_locking_inset) {
2444 the_locking_inset->selectSelectedWord(bview);
2447 // move cursor to the beginning
2448 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2450 // set the sel cursor
2451 selection.cursor = cursor;
2453 // now find the end of the word
2454 while (cursor.pos() < cursor.par()->size()
2455 && (cursor.par()->isLetter(cursor.pos())))
2456 cursor.pos(cursor.pos() + 1);
2458 setCursor(bview, cursor.par(), cursor.pos());
2460 // finally set the selection
2461 setSelection(bview);
2465 /* -------> Delete from cursor up to the end of the current or next word. */
2466 void LyXText::deleteWordForward(BufferView * bview)
2468 if (!cursor.par()->size())
2471 LyXCursor tmpcursor = cursor;
2472 tmpcursor.row(0); //Â ??
2473 selection.set(true); // to avoid deletion
2474 cursorRightOneWord(bview);
2475 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2476 selection.cursor = cursor;
2478 setSelection(bview);
2480 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2481 cutSelection(bview, true, false);
2486 /* -------> Delete from cursor to start of current or prior word. */
2487 void LyXText::deleteWordBackward(BufferView * bview)
2489 if (!cursor.par()->size())
2492 LyXCursor tmpcursor = cursor;
2493 tmpcursor.row(0); // ??
2494 selection.set(true); // to avoid deletion
2495 cursorLeftOneWord(bview);
2496 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2497 selection.cursor = cursor;
2499 setSelection(bview);
2500 cutSelection(bview, true, false);
2505 /* -------> Kill to end of line. */
2506 void LyXText::deleteLineForward(BufferView * bview)
2508 if (!cursor.par()->size())
2509 // Paragraph is empty, so we just go to the right
2512 LyXCursor tmpcursor = cursor;
2513 // We can't store the row over a regular setCursor
2514 // so we set it to 0 and reset it afterwards.
2515 tmpcursor.row(0); //Â ??
2516 selection.set(true); // to avoid deletion
2518 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2519 selection.cursor = cursor;
2521 setSelection(bview);
2522 // What is this test for ??? (JMarc)
2523 if (!selection.set()) {
2524 deleteWordForward(bview);
2526 cutSelection(bview, true, false);
2532 // Change the case of a word at cursor position.
2533 // This function directly manipulates Paragraph::text because there
2534 // is no Paragraph::SetChar currently. I did what I could to ensure
2535 // that it is correct. I guess part of it should be moved to
2536 // Paragraph, but it will have to change for 1.1 anyway. At least
2537 // it does not access outside of the allocated array as the older
2538 // version did. (JMarc)
2539 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2544 if (selection.set()) {
2545 from = selection.start;
2548 getWord(from, to, PARTIAL_WORD);
2549 setCursor(bview, to.par(), to.pos() + 1);
2552 changeRegionCase(bview, from, to, action);
2556 void LyXText::changeRegionCase(BufferView * bview,
2557 LyXCursor const & from,
2558 LyXCursor const & to,
2559 LyXText::TextCase action)
2561 lyx::Assert(from <= to);
2563 setUndo(bview, Undo::FINISH,
2564 from.par(), to.par()->next());
2566 pos_type pos = from.pos();
2567 Paragraph * par = from.par();
2569 while (par && (pos != to.pos() || par != to.par())) {
2570 unsigned char c = par->getChar(pos);
2571 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2573 case text_lowercase:
2576 case text_capitalization:
2578 action = text_lowercase;
2580 case text_uppercase:
2585 par->setChar(pos, c);
2586 checkParagraph(bview, par, pos);
2589 if (pos == par->size()) {
2594 if (to.row() != from.row()) {
2595 refresh_y = from.y() - from.row()->baseline();
2596 refresh_row = from.row();
2597 status(bview, LyXText::NEED_MORE_REFRESH);
2602 void LyXText::transposeChars(BufferView & bview)
2604 Paragraph * tmppar = cursor.par();
2606 setUndo(&bview, Undo::FINISH,
2607 tmppar, tmppar->next());
2609 pos_type tmppos = cursor.pos();
2611 // First decide if it is possible to transpose at all
2613 // We are at the beginning of a paragraph.
2614 if (tmppos == 0) return;
2616 // We are at the end of a paragraph.
2617 if (tmppos == tmppar->size() - 1) return;
2619 unsigned char c1 = tmppar->getChar(tmppos);
2620 unsigned char c2 = tmppar->getChar(tmppos - 1);
2622 if (c1 != Paragraph::META_INSET
2623 && c2 != Paragraph::META_INSET) {
2624 tmppar->setChar(tmppos, c2);
2625 tmppar->setChar(tmppos - 1, c1);
2627 // We should have an implementation that handles insets
2628 // as well, but that will have to come later. (Lgb)
2629 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2633 void LyXText::Delete(BufferView * bview)
2635 // this is a very easy implementation
2637 LyXCursor old_cursor = cursor;
2638 int const old_cur_par_id = old_cursor.par()->id();
2639 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2640 old_cursor.par()->previous()->id() : 0;
2642 // just move to the right
2645 // CHECK Look at the comment here.
2646 // This check is not very good...
2647 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2648 // and that can very well delete the par or par->previous in
2649 // old_cursor. Will a solution where we compare paragraph id's
2651 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2652 == old_cur_par_prev_id
2653 && cursor.par()->id() != old_cur_par_id) {
2654 // delete-empty-paragraph-mechanism has done it
2658 // if you had success make a backspace
2659 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2660 LyXCursor tmpcursor = cursor;
2661 // to make sure undo gets the right cursor position
2662 cursor = old_cursor;
2663 setUndo(bview, Undo::DELETE,
2664 cursor.par(), cursor.par()->next());
2671 void LyXText::backspace(BufferView * bview)
2673 // Get the font that is used to calculate the baselineskip
2674 pos_type lastpos = cursor.par()->size();
2675 LyXFont rawparfont =
2676 cursor.par()->getFontSettings(bview->buffer()->params,
2679 if (cursor.pos() == 0) {
2680 // The cursor is at the beginning of a paragraph,
2681 // so the the backspace will collapse two paragraphs into one.
2683 // we may paste some paragraphs
2685 // is it an empty paragraph?
2688 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2689 // This is an empty paragraph and we delete it just by moving the cursor one step
2690 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2691 // of the paragraph.
2693 if (cursor.par()->previous()) {
2694 Paragraph * tmppar = cursor.par()->previous();
2695 if (cursor.par()->getLayout() == tmppar->getLayout()
2696 && cursor.par()->getAlign() == tmppar->getAlign()) {
2697 // Inherit bottom DTD from the paragraph below.
2698 // (the one we are deleting)
2699 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2700 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2701 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2706 // the layout things can change the height of a row !
2707 int const tmpheight = cursor.row()->height();
2708 setHeightOfRow(bview, cursor.row());
2709 if (cursor.row()->height() != tmpheight) {
2710 refresh_y = cursor.y() - cursor.row()->baseline();
2711 refresh_row = cursor.row();
2712 status(bview, LyXText::NEED_MORE_REFRESH);
2718 if (cursor.par()->previous()) {
2719 setUndo(bview, Undo::DELETE,
2720 cursor.par()->previous(), cursor.par()->next());
2723 Paragraph * tmppar = cursor.par();
2724 Row * tmprow = cursor.row();
2726 // We used to do cursorLeftIntern() here, but it is
2727 // not a good idea since it triggers the auto-delete
2728 // mechanism. So we do a cursorLeftIntern()-lite,
2729 // without the dreaded mechanism. (JMarc)
2730 if (cursor.par()->previous()) {
2731 // steps into the above paragraph.
2732 setCursorIntern(bview, cursor.par()->previous(),
2733 cursor.par()->previous()->size(),
2737 /* Pasting is not allowed, if the paragraphs have different
2738 layout. I think it is a real bug of all other
2739 word processors to allow it. It confuses the user.
2740 Even so with a footnote paragraph and a non-footnote
2741 paragraph. I will not allow pasting in this case,
2742 because the user would be confused if the footnote behaves
2743 different wether it is open or closed.
2745 Correction: Pasting is always allowed with standard-layout
2747 if (cursor.par() != tmppar
2748 && (cursor.par()->getLayout() == tmppar->getLayout()
2749 || tmppar->getLayout() == 0 /*standard*/)
2750 && cursor.par()->getAlign() == tmppar->getAlign())
2752 removeParagraph(tmprow);
2754 cursor.par()->pasteParagraph(bview->buffer()->params);
2756 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2757 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2758 // strangely enough it seems that commenting out the line above removes
2759 // most or all of the segfaults. I will however also try to move the
2760 // two Remove... lines in front of the PasteParagraph too.
2763 cursor.pos(cursor.pos() - 1);
2765 status(bview, LyXText::NEED_MORE_REFRESH);
2766 refresh_row = cursor.row();
2767 refresh_y = cursor.y() - cursor.row()->baseline();
2769 // remove the lost paragraph
2770 // This one is not safe, since the paragraph that the tmprow and the
2771 // following rows belong to has been deleted by the PasteParagraph
2772 // above. The question is... could this be moved in front of the
2774 //RemoveParagraph(tmprow);
2775 //RemoveRow(tmprow);
2777 // This rebuilds the rows.
2778 appendParagraph(bview, cursor.row());
2779 updateCounters(bview, cursor.row());
2781 // the row may have changed, block, hfills etc.
2782 setCursor(bview, cursor.par(), cursor.pos(), false);
2785 /* this is the code for a normal backspace, not pasting
2787 setUndo(bview, Undo::DELETE,
2788 cursor.par(), cursor.par()->next());
2789 // We used to do cursorLeftIntern() here, but it is
2790 // not a good idea since it triggers the auto-delete
2791 // mechanism. So we do a cursorLeftIntern()-lite,
2792 // without the dreaded mechanism. (JMarc)
2793 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2794 false, cursor.boundary());
2796 // some insets are undeletable here
2797 if (cursor.par()->isInset(cursor.pos())) {
2798 if (!cursor.par()->getInset(cursor.pos())->deletable())
2800 // force complete redo when erasing display insets
2801 // this is a cruel method but safe..... Matthias
2802 if (cursor.par()->getInset(cursor.pos())->display() ||
2803 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2804 cursor.par()->erase(cursor.pos());
2805 redoParagraph(bview);
2810 Row * row = cursor.row();
2811 int y = cursor.y() - row->baseline();
2813 /* remember that a space at the end of a row doesnt count
2814 * when calculating the fill */
2815 if (cursor.pos() < rowLast(row) ||
2816 !cursor.par()->isLineSeparator(cursor.pos())) {
2817 row->fill(row->fill() + singleWidth(bview,
2822 /* some special code when deleting a newline. This is similar
2823 * to the behavior when pasting paragraphs */
2824 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2825 cursor.par()->erase(cursor.pos());
2826 // refresh the positions
2828 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2829 tmprow = tmprow->next();
2830 tmprow->pos(tmprow->pos() - 1);
2832 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2833 cursor.pos(cursor.pos() - 1);
2835 if (cursor.pos() < cursor.par()->size()
2836 && !cursor.par()->isSeparator(cursor.pos())) {
2837 cursor.par()->insertChar(cursor.pos(), ' ');
2838 setCharFont(bview->buffer(), cursor.par(),
2839 cursor.pos(), current_font);
2840 // refresh the positions
2842 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2843 tmprow = tmprow->next();
2844 tmprow->pos(tmprow->pos() + 1);
2848 cursor.par()->erase(cursor.pos());
2850 // refresh the positions
2852 while (tmprow->next()
2853 && tmprow->next()->par() == row->par()) {
2854 tmprow = tmprow->next();
2855 tmprow->pos(tmprow->pos() - 1);
2858 // delete newlines at the beginning of paragraphs
2859 while (cursor.par()->size() &&
2860 cursor.par()->isNewline(cursor.pos()) &&
2861 cursor.pos() == beginningOfMainBody(bview->buffer(),
2863 cursor.par()->erase(cursor.pos());
2864 // refresh the positions
2866 while (tmprow->next() &&
2867 tmprow->next()->par() == row->par()) {
2868 tmprow = tmprow->next();
2869 tmprow->pos(tmprow->pos() - 1);
2874 // is there a break one row above
2875 if (row->previous() && row->previous()->par() == row->par()) {
2876 z = nextBreakPoint(bview, row->previous(),
2878 if (z >= row->pos()) {
2881 Row * tmprow = row->previous();
2883 // maybe the current row is now empty
2884 if (row->pos() >= row->par()->size()) {
2889 breakAgainOneRow(bview, row);
2890 if (row->next() && row->next()->par() == row->par())
2891 need_break_row = row->next();
2896 // set the dimensions of the row above
2897 y -= tmprow->height();
2898 tmprow->fill(fill(bview, tmprow,
2900 setHeightOfRow(bview, tmprow);
2903 refresh_row = tmprow;
2904 status(bview, LyXText::NEED_MORE_REFRESH);
2905 setCursor(bview, cursor.par(), cursor.pos(),
2906 false, cursor.boundary());
2907 //current_font = rawtmpfont;
2908 //real_current_font = realtmpfont;
2909 // check, whether the last character's font has changed.
2911 cursor.par()->getFontSettings(bview->buffer()->params,
2912 cursor.par()->size() - 1))
2913 redoHeightOfParagraph(bview, cursor);
2918 // break the cursor row again
2919 if (row->next() && row->next()->par() == row->par() &&
2920 (rowLast(row) == row->par()->size() - 1 ||
2921 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2923 /* it can happen that a paragraph loses one row
2924 * without a real breakup. This is when a word
2925 * is to long to be broken. Well, I don t care this
2927 if (rowLast(row) == row->par()->size() - 1)
2928 removeRow(row->next());
2932 status(bview, LyXText::NEED_MORE_REFRESH);
2934 breakAgainOneRow(bview, row);
2935 // will the cursor be in another row now?
2936 if (row->next() && row->next()->par() == row->par() &&
2937 rowLast(row) <= cursor.pos()) {
2939 breakAgainOneRow(bview, row);
2942 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2944 if (row->next() && row->next()->par() == row->par())
2945 need_break_row = row->next();
2949 // set the dimensions of the row
2950 row->fill(fill(bview, row, workWidth(bview)));
2951 int const tmpheight = row->height();
2952 setHeightOfRow(bview, row);
2953 if (tmpheight == row->height())
2954 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2956 status(bview, LyXText::NEED_MORE_REFRESH);
2959 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2963 // current_font = rawtmpfont;
2964 // real_current_font = realtmpfont;
2966 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2967 != cursor.boundary())
2968 setCursor(bview, cursor.par(), cursor.pos(), false,
2969 !cursor.boundary());
2971 lastpos = cursor.par()->size();
2972 if (cursor.pos() == lastpos)
2973 setCurrentFont(bview);
2975 // check, whether the last characters font has changed.
2977 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2978 redoHeightOfParagraph(bview, cursor);
2980 // now the special right address boxes
2981 if (textclasslist.Style(bview->buffer()->params.textclass,
2982 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2983 redoDrawingOfParagraph(bview, cursor);
2989 bool LyXText::paintRowBackground(DrawRowParams & p)
2991 bool clear_area = true;
2993 LyXFont font(LyXFont::ALL_SANE);
2995 pos_type const last = rowLastPrintable(p.row);
2997 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2998 && p.row->par()->isInset(p.row->pos())) {
2999 inset = p.row->par()->getInset(p.row->pos());
3001 clear_area = inset->doClearArea();
3011 int const y = p.yo < 0 ? 0 : p.yo;
3012 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3013 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3020 int h = p.row->baseline() - inset->ascent(p.bv, font);
3022 // first clear the whole row above the inset!
3024 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3027 // clear the space below the inset!
3028 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3029 if ((p.row->height() - h) > 0) {
3030 p.pain->fillRectangle(p.xo, p.yo + h,
3031 p.width, p.row->height() - h, backgroundColor());
3034 // clear the space behind the inset, if needed
3035 if (!inset->display() && !inset->needFullRow()) {
3036 int const xp = int(p.x) + inset->width(p.bv, font);
3037 if (p.width - xp > 0) {
3038 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3039 p.row->height(), backgroundColor());
3047 void LyXText::paintRowSelection(DrawRowParams & p)
3049 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3051 // the current selection
3052 int const startx = selection.start.x();
3053 int const endx = selection.end.x();
3054 int const starty = selection.start.y();
3055 int const endy = selection.end.y();
3056 Row const * startrow = selection.start.row();
3057 Row const * endrow = selection.end.row();
3061 if (bidi_same_direction) {
3065 int h = row->height();
3067 if (startrow == row && endrow == row) {
3068 if (startx < endx) {
3071 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3075 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3077 } else if (startrow == row) {
3078 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3079 int const w = (is_rtl) ? startx : (p.width - startx);
3080 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3081 } else if (endrow == row) {
3082 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3083 int const w = (is_rtl) ? (p.width - endx) : endx;
3084 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3085 } else if (p.y > starty && p.y < endy) {
3086 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3089 } else if (startrow != row && endrow != row) {
3091 int h = row->height();
3092 if (p.y > starty && p.y < endy) {
3093 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3098 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3104 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3106 Buffer const * buffer = p.bv->buffer();
3107 Paragraph * par = row->par();
3108 pos_type main_body = beginningOfMainBody(buffer, par);
3109 pos_type const last = rowLastPrintable(row);
3111 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3112 pos_type pos = vis2log(vpos);
3113 float const old_tmpx = tmpx;
3114 if (main_body > 0 && pos == main_body - 1) {
3115 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3117 LyXFont const lfont = getLabelFont(buffer, par);
3120 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3122 if (par->isLineSeparator(main_body - 1))
3123 tmpx -= singleWidth(p.bv, par, main_body - 1);
3126 if (hfillExpansion(buffer, row, pos)) {
3127 tmpx += singleWidth(p.bv, par, pos);
3128 if (pos >= main_body)
3131 tmpx += p.label_hfill;
3134 else if (par->isSeparator(pos)) {
3135 tmpx += singleWidth(p.bv, par, pos);
3136 if (pos >= main_body)
3137 tmpx += p.separator;
3139 tmpx += singleWidth(p.bv, par, pos);
3142 if ((startrow != row || selection.start.pos() <= pos) &&
3143 (endrow != row || pos < selection.end.pos())) {
3144 // Here we do not use p.x as p.xo was added to p.x.
3145 p.pain->fillRectangle(int(old_tmpx), p.yo,
3146 int(tmpx - old_tmpx + 1),
3147 row->height(), LColor::selection);
3150 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3151 p.pain->fillRectangle(p.xo + int(tmpx),
3152 p.yo, int(p.bv->workWidth() - tmpx),
3153 row->height(), LColor::selection);
3159 void LyXText::paintRowAppendix(DrawRowParams & p)
3161 // FIXME: can be just p.width ?
3162 int const ww = p.bv->workWidth();
3163 Paragraph * firstpar = p.row->par();
3165 if (firstpar->params().appendix()) {
3166 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3167 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3172 void LyXText::paintRowDepthBar(DrawRowParams & p)
3174 Paragraph::depth_type const depth = p.row->par()->getDepth();
3179 Paragraph::depth_type prev_depth = 0;
3180 if (p.row->previous())
3181 prev_depth = p.row->previous()->par()->getDepth();
3182 Paragraph::depth_type next_depth = 0;
3184 next_depth = p.row->next()->par()->getDepth();
3186 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3187 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3188 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3190 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3192 int const w = LYX_PAPER_MARGIN / 5;
3194 if (i > prev_depth) {
3195 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3197 if (i > next_depth) {
3198 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3204 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3206 int const arrow_size = 4;
3207 int const space_size = int(vsp.inPixels(bv));
3209 if (vsp.kind() != VSpace::LENGTH) {
3215 int const min_size = std::max(3 * arrow_size,
3216 lyxfont::maxAscent(font)
3217 + lyxfont::maxDescent(font));
3219 if (vsp.length().len().value() < 0.0)
3222 return std::max(min_size, space_size);
3226 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3227 VSpace const & vsp, int start)
3229 int const arrow_size = 4;
3230 int const size = getLengthMarkerHeight(p.bv, vsp);
3231 int const end = start + size;
3233 // the label to display (if any)
3235 // y-values for top arrow
3237 // y-values for bottom arrow
3239 switch (vsp.kind()) {
3240 case VSpace::LENGTH:
3242 str = prefix + " (" + vsp.asLyXCommand() + ")";
3243 // adding or removing space
3244 bool const added = !(vsp.length().len().value() < 0.0);
3245 ty1 = added ? (start + arrow_size) : start;
3246 ty2 = added ? start : (start + arrow_size);
3247 by1 = added ? (end - arrow_size) : end;
3248 by2 = added ? end : (end - arrow_size);
3251 case VSpace:: VFILL:
3252 str = prefix + " (vertical fill)";
3257 // nothing to draw here
3261 int const leftx = p.xo + leftMargin(p.bv, p.row);
3262 int const midx = leftx + arrow_size;
3263 int const rightx = midx + arrow_size;
3271 font.setColor(LColor::added_space).decSize();
3272 lyxfont::rectText(str, font, w, a, d);
3274 p.pain->rectText(leftx + 2 * arrow_size + 5,
3275 start + ((end - start) / 2) + d,
3281 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3282 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3285 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3286 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3289 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3295 void LyXText::paintFirstRow(DrawRowParams & p)
3297 Paragraph * par = p.row->par();
3298 ParagraphParameters const & parparams = par->params();
3300 // start of appendix?
3301 if (parparams.startOfAppendix()) {
3302 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3307 // think about the margins
3308 if (!p.row->previous() && bv_owner)
3309 y_top += LYX_PAPER_MARGIN;
3311 // draw a top pagebreak
3312 if (parparams.pagebreakTop()) {
3313 int const y = p.yo + y_top + 2*defaultHeight();
3314 p.pain->line(p.xo, y, p.xo + p.width, y,
3315 LColor::pagebreak, Painter::line_onoffdash);
3322 pb_font.setColor(LColor::pagebreak).decSize();
3323 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3324 p.pain->rectText((p.width - w)/2, y + d,
3325 _("Page Break (top)"), pb_font,
3328 y_top += 3 * defaultHeight();
3331 // draw the additional space if needed:
3332 y_top += drawLengthMarker(p, _("Space above"),
3333 parparams.spaceTop(), p.yo + y_top);
3335 Buffer const * buffer = p.bv->buffer();
3337 LyXLayout const & layout =
3338 textclasslist.Style(buffer->params.textclass, par->getLayout());
3340 // think about the parskip
3341 // some parskips VERY EASY IMPLEMENTATION
3342 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3343 if (par->previous()) {
3344 if (layout.latextype == LATEX_PARAGRAPH
3345 && !par->getDepth()) {
3346 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3348 LyXLayout const & playout =
3349 textclasslist.Style(buffer->params.textclass,
3350 par->previous()->getLayout());
3351 if (playout.latextype == LATEX_PARAGRAPH
3352 && !par->previous()->getDepth()) {
3353 // is it right to use defskip here, too? (AS)
3354 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3360 int const ww = p.bv->workWidth();
3363 if (parparams.lineTop()) {
3364 LyXFont font(LyXFont::ALL_SANE);
3365 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3369 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3370 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3371 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3372 LColor::topline, Painter::line_solid,
3373 Painter::line_thick);
3378 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3380 // should we print a label?
3381 if (layout.labeltype >= LABEL_STATIC
3382 && (layout.labeltype != LABEL_STATIC
3383 || layout.latextype != LATEX_ENVIRONMENT
3384 || par->isFirstInSequence())) {
3386 LyXFont font = getLabelFont(buffer, par);
3387 if (!par->getLabelstring().empty()) {
3389 string const str = par->getLabelstring();
3391 // this is special code for the chapter layout. This is
3392 // printed in an extra row and has a pagebreak at
3394 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3395 if (buffer->params.secnumdepth >= 0) {
3396 float spacing_val = 1.0;
3397 if (!parparams.spacing().isDefault()) {
3398 spacing_val = parparams.spacing().getValue();
3400 spacing_val = buffer->params.spacing.getValue();
3404 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3405 + int(layout.parsep) * defaultHeight();
3408 x = ww - leftMargin(p.bv, p.row) -
3409 lyxfont::width(str, font);
3412 p.pain->text(int(x),
3413 p.yo + p.row->baseline() -
3414 p.row->ascent_of_text() - maxdesc,
3419 x = ww - leftMargin(p.bv, p.row)
3420 + lyxfont::width(layout.labelsep, font);
3422 x = p.x - lyxfont::width(layout.labelsep, font)
3423 - lyxfont::width(str, font);
3426 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3429 // the labels at the top of an environment.
3430 // More or less for bibliography
3431 } else if (par->isFirstInSequence() &&
3432 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3433 layout.labeltype == LABEL_BIBLIO ||
3434 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3435 LyXFont font = getLabelFont(buffer, par);
3436 if (!par->getLabelstring().empty()) {
3437 string const str = par->getLabelstring();
3438 float spacing_val = 1.0;
3439 if (!parparams.spacing().isDefault()) {
3440 spacing_val = parparams.spacing().getValue();
3442 spacing_val = buffer->params.spacing.getValue();
3446 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3447 + (layout.labelbottomsep * defaultHeight()));
3450 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3451 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3452 + ww - rightMargin(buffer, p.row) ) / 2;
3453 x -= lyxfont::width(str, font) / 2;
3454 } else if (is_rtl) {
3455 x = ww - leftMargin(p.bv, p.row) -
3456 lyxfont::width(str, font);
3458 p.pain->text(int(x), p.yo + p.row->baseline()
3459 - p.row->ascent_of_text() - maxdesc,
3464 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3465 LyXFont font = getLayoutFont(buffer, par);
3468 x = ww - leftMargin(p.bv, p.row)
3469 + lyxfont::width(layout.labelsep, font);
3471 x = p.x - lyxfont::width(layout.labelsep, font)
3472 - par->bibkey->width(p.bv, font);
3474 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3479 void LyXText::paintLastRow(DrawRowParams & p)
3481 Paragraph * par = p.row->par();
3482 ParagraphParameters const & parparams = par->params();
3483 int y_bottom = p.row->height() - 1;
3485 // think about the margins
3486 if (!p.row->next() && bv_owner)
3487 y_bottom -= LYX_PAPER_MARGIN;
3489 int const ww = p.bv->workWidth();
3491 // draw a bottom pagebreak
3492 if (parparams.pagebreakBottom()) {
3494 pb_font.setColor(LColor::pagebreak).decSize();
3495 int const y = p.yo + y_bottom - 2 * defaultHeight();
3497 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3498 Painter::line_onoffdash);
3503 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3504 p.pain->rectText((ww - w) / 2, y + d,
3505 _("Page Break (bottom)"),
3506 pb_font, backgroundColor(), backgroundColor());
3508 y_bottom -= 3 * defaultHeight();
3511 // draw the additional space if needed:
3512 int const height = getLengthMarkerHeight(p.bv,
3513 parparams.spaceBottom());
3514 y_bottom -= drawLengthMarker(p, _("Space below"),
3515 parparams.spaceBottom(),
3516 p.yo + y_bottom - height);
3518 Buffer const * buffer = p.bv->buffer();
3520 // draw a bottom line
3521 if (parparams.lineBottom()) {
3522 LyXFont font(LyXFont::ALL_SANE);
3523 int const asc = lyxfont::ascent('x',
3524 getFont(buffer, par,
3525 max(pos_type(0), par->size() - 1)));
3529 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3530 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3531 int const y = p.yo + y_bottom;
3532 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3533 Painter::line_thick);
3538 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3539 int const endlabel = par->getEndLabel(buffer->params);
3544 case END_LABEL_FILLED_BOX:
3546 LyXFont const font = getLabelFont(buffer, par);
3547 int const size = int(0.75 * lyxfont::maxAscent(font));
3548 int const y = (p.yo + p.row->baseline()) - size;
3549 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3551 if (p.row->fill() <= size)
3552 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3554 if (endlabel == END_LABEL_BOX) {
3555 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3557 p.pain->fillRectangle(x, y, size, size,
3562 case END_LABEL_STATIC:
3564 LyXFont font(LyXFont::ALL_SANE);
3565 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3566 string const str = textclasslist.
3567 Style(buffer->params.textclass, layout).endlabelstring();
3568 font = getLabelFont(buffer, par);
3569 int const x = is_rtl ?
3570 int(p.x) - lyxfont::width(str, font)
3571 : ww - rightMargin(buffer, p.row) - p.row->fill();
3572 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3575 case END_LABEL_NO_LABEL:
3580 void LyXText::paintRowText(DrawRowParams & p)
3582 Paragraph * par = p.row->par();
3583 Buffer const * buffer = p.bv->buffer();
3585 pos_type const last = rowLastPrintable(p.row);
3586 pos_type main_body =
3587 beginningOfMainBody(buffer, par);
3588 if (main_body > 0 &&
3589 (main_body - 1 > last ||
3590 !par->isLineSeparator(main_body - 1))) {
3594 LyXLayout const & layout =
3595 textclasslist.Style(buffer->params.textclass, par->getLayout());
3597 pos_type vpos = p.row->pos();
3598 while (vpos <= last) {
3599 pos_type pos = vis2log(vpos);
3600 if (main_body > 0 && pos == main_body - 1) {
3601 int const lwidth = lyxfont::width(layout.labelsep,
3602 getLabelFont(buffer, par));
3604 p.x += p.label_hfill + lwidth
3605 - singleWidth(p.bv, par, main_body - 1);
3608 if (par->isHfill(pos)) {
3611 int const y0 = p.yo + p.row->baseline();
3612 int const y1 = y0 - defaultHeight() / 2;
3614 p.pain->line(int(p.x), y1, int(p.x), y0,
3615 LColor::added_space);
3617 if (hfillExpansion(buffer, p.row, pos)) {
3618 int const y2 = (y0 + y1) / 2;
3620 if (pos >= main_body) {
3621 p.pain->line(int(p.x), y2,
3622 int(p.x + p.hfill), y2,
3623 LColor::added_space,
3624 Painter::line_onoffdash);
3627 p.pain->line(int(p.x), y2,
3628 int(p.x + p.label_hfill), y2,
3629 LColor::added_space,
3630 Painter::line_onoffdash);
3631 p.x += p.label_hfill;
3633 p.pain->line(int(p.x), y1,
3635 LColor::added_space);
3639 } else if (par->isSeparator(pos)) {
3640 p.x += singleWidth(p.bv, par, pos);
3641 if (pos >= main_body)
3651 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3652 Row * row, int y, bool cleared)
3654 if (row->height() <= 0) {
3655 lyxerr << "LYX_ERROR: row.height: "
3656 << row->height() << endl;
3662 // set up drawing parameters
3664 p.pain = &bv->painter();
3668 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3669 if (inset_owner && (p.x < 0))
3673 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3674 p.cleared = cleared;
3678 // clear to background if necessary
3679 p.cleared = paintRowBackground(p);
3681 // paint the selection background
3682 if (selection.set()) {
3683 paintRowSelection(p);
3686 // vertical lines for appendix
3687 paintRowAppendix(p);
3689 // environment depth brackets
3690 paintRowDepthBar(p);
3692 // draw any stuff wanted for a first row of a paragraph
3697 // draw any stuff wanted for the last row of a paragraph
3698 if (!row->next() || (row->next()->par() != row->par())) {
3707 int LyXText::defaultHeight() const
3709 LyXFont font(LyXFont::ALL_SANE);
3710 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3714 /* returns the column near the specified x-coordinate of the row
3715 * x is set to the real beginning of this column */
3717 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3718 bool & boundary) const
3721 float fill_separator;
3723 float fill_label_hfill;
3725 prepareToPrint(bview, row, tmpx, fill_separator,
3726 fill_hfill, fill_label_hfill);
3728 pos_type vc = row->pos();
3729 pos_type last = rowLastPrintable(row);
3731 LyXLayout const & layout =
3732 textclasslist.Style(bview->buffer()->params.textclass,
3733 row->par()->getLayout());
3734 bool left_side = false;
3736 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3737 float last_tmpx = tmpx;
3739 if (main_body > 0 &&
3740 (main_body - 1 > last ||
3741 !row->par()->isLineSeparator(main_body - 1)))
3744 while (vc <= last && tmpx <= x) {
3747 if (main_body > 0 && c == main_body-1) {
3748 tmpx += fill_label_hfill +
3749 lyxfont::width(layout.labelsep,
3750 getLabelFont(bview->buffer(), row->par()));
3751 if (row->par()->isLineSeparator(main_body - 1))
3752 tmpx -= singleWidth(bview, row->par(), main_body-1);
3755 if (hfillExpansion(bview->buffer(), row, c)) {
3756 x += singleWidth(bview, row->par(), c);
3760 tmpx += fill_label_hfill;
3762 else if (row->par()->isSeparator(c)) {
3763 tmpx += singleWidth(bview, row->par(), c);
3765 tmpx+= fill_separator;
3767 tmpx += singleWidth(bview, row->par(), c);
3771 if ((tmpx + last_tmpx) / 2 > x) {
3776 if (vc > last + 1) // This shouldn't happen.
3780 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3781 // some speedup if rtl_support=false
3782 && (!row->next() || row->next()->par() != row->par());
3783 bool const rtl = (lastrow)
3784 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3785 : false; // If lastrow is false, we don't need to compute
3786 // the value of rtl.
3788 if (row->pos() > last) // Row is empty?
3791 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3792 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3794 else if (vc == row->pos()) {
3796 if (bidi_level(c) % 2 == 1)
3799 c = vis2log(vc - 1);
3800 bool const rtl = (bidi_level(c) % 2 == 1);
3801 if (left_side == rtl) {
3803 boundary = isBoundary(bview->buffer(), row->par(), c);
3807 if (row->pos() <= last && c > last
3808 && row->par()->isNewline(last)) {
3809 if (bidi_level(last) % 2 == 0)
3810 tmpx -= singleWidth(bview, row->par(), last);
3812 tmpx += singleWidth(bview, row->par(), last);
3822 // returns pointer to a specified row
3823 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3828 Row * tmprow = firstrow;
3831 // find the first row of the specified paragraph
3832 while (tmprow->next() && tmprow->par() != par) {
3833 y += tmprow->height();
3834 tmprow = tmprow->next();
3837 // now find the wanted row
3838 while (tmprow->pos() < pos
3840 && tmprow->next()->par() == par
3841 && tmprow->next()->pos() <= pos) {
3842 y += tmprow->height();
3843 tmprow = tmprow->next();
3850 Row * LyXText::getRowNearY(int & y) const
3852 // If possible we should optimize this method. (Lgb)
3853 Row * tmprow = firstrow;
3856 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3857 tmpy += tmprow->height();
3858 tmprow = tmprow->next();
3861 y = tmpy; // return the real y
3866 int LyXText::getDepth() const
3868 return cursor.par()->getDepth();