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"
49 int const LYX_PAPER_MARGIN = 20;
53 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
56 int LyXText::workWidth(BufferView * bview) const
59 return inset_owner->textWidth(bview);
61 return bview->workWidth();
65 int LyXText::getRealCursorX(BufferView * bview) const
68 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
69 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
74 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
75 Paragraph::size_type pos) const
77 if (!Encodings::is_arabic(c))
78 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
79 return c + (0xb0 - '0');
83 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
84 unsigned char next_char = ' ';
86 for (Paragraph::size_type i = pos+1; i < par->size(); ++i)
87 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
88 next_char = par->getChar(i);
92 if (Encodings::is_arabic(next_char)) {
93 if (Encodings::is_arabic(prev_char))
94 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
96 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
98 if (Encodings::is_arabic(prev_char))
99 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
101 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
105 // This is the comments that some of the warnings below refers to.
106 // There are some issues in this file and I don't think they are
107 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
108 // this is a problem that has been here almost from day one and that a
109 // larger userbase with differenct access patters triggers the bad
110 // behaviour. (segfaults.) What I think happen is: In several places
111 // we store the paragraph in the current cursor and then moves the
112 // cursor. This movement of the cursor will delete paragraph at the
113 // old position if it is now empty. This will make the temporary
114 // pointer to the old cursor paragraph invalid and dangerous to use.
115 // And is some cases this will trigger a segfault. I have marked some
116 // of the cases where this happens with a warning, but I am sure there
117 // are others in this file and in text2.C. There is also a note in
118 // Delete() that you should read. In Delete I store the paragraph->id
119 // instead of a pointer to the paragraph. I am pretty sure this faulty
120 // use of temporary pointers to paragraphs that might have gotten
121 // invalidated (through a cursor movement) before they are used, are
122 // the cause of the strange crashes we get reported often.
124 // It is very tiresom to change this code, especially when it is as
125 // hard to read as it is. Help to fix all the cases where this is done
126 // would be greately appreciated.
130 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
131 Paragraph::size_type pos) const
133 char const c = par->getChar(pos);
134 return singleWidth(bview, par, pos, c);
138 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
139 Paragraph::size_type pos, char c) const
141 LyXFont const font = getFont(bview->buffer(), par, pos);
143 // The most common case is handled first (Asger)
144 if (IsPrintable(c)) {
145 if (font.language()->RightToLeft()) {
146 if (font.language()->lang() == "arabic" &&
147 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
148 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
149 if (Encodings::IsComposeChar_arabic(c))
152 c = transformChar(c, par, pos);
153 else if (font.language()->lang() == "hebrew" &&
154 Encodings::IsComposeChar_hebrew(c))
157 return lyxfont::width(c, font);
159 } else if (IsHfillChar(c)) {
160 return 3; /* Because of the representation
161 * as vertical lines */
162 } else if (c == Paragraph::META_INSET) {
163 Inset * tmpinset = par->getInset(pos);
165 #if 0 // seems not to be needed, but ...
166 tmpinset->update(bview, font);
168 return tmpinset->width(bview, font);
172 } else if (IsSeparatorChar(c))
174 else if (IsNewlineChar(c))
176 return lyxfont::width(c, font);
180 // Returns the paragraph position of the last character in the specified row
181 Paragraph::size_type LyXText::rowLast(Row const * row) const
183 if (row->next() == 0)
184 return row->par()->size() - 1;
185 else if (row->next()->par() != row->par())
186 return row->par()->size() - 1;
188 return row->next()->pos() - 1;
192 Paragraph::size_type LyXText::rowLastPrintable(Row const * row) const
194 Paragraph::size_type const last = rowLast(row);
195 if (last >= row->pos()
197 && row->next()->par() == row->par()
198 && row->par()->isSeparator(last))
205 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
207 bidi_same_direction = true;
208 if (!lyxrc.rtl_support) {
213 bidi_start = row->pos();
214 bidi_end = rowLastPrintable(row);
216 if (bidi_start > bidi_end) {
221 if (bidi_end + 2 - bidi_start >
222 static_cast<Paragraph::size_type>(log2vis_list.size())) {
223 Paragraph::size_type new_size =
224 (bidi_end + 2 - bidi_start < 500) ?
225 500 : 2 * (bidi_end + 2 - bidi_start);
226 log2vis_list.resize(new_size);
227 vis2log_list.resize(new_size);
228 bidi_levels.resize(new_size);
231 vis2log_list[bidi_end + 1 - bidi_start] = -1;
232 log2vis_list[bidi_end + 1 - bidi_start] = -1;
234 Paragraph::size_type stack[2];
236 row->par()->getParLanguage(buf->params)->RightToLeft();
240 Paragraph::size_type const main_body =
241 beginningOfMainBody(buf, row->par());
243 for (Paragraph::size_type lpos = bidi_start;
244 lpos <= bidi_end; ++lpos) {
245 bool is_space = row->par()->isLineSeparator(lpos);
246 Paragraph::size_type const pos =
247 (is_space && lpos + 1 <= bidi_end &&
248 !row->par()->isLineSeparator(lpos + 1) &&
249 !row->par()->isNewline(lpos + 1))
251 LyXFont font = row->par()->getFontSettings(buf->params, pos);
252 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
253 font.number() == LyXFont::ON &&
254 row->par()->getFontSettings(buf->params, lpos-1).number()
256 font = row->par()->getFontSettings(buf->params, lpos);
261 bool new_rtl = font.isVisibleRightToLeft();
262 bool new_rtl0 = font.isRightToLeft();
265 if (lpos == main_body - 1
266 && row->pos() < main_body - 1
268 new_level = (rtl_par) ? 1 : 0;
269 new_rtl = new_rtl0 = rtl_par;
271 new_level = (new_rtl) ? 1 : 2;
273 new_level = (rtl_par) ? 2 : 0;
275 if (is_space && new_level >= level) {
281 int new_level2 = new_level;
283 if (level == new_level && rtl0 != new_rtl0) {
285 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
286 } else if (level < new_level) {
287 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
288 if (new_level > rtl_par)
289 bidi_same_direction = false;
291 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
294 bidi_levels[lpos - bidi_start] = new_level;
296 while (level > new_level2) {
297 Paragraph::size_type old_lpos =
299 int delta = lpos - old_lpos - 1;
302 log2vis_list[lpos - bidi_start] += delta;
303 log2vis_list[old_lpos - bidi_start] += delta;
305 while (level < new_level)
306 stack[level++] = lpos;
310 Paragraph::size_type const old_lpos = stack[--level];
311 int delta = bidi_end - old_lpos;
314 log2vis_list[old_lpos - bidi_start] += delta;
317 Paragraph::size_type vpos = bidi_start - 1;
318 for (Paragraph::size_type lpos = bidi_start;
319 lpos <= bidi_end; ++lpos) {
320 vpos += log2vis_list[lpos - bidi_start];
321 vis2log_list[vpos - bidi_start] = lpos;
322 log2vis_list[lpos - bidi_start] = vpos;
327 // This method requires a previous call to ComputeBidiTables()
328 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
329 Paragraph::size_type pos) const
331 if (!lyxrc.rtl_support || pos == 0)
334 if (!bidi_InRange(pos - 1)) {
335 /// This can happen if pos is the first char of a row.
336 /// Returning false in this case is incorrect!
340 bool const rtl = bidi_level(pos - 1) % 2;
341 bool const rtl2 = bidi_InRange(pos)
342 ? bidi_level(pos) % 2
343 : par->isRightToLeftPar(buf->params);
348 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
349 Paragraph::size_type pos,
350 LyXFont const & font) const
352 if (!lyxrc.rtl_support)
353 return false; // This is just for speedup
355 bool const rtl = font.isVisibleRightToLeft();
356 bool const rtl2 = bidi_InRange(pos)
357 ? bidi_level(pos) % 2
358 : par->isRightToLeftPar(buf->params);
363 void LyXText::draw(BufferView * bview, Row const * row,
364 Paragraph::size_type & vpos,
365 int offset, float & x, bool cleared)
367 Painter & pain = bview->painter();
369 Paragraph::size_type pos = vis2log(vpos);
370 char c = row->par()->getChar(pos);
373 if (IsNewlineChar(c)) {
375 // Draw end-of-line marker
376 LyXFont const font = getFont(bview->buffer(), row->par(), pos);
377 int const wid = lyxfont::width('n', font);
378 int const asc = lyxfont::maxAscent(font);
379 int const y = offset + row->baseline();
383 if (bidi_level(pos) % 2 == 0) {
384 xp[0] = int(x + wid * 0.375);
385 yp[0] = int(y - 0.875 * asc * 0.75);
388 yp[1] = int(y - 0.500 * asc * 0.75);
390 xp[2] = int(x + wid * 0.375);
391 yp[2] = int(y - 0.125 * asc * 0.75);
393 pain.lines(xp, yp, 3, LColor::eolmarker);
396 yp[0] = int(y - 0.500 * asc * 0.75);
398 xp[1] = int(x + wid);
399 yp[1] = int(y - 0.500 * asc * 0.75);
401 xp[2] = int(x + wid);
402 yp[2] = int(y - asc * 0.75);
404 pain.lines(xp, yp, 3, LColor::eolmarker);
406 xp[0] = int(x + wid * 0.625);
407 yp[0] = int(y - 0.875 * asc * 0.75);
409 xp[1] = int(x + wid);
410 yp[1] = int(y - 0.500 * asc * 0.75);
412 xp[2] = int(x + wid * 0.625);
413 yp[2] = int(y - 0.125 * asc * 0.75);
415 pain.lines(xp, yp, 3, LColor::eolmarker);
417 xp[0] = int(x + wid);
418 yp[0] = int(y - 0.500 * asc * 0.75);
421 yp[1] = int(y - 0.500 * asc * 0.75);
424 yp[2] = int(y - asc * 0.75);
426 pain.lines(xp, yp, 3, LColor::eolmarker);
432 LyXFont font = getFont(bview->buffer(), row->par(), pos);
433 LyXFont font2 = font;
435 if (c == Paragraph::META_INSET) {
436 Inset * tmpinset = row->par()->getInset(pos);
438 tmpinset->update(bview, font, false);
439 tmpinset->draw(bview, font, offset+row->baseline(), x,
441 #ifdef SEEMS_TO_BE_NOT_NEEDED
442 if (status_ == CHANGED_IN_DRAW) {
443 UpdateInset(bview, tmpinset);
444 status(bview, CHANGED_IN_DRAW);
450 if (lyxrc.mark_foreign_language &&
451 font.language() != bview->buffer()->params.language) {
452 int const y = offset + row->height() - 1;
453 pain.line(int(tmpx), y, int(x), y,
460 /* usual characters, no insets */
462 // Collect character that we can draw in one command
464 // This is dirty, but fast. Notice that it will never be too small.
465 // For the record, I'll note that Microsoft Word has a limit
466 // of 768 here. We have none :-) (Asger)
467 // Ok. I am the first to admit that the use of std::string will be
468 // a tiny bit slower than using a POD char array. However, I claim
469 // that this slowdown is so small that it is close to inperceptive.
470 // So IMHO we should go with the easier and clearer implementation.
471 // And even if 1024 is a large number here it might overflow, string
472 // will only overflow if the machine is out of memory...
473 static string textstring;
477 Paragraph::size_type const last = rowLastPrintable(row);
479 if (font.language()->lang() == "hebrew") {
480 if (Encodings::IsComposeChar_hebrew(c)) {
481 int const width = lyxfont::width(c, font2);
483 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
484 c = row->par()->getChar(i);
485 if (!Encodings::IsComposeChar_hebrew(c)) {
486 if (IsPrintableNonspace(c)) {
491 dx = (c == 'ø' || c == 'ã') // dalet / resh
492 ? width2 - width : (width2 - width) / 2;
498 pain.text(int(x) + dx, offset + row->baseline(),
501 while (vpos <= last &&
502 (pos = vis2log(vpos)) >= 0
503 && IsPrintableNonspace(c = row->par()->getChar(pos))
504 && !Encodings::IsComposeChar_hebrew(c)
505 && font2 == getFont(bview->buffer(), row->par(), pos)) {
509 // Draw text and set the new x position
510 pain.text(int(x), offset + row->baseline(),
512 x += lyxfont::width(textstring, font);
514 } else if (font.language()->lang() == "arabic" &&
515 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
516 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
517 if (Encodings::IsComposeChar_arabic(c)) {
518 c = transformChar(c, row->par(), pos);
520 int const width = lyxfont::width(c, font2);
522 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
523 c = row->par()->getChar(i);
524 if (!Encodings::IsComposeChar_arabic(c)) {
525 if (IsPrintableNonspace(c)) {
530 dx = (width2 - width) / 2;
536 pain.text(int(x) + dx, offset + row->baseline(),
539 textstring = transformChar(c, row->par(), pos);
540 while (vpos <= last &&
541 (pos = vis2log(vpos)) >= 0
542 && IsPrintableNonspace(c = row->par()->getChar(pos))
543 && !Encodings::IsComposeChar_arabic(c)
544 && font2 == getFont(bview->buffer(), row->par(), pos)) {
545 c = transformChar(c, row->par(), pos);
549 // Draw text and set the new x position
550 pain.text(int(x), offset + row->baseline(),
552 x += lyxfont::width(textstring, font);
555 while (vpos <= last &&
556 (pos = vis2log(vpos)) >= 0
557 && IsPrintableNonspace(c = row->par()->getChar(pos))
558 && font2 == getFont(bview->buffer(), row->par(), pos)) {
562 // Draw text and set the new x position
563 pain.text(int(x), offset + row->baseline(), textstring, font);
564 x += lyxfont::width(textstring, font);
567 if (lyxrc.mark_foreign_language &&
568 font.language() != bview->buffer()->params.language) {
569 int const y = offset + row->height() - 1;
570 pain.line(int(tmpx), y, int(x), y,
574 // If we want ulem.sty support, drawing
575 // routines should go here. (Asger)
576 // Why shouldn't LyXFont::drawText handle it internally?
580 // Returns the left beginning of the text.
581 // This information cannot be taken from the layouts-objekt, because in
582 // LaTeX the beginning of the text fits in some cases (for example sections)
583 // exactly the label-width.
584 int LyXText::leftMargin(BufferView * bview, Row const * row) const
586 LyXLayout const & layout =
587 textclasslist.Style(bview->buffer()->params.textclass,
588 row->par()->getLayout());
590 string parindent = layout.parindent;
592 int x = LYX_PAPER_MARGIN;
594 x += lyxfont::signedWidth(textclasslist
595 .TextClass(bview->buffer()->params.textclass)
598 .TextClass(bview->buffer()->params.textclass)
601 // this is the way, LyX handles the LaTeX-Environments.
602 // I have had this idea very late, so it seems to be a
603 // later added hack and this is true
604 if (!row->par()->getDepth()) {
605 if (!row->par()->getLayout()) {
606 // find the previous same level paragraph
607 if (row->par()->previous()) {
608 Paragraph * newpar = row->par()
609 ->depthHook(row->par()->getDepth());
611 textclasslist.Style(bview->buffer()->params.textclass,
618 // find the next level paragraph
621 row->par()->outerHook();
623 // make a corresponding row. Needed to call LeftMargin()
625 // check wether it is a sufficent paragraph
628 .Style(bview->buffer()->params.textclass,
629 newpar->getLayout()).isEnvironment()) {
631 dummyrow.par(newpar);
632 dummyrow.pos(newpar->size());
633 x = leftMargin(bview, &dummyrow);
635 // this is no longer an error, because this function
636 // is used to clear impossible depths after changing
637 // a layout. Since there is always a redo,
638 // LeftMargin() is always called
639 row->par()->params().depth(0);
642 if (newpar && !row->par()->getLayout()) {
643 if (newpar->params().noindent())
646 parindent = textclasslist
647 .Style(bview->buffer()->params.textclass,
648 newpar->getLayout()).parindent;
653 LyXFont const labelfont = getFont(bview->buffer(), row->par(), -2);
654 switch (layout.margintype) {
656 if (!layout.leftmargin.empty()) {
657 x += lyxfont::signedWidth(layout.leftmargin,
659 .TextClass(bview->buffer()->params.
663 if (!row->par()->getLabelstring().empty()) {
664 x += lyxfont::signedWidth(layout.labelindent,
666 x += lyxfont::width(row->par()->getLabelstring(),
668 x += lyxfont::width(layout.labelsep, labelfont);
672 x += lyxfont::signedWidth(layout.labelindent, labelfont);
673 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
674 if (!row->par()->getLabelWidthString().empty()) {
675 x += lyxfont::width(row->par()->getLabelWidthString(),
677 x += lyxfont::width(layout.labelsep, labelfont);
682 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
683 / (row->par()->getDepth() + 4);
685 case MARGIN_FIRST_DYNAMIC:
686 if (layout.labeltype == LABEL_MANUAL) {
687 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
688 x += lyxfont::signedWidth(layout.leftmargin,
691 x += lyxfont::signedWidth(layout.labelindent,
694 } else if (row->pos()
695 // Special case to fix problems with
697 || (layout.labeltype == LABEL_STATIC
698 && layout.latextype == LATEX_ENVIRONMENT
699 && ! row->par()->isFirstInSequence())) {
700 x += lyxfont::signedWidth(layout.leftmargin,
702 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
703 && layout.labeltype != LABEL_BIBLIO
704 && layout.labeltype !=
705 LABEL_CENTERED_TOP_ENVIRONMENT) {
706 x += lyxfont::signedWidth(layout.labelindent,
708 x += lyxfont::width(layout.labelsep, labelfont);
709 x += lyxfont::width(row->par()->getLabelstring(),
714 case MARGIN_RIGHT_ADDRESS_BOX:
716 // ok, a terrible hack. The left margin depends on the widest
717 // row in this paragraph. Do not care about footnotes, they
718 // are *NOT* allowed in the LaTeX realisation of this layout.
720 // find the first row of this paragraph
721 Row const * tmprow = row;
722 while (tmprow->previous()
723 && tmprow->previous()->par() == row->par())
724 tmprow = tmprow->previous();
726 int minfill = tmprow->fill();
727 while (tmprow->next() && tmprow->next()->par() == row->par()) {
728 tmprow = tmprow->next();
729 if (tmprow->fill() < minfill)
730 minfill = tmprow->fill();
733 x += lyxfont::signedWidth(layout.leftmargin,
735 .TextClass(bview->buffer()->params.textclass)
742 int align; // wrong type
744 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
745 align = layout.align;
747 align = row->par()->params().align();
749 // set the correct parindent
750 if (row->pos() == 0) {
751 if ((layout.labeltype == LABEL_NO_LABEL
752 || layout.labeltype == LABEL_TOP_ENVIRONMENT
753 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
754 || (layout.labeltype == LABEL_STATIC
755 && layout.latextype == LATEX_ENVIRONMENT
756 && ! row->par()->isFirstInSequence()))
757 && align == LYX_ALIGN_BLOCK
758 && !row->par()->params().noindent()
759 && (row->par()->layout ||
760 bview->buffer()->params.paragraph_separation ==
761 BufferParams::PARSEP_INDENT))
762 x += lyxfont::signedWidth(parindent,
764 .TextClass(bview->buffer()->params
767 else if (layout.labeltype == LABEL_BIBLIO) {
768 // ale970405 Right width for bibitems
769 x += bibitemMaxWidth(bview,textclasslist
770 .TextClass(bview->buffer()->params
779 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
781 LyXLayout const & layout =
782 textclasslist.Style(buf->params.textclass,
783 row->par()->getLayout());
785 int x = LYX_PAPER_MARGIN
786 + lyxfont::signedWidth(textclasslist
787 .TextClass(buf->params.textclass)
790 .TextClass(buf->params.textclass)
793 // this is the way, LyX handles the LaTeX-Environments.
794 // I have had this idea very late, so it seems to be a
795 // later added hack and this is true
796 if (row->par()->getDepth()) {
797 // find the next level paragraph
799 Paragraph * newpar = row->par();
802 newpar = newpar->previous();
804 && newpar->getDepth() >= row->par()->getDepth());
806 // make a corresponding row. Needed to call LeftMargin()
808 // check wether it is a sufficent paragraph
810 && textclasslist.Style(buf->params.textclass,
814 dummyrow.par(newpar);
816 x = rightMargin(buf, &dummyrow);
818 // this is no longer an error, because this function
819 // is used to clear impossible depths after changing
820 // a layout. Since there is always a redo,
821 // LeftMargin() is always called
822 row->par()->params().depth(0);
826 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
827 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
828 .TextClass(buf->params.textclass)
829 .defaultfont()) * 4 / (row->par()->getDepth()
835 int LyXText::labelEnd(BufferView * bview, Row const * row) const
837 if (textclasslist.Style(bview->buffer()->params.textclass,
838 row->par()->getLayout()).margintype
842 tmprow.pos(row->par()->size());
843 return leftMargin(bview, &tmprow); /* just the beginning
846 return 0; /* LabelEnd is only needed, if the
847 layout fills a flushleft
852 // get the next breakpoint in a given paragraph
854 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
856 Paragraph * par = row->par();
861 Paragraph::size_type const pos = row->pos();
864 // position of the last possible breakpoint
865 // -1 isn't a suitable value, but a flag
866 Paragraph::size_type last_separator = -1;
867 width -= rightMargin(bview->buffer(), row);
869 Paragraph::size_type const main_body =
870 beginningOfMainBody(bview->buffer(), par);
871 LyXLayout const & layout =
872 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
873 Paragraph::size_type i = pos;
875 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
876 /* special code for right address boxes, only newlines count */
877 while (i < par->size()) {
878 if (par->isNewline(i)) {
880 i = par->size() - 1; // this means break
882 } else if (par->getChar(i) == Paragraph::META_INSET &&
883 par->getInset(i) && par->getInset(i)->display()){
884 par->getInset(i)->display(false);
889 // Last position is an invariant
890 Paragraph::size_type const last =
892 // this is the usual handling
893 int x = leftMargin(bview, row);
894 bool doitonetime = true;
895 while (doitonetime || ((x < width) && (i < last))) {
897 char const c = par->getChar(i);
898 if (IsNewlineChar(c)) {
900 x = width; // this means break
901 } else if (c == Paragraph::META_INSET &&
904 // check wether a Display() inset is
905 // valid here. if not, change it to
907 if (par->getInset(i)->display() &&
908 (layout.isCommand() ||
909 (layout.labeltype == LABEL_MANUAL
910 && i < beginningOfMainBody(bview->buffer(), par)))) {
911 // display istn't allowd
912 par->getInset(i)->display(false);
913 x += singleWidth(bview, par, i, c);
914 } else if (par->getInset(i)->display() ||
915 par->getInset(i)->needFullRow()) {
916 // So break the line here
920 if (IsLineSeparatorChar(par->getChar(i+1)))
923 last_separator = last; // to avoid extra rows
925 last_separator = i - 1;
926 x = width; // this means break
928 x += singleWidth(bview, par, i, c);
931 if (IsLineSeparatorChar(c))
933 x += singleWidth(bview, par, i, c);
936 if (i == main_body) {
937 x += lyxfont::width(layout.labelsep,
938 getFont(bview->buffer(), par, -2));
939 if (par->isLineSeparator(i - 1))
940 x-= singleWidth(bview, par, i - 1);
941 int left_margin = labelEnd(bview, row);
946 // end of paragraph is always a suitable separator
947 if (i == last && x < width)
951 // well, if last_separator is still 0, the line isn't breakable.
952 // don't care and cut simply at the end
953 if (last_separator < 0) {
957 // manual labels cannot be broken in LaTeX, do not care
958 if (main_body && last_separator < main_body)
959 last_separator = main_body - 1;
961 return last_separator;
965 // returns the minimum space a row needs on the screen in pixel
966 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
972 // get the pure distance
973 Paragraph::size_type const last = rowLastPrintable(row);
975 // special handling of the right address boxes
976 if (textclasslist.Style(bview->buffer()->params.textclass,
977 row->par()->getLayout()).margintype
978 == MARGIN_RIGHT_ADDRESS_BOX) {
979 int const tmpfill = row->fill();
980 row->fill(0); // the minfill in MarginLeft()
981 w = leftMargin(bview, row);
984 w = leftMargin(bview, row);
986 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
987 row->par()->getLayout());
988 Paragraph::size_type const main_body =
989 beginningOfMainBody(bview->buffer(), row->par());
990 Paragraph::size_type i = row->pos();
993 if (main_body > 0 && i == main_body) {
994 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
995 if (row->par()->isLineSeparator(i - 1))
996 w -= singleWidth(bview, row->par(), i - 1);
997 int left_margin = labelEnd(bview, row);
1001 w += singleWidth(bview, row->par(), i);
1004 if (main_body > 0 && main_body > last) {
1005 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
1006 if (last >= 0 && row->par()->isLineSeparator(last))
1007 w -= singleWidth(bview, row->par(), last);
1008 int const left_margin = labelEnd(bview, row);
1009 if (w < left_margin)
1013 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1014 #ifdef WITH_WARNINGS
1015 #warning Please fix me (Jug!)
1025 // returns the minimum space a manual label needs on the screen in pixel
1026 int LyXText::labelFill(BufferView * bview, Row const * row) const
1028 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1029 // -1 because a label ends either with a space that is in the label,
1030 // or with the beginning of a footnote that is outside the label.
1032 // I don't understand this code in depth, but sometimes "last" is
1033 // less than 0 and this causes a crash. This fix seems to work
1034 // correctly, but I bet the real error is elsewhere. The bug is
1035 // triggered when you have an open footnote in a paragraph
1036 // environment with a manual label. (Asger)
1037 if (last < 0) last = 0;
1039 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1046 w += singleWidth(bview, row->par(), i);
1051 if (!row->par()->params().labelWidthString().empty()) {
1052 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1053 getFont(bview->buffer(), row->par(), -2)) - w,
1061 // returns the number of separators in the specified row. The separator
1062 // on the very last column doesnt count
1063 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1065 Paragraph::size_type const last = rowLast(row);
1066 Paragraph::size_type p =
1067 max(row->pos(), beginningOfMainBody(buf, row->par()));
1069 for (; p < last; ++p) {
1070 if (row->par()->isSeparator(p)) {
1078 // returns the number of hfills in the specified row. The LyX-Hfill is
1079 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1080 // ignored. This is *MUCH* more usefull than not to ignore!
1081 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1083 Paragraph::size_type const last = rowLast(row);
1084 Paragraph::size_type first = row->pos();
1085 if (first) { /* hfill *DO* count at the beginning
1087 while(first <= last && row->par()->isHfill(first))
1091 first = max(first, beginningOfMainBody(buf, row->par()));
1093 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1094 if (row->par()->isHfill(p)) {
1102 // like NumberOfHfills, but only those in the manual label!
1103 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1105 Paragraph::size_type last = rowLast(row);
1106 Paragraph::size_type first = row->pos();
1107 if (first) { /* hfill *DO* count at the beginning
1109 while(first < last && row->par()->isHfill(first))
1113 last = min(last, beginningOfMainBody(buf, row->par()));
1115 for (Paragraph::size_type p = first;
1116 p < last; ++p) { // last, because the end is ignored!
1117 if (row->par()->isHfill(p)) {
1125 // returns true, if a expansion is needed.
1126 // Rules are given by LaTeX
1127 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1128 Paragraph::size_type pos) const
1130 // by the way, is it a hfill?
1131 if (!row_ptr->par()->isHfill(pos))
1134 // at the end of a row it does not count
1135 if (pos >= rowLast(row_ptr))
1138 // at the beginning of a row it does not count, if it is not
1139 // the first row of a paragaph
1140 if (!row_ptr->pos())
1143 // in some labels it does not count
1144 if (textclasslist.Style(buf->params.textclass,
1145 row_ptr->par()->getLayout()).margintype
1147 && pos < beginningOfMainBody(buf, row_ptr->par()))
1150 // if there is anything between the first char of the row and
1151 // the sepcified position that is not a newline and not a hfill,
1152 // the hfill will count, otherwise not
1153 Paragraph::size_type i = row_ptr->pos();
1154 while (i < pos && (row_ptr->par()->isNewline(i)
1155 || row_ptr->par()->isHfill(i)))
1162 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1164 /* get the maximum ascent and the maximum descent */
1167 float layoutasc = 0;
1168 float layoutdesc = 0;
1171 Inset * tmpinset = 0;
1173 /* this must not happen before the currentrow for clear reasons.
1174 so the trick is just to set the current row onto this row */
1176 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1178 /* ok , let us initialize the maxasc and maxdesc value.
1179 * This depends in LaTeX of the font of the last character
1180 * in the paragraph. The hack below is necessary because
1181 * of the possibility of open footnotes */
1183 /* Correction: only the fontsize count. The other properties
1184 are taken from the layoutfont. Nicer on the screen :) */
1185 Paragraph * par = row_ptr->par();
1186 Paragraph * firstpar = row_ptr->par();
1188 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1189 firstpar->getLayout());
1191 LyXFont font = getFont(bview->buffer(), par, par->size() - 1);
1192 LyXFont::FONT_SIZE const size = font.size();
1193 font = getFont(bview->buffer(), par, -1);
1196 LyXFont labelfont = getFont(bview->buffer(), par, -2);
1198 float spacing_val = 1.0;
1199 if (!row_ptr->par()->params().spacing().isDefault()) {
1200 spacing_val = row_ptr->par()->params().spacing().getValue();
1202 spacing_val = bview->buffer()->params.spacing.getValue();
1204 //lyxerr << "spacing_val = " << spacing_val << endl;
1206 int maxasc = int(lyxfont::maxAscent(font) *
1207 layout.spacing.getValue() *
1209 int maxdesc = int(lyxfont::maxDescent(font) *
1210 layout.spacing.getValue() *
1212 int const pos_end = rowLast(row_ptr);
1216 // Check if any insets are larger
1217 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1218 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1219 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1220 tmpinset = row_ptr->par()->getInset(pos);
1222 #if 1 // this is needed for deep update on initialitation
1223 tmpinset->update(bview, tmpfont);
1225 asc = tmpinset->ascent(bview, tmpfont);
1226 desc = tmpinset->descent(bview, tmpfont);
1227 maxwidth += tmpinset->width(bview, tmpfont);
1228 maxasc = max(maxasc, asc);
1229 maxdesc = max(maxdesc, desc);
1232 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1236 // Check if any custom fonts are larger (Asger)
1237 // This is not completely correct, but we can live with the small,
1238 // cosmetic error for now.
1239 LyXFont::FONT_SIZE const maxsize =
1240 row_ptr->par()->highestFontInRange(row_ptr->pos(),
1242 if (maxsize > font.size()) {
1243 font.setSize(maxsize);
1245 asc = lyxfont::maxAscent(font);
1246 desc = lyxfont::maxDescent(font);
1253 // This is nicer with box insets:
1257 row_ptr->ascent_of_text(maxasc);
1259 // is it a top line?
1260 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1262 // some parksips VERY EASY IMPLEMENTATION
1263 if (bview->buffer()->params.paragraph_separation ==
1264 BufferParams::PARSEP_SKIP) {
1265 if (layout.isParagraph()
1266 && firstpar->getDepth() == 0
1267 && firstpar->previous())
1268 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1269 else if (firstpar->previous()
1270 && textclasslist.Style(bview->buffer()->params.textclass,
1271 firstpar->previous()->getLayout()).isParagraph()
1272 && firstpar->previous()->getDepth() == 0)
1273 // is it right to use defskip here too? (AS)
1274 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1277 // the paper margins
1278 if (!row_ptr->par()->previous() && bv_owner)
1279 maxasc += LYX_PAPER_MARGIN;
1281 // add the vertical spaces, that the user added
1282 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1283 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1285 // do not forget the DTP-lines!
1286 // there height depends on the font of the nearest character
1287 if (firstpar->params().lineTop())
1288 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1291 // and now the pagebreaks
1292 if (firstpar->params().pagebreakTop())
1293 maxasc += 3 * defaultHeight();
1295 // This is special code for the chapter, since the label of this
1296 // layout is printed in an extra row
1297 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1298 && bview->buffer()->params.secnumdepth >= 0) {
1299 float spacing_val = 1.0;
1300 if (!row_ptr->par()->params().spacing().isDefault()) {
1301 spacing_val = row_ptr->par()->params().spacing().getValue();
1303 spacing_val = bview->buffer()->params.spacing.getValue();
1306 labeladdon = int(lyxfont::maxDescent(labelfont) *
1307 layout.spacing.getValue() *
1309 + int(lyxfont::maxAscent(labelfont) *
1310 layout.spacing.getValue() *
1314 // special code for the top label
1315 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1316 || layout.labeltype == LABEL_BIBLIO
1317 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1318 && row_ptr->par()->isFirstInSequence()
1319 && !row_ptr->par()->getLabelstring().empty()) {
1320 float spacing_val = 1.0;
1321 if (!row_ptr->par()->params().spacing().isDefault()) {
1322 spacing_val = row_ptr->par()->params().spacing().getValue();
1324 spacing_val = bview->buffer()->params.spacing.getValue();
1328 (lyxfont::maxAscent(labelfont) *
1329 layout.spacing.getValue() *
1331 +(lyxfont::maxDescent(labelfont) *
1332 layout.spacing.getValue() *
1334 + layout.topsep * defaultHeight()
1335 + layout.labelbottomsep * defaultHeight());
1338 // and now the layout spaces, for example before and after a section,
1339 // or between the items of a itemize or enumerate environment
1341 if (!firstpar->params().pagebreakTop()) {
1342 Paragraph * prev = row_ptr->par()->previous();
1344 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1345 if (prev && prev->getLayout() == firstpar->getLayout()
1346 && prev->getDepth() == firstpar->getDepth()
1347 && prev->getLabelWidthString() == firstpar->getLabelWidthString())
1349 layoutasc = (layout.itemsep * defaultHeight());
1351 else if (row_ptr->previous()) {
1352 tmptop = layout.topsep;
1354 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1355 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1356 row_ptr->previous()->par()->
1357 getLayout()).bottomsep;
1360 layoutasc = (tmptop * defaultHeight());
1362 else if (row_ptr->par()->params().lineTop()) {
1363 tmptop = layout.topsep;
1366 layoutasc = (tmptop * defaultHeight());
1369 prev = row_ptr->par()->outerHook();
1371 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1372 prev->getLayout()).parsep * defaultHeight());
1375 if (firstpar->previous()
1376 && firstpar->previous()->getDepth() == 0
1377 && firstpar->previous()->getLayout() != firstpar->getLayout()) {
1380 else if (firstpar->previous()){
1381 maxasc += int(layout.parsep * defaultHeight());
1387 // is it a bottom line?
1388 if (row_ptr->par() == par
1389 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
1391 // the paper margins
1392 if (!par->next() && bv_owner)
1393 maxdesc += LYX_PAPER_MARGIN;
1395 // add the vertical spaces, that the user added
1396 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1397 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1399 // do not forget the DTP-lines!
1400 // there height depends on the font of the nearest character
1401 if (firstpar->params().lineBottom())
1402 maxdesc += 2 * lyxfont::ascent('x',
1403 getFont(bview->buffer(),
1404 par, par->size() - 1));
1406 // and now the pagebreaks
1407 if (firstpar->params().pagebreakBottom())
1408 maxdesc += 3 * defaultHeight();
1410 // and now the layout spaces, for example before and after
1411 // a section, or between the items of a itemize or enumerate
1413 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1414 Paragraph * nextpar = row_ptr->par()->next();
1415 Paragraph * comparepar = row_ptr->par();
1419 if (comparepar->getDepth() > nextpar->getDepth()) {
1420 usual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1421 comparepar = comparepar->depthHook(nextpar->getDepth());
1422 if (comparepar->getLayout()!= nextpar->getLayout()
1423 || nextpar->getLabelWidthString() !=
1424 comparepar->getLabelWidthString())
1425 unusual = (textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1427 if (unusual > usual)
1428 layoutdesc = unusual;
1432 else if (comparepar->getDepth() == nextpar->getDepth()) {
1434 if (comparepar->getLayout()!= nextpar->getLayout()
1435 || nextpar->getLabelWidthString() !=
1436 comparepar->getLabelWidthString())
1437 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass, comparepar->getLayout()).bottomsep * defaultHeight());
1442 // incalculate the layout spaces
1443 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1444 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1446 // calculate the new height of the text
1447 height -= row_ptr->height();
1449 row_ptr->height(maxasc + maxdesc + labeladdon);
1450 row_ptr->baseline(maxasc + labeladdon);
1452 height += row_ptr->height();
1455 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1456 row_ptr->width(int(maxwidth + x));
1459 width = max(0,workWidth(bview));
1461 if (r->width() > width)
1469 /* Appends the implicit specified paragraph behind the specified row,
1470 * start at the implicit given position */
1471 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1473 bool not_ready = true;
1475 // The last character position of a paragraph is an invariant so we can
1476 // safely get it here. (Asger)
1477 int const lastposition = row->par()->size();
1479 // Get the next breakpoint
1480 int z = nextBreakPoint(bview, row, workWidth(bview));
1484 // Insert the new row
1485 if (z < lastposition) {
1487 insertRow(row, row->par(), z);
1494 // Set the dimensions of the row
1495 #ifdef WITH_WARNINGS
1496 #warning Something is rotten here! (Jug)
1498 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1499 setHeightOfRow(bview, tmprow);
1501 } while (not_ready);
1505 void LyXText::breakAgain(BufferView * bview, Row * row) const
1507 bool not_ready = true;
1510 // get the next breakpoint
1511 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1514 if (z < row->par()->size()) {
1515 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1518 insertRow(row, row->par(), z);
1524 if (row->pos() == z)
1525 not_ready = false; // the rest will not change
1531 /* if there are some rows too much, delete them */
1532 /* only if you broke the whole paragraph! */
1533 Row * tmprow2 = row;
1534 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1535 tmprow2 = tmprow2->next();
1537 while (tmprow2 != row) {
1538 tmprow2 = tmprow2->previous();
1539 removeRow(tmprow2->next());
1544 /* set the dimensions of the row */
1545 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1546 setHeightOfRow(bview, tmprow);
1547 } while (not_ready);
1551 // this is just a little changed version of break again
1552 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1554 // get the next breakpoint
1555 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1558 if (z < row->par()->size()) {
1560 || (row->next() && row->next()->par() != row->par())) {
1561 /* insert a new row */
1563 insertRow(row, row->par(), z);
1569 if (row->pos() != z)
1573 // if there are some rows too much, delete them
1574 // only if you broke the whole paragraph!
1575 Row * tmprow2 = row;
1576 while (tmprow2->next()
1577 && tmprow2->next()->par() == row->par()) {
1578 tmprow2 = tmprow2->next();
1580 while (tmprow2 != row) {
1581 tmprow2 = tmprow2->previous();
1582 removeRow(tmprow2->next());
1586 // set the dimensions of the row
1587 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1588 setHeightOfRow(bview, tmprow);
1592 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1594 LyXLayout const & layout =
1595 textclasslist.Style(bview->buffer()->params.textclass,
1596 cursor.par()->getLayout());
1598 // this is only allowed, if the current paragraph is not empty or caption
1599 if ((cursor.par()->size() <= 0)
1600 && layout.labeltype!= LABEL_SENSITIVE)
1603 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1605 // Always break behind a space
1607 // It is better to erase the space (Dekel)
1608 if (cursor.pos() < cursor.par()->size()
1609 && cursor.par()->isLineSeparator(cursor.pos()))
1610 cursor.par()->erase(cursor.pos());
1611 // cursor.pos(cursor.pos() + 1);
1613 // break the paragraph
1617 keep_layout = layout.isEnvironment();
1618 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1621 // well this is the caption hack since one caption is really enough
1622 if (layout.labeltype == LABEL_SENSITIVE) {
1624 // set to standard-layout
1625 cursor.par()->setLayout(0);
1627 // set to standard-layout
1628 cursor.par()->next()->setLayout(0);
1631 /* if the cursor is at the beginning of a row without prior newline,
1633 * This touches only the screen-update. Otherwise we would may have
1634 * an empty row on the screen */
1635 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1636 && cursor.row()->pos() == cursor.pos()) {
1640 status(bview, LyXText::NEED_MORE_REFRESH);
1641 refresh_row = cursor.row();
1642 refresh_y = cursor.y() - cursor.row()->baseline();
1644 // Do not forget the special right address boxes
1645 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1646 while (refresh_row->previous() &&
1647 refresh_row->previous()->par() == refresh_row->par()) {
1648 refresh_row = refresh_row->previous();
1649 refresh_y -= refresh_row->height();
1652 removeParagraph(cursor.row());
1654 // set the dimensions of the cursor row
1655 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1657 setHeightOfRow(bview, cursor.row());
1659 while (cursor.par()->next()->size()
1660 && cursor.par()->next()->isNewline(0))
1661 cursor.par()->next()->erase(0);
1663 insertParagraph(bview, cursor.par()->next(), cursor.row());
1665 updateCounters(bview, cursor.row()->previous());
1667 /* This check is necessary. Otherwise the new empty paragraph will
1668 * be deleted automatically. And it is more friendly for the user! */
1670 setCursor(bview, cursor.par()->next(), 0);
1672 setCursor(bview, cursor.par(), 0);
1674 if (cursor.row()->next())
1675 breakAgain(bview, cursor.row()->next());
1681 // Just a macro to make some thing easier.
1682 void LyXText::redoParagraph(BufferView * bview) const
1684 clearSelection(bview);
1685 redoParagraphs(bview, cursor, cursor.par()->next());
1686 setCursorIntern(bview, cursor.par(), cursor.pos());
1690 /* insert a character, moves all the following breaks in the
1691 * same Paragraph one to the right and make a rebreak */
1692 void LyXText::insertChar(BufferView * bview, char c)
1694 setUndo(bview, Undo::INSERT,
1695 cursor.par(), cursor.par()->next());
1697 // When the free-spacing option is set for the current layout,
1698 // disable the double-space checking
1700 bool const freeSpacing =
1701 textclasslist.Style(bview->buffer()->params.textclass,
1702 cursor.row()->par()->getLayout()).free_spacing;
1705 if (lyxrc.auto_number) {
1706 static string const number_operators = "+-/*";
1707 static string const number_unary_operators = "+-";
1708 static string const number_seperators = ".,:";
1710 if (current_font.number() == LyXFont::ON) {
1711 if (!isdigit(c) && !contains(number_operators, c) &&
1712 !(contains(number_seperators, c) &&
1713 cursor.pos() >= 1 &&
1714 cursor.pos() < cursor.par()->size() &&
1715 getFont(bview->buffer(),
1717 cursor.pos()).number() == LyXFont::ON &&
1718 getFont(bview->buffer(),
1720 cursor.pos()-1).number() == LyXFont::ON)
1722 Number(bview); // Set current_font.number to OFF
1723 } else if (isdigit(c) &&
1724 real_current_font.isVisibleRightToLeft()) {
1725 Number(bview); // Set current_font.number to ON
1727 if (cursor.pos() > 0) {
1728 char const c = cursor.par()->getChar(cursor.pos() - 1);
1729 if (contains(number_unary_operators, c) &&
1730 (cursor.pos() == 1 ||
1731 cursor.par()->isSeparator(cursor.pos() - 2) ||
1732 cursor.par()->isNewline(cursor.pos() - 2) )
1734 setCharFont(bview->buffer(),
1738 } else if (contains(number_seperators, c) &&
1739 cursor.pos() >= 2 &&
1740 getFont(bview->buffer(),
1742 cursor.pos()-2).number() == LyXFont::ON) {
1743 setCharFont(bview->buffer(),
1753 /* First check, if there will be two blanks together or a blank at
1754 the beginning of a paragraph.
1755 I decided to handle blanks like normal characters, the main
1756 difference are the special checks when calculating the row.fill
1757 (blank does not count at the end of a row) and the check here */
1759 // The bug is triggered when we type in a description environment:
1760 // The current_font is not changed when we go from label to main text
1761 // and it should (along with realtmpfont) when we type the space.
1762 // CHECK There is a bug here! (Asger)
1764 LyXFont realtmpfont = real_current_font;
1765 LyXFont rawtmpfont = current_font; /* store the current font.
1766 * This is because of the use
1767 * of cursor movements. The moving
1768 * cursor would refresh the
1771 // Get the font that is used to calculate the baselineskip
1772 Paragraph::size_type const lastpos = cursor.par()->size();
1773 LyXFont rawparfont =
1774 cursor.par()->getFontSettings(bview->buffer()->params,
1777 bool jumped_over_space = false;
1779 if (!freeSpacing && IsLineSeparatorChar(c)) {
1780 if ((cursor.pos() > 0
1781 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1782 || (cursor.pos() > 0
1783 && cursor.par()->isNewline(cursor.pos() - 1))
1784 || (cursor.pos() == 0)) {
1785 static bool sent_space_message = false;
1786 if (!sent_space_message) {
1787 if (cursor.pos() == 0)
1788 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1790 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1791 sent_space_message = true;
1796 } else if (IsNewlineChar(c)) {
1797 if (cursor.par() == cursor.par()
1798 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1802 /* No newline at first position
1803 * of a paragraph or behind labels.
1804 * TeX does not allow that. */
1806 if (cursor.pos() < cursor.par()->size() &&
1807 cursor.par()->isLineSeparator(cursor.pos()))
1808 // newline always after a blank!
1810 cursor.row()->fill(-1); // to force a new break
1813 // the display inset stuff
1814 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1815 && cursor.row()->par()->getInset(cursor.row()->pos())
1816 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1817 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1818 cursor.row()->fill(-1); // to force a new break
1820 // get the cursor row fist
1821 Row * row = cursor.row();
1822 int y = cursor.y() - row->baseline();
1823 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1824 * already insertet the character */
1825 cursor.par()->insertChar(cursor.pos(), c);
1826 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1828 if (!jumped_over_space) {
1829 // refresh the positions
1831 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1832 tmprow = tmprow->next();
1833 tmprow->pos(tmprow->pos() + 1);
1837 // Is there a break one row above
1838 if ((cursor.par()->isLineSeparator(cursor.pos())
1839 || cursor.par()->isNewline(cursor.pos())
1840 || cursor.row()->fill() == -1)
1841 && row->previous() && row->previous()->par() == row->par()) {
1842 Paragraph::size_type z = nextBreakPoint(bview,
1845 if (z >= row->pos()) {
1848 // set the dimensions of the row above
1849 row->previous()->fill(fill(bview,
1853 setHeightOfRow(bview, row->previous());
1855 y -= row->previous()->height();
1857 refresh_row = row->previous();
1858 status(bview, LyXText::NEED_MORE_REFRESH);
1860 breakAgainOneRow(bview, row);
1862 current_font = rawtmpfont;
1863 real_current_font = realtmpfont;
1864 setCursor(bview, cursor.par(), cursor.pos() + 1,
1865 false, cursor.boundary());
1866 // cursor MUST be in row now.
1868 if (row->next() && row->next()->par() == row->par())
1869 need_break_row = row->next();
1873 // check, wether the last characters font has changed.
1874 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1875 && rawparfont != rawtmpfont)
1876 redoHeightOfParagraph(bview, cursor);
1883 // recalculate the fill of the row
1884 if (row->fill() >= 0) /* needed because a newline
1885 * will set fill to -1. Otherwise
1886 * we would not get a rebreak! */
1887 row->fill(fill(bview, row, workWidth(bview)));
1888 if (row->fill() < 0) {
1891 refresh_x = cursor.x();
1892 refresh_pos = cursor.pos();
1893 status(bview, LyXText::NEED_MORE_REFRESH);
1894 breakAgainOneRow(bview, row);
1895 // will the cursor be in another row now?
1896 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1897 if (row->next() && row->next()->par() == row->par())
1898 // this should always be true
1900 breakAgainOneRow(bview, row);
1902 current_font = rawtmpfont;
1903 real_current_font = realtmpfont;
1905 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1907 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1908 != cursor.boundary())
1909 setCursor(bview, cursor.par(), cursor.pos(), false,
1910 !cursor.boundary());
1911 if (row->next() && row->next()->par() == row->par())
1912 need_break_row = row->next();
1917 refresh_x = cursor.x();
1919 refresh_pos = cursor.pos();
1921 int const tmpheight = row->height();
1922 setHeightOfRow(bview, row);
1923 if (tmpheight == row->height())
1924 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1926 status(bview, LyXText::NEED_MORE_REFRESH);
1928 current_font = rawtmpfont;
1929 real_current_font = realtmpfont;
1930 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1934 // check, wether the last characters font has changed.
1935 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1936 && rawparfont != rawtmpfont) {
1937 redoHeightOfParagraph(bview, cursor);
1939 // now the special right address boxes
1940 if (textclasslist.Style(bview->buffer()->params.textclass,
1941 cursor.par()->getLayout()).margintype
1942 == MARGIN_RIGHT_ADDRESS_BOX) {
1943 redoDrawingOfParagraph(bview, cursor);
1951 void LyXText::charInserted()
1953 // Here we could call FinishUndo for every 20 characters inserted.
1954 // This is from my experience how emacs does it.
1955 static unsigned int counter;
1965 void LyXText::prepareToPrint(BufferView * bview,
1966 Row * row, float & x,
1967 float & fill_separator,
1969 float & fill_label_hfill,
1975 float w = row->fill();
1977 fill_label_hfill = 0;
1979 fill_label_hfill = 0;
1982 row->par()->isRightToLeftPar(bview->buffer()->params);
1984 x = (workWidth(bview) > 0)
1985 ? rightMargin(bview->buffer(), row) : 0;
1987 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
1989 // is there a manual margin with a manual label
1990 if (textclasslist.Style(bview->buffer()->params.textclass,
1991 row->par()->getLayout()).margintype == MARGIN_MANUAL
1992 && textclasslist.Style(bview->buffer()->params.textclass,
1993 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
1995 /* one more since labels are left aligned */
1996 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
1997 if (nlh && !row->par()->getLabelWidthString().empty()) {
1998 fill_label_hfill = labelFill(bview, row) / nlh;
2002 // are there any hfills in the row?
2003 float const nh = numberOfHfills(bview->buffer(), row);
2006 fill_hfill = w / nh;
2008 // is it block, flushleft or flushright?
2009 // set x how you need it
2011 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
2012 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2014 align = row->par()->params().align();
2016 // center displayed insets
2018 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2019 && (inset=row->par()->getInset(row->pos()))
2020 && (inset->display())) // || (inset->scroll() < 0)))
2021 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2022 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2025 case LYX_ALIGN_BLOCK:
2026 ns = numberOfSeparators(bview->buffer(), row);
2027 if (ns && row->next() && row->next()->par() == row->par() &&
2028 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2029 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2030 && row->next()->par()->getInset(row->next()->pos())
2031 && row->next()->par()->getInset(row->next()->pos())->display())
2033 fill_separator = w / ns;
2037 case LYX_ALIGN_RIGHT:
2040 case LYX_ALIGN_CENTER:
2048 computeBidiTables(bview->buffer(), row);
2050 Paragraph::size_type main_body =
2051 beginningOfMainBody(bview->buffer(), row->par());
2052 Paragraph::size_type last = rowLast(row);
2054 if (main_body > 0 &&
2055 (main_body-1 > last ||
2056 !row->par()->isLineSeparator(main_body-1))) {
2057 LyXLayout const & layout =
2058 textclasslist.Style(bview->buffer()->params.textclass,
2059 row->par()->getLayout());
2060 x += lyxfont::width(layout.labelsep,
2061 getFont(bview->buffer(), row->par(), -2));
2062 if (main_body-1 <= last)
2063 x += fill_label_hfill;
2068 /* important for the screen */
2071 /* the cursor set functions have a special mechanism. When they
2072 * realize, that you left an empty paragraph, they will delete it.
2073 * They also delete the corresponding row */
2075 void LyXText::cursorRightOneWord(BufferView * bview) const
2077 // treat floats, HFills and Insets as words
2078 LyXCursor tmpcursor = cursor;
2079 // CHECK See comment on top of text.C
2081 if (tmpcursor.pos() == tmpcursor.par()->size()
2082 && tmpcursor.par()->next()) {
2083 tmpcursor.par(tmpcursor.par()->next());
2088 // Skip through initial nonword stuff.
2089 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2090 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2091 // printf("Current pos1 %d", tmpcursor.pos()) ;
2092 tmpcursor.pos(tmpcursor.pos() + 1);
2095 // Advance through word.
2096 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2097 tmpcursor.par()->isWord( tmpcursor.pos())) {
2098 // printf("Current pos2 %d", tmpcursor.pos()) ;
2099 tmpcursor.pos(tmpcursor.pos() + 1);
2103 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2107 void LyXText::cursorTab(BufferView * bview) const
2109 LyXCursor tmpcursor = cursor;
2110 while (tmpcursor.pos() < tmpcursor.par()->size()
2111 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2112 tmpcursor.pos(tmpcursor.pos() + 1);
2114 if (tmpcursor.pos() == tmpcursor.par()->size()){
2115 if (tmpcursor.par()->next()) {
2116 tmpcursor.par(tmpcursor.par()->next());
2120 tmpcursor.pos(tmpcursor.pos() + 1);
2121 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2125 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2126 of prior word, not to end of next prior word. */
2128 void LyXText::cursorLeftOneWord(BufferView * bview) const
2130 LyXCursor tmpcursor = cursor;
2131 cursorLeftOneWord(tmpcursor);
2132 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2135 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2137 // treat HFills, floats and Insets as words
2140 && (cur.par()->isSeparator(cur.pos() - 1)
2141 || cur.par()->isKomma(cur.pos() - 1))
2142 && !(cur.par()->isHfill(cur.pos() - 1)
2143 || cur.par()->isInset(cur.pos() - 1)))
2144 cur.pos(cur.pos() - 1);
2147 && (cur.par()->isInset(cur.pos() - 1)
2148 || cur.par()->isHfill(cur.pos() - 1))) {
2149 cur.pos(cur.pos() - 1);
2150 } else if (!cur.pos()) {
2151 if (cur.par()->previous()){
2152 cur.par(cur.par()->previous());
2153 cur.pos(cur.par()->size());
2155 } else { // Here, cur != 0
2156 while (cur.pos() > 0 &&
2157 cur.par()->isWord(cur.pos()-1) )
2158 cur.pos(cur.pos() - 1);
2162 /* -------> Select current word. This depends on behaviour of
2163 CursorLeftOneWord(), so it is patched as well. */
2164 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2165 word_location const loc) const
2167 // first put the cursor where we wana start to select the word
2171 // Move cursor to the beginning, when not already there.
2172 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2173 && !from.par()->isKomma(from.pos() - 1))
2174 cursorLeftOneWord(from);
2177 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2183 while (to.pos() < to.par()->size()
2184 && !to.par()->isSeparator(to.pos())
2185 && !to.par()->isKomma(to.pos())
2186 && !to.par()->isHfill(to.pos()) )
2188 to.pos(to.pos() + 1);
2193 void LyXText::selectWord(BufferView * bview, word_location const loc)
2197 getWord(from, to, loc);
2199 setCursor(bview, from.par(), from.pos());
2200 selection.cursor = cursor;
2201 setCursor(bview, to.par(), to.pos() );
2202 setSelection(bview);
2205 /* -------> Select the word currently under the cursor when:
2206 1: no selection is currently set,
2207 [disabled] 2: the cursor is not at the borders of the word. */
2209 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2210 word_location const loc)
2212 if (!selection.set()
2214 && cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
2215 && !cursor.par()->isSeparator(cursor.pos())
2216 && !cursor.par()->isKomma(cursor.pos())
2217 && !cursor.par()->isSeparator(cursor.pos() -1)
2218 && !cursor.par()->isKomma(cursor.pos() -1)
2221 selectWord(bview, loc);
2228 // This function is only used by the spellchecker for NextWord().
2229 // It doesn't handle LYX_ACCENTs and probably never will.
2230 string const LyXText::selectNextWord(BufferView * bview,
2231 float & value) const
2233 if (the_locking_inset) {
2234 string str = the_locking_inset->selectNextWord(bview, value);
2236 value += float(cursor.y())/float(height);
2239 #warning Dekel please have a look on this one RTL? (Jug)
2240 // we have to go on checking so move cusor to the right
2241 if (cursor.pos() == cursor.par()->size()) {
2242 if (!cursor.par()->next())
2244 cursor.par(cursor.par()->next());
2247 cursor.pos(cursor.pos() + 1);
2249 Paragraph * tmppar = cursor.par();
2251 // If this is not the very first word, skip rest of
2252 // current word because we are probably in the middle
2253 // of a word if there is text here.
2254 if (cursor.pos() || cursor.par()->previous()) {
2255 while (cursor.pos() < cursor.par()->size()
2256 && cursor.par()->isLetter(cursor.pos()))
2257 cursor.pos(cursor.pos() + 1);
2260 // Now, skip until we have real text (will jump paragraphs)
2261 while ((cursor.par()->size() > cursor.pos()
2262 && (!cursor.par()->isLetter(cursor.pos())
2264 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2265 .latex() == LyXFont::ON
2268 && (!cursor.par()->isInset(cursor.pos()) ||
2269 !cursor.par()->getInset(cursor.pos())->isTextInset()))
2270 || (cursor.par()->size() == cursor.pos()
2271 && cursor.par()->next()))
2273 if (cursor.pos() == cursor.par()->size()) {
2274 cursor.par(cursor.par()->next());
2277 cursor.pos(cursor.pos() + 1);
2280 // now check if we hit an inset so it has to be a inset containing text!
2281 if (cursor.pos() < cursor.par()->size() &&
2282 cursor.par()->isInset(cursor.pos()))
2285 cursor.par()->getInset(cursor.pos())->edit(bview);
2286 // now call us again to do the above trick
2287 // but obviously we have to start from down below ;)
2288 return bview->text->selectNextWord(bview, value);
2291 // Update the value if we changed paragraphs
2292 if (cursor.par() != tmppar){
2293 setCursor(bview, cursor.par(), cursor.pos());
2294 value = float(cursor.y())/float(height);
2297 // Start the selection from here
2298 selection.cursor = cursor;
2302 // and find the end of the word
2303 // (optional hyphens are part of a word)
2304 while (cursor.pos() < cursor.par()->size()
2305 && (cursor.par()->isLetter(cursor.pos()))
2306 // assignment is intentional here
2307 || ((inset = getInset())
2308 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2309 && static_cast<InsetSpecialChar *>(inset)->kind()
2310 == InsetSpecialChar::HYPHENATION
2312 cursor.pos(cursor.pos() + 1);
2314 // Finally, we copy the word to a string and return it
2316 if (selection.cursor.pos() < cursor.pos()) {
2317 Paragraph::size_type i;
2318 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2319 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2320 str += cursor.par()->getChar(i);
2327 // This one is also only for the spellchecker
2328 void LyXText::selectSelectedWord(BufferView * bview)
2330 if (the_locking_inset) {
2331 the_locking_inset->selectSelectedWord(bview);
2334 // move cursor to the beginning
2335 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2337 // set the sel cursor
2338 selection.cursor = cursor;
2341 // now find the end of the word
2342 while (cursor.pos() < cursor.par()->size()
2343 && (cursor.par()->isLetter(cursor.pos())
2344 // assignment is intentional here
2345 || ((inset = getInset())
2346 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2347 && static_cast<InsetSpecialChar *>(inset)->kind()
2348 == InsetSpecialChar::HYPHENATION
2350 cursor.pos(cursor.pos() + 1);
2352 setCursor(bview, cursor.par(), cursor.pos());
2354 // finally set the selection
2355 setSelection(bview);
2359 /* -------> Delete from cursor up to the end of the current or next word. */
2360 void LyXText::deleteWordForward(BufferView * bview)
2362 if (!cursor.par()->size())
2365 LyXCursor tmpcursor = cursor;
2366 tmpcursor.row(0); //Â ??
2367 selection.set(true); // to avoid deletion
2368 cursorRightOneWord(bview);
2369 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2370 selection.cursor = cursor;
2372 setSelection(bview);
2374 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2375 cutSelection(bview);
2380 /* -------> Delete from cursor to start of current or prior word. */
2381 void LyXText::deleteWordBackward(BufferView * bview)
2383 if (!cursor.par()->size())
2386 LyXCursor tmpcursor = cursor;
2387 tmpcursor.row(0); // ??
2388 selection.set(true); // to avoid deletion
2389 cursorLeftOneWord(bview);
2390 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2391 selection.cursor = cursor;
2393 setSelection(bview);
2394 cutSelection(bview);
2399 /* -------> Kill to end of line. */
2400 void LyXText::deleteLineForward(BufferView * bview)
2402 if (!cursor.par()->size())
2403 // Paragraph is empty, so we just go to the right
2406 LyXCursor tmpcursor = cursor;
2407 // We can't store the row over a regular setCursor
2408 // so we set it to 0 and reset it afterwards.
2409 tmpcursor.row(0); //Â ??
2410 selection.set(true); // to avoid deletion
2412 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2413 selection.cursor = cursor;
2415 setSelection(bview);
2416 // What is this test for ??? (JMarc)
2417 if (!selection.set()) {
2418 deleteWordForward(bview);
2420 cutSelection(bview);
2426 // Change the case of a word at cursor position.
2427 // This function directly manipulates Paragraph::text because there
2428 // is no Paragraph::SetChar currently. I did what I could to ensure
2429 // that it is correct. I guess part of it should be moved to
2430 // Paragraph, but it will have to change for 1.1 anyway. At least
2431 // it does not access outside of the allocated array as the older
2432 // version did. (JMarc)
2433 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2438 if (selection.set()) {
2439 from = selection.start;
2442 getWord(from, to, PARTIAL_WORD);
2443 setCursor(bview, to.par(), to.pos() + 1);
2446 changeRegionCase(bview, from, to, action);
2450 void LyXText::changeRegionCase(BufferView * bview,
2451 LyXCursor const & from,
2452 LyXCursor const & to,
2453 LyXText::TextCase action)
2455 lyx::Assert(from <= to);
2457 setUndo(bview, Undo::FINISH,
2458 from.par(), to.par()->next());
2460 Paragraph::size_type pos = from.pos();
2461 Paragraph * par = from.par();
2463 while (par && (pos != to.pos() || par != to.par())) {
2464 unsigned char c = par->getChar(pos);
2465 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2467 case text_lowercase:
2470 case text_capitalization:
2472 action = text_lowercase;
2474 case text_uppercase:
2479 par->setChar(pos, c);
2480 checkParagraph(bview, par, pos);
2483 if (pos == par->size()) {
2488 if (to.row() != from.row()) {
2489 refresh_y = from.y() - from.row()->baseline();
2490 refresh_row = from.row();
2491 status(bview, LyXText::NEED_MORE_REFRESH);
2496 void LyXText::transposeChars(BufferView & bview)
2498 Paragraph * tmppar = cursor.par();
2500 setUndo(&bview, Undo::FINISH,
2501 tmppar, tmppar->next());
2503 Paragraph::size_type tmppos = cursor.pos();
2505 // First decide if it is possible to transpose at all
2507 // We are at the beginning of a paragraph.
2508 if (tmppos == 0) return;
2510 // We are at the end of a paragraph.
2511 if (tmppos == tmppar->size() - 1) return;
2513 unsigned char c1 = tmppar->getChar(tmppos);
2514 unsigned char c2 = tmppar->getChar(tmppos - 1);
2516 if (c1 != Paragraph::META_INSET
2517 && c2 != Paragraph::META_INSET) {
2518 tmppar->setChar(tmppos, c2);
2519 tmppar->setChar(tmppos - 1, c1);
2521 // We should have an implementation that handles insets
2522 // as well, but that will have to come later. (Lgb)
2523 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2527 void LyXText::Delete(BufferView * bview)
2529 // this is a very easy implementation
2531 LyXCursor old_cursor = cursor;
2532 int const old_cur_par_id = old_cursor.par()->id();
2533 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2534 old_cursor.par()->previous()->id() : 0;
2536 // just move to the right
2539 // CHECK Look at the comment here.
2540 // This check is not very good...
2541 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2542 // and that can very well delete the par or par->previous in
2543 // old_cursor. Will a solution where we compare paragraph id's
2545 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2546 == old_cur_par_prev_id
2547 && cursor.par()->id() != old_cur_par_id)
2548 return; // delete-empty-paragraph-mechanism has done it
2550 // if you had success make a backspace
2551 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2552 LyXCursor tmpcursor = cursor;
2553 cursor = old_cursor; // to make sure undo gets the right cursor position
2554 setUndo(bview, Undo::DELETE,
2555 cursor.par(), cursor.par()->next());
2562 void LyXText::backspace(BufferView * bview)
2564 // Get the font that is used to calculate the baselineskip
2565 Paragraph::size_type lastpos = cursor.par()->size();
2566 LyXFont rawparfont =
2567 cursor.par()->getFontSettings(bview->buffer()->params,
2570 if (cursor.pos() == 0) {
2571 // The cursor is at the beginning of a paragraph,
2572 // so the the backspace will collapse two paragraphs into one.
2574 // we may paste some paragraphs
2576 // is it an empty paragraph?
2579 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2580 // This is an empty paragraph and we delete it just by moving the cursor one step
2581 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2582 // of the paragraph.
2584 if (cursor.par()->previous()) {
2585 Paragraph * tmppar = cursor.par()->previous();
2586 if (cursor.par()->getLayout() == tmppar->getLayout()
2587 && cursor.par()->getAlign() == tmppar->getAlign()) {
2588 // Inherit bottom DTD from the paragraph below.
2589 // (the one we are deleting)
2590 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2591 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2592 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2597 // the layout things can change the height of a row !
2598 int const tmpheight = cursor.row()->height();
2599 setHeightOfRow(bview, cursor.row());
2600 if (cursor.row()->height() != tmpheight) {
2601 refresh_y = cursor.y() - cursor.row()->baseline();
2602 refresh_row = cursor.row();
2603 status(bview, LyXText::NEED_MORE_REFRESH);
2609 if (cursor.par()->previous()) {
2610 setUndo(bview, Undo::DELETE,
2611 cursor.par()->previous(),
2612 cursor.par()->next());
2615 Paragraph * tmppar = cursor.par();
2616 Row * tmprow = cursor.row();
2618 // We used to do cursorLeftIntern() here, but it is
2619 // not a good idea since it triggers the auto-delete
2620 // mechanism. So we do a cursorLeftIntern()-lite,
2621 // without the dreaded mechanism. (JMarc)
2622 if (cursor.par()->previous()) {
2623 // steps into the above paragraph.
2624 setCursorIntern(bview, cursor.par()->previous(),
2625 cursor.par()->previous()->size(),
2629 /* Pasting is not allowed, if the paragraphs have different
2630 layout. I think it is a real bug of all other
2631 word processors to allow it. It confuses the user.
2632 Even so with a footnote paragraph and a non-footnote
2633 paragraph. I will not allow pasting in this case,
2634 because the user would be confused if the footnote behaves
2635 different wether it is open or closed.
2637 Correction: Pasting is always allowed with standard-layout
2639 if (cursor.par() != tmppar
2640 && (cursor.par()->getLayout() == tmppar->getLayout()
2641 || tmppar->getLayout() == 0 /*standard*/)
2642 && cursor.par()->getAlign() == tmppar->getAlign()) {
2644 removeParagraph(tmprow);
2646 cursor.par()->pasteParagraph(bview->buffer()->params);
2648 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2649 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2650 // strangely enough it seems that commenting out the line above removes
2651 // most or all of the segfaults. I will however also try to move the
2652 // two Remove... lines in front of the PasteParagraph too.
2655 cursor.pos(cursor.pos() - 1);
2657 status(bview, LyXText::NEED_MORE_REFRESH);
2658 refresh_row = cursor.row();
2659 refresh_y = cursor.y() - cursor.row()->baseline();
2661 // remove the lost paragraph
2662 // This one is not safe, since the paragraph that the tmprow and the
2663 // following rows belong to has been deleted by the PasteParagraph
2664 // above. The question is... could this be moved in front of the
2666 //RemoveParagraph(tmprow);
2667 //RemoveRow(tmprow);
2669 // This rebuilds the rows.
2670 appendParagraph(bview, cursor.row());
2671 updateCounters(bview, cursor.row());
2673 // the row may have changed, block, hfills etc.
2674 setCursor(bview, cursor.par(), cursor.pos(), false);
2677 /* this is the code for a normal backspace, not pasting
2679 setUndo(bview, Undo::DELETE,
2680 cursor.par(), cursor.par()->next());
2681 // We used to do cursorLeftIntern() here, but it is
2682 // not a good idea since it triggers the auto-delete
2683 // mechanism. So we do a cursorLeftIntern()-lite,
2684 // without the dreaded mechanism. (JMarc)
2685 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2686 false, cursor.boundary());
2688 // some insets are undeletable here
2689 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2690 if (!cursor.par()->getInset(cursor.pos())->deletable())
2692 // force complete redo when erasing display insets
2693 // this is a cruel method but safe..... Matthias
2694 if (cursor.par()->getInset(cursor.pos())->display() ||
2695 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2696 cursor.par()->erase(cursor.pos());
2697 redoParagraph(bview);
2702 Row * row = cursor.row();
2703 int y = cursor.y() - row->baseline();
2704 Paragraph::size_type z;
2705 /* remember that a space at the end of a row doesnt count
2706 * when calculating the fill */
2707 if (cursor.pos() < rowLast(row) ||
2708 !cursor.par()->isLineSeparator(cursor.pos())) {
2709 row->fill(row->fill() + singleWidth(bview,
2714 /* some special code when deleting a newline. This is similar
2715 * to the behavior when pasting paragraphs */
2716 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2717 cursor.par()->erase(cursor.pos());
2718 // refresh the positions
2720 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2721 tmprow = tmprow->next();
2722 tmprow->pos(tmprow->pos() - 1);
2724 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2725 cursor.pos(cursor.pos() - 1);
2727 if (cursor.pos() < cursor.par()->size()
2728 && !cursor.par()->isSeparator(cursor.pos())) {
2729 cursor.par()->insertChar(cursor.pos(), ' ');
2730 setCharFont(bview->buffer(), cursor.par(),
2731 cursor.pos(), current_font);
2732 // refresh the positions
2734 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2735 tmprow = tmprow->next();
2736 tmprow->pos(tmprow->pos() + 1);
2740 cursor.par()->erase(cursor.pos());
2742 // refresh the positions
2744 while (tmprow->next()
2745 && tmprow->next()->par() == row->par()) {
2746 tmprow = tmprow->next();
2747 tmprow->pos(tmprow->pos() - 1);
2750 // delete newlines at the beginning of paragraphs
2751 while (cursor.par()->size() &&
2752 cursor.par()->isNewline(cursor.pos()) &&
2753 cursor.pos() == beginningOfMainBody(bview->buffer(),
2755 cursor.par()->erase(cursor.pos());
2756 // refresh the positions
2758 while (tmprow->next() &&
2759 tmprow->next()->par() == row->par()) {
2760 tmprow = tmprow->next();
2761 tmprow->pos(tmprow->pos() - 1);
2766 // is there a break one row above
2767 if (row->previous() && row->previous()->par() == row->par()) {
2768 z = nextBreakPoint(bview, row->previous(),
2770 if (z >= row->pos()) {
2773 Row * tmprow = row->previous();
2775 // maybe the current row is now empty
2776 if (row->pos() >= row->par()->size()) {
2781 breakAgainOneRow(bview, row);
2782 if (row->next() && row->next()->par() == row->par())
2783 need_break_row = row->next();
2788 // set the dimensions of the row above
2789 y -= tmprow->height();
2790 tmprow->fill(fill(bview, tmprow,
2792 setHeightOfRow(bview, tmprow);
2795 refresh_row = tmprow;
2796 status(bview, LyXText::NEED_MORE_REFRESH);
2797 setCursor(bview, cursor.par(), cursor.pos(),
2798 false, cursor.boundary());
2799 //current_font = rawtmpfont;
2800 //real_current_font = realtmpfont;
2801 // check, whether the last character's font has changed.
2803 cursor.par()->getFontSettings(bview->buffer()->params,
2804 cursor.par()->size() - 1))
2805 redoHeightOfParagraph(bview, cursor);
2810 // break the cursor row again
2811 if (row->next() && row->next()->par() == row->par() &&
2812 (rowLast(row) == row->par()->size() - 1 ||
2813 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2815 /* it can happen that a paragraph loses one row
2816 * without a real breakup. This is when a word
2817 * is to long to be broken. Well, I don t care this
2819 if (rowLast(row) == row->par()->size() - 1)
2820 removeRow(row->next());
2824 status(bview, LyXText::NEED_MORE_REFRESH);
2826 breakAgainOneRow(bview, row);
2827 // will the cursor be in another row now?
2828 if (row->next() && row->next()->par() == row->par() &&
2829 rowLast(row) <= cursor.pos()) {
2831 breakAgainOneRow(bview, row);
2834 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2836 if (row->next() && row->next()->par() == row->par())
2837 need_break_row = row->next();
2841 // set the dimensions of the row
2842 row->fill(fill(bview, row, workWidth(bview)));
2843 int const tmpheight = row->height();
2844 setHeightOfRow(bview, row);
2845 if (tmpheight == row->height())
2846 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2848 status(bview, LyXText::NEED_MORE_REFRESH);
2851 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2855 // current_font = rawtmpfont;
2856 // real_current_font = realtmpfont;
2858 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2859 != cursor.boundary())
2860 setCursor(bview, cursor.par(), cursor.pos(), false,
2861 !cursor.boundary());
2863 lastpos = cursor.par()->size();
2864 if (cursor.pos() == lastpos)
2865 setCurrentFont(bview);
2867 // check, whether the last characters font has changed.
2869 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2870 redoHeightOfParagraph(bview, cursor);
2872 // now the special right address boxes
2873 if (textclasslist.Style(bview->buffer()->params.textclass,
2874 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2875 redoDrawingOfParagraph(bview, cursor);
2881 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2882 Row * row_ptr, int y, bool cleared)
2884 // returns a printed row
2885 Painter & pain = bview->painter();
2888 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2890 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2892 Paragraph::size_type vpos;
2893 Paragraph::size_type pos;
2897 LyXFont font(LyXFont::ALL_SANE);
2899 if (row_ptr->height() <= 0) {
2900 lyxerr << "LYX_ERROR: row.height: "
2901 << row_ptr->height() << endl;
2906 float fill_separator;
2908 float fill_label_hfill;
2909 prepareToPrint(bview, row_ptr, x, fill_separator,
2910 fill_hfill, fill_label_hfill);
2912 if (inset_owner && (x < 0))
2916 // clear the area where we want to paint/print
2917 int const ww = bview->workWidth();
2919 bool clear_area = true;
2922 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2923 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2924 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2925 clear_area = inset->doClearArea();
2927 // we don't need to clear it's already done!!!
2930 } else if (clear_area) {
2931 #ifdef WITH_WARNINGS
2932 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
2933 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
2935 int const y = y_offset < 0 ? 0 : y_offset;
2936 int const h = y_offset < 0 ?
2937 row_ptr->height() + y_offset : row_ptr->height();
2938 int const w = inset_owner ?
2939 inset_owner->width(bview, font) - 2 : ww;
2940 int const x = x_offset;
2941 pain.fillRectangle(x, y, w, h);
2942 } else if (inset != 0) {
2943 int h = row_ptr->baseline() - inset->ascent(bview, font);
2945 int const w = (inset_owner ?
2946 inset_owner->width(bview, font) : ww);
2947 pain.fillRectangle(x_offset, y_offset, w, h);
2949 h += inset->ascent(bview, font) + inset->descent(bview, font);
2950 if ((row_ptr->height() - h) > 0) {
2951 int const w = (inset_owner ?
2952 inset_owner->width(bview, font) : ww);
2953 pain.fillRectangle(x_offset, y_offset + h,
2954 w, row_ptr->height() - h);
2956 if (!inset_owner && !inset->display() && !inset->needFullRow())
2958 int const w = inset->width(bview, font) + int(x);
2959 pain.fillRectangle(w, y_offset, ww - w, row_ptr->height());
2963 if (selection.set()) {
2964 int const w = (inset_owner ?
2965 inset_owner->width(bview, font) : ww);
2967 if (bidi_same_direction) {
2968 if (selection.start.row() == row_ptr &&
2969 selection.end.row() == row_ptr) {
2970 if (selection.start.x() < selection.end.x())
2971 pain.fillRectangle(x_offset + selection.start.x(),
2973 selection.end.x() - selection.start.x(),
2977 pain.fillRectangle(x_offset + selection.end.x(),
2979 selection.start.x() - selection.end.x(),
2982 } else if (selection.start.row() == row_ptr) {
2984 pain.fillRectangle(x_offset, y_offset,
2985 selection.start.x(),
2989 pain.fillRectangle(x_offset + selection.start.x(),
2991 w - selection.start.x(),
2994 } else if (selection.end.row() == row_ptr) {
2996 pain.fillRectangle(x_offset + selection.end.x(),
2998 w - selection.end.x(),
3002 pain.fillRectangle(x_offset, y_offset,
3006 } else if (y > selection.start.y()
3007 && y < selection.end.y()) {
3008 pain.fillRectangle(x_offset, y_offset, w,
3012 } else if (selection.start.row() != row_ptr &&
3013 selection.end.row() != row_ptr &&
3014 y > selection.start.y()
3015 && y < selection.end.y()) {
3016 pain.fillRectangle(x_offset, y_offset, w,
3019 } else if (selection.start.row() == row_ptr ||
3020 selection.end.row() == row_ptr) {
3022 if ((selection.start.row() != row_ptr && !is_rtl) ||
3023 (selection.end.row() != row_ptr && is_rtl))
3024 pain.fillRectangle(x_offset, y_offset,
3028 Paragraph::size_type main_body =
3029 beginningOfMainBody(bview->buffer(),
3032 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3033 pos = vis2log(vpos);
3034 float const old_tmpx = tmpx;
3035 if (main_body > 0 && pos == main_body-1) {
3036 tmpx += fill_label_hfill +
3037 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3038 row_ptr->par()->getLayout()).labelsep,
3039 getFont(bview->buffer(),row_ptr->par(), -2));
3040 if (row_ptr->par()->isLineSeparator(main_body-1))
3041 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3043 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3044 tmpx += singleWidth(bview, row_ptr->par(), pos);
3045 if (pos >= main_body)
3048 tmpx += fill_label_hfill;
3050 else if (row_ptr->par()->isSeparator(pos)) {
3051 tmpx += singleWidth(bview, row_ptr->par(), pos);
3052 if (pos >= main_body)
3053 tmpx += fill_separator;
3055 tmpx += singleWidth(bview, row_ptr->par(), pos);
3057 if ((selection.start.row() != row_ptr ||
3058 selection.start.pos() <= pos) &&
3059 (selection.end.row() != row_ptr ||
3060 pos < selection.end.pos()) )
3061 // Here we do not use x_offset as x_offset was
3063 pain.fillRectangle(int(old_tmpx),
3065 int(tmpx - old_tmpx + 1),
3070 if ((selection.start.row() != row_ptr && is_rtl) ||
3071 (selection.end.row() != row_ptr && !is_rtl) )
3072 pain.fillRectangle(x_offset + int(tmpx),
3082 // Draw appendix lines
3083 Paragraph * firstpar = row_ptr->par();
3085 if (firstpar->params().appendix()) {
3086 pain.line(1, y_offset,
3087 1, y_offset + row_ptr->height(),
3088 LColor::appendixline);
3089 pain.line(ww - 2, y_offset,
3090 ww - 2, y_offset + row_ptr->height(),
3091 LColor::appendixline);
3095 Paragraph::depth_type const depth = firstpar->getDepth();
3097 Paragraph::depth_type next_depth = 0;
3098 Paragraph::depth_type prev_depth = 0;
3099 if (row_ptr->next())
3100 next_depth = row_ptr->next()->par()->getDepth();
3101 if (row_ptr->previous())
3102 prev_depth = row_ptr->previous()->par()->getDepth();
3104 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3105 int const line_x = (LYX_PAPER_MARGIN / 5) *
3106 i + box_x + x_offset;
3107 pain.line(line_x, y_offset, line_x,
3108 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3112 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3115 pain.fillRectangle(line_x,
3116 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3117 LYX_PAPER_MARGIN / 5, 2,
3123 LyXLayout const & layout =
3124 textclasslist.Style(bview->buffer()->params.textclass,
3125 row_ptr->par()->getLayout());
3128 int y_bottom = row_ptr->height();
3130 // is it a first row?
3131 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3133 // start of appendix?
3134 if (row_ptr->par()->params().startOfAppendix()) {
3135 pain.line(1, y_offset,
3137 LColor::appendixline);
3140 // think about the margins
3141 if (!row_ptr->previous() && bv_owner)
3142 y_top += LYX_PAPER_MARGIN;
3144 // draw a top pagebreak
3145 if (row_ptr->par()->params().pagebreakTop()) {
3147 pb_font.setColor(LColor::pagebreak).decSize();
3151 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3153 y_offset + y_top + 2 * defaultHeight(),
3155 Painter::line_onoffdash)
3159 _("Page Break (top)"),
3162 LColor::background, false, w, a, d);
3165 lyxfont::rectText(_("Page Break (top)"), pb_font,
3168 pain.rectText((ww - w)/2,
3169 y_offset + y_top + 2 * defaultHeight() + d,
3170 _("Page Break (top)"),
3173 LColor::background);
3174 y_top += 3 * defaultHeight();
3177 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3179 pain.line(0, y_offset + 2 + y_top,
3180 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3183 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3185 y_offset + y_top + 3 * defaultHeight(),
3188 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3189 LYX_PAPER_MARGIN / 2,
3190 y_offset + y_top + 3 * defaultHeight(),
3193 y_top += 3 * defaultHeight();
3196 // think about user added space
3197 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3199 // think about the parskip
3200 // some parskips VERY EASY IMPLEMENTATION
3201 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3202 if (layout.latextype == LATEX_PARAGRAPH
3203 && firstpar->getDepth() == 0
3204 && firstpar->previous())
3205 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3206 else if (firstpar->previous()
3207 && textclasslist.Style(bview->buffer()->params.textclass,
3208 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3209 && firstpar->previous()->getDepth() == 0)
3210 // is it right to use defskip here, too? (AS)
3211 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3214 if (row_ptr->par()->params().lineTop()) {
3216 y_top += lyxfont::ascent('x',
3217 getFont(bview->buffer(),
3218 row_ptr->par(), 0));
3219 int const w = (inset_owner ?
3220 inset_owner->width(bview, font) : ww);
3221 int const xp = static_cast<int>(inset_owner ? x : 0);
3222 pain.line(xp, y_offset + y_top,
3223 w, y_offset + y_top,
3225 Painter::line_solid,
3226 Painter::line_thick);
3228 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3229 row_ptr->par(), 0));
3232 // should we print a label?
3233 if (layout.labeltype >= LABEL_STATIC
3234 && (layout.labeltype != LABEL_STATIC
3235 || layout.latextype != LATEX_ENVIRONMENT
3236 || row_ptr->par()->isFirstInSequence())) {
3237 font = getFont(bview->buffer(), row_ptr->par(), -2);
3238 if (!row_ptr->par()->getLabelstring().empty()) {
3240 string const tmpstring =
3241 row_ptr->par()->getLabelstring();
3243 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3244 if (bview->buffer()->params.secnumdepth >= 0) {
3245 // this is special code for
3246 // the chapter layout. This is
3247 // printed in an extra row
3248 // and has a pagebreak at
3250 float spacing_val = 1.0;
3251 if (!row_ptr->par()->params().spacing().isDefault()) {
3252 spacing_val = row_ptr->par()->params().spacing().getValue();
3254 spacing_val = bview->buffer()->params.spacing.getValue();
3257 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3258 + int(layout.parsep) * defaultHeight();
3260 tmpx = ww - leftMargin(bview, row_ptr) -
3261 lyxfont::width(tmpstring, font);
3262 pain.text(int(tmpx),
3263 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3268 tmpx = ww - leftMargin(bview, row_ptr)
3269 + lyxfont::width(layout.labelsep, font);
3271 tmpx = x - lyxfont::width(layout.labelsep, font)
3272 - lyxfont::width(tmpstring, font);
3275 pain.text(int(tmpx),
3276 y_offset + row_ptr->baseline(),
3280 // the labels at the top of an environment.
3281 // More or less for bibliography
3282 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3283 layout.labeltype == LABEL_BIBLIO ||
3284 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3285 if (row_ptr->par()->isFirstInSequence()) {
3286 font = getFont(bview->buffer(),
3287 row_ptr->par(), -2);
3288 if (!row_ptr->par()->getLabelstring().empty()) {
3289 string const tmpstring =
3290 row_ptr->par()->getLabelstring();
3291 float spacing_val = 1.0;
3292 if (!row_ptr->par()->params().spacing().isDefault()) {
3293 spacing_val = row_ptr->par()->params().spacing().getValue();
3295 spacing_val = bview->buffer()->params.spacing.getValue();
3298 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3299 + (layout.labelbottomsep * defaultHeight()));
3302 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3303 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3304 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3305 tmpx -= lyxfont::width(tmpstring, font) / 2;
3307 tmpx = ww - leftMargin(bview, row_ptr) -
3308 lyxfont::width(tmpstring, font);
3309 pain.text(int(tmpx),
3310 y_offset + row_ptr->baseline()
3311 - row_ptr->ascent_of_text()
3317 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3318 font = getFont(bview->buffer(), row_ptr->par(), -1);
3320 tmpx = ww - leftMargin(bview, row_ptr)
3321 + lyxfont::width(layout.labelsep, font);
3323 tmpx = x - lyxfont::width(layout.labelsep, font)
3324 - row_ptr->par()->bibkey->width(bview, font);
3325 row_ptr->par()->bibkey->draw(bview, font,
3326 y_offset + row_ptr->baseline(),
3331 // is it a last row?
3332 Paragraph * par = row_ptr->par();
3333 if (row_ptr->par() == par
3334 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3335 // think about the margins
3336 if (!row_ptr->next() && bv_owner)
3337 y_bottom -= LYX_PAPER_MARGIN;
3339 // draw a bottom pagebreak
3340 if (firstpar->params().pagebreakBottom()) {
3342 pb_font.setColor(LColor::pagebreak).decSize();
3343 int const y_place = y_offset + y_bottom
3344 - 2 * defaultHeight();
3350 .line(0, y_place, ww, y_place,
3352 Painter::line_onoffdash)
3355 _("Page Break (bottom)"),
3358 LColor::background, false, w, a, d);
3361 lyxfont::rectText(_("Page Break (bottom)"), pb_font,
3364 pain.rectText((ww - w) / 2, y_place + d,
3365 _("Page Break (bottom)"),
3368 LColor::background);
3369 y_bottom -= 3 * defaultHeight();
3372 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3373 // draw a vfill bottom
3374 int const y_place = y_offset + y_bottom
3375 - 3 * defaultHeight();
3377 pain.line(0, y_place,
3378 LYX_PAPER_MARGIN, y_place,
3380 pain.line(0, y_offset + y_bottom - 2,
3382 y_offset + y_bottom - 2,
3384 pain.line(LYX_PAPER_MARGIN / 2,
3386 LYX_PAPER_MARGIN / 2,
3387 y_offset + y_bottom - 2,
3389 y_bottom -= 3 * defaultHeight();
3392 // think about user added space
3393 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3395 if (firstpar->params().lineBottom()) {
3396 // draw a bottom line
3397 y_bottom -= lyxfont::ascent('x',
3398 getFont(bview->buffer(),
3401 int const w = (inset_owner ?
3402 inset_owner->width(bview, font) : ww);
3403 int const xp = static_cast<int>(inset_owner ? x : 0);
3404 pain.line(xp, y_offset + y_bottom,
3405 w, y_offset + y_bottom,
3406 LColor::topline, Painter::line_solid,
3407 Painter::line_thick);
3408 y_bottom -= lyxfont::ascent('x',
3409 getFont(bview->buffer(),
3415 int const endlabel =
3416 row_ptr->par()->getEndLabel(bview->buffer()->params);
3419 case END_LABEL_FILLED_BOX:
3421 LyXFont const font = getFont(bview->buffer(),
3422 row_ptr->par(), last);
3423 int const size = int(0.75 * lyxfont::maxAscent(font));
3424 int const y = (y_offset + row_ptr->baseline()) - size;
3425 int x = is_rtl ? LYX_PAPER_MARGIN
3426 : ww - LYX_PAPER_MARGIN - size;
3428 if (row_ptr->fill() <= size)
3429 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3430 if (endlabel == END_LABEL_BOX) {
3431 pain.line(x, y, x, y + size,
3433 pain.line(x + size, y, x + size , y + size,
3435 pain.line(x, y, x + size, y,
3437 pain.line(x, y + size, x + size, y + size,
3440 pain.fillRectangle(x, y, size, size,
3444 case END_LABEL_STATIC:
3446 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3447 string const tmpstring = textclasslist.
3448 Style(bview->buffer()->params.textclass,
3449 layout).endlabelstring();
3450 font = getFont(bview->buffer(), row_ptr->par(), -2);
3451 int const tmpx = is_rtl ?
3452 int(x) - lyxfont::width(tmpstring, font)
3453 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3454 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3457 case END_LABEL_NO_LABEL:
3462 // draw the text in the pixmap
3464 vpos = row_ptr->pos();
3466 Paragraph::size_type main_body =
3467 beginningOfMainBody(bview->buffer(), row_ptr->par());
3468 if (main_body > 0 &&
3469 (main_body-1 > last ||
3470 !row_ptr->par()->isLineSeparator(main_body - 1)))
3473 while (vpos <= last) {
3474 pos = vis2log(vpos);
3475 if (main_body > 0 && pos == main_body - 1) {
3476 x += fill_label_hfill
3477 + lyxfont::width(layout.labelsep,
3478 getFont(bview->buffer(),
3479 row_ptr->par(), -2))
3480 - singleWidth(bview,
3485 if (row_ptr->par() ->isHfill(pos)) {
3488 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3490 y_offset + row_ptr->baseline(),
3493 if (hfillExpansion(bview->buffer(),
3495 if (pos >= main_body) {
3497 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3498 int(x + fill_hfill),
3499 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3501 Painter::line_onoffdash);
3505 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3506 int(x + fill_label_hfill),
3507 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3509 Painter::line_onoffdash);
3511 x += fill_label_hfill;
3514 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3516 y_offset + row_ptr->baseline(),
3521 } else if (row_ptr->par()->isSeparator(pos)) {
3522 x += singleWidth(bview,
3523 row_ptr->par(), pos);
3524 if (pos >= main_body)
3525 x += fill_separator;
3528 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3533 int LyXText::defaultHeight() const
3535 LyXFont font(LyXFont::ALL_SANE);
3536 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3540 /* returns the column near the specified x-coordinate of the row
3541 * x is set to the real beginning of this column */
3542 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3543 bool & boundary) const
3546 float fill_separator;
3548 float fill_label_hfill;
3550 prepareToPrint(bview, row, tmpx, fill_separator,
3551 fill_hfill, fill_label_hfill);
3553 Paragraph::size_type vc = row->pos();
3554 Paragraph::size_type last = rowLastPrintable(row);
3555 Paragraph::size_type c = 0;
3556 LyXLayout const & layout =
3557 textclasslist.Style(bview->buffer()->params.textclass,
3558 row->par()->getLayout());
3559 bool left_side = false;
3561 Paragraph::size_type
3562 main_body = beginningOfMainBody(bview->buffer(), row->par());
3563 float last_tmpx = tmpx;
3565 if (main_body > 0 &&
3566 (main_body - 1 > last ||
3567 !row->par()->isLineSeparator(main_body - 1)))
3570 while (vc <= last && tmpx <= x) {
3573 if (main_body > 0 && c == main_body-1) {
3574 tmpx += fill_label_hfill +
3575 lyxfont::width(layout.labelsep,
3576 getFont(bview->buffer(), row->par(), -2));
3577 if (row->par()->isLineSeparator(main_body - 1))
3578 tmpx -= singleWidth(bview, row->par(), main_body-1);
3581 if (hfillExpansion(bview->buffer(), row, c)) {
3582 x += singleWidth(bview, row->par(), c);
3586 tmpx += fill_label_hfill;
3588 else if (row->par()->isSeparator(c)) {
3589 tmpx += singleWidth(bview, row->par(), c);
3591 tmpx+= fill_separator;
3593 tmpx += singleWidth(bview, row->par(), c);
3597 if ((tmpx + last_tmpx) / 2 > x) {
3602 if (vc > last + 1) // This shouldn't happen.
3606 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3607 // some speedup if rtl_support=false
3608 && (!row->next() || row->next()->par() != row->par());
3609 bool const rtl = (lastrow)
3610 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3611 : false; // If lastrow is false, we don't need to compute
3612 // the value of rtl.
3614 if (row->pos() > last) // Row is empty?
3617 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3618 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3620 else if (vc == row->pos()) {
3622 if (bidi_level(c) % 2 == 1)
3625 c = vis2log(vc - 1);
3626 bool const rtl = (bidi_level(c) % 2 == 1);
3627 if (left_side == rtl) {
3629 boundary = isBoundary(bview->buffer(), row->par(), c);
3633 if (row->pos() <= last && c > last
3634 && row->par()->isNewline(last)) {
3635 if (bidi_level(last) % 2 == 0)
3636 tmpx -= singleWidth(bview, row->par(), last);
3638 tmpx += singleWidth(bview, row->par(), last);
3648 // returns pointer to a specified row
3649 Row * LyXText::getRow(Paragraph * par,
3650 Paragraph::size_type pos, int & y) const
3655 Row * tmprow = firstrow;
3658 // find the first row of the specified paragraph
3659 while (tmprow->next() && tmprow->par() != par) {
3660 y += tmprow->height();
3661 tmprow = tmprow->next();
3664 // now find the wanted row
3665 while (tmprow->pos() < pos
3667 && tmprow->next()->par() == par
3668 && tmprow->next()->pos() <= pos) {
3669 y += tmprow->height();
3670 tmprow = tmprow->next();