1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
15 #include "lyxtextclasslist.h"
16 #include "paragraph.h"
17 #include "lyx_gui_misc.h"
19 #include "bufferparams.h"
28 #include "lyxscreen.h"
29 #include "bufferview_funcs.h"
30 #include "BufferView.h"
32 #include "ParagraphParameters.h"
33 #include "undo_funcs.h"
36 #include "insets/insetbib.h"
37 #include "insets/insettext.h"
39 #include "support/textutils.h"
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
53 int const LYX_PAPER_MARGIN = 20;
57 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
60 int LyXText::workWidth(BufferView * bview) const
63 return inset_owner->textWidth(bview);
65 return bview->workWidth();
69 int LyXText::workWidth(BufferView * bview, Inset * inset) const
74 par = inset->parOwner();
76 pos = par->getPositionOfInset(inset);
78 if (!par || pos == -1) {
79 lyxerr << "LyXText::workWidth: something is wrong,"
80 " fall back to the brute force method" << endl;
81 Buffer::inset_iterator it = bview->buffer()->inset_iterator_begin();
82 Buffer::inset_iterator end = bview->buffer()->inset_iterator_end();
83 for ( ; it != end; ++it) {
93 return workWidth(bview);
96 LyXLayout const & layout =
97 textclasslist.Style(bview->buffer()->params.textclass,
100 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
101 // Optimization here: in most cases, the real row is
102 // not needed, but only the par/pos values. So we just
103 // construct a dummy row for leftMargin. (JMarc)
107 return workWidth(bview) - leftMargin(bview, &dummyrow);
110 Row * row = getRow(par, pos, dummy_y);
112 while (frow->previous() && frow->par() == frow->previous()->par())
113 frow = frow->previous();
114 unsigned int maxw = 0;
115 while (frow->next() && frow->par() == frow->next()->par()) {
116 if ((frow != row) && (maxw < frow->width()))
117 maxw = frow->width();
123 return workWidth(bview);
127 int LyXText::getRealCursorX(BufferView * bview) const
130 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
131 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
136 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
139 if (!Encodings::is_arabic(c))
140 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
141 return c + (0xb0 - '0');
145 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
146 unsigned char next_char = ' ';
148 for (pos_type i = pos+1; i < par->size(); ++i)
149 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
150 next_char = par->getChar(i);
154 if (Encodings::is_arabic(next_char)) {
155 if (Encodings::is_arabic(prev_char))
156 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
158 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
160 if (Encodings::is_arabic(prev_char))
161 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
163 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
167 // This is the comments that some of the warnings below refers to.
168 // There are some issues in this file and I don't think they are
169 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
170 // this is a problem that has been here almost from day one and that a
171 // larger userbase with differenct access patters triggers the bad
172 // behaviour. (segfaults.) What I think happen is: In several places
173 // we store the paragraph in the current cursor and then moves the
174 // cursor. This movement of the cursor will delete paragraph at the
175 // old position if it is now empty. This will make the temporary
176 // pointer to the old cursor paragraph invalid and dangerous to use.
177 // And is some cases this will trigger a segfault. I have marked some
178 // of the cases where this happens with a warning, but I am sure there
179 // are others in this file and in text2.C. There is also a note in
180 // Delete() that you should read. In Delete I store the paragraph->id
181 // instead of a pointer to the paragraph. I am pretty sure this faulty
182 // use of temporary pointers to paragraphs that might have gotten
183 // invalidated (through a cursor movement) before they are used, are
184 // the cause of the strange crashes we get reported often.
186 // It is very tiresom to change this code, especially when it is as
187 // hard to read as it is. Help to fix all the cases where this is done
188 // would be greately appreciated.
192 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
195 char const c = par->getChar(pos);
196 return singleWidth(bview, par, pos, c);
200 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
201 pos_type pos, char c) const
203 LyXFont const font = getFont(bview->buffer(), par, pos);
205 // The most common case is handled first (Asger)
206 if (IsPrintable(c)) {
207 if (font.language()->RightToLeft()) {
208 if (font.language()->lang() == "arabic" &&
209 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
210 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
211 if (Encodings::IsComposeChar_arabic(c))
214 c = transformChar(c, par, pos);
215 else if (font.language()->lang() == "hebrew" &&
216 Encodings::IsComposeChar_hebrew(c))
219 return lyxfont::width(c, font);
221 } else if (IsHfillChar(c)) {
222 return 3; /* Because of the representation
223 * as vertical lines */
224 } else if (c == Paragraph::META_INSET) {
225 Inset * tmpinset = par->getInset(pos);
228 // this IS needed otherwise on initialitation we don't get the fill
229 // of the row right (ONLY on initialization if we read a file!)
230 // should be changed! (Jug 20011204)
231 tmpinset->update(bview, font);
233 return tmpinset->width(bview, font);
237 } else if (IsSeparatorChar(c))
239 else if (IsNewlineChar(c))
241 return lyxfont::width(c, font);
245 // Returns the paragraph position of the last character in the specified row
246 pos_type LyXText::rowLast(Row const * row) const
248 if (!row->next() || row->next()->par() != row->par()) {
249 return row->par()->size() - 1;
251 return row->next()->pos() - 1;
256 pos_type LyXText::rowLastPrintable(Row const * row) const
258 pos_type const last = rowLast(row);
259 if (last >= row->pos()
261 && row->next()->par() == row->par()
262 && row->par()->isSeparator(last))
269 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
271 bidi_same_direction = true;
272 if (!lyxrc.rtl_support) {
277 bidi_start = row->pos();
278 bidi_end = rowLastPrintable(row);
280 if (bidi_start > bidi_end) {
285 if (bidi_end + 2 - bidi_start >
286 static_cast<pos_type>(log2vis_list.size())) {
288 (bidi_end + 2 - bidi_start < 500) ?
289 500 : 2 * (bidi_end + 2 - bidi_start);
290 log2vis_list.resize(new_size);
291 vis2log_list.resize(new_size);
292 bidi_levels.resize(new_size);
295 vis2log_list[bidi_end + 1 - bidi_start] = -1;
296 log2vis_list[bidi_end + 1 - bidi_start] = -1;
300 row->par()->getParLanguage(buf->params)->RightToLeft();
304 pos_type const main_body = beginningOfMainBody(buf, row->par());
306 for (pos_type lpos = bidi_start;
307 lpos <= bidi_end; ++lpos) {
308 bool is_space = row->par()->isLineSeparator(lpos);
310 (is_space && lpos + 1 <= bidi_end &&
311 !row->par()->isLineSeparator(lpos + 1) &&
312 !row->par()->isNewline(lpos + 1))
314 LyXFont font = row->par()->getFontSettings(buf->params, pos);
315 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
316 font.number() == LyXFont::ON &&
317 row->par()->getFontSettings(buf->params, lpos - 1).number()
319 font = row->par()->getFontSettings(buf->params, lpos);
324 bool new_rtl = font.isVisibleRightToLeft();
325 bool new_rtl0 = font.isRightToLeft();
328 if (lpos == main_body - 1
329 && row->pos() < main_body - 1
331 new_level = (rtl_par) ? 1 : 0;
332 new_rtl = new_rtl0 = rtl_par;
334 new_level = (new_rtl) ? 1 : 2;
336 new_level = (rtl_par) ? 2 : 0;
338 if (is_space && new_level >= level) {
344 int new_level2 = new_level;
346 if (level == new_level && rtl0 != new_rtl0) {
348 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
349 } else if (level < new_level) {
350 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
351 if (new_level > rtl_par)
352 bidi_same_direction = false;
354 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
357 bidi_levels[lpos - bidi_start] = new_level;
359 while (level > new_level2) {
360 pos_type old_lpos = stack[--level];
361 int delta = lpos - old_lpos - 1;
364 log2vis_list[lpos - bidi_start] += delta;
365 log2vis_list[old_lpos - bidi_start] += delta;
367 while (level < new_level)
368 stack[level++] = lpos;
372 pos_type const old_lpos = stack[--level];
373 int delta = bidi_end - old_lpos;
376 log2vis_list[old_lpos - bidi_start] += delta;
379 pos_type vpos = bidi_start - 1;
380 for (pos_type lpos = bidi_start;
381 lpos <= bidi_end; ++lpos) {
382 vpos += log2vis_list[lpos - bidi_start];
383 vis2log_list[vpos - bidi_start] = lpos;
384 log2vis_list[lpos - bidi_start] = vpos;
389 // This method requires a previous call to ComputeBidiTables()
390 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
393 if (!lyxrc.rtl_support || pos == 0)
396 if (!bidi_InRange(pos - 1)) {
397 /// This can happen if pos is the first char of a row.
398 /// Returning false in this case is incorrect!
402 bool const rtl = bidi_level(pos - 1) % 2;
403 bool const rtl2 = bidi_InRange(pos)
404 ? bidi_level(pos) % 2
405 : par->isRightToLeftPar(buf->params);
410 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
411 pos_type pos, LyXFont const & font) const
413 if (!lyxrc.rtl_support)
414 return false; // This is just for speedup
416 bool const rtl = font.isVisibleRightToLeft();
417 bool const rtl2 = bidi_InRange(pos)
418 ? bidi_level(pos) % 2
419 : par->isRightToLeftPar(buf->params);
423 void LyXText::drawNewline(DrawRowParams & p, pos_type const pos)
425 // Draw end-of-line marker
426 LyXFont const font = getFont(p.bv->buffer(), p.row->par(), pos);
427 int const wid = lyxfont::width('n', font);
428 int const asc = lyxfont::maxAscent(font);
429 int const y = p.yo + p.row->baseline();
433 yp[0] = int(y - 0.875 * asc * 0.75);
434 yp[1] = int(y - 0.500 * asc * 0.75);
435 yp[2] = int(y - 0.125 * asc * 0.75);
437 if (bidi_level(pos) % 2 == 0) {
438 xp[0] = int(p.x + wid * 0.375);
440 xp[2] = int(p.x + wid * 0.375);
442 xp[0] = int(p.x + wid * 0.625);
443 xp[1] = int(p.x + wid);
444 xp[2] = int(p.x + wid * 0.625);
447 p.pain->lines(xp, yp, 3, LColor::eolmarker);
449 yp[0] = int(y - 0.500 * asc * 0.75);
450 yp[1] = int(y - 0.500 * asc * 0.75);
451 yp[2] = int(y - asc * 0.75);
453 if (bidi_level(pos) % 2 == 0) {
455 xp[1] = int(p.x + wid);
456 xp[2] = int(p.x + wid);
458 xp[0] = int(p.x + wid);
463 p.pain->lines(xp, yp, 3, LColor::eolmarker);
469 void LyXText::drawInset(DrawRowParams & p, pos_type const pos)
471 Inset * inset = p.row->par()->getInset(pos);
473 // FIXME: shouldn't happen
478 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
480 inset->update(p.bv, font, false);
481 inset->draw(p.bv, font, p.yo + p.row->baseline(), p.x, p.cleared);
483 if (!need_break_row && !inset_owner
484 && p.bv->text->status() == CHANGED_IN_DRAW) {
485 Row * prev = p.row->previous();
486 if (prev && prev->par() == p.row->par()) {
487 breakAgainOneRow(p.bv, prev);
489 setCursor(p.bv, cursor.par(), cursor.pos());
490 need_break_row = p.row;
495 void LyXText::drawForeignMark(DrawRowParams & p, float const orig_x, LyXFont const & orig_font)
497 if (!lyxrc.mark_foreign_language)
499 if (orig_font.language() == latex_language)
501 if (orig_font.language() == p.bv->buffer()->params.language)
504 int const y = p.yo + p.row->height() - 1;
505 p.pain->line(int(orig_x), y, int(p.x), y, LColor::language);
509 void LyXText::drawHebrewComposeChar(DrawRowParams & p, pos_type & vpos)
511 pos_type pos = vis2log(vpos);
516 char c = p.row->par()->getChar(pos);
520 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
521 int const width = lyxfont::width(c, font);
524 for (pos_type i = pos-1; i >= 0; --i) {
525 c = p.row->par()->getChar(i);
526 if (!Encodings::IsComposeChar_hebrew(c)) {
527 if (IsPrintableNonspace(c)) {
529 singleWidth(p.bv, p.row->par(), i, c);
531 dx = (c == 'ø' || c == 'ã')
533 : (width2 - width) / 2;
540 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
544 void LyXText::drawArabicComposeChar(DrawRowParams & p, pos_type & vpos)
546 pos_type pos = vis2log(vpos);
550 char c = p.row->par()->getChar(pos);
551 c = transformChar(c, p.row->par(), pos);
555 LyXFont const & font = getFont(p.bv->buffer(), p.row->par(), pos);
556 int const width = lyxfont::width(c, font);
559 for (pos_type i = pos-1; i >= 0; --i) {
560 c = p.row->par()->getChar(i);
561 if (!Encodings::IsComposeChar_arabic(c)) {
562 if (IsPrintableNonspace(c)) {
564 singleWidth(p.bv, p.row->par(), i, c);
565 dx = (width2 - width) / 2;
571 p.pain->text(int(p.x) + dx, p.yo + p.row->baseline(), str, font);
575 void LyXText::drawChars(DrawRowParams & p, pos_type & vpos,
576 bool hebrew, bool arabic)
578 pos_type pos = vis2log(vpos);
579 pos_type const last = rowLastPrintable(p.row);
580 LyXFont const & orig_font = getFont(p.bv->buffer(), p.row->par(), pos);
584 str += p.row->par()->getChar(pos);
586 unsigned char c = str[0];
587 str[0] = transformChar(c, p.row->par(), pos);
591 // collect as much similar chars as we can
592 while (vpos <= last && (pos = vis2log(vpos)) >= 0) {
593 char c = p.row->par()->getChar(pos);
595 if (!IsPrintableNonspace(c))
598 if (arabic && Encodings::IsComposeChar_arabic(c))
600 if (hebrew && Encodings::IsComposeChar_hebrew(c))
603 if (orig_font != getFont(p.bv->buffer(), p.row->par(), pos))
610 // Draw text and set the new x position
611 p.pain->text(int(p.x), p.yo + p.row->baseline(), str, orig_font);
612 p.x += lyxfont::width(str, orig_font);
616 void LyXText::draw(DrawRowParams & p, pos_type & vpos)
618 pos_type const pos = vis2log(vpos);
619 Paragraph * par = p.row->par();
621 LyXFont const & orig_font = getFont(p.bv->buffer(), par, pos);
623 float const orig_x = p.x;
625 char const c = par->getChar(pos);
627 if (IsNewlineChar(c)) {
631 } else if (IsInsetChar(c)) {
634 drawForeignMark(p, orig_x, orig_font);
638 // usual characters, no insets
640 // special case languages
641 bool const hebrew = (orig_font.language()->lang() == "hebrew");
643 orig_font.language()->lang() == "arabic" &&
644 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
645 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
647 // draw as many chars as we can
648 if ((!hebrew && !arabic)
649 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
650 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
651 drawChars(p, vpos, true, false);
653 drawHebrewComposeChar(p, vpos);
655 drawArabicComposeChar(p, vpos);
658 drawForeignMark(p, orig_x, orig_font);
660 #ifdef INHERIT_LANGUAGE
662 if ((font.language() == inherit_language) ||
663 (font.language() == ignore_language))
664 lyxerr << "No this shouldn't happen!\n";
670 // Returns the left beginning of the text.
671 // This information cannot be taken from the layouts-objekt, because in
672 // LaTeX the beginning of the text fits in some cases (for example sections)
673 // exactly the label-width.
674 int LyXText::leftMargin(BufferView * bview, Row const * row) const
676 LyXTextClass const & tclass =
677 textclasslist.TextClass(bview->buffer()->params.textclass);
678 LyXLayout const & layout = tclass[row->par()->getLayout()];
680 string parindent = layout.parindent;
682 int x = LYX_PAPER_MARGIN;
684 x += lyxfont::signedWidth(tclass.leftmargin(), tclass.defaultfont());
686 // this is the way, LyX handles the LaTeX-Environments.
687 // I have had this idea very late, so it seems to be a
688 // later added hack and this is true
689 if (!row->par()->getDepth()) {
690 if (!row->par()->getLayout()) {
691 // find the previous same level paragraph
692 if (row->par()->previous()) {
693 Paragraph * newpar = row->par()
694 ->depthHook(row->par()->getDepth());
696 tclass[newpar->getLayout()].nextnoindent)
701 // find the next level paragraph
704 row->par()->outerHook();
706 // make a corresponding row. Needed to call LeftMargin()
708 // check wether it is a sufficent paragraph
709 if (newpar && tclass[newpar->getLayout()].isEnvironment())
712 dummyrow.par(newpar);
713 dummyrow.pos(newpar->size());
714 x = leftMargin(bview, &dummyrow);
716 // this is no longer an error, because this function
717 // is used to clear impossible depths after changing
718 // a layout. Since there is always a redo,
719 // LeftMargin() is always called
720 row->par()->params().depth(0);
723 if (newpar && !row->par()->getLayout()) {
724 if (newpar->params().noindent())
727 parindent = tclass[newpar->getLayout()].parindent;
732 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
733 switch (layout.margintype) {
735 if (!layout.leftmargin.empty()) {
736 x += lyxfont::signedWidth(layout.leftmargin,
737 tclass.defaultfont());
739 if (!row->par()->getLabelstring().empty()) {
740 x += lyxfont::signedWidth(layout.labelindent,
742 x += lyxfont::width(row->par()->getLabelstring(),
744 x += lyxfont::width(layout.labelsep, labelfont);
748 x += lyxfont::signedWidth(layout.labelindent, labelfont);
749 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
750 if (!row->par()->getLabelWidthString().empty()) {
751 x += lyxfont::width(row->par()->getLabelWidthString(),
753 x += lyxfont::width(layout.labelsep, labelfont);
758 x += lyxfont::signedWidth(layout.leftmargin, tclass.defaultfont()) * 4
759 / (row->par()->getDepth() + 4);
761 case MARGIN_FIRST_DYNAMIC:
762 if (layout.labeltype == LABEL_MANUAL) {
763 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
764 x += lyxfont::signedWidth(layout.leftmargin,
767 x += lyxfont::signedWidth(layout.labelindent,
770 } else if (row->pos()
771 // Special case to fix problems with
773 || (layout.labeltype == LABEL_STATIC
774 && layout.latextype == LATEX_ENVIRONMENT
775 && ! row->par()->isFirstInSequence())) {
776 x += lyxfont::signedWidth(layout.leftmargin,
778 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
779 && layout.labeltype != LABEL_BIBLIO
780 && layout.labeltype !=
781 LABEL_CENTERED_TOP_ENVIRONMENT) {
782 x += lyxfont::signedWidth(layout.labelindent,
784 x += lyxfont::width(layout.labelsep, labelfont);
785 x += lyxfont::width(row->par()->getLabelstring(),
790 case MARGIN_RIGHT_ADDRESS_BOX:
792 // ok, a terrible hack. The left margin depends on the widest
793 // row in this paragraph. Do not care about footnotes, they
794 // are *NOT* allowed in the LaTeX realisation of this layout.
796 // find the first row of this paragraph
797 Row const * tmprow = row;
798 while (tmprow->previous()
799 && tmprow->previous()->par() == row->par())
800 tmprow = tmprow->previous();
802 int minfill = tmprow->fill();
803 while (tmprow->next() && tmprow->next()->par() == row->par()) {
804 tmprow = tmprow->next();
805 if (tmprow->fill() < minfill)
806 minfill = tmprow->fill();
809 x += lyxfont::signedWidth(layout.leftmargin,
810 tclass.defaultfont());
816 LyXAlignment align; // wrong type
818 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
819 align = layout.align;
821 align = row->par()->params().align();
823 // set the correct parindent
824 if (row->pos() == 0) {
825 if ((layout.labeltype == LABEL_NO_LABEL
826 || layout.labeltype == LABEL_TOP_ENVIRONMENT
827 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
828 || (layout.labeltype == LABEL_STATIC
829 && layout.latextype == LATEX_ENVIRONMENT
830 && ! row->par()->isFirstInSequence()))
831 && align == LYX_ALIGN_BLOCK
832 && !row->par()->params().noindent()
833 && (row->par()->layout ||
834 bview->buffer()->params.paragraph_separation ==
835 BufferParams::PARSEP_INDENT))
836 x += lyxfont::signedWidth(parindent,
837 tclass.defaultfont());
838 else if (layout.labeltype == LABEL_BIBLIO) {
839 // ale970405 Right width for bibitems
840 x += bibitemMaxWidth(bview, tclass.defaultfont());
847 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
849 LyXTextClass const & tclass =
850 textclasslist.TextClass(buf->params.textclass);
851 LyXLayout const & layout = tclass[row->par()->getLayout()];
853 int x = LYX_PAPER_MARGIN
854 + lyxfont::signedWidth(tclass.rightmargin(),
855 tclass.defaultfont());
857 // this is the way, LyX handles the LaTeX-Environments.
858 // I have had this idea very late, so it seems to be a
859 // later added hack and this is true
860 if (row->par()->getDepth()) {
861 // find the next level paragraph
863 Paragraph * newpar = row->par();
866 newpar = newpar->previous();
868 && newpar->getDepth() >= row->par()->getDepth());
870 // make a corresponding row. Needed to call LeftMargin()
872 // check wether it is a sufficent paragraph
874 && tclass[newpar->getLayout()].isEnvironment()) {
876 dummyrow.par(newpar);
878 x = rightMargin(buf, &dummyrow);
880 // this is no longer an error, because this function
881 // is used to clear impossible depths after changing
882 // a layout. Since there is always a redo,
883 // LeftMargin() is always called
884 row->par()->params().depth(0);
888 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
889 x += lyxfont::signedWidth(layout.rightmargin, tclass.defaultfont())
890 * 4 / (row->par()->getDepth() + 4);
895 int LyXText::labelEnd(BufferView * bview, Row const * row) const
897 if (textclasslist.Style(bview->buffer()->params.textclass,
898 row->par()->getLayout()).margintype
902 tmprow.pos(row->par()->size());
903 return leftMargin(bview, &tmprow); /* just the beginning
906 return 0; /* LabelEnd is only needed, if the
907 layout fills a flushleft
912 // get the next breakpoint in a given paragraph
914 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
916 Paragraph * par = row->par();
921 pos_type const pos = row->pos();
923 // position of the last possible breakpoint
924 // -1 isn't a suitable value, but a flag
925 pos_type last_separator = -1;
926 width -= rightMargin(bview->buffer(), row);
928 pos_type const main_body =
929 beginningOfMainBody(bview->buffer(), par);
930 LyXLayout const & layout =
931 textclasslist.Style(bview->buffer()->params.textclass,
935 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
936 /* special code for right address boxes, only newlines count */
937 while (i < par->size()) {
938 if (par->isNewline(i)) {
940 i = par->size() - 1; // this means break
942 } else if (par->isInset(i) && par->getInset(i)
943 && par->getInset(i)->display()) {
944 par->getInset(i)->display(false);
949 // Last position is an invariant
950 pos_type const last =
952 // this is the usual handling
953 int x = leftMargin(bview, row);
954 bool doitonetime = true;
955 while (doitonetime || ((x < width) && (i < last))) {
957 char const c = par->getChar(i);
958 if (IsNewlineChar(c)) {
960 x = width; // this means break
961 } else if (c == Paragraph::META_INSET &&
964 // check wether a Display() inset is
965 // valid here. if not, change it to
967 if (par->getInset(i)->display() &&
968 (layout.isCommand() ||
969 (layout.labeltype == LABEL_MANUAL
970 && i < beginningOfMainBody(bview->buffer(), par)))) {
971 // display istn't allowd
972 par->getInset(i)->display(false);
973 x += singleWidth(bview, par, i, c);
974 } else if (par->getInset(i)->display() ||
975 par->getInset(i)->needFullRow()) {
976 // So break the line here
980 if (IsLineSeparatorChar(par->getChar(i+1)))
983 last_separator = last; // to avoid extra rows
985 last_separator = i - 1;
986 x = width; // this means break
988 x += singleWidth(bview, par, i, c);
991 if (IsLineSeparatorChar(c))
993 x += singleWidth(bview, par, i, c);
996 if (i == main_body) {
997 x += lyxfont::width(layout.labelsep,
998 getLabelFont(bview->buffer(), par));
999 if (par->isLineSeparator(i - 1))
1000 x-= singleWidth(bview, par, i - 1);
1001 int left_margin = labelEnd(bview, row);
1002 if (x < left_margin)
1006 // end of paragraph is always a suitable separator
1007 if (i == last && x < width)
1011 // well, if last_separator is still 0, the line isn't breakable.
1012 // don't care and cut simply at the end
1013 if (last_separator < 0) {
1017 // manual labels cannot be broken in LaTeX, do not care
1018 if (main_body && last_separator < main_body)
1019 last_separator = main_body - 1;
1021 return last_separator;
1025 // returns the minimum space a row needs on the screen in pixel
1026 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1028 if (paper_width < 0)
1032 // get the pure distance
1033 pos_type const last = rowLastPrintable(row);
1035 // special handling of the right address boxes
1036 if (textclasslist.Style(bview->buffer()->params.textclass,
1037 row->par()->getLayout()).margintype
1038 == MARGIN_RIGHT_ADDRESS_BOX)
1040 int const tmpfill = row->fill();
1041 row->fill(0); // the minfill in MarginLeft()
1042 w = leftMargin(bview, row);
1045 w = leftMargin(bview, row);
1047 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1048 row->par()->getLayout());
1049 pos_type const main_body =
1050 beginningOfMainBody(bview->buffer(), row->par());
1051 pos_type i = row->pos();
1054 if (main_body > 0 && i == main_body) {
1055 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1056 if (row->par()->isLineSeparator(i - 1))
1057 w -= singleWidth(bview, row->par(), i - 1);
1058 int left_margin = labelEnd(bview, row);
1059 if (w < left_margin)
1062 w += singleWidth(bview, row->par(), i);
1065 if (main_body > 0 && main_body > last) {
1066 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1067 if (last >= 0 && row->par()->isLineSeparator(last))
1068 w -= singleWidth(bview, row->par(), last);
1069 int const left_margin = labelEnd(bview, row);
1070 if (w < left_margin)
1074 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1079 // returns the minimum space a manual label needs on the screen in pixel
1080 int LyXText::labelFill(BufferView * bview, Row const * row) const
1082 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1083 // -1 because a label ends either with a space that is in the label,
1084 // or with the beginning of a footnote that is outside the label.
1086 // I don't understand this code in depth, but sometimes "last" is
1087 // less than 0 and this causes a crash. This fix seems to work
1088 // correctly, but I bet the real error is elsewhere. The bug is
1089 // triggered when you have an open footnote in a paragraph
1090 // environment with a manual label. (Asger)
1091 if (last < 0) last = 0;
1093 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1098 pos_type i = row->pos();
1100 w += singleWidth(bview, row->par(), i);
1105 if (!row->par()->params().labelWidthString().empty()) {
1106 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1107 getLabelFont(bview->buffer(), row->par())) - w,
1115 // returns the number of separators in the specified row. The separator
1116 // on the very last column doesnt count
1117 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1119 pos_type const last = rowLast(row);
1120 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1122 for (; p < last; ++p) {
1123 if (row->par()->isSeparator(p)) {
1131 // returns the number of hfills in the specified row. The LyX-Hfill is
1132 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1133 // ignored. This is *MUCH* more usefull than not to ignore!
1134 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1136 pos_type const last = rowLast(row);
1137 pos_type first = row->pos();
1139 if (first) { /* hfill *DO* count at the beginning
1141 while (first <= last && row->par()->isHfill(first)) {
1146 first = max(first, beginningOfMainBody(buf, row->par()));
1148 for (pos_type p = first; p <= last; ++p) {
1149 // last, because the end is ignored!
1151 if (row->par()->isHfill(p)) {
1159 // like NumberOfHfills, but only those in the manual label!
1160 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1162 pos_type last = rowLast(row);
1163 pos_type first = row->pos();
1164 if (first) { /* hfill *DO* count at the beginning
1166 while (first < last && row->par()->isHfill(first))
1170 last = min(last, beginningOfMainBody(buf, row->par()));
1172 for (pos_type p = first; p < last; ++p) {
1173 // last, because the end is ignored!
1174 if (row->par()->isHfill(p)) {
1182 // returns true, if a expansion is needed.
1183 // Rules are given by LaTeX
1184 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1187 // by the way, is it a hfill?
1188 if (!row_ptr->par()->isHfill(pos))
1191 // at the end of a row it does not count
1192 // unless another hfill exists on the line
1193 if (pos >= rowLast(row_ptr)) {
1194 pos_type i = row_ptr->pos();
1195 while (i < pos && !row_ptr->par()->isHfill(i)) {
1203 // at the beginning of a row it does not count, if it is not
1204 // the first row of a paragaph
1205 if (!row_ptr->pos())
1208 // in some labels it does not count
1209 if (textclasslist.Style(buf->params.textclass,
1210 row_ptr->par()->getLayout()).margintype
1212 && pos < beginningOfMainBody(buf, row_ptr->par()))
1215 // if there is anything between the first char of the row and
1216 // the sepcified position that is not a newline and not a hfill,
1217 // the hfill will count, otherwise not
1218 pos_type i = row_ptr->pos();
1219 while (i < pos && (row_ptr->par()->isNewline(i)
1220 || row_ptr->par()->isHfill(i)))
1227 LColor::color LyXText::backgroundColor()
1230 return inset_owner->backgroundColor();
1232 return LColor::background;
1235 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1237 /* get the maximum ascent and the maximum descent */
1240 float layoutasc = 0;
1241 float layoutdesc = 0;
1244 Inset * tmpinset = 0;
1246 /* ok , let us initialize the maxasc and maxdesc value.
1247 * This depends in LaTeX of the font of the last character
1248 * in the paragraph. The hack below is necessary because
1249 * of the possibility of open footnotes */
1251 /* Correction: only the fontsize count. The other properties
1252 are taken from the layoutfont. Nicer on the screen :) */
1253 Paragraph * par = row_ptr->par();
1254 Paragraph * firstpar = row_ptr->par();
1256 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1257 firstpar->getLayout());
1259 // as max get the first character of this row then it can increes but not
1260 // decrees the height. Just some point to start with so we don't have to
1261 // do the assignment below too often.
1262 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1263 LyXFont::FONT_SIZE const tmpsize = font.size();
1264 font = getLayoutFont(bview->buffer(), par);
1265 LyXFont::FONT_SIZE const size = font.size();
1266 font.setSize(tmpsize);
1268 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1270 float spacing_val = 1.0;
1271 if (!row_ptr->par()->params().spacing().isDefault()) {
1272 spacing_val = row_ptr->par()->params().spacing().getValue();
1274 spacing_val = bview->buffer()->params.spacing.getValue();
1276 //lyxerr << "spacing_val = " << spacing_val << endl;
1278 int maxasc = int(lyxfont::maxAscent(font) *
1279 layout.spacing.getValue() *
1281 int maxdesc = int(lyxfont::maxDescent(font) *
1282 layout.spacing.getValue() *
1284 pos_type const pos_end = rowLast(row_ptr);
1288 // Check if any insets are larger
1289 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1290 if (row_ptr->par()->isInset(pos)) {
1291 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1292 tmpinset = row_ptr->par()->getInset(pos);
1294 #if 1 // this is needed for deep update on initialitation
1295 tmpinset->update(bview, tmpfont);
1297 asc = tmpinset->ascent(bview, tmpfont);
1298 desc = tmpinset->descent(bview, tmpfont);
1299 maxwidth += tmpinset->width(bview, tmpfont);
1300 maxasc = max(maxasc, asc);
1301 maxdesc = max(maxdesc, desc);
1304 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1308 // Check if any custom fonts are larger (Asger)
1309 // This is not completely correct, but we can live with the small,
1310 // cosmetic error for now.
1311 LyXFont::FONT_SIZE maxsize =
1312 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1313 if (maxsize > font.size()) {
1314 font.setSize(maxsize);
1316 asc = lyxfont::maxAscent(font);
1317 desc = lyxfont::maxDescent(font);
1324 // This is nicer with box insets:
1328 row_ptr->ascent_of_text(maxasc);
1330 // is it a top line?
1331 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1333 // some parksips VERY EASY IMPLEMENTATION
1334 if (bview->buffer()->params.paragraph_separation ==
1335 BufferParams::PARSEP_SKIP)
1337 if (layout.isParagraph()
1338 && firstpar->getDepth() == 0
1339 && firstpar->previous())
1341 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1342 } else if (firstpar->previous() &&
1343 textclasslist.Style(bview->buffer()->params.textclass,
1344 firstpar->previous()->
1345 getLayout()).isParagraph() &&
1346 firstpar->previous()->getDepth() == 0)
1348 // is it right to use defskip here too? (AS)
1349 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1353 // the paper margins
1354 if (!row_ptr->par()->previous() && bv_owner)
1355 maxasc += LYX_PAPER_MARGIN;
1357 // add the vertical spaces, that the user added
1358 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1360 // do not forget the DTP-lines!
1361 // there height depends on the font of the nearest character
1362 if (firstpar->params().lineTop())
1363 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1366 // and now the pagebreaks
1367 if (firstpar->params().pagebreakTop())
1368 maxasc += 3 * defaultHeight();
1370 // This is special code for the chapter, since the label of this
1371 // layout is printed in an extra row
1372 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1373 && bview->buffer()->params.secnumdepth >= 0)
1375 float spacing_val = 1.0;
1376 if (!row_ptr->par()->params().spacing().isDefault()) {
1377 spacing_val = row_ptr->par()->params().spacing().getValue();
1379 spacing_val = bview->buffer()->params.spacing.getValue();
1382 labeladdon = int(lyxfont::maxDescent(labelfont) *
1383 layout.spacing.getValue() *
1385 + int(lyxfont::maxAscent(labelfont) *
1386 layout.spacing.getValue() *
1390 // special code for the top label
1391 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1392 || layout.labeltype == LABEL_BIBLIO
1393 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1394 && row_ptr->par()->isFirstInSequence()
1395 && !row_ptr->par()->getLabelstring().empty())
1397 float spacing_val = 1.0;
1398 if (!row_ptr->par()->params().spacing().isDefault()) {
1399 spacing_val = row_ptr->par()->params().spacing().getValue();
1401 spacing_val = bview->buffer()->params.spacing.getValue();
1405 (lyxfont::maxAscent(labelfont) *
1406 layout.spacing.getValue() *
1408 +(lyxfont::maxDescent(labelfont) *
1409 layout.spacing.getValue() *
1411 + layout.topsep * defaultHeight()
1412 + layout.labelbottomsep * defaultHeight());
1415 // and now the layout spaces, for example before and after a section,
1416 // or between the items of a itemize or enumerate environment
1418 if (!firstpar->params().pagebreakTop()) {
1419 Paragraph * prev = row_ptr->par()->previous();
1421 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1422 if (prev && prev->getLayout() == firstpar->getLayout() &&
1423 prev->getDepth() == firstpar->getDepth() &&
1424 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1426 layoutasc = (layout.itemsep * defaultHeight());
1427 } else if (row_ptr->previous()) {
1428 tmptop = layout.topsep;
1430 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1431 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1432 row_ptr->previous()->par()->
1433 getLayout()).bottomsep;
1436 layoutasc = (tmptop * defaultHeight());
1437 } else if (row_ptr->par()->params().lineTop()) {
1438 tmptop = layout.topsep;
1441 layoutasc = (tmptop * defaultHeight());
1444 prev = row_ptr->par()->outerHook();
1446 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1447 prev->getLayout()).parsep * defaultHeight());
1449 if (firstpar->previous() &&
1450 firstpar->previous()->getDepth() == 0 &&
1451 firstpar->previous()->getLayout() !=
1452 firstpar->getLayout())
1455 } else if (firstpar->previous()) {
1456 maxasc += int(layout.parsep * defaultHeight());
1462 // is it a bottom line?
1463 if (row_ptr->par() == par
1464 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1466 // the paper margins
1467 if (!par->next() && bv_owner)
1468 maxdesc += LYX_PAPER_MARGIN;
1470 // add the vertical spaces, that the user added
1471 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1473 // do not forget the DTP-lines!
1474 // there height depends on the font of the nearest character
1475 if (firstpar->params().lineBottom())
1476 maxdesc += 2 * lyxfont::ascent('x',
1477 getFont(bview->buffer(),
1479 max(pos_type(0), par->size() - 1)));
1481 // and now the pagebreaks
1482 if (firstpar->params().pagebreakBottom())
1483 maxdesc += 3 * defaultHeight();
1485 // and now the layout spaces, for example before and after
1486 // a section, or between the items of a itemize or enumerate
1488 if (!firstpar->params().pagebreakBottom()
1489 && row_ptr->par()->next()) {
1490 Paragraph * nextpar = row_ptr->par()->next();
1491 Paragraph * comparepar = row_ptr->par();
1495 if (comparepar->getDepth() > nextpar->getDepth()) {
1496 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1497 comparepar->getLayout()).bottomsep * defaultHeight());
1498 comparepar = comparepar->depthHook(nextpar->getDepth());
1499 if (comparepar->getLayout()!= nextpar->getLayout()
1500 || nextpar->getLabelWidthString() !=
1501 comparepar->getLabelWidthString())
1503 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1504 comparepar->getLayout()).bottomsep * defaultHeight());
1506 if (unusual > usual)
1507 layoutdesc = unusual;
1510 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1512 if (comparepar->getLayout()!= nextpar->getLayout()
1513 || nextpar->getLabelWidthString() !=
1514 comparepar->getLabelWidthString())
1515 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1516 comparepar->getLayout()).bottomsep * defaultHeight());
1521 // incalculate the layout spaces
1522 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1523 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1525 // calculate the new height of the text
1526 height -= row_ptr->height();
1528 row_ptr->height(maxasc + maxdesc + labeladdon);
1529 row_ptr->baseline(maxasc + labeladdon);
1531 height += row_ptr->height();
1533 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1535 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1537 row_ptr->width(int(maxwidth + x));
1540 width = max(0,workWidth(bview));
1542 if (r->width() > width)
1550 /* Appends the implicit specified paragraph behind the specified row,
1551 * start at the implicit given position */
1552 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1554 bool not_ready = true;
1556 // The last character position of a paragraph is an invariant so we can
1557 // safely get it here. (Asger)
1558 pos_type const lastposition = row->par()->size();
1560 // Get the next breakpoint
1561 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1565 // Insert the new row
1566 if (z < lastposition) {
1568 insertRow(row, row->par(), z);
1575 // Set the dimensions of the row
1576 // fixed fill setting now by calling inset->update() in
1577 // SingleWidth when needed!
1578 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1579 setHeightOfRow(bview, tmprow);
1581 } while (not_ready);
1585 void LyXText::breakAgain(BufferView * bview, Row * row) const
1587 bool not_ready = true;
1590 // get the next breakpoint
1591 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1594 if (z < row->par()->size()) {
1595 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1598 insertRow(row, row->par(), z);
1604 if (row->pos() == z)
1605 not_ready = false; // the rest will not change
1611 /* if there are some rows too much, delete them */
1612 /* only if you broke the whole paragraph! */
1613 Row * tmprow2 = row;
1614 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1615 tmprow2 = tmprow2->next();
1617 while (tmprow2 != row) {
1618 tmprow2 = tmprow2->previous();
1619 removeRow(tmprow2->next());
1624 /* set the dimensions of the row */
1625 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1626 setHeightOfRow(bview, tmprow);
1627 } while (not_ready);
1631 // this is just a little changed version of break again
1632 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1634 // get the next breakpoint
1635 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1638 if (z < row->par()->size()) {
1640 || (row->next() && row->next()->par() != row->par())) {
1641 /* insert a new row */
1643 insertRow(row, row->par(), z);
1649 if (row->pos() != z)
1653 // if there are some rows too much, delete them
1654 // only if you broke the whole paragraph!
1655 Row * tmprow2 = row;
1656 while (tmprow2->next()
1657 && tmprow2->next()->par() == row->par()) {
1658 tmprow2 = tmprow2->next();
1660 while (tmprow2 != row) {
1661 tmprow2 = tmprow2->previous();
1662 removeRow(tmprow2->next());
1666 // set the dimensions of the row
1667 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1668 setHeightOfRow(bview, tmprow);
1672 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1674 LyXLayout const & layout =
1675 textclasslist.Style(bview->buffer()->params.textclass,
1676 cursor.par()->getLayout());
1678 // this is only allowed, if the current paragraph is not empty or caption
1679 if ((cursor.par()->size() <= 0)
1680 && layout.labeltype!= LABEL_SENSITIVE)
1683 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1685 // Always break behind a space
1687 // It is better to erase the space (Dekel)
1688 if (cursor.pos() < cursor.par()->size()
1689 && cursor.par()->isLineSeparator(cursor.pos()))
1690 cursor.par()->erase(cursor.pos());
1691 // cursor.pos(cursor.pos() + 1);
1693 // break the paragraph
1697 keep_layout = layout.isEnvironment();
1698 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1701 // well this is the caption hack since one caption is really enough
1702 if (layout.labeltype == LABEL_SENSITIVE) {
1704 // set to standard-layout
1705 cursor.par()->setLayout(0);
1707 // set to standard-layout
1708 cursor.par()->next()->setLayout(0);
1711 /* if the cursor is at the beginning of a row without prior newline,
1713 * This touches only the screen-update. Otherwise we would may have
1714 * an empty row on the screen */
1715 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1716 && cursor.row()->pos() == cursor.pos()) {
1720 status(bview, LyXText::NEED_MORE_REFRESH);
1721 refresh_row = cursor.row();
1722 refresh_y = cursor.y() - cursor.row()->baseline();
1724 // Do not forget the special right address boxes
1725 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1726 while (refresh_row->previous() &&
1727 refresh_row->previous()->par() == refresh_row->par()) {
1728 refresh_row = refresh_row->previous();
1729 refresh_y -= refresh_row->height();
1732 removeParagraph(cursor.row());
1734 // set the dimensions of the cursor row
1735 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1737 setHeightOfRow(bview, cursor.row());
1739 while (cursor.par()->next()->size()
1740 && cursor.par()->next()->isNewline(0))
1741 cursor.par()->next()->erase(0);
1743 insertParagraph(bview, cursor.par()->next(), cursor.row());
1745 updateCounters(bview, cursor.row()->previous());
1747 /* This check is necessary. Otherwise the new empty paragraph will
1748 * be deleted automatically. And it is more friendly for the user! */
1750 setCursor(bview, cursor.par()->next(), 0);
1752 setCursor(bview, cursor.par(), 0);
1754 if (cursor.row()->next())
1755 breakAgain(bview, cursor.row()->next());
1761 // Just a macro to make some thing easier.
1762 void LyXText::redoParagraph(BufferView * bview) const
1765 redoParagraphs(bview, cursor, cursor.par()->next());
1766 setCursorIntern(bview, cursor.par(), cursor.pos());
1770 /* insert a character, moves all the following breaks in the
1771 * same Paragraph one to the right and make a rebreak */
1772 void LyXText::insertChar(BufferView * bview, char c)
1774 setUndo(bview, Undo::INSERT,
1775 cursor.par(), cursor.par()->next());
1777 // When the free-spacing option is set for the current layout,
1778 // disable the double-space checking
1780 bool const freeSpacing =
1781 textclasslist.Style(bview->buffer()->params.textclass,
1782 cursor.row()->par()->getLayout()).free_spacing ||
1783 cursor.row()->par()->isFreeSpacing();
1786 if (lyxrc.auto_number) {
1787 static string const number_operators = "+-/*";
1788 static string const number_unary_operators = "+-";
1789 static string const number_seperators = ".,:";
1791 if (current_font.number() == LyXFont::ON) {
1792 if (!IsDigit(c) && !contains(number_operators, c) &&
1793 !(contains(number_seperators, c) &&
1794 cursor.pos() >= 1 &&
1795 cursor.pos() < cursor.par()->size() &&
1796 getFont(bview->buffer(),
1798 cursor.pos()).number() == LyXFont::ON &&
1799 getFont(bview->buffer(),
1801 cursor.pos() - 1).number() == LyXFont::ON)
1803 number(bview); // Set current_font.number to OFF
1804 } else if (IsDigit(c) &&
1805 real_current_font.isVisibleRightToLeft()) {
1806 number(bview); // Set current_font.number to ON
1808 if (cursor.pos() > 0) {
1809 char const c = cursor.par()->getChar(cursor.pos() - 1);
1810 if (contains(number_unary_operators, c) &&
1811 (cursor.pos() == 1 ||
1812 cursor.par()->isSeparator(cursor.pos() - 2) ||
1813 cursor.par()->isNewline(cursor.pos() - 2) )
1815 setCharFont(bview->buffer(),
1819 } else if (contains(number_seperators, c) &&
1820 cursor.pos() >= 2 &&
1821 getFont(bview->buffer(),
1823 cursor.pos() - 2).number() == LyXFont::ON) {
1824 setCharFont(bview->buffer(),
1834 /* First check, if there will be two blanks together or a blank at
1835 the beginning of a paragraph.
1836 I decided to handle blanks like normal characters, the main
1837 difference are the special checks when calculating the row.fill
1838 (blank does not count at the end of a row) and the check here */
1840 // The bug is triggered when we type in a description environment:
1841 // The current_font is not changed when we go from label to main text
1842 // and it should (along with realtmpfont) when we type the space.
1843 // CHECK There is a bug here! (Asger)
1845 LyXFont realtmpfont = real_current_font;
1846 LyXFont rawtmpfont = current_font; /* store the current font.
1847 * This is because of the use
1848 * of cursor movements. The moving
1849 * cursor would refresh the
1852 // Get the font that is used to calculate the baselineskip
1853 pos_type const lastpos = cursor.par()->size();
1854 LyXFont rawparfont =
1855 cursor.par()->getFontSettings(bview->buffer()->params,
1858 bool jumped_over_space = false;
1860 if (!freeSpacing && IsLineSeparatorChar(c)) {
1861 if ((cursor.pos() > 0
1862 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1863 || (cursor.pos() > 0
1864 && cursor.par()->isNewline(cursor.pos() - 1))
1865 || (cursor.pos() == 0)) {
1866 static bool sent_space_message = false;
1867 if (!sent_space_message) {
1868 if (cursor.pos() == 0)
1869 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1871 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1872 sent_space_message = true;
1877 } else if (IsNewlineChar(c)) {
1878 if (cursor.par() == cursor.par()
1879 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1883 /* No newline at first position
1884 * of a paragraph or behind labels.
1885 * TeX does not allow that. */
1887 if (cursor.pos() < cursor.par()->size() &&
1888 cursor.par()->isLineSeparator(cursor.pos()))
1889 // newline always after a blank!
1891 cursor.row()->fill(-1); // to force a new break
1894 // the display inset stuff
1895 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1896 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1897 if (inset && (inset->display() || inset->needFullRow())) {
1898 // force a new break
1899 cursor.row()->fill(-1); // to force a new break
1903 // get the cursor row fist
1904 Row * row = cursor.row();
1905 int y = cursor.y() - row->baseline();
1906 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1907 * already insertet the character */
1908 cursor.par()->insertChar(cursor.pos(), c);
1909 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1911 if (!jumped_over_space) {
1912 // refresh the positions
1914 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1915 tmprow = tmprow->next();
1916 tmprow->pos(tmprow->pos() + 1);
1920 // Is there a break one row above
1921 if ((cursor.par()->isLineSeparator(cursor.pos())
1922 || cursor.par()->isNewline(cursor.pos())
1923 || cursor.row()->fill() == -1)
1924 && row->previous() && row->previous()->par() == row->par()) {
1925 pos_type z = nextBreakPoint(bview,
1928 if (z >= row->pos()) {
1931 // set the dimensions of the row above
1932 row->previous()->fill(fill(bview,
1936 setHeightOfRow(bview, row->previous());
1938 y -= row->previous()->height();
1940 refresh_row = row->previous();
1941 status(bview, LyXText::NEED_MORE_REFRESH);
1943 breakAgainOneRow(bview, row);
1945 current_font = rawtmpfont;
1946 real_current_font = realtmpfont;
1947 setCursor(bview, cursor.par(), cursor.pos() + 1,
1948 false, cursor.boundary());
1949 // cursor MUST be in row now.
1951 if (row->next() && row->next()->par() == row->par())
1952 need_break_row = row->next();
1956 // check, wether the last characters font has changed.
1957 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1958 && rawparfont != rawtmpfont)
1959 redoHeightOfParagraph(bview, cursor);
1966 // recalculate the fill of the row
1967 if (row->fill() >= 0) /* needed because a newline
1968 * will set fill to -1. Otherwise
1969 * we would not get a rebreak! */
1970 row->fill(fill(bview, row, workWidth(bview)));
1971 if (row->fill() < 0) {
1974 refresh_x = cursor.x();
1975 refresh_pos = cursor.pos();
1976 status(bview, LyXText::NEED_MORE_REFRESH);
1977 breakAgainOneRow(bview, row);
1978 // will the cursor be in another row now?
1979 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1980 if (row->next() && row->next()->par() == row->par())
1981 // this should always be true
1983 breakAgainOneRow(bview, row);
1985 current_font = rawtmpfont;
1986 real_current_font = realtmpfont;
1988 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1990 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1991 != cursor.boundary())
1992 setCursor(bview, cursor.par(), cursor.pos(), false,
1993 !cursor.boundary());
1994 if (row->next() && row->next()->par() == row->par())
1995 need_break_row = row->next();
2000 refresh_x = cursor.x();
2002 refresh_pos = cursor.pos();
2004 int const tmpheight = row->height();
2005 setHeightOfRow(bview, row);
2006 if (tmpheight == row->height())
2007 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2009 status(bview, LyXText::NEED_MORE_REFRESH);
2011 current_font = rawtmpfont;
2012 real_current_font = realtmpfont;
2013 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2017 // check, wether the last characters font has changed.
2018 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2019 && rawparfont != rawtmpfont) {
2020 redoHeightOfParagraph(bview, cursor);
2022 // now the special right address boxes
2023 if (textclasslist.Style(bview->buffer()->params.textclass,
2024 cursor.par()->getLayout()).margintype
2025 == MARGIN_RIGHT_ADDRESS_BOX) {
2026 redoDrawingOfParagraph(bview, cursor);
2034 void LyXText::charInserted()
2036 // Here we could call FinishUndo for every 20 characters inserted.
2037 // This is from my experience how emacs does it.
2038 static unsigned int counter;
2048 void LyXText::prepareToPrint(BufferView * bview,
2049 Row * row, float & x,
2050 float & fill_separator,
2052 float & fill_label_hfill,
2058 float w = row->fill();
2060 fill_label_hfill = 0;
2062 fill_label_hfill = 0;
2065 row->par()->isRightToLeftPar(bview->buffer()->params);
2067 x = (workWidth(bview) > 0)
2068 ? rightMargin(bview->buffer(), row) : 0;
2070 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2072 // is there a manual margin with a manual label
2073 if (textclasslist.Style(bview->buffer()->params.textclass,
2074 row->par()->getLayout()).margintype == MARGIN_MANUAL
2075 && textclasslist.Style(bview->buffer()->params.textclass,
2076 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2078 /* one more since labels are left aligned */
2079 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2080 if (nlh && !row->par()->getLabelWidthString().empty()) {
2081 fill_label_hfill = labelFill(bview, row) / nlh;
2085 // are there any hfills in the row?
2086 float const nh = numberOfHfills(bview->buffer(), row);
2090 fill_hfill = w / nh;
2092 // is it block, flushleft or flushright?
2093 // set x how you need it
2095 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2096 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2098 align = row->par()->params().align();
2101 // center displayed insets
2103 if (row->par()->isInset(row->pos())
2104 && (inset=row->par()->getInset(row->pos()))
2105 && (inset->display())) // || (inset->scroll() < 0)))
2106 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2107 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2110 case LYX_ALIGN_BLOCK:
2111 ns = numberOfSeparators(bview->buffer(), row);
2112 if (ns && row->next() && row->next()->par() == row->par() &&
2113 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2114 && !(row->next()->par()->isInset(row->next()->pos())
2115 && row->next()->par()->getInset(row->next()->pos())
2116 && row->next()->par()->getInset(row->next()->pos())->display())
2119 fill_separator = w / ns;
2120 } else if (is_rtl) {
2124 case LYX_ALIGN_RIGHT:
2127 case LYX_ALIGN_CENTER:
2135 computeBidiTables(bview->buffer(), row);
2137 pos_type main_body =
2138 beginningOfMainBody(bview->buffer(), row->par());
2139 pos_type last = rowLast(row);
2141 if (main_body > 0 &&
2142 (main_body-1 > last ||
2143 !row->par()->isLineSeparator(main_body-1))) {
2144 LyXLayout const & layout =
2145 textclasslist.Style(bview->buffer()->params.textclass,
2146 row->par()->getLayout());
2147 x += lyxfont::width(layout.labelsep,
2148 getLabelFont(bview->buffer(), row->par()));
2149 if (main_body-1 <= last)
2150 x += fill_label_hfill;
2155 /* important for the screen */
2158 /* the cursor set functions have a special mechanism. When they
2159 * realize, that you left an empty paragraph, they will delete it.
2160 * They also delete the corresponding row */
2162 void LyXText::cursorRightOneWord(BufferView * bview) const
2164 // treat floats, HFills and Insets as words
2165 LyXCursor tmpcursor = cursor;
2166 // CHECK See comment on top of text.C
2168 if (tmpcursor.pos() == tmpcursor.par()->size()
2169 && tmpcursor.par()->next()) {
2170 tmpcursor.par(tmpcursor.par()->next());
2175 // Skip through initial nonword stuff.
2176 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2177 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2178 // printf("Current pos1 %d", tmpcursor.pos()) ;
2179 tmpcursor.pos(tmpcursor.pos() + 1);
2182 // Advance through word.
2183 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2184 tmpcursor.par()->isWord( tmpcursor.pos())) {
2185 // printf("Current pos2 %d", tmpcursor.pos()) ;
2186 tmpcursor.pos(tmpcursor.pos() + 1);
2190 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2194 void LyXText::cursorTab(BufferView * bview) const
2196 LyXCursor tmpcursor = cursor;
2197 while (tmpcursor.pos() < tmpcursor.par()->size()
2198 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2199 tmpcursor.pos(tmpcursor.pos() + 1);
2201 if (tmpcursor.pos() == tmpcursor.par()->size()){
2202 if (tmpcursor.par()->next()) {
2203 tmpcursor.par(tmpcursor.par()->next());
2207 tmpcursor.pos(tmpcursor.pos() + 1);
2208 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2212 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2213 of prior word, not to end of next prior word. */
2215 void LyXText::cursorLeftOneWord(BufferView * bview) const
2217 LyXCursor tmpcursor = cursor;
2218 cursorLeftOneWord(tmpcursor);
2219 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2222 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2224 // treat HFills, floats and Insets as words
2227 && (cur.par()->isSeparator(cur.pos() - 1)
2228 || cur.par()->isKomma(cur.pos() - 1))
2229 && !(cur.par()->isHfill(cur.pos() - 1)
2230 || cur.par()->isInset(cur.pos() - 1)))
2231 cur.pos(cur.pos() - 1);
2234 && (cur.par()->isInset(cur.pos() - 1)
2235 || cur.par()->isHfill(cur.pos() - 1))) {
2236 cur.pos(cur.pos() - 1);
2237 } else if (!cur.pos()) {
2238 if (cur.par()->previous()){
2239 cur.par(cur.par()->previous());
2240 cur.pos(cur.par()->size());
2242 } else { // Here, cur != 0
2243 while (cur.pos() > 0 &&
2244 cur.par()->isWord(cur.pos()-1) )
2245 cur.pos(cur.pos() - 1);
2249 /* -------> Select current word. This depends on behaviour of
2250 CursorLeftOneWord(), so it is patched as well. */
2251 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2252 word_location const loc) const
2254 // first put the cursor where we wana start to select the word
2257 case WHOLE_WORD_STRICT:
2258 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2259 || cursor.par()->isSeparator(cursor.pos())
2260 || cursor.par()->isKomma(cursor.pos())
2261 || cursor.par()->isSeparator(cursor.pos() -1)
2262 || cursor.par()->isKomma(cursor.pos() -1)) {
2266 // no break here, we go to the next
2269 // Move cursor to the beginning, when not already there.
2270 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2271 && !from.par()->isKomma(from.pos() - 1))
2272 cursorLeftOneWord(from);
2275 // always move the cursor to the beginning of previous word
2276 cursorLeftOneWord(from);
2279 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2285 while (to.pos() < to.par()->size()
2286 && !to.par()->isSeparator(to.pos())
2287 && !to.par()->isKomma(to.pos())
2288 && !to.par()->isHfill(to.pos()) )
2290 to.pos(to.pos() + 1);
2295 void LyXText::selectWord(BufferView * bview, word_location const loc)
2299 getWord(from, to, loc);
2301 setCursor(bview, from.par(), from.pos());
2304 selection.cursor = cursor;
2305 setCursor(bview, to.par(), to.pos() );
2306 setSelection(bview);
2310 /* -------> Select the word currently under the cursor when no
2311 selection is currently set */
2312 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2313 word_location const loc)
2315 if (!selection.set()) {
2316 selectWord(bview, loc);
2317 return selection.set();
2323 // This function is only used by the spellchecker for NextWord().
2324 // It doesn't handle LYX_ACCENTs and probably never will.
2325 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2326 float & value) const
2328 if (the_locking_inset) {
2329 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2331 value += float(cursor.y())/float(height);
2334 #warning Dekel please have a look on this one RTL? (Jug)
2336 // we have to go on checking so move cusor to the right
2337 if (cursor.pos() == cursor.par()->size()) {
2338 if (!cursor.par()->next())
2340 cursor.par(cursor.par()->next());
2343 cursor.pos(cursor.pos() + 1);
2345 Paragraph * tmppar = cursor.par();
2347 // If this is not the very first word, skip rest of
2348 // current word because we are probably in the middle
2349 // of a word if there is text here.
2350 if (cursor.pos() || cursor.par()->previous()) {
2351 while (cursor.pos() < cursor.par()->size()
2352 && cursor.par()->isLetter(cursor.pos()))
2353 cursor.pos(cursor.pos() + 1);
2356 // Now, skip until we have real text (will jump paragraphs)
2357 while ((cursor.par()->size() > cursor.pos()
2358 && (!cursor.par()->isLetter(cursor.pos()))
2359 && (!cursor.par()->isInset(cursor.pos()) ||
2360 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2361 || (cursor.par()->size() == cursor.pos()
2362 && cursor.par()->next()))
2364 if (cursor.pos() == cursor.par()->size()) {
2365 cursor.par(cursor.par()->next());
2368 cursor.pos(cursor.pos() + 1);
2371 // now check if we hit an inset so it has to be a inset containing text!
2372 if (cursor.pos() < cursor.par()->size() &&
2373 cursor.par()->isInset(cursor.pos()))
2376 cursor.par()->getInset(cursor.pos())->edit(bview);
2377 // now call us again to do the above trick
2378 // but obviously we have to start from down below ;)
2379 return bview->text->selectNextWordToSpellcheck(bview, value);
2382 // Update the value if we changed paragraphs
2383 if (cursor.par() != tmppar){
2384 setCursor(bview, cursor.par(), cursor.pos());
2385 value = float(cursor.y())/float(height);
2388 // Start the selection from here
2389 selection.cursor = cursor;
2391 // and find the end of the word (insets like optional hyphens
2392 // and ligature break are part of a word)
2393 while (cursor.pos() < cursor.par()->size()
2394 && (cursor.par()->isLetter(cursor.pos())))
2395 cursor.pos(cursor.pos() + 1);
2397 // Finally, we copy the word to a string and return it
2399 if (selection.cursor.pos() < cursor.pos()) {
2401 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2402 if (!cursor.par()->isInset(i))
2403 str += cursor.par()->getChar(i);
2410 // This one is also only for the spellchecker
2411 void LyXText::selectSelectedWord(BufferView * bview)
2413 if (the_locking_inset) {
2414 the_locking_inset->selectSelectedWord(bview);
2417 // move cursor to the beginning
2418 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2420 // set the sel cursor
2421 selection.cursor = cursor;
2423 // now find the end of the word
2424 while (cursor.pos() < cursor.par()->size()
2425 && (cursor.par()->isLetter(cursor.pos())))
2426 cursor.pos(cursor.pos() + 1);
2428 setCursor(bview, cursor.par(), cursor.pos());
2430 // finally set the selection
2431 setSelection(bview);
2435 /* -------> Delete from cursor up to the end of the current or next word. */
2436 void LyXText::deleteWordForward(BufferView * bview)
2438 if (!cursor.par()->size())
2441 LyXCursor tmpcursor = cursor;
2442 tmpcursor.row(0); //Â ??
2443 selection.set(true); // to avoid deletion
2444 cursorRightOneWord(bview);
2445 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2446 selection.cursor = cursor;
2448 setSelection(bview);
2450 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2451 cutSelection(bview, true, false);
2456 /* -------> Delete from cursor to start of current or prior word. */
2457 void LyXText::deleteWordBackward(BufferView * bview)
2459 if (!cursor.par()->size())
2462 LyXCursor tmpcursor = cursor;
2463 tmpcursor.row(0); // ??
2464 selection.set(true); // to avoid deletion
2465 cursorLeftOneWord(bview);
2466 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2467 selection.cursor = cursor;
2469 setSelection(bview);
2470 cutSelection(bview, true, false);
2475 /* -------> Kill to end of line. */
2476 void LyXText::deleteLineForward(BufferView * bview)
2478 if (!cursor.par()->size())
2479 // Paragraph is empty, so we just go to the right
2482 LyXCursor tmpcursor = cursor;
2483 // We can't store the row over a regular setCursor
2484 // so we set it to 0 and reset it afterwards.
2485 tmpcursor.row(0); //Â ??
2486 selection.set(true); // to avoid deletion
2488 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2489 selection.cursor = cursor;
2491 setSelection(bview);
2492 // What is this test for ??? (JMarc)
2493 if (!selection.set()) {
2494 deleteWordForward(bview);
2496 cutSelection(bview, true, false);
2502 // Change the case of a word at cursor position.
2503 // This function directly manipulates Paragraph::text because there
2504 // is no Paragraph::SetChar currently. I did what I could to ensure
2505 // that it is correct. I guess part of it should be moved to
2506 // Paragraph, but it will have to change for 1.1 anyway. At least
2507 // it does not access outside of the allocated array as the older
2508 // version did. (JMarc)
2509 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2514 if (selection.set()) {
2515 from = selection.start;
2518 getWord(from, to, PARTIAL_WORD);
2519 setCursor(bview, to.par(), to.pos() + 1);
2522 changeRegionCase(bview, from, to, action);
2526 void LyXText::changeRegionCase(BufferView * bview,
2527 LyXCursor const & from,
2528 LyXCursor const & to,
2529 LyXText::TextCase action)
2531 lyx::Assert(from <= to);
2533 setUndo(bview, Undo::FINISH,
2534 from.par(), to.par()->next());
2536 pos_type pos = from.pos();
2537 Paragraph * par = from.par();
2539 while (par && (pos != to.pos() || par != to.par())) {
2540 unsigned char c = par->getChar(pos);
2541 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2543 case text_lowercase:
2546 case text_capitalization:
2548 action = text_lowercase;
2550 case text_uppercase:
2555 par->setChar(pos, c);
2556 checkParagraph(bview, par, pos);
2559 if (pos == par->size()) {
2564 if (to.row() != from.row()) {
2565 refresh_y = from.y() - from.row()->baseline();
2566 refresh_row = from.row();
2567 status(bview, LyXText::NEED_MORE_REFRESH);
2572 void LyXText::transposeChars(BufferView & bview)
2574 Paragraph * tmppar = cursor.par();
2576 setUndo(&bview, Undo::FINISH,
2577 tmppar, tmppar->next());
2579 pos_type tmppos = cursor.pos();
2581 // First decide if it is possible to transpose at all
2583 // We are at the beginning of a paragraph.
2584 if (tmppos == 0) return;
2586 // We are at the end of a paragraph.
2587 if (tmppos == tmppar->size() - 1) return;
2589 unsigned char c1 = tmppar->getChar(tmppos);
2590 unsigned char c2 = tmppar->getChar(tmppos - 1);
2592 if (c1 != Paragraph::META_INSET
2593 && c2 != Paragraph::META_INSET) {
2594 tmppar->setChar(tmppos, c2);
2595 tmppar->setChar(tmppos - 1, c1);
2597 // We should have an implementation that handles insets
2598 // as well, but that will have to come later. (Lgb)
2599 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2603 void LyXText::Delete(BufferView * bview)
2605 // this is a very easy implementation
2607 LyXCursor old_cursor = cursor;
2608 int const old_cur_par_id = old_cursor.par()->id();
2609 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2610 old_cursor.par()->previous()->id() : 0;
2612 // just move to the right
2615 // CHECK Look at the comment here.
2616 // This check is not very good...
2617 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2618 // and that can very well delete the par or par->previous in
2619 // old_cursor. Will a solution where we compare paragraph id's
2621 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2622 == old_cur_par_prev_id
2623 && cursor.par()->id() != old_cur_par_id) {
2624 // delete-empty-paragraph-mechanism has done it
2628 // if you had success make a backspace
2629 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2630 LyXCursor tmpcursor = cursor;
2631 // to make sure undo gets the right cursor position
2632 cursor = old_cursor;
2633 setUndo(bview, Undo::DELETE,
2634 cursor.par(), cursor.par()->next());
2641 void LyXText::backspace(BufferView * bview)
2643 // Get the font that is used to calculate the baselineskip
2644 pos_type lastpos = cursor.par()->size();
2645 LyXFont rawparfont =
2646 cursor.par()->getFontSettings(bview->buffer()->params,
2649 if (cursor.pos() == 0) {
2650 // The cursor is at the beginning of a paragraph,
2651 // so the the backspace will collapse two paragraphs into one.
2653 // we may paste some paragraphs
2655 // is it an empty paragraph?
2658 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2659 // This is an empty paragraph and we delete it just by moving the cursor one step
2660 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2661 // of the paragraph.
2663 if (cursor.par()->previous()) {
2664 Paragraph * tmppar = cursor.par()->previous();
2665 if (cursor.par()->getLayout() == tmppar->getLayout()
2666 && cursor.par()->getAlign() == tmppar->getAlign()) {
2667 // Inherit bottom DTD from the paragraph below.
2668 // (the one we are deleting)
2669 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2670 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2671 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2676 // the layout things can change the height of a row !
2677 int const tmpheight = cursor.row()->height();
2678 setHeightOfRow(bview, cursor.row());
2679 if (cursor.row()->height() != tmpheight) {
2680 refresh_y = cursor.y() - cursor.row()->baseline();
2681 refresh_row = cursor.row();
2682 status(bview, LyXText::NEED_MORE_REFRESH);
2688 if (cursor.par()->previous()) {
2689 setUndo(bview, Undo::DELETE,
2690 cursor.par()->previous(), cursor.par()->next());
2693 Paragraph * tmppar = cursor.par();
2694 Row * tmprow = cursor.row();
2696 // We used to do cursorLeftIntern() here, but it is
2697 // not a good idea since it triggers the auto-delete
2698 // mechanism. So we do a cursorLeftIntern()-lite,
2699 // without the dreaded mechanism. (JMarc)
2700 if (cursor.par()->previous()) {
2701 // steps into the above paragraph.
2702 setCursorIntern(bview, cursor.par()->previous(),
2703 cursor.par()->previous()->size(),
2707 /* Pasting is not allowed, if the paragraphs have different
2708 layout. I think it is a real bug of all other
2709 word processors to allow it. It confuses the user.
2710 Even so with a footnote paragraph and a non-footnote
2711 paragraph. I will not allow pasting in this case,
2712 because the user would be confused if the footnote behaves
2713 different wether it is open or closed.
2715 Correction: Pasting is always allowed with standard-layout
2717 if (cursor.par() != tmppar
2718 && (cursor.par()->getLayout() == tmppar->getLayout()
2719 || tmppar->getLayout() == 0 /*standard*/)
2720 && cursor.par()->getAlign() == tmppar->getAlign())
2722 removeParagraph(tmprow);
2724 cursor.par()->pasteParagraph(bview->buffer()->params);
2726 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2727 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2728 // strangely enough it seems that commenting out the line above removes
2729 // most or all of the segfaults. I will however also try to move the
2730 // two Remove... lines in front of the PasteParagraph too.
2733 cursor.pos(cursor.pos() - 1);
2735 status(bview, LyXText::NEED_MORE_REFRESH);
2736 refresh_row = cursor.row();
2737 refresh_y = cursor.y() - cursor.row()->baseline();
2739 // remove the lost paragraph
2740 // This one is not safe, since the paragraph that the tmprow and the
2741 // following rows belong to has been deleted by the PasteParagraph
2742 // above. The question is... could this be moved in front of the
2744 //RemoveParagraph(tmprow);
2745 //RemoveRow(tmprow);
2747 // This rebuilds the rows.
2748 appendParagraph(bview, cursor.row());
2749 updateCounters(bview, cursor.row());
2751 // the row may have changed, block, hfills etc.
2752 setCursor(bview, cursor.par(), cursor.pos(), false);
2755 /* this is the code for a normal backspace, not pasting
2757 setUndo(bview, Undo::DELETE,
2758 cursor.par(), cursor.par()->next());
2759 // We used to do cursorLeftIntern() here, but it is
2760 // not a good idea since it triggers the auto-delete
2761 // mechanism. So we do a cursorLeftIntern()-lite,
2762 // without the dreaded mechanism. (JMarc)
2763 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2764 false, cursor.boundary());
2766 // some insets are undeletable here
2767 if (cursor.par()->isInset(cursor.pos())) {
2768 if (!cursor.par()->getInset(cursor.pos())->deletable())
2770 // force complete redo when erasing display insets
2771 // this is a cruel method but safe..... Matthias
2772 if (cursor.par()->getInset(cursor.pos())->display() ||
2773 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2774 cursor.par()->erase(cursor.pos());
2775 redoParagraph(bview);
2780 Row * row = cursor.row();
2781 int y = cursor.y() - row->baseline();
2783 /* remember that a space at the end of a row doesnt count
2784 * when calculating the fill */
2785 if (cursor.pos() < rowLast(row) ||
2786 !cursor.par()->isLineSeparator(cursor.pos())) {
2787 row->fill(row->fill() + singleWidth(bview,
2792 /* some special code when deleting a newline. This is similar
2793 * to the behavior when pasting paragraphs */
2794 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2795 cursor.par()->erase(cursor.pos());
2796 // refresh the positions
2798 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2799 tmprow = tmprow->next();
2800 tmprow->pos(tmprow->pos() - 1);
2802 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2803 cursor.pos(cursor.pos() - 1);
2805 if (cursor.pos() < cursor.par()->size()
2806 && !cursor.par()->isSeparator(cursor.pos())) {
2807 cursor.par()->insertChar(cursor.pos(), ' ');
2808 setCharFont(bview->buffer(), cursor.par(),
2809 cursor.pos(), current_font);
2810 // refresh the positions
2812 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2813 tmprow = tmprow->next();
2814 tmprow->pos(tmprow->pos() + 1);
2818 cursor.par()->erase(cursor.pos());
2820 // refresh the positions
2822 while (tmprow->next()
2823 && tmprow->next()->par() == row->par()) {
2824 tmprow = tmprow->next();
2825 tmprow->pos(tmprow->pos() - 1);
2828 // delete newlines at the beginning of paragraphs
2829 while (cursor.par()->size() &&
2830 cursor.par()->isNewline(cursor.pos()) &&
2831 cursor.pos() == beginningOfMainBody(bview->buffer(),
2833 cursor.par()->erase(cursor.pos());
2834 // refresh the positions
2836 while (tmprow->next() &&
2837 tmprow->next()->par() == row->par()) {
2838 tmprow = tmprow->next();
2839 tmprow->pos(tmprow->pos() - 1);
2844 // is there a break one row above
2845 if (row->previous() && row->previous()->par() == row->par()) {
2846 z = nextBreakPoint(bview, row->previous(),
2848 if (z >= row->pos()) {
2851 Row * tmprow = row->previous();
2853 // maybe the current row is now empty
2854 if (row->pos() >= row->par()->size()) {
2859 breakAgainOneRow(bview, row);
2860 if (row->next() && row->next()->par() == row->par())
2861 need_break_row = row->next();
2866 // set the dimensions of the row above
2867 y -= tmprow->height();
2868 tmprow->fill(fill(bview, tmprow,
2870 setHeightOfRow(bview, tmprow);
2873 refresh_row = tmprow;
2874 status(bview, LyXText::NEED_MORE_REFRESH);
2875 setCursor(bview, cursor.par(), cursor.pos(),
2876 false, cursor.boundary());
2877 //current_font = rawtmpfont;
2878 //real_current_font = realtmpfont;
2879 // check, whether the last character's font has changed.
2881 cursor.par()->getFontSettings(bview->buffer()->params,
2882 cursor.par()->size() - 1))
2883 redoHeightOfParagraph(bview, cursor);
2888 // break the cursor row again
2889 if (row->next() && row->next()->par() == row->par() &&
2890 (rowLast(row) == row->par()->size() - 1 ||
2891 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2893 /* it can happen that a paragraph loses one row
2894 * without a real breakup. This is when a word
2895 * is to long to be broken. Well, I don t care this
2897 if (rowLast(row) == row->par()->size() - 1)
2898 removeRow(row->next());
2902 status(bview, LyXText::NEED_MORE_REFRESH);
2904 breakAgainOneRow(bview, row);
2905 // will the cursor be in another row now?
2906 if (row->next() && row->next()->par() == row->par() &&
2907 rowLast(row) <= cursor.pos()) {
2909 breakAgainOneRow(bview, row);
2912 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2914 if (row->next() && row->next()->par() == row->par())
2915 need_break_row = row->next();
2919 // set the dimensions of the row
2920 row->fill(fill(bview, row, workWidth(bview)));
2921 int const tmpheight = row->height();
2922 setHeightOfRow(bview, row);
2923 if (tmpheight == row->height())
2924 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2926 status(bview, LyXText::NEED_MORE_REFRESH);
2929 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2933 // current_font = rawtmpfont;
2934 // real_current_font = realtmpfont;
2936 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2937 != cursor.boundary())
2938 setCursor(bview, cursor.par(), cursor.pos(), false,
2939 !cursor.boundary());
2941 lastpos = cursor.par()->size();
2942 if (cursor.pos() == lastpos)
2943 setCurrentFont(bview);
2945 // check, whether the last characters font has changed.
2947 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2948 redoHeightOfParagraph(bview, cursor);
2950 // now the special right address boxes
2951 if (textclasslist.Style(bview->buffer()->params.textclass,
2952 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2953 redoDrawingOfParagraph(bview, cursor);
2959 bool LyXText::paintRowBackground(DrawRowParams & p)
2961 bool clear_area = true;
2963 LyXFont font(LyXFont::ALL_SANE);
2965 pos_type const last = rowLastPrintable(p.row);
2967 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2968 && p.row->par()->isInset(p.row->pos())) {
2969 inset = p.row->par()->getInset(p.row->pos());
2971 clear_area = inset->doClearArea();
2981 int const y = p.yo < 0 ? 0 : p.yo;
2982 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
2983 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
2990 int h = p.row->baseline() - inset->ascent(p.bv, font);
2992 // first clear the whole row above the inset!
2994 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
2997 // clear the space below the inset!
2998 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
2999 if ((p.row->height() - h) > 0) {
3000 p.pain->fillRectangle(p.xo, p.yo + h,
3001 p.width, p.row->height() - h, backgroundColor());
3004 // clear the space behind the inset, if needed
3005 if (!inset->display() && !inset->needFullRow()) {
3006 int const xp = int(p.x) + inset->width(p.bv, font);
3007 if (p.width - xp > 0) {
3008 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3009 p.row->height(), backgroundColor());
3017 void LyXText::paintRowSelection(DrawRowParams & p)
3019 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3021 // the current selection
3022 int const startx = selection.start.x();
3023 int const endx = selection.end.x();
3024 int const starty = selection.start.y();
3025 int const endy = selection.end.y();
3026 Row const * startrow = selection.start.row();
3027 Row const * endrow = selection.end.row();
3031 if (bidi_same_direction) {
3035 int h = row->height();
3037 if (startrow == row && endrow == row) {
3038 if (startx < endx) {
3041 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3045 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3047 } else if (startrow == row) {
3048 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3049 int const w = (is_rtl) ? startx : (p.width - startx);
3050 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3051 } else if (endrow == row) {
3052 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3053 int const w = (is_rtl) ? (p.width - endx) : endx;
3054 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3055 } else if (p.y > starty && p.y < endy) {
3056 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3059 } else if (startrow != row && endrow != row) {
3061 int h = row->height();
3062 if (p.y > starty && p.y < endy) {
3063 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3068 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3074 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3076 Buffer const * buffer = p.bv->buffer();
3077 Paragraph * par = row->par();
3078 pos_type main_body = beginningOfMainBody(buffer, par);
3079 pos_type const last = rowLastPrintable(row);
3081 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3082 pos_type pos = vis2log(vpos);
3083 float const old_tmpx = tmpx;
3084 if (main_body > 0 && pos == main_body - 1) {
3085 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3087 LyXFont const lfont = getLabelFont(buffer, par);
3090 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3092 if (par->isLineSeparator(main_body - 1))
3093 tmpx -= singleWidth(p.bv, par, main_body - 1);
3096 if (hfillExpansion(buffer, row, pos)) {
3097 tmpx += singleWidth(p.bv, par, pos);
3098 if (pos >= main_body)
3101 tmpx += p.label_hfill;
3104 else if (par->isSeparator(pos)) {
3105 tmpx += singleWidth(p.bv, par, pos);
3106 if (pos >= main_body)
3107 tmpx += p.separator;
3109 tmpx += singleWidth(p.bv, par, pos);
3112 if ((startrow != row || selection.start.pos() <= pos) &&
3113 (endrow != row || pos < selection.end.pos())) {
3114 // Here we do not use p.x as p.xo was added to p.x.
3115 p.pain->fillRectangle(int(old_tmpx), p.yo,
3116 int(tmpx - old_tmpx + 1),
3117 row->height(), LColor::selection);
3120 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3121 p.pain->fillRectangle(p.xo + int(tmpx),
3122 p.yo, int(p.bv->workWidth() - tmpx),
3123 row->height(), LColor::selection);
3129 void LyXText::paintRowAppendix(DrawRowParams & p)
3131 // FIXME: can be just p.width ?
3132 int const ww = p.bv->workWidth();
3133 Paragraph * firstpar = p.row->par();
3135 if (firstpar->params().appendix()) {
3136 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3137 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3142 void LyXText::paintRowDepthBar(DrawRowParams & p)
3144 Paragraph::depth_type const depth = p.row->par()->getDepth();
3149 Paragraph::depth_type prev_depth = 0;
3150 if (p.row->previous())
3151 prev_depth = p.row->previous()->par()->getDepth();
3152 Paragraph::depth_type next_depth = 0;
3154 next_depth = p.row->next()->par()->getDepth();
3156 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3157 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3158 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3160 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3162 int const w = LYX_PAPER_MARGIN / 5;
3164 if (i > prev_depth) {
3165 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3167 if (i > next_depth) {
3168 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3174 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3176 int const arrow_size = 4;
3177 int const space_size = int(vsp.inPixels(bv));
3179 if (vsp.kind() != VSpace::LENGTH) {
3185 int const min_size = std::max(3 * arrow_size,
3186 lyxfont::maxAscent(font)
3187 + lyxfont::maxDescent(font));
3189 if (vsp.length().len().value() < 0.0)
3192 return std::max(min_size, space_size);
3196 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3197 VSpace const & vsp, int start)
3199 int const arrow_size = 4;
3200 int const size = getLengthMarkerHeight(p.bv, vsp);
3201 int const end = start + size;
3203 // the label to display (if any)
3205 // y-values for top arrow
3207 // y-values for bottom arrow
3209 switch (vsp.kind()) {
3210 case VSpace::LENGTH:
3212 str = prefix + " (" + vsp.asLyXCommand() + ")";
3213 // adding or removing space
3214 bool const added = !(vsp.length().len().value() < 0.0);
3215 ty1 = added ? (start + arrow_size) : start;
3216 ty2 = added ? start : (start + arrow_size);
3217 by1 = added ? (end - arrow_size) : end;
3218 by2 = added ? end : (end - arrow_size);
3221 case VSpace:: VFILL:
3222 str = prefix + " (vertical fill)";
3227 // nothing to draw here
3231 int const leftx = p.xo + leftMargin(p.bv, p.row);
3232 int const midx = leftx + arrow_size;
3233 int const rightx = midx + arrow_size;
3241 font.setColor(LColor::added_space).decSize();
3242 lyxfont::rectText(str, font, w, a, d);
3244 p.pain->rectText(leftx + 2 * arrow_size + 5,
3245 start + ((end - start) / 2) + d,
3251 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3252 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3255 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3256 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3259 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3265 void LyXText::paintFirstRow(DrawRowParams & p)
3267 Paragraph * par = p.row->par();
3268 ParagraphParameters const & parparams = par->params();
3270 // start of appendix?
3271 if (parparams.startOfAppendix()) {
3272 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3277 // think about the margins
3278 if (!p.row->previous() && bv_owner)
3279 y_top += LYX_PAPER_MARGIN;
3281 // draw a top pagebreak
3282 if (parparams.pagebreakTop()) {
3283 int const y = p.yo + y_top + 2*defaultHeight();
3284 p.pain->line(p.xo, y, p.xo + p.width, y,
3285 LColor::pagebreak, Painter::line_onoffdash);
3292 pb_font.setColor(LColor::pagebreak).decSize();
3293 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3294 p.pain->rectText((p.width - w)/2, y + d,
3295 _("Page Break (top)"), pb_font,
3298 y_top += 3 * defaultHeight();
3301 // draw the additional space if needed:
3302 y_top += drawLengthMarker(p, _("Space above"),
3303 parparams.spaceTop(), p.yo + y_top);
3305 Buffer const * buffer = p.bv->buffer();
3307 LyXLayout const & layout =
3308 textclasslist.Style(buffer->params.textclass, par->getLayout());
3310 // think about the parskip
3311 // some parskips VERY EASY IMPLEMENTATION
3312 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3313 if (par->previous()) {
3314 if (layout.latextype == LATEX_PARAGRAPH
3315 && !par->getDepth()) {
3316 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3318 LyXLayout const & playout =
3319 textclasslist.Style(buffer->params.textclass,
3320 par->previous()->getLayout());
3321 if (playout.latextype == LATEX_PARAGRAPH
3322 && !par->previous()->getDepth()) {
3323 // is it right to use defskip here, too? (AS)
3324 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3330 int const ww = p.bv->workWidth();
3333 if (parparams.lineTop()) {
3334 LyXFont font(LyXFont::ALL_SANE);
3335 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3339 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3340 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3341 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3342 LColor::topline, Painter::line_solid,
3343 Painter::line_thick);
3348 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3350 // should we print a label?
3351 if (layout.labeltype >= LABEL_STATIC
3352 && (layout.labeltype != LABEL_STATIC
3353 || layout.latextype != LATEX_ENVIRONMENT
3354 || par->isFirstInSequence())) {
3356 LyXFont font = getLabelFont(buffer, par);
3357 if (!par->getLabelstring().empty()) {
3359 string const str = par->getLabelstring();
3361 // this is special code for the chapter layout. This is
3362 // printed in an extra row and has a pagebreak at
3364 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3365 if (buffer->params.secnumdepth >= 0) {
3366 float spacing_val = 1.0;
3367 if (!parparams.spacing().isDefault()) {
3368 spacing_val = parparams.spacing().getValue();
3370 spacing_val = buffer->params.spacing.getValue();
3374 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3375 + int(layout.parsep) * defaultHeight();
3378 x = ww - leftMargin(p.bv, p.row) -
3379 lyxfont::width(str, font);
3382 p.pain->text(int(x),
3383 p.yo + p.row->baseline() -
3384 p.row->ascent_of_text() - maxdesc,
3389 x = ww - leftMargin(p.bv, p.row)
3390 + lyxfont::width(layout.labelsep, font);
3392 x = p.x - lyxfont::width(layout.labelsep, font)
3393 - lyxfont::width(str, font);
3396 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3399 // the labels at the top of an environment.
3400 // More or less for bibliography
3401 } else if (par->isFirstInSequence() &&
3402 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3403 layout.labeltype == LABEL_BIBLIO ||
3404 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3405 LyXFont font = getLabelFont(buffer, par);
3406 if (!par->getLabelstring().empty()) {
3407 string const str = par->getLabelstring();
3408 float spacing_val = 1.0;
3409 if (!parparams.spacing().isDefault()) {
3410 spacing_val = parparams.spacing().getValue();
3412 spacing_val = buffer->params.spacing.getValue();
3416 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3417 + (layout.labelbottomsep * defaultHeight()));
3420 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3421 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3422 + ww - rightMargin(buffer, p.row) ) / 2;
3423 x -= lyxfont::width(str, font) / 2;
3424 } else if (is_rtl) {
3425 x = ww - leftMargin(p.bv, p.row) -
3426 lyxfont::width(str, font);
3428 p.pain->text(int(x), p.yo + p.row->baseline()
3429 - p.row->ascent_of_text() - maxdesc,
3434 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3435 LyXFont font = getLayoutFont(buffer, par);
3438 x = ww - leftMargin(p.bv, p.row)
3439 + lyxfont::width(layout.labelsep, font);
3441 x = p.x - lyxfont::width(layout.labelsep, font)
3442 - par->bibkey->width(p.bv, font);
3444 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3449 void LyXText::paintLastRow(DrawRowParams & p)
3451 Paragraph * par = p.row->par();
3452 ParagraphParameters const & parparams = par->params();
3453 int y_bottom = p.row->height() - 1;
3455 // think about the margins
3456 if (!p.row->next() && bv_owner)
3457 y_bottom -= LYX_PAPER_MARGIN;
3459 int const ww = p.bv->workWidth();
3461 // draw a bottom pagebreak
3462 if (parparams.pagebreakBottom()) {
3464 pb_font.setColor(LColor::pagebreak).decSize();
3465 int const y = p.yo + y_bottom - 2 * defaultHeight();
3467 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3468 Painter::line_onoffdash);
3473 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3474 p.pain->rectText((ww - w) / 2, y + d,
3475 _("Page Break (bottom)"),
3476 pb_font, backgroundColor(), backgroundColor());
3478 y_bottom -= 3 * defaultHeight();
3481 // draw the additional space if needed:
3482 int const height = getLengthMarkerHeight(p.bv,
3483 parparams.spaceBottom());
3484 y_bottom -= drawLengthMarker(p, _("Space below"),
3485 parparams.spaceBottom(),
3486 p.yo + y_bottom - height);
3488 Buffer const * buffer = p.bv->buffer();
3490 // draw a bottom line
3491 if (parparams.lineBottom()) {
3492 LyXFont font(LyXFont::ALL_SANE);
3493 int const asc = lyxfont::ascent('x',
3494 getFont(buffer, par,
3495 max(pos_type(0), par->size() - 1)));
3499 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3500 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3501 int const y = p.yo + y_bottom;
3502 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3503 Painter::line_thick);
3508 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3509 int const endlabel = par->getEndLabel(buffer->params);
3514 case END_LABEL_FILLED_BOX:
3516 LyXFont const font = getLabelFont(buffer, par);
3517 int const size = int(0.75 * lyxfont::maxAscent(font));
3518 int const y = (p.yo + p.row->baseline()) - size;
3519 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3521 if (p.row->fill() <= size)
3522 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3524 if (endlabel == END_LABEL_BOX) {
3525 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3527 p.pain->fillRectangle(x, y, size, size,
3532 case END_LABEL_STATIC:
3534 LyXFont font(LyXFont::ALL_SANE);
3535 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3536 string const str = textclasslist.
3537 Style(buffer->params.textclass, layout).endlabelstring();
3538 font = getLabelFont(buffer, par);
3539 int const x = is_rtl ?
3540 int(p.x) - lyxfont::width(str, font)
3541 : ww - rightMargin(buffer, p.row) - p.row->fill();
3542 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3545 case END_LABEL_NO_LABEL:
3550 void LyXText::paintRowText(DrawRowParams & p)
3552 Paragraph * par = p.row->par();
3553 Buffer const * buffer = p.bv->buffer();
3555 pos_type const last = rowLastPrintable(p.row);
3556 pos_type main_body =
3557 beginningOfMainBody(buffer, par);
3558 if (main_body > 0 &&
3559 (main_body - 1 > last ||
3560 !par->isLineSeparator(main_body - 1))) {
3564 LyXLayout const & layout =
3565 textclasslist.Style(buffer->params.textclass, par->getLayout());
3567 pos_type vpos = p.row->pos();
3568 while (vpos <= last) {
3569 pos_type pos = vis2log(vpos);
3570 if (main_body > 0 && pos == main_body - 1) {
3571 int const lwidth = lyxfont::width(layout.labelsep,
3572 getLabelFont(buffer, par));
3574 p.x += p.label_hfill + lwidth
3575 - singleWidth(p.bv, par, main_body - 1);
3578 if (par->isHfill(pos)) {
3581 int const y0 = p.yo + p.row->baseline();
3582 int const y1 = y0 - defaultHeight() / 2;
3584 p.pain->line(int(p.x), y1, int(p.x), y0,
3585 LColor::added_space);
3587 if (hfillExpansion(buffer, p.row, pos)) {
3588 int const y2 = (y0 + y1) / 2;
3590 if (pos >= main_body) {
3591 p.pain->line(int(p.x), y2,
3592 int(p.x + p.hfill), y2,
3593 LColor::added_space,
3594 Painter::line_onoffdash);
3597 p.pain->line(int(p.x), y2,
3598 int(p.x + p.label_hfill), y2,
3599 LColor::added_space,
3600 Painter::line_onoffdash);
3601 p.x += p.label_hfill;
3603 p.pain->line(int(p.x), y1,
3605 LColor::added_space);
3609 } else if (par->isSeparator(pos)) {
3610 p.x += singleWidth(p.bv, par, pos);
3611 if (pos >= main_body)
3621 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3622 Row * row, int y, bool cleared)
3624 if (row->height() <= 0) {
3625 lyxerr << "LYX_ERROR: row.height: "
3626 << row->height() << endl;
3632 // set up drawing parameters
3634 p.pain = &bv->painter();
3638 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3639 if (inset_owner && (p.x < 0))
3643 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3644 p.cleared = cleared;
3648 // clear to background if necessary
3649 p.cleared = paintRowBackground(p);
3651 // paint the selection background
3652 if (selection.set()) {
3653 paintRowSelection(p);
3656 // vertical lines for appendix
3657 paintRowAppendix(p);
3659 // environment depth brackets
3660 paintRowDepthBar(p);
3662 // draw any stuff wanted for a first row of a paragraph
3667 // draw any stuff wanted for the last row of a paragraph
3668 if (!row->next() || (row->next()->par() != row->par())) {
3677 int LyXText::defaultHeight() const
3679 LyXFont font(LyXFont::ALL_SANE);
3680 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3684 /* returns the column near the specified x-coordinate of the row
3685 * x is set to the real beginning of this column */
3687 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3688 bool & boundary) const
3691 float fill_separator;
3693 float fill_label_hfill;
3695 prepareToPrint(bview, row, tmpx, fill_separator,
3696 fill_hfill, fill_label_hfill);
3698 pos_type vc = row->pos();
3699 pos_type last = rowLastPrintable(row);
3701 LyXLayout const & layout =
3702 textclasslist.Style(bview->buffer()->params.textclass,
3703 row->par()->getLayout());
3704 bool left_side = false;
3706 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3707 float last_tmpx = tmpx;
3709 if (main_body > 0 &&
3710 (main_body - 1 > last ||
3711 !row->par()->isLineSeparator(main_body - 1)))
3714 while (vc <= last && tmpx <= x) {
3717 if (main_body > 0 && c == main_body-1) {
3718 tmpx += fill_label_hfill +
3719 lyxfont::width(layout.labelsep,
3720 getLabelFont(bview->buffer(), row->par()));
3721 if (row->par()->isLineSeparator(main_body - 1))
3722 tmpx -= singleWidth(bview, row->par(), main_body-1);
3725 if (hfillExpansion(bview->buffer(), row, c)) {
3726 x += singleWidth(bview, row->par(), c);
3730 tmpx += fill_label_hfill;
3732 else if (row->par()->isSeparator(c)) {
3733 tmpx += singleWidth(bview, row->par(), c);
3735 tmpx+= fill_separator;
3737 tmpx += singleWidth(bview, row->par(), c);
3741 if ((tmpx + last_tmpx) / 2 > x) {
3746 if (vc > last + 1) // This shouldn't happen.
3750 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3751 // some speedup if rtl_support=false
3752 && (!row->next() || row->next()->par() != row->par());
3753 bool const rtl = (lastrow)
3754 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3755 : false; // If lastrow is false, we don't need to compute
3756 // the value of rtl.
3758 if (row->pos() > last) // Row is empty?
3761 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3762 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3764 else if (vc == row->pos()) {
3766 if (bidi_level(c) % 2 == 1)
3769 c = vis2log(vc - 1);
3770 bool const rtl = (bidi_level(c) % 2 == 1);
3771 if (left_side == rtl) {
3773 boundary = isBoundary(bview->buffer(), row->par(), c);
3777 if (row->pos() <= last && c > last
3778 && row->par()->isNewline(last)) {
3779 if (bidi_level(last) % 2 == 0)
3780 tmpx -= singleWidth(bview, row->par(), last);
3782 tmpx += singleWidth(bview, row->par(), last);
3792 // returns pointer to a specified row
3793 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3798 Row * tmprow = firstrow;
3801 // find the first row of the specified paragraph
3802 while (tmprow->next() && tmprow->par() != par) {
3803 y += tmprow->height();
3804 tmprow = tmprow->next();
3807 // now find the wanted row
3808 while (tmprow->pos() < pos
3810 && tmprow->next()->par() == par
3811 && tmprow->next()->pos() <= pos) {
3812 y += tmprow->height();
3813 tmprow = tmprow->next();
3820 Row * LyXText::getRowNearY(int & y) const
3822 // If possible we should optimize this method. (Lgb)
3823 Row * tmprow = firstrow;
3826 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3827 tmpy += tmprow->height();
3828 tmprow = tmprow->next();
3831 y = tmpy; // return the real y
3836 int LyXText::getDepth() const
3838 return cursor.par()->getDepth();