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 if (!need_break_row && !inset_owner &&
444 bview->text->status() == CHANGED_IN_DRAW)
446 if (row->previous() && row->previous()->par() == row->par())
447 breakAgainOneRow(bview, row->previous());
448 setCursor(bview, cursor.par(), cursor.pos());
449 need_break_row = const_cast<Row *>(row);
454 if (lyxrc.mark_foreign_language &&
455 font.language() != ignore_language &&
456 font.language() != bview->buffer()->params.language) {
457 int const y = offset + row->height() - 1;
458 pain.line(int(tmpx), y, int(x), y, LColor::language);
464 /* usual characters, no insets */
466 // Collect character that we can draw in one command
468 // This is dirty, but fast. Notice that it will never be too small.
469 // For the record, I'll note that Microsoft Word has a limit
470 // of 768 here. We have none :-) (Asger)
471 // Ok. I am the first to admit that the use of std::string will be
472 // a tiny bit slower than using a POD char array. However, I claim
473 // that this slowdown is so small that it is close to inperceptive.
474 // So IMHO we should go with the easier and clearer implementation.
475 // And even if 1024 is a large number here it might overflow, string
476 // will only overflow if the machine is out of memory...
477 static string textstring;
481 Paragraph::size_type const last = rowLastPrintable(row);
483 if (font.language()->lang() == "hebrew") {
484 if (Encodings::IsComposeChar_hebrew(c)) {
485 int const width = lyxfont::width(c, font2);
487 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
488 c = row->par()->getChar(i);
489 if (!Encodings::IsComposeChar_hebrew(c)) {
490 if (IsPrintableNonspace(c)) {
495 dx = (c == 'ø' || c == 'ã') // dalet / resh
496 ? width2 - width : (width2 - width) / 2;
502 pain.text(int(x) + dx, offset + row->baseline(),
505 while (vpos <= last &&
506 (pos = vis2log(vpos)) >= 0
507 && IsPrintableNonspace(c = row->par()->getChar(pos))
508 && !Encodings::IsComposeChar_hebrew(c)
509 && font2 == getFont(bview->buffer(), row->par(), pos)) {
513 // Draw text and set the new x position
514 pain.text(int(x), offset + row->baseline(),
516 x += lyxfont::width(textstring, font);
518 } else if (font.language()->lang() == "arabic" &&
519 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
520 lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
521 if (Encodings::IsComposeChar_arabic(c)) {
522 c = transformChar(c, row->par(), pos);
524 int const width = lyxfont::width(c, font2);
526 for (Paragraph::size_type i = pos-1; i >= 0; --i) {
527 c = row->par()->getChar(i);
528 if (!Encodings::IsComposeChar_arabic(c)) {
529 if (IsPrintableNonspace(c)) {
534 dx = (width2 - width) / 2;
540 pain.text(int(x) + dx, offset + row->baseline(),
543 textstring = transformChar(c, row->par(), pos);
544 while (vpos <= last &&
545 (pos = vis2log(vpos)) >= 0
546 && IsPrintableNonspace(c = row->par()->getChar(pos))
547 && !Encodings::IsComposeChar_arabic(c)
548 && font2 == getFont(bview->buffer(), row->par(), pos)) {
549 c = transformChar(c, row->par(), pos);
553 // Draw text and set the new x position
554 pain.text(int(x), offset + row->baseline(),
556 x += lyxfont::width(textstring, font);
559 while (vpos <= last &&
560 (pos = vis2log(vpos)) >= 0
561 && IsPrintableNonspace(c = row->par()->getChar(pos))
562 && font2 == getFont(bview->buffer(), row->par(), pos)) {
566 // Draw text and set the new x position
567 pain.text(int(x), offset + row->baseline(), textstring, font);
568 x += lyxfont::width(textstring, font);
572 if ((font.language() == inherit_language) ||
573 (font.language() == ignore_language))
574 lyxerr << "No this shouldn't happen!\n";
576 if (lyxrc.mark_foreign_language &&
577 font.language() != bview->buffer()->params.language) {
578 int const y = offset + row->height() - 1;
579 pain.line(int(tmpx), y, int(x), y,
583 // If we want ulem.sty support, drawing
584 // routines should go here. (Asger)
585 // Why shouldn't LyXFont::drawText handle it internally?
589 // Returns the left beginning of the text.
590 // This information cannot be taken from the layouts-objekt, because in
591 // LaTeX the beginning of the text fits in some cases (for example sections)
592 // exactly the label-width.
593 int LyXText::leftMargin(BufferView * bview, Row const * row) const
595 LyXLayout const & layout =
596 textclasslist.Style(bview->buffer()->params.textclass,
597 row->par()->getLayout());
599 string parindent = layout.parindent;
601 int x = LYX_PAPER_MARGIN;
603 x += lyxfont::signedWidth(textclasslist
604 .TextClass(bview->buffer()->params.textclass)
607 .TextClass(bview->buffer()->params.textclass)
610 // this is the way, LyX handles the LaTeX-Environments.
611 // I have had this idea very late, so it seems to be a
612 // later added hack and this is true
613 if (!row->par()->getDepth()) {
614 if (!row->par()->getLayout()) {
615 // find the previous same level paragraph
616 if (row->par()->previous()) {
617 Paragraph * newpar = row->par()
618 ->depthHook(row->par()->getDepth());
620 textclasslist.Style(bview->buffer()->params.textclass,
627 // find the next level paragraph
630 row->par()->outerHook();
632 // make a corresponding row. Needed to call LeftMargin()
634 // check wether it is a sufficent paragraph
637 .Style(bview->buffer()->params.textclass,
638 newpar->getLayout()).isEnvironment()) {
640 dummyrow.par(newpar);
641 dummyrow.pos(newpar->size());
642 x = leftMargin(bview, &dummyrow);
644 // this is no longer an error, because this function
645 // is used to clear impossible depths after changing
646 // a layout. Since there is always a redo,
647 // LeftMargin() is always called
648 row->par()->params().depth(0);
651 if (newpar && !row->par()->getLayout()) {
652 if (newpar->params().noindent())
655 parindent = textclasslist
656 .Style(bview->buffer()->params.textclass,
657 newpar->getLayout()).parindent;
662 LyXFont const labelfont = getLabelFont(bview->buffer(), row->par());
663 switch (layout.margintype) {
665 if (!layout.leftmargin.empty()) {
666 x += lyxfont::signedWidth(layout.leftmargin,
668 .TextClass(bview->buffer()->params.
672 if (!row->par()->getLabelstring().empty()) {
673 x += lyxfont::signedWidth(layout.labelindent,
675 x += lyxfont::width(row->par()->getLabelstring(),
677 x += lyxfont::width(layout.labelsep, labelfont);
681 x += lyxfont::signedWidth(layout.labelindent, labelfont);
682 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
683 if (!row->par()->getLabelWidthString().empty()) {
684 x += lyxfont::width(row->par()->getLabelWidthString(),
686 x += lyxfont::width(layout.labelsep, labelfont);
691 x += lyxfont::signedWidth(layout.leftmargin, textclasslist.TextClass(bview->buffer()->params.textclass).defaultfont()) * 4
692 / (row->par()->getDepth() + 4);
694 case MARGIN_FIRST_DYNAMIC:
695 if (layout.labeltype == LABEL_MANUAL) {
696 if (row->pos() >= beginningOfMainBody(bview->buffer(), row->par())) {
697 x += lyxfont::signedWidth(layout.leftmargin,
700 x += lyxfont::signedWidth(layout.labelindent,
703 } else if (row->pos()
704 // Special case to fix problems with
706 || (layout.labeltype == LABEL_STATIC
707 && layout.latextype == LATEX_ENVIRONMENT
708 && ! row->par()->isFirstInSequence())) {
709 x += lyxfont::signedWidth(layout.leftmargin,
711 } else if (layout.labeltype != LABEL_TOP_ENVIRONMENT
712 && layout.labeltype != LABEL_BIBLIO
713 && layout.labeltype !=
714 LABEL_CENTERED_TOP_ENVIRONMENT) {
715 x += lyxfont::signedWidth(layout.labelindent,
717 x += lyxfont::width(layout.labelsep, labelfont);
718 x += lyxfont::width(row->par()->getLabelstring(),
723 case MARGIN_RIGHT_ADDRESS_BOX:
725 // ok, a terrible hack. The left margin depends on the widest
726 // row in this paragraph. Do not care about footnotes, they
727 // are *NOT* allowed in the LaTeX realisation of this layout.
729 // find the first row of this paragraph
730 Row const * tmprow = row;
731 while (tmprow->previous()
732 && tmprow->previous()->par() == row->par())
733 tmprow = tmprow->previous();
735 int minfill = tmprow->fill();
736 while (tmprow->next() && tmprow->next()->par() == row->par()) {
737 tmprow = tmprow->next();
738 if (tmprow->fill() < minfill)
739 minfill = tmprow->fill();
742 x += lyxfont::signedWidth(layout.leftmargin,
744 .TextClass(bview->buffer()->params.textclass)
751 int align; // wrong type
753 if (row->par()->params().align() == LYX_ALIGN_LAYOUT)
754 align = layout.align;
756 align = row->par()->params().align();
758 // set the correct parindent
759 if (row->pos() == 0) {
760 if ((layout.labeltype == LABEL_NO_LABEL
761 || layout.labeltype == LABEL_TOP_ENVIRONMENT
762 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
763 || (layout.labeltype == LABEL_STATIC
764 && layout.latextype == LATEX_ENVIRONMENT
765 && ! row->par()->isFirstInSequence()))
766 && align == LYX_ALIGN_BLOCK
767 && !row->par()->params().noindent()
768 && (row->par()->layout ||
769 bview->buffer()->params.paragraph_separation ==
770 BufferParams::PARSEP_INDENT))
771 x += lyxfont::signedWidth(parindent,
773 .TextClass(bview->buffer()->params
776 else if (layout.labeltype == LABEL_BIBLIO) {
777 // ale970405 Right width for bibitems
778 x += bibitemMaxWidth(bview,textclasslist
779 .TextClass(bview->buffer()->params
788 int LyXText::rightMargin(Buffer const * buf, Row const * row) const
790 LyXLayout const & layout =
791 textclasslist.Style(buf->params.textclass,
792 row->par()->getLayout());
794 int x = LYX_PAPER_MARGIN
795 + lyxfont::signedWidth(textclasslist
796 .TextClass(buf->params.textclass)
799 .TextClass(buf->params.textclass)
802 // this is the way, LyX handles the LaTeX-Environments.
803 // I have had this idea very late, so it seems to be a
804 // later added hack and this is true
805 if (row->par()->getDepth()) {
806 // find the next level paragraph
808 Paragraph * newpar = row->par();
811 newpar = newpar->previous();
813 && newpar->getDepth() >= row->par()->getDepth());
815 // make a corresponding row. Needed to call LeftMargin()
817 // check wether it is a sufficent paragraph
819 && textclasslist.Style(buf->params.textclass,
823 dummyrow.par(newpar);
825 x = rightMargin(buf, &dummyrow);
827 // this is no longer an error, because this function
828 // is used to clear impossible depths after changing
829 // a layout. Since there is always a redo,
830 // LeftMargin() is always called
831 row->par()->params().depth(0);
835 //lyxerr << "rightmargin: " << layout->rightmargin << endl;
836 x += lyxfont::signedWidth(layout.rightmargin, textclasslist
837 .TextClass(buf->params.textclass)
838 .defaultfont()) * 4 / (row->par()->getDepth()
844 int LyXText::labelEnd(BufferView * bview, Row const * row) const
846 if (textclasslist.Style(bview->buffer()->params.textclass,
847 row->par()->getLayout()).margintype
851 tmprow.pos(row->par()->size());
852 return leftMargin(bview, &tmprow); /* just the beginning
855 return 0; /* LabelEnd is only needed, if the
856 layout fills a flushleft
861 // get the next breakpoint in a given paragraph
863 LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
865 Paragraph * par = row->par();
870 Paragraph::size_type const pos = row->pos();
873 // position of the last possible breakpoint
874 // -1 isn't a suitable value, but a flag
875 Paragraph::size_type last_separator = -1;
876 width -= rightMargin(bview->buffer(), row);
878 Paragraph::size_type const main_body =
879 beginningOfMainBody(bview->buffer(), par);
880 LyXLayout const & layout =
881 textclasslist.Style(bview->buffer()->params.textclass, par->getLayout());
882 Paragraph::size_type i = pos;
884 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
885 /* special code for right address boxes, only newlines count */
886 while (i < par->size()) {
887 if (par->isNewline(i)) {
889 i = par->size() - 1; // this means break
891 } else if (par->getChar(i) == Paragraph::META_INSET &&
892 par->getInset(i) && par->getInset(i)->display()){
893 par->getInset(i)->display(false);
898 // Last position is an invariant
899 Paragraph::size_type const last =
901 // this is the usual handling
902 int x = leftMargin(bview, row);
903 bool doitonetime = true;
904 while (doitonetime || ((x < width) && (i < last))) {
906 char const c = par->getChar(i);
907 if (IsNewlineChar(c)) {
909 x = width; // this means break
910 } else if (c == Paragraph::META_INSET &&
913 // check wether a Display() inset is
914 // valid here. if not, change it to
916 if (par->getInset(i)->display() &&
917 (layout.isCommand() ||
918 (layout.labeltype == LABEL_MANUAL
919 && i < beginningOfMainBody(bview->buffer(), par)))) {
920 // display istn't allowd
921 par->getInset(i)->display(false);
922 x += singleWidth(bview, par, i, c);
923 } else if (par->getInset(i)->display() ||
924 par->getInset(i)->needFullRow()) {
925 // So break the line here
929 if (IsLineSeparatorChar(par->getChar(i+1)))
932 last_separator = last; // to avoid extra rows
934 last_separator = i - 1;
935 x = width; // this means break
937 x += singleWidth(bview, par, i, c);
940 if (IsLineSeparatorChar(c))
942 x += singleWidth(bview, par, i, c);
945 if (i == main_body) {
946 x += lyxfont::width(layout.labelsep,
947 getLabelFont(bview->buffer(), par));
948 if (par->isLineSeparator(i - 1))
949 x-= singleWidth(bview, par, i - 1);
950 int left_margin = labelEnd(bview, row);
955 // end of paragraph is always a suitable separator
956 if (i == last && x < width)
960 // well, if last_separator is still 0, the line isn't breakable.
961 // don't care and cut simply at the end
962 if (last_separator < 0) {
966 // manual labels cannot be broken in LaTeX, do not care
967 if (main_body && last_separator < main_body)
968 last_separator = main_body - 1;
970 return last_separator;
974 // returns the minimum space a row needs on the screen in pixel
975 int LyXText::fill(BufferView * bview, Row * row, int paper_width) const
981 // get the pure distance
982 Paragraph::size_type const last = rowLastPrintable(row);
984 // special handling of the right address boxes
985 if (textclasslist.Style(bview->buffer()->params.textclass,
986 row->par()->getLayout()).margintype
987 == MARGIN_RIGHT_ADDRESS_BOX) {
988 int const tmpfill = row->fill();
989 row->fill(0); // the minfill in MarginLeft()
990 w = leftMargin(bview, row);
993 w = leftMargin(bview, row);
995 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
996 row->par()->getLayout());
997 Paragraph::size_type const main_body =
998 beginningOfMainBody(bview->buffer(), row->par());
999 Paragraph::size_type i = row->pos();
1002 if (main_body > 0 && i == main_body) {
1003 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1004 if (row->par()->isLineSeparator(i - 1))
1005 w -= singleWidth(bview, row->par(), i - 1);
1006 int left_margin = labelEnd(bview, row);
1007 if (w < left_margin)
1010 w += singleWidth(bview, row->par(), i);
1013 if (main_body > 0 && main_body > last) {
1014 w += lyxfont::width(layout.labelsep, getLabelFont(bview->buffer(), row->par()));
1015 if (last >= 0 && row->par()->isLineSeparator(last))
1016 w -= singleWidth(bview, row->par(), last);
1017 int const left_margin = labelEnd(bview, row);
1018 if (w < left_margin)
1022 int const fill = paper_width - w - rightMargin(bview->buffer(), row);
1023 #ifdef WITH_WARNINGS
1024 #warning Please fix me (Jug!)
1034 // returns the minimum space a manual label needs on the screen in pixel
1035 int LyXText::labelFill(BufferView * bview, Row const * row) const
1037 Paragraph::size_type last = beginningOfMainBody(bview->buffer(), row->par()) - 1;
1038 // -1 because a label ends either with a space that is in the label,
1039 // or with the beginning of a footnote that is outside the label.
1041 // I don't understand this code in depth, but sometimes "last" is
1042 // less than 0 and this causes a crash. This fix seems to work
1043 // correctly, but I bet the real error is elsewhere. The bug is
1044 // triggered when you have an open footnote in a paragraph
1045 // environment with a manual label. (Asger)
1046 if (last < 0) last = 0;
1048 if (row->par()->isLineSeparator(last)) /* a sepearator at this end
1055 w += singleWidth(bview, row->par(), i);
1060 if (!row->par()->params().labelWidthString().empty()) {
1061 fill = max(lyxfont::width(row->par()->params().labelWidthString(),
1062 getLabelFont(bview->buffer(), row->par())) - w,
1070 // returns the number of separators in the specified row. The separator
1071 // on the very last column doesnt count
1072 int LyXText::numberOfSeparators(Buffer const * buf, Row const * row) const
1074 Paragraph::size_type const last = rowLast(row);
1075 Paragraph::size_type p =
1076 max(row->pos(), beginningOfMainBody(buf, row->par()));
1078 for (; p < last; ++p) {
1079 if (row->par()->isSeparator(p)) {
1087 // returns the number of hfills in the specified row. The LyX-Hfill is
1088 // a LaTeX \hfill so that the hfills at the beginning and at the end were
1089 // ignored. This is *MUCH* more usefull than not to ignore!
1090 int LyXText::numberOfHfills(Buffer const * buf, Row const * row) const
1092 Paragraph::size_type const last = rowLast(row);
1093 Paragraph::size_type first = row->pos();
1094 if (first) { /* hfill *DO* count at the beginning
1096 while(first <= last && row->par()->isHfill(first))
1100 first = max(first, beginningOfMainBody(buf, row->par()));
1102 for (int p = first; p <= last; ++p) { // last, because the end is ignored!
1103 if (row->par()->isHfill(p)) {
1111 // like NumberOfHfills, but only those in the manual label!
1112 int LyXText::numberOfLabelHfills(Buffer const * buf, Row const * row) const
1114 Paragraph::size_type last = rowLast(row);
1115 Paragraph::size_type first = row->pos();
1116 if (first) { /* hfill *DO* count at the beginning
1118 while(first < last && row->par()->isHfill(first))
1122 last = min(last, beginningOfMainBody(buf, row->par()));
1124 for (Paragraph::size_type p = first;
1125 p < last; ++p) { // last, because the end is ignored!
1126 if (row->par()->isHfill(p)) {
1134 // returns true, if a expansion is needed.
1135 // Rules are given by LaTeX
1136 bool LyXText::hfillExpansion(Buffer const * buf, Row const * row_ptr,
1137 Paragraph::size_type pos) const
1139 // by the way, is it a hfill?
1140 if (!row_ptr->par()->isHfill(pos))
1143 // at the end of a row it does not count
1144 if (pos >= rowLast(row_ptr))
1147 // at the beginning of a row it does not count, if it is not
1148 // the first row of a paragaph
1149 if (!row_ptr->pos())
1152 // in some labels it does not count
1153 if (textclasslist.Style(buf->params.textclass,
1154 row_ptr->par()->getLayout()).margintype
1156 && pos < beginningOfMainBody(buf, row_ptr->par()))
1159 // if there is anything between the first char of the row and
1160 // the sepcified position that is not a newline and not a hfill,
1161 // the hfill will count, otherwise not
1162 Paragraph::size_type i = row_ptr->pos();
1163 while (i < pos && (row_ptr->par()->isNewline(i)
1164 || row_ptr->par()->isHfill(i)))
1171 LColor::color LyXText::backgroundColor()
1174 return inset_owner->backgroundColor();
1176 return LColor::background;
1179 void LyXText::setHeightOfRow(BufferView * bview, Row * row_ptr) const
1181 /* get the maximum ascent and the maximum descent */
1184 float layoutasc = 0;
1185 float layoutdesc = 0;
1188 Inset * tmpinset = 0;
1190 /* this must not happen before the currentrow for clear reasons.
1191 so the trick is just to set the current row onto this row */
1193 getRow(row_ptr->par(), row_ptr->pos(), unused_y);
1195 /* ok , let us initialize the maxasc and maxdesc value.
1196 * This depends in LaTeX of the font of the last character
1197 * in the paragraph. The hack below is necessary because
1198 * of the possibility of open footnotes */
1200 /* Correction: only the fontsize count. The other properties
1201 are taken from the layoutfont. Nicer on the screen :) */
1202 Paragraph * par = row_ptr->par();
1203 Paragraph * firstpar = row_ptr->par();
1205 LyXLayout const & layout = textclasslist.Style(bview->buffer()->params.textclass,
1206 firstpar->getLayout());
1208 // as max get the first character of this row then it can increes but not
1209 // decrees the height. Just some point to start with so we don't have to
1210 // do the assignment below too often.
1211 LyXFont font = getFont(bview->buffer(), par, row_ptr->pos());
1212 LyXFont::FONT_SIZE const tmpsize = font.size();
1213 font = getLayoutFont(bview->buffer(), par);
1214 LyXFont::FONT_SIZE const size = font.size();
1215 font.setSize(tmpsize);
1217 LyXFont labelfont = getLabelFont(bview->buffer(), par);
1219 float spacing_val = 1.0;
1220 if (!row_ptr->par()->params().spacing().isDefault()) {
1221 spacing_val = row_ptr->par()->params().spacing().getValue();
1223 spacing_val = bview->buffer()->params.spacing.getValue();
1225 //lyxerr << "spacing_val = " << spacing_val << endl;
1227 int maxasc = int(lyxfont::maxAscent(font) *
1228 layout.spacing.getValue() *
1230 int maxdesc = int(lyxfont::maxDescent(font) *
1231 layout.spacing.getValue() *
1233 int const pos_end = rowLast(row_ptr);
1237 // Check if any insets are larger
1238 for (int pos = row_ptr->pos(); pos <= pos_end; ++pos) {
1239 if (row_ptr->par()->getChar(pos) == Paragraph::META_INSET) {
1240 tmpfont = getFont(bview->buffer(), row_ptr->par(), pos);
1241 tmpinset = row_ptr->par()->getInset(pos);
1243 #if 1 // this is needed for deep update on initialitation
1244 tmpinset->update(bview, tmpfont);
1246 asc = tmpinset->ascent(bview, tmpfont);
1247 desc = tmpinset->descent(bview, tmpfont);
1248 maxwidth += tmpinset->width(bview, tmpfont);
1249 maxasc = max(maxasc, asc);
1250 maxdesc = max(maxdesc, desc);
1253 maxwidth += singleWidth(bview, row_ptr->par(), pos);
1257 // Check if any custom fonts are larger (Asger)
1258 // This is not completely correct, but we can live with the small,
1259 // cosmetic error for now.
1260 LyXFont::FONT_SIZE maxsize =
1261 row_ptr->par()->highestFontInRange(row_ptr->pos(), pos_end, size);
1262 if (maxsize > font.size()) {
1263 font.setSize(maxsize);
1265 asc = lyxfont::maxAscent(font);
1266 desc = lyxfont::maxDescent(font);
1273 // This is nicer with box insets:
1277 row_ptr->ascent_of_text(maxasc);
1279 // is it a top line?
1280 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
1282 // some parksips VERY EASY IMPLEMENTATION
1283 if (bview->buffer()->params.paragraph_separation ==
1284 BufferParams::PARSEP_SKIP)
1286 if (layout.isParagraph()
1287 && firstpar->getDepth() == 0
1288 && firstpar->previous())
1290 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1291 } else if (firstpar->previous() &&
1292 textclasslist.Style(bview->buffer()->params.textclass,
1293 firstpar->previous()->
1294 getLayout()).isParagraph() &&
1295 firstpar->previous()->getDepth() == 0)
1297 // is it right to use defskip here too? (AS)
1298 maxasc += bview->buffer()->params.getDefSkip().inPixels(bview);
1302 // the paper margins
1303 if (!row_ptr->par()->previous() && bv_owner)
1304 maxasc += LYX_PAPER_MARGIN;
1306 // add the vertical spaces, that the user added
1307 if (firstpar->params().spaceTop().kind() != VSpace::NONE)
1308 maxasc += int(firstpar->params().spaceTop().inPixels(bview));
1310 // do not forget the DTP-lines!
1311 // there height depends on the font of the nearest character
1312 if (firstpar->params().lineTop())
1313 maxasc += 2 * lyxfont::ascent('x', getFont(bview->buffer(),
1316 // and now the pagebreaks
1317 if (firstpar->params().pagebreakTop())
1318 maxasc += 3 * defaultHeight();
1320 // This is special code for the chapter, since the label of this
1321 // layout is printed in an extra row
1322 if (layout.labeltype == LABEL_COUNTER_CHAPTER
1323 && bview->buffer()->params.secnumdepth >= 0)
1325 float spacing_val = 1.0;
1326 if (!row_ptr->par()->params().spacing().isDefault()) {
1327 spacing_val = row_ptr->par()->params().spacing().getValue();
1329 spacing_val = bview->buffer()->params.spacing.getValue();
1332 labeladdon = int(lyxfont::maxDescent(labelfont) *
1333 layout.spacing.getValue() *
1335 + int(lyxfont::maxAscent(labelfont) *
1336 layout.spacing.getValue() *
1340 // special code for the top label
1341 if ((layout.labeltype == LABEL_TOP_ENVIRONMENT
1342 || layout.labeltype == LABEL_BIBLIO
1343 || layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1344 && row_ptr->par()->isFirstInSequence()
1345 && !row_ptr->par()->getLabelstring().empty())
1347 float spacing_val = 1.0;
1348 if (!row_ptr->par()->params().spacing().isDefault()) {
1349 spacing_val = row_ptr->par()->params().spacing().getValue();
1351 spacing_val = bview->buffer()->params.spacing.getValue();
1355 (lyxfont::maxAscent(labelfont) *
1356 layout.spacing.getValue() *
1358 +(lyxfont::maxDescent(labelfont) *
1359 layout.spacing.getValue() *
1361 + layout.topsep * defaultHeight()
1362 + layout.labelbottomsep * defaultHeight());
1365 // and now the layout spaces, for example before and after a section,
1366 // or between the items of a itemize or enumerate environment
1368 if (!firstpar->params().pagebreakTop()) {
1369 Paragraph * prev = row_ptr->par()->previous();
1371 prev = row_ptr->par()->depthHook(row_ptr->par()->getDepth());
1372 if (prev && prev->getLayout() == firstpar->getLayout() &&
1373 prev->getDepth() == firstpar->getDepth() &&
1374 prev->getLabelWidthString() == firstpar->getLabelWidthString())
1376 layoutasc = (layout.itemsep * defaultHeight());
1377 } else if (row_ptr->previous()) {
1378 tmptop = layout.topsep;
1380 if (row_ptr->previous()->par()->getDepth() >= row_ptr->par()->getDepth())
1381 tmptop -= textclasslist.Style(bview->buffer()->params.textclass,
1382 row_ptr->previous()->par()->
1383 getLayout()).bottomsep;
1386 layoutasc = (tmptop * defaultHeight());
1387 } else if (row_ptr->par()->params().lineTop()) {
1388 tmptop = layout.topsep;
1391 layoutasc = (tmptop * defaultHeight());
1394 prev = row_ptr->par()->outerHook();
1396 maxasc += int(textclasslist.Style(bview->buffer()->params.textclass,
1397 prev->getLayout()).parsep * defaultHeight());
1399 if (firstpar->previous() &&
1400 firstpar->previous()->getDepth() == 0 &&
1401 firstpar->previous()->getLayout() !=
1402 firstpar->getLayout())
1405 } else if (firstpar->previous()) {
1406 maxasc += int(layout.parsep * defaultHeight());
1412 // is it a bottom line?
1413 if (row_ptr->par() == par
1414 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par()))
1416 // the paper margins
1417 if (!par->next() && bv_owner)
1418 maxdesc += LYX_PAPER_MARGIN;
1420 // add the vertical spaces, that the user added
1421 if (firstpar->params().spaceBottom().kind() != VSpace::NONE)
1422 maxdesc += int(firstpar->params().spaceBottom().inPixels(bview));
1424 // do not forget the DTP-lines!
1425 // there height depends on the font of the nearest character
1426 if (firstpar->params().lineBottom())
1427 maxdesc += 2 * lyxfont::ascent('x',
1428 getFont(bview->buffer(),
1429 par, par->size() - 1));
1431 // and now the pagebreaks
1432 if (firstpar->params().pagebreakBottom())
1433 maxdesc += 3 * defaultHeight();
1435 // and now the layout spaces, for example before and after
1436 // a section, or between the items of a itemize or enumerate
1438 if (!firstpar->params().pagebreakBottom() && row_ptr->par()->next()) {
1439 Paragraph * nextpar = row_ptr->par()->next();
1440 Paragraph * comparepar = row_ptr->par();
1444 if (comparepar->getDepth() > nextpar->getDepth()) {
1445 usual = (textclasslist.Style(bview->buffer()->params.textclass,
1446 comparepar->getLayout()).bottomsep * defaultHeight());
1447 comparepar = comparepar->depthHook(nextpar->getDepth());
1448 if (comparepar->getLayout()!= nextpar->getLayout()
1449 || nextpar->getLabelWidthString() !=
1450 comparepar->getLabelWidthString())
1452 unusual = (textclasslist.Style(bview->buffer()->params.textclass,
1453 comparepar->getLayout()).bottomsep * defaultHeight());
1455 if (unusual > usual)
1456 layoutdesc = unusual;
1459 } else if (comparepar->getDepth() == nextpar->getDepth()) {
1461 if (comparepar->getLayout()!= nextpar->getLayout()
1462 || nextpar->getLabelWidthString() !=
1463 comparepar->getLabelWidthString())
1464 layoutdesc = int(textclasslist.Style(bview->buffer()->params.textclass,
1465 comparepar->getLayout()).bottomsep * defaultHeight());
1470 // incalculate the layout spaces
1471 maxasc += int(layoutasc * 2 / (2 + firstpar->getDepth()));
1472 maxdesc += int(layoutdesc * 2 / (2 + firstpar->getDepth()));
1474 // calculate the new height of the text
1475 height -= row_ptr->height();
1477 row_ptr->height(maxasc + maxdesc + labeladdon);
1478 row_ptr->baseline(maxasc + labeladdon);
1480 height += row_ptr->height();
1483 prepareToPrint(bview, row_ptr, x, dummy, dummy, dummy, false);
1484 row_ptr->width(int(maxwidth + x));
1487 width = max(0,workWidth(bview));
1489 if (r->width() > width)
1497 /* Appends the implicit specified paragraph behind the specified row,
1498 * start at the implicit given position */
1499 void LyXText::appendParagraph(BufferView * bview, Row * row) const
1501 bool not_ready = true;
1503 // The last character position of a paragraph is an invariant so we can
1504 // safely get it here. (Asger)
1505 int const lastposition = row->par()->size();
1507 // Get the next breakpoint
1508 int z = nextBreakPoint(bview, row, workWidth(bview));
1512 // Insert the new row
1513 if (z < lastposition) {
1515 insertRow(row, row->par(), z);
1522 // Set the dimensions of the row
1523 #ifdef WITH_WARNINGS
1524 #warning Something is rotten here! (Jug)
1526 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1527 setHeightOfRow(bview, tmprow);
1529 } while (not_ready);
1533 void LyXText::breakAgain(BufferView * bview, Row * row) const
1535 bool not_ready = true;
1538 // get the next breakpoint
1539 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1542 if (z < row->par()->size()) {
1543 if (!row->next() || (row->next() && row->next()->par() != row->par())) {
1546 insertRow(row, row->par(), z);
1552 if (row->pos() == z)
1553 not_ready = false; // the rest will not change
1559 /* if there are some rows too much, delete them */
1560 /* only if you broke the whole paragraph! */
1561 Row * tmprow2 = row;
1562 while (tmprow2->next() && tmprow2->next()->par() == row->par()) {
1563 tmprow2 = tmprow2->next();
1565 while (tmprow2 != row) {
1566 tmprow2 = tmprow2->previous();
1567 removeRow(tmprow2->next());
1572 /* set the dimensions of the row */
1573 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1574 setHeightOfRow(bview, tmprow);
1575 } while (not_ready);
1579 // this is just a little changed version of break again
1580 void LyXText::breakAgainOneRow(BufferView * bview, Row * row)
1582 // get the next breakpoint
1583 Paragraph::size_type z = nextBreakPoint(bview, row, workWidth(bview));
1586 if (z < row->par()->size()) {
1588 || (row->next() && row->next()->par() != row->par())) {
1589 /* insert a new row */
1591 insertRow(row, row->par(), z);
1597 if (row->pos() != z)
1601 // if there are some rows too much, delete them
1602 // only if you broke the whole paragraph!
1603 Row * tmprow2 = row;
1604 while (tmprow2->next()
1605 && tmprow2->next()->par() == row->par()) {
1606 tmprow2 = tmprow2->next();
1608 while (tmprow2 != row) {
1609 tmprow2 = tmprow2->previous();
1610 removeRow(tmprow2->next());
1614 // set the dimensions of the row
1615 tmprow->fill(fill(bview, tmprow, workWidth(bview)));
1616 setHeightOfRow(bview, tmprow);
1620 void LyXText::breakParagraph(BufferView * bview, char keep_layout)
1622 LyXLayout const & layout =
1623 textclasslist.Style(bview->buffer()->params.textclass,
1624 cursor.par()->getLayout());
1626 // this is only allowed, if the current paragraph is not empty or caption
1627 if ((cursor.par()->size() <= 0)
1628 && layout.labeltype!= LABEL_SENSITIVE)
1631 setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next());
1633 // Always break behind a space
1635 // It is better to erase the space (Dekel)
1636 if (cursor.pos() < cursor.par()->size()
1637 && cursor.par()->isLineSeparator(cursor.pos()))
1638 cursor.par()->erase(cursor.pos());
1639 // cursor.pos(cursor.pos() + 1);
1641 // break the paragraph
1645 keep_layout = layout.isEnvironment();
1646 cursor.par()->breakParagraph(bview->buffer()->params, cursor.pos(),
1649 // well this is the caption hack since one caption is really enough
1650 if (layout.labeltype == LABEL_SENSITIVE) {
1652 // set to standard-layout
1653 cursor.par()->setLayout(0);
1655 // set to standard-layout
1656 cursor.par()->next()->setLayout(0);
1659 /* if the cursor is at the beginning of a row without prior newline,
1661 * This touches only the screen-update. Otherwise we would may have
1662 * an empty row on the screen */
1663 if (cursor.pos() && !cursor.row()->par()->isNewline(cursor.row()->pos() - 1)
1664 && cursor.row()->pos() == cursor.pos()) {
1668 status(bview, LyXText::NEED_MORE_REFRESH);
1669 refresh_row = cursor.row();
1670 refresh_y = cursor.y() - cursor.row()->baseline();
1672 // Do not forget the special right address boxes
1673 if (layout.margintype == MARGIN_RIGHT_ADDRESS_BOX) {
1674 while (refresh_row->previous() &&
1675 refresh_row->previous()->par() == refresh_row->par()) {
1676 refresh_row = refresh_row->previous();
1677 refresh_y -= refresh_row->height();
1680 removeParagraph(cursor.row());
1682 // set the dimensions of the cursor row
1683 cursor.row()->fill(fill(bview, cursor.row(), workWidth(bview)));
1685 setHeightOfRow(bview, cursor.row());
1687 while (cursor.par()->next()->size()
1688 && cursor.par()->next()->isNewline(0))
1689 cursor.par()->next()->erase(0);
1691 insertParagraph(bview, cursor.par()->next(), cursor.row());
1693 updateCounters(bview, cursor.row()->previous());
1695 /* This check is necessary. Otherwise the new empty paragraph will
1696 * be deleted automatically. And it is more friendly for the user! */
1698 setCursor(bview, cursor.par()->next(), 0);
1700 setCursor(bview, cursor.par(), 0);
1702 if (cursor.row()->next())
1703 breakAgain(bview, cursor.row()->next());
1709 // Just a macro to make some thing easier.
1710 void LyXText::redoParagraph(BufferView * bview) const
1713 redoParagraphs(bview, cursor, cursor.par()->next());
1714 setCursorIntern(bview, cursor.par(), cursor.pos());
1718 /* insert a character, moves all the following breaks in the
1719 * same Paragraph one to the right and make a rebreak */
1720 void LyXText::insertChar(BufferView * bview, char c)
1722 setUndo(bview, Undo::INSERT,
1723 cursor.par(), cursor.par()->next());
1725 // When the free-spacing option is set for the current layout,
1726 // disable the double-space checking
1728 bool const freeSpacing =
1729 textclasslist.Style(bview->buffer()->params.textclass,
1730 cursor.row()->par()->getLayout()).free_spacing;
1733 if (lyxrc.auto_number) {
1734 static string const number_operators = "+-/*";
1735 static string const number_unary_operators = "+-";
1736 static string const number_seperators = ".,:";
1738 if (current_font.number() == LyXFont::ON) {
1739 if (!isdigit(c) && !contains(number_operators, c) &&
1740 !(contains(number_seperators, c) &&
1741 cursor.pos() >= 1 &&
1742 cursor.pos() < cursor.par()->size() &&
1743 getFont(bview->buffer(),
1745 cursor.pos()).number() == LyXFont::ON &&
1746 getFont(bview->buffer(),
1748 cursor.pos() - 1).number() == LyXFont::ON)
1750 number(bview); // Set current_font.number to OFF
1751 } else if (isdigit(c) &&
1752 real_current_font.isVisibleRightToLeft()) {
1753 number(bview); // Set current_font.number to ON
1755 if (cursor.pos() > 0) {
1756 char const c = cursor.par()->getChar(cursor.pos() - 1);
1757 if (contains(number_unary_operators, c) &&
1758 (cursor.pos() == 1 ||
1759 cursor.par()->isSeparator(cursor.pos() - 2) ||
1760 cursor.par()->isNewline(cursor.pos() - 2) )
1762 setCharFont(bview->buffer(),
1766 } else if (contains(number_seperators, c) &&
1767 cursor.pos() >= 2 &&
1768 getFont(bview->buffer(),
1770 cursor.pos() - 2).number() == LyXFont::ON) {
1771 setCharFont(bview->buffer(),
1781 /* First check, if there will be two blanks together or a blank at
1782 the beginning of a paragraph.
1783 I decided to handle blanks like normal characters, the main
1784 difference are the special checks when calculating the row.fill
1785 (blank does not count at the end of a row) and the check here */
1787 // The bug is triggered when we type in a description environment:
1788 // The current_font is not changed when we go from label to main text
1789 // and it should (along with realtmpfont) when we type the space.
1790 // CHECK There is a bug here! (Asger)
1792 LyXFont realtmpfont = real_current_font;
1793 LyXFont rawtmpfont = current_font; /* store the current font.
1794 * This is because of the use
1795 * of cursor movements. The moving
1796 * cursor would refresh the
1799 // Get the font that is used to calculate the baselineskip
1800 Paragraph::size_type const lastpos = cursor.par()->size();
1801 LyXFont rawparfont =
1802 cursor.par()->getFontSettings(bview->buffer()->params,
1805 bool jumped_over_space = false;
1807 if (!freeSpacing && IsLineSeparatorChar(c)) {
1808 if ((cursor.pos() > 0
1809 && cursor.par()->isLineSeparator(cursor.pos() - 1))
1810 || (cursor.pos() > 0
1811 && cursor.par()->isNewline(cursor.pos() - 1))
1812 || (cursor.pos() == 0)) {
1813 static bool sent_space_message = false;
1814 if (!sent_space_message) {
1815 if (cursor.pos() == 0)
1816 bview->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1818 bview->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1819 sent_space_message = true;
1824 } else if (IsNewlineChar(c)) {
1825 if (cursor.par() == cursor.par()
1826 && cursor.pos() <= beginningOfMainBody(bview->buffer(), cursor.par())) {
1830 /* No newline at first position
1831 * of a paragraph or behind labels.
1832 * TeX does not allow that. */
1834 if (cursor.pos() < cursor.par()->size() &&
1835 cursor.par()->isLineSeparator(cursor.pos()))
1836 // newline always after a blank!
1838 cursor.row()->fill(-1); // to force a new break
1841 // the display inset stuff
1842 if (cursor.row()->par()->getChar(cursor.row()->pos()) == Paragraph::META_INSET
1843 && cursor.row()->par()->getInset(cursor.row()->pos())
1844 && (cursor.row()->par()->getInset(cursor.row()->pos())->display() ||
1845 cursor.row()->par()->getInset(cursor.row()->pos())->needFullRow()))
1846 cursor.row()->fill(-1); // to force a new break
1848 // get the cursor row fist
1849 Row * row = cursor.row();
1850 int y = cursor.y() - row->baseline();
1851 if (c != Paragraph::META_INSET) /* Here case LyXText::InsertInset
1852 * already insertet the character */
1853 cursor.par()->insertChar(cursor.pos(), c);
1854 setCharFont(bview->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1856 if (!jumped_over_space) {
1857 // refresh the positions
1859 while (tmprow->next() && tmprow->next()->par() == row->par()) {
1860 tmprow = tmprow->next();
1861 tmprow->pos(tmprow->pos() + 1);
1865 // Is there a break one row above
1866 if ((cursor.par()->isLineSeparator(cursor.pos())
1867 || cursor.par()->isNewline(cursor.pos())
1868 || cursor.row()->fill() == -1)
1869 && row->previous() && row->previous()->par() == row->par()) {
1870 Paragraph::size_type z = nextBreakPoint(bview,
1873 if (z >= row->pos()) {
1876 // set the dimensions of the row above
1877 row->previous()->fill(fill(bview,
1881 setHeightOfRow(bview, row->previous());
1883 y -= row->previous()->height();
1885 refresh_row = row->previous();
1886 status(bview, LyXText::NEED_MORE_REFRESH);
1888 breakAgainOneRow(bview, row);
1890 current_font = rawtmpfont;
1891 real_current_font = realtmpfont;
1892 setCursor(bview, cursor.par(), cursor.pos() + 1,
1893 false, cursor.boundary());
1894 // cursor MUST be in row now.
1896 if (row->next() && row->next()->par() == row->par())
1897 need_break_row = row->next();
1901 // check, wether the last characters font has changed.
1902 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1903 && rawparfont != rawtmpfont)
1904 redoHeightOfParagraph(bview, cursor);
1911 // recalculate the fill of the row
1912 if (row->fill() >= 0) /* needed because a newline
1913 * will set fill to -1. Otherwise
1914 * we would not get a rebreak! */
1915 row->fill(fill(bview, row, workWidth(bview)));
1916 if (row->fill() < 0) {
1919 refresh_x = cursor.x();
1920 refresh_pos = cursor.pos();
1921 status(bview, LyXText::NEED_MORE_REFRESH);
1922 breakAgainOneRow(bview, row);
1923 // will the cursor be in another row now?
1924 if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
1925 if (row->next() && row->next()->par() == row->par())
1926 // this should always be true
1928 breakAgainOneRow(bview, row);
1930 current_font = rawtmpfont;
1931 real_current_font = realtmpfont;
1933 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1935 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
1936 != cursor.boundary())
1937 setCursor(bview, cursor.par(), cursor.pos(), false,
1938 !cursor.boundary());
1939 if (row->next() && row->next()->par() == row->par())
1940 need_break_row = row->next();
1945 refresh_x = cursor.x();
1947 refresh_pos = cursor.pos();
1949 int const tmpheight = row->height();
1950 setHeightOfRow(bview, row);
1951 if (tmpheight == row->height())
1952 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
1954 status(bview, LyXText::NEED_MORE_REFRESH);
1956 current_font = rawtmpfont;
1957 real_current_font = realtmpfont;
1958 setCursor(bview, cursor.par(), cursor.pos() + 1, false,
1962 // check, wether the last characters font has changed.
1963 if (cursor.pos() && cursor.pos() == cursor.par()->size()
1964 && rawparfont != rawtmpfont) {
1965 redoHeightOfParagraph(bview, cursor);
1967 // now the special right address boxes
1968 if (textclasslist.Style(bview->buffer()->params.textclass,
1969 cursor.par()->getLayout()).margintype
1970 == MARGIN_RIGHT_ADDRESS_BOX) {
1971 redoDrawingOfParagraph(bview, cursor);
1979 void LyXText::charInserted()
1981 // Here we could call FinishUndo for every 20 characters inserted.
1982 // This is from my experience how emacs does it.
1983 static unsigned int counter;
1993 void LyXText::prepareToPrint(BufferView * bview,
1994 Row * row, float & x,
1995 float & fill_separator,
1997 float & fill_label_hfill,
2003 float w = row->fill();
2005 fill_label_hfill = 0;
2007 fill_label_hfill = 0;
2010 row->par()->isRightToLeftPar(bview->buffer()->params);
2012 x = (workWidth(bview) > 0)
2013 ? rightMargin(bview->buffer(), row) : 0;
2015 x = (workWidth(bview) > 0) ? leftMargin(bview, row) : 0;
2017 // is there a manual margin with a manual label
2018 if (textclasslist.Style(bview->buffer()->params.textclass,
2019 row->par()->getLayout()).margintype == MARGIN_MANUAL
2020 && textclasslist.Style(bview->buffer()->params.textclass,
2021 row->par()->getLayout()).labeltype == LABEL_MANUAL) {
2023 /* one more since labels are left aligned */
2024 nlh = numberOfLabelHfills(bview->buffer(), row) + 1;
2025 if (nlh && !row->par()->getLabelWidthString().empty()) {
2026 fill_label_hfill = labelFill(bview, row) / nlh;
2030 // are there any hfills in the row?
2031 float const nh = numberOfHfills(bview->buffer(), row);
2035 fill_hfill = w / nh;
2037 // is it block, flushleft or flushright?
2038 // set x how you need it
2040 if (row->par()->params().align() == LYX_ALIGN_LAYOUT) {
2041 align = textclasslist.Style(bview->buffer()->params.textclass, row->par()->getLayout()).align;
2043 align = row->par()->params().align();
2046 // center displayed insets
2048 if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
2049 && (inset=row->par()->getInset(row->pos()))
2050 && (inset->display())) // || (inset->scroll() < 0)))
2051 align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
2052 ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
2055 case LYX_ALIGN_BLOCK:
2056 ns = numberOfSeparators(bview->buffer(), row);
2057 if (ns && row->next() && row->next()->par() == row->par() &&
2058 !(row->next()->par()->isNewline(row->next()->pos() - 1))
2059 && !(row->next()->par()->getChar(row->next()->pos()) == Paragraph::META_INSET
2060 && row->next()->par()->getInset(row->next()->pos())
2061 && row->next()->par()->getInset(row->next()->pos())->display())
2064 fill_separator = w / ns;
2065 } else if (is_rtl) {
2069 case LYX_ALIGN_RIGHT:
2072 case LYX_ALIGN_CENTER:
2080 computeBidiTables(bview->buffer(), row);
2082 Paragraph::size_type main_body =
2083 beginningOfMainBody(bview->buffer(), row->par());
2084 Paragraph::size_type last = rowLast(row);
2086 if (main_body > 0 &&
2087 (main_body-1 > last ||
2088 !row->par()->isLineSeparator(main_body-1))) {
2089 LyXLayout const & layout =
2090 textclasslist.Style(bview->buffer()->params.textclass,
2091 row->par()->getLayout());
2092 x += lyxfont::width(layout.labelsep,
2093 getLabelFont(bview->buffer(), row->par()));
2094 if (main_body-1 <= last)
2095 x += fill_label_hfill;
2100 /* important for the screen */
2103 /* the cursor set functions have a special mechanism. When they
2104 * realize, that you left an empty paragraph, they will delete it.
2105 * They also delete the corresponding row */
2107 void LyXText::cursorRightOneWord(BufferView * bview) const
2109 // treat floats, HFills and Insets as words
2110 LyXCursor tmpcursor = cursor;
2111 // CHECK See comment on top of text.C
2113 if (tmpcursor.pos() == tmpcursor.par()->size()
2114 && tmpcursor.par()->next()) {
2115 tmpcursor.par(tmpcursor.par()->next());
2120 // Skip through initial nonword stuff.
2121 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2122 ! tmpcursor.par()->isWord(tmpcursor.pos())) {
2123 // printf("Current pos1 %d", tmpcursor.pos()) ;
2124 tmpcursor.pos(tmpcursor.pos() + 1);
2127 // Advance through word.
2128 while (tmpcursor.pos() < tmpcursor.par()->size() &&
2129 tmpcursor.par()->isWord( tmpcursor.pos())) {
2130 // printf("Current pos2 %d", tmpcursor.pos()) ;
2131 tmpcursor.pos(tmpcursor.pos() + 1);
2135 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2139 void LyXText::cursorTab(BufferView * bview) const
2141 LyXCursor tmpcursor = cursor;
2142 while (tmpcursor.pos() < tmpcursor.par()->size()
2143 && !tmpcursor.par()->isNewline(tmpcursor.pos()))
2144 tmpcursor.pos(tmpcursor.pos() + 1);
2146 if (tmpcursor.pos() == tmpcursor.par()->size()){
2147 if (tmpcursor.par()->next()) {
2148 tmpcursor.par(tmpcursor.par()->next());
2152 tmpcursor.pos(tmpcursor.pos() + 1);
2153 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2157 /* -------> Skip initial whitespace at end of word and move cursor to *start*
2158 of prior word, not to end of next prior word. */
2160 void LyXText::cursorLeftOneWord(BufferView * bview) const
2162 LyXCursor tmpcursor = cursor;
2163 cursorLeftOneWord(tmpcursor);
2164 setCursor(bview, tmpcursor.par(), tmpcursor.pos());
2167 void LyXText::cursorLeftOneWord(LyXCursor & cur) const
2169 // treat HFills, floats and Insets as words
2172 && (cur.par()->isSeparator(cur.pos() - 1)
2173 || cur.par()->isKomma(cur.pos() - 1))
2174 && !(cur.par()->isHfill(cur.pos() - 1)
2175 || cur.par()->isInset(cur.pos() - 1)))
2176 cur.pos(cur.pos() - 1);
2179 && (cur.par()->isInset(cur.pos() - 1)
2180 || cur.par()->isHfill(cur.pos() - 1))) {
2181 cur.pos(cur.pos() - 1);
2182 } else if (!cur.pos()) {
2183 if (cur.par()->previous()){
2184 cur.par(cur.par()->previous());
2185 cur.pos(cur.par()->size());
2187 } else { // Here, cur != 0
2188 while (cur.pos() > 0 &&
2189 cur.par()->isWord(cur.pos()-1) )
2190 cur.pos(cur.pos() - 1);
2194 /* -------> Select current word. This depends on behaviour of
2195 CursorLeftOneWord(), so it is patched as well. */
2196 void LyXText::getWord(LyXCursor & from, LyXCursor & to,
2197 word_location const loc) const
2199 // first put the cursor where we wana start to select the word
2202 case WHOLE_WORD_STRICT:
2203 if (cursor.pos() == 0 || cursor.pos() == cursor.par()->size()
2204 || cursor.par()->isSeparator(cursor.pos())
2205 || cursor.par()->isKomma(cursor.pos())
2206 || cursor.par()->isSeparator(cursor.pos() -1)
2207 || cursor.par()->isKomma(cursor.pos() -1)) {
2211 // no break here, we go to the next
2214 // Move cursor to the beginning, when not already there.
2215 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
2216 && !from.par()->isKomma(from.pos() - 1))
2217 cursorLeftOneWord(from);
2220 // always move the cursor to the beginning of previous word
2221 cursorLeftOneWord(from);
2224 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
2230 while (to.pos() < to.par()->size()
2231 && !to.par()->isSeparator(to.pos())
2232 && !to.par()->isKomma(to.pos())
2233 && !to.par()->isHfill(to.pos()) )
2235 to.pos(to.pos() + 1);
2240 void LyXText::selectWord(BufferView * bview, word_location const loc)
2244 getWord(from, to, loc);
2246 setCursor(bview, from.par(), from.pos());
2249 selection.cursor = cursor;
2250 setCursor(bview, to.par(), to.pos() );
2251 setSelection(bview);
2255 /* -------> Select the word currently under the cursor when no
2256 selection is currently set */
2257 bool LyXText::selectWordWhenUnderCursor(BufferView * bview,
2258 word_location const loc)
2260 if (!selection.set()) {
2261 selectWord(bview, loc);
2262 return selection.set();
2268 // This function is only used by the spellchecker for NextWord().
2269 // It doesn't handle LYX_ACCENTs and probably never will.
2270 string const LyXText::selectNextWord(BufferView * bview,
2271 float & value) const
2273 if (the_locking_inset) {
2274 string str = the_locking_inset->selectNextWord(bview, value);
2276 value += float(cursor.y())/float(height);
2279 #warning Dekel please have a look on this one RTL? (Jug)
2280 // we have to go on checking so move cusor to the right
2281 if (cursor.pos() == cursor.par()->size()) {
2282 if (!cursor.par()->next())
2284 cursor.par(cursor.par()->next());
2287 cursor.pos(cursor.pos() + 1);
2289 Paragraph * tmppar = cursor.par();
2291 // If this is not the very first word, skip rest of
2292 // current word because we are probably in the middle
2293 // of a word if there is text here.
2294 if (cursor.pos() || cursor.par()->previous()) {
2295 while (cursor.pos() < cursor.par()->size()
2296 && cursor.par()->isLetter(cursor.pos()))
2297 cursor.pos(cursor.pos() + 1);
2300 // Now, skip until we have real text (will jump paragraphs)
2301 while ((cursor.par()->size() > cursor.pos()
2302 && (!cursor.par()->isLetter(cursor.pos()))
2303 && (!cursor.par()->isInset(cursor.pos()) ||
2304 !cursor.par()->getInset(cursor.pos())->isTextInset()))
2305 || (cursor.par()->size() == cursor.pos()
2306 && cursor.par()->next()))
2308 if (cursor.pos() == cursor.par()->size()) {
2309 cursor.par(cursor.par()->next());
2312 cursor.pos(cursor.pos() + 1);
2315 // now check if we hit an inset so it has to be a inset containing text!
2316 if (cursor.pos() < cursor.par()->size() &&
2317 cursor.par()->isInset(cursor.pos()))
2320 cursor.par()->getInset(cursor.pos())->edit(bview);
2321 // now call us again to do the above trick
2322 // but obviously we have to start from down below ;)
2323 return bview->text->selectNextWord(bview, value);
2326 // Update the value if we changed paragraphs
2327 if (cursor.par() != tmppar){
2328 setCursor(bview, cursor.par(), cursor.pos());
2329 value = float(cursor.y())/float(height);
2332 // Start the selection from here
2333 selection.cursor = cursor;
2337 // and find the end of the word
2338 // (optional hyphens are part of a word)
2339 while (cursor.pos() < cursor.par()->size()
2340 && (cursor.par()->isLetter(cursor.pos()))
2341 // assignment is intentional here
2342 || ((inset = getInset())
2343 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2344 && static_cast<InsetSpecialChar *>(inset)->kind()
2345 == InsetSpecialChar::HYPHENATION
2347 cursor.pos(cursor.pos() + 1);
2349 // Finally, we copy the word to a string and return it
2351 if (selection.cursor.pos() < cursor.pos()) {
2352 Paragraph::size_type i;
2353 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2354 if (cursor.par()->getChar(i) != Paragraph::META_INSET)
2355 str += cursor.par()->getChar(i);
2362 // This one is also only for the spellchecker
2363 void LyXText::selectSelectedWord(BufferView * bview)
2365 if (the_locking_inset) {
2366 the_locking_inset->selectSelectedWord(bview);
2369 // move cursor to the beginning
2370 setCursor(bview, selection.cursor.par(), selection.cursor.pos());
2372 // set the sel cursor
2373 selection.cursor = cursor;
2376 // now find the end of the word
2377 while (cursor.pos() < cursor.par()->size()
2378 && (cursor.par()->isLetter(cursor.pos())
2379 // assignment is intentional here
2380 || ((inset = getInset())
2381 && inset->lyxCode() == Inset::SPECIALCHAR_CODE
2382 && static_cast<InsetSpecialChar *>(inset)->kind()
2383 == InsetSpecialChar::HYPHENATION
2385 cursor.pos(cursor.pos() + 1);
2387 setCursor(bview, cursor.par(), cursor.pos());
2389 // finally set the selection
2390 setSelection(bview);
2394 /* -------> Delete from cursor up to the end of the current or next word. */
2395 void LyXText::deleteWordForward(BufferView * bview)
2397 if (!cursor.par()->size())
2400 LyXCursor tmpcursor = cursor;
2401 tmpcursor.row(0); //Â ??
2402 selection.set(true); // to avoid deletion
2403 cursorRightOneWord(bview);
2404 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2405 selection.cursor = cursor;
2407 setSelection(bview);
2409 /* -----> Great, CutSelection() gets rid of multiple spaces. */
2410 cutSelection(bview, true, false);
2415 /* -------> Delete from cursor to start of current or prior word. */
2416 void LyXText::deleteWordBackward(BufferView * bview)
2418 if (!cursor.par()->size())
2421 LyXCursor tmpcursor = cursor;
2422 tmpcursor.row(0); // ??
2423 selection.set(true); // to avoid deletion
2424 cursorLeftOneWord(bview);
2425 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2426 selection.cursor = cursor;
2428 setSelection(bview);
2429 cutSelection(bview, true, false);
2434 /* -------> Kill to end of line. */
2435 void LyXText::deleteLineForward(BufferView * bview)
2437 if (!cursor.par()->size())
2438 // Paragraph is empty, so we just go to the right
2441 LyXCursor tmpcursor = cursor;
2442 // We can't store the row over a regular setCursor
2443 // so we set it to 0 and reset it afterwards.
2444 tmpcursor.row(0); //Â ??
2445 selection.set(true); // to avoid deletion
2447 setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
2448 selection.cursor = cursor;
2450 setSelection(bview);
2451 // What is this test for ??? (JMarc)
2452 if (!selection.set()) {
2453 deleteWordForward(bview);
2455 cutSelection(bview, true, false);
2461 // Change the case of a word at cursor position.
2462 // This function directly manipulates Paragraph::text because there
2463 // is no Paragraph::SetChar currently. I did what I could to ensure
2464 // that it is correct. I guess part of it should be moved to
2465 // Paragraph, but it will have to change for 1.1 anyway. At least
2466 // it does not access outside of the allocated array as the older
2467 // version did. (JMarc)
2468 void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
2473 if (selection.set()) {
2474 from = selection.start;
2477 getWord(from, to, PARTIAL_WORD);
2478 setCursor(bview, to.par(), to.pos() + 1);
2481 changeRegionCase(bview, from, to, action);
2485 void LyXText::changeRegionCase(BufferView * bview,
2486 LyXCursor const & from,
2487 LyXCursor const & to,
2488 LyXText::TextCase action)
2490 lyx::Assert(from <= to);
2492 setUndo(bview, Undo::FINISH,
2493 from.par(), to.par()->next());
2495 Paragraph::size_type pos = from.pos();
2496 Paragraph * par = from.par();
2498 while (par && (pos != to.pos() || par != to.par())) {
2499 unsigned char c = par->getChar(pos);
2500 if (!IsInsetChar(c) && !IsHfillChar(c)) {
2502 case text_lowercase:
2505 case text_capitalization:
2507 action = text_lowercase;
2509 case text_uppercase:
2514 par->setChar(pos, c);
2515 checkParagraph(bview, par, pos);
2518 if (pos == par->size()) {
2523 if (to.row() != from.row()) {
2524 refresh_y = from.y() - from.row()->baseline();
2525 refresh_row = from.row();
2526 status(bview, LyXText::NEED_MORE_REFRESH);
2531 void LyXText::transposeChars(BufferView & bview)
2533 Paragraph * tmppar = cursor.par();
2535 setUndo(&bview, Undo::FINISH,
2536 tmppar, tmppar->next());
2538 Paragraph::size_type tmppos = cursor.pos();
2540 // First decide if it is possible to transpose at all
2542 // We are at the beginning of a paragraph.
2543 if (tmppos == 0) return;
2545 // We are at the end of a paragraph.
2546 if (tmppos == tmppar->size() - 1) return;
2548 unsigned char c1 = tmppar->getChar(tmppos);
2549 unsigned char c2 = tmppar->getChar(tmppos - 1);
2551 if (c1 != Paragraph::META_INSET
2552 && c2 != Paragraph::META_INSET) {
2553 tmppar->setChar(tmppos, c2);
2554 tmppar->setChar(tmppos - 1, c1);
2556 // We should have an implementation that handles insets
2557 // as well, but that will have to come later. (Lgb)
2558 checkParagraph(const_cast<BufferView*>(&bview), tmppar, tmppos);
2562 void LyXText::Delete(BufferView * bview)
2564 // this is a very easy implementation
2566 LyXCursor old_cursor = cursor;
2567 int const old_cur_par_id = old_cursor.par()->id();
2568 int const old_cur_par_prev_id = old_cursor.par()->previous() ?
2569 old_cursor.par()->previous()->id() : 0;
2571 // just move to the right
2574 // CHECK Look at the comment here.
2575 // This check is not very good...
2576 // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2577 // and that can very well delete the par or par->previous in
2578 // old_cursor. Will a solution where we compare paragraph id's
2580 if ((cursor.par()->previous() ? cursor.par()->previous()->id() : 0)
2581 == old_cur_par_prev_id
2582 && cursor.par()->id() != old_cur_par_id)
2584 return; // delete-empty-paragraph-mechanism has done it
2587 // if you had success make a backspace
2588 if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2589 LyXCursor tmpcursor = cursor;
2590 cursor = old_cursor; // to make sure undo gets the right cursor position
2591 setUndo(bview, Undo::DELETE,
2592 cursor.par(), cursor.par()->next());
2599 void LyXText::backspace(BufferView * bview)
2601 // Get the font that is used to calculate the baselineskip
2602 Paragraph::size_type lastpos = cursor.par()->size();
2603 LyXFont rawparfont =
2604 cursor.par()->getFontSettings(bview->buffer()->params,
2607 if (cursor.pos() == 0) {
2608 // The cursor is at the beginning of a paragraph,
2609 // so the the backspace will collapse two paragraphs into one.
2611 // we may paste some paragraphs
2613 // is it an empty paragraph?
2616 || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2617 // This is an empty paragraph and we delete it just by moving the cursor one step
2618 // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2619 // of the paragraph.
2621 if (cursor.par()->previous()) {
2622 Paragraph * tmppar = cursor.par()->previous();
2623 if (cursor.par()->getLayout() == tmppar->getLayout()
2624 && cursor.par()->getAlign() == tmppar->getAlign()) {
2625 // Inherit bottom DTD from the paragraph below.
2626 // (the one we are deleting)
2627 tmppar->params().lineBottom(cursor.par()->params().lineBottom());
2628 tmppar->params().spaceBottom(cursor.par()->params().spaceBottom());
2629 tmppar->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2634 // the layout things can change the height of a row !
2635 int const tmpheight = cursor.row()->height();
2636 setHeightOfRow(bview, cursor.row());
2637 if (cursor.row()->height() != tmpheight) {
2638 refresh_y = cursor.y() - cursor.row()->baseline();
2639 refresh_row = cursor.row();
2640 status(bview, LyXText::NEED_MORE_REFRESH);
2646 if (cursor.par()->previous()) {
2647 setUndo(bview, Undo::DELETE,
2648 cursor.par()->previous(), cursor.par()->next());
2651 Paragraph * tmppar = cursor.par();
2652 Row * tmprow = cursor.row();
2654 // We used to do cursorLeftIntern() here, but it is
2655 // not a good idea since it triggers the auto-delete
2656 // mechanism. So we do a cursorLeftIntern()-lite,
2657 // without the dreaded mechanism. (JMarc)
2658 if (cursor.par()->previous()) {
2659 // steps into the above paragraph.
2660 setCursorIntern(bview, cursor.par()->previous(),
2661 cursor.par()->previous()->size(),
2665 /* Pasting is not allowed, if the paragraphs have different
2666 layout. I think it is a real bug of all other
2667 word processors to allow it. It confuses the user.
2668 Even so with a footnote paragraph and a non-footnote
2669 paragraph. I will not allow pasting in this case,
2670 because the user would be confused if the footnote behaves
2671 different wether it is open or closed.
2673 Correction: Pasting is always allowed with standard-layout
2675 if (cursor.par() != tmppar
2676 && (cursor.par()->getLayout() == tmppar->getLayout()
2677 || tmppar->getLayout() == 0 /*standard*/)
2678 && cursor.par()->getAlign() == tmppar->getAlign())
2680 removeParagraph(tmprow);
2682 cursor.par()->pasteParagraph(bview->buffer()->params);
2684 if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2685 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2686 // strangely enough it seems that commenting out the line above removes
2687 // most or all of the segfaults. I will however also try to move the
2688 // two Remove... lines in front of the PasteParagraph too.
2691 cursor.pos(cursor.pos() - 1);
2693 status(bview, LyXText::NEED_MORE_REFRESH);
2694 refresh_row = cursor.row();
2695 refresh_y = cursor.y() - cursor.row()->baseline();
2697 // remove the lost paragraph
2698 // This one is not safe, since the paragraph that the tmprow and the
2699 // following rows belong to has been deleted by the PasteParagraph
2700 // above. The question is... could this be moved in front of the
2702 //RemoveParagraph(tmprow);
2703 //RemoveRow(tmprow);
2705 // This rebuilds the rows.
2706 appendParagraph(bview, cursor.row());
2707 updateCounters(bview, cursor.row());
2709 // the row may have changed, block, hfills etc.
2710 setCursor(bview, cursor.par(), cursor.pos(), false);
2713 /* this is the code for a normal backspace, not pasting
2715 setUndo(bview, Undo::DELETE,
2716 cursor.par(), cursor.par()->next());
2717 // We used to do cursorLeftIntern() here, but it is
2718 // not a good idea since it triggers the auto-delete
2719 // mechanism. So we do a cursorLeftIntern()-lite,
2720 // without the dreaded mechanism. (JMarc)
2721 setCursorIntern(bview, cursor.par(), cursor.pos()- 1,
2722 false, cursor.boundary());
2724 // some insets are undeletable here
2725 if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
2726 if (!cursor.par()->getInset(cursor.pos())->deletable())
2728 // force complete redo when erasing display insets
2729 // this is a cruel method but safe..... Matthias
2730 if (cursor.par()->getInset(cursor.pos())->display() ||
2731 cursor.par()->getInset(cursor.pos())->needFullRow()) {
2732 cursor.par()->erase(cursor.pos());
2733 redoParagraph(bview);
2738 Row * row = cursor.row();
2739 int y = cursor.y() - row->baseline();
2740 Paragraph::size_type z;
2741 /* remember that a space at the end of a row doesnt count
2742 * when calculating the fill */
2743 if (cursor.pos() < rowLast(row) ||
2744 !cursor.par()->isLineSeparator(cursor.pos())) {
2745 row->fill(row->fill() + singleWidth(bview,
2750 /* some special code when deleting a newline. This is similar
2751 * to the behavior when pasting paragraphs */
2752 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2753 cursor.par()->erase(cursor.pos());
2754 // refresh the positions
2756 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2757 tmprow = tmprow->next();
2758 tmprow->pos(tmprow->pos() - 1);
2760 if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2761 cursor.pos(cursor.pos() - 1);
2763 if (cursor.pos() < cursor.par()->size()
2764 && !cursor.par()->isSeparator(cursor.pos())) {
2765 cursor.par()->insertChar(cursor.pos(), ' ');
2766 setCharFont(bview->buffer(), cursor.par(),
2767 cursor.pos(), current_font);
2768 // refresh the positions
2770 while (tmprow->next() && tmprow->next()->par() == row->par()) {
2771 tmprow = tmprow->next();
2772 tmprow->pos(tmprow->pos() + 1);
2776 cursor.par()->erase(cursor.pos());
2778 // refresh the positions
2780 while (tmprow->next()
2781 && tmprow->next()->par() == row->par()) {
2782 tmprow = tmprow->next();
2783 tmprow->pos(tmprow->pos() - 1);
2786 // delete newlines at the beginning of paragraphs
2787 while (cursor.par()->size() &&
2788 cursor.par()->isNewline(cursor.pos()) &&
2789 cursor.pos() == beginningOfMainBody(bview->buffer(),
2791 cursor.par()->erase(cursor.pos());
2792 // refresh the positions
2794 while (tmprow->next() &&
2795 tmprow->next()->par() == row->par()) {
2796 tmprow = tmprow->next();
2797 tmprow->pos(tmprow->pos() - 1);
2802 // is there a break one row above
2803 if (row->previous() && row->previous()->par() == row->par()) {
2804 z = nextBreakPoint(bview, row->previous(),
2806 if (z >= row->pos()) {
2809 Row * tmprow = row->previous();
2811 // maybe the current row is now empty
2812 if (row->pos() >= row->par()->size()) {
2817 breakAgainOneRow(bview, row);
2818 if (row->next() && row->next()->par() == row->par())
2819 need_break_row = row->next();
2824 // set the dimensions of the row above
2825 y -= tmprow->height();
2826 tmprow->fill(fill(bview, tmprow,
2828 setHeightOfRow(bview, tmprow);
2831 refresh_row = tmprow;
2832 status(bview, LyXText::NEED_MORE_REFRESH);
2833 setCursor(bview, cursor.par(), cursor.pos(),
2834 false, cursor.boundary());
2835 //current_font = rawtmpfont;
2836 //real_current_font = realtmpfont;
2837 // check, whether the last character's font has changed.
2839 cursor.par()->getFontSettings(bview->buffer()->params,
2840 cursor.par()->size() - 1))
2841 redoHeightOfParagraph(bview, cursor);
2846 // break the cursor row again
2847 if (row->next() && row->next()->par() == row->par() &&
2848 (rowLast(row) == row->par()->size() - 1 ||
2849 nextBreakPoint(bview, row, workWidth(bview)) != rowLast(row))) {
2851 /* it can happen that a paragraph loses one row
2852 * without a real breakup. This is when a word
2853 * is to long to be broken. Well, I don t care this
2855 if (rowLast(row) == row->par()->size() - 1)
2856 removeRow(row->next());
2860 status(bview, LyXText::NEED_MORE_REFRESH);
2862 breakAgainOneRow(bview, row);
2863 // will the cursor be in another row now?
2864 if (row->next() && row->next()->par() == row->par() &&
2865 rowLast(row) <= cursor.pos()) {
2867 breakAgainOneRow(bview, row);
2870 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2872 if (row->next() && row->next()->par() == row->par())
2873 need_break_row = row->next();
2877 // set the dimensions of the row
2878 row->fill(fill(bview, row, workWidth(bview)));
2879 int const tmpheight = row->height();
2880 setHeightOfRow(bview, row);
2881 if (tmpheight == row->height())
2882 status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
2884 status(bview, LyXText::NEED_MORE_REFRESH);
2887 setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
2891 // current_font = rawtmpfont;
2892 // real_current_font = realtmpfont;
2894 if (isBoundary(bview->buffer(), cursor.par(), cursor.pos())
2895 != cursor.boundary())
2896 setCursor(bview, cursor.par(), cursor.pos(), false,
2897 !cursor.boundary());
2899 lastpos = cursor.par()->size();
2900 if (cursor.pos() == lastpos)
2901 setCurrentFont(bview);
2903 // check, whether the last characters font has changed.
2905 cursor.par()->getFontSettings(bview->buffer()->params, lastpos - 1)) {
2906 redoHeightOfParagraph(bview, cursor);
2908 // now the special right address boxes
2909 if (textclasslist.Style(bview->buffer()->params.textclass,
2910 cursor.par()->getLayout()).margintype == MARGIN_RIGHT_ADDRESS_BOX) {
2911 redoDrawingOfParagraph(bview, cursor);
2917 void LyXText::getVisibleRow(BufferView * bview, int y_offset, int x_offset,
2918 Row * row_ptr, int y, bool cleared)
2920 // returns a printed row
2921 Painter & pain = bview->painter();
2924 row_ptr->par()->isRightToLeftPar(bview->buffer()->params);
2926 Paragraph::size_type const last = rowLastPrintable(row_ptr);
2928 Paragraph::size_type vpos;
2929 Paragraph::size_type pos;
2933 LyXFont font(LyXFont::ALL_SANE);
2935 if (row_ptr->height() <= 0) {
2936 lyxerr << "LYX_ERROR: row.height: "
2937 << row_ptr->height() << endl;
2942 float fill_separator;
2944 float fill_label_hfill;
2945 prepareToPrint(bview, row_ptr, x, fill_separator,
2946 fill_hfill, fill_label_hfill);
2948 if (inset_owner && (x < 0))
2952 // clear the area where we want to paint/print
2953 int const ww = bview->workWidth();
2955 bool clear_area = true;
2958 if (!bview->screen()->forceClear() && last == row_ptr->pos()
2959 && row_ptr->par()->getChar(row_ptr->pos()) == Paragraph::META_INSET
2960 && (inset = row_ptr->par()->getInset(row_ptr->pos()))) {
2961 clear_area = inset->doClearArea();
2963 // we don't need to clear it's already done!!!
2966 } else if (clear_area) {
2967 int const y = y_offset < 0 ? 0 : y_offset;
2968 int const h = y_offset < 0 ?
2969 row_ptr->height() + y_offset : row_ptr->height();
2970 int const w = inset_owner ?
2971 inset_owner->textWidth(bview, true) : ww;
2972 int const x = x_offset;
2973 pain.fillRectangle(x, y, w, h, backgroundColor());
2974 } else if (inset != 0) {
2975 int h = row_ptr->baseline() - inset->ascent(bview, font);
2976 // first clear the whole row above the inset!
2978 int const w = (inset_owner ?
2979 inset_owner->textWidth(bview, true) : ww);
2980 pain.fillRectangle(x_offset, y_offset, w, 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) {
2986 int const w = (inset_owner ?
2987 inset_owner->textWidth(bview, true) : ww);
2988 pain.fillRectangle(x_offset, y_offset + h,
2989 w, row_ptr->height() - h,
2992 // clear the space behind the inset, if needed
2993 if (!inset->display() && !inset->needFullRow()) {
2994 int const w = (inset_owner ?
2995 inset_owner->textWidth(bview, true) : ww);
2996 int const xp = int(x) + inset->width(bview, font);
2998 pain.fillRectangle(xp, y_offset,
2999 w-xp, row_ptr->height(),
3005 if (selection.set()) {
3006 int const w = (inset_owner ?
3007 inset_owner->textWidth(bview, true) : ww);
3009 if (bidi_same_direction) {
3010 if (selection.start.row() == row_ptr &&
3011 selection.end.row() == row_ptr) {
3012 if (selection.start.x() < selection.end.x())
3013 pain.fillRectangle(x_offset + selection.start.x(),
3015 selection.end.x() - selection.start.x(),
3019 pain.fillRectangle(x_offset + selection.end.x(),
3021 selection.start.x() - selection.end.x(),
3024 } else if (selection.start.row() == row_ptr) {
3026 pain.fillRectangle(x_offset, y_offset,
3027 selection.start.x(),
3031 pain.fillRectangle(x_offset + selection.start.x(),
3033 w - selection.start.x(),
3036 } else if (selection.end.row() == row_ptr) {
3038 pain.fillRectangle(x_offset + selection.end.x(),
3040 w - selection.end.x(),
3044 pain.fillRectangle(x_offset, y_offset,
3048 } else if (y > selection.start.y()
3049 && y < selection.end.y()) {
3050 pain.fillRectangle(x_offset, y_offset, w,
3054 } else if (selection.start.row() != row_ptr &&
3055 selection.end.row() != row_ptr &&
3056 y > selection.start.y()
3057 && y < selection.end.y()) {
3058 pain.fillRectangle(x_offset, y_offset, w,
3061 } else if (selection.start.row() == row_ptr ||
3062 selection.end.row() == row_ptr) {
3064 if ((selection.start.row() != row_ptr && !is_rtl) ||
3065 (selection.end.row() != row_ptr && is_rtl))
3066 pain.fillRectangle(x_offset, y_offset,
3070 Paragraph::size_type main_body =
3071 beginningOfMainBody(bview->buffer(),
3074 for (vpos = row_ptr->pos(); vpos <= last; ++vpos) {
3075 pos = vis2log(vpos);
3076 float const old_tmpx = tmpx;
3077 if (main_body > 0 && pos == main_body-1) {
3078 tmpx += fill_label_hfill +
3079 lyxfont::width(textclasslist.Style(bview->buffer()->params.textclass,
3080 row_ptr->par()->getLayout()).labelsep,
3081 getLabelFont(bview->buffer(),row_ptr->par()));
3082 if (row_ptr->par()->isLineSeparator(main_body-1))
3083 tmpx -= singleWidth(bview, row_ptr->par(), main_body-1);
3085 if (hfillExpansion(bview->buffer(), row_ptr, pos)) {
3086 tmpx += singleWidth(bview, row_ptr->par(), pos);
3087 if (pos >= main_body)
3090 tmpx += fill_label_hfill;
3092 else if (row_ptr->par()->isSeparator(pos)) {
3093 tmpx += singleWidth(bview, row_ptr->par(), pos);
3094 if (pos >= main_body)
3095 tmpx += fill_separator;
3097 tmpx += singleWidth(bview, row_ptr->par(), pos);
3099 if ((selection.start.row() != row_ptr ||
3100 selection.start.pos() <= pos) &&
3101 (selection.end.row() != row_ptr ||
3102 pos < selection.end.pos()) )
3103 // Here we do not use x_offset as x_offset was
3105 pain.fillRectangle(int(old_tmpx),
3107 int(tmpx - old_tmpx + 1),
3112 if ((selection.start.row() != row_ptr && is_rtl) ||
3113 (selection.end.row() != row_ptr && !is_rtl) )
3114 pain.fillRectangle(x_offset + int(tmpx),
3124 // Draw appendix lines
3125 Paragraph * firstpar = row_ptr->par();
3127 if (firstpar->params().appendix()) {
3128 pain.line(1, y_offset,
3129 1, y_offset + row_ptr->height(),
3130 LColor::appendixline);
3131 pain.line(ww - 2, y_offset,
3132 ww - 2, y_offset + row_ptr->height(),
3133 LColor::appendixline);
3137 Paragraph::depth_type const depth = firstpar->getDepth();
3139 Paragraph::depth_type next_depth = 0;
3140 Paragraph::depth_type prev_depth = 0;
3141 if (row_ptr->next())
3142 next_depth = row_ptr->next()->par()->getDepth();
3143 if (row_ptr->previous())
3144 prev_depth = row_ptr->previous()->par()->getDepth();
3146 for (Paragraph::depth_type i = 1; i <= depth; ++i) {
3147 int const line_x = (LYX_PAPER_MARGIN / 5) *
3148 i + box_x + x_offset;
3149 pain.line(line_x, y_offset, line_x,
3150 y_offset + row_ptr->height() - 1 - (i - next_depth - 1) * 3,
3154 pain.fillRectangle(line_x, y_offset, LYX_PAPER_MARGIN / 5, 2,
3157 pain.fillRectangle(line_x,
3158 y_offset + row_ptr->height() - 2 - (i - next_depth - 1) * 3,
3159 LYX_PAPER_MARGIN / 5, 2,
3165 LyXLayout const & layout =
3166 textclasslist.Style(bview->buffer()->params.textclass,
3167 row_ptr->par()->getLayout());
3170 int y_bottom = row_ptr->height();
3172 // is it a first row?
3173 if (!row_ptr->pos() && (row_ptr->par() == firstpar)) {
3175 // start of appendix?
3176 if (row_ptr->par()->params().startOfAppendix()) {
3177 pain.line(1, y_offset,
3179 LColor::appendixline);
3182 // think about the margins
3183 if (!row_ptr->previous() && bv_owner)
3184 y_top += LYX_PAPER_MARGIN;
3186 // draw a top pagebreak
3187 if (row_ptr->par()->params().pagebreakTop()) {
3189 pb_font.setColor(LColor::pagebreak).decSize();
3193 pain.line(0, y_offset + y_top + 2*defaultHeight(),
3195 y_offset + y_top + 2 * defaultHeight(),
3197 Painter::line_onoffdash);
3198 lyxfont::rectText(_("Page Break (top)"), pb_font,
3200 pain.rectText((ww - w)/2,
3201 y_offset + y_top + 2 * defaultHeight() + d,
3202 _("Page Break (top)"),
3206 y_top += 3 * defaultHeight();
3209 if (row_ptr->par()->params().spaceTop().kind() == VSpace::VFILL) {
3211 pain.line(0, y_offset + 2 + y_top,
3212 LYX_PAPER_MARGIN, y_offset + 2 + y_top,
3215 pain.line(0, y_offset + y_top + 3 * defaultHeight(),
3217 y_offset + y_top + 3 * defaultHeight(),
3220 pain.line(LYX_PAPER_MARGIN / 2, y_offset + 2 + y_top,
3221 LYX_PAPER_MARGIN / 2,
3222 y_offset + y_top + 3 * defaultHeight(),
3225 y_top += 3 * defaultHeight();
3228 // think about user added space
3229 y_top += int(row_ptr->par()->params().spaceTop().inPixels(bview));
3231 // think about the parskip
3232 // some parskips VERY EASY IMPLEMENTATION
3233 if (bview->buffer()->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
3234 if (layout.latextype == LATEX_PARAGRAPH
3235 && firstpar->getDepth() == 0
3236 && firstpar->previous())
3237 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3238 else if (firstpar->previous()
3239 && textclasslist.Style(bview->buffer()->params.textclass,
3240 firstpar->previous()->getLayout()).latextype == LATEX_PARAGRAPH
3241 && firstpar->previous()->getDepth() == 0)
3242 // is it right to use defskip here, too? (AS)
3243 y_top += bview->buffer()->params.getDefSkip().inPixels(bview);
3246 if (row_ptr->par()->params().lineTop()) {
3248 y_top += lyxfont::ascent('x',
3249 getFont(bview->buffer(),
3250 row_ptr->par(), 0));
3251 int const w = (inset_owner ?
3252 inset_owner->width(bview, font) : ww);
3253 int const xp = static_cast<int>(inset_owner ? x : 0);
3254 pain.line(xp, y_offset + y_top,
3255 w, y_offset + y_top,
3257 Painter::line_solid,
3258 Painter::line_thick);
3260 y_top += lyxfont::ascent('x',getFont(bview->buffer(),
3261 row_ptr->par(), 0));
3264 // should we print a label?
3265 if (layout.labeltype >= LABEL_STATIC
3266 && (layout.labeltype != LABEL_STATIC
3267 || layout.latextype != LATEX_ENVIRONMENT
3268 || row_ptr->par()->isFirstInSequence())) {
3269 font = getLabelFont(bview->buffer(), row_ptr->par());
3270 if (!row_ptr->par()->getLabelstring().empty()) {
3272 string const tmpstring =
3273 row_ptr->par()->getLabelstring();
3275 if (layout.labeltype == LABEL_COUNTER_CHAPTER) {
3276 if (bview->buffer()->params.secnumdepth >= 0) {
3277 // this is special code for
3278 // the chapter layout. This is
3279 // printed in an extra row
3280 // and has a pagebreak at
3282 float spacing_val = 1.0;
3283 if (!row_ptr->par()->params().spacing().isDefault()) {
3284 spacing_val = row_ptr->par()->params().spacing().getValue();
3286 spacing_val = bview->buffer()->params.spacing.getValue();
3289 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val)
3290 + int(layout.parsep) * defaultHeight();
3292 tmpx = ww - leftMargin(bview, row_ptr) -
3293 lyxfont::width(tmpstring, font);
3294 pain.text(int(tmpx),
3295 y_offset + row_ptr->baseline() - row_ptr->ascent_of_text() - maxdesc,
3300 tmpx = ww - leftMargin(bview, row_ptr)
3301 + lyxfont::width(layout.labelsep, font);
3303 tmpx = x - lyxfont::width(layout.labelsep, font)
3304 - lyxfont::width(tmpstring, font);
3307 pain.text(int(tmpx),
3308 y_offset + row_ptr->baseline(),
3312 // the labels at the top of an environment.
3313 // More or less for bibliography
3314 } else if (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
3315 layout.labeltype == LABEL_BIBLIO ||
3316 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
3317 if (row_ptr->par()->isFirstInSequence()) {
3318 font = getLabelFont(bview->buffer(),
3320 if (!row_ptr->par()->getLabelstring().empty()) {
3321 string const tmpstring =
3322 row_ptr->par()->getLabelstring();
3323 float spacing_val = 1.0;
3324 if (!row_ptr->par()->params().spacing().isDefault()) {
3325 spacing_val = row_ptr->par()->params().spacing().getValue();
3327 spacing_val = bview->buffer()->params.spacing.getValue();
3330 maxdesc = int(lyxfont::maxDescent(font) * layout.spacing.getValue() * spacing_val
3331 + (layout.labelbottomsep * defaultHeight()));
3334 if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT){
3335 tmpx = ( (is_rtl ? leftMargin(bview, row_ptr) : x)
3336 + ww - rightMargin(bview->buffer(), row_ptr) ) / 2;
3337 tmpx -= lyxfont::width(tmpstring, font) / 2;
3339 tmpx = ww - leftMargin(bview, row_ptr) -
3340 lyxfont::width(tmpstring, font);
3341 pain.text(int(tmpx),
3342 y_offset + row_ptr->baseline()
3343 - row_ptr->ascent_of_text()
3349 if (layout.labeltype == LABEL_BIBLIO && row_ptr->par()->bibkey) {
3350 font = getLayoutFont(bview->buffer(), row_ptr->par());
3352 tmpx = ww - leftMargin(bview, row_ptr)
3353 + lyxfont::width(layout.labelsep, font);
3355 tmpx = x - lyxfont::width(layout.labelsep, font)
3356 - row_ptr->par()->bibkey->width(bview, font);
3357 row_ptr->par()->bibkey->draw(bview, font,
3358 y_offset + row_ptr->baseline(),
3363 // is it a last row?
3364 Paragraph * par = row_ptr->par();
3365 if (row_ptr->par() == par
3366 && (!row_ptr->next() || row_ptr->next()->par() != row_ptr->par())) {
3367 // think about the margins
3368 if (!row_ptr->next() && bv_owner)
3369 y_bottom -= LYX_PAPER_MARGIN;
3371 // draw a bottom pagebreak
3372 if (firstpar->params().pagebreakBottom()) {
3374 pb_font.setColor(LColor::pagebreak).decSize();
3375 int const y_place = y_offset + y_bottom
3376 - 2 * defaultHeight();
3382 .line(0, y_place, ww, y_place,
3384 Painter::line_onoffdash);
3385 lyxfont::rectText(_("Page Break (bottom)"), pb_font,
3387 pain.rectText((ww - w) / 2, y_place + d,
3388 _("Page Break (bottom)"),
3392 y_bottom -= 3 * defaultHeight();
3395 if (firstpar->params().spaceBottom().kind() == VSpace::VFILL) {
3396 // draw a vfill bottom
3397 int const y_place = y_offset + y_bottom
3398 - 3 * defaultHeight();
3400 pain.line(0, y_place,
3401 LYX_PAPER_MARGIN, y_place,
3403 pain.line(0, y_offset + y_bottom - 2,
3405 y_offset + y_bottom - 2,
3407 pain.line(LYX_PAPER_MARGIN / 2,
3409 LYX_PAPER_MARGIN / 2,
3410 y_offset + y_bottom - 2,
3412 y_bottom -= 3 * defaultHeight();
3415 // think about user added space
3416 y_bottom -= int(firstpar->params().spaceBottom().inPixels(bview));
3418 if (firstpar->params().lineBottom()) {
3419 // draw a bottom line
3420 y_bottom -= lyxfont::ascent('x',
3421 getFont(bview->buffer(),
3424 int const w = (inset_owner ?
3425 inset_owner->width(bview, font) : ww);
3426 int const xp = static_cast<int>(inset_owner ? x : 0);
3427 pain.line(xp, y_offset + y_bottom,
3428 w, y_offset + y_bottom,
3429 LColor::topline, Painter::line_solid,
3430 Painter::line_thick);
3431 y_bottom -= lyxfont::ascent('x',
3432 getFont(bview->buffer(),
3438 int const endlabel =
3439 row_ptr->par()->getEndLabel(bview->buffer()->params);
3442 case END_LABEL_FILLED_BOX:
3444 LyXFont const font = getFont(bview->buffer(),
3445 row_ptr->par(), last);
3446 int const size = int(0.75 * lyxfont::maxAscent(font));
3447 int const y = (y_offset + row_ptr->baseline()) - size;
3448 int x = is_rtl ? LYX_PAPER_MARGIN
3449 : ww - LYX_PAPER_MARGIN - size;
3451 if (row_ptr->fill() <= size)
3452 x += (size - row_ptr->fill() + 1) * (is_rtl ? -1 : 1);
3453 if (endlabel == END_LABEL_BOX) {
3454 pain.line(x, y, x, y + size,
3456 pain.line(x + size, y, x + size , y + size,
3458 pain.line(x, y, x + size, y,
3460 pain.line(x, y + size, x + size, y + size,
3463 pain.fillRectangle(x, y, size, size,
3467 case END_LABEL_STATIC:
3469 LyXTextClass::LayoutList::size_type layout = row_ptr->par()->getLayout();
3470 string const tmpstring = textclasslist.
3471 Style(bview->buffer()->params.textclass,
3472 layout).endlabelstring();
3473 font = getLabelFont(bview->buffer(), row_ptr->par());
3474 int const tmpx = is_rtl ?
3475 int(x) - lyxfont::width(tmpstring, font)
3476 : ww - rightMargin(bview->buffer(), row_ptr) - row_ptr->fill();
3477 pain.text( tmpx, y_offset + row_ptr->baseline(), tmpstring, font);
3480 case END_LABEL_NO_LABEL:
3485 // draw the text in the pixmap
3487 vpos = row_ptr->pos();
3489 Paragraph::size_type main_body =
3490 beginningOfMainBody(bview->buffer(), row_ptr->par());
3491 if (main_body > 0 &&
3492 (main_body-1 > last ||
3493 !row_ptr->par()->isLineSeparator(main_body - 1)))
3496 while (vpos <= last) {
3497 pos = vis2log(vpos);
3498 if (main_body > 0 && pos == main_body - 1) {
3499 x += fill_label_hfill
3500 + lyxfont::width(layout.labelsep,
3501 getLabelFont(bview->buffer(),
3503 - singleWidth(bview,
3508 if (row_ptr->par() ->isHfill(pos)) {
3511 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3513 y_offset + row_ptr->baseline(),
3516 if (hfillExpansion(bview->buffer(),
3518 if (pos >= main_body) {
3520 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3521 int(x + fill_hfill),
3522 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3524 Painter::line_onoffdash);
3528 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3529 int(x + fill_label_hfill),
3530 y_offset + row_ptr->baseline() - defaultHeight() / 4,
3532 Painter::line_onoffdash);
3534 x += fill_label_hfill;
3537 y_offset + row_ptr->baseline() - defaultHeight() / 2,
3539 y_offset + row_ptr->baseline(),
3544 } else if (row_ptr->par()->isSeparator(pos)) {
3545 x += singleWidth(bview,
3546 row_ptr->par(), pos);
3547 if (pos >= main_body)
3548 x += fill_separator;
3551 draw(bview, row_ptr, vpos, y_offset, x, clear_area);
3556 int LyXText::defaultHeight() const
3558 LyXFont font(LyXFont::ALL_SANE);
3559 return int(lyxfont::maxAscent(font) + lyxfont::maxDescent(font) * 1.5);
3563 /* returns the column near the specified x-coordinate of the row
3564 * x is set to the real beginning of this column */
3565 int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
3566 bool & boundary) const
3569 float fill_separator;
3571 float fill_label_hfill;
3573 prepareToPrint(bview, row, tmpx, fill_separator,
3574 fill_hfill, fill_label_hfill);
3576 Paragraph::size_type vc = row->pos();
3577 Paragraph::size_type last = rowLastPrintable(row);
3578 Paragraph::size_type c = 0;
3579 LyXLayout const & layout =
3580 textclasslist.Style(bview->buffer()->params.textclass,
3581 row->par()->getLayout());
3582 bool left_side = false;
3584 Paragraph::size_type
3585 main_body = beginningOfMainBody(bview->buffer(), row->par());
3586 float last_tmpx = tmpx;
3588 if (main_body > 0 &&
3589 (main_body - 1 > last ||
3590 !row->par()->isLineSeparator(main_body - 1)))
3593 while (vc <= last && tmpx <= x) {
3596 if (main_body > 0 && c == main_body-1) {
3597 tmpx += fill_label_hfill +
3598 lyxfont::width(layout.labelsep,
3599 getLabelFont(bview->buffer(), row->par()));
3600 if (row->par()->isLineSeparator(main_body - 1))
3601 tmpx -= singleWidth(bview, row->par(), main_body-1);
3604 if (hfillExpansion(bview->buffer(), row, c)) {
3605 x += singleWidth(bview, row->par(), c);
3609 tmpx += fill_label_hfill;
3611 else if (row->par()->isSeparator(c)) {
3612 tmpx += singleWidth(bview, row->par(), c);
3614 tmpx+= fill_separator;
3616 tmpx += singleWidth(bview, row->par(), c);
3620 if ((tmpx + last_tmpx) / 2 > x) {
3625 if (vc > last + 1) // This shouldn't happen.
3629 bool const lastrow = lyxrc.rtl_support // This is not needed, but gives
3630 // some speedup if rtl_support=false
3631 && (!row->next() || row->next()->par() != row->par());
3632 bool const rtl = (lastrow)
3633 ? row->par()->isRightToLeftPar(bview->buffer()->params)
3634 : false; // If lastrow is false, we don't need to compute
3635 // the value of rtl.
3637 if (row->pos() > last) // Row is empty?
3640 ( ( rtl && left_side && vc == row->pos() && x < tmpx - 5) ||
3641 (!rtl && !left_side && vc == last + 1 && x > tmpx + 5) ))
3643 else if (vc == row->pos()) {
3645 if (bidi_level(c) % 2 == 1)
3648 c = vis2log(vc - 1);
3649 bool const rtl = (bidi_level(c) % 2 == 1);
3650 if (left_side == rtl) {
3652 boundary = isBoundary(bview->buffer(), row->par(), c);
3656 if (row->pos() <= last && c > last
3657 && row->par()->isNewline(last)) {
3658 if (bidi_level(last) % 2 == 0)
3659 tmpx -= singleWidth(bview, row->par(), last);
3661 tmpx += singleWidth(bview, row->par(), last);
3671 // returns pointer to a specified row
3672 Row * LyXText::getRow(Paragraph * par,
3673 Paragraph::size_type pos, int & y) const
3678 Row * tmprow = firstrow;
3681 // find the first row of the specified paragraph
3682 while (tmprow->next() && tmprow->par() != par) {
3683 y += tmprow->height();
3684 tmprow = tmprow->next();
3687 // now find the wanted row
3688 while (tmprow->pos() < pos
3690 && tmprow->next()->par() == par
3691 && tmprow->next()->pos() <= pos) {
3692 y += tmprow->height();
3693 tmprow = tmprow->next();