1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
18 #include "paragraph.h"
19 #include "support/textutils.h"
20 #include "insets/insetbib.h"
21 #include "insets/insettext.h"
22 #include "insets/insetspecialchar.h"
23 #include "lyx_gui_misc.h"
25 #include "bufferparams.h"
34 #include "lyxscreen.h"
35 #include "bufferview_funcs.h"
36 #include "BufferView.h"
38 #include "ParagraphParameters.h"
39 #include "undo_funcs.h"
48 int const LYX_PAPER_MARGIN = 20;
52 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
55 int LyXText::workWidth(BufferView * bview) const
58 return inset_owner->textWidth(bview);
60 return bview->workWidth();
64 int LyXText::getRealCursorX(BufferView * bview) const
67 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
68 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
73 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
74 Paragraph::size_type pos) const
76 if (!Encodings::is_arabic(c))
77 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
78 return c + (0xb0 - '0');
82 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
83 unsigned char next_char = ' ';
85 for (Paragraph::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, Paragraph * par,
130 Paragraph::size_type pos) const
132 char const c = par->getChar(pos);
133 return singleWidth(bview, par, pos, c);
137 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
138 Paragraph::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 */
161 } else if (c == Paragraph::META_INSET) {
162 Inset * tmpinset = par->getInset(pos);
164 #if 0 // seems not to be needed, but ...
165 tmpinset->update(bview, font);
167 return tmpinset->width(bview, font);
171 } else if (IsSeparatorChar(c))
173 else if (IsNewlineChar(c))
175 return lyxfont::width(c, font);
179 // Returns the paragraph position of the last character in the specified row
180 Paragraph::size_type LyXText::rowLast(Row const * row) const
182 if (row->next() == 0)
183 return row->par()->size() - 1;
184 else if (row->next()->par() != row->par())
185 return row->par()->size() - 1;
187 return row->next()->pos() - 1;
191 Paragraph::size_type LyXText::rowLastPrintable(Row const * row) const
193 Paragraph::size_type const last = rowLast(row);
194 if (last >= row->pos()
196 && row->next()->par() == row->par()
197 && row->par()->isSeparator(last))
204 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
206 bidi_same_direction = true;
207 if (!lyxrc.rtl_support) {
212 bidi_start = row->pos();
213 bidi_end = rowLastPrintable(row);
215 if (bidi_start > bidi_end) {
220 if (bidi_end + 2 - bidi_start >
221 static_cast<Paragraph::size_type>(log2vis_list.size())) {
222 Paragraph::size_type new_size =
223 (bidi_end + 2 - bidi_start < 500) ?
224 500 : 2 * (bidi_end + 2 - bidi_start);
225 log2vis_list.resize(new_size);
226 vis2log_list.resize(new_size);
227 bidi_levels.resize(new_size);
230 vis2log_list[bidi_end + 1 - bidi_start] = -1;
231 log2vis_list[bidi_end + 1 - bidi_start] = -1;
233 Paragraph::size_type stack[2];
235 row->par()->getParLanguage(buf->params)->RightToLeft();
239 Paragraph::size_type const main_body =
240 beginningOfMainBody(buf, row->par());
242 for (Paragraph::size_type lpos = bidi_start;
243 lpos <= bidi_end; ++lpos) {
244 bool is_space = row->par()->isLineSeparator(lpos);
245 Paragraph::size_type const pos =
246 (is_space && lpos + 1 <= bidi_end &&
247 !row->par()->isLineSeparator(lpos + 1) &&
248 !row->par()->isNewline(lpos + 1))
250 LyXFont font = row->par()->getFontSettings(buf->params, pos);
251 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
252 font.number() == LyXFont::ON &&
253 row->par()->getFontSettings(buf->params, lpos-1).number()
255 font = row->par()->getFontSettings(buf->params, lpos);
260 bool new_rtl = font.isVisibleRightToLeft();
261 bool new_rtl0 = font.isRightToLeft();
264 if (lpos == main_body - 1
265 && row->pos() < main_body - 1
267 new_level = (rtl_par) ? 1 : 0;
268 new_rtl = new_rtl0 = rtl_par;
270 new_level = (new_rtl) ? 1 : 2;
272 new_level = (rtl_par) ? 2 : 0;
274 if (is_space && new_level >= level) {
280 int new_level2 = new_level;
282 if (level == new_level && rtl0 != new_rtl0) {
284 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
285 } else if (level < new_level) {
286 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
287 if (new_level > rtl_par)
288 bidi_same_direction = false;
290 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
293 bidi_levels[lpos - bidi_start] = new_level;
295 while (level > new_level2) {
296 Paragraph::size_type old_lpos =
298 int delta = lpos - old_lpos - 1;
301 log2vis_list[lpos - bidi_start] += delta;
302 log2vis_list[old_lpos - bidi_start] += delta;
304 while (level < new_level)
305 stack[level++] = lpos;
309 Paragraph::size_type const old_lpos = stack[--level];
310 int delta = bidi_end - old_lpos;
313 log2vis_list[old_lpos - bidi_start] += delta;
316 Paragraph::size_type vpos = bidi_start - 1;
317 for (Paragraph::size_type lpos = bidi_start;
318 lpos <= bidi_end; ++lpos) {
319 vpos += log2vis_list[lpos - bidi_start];
320 vis2log_list[vpos - bidi_start] = lpos;
321 log2vis_list[lpos - bidi_start] = vpos;
326 // This method requires a previous call to ComputeBidiTables()
327 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
328 Paragraph::size_type pos) const
330 if (!lyxrc.rtl_support || pos == 0)
333 if (!bidi_InRange(pos - 1)) {
334 /// This can happen if pos is the first char of a row.
335 /// Returning false in this case is incorrect!
339 bool const rtl = bidi_level(pos - 1) % 2;
340 bool const rtl2 = bidi_InRange(pos)
341 ? bidi_level(pos) % 2
342 : par->isRightToLeftPar(buf->params);
347 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
348 Paragraph::size_type pos,
349 LyXFont const & font) const
351 if (!lyxrc.rtl_support)
352 return false; // This is just for speedup
354 bool const rtl = font.isVisibleRightToLeft();
355 bool const rtl2 = bidi_InRange(pos)
356 ? bidi_level(pos) % 2
357 : par->isRightToLeftPar(buf->params);
362 void LyXText::draw(BufferView * bview, Row const * row,
363 Paragraph::size_type & vpos,
364 int offset, float & x, bool cleared)
366 Painter & pain = bview->painter();
368 Paragraph::size_type pos = vis2log(vpos);
369 char c = row->par()->getChar(pos);
372 if (IsNewlineChar(c)) {
374 // Draw end-of-line marker
375 LyXFont const font = getFont(bview->buffer(), row->par(), pos);
376 int const wid = lyxfont::width('n', font);
377 int const asc = lyxfont::maxAscent(font);
378 int const y = offset + row->baseline();
382 if (bidi_level(pos) % 2 == 0) {
383 xp[0] = int(x + wid * 0.375);
384 yp[0] = int(y - 0.875 * asc * 0.75);
387 yp[1] = int(y - 0.500 * asc * 0.75);
389 xp[2] = int(x + wid * 0.375);
390 yp[2] = int(y - 0.125 * asc * 0.75);
392 pain.lines(xp, yp, 3, LColor::eolmarker);
395 yp[0] = int(y - 0.500 * asc * 0.75);
397 xp[1] = int(x + wid);
398 yp[1] = int(y - 0.500 * asc * 0.75);
400 xp[2] = int(x + wid);
401 yp[2] = int(y - asc * 0.75);
403 pain.lines(xp, yp, 3, LColor::eolmarker);
405 xp[0] = int(x + wid * 0.625);
406 yp[0] = int(y - 0.875 * asc * 0.75);
408 xp[1] = int(x + wid);
409 yp[1] = int(y - 0.500 * asc * 0.75);
411 xp[2] = int(x + wid * 0.625);
412 yp[2] = int(y - 0.125 * asc * 0.75);
414 pain.lines(xp, yp, 3, LColor::eolmarker);
416 xp[0] = int(x + wid);
417 yp[0] = int(y - 0.500 * asc * 0.75);
420 yp[1] = int(y - 0.500 * asc * 0.75);
423 yp[2] = int(y - asc * 0.75);
425 pain.lines(xp, yp, 3, LColor::eolmarker);
431 LyXFont font = getFont(bview->buffer(), row->par(), pos);
432 LyXFont font2 = font;
434 if (c == Paragraph::META_INSET) {
435 Inset * tmpinset = row->par()->getInset(pos);
437 tmpinset->update(bview, font, false);
438 tmpinset->draw(bview, font, offset+row->baseline(), x,
440 #ifdef SEEMS_TO_BE_NOT_NEEDED
441 if (status_ == CHANGED_IN_DRAW) {
442 UpdateInset(bview, tmpinset);
443 status(bview, CHANGED_IN_DRAW);
449 if (lyxrc.mark_foreign_language &&
450 font.language() != bview->buffer()->params.language) {
451 int const y = offset + row->height() - 1;
452 pain.line(int(tmpx), y, int(x), y,
459 /* usual characters, no insets */
461 // Collect character that we can draw in one command
463 // This is dirty, but fast. Notice that it will never be too small.
464 // For the record, I'll note that Microsoft Word has a limit
465 // of 768 here. We have none :-) (Asger)
466 // Ok. I am the first to admit that the use of std::string will be
467 // a tiny bit slower than using a POD char array. However, I claim
468 // that this slowdown is so small that it is close to inperceptive.
469 // So IMHO we should go with the easier and clearer implementation.
470 // And even if 1024 is a large number here it might overflow, string
471 // will only overflow if the machine is out of memory...
472 static string textstring;
476 Paragraph::size_type const last = rowLastPrintable(row);
478 if (font.language()->lang() == "hebrew") {
479 if (Encodings::IsComposeChar_hebrew(c)) {
480 int const width = lyxfont::width(c, font2);
482 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
483 c = row->par()->getChar(i);
484 if (!Encodings::IsComposeChar_hebrew(c)) {
485 if (IsPrintableNonspace(c)) {
490 dx = (c == 'ø' || c == 'ã') // dalet / resh
491 ? width2 - width : (width2 - width) / 2;
497 pain.text(int(x) + dx, offset + row->baseline(),
500 while (vpos <= last &&
501 (pos = vis2log(vpos)) >= 0
502 && IsPrintableNonspace(c = row->par()->getChar(pos))
503 && !Encodings::IsComposeChar_hebrew(c)
504 && font2 == getFont(bview->buffer(), row->par(), pos)) {
508 // Draw text and set the new x position
509 pain.text(int(x), offset + row->baseline(),
511 x += lyxfont::width(textstring, font);
513 } else if (font.language()->lang() == "arabic" &&
514 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
515 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
516 if (Encodings::IsComposeChar_arabic(c)) {
517 c = transformChar(c, row->par(), pos);
519 int const width = lyxfont::width(c, font2);
521 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
522 c = row->par()->getChar(i);
523 if (!Encodings::IsComposeChar_arabic(c)) {
524 if (IsPrintableNonspace(c)) {
529 dx = (width2 - width) / 2;
535 pain.text(int(x) + dx, offset + row->baseline(),
538 textstring = transformChar(c, row->par(), pos);
539 while (vpos <= last &&
540 (pos = vis2log(vpos)) >= 0
541 && IsPrintableNonspace(c = row->par()->getChar(pos))
542 && !Encodings::IsComposeChar_arabic(c)
543 && font2 == getFont(bview->buffer(), row->par(), pos)) {
544 c = transformChar(c, row->par(), pos);
548 // Draw text and set the new x position
549 pain.text(int(x), offset + row->baseline(),
551 x += lyxfont::width(textstring, font);
554 while (vpos <= last &&
555 (pos = vis2log(vpos)) >= 0
556 && IsPrintableNonspace(c = row->par()->getChar(pos))
557 && font2 == getFont(bview->buffer(), row->par(), pos)) {
561 // Draw text and set the new x position
562 pain.text(int(x), offset + row->baseline(), textstring, font);
563 x += lyxfont::width(textstring, font);
566 if (lyxrc.mark_foreign_language &&
567 font.language() != bview->buffer()->params.language) {
568 int const y = offset + row->height() - 1;
569 pain.line(int(tmpx), y, int(x), y,
573 // If we want ulem.sty support, drawing
574 // routines should go here. (Asger)
575 // Why shouldn't LyXFont::drawText handle it internally?
579 // Returns the left beginning of the text.
580 // This information cannot be taken from the layouts-objekt, because in
581 // LaTeX the beginning of the text fits in some cases (for example sections)
582 // exactly the label-width.
583 int LyXText::leftMargin(BufferView * bview, Row const * row) const
585 LyXLayout const & layout =
586 textclasslist.Style(bview->buffer()->params.textclass,
587 row->par()->getLayout());
589 string parindent = layout.parindent;
591 int x = LYX_PAPER_MARGIN;
593 x += lyxfont::signedWidth(textclasslist
594 .TextClass(bview->buffer()->params.textclass)
597 .TextClass(bview->buffer()->params.textclass)
600 // this is the way, LyX handles the LaTeX-Environments.
601 // I have had this idea very late, so it seems to be a
602 // later added hack and this is true
603 if (!row->par()->getDepth()) {
604 if (!row->par()->getLayout()) {
605 // find the previous same level paragraph
606 if (row->par()->previous()) {
607 Paragraph * newpar = row->par()
608 ->depthHook(row->par()->getDepth());
610 textclasslist.Style(bview->buffer()->params.textclass,
617 // find the next level paragraph
620 row->par()->outerHook();
622 // make a corresponding row. Needed to call LeftMargin()
624 // check wether it is a sufficent paragraph
627 .Style(bview->buffer()->params.textclass,
628 newpar->getLayout()).isEnvironment()) {
630 dummyrow.par(newpar);
631 dummyrow.pos(newpar->size());
632 x = leftMargin(bview, &dummyrow);
634 // this is no longer an error, because this function
635 // is used to clear impossible depths after changing
636 // a layout. Since there is always a redo,
637 // LeftMargin() is always called
638 row->par()->params().depth(0);
641 if (newpar && !row->par()->getLayout()) {
642 if (newpar->params().noindent())
645 parindent = textclasslist
646 .Style(bview->buffer()->params.textclass,
647 newpar->getLayout()).parindent;
652 LyXFont const labelfont = getFont(bview->buffer(), row->par(), -2);
653 switch (layout.margintype) {
655 if (!layout.leftmargin.empty()) {
656 x += lyxfont::signedWidth(layout.leftmargin,
658 .TextClass(bview->buffer()->params.
662 if (!row->par()->getLabelstring().empty()) {
663 x += lyxfont::signedWidth(layout.labelindent,
665 x += lyxfont::width(row->par()->getLabelstring(),
667 x += lyxfont::width(layout.labelsep, labelfont);
671 x += lyxfont::signedWidth(layout.labelindent, labelfont);
672 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
673 if (!row->par()->getLabelWidthString().empty()) {
674 x += lyxfont::width(row->par()->getLabelWidthString(),
676 x += lyxfont::width(layout.labelsep, labelfont);
681 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
682 / (row->par()->getDepth() + 4);
684 case MARGIN_FIRST_DYNAMIC:
685 if (layout.labeltype == LABEL_MANUAL) {
686 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
687 x += lyxfont::signedWidth(layout.leftmargin,
690 x += lyxfont::signedWidth(layout.labelindent,
693 } else if (row->pos()
694 // Special case to fix problems with
696 || (layout.labeltype == LABEL_STATIC
697 && layout.latextype == LATEX_ENVIRONMENT
698 && ! row->par()->isFirstInSequence())) {
699 x += lyxfont::signedWidth(layout.leftmargin,
701 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
702 && layout.labeltype != LABEL_BIBLIO
703 && layout.labeltype !=
704 LABEL_CENTERED_TOP_ENVIRONMENT) {
705 x += lyxfont::signedWidth(layout.labelindent,
707 x += lyxfont::width(layout.labelsep, labelfont);
708 x += lyxfont::width(row->par()->getLabelstring(),
713 case MARGIN_RIGHT_ADDRESS_BOX:
715 // ok, a terrible hack. The left margin depends on the widest
716 // row in this paragraph. Do not care about footnotes, they
717 // are *NOT* allowed in the LaTeX realisation of this layout.
719 // find the first row of this paragraph
720 Row const * tmprow = row;
721 while (tmprow->previous()
722 && tmprow->previous()->par() == row->par())
723 tmprow = tmprow->previous();
725 int minfill = tmprow->fill();
726 while (tmprow->next() && tmprow->next()->par() == row->par()) {
727 tmprow = tmprow->next();
728 if (tmprow->fill() < minfill)
729 minfill = tmprow->fill();
732 x += lyxfont::signedWidth(layout.leftmargin,
734 .TextClass(bview->buffer()->params.textclass)
741 int align; // wrong type
743 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
744 align = layout.align;
746 align = row->par()->params().align();
748 // set the correct parindent
749 if (row->pos() == 0) {
750 if ((layout.labeltype == LABEL_NO_LABEL
751 || layout.labeltype == LABEL_TOP_ENVIRONMENT
752 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
753 || (layout.labeltype == LABEL_STATIC
754 && layout.latextype == LATEX_ENVIRONMENT
755 && ! row->par()->isFirstInSequence()))
756 && align == LYX_ALIGN_BLOCK
757 && !row->par()->params().noindent()
758 && (row->par()->layout ||
759 bview->buffer()->params.paragraph_separation ==
760 BufferParams::PARSEP_INDENT))
761 x += lyxfont::signedWidth(parindent,
763 .TextClass(bview->buffer()->params
766 else if (layout.labeltype == LABEL_BIBLIO) {
767 // ale970405 Right width for bibitems
768 x += bibitemMaxWidth(bview,textclasslist
769 .TextClass(bview->buffer()->params
778 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
780 LyXLayout const & layout =
781 textclasslist.Style(buf->params.textclass,
782 row->par()->getLayout());
784 int x = LYX_PAPER_MARGIN
785 + lyxfont::signedWidth(textclasslist
786 .TextClass(buf->params.textclass)
789 .TextClass(buf->params.textclass)
792 // this is the way, LyX handles the LaTeX-Environments.
793 // I have had this idea very late, so it seems to be a
794 // later added hack and this is true
795 if (row->par()->getDepth()) {
796 // find the next level paragraph
798 Paragraph * newpar = row->par();
801 newpar = newpar->previous();
803 && newpar->getDepth() >= row->par()->getDepth());
805 // make a corresponding row. Needed to call LeftMargin()
807 // check wether it is a sufficent paragraph
809 && textclasslist.Style(buf->params.textclass,
813 dummyrow.par(newpar);
815 x = rightMargin(buf, &dummyrow);
817 // this is no longer an error, because this function
818 // is used to clear impossible depths after changing
819 // a layout. Since there is always a redo,
820 // LeftMargin() is always called
821 row->par()->params().depth(0);
825 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
826 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
827 .TextClass(buf->params.textclass)
828 .defaultfont()) * 4 / (row->par()->getDepth()
834 int LyXText::labelEnd(BufferView * bview, Row const * row) const
836 if (textclasslist.Style(bview->buffer()->params.textclass,
837 row->par()->getLayout()).margintype
841 tmprow.pos(row->par()->size());
842 return leftMargin(bview, &tmprow); /* just the beginning
845 return 0; /* LabelEnd is only needed, if the
846 layout fills a flushleft
851 // get the next breakpoint in a given paragraph
853 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
855 Paragraph * par = row->par();
860 Paragraph::size_type const pos = row->pos();
863 // position of the last possible breakpoint
864 // -1 isn't a suitable value, but a flag
865 Paragraph::size_type last_separator = -1;
866 width -= rightMargin(bview->buffer(), row);
868 Paragraph::size_type const main_body =
869 beginningOfMainBody(bview->buffer(), par);
870 LyXLayout const & layout =
871 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
872 Paragraph::size_type i = pos;
874 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
875 /* special code for right address boxes, only newlines count */
876 while (i < par->size()) {
877 if (par->isNewline(i)) {
879 i = par->size() - 1; // this means break
881 } else if (par->getChar(i) == Paragraph::META_INSET &&
882 par->getInset(i) && par->getInset(i)->display()){
883 par->getInset(i)->display(false);
888 // Last position is an invariant
889 Paragraph::size_type const last =
891 // this is the usual handling
892 int x = leftMargin(bview, row);
893 bool doitonetime = true;
894 while (doitonetime || ((x < width) && (i < last))) {
896 char const c = par->getChar(i);
897 if (IsNewlineChar(c)) {
899 x = width; // this means break
900 } else if (c == Paragraph::META_INSET &&
903 // check wether a Display() inset is
904 // valid here. if not, change it to
906 if (par->getInset(i)->display() &&
907 (layout.isCommand() ||
908 (layout.labeltype == LABEL_MANUAL
909 && i < beginningOfMainBody(bview->buffer(), par)))) {
910 // display istn't allowd
911 par->getInset(i)->display(false);
912 x += singleWidth(bview, par, i, c);
913 } else if (par->getInset(i)->display() ||
914 par->getInset(i)->needFullRow()) {
915 // So break the line here
919 if (IsLineSeparatorChar(par->getChar(i+1)))
922 last_separator = last; // to avoid extra rows
924 last_separator = i - 1;
925 x = width; // this means break
927 x += singleWidth(bview, par, i, c);
930 if (IsLineSeparatorChar(c))
932 x += singleWidth(bview, par, i, c);
935 if (i == main_body) {
936 x += lyxfont::width(layout.labelsep,
937 getFont(bview->buffer(), par, -2));
938 if (par->isLineSeparator(i - 1))
939 x-= singleWidth(bview, par, i - 1);
940 int left_margin = labelEnd(bview, row);
945 // end of paragraph is always a suitable separator
946 if (i == last && x < width)
950 // well, if last_separator is still 0, the line isn't breakable.
951 // don't care and cut simply at the end
952 if (last_separator < 0) {
956 // manual labels cannot be broken in LaTeX, do not care
957 if (main_body && last_separator < main_body)
958 last_separator = main_body - 1;
960 return last_separator;
964 // returns the minimum space a row needs on the screen in pixel
965 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
971 // get the pure distance
972 Paragraph::size_type const last = rowLastPrintable(row);
974 // special handling of the right address boxes
975 if (textclasslist.Style(bview->buffer()->params.textclass,
976 row->par()->getLayout()).margintype
977 == MARGIN_RIGHT_ADDRESS_BOX) {
978 int const tmpfill = row->fill();
979 row->fill(0); // the minfill in MarginLeft()
980 w = leftMargin(bview, row);
983 w = leftMargin(bview, row);
985 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
986 row->par()->getLayout());
987 Paragraph::size_type const main_body =
988 beginningOfMainBody(bview->buffer(), row->par());
989 Paragraph::size_type i = row->pos();
992 if (main_body > 0 && i == main_body) {
993 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
994 if (row->par()->isLineSeparator(i - 1))
995 w -= singleWidth(bview, row->par(), i - 1);
996 int left_margin = labelEnd(bview, row);
1000 w += singleWidth(bview, row->par(), i);
1003 if (main_body > 0 && main_body > last) {
1004 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
1005 if (last >= 0 && row->par()->isLineSeparator(last))
1006 w -= singleWidth(bview, row->par(), last);
1007 int const left_margin = labelEnd(bview, row);
1008 if (w < left_margin)
1012 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1013 #ifdef WITH_WARNINGS
1014 #warning Please fix me (Jug!)
1024 // returns the minimum space a manual label needs on the screen in pixel
1025 int LyXText::labelFill(BufferView * bview, Row const * row) const
1027 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1028 // -1 because a label ends either with a space that is in the label,
1029 // or with the beginning of a footnote that is outside the label.
1031 // I don't understand this code in depth, but sometimes "last" is
1032 // less than 0 and this causes a crash. This fix seems to work
1033 // correctly, but I bet the real error is elsewhere. The bug is
1034 // triggered when you have an open footnote in a paragraph
1035 // environment with a manual label. (Asger)
1036 if (last < 0) last = 0;
1038 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1045 w += singleWidth(bview, row->par(), i);
1050 if (!row->par()->params().labelWidthString().empty()) {
1051 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1052 getFont(bview->buffer(), row->par(), -2)) - w,
1060 // returns the number of separators in the specified row. The separator
1061 // on the very last column doesnt count
1062 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1064 Paragraph::size_type const last = rowLast(row);
1065 Paragraph::size_type p =
1066 max(row->pos(), beginningOfMainBody(buf, row->par()));
1068 for (; p < last; ++p) {
1069 if (row->par()->isSeparator(p)) {
1077 // returns the number of hfills in the specified row. The LyX-Hfill is
1078 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1079 // ignored. This is *MUCH* more usefull than not to ignore!
1080 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1082 Paragraph::size_type const last = rowLast(row);
1083 Paragraph::size_type first = row->pos();
1084 if (first) { /* hfill *DO* count at the beginning
1086 while(first <= last && row->par()->isHfill(first))
1090 first = max(first, beginningOfMainBody(buf, row->par()));
1092 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1093 if (row->par()->isHfill(p)) {
1101 // like NumberOfHfills, but only those in the manual label!
1102 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1104 Paragraph::size_type last = rowLast(row);
1105 Paragraph::size_type first = row->pos();
1106 if (first) { /* hfill *DO* count at the beginning
1108 while(first < last && row->par()->isHfill(first))
1112 last = min(last, beginningOfMainBody(buf, row->par()));
1114 for (Paragraph::size_type p = first;
1115 p < last; ++p) { // last, because the end is ignored!
1116 if (row->par()->isHfill(p)) {
1124 // returns true, if a expansion is needed.
1125 // Rules are given by LaTeX
1126 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1127 Paragraph::size_type pos) const
1129 // by the way, is it a hfill?
1130 if (!row_ptr->par()->isHfill(pos))
1133 // at the end of a row it does not count
1134 if (pos >= rowLast(row_ptr))
1137 // at the beginning of a row it does not count, if it is not
1138 // the first row of a paragaph
1139 if (!row_ptr->pos())
1142 // in some labels it does not count
1143 if (textclasslist.Style(buf->params.textclass,
1144 row_ptr->par()->getLayout()).margintype
1146 && pos < beginningOfMainBody(buf, row_ptr->par()))
1149 // if there is anything between the first char of the row and
1150 // the sepcified position that is not a newline and not a hfill,
1151 // the hfill will count, otherwise not
1152 Paragraph::size_type i = row_ptr->pos();
1153 while (i < pos && (row_ptr->par()->isNewline(i)
1154 || row_ptr->par()->isHfill(i)))
1161 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1163 /* get the maximum ascent and the maximum descent */
1166 float layoutasc = 0;
1167 float layoutdesc = 0;
1170 Inset * tmpinset = 0;
1172 /* this must not happen before the currentrow for clear reasons.
1173 so the trick is just to set the current row onto this row */
1175 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1177 /* ok , let us initialize the maxasc and maxdesc value.
1178 * This depends in LaTeX of the font of the last character
1179 * in the paragraph. The hack below is necessary because
1180 * of the possibility of open footnotes */
1182 /* Correction: only the fontsize count. The other properties
1183 are taken from the layoutfont. Nicer on the screen :) */
1184 Paragraph * par = row_ptr->par();
1185 Paragraph * firstpar = row_ptr->par();
1187 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1188 firstpar->getLayout());
1190 LyXFont font = getFont(bview->buffer(), par, par->size() - 1);
1191 LyXFont::FONT_SIZE const size = font.size();
1192 font = getFont(bview->buffer(), par, -1);
1195 LyXFont labelfont = getFont(bview->buffer(), par, -2);
1197 float spacing_val = 1.0;
1198 if (!row_ptr->par()->params().spacing().isDefault()) {
1199 spacing_val = row_ptr->par()->params().spacing().getValue();
1201 spacing_val = bview->buffer()->params.spacing.getValue();
1203 //lyxerr << "spacing_val = " << spacing_val << endl;
1205 int maxasc = int(lyxfont::maxAscent(font) *
1206 layout.spacing.getValue() *
1208 int maxdesc = int(lyxfont::maxDescent(font) *
1209 layout.spacing.getValue() *
1211 int const pos_end = rowLast(row_ptr);
1215 // Check if any insets are larger
1216 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1217 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1218 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1219 tmpinset = row_ptr->par()->getInset(pos);
1221 #if 1 // this is needed for deep update on initialitation
1222 tmpinset->update(bview, tmpfont);
1224 asc = tmpinset->ascent(bview, tmpfont);
1225 desc = tmpinset->descent(bview, tmpfont);
1226 maxwidth += tmpinset->width(bview, tmpfont);
1227 maxasc = max(maxasc, asc);
1228 maxdesc = max(maxdesc, desc);
1231 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1235 // Check if any custom fonts are larger (Asger)
1236 // This is not completely correct, but we can live with the small,
1237 // cosmetic error for now.
1238 LyXFont::FONT_SIZE const maxsize =
1239 row_ptr->par()->highestFontInRange(row_ptr->pos(),
1241 if (maxsize > font.size()) {
1242 font.setSize(maxsize);
1244 asc = lyxfont::maxAscent(font);
1245 desc = lyxfont::maxDescent(font);
1252 // This is nicer with box insets:
1256 row_ptr->ascent_of_text(maxasc);
1258 // is it a top line?
1259 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1261 // some parksips VERY EASY IMPLEMENTATION
1262 if (bview->buffer()->params.paragraph_separation ==
1263 BufferParams::PARSEP_SKIP) {
1264 if (layout.isParagraph()
1265 && firstpar->getDepth() == 0
1266 && firstpar->previous())
1267 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1268 else if (firstpar->previous()
1269 && textclasslist.Style(bview->buffer()->params.textclass,
1270 firstpar->previous()->getLayout()).isParagraph()
1271 && firstpar->previous()->getDepth() == 0)
1272 // is it right to use defskip here too? (AS)
1273 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1276 // the paper margins
1277 if (!row_ptr->par()->previous() && bv_owner)
1278 maxasc += LYX_PAPER_MARGIN;
1280 // add the vertical spaces, that the user added
1281 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1282 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1284 // do not forget the DTP-lines!
1285 // there height depends on the font of the nearest character
1286 if (firstpar->params().lineTop())
1287 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1290 // and now the pagebreaks
1291 if (firstpar->params().pagebreakTop())
1292 maxasc += 3 * defaultHeight();
1294 // This is special code for the chapter, since the label of this
1295 // layout is printed in an extra row
1296 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1297 && bview->buffer()->params.secnumdepth >= 0) {
1298 float spacing_val = 1.0;
1299 if (!row_ptr->par()->params().spacing().isDefault()) {
1300 spacing_val = row_ptr->par()->params().spacing().getValue();
1302 spacing_val = bview->buffer()->params.spacing.getValue();
1305 labeladdon = int(lyxfont::maxDescent(labelfont) *
1306 layout.spacing.getValue() *
1308 + int(lyxfont::maxAscent(labelfont) *
1309 layout.spacing.getValue() *
1313 // special code for the top label
1314 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1315 || layout.labeltype == LABEL_BIBLIO
1316 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1317 && row_ptr->par()->isFirstInSequence()
1318 && !row_ptr->par()->getLabelstring().empty()) {
1319 float spacing_val = 1.0;
1320 if (!row_ptr->par()->params().spacing().isDefault()) {
1321 spacing_val = row_ptr->par()->params().spacing().getValue();
1323 spacing_val = bview->buffer()->params.spacing.getValue();
1327 (lyxfont::maxAscent(labelfont) *
1328 layout.spacing.getValue() *
1330 +(lyxfont::maxDescent(labelfont) *
1331 layout.spacing.getValue() *
1333 + layout.topsep * defaultHeight()
1334 + layout.labelbottomsep * defaultHeight());
1337 // and now the layout spaces, for example before and after a section,
1338 // or between the items of a itemize or enumerate environment
1340 if (!firstpar->params().pagebreakTop()) {
1341 Paragraph * prev = row_ptr->par()->previous();
1343 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1344 if (prev && prev->getLayout() == firstpar->getLayout()
1345 && prev->getDepth() == firstpar->getDepth()
1346 && prev->getLabelWidthString() == firstpar->getLabelWidthString())
1348 layoutasc = (layout.itemsep * defaultHeight());
1350 else if (row_ptr->previous()) {
1351 tmptop = layout.topsep;
1353 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1354 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1355 row_ptr->previous()->par()->
1356 getLayout()).bottomsep;
1359 layoutasc = (tmptop * defaultHeight());
1361 else if (row_ptr->par()->params().lineTop()) {
1362 tmptop = layout.topsep;
1365 layoutasc = (tmptop * defaultHeight());
1368 prev = row_ptr->par()->outerHook();
1370 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1371 prev->getLayout()).parsep * defaultHeight());
1374 if (firstpar->previous()
1375 && firstpar->previous()->getDepth() == 0
1376 && firstpar->previous()->getLayout() != firstpar->getLayout()) {
1379 else if (firstpar->previous()){
1380 maxasc += int(layout.parsep * defaultHeight());
1386 // is it a bottom line?
1387 if (row_ptr->par() == par
1388 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1390 // the paper margins
1391 if (!par->next() && bv_owner)
1392 maxdesc += LYX_PAPER_MARGIN;
1394 // add the vertical spaces, that the user added
1395 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1396 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1398 // do not forget the DTP-lines!
1399 // there height depends on the font of the nearest character
1400 if (firstpar->params().lineBottom())
1401 maxdesc += 2 * lyxfont::ascent('x',
1402 getFont(bview->buffer(),
1403 par, par->size() - 1));
1405 // and now the pagebreaks
1406 if (firstpar->params().pagebreakBottom())
1407 maxdesc += 3 * defaultHeight();
1409 // and now the layout spaces, for example before and after
1410 // a section, or between the items of a itemize or enumerate
1412 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1413 Paragraph * nextpar = row_ptr->par()->next();
1414 Paragraph * comparepar = row_ptr->par();
1418 if (comparepar->getDepth() > nextpar->getDepth()) {
1419 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1420 comparepar = comparepar->depthHook(nextpar->getDepth());
1421 if (comparepar->getLayout()!= nextpar->getLayout()
1422 || nextpar->getLabelWidthString() !=
1423 comparepar->getLabelWidthString())
1424 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1426 if (unusual > usual)
1427 layoutdesc = unusual;
1431 else if (comparepar->getDepth() == nextpar->getDepth()) {
1433 if (comparepar->getLayout()!= nextpar->getLayout()
1434 || nextpar->getLabelWidthString() !=
1435 comparepar->getLabelWidthString())
1436 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1441 // incalculate the layout spaces
1442 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1443 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1445 // calculate the new height of the text
1446 height -= row_ptr->height();
1448 row_ptr->height(maxasc + maxdesc + labeladdon);
1449 row_ptr->baseline(maxasc + labeladdon);
1451 height += row_ptr->height();
1454 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1455 row_ptr->width(int(maxwidth + x));
1458 width = max(0,workWidth(bview));
1460 if (r->width() > width)
1468 /* Appends the implicit specified paragraph behind the specified row,
1469 * start at the implicit given position */
1470 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1472 bool not_ready = true;
1474 // The last character position of a paragraph is an invariant so we can
1475 // safely get it here. (Asger)
1476 int const lastposition = row->par()->size();
1478 // Get the next breakpoint
1479 int z = nextBreakPoint(bview, row, workWidth(bview));
1483 // Insert the new row
1484 if (z < lastposition) {
1486 insertRow(row, row->par(), z);
1493 // Set the dimensions of the row
1494 #ifdef WITH_WARNINGS
1495 #warning Something is rotten here! (Jug)
1497 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1498 setHeightOfRow(bview, tmprow);
1500 } while (not_ready);
1504 void LyXText::breakAgain(BufferView * bview, Row * row) const
1506 bool not_ready = true;
1509 // get the next breakpoint
1510 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1513 if (z < row->par()->size()) {
1514 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1517 insertRow(row, row->par(), z);
1523 if (row->pos() == z)
1524 not_ready = false; // the rest will not change
1530 /* if there are some rows too much, delete them */
1531 /* only if you broke the whole paragraph! */
1532 Row * tmprow2 = row;
1533 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1534 tmprow2 = tmprow2->next();
1536 while (tmprow2 != row) {
1537 tmprow2 = tmprow2->previous();
1538 removeRow(tmprow2->next());
1543 /* set the dimensions of the row */
1544 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1545 setHeightOfRow(bview, tmprow);
1546 } while (not_ready);
1550 // this is just a little changed version of break again
1551 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1553 // get the next breakpoint
1554 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1557 if (z < row->par()->size()) {
1559 || (row->next() && row->next()->par() != row->par())) {
1560 /* insert a new row */
1562 insertRow(row, row->par(), z);
1568 if (row->pos() != z)
1572 // if there are some rows too much, delete them
1573 // only if you broke the whole paragraph!
1574 Row * tmprow2 = row;
1575 while (tmprow2->next()
1576 && tmprow2->next()->par() == row->par()) {
1577 tmprow2 = tmprow2->next();
1579 while (tmprow2 != row) {
1580 tmprow2 = tmprow2->previous();
1581 removeRow(tmprow2->next());
1585 // set the dimensions of the row
1586 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1587 setHeightOfRow(bview, tmprow);
1591 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1593 LyXLayout const & layout =
1594 textclasslist.Style(bview->buffer()->params.textclass,
1595 cursor.par()->getLayout());
1597 // this is only allowed, if the current paragraph is not empty or caption
1598 if ((cursor.par()->size() <= 0)
1599 && layout.labeltype!= LABEL_SENSITIVE)
1602 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1604 // Always break behind a space
1606 // It is better to erase the space (Dekel)
1607 if (cursor.pos() < cursor.par()->size()
1608 && cursor.par()->isLineSeparator(cursor.pos()))
1609 cursor.par()->erase(cursor.pos());
1610 // cursor.pos(cursor.pos() + 1);
1612 // break the paragraph
1616 keep_layout = layout.isEnvironment();
1617 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1620 // well this is the caption hack since one caption is really enough
1621 if (layout.labeltype == LABEL_SENSITIVE) {
1623 // set to standard-layout
1624 cursor.par()->setLayout(0);
1626 // set to standard-layout
1627 cursor.par()->next()->setLayout(0);
1630 /* if the cursor is at the beginning of a row without prior newline,
1632 * This touches only the screen-update. Otherwise we would may have
1633 * an empty row on the screen */
1634 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1635 && cursor.row()->pos() == cursor.pos()) {
1639 status(bview, LyXText::NEED_MORE_REFRESH);
1640 refresh_row = cursor.row();
1641 refresh_y = cursor.y() - cursor.row()->baseline();
1643 // Do not forget the special right address boxes
1644 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1645 while (refresh_row->previous() &&
1646 refresh_row->previous()->par() == refresh_row->par()) {
1647 refresh_row = refresh_row->previous();
1648 refresh_y -= refresh_row->height();
1651 removeParagraph(cursor.row());
1653 // set the dimensions of the cursor row
1654 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1656 setHeightOfRow(bview, cursor.row());
1658 while (cursor.par()->next()->size()
1659 && cursor.par()->next()->isNewline(0))
1660 cursor.par()->next()->erase(0);
1662 insertParagraph(bview, cursor.par()->next(), cursor.row());
1664 updateCounters(bview, cursor.row()->previous());
1666 /* This check is necessary. Otherwise the new empty paragraph will
1667 * be deleted automatically. And it is more friendly for the user! */
1669 setCursor(bview, cursor.par()->next(), 0);
1671 setCursor(bview, cursor.par(), 0);
1673 if (cursor.row()->next())
1674 breakAgain(bview, cursor.row()->next());
1680 // Just a macro to make some thing easier.
1681 void LyXText::redoParagraph(BufferView * bview) const
1683 clearSelection(bview);
1684 redoParagraphs(bview, cursor, cursor.par()->next());
1685 setCursorIntern(bview, cursor.par(), cursor.pos());
1689 /* insert a character, moves all the following breaks in the
1690 * same Paragraph one to the right and make a rebreak */
1691 void LyXText::insertChar(BufferView * bview, char c)
1693 setUndo(bview, Undo::INSERT,
1694 cursor.par(), cursor.par()->next());
1696 // When the free-spacing option is set for the current layout,
1697 // disable the double-space checking
1699 bool const freeSpacing =
1700 textclasslist.Style(bview->buffer()->params.textclass,
1701 cursor.row()->par()->getLayout()).free_spacing;
1704 if (lyxrc.auto_number) {
1705 static string const number_operators = "+-/*";
1706 static string const number_unary_operators = "+-";
1707 static string const number_seperators = ".,:";
1709 if (current_font.number() == LyXFont::ON) {
1710 if (!isdigit(c) && !contains(number_operators, c) &&
1711 !(contains(number_seperators, c) &&
1712 cursor.pos() >= 1 &&
1713 cursor.pos() < cursor.par()->size() &&
1714 getFont(bview->buffer(),
1716 cursor.pos()).number() == LyXFont::ON &&
1717 getFont(bview->buffer(),
1719 cursor.pos()-1).number() == LyXFont::ON)
1721 Number(bview); // Set current_font.number to OFF
1722 } else if (isdigit(c) &&
1723 real_current_font.isVisibleRightToLeft()) {
1724 Number(bview); // Set current_font.number to ON
1726 if (cursor.pos() > 0) {
1727 char const c = cursor.par()->getChar(cursor.pos() - 1);
1728 if (contains(number_unary_operators, c) &&
1729 (cursor.pos() == 1 ||
1730 cursor.par()->isSeparator(cursor.pos() - 2) ||
1731 cursor.par()->isNewline(cursor.pos() - 2) )
1733 setCharFont(bview->buffer(),
1737 } else if (contains(number_seperators, c) &&
1738 cursor.pos() >= 2 &&
1739 getFont(bview->buffer(),
1741 cursor.pos()-2).number() == LyXFont::ON) {
1742 setCharFont(bview->buffer(),
1752 /* First check, if there will be two blanks together or a blank at
1753 the beginning of a paragraph.
1754 I decided to handle blanks like normal characters, the main
1755 difference are the special checks when calculating the row.fill
1756 (blank does not count at the end of a row) and the check here */
1758 // The bug is triggered when we type in a description environment:
1759 // The current_font is not changed when we go from label to main text
1760 // and it should (along with realtmpfont) when we type the space.
1761 // CHECK There is a bug here! (Asger)
1763 LyXFont realtmpfont = real_current_font;
1764 LyXFont rawtmpfont = current_font; /* store the current font.
1765 * This is because of the use
1766 * of cursor movements. The moving
1767 * cursor would refresh the
1770 // Get the font that is used to calculate the baselineskip
1771 Paragraph::size_type const lastpos = cursor.par()->size();
1772 LyXFont rawparfont =
1773 cursor.par()->getFontSettings(bview->buffer()->params,
1776 bool jumped_over_space = false;
1778 if (!freeSpacing && IsLineSeparatorChar(c)) {
1779 if ((cursor.pos() > 0
1780 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1781 || (cursor.pos() > 0
1782 && cursor.par()->isNewline(cursor.pos() - 1))
1783 || (cursor.pos() == 0)) {
1784 static bool sent_space_message = false;
1785 if (!sent_space_message) {
1786 if (cursor.pos() == 0)
1787 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1789 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1790 sent_space_message = true;
1795 } else if (IsNewlineChar(c)) {
1796 if (cursor.par() == cursor.par()
1797 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1801 /* No newline at first position
1802 * of a paragraph or behind labels.
1803 * TeX does not allow that. */
1805 if (cursor.pos() < cursor.par()->size() &&
1806 cursor.par()->isLineSeparator(cursor.pos()))
1807 // newline always after a blank!
1809 cursor.row()->fill(-1); // to force a new break
1812 // the display inset stuff
1813 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1814 && cursor.row()->par()->getInset(cursor.row()->pos())
1815 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1816 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1817 cursor.row()->fill(-1); // to force a new break
1819 // get the cursor row fist
1820 Row * row = cursor.row();
1821 int y = cursor.y() - row->baseline();
1822 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1823 * already insertet the character */
1824 cursor.par()->insertChar(cursor.pos(), c);
1825 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1827 if (!jumped_over_space) {
1828 // refresh the positions
1830 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1831 tmprow = tmprow->next();
1832 tmprow->pos(tmprow->pos() + 1);
1836 // Is there a break one row above
1837 if ((cursor.par()->isLineSeparator(cursor.pos())
1838 || cursor.par()->isNewline(cursor.pos())
1839 || cursor.row()->fill() == -1)
1840 && row->previous() && row->previous()->par() == row->par()) {
1841 Paragraph::size_type z = nextBreakPoint(bview,
1844 if (z >= row->pos()) {
1847 // set the dimensions of the row above
1848 row->previous()->fill(fill(bview,
1852 setHeightOfRow(bview, row->previous());
1854 y -= row->previous()->height();
1856 refresh_row = row->previous();
1857 status(bview, LyXText::NEED_MORE_REFRESH);
1859 breakAgainOneRow(bview, row);
1861 current_font = rawtmpfont;
1862 real_current_font = realtmpfont;
1863 setCursor(bview, cursor.par(), cursor.pos() + 1,
1864 false, cursor.boundary());
1865 // cursor MUST be in row now.
1867 if (row->next() && row->next()->par() == row->par())
1868 need_break_row = row->next();
1872 // check, wether the last characters font has changed.
1873 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1874 && rawparfont != rawtmpfont)
1875 redoHeightOfParagraph(bview, cursor);
1882 // recalculate the fill of the row
1883 if (row->fill() >= 0) /* needed because a newline
1884 * will set fill to -1. Otherwise
1885 * we would not get a rebreak! */
1886 row->fill(fill(bview, row, workWidth(bview)));
1887 if (row->fill() < 0) {
1890 refresh_x = cursor.x();
1891 refresh_pos = cursor.pos();
1892 status(bview, LyXText::NEED_MORE_REFRESH);
1893 breakAgainOneRow(bview, row);
1894 // will the cursor be in another row now?
1895 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1896 if (row->next() && row->next()->par() == row->par())
1897 // this should always be true
1899 breakAgainOneRow(bview, row);
1901 current_font = rawtmpfont;
1902 real_current_font = realtmpfont;
1904 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1906 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1907 != cursor.boundary())
1908 setCursor(bview, cursor.par(), cursor.pos(), false,
1909 !cursor.boundary());
1910 if (row->next() && row->next()->par() == row->par())
1911 need_break_row = row->next();
1916 refresh_x = cursor.x();
1918 refresh_pos = cursor.pos();
1920 int const tmpheight = row->height();
1921 setHeightOfRow(bview, row);
1922 if (tmpheight == row->height())
1923 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1925 status(bview, LyXText::NEED_MORE_REFRESH);
1927 current_font = rawtmpfont;
1928 real_current_font = realtmpfont;
1929 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1933 // check, wether the last characters font has changed.
1934 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1935 && rawparfont != rawtmpfont) {
1936 redoHeightOfParagraph(bview, cursor);
1938 // now the special right address boxes
1939 if (textclasslist.Style(bview->buffer()->params.textclass,
1940 cursor.par()->getLayout()).margintype
1941 == MARGIN_RIGHT_ADDRESS_BOX) {
1942 redoDrawingOfParagraph(bview, cursor);
1950 void LyXText::charInserted()
1952 // Here we could call FinishUndo for every 20 characters inserted.
1953 // This is from my experience how emacs does it.
1954 static unsigned int counter;
1964 void LyXText::prepareToPrint(BufferView * bview,
1965 Row * row, float & x,
1966 float & fill_separator,
1968 float & fill_label_hfill,
1974 float w = row->fill();
1976 fill_label_hfill = 0;
1978 fill_label_hfill = 0;
1981 row->par()->isRightToLeftPar(bview->buffer()->params);
1983 x = (workWidth(bview) > 0)
1984 ? rightMargin(bview->buffer(), row) : 0;
1986 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
1988 // is there a manual margin with a manual label
1989 if (textclasslist.Style(bview->buffer()->params.textclass,
1990 row->par()->getLayout()).margintype == MARGIN_MANUAL
1991 && textclasslist.Style(bview->buffer()->params.textclass,
1992 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
1994 /* one more since labels are left aligned */
1995 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
1996 if (nlh && !row->par()->getLabelWidthString().empty()) {
1997 fill_label_hfill = labelFill(bview, row) / nlh;
2001 // are there any hfills in the row?
2002 float const nh = numberOfHfills(bview->buffer(), row);
2005 fill_hfill = w / nh;
2007 // is it block, flushleft or flushright?
2008 // set x how you need it
2010 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
2011 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2013 align = row->par()->params().align();
2015 // center displayed insets
2017 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2018 && (inset=row->par()->getInset(row->pos()))
2019 && (inset->display())) // || (inset->scroll() < 0)))
2020 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2021 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2024 case LYX_ALIGN_BLOCK:
2025 ns = numberOfSeparators(bview->buffer(), row);
2026 if (ns && row->next() && row->next()->par() == row->par() &&
2027 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2028 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2029 && row->next()->par()->getInset(row->next()->pos())
2030 && row->next()->par()->getInset(row->next()->pos())->display())
2032 fill_separator = w / ns;
2036 case LYX_ALIGN_RIGHT:
2039 case LYX_ALIGN_CENTER:
2047 computeBidiTables(bview->buffer(), row);
2049 Paragraph::size_type main_body =
2050 beginningOfMainBody(bview->buffer(), row->par());
2051 Paragraph::size_type last = rowLast(row);
2053 if (main_body > 0 &&
2054 (main_body-1 > last ||
2055 !row->par()->isLineSeparator(main_body-1))) {
2056 LyXLayout const & layout =
2057 textclasslist.Style(bview->buffer()->params.textclass,
2058 row->par()->getLayout());
2059 x += lyxfont::width(layout.labelsep,
2060 getFont(bview->buffer(), row->par(), -2));
2061 if (main_body-1 <= last)
2062 x += fill_label_hfill;
2067 /* important for the screen */
2070 /* the cursor set functions have a special mechanism. When they
2071 * realize, that you left an empty paragraph, they will delete it.
2072 * They also delete the corresponding row */
2074 void LyXText::cursorRightOneWord(BufferView * bview) const
2076 // treat floats, HFills and Insets as words
2077 LyXCursor tmpcursor = cursor;
2078 // CHECK See comment on top of text.C
2080 if (tmpcursor.pos() == tmpcursor.par()->size()
2081 && tmpcursor.par()->next()) {
2082 tmpcursor.par(tmpcursor.par()->next());
2087 // Skip through initial nonword stuff.
2088 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2089 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2090 // printf("Current pos1 %d", tmpcursor.pos()) ;
2091 tmpcursor.pos(tmpcursor.pos() + 1);
2094 // Advance through word.
2095 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2096 tmpcursor.par()->isWord( tmpcursor.pos())) {
2097 // printf("Current pos2 %d", tmpcursor.pos()) ;
2098 tmpcursor.pos(tmpcursor.pos() + 1);
2102 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2106 void LyXText::cursorTab(BufferView * bview) const
2108 LyXCursor tmpcursor = cursor;
2109 while (tmpcursor.pos() < tmpcursor.par()->size()
2110 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2111 tmpcursor.pos(tmpcursor.pos() + 1);
2113 if (tmpcursor.pos() == tmpcursor.par()->size()){
2114 if (tmpcursor.par()->next()) {
2115 tmpcursor.par(tmpcursor.par()->next());
2119 tmpcursor.pos(tmpcursor.pos() + 1);
2120 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2124 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2125 of prior word, not to end of next prior word. */
2127 void LyXText::cursorLeftOneWord(BufferView * bview) const
2129 LyXCursor tmpcursor = cursor;
2130 cursorLeftOneWord(tmpcursor);
2131 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2134 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2136 // treat HFills, floats and Insets as words
2139 && (cur.par()->isSeparator(cur.pos() - 1)
2140 || cur.par()->isKomma(cur.pos() - 1))
2141 && !(cur.par()->isHfill(cur.pos() - 1)
2142 || cur.par()->isInset(cur.pos() - 1)))
2143 cur.pos(cur.pos() - 1);
2146 && (cur.par()->isInset(cur.pos() - 1)
2147 || cur.par()->isHfill(cur.pos() - 1))) {
2148 cur.pos(cur.pos() - 1);
2149 } else if (!cur.pos()) {
2150 if (cur.par()->previous()){
2151 cur.par(cur.par()->previous());
2152 cur.pos(cur.par()->size());
2154 } else { // Here, cur != 0
2155 while (cur.pos() > 0 &&
2156 cur.par()->isWord(cur.pos()-1) )
2157 cur.pos(cur.pos() - 1);
2161 /* -------> Select current word. This depends on behaviour of
2162 CursorLeftOneWord(), so it is patched as well. */
2163 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2164 word_location const loc) const
2166 // first put the cursor where we wana start to select the word
2170 // Move cursor to the beginning, when not already there.
2171 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2172 && !from.par()->isKomma(from.pos() - 1))
2173 cursorLeftOneWord(from);
2176 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2182 while (to.pos() < to.par()->size()
2183 && !to.par()->isSeparator(to.pos())
2184 && !to.par()->isKomma(to.pos())
2185 && !to.par()->isHfill(to.pos()) )
2187 to.pos(to.pos() + 1);
2192 void LyXText::selectWord(BufferView * bview, word_location const loc)
2196 getWord(from, to, loc);
2198 setCursor(bview, from.par(), from.pos());
2199 selection.cursor = cursor;
2200 setCursor(bview, to.par(), to.pos() );
2201 setSelection(bview);
2204 /* -------> Select the word currently under the cursor when:
2205 1: no selection is currently set,
2206 [disabled] 2: the cursor is not at the borders of the word. */
2208 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2209 word_location const loc)
2211 if (!selection.set()
2213 && cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2214 && !cursor.par()->isSeparator(cursor.pos())
2215 && !cursor.par()->isKomma(cursor.pos())
2216 && !cursor.par()->isSeparator(cursor.pos() -1)
2217 && !cursor.par()->isKomma(cursor.pos() -1)
2220 selectWord(bview, loc);
2227 // This function is only used by the spellchecker for NextWord().
2228 // It doesn't handle LYX_ACCENTs and probably never will.
2229 string const LyXText::selectNextWord(BufferView * bview,
2230 float & value) const
2232 if (the_locking_inset) {
2233 string str = the_locking_inset->selectNextWord(bview, value);
2235 value += float(cursor.y())/float(height);
2238 #warning Dekel please have a look on this one RTL? (Jug)
2239 // we have to go on checking so move cusor to the right
2240 if (cursor.pos() == cursor.par()->size()) {
2241 if (!cursor.par()->next())
2243 cursor.par(cursor.par()->next());
2246 cursor.pos(cursor.pos() + 1);
2248 Paragraph * tmppar = cursor.par();
2250 // If this is not the very first word, skip rest of
2251 // current word because we are probably in the middle
2252 // of a word if there is text here.
2253 if (cursor.pos() || cursor.par()->previous()) {
2254 while (cursor.pos() < cursor.par()->size()
2255 && cursor.par()->isLetter(cursor.pos()))
2256 cursor.pos(cursor.pos() + 1);
2259 // Now, skip until we have real text (will jump paragraphs)
2260 while ((cursor.par()->size() > cursor.pos()
2261 && (!cursor.par()->isLetter(cursor.pos())
2263 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2264 .latex() == LyXFont::ON
2267 && (!cursor.par()->isInset(cursor.pos()) ||
2268 !cursor.par()->getInset(cursor.pos())->isTextInset()))
2269 || (cursor.par()->size() == cursor.pos()
2270 && cursor.par()->next()))
2272 if (cursor.pos() == cursor.par()->size()) {
2273 cursor.par(cursor.par()->next());
2276 cursor.pos(cursor.pos() + 1);
2279 // now check if we hit an inset so it has to be a inset containing text!
2280 if (cursor.pos() < cursor.par()->size() &&
2281 cursor.par()->isInset(cursor.pos()))
2284 cursor.par()->getInset(cursor.pos())->edit(bview);
2285 // now call us again to do the above trick
2286 // but obviously we have to start from down below ;)
2287 return bview->text->selectNextWord(bview, value);
2290 // Update the value if we changed paragraphs
2291 if (cursor.par() != tmppar){
2292 setCursor(bview, cursor.par(), cursor.pos());
2293 value = float(cursor.y())/float(height);
2296 // Start the selection from here
2297 selection.cursor = cursor;
2301 // and find the end of the word
2302 // (optional hyphens are part of a word)
2303 while (cursor.pos() < cursor.par()->size()
2304 && (cursor.par()->isLetter(cursor.pos()))
2305 // assignment is intentional here
2306 || ((inset = getInset())
2307 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2308 && static_cast<InsetSpecialChar *>(inset)->kind()
2309 == InsetSpecialChar::HYPHENATION
2311 cursor.pos(cursor.pos() + 1);
2313 // Finally, we copy the word to a string and return it
2315 if (selection.cursor.pos() < cursor.pos()) {
2316 Paragraph::size_type i;
2317 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2318 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2319 str += cursor.par()->getChar(i);
2326 // This one is also only for the spellchecker
2327 void LyXText::selectSelectedWord(BufferView * bview)
2329 if (the_locking_inset) {
2330 the_locking_inset->selectSelectedWord(bview);
2333 // move cursor to the beginning
2334 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2336 // set the sel cursor
2337 selection.cursor = cursor;
2340 // now find the end of the word
2341 while (cursor.pos() < cursor.par()->size()
2342 && (cursor.par()->isLetter(cursor.pos())
2343 // assignment is intentional here
2344 || ((inset = getInset())
2345 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2346 && static_cast<InsetSpecialChar *>(inset)->kind()
2347 == InsetSpecialChar::HYPHENATION
2349 cursor.pos(cursor.pos() + 1);
2351 setCursor(bview, cursor.par(), cursor.pos());
2353 // finally set the selection
2354 setSelection(bview);
2358 /* -------> Delete from cursor up to the end of the current or next word. */
2359 void LyXText::deleteWordForward(BufferView * bview)
2361 if (!cursor.par()->size())
2364 LyXCursor tmpcursor = cursor;
2365 tmpcursor.row(0); //Â ??
2366 selection.set(true); // to avoid deletion
2367 cursorRightOneWord(bview);
2368 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2369 selection.cursor = cursor;
2371 setSelection(bview);
2373 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2374 cutSelection(bview);
2379 /* -------> Delete from cursor to start of current or prior word. */
2380 void LyXText::deleteWordBackward(BufferView * bview)
2382 if (!cursor.par()->size())
2385 LyXCursor tmpcursor = cursor;
2386 tmpcursor.row(0); // ??
2387 selection.set(true); // to avoid deletion
2388 cursorLeftOneWord(bview);
2389 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2390 selection.cursor = cursor;
2392 setSelection(bview);
2393 cutSelection(bview);
2398 /* -------> Kill to end of line. */
2399 void LyXText::deleteLineForward(BufferView * bview)
2401 if (!cursor.par()->size())
2402 // Paragraph is empty, so we just go to the right
2405 LyXCursor tmpcursor = cursor;
2406 // We can't store the row over a regular setCursor
2407 // so we set it to 0 and reset it afterwards.
2408 tmpcursor.row(0); //Â ??
2409 selection.set(true); // to avoid deletion
2411 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2412 selection.cursor = cursor;
2414 setSelection(bview);
2415 // What is this test for ??? (JMarc)
2416 if (!selection.set()) {
2417 deleteWordForward(bview);
2419 cutSelection(bview);
2425 // Change the case of a word at cursor position.
2426 // This function directly manipulates Paragraph::text because there
2427 // is no Paragraph::SetChar currently. I did what I could to ensure
2428 // that it is correct. I guess part of it should be moved to
2429 // Paragraph, but it will have to change for 1.1 anyway. At least
2430 // it does not access outside of the allocated array as the older
2431 // version did. (JMarc)
2432 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2437 if (selection.set()) {
2438 from = selection.start;
2441 getWord(from, to, PARTIAL_WORD);
2442 setCursor(bview, to.par(), to.pos() + 1);
2445 changeRegionCase(bview, from, to, action);
2449 void LyXText::changeRegionCase(BufferView * bview,
2450 LyXCursor const & from,
2451 LyXCursor const & to,
2452 LyXText::TextCase action)
2454 lyx::Assert(from <= to);
2456 setUndo(bview, Undo::FINISH,
2457 from.par(), to.par()->next());
2459 Paragraph::size_type pos = from.pos();
2460 Paragraph * par = from.par();
2462 while (par && (pos != to.pos() || par != to.par())) {
2463 unsigned char c = par->getChar(pos);
2464 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2466 case text_lowercase:
2469 case text_capitalization:
2471 action = text_lowercase;
2473 case text_uppercase:
2478 par->setChar(pos, c);
2479 checkParagraph(bview, par, pos);
2482 if (pos == par->size()) {
2487 if (to.row() != from.row()) {
2488 refresh_y = from.y() - from.row()->baseline();
2489 refresh_row = from.row();
2490 status(bview, LyXText::NEED_MORE_REFRESH);
2495 void LyXText::transposeChars(BufferView & bview)
2497 Paragraph * tmppar = cursor.par();
2499 setUndo(&bview, Undo::FINISH,
2500 tmppar, tmppar->next());
2502 Paragraph::size_type tmppos = cursor.pos();
2504 // First decide if it is possible to transpose at all
2506 // We are at the beginning of a paragraph.
2507 if (tmppos == 0) return;
2509 // We are at the end of a paragraph.
2510 if (tmppos == tmppar->size() - 1) return;
2512 unsigned char c1 = tmppar->getChar(tmppos);
2513 unsigned char c2 = tmppar->getChar(tmppos - 1);
2515 if (c1 != Paragraph::META_INSET
2516 && c2 != Paragraph::META_INSET) {
2517 tmppar->setChar(tmppos, c2);
2518 tmppar->setChar(tmppos - 1, c1);
2520 // We should have an implementation that handles insets
2521 // as well, but that will have to come later. (Lgb)
2522 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2526 void LyXText::Delete(BufferView * bview)
2528 // this is a very easy implementation
2530 LyXCursor old_cursor = cursor;
2531 int const old_cur_par_id = old_cursor.par()->id();
2532 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2533 old_cursor.par()->previous()->id() : 0;
2535 // just move to the right
2538 // CHECK Look at the comment here.
2539 // This check is not very good...
2540 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2541 // and that can very well delete the par or par->previous in
2542 // old_cursor. Will a solution where we compare paragraph id's
2544 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2545 == old_cur_par_prev_id
2546 && cursor.par()->id() != old_cur_par_id)
2547 return; // delete-empty-paragraph-mechanism has done it
2549 // if you had success make a backspace
2550 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2551 LyXCursor tmpcursor = cursor;
2552 cursor = old_cursor; // to make sure undo gets the right cursor position
2553 setUndo(bview, Undo::DELETE,
2554 cursor.par(), cursor.par()->next());
2561 void LyXText::backspace(BufferView * bview)
2563 // Get the font that is used to calculate the baselineskip
2564 Paragraph::size_type lastpos = cursor.par()->size();
2565 LyXFont rawparfont =
2566 cursor.par()->getFontSettings(bview->buffer()->params,
2569 if (cursor.pos() == 0) {
2570 // The cursor is at the beginning of a paragraph,
2571 // so the the backspace will collapse two paragraphs into one.
2573 // we may paste some paragraphs
2575 // is it an empty paragraph?
2578 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2579 // This is an empty paragraph and we delete it just by moving the cursor one step
2580 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2581 // of the paragraph.
2583 if (cursor.par()->previous()) {
2584 Paragraph * tmppar = cursor.par()->previous();
2585 if (cursor.par()->getLayout() == tmppar->getLayout()
2586 && cursor.par()->getAlign() == tmppar->getAlign()) {
2587 // Inherit bottom DTD from the paragraph below.
2588 // (the one we are deleting)
2589 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2590 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2591 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2596 // the layout things can change the height of a row !
2597 int const tmpheight = cursor.row()->height();
2598 setHeightOfRow(bview, cursor.row());
2599 if (cursor.row()->height() != tmpheight) {
2600 refresh_y = cursor.y() - cursor.row()->baseline();
2601 refresh_row = cursor.row();
2602 status(bview, LyXText::NEED_MORE_REFRESH);
2608 if (cursor.par()->previous()) {
2609 setUndo(bview, Undo::DELETE,
2610 cursor.par()->previous(),
2611 cursor.par()->next());
2614 Paragraph * tmppar = cursor.par();
2615 Row * tmprow = cursor.row();
2617 // We used to do cursorLeftIntern() here, but it is
2618 // not a good idea since it triggers the auto-delete
2619 // mechanism. So we do a cursorLeftIntern()-lite,
2620 // without the dreaded mechanism. (JMarc)
2621 if (cursor.par()->previous()) {
2622 // steps into the above paragraph.
2623 setCursorIntern(bview, cursor.par()->previous(),
2624 cursor.par()->previous()->size(),
2628 /* Pasting is not allowed, if the paragraphs have different
2629 layout. I think it is a real bug of all other
2630 word processors to allow it. It confuses the user.
2631 Even so with a footnote paragraph and a non-footnote
2632 paragraph. I will not allow pasting in this case,
2633 because the user would be confused if the footnote behaves
2634 different wether it is open or closed.
2636 Correction: Pasting is always allowed with standard-layout
2638 if (cursor.par() != tmppar
2639 && (cursor.par()->getLayout() == tmppar->getLayout()
2640 || tmppar->getLayout() == 0 /*standard*/)
2641 && cursor.par()->getAlign() == tmppar->getAlign()) {
2643 removeParagraph(tmprow);
2645 cursor.par()->pasteParagraph(bview->buffer()->params);
2647 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2648 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2649 // strangely enough it seems that commenting out the line above removes
2650 // most or all of the segfaults. I will however also try to move the
2651 // two Remove... lines in front of the PasteParagraph too.
2654 cursor.pos(cursor.pos() - 1);
2656 status(bview, LyXText::NEED_MORE_REFRESH);
2657 refresh_row = cursor.row();
2658 refresh_y = cursor.y() - cursor.row()->baseline();
2660 // remove the lost paragraph
2661 // This one is not safe, since the paragraph that the tmprow and the
2662 // following rows belong to has been deleted by the PasteParagraph
2663 // above. The question is... could this be moved in front of the
2665 //RemoveParagraph(tmprow);
2666 //RemoveRow(tmprow);
2668 // This rebuilds the rows.
2669 appendParagraph(bview, cursor.row());
2670 updateCounters(bview, cursor.row());
2672 // the row may have changed, block, hfills etc.
2673 setCursor(bview, cursor.par(), cursor.pos(), false);
2676 /* this is the code for a normal backspace, not pasting
2678 setUndo(bview, Undo::DELETE,
2679 cursor.par(), cursor.par()->next());
2680 // We used to do cursorLeftIntern() here, but it is
2681 // not a good idea since it triggers the auto-delete
2682 // mechanism. So we do a cursorLeftIntern()-lite,
2683 // without the dreaded mechanism. (JMarc)
2684 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2685 false, cursor.boundary());
2687 // some insets are undeletable here
2688 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2689 if (!cursor.par()->getInset(cursor.pos())->deletable())
2691 // force complete redo when erasing display insets
2692 // this is a cruel method but safe..... Matthias
2693 if (cursor.par()->getInset(cursor.pos())->display() ||
2694 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2695 cursor.par()->erase(cursor.pos());
2696 redoParagraph(bview);
2701 Row * row = cursor.row();
2702 int y = cursor.y() - row->baseline();
2703 Paragraph::size_type z;
2704 /* remember that a space at the end of a row doesnt count
2705 * when calculating the fill */
2706 if (cursor.pos() < rowLast(row) ||
2707 !cursor.par()->isLineSeparator(cursor.pos())) {
2708 row->fill(row->fill() + singleWidth(bview,
2713 /* some special code when deleting a newline. This is similar
2714 * to the behavior when pasting paragraphs */
2715 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2716 cursor.par()->erase(cursor.pos());
2717 // refresh the positions
2719 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2720 tmprow = tmprow->next();
2721 tmprow->pos(tmprow->pos() - 1);
2723 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2724 cursor.pos(cursor.pos() - 1);
2726 if (cursor.pos() < cursor.par()->size()
2727 && !cursor.par()->isSeparator(cursor.pos())) {
2728 cursor.par()->insertChar(cursor.pos(), ' ');
2729 setCharFont(bview->buffer(), cursor.par(),
2730 cursor.pos(), current_font);
2731 // refresh the positions
2733 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2734 tmprow = tmprow->next();
2735 tmprow->pos(tmprow->pos() + 1);
2739 cursor.par()->erase(cursor.pos());
2741 // refresh the positions
2743 while (tmprow->next()
2744 && tmprow->next()->par() == row->par()) {
2745 tmprow = tmprow->next();
2746 tmprow->pos(tmprow->pos() - 1);
2749 // delete newlines at the beginning of paragraphs
2750 while (cursor.par()->size() &&
2751 cursor.par()->isNewline(cursor.pos()) &&
2752 cursor.pos() == beginningOfMainBody(bview->buffer(),
2754 cursor.par()->erase(cursor.pos());
2755 // refresh the positions
2757 while (tmprow->next() &&
2758 tmprow->next()->par() == row->par()) {
2759 tmprow = tmprow->next();
2760 tmprow->pos(tmprow->pos() - 1);
2765 // is there a break one row above
2766 if (row->previous() && row->previous()->par() == row->par()) {
2767 z = nextBreakPoint(bview, row->previous(),
2769 if (z >= row->pos()) {
2772 Row * tmprow = row->previous();
2774 // maybe the current row is now empty
2775 if (row->pos() >= row->par()->size()) {
2780 breakAgainOneRow(bview, row);
2781 if (row->next() && row->next()->par() == row->par())
2782 need_break_row = row->next();
2787 // set the dimensions of the row above
2788 y -= tmprow->height();
2789 tmprow->fill(fill(bview, tmprow,
2791 setHeightOfRow(bview, tmprow);
2794 refresh_row = tmprow;
2795 status(bview, LyXText::NEED_MORE_REFRESH);
2796 setCursor(bview, cursor.par(), cursor.pos(),
2797 false, cursor.boundary());
2798 //current_font = rawtmpfont;
2799 //real_current_font = realtmpfont;
2800 // check, whether the last character's font has changed.
2802 cursor.par()->getFontSettings(bview->buffer()->params,
2803 cursor.par()->size() - 1))
2804 redoHeightOfParagraph(bview, cursor);
2809 // break the cursor row again
2810 if (row->next() && row->next()->par() == row->par() &&
2811 (rowLast(row) == row->par()->size() - 1 ||
2812 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2814 /* it can happen that a paragraph loses one row
2815 * without a real breakup. This is when a word
2816 * is to long to be broken. Well, I don t care this
2818 if (rowLast(row) == row->par()->size() - 1)
2819 removeRow(row->next());
2823 status(bview, LyXText::NEED_MORE_REFRESH);
2825 breakAgainOneRow(bview, row);
2826 // will the cursor be in another row now?
2827 if (row->next() && row->next()->par() == row->par() &&
2828 rowLast(row) <= cursor.pos()) {
2830 breakAgainOneRow(bview, row);
2833 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2835 if (row->next() && row->next()->par() == row->par())
2836 need_break_row = row->next();
2840 // set the dimensions of the row
2841 row->fill(fill(bview, row, workWidth(bview)));
2842 int const tmpheight = row->height();
2843 setHeightOfRow(bview, row);
2844 if (tmpheight == row->height())
2845 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2847 status(bview, LyXText::NEED_MORE_REFRESH);
2850 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2854 // current_font = rawtmpfont;
2855 // real_current_font = realtmpfont;
2857 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2858 != cursor.boundary())
2859 setCursor(bview, cursor.par(), cursor.pos(), false,
2860 !cursor.boundary());
2862 lastpos = cursor.par()->size();
2863 if (cursor.pos() == lastpos)
2864 setCurrentFont(bview);
2866 // check, whether the last characters font has changed.
2868 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2869 redoHeightOfParagraph(bview, cursor);
2871 // now the special right address boxes
2872 if (textclasslist.Style(bview->buffer()->params.textclass,
2873 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2874 redoDrawingOfParagraph(bview, cursor);
2880 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2881 Row * row_ptr, int y, bool cleared)
2883 // returns a printed row
2884 Painter & pain = bview->painter();
2887 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2889 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2891 Paragraph::size_type vpos;
2892 Paragraph::size_type pos;
2896 LyXFont font(LyXFont::ALL_SANE);
2898 if (row_ptr->height() <= 0) {
2899 lyxerr << "LYX_ERROR: row.height: "
2900 << row_ptr->height() << endl;
2905 float fill_separator;
2907 float fill_label_hfill;
2908 prepareToPrint(bview, row_ptr, x, fill_separator,
2909 fill_hfill, fill_label_hfill);
2911 if (inset_owner && (x < 0))
2915 // clear the area where we want to paint/print
2916 int const ww = bview->workWidth();
2918 bool clear_area = true;
2921 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2922 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2923 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2924 clear_area = inset->doClearArea();
2926 // we don't need to clear it's already done!!!
2929 } else if (clear_area) {
2930 #ifdef WITH_WARNINGS
2931 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
2932 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
2934 int const y = y_offset < 0 ? 0 : y_offset;
2935 int const h = y_offset < 0 ?
2936 row_ptr->height() + y_offset : row_ptr->height();
2937 int const w = inset_owner ?
2938 inset_owner->width(bview, font) - 2 : ww;
2939 int const x = x_offset;
2940 pain.fillRectangle(x, y, w, h);
2941 } else if (inset != 0) {
2942 int h = row_ptr->baseline() - inset->ascent(bview, font);
2944 int const w = (inset_owner ?
2945 inset_owner->width(bview, font) : ww);
2946 pain.fillRectangle(x_offset, y_offset, w, h);
2948 h += inset->ascent(bview, font) + inset->descent(bview, font);
2949 if ((row_ptr->height() - h) > 0) {
2950 int const w = (inset_owner ?
2951 inset_owner->width(bview, font) : ww);
2952 pain.fillRectangle(x_offset, y_offset + h,
2953 w, row_ptr->height() - h);
2955 if (!inset_owner && !inset->display() && !inset->needFullRow())
2957 int const w = inset->width(bview, font) + int(x);
2958 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
2962 if (selection.set()) {
2963 int const w = (inset_owner ?
2964 inset_owner->width(bview, font) : ww);
2966 if (bidi_same_direction) {
2967 if (selection.start.row() == row_ptr &&
2968 selection.end.row() == row_ptr) {
2969 if (selection.start.x() < selection.end.x())
2970 pain.fillRectangle(x_offset + selection.start.x(),
2972 selection.end.x() - selection.start.x(),
2976 pain.fillRectangle(x_offset + selection.end.x(),
2978 selection.start.x() - selection.end.x(),
2981 } else if (selection.start.row() == row_ptr) {
2983 pain.fillRectangle(x_offset, y_offset,
2984 selection.start.x(),
2988 pain.fillRectangle(x_offset + selection.start.x(),
2990 w - selection.start.x(),
2993 } else if (selection.end.row() == row_ptr) {
2995 pain.fillRectangle(x_offset + selection.end.x(),
2997 w - selection.end.x(),
3001 pain.fillRectangle(x_offset, y_offset,
3005 } else if (y > selection.start.y()
3006 && y < selection.end.y()) {
3007 pain.fillRectangle(x_offset, y_offset, w,
3011 } else if (selection.start.row() != row_ptr &&
3012 selection.end.row() != row_ptr &&
3013 y > selection.start.y()
3014 && y < selection.end.y()) {
3015 pain.fillRectangle(x_offset, y_offset, w,
3018 } else if (selection.start.row() == row_ptr ||
3019 selection.end.row() == row_ptr) {
3021 if ((selection.start.row() != row_ptr && !is_rtl) ||
3022 (selection.end.row() != row_ptr && is_rtl))
3023 pain.fillRectangle(x_offset, y_offset,
3027 Paragraph::size_type main_body =
3028 beginningOfMainBody(bview->buffer(),
3031 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3032 pos = vis2log(vpos);
3033 float const old_tmpx = tmpx;
3034 if (main_body > 0 && pos == main_body-1) {
3035 tmpx += fill_label_hfill +
3036 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3037 row_ptr->par()->getLayout()).labelsep,
3038 getFont(bview->buffer(),row_ptr->par(), -2));
3039 if (row_ptr->par()->isLineSeparator(main_body-1))
3040 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3042 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3043 tmpx += singleWidth(bview, row_ptr->par(), pos);
3044 if (pos >= main_body)
3047 tmpx += fill_label_hfill;
3049 else if (row_ptr->par()->isSeparator(pos)) {
3050 tmpx += singleWidth(bview, row_ptr->par(), pos);
3051 if (pos >= main_body)
3052 tmpx += fill_separator;
3054 tmpx += singleWidth(bview, row_ptr->par(), pos);
3056 if ((selection.start.row() != row_ptr ||
3057 selection.start.pos() <= pos) &&
3058 (selection.end.row() != row_ptr ||
3059 pos < selection.end.pos()) )
3060 // Here we do not use x_offset as x_offset was
3062 pain.fillRectangle(int(old_tmpx),
3064 int(tmpx - old_tmpx + 1),
3069 if ((selection.start.row() != row_ptr && is_rtl) ||
3070 (selection.end.row() != row_ptr && !is_rtl) )
3071 pain.fillRectangle(x_offset + int(tmpx),
3081 // Draw appendix lines
3082 Paragraph * firstpar = row_ptr->par();
3084 if (firstpar->params().appendix()) {
3085 pain.line(1, y_offset,
3086 1, y_offset + row_ptr->height(),
3087 LColor::appendixline);
3088 pain.line(ww - 2, y_offset,
3089 ww - 2, y_offset + row_ptr->height(),
3090 LColor::appendixline);
3094 Paragraph::depth_type const depth = firstpar->getDepth();
3096 Paragraph::depth_type next_depth = 0;
3097 Paragraph::depth_type prev_depth = 0;
3098 if (row_ptr->next())
3099 next_depth = row_ptr->next()->par()->getDepth();
3100 if (row_ptr->previous())
3101 prev_depth = row_ptr->previous()->par()->getDepth();
3103 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3104 int const line_x = (LYX_PAPER_MARGIN / 5) *
3105 i + box_x + x_offset;
3106 pain.line(line_x, y_offset, line_x,
3107 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3111 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3114 pain.fillRectangle(line_x,
3115 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3116 LYX_PAPER_MARGIN / 5, 2,
3122 LyXLayout const & layout =
3123 textclasslist.Style(bview->buffer()->params.textclass,
3124 row_ptr->par()->getLayout());
3127 int y_bottom = row_ptr->height();
3129 // is it a first row?
3130 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3132 // start of appendix?
3133 if (row_ptr->par()->params().startOfAppendix()) {
3134 pain.line(1, y_offset,
3136 LColor::appendixline);
3139 // think about the margins
3140 if (!row_ptr->previous() && bv_owner)
3141 y_top += LYX_PAPER_MARGIN;
3143 // draw a top pagebreak
3144 if (row_ptr->par()->params().pagebreakTop()) {
3146 pb_font.setColor(LColor::pagebreak).decSize();
3150 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3152 y_offset + y_top + 2 * defaultHeight(),
3154 Painter::line_onoffdash)
3157 _("Page Break (top)"),
3160 LColor::background, false, w, a, d);
3161 pain.rectText((ww - w)/2,
3162 y_offset + y_top + 2 * defaultHeight() + d,
3163 _("Page Break (top)"),
3166 LColor::background);
3167 y_top += 3 * defaultHeight();
3170 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3172 pain.line(0, y_offset + 2 + y_top,
3173 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3176 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3178 y_offset + y_top + 3 * defaultHeight(),
3181 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3182 LYX_PAPER_MARGIN / 2,
3183 y_offset + y_top + 3 * defaultHeight(),
3186 y_top += 3 * defaultHeight();
3189 // think about user added space
3190 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3192 // think about the parskip
3193 // some parskips VERY EASY IMPLEMENTATION
3194 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3195 if (layout.latextype == LATEX_PARAGRAPH
3196 && firstpar->getDepth() == 0
3197 && firstpar->previous())
3198 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3199 else if (firstpar->previous()
3200 && textclasslist.Style(bview->buffer()->params.textclass,
3201 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3202 && firstpar->previous()->getDepth() == 0)
3203 // is it right to use defskip here, too? (AS)
3204 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3207 if (row_ptr->par()->params().lineTop()) {
3209 y_top += lyxfont::ascent('x',
3210 getFont(bview->buffer(),
3211 row_ptr->par(), 0));
3212 int const w = (inset_owner ?
3213 inset_owner->width(bview, font) : ww);
3214 int const xp = static_cast<int>(inset_owner ? x : 0);
3215 pain.line(xp, y_offset + y_top,
3216 w, y_offset + y_top,
3218 Painter::line_solid,
3219 Painter::line_thick);
3221 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3222 row_ptr->par(), 0));
3225 // should we print a label?
3226 if (layout.labeltype >= LABEL_STATIC
3227 && (layout.labeltype != LABEL_STATIC
3228 || layout.latextype != LATEX_ENVIRONMENT
3229 || row_ptr->par()->isFirstInSequence())) {
3230 font = getFont(bview->buffer(), row_ptr->par(), -2);
3231 if (!row_ptr->par()->getLabelstring().empty()) {
3233 string const tmpstring =
3234 row_ptr->par()->getLabelstring();
3236 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3237 if (bview->buffer()->params.secnumdepth >= 0) {
3238 // this is special code for
3239 // the chapter layout. This is
3240 // printed in an extra row
3241 // and has a pagebreak at
3243 float spacing_val = 1.0;
3244 if (!row_ptr->par()->params().spacing().isDefault()) {
3245 spacing_val = row_ptr->par()->params().spacing().getValue();
3247 spacing_val = bview->buffer()->params.spacing.getValue();
3250 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3251 + int(layout.parsep) * defaultHeight();
3253 tmpx = ww - leftMargin(bview, row_ptr) -
3254 lyxfont::width(tmpstring, font);
3255 pain.text(int(tmpx),
3256 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3261 tmpx = ww - leftMargin(bview, row_ptr)
3262 + lyxfont::width(layout.labelsep, font);
3264 tmpx = x - lyxfont::width(layout.labelsep, font)
3265 - lyxfont::width(tmpstring, font);
3268 pain.text(int(tmpx),
3269 y_offset + row_ptr->baseline(),
3273 // the labels at the top of an environment.
3274 // More or less for bibliography
3275 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3276 layout.labeltype == LABEL_BIBLIO ||
3277 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3278 if (row_ptr->par()->isFirstInSequence()) {
3279 font = getFont(bview->buffer(),
3280 row_ptr->par(), -2);
3281 if (!row_ptr->par()->getLabelstring().empty()) {
3282 string const tmpstring =
3283 row_ptr->par()->getLabelstring();
3284 float spacing_val = 1.0;
3285 if (!row_ptr->par()->params().spacing().isDefault()) {
3286 spacing_val = row_ptr->par()->params().spacing().getValue();
3288 spacing_val = bview->buffer()->params.spacing.getValue();
3291 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3292 + (layout.labelbottomsep * defaultHeight()));
3295 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3296 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3297 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3298 tmpx -= lyxfont::width(tmpstring, font) / 2;
3300 tmpx = ww - leftMargin(bview, row_ptr) -
3301 lyxfont::width(tmpstring, font);
3302 pain.text(int(tmpx),
3303 y_offset + row_ptr->baseline()
3304 - row_ptr->ascent_of_text()
3310 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3311 font = getFont(bview->buffer(), row_ptr->par(), -1);
3313 tmpx = ww - leftMargin(bview, row_ptr)
3314 + lyxfont::width(layout.labelsep, font);
3316 tmpx = x - lyxfont::width(layout.labelsep, font)
3317 - row_ptr->par()->bibkey->width(bview, font);
3318 row_ptr->par()->bibkey->draw(bview, font,
3319 y_offset + row_ptr->baseline(),
3324 // is it a last row?
3325 Paragraph * par = row_ptr->par();
3326 if (row_ptr->par() == par
3327 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3328 // think about the margins
3329 if (!row_ptr->next() && bv_owner)
3330 y_bottom -= LYX_PAPER_MARGIN;
3332 // draw a bottom pagebreak
3333 if (firstpar->params().pagebreakBottom()) {
3335 pb_font.setColor(LColor::pagebreak).decSize();
3336 int const y_place = y_offset + y_bottom
3337 - 2 * defaultHeight();
3343 .line(0, y_place, ww, y_place,
3345 Painter::line_onoffdash)
3347 _("Page Break (bottom)"),
3350 LColor::background, false, w, a, d);
3351 pain.rectText((ww - w) / 2, y_place + d,
3352 _("Page Break (bottom)"),
3355 LColor::background);
3356 y_bottom -= 3 * defaultHeight();
3359 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3360 // draw a vfill bottom
3361 int const y_place = y_offset + y_bottom
3362 - 3 * defaultHeight();
3364 pain.line(0, y_place,
3365 LYX_PAPER_MARGIN, y_place,
3367 pain.line(0, y_offset + y_bottom - 2,
3369 y_offset + y_bottom - 2,
3371 pain.line(LYX_PAPER_MARGIN / 2,
3373 LYX_PAPER_MARGIN / 2,
3374 y_offset + y_bottom - 2,
3376 y_bottom -= 3 * defaultHeight();
3379 // think about user added space
3380 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3382 if (firstpar->params().lineBottom()) {
3383 // draw a bottom line
3384 y_bottom -= lyxfont::ascent('x',
3385 getFont(bview->buffer(),
3388 int const w = (inset_owner ?
3389 inset_owner->width(bview, font) : ww);
3390 int const xp = static_cast<int>(inset_owner ? x : 0);
3391 pain.line(xp, y_offset + y_bottom,
3392 w, y_offset + y_bottom,
3393 LColor::topline, Painter::line_solid,
3394 Painter::line_thick);
3395 y_bottom -= lyxfont::ascent('x',
3396 getFont(bview->buffer(),
3402 int const endlabel =
3403 row_ptr->par()->getEndLabel(bview->buffer()->params);
3406 case END_LABEL_FILLED_BOX:
3408 LyXFont const font = getFont(bview->buffer(),
3409 row_ptr->par(), last);
3410 int const size = int(0.75 * lyxfont::maxAscent(font));
3411 int const y = (y_offset + row_ptr->baseline()) - size;
3412 int x = is_rtl ? LYX_PAPER_MARGIN
3413 : ww - LYX_PAPER_MARGIN - size;
3415 if (row_ptr->fill() <= size)
3416 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3417 if (endlabel == END_LABEL_BOX) {
3418 pain.line(x, y, x, y + size,
3420 pain.line(x + size, y, x + size , y + size,
3422 pain.line(x, y, x + size, y,
3424 pain.line(x, y + size, x + size, y + size,
3427 pain.fillRectangle(x, y, size, size,
3431 case END_LABEL_STATIC:
3433 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3434 string const tmpstring = textclasslist.
3435 Style(bview->buffer()->params.textclass,
3436 layout).endlabelstring();
3437 font = getFont(bview->buffer(), row_ptr->par(), -2);
3438 int const tmpx = is_rtl ?
3439 int(x) - lyxfont::width(tmpstring, font)
3440 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3441 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3444 case END_LABEL_NO_LABEL:
3449 // draw the text in the pixmap
3451 vpos = row_ptr->pos();
3453 Paragraph::size_type main_body =
3454 beginningOfMainBody(bview->buffer(), row_ptr->par());
3455 if (main_body > 0 &&
3456 (main_body-1 > last ||
3457 !row_ptr->par()->isLineSeparator(main_body - 1)))
3460 while (vpos <= last) {
3461 pos = vis2log(vpos);
3462 if (main_body > 0 && pos == main_body - 1) {
3463 x += fill_label_hfill
3464 + lyxfont::width(layout.labelsep,
3465 getFont(bview->buffer(),
3466 row_ptr->par(), -2))
3467 - singleWidth(bview,
3472 if (row_ptr->par() ->isHfill(pos)) {
3475 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3477 y_offset + row_ptr->baseline(),
3480 if (hfillExpansion(bview->buffer(),
3482 if (pos >= main_body) {
3484 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3485 int(x + fill_hfill),
3486 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3488 Painter::line_onoffdash);
3492 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3493 int(x + fill_label_hfill),
3494 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3496 Painter::line_onoffdash);
3498 x += fill_label_hfill;
3501 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3503 y_offset + row_ptr->baseline(),
3508 } else if (row_ptr->par()->isSeparator(pos)) {
3509 x += singleWidth(bview,
3510 row_ptr->par(), pos);
3511 if (pos >= main_body)
3512 x += fill_separator;
3515 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3520 int LyXText::defaultHeight() const
3522 LyXFont font(LyXFont::ALL_SANE);
3523 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3527 /* returns the column near the specified x-coordinate of the row
3528 * x is set to the real beginning of this column */
3529 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3530 bool & boundary) const
3533 float fill_separator;
3535 float fill_label_hfill;
3537 prepareToPrint(bview, row, tmpx, fill_separator,
3538 fill_hfill, fill_label_hfill);
3540 Paragraph::size_type vc = row->pos();
3541 Paragraph::size_type last = rowLastPrintable(row);
3542 Paragraph::size_type c = 0;
3543 LyXLayout const & layout =
3544 textclasslist.Style(bview->buffer()->params.textclass,
3545 row->par()->getLayout());
3546 bool left_side = false;
3548 Paragraph::size_type
3549 main_body = beginningOfMainBody(bview->buffer(), row->par());
3550 float last_tmpx = tmpx;
3552 if (main_body > 0 &&
3553 (main_body - 1 > last ||
3554 !row->par()->isLineSeparator(main_body - 1)))
3557 while (vc <= last && tmpx <= x) {
3560 if (main_body > 0 && c == main_body-1) {
3561 tmpx += fill_label_hfill +
3562 lyxfont::width(layout.labelsep,
3563 getFont(bview->buffer(), row->par(), -2));
3564 if (row->par()->isLineSeparator(main_body - 1))
3565 tmpx -= singleWidth(bview, row->par(), main_body-1);
3568 if (hfillExpansion(bview->buffer(), row, c)) {
3569 x += singleWidth(bview, row->par(), c);
3573 tmpx += fill_label_hfill;
3575 else if (row->par()->isSeparator(c)) {
3576 tmpx += singleWidth(bview, row->par(), c);
3578 tmpx+= fill_separator;
3580 tmpx += singleWidth(bview, row->par(), c);
3584 if ((tmpx + last_tmpx) / 2 > x) {
3589 if (vc > last + 1) // This shouldn't happen.
3593 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3594 // some speedup if rtl_support=false
3595 && (!row->next() || row->next()->par() != row->par());
3596 bool const rtl = (lastrow)
3597 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3598 : false; // If lastrow is false, we don't need to compute
3599 // the value of rtl.
3601 if (row->pos() > last) // Row is empty?
3604 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3605 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3607 else if (vc == row->pos()) {
3609 if (bidi_level(c) % 2 == 1)
3612 c = vis2log(vc - 1);
3613 bool const rtl = (bidi_level(c) % 2 == 1);
3614 if (left_side == rtl) {
3616 boundary = isBoundary(bview->buffer(), row->par(), c);
3620 if (row->pos() <= last && c > last
3621 && row->par()->isNewline(last)) {
3622 if (bidi_level(last) % 2 == 0)
3623 tmpx -= singleWidth(bview, row->par(), last);
3625 tmpx += singleWidth(bview, row->par(), last);
3635 // returns pointer to a specified row
3636 Row * LyXText::getRow(Paragraph * par,
3637 Paragraph::size_type pos, int & y) const
3642 Row * tmprow = firstrow;
3645 // find the first row of the specified paragraph
3646 while (tmprow->next() && tmprow->par() != par) {
3647 y += tmprow->height();
3648 tmprow = tmprow->next();
3651 // now find the wanted row
3652 while (tmprow->pos() < pos
3654 && tmprow->next()->par() == par
3655 && tmprow->next()->pos() <= pos) {
3656 y += tmprow->height();
3657 tmprow = tmprow->next();