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)
857 if ((workWidth(bview) > 0) &&
858 (row->par()->params.pextraType() == LyXParagraph::PEXTRA_INDENT)) {
859 if (!row->par()->params.pextraWidthp().empty()) {
860 x += workWidth(bview) *
861 lyx::atoi(row->par()->params.pextraWidthp()) / 100;
862 } else if (!row->par()->params.pextraWidth().empty()) {
863 int xx = VSpace(row->par()->params.pextraWidth()).inPixels(bview);
864 if (xx > workWidth(bview))
865 xx = workWidth(bview) * 80 / 100;
867 } else { // should not happen
868 LyXFont font(LyXFont::ALL_SANE);
869 x += lyxfont::width("XXXXXX", font);
874 int align; // wrong type
876 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
877 align = layout.align;
879 align = row->par()->FirstPhysicalPar()->params.align();
881 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
882 align = layout.align;
884 align = row->par()->params.align();
886 // set the correct parindent
887 if (row->pos() == 0) {
888 if ((layout.labeltype == LABEL_NO_LABEL
889 || layout.labeltype == LABEL_TOP_ENVIRONMENT
890 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
891 || (layout.labeltype == LABEL_STATIC
892 && layout.latextype == LATEX_ENVIRONMENT
893 && ! row->par()->IsFirstInSequence()))
895 && row->par() == row->par()->FirstPhysicalPar()
897 && align == LYX_ALIGN_BLOCK
898 && !row->par()->params.noindent()
899 && (row->par()->layout ||
900 bview->buffer()->params.paragraph_separation ==
901 BufferParams::PARSEP_INDENT))
902 x += lyxfont::signedWidth(parindent,
904 .TextClass(bview->buffer()->params
907 else if (layout.labeltype == LABEL_BIBLIO) {
908 // ale970405 Right width for bibitems
909 x += bibitemMaxWidth(bview,textclasslist
910 .TextClass(bview->buffer()->params
919 int LyXText::RightMargin(Buffer const * buf, Row const * row) const
921 LyXLayout const & layout =
922 textclasslist.Style(buf->params.textclass,
923 row->par()->GetLayout());
925 int x = LYX_PAPER_MARGIN
926 + lyxfont::signedWidth(textclasslist
927 .TextClass(buf->params.textclass)
930 .TextClass(buf->params.textclass)
934 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
935 x += LYX_PAPER_MARGIN / 2;
938 // this is the way, LyX handles the LaTeX-Environments.
939 // I have had this idea very late, so it seems to be a
940 // later added hack and this is true
941 if (row->par()->GetDepth()) {
942 // find the next level paragraph
944 LyXParagraph * newpar = row->par();
948 newpar = newpar->FirstPhysicalPar()->previous();
950 newpar = newpar->FirstPhysicalPar();
951 } while (newpar && newpar->GetDepth() >= row->par()->GetDepth()
952 && newpar->footnoteflag == row->par()->footnoteflag);
955 newpar = newpar->previous();
957 && newpar->GetDepth() >= row->par()->GetDepth());
960 // make a corresponding row. Needed to call LeftMargin()
962 // check wether it is a sufficent paragraph
965 && newpar->footnoteflag == row->par()->footnoteflag
967 && textclasslist.Style(buf->params.textclass,
971 dummyrow.par(newpar);
973 x = RightMargin(buf, &dummyrow);
975 // this is no longer an error, because this function
976 // is used to clear impossible depths after changing
977 // a layout. Since there is always a redo,
978 // LeftMargin() is always called
980 row->par()->FirstPhysicalPar()->params.depth(0);
982 row->par()->params.depth(0);
987 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
988 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
989 .TextClass(buf->params.textclass)
990 .defaultfont()) * 4 / (row->par()->GetDepth()
996 int LyXText::LabelEnd(BufferView * bview, Row const * row) const
998 if (textclasslist.Style(bview->buffer()->params.textclass,
999 row->par()->GetLayout()).margintype
1004 tmprow.pos(row->par()->Last());
1006 tmprow.pos(row->par()->size());
1008 return LeftMargin(bview, &tmprow); /* just the beginning
1011 return 0; /* LabelEnd is only needed, if the
1012 layout fills a flushleft
1017 // get the next breakpoint in a given paragraph
1018 LyXParagraph::size_type
1019 LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
1021 LyXParagraph * par = row->par();
1030 LyXParagraph::size_type const pos = row->pos();
1033 // position of the last possible breakpoint
1034 // -1 isn't a suitable value, but a flag
1035 LyXParagraph::size_type last_separator = -1;
1036 width -= RightMargin(bview->buffer(), row);
1038 LyXParagraph::size_type const main_body =
1039 BeginningOfMainBody(bview->buffer(), par);
1040 LyXLayout const & layout =
1041 textclasslist.Style(bview->buffer()->params.textclass, par->GetLayout());
1042 LyXParagraph::size_type i = pos;
1044 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1045 /* special code for right address boxes, only newlines count */
1047 while (i < par->Last()) {
1049 while (i < par->size()) {
1051 if (par->IsNewline(i)) {
1054 i = par->Last() - 1; // this means break
1056 i = par->size() - 1; // this means break
1059 } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
1060 par->GetInset(i) && par->GetInset(i)->display()){
1061 par->GetInset(i)->display(false);
1066 // Last position is an invariant
1068 LyXParagraph::size_type const last =
1071 LyXParagraph::size_type const last =
1074 // this is the usual handling
1075 int x = LeftMargin(bview, row);
1076 bool doitonetime = true;
1077 while (doitonetime || ((x < width) && (i < last))) {
1078 doitonetime = false;
1079 char const c = par->GetChar(i);
1080 if (IsNewlineChar(c)) {
1082 x = width; // this means break
1083 } else if (c == LyXParagraph::META_INSET &&
1086 // check wether a Display() inset is
1087 // valid here. if not, change it to
1089 if (par->GetInset(i)->display() &&
1090 (layout.isCommand() ||
1091 (layout.labeltype == LABEL_MANUAL
1092 && i < BeginningOfMainBody(bview->buffer(), par)))) {
1093 // display istn't allowd
1094 par->GetInset(i)->display(false);
1095 x += SingleWidth(bview, par, i, c);
1096 } else if (par->GetInset(i)->display() ||
1097 par->GetInset(i)->needFullRow()) {
1098 // So break the line here
1102 if (IsLineSeparatorChar(par->GetChar(i+1)))
1105 last_separator = last; // to avoid extra rows
1107 last_separator = i - 1;
1108 x = width; // this means break
1114 x += SingleWidth(bview, par, i, c);
1118 if (IsLineSeparatorChar(c))
1120 x += SingleWidth(bview, par, i, c);
1123 if (i == main_body) {
1124 x += lyxfont::width(layout.labelsep,
1125 GetFont(bview->buffer(), par, -2));
1126 if (par->IsLineSeparator(i - 1))
1127 x-= SingleWidth(bview, par, i - 1);
1128 int left_margin = LabelEnd(bview, row);
1129 if (x < left_margin)
1133 // end of paragraph is always a suitable separator
1134 if (i == last && x < width)
1138 // well, if last_separator is still 0, the line isn't breakable.
1139 // don't care and cut simply at the end
1140 if (last_separator < 0) {
1144 // manual labels cannot be broken in LaTeX, do not care
1145 if (main_body && last_separator < main_body)
1146 last_separator = main_body - 1;
1148 return last_separator;
1152 // returns the minimum space a row needs on the screen in pixel
1153 int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
1155 if (paper_width < 0)
1159 // get the pure distance
1160 LyXParagraph::size_type const last = RowLastPrintable(row);
1162 // special handling of the right address boxes
1163 if (textclasslist.Style(bview->buffer()->params.textclass,
1164 row->par()->GetLayout()).margintype
1165 == MARGIN_RIGHT_ADDRESS_BOX) {
1166 int const tmpfill = row->fill();
1167 row->fill(0); // the minfill in MarginLeft()
1168 w = LeftMargin(bview, row);
1171 w = LeftMargin(bview, row);
1173 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1174 row->par()->GetLayout());
1175 LyXParagraph::size_type const main_body =
1176 BeginningOfMainBody(bview->buffer(), row->par());
1177 LyXParagraph::size_type i = row->pos();
1180 if (main_body > 0 && i == main_body) {
1181 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1182 if (row->par()->IsLineSeparator(i - 1))
1183 w -= SingleWidth(bview, row->par(), i - 1);
1184 int left_margin = LabelEnd(bview, row);
1185 if (w < left_margin)
1188 w += SingleWidth(bview, row->par(), i);
1191 if (main_body > 0 && main_body > last) {
1192 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1193 if (last >= 0 && row->par()->IsLineSeparator(last))
1194 w -= SingleWidth(bview, row->par(), last);
1195 int const left_margin = LabelEnd(bview, row);
1196 if (w < left_margin)
1200 int const fill = paper_width - w - RightMargin(bview->buffer(), row);
1205 // returns the minimum space a manual label needs on the screen in pixel
1206 int LyXText::LabelFill(BufferView * bview, Row const * row) const
1208 LyXParagraph::size_type last = BeginningOfMainBody(bview->buffer(), row->par()) - 1;
1209 // -1 because a label ends either with a space that is in the label,
1210 // or with the beginning of a footnote that is outside the label.
1212 // I don't understand this code in depth, but sometimes "last" is
1213 // less than 0 and this causes a crash. This fix seems to work
1214 // correctly, but I bet the real error is elsewhere. The bug is
1215 // triggered when you have an open footnote in a paragraph
1216 // environment with a manual label. (Asger)
1217 if (last < 0) last = 0;
1219 if (row->par()->IsLineSeparator(last)) /* a sepearator at this end
1226 w += SingleWidth(bview, row->par(), i);
1231 if (!row->par()->params.labelWidthString().empty()) {
1232 fill = max(lyxfont::width(row->par()->params.labelWidthString(),
1233 GetFont(bview->buffer(), row->par(), -2)) - w,
1241 // returns the number of separators in the specified row. The separator
1242 // on the very last column doesnt count
1243 int LyXText::NumberOfSeparators(Buffer const * buf, Row const * row) const
1245 LyXParagraph::size_type const last = RowLast(row);
1246 LyXParagraph::size_type p =
1247 max(row->pos(), BeginningOfMainBody(buf, row->par()));
1249 for (; p < last; ++p) {
1250 if (row->par()->IsSeparator(p)) {
1258 // returns the number of hfills in the specified row. The LyX-Hfill is
1259 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1260 // ignored. This is *MUCH* more usefull than not to ignore!
1261 int LyXText::NumberOfHfills(Buffer const * buf, Row const * row) const
1263 LyXParagraph::size_type const last = RowLast(row);
1264 LyXParagraph::size_type first = row->pos();
1265 if (first) { /* hfill *DO* count at the beginning
1267 while(first <= last && row->par()->IsHfill(first))
1271 first = max(first, BeginningOfMainBody(buf, row->par()));
1273 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1274 if (row->par()->IsHfill(p)) {
1282 // like NumberOfHfills, but only those in the manual label!
1283 int LyXText::NumberOfLabelHfills(Buffer const * buf, Row const * row) const
1285 LyXParagraph::size_type last = RowLast(row);
1286 LyXParagraph::size_type first = row->pos();
1287 if (first) { /* hfill *DO* count at the beginning
1289 while(first < last && row->par()->IsHfill(first))
1293 last = min(last, BeginningOfMainBody(buf, row->par()));
1295 for (LyXParagraph::size_type p = first;
1296 p < last; ++p) { // last, because the end is ignored!
1297 if (row->par()->IsHfill(p)) {
1305 // returns true, if a expansion is needed.
1306 // Rules are given by LaTeX
1307 bool LyXText::HfillExpansion(Buffer const * buf, Row const * row_ptr,
1308 LyXParagraph::size_type pos) const
1310 // by the way, is it a hfill?
1311 if (!row_ptr->par()->IsHfill(pos))
1314 // at the end of a row it does not count
1315 if (pos >= RowLast(row_ptr))
1318 // at the beginning of a row it does not count, if it is not
1319 // the first row of a paragaph
1320 if (!row_ptr->pos())
1323 // in some labels it does not count
1324 if (textclasslist.Style(buf->params.textclass,
1325 row_ptr->par()->GetLayout()).margintype
1327 && pos < BeginningOfMainBody(buf, row_ptr->par()))
1330 // if there is anything between the first char of the row and
1331 // the sepcified position that is not a newline and not a hfill,
1332 // the hfill will count, otherwise not
1333 LyXParagraph::size_type i = row_ptr->pos();
1334 while (i < pos && (row_ptr->par()->IsNewline(i)
1335 || row_ptr->par()->IsHfill(i)))
1342 void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
1344 /* get the maximum ascent and the maximum descent */
1347 float layoutasc = 0;
1348 float layoutdesc = 0;
1351 Inset * tmpinset = 0;
1353 /* this must not happen before the currentrow for clear reasons.
1354 so the trick is just to set the current row onto this row */
1356 GetRow(row_ptr->par(), row_ptr->pos(), unused_y);
1358 /* ok , let us initialize the maxasc and maxdesc value.
1359 * This depends in LaTeX of the font of the last character
1360 * in the paragraph. The hack below is necessary because
1361 * of the possibility of open footnotes */
1363 /* Correction: only the fontsize count. The other properties
1364 are taken from the layoutfont. Nicer on the screen :) */
1366 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
1367 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
1369 LyXParagraph * par = row_ptr->par();
1370 LyXParagraph * firstpar = row_ptr->par();
1373 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1374 firstpar->GetLayout());
1377 LyXFont font = GetFont(bview->buffer(), par, par->Last() - 1);
1379 LyXFont font = GetFont(bview->buffer(), par, par->size() - 1);
1381 LyXFont::FONT_SIZE const size = font.size();
1382 font = GetFont(bview->buffer(), par, -1);
1385 LyXFont labelfont = GetFont(bview->buffer(), par, -2);
1387 float spacing_val = 1.0;
1388 if (!row_ptr->par()->params.spacing().isDefault()) {
1389 spacing_val = row_ptr->par()->params.spacing().getValue();
1391 spacing_val = bview->buffer()->params.spacing.getValue();
1393 //lyxerr << "spacing_val = " << spacing_val << endl;
1395 int maxasc = int(lyxfont::maxAscent(font) *
1396 layout.spacing.getValue() *
1398 int maxdesc = int(lyxfont::maxDescent(font) *
1399 layout.spacing.getValue() *
1401 int const pos_end = RowLast(row_ptr);
1405 // Check if any insets are larger
1406 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1407 if (row_ptr->par()->GetChar(pos) == LyXParagraph::META_INSET) {
1408 tmpfont = GetFont(bview->buffer(), row_ptr->par(), pos);
1409 tmpinset = row_ptr->par()->GetInset(pos);
1411 tmpinset->update(bview, tmpfont);
1412 asc = tmpinset->ascent(bview, tmpfont);
1413 desc = tmpinset->descent(bview, tmpfont);
1414 maxwidth += tmpinset->width(bview, tmpfont);
1415 maxasc = max(maxasc, asc);
1416 maxdesc = max(maxdesc, desc);
1419 maxwidth += SingleWidth(bview, row_ptr->par(), pos);
1423 // Check if any custom fonts are larger (Asger)
1424 // This is not completely correct, but we can live with the small,
1425 // cosmetic error for now.
1426 LyXFont::FONT_SIZE const maxsize =
1427 row_ptr->par()->HighestFontInRange(row_ptr->pos(),
1429 if (maxsize > font.size()) {
1430 font.setSize(maxsize);
1432 asc = lyxfont::maxAscent(font);
1433 desc = lyxfont::maxDescent(font);
1440 // This is nicer with box insets:
1444 row_ptr->ascent_of_text(maxasc);
1446 /* is it a top line? */
1447 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1449 /* some parksips VERY EASY IMPLEMENTATION */
1450 if (bview->buffer()->params.paragraph_separation ==
1451 BufferParams::PARSEP_SKIP) {
1452 if (layout.isParagraph()
1453 && firstpar->GetDepth() == 0
1454 && firstpar->previous())
1455 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1456 else if (firstpar->previous()
1457 && textclasslist.Style(bview->buffer()->params.textclass,
1458 firstpar->previous()->GetLayout()).isParagraph()
1459 && firstpar->previous()->GetDepth() == 0)
1460 // is it right to use defskip here too? (AS)
1461 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1464 /* the paper margins */
1466 if (!row_ptr->par()->previous_ && bv_owner)
1467 maxasc += LYX_PAPER_MARGIN;
1469 if (!row_ptr->par()->previous() && bv_owner)
1470 maxasc += LYX_PAPER_MARGIN;
1473 /* add the vertical spaces, that the user added */
1474 if (firstpar->params.spaceTop().kind() != VSpace::NONE)
1475 maxasc += int(firstpar->params.spaceTop().inPixels(bview));
1477 /* do not forget the DTP-lines!
1478 * there height depends on the font of the nearest character */
1479 if (firstpar->params.lineTop())
1480 maxasc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1483 /* and now the pagebreaks */
1484 if (firstpar->params.pagebreakTop())
1485 maxasc += 3 * DefaultHeight();
1487 /* this is special code for the chapter, since the label of this
1488 * layout is printed in an extra row */
1489 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1490 && bview->buffer()->params.secnumdepth >= 0) {
1491 float spacing_val = 1.0;
1492 if (!row_ptr->par()->params.spacing().isDefault()) {
1493 spacing_val = row_ptr->par()->params.spacing().getValue();
1495 spacing_val = bview->buffer()->params.spacing.getValue();
1498 labeladdon = int(lyxfont::maxDescent(labelfont) *
1499 layout.spacing.getValue() *
1501 + int(lyxfont::maxAscent(labelfont) *
1502 layout.spacing.getValue() *
1506 /* special code for the top label */
1507 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1508 || layout.labeltype == LABEL_BIBLIO
1509 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1510 && row_ptr->par()->IsFirstInSequence()
1511 && !row_ptr->par()->GetLabelstring().empty()) {
1512 float spacing_val = 1.0;
1513 if (!row_ptr->par()->params.spacing().isDefault()) {
1514 spacing_val = row_ptr->par()->params.spacing().getValue();
1516 spacing_val = bview->buffer()->params.spacing.getValue();
1520 (lyxfont::maxAscent(labelfont) *
1521 layout.spacing.getValue() *
1523 +(lyxfont::maxDescent(labelfont) *
1524 layout.spacing.getValue() *
1526 + layout.topsep * DefaultHeight()
1527 + layout.labelbottomsep * DefaultHeight());
1530 /* and now the layout spaces, for example before and after a section,
1531 * or between the items of a itemize or enumerate environment */
1533 if (!firstpar->params.pagebreakTop()) {
1534 LyXParagraph * prev = row_ptr->par()->previous();
1536 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth());
1537 if (prev && prev->GetLayout() == firstpar->GetLayout()
1538 && prev->GetDepth() == firstpar->GetDepth()
1539 && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1541 layoutasc = (layout.itemsep * DefaultHeight());
1543 else if (row_ptr->previous()) {
1544 tmptop = layout.topsep;
1546 if (row_ptr->previous()->par()->GetDepth() >= row_ptr->par()->GetDepth())
1547 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1548 row_ptr->previous()->par()->
1549 GetLayout()).bottomsep;
1552 layoutasc = (tmptop * DefaultHeight());
1554 else if (row_ptr->par()->params.lineTop()) {
1555 tmptop = layout.topsep;
1558 layoutasc = (tmptop * DefaultHeight());
1561 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth()-1);
1563 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1564 prev->GetLayout()).parsep * DefaultHeight());
1567 if (firstpar->previous()
1568 && firstpar->previous()->GetDepth() == 0
1569 && firstpar->previous()->GetLayout() != firstpar->GetLayout()) {
1572 else if (firstpar->previous()){
1573 maxasc += int(layout.parsep * DefaultHeight());
1579 /* is it a bottom line? */
1582 row_ptr->par()->ParFromPos(RowLast(row_ptr) + 1) == par
1584 row_ptr->par() == par
1586 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1588 /* the paper margins */
1590 if (!par->next_ && bv_owner)
1591 maxdesc += LYX_PAPER_MARGIN;
1593 if (!par->next() && bv_owner)
1594 maxdesc += LYX_PAPER_MARGIN;
1597 /* add the vertical spaces, that the user added */
1598 if (firstpar->params.spaceBottom().kind() != VSpace::NONE)
1599 maxdesc += int(firstpar->params.spaceBottom().inPixels(bview));
1601 /* do not forget the DTP-lines!
1602 * there height depends on the font of the nearest character */
1604 if (firstpar->params.lineBottom())
1605 maxdesc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1606 par, par->Last() - 1));
1608 if (firstpar->params.lineBottom())
1609 maxdesc += 2 * lyxfont::ascent('x',
1610 GetFont(bview->buffer(),
1611 par, par->size() - 1));
1614 /* and now the pagebreaks */
1615 if (firstpar->params.pagebreakBottom())
1616 maxdesc += 3 * DefaultHeight();
1618 /* and now the layout spaces, for example before and after a section,
1619 * or between the items of a itemize or enumerate environment */
1620 if (!firstpar->params.pagebreakBottom() && row_ptr->par()->next()) {
1621 LyXParagraph * nextpar = row_ptr->par()->next();
1622 LyXParagraph * comparepar = row_ptr->par();
1626 if (comparepar->GetDepth() > nextpar->GetDepth()) {
1627 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1628 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1629 if (comparepar->GetLayout()!= nextpar->GetLayout()
1630 || nextpar->GetLabelWidthString() !=
1631 comparepar->GetLabelWidthString())
1632 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1634 if (unusual > usual)
1635 layoutdesc = unusual;
1639 else if (comparepar->GetDepth() == nextpar->GetDepth()) {
1641 if (comparepar->GetLayout()!= nextpar->GetLayout()
1642 || nextpar->GetLabelWidthString() !=
1643 comparepar->GetLabelWidthString())
1644 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1649 /* incalculate the layout spaces */
1650 maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1651 maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1653 /* calculate the new height of the text */
1654 height -= row_ptr->height();
1656 row_ptr->height(maxasc + maxdesc + labeladdon);
1657 row_ptr->baseline(maxasc + labeladdon);
1659 height += row_ptr->height();
1661 PrepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1662 row_ptr->width(int(maxwidth + x));
1665 width = max(0,workWidth(bview));
1667 if (r->width() > width)
1675 /* Appends the implicit specified paragraph behind the specified row,
1676 * start at the implicit given position */
1677 void LyXText::AppendParagraph(BufferView * bview, Row * row) const
1679 bool not_ready = true;
1681 // The last character position of a paragraph is an invariant so we can
1682 // safely get it here. (Asger)
1684 int const lastposition = row->par()->Last();
1686 int const lastposition = row->par()->size();
1689 // Get the next breakpoint
1690 int z = NextBreakPoint(bview, row, workWidth(bview));
1694 // Insert the new row
1695 if (z < lastposition) {
1697 InsertRow(row, row->par(), z);
1704 // Set the dimensions of the row
1705 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1706 SetHeightOfRow(bview, tmprow);
1708 } while (not_ready);
1712 void LyXText::BreakAgain(BufferView * bview, Row * row) const
1714 bool not_ready = true;
1717 /* get the next breakpoint */
1718 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1722 if (z < row->par()->Last()) {
1724 if (z < row->par()->size()) {
1726 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1729 InsertRow(row, row->par(), z);
1735 if (row->pos() == z)
1736 not_ready = false; // the rest will not change
1742 /* if there are some rows too much, delete them */
1743 /* only if you broke the whole paragraph! */
1744 Row * tmprow2 = row;
1745 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1746 tmprow2 = tmprow2->next();
1748 while (tmprow2 != row) {
1749 tmprow2 = tmprow2->previous();
1750 RemoveRow(tmprow2->next());
1755 /* set the dimensions of the row */
1756 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1757 SetHeightOfRow(bview, tmprow);
1758 } while (not_ready);
1762 // this is just a little changed version of break again
1763 void LyXText::BreakAgainOneRow(BufferView * bview, Row * row)
1765 // get the next breakpoint
1766 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1770 if (z < row->par()->Last()) {
1772 if (z < row->par()->size()) {
1775 || (row->next() && row->next()->par() != row->par())) {
1776 /* insert a new row */
1778 InsertRow(row, row->par(), z);
1784 if (row->pos() != z)
1788 // if there are some rows too much, delete them
1789 // only if you broke the whole paragraph!
1790 Row * tmprow2 = row;
1791 while (tmprow2->next()
1792 && tmprow2->next()->par() == row->par()) {
1793 tmprow2 = tmprow2->next();
1795 while (tmprow2 != row) {
1796 tmprow2 = tmprow2->previous();
1797 RemoveRow(tmprow2->next());
1801 // set the dimensions of the row
1802 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1803 SetHeightOfRow(bview, tmprow);
1807 void LyXText::BreakParagraph(BufferView * bview, char keep_layout)
1809 LyXLayout const & layout =
1810 textclasslist.Style(bview->buffer()->params.textclass,
1811 cursor.par()->GetLayout());
1813 // this is only allowed, if the current paragraph is not empty or caption
1815 if ((cursor.par()->Last() <= 0
1816 && !cursor.par()->IsDummy())
1817 && layout.labeltype!= LABEL_SENSITIVE)
1820 SetUndo(bview->buffer(), Undo::INSERT,
1821 cursor.par()->ParFromPos(cursor.pos())->previous_,
1822 cursor.par()->ParFromPos(cursor.pos())->next_);
1824 if ((cursor.par()->size() <= 0)
1825 && layout.labeltype!= LABEL_SENSITIVE)
1828 SetUndo(bview->buffer(), Undo::INSERT,
1829 cursor.par()->previous(),
1830 cursor.par()->next());
1833 // Always break behind a space
1835 // It is better to erase the space (Dekel)
1837 if (cursor.pos() < cursor.par()->Last()
1839 if (cursor.pos() < cursor.par()->size()
1841 && cursor.par()->IsLineSeparator(cursor.pos()))
1842 cursor.par()->Erase(cursor.pos());
1843 // cursor.pos(cursor.pos() + 1);
1845 // break the paragraph
1849 keep_layout = layout.isEnvironment();
1850 cursor.par()->BreakParagraph(bview->buffer()->params, cursor.pos(),
1853 // well this is the caption hack since one caption is really enough
1854 if (layout.labeltype == LABEL_SENSITIVE) {
1856 // set to standard-layout
1857 cursor.par()->SetLayout(bview->buffer()->params, 0);
1859 // set to standard-layout
1860 cursor.par()->next()->SetLayout(bview->buffer()->params, 0);
1863 /* if the cursor is at the beginning of a row without prior newline,
1865 * This touches only the screen-update. Otherwise we would may have
1866 * an empty row on the screen */
1867 if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1)
1868 && cursor.row()->pos() == cursor.pos()) {
1872 status = LyXText::NEED_MORE_REFRESH;
1873 refresh_row = cursor.row();
1874 refresh_y = cursor.y() - cursor.row()->baseline();
1876 // Do not forget the special right address boxes
1877 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1878 while (refresh_row->previous() &&
1879 refresh_row->previous()->par() == refresh_row->par()) {
1880 refresh_row = refresh_row->previous();
1881 refresh_y -= refresh_row->height();
1884 RemoveParagraph(cursor.row());
1886 // set the dimensions of the cursor row
1887 cursor.row()->fill(Fill(bview, cursor.row(), workWidth(bview)));
1889 SetHeightOfRow(bview, cursor.row());
1892 while (cursor.par()->next()->Last()
1894 while (cursor.par()->next()->size()
1896 && cursor.par()->next()->IsNewline(0))
1897 cursor.par()->next()->Erase(0);
1899 InsertParagraph(bview, cursor.par()->next(), cursor.row());
1901 UpdateCounters(bview, cursor.row()->previous());
1903 /* This check is necessary. Otherwise the new empty paragraph will
1904 * be deleted automatically. And it is more friendly for the user! */
1906 SetCursor(bview, cursor.par()->next(), 0);
1908 SetCursor(bview, cursor.par(), 0);
1910 if (cursor.row()->next())
1911 BreakAgain(bview, cursor.row()->next());
1918 void LyXText::OpenFootnote(BufferView * bview)
1920 LyXParagraph * endpar,* tmppar;
1923 LyXParagraph * par = cursor.par()->ParFromPos(cursor.pos());
1925 /* if there is no footnote in this paragraph, just return. */
1927 || par->next_->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1930 /* ok, move the cursor right before the footnote */
1932 /* just a little faster than using CursorRight() */
1934 cursor.par()->ParFromPos(cursor.pos()) != par;) {
1935 cursor.pos(cursor.pos() + 1);
1938 /* now the cursor is at the beginning of the physical par */
1939 SetCursor(bview, cursor.par(),
1940 cursor.pos() + cursor.par()->ParFromPos(cursor.pos())->size());
1942 /* the cursor must be exactly before the footnote */
1943 par = cursor.par()->ParFromPos(cursor.pos());
1945 status = LyXText::NEED_MORE_REFRESH;
1946 refresh_row = cursor.row();
1947 refresh_y = cursor.y() - cursor.row()->baseline();
1949 tmppar = cursor.par();
1950 endpar = cursor.par()->next();
1953 tmppar->OpenFootnote(cursor.pos());
1954 RemoveParagraph(row);
1955 /* set the dimensions of the cursor row */
1956 row->fill(Fill(bview, row, workWidth(bview)));
1957 SetHeightOfRow(bview, row);
1958 // CHECK See comment on top of text.C
1959 tmppar = tmppar->next();
1961 while (tmppar != endpar) {
1963 InsertParagraph(bview, tmppar, row);
1964 while (row->next() && row->next()->par() == tmppar)
1966 tmppar = tmppar->next();
1969 SetCursor(bview, par->next_, 0);
1970 sel_cursor = cursor;
1975 // Just a macro to make some thing easier.
1976 void LyXText::RedoParagraph(BufferView * bview) const
1978 ClearSelection(bview);
1979 RedoParagraphs(bview, cursor, cursor.par()->next());
1980 SetCursorIntern(bview, cursor.par(), cursor.pos());
1984 /* insert a character, moves all the following breaks in the
1985 * same Paragraph one to the right and make a rebreak */
1986 void LyXText::InsertChar(BufferView * bview, char c)
1988 SetUndo(bview->buffer(), Undo::INSERT,
1990 cursor.par()->ParFromPos(cursor.pos())->previous_,
1991 cursor.par()->ParFromPos(cursor.pos())->next_
1993 cursor.par()->previous(),
1994 cursor.par()->next()
1998 // When the free-spacing option is set for the current layout,
1999 // disable the double-space checking
2001 bool const freeSpacing =
2002 textclasslist.Style(bview->buffer()->params.textclass,
2003 cursor.row()->par()->GetLayout()).free_spacing;
2006 if (lyxrc.auto_number) {
2007 static string const number_operators = "+-/*";
2008 static string const number_unary_operators = "+-";
2009 static string const number_seperators = ".,:";
2011 if (current_font.number() == LyXFont::ON) {
2012 if (!isdigit(c) && !contains(number_operators, c) &&
2013 !(contains(number_seperators, c) &&
2014 cursor.pos() >= 1 &&
2015 cursor.pos() < cursor.par()->size() &&
2016 GetFont(bview->buffer(),
2018 cursor.pos()).number() == LyXFont::ON &&
2019 GetFont(bview->buffer(),
2021 cursor.pos()-1).number() == LyXFont::ON)
2023 Number(bview); // Set current_font.number to OFF
2024 } else if (isdigit(c) &&
2025 real_current_font.isVisibleRightToLeft()) {
2026 Number(bview); // Set current_font.number to ON
2028 if (cursor.pos() > 0) {
2029 char const c = cursor.par()->GetChar(cursor.pos() - 1);
2030 if (contains(number_unary_operators, c) &&
2031 (cursor.pos() == 1 ||
2032 cursor.par()->IsSeparator(cursor.pos() - 2) ||
2033 cursor.par()->IsNewline(cursor.pos() - 2) )
2035 SetCharFont(bview->buffer(),
2039 } else if (contains(number_seperators, c) &&
2040 cursor.pos() >= 2 &&
2041 GetFont(bview->buffer(),
2043 cursor.pos()-2).number() == LyXFont::ON) {
2044 SetCharFont(bview->buffer(),
2054 /* First check, if there will be two blanks together or a blank at
2055 the beginning of a paragraph.
2056 I decided to handle blanks like normal characters, the main
2057 difference are the special checks when calculating the row.fill
2058 (blank does not count at the end of a row) and the check here */
2060 // The bug is triggered when we type in a description environment:
2061 // The current_font is not changed when we go from label to main text
2062 // and it should (along with realtmpfont) when we type the space.
2063 // CHECK There is a bug here! (Asger)
2065 LyXFont realtmpfont = real_current_font;
2066 LyXFont rawtmpfont = current_font; /* store the current font.
2067 * This is because of the use
2068 * of cursor movements. The moving
2069 * cursor would refresh the
2072 // Get the font that is used to calculate the baselineskip
2074 LyXParagraph::size_type const lastpos = cursor.par()->Last();
2076 LyXParagraph::size_type const lastpos = cursor.par()->size();
2078 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2081 bool jumped_over_space = false;
2083 if (!freeSpacing && IsLineSeparatorChar(c)) {
2084 if ((cursor.pos() > 0
2085 && cursor.par()->IsLineSeparator(cursor.pos() - 1))
2086 || (cursor.pos() > 0
2087 && cursor.par()->IsNewline(cursor.pos() - 1))
2088 || (cursor.pos() == 0
2090 && !(cursor.par()->previous()
2091 && cursor.par()->previous()->footnoteflag
2092 == LyXParagraph::OPEN_FOOTNOTE)
2095 if (cursor.pos() == 0 )
2096 bview->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
2098 bview->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way. Please read the Tutorial."));
2102 } else if (IsNewlineChar(c)) {
2104 if (cursor.par()->FirstPhysicalPar() == cursor.par()
2106 if (cursor.par() == cursor.par()
2108 && cursor.pos() <= BeginningOfMainBody(bview->buffer(), cursor.par())) {
2112 /* No newline at first position
2113 * of a paragraph or behind labels.
2114 * TeX does not allow that. */
2117 if (cursor.pos() < cursor.par()->Last() &&
2119 if (cursor.pos() < cursor.par()->size() &&
2121 cursor.par()->IsLineSeparator(cursor.pos()))
2122 // newline always after a blank!
2124 cursor.row()->fill(-1); // to force a new break
2127 // the display inset stuff
2128 if (cursor.row()->par()->GetChar(cursor.row()->pos()) == LyXParagraph::META_INSET
2129 && cursor.row()->par()->GetInset(cursor.row()->pos())
2130 && (cursor.row()->par()->GetInset(cursor.row()->pos())->display() ||
2131 cursor.row()->par()->GetInset(cursor.row()->pos())->needFullRow()))
2132 cursor.row()->fill(-1); // to force a new break
2134 // get the cursor row fist
2135 Row * row = cursor.row();
2136 int y = cursor.y() - row->baseline();
2137 if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset
2138 * already insertet the character */
2139 cursor.par()->InsertChar(cursor.pos(), c);
2140 SetCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
2142 if (!jumped_over_space) {
2143 // refresh the positions
2145 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2146 tmprow = tmprow->next();
2147 tmprow->pos(tmprow->pos() + 1);
2151 // Is there a break one row above
2152 if ((cursor.par()->IsLineSeparator(cursor.pos())
2153 || cursor.par()->IsNewline(cursor.pos())
2154 || cursor.row()->fill() == -1)
2155 && row->previous() && row->previous()->par() == row->par()) {
2156 LyXParagraph::size_type z = NextBreakPoint(bview,
2159 if (z >= row->pos()) {
2162 // set the dimensions of the row above
2163 row->previous()->fill(Fill(bview,
2167 SetHeightOfRow(bview, row->previous());
2169 y -= row->previous()->height();
2171 refresh_row = row->previous();
2172 status = LyXText::NEED_MORE_REFRESH;
2174 BreakAgainOneRow(bview, row);
2176 current_font = rawtmpfont;
2177 real_current_font = realtmpfont;
2178 SetCursor(bview, cursor.par(), cursor.pos() + 1,
2179 false, cursor.boundary());
2180 // cursor MUST be in row now.
2182 if (row->next() && row->next()->par() == row->par())
2183 need_break_row = row->next();
2187 // check, wether the last characters font has changed.
2189 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2191 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2193 && rawparfont != rawtmpfont)
2194 RedoHeightOfParagraph(bview, cursor);
2201 // recalculate the fill of the row
2202 if (row->fill() >= 0) /* needed because a newline
2203 * will set fill to -1. Otherwise
2204 * we would not get a rebreak! */
2205 row->fill(Fill(bview, row, workWidth(bview)));
2206 if (row->fill() < 0) {
2209 refresh_x = cursor.x();
2210 refresh_pos = cursor.pos();
2211 status = LyXText::NEED_MORE_REFRESH;
2212 BreakAgainOneRow(bview, row);
2213 // will the cursor be in another row now?
2214 if (RowLast(row) <= cursor.pos() + 1 && row->next()) {
2215 if (row->next() && row->next()->par() == row->par())
2216 // this should always be true
2218 BreakAgainOneRow(bview, row);
2220 current_font = rawtmpfont;
2221 real_current_font = realtmpfont;
2223 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2225 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
2226 != cursor.boundary())
2227 SetCursor(bview, cursor.par(), cursor.pos(), false,
2228 !cursor.boundary());
2229 if (row->next() && row->next()->par() == row->par())
2230 need_break_row = row->next();
2235 refresh_x = cursor.x();
2237 refresh_pos = cursor.pos();
2239 int const tmpheight = row->height();
2240 SetHeightOfRow(bview, row);
2241 if (tmpheight == row->height())
2242 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2244 status = LyXText::NEED_MORE_REFRESH;
2246 current_font = rawtmpfont;
2247 real_current_font = realtmpfont;
2248 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2252 // check, wether the last characters font has changed.
2254 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2256 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2258 && rawparfont != rawtmpfont) {
2259 RedoHeightOfParagraph(bview, cursor);
2261 // now the special right address boxes
2262 if (textclasslist.Style(bview->buffer()->params.textclass,
2263 cursor.par()->GetLayout()).margintype
2264 == MARGIN_RIGHT_ADDRESS_BOX) {
2265 RedoDrawingOfParagraph(bview, cursor);
2273 void LyXText::charInserted()
2275 // Here we could call FinishUndo for every 20 characters inserted.
2276 // This is from my experience how emacs does it.
2277 static unsigned int counter = 0;
2287 void LyXText::PrepareToPrint(BufferView * bview,
2288 Row * row, float & x,
2289 float & fill_separator,
2291 float & fill_label_hfill,
2296 float w = row->fill();
2298 fill_label_hfill = 0;
2300 fill_label_hfill = 0;
2303 row->par()->isRightToLeftPar(bview->buffer()->params);
2305 x = (workWidth(bview) > 0)
2306 ? RightMargin(bview->buffer(), row) : 0;
2308 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
2309 LyXFont font(LyXFont::ALL_SANE);
2310 font.setSize(LyXFont::SIZE_SMALL);
2311 x += lyxfont::width("Mwide-figM", font);
2315 x = (workWidth(bview) > 0) ? LeftMargin(bview, row) : 0;
2317 // is there a manual margin with a manual label
2318 if (textclasslist.Style(bview->buffer()->params.textclass,
2319 row->par()->GetLayout()).margintype == MARGIN_MANUAL
2320 && textclasslist.Style(bview->buffer()->params.textclass,
2321 row->par()->GetLayout()).labeltype == LABEL_MANUAL) {
2323 /* one more since labels are left aligned */
2324 nlh = NumberOfLabelHfills(bview->buffer(), row) + 1;
2325 if (nlh && !row->par()->GetLabelWidthString().empty()) {
2326 fill_label_hfill = LabelFill(bview, row) / nlh;
2330 // are there any hfills in the row?
2331 float const nh = NumberOfHfills(bview->buffer(), row);
2334 fill_hfill = w / nh;
2336 // is it block, flushleft or flushright?
2337 // set x how you need it
2340 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
2341 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2343 align = row->par()->FirstPhysicalPar()->params.align();
2345 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
2346 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2348 align = row->par()->params.align();
2351 // center displayed insets
2353 if (row->par()->GetChar(row->pos()) == LyXParagraph::META_INSET
2354 && (inset=row->par()->GetInset(row->pos()))
2355 && (inset->display())) // || (inset->scroll() < 0)))
2356 align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
2357 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2360 case LYX_ALIGN_BLOCK:
2361 ns = NumberOfSeparators(bview->buffer(), row);
2362 if (ns && row->next() && row->next()->par() == row->par() &&
2363 !(row->next()->par()->IsNewline(row->next()->pos() - 1))
2364 && !(row->next()->par()->GetChar(row->next()->pos()) == LyXParagraph::META_INSET
2365 && row->next()->par()->GetInset(row->next()->pos())
2366 && row->next()->par()->GetInset(row->next()->pos())->display())
2368 fill_separator = w / ns;
2372 case LYX_ALIGN_RIGHT:
2375 case LYX_ALIGN_CENTER:
2383 ComputeBidiTables(bview->buffer(), row);
2385 LyXParagraph::size_type main_body =
2386 BeginningOfMainBody(bview->buffer(), row->par());
2387 LyXParagraph::size_type last = RowLast(row);
2389 if (main_body > 0 &&
2390 (main_body-1 > last ||
2391 !row->par()->IsLineSeparator(main_body-1))) {
2392 LyXLayout const & layout =
2393 textclasslist.Style(bview->buffer()->params.textclass,
2394 row->par()->GetLayout());
2395 x += lyxfont::width(layout.labelsep,
2396 GetFont(bview->buffer(), row->par(), -2));
2397 if (main_body-1 <= last)
2398 x += fill_label_hfill;
2403 /* important for the screen */
2406 /* the cursor set functions have a special mechanism. When they
2407 * realize, that you left an empty paragraph, they will delete it.
2408 * They also delete the corresponding row */
2410 void LyXText::CursorRightOneWord(BufferView * bview) const
2412 // treat floats, HFills and Insets as words
2413 LyXCursor tmpcursor = cursor;
2414 // CHECK See comment on top of text.C
2417 if (tmpcursor.pos() == tmpcursor.par()->Last()
2419 if (tmpcursor.pos() == tmpcursor.par()->size()
2421 && tmpcursor.par()->next()) {
2422 tmpcursor.par(tmpcursor.par()->next());
2427 // Skip through initial nonword stuff.
2429 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2431 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2433 ! tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2435 // printf("Current pos1 %d", tmpcursor.pos()) ;
2436 tmpcursor.pos(tmpcursor.pos() + 1);
2439 // Advance through word.
2441 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2443 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2445 tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2447 // printf("Current pos2 %d", tmpcursor.pos()) ;
2448 tmpcursor.pos(tmpcursor.pos() + 1);
2452 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2456 void LyXText::CursorTab(BufferView * bview) const
2458 LyXCursor tmpcursor = cursor;
2460 while (tmpcursor.pos() < tmpcursor.par()->Last()
2462 while (tmpcursor.pos() < tmpcursor.par()->size()
2464 && !tmpcursor.par()->IsNewline(tmpcursor.pos()))
2465 tmpcursor.pos(tmpcursor.pos() + 1);
2468 if (tmpcursor.pos() == tmpcursor.par()->Last()){
2470 if (tmpcursor.pos() == tmpcursor.par()->size()){
2472 if (tmpcursor.par()->next()) {
2473 tmpcursor.par(tmpcursor.par()->next());
2477 tmpcursor.pos(tmpcursor.pos() + 1);
2478 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2482 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2483 of prior word, not to end of next prior word. */
2485 void LyXText::CursorLeftOneWord(BufferView * bview) const
2487 // treat HFills, floats and Insets as words
2488 LyXCursor tmpcursor = cursor;
2489 while (tmpcursor.pos()
2490 && (tmpcursor.par()->IsSeparator(tmpcursor.pos() - 1)
2491 || tmpcursor.par()->IsKomma(tmpcursor.pos() - 1))
2492 && !(tmpcursor.par()->IsHfill(tmpcursor.pos() - 1)
2494 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2496 || tmpcursor.par()->IsInset(tmpcursor.pos() - 1)))
2497 tmpcursor.pos(tmpcursor.pos() - 1);
2500 && (tmpcursor.par()->IsInset(tmpcursor.pos() - 1)
2502 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2504 || tmpcursor.par()->IsHfill(tmpcursor.pos() - 1))) {
2505 tmpcursor.pos(tmpcursor.pos() - 1);
2506 } else if (!tmpcursor.pos()) {
2507 if (tmpcursor.par()->previous()){
2508 tmpcursor.par(tmpcursor.par()->previous());
2510 tmpcursor.pos(tmpcursor.par()->Last());
2512 tmpcursor.pos(tmpcursor.par()->size());
2515 } else { // Here, tmpcursor != 0
2516 while (tmpcursor.pos() > 0 &&
2517 tmpcursor.par()->IsWord(tmpcursor.pos()-1) )
2518 tmpcursor.pos(tmpcursor.pos() - 1);
2520 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2523 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2526 void LyXText::SelectWord(BufferView * bview)
2528 // Move cursor to the beginning, when not already there.
2530 && !cursor.par()->IsSeparator(cursor.pos()-1)
2531 && !cursor.par()->IsKomma(cursor.pos()-1) )
2532 CursorLeftOneWord(bview);
2534 // set the sel cursor
2535 sel_cursor = cursor;
2538 while (cursor.pos() < cursor.par()->Last()
2540 while (cursor.pos() < cursor.par()->size()
2542 && !cursor.par()->IsSeparator(cursor.pos())
2543 && !cursor.par()->IsKomma(cursor.pos()) )
2544 cursor.pos(cursor.pos() + 1);
2545 SetCursor(bview, cursor.par(), cursor.pos() );
2547 // finally set the selection
2548 SetSelection(bview);
2552 /* -------> Select the word currently under the cursor when:
2553 1: no selection is currently set,
2554 2: the cursor is not at the borders of the word. */
2556 bool LyXText::SelectWordWhenUnderCursor(BufferView * bview)
2560 cursor.pos() > 0 && cursor.pos() < cursor.par()->Last()
2562 cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2564 && !cursor.par()->IsSeparator(cursor.pos())
2565 && !cursor.par()->IsKomma(cursor.pos())
2566 && !cursor.par()->IsSeparator(cursor.pos() -1)
2567 && !cursor.par()->IsKomma(cursor.pos() -1)) {
2575 // This function is only used by the spellchecker for NextWord().
2576 // It doesn't handle LYX_ACCENTs and probably never will.
2577 string const LyXText::SelectNextWord(BufferView * bview,
2578 float & value) const
2580 LyXParagraph * tmppar = cursor.par();
2582 // If this is not the very first word, skip rest of
2583 // current word because we are probably in the middle
2584 // of a word if there is text here.
2586 if (cursor.pos() || cursor.par()->previous_) {
2587 while (cursor.pos() < cursor.par()->Last()
2588 && cursor.par()->IsLetter(cursor.pos()))
2589 cursor.pos(cursor.pos() + 1);
2592 if (cursor.pos() || cursor.par()->previous()) {
2593 while (cursor.pos() < cursor.par()->size()
2594 && cursor.par()->IsLetter(cursor.pos()))
2595 cursor.pos(cursor.pos() + 1);
2599 // Now, skip until we have real text (will jump paragraphs)
2601 while ((cursor.par()->Last() > cursor.pos()
2603 while ((cursor.par()->size() > cursor.pos()
2605 && (!cursor.par()->IsLetter(cursor.pos())
2606 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2607 .latex() == LyXFont::ON))
2609 || (cursor.par()->Last() == cursor.pos()
2611 || (cursor.par()->size() == cursor.pos()
2613 && cursor.par()->next())){
2615 if (cursor.pos() == cursor.par()->Last()) {
2617 if (cursor.pos() == cursor.par()->size()) {
2619 cursor.par(cursor.par()->next());
2622 cursor.pos(cursor.pos() + 1);
2625 // Update the value if we changed paragraphs
2626 if (cursor.par() != tmppar){
2627 SetCursor(bview, cursor.par(), cursor.pos());
2628 value = float(cursor.y())/float(height);
2631 // Start the selection from here
2632 sel_cursor = cursor;
2634 std::ostringstream latex;
2636 // and find the end of the word
2637 // (optional hyphens are part of a word)
2639 while (cursor.pos() < cursor.par()->Last()
2641 while (cursor.pos() < cursor.par()->size()
2643 && (cursor.par()->IsLetter(cursor.pos()))
2644 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2645 && cursor.par()->GetInset(cursor.pos()) != 0
2646 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2647 && latex.str() == "\\-"
2649 cursor.pos(cursor.pos() + 1);
2651 // Finally, we copy the word to a string and return it
2653 if (sel_cursor.pos() < cursor.pos()) {
2654 LyXParagraph::size_type i;
2655 for (i = sel_cursor.pos(); i < cursor.pos(); ++i) {
2656 if (cursor.par()->GetChar(i) != LyXParagraph::META_INSET)
2657 str += cursor.par()->GetChar(i);
2664 // This one is also only for the spellchecker
2665 void LyXText::SelectSelectedWord(BufferView * bview)
2667 // move cursor to the beginning
2668 SetCursor(bview, sel_cursor.par(), sel_cursor.pos());
2670 // set the sel cursor
2671 sel_cursor = cursor;
2673 std::ostringstream latex;
2675 // now find the end of the word
2677 while (cursor.pos() < cursor.par()->Last()
2679 while (cursor.pos() < cursor.par()->size()
2681 && (cursor.par()->IsLetter(cursor.pos())
2682 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2683 && cursor.par()->GetInset(cursor.pos()) != 0
2684 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2685 && latex.str() == "\\-"
2687 cursor.pos(cursor.pos() + 1);
2689 SetCursor(bview, cursor.par(), cursor.pos());
2691 // finally set the selection
2692 SetSelection(bview);
2696 /* -------> Delete from cursor up to the end of the current or next word. */
2697 void LyXText::DeleteWordForward(BufferView * bview)
2700 if (!cursor.par()->Last())
2702 if (!cursor.par()->size())
2706 LyXCursor tmpcursor = cursor;
2707 tmpcursor.row(0); //Â ??
2708 selection = true; // to avoid deletion
2709 CursorRightOneWord(bview);
2710 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2711 sel_cursor = cursor;
2713 SetSelection(bview);
2715 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2716 CutSelection(bview);
2721 /* -------> Delete from cursor to start of current or prior word. */
2722 void LyXText::DeleteWordBackward(BufferView * bview)
2725 if (!cursor.par()->Last())
2727 if (!cursor.par()->size())
2731 LyXCursor tmpcursor = cursor;
2732 tmpcursor.row(0); // ??
2733 selection = true; // to avoid deletion
2734 CursorLeftOneWord(bview);
2735 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2736 sel_cursor = cursor;
2738 SetSelection(bview);
2739 CutSelection(bview);
2744 /* -------> Kill to end of line. */
2745 void LyXText::DeleteLineForward(BufferView * bview)
2748 if (!cursor.par()->Last())
2750 if (!cursor.par()->size())
2752 // Paragraph is empty, so we just go to the right
2755 LyXCursor tmpcursor = cursor;
2756 // We can't store the row over a regular SetCursor
2757 // so we set it to 0 and reset it afterwards.
2758 tmpcursor.row(0); //Â ??
2759 selection = true; // to avoid deletion
2761 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2762 sel_cursor = cursor;
2764 SetSelection(bview);
2765 // What is this test for ??? (JMarc)
2767 DeleteWordForward(bview);
2769 CutSelection(bview);
2775 // Change the case of a word at cursor position.
2776 // This function directly manipulates LyXParagraph::text because there
2777 // is no LyXParagraph::SetChar currently. I did what I could to ensure
2778 // that it is correct. I guess part of it should be moved to
2779 // LyXParagraph, but it will have to change for 1.1 anyway. At least
2780 // it does not access outside of the allocated array as the older
2781 // version did. (JMarc)
2782 void LyXText::ChangeWordCase(BufferView * bview, LyXText::TextCase action)
2785 LyXParagraph * tmppar = cursor.par()->ParFromPos(cursor.pos());
2787 SetUndo(bview->buffer(),Undo::FINISH,
2788 tmppar->previous_, tmppar->next_);
2790 LyXParagraph::size_type tmppos =
2791 cursor.par()->PositionInParFromPos(cursor.pos());
2793 LyXParagraph * tmppar = cursor.par();
2795 SetUndo(bview->buffer(),Undo::FINISH,
2796 tmppar->previous(), tmppar->next());
2798 LyXParagraph::size_type tmppos = cursor.pos();
2801 while (tmppos < tmppar->size()) {
2802 unsigned char c = tmppar->GetChar(tmppos);
2803 if (IsKommaChar(c) || IsLineSeparatorChar(c))
2805 if (c != LyXParagraph::META_INSET) {
2807 case text_lowercase:
2810 case text_capitalization:
2812 action = text_lowercase;
2814 case text_uppercase:
2820 //tmppar->text[tmppos] = c;
2821 tmppar->SetChar(tmppos, c);
2824 CheckParagraph(bview, tmppar, tmppos);
2825 CursorRightOneWord(bview);
2829 void LyXText::Delete(BufferView * bview)
2831 // this is a very easy implementation
2833 LyXCursor old_cursor = cursor;
2834 int const old_cur_par_id = old_cursor.par()->id();
2836 int const old_cur_par_prev_id = old_cursor.par()->previous_ ?
2837 old_cursor.par()->previous_->id() : 0;
2839 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2840 old_cursor.par()->previous()->id() : 0;
2843 // just move to the right
2846 // CHECK Look at the comment here.
2847 // This check is not very good...
2848 // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
2849 // and that can very well delete the par or par->previous in
2850 // old_cursor. Will a solution where we compare paragraph id's
2854 (cursor.par()->previous_ ? cursor.par()->previous_->id() : 0)
2856 (cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2858 == old_cur_par_prev_id
2859 && cursor.par()->id() != old_cur_par_id)
2860 return; // delete-empty-paragraph-mechanism has done it
2862 // if you had success make a backspace
2863 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2864 LyXCursor tmpcursor = cursor;
2865 cursor = old_cursor; // to make sure undo gets the right cursor position
2866 SetUndo(bview->buffer(), Undo::DELETE,
2868 cursor.par()->ParFromPos(cursor.pos())->previous_,
2869 cursor.par()->ParFromPos(cursor.pos())->next_
2871 cursor.par()->previous(),
2872 cursor.par()->next()
2881 void LyXText::Backspace(BufferView * bview)
2883 // Get the font that is used to calculate the baselineskip
2885 LyXParagraph::size_type lastpos = cursor.par()->Last();
2887 LyXParagraph::size_type lastpos = cursor.par()->size();
2889 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2892 if (cursor.pos() == 0) {
2893 // The cursor is at the beginning of a paragraph,
2894 // so the the backspace will collapse two paragraphs into one.
2896 // we may paste some paragraphs
2898 // is it an empty paragraph?
2901 || (lastpos == 1 && cursor.par()->IsSeparator(0)))
2903 && !(cursor.par()->next()
2904 && cursor.par()->footnoteflag == LyXParagraph::NO_FOOTNOTE
2905 && cursor.par()->next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
2908 // This is an empty paragraph and we delete it just by moving the cursor one step
2909 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2910 // of the paragraph.
2913 if (cursor.par()->previous_) {
2914 LyXParagraph * tmppar = cursor.par()->previous_->FirstPhysicalPar();
2915 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2916 && cursor.par()->footnoteflag == tmppar->footnoteflag
2918 if (cursor.par()->previous()) {
2919 LyXParagraph * tmppar = cursor.par()->previous();
2920 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2922 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2923 // Inherit bottom DTD from the paragraph below.
2924 // (the one we are deleting)
2925 tmppar->params.lineBottom(cursor.par()->params.lineBottom());
2926 tmppar->params.spaceBottom(cursor.par()->params.spaceBottom());
2927 tmppar->params.pagebreakBottom(cursor.par()->params.pagebreakBottom());
2932 // the layout things can change the height of a row !
2933 int const tmpheight = cursor.row()->height();
2934 SetHeightOfRow(bview, cursor.row());
2935 if (cursor.row()->height() != tmpheight) {
2936 refresh_y = cursor.y() - cursor.row()->baseline();
2937 refresh_row = cursor.row();
2938 status = LyXText::NEED_MORE_REFRESH;
2945 if (cursor.par()->ParFromPos(cursor.pos())->previous_) {
2946 SetUndo(bview->buffer(), Undo::DELETE,
2947 cursor.par()->ParFromPos(cursor.pos())->previous_->previous_,
2948 cursor.par()->ParFromPos(cursor.pos())->next_);
2951 if (cursor.par()->previous()) {
2952 SetUndo(bview->buffer(), Undo::DELETE,
2953 cursor.par()->previous()->previous(),
2954 cursor.par()->next());
2958 LyXParagraph * tmppar = cursor.par();
2959 Row * tmprow = cursor.row();
2961 // We used to do CursorLeftIntern() here, but it is
2962 // not a good idea since it triggers the auto-delete
2963 // mechanism. So we do a CursorLeftIntern()-lite,
2964 // without the dreaded mechanism. (JMarc)
2965 if (cursor.par()->previous()) {
2966 // steps into the above paragraph.
2968 SetCursorIntern(bview, cursor.par()->previous(),
2969 cursor.par()->previous()->Last(), false);
2971 SetCursorIntern(bview, cursor.par()->previous(),
2972 cursor.par()->previous()->size(),
2977 /* Pasting is not allowed, if the paragraphs have different
2978 layout. I think it is a real bug of all other
2979 word processors to allow it. It confuses the user.
2980 Even so with a footnote paragraph and a non-footnote
2981 paragraph. I will not allow pasting in this case,
2982 because the user would be confused if the footnote behaves
2983 different wether it is open or closed.
2985 Correction: Pasting is always allowed with standard-layout
2987 if (cursor.par() != tmppar
2988 && (cursor.par()->GetLayout() == tmppar->GetLayout()
2989 || tmppar->GetLayout() == 0 /*standard*/)
2991 && cursor.par()->footnoteflag == tmppar->footnoteflag
2993 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2995 RemoveParagraph(tmprow);
2997 cursor.par()->PasteParagraph(bview->buffer()->params);
2999 if (!cursor.pos() || !cursor.par()->IsSeparator(cursor.pos() - 1))
3000 ; //cursor.par()->InsertChar(cursor.pos(), ' ');
3001 // strangely enough it seems that commenting out the line above removes
3002 // most or all of the segfaults. I will however also try to move the
3003 // two Remove... lines in front of the PasteParagraph too.
3006 cursor.pos(cursor.pos() - 1);
3008 status = LyXText::NEED_MORE_REFRESH;
3009 refresh_row = cursor.row();
3010 refresh_y = cursor.y() - cursor.row()->baseline();
3012 // remove the lost paragraph
3013 // This one is not safe, since the paragraph that the tmprow and the
3014 // following rows belong to has been deleted by the PasteParagraph
3015 // above. The question is... could this be moved in front of the
3017 //RemoveParagraph(tmprow);
3018 //RemoveRow(tmprow);
3020 // This rebuilds the rows.
3021 AppendParagraph(bview, cursor.row());
3022 UpdateCounters(bview, cursor.row());
3024 // the row may have changed, block, hfills etc.
3025 SetCursor(bview, cursor.par(), cursor.pos(), false);
3028 /* this is the code for a normal backspace, not pasting
3030 SetUndo(bview->buffer(), Undo::DELETE,
3032 cursor.par()->ParFromPos(cursor.pos())->previous_,
3033 cursor.par()->ParFromPos(cursor.pos())->next_
3035 cursor.par()->previous(),
3036 cursor.par()->next()
3039 // We used to do CursorLeftIntern() here, but it is
3040 // not a good idea since it triggers the auto-delete
3041 // mechanism. So we do a CursorLeftIntern()-lite,
3042 // without the dreaded mechanism. (JMarc)
3043 SetCursorIntern(bview, cursor.par(), cursor.pos()- 1,
3044 false, cursor.boundary());
3046 // some insets are undeletable here
3047 if (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
3048 if (!cursor.par()->GetInset(cursor.pos())->Deletable())
3050 // force complete redo when erasing display insets
3051 // this is a cruel method but safe..... Matthias
3052 if (cursor.par()->GetInset(cursor.pos())->display() ||
3053 cursor.par()->GetInset(cursor.pos())->needFullRow()) {
3054 cursor.par()->Erase(cursor.pos());
3055 RedoParagraph(bview);
3060 Row * row = cursor.row();
3061 int y = cursor.y() - row->baseline();
3062 LyXParagraph::size_type z;
3063 /* remember that a space at the end of a row doesnt count
3064 * when calculating the fill */
3065 if (cursor.pos() < RowLast(row) ||
3066 !cursor.par()->IsLineSeparator(cursor.pos())) {
3067 row->fill(row->fill() + SingleWidth(bview,
3072 /* some special code when deleting a newline. This is similar
3073 * to the behavior when pasting paragraphs */
3074 if (cursor.pos() && cursor.par()->IsNewline(cursor.pos())) {
3075 cursor.par()->Erase(cursor.pos());
3076 // refresh the positions
3078 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3079 tmprow = tmprow->next();
3080 tmprow->pos(tmprow->pos() - 1);
3082 if (cursor.par()->IsLineSeparator(cursor.pos() - 1))
3083 cursor.pos(cursor.pos() - 1);
3086 if (cursor.pos() < cursor.par()->Last()
3087 && !cursor.par()->IsSeparator(cursor.pos())) {
3089 if (cursor.pos() < cursor.par()->size()
3090 && !cursor.par()->IsSeparator(cursor.pos())) {
3092 cursor.par()->InsertChar(cursor.pos(), ' ');
3093 SetCharFont(bview->buffer(), cursor.par(),
3094 cursor.pos(), current_font);
3095 // refresh the positions
3097 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3098 tmprow = tmprow->next();
3099 tmprow->pos(tmprow->pos() + 1);
3103 cursor.par()->Erase(cursor.pos());
3105 // refresh the positions
3107 while (tmprow->next()
3108 && tmprow->next()->par() == row->par()) {
3109 tmprow = tmprow->next();
3110 tmprow->pos(tmprow->pos() - 1);
3113 // delete newlines at the beginning of paragraphs
3115 while (cursor.par()->Last() &&
3117 while (cursor.par()->size() &&
3119 cursor.par()->IsNewline(cursor.pos()) &&
3120 cursor.pos() == BeginningOfMainBody(bview->buffer(),
3122 cursor.par()->Erase(cursor.pos());
3123 // refresh the positions
3125 while (tmprow->next() &&
3126 tmprow->next()->par() == row->par()) {
3127 tmprow = tmprow->next();
3128 tmprow->pos(tmprow->pos() - 1);
3133 // is there a break one row above
3134 if (row->previous() && row->previous()->par() == row->par()) {
3135 z = NextBreakPoint(bview, row->previous(),
3137 if (z >= row->pos()) {
3140 Row * tmprow = row->previous();
3142 // maybe the current row is now empty
3144 if (row->pos() >= row->par()->Last()) {
3146 if (row->pos() >= row->par()->size()) {
3152 BreakAgainOneRow(bview, row);
3153 if (row->next() && row->next()->par() == row->par())
3154 need_break_row = row->next();
3159 // set the dimensions of the row above
3160 y -= tmprow->height();
3161 tmprow->fill(Fill(bview, tmprow,
3163 SetHeightOfRow(bview, tmprow);
3166 refresh_row = tmprow;
3167 status = LyXText::NEED_MORE_REFRESH;
3168 SetCursor(bview, cursor.par(), cursor.pos(),
3169 false, cursor.boundary());
3170 //current_font = rawtmpfont;
3171 //real_current_font = realtmpfont;
3172 // check, whether the last character's font has changed.
3175 cursor.par()->GetFontSettings(bview->buffer()->params,
3176 cursor.par()->Last() - 1))
3179 cursor.par()->GetFontSettings(bview->buffer()->params,
3180 cursor.par()->size() - 1))
3182 RedoHeightOfParagraph(bview, cursor);
3187 // break the cursor row again
3188 if (row->next() && row->next()->par() == row->par() &&
3190 (RowLast(row) == row->par()->Last() - 1 ||
3192 (RowLast(row) == row->par()->size() - 1 ||
3194 NextBreakPoint(bview, row, workWidth(bview)) != RowLast(row))) {
3196 /* it can happen that a paragraph loses one row
3197 * without a real breakup. This is when a word
3198 * is to long to be broken. Well, I don t care this
3201 if (RowLast(row) == row->par()->Last() - 1)
3203 if (RowLast(row) == row->par()->size() - 1)
3205 RemoveRow(row->next());
3209 status = LyXText::NEED_MORE_REFRESH;
3211 BreakAgainOneRow(bview, row);
3212 // will the cursor be in another row now?
3213 if (row->next() && row->next()->par() == row->par() &&
3214 RowLast(row) <= cursor.pos()) {
3216 BreakAgainOneRow(bview, row);
3219 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3221 if (row->next() && row->next()->par() == row->par())
3222 need_break_row = row->next();
3226 // set the dimensions of the row
3227 row->fill(Fill(bview, row, workWidth(bview)));
3228 int const tmpheight = row->height();
3229 SetHeightOfRow(bview, row);
3230 if (tmpheight == row->height())
3231 status = LyXText::NEED_VERY_LITTLE_REFRESH;
3233 status = LyXText::NEED_MORE_REFRESH;
3236 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3240 // current_font = rawtmpfont;
3241 // real_current_font = realtmpfont;
3243 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
3244 != cursor.boundary())
3245 SetCursor(bview, cursor.par(), cursor.pos(), false,
3246 !cursor.boundary());
3249 lastpos = cursor.par()->Last();
3251 lastpos = cursor.par()->size();
3253 if (cursor.pos() == lastpos)
3254 SetCurrentFont(bview);
3256 // check, whether the last characters font has changed.
3258 cursor.par()->GetFontSettings(bview->buffer()->params, lastpos - 1)) {
3259 RedoHeightOfParagraph(bview, cursor);
3261 // now the special right address boxes
3262 if (textclasslist.Style(bview->buffer()->params.textclass,
3263 cursor.par()->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3264 RedoDrawingOfParagraph(bview, cursor);
3270 void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
3271 Row * row_ptr, int y, bool cleared)
3273 /* returns a printed row */
3274 Painter & pain = bview->painter();
3277 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
3279 LyXParagraph::size_type const last = RowLastPrintable(row_ptr);
3281 LyXParagraph::size_type vpos, pos;
3287 LyXFont font(LyXFont::ALL_SANE);
3289 if (row_ptr->height() <= 0) {
3290 lyxerr << "LYX_ERROR: row.height: "
3291 << row_ptr->height() << endl;
3295 float fill_separator;
3297 float fill_label_hfill;
3298 PrepareToPrint(bview, row_ptr, x, fill_separator,
3299 fill_hfill, fill_label_hfill);
3301 if (inset_owner && (x < 0))
3305 // clear the area where we want to paint/print
3306 int ww = bview->workWidth();
3308 bool clear_area = true;
3311 if (!bview->screen()->forceClear() && last == row_ptr->pos()
3312 && row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET
3313 && (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
3314 clear_area = inset->doClearArea();
3316 // we don't need to clear it's already done!!!
3319 } else if (clear_area) {
3320 #ifdef WITH_WARNINGS
3321 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
3322 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
3324 int const y = y_offset < 0 ? 0 : y_offset;
3325 int const h = y_offset < 0 ?
3326 row_ptr->height() + y_offset : row_ptr->height();
3327 int const w = inset_owner ?
3328 inset_owner->width(bview, font) - 2 : ww;
3329 int const x = x_offset;
3330 pain.fillRectangle(x, y, w, h);
3331 } else if (inset != 0) {
3332 int h = row_ptr->baseline() - inset->ascent(bview, font);
3334 int const w = (inset_owner ?
3335 inset_owner->width(bview, font) : ww);
3336 pain.fillRectangle(x_offset, y_offset, w, h);
3338 h += inset->ascent(bview, font) + inset->descent(bview, font);
3339 if ((row_ptr->height() - h) > 0) {
3340 int const w = (inset_owner ?
3341 inset_owner->width(bview, font) : ww);
3342 pain.fillRectangle(x_offset, y_offset + h,
3343 w, row_ptr->height() - h);
3345 if (!inset_owner && !inset->display() && !inset->needFullRow())
3347 int const w = inset->width(bview, font) + int(x);
3348 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
3353 int const w = (inset_owner ?
3354 inset_owner->width(bview, font) : ww);
3355 /* selection code */
3356 if (bidi_same_direction) {
3357 if (sel_start_cursor.row() == row_ptr &&
3358 sel_end_cursor.row() == row_ptr) {
3359 if (sel_start_cursor.x() < sel_end_cursor.x())
3360 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3362 sel_end_cursor.x() - sel_start_cursor.x(),
3366 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3368 sel_start_cursor.x() - sel_end_cursor.x(),
3371 } else if (sel_start_cursor.row() == row_ptr) {
3373 pain.fillRectangle(x_offset, y_offset,
3374 sel_start_cursor.x(),
3378 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3380 w - sel_start_cursor.x(),
3383 } else if (sel_end_cursor.row() == row_ptr) {
3385 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3387 w - sel_end_cursor.x(),
3391 pain.fillRectangle(x_offset, y_offset,
3395 } else if (y > sel_start_cursor.y()
3396 && y < sel_end_cursor.y()) {
3397 pain.fillRectangle(x_offset, y_offset, w,
3401 } else if (sel_start_cursor.row() != row_ptr &&
3402 sel_end_cursor.row() != row_ptr &&
3403 y > sel_start_cursor.y()
3404 && y < sel_end_cursor.y()) {
3405 pain.fillRectangle(x_offset, y_offset, w,
3408 } else if (sel_start_cursor.row() == row_ptr ||
3409 sel_end_cursor.row() == row_ptr) {
3411 if ((sel_start_cursor.row() != row_ptr && !is_rtl) ||
3412 (sel_end_cursor.row() != row_ptr && is_rtl))
3413 pain.fillRectangle(x_offset, y_offset,
3417 LyXParagraph::size_type main_body =
3418 BeginningOfMainBody(bview->buffer(),
3421 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3422 pos = vis2log(vpos);
3423 float const old_tmpx = tmpx;
3424 if (main_body > 0 && pos == main_body-1) {
3425 tmpx += fill_label_hfill +
3426 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3427 row_ptr->par()->GetLayout()).labelsep,
3428 GetFont(bview->buffer(),row_ptr->par(), -2));
3429 if (row_ptr->par()->IsLineSeparator(main_body-1))
3430 tmpx -= SingleWidth(bview, row_ptr->par(), main_body-1);
3432 if (HfillExpansion(bview->buffer(), row_ptr, pos)) {
3433 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3434 if (pos >= main_body)
3437 tmpx += fill_label_hfill;
3439 else if (row_ptr->par()->IsSeparator(pos)) {
3440 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3441 if (pos >= main_body)
3442 tmpx += fill_separator;
3444 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3446 if ((sel_start_cursor.row() != row_ptr ||
3447 sel_start_cursor.pos() <= pos) &&
3448 (sel_end_cursor.row() != row_ptr ||
3449 pos < sel_end_cursor.pos()) )
3450 /// Here we do not use x_offset as x_offset was
3452 pain.fillRectangle(int(old_tmpx),
3454 int(tmpx - old_tmpx + 1),
3459 if ((sel_start_cursor.row() != row_ptr && is_rtl) ||
3460 (sel_end_cursor.row() != row_ptr && !is_rtl) )
3461 pain.fillRectangle(x_offset + int(tmpx),
3471 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3472 LyXFont font(LyXFont::ALL_SANE);
3473 font.setSize(LyXFont::SIZE_FOOTNOTE);
3474 font.setColor(LColor::footnote);
3476 box_x = LYX_PAPER_MARGIN + lyxfont::width(" wide-tab ", font);
3477 if (row_ptr->previous() &&
3478 row_ptr->previous()->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3480 switch (row_ptr->par()->footnotekind) {
3481 case LyXParagraph::MARGIN:
3484 case LyXParagraph::FIG:
3487 case LyXParagraph::TAB:
3490 case LyXParagraph::WIDE_FIG:
3493 case LyXParagraph::WIDE_TAB:
3496 case LyXParagraph::ALGORITHM:
3499 case LyXParagraph::FOOTNOTE:
3504 pain.fillRectangle(LYX_PAPER_MARGIN,
3506 box_x - LYX_PAPER_MARGIN,
3507 int(lyxfont::maxAscent(font)
3508 + lyxfont::maxDescent(font)),
3509 LColor::footnotebg);
3511 pain.line(LYX_PAPER_MARGIN, y_offset,
3512 workWidth(bview) - LYX_PAPER_MARGIN, y_offset,
3513 LColor::footnoteframe);
3515 pain.text(LYX_PAPER_MARGIN,
3516 y_offset + int(lyxfont::maxAscent(font)) + 1,
3519 pain.line(LYX_PAPER_MARGIN, y_offset,
3521 y_offset + int(lyxfont::maxAscent(font)
3522 + lyxfont::maxDescent(font)),
3523 LColor::footnoteframe);
3525 pain.line(LYX_PAPER_MARGIN,
3526 y_offset + int(lyxfont::maxAscent(font)
3527 + lyxfont::maxDescent(font)) + 1,
3529 y_offset + int(lyxfont::maxAscent(font)
3530 + lyxfont::maxDescent(font)) + 1,
3531 LColor::footnoteframe);
3535 /* draw the open floats in a red box */
3536 pain.line(box_x, y_offset,
3537 box_x, y_offset + row_ptr->height(),
3538 LColor::footnoteframe);
3540 pain.line(workWidth(bview) - LYX_PAPER_MARGIN,
3542 workWidth(bview) - LYX_PAPER_MARGIN,
3543 y_offset + row_ptr->height(),
3544 LColor::footnoteframe);
3547 // Draw appendix lines
3548 LyXParagraph * p = row_ptr->par()->PreviousBeforeFootnote()->FirstPhysicalPar();
3549 if (p->params.appendix()) {
3550 pain.line(1, y_offset,
3551 1, y_offset + row_ptr->height(),
3552 LColor::appendixline);
3553 pain.line(workWidth(bview) - 2, y_offset,
3554 workWidth(bview) - 2,
3555 y_offset + row_ptr->height(),
3556 LColor::appendixline);
3560 // Draw minipage line
3561 bool const minipage =
3562 (p->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3564 pain.line(LYX_PAPER_MARGIN/5, y_offset,
3566 y_offset + row_ptr->height() - 1,
3567 LColor::minipageline);
3570 int const depth = p->GetDepth();
3571 for (int i = 1; i <= depth; ++i) {
3572 int const line_x = (LYX_PAPER_MARGIN / 5) *
3574 pain.line(line_x, y_offset, line_x,
3575 y_offset + row_ptr->height() - 1,
3578 } else if (row_ptr->previous() &&
3579 row_ptr->previous()->par()->footnoteflag
3580 == LyXParagraph::OPEN_FOOTNOTE) {
3581 LyXFont font(LyXFont::ALL_SANE);
3582 font.setSize(LyXFont::SIZE_FOOTNOTE);
3584 int const box_x = LYX_PAPER_MARGIN
3585 + lyxfont::width(" wide-tab ", font);
3587 pain.line(box_x, y_offset,
3588 workWidth(bview) - LYX_PAPER_MARGIN,
3589 y_offset, LColor::footnote);
3592 // Draw appendix lines
3594 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
3596 LyXParagraph * firstpar = row_ptr->par();
3598 if (firstpar->params.appendix()) {
3599 pain.line(1, y_offset,
3600 1, y_offset + row_ptr->height(),
3601 LColor::appendixline);
3602 pain.line(ww - 2, y_offset,
3603 ww - 2, y_offset + row_ptr->height(),
3604 LColor::appendixline);
3607 // Draw minipage line
3608 bool const minipage =
3609 (firstpar->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3611 pain.line(LYX_PAPER_MARGIN/5 + box_x, y_offset,
3612 LYX_PAPER_MARGIN/5 + box_x,
3613 y_offset + row_ptr->height() - 1,
3614 LColor::minipageline);
3617 int const depth = firstpar->GetDepth();
3621 if (row_ptr->next())
3623 if (row_ptr->par()->footnoteflag ==
3624 row_ptr->next()->par()->footnoteflag)
3625 next_depth = row_ptr->next()->par()->GetDepth();
3626 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3629 next_depth = row_ptr->next()->par()->GetDepth();
3631 if (row_ptr->previous())
3633 if (row_ptr->par()->footnoteflag ==
3634 row_ptr->previous()->par()->footnoteflag)
3635 prev_depth = row_ptr->previous()->par()->GetDepth();
3636 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3639 prev_depth = row_ptr->previous()->par()->GetDepth();
3642 for (int i = 1; i <= depth; ++i) {
3643 int const line_x = (LYX_PAPER_MARGIN / 5) *
3648 ) + box_x + x_offset;
3649 pain.line(line_x, y_offset, line_x,
3650 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3654 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3657 pain.fillRectangle(line_x,
3658 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3659 LYX_PAPER_MARGIN / 5, 2,
3665 LyXLayout const & layout =
3666 textclasslist.Style(bview->buffer()->params.textclass,
3667 row_ptr->par()->GetLayout());
3670 y_bottom = row_ptr->height();
3672 /* is it a first row? */
3673 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3675 /* start of appendix? */
3676 if (row_ptr->par()->params.startOfAppendix()) {
3677 pain.line(1, y_offset,
3679 LColor::appendixline);
3682 /* think about the margins */
3683 if (!row_ptr->previous() && bv_owner)
3684 y_top += LYX_PAPER_MARGIN;
3686 /* draw a top pagebreak */
3687 if (row_ptr->par()->params.pagebreakTop()) {
3689 pb_font.setColor(LColor::pagebreak).decSize();
3690 int w = 0, a = 0, d = 0;
3691 pain.line(0, y_offset + y_top + 2*DefaultHeight(),
3693 y_offset + y_top + 2 * DefaultHeight(),
3695 Painter::line_onoffdash)
3698 _("Page Break (top)"),
3701 LColor::background, false, w, a, d);
3702 pain.rectText((ww - w)/2,
3703 y_offset + y_top + 2 * DefaultHeight() + d,
3704 _("Page Break (top)"),
3707 LColor::background);
3708 y_top += 3 * DefaultHeight();
3711 if (row_ptr->par()->params.spaceTop().kind() == VSpace::VFILL) {
3712 /* draw a vfill top */
3713 pain.line(0, y_offset + 2 + y_top,
3714 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3717 pain.line(0, y_offset + y_top + 3 * DefaultHeight(),
3719 y_offset + y_top + 3 * DefaultHeight(),
3722 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3723 LYX_PAPER_MARGIN / 2,
3724 y_offset + y_top + 3 * DefaultHeight(),
3727 y_top += 3 * DefaultHeight();
3730 /* think about user added space */
3731 y_top += int(row_ptr->par()->params.spaceTop().inPixels(bview));
3733 /* think about the parskip */
3734 /* some parskips VERY EASY IMPLEMENTATION */
3735 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3736 if (layout.latextype == LATEX_PARAGRAPH
3737 && firstpar->GetDepth() == 0
3738 && firstpar->previous())
3739 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3740 else if (firstpar->previous()
3741 && textclasslist.Style(bview->buffer()->params.textclass,
3742 firstpar->previous()->GetLayout()).latextype == LATEX_PARAGRAPH
3743 && firstpar->previous()->GetDepth() == 0)
3744 // is it right to use defskip here, too? (AS)
3745 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3748 if (row_ptr->par()->params.lineTop()) { /* draw a top line */
3749 y_top += lyxfont::ascent('x',
3750 GetFont(bview->buffer(),
3751 row_ptr->par(), 0));
3752 int const w = (inset_owner ?
3753 inset_owner->width(bview, font) : ww);
3754 int const xp = static_cast<int>(inset_owner ? x : 0);
3755 pain.line(xp, y_offset + y_top,
3756 w, y_offset + y_top,
3758 Painter::line_solid,
3759 Painter::line_thick);
3761 y_top += lyxfont::ascent('x',GetFont(bview->buffer(),
3762 row_ptr->par(), 0));
3765 /* should we print a label? */
3766 if (layout.labeltype >= LABEL_STATIC
3767 && (layout.labeltype != LABEL_STATIC
3768 || layout.latextype != LATEX_ENVIRONMENT
3769 || row_ptr->par()->IsFirstInSequence())) {
3770 font = GetFont(bview->buffer(), row_ptr->par(), -2);
3771 if (!row_ptr->par()->GetLabelstring().empty()) {
3773 string const tmpstring =
3774 row_ptr->par()->GetLabelstring();
3776 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3777 if (bview->buffer()->params.secnumdepth >= 0) {
3778 /* this is special code for the chapter layout. This is printed in
3779 * an extra row and has a pagebreak at the top. */
3780 float spacing_val = 1.0;
3781 if (!row_ptr->par()->params.spacing().isDefault()) {
3782 spacing_val = row_ptr->par()->params.spacing().getValue();
3784 spacing_val = bview->buffer()->params.spacing.getValue();
3787 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3788 + int(layout.parsep) * DefaultHeight();
3790 tmpx = ww - LeftMargin(bview, row_ptr) -
3791 lyxfont::width(tmpstring, font);
3792 pain.text(int(tmpx),
3793 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3798 tmpx = ww - LeftMargin(bview, row_ptr)
3799 + lyxfont::width(layout.labelsep, font);
3801 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3802 LyXFont font(LyXFont::ALL_SANE);
3803 font.setSize(LyXFont::SIZE_SMALL);
3804 tmpx += lyxfont::width("Mwide-fixM", font);
3808 tmpx = x - lyxfont::width(layout.labelsep, font)
3809 - lyxfont::width(tmpstring, font);
3812 pain.text(int(tmpx),
3813 y_offset + row_ptr->baseline(),
3817 /* the labels at the top of an environment. More or less for bibliography */
3818 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3819 layout.labeltype == LABEL_BIBLIO ||
3820 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3821 if (row_ptr->par()->IsFirstInSequence()) {
3822 font = GetFont(bview->buffer(),
3823 row_ptr->par(), -2);
3824 if (!row_ptr->par()->GetLabelstring().empty()) {
3825 string const tmpstring =
3826 row_ptr->par()->GetLabelstring();
3827 float spacing_val = 1.0;
3828 if (!row_ptr->par()->params.spacing().isDefault()) {
3829 spacing_val = row_ptr->par()->params.spacing().getValue();
3831 spacing_val = bview->buffer()->params.spacing.getValue();
3834 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3835 + (layout.labelbottomsep * DefaultHeight()));
3838 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3839 tmpx = ( (is_rtl ? LeftMargin(bview, row_ptr) : x)
3840 + ww - RightMargin(bview->buffer(), row_ptr) ) / 2;
3841 tmpx -= lyxfont::width(tmpstring, font) / 2;
3843 tmpx = ww - LeftMargin(bview, row_ptr) -
3844 lyxfont::width(tmpstring, font);
3845 pain.text(int(tmpx),
3846 y_offset + row_ptr->baseline()
3847 - row_ptr->ascent_of_text()
3853 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3854 font = GetFont(bview->buffer(), row_ptr->par(), -1);
3856 tmpx = ww - LeftMargin(bview, row_ptr)
3857 + lyxfont::width(layout.labelsep, font);
3859 tmpx = x - lyxfont::width(layout.labelsep, font)
3860 - row_ptr->par()->bibkey->width(bview, font);
3861 row_ptr->par()->bibkey->draw(bview, font,
3862 y_offset + row_ptr->baseline(),
3867 /* is it a last row? */
3869 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
3871 LyXParagraph * par = row_ptr->par();
3875 row_ptr->par()->ParFromPos(last + 1) == par
3877 row_ptr->par() == par
3879 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
3881 /* think about the margins */
3882 if (!row_ptr->next() && bv_owner)
3883 y_bottom -= LYX_PAPER_MARGIN;
3885 /* draw a bottom pagebreak */
3886 if (firstpar->params.pagebreakBottom()) {
3888 pb_font.setColor(LColor::pagebreak).decSize();
3889 int w = 0, a = 0, d = 0;
3891 y_offset + y_bottom - 2 * DefaultHeight(),
3893 y_offset + y_bottom - 2 * DefaultHeight(),
3895 Painter::line_onoffdash)
3898 _("Page Break (bottom)"),
3901 LColor::background, false, w, a, d);
3902 pain.rectText((ww - w) / 2,
3903 y_offset + y_top + 2 * DefaultHeight() + d,
3904 _("Page Break (bottom)"),
3907 LColor::background);
3908 y_bottom -= 3 * DefaultHeight();
3911 if (firstpar->params.spaceBottom().kind() == VSpace::VFILL) {
3912 /* draw a vfill bottom */
3913 pain.line(0, y_offset + y_bottom - 3 * DefaultHeight(),
3915 y_offset + y_bottom - 3 * DefaultHeight(),
3917 pain.line(0, y_offset + y_bottom - 2,
3919 y_offset + y_bottom - 2,
3921 pain.line(LYX_PAPER_MARGIN / 2,
3922 y_offset + y_bottom - 3 * DefaultHeight(),
3923 LYX_PAPER_MARGIN / 2,
3924 y_offset + y_bottom - 2,
3926 y_bottom -= 3 * DefaultHeight();
3929 /* think about user added space */
3930 y_bottom -= int(firstpar->params.spaceBottom().inPixels(bview));
3932 if (firstpar->params.lineBottom()) {
3933 /* draw a bottom line */
3935 y_bottom -= lyxfont::ascent('x',
3936 GetFont(bview->buffer(),
3940 y_bottom -= lyxfont::ascent('x',
3941 GetFont(bview->buffer(),
3945 int const w = (inset_owner ?
3946 inset_owner->width(bview, font) : ww);
3947 int const xp = static_cast<int>(inset_owner ? x : 0);
3948 pain.line(xp, y_offset + y_bottom,
3949 w, y_offset + y_bottom,
3950 LColor::topline, Painter::line_solid,
3951 Painter::line_thick);
3953 y_bottom -= lyxfont::ascent('x',
3954 GetFont(bview->buffer(),
3958 y_bottom -= lyxfont::ascent('x',
3959 GetFont(bview->buffer(),
3966 int const endlabel =
3967 row_ptr->par()->GetEndLabel(bview->buffer()->params);
3970 case END_LABEL_FILLED_BOX:
3972 LyXFont const font = GetFont(bview->buffer(),
3973 row_ptr->par(), last);
3974 int const size = int(0.75 * lyxfont::maxAscent(font));
3975 int const y = (y_offset + row_ptr->baseline()) - size;
3976 int x = is_rtl ? LYX_PAPER_MARGIN
3977 : ww - LYX_PAPER_MARGIN - size;
3979 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
3981 LyXFont font(LyXFont::ALL_SANE);
3982 font.setSize(LyXFont::SIZE_SMALL);
3983 x += lyxfont::width("Mwide-figM", font);
3985 x -= LYX_PAPER_MARGIN/2;
3987 if (row_ptr->fill() <= size)
3988 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3989 if (endlabel == END_LABEL_BOX) {
3990 pain.line(x, y, x, y + size,
3992 pain.line(x + size, y, x + size , y + size,
3994 pain.line(x, y, x + size, y,
3996 pain.line(x, y + size, x + size, y + size,
3999 pain.fillRectangle(x, y, size, size,
4003 case END_LABEL_STATIC:
4005 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->GetLayout();
4006 string const tmpstring = textclasslist.
4007 Style(bview->buffer()->params.textclass,
4008 layout).endlabelstring();
4009 font = GetFont(bview->buffer(), row_ptr->par(), -2);
4010 int const tmpx = is_rtl ?
4011 int(x) - lyxfont::width(tmpstring, font)
4012 : ww - RightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
4013 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
4016 case END_LABEL_NO_LABEL:
4021 /* draw the text in the pixmap */
4023 vpos = row_ptr->pos();
4025 LyXParagraph::size_type main_body =
4026 BeginningOfMainBody(bview->buffer(), row_ptr->par());
4027 if (main_body > 0 &&
4028 (main_body-1 > last ||
4029 !row_ptr->par()->IsLineSeparator(main_body - 1)))
4032 while (vpos <= last) {
4033 pos = vis2log(vpos);
4034 if (main_body > 0 && pos == main_body - 1) {
4035 x += fill_label_hfill
4036 + lyxfont::width(layout.labelsep,
4037 GetFont(bview->buffer(),
4038 row_ptr->par(), -2))
4039 - SingleWidth(bview,
4044 if (row_ptr->par() ->IsHfill(pos)) {
4047 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4049 y_offset + row_ptr->baseline(),
4052 if (HfillExpansion(bview->buffer(),
4054 if (pos >= main_body) {
4056 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4057 int(x + fill_hfill),
4058 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4060 Painter::line_onoffdash);
4064 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4065 int(x + fill_label_hfill),
4066 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4068 Painter::line_onoffdash);
4070 x += fill_label_hfill;
4073 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4075 y_offset + row_ptr->baseline(),
4080 } else if (row_ptr->par()->IsSeparator(pos)) {
4081 x += SingleWidth(bview,
4082 row_ptr->par(), pos);
4083 if (pos >= main_body)
4084 x += fill_separator;
4087 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
4092 int LyXText::DefaultHeight() const
4094 LyXFont font(LyXFont::ALL_SANE);
4095 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
4099 /* returns the column near the specified x-coordinate of the row
4100 * x is set to the real beginning of this column */
4101 int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
4102 bool & boundary) const
4105 float fill_separator, fill_hfill, fill_label_hfill;
4107 PrepareToPrint(bview, row, tmpx, fill_separator,
4108 fill_hfill, fill_label_hfill);
4110 LyXParagraph::size_type vc = row->pos();
4111 LyXParagraph::size_type last = RowLastPrintable(row);
4112 LyXParagraph::size_type c = 0;
4113 LyXLayout const & layout =
4114 textclasslist.Style(bview->buffer()->params.textclass,
4115 row->par()->GetLayout());
4116 bool left_side = false;
4118 LyXParagraph::size_type
4119 main_body = BeginningOfMainBody(bview->buffer(), row->par());
4120 float last_tmpx = tmpx;
4122 if (main_body > 0 &&
4123 (main_body-1 > last ||
4124 !row->par()->IsLineSeparator(main_body - 1)))
4127 while (vc <= last && tmpx <= x) {
4130 if (main_body > 0 && c == main_body-1) {
4131 tmpx += fill_label_hfill +
4132 lyxfont::width(layout.labelsep,
4133 GetFont(bview->buffer(), row->par(), -2));
4134 if (row->par()->IsLineSeparator(main_body - 1))
4135 tmpx -= SingleWidth(bview, row->par(), main_body-1);
4138 if (HfillExpansion(bview->buffer(), row, c)) {
4139 x += SingleWidth(bview, row->par(), c);
4143 tmpx += fill_label_hfill;
4145 else if (row->par()->IsSeparator(c)) {
4146 tmpx += SingleWidth(bview, row->par(), c);
4148 tmpx+= fill_separator;
4150 tmpx += SingleWidth(bview, row->par(), c);
4154 if ((tmpx + last_tmpx) / 2 > x) {
4159 if (vc > last + 1) // This shouldn't happen.
4163 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
4164 // some speedup if rtl_support=false
4165 && (!row->next() || row->next()->par() != row->par());
4166 bool const rtl = (lastrow)
4167 ? row->par()->isRightToLeftPar(bview->buffer()->params)
4168 : false; // If lastrow is false, we don't need to compute
4169 // the value of rtl.
4171 if (row->pos() > last) // Row is empty?
4174 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
4175 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
4177 else if (vc == row->pos()) {
4179 if (bidi_level(c) % 2 == 1)
4182 c = vis2log(vc - 1);
4183 bool const rtl = (bidi_level(c) % 2 == 1);
4184 if (left_side == rtl) {
4186 boundary = IsBoundary(bview->buffer(), row->par(), c);
4190 if (row->pos() <= last && c > last
4191 && row->par()->IsNewline(last)) {
4192 if (bidi_level(last) % 2 == 0)
4193 tmpx -= SingleWidth(bview, row->par(), last);
4195 tmpx += SingleWidth(bview, row->par(), last);
4206 /* turn the selection into a new environment. If there is no selection,
4207 * create an empty environment */
4208 void LyXText::InsertFootnoteEnvironment(BufferView * bview,
4209 LyXParagraph::footnote_kind kind)
4211 /* no footnoteenvironment in a footnoteenvironment */
4212 if (cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4213 WriteAlert(_("Impossible operation"),
4214 _("You can't insert a float in a float!"),
4219 /* no marginpars in minipages */
4220 if (kind == LyXParagraph::MARGIN
4221 && cursor.par()->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE) {
4222 WriteAlert(_("Impossible operation"),
4223 _("You can't insert a marginpar in a minipage!"),
4228 /* this doesnt make sense, if there is no selection */
4229 bool dummy_selection = false;
4231 sel_start_cursor = cursor; /* dummy selection */
4232 sel_end_cursor = cursor;
4233 dummy_selection = true;
4236 LyXParagraph * tmppar;
4238 /* a test to make sure there is not already a footnote
4239 * in the selection. */
4241 tmppar = sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
4243 while (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos()) &&
4244 tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
4245 tmppar = tmppar->next_;
4247 if (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())
4248 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4249 WriteAlert(_("Impossible operation"),
4250 _("Float would include float!"),
4255 /* ok we have a selection. This is always between sel_start_cursor
4256 * and sel_end cursor */
4258 SetUndo(bview->buffer(), Undo::FINISH,
4259 sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous_,
4260 sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next_);
4262 if (sel_end_cursor.pos() > 0
4263 && sel_end_cursor.par()->IsLineSeparator(sel_end_cursor.pos() - 1))
4264 sel_end_cursor.pos(sel_end_cursor.pos() - 1);
4265 /* please break before a space at the end */
4266 if (sel_start_cursor.par() == sel_end_cursor.par()
4267 && sel_start_cursor.pos() > sel_end_cursor.pos())
4268 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4270 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, sel_end_cursor.pos());
4272 sel_end_cursor.par(sel_end_cursor.par()->next());
4273 sel_end_cursor.pos(0);
4275 // don't forget to insert a dummy layout paragraph if necessary
4276 if (sel_start_cursor.par()->GetLayout() != sel_end_cursor.par()->layout){
4277 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, 0);
4278 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4279 sel_end_cursor.par(sel_end_cursor.par()->next_);
4282 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4284 cursor = sel_end_cursor;
4286 /* please break behind a space, if there is one. The space should
4288 if (sel_start_cursor.pos() > 0
4289 && sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos() - 1))
4290 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4291 if (sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos())) {
4292 sel_start_cursor.par()->Erase(sel_start_cursor.pos());
4295 sel_start_cursor.par()->BreakParagraphConservative(bview->buffer()->params,
4296 sel_start_cursor.pos());
4297 tmppar = sel_start_cursor.par()->next();
4299 if (dummy_selection) {
4301 if (kind == LyXParagraph::TAB
4302 || kind == LyXParagraph::FIG
4303 || kind == LyXParagraph::WIDE_TAB
4304 || kind == LyXParagraph::WIDE_FIG
4305 || kind == LyXParagraph::ALGORITHM) {
4306 pair<bool, LyXTextClass::size_type> lres =
4307 textclasslist.NumberOfLayout(bview->buffer()->params.textclass,
4309 LyXTextClass::size_type lay;
4315 lay = 0; // use default layout "Standard" (0)
4317 tmppar->SetLayout(bview->buffer()->params, lay);
4320 if (sel_start_cursor.pos() > 0) {
4321 /* the footnote-environment should begin with a standard layout.
4322 * Imagine you insert a footnote within an enumeration, you
4323 * certainly do not want an enumerated footnote! */
4326 /* this is a exception the user would sometimes expect, I hope */
4327 sel_start_cursor.par()->Clear();
4331 while (tmppar != sel_end_cursor.par()) {
4332 tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4333 tmppar->footnotekind = kind;
4334 tmppar = tmppar->next();
4337 RedoParagraphs(bview, sel_start_cursor, sel_end_cursor.par()->next());
4339 SetCursor(bview, sel_start_cursor.par()->next(), 0);
4341 ClearSelection(bview);
4346 // returns pointer to a specified row
4347 Row * LyXText::GetRow(LyXParagraph * par,
4348 LyXParagraph::size_type pos, int & y) const
4353 Row * tmprow = firstrow;
4356 // find the first row of the specified paragraph
4357 while (tmprow->next() && tmprow->par() != par) {
4358 y += tmprow->height();
4359 tmprow = tmprow->next();
4362 // now find the wanted row
4363 while (tmprow->pos() < pos
4365 && tmprow->next()->par() == par
4366 && tmprow->next()->pos() <= pos) {
4367 y += tmprow->height();
4368 tmprow = tmprow->next();