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"
45 int const LYX_PAPER_MARGIN = 20;
49 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
52 int LyXText::workWidth(BufferView * bview) const
55 return inset_owner->textWidth(bview->painter());
57 return bview->workWidth();
61 int LyXText::GetRealCursorX(BufferView * bview) const
64 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
65 x = the_locking_inset->getLyXText(bview)->GetRealCursorX(bview);
70 unsigned char LyXText::TransformChar(unsigned char c, LyXParagraph * par,
71 LyXParagraph::size_type pos) const
73 if (!Encodings::is_arabic(c))
74 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
75 return c + (0xb0 - '0');
79 unsigned char const prev_char = pos > 0 ? par->GetChar(pos-1) : ' ';
80 unsigned char next_char = ' ';
82 for (LyXParagraph::size_type i = pos+1; i < par->Last(); ++i)
84 for (LyXParagraph::size_type i = pos+1; i < par->size(); ++i)
86 if (!Encodings::IsComposeChar_arabic(par->GetChar(i))) {
87 next_char = par->GetChar(i);
91 if (Encodings::is_arabic(next_char)) {
92 if (Encodings::is_arabic(prev_char))
93 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
95 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
97 if (Encodings::is_arabic(prev_char))
98 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
100 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
104 // This is the comments that some of the warnings below refers to.
105 // There are some issues in this file and I don't think they are
106 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
107 // this is a problem that has been here almost from day one and that a
108 // larger userbase with differenct access patters triggers the bad
109 // behaviour. (segfaults.) What I think happen is: In several places
110 // we store the paragraph in the current cursor and then moves the
111 // cursor. This movement of the cursor will delete paragraph at the
112 // old position if it is now empty. This will make the temporary
113 // pointer to the old cursor paragraph invalid and dangerous to use.
114 // And is some cases this will trigger a segfault. I have marked some
115 // of the cases where this happens with a warning, but I am sure there
116 // are others in this file and in text2.C. There is also a note in
117 // Delete() that you should read. In Delete I store the paragraph->id
118 // instead of a pointer to the paragraph. I am pretty sure this faulty
119 // use of temporary pointers to paragraphs that might have gotten
120 // invalidated (through a cursor movement) before they are used, are
121 // the cause of the strange crashes we get reported often.
123 // It is very tiresom to change this code, especially when it is as
124 // hard to read as it is. Help to fix all the cases where this is done
125 // would be greately appreciated.
129 int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
130 LyXParagraph::size_type pos) const
132 char const c = par->GetChar(pos);
133 return SingleWidth(bview, par, pos, c);
137 int LyXText::SingleWidth(BufferView * bview, LyXParagraph * par,
138 LyXParagraph::size_type pos, char c) const
140 LyXFont font = GetFont(bview->buffer(), par, pos);
142 // The most common case is handled first (Asger)
143 if (IsPrintable(c)) {
144 if (font.language()->RightToLeft()) {
145 if (font.language()->lang() == "arabic" &&
146 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
147 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
148 if (Encodings::IsComposeChar_arabic(c))
151 c = TransformChar(c, par, pos);
152 else if (font.language()->lang() == "hebrew" &&
153 Encodings::IsComposeChar_hebrew(c))
156 return lyxfont::width(c, font);
158 } else if (IsHfillChar(c)) {
159 return 3; /* Because of the representation
160 * as vertical lines */
162 } else if (c == LyXParagraph::META_FOOTNOTE ||
163 c == LyXParagraph::META_MARGIN ||
164 c == LyXParagraph::META_FIG ||
165 c == LyXParagraph::META_TAB ||
166 c == LyXParagraph::META_WIDE_FIG ||
167 c == LyXParagraph::META_WIDE_TAB ||
168 c == LyXParagraph::META_ALGORITHM) {
171 case LyXParagraph::META_MARGIN:
174 case LyXParagraph::META_FIG:
177 case LyXParagraph::META_TAB:
180 case LyXParagraph::META_ALGORITHM:
183 case LyXParagraph::META_WIDE_FIG:
186 case LyXParagraph::META_WIDE_TAB:
189 case LyXParagraph::META_FOOTNOTE:
195 return lyxfont::width(fs, font);
197 } else if (c == LyXParagraph::META_INSET) {
198 Inset * tmpinset = par->GetInset(pos);
200 tmpinset->update(bview, font);
201 return tmpinset->width(bview, font);
205 } else if (IsSeparatorChar(c))
207 else if (IsNewlineChar(c))
209 return lyxfont::width(c, font);
213 // Returns the paragraph position of the last character in the specified row
214 LyXParagraph::size_type LyXText::RowLast(Row const * row) const
217 if (row->next() == 0)
218 return row->par()->Last() - 1;
219 else if (row->next()->par() != row->par())
220 return row->par()->Last() - 1;
222 return row->next()->pos() - 1;
224 if (row->next() == 0)
225 return row->par()->size() - 1;
226 else if (row->next()->par() != row->par())
227 return row->par()->size() - 1;
229 return row->next()->pos() - 1;
234 LyXParagraph::size_type LyXText::RowLastPrintable(Row const * row) const
236 LyXParagraph::size_type const last = RowLast(row);
237 if (last >= row->pos()
239 && row->next()->par() == row->par()
240 && row->par()->IsSeparator(last))
247 void LyXText::ComputeBidiTables(Buffer const * buf, Row * row) const
249 bidi_same_direction = true;
250 if (!lyxrc.rtl_support) {
255 bidi_start = row->pos();
256 bidi_end = RowLastPrintable(row);
258 if (bidi_start > bidi_end) {
263 if (bidi_end + 2 - bidi_start >
264 static_cast<LyXParagraph::size_type>(log2vis_list.size())) {
265 LyXParagraph::size_type new_size =
266 (bidi_end + 2 - bidi_start < 500) ?
267 500 : 2 * (bidi_end + 2 - bidi_start);
268 log2vis_list.resize(new_size);
269 vis2log_list.resize(new_size);
270 bidi_levels.resize(new_size);
273 vis2log_list[bidi_end + 1 - bidi_start] = -1;
274 log2vis_list[bidi_end + 1 - bidi_start] = -1;
276 LyXParagraph::size_type stack[2];
278 row->par()->getParLanguage(buf->params)->RightToLeft();
282 LyXParagraph::size_type const main_body =
283 BeginningOfMainBody(buf, row->par());
285 for (LyXParagraph::size_type lpos = bidi_start;
286 lpos <= bidi_end; ++lpos) {
287 bool is_space = row->par()->IsLineSeparator(lpos);
288 LyXParagraph::size_type const pos =
289 (is_space && lpos + 1 <= bidi_end &&
290 !row->par()->IsLineSeparator(lpos + 1) &&
291 !row->par()->IsNewline(lpos + 1))
293 LyXFont font = row->par()->GetFontSettings(buf->params, pos);
294 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
295 font.number() == LyXFont::ON &&
296 row->par()->GetFontSettings(buf->params, lpos-1).number()
298 font = row->par()->GetFontSettings(buf->params, lpos);
303 bool new_rtl = font.isVisibleRightToLeft();
304 bool new_rtl0 = font.isRightToLeft();
307 if (lpos == main_body - 1
308 && row->pos() < main_body - 1
310 new_level = (rtl_par) ? 1 : 0;
311 new_rtl = new_rtl0 = rtl_par;
313 new_level = (new_rtl) ? 1 : 2;
315 new_level = (rtl_par) ? 2 : 0;
317 if (is_space && new_level >= level) {
323 int new_level2 = new_level;
325 if (level == new_level && rtl0 != new_rtl0) {
327 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
328 } else if (level < new_level) {
329 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
330 if (new_level > rtl_par)
331 bidi_same_direction = false;
333 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
336 bidi_levels[lpos - bidi_start] = new_level;
338 while (level > new_level2) {
339 LyXParagraph::size_type old_lpos =
341 int delta = lpos - old_lpos - 1;
344 log2vis_list[lpos - bidi_start] += delta;
345 log2vis_list[old_lpos - bidi_start] += delta;
347 while (level < new_level)
348 stack[level++] = lpos;
352 LyXParagraph::size_type const old_lpos = stack[--level];
353 int delta = bidi_end - old_lpos;
356 log2vis_list[old_lpos - bidi_start] += delta;
359 LyXParagraph::size_type vpos = bidi_start - 1;
360 for (LyXParagraph::size_type lpos = bidi_start;
361 lpos <= bidi_end; ++lpos) {
362 vpos += log2vis_list[lpos - bidi_start];
363 vis2log_list[vpos - bidi_start] = lpos;
364 log2vis_list[lpos - bidi_start] = vpos;
369 // This method requires a previous call to ComputeBidiTables()
370 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
371 LyXParagraph::size_type pos) const
373 if (!lyxrc.rtl_support || pos == 0)
376 if (!bidi_InRange(pos - 1)) {
377 /// This can happen if pos is the first char of a row.
378 /// Returning false in this case is incorrect!
382 bool const rtl = bidi_level(pos - 1) % 2;
383 bool rtl2 = bidi_InRange(pos)
384 ? bidi_level(pos) % 2
385 : par->isRightToLeftPar(buf->params);
390 bool LyXText::IsBoundary(Buffer const * buf, LyXParagraph * par,
391 LyXParagraph::size_type pos,
392 LyXFont const & font) const
394 if (!lyxrc.rtl_support)
395 return false; // This is just for speedup
397 bool const rtl = font.isVisibleRightToLeft();
398 bool rtl2 = bidi_InRange(pos)
399 ? bidi_level(pos) % 2
400 : par->isRightToLeftPar(buf->params);
405 void LyXText::draw(BufferView * bview, Row const * row,
406 LyXParagraph::size_type & vpos,
407 int offset, float & x, bool cleared)
409 Painter & pain = bview->painter();
411 LyXParagraph::size_type pos = vis2log(vpos);
412 char c = row->par()->GetChar(pos);
415 if (IsNewlineChar(c)) {
417 // Draw end-of-line marker
418 LyXFont const font = GetFont(bview->buffer(), row->par(), pos);
419 int const wid = lyxfont::width('n', font);
420 int const asc = lyxfont::maxAscent(font);
421 int const y = offset + row->baseline();
424 if (bidi_level(pos) % 2 == 0) {
425 xp[0] = int(x + wid * 0.375);
426 yp[0] = int(y - 0.875 * asc * 0.75);
429 yp[1] = int(y - 0.500 * asc * 0.75);
431 xp[2] = int(x + wid * 0.375);
432 yp[2] = int(y - 0.125 * asc * 0.75);
434 pain.lines(xp, yp, 3, LColor::eolmarker);
437 yp[0] = int(y - 0.500 * asc * 0.75);
439 xp[1] = int(x + wid);
440 yp[1] = int(y - 0.500 * asc * 0.75);
442 xp[2] = int(x + wid);
443 yp[2] = int(y - asc * 0.75);
445 pain.lines(xp, yp, 3, LColor::eolmarker);
447 xp[0] = int(x + wid * 0.625);
448 yp[0] = int(y - 0.875 * asc * 0.75);
450 xp[1] = int(x + wid);
451 yp[1] = int(y - 0.500 * asc * 0.75);
453 xp[2] = int(x + wid * 0.625);
454 yp[2] = int(y - 0.125 * asc * 0.75);
456 pain.lines(xp, yp, 3, LColor::eolmarker);
458 xp[0] = int(x + wid);
459 yp[0] = int(y - 0.500 * asc * 0.75);
462 yp[1] = int(y - 0.500 * asc * 0.75);
465 yp[2] = int(y - asc * 0.75);
467 pain.lines(xp, yp, 3, LColor::eolmarker);
473 LyXFont font = GetFont(bview->buffer(), row->par(), pos);
474 LyXFont font2 = font;
476 if (c == LyXParagraph::META_FOOTNOTE
477 || c == LyXParagraph::META_MARGIN
478 || c == LyXParagraph::META_FIG
479 || c == LyXParagraph::META_TAB
480 || c == LyXParagraph::META_WIDE_FIG
481 || c == LyXParagraph::META_WIDE_TAB
482 || c == LyXParagraph::META_ALGORITHM) {
485 case LyXParagraph::META_MARGIN:
488 case LyXParagraph::META_FIG:
491 case LyXParagraph::META_TAB:
494 case LyXParagraph::META_ALGORITHM:
497 case LyXParagraph::META_WIDE_FIG:
500 case LyXParagraph::META_WIDE_TAB:
503 case LyXParagraph::META_FOOTNOTE:
510 // calculate the position of the footnotemark
511 int const y = (row->baseline() - lyxfont::maxAscent(font2)
512 + lyxfont::maxAscent(font));
514 font.setColor(LColor::footnote);
516 // draw it and set new x position
518 pain.text(int(x), offset + y, fs, font);
519 x += lyxfont::width(fs, font);
520 pain.line(int(tmpx), offset + row->baseline(),
521 int(x), offset + row->baseline(),
528 if (c == LyXParagraph::META_INSET) {
529 Inset * tmpinset = row->par()->GetInset(pos);
531 // tmpinset->update(bview, font, false);
532 tmpinset->draw(bview, font, offset+row->baseline(), x,
534 #ifdef SEEMS_TO_BE_NOT_NEEDED
535 if (status == CHANGED_IN_DRAW) {
536 UpdateInset(bview, tmpinset);
537 status = CHANGED_IN_DRAW;
543 if (lyxrc.mark_foreign_language &&
544 font.language() != bview->buffer()->params.language) {
545 int const y = offset + row->height() - 1;
546 pain.line(int(tmpx), y, int(x), y,
553 /* usual characters, no insets */
555 // Collect character that we can draw in one command
557 // This is dirty, but fast. Notice that it will never be too small.
558 // For the record, I'll note that Microsoft Word has a limit
559 // of 768 here. We have none :-) (Asger)
560 // Ok. I am the first to admit that the use of std::string will be
561 // a tiny bit slower than using a POD char array. However, I claim
562 // that this slowdown is so small that it is close to inperceptive.
563 // So IMHO we should go with the easier and clearer implementation.
564 // And even if 1024 is a large number here it might overflow, string
565 // will only overflow if the machine is out of memory...
566 static string textstring;
570 LyXParagraph::size_type const last = RowLastPrintable(row);
572 if (font.language()->lang() == "hebrew") {
573 if (Encodings::IsComposeChar_hebrew(c)) {
574 int const width = lyxfont::width(c, font2);
576 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
577 c = row->par()->GetChar(i);
578 if (!Encodings::IsComposeChar_hebrew(c)) {
579 if (IsPrintableNonspace(c)) {
584 dx = (c == 'ø' || c == 'ã') // dalet / resh
585 ? width2 - width : (width2 - width) / 2;
591 pain.text(int(x) + dx, offset + row->baseline(),
594 while (vpos <= last &&
595 (pos = vis2log(vpos)) >= 0
596 && IsPrintableNonspace(c = row->par()->GetChar(pos))
597 && !Encodings::IsComposeChar_hebrew(c)
598 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
602 // Draw text and set the new x position
603 pain.text(int(x), offset + row->baseline(),
605 x += lyxfont::width(textstring, font);
607 } else if (font.language()->lang() == "arabic" &&
608 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
609 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
610 if (Encodings::IsComposeChar_arabic(c)) {
611 c = TransformChar(c, row->par(), pos);
613 int const width = lyxfont::width(c, font2);
615 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
616 c = row->par()->GetChar(i);
617 if (!Encodings::IsComposeChar_arabic(c)) {
618 if (IsPrintableNonspace(c)) {
623 dx = (width2 - width) / 2;
629 pain.text(int(x) + dx, offset + row->baseline(),
632 textstring = TransformChar(c, row->par(), pos);
633 while (vpos <= last &&
634 (pos = vis2log(vpos)) >= 0
635 && IsPrintableNonspace(c = row->par()->GetChar(pos))
636 && !Encodings::IsComposeChar_arabic(c)
637 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
638 c = TransformChar(c, row->par(), pos);
642 // Draw text and set the new x position
643 pain.text(int(x), offset + row->baseline(),
645 x += lyxfont::width(textstring, font);
648 while (vpos <= last &&
649 (pos = vis2log(vpos)) >= 0
650 && IsPrintableNonspace(c = row->par()->GetChar(pos))
651 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
655 // Draw text and set the new x position
656 pain.text(int(x), offset + row->baseline(), textstring, font);
657 x += lyxfont::width(textstring, font);
660 if (lyxrc.mark_foreign_language &&
661 font.language() != bview->buffer()->params.language) {
662 int const y = offset + row->height() - 1;
663 pain.line(int(tmpx), y, int(x), y,
667 // If we want ulem.sty support, drawing
668 // routines should go here. (Asger)
669 // Why shouldn't LyXFont::drawText handle it internally?
673 // Returns the left beginning of the text.
674 // This information cannot be taken from the layouts-objekt, because in
675 // LaTeX the beginning of the text fits in some cases (for example sections)
676 // exactly the label-width.
677 int LyXText::LeftMargin(BufferView * bview, Row const * row) const
679 LyXLayout const & layout =
680 textclasslist.Style(bview->buffer()->params.textclass,
681 row->par()->GetLayout());
683 string parindent = layout.parindent;
685 int x = LYX_PAPER_MARGIN;
687 x += lyxfont::signedWidth(textclasslist
688 .TextClass(bview->buffer()->params.textclass)
691 .TextClass(bview->buffer()->params.textclass)
694 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
695 LyXFont font(LyXFont::ALL_SANE);
696 font.setSize(LyXFont::SIZE_SMALL);
697 x += lyxfont::width("Mwide-figM", font)
698 + LYX_PAPER_MARGIN/2;
701 // this is the way, LyX handles the LaTeX-Environments.
702 // I have had this idea very late, so it seems to be a
703 // later added hack and this is true
704 if (!row->par()->GetDepth()) {
705 if (!row->par()->GetLayout()) {
706 // find the previous same level paragraph
708 if (row->par()->FirstPhysicalPar()->previous()) {
710 if (row->par()->previous()) {
712 LyXParagraph * newpar = row->par()
713 ->DepthHook(row->par()->GetDepth());
715 textclasslist.Style(bview->buffer()->params.textclass,
722 // find the next level paragraph
724 LyXParagraph * newpar =
725 row->par()->DepthHook(row->par()->GetDepth()-1);
727 // make a corresponding row. Needed to call LeftMargin()
729 // check wether it is a sufficent paragraph
732 && newpar->footnoteflag == row->par()->footnoteflag
735 .Style(bview->buffer()->params.textclass,
736 newpar->GetLayout()).isEnvironment()) {
738 dummyrow.par(newpar);
740 dummyrow.pos(newpar->Last());
742 dummyrow.pos(newpar->size());
744 x = LeftMargin(bview, &dummyrow);
746 // this is no longer an error, because this function
747 // is used to clear impossible depths after changing
748 // a layout. Since there is always a redo,
749 // LeftMargin() is always called
751 row->par()->FirstPhysicalPar()->params.depth(0);
753 row->par()->params.depth(0);
757 if (newpar && !row->par()->GetLayout()) {
759 if (newpar->FirstPhysicalPar()->params.noindent())
761 if (newpar->params.noindent())
765 parindent = textclasslist
766 .Style(bview->buffer()->params.textclass,
767 newpar->GetLayout()).parindent;
772 LyXFont const labelfont = GetFont(bview->buffer(), row->par(), -2);
773 switch (layout.margintype) {
775 if (!layout.leftmargin.empty()) {
776 x += lyxfont::signedWidth(layout.leftmargin,
778 .TextClass(bview->buffer()->params.
782 if (!row->par()->GetLabelstring().empty()) {
783 x += lyxfont::signedWidth(layout.labelindent,
785 x += lyxfont::width(row->par()->GetLabelstring(),
787 x += lyxfont::width(layout.labelsep, labelfont);
791 x += lyxfont::signedWidth(layout.labelindent, labelfont);
792 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
793 if (!row->par()->GetLabelWidthString().empty()) {
794 x += lyxfont::width(row->par()->GetLabelWidthString(),
796 x += lyxfont::width(layout.labelsep, labelfont);
801 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
802 / (row->par()->GetDepth() + 4);
804 case MARGIN_FIRST_DYNAMIC:
805 if (layout.labeltype == LABEL_MANUAL) {
806 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
807 x += lyxfont::signedWidth(layout.leftmargin,
810 x += lyxfont::signedWidth(layout.labelindent,
813 } else if (row->pos()
814 // Special case to fix problems with
816 || (layout.labeltype == LABEL_STATIC
817 && layout.latextype == LATEX_ENVIRONMENT
818 && ! row->par()->IsFirstInSequence())) {
819 x += lyxfont::signedWidth(layout.leftmargin,
821 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
822 && layout.labeltype != LABEL_BIBLIO
823 && layout.labeltype !=
824 LABEL_CENTERED_TOP_ENVIRONMENT) {
825 x += lyxfont::signedWidth(layout.labelindent,
827 x += lyxfont::width(layout.labelsep, labelfont);
828 x += lyxfont::width(row->par()->GetLabelstring(),
833 case MARGIN_RIGHT_ADDRESS_BOX:
835 // ok, a terrible hack. The left margin depends on the widest
836 // row in this paragraph. Do not care about footnotes, they
837 // are *NOT* allowed in the LaTeX realisation of this layout.
839 // find the first row of this paragraph
840 Row const * tmprow = row;
841 while (tmprow->previous()
842 && tmprow->previous()->par() == row->par())
843 tmprow = tmprow->previous();
845 int minfill = tmprow->fill();
846 while (tmprow->next() && tmprow->next()->par() == row->par()) {
847 tmprow = tmprow->next();
848 if (tmprow->fill() < minfill)
849 minfill = tmprow->fill();
852 x += lyxfont::signedWidth(layout.leftmargin,
854 .TextClass(bview->buffer()->params.textclass)
861 if ((workWidth(bview) > 0) &&
862 (row->par()->params.pextraType() == LyXParagraph::PEXTRA_INDENT)) {
863 if (!row->par()->params.pextraWidthp().empty()) {
864 x += workWidth(bview) *
865 lyx::atoi(row->par()->params.pextraWidthp()) / 100;
866 } else if (!row->par()->params.pextraWidth().empty()) {
867 int xx = VSpace(row->par()->params.pextraWidth()).inPixels(bview);
868 if (xx > workWidth(bview))
869 xx = workWidth(bview) * 80 / 100;
871 } else { // should not happen
872 LyXFont font(LyXFont::ALL_SANE);
873 x += lyxfont::width("XXXXXX", font);
878 int align; // wrong type
880 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
881 align = layout.align;
883 align = row->par()->FirstPhysicalPar()->params.align();
885 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
886 align = layout.align;
888 align = row->par()->params.align();
890 // set the correct parindent
891 if (row->pos() == 0) {
892 if ((layout.labeltype == LABEL_NO_LABEL
893 || layout.labeltype == LABEL_TOP_ENVIRONMENT
894 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
895 || (layout.labeltype == LABEL_STATIC
896 && layout.latextype == LATEX_ENVIRONMENT
897 && ! row->par()->IsFirstInSequence()))
899 && row->par() == row->par()->FirstPhysicalPar()
901 && align == LYX_ALIGN_BLOCK
902 && !row->par()->params.noindent()
903 && (row->par()->layout ||
904 bview->buffer()->params.paragraph_separation ==
905 BufferParams::PARSEP_INDENT))
906 x += lyxfont::signedWidth(parindent,
908 .TextClass(bview->buffer()->params
911 else if (layout.labeltype == LABEL_BIBLIO) {
912 // ale970405 Right width for bibitems
913 x += bibitemMaxWidth(bview,textclasslist
914 .TextClass(bview->buffer()->params
923 int LyXText::RightMargin(Buffer const * buf, Row const * row) const
925 LyXLayout const & layout =
926 textclasslist.Style(buf->params.textclass,
927 row->par()->GetLayout());
929 int x = LYX_PAPER_MARGIN
930 + lyxfont::signedWidth(textclasslist
931 .TextClass(buf->params.textclass)
934 .TextClass(buf->params.textclass)
938 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
939 x += LYX_PAPER_MARGIN / 2;
942 // this is the way, LyX handles the LaTeX-Environments.
943 // I have had this idea very late, so it seems to be a
944 // later added hack and this is true
945 if (row->par()->GetDepth()) {
946 // find the next level paragraph
948 LyXParagraph * newpar = row->par();
952 newpar = newpar->FirstPhysicalPar()->previous();
954 newpar = newpar->FirstPhysicalPar();
955 } while (newpar && newpar->GetDepth() >= row->par()->GetDepth()
956 && newpar->footnoteflag == row->par()->footnoteflag);
959 newpar = newpar->previous();
961 && newpar->GetDepth() >= row->par()->GetDepth());
964 // make a corresponding row. Needed to call LeftMargin()
966 // check wether it is a sufficent paragraph
969 && newpar->footnoteflag == row->par()->footnoteflag
971 && textclasslist.Style(buf->params.textclass,
975 dummyrow.par(newpar);
977 x = RightMargin(buf, &dummyrow);
979 // this is no longer an error, because this function
980 // is used to clear impossible depths after changing
981 // a layout. Since there is always a redo,
982 // LeftMargin() is always called
984 row->par()->FirstPhysicalPar()->params.depth(0);
986 row->par()->params.depth(0);
991 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
992 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
993 .TextClass(buf->params.textclass)
994 .defaultfont()) * 4 / (row->par()->GetDepth()
1000 int LyXText::LabelEnd(BufferView * bview, Row const * row) const
1002 if (textclasslist.Style(bview->buffer()->params.textclass,
1003 row->par()->GetLayout()).margintype
1008 tmprow.pos(row->par()->Last());
1010 tmprow.pos(row->par()->size());
1012 return LeftMargin(bview, &tmprow); /* just the beginning
1015 return 0; /* LabelEnd is only needed, if the
1016 layout fills a flushleft
1021 // get the next breakpoint in a given paragraph
1022 LyXParagraph::size_type
1023 LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
1025 LyXParagraph * par = row->par();
1034 LyXParagraph::size_type const pos = row->pos();
1037 // position of the last possible breakpoint
1038 // -1 isn't a suitable value, but a flag
1039 LyXParagraph::size_type last_separator = -1;
1040 width -= RightMargin(bview->buffer(), row);
1042 LyXParagraph::size_type const main_body =
1043 BeginningOfMainBody(bview->buffer(), par);
1044 LyXLayout const & layout =
1045 textclasslist.Style(bview->buffer()->params.textclass, par->GetLayout());
1046 LyXParagraph::size_type i = pos;
1048 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1049 /* special code for right address boxes, only newlines count */
1051 while (i < par->Last()) {
1053 while (i < par->size()) {
1055 if (par->IsNewline(i)) {
1058 i = par->Last() - 1; // this means break
1060 i = par->size() - 1; // this means break
1063 } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
1064 par->GetInset(i) && par->GetInset(i)->display()){
1065 par->GetInset(i)->display(false);
1070 // Last position is an invariant
1072 LyXParagraph::size_type const last =
1075 LyXParagraph::size_type const last =
1078 // this is the usual handling
1079 int x = LeftMargin(bview, row);
1080 bool doitonetime = true;
1081 while (doitonetime || ((x < width) && (i < last))) {
1082 doitonetime = false;
1083 char const c = par->GetChar(i);
1084 if (IsNewlineChar(c)) {
1086 x = width; // this means break
1087 } else if (c == LyXParagraph::META_INSET &&
1090 // check wether a Display() inset is
1091 // valid here. if not, change it to
1093 if (par->GetInset(i)->display() &&
1094 (layout.isCommand() ||
1095 (layout.labeltype == LABEL_MANUAL
1096 && i < BeginningOfMainBody(bview->buffer(), par)))) {
1097 // display istn't allowd
1098 par->GetInset(i)->display(false);
1099 x += SingleWidth(bview, par, i, c);
1100 } else if (par->GetInset(i)->display() ||
1101 par->GetInset(i)->needFullRow()) {
1102 // So break the line here
1106 if (IsLineSeparatorChar(par->GetChar(i+1)))
1109 last_separator = last; // to avoid extra rows
1111 last_separator = i - 1;
1112 x = width; // this means break
1118 x += SingleWidth(bview, par, i, c);
1122 if (IsLineSeparatorChar(c))
1124 x += SingleWidth(bview, par, i, c);
1127 if (i == main_body) {
1128 x += lyxfont::width(layout.labelsep,
1129 GetFont(bview->buffer(), par, -2));
1130 if (par->IsLineSeparator(i - 1))
1131 x-= SingleWidth(bview, par, i - 1);
1132 int left_margin = LabelEnd(bview, row);
1133 if (x < left_margin)
1137 // end of paragraph is always a suitable separator
1138 if (i == last && x < width)
1142 // well, if last_separator is still 0, the line isn't breakable.
1143 // don't care and cut simply at the end
1144 if (last_separator < 0) {
1148 // manual labels cannot be broken in LaTeX, do not care
1149 if (main_body && last_separator < main_body)
1150 last_separator = main_body - 1;
1152 return last_separator;
1156 // returns the minimum space a row needs on the screen in pixel
1157 int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
1159 if (paper_width < 0)
1163 // get the pure distance
1164 LyXParagraph::size_type const last = RowLastPrintable(row);
1166 // special handling of the right address boxes
1167 if (textclasslist.Style(bview->buffer()->params.textclass,
1168 row->par()->GetLayout()).margintype
1169 == MARGIN_RIGHT_ADDRESS_BOX) {
1170 int const tmpfill = row->fill();
1171 row->fill(0); // the minfill in MarginLeft()
1172 w = LeftMargin(bview, row);
1175 w = LeftMargin(bview, row);
1177 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1178 row->par()->GetLayout());
1179 LyXParagraph::size_type const main_body =
1180 BeginningOfMainBody(bview->buffer(), row->par());
1181 LyXParagraph::size_type i = row->pos();
1184 if (main_body > 0 && i == main_body) {
1185 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1186 if (row->par()->IsLineSeparator(i - 1))
1187 w -= SingleWidth(bview, row->par(), i - 1);
1188 int left_margin = LabelEnd(bview, row);
1189 if (w < left_margin)
1192 w += SingleWidth(bview, row->par(), i);
1195 if (main_body > 0 && main_body > last) {
1196 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1197 if (last >= 0 && row->par()->IsLineSeparator(last))
1198 w -= SingleWidth(bview, row->par(), last);
1199 int const left_margin = LabelEnd(bview, row);
1200 if (w < left_margin)
1204 int const fill = paper_width - w - RightMargin(bview->buffer(), row);
1209 // returns the minimum space a manual label needs on the screen in pixel
1210 int LyXText::LabelFill(BufferView * bview, Row const * row) const
1212 LyXParagraph::size_type last = BeginningOfMainBody(bview->buffer(), row->par()) - 1;
1213 // -1 because a label ends either with a space that is in the label,
1214 // or with the beginning of a footnote that is outside the label.
1216 // I don't understand this code in depth, but sometimes "last" is
1217 // less than 0 and this causes a crash. This fix seems to work
1218 // correctly, but I bet the real error is elsewhere. The bug is
1219 // triggered when you have an open footnote in a paragraph
1220 // environment with a manual label. (Asger)
1221 if (last < 0) last = 0;
1223 if (row->par()->IsLineSeparator(last)) /* a sepearator at this end
1230 w += SingleWidth(bview, row->par(), i);
1235 if (!row->par()->params.labelWidthString().empty()) {
1236 fill = max(lyxfont::width(row->par()->params.labelWidthString(),
1237 GetFont(bview->buffer(), row->par(), -2)) - w,
1245 // returns the number of separators in the specified row. The separator
1246 // on the very last column doesnt count
1247 int LyXText::NumberOfSeparators(Buffer const * buf, Row const * row) const
1249 LyXParagraph::size_type const last = RowLast(row);
1250 LyXParagraph::size_type p =
1251 max(row->pos(), BeginningOfMainBody(buf, row->par()));
1253 for (; p < last; ++p) {
1254 if (row->par()->IsSeparator(p)) {
1262 // returns the number of hfills in the specified row. The LyX-Hfill is
1263 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1264 // ignored. This is *MUCH* more usefull than not to ignore!
1265 int LyXText::NumberOfHfills(Buffer const * buf, Row const * row) const
1267 LyXParagraph::size_type const last = RowLast(row);
1268 LyXParagraph::size_type first = row->pos();
1269 if (first) { /* hfill *DO* count at the beginning
1271 while(first <= last && row->par()->IsHfill(first))
1275 first = max(first, BeginningOfMainBody(buf, row->par()));
1277 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1278 if (row->par()->IsHfill(p)) {
1286 // like NumberOfHfills, but only those in the manual label!
1287 int LyXText::NumberOfLabelHfills(Buffer const * buf, Row const * row) const
1289 LyXParagraph::size_type last = RowLast(row);
1290 LyXParagraph::size_type first = row->pos();
1291 if (first) { /* hfill *DO* count at the beginning
1293 while(first < last && row->par()->IsHfill(first))
1297 last = min(last, BeginningOfMainBody(buf, row->par()));
1299 for (LyXParagraph::size_type p = first;
1300 p < last; ++p) { // last, because the end is ignored!
1301 if (row->par()->IsHfill(p)) {
1309 // returns true, if a expansion is needed.
1310 // Rules are given by LaTeX
1311 bool LyXText::HfillExpansion(Buffer const * buf, Row const * row_ptr,
1312 LyXParagraph::size_type pos) const
1314 // by the way, is it a hfill?
1315 if (!row_ptr->par()->IsHfill(pos))
1318 // at the end of a row it does not count
1319 if (pos >= RowLast(row_ptr))
1322 // at the beginning of a row it does not count, if it is not
1323 // the first row of a paragaph
1324 if (!row_ptr->pos())
1327 // in some labels it does not count
1328 if (textclasslist.Style(buf->params.textclass,
1329 row_ptr->par()->GetLayout()).margintype
1331 && pos < BeginningOfMainBody(buf, row_ptr->par()))
1334 // if there is anything between the first char of the row and
1335 // the sepcified position that is not a newline and not a hfill,
1336 // the hfill will count, otherwise not
1337 LyXParagraph::size_type i = row_ptr->pos();
1338 while (i < pos && (row_ptr->par()->IsNewline(i)
1339 || row_ptr->par()->IsHfill(i)))
1346 void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
1348 /* get the maximum ascent and the maximum descent */
1351 float layoutasc = 0;
1352 float layoutdesc = 0;
1355 Inset * tmpinset = 0;
1357 /* this must not happen before the currentrow for clear reasons.
1358 so the trick is just to set the current row onto this row */
1360 GetRow(row_ptr->par(), row_ptr->pos(), unused_y);
1362 /* ok , let us initialize the maxasc and maxdesc value.
1363 * This depends in LaTeX of the font of the last character
1364 * in the paragraph. The hack below is necessary because
1365 * of the possibility of open footnotes */
1367 /* Correction: only the fontsize count. The other properties
1368 are taken from the layoutfont. Nicer on the screen :) */
1370 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
1371 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
1373 LyXParagraph * par = row_ptr->par();
1374 LyXParagraph * firstpar = row_ptr->par();
1377 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1378 firstpar->GetLayout());
1381 LyXFont font = GetFont(bview->buffer(), par, par->Last() - 1);
1383 LyXFont font = GetFont(bview->buffer(), par, par->size() - 1);
1385 LyXFont::FONT_SIZE const size = font.size();
1386 font = GetFont(bview->buffer(), par, -1);
1389 LyXFont labelfont = GetFont(bview->buffer(), par, -2);
1391 float spacing_val = 1.0;
1392 if (!row_ptr->par()->params.spacing().isDefault()) {
1393 spacing_val = row_ptr->par()->params.spacing().getValue();
1395 spacing_val = bview->buffer()->params.spacing.getValue();
1397 //lyxerr << "spacing_val = " << spacing_val << endl;
1399 int maxasc = int(lyxfont::maxAscent(font) *
1400 layout.spacing.getValue() *
1402 int maxdesc = int(lyxfont::maxDescent(font) *
1403 layout.spacing.getValue() *
1405 int const pos_end = RowLast(row_ptr);
1409 // Check if any insets are larger
1410 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1411 if (row_ptr->par()->GetChar(pos) == LyXParagraph::META_INSET) {
1412 tmpfont = GetFont(bview->buffer(), row_ptr->par(), pos);
1413 tmpinset = row_ptr->par()->GetInset(pos);
1415 tmpinset->update(bview, tmpfont);
1416 asc = tmpinset->ascent(bview, tmpfont);
1417 desc = tmpinset->descent(bview, tmpfont);
1418 maxwidth += tmpinset->width(bview, tmpfont);
1419 maxasc = max(maxasc, asc);
1420 maxdesc = max(maxdesc, desc);
1423 maxwidth += SingleWidth(bview, row_ptr->par(), pos);
1427 // Check if any custom fonts are larger (Asger)
1428 // This is not completely correct, but we can live with the small,
1429 // cosmetic error for now.
1430 LyXFont::FONT_SIZE const maxsize =
1431 row_ptr->par()->HighestFontInRange(row_ptr->pos(),
1433 if (maxsize > font.size()) {
1434 font.setSize(maxsize);
1436 asc = lyxfont::maxAscent(font);
1437 desc = lyxfont::maxDescent(font);
1444 // This is nicer with box insets:
1448 row_ptr->ascent_of_text(maxasc);
1450 /* is it a top line? */
1451 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1453 /* some parksips VERY EASY IMPLEMENTATION */
1454 if (bview->buffer()->params.paragraph_separation ==
1455 BufferParams::PARSEP_SKIP) {
1456 if (layout.isParagraph()
1457 && firstpar->GetDepth() == 0
1458 && firstpar->previous())
1459 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1460 else if (firstpar->previous()
1461 && textclasslist.Style(bview->buffer()->params.textclass,
1462 firstpar->previous()->GetLayout()).isParagraph()
1463 && firstpar->previous()->GetDepth() == 0)
1464 // is it right to use defskip here too? (AS)
1465 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1468 /* the paper margins */
1470 if (!row_ptr->par()->previous_ && bv_owner)
1471 maxasc += LYX_PAPER_MARGIN;
1473 if (!row_ptr->par()->previous() && bv_owner)
1474 maxasc += LYX_PAPER_MARGIN;
1477 /* add the vertical spaces, that the user added */
1478 if (firstpar->params.spaceTop().kind() != VSpace::NONE)
1479 maxasc += int(firstpar->params.spaceTop().inPixels(bview));
1481 /* do not forget the DTP-lines!
1482 * there height depends on the font of the nearest character */
1483 if (firstpar->params.lineTop())
1484 maxasc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1487 /* and now the pagebreaks */
1488 if (firstpar->params.pagebreakTop())
1489 maxasc += 3 * DefaultHeight();
1491 /* this is special code for the chapter, since the label of this
1492 * layout is printed in an extra row */
1493 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1494 && bview->buffer()->params.secnumdepth >= 0) {
1495 float spacing_val = 1.0;
1496 if (!row_ptr->par()->params.spacing().isDefault()) {
1497 spacing_val = row_ptr->par()->params.spacing().getValue();
1499 spacing_val = bview->buffer()->params.spacing.getValue();
1502 labeladdon = int(lyxfont::maxDescent(labelfont) *
1503 layout.spacing.getValue() *
1505 + int(lyxfont::maxAscent(labelfont) *
1506 layout.spacing.getValue() *
1510 /* special code for the top label */
1511 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1512 || layout.labeltype == LABEL_BIBLIO
1513 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1514 && row_ptr->par()->IsFirstInSequence()
1515 && !row_ptr->par()->GetLabelstring().empty()) {
1516 float spacing_val = 1.0;
1517 if (!row_ptr->par()->params.spacing().isDefault()) {
1518 spacing_val = row_ptr->par()->params.spacing().getValue();
1520 spacing_val = bview->buffer()->params.spacing.getValue();
1524 (lyxfont::maxAscent(labelfont) *
1525 layout.spacing.getValue() *
1527 +(lyxfont::maxDescent(labelfont) *
1528 layout.spacing.getValue() *
1530 + layout.topsep * DefaultHeight()
1531 + layout.labelbottomsep * DefaultHeight());
1534 /* and now the layout spaces, for example before and after a section,
1535 * or between the items of a itemize or enumerate environment */
1537 if (!firstpar->params.pagebreakTop()) {
1538 LyXParagraph * prev = row_ptr->par()->previous();
1540 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth());
1541 if (prev && prev->GetLayout() == firstpar->GetLayout()
1542 && prev->GetDepth() == firstpar->GetDepth()
1543 && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1545 layoutasc = (layout.itemsep * DefaultHeight());
1547 else if (row_ptr->previous()) {
1548 tmptop = layout.topsep;
1550 if (row_ptr->previous()->par()->GetDepth() >= row_ptr->par()->GetDepth())
1551 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1552 row_ptr->previous()->par()->
1553 GetLayout()).bottomsep;
1556 layoutasc = (tmptop * DefaultHeight());
1558 else if (row_ptr->par()->params.lineTop()) {
1559 tmptop = layout.topsep;
1562 layoutasc = (tmptop * DefaultHeight());
1565 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth()-1);
1567 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1568 prev->GetLayout()).parsep * DefaultHeight());
1571 if (firstpar->previous()
1572 && firstpar->previous()->GetDepth() == 0
1573 && firstpar->previous()->GetLayout() != firstpar->GetLayout()) {
1576 else if (firstpar->previous()){
1577 maxasc += int(layout.parsep * DefaultHeight());
1583 /* is it a bottom line? */
1586 row_ptr->par()->ParFromPos(RowLast(row_ptr) + 1) == par
1588 row_ptr->par() == par
1590 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1592 /* the paper margins */
1594 if (!par->next_ && bv_owner)
1595 maxdesc += LYX_PAPER_MARGIN;
1597 if (!par->next() && bv_owner)
1598 maxdesc += LYX_PAPER_MARGIN;
1601 /* add the vertical spaces, that the user added */
1602 if (firstpar->params.spaceBottom().kind() != VSpace::NONE)
1603 maxdesc += int(firstpar->params.spaceBottom().inPixels(bview));
1605 /* do not forget the DTP-lines!
1606 * there height depends on the font of the nearest character */
1608 if (firstpar->params.lineBottom())
1609 maxdesc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1610 par, par->Last() - 1));
1612 if (firstpar->params.lineBottom())
1613 maxdesc += 2 * lyxfont::ascent('x',
1614 GetFont(bview->buffer(),
1615 par, par->size() - 1));
1618 /* and now the pagebreaks */
1619 if (firstpar->params.pagebreakBottom())
1620 maxdesc += 3 * DefaultHeight();
1622 /* and now the layout spaces, for example before and after a section,
1623 * or between the items of a itemize or enumerate environment */
1624 if (!firstpar->params.pagebreakBottom() && row_ptr->par()->next()) {
1625 LyXParagraph * nextpar = row_ptr->par()->next();
1626 LyXParagraph * comparepar = row_ptr->par();
1630 if (comparepar->GetDepth() > nextpar->GetDepth()) {
1631 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1632 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1633 if (comparepar->GetLayout()!= nextpar->GetLayout()
1634 || nextpar->GetLabelWidthString() !=
1635 comparepar->GetLabelWidthString())
1636 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1638 if (unusual > usual)
1639 layoutdesc = unusual;
1643 else if (comparepar->GetDepth() == nextpar->GetDepth()) {
1645 if (comparepar->GetLayout()!= nextpar->GetLayout()
1646 || nextpar->GetLabelWidthString() !=
1647 comparepar->GetLabelWidthString())
1648 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1653 /* incalculate the layout spaces */
1654 maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1655 maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1657 /* calculate the new height of the text */
1658 height -= row_ptr->height();
1660 row_ptr->height(maxasc + maxdesc + labeladdon);
1661 row_ptr->baseline(maxasc + labeladdon);
1663 height += row_ptr->height();
1665 PrepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1666 row_ptr->width(int(maxwidth + x));
1669 width = max(0,workWidth(bview));
1671 if (r->width() > width)
1679 /* Appends the implicit specified paragraph behind the specified row,
1680 * start at the implicit given position */
1681 void LyXText::AppendParagraph(BufferView * bview, Row * row) const
1683 bool not_ready = true;
1685 // The last character position of a paragraph is an invariant so we can
1686 // safely get it here. (Asger)
1688 int const lastposition = row->par()->Last();
1690 int const lastposition = row->par()->size();
1693 // Get the next breakpoint
1694 int z = NextBreakPoint(bview, row, workWidth(bview));
1698 // Insert the new row
1699 if (z < lastposition) {
1701 InsertRow(row, row->par(), z);
1708 // Set the dimensions of the row
1709 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1710 SetHeightOfRow(bview, tmprow);
1712 } while (not_ready);
1716 void LyXText::BreakAgain(BufferView * bview, Row * row) const
1718 bool not_ready = true;
1721 /* get the next breakpoint */
1722 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1726 if (z < row->par()->Last()) {
1728 if (z < row->par()->size()) {
1730 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1733 InsertRow(row, row->par(), z);
1739 if (row->pos() == z)
1740 not_ready = false; // the rest will not change
1746 /* if there are some rows too much, delete them */
1747 /* only if you broke the whole paragraph! */
1748 Row * tmprow2 = row;
1749 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1750 tmprow2 = tmprow2->next();
1752 while (tmprow2 != row) {
1753 tmprow2 = tmprow2->previous();
1754 RemoveRow(tmprow2->next());
1759 /* set the dimensions of the row */
1760 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1761 SetHeightOfRow(bview, tmprow);
1762 } while (not_ready);
1766 // this is just a little changed version of break again
1767 void LyXText::BreakAgainOneRow(BufferView * bview, Row * row)
1769 // get the next breakpoint
1770 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1774 if (z < row->par()->Last()) {
1776 if (z < row->par()->size()) {
1779 || (row->next() && row->next()->par() != row->par())) {
1780 /* insert a new row */
1782 InsertRow(row, row->par(), z);
1788 if (row->pos() != z)
1792 // if there are some rows too much, delete them
1793 // only if you broke the whole paragraph!
1794 Row * tmprow2 = row;
1795 while (tmprow2->next()
1796 && tmprow2->next()->par() == row->par()) {
1797 tmprow2 = tmprow2->next();
1799 while (tmprow2 != row) {
1800 tmprow2 = tmprow2->previous();
1801 RemoveRow(tmprow2->next());
1805 // set the dimensions of the row
1806 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1807 SetHeightOfRow(bview, tmprow);
1811 void LyXText::BreakParagraph(BufferView * bview, char keep_layout)
1813 LyXLayout const & layout =
1814 textclasslist.Style(bview->buffer()->params.textclass,
1815 cursor.par()->GetLayout());
1817 // this is only allowed, if the current paragraph is not empty or caption
1819 if ((cursor.par()->Last() <= 0
1820 && !cursor.par()->IsDummy())
1821 && layout.labeltype!= LABEL_SENSITIVE)
1824 SetUndo(bview->buffer(), Undo::INSERT,
1825 cursor.par()->ParFromPos(cursor.pos())->previous_,
1826 cursor.par()->ParFromPos(cursor.pos())->next_);
1828 if ((cursor.par()->size() <= 0)
1829 && layout.labeltype!= LABEL_SENSITIVE)
1832 SetUndo(bview->buffer(), Undo::INSERT,
1833 cursor.par()->previous(),
1834 cursor.par()->next());
1837 // Always break behind a space
1839 // It is better to erase the space (Dekel)
1841 if (cursor.pos() < cursor.par()->Last()
1843 if (cursor.pos() < cursor.par()->size()
1845 && cursor.par()->IsLineSeparator(cursor.pos()))
1846 cursor.par()->Erase(cursor.pos());
1847 // cursor.pos(cursor.pos() + 1);
1849 // break the paragraph
1853 keep_layout = layout.isEnvironment();
1854 cursor.par()->BreakParagraph(bview->buffer()->params, cursor.pos(),
1857 // well this is the caption hack since one caption is really enough
1858 if (layout.labeltype == LABEL_SENSITIVE) {
1860 // set to standard-layout
1861 cursor.par()->SetLayout(bview->buffer()->params, 0);
1863 // set to standard-layout
1864 cursor.par()->next()->SetLayout(bview->buffer()->params, 0);
1867 /* if the cursor is at the beginning of a row without prior newline,
1869 * This touches only the screen-update. Otherwise we would may have
1870 * an empty row on the screen */
1871 if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1)
1872 && cursor.row()->pos() == cursor.pos()) {
1876 status = LyXText::NEED_MORE_REFRESH;
1877 refresh_row = cursor.row();
1878 refresh_y = cursor.y() - cursor.row()->baseline();
1880 // Do not forget the special right address boxes
1881 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1882 while (refresh_row->previous() &&
1883 refresh_row->previous()->par() == refresh_row->par()) {
1884 refresh_row = refresh_row->previous();
1885 refresh_y -= refresh_row->height();
1888 RemoveParagraph(cursor.row());
1890 // set the dimensions of the cursor row
1891 cursor.row()->fill(Fill(bview, cursor.row(), workWidth(bview)));
1893 SetHeightOfRow(bview, cursor.row());
1896 while (cursor.par()->next()->Last()
1898 while (cursor.par()->next()->size()
1900 && cursor.par()->next()->IsNewline(0))
1901 cursor.par()->next()->Erase(0);
1903 InsertParagraph(bview, cursor.par()->next(), cursor.row());
1905 UpdateCounters(bview, cursor.row()->previous());
1907 /* This check is necessary. Otherwise the new empty paragraph will
1908 * be deleted automatically. And it is more friendly for the user! */
1910 SetCursor(bview, cursor.par()->next(), 0);
1912 SetCursor(bview, cursor.par(), 0);
1914 if (cursor.row()->next())
1915 BreakAgain(bview, cursor.row()->next());
1922 void LyXText::OpenFootnote(BufferView * bview)
1924 LyXParagraph * endpar,* tmppar;
1927 LyXParagraph * par = cursor.par()->ParFromPos(cursor.pos());
1929 /* if there is no footnote in this paragraph, just return. */
1931 || par->next_->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1934 /* ok, move the cursor right before the footnote */
1936 /* just a little faster than using CursorRight() */
1938 cursor.par()->ParFromPos(cursor.pos()) != par;) {
1939 cursor.pos(cursor.pos() + 1);
1942 /* now the cursor is at the beginning of the physical par */
1943 SetCursor(bview, cursor.par(),
1944 cursor.pos() + cursor.par()->ParFromPos(cursor.pos())->size());
1946 /* the cursor must be exactly before the footnote */
1947 par = cursor.par()->ParFromPos(cursor.pos());
1949 status = LyXText::NEED_MORE_REFRESH;
1950 refresh_row = cursor.row();
1951 refresh_y = cursor.y() - cursor.row()->baseline();
1953 tmppar = cursor.par();
1954 endpar = cursor.par()->next();
1957 tmppar->OpenFootnote(cursor.pos());
1958 RemoveParagraph(row);
1959 /* set the dimensions of the cursor row */
1960 row->fill(Fill(bview, row, workWidth(bview)));
1961 SetHeightOfRow(bview, row);
1962 // CHECK See comment on top of text.C
1963 tmppar = tmppar->next();
1965 while (tmppar != endpar) {
1967 InsertParagraph(bview, tmppar, row);
1968 while (row->next() && row->next()->par() == tmppar)
1970 tmppar = tmppar->next();
1973 SetCursor(bview, par->next_, 0);
1974 sel_cursor = cursor;
1979 // Just a macro to make some thing easier.
1980 void LyXText::RedoParagraph(BufferView * bview) const
1982 ClearSelection(bview);
1983 RedoParagraphs(bview, cursor, cursor.par()->next());
1984 SetCursorIntern(bview, cursor.par(), cursor.pos());
1988 /* insert a character, moves all the following breaks in the
1989 * same Paragraph one to the right and make a rebreak */
1990 void LyXText::InsertChar(BufferView * bview, char c)
1992 SetUndo(bview->buffer(), Undo::INSERT,
1994 cursor.par()->ParFromPos(cursor.pos())->previous_,
1995 cursor.par()->ParFromPos(cursor.pos())->next_
1997 cursor.par()->previous(),
1998 cursor.par()->next()
2002 // When the free-spacing option is set for the current layout,
2003 // disable the double-space checking
2005 bool const freeSpacing =
2006 textclasslist.Style(bview->buffer()->params.textclass,
2007 cursor.row()->par()->GetLayout()).free_spacing;
2010 if (lyxrc.auto_number) {
2011 static string const number_operators = "+-/*";
2012 static string const number_unary_operators = "+-";
2013 static string const number_seperators = ".,:";
2015 if (current_font.number() == LyXFont::ON) {
2016 if (!isdigit(c) && !contains(number_operators, c) &&
2017 !(contains(number_seperators, c) &&
2018 cursor.pos() >= 1 &&
2019 cursor.pos() < cursor.par()->size() &&
2020 GetFont(bview->buffer(),
2022 cursor.pos()).number() == LyXFont::ON &&
2023 GetFont(bview->buffer(),
2025 cursor.pos()-1).number() == LyXFont::ON)
2027 Number(bview); // Set current_font.number to OFF
2028 } else if (isdigit(c) &&
2029 real_current_font.isVisibleRightToLeft()) {
2030 Number(bview); // Set current_font.number to ON
2032 if (cursor.pos() > 0) {
2033 char const c = cursor.par()->GetChar(cursor.pos() - 1);
2034 if (contains(number_unary_operators, c) &&
2035 (cursor.pos() == 1 ||
2036 cursor.par()->IsSeparator(cursor.pos() - 2) ||
2037 cursor.par()->IsNewline(cursor.pos() - 2) )
2039 SetCharFont(bview->buffer(),
2043 } else if (contains(number_seperators, c) &&
2044 cursor.pos() >= 2 &&
2045 GetFont(bview->buffer(),
2047 cursor.pos()-2).number() == LyXFont::ON) {
2048 SetCharFont(bview->buffer(),
2058 /* First check, if there will be two blanks together or a blank at
2059 the beginning of a paragraph.
2060 I decided to handle blanks like normal characters, the main
2061 difference are the special checks when calculating the row.fill
2062 (blank does not count at the end of a row) and the check here */
2064 // The bug is triggered when we type in a description environment:
2065 // The current_font is not changed when we go from label to main text
2066 // and it should (along with realtmpfont) when we type the space.
2067 // CHECK There is a bug here! (Asger)
2069 LyXFont realtmpfont = real_current_font;
2070 LyXFont rawtmpfont = current_font; /* store the current font.
2071 * This is because of the use
2072 * of cursor movements. The moving
2073 * cursor would refresh the
2076 // Get the font that is used to calculate the baselineskip
2078 LyXParagraph::size_type const lastpos = cursor.par()->Last();
2080 LyXParagraph::size_type const lastpos = cursor.par()->size();
2082 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2085 bool jumped_over_space = false;
2087 if (!freeSpacing && IsLineSeparatorChar(c)) {
2088 if ((cursor.pos() > 0
2089 && cursor.par()->IsLineSeparator(cursor.pos() - 1))
2090 || (cursor.pos() > 0
2091 && cursor.par()->IsNewline(cursor.pos() - 1))
2092 || (cursor.pos() == 0
2094 && !(cursor.par()->previous()
2095 && cursor.par()->previous()->footnoteflag
2096 == LyXParagraph::OPEN_FOOTNOTE)
2099 if (cursor.pos() == 0 )
2100 bview->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
2102 bview->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way. Please read the Tutorial."));
2106 } else if (IsNewlineChar(c)) {
2108 if (cursor.par()->FirstPhysicalPar() == cursor.par()
2110 if (cursor.par() == cursor.par()
2112 && cursor.pos() <= BeginningOfMainBody(bview->buffer(), cursor.par())) {
2116 /* No newline at first position
2117 * of a paragraph or behind labels.
2118 * TeX does not allow that. */
2121 if (cursor.pos() < cursor.par()->Last() &&
2123 if (cursor.pos() < cursor.par()->size() &&
2125 cursor.par()->IsLineSeparator(cursor.pos()))
2126 // newline always after a blank!
2128 cursor.row()->fill(-1); // to force a new break
2131 // the display inset stuff
2132 if (cursor.row()->par()->GetChar(cursor.row()->pos()) == LyXParagraph::META_INSET
2133 && cursor.row()->par()->GetInset(cursor.row()->pos())
2134 && (cursor.row()->par()->GetInset(cursor.row()->pos())->display() ||
2135 cursor.row()->par()->GetInset(cursor.row()->pos())->needFullRow()))
2136 cursor.row()->fill(-1); // to force a new break
2138 // get the cursor row fist
2139 Row * row = cursor.row();
2140 int y = cursor.y() - row->baseline();
2141 if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset
2142 * already insertet the character */
2143 cursor.par()->InsertChar(cursor.pos(), c);
2144 SetCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
2146 if (!jumped_over_space) {
2147 // refresh the positions
2149 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2150 tmprow = tmprow->next();
2151 tmprow->pos(tmprow->pos() + 1);
2155 // Is there a break one row above
2156 if ((cursor.par()->IsLineSeparator(cursor.pos())
2157 || cursor.par()->IsNewline(cursor.pos())
2158 || cursor.row()->fill() == -1)
2159 && row->previous() && row->previous()->par() == row->par()) {
2160 LyXParagraph::size_type z = NextBreakPoint(bview,
2163 if (z >= row->pos()) {
2166 // set the dimensions of the row above
2167 row->previous()->fill(Fill(bview,
2171 SetHeightOfRow(bview, row->previous());
2173 y -= row->previous()->height();
2175 refresh_row = row->previous();
2176 status = LyXText::NEED_MORE_REFRESH;
2178 BreakAgainOneRow(bview, row);
2180 current_font = rawtmpfont;
2181 real_current_font = realtmpfont;
2182 SetCursor(bview, cursor.par(), cursor.pos() + 1,
2183 false, cursor.boundary());
2184 // cursor MUST be in row now.
2186 if (row->next() && row->next()->par() == row->par())
2187 need_break_row = row->next();
2191 // check, wether the last characters font has changed.
2193 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2195 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2197 && rawparfont != rawtmpfont)
2198 RedoHeightOfParagraph(bview, cursor);
2205 // recalculate the fill of the row
2206 if (row->fill() >= 0) /* needed because a newline
2207 * will set fill to -1. Otherwise
2208 * we would not get a rebreak! */
2209 row->fill(Fill(bview, row, workWidth(bview)));
2210 if (row->fill() < 0) {
2213 refresh_x = cursor.x();
2214 refresh_pos = cursor.pos();
2215 status = LyXText::NEED_MORE_REFRESH;
2216 BreakAgainOneRow(bview, row);
2217 // will the cursor be in another row now?
2218 if (RowLast(row) <= cursor.pos() + 1 && row->next()) {
2219 if (row->next() && row->next()->par() == row->par())
2220 // this should always be true
2222 BreakAgainOneRow(bview, row);
2224 current_font = rawtmpfont;
2225 real_current_font = realtmpfont;
2227 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2229 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
2230 != cursor.boundary())
2231 SetCursor(bview, cursor.par(), cursor.pos(), false,
2232 !cursor.boundary());
2233 if (row->next() && row->next()->par() == row->par())
2234 need_break_row = row->next();
2239 refresh_x = cursor.x();
2241 refresh_pos = cursor.pos();
2243 int const tmpheight = row->height();
2244 SetHeightOfRow(bview, row);
2245 if (tmpheight == row->height())
2246 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2248 status = LyXText::NEED_MORE_REFRESH;
2250 current_font = rawtmpfont;
2251 real_current_font = realtmpfont;
2252 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2256 // check, wether the last characters font has changed.
2258 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2260 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2262 && rawparfont != rawtmpfont) {
2263 RedoHeightOfParagraph(bview, cursor);
2265 // now the special right address boxes
2266 if (textclasslist.Style(bview->buffer()->params.textclass,
2267 cursor.par()->GetLayout()).margintype
2268 == MARGIN_RIGHT_ADDRESS_BOX) {
2269 RedoDrawingOfParagraph(bview, cursor);
2277 void LyXText::charInserted()
2279 // Here we could call FinishUndo for every 20 characters inserted.
2280 // This is from my experience how emacs does it.
2281 static unsigned int counter = 0;
2291 void LyXText::PrepareToPrint(BufferView * bview,
2292 Row * row, float & x,
2293 float & fill_separator,
2295 float & fill_label_hfill,
2300 float w = row->fill();
2302 fill_label_hfill = 0;
2304 fill_label_hfill = 0;
2307 row->par()->isRightToLeftPar(bview->buffer()->params);
2309 x = (workWidth(bview) > 0)
2310 ? RightMargin(bview->buffer(), row) : 0;
2312 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
2313 LyXFont font(LyXFont::ALL_SANE);
2314 font.setSize(LyXFont::SIZE_SMALL);
2315 x += lyxfont::width("Mwide-figM", font);
2319 x = (workWidth(bview) > 0) ? LeftMargin(bview, row) : 0;
2321 // is there a manual margin with a manual label
2322 if (textclasslist.Style(bview->buffer()->params.textclass,
2323 row->par()->GetLayout()).margintype == MARGIN_MANUAL
2324 && textclasslist.Style(bview->buffer()->params.textclass,
2325 row->par()->GetLayout()).labeltype == LABEL_MANUAL) {
2327 /* one more since labels are left aligned */
2328 nlh = NumberOfLabelHfills(bview->buffer(), row) + 1;
2329 if (nlh && !row->par()->GetLabelWidthString().empty()) {
2330 fill_label_hfill = LabelFill(bview, row) / nlh;
2334 // are there any hfills in the row?
2335 float const nh = NumberOfHfills(bview->buffer(), row);
2338 fill_hfill = w / nh;
2340 // is it block, flushleft or flushright?
2341 // set x how you need it
2344 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
2345 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2347 align = row->par()->FirstPhysicalPar()->params.align();
2349 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
2350 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2352 align = row->par()->params.align();
2355 // center displayed insets
2357 if (row->par()->GetChar(row->pos()) == LyXParagraph::META_INSET
2358 && (inset=row->par()->GetInset(row->pos()))
2359 && (inset->display())) // || (inset->scroll() < 0)))
2360 align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
2361 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2364 case LYX_ALIGN_BLOCK:
2365 ns = NumberOfSeparators(bview->buffer(), row);
2366 if (ns && row->next() && row->next()->par() == row->par() &&
2367 !(row->next()->par()->IsNewline(row->next()->pos() - 1))
2368 && !(row->next()->par()->GetChar(row->next()->pos()) == LyXParagraph::META_INSET
2369 && row->next()->par()->GetInset(row->next()->pos())
2370 && row->next()->par()->GetInset(row->next()->pos())->display())
2372 fill_separator = w / ns;
2376 case LYX_ALIGN_RIGHT:
2379 case LYX_ALIGN_CENTER:
2387 ComputeBidiTables(bview->buffer(), row);
2389 LyXParagraph::size_type main_body =
2390 BeginningOfMainBody(bview->buffer(), row->par());
2391 LyXParagraph::size_type last = RowLast(row);
2393 if (main_body > 0 &&
2394 (main_body-1 > last ||
2395 !row->par()->IsLineSeparator(main_body-1))) {
2396 LyXLayout const & layout =
2397 textclasslist.Style(bview->buffer()->params.textclass,
2398 row->par()->GetLayout());
2399 x += lyxfont::width(layout.labelsep,
2400 GetFont(bview->buffer(), row->par(), -2));
2401 if (main_body-1 <= last)
2402 x += fill_label_hfill;
2407 /* important for the screen */
2410 /* the cursor set functions have a special mechanism. When they
2411 * realize, that you left an empty paragraph, they will delete it.
2412 * They also delete the corresponding row */
2414 void LyXText::CursorRightOneWord(BufferView * bview) const
2416 // treat floats, HFills and Insets as words
2417 LyXCursor tmpcursor = cursor;
2418 // CHECK See comment on top of text.C
2421 if (tmpcursor.pos() == tmpcursor.par()->Last()
2423 if (tmpcursor.pos() == tmpcursor.par()->size()
2425 && tmpcursor.par()->next()) {
2426 tmpcursor.par(tmpcursor.par()->next());
2431 // Skip through initial nonword stuff.
2433 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2435 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2437 ! tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2439 // printf("Current pos1 %d", tmpcursor.pos()) ;
2440 tmpcursor.pos(tmpcursor.pos() + 1);
2443 // Advance through word.
2445 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2447 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2449 tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2451 // printf("Current pos2 %d", tmpcursor.pos()) ;
2452 tmpcursor.pos(tmpcursor.pos() + 1);
2456 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2460 void LyXText::CursorTab(BufferView * bview) const
2462 LyXCursor tmpcursor = cursor;
2464 while (tmpcursor.pos() < tmpcursor.par()->Last()
2466 while (tmpcursor.pos() < tmpcursor.par()->size()
2468 && !tmpcursor.par()->IsNewline(tmpcursor.pos()))
2469 tmpcursor.pos(tmpcursor.pos() + 1);
2472 if (tmpcursor.pos() == tmpcursor.par()->Last()){
2474 if (tmpcursor.pos() == tmpcursor.par()->size()){
2476 if (tmpcursor.par()->next()) {
2477 tmpcursor.par(tmpcursor.par()->next());
2481 tmpcursor.pos(tmpcursor.pos() + 1);
2482 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2486 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2487 of prior word, not to end of next prior word. */
2489 void LyXText::CursorLeftOneWord(BufferView * bview) const
2491 // treat HFills, floats and Insets as words
2492 LyXCursor tmpcursor = cursor;
2493 while (tmpcursor.pos()
2494 && (tmpcursor.par()->IsSeparator(tmpcursor.pos() - 1)
2495 || tmpcursor.par()->IsKomma(tmpcursor.pos() - 1))
2496 && !(tmpcursor.par()->IsHfill(tmpcursor.pos() - 1)
2498 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2500 || tmpcursor.par()->IsInset(tmpcursor.pos() - 1)))
2501 tmpcursor.pos(tmpcursor.pos() - 1);
2504 && (tmpcursor.par()->IsInset(tmpcursor.pos() - 1)
2506 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2508 || tmpcursor.par()->IsHfill(tmpcursor.pos() - 1))) {
2509 tmpcursor.pos(tmpcursor.pos() - 1);
2510 } else if (!tmpcursor.pos()) {
2511 if (tmpcursor.par()->previous()){
2512 tmpcursor.par(tmpcursor.par()->previous());
2514 tmpcursor.pos(tmpcursor.par()->Last());
2516 tmpcursor.pos(tmpcursor.par()->size());
2519 } else { // Here, tmpcursor != 0
2520 while (tmpcursor.pos() > 0 &&
2521 tmpcursor.par()->IsWord(tmpcursor.pos()-1) )
2522 tmpcursor.pos(tmpcursor.pos() - 1);
2524 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2527 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2530 void LyXText::SelectWord(BufferView * bview)
2532 // Move cursor to the beginning, when not already there.
2534 && !cursor.par()->IsSeparator(cursor.pos()-1)
2535 && !cursor.par()->IsKomma(cursor.pos()-1) )
2536 CursorLeftOneWord(bview);
2538 // set the sel cursor
2539 sel_cursor = cursor;
2542 while (cursor.pos() < cursor.par()->Last()
2544 while (cursor.pos() < cursor.par()->size()
2546 && !cursor.par()->IsSeparator(cursor.pos())
2547 && !cursor.par()->IsKomma(cursor.pos()) )
2548 cursor.pos(cursor.pos() + 1);
2549 SetCursor(bview, cursor.par(), cursor.pos() );
2551 // finally set the selection
2552 SetSelection(bview);
2556 /* -------> Select the word currently under the cursor when:
2557 1: no selection is currently set,
2558 2: the cursor is not at the borders of the word. */
2560 bool LyXText::SelectWordWhenUnderCursor(BufferView * bview)
2564 cursor.pos() > 0 && cursor.pos() < cursor.par()->Last()
2566 cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2568 && !cursor.par()->IsSeparator(cursor.pos())
2569 && !cursor.par()->IsKomma(cursor.pos())
2570 && !cursor.par()->IsSeparator(cursor.pos() -1)
2571 && !cursor.par()->IsKomma(cursor.pos() -1)) {
2579 // This function is only used by the spellchecker for NextWord().
2580 // It doesn't handle LYX_ACCENTs and probably never will.
2581 string const LyXText::SelectNextWord(BufferView * bview,
2582 float & value) const
2584 LyXParagraph * tmppar = cursor.par();
2586 // If this is not the very first word, skip rest of
2587 // current word because we are probably in the middle
2588 // of a word if there is text here.
2590 if (cursor.pos() || cursor.par()->previous_) {
2591 while (cursor.pos() < cursor.par()->Last()
2592 && cursor.par()->IsLetter(cursor.pos()))
2593 cursor.pos(cursor.pos() + 1);
2596 if (cursor.pos() || cursor.par()->previous()) {
2597 while (cursor.pos() < cursor.par()->size()
2598 && cursor.par()->IsLetter(cursor.pos()))
2599 cursor.pos(cursor.pos() + 1);
2603 // Now, skip until we have real text (will jump paragraphs)
2605 while ((cursor.par()->Last() > cursor.pos()
2607 while ((cursor.par()->size() > cursor.pos()
2609 && (!cursor.par()->IsLetter(cursor.pos())
2610 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2611 .latex() == LyXFont::ON))
2613 || (cursor.par()->Last() == cursor.pos()
2615 || (cursor.par()->size() == cursor.pos()
2617 && cursor.par()->next())){
2619 if (cursor.pos() == cursor.par()->Last()) {
2621 if (cursor.pos() == cursor.par()->size()) {
2623 cursor.par(cursor.par()->next());
2626 cursor.pos(cursor.pos() + 1);
2629 // Update the value if we changed paragraphs
2630 if (cursor.par() != tmppar){
2631 SetCursor(bview, cursor.par(), cursor.pos());
2632 value = float(cursor.y())/float(height);
2635 // Start the selection from here
2636 sel_cursor = cursor;
2638 std::ostringstream latex;
2640 // and find the end of the word
2641 // (optional hyphens are part of a word)
2643 while (cursor.pos() < cursor.par()->Last()
2645 while (cursor.pos() < cursor.par()->size()
2647 && (cursor.par()->IsLetter(cursor.pos()))
2648 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2649 && cursor.par()->GetInset(cursor.pos()) != 0
2650 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2651 && latex.str() == "\\-"
2653 cursor.pos(cursor.pos() + 1);
2655 // Finally, we copy the word to a string and return it
2657 if (sel_cursor.pos() < cursor.pos()) {
2658 LyXParagraph::size_type i;
2659 for (i = sel_cursor.pos(); i < cursor.pos(); ++i) {
2660 if (cursor.par()->GetChar(i) != LyXParagraph::META_INSET)
2661 str += cursor.par()->GetChar(i);
2668 // This one is also only for the spellchecker
2669 void LyXText::SelectSelectedWord(BufferView * bview)
2671 // move cursor to the beginning
2672 SetCursor(bview, sel_cursor.par(), sel_cursor.pos());
2674 // set the sel cursor
2675 sel_cursor = cursor;
2677 std::ostringstream latex;
2679 // now find the end of the word
2681 while (cursor.pos() < cursor.par()->Last()
2683 while (cursor.pos() < cursor.par()->size()
2685 && (cursor.par()->IsLetter(cursor.pos())
2686 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2687 && cursor.par()->GetInset(cursor.pos()) != 0
2688 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2689 && latex.str() == "\\-"
2691 cursor.pos(cursor.pos() + 1);
2693 SetCursor(bview, cursor.par(), cursor.pos());
2695 // finally set the selection
2696 SetSelection(bview);
2700 /* -------> Delete from cursor up to the end of the current or next word. */
2701 void LyXText::DeleteWordForward(BufferView * bview)
2704 if (!cursor.par()->Last())
2706 if (!cursor.par()->size())
2710 LyXCursor tmpcursor = cursor;
2711 tmpcursor.row(0); //Â ??
2712 selection = true; // to avoid deletion
2713 CursorRightOneWord(bview);
2714 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2715 sel_cursor = cursor;
2717 SetSelection(bview);
2719 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2720 CutSelection(bview);
2725 /* -------> Delete from cursor to start of current or prior word. */
2726 void LyXText::DeleteWordBackward(BufferView * bview)
2729 if (!cursor.par()->Last())
2731 if (!cursor.par()->size())
2735 LyXCursor tmpcursor = cursor;
2736 tmpcursor.row(0); // ??
2737 selection = true; // to avoid deletion
2738 CursorLeftOneWord(bview);
2739 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2740 sel_cursor = cursor;
2742 SetSelection(bview);
2743 CutSelection(bview);
2748 /* -------> Kill to end of line. */
2749 void LyXText::DeleteLineForward(BufferView * bview)
2752 if (!cursor.par()->Last())
2754 if (!cursor.par()->size())
2756 // Paragraph is empty, so we just go to the right
2759 LyXCursor tmpcursor = cursor;
2760 // We can't store the row over a regular SetCursor
2761 // so we set it to 0 and reset it afterwards.
2762 tmpcursor.row(0); //Â ??
2763 selection = true; // to avoid deletion
2765 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2766 sel_cursor = cursor;
2768 SetSelection(bview);
2769 // What is this test for ??? (JMarc)
2771 DeleteWordForward(bview);
2773 CutSelection(bview);
2779 // Change the case of a word at cursor position.
2780 // This function directly manipulates LyXParagraph::text because there
2781 // is no LyXParagraph::SetChar currently. I did what I could to ensure
2782 // that it is correct. I guess part of it should be moved to
2783 // LyXParagraph, but it will have to change for 1.1 anyway. At least
2784 // it does not access outside of the allocated array as the older
2785 // version did. (JMarc)
2786 void LyXText::ChangeWordCase(BufferView * bview, LyXText::TextCase action)
2789 LyXParagraph * tmppar = cursor.par()->ParFromPos(cursor.pos());
2791 SetUndo(bview->buffer(),Undo::FINISH,
2792 tmppar->previous_, tmppar->next_);
2794 LyXParagraph::size_type tmppos =
2795 cursor.par()->PositionInParFromPos(cursor.pos());
2797 LyXParagraph * tmppar = cursor.par();
2799 SetUndo(bview->buffer(),Undo::FINISH,
2800 tmppar->previous(), tmppar->next());
2802 LyXParagraph::size_type tmppos = cursor.pos();
2805 while (tmppos < tmppar->size()) {
2806 unsigned char c = tmppar->GetChar(tmppos);
2807 if (IsKommaChar(c) || IsLineSeparatorChar(c))
2809 if (c != LyXParagraph::META_INSET) {
2811 case text_lowercase:
2814 case text_capitalization:
2816 action = text_lowercase;
2818 case text_uppercase:
2824 //tmppar->text[tmppos] = c;
2825 tmppar->SetChar(tmppos, c);
2828 CheckParagraph(bview, tmppar, tmppos);
2829 CursorRightOneWord(bview);
2833 void LyXText::Delete(BufferView * bview)
2835 // this is a very easy implementation
2837 LyXCursor old_cursor = cursor;
2838 int const old_cur_par_id = old_cursor.par()->id();
2840 int const old_cur_par_prev_id = old_cursor.par()->previous_ ?
2841 old_cursor.par()->previous_->id() : 0;
2843 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2844 old_cursor.par()->previous()->id() : 0;
2847 // just move to the right
2850 // CHECK Look at the comment here.
2851 // This check is not very good...
2852 // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
2853 // and that can very well delete the par or par->previous in
2854 // old_cursor. Will a solution where we compare paragraph id's
2858 (cursor.par()->previous_ ? cursor.par()->previous_->id() : 0)
2860 (cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2862 == old_cur_par_prev_id
2863 && cursor.par()->id() != old_cur_par_id)
2864 return; // delete-empty-paragraph-mechanism has done it
2866 // if you had success make a backspace
2867 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2868 LyXCursor tmpcursor = cursor;
2869 cursor = old_cursor; // to make sure undo gets the right cursor position
2870 SetUndo(bview->buffer(), Undo::DELETE,
2872 cursor.par()->ParFromPos(cursor.pos())->previous_,
2873 cursor.par()->ParFromPos(cursor.pos())->next_
2875 cursor.par()->previous(),
2876 cursor.par()->next()
2885 void LyXText::Backspace(BufferView * bview)
2887 // Get the font that is used to calculate the baselineskip
2889 LyXParagraph::size_type lastpos = cursor.par()->Last();
2891 LyXParagraph::size_type lastpos = cursor.par()->size();
2893 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2896 if (cursor.pos() == 0) {
2897 // The cursor is at the beginning of a paragraph,
2898 // so the the backspace will collapse two paragraphs into one.
2900 // we may paste some paragraphs
2902 // is it an empty paragraph?
2905 || (lastpos == 1 && cursor.par()->IsSeparator(0)))
2907 && !(cursor.par()->next()
2908 && cursor.par()->footnoteflag == LyXParagraph::NO_FOOTNOTE
2909 && cursor.par()->next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
2912 // This is an empty paragraph and we delete it just by moving the cursor one step
2913 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2914 // of the paragraph.
2917 if (cursor.par()->previous_) {
2918 LyXParagraph * tmppar = cursor.par()->previous_->FirstPhysicalPar();
2919 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2920 && cursor.par()->footnoteflag == tmppar->footnoteflag
2922 if (cursor.par()->previous()) {
2923 LyXParagraph * tmppar = cursor.par()->previous();
2924 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2926 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2927 // Inherit bottom DTD from the paragraph below.
2928 // (the one we are deleting)
2929 tmppar->params.lineBottom(cursor.par()->params.lineBottom());
2930 tmppar->params.spaceBottom(cursor.par()->params.spaceBottom());
2931 tmppar->params.pagebreakBottom(cursor.par()->params.pagebreakBottom());
2936 // the layout things can change the height of a row !
2937 int const tmpheight = cursor.row()->height();
2938 SetHeightOfRow(bview, cursor.row());
2939 if (cursor.row()->height() != tmpheight) {
2940 refresh_y = cursor.y() - cursor.row()->baseline();
2941 refresh_row = cursor.row();
2942 status = LyXText::NEED_MORE_REFRESH;
2949 if (cursor.par()->ParFromPos(cursor.pos())->previous_) {
2950 SetUndo(bview->buffer(), Undo::DELETE,
2951 cursor.par()->ParFromPos(cursor.pos())->previous_->previous_,
2952 cursor.par()->ParFromPos(cursor.pos())->next_);
2955 if (cursor.par()->previous()) {
2956 SetUndo(bview->buffer(), Undo::DELETE,
2957 cursor.par()->previous()->previous(),
2958 cursor.par()->next());
2962 LyXParagraph * tmppar = cursor.par();
2963 Row * tmprow = cursor.row();
2965 // We used to do CursorLeftIntern() here, but it is
2966 // not a good idea since it triggers the auto-delete
2967 // mechanism. So we do a CursorLeftIntern()-lite,
2968 // without the dreaded mechanism. (JMarc)
2969 if (cursor.par()->previous()) {
2970 // steps into the above paragraph.
2972 SetCursorIntern(bview, cursor.par()->previous(),
2973 cursor.par()->previous()->Last(), false);
2975 SetCursorIntern(bview, cursor.par()->previous(),
2976 cursor.par()->previous()->size(),
2981 /* Pasting is not allowed, if the paragraphs have different
2982 layout. I think it is a real bug of all other
2983 word processors to allow it. It confuses the user.
2984 Even so with a footnote paragraph and a non-footnote
2985 paragraph. I will not allow pasting in this case,
2986 because the user would be confused if the footnote behaves
2987 different wether it is open or closed.
2989 Correction: Pasting is always allowed with standard-layout
2991 if (cursor.par() != tmppar
2992 && (cursor.par()->GetLayout() == tmppar->GetLayout()
2993 || tmppar->GetLayout() == 0 /*standard*/)
2995 && cursor.par()->footnoteflag == tmppar->footnoteflag
2997 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2999 RemoveParagraph(tmprow);
3001 cursor.par()->PasteParagraph(bview->buffer()->params);
3003 if (!cursor.pos() || !cursor.par()->IsSeparator(cursor.pos() - 1))
3004 ; //cursor.par()->InsertChar(cursor.pos(), ' ');
3005 // strangely enough it seems that commenting out the line above removes
3006 // most or all of the segfaults. I will however also try to move the
3007 // two Remove... lines in front of the PasteParagraph too.
3010 cursor.pos(cursor.pos() - 1);
3012 status = LyXText::NEED_MORE_REFRESH;
3013 refresh_row = cursor.row();
3014 refresh_y = cursor.y() - cursor.row()->baseline();
3016 // remove the lost paragraph
3017 // This one is not safe, since the paragraph that the tmprow and the
3018 // following rows belong to has been deleted by the PasteParagraph
3019 // above. The question is... could this be moved in front of the
3021 //RemoveParagraph(tmprow);
3022 //RemoveRow(tmprow);
3024 // This rebuilds the rows.
3025 AppendParagraph(bview, cursor.row());
3026 UpdateCounters(bview, cursor.row());
3028 // the row may have changed, block, hfills etc.
3029 SetCursor(bview, cursor.par(), cursor.pos(), false);
3032 /* this is the code for a normal backspace, not pasting
3034 SetUndo(bview->buffer(), Undo::DELETE,
3036 cursor.par()->ParFromPos(cursor.pos())->previous_,
3037 cursor.par()->ParFromPos(cursor.pos())->next_
3039 cursor.par()->previous(),
3040 cursor.par()->next()
3043 // We used to do CursorLeftIntern() here, but it is
3044 // not a good idea since it triggers the auto-delete
3045 // mechanism. So we do a CursorLeftIntern()-lite,
3046 // without the dreaded mechanism. (JMarc)
3047 SetCursorIntern(bview, cursor.par(), cursor.pos()- 1,
3048 false, cursor.boundary());
3050 // some insets are undeletable here
3051 if (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
3052 if (!cursor.par()->GetInset(cursor.pos())->Deletable())
3054 // force complete redo when erasing display insets
3055 // this is a cruel method but safe..... Matthias
3056 if (cursor.par()->GetInset(cursor.pos())->display() ||
3057 cursor.par()->GetInset(cursor.pos())->needFullRow()) {
3058 cursor.par()->Erase(cursor.pos());
3059 RedoParagraph(bview);
3064 Row * row = cursor.row();
3065 int y = cursor.y() - row->baseline();
3066 LyXParagraph::size_type z;
3067 /* remember that a space at the end of a row doesnt count
3068 * when calculating the fill */
3069 if (cursor.pos() < RowLast(row) ||
3070 !cursor.par()->IsLineSeparator(cursor.pos())) {
3071 row->fill(row->fill() + SingleWidth(bview,
3076 /* some special code when deleting a newline. This is similar
3077 * to the behavior when pasting paragraphs */
3078 if (cursor.pos() && cursor.par()->IsNewline(cursor.pos())) {
3079 cursor.par()->Erase(cursor.pos());
3080 // refresh the positions
3082 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3083 tmprow = tmprow->next();
3084 tmprow->pos(tmprow->pos() - 1);
3086 if (cursor.par()->IsLineSeparator(cursor.pos() - 1))
3087 cursor.pos(cursor.pos() - 1);
3090 if (cursor.pos() < cursor.par()->Last()
3091 && !cursor.par()->IsSeparator(cursor.pos())) {
3093 if (cursor.pos() < cursor.par()->size()
3094 && !cursor.par()->IsSeparator(cursor.pos())) {
3096 cursor.par()->InsertChar(cursor.pos(), ' ');
3097 SetCharFont(bview->buffer(), cursor.par(),
3098 cursor.pos(), current_font);
3099 // refresh the positions
3101 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3102 tmprow = tmprow->next();
3103 tmprow->pos(tmprow->pos() + 1);
3107 cursor.par()->Erase(cursor.pos());
3109 // refresh the positions
3111 while (tmprow->next()
3112 && tmprow->next()->par() == row->par()) {
3113 tmprow = tmprow->next();
3114 tmprow->pos(tmprow->pos() - 1);
3117 // delete newlines at the beginning of paragraphs
3119 while (cursor.par()->Last() &&
3121 while (cursor.par()->size() &&
3123 cursor.par()->IsNewline(cursor.pos()) &&
3124 cursor.pos() == BeginningOfMainBody(bview->buffer(),
3126 cursor.par()->Erase(cursor.pos());
3127 // refresh the positions
3129 while (tmprow->next() &&
3130 tmprow->next()->par() == row->par()) {
3131 tmprow = tmprow->next();
3132 tmprow->pos(tmprow->pos() - 1);
3137 // is there a break one row above
3138 if (row->previous() && row->previous()->par() == row->par()) {
3139 z = NextBreakPoint(bview, row->previous(),
3141 if (z >= row->pos()) {
3144 Row * tmprow = row->previous();
3146 // maybe the current row is now empty
3148 if (row->pos() >= row->par()->Last()) {
3150 if (row->pos() >= row->par()->size()) {
3156 BreakAgainOneRow(bview, row);
3157 if (row->next() && row->next()->par() == row->par())
3158 need_break_row = row->next();
3163 // set the dimensions of the row above
3164 y -= tmprow->height();
3165 tmprow->fill(Fill(bview, tmprow,
3167 SetHeightOfRow(bview, tmprow);
3170 refresh_row = tmprow;
3171 status = LyXText::NEED_MORE_REFRESH;
3172 SetCursor(bview, cursor.par(), cursor.pos(),
3173 false, cursor.boundary());
3174 //current_font = rawtmpfont;
3175 //real_current_font = realtmpfont;
3176 // check, whether the last character's font has changed.
3179 cursor.par()->GetFontSettings(bview->buffer()->params,
3180 cursor.par()->Last() - 1))
3183 cursor.par()->GetFontSettings(bview->buffer()->params,
3184 cursor.par()->size() - 1))
3186 RedoHeightOfParagraph(bview, cursor);
3191 // break the cursor row again
3192 if (row->next() && row->next()->par() == row->par() &&
3194 (RowLast(row) == row->par()->Last() - 1 ||
3196 (RowLast(row) == row->par()->size() - 1 ||
3198 NextBreakPoint(bview, row, workWidth(bview)) != RowLast(row))) {
3200 /* it can happen that a paragraph loses one row
3201 * without a real breakup. This is when a word
3202 * is to long to be broken. Well, I don t care this
3205 if (RowLast(row) == row->par()->Last() - 1)
3207 if (RowLast(row) == row->par()->size() - 1)
3209 RemoveRow(row->next());
3213 status = LyXText::NEED_MORE_REFRESH;
3215 BreakAgainOneRow(bview, row);
3216 // will the cursor be in another row now?
3217 if (row->next() && row->next()->par() == row->par() &&
3218 RowLast(row) <= cursor.pos()) {
3220 BreakAgainOneRow(bview, row);
3223 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3225 if (row->next() && row->next()->par() == row->par())
3226 need_break_row = row->next();
3230 // set the dimensions of the row
3231 row->fill(Fill(bview, row, workWidth(bview)));
3232 int const tmpheight = row->height();
3233 SetHeightOfRow(bview, row);
3234 if (tmpheight == row->height())
3235 status = LyXText::NEED_VERY_LITTLE_REFRESH;
3237 status = LyXText::NEED_MORE_REFRESH;
3240 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3244 // current_font = rawtmpfont;
3245 // real_current_font = realtmpfont;
3247 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
3248 != cursor.boundary())
3249 SetCursor(bview, cursor.par(), cursor.pos(), false,
3250 !cursor.boundary());
3253 lastpos = cursor.par()->Last();
3255 lastpos = cursor.par()->size();
3257 if (cursor.pos() == lastpos)
3258 SetCurrentFont(bview);
3260 // check, whether the last characters font has changed.
3262 cursor.par()->GetFontSettings(bview->buffer()->params, lastpos - 1)) {
3263 RedoHeightOfParagraph(bview, cursor);
3265 // now the special right address boxes
3266 if (textclasslist.Style(bview->buffer()->params.textclass,
3267 cursor.par()->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3268 RedoDrawingOfParagraph(bview, cursor);
3274 void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
3275 Row * row_ptr, int y, bool cleared)
3277 /* returns a printed row */
3278 Painter & pain = bview->painter();
3281 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
3283 LyXParagraph::size_type const last = RowLastPrintable(row_ptr);
3285 LyXParagraph::size_type vpos, pos;
3291 LyXFont font(LyXFont::ALL_SANE);
3293 if (row_ptr->height() <= 0) {
3294 lyxerr << "LYX_ERROR: row.height: "
3295 << row_ptr->height() << endl;
3299 float fill_separator;
3301 float fill_label_hfill;
3302 PrepareToPrint(bview, row_ptr, x, fill_separator,
3303 fill_hfill, fill_label_hfill);
3305 if (inset_owner && (x < 0))
3309 // clear the area where we want to paint/print
3310 int ww = bview->workWidth();
3312 bool clear_area = true;
3315 if (!bview->screen()->forceClear() && last == row_ptr->pos()
3316 && row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET
3317 && (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
3318 clear_area = inset->doClearArea();
3320 // we don't need to clear it's already done!!!
3323 } else if (clear_area) {
3324 #ifdef WITH_WARNINGS
3325 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
3326 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
3328 int const y = y_offset < 0 ? 0 : y_offset;
3329 int const h = y_offset < 0 ?
3330 row_ptr->height() + y_offset : row_ptr->height();
3331 int const w = inset_owner ?
3332 inset_owner->width(bview, font) - 2 : ww;
3333 int const x = x_offset;
3334 pain.fillRectangle(x, y, w, h);
3335 } else if (inset != 0) {
3336 int h = row_ptr->baseline() - inset->ascent(bview, font);
3338 int const w = (inset_owner ?
3339 inset_owner->width(bview, font) : ww);
3340 pain.fillRectangle(x_offset, y_offset, w, h);
3342 h += inset->ascent(bview, font) + inset->descent(bview, font);
3343 if ((row_ptr->height() - h) > 0) {
3344 int const w = (inset_owner ?
3345 inset_owner->width(bview, font) : ww);
3346 pain.fillRectangle(x_offset, y_offset + h,
3347 w, row_ptr->height() - h);
3349 if (!inset_owner && !inset->display() && !inset->needFullRow())
3351 int const w = inset->width(bview, font) + int(x);
3352 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
3357 int const w = (inset_owner ?
3358 inset_owner->width(bview, font) : ww);
3359 /* selection code */
3360 if (bidi_same_direction) {
3361 if (sel_start_cursor.row() == row_ptr &&
3362 sel_end_cursor.row() == row_ptr) {
3363 if (sel_start_cursor.x() < sel_end_cursor.x())
3364 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3366 sel_end_cursor.x() - sel_start_cursor.x(),
3370 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3372 sel_start_cursor.x() - sel_end_cursor.x(),
3375 } else if (sel_start_cursor.row() == row_ptr) {
3377 pain.fillRectangle(x_offset, y_offset,
3378 sel_start_cursor.x(),
3382 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3384 w - sel_start_cursor.x(),
3387 } else if (sel_end_cursor.row() == row_ptr) {
3389 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3391 w - sel_end_cursor.x(),
3395 pain.fillRectangle(x_offset, y_offset,
3399 } else if (y > sel_start_cursor.y()
3400 && y < sel_end_cursor.y()) {
3401 pain.fillRectangle(x_offset, y_offset, w,
3405 } else if (sel_start_cursor.row() != row_ptr &&
3406 sel_end_cursor.row() != row_ptr &&
3407 y > sel_start_cursor.y()
3408 && y < sel_end_cursor.y()) {
3409 pain.fillRectangle(x_offset, y_offset, w,
3412 } else if (sel_start_cursor.row() == row_ptr ||
3413 sel_end_cursor.row() == row_ptr) {
3415 if ((sel_start_cursor.row() != row_ptr && !is_rtl) ||
3416 (sel_end_cursor.row() != row_ptr && is_rtl))
3417 pain.fillRectangle(x_offset, y_offset,
3421 LyXParagraph::size_type main_body =
3422 BeginningOfMainBody(bview->buffer(),
3425 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3426 pos = vis2log(vpos);
3427 float const old_tmpx = tmpx;
3428 if (main_body > 0 && pos == main_body-1) {
3429 tmpx += fill_label_hfill +
3430 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3431 row_ptr->par()->GetLayout()).labelsep,
3432 GetFont(bview->buffer(),row_ptr->par(), -2));
3433 if (row_ptr->par()->IsLineSeparator(main_body-1))
3434 tmpx -= SingleWidth(bview, row_ptr->par(), main_body-1);
3436 if (HfillExpansion(bview->buffer(), row_ptr, pos)) {
3437 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3438 if (pos >= main_body)
3441 tmpx += fill_label_hfill;
3443 else if (row_ptr->par()->IsSeparator(pos)) {
3444 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3445 if (pos >= main_body)
3446 tmpx += fill_separator;
3448 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3450 if ((sel_start_cursor.row() != row_ptr ||
3451 sel_start_cursor.pos() <= pos) &&
3452 (sel_end_cursor.row() != row_ptr ||
3453 pos < sel_end_cursor.pos()) )
3454 /// Here we do not use x_offset as x_offset was
3456 pain.fillRectangle(int(old_tmpx),
3458 int(tmpx - old_tmpx + 1),
3463 if ((sel_start_cursor.row() != row_ptr && is_rtl) ||
3464 (sel_end_cursor.row() != row_ptr && !is_rtl) )
3465 pain.fillRectangle(x_offset + int(tmpx),
3475 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3476 LyXFont font(LyXFont::ALL_SANE);
3477 font.setSize(LyXFont::SIZE_FOOTNOTE);
3478 font.setColor(LColor::footnote);
3480 box_x = LYX_PAPER_MARGIN + lyxfont::width(" wide-tab ", font);
3481 if (row_ptr->previous() &&
3482 row_ptr->previous()->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3484 switch (row_ptr->par()->footnotekind) {
3485 case LyXParagraph::MARGIN:
3488 case LyXParagraph::FIG:
3491 case LyXParagraph::TAB:
3494 case LyXParagraph::WIDE_FIG:
3497 case LyXParagraph::WIDE_TAB:
3500 case LyXParagraph::ALGORITHM:
3503 case LyXParagraph::FOOTNOTE:
3508 pain.fillRectangle(LYX_PAPER_MARGIN,
3510 box_x - LYX_PAPER_MARGIN,
3511 int(lyxfont::maxAscent(font)
3512 + lyxfont::maxDescent(font)),
3513 LColor::footnotebg);
3515 pain.line(LYX_PAPER_MARGIN, y_offset,
3516 workWidth(bview) - LYX_PAPER_MARGIN, y_offset,
3517 LColor::footnoteframe);
3519 pain.text(LYX_PAPER_MARGIN,
3520 y_offset + int(lyxfont::maxAscent(font)) + 1,
3523 pain.line(LYX_PAPER_MARGIN, y_offset,
3525 y_offset + int(lyxfont::maxAscent(font)
3526 + lyxfont::maxDescent(font)),
3527 LColor::footnoteframe);
3529 pain.line(LYX_PAPER_MARGIN,
3530 y_offset + int(lyxfont::maxAscent(font)
3531 + lyxfont::maxDescent(font)) + 1,
3533 y_offset + int(lyxfont::maxAscent(font)
3534 + lyxfont::maxDescent(font)) + 1,
3535 LColor::footnoteframe);
3539 /* draw the open floats in a red box */
3540 pain.line(box_x, y_offset,
3541 box_x, y_offset + row_ptr->height(),
3542 LColor::footnoteframe);
3544 pain.line(workWidth(bview) - LYX_PAPER_MARGIN,
3546 workWidth(bview) - LYX_PAPER_MARGIN,
3547 y_offset + row_ptr->height(),
3548 LColor::footnoteframe);
3551 // Draw appendix lines
3552 LyXParagraph * p = row_ptr->par()->PreviousBeforeFootnote()->FirstPhysicalPar();
3553 if (p->params.appendix()) {
3554 pain.line(1, y_offset,
3555 1, y_offset + row_ptr->height(),
3556 LColor::appendixline);
3557 pain.line(workWidth(bview) - 2, y_offset,
3558 workWidth(bview) - 2,
3559 y_offset + row_ptr->height(),
3560 LColor::appendixline);
3564 // Draw minipage line
3565 bool const minipage =
3566 (p->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3568 pain.line(LYX_PAPER_MARGIN/5, y_offset,
3570 y_offset + row_ptr->height() - 1,
3571 LColor::minipageline);
3574 int const depth = p->GetDepth();
3575 for (int i = 1; i <= depth; ++i) {
3576 int const line_x = (LYX_PAPER_MARGIN / 5) *
3578 pain.line(line_x, y_offset, line_x,
3579 y_offset + row_ptr->height() - 1,
3582 } else if (row_ptr->previous() &&
3583 row_ptr->previous()->par()->footnoteflag
3584 == LyXParagraph::OPEN_FOOTNOTE) {
3585 LyXFont font(LyXFont::ALL_SANE);
3586 font.setSize(LyXFont::SIZE_FOOTNOTE);
3588 int const box_x = LYX_PAPER_MARGIN
3589 + lyxfont::width(" wide-tab ", font);
3591 pain.line(box_x, y_offset,
3592 workWidth(bview) - LYX_PAPER_MARGIN,
3593 y_offset, LColor::footnote);
3596 // Draw appendix lines
3598 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
3600 LyXParagraph * firstpar = row_ptr->par();
3602 if (firstpar->params.appendix()) {
3603 pain.line(1, y_offset,
3604 1, y_offset + row_ptr->height(),
3605 LColor::appendixline);
3606 pain.line(ww - 2, y_offset,
3607 ww - 2, y_offset + row_ptr->height(),
3608 LColor::appendixline);
3611 // Draw minipage line
3612 bool const minipage =
3613 (firstpar->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3615 pain.line(LYX_PAPER_MARGIN/5 + box_x, y_offset,
3616 LYX_PAPER_MARGIN/5 + box_x,
3617 y_offset + row_ptr->height() - 1,
3618 LColor::minipageline);
3621 int const depth = firstpar->GetDepth();
3625 if (row_ptr->next())
3627 if (row_ptr->par()->footnoteflag ==
3628 row_ptr->next()->par()->footnoteflag)
3629 next_depth = row_ptr->next()->par()->GetDepth();
3630 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3633 next_depth = row_ptr->next()->par()->GetDepth();
3635 if (row_ptr->previous())
3637 if (row_ptr->par()->footnoteflag ==
3638 row_ptr->previous()->par()->footnoteflag)
3639 prev_depth = row_ptr->previous()->par()->GetDepth();
3640 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3643 prev_depth = row_ptr->previous()->par()->GetDepth();
3646 for (int i = 1; i <= depth; ++i) {
3647 int const line_x = (LYX_PAPER_MARGIN / 5) *
3652 ) + box_x + x_offset;
3653 pain.line(line_x, y_offset, line_x,
3654 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3658 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3661 pain.fillRectangle(line_x,
3662 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3663 LYX_PAPER_MARGIN / 5, 2,
3669 LyXLayout const & layout =
3670 textclasslist.Style(bview->buffer()->params.textclass,
3671 row_ptr->par()->GetLayout());
3674 y_bottom = row_ptr->height();
3676 /* is it a first row? */
3677 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3679 /* start of appendix? */
3680 if (row_ptr->par()->params.startOfAppendix()) {
3681 pain.line(1, y_offset,
3683 LColor::appendixline);
3686 /* think about the margins */
3687 if (!row_ptr->previous() && bv_owner)
3688 y_top += LYX_PAPER_MARGIN;
3690 /* draw a top pagebreak */
3691 if (row_ptr->par()->params.pagebreakTop()) {
3693 pb_font.setColor(LColor::pagebreak).decSize();
3694 int w = 0, a = 0, d = 0;
3695 pain.line(0, y_offset + y_top + 2*DefaultHeight(),
3697 y_offset + y_top + 2 * DefaultHeight(),
3699 Painter::line_onoffdash)
3702 _("Page Break (top)"),
3705 LColor::background, false, w, a, d);
3706 pain.rectText((ww - w)/2,
3707 y_offset + y_top + 2 * DefaultHeight() + d,
3708 _("Page Break (top)"),
3711 LColor::background);
3712 y_top += 3 * DefaultHeight();
3715 if (row_ptr->par()->params.spaceTop().kind() == VSpace::VFILL) {
3716 /* draw a vfill top */
3717 pain.line(0, y_offset + 2 + y_top,
3718 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3721 pain.line(0, y_offset + y_top + 3 * DefaultHeight(),
3723 y_offset + y_top + 3 * DefaultHeight(),
3726 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3727 LYX_PAPER_MARGIN / 2,
3728 y_offset + y_top + 3 * DefaultHeight(),
3731 y_top += 3 * DefaultHeight();
3734 /* think about user added space */
3735 y_top += int(row_ptr->par()->params.spaceTop().inPixels(bview));
3737 /* think about the parskip */
3738 /* some parskips VERY EASY IMPLEMENTATION */
3739 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3740 if (layout.latextype == LATEX_PARAGRAPH
3741 && firstpar->GetDepth() == 0
3742 && firstpar->previous())
3743 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3744 else if (firstpar->previous()
3745 && textclasslist.Style(bview->buffer()->params.textclass,
3746 firstpar->previous()->GetLayout()).latextype == LATEX_PARAGRAPH
3747 && firstpar->previous()->GetDepth() == 0)
3748 // is it right to use defskip here, too? (AS)
3749 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3752 if (row_ptr->par()->params.lineTop()) { /* draw a top line */
3753 y_top += lyxfont::ascent('x',
3754 GetFont(bview->buffer(),
3755 row_ptr->par(), 0));
3756 int const w = (inset_owner ?
3757 inset_owner->width(bview, font) : ww);
3758 int const xp = static_cast<int>(inset_owner ? x : 0);
3759 pain.line(xp, y_offset + y_top,
3760 w, y_offset + y_top,
3762 Painter::line_solid,
3763 Painter::line_thick);
3765 y_top += lyxfont::ascent('x',GetFont(bview->buffer(),
3766 row_ptr->par(), 0));
3769 /* should we print a label? */
3770 if (layout.labeltype >= LABEL_STATIC
3771 && (layout.labeltype != LABEL_STATIC
3772 || layout.latextype != LATEX_ENVIRONMENT
3773 || row_ptr->par()->IsFirstInSequence())) {
3774 font = GetFont(bview->buffer(), row_ptr->par(), -2);
3775 if (!row_ptr->par()->GetLabelstring().empty()) {
3777 string const tmpstring =
3778 row_ptr->par()->GetLabelstring();
3780 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3781 if (bview->buffer()->params.secnumdepth >= 0) {
3782 /* this is special code for the chapter layout. This is printed in
3783 * an extra row and has a pagebreak at the top. */
3784 float spacing_val = 1.0;
3785 if (!row_ptr->par()->params.spacing().isDefault()) {
3786 spacing_val = row_ptr->par()->params.spacing().getValue();
3788 spacing_val = bview->buffer()->params.spacing.getValue();
3791 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3792 + int(layout.parsep) * DefaultHeight();
3794 tmpx = ww - LeftMargin(bview, row_ptr) -
3795 lyxfont::width(tmpstring, font);
3796 pain.text(int(tmpx),
3797 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3802 tmpx = ww - LeftMargin(bview, row_ptr)
3803 + lyxfont::width(layout.labelsep, font);
3805 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3806 LyXFont font(LyXFont::ALL_SANE);
3807 font.setSize(LyXFont::SIZE_SMALL);
3808 tmpx += lyxfont::width("Mwide-fixM", font);
3812 tmpx = x - lyxfont::width(layout.labelsep, font)
3813 - lyxfont::width(tmpstring, font);
3816 pain.text(int(tmpx),
3817 y_offset + row_ptr->baseline(),
3821 /* the labels at the top of an environment. More or less for bibliography */
3822 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3823 layout.labeltype == LABEL_BIBLIO ||
3824 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3825 if (row_ptr->par()->IsFirstInSequence()) {
3826 font = GetFont(bview->buffer(),
3827 row_ptr->par(), -2);
3828 if (!row_ptr->par()->GetLabelstring().empty()) {
3829 string const tmpstring =
3830 row_ptr->par()->GetLabelstring();
3831 float spacing_val = 1.0;
3832 if (!row_ptr->par()->params.spacing().isDefault()) {
3833 spacing_val = row_ptr->par()->params.spacing().getValue();
3835 spacing_val = bview->buffer()->params.spacing.getValue();
3838 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3839 + (layout.labelbottomsep * DefaultHeight()));
3842 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3843 tmpx = ( (is_rtl ? LeftMargin(bview, row_ptr) : x)
3844 + ww - RightMargin(bview->buffer(), row_ptr) ) / 2;
3845 tmpx -= lyxfont::width(tmpstring, font) / 2;
3847 tmpx = ww - LeftMargin(bview, row_ptr) -
3848 lyxfont::width(tmpstring, font);
3849 pain.text(int(tmpx),
3850 y_offset + row_ptr->baseline()
3851 - row_ptr->ascent_of_text()
3857 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3858 font = GetFont(bview->buffer(), row_ptr->par(), -1);
3860 tmpx = ww - LeftMargin(bview, row_ptr)
3861 + lyxfont::width(layout.labelsep, font);
3863 tmpx = x - lyxfont::width(layout.labelsep, font)
3864 - row_ptr->par()->bibkey->width(bview, font);
3865 row_ptr->par()->bibkey->draw(bview, font,
3866 y_offset + row_ptr->baseline(),
3871 /* is it a last row? */
3873 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
3875 LyXParagraph * par = row_ptr->par();
3879 row_ptr->par()->ParFromPos(last + 1) == par
3881 row_ptr->par() == par
3883 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
3885 /* think about the margins */
3886 if (!row_ptr->next() && bv_owner)
3887 y_bottom -= LYX_PAPER_MARGIN;
3889 /* draw a bottom pagebreak */
3890 if (firstpar->params.pagebreakBottom()) {
3892 pb_font.setColor(LColor::pagebreak).decSize();
3893 int w = 0, a = 0, d = 0;
3895 y_offset + y_bottom - 2 * DefaultHeight(),
3897 y_offset + y_bottom - 2 * DefaultHeight(),
3899 Painter::line_onoffdash)
3902 _("Page Break (bottom)"),
3905 LColor::background, false, w, a, d);
3906 pain.rectText((ww - w) / 2,
3907 y_offset + y_top + 2 * DefaultHeight() + d,
3908 _("Page Break (bottom)"),
3911 LColor::background);
3912 y_bottom -= 3 * DefaultHeight();
3915 if (firstpar->params.spaceBottom().kind() == VSpace::VFILL) {
3916 /* draw a vfill bottom */
3917 pain.line(0, y_offset + y_bottom - 3 * DefaultHeight(),
3919 y_offset + y_bottom - 3 * DefaultHeight(),
3921 pain.line(0, y_offset + y_bottom - 2,
3923 y_offset + y_bottom - 2,
3925 pain.line(LYX_PAPER_MARGIN / 2,
3926 y_offset + y_bottom - 3 * DefaultHeight(),
3927 LYX_PAPER_MARGIN / 2,
3928 y_offset + y_bottom - 2,
3930 y_bottom -= 3 * DefaultHeight();
3933 /* think about user added space */
3934 y_bottom -= int(firstpar->params.spaceBottom().inPixels(bview));
3936 if (firstpar->params.lineBottom()) {
3937 /* draw a bottom line */
3939 y_bottom -= lyxfont::ascent('x',
3940 GetFont(bview->buffer(),
3944 y_bottom -= lyxfont::ascent('x',
3945 GetFont(bview->buffer(),
3949 int const w = (inset_owner ?
3950 inset_owner->width(bview, font) : ww);
3951 int const xp = static_cast<int>(inset_owner ? x : 0);
3952 pain.line(xp, y_offset + y_bottom,
3953 w, y_offset + y_bottom,
3954 LColor::topline, Painter::line_solid,
3955 Painter::line_thick);
3957 y_bottom -= lyxfont::ascent('x',
3958 GetFont(bview->buffer(),
3962 y_bottom -= lyxfont::ascent('x',
3963 GetFont(bview->buffer(),
3970 int const endlabel =
3971 row_ptr->par()->GetEndLabel(bview->buffer()->params);
3974 case END_LABEL_FILLED_BOX:
3976 LyXFont const font = GetFont(bview->buffer(),
3977 row_ptr->par(), last);
3978 int const size = int(0.75 * lyxfont::maxAscent(font));
3979 int const y = (y_offset + row_ptr->baseline()) - size;
3980 int x = is_rtl ? LYX_PAPER_MARGIN
3981 : ww - LYX_PAPER_MARGIN - size;
3983 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
3985 LyXFont font(LyXFont::ALL_SANE);
3986 font.setSize(LyXFont::SIZE_SMALL);
3987 x += lyxfont::width("Mwide-figM", font);
3989 x -= LYX_PAPER_MARGIN/2;
3991 if (row_ptr->fill() <= size)
3992 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3993 if (endlabel == END_LABEL_BOX) {
3994 pain.line(x, y, x, y + size,
3996 pain.line(x + size, y, x + size , y + size,
3998 pain.line(x, y, x + size, y,
4000 pain.line(x, y + size, x + size, y + size,
4003 pain.fillRectangle(x, y, size, size,
4007 case END_LABEL_STATIC:
4009 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->GetLayout();
4010 string const tmpstring = textclasslist.
4011 Style(bview->buffer()->params.textclass,
4012 layout).endlabelstring();
4013 font = GetFont(bview->buffer(), row_ptr->par(), -2);
4014 int const tmpx = is_rtl ?
4015 int(x) - lyxfont::width(tmpstring, font)
4016 : ww - RightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
4017 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
4020 case END_LABEL_NO_LABEL:
4025 /* draw the text in the pixmap */
4027 vpos = row_ptr->pos();
4029 LyXParagraph::size_type main_body =
4030 BeginningOfMainBody(bview->buffer(), row_ptr->par());
4031 if (main_body > 0 &&
4032 (main_body-1 > last ||
4033 !row_ptr->par()->IsLineSeparator(main_body - 1)))
4036 while (vpos <= last) {
4037 pos = vis2log(vpos);
4038 if (main_body > 0 && pos == main_body - 1) {
4039 x += fill_label_hfill
4040 + lyxfont::width(layout.labelsep,
4041 GetFont(bview->buffer(),
4042 row_ptr->par(), -2))
4043 - SingleWidth(bview,
4048 if (row_ptr->par() ->IsHfill(pos)) {
4051 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4053 y_offset + row_ptr->baseline(),
4056 if (HfillExpansion(bview->buffer(),
4058 if (pos >= main_body) {
4060 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4061 int(x + fill_hfill),
4062 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4064 Painter::line_onoffdash);
4068 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4069 int(x + fill_label_hfill),
4070 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4072 Painter::line_onoffdash);
4074 x += fill_label_hfill;
4077 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4079 y_offset + row_ptr->baseline(),
4084 } else if (row_ptr->par()->IsSeparator(pos)) {
4085 x += SingleWidth(bview,
4086 row_ptr->par(), pos);
4087 if (pos >= main_body)
4088 x += fill_separator;
4091 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
4096 int LyXText::DefaultHeight() const
4098 LyXFont font(LyXFont::ALL_SANE);
4099 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
4103 /* returns the column near the specified x-coordinate of the row
4104 * x is set to the real beginning of this column */
4105 int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
4106 bool & boundary) const
4109 float fill_separator, fill_hfill, fill_label_hfill;
4111 PrepareToPrint(bview, row, tmpx, fill_separator,
4112 fill_hfill, fill_label_hfill);
4114 LyXParagraph::size_type vc = row->pos();
4115 LyXParagraph::size_type last = RowLastPrintable(row);
4116 LyXParagraph::size_type c = 0;
4117 LyXLayout const & layout =
4118 textclasslist.Style(bview->buffer()->params.textclass,
4119 row->par()->GetLayout());
4120 bool left_side = false;
4122 LyXParagraph::size_type
4123 main_body = BeginningOfMainBody(bview->buffer(), row->par());
4124 float last_tmpx = tmpx;
4126 if (main_body > 0 &&
4127 (main_body-1 > last ||
4128 !row->par()->IsLineSeparator(main_body - 1)))
4131 while (vc <= last && tmpx <= x) {
4134 if (main_body > 0 && c == main_body-1) {
4135 tmpx += fill_label_hfill +
4136 lyxfont::width(layout.labelsep,
4137 GetFont(bview->buffer(), row->par(), -2));
4138 if (row->par()->IsLineSeparator(main_body - 1))
4139 tmpx -= SingleWidth(bview, row->par(), main_body-1);
4142 if (HfillExpansion(bview->buffer(), row, c)) {
4143 x += SingleWidth(bview, row->par(), c);
4147 tmpx += fill_label_hfill;
4149 else if (row->par()->IsSeparator(c)) {
4150 tmpx += SingleWidth(bview, row->par(), c);
4152 tmpx+= fill_separator;
4154 tmpx += SingleWidth(bview, row->par(), c);
4158 if ((tmpx + last_tmpx) / 2 > x) {
4163 if (vc > last + 1) // This shouldn't happen.
4167 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
4168 // some speedup if rtl_support=false
4169 && (!row->next() || row->next()->par() != row->par());
4170 bool const rtl = (lastrow)
4171 ? row->par()->isRightToLeftPar(bview->buffer()->params)
4172 : false; // If lastrow is false, we don't need to compute
4173 // the value of rtl.
4175 if (row->pos() > last) // Row is empty?
4178 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
4179 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
4181 else if (vc == row->pos()) {
4183 if (bidi_level(c) % 2 == 1)
4186 c = vis2log(vc - 1);
4187 bool const rtl = (bidi_level(c) % 2 == 1);
4188 if (left_side == rtl) {
4190 boundary = IsBoundary(bview->buffer(), row->par(), c);
4194 if (row->pos() <= last && c > last
4195 && row->par()->IsNewline(last)) {
4196 if (bidi_level(last) % 2 == 0)
4197 tmpx -= SingleWidth(bview, row->par(), last);
4199 tmpx += SingleWidth(bview, row->par(), last);
4210 /* turn the selection into a new environment. If there is no selection,
4211 * create an empty environment */
4212 void LyXText::InsertFootnoteEnvironment(BufferView * bview,
4213 LyXParagraph::footnote_kind kind)
4215 /* no footnoteenvironment in a footnoteenvironment */
4216 if (cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4217 WriteAlert(_("Impossible operation"),
4218 _("You can't insert a float in a float!"),
4223 /* no marginpars in minipages */
4224 if (kind == LyXParagraph::MARGIN
4225 && cursor.par()->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE) {
4226 WriteAlert(_("Impossible operation"),
4227 _("You can't insert a marginpar in a minipage!"),
4232 /* this doesnt make sense, if there is no selection */
4233 bool dummy_selection = false;
4235 sel_start_cursor = cursor; /* dummy selection */
4236 sel_end_cursor = cursor;
4237 dummy_selection = true;
4240 LyXParagraph * tmppar;
4242 /* a test to make sure there is not already a footnote
4243 * in the selection. */
4245 tmppar = sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
4247 while (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos()) &&
4248 tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
4249 tmppar = tmppar->next_;
4251 if (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())
4252 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4253 WriteAlert(_("Impossible operation"),
4254 _("Float would include float!"),
4259 /* ok we have a selection. This is always between sel_start_cursor
4260 * and sel_end cursor */
4262 SetUndo(bview->buffer(), Undo::FINISH,
4263 sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous_,
4264 sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next_);
4266 if (sel_end_cursor.pos() > 0
4267 && sel_end_cursor.par()->IsLineSeparator(sel_end_cursor.pos() - 1))
4268 sel_end_cursor.pos(sel_end_cursor.pos() - 1);
4269 /* please break before a space at the end */
4270 if (sel_start_cursor.par() == sel_end_cursor.par()
4271 && sel_start_cursor.pos() > sel_end_cursor.pos())
4272 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4274 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, sel_end_cursor.pos());
4276 sel_end_cursor.par(sel_end_cursor.par()->next());
4277 sel_end_cursor.pos(0);
4279 // don't forget to insert a dummy layout paragraph if necessary
4280 if (sel_start_cursor.par()->GetLayout() != sel_end_cursor.par()->layout){
4281 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, 0);
4282 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4283 sel_end_cursor.par(sel_end_cursor.par()->next_);
4286 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4288 cursor = sel_end_cursor;
4290 /* please break behind a space, if there is one. The space should
4292 if (sel_start_cursor.pos() > 0
4293 && sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos() - 1))
4294 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4295 if (sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos())) {
4296 sel_start_cursor.par()->Erase(sel_start_cursor.pos());
4299 sel_start_cursor.par()->BreakParagraphConservative(bview->buffer()->params,
4300 sel_start_cursor.pos());
4301 tmppar = sel_start_cursor.par()->next();
4303 if (dummy_selection) {
4305 if (kind == LyXParagraph::TAB
4306 || kind == LyXParagraph::FIG
4307 || kind == LyXParagraph::WIDE_TAB
4308 || kind == LyXParagraph::WIDE_FIG
4309 || kind == LyXParagraph::ALGORITHM) {
4310 pair<bool, LyXTextClass::size_type> lres =
4311 textclasslist.NumberOfLayout(bview->buffer()->params.textclass,
4313 LyXTextClass::size_type lay;
4319 lay = 0; // use default layout "Standard" (0)
4321 tmppar->SetLayout(bview->buffer()->params, lay);
4324 if (sel_start_cursor.pos() > 0) {
4325 /* the footnote-environment should begin with a standard layout.
4326 * Imagine you insert a footnote within an enumeration, you
4327 * certainly do not want an enumerated footnote! */
4330 /* this is a exception the user would sometimes expect, I hope */
4331 sel_start_cursor.par()->Clear();
4335 while (tmppar != sel_end_cursor.par()) {
4336 tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4337 tmppar->footnotekind = kind;
4338 tmppar = tmppar->next();
4341 RedoParagraphs(bview, sel_start_cursor, sel_end_cursor.par()->next());
4343 SetCursor(bview, sel_start_cursor.par()->next(), 0);
4345 ClearSelection(bview);
4350 // returns pointer to a specified row
4351 Row * LyXText::GetRow(LyXParagraph * par,
4352 LyXParagraph::size_type pos, int & y) const
4357 Row * tmprow = firstrow;
4360 // find the first row of the specified paragraph
4361 while (tmprow->next() && tmprow->par() != par) {
4362 y += tmprow->height();
4363 tmprow = tmprow->next();
4366 // now find the wanted row
4367 while (tmprow->pos() < pos
4369 && tmprow->next()->par() == par
4370 && tmprow->next()->pos() <= pos) {
4371 y += tmprow->height();
4372 tmprow = tmprow->next();