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 unsigned char LyXText::TransformChar(unsigned char c, LyXParagraph * par,
58 LyXParagraph::size_type pos) const
60 if (!Encodings::is_arabic(c))
61 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
62 return c + (0xb0 - '0');
66 unsigned char const prev_char = pos > 0 ? par->GetChar(pos-1) : ' ';
67 unsigned char next_char = ' ';
68 for (LyXParagraph::size_type i = pos+1; i < par->Last(); ++i)
69 if (!Encodings::IsComposeChar_arabic(par->GetChar(i))) {
70 next_char = par->GetChar(i);
74 if (Encodings::is_arabic(next_char)) {
75 if (Encodings::is_arabic(prev_char))
76 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
78 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
80 if (Encodings::is_arabic(prev_char))
81 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
83 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
87 // This is the comments that some of the warnings below refers to.
88 // There are some issues in this file and I don't think they are
89 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
90 // this is a problem that has been here almost from day one and that a
91 // larger userbase with differenct access patters triggers the bad
92 // behaviour. (segfaults.) What I think happen is: In several places
93 // we store the paragraph in the current cursor and then moves the
94 // cursor. This movement of the cursor will delete paragraph at the
95 // old position if it is now empty. This will make the temporary
96 // pointer to the old cursor paragraph invalid and dangerous to use.
97 // And is some cases this will trigger a segfault. I have marked some
98 // of the cases where this happens with a warning, but I am sure there
99 // are others in this file and in text2.C. There is also a note in
100 // Delete() that you should read. In Delete I store the paragraph->id
101 // instead of a pointer to the paragraph. I am pretty sure this faulty
102 // use of temporary pointers to paragraphs that might have gotten
103 // invalidated (through a cursor movement) before they are used, are
104 // the cause of the strange crashes we get reported often.
106 // It is very tiresom to change this code, especially when it is as
107 // hard to read as it is. Help to fix all the cases where this is done
108 // would be greately appreciated.
112 int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
113 LyXParagraph::size_type pos) const
115 char const c = par->GetChar(pos);
116 return SingleWidth(bview, par, pos, c);
120 int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
121 LyXParagraph::size_type pos, char c) const
123 LyXFont font = GetFont(bview->buffer(), par, pos);
125 // The most common case is handled first (Asger)
126 if (IsPrintable(c)) {
127 if (font.language()->RightToLeft()) {
128 if (font.language()->lang() == "arabic" &&
129 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
130 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
131 if (Encodings::IsComposeChar_arabic(c))
134 c = TransformChar(c, par, pos);
135 else if (font.language()->lang() == "hebrew" &&
136 Encodings::IsComposeChar_hebrew(c))
139 return lyxfont::width(c, font);
141 } else if (IsHfillChar(c)) {
142 return 3; /* Because of the representation
143 * as vertical lines */
145 } else if (c == LyXParagraph::META_FOOTNOTE ||
146 c == LyXParagraph::META_MARGIN ||
147 c == LyXParagraph::META_FIG ||
148 c == LyXParagraph::META_TAB ||
149 c == LyXParagraph::META_WIDE_FIG ||
150 c == LyXParagraph::META_WIDE_TAB ||
151 c == LyXParagraph::META_ALGORITHM) {
154 case LyXParagraph::META_MARGIN:
157 case LyXParagraph::META_FIG:
160 case LyXParagraph::META_TAB:
163 case LyXParagraph::META_ALGORITHM:
166 case LyXParagraph::META_WIDE_FIG:
169 case LyXParagraph::META_WIDE_TAB:
172 case LyXParagraph::META_FOOTNOTE:
178 return lyxfont::width(fs, font);
180 } else if (c == LyXParagraph::META_INSET) {
181 Inset * tmpinset = par->GetInset(pos);
183 tmpinset->update(bview, font);
184 return tmpinset->width(bview, font);
188 } else if (IsSeparatorChar(c))
190 else if (IsNewlineChar(c))
192 return lyxfont::width(c, font);
196 // Returns the paragraph position of the last character in the specified row
197 LyXParagraph::size_type LyXText::RowLast(Row const * row) const
199 if (row->next() == 0)
200 return row->par()->Last() - 1;
201 else if (row->next()->par() != row->par())
202 return row->par()->Last() - 1;
204 return row->next()->pos() - 1;
208 LyXParagraph::size_type LyXText::RowLastPrintable(Row const * row) const
210 LyXParagraph::size_type const last = RowLast(row);
211 if (last >= row->pos()
213 && row->next()->par() == row->par()
214 && row->par()->IsSeparator(last))
221 void LyXText::ComputeBidiTables(Buffer const * buf, Row * row) const
223 bidi_same_direction = true;
224 if (!lyxrc.rtl_support) {
229 bidi_start = row->pos();
230 bidi_end = RowLastPrintable(row);
232 if (bidi_start > bidi_end) {
237 if (bidi_end + 2 - bidi_start >
238 static_cast<LyXParagraph::size_type>(log2vis_list.size())) {
239 LyXParagraph::size_type new_size =
240 (bidi_end + 2 - bidi_start < 500) ?
241 500 : 2 * (bidi_end + 2 - bidi_start);
242 log2vis_list.resize(new_size);
243 vis2log_list.resize(new_size);
244 bidi_levels.resize(new_size);
247 vis2log_list[bidi_end + 1 - bidi_start] = -1;
248 log2vis_list[bidi_end + 1 - bidi_start] = -1;
250 LyXParagraph::size_type stack[2];
252 row->par()->getParLanguage(buf->params)->RightToLeft();
256 LyXParagraph::size_type const main_body =
257 BeginningOfMainBody(buf, row->par());
259 for (LyXParagraph::size_type lpos = bidi_start;
260 lpos <= bidi_end; ++lpos) {
261 bool is_space = row->par()->IsLineSeparator(lpos);
262 LyXParagraph::size_type const pos =
263 (is_space && lpos + 1 <= bidi_end &&
264 !row->par()->IsLineSeparator(lpos + 1) &&
265 !row->par()->IsNewline(lpos + 1))
267 LyXFont font = row->par()->GetFontSettings(buf->params, pos);
268 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
269 font.number() == LyXFont::ON &&
270 row->par()->GetFontSettings(buf->params, lpos-1).number()
272 font = row->par()->GetFontSettings(buf->params, lpos);
277 bool new_rtl = font.isVisibleRightToLeft();
278 bool new_rtl0 = font.isRightToLeft();
281 if (lpos == main_body - 1
282 && row->pos() < main_body - 1
284 new_level = (rtl_par) ? 1 : 0;
285 new_rtl = new_rtl0 = rtl_par;
287 new_level = (new_rtl) ? 1 : 2;
289 new_level = (rtl_par) ? 2 : 0;
291 if (is_space && new_level >= level) {
297 int new_level2 = new_level;
299 if (level == new_level && rtl0 != new_rtl0) {
301 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
302 } else if (level < new_level) {
303 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
304 if (new_level > rtl_par)
305 bidi_same_direction = false;
307 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
310 bidi_levels[lpos - bidi_start] = new_level;
312 while (level > new_level2) {
313 LyXParagraph::size_type old_lpos =
315 int delta = lpos - old_lpos - 1;
318 log2vis_list[lpos - bidi_start] += delta;
319 log2vis_list[old_lpos - bidi_start] += delta;
321 while (level < new_level)
322 stack[level++] = lpos;
326 LyXParagraph::size_type const old_lpos = stack[--level];
327 int delta = bidi_end - old_lpos;
330 log2vis_list[old_lpos - bidi_start] += delta;
333 LyXParagraph::size_type vpos = bidi_start - 1;
334 for (LyXParagraph::size_type lpos = bidi_start;
335 lpos <= bidi_end; ++lpos) {
336 vpos += log2vis_list[lpos - bidi_start];
337 vis2log_list[vpos - bidi_start] = lpos;
338 log2vis_list[lpos - bidi_start] = vpos;
343 // This method requires a previous call to ComputeBidiTables()
344 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
345 LyXParagraph::size_type pos) const
347 if (!lyxrc.rtl_support)
348 return false; // This is just for speedup
350 if (!bidi_InRange(pos - 1)) {
351 lyxerr << "LyXText::IsBoundary This shouldn't happen\n";
355 bool const rtl = bidi_level(pos - 1) % 2;
356 bool rtl2 = bidi_InRange(pos)
357 ? bidi_level(pos) % 2
358 : par->isRightToLeftPar(buf->params);
363 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
364 LyXParagraph::size_type pos,
365 LyXFont const & font) const
367 if (!lyxrc.rtl_support)
368 return false; // This is just for speedup
370 bool const rtl = font.isVisibleRightToLeft();
371 bool rtl2 = bidi_InRange(pos)
372 ? bidi_level(pos) % 2
373 : par->isRightToLeftPar(buf->params);
378 void LyXText::draw(BufferView * bview, Row const * row,
379 LyXParagraph::size_type & vpos,
380 int offset, float & x, bool cleared)
382 Painter & pain = bview->painter();
384 LyXParagraph::size_type pos = vis2log(vpos);
385 char c = row->par()->GetChar(pos);
388 if (IsNewlineChar(c)) {
390 // Draw end-of-line marker
391 LyXFont const font = GetFont(bview->buffer(), row->par(), pos);
392 int const wid = lyxfont::width('n', font);
393 int const asc = lyxfont::maxAscent(font);
394 int const y = offset + row->baseline();
397 if (bidi_level(pos) % 2 == 0) {
398 xp[0] = int(x + wid * 0.375);
399 yp[0] = int(y - 0.875 * asc * 0.75);
402 yp[1] = int(y - 0.500 * asc * 0.75);
404 xp[2] = int(x + wid * 0.375);
405 yp[2] = int(y - 0.125 * asc * 0.75);
407 pain.lines(xp, yp, 3, LColor::eolmarker);
410 yp[0] = int(y - 0.500 * asc * 0.75);
412 xp[1] = int(x + wid);
413 yp[1] = int(y - 0.500 * asc * 0.75);
415 xp[2] = int(x + wid);
416 yp[2] = int(y - asc * 0.75);
418 pain.lines(xp, yp, 3, LColor::eolmarker);
420 xp[0] = int(x + wid * 0.625);
421 yp[0] = int(y - 0.875 * asc * 0.75);
423 xp[1] = int(x + wid);
424 yp[1] = int(y - 0.500 * asc * 0.75);
426 xp[2] = int(x + wid * 0.625);
427 yp[2] = int(y - 0.125 * asc * 0.75);
429 pain.lines(xp, yp, 3, LColor::eolmarker);
431 xp[0] = int(x + wid);
432 yp[0] = int(y - 0.500 * asc * 0.75);
435 yp[1] = int(y - 0.500 * asc * 0.75);
438 yp[2] = int(y - asc * 0.75);
440 pain.lines(xp, yp, 3, LColor::eolmarker);
446 LyXFont font = GetFont(bview->buffer(), row->par(), pos);
447 LyXFont font2 = font;
449 if (c == LyXParagraph::META_FOOTNOTE
450 || c == LyXParagraph::META_MARGIN
451 || c == LyXParagraph::META_FIG
452 || c == LyXParagraph::META_TAB
453 || c == LyXParagraph::META_WIDE_FIG
454 || c == LyXParagraph::META_WIDE_TAB
455 || c == LyXParagraph::META_ALGORITHM) {
458 case LyXParagraph::META_MARGIN:
461 case LyXParagraph::META_FIG:
464 case LyXParagraph::META_TAB:
467 case LyXParagraph::META_ALGORITHM:
470 case LyXParagraph::META_WIDE_FIG:
473 case LyXParagraph::META_WIDE_TAB:
476 case LyXParagraph::META_FOOTNOTE:
483 // calculate the position of the footnotemark
484 int const y = (row->baseline() - lyxfont::maxAscent(font2)
485 + lyxfont::maxAscent(font));
487 font.setColor(LColor::footnote);
489 // draw it and set new x position
491 pain.text(int(x), offset + y, fs, font);
492 x += lyxfont::width(fs, font);
493 pain.line(int(tmpx), offset + row->baseline(),
494 int(x), offset + row->baseline(),
501 if (c == LyXParagraph::META_INSET) {
502 Inset * tmpinset = row->par()->GetInset(pos);
504 // tmpinset->update(bview, font, false);
505 tmpinset->draw(bview, font, offset+row->baseline(), x,
507 if (status == CHANGED_IN_DRAW) {
508 UpdateInset(bview, tmpinset);
509 status = CHANGED_IN_DRAW;
514 if (lyxrc.mark_foreign_language &&
515 font.language() != bview->buffer()->params.language) {
516 int const y = offset + row->height() - 1;
517 pain.line(int(tmpx), y, int(x), y,
524 /* usual characters, no insets */
526 // Collect character that we can draw in one command
528 // This is dirty, but fast. Notice that it will never be too small.
529 // For the record, I'll note that Microsoft Word has a limit
530 // of 768 here. We have none :-) (Asger)
531 // Ok. I am the first to admit that the use of std::string will be
532 // a tiny bit slower than using a POD char array. However, I claim
533 // that this slowdown is so small that it is close to inperceptive.
534 // So IMHO we should go with the easier and clearer implementation.
535 // And even if 1024 is a large number here it might overflow, string
536 // will only overflow if the machine is out of memory...
537 static string textstring;
541 LyXParagraph::size_type const last = RowLastPrintable(row);
543 if (font.language()->lang() == "hebrew") {
544 if (Encodings::IsComposeChar_hebrew(c)) {
545 int const width = lyxfont::width(c, font2);
547 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
548 c = row->par()->GetChar(i);
549 if (!Encodings::IsComposeChar_hebrew(c)) {
550 if (IsPrintableNonspace(c)) {
555 dx = (c == 'ø' || c == 'ã') // dalet / resh
556 ? width2 - width : (width2 - width) / 2;
562 pain.text(int(x) + dx, offset + row->baseline(),
565 while (vpos <= last &&
566 (pos = vis2log(vpos)) >= 0
567 && IsPrintableNonspace(c = row->par()->GetChar(pos))
568 && !Encodings::IsComposeChar_hebrew(c)
569 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
573 // Draw text and set the new x position
574 pain.text(int(x), offset + row->baseline(),
576 x += lyxfont::width(textstring, font);
578 } else if (font.language()->lang() == "arabic" &&
579 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
580 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
581 if (Encodings::IsComposeChar_arabic(c)) {
582 c = TransformChar(c, row->par(), pos);
584 int const width = lyxfont::width(c, font2);
586 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
587 c = row->par()->GetChar(i);
588 if (!Encodings::IsComposeChar_arabic(c)) {
589 if (IsPrintableNonspace(c)) {
594 dx = (width2 - width) / 2;
600 pain.text(int(x) + dx, offset + row->baseline(),
603 textstring = TransformChar(c, row->par(), pos);
604 while (vpos <= last &&
605 (pos = vis2log(vpos)) >= 0
606 && IsPrintableNonspace(c = row->par()->GetChar(pos))
607 && !Encodings::IsComposeChar_arabic(c)
608 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
609 c = TransformChar(c, row->par(), pos);
613 // Draw text and set the new x position
614 pain.text(int(x), offset + row->baseline(),
616 x += lyxfont::width(textstring, font);
619 while (vpos <= last &&
620 (pos = vis2log(vpos)) >= 0
621 && IsPrintableNonspace(c = row->par()->GetChar(pos))
622 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
626 // Draw text and set the new x position
627 pain.text(int(x), offset + row->baseline(), textstring, font);
628 x += lyxfont::width(textstring, font);
631 if (lyxrc.mark_foreign_language &&
632 font.language() != bview->buffer()->params.language) {
633 int const y = offset + row->height() - 1;
634 pain.line(int(tmpx), y, int(x), y,
638 // If we want ulem.sty support, drawing
639 // routines should go here. (Asger)
640 // Why shouldn't LyXFont::drawText handle it internally?
644 // Returns the left beginning of the text.
645 // This information cannot be taken from the layouts-objekt, because in
646 // LaTeX the beginning of the text fits in some cases (for example sections)
647 // exactly the label-width.
648 int LyXText::LeftMargin(BufferView * bview, Row const * row) const
650 LyXLayout const & layout =
651 textclasslist.Style(bview->buffer()->params.textclass,
652 row->par()->GetLayout());
654 string parindent = layout.parindent;
656 int x = LYX_PAPER_MARGIN;
658 x += lyxfont::signedWidth(textclasslist
659 .TextClass(bview->buffer()->params.textclass)
662 .TextClass(bview->buffer()->params.textclass)
665 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
666 LyXFont font(LyXFont::ALL_SANE);
667 font.setSize(LyXFont::SIZE_SMALL);
668 x += lyxfont::width("Mwide-figM", font)
669 + LYX_PAPER_MARGIN/2;
672 // this is the way, LyX handles the LaTeX-Environments.
673 // I have had this idea very late, so it seems to be a
674 // later added hack and this is true
675 if (!row->par()->GetDepth()) {
676 if (!row->par()->GetLayout()) {
677 // find the previous same level paragraph
679 if (row->par()->FirstPhysicalPar()->Previous()) {
681 if (row->par()->Previous()) {
683 LyXParagraph * newpar = row->par()
684 ->DepthHook(row->par()->GetDepth());
686 textclasslist.Style(bview->buffer()->params.textclass,
693 // find the next level paragraph
695 LyXParagraph * newpar =
696 row->par()->DepthHook(row->par()->GetDepth()-1);
698 // make a corresponding row. Needed to call LeftMargin()
700 // check wether it is a sufficent paragraph
703 && newpar->footnoteflag == row->par()->footnoteflag
706 .Style(bview->buffer()->params.textclass,
707 newpar->GetLayout()).isEnvironment()) {
709 dummyrow.par(newpar);
710 dummyrow.pos(newpar->Last());
711 x = LeftMargin(bview, &dummyrow);
713 // this is no longer an error, because this function
714 // is used to clear impossible depths after changing
715 // a layout. Since there is always a redo,
716 // LeftMargin() is always called
718 row->par()->FirstPhysicalPar()->depth = 0;
720 row->par()->depth = 0;
724 if (newpar && !row->par()->GetLayout()) {
726 if (newpar->FirstPhysicalPar()->noindent)
728 if (newpar->noindent)
732 parindent = textclasslist
733 .Style(bview->buffer()->params.textclass,
734 newpar->GetLayout()).parindent;
739 LyXFont const labelfont = GetFont(bview->buffer(), row->par(), -2);
740 switch (layout.margintype) {
742 if (!layout.leftmargin.empty()) {
743 x += lyxfont::signedWidth(layout.leftmargin,
745 .TextClass(bview->buffer()->params.
749 if (!row->par()->GetLabelstring().empty()) {
750 x += lyxfont::signedWidth(layout.labelindent,
752 x += lyxfont::width(row->par()->GetLabelstring(),
754 x += lyxfont::width(layout.labelsep, labelfont);
758 x += lyxfont::signedWidth(layout.labelindent, labelfont);
759 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
760 if (!row->par()->GetLabelWidthString().empty()) {
761 x += lyxfont::width(row->par()->GetLabelWidthString(),
763 x += lyxfont::width(layout.labelsep, labelfont);
768 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
769 / (row->par()->GetDepth() + 4);
771 case MARGIN_FIRST_DYNAMIC:
772 if (layout.labeltype == LABEL_MANUAL) {
773 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
774 x += lyxfont::signedWidth(layout.leftmargin,
777 x += lyxfont::signedWidth(layout.labelindent,
780 } else if (row->pos()
781 // Special case to fix problems with
783 || (layout.labeltype == LABEL_STATIC
784 && layout.latextype == LATEX_ENVIRONMENT
785 && ! row->par()->IsFirstInSequence())) {
786 x += lyxfont::signedWidth(layout.leftmargin,
788 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
789 && layout.labeltype != LABEL_BIBLIO
790 && layout.labeltype !=
791 LABEL_CENTERED_TOP_ENVIRONMENT) {
792 x += lyxfont::signedWidth(layout.labelindent,
794 x += lyxfont::width(layout.labelsep, labelfont);
795 x += lyxfont::width(row->par()->GetLabelstring(),
800 case MARGIN_RIGHT_ADDRESS_BOX:
802 // ok, a terrible hack. The left margin depends on the widest
803 // row in this paragraph. Do not care about footnotes, they
804 // are *NOT* allowed in the LaTeX realisation of this layout.
806 // find the first row of this paragraph
807 Row const * tmprow = row;
808 while (tmprow->previous()
809 && tmprow->previous()->par() == row->par())
810 tmprow = tmprow->previous();
812 int minfill = tmprow->fill();
813 while (tmprow->next() && tmprow->next()->par() == row->par()) {
814 tmprow = tmprow->next();
815 if (tmprow->fill() < minfill)
816 minfill = tmprow->fill();
819 x += lyxfont::signedWidth(layout.leftmargin,
821 .TextClass(bview->buffer()->params.textclass)
827 if ((workWidth(bview) > 0) &&
828 (row->par()->pextra_type == LyXParagraph::PEXTRA_INDENT)) {
829 if (!row->par()->pextra_widthp.empty()) {
830 x += workWidth(bview) *
831 lyx::atoi(row->par()->pextra_widthp) / 100;
832 } else if (!row->par()->pextra_width.empty()) {
833 int xx = VSpace(row->par()->pextra_width).inPixels(bview);
834 if (xx > workWidth(bview))
835 xx = workWidth(bview) * 80 / 100;
837 } else { // should not happen
838 LyXFont font(LyXFont::ALL_SANE);
839 x += lyxfont::width("XXXXXX", font);
843 int align; // wrong type
845 if (row->par()->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
846 align = layout.align;
848 align = row->par()->FirstPhysicalPar()->align;
850 if (row->par()->align == LYX_ALIGN_LAYOUT)
851 align = layout.align;
853 align = row->par()->align;
855 // set the correct parindent
856 if (row->pos() == 0) {
857 if ((layout.labeltype == LABEL_NO_LABEL
858 || layout.labeltype == LABEL_TOP_ENVIRONMENT
859 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
860 || (layout.labeltype == LABEL_STATIC
861 && layout.latextype == LATEX_ENVIRONMENT
862 && ! row->par()->IsFirstInSequence()))
864 && row->par() == row->par()->FirstPhysicalPar()
866 && align == LYX_ALIGN_BLOCK
867 && !row->par()->noindent
868 && (row->par()->layout ||
869 bview->buffer()->params.paragraph_separation ==
870 BufferParams::PARSEP_INDENT))
871 x += lyxfont::signedWidth(parindent,
873 .TextClass(bview->buffer()->params
876 else if (layout.labeltype == LABEL_BIBLIO) {
877 // ale970405 Right width for bibitems
878 x += bibitemMaxWidth(bview,textclasslist
879 .TextClass(bview->buffer()->params
888 int LyXText::RightMargin(Buffer const * buf, Row const * row) const
890 LyXLayout const & layout =
891 textclasslist.Style(buf->params.textclass,
892 row->par()->GetLayout());
894 int x = LYX_PAPER_MARGIN
895 + lyxfont::signedWidth(textclasslist
896 .TextClass(buf->params.textclass)
899 .TextClass(buf->params.textclass)
903 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
904 x += LYX_PAPER_MARGIN / 2;
907 // this is the way, LyX handles the LaTeX-Environments.
908 // I have had this idea very late, so it seems to be a
909 // later added hack and this is true
910 if (row->par()->GetDepth()) {
911 // find the next level paragraph
913 LyXParagraph * newpar = row->par();
917 newpar = newpar->FirstPhysicalPar()->Previous();
919 newpar = newpar->FirstPhysicalPar();
920 } while (newpar && newpar->GetDepth() >= row->par()->GetDepth()
921 && newpar->footnoteflag == row->par()->footnoteflag);
924 newpar = newpar->Previous();
926 && newpar->GetDepth() >= row->par()->GetDepth());
929 // make a corresponding row. Needed to call LeftMargin()
931 // check wether it is a sufficent paragraph
934 && newpar->footnoteflag == row->par()->footnoteflag
936 && textclasslist.Style(buf->params.textclass,
940 dummyrow.par(newpar);
942 x = RightMargin(buf, &dummyrow);
944 // this is no longer an error, because this function
945 // is used to clear impossible depths after changing
946 // a layout. Since there is always a redo,
947 // LeftMargin() is always called
949 row->par()->FirstPhysicalPar()->depth = 0;
951 row->par()->depth = 0;
956 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
957 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
958 .TextClass(buf->params.textclass)
959 .defaultfont()) * 4 / (row->par()->GetDepth()
965 int LyXText::LabelEnd(BufferView * bview, Row const * row) const
967 if (textclasslist.Style(bview->buffer()->params.textclass,
968 row->par()->GetLayout()).margintype
972 tmprow.pos(row->par()->Last());
973 return LeftMargin(bview, &tmprow); /* just the beginning
976 return 0; /* LabelEnd is only needed, if the
977 layout fills a flushleft
982 // get the next breakpoint in a given paragraph
983 LyXParagraph::size_type
984 LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
986 LyXParagraph * par = row->par();
991 LyXParagraph::size_type const pos = row->pos();
994 // position of the last possible breakpoint
995 // -1 isn't a suitable value, but a flag
996 LyXParagraph::size_type last_separator = -1;
997 width -= RightMargin(bview->buffer(), row);
999 LyXParagraph::size_type const main_body =
1000 BeginningOfMainBody(bview->buffer(), par);
1001 LyXLayout const & layout =
1002 textclasslist.Style(bview->buffer()->params.textclass, par->GetLayout());
1003 LyXParagraph::size_type i = pos;
1005 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1006 /* special code for right address boxes, only newlines count */
1007 while (i < par->Last()) {
1008 if (par->IsNewline(i)) {
1010 i = par->Last() - 1; // this means break
1012 } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
1013 par->GetInset(i) && par->GetInset(i)->display()){
1014 par->GetInset(i)->display(false);
1019 // Last position is an invariant
1020 LyXParagraph::size_type const last =
1022 // this is the usual handling
1023 int x = LeftMargin(bview, row);
1024 bool doitonetime = true;
1025 while (doitonetime || ((x < width) && (i < last))) {
1026 doitonetime = false;
1027 char const c = par->GetChar(i);
1028 if (IsNewlineChar(c)) {
1030 x = width; // this means break
1031 } else if (c == LyXParagraph::META_INSET &&
1034 // check wether a Display() inset is
1035 // valid here. if not, change it to
1037 if (par->GetInset(i)->display() &&
1038 (layout.isCommand() ||
1039 (layout.labeltype == LABEL_MANUAL
1040 && i < BeginningOfMainBody(bview->buffer(), par)))) {
1041 // display istn't allowd
1042 par->GetInset(i)->display(false);
1043 x += SingleWidth(bview, par, i, c);
1044 } else if (par->GetInset(i)->display() ||
1045 par->GetInset(i)->needFullRow()) {
1046 // So break the line here
1050 if (IsLineSeparatorChar(par->GetChar(i+1)))
1053 last_separator = last; // to avoid extra rows
1055 last_separator = i - 1;
1056 x = width; // this means break
1062 x += SingleWidth(bview, par, i, c);
1066 if (IsLineSeparatorChar(c))
1068 x += SingleWidth(bview, par, i, c);
1071 if (i == main_body) {
1072 x += lyxfont::width(layout.labelsep,
1073 GetFont(bview->buffer(), par, -2));
1074 if (par->IsLineSeparator(i - 1))
1075 x-= SingleWidth(bview, par, i - 1);
1076 int left_margin = LabelEnd(bview, row);
1077 if (x < left_margin)
1081 // end of paragraph is always a suitable separator
1082 if (i == last && x < width)
1086 // well, if last_separator is still 0, the line isn't breakable.
1087 // don't care and cut simply at the end
1088 if (last_separator < 0) {
1092 // manual labels cannot be broken in LaTeX, do not care
1093 if (main_body && last_separator < main_body)
1094 last_separator = main_body - 1;
1096 return last_separator;
1100 // returns the minimum space a row needs on the screen in pixel
1101 int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
1103 if (paper_width < 0)
1107 // get the pure distance
1108 LyXParagraph::size_type const last = RowLastPrintable(row);
1110 // special handling of the right address boxes
1111 if (textclasslist.Style(bview->buffer()->params.textclass,
1112 row->par()->GetLayout()).margintype
1113 == MARGIN_RIGHT_ADDRESS_BOX) {
1114 int const tmpfill = row->fill();
1115 row->fill(0); // the minfill in MarginLeft()
1116 w = LeftMargin(bview, row);
1119 w = LeftMargin(bview, row);
1121 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1122 row->par()->GetLayout());
1123 LyXParagraph::size_type const main_body =
1124 BeginningOfMainBody(bview->buffer(), row->par());
1125 LyXParagraph::size_type i = row->pos();
1128 if (main_body > 0 && i == main_body) {
1129 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1130 if (row->par()->IsLineSeparator(i - 1))
1131 w -= SingleWidth(bview, row->par(), i - 1);
1132 int left_margin = LabelEnd(bview, row);
1133 if (w < left_margin)
1136 w += SingleWidth(bview, row->par(), i);
1139 if (main_body > 0 && main_body > last) {
1140 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1141 if (last >= 0 && row->par()->IsLineSeparator(last))
1142 w -= SingleWidth(bview, row->par(), last);
1143 int const left_margin = LabelEnd(bview, row);
1144 if (w < left_margin)
1148 int const fill = paper_width - w - RightMargin(bview->buffer(), row);
1153 // returns the minimum space a manual label needs on the screen in pixel
1154 int LyXText::LabelFill(BufferView * bview, Row const * row) const
1156 LyXParagraph::size_type last = BeginningOfMainBody(bview->buffer(), row->par()) - 1;
1157 // -1 because a label ends either with a space that is in the label,
1158 // or with the beginning of a footnote that is outside the label.
1160 // I don't understand this code in depth, but sometimes "last" is
1161 // less than 0 and this causes a crash. This fix seems to work
1162 // correctly, but I bet the real error is elsewhere. The bug is
1163 // triggered when you have an open footnote in a paragraph
1164 // environment with a manual label. (Asger)
1165 if (last < 0) last = 0;
1167 if (row->par()->IsLineSeparator(last)) /* a sepearator at this end
1174 w += SingleWidth(bview, row->par(), i);
1179 if (!row->par()->labelwidthstring.empty()) {
1180 fill = max(lyxfont::width(row->par()->labelwidthstring,
1181 GetFont(bview->buffer(), row->par(), -2)) - w,
1189 // returns the number of separators in the specified row. The separator
1190 // on the very last column doesnt count
1191 int LyXText::NumberOfSeparators(Buffer const * buf, Row const * row) const
1193 LyXParagraph::size_type const last = RowLast(row);
1194 LyXParagraph::size_type p =
1195 max(row->pos(), BeginningOfMainBody(buf, row->par()));
1197 for (; p < last; ++p) {
1198 if (row->par()->IsSeparator(p)) {
1206 // returns the number of hfills in the specified row. The LyX-Hfill is
1207 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1208 // ignored. This is *MUCH* more usefull than not to ignore!
1209 int LyXText::NumberOfHfills(Buffer const * buf, Row const * row) const
1211 LyXParagraph::size_type const last = RowLast(row);
1212 LyXParagraph::size_type first = row->pos();
1213 if (first) { /* hfill *DO* count at the beginning
1215 while(first <= last && row->par()->IsHfill(first))
1219 first = max(first, BeginningOfMainBody(buf, row->par()));
1221 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1222 if (row->par()->IsHfill(p)) {
1230 // like NumberOfHfills, but only those in the manual label!
1231 int LyXText::NumberOfLabelHfills(Buffer const * buf, Row const * row) const
1233 LyXParagraph::size_type last = RowLast(row);
1234 LyXParagraph::size_type first = row->pos();
1235 if (first) { /* hfill *DO* count at the beginning
1237 while(first < last && row->par()->IsHfill(first))
1241 last = min(last, BeginningOfMainBody(buf, row->par()));
1243 for (LyXParagraph::size_type p = first;
1244 p < last; ++p) { // last, because the end is ignored!
1245 if (row->par()->IsHfill(p)) {
1253 // returns true, if a expansion is needed.
1254 // Rules are given by LaTeX
1255 bool LyXText::HfillExpansion(Buffer const * buf, Row const * row_ptr,
1256 LyXParagraph::size_type pos) const
1258 // by the way, is it a hfill?
1259 if (!row_ptr->par()->IsHfill(pos))
1262 // at the end of a row it does not count
1263 if (pos >= RowLast(row_ptr))
1266 // at the beginning of a row it does not count, if it is not
1267 // the first row of a paragaph
1268 if (!row_ptr->pos())
1271 // in some labels it does not count
1272 if (textclasslist.Style(buf->params.textclass,
1273 row_ptr->par()->GetLayout()).margintype
1275 && pos < BeginningOfMainBody(buf, row_ptr->par()))
1278 // if there is anything between the first char of the row and
1279 // the sepcified position that is not a newline and not a hfill,
1280 // the hfill will count, otherwise not
1281 LyXParagraph::size_type i = row_ptr->pos();
1282 while (i < pos && (row_ptr->par()->IsNewline(i)
1283 || row_ptr->par()->IsHfill(i)))
1290 void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
1292 /* get the maximum ascent and the maximum descent */
1295 float layoutasc = 0;
1296 float layoutdesc = 0;
1299 Inset * tmpinset = 0;
1301 /* this must not happen before the currentrow for clear reasons.
1302 so the trick is just to set the current row onto this row */
1304 GetRow(row_ptr->par(), row_ptr->pos(), unused_y);
1306 /* ok , let us initialize the maxasc and maxdesc value.
1307 * This depends in LaTeX of the font of the last character
1308 * in the paragraph. The hack below is necessary because
1309 * of the possibility of open footnotes */
1311 /* Correction: only the fontsize count. The other properties
1312 are taken from the layoutfont. Nicer on the screen :) */
1314 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
1315 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
1317 LyXParagraph * par = row_ptr->par();
1318 LyXParagraph * firstpar = row_ptr->par();
1321 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1322 firstpar->GetLayout());
1324 LyXFont font = GetFont(bview->buffer(), par, par->Last() - 1);
1325 LyXFont::FONT_SIZE const size = font.size();
1326 font = GetFont(bview->buffer(), par, -1);
1329 LyXFont labelfont = GetFont(bview->buffer(), par, -2);
1331 float spacing_val = 1.0;
1332 if (!row_ptr->par()->spacing.isDefault()) {
1333 spacing_val = row_ptr->par()->spacing.getValue();
1335 spacing_val = bview->buffer()->params.spacing.getValue();
1337 //lyxerr << "spacing_val = " << spacing_val << endl;
1339 int maxasc = int(lyxfont::maxAscent(font) *
1340 layout.spacing.getValue() *
1342 int maxdesc = int(lyxfont::maxDescent(font) *
1343 layout.spacing.getValue() *
1345 int const pos_end = RowLast(row_ptr);
1349 // Check if any insets are larger
1350 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1351 if (row_ptr->par()->GetChar(pos) == LyXParagraph::META_INSET) {
1352 tmpfont = GetFont(bview->buffer(), row_ptr->par(), pos);
1353 tmpinset = row_ptr->par()->GetInset(pos);
1355 tmpinset->update(bview, tmpfont);
1356 asc = tmpinset->ascent(bview, tmpfont);
1357 desc = tmpinset->descent(bview, tmpfont);
1358 maxwidth += tmpinset->width(bview, tmpfont);
1359 maxasc = max(maxasc, asc);
1360 maxdesc = max(maxdesc, desc);
1363 maxwidth += SingleWidth(bview, row_ptr->par(), pos);
1367 // Check if any custom fonts are larger (Asger)
1368 // This is not completely correct, but we can live with the small,
1369 // cosmetic error for now.
1370 LyXFont::FONT_SIZE const maxsize =
1371 row_ptr->par()->HighestFontInRange(row_ptr->pos(),
1373 if (maxsize > font.size()) {
1374 font.setSize(maxsize);
1376 asc = lyxfont::maxAscent(font);
1377 desc = lyxfont::maxDescent(font);
1384 // This is nicer with box insets:
1388 row_ptr->ascent_of_text(maxasc);
1390 /* is it a top line? */
1391 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1393 /* some parksips VERY EASY IMPLEMENTATION */
1394 if (bview->buffer()->params.paragraph_separation ==
1395 BufferParams::PARSEP_SKIP) {
1396 if (layout.isParagraph()
1397 && firstpar->GetDepth() == 0
1398 && firstpar->Previous())
1399 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1400 else if (firstpar->Previous()
1401 && textclasslist.Style(bview->buffer()->params.textclass,
1402 firstpar->Previous()->GetLayout()).isParagraph()
1403 && firstpar->Previous()->GetDepth() == 0)
1404 // is it right to use defskip here too? (AS)
1405 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1408 /* the paper margins */
1409 if (!row_ptr->par()->previous && bv_owner)
1410 maxasc += LYX_PAPER_MARGIN;
1412 /* add the vertical spaces, that the user added */
1413 if (firstpar->added_space_top.kind() != VSpace::NONE)
1414 maxasc += int(firstpar->added_space_top.inPixels(bview));
1416 /* do not forget the DTP-lines!
1417 * there height depends on the font of the nearest character */
1418 if (firstpar->line_top)
1419 maxasc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1422 /* and now the pagebreaks */
1423 if (firstpar->pagebreak_top)
1424 maxasc += 3 * DefaultHeight();
1426 /* this is special code for the chapter, since the label of this
1427 * layout is printed in an extra row */
1428 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1429 && bview->buffer()->params.secnumdepth >= 0) {
1430 float spacing_val = 1.0;
1431 if (!row_ptr->par()->spacing.isDefault()) {
1432 spacing_val = row_ptr->par()->spacing.getValue();
1434 spacing_val = bview->buffer()->params.spacing.getValue();
1437 labeladdon = int(lyxfont::maxDescent(labelfont) *
1438 layout.spacing.getValue() *
1440 + int(lyxfont::maxAscent(labelfont) *
1441 layout.spacing.getValue() *
1445 /* special code for the top label */
1446 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1447 || layout.labeltype == LABEL_BIBLIO
1448 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1449 && row_ptr->par()->IsFirstInSequence()
1450 && !row_ptr->par()->GetLabelstring().empty()) {
1451 float spacing_val = 1.0;
1452 if (!row_ptr->par()->spacing.isDefault()) {
1453 spacing_val = row_ptr->par()->spacing.getValue();
1455 spacing_val = bview->buffer()->params.spacing.getValue();
1459 (lyxfont::maxAscent(labelfont) *
1460 layout.spacing.getValue() *
1462 +(lyxfont::maxDescent(labelfont) *
1463 layout.spacing.getValue() *
1465 + layout.topsep * DefaultHeight()
1466 + layout.labelbottomsep * DefaultHeight());
1469 /* and now the layout spaces, for example before and after a section,
1470 * or between the items of a itemize or enumerate environment */
1472 if (!firstpar->pagebreak_top) {
1473 LyXParagraph * prev = row_ptr->par()->Previous();
1475 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth());
1476 if (prev && prev->GetLayout() == firstpar->GetLayout()
1477 && prev->GetDepth() == firstpar->GetDepth()
1478 && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1480 layoutasc = (layout.itemsep * DefaultHeight());
1482 else if (row_ptr->previous()) {
1483 tmptop = layout.topsep;
1485 if (row_ptr->previous()->par()->GetDepth() >= row_ptr->par()->GetDepth())
1486 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1487 row_ptr->previous()->par()->
1488 GetLayout()).bottomsep;
1491 layoutasc = (tmptop * DefaultHeight());
1493 else if (row_ptr->par()->line_top){
1494 tmptop = layout.topsep;
1497 layoutasc = (tmptop * DefaultHeight());
1500 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth()-1);
1502 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1503 prev->GetLayout()).parsep * DefaultHeight());
1506 if (firstpar->Previous()
1507 && firstpar->Previous()->GetDepth() == 0
1508 && firstpar->Previous()->GetLayout() != firstpar->GetLayout()) {
1511 else if (firstpar->Previous()){
1512 maxasc += int(layout.parsep * DefaultHeight());
1518 /* is it a bottom line? */
1521 row_ptr->par()->ParFromPos(RowLast(row_ptr) + 1) == par
1523 row_ptr->par() == par
1525 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1527 /* the paper margins */
1528 if (!par->next && bv_owner)
1529 maxdesc += LYX_PAPER_MARGIN;
1531 /* add the vertical spaces, that the user added */
1532 if (firstpar->added_space_bottom.kind() != VSpace::NONE)
1533 maxdesc += int(firstpar->added_space_bottom.inPixels(bview));
1535 /* do not forget the DTP-lines!
1536 * there height depends on the font of the nearest character */
1537 if (firstpar->line_bottom)
1538 maxdesc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1539 par, par->Last() - 1));
1541 /* and now the pagebreaks */
1542 if (firstpar->pagebreak_bottom)
1543 maxdesc += 3 * DefaultHeight();
1545 /* and now the layout spaces, for example before and after a section,
1546 * or between the items of a itemize or enumerate environment */
1547 if (!firstpar->pagebreak_bottom && row_ptr->par()->Next()) {
1548 LyXParagraph * nextpar = row_ptr->par()->Next();
1549 LyXParagraph * comparepar = row_ptr->par();
1553 if (comparepar->GetDepth() > nextpar->GetDepth()) {
1554 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1555 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1556 if (comparepar->GetLayout()!= nextpar->GetLayout()
1557 || nextpar->GetLabelWidthString() !=
1558 comparepar->GetLabelWidthString())
1559 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1561 if (unusual > usual)
1562 layoutdesc = unusual;
1566 else if (comparepar->GetDepth() == nextpar->GetDepth()) {
1568 if (comparepar->GetLayout()!= nextpar->GetLayout()
1569 || nextpar->GetLabelWidthString() !=
1570 comparepar->GetLabelWidthString())
1571 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1576 /* incalculate the layout spaces */
1577 maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1578 maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1580 /* calculate the new height of the text */
1581 height -= row_ptr->height();
1583 row_ptr->height(maxasc + maxdesc + labeladdon);
1584 row_ptr->baseline(maxasc + labeladdon);
1586 height += row_ptr->height();
1588 PrepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1589 row_ptr->width(int(maxwidth + x));
1592 width = max(0,workWidth(bview));
1594 if (r->width() > width)
1602 /* Appends the implicit specified paragraph behind the specified row,
1603 * start at the implicit given position */
1604 void LyXText::AppendParagraph(BufferView * bview, Row * row) const
1606 bool not_ready = true;
1608 // The last character position of a paragraph is an invariant so we can
1609 // safely get it here. (Asger)
1610 int const lastposition = row->par()->Last();
1613 // Get the next breakpoint
1614 int z = NextBreakPoint(bview, row, workWidth(bview));
1618 // Insert the new row
1619 if (z < lastposition) {
1621 InsertRow(row, row->par(), z);
1628 // Set the dimensions of the row
1629 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1630 SetHeightOfRow(bview, tmprow);
1632 } while (not_ready);
1636 void LyXText::BreakAgain(BufferView * bview, Row * row) const
1638 bool not_ready = true;
1641 /* get the next breakpoint */
1642 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1645 if (z < row->par()->Last()) {
1646 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1649 InsertRow(row, row->par(), z);
1655 if (row->pos() == z)
1656 not_ready = false; // the rest will not change
1662 /* if there are some rows too much, delete them */
1663 /* only if you broke the whole paragraph! */
1664 Row * tmprow2 = row;
1665 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1666 tmprow2 = tmprow2->next();
1668 while (tmprow2 != row) {
1669 tmprow2 = tmprow2->previous();
1670 RemoveRow(tmprow2->next());
1675 /* set the dimensions of the row */
1676 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1677 SetHeightOfRow(bview, tmprow);
1678 } while (not_ready);
1682 // this is just a little changed version of break again
1683 void LyXText::BreakAgainOneRow(BufferView * bview, Row * row)
1685 // get the next breakpoint
1686 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1689 if (z < row->par()->Last()) {
1691 || (row->next() && row->next()->par() != row->par())) {
1692 /* insert a new row */
1694 InsertRow(row, row->par(), z);
1700 if (row->pos() != z)
1704 // if there are some rows too much, delete them
1705 // only if you broke the whole paragraph!
1706 Row * tmprow2 = row;
1707 while (tmprow2->next()
1708 && tmprow2->next()->par() == row->par()) {
1709 tmprow2 = tmprow2->next();
1711 while (tmprow2 != row) {
1712 tmprow2 = tmprow2->previous();
1713 RemoveRow(tmprow2->next());
1717 // set the dimensions of the row
1718 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1719 SetHeightOfRow(bview, tmprow);
1723 void LyXText::BreakParagraph(BufferView * bview, char keep_layout)
1725 LyXLayout const & layout =
1726 textclasslist.Style(bview->buffer()->params.textclass,
1727 cursor.par()->GetLayout());
1729 // this is only allowed, if the current paragraph is not empty or caption
1730 if ((cursor.par()->Last() <= 0
1732 && !cursor.par()->IsDummy()
1736 layout.labeltype!= LABEL_SENSITIVE)
1739 SetUndo(bview->buffer(), Undo::INSERT,
1741 cursor.par()->ParFromPos(cursor.pos())->previous,
1742 cursor.par()->ParFromPos(cursor.pos())->next
1744 cursor.par()->previous,
1749 // please break always behind a space
1750 if (cursor.pos() < cursor.par()->Last()
1751 && cursor.par()->IsLineSeparator(cursor.pos()))
1752 cursor.pos(cursor.pos() + 1);
1754 // break the paragraph
1758 keep_layout = layout.isEnvironment();
1759 cursor.par()->BreakParagraph(bview->buffer()->params, cursor.pos(),
1762 // well this is the caption hack since one caption is really enough
1763 if (layout.labeltype == LABEL_SENSITIVE) {
1765 // set to standard-layout
1766 cursor.par()->SetLayout(bview->buffer()->params, 0);
1768 // set to standard-layout
1769 cursor.par()->Next()->SetLayout(bview->buffer()->params, 0);
1772 /* if the cursor is at the beginning of a row without prior newline,
1774 * This touches only the screen-update. Otherwise we would may have
1775 * an empty row on the screen */
1776 if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1)
1777 && cursor.row()->pos() == cursor.pos()) {
1781 status = LyXText::NEED_MORE_REFRESH;
1782 refresh_row = cursor.row();
1783 refresh_y = cursor.y() - cursor.row()->baseline();
1785 // Do not forget the special right address boxes
1786 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1787 while (refresh_row->previous() &&
1788 refresh_row->previous()->par() == refresh_row->par()) {
1789 refresh_row = refresh_row->previous();
1790 refresh_y -= refresh_row->height();
1793 RemoveParagraph(cursor.row());
1795 // set the dimensions of the cursor row
1796 cursor.row()->fill(Fill(bview, cursor.row(), workWidth(bview)));
1798 SetHeightOfRow(bview, cursor.row());
1800 while (cursor.par()->Next()->Last()
1801 && cursor.par()->Next()->IsNewline(0))
1802 cursor.par()->Next()->Erase(0);
1804 InsertParagraph(bview, cursor.par()->Next(), cursor.row());
1806 UpdateCounters(bview, cursor.row()->previous());
1808 /* This check is necessary. Otherwise the new empty paragraph will
1809 * be deleted automatically. And it is more friendly for the user! */
1811 SetCursor(bview, cursor.par()->Next(), 0);
1813 SetCursor(bview, cursor.par(), 0);
1815 if (cursor.row()->next())
1816 BreakAgain(bview, cursor.row()->next());
1823 void LyXText::OpenFootnote(BufferView * bview)
1825 LyXParagraph * endpar,* tmppar;
1828 LyXParagraph * par = cursor.par()->ParFromPos(cursor.pos());
1830 /* if there is no footnote in this paragraph, just return. */
1832 || par->next->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1835 /* ok, move the cursor right before the footnote */
1837 /* just a little faster than using CursorRight() */
1839 cursor.par()->ParFromPos(cursor.pos()) != par;) {
1840 cursor.pos(cursor.pos() + 1);
1843 /* now the cursor is at the beginning of the physical par */
1844 SetCursor(bview, cursor.par(),
1845 cursor.pos() + cursor.par()->ParFromPos(cursor.pos())->size());
1847 /* the cursor must be exactly before the footnote */
1848 par = cursor.par()->ParFromPos(cursor.pos());
1850 status = LyXText::NEED_MORE_REFRESH;
1851 refresh_row = cursor.row();
1852 refresh_y = cursor.y() - cursor.row()->baseline();
1854 tmppar = cursor.par();
1855 endpar = cursor.par()->Next();
1858 tmppar->OpenFootnote(cursor.pos());
1859 RemoveParagraph(row);
1860 /* set the dimensions of the cursor row */
1861 row->fill(Fill(bview, row, workWidth(bview)));
1862 SetHeightOfRow(bview, row);
1863 // CHECK See comment on top of text.C
1864 tmppar = tmppar->Next();
1866 while (tmppar != endpar) {
1868 InsertParagraph(bview, tmppar, row);
1869 while (row->next() && row->next()->par() == tmppar)
1871 tmppar = tmppar->Next();
1874 SetCursor(bview, par->next, 0);
1875 sel_cursor = cursor;
1880 // Just a macro to make some thing easier.
1881 void LyXText::RedoParagraph(BufferView * bview) const
1884 RedoParagraphs(bview, cursor, cursor.par()->Next());
1885 SetCursorIntern(bview, cursor.par(), cursor.pos());
1889 /* insert a character, moves all the following breaks in the
1890 * same Paragraph one to the right and make a rebreak */
1891 void LyXText::InsertChar(BufferView * bview, char c)
1893 SetUndo(bview->buffer(), Undo::INSERT,
1895 cursor.par()->ParFromPos(cursor.pos())->previous,
1896 cursor.par()->ParFromPos(cursor.pos())->next
1898 cursor.par()->previous,
1903 // When the free-spacing option is set for the current layout,
1904 // disable the double-space checking
1906 bool const freeSpacing =
1907 textclasslist.Style(bview->buffer()->params.textclass,
1908 cursor.row()->par()->GetLayout()).free_spacing;
1911 if (lyxrc.auto_number) {
1912 if (current_font.number() == LyXFont::ON) {
1913 if (!isdigit(c) && !strchr("+-/*", c) &&
1915 cursor.pos() >= 1 &&
1916 cursor.pos() < cursor.par()->size() &&
1917 GetFont(bview->buffer(),
1919 cursor.pos()).number() == LyXFont::ON &&
1920 GetFont(bview->buffer(),
1922 cursor.pos()-1).number() == LyXFont::ON)
1924 Number(bview); // Set current_font.number to OFF
1925 } else if (isdigit(c) &&
1926 real_current_font.isVisibleRightToLeft()) {
1927 Number(bview); // Set current_font.number to ON
1929 if (cursor.pos() > 0) {
1930 char const c = cursor.par()->GetChar(cursor.pos() - 1);
1931 if (strchr("+-",c) &&
1932 (cursor.pos() == 1 ||
1933 cursor.par()->IsSeparator(cursor.pos() - 2) ||
1934 cursor.par()->IsNewline(cursor.pos() - 2) )
1936 SetCharFont(bview->buffer(),
1940 } else if (strchr(".,", c) &&
1941 cursor.pos() >= 2 &&
1942 GetFont(bview->buffer(),
1944 cursor.pos()-2).number() == LyXFont::ON) {
1945 SetCharFont(bview->buffer(),
1955 /* First check, if there will be two blanks together or a blank at
1956 the beginning of a paragraph.
1957 I decided to handle blanks like normal characters, the main
1958 difference are the special checks when calculating the row.fill
1959 (blank does not count at the end of a row) and the check here */
1961 // The bug is triggered when we type in a description environment:
1962 // The current_font is not changed when we go from label to main text
1963 // and it should (along with realtmpfont) when we type the space.
1964 // CHECK There is a bug here! (Asger)
1966 LyXFont realtmpfont = real_current_font;
1967 LyXFont rawtmpfont = current_font; /* store the current font.
1968 * This is because of the use
1969 * of cursor movements. The moving
1970 * cursor would refresh the
1973 // Get the font that is used to calculate the baselineskip
1974 LyXParagraph::size_type const lastpos = cursor.par()->Last();
1975 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
1978 bool jumped_over_space = false;
1980 if (!freeSpacing && IsLineSeparatorChar(c)) {
1981 if ((cursor.pos() > 0
1982 && cursor.par()->IsLineSeparator(cursor.pos() - 1))
1983 || (cursor.pos() > 0
1984 && cursor.par()->IsNewline(cursor.pos() - 1))
1985 || (cursor.pos() == 0
1987 && !(cursor.par()->Previous()
1988 && cursor.par()->Previous()->footnoteflag
1989 == LyXParagraph::OPEN_FOOTNOTE)
1992 if (cursor.pos() == 0 )
1993 bview->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1995 bview->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way. Please read the Tutorial."));
1999 } else if (IsNewlineChar(c)) {
2001 if (cursor.par()->FirstPhysicalPar() == cursor.par()
2003 if (cursor.par() == cursor.par()
2005 && cursor.pos() <= BeginningOfMainBody(bview->buffer(), cursor.par())) {
2009 /* No newline at first position
2010 * of a paragraph or behind labels.
2011 * TeX does not allow that. */
2013 if (cursor.pos() < cursor.par()->Last() &&
2014 cursor.par()->IsLineSeparator(cursor.pos()))
2015 // newline always after a blank!
2017 cursor.row()->fill(-1); // to force a new break
2020 // the display inset stuff
2021 if (cursor.row()->par()->GetChar(cursor.row()->pos()) == LyXParagraph::META_INSET
2022 && cursor.row()->par()->GetInset(cursor.row()->pos())
2023 && (cursor.row()->par()->GetInset(cursor.row()->pos())->display() ||
2024 cursor.row()->par()->GetInset(cursor.row()->pos())->needFullRow()))
2025 cursor.row()->fill(-1); // to force a new break
2027 // get the cursor row fist
2028 Row * row = cursor.row();
2029 int y = cursor.y() - row->baseline();
2030 if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset
2031 * already insertet the character */
2032 cursor.par()->InsertChar(cursor.pos(), c);
2033 SetCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
2035 if (!jumped_over_space) {
2036 // refresh the positions
2038 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2039 tmprow = tmprow->next();
2040 tmprow->pos(tmprow->pos() + 1);
2044 // Is there a break one row above
2045 if ((cursor.par()->IsLineSeparator(cursor.pos())
2046 || cursor.par()->IsNewline(cursor.pos())
2047 || cursor.row()->fill() == -1)
2048 && row->previous() && row->previous()->par() == row->par()) {
2049 LyXParagraph::size_type z = NextBreakPoint(bview,
2052 if (z >= row->pos()) {
2055 // set the dimensions of the row above
2056 row->previous()->fill(Fill(bview,
2060 SetHeightOfRow(bview, row->previous());
2062 y -= row->previous()->height();
2064 refresh_row = row->previous();
2065 status = LyXText::NEED_MORE_REFRESH;
2067 BreakAgainOneRow(bview, row);
2069 current_font = rawtmpfont;
2070 real_current_font = realtmpfont;
2071 SetCursor(bview, cursor.par(), cursor.pos() + 1,
2072 false, cursor.boundary());
2073 // cursor MUST be in row now.
2075 if (row->next() && row->next()->par() == row->par())
2076 need_break_row = row->next();
2080 // check, wether the last characters font has changed.
2081 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2082 && rawparfont != rawtmpfont)
2083 RedoHeightOfParagraph(bview, cursor);
2090 // recalculate the fill of the row
2091 if (row->fill() >= 0) /* needed because a newline
2092 * will set fill to -1. Otherwise
2093 * we would not get a rebreak! */
2094 row->fill(Fill(bview, row, workWidth(bview)));
2095 if (row->fill() < 0) {
2098 refresh_x = cursor.x();
2099 refresh_pos = cursor.pos();
2100 status = LyXText::NEED_MORE_REFRESH;
2101 BreakAgainOneRow(bview, row);
2102 // will the cursor be in another row now?
2103 if (RowLast(row) <= cursor.pos() + 1 && row->next()) {
2104 if (row->next() && row->next()->par() == row->par())
2105 // this should always be true
2107 BreakAgainOneRow(bview, row);
2109 current_font = rawtmpfont;
2110 real_current_font = realtmpfont;
2112 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2114 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
2115 != cursor.boundary())
2116 SetCursor(bview, cursor.par(), cursor.pos(), false,
2117 !cursor.boundary());
2118 if (row->next() && row->next()->par() == row->par())
2119 need_break_row = row->next();
2124 refresh_x = cursor.x();
2126 refresh_pos = cursor.pos();
2128 int const tmpheight = row->height();
2129 SetHeightOfRow(bview, row);
2130 if (tmpheight == row->height())
2131 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2133 status = LyXText::NEED_MORE_REFRESH;
2135 current_font = rawtmpfont;
2136 real_current_font = realtmpfont;
2137 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2141 // check, wether the last characters font has changed.
2142 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2143 && rawparfont != rawtmpfont) {
2144 RedoHeightOfParagraph(bview, cursor);
2146 // now the special right address boxes
2147 if (textclasslist.Style(bview->buffer()->params.textclass,
2148 cursor.par()->GetLayout()).margintype
2149 == MARGIN_RIGHT_ADDRESS_BOX) {
2150 RedoDrawingOfParagraph(bview, cursor);
2158 void LyXText::charInserted()
2160 // Here we could call FinishUndo for every 20 characters inserted.
2161 // This is from my experience how emacs does it.
2162 static unsigned int counter = 0;
2172 void LyXText::PrepareToPrint(BufferView * bview,
2173 Row * row, float & x,
2174 float & fill_separator,
2176 float & fill_label_hfill,
2181 float w = row->fill();
2183 fill_label_hfill = 0;
2185 fill_label_hfill = 0;
2188 row->par()->isRightToLeftPar(bview->buffer()->params);
2192 x = RightMargin(bview->buffer(), row);
2193 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
2194 LyXFont font(LyXFont::ALL_SANE);
2195 font.setSize(LyXFont::SIZE_SMALL);
2196 x += lyxfont::width("Mwide-figM", font);
2200 if (workWidth(bview) > 0)
2201 x = LeftMargin(bview, row);
2205 // is there a manual margin with a manual label
2206 if (textclasslist.Style(bview->buffer()->params.textclass,
2207 row->par()->GetLayout()).margintype == MARGIN_MANUAL
2208 && textclasslist.Style(bview->buffer()->params.textclass,
2209 row->par()->GetLayout()).labeltype == LABEL_MANUAL) {
2211 /* one more since labels are left aligned */
2212 nlh = NumberOfLabelHfills(bview->buffer(), row) + 1;
2213 if (nlh && !row->par()->GetLabelWidthString().empty()) {
2214 fill_label_hfill = LabelFill(bview, row) / nlh;
2218 // are there any hfills in the row?
2219 float const nh = NumberOfHfills(bview->buffer(), row);
2222 fill_hfill = w / nh;
2224 // is it block, flushleft or flushright?
2225 // set x how you need it
2228 if (row->par()->FirstPhysicalPar()->align == LYX_ALIGN_LAYOUT)
2229 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2231 align = row->par()->FirstPhysicalPar()->align;
2233 if (row->par()->align == LYX_ALIGN_LAYOUT)
2234 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2236 align = row->par()->align;
2239 // center displayed insets
2241 if (row->par()->GetChar(row->pos()) == LyXParagraph::META_INSET
2242 && (inset=row->par()->GetInset(row->pos()))
2243 && (inset->display())) // || (inset->scroll() < 0)))
2244 align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
2245 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2248 case LYX_ALIGN_BLOCK:
2249 ns = NumberOfSeparators(bview->buffer(), row);
2250 if (ns && row->next() && row->next()->par() == row->par() &&
2251 !(row->next()->par()->IsNewline(row->next()->pos() - 1))
2252 && !(row->next()->par()->GetChar(row->next()->pos()) == LyXParagraph::META_INSET
2253 && row->next()->par()->GetInset(row->next()->pos())
2254 && row->next()->par()->GetInset(row->next()->pos())->display())
2256 fill_separator = w / ns;
2260 case LYX_ALIGN_RIGHT:
2263 case LYX_ALIGN_CENTER:
2271 ComputeBidiTables(bview->buffer(), row);
2273 LyXParagraph::size_type main_body =
2274 BeginningOfMainBody(bview->buffer(), row->par());
2275 LyXParagraph::size_type last = RowLast(row);
2277 if (main_body > 0 &&
2278 (main_body-1 > last ||
2279 !row->par()->IsLineSeparator(main_body-1))) {
2280 LyXLayout const & layout =
2281 textclasslist.Style(bview->buffer()->params.textclass,
2282 row->par()->GetLayout());
2283 x += lyxfont::width(layout.labelsep,
2284 GetFont(bview->buffer(), row->par(), -2));
2285 if (main_body-1 <= last)
2286 x += fill_label_hfill;
2291 /* important for the screen */
2294 /* the cursor set functions have a special mechanism. When they
2295 * realize, that you left an empty paragraph, they will delete it.
2296 * They also delete the corresponding row */
2298 void LyXText::CursorRightOneWord(BufferView * bview) const
2300 // treat floats, HFills and Insets as words
2301 LyXCursor tmpcursor = cursor;
2302 // CHECK See comment on top of text.C
2304 if (tmpcursor.pos() == tmpcursor.par()->Last()
2305 && tmpcursor.par()->Next()) {
2306 tmpcursor.par(tmpcursor.par()->Next());
2311 // Skip through initial nonword stuff.
2312 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2313 ! tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2315 // printf("Current pos1 %d", tmpcursor.pos()) ;
2316 tmpcursor.pos(tmpcursor.pos() + 1);
2319 // Advance through word.
2320 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2321 tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2323 // printf("Current pos2 %d", tmpcursor.pos()) ;
2324 tmpcursor.pos(tmpcursor.pos() + 1);
2328 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2332 void LyXText::CursorTab(BufferView * bview) const
2334 LyXCursor tmpcursor = cursor;
2335 while (tmpcursor.pos() < tmpcursor.par()->Last()
2336 && !tmpcursor.par()->IsNewline(tmpcursor.pos()))
2337 tmpcursor.pos(tmpcursor.pos() + 1);
2339 if (tmpcursor.pos() == tmpcursor.par()->Last()){
2340 if (tmpcursor.par()->Next()) {
2341 tmpcursor.par(tmpcursor.par()->Next());
2345 tmpcursor.pos(tmpcursor.pos() + 1);
2346 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2350 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2351 of prior word, not to end of next prior word. */
2353 void LyXText::CursorLeftOneWord(BufferView * bview) const
2355 // treat HFills, floats and Insets as words
2356 LyXCursor tmpcursor = cursor;
2357 while (tmpcursor.pos()
2358 && (tmpcursor.par()->IsSeparator(tmpcursor.pos() - 1)
2359 || tmpcursor.par()->IsKomma(tmpcursor.pos() - 1))
2360 && !(tmpcursor.par()->IsHfill(tmpcursor.pos() - 1)
2362 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2364 || tmpcursor.par()->IsInset(tmpcursor.pos() - 1)))
2365 tmpcursor.pos(tmpcursor.pos() - 1);
2368 && (tmpcursor.par()->IsInset(tmpcursor.pos() - 1)
2370 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2372 || tmpcursor.par()->IsHfill(tmpcursor.pos() - 1))) {
2373 tmpcursor.pos(tmpcursor.pos() - 1);
2374 } else if (!tmpcursor.pos()) {
2375 if (tmpcursor.par()->Previous()){
2376 tmpcursor.par(tmpcursor.par()->Previous());
2377 tmpcursor.pos(tmpcursor.par()->Last());
2379 } else { // Here, tmpcursor != 0
2380 while (tmpcursor.pos() > 0 &&
2381 tmpcursor.par()->IsWord(tmpcursor.pos()-1) )
2382 tmpcursor.pos(tmpcursor.pos() - 1);
2384 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2387 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2390 void LyXText::SelectWord(BufferView * bview)
2392 // Move cursor to the beginning, when not already there.
2394 && !cursor.par()->IsSeparator(cursor.pos()-1)
2395 && !cursor.par()->IsKomma(cursor.pos()-1) )
2396 CursorLeftOneWord(bview);
2398 // set the sel cursor
2399 sel_cursor = cursor;
2401 while (cursor.pos() < cursor.par()->Last()
2402 && !cursor.par()->IsSeparator(cursor.pos())
2403 && !cursor.par()->IsKomma(cursor.pos()) )
2404 cursor.pos(cursor.pos() + 1);
2405 SetCursor(bview, cursor.par(), cursor.pos() );
2407 // finally set the selection
2412 /* -------> Select the word currently under the cursor when:
2413 1: no selection is currently set,
2414 2: the cursor is not at the borders of the word. */
2416 bool LyXText::SelectWordWhenUnderCursor(BufferView * bview)
2419 cursor.pos() > 0 && cursor.pos() < cursor.par()->Last()
2420 && !cursor.par()->IsSeparator(cursor.pos())
2421 && !cursor.par()->IsKomma(cursor.pos())
2422 && !cursor.par()->IsSeparator(cursor.pos() -1)
2423 && !cursor.par()->IsKomma(cursor.pos() -1)) {
2431 // This function is only used by the spellchecker for NextWord().
2432 // It doesn't handle LYX_ACCENTs and probably never will.
2433 string const LyXText::SelectNextWord(BufferView * bview,
2434 float & value) const
2436 LyXParagraph * tmppar = cursor.par();
2438 // If this is not the very first word, skip rest of
2439 // current word because we are probably in the middle
2440 // of a word if there is text here.
2441 if (cursor.pos() || cursor.par()->previous) {
2442 while (cursor.pos() < cursor.par()->Last()
2443 && cursor.par()->IsLetter(cursor.pos()))
2444 cursor.pos(cursor.pos() + 1);
2446 // Now, skip until we have real text (will jump paragraphs)
2447 while ((cursor.par()->Last() > cursor.pos()
2448 && (!cursor.par()->IsLetter(cursor.pos())
2449 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2450 .latex() == LyXFont::ON))
2451 || (cursor.par()->Last() == cursor.pos()
2452 && cursor.par()->Next())){
2453 if (cursor.pos() == cursor.par()->Last()) {
2454 cursor.par(cursor.par()->Next());
2457 cursor.pos(cursor.pos() + 1);
2460 // Update the value if we changed paragraphs
2461 if (cursor.par() != tmppar){
2462 SetCursor(bview, cursor.par(), cursor.pos());
2463 value = float(cursor.y())/float(height);
2466 // Start the selection from here
2467 sel_cursor = cursor;
2469 std::ostringstream latex;
2471 // and find the end of the word
2472 // (optional hyphens are part of a word)
2473 while (cursor.pos() < cursor.par()->Last()
2474 && (cursor.par()->IsLetter(cursor.pos()))
2475 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2476 && cursor.par()->GetInset(cursor.pos()) != 0
2477 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2478 && latex.str() == "\\-"
2480 cursor.pos(cursor.pos() + 1);
2482 // Finally, we copy the word to a string and return it
2484 if (sel_cursor.pos() < cursor.pos()) {
2485 LyXParagraph::size_type i;
2486 for (i = sel_cursor.pos(); i < cursor.pos(); ++i) {
2487 if (cursor.par()->GetChar(i) != LyXParagraph::META_INSET)
2488 str += cursor.par()->GetChar(i);
2495 // This one is also only for the spellchecker
2496 void LyXText::SelectSelectedWord(BufferView * bview)
2498 // move cursor to the beginning
2499 SetCursor(bview, sel_cursor.par(), sel_cursor.pos());
2501 // set the sel cursor
2502 sel_cursor = cursor;
2504 std::ostringstream latex;
2506 // now find the end of the word
2507 while (cursor.pos() < cursor.par()->Last()
2508 && (cursor.par()->IsLetter(cursor.pos())
2509 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2510 && cursor.par()->GetInset(cursor.pos()) != 0
2511 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2512 && latex.str() == "\\-"
2514 cursor.pos(cursor.pos() + 1);
2516 SetCursor(bview, cursor.par(), cursor.pos());
2518 // finally set the selection
2523 /* -------> Delete from cursor up to the end of the current or next word. */
2524 void LyXText::DeleteWordForward(BufferView * bview)
2526 if (!cursor.par()->Last())
2529 LyXCursor tmpcursor = cursor;
2530 tmpcursor.row(0); //Â ??
2531 selection = true; // to avoid deletion
2532 CursorRightOneWord(bview);
2533 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2534 sel_cursor = cursor;
2538 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2539 CutSelection(bview);
2544 /* -------> Delete from cursor to start of current or prior word. */
2545 void LyXText::DeleteWordBackward(BufferView * bview)
2547 if (!cursor.par()->Last())
2550 LyXCursor tmpcursor = cursor;
2551 tmpcursor.row(0); // ??
2552 selection = true; // to avoid deletion
2553 CursorLeftOneWord(bview);
2554 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2555 sel_cursor = cursor;
2558 CutSelection(bview);
2563 /* -------> Kill to end of line. */
2564 void LyXText::DeleteLineForward(BufferView * bview)
2566 if (!cursor.par()->Last())
2567 // Paragraph is empty, so we just go to the right
2570 LyXCursor tmpcursor = cursor;
2571 // We can't store the row over a regular SetCursor
2572 // so we set it to 0 and reset it afterwards.
2573 tmpcursor.row(0); //Â ??
2574 selection = true; // to avoid deletion
2576 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2577 sel_cursor = cursor;
2580 // What is this test for ??? (JMarc)
2582 DeleteWordForward(bview);
2584 CutSelection(bview);
2590 // Change the case of a word at cursor position.
2591 // This function directly manipulates LyXParagraph::text because there
2592 // is no LyXParagraph::SetChar currently. I did what I could to ensure
2593 // that it is correct. I guess part of it should be moved to
2594 // LyXParagraph, but it will have to change for 1.1 anyway. At least
2595 // it does not access outside of the allocated array as the older
2596 // version did. (JMarc)
2597 void LyXText::ChangeWordCase(BufferView * bview, LyXText::TextCase action)
2600 LyXParagraph * tmppar = cursor.par()->ParFromPos(cursor.pos());
2602 LyXParagraph * tmppar = cursor.par();
2605 SetUndo(bview->buffer(),Undo::FINISH, tmppar->previous, tmppar->next);
2608 LyXParagraph::size_type tmppos =
2609 cursor.par()->PositionInParFromPos(cursor.pos());
2611 LyXParagraph::size_type tmppos = cursor.pos();
2613 while (tmppos < tmppar->size()) {
2614 unsigned char c = tmppar->GetChar(tmppos);
2615 if (IsKommaChar(c) || IsLineSeparatorChar(c))
2617 if (c != LyXParagraph::META_INSET) {
2619 case text_lowercase:
2622 case text_capitalization:
2624 action = text_lowercase;
2626 case text_uppercase:
2632 //tmppar->text[tmppos] = c;
2633 tmppar->SetChar(tmppos, c);
2636 CheckParagraph(bview, tmppar, tmppos);
2637 CursorRightOneWord(bview);
2641 void LyXText::Delete(BufferView * bview)
2643 // this is a very easy implementation
2645 LyXCursor old_cursor = cursor;
2646 int const old_cur_par_id = old_cursor.par()->id();
2647 int const old_cur_par_prev_id = old_cursor.par()->previous ?
2648 old_cursor.par()->previous->id() : 0;
2650 // just move to the right
2653 // CHECK Look at the comment here.
2654 // This check is not very good...
2655 // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
2656 // and that can very well delete the par or par->previous in
2657 // old_cursor. Will a solution where we compare paragraph id's
2659 if ((cursor.par()->previous ? cursor.par()->previous->id() : 0)
2660 == old_cur_par_prev_id
2661 && cursor.par()->id() != old_cur_par_id)
2662 return; // delete-empty-paragraph-mechanism has done it
2664 // if you had success make a backspace
2665 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2666 LyXCursor tmpcursor = cursor;
2667 cursor = old_cursor; // to make sure undo gets the right cursor position
2668 SetUndo(bview->buffer(), Undo::DELETE,
2670 cursor.par()->ParFromPos(cursor.pos())->previous,
2671 cursor.par()->ParFromPos(cursor.pos())->next
2673 cursor.par()->previous,
2683 void LyXText::Backspace(BufferView * bview)
2685 // Get the font that is used to calculate the baselineskip
2686 LyXParagraph::size_type lastpos = cursor.par()->Last();
2687 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2690 if (cursor.pos() == 0) {
2691 // The cursor is at the beginning of a paragraph,
2692 // so the the backspace will collapse two paragraphs into one.
2694 // we may paste some paragraphs
2696 // is it an empty paragraph?
2699 || (lastpos == 1 && cursor.par()->IsSeparator(0)))
2701 && !(cursor.par()->Next()
2702 && cursor.par()->footnoteflag == LyXParagraph::NO_FOOTNOTE
2703 && cursor.par()->Next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
2706 // This is an empty paragraph and we delete it just by moving the cursor one step
2707 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2708 // of the paragraph.
2710 if (cursor.par()->previous) {
2712 LyXParagraph * tmppar = cursor.par()->previous->FirstPhysicalPar();
2714 LyXParagraph * tmppar = cursor.par()->previous;
2716 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2718 && cursor.par()->footnoteflag == tmppar->footnoteflag
2720 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2721 // Inherit botom DTD from the paragraph below.
2722 // (the one we are deleting)
2723 tmppar->line_bottom = cursor.par()->line_bottom;
2724 tmppar->added_space_bottom = cursor.par()->added_space_bottom;
2725 tmppar->pagebreak_bottom = cursor.par()->pagebreak_bottom;
2730 // the layout things can change the height of a row !
2731 int const tmpheight = cursor.row()->height();
2732 SetHeightOfRow(bview, cursor.row());
2733 if (cursor.row()->height() != tmpheight) {
2734 refresh_y = cursor.y() - cursor.row()->baseline();
2735 refresh_row = cursor.row();
2736 status = LyXText::NEED_MORE_REFRESH;
2743 if (cursor.par()->ParFromPos(cursor.pos())->previous){
2744 SetUndo(bview->buffer(), Undo::DELETE,
2745 cursor.par()->ParFromPos(cursor.pos())->previous->previous,
2746 cursor.par()->ParFromPos(cursor.pos())->next);
2749 if (cursor.par()->previous) {
2750 SetUndo(bview->buffer(), Undo::DELETE,
2751 cursor.par()->previous->previous,
2752 cursor.par()->next);
2756 LyXParagraph * tmppar = cursor.par();
2757 Row * tmprow = cursor.row();
2759 // We used to do CursorLeftIntern() here, but it is
2760 // not a good idea since it triggers the auto-delete
2761 // mechanism. So we do a CursorLeftIntern()-lite,
2762 // without the dreaded mechanism. (JMarc)
2763 if (cursor.par()->Previous()) {
2764 // steps into the above paragraph.
2765 SetCursorIntern(bview, cursor.par()->Previous(),
2766 cursor.par()->Previous()->Last(), false);
2769 /* Pasting is not allowed, if the paragraphs have different
2770 layout. I think it is a real bug of all other
2771 word processors to allow it. It confuses the user.
2772 Even so with a footnote paragraph and a non-footnote
2773 paragraph. I will not allow pasting in this case,
2774 because the user would be confused if the footnote behaves
2775 different wether it is open or closed.
2777 Correction: Pasting is always allowed with standard-layout
2779 if (cursor.par() != tmppar
2780 && (cursor.par()->GetLayout() == tmppar->GetLayout()
2781 || tmppar->GetLayout() == 0 /*standard*/)
2783 && cursor.par()->footnoteflag == tmppar->footnoteflag
2785 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2787 RemoveParagraph(tmprow);
2789 cursor.par()->PasteParagraph(bview->buffer()->params);
2791 if (!cursor.pos() || !cursor.par()->IsSeparator(cursor.pos() - 1))
2792 ; //cursor.par()->InsertChar(cursor.pos(), ' ');
2793 // strangely enough it seems that commenting out the line above removes
2794 // most or all of the segfaults. I will however also try to move the
2795 // two Remove... lines in front of the PasteParagraph too.
2798 cursor.pos(cursor.pos() - 1);
2800 status = LyXText::NEED_MORE_REFRESH;
2801 refresh_row = cursor.row();
2802 refresh_y = cursor.y() - cursor.row()->baseline();
2804 // remove the lost paragraph
2805 // This one is not safe, since the paragraph that the tmprow and the
2806 // following rows belong to has been deleted by the PasteParagraph
2807 // above. The question is... could this be moved in front of the
2809 //RemoveParagraph(tmprow);
2810 //RemoveRow(tmprow);
2812 // This rebuilds the rows.
2813 AppendParagraph(bview, cursor.row());
2814 UpdateCounters(bview, cursor.row());
2816 // the row may have changed, block, hfills etc.
2817 SetCursor(bview, cursor.par(), cursor.pos(), false);
2820 /* this is the code for a normal backspace, not pasting
2822 SetUndo(bview->buffer(), Undo::DELETE,
2824 cursor.par()->ParFromPos(cursor.pos())->previous,
2825 cursor.par()->ParFromPos(cursor.pos())->next
2827 cursor.par()->previous,
2831 // We used to do CursorLeftIntern() here, but it is
2832 // not a good idea since it triggers the auto-delete
2833 // mechanism. So we do a CursorLeftIntern()-lite,
2834 // without the dreaded mechanism. (JMarc)
2835 SetCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2836 false, cursor.boundary());
2838 // some insets are undeletable here
2839 if (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
2840 if (!cursor.par()->GetInset(cursor.pos())->Deletable())
2842 // force complete redo when erasing display insets
2843 // this is a cruel method but safe..... Matthias
2844 if (cursor.par()->GetInset(cursor.pos())->display() ||
2845 cursor.par()->GetInset(cursor.pos())->needFullRow()) {
2846 cursor.par()->Erase(cursor.pos());
2847 RedoParagraph(bview);
2852 Row * row = cursor.row();
2853 int y = cursor.y() - row->baseline();
2854 LyXParagraph::size_type z;
2855 /* remember that a space at the end of a row doesnt count
2856 * when calculating the fill */
2857 if (cursor.pos() < RowLast(row) ||
2858 !cursor.par()->IsLineSeparator(cursor.pos())) {
2859 row->fill(row->fill() + SingleWidth(bview,
2864 /* some special code when deleting a newline. This is similar
2865 * to the behavior when pasting paragraphs */
2866 if (cursor.pos() && cursor.par()->IsNewline(cursor.pos())) {
2867 cursor.par()->Erase(cursor.pos());
2868 // refresh the positions
2870 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2871 tmprow = tmprow->next();
2872 tmprow->pos(tmprow->pos() - 1);
2874 if (cursor.par()->IsLineSeparator(cursor.pos() - 1))
2875 cursor.pos(cursor.pos() - 1);
2877 if (cursor.pos() < cursor.par()->Last() && !cursor.par()->IsSeparator(cursor.pos())) {
2878 cursor.par()->InsertChar(cursor.pos(), ' ');
2879 // refresh the positions
2881 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2882 tmprow = tmprow->next();
2883 tmprow->pos(tmprow->pos() + 1);
2887 cursor.par()->Erase(cursor.pos());
2889 // refresh the positions
2891 while (tmprow->next()
2892 && tmprow->next()->par() == row->par()) {
2893 tmprow = tmprow->next();
2894 tmprow->pos(tmprow->pos() - 1);
2897 // delete newlines at the beginning of paragraphs
2898 while (cursor.par()->Last() &&
2899 cursor.par()->IsNewline(cursor.pos()) &&
2900 cursor.pos() == BeginningOfMainBody(bview->buffer(),
2902 cursor.par()->Erase(cursor.pos());
2903 // refresh the positions
2905 while (tmprow->next() &&
2906 tmprow->next()->par() == row->par()) {
2907 tmprow = tmprow->next();
2908 tmprow->pos(tmprow->pos() - 1);
2913 // is there a break one row above
2914 if (row->previous() && row->previous()->par() == row->par()) {
2915 z = NextBreakPoint(bview, row->previous(),
2917 if (z >= row->pos()) {
2920 Row * tmprow = row->previous();
2922 // maybe the current row is now empty
2923 if (row->pos() >= row->par()->Last()) {
2928 BreakAgainOneRow(bview, row);
2929 if (row->next() && row->next()->par() == row->par())
2930 need_break_row = row->next();
2935 // set the dimensions of the row above
2936 y -= tmprow->height();
2937 tmprow->fill(Fill(bview, tmprow,
2939 SetHeightOfRow(bview, tmprow);
2942 refresh_row = tmprow;
2943 status = LyXText::NEED_MORE_REFRESH;
2944 SetCursor(bview, cursor.par(), cursor.pos(),
2945 false, cursor.boundary());
2946 //current_font = rawtmpfont;
2947 //real_current_font = realtmpfont;
2948 // check, whether the last character's font has changed.
2950 cursor.par()->GetFontSettings(bview->buffer()->params,
2951 cursor.par()->Last() - 1))
2952 RedoHeightOfParagraph(bview, cursor);
2957 // break the cursor row again
2958 if (row->next() && row->next()->par() == row->par() &&
2959 (RowLast(row) == row->par()->Last() - 1 ||
2960 NextBreakPoint(bview, row, workWidth(bview)) != RowLast(row))) {
2962 /* it can happen that a paragraph loses one row
2963 * without a real breakup. This is when a word
2964 * is to long to be broken. Well, I don t care this
2966 if (RowLast(row) == row->par()->Last() - 1)
2967 RemoveRow(row->next());
2971 status = LyXText::NEED_MORE_REFRESH;
2973 BreakAgainOneRow(bview, row);
2974 // will the cursor be in another row now?
2975 if (row->next() && row->next()->par() == row->par() &&
2976 RowLast(row) <= cursor.pos()) {
2978 BreakAgainOneRow(bview, row);
2981 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2983 if (row->next() && row->next()->par() == row->par())
2984 need_break_row = row->next();
2988 // set the dimensions of the row
2989 row->fill(Fill(bview, row, workWidth(bview)));
2990 int const tmpheight = row->height();
2991 SetHeightOfRow(bview, row);
2992 if (tmpheight == row->height())
2993 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2995 status = LyXText::NEED_MORE_REFRESH;
2998 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3002 // current_font = rawtmpfont;
3003 // real_current_font = realtmpfont;
3005 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
3006 != cursor.boundary())
3007 SetCursor(bview, cursor.par(), cursor.pos(), false,
3008 !cursor.boundary());
3010 lastpos = cursor.par()->Last();
3011 if (cursor.pos() == lastpos)
3012 SetCurrentFont(bview);
3014 // check, whether the last characters font has changed.
3016 cursor.par()->GetFontSettings(bview->buffer()->params, lastpos - 1)) {
3017 RedoHeightOfParagraph(bview, cursor);
3019 // now the special right address boxes
3020 if (textclasslist.Style(bview->buffer()->params.textclass,
3021 cursor.par()->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3022 RedoDrawingOfParagraph(bview, cursor);
3028 void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
3029 Row * row_ptr, int y, bool cleared)
3031 /* returns a printed row */
3032 Painter & pain = bview->painter();
3035 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
3037 LyXParagraph::size_type const last = RowLastPrintable(row_ptr);
3039 LyXParagraph::size_type vpos, pos;
3045 LyXFont font(LyXFont::ALL_SANE);
3047 if (row_ptr->height() <= 0) {
3048 lyxerr << "LYX_ERROR: row.height: "
3049 << row_ptr->height() << endl;
3053 float fill_separator;
3055 float fill_label_hfill;
3056 PrepareToPrint(bview, row_ptr, x, fill_separator,
3057 fill_hfill, fill_label_hfill);
3059 if (inset_owner && (x < 0))
3063 // clear the area where we want to paint/print
3064 int ww = bview->workWidth();
3066 bool clear_area = true;
3069 if (!bview->screen()->forceClear() && last == row_ptr->pos()
3070 && row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET
3071 && (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
3072 clear_area = inset->doClearArea();
3074 // we don't need to clear it's already done!!!
3077 } else if (clear_area) {
3078 #ifdef WITH_WARNINGS
3079 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
3081 int const w = (inset_owner ?
3082 inset_owner->width(bview, font)-2 : ww);
3083 pain.fillRectangle(x_offset, y_offset, w, row_ptr->height());
3084 } else if (inset != 0) {
3085 int h = row_ptr->baseline() - inset->ascent(bview, font);
3087 int const w = (inset_owner ?
3088 inset_owner->width(bview, font) : ww);
3089 pain.fillRectangle(x_offset, y_offset, w, h);
3091 h += inset->ascent(bview, font) + inset->descent(bview, font);
3092 if ((row_ptr->height() - h) > 0) {
3093 int const w = (inset_owner ?
3094 inset_owner->width(bview, font) : ww);
3095 pain.fillRectangle(x_offset, y_offset + h,
3096 w, row_ptr->height() - h);
3098 if (!inset_owner && !inset->display() && !inset->needFullRow())
3100 int const w = inset->width(bview, font) + int(x);
3101 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
3106 int const w = (inset_owner ?
3107 inset_owner->width(bview, font) : ww);
3108 /* selection code */
3109 if (bidi_same_direction) {
3110 if (sel_start_cursor.row() == row_ptr &&
3111 sel_end_cursor.row() == row_ptr) {
3112 if (sel_start_cursor.x() < sel_end_cursor.x())
3113 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3115 sel_end_cursor.x() - sel_start_cursor.x(),
3119 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3121 sel_start_cursor.x() - sel_end_cursor.x(),
3124 } else if (sel_start_cursor.row() == row_ptr) {
3126 pain.fillRectangle(x_offset, y_offset,
3127 sel_start_cursor.x(),
3131 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3133 w - sel_start_cursor.x(),
3136 } else if (sel_end_cursor.row() == row_ptr) {
3138 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3140 w - sel_end_cursor.x(),
3144 pain.fillRectangle(x_offset, y_offset,
3148 } else if (y > sel_start_cursor.y()
3149 && y < sel_end_cursor.y()) {
3150 pain.fillRectangle(x_offset, y_offset, w,
3154 } else if (sel_start_cursor.row() != row_ptr &&
3155 sel_end_cursor.row() != row_ptr &&
3156 y > sel_start_cursor.y()
3157 && y < sel_end_cursor.y()) {
3158 pain.fillRectangle(x_offset, y_offset, w,
3161 } else if (sel_start_cursor.row() == row_ptr ||
3162 sel_end_cursor.row() == row_ptr) {
3164 if ((sel_start_cursor.row() != row_ptr && !is_rtl) ||
3165 (sel_end_cursor.row() != row_ptr && is_rtl))
3166 pain.fillRectangle(x_offset, y_offset,
3170 LyXParagraph::size_type main_body =
3171 BeginningOfMainBody(bview->buffer(),
3174 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3175 pos = vis2log(vpos);
3176 float const old_tmpx = tmpx;
3177 if (main_body > 0 && pos == main_body-1) {
3178 tmpx += fill_label_hfill +
3179 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3180 row_ptr->par()->GetLayout()).labelsep,
3181 GetFont(bview->buffer(),row_ptr->par(), -2));
3182 if (row_ptr->par()->IsLineSeparator(main_body-1))
3183 tmpx -= SingleWidth(bview, row_ptr->par(), main_body-1);
3185 if (HfillExpansion(bview->buffer(), row_ptr, pos)) {
3186 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3187 if (pos >= main_body)
3190 tmpx += fill_label_hfill;
3192 else if (row_ptr->par()->IsSeparator(pos)) {
3193 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3194 if (pos >= main_body)
3195 tmpx += fill_separator;
3197 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3199 if ((sel_start_cursor.row() != row_ptr ||
3200 sel_start_cursor.pos() <= pos) &&
3201 (sel_end_cursor.row() != row_ptr ||
3202 pos < sel_end_cursor.pos()) )
3203 pain.fillRectangle(x_offset + int(old_tmpx),
3205 int(tmpx - old_tmpx + 1),
3210 if ((sel_start_cursor.row() != row_ptr && is_rtl) ||
3211 (sel_end_cursor.row() != row_ptr && !is_rtl) )
3212 pain.fillRectangle(x_offset + int(tmpx),
3222 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3223 LyXFont font(LyXFont::ALL_SANE);
3224 font.setSize(LyXFont::SIZE_FOOTNOTE);
3225 font.setColor(LColor::footnote);
3227 box_x = LYX_PAPER_MARGIN + lyxfont::width(" wide-tab ", font);
3228 if (row_ptr->previous() &&
3229 row_ptr->previous()->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3231 switch (row_ptr->par()->footnotekind) {
3232 case LyXParagraph::MARGIN:
3235 case LyXParagraph::FIG:
3238 case LyXParagraph::TAB:
3241 case LyXParagraph::WIDE_FIG:
3244 case LyXParagraph::WIDE_TAB:
3247 case LyXParagraph::ALGORITHM:
3250 case LyXParagraph::FOOTNOTE:
3255 pain.fillRectangle(LYX_PAPER_MARGIN,
3257 box_x - LYX_PAPER_MARGIN,
3258 int(lyxfont::maxAscent(font)
3259 + lyxfont::maxDescent(font)),
3260 LColor::footnotebg);
3262 pain.line(LYX_PAPER_MARGIN, y_offset,
3263 workWidth(bview) - LYX_PAPER_MARGIN, y_offset,
3264 LColor::footnoteframe);
3266 pain.text(LYX_PAPER_MARGIN,
3267 y_offset + int(lyxfont::maxAscent(font)) + 1,
3270 pain.line(LYX_PAPER_MARGIN, y_offset,
3272 y_offset + int(lyxfont::maxAscent(font)
3273 + lyxfont::maxDescent(font)),
3274 LColor::footnoteframe);
3276 pain.line(LYX_PAPER_MARGIN,
3277 y_offset + int(lyxfont::maxAscent(font)
3278 + lyxfont::maxDescent(font)) + 1,
3280 y_offset + int(lyxfont::maxAscent(font)
3281 + lyxfont::maxDescent(font)) + 1,
3282 LColor::footnoteframe);
3286 /* draw the open floats in a red box */
3287 pain.line(box_x, y_offset,
3288 box_x, y_offset + row_ptr->height(),
3289 LColor::footnoteframe);
3291 pain.line(workWidth(bview) - LYX_PAPER_MARGIN,
3293 workWidth(bview) - LYX_PAPER_MARGIN,
3294 y_offset + row_ptr->height(),
3295 LColor::footnoteframe);
3298 // Draw appendix lines
3299 LyXParagraph * p = row_ptr->par()->PreviousBeforeFootnote()->FirstPhysicalPar();
3301 pain.line(1, y_offset,
3302 1, y_offset + row_ptr->height(),
3303 LColor::appendixline);
3304 pain.line(workWidth(bview) - 2, y_offset,
3305 workWidth(bview) - 2,
3306 y_offset + row_ptr->height(),
3307 LColor::appendixline);
3310 // Draw minipage line
3311 bool const minipage =
3312 (p->pextra_type == LyXParagraph::PEXTRA_MINIPAGE);
3314 pain.line(LYX_PAPER_MARGIN/5, y_offset,
3316 y_offset + row_ptr->height() - 1,
3317 LColor::minipageline);
3320 int const depth = p->GetDepth();
3321 for (int i = 1; i <= depth; ++i) {
3322 int const line_x = (LYX_PAPER_MARGIN / 5) *
3324 pain.line(line_x, y_offset, line_x,
3325 y_offset + row_ptr->height() - 1,
3328 } else if (row_ptr->previous() &&
3329 row_ptr->previous()->par()->footnoteflag
3330 == LyXParagraph::OPEN_FOOTNOTE) {
3331 LyXFont font(LyXFont::ALL_SANE);
3332 font.setSize(LyXFont::SIZE_FOOTNOTE);
3334 int const box_x = LYX_PAPER_MARGIN
3335 + lyxfont::width(" wide-tab ", font);
3337 pain.line(box_x, y_offset,
3338 workWidth(bview) - LYX_PAPER_MARGIN,
3339 y_offset, LColor::footnote);
3342 // Draw appendix lines
3344 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
3346 LyXParagraph * firstpar = row_ptr->par();
3348 if (firstpar->appendix){
3349 pain.line(1, y_offset,
3350 1, y_offset + row_ptr->height(),
3351 LColor::appendixline);
3352 pain.line(ww - 2, y_offset,
3353 ww - 2, y_offset + row_ptr->height(),
3354 LColor::appendixline);
3357 // Draw minipage line
3358 bool const minipage =
3359 (firstpar->pextra_type == LyXParagraph::PEXTRA_MINIPAGE);
3361 pain.line(LYX_PAPER_MARGIN/5 + box_x, y_offset,
3362 LYX_PAPER_MARGIN/5 + box_x,
3363 y_offset + row_ptr->height() - 1,
3364 LColor::minipageline);
3367 int const depth = firstpar->GetDepth();
3371 if (row_ptr->next())
3373 if (row_ptr->par()->footnoteflag ==
3374 row_ptr->next()->par()->footnoteflag)
3375 next_depth = row_ptr->next()->par()->GetDepth();
3376 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3379 next_depth = row_ptr->next()->par()->GetDepth();
3381 if (row_ptr->previous())
3383 if (row_ptr->par()->footnoteflag ==
3384 row_ptr->previous()->par()->footnoteflag)
3385 prev_depth = row_ptr->previous()->par()->GetDepth();
3386 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3389 prev_depth = row_ptr->previous()->par()->GetDepth();
3392 for (int i = 1; i <= depth; ++i) {
3393 int const line_x = (LYX_PAPER_MARGIN / 5) *
3394 (i + minipage) + box_x;
3395 pain.line(line_x, y_offset, line_x,
3396 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3400 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3403 pain.fillRectangle(line_x,
3404 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3405 LYX_PAPER_MARGIN / 5, 2,
3411 LyXLayout const & layout =
3412 textclasslist.Style(bview->buffer()->params.textclass,
3413 row_ptr->par()->GetLayout());
3416 y_bottom = row_ptr->height();
3418 /* is it a first row? */
3419 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3421 /* start of appendix? */
3422 if (row_ptr->par()->start_of_appendix){
3423 pain.line(1, y_offset,
3425 LColor::appendixline);
3428 /* think about the margins */
3429 if (!row_ptr->previous() && bv_owner)
3430 y_top += LYX_PAPER_MARGIN;
3432 if (row_ptr->par()->pagebreak_top){ /* draw a top pagebreak */
3434 pb_font.setColor(LColor::pagebreak).decSize();
3435 int w = 0, a = 0, d = 0;
3436 pain.line(0, y_offset + y_top + 2*DefaultHeight(),
3438 y_offset + y_top + 2 * DefaultHeight(),
3440 Painter::line_onoffdash)
3443 _("Page Break (top)"),
3446 LColor::background, false, w, a, d);
3447 pain.rectText((ww - w)/2,
3448 y_offset + y_top + 2 * DefaultHeight() + d,
3449 _("Page Break (top)"),
3452 LColor::background);
3453 y_top += 3 * DefaultHeight();
3456 if (row_ptr->par()->added_space_top.kind() == VSpace::VFILL) {
3457 /* draw a vfill top */
3458 pain.line(0, y_offset + 2 + y_top,
3459 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3462 pain.line(0, y_offset + y_top + 3 * DefaultHeight(),
3464 y_offset + y_top + 3 * DefaultHeight(),
3467 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3468 LYX_PAPER_MARGIN / 2,
3469 y_offset + y_top + 3 * DefaultHeight(),
3472 y_top += 3 * DefaultHeight();
3475 /* think about user added space */
3476 y_top += int(row_ptr->par()->added_space_top.inPixels(bview));
3478 /* think about the parskip */
3479 /* some parskips VERY EASY IMPLEMENTATION */
3480 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3481 if (layout.latextype == LATEX_PARAGRAPH
3482 && firstpar->GetDepth() == 0
3483 && firstpar->Previous())
3484 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3485 else if (firstpar->Previous()
3486 && textclasslist.Style(bview->buffer()->params.textclass,
3487 firstpar->Previous()->GetLayout()).latextype == LATEX_PARAGRAPH
3488 && firstpar->Previous()->GetDepth() == 0)
3489 // is it right to use defskip here, too? (AS)
3490 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3493 if (row_ptr->par()->line_top) { /* draw a top line */
3494 y_top += lyxfont::ascent('x',
3495 GetFont(bview->buffer(),
3496 row_ptr->par(), 0));
3497 int const w = (inset_owner ?
3498 inset_owner->width(bview, font) : ww);
3499 int const xp = static_cast<int>(inset_owner ? x : 0);
3500 pain.line(xp, y_offset + y_top,
3501 w, y_offset + y_top,
3503 Painter::line_solid,
3504 Painter::line_thick);
3506 y_top += lyxfont::ascent('x',GetFont(bview->buffer(),
3507 row_ptr->par(), 0));
3510 /* should we print a label? */
3511 if (layout.labeltype >= LABEL_STATIC
3512 && (layout.labeltype != LABEL_STATIC
3513 || layout.latextype != LATEX_ENVIRONMENT
3514 || row_ptr->par()->IsFirstInSequence())) {
3515 font = GetFont(bview->buffer(), row_ptr->par(), -2);
3516 if (!row_ptr->par()->GetLabelstring().empty()) {
3518 string const tmpstring =
3519 row_ptr->par()->GetLabelstring();
3521 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3522 if (bview->buffer()->params.secnumdepth >= 0) {
3523 /* this is special code for the chapter layout. This is printed in
3524 * an extra row and has a pagebreak at the top. */
3525 float spacing_val = 1.0;
3526 if (!row_ptr->par()->spacing.isDefault()) {
3527 spacing_val = row_ptr->par()->spacing.getValue();
3529 spacing_val = bview->buffer()->params.spacing.getValue();
3532 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3533 + int(layout.parsep) * DefaultHeight();
3535 tmpx = ww - LeftMargin(bview, row_ptr) -
3536 lyxfont::width(tmpstring, font);
3537 pain.text(int(tmpx),
3538 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3543 tmpx = ww - LeftMargin(bview, row_ptr)
3544 + lyxfont::width(layout.labelsep, font);
3546 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3547 LyXFont font(LyXFont::ALL_SANE);
3548 font.setSize(LyXFont::SIZE_SMALL);
3549 tmpx += lyxfont::width("Mwide-fixM", font);
3553 tmpx = x - lyxfont::width(layout.labelsep, font)
3554 - lyxfont::width(tmpstring, font);
3557 pain.text(int(tmpx),
3558 y_offset + row_ptr->baseline(),
3562 /* the labels at the top of an environment. More or less for bibliography */
3563 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3564 layout.labeltype == LABEL_BIBLIO ||
3565 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3566 if (row_ptr->par()->IsFirstInSequence()) {
3567 font = GetFont(bview->buffer(),
3568 row_ptr->par(), -2);
3569 if (!row_ptr->par()->GetLabelstring().empty()) {
3570 string const tmpstring =
3571 row_ptr->par()->GetLabelstring();
3572 float spacing_val = 1.0;
3573 if (!row_ptr->par()->spacing.isDefault()) {
3574 spacing_val = row_ptr->par()->spacing.getValue();
3576 spacing_val = bview->buffer()->params.spacing.getValue();
3579 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3580 + (layout.labelbottomsep * DefaultHeight()));
3583 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3584 tmpx = ( (is_rtl ? LeftMargin(bview, row_ptr) : x)
3585 + ww - RightMargin(bview->buffer(), row_ptr) ) / 2;
3586 tmpx -= lyxfont::width(tmpstring, font) / 2;
3588 tmpx = ww - LeftMargin(bview, row_ptr) -
3589 lyxfont::width(tmpstring, font);
3590 pain.text(int(tmpx),
3591 y_offset + row_ptr->baseline()
3592 - row_ptr->ascent_of_text()
3598 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3599 font = GetFont(bview->buffer(), row_ptr->par(), -1);
3601 tmpx = ww - LeftMargin(bview, row_ptr)
3602 + lyxfont::width(layout.labelsep, font);
3604 tmpx = x - lyxfont::width(layout.labelsep, font)
3605 - row_ptr->par()->bibkey->width(bview, font);
3606 row_ptr->par()->bibkey->draw(bview, font,
3607 y_offset + row_ptr->baseline(),
3612 /* is it a last row? */
3614 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
3616 LyXParagraph * par = row_ptr->par();
3620 row_ptr->par()->ParFromPos(last + 1) == par
3622 row_ptr->par() == par
3624 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
3626 /* think about the margins */
3627 if (!row_ptr->next() && bv_owner)
3628 y_bottom -= LYX_PAPER_MARGIN;
3630 /* draw a bottom pagebreak */
3631 if (firstpar->pagebreak_bottom) {
3633 pb_font.setColor(LColor::pagebreak).decSize();
3634 int w = 0, a = 0, d = 0;
3636 y_offset + y_bottom - 2 * DefaultHeight(),
3638 y_offset + y_bottom - 2 * DefaultHeight(),
3640 Painter::line_onoffdash)
3643 _("Page Break (bottom)"),
3646 LColor::background, false, w, a, d);
3647 pain.rectText((ww - w) / 2,
3648 y_offset + y_top + 2 * DefaultHeight() + d,
3649 _("Page Break (bottom)"),
3652 LColor::background);
3653 y_bottom -= 3 * DefaultHeight();
3656 if (firstpar->added_space_bottom.kind() == VSpace::VFILL) {
3657 /* draw a vfill bottom */
3658 pain.line(0, y_offset + y_bottom - 3 * DefaultHeight(),
3660 y_offset + y_bottom - 3 * DefaultHeight(),
3662 pain.line(0, y_offset + y_bottom - 2,
3664 y_offset + y_bottom - 2,
3666 pain.line(LYX_PAPER_MARGIN / 2,
3667 y_offset + y_bottom - 3 * DefaultHeight(),
3668 LYX_PAPER_MARGIN / 2,
3669 y_offset + y_bottom - 2,
3671 y_bottom -= 3 * DefaultHeight();
3674 /* think about user added space */
3675 y_bottom -= int(firstpar->added_space_bottom.inPixels(bview));
3677 if (firstpar->line_bottom) {
3678 /* draw a bottom line */
3679 y_bottom -= lyxfont::ascent('x', GetFont(bview->buffer(),
3680 par, par->Last() - 1));
3681 int const w = (inset_owner ?
3682 inset_owner->width(bview, font) : ww);
3683 int const xp = static_cast<int>(inset_owner ? x : 0);
3684 pain.line(xp, y_offset + y_bottom,
3685 w, y_offset + y_bottom,
3686 LColor::topline, Painter::line_solid,
3687 Painter::line_thick);
3688 y_bottom -= lyxfont::ascent('x',
3689 GetFont(bview->buffer(),
3695 int const endlabel =
3696 row_ptr->par()->GetEndLabel(bview->buffer()->params);
3699 case END_LABEL_FILLED_BOX:
3701 LyXFont const font = GetFont(bview->buffer(),
3702 row_ptr->par(), last);
3703 int const size = int(0.75 * lyxfont::maxAscent(font));
3704 int const y = (y_offset + row_ptr->baseline()) - size;
3705 int x = is_rtl ? LYX_PAPER_MARGIN
3706 : ww - LYX_PAPER_MARGIN - size;
3708 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
3710 LyXFont font(LyXFont::ALL_SANE);
3711 font.setSize(LyXFont::SIZE_SMALL);
3712 x += lyxfont::width("Mwide-figM", font);
3714 x -= LYX_PAPER_MARGIN/2;
3716 if (row_ptr->fill() <= size)
3717 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3718 if (endlabel == END_LABEL_BOX) {
3719 pain.line(x, y, x, y + size,
3721 pain.line(x + size, y, x + size , y + size,
3723 pain.line(x, y, x + size, y,
3725 pain.line(x, y + size, x + size, y + size,
3728 pain.fillRectangle(x, y, size, size,
3732 case END_LABEL_STATIC:
3734 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->GetLayout();
3735 string const tmpstring = textclasslist.
3736 Style(bview->buffer()->params.textclass,
3737 layout).endlabelstring();
3738 font = GetFont(bview->buffer(), row_ptr->par(), -2);
3739 int const tmpx = is_rtl ?
3740 int(x) - lyxfont::width(tmpstring, font)
3741 : ww - RightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3742 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3745 case END_LABEL_NO_LABEL:
3750 /* draw the text in the pixmap */
3752 vpos = row_ptr->pos();
3754 LyXParagraph::size_type main_body =
3755 BeginningOfMainBody(bview->buffer(), row_ptr->par());
3756 if (main_body > 0 &&
3757 (main_body-1 > last ||
3758 !row_ptr->par()->IsLineSeparator(main_body - 1)))
3761 while (vpos <= last) {
3762 pos = vis2log(vpos);
3763 if (main_body > 0 && pos == main_body - 1) {
3764 x += fill_label_hfill
3765 + lyxfont::width(layout.labelsep,
3766 GetFont(bview->buffer(),
3767 row_ptr->par(), -2))
3768 - SingleWidth(bview,
3773 if (row_ptr->par() ->IsHfill(pos)) {
3776 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
3778 y_offset + row_ptr->baseline(),
3781 if (HfillExpansion(bview->buffer(),
3783 if (pos >= main_body) {
3785 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
3786 int(x + fill_hfill),
3787 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
3789 Painter::line_onoffdash);
3793 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
3794 int(x + fill_label_hfill),
3795 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
3797 Painter::line_onoffdash);
3799 x += fill_label_hfill;
3802 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
3804 y_offset + row_ptr->baseline(),
3809 } else if (row_ptr->par()->IsSeparator(pos)) {
3810 x += SingleWidth(bview,
3811 row_ptr->par(), pos);
3812 if (pos >= main_body)
3813 x += fill_separator;
3816 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3821 int LyXText::DefaultHeight() const
3823 LyXFont font(LyXFont::ALL_SANE);
3824 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3828 /* returns the column near the specified x-coordinate of the row
3829 * x is set to the real beginning of this column */
3830 int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
3831 bool & boundary) const
3834 float fill_separator, fill_hfill, fill_label_hfill;
3836 PrepareToPrint(bview, row, tmpx, fill_separator,
3837 fill_hfill, fill_label_hfill);
3839 LyXParagraph::size_type vc = row->pos();
3840 LyXParagraph::size_type last = RowLastPrintable(row);
3841 LyXParagraph::size_type c = 0;
3842 LyXLayout const & layout =
3843 textclasslist.Style(bview->buffer()->params.textclass,
3844 row->par()->GetLayout());
3845 bool left_side = false;
3847 LyXParagraph::size_type
3848 main_body = BeginningOfMainBody(bview->buffer(), row->par());
3849 float last_tmpx = tmpx;
3851 if (main_body > 0 &&
3852 (main_body-1 > last ||
3853 !row->par()->IsLineSeparator(main_body - 1)))
3856 while (vc <= last && tmpx <= x) {
3859 if (main_body > 0 && c == main_body-1) {
3860 tmpx += fill_label_hfill +
3861 lyxfont::width(layout.labelsep,
3862 GetFont(bview->buffer(), row->par(), -2));
3863 if (row->par()->IsLineSeparator(main_body - 1))
3864 tmpx -= SingleWidth(bview, row->par(), main_body-1);
3867 if (HfillExpansion(bview->buffer(), row, c)) {
3868 x += SingleWidth(bview, row->par(), c);
3872 tmpx += fill_label_hfill;
3874 else if (row->par()->IsSeparator(c)) {
3875 tmpx += SingleWidth(bview, row->par(), c);
3877 tmpx+= fill_separator;
3879 tmpx += SingleWidth(bview, row->par(), c);
3883 if ((tmpx + last_tmpx) / 2 > x) {
3888 if (vc > last + 1) // This shouldn't happen.
3892 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3893 // some speedup if rtl_support=false
3894 && (!row->next() || row->next()->par() != row->par());
3895 bool const rtl = (lastrow)
3896 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3897 : false; // If lastrow is false, we don't need to compute
3898 // the value of rtl.
3900 if (row->pos() > last) // Row is empty?
3903 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3904 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3906 else if (vc == row->pos()) {
3908 if (bidi_level(c) % 2 == 1)
3911 c = vis2log(vc - 1);
3912 bool const rtl = (bidi_level(c) % 2 == 1);
3913 if (left_side == rtl) {
3915 boundary = IsBoundary(bview->buffer(), row->par(), c);
3919 if (row->pos() <= last && c > last
3920 && row->par()->IsNewline(last)) {
3921 if (bidi_level(last) % 2 == 0)
3922 tmpx -= SingleWidth(bview, row->par(), last);
3924 tmpx += SingleWidth(bview, row->par(), last);
3935 /* turn the selection into a new environment. If there is no selection,
3936 * create an empty environment */
3937 void LyXText::InsertFootnoteEnvironment(BufferView * bview,
3938 LyXParagraph::footnote_kind kind)
3940 /* no footnoteenvironment in a footnoteenvironment */
3941 if (cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
3942 WriteAlert(_("Impossible operation"),
3943 _("You can't insert a float in a float!"),
3947 /* no marginpars in minipages */
3948 if (kind == LyXParagraph::MARGIN
3949 && cursor.par()->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
3950 WriteAlert(_("Impossible operation"),
3951 _("You can't insert a marginpar in a minipage!"),
3956 /* this doesnt make sense, if there is no selection */
3957 bool dummy_selection = false;
3959 sel_start_cursor = cursor; /* dummy selection */
3960 sel_end_cursor = cursor;
3961 dummy_selection = true;
3964 LyXParagraph * tmppar;
3966 /* a test to make sure there is not already a footnote
3967 * in the selection. */
3969 tmppar = sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
3971 while (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos()) &&
3972 tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
3973 tmppar = tmppar->next;
3975 if (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())
3976 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
3977 WriteAlert(_("Impossible operation"),
3978 _("Float would include float!"),
3983 /* ok we have a selection. This is always between sel_start_cursor
3984 * and sel_end cursor */
3986 SetUndo(bview->buffer(), Undo::FINISH,
3987 sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous,
3988 sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next);
3990 if (sel_end_cursor.pos() > 0
3991 && sel_end_cursor.par()->IsLineSeparator(sel_end_cursor.pos() - 1))
3992 sel_end_cursor.pos(sel_end_cursor.pos() - 1);
3993 /* please break before a space at the end */
3994 if (sel_start_cursor.par() == sel_end_cursor.par()
3995 && sel_start_cursor.pos() > sel_end_cursor.pos())
3996 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
3998 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, sel_end_cursor.pos());
4000 sel_end_cursor.par(sel_end_cursor.par()->Next());
4001 sel_end_cursor.pos(0);
4003 // don't forget to insert a dummy layout paragraph if necessary
4004 if (sel_start_cursor.par()->GetLayout() != sel_end_cursor.par()->layout){
4005 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, 0);
4006 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4007 sel_end_cursor.par(sel_end_cursor.par()->next);
4010 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4012 cursor = sel_end_cursor;
4014 /* please break behind a space, if there is one. The space should
4016 if (sel_start_cursor.pos() > 0
4017 && sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos() - 1))
4018 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4019 if (sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos())) {
4020 sel_start_cursor.par()->Erase(sel_start_cursor.pos());
4023 sel_start_cursor.par()->BreakParagraphConservative(bview->buffer()->params,
4024 sel_start_cursor.pos());
4025 tmppar = sel_start_cursor.par()->Next();
4027 if (dummy_selection) {
4029 if (kind == LyXParagraph::TAB
4030 || kind == LyXParagraph::FIG
4031 || kind == LyXParagraph::WIDE_TAB
4032 || kind == LyXParagraph::WIDE_FIG
4033 || kind == LyXParagraph::ALGORITHM) {
4034 pair<bool, LyXTextClass::size_type> lres =
4035 textclasslist.NumberOfLayout(bview->buffer()->params.textclass,
4037 LyXTextClass::size_type lay;
4043 lay = 0; // use default layout "Standard" (0)
4045 tmppar->SetLayout(bview->buffer()->params, lay);
4048 if (sel_start_cursor.pos() > 0) {
4049 /* the footnote-environment should begin with a standard layout.
4050 * Imagine you insert a footnote within an enumeration, you
4051 * certainly do not want an enumerated footnote! */
4054 /* this is a exception the user would sometimes expect, I hope */
4055 sel_start_cursor.par()->Clear();
4059 while (tmppar != sel_end_cursor.par()) {
4060 tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4061 tmppar->footnotekind = kind;
4062 tmppar = tmppar->Next();
4065 RedoParagraphs(bview, sel_start_cursor, sel_end_cursor.par()->Next());
4067 SetCursor(bview, sel_start_cursor.par()->Next(), 0);
4074 // returns pointer to a specified row
4075 Row * LyXText::GetRow(LyXParagraph * par,
4076 LyXParagraph::size_type pos, int & y) const
4081 Row * tmprow = firstrow;
4084 // find the first row of the specified paragraph
4085 while (tmprow->next() && tmprow->par() != par) {
4086 y += tmprow->height();
4087 tmprow = tmprow->next();
4090 // now find the wanted row
4091 while (tmprow->pos() < pos
4093 && tmprow->next()->par() == par
4094 && tmprow->next()->pos() <= pos) {
4095 y += tmprow->height();
4096 tmprow = tmprow->next();