1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
17 #include "lyxparagraph.h"
19 #include "support/textutils.h"
20 #include "insets/insetbib.h"
21 #include "insets/insettext.h"
22 #include "lyx_gui_misc.h"
24 #include "bufferparams.h"
26 #include "minibuffer.h"
35 #include "lyxscreen.h"
36 #include "bufferview_funcs.h"
43 static const int LYX_PAPER_MARGIN = 20;
45 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
48 int LyXText::workWidth(BufferView * bview) const
51 return inset_owner->textWidth(bview->painter());
53 return bview->workWidth();
57 int LyXText::GetRealCursorX(BufferView * bview) const
60 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
61 x = the_locking_inset->getLyXText(bview)->GetRealCursorX(bview);
66 unsigned char LyXText::TransformChar(unsigned char c, LyXParagraph * par,
67 LyXParagraph::size_type pos) const
69 if (!Encodings::is_arabic(c))
70 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
71 return c + (0xb0 - '0');
75 unsigned char const prev_char = pos > 0 ? par->GetChar(pos-1) : ' ';
76 unsigned char next_char = ' ';
78 for (LyXParagraph::size_type i = pos+1; i < par->Last(); ++i)
80 for (LyXParagraph::size_type i = pos+1; i < par->size(); ++i)
82 if (!Encodings::IsComposeChar_arabic(par->GetChar(i))) {
83 next_char = par->GetChar(i);
87 if (Encodings::is_arabic(next_char)) {
88 if (Encodings::is_arabic(prev_char))
89 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
91 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
93 if (Encodings::is_arabic(prev_char))
94 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
96 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
100 // This is the comments that some of the warnings below refers to.
101 // There are some issues in this file and I don't think they are
102 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
103 // this is a problem that has been here almost from day one and that a
104 // larger userbase with differenct access patters triggers the bad
105 // behaviour. (segfaults.) What I think happen is: In several places
106 // we store the paragraph in the current cursor and then moves the
107 // cursor. This movement of the cursor will delete paragraph at the
108 // old position if it is now empty. This will make the temporary
109 // pointer to the old cursor paragraph invalid and dangerous to use.
110 // And is some cases this will trigger a segfault. I have marked some
111 // of the cases where this happens with a warning, but I am sure there
112 // are others in this file and in text2.C. There is also a note in
113 // Delete() that you should read. In Delete I store the paragraph->id
114 // instead of a pointer to the paragraph. I am pretty sure this faulty
115 // use of temporary pointers to paragraphs that might have gotten
116 // invalidated (through a cursor movement) before they are used, are
117 // the cause of the strange crashes we get reported often.
119 // It is very tiresom to change this code, especially when it is as
120 // hard to read as it is. Help to fix all the cases where this is done
121 // would be greately appreciated.
125 int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
126 LyXParagraph::size_type pos) const
128 char const c = par->GetChar(pos);
129 return SingleWidth(bview, par, pos, c);
133 int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
134 LyXParagraph::size_type pos, char c) const
136 LyXFont font = GetFont(bview->buffer(), par, pos);
138 // The most common case is handled first (Asger)
139 if (IsPrintable(c)) {
140 if (font.language()->RightToLeft()) {
141 if (font.language()->lang() == "arabic" &&
142 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
143 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
144 if (Encodings::IsComposeChar_arabic(c))
147 c = TransformChar(c, par, pos);
148 else if (font.language()->lang() == "hebrew" &&
149 Encodings::IsComposeChar_hebrew(c))
152 return lyxfont::width(c, font);
154 } else if (IsHfillChar(c)) {
155 return 3; /* Because of the representation
156 * as vertical lines */
158 } else if (c == LyXParagraph::META_FOOTNOTE ||
159 c == LyXParagraph::META_MARGIN ||
160 c == LyXParagraph::META_FIG ||
161 c == LyXParagraph::META_TAB ||
162 c == LyXParagraph::META_WIDE_FIG ||
163 c == LyXParagraph::META_WIDE_TAB ||
164 c == LyXParagraph::META_ALGORITHM) {
167 case LyXParagraph::META_MARGIN:
170 case LyXParagraph::META_FIG:
173 case LyXParagraph::META_TAB:
176 case LyXParagraph::META_ALGORITHM:
179 case LyXParagraph::META_WIDE_FIG:
182 case LyXParagraph::META_WIDE_TAB:
185 case LyXParagraph::META_FOOTNOTE:
191 return lyxfont::width(fs, font);
193 } else if (c == LyXParagraph::META_INSET) {
194 Inset * tmpinset = par->GetInset(pos);
196 tmpinset->update(bview, font);
197 return tmpinset->width(bview, font);
201 } else if (IsSeparatorChar(c))
203 else if (IsNewlineChar(c))
205 return lyxfont::width(c, font);
209 // Returns the paragraph position of the last character in the specified row
210 LyXParagraph::size_type LyXText::RowLast(Row const * row) const
213 if (row->next() == 0)
214 return row->par()->Last() - 1;
215 else if (row->next()->par() != row->par())
216 return row->par()->Last() - 1;
218 return row->next()->pos() - 1;
220 if (row->next() == 0)
221 return row->par()->size() - 1;
222 else if (row->next()->par() != row->par())
223 return row->par()->size() - 1;
225 return row->next()->pos() - 1;
230 LyXParagraph::size_type LyXText::RowLastPrintable(Row const * row) const
232 LyXParagraph::size_type const last = RowLast(row);
233 if (last >= row->pos()
235 && row->next()->par() == row->par()
236 && row->par()->IsSeparator(last))
243 void LyXText::ComputeBidiTables(Buffer const * buf, Row * row) const
245 bidi_same_direction = true;
246 if (!lyxrc.rtl_support) {
251 bidi_start = row->pos();
252 bidi_end = RowLastPrintable(row);
254 if (bidi_start > bidi_end) {
259 if (bidi_end + 2 - bidi_start >
260 static_cast<LyXParagraph::size_type>(log2vis_list.size())) {
261 LyXParagraph::size_type new_size =
262 (bidi_end + 2 - bidi_start < 500) ?
263 500 : 2 * (bidi_end + 2 - bidi_start);
264 log2vis_list.resize(new_size);
265 vis2log_list.resize(new_size);
266 bidi_levels.resize(new_size);
269 vis2log_list[bidi_end + 1 - bidi_start] = -1;
270 log2vis_list[bidi_end + 1 - bidi_start] = -1;
272 LyXParagraph::size_type stack[2];
274 row->par()->getParLanguage(buf->params)->RightToLeft();
278 LyXParagraph::size_type const main_body =
279 BeginningOfMainBody(buf, row->par());
281 for (LyXParagraph::size_type lpos = bidi_start;
282 lpos <= bidi_end; ++lpos) {
283 bool is_space = row->par()->IsLineSeparator(lpos);
284 LyXParagraph::size_type const pos =
285 (is_space && lpos + 1 <= bidi_end &&
286 !row->par()->IsLineSeparator(lpos + 1) &&
287 !row->par()->IsNewline(lpos + 1))
289 LyXFont font = row->par()->GetFontSettings(buf->params, pos);
290 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
291 font.number() == LyXFont::ON &&
292 row->par()->GetFontSettings(buf->params, lpos-1).number()
294 font = row->par()->GetFontSettings(buf->params, lpos);
299 bool new_rtl = font.isVisibleRightToLeft();
300 bool new_rtl0 = font.isRightToLeft();
303 if (lpos == main_body - 1
304 && row->pos() < main_body - 1
306 new_level = (rtl_par) ? 1 : 0;
307 new_rtl = new_rtl0 = rtl_par;
309 new_level = (new_rtl) ? 1 : 2;
311 new_level = (rtl_par) ? 2 : 0;
313 if (is_space && new_level >= level) {
319 int new_level2 = new_level;
321 if (level == new_level && rtl0 != new_rtl0) {
323 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
324 } else if (level < new_level) {
325 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
326 if (new_level > rtl_par)
327 bidi_same_direction = false;
329 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
332 bidi_levels[lpos - bidi_start] = new_level;
334 while (level > new_level2) {
335 LyXParagraph::size_type old_lpos =
337 int delta = lpos - old_lpos - 1;
340 log2vis_list[lpos - bidi_start] += delta;
341 log2vis_list[old_lpos - bidi_start] += delta;
343 while (level < new_level)
344 stack[level++] = lpos;
348 LyXParagraph::size_type const old_lpos = stack[--level];
349 int delta = bidi_end - old_lpos;
352 log2vis_list[old_lpos - bidi_start] += delta;
355 LyXParagraph::size_type vpos = bidi_start - 1;
356 for (LyXParagraph::size_type lpos = bidi_start;
357 lpos <= bidi_end; ++lpos) {
358 vpos += log2vis_list[lpos - bidi_start];
359 vis2log_list[vpos - bidi_start] = lpos;
360 log2vis_list[lpos - bidi_start] = vpos;
365 // This method requires a previous call to ComputeBidiTables()
366 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
367 LyXParagraph::size_type pos) const
369 if (!lyxrc.rtl_support || pos == 0)
372 if (!bidi_InRange(pos - 1)) {
373 /// This can happen if pos is the first char of a row.
374 /// Returning false in this case is incorrect!
378 bool const rtl = bidi_level(pos - 1) % 2;
379 bool rtl2 = bidi_InRange(pos)
380 ? bidi_level(pos) % 2
381 : par->isRightToLeftPar(buf->params);
386 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
387 LyXParagraph::size_type pos,
388 LyXFont const & font) const
390 if (!lyxrc.rtl_support)
391 return false; // This is just for speedup
393 bool const rtl = font.isVisibleRightToLeft();
394 bool rtl2 = bidi_InRange(pos)
395 ? bidi_level(pos) % 2
396 : par->isRightToLeftPar(buf->params);
401 void LyXText::draw(BufferView * bview, Row const * row,
402 LyXParagraph::size_type & vpos,
403 int offset, float & x, bool cleared)
405 Painter & pain = bview->painter();
407 LyXParagraph::size_type pos = vis2log(vpos);
408 char c = row->par()->GetChar(pos);
411 if (IsNewlineChar(c)) {
413 // Draw end-of-line marker
414 LyXFont const font = GetFont(bview->buffer(), row->par(), pos);
415 int const wid = lyxfont::width('n', font);
416 int const asc = lyxfont::maxAscent(font);
417 int const y = offset + row->baseline();
420 if (bidi_level(pos) % 2 == 0) {
421 xp[0] = int(x + wid * 0.375);
422 yp[0] = int(y - 0.875 * asc * 0.75);
425 yp[1] = int(y - 0.500 * asc * 0.75);
427 xp[2] = int(x + wid * 0.375);
428 yp[2] = int(y - 0.125 * asc * 0.75);
430 pain.lines(xp, yp, 3, LColor::eolmarker);
433 yp[0] = int(y - 0.500 * asc * 0.75);
435 xp[1] = int(x + wid);
436 yp[1] = int(y - 0.500 * asc * 0.75);
438 xp[2] = int(x + wid);
439 yp[2] = int(y - asc * 0.75);
441 pain.lines(xp, yp, 3, LColor::eolmarker);
443 xp[0] = int(x + wid * 0.625);
444 yp[0] = int(y - 0.875 * asc * 0.75);
446 xp[1] = int(x + wid);
447 yp[1] = int(y - 0.500 * asc * 0.75);
449 xp[2] = int(x + wid * 0.625);
450 yp[2] = int(y - 0.125 * asc * 0.75);
452 pain.lines(xp, yp, 3, LColor::eolmarker);
454 xp[0] = int(x + wid);
455 yp[0] = int(y - 0.500 * asc * 0.75);
458 yp[1] = int(y - 0.500 * asc * 0.75);
461 yp[2] = int(y - asc * 0.75);
463 pain.lines(xp, yp, 3, LColor::eolmarker);
469 LyXFont font = GetFont(bview->buffer(), row->par(), pos);
470 LyXFont font2 = font;
472 if (c == LyXParagraph::META_FOOTNOTE
473 || c == LyXParagraph::META_MARGIN
474 || c == LyXParagraph::META_FIG
475 || c == LyXParagraph::META_TAB
476 || c == LyXParagraph::META_WIDE_FIG
477 || c == LyXParagraph::META_WIDE_TAB
478 || c == LyXParagraph::META_ALGORITHM) {
481 case LyXParagraph::META_MARGIN:
484 case LyXParagraph::META_FIG:
487 case LyXParagraph::META_TAB:
490 case LyXParagraph::META_ALGORITHM:
493 case LyXParagraph::META_WIDE_FIG:
496 case LyXParagraph::META_WIDE_TAB:
499 case LyXParagraph::META_FOOTNOTE:
506 // calculate the position of the footnotemark
507 int const y = (row->baseline() - lyxfont::maxAscent(font2)
508 + lyxfont::maxAscent(font));
510 font.setColor(LColor::footnote);
512 // draw it and set new x position
514 pain.text(int(x), offset + y, fs, font);
515 x += lyxfont::width(fs, font);
516 pain.line(int(tmpx), offset + row->baseline(),
517 int(x), offset + row->baseline(),
524 if (c == LyXParagraph::META_INSET) {
525 Inset * tmpinset = row->par()->GetInset(pos);
527 // tmpinset->update(bview, font, false);
528 tmpinset->draw(bview, font, offset+row->baseline(), x,
530 #ifdef SEEMS_TO_BE_NOT_NEEDED
531 if (status == CHANGED_IN_DRAW) {
532 UpdateInset(bview, tmpinset);
533 status = CHANGED_IN_DRAW;
539 if (lyxrc.mark_foreign_language &&
540 font.language() != bview->buffer()->params.language) {
541 int const y = offset + row->height() - 1;
542 pain.line(int(tmpx), y, int(x), y,
549 /* usual characters, no insets */
551 // Collect character that we can draw in one command
553 // This is dirty, but fast. Notice that it will never be too small.
554 // For the record, I'll note that Microsoft Word has a limit
555 // of 768 here. We have none :-) (Asger)
556 // Ok. I am the first to admit that the use of std::string will be
557 // a tiny bit slower than using a POD char array. However, I claim
558 // that this slowdown is so small that it is close to inperceptive.
559 // So IMHO we should go with the easier and clearer implementation.
560 // And even if 1024 is a large number here it might overflow, string
561 // will only overflow if the machine is out of memory...
562 static string textstring;
566 LyXParagraph::size_type const last = RowLastPrintable(row);
568 if (font.language()->lang() == "hebrew") {
569 if (Encodings::IsComposeChar_hebrew(c)) {
570 int const width = lyxfont::width(c, font2);
572 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
573 c = row->par()->GetChar(i);
574 if (!Encodings::IsComposeChar_hebrew(c)) {
575 if (IsPrintableNonspace(c)) {
580 dx = (c == 'ø' || c == 'ã') // dalet / resh
581 ? width2 - width : (width2 - width) / 2;
587 pain.text(int(x) + dx, offset + row->baseline(),
590 while (vpos <= last &&
591 (pos = vis2log(vpos)) >= 0
592 && IsPrintableNonspace(c = row->par()->GetChar(pos))
593 && !Encodings::IsComposeChar_hebrew(c)
594 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
598 // Draw text and set the new x position
599 pain.text(int(x), offset + row->baseline(),
601 x += lyxfont::width(textstring, font);
603 } else if (font.language()->lang() == "arabic" &&
604 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
605 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
606 if (Encodings::IsComposeChar_arabic(c)) {
607 c = TransformChar(c, row->par(), pos);
609 int const width = lyxfont::width(c, font2);
611 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
612 c = row->par()->GetChar(i);
613 if (!Encodings::IsComposeChar_arabic(c)) {
614 if (IsPrintableNonspace(c)) {
619 dx = (width2 - width) / 2;
625 pain.text(int(x) + dx, offset + row->baseline(),
628 textstring = TransformChar(c, row->par(), pos);
629 while (vpos <= last &&
630 (pos = vis2log(vpos)) >= 0
631 && IsPrintableNonspace(c = row->par()->GetChar(pos))
632 && !Encodings::IsComposeChar_arabic(c)
633 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
634 c = TransformChar(c, row->par(), pos);
638 // Draw text and set the new x position
639 pain.text(int(x), offset + row->baseline(),
641 x += lyxfont::width(textstring, font);
644 while (vpos <= last &&
645 (pos = vis2log(vpos)) >= 0
646 && IsPrintableNonspace(c = row->par()->GetChar(pos))
647 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
651 // Draw text and set the new x position
652 pain.text(int(x), offset + row->baseline(), textstring, font);
653 x += lyxfont::width(textstring, font);
656 if (lyxrc.mark_foreign_language &&
657 font.language() != bview->buffer()->params.language) {
658 int const y = offset + row->height() - 1;
659 pain.line(int(tmpx), y, int(x), y,
663 // If we want ulem.sty support, drawing
664 // routines should go here. (Asger)
665 // Why shouldn't LyXFont::drawText handle it internally?
669 // Returns the left beginning of the text.
670 // This information cannot be taken from the layouts-objekt, because in
671 // LaTeX the beginning of the text fits in some cases (for example sections)
672 // exactly the label-width.
673 int LyXText::LeftMargin(BufferView * bview, Row const * row) const
675 LyXLayout const & layout =
676 textclasslist.Style(bview->buffer()->params.textclass,
677 row->par()->GetLayout());
679 string parindent = layout.parindent;
681 int x = LYX_PAPER_MARGIN;
683 x += lyxfont::signedWidth(textclasslist
684 .TextClass(bview->buffer()->params.textclass)
687 .TextClass(bview->buffer()->params.textclass)
690 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
691 LyXFont font(LyXFont::ALL_SANE);
692 font.setSize(LyXFont::SIZE_SMALL);
693 x += lyxfont::width("Mwide-figM", font)
694 + LYX_PAPER_MARGIN/2;
697 // this is the way, LyX handles the LaTeX-Environments.
698 // I have had this idea very late, so it seems to be a
699 // later added hack and this is true
700 if (!row->par()->GetDepth()) {
701 if (!row->par()->GetLayout()) {
702 // find the previous same level paragraph
704 if (row->par()->FirstPhysicalPar()->previous()) {
706 if (row->par()->previous()) {
708 LyXParagraph * newpar = row->par()
709 ->DepthHook(row->par()->GetDepth());
711 textclasslist.Style(bview->buffer()->params.textclass,
718 // find the next level paragraph
720 LyXParagraph * newpar =
721 row->par()->DepthHook(row->par()->GetDepth()-1);
723 // make a corresponding row. Needed to call LeftMargin()
725 // check wether it is a sufficent paragraph
728 && newpar->footnoteflag == row->par()->footnoteflag
731 .Style(bview->buffer()->params.textclass,
732 newpar->GetLayout()).isEnvironment()) {
734 dummyrow.par(newpar);
736 dummyrow.pos(newpar->Last());
738 dummyrow.pos(newpar->size());
740 x = LeftMargin(bview, &dummyrow);
742 // this is no longer an error, because this function
743 // is used to clear impossible depths after changing
744 // a layout. Since there is always a redo,
745 // LeftMargin() is always called
747 row->par()->FirstPhysicalPar()->params.depth(0);
749 row->par()->params.depth(0);
753 if (newpar && !row->par()->GetLayout()) {
755 if (newpar->FirstPhysicalPar()->params.noindent())
757 if (newpar->params.noindent())
761 parindent = textclasslist
762 .Style(bview->buffer()->params.textclass,
763 newpar->GetLayout()).parindent;
768 LyXFont const labelfont = GetFont(bview->buffer(), row->par(), -2);
769 switch (layout.margintype) {
771 if (!layout.leftmargin.empty()) {
772 x += lyxfont::signedWidth(layout.leftmargin,
774 .TextClass(bview->buffer()->params.
778 if (!row->par()->GetLabelstring().empty()) {
779 x += lyxfont::signedWidth(layout.labelindent,
781 x += lyxfont::width(row->par()->GetLabelstring(),
783 x += lyxfont::width(layout.labelsep, labelfont);
787 x += lyxfont::signedWidth(layout.labelindent, labelfont);
788 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
789 if (!row->par()->GetLabelWidthString().empty()) {
790 x += lyxfont::width(row->par()->GetLabelWidthString(),
792 x += lyxfont::width(layout.labelsep, labelfont);
797 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
798 / (row->par()->GetDepth() + 4);
800 case MARGIN_FIRST_DYNAMIC:
801 if (layout.labeltype == LABEL_MANUAL) {
802 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
803 x += lyxfont::signedWidth(layout.leftmargin,
806 x += lyxfont::signedWidth(layout.labelindent,
809 } else if (row->pos()
810 // Special case to fix problems with
812 || (layout.labeltype == LABEL_STATIC
813 && layout.latextype == LATEX_ENVIRONMENT
814 && ! row->par()->IsFirstInSequence())) {
815 x += lyxfont::signedWidth(layout.leftmargin,
817 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
818 && layout.labeltype != LABEL_BIBLIO
819 && layout.labeltype !=
820 LABEL_CENTERED_TOP_ENVIRONMENT) {
821 x += lyxfont::signedWidth(layout.labelindent,
823 x += lyxfont::width(layout.labelsep, labelfont);
824 x += lyxfont::width(row->par()->GetLabelstring(),
829 case MARGIN_RIGHT_ADDRESS_BOX:
831 // ok, a terrible hack. The left margin depends on the widest
832 // row in this paragraph. Do not care about footnotes, they
833 // are *NOT* allowed in the LaTeX realisation of this layout.
835 // find the first row of this paragraph
836 Row const * tmprow = row;
837 while (tmprow->previous()
838 && tmprow->previous()->par() == row->par())
839 tmprow = tmprow->previous();
841 int minfill = tmprow->fill();
842 while (tmprow->next() && tmprow->next()->par() == row->par()) {
843 tmprow = tmprow->next();
844 if (tmprow->fill() < minfill)
845 minfill = tmprow->fill();
848 x += lyxfont::signedWidth(layout.leftmargin,
850 .TextClass(bview->buffer()->params.textclass)
856 if ((workWidth(bview) > 0) &&
857 (row->par()->params.pextraType() == LyXParagraph::PEXTRA_INDENT)) {
858 if (!row->par()->params.pextraWidthp().empty()) {
859 x += workWidth(bview) *
860 lyx::atoi(row->par()->params.pextraWidthp()) / 100;
861 } else if (!row->par()->params.pextraWidth().empty()) {
862 int xx = VSpace(row->par()->params.pextraWidth()).inPixels(bview);
863 if (xx > workWidth(bview))
864 xx = workWidth(bview) * 80 / 100;
866 } else { // should not happen
867 LyXFont font(LyXFont::ALL_SANE);
868 x += lyxfont::width("XXXXXX", font);
872 int align; // wrong type
874 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
875 align = layout.align;
877 align = row->par()->FirstPhysicalPar()->params.align();
879 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
880 align = layout.align;
882 align = row->par()->params.align();
884 // set the correct parindent
885 if (row->pos() == 0) {
886 if ((layout.labeltype == LABEL_NO_LABEL
887 || layout.labeltype == LABEL_TOP_ENVIRONMENT
888 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
889 || (layout.labeltype == LABEL_STATIC
890 && layout.latextype == LATEX_ENVIRONMENT
891 && ! row->par()->IsFirstInSequence()))
893 && row->par() == row->par()->FirstPhysicalPar()
895 && align == LYX_ALIGN_BLOCK
896 && !row->par()->params.noindent()
897 && (row->par()->layout ||
898 bview->buffer()->params.paragraph_separation ==
899 BufferParams::PARSEP_INDENT))
900 x += lyxfont::signedWidth(parindent,
902 .TextClass(bview->buffer()->params
905 else if (layout.labeltype == LABEL_BIBLIO) {
906 // ale970405 Right width for bibitems
907 x += bibitemMaxWidth(bview,textclasslist
908 .TextClass(bview->buffer()->params
917 int LyXText::RightMargin(Buffer const * buf, Row const * row) const
919 LyXLayout const & layout =
920 textclasslist.Style(buf->params.textclass,
921 row->par()->GetLayout());
923 int x = LYX_PAPER_MARGIN
924 + lyxfont::signedWidth(textclasslist
925 .TextClass(buf->params.textclass)
928 .TextClass(buf->params.textclass)
932 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
933 x += LYX_PAPER_MARGIN / 2;
936 // this is the way, LyX handles the LaTeX-Environments.
937 // I have had this idea very late, so it seems to be a
938 // later added hack and this is true
939 if (row->par()->GetDepth()) {
940 // find the next level paragraph
942 LyXParagraph * newpar = row->par();
946 newpar = newpar->FirstPhysicalPar()->previous();
948 newpar = newpar->FirstPhysicalPar();
949 } while (newpar && newpar->GetDepth() >= row->par()->GetDepth()
950 && newpar->footnoteflag == row->par()->footnoteflag);
953 newpar = newpar->previous();
955 && newpar->GetDepth() >= row->par()->GetDepth());
958 // make a corresponding row. Needed to call LeftMargin()
960 // check wether it is a sufficent paragraph
963 && newpar->footnoteflag == row->par()->footnoteflag
965 && textclasslist.Style(buf->params.textclass,
969 dummyrow.par(newpar);
971 x = RightMargin(buf, &dummyrow);
973 // this is no longer an error, because this function
974 // is used to clear impossible depths after changing
975 // a layout. Since there is always a redo,
976 // LeftMargin() is always called
978 row->par()->FirstPhysicalPar()->params.depth(0);
980 row->par()->params.depth(0);
985 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
986 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
987 .TextClass(buf->params.textclass)
988 .defaultfont()) * 4 / (row->par()->GetDepth()
994 int LyXText::LabelEnd(BufferView * bview, Row const * row) const
996 if (textclasslist.Style(bview->buffer()->params.textclass,
997 row->par()->GetLayout()).margintype
1002 tmprow.pos(row->par()->Last());
1004 tmprow.pos(row->par()->size());
1006 return LeftMargin(bview, &tmprow); /* just the beginning
1009 return 0; /* LabelEnd is only needed, if the
1010 layout fills a flushleft
1015 // get the next breakpoint in a given paragraph
1016 LyXParagraph::size_type
1017 LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
1019 LyXParagraph * par = row->par();
1028 LyXParagraph::size_type const pos = row->pos();
1031 // position of the last possible breakpoint
1032 // -1 isn't a suitable value, but a flag
1033 LyXParagraph::size_type last_separator = -1;
1034 width -= RightMargin(bview->buffer(), row);
1036 LyXParagraph::size_type const main_body =
1037 BeginningOfMainBody(bview->buffer(), par);
1038 LyXLayout const & layout =
1039 textclasslist.Style(bview->buffer()->params.textclass, par->GetLayout());
1040 LyXParagraph::size_type i = pos;
1042 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1043 /* special code for right address boxes, only newlines count */
1045 while (i < par->Last()) {
1047 while (i < par->size()) {
1049 if (par->IsNewline(i)) {
1052 i = par->Last() - 1; // this means break
1054 i = par->size() - 1; // this means break
1057 } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
1058 par->GetInset(i) && par->GetInset(i)->display()){
1059 par->GetInset(i)->display(false);
1064 // Last position is an invariant
1066 LyXParagraph::size_type const last =
1069 LyXParagraph::size_type const last =
1072 // this is the usual handling
1073 int x = LeftMargin(bview, row);
1074 bool doitonetime = true;
1075 while (doitonetime || ((x < width) && (i < last))) {
1076 doitonetime = false;
1077 char const c = par->GetChar(i);
1078 if (IsNewlineChar(c)) {
1080 x = width; // this means break
1081 } else if (c == LyXParagraph::META_INSET &&
1084 // check wether a Display() inset is
1085 // valid here. if not, change it to
1087 if (par->GetInset(i)->display() &&
1088 (layout.isCommand() ||
1089 (layout.labeltype == LABEL_MANUAL
1090 && i < BeginningOfMainBody(bview->buffer(), par)))) {
1091 // display istn't allowd
1092 par->GetInset(i)->display(false);
1093 x += SingleWidth(bview, par, i, c);
1094 } else if (par->GetInset(i)->display() ||
1095 par->GetInset(i)->needFullRow()) {
1096 // So break the line here
1100 if (IsLineSeparatorChar(par->GetChar(i+1)))
1103 last_separator = last; // to avoid extra rows
1105 last_separator = i - 1;
1106 x = width; // this means break
1112 x += SingleWidth(bview, par, i, c);
1116 if (IsLineSeparatorChar(c))
1118 x += SingleWidth(bview, par, i, c);
1121 if (i == main_body) {
1122 x += lyxfont::width(layout.labelsep,
1123 GetFont(bview->buffer(), par, -2));
1124 if (par->IsLineSeparator(i - 1))
1125 x-= SingleWidth(bview, par, i - 1);
1126 int left_margin = LabelEnd(bview, row);
1127 if (x < left_margin)
1131 // end of paragraph is always a suitable separator
1132 if (i == last && x < width)
1136 // well, if last_separator is still 0, the line isn't breakable.
1137 // don't care and cut simply at the end
1138 if (last_separator < 0) {
1142 // manual labels cannot be broken in LaTeX, do not care
1143 if (main_body && last_separator < main_body)
1144 last_separator = main_body - 1;
1146 return last_separator;
1150 // returns the minimum space a row needs on the screen in pixel
1151 int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
1153 if (paper_width < 0)
1157 // get the pure distance
1158 LyXParagraph::size_type const last = RowLastPrintable(row);
1160 // special handling of the right address boxes
1161 if (textclasslist.Style(bview->buffer()->params.textclass,
1162 row->par()->GetLayout()).margintype
1163 == MARGIN_RIGHT_ADDRESS_BOX) {
1164 int const tmpfill = row->fill();
1165 row->fill(0); // the minfill in MarginLeft()
1166 w = LeftMargin(bview, row);
1169 w = LeftMargin(bview, row);
1171 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1172 row->par()->GetLayout());
1173 LyXParagraph::size_type const main_body =
1174 BeginningOfMainBody(bview->buffer(), row->par());
1175 LyXParagraph::size_type i = row->pos();
1178 if (main_body > 0 && i == main_body) {
1179 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1180 if (row->par()->IsLineSeparator(i - 1))
1181 w -= SingleWidth(bview, row->par(), i - 1);
1182 int left_margin = LabelEnd(bview, row);
1183 if (w < left_margin)
1186 w += SingleWidth(bview, row->par(), i);
1189 if (main_body > 0 && main_body > last) {
1190 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1191 if (last >= 0 && row->par()->IsLineSeparator(last))
1192 w -= SingleWidth(bview, row->par(), last);
1193 int const left_margin = LabelEnd(bview, row);
1194 if (w < left_margin)
1198 int const fill = paper_width - w - RightMargin(bview->buffer(), row);
1203 // returns the minimum space a manual label needs on the screen in pixel
1204 int LyXText::LabelFill(BufferView * bview, Row const * row) const
1206 LyXParagraph::size_type last = BeginningOfMainBody(bview->buffer(), row->par()) - 1;
1207 // -1 because a label ends either with a space that is in the label,
1208 // or with the beginning of a footnote that is outside the label.
1210 // I don't understand this code in depth, but sometimes "last" is
1211 // less than 0 and this causes a crash. This fix seems to work
1212 // correctly, but I bet the real error is elsewhere. The bug is
1213 // triggered when you have an open footnote in a paragraph
1214 // environment with a manual label. (Asger)
1215 if (last < 0) last = 0;
1217 if (row->par()->IsLineSeparator(last)) /* a sepearator at this end
1224 w += SingleWidth(bview, row->par(), i);
1229 if (!row->par()->params.labelWidthString().empty()) {
1230 fill = max(lyxfont::width(row->par()->params.labelWidthString(),
1231 GetFont(bview->buffer(), row->par(), -2)) - w,
1239 // returns the number of separators in the specified row. The separator
1240 // on the very last column doesnt count
1241 int LyXText::NumberOfSeparators(Buffer const * buf, Row const * row) const
1243 LyXParagraph::size_type const last = RowLast(row);
1244 LyXParagraph::size_type p =
1245 max(row->pos(), BeginningOfMainBody(buf, row->par()));
1247 for (; p < last; ++p) {
1248 if (row->par()->IsSeparator(p)) {
1256 // returns the number of hfills in the specified row. The LyX-Hfill is
1257 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1258 // ignored. This is *MUCH* more usefull than not to ignore!
1259 int LyXText::NumberOfHfills(Buffer const * buf, Row const * row) const
1261 LyXParagraph::size_type const last = RowLast(row);
1262 LyXParagraph::size_type first = row->pos();
1263 if (first) { /* hfill *DO* count at the beginning
1265 while(first <= last && row->par()->IsHfill(first))
1269 first = max(first, BeginningOfMainBody(buf, row->par()));
1271 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1272 if (row->par()->IsHfill(p)) {
1280 // like NumberOfHfills, but only those in the manual label!
1281 int LyXText::NumberOfLabelHfills(Buffer const * buf, Row const * row) const
1283 LyXParagraph::size_type last = RowLast(row);
1284 LyXParagraph::size_type first = row->pos();
1285 if (first) { /* hfill *DO* count at the beginning
1287 while(first < last && row->par()->IsHfill(first))
1291 last = min(last, BeginningOfMainBody(buf, row->par()));
1293 for (LyXParagraph::size_type p = first;
1294 p < last; ++p) { // last, because the end is ignored!
1295 if (row->par()->IsHfill(p)) {
1303 // returns true, if a expansion is needed.
1304 // Rules are given by LaTeX
1305 bool LyXText::HfillExpansion(Buffer const * buf, Row const * row_ptr,
1306 LyXParagraph::size_type pos) const
1308 // by the way, is it a hfill?
1309 if (!row_ptr->par()->IsHfill(pos))
1312 // at the end of a row it does not count
1313 if (pos >= RowLast(row_ptr))
1316 // at the beginning of a row it does not count, if it is not
1317 // the first row of a paragaph
1318 if (!row_ptr->pos())
1321 // in some labels it does not count
1322 if (textclasslist.Style(buf->params.textclass,
1323 row_ptr->par()->GetLayout()).margintype
1325 && pos < BeginningOfMainBody(buf, row_ptr->par()))
1328 // if there is anything between the first char of the row and
1329 // the sepcified position that is not a newline and not a hfill,
1330 // the hfill will count, otherwise not
1331 LyXParagraph::size_type i = row_ptr->pos();
1332 while (i < pos && (row_ptr->par()->IsNewline(i)
1333 || row_ptr->par()->IsHfill(i)))
1340 void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
1342 /* get the maximum ascent and the maximum descent */
1345 float layoutasc = 0;
1346 float layoutdesc = 0;
1349 Inset * tmpinset = 0;
1351 /* this must not happen before the currentrow for clear reasons.
1352 so the trick is just to set the current row onto this row */
1354 GetRow(row_ptr->par(), row_ptr->pos(), unused_y);
1356 /* ok , let us initialize the maxasc and maxdesc value.
1357 * This depends in LaTeX of the font of the last character
1358 * in the paragraph. The hack below is necessary because
1359 * of the possibility of open footnotes */
1361 /* Correction: only the fontsize count. The other properties
1362 are taken from the layoutfont. Nicer on the screen :) */
1364 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
1365 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
1367 LyXParagraph * par = row_ptr->par();
1368 LyXParagraph * firstpar = row_ptr->par();
1371 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1372 firstpar->GetLayout());
1375 LyXFont font = GetFont(bview->buffer(), par, par->Last() - 1);
1377 LyXFont font = GetFont(bview->buffer(), par, par->size() - 1);
1379 LyXFont::FONT_SIZE const size = font.size();
1380 font = GetFont(bview->buffer(), par, -1);
1383 LyXFont labelfont = GetFont(bview->buffer(), par, -2);
1385 float spacing_val = 1.0;
1386 if (!row_ptr->par()->params.spacing().isDefault()) {
1387 spacing_val = row_ptr->par()->params.spacing().getValue();
1389 spacing_val = bview->buffer()->params.spacing.getValue();
1391 //lyxerr << "spacing_val = " << spacing_val << endl;
1393 int maxasc = int(lyxfont::maxAscent(font) *
1394 layout.spacing.getValue() *
1396 int maxdesc = int(lyxfont::maxDescent(font) *
1397 layout.spacing.getValue() *
1399 int const pos_end = RowLast(row_ptr);
1403 // Check if any insets are larger
1404 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1405 if (row_ptr->par()->GetChar(pos) == LyXParagraph::META_INSET) {
1406 tmpfont = GetFont(bview->buffer(), row_ptr->par(), pos);
1407 tmpinset = row_ptr->par()->GetInset(pos);
1409 tmpinset->update(bview, tmpfont);
1410 asc = tmpinset->ascent(bview, tmpfont);
1411 desc = tmpinset->descent(bview, tmpfont);
1412 maxwidth += tmpinset->width(bview, tmpfont);
1413 maxasc = max(maxasc, asc);
1414 maxdesc = max(maxdesc, desc);
1417 maxwidth += SingleWidth(bview, row_ptr->par(), pos);
1421 // Check if any custom fonts are larger (Asger)
1422 // This is not completely correct, but we can live with the small,
1423 // cosmetic error for now.
1424 LyXFont::FONT_SIZE const maxsize =
1425 row_ptr->par()->HighestFontInRange(row_ptr->pos(),
1427 if (maxsize > font.size()) {
1428 font.setSize(maxsize);
1430 asc = lyxfont::maxAscent(font);
1431 desc = lyxfont::maxDescent(font);
1438 // This is nicer with box insets:
1442 row_ptr->ascent_of_text(maxasc);
1444 /* is it a top line? */
1445 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1447 /* some parksips VERY EASY IMPLEMENTATION */
1448 if (bview->buffer()->params.paragraph_separation ==
1449 BufferParams::PARSEP_SKIP) {
1450 if (layout.isParagraph()
1451 && firstpar->GetDepth() == 0
1452 && firstpar->previous())
1453 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1454 else if (firstpar->previous()
1455 && textclasslist.Style(bview->buffer()->params.textclass,
1456 firstpar->previous()->GetLayout()).isParagraph()
1457 && firstpar->previous()->GetDepth() == 0)
1458 // is it right to use defskip here too? (AS)
1459 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1462 /* the paper margins */
1464 if (!row_ptr->par()->previous_ && bv_owner)
1465 maxasc += LYX_PAPER_MARGIN;
1467 if (!row_ptr->par()->previous() && bv_owner)
1468 maxasc += LYX_PAPER_MARGIN;
1471 /* add the vertical spaces, that the user added */
1472 if (firstpar->params.spaceTop().kind() != VSpace::NONE)
1473 maxasc += int(firstpar->params.spaceTop().inPixels(bview));
1475 /* do not forget the DTP-lines!
1476 * there height depends on the font of the nearest character */
1477 if (firstpar->params.lineTop())
1478 maxasc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1481 /* and now the pagebreaks */
1482 if (firstpar->params.pagebreakTop())
1483 maxasc += 3 * DefaultHeight();
1485 /* this is special code for the chapter, since the label of this
1486 * layout is printed in an extra row */
1487 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1488 && bview->buffer()->params.secnumdepth >= 0) {
1489 float spacing_val = 1.0;
1490 if (!row_ptr->par()->params.spacing().isDefault()) {
1491 spacing_val = row_ptr->par()->params.spacing().getValue();
1493 spacing_val = bview->buffer()->params.spacing.getValue();
1496 labeladdon = int(lyxfont::maxDescent(labelfont) *
1497 layout.spacing.getValue() *
1499 + int(lyxfont::maxAscent(labelfont) *
1500 layout.spacing.getValue() *
1504 /* special code for the top label */
1505 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1506 || layout.labeltype == LABEL_BIBLIO
1507 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1508 && row_ptr->par()->IsFirstInSequence()
1509 && !row_ptr->par()->GetLabelstring().empty()) {
1510 float spacing_val = 1.0;
1511 if (!row_ptr->par()->params.spacing().isDefault()) {
1512 spacing_val = row_ptr->par()->params.spacing().getValue();
1514 spacing_val = bview->buffer()->params.spacing.getValue();
1518 (lyxfont::maxAscent(labelfont) *
1519 layout.spacing.getValue() *
1521 +(lyxfont::maxDescent(labelfont) *
1522 layout.spacing.getValue() *
1524 + layout.topsep * DefaultHeight()
1525 + layout.labelbottomsep * DefaultHeight());
1528 /* and now the layout spaces, for example before and after a section,
1529 * or between the items of a itemize or enumerate environment */
1531 if (!firstpar->params.pagebreakTop()) {
1532 LyXParagraph * prev = row_ptr->par()->previous();
1534 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth());
1535 if (prev && prev->GetLayout() == firstpar->GetLayout()
1536 && prev->GetDepth() == firstpar->GetDepth()
1537 && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1539 layoutasc = (layout.itemsep * DefaultHeight());
1541 else if (row_ptr->previous()) {
1542 tmptop = layout.topsep;
1544 if (row_ptr->previous()->par()->GetDepth() >= row_ptr->par()->GetDepth())
1545 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1546 row_ptr->previous()->par()->
1547 GetLayout()).bottomsep;
1550 layoutasc = (tmptop * DefaultHeight());
1552 else if (row_ptr->par()->params.lineTop()) {
1553 tmptop = layout.topsep;
1556 layoutasc = (tmptop * DefaultHeight());
1559 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth()-1);
1561 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1562 prev->GetLayout()).parsep * DefaultHeight());
1565 if (firstpar->previous()
1566 && firstpar->previous()->GetDepth() == 0
1567 && firstpar->previous()->GetLayout() != firstpar->GetLayout()) {
1570 else if (firstpar->previous()){
1571 maxasc += int(layout.parsep * DefaultHeight());
1577 /* is it a bottom line? */
1580 row_ptr->par()->ParFromPos(RowLast(row_ptr) + 1) == par
1582 row_ptr->par() == par
1584 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1586 /* the paper margins */
1588 if (!par->next_ && bv_owner)
1589 maxdesc += LYX_PAPER_MARGIN;
1591 if (!par->next() && bv_owner)
1592 maxdesc += LYX_PAPER_MARGIN;
1595 /* add the vertical spaces, that the user added */
1596 if (firstpar->params.spaceBottom().kind() != VSpace::NONE)
1597 maxdesc += int(firstpar->params.spaceBottom().inPixels(bview));
1599 /* do not forget the DTP-lines!
1600 * there height depends on the font of the nearest character */
1602 if (firstpar->params.lineBottom())
1603 maxdesc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1604 par, par->Last() - 1));
1606 if (firstpar->params.lineBottom())
1607 maxdesc += 2 * lyxfont::ascent('x',
1608 GetFont(bview->buffer(),
1609 par, par->size() - 1));
1612 /* and now the pagebreaks */
1613 if (firstpar->params.pagebreakBottom())
1614 maxdesc += 3 * DefaultHeight();
1616 /* and now the layout spaces, for example before and after a section,
1617 * or between the items of a itemize or enumerate environment */
1618 if (!firstpar->params.pagebreakBottom() && row_ptr->par()->next()) {
1619 LyXParagraph * nextpar = row_ptr->par()->next();
1620 LyXParagraph * comparepar = row_ptr->par();
1624 if (comparepar->GetDepth() > nextpar->GetDepth()) {
1625 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1626 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1627 if (comparepar->GetLayout()!= nextpar->GetLayout()
1628 || nextpar->GetLabelWidthString() !=
1629 comparepar->GetLabelWidthString())
1630 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1632 if (unusual > usual)
1633 layoutdesc = unusual;
1637 else if (comparepar->GetDepth() == nextpar->GetDepth()) {
1639 if (comparepar->GetLayout()!= nextpar->GetLayout()
1640 || nextpar->GetLabelWidthString() !=
1641 comparepar->GetLabelWidthString())
1642 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1647 /* incalculate the layout spaces */
1648 maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1649 maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1651 /* calculate the new height of the text */
1652 height -= row_ptr->height();
1654 row_ptr->height(maxasc + maxdesc + labeladdon);
1655 row_ptr->baseline(maxasc + labeladdon);
1657 height += row_ptr->height();
1659 PrepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1660 row_ptr->width(int(maxwidth + x));
1663 width = max(0,workWidth(bview));
1665 if (r->width() > width)
1673 /* Appends the implicit specified paragraph behind the specified row,
1674 * start at the implicit given position */
1675 void LyXText::AppendParagraph(BufferView * bview, Row * row) const
1677 bool not_ready = true;
1679 // The last character position of a paragraph is an invariant so we can
1680 // safely get it here. (Asger)
1682 int const lastposition = row->par()->Last();
1684 int const lastposition = row->par()->size();
1687 // Get the next breakpoint
1688 int z = NextBreakPoint(bview, row, workWidth(bview));
1692 // Insert the new row
1693 if (z < lastposition) {
1695 InsertRow(row, row->par(), z);
1702 // Set the dimensions of the row
1703 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1704 SetHeightOfRow(bview, tmprow);
1706 } while (not_ready);
1710 void LyXText::BreakAgain(BufferView * bview, Row * row) const
1712 bool not_ready = true;
1715 /* get the next breakpoint */
1716 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1720 if (z < row->par()->Last()) {
1722 if (z < row->par()->size()) {
1724 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1727 InsertRow(row, row->par(), z);
1733 if (row->pos() == z)
1734 not_ready = false; // the rest will not change
1740 /* if there are some rows too much, delete them */
1741 /* only if you broke the whole paragraph! */
1742 Row * tmprow2 = row;
1743 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1744 tmprow2 = tmprow2->next();
1746 while (tmprow2 != row) {
1747 tmprow2 = tmprow2->previous();
1748 RemoveRow(tmprow2->next());
1753 /* set the dimensions of the row */
1754 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1755 SetHeightOfRow(bview, tmprow);
1756 } while (not_ready);
1760 // this is just a little changed version of break again
1761 void LyXText::BreakAgainOneRow(BufferView * bview, Row * row)
1763 // get the next breakpoint
1764 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1768 if (z < row->par()->Last()) {
1770 if (z < row->par()->size()) {
1773 || (row->next() && row->next()->par() != row->par())) {
1774 /* insert a new row */
1776 InsertRow(row, row->par(), z);
1782 if (row->pos() != z)
1786 // if there are some rows too much, delete them
1787 // only if you broke the whole paragraph!
1788 Row * tmprow2 = row;
1789 while (tmprow2->next()
1790 && tmprow2->next()->par() == row->par()) {
1791 tmprow2 = tmprow2->next();
1793 while (tmprow2 != row) {
1794 tmprow2 = tmprow2->previous();
1795 RemoveRow(tmprow2->next());
1799 // set the dimensions of the row
1800 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1801 SetHeightOfRow(bview, tmprow);
1805 void LyXText::BreakParagraph(BufferView * bview, char keep_layout)
1807 LyXLayout const & layout =
1808 textclasslist.Style(bview->buffer()->params.textclass,
1809 cursor.par()->GetLayout());
1811 // this is only allowed, if the current paragraph is not empty or caption
1813 if ((cursor.par()->Last() <= 0
1814 && !cursor.par()->IsDummy())
1815 && layout.labeltype!= LABEL_SENSITIVE)
1818 SetUndo(bview->buffer(), Undo::INSERT,
1819 cursor.par()->ParFromPos(cursor.pos())->previous_,
1820 cursor.par()->ParFromPos(cursor.pos())->next_);
1822 if ((cursor.par()->size() <= 0)
1823 && layout.labeltype!= LABEL_SENSITIVE)
1826 SetUndo(bview->buffer(), Undo::INSERT,
1827 cursor.par()->previous(),
1828 cursor.par()->next());
1831 // Always break behind a space
1833 // It is better to erase the space (Dekel)
1835 if (cursor.pos() < cursor.par()->Last()
1837 if (cursor.pos() < cursor.par()->size()
1839 && cursor.par()->IsLineSeparator(cursor.pos()))
1840 cursor.par()->Erase(cursor.pos());
1841 // cursor.pos(cursor.pos() + 1);
1843 // break the paragraph
1847 keep_layout = layout.isEnvironment();
1848 cursor.par()->BreakParagraph(bview->buffer()->params, cursor.pos(),
1851 // well this is the caption hack since one caption is really enough
1852 if (layout.labeltype == LABEL_SENSITIVE) {
1854 // set to standard-layout
1855 cursor.par()->SetLayout(bview->buffer()->params, 0);
1857 // set to standard-layout
1858 cursor.par()->next()->SetLayout(bview->buffer()->params, 0);
1861 /* if the cursor is at the beginning of a row without prior newline,
1863 * This touches only the screen-update. Otherwise we would may have
1864 * an empty row on the screen */
1865 if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1)
1866 && cursor.row()->pos() == cursor.pos()) {
1870 status = LyXText::NEED_MORE_REFRESH;
1871 refresh_row = cursor.row();
1872 refresh_y = cursor.y() - cursor.row()->baseline();
1874 // Do not forget the special right address boxes
1875 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1876 while (refresh_row->previous() &&
1877 refresh_row->previous()->par() == refresh_row->par()) {
1878 refresh_row = refresh_row->previous();
1879 refresh_y -= refresh_row->height();
1882 RemoveParagraph(cursor.row());
1884 // set the dimensions of the cursor row
1885 cursor.row()->fill(Fill(bview, cursor.row(), workWidth(bview)));
1887 SetHeightOfRow(bview, cursor.row());
1890 while (cursor.par()->next()->Last()
1892 while (cursor.par()->next()->size()
1894 && cursor.par()->next()->IsNewline(0))
1895 cursor.par()->next()->Erase(0);
1897 InsertParagraph(bview, cursor.par()->next(), cursor.row());
1899 UpdateCounters(bview, cursor.row()->previous());
1901 /* This check is necessary. Otherwise the new empty paragraph will
1902 * be deleted automatically. And it is more friendly for the user! */
1904 SetCursor(bview, cursor.par()->next(), 0);
1906 SetCursor(bview, cursor.par(), 0);
1908 if (cursor.row()->next())
1909 BreakAgain(bview, cursor.row()->next());
1916 void LyXText::OpenFootnote(BufferView * bview)
1918 LyXParagraph * endpar,* tmppar;
1921 LyXParagraph * par = cursor.par()->ParFromPos(cursor.pos());
1923 /* if there is no footnote in this paragraph, just return. */
1925 || par->next_->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1928 /* ok, move the cursor right before the footnote */
1930 /* just a little faster than using CursorRight() */
1932 cursor.par()->ParFromPos(cursor.pos()) != par;) {
1933 cursor.pos(cursor.pos() + 1);
1936 /* now the cursor is at the beginning of the physical par */
1937 SetCursor(bview, cursor.par(),
1938 cursor.pos() + cursor.par()->ParFromPos(cursor.pos())->size());
1940 /* the cursor must be exactly before the footnote */
1941 par = cursor.par()->ParFromPos(cursor.pos());
1943 status = LyXText::NEED_MORE_REFRESH;
1944 refresh_row = cursor.row();
1945 refresh_y = cursor.y() - cursor.row()->baseline();
1947 tmppar = cursor.par();
1948 endpar = cursor.par()->next();
1951 tmppar->OpenFootnote(cursor.pos());
1952 RemoveParagraph(row);
1953 /* set the dimensions of the cursor row */
1954 row->fill(Fill(bview, row, workWidth(bview)));
1955 SetHeightOfRow(bview, row);
1956 // CHECK See comment on top of text.C
1957 tmppar = tmppar->next();
1959 while (tmppar != endpar) {
1961 InsertParagraph(bview, tmppar, row);
1962 while (row->next() && row->next()->par() == tmppar)
1964 tmppar = tmppar->next();
1967 SetCursor(bview, par->next_, 0);
1968 sel_cursor = cursor;
1973 // Just a macro to make some thing easier.
1974 void LyXText::RedoParagraph(BufferView * bview) const
1976 ClearSelection(bview);
1977 RedoParagraphs(bview, cursor, cursor.par()->next());
1978 SetCursorIntern(bview, cursor.par(), cursor.pos());
1982 /* insert a character, moves all the following breaks in the
1983 * same Paragraph one to the right and make a rebreak */
1984 void LyXText::InsertChar(BufferView * bview, char c)
1986 SetUndo(bview->buffer(), Undo::INSERT,
1988 cursor.par()->ParFromPos(cursor.pos())->previous_,
1989 cursor.par()->ParFromPos(cursor.pos())->next_
1991 cursor.par()->previous(),
1992 cursor.par()->next()
1996 // When the free-spacing option is set for the current layout,
1997 // disable the double-space checking
1999 bool const freeSpacing =
2000 textclasslist.Style(bview->buffer()->params.textclass,
2001 cursor.row()->par()->GetLayout()).free_spacing;
2004 if (lyxrc.auto_number) {
2005 static string const number_operators = "+-/*";
2006 static string const number_unary_operators = "+-";
2007 static string const number_seperators = ".,:";
2009 if (current_font.number() == LyXFont::ON) {
2010 if (!isdigit(c) && !contains(number_operators, c) &&
2011 !(contains(number_seperators, c) &&
2012 cursor.pos() >= 1 &&
2013 cursor.pos() < cursor.par()->size() &&
2014 GetFont(bview->buffer(),
2016 cursor.pos()).number() == LyXFont::ON &&
2017 GetFont(bview->buffer(),
2019 cursor.pos()-1).number() == LyXFont::ON)
2021 Number(bview); // Set current_font.number to OFF
2022 } else if (isdigit(c) &&
2023 real_current_font.isVisibleRightToLeft()) {
2024 Number(bview); // Set current_font.number to ON
2026 if (cursor.pos() > 0) {
2027 char const c = cursor.par()->GetChar(cursor.pos() - 1);
2028 if (contains(number_unary_operators, c) &&
2029 (cursor.pos() == 1 ||
2030 cursor.par()->IsSeparator(cursor.pos() - 2) ||
2031 cursor.par()->IsNewline(cursor.pos() - 2) )
2033 SetCharFont(bview->buffer(),
2037 } else if (contains(number_seperators, c) &&
2038 cursor.pos() >= 2 &&
2039 GetFont(bview->buffer(),
2041 cursor.pos()-2).number() == LyXFont::ON) {
2042 SetCharFont(bview->buffer(),
2052 /* First check, if there will be two blanks together or a blank at
2053 the beginning of a paragraph.
2054 I decided to handle blanks like normal characters, the main
2055 difference are the special checks when calculating the row.fill
2056 (blank does not count at the end of a row) and the check here */
2058 // The bug is triggered when we type in a description environment:
2059 // The current_font is not changed when we go from label to main text
2060 // and it should (along with realtmpfont) when we type the space.
2061 // CHECK There is a bug here! (Asger)
2063 LyXFont realtmpfont = real_current_font;
2064 LyXFont rawtmpfont = current_font; /* store the current font.
2065 * This is because of the use
2066 * of cursor movements. The moving
2067 * cursor would refresh the
2070 // Get the font that is used to calculate the baselineskip
2072 LyXParagraph::size_type const lastpos = cursor.par()->Last();
2074 LyXParagraph::size_type const lastpos = cursor.par()->size();
2076 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2079 bool jumped_over_space = false;
2081 if (!freeSpacing && IsLineSeparatorChar(c)) {
2082 if ((cursor.pos() > 0
2083 && cursor.par()->IsLineSeparator(cursor.pos() - 1))
2084 || (cursor.pos() > 0
2085 && cursor.par()->IsNewline(cursor.pos() - 1))
2086 || (cursor.pos() == 0
2088 && !(cursor.par()->previous()
2089 && cursor.par()->previous()->footnoteflag
2090 == LyXParagraph::OPEN_FOOTNOTE)
2093 if (cursor.pos() == 0 )
2094 bview->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
2096 bview->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way. Please read the Tutorial."));
2100 } else if (IsNewlineChar(c)) {
2102 if (cursor.par()->FirstPhysicalPar() == cursor.par()
2104 if (cursor.par() == cursor.par()
2106 && cursor.pos() <= BeginningOfMainBody(bview->buffer(), cursor.par())) {
2110 /* No newline at first position
2111 * of a paragraph or behind labels.
2112 * TeX does not allow that. */
2115 if (cursor.pos() < cursor.par()->Last() &&
2117 if (cursor.pos() < cursor.par()->size() &&
2119 cursor.par()->IsLineSeparator(cursor.pos()))
2120 // newline always after a blank!
2122 cursor.row()->fill(-1); // to force a new break
2125 // the display inset stuff
2126 if (cursor.row()->par()->GetChar(cursor.row()->pos()) == LyXParagraph::META_INSET
2127 && cursor.row()->par()->GetInset(cursor.row()->pos())
2128 && (cursor.row()->par()->GetInset(cursor.row()->pos())->display() ||
2129 cursor.row()->par()->GetInset(cursor.row()->pos())->needFullRow()))
2130 cursor.row()->fill(-1); // to force a new break
2132 // get the cursor row fist
2133 Row * row = cursor.row();
2134 int y = cursor.y() - row->baseline();
2135 if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset
2136 * already insertet the character */
2137 cursor.par()->InsertChar(cursor.pos(), c);
2138 SetCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
2140 if (!jumped_over_space) {
2141 // refresh the positions
2143 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2144 tmprow = tmprow->next();
2145 tmprow->pos(tmprow->pos() + 1);
2149 // Is there a break one row above
2150 if ((cursor.par()->IsLineSeparator(cursor.pos())
2151 || cursor.par()->IsNewline(cursor.pos())
2152 || cursor.row()->fill() == -1)
2153 && row->previous() && row->previous()->par() == row->par()) {
2154 LyXParagraph::size_type z = NextBreakPoint(bview,
2157 if (z >= row->pos()) {
2160 // set the dimensions of the row above
2161 row->previous()->fill(Fill(bview,
2165 SetHeightOfRow(bview, row->previous());
2167 y -= row->previous()->height();
2169 refresh_row = row->previous();
2170 status = LyXText::NEED_MORE_REFRESH;
2172 BreakAgainOneRow(bview, row);
2174 current_font = rawtmpfont;
2175 real_current_font = realtmpfont;
2176 SetCursor(bview, cursor.par(), cursor.pos() + 1,
2177 false, cursor.boundary());
2178 // cursor MUST be in row now.
2180 if (row->next() && row->next()->par() == row->par())
2181 need_break_row = row->next();
2185 // check, wether the last characters font has changed.
2187 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2189 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2191 && rawparfont != rawtmpfont)
2192 RedoHeightOfParagraph(bview, cursor);
2199 // recalculate the fill of the row
2200 if (row->fill() >= 0) /* needed because a newline
2201 * will set fill to -1. Otherwise
2202 * we would not get a rebreak! */
2203 row->fill(Fill(bview, row, workWidth(bview)));
2204 if (row->fill() < 0) {
2207 refresh_x = cursor.x();
2208 refresh_pos = cursor.pos();
2209 status = LyXText::NEED_MORE_REFRESH;
2210 BreakAgainOneRow(bview, row);
2211 // will the cursor be in another row now?
2212 if (RowLast(row) <= cursor.pos() + 1 && row->next()) {
2213 if (row->next() && row->next()->par() == row->par())
2214 // this should always be true
2216 BreakAgainOneRow(bview, row);
2218 current_font = rawtmpfont;
2219 real_current_font = realtmpfont;
2221 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2223 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
2224 != cursor.boundary())
2225 SetCursor(bview, cursor.par(), cursor.pos(), false,
2226 !cursor.boundary());
2227 if (row->next() && row->next()->par() == row->par())
2228 need_break_row = row->next();
2233 refresh_x = cursor.x();
2235 refresh_pos = cursor.pos();
2237 int const tmpheight = row->height();
2238 SetHeightOfRow(bview, row);
2239 if (tmpheight == row->height())
2240 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2242 status = LyXText::NEED_MORE_REFRESH;
2244 current_font = rawtmpfont;
2245 real_current_font = realtmpfont;
2246 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2250 // check, wether the last characters font has changed.
2252 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2254 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2256 && rawparfont != rawtmpfont) {
2257 RedoHeightOfParagraph(bview, cursor);
2259 // now the special right address boxes
2260 if (textclasslist.Style(bview->buffer()->params.textclass,
2261 cursor.par()->GetLayout()).margintype
2262 == MARGIN_RIGHT_ADDRESS_BOX) {
2263 RedoDrawingOfParagraph(bview, cursor);
2271 void LyXText::charInserted()
2273 // Here we could call FinishUndo for every 20 characters inserted.
2274 // This is from my experience how emacs does it.
2275 static unsigned int counter = 0;
2285 void LyXText::PrepareToPrint(BufferView * bview,
2286 Row * row, float & x,
2287 float & fill_separator,
2289 float & fill_label_hfill,
2294 float w = row->fill();
2296 fill_label_hfill = 0;
2298 fill_label_hfill = 0;
2301 row->par()->isRightToLeftPar(bview->buffer()->params);
2303 x = (workWidth(bview) > 0)
2304 ? RightMargin(bview->buffer(), row) : 0;
2306 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
2307 LyXFont font(LyXFont::ALL_SANE);
2308 font.setSize(LyXFont::SIZE_SMALL);
2309 x += lyxfont::width("Mwide-figM", font);
2313 x = (workWidth(bview) > 0) ? LeftMargin(bview, row) : 0;
2315 // is there a manual margin with a manual label
2316 if (textclasslist.Style(bview->buffer()->params.textclass,
2317 row->par()->GetLayout()).margintype == MARGIN_MANUAL
2318 && textclasslist.Style(bview->buffer()->params.textclass,
2319 row->par()->GetLayout()).labeltype == LABEL_MANUAL) {
2321 /* one more since labels are left aligned */
2322 nlh = NumberOfLabelHfills(bview->buffer(), row) + 1;
2323 if (nlh && !row->par()->GetLabelWidthString().empty()) {
2324 fill_label_hfill = LabelFill(bview, row) / nlh;
2328 // are there any hfills in the row?
2329 float const nh = NumberOfHfills(bview->buffer(), row);
2332 fill_hfill = w / nh;
2334 // is it block, flushleft or flushright?
2335 // set x how you need it
2338 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
2339 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2341 align = row->par()->FirstPhysicalPar()->params.align();
2343 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
2344 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2346 align = row->par()->params.align();
2349 // center displayed insets
2351 if (row->par()->GetChar(row->pos()) == LyXParagraph::META_INSET
2352 && (inset=row->par()->GetInset(row->pos()))
2353 && (inset->display())) // || (inset->scroll() < 0)))
2354 align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
2355 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2358 case LYX_ALIGN_BLOCK:
2359 ns = NumberOfSeparators(bview->buffer(), row);
2360 if (ns && row->next() && row->next()->par() == row->par() &&
2361 !(row->next()->par()->IsNewline(row->next()->pos() - 1))
2362 && !(row->next()->par()->GetChar(row->next()->pos()) == LyXParagraph::META_INSET
2363 && row->next()->par()->GetInset(row->next()->pos())
2364 && row->next()->par()->GetInset(row->next()->pos())->display())
2366 fill_separator = w / ns;
2370 case LYX_ALIGN_RIGHT:
2373 case LYX_ALIGN_CENTER:
2381 ComputeBidiTables(bview->buffer(), row);
2383 LyXParagraph::size_type main_body =
2384 BeginningOfMainBody(bview->buffer(), row->par());
2385 LyXParagraph::size_type last = RowLast(row);
2387 if (main_body > 0 &&
2388 (main_body-1 > last ||
2389 !row->par()->IsLineSeparator(main_body-1))) {
2390 LyXLayout const & layout =
2391 textclasslist.Style(bview->buffer()->params.textclass,
2392 row->par()->GetLayout());
2393 x += lyxfont::width(layout.labelsep,
2394 GetFont(bview->buffer(), row->par(), -2));
2395 if (main_body-1 <= last)
2396 x += fill_label_hfill;
2401 /* important for the screen */
2404 /* the cursor set functions have a special mechanism. When they
2405 * realize, that you left an empty paragraph, they will delete it.
2406 * They also delete the corresponding row */
2408 void LyXText::CursorRightOneWord(BufferView * bview) const
2410 // treat floats, HFills and Insets as words
2411 LyXCursor tmpcursor = cursor;
2412 // CHECK See comment on top of text.C
2415 if (tmpcursor.pos() == tmpcursor.par()->Last()
2417 if (tmpcursor.pos() == tmpcursor.par()->size()
2419 && tmpcursor.par()->next()) {
2420 tmpcursor.par(tmpcursor.par()->next());
2425 // Skip through initial nonword stuff.
2427 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2429 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2431 ! tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2433 // printf("Current pos1 %d", tmpcursor.pos()) ;
2434 tmpcursor.pos(tmpcursor.pos() + 1);
2437 // Advance through word.
2439 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2441 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2443 tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2445 // printf("Current pos2 %d", tmpcursor.pos()) ;
2446 tmpcursor.pos(tmpcursor.pos() + 1);
2450 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2454 void LyXText::CursorTab(BufferView * bview) const
2456 LyXCursor tmpcursor = cursor;
2458 while (tmpcursor.pos() < tmpcursor.par()->Last()
2460 while (tmpcursor.pos() < tmpcursor.par()->size()
2462 && !tmpcursor.par()->IsNewline(tmpcursor.pos()))
2463 tmpcursor.pos(tmpcursor.pos() + 1);
2466 if (tmpcursor.pos() == tmpcursor.par()->Last()){
2468 if (tmpcursor.pos() == tmpcursor.par()->size()){
2470 if (tmpcursor.par()->next()) {
2471 tmpcursor.par(tmpcursor.par()->next());
2475 tmpcursor.pos(tmpcursor.pos() + 1);
2476 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2480 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2481 of prior word, not to end of next prior word. */
2483 void LyXText::CursorLeftOneWord(BufferView * bview) const
2485 // treat HFills, floats and Insets as words
2486 LyXCursor tmpcursor = cursor;
2487 while (tmpcursor.pos()
2488 && (tmpcursor.par()->IsSeparator(tmpcursor.pos() - 1)
2489 || tmpcursor.par()->IsKomma(tmpcursor.pos() - 1))
2490 && !(tmpcursor.par()->IsHfill(tmpcursor.pos() - 1)
2492 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2494 || tmpcursor.par()->IsInset(tmpcursor.pos() - 1)))
2495 tmpcursor.pos(tmpcursor.pos() - 1);
2498 && (tmpcursor.par()->IsInset(tmpcursor.pos() - 1)
2500 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2502 || tmpcursor.par()->IsHfill(tmpcursor.pos() - 1))) {
2503 tmpcursor.pos(tmpcursor.pos() - 1);
2504 } else if (!tmpcursor.pos()) {
2505 if (tmpcursor.par()->previous()){
2506 tmpcursor.par(tmpcursor.par()->previous());
2508 tmpcursor.pos(tmpcursor.par()->Last());
2510 tmpcursor.pos(tmpcursor.par()->size());
2513 } else { // Here, tmpcursor != 0
2514 while (tmpcursor.pos() > 0 &&
2515 tmpcursor.par()->IsWord(tmpcursor.pos()-1) )
2516 tmpcursor.pos(tmpcursor.pos() - 1);
2518 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2521 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2524 void LyXText::SelectWord(BufferView * bview)
2526 // Move cursor to the beginning, when not already there.
2528 && !cursor.par()->IsSeparator(cursor.pos()-1)
2529 && !cursor.par()->IsKomma(cursor.pos()-1) )
2530 CursorLeftOneWord(bview);
2532 // set the sel cursor
2533 sel_cursor = cursor;
2536 while (cursor.pos() < cursor.par()->Last()
2538 while (cursor.pos() < cursor.par()->size()
2540 && !cursor.par()->IsSeparator(cursor.pos())
2541 && !cursor.par()->IsKomma(cursor.pos()) )
2542 cursor.pos(cursor.pos() + 1);
2543 SetCursor(bview, cursor.par(), cursor.pos() );
2545 // finally set the selection
2546 SetSelection(bview);
2550 /* -------> Select the word currently under the cursor when:
2551 1: no selection is currently set,
2552 2: the cursor is not at the borders of the word. */
2554 bool LyXText::SelectWordWhenUnderCursor(BufferView * bview)
2558 cursor.pos() > 0 && cursor.pos() < cursor.par()->Last()
2560 cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2562 && !cursor.par()->IsSeparator(cursor.pos())
2563 && !cursor.par()->IsKomma(cursor.pos())
2564 && !cursor.par()->IsSeparator(cursor.pos() -1)
2565 && !cursor.par()->IsKomma(cursor.pos() -1)) {
2573 // This function is only used by the spellchecker for NextWord().
2574 // It doesn't handle LYX_ACCENTs and probably never will.
2575 string const LyXText::SelectNextWord(BufferView * bview,
2576 float & value) const
2578 LyXParagraph * tmppar = cursor.par();
2580 // If this is not the very first word, skip rest of
2581 // current word because we are probably in the middle
2582 // of a word if there is text here.
2584 if (cursor.pos() || cursor.par()->previous_) {
2585 while (cursor.pos() < cursor.par()->Last()
2586 && cursor.par()->IsLetter(cursor.pos()))
2587 cursor.pos(cursor.pos() + 1);
2590 if (cursor.pos() || cursor.par()->previous()) {
2591 while (cursor.pos() < cursor.par()->size()
2592 && cursor.par()->IsLetter(cursor.pos()))
2593 cursor.pos(cursor.pos() + 1);
2597 // Now, skip until we have real text (will jump paragraphs)
2599 while ((cursor.par()->Last() > cursor.pos()
2601 while ((cursor.par()->size() > cursor.pos()
2603 && (!cursor.par()->IsLetter(cursor.pos())
2604 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2605 .latex() == LyXFont::ON))
2607 || (cursor.par()->Last() == cursor.pos()
2609 || (cursor.par()->size() == cursor.pos()
2611 && cursor.par()->next())){
2613 if (cursor.pos() == cursor.par()->Last()) {
2615 if (cursor.pos() == cursor.par()->size()) {
2617 cursor.par(cursor.par()->next());
2620 cursor.pos(cursor.pos() + 1);
2623 // Update the value if we changed paragraphs
2624 if (cursor.par() != tmppar){
2625 SetCursor(bview, cursor.par(), cursor.pos());
2626 value = float(cursor.y())/float(height);
2629 // Start the selection from here
2630 sel_cursor = cursor;
2632 std::ostringstream latex;
2634 // and find the end of the word
2635 // (optional hyphens are part of a word)
2637 while (cursor.pos() < cursor.par()->Last()
2639 while (cursor.pos() < cursor.par()->size()
2641 && (cursor.par()->IsLetter(cursor.pos()))
2642 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2643 && cursor.par()->GetInset(cursor.pos()) != 0
2644 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2645 && latex.str() == "\\-"
2647 cursor.pos(cursor.pos() + 1);
2649 // Finally, we copy the word to a string and return it
2651 if (sel_cursor.pos() < cursor.pos()) {
2652 LyXParagraph::size_type i;
2653 for (i = sel_cursor.pos(); i < cursor.pos(); ++i) {
2654 if (cursor.par()->GetChar(i) != LyXParagraph::META_INSET)
2655 str += cursor.par()->GetChar(i);
2662 // This one is also only for the spellchecker
2663 void LyXText::SelectSelectedWord(BufferView * bview)
2665 // move cursor to the beginning
2666 SetCursor(bview, sel_cursor.par(), sel_cursor.pos());
2668 // set the sel cursor
2669 sel_cursor = cursor;
2671 std::ostringstream latex;
2673 // now find the end of the word
2675 while (cursor.pos() < cursor.par()->Last()
2677 while (cursor.pos() < cursor.par()->size()
2679 && (cursor.par()->IsLetter(cursor.pos())
2680 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2681 && cursor.par()->GetInset(cursor.pos()) != 0
2682 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2683 && latex.str() == "\\-"
2685 cursor.pos(cursor.pos() + 1);
2687 SetCursor(bview, cursor.par(), cursor.pos());
2689 // finally set the selection
2690 SetSelection(bview);
2694 /* -------> Delete from cursor up to the end of the current or next word. */
2695 void LyXText::DeleteWordForward(BufferView * bview)
2698 if (!cursor.par()->Last())
2700 if (!cursor.par()->size())
2704 LyXCursor tmpcursor = cursor;
2705 tmpcursor.row(0); //Â ??
2706 selection = true; // to avoid deletion
2707 CursorRightOneWord(bview);
2708 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2709 sel_cursor = cursor;
2711 SetSelection(bview);
2713 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2714 CutSelection(bview);
2719 /* -------> Delete from cursor to start of current or prior word. */
2720 void LyXText::DeleteWordBackward(BufferView * bview)
2723 if (!cursor.par()->Last())
2725 if (!cursor.par()->size())
2729 LyXCursor tmpcursor = cursor;
2730 tmpcursor.row(0); // ??
2731 selection = true; // to avoid deletion
2732 CursorLeftOneWord(bview);
2733 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2734 sel_cursor = cursor;
2736 SetSelection(bview);
2737 CutSelection(bview);
2742 /* -------> Kill to end of line. */
2743 void LyXText::DeleteLineForward(BufferView * bview)
2746 if (!cursor.par()->Last())
2748 if (!cursor.par()->size())
2750 // Paragraph is empty, so we just go to the right
2753 LyXCursor tmpcursor = cursor;
2754 // We can't store the row over a regular SetCursor
2755 // so we set it to 0 and reset it afterwards.
2756 tmpcursor.row(0); //Â ??
2757 selection = true; // to avoid deletion
2759 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2760 sel_cursor = cursor;
2762 SetSelection(bview);
2763 // What is this test for ??? (JMarc)
2765 DeleteWordForward(bview);
2767 CutSelection(bview);
2773 // Change the case of a word at cursor position.
2774 // This function directly manipulates LyXParagraph::text because there
2775 // is no LyXParagraph::SetChar currently. I did what I could to ensure
2776 // that it is correct. I guess part of it should be moved to
2777 // LyXParagraph, but it will have to change for 1.1 anyway. At least
2778 // it does not access outside of the allocated array as the older
2779 // version did. (JMarc)
2780 void LyXText::ChangeWordCase(BufferView * bview, LyXText::TextCase action)
2783 LyXParagraph * tmppar = cursor.par()->ParFromPos(cursor.pos());
2785 SetUndo(bview->buffer(),Undo::FINISH,
2786 tmppar->previous_, tmppar->next_);
2788 LyXParagraph::size_type tmppos =
2789 cursor.par()->PositionInParFromPos(cursor.pos());
2791 LyXParagraph * tmppar = cursor.par();
2793 SetUndo(bview->buffer(),Undo::FINISH,
2794 tmppar->previous(), tmppar->next());
2796 LyXParagraph::size_type tmppos = cursor.pos();
2799 while (tmppos < tmppar->size()) {
2800 unsigned char c = tmppar->GetChar(tmppos);
2801 if (IsKommaChar(c) || IsLineSeparatorChar(c))
2803 if (c != LyXParagraph::META_INSET) {
2805 case text_lowercase:
2808 case text_capitalization:
2810 action = text_lowercase;
2812 case text_uppercase:
2818 //tmppar->text[tmppos] = c;
2819 tmppar->SetChar(tmppos, c);
2822 CheckParagraph(bview, tmppar, tmppos);
2823 CursorRightOneWord(bview);
2827 void LyXText::Delete(BufferView * bview)
2829 // this is a very easy implementation
2831 LyXCursor old_cursor = cursor;
2832 int const old_cur_par_id = old_cursor.par()->id();
2834 int const old_cur_par_prev_id = old_cursor.par()->previous_ ?
2835 old_cursor.par()->previous_->id() : 0;
2837 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2838 old_cursor.par()->previous()->id() : 0;
2841 // just move to the right
2844 // CHECK Look at the comment here.
2845 // This check is not very good...
2846 // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
2847 // and that can very well delete the par or par->previous in
2848 // old_cursor. Will a solution where we compare paragraph id's
2852 (cursor.par()->previous_ ? cursor.par()->previous_->id() : 0)
2854 (cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2856 == old_cur_par_prev_id
2857 && cursor.par()->id() != old_cur_par_id)
2858 return; // delete-empty-paragraph-mechanism has done it
2860 // if you had success make a backspace
2861 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2862 LyXCursor tmpcursor = cursor;
2863 cursor = old_cursor; // to make sure undo gets the right cursor position
2864 SetUndo(bview->buffer(), Undo::DELETE,
2866 cursor.par()->ParFromPos(cursor.pos())->previous_,
2867 cursor.par()->ParFromPos(cursor.pos())->next_
2869 cursor.par()->previous(),
2870 cursor.par()->next()
2879 void LyXText::Backspace(BufferView * bview)
2881 // Get the font that is used to calculate the baselineskip
2883 LyXParagraph::size_type lastpos = cursor.par()->Last();
2885 LyXParagraph::size_type lastpos = cursor.par()->size();
2887 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2890 if (cursor.pos() == 0) {
2891 // The cursor is at the beginning of a paragraph,
2892 // so the the backspace will collapse two paragraphs into one.
2894 // we may paste some paragraphs
2896 // is it an empty paragraph?
2899 || (lastpos == 1 && cursor.par()->IsSeparator(0)))
2901 && !(cursor.par()->next()
2902 && cursor.par()->footnoteflag == LyXParagraph::NO_FOOTNOTE
2903 && cursor.par()->next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
2906 // This is an empty paragraph and we delete it just by moving the cursor one step
2907 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2908 // of the paragraph.
2911 if (cursor.par()->previous_) {
2912 LyXParagraph * tmppar = cursor.par()->previous_->FirstPhysicalPar();
2913 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2914 && cursor.par()->footnoteflag == tmppar->footnoteflag
2916 if (cursor.par()->previous()) {
2917 LyXParagraph * tmppar = cursor.par()->previous();
2918 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2920 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2921 // Inherit bottom DTD from the paragraph below.
2922 // (the one we are deleting)
2923 tmppar->params.lineBottom(cursor.par()->params.lineBottom());
2924 tmppar->params.spaceBottom(cursor.par()->params.spaceBottom());
2925 tmppar->params.pagebreakBottom(cursor.par()->params.pagebreakBottom());
2930 // the layout things can change the height of a row !
2931 int const tmpheight = cursor.row()->height();
2932 SetHeightOfRow(bview, cursor.row());
2933 if (cursor.row()->height() != tmpheight) {
2934 refresh_y = cursor.y() - cursor.row()->baseline();
2935 refresh_row = cursor.row();
2936 status = LyXText::NEED_MORE_REFRESH;
2943 if (cursor.par()->ParFromPos(cursor.pos())->previous_) {
2944 SetUndo(bview->buffer(), Undo::DELETE,
2945 cursor.par()->ParFromPos(cursor.pos())->previous_->previous_,
2946 cursor.par()->ParFromPos(cursor.pos())->next_);
2949 if (cursor.par()->previous()) {
2950 SetUndo(bview->buffer(), Undo::DELETE,
2951 cursor.par()->previous()->previous(),
2952 cursor.par()->next());
2956 LyXParagraph * tmppar = cursor.par();
2957 Row * tmprow = cursor.row();
2959 // We used to do CursorLeftIntern() here, but it is
2960 // not a good idea since it triggers the auto-delete
2961 // mechanism. So we do a CursorLeftIntern()-lite,
2962 // without the dreaded mechanism. (JMarc)
2963 if (cursor.par()->previous()) {
2964 // steps into the above paragraph.
2966 SetCursorIntern(bview, cursor.par()->previous(),
2967 cursor.par()->previous()->Last(), false);
2969 SetCursorIntern(bview, cursor.par()->previous(),
2970 cursor.par()->previous()->size(),
2975 /* Pasting is not allowed, if the paragraphs have different
2976 layout. I think it is a real bug of all other
2977 word processors to allow it. It confuses the user.
2978 Even so with a footnote paragraph and a non-footnote
2979 paragraph. I will not allow pasting in this case,
2980 because the user would be confused if the footnote behaves
2981 different wether it is open or closed.
2983 Correction: Pasting is always allowed with standard-layout
2985 if (cursor.par() != tmppar
2986 && (cursor.par()->GetLayout() == tmppar->GetLayout()
2987 || tmppar->GetLayout() == 0 /*standard*/)
2989 && cursor.par()->footnoteflag == tmppar->footnoteflag
2991 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2993 RemoveParagraph(tmprow);
2995 cursor.par()->PasteParagraph(bview->buffer()->params);
2997 if (!cursor.pos() || !cursor.par()->IsSeparator(cursor.pos() - 1))
2998 ; //cursor.par()->InsertChar(cursor.pos(), ' ');
2999 // strangely enough it seems that commenting out the line above removes
3000 // most or all of the segfaults. I will however also try to move the
3001 // two Remove... lines in front of the PasteParagraph too.
3004 cursor.pos(cursor.pos() - 1);
3006 status = LyXText::NEED_MORE_REFRESH;
3007 refresh_row = cursor.row();
3008 refresh_y = cursor.y() - cursor.row()->baseline();
3010 // remove the lost paragraph
3011 // This one is not safe, since the paragraph that the tmprow and the
3012 // following rows belong to has been deleted by the PasteParagraph
3013 // above. The question is... could this be moved in front of the
3015 //RemoveParagraph(tmprow);
3016 //RemoveRow(tmprow);
3018 // This rebuilds the rows.
3019 AppendParagraph(bview, cursor.row());
3020 UpdateCounters(bview, cursor.row());
3022 // the row may have changed, block, hfills etc.
3023 SetCursor(bview, cursor.par(), cursor.pos(), false);
3026 /* this is the code for a normal backspace, not pasting
3028 SetUndo(bview->buffer(), Undo::DELETE,
3030 cursor.par()->ParFromPos(cursor.pos())->previous_,
3031 cursor.par()->ParFromPos(cursor.pos())->next_
3033 cursor.par()->previous(),
3034 cursor.par()->next()
3037 // We used to do CursorLeftIntern() here, but it is
3038 // not a good idea since it triggers the auto-delete
3039 // mechanism. So we do a CursorLeftIntern()-lite,
3040 // without the dreaded mechanism. (JMarc)
3041 SetCursorIntern(bview, cursor.par(), cursor.pos()- 1,
3042 false, cursor.boundary());
3044 // some insets are undeletable here
3045 if (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
3046 if (!cursor.par()->GetInset(cursor.pos())->Deletable())
3048 // force complete redo when erasing display insets
3049 // this is a cruel method but safe..... Matthias
3050 if (cursor.par()->GetInset(cursor.pos())->display() ||
3051 cursor.par()->GetInset(cursor.pos())->needFullRow()) {
3052 cursor.par()->Erase(cursor.pos());
3053 RedoParagraph(bview);
3058 Row * row = cursor.row();
3059 int y = cursor.y() - row->baseline();
3060 LyXParagraph::size_type z;
3061 /* remember that a space at the end of a row doesnt count
3062 * when calculating the fill */
3063 if (cursor.pos() < RowLast(row) ||
3064 !cursor.par()->IsLineSeparator(cursor.pos())) {
3065 row->fill(row->fill() + SingleWidth(bview,
3070 /* some special code when deleting a newline. This is similar
3071 * to the behavior when pasting paragraphs */
3072 if (cursor.pos() && cursor.par()->IsNewline(cursor.pos())) {
3073 cursor.par()->Erase(cursor.pos());
3074 // refresh the positions
3076 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3077 tmprow = tmprow->next();
3078 tmprow->pos(tmprow->pos() - 1);
3080 if (cursor.par()->IsLineSeparator(cursor.pos() - 1))
3081 cursor.pos(cursor.pos() - 1);
3084 if (cursor.pos() < cursor.par()->Last()
3085 && !cursor.par()->IsSeparator(cursor.pos())) {
3087 if (cursor.pos() < cursor.par()->size()
3088 && !cursor.par()->IsSeparator(cursor.pos())) {
3090 cursor.par()->InsertChar(cursor.pos(), ' ');
3091 SetCharFont(bview->buffer(), cursor.par(),
3092 cursor.pos(), current_font);
3093 // refresh the positions
3095 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3096 tmprow = tmprow->next();
3097 tmprow->pos(tmprow->pos() + 1);
3101 cursor.par()->Erase(cursor.pos());
3103 // refresh the positions
3105 while (tmprow->next()
3106 && tmprow->next()->par() == row->par()) {
3107 tmprow = tmprow->next();
3108 tmprow->pos(tmprow->pos() - 1);
3111 // delete newlines at the beginning of paragraphs
3113 while (cursor.par()->Last() &&
3115 while (cursor.par()->size() &&
3117 cursor.par()->IsNewline(cursor.pos()) &&
3118 cursor.pos() == BeginningOfMainBody(bview->buffer(),
3120 cursor.par()->Erase(cursor.pos());
3121 // refresh the positions
3123 while (tmprow->next() &&
3124 tmprow->next()->par() == row->par()) {
3125 tmprow = tmprow->next();
3126 tmprow->pos(tmprow->pos() - 1);
3131 // is there a break one row above
3132 if (row->previous() && row->previous()->par() == row->par()) {
3133 z = NextBreakPoint(bview, row->previous(),
3135 if (z >= row->pos()) {
3138 Row * tmprow = row->previous();
3140 // maybe the current row is now empty
3142 if (row->pos() >= row->par()->Last()) {
3144 if (row->pos() >= row->par()->size()) {
3150 BreakAgainOneRow(bview, row);
3151 if (row->next() && row->next()->par() == row->par())
3152 need_break_row = row->next();
3157 // set the dimensions of the row above
3158 y -= tmprow->height();
3159 tmprow->fill(Fill(bview, tmprow,
3161 SetHeightOfRow(bview, tmprow);
3164 refresh_row = tmprow;
3165 status = LyXText::NEED_MORE_REFRESH;
3166 SetCursor(bview, cursor.par(), cursor.pos(),
3167 false, cursor.boundary());
3168 //current_font = rawtmpfont;
3169 //real_current_font = realtmpfont;
3170 // check, whether the last character's font has changed.
3173 cursor.par()->GetFontSettings(bview->buffer()->params,
3174 cursor.par()->Last() - 1))
3177 cursor.par()->GetFontSettings(bview->buffer()->params,
3178 cursor.par()->size() - 1))
3180 RedoHeightOfParagraph(bview, cursor);
3185 // break the cursor row again
3186 if (row->next() && row->next()->par() == row->par() &&
3188 (RowLast(row) == row->par()->Last() - 1 ||
3190 (RowLast(row) == row->par()->size() - 1 ||
3192 NextBreakPoint(bview, row, workWidth(bview)) != RowLast(row))) {
3194 /* it can happen that a paragraph loses one row
3195 * without a real breakup. This is when a word
3196 * is to long to be broken. Well, I don t care this
3199 if (RowLast(row) == row->par()->Last() - 1)
3201 if (RowLast(row) == row->par()->size() - 1)
3203 RemoveRow(row->next());
3207 status = LyXText::NEED_MORE_REFRESH;
3209 BreakAgainOneRow(bview, row);
3210 // will the cursor be in another row now?
3211 if (row->next() && row->next()->par() == row->par() &&
3212 RowLast(row) <= cursor.pos()) {
3214 BreakAgainOneRow(bview, row);
3217 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3219 if (row->next() && row->next()->par() == row->par())
3220 need_break_row = row->next();
3224 // set the dimensions of the row
3225 row->fill(Fill(bview, row, workWidth(bview)));
3226 int const tmpheight = row->height();
3227 SetHeightOfRow(bview, row);
3228 if (tmpheight == row->height())
3229 status = LyXText::NEED_VERY_LITTLE_REFRESH;
3231 status = LyXText::NEED_MORE_REFRESH;
3234 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3238 // current_font = rawtmpfont;
3239 // real_current_font = realtmpfont;
3241 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
3242 != cursor.boundary())
3243 SetCursor(bview, cursor.par(), cursor.pos(), false,
3244 !cursor.boundary());
3247 lastpos = cursor.par()->Last();
3249 lastpos = cursor.par()->size();
3251 if (cursor.pos() == lastpos)
3252 SetCurrentFont(bview);
3254 // check, whether the last characters font has changed.
3256 cursor.par()->GetFontSettings(bview->buffer()->params, lastpos - 1)) {
3257 RedoHeightOfParagraph(bview, cursor);
3259 // now the special right address boxes
3260 if (textclasslist.Style(bview->buffer()->params.textclass,
3261 cursor.par()->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3262 RedoDrawingOfParagraph(bview, cursor);
3268 void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
3269 Row * row_ptr, int y, bool cleared)
3271 /* returns a printed row */
3272 Painter & pain = bview->painter();
3275 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
3277 LyXParagraph::size_type const last = RowLastPrintable(row_ptr);
3279 LyXParagraph::size_type vpos, pos;
3285 LyXFont font(LyXFont::ALL_SANE);
3287 if (row_ptr->height() <= 0) {
3288 lyxerr << "LYX_ERROR: row.height: "
3289 << row_ptr->height() << endl;
3293 float fill_separator;
3295 float fill_label_hfill;
3296 PrepareToPrint(bview, row_ptr, x, fill_separator,
3297 fill_hfill, fill_label_hfill);
3299 if (inset_owner && (x < 0))
3303 // clear the area where we want to paint/print
3304 int ww = bview->workWidth();
3306 bool clear_area = true;
3309 if (!bview->screen()->forceClear() && last == row_ptr->pos()
3310 && row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET
3311 && (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
3312 clear_area = inset->doClearArea();
3314 // we don't need to clear it's already done!!!
3317 } else if (clear_area) {
3318 #ifdef WITH_WARNINGS
3319 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
3320 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
3322 int const y = y_offset < 0 ? 0 : y_offset;
3323 int const h = y_offset < 0 ?
3324 row_ptr->height() + y_offset : row_ptr->height();
3325 int const w = inset_owner ?
3326 inset_owner->width(bview, font) - 2 : ww;
3327 int const x = x_offset;
3328 pain.fillRectangle(x, y, w, h);
3329 } else if (inset != 0) {
3330 int h = row_ptr->baseline() - inset->ascent(bview, font);
3332 int const w = (inset_owner ?
3333 inset_owner->width(bview, font) : ww);
3334 pain.fillRectangle(x_offset, y_offset, w, h);
3336 h += inset->ascent(bview, font) + inset->descent(bview, font);
3337 if ((row_ptr->height() - h) > 0) {
3338 int const w = (inset_owner ?
3339 inset_owner->width(bview, font) : ww);
3340 pain.fillRectangle(x_offset, y_offset + h,
3341 w, row_ptr->height() - h);
3343 if (!inset_owner && !inset->display() && !inset->needFullRow())
3345 int const w = inset->width(bview, font) + int(x);
3346 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
3351 int const w = (inset_owner ?
3352 inset_owner->width(bview, font) : ww);
3353 /* selection code */
3354 if (bidi_same_direction) {
3355 if (sel_start_cursor.row() == row_ptr &&
3356 sel_end_cursor.row() == row_ptr) {
3357 if (sel_start_cursor.x() < sel_end_cursor.x())
3358 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3360 sel_end_cursor.x() - sel_start_cursor.x(),
3364 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3366 sel_start_cursor.x() - sel_end_cursor.x(),
3369 } else if (sel_start_cursor.row() == row_ptr) {
3371 pain.fillRectangle(x_offset, y_offset,
3372 sel_start_cursor.x(),
3376 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3378 w - sel_start_cursor.x(),
3381 } else if (sel_end_cursor.row() == row_ptr) {
3383 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3385 w - sel_end_cursor.x(),
3389 pain.fillRectangle(x_offset, y_offset,
3393 } else if (y > sel_start_cursor.y()
3394 && y < sel_end_cursor.y()) {
3395 pain.fillRectangle(x_offset, y_offset, w,
3399 } else if (sel_start_cursor.row() != row_ptr &&
3400 sel_end_cursor.row() != row_ptr &&
3401 y > sel_start_cursor.y()
3402 && y < sel_end_cursor.y()) {
3403 pain.fillRectangle(x_offset, y_offset, w,
3406 } else if (sel_start_cursor.row() == row_ptr ||
3407 sel_end_cursor.row() == row_ptr) {
3409 if ((sel_start_cursor.row() != row_ptr && !is_rtl) ||
3410 (sel_end_cursor.row() != row_ptr && is_rtl))
3411 pain.fillRectangle(x_offset, y_offset,
3415 LyXParagraph::size_type main_body =
3416 BeginningOfMainBody(bview->buffer(),
3419 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3420 pos = vis2log(vpos);
3421 float const old_tmpx = tmpx;
3422 if (main_body > 0 && pos == main_body-1) {
3423 tmpx += fill_label_hfill +
3424 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3425 row_ptr->par()->GetLayout()).labelsep,
3426 GetFont(bview->buffer(),row_ptr->par(), -2));
3427 if (row_ptr->par()->IsLineSeparator(main_body-1))
3428 tmpx -= SingleWidth(bview, row_ptr->par(), main_body-1);
3430 if (HfillExpansion(bview->buffer(), row_ptr, pos)) {
3431 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3432 if (pos >= main_body)
3435 tmpx += fill_label_hfill;
3437 else if (row_ptr->par()->IsSeparator(pos)) {
3438 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3439 if (pos >= main_body)
3440 tmpx += fill_separator;
3442 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3444 if ((sel_start_cursor.row() != row_ptr ||
3445 sel_start_cursor.pos() <= pos) &&
3446 (sel_end_cursor.row() != row_ptr ||
3447 pos < sel_end_cursor.pos()) )
3448 /// Here we do not use x_offset as x_offset was
3450 pain.fillRectangle(int(old_tmpx),
3452 int(tmpx - old_tmpx + 1),
3457 if ((sel_start_cursor.row() != row_ptr && is_rtl) ||
3458 (sel_end_cursor.row() != row_ptr && !is_rtl) )
3459 pain.fillRectangle(x_offset + int(tmpx),
3469 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3470 LyXFont font(LyXFont::ALL_SANE);
3471 font.setSize(LyXFont::SIZE_FOOTNOTE);
3472 font.setColor(LColor::footnote);
3474 box_x = LYX_PAPER_MARGIN + lyxfont::width(" wide-tab ", font);
3475 if (row_ptr->previous() &&
3476 row_ptr->previous()->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3478 switch (row_ptr->par()->footnotekind) {
3479 case LyXParagraph::MARGIN:
3482 case LyXParagraph::FIG:
3485 case LyXParagraph::TAB:
3488 case LyXParagraph::WIDE_FIG:
3491 case LyXParagraph::WIDE_TAB:
3494 case LyXParagraph::ALGORITHM:
3497 case LyXParagraph::FOOTNOTE:
3502 pain.fillRectangle(LYX_PAPER_MARGIN,
3504 box_x - LYX_PAPER_MARGIN,
3505 int(lyxfont::maxAscent(font)
3506 + lyxfont::maxDescent(font)),
3507 LColor::footnotebg);
3509 pain.line(LYX_PAPER_MARGIN, y_offset,
3510 workWidth(bview) - LYX_PAPER_MARGIN, y_offset,
3511 LColor::footnoteframe);
3513 pain.text(LYX_PAPER_MARGIN,
3514 y_offset + int(lyxfont::maxAscent(font)) + 1,
3517 pain.line(LYX_PAPER_MARGIN, y_offset,
3519 y_offset + int(lyxfont::maxAscent(font)
3520 + lyxfont::maxDescent(font)),
3521 LColor::footnoteframe);
3523 pain.line(LYX_PAPER_MARGIN,
3524 y_offset + int(lyxfont::maxAscent(font)
3525 + lyxfont::maxDescent(font)) + 1,
3527 y_offset + int(lyxfont::maxAscent(font)
3528 + lyxfont::maxDescent(font)) + 1,
3529 LColor::footnoteframe);
3533 /* draw the open floats in a red box */
3534 pain.line(box_x, y_offset,
3535 box_x, y_offset + row_ptr->height(),
3536 LColor::footnoteframe);
3538 pain.line(workWidth(bview) - LYX_PAPER_MARGIN,
3540 workWidth(bview) - LYX_PAPER_MARGIN,
3541 y_offset + row_ptr->height(),
3542 LColor::footnoteframe);
3545 // Draw appendix lines
3546 LyXParagraph * p = row_ptr->par()->PreviousBeforeFootnote()->FirstPhysicalPar();
3547 if (p->params.appendix()) {
3548 pain.line(1, y_offset,
3549 1, y_offset + row_ptr->height(),
3550 LColor::appendixline);
3551 pain.line(workWidth(bview) - 2, y_offset,
3552 workWidth(bview) - 2,
3553 y_offset + row_ptr->height(),
3554 LColor::appendixline);
3557 // Draw minipage line
3558 bool const minipage =
3559 (p->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3561 pain.line(LYX_PAPER_MARGIN/5, y_offset,
3563 y_offset + row_ptr->height() - 1,
3564 LColor::minipageline);
3567 int const depth = p->GetDepth();
3568 for (int i = 1; i <= depth; ++i) {
3569 int const line_x = (LYX_PAPER_MARGIN / 5) *
3571 pain.line(line_x, y_offset, line_x,
3572 y_offset + row_ptr->height() - 1,
3575 } else if (row_ptr->previous() &&
3576 row_ptr->previous()->par()->footnoteflag
3577 == LyXParagraph::OPEN_FOOTNOTE) {
3578 LyXFont font(LyXFont::ALL_SANE);
3579 font.setSize(LyXFont::SIZE_FOOTNOTE);
3581 int const box_x = LYX_PAPER_MARGIN
3582 + lyxfont::width(" wide-tab ", font);
3584 pain.line(box_x, y_offset,
3585 workWidth(bview) - LYX_PAPER_MARGIN,
3586 y_offset, LColor::footnote);
3589 // Draw appendix lines
3591 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
3593 LyXParagraph * firstpar = row_ptr->par();
3595 if (firstpar->params.appendix()) {
3596 pain.line(1, y_offset,
3597 1, y_offset + row_ptr->height(),
3598 LColor::appendixline);
3599 pain.line(ww - 2, y_offset,
3600 ww - 2, y_offset + row_ptr->height(),
3601 LColor::appendixline);
3604 // Draw minipage line
3605 bool const minipage =
3606 (firstpar->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3608 pain.line(LYX_PAPER_MARGIN/5 + box_x, y_offset,
3609 LYX_PAPER_MARGIN/5 + box_x,
3610 y_offset + row_ptr->height() - 1,
3611 LColor::minipageline);
3614 int const depth = firstpar->GetDepth();
3618 if (row_ptr->next())
3620 if (row_ptr->par()->footnoteflag ==
3621 row_ptr->next()->par()->footnoteflag)
3622 next_depth = row_ptr->next()->par()->GetDepth();
3623 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3626 next_depth = row_ptr->next()->par()->GetDepth();
3628 if (row_ptr->previous())
3630 if (row_ptr->par()->footnoteflag ==
3631 row_ptr->previous()->par()->footnoteflag)
3632 prev_depth = row_ptr->previous()->par()->GetDepth();
3633 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3636 prev_depth = row_ptr->previous()->par()->GetDepth();
3639 for (int i = 1; i <= depth; ++i) {
3640 int const line_x = (LYX_PAPER_MARGIN / 5) *
3641 (i + minipage) + box_x + x_offset;
3642 pain.line(line_x, y_offset, line_x,
3643 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3647 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3650 pain.fillRectangle(line_x,
3651 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3652 LYX_PAPER_MARGIN / 5, 2,
3658 LyXLayout const & layout =
3659 textclasslist.Style(bview->buffer()->params.textclass,
3660 row_ptr->par()->GetLayout());
3663 y_bottom = row_ptr->height();
3665 /* is it a first row? */
3666 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3668 /* start of appendix? */
3669 if (row_ptr->par()->params.startOfAppendix()) {
3670 pain.line(1, y_offset,
3672 LColor::appendixline);
3675 /* think about the margins */
3676 if (!row_ptr->previous() && bv_owner)
3677 y_top += LYX_PAPER_MARGIN;
3679 /* draw a top pagebreak */
3680 if (row_ptr->par()->params.pagebreakTop()) {
3682 pb_font.setColor(LColor::pagebreak).decSize();
3683 int w = 0, a = 0, d = 0;
3684 pain.line(0, y_offset + y_top + 2*DefaultHeight(),
3686 y_offset + y_top + 2 * DefaultHeight(),
3688 Painter::line_onoffdash)
3691 _("Page Break (top)"),
3694 LColor::background, false, w, a, d);
3695 pain.rectText((ww - w)/2,
3696 y_offset + y_top + 2 * DefaultHeight() + d,
3697 _("Page Break (top)"),
3700 LColor::background);
3701 y_top += 3 * DefaultHeight();
3704 if (row_ptr->par()->params.spaceTop().kind() == VSpace::VFILL) {
3705 /* draw a vfill top */
3706 pain.line(0, y_offset + 2 + y_top,
3707 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3710 pain.line(0, y_offset + y_top + 3 * DefaultHeight(),
3712 y_offset + y_top + 3 * DefaultHeight(),
3715 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3716 LYX_PAPER_MARGIN / 2,
3717 y_offset + y_top + 3 * DefaultHeight(),
3720 y_top += 3 * DefaultHeight();
3723 /* think about user added space */
3724 y_top += int(row_ptr->par()->params.spaceTop().inPixels(bview));
3726 /* think about the parskip */
3727 /* some parskips VERY EASY IMPLEMENTATION */
3728 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3729 if (layout.latextype == LATEX_PARAGRAPH
3730 && firstpar->GetDepth() == 0
3731 && firstpar->previous())
3732 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3733 else if (firstpar->previous()
3734 && textclasslist.Style(bview->buffer()->params.textclass,
3735 firstpar->previous()->GetLayout()).latextype == LATEX_PARAGRAPH
3736 && firstpar->previous()->GetDepth() == 0)
3737 // is it right to use defskip here, too? (AS)
3738 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3741 if (row_ptr->par()->params.lineTop()) { /* draw a top line */
3742 y_top += lyxfont::ascent('x',
3743 GetFont(bview->buffer(),
3744 row_ptr->par(), 0));
3745 int const w = (inset_owner ?
3746 inset_owner->width(bview, font) : ww);
3747 int const xp = static_cast<int>(inset_owner ? x : 0);
3748 pain.line(xp, y_offset + y_top,
3749 w, y_offset + y_top,
3751 Painter::line_solid,
3752 Painter::line_thick);
3754 y_top += lyxfont::ascent('x',GetFont(bview->buffer(),
3755 row_ptr->par(), 0));
3758 /* should we print a label? */
3759 if (layout.labeltype >= LABEL_STATIC
3760 && (layout.labeltype != LABEL_STATIC
3761 || layout.latextype != LATEX_ENVIRONMENT
3762 || row_ptr->par()->IsFirstInSequence())) {
3763 font = GetFont(bview->buffer(), row_ptr->par(), -2);
3764 if (!row_ptr->par()->GetLabelstring().empty()) {
3766 string const tmpstring =
3767 row_ptr->par()->GetLabelstring();
3769 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3770 if (bview->buffer()->params.secnumdepth >= 0) {
3771 /* this is special code for the chapter layout. This is printed in
3772 * an extra row and has a pagebreak at the top. */
3773 float spacing_val = 1.0;
3774 if (!row_ptr->par()->params.spacing().isDefault()) {
3775 spacing_val = row_ptr->par()->params.spacing().getValue();
3777 spacing_val = bview->buffer()->params.spacing.getValue();
3780 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3781 + int(layout.parsep) * DefaultHeight();
3783 tmpx = ww - LeftMargin(bview, row_ptr) -
3784 lyxfont::width(tmpstring, font);
3785 pain.text(int(tmpx),
3786 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3791 tmpx = ww - LeftMargin(bview, row_ptr)
3792 + lyxfont::width(layout.labelsep, font);
3794 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3795 LyXFont font(LyXFont::ALL_SANE);
3796 font.setSize(LyXFont::SIZE_SMALL);
3797 tmpx += lyxfont::width("Mwide-fixM", font);
3801 tmpx = x - lyxfont::width(layout.labelsep, font)
3802 - lyxfont::width(tmpstring, font);
3805 pain.text(int(tmpx),
3806 y_offset + row_ptr->baseline(),
3810 /* the labels at the top of an environment. More or less for bibliography */
3811 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3812 layout.labeltype == LABEL_BIBLIO ||
3813 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3814 if (row_ptr->par()->IsFirstInSequence()) {
3815 font = GetFont(bview->buffer(),
3816 row_ptr->par(), -2);
3817 if (!row_ptr->par()->GetLabelstring().empty()) {
3818 string const tmpstring =
3819 row_ptr->par()->GetLabelstring();
3820 float spacing_val = 1.0;
3821 if (!row_ptr->par()->params.spacing().isDefault()) {
3822 spacing_val = row_ptr->par()->params.spacing().getValue();
3824 spacing_val = bview->buffer()->params.spacing.getValue();
3827 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3828 + (layout.labelbottomsep * DefaultHeight()));
3831 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3832 tmpx = ( (is_rtl ? LeftMargin(bview, row_ptr) : x)
3833 + ww - RightMargin(bview->buffer(), row_ptr) ) / 2;
3834 tmpx -= lyxfont::width(tmpstring, font) / 2;
3836 tmpx = ww - LeftMargin(bview, row_ptr) -
3837 lyxfont::width(tmpstring, font);
3838 pain.text(int(tmpx),
3839 y_offset + row_ptr->baseline()
3840 - row_ptr->ascent_of_text()
3846 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3847 font = GetFont(bview->buffer(), row_ptr->par(), -1);
3849 tmpx = ww - LeftMargin(bview, row_ptr)
3850 + lyxfont::width(layout.labelsep, font);
3852 tmpx = x - lyxfont::width(layout.labelsep, font)
3853 - row_ptr->par()->bibkey->width(bview, font);
3854 row_ptr->par()->bibkey->draw(bview, font,
3855 y_offset + row_ptr->baseline(),
3860 /* is it a last row? */
3862 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
3864 LyXParagraph * par = row_ptr->par();
3868 row_ptr->par()->ParFromPos(last + 1) == par
3870 row_ptr->par() == par
3872 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
3874 /* think about the margins */
3875 if (!row_ptr->next() && bv_owner)
3876 y_bottom -= LYX_PAPER_MARGIN;
3878 /* draw a bottom pagebreak */
3879 if (firstpar->params.pagebreakBottom()) {
3881 pb_font.setColor(LColor::pagebreak).decSize();
3882 int w = 0, a = 0, d = 0;
3884 y_offset + y_bottom - 2 * DefaultHeight(),
3886 y_offset + y_bottom - 2 * DefaultHeight(),
3888 Painter::line_onoffdash)
3891 _("Page Break (bottom)"),
3894 LColor::background, false, w, a, d);
3895 pain.rectText((ww - w) / 2,
3896 y_offset + y_top + 2 * DefaultHeight() + d,
3897 _("Page Break (bottom)"),
3900 LColor::background);
3901 y_bottom -= 3 * DefaultHeight();
3904 if (firstpar->params.spaceBottom().kind() == VSpace::VFILL) {
3905 /* draw a vfill bottom */
3906 pain.line(0, y_offset + y_bottom - 3 * DefaultHeight(),
3908 y_offset + y_bottom - 3 * DefaultHeight(),
3910 pain.line(0, y_offset + y_bottom - 2,
3912 y_offset + y_bottom - 2,
3914 pain.line(LYX_PAPER_MARGIN / 2,
3915 y_offset + y_bottom - 3 * DefaultHeight(),
3916 LYX_PAPER_MARGIN / 2,
3917 y_offset + y_bottom - 2,
3919 y_bottom -= 3 * DefaultHeight();
3922 /* think about user added space */
3923 y_bottom -= int(firstpar->params.spaceBottom().inPixels(bview));
3925 if (firstpar->params.lineBottom()) {
3926 /* draw a bottom line */
3928 y_bottom -= lyxfont::ascent('x',
3929 GetFont(bview->buffer(),
3933 y_bottom -= lyxfont::ascent('x',
3934 GetFont(bview->buffer(),
3938 int const w = (inset_owner ?
3939 inset_owner->width(bview, font) : ww);
3940 int const xp = static_cast<int>(inset_owner ? x : 0);
3941 pain.line(xp, y_offset + y_bottom,
3942 w, y_offset + y_bottom,
3943 LColor::topline, Painter::line_solid,
3944 Painter::line_thick);
3946 y_bottom -= lyxfont::ascent('x',
3947 GetFont(bview->buffer(),
3951 y_bottom -= lyxfont::ascent('x',
3952 GetFont(bview->buffer(),
3959 int const endlabel =
3960 row_ptr->par()->GetEndLabel(bview->buffer()->params);
3963 case END_LABEL_FILLED_BOX:
3965 LyXFont const font = GetFont(bview->buffer(),
3966 row_ptr->par(), last);
3967 int const size = int(0.75 * lyxfont::maxAscent(font));
3968 int const y = (y_offset + row_ptr->baseline()) - size;
3969 int x = is_rtl ? LYX_PAPER_MARGIN
3970 : ww - LYX_PAPER_MARGIN - size;
3972 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
3974 LyXFont font(LyXFont::ALL_SANE);
3975 font.setSize(LyXFont::SIZE_SMALL);
3976 x += lyxfont::width("Mwide-figM", font);
3978 x -= LYX_PAPER_MARGIN/2;
3980 if (row_ptr->fill() <= size)
3981 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3982 if (endlabel == END_LABEL_BOX) {
3983 pain.line(x, y, x, y + size,
3985 pain.line(x + size, y, x + size , y + size,
3987 pain.line(x, y, x + size, y,
3989 pain.line(x, y + size, x + size, y + size,
3992 pain.fillRectangle(x, y, size, size,
3996 case END_LABEL_STATIC:
3998 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->GetLayout();
3999 string const tmpstring = textclasslist.
4000 Style(bview->buffer()->params.textclass,
4001 layout).endlabelstring();
4002 font = GetFont(bview->buffer(), row_ptr->par(), -2);
4003 int const tmpx = is_rtl ?
4004 int(x) - lyxfont::width(tmpstring, font)
4005 : ww - RightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
4006 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
4009 case END_LABEL_NO_LABEL:
4014 /* draw the text in the pixmap */
4016 vpos = row_ptr->pos();
4018 LyXParagraph::size_type main_body =
4019 BeginningOfMainBody(bview->buffer(), row_ptr->par());
4020 if (main_body > 0 &&
4021 (main_body-1 > last ||
4022 !row_ptr->par()->IsLineSeparator(main_body - 1)))
4025 while (vpos <= last) {
4026 pos = vis2log(vpos);
4027 if (main_body > 0 && pos == main_body - 1) {
4028 x += fill_label_hfill
4029 + lyxfont::width(layout.labelsep,
4030 GetFont(bview->buffer(),
4031 row_ptr->par(), -2))
4032 - SingleWidth(bview,
4037 if (row_ptr->par() ->IsHfill(pos)) {
4040 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4042 y_offset + row_ptr->baseline(),
4045 if (HfillExpansion(bview->buffer(),
4047 if (pos >= main_body) {
4049 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4050 int(x + fill_hfill),
4051 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4053 Painter::line_onoffdash);
4057 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4058 int(x + fill_label_hfill),
4059 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4061 Painter::line_onoffdash);
4063 x += fill_label_hfill;
4066 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4068 y_offset + row_ptr->baseline(),
4073 } else if (row_ptr->par()->IsSeparator(pos)) {
4074 x += SingleWidth(bview,
4075 row_ptr->par(), pos);
4076 if (pos >= main_body)
4077 x += fill_separator;
4080 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
4085 int LyXText::DefaultHeight() const
4087 LyXFont font(LyXFont::ALL_SANE);
4088 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
4092 /* returns the column near the specified x-coordinate of the row
4093 * x is set to the real beginning of this column */
4094 int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
4095 bool & boundary) const
4098 float fill_separator, fill_hfill, fill_label_hfill;
4100 PrepareToPrint(bview, row, tmpx, fill_separator,
4101 fill_hfill, fill_label_hfill);
4103 LyXParagraph::size_type vc = row->pos();
4104 LyXParagraph::size_type last = RowLastPrintable(row);
4105 LyXParagraph::size_type c = 0;
4106 LyXLayout const & layout =
4107 textclasslist.Style(bview->buffer()->params.textclass,
4108 row->par()->GetLayout());
4109 bool left_side = false;
4111 LyXParagraph::size_type
4112 main_body = BeginningOfMainBody(bview->buffer(), row->par());
4113 float last_tmpx = tmpx;
4115 if (main_body > 0 &&
4116 (main_body-1 > last ||
4117 !row->par()->IsLineSeparator(main_body - 1)))
4120 while (vc <= last && tmpx <= x) {
4123 if (main_body > 0 && c == main_body-1) {
4124 tmpx += fill_label_hfill +
4125 lyxfont::width(layout.labelsep,
4126 GetFont(bview->buffer(), row->par(), -2));
4127 if (row->par()->IsLineSeparator(main_body - 1))
4128 tmpx -= SingleWidth(bview, row->par(), main_body-1);
4131 if (HfillExpansion(bview->buffer(), row, c)) {
4132 x += SingleWidth(bview, row->par(), c);
4136 tmpx += fill_label_hfill;
4138 else if (row->par()->IsSeparator(c)) {
4139 tmpx += SingleWidth(bview, row->par(), c);
4141 tmpx+= fill_separator;
4143 tmpx += SingleWidth(bview, row->par(), c);
4147 if ((tmpx + last_tmpx) / 2 > x) {
4152 if (vc > last + 1) // This shouldn't happen.
4156 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
4157 // some speedup if rtl_support=false
4158 && (!row->next() || row->next()->par() != row->par());
4159 bool const rtl = (lastrow)
4160 ? row->par()->isRightToLeftPar(bview->buffer()->params)
4161 : false; // If lastrow is false, we don't need to compute
4162 // the value of rtl.
4164 if (row->pos() > last) // Row is empty?
4167 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
4168 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
4170 else if (vc == row->pos()) {
4172 if (bidi_level(c) % 2 == 1)
4175 c = vis2log(vc - 1);
4176 bool const rtl = (bidi_level(c) % 2 == 1);
4177 if (left_side == rtl) {
4179 boundary = IsBoundary(bview->buffer(), row->par(), c);
4183 if (row->pos() <= last && c > last
4184 && row->par()->IsNewline(last)) {
4185 if (bidi_level(last) % 2 == 0)
4186 tmpx -= SingleWidth(bview, row->par(), last);
4188 tmpx += SingleWidth(bview, row->par(), last);
4199 /* turn the selection into a new environment. If there is no selection,
4200 * create an empty environment */
4201 void LyXText::InsertFootnoteEnvironment(BufferView * bview,
4202 LyXParagraph::footnote_kind kind)
4204 /* no footnoteenvironment in a footnoteenvironment */
4205 if (cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4206 WriteAlert(_("Impossible operation"),
4207 _("You can't insert a float in a float!"),
4211 /* no marginpars in minipages */
4212 if (kind == LyXParagraph::MARGIN
4213 && cursor.par()->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE) {
4214 WriteAlert(_("Impossible operation"),
4215 _("You can't insert a marginpar in a minipage!"),
4220 /* this doesnt make sense, if there is no selection */
4221 bool dummy_selection = false;
4223 sel_start_cursor = cursor; /* dummy selection */
4224 sel_end_cursor = cursor;
4225 dummy_selection = true;
4228 LyXParagraph * tmppar;
4230 /* a test to make sure there is not already a footnote
4231 * in the selection. */
4233 tmppar = sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
4235 while (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos()) &&
4236 tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
4237 tmppar = tmppar->next_;
4239 if (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())
4240 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4241 WriteAlert(_("Impossible operation"),
4242 _("Float would include float!"),
4247 /* ok we have a selection. This is always between sel_start_cursor
4248 * and sel_end cursor */
4250 SetUndo(bview->buffer(), Undo::FINISH,
4251 sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous_,
4252 sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next_);
4254 if (sel_end_cursor.pos() > 0
4255 && sel_end_cursor.par()->IsLineSeparator(sel_end_cursor.pos() - 1))
4256 sel_end_cursor.pos(sel_end_cursor.pos() - 1);
4257 /* please break before a space at the end */
4258 if (sel_start_cursor.par() == sel_end_cursor.par()
4259 && sel_start_cursor.pos() > sel_end_cursor.pos())
4260 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4262 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, sel_end_cursor.pos());
4264 sel_end_cursor.par(sel_end_cursor.par()->next());
4265 sel_end_cursor.pos(0);
4267 // don't forget to insert a dummy layout paragraph if necessary
4268 if (sel_start_cursor.par()->GetLayout() != sel_end_cursor.par()->layout){
4269 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, 0);
4270 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4271 sel_end_cursor.par(sel_end_cursor.par()->next_);
4274 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4276 cursor = sel_end_cursor;
4278 /* please break behind a space, if there is one. The space should
4280 if (sel_start_cursor.pos() > 0
4281 && sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos() - 1))
4282 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4283 if (sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos())) {
4284 sel_start_cursor.par()->Erase(sel_start_cursor.pos());
4287 sel_start_cursor.par()->BreakParagraphConservative(bview->buffer()->params,
4288 sel_start_cursor.pos());
4289 tmppar = sel_start_cursor.par()->next();
4291 if (dummy_selection) {
4293 if (kind == LyXParagraph::TAB
4294 || kind == LyXParagraph::FIG
4295 || kind == LyXParagraph::WIDE_TAB
4296 || kind == LyXParagraph::WIDE_FIG
4297 || kind == LyXParagraph::ALGORITHM) {
4298 pair<bool, LyXTextClass::size_type> lres =
4299 textclasslist.NumberOfLayout(bview->buffer()->params.textclass,
4301 LyXTextClass::size_type lay;
4307 lay = 0; // use default layout "Standard" (0)
4309 tmppar->SetLayout(bview->buffer()->params, lay);
4312 if (sel_start_cursor.pos() > 0) {
4313 /* the footnote-environment should begin with a standard layout.
4314 * Imagine you insert a footnote within an enumeration, you
4315 * certainly do not want an enumerated footnote! */
4318 /* this is a exception the user would sometimes expect, I hope */
4319 sel_start_cursor.par()->Clear();
4323 while (tmppar != sel_end_cursor.par()) {
4324 tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4325 tmppar->footnotekind = kind;
4326 tmppar = tmppar->next();
4329 RedoParagraphs(bview, sel_start_cursor, sel_end_cursor.par()->next());
4331 SetCursor(bview, sel_start_cursor.par()->next(), 0);
4333 ClearSelection(bview);
4338 // returns pointer to a specified row
4339 Row * LyXText::GetRow(LyXParagraph * par,
4340 LyXParagraph::size_type pos, int & y) const
4345 Row * tmprow = firstrow;
4348 // find the first row of the specified paragraph
4349 while (tmprow->next() && tmprow->par() != par) {
4350 y += tmprow->height();
4351 tmprow = tmprow->next();
4354 // now find the wanted row
4355 while (tmprow->pos() < pos
4357 && tmprow->next()->par() == par
4358 && tmprow->next()->pos() <= pos) {
4359 y += tmprow->height();
4360 tmprow = tmprow->next();