1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
16 #include "paragraph.h"
17 #include "lyx_gui_misc.h"
19 #include "bufferparams.h"
28 #include "lyxscreen.h"
29 #include "bufferview_funcs.h"
30 #include "BufferView.h"
32 #include "ParagraphParameters.h"
33 #include "undo_funcs.h"
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
53 int const LYX_PAPER_MARGIN = 20;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
60 int LyXText::workWidth(BufferView * bview) const
63 return inset_owner->textWidth(bview);
65 return bview->workWidth();
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
74 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;
255 pos_type LyXText::rowLastPrintable(Row const * row) const
257 pos_type const last = rowLast(row);
258 if (last >= row->pos()
260 && row->next()->par() == row->par()
261 && row->par()->isSeparator(last))
268 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
270 bidi_same_direction = true;
271 if (!lyxrc.rtl_support) {
276 bidi_start = row->pos();
277 bidi_end = rowLastPrintable(row);
279 if (bidi_start > bidi_end) {
284 if (bidi_end + 2 - bidi_start >
285 static_cast<pos_type>(log2vis_list.size())) {
287 (bidi_end + 2 - bidi_start < 500) ?
288 500 : 2 * (bidi_end + 2 - bidi_start);
289 log2vis_list.resize(new_size);
290 vis2log_list.resize(new_size);
291 bidi_levels.resize(new_size);
294 vis2log_list[bidi_end + 1 - bidi_start] = -1;
295 log2vis_list[bidi_end + 1 - bidi_start] = -1;
299 row->par()->getParLanguage(buf->params)->RightToLeft();
303 pos_type const main_body = beginningOfMainBody(buf, row->par());
305 for (pos_type lpos = bidi_start;
306 lpos <= bidi_end; ++lpos) {
307 bool is_space = row->par()->isLineSeparator(lpos);
309 (is_space && lpos + 1 <= bidi_end &&
310 !row->par()->isLineSeparator(lpos + 1) &&
311 !row->par()->isNewline(lpos + 1))
313 LyXFont font = row->par()->getFontSettings(buf->params, pos);
314 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
315 font.number() == LyXFont::ON &&
316 row->par()->getFontSettings(buf->params, lpos - 1).number()
318 font = row->par()->getFontSettings(buf->params, lpos);
323 bool new_rtl = font.isVisibleRightToLeft();
324 bool new_rtl0 = font.isRightToLeft();
327 if (lpos == main_body - 1
328 && row->pos() < main_body - 1
330 new_level = (rtl_par) ? 1 : 0;
331 new_rtl = new_rtl0 = rtl_par;
333 new_level = (new_rtl) ? 1 : 2;
335 new_level = (rtl_par) ? 2 : 0;
337 if (is_space && new_level >= level) {
343 int new_level2 = new_level;
345 if (level == new_level && rtl0 != new_rtl0) {
347 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
348 } else if (level < new_level) {
349 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
350 if (new_level > rtl_par)
351 bidi_same_direction = false;
353 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
356 bidi_levels[lpos - bidi_start] = new_level;
358 while (level > new_level2) {
359 pos_type old_lpos = stack[--level];
360 int delta = lpos - old_lpos - 1;
363 log2vis_list[lpos - bidi_start] += delta;
364 log2vis_list[old_lpos - bidi_start] += delta;
366 while (level < new_level)
367 stack[level++] = lpos;
371 pos_type const old_lpos = stack[--level];
372 int delta = bidi_end - old_lpos;
375 log2vis_list[old_lpos - bidi_start] += delta;
378 pos_type vpos = bidi_start - 1;
379 for (pos_type lpos = bidi_start;
380 lpos <= bidi_end; ++lpos) {
381 vpos += log2vis_list[lpos - bidi_start];
382 vis2log_list[vpos - bidi_start] = lpos;
383 log2vis_list[lpos - bidi_start] = vpos;
388 // This method requires a previous call to ComputeBidiTables()
389 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
392 if (!lyxrc.rtl_support || pos == 0)
395 if (!bidi_InRange(pos - 1)) {
396 /// This can happen if pos is the first char of a row.
397 /// Returning false in this case is incorrect!
401 bool const rtl = bidi_level(pos - 1) % 2;
402 bool const rtl2 = bidi_InRange(pos)
403 ? bidi_level(pos) % 2
404 : par->isRightToLeftPar(buf->params);
409 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
410 pos_type pos, LyXFont const & font) const
412 if (!lyxrc.rtl_support)
413 return false; // This is just for speedup
415 bool const rtl = font.isVisibleRightToLeft();
416 bool const rtl2 = bidi_InRange(pos)
417 ? bidi_level(pos) % 2
418 : par->isRightToLeftPar(buf->params);
422 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
424 // Draw end-of-line marker
425 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
426 int const wid = lyxfont::width('n', font);
427 int const asc = lyxfont::maxAscent(font);
428 int const y = p.yo + p.row->baseline();
432 yp[0] = int(y - 0.875 * asc * 0.75);
433 yp[1] = int(y - 0.500 * asc * 0.75);
434 yp[2] = int(y - 0.125 * asc * 0.75);
436 if (bidi_level(pos) % 2 == 0) {
437 xp[0] = int(p.x + wid * 0.375);
439 xp[2] = int(p.x + wid * 0.375);
441 xp[0] = int(p.x + wid * 0.625);
442 xp[1] = int(p.x + wid);
443 xp[2] = int(p.x + wid * 0.625);
446 p.pain->lines(xp, yp, 3, LColor::eolmarker);
448 yp[0] = int(y - 0.500 * asc * 0.75);
449 yp[1] = int(y - 0.500 * asc * 0.75);
450 yp[2] = int(y - asc * 0.75);
452 if (bidi_level(pos) % 2 == 0) {
454 xp[1] = int(p.x + wid);
455 xp[2] = int(p.x + wid);
457 xp[0] = int(p.x + wid);
462 p.pain->lines(xp, yp, 3, LColor::eolmarker);
468 void LyXText::drawInset(DrawRowParams & p, pos_type const pos)
470 Inset * inset = p.row->par()->getInset(pos);
472 // FIXME: shouldn't happen
477 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
479 inset->update(p.bv, font, false);
480 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
482 if (!need_break_row && !inset_owner
483 && p.bv->text->status() == CHANGED_IN_DRAW) {
484 Row * prev = p.row->previous();
485 if (prev && prev->par() == p.row->par()) {
486 breakAgainOneRow(p.bv, prev);
488 setCursor(p.bv, cursor.par(), cursor.pos());
489 need_break_row = p.row;
494 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
496 if (!lyxrc.mark_foreign_language)
498 if (orig_font.language() == latex_language)
500 if (orig_font.language() == p.bv->buffer()->params.language)
503 int const y = p.yo + p.row->height() - 1;
504 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
508 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
510 pos_type pos = vis2log(vpos);
515 char c = p.row->par()->getChar(pos);
519 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
520 int const width = lyxfont::width(c, font);
523 for (pos_type i = pos-1; i >= 0; --i) {
524 c = p.row->par()->getChar(i);
525 if (!Encodings::IsComposeChar_hebrew(c)) {
526 if (IsPrintableNonspace(c)) {
528 singleWidth(p.bv, p.row->par(), i, c);
530 dx = (c == 'ø' || c == 'ã')
532 : (width2 - width) / 2;
539 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
543 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
545 pos_type pos = vis2log(vpos);
549 char c = p.row->par()->getChar(pos);
550 c = transformChar(c, p.row->par(), pos);
554 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
555 int const width = lyxfont::width(c, font);
558 for (pos_type i = pos-1; i >= 0; --i) {
559 c = p.row->par()->getChar(i);
560 if (!Encodings::IsComposeChar_arabic(c)) {
561 if (IsPrintableNonspace(c)) {
563 singleWidth(p.bv, p.row->par(), i, c);
564 dx = (width2 - width) / 2;
570 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
574 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
575 bool hebrew, bool arabic)
577 pos_type pos = vis2log(vpos);
578 pos_type const last = rowLastPrintable(p.row);
579 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
583 str += p.row->par()->getChar(pos);
585 unsigned char c = str[0];
586 str[0] = transformChar(c, p.row->par(), pos);
590 // collect as much similar chars as we can
591 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
592 char c = p.row->par()->getChar(pos);
594 if (!IsPrintableNonspace(c))
597 if (arabic && Encodings::IsComposeChar_arabic(c))
599 if (hebrew && Encodings::IsComposeChar_hebrew(c))
602 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
609 // Draw text and set the new x position
610 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
611 p.x += lyxfont::width(str, orig_font);
615 void LyXText::draw(DrawRowParams & p, pos_type & vpos)
617 pos_type const pos = vis2log(vpos);
618 Paragraph * par = p.row->par();
620 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
622 float const orig_x = p.x;
624 char const c = par->getChar(pos);
626 if (IsNewlineChar(c)) {
630 } else if (IsInsetChar(c)) {
633 drawForeignMark(p, orig_x, orig_font);
637 // usual characters, no insets
639 // special case languages
640 bool const hebrew = (orig_font.language()->lang() == "hebrew");
642 orig_font.language()->lang() == "arabic" &&
643 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
644 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
646 // draw as many chars as we can
647 if ((!hebrew && !arabic)
648 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
649 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
650 drawChars(p, vpos, true, false);
652 drawHebrewComposeChar(p, vpos);
654 drawArabicComposeChar(p, vpos);
657 drawForeignMark(p, orig_x, orig_font);
659 #ifdef INHERIT_LANGUAGE
661 if ((font.language() == inherit_language) ||
662 (font.language() == ignore_language))
663 lyxerr << "No this shouldn't happen!\n";
669 // Returns the left beginning of the text.
670 // This information cannot be taken from the layouts-objekt, because in
671 // LaTeX the beginning of the text fits in some cases (for example sections)
672 // exactly the label-width.
673 int LyXText::leftMargin(BufferView * bview, Row const * row) const
675 LyXTextClass const & tclass =
676 textclasslist.TextClass(bview->buffer()->params.textclass);
677 LyXLayout const & layout = tclass[row->par()->getLayout()];
679 string parindent = layout.parindent;
681 int x = LYX_PAPER_MARGIN;
683 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
685 // this is the way, LyX handles the LaTeX-Environments.
686 // I have had this idea very late, so it seems to be a
687 // later added hack and this is true
688 if (!row->par()->getDepth()) {
689 if (!row->par()->getLayout()) {
690 // find the previous same level paragraph
691 if (row->par()->previous()) {
692 Paragraph * newpar = row->par()
693 ->depthHook(row->par()->getDepth());
695 tclass[newpar->getLayout()].nextnoindent)
700 // find the next level paragraph
703 row->par()->outerHook();
705 // make a corresponding row. Needed to call LeftMargin()
707 // check wether it is a sufficent paragraph
708 if (newpar && tclass[newpar->getLayout()].isEnvironment())
711 dummyrow.par(newpar);
712 dummyrow.pos(newpar->size());
713 x = leftMargin(bview, &dummyrow);
715 // this is no longer an error, because this function
716 // is used to clear impossible depths after changing
717 // a layout. Since there is always a redo,
718 // LeftMargin() is always called
719 row->par()->params().depth(0);
722 if (newpar && !row->par()->getLayout()) {
723 if (newpar->params().noindent())
726 parindent = tclass[newpar->getLayout()].parindent;
731 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
732 switch (layout.margintype) {
734 if (!layout.leftmargin.empty()) {
735 x += lyxfont::signedWidth(layout.leftmargin,
736 tclass.defaultfont());
738 if (!row->par()->getLabelstring().empty()) {
739 x += lyxfont::signedWidth(layout.labelindent,
741 x += lyxfont::width(row->par()->getLabelstring(),
743 x += lyxfont::width(layout.labelsep, labelfont);
747 x += lyxfont::signedWidth(layout.labelindent, labelfont);
748 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
749 if (!row->par()->getLabelWidthString().empty()) {
750 x += lyxfont::width(row->par()->getLabelWidthString(),
752 x += lyxfont::width(layout.labelsep, labelfont);
757 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
758 / (row->par()->getDepth() + 4);
760 case MARGIN_FIRST_DYNAMIC:
761 if (layout.labeltype == LABEL_MANUAL) {
762 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
763 x += lyxfont::signedWidth(layout.leftmargin,
766 x += lyxfont::signedWidth(layout.labelindent,
769 } else if (row->pos()
770 // Special case to fix problems with
772 || (layout.labeltype == LABEL_STATIC
773 && layout.latextype == LATEX_ENVIRONMENT
774 && ! row->par()->isFirstInSequence())) {
775 x += lyxfont::signedWidth(layout.leftmargin,
777 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
778 && layout.labeltype != LABEL_BIBLIO
779 && layout.labeltype !=
780 LABEL_CENTERED_TOP_ENVIRONMENT) {
781 x += lyxfont::signedWidth(layout.labelindent,
783 x += lyxfont::width(layout.labelsep, labelfont);
784 x += lyxfont::width(row->par()->getLabelstring(),
789 case MARGIN_RIGHT_ADDRESS_BOX:
791 // ok, a terrible hack. The left margin depends on the widest
792 // row in this paragraph. Do not care about footnotes, they
793 // are *NOT* allowed in the LaTeX realisation of this layout.
795 // find the first row of this paragraph
796 Row const * tmprow = row;
797 while (tmprow->previous()
798 && tmprow->previous()->par() == row->par())
799 tmprow = tmprow->previous();
801 int minfill = tmprow->fill();
802 while (tmprow->next() && tmprow->next()->par() == row->par()) {
803 tmprow = tmprow->next();
804 if (tmprow->fill() < minfill)
805 minfill = tmprow->fill();
808 x += lyxfont::signedWidth(layout.leftmargin,
809 tclass.defaultfont());
815 LyXAlignment align; // wrong type
817 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
818 align = layout.align;
820 align = row->par()->params().align();
822 // set the correct parindent
823 if (row->pos() == 0) {
824 if ((layout.labeltype == LABEL_NO_LABEL
825 || layout.labeltype == LABEL_TOP_ENVIRONMENT
826 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
827 || (layout.labeltype == LABEL_STATIC
828 && layout.latextype == LATEX_ENVIRONMENT
829 && ! row->par()->isFirstInSequence()))
830 && align == LYX_ALIGN_BLOCK
831 && !row->par()->params().noindent()
832 && (row->par()->layout ||
833 bview->buffer()->params.paragraph_separation ==
834 BufferParams::PARSEP_INDENT))
835 x += lyxfont::signedWidth(parindent,
836 tclass.defaultfont());
837 else if (layout.labeltype == LABEL_BIBLIO) {
838 // ale970405 Right width for bibitems
839 x += bibitemMaxWidth(bview, tclass.defaultfont());
846 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
848 LyXTextClass const & tclass =
849 textclasslist.TextClass(buf->params.textclass);
850 LyXLayout const & layout = tclass[row->par()->getLayout()];
852 int x = LYX_PAPER_MARGIN
853 + lyxfont::signedWidth(tclass.rightmargin(),
854 tclass.defaultfont());
856 // this is the way, LyX handles the LaTeX-Environments.
857 // I have had this idea very late, so it seems to be a
858 // later added hack and this is true
859 if (row->par()->getDepth()) {
860 // find the next level paragraph
862 Paragraph * newpar = row->par();
865 newpar = newpar->previous();
867 && newpar->getDepth() >= row->par()->getDepth());
869 // make a corresponding row. Needed to call LeftMargin()
871 // check wether it is a sufficent paragraph
873 && tclass[newpar->getLayout()].isEnvironment()) {
875 dummyrow.par(newpar);
877 x = rightMargin(buf, &dummyrow);
879 // this is no longer an error, because this function
880 // is used to clear impossible depths after changing
881 // a layout. Since there is always a redo,
882 // LeftMargin() is always called
883 row->par()->params().depth(0);
887 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
888 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
889 * 4 / (row->par()->getDepth() + 4);
894 int LyXText::labelEnd(BufferView * bview, Row const * row) const
896 if (textclasslist.Style(bview->buffer()->params.textclass,
897 row->par()->getLayout()).margintype
901 tmprow.pos(row->par()->size());
902 return leftMargin(bview, &tmprow); /* just the beginning
905 return 0; /* LabelEnd is only needed, if the
906 layout fills a flushleft
911 // get the next breakpoint in a given paragraph
913 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
915 Paragraph * par = row->par();
920 pos_type const pos = row->pos();
922 // position of the last possible breakpoint
923 // -1 isn't a suitable value, but a flag
924 pos_type last_separator = -1;
925 width -= rightMargin(bview->buffer(), row);
927 pos_type const main_body =
928 beginningOfMainBody(bview->buffer(), par);
929 LyXLayout const & layout =
930 textclasslist.Style(bview->buffer()->params.textclass,
934 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
935 /* special code for right address boxes, only newlines count */
936 while (i < par->size()) {
937 if (par->isNewline(i)) {
939 i = par->size() - 1; // this means break
941 } else if (par->isInset(i) && par->getInset(i)
942 && par->getInset(i)->display()) {
943 par->getInset(i)->display(false);
948 // Last position is an invariant
949 pos_type const last =
951 // this is the usual handling
952 int x = leftMargin(bview, row);
953 bool doitonetime = true;
954 while (doitonetime || ((x < width) && (i < last))) {
956 char const c = par->getChar(i);
957 if (IsNewlineChar(c)) {
959 x = width; // this means break
960 } else if (c == Paragraph::META_INSET &&
963 // check wether a Display() inset is
964 // valid here. if not, change it to
966 if (par->getInset(i)->display() &&
967 (layout.isCommand() ||
968 (layout.labeltype == LABEL_MANUAL
969 && i < beginningOfMainBody(bview->buffer(), par)))) {
970 // display istn't allowd
971 par->getInset(i)->display(false);
972 x += singleWidth(bview, par, i, c);
973 } else if (par->getInset(i)->display() ||
974 par->getInset(i)->needFullRow()) {
975 // So break the line here
979 if (IsLineSeparatorChar(par->getChar(i+1)))
982 last_separator = last; // to avoid extra rows
984 last_separator = i - 1;
985 x = width; // this means break
987 x += singleWidth(bview, par, i, c);
990 if (IsLineSeparatorChar(c))
992 x += singleWidth(bview, par, i, c);
995 if (i == main_body) {
996 x += lyxfont::width(layout.labelsep,
997 getLabelFont(bview->buffer(), par));
998 if (par->isLineSeparator(i - 1))
999 x-= singleWidth(bview, par, i - 1);
1000 int left_margin = labelEnd(bview, row);
1001 if (x < left_margin)
1005 // end of paragraph is always a suitable separator
1006 if (i == last && x < width)
1010 // well, if last_separator is still 0, the line isn't breakable.
1011 // don't care and cut simply at the end
1012 if (last_separator < 0) {
1016 // manual labels cannot be broken in LaTeX, do not care
1017 if (main_body && last_separator < main_body)
1018 last_separator = main_body - 1;
1020 return last_separator;
1024 // returns the minimum space a row needs on the screen in pixel
1025 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1027 if (paper_width < 0)
1031 // get the pure distance
1032 pos_type const last = rowLastPrintable(row);
1034 // special handling of the right address boxes
1035 if (textclasslist.Style(bview->buffer()->params.textclass,
1036 row->par()->getLayout()).margintype
1037 == MARGIN_RIGHT_ADDRESS_BOX)
1039 int const tmpfill = row->fill();
1040 row->fill(0); // the minfill in MarginLeft()
1041 w = leftMargin(bview, row);
1044 w = leftMargin(bview, row);
1046 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1047 row->par()->getLayout());
1048 pos_type const main_body =
1049 beginningOfMainBody(bview->buffer(), row->par());
1050 pos_type i = row->pos();
1053 if (main_body > 0 && i == main_body) {
1054 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1055 if (row->par()->isLineSeparator(i - 1))
1056 w -= singleWidth(bview, row->par(), i - 1);
1057 int left_margin = labelEnd(bview, row);
1058 if (w < left_margin)
1061 w += singleWidth(bview, row->par(), i);
1064 if (main_body > 0 && main_body > last) {
1065 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1066 if (last >= 0 && row->par()->isLineSeparator(last))
1067 w -= singleWidth(bview, row->par(), last);
1068 int const left_margin = labelEnd(bview, row);
1069 if (w < left_margin)
1073 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1078 // returns the minimum space a manual label needs on the screen in pixel
1079 int LyXText::labelFill(BufferView * bview, Row const * row) const
1081 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1082 // -1 because a label ends either with a space that is in the label,
1083 // or with the beginning of a footnote that is outside the label.
1085 // I don't understand this code in depth, but sometimes "last" is
1086 // less than 0 and this causes a crash. This fix seems to work
1087 // correctly, but I bet the real error is elsewhere. The bug is
1088 // triggered when you have an open footnote in a paragraph
1089 // environment with a manual label. (Asger)
1090 if (last < 0) last = 0;
1092 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1097 pos_type i = row->pos();
1099 w += singleWidth(bview, row->par(), i);
1104 if (!row->par()->params().labelWidthString().empty()) {
1105 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1106 getLabelFont(bview->buffer(), row->par())) - w,
1114 // returns the number of separators in the specified row. The separator
1115 // on the very last column doesnt count
1116 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1118 pos_type const last = rowLast(row);
1119 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1121 for (; p < last; ++p) {
1122 if (row->par()->isSeparator(p)) {
1130 // returns the number of hfills in the specified row. The LyX-Hfill is
1131 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1132 // ignored. This is *MUCH* more usefull than not to ignore!
1133 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1135 pos_type const last = rowLast(row);
1136 pos_type first = row->pos();
1137 if (first) { /* hfill *DO* count at the beginning
1139 while (first <= last && row->par()->isHfill(first))
1143 first = max(first, beginningOfMainBody(buf, row->par()));
1145 for (pos_type p = first; p <= last; ++p) {
1146 // last, because the end is ignored!
1147 if (row->par()->isHfill(p)) {
1155 // like NumberOfHfills, but only those in the manual label!
1156 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1158 pos_type last = rowLast(row);
1159 pos_type first = row->pos();
1160 if (first) { /* hfill *DO* count at the beginning
1162 while (first < last && row->par()->isHfill(first))
1166 last = min(last, beginningOfMainBody(buf, row->par()));
1168 for (pos_type p = first; p < last; ++p) {
1169 // last, because the end is ignored!
1170 if (row->par()->isHfill(p)) {
1178 // returns true, if a expansion is needed.
1179 // Rules are given by LaTeX
1180 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1183 // by the way, is it a hfill?
1184 if (!row_ptr->par()->isHfill(pos))
1187 // at the end of a row it does not count
1188 // unless another hfill exists on the line
1189 if (pos >= rowLast(row_ptr)) {
1190 pos_type i = row_ptr->pos();
1191 while (i < pos && !row_ptr->par()->isHfill(i)) {
1199 // at the beginning of a row it does not count, if it is not
1200 // the first row of a paragaph
1201 if (!row_ptr->pos())
1204 // in some labels it does not count
1205 if (textclasslist.Style(buf->params.textclass,
1206 row_ptr->par()->getLayout()).margintype
1208 && pos < beginningOfMainBody(buf, row_ptr->par()))
1211 // if there is anything between the first char of the row and
1212 // the sepcified position that is not a newline and not a hfill,
1213 // the hfill will count, otherwise not
1214 pos_type i = row_ptr->pos();
1215 while (i < pos && (row_ptr->par()->isNewline(i)
1216 || row_ptr->par()->isHfill(i)))
1223 LColor::color LyXText::backgroundColor()
1226 return inset_owner->backgroundColor();
1228 return LColor::background;
1231 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1233 /* get the maximum ascent and the maximum descent */
1236 float layoutasc = 0;
1237 float layoutdesc = 0;
1240 Inset * tmpinset = 0;
1242 /* ok , let us initialize the maxasc and maxdesc value.
1243 * This depends in LaTeX of the font of the last character
1244 * in the paragraph. The hack below is necessary because
1245 * of the possibility of open footnotes */
1247 /* Correction: only the fontsize count. The other properties
1248 are taken from the layoutfont. Nicer on the screen :) */
1249 Paragraph * par = row_ptr->par();
1250 Paragraph * firstpar = row_ptr->par();
1252 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1253 firstpar->getLayout());
1255 // as max get the first character of this row then it can increes but not
1256 // decrees the height. Just some point to start with so we don't have to
1257 // do the assignment below too often.
1258 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1259 LyXFont::FONT_SIZE const tmpsize = font.size();
1260 font = getLayoutFont(bview->buffer(), par);
1261 LyXFont::FONT_SIZE const size = font.size();
1262 font.setSize(tmpsize);
1264 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1266 float spacing_val = 1.0;
1267 if (!row_ptr->par()->params().spacing().isDefault()) {
1268 spacing_val = row_ptr->par()->params().spacing().getValue();
1270 spacing_val = bview->buffer()->params.spacing.getValue();
1272 //lyxerr << "spacing_val = " << spacing_val << endl;
1274 int maxasc = int(lyxfont::maxAscent(font) *
1275 layout.spacing.getValue() *
1277 int maxdesc = int(lyxfont::maxDescent(font) *
1278 layout.spacing.getValue() *
1280 pos_type const pos_end = rowLast(row_ptr);
1284 // Check if any insets are larger
1285 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1286 if (row_ptr->par()->isInset(pos)) {
1287 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1288 tmpinset = row_ptr->par()->getInset(pos);
1290 #if 1 // this is needed for deep update on initialitation
1291 tmpinset->update(bview, tmpfont);
1293 asc = tmpinset->ascent(bview, tmpfont);
1294 desc = tmpinset->descent(bview, tmpfont);
1295 maxwidth += tmpinset->width(bview, tmpfont);
1296 maxasc = max(maxasc, asc);
1297 maxdesc = max(maxdesc, desc);
1300 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1304 // Check if any custom fonts are larger (Asger)
1305 // This is not completely correct, but we can live with the small,
1306 // cosmetic error for now.
1307 LyXFont::FONT_SIZE maxsize =
1308 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1309 if (maxsize > font.size()) {
1310 font.setSize(maxsize);
1312 asc = lyxfont::maxAscent(font);
1313 desc = lyxfont::maxDescent(font);
1320 // This is nicer with box insets:
1324 row_ptr->ascent_of_text(maxasc);
1326 // is it a top line?
1327 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1329 // some parksips VERY EASY IMPLEMENTATION
1330 if (bview->buffer()->params.paragraph_separation ==
1331 BufferParams::PARSEP_SKIP)
1333 if (layout.isParagraph()
1334 && firstpar->getDepth() == 0
1335 && firstpar->previous())
1337 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1338 } else if (firstpar->previous() &&
1339 textclasslist.Style(bview->buffer()->params.textclass,
1340 firstpar->previous()->
1341 getLayout()).isParagraph() &&
1342 firstpar->previous()->getDepth() == 0)
1344 // is it right to use defskip here too? (AS)
1345 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1349 // the paper margins
1350 if (!row_ptr->par()->previous() && bv_owner)
1351 maxasc += LYX_PAPER_MARGIN;
1353 // add the vertical spaces, that the user added
1354 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1356 // do not forget the DTP-lines!
1357 // there height depends on the font of the nearest character
1358 if (firstpar->params().lineTop())
1359 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1362 // and now the pagebreaks
1363 if (firstpar->params().pagebreakTop())
1364 maxasc += 3 * defaultHeight();
1366 // This is special code for the chapter, since the label of this
1367 // layout is printed in an extra row
1368 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1369 && bview->buffer()->params.secnumdepth >= 0)
1371 float spacing_val = 1.0;
1372 if (!row_ptr->par()->params().spacing().isDefault()) {
1373 spacing_val = row_ptr->par()->params().spacing().getValue();
1375 spacing_val = bview->buffer()->params.spacing.getValue();
1378 labeladdon = int(lyxfont::maxDescent(labelfont) *
1379 layout.spacing.getValue() *
1381 + int(lyxfont::maxAscent(labelfont) *
1382 layout.spacing.getValue() *
1386 // special code for the top label
1387 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1388 || layout.labeltype == LABEL_BIBLIO
1389 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1390 && row_ptr->par()->isFirstInSequence()
1391 && !row_ptr->par()->getLabelstring().empty())
1393 float spacing_val = 1.0;
1394 if (!row_ptr->par()->params().spacing().isDefault()) {
1395 spacing_val = row_ptr->par()->params().spacing().getValue();
1397 spacing_val = bview->buffer()->params.spacing.getValue();
1401 (lyxfont::maxAscent(labelfont) *
1402 layout.spacing.getValue() *
1404 +(lyxfont::maxDescent(labelfont) *
1405 layout.spacing.getValue() *
1407 + layout.topsep * defaultHeight()
1408 + layout.labelbottomsep * defaultHeight());
1411 // and now the layout spaces, for example before and after a section,
1412 // or between the items of a itemize or enumerate environment
1414 if (!firstpar->params().pagebreakTop()) {
1415 Paragraph * prev = row_ptr->par()->previous();
1417 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1418 if (prev && prev->getLayout() == firstpar->getLayout() &&
1419 prev->getDepth() == firstpar->getDepth() &&
1420 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1422 layoutasc = (layout.itemsep * defaultHeight());
1423 } else if (row_ptr->previous()) {
1424 tmptop = layout.topsep;
1426 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1427 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1428 row_ptr->previous()->par()->
1429 getLayout()).bottomsep;
1432 layoutasc = (tmptop * defaultHeight());
1433 } else if (row_ptr->par()->params().lineTop()) {
1434 tmptop = layout.topsep;
1437 layoutasc = (tmptop * defaultHeight());
1440 prev = row_ptr->par()->outerHook();
1442 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1443 prev->getLayout()).parsep * defaultHeight());
1445 if (firstpar->previous() &&
1446 firstpar->previous()->getDepth() == 0 &&
1447 firstpar->previous()->getLayout() !=
1448 firstpar->getLayout())
1451 } else if (firstpar->previous()) {
1452 maxasc += int(layout.parsep * defaultHeight());
1458 // is it a bottom line?
1459 if (row_ptr->par() == par
1460 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1462 // the paper margins
1463 if (!par->next() && bv_owner)
1464 maxdesc += LYX_PAPER_MARGIN;
1466 // add the vertical spaces, that the user added
1467 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1469 // do not forget the DTP-lines!
1470 // there height depends on the font of the nearest character
1471 if (firstpar->params().lineBottom())
1472 maxdesc += 2 * lyxfont::ascent('x',
1473 getFont(bview->buffer(),
1475 max(pos_type(0), par->size() - 1)));
1477 // and now the pagebreaks
1478 if (firstpar->params().pagebreakBottom())
1479 maxdesc += 3 * defaultHeight();
1481 // and now the layout spaces, for example before and after
1482 // a section, or between the items of a itemize or enumerate
1484 if (!firstpar->params().pagebreakBottom()
1485 && row_ptr->par()->next()) {
1486 Paragraph * nextpar = row_ptr->par()->next();
1487 Paragraph * comparepar = row_ptr->par();
1491 if (comparepar->getDepth() > nextpar->getDepth()) {
1492 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1493 comparepar->getLayout()).bottomsep * defaultHeight());
1494 comparepar = comparepar->depthHook(nextpar->getDepth());
1495 if (comparepar->getLayout()!= nextpar->getLayout()
1496 || nextpar->getLabelWidthString() !=
1497 comparepar->getLabelWidthString())
1499 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1500 comparepar->getLayout()).bottomsep * defaultHeight());
1502 if (unusual > usual)
1503 layoutdesc = unusual;
1506 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1508 if (comparepar->getLayout()!= nextpar->getLayout()
1509 || nextpar->getLabelWidthString() !=
1510 comparepar->getLabelWidthString())
1511 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1512 comparepar->getLayout()).bottomsep * defaultHeight());
1517 // incalculate the layout spaces
1518 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1519 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1521 // calculate the new height of the text
1522 height -= row_ptr->height();
1524 row_ptr->height(maxasc + maxdesc + labeladdon);
1525 row_ptr->baseline(maxasc + labeladdon);
1527 height += row_ptr->height();
1529 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1531 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1533 row_ptr->width(int(maxwidth + x));
1536 width = max(0,workWidth(bview));
1538 if (r->width() > width)
1546 /* Appends the implicit specified paragraph behind the specified row,
1547 * start at the implicit given position */
1548 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1550 bool not_ready = true;
1552 // The last character position of a paragraph is an invariant so we can
1553 // safely get it here. (Asger)
1554 pos_type const lastposition = row->par()->size();
1556 // Get the next breakpoint
1557 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1561 // Insert the new row
1562 if (z < lastposition) {
1564 insertRow(row, row->par(), z);
1571 // Set the dimensions of the row
1572 // fixed fill setting now by calling inset->update() in
1573 // SingleWidth when needed!
1574 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1575 setHeightOfRow(bview, tmprow);
1577 } while (not_ready);
1581 void LyXText::breakAgain(BufferView * bview, Row * row) const
1583 bool not_ready = true;
1586 // get the next breakpoint
1587 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1590 if (z < row->par()->size()) {
1591 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1594 insertRow(row, row->par(), z);
1600 if (row->pos() == z)
1601 not_ready = false; // the rest will not change
1607 /* if there are some rows too much, delete them */
1608 /* only if you broke the whole paragraph! */
1609 Row * tmprow2 = row;
1610 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1611 tmprow2 = tmprow2->next();
1613 while (tmprow2 != row) {
1614 tmprow2 = tmprow2->previous();
1615 removeRow(tmprow2->next());
1620 /* set the dimensions of the row */
1621 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1622 setHeightOfRow(bview, tmprow);
1623 } while (not_ready);
1627 // this is just a little changed version of break again
1628 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1630 // get the next breakpoint
1631 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1634 if (z < row->par()->size()) {
1636 || (row->next() && row->next()->par() != row->par())) {
1637 /* insert a new row */
1639 insertRow(row, row->par(), z);
1645 if (row->pos() != z)
1649 // if there are some rows too much, delete them
1650 // only if you broke the whole paragraph!
1651 Row * tmprow2 = row;
1652 while (tmprow2->next()
1653 && tmprow2->next()->par() == row->par()) {
1654 tmprow2 = tmprow2->next();
1656 while (tmprow2 != row) {
1657 tmprow2 = tmprow2->previous();
1658 removeRow(tmprow2->next());
1662 // set the dimensions of the row
1663 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1664 setHeightOfRow(bview, tmprow);
1668 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1670 LyXLayout const & layout =
1671 textclasslist.Style(bview->buffer()->params.textclass,
1672 cursor.par()->getLayout());
1674 // this is only allowed, if the current paragraph is not empty or caption
1675 if ((cursor.par()->size() <= 0)
1676 && layout.labeltype!= LABEL_SENSITIVE)
1679 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1681 // Always break behind a space
1683 // It is better to erase the space (Dekel)
1684 if (cursor.pos() < cursor.par()->size()
1685 && cursor.par()->isLineSeparator(cursor.pos()))
1686 cursor.par()->erase(cursor.pos());
1687 // cursor.pos(cursor.pos() + 1);
1689 // break the paragraph
1693 keep_layout = layout.isEnvironment();
1694 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1697 // well this is the caption hack since one caption is really enough
1698 if (layout.labeltype == LABEL_SENSITIVE) {
1700 // set to standard-layout
1701 cursor.par()->setLayout(0);
1703 // set to standard-layout
1704 cursor.par()->next()->setLayout(0);
1707 /* if the cursor is at the beginning of a row without prior newline,
1709 * This touches only the screen-update. Otherwise we would may have
1710 * an empty row on the screen */
1711 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1712 && cursor.row()->pos() == cursor.pos()) {
1716 status(bview, LyXText::NEED_MORE_REFRESH);
1717 refresh_row = cursor.row();
1718 refresh_y = cursor.y() - cursor.row()->baseline();
1720 // Do not forget the special right address boxes
1721 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1722 while (refresh_row->previous() &&
1723 refresh_row->previous()->par() == refresh_row->par()) {
1724 refresh_row = refresh_row->previous();
1725 refresh_y -= refresh_row->height();
1728 removeParagraph(cursor.row());
1730 // set the dimensions of the cursor row
1731 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1733 setHeightOfRow(bview, cursor.row());
1735 while (cursor.par()->next()->size()
1736 && cursor.par()->next()->isNewline(0))
1737 cursor.par()->next()->erase(0);
1739 insertParagraph(bview, cursor.par()->next(), cursor.row());
1741 updateCounters(bview, cursor.row()->previous());
1743 /* This check is necessary. Otherwise the new empty paragraph will
1744 * be deleted automatically. And it is more friendly for the user! */
1746 setCursor(bview, cursor.par()->next(), 0);
1748 setCursor(bview, cursor.par(), 0);
1750 if (cursor.row()->next())
1751 breakAgain(bview, cursor.row()->next());
1757 // Just a macro to make some thing easier.
1758 void LyXText::redoParagraph(BufferView * bview) const
1761 redoParagraphs(bview, cursor, cursor.par()->next());
1762 setCursorIntern(bview, cursor.par(), cursor.pos());
1766 /* insert a character, moves all the following breaks in the
1767 * same Paragraph one to the right and make a rebreak */
1768 void LyXText::insertChar(BufferView * bview, char c)
1770 setUndo(bview, Undo::INSERT,
1771 cursor.par(), cursor.par()->next());
1773 // When the free-spacing option is set for the current layout,
1774 // disable the double-space checking
1776 bool const freeSpacing =
1777 textclasslist.Style(bview->buffer()->params.textclass,
1778 cursor.row()->par()->getLayout()).free_spacing ||
1779 cursor.row()->par()->isFreeSpacing();
1782 if (lyxrc.auto_number) {
1783 static string const number_operators = "+-/*";
1784 static string const number_unary_operators = "+-";
1785 static string const number_seperators = ".,:";
1787 if (current_font.number() == LyXFont::ON) {
1788 if (!IsDigit(c) && !contains(number_operators, c) &&
1789 !(contains(number_seperators, c) &&
1790 cursor.pos() >= 1 &&
1791 cursor.pos() < cursor.par()->size() &&
1792 getFont(bview->buffer(),
1794 cursor.pos()).number() == LyXFont::ON &&
1795 getFont(bview->buffer(),
1797 cursor.pos() - 1).number() == LyXFont::ON)
1799 number(bview); // Set current_font.number to OFF
1800 } else if (IsDigit(c) &&
1801 real_current_font.isVisibleRightToLeft()) {
1802 number(bview); // Set current_font.number to ON
1804 if (cursor.pos() > 0) {
1805 char const c = cursor.par()->getChar(cursor.pos() - 1);
1806 if (contains(number_unary_operators, c) &&
1807 (cursor.pos() == 1 ||
1808 cursor.par()->isSeparator(cursor.pos() - 2) ||
1809 cursor.par()->isNewline(cursor.pos() - 2) )
1811 setCharFont(bview->buffer(),
1815 } else if (contains(number_seperators, c) &&
1816 cursor.pos() >= 2 &&
1817 getFont(bview->buffer(),
1819 cursor.pos() - 2).number() == LyXFont::ON) {
1820 setCharFont(bview->buffer(),
1830 /* First check, if there will be two blanks together or a blank at
1831 the beginning of a paragraph.
1832 I decided to handle blanks like normal characters, the main
1833 difference are the special checks when calculating the row.fill
1834 (blank does not count at the end of a row) and the check here */
1836 // The bug is triggered when we type in a description environment:
1837 // The current_font is not changed when we go from label to main text
1838 // and it should (along with realtmpfont) when we type the space.
1839 // CHECK There is a bug here! (Asger)
1841 LyXFont realtmpfont = real_current_font;
1842 LyXFont rawtmpfont = current_font; /* store the current font.
1843 * This is because of the use
1844 * of cursor movements. The moving
1845 * cursor would refresh the
1848 // Get the font that is used to calculate the baselineskip
1849 pos_type const lastpos = cursor.par()->size();
1850 LyXFont rawparfont =
1851 cursor.par()->getFontSettings(bview->buffer()->params,
1854 bool jumped_over_space = false;
1856 if (!freeSpacing && IsLineSeparatorChar(c)) {
1857 if ((cursor.pos() > 0
1858 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1859 || (cursor.pos() > 0
1860 && cursor.par()->isNewline(cursor.pos() - 1))
1861 || (cursor.pos() == 0)) {
1862 static bool sent_space_message = false;
1863 if (!sent_space_message) {
1864 if (cursor.pos() == 0)
1865 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1867 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1868 sent_space_message = true;
1873 } else if (IsNewlineChar(c)) {
1874 if (cursor.par() == cursor.par()
1875 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1879 /* No newline at first position
1880 * of a paragraph or behind labels.
1881 * TeX does not allow that. */
1883 if (cursor.pos() < cursor.par()->size() &&
1884 cursor.par()->isLineSeparator(cursor.pos()))
1885 // newline always after a blank!
1887 cursor.row()->fill(-1); // to force a new break
1890 // the display inset stuff
1891 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1892 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1893 if (inset && (inset->display() || inset->needFullRow())) {
1894 // force a new break
1895 cursor.row()->fill(-1); // to force a new break
1899 // get the cursor row fist
1900 Row * row = cursor.row();
1901 int y = cursor.y() - row->baseline();
1902 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1903 * already insertet the character */
1904 cursor.par()->insertChar(cursor.pos(), c);
1905 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1907 if (!jumped_over_space) {
1908 // refresh the positions
1910 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1911 tmprow = tmprow->next();
1912 tmprow->pos(tmprow->pos() + 1);
1916 // Is there a break one row above
1917 if ((cursor.par()->isLineSeparator(cursor.pos())
1918 || cursor.par()->isNewline(cursor.pos())
1919 || cursor.row()->fill() == -1)
1920 && row->previous() && row->previous()->par() == row->par()) {
1921 pos_type z = nextBreakPoint(bview,
1924 if (z >= row->pos()) {
1927 // set the dimensions of the row above
1928 row->previous()->fill(fill(bview,
1932 setHeightOfRow(bview, row->previous());
1934 y -= row->previous()->height();
1936 refresh_row = row->previous();
1937 status(bview, LyXText::NEED_MORE_REFRESH);
1939 breakAgainOneRow(bview, row);
1941 current_font = rawtmpfont;
1942 real_current_font = realtmpfont;
1943 setCursor(bview, cursor.par(), cursor.pos() + 1,
1944 false, cursor.boundary());
1945 // cursor MUST be in row now.
1947 if (row->next() && row->next()->par() == row->par())
1948 need_break_row = row->next();
1952 // check, wether the last characters font has changed.
1953 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1954 && rawparfont != rawtmpfont)
1955 redoHeightOfParagraph(bview, cursor);
1962 // recalculate the fill of the row
1963 if (row->fill() >= 0) /* needed because a newline
1964 * will set fill to -1. Otherwise
1965 * we would not get a rebreak! */
1966 row->fill(fill(bview, row, workWidth(bview)));
1967 if (row->fill() < 0) {
1970 refresh_x = cursor.x();
1971 refresh_pos = cursor.pos();
1972 status(bview, LyXText::NEED_MORE_REFRESH);
1973 breakAgainOneRow(bview, row);
1974 // will the cursor be in another row now?
1975 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1976 if (row->next() && row->next()->par() == row->par())
1977 // this should always be true
1979 breakAgainOneRow(bview, row);
1981 current_font = rawtmpfont;
1982 real_current_font = realtmpfont;
1984 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1986 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1987 != cursor.boundary())
1988 setCursor(bview, cursor.par(), cursor.pos(), false,
1989 !cursor.boundary());
1990 if (row->next() && row->next()->par() == row->par())
1991 need_break_row = row->next();
1996 refresh_x = cursor.x();
1998 refresh_pos = cursor.pos();
2000 int const tmpheight = row->height();
2001 setHeightOfRow(bview, row);
2002 if (tmpheight == row->height())
2003 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2005 status(bview, LyXText::NEED_MORE_REFRESH);
2007 current_font = rawtmpfont;
2008 real_current_font = realtmpfont;
2009 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2013 // check, wether the last characters font has changed.
2014 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2015 && rawparfont != rawtmpfont) {
2016 redoHeightOfParagraph(bview, cursor);
2018 // now the special right address boxes
2019 if (textclasslist.Style(bview->buffer()->params.textclass,
2020 cursor.par()->getLayout()).margintype
2021 == MARGIN_RIGHT_ADDRESS_BOX) {
2022 redoDrawingOfParagraph(bview, cursor);
2030 void LyXText::charInserted()
2032 // Here we could call FinishUndo for every 20 characters inserted.
2033 // This is from my experience how emacs does it.
2034 static unsigned int counter;
2044 void LyXText::prepareToPrint(BufferView * bview,
2045 Row * row, float & x,
2046 float & fill_separator,
2048 float & fill_label_hfill,
2054 float w = row->fill();
2056 fill_label_hfill = 0;
2058 fill_label_hfill = 0;
2061 row->par()->isRightToLeftPar(bview->buffer()->params);
2063 x = (workWidth(bview) > 0)
2064 ? rightMargin(bview->buffer(), row) : 0;
2066 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2068 // is there a manual margin with a manual label
2069 if (textclasslist.Style(bview->buffer()->params.textclass,
2070 row->par()->getLayout()).margintype == MARGIN_MANUAL
2071 && textclasslist.Style(bview->buffer()->params.textclass,
2072 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2074 /* one more since labels are left aligned */
2075 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2076 if (nlh && !row->par()->getLabelWidthString().empty()) {
2077 fill_label_hfill = labelFill(bview, row) / nlh;
2081 // are there any hfills in the row?
2082 float const nh = numberOfHfills(bview->buffer(), row);
2086 fill_hfill = w / nh;
2088 // is it block, flushleft or flushright?
2089 // set x how you need it
2091 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2092 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2094 align = row->par()->params().align();
2097 // center displayed insets
2099 if (row->par()->isInset(row->pos())
2100 && (inset=row->par()->getInset(row->pos()))
2101 && (inset->display())) // || (inset->scroll() < 0)))
2102 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2103 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2106 case LYX_ALIGN_BLOCK:
2107 ns = numberOfSeparators(bview->buffer(), row);
2108 if (ns && row->next() && row->next()->par() == row->par() &&
2109 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2110 && !(row->next()->par()->isInset(row->next()->pos())
2111 && row->next()->par()->getInset(row->next()->pos())
2112 && row->next()->par()->getInset(row->next()->pos())->display())
2115 fill_separator = w / ns;
2116 } else if (is_rtl) {
2120 case LYX_ALIGN_RIGHT:
2123 case LYX_ALIGN_CENTER:
2131 computeBidiTables(bview->buffer(), row);
2133 pos_type main_body =
2134 beginningOfMainBody(bview->buffer(), row->par());
2135 pos_type last = rowLast(row);
2137 if (main_body > 0 &&
2138 (main_body-1 > last ||
2139 !row->par()->isLineSeparator(main_body-1))) {
2140 LyXLayout const & layout =
2141 textclasslist.Style(bview->buffer()->params.textclass,
2142 row->par()->getLayout());
2143 x += lyxfont::width(layout.labelsep,
2144 getLabelFont(bview->buffer(), row->par()));
2145 if (main_body-1 <= last)
2146 x += fill_label_hfill;
2151 /* important for the screen */
2154 /* the cursor set functions have a special mechanism. When they
2155 * realize, that you left an empty paragraph, they will delete it.
2156 * They also delete the corresponding row */
2158 void LyXText::cursorRightOneWord(BufferView * bview) const
2160 // treat floats, HFills and Insets as words
2161 LyXCursor tmpcursor = cursor;
2162 // CHECK See comment on top of text.C
2164 if (tmpcursor.pos() == tmpcursor.par()->size()
2165 && tmpcursor.par()->next()) {
2166 tmpcursor.par(tmpcursor.par()->next());
2171 // Skip through initial nonword stuff.
2172 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2173 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2174 // printf("Current pos1 %d", tmpcursor.pos()) ;
2175 tmpcursor.pos(tmpcursor.pos() + 1);
2178 // Advance through word.
2179 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2180 tmpcursor.par()->isWord( tmpcursor.pos())) {
2181 // printf("Current pos2 %d", tmpcursor.pos()) ;
2182 tmpcursor.pos(tmpcursor.pos() + 1);
2186 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2190 void LyXText::cursorTab(BufferView * bview) const
2192 LyXCursor tmpcursor = cursor;
2193 while (tmpcursor.pos() < tmpcursor.par()->size()
2194 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2195 tmpcursor.pos(tmpcursor.pos() + 1);
2197 if (tmpcursor.pos() == tmpcursor.par()->size()){
2198 if (tmpcursor.par()->next()) {
2199 tmpcursor.par(tmpcursor.par()->next());
2203 tmpcursor.pos(tmpcursor.pos() + 1);
2204 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2208 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2209 of prior word, not to end of next prior word. */
2211 void LyXText::cursorLeftOneWord(BufferView * bview) const
2213 LyXCursor tmpcursor = cursor;
2214 cursorLeftOneWord(tmpcursor);
2215 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2218 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2220 // treat HFills, floats and Insets as words
2223 && (cur.par()->isSeparator(cur.pos() - 1)
2224 || cur.par()->isKomma(cur.pos() - 1))
2225 && !(cur.par()->isHfill(cur.pos() - 1)
2226 || cur.par()->isInset(cur.pos() - 1)))
2227 cur.pos(cur.pos() - 1);
2230 && (cur.par()->isInset(cur.pos() - 1)
2231 || cur.par()->isHfill(cur.pos() - 1))) {
2232 cur.pos(cur.pos() - 1);
2233 } else if (!cur.pos()) {
2234 if (cur.par()->previous()){
2235 cur.par(cur.par()->previous());
2236 cur.pos(cur.par()->size());
2238 } else { // Here, cur != 0
2239 while (cur.pos() > 0 &&
2240 cur.par()->isWord(cur.pos()-1) )
2241 cur.pos(cur.pos() - 1);
2245 /* -------> Select current word. This depends on behaviour of
2246 CursorLeftOneWord(), so it is patched as well. */
2247 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2248 word_location const loc) const
2250 // first put the cursor where we wana start to select the word
2253 case WHOLE_WORD_STRICT:
2254 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2255 || cursor.par()->isSeparator(cursor.pos())
2256 || cursor.par()->isKomma(cursor.pos())
2257 || cursor.par()->isSeparator(cursor.pos() -1)
2258 || cursor.par()->isKomma(cursor.pos() -1)) {
2262 // no break here, we go to the next
2265 // Move cursor to the beginning, when not already there.
2266 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2267 && !from.par()->isKomma(from.pos() - 1))
2268 cursorLeftOneWord(from);
2271 // always move the cursor to the beginning of previous word
2272 cursorLeftOneWord(from);
2275 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2281 while (to.pos() < to.par()->size()
2282 && !to.par()->isSeparator(to.pos())
2283 && !to.par()->isKomma(to.pos())
2284 && !to.par()->isHfill(to.pos()) )
2286 to.pos(to.pos() + 1);
2291 void LyXText::selectWord(BufferView * bview, word_location const loc)
2295 getWord(from, to, loc);
2297 setCursor(bview, from.par(), from.pos());
2300 selection.cursor = cursor;
2301 setCursor(bview, to.par(), to.pos() );
2302 setSelection(bview);
2306 /* -------> Select the word currently under the cursor when no
2307 selection is currently set */
2308 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2309 word_location const loc)
2311 if (!selection.set()) {
2312 selectWord(bview, loc);
2313 return selection.set();
2319 // This function is only used by the spellchecker for NextWord().
2320 // It doesn't handle LYX_ACCENTs and probably never will.
2321 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2322 float & value) const
2324 if (the_locking_inset) {
2325 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2327 value += float(cursor.y())/float(height);
2330 #warning Dekel please have a look on this one RTL? (Jug)
2332 // we have to go on checking so move cusor to the right
2333 if (cursor.pos() == cursor.par()->size()) {
2334 if (!cursor.par()->next())
2336 cursor.par(cursor.par()->next());
2339 cursor.pos(cursor.pos() + 1);
2341 Paragraph * tmppar = cursor.par();
2343 // If this is not the very first word, skip rest of
2344 // current word because we are probably in the middle
2345 // of a word if there is text here.
2346 if (cursor.pos() || cursor.par()->previous()) {
2347 while (cursor.pos() < cursor.par()->size()
2348 && cursor.par()->isLetter(cursor.pos()))
2349 cursor.pos(cursor.pos() + 1);
2352 // Now, skip until we have real text (will jump paragraphs)
2353 while ((cursor.par()->size() > cursor.pos()
2354 && (!cursor.par()->isLetter(cursor.pos()))
2355 && (!cursor.par()->isInset(cursor.pos()) ||
2356 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2357 || (cursor.par()->size() == cursor.pos()
2358 && cursor.par()->next()))
2360 if (cursor.pos() == cursor.par()->size()) {
2361 cursor.par(cursor.par()->next());
2364 cursor.pos(cursor.pos() + 1);
2367 // now check if we hit an inset so it has to be a inset containing text!
2368 if (cursor.pos() < cursor.par()->size() &&
2369 cursor.par()->isInset(cursor.pos()))
2372 cursor.par()->getInset(cursor.pos())->edit(bview);
2373 // now call us again to do the above trick
2374 // but obviously we have to start from down below ;)
2375 return bview->text->selectNextWordToSpellcheck(bview, value);
2378 // Update the value if we changed paragraphs
2379 if (cursor.par() != tmppar){
2380 setCursor(bview, cursor.par(), cursor.pos());
2381 value = float(cursor.y())/float(height);
2384 // Start the selection from here
2385 selection.cursor = cursor;
2387 // and find the end of the word (insets like optional hyphens
2388 // and ligature break are part of a word)
2389 while (cursor.pos() < cursor.par()->size()
2390 && (cursor.par()->isLetter(cursor.pos())))
2391 cursor.pos(cursor.pos() + 1);
2393 // Finally, we copy the word to a string and return it
2395 if (selection.cursor.pos() < cursor.pos()) {
2397 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2398 if (!cursor.par()->isInset(i))
2399 str += cursor.par()->getChar(i);
2406 // This one is also only for the spellchecker
2407 void LyXText::selectSelectedWord(BufferView * bview)
2409 if (the_locking_inset) {
2410 the_locking_inset->selectSelectedWord(bview);
2413 // move cursor to the beginning
2414 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2416 // set the sel cursor
2417 selection.cursor = cursor;
2419 // now find the end of the word
2420 while (cursor.pos() < cursor.par()->size()
2421 && (cursor.par()->isLetter(cursor.pos())))
2422 cursor.pos(cursor.pos() + 1);
2424 setCursor(bview, cursor.par(), cursor.pos());
2426 // finally set the selection
2427 setSelection(bview);
2431 /* -------> Delete from cursor up to the end of the current or next word. */
2432 void LyXText::deleteWordForward(BufferView * bview)
2434 if (!cursor.par()->size())
2437 LyXCursor tmpcursor = cursor;
2438 tmpcursor.row(0); //Â ??
2439 selection.set(true); // to avoid deletion
2440 cursorRightOneWord(bview);
2441 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2442 selection.cursor = cursor;
2444 setSelection(bview);
2446 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2447 cutSelection(bview, true, false);
2452 /* -------> Delete from cursor to start of current or prior word. */
2453 void LyXText::deleteWordBackward(BufferView * bview)
2455 if (!cursor.par()->size())
2458 LyXCursor tmpcursor = cursor;
2459 tmpcursor.row(0); // ??
2460 selection.set(true); // to avoid deletion
2461 cursorLeftOneWord(bview);
2462 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2463 selection.cursor = cursor;
2465 setSelection(bview);
2466 cutSelection(bview, true, false);
2471 /* -------> Kill to end of line. */
2472 void LyXText::deleteLineForward(BufferView * bview)
2474 if (!cursor.par()->size())
2475 // Paragraph is empty, so we just go to the right
2478 LyXCursor tmpcursor = cursor;
2479 // We can't store the row over a regular setCursor
2480 // so we set it to 0 and reset it afterwards.
2481 tmpcursor.row(0); //Â ??
2482 selection.set(true); // to avoid deletion
2484 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2485 selection.cursor = cursor;
2487 setSelection(bview);
2488 // What is this test for ??? (JMarc)
2489 if (!selection.set()) {
2490 deleteWordForward(bview);
2492 cutSelection(bview, true, false);
2498 // Change the case of a word at cursor position.
2499 // This function directly manipulates Paragraph::text because there
2500 // is no Paragraph::SetChar currently. I did what I could to ensure
2501 // that it is correct. I guess part of it should be moved to
2502 // Paragraph, but it will have to change for 1.1 anyway. At least
2503 // it does not access outside of the allocated array as the older
2504 // version did. (JMarc)
2505 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2510 if (selection.set()) {
2511 from = selection.start;
2514 getWord(from, to, PARTIAL_WORD);
2515 setCursor(bview, to.par(), to.pos() + 1);
2518 changeRegionCase(bview, from, to, action);
2522 void LyXText::changeRegionCase(BufferView * bview,
2523 LyXCursor const & from,
2524 LyXCursor const & to,
2525 LyXText::TextCase action)
2527 lyx::Assert(from <= to);
2529 setUndo(bview, Undo::FINISH,
2530 from.par(), to.par()->next());
2532 pos_type pos = from.pos();
2533 Paragraph * par = from.par();
2535 while (par && (pos != to.pos() || par != to.par())) {
2536 unsigned char c = par->getChar(pos);
2537 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2539 case text_lowercase:
2542 case text_capitalization:
2544 action = text_lowercase;
2546 case text_uppercase:
2551 par->setChar(pos, c);
2552 checkParagraph(bview, par, pos);
2555 if (pos == par->size()) {
2560 if (to.row() != from.row()) {
2561 refresh_y = from.y() - from.row()->baseline();
2562 refresh_row = from.row();
2563 status(bview, LyXText::NEED_MORE_REFRESH);
2568 void LyXText::transposeChars(BufferView & bview)
2570 Paragraph * tmppar = cursor.par();
2572 setUndo(&bview, Undo::FINISH,
2573 tmppar, tmppar->next());
2575 pos_type tmppos = cursor.pos();
2577 // First decide if it is possible to transpose at all
2579 // We are at the beginning of a paragraph.
2580 if (tmppos == 0) return;
2582 // We are at the end of a paragraph.
2583 if (tmppos == tmppar->size() - 1) return;
2585 unsigned char c1 = tmppar->getChar(tmppos);
2586 unsigned char c2 = tmppar->getChar(tmppos - 1);
2588 if (c1 != Paragraph::META_INSET
2589 && c2 != Paragraph::META_INSET) {
2590 tmppar->setChar(tmppos, c2);
2591 tmppar->setChar(tmppos - 1, c1);
2593 // We should have an implementation that handles insets
2594 // as well, but that will have to come later. (Lgb)
2595 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2599 void LyXText::Delete(BufferView * bview)
2601 // this is a very easy implementation
2603 LyXCursor old_cursor = cursor;
2604 int const old_cur_par_id = old_cursor.par()->id();
2605 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2606 old_cursor.par()->previous()->id() : 0;
2608 // just move to the right
2611 // CHECK Look at the comment here.
2612 // This check is not very good...
2613 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2614 // and that can very well delete the par or par->previous in
2615 // old_cursor. Will a solution where we compare paragraph id's
2617 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2618 == old_cur_par_prev_id
2619 && cursor.par()->id() != old_cur_par_id) {
2620 // delete-empty-paragraph-mechanism has done it
2624 // if you had success make a backspace
2625 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2626 LyXCursor tmpcursor = cursor;
2627 // to make sure undo gets the right cursor position
2628 cursor = old_cursor;
2629 setUndo(bview, Undo::DELETE,
2630 cursor.par(), cursor.par()->next());
2637 void LyXText::backspace(BufferView * bview)
2639 // Get the font that is used to calculate the baselineskip
2640 pos_type lastpos = cursor.par()->size();
2641 LyXFont rawparfont =
2642 cursor.par()->getFontSettings(bview->buffer()->params,
2645 if (cursor.pos() == 0) {
2646 // The cursor is at the beginning of a paragraph,
2647 // so the the backspace will collapse two paragraphs into one.
2649 // we may paste some paragraphs
2651 // is it an empty paragraph?
2654 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2655 // This is an empty paragraph and we delete it just by moving the cursor one step
2656 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2657 // of the paragraph.
2659 if (cursor.par()->previous()) {
2660 Paragraph * tmppar = cursor.par()->previous();
2661 if (cursor.par()->getLayout() == tmppar->getLayout()
2662 && cursor.par()->getAlign() == tmppar->getAlign()) {
2663 // Inherit bottom DTD from the paragraph below.
2664 // (the one we are deleting)
2665 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2666 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2667 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2672 // the layout things can change the height of a row !
2673 int const tmpheight = cursor.row()->height();
2674 setHeightOfRow(bview, cursor.row());
2675 if (cursor.row()->height() != tmpheight) {
2676 refresh_y = cursor.y() - cursor.row()->baseline();
2677 refresh_row = cursor.row();
2678 status(bview, LyXText::NEED_MORE_REFRESH);
2684 if (cursor.par()->previous()) {
2685 setUndo(bview, Undo::DELETE,
2686 cursor.par()->previous(), cursor.par()->next());
2689 Paragraph * tmppar = cursor.par();
2690 Row * tmprow = cursor.row();
2692 // We used to do cursorLeftIntern() here, but it is
2693 // not a good idea since it triggers the auto-delete
2694 // mechanism. So we do a cursorLeftIntern()-lite,
2695 // without the dreaded mechanism. (JMarc)
2696 if (cursor.par()->previous()) {
2697 // steps into the above paragraph.
2698 setCursorIntern(bview, cursor.par()->previous(),
2699 cursor.par()->previous()->size(),
2703 /* Pasting is not allowed, if the paragraphs have different
2704 layout. I think it is a real bug of all other
2705 word processors to allow it. It confuses the user.
2706 Even so with a footnote paragraph and a non-footnote
2707 paragraph. I will not allow pasting in this case,
2708 because the user would be confused if the footnote behaves
2709 different wether it is open or closed.
2711 Correction: Pasting is always allowed with standard-layout
2713 if (cursor.par() != tmppar
2714 && (cursor.par()->getLayout() == tmppar->getLayout()
2715 || tmppar->getLayout() == 0 /*standard*/)
2716 && cursor.par()->getAlign() == tmppar->getAlign())
2718 removeParagraph(tmprow);
2720 cursor.par()->pasteParagraph(bview->buffer()->params);
2722 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2723 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2724 // strangely enough it seems that commenting out the line above removes
2725 // most or all of the segfaults. I will however also try to move the
2726 // two Remove... lines in front of the PasteParagraph too.
2729 cursor.pos(cursor.pos() - 1);
2731 status(bview, LyXText::NEED_MORE_REFRESH);
2732 refresh_row = cursor.row();
2733 refresh_y = cursor.y() - cursor.row()->baseline();
2735 // remove the lost paragraph
2736 // This one is not safe, since the paragraph that the tmprow and the
2737 // following rows belong to has been deleted by the PasteParagraph
2738 // above. The question is... could this be moved in front of the
2740 //RemoveParagraph(tmprow);
2741 //RemoveRow(tmprow);
2743 // This rebuilds the rows.
2744 appendParagraph(bview, cursor.row());
2745 updateCounters(bview, cursor.row());
2747 // the row may have changed, block, hfills etc.
2748 setCursor(bview, cursor.par(), cursor.pos(), false);
2751 /* this is the code for a normal backspace, not pasting
2753 setUndo(bview, Undo::DELETE,
2754 cursor.par(), cursor.par()->next());
2755 // We used to do cursorLeftIntern() here, but it is
2756 // not a good idea since it triggers the auto-delete
2757 // mechanism. So we do a cursorLeftIntern()-lite,
2758 // without the dreaded mechanism. (JMarc)
2759 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2760 false, cursor.boundary());
2762 // some insets are undeletable here
2763 if (cursor.par()->isInset(cursor.pos())) {
2764 if (!cursor.par()->getInset(cursor.pos())->deletable())
2766 // force complete redo when erasing display insets
2767 // this is a cruel method but safe..... Matthias
2768 if (cursor.par()->getInset(cursor.pos())->display() ||
2769 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2770 cursor.par()->erase(cursor.pos());
2771 redoParagraph(bview);
2776 Row * row = cursor.row();
2777 int y = cursor.y() - row->baseline();
2779 /* remember that a space at the end of a row doesnt count
2780 * when calculating the fill */
2781 if (cursor.pos() < rowLast(row) ||
2782 !cursor.par()->isLineSeparator(cursor.pos())) {
2783 row->fill(row->fill() + singleWidth(bview,
2788 /* some special code when deleting a newline. This is similar
2789 * to the behavior when pasting paragraphs */
2790 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2791 cursor.par()->erase(cursor.pos());
2792 // refresh the positions
2794 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2795 tmprow = tmprow->next();
2796 tmprow->pos(tmprow->pos() - 1);
2798 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2799 cursor.pos(cursor.pos() - 1);
2801 if (cursor.pos() < cursor.par()->size()
2802 && !cursor.par()->isSeparator(cursor.pos())) {
2803 cursor.par()->insertChar(cursor.pos(), ' ');
2804 setCharFont(bview->buffer(), cursor.par(),
2805 cursor.pos(), current_font);
2806 // refresh the positions
2808 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2809 tmprow = tmprow->next();
2810 tmprow->pos(tmprow->pos() + 1);
2814 cursor.par()->erase(cursor.pos());
2816 // refresh the positions
2818 while (tmprow->next()
2819 && tmprow->next()->par() == row->par()) {
2820 tmprow = tmprow->next();
2821 tmprow->pos(tmprow->pos() - 1);
2824 // delete newlines at the beginning of paragraphs
2825 while (cursor.par()->size() &&
2826 cursor.par()->isNewline(cursor.pos()) &&
2827 cursor.pos() == beginningOfMainBody(bview->buffer(),
2829 cursor.par()->erase(cursor.pos());
2830 // refresh the positions
2832 while (tmprow->next() &&
2833 tmprow->next()->par() == row->par()) {
2834 tmprow = tmprow->next();
2835 tmprow->pos(tmprow->pos() - 1);
2840 // is there a break one row above
2841 if (row->previous() && row->previous()->par() == row->par()) {
2842 z = nextBreakPoint(bview, row->previous(),
2844 if (z >= row->pos()) {
2847 Row * tmprow = row->previous();
2849 // maybe the current row is now empty
2850 if (row->pos() >= row->par()->size()) {
2855 breakAgainOneRow(bview, row);
2856 if (row->next() && row->next()->par() == row->par())
2857 need_break_row = row->next();
2862 // set the dimensions of the row above
2863 y -= tmprow->height();
2864 tmprow->fill(fill(bview, tmprow,
2866 setHeightOfRow(bview, tmprow);
2869 refresh_row = tmprow;
2870 status(bview, LyXText::NEED_MORE_REFRESH);
2871 setCursor(bview, cursor.par(), cursor.pos(),
2872 false, cursor.boundary());
2873 //current_font = rawtmpfont;
2874 //real_current_font = realtmpfont;
2875 // check, whether the last character's font has changed.
2877 cursor.par()->getFontSettings(bview->buffer()->params,
2878 cursor.par()->size() - 1))
2879 redoHeightOfParagraph(bview, cursor);
2884 // break the cursor row again
2885 if (row->next() && row->next()->par() == row->par() &&
2886 (rowLast(row) == row->par()->size() - 1 ||
2887 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2889 /* it can happen that a paragraph loses one row
2890 * without a real breakup. This is when a word
2891 * is to long to be broken. Well, I don t care this
2893 if (rowLast(row) == row->par()->size() - 1)
2894 removeRow(row->next());
2898 status(bview, LyXText::NEED_MORE_REFRESH);
2900 breakAgainOneRow(bview, row);
2901 // will the cursor be in another row now?
2902 if (row->next() && row->next()->par() == row->par() &&
2903 rowLast(row) <= cursor.pos()) {
2905 breakAgainOneRow(bview, row);
2908 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2910 if (row->next() && row->next()->par() == row->par())
2911 need_break_row = row->next();
2915 // set the dimensions of the row
2916 row->fill(fill(bview, row, workWidth(bview)));
2917 int const tmpheight = row->height();
2918 setHeightOfRow(bview, row);
2919 if (tmpheight == row->height())
2920 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2922 status(bview, LyXText::NEED_MORE_REFRESH);
2925 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2929 // current_font = rawtmpfont;
2930 // real_current_font = realtmpfont;
2932 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2933 != cursor.boundary())
2934 setCursor(bview, cursor.par(), cursor.pos(), false,
2935 !cursor.boundary());
2937 lastpos = cursor.par()->size();
2938 if (cursor.pos() == lastpos)
2939 setCurrentFont(bview);
2941 // check, whether the last characters font has changed.
2943 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2944 redoHeightOfParagraph(bview, cursor);
2946 // now the special right address boxes
2947 if (textclasslist.Style(bview->buffer()->params.textclass,
2948 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2949 redoDrawingOfParagraph(bview, cursor);
2955 bool LyXText::paintRowBackground(DrawRowParams & p)
2957 bool clear_area = true;
2959 LyXFont font(LyXFont::ALL_SANE);
2961 pos_type const last = rowLastPrintable(p.row);
2963 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2964 && p.row->par()->isInset(p.row->pos())) {
2965 inset = p.row->par()->getInset(p.row->pos());
2967 clear_area = inset->doClearArea();
2977 int const y = p.yo < 0 ? 0 : p.yo;
2978 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
2979 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
2986 int h = p.row->baseline() - inset->ascent(p.bv, font);
2988 // first clear the whole row above the inset!
2990 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
2993 // clear the space below the inset!
2994 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
2995 if ((p.row->height() - h) > 0) {
2996 p.pain->fillRectangle(p.xo, p.yo + h,
2997 p.width, p.row->height() - h, backgroundColor());
3000 // clear the space behind the inset, if needed
3001 if (!inset->display() && !inset->needFullRow()) {
3002 int const xp = int(p.x) + inset->width(p.bv, font);
3003 if (p.width - xp > 0) {
3004 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3005 p.row->height(), backgroundColor());
3013 void LyXText::paintRowSelection(DrawRowParams & p)
3015 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3017 // the current selection
3018 int const startx = selection.start.x();
3019 int const endx = selection.end.x();
3020 int const starty = selection.start.y();
3021 int const endy = selection.end.y();
3022 Row const * startrow = selection.start.row();
3023 Row const * endrow = selection.end.row();
3027 if (bidi_same_direction) {
3031 int h = row->height();
3033 if (startrow == row && endrow == row) {
3034 if (startx < endx) {
3037 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3041 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3043 } else if (startrow == row) {
3044 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3045 int const w = (is_rtl) ? startx : (p.width - startx);
3046 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3047 } else if (endrow == row) {
3048 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3049 int const w = (is_rtl) ? (p.width - endx) : endx;
3050 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3051 } else if (p.y > starty && p.y < endy) {
3052 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3055 } else if (startrow != row && endrow != row) {
3057 int h = row->height();
3058 if (p.y > starty && p.y < endy) {
3059 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3064 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3070 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3072 Buffer const * buffer = p.bv->buffer();
3073 Paragraph * par = row->par();
3074 pos_type main_body = beginningOfMainBody(buffer, par);
3075 pos_type const last = rowLastPrintable(row);
3077 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3078 pos_type pos = vis2log(vpos);
3079 float const old_tmpx = tmpx;
3080 if (main_body > 0 && pos == main_body - 1) {
3081 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3083 LyXFont const lfont = getLabelFont(buffer, par);
3086 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3088 if (par->isLineSeparator(main_body - 1))
3089 tmpx -= singleWidth(p.bv, par, main_body - 1);
3092 if (hfillExpansion(buffer, row, pos)) {
3093 tmpx += singleWidth(p.bv, par, pos);
3094 if (pos >= main_body)
3097 tmpx += p.label_hfill;
3100 else if (par->isSeparator(pos)) {
3101 tmpx += singleWidth(p.bv, par, pos);
3102 if (pos >= main_body)
3103 tmpx += p.separator;
3105 tmpx += singleWidth(p.bv, par, pos);
3108 if ((startrow != row || selection.start.pos() <= pos) &&
3109 (endrow != row || pos < selection.end.pos())) {
3110 // Here we do not use p.x as p.xo was added to p.x.
3111 p.pain->fillRectangle(int(old_tmpx), p.yo,
3112 int(tmpx - old_tmpx + 1),
3113 row->height(), LColor::selection);
3116 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3117 p.pain->fillRectangle(p.xo + int(tmpx),
3118 p.yo, int(p.bv->workWidth() - tmpx),
3119 row->height(), LColor::selection);
3125 void LyXText::paintRowAppendix(DrawRowParams & p)
3127 // FIXME: can be just p.width ?
3128 int const ww = p.bv->workWidth();
3129 Paragraph * firstpar = p.row->par();
3131 if (firstpar->params().appendix()) {
3132 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3133 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3138 void LyXText::paintRowDepthBar(DrawRowParams & p)
3140 Paragraph::depth_type const depth = p.row->par()->getDepth();
3145 Paragraph::depth_type prev_depth = 0;
3146 if (p.row->previous())
3147 prev_depth = p.row->previous()->par()->getDepth();
3148 Paragraph::depth_type next_depth = 0;
3150 next_depth = p.row->next()->par()->getDepth();
3152 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3153 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3154 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3156 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3158 int const w = LYX_PAPER_MARGIN / 5;
3160 if (i > prev_depth) {
3161 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3163 if (i > next_depth) {
3164 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3170 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3172 int const arrow_size = 4;
3173 int const space_size = int(vsp.inPixels(bv));
3175 if (vsp.kind() != VSpace::LENGTH) {
3181 int const min_size = std::max(3 * arrow_size,
3182 lyxfont::maxAscent(font)
3183 + lyxfont::maxDescent(font));
3185 if (vsp.length().len().value() < 0.0)
3188 return std::max(min_size, space_size);
3192 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3193 VSpace const & vsp, int start)
3195 int const arrow_size = 4;
3196 int const size = getLengthMarkerHeight(p.bv, vsp);
3197 int const end = start + size;
3199 // the label to display (if any)
3201 // y-values for top arrow
3203 // y-values for bottom arrow
3205 switch (vsp.kind()) {
3206 case VSpace::LENGTH:
3208 str = prefix + " (" + vsp.asLyXCommand() + ")";
3209 // adding or removing space
3210 bool const added = !(vsp.length().len().value() < 0.0);
3211 ty1 = added ? (start + arrow_size) : start;
3212 ty2 = added ? start : (start + arrow_size);
3213 by1 = added ? (end - arrow_size) : end;
3214 by2 = added ? end : (end - arrow_size);
3217 case VSpace:: VFILL:
3218 str = prefix + " (vertical fill)";
3223 // nothing to draw here
3227 int const leftx = p.xo + leftMargin(p.bv, p.row);
3228 int const midx = leftx + arrow_size;
3229 int const rightx = midx + arrow_size;
3237 font.setColor(LColor::added_space).decSize();
3238 lyxfont::rectText(str, font, w, a, d);
3240 p.pain->rectText(leftx + 2 * arrow_size + 5,
3241 start + ((end - start) / 2) + d,
3247 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3248 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3251 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3252 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3255 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3261 void LyXText::paintFirstRow(DrawRowParams & p)
3263 Paragraph * par = p.row->par();
3264 ParagraphParameters const & parparams = par->params();
3266 // start of appendix?
3267 if (parparams.startOfAppendix()) {
3268 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3273 // think about the margins
3274 if (!p.row->previous() && bv_owner)
3275 y_top += LYX_PAPER_MARGIN;
3277 // draw a top pagebreak
3278 if (parparams.pagebreakTop()) {
3279 int const y = p.yo + y_top + 2*defaultHeight();
3280 p.pain->line(p.xo, y, p.xo + p.width, y,
3281 LColor::pagebreak, Painter::line_onoffdash);
3288 pb_font.setColor(LColor::pagebreak).decSize();
3289 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3290 p.pain->rectText((p.width - w)/2, y + d,
3291 _("Page Break (top)"), pb_font,
3294 y_top += 3 * defaultHeight();
3297 // draw the additional space if needed:
3298 y_top += drawLengthMarker(p, _("Space above"),
3299 parparams.spaceTop(), p.yo + y_top);
3301 Buffer const * buffer = p.bv->buffer();
3303 LyXLayout const & layout =
3304 textclasslist.Style(buffer->params.textclass, par->getLayout());
3306 // think about the parskip
3307 // some parskips VERY EASY IMPLEMENTATION
3308 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3309 if (par->previous()) {
3310 if (layout.latextype == LATEX_PARAGRAPH
3311 && !par->getDepth()) {
3312 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3314 LyXLayout const & playout =
3315 textclasslist.Style(buffer->params.textclass,
3316 par->previous()->getLayout());
3317 if (playout.latextype == LATEX_PARAGRAPH
3318 && !par->previous()->getDepth()) {
3319 // is it right to use defskip here, too? (AS)
3320 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3326 int const ww = p.bv->workWidth();
3329 if (parparams.lineTop()) {
3330 LyXFont font(LyXFont::ALL_SANE);
3331 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3335 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3336 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3337 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3338 LColor::topline, Painter::line_solid,
3339 Painter::line_thick);
3344 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3346 // should we print a label?
3347 if (layout.labeltype >= LABEL_STATIC
3348 && (layout.labeltype != LABEL_STATIC
3349 || layout.latextype != LATEX_ENVIRONMENT
3350 || par->isFirstInSequence())) {
3352 LyXFont font = getLabelFont(buffer, par);
3353 if (!par->getLabelstring().empty()) {
3355 string const str = par->getLabelstring();
3357 // this is special code for the chapter layout. This is
3358 // printed in an extra row and has a pagebreak at
3360 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3361 if (buffer->params.secnumdepth >= 0) {
3362 float spacing_val = 1.0;
3363 if (!parparams.spacing().isDefault()) {
3364 spacing_val = parparams.spacing().getValue();
3366 spacing_val = buffer->params.spacing.getValue();
3370 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3371 + int(layout.parsep) * defaultHeight();
3374 x = ww - leftMargin(p.bv, p.row) -
3375 lyxfont::width(str, font);
3378 p.pain->text(int(x),
3379 p.yo + p.row->baseline() -
3380 p.row->ascent_of_text() - maxdesc,
3385 x = ww - leftMargin(p.bv, p.row)
3386 + lyxfont::width(layout.labelsep, font);
3388 x = p.x - lyxfont::width(layout.labelsep, font)
3389 - lyxfont::width(str, font);
3392 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3395 // the labels at the top of an environment.
3396 // More or less for bibliography
3397 } else if (par->isFirstInSequence() &&
3398 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3399 layout.labeltype == LABEL_BIBLIO ||
3400 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3401 LyXFont font = getLabelFont(buffer, par);
3402 if (!par->getLabelstring().empty()) {
3403 string const str = par->getLabelstring();
3404 float spacing_val = 1.0;
3405 if (!parparams.spacing().isDefault()) {
3406 spacing_val = parparams.spacing().getValue();
3408 spacing_val = buffer->params.spacing.getValue();
3412 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3413 + (layout.labelbottomsep * defaultHeight()));
3416 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3417 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3418 + ww - rightMargin(buffer, p.row) ) / 2;
3419 x -= lyxfont::width(str, font) / 2;
3420 } else if (is_rtl) {
3421 x = ww - leftMargin(p.bv, p.row) -
3422 lyxfont::width(str, font);
3424 p.pain->text(int(x), p.yo + p.row->baseline()
3425 - p.row->ascent_of_text() - maxdesc,
3430 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3431 LyXFont font = getLayoutFont(buffer, par);
3434 x = ww - leftMargin(p.bv, p.row)
3435 + lyxfont::width(layout.labelsep, font);
3437 x = p.x - lyxfont::width(layout.labelsep, font)
3438 - par->bibkey->width(p.bv, font);
3440 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3445 void LyXText::paintLastRow(DrawRowParams & p)
3447 Paragraph * par = p.row->par();
3448 ParagraphParameters const & parparams = par->params();
3449 int y_bottom = p.row->height() - 1;
3451 // think about the margins
3452 if (!p.row->next() && bv_owner)
3453 y_bottom -= LYX_PAPER_MARGIN;
3455 int const ww = p.bv->workWidth();
3457 // draw a bottom pagebreak
3458 if (parparams.pagebreakBottom()) {
3460 pb_font.setColor(LColor::pagebreak).decSize();
3461 int const y = p.yo + y_bottom - 2 * defaultHeight();
3463 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3464 Painter::line_onoffdash);
3469 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3470 p.pain->rectText((ww - w) / 2, y + d,
3471 _("Page Break (bottom)"),
3472 pb_font, backgroundColor(), backgroundColor());
3474 y_bottom -= 3 * defaultHeight();
3477 // draw the additional space if needed:
3478 int const height = getLengthMarkerHeight(p.bv,
3479 parparams.spaceBottom());
3480 y_bottom -= drawLengthMarker(p, _("Space below"),
3481 parparams.spaceBottom(),
3482 p.yo + y_bottom - height);
3484 Buffer const * buffer = p.bv->buffer();
3486 // draw a bottom line
3487 if (parparams.lineBottom()) {
3488 LyXFont font(LyXFont::ALL_SANE);
3489 int const asc = lyxfont::ascent('x',
3490 getFont(buffer, par,
3491 max(pos_type(0), par->size() - 1)));
3495 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3496 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3497 int const y = p.yo + y_bottom;
3498 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3499 Painter::line_thick);
3504 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3505 int const endlabel = par->getEndLabel(buffer->params);
3510 case END_LABEL_FILLED_BOX:
3512 LyXFont const font = getLabelFont(buffer, par);
3513 int const size = int(0.75 * lyxfont::maxAscent(font));
3514 int const y = (p.yo + p.row->baseline()) - size;
3515 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3517 if (p.row->fill() <= size)
3518 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3520 if (endlabel == END_LABEL_BOX) {
3521 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3523 p.pain->fillRectangle(x, y, size, size,
3528 case END_LABEL_STATIC:
3530 LyXFont font(LyXFont::ALL_SANE);
3531 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3532 string const str = textclasslist.
3533 Style(buffer->params.textclass, layout).endlabelstring();
3534 font = getLabelFont(buffer, par);
3535 int const x = is_rtl ?
3536 int(p.x) - lyxfont::width(str, font)
3537 : ww - rightMargin(buffer, p.row) - p.row->fill();
3538 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3541 case END_LABEL_NO_LABEL:
3546 void LyXText::paintRowText(DrawRowParams & p)
3548 Paragraph * par = p.row->par();
3549 Buffer const * buffer = p.bv->buffer();
3551 pos_type const last = rowLastPrintable(p.row);
3552 pos_type main_body =
3553 beginningOfMainBody(buffer, par);
3554 if (main_body > 0 &&
3555 (main_body - 1 > last ||
3556 !par->isLineSeparator(main_body - 1))) {
3560 LyXLayout const & layout =
3561 textclasslist.Style(buffer->params.textclass, par->getLayout());
3563 pos_type vpos = p.row->pos();
3564 while (vpos <= last) {
3565 pos_type pos = vis2log(vpos);
3566 if (main_body > 0 && pos == main_body - 1) {
3567 int const lwidth = lyxfont::width(layout.labelsep,
3568 getLabelFont(buffer, par));
3570 p.x += p.label_hfill + lwidth
3571 - singleWidth(p.bv, par, main_body - 1);
3574 if (par->isHfill(pos)) {
3577 int const y0 = p.yo + p.row->baseline();
3578 int const y1 = y0 - defaultHeight() / 2;
3580 p.pain->line(int(p.x), y1, int(p.x), y0,
3581 LColor::added_space);
3583 if (hfillExpansion(buffer, p.row, pos)) {
3584 int const y2 = (y0 + y1) / 2;
3586 if (pos >= main_body) {
3587 p.pain->line(int(p.x), y2,
3588 int(p.x + p.hfill), y2,
3589 LColor::added_space,
3590 Painter::line_onoffdash);
3593 p.pain->line(int(p.x), y2,
3594 int(p.x + p.label_hfill), y2,
3595 LColor::added_space,
3596 Painter::line_onoffdash);
3597 p.x += p.label_hfill;
3599 p.pain->line(int(p.x), y1,
3601 LColor::added_space);
3605 } else if (par->isSeparator(pos)) {
3606 p.x += singleWidth(p.bv, par, pos);
3607 if (pos >= main_body)
3617 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3618 Row * row, int y, bool cleared)
3620 if (row->height() <= 0) {
3621 lyxerr << "LYX_ERROR: row.height: "
3622 << row->height() << endl;
3628 // set up drawing parameters
3630 p.pain = &bv->painter();
3634 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3635 if (inset_owner && (p.x < 0))
3639 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3640 p.cleared = cleared;
3644 // clear to background if necessary
3645 p.cleared = paintRowBackground(p);
3647 // paint the selection background
3648 if (selection.set()) {
3649 paintRowSelection(p);
3652 // vertical lines for appendix
3653 paintRowAppendix(p);
3655 // environment depth brackets
3656 paintRowDepthBar(p);
3658 // draw any stuff wanted for a first row of a paragraph
3663 // draw any stuff wanted for the last row of a paragraph
3664 if (!row->next() || (row->next()->par() != row->par())) {
3673 int LyXText::defaultHeight() const
3675 LyXFont font(LyXFont::ALL_SANE);
3676 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3680 /* returns the column near the specified x-coordinate of the row
3681 * x is set to the real beginning of this column */
3683 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3684 bool & boundary) const
3687 float fill_separator;
3689 float fill_label_hfill;
3691 prepareToPrint(bview, row, tmpx, fill_separator,
3692 fill_hfill, fill_label_hfill);
3694 pos_type vc = row->pos();
3695 pos_type last = rowLastPrintable(row);
3697 LyXLayout const & layout =
3698 textclasslist.Style(bview->buffer()->params.textclass,
3699 row->par()->getLayout());
3700 bool left_side = false;
3702 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3703 float last_tmpx = tmpx;
3705 if (main_body > 0 &&
3706 (main_body - 1 > last ||
3707 !row->par()->isLineSeparator(main_body - 1)))
3710 while (vc <= last && tmpx <= x) {
3713 if (main_body > 0 && c == main_body-1) {
3714 tmpx += fill_label_hfill +
3715 lyxfont::width(layout.labelsep,
3716 getLabelFont(bview->buffer(), row->par()));
3717 if (row->par()->isLineSeparator(main_body - 1))
3718 tmpx -= singleWidth(bview, row->par(), main_body-1);
3721 if (hfillExpansion(bview->buffer(), row, c)) {
3722 x += singleWidth(bview, row->par(), c);
3726 tmpx += fill_label_hfill;
3728 else if (row->par()->isSeparator(c)) {
3729 tmpx += singleWidth(bview, row->par(), c);
3731 tmpx+= fill_separator;
3733 tmpx += singleWidth(bview, row->par(), c);
3737 if ((tmpx + last_tmpx) / 2 > x) {
3742 if (vc > last + 1) // This shouldn't happen.
3746 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3747 // some speedup if rtl_support=false
3748 && (!row->next() || row->next()->par() != row->par());
3749 bool const rtl = (lastrow)
3750 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3751 : false; // If lastrow is false, we don't need to compute
3752 // the value of rtl.
3754 if (row->pos() > last) // Row is empty?
3757 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3758 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3760 else if (vc == row->pos()) {
3762 if (bidi_level(c) % 2 == 1)
3765 c = vis2log(vc - 1);
3766 bool const rtl = (bidi_level(c) % 2 == 1);
3767 if (left_side == rtl) {
3769 boundary = isBoundary(bview->buffer(), row->par(), c);
3773 if (row->pos() <= last && c > last
3774 && row->par()->isNewline(last)) {
3775 if (bidi_level(last) % 2 == 0)
3776 tmpx -= singleWidth(bview, row->par(), last);
3778 tmpx += singleWidth(bview, row->par(), last);
3788 // returns pointer to a specified row
3789 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3794 Row * tmprow = firstrow;
3797 // find the first row of the specified paragraph
3798 while (tmprow->next() && tmprow->par() != par) {
3799 y += tmprow->height();
3800 tmprow = tmprow->next();
3803 // now find the wanted row
3804 while (tmprow->pos() < pos
3806 && tmprow->next()->par() == par
3807 && tmprow->next()->pos() <= pos) {
3808 y += tmprow->height();
3809 tmprow = tmprow->next();
3816 Row * LyXText::getRowNearY(int & y) const
3818 // If possible we should optimize this method. (Lgb)
3819 Row * tmprow = firstrow;
3822 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3823 tmpy += tmprow->height();
3824 tmprow = tmprow->next();
3827 y = tmpy; // return the real y
3832 int LyXText::getDepth() const
3834 return cursor.par()->getDepth();