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))))
972 // display istn't allowd
973 par->getInset(i)->display(false);
974 x += singleWidth(bview, par, i, c);
975 } else if (par->getInset(i)->display() ||
976 par->getInset(i)->needFullRow())
978 // So break the line here
982 if (IsLineSeparatorChar(par->getChar(i+1)))
985 last_separator = last; // to avoid extra rows
987 last_separator = i - 1;
988 x = width; // this means break
990 x += singleWidth(bview, par, i, c);
991 // we have to check this separately as we could have a
992 // lineseparator and then the algorithm below would prefer
993 // that which IS wrong! We should always break on an inset
994 // if it's too long and not on the last separator.
995 // Maybe the only exeption is insets used as chars but
996 // then we would have to have a special function inside
997 // the inset to tell us this. Till then we leave it as
998 // it is now. (Jug 20020106)
999 if (pos < i && x >= width && last_separator >= 0)
1000 last_separator = i - 1;
1003 if (IsLineSeparatorChar(c))
1005 x += singleWidth(bview, par, i, c);
1008 if (i == main_body) {
1009 x += lyxfont::width(layout.labelsep,
1010 getLabelFont(bview->buffer(), par));
1011 if (par->isLineSeparator(i - 1))
1012 x-= singleWidth(bview, par, i - 1);
1013 int left_margin = labelEnd(bview, row);
1014 if (x < left_margin)
1018 if ((pos+1 < i) && (last_separator < 0) && (x >= width))
1019 last_separator = i - 2;
1020 else if ((pos < i) && (last_separator < 0) && (x >= width))
1021 last_separator = i - 1;
1022 // end of paragraph is always a suitable separator
1023 else if (i == last && x < width)
1027 // well, if last_separator is still 0, the line isn't breakable.
1028 // don't care and cut simply at the end
1029 if (last_separator < 0) {
1033 // manual labels cannot be broken in LaTeX, do not care
1034 if (main_body && last_separator < main_body)
1035 last_separator = main_body - 1;
1037 lyxerr << last_separator << ":" << pos << endl;
1038 return last_separator;
1042 // returns the minimum space a row needs on the screen in pixel
1043 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
1045 if (paper_width < 0)
1049 // get the pure distance
1050 pos_type const last = rowLastPrintable(row);
1052 // special handling of the right address boxes
1053 if (textclasslist.Style(bview->buffer()->params.textclass,
1054 row->par()->getLayout()).margintype
1055 == MARGIN_RIGHT_ADDRESS_BOX)
1057 int const tmpfill = row->fill();
1058 row->fill(0); // the minfill in MarginLeft()
1059 w = leftMargin(bview, row);
1062 w = leftMargin(bview, row);
1064 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1065 row->par()->getLayout());
1066 pos_type const main_body =
1067 beginningOfMainBody(bview->buffer(), row->par());
1068 pos_type i = row->pos();
1071 if (main_body > 0 && i == main_body) {
1072 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1073 if (row->par()->isLineSeparator(i - 1))
1074 w -= singleWidth(bview, row->par(), i - 1);
1075 int left_margin = labelEnd(bview, row);
1076 if (w < left_margin)
1079 w += singleWidth(bview, row->par(), i);
1082 if (main_body > 0 && main_body > last) {
1083 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1084 if (last >= 0 && row->par()->isLineSeparator(last))
1085 w -= singleWidth(bview, row->par(), last);
1086 int const left_margin = labelEnd(bview, row);
1087 if (w < left_margin)
1091 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1096 // returns the minimum space a manual label needs on the screen in pixel
1097 int LyXText::labelFill(BufferView * bview, Row const * row) const
1099 pos_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1100 // -1 because a label ends either with a space that is in the label,
1101 // or with the beginning of a footnote that is outside the label.
1103 // I don't understand this code in depth, but sometimes "last" is
1104 // less than 0 and this causes a crash. This fix seems to work
1105 // correctly, but I bet the real error is elsewhere. The bug is
1106 // triggered when you have an open footnote in a paragraph
1107 // environment with a manual label. (Asger)
1108 if (last < 0) last = 0;
1110 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1115 pos_type i = row->pos();
1117 w += singleWidth(bview, row->par(), i);
1122 if (!row->par()->params().labelWidthString().empty()) {
1123 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1124 getLabelFont(bview->buffer(), row->par())) - w,
1132 // returns the number of separators in the specified row. The separator
1133 // on the very last column doesnt count
1134 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1136 pos_type const last = rowLast(row);
1137 pos_type p = max(row->pos(), beginningOfMainBody(buf, row->par()));
1139 for (; p < last; ++p) {
1140 if (row->par()->isSeparator(p)) {
1148 // returns the number of hfills in the specified row. The LyX-Hfill is
1149 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1150 // ignored. This is *MUCH* more usefull than not to ignore!
1151 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1153 pos_type const last = rowLast(row);
1154 pos_type first = row->pos();
1156 if (first) { /* hfill *DO* count at the beginning
1158 while (first <= last && row->par()->isHfill(first)) {
1163 first = max(first, beginningOfMainBody(buf, row->par()));
1165 for (pos_type p = first; p <= last; ++p) {
1166 // last, because the end is ignored!
1168 if (row->par()->isHfill(p)) {
1176 // like NumberOfHfills, but only those in the manual label!
1177 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1179 pos_type last = rowLast(row);
1180 pos_type first = row->pos();
1181 if (first) { /* hfill *DO* count at the beginning
1183 while (first < last && row->par()->isHfill(first))
1187 last = min(last, beginningOfMainBody(buf, row->par()));
1189 for (pos_type p = first; p < last; ++p) {
1190 // last, because the end is ignored!
1191 if (row->par()->isHfill(p)) {
1199 // returns true, if a expansion is needed.
1200 // Rules are given by LaTeX
1201 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1204 // by the way, is it a hfill?
1205 if (!row_ptr->par()->isHfill(pos))
1208 // at the end of a row it does not count
1209 // unless another hfill exists on the line
1210 if (pos >= rowLast(row_ptr)) {
1211 pos_type i = row_ptr->pos();
1212 while (i < pos && !row_ptr->par()->isHfill(i)) {
1220 // at the beginning of a row it does not count, if it is not
1221 // the first row of a paragaph
1222 if (!row_ptr->pos())
1225 // in some labels it does not count
1226 if (textclasslist.Style(buf->params.textclass,
1227 row_ptr->par()->getLayout()).margintype
1229 && pos < beginningOfMainBody(buf, row_ptr->par()))
1232 // if there is anything between the first char of the row and
1233 // the sepcified position that is not a newline and not a hfill,
1234 // the hfill will count, otherwise not
1235 pos_type i = row_ptr->pos();
1236 while (i < pos && (row_ptr->par()->isNewline(i)
1237 || row_ptr->par()->isHfill(i)))
1244 LColor::color LyXText::backgroundColor()
1247 return inset_owner->backgroundColor();
1249 return LColor::background;
1252 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1254 /* get the maximum ascent and the maximum descent */
1257 float layoutasc = 0;
1258 float layoutdesc = 0;
1261 Inset * tmpinset = 0;
1263 /* ok , let us initialize the maxasc and maxdesc value.
1264 * This depends in LaTeX of the font of the last character
1265 * in the paragraph. The hack below is necessary because
1266 * of the possibility of open footnotes */
1268 /* Correction: only the fontsize count. The other properties
1269 are taken from the layoutfont. Nicer on the screen :) */
1270 Paragraph * par = row_ptr->par();
1271 Paragraph * firstpar = row_ptr->par();
1273 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1274 firstpar->getLayout());
1276 // as max get the first character of this row then it can increes but not
1277 // decrees the height. Just some point to start with so we don't have to
1278 // do the assignment below too often.
1279 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1280 LyXFont::FONT_SIZE const tmpsize = font.size();
1281 font = getLayoutFont(bview->buffer(), par);
1282 LyXFont::FONT_SIZE const size = font.size();
1283 font.setSize(tmpsize);
1285 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1287 float spacing_val = 1.0;
1288 if (!row_ptr->par()->params().spacing().isDefault()) {
1289 spacing_val = row_ptr->par()->params().spacing().getValue();
1291 spacing_val = bview->buffer()->params.spacing.getValue();
1293 //lyxerr << "spacing_val = " << spacing_val << endl;
1295 int maxasc = int(lyxfont::maxAscent(font) *
1296 layout.spacing.getValue() *
1298 int maxdesc = int(lyxfont::maxDescent(font) *
1299 layout.spacing.getValue() *
1301 pos_type const pos_end = rowLast(row_ptr);
1305 // Check if any insets are larger
1306 for (pos_type pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1307 if (row_ptr->par()->isInset(pos)) {
1308 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1309 tmpinset = row_ptr->par()->getInset(pos);
1311 #if 1 // this is needed for deep update on initialitation
1312 tmpinset->update(bview, tmpfont);
1314 asc = tmpinset->ascent(bview, tmpfont);
1315 desc = tmpinset->descent(bview, tmpfont);
1316 maxwidth += tmpinset->width(bview, tmpfont);
1317 maxasc = max(maxasc, asc);
1318 maxdesc = max(maxdesc, desc);
1321 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1325 // Check if any custom fonts are larger (Asger)
1326 // This is not completely correct, but we can live with the small,
1327 // cosmetic error for now.
1328 LyXFont::FONT_SIZE maxsize =
1329 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1330 if (maxsize > font.size()) {
1331 font.setSize(maxsize);
1333 asc = lyxfont::maxAscent(font);
1334 desc = lyxfont::maxDescent(font);
1341 // This is nicer with box insets:
1345 row_ptr->ascent_of_text(maxasc);
1347 // is it a top line?
1348 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1350 // some parksips VERY EASY IMPLEMENTATION
1351 if (bview->buffer()->params.paragraph_separation ==
1352 BufferParams::PARSEP_SKIP)
1354 if (layout.isParagraph()
1355 && firstpar->getDepth() == 0
1356 && firstpar->previous())
1358 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1359 } else if (firstpar->previous() &&
1360 textclasslist.Style(bview->buffer()->params.textclass,
1361 firstpar->previous()->
1362 getLayout()).isParagraph() &&
1363 firstpar->previous()->getDepth() == 0)
1365 // is it right to use defskip here too? (AS)
1366 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1370 // the paper margins
1371 if (!row_ptr->par()->previous() && bv_owner)
1372 maxasc += LYX_PAPER_MARGIN;
1374 // add the vertical spaces, that the user added
1375 maxasc += getLengthMarkerHeight(bview, firstpar->params().spaceTop());
1377 // do not forget the DTP-lines!
1378 // there height depends on the font of the nearest character
1379 if (firstpar->params().lineTop())
1380 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1383 // and now the pagebreaks
1384 if (firstpar->params().pagebreakTop())
1385 maxasc += 3 * defaultHeight();
1387 // This is special code for the chapter, since the label of this
1388 // layout is printed in an extra row
1389 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1390 && bview->buffer()->params.secnumdepth >= 0)
1392 float spacing_val = 1.0;
1393 if (!row_ptr->par()->params().spacing().isDefault()) {
1394 spacing_val = row_ptr->par()->params().spacing().getValue();
1396 spacing_val = bview->buffer()->params.spacing.getValue();
1399 labeladdon = int(lyxfont::maxDescent(labelfont) *
1400 layout.spacing.getValue() *
1402 + int(lyxfont::maxAscent(labelfont) *
1403 layout.spacing.getValue() *
1407 // special code for the top label
1408 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1409 || layout.labeltype == LABEL_BIBLIO
1410 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1411 && row_ptr->par()->isFirstInSequence()
1412 && !row_ptr->par()->getLabelstring().empty())
1414 float spacing_val = 1.0;
1415 if (!row_ptr->par()->params().spacing().isDefault()) {
1416 spacing_val = row_ptr->par()->params().spacing().getValue();
1418 spacing_val = bview->buffer()->params.spacing.getValue();
1422 (lyxfont::maxAscent(labelfont) *
1423 layout.spacing.getValue() *
1425 +(lyxfont::maxDescent(labelfont) *
1426 layout.spacing.getValue() *
1428 + layout.topsep * defaultHeight()
1429 + layout.labelbottomsep * defaultHeight());
1432 // and now the layout spaces, for example before and after a section,
1433 // or between the items of a itemize or enumerate environment
1435 if (!firstpar->params().pagebreakTop()) {
1436 Paragraph * prev = row_ptr->par()->previous();
1438 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1439 if (prev && prev->getLayout() == firstpar->getLayout() &&
1440 prev->getDepth() == firstpar->getDepth() &&
1441 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1443 layoutasc = (layout.itemsep * defaultHeight());
1444 } else if (row_ptr->previous()) {
1445 tmptop = layout.topsep;
1447 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1448 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1449 row_ptr->previous()->par()->
1450 getLayout()).bottomsep;
1453 layoutasc = (tmptop * defaultHeight());
1454 } else if (row_ptr->par()->params().lineTop()) {
1455 tmptop = layout.topsep;
1458 layoutasc = (tmptop * defaultHeight());
1461 prev = row_ptr->par()->outerHook();
1463 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1464 prev->getLayout()).parsep * defaultHeight());
1466 if (firstpar->previous() &&
1467 firstpar->previous()->getDepth() == 0 &&
1468 firstpar->previous()->getLayout() !=
1469 firstpar->getLayout())
1472 } else if (firstpar->previous()) {
1473 maxasc += int(layout.parsep * defaultHeight());
1479 // is it a bottom line?
1480 if (row_ptr->par() == par
1481 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1483 // the paper margins
1484 if (!par->next() && bv_owner)
1485 maxdesc += LYX_PAPER_MARGIN;
1487 // add the vertical spaces, that the user added
1488 maxdesc += getLengthMarkerHeight(bview, firstpar->params().spaceBottom());
1490 // do not forget the DTP-lines!
1491 // there height depends on the font of the nearest character
1492 if (firstpar->params().lineBottom())
1493 maxdesc += 2 * lyxfont::ascent('x',
1494 getFont(bview->buffer(),
1496 max(pos_type(0), par->size() - 1)));
1498 // and now the pagebreaks
1499 if (firstpar->params().pagebreakBottom())
1500 maxdesc += 3 * defaultHeight();
1502 // and now the layout spaces, for example before and after
1503 // a section, or between the items of a itemize or enumerate
1505 if (!firstpar->params().pagebreakBottom()
1506 && row_ptr->par()->next()) {
1507 Paragraph * nextpar = row_ptr->par()->next();
1508 Paragraph * comparepar = row_ptr->par();
1512 if (comparepar->getDepth() > nextpar->getDepth()) {
1513 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1514 comparepar->getLayout()).bottomsep * defaultHeight());
1515 comparepar = comparepar->depthHook(nextpar->getDepth());
1516 if (comparepar->getLayout()!= nextpar->getLayout()
1517 || nextpar->getLabelWidthString() !=
1518 comparepar->getLabelWidthString())
1520 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1521 comparepar->getLayout()).bottomsep * defaultHeight());
1523 if (unusual > usual)
1524 layoutdesc = unusual;
1527 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1529 if (comparepar->getLayout()!= nextpar->getLayout()
1530 || nextpar->getLabelWidthString() !=
1531 comparepar->getLabelWidthString())
1532 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1533 comparepar->getLayout()).bottomsep * defaultHeight());
1538 // incalculate the layout spaces
1539 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1540 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1542 // calculate the new height of the text
1543 height -= row_ptr->height();
1545 row_ptr->height(maxasc + maxdesc + labeladdon);
1546 row_ptr->baseline(maxasc + labeladdon);
1548 height += row_ptr->height();
1550 if (layout.margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1552 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1554 row_ptr->width(int(maxwidth + x));
1557 width = max(0,workWidth(bview));
1559 if (r->width() > width)
1567 /* Appends the implicit specified paragraph behind the specified row,
1568 * start at the implicit given position */
1569 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1571 bool not_ready = true;
1573 // The last character position of a paragraph is an invariant so we can
1574 // safely get it here. (Asger)
1575 pos_type const lastposition = row->par()->size();
1577 // Get the next breakpoint
1578 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1582 // Insert the new row
1583 if (z < lastposition) {
1585 insertRow(row, row->par(), z);
1592 // Set the dimensions of the row
1593 // fixed fill setting now by calling inset->update() in
1594 // SingleWidth when needed!
1595 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1596 setHeightOfRow(bview, tmprow);
1598 } while (not_ready);
1602 void LyXText::breakAgain(BufferView * bview, Row * row) const
1604 bool not_ready = true;
1607 // get the next breakpoint
1608 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1611 if (z < row->par()->size()) {
1612 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1615 insertRow(row, row->par(), z);
1621 if (row->pos() == z)
1622 not_ready = false; // the rest will not change
1628 /* if there are some rows too much, delete them */
1629 /* only if you broke the whole paragraph! */
1630 Row * tmprow2 = row;
1631 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1632 tmprow2 = tmprow2->next();
1634 while (tmprow2 != row) {
1635 tmprow2 = tmprow2->previous();
1636 removeRow(tmprow2->next());
1641 /* set the dimensions of the row */
1642 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1643 setHeightOfRow(bview, tmprow);
1644 } while (not_ready);
1648 // this is just a little changed version of break again
1649 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1651 // get the next breakpoint
1652 pos_type z = nextBreakPoint(bview, row, workWidth(bview));
1655 if (z < row->par()->size()) {
1657 || (row->next() && row->next()->par() != row->par())) {
1658 /* insert a new row */
1660 insertRow(row, row->par(), z);
1666 if (row->pos() != z)
1670 // if there are some rows too much, delete them
1671 // only if you broke the whole paragraph!
1672 Row * tmprow2 = row;
1673 while (tmprow2->next()
1674 && tmprow2->next()->par() == row->par()) {
1675 tmprow2 = tmprow2->next();
1677 while (tmprow2 != row) {
1678 tmprow2 = tmprow2->previous();
1679 removeRow(tmprow2->next());
1683 // set the dimensions of the row
1684 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1685 setHeightOfRow(bview, tmprow);
1689 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1691 LyXLayout const & layout =
1692 textclasslist.Style(bview->buffer()->params.textclass,
1693 cursor.par()->getLayout());
1695 // this is only allowed, if the current paragraph is not empty or caption
1696 if ((cursor.par()->size() <= 0)
1697 && layout.labeltype!= LABEL_SENSITIVE)
1700 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1702 // Always break behind a space
1704 // It is better to erase the space (Dekel)
1705 if (cursor.pos() < cursor.par()->size()
1706 && cursor.par()->isLineSeparator(cursor.pos()))
1707 cursor.par()->erase(cursor.pos());
1708 // cursor.pos(cursor.pos() + 1);
1710 // break the paragraph
1714 keep_layout = layout.isEnvironment();
1715 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1718 // well this is the caption hack since one caption is really enough
1719 if (layout.labeltype == LABEL_SENSITIVE) {
1721 // set to standard-layout
1722 cursor.par()->setLayout(0);
1724 // set to standard-layout
1725 cursor.par()->next()->setLayout(0);
1728 /* if the cursor is at the beginning of a row without prior newline,
1730 * This touches only the screen-update. Otherwise we would may have
1731 * an empty row on the screen */
1732 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1733 && cursor.row()->pos() == cursor.pos()) {
1737 status(bview, LyXText::NEED_MORE_REFRESH);
1738 refresh_row = cursor.row();
1739 refresh_y = cursor.y() - cursor.row()->baseline();
1741 // Do not forget the special right address boxes
1742 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1743 while (refresh_row->previous() &&
1744 refresh_row->previous()->par() == refresh_row->par()) {
1745 refresh_row = refresh_row->previous();
1746 refresh_y -= refresh_row->height();
1749 removeParagraph(cursor.row());
1751 // set the dimensions of the cursor row
1752 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1754 setHeightOfRow(bview, cursor.row());
1756 while (cursor.par()->next()->size()
1757 && cursor.par()->next()->isNewline(0))
1758 cursor.par()->next()->erase(0);
1760 insertParagraph(bview, cursor.par()->next(), cursor.row());
1762 updateCounters(bview, cursor.row()->previous());
1764 /* This check is necessary. Otherwise the new empty paragraph will
1765 * be deleted automatically. And it is more friendly for the user! */
1767 setCursor(bview, cursor.par()->next(), 0);
1769 setCursor(bview, cursor.par(), 0);
1771 if (cursor.row()->next())
1772 breakAgain(bview, cursor.row()->next());
1778 // Just a macro to make some thing easier.
1779 void LyXText::redoParagraph(BufferView * bview) const
1782 redoParagraphs(bview, cursor, cursor.par()->next());
1783 setCursorIntern(bview, cursor.par(), cursor.pos());
1787 /* insert a character, moves all the following breaks in the
1788 * same Paragraph one to the right and make a rebreak */
1789 void LyXText::insertChar(BufferView * bview, char c)
1791 setUndo(bview, Undo::INSERT,
1792 cursor.par(), cursor.par()->next());
1794 // When the free-spacing option is set for the current layout,
1795 // disable the double-space checking
1797 bool const freeSpacing =
1798 textclasslist.Style(bview->buffer()->params.textclass,
1799 cursor.row()->par()->getLayout()).free_spacing ||
1800 cursor.row()->par()->isFreeSpacing();
1803 if (lyxrc.auto_number) {
1804 static string const number_operators = "+-/*";
1805 static string const number_unary_operators = "+-";
1806 static string const number_seperators = ".,:";
1808 if (current_font.number() == LyXFont::ON) {
1809 if (!IsDigit(c) && !contains(number_operators, c) &&
1810 !(contains(number_seperators, c) &&
1811 cursor.pos() >= 1 &&
1812 cursor.pos() < cursor.par()->size() &&
1813 getFont(bview->buffer(),
1815 cursor.pos()).number() == LyXFont::ON &&
1816 getFont(bview->buffer(),
1818 cursor.pos() - 1).number() == LyXFont::ON)
1820 number(bview); // Set current_font.number to OFF
1821 } else if (IsDigit(c) &&
1822 real_current_font.isVisibleRightToLeft()) {
1823 number(bview); // Set current_font.number to ON
1825 if (cursor.pos() > 0) {
1826 char const c = cursor.par()->getChar(cursor.pos() - 1);
1827 if (contains(number_unary_operators, c) &&
1828 (cursor.pos() == 1 ||
1829 cursor.par()->isSeparator(cursor.pos() - 2) ||
1830 cursor.par()->isNewline(cursor.pos() - 2) )
1832 setCharFont(bview->buffer(),
1836 } else if (contains(number_seperators, c) &&
1837 cursor.pos() >= 2 &&
1838 getFont(bview->buffer(),
1840 cursor.pos() - 2).number() == LyXFont::ON) {
1841 setCharFont(bview->buffer(),
1851 /* First check, if there will be two blanks together or a blank at
1852 the beginning of a paragraph.
1853 I decided to handle blanks like normal characters, the main
1854 difference are the special checks when calculating the row.fill
1855 (blank does not count at the end of a row) and the check here */
1857 // The bug is triggered when we type in a description environment:
1858 // The current_font is not changed when we go from label to main text
1859 // and it should (along with realtmpfont) when we type the space.
1860 // CHECK There is a bug here! (Asger)
1862 LyXFont realtmpfont = real_current_font;
1863 LyXFont rawtmpfont = current_font; /* store the current font.
1864 * This is because of the use
1865 * of cursor movements. The moving
1866 * cursor would refresh the
1869 // Get the font that is used to calculate the baselineskip
1870 pos_type const lastpos = cursor.par()->size();
1871 LyXFont rawparfont =
1872 cursor.par()->getFontSettings(bview->buffer()->params,
1875 bool jumped_over_space = false;
1877 if (!freeSpacing && IsLineSeparatorChar(c)) {
1878 if ((cursor.pos() > 0
1879 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1880 || (cursor.pos() > 0
1881 && cursor.par()->isNewline(cursor.pos() - 1))
1882 || (cursor.pos() == 0)) {
1883 static bool sent_space_message = false;
1884 if (!sent_space_message) {
1885 if (cursor.pos() == 0)
1886 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1888 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1889 sent_space_message = true;
1894 } else if (IsNewlineChar(c)) {
1895 if (cursor.par() == cursor.par()
1896 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1900 /* No newline at first position
1901 * of a paragraph or behind labels.
1902 * TeX does not allow that. */
1904 if (cursor.pos() < cursor.par()->size() &&
1905 cursor.par()->isLineSeparator(cursor.pos()))
1906 // newline always after a blank!
1908 cursor.row()->fill(-1); // to force a new break
1911 // the display inset stuff
1912 if (cursor.row()->par()->isInset(cursor.row()->pos())) {
1913 Inset * inset = cursor.row()->par()->getInset(cursor.row()->pos());
1914 if (inset && (inset->display() || inset->needFullRow())) {
1915 // force a new break
1916 cursor.row()->fill(-1); // to force a new break
1920 // get the cursor row fist
1921 Row * row = cursor.row();
1922 int y = cursor.y() - row->baseline();
1923 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1924 * already insertet the character */
1925 cursor.par()->insertChar(cursor.pos(), c);
1926 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1928 if (!jumped_over_space) {
1929 // refresh the positions
1931 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1932 tmprow = tmprow->next();
1933 tmprow->pos(tmprow->pos() + 1);
1937 // Is there a break one row above
1938 if ((cursor.par()->isLineSeparator(cursor.pos())
1939 || cursor.par()->isNewline(cursor.pos())
1940 || ((cursor.pos() < cursor.par()->size()) &&
1941 cursor.par()->isInset(cursor.pos()+1))
1942 || cursor.row()->fill() == -1)
1943 && row->previous() && row->previous()->par() == row->par()) {
1944 pos_type z = nextBreakPoint(bview,
1947 if (z >= row->pos()) {
1950 // set the dimensions of the row above
1951 row->previous()->fill(fill(bview,
1955 setHeightOfRow(bview, row->previous());
1957 y -= row->previous()->height();
1959 refresh_row = row->previous();
1960 status(bview, LyXText::NEED_MORE_REFRESH);
1962 breakAgainOneRow(bview, row);
1964 current_font = rawtmpfont;
1965 real_current_font = realtmpfont;
1966 setCursor(bview, cursor.par(), cursor.pos() + 1,
1967 false, cursor.boundary());
1968 // cursor MUST be in row now.
1970 if (row->next() && row->next()->par() == row->par())
1971 need_break_row = row->next();
1975 // check, wether the last characters font has changed.
1976 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1977 && rawparfont != rawtmpfont)
1978 redoHeightOfParagraph(bview, cursor);
1985 // recalculate the fill of the row
1986 if (row->fill() >= 0) /* needed because a newline
1987 * will set fill to -1. Otherwise
1988 * we would not get a rebreak! */
1989 row->fill(fill(bview, row, workWidth(bview)));
1990 if (row->fill() < 0) {
1993 refresh_x = cursor.x();
1994 refresh_pos = cursor.pos();
1995 status(bview, LyXText::NEED_MORE_REFRESH);
1996 breakAgainOneRow(bview, row);
1997 // will the cursor be in another row now?
1998 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1999 if (row->next() && row->next()->par() == row->par())
2000 // this should always be true
2002 breakAgainOneRow(bview, row);
2004 current_font = rawtmpfont;
2005 real_current_font = realtmpfont;
2007 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2009 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2010 != cursor.boundary())
2011 setCursor(bview, cursor.par(), cursor.pos(), false,
2012 !cursor.boundary());
2013 if (row->next() && row->next()->par() == row->par())
2014 need_break_row = row->next();
2019 refresh_x = cursor.x();
2021 refresh_pos = cursor.pos();
2023 int const tmpheight = row->height();
2024 setHeightOfRow(bview, row);
2025 if (tmpheight == row->height())
2026 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2028 status(bview, LyXText::NEED_MORE_REFRESH);
2030 current_font = rawtmpfont;
2031 real_current_font = realtmpfont;
2032 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
2036 // check, wether the last characters font has changed.
2037 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2038 && rawparfont != rawtmpfont) {
2039 redoHeightOfParagraph(bview, cursor);
2041 // now the special right address boxes
2042 if (textclasslist.Style(bview->buffer()->params.textclass,
2043 cursor.par()->getLayout()).margintype
2044 == MARGIN_RIGHT_ADDRESS_BOX) {
2045 redoDrawingOfParagraph(bview, cursor);
2053 void LyXText::charInserted()
2055 // Here we could call FinishUndo for every 20 characters inserted.
2056 // This is from my experience how emacs does it.
2057 static unsigned int counter;
2067 void LyXText::prepareToPrint(BufferView * bview,
2068 Row * row, float & x,
2069 float & fill_separator,
2071 float & fill_label_hfill,
2077 float w = row->fill();
2079 fill_label_hfill = 0;
2081 fill_label_hfill = 0;
2084 row->par()->isRightToLeftPar(bview->buffer()->params);
2086 x = (workWidth(bview) > 0)
2087 ? rightMargin(bview->buffer(), row) : 0;
2089 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2091 // is there a manual margin with a manual label
2092 if (textclasslist.Style(bview->buffer()->params.textclass,
2093 row->par()->getLayout()).margintype == MARGIN_MANUAL
2094 && textclasslist.Style(bview->buffer()->params.textclass,
2095 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2097 /* one more since labels are left aligned */
2098 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2099 if (nlh && !row->par()->getLabelWidthString().empty()) {
2100 fill_label_hfill = labelFill(bview, row) / nlh;
2104 // are there any hfills in the row?
2105 float const nh = numberOfHfills(bview->buffer(), row);
2109 fill_hfill = w / nh;
2111 // is it block, flushleft or flushright?
2112 // set x how you need it
2114 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2115 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2117 align = row->par()->params().align();
2120 // center displayed insets
2122 if (row->par()->isInset(row->pos())
2123 && (inset=row->par()->getInset(row->pos()))
2124 && (inset->display())) // || (inset->scroll() < 0)))
2125 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2126 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2129 case LYX_ALIGN_BLOCK:
2130 ns = numberOfSeparators(bview->buffer(), row);
2131 if (ns && row->next() && row->next()->par() == row->par() &&
2132 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2133 && !(row->next()->par()->isInset(row->next()->pos())
2134 && row->next()->par()->getInset(row->next()->pos())
2135 && row->next()->par()->getInset(row->next()->pos())->display())
2138 fill_separator = w / ns;
2139 } else if (is_rtl) {
2143 case LYX_ALIGN_RIGHT:
2146 case LYX_ALIGN_CENTER:
2154 computeBidiTables(bview->buffer(), row);
2156 pos_type main_body =
2157 beginningOfMainBody(bview->buffer(), row->par());
2158 pos_type last = rowLast(row);
2160 if (main_body > 0 &&
2161 (main_body-1 > last ||
2162 !row->par()->isLineSeparator(main_body-1))) {
2163 LyXLayout const & layout =
2164 textclasslist.Style(bview->buffer()->params.textclass,
2165 row->par()->getLayout());
2166 x += lyxfont::width(layout.labelsep,
2167 getLabelFont(bview->buffer(), row->par()));
2168 if (main_body-1 <= last)
2169 x += fill_label_hfill;
2174 /* important for the screen */
2177 /* the cursor set functions have a special mechanism. When they
2178 * realize, that you left an empty paragraph, they will delete it.
2179 * They also delete the corresponding row */
2181 void LyXText::cursorRightOneWord(BufferView * bview) const
2183 // treat floats, HFills and Insets as words
2184 LyXCursor tmpcursor = cursor;
2185 // CHECK See comment on top of text.C
2187 if (tmpcursor.pos() == tmpcursor.par()->size()
2188 && tmpcursor.par()->next()) {
2189 tmpcursor.par(tmpcursor.par()->next());
2194 // Skip through initial nonword stuff.
2195 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2196 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2197 // printf("Current pos1 %d", tmpcursor.pos()) ;
2198 tmpcursor.pos(tmpcursor.pos() + 1);
2201 // Advance through word.
2202 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2203 tmpcursor.par()->isWord( tmpcursor.pos())) {
2204 // printf("Current pos2 %d", tmpcursor.pos()) ;
2205 tmpcursor.pos(tmpcursor.pos() + 1);
2209 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2213 void LyXText::cursorTab(BufferView * bview) const
2215 LyXCursor tmpcursor = cursor;
2216 while (tmpcursor.pos() < tmpcursor.par()->size()
2217 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2218 tmpcursor.pos(tmpcursor.pos() + 1);
2220 if (tmpcursor.pos() == tmpcursor.par()->size()){
2221 if (tmpcursor.par()->next()) {
2222 tmpcursor.par(tmpcursor.par()->next());
2226 tmpcursor.pos(tmpcursor.pos() + 1);
2227 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2231 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2232 of prior word, not to end of next prior word. */
2234 void LyXText::cursorLeftOneWord(BufferView * bview) const
2236 LyXCursor tmpcursor = cursor;
2237 cursorLeftOneWord(tmpcursor);
2238 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2241 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2243 // treat HFills, floats and Insets as words
2246 && (cur.par()->isSeparator(cur.pos() - 1)
2247 || cur.par()->isKomma(cur.pos() - 1))
2248 && !(cur.par()->isHfill(cur.pos() - 1)
2249 || cur.par()->isInset(cur.pos() - 1)))
2250 cur.pos(cur.pos() - 1);
2253 && (cur.par()->isInset(cur.pos() - 1)
2254 || cur.par()->isHfill(cur.pos() - 1))) {
2255 cur.pos(cur.pos() - 1);
2256 } else if (!cur.pos()) {
2257 if (cur.par()->previous()){
2258 cur.par(cur.par()->previous());
2259 cur.pos(cur.par()->size());
2261 } else { // Here, cur != 0
2262 while (cur.pos() > 0 &&
2263 cur.par()->isWord(cur.pos()-1) )
2264 cur.pos(cur.pos() - 1);
2268 /* -------> Select current word. This depends on behaviour of
2269 CursorLeftOneWord(), so it is patched as well. */
2270 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2271 word_location const loc) const
2273 // first put the cursor where we wana start to select the word
2276 case WHOLE_WORD_STRICT:
2277 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2278 || cursor.par()->isSeparator(cursor.pos())
2279 || cursor.par()->isKomma(cursor.pos())
2280 || cursor.par()->isSeparator(cursor.pos() -1)
2281 || cursor.par()->isKomma(cursor.pos() -1)) {
2285 // no break here, we go to the next
2288 // Move cursor to the beginning, when not already there.
2289 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2290 && !from.par()->isKomma(from.pos() - 1))
2291 cursorLeftOneWord(from);
2294 // always move the cursor to the beginning of previous word
2295 cursorLeftOneWord(from);
2298 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2304 while (to.pos() < to.par()->size()
2305 && !to.par()->isSeparator(to.pos())
2306 && !to.par()->isKomma(to.pos())
2307 && !to.par()->isHfill(to.pos()) )
2309 to.pos(to.pos() + 1);
2314 void LyXText::selectWord(BufferView * bview, word_location const loc)
2318 getWord(from, to, loc);
2320 setCursor(bview, from.par(), from.pos());
2323 selection.cursor = cursor;
2324 setCursor(bview, to.par(), to.pos() );
2325 setSelection(bview);
2329 /* -------> Select the word currently under the cursor when no
2330 selection is currently set */
2331 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2332 word_location const loc)
2334 if (!selection.set()) {
2335 selectWord(bview, loc);
2336 return selection.set();
2342 // This function is only used by the spellchecker for NextWord().
2343 // It doesn't handle LYX_ACCENTs and probably never will.
2344 string const LyXText::selectNextWordToSpellcheck(BufferView * bview,
2345 float & value) const
2347 if (the_locking_inset) {
2348 string str = the_locking_inset->selectNextWordToSpellcheck(bview, value);
2350 value += float(cursor.y())/float(height);
2353 #warning Dekel please have a look on this one RTL? (Jug)
2355 // we have to go on checking so move cusor to the right
2356 if (cursor.pos() == cursor.par()->size()) {
2357 if (!cursor.par()->next())
2359 cursor.par(cursor.par()->next());
2362 cursor.pos(cursor.pos() + 1);
2364 Paragraph * tmppar = cursor.par();
2366 // If this is not the very first word, skip rest of
2367 // current word because we are probably in the middle
2368 // of a word if there is text here.
2369 if (cursor.pos() || cursor.par()->previous()) {
2370 while (cursor.pos() < cursor.par()->size()
2371 && cursor.par()->isLetter(cursor.pos()))
2372 cursor.pos(cursor.pos() + 1);
2375 // Now, skip until we have real text (will jump paragraphs)
2376 while ((cursor.par()->size() > cursor.pos()
2377 && (!cursor.par()->isLetter(cursor.pos()))
2378 && (!cursor.par()->isInset(cursor.pos()) ||
2379 !cursor.par()->getInset(cursor.pos())->allowSpellcheck()))
2380 || (cursor.par()->size() == cursor.pos()
2381 && cursor.par()->next()))
2383 if (cursor.pos() == cursor.par()->size()) {
2384 cursor.par(cursor.par()->next());
2387 cursor.pos(cursor.pos() + 1);
2390 // now check if we hit an inset so it has to be a inset containing text!
2391 if (cursor.pos() < cursor.par()->size() &&
2392 cursor.par()->isInset(cursor.pos()))
2395 cursor.par()->getInset(cursor.pos())->edit(bview);
2396 // now call us again to do the above trick
2397 // but obviously we have to start from down below ;)
2398 return bview->text->selectNextWordToSpellcheck(bview, value);
2401 // Update the value if we changed paragraphs
2402 if (cursor.par() != tmppar){
2403 setCursor(bview, cursor.par(), cursor.pos());
2404 value = float(cursor.y())/float(height);
2407 // Start the selection from here
2408 selection.cursor = cursor;
2410 // and find the end of the word (insets like optional hyphens
2411 // and ligature break are part of a word)
2412 while (cursor.pos() < cursor.par()->size()
2413 && (cursor.par()->isLetter(cursor.pos())))
2414 cursor.pos(cursor.pos() + 1);
2416 // Finally, we copy the word to a string and return it
2418 if (selection.cursor.pos() < cursor.pos()) {
2420 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2421 if (!cursor.par()->isInset(i))
2422 str += cursor.par()->getChar(i);
2429 // This one is also only for the spellchecker
2430 void LyXText::selectSelectedWord(BufferView * bview)
2432 if (the_locking_inset) {
2433 the_locking_inset->selectSelectedWord(bview);
2436 // move cursor to the beginning
2437 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2439 // set the sel cursor
2440 selection.cursor = cursor;
2442 // now find the end of the word
2443 while (cursor.pos() < cursor.par()->size()
2444 && (cursor.par()->isLetter(cursor.pos())))
2445 cursor.pos(cursor.pos() + 1);
2447 setCursor(bview, cursor.par(), cursor.pos());
2449 // finally set the selection
2450 setSelection(bview);
2454 /* -------> Delete from cursor up to the end of the current or next word. */
2455 void LyXText::deleteWordForward(BufferView * bview)
2457 if (!cursor.par()->size())
2460 LyXCursor tmpcursor = cursor;
2461 tmpcursor.row(0); //Â ??
2462 selection.set(true); // to avoid deletion
2463 cursorRightOneWord(bview);
2464 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2465 selection.cursor = cursor;
2467 setSelection(bview);
2469 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2470 cutSelection(bview, true, false);
2475 /* -------> Delete from cursor to start of current or prior word. */
2476 void LyXText::deleteWordBackward(BufferView * bview)
2478 if (!cursor.par()->size())
2481 LyXCursor tmpcursor = cursor;
2482 tmpcursor.row(0); // ??
2483 selection.set(true); // to avoid deletion
2484 cursorLeftOneWord(bview);
2485 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2486 selection.cursor = cursor;
2488 setSelection(bview);
2489 cutSelection(bview, true, false);
2494 /* -------> Kill to end of line. */
2495 void LyXText::deleteLineForward(BufferView * bview)
2497 if (!cursor.par()->size())
2498 // Paragraph is empty, so we just go to the right
2501 LyXCursor tmpcursor = cursor;
2502 // We can't store the row over a regular setCursor
2503 // so we set it to 0 and reset it afterwards.
2504 tmpcursor.row(0); //Â ??
2505 selection.set(true); // to avoid deletion
2507 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2508 selection.cursor = cursor;
2510 setSelection(bview);
2511 // What is this test for ??? (JMarc)
2512 if (!selection.set()) {
2513 deleteWordForward(bview);
2515 cutSelection(bview, true, false);
2521 // Change the case of a word at cursor position.
2522 // This function directly manipulates Paragraph::text because there
2523 // is no Paragraph::SetChar currently. I did what I could to ensure
2524 // that it is correct. I guess part of it should be moved to
2525 // Paragraph, but it will have to change for 1.1 anyway. At least
2526 // it does not access outside of the allocated array as the older
2527 // version did. (JMarc)
2528 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2533 if (selection.set()) {
2534 from = selection.start;
2537 getWord(from, to, PARTIAL_WORD);
2538 setCursor(bview, to.par(), to.pos() + 1);
2541 changeRegionCase(bview, from, to, action);
2545 void LyXText::changeRegionCase(BufferView * bview,
2546 LyXCursor const & from,
2547 LyXCursor const & to,
2548 LyXText::TextCase action)
2550 lyx::Assert(from <= to);
2552 setUndo(bview, Undo::FINISH,
2553 from.par(), to.par()->next());
2555 pos_type pos = from.pos();
2556 Paragraph * par = from.par();
2558 while (par && (pos != to.pos() || par != to.par())) {
2559 unsigned char c = par->getChar(pos);
2560 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2562 case text_lowercase:
2565 case text_capitalization:
2567 action = text_lowercase;
2569 case text_uppercase:
2574 par->setChar(pos, c);
2575 checkParagraph(bview, par, pos);
2578 if (pos == par->size()) {
2583 if (to.row() != from.row()) {
2584 refresh_y = from.y() - from.row()->baseline();
2585 refresh_row = from.row();
2586 status(bview, LyXText::NEED_MORE_REFRESH);
2591 void LyXText::transposeChars(BufferView & bview)
2593 Paragraph * tmppar = cursor.par();
2595 setUndo(&bview, Undo::FINISH,
2596 tmppar, tmppar->next());
2598 pos_type tmppos = cursor.pos();
2600 // First decide if it is possible to transpose at all
2602 // We are at the beginning of a paragraph.
2603 if (tmppos == 0) return;
2605 // We are at the end of a paragraph.
2606 if (tmppos == tmppar->size() - 1) return;
2608 unsigned char c1 = tmppar->getChar(tmppos);
2609 unsigned char c2 = tmppar->getChar(tmppos - 1);
2611 if (c1 != Paragraph::META_INSET
2612 && c2 != Paragraph::META_INSET) {
2613 tmppar->setChar(tmppos, c2);
2614 tmppar->setChar(tmppos - 1, c1);
2616 // We should have an implementation that handles insets
2617 // as well, but that will have to come later. (Lgb)
2618 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2622 void LyXText::Delete(BufferView * bview)
2624 // this is a very easy implementation
2626 LyXCursor old_cursor = cursor;
2627 int const old_cur_par_id = old_cursor.par()->id();
2628 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2629 old_cursor.par()->previous()->id() : 0;
2631 // just move to the right
2634 // CHECK Look at the comment here.
2635 // This check is not very good...
2636 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2637 // and that can very well delete the par or par->previous in
2638 // old_cursor. Will a solution where we compare paragraph id's
2640 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2641 == old_cur_par_prev_id
2642 && cursor.par()->id() != old_cur_par_id) {
2643 // delete-empty-paragraph-mechanism has done it
2647 // if you had success make a backspace
2648 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2649 LyXCursor tmpcursor = cursor;
2650 // to make sure undo gets the right cursor position
2651 cursor = old_cursor;
2652 setUndo(bview, Undo::DELETE,
2653 cursor.par(), cursor.par()->next());
2660 void LyXText::backspace(BufferView * bview)
2662 // Get the font that is used to calculate the baselineskip
2663 pos_type lastpos = cursor.par()->size();
2664 LyXFont rawparfont =
2665 cursor.par()->getFontSettings(bview->buffer()->params,
2668 if (cursor.pos() == 0) {
2669 // The cursor is at the beginning of a paragraph,
2670 // so the the backspace will collapse two paragraphs into one.
2672 // we may paste some paragraphs
2674 // is it an empty paragraph?
2677 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2678 // This is an empty paragraph and we delete it just by moving the cursor one step
2679 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2680 // of the paragraph.
2682 if (cursor.par()->previous()) {
2683 Paragraph * tmppar = cursor.par()->previous();
2684 if (cursor.par()->getLayout() == tmppar->getLayout()
2685 && cursor.par()->getAlign() == tmppar->getAlign()) {
2686 // Inherit bottom DTD from the paragraph below.
2687 // (the one we are deleting)
2688 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2689 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2690 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2695 // the layout things can change the height of a row !
2696 int const tmpheight = cursor.row()->height();
2697 setHeightOfRow(bview, cursor.row());
2698 if (cursor.row()->height() != tmpheight) {
2699 refresh_y = cursor.y() - cursor.row()->baseline();
2700 refresh_row = cursor.row();
2701 status(bview, LyXText::NEED_MORE_REFRESH);
2707 if (cursor.par()->previous()) {
2708 setUndo(bview, Undo::DELETE,
2709 cursor.par()->previous(), cursor.par()->next());
2712 Paragraph * tmppar = cursor.par();
2713 Row * tmprow = cursor.row();
2715 // We used to do cursorLeftIntern() here, but it is
2716 // not a good idea since it triggers the auto-delete
2717 // mechanism. So we do a cursorLeftIntern()-lite,
2718 // without the dreaded mechanism. (JMarc)
2719 if (cursor.par()->previous()) {
2720 // steps into the above paragraph.
2721 setCursorIntern(bview, cursor.par()->previous(),
2722 cursor.par()->previous()->size(),
2726 /* Pasting is not allowed, if the paragraphs have different
2727 layout. I think it is a real bug of all other
2728 word processors to allow it. It confuses the user.
2729 Even so with a footnote paragraph and a non-footnote
2730 paragraph. I will not allow pasting in this case,
2731 because the user would be confused if the footnote behaves
2732 different wether it is open or closed.
2734 Correction: Pasting is always allowed with standard-layout
2736 if (cursor.par() != tmppar
2737 && (cursor.par()->getLayout() == tmppar->getLayout()
2738 || tmppar->getLayout() == 0 /*standard*/)
2739 && cursor.par()->getAlign() == tmppar->getAlign())
2741 removeParagraph(tmprow);
2743 cursor.par()->pasteParagraph(bview->buffer()->params);
2745 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2746 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2747 // strangely enough it seems that commenting out the line above removes
2748 // most or all of the segfaults. I will however also try to move the
2749 // two Remove... lines in front of the PasteParagraph too.
2752 cursor.pos(cursor.pos() - 1);
2754 status(bview, LyXText::NEED_MORE_REFRESH);
2755 refresh_row = cursor.row();
2756 refresh_y = cursor.y() - cursor.row()->baseline();
2758 // remove the lost paragraph
2759 // This one is not safe, since the paragraph that the tmprow and the
2760 // following rows belong to has been deleted by the PasteParagraph
2761 // above. The question is... could this be moved in front of the
2763 //RemoveParagraph(tmprow);
2764 //RemoveRow(tmprow);
2766 // This rebuilds the rows.
2767 appendParagraph(bview, cursor.row());
2768 updateCounters(bview, cursor.row());
2770 // the row may have changed, block, hfills etc.
2771 setCursor(bview, cursor.par(), cursor.pos(), false);
2774 /* this is the code for a normal backspace, not pasting
2776 setUndo(bview, Undo::DELETE,
2777 cursor.par(), cursor.par()->next());
2778 // We used to do cursorLeftIntern() here, but it is
2779 // not a good idea since it triggers the auto-delete
2780 // mechanism. So we do a cursorLeftIntern()-lite,
2781 // without the dreaded mechanism. (JMarc)
2782 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2783 false, cursor.boundary());
2785 // some insets are undeletable here
2786 if (cursor.par()->isInset(cursor.pos())) {
2787 if (!cursor.par()->getInset(cursor.pos())->deletable())
2789 // force complete redo when erasing display insets
2790 // this is a cruel method but safe..... Matthias
2791 if (cursor.par()->getInset(cursor.pos())->display() ||
2792 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2793 cursor.par()->erase(cursor.pos());
2794 redoParagraph(bview);
2799 Row * row = cursor.row();
2800 int y = cursor.y() - row->baseline();
2802 /* remember that a space at the end of a row doesnt count
2803 * when calculating the fill */
2804 if (cursor.pos() < rowLast(row) ||
2805 !cursor.par()->isLineSeparator(cursor.pos())) {
2806 row->fill(row->fill() + singleWidth(bview,
2811 /* some special code when deleting a newline. This is similar
2812 * to the behavior when pasting paragraphs */
2813 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2814 cursor.par()->erase(cursor.pos());
2815 // refresh the positions
2817 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2818 tmprow = tmprow->next();
2819 tmprow->pos(tmprow->pos() - 1);
2821 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2822 cursor.pos(cursor.pos() - 1);
2824 if (cursor.pos() < cursor.par()->size()
2825 && !cursor.par()->isSeparator(cursor.pos())) {
2826 cursor.par()->insertChar(cursor.pos(), ' ');
2827 setCharFont(bview->buffer(), cursor.par(),
2828 cursor.pos(), current_font);
2829 // refresh the positions
2831 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2832 tmprow = tmprow->next();
2833 tmprow->pos(tmprow->pos() + 1);
2837 cursor.par()->erase(cursor.pos());
2839 // refresh the positions
2841 while (tmprow->next()
2842 && tmprow->next()->par() == row->par()) {
2843 tmprow = tmprow->next();
2844 tmprow->pos(tmprow->pos() - 1);
2847 // delete newlines at the beginning of paragraphs
2848 while (cursor.par()->size() &&
2849 cursor.par()->isNewline(cursor.pos()) &&
2850 cursor.pos() == beginningOfMainBody(bview->buffer(),
2852 cursor.par()->erase(cursor.pos());
2853 // refresh the positions
2855 while (tmprow->next() &&
2856 tmprow->next()->par() == row->par()) {
2857 tmprow = tmprow->next();
2858 tmprow->pos(tmprow->pos() - 1);
2863 // is there a break one row above
2864 if (row->previous() && row->previous()->par() == row->par()) {
2865 z = nextBreakPoint(bview, row->previous(),
2867 if (z >= row->pos()) {
2870 Row * tmprow = row->previous();
2872 // maybe the current row is now empty
2873 if (row->pos() >= row->par()->size()) {
2878 breakAgainOneRow(bview, row);
2879 if (row->next() && row->next()->par() == row->par())
2880 need_break_row = row->next();
2885 // set the dimensions of the row above
2886 y -= tmprow->height();
2887 tmprow->fill(fill(bview, tmprow,
2889 setHeightOfRow(bview, tmprow);
2892 refresh_row = tmprow;
2893 status(bview, LyXText::NEED_MORE_REFRESH);
2894 setCursor(bview, cursor.par(), cursor.pos(),
2895 false, cursor.boundary());
2896 //current_font = rawtmpfont;
2897 //real_current_font = realtmpfont;
2898 // check, whether the last character's font has changed.
2900 cursor.par()->getFontSettings(bview->buffer()->params,
2901 cursor.par()->size() - 1))
2902 redoHeightOfParagraph(bview, cursor);
2907 // break the cursor row again
2908 if (row->next() && row->next()->par() == row->par() &&
2909 (rowLast(row) == row->par()->size() - 1 ||
2910 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2912 /* it can happen that a paragraph loses one row
2913 * without a real breakup. This is when a word
2914 * is to long to be broken. Well, I don t care this
2916 if (rowLast(row) == row->par()->size() - 1)
2917 removeRow(row->next());
2921 status(bview, LyXText::NEED_MORE_REFRESH);
2923 breakAgainOneRow(bview, row);
2924 // will the cursor be in another row now?
2925 if (row->next() && row->next()->par() == row->par() &&
2926 rowLast(row) <= cursor.pos()) {
2928 breakAgainOneRow(bview, row);
2931 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2933 if (row->next() && row->next()->par() == row->par())
2934 need_break_row = row->next();
2938 // set the dimensions of the row
2939 row->fill(fill(bview, row, workWidth(bview)));
2940 int const tmpheight = row->height();
2941 setHeightOfRow(bview, row);
2942 if (tmpheight == row->height())
2943 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2945 status(bview, LyXText::NEED_MORE_REFRESH);
2948 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2952 // current_font = rawtmpfont;
2953 // real_current_font = realtmpfont;
2955 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2956 != cursor.boundary())
2957 setCursor(bview, cursor.par(), cursor.pos(), false,
2958 !cursor.boundary());
2960 lastpos = cursor.par()->size();
2961 if (cursor.pos() == lastpos)
2962 setCurrentFont(bview);
2964 // check, whether the last characters font has changed.
2966 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2967 redoHeightOfParagraph(bview, cursor);
2969 // now the special right address boxes
2970 if (textclasslist.Style(bview->buffer()->params.textclass,
2971 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2972 redoDrawingOfParagraph(bview, cursor);
2978 bool LyXText::paintRowBackground(DrawRowParams & p)
2980 bool clear_area = true;
2982 LyXFont font(LyXFont::ALL_SANE);
2984 pos_type const last = rowLastPrintable(p.row);
2986 if (!p.bv->screen()->forceClear() && last == p.row->pos()
2987 && p.row->par()->isInset(p.row->pos())) {
2988 inset = p.row->par()->getInset(p.row->pos());
2990 clear_area = inset->doClearArea();
3000 int const y = p.yo < 0 ? 0 : p.yo;
3001 int const h = p.yo < 0 ? p.row->height() + p.yo : p.row->height();
3002 p.pain->fillRectangle(x, y, p.width, h, backgroundColor());
3009 int h = p.row->baseline() - inset->ascent(p.bv, font);
3011 // first clear the whole row above the inset!
3013 p.pain->fillRectangle(p.xo, p.yo, p.width, h, backgroundColor());
3016 // clear the space below the inset!
3017 h += inset->ascent(p.bv, font) + inset->descent(p.bv, font);
3018 if ((p.row->height() - h) > 0) {
3019 p.pain->fillRectangle(p.xo, p.yo + h,
3020 p.width, p.row->height() - h, backgroundColor());
3023 // clear the space behind the inset, if needed
3024 if (!inset->display() && !inset->needFullRow()) {
3025 int const xp = int(p.x) + inset->width(p.bv, font);
3026 if (p.width - xp > 0) {
3027 p.pain->fillRectangle(xp, p.yo, p.width - xp,
3028 p.row->height(), backgroundColor());
3036 void LyXText::paintRowSelection(DrawRowParams & p)
3038 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3040 // the current selection
3041 int const startx = selection.start.x();
3042 int const endx = selection.end.x();
3043 int const starty = selection.start.y();
3044 int const endy = selection.end.y();
3045 Row const * startrow = selection.start.row();
3046 Row const * endrow = selection.end.row();
3050 if (bidi_same_direction) {
3054 int h = row->height();
3056 if (startrow == row && endrow == row) {
3057 if (startx < endx) {
3060 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3064 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3066 } else if (startrow == row) {
3067 int const x = (is_rtl) ? p.xo : (p.xo + startx);
3068 int const w = (is_rtl) ? startx : (p.width - startx);
3069 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3070 } else if (endrow == row) {
3071 int const x = (is_rtl) ? (p.xo + endx) : p.xo;
3072 int const w = (is_rtl) ? (p.width - endx) : endx;
3073 p.pain->fillRectangle(x, y, w, h, LColor::selection);
3074 } else if (p.y > starty && p.y < endy) {
3075 p.pain->fillRectangle(p.xo, y, p.width, h, LColor::selection);
3078 } else if (startrow != row && endrow != row) {
3080 int h = row->height();
3081 if (p.y > starty && p.y < endy) {
3082 p.pain->fillRectangle(p.xo, p.yo, w, h, LColor::selection);
3087 if (!((startrow != row && !is_rtl) || (endrow != row && is_rtl))) {
3093 p.pain->fillRectangle(p.xo, p.yo, int(p.x), row->height(), LColor::selection);
3095 Buffer const * buffer = p.bv->buffer();
3096 Paragraph * par = row->par();
3097 pos_type main_body = beginningOfMainBody(buffer, par);
3098 pos_type const last = rowLastPrintable(row);
3100 for (pos_type vpos = row->pos(); vpos <= last; ++vpos) {
3101 pos_type pos = vis2log(vpos);
3102 float const old_tmpx = tmpx;
3103 if (main_body > 0 && pos == main_body - 1) {
3104 LyXLayout const & layout = textclasslist.Style(buffer->params.textclass,
3106 LyXFont const lfont = getLabelFont(buffer, par);
3109 tmpx += p.label_hfill + lyxfont::width(layout.labelsep, lfont);
3111 if (par->isLineSeparator(main_body - 1))
3112 tmpx -= singleWidth(p.bv, par, main_body - 1);
3115 if (hfillExpansion(buffer, row, pos)) {
3116 tmpx += singleWidth(p.bv, par, pos);
3117 if (pos >= main_body)
3120 tmpx += p.label_hfill;
3123 else if (par->isSeparator(pos)) {
3124 tmpx += singleWidth(p.bv, par, pos);
3125 if (pos >= main_body)
3126 tmpx += p.separator;
3128 tmpx += singleWidth(p.bv, par, pos);
3131 if ((startrow != row || selection.start.pos() <= pos) &&
3132 (endrow != row || pos < selection.end.pos())) {
3133 // Here we do not use p.x as p.xo was added to p.x.
3134 p.pain->fillRectangle(int(old_tmpx), p.yo,
3135 int(tmpx - old_tmpx + 1),
3136 row->height(), LColor::selection);
3139 if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
3140 p.pain->fillRectangle(p.xo + int(tmpx),
3141 p.yo, int(p.bv->workWidth() - tmpx),
3142 row->height(), LColor::selection);
3148 void LyXText::paintRowAppendix(DrawRowParams & p)
3150 // FIXME: can be just p.width ?
3151 int const ww = p.bv->workWidth();
3152 Paragraph * firstpar = p.row->par();
3154 if (firstpar->params().appendix()) {
3155 p.pain->line(1, p.yo, 1, p.yo + p.row->height(), LColor::appendixline);
3156 p.pain->line(ww - 2, p.yo, ww - 2, p.yo + p.row->height(), LColor::appendixline);
3161 void LyXText::paintRowDepthBar(DrawRowParams & p)
3163 Paragraph::depth_type const depth = p.row->par()->getDepth();
3168 Paragraph::depth_type prev_depth = 0;
3169 if (p.row->previous())
3170 prev_depth = p.row->previous()->par()->getDepth();
3171 Paragraph::depth_type next_depth = 0;
3173 next_depth = p.row->next()->par()->getDepth();
3175 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3176 int const x = (LYX_PAPER_MARGIN / 5) * i + p.xo;
3177 int const h = p.yo + p.row->height() - 1 - (i - next_depth - 1) * 3;
3179 p.pain->line(x, p.yo, x, h, LColor::depthbar);
3181 int const w = LYX_PAPER_MARGIN / 5;
3183 if (i > prev_depth) {
3184 p.pain->fillRectangle(x, p.yo, w, 2, LColor::depthbar);
3186 if (i > next_depth) {
3187 p.pain->fillRectangle(x, h, w, 2, LColor::depthbar);
3193 int LyXText::getLengthMarkerHeight(BufferView * bv, VSpace const & vsp) const
3195 int const arrow_size = 4;
3196 int const space_size = int(vsp.inPixels(bv));
3198 if (vsp.kind() != VSpace::LENGTH) {
3204 int const min_size = std::max(3 * arrow_size,
3205 lyxfont::maxAscent(font)
3206 + lyxfont::maxDescent(font));
3208 if (vsp.length().len().value() < 0.0)
3211 return std::max(min_size, space_size);
3215 int LyXText::drawLengthMarker(DrawRowParams & p, string const & prefix,
3216 VSpace const & vsp, int start)
3218 int const arrow_size = 4;
3219 int const size = getLengthMarkerHeight(p.bv, vsp);
3220 int const end = start + size;
3222 // the label to display (if any)
3224 // y-values for top arrow
3226 // y-values for bottom arrow
3228 switch (vsp.kind()) {
3229 case VSpace::LENGTH:
3231 str = prefix + " (" + vsp.asLyXCommand() + ")";
3232 // adding or removing space
3233 bool const added = !(vsp.length().len().value() < 0.0);
3234 ty1 = added ? (start + arrow_size) : start;
3235 ty2 = added ? start : (start + arrow_size);
3236 by1 = added ? (end - arrow_size) : end;
3237 by2 = added ? end : (end - arrow_size);
3240 case VSpace:: VFILL:
3241 str = prefix + " (vertical fill)";
3246 // nothing to draw here
3250 int const leftx = p.xo + leftMargin(p.bv, p.row);
3251 int const midx = leftx + arrow_size;
3252 int const rightx = midx + arrow_size;
3260 font.setColor(LColor::added_space).decSize();
3261 lyxfont::rectText(str, font, w, a, d);
3263 p.pain->rectText(leftx + 2 * arrow_size + 5,
3264 start + ((end - start) / 2) + d,
3270 p.pain->line(leftx, ty1, midx, ty2, LColor::added_space);
3271 p.pain->line(midx, ty2, rightx, ty1, LColor::added_space);
3274 p.pain->line(leftx, by1, midx, by2, LColor::added_space);
3275 p.pain->line(midx, by2, rightx, by1, LColor::added_space);
3278 p.pain->line(midx, ty2, midx, by2, LColor::added_space);
3284 void LyXText::paintFirstRow(DrawRowParams & p)
3286 Paragraph * par = p.row->par();
3287 ParagraphParameters const & parparams = par->params();
3289 // start of appendix?
3290 if (parparams.startOfAppendix()) {
3291 p.pain->line(1, p.yo, p.width - 2, p.yo, LColor::appendixline);
3296 // think about the margins
3297 if (!p.row->previous() && bv_owner)
3298 y_top += LYX_PAPER_MARGIN;
3300 // draw a top pagebreak
3301 if (parparams.pagebreakTop()) {
3302 int const y = p.yo + y_top + 2*defaultHeight();
3303 p.pain->line(p.xo, y, p.xo + p.width, y,
3304 LColor::pagebreak, Painter::line_onoffdash);
3311 pb_font.setColor(LColor::pagebreak).decSize();
3312 lyxfont::rectText(_("Page Break (top)"), pb_font, w, a, d);
3313 p.pain->rectText((p.width - w)/2, y + d,
3314 _("Page Break (top)"), pb_font,
3317 y_top += 3 * defaultHeight();
3320 // draw the additional space if needed:
3321 y_top += drawLengthMarker(p, _("Space above"),
3322 parparams.spaceTop(), p.yo + y_top);
3324 Buffer const * buffer = p.bv->buffer();
3326 LyXLayout const & layout =
3327 textclasslist.Style(buffer->params.textclass, par->getLayout());
3329 // think about the parskip
3330 // some parskips VERY EASY IMPLEMENTATION
3331 if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3332 if (par->previous()) {
3333 if (layout.latextype == LATEX_PARAGRAPH
3334 && !par->getDepth()) {
3335 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3337 LyXLayout const & playout =
3338 textclasslist.Style(buffer->params.textclass,
3339 par->previous()->getLayout());
3340 if (playout.latextype == LATEX_PARAGRAPH
3341 && !par->previous()->getDepth()) {
3342 // is it right to use defskip here, too? (AS)
3343 y_top += buffer->params.getDefSkip().inPixels(p.bv);
3349 int const ww = p.bv->workWidth();
3352 if (parparams.lineTop()) {
3353 LyXFont font(LyXFont::ALL_SANE);
3354 int const asc = lyxfont::ascent('x', getFont(buffer, par, 0));
3358 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3359 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3360 p.pain->line(xp, p.yo + y_top, xp + w, p.yo + y_top,
3361 LColor::topline, Painter::line_solid,
3362 Painter::line_thick);
3367 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3369 // should we print a label?
3370 if (layout.labeltype >= LABEL_STATIC
3371 && (layout.labeltype != LABEL_STATIC
3372 || layout.latextype != LATEX_ENVIRONMENT
3373 || par->isFirstInSequence())) {
3375 LyXFont font = getLabelFont(buffer, par);
3376 if (!par->getLabelstring().empty()) {
3378 string const str = par->getLabelstring();
3380 // this is special code for the chapter layout. This is
3381 // printed in an extra row and has a pagebreak at
3383 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3384 if (buffer->params.secnumdepth >= 0) {
3385 float spacing_val = 1.0;
3386 if (!parparams.spacing().isDefault()) {
3387 spacing_val = parparams.spacing().getValue();
3389 spacing_val = buffer->params.spacing.getValue();
3393 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3394 + int(layout.parsep) * defaultHeight();
3397 x = ww - leftMargin(p.bv, p.row) -
3398 lyxfont::width(str, font);
3401 p.pain->text(int(x),
3402 p.yo + p.row->baseline() -
3403 p.row->ascent_of_text() - maxdesc,
3408 x = ww - leftMargin(p.bv, p.row)
3409 + lyxfont::width(layout.labelsep, font);
3411 x = p.x - lyxfont::width(layout.labelsep, font)
3412 - lyxfont::width(str, font);
3415 p.pain->text(int(x), p.yo + p.row->baseline(), str, font);
3418 // the labels at the top of an environment.
3419 // More or less for bibliography
3420 } else if (par->isFirstInSequence() &&
3421 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3422 layout.labeltype == LABEL_BIBLIO ||
3423 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
3424 LyXFont font = getLabelFont(buffer, par);
3425 if (!par->getLabelstring().empty()) {
3426 string const str = par->getLabelstring();
3427 float spacing_val = 1.0;
3428 if (!parparams.spacing().isDefault()) {
3429 spacing_val = parparams.spacing().getValue();
3431 spacing_val = buffer->params.spacing.getValue();
3435 int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3436 + (layout.labelbottomsep * defaultHeight()));
3439 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3440 x = ((is_rtl ? leftMargin(p.bv, p.row) : p.x)
3441 + ww - rightMargin(buffer, p.row) ) / 2;
3442 x -= lyxfont::width(str, font) / 2;
3443 } else if (is_rtl) {
3444 x = ww - leftMargin(p.bv, p.row) -
3445 lyxfont::width(str, font);
3447 p.pain->text(int(x), p.yo + p.row->baseline()
3448 - p.row->ascent_of_text() - maxdesc,
3453 if (layout.labeltype == LABEL_BIBLIO && par->bibkey) {
3454 LyXFont font = getLayoutFont(buffer, par);
3457 x = ww - leftMargin(p.bv, p.row)
3458 + lyxfont::width(layout.labelsep, font);
3460 x = p.x - lyxfont::width(layout.labelsep, font)
3461 - par->bibkey->width(p.bv, font);
3463 par->bibkey->draw(p.bv, font, p.yo + p.row->baseline(), x, p.cleared);
3468 void LyXText::paintLastRow(DrawRowParams & p)
3470 Paragraph * par = p.row->par();
3471 ParagraphParameters const & parparams = par->params();
3472 int y_bottom = p.row->height() - 1;
3474 // think about the margins
3475 if (!p.row->next() && bv_owner)
3476 y_bottom -= LYX_PAPER_MARGIN;
3478 int const ww = p.bv->workWidth();
3480 // draw a bottom pagebreak
3481 if (parparams.pagebreakBottom()) {
3483 pb_font.setColor(LColor::pagebreak).decSize();
3484 int const y = p.yo + y_bottom - 2 * defaultHeight();
3486 p.pain->line(p.xo, y, p.xo + p.width, y, LColor::pagebreak,
3487 Painter::line_onoffdash);
3492 lyxfont::rectText(_("Page Break (bottom)"), pb_font, w, a, d);
3493 p.pain->rectText((ww - w) / 2, y + d,
3494 _("Page Break (bottom)"),
3495 pb_font, backgroundColor(), backgroundColor());
3497 y_bottom -= 3 * defaultHeight();
3500 // draw the additional space if needed:
3501 int const height = getLengthMarkerHeight(p.bv,
3502 parparams.spaceBottom());
3503 y_bottom -= drawLengthMarker(p, _("Space below"),
3504 parparams.spaceBottom(),
3505 p.yo + y_bottom - height);
3507 Buffer const * buffer = p.bv->buffer();
3509 // draw a bottom line
3510 if (parparams.lineBottom()) {
3511 LyXFont font(LyXFont::ALL_SANE);
3512 int const asc = lyxfont::ascent('x',
3513 getFont(buffer, par,
3514 max(pos_type(0), par->size() - 1)));
3518 int const w = (inset_owner ? inset_owner->width(p.bv, font) : ww);
3519 int const xp = static_cast<int>(inset_owner ? p.xo : 0);
3520 int const y = p.yo + y_bottom;
3521 p.pain->line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
3522 Painter::line_thick);
3527 bool const is_rtl = p.row->par()->isRightToLeftPar(p.bv->buffer()->params);
3528 int const endlabel = par->getEndLabel(buffer->params);
3533 case END_LABEL_FILLED_BOX:
3535 LyXFont const font = getLabelFont(buffer, par);
3536 int const size = int(0.75 * lyxfont::maxAscent(font));
3537 int const y = (p.yo + p.row->baseline()) - size;
3538 int x = is_rtl ? LYX_PAPER_MARGIN : ww - LYX_PAPER_MARGIN - size;
3540 if (p.row->fill() <= size)
3541 x += (size - p.row->fill() + 1) * (is_rtl ? -1 : 1);
3543 if (endlabel == END_LABEL_BOX) {
3544 p.pain->rectangle(x, y, size, size, LColor::eolmarker);
3546 p.pain->fillRectangle(x, y, size, size,
3551 case END_LABEL_STATIC:
3553 LyXFont font(LyXFont::ALL_SANE);
3554 LyXTextClass::LayoutList::size_type layout = par->getLayout();
3555 string const str = textclasslist.
3556 Style(buffer->params.textclass, layout).endlabelstring();
3557 font = getLabelFont(buffer, par);
3558 int const x = is_rtl ?
3559 int(p.x) - lyxfont::width(str, font)
3560 : ww - rightMargin(buffer, p.row) - p.row->fill();
3561 p.pain->text(x, p.yo + p.row->baseline(), str, font);
3564 case END_LABEL_NO_LABEL:
3569 void LyXText::paintRowText(DrawRowParams & p)
3571 Paragraph * par = p.row->par();
3572 Buffer const * buffer = p.bv->buffer();
3574 pos_type const last = rowLastPrintable(p.row);
3575 pos_type main_body =
3576 beginningOfMainBody(buffer, par);
3577 if (main_body > 0 &&
3578 (main_body - 1 > last ||
3579 !par->isLineSeparator(main_body - 1))) {
3583 LyXLayout const & layout =
3584 textclasslist.Style(buffer->params.textclass, par->getLayout());
3586 pos_type vpos = p.row->pos();
3587 while (vpos <= last) {
3588 pos_type pos = vis2log(vpos);
3589 if (main_body > 0 && pos == main_body - 1) {
3590 int const lwidth = lyxfont::width(layout.labelsep,
3591 getLabelFont(buffer, par));
3593 p.x += p.label_hfill + lwidth
3594 - singleWidth(p.bv, par, main_body - 1);
3597 if (par->isHfill(pos)) {
3600 int const y0 = p.yo + p.row->baseline();
3601 int const y1 = y0 - defaultHeight() / 2;
3603 p.pain->line(int(p.x), y1, int(p.x), y0,
3604 LColor::added_space);
3606 if (hfillExpansion(buffer, p.row, pos)) {
3607 int const y2 = (y0 + y1) / 2;
3609 if (pos >= main_body) {
3610 p.pain->line(int(p.x), y2,
3611 int(p.x + p.hfill), y2,
3612 LColor::added_space,
3613 Painter::line_onoffdash);
3616 p.pain->line(int(p.x), y2,
3617 int(p.x + p.label_hfill), y2,
3618 LColor::added_space,
3619 Painter::line_onoffdash);
3620 p.x += p.label_hfill;
3622 p.pain->line(int(p.x), y1,
3624 LColor::added_space);
3628 } else if (par->isSeparator(pos)) {
3629 p.x += singleWidth(p.bv, par, pos);
3630 if (pos >= main_body)
3640 void LyXText::getVisibleRow(BufferView * bv, int y_offset, int x_offset,
3641 Row * row, int y, bool cleared)
3643 if (row->height() <= 0) {
3644 lyxerr << "LYX_ERROR: row.height: "
3645 << row->height() << endl;
3651 // set up drawing parameters
3653 p.pain = &bv->painter();
3657 prepareToPrint(bv, row, p.x, p.separator, p.hfill, p.label_hfill);
3658 if (inset_owner && (p.x < 0))
3662 p.width = inset_owner ? inset_owner->textWidth(bv, true) : bv->workWidth();
3663 p.cleared = cleared;
3667 // clear to background if necessary
3668 p.cleared = paintRowBackground(p);
3670 // paint the selection background
3671 if (selection.set()) {
3672 paintRowSelection(p);
3675 // vertical lines for appendix
3676 paintRowAppendix(p);
3678 // environment depth brackets
3679 paintRowDepthBar(p);
3681 // draw any stuff wanted for a first row of a paragraph
3686 // draw any stuff wanted for the last row of a paragraph
3687 if (!row->next() || (row->next()->par() != row->par())) {
3696 int LyXText::defaultHeight() const
3698 LyXFont font(LyXFont::ALL_SANE);
3699 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3703 /* returns the column near the specified x-coordinate of the row
3704 * x is set to the real beginning of this column */
3706 LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3707 bool & boundary) const
3710 float fill_separator;
3712 float fill_label_hfill;
3714 prepareToPrint(bview, row, tmpx, fill_separator,
3715 fill_hfill, fill_label_hfill);
3717 pos_type vc = row->pos();
3718 pos_type last = rowLastPrintable(row);
3720 LyXLayout const & layout =
3721 textclasslist.Style(bview->buffer()->params.textclass,
3722 row->par()->getLayout());
3723 bool left_side = false;
3725 pos_type main_body = beginningOfMainBody(bview->buffer(), row->par());
3726 float last_tmpx = tmpx;
3728 if (main_body > 0 &&
3729 (main_body - 1 > last ||
3730 !row->par()->isLineSeparator(main_body - 1)))
3733 while (vc <= last && tmpx <= x) {
3736 if (main_body > 0 && c == main_body-1) {
3737 tmpx += fill_label_hfill +
3738 lyxfont::width(layout.labelsep,
3739 getLabelFont(bview->buffer(), row->par()));
3740 if (row->par()->isLineSeparator(main_body - 1))
3741 tmpx -= singleWidth(bview, row->par(), main_body-1);
3744 if (hfillExpansion(bview->buffer(), row, c)) {
3745 x += singleWidth(bview, row->par(), c);
3749 tmpx += fill_label_hfill;
3751 else if (row->par()->isSeparator(c)) {
3752 tmpx += singleWidth(bview, row->par(), c);
3754 tmpx+= fill_separator;
3756 tmpx += singleWidth(bview, row->par(), c);
3760 if ((tmpx + last_tmpx) / 2 > x) {
3765 if (vc > last + 1) // This shouldn't happen.
3769 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3770 // some speedup if rtl_support=false
3771 && (!row->next() || row->next()->par() != row->par());
3772 bool const rtl = (lastrow)
3773 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3774 : false; // If lastrow is false, we don't need to compute
3775 // the value of rtl.
3777 if (row->pos() > last) // Row is empty?
3780 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3781 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3783 else if (vc == row->pos()) {
3785 if (bidi_level(c) % 2 == 1)
3788 c = vis2log(vc - 1);
3789 bool const rtl = (bidi_level(c) % 2 == 1);
3790 if (left_side == rtl) {
3792 boundary = isBoundary(bview->buffer(), row->par(), c);
3796 if (row->pos() <= last && c > last
3797 && row->par()->isNewline(last)) {
3798 if (bidi_level(last) % 2 == 0)
3799 tmpx -= singleWidth(bview, row->par(), last);
3801 tmpx += singleWidth(bview, row->par(), last);
3811 // returns pointer to a specified row
3812 Row * LyXText::getRow(Paragraph * par, pos_type pos, int & y) const
3817 Row * tmprow = firstrow;
3820 // find the first row of the specified paragraph
3821 while (tmprow->next() && tmprow->par() != par) {
3822 y += tmprow->height();
3823 tmprow = tmprow->next();
3826 // now find the wanted row
3827 while (tmprow->pos() < pos
3829 && tmprow->next()->par() == par
3830 && tmprow->next()->pos() <= pos) {
3831 y += tmprow->height();
3832 tmprow = tmprow->next();
3839 Row * LyXText::getRowNearY(int & y) const
3841 // If possible we should optimize this method. (Lgb)
3842 Row * tmprow = firstrow;
3845 while (tmprow->next() && tmpy + tmprow->height() <= y) {
3846 tmpy += tmprow->height();
3847 tmprow = tmprow->next();
3850 y = tmpy; // return the real y
3855 int LyXText::getDepth() const
3857 return cursor.par()->getDepth();