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 "support/LAssert.h"
21 #include "support/lstrings.h"
22 #include "insets/insetbib.h"
23 #include "insets/insettext.h"
24 #include "insets/insetspecialchar.h"
25 #include "lyx_gui_misc.h"
27 #include "bufferparams.h"
36 #include "lyxscreen.h"
37 #include "bufferview_funcs.h"
38 #include "BufferView.h"
40 #include "ParagraphParameters.h"
41 #include "undo_funcs.h"
51 int const LYX_PAPER_MARGIN = 20;
55 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
58 int LyXText::workWidth(BufferView * bview) const
61 return inset_owner->textWidth(bview);
63 return bview->workWidth();
67 int LyXText::getRealCursorX(BufferView * bview) const
70 if (the_locking_inset && (the_locking_inset->getLyXText(bview)!=this))
71 x = the_locking_inset->getLyXText(bview)->getRealCursorX(bview);
76 unsigned char LyXText::transformChar(unsigned char c, Paragraph * par,
77 Paragraph::size_type pos) const
79 if (!Encodings::is_arabic(c))
80 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isdigit(c))
81 return c + (0xb0 - '0');
85 unsigned char const prev_char = pos > 0 ? par->getChar(pos-1) : ' ';
86 unsigned char next_char = ' ';
88 for (Paragraph::size_type i = pos+1; i < par->size(); ++i)
89 if (!Encodings::IsComposeChar_arabic(par->getChar(i))) {
90 next_char = par->getChar(i);
94 if (Encodings::is_arabic(next_char)) {
95 if (Encodings::is_arabic(prev_char))
96 return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
98 return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
100 if (Encodings::is_arabic(prev_char))
101 return Encodings::TransformChar(c, Encodings::FORM_FINAL);
103 return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
107 // This is the comments that some of the warnings below refers to.
108 // There are some issues in this file and I don't think they are
109 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
110 // this is a problem that has been here almost from day one and that a
111 // larger userbase with differenct access patters triggers the bad
112 // behaviour. (segfaults.) What I think happen is: In several places
113 // we store the paragraph in the current cursor and then moves the
114 // cursor. This movement of the cursor will delete paragraph at the
115 // old position if it is now empty. This will make the temporary
116 // pointer to the old cursor paragraph invalid and dangerous to use.
117 // And is some cases this will trigger a segfault. I have marked some
118 // of the cases where this happens with a warning, but I am sure there
119 // are others in this file and in text2.C. There is also a note in
120 // Delete() that you should read. In Delete I store the paragraph->id
121 // instead of a pointer to the paragraph. I am pretty sure this faulty
122 // use of temporary pointers to paragraphs that might have gotten
123 // invalidated (through a cursor movement) before they are used, are
124 // the cause of the strange crashes we get reported often.
126 // It is very tiresom to change this code, especially when it is as
127 // hard to read as it is. Help to fix all the cases where this is done
128 // would be greately appreciated.
132 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
133 Paragraph::size_type pos) const
135 char const c = par->getChar(pos);
136 return singleWidth(bview, par, pos, c);
140 int LyXText::singleWidth(BufferView * bview, Paragraph * par,
141 Paragraph::size_type pos, char c) const
143 LyXFont const font = getFont(bview->buffer(), par, pos);
145 // The most common case is handled first (Asger)
146 if (IsPrintable(c)) {
147 if (font.language()->RightToLeft()) {
148 if (font.language()->lang() == "arabic" &&
149 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
150 lyxrc.font_norm_type == LyXRC::ISO_10646_1))
151 if (Encodings::IsComposeChar_arabic(c))
154 c = transformChar(c, par, pos);
155 else if (font.language()->lang() == "hebrew" &&
156 Encodings::IsComposeChar_hebrew(c))
159 return lyxfont::width(c, font);
161 } else if (IsHfillChar(c)) {
162 return 3; /* Because of the representation
163 * as vertical lines */
164 } else if (c == Paragraph::META_INSET) {
165 Inset * tmpinset = par->getInset(pos);
167 #if 0 // seems not to be needed, but ...
168 tmpinset->update(bview, font);
170 return tmpinset->width(bview, font);
174 } else if (IsSeparatorChar(c))
176 else if (IsNewlineChar(c))
178 return lyxfont::width(c, font);
182 // Returns the paragraph position of the last character in the specified row
183 Paragraph::size_type LyXText::rowLast(Row const * row) const
185 if (row->next() == 0)
186 return row->par()->size() - 1;
187 else if (row->next()->par() != row->par())
188 return row->par()->size() - 1;
190 return row->next()->pos() - 1;
194 Paragraph::size_type LyXText::rowLastPrintable(Row const * row) const
196 Paragraph::size_type const last = rowLast(row);
197 if (last >= row->pos()
199 && row->next()->par() == row->par()
200 && row->par()->isSeparator(last))
207 void LyXText::computeBidiTables(Buffer const * buf, Row * row) const
209 bidi_same_direction = true;
210 if (!lyxrc.rtl_support) {
215 bidi_start = row->pos();
216 bidi_end = rowLastPrintable(row);
218 if (bidi_start > bidi_end) {
223 if (bidi_end + 2 - bidi_start >
224 static_cast<Paragraph::size_type>(log2vis_list.size())) {
225 Paragraph::size_type new_size =
226 (bidi_end + 2 - bidi_start < 500) ?
227 500 : 2 * (bidi_end + 2 - bidi_start);
228 log2vis_list.resize(new_size);
229 vis2log_list.resize(new_size);
230 bidi_levels.resize(new_size);
233 vis2log_list[bidi_end + 1 - bidi_start] = -1;
234 log2vis_list[bidi_end + 1 - bidi_start] = -1;
236 Paragraph::size_type stack[2];
238 row->par()->getParLanguage(buf->params)->RightToLeft();
242 Paragraph::size_type const main_body =
243 beginningOfMainBody(buf, row->par());
245 for (Paragraph::size_type lpos = bidi_start;
246 lpos <= bidi_end; ++lpos) {
247 bool is_space = row->par()->isLineSeparator(lpos);
248 Paragraph::size_type const pos =
249 (is_space && lpos + 1 <= bidi_end &&
250 !row->par()->isLineSeparator(lpos + 1) &&
251 !row->par()->isNewline(lpos + 1))
253 LyXFont font = row->par()->getFontSettings(buf->params, pos);
254 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
255 font.number() == LyXFont::ON &&
256 row->par()->getFontSettings(buf->params, lpos-1).number()
258 font = row->par()->getFontSettings(buf->params, lpos);
263 bool new_rtl = font.isVisibleRightToLeft();
264 bool new_rtl0 = font.isRightToLeft();
267 if (lpos == main_body - 1
268 && row->pos() < main_body - 1
270 new_level = (rtl_par) ? 1 : 0;
271 new_rtl = new_rtl0 = rtl_par;
273 new_level = (new_rtl) ? 1 : 2;
275 new_level = (rtl_par) ? 2 : 0;
277 if (is_space && new_level >= level) {
283 int new_level2 = new_level;
285 if (level == new_level && rtl0 != new_rtl0) {
287 log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
288 } else if (level < new_level) {
289 log2vis_list[lpos - bidi_start] = (rtl) ? -1 : 1;
290 if (new_level > rtl_par)
291 bidi_same_direction = false;
293 log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
296 bidi_levels[lpos - bidi_start] = new_level;
298 while (level > new_level2) {
299 Paragraph::size_type old_lpos =
301 int delta = lpos - old_lpos - 1;
304 log2vis_list[lpos - bidi_start] += delta;
305 log2vis_list[old_lpos - bidi_start] += delta;
307 while (level < new_level)
308 stack[level++] = lpos;
312 Paragraph::size_type const old_lpos = stack[--level];
313 int delta = bidi_end - old_lpos;
316 log2vis_list[old_lpos - bidi_start] += delta;
319 Paragraph::size_type vpos = bidi_start - 1;
320 for (Paragraph::size_type lpos = bidi_start;
321 lpos <= bidi_end; ++lpos) {
322 vpos += log2vis_list[lpos - bidi_start];
323 vis2log_list[vpos - bidi_start] = lpos;
324 log2vis_list[lpos - bidi_start] = vpos;
329 // This method requires a previous call to ComputeBidiTables()
330 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
331 Paragraph::size_type pos) const
333 if (!lyxrc.rtl_support || pos == 0)
336 if (!bidi_InRange(pos - 1)) {
337 /// This can happen if pos is the first char of a row.
338 /// Returning false in this case is incorrect!
342 bool const rtl = bidi_level(pos - 1) % 2;
343 bool const rtl2 = bidi_InRange(pos)
344 ? bidi_level(pos) % 2
345 : par->isRightToLeftPar(buf->params);
350 bool LyXText::isBoundary(Buffer const * buf, Paragraph * par,
351 Paragraph::size_type pos,
352 LyXFont const & font) const
354 if (!lyxrc.rtl_support)
355 return false; // This is just for speedup
357 bool const rtl = font.isVisibleRightToLeft();
358 bool const rtl2 = bidi_InRange(pos)
359 ? bidi_level(pos) % 2
360 : par->isRightToLeftPar(buf->params);
365 void LyXText::draw(BufferView * bview, Row const * row,
366 Paragraph::size_type & vpos,
367 int offset, float & x, bool cleared)
369 Painter & pain = bview->painter();
371 Paragraph::size_type pos = vis2log(vpos);
372 char c = row->par()->getChar(pos);
375 if (IsNewlineChar(c)) {
377 // Draw end-of-line marker
378 LyXFont const font = getFont(bview->buffer(), row->par(), pos);
379 int const wid = lyxfont::width('n', font);
380 int const asc = lyxfont::maxAscent(font);
381 int const y = offset + row->baseline();
385 if (bidi_level(pos) % 2 == 0) {
386 xp[0] = int(x + wid * 0.375);
387 yp[0] = int(y - 0.875 * asc * 0.75);
390 yp[1] = int(y - 0.500 * asc * 0.75);
392 xp[2] = int(x + wid * 0.375);
393 yp[2] = int(y - 0.125 * asc * 0.75);
395 pain.lines(xp, yp, 3, LColor::eolmarker);
398 yp[0] = int(y - 0.500 * asc * 0.75);
400 xp[1] = int(x + wid);
401 yp[1] = int(y - 0.500 * asc * 0.75);
403 xp[2] = int(x + wid);
404 yp[2] = int(y - asc * 0.75);
406 pain.lines(xp, yp, 3, LColor::eolmarker);
408 xp[0] = int(x + wid * 0.625);
409 yp[0] = int(y - 0.875 * asc * 0.75);
411 xp[1] = int(x + wid);
412 yp[1] = int(y - 0.500 * asc * 0.75);
414 xp[2] = int(x + wid * 0.625);
415 yp[2] = int(y - 0.125 * asc * 0.75);
417 pain.lines(xp, yp, 3, LColor::eolmarker);
419 xp[0] = int(x + wid);
420 yp[0] = int(y - 0.500 * asc * 0.75);
423 yp[1] = int(y - 0.500 * asc * 0.75);
426 yp[2] = int(y - asc * 0.75);
428 pain.lines(xp, yp, 3, LColor::eolmarker);
434 LyXFont font = getFont(bview->buffer(), row->par(), pos);
435 LyXFont font2 = font;
437 if (c == Paragraph::META_INSET) {
438 Inset * tmpinset = row->par()->getInset(pos);
440 tmpinset->update(bview, font, false);
441 tmpinset->draw(bview, font, offset+row->baseline(), x,
443 #ifdef SEEMS_TO_BE_NOT_NEEDED
444 if (status_ == CHANGED_IN_DRAW) {
445 UpdateInset(bview, tmpinset);
446 status(bview, CHANGED_IN_DRAW);
452 if (lyxrc.mark_foreign_language &&
453 font.language() != ignore_language &&
454 font.language() != bview->buffer()->params.language) {
455 int const y = offset + row->height() - 1;
456 pain.line(int(tmpx), y, int(x), y, LColor::language);
462 /* usual characters, no insets */
464 // Collect character that we can draw in one command
466 // This is dirty, but fast. Notice that it will never be too small.
467 // For the record, I'll note that Microsoft Word has a limit
468 // of 768 here. We have none :-) (Asger)
469 // Ok. I am the first to admit that the use of std::string will be
470 // a tiny bit slower than using a POD char array. However, I claim
471 // that this slowdown is so small that it is close to inperceptive.
472 // So IMHO we should go with the easier and clearer implementation.
473 // And even if 1024 is a large number here it might overflow, string
474 // will only overflow if the machine is out of memory...
475 static string textstring;
479 Paragraph::size_type const last = rowLastPrintable(row);
481 if (font.language()->lang() == "hebrew") {
482 if (Encodings::IsComposeChar_hebrew(c)) {
483 int const width = lyxfont::width(c, font2);
485 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
486 c = row->par()->getChar(i);
487 if (!Encodings::IsComposeChar_hebrew(c)) {
488 if (IsPrintableNonspace(c)) {
493 dx = (c == 'ø' || c == 'ã') // dalet / resh
494 ? width2 - width : (width2 - width) / 2;
500 pain.text(int(x) + dx, offset + row->baseline(),
503 while (vpos <= last &&
504 (pos = vis2log(vpos)) >= 0
505 && IsPrintableNonspace(c = row->par()->getChar(pos))
506 && !Encodings::IsComposeChar_hebrew(c)
507 && font2 == getFont(bview->buffer(), row->par(), pos)) {
511 // Draw text and set the new x position
512 pain.text(int(x), offset + row->baseline(),
514 x += lyxfont::width(textstring, font);
516 } else if (font.language()->lang() == "arabic" &&
517 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
518 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
519 if (Encodings::IsComposeChar_arabic(c)) {
520 c = transformChar(c, row->par(), pos);
522 int const width = lyxfont::width(c, font2);
524 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
525 c = row->par()->getChar(i);
526 if (!Encodings::IsComposeChar_arabic(c)) {
527 if (IsPrintableNonspace(c)) {
532 dx = (width2 - width) / 2;
538 pain.text(int(x) + dx, offset + row->baseline(),
541 textstring = transformChar(c, row->par(), pos);
542 while (vpos <= last &&
543 (pos = vis2log(vpos)) >= 0
544 && IsPrintableNonspace(c = row->par()->getChar(pos))
545 && !Encodings::IsComposeChar_arabic(c)
546 && font2 == getFont(bview->buffer(), row->par(), pos)) {
547 c = transformChar(c, row->par(), pos);
551 // Draw text and set the new x position
552 pain.text(int(x), offset + row->baseline(),
554 x += lyxfont::width(textstring, font);
557 while (vpos <= last &&
558 (pos = vis2log(vpos)) >= 0
559 && IsPrintableNonspace(c = row->par()->getChar(pos))
560 && font2 == getFont(bview->buffer(), row->par(), pos)) {
564 // Draw text and set the new x position
565 pain.text(int(x), offset + row->baseline(), textstring, font);
566 x += lyxfont::width(textstring, font);
570 if ((font.language() == inherit_language) ||
571 (font.language() == ignore_language))
572 lyxerr << "No this shouldn't happen!\n";
574 if (lyxrc.mark_foreign_language &&
575 font.language() != bview->buffer()->params.language) {
576 int const y = offset + row->height() - 1;
577 pain.line(int(tmpx), y, int(x), y,
581 // If we want ulem.sty support, drawing
582 // routines should go here. (Asger)
583 // Why shouldn't LyXFont::drawText handle it internally?
587 // Returns the left beginning of the text.
588 // This information cannot be taken from the layouts-objekt, because in
589 // LaTeX the beginning of the text fits in some cases (for example sections)
590 // exactly the label-width.
591 int LyXText::leftMargin(BufferView * bview, Row const * row) const
593 LyXLayout const & layout =
594 textclasslist.Style(bview->buffer()->params.textclass,
595 row->par()->getLayout());
597 string parindent = layout.parindent;
599 int x = LYX_PAPER_MARGIN;
601 x += lyxfont::signedWidth(textclasslist
602 .TextClass(bview->buffer()->params.textclass)
605 .TextClass(bview->buffer()->params.textclass)
608 // this is the way, LyX handles the LaTeX-Environments.
609 // I have had this idea very late, so it seems to be a
610 // later added hack and this is true
611 if (!row->par()->getDepth()) {
612 if (!row->par()->getLayout()) {
613 // find the previous same level paragraph
614 if (row->par()->previous()) {
615 Paragraph * newpar = row->par()
616 ->depthHook(row->par()->getDepth());
618 textclasslist.Style(bview->buffer()->params.textclass,
625 // find the next level paragraph
628 row->par()->outerHook();
630 // make a corresponding row. Needed to call LeftMargin()
632 // check wether it is a sufficent paragraph
635 .Style(bview->buffer()->params.textclass,
636 newpar->getLayout()).isEnvironment()) {
638 dummyrow.par(newpar);
639 dummyrow.pos(newpar->size());
640 x = leftMargin(bview, &dummyrow);
642 // this is no longer an error, because this function
643 // is used to clear impossible depths after changing
644 // a layout. Since there is always a redo,
645 // LeftMargin() is always called
646 row->par()->params().depth(0);
649 if (newpar && !row->par()->getLayout()) {
650 if (newpar->params().noindent())
653 parindent = textclasslist
654 .Style(bview->buffer()->params.textclass,
655 newpar->getLayout()).parindent;
660 LyXFont const labelfont = getFont(bview->buffer(), row->par(), -2);
661 switch (layout.margintype) {
663 if (!layout.leftmargin.empty()) {
664 x += lyxfont::signedWidth(layout.leftmargin,
666 .TextClass(bview->buffer()->params.
670 if (!row->par()->getLabelstring().empty()) {
671 x += lyxfont::signedWidth(layout.labelindent,
673 x += lyxfont::width(row->par()->getLabelstring(),
675 x += lyxfont::width(layout.labelsep, labelfont);
679 x += lyxfont::signedWidth(layout.labelindent, labelfont);
680 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
681 if (!row->par()->getLabelWidthString().empty()) {
682 x += lyxfont::width(row->par()->getLabelWidthString(),
684 x += lyxfont::width(layout.labelsep, labelfont);
689 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
690 / (row->par()->getDepth() + 4);
692 case MARGIN_FIRST_DYNAMIC:
693 if (layout.labeltype == LABEL_MANUAL) {
694 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
695 x += lyxfont::signedWidth(layout.leftmargin,
698 x += lyxfont::signedWidth(layout.labelindent,
701 } else if (row->pos()
702 // Special case to fix problems with
704 || (layout.labeltype == LABEL_STATIC
705 && layout.latextype == LATEX_ENVIRONMENT
706 && ! row->par()->isFirstInSequence())) {
707 x += lyxfont::signedWidth(layout.leftmargin,
709 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
710 && layout.labeltype != LABEL_BIBLIO
711 && layout.labeltype !=
712 LABEL_CENTERED_TOP_ENVIRONMENT) {
713 x += lyxfont::signedWidth(layout.labelindent,
715 x += lyxfont::width(layout.labelsep, labelfont);
716 x += lyxfont::width(row->par()->getLabelstring(),
721 case MARGIN_RIGHT_ADDRESS_BOX:
723 // ok, a terrible hack. The left margin depends on the widest
724 // row in this paragraph. Do not care about footnotes, they
725 // are *NOT* allowed in the LaTeX realisation of this layout.
727 // find the first row of this paragraph
728 Row const * tmprow = row;
729 while (tmprow->previous()
730 && tmprow->previous()->par() == row->par())
731 tmprow = tmprow->previous();
733 int minfill = tmprow->fill();
734 while (tmprow->next() && tmprow->next()->par() == row->par()) {
735 tmprow = tmprow->next();
736 if (tmprow->fill() < minfill)
737 minfill = tmprow->fill();
740 x += lyxfont::signedWidth(layout.leftmargin,
742 .TextClass(bview->buffer()->params.textclass)
749 int align; // wrong type
751 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
752 align = layout.align;
754 align = row->par()->params().align();
756 // set the correct parindent
757 if (row->pos() == 0) {
758 if ((layout.labeltype == LABEL_NO_LABEL
759 || layout.labeltype == LABEL_TOP_ENVIRONMENT
760 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
761 || (layout.labeltype == LABEL_STATIC
762 && layout.latextype == LATEX_ENVIRONMENT
763 && ! row->par()->isFirstInSequence()))
764 && align == LYX_ALIGN_BLOCK
765 && !row->par()->params().noindent()
766 && (row->par()->layout ||
767 bview->buffer()->params.paragraph_separation ==
768 BufferParams::PARSEP_INDENT))
769 x += lyxfont::signedWidth(parindent,
771 .TextClass(bview->buffer()->params
774 else if (layout.labeltype == LABEL_BIBLIO) {
775 // ale970405 Right width for bibitems
776 x += bibitemMaxWidth(bview,textclasslist
777 .TextClass(bview->buffer()->params
786 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
788 LyXLayout const & layout =
789 textclasslist.Style(buf->params.textclass,
790 row->par()->getLayout());
792 int x = LYX_PAPER_MARGIN
793 + lyxfont::signedWidth(textclasslist
794 .TextClass(buf->params.textclass)
797 .TextClass(buf->params.textclass)
800 // this is the way, LyX handles the LaTeX-Environments.
801 // I have had this idea very late, so it seems to be a
802 // later added hack and this is true
803 if (row->par()->getDepth()) {
804 // find the next level paragraph
806 Paragraph * newpar = row->par();
809 newpar = newpar->previous();
811 && newpar->getDepth() >= row->par()->getDepth());
813 // make a corresponding row. Needed to call LeftMargin()
815 // check wether it is a sufficent paragraph
817 && textclasslist.Style(buf->params.textclass,
821 dummyrow.par(newpar);
823 x = rightMargin(buf, &dummyrow);
825 // this is no longer an error, because this function
826 // is used to clear impossible depths after changing
827 // a layout. Since there is always a redo,
828 // LeftMargin() is always called
829 row->par()->params().depth(0);
833 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
834 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
835 .TextClass(buf->params.textclass)
836 .defaultfont()) * 4 / (row->par()->getDepth()
842 int LyXText::labelEnd(BufferView * bview, Row const * row) const
844 if (textclasslist.Style(bview->buffer()->params.textclass,
845 row->par()->getLayout()).margintype
849 tmprow.pos(row->par()->size());
850 return leftMargin(bview, &tmprow); /* just the beginning
853 return 0; /* LabelEnd is only needed, if the
854 layout fills a flushleft
859 // get the next breakpoint in a given paragraph
861 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
863 Paragraph * par = row->par();
868 Paragraph::size_type const pos = row->pos();
871 // position of the last possible breakpoint
872 // -1 isn't a suitable value, but a flag
873 Paragraph::size_type last_separator = -1;
874 width -= rightMargin(bview->buffer(), row);
876 Paragraph::size_type const main_body =
877 beginningOfMainBody(bview->buffer(), par);
878 LyXLayout const & layout =
879 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
880 Paragraph::size_type i = pos;
882 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
883 /* special code for right address boxes, only newlines count */
884 while (i < par->size()) {
885 if (par->isNewline(i)) {
887 i = par->size() - 1; // this means break
889 } else if (par->getChar(i) == Paragraph::META_INSET &&
890 par->getInset(i) && par->getInset(i)->display()){
891 par->getInset(i)->display(false);
896 // Last position is an invariant
897 Paragraph::size_type const last =
899 // this is the usual handling
900 int x = leftMargin(bview, row);
901 bool doitonetime = true;
902 while (doitonetime || ((x < width) && (i < last))) {
904 char const c = par->getChar(i);
905 if (IsNewlineChar(c)) {
907 x = width; // this means break
908 } else if (c == Paragraph::META_INSET &&
911 // check wether a Display() inset is
912 // valid here. if not, change it to
914 if (par->getInset(i)->display() &&
915 (layout.isCommand() ||
916 (layout.labeltype == LABEL_MANUAL
917 && i < beginningOfMainBody(bview->buffer(), par)))) {
918 // display istn't allowd
919 par->getInset(i)->display(false);
920 x += singleWidth(bview, par, i, c);
921 } else if (par->getInset(i)->display() ||
922 par->getInset(i)->needFullRow()) {
923 // So break the line here
927 if (IsLineSeparatorChar(par->getChar(i+1)))
930 last_separator = last; // to avoid extra rows
932 last_separator = i - 1;
933 x = width; // this means break
935 x += singleWidth(bview, par, i, c);
938 if (IsLineSeparatorChar(c))
940 x += singleWidth(bview, par, i, c);
943 if (i == main_body) {
944 x += lyxfont::width(layout.labelsep,
945 getFont(bview->buffer(), par, -2));
946 if (par->isLineSeparator(i - 1))
947 x-= singleWidth(bview, par, i - 1);
948 int left_margin = labelEnd(bview, row);
953 // end of paragraph is always a suitable separator
954 if (i == last && x < width)
958 // well, if last_separator is still 0, the line isn't breakable.
959 // don't care and cut simply at the end
960 if (last_separator < 0) {
964 // manual labels cannot be broken in LaTeX, do not care
965 if (main_body && last_separator < main_body)
966 last_separator = main_body - 1;
968 return last_separator;
972 // returns the minimum space a row needs on the screen in pixel
973 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
979 // get the pure distance
980 Paragraph::size_type const last = rowLastPrintable(row);
982 // special handling of the right address boxes
983 if (textclasslist.Style(bview->buffer()->params.textclass,
984 row->par()->getLayout()).margintype
985 == MARGIN_RIGHT_ADDRESS_BOX) {
986 int const tmpfill = row->fill();
987 row->fill(0); // the minfill in MarginLeft()
988 w = leftMargin(bview, row);
991 w = leftMargin(bview, row);
993 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
994 row->par()->getLayout());
995 Paragraph::size_type const main_body =
996 beginningOfMainBody(bview->buffer(), row->par());
997 Paragraph::size_type i = row->pos();
1000 if (main_body > 0 && i == main_body) {
1001 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
1002 if (row->par()->isLineSeparator(i - 1))
1003 w -= singleWidth(bview, row->par(), i - 1);
1004 int left_margin = labelEnd(bview, row);
1005 if (w < left_margin)
1008 w += singleWidth(bview, row->par(), i);
1011 if (main_body > 0 && main_body > last) {
1012 w += lyxfont::width(layout.labelsep, getFont(bview->buffer(), row->par(), -2));
1013 if (last >= 0 && row->par()->isLineSeparator(last))
1014 w -= singleWidth(bview, row->par(), last);
1015 int const left_margin = labelEnd(bview, row);
1016 if (w < left_margin)
1020 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1021 #ifdef WITH_WARNINGS
1022 #warning Please fix me (Jug!)
1032 // returns the minimum space a manual label needs on the screen in pixel
1033 int LyXText::labelFill(BufferView * bview, Row const * row) const
1035 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1036 // -1 because a label ends either with a space that is in the label,
1037 // or with the beginning of a footnote that is outside the label.
1039 // I don't understand this code in depth, but sometimes "last" is
1040 // less than 0 and this causes a crash. This fix seems to work
1041 // correctly, but I bet the real error is elsewhere. The bug is
1042 // triggered when you have an open footnote in a paragraph
1043 // environment with a manual label. (Asger)
1044 if (last < 0) last = 0;
1046 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1053 w += singleWidth(bview, row->par(), i);
1058 if (!row->par()->params().labelWidthString().empty()) {
1059 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1060 getFont(bview->buffer(), row->par(), -2)) - w,
1068 // returns the number of separators in the specified row. The separator
1069 // on the very last column doesnt count
1070 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1072 Paragraph::size_type const last = rowLast(row);
1073 Paragraph::size_type p =
1074 max(row->pos(), beginningOfMainBody(buf, row->par()));
1076 for (; p < last; ++p) {
1077 if (row->par()->isSeparator(p)) {
1085 // returns the number of hfills in the specified row. The LyX-Hfill is
1086 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1087 // ignored. This is *MUCH* more usefull than not to ignore!
1088 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1090 Paragraph::size_type const last = rowLast(row);
1091 Paragraph::size_type first = row->pos();
1092 if (first) { /* hfill *DO* count at the beginning
1094 while(first <= last && row->par()->isHfill(first))
1098 first = max(first, beginningOfMainBody(buf, row->par()));
1100 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1101 if (row->par()->isHfill(p)) {
1109 // like NumberOfHfills, but only those in the manual label!
1110 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1112 Paragraph::size_type last = rowLast(row);
1113 Paragraph::size_type first = row->pos();
1114 if (first) { /* hfill *DO* count at the beginning
1116 while(first < last && row->par()->isHfill(first))
1120 last = min(last, beginningOfMainBody(buf, row->par()));
1122 for (Paragraph::size_type p = first;
1123 p < last; ++p) { // last, because the end is ignored!
1124 if (row->par()->isHfill(p)) {
1132 // returns true, if a expansion is needed.
1133 // Rules are given by LaTeX
1134 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1135 Paragraph::size_type pos) const
1137 // by the way, is it a hfill?
1138 if (!row_ptr->par()->isHfill(pos))
1141 // at the end of a row it does not count
1142 if (pos >= rowLast(row_ptr))
1145 // at the beginning of a row it does not count, if it is not
1146 // the first row of a paragaph
1147 if (!row_ptr->pos())
1150 // in some labels it does not count
1151 if (textclasslist.Style(buf->params.textclass,
1152 row_ptr->par()->getLayout()).margintype
1154 && pos < beginningOfMainBody(buf, row_ptr->par()))
1157 // if there is anything between the first char of the row and
1158 // the sepcified position that is not a newline and not a hfill,
1159 // the hfill will count, otherwise not
1160 Paragraph::size_type i = row_ptr->pos();
1161 while (i < pos && (row_ptr->par()->isNewline(i)
1162 || row_ptr->par()->isHfill(i)))
1169 LColor::color LyXText::backgroundColor()
1172 return inset_owner->backgroundColor();
1174 return LColor::background;
1177 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1179 /* get the maximum ascent and the maximum descent */
1182 float layoutasc = 0;
1183 float layoutdesc = 0;
1186 Inset * tmpinset = 0;
1188 /* this must not happen before the currentrow for clear reasons.
1189 so the trick is just to set the current row onto this row */
1191 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1193 /* ok , let us initialize the maxasc and maxdesc value.
1194 * This depends in LaTeX of the font of the last character
1195 * in the paragraph. The hack below is necessary because
1196 * of the possibility of open footnotes */
1198 /* Correction: only the fontsize count. The other properties
1199 are taken from the layoutfont. Nicer on the screen :) */
1200 Paragraph * par = row_ptr->par();
1201 Paragraph * firstpar = row_ptr->par();
1203 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1204 firstpar->getLayout());
1206 LyXFont font = getFont(bview->buffer(), par, par->size() - 1);
1207 LyXFont::FONT_SIZE const size = font.size();
1208 font = getFont(bview->buffer(), par, -1);
1211 LyXFont labelfont = getFont(bview->buffer(), par, -2);
1213 float spacing_val = 1.0;
1214 if (!row_ptr->par()->params().spacing().isDefault()) {
1215 spacing_val = row_ptr->par()->params().spacing().getValue();
1217 spacing_val = bview->buffer()->params.spacing.getValue();
1219 //lyxerr << "spacing_val = " << spacing_val << endl;
1221 int maxasc = int(lyxfont::maxAscent(font) *
1222 layout.spacing.getValue() *
1224 int maxdesc = int(lyxfont::maxDescent(font) *
1225 layout.spacing.getValue() *
1227 int const pos_end = rowLast(row_ptr);
1231 // Check if any insets are larger
1232 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1233 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1234 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1235 tmpinset = row_ptr->par()->getInset(pos);
1237 #if 1 // this is needed for deep update on initialitation
1238 tmpinset->update(bview, tmpfont);
1240 asc = tmpinset->ascent(bview, tmpfont);
1241 desc = tmpinset->descent(bview, tmpfont);
1242 maxwidth += tmpinset->width(bview, tmpfont);
1243 maxasc = max(maxasc, asc);
1244 maxdesc = max(maxdesc, desc);
1247 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1251 // Check if any custom fonts are larger (Asger)
1252 // This is not completely correct, but we can live with the small,
1253 // cosmetic error for now.
1254 LyXFont::FONT_SIZE const maxsize =
1255 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end);
1256 if (maxsize > font.size()) {
1257 font.setSize(maxsize);
1259 asc = lyxfont::maxAscent(font);
1260 desc = lyxfont::maxDescent(font);
1267 // This is nicer with box insets:
1271 row_ptr->ascent_of_text(maxasc);
1273 // is it a top line?
1274 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1276 // some parksips VERY EASY IMPLEMENTATION
1277 if (bview->buffer()->params.paragraph_separation ==
1278 BufferParams::PARSEP_SKIP)
1280 if (layout.isParagraph()
1281 && firstpar->getDepth() == 0
1282 && firstpar->previous())
1284 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1285 } else if (firstpar->previous() &&
1286 textclasslist.Style(bview->buffer()->params.textclass,
1287 firstpar->previous()->
1288 getLayout()).isParagraph() &&
1289 firstpar->previous()->getDepth() == 0)
1291 // is it right to use defskip here too? (AS)
1292 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1296 // the paper margins
1297 if (!row_ptr->par()->previous() && bv_owner)
1298 maxasc += LYX_PAPER_MARGIN;
1300 // add the vertical spaces, that the user added
1301 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1302 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1304 // do not forget the DTP-lines!
1305 // there height depends on the font of the nearest character
1306 if (firstpar->params().lineTop())
1307 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1310 // and now the pagebreaks
1311 if (firstpar->params().pagebreakTop())
1312 maxasc += 3 * defaultHeight();
1314 // This is special code for the chapter, since the label of this
1315 // layout is printed in an extra row
1316 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1317 && bview->buffer()->params.secnumdepth >= 0)
1319 float spacing_val = 1.0;
1320 if (!row_ptr->par()->params().spacing().isDefault()) {
1321 spacing_val = row_ptr->par()->params().spacing().getValue();
1323 spacing_val = bview->buffer()->params.spacing.getValue();
1326 labeladdon = int(lyxfont::maxDescent(labelfont) *
1327 layout.spacing.getValue() *
1329 + int(lyxfont::maxAscent(labelfont) *
1330 layout.spacing.getValue() *
1334 // special code for the top label
1335 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1336 || layout.labeltype == LABEL_BIBLIO
1337 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1338 && row_ptr->par()->isFirstInSequence()
1339 && !row_ptr->par()->getLabelstring().empty())
1341 float spacing_val = 1.0;
1342 if (!row_ptr->par()->params().spacing().isDefault()) {
1343 spacing_val = row_ptr->par()->params().spacing().getValue();
1345 spacing_val = bview->buffer()->params.spacing.getValue();
1349 (lyxfont::maxAscent(labelfont) *
1350 layout.spacing.getValue() *
1352 +(lyxfont::maxDescent(labelfont) *
1353 layout.spacing.getValue() *
1355 + layout.topsep * defaultHeight()
1356 + layout.labelbottomsep * defaultHeight());
1359 // and now the layout spaces, for example before and after a section,
1360 // or between the items of a itemize or enumerate environment
1362 if (!firstpar->params().pagebreakTop()) {
1363 Paragraph * prev = row_ptr->par()->previous();
1365 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1366 if (prev && prev->getLayout() == firstpar->getLayout() &&
1367 prev->getDepth() == firstpar->getDepth() &&
1368 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1370 layoutasc = (layout.itemsep * defaultHeight());
1371 } else if (row_ptr->previous()) {
1372 tmptop = layout.topsep;
1374 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1375 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1376 row_ptr->previous()->par()->
1377 getLayout()).bottomsep;
1380 layoutasc = (tmptop * defaultHeight());
1381 } else if (row_ptr->par()->params().lineTop()) {
1382 tmptop = layout.topsep;
1385 layoutasc = (tmptop * defaultHeight());
1388 prev = row_ptr->par()->outerHook();
1390 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1391 prev->getLayout()).parsep * defaultHeight());
1393 if (firstpar->previous() &&
1394 firstpar->previous()->getDepth() == 0 &&
1395 firstpar->previous()->getLayout() !=
1396 firstpar->getLayout())
1399 } else if (firstpar->previous()) {
1400 maxasc += int(layout.parsep * defaultHeight());
1406 // is it a bottom line?
1407 if (row_ptr->par() == par
1408 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1410 // the paper margins
1411 if (!par->next() && bv_owner)
1412 maxdesc += LYX_PAPER_MARGIN;
1414 // add the vertical spaces, that the user added
1415 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1416 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1418 // do not forget the DTP-lines!
1419 // there height depends on the font of the nearest character
1420 if (firstpar->params().lineBottom())
1421 maxdesc += 2 * lyxfont::ascent('x',
1422 getFont(bview->buffer(),
1423 par, par->size() - 1));
1425 // and now the pagebreaks
1426 if (firstpar->params().pagebreakBottom())
1427 maxdesc += 3 * defaultHeight();
1429 // and now the layout spaces, for example before and after
1430 // a section, or between the items of a itemize or enumerate
1432 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1433 Paragraph * nextpar = row_ptr->par()->next();
1434 Paragraph * comparepar = row_ptr->par();
1438 if (comparepar->getDepth() > nextpar->getDepth()) {
1439 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1440 comparepar->getLayout()).bottomsep * defaultHeight());
1441 comparepar = comparepar->depthHook(nextpar->getDepth());
1442 if (comparepar->getLayout()!= nextpar->getLayout()
1443 || nextpar->getLabelWidthString() !=
1444 comparepar->getLabelWidthString())
1446 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1447 comparepar->getLayout()).bottomsep * defaultHeight());
1449 if (unusual > usual)
1450 layoutdesc = unusual;
1453 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1455 if (comparepar->getLayout()!= nextpar->getLayout()
1456 || nextpar->getLabelWidthString() !=
1457 comparepar->getLabelWidthString())
1458 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1459 comparepar->getLayout()).bottomsep * defaultHeight());
1464 // incalculate the layout spaces
1465 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1466 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1468 // calculate the new height of the text
1469 height -= row_ptr->height();
1471 row_ptr->height(maxasc + maxdesc + labeladdon);
1472 row_ptr->baseline(maxasc + labeladdon);
1474 height += row_ptr->height();
1477 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1478 row_ptr->width(int(maxwidth + x));
1481 width = max(0,workWidth(bview));
1483 if (r->width() > width)
1491 /* Appends the implicit specified paragraph behind the specified row,
1492 * start at the implicit given position */
1493 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1495 bool not_ready = true;
1497 // The last character position of a paragraph is an invariant so we can
1498 // safely get it here. (Asger)
1499 int const lastposition = row->par()->size();
1501 // Get the next breakpoint
1502 int z = nextBreakPoint(bview, row, workWidth(bview));
1506 // Insert the new row
1507 if (z < lastposition) {
1509 insertRow(row, row->par(), z);
1516 // Set the dimensions of the row
1517 #ifdef WITH_WARNINGS
1518 #warning Something is rotten here! (Jug)
1520 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1521 setHeightOfRow(bview, tmprow);
1523 } while (not_ready);
1527 void LyXText::breakAgain(BufferView * bview, Row * row) const
1529 bool not_ready = true;
1532 // get the next breakpoint
1533 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1536 if (z < row->par()->size()) {
1537 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1540 insertRow(row, row->par(), z);
1546 if (row->pos() == z)
1547 not_ready = false; // the rest will not change
1553 /* if there are some rows too much, delete them */
1554 /* only if you broke the whole paragraph! */
1555 Row * tmprow2 = row;
1556 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1557 tmprow2 = tmprow2->next();
1559 while (tmprow2 != row) {
1560 tmprow2 = tmprow2->previous();
1561 removeRow(tmprow2->next());
1566 /* set the dimensions of the row */
1567 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1568 setHeightOfRow(bview, tmprow);
1569 } while (not_ready);
1573 // this is just a little changed version of break again
1574 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1576 // get the next breakpoint
1577 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1580 if (z < row->par()->size()) {
1582 || (row->next() && row->next()->par() != row->par())) {
1583 /* insert a new row */
1585 insertRow(row, row->par(), z);
1591 if (row->pos() != z)
1595 // if there are some rows too much, delete them
1596 // only if you broke the whole paragraph!
1597 Row * tmprow2 = row;
1598 while (tmprow2->next()
1599 && tmprow2->next()->par() == row->par()) {
1600 tmprow2 = tmprow2->next();
1602 while (tmprow2 != row) {
1603 tmprow2 = tmprow2->previous();
1604 removeRow(tmprow2->next());
1608 // set the dimensions of the row
1609 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1610 setHeightOfRow(bview, tmprow);
1614 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1616 LyXLayout const & layout =
1617 textclasslist.Style(bview->buffer()->params.textclass,
1618 cursor.par()->getLayout());
1620 // this is only allowed, if the current paragraph is not empty or caption
1621 if ((cursor.par()->size() <= 0)
1622 && layout.labeltype!= LABEL_SENSITIVE)
1625 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1627 // Always break behind a space
1629 // It is better to erase the space (Dekel)
1630 if (cursor.pos() < cursor.par()->size()
1631 && cursor.par()->isLineSeparator(cursor.pos()))
1632 cursor.par()->erase(cursor.pos());
1633 // cursor.pos(cursor.pos() + 1);
1635 // break the paragraph
1639 keep_layout = layout.isEnvironment();
1640 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1643 // well this is the caption hack since one caption is really enough
1644 if (layout.labeltype == LABEL_SENSITIVE) {
1646 // set to standard-layout
1647 cursor.par()->setLayout(0);
1649 // set to standard-layout
1650 cursor.par()->next()->setLayout(0);
1653 /* if the cursor is at the beginning of a row without prior newline,
1655 * This touches only the screen-update. Otherwise we would may have
1656 * an empty row on the screen */
1657 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1658 && cursor.row()->pos() == cursor.pos()) {
1662 status(bview, LyXText::NEED_MORE_REFRESH);
1663 refresh_row = cursor.row();
1664 refresh_y = cursor.y() - cursor.row()->baseline();
1666 // Do not forget the special right address boxes
1667 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1668 while (refresh_row->previous() &&
1669 refresh_row->previous()->par() == refresh_row->par()) {
1670 refresh_row = refresh_row->previous();
1671 refresh_y -= refresh_row->height();
1674 removeParagraph(cursor.row());
1676 // set the dimensions of the cursor row
1677 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1679 setHeightOfRow(bview, cursor.row());
1681 while (cursor.par()->next()->size()
1682 && cursor.par()->next()->isNewline(0))
1683 cursor.par()->next()->erase(0);
1685 insertParagraph(bview, cursor.par()->next(), cursor.row());
1687 updateCounters(bview, cursor.row()->previous());
1689 /* This check is necessary. Otherwise the new empty paragraph will
1690 * be deleted automatically. And it is more friendly for the user! */
1692 setCursor(bview, cursor.par()->next(), 0);
1694 setCursor(bview, cursor.par(), 0);
1696 if (cursor.row()->next())
1697 breakAgain(bview, cursor.row()->next());
1703 // Just a macro to make some thing easier.
1704 void LyXText::redoParagraph(BufferView * bview) const
1706 clearSelection(bview);
1707 redoParagraphs(bview, cursor, cursor.par()->next());
1708 setCursorIntern(bview, cursor.par(), cursor.pos());
1712 /* insert a character, moves all the following breaks in the
1713 * same Paragraph one to the right and make a rebreak */
1714 void LyXText::insertChar(BufferView * bview, char c)
1716 setUndo(bview, Undo::INSERT,
1717 cursor.par(), cursor.par()->next());
1719 // When the free-spacing option is set for the current layout,
1720 // disable the double-space checking
1722 bool const freeSpacing =
1723 textclasslist.Style(bview->buffer()->params.textclass,
1724 cursor.row()->par()->getLayout()).free_spacing;
1727 if (lyxrc.auto_number) {
1728 static string const number_operators = "+-/*";
1729 static string const number_unary_operators = "+-";
1730 static string const number_seperators = ".,:";
1732 if (current_font.number() == LyXFont::ON) {
1733 if (!isdigit(c) && !contains(number_operators, c) &&
1734 !(contains(number_seperators, c) &&
1735 cursor.pos() >= 1 &&
1736 cursor.pos() < cursor.par()->size() &&
1737 getFont(bview->buffer(),
1739 cursor.pos()).number() == LyXFont::ON &&
1740 getFont(bview->buffer(),
1742 cursor.pos()-1).number() == LyXFont::ON)
1744 Number(bview); // Set current_font.number to OFF
1745 } else if (isdigit(c) &&
1746 real_current_font.isVisibleRightToLeft()) {
1747 Number(bview); // Set current_font.number to ON
1749 if (cursor.pos() > 0) {
1750 char const c = cursor.par()->getChar(cursor.pos() - 1);
1751 if (contains(number_unary_operators, c) &&
1752 (cursor.pos() == 1 ||
1753 cursor.par()->isSeparator(cursor.pos() - 2) ||
1754 cursor.par()->isNewline(cursor.pos() - 2) )
1756 setCharFont(bview->buffer(),
1760 } else if (contains(number_seperators, c) &&
1761 cursor.pos() >= 2 &&
1762 getFont(bview->buffer(),
1764 cursor.pos()-2).number() == LyXFont::ON) {
1765 setCharFont(bview->buffer(),
1775 /* First check, if there will be two blanks together or a blank at
1776 the beginning of a paragraph.
1777 I decided to handle blanks like normal characters, the main
1778 difference are the special checks when calculating the row.fill
1779 (blank does not count at the end of a row) and the check here */
1781 // The bug is triggered when we type in a description environment:
1782 // The current_font is not changed when we go from label to main text
1783 // and it should (along with realtmpfont) when we type the space.
1784 // CHECK There is a bug here! (Asger)
1786 LyXFont realtmpfont = real_current_font;
1787 LyXFont rawtmpfont = current_font; /* store the current font.
1788 * This is because of the use
1789 * of cursor movements. The moving
1790 * cursor would refresh the
1793 // Get the font that is used to calculate the baselineskip
1794 Paragraph::size_type const lastpos = cursor.par()->size();
1795 LyXFont rawparfont =
1796 cursor.par()->getFontSettings(bview->buffer()->params,
1799 bool jumped_over_space = false;
1801 if (!freeSpacing && IsLineSeparatorChar(c)) {
1802 if ((cursor.pos() > 0
1803 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1804 || (cursor.pos() > 0
1805 && cursor.par()->isNewline(cursor.pos() - 1))
1806 || (cursor.pos() == 0)) {
1807 static bool sent_space_message = false;
1808 if (!sent_space_message) {
1809 if (cursor.pos() == 0)
1810 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1812 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1813 sent_space_message = true;
1818 } else if (IsNewlineChar(c)) {
1819 if (cursor.par() == cursor.par()
1820 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1824 /* No newline at first position
1825 * of a paragraph or behind labels.
1826 * TeX does not allow that. */
1828 if (cursor.pos() < cursor.par()->size() &&
1829 cursor.par()->isLineSeparator(cursor.pos()))
1830 // newline always after a blank!
1832 cursor.row()->fill(-1); // to force a new break
1835 // the display inset stuff
1836 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1837 && cursor.row()->par()->getInset(cursor.row()->pos())
1838 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1839 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1840 cursor.row()->fill(-1); // to force a new break
1842 // get the cursor row fist
1843 Row * row = cursor.row();
1844 int y = cursor.y() - row->baseline();
1845 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1846 * already insertet the character */
1847 cursor.par()->insertChar(cursor.pos(), c);
1848 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1850 if (!jumped_over_space) {
1851 // refresh the positions
1853 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1854 tmprow = tmprow->next();
1855 tmprow->pos(tmprow->pos() + 1);
1859 // Is there a break one row above
1860 if ((cursor.par()->isLineSeparator(cursor.pos())
1861 || cursor.par()->isNewline(cursor.pos())
1862 || cursor.row()->fill() == -1)
1863 && row->previous() && row->previous()->par() == row->par()) {
1864 Paragraph::size_type z = nextBreakPoint(bview,
1867 if (z >= row->pos()) {
1870 // set the dimensions of the row above
1871 row->previous()->fill(fill(bview,
1875 setHeightOfRow(bview, row->previous());
1877 y -= row->previous()->height();
1879 refresh_row = row->previous();
1880 status(bview, LyXText::NEED_MORE_REFRESH);
1882 breakAgainOneRow(bview, row);
1884 current_font = rawtmpfont;
1885 real_current_font = realtmpfont;
1886 setCursor(bview, cursor.par(), cursor.pos() + 1,
1887 false, cursor.boundary());
1888 // cursor MUST be in row now.
1890 if (row->next() && row->next()->par() == row->par())
1891 need_break_row = row->next();
1895 // check, wether the last characters font has changed.
1896 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1897 && rawparfont != rawtmpfont)
1898 redoHeightOfParagraph(bview, cursor);
1905 // recalculate the fill of the row
1906 if (row->fill() >= 0) /* needed because a newline
1907 * will set fill to -1. Otherwise
1908 * we would not get a rebreak! */
1909 row->fill(fill(bview, row, workWidth(bview)));
1910 if (row->fill() < 0) {
1913 refresh_x = cursor.x();
1914 refresh_pos = cursor.pos();
1915 status(bview, LyXText::NEED_MORE_REFRESH);
1916 breakAgainOneRow(bview, row);
1917 // will the cursor be in another row now?
1918 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1919 if (row->next() && row->next()->par() == row->par())
1920 // this should always be true
1922 breakAgainOneRow(bview, row);
1924 current_font = rawtmpfont;
1925 real_current_font = realtmpfont;
1927 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1929 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1930 != cursor.boundary())
1931 setCursor(bview, cursor.par(), cursor.pos(), false,
1932 !cursor.boundary());
1933 if (row->next() && row->next()->par() == row->par())
1934 need_break_row = row->next();
1939 refresh_x = cursor.x();
1941 refresh_pos = cursor.pos();
1943 int const tmpheight = row->height();
1944 setHeightOfRow(bview, row);
1945 if (tmpheight == row->height())
1946 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1948 status(bview, LyXText::NEED_MORE_REFRESH);
1950 current_font = rawtmpfont;
1951 real_current_font = realtmpfont;
1952 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1956 // check, wether the last characters font has changed.
1957 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1958 && rawparfont != rawtmpfont) {
1959 redoHeightOfParagraph(bview, cursor);
1961 // now the special right address boxes
1962 if (textclasslist.Style(bview->buffer()->params.textclass,
1963 cursor.par()->getLayout()).margintype
1964 == MARGIN_RIGHT_ADDRESS_BOX) {
1965 redoDrawingOfParagraph(bview, cursor);
1973 void LyXText::charInserted()
1975 // Here we could call FinishUndo for every 20 characters inserted.
1976 // This is from my experience how emacs does it.
1977 static unsigned int counter;
1987 void LyXText::prepareToPrint(BufferView * bview,
1988 Row * row, float & x,
1989 float & fill_separator,
1991 float & fill_label_hfill,
1997 float w = row->fill();
1999 fill_label_hfill = 0;
2001 fill_label_hfill = 0;
2004 row->par()->isRightToLeftPar(bview->buffer()->params);
2006 x = (workWidth(bview) > 0)
2007 ? rightMargin(bview->buffer(), row) : 0;
2009 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2011 // is there a manual margin with a manual label
2012 if (textclasslist.Style(bview->buffer()->params.textclass,
2013 row->par()->getLayout()).margintype == MARGIN_MANUAL
2014 && textclasslist.Style(bview->buffer()->params.textclass,
2015 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2017 /* one more since labels are left aligned */
2018 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2019 if (nlh && !row->par()->getLabelWidthString().empty()) {
2020 fill_label_hfill = labelFill(bview, row) / nlh;
2024 // are there any hfills in the row?
2025 float const nh = numberOfHfills(bview->buffer(), row);
2028 fill_hfill = w / nh;
2030 // is it block, flushleft or flushright?
2031 // set x how you need it
2033 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
2034 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2036 align = row->par()->params().align();
2038 // center displayed insets
2040 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2041 && (inset=row->par()->getInset(row->pos()))
2042 && (inset->display())) // || (inset->scroll() < 0)))
2043 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2044 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2047 case LYX_ALIGN_BLOCK:
2048 ns = numberOfSeparators(bview->buffer(), row);
2049 if (ns && row->next() && row->next()->par() == row->par() &&
2050 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2051 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2052 && row->next()->par()->getInset(row->next()->pos())
2053 && row->next()->par()->getInset(row->next()->pos())->display())
2055 fill_separator = w / ns;
2059 case LYX_ALIGN_RIGHT:
2062 case LYX_ALIGN_CENTER:
2070 computeBidiTables(bview->buffer(), row);
2072 Paragraph::size_type main_body =
2073 beginningOfMainBody(bview->buffer(), row->par());
2074 Paragraph::size_type last = rowLast(row);
2076 if (main_body > 0 &&
2077 (main_body-1 > last ||
2078 !row->par()->isLineSeparator(main_body-1))) {
2079 LyXLayout const & layout =
2080 textclasslist.Style(bview->buffer()->params.textclass,
2081 row->par()->getLayout());
2082 x += lyxfont::width(layout.labelsep,
2083 getFont(bview->buffer(), row->par(), -2));
2084 if (main_body-1 <= last)
2085 x += fill_label_hfill;
2090 /* important for the screen */
2093 /* the cursor set functions have a special mechanism. When they
2094 * realize, that you left an empty paragraph, they will delete it.
2095 * They also delete the corresponding row */
2097 void LyXText::cursorRightOneWord(BufferView * bview) const
2099 // treat floats, HFills and Insets as words
2100 LyXCursor tmpcursor = cursor;
2101 // CHECK See comment on top of text.C
2103 if (tmpcursor.pos() == tmpcursor.par()->size()
2104 && tmpcursor.par()->next()) {
2105 tmpcursor.par(tmpcursor.par()->next());
2110 // Skip through initial nonword stuff.
2111 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2112 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2113 // printf("Current pos1 %d", tmpcursor.pos()) ;
2114 tmpcursor.pos(tmpcursor.pos() + 1);
2117 // Advance through word.
2118 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2119 tmpcursor.par()->isWord( tmpcursor.pos())) {
2120 // printf("Current pos2 %d", tmpcursor.pos()) ;
2121 tmpcursor.pos(tmpcursor.pos() + 1);
2125 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2129 void LyXText::cursorTab(BufferView * bview) const
2131 LyXCursor tmpcursor = cursor;
2132 while (tmpcursor.pos() < tmpcursor.par()->size()
2133 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2134 tmpcursor.pos(tmpcursor.pos() + 1);
2136 if (tmpcursor.pos() == tmpcursor.par()->size()){
2137 if (tmpcursor.par()->next()) {
2138 tmpcursor.par(tmpcursor.par()->next());
2142 tmpcursor.pos(tmpcursor.pos() + 1);
2143 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2147 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2148 of prior word, not to end of next prior word. */
2150 void LyXText::cursorLeftOneWord(BufferView * bview) const
2152 LyXCursor tmpcursor = cursor;
2153 cursorLeftOneWord(tmpcursor);
2154 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2157 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2159 // treat HFills, floats and Insets as words
2162 && (cur.par()->isSeparator(cur.pos() - 1)
2163 || cur.par()->isKomma(cur.pos() - 1))
2164 && !(cur.par()->isHfill(cur.pos() - 1)
2165 || cur.par()->isInset(cur.pos() - 1)))
2166 cur.pos(cur.pos() - 1);
2169 && (cur.par()->isInset(cur.pos() - 1)
2170 || cur.par()->isHfill(cur.pos() - 1))) {
2171 cur.pos(cur.pos() - 1);
2172 } else if (!cur.pos()) {
2173 if (cur.par()->previous()){
2174 cur.par(cur.par()->previous());
2175 cur.pos(cur.par()->size());
2177 } else { // Here, cur != 0
2178 while (cur.pos() > 0 &&
2179 cur.par()->isWord(cur.pos()-1) )
2180 cur.pos(cur.pos() - 1);
2184 /* -------> Select current word. This depends on behaviour of
2185 CursorLeftOneWord(), so it is patched as well. */
2186 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2187 word_location const loc) const
2189 // first put the cursor where we wana start to select the word
2192 case WHOLE_WORD_STRICT:
2193 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2194 || cursor.par()->isSeparator(cursor.pos())
2195 || cursor.par()->isKomma(cursor.pos())
2196 || cursor.par()->isSeparator(cursor.pos() -1)
2197 || cursor.par()->isKomma(cursor.pos() -1)) {
2201 // no break here, we go to the next
2204 // Move cursor to the beginning, when not already there.
2205 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2206 && !from.par()->isKomma(from.pos() - 1))
2207 cursorLeftOneWord(from);
2210 // always move the cursor to the beginning of previous word
2211 cursorLeftOneWord(from);
2214 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2220 while (to.pos() < to.par()->size()
2221 && !to.par()->isSeparator(to.pos())
2222 && !to.par()->isKomma(to.pos())
2223 && !to.par()->isHfill(to.pos()) )
2225 to.pos(to.pos() + 1);
2230 void LyXText::selectWord(BufferView * bview, word_location const loc)
2234 getWord(from, to, loc);
2236 setCursor(bview, from.par(), from.pos());
2239 selection.cursor = cursor;
2240 setCursor(bview, to.par(), to.pos() );
2241 setSelection(bview);
2245 /* -------> Select the word currently under the cursor when no
2246 selection is currently set */
2247 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2248 word_location const loc)
2250 if (!selection.set()) {
2251 selectWord(bview, loc);
2252 return selection.set();
2258 // This function is only used by the spellchecker for NextWord().
2259 // It doesn't handle LYX_ACCENTs and probably never will.
2260 string const LyXText::selectNextWord(BufferView * bview,
2261 float & value) const
2263 if (the_locking_inset) {
2264 string str = the_locking_inset->selectNextWord(bview, value);
2266 value += float(cursor.y())/float(height);
2269 #warning Dekel please have a look on this one RTL? (Jug)
2270 // we have to go on checking so move cusor to the right
2271 if (cursor.pos() == cursor.par()->size()) {
2272 if (!cursor.par()->next())
2274 cursor.par(cursor.par()->next());
2277 cursor.pos(cursor.pos() + 1);
2279 Paragraph * tmppar = cursor.par();
2281 // If this is not the very first word, skip rest of
2282 // current word because we are probably in the middle
2283 // of a word if there is text here.
2284 if (cursor.pos() || cursor.par()->previous()) {
2285 while (cursor.pos() < cursor.par()->size()
2286 && cursor.par()->isLetter(cursor.pos()))
2287 cursor.pos(cursor.pos() + 1);
2290 // Now, skip until we have real text (will jump paragraphs)
2291 while ((cursor.par()->size() > cursor.pos()
2292 && (!cursor.par()->isLetter(cursor.pos())
2294 || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
2295 .latex() == LyXFont::ON
2298 && (!cursor.par()->isInset(cursor.pos()) ||
2299 !cursor.par()->getInset(cursor.pos())->isTextInset()))
2300 || (cursor.par()->size() == cursor.pos()
2301 && cursor.par()->next()))
2303 if (cursor.pos() == cursor.par()->size()) {
2304 cursor.par(cursor.par()->next());
2307 cursor.pos(cursor.pos() + 1);
2310 // now check if we hit an inset so it has to be a inset containing text!
2311 if (cursor.pos() < cursor.par()->size() &&
2312 cursor.par()->isInset(cursor.pos()))
2315 cursor.par()->getInset(cursor.pos())->edit(bview);
2316 // now call us again to do the above trick
2317 // but obviously we have to start from down below ;)
2318 return bview->text->selectNextWord(bview, value);
2321 // Update the value if we changed paragraphs
2322 if (cursor.par() != tmppar){
2323 setCursor(bview, cursor.par(), cursor.pos());
2324 value = float(cursor.y())/float(height);
2327 // Start the selection from here
2328 selection.cursor = cursor;
2332 // and find the end of the word
2333 // (optional hyphens are part of a word)
2334 while (cursor.pos() < cursor.par()->size()
2335 && (cursor.par()->isLetter(cursor.pos()))
2336 // assignment is intentional here
2337 || ((inset = getInset())
2338 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2339 && static_cast<InsetSpecialChar *>(inset)->kind()
2340 == InsetSpecialChar::HYPHENATION
2342 cursor.pos(cursor.pos() + 1);
2344 // Finally, we copy the word to a string and return it
2346 if (selection.cursor.pos() < cursor.pos()) {
2347 Paragraph::size_type i;
2348 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2349 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2350 str += cursor.par()->getChar(i);
2357 // This one is also only for the spellchecker
2358 void LyXText::selectSelectedWord(BufferView * bview)
2360 if (the_locking_inset) {
2361 the_locking_inset->selectSelectedWord(bview);
2364 // move cursor to the beginning
2365 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2367 // set the sel cursor
2368 selection.cursor = cursor;
2371 // now find the end of the word
2372 while (cursor.pos() < cursor.par()->size()
2373 && (cursor.par()->isLetter(cursor.pos())
2374 // assignment is intentional here
2375 || ((inset = getInset())
2376 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2377 && static_cast<InsetSpecialChar *>(inset)->kind()
2378 == InsetSpecialChar::HYPHENATION
2380 cursor.pos(cursor.pos() + 1);
2382 setCursor(bview, cursor.par(), cursor.pos());
2384 // finally set the selection
2385 setSelection(bview);
2389 /* -------> Delete from cursor up to the end of the current or next word. */
2390 void LyXText::deleteWordForward(BufferView * bview)
2392 if (!cursor.par()->size())
2395 LyXCursor tmpcursor = cursor;
2396 tmpcursor.row(0); //Â ??
2397 selection.set(true); // to avoid deletion
2398 cursorRightOneWord(bview);
2399 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2400 selection.cursor = cursor;
2402 setSelection(bview);
2404 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2405 cutSelection(bview);
2410 /* -------> Delete from cursor to start of current or prior word. */
2411 void LyXText::deleteWordBackward(BufferView * bview)
2413 if (!cursor.par()->size())
2416 LyXCursor tmpcursor = cursor;
2417 tmpcursor.row(0); // ??
2418 selection.set(true); // to avoid deletion
2419 cursorLeftOneWord(bview);
2420 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2421 selection.cursor = cursor;
2423 setSelection(bview);
2424 cutSelection(bview);
2429 /* -------> Kill to end of line. */
2430 void LyXText::deleteLineForward(BufferView * bview)
2432 if (!cursor.par()->size())
2433 // Paragraph is empty, so we just go to the right
2436 LyXCursor tmpcursor = cursor;
2437 // We can't store the row over a regular setCursor
2438 // so we set it to 0 and reset it afterwards.
2439 tmpcursor.row(0); //Â ??
2440 selection.set(true); // to avoid deletion
2442 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2443 selection.cursor = cursor;
2445 setSelection(bview);
2446 // What is this test for ??? (JMarc)
2447 if (!selection.set()) {
2448 deleteWordForward(bview);
2450 cutSelection(bview);
2456 // Change the case of a word at cursor position.
2457 // This function directly manipulates Paragraph::text because there
2458 // is no Paragraph::SetChar currently. I did what I could to ensure
2459 // that it is correct. I guess part of it should be moved to
2460 // Paragraph, but it will have to change for 1.1 anyway. At least
2461 // it does not access outside of the allocated array as the older
2462 // version did. (JMarc)
2463 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2468 if (selection.set()) {
2469 from = selection.start;
2472 getWord(from, to, PARTIAL_WORD);
2473 setCursor(bview, to.par(), to.pos() + 1);
2476 changeRegionCase(bview, from, to, action);
2480 void LyXText::changeRegionCase(BufferView * bview,
2481 LyXCursor const & from,
2482 LyXCursor const & to,
2483 LyXText::TextCase action)
2485 lyx::Assert(from <= to);
2487 setUndo(bview, Undo::FINISH,
2488 from.par(), to.par()->next());
2490 Paragraph::size_type pos = from.pos();
2491 Paragraph * par = from.par();
2493 while (par && (pos != to.pos() || par != to.par())) {
2494 unsigned char c = par->getChar(pos);
2495 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2497 case text_lowercase:
2500 case text_capitalization:
2502 action = text_lowercase;
2504 case text_uppercase:
2509 par->setChar(pos, c);
2510 checkParagraph(bview, par, pos);
2513 if (pos == par->size()) {
2518 if (to.row() != from.row()) {
2519 refresh_y = from.y() - from.row()->baseline();
2520 refresh_row = from.row();
2521 status(bview, LyXText::NEED_MORE_REFRESH);
2526 void LyXText::transposeChars(BufferView & bview)
2528 Paragraph * tmppar = cursor.par();
2530 setUndo(&bview, Undo::FINISH,
2531 tmppar, tmppar->next());
2533 Paragraph::size_type tmppos = cursor.pos();
2535 // First decide if it is possible to transpose at all
2537 // We are at the beginning of a paragraph.
2538 if (tmppos == 0) return;
2540 // We are at the end of a paragraph.
2541 if (tmppos == tmppar->size() - 1) return;
2543 unsigned char c1 = tmppar->getChar(tmppos);
2544 unsigned char c2 = tmppar->getChar(tmppos - 1);
2546 if (c1 != Paragraph::META_INSET
2547 && c2 != Paragraph::META_INSET) {
2548 tmppar->setChar(tmppos, c2);
2549 tmppar->setChar(tmppos - 1, c1);
2551 // We should have an implementation that handles insets
2552 // as well, but that will have to come later. (Lgb)
2553 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2557 void LyXText::Delete(BufferView * bview)
2559 // this is a very easy implementation
2561 LyXCursor old_cursor = cursor;
2562 int const old_cur_par_id = old_cursor.par()->id();
2563 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2564 old_cursor.par()->previous()->id() : 0;
2566 // just move to the right
2569 // CHECK Look at the comment here.
2570 // This check is not very good...
2571 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2572 // and that can very well delete the par or par->previous in
2573 // old_cursor. Will a solution where we compare paragraph id's
2575 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2576 == old_cur_par_prev_id
2577 && cursor.par()->id() != old_cur_par_id)
2578 return; // delete-empty-paragraph-mechanism has done it
2580 // if you had success make a backspace
2581 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2582 LyXCursor tmpcursor = cursor;
2583 cursor = old_cursor; // to make sure undo gets the right cursor position
2584 setUndo(bview, Undo::DELETE,
2585 cursor.par(), cursor.par()->next());
2592 void LyXText::backspace(BufferView * bview)
2594 // Get the font that is used to calculate the baselineskip
2595 Paragraph::size_type lastpos = cursor.par()->size();
2596 LyXFont rawparfont =
2597 cursor.par()->getFontSettings(bview->buffer()->params,
2600 if (cursor.pos() == 0) {
2601 // The cursor is at the beginning of a paragraph,
2602 // so the the backspace will collapse two paragraphs into one.
2604 // we may paste some paragraphs
2606 // is it an empty paragraph?
2609 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2610 // This is an empty paragraph and we delete it just by moving the cursor one step
2611 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2612 // of the paragraph.
2614 if (cursor.par()->previous()) {
2615 Paragraph * tmppar = cursor.par()->previous();
2616 if (cursor.par()->getLayout() == tmppar->getLayout()
2617 && cursor.par()->getAlign() == tmppar->getAlign()) {
2618 // Inherit bottom DTD from the paragraph below.
2619 // (the one we are deleting)
2620 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2621 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2622 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2627 // the layout things can change the height of a row !
2628 int const tmpheight = cursor.row()->height();
2629 setHeightOfRow(bview, cursor.row());
2630 if (cursor.row()->height() != tmpheight) {
2631 refresh_y = cursor.y() - cursor.row()->baseline();
2632 refresh_row = cursor.row();
2633 status(bview, LyXText::NEED_MORE_REFRESH);
2639 if (cursor.par()->previous()) {
2640 setUndo(bview, Undo::DELETE,
2641 cursor.par()->previous(),
2642 cursor.par()->next());
2645 Paragraph * tmppar = cursor.par();
2646 Row * tmprow = cursor.row();
2648 // We used to do cursorLeftIntern() here, but it is
2649 // not a good idea since it triggers the auto-delete
2650 // mechanism. So we do a cursorLeftIntern()-lite,
2651 // without the dreaded mechanism. (JMarc)
2652 if (cursor.par()->previous()) {
2653 // steps into the above paragraph.
2654 setCursorIntern(bview, cursor.par()->previous(),
2655 cursor.par()->previous()->size(),
2659 /* Pasting is not allowed, if the paragraphs have different
2660 layout. I think it is a real bug of all other
2661 word processors to allow it. It confuses the user.
2662 Even so with a footnote paragraph and a non-footnote
2663 paragraph. I will not allow pasting in this case,
2664 because the user would be confused if the footnote behaves
2665 different wether it is open or closed.
2667 Correction: Pasting is always allowed with standard-layout
2669 if (cursor.par() != tmppar
2670 && (cursor.par()->getLayout() == tmppar->getLayout()
2671 || tmppar->getLayout() == 0 /*standard*/)
2672 && cursor.par()->getAlign() == tmppar->getAlign()) {
2674 removeParagraph(tmprow);
2676 cursor.par()->pasteParagraph(bview->buffer()->params);
2678 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2679 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2680 // strangely enough it seems that commenting out the line above removes
2681 // most or all of the segfaults. I will however also try to move the
2682 // two Remove... lines in front of the PasteParagraph too.
2685 cursor.pos(cursor.pos() - 1);
2687 status(bview, LyXText::NEED_MORE_REFRESH);
2688 refresh_row = cursor.row();
2689 refresh_y = cursor.y() - cursor.row()->baseline();
2691 // remove the lost paragraph
2692 // This one is not safe, since the paragraph that the tmprow and the
2693 // following rows belong to has been deleted by the PasteParagraph
2694 // above. The question is... could this be moved in front of the
2696 //RemoveParagraph(tmprow);
2697 //RemoveRow(tmprow);
2699 // This rebuilds the rows.
2700 appendParagraph(bview, cursor.row());
2701 updateCounters(bview, cursor.row());
2703 // the row may have changed, block, hfills etc.
2704 setCursor(bview, cursor.par(), cursor.pos(), false);
2707 /* this is the code for a normal backspace, not pasting
2709 setUndo(bview, Undo::DELETE,
2710 cursor.par(), cursor.par()->next());
2711 // We used to do cursorLeftIntern() here, but it is
2712 // not a good idea since it triggers the auto-delete
2713 // mechanism. So we do a cursorLeftIntern()-lite,
2714 // without the dreaded mechanism. (JMarc)
2715 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2716 false, cursor.boundary());
2718 // some insets are undeletable here
2719 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2720 if (!cursor.par()->getInset(cursor.pos())->deletable())
2722 // force complete redo when erasing display insets
2723 // this is a cruel method but safe..... Matthias
2724 if (cursor.par()->getInset(cursor.pos())->display() ||
2725 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2726 cursor.par()->erase(cursor.pos());
2727 redoParagraph(bview);
2732 Row * row = cursor.row();
2733 int y = cursor.y() - row->baseline();
2734 Paragraph::size_type z;
2735 /* remember that a space at the end of a row doesnt count
2736 * when calculating the fill */
2737 if (cursor.pos() < rowLast(row) ||
2738 !cursor.par()->isLineSeparator(cursor.pos())) {
2739 row->fill(row->fill() + singleWidth(bview,
2744 /* some special code when deleting a newline. This is similar
2745 * to the behavior when pasting paragraphs */
2746 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2747 cursor.par()->erase(cursor.pos());
2748 // refresh the positions
2750 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2751 tmprow = tmprow->next();
2752 tmprow->pos(tmprow->pos() - 1);
2754 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2755 cursor.pos(cursor.pos() - 1);
2757 if (cursor.pos() < cursor.par()->size()
2758 && !cursor.par()->isSeparator(cursor.pos())) {
2759 cursor.par()->insertChar(cursor.pos(), ' ');
2760 setCharFont(bview->buffer(), cursor.par(),
2761 cursor.pos(), current_font);
2762 // refresh the positions
2764 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2765 tmprow = tmprow->next();
2766 tmprow->pos(tmprow->pos() + 1);
2770 cursor.par()->erase(cursor.pos());
2772 // refresh the positions
2774 while (tmprow->next()
2775 && tmprow->next()->par() == row->par()) {
2776 tmprow = tmprow->next();
2777 tmprow->pos(tmprow->pos() - 1);
2780 // delete newlines at the beginning of paragraphs
2781 while (cursor.par()->size() &&
2782 cursor.par()->isNewline(cursor.pos()) &&
2783 cursor.pos() == beginningOfMainBody(bview->buffer(),
2785 cursor.par()->erase(cursor.pos());
2786 // refresh the positions
2788 while (tmprow->next() &&
2789 tmprow->next()->par() == row->par()) {
2790 tmprow = tmprow->next();
2791 tmprow->pos(tmprow->pos() - 1);
2796 // is there a break one row above
2797 if (row->previous() && row->previous()->par() == row->par()) {
2798 z = nextBreakPoint(bview, row->previous(),
2800 if (z >= row->pos()) {
2803 Row * tmprow = row->previous();
2805 // maybe the current row is now empty
2806 if (row->pos() >= row->par()->size()) {
2811 breakAgainOneRow(bview, row);
2812 if (row->next() && row->next()->par() == row->par())
2813 need_break_row = row->next();
2818 // set the dimensions of the row above
2819 y -= tmprow->height();
2820 tmprow->fill(fill(bview, tmprow,
2822 setHeightOfRow(bview, tmprow);
2825 refresh_row = tmprow;
2826 status(bview, LyXText::NEED_MORE_REFRESH);
2827 setCursor(bview, cursor.par(), cursor.pos(),
2828 false, cursor.boundary());
2829 //current_font = rawtmpfont;
2830 //real_current_font = realtmpfont;
2831 // check, whether the last character's font has changed.
2833 cursor.par()->getFontSettings(bview->buffer()->params,
2834 cursor.par()->size() - 1))
2835 redoHeightOfParagraph(bview, cursor);
2840 // break the cursor row again
2841 if (row->next() && row->next()->par() == row->par() &&
2842 (rowLast(row) == row->par()->size() - 1 ||
2843 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2845 /* it can happen that a paragraph loses one row
2846 * without a real breakup. This is when a word
2847 * is to long to be broken. Well, I don t care this
2849 if (rowLast(row) == row->par()->size() - 1)
2850 removeRow(row->next());
2854 status(bview, LyXText::NEED_MORE_REFRESH);
2856 breakAgainOneRow(bview, row);
2857 // will the cursor be in another row now?
2858 if (row->next() && row->next()->par() == row->par() &&
2859 rowLast(row) <= cursor.pos()) {
2861 breakAgainOneRow(bview, row);
2864 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2866 if (row->next() && row->next()->par() == row->par())
2867 need_break_row = row->next();
2871 // set the dimensions of the row
2872 row->fill(fill(bview, row, workWidth(bview)));
2873 int const tmpheight = row->height();
2874 setHeightOfRow(bview, row);
2875 if (tmpheight == row->height())
2876 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2878 status(bview, LyXText::NEED_MORE_REFRESH);
2881 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2885 // current_font = rawtmpfont;
2886 // real_current_font = realtmpfont;
2888 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2889 != cursor.boundary())
2890 setCursor(bview, cursor.par(), cursor.pos(), false,
2891 !cursor.boundary());
2893 lastpos = cursor.par()->size();
2894 if (cursor.pos() == lastpos)
2895 setCurrentFont(bview);
2897 // check, whether the last characters font has changed.
2899 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2900 redoHeightOfParagraph(bview, cursor);
2902 // now the special right address boxes
2903 if (textclasslist.Style(bview->buffer()->params.textclass,
2904 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2905 redoDrawingOfParagraph(bview, cursor);
2911 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2912 Row * row_ptr, int y, bool cleared)
2914 // returns a printed row
2915 Painter & pain = bview->painter();
2918 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2920 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2922 Paragraph::size_type vpos;
2923 Paragraph::size_type pos;
2927 LyXFont font(LyXFont::ALL_SANE);
2929 if (row_ptr->height() <= 0) {
2930 lyxerr << "LYX_ERROR: row.height: "
2931 << row_ptr->height() << endl;
2936 float fill_separator;
2938 float fill_label_hfill;
2939 prepareToPrint(bview, row_ptr, x, fill_separator,
2940 fill_hfill, fill_label_hfill);
2942 if (inset_owner && (x < 0))
2946 // clear the area where we want to paint/print
2947 int const ww = bview->workWidth();
2949 bool clear_area = true;
2952 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2953 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2954 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2955 clear_area = inset->doClearArea();
2957 // we don't need to clear it's already done!!!
2960 } else if (clear_area) {
2961 #ifdef WITH_WARNINGS
2962 #warning Should be fixed with a lyxinset::clear_width(bv, font) function! (Jug)
2963 #warning Should we not fix this in the Painter, please have a look Lars! (Jug)
2965 int const y = y_offset < 0 ? 0 : y_offset;
2966 int const h = y_offset < 0 ?
2967 row_ptr->height() + y_offset : row_ptr->height();
2968 int const w = inset_owner ?
2969 inset_owner->width(bview, font) - 2 : ww;
2970 int const x = x_offset;
2971 pain.fillRectangle(x, y, w, h, backgroundColor());
2972 } else if (inset != 0) {
2973 int h = row_ptr->baseline() - inset->ascent(bview, font);
2974 // first clear the whole row above the inset!
2977 int const w = (inset_owner ?
2978 inset_owner->width(bview, font) : ww);
2980 pain.fillRectangle(x_offset, y_offset, ww, h,
2983 h += inset->ascent(bview, font) + inset->descent(bview, font);
2984 // clear the space below the inset!
2985 if ((row_ptr->height() - h) > 0) {
2987 int const w = (inset_owner ?
2988 inset_owner->width(bview, font) : ww);
2990 pain.fillRectangle(x_offset, y_offset + h,
2991 ww, row_ptr->height() - h,
2994 // clear the space behind the inset, if needed
2995 if (!inset_owner && !inset->display() && !inset->needFullRow())
2997 int const w = inset->width(bview, font) + int(x);
2998 pain.fillRectangle(w, y_offset,
2999 ww - w, row_ptr->height(),
3004 if (selection.set()) {
3005 int const w = (inset_owner ?
3006 inset_owner->width(bview, font) : ww);
3008 if (bidi_same_direction) {
3009 if (selection.start.row() == row_ptr &&
3010 selection.end.row() == row_ptr) {
3011 if (selection.start.x() < selection.end.x())
3012 pain.fillRectangle(x_offset + selection.start.x(),
3014 selection.end.x() - selection.start.x(),
3018 pain.fillRectangle(x_offset + selection.end.x(),
3020 selection.start.x() - selection.end.x(),
3023 } else if (selection.start.row() == row_ptr) {
3025 pain.fillRectangle(x_offset, y_offset,
3026 selection.start.x(),
3030 pain.fillRectangle(x_offset + selection.start.x(),
3032 w - selection.start.x(),
3035 } else if (selection.end.row() == row_ptr) {
3037 pain.fillRectangle(x_offset + selection.end.x(),
3039 w - selection.end.x(),
3043 pain.fillRectangle(x_offset, y_offset,
3047 } else if (y > selection.start.y()
3048 && y < selection.end.y()) {
3049 pain.fillRectangle(x_offset, y_offset, w,
3053 } else if (selection.start.row() != row_ptr &&
3054 selection.end.row() != row_ptr &&
3055 y > selection.start.y()
3056 && y < selection.end.y()) {
3057 pain.fillRectangle(x_offset, y_offset, w,
3060 } else if (selection.start.row() == row_ptr ||
3061 selection.end.row() == row_ptr) {
3063 if ((selection.start.row() != row_ptr && !is_rtl) ||
3064 (selection.end.row() != row_ptr && is_rtl))
3065 pain.fillRectangle(x_offset, y_offset,
3069 Paragraph::size_type main_body =
3070 beginningOfMainBody(bview->buffer(),
3073 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3074 pos = vis2log(vpos);
3075 float const old_tmpx = tmpx;
3076 if (main_body > 0 && pos == main_body-1) {
3077 tmpx += fill_label_hfill +
3078 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3079 row_ptr->par()->getLayout()).labelsep,
3080 getFont(bview->buffer(),row_ptr->par(), -2));
3081 if (row_ptr->par()->isLineSeparator(main_body-1))
3082 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3084 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3085 tmpx += singleWidth(bview, row_ptr->par(), pos);
3086 if (pos >= main_body)
3089 tmpx += fill_label_hfill;
3091 else if (row_ptr->par()->isSeparator(pos)) {
3092 tmpx += singleWidth(bview, row_ptr->par(), pos);
3093 if (pos >= main_body)
3094 tmpx += fill_separator;
3096 tmpx += singleWidth(bview, row_ptr->par(), pos);
3098 if ((selection.start.row() != row_ptr ||
3099 selection.start.pos() <= pos) &&
3100 (selection.end.row() != row_ptr ||
3101 pos < selection.end.pos()) )
3102 // Here we do not use x_offset as x_offset was
3104 pain.fillRectangle(int(old_tmpx),
3106 int(tmpx - old_tmpx + 1),
3111 if ((selection.start.row() != row_ptr && is_rtl) ||
3112 (selection.end.row() != row_ptr && !is_rtl) )
3113 pain.fillRectangle(x_offset + int(tmpx),
3123 // Draw appendix lines
3124 Paragraph * firstpar = row_ptr->par();
3126 if (firstpar->params().appendix()) {
3127 pain.line(1, y_offset,
3128 1, y_offset + row_ptr->height(),
3129 LColor::appendixline);
3130 pain.line(ww - 2, y_offset,
3131 ww - 2, y_offset + row_ptr->height(),
3132 LColor::appendixline);
3136 Paragraph::depth_type const depth = firstpar->getDepth();
3138 Paragraph::depth_type next_depth = 0;
3139 Paragraph::depth_type prev_depth = 0;
3140 if (row_ptr->next())
3141 next_depth = row_ptr->next()->par()->getDepth();
3142 if (row_ptr->previous())
3143 prev_depth = row_ptr->previous()->par()->getDepth();
3145 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3146 int const line_x = (LYX_PAPER_MARGIN / 5) *
3147 i + box_x + x_offset;
3148 pain.line(line_x, y_offset, line_x,
3149 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3153 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3156 pain.fillRectangle(line_x,
3157 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3158 LYX_PAPER_MARGIN / 5, 2,
3164 LyXLayout const & layout =
3165 textclasslist.Style(bview->buffer()->params.textclass,
3166 row_ptr->par()->getLayout());
3169 int y_bottom = row_ptr->height();
3171 // is it a first row?
3172 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3174 // start of appendix?
3175 if (row_ptr->par()->params().startOfAppendix()) {
3176 pain.line(1, y_offset,
3178 LColor::appendixline);
3181 // think about the margins
3182 if (!row_ptr->previous() && bv_owner)
3183 y_top += LYX_PAPER_MARGIN;
3185 // draw a top pagebreak
3186 if (row_ptr->par()->params().pagebreakTop()) {
3188 pb_font.setColor(LColor::pagebreak).decSize();
3192 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3194 y_offset + y_top + 2 * defaultHeight(),
3196 Painter::line_onoffdash);
3197 lyxfont::rectText(_("Page Break (top)"), pb_font,
3199 pain.rectText((ww - w)/2,
3200 y_offset + y_top + 2 * defaultHeight() + d,
3201 _("Page Break (top)"),
3205 y_top += 3 * defaultHeight();
3208 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3210 pain.line(0, y_offset + 2 + y_top,
3211 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3214 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3216 y_offset + y_top + 3 * defaultHeight(),
3219 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3220 LYX_PAPER_MARGIN / 2,
3221 y_offset + y_top + 3 * defaultHeight(),
3224 y_top += 3 * defaultHeight();
3227 // think about user added space
3228 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3230 // think about the parskip
3231 // some parskips VERY EASY IMPLEMENTATION
3232 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3233 if (layout.latextype == LATEX_PARAGRAPH
3234 && firstpar->getDepth() == 0
3235 && firstpar->previous())
3236 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3237 else if (firstpar->previous()
3238 && textclasslist.Style(bview->buffer()->params.textclass,
3239 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3240 && firstpar->previous()->getDepth() == 0)
3241 // is it right to use defskip here, too? (AS)
3242 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3245 if (row_ptr->par()->params().lineTop()) {
3247 y_top += lyxfont::ascent('x',
3248 getFont(bview->buffer(),
3249 row_ptr->par(), 0));
3250 int const w = (inset_owner ?
3251 inset_owner->width(bview, font) : ww);
3252 int const xp = static_cast<int>(inset_owner ? x : 0);
3253 pain.line(xp, y_offset + y_top,
3254 w, y_offset + y_top,
3256 Painter::line_solid,
3257 Painter::line_thick);
3259 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3260 row_ptr->par(), 0));
3263 // should we print a label?
3264 if (layout.labeltype >= LABEL_STATIC
3265 && (layout.labeltype != LABEL_STATIC
3266 || layout.latextype != LATEX_ENVIRONMENT
3267 || row_ptr->par()->isFirstInSequence())) {
3268 font = getFont(bview->buffer(), row_ptr->par(), -2);
3269 if (!row_ptr->par()->getLabelstring().empty()) {
3271 string const tmpstring =
3272 row_ptr->par()->getLabelstring();
3274 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3275 if (bview->buffer()->params.secnumdepth >= 0) {
3276 // this is special code for
3277 // the chapter layout. This is
3278 // printed in an extra row
3279 // and has a pagebreak at
3281 float spacing_val = 1.0;
3282 if (!row_ptr->par()->params().spacing().isDefault()) {
3283 spacing_val = row_ptr->par()->params().spacing().getValue();
3285 spacing_val = bview->buffer()->params.spacing.getValue();
3288 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3289 + int(layout.parsep) * defaultHeight();
3291 tmpx = ww - leftMargin(bview, row_ptr) -
3292 lyxfont::width(tmpstring, font);
3293 pain.text(int(tmpx),
3294 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3299 tmpx = ww - leftMargin(bview, row_ptr)
3300 + lyxfont::width(layout.labelsep, font);
3302 tmpx = x - lyxfont::width(layout.labelsep, font)
3303 - lyxfont::width(tmpstring, font);
3306 pain.text(int(tmpx),
3307 y_offset + row_ptr->baseline(),
3311 // the labels at the top of an environment.
3312 // More or less for bibliography
3313 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3314 layout.labeltype == LABEL_BIBLIO ||
3315 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3316 if (row_ptr->par()->isFirstInSequence()) {
3317 font = getFont(bview->buffer(),
3318 row_ptr->par(), -2);
3319 if (!row_ptr->par()->getLabelstring().empty()) {
3320 string const tmpstring =
3321 row_ptr->par()->getLabelstring();
3322 float spacing_val = 1.0;
3323 if (!row_ptr->par()->params().spacing().isDefault()) {
3324 spacing_val = row_ptr->par()->params().spacing().getValue();
3326 spacing_val = bview->buffer()->params.spacing.getValue();
3329 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3330 + (layout.labelbottomsep * defaultHeight()));
3333 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3334 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3335 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3336 tmpx -= lyxfont::width(tmpstring, font) / 2;
3338 tmpx = ww - leftMargin(bview, row_ptr) -
3339 lyxfont::width(tmpstring, font);
3340 pain.text(int(tmpx),
3341 y_offset + row_ptr->baseline()
3342 - row_ptr->ascent_of_text()
3348 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3349 font = getFont(bview->buffer(), row_ptr->par(), -1);
3351 tmpx = ww - leftMargin(bview, row_ptr)
3352 + lyxfont::width(layout.labelsep, font);
3354 tmpx = x - lyxfont::width(layout.labelsep, font)
3355 - row_ptr->par()->bibkey->width(bview, font);
3356 row_ptr->par()->bibkey->draw(bview, font,
3357 y_offset + row_ptr->baseline(),
3362 // is it a last row?
3363 Paragraph * par = row_ptr->par();
3364 if (row_ptr->par() == par
3365 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3366 // think about the margins
3367 if (!row_ptr->next() && bv_owner)
3368 y_bottom -= LYX_PAPER_MARGIN;
3370 // draw a bottom pagebreak
3371 if (firstpar->params().pagebreakBottom()) {
3373 pb_font.setColor(LColor::pagebreak).decSize();
3374 int const y_place = y_offset + y_bottom
3375 - 2 * defaultHeight();
3381 .line(0, y_place, ww, y_place,
3383 Painter::line_onoffdash);
3384 lyxfont::rectText(_("Page Break (bottom)"), pb_font,
3386 pain.rectText((ww - w) / 2, y_place + d,
3387 _("Page Break (bottom)"),
3391 y_bottom -= 3 * defaultHeight();
3394 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3395 // draw a vfill bottom
3396 int const y_place = y_offset + y_bottom
3397 - 3 * defaultHeight();
3399 pain.line(0, y_place,
3400 LYX_PAPER_MARGIN, y_place,
3402 pain.line(0, y_offset + y_bottom - 2,
3404 y_offset + y_bottom - 2,
3406 pain.line(LYX_PAPER_MARGIN / 2,
3408 LYX_PAPER_MARGIN / 2,
3409 y_offset + y_bottom - 2,
3411 y_bottom -= 3 * defaultHeight();
3414 // think about user added space
3415 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3417 if (firstpar->params().lineBottom()) {
3418 // draw a bottom line
3419 y_bottom -= lyxfont::ascent('x',
3420 getFont(bview->buffer(),
3423 int const w = (inset_owner ?
3424 inset_owner->width(bview, font) : ww);
3425 int const xp = static_cast<int>(inset_owner ? x : 0);
3426 pain.line(xp, y_offset + y_bottom,
3427 w, y_offset + y_bottom,
3428 LColor::topline, Painter::line_solid,
3429 Painter::line_thick);
3430 y_bottom -= lyxfont::ascent('x',
3431 getFont(bview->buffer(),
3437 int const endlabel =
3438 row_ptr->par()->getEndLabel(bview->buffer()->params);
3441 case END_LABEL_FILLED_BOX:
3443 LyXFont const font = getFont(bview->buffer(),
3444 row_ptr->par(), last);
3445 int const size = int(0.75 * lyxfont::maxAscent(font));
3446 int const y = (y_offset + row_ptr->baseline()) - size;
3447 int x = is_rtl ? LYX_PAPER_MARGIN
3448 : ww - LYX_PAPER_MARGIN - size;
3450 if (row_ptr->fill() <= size)
3451 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3452 if (endlabel == END_LABEL_BOX) {
3453 pain.line(x, y, x, y + size,
3455 pain.line(x + size, y, x + size , y + size,
3457 pain.line(x, y, x + size, y,
3459 pain.line(x, y + size, x + size, y + size,
3462 pain.fillRectangle(x, y, size, size,
3466 case END_LABEL_STATIC:
3468 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3469 string const tmpstring = textclasslist.
3470 Style(bview->buffer()->params.textclass,
3471 layout).endlabelstring();
3472 font = getFont(bview->buffer(), row_ptr->par(), -2);
3473 int const tmpx = is_rtl ?
3474 int(x) - lyxfont::width(tmpstring, font)
3475 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3476 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3479 case END_LABEL_NO_LABEL:
3484 // draw the text in the pixmap
3486 vpos = row_ptr->pos();
3488 Paragraph::size_type main_body =
3489 beginningOfMainBody(bview->buffer(), row_ptr->par());
3490 if (main_body > 0 &&
3491 (main_body-1 > last ||
3492 !row_ptr->par()->isLineSeparator(main_body - 1)))
3495 while (vpos <= last) {
3496 pos = vis2log(vpos);
3497 if (main_body > 0 && pos == main_body - 1) {
3498 x += fill_label_hfill
3499 + lyxfont::width(layout.labelsep,
3500 getFont(bview->buffer(),
3501 row_ptr->par(), -2))
3502 - singleWidth(bview,
3507 if (row_ptr->par() ->isHfill(pos)) {
3510 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3512 y_offset + row_ptr->baseline(),
3515 if (hfillExpansion(bview->buffer(),
3517 if (pos >= main_body) {
3519 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3520 int(x + fill_hfill),
3521 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3523 Painter::line_onoffdash);
3527 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3528 int(x + fill_label_hfill),
3529 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3531 Painter::line_onoffdash);
3533 x += fill_label_hfill;
3536 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3538 y_offset + row_ptr->baseline(),
3543 } else if (row_ptr->par()->isSeparator(pos)) {
3544 x += singleWidth(bview,
3545 row_ptr->par(), pos);
3546 if (pos >= main_body)
3547 x += fill_separator;
3550 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3555 int LyXText::defaultHeight() const
3557 LyXFont font(LyXFont::ALL_SANE);
3558 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3562 /* returns the column near the specified x-coordinate of the row
3563 * x is set to the real beginning of this column */
3564 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3565 bool & boundary) const
3568 float fill_separator;
3570 float fill_label_hfill;
3572 prepareToPrint(bview, row, tmpx, fill_separator,
3573 fill_hfill, fill_label_hfill);
3575 Paragraph::size_type vc = row->pos();
3576 Paragraph::size_type last = rowLastPrintable(row);
3577 Paragraph::size_type c = 0;
3578 LyXLayout const & layout =
3579 textclasslist.Style(bview->buffer()->params.textclass,
3580 row->par()->getLayout());
3581 bool left_side = false;
3583 Paragraph::size_type
3584 main_body = beginningOfMainBody(bview->buffer(), row->par());
3585 float last_tmpx = tmpx;
3587 if (main_body > 0 &&
3588 (main_body - 1 > last ||
3589 !row->par()->isLineSeparator(main_body - 1)))
3592 while (vc <= last && tmpx <= x) {
3595 if (main_body > 0 && c == main_body-1) {
3596 tmpx += fill_label_hfill +
3597 lyxfont::width(layout.labelsep,
3598 getFont(bview->buffer(), row->par(), -2));
3599 if (row->par()->isLineSeparator(main_body - 1))
3600 tmpx -= singleWidth(bview, row->par(), main_body-1);
3603 if (hfillExpansion(bview->buffer(), row, c)) {
3604 x += singleWidth(bview, row->par(), c);
3608 tmpx += fill_label_hfill;
3610 else if (row->par()->isSeparator(c)) {
3611 tmpx += singleWidth(bview, row->par(), c);
3613 tmpx+= fill_separator;
3615 tmpx += singleWidth(bview, row->par(), c);
3619 if ((tmpx + last_tmpx) / 2 > x) {
3624 if (vc > last + 1) // This shouldn't happen.
3628 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3629 // some speedup if rtl_support=false
3630 && (!row->next() || row->next()->par() != row->par());
3631 bool const rtl = (lastrow)
3632 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3633 : false; // If lastrow is false, we don't need to compute
3634 // the value of rtl.
3636 if (row->pos() > last) // Row is empty?
3639 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3640 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3642 else if (vc == row->pos()) {
3644 if (bidi_level(c) % 2 == 1)
3647 c = vis2log(vc - 1);
3648 bool const rtl = (bidi_level(c) % 2 == 1);
3649 if (left_side == rtl) {
3651 boundary = isBoundary(bview->buffer(), row->par(), c);
3655 if (row->pos() <= last && c > last
3656 && row->par()->isNewline(last)) {
3657 if (bidi_level(last) % 2 == 0)
3658 tmpx -= singleWidth(bview, row->par(), last);
3660 tmpx += singleWidth(bview, row->par(), last);
3670 // returns pointer to a specified row
3671 Row * LyXText::getRow(Paragraph * par,
3672 Paragraph::size_type pos, int & y) const
3677 Row * tmprow = firstrow;
3680 // find the first row of the specified paragraph
3681 while (tmprow->next() && tmprow->par() != par) {
3682 y += tmprow->height();
3683 tmprow = tmprow->next();
3686 // now find the wanted row
3687 while (tmprow->pos() < pos
3689 && tmprow->next()->par() == par
3690 && tmprow->next()->pos() <= pos) {
3691 y += tmprow->height();
3692 tmprow = tmprow->next();