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);
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 const 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 const 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 const 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();
425 if (bidi_level(pos) % 2 == 0) {
426 xp[0] = int(x + wid * 0.375);
427 yp[0] = int(y - 0.875 * asc * 0.75);
430 yp[1] = int(y - 0.500 * asc * 0.75);
432 xp[2] = int(x + wid * 0.375);
433 yp[2] = int(y - 0.125 * asc * 0.75);
435 pain.lines(xp, yp, 3, LColor::eolmarker);
438 yp[0] = int(y - 0.500 * asc * 0.75);
440 xp[1] = int(x + wid);
441 yp[1] = int(y - 0.500 * asc * 0.75);
443 xp[2] = int(x + wid);
444 yp[2] = int(y - asc * 0.75);
446 pain.lines(xp, yp, 3, LColor::eolmarker);
448 xp[0] = int(x + wid * 0.625);
449 yp[0] = int(y - 0.875 * asc * 0.75);
451 xp[1] = int(x + wid);
452 yp[1] = int(y - 0.500 * asc * 0.75);
454 xp[2] = int(x + wid * 0.625);
455 yp[2] = int(y - 0.125 * asc * 0.75);
457 pain.lines(xp, yp, 3, LColor::eolmarker);
459 xp[0] = int(x + wid);
460 yp[0] = int(y - 0.500 * asc * 0.75);
463 yp[1] = int(y - 0.500 * asc * 0.75);
466 yp[2] = int(y - asc * 0.75);
468 pain.lines(xp, yp, 3, LColor::eolmarker);
474 LyXFont font = GetFont(bview->buffer(), row->par(), pos);
475 LyXFont font2 = font;
477 if (c == LyXParagraph::META_FOOTNOTE
478 || c == LyXParagraph::META_MARGIN
479 || c == LyXParagraph::META_FIG
480 || c == LyXParagraph::META_TAB
481 || c == LyXParagraph::META_WIDE_FIG
482 || c == LyXParagraph::META_WIDE_TAB
483 || c == LyXParagraph::META_ALGORITHM) {
486 case LyXParagraph::META_MARGIN:
489 case LyXParagraph::META_FIG:
492 case LyXParagraph::META_TAB:
495 case LyXParagraph::META_ALGORITHM:
498 case LyXParagraph::META_WIDE_FIG:
501 case LyXParagraph::META_WIDE_TAB:
504 case LyXParagraph::META_FOOTNOTE:
511 // calculate the position of the footnotemark
512 int const y = (row->baseline() - lyxfont::maxAscent(font2)
513 + lyxfont::maxAscent(font));
515 font.setColor(LColor::footnote);
517 // draw it and set new x position
519 pain.text(int(x), offset + y, fs, font);
520 x += lyxfont::width(fs, font);
521 pain.line(int(tmpx), offset + row->baseline(),
522 int(x), offset + row->baseline(),
529 if (c == LyXParagraph::META_INSET) {
530 Inset * tmpinset = row->par()->GetInset(pos);
532 // tmpinset->update(bview, font, false);
533 tmpinset->draw(bview, font, offset+row->baseline(), x,
535 #ifdef SEEMS_TO_BE_NOT_NEEDED
536 if (status == CHANGED_IN_DRAW) {
537 UpdateInset(bview, tmpinset);
538 status = CHANGED_IN_DRAW;
544 if (lyxrc.mark_foreign_language &&
545 font.language() != bview->buffer()->params.language) {
546 int const y = offset + row->height() - 1;
547 pain.line(int(tmpx), y, int(x), y,
554 /* usual characters, no insets */
556 // Collect character that we can draw in one command
558 // This is dirty, but fast. Notice that it will never be too small.
559 // For the record, I'll note that Microsoft Word has a limit
560 // of 768 here. We have none :-) (Asger)
561 // Ok. I am the first to admit that the use of std::string will be
562 // a tiny bit slower than using a POD char array. However, I claim
563 // that this slowdown is so small that it is close to inperceptive.
564 // So IMHO we should go with the easier and clearer implementation.
565 // And even if 1024 is a large number here it might overflow, string
566 // will only overflow if the machine is out of memory...
567 static string textstring;
571 LyXParagraph::size_type const last = RowLastPrintable(row);
573 if (font.language()->lang() == "hebrew") {
574 if (Encodings::IsComposeChar_hebrew(c)) {
575 int const width = lyxfont::width(c, font2);
577 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
578 c = row->par()->GetChar(i);
579 if (!Encodings::IsComposeChar_hebrew(c)) {
580 if (IsPrintableNonspace(c)) {
585 dx = (c == 'ø' || c == 'ã') // dalet / resh
586 ? width2 - width : (width2 - width) / 2;
592 pain.text(int(x) + dx, offset + row->baseline(),
595 while (vpos <= last &&
596 (pos = vis2log(vpos)) >= 0
597 && IsPrintableNonspace(c = row->par()->GetChar(pos))
598 && !Encodings::IsComposeChar_hebrew(c)
599 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
603 // Draw text and set the new x position
604 pain.text(int(x), offset + row->baseline(),
606 x += lyxfont::width(textstring, font);
608 } else if (font.language()->lang() == "arabic" &&
609 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
610 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
611 if (Encodings::IsComposeChar_arabic(c)) {
612 c = TransformChar(c, row->par(), pos);
614 int const width = lyxfont::width(c, font2);
616 for (LyXParagraph::size_type i = pos-1; i >= 0; --i) {
617 c = row->par()->GetChar(i);
618 if (!Encodings::IsComposeChar_arabic(c)) {
619 if (IsPrintableNonspace(c)) {
624 dx = (width2 - width) / 2;
630 pain.text(int(x) + dx, offset + row->baseline(),
633 textstring = TransformChar(c, row->par(), pos);
634 while (vpos <= last &&
635 (pos = vis2log(vpos)) >= 0
636 && IsPrintableNonspace(c = row->par()->GetChar(pos))
637 && !Encodings::IsComposeChar_arabic(c)
638 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
639 c = TransformChar(c, row->par(), pos);
643 // Draw text and set the new x position
644 pain.text(int(x), offset + row->baseline(),
646 x += lyxfont::width(textstring, font);
649 while (vpos <= last &&
650 (pos = vis2log(vpos)) >= 0
651 && IsPrintableNonspace(c = row->par()->GetChar(pos))
652 && font2 == GetFont(bview->buffer(), row->par(), pos)) {
656 // Draw text and set the new x position
657 pain.text(int(x), offset + row->baseline(), textstring, font);
658 x += lyxfont::width(textstring, font);
661 if (lyxrc.mark_foreign_language &&
662 font.language() != bview->buffer()->params.language) {
663 int const y = offset + row->height() - 1;
664 pain.line(int(tmpx), y, int(x), y,
668 // If we want ulem.sty support, drawing
669 // routines should go here. (Asger)
670 // Why shouldn't LyXFont::drawText handle it internally?
674 // Returns the left beginning of the text.
675 // This information cannot be taken from the layouts-objekt, because in
676 // LaTeX the beginning of the text fits in some cases (for example sections)
677 // exactly the label-width.
678 int LyXText::LeftMargin(BufferView * bview, Row const * row) const
680 LyXLayout const & layout =
681 textclasslist.Style(bview->buffer()->params.textclass,
682 row->par()->GetLayout());
684 string parindent = layout.parindent;
686 int x = LYX_PAPER_MARGIN;
688 x += lyxfont::signedWidth(textclasslist
689 .TextClass(bview->buffer()->params.textclass)
692 .TextClass(bview->buffer()->params.textclass)
695 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
696 LyXFont font(LyXFont::ALL_SANE);
697 font.setSize(LyXFont::SIZE_SMALL);
698 x += lyxfont::width("Mwide-figM", font)
699 + LYX_PAPER_MARGIN/2;
702 // this is the way, LyX handles the LaTeX-Environments.
703 // I have had this idea very late, so it seems to be a
704 // later added hack and this is true
705 if (!row->par()->GetDepth()) {
706 if (!row->par()->GetLayout()) {
707 // find the previous same level paragraph
709 if (row->par()->FirstPhysicalPar()->previous()) {
711 if (row->par()->previous()) {
713 LyXParagraph * newpar = row->par()
714 ->DepthHook(row->par()->GetDepth());
716 textclasslist.Style(bview->buffer()->params.textclass,
723 // find the next level paragraph
725 LyXParagraph * newpar =
726 row->par()->DepthHook(row->par()->GetDepth()-1);
728 // make a corresponding row. Needed to call LeftMargin()
730 // check wether it is a sufficent paragraph
733 && newpar->footnoteflag == row->par()->footnoteflag
736 .Style(bview->buffer()->params.textclass,
737 newpar->GetLayout()).isEnvironment()) {
739 dummyrow.par(newpar);
741 dummyrow.pos(newpar->Last());
743 dummyrow.pos(newpar->size());
745 x = LeftMargin(bview, &dummyrow);
747 // this is no longer an error, because this function
748 // is used to clear impossible depths after changing
749 // a layout. Since there is always a redo,
750 // LeftMargin() is always called
752 row->par()->FirstPhysicalPar()->params.depth(0);
754 row->par()->params.depth(0);
758 if (newpar && !row->par()->GetLayout()) {
760 if (newpar->FirstPhysicalPar()->params.noindent())
762 if (newpar->params.noindent())
766 parindent = textclasslist
767 .Style(bview->buffer()->params.textclass,
768 newpar->GetLayout()).parindent;
773 LyXFont const labelfont = GetFont(bview->buffer(), row->par(), -2);
774 switch (layout.margintype) {
776 if (!layout.leftmargin.empty()) {
777 x += lyxfont::signedWidth(layout.leftmargin,
779 .TextClass(bview->buffer()->params.
783 if (!row->par()->GetLabelstring().empty()) {
784 x += lyxfont::signedWidth(layout.labelindent,
786 x += lyxfont::width(row->par()->GetLabelstring(),
788 x += lyxfont::width(layout.labelsep, labelfont);
792 x += lyxfont::signedWidth(layout.labelindent, labelfont);
793 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
794 if (!row->par()->GetLabelWidthString().empty()) {
795 x += lyxfont::width(row->par()->GetLabelWidthString(),
797 x += lyxfont::width(layout.labelsep, labelfont);
802 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
803 / (row->par()->GetDepth() + 4);
805 case MARGIN_FIRST_DYNAMIC:
806 if (layout.labeltype == LABEL_MANUAL) {
807 if (row->pos() >= BeginningOfMainBody(bview->buffer(), row->par())) {
808 x += lyxfont::signedWidth(layout.leftmargin,
811 x += lyxfont::signedWidth(layout.labelindent,
814 } else if (row->pos()
815 // Special case to fix problems with
817 || (layout.labeltype == LABEL_STATIC
818 && layout.latextype == LATEX_ENVIRONMENT
819 && ! row->par()->IsFirstInSequence())) {
820 x += lyxfont::signedWidth(layout.leftmargin,
822 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
823 && layout.labeltype != LABEL_BIBLIO
824 && layout.labeltype !=
825 LABEL_CENTERED_TOP_ENVIRONMENT) {
826 x += lyxfont::signedWidth(layout.labelindent,
828 x += lyxfont::width(layout.labelsep, labelfont);
829 x += lyxfont::width(row->par()->GetLabelstring(),
834 case MARGIN_RIGHT_ADDRESS_BOX:
836 // ok, a terrible hack. The left margin depends on the widest
837 // row in this paragraph. Do not care about footnotes, they
838 // are *NOT* allowed in the LaTeX realisation of this layout.
840 // find the first row of this paragraph
841 Row const * tmprow = row;
842 while (tmprow->previous()
843 && tmprow->previous()->par() == row->par())
844 tmprow = tmprow->previous();
846 int minfill = tmprow->fill();
847 while (tmprow->next() && tmprow->next()->par() == row->par()) {
848 tmprow = tmprow->next();
849 if (tmprow->fill() < minfill)
850 minfill = tmprow->fill();
853 x += lyxfont::signedWidth(layout.leftmargin,
855 .TextClass(bview->buffer()->params.textclass)
862 if ((workWidth(bview) > 0) &&
863 (row->par()->params.pextraType() == LyXParagraph::PEXTRA_INDENT)) {
864 if (!row->par()->params.pextraWidthp().empty()) {
865 x += workWidth(bview) *
866 lyx::atoi(row->par()->params.pextraWidthp()) / 100;
867 } else if (!row->par()->params.pextraWidth().empty()) {
868 int xx = VSpace(row->par()->params.pextraWidth()).inPixels(bview);
869 if (xx > workWidth(bview))
870 xx = workWidth(bview) * 80 / 100;
872 } else { // should not happen
873 LyXFont font(LyXFont::ALL_SANE);
874 x += lyxfont::width("XXXXXX", font);
879 int align; // wrong type
881 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
882 align = layout.align;
884 align = row->par()->FirstPhysicalPar()->params.align();
886 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
887 align = layout.align;
889 align = row->par()->params.align();
891 // set the correct parindent
892 if (row->pos() == 0) {
893 if ((layout.labeltype == LABEL_NO_LABEL
894 || layout.labeltype == LABEL_TOP_ENVIRONMENT
895 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
896 || (layout.labeltype == LABEL_STATIC
897 && layout.latextype == LATEX_ENVIRONMENT
898 && ! row->par()->IsFirstInSequence()))
900 && row->par() == row->par()->FirstPhysicalPar()
902 && align == LYX_ALIGN_BLOCK
903 && !row->par()->params.noindent()
904 && (row->par()->layout ||
905 bview->buffer()->params.paragraph_separation ==
906 BufferParams::PARSEP_INDENT))
907 x += lyxfont::signedWidth(parindent,
909 .TextClass(bview->buffer()->params
912 else if (layout.labeltype == LABEL_BIBLIO) {
913 // ale970405 Right width for bibitems
914 x += bibitemMaxWidth(bview,textclasslist
915 .TextClass(bview->buffer()->params
924 int LyXText::RightMargin(Buffer const * buf, Row const * row) const
926 LyXLayout const & layout =
927 textclasslist.Style(buf->params.textclass,
928 row->par()->GetLayout());
930 int x = LYX_PAPER_MARGIN
931 + lyxfont::signedWidth(textclasslist
932 .TextClass(buf->params.textclass)
935 .TextClass(buf->params.textclass)
939 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
940 x += LYX_PAPER_MARGIN / 2;
943 // this is the way, LyX handles the LaTeX-Environments.
944 // I have had this idea very late, so it seems to be a
945 // later added hack and this is true
946 if (row->par()->GetDepth()) {
947 // find the next level paragraph
949 LyXParagraph * newpar = row->par();
953 newpar = newpar->FirstPhysicalPar()->previous();
955 newpar = newpar->FirstPhysicalPar();
956 } while (newpar && newpar->GetDepth() >= row->par()->GetDepth()
957 && newpar->footnoteflag == row->par()->footnoteflag);
960 newpar = newpar->previous();
962 && newpar->GetDepth() >= row->par()->GetDepth());
965 // make a corresponding row. Needed to call LeftMargin()
967 // check wether it is a sufficent paragraph
970 && newpar->footnoteflag == row->par()->footnoteflag
972 && textclasslist.Style(buf->params.textclass,
976 dummyrow.par(newpar);
978 x = RightMargin(buf, &dummyrow);
980 // this is no longer an error, because this function
981 // is used to clear impossible depths after changing
982 // a layout. Since there is always a redo,
983 // LeftMargin() is always called
985 row->par()->FirstPhysicalPar()->params.depth(0);
987 row->par()->params.depth(0);
992 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
993 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
994 .TextClass(buf->params.textclass)
995 .defaultfont()) * 4 / (row->par()->GetDepth()
1001 int LyXText::LabelEnd(BufferView * bview, Row const * row) const
1003 if (textclasslist.Style(bview->buffer()->params.textclass,
1004 row->par()->GetLayout()).margintype
1009 tmprow.pos(row->par()->Last());
1011 tmprow.pos(row->par()->size());
1013 return LeftMargin(bview, &tmprow); /* just the beginning
1016 return 0; /* LabelEnd is only needed, if the
1017 layout fills a flushleft
1022 // get the next breakpoint in a given paragraph
1023 LyXParagraph::size_type
1024 LyXText::NextBreakPoint(BufferView * bview, Row const * row, int width) const
1026 LyXParagraph * par = row->par();
1035 LyXParagraph::size_type const pos = row->pos();
1038 // position of the last possible breakpoint
1039 // -1 isn't a suitable value, but a flag
1040 LyXParagraph::size_type last_separator = -1;
1041 width -= RightMargin(bview->buffer(), row);
1043 LyXParagraph::size_type const main_body =
1044 BeginningOfMainBody(bview->buffer(), par);
1045 LyXLayout const & layout =
1046 textclasslist.Style(bview->buffer()->params.textclass, par->GetLayout());
1047 LyXParagraph::size_type i = pos;
1049 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1050 /* special code for right address boxes, only newlines count */
1052 while (i < par->Last()) {
1054 while (i < par->size()) {
1056 if (par->IsNewline(i)) {
1059 i = par->Last() - 1; // this means break
1061 i = par->size() - 1; // this means break
1064 } else if (par->GetChar(i) == LyXParagraph::META_INSET &&
1065 par->GetInset(i) && par->GetInset(i)->display()){
1066 par->GetInset(i)->display(false);
1071 // Last position is an invariant
1073 LyXParagraph::size_type const last =
1076 LyXParagraph::size_type const last =
1079 // this is the usual handling
1080 int x = LeftMargin(bview, row);
1081 bool doitonetime = true;
1082 while (doitonetime || ((x < width) && (i < last))) {
1083 doitonetime = false;
1084 char const c = par->GetChar(i);
1085 if (IsNewlineChar(c)) {
1087 x = width; // this means break
1088 } else if (c == LyXParagraph::META_INSET &&
1091 // check wether a Display() inset is
1092 // valid here. if not, change it to
1094 if (par->GetInset(i)->display() &&
1095 (layout.isCommand() ||
1096 (layout.labeltype == LABEL_MANUAL
1097 && i < BeginningOfMainBody(bview->buffer(), par)))) {
1098 // display istn't allowd
1099 par->GetInset(i)->display(false);
1100 x += SingleWidth(bview, par, i, c);
1101 } else if (par->GetInset(i)->display() ||
1102 par->GetInset(i)->needFullRow()) {
1103 // So break the line here
1107 if (IsLineSeparatorChar(par->GetChar(i+1)))
1110 last_separator = last; // to avoid extra rows
1112 last_separator = i - 1;
1113 x = width; // this means break
1119 x += SingleWidth(bview, par, i, c);
1123 if (IsLineSeparatorChar(c))
1125 x += SingleWidth(bview, par, i, c);
1128 if (i == main_body) {
1129 x += lyxfont::width(layout.labelsep,
1130 GetFont(bview->buffer(), par, -2));
1131 if (par->IsLineSeparator(i - 1))
1132 x-= SingleWidth(bview, par, i - 1);
1133 int left_margin = LabelEnd(bview, row);
1134 if (x < left_margin)
1138 // end of paragraph is always a suitable separator
1139 if (i == last && x < width)
1143 // well, if last_separator is still 0, the line isn't breakable.
1144 // don't care and cut simply at the end
1145 if (last_separator < 0) {
1149 // manual labels cannot be broken in LaTeX, do not care
1150 if (main_body && last_separator < main_body)
1151 last_separator = main_body - 1;
1153 return last_separator;
1157 // returns the minimum space a row needs on the screen in pixel
1158 int LyXText::Fill(BufferView * bview, Row * row, int paper_width) const
1160 if (paper_width < 0)
1164 // get the pure distance
1165 LyXParagraph::size_type const last = RowLastPrintable(row);
1167 // special handling of the right address boxes
1168 if (textclasslist.Style(bview->buffer()->params.textclass,
1169 row->par()->GetLayout()).margintype
1170 == MARGIN_RIGHT_ADDRESS_BOX) {
1171 int const tmpfill = row->fill();
1172 row->fill(0); // the minfill in MarginLeft()
1173 w = LeftMargin(bview, row);
1176 w = LeftMargin(bview, row);
1178 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1179 row->par()->GetLayout());
1180 LyXParagraph::size_type const main_body =
1181 BeginningOfMainBody(bview->buffer(), row->par());
1182 LyXParagraph::size_type i = row->pos();
1185 if (main_body > 0 && i == main_body) {
1186 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1187 if (row->par()->IsLineSeparator(i - 1))
1188 w -= SingleWidth(bview, row->par(), i - 1);
1189 int left_margin = LabelEnd(bview, row);
1190 if (w < left_margin)
1193 w += SingleWidth(bview, row->par(), i);
1196 if (main_body > 0 && main_body > last) {
1197 w += lyxfont::width(layout.labelsep, GetFont(bview->buffer(), row->par(), -2));
1198 if (last >= 0 && row->par()->IsLineSeparator(last))
1199 w -= SingleWidth(bview, row->par(), last);
1200 int const left_margin = LabelEnd(bview, row);
1201 if (w < left_margin)
1205 int const fill = paper_width - w - RightMargin(bview->buffer(), row);
1210 // returns the minimum space a manual label needs on the screen in pixel
1211 int LyXText::LabelFill(BufferView * bview, Row const * row) const
1213 LyXParagraph::size_type last = BeginningOfMainBody(bview->buffer(), row->par()) - 1;
1214 // -1 because a label ends either with a space that is in the label,
1215 // or with the beginning of a footnote that is outside the label.
1217 // I don't understand this code in depth, but sometimes "last" is
1218 // less than 0 and this causes a crash. This fix seems to work
1219 // correctly, but I bet the real error is elsewhere. The bug is
1220 // triggered when you have an open footnote in a paragraph
1221 // environment with a manual label. (Asger)
1222 if (last < 0) last = 0;
1224 if (row->par()->IsLineSeparator(last)) /* a sepearator at this end
1231 w += SingleWidth(bview, row->par(), i);
1236 if (!row->par()->params.labelWidthString().empty()) {
1237 fill = max(lyxfont::width(row->par()->params.labelWidthString(),
1238 GetFont(bview->buffer(), row->par(), -2)) - w,
1246 // returns the number of separators in the specified row. The separator
1247 // on the very last column doesnt count
1248 int LyXText::NumberOfSeparators(Buffer const * buf, Row const * row) const
1250 LyXParagraph::size_type const last = RowLast(row);
1251 LyXParagraph::size_type p =
1252 max(row->pos(), BeginningOfMainBody(buf, row->par()));
1254 for (; p < last; ++p) {
1255 if (row->par()->IsSeparator(p)) {
1263 // returns the number of hfills in the specified row. The LyX-Hfill is
1264 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1265 // ignored. This is *MUCH* more usefull than not to ignore!
1266 int LyXText::NumberOfHfills(Buffer const * buf, Row const * row) const
1268 LyXParagraph::size_type const last = RowLast(row);
1269 LyXParagraph::size_type first = row->pos();
1270 if (first) { /* hfill *DO* count at the beginning
1272 while(first <= last && row->par()->IsHfill(first))
1276 first = max(first, BeginningOfMainBody(buf, row->par()));
1278 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1279 if (row->par()->IsHfill(p)) {
1287 // like NumberOfHfills, but only those in the manual label!
1288 int LyXText::NumberOfLabelHfills(Buffer const * buf, Row const * row) const
1290 LyXParagraph::size_type last = RowLast(row);
1291 LyXParagraph::size_type first = row->pos();
1292 if (first) { /* hfill *DO* count at the beginning
1294 while(first < last && row->par()->IsHfill(first))
1298 last = min(last, BeginningOfMainBody(buf, row->par()));
1300 for (LyXParagraph::size_type p = first;
1301 p < last; ++p) { // last, because the end is ignored!
1302 if (row->par()->IsHfill(p)) {
1310 // returns true, if a expansion is needed.
1311 // Rules are given by LaTeX
1312 bool LyXText::HfillExpansion(Buffer const * buf, Row const * row_ptr,
1313 LyXParagraph::size_type pos) const
1315 // by the way, is it a hfill?
1316 if (!row_ptr->par()->IsHfill(pos))
1319 // at the end of a row it does not count
1320 if (pos >= RowLast(row_ptr))
1323 // at the beginning of a row it does not count, if it is not
1324 // the first row of a paragaph
1325 if (!row_ptr->pos())
1328 // in some labels it does not count
1329 if (textclasslist.Style(buf->params.textclass,
1330 row_ptr->par()->GetLayout()).margintype
1332 && pos < BeginningOfMainBody(buf, row_ptr->par()))
1335 // if there is anything between the first char of the row and
1336 // the sepcified position that is not a newline and not a hfill,
1337 // the hfill will count, otherwise not
1338 LyXParagraph::size_type i = row_ptr->pos();
1339 while (i < pos && (row_ptr->par()->IsNewline(i)
1340 || row_ptr->par()->IsHfill(i)))
1347 void LyXText::SetHeightOfRow(BufferView * bview, Row * row_ptr) const
1349 /* get the maximum ascent and the maximum descent */
1352 float layoutasc = 0;
1353 float layoutdesc = 0;
1356 Inset * tmpinset = 0;
1358 /* this must not happen before the currentrow for clear reasons.
1359 so the trick is just to set the current row onto this row */
1361 GetRow(row_ptr->par(), row_ptr->pos(), unused_y);
1363 /* ok , let us initialize the maxasc and maxdesc value.
1364 * This depends in LaTeX of the font of the last character
1365 * in the paragraph. The hack below is necessary because
1366 * of the possibility of open footnotes */
1368 /* Correction: only the fontsize count. The other properties
1369 are taken from the layoutfont. Nicer on the screen :) */
1371 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
1372 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
1374 LyXParagraph * par = row_ptr->par();
1375 LyXParagraph * firstpar = row_ptr->par();
1378 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1379 firstpar->GetLayout());
1382 LyXFont font = GetFont(bview->buffer(), par, par->Last() - 1);
1384 LyXFont font = GetFont(bview->buffer(), par, par->size() - 1);
1386 LyXFont::FONT_SIZE const size = font.size();
1387 font = GetFont(bview->buffer(), par, -1);
1390 LyXFont labelfont = GetFont(bview->buffer(), par, -2);
1392 float spacing_val = 1.0;
1393 if (!row_ptr->par()->params.spacing().isDefault()) {
1394 spacing_val = row_ptr->par()->params.spacing().getValue();
1396 spacing_val = bview->buffer()->params.spacing.getValue();
1398 //lyxerr << "spacing_val = " << spacing_val << endl;
1400 int maxasc = int(lyxfont::maxAscent(font) *
1401 layout.spacing.getValue() *
1403 int maxdesc = int(lyxfont::maxDescent(font) *
1404 layout.spacing.getValue() *
1406 int const pos_end = RowLast(row_ptr);
1410 // Check if any insets are larger
1411 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1412 if (row_ptr->par()->GetChar(pos) == LyXParagraph::META_INSET) {
1413 tmpfont = GetFont(bview->buffer(), row_ptr->par(), pos);
1414 tmpinset = row_ptr->par()->GetInset(pos);
1416 tmpinset->update(bview, tmpfont);
1417 asc = tmpinset->ascent(bview, tmpfont);
1418 desc = tmpinset->descent(bview, tmpfont);
1419 maxwidth += tmpinset->width(bview, tmpfont);
1420 maxasc = max(maxasc, asc);
1421 maxdesc = max(maxdesc, desc);
1424 maxwidth += SingleWidth(bview, row_ptr->par(), pos);
1428 // Check if any custom fonts are larger (Asger)
1429 // This is not completely correct, but we can live with the small,
1430 // cosmetic error for now.
1431 LyXFont::FONT_SIZE const maxsize =
1432 row_ptr->par()->HighestFontInRange(row_ptr->pos(),
1434 if (maxsize > font.size()) {
1435 font.setSize(maxsize);
1437 asc = lyxfont::maxAscent(font);
1438 desc = lyxfont::maxDescent(font);
1445 // This is nicer with box insets:
1449 row_ptr->ascent_of_text(maxasc);
1451 // is it a top line?
1452 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1454 // some parksips VERY EASY IMPLEMENTATION
1455 if (bview->buffer()->params.paragraph_separation ==
1456 BufferParams::PARSEP_SKIP) {
1457 if (layout.isParagraph()
1458 && firstpar->GetDepth() == 0
1459 && firstpar->previous())
1460 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1461 else if (firstpar->previous()
1462 && textclasslist.Style(bview->buffer()->params.textclass,
1463 firstpar->previous()->GetLayout()).isParagraph()
1464 && firstpar->previous()->GetDepth() == 0)
1465 // is it right to use defskip here too? (AS)
1466 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1469 // the paper margins
1471 if (!row_ptr->par()->previous_ && bv_owner)
1472 maxasc += LYX_PAPER_MARGIN;
1474 if (!row_ptr->par()->previous() && bv_owner)
1475 maxasc += LYX_PAPER_MARGIN;
1478 // add the vertical spaces, that the user added
1479 if (firstpar->params.spaceTop().kind() != VSpace::NONE)
1480 maxasc += int(firstpar->params.spaceTop().inPixels(bview));
1482 // do not forget the DTP-lines!
1483 // there height depends on the font of the nearest character
1484 if (firstpar->params.lineTop())
1485 maxasc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1488 // and now the pagebreaks
1489 if (firstpar->params.pagebreakTop())
1490 maxasc += 3 * DefaultHeight();
1492 // This is special code for the chapter, since the label of this
1493 // layout is printed in an extra row
1494 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1495 && bview->buffer()->params.secnumdepth >= 0) {
1496 float spacing_val = 1.0;
1497 if (!row_ptr->par()->params.spacing().isDefault()) {
1498 spacing_val = row_ptr->par()->params.spacing().getValue();
1500 spacing_val = bview->buffer()->params.spacing.getValue();
1503 labeladdon = int(lyxfont::maxDescent(labelfont) *
1504 layout.spacing.getValue() *
1506 + int(lyxfont::maxAscent(labelfont) *
1507 layout.spacing.getValue() *
1511 // special code for the top label
1512 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1513 || layout.labeltype == LABEL_BIBLIO
1514 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1515 && row_ptr->par()->IsFirstInSequence()
1516 && !row_ptr->par()->GetLabelstring().empty()) {
1517 float spacing_val = 1.0;
1518 if (!row_ptr->par()->params.spacing().isDefault()) {
1519 spacing_val = row_ptr->par()->params.spacing().getValue();
1521 spacing_val = bview->buffer()->params.spacing.getValue();
1525 (lyxfont::maxAscent(labelfont) *
1526 layout.spacing.getValue() *
1528 +(lyxfont::maxDescent(labelfont) *
1529 layout.spacing.getValue() *
1531 + layout.topsep * DefaultHeight()
1532 + layout.labelbottomsep * DefaultHeight());
1535 // and now the layout spaces, for example before and after a section,
1536 // or between the items of a itemize or enumerate environment
1538 if (!firstpar->params.pagebreakTop()) {
1539 LyXParagraph * prev = row_ptr->par()->previous();
1541 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth());
1542 if (prev && prev->GetLayout() == firstpar->GetLayout()
1543 && prev->GetDepth() == firstpar->GetDepth()
1544 && prev->GetLabelWidthString() == firstpar->GetLabelWidthString())
1546 layoutasc = (layout.itemsep * DefaultHeight());
1548 else if (row_ptr->previous()) {
1549 tmptop = layout.topsep;
1551 if (row_ptr->previous()->par()->GetDepth() >= row_ptr->par()->GetDepth())
1552 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1553 row_ptr->previous()->par()->
1554 GetLayout()).bottomsep;
1557 layoutasc = (tmptop * DefaultHeight());
1559 else if (row_ptr->par()->params.lineTop()) {
1560 tmptop = layout.topsep;
1563 layoutasc = (tmptop * DefaultHeight());
1566 prev = row_ptr->par()->DepthHook(row_ptr->par()->GetDepth()-1);
1568 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1569 prev->GetLayout()).parsep * DefaultHeight());
1572 if (firstpar->previous()
1573 && firstpar->previous()->GetDepth() == 0
1574 && firstpar->previous()->GetLayout() != firstpar->GetLayout()) {
1577 else if (firstpar->previous()){
1578 maxasc += int(layout.parsep * DefaultHeight());
1584 // is it a bottom line?
1587 row_ptr->par()->ParFromPos(RowLast(row_ptr) + 1) == par
1589 row_ptr->par() == par
1591 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1593 // the paper margins
1595 if (!par->next_ && bv_owner)
1596 maxdesc += LYX_PAPER_MARGIN;
1598 if (!par->next() && bv_owner)
1599 maxdesc += LYX_PAPER_MARGIN;
1602 // add the vertical spaces, that the user added
1603 if (firstpar->params.spaceBottom().kind() != VSpace::NONE)
1604 maxdesc += int(firstpar->params.spaceBottom().inPixels(bview));
1606 // do not forget the DTP-lines!
1607 // there height depends on the font of the nearest character
1609 if (firstpar->params.lineBottom())
1610 maxdesc += 2 * lyxfont::ascent('x', GetFont(bview->buffer(),
1611 par, par->Last() - 1));
1613 if (firstpar->params.lineBottom())
1614 maxdesc += 2 * lyxfont::ascent('x',
1615 GetFont(bview->buffer(),
1616 par, par->size() - 1));
1619 // and now the pagebreaks
1620 if (firstpar->params.pagebreakBottom())
1621 maxdesc += 3 * DefaultHeight();
1623 // and now the layout spaces, for example before and after
1624 // a section, or between the items of a itemize or enumerate
1626 if (!firstpar->params.pagebreakBottom() && row_ptr->par()->next()) {
1627 LyXParagraph * nextpar = row_ptr->par()->next();
1628 LyXParagraph * comparepar = row_ptr->par();
1632 if (comparepar->GetDepth() > nextpar->GetDepth()) {
1633 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1634 comparepar = comparepar->DepthHook(nextpar->GetDepth());
1635 if (comparepar->GetLayout()!= nextpar->GetLayout()
1636 || nextpar->GetLabelWidthString() !=
1637 comparepar->GetLabelWidthString())
1638 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1640 if (unusual > usual)
1641 layoutdesc = unusual;
1645 else if (comparepar->GetDepth() == nextpar->GetDepth()) {
1647 if (comparepar->GetLayout()!= nextpar->GetLayout()
1648 || nextpar->GetLabelWidthString() !=
1649 comparepar->GetLabelWidthString())
1650 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->GetLayout()).bottomsep * DefaultHeight());
1655 // incalculate the layout spaces
1656 maxasc += int(layoutasc * 2 / (2 + firstpar->GetDepth()));
1657 maxdesc += int(layoutdesc * 2 / (2 + firstpar->GetDepth()));
1659 // calculate the new height of the text
1660 height -= row_ptr->height();
1662 row_ptr->height(maxasc + maxdesc + labeladdon);
1663 row_ptr->baseline(maxasc + labeladdon);
1665 height += row_ptr->height();
1667 PrepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1668 row_ptr->width(int(maxwidth + x));
1671 width = max(0,workWidth(bview));
1673 if (r->width() > width)
1681 /* Appends the implicit specified paragraph behind the specified row,
1682 * start at the implicit given position */
1683 void LyXText::AppendParagraph(BufferView * bview, Row * row) const
1685 bool not_ready = true;
1687 // The last character position of a paragraph is an invariant so we can
1688 // safely get it here. (Asger)
1690 int const lastposition = row->par()->Last();
1692 int const lastposition = row->par()->size();
1695 // Get the next breakpoint
1696 int z = NextBreakPoint(bview, row, workWidth(bview));
1700 // Insert the new row
1701 if (z < lastposition) {
1703 InsertRow(row, row->par(), z);
1710 // Set the dimensions of the row
1711 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1712 SetHeightOfRow(bview, tmprow);
1714 } while (not_ready);
1718 void LyXText::BreakAgain(BufferView * bview, Row * row) const
1720 bool not_ready = true;
1723 // get the next breakpoint
1724 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1728 if (z < row->par()->Last()) {
1730 if (z < row->par()->size()) {
1732 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1735 InsertRow(row, row->par(), z);
1741 if (row->pos() == z)
1742 not_ready = false; // the rest will not change
1748 /* if there are some rows too much, delete them */
1749 /* only if you broke the whole paragraph! */
1750 Row * tmprow2 = row;
1751 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1752 tmprow2 = tmprow2->next();
1754 while (tmprow2 != row) {
1755 tmprow2 = tmprow2->previous();
1756 RemoveRow(tmprow2->next());
1761 /* set the dimensions of the row */
1762 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1763 SetHeightOfRow(bview, tmprow);
1764 } while (not_ready);
1768 // this is just a little changed version of break again
1769 void LyXText::BreakAgainOneRow(BufferView * bview, Row * row)
1771 // get the next breakpoint
1772 LyXParagraph::size_type z = NextBreakPoint(bview, row, workWidth(bview));
1776 if (z < row->par()->Last()) {
1778 if (z < row->par()->size()) {
1781 || (row->next() && row->next()->par() != row->par())) {
1782 /* insert a new row */
1784 InsertRow(row, row->par(), z);
1790 if (row->pos() != z)
1794 // if there are some rows too much, delete them
1795 // only if you broke the whole paragraph!
1796 Row * tmprow2 = row;
1797 while (tmprow2->next()
1798 && tmprow2->next()->par() == row->par()) {
1799 tmprow2 = tmprow2->next();
1801 while (tmprow2 != row) {
1802 tmprow2 = tmprow2->previous();
1803 RemoveRow(tmprow2->next());
1807 // set the dimensions of the row
1808 tmprow->fill(Fill(bview, tmprow, workWidth(bview)));
1809 SetHeightOfRow(bview, tmprow);
1813 void LyXText::BreakParagraph(BufferView * bview, char keep_layout)
1815 LyXLayout const & layout =
1816 textclasslist.Style(bview->buffer()->params.textclass,
1817 cursor.par()->GetLayout());
1819 // this is only allowed, if the current paragraph is not empty or caption
1821 if ((cursor.par()->Last() <= 0
1822 && !cursor.par()->IsDummy())
1823 && layout.labeltype!= LABEL_SENSITIVE)
1826 SetUndo(bview->buffer(), Undo::INSERT,
1827 cursor.par()->ParFromPos(cursor.pos())->previous_,
1828 cursor.par()->ParFromPos(cursor.pos())->next_);
1830 if ((cursor.par()->size() <= 0)
1831 && layout.labeltype!= LABEL_SENSITIVE)
1834 SetUndo(bview->buffer(), Undo::INSERT,
1835 cursor.par()->previous(),
1836 cursor.par()->next());
1839 // Always break behind a space
1841 // It is better to erase the space (Dekel)
1843 if (cursor.pos() < cursor.par()->Last()
1845 if (cursor.pos() < cursor.par()->size()
1847 && cursor.par()->IsLineSeparator(cursor.pos()))
1848 cursor.par()->Erase(cursor.pos());
1849 // cursor.pos(cursor.pos() + 1);
1851 // break the paragraph
1855 keep_layout = layout.isEnvironment();
1856 cursor.par()->BreakParagraph(bview->buffer()->params, cursor.pos(),
1859 // well this is the caption hack since one caption is really enough
1860 if (layout.labeltype == LABEL_SENSITIVE) {
1862 // set to standard-layout
1864 cursor.par()->SetLayout(bview->buffer()->params, 0);
1866 cursor.par()->SetLayout(0);
1869 // set to standard-layout
1871 cursor.par()->next()->SetLayout(bview->buffer()->params, 0);
1873 cursor.par()->next()->SetLayout(0);
1877 /* if the cursor is at the beginning of a row without prior newline,
1879 * This touches only the screen-update. Otherwise we would may have
1880 * an empty row on the screen */
1881 if (cursor.pos() && !cursor.row()->par()->IsNewline(cursor.row()->pos() - 1)
1882 && cursor.row()->pos() == cursor.pos()) {
1886 status = LyXText::NEED_MORE_REFRESH;
1887 refresh_row = cursor.row();
1888 refresh_y = cursor.y() - cursor.row()->baseline();
1890 // Do not forget the special right address boxes
1891 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1892 while (refresh_row->previous() &&
1893 refresh_row->previous()->par() == refresh_row->par()) {
1894 refresh_row = refresh_row->previous();
1895 refresh_y -= refresh_row->height();
1898 RemoveParagraph(cursor.row());
1900 // set the dimensions of the cursor row
1901 cursor.row()->fill(Fill(bview, cursor.row(), workWidth(bview)));
1903 SetHeightOfRow(bview, cursor.row());
1906 while (cursor.par()->next()->Last()
1908 while (cursor.par()->next()->size()
1910 && cursor.par()->next()->IsNewline(0))
1911 cursor.par()->next()->Erase(0);
1913 InsertParagraph(bview, cursor.par()->next(), cursor.row());
1915 UpdateCounters(bview, cursor.row()->previous());
1917 /* This check is necessary. Otherwise the new empty paragraph will
1918 * be deleted automatically. And it is more friendly for the user! */
1920 SetCursor(bview, cursor.par()->next(), 0);
1922 SetCursor(bview, cursor.par(), 0);
1924 if (cursor.row()->next())
1925 BreakAgain(bview, cursor.row()->next());
1932 void LyXText::OpenFootnote(BufferView * bview)
1934 LyXParagraph * endpar,* tmppar;
1937 LyXParagraph * par = cursor.par()->ParFromPos(cursor.pos());
1939 /* if there is no footnote in this paragraph, just return. */
1941 || par->next_->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1944 /* ok, move the cursor right before the footnote */
1946 /* just a little faster than using CursorRight() */
1948 cursor.par()->ParFromPos(cursor.pos()) != par;) {
1949 cursor.pos(cursor.pos() + 1);
1952 /* now the cursor is at the beginning of the physical par */
1953 SetCursor(bview, cursor.par(),
1954 cursor.pos() + cursor.par()->ParFromPos(cursor.pos())->size());
1956 /* the cursor must be exactly before the footnote */
1957 par = cursor.par()->ParFromPos(cursor.pos());
1959 status = LyXText::NEED_MORE_REFRESH;
1960 refresh_row = cursor.row();
1961 refresh_y = cursor.y() - cursor.row()->baseline();
1963 tmppar = cursor.par();
1964 endpar = cursor.par()->next();
1967 tmppar->OpenFootnote(cursor.pos());
1968 RemoveParagraph(row);
1969 /* set the dimensions of the cursor row */
1970 row->fill(Fill(bview, row, workWidth(bview)));
1971 SetHeightOfRow(bview, row);
1972 // CHECK See comment on top of text.C
1973 tmppar = tmppar->next();
1975 while (tmppar != endpar) {
1977 InsertParagraph(bview, tmppar, row);
1978 while (row->next() && row->next()->par() == tmppar)
1980 tmppar = tmppar->next();
1983 SetCursor(bview, par->next_, 0);
1984 sel_cursor = cursor;
1989 // Just a macro to make some thing easier.
1990 void LyXText::RedoParagraph(BufferView * bview) const
1992 ClearSelection(bview);
1993 RedoParagraphs(bview, cursor, cursor.par()->next());
1994 SetCursorIntern(bview, cursor.par(), cursor.pos());
1998 /* insert a character, moves all the following breaks in the
1999 * same Paragraph one to the right and make a rebreak */
2000 void LyXText::InsertChar(BufferView * bview, char c)
2002 SetUndo(bview->buffer(), Undo::INSERT,
2004 cursor.par()->ParFromPos(cursor.pos())->previous_,
2005 cursor.par()->ParFromPos(cursor.pos())->next_
2007 cursor.par()->previous(),
2008 cursor.par()->next()
2012 // When the free-spacing option is set for the current layout,
2013 // disable the double-space checking
2015 bool const freeSpacing =
2016 textclasslist.Style(bview->buffer()->params.textclass,
2017 cursor.row()->par()->GetLayout()).free_spacing;
2020 if (lyxrc.auto_number) {
2021 static string const number_operators = "+-/*";
2022 static string const number_unary_operators = "+-";
2023 static string const number_seperators = ".,:";
2025 if (current_font.number() == LyXFont::ON) {
2026 if (!isdigit(c) && !contains(number_operators, c) &&
2027 !(contains(number_seperators, c) &&
2028 cursor.pos() >= 1 &&
2029 cursor.pos() < cursor.par()->size() &&
2030 GetFont(bview->buffer(),
2032 cursor.pos()).number() == LyXFont::ON &&
2033 GetFont(bview->buffer(),
2035 cursor.pos()-1).number() == LyXFont::ON)
2037 Number(bview); // Set current_font.number to OFF
2038 } else if (isdigit(c) &&
2039 real_current_font.isVisibleRightToLeft()) {
2040 Number(bview); // Set current_font.number to ON
2042 if (cursor.pos() > 0) {
2043 char const c = cursor.par()->GetChar(cursor.pos() - 1);
2044 if (contains(number_unary_operators, c) &&
2045 (cursor.pos() == 1 ||
2046 cursor.par()->IsSeparator(cursor.pos() - 2) ||
2047 cursor.par()->IsNewline(cursor.pos() - 2) )
2049 SetCharFont(bview->buffer(),
2053 } else if (contains(number_seperators, c) &&
2054 cursor.pos() >= 2 &&
2055 GetFont(bview->buffer(),
2057 cursor.pos()-2).number() == LyXFont::ON) {
2058 SetCharFont(bview->buffer(),
2068 /* First check, if there will be two blanks together or a blank at
2069 the beginning of a paragraph.
2070 I decided to handle blanks like normal characters, the main
2071 difference are the special checks when calculating the row.fill
2072 (blank does not count at the end of a row) and the check here */
2074 // The bug is triggered when we type in a description environment:
2075 // The current_font is not changed when we go from label to main text
2076 // and it should (along with realtmpfont) when we type the space.
2077 // CHECK There is a bug here! (Asger)
2079 LyXFont realtmpfont = real_current_font;
2080 LyXFont rawtmpfont = current_font; /* store the current font.
2081 * This is because of the use
2082 * of cursor movements. The moving
2083 * cursor would refresh the
2086 // Get the font that is used to calculate the baselineskip
2088 LyXParagraph::size_type const lastpos = cursor.par()->Last();
2090 LyXParagraph::size_type const lastpos = cursor.par()->size();
2092 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2095 bool jumped_over_space = false;
2097 if (!freeSpacing && IsLineSeparatorChar(c)) {
2098 if ((cursor.pos() > 0
2099 && cursor.par()->IsLineSeparator(cursor.pos() - 1))
2100 || (cursor.pos() > 0
2101 && cursor.par()->IsNewline(cursor.pos() - 1))
2102 || (cursor.pos() == 0
2104 && !(cursor.par()->previous()
2105 && cursor.par()->previous()->footnoteflag
2106 == LyXParagraph::OPEN_FOOTNOTE)
2109 if (cursor.pos() == 0 )
2110 bview->owner()->getMiniBuffer()->Set(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
2112 bview->owner()->getMiniBuffer()->Set(_("You cannot type two spaces this way. Please read the Tutorial."));
2116 } else if (IsNewlineChar(c)) {
2118 if (cursor.par()->FirstPhysicalPar() == cursor.par()
2120 if (cursor.par() == cursor.par()
2122 && cursor.pos() <= BeginningOfMainBody(bview->buffer(), cursor.par())) {
2126 /* No newline at first position
2127 * of a paragraph or behind labels.
2128 * TeX does not allow that. */
2131 if (cursor.pos() < cursor.par()->Last() &&
2133 if (cursor.pos() < cursor.par()->size() &&
2135 cursor.par()->IsLineSeparator(cursor.pos()))
2136 // newline always after a blank!
2138 cursor.row()->fill(-1); // to force a new break
2141 // the display inset stuff
2142 if (cursor.row()->par()->GetChar(cursor.row()->pos()) == LyXParagraph::META_INSET
2143 && cursor.row()->par()->GetInset(cursor.row()->pos())
2144 && (cursor.row()->par()->GetInset(cursor.row()->pos())->display() ||
2145 cursor.row()->par()->GetInset(cursor.row()->pos())->needFullRow()))
2146 cursor.row()->fill(-1); // to force a new break
2148 // get the cursor row fist
2149 Row * row = cursor.row();
2150 int y = cursor.y() - row->baseline();
2151 if (c != LyXParagraph::META_INSET) /* Here case LyXText::InsertInset
2152 * already insertet the character */
2153 cursor.par()->InsertChar(cursor.pos(), c);
2154 SetCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
2156 if (!jumped_over_space) {
2157 // refresh the positions
2159 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2160 tmprow = tmprow->next();
2161 tmprow->pos(tmprow->pos() + 1);
2165 // Is there a break one row above
2166 if ((cursor.par()->IsLineSeparator(cursor.pos())
2167 || cursor.par()->IsNewline(cursor.pos())
2168 || cursor.row()->fill() == -1)
2169 && row->previous() && row->previous()->par() == row->par()) {
2170 LyXParagraph::size_type z = NextBreakPoint(bview,
2173 if (z >= row->pos()) {
2176 // set the dimensions of the row above
2177 row->previous()->fill(Fill(bview,
2181 SetHeightOfRow(bview, row->previous());
2183 y -= row->previous()->height();
2185 refresh_row = row->previous();
2186 status = LyXText::NEED_MORE_REFRESH;
2188 BreakAgainOneRow(bview, row);
2190 current_font = rawtmpfont;
2191 real_current_font = realtmpfont;
2192 SetCursor(bview, cursor.par(), cursor.pos() + 1,
2193 false, cursor.boundary());
2194 // cursor MUST be in row now.
2196 if (row->next() && row->next()->par() == row->par())
2197 need_break_row = row->next();
2201 // check, wether the last characters font has changed.
2203 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2205 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2207 && rawparfont != rawtmpfont)
2208 RedoHeightOfParagraph(bview, cursor);
2215 // recalculate the fill of the row
2216 if (row->fill() >= 0) /* needed because a newline
2217 * will set fill to -1. Otherwise
2218 * we would not get a rebreak! */
2219 row->fill(Fill(bview, row, workWidth(bview)));
2220 if (row->fill() < 0) {
2223 refresh_x = cursor.x();
2224 refresh_pos = cursor.pos();
2225 status = LyXText::NEED_MORE_REFRESH;
2226 BreakAgainOneRow(bview, row);
2227 // will the cursor be in another row now?
2228 if (RowLast(row) <= cursor.pos() + 1 && row->next()) {
2229 if (row->next() && row->next()->par() == row->par())
2230 // this should always be true
2232 BreakAgainOneRow(bview, row);
2234 current_font = rawtmpfont;
2235 real_current_font = realtmpfont;
2237 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2239 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
2240 != cursor.boundary())
2241 SetCursor(bview, cursor.par(), cursor.pos(), false,
2242 !cursor.boundary());
2243 if (row->next() && row->next()->par() == row->par())
2244 need_break_row = row->next();
2249 refresh_x = cursor.x();
2251 refresh_pos = cursor.pos();
2253 int const tmpheight = row->height();
2254 SetHeightOfRow(bview, row);
2255 if (tmpheight == row->height())
2256 status = LyXText::NEED_VERY_LITTLE_REFRESH;
2258 status = LyXText::NEED_MORE_REFRESH;
2260 current_font = rawtmpfont;
2261 real_current_font = realtmpfont;
2262 SetCursor(bview, cursor.par(), cursor.pos() + 1, false,
2266 // check, wether the last characters font has changed.
2268 if (cursor.pos() && cursor.pos() == cursor.par()->Last()
2270 if (cursor.pos() && cursor.pos() == cursor.par()->size()
2272 && rawparfont != rawtmpfont) {
2273 RedoHeightOfParagraph(bview, cursor);
2275 // now the special right address boxes
2276 if (textclasslist.Style(bview->buffer()->params.textclass,
2277 cursor.par()->GetLayout()).margintype
2278 == MARGIN_RIGHT_ADDRESS_BOX) {
2279 RedoDrawingOfParagraph(bview, cursor);
2287 void LyXText::charInserted()
2289 // Here we could call FinishUndo for every 20 characters inserted.
2290 // This is from my experience how emacs does it.
2291 static unsigned int counter = 0;
2301 void LyXText::PrepareToPrint(BufferView * bview,
2302 Row * row, float & x,
2303 float & fill_separator,
2305 float & fill_label_hfill,
2310 float w = row->fill();
2312 fill_label_hfill = 0;
2314 fill_label_hfill = 0;
2317 row->par()->isRightToLeftPar(bview->buffer()->params);
2319 x = (workWidth(bview) > 0)
2320 ? RightMargin(bview->buffer(), row) : 0;
2322 if (row->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
2323 LyXFont font(LyXFont::ALL_SANE);
2324 font.setSize(LyXFont::SIZE_SMALL);
2325 x += lyxfont::width("Mwide-figM", font);
2329 x = (workWidth(bview) > 0) ? LeftMargin(bview, row) : 0;
2331 // is there a manual margin with a manual label
2332 if (textclasslist.Style(bview->buffer()->params.textclass,
2333 row->par()->GetLayout()).margintype == MARGIN_MANUAL
2334 && textclasslist.Style(bview->buffer()->params.textclass,
2335 row->par()->GetLayout()).labeltype == LABEL_MANUAL) {
2337 /* one more since labels are left aligned */
2338 nlh = NumberOfLabelHfills(bview->buffer(), row) + 1;
2339 if (nlh && !row->par()->GetLabelWidthString().empty()) {
2340 fill_label_hfill = LabelFill(bview, row) / nlh;
2344 // are there any hfills in the row?
2345 float const nh = NumberOfHfills(bview->buffer(), row);
2348 fill_hfill = w / nh;
2350 // is it block, flushleft or flushright?
2351 // set x how you need it
2354 if (row->par()->FirstPhysicalPar()->params.align() == LYX_ALIGN_LAYOUT)
2355 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2357 align = row->par()->FirstPhysicalPar()->params.align();
2359 if (row->par()->params.align() == LYX_ALIGN_LAYOUT)
2360 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->GetLayout()).align;
2362 align = row->par()->params.align();
2365 // center displayed insets
2367 if (row->par()->GetChar(row->pos()) == LyXParagraph::META_INSET
2368 && (inset=row->par()->GetInset(row->pos()))
2369 && (inset->display())) // || (inset->scroll() < 0)))
2370 align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
2371 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2374 case LYX_ALIGN_BLOCK:
2375 ns = NumberOfSeparators(bview->buffer(), row);
2376 if (ns && row->next() && row->next()->par() == row->par() &&
2377 !(row->next()->par()->IsNewline(row->next()->pos() - 1))
2378 && !(row->next()->par()->GetChar(row->next()->pos()) == LyXParagraph::META_INSET
2379 && row->next()->par()->GetInset(row->next()->pos())
2380 && row->next()->par()->GetInset(row->next()->pos())->display())
2382 fill_separator = w / ns;
2386 case LYX_ALIGN_RIGHT:
2389 case LYX_ALIGN_CENTER:
2397 ComputeBidiTables(bview->buffer(), row);
2399 LyXParagraph::size_type main_body =
2400 BeginningOfMainBody(bview->buffer(), row->par());
2401 LyXParagraph::size_type last = RowLast(row);
2403 if (main_body > 0 &&
2404 (main_body-1 > last ||
2405 !row->par()->IsLineSeparator(main_body-1))) {
2406 LyXLayout const & layout =
2407 textclasslist.Style(bview->buffer()->params.textclass,
2408 row->par()->GetLayout());
2409 x += lyxfont::width(layout.labelsep,
2410 GetFont(bview->buffer(), row->par(), -2));
2411 if (main_body-1 <= last)
2412 x += fill_label_hfill;
2417 /* important for the screen */
2420 /* the cursor set functions have a special mechanism. When they
2421 * realize, that you left an empty paragraph, they will delete it.
2422 * They also delete the corresponding row */
2424 void LyXText::CursorRightOneWord(BufferView * bview) const
2426 // treat floats, HFills and Insets as words
2427 LyXCursor tmpcursor = cursor;
2428 // CHECK See comment on top of text.C
2431 if (tmpcursor.pos() == tmpcursor.par()->Last()
2433 if (tmpcursor.pos() == tmpcursor.par()->size()
2435 && tmpcursor.par()->next()) {
2436 tmpcursor.par(tmpcursor.par()->next());
2441 // Skip through initial nonword stuff.
2443 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2445 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2447 ! tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2449 // printf("Current pos1 %d", tmpcursor.pos()) ;
2450 tmpcursor.pos(tmpcursor.pos() + 1);
2453 // Advance through word.
2455 while (tmpcursor.pos() < tmpcursor.par()->Last() &&
2457 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2459 tmpcursor.par()->IsWord( tmpcursor.pos() ) )
2461 // printf("Current pos2 %d", tmpcursor.pos()) ;
2462 tmpcursor.pos(tmpcursor.pos() + 1);
2466 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2470 void LyXText::CursorTab(BufferView * bview) const
2472 LyXCursor tmpcursor = cursor;
2474 while (tmpcursor.pos() < tmpcursor.par()->Last()
2476 while (tmpcursor.pos() < tmpcursor.par()->size()
2478 && !tmpcursor.par()->IsNewline(tmpcursor.pos()))
2479 tmpcursor.pos(tmpcursor.pos() + 1);
2482 if (tmpcursor.pos() == tmpcursor.par()->Last()){
2484 if (tmpcursor.pos() == tmpcursor.par()->size()){
2486 if (tmpcursor.par()->next()) {
2487 tmpcursor.par(tmpcursor.par()->next());
2491 tmpcursor.pos(tmpcursor.pos() + 1);
2492 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2496 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2497 of prior word, not to end of next prior word. */
2499 void LyXText::CursorLeftOneWord(BufferView * bview) const
2501 // treat HFills, floats and Insets as words
2502 LyXCursor tmpcursor = cursor;
2503 while (tmpcursor.pos()
2504 && (tmpcursor.par()->IsSeparator(tmpcursor.pos() - 1)
2505 || tmpcursor.par()->IsKomma(tmpcursor.pos() - 1))
2506 && !(tmpcursor.par()->IsHfill(tmpcursor.pos() - 1)
2508 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2510 || tmpcursor.par()->IsInset(tmpcursor.pos() - 1)))
2511 tmpcursor.pos(tmpcursor.pos() - 1);
2514 && (tmpcursor.par()->IsInset(tmpcursor.pos() - 1)
2516 || tmpcursor.par()->IsFloat(tmpcursor.pos() - 1)
2518 || tmpcursor.par()->IsHfill(tmpcursor.pos() - 1))) {
2519 tmpcursor.pos(tmpcursor.pos() - 1);
2520 } else if (!tmpcursor.pos()) {
2521 if (tmpcursor.par()->previous()){
2522 tmpcursor.par(tmpcursor.par()->previous());
2524 tmpcursor.pos(tmpcursor.par()->Last());
2526 tmpcursor.pos(tmpcursor.par()->size());
2529 } else { // Here, tmpcursor != 0
2530 while (tmpcursor.pos() > 0 &&
2531 tmpcursor.par()->IsWord(tmpcursor.pos()-1) )
2532 tmpcursor.pos(tmpcursor.pos() - 1);
2534 SetCursor(bview, tmpcursor.par(), tmpcursor.pos());
2537 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
2540 void LyXText::SelectWord(BufferView * bview)
2542 // Move cursor to the beginning, when not already there.
2544 && !cursor.par()->IsSeparator(cursor.pos()-1)
2545 && !cursor.par()->IsKomma(cursor.pos()-1) )
2546 CursorLeftOneWord(bview);
2548 // set the sel cursor
2549 sel_cursor = cursor;
2552 while (cursor.pos() < cursor.par()->Last()
2554 while (cursor.pos() < cursor.par()->size()
2556 && !cursor.par()->IsSeparator(cursor.pos())
2557 && !cursor.par()->IsKomma(cursor.pos()) )
2558 cursor.pos(cursor.pos() + 1);
2559 SetCursor(bview, cursor.par(), cursor.pos() );
2561 // finally set the selection
2562 SetSelection(bview);
2566 /* -------> Select the word currently under the cursor when:
2567 1: no selection is currently set,
2568 2: the cursor is not at the borders of the word. */
2570 bool LyXText::SelectWordWhenUnderCursor(BufferView * bview)
2574 cursor.pos() > 0 && cursor.pos() < cursor.par()->Last()
2576 cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2578 && !cursor.par()->IsSeparator(cursor.pos())
2579 && !cursor.par()->IsKomma(cursor.pos())
2580 && !cursor.par()->IsSeparator(cursor.pos() -1)
2581 && !cursor.par()->IsKomma(cursor.pos() -1)) {
2589 // This function is only used by the spellchecker for NextWord().
2590 // It doesn't handle LYX_ACCENTs and probably never will.
2591 string const LyXText::SelectNextWord(BufferView * bview,
2592 float & value) const
2594 LyXParagraph * tmppar = cursor.par();
2596 // If this is not the very first word, skip rest of
2597 // current word because we are probably in the middle
2598 // of a word if there is text here.
2600 if (cursor.pos() || cursor.par()->previous_) {
2601 while (cursor.pos() < cursor.par()->Last()
2602 && cursor.par()->IsLetter(cursor.pos()))
2603 cursor.pos(cursor.pos() + 1);
2606 if (cursor.pos() || cursor.par()->previous()) {
2607 while (cursor.pos() < cursor.par()->size()
2608 && cursor.par()->IsLetter(cursor.pos()))
2609 cursor.pos(cursor.pos() + 1);
2613 // Now, skip until we have real text (will jump paragraphs)
2615 while ((cursor.par()->Last() > cursor.pos()
2617 while ((cursor.par()->size() > cursor.pos()
2619 && (!cursor.par()->IsLetter(cursor.pos())
2620 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2621 .latex() == LyXFont::ON))
2623 || (cursor.par()->Last() == cursor.pos()
2625 || (cursor.par()->size() == cursor.pos()
2627 && cursor.par()->next())){
2629 if (cursor.pos() == cursor.par()->Last()) {
2631 if (cursor.pos() == cursor.par()->size()) {
2633 cursor.par(cursor.par()->next());
2636 cursor.pos(cursor.pos() + 1);
2639 // Update the value if we changed paragraphs
2640 if (cursor.par() != tmppar){
2641 SetCursor(bview, cursor.par(), cursor.pos());
2642 value = float(cursor.y())/float(height);
2645 // Start the selection from here
2646 sel_cursor = cursor;
2648 std::ostringstream latex;
2650 // and find the end of the word
2651 // (optional hyphens are part of a word)
2653 while (cursor.pos() < cursor.par()->Last()
2655 while (cursor.pos() < cursor.par()->size()
2657 && (cursor.par()->IsLetter(cursor.pos()))
2658 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2659 && cursor.par()->GetInset(cursor.pos()) != 0
2660 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2661 && latex.str() == "\\-"
2663 cursor.pos(cursor.pos() + 1);
2665 // Finally, we copy the word to a string and return it
2667 if (sel_cursor.pos() < cursor.pos()) {
2668 LyXParagraph::size_type i;
2669 for (i = sel_cursor.pos(); i < cursor.pos(); ++i) {
2670 if (cursor.par()->GetChar(i) != LyXParagraph::META_INSET)
2671 str += cursor.par()->GetChar(i);
2678 // This one is also only for the spellchecker
2679 void LyXText::SelectSelectedWord(BufferView * bview)
2681 // move cursor to the beginning
2682 SetCursor(bview, sel_cursor.par(), sel_cursor.pos());
2684 // set the sel cursor
2685 sel_cursor = cursor;
2687 std::ostringstream latex;
2689 // now find the end of the word
2691 while (cursor.pos() < cursor.par()->Last()
2693 while (cursor.pos() < cursor.par()->size()
2695 && (cursor.par()->IsLetter(cursor.pos())
2696 || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET
2697 && cursor.par()->GetInset(cursor.pos()) != 0
2698 && cursor.par()->GetInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
2699 && latex.str() == "\\-"
2701 cursor.pos(cursor.pos() + 1);
2703 SetCursor(bview, cursor.par(), cursor.pos());
2705 // finally set the selection
2706 SetSelection(bview);
2710 /* -------> Delete from cursor up to the end of the current or next word. */
2711 void LyXText::DeleteWordForward(BufferView * bview)
2714 if (!cursor.par()->Last())
2716 if (!cursor.par()->size())
2720 LyXCursor tmpcursor = cursor;
2721 tmpcursor.row(0); //Â ??
2722 selection = true; // to avoid deletion
2723 CursorRightOneWord(bview);
2724 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2725 sel_cursor = cursor;
2727 SetSelection(bview);
2729 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2730 CutSelection(bview);
2735 /* -------> Delete from cursor to start of current or prior word. */
2736 void LyXText::DeleteWordBackward(BufferView * bview)
2739 if (!cursor.par()->Last())
2741 if (!cursor.par()->size())
2745 LyXCursor tmpcursor = cursor;
2746 tmpcursor.row(0); // ??
2747 selection = true; // to avoid deletion
2748 CursorLeftOneWord(bview);
2749 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2750 sel_cursor = cursor;
2752 SetSelection(bview);
2753 CutSelection(bview);
2758 /* -------> Kill to end of line. */
2759 void LyXText::DeleteLineForward(BufferView * bview)
2762 if (!cursor.par()->Last())
2764 if (!cursor.par()->size())
2766 // Paragraph is empty, so we just go to the right
2769 LyXCursor tmpcursor = cursor;
2770 // We can't store the row over a regular SetCursor
2771 // so we set it to 0 and reset it afterwards.
2772 tmpcursor.row(0); //Â ??
2773 selection = true; // to avoid deletion
2775 SetCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2776 sel_cursor = cursor;
2778 SetSelection(bview);
2779 // What is this test for ??? (JMarc)
2781 DeleteWordForward(bview);
2783 CutSelection(bview);
2789 // Change the case of a word at cursor position.
2790 // This function directly manipulates LyXParagraph::text because there
2791 // is no LyXParagraph::SetChar currently. I did what I could to ensure
2792 // that it is correct. I guess part of it should be moved to
2793 // LyXParagraph, but it will have to change for 1.1 anyway. At least
2794 // it does not access outside of the allocated array as the older
2795 // version did. (JMarc)
2796 void LyXText::ChangeWordCase(BufferView * bview, LyXText::TextCase action)
2799 LyXParagraph * tmppar = cursor.par()->ParFromPos(cursor.pos());
2801 SetUndo(bview->buffer(),Undo::FINISH,
2802 tmppar->previous_, tmppar->next_);
2804 LyXParagraph::size_type tmppos =
2805 cursor.par()->PositionInParFromPos(cursor.pos());
2807 LyXParagraph * tmppar = cursor.par();
2809 SetUndo(bview->buffer(),Undo::FINISH,
2810 tmppar->previous(), tmppar->next());
2812 LyXParagraph::size_type tmppos = cursor.pos();
2815 while (tmppos < tmppar->size()) {
2816 unsigned char c = tmppar->GetChar(tmppos);
2817 if (IsKommaChar(c) || IsLineSeparatorChar(c))
2819 if (c != LyXParagraph::META_INSET) {
2821 case text_lowercase:
2824 case text_capitalization:
2826 action = text_lowercase;
2828 case text_uppercase:
2834 //tmppar->text[tmppos] = c;
2835 tmppar->SetChar(tmppos, c);
2838 CheckParagraph(bview, tmppar, tmppos);
2839 CursorRightOneWord(bview);
2843 void LyXText::Delete(BufferView * bview)
2845 // this is a very easy implementation
2847 LyXCursor old_cursor = cursor;
2848 int const old_cur_par_id = old_cursor.par()->id();
2850 int const old_cur_par_prev_id = old_cursor.par()->previous_ ?
2851 old_cursor.par()->previous_->id() : 0;
2853 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2854 old_cursor.par()->previous()->id() : 0;
2857 // just move to the right
2860 // CHECK Look at the comment here.
2861 // This check is not very good...
2862 // The CursorRightIntern calls DeleteEmptyParagrapgMechanism
2863 // and that can very well delete the par or par->previous in
2864 // old_cursor. Will a solution where we compare paragraph id's
2868 (cursor.par()->previous_ ? cursor.par()->previous_->id() : 0)
2870 (cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2872 == old_cur_par_prev_id
2873 && cursor.par()->id() != old_cur_par_id)
2874 return; // delete-empty-paragraph-mechanism has done it
2876 // if you had success make a backspace
2877 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2878 LyXCursor tmpcursor = cursor;
2879 cursor = old_cursor; // to make sure undo gets the right cursor position
2880 SetUndo(bview->buffer(), Undo::DELETE,
2882 cursor.par()->ParFromPos(cursor.pos())->previous_,
2883 cursor.par()->ParFromPos(cursor.pos())->next_
2885 cursor.par()->previous(),
2886 cursor.par()->next()
2895 void LyXText::Backspace(BufferView * bview)
2897 // Get the font that is used to calculate the baselineskip
2899 LyXParagraph::size_type lastpos = cursor.par()->Last();
2901 LyXParagraph::size_type lastpos = cursor.par()->size();
2903 LyXFont rawparfont = cursor.par()->GetFontSettings(bview->buffer()->params,
2906 if (cursor.pos() == 0) {
2907 // The cursor is at the beginning of a paragraph,
2908 // so the the backspace will collapse two paragraphs into one.
2910 // we may paste some paragraphs
2912 // is it an empty paragraph?
2915 || (lastpos == 1 && cursor.par()->IsSeparator(0)))
2917 && !(cursor.par()->next()
2918 && cursor.par()->footnoteflag == LyXParagraph::NO_FOOTNOTE
2919 && cursor.par()->next()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
2922 // This is an empty paragraph and we delete it just by moving the cursor one step
2923 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2924 // of the paragraph.
2927 if (cursor.par()->previous_) {
2928 LyXParagraph * tmppar = cursor.par()->previous_->FirstPhysicalPar();
2929 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2930 && cursor.par()->footnoteflag == tmppar->footnoteflag
2932 if (cursor.par()->previous()) {
2933 LyXParagraph * tmppar = cursor.par()->previous();
2934 if (cursor.par()->GetLayout() == tmppar->GetLayout()
2936 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
2937 // Inherit bottom DTD from the paragraph below.
2938 // (the one we are deleting)
2939 tmppar->params.lineBottom(cursor.par()->params.lineBottom());
2940 tmppar->params.spaceBottom(cursor.par()->params.spaceBottom());
2941 tmppar->params.pagebreakBottom(cursor.par()->params.pagebreakBottom());
2946 // the layout things can change the height of a row !
2947 int const tmpheight = cursor.row()->height();
2948 SetHeightOfRow(bview, cursor.row());
2949 if (cursor.row()->height() != tmpheight) {
2950 refresh_y = cursor.y() - cursor.row()->baseline();
2951 refresh_row = cursor.row();
2952 status = LyXText::NEED_MORE_REFRESH;
2959 if (cursor.par()->ParFromPos(cursor.pos())->previous_) {
2960 SetUndo(bview->buffer(), Undo::DELETE,
2961 cursor.par()->ParFromPos(cursor.pos())->previous_->previous_,
2962 cursor.par()->ParFromPos(cursor.pos())->next_);
2965 if (cursor.par()->previous()) {
2966 SetUndo(bview->buffer(), Undo::DELETE,
2967 cursor.par()->previous()->previous(),
2968 cursor.par()->next());
2972 LyXParagraph * tmppar = cursor.par();
2973 Row * tmprow = cursor.row();
2975 // We used to do CursorLeftIntern() here, but it is
2976 // not a good idea since it triggers the auto-delete
2977 // mechanism. So we do a CursorLeftIntern()-lite,
2978 // without the dreaded mechanism. (JMarc)
2979 if (cursor.par()->previous()) {
2980 // steps into the above paragraph.
2982 SetCursorIntern(bview, cursor.par()->previous(),
2983 cursor.par()->previous()->Last(), false);
2985 SetCursorIntern(bview, cursor.par()->previous(),
2986 cursor.par()->previous()->size(),
2991 /* Pasting is not allowed, if the paragraphs have different
2992 layout. I think it is a real bug of all other
2993 word processors to allow it. It confuses the user.
2994 Even so with a footnote paragraph and a non-footnote
2995 paragraph. I will not allow pasting in this case,
2996 because the user would be confused if the footnote behaves
2997 different wether it is open or closed.
2999 Correction: Pasting is always allowed with standard-layout
3001 if (cursor.par() != tmppar
3002 && (cursor.par()->GetLayout() == tmppar->GetLayout()
3003 || tmppar->GetLayout() == 0 /*standard*/)
3005 && cursor.par()->footnoteflag == tmppar->footnoteflag
3007 && cursor.par()->GetAlign() == tmppar->GetAlign()) {
3009 RemoveParagraph(tmprow);
3011 cursor.par()->PasteParagraph(bview->buffer()->params);
3013 if (!cursor.pos() || !cursor.par()->IsSeparator(cursor.pos() - 1))
3014 ; //cursor.par()->InsertChar(cursor.pos(), ' ');
3015 // strangely enough it seems that commenting out the line above removes
3016 // most or all of the segfaults. I will however also try to move the
3017 // two Remove... lines in front of the PasteParagraph too.
3020 cursor.pos(cursor.pos() - 1);
3022 status = LyXText::NEED_MORE_REFRESH;
3023 refresh_row = cursor.row();
3024 refresh_y = cursor.y() - cursor.row()->baseline();
3026 // remove the lost paragraph
3027 // This one is not safe, since the paragraph that the tmprow and the
3028 // following rows belong to has been deleted by the PasteParagraph
3029 // above. The question is... could this be moved in front of the
3031 //RemoveParagraph(tmprow);
3032 //RemoveRow(tmprow);
3034 // This rebuilds the rows.
3035 AppendParagraph(bview, cursor.row());
3036 UpdateCounters(bview, cursor.row());
3038 // the row may have changed, block, hfills etc.
3039 SetCursor(bview, cursor.par(), cursor.pos(), false);
3042 /* this is the code for a normal backspace, not pasting
3044 SetUndo(bview->buffer(), Undo::DELETE,
3046 cursor.par()->ParFromPos(cursor.pos())->previous_,
3047 cursor.par()->ParFromPos(cursor.pos())->next_
3049 cursor.par()->previous(),
3050 cursor.par()->next()
3053 // We used to do CursorLeftIntern() here, but it is
3054 // not a good idea since it triggers the auto-delete
3055 // mechanism. So we do a CursorLeftIntern()-lite,
3056 // without the dreaded mechanism. (JMarc)
3057 SetCursorIntern(bview, cursor.par(), cursor.pos()- 1,
3058 false, cursor.boundary());
3060 // some insets are undeletable here
3061 if (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
3062 if (!cursor.par()->GetInset(cursor.pos())->Deletable())
3064 // force complete redo when erasing display insets
3065 // this is a cruel method but safe..... Matthias
3066 if (cursor.par()->GetInset(cursor.pos())->display() ||
3067 cursor.par()->GetInset(cursor.pos())->needFullRow()) {
3068 cursor.par()->Erase(cursor.pos());
3069 RedoParagraph(bview);
3074 Row * row = cursor.row();
3075 int y = cursor.y() - row->baseline();
3076 LyXParagraph::size_type z;
3077 /* remember that a space at the end of a row doesnt count
3078 * when calculating the fill */
3079 if (cursor.pos() < RowLast(row) ||
3080 !cursor.par()->IsLineSeparator(cursor.pos())) {
3081 row->fill(row->fill() + SingleWidth(bview,
3086 /* some special code when deleting a newline. This is similar
3087 * to the behavior when pasting paragraphs */
3088 if (cursor.pos() && cursor.par()->IsNewline(cursor.pos())) {
3089 cursor.par()->Erase(cursor.pos());
3090 // refresh the positions
3092 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3093 tmprow = tmprow->next();
3094 tmprow->pos(tmprow->pos() - 1);
3096 if (cursor.par()->IsLineSeparator(cursor.pos() - 1))
3097 cursor.pos(cursor.pos() - 1);
3100 if (cursor.pos() < cursor.par()->Last()
3101 && !cursor.par()->IsSeparator(cursor.pos())) {
3103 if (cursor.pos() < cursor.par()->size()
3104 && !cursor.par()->IsSeparator(cursor.pos())) {
3106 cursor.par()->InsertChar(cursor.pos(), ' ');
3107 SetCharFont(bview->buffer(), cursor.par(),
3108 cursor.pos(), current_font);
3109 // refresh the positions
3111 while (tmprow->next() && tmprow->next()->par() == row->par()) {
3112 tmprow = tmprow->next();
3113 tmprow->pos(tmprow->pos() + 1);
3117 cursor.par()->Erase(cursor.pos());
3119 // refresh the positions
3121 while (tmprow->next()
3122 && tmprow->next()->par() == row->par()) {
3123 tmprow = tmprow->next();
3124 tmprow->pos(tmprow->pos() - 1);
3127 // delete newlines at the beginning of paragraphs
3129 while (cursor.par()->Last() &&
3131 while (cursor.par()->size() &&
3133 cursor.par()->IsNewline(cursor.pos()) &&
3134 cursor.pos() == BeginningOfMainBody(bview->buffer(),
3136 cursor.par()->Erase(cursor.pos());
3137 // refresh the positions
3139 while (tmprow->next() &&
3140 tmprow->next()->par() == row->par()) {
3141 tmprow = tmprow->next();
3142 tmprow->pos(tmprow->pos() - 1);
3147 // is there a break one row above
3148 if (row->previous() && row->previous()->par() == row->par()) {
3149 z = NextBreakPoint(bview, row->previous(),
3151 if (z >= row->pos()) {
3154 Row * tmprow = row->previous();
3156 // maybe the current row is now empty
3158 if (row->pos() >= row->par()->Last()) {
3160 if (row->pos() >= row->par()->size()) {
3166 BreakAgainOneRow(bview, row);
3167 if (row->next() && row->next()->par() == row->par())
3168 need_break_row = row->next();
3173 // set the dimensions of the row above
3174 y -= tmprow->height();
3175 tmprow->fill(Fill(bview, tmprow,
3177 SetHeightOfRow(bview, tmprow);
3180 refresh_row = tmprow;
3181 status = LyXText::NEED_MORE_REFRESH;
3182 SetCursor(bview, cursor.par(), cursor.pos(),
3183 false, cursor.boundary());
3184 //current_font = rawtmpfont;
3185 //real_current_font = realtmpfont;
3186 // check, whether the last character's font has changed.
3189 cursor.par()->GetFontSettings(bview->buffer()->params,
3190 cursor.par()->Last() - 1))
3193 cursor.par()->GetFontSettings(bview->buffer()->params,
3194 cursor.par()->size() - 1))
3196 RedoHeightOfParagraph(bview, cursor);
3201 // break the cursor row again
3202 if (row->next() && row->next()->par() == row->par() &&
3204 (RowLast(row) == row->par()->Last() - 1 ||
3206 (RowLast(row) == row->par()->size() - 1 ||
3208 NextBreakPoint(bview, row, workWidth(bview)) != RowLast(row))) {
3210 /* it can happen that a paragraph loses one row
3211 * without a real breakup. This is when a word
3212 * is to long to be broken. Well, I don t care this
3215 if (RowLast(row) == row->par()->Last() - 1)
3217 if (RowLast(row) == row->par()->size() - 1)
3219 RemoveRow(row->next());
3223 status = LyXText::NEED_MORE_REFRESH;
3225 BreakAgainOneRow(bview, row);
3226 // will the cursor be in another row now?
3227 if (row->next() && row->next()->par() == row->par() &&
3228 RowLast(row) <= cursor.pos()) {
3230 BreakAgainOneRow(bview, row);
3233 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3235 if (row->next() && row->next()->par() == row->par())
3236 need_break_row = row->next();
3240 // set the dimensions of the row
3241 row->fill(Fill(bview, row, workWidth(bview)));
3242 int const tmpheight = row->height();
3243 SetHeightOfRow(bview, row);
3244 if (tmpheight == row->height())
3245 status = LyXText::NEED_VERY_LITTLE_REFRESH;
3247 status = LyXText::NEED_MORE_REFRESH;
3250 SetCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
3254 // current_font = rawtmpfont;
3255 // real_current_font = realtmpfont;
3257 if (IsBoundary(bview->buffer(), cursor.par(), cursor.pos())
3258 != cursor.boundary())
3259 SetCursor(bview, cursor.par(), cursor.pos(), false,
3260 !cursor.boundary());
3263 lastpos = cursor.par()->Last();
3265 lastpos = cursor.par()->size();
3267 if (cursor.pos() == lastpos)
3268 SetCurrentFont(bview);
3270 // check, whether the last characters font has changed.
3272 cursor.par()->GetFontSettings(bview->buffer()->params, lastpos - 1)) {
3273 RedoHeightOfParagraph(bview, cursor);
3275 // now the special right address boxes
3276 if (textclasslist.Style(bview->buffer()->params.textclass,
3277 cursor.par()->GetLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
3278 RedoDrawingOfParagraph(bview, cursor);
3284 void LyXText::GetVisibleRow(BufferView * bview, int y_offset, int x_offset,
3285 Row * row_ptr, int y, bool cleared)
3287 // returns a printed row
3288 Painter & pain = bview->painter();
3291 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
3293 LyXParagraph::size_type const last = RowLastPrintable(row_ptr);
3295 LyXParagraph::size_type vpos;
3296 LyXParagraph::size_type pos;
3300 LyXFont font(LyXFont::ALL_SANE);
3302 if (row_ptr->height() <= 0) {
3303 lyxerr << "LYX_ERROR: row.height: "
3304 << row_ptr->height() << endl;
3309 float fill_separator;
3311 float fill_label_hfill;
3312 PrepareToPrint(bview, row_ptr, x, fill_separator,
3313 fill_hfill, fill_label_hfill);
3315 if (inset_owner && (x < 0))
3319 // clear the area where we want to paint/print
3320 int const ww = bview->workWidth();
3322 bool clear_area = true;
3325 if (!bview->screen()->forceClear() && last == row_ptr->pos()
3326 && row_ptr->par()->GetChar(row_ptr->pos()) == LyXParagraph::META_INSET
3327 && (inset = row_ptr->par()->GetInset(row_ptr->pos()))) {
3328 clear_area = inset->doClearArea();
3330 // we don't need to clear it's already done!!!
3333 } else if (clear_area) {
3334 #ifdef WITH_WARNINGS
3335 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
3336 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
3338 int const y = y_offset < 0 ? 0 : y_offset;
3339 int const h = y_offset < 0 ?
3340 row_ptr->height() + y_offset : row_ptr->height();
3341 int const w = inset_owner ?
3342 inset_owner->width(bview, font) - 2 : ww;
3343 int const x = x_offset;
3344 pain.fillRectangle(x, y, w, h);
3345 } else if (inset != 0) {
3346 int h = row_ptr->baseline() - inset->ascent(bview, font);
3348 int const w = (inset_owner ?
3349 inset_owner->width(bview, font) : ww);
3350 pain.fillRectangle(x_offset, y_offset, w, h);
3352 h += inset->ascent(bview, font) + inset->descent(bview, font);
3353 if ((row_ptr->height() - h) > 0) {
3354 int const w = (inset_owner ?
3355 inset_owner->width(bview, font) : ww);
3356 pain.fillRectangle(x_offset, y_offset + h,
3357 w, row_ptr->height() - h);
3359 if (!inset_owner && !inset->display() && !inset->needFullRow())
3361 int const w = inset->width(bview, font) + int(x);
3362 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
3367 int const w = (inset_owner ?
3368 inset_owner->width(bview, font) : ww);
3370 if (bidi_same_direction) {
3371 if (sel_start_cursor.row() == row_ptr &&
3372 sel_end_cursor.row() == row_ptr) {
3373 if (sel_start_cursor.x() < sel_end_cursor.x())
3374 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3376 sel_end_cursor.x() - sel_start_cursor.x(),
3380 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3382 sel_start_cursor.x() - sel_end_cursor.x(),
3385 } else if (sel_start_cursor.row() == row_ptr) {
3387 pain.fillRectangle(x_offset, y_offset,
3388 sel_start_cursor.x(),
3392 pain.fillRectangle(x_offset + sel_start_cursor.x(),
3394 w - sel_start_cursor.x(),
3397 } else if (sel_end_cursor.row() == row_ptr) {
3399 pain.fillRectangle(x_offset + sel_end_cursor.x(),
3401 w - sel_end_cursor.x(),
3405 pain.fillRectangle(x_offset, y_offset,
3409 } else if (y > sel_start_cursor.y()
3410 && y < sel_end_cursor.y()) {
3411 pain.fillRectangle(x_offset, y_offset, w,
3415 } else if (sel_start_cursor.row() != row_ptr &&
3416 sel_end_cursor.row() != row_ptr &&
3417 y > sel_start_cursor.y()
3418 && y < sel_end_cursor.y()) {
3419 pain.fillRectangle(x_offset, y_offset, w,
3422 } else if (sel_start_cursor.row() == row_ptr ||
3423 sel_end_cursor.row() == row_ptr) {
3425 if ((sel_start_cursor.row() != row_ptr && !is_rtl) ||
3426 (sel_end_cursor.row() != row_ptr && is_rtl))
3427 pain.fillRectangle(x_offset, y_offset,
3431 LyXParagraph::size_type main_body =
3432 BeginningOfMainBody(bview->buffer(),
3435 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3436 pos = vis2log(vpos);
3437 float const old_tmpx = tmpx;
3438 if (main_body > 0 && pos == main_body-1) {
3439 tmpx += fill_label_hfill +
3440 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3441 row_ptr->par()->GetLayout()).labelsep,
3442 GetFont(bview->buffer(),row_ptr->par(), -2));
3443 if (row_ptr->par()->IsLineSeparator(main_body-1))
3444 tmpx -= SingleWidth(bview, row_ptr->par(), main_body-1);
3446 if (HfillExpansion(bview->buffer(), row_ptr, pos)) {
3447 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3448 if (pos >= main_body)
3451 tmpx += fill_label_hfill;
3453 else if (row_ptr->par()->IsSeparator(pos)) {
3454 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3455 if (pos >= main_body)
3456 tmpx += fill_separator;
3458 tmpx += SingleWidth(bview, row_ptr->par(), pos);
3460 if ((sel_start_cursor.row() != row_ptr ||
3461 sel_start_cursor.pos() <= pos) &&
3462 (sel_end_cursor.row() != row_ptr ||
3463 pos < sel_end_cursor.pos()) )
3464 // Here we do not use x_offset as x_offset was
3466 pain.fillRectangle(int(old_tmpx),
3468 int(tmpx - old_tmpx + 1),
3473 if ((sel_start_cursor.row() != row_ptr && is_rtl) ||
3474 (sel_end_cursor.row() != row_ptr && !is_rtl) )
3475 pain.fillRectangle(x_offset + int(tmpx),
3485 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3486 LyXFont font(LyXFont::ALL_SANE);
3487 font.setSize(LyXFont::SIZE_FOOTNOTE);
3488 font.setColor(LColor::footnote);
3490 box_x = LYX_PAPER_MARGIN + lyxfont::width(" wide-tab ", font);
3491 if (row_ptr->previous() &&
3492 row_ptr->previous()->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE){
3494 switch (row_ptr->par()->footnotekind) {
3495 case LyXParagraph::MARGIN:
3498 case LyXParagraph::FIG:
3501 case LyXParagraph::TAB:
3504 case LyXParagraph::WIDE_FIG:
3507 case LyXParagraph::WIDE_TAB:
3510 case LyXParagraph::ALGORITHM:
3513 case LyXParagraph::FOOTNOTE:
3518 pain.fillRectangle(LYX_PAPER_MARGIN,
3520 box_x - LYX_PAPER_MARGIN,
3521 int(lyxfont::maxAscent(font)
3522 + lyxfont::maxDescent(font)),
3523 LColor::footnotebg);
3525 pain.line(LYX_PAPER_MARGIN, y_offset,
3526 workWidth(bview) - LYX_PAPER_MARGIN, y_offset,
3527 LColor::footnoteframe);
3529 pain.text(LYX_PAPER_MARGIN,
3530 y_offset + int(lyxfont::maxAscent(font)) + 1,
3533 pain.line(LYX_PAPER_MARGIN, y_offset,
3535 y_offset + int(lyxfont::maxAscent(font)
3536 + lyxfont::maxDescent(font)),
3537 LColor::footnoteframe);
3539 pain.line(LYX_PAPER_MARGIN,
3540 y_offset + int(lyxfont::maxAscent(font)
3541 + lyxfont::maxDescent(font)) + 1,
3543 y_offset + int(lyxfont::maxAscent(font)
3544 + lyxfont::maxDescent(font)) + 1,
3545 LColor::footnoteframe);
3549 /* draw the open floats in a red box */
3550 pain.line(box_x, y_offset,
3551 box_x, y_offset + row_ptr->height(),
3552 LColor::footnoteframe);
3554 pain.line(workWidth(bview) - LYX_PAPER_MARGIN,
3556 workWidth(bview) - LYX_PAPER_MARGIN,
3557 y_offset + row_ptr->height(),
3558 LColor::footnoteframe);
3561 // Draw appendix lines
3562 LyXParagraph * p = row_ptr->par()->PreviousBeforeFootnote()->FirstPhysicalPar();
3563 if (p->params.appendix()) {
3564 pain.line(1, y_offset,
3565 1, y_offset + row_ptr->height(),
3566 LColor::appendixline);
3567 pain.line(workWidth(bview) - 2, y_offset,
3568 workWidth(bview) - 2,
3569 y_offset + row_ptr->height(),
3570 LColor::appendixline);
3574 // Draw minipage line
3575 bool const minipage =
3576 (p->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3578 pain.line(LYX_PAPER_MARGIN/5, y_offset,
3580 y_offset + row_ptr->height() - 1,
3581 LColor::minipageline);
3584 int const depth = p->GetDepth();
3585 for (int i = 1; i <= depth; ++i) {
3586 int const line_x = (LYX_PAPER_MARGIN / 5) *
3588 pain.line(line_x, y_offset, line_x,
3589 y_offset + row_ptr->height() - 1,
3592 } else if (row_ptr->previous() &&
3593 row_ptr->previous()->par()->footnoteflag
3594 == LyXParagraph::OPEN_FOOTNOTE) {
3595 LyXFont font(LyXFont::ALL_SANE);
3596 font.setSize(LyXFont::SIZE_FOOTNOTE);
3598 int const box_x = LYX_PAPER_MARGIN
3599 + lyxfont::width(" wide-tab ", font);
3601 pain.line(box_x, y_offset,
3602 workWidth(bview) - LYX_PAPER_MARGIN,
3603 y_offset, LColor::footnote);
3606 // Draw appendix lines
3608 LyXParagraph * firstpar = row_ptr->par()->FirstPhysicalPar();
3610 LyXParagraph * firstpar = row_ptr->par();
3612 if (firstpar->params.appendix()) {
3613 pain.line(1, y_offset,
3614 1, y_offset + row_ptr->height(),
3615 LColor::appendixline);
3616 pain.line(ww - 2, y_offset,
3617 ww - 2, y_offset + row_ptr->height(),
3618 LColor::appendixline);
3621 // Draw minipage line
3622 bool const minipage =
3623 (firstpar->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE);
3625 pain.line(LYX_PAPER_MARGIN/5 + box_x, y_offset,
3626 LYX_PAPER_MARGIN/5 + box_x,
3627 y_offset + row_ptr->height() - 1,
3628 LColor::minipageline);
3631 int const depth = firstpar->GetDepth();
3635 if (row_ptr->next())
3637 if (row_ptr->par()->footnoteflag ==
3638 row_ptr->next()->par()->footnoteflag)
3639 next_depth = row_ptr->next()->par()->GetDepth();
3640 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3643 next_depth = row_ptr->next()->par()->GetDepth();
3645 if (row_ptr->previous())
3647 if (row_ptr->par()->footnoteflag ==
3648 row_ptr->previous()->par()->footnoteflag)
3649 prev_depth = row_ptr->previous()->par()->GetDepth();
3650 else if (row_ptr->par()->footnoteflag != LyXParagraph::OPEN_FOOTNOTE)
3653 prev_depth = row_ptr->previous()->par()->GetDepth();
3656 for (int i = 1; i <= depth; ++i) {
3657 int const line_x = (LYX_PAPER_MARGIN / 5) *
3662 ) + box_x + x_offset;
3663 pain.line(line_x, y_offset, line_x,
3664 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3668 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3671 pain.fillRectangle(line_x,
3672 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3673 LYX_PAPER_MARGIN / 5, 2,
3679 LyXLayout const & layout =
3680 textclasslist.Style(bview->buffer()->params.textclass,
3681 row_ptr->par()->GetLayout());
3684 int y_bottom = row_ptr->height();
3686 // is it a first row?
3687 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3689 // start of appendix?
3690 if (row_ptr->par()->params.startOfAppendix()) {
3691 pain.line(1, y_offset,
3693 LColor::appendixline);
3696 // think about the margins
3697 if (!row_ptr->previous() && bv_owner)
3698 y_top += LYX_PAPER_MARGIN;
3700 // draw a top pagebreak
3701 if (row_ptr->par()->params.pagebreakTop()) {
3703 pb_font.setColor(LColor::pagebreak).decSize();
3707 pain.line(0, y_offset + y_top + 2*DefaultHeight(),
3709 y_offset + y_top + 2 * DefaultHeight(),
3711 Painter::line_onoffdash)
3714 _("Page Break (top)"),
3717 LColor::background, false, w, a, d);
3718 pain.rectText((ww - w)/2,
3719 y_offset + y_top + 2 * DefaultHeight() + d,
3720 _("Page Break (top)"),
3723 LColor::background);
3724 y_top += 3 * DefaultHeight();
3727 if (row_ptr->par()->params.spaceTop().kind() == VSpace::VFILL) {
3729 pain.line(0, y_offset + 2 + y_top,
3730 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3733 pain.line(0, y_offset + y_top + 3 * DefaultHeight(),
3735 y_offset + y_top + 3 * DefaultHeight(),
3738 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3739 LYX_PAPER_MARGIN / 2,
3740 y_offset + y_top + 3 * DefaultHeight(),
3743 y_top += 3 * DefaultHeight();
3746 // think about user added space
3747 y_top += int(row_ptr->par()->params.spaceTop().inPixels(bview));
3749 // think about the parskip
3750 // some parskips VERY EASY IMPLEMENTATION
3751 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3752 if (layout.latextype == LATEX_PARAGRAPH
3753 && firstpar->GetDepth() == 0
3754 && firstpar->previous())
3755 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3756 else if (firstpar->previous()
3757 && textclasslist.Style(bview->buffer()->params.textclass,
3758 firstpar->previous()->GetLayout()).latextype == LATEX_PARAGRAPH
3759 && firstpar->previous()->GetDepth() == 0)
3760 // is it right to use defskip here, too? (AS)
3761 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3764 if (row_ptr->par()->params.lineTop()) {
3766 y_top += lyxfont::ascent('x',
3767 GetFont(bview->buffer(),
3768 row_ptr->par(), 0));
3769 int const w = (inset_owner ?
3770 inset_owner->width(bview, font) : ww);
3771 int const xp = static_cast<int>(inset_owner ? x : 0);
3772 pain.line(xp, y_offset + y_top,
3773 w, y_offset + y_top,
3775 Painter::line_solid,
3776 Painter::line_thick);
3778 y_top += lyxfont::ascent('x',GetFont(bview->buffer(),
3779 row_ptr->par(), 0));
3782 // should we print a label?
3783 if (layout.labeltype >= LABEL_STATIC
3784 && (layout.labeltype != LABEL_STATIC
3785 || layout.latextype != LATEX_ENVIRONMENT
3786 || row_ptr->par()->IsFirstInSequence())) {
3787 font = GetFont(bview->buffer(), row_ptr->par(), -2);
3788 if (!row_ptr->par()->GetLabelstring().empty()) {
3790 string const tmpstring =
3791 row_ptr->par()->GetLabelstring();
3793 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3794 if (bview->buffer()->params.secnumdepth >= 0) {
3795 // this is special code for
3796 // the chapter layout. This is
3797 // printed in an extra row
3798 // and has a pagebreak at
3800 float spacing_val = 1.0;
3801 if (!row_ptr->par()->params.spacing().isDefault()) {
3802 spacing_val = row_ptr->par()->params.spacing().getValue();
3804 spacing_val = bview->buffer()->params.spacing.getValue();
3807 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3808 + int(layout.parsep) * DefaultHeight();
3810 tmpx = ww - LeftMargin(bview, row_ptr) -
3811 lyxfont::width(tmpstring, font);
3812 pain.text(int(tmpx),
3813 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3818 tmpx = ww - LeftMargin(bview, row_ptr)
3819 + lyxfont::width(layout.labelsep, font);
3821 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
3822 LyXFont font(LyXFont::ALL_SANE);
3823 font.setSize(LyXFont::SIZE_SMALL);
3824 tmpx += lyxfont::width("Mwide-fixM", font);
3828 tmpx = x - lyxfont::width(layout.labelsep, font)
3829 - lyxfont::width(tmpstring, font);
3832 pain.text(int(tmpx),
3833 y_offset + row_ptr->baseline(),
3837 // the labels at the top of an environment.
3838 // More or less for bibliography
3839 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3840 layout.labeltype == LABEL_BIBLIO ||
3841 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3842 if (row_ptr->par()->IsFirstInSequence()) {
3843 font = GetFont(bview->buffer(),
3844 row_ptr->par(), -2);
3845 if (!row_ptr->par()->GetLabelstring().empty()) {
3846 string const tmpstring =
3847 row_ptr->par()->GetLabelstring();
3848 float spacing_val = 1.0;
3849 if (!row_ptr->par()->params.spacing().isDefault()) {
3850 spacing_val = row_ptr->par()->params.spacing().getValue();
3852 spacing_val = bview->buffer()->params.spacing.getValue();
3855 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3856 + (layout.labelbottomsep * DefaultHeight()));
3859 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3860 tmpx = ( (is_rtl ? LeftMargin(bview, row_ptr) : x)
3861 + ww - RightMargin(bview->buffer(), row_ptr) ) / 2;
3862 tmpx -= lyxfont::width(tmpstring, font) / 2;
3864 tmpx = ww - LeftMargin(bview, row_ptr) -
3865 lyxfont::width(tmpstring, font);
3866 pain.text(int(tmpx),
3867 y_offset + row_ptr->baseline()
3868 - row_ptr->ascent_of_text()
3874 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3875 font = GetFont(bview->buffer(), row_ptr->par(), -1);
3877 tmpx = ww - LeftMargin(bview, row_ptr)
3878 + lyxfont::width(layout.labelsep, font);
3880 tmpx = x - lyxfont::width(layout.labelsep, font)
3881 - row_ptr->par()->bibkey->width(bview, font);
3882 row_ptr->par()->bibkey->draw(bview, font,
3883 y_offset + row_ptr->baseline(),
3888 // is it a last row?
3890 LyXParagraph * par = row_ptr->par()->LastPhysicalPar();
3892 LyXParagraph * par = row_ptr->par();
3896 row_ptr->par()->ParFromPos(last + 1) == par
3898 row_ptr->par() == par
3900 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
3902 // think about the margins
3903 if (!row_ptr->next() && bv_owner)
3904 y_bottom -= LYX_PAPER_MARGIN;
3906 // draw a bottom pagebreak
3907 if (firstpar->params.pagebreakBottom()) {
3909 pb_font.setColor(LColor::pagebreak).decSize();
3910 int const y_place = y_offset + y_bottom
3911 - 2 * DefaultHeight();
3917 .line(0, y_place, ww, y_place,
3919 Painter::line_onoffdash)
3921 _("Page Break (bottom)"),
3924 LColor::background, false, w, a, d);
3925 pain.rectText((ww - w) / 2, y_place + d,
3926 _("Page Break (bottom)"),
3929 LColor::background);
3930 y_bottom -= 3 * DefaultHeight();
3933 if (firstpar->params.spaceBottom().kind() == VSpace::VFILL) {
3934 // draw a vfill bottom
3935 int const y_place = y_offset + y_bottom
3936 - 3 * DefaultHeight();
3938 pain.line(0, y_place,
3939 LYX_PAPER_MARGIN, y_place,
3941 pain.line(0, y_offset + y_bottom - 2,
3943 y_offset + y_bottom - 2,
3945 pain.line(LYX_PAPER_MARGIN / 2,
3947 LYX_PAPER_MARGIN / 2,
3948 y_offset + y_bottom - 2,
3950 y_bottom -= 3 * DefaultHeight();
3953 // think about user added space
3954 y_bottom -= int(firstpar->params.spaceBottom().inPixels(bview));
3956 if (firstpar->params.lineBottom()) {
3957 // draw a bottom line
3959 y_bottom -= lyxfont::ascent('x',
3960 GetFont(bview->buffer(),
3964 y_bottom -= lyxfont::ascent('x',
3965 GetFont(bview->buffer(),
3969 int const w = (inset_owner ?
3970 inset_owner->width(bview, font) : ww);
3971 int const xp = static_cast<int>(inset_owner ? x : 0);
3972 pain.line(xp, y_offset + y_bottom,
3973 w, y_offset + y_bottom,
3974 LColor::topline, Painter::line_solid,
3975 Painter::line_thick);
3977 y_bottom -= lyxfont::ascent('x',
3978 GetFont(bview->buffer(),
3982 y_bottom -= lyxfont::ascent('x',
3983 GetFont(bview->buffer(),
3990 int const endlabel =
3991 row_ptr->par()->GetEndLabel(bview->buffer()->params);
3994 case END_LABEL_FILLED_BOX:
3996 LyXFont const font = GetFont(bview->buffer(),
3997 row_ptr->par(), last);
3998 int const size = int(0.75 * lyxfont::maxAscent(font));
3999 int const y = (y_offset + row_ptr->baseline()) - size;
4000 int x = is_rtl ? LYX_PAPER_MARGIN
4001 : ww - LYX_PAPER_MARGIN - size;
4003 if (row_ptr->par()->footnoteflag == LyXParagraph::OPEN_FOOTNOTE)
4005 LyXFont font(LyXFont::ALL_SANE);
4006 font.setSize(LyXFont::SIZE_SMALL);
4007 x += lyxfont::width("Mwide-figM", font);
4009 x -= LYX_PAPER_MARGIN/2;
4011 if (row_ptr->fill() <= size)
4012 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
4013 if (endlabel == END_LABEL_BOX) {
4014 pain.line(x, y, x, y + size,
4016 pain.line(x + size, y, x + size , y + size,
4018 pain.line(x, y, x + size, y,
4020 pain.line(x, y + size, x + size, y + size,
4023 pain.fillRectangle(x, y, size, size,
4027 case END_LABEL_STATIC:
4029 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->GetLayout();
4030 string const tmpstring = textclasslist.
4031 Style(bview->buffer()->params.textclass,
4032 layout).endlabelstring();
4033 font = GetFont(bview->buffer(), row_ptr->par(), -2);
4034 int const tmpx = is_rtl ?
4035 int(x) - lyxfont::width(tmpstring, font)
4036 : ww - RightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
4037 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
4040 case END_LABEL_NO_LABEL:
4045 // draw the text in the pixmap
4047 vpos = row_ptr->pos();
4049 LyXParagraph::size_type main_body =
4050 BeginningOfMainBody(bview->buffer(), row_ptr->par());
4051 if (main_body > 0 &&
4052 (main_body-1 > last ||
4053 !row_ptr->par()->IsLineSeparator(main_body - 1)))
4056 while (vpos <= last) {
4057 pos = vis2log(vpos);
4058 if (main_body > 0 && pos == main_body - 1) {
4059 x += fill_label_hfill
4060 + lyxfont::width(layout.labelsep,
4061 GetFont(bview->buffer(),
4062 row_ptr->par(), -2))
4063 - SingleWidth(bview,
4068 if (row_ptr->par() ->IsHfill(pos)) {
4071 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4073 y_offset + row_ptr->baseline(),
4076 if (HfillExpansion(bview->buffer(),
4078 if (pos >= main_body) {
4080 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4081 int(x + fill_hfill),
4082 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4084 Painter::line_onoffdash);
4088 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4089 int(x + fill_label_hfill),
4090 y_offset + row_ptr->baseline() - DefaultHeight() / 4,
4092 Painter::line_onoffdash);
4094 x += fill_label_hfill;
4097 y_offset + row_ptr->baseline() - DefaultHeight() / 2,
4099 y_offset + row_ptr->baseline(),
4104 } else if (row_ptr->par()->IsSeparator(pos)) {
4105 x += SingleWidth(bview,
4106 row_ptr->par(), pos);
4107 if (pos >= main_body)
4108 x += fill_separator;
4111 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
4116 int LyXText::DefaultHeight() const
4118 LyXFont font(LyXFont::ALL_SANE);
4119 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
4123 /* returns the column near the specified x-coordinate of the row
4124 * x is set to the real beginning of this column */
4125 int LyXText::GetColumnNearX(BufferView * bview, Row * row, int & x,
4126 bool & boundary) const
4129 float fill_separator, fill_hfill, fill_label_hfill;
4131 PrepareToPrint(bview, row, tmpx, fill_separator,
4132 fill_hfill, fill_label_hfill);
4134 LyXParagraph::size_type vc = row->pos();
4135 LyXParagraph::size_type last = RowLastPrintable(row);
4136 LyXParagraph::size_type c = 0;
4137 LyXLayout const & layout =
4138 textclasslist.Style(bview->buffer()->params.textclass,
4139 row->par()->GetLayout());
4140 bool left_side = false;
4142 LyXParagraph::size_type
4143 main_body = BeginningOfMainBody(bview->buffer(), row->par());
4144 float last_tmpx = tmpx;
4146 if (main_body > 0 &&
4147 (main_body-1 > last ||
4148 !row->par()->IsLineSeparator(main_body - 1)))
4151 while (vc <= last && tmpx <= x) {
4154 if (main_body > 0 && c == main_body-1) {
4155 tmpx += fill_label_hfill +
4156 lyxfont::width(layout.labelsep,
4157 GetFont(bview->buffer(), row->par(), -2));
4158 if (row->par()->IsLineSeparator(main_body - 1))
4159 tmpx -= SingleWidth(bview, row->par(), main_body-1);
4162 if (HfillExpansion(bview->buffer(), row, c)) {
4163 x += SingleWidth(bview, row->par(), c);
4167 tmpx += fill_label_hfill;
4169 else if (row->par()->IsSeparator(c)) {
4170 tmpx += SingleWidth(bview, row->par(), c);
4172 tmpx+= fill_separator;
4174 tmpx += SingleWidth(bview, row->par(), c);
4178 if ((tmpx + last_tmpx) / 2 > x) {
4183 if (vc > last + 1) // This shouldn't happen.
4187 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
4188 // some speedup if rtl_support=false
4189 && (!row->next() || row->next()->par() != row->par());
4190 bool const rtl = (lastrow)
4191 ? row->par()->isRightToLeftPar(bview->buffer()->params)
4192 : false; // If lastrow is false, we don't need to compute
4193 // the value of rtl.
4195 if (row->pos() > last) // Row is empty?
4198 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
4199 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
4201 else if (vc == row->pos()) {
4203 if (bidi_level(c) % 2 == 1)
4206 c = vis2log(vc - 1);
4207 bool const rtl = (bidi_level(c) % 2 == 1);
4208 if (left_side == rtl) {
4210 boundary = IsBoundary(bview->buffer(), row->par(), c);
4214 if (row->pos() <= last && c > last
4215 && row->par()->IsNewline(last)) {
4216 if (bidi_level(last) % 2 == 0)
4217 tmpx -= SingleWidth(bview, row->par(), last);
4219 tmpx += SingleWidth(bview, row->par(), last);
4230 /* turn the selection into a new environment. If there is no selection,
4231 * create an empty environment */
4232 void LyXText::InsertFootnoteEnvironment(BufferView * bview,
4233 LyXParagraph::footnote_kind kind)
4235 /* no footnoteenvironment in a footnoteenvironment */
4236 if (cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4237 WriteAlert(_("Impossible operation"),
4238 _("You can't insert a float in a float!"),
4243 /* no marginpars in minipages */
4244 if (kind == LyXParagraph::MARGIN
4245 && cursor.par()->params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE) {
4246 WriteAlert(_("Impossible operation"),
4247 _("You can't insert a marginpar in a minipage!"),
4252 /* this doesnt make sense, if there is no selection */
4253 bool dummy_selection = false;
4255 sel_start_cursor = cursor; /* dummy selection */
4256 sel_end_cursor = cursor;
4257 dummy_selection = true;
4260 LyXParagraph * tmppar;
4262 /* a test to make sure there is not already a footnote
4263 * in the selection. */
4265 tmppar = sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos());
4267 while (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos()) &&
4268 tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
4269 tmppar = tmppar->next_;
4271 if (tmppar != sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())
4272 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
4273 WriteAlert(_("Impossible operation"),
4274 _("Float would include float!"),
4279 /* ok we have a selection. This is always between sel_start_cursor
4280 * and sel_end cursor */
4282 SetUndo(bview->buffer(), Undo::FINISH,
4283 sel_start_cursor.par()->ParFromPos(sel_start_cursor.pos())->previous_,
4284 sel_end_cursor.par()->ParFromPos(sel_end_cursor.pos())->next_);
4286 if (sel_end_cursor.pos() > 0
4287 && sel_end_cursor.par()->IsLineSeparator(sel_end_cursor.pos() - 1))
4288 sel_end_cursor.pos(sel_end_cursor.pos() - 1);
4289 /* please break before a space at the end */
4290 if (sel_start_cursor.par() == sel_end_cursor.par()
4291 && sel_start_cursor.pos() > sel_end_cursor.pos())
4292 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4294 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, sel_end_cursor.pos());
4296 sel_end_cursor.par(sel_end_cursor.par()->next());
4297 sel_end_cursor.pos(0);
4299 // don't forget to insert a dummy layout paragraph if necessary
4300 if (sel_start_cursor.par()->GetLayout() != sel_end_cursor.par()->layout){
4301 sel_end_cursor.par()->BreakParagraphConservative(bview->buffer()->params, 0);
4302 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4303 sel_end_cursor.par(sel_end_cursor.par()->next_);
4306 sel_end_cursor.par()->layout = LYX_DUMMY_LAYOUT;
4308 cursor = sel_end_cursor;
4310 /* please break behind a space, if there is one. The space should
4312 if (sel_start_cursor.pos() > 0
4313 && sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos() - 1))
4314 sel_start_cursor.pos(sel_start_cursor.pos() - 1);
4315 if (sel_start_cursor.par()->IsLineSeparator(sel_start_cursor.pos())) {
4316 sel_start_cursor.par()->Erase(sel_start_cursor.pos());
4319 sel_start_cursor.par()->BreakParagraphConservative(bview->buffer()->params,
4320 sel_start_cursor.pos());
4321 tmppar = sel_start_cursor.par()->next();
4323 if (dummy_selection) {
4325 if (kind == LyXParagraph::TAB
4326 || kind == LyXParagraph::FIG
4327 || kind == LyXParagraph::WIDE_TAB
4328 || kind == LyXParagraph::WIDE_FIG
4329 || kind == LyXParagraph::ALGORITHM) {
4330 pair<bool, LyXTextClass::size_type> lres =
4331 textclasslist.NumberOfLayout(bview->buffer()->params.textclass,
4333 LyXTextClass::size_type lay;
4339 lay = 0; // use default layout "Standard" (0)
4341 tmppar->SetLayout(bview->buffer()->params, lay);
4344 if (sel_start_cursor.pos() > 0) {
4345 /* the footnote-environment should begin with a standard layout.
4346 * Imagine you insert a footnote within an enumeration, you
4347 * certainly do not want an enumerated footnote! */
4350 /* this is a exception the user would sometimes expect, I hope */
4351 sel_start_cursor.par()->Clear();
4355 while (tmppar != sel_end_cursor.par()) {
4356 tmppar->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
4357 tmppar->footnotekind = kind;
4358 tmppar = tmppar->next();
4361 RedoParagraphs(bview, sel_start_cursor, sel_end_cursor.par()->next());
4363 SetCursor(bview, sel_start_cursor.par()->next(), 0);
4365 ClearSelection(bview);
4370 // returns pointer to a specified row
4371 Row * LyXText::GetRow(LyXParagraph * par,
4372 LyXParagraph::size_type pos, int & y) const
4377 Row * tmprow = firstrow;
4380 // find the first row of the specified paragraph
4381 while (tmprow->next() && tmprow->par() != par) {
4382 y += tmprow->height();
4383 tmprow = tmprow->next();
4386 // now find the wanted row
4387 while (tmprow->pos() < pos
4389 && tmprow->next()->par() == par
4390 && tmprow->next()->pos() <= pos) {
4391 y += tmprow->height();
4392 tmprow = tmprow->next();